1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
5 * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
6 * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/kvm.h>
12 #include <linux/kvm_host.h>
13 #include <linux/highmem.h>
14 #include <linux/gfp.h>
15 #include <linux/slab.h>
16 #include <linux/hugetlb.h>
17 #include <linux/list.h>
18 #include <linux/stringify.h>
20 #include <asm/kvm_ppc.h>
21 #include <asm/kvm_book3s.h>
22 #include <asm/book3s/64/mmu-hash.h>
23 #include <asm/mmu_context.h>
24 #include <asm/hvcall.h>
25 #include <asm/synch.h>
26 #include <asm/ppc-opcode.h>
27 #include <asm/kvm_host.h>
29 #include <asm/iommu.h>
31 #include <asm/pte-walk.h>
35 #define WARN_ON_ONCE_RM(condition) ({ \
36 static bool __section(.data.unlikely) __warned; \
37 int __ret_warn_once = !!(condition); \
39 if (unlikely(__ret_warn_once && !__warned)) { \
41 pr_err("WARN_ON_ONCE_RM: (%s) at %s:%u\n", \
42 __stringify(condition), \
43 __func__, __LINE__); \
46 unlikely(__ret_warn_once); \
51 #define WARN_ON_ONCE_RM(condition) ({ \
52 int __ret_warn_on = !!(condition); \
53 unlikely(__ret_warn_on); \
59 * Finds a TCE table descriptor by LIOBN.
61 * WARNING: This will be called in real or virtual mode on HV KVM and virtual
64 struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
67 struct kvmppc_spapr_tce_table *stt;
69 list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
70 if (stt->liobn == liobn)
75 EXPORT_SYMBOL_GPL(kvmppc_find_table);
77 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
78 static long kvmppc_rm_tce_to_ua(struct kvm *kvm, unsigned long tce,
79 unsigned long *ua, unsigned long **prmap)
81 unsigned long gfn = tce >> PAGE_SHIFT;
82 struct kvm_memory_slot *memslot;
84 memslot = search_memslots(kvm_memslots_raw(kvm), gfn);
88 *ua = __gfn_to_hva_memslot(memslot, gfn) |
89 (tce & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
92 *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
98 * Validates TCE address.
99 * At the moment flags and page mask are validated.
100 * As the host kernel does not access those addresses (just puts them
101 * to the table and user space is supposed to process them), we can skip
102 * checking other things (such as TCE is a guest RAM address or the page
103 * was actually allocated).
105 static long kvmppc_rm_tce_validate(struct kvmppc_spapr_tce_table *stt,
108 unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
109 enum dma_data_direction dir = iommu_tce_direction(tce);
110 struct kvmppc_spapr_tce_iommu_table *stit;
111 unsigned long ua = 0;
113 /* Allow userspace to poison TCE table */
117 if (iommu_tce_check_gpa(stt->page_shift, gpa))
120 if (kvmppc_rm_tce_to_ua(stt->kvm, tce, &ua, NULL))
123 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
124 unsigned long hpa = 0;
125 struct mm_iommu_table_group_mem_t *mem;
126 long shift = stit->tbl->it_page_shift;
128 mem = mm_iommu_lookup_rm(stt->kvm->mm, ua, 1ULL << shift);
132 if (mm_iommu_ua_to_hpa_rm(mem, ua, shift, &hpa))
139 /* Note on the use of page_address() in real mode,
141 * It is safe to use page_address() in real mode on ppc64 because
142 * page_address() is always defined as lowmem_page_address()
143 * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
144 * operation and does not access page struct.
146 * Theoretically page_address() could be defined different
147 * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
148 * would have to be enabled.
149 * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
150 * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
151 * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
152 * is not expected to be enabled on ppc32, page_address()
153 * is safe for ppc32 as well.
155 * WARNING: This will be called in real-mode on HV KVM and virtual
158 static u64 *kvmppc_page_address(struct page *page)
160 #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
161 #error TODO: fix to avoid page_address() here
163 return (u64 *) page_address(page);
167 * Handles TCE requests for emulated devices.
168 * Puts guest TCE values to the table and expects user space to convert them.
169 * Cannot fail so kvmppc_rm_tce_validate must be called before it.
171 static void kvmppc_rm_tce_put(struct kvmppc_spapr_tce_table *stt,
172 unsigned long idx, unsigned long tce)
178 page = stt->pages[idx / TCES_PER_PAGE];
180 * kvmppc_rm_ioba_validate() allows pages not be allocated if TCE is
181 * being cleared, otherwise it returns H_TOO_HARD and we skip this.
184 WARN_ON_ONCE_RM(tce != 0);
187 tbl = kvmppc_page_address(page);
189 tbl[idx % TCES_PER_PAGE] = tce;
193 * TCEs pages are allocated in kvmppc_rm_tce_put() which won't be able to do so
195 * Check if kvmppc_rm_tce_put() can succeed in real mode, i.e. a TCEs page is
196 * allocated or not required (when clearing a tce entry).
198 static long kvmppc_rm_ioba_validate(struct kvmppc_spapr_tce_table *stt,
199 unsigned long ioba, unsigned long npages, bool clearing)
201 unsigned long i, idx, sttpage, sttpages;
202 unsigned long ret = kvmppc_ioba_validate(stt, ioba, npages);
207 * clearing==true says kvmppc_rm_tce_put won't be allocating pages
213 idx = (ioba >> stt->page_shift) - stt->offset;
214 sttpage = idx / TCES_PER_PAGE;
215 sttpages = _ALIGN_UP(idx % TCES_PER_PAGE + npages, TCES_PER_PAGE) /
217 for (i = sttpage; i < sttpage + sttpages; ++i)
224 static long iommu_tce_xchg_no_kill_rm(struct mm_struct *mm,
225 struct iommu_table *tbl,
226 unsigned long entry, unsigned long *hpa,
227 enum dma_data_direction *direction)
231 ret = tbl->it_ops->xchg_no_kill(tbl, entry, hpa, direction, true);
233 if (!ret && ((*direction == DMA_FROM_DEVICE) ||
234 (*direction == DMA_BIDIRECTIONAL))) {
235 __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RO(tbl, entry);
237 * kvmppc_rm_tce_iommu_do_map() updates the UA cache after
238 * calling this so we still get here a valid UA.
241 mm_iommu_ua_mark_dirty_rm(mm, be64_to_cpu(*pua));
247 extern void iommu_tce_kill_rm(struct iommu_table *tbl,
248 unsigned long entry, unsigned long pages)
250 if (tbl->it_ops->tce_kill)
251 tbl->it_ops->tce_kill(tbl, entry, pages, true);
254 static void kvmppc_rm_clear_tce(struct kvm *kvm, struct kvmppc_spapr_tce_table *stt,
255 struct iommu_table *tbl, unsigned long entry)
258 unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
259 unsigned long io_entry = entry << (stt->page_shift - tbl->it_page_shift);
261 for (i = 0; i < subpages; ++i) {
262 unsigned long hpa = 0;
263 enum dma_data_direction dir = DMA_NONE;
265 iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, io_entry + i, &hpa, &dir);
269 static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm *kvm,
270 struct iommu_table *tbl, unsigned long entry)
272 struct mm_iommu_table_group_mem_t *mem = NULL;
273 const unsigned long pgsize = 1ULL << tbl->it_page_shift;
274 __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RO(tbl, entry);
277 /* it_userspace allocation might be delayed */
280 mem = mm_iommu_lookup_rm(kvm->mm, be64_to_cpu(*pua), pgsize);
284 mm_iommu_mapped_dec(mem);
286 *pua = cpu_to_be64(0);
291 static long kvmppc_rm_tce_iommu_do_unmap(struct kvm *kvm,
292 struct iommu_table *tbl, unsigned long entry)
294 enum dma_data_direction dir = DMA_NONE;
295 unsigned long hpa = 0;
298 if (iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, entry, &hpa, &dir))
300 * real mode xchg can fail if struct page crosses
308 ret = kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
310 iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, entry, &hpa, &dir);
315 static long kvmppc_rm_tce_iommu_unmap(struct kvm *kvm,
316 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
319 unsigned long i, ret = H_SUCCESS;
320 unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
321 unsigned long io_entry = entry * subpages;
323 for (i = 0; i < subpages; ++i) {
324 ret = kvmppc_rm_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
325 if (ret != H_SUCCESS)
329 iommu_tce_kill_rm(tbl, io_entry, subpages);
334 static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
335 unsigned long entry, unsigned long ua,
336 enum dma_data_direction dir)
339 unsigned long hpa = 0;
340 __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RO(tbl, entry);
341 struct mm_iommu_table_group_mem_t *mem;
344 /* it_userspace allocation might be delayed */
347 mem = mm_iommu_lookup_rm(kvm->mm, ua, 1ULL << tbl->it_page_shift);
351 if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, tbl->it_page_shift,
355 if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem)))
358 ret = iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, entry, &hpa, &dir);
360 mm_iommu_mapped_dec(mem);
362 * real mode xchg can fail if struct page crosses
369 kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
371 *pua = cpu_to_be64(ua);
376 static long kvmppc_rm_tce_iommu_map(struct kvm *kvm,
377 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
378 unsigned long entry, unsigned long ua,
379 enum dma_data_direction dir)
381 unsigned long i, pgoff, ret = H_SUCCESS;
382 unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
383 unsigned long io_entry = entry * subpages;
385 for (i = 0, pgoff = 0; i < subpages;
386 ++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {
388 ret = kvmppc_rm_tce_iommu_do_map(kvm, tbl,
389 io_entry + i, ua + pgoff, dir);
390 if (ret != H_SUCCESS)
394 iommu_tce_kill_rm(tbl, io_entry, subpages);
399 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
400 unsigned long ioba, unsigned long tce)
402 struct kvmppc_spapr_tce_table *stt;
404 struct kvmppc_spapr_tce_iommu_table *stit;
405 unsigned long entry, ua = 0;
406 enum dma_data_direction dir;
408 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
409 /* liobn, ioba, tce); */
411 /* For radix, we might be in virtual mode, so punt */
412 if (kvm_is_radix(vcpu->kvm))
415 stt = kvmppc_find_table(vcpu->kvm, liobn);
419 ret = kvmppc_rm_ioba_validate(stt, ioba, 1, tce == 0);
420 if (ret != H_SUCCESS)
423 ret = kvmppc_rm_tce_validate(stt, tce);
424 if (ret != H_SUCCESS)
427 dir = iommu_tce_direction(tce);
428 if ((dir != DMA_NONE) && kvmppc_rm_tce_to_ua(vcpu->kvm, tce, &ua, NULL))
431 entry = ioba >> stt->page_shift;
433 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
435 ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
438 ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
439 stit->tbl, entry, ua, dir);
441 if (ret != H_SUCCESS) {
442 kvmppc_rm_clear_tce(vcpu->kvm, stt, stit->tbl, entry);
447 kvmppc_rm_tce_put(stt, entry, tce);
452 static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu,
453 unsigned long ua, unsigned long *phpa)
459 * Called in real mode with MSR_EE = 0. We are safe here.
460 * It is ok to do the lookup with arch.pgdir here, because
461 * we are doing this on secondary cpus and current task there
462 * is not the hypervisor. Also this is safe against THP in the
463 * host, because an IPI to primary thread will wait for the secondary
464 * to exit which will agains result in the below page table walk
467 ptep = __find_linux_pte(vcpu->arch.pgdir, ua, NULL, &shift);
468 if (!ptep || !pte_present(*ptep))
475 /* Avoid handling anything potentially complicated in realmode */
476 if (shift > PAGE_SHIFT)
482 *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
488 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
489 unsigned long liobn, unsigned long ioba,
490 unsigned long tce_list, unsigned long npages)
492 struct kvmppc_spapr_tce_table *stt;
493 long i, ret = H_SUCCESS;
494 unsigned long tces, entry, ua = 0;
495 unsigned long *rmap = NULL;
497 struct kvmppc_spapr_tce_iommu_table *stit;
499 /* For radix, we might be in virtual mode, so punt */
500 if (kvm_is_radix(vcpu->kvm))
503 stt = kvmppc_find_table(vcpu->kvm, liobn);
507 entry = ioba >> stt->page_shift;
509 * The spec says that the maximum size of the list is 512 TCEs
510 * so the whole table addressed resides in 4K page
515 if (tce_list & (SZ_4K - 1))
518 ret = kvmppc_rm_ioba_validate(stt, ioba, npages, false);
519 if (ret != H_SUCCESS)
522 if (mm_iommu_preregistered(vcpu->kvm->mm)) {
524 * We get here if guest memory was pre-registered which
525 * is normally VFIO case and gpa->hpa translation does not
528 struct mm_iommu_table_group_mem_t *mem;
530 if (kvmppc_rm_tce_to_ua(vcpu->kvm, tce_list, &ua, NULL))
533 mem = mm_iommu_lookup_rm(vcpu->kvm->mm, ua, IOMMU_PAGE_SIZE_4K);
535 prereg = mm_iommu_ua_to_hpa_rm(mem, ua,
536 IOMMU_PAGE_SHIFT_4K, &tces) == 0;
541 * This is usually a case of a guest with emulated devices only
542 * when TCE list is not in preregistered memory.
543 * We do not require memory to be preregistered in this case
544 * so lock rmap and do __find_linux_pte_or_hugepte().
546 if (kvmppc_rm_tce_to_ua(vcpu->kvm, tce_list, &ua, &rmap))
549 rmap = (void *) vmalloc_to_phys(rmap);
550 if (WARN_ON_ONCE_RM(!rmap))
554 * Synchronize with the MMU notifier callbacks in
555 * book3s_64_mmu_hv.c (kvm_unmap_hva_range_hv etc.).
556 * While we have the rmap lock, code running on other CPUs
557 * cannot finish unmapping the host real page that backs
558 * this guest real page, so we are OK to access the host
562 if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) {
568 for (i = 0; i < npages; ++i) {
569 unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
571 ret = kvmppc_rm_tce_validate(stt, tce);
572 if (ret != H_SUCCESS)
576 for (i = 0; i < npages; ++i) {
577 unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
580 if (kvmppc_rm_tce_to_ua(vcpu->kvm, tce, &ua, NULL)) {
585 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
586 ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
587 stit->tbl, entry + i, ua,
588 iommu_tce_direction(tce));
590 if (ret != H_SUCCESS) {
591 kvmppc_rm_clear_tce(vcpu->kvm, stt, stit->tbl,
597 kvmppc_rm_tce_put(stt, entry + i, tce);
607 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
608 unsigned long liobn, unsigned long ioba,
609 unsigned long tce_value, unsigned long npages)
611 struct kvmppc_spapr_tce_table *stt;
613 struct kvmppc_spapr_tce_iommu_table *stit;
615 /* For radix, we might be in virtual mode, so punt */
616 if (kvm_is_radix(vcpu->kvm))
619 stt = kvmppc_find_table(vcpu->kvm, liobn);
623 ret = kvmppc_rm_ioba_validate(stt, ioba, npages, tce_value == 0);
624 if (ret != H_SUCCESS)
627 /* Check permission bits only to allow userspace poison TCE for debug */
628 if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
631 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
632 unsigned long entry = ioba >> stt->page_shift;
634 for (i = 0; i < npages; ++i) {
635 ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
636 stit->tbl, entry + i);
638 if (ret == H_SUCCESS)
641 if (ret == H_TOO_HARD)
645 kvmppc_rm_clear_tce(vcpu->kvm, stt, stit->tbl, entry + i);
649 for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
650 kvmppc_rm_tce_put(stt, ioba >> stt->page_shift, tce_value);
655 /* This can be called in either virtual mode or real mode */
656 long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
659 struct kvmppc_spapr_tce_table *stt;
665 stt = kvmppc_find_table(vcpu->kvm, liobn);
669 ret = kvmppc_ioba_validate(stt, ioba, 1);
670 if (ret != H_SUCCESS)
673 idx = (ioba >> stt->page_shift) - stt->offset;
674 page = stt->pages[idx / TCES_PER_PAGE];
676 vcpu->arch.regs.gpr[4] = 0;
679 tbl = (u64 *)page_address(page);
681 vcpu->arch.regs.gpr[4] = tbl[idx % TCES_PER_PAGE];
685 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
687 #endif /* KVM_BOOK3S_HV_POSSIBLE */