2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
16 * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
17 * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/highmem.h>
25 #include <linux/gfp.h>
26 #include <linux/slab.h>
27 #include <linux/hugetlb.h>
28 #include <linux/list.h>
29 #include <linux/stringify.h>
31 #include <asm/kvm_ppc.h>
32 #include <asm/kvm_book3s.h>
33 #include <asm/book3s/64/mmu-hash.h>
34 #include <asm/mmu_context.h>
35 #include <asm/hvcall.h>
36 #include <asm/synch.h>
37 #include <asm/ppc-opcode.h>
38 #include <asm/kvm_host.h>
40 #include <asm/iommu.h>
42 #include <asm/pte-walk.h>
46 #define WARN_ON_ONCE_RM(condition) ({ \
47 static bool __section(.data.unlikely) __warned; \
48 int __ret_warn_once = !!(condition); \
50 if (unlikely(__ret_warn_once && !__warned)) { \
52 pr_err("WARN_ON_ONCE_RM: (%s) at %s:%u\n", \
53 __stringify(condition), \
54 __func__, __LINE__); \
57 unlikely(__ret_warn_once); \
62 #define WARN_ON_ONCE_RM(condition) ({ \
63 int __ret_warn_on = !!(condition); \
64 unlikely(__ret_warn_on); \
69 #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
72 * Finds a TCE table descriptor by LIOBN.
74 * WARNING: This will be called in real or virtual mode on HV KVM and virtual
77 struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
80 struct kvmppc_spapr_tce_table *stt;
82 list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
83 if (stt->liobn == liobn)
88 EXPORT_SYMBOL_GPL(kvmppc_find_table);
91 * Validates TCE address.
92 * At the moment flags and page mask are validated.
93 * As the host kernel does not access those addresses (just puts them
94 * to the table and user space is supposed to process them), we can skip
95 * checking other things (such as TCE is a guest RAM address or the page
96 * was actually allocated).
98 * WARNING: This will be called in real-mode on HV KVM and virtual
101 long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt, unsigned long tce)
103 unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
104 enum dma_data_direction dir = iommu_tce_direction(tce);
106 /* Allow userspace to poison TCE table */
110 if (iommu_tce_check_gpa(stt->page_shift, gpa))
115 EXPORT_SYMBOL_GPL(kvmppc_tce_validate);
117 /* Note on the use of page_address() in real mode,
119 * It is safe to use page_address() in real mode on ppc64 because
120 * page_address() is always defined as lowmem_page_address()
121 * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
122 * operation and does not access page struct.
124 * Theoretically page_address() could be defined different
125 * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
126 * would have to be enabled.
127 * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
128 * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
129 * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
130 * is not expected to be enabled on ppc32, page_address()
131 * is safe for ppc32 as well.
133 * WARNING: This will be called in real-mode on HV KVM and virtual
136 static u64 *kvmppc_page_address(struct page *page)
138 #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
139 #error TODO: fix to avoid page_address() here
141 return (u64 *) page_address(page);
145 * Handles TCE requests for emulated devices.
146 * Puts guest TCE values to the table and expects user space to convert them.
147 * Called in both real and virtual modes.
148 * Cannot fail so kvmppc_tce_validate must be called before it.
150 * WARNING: This will be called in real-mode on HV KVM and virtual
153 void kvmppc_tce_put(struct kvmppc_spapr_tce_table *stt,
154 unsigned long idx, unsigned long tce)
160 page = stt->pages[idx / TCES_PER_PAGE];
161 tbl = kvmppc_page_address(page);
163 tbl[idx % TCES_PER_PAGE] = tce;
165 EXPORT_SYMBOL_GPL(kvmppc_tce_put);
167 long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
168 unsigned long *ua, unsigned long **prmap)
170 unsigned long gfn = gpa >> PAGE_SHIFT;
171 struct kvm_memory_slot *memslot;
173 memslot = search_memslots(kvm_memslots(kvm), gfn);
177 *ua = __gfn_to_hva_memslot(memslot, gfn) |
178 (gpa & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
180 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
182 *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
187 EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua);
189 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
190 static long iommu_tce_xchg_rm(struct mm_struct *mm, struct iommu_table *tbl,
191 unsigned long entry, unsigned long *hpa,
192 enum dma_data_direction *direction)
196 ret = tbl->it_ops->exchange_rm(tbl, entry, hpa, direction);
198 if (!ret && ((*direction == DMA_FROM_DEVICE) ||
199 (*direction == DMA_BIDIRECTIONAL))) {
200 __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
202 * kvmppc_rm_tce_iommu_do_map() updates the UA cache after
203 * calling this so we still get here a valid UA.
206 mm_iommu_ua_mark_dirty_rm(mm, be64_to_cpu(*pua));
212 static void kvmppc_rm_clear_tce(struct kvm *kvm, struct iommu_table *tbl,
215 unsigned long hpa = 0;
216 enum dma_data_direction dir = DMA_NONE;
218 iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
221 static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm *kvm,
222 struct iommu_table *tbl, unsigned long entry)
224 struct mm_iommu_table_group_mem_t *mem = NULL;
225 const unsigned long pgsize = 1ULL << tbl->it_page_shift;
226 __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
229 /* it_userspace allocation might be delayed */
232 mem = mm_iommu_lookup_rm(kvm->mm, be64_to_cpu(*pua), pgsize);
236 mm_iommu_mapped_dec(mem);
238 *pua = cpu_to_be64(0);
243 static long kvmppc_rm_tce_iommu_do_unmap(struct kvm *kvm,
244 struct iommu_table *tbl, unsigned long entry)
246 enum dma_data_direction dir = DMA_NONE;
247 unsigned long hpa = 0;
250 if (iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir))
252 * real mode xchg can fail if struct page crosses
260 ret = kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
262 iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
267 static long kvmppc_rm_tce_iommu_unmap(struct kvm *kvm,
268 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
271 unsigned long i, ret = H_SUCCESS;
272 unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
273 unsigned long io_entry = entry * subpages;
275 for (i = 0; i < subpages; ++i) {
276 ret = kvmppc_rm_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
277 if (ret != H_SUCCESS)
284 static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
285 unsigned long entry, unsigned long ua,
286 enum dma_data_direction dir)
289 unsigned long hpa = 0;
290 __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
291 struct mm_iommu_table_group_mem_t *mem;
294 /* it_userspace allocation might be delayed */
297 mem = mm_iommu_lookup_rm(kvm->mm, ua, 1ULL << tbl->it_page_shift);
301 if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, tbl->it_page_shift,
305 if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem)))
308 ret = iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
310 mm_iommu_mapped_dec(mem);
312 * real mode xchg can fail if struct page crosses
319 kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
321 *pua = cpu_to_be64(ua);
326 static long kvmppc_rm_tce_iommu_map(struct kvm *kvm,
327 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
328 unsigned long entry, unsigned long ua,
329 enum dma_data_direction dir)
331 unsigned long i, pgoff, ret = H_SUCCESS;
332 unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
333 unsigned long io_entry = entry * subpages;
335 for (i = 0, pgoff = 0; i < subpages;
336 ++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {
338 ret = kvmppc_rm_tce_iommu_do_map(kvm, tbl,
339 io_entry + i, ua + pgoff, dir);
340 if (ret != H_SUCCESS)
347 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
348 unsigned long ioba, unsigned long tce)
350 struct kvmppc_spapr_tce_table *stt;
352 struct kvmppc_spapr_tce_iommu_table *stit;
353 unsigned long entry, ua = 0;
354 enum dma_data_direction dir;
356 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
357 /* liobn, ioba, tce); */
359 /* For radix, we might be in virtual mode, so punt */
360 if (kvm_is_radix(vcpu->kvm))
363 stt = kvmppc_find_table(vcpu->kvm, liobn);
367 ret = kvmppc_ioba_validate(stt, ioba, 1);
368 if (ret != H_SUCCESS)
371 ret = kvmppc_tce_validate(stt, tce);
372 if (ret != H_SUCCESS)
375 dir = iommu_tce_direction(tce);
376 if ((dir != DMA_NONE) && kvmppc_gpa_to_ua(vcpu->kvm,
377 tce & ~(TCE_PCI_READ | TCE_PCI_WRITE), &ua, NULL))
380 entry = ioba >> stt->page_shift;
382 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
384 ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
387 ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
388 stit->tbl, entry, ua, dir);
390 if (ret == H_SUCCESS)
393 if (ret == H_TOO_HARD)
397 kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
400 kvmppc_tce_put(stt, entry, tce);
405 static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu,
406 unsigned long ua, unsigned long *phpa)
412 * Called in real mode with MSR_EE = 0. We are safe here.
413 * It is ok to do the lookup with arch.pgdir here, because
414 * we are doing this on secondary cpus and current task there
415 * is not the hypervisor. Also this is safe against THP in the
416 * host, because an IPI to primary thread will wait for the secondary
417 * to exit which will agains result in the below page table walk
420 ptep = __find_linux_pte(vcpu->arch.pgdir, ua, NULL, &shift);
421 if (!ptep || !pte_present(*ptep))
428 /* Avoid handling anything potentially complicated in realmode */
429 if (shift > PAGE_SHIFT)
435 *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
441 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
442 unsigned long liobn, unsigned long ioba,
443 unsigned long tce_list, unsigned long npages)
445 struct kvmppc_spapr_tce_table *stt;
446 long i, ret = H_SUCCESS;
447 unsigned long tces, entry, ua = 0;
448 unsigned long *rmap = NULL;
450 struct kvmppc_spapr_tce_iommu_table *stit;
452 /* For radix, we might be in virtual mode, so punt */
453 if (kvm_is_radix(vcpu->kvm))
456 stt = kvmppc_find_table(vcpu->kvm, liobn);
460 entry = ioba >> stt->page_shift;
462 * The spec says that the maximum size of the list is 512 TCEs
463 * so the whole table addressed resides in 4K page
468 if (tce_list & (SZ_4K - 1))
471 ret = kvmppc_ioba_validate(stt, ioba, npages);
472 if (ret != H_SUCCESS)
475 if (mm_iommu_preregistered(vcpu->kvm->mm)) {
477 * We get here if guest memory was pre-registered which
478 * is normally VFIO case and gpa->hpa translation does not
481 struct mm_iommu_table_group_mem_t *mem;
483 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL))
486 mem = mm_iommu_lookup_rm(vcpu->kvm->mm, ua, IOMMU_PAGE_SIZE_4K);
488 prereg = mm_iommu_ua_to_hpa_rm(mem, ua,
489 IOMMU_PAGE_SHIFT_4K, &tces) == 0;
494 * This is usually a case of a guest with emulated devices only
495 * when TCE list is not in preregistered memory.
496 * We do not require memory to be preregistered in this case
497 * so lock rmap and do __find_linux_pte_or_hugepte().
499 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, &rmap))
502 rmap = (void *) vmalloc_to_phys(rmap);
503 if (WARN_ON_ONCE_RM(!rmap))
507 * Synchronize with the MMU notifier callbacks in
508 * book3s_64_mmu_hv.c (kvm_unmap_hva_range_hv etc.).
509 * While we have the rmap lock, code running on other CPUs
510 * cannot finish unmapping the host real page that backs
511 * this guest real page, so we are OK to access the host
515 if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) {
521 for (i = 0; i < npages; ++i) {
522 unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
524 ret = kvmppc_tce_validate(stt, tce);
525 if (ret != H_SUCCESS)
529 if (kvmppc_gpa_to_ua(vcpu->kvm,
530 tce & ~(TCE_PCI_READ | TCE_PCI_WRITE),
536 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
537 ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
538 stit->tbl, entry + i, ua,
539 iommu_tce_direction(tce));
541 if (ret == H_SUCCESS)
544 if (ret == H_TOO_HARD)
548 kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
551 kvmppc_tce_put(stt, entry + i, tce);
561 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
562 unsigned long liobn, unsigned long ioba,
563 unsigned long tce_value, unsigned long npages)
565 struct kvmppc_spapr_tce_table *stt;
567 struct kvmppc_spapr_tce_iommu_table *stit;
569 /* For radix, we might be in virtual mode, so punt */
570 if (kvm_is_radix(vcpu->kvm))
573 stt = kvmppc_find_table(vcpu->kvm, liobn);
577 ret = kvmppc_ioba_validate(stt, ioba, npages);
578 if (ret != H_SUCCESS)
581 /* Check permission bits only to allow userspace poison TCE for debug */
582 if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
585 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
586 unsigned long entry = ioba >> stt->page_shift;
588 for (i = 0; i < npages; ++i) {
589 ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
590 stit->tbl, entry + i);
592 if (ret == H_SUCCESS)
595 if (ret == H_TOO_HARD)
599 kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
603 for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
604 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
609 /* This can be called in either virtual mode or real mode */
610 long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
613 struct kvmppc_spapr_tce_table *stt;
619 stt = kvmppc_find_table(vcpu->kvm, liobn);
623 ret = kvmppc_ioba_validate(stt, ioba, 1);
624 if (ret != H_SUCCESS)
627 idx = (ioba >> stt->page_shift) - stt->offset;
628 page = stt->pages[idx / TCES_PER_PAGE];
629 tbl = (u64 *)page_address(page);
631 vcpu->arch.regs.gpr[4] = tbl[idx % TCES_PER_PAGE];
635 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
637 #endif /* KVM_BOOK3S_HV_POSSIBLE */