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>
30 #include <asm/tlbflush.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>
45 #define WARN_ON_ONCE_RM(condition) ({ \
46 static bool __section(.data.unlikely) __warned; \
47 int __ret_warn_once = !!(condition); \
49 if (unlikely(__ret_warn_once && !__warned)) { \
51 pr_err("WARN_ON_ONCE_RM: (%s) at %s:%u\n", \
52 __stringify(condition), \
53 __func__, __LINE__); \
56 unlikely(__ret_warn_once); \
61 #define WARN_ON_ONCE_RM(condition) ({ \
62 int __ret_warn_on = !!(condition); \
63 unlikely(__ret_warn_on); \
68 #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
71 * Finds a TCE table descriptor by LIOBN.
73 * WARNING: This will be called in real or virtual mode on HV KVM and virtual
76 struct kvmppc_spapr_tce_table
*kvmppc_find_table(struct kvm
*kvm
,
79 struct kvmppc_spapr_tce_table
*stt
;
81 list_for_each_entry_lockless(stt
, &kvm
->arch
.spapr_tce_tables
, list
)
82 if (stt
->liobn
== liobn
)
87 EXPORT_SYMBOL_GPL(kvmppc_find_table
);
90 * Validates IO address.
92 * WARNING: This will be called in real-mode on HV KVM and virtual
95 long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table
*stt
,
96 unsigned long ioba
, unsigned long npages
)
98 unsigned long mask
= (1ULL << stt
->page_shift
) - 1;
99 unsigned long idx
= ioba
>> stt
->page_shift
;
101 if ((ioba
& mask
) || (idx
< stt
->offset
) ||
102 (idx
- stt
->offset
+ npages
> stt
->size
) ||
103 (idx
+ npages
< idx
))
108 EXPORT_SYMBOL_GPL(kvmppc_ioba_validate
);
111 * Validates TCE address.
112 * At the moment flags and page mask are validated.
113 * As the host kernel does not access those addresses (just puts them
114 * to the table and user space is supposed to process them), we can skip
115 * checking other things (such as TCE is a guest RAM address or the page
116 * was actually allocated).
118 * WARNING: This will be called in real-mode on HV KVM and virtual
121 long kvmppc_tce_validate(struct kvmppc_spapr_tce_table
*stt
, unsigned long tce
)
123 unsigned long page_mask
= ~((1ULL << stt
->page_shift
) - 1);
124 unsigned long mask
= ~(page_mask
| TCE_PCI_WRITE
| TCE_PCI_READ
);
131 EXPORT_SYMBOL_GPL(kvmppc_tce_validate
);
133 /* Note on the use of page_address() in real mode,
135 * It is safe to use page_address() in real mode on ppc64 because
136 * page_address() is always defined as lowmem_page_address()
137 * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
138 * operation and does not access page struct.
140 * Theoretically page_address() could be defined different
141 * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
142 * would have to be enabled.
143 * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
144 * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
145 * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
146 * is not expected to be enabled on ppc32, page_address()
147 * is safe for ppc32 as well.
149 * WARNING: This will be called in real-mode on HV KVM and virtual
152 static u64
*kvmppc_page_address(struct page
*page
)
154 #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
155 #error TODO: fix to avoid page_address() here
157 return (u64
*) page_address(page
);
161 * Handles TCE requests for emulated devices.
162 * Puts guest TCE values to the table and expects user space to convert them.
163 * Called in both real and virtual modes.
164 * Cannot fail so kvmppc_tce_validate must be called before it.
166 * WARNING: This will be called in real-mode on HV KVM and virtual
169 void kvmppc_tce_put(struct kvmppc_spapr_tce_table
*stt
,
170 unsigned long idx
, unsigned long tce
)
176 page
= stt
->pages
[idx
/ TCES_PER_PAGE
];
177 tbl
= kvmppc_page_address(page
);
179 tbl
[idx
% TCES_PER_PAGE
] = tce
;
181 EXPORT_SYMBOL_GPL(kvmppc_tce_put
);
183 long kvmppc_gpa_to_ua(struct kvm
*kvm
, unsigned long gpa
,
184 unsigned long *ua
, unsigned long **prmap
)
186 unsigned long gfn
= gpa
>> PAGE_SHIFT
;
187 struct kvm_memory_slot
*memslot
;
189 memslot
= search_memslots(kvm_memslots(kvm
), gfn
);
193 *ua
= __gfn_to_hva_memslot(memslot
, gfn
) |
194 (gpa
& ~(PAGE_MASK
| TCE_PCI_READ
| TCE_PCI_WRITE
));
196 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
198 *prmap
= &memslot
->arch
.rmap
[gfn
- memslot
->base_gfn
];
203 EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua
);
205 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
206 static void kvmppc_rm_clear_tce(struct iommu_table
*tbl
, unsigned long entry
)
208 unsigned long hpa
= 0;
209 enum dma_data_direction dir
= DMA_NONE
;
211 iommu_tce_xchg_rm(tbl
, entry
, &hpa
, &dir
);
214 static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm
*kvm
,
215 struct iommu_table
*tbl
, unsigned long entry
)
217 struct mm_iommu_table_group_mem_t
*mem
= NULL
;
218 const unsigned long pgsize
= 1ULL << tbl
->it_page_shift
;
219 unsigned long *pua
= IOMMU_TABLE_USERSPACE_ENTRY(tbl
, entry
);
222 /* it_userspace allocation might be delayed */
225 pua
= (void *) vmalloc_to_phys(pua
);
226 if (WARN_ON_ONCE_RM(!pua
))
229 mem
= mm_iommu_lookup_rm(kvm
->mm
, *pua
, pgsize
);
233 mm_iommu_mapped_dec(mem
);
240 static long kvmppc_rm_tce_iommu_unmap(struct kvm
*kvm
,
241 struct iommu_table
*tbl
, unsigned long entry
)
243 enum dma_data_direction dir
= DMA_NONE
;
244 unsigned long hpa
= 0;
247 if (iommu_tce_xchg_rm(tbl
, entry
, &hpa
, &dir
))
249 * real mode xchg can fail if struct page crosses
257 ret
= kvmppc_rm_tce_iommu_mapped_dec(kvm
, tbl
, entry
);
259 iommu_tce_xchg_rm(tbl
, entry
, &hpa
, &dir
);
264 static long kvmppc_rm_tce_iommu_map(struct kvm
*kvm
, struct iommu_table
*tbl
,
265 unsigned long entry
, unsigned long ua
,
266 enum dma_data_direction dir
)
269 unsigned long hpa
= 0;
270 unsigned long *pua
= IOMMU_TABLE_USERSPACE_ENTRY(tbl
, entry
);
271 struct mm_iommu_table_group_mem_t
*mem
;
274 /* it_userspace allocation might be delayed */
277 mem
= mm_iommu_lookup_rm(kvm
->mm
, ua
, 1ULL << tbl
->it_page_shift
);
281 if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem
, ua
, &hpa
)))
284 pua
= (void *) vmalloc_to_phys(pua
);
285 if (WARN_ON_ONCE_RM(!pua
))
288 if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem
)))
291 ret
= iommu_tce_xchg_rm(tbl
, entry
, &hpa
, &dir
);
293 mm_iommu_mapped_dec(mem
);
295 * real mode xchg can fail if struct page crosses
302 kvmppc_rm_tce_iommu_mapped_dec(kvm
, tbl
, entry
);
309 long kvmppc_rm_h_put_tce(struct kvm_vcpu
*vcpu
, unsigned long liobn
,
310 unsigned long ioba
, unsigned long tce
)
312 struct kvmppc_spapr_tce_table
*stt
;
314 struct kvmppc_spapr_tce_iommu_table
*stit
;
315 unsigned long entry
, ua
= 0;
316 enum dma_data_direction dir
;
318 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
319 /* liobn, ioba, tce); */
321 stt
= kvmppc_find_table(vcpu
->kvm
, liobn
);
325 ret
= kvmppc_ioba_validate(stt
, ioba
, 1);
326 if (ret
!= H_SUCCESS
)
329 ret
= kvmppc_tce_validate(stt
, tce
);
330 if (ret
!= H_SUCCESS
)
333 dir
= iommu_tce_direction(tce
);
334 if ((dir
!= DMA_NONE
) && kvmppc_gpa_to_ua(vcpu
->kvm
,
335 tce
& ~(TCE_PCI_READ
| TCE_PCI_WRITE
), &ua
, NULL
))
338 entry
= ioba
>> stt
->page_shift
;
340 list_for_each_entry_lockless(stit
, &stt
->iommu_tables
, next
) {
342 ret
= kvmppc_rm_tce_iommu_unmap(vcpu
->kvm
,
345 ret
= kvmppc_rm_tce_iommu_map(vcpu
->kvm
,
346 stit
->tbl
, entry
, ua
, dir
);
348 if (ret
== H_SUCCESS
)
351 if (ret
== H_TOO_HARD
)
355 kvmppc_rm_clear_tce(stit
->tbl
, entry
);
358 kvmppc_tce_put(stt
, entry
, tce
);
363 static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu
*vcpu
,
364 unsigned long ua
, unsigned long *phpa
)
369 ptep
= __find_linux_pte_or_hugepte(vcpu
->arch
.pgdir
, ua
, NULL
, &shift
);
370 if (!ptep
|| !pte_present(*ptep
))
377 /* Avoid handling anything potentially complicated in realmode */
378 if (shift
> PAGE_SHIFT
)
384 *phpa
= (pte_pfn(pte
) << PAGE_SHIFT
) | (ua
& ((1ULL << shift
) - 1)) |
390 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu
*vcpu
,
391 unsigned long liobn
, unsigned long ioba
,
392 unsigned long tce_list
, unsigned long npages
)
394 struct kvmppc_spapr_tce_table
*stt
;
395 long i
, ret
= H_SUCCESS
;
396 unsigned long tces
, entry
, ua
= 0;
397 unsigned long *rmap
= NULL
;
399 struct kvmppc_spapr_tce_iommu_table
*stit
;
401 stt
= kvmppc_find_table(vcpu
->kvm
, liobn
);
405 entry
= ioba
>> stt
->page_shift
;
407 * The spec says that the maximum size of the list is 512 TCEs
408 * so the whole table addressed resides in 4K page
413 if (tce_list
& (SZ_4K
- 1))
416 ret
= kvmppc_ioba_validate(stt
, ioba
, npages
);
417 if (ret
!= H_SUCCESS
)
420 if (mm_iommu_preregistered(vcpu
->kvm
->mm
)) {
422 * We get here if guest memory was pre-registered which
423 * is normally VFIO case and gpa->hpa translation does not
426 struct mm_iommu_table_group_mem_t
*mem
;
428 if (kvmppc_gpa_to_ua(vcpu
->kvm
, tce_list
, &ua
, NULL
))
431 mem
= mm_iommu_lookup_rm(vcpu
->kvm
->mm
, ua
, IOMMU_PAGE_SIZE_4K
);
433 prereg
= mm_iommu_ua_to_hpa_rm(mem
, ua
, &tces
) == 0;
438 * This is usually a case of a guest with emulated devices only
439 * when TCE list is not in preregistered memory.
440 * We do not require memory to be preregistered in this case
441 * so lock rmap and do __find_linux_pte_or_hugepte().
443 if (kvmppc_gpa_to_ua(vcpu
->kvm
, tce_list
, &ua
, &rmap
))
446 rmap
= (void *) vmalloc_to_phys(rmap
);
447 if (WARN_ON_ONCE_RM(!rmap
))
451 * Synchronize with the MMU notifier callbacks in
452 * book3s_64_mmu_hv.c (kvm_unmap_hva_hv etc.).
453 * While we have the rmap lock, code running on other CPUs
454 * cannot finish unmapping the host real page that backs
455 * this guest real page, so we are OK to access the host
459 if (kvmppc_rm_ua_to_hpa(vcpu
, ua
, &tces
)) {
465 for (i
= 0; i
< npages
; ++i
) {
466 unsigned long tce
= be64_to_cpu(((u64
*)tces
)[i
]);
468 ret
= kvmppc_tce_validate(stt
, tce
);
469 if (ret
!= H_SUCCESS
)
473 if (kvmppc_gpa_to_ua(vcpu
->kvm
,
474 tce
& ~(TCE_PCI_READ
| TCE_PCI_WRITE
),
478 list_for_each_entry_lockless(stit
, &stt
->iommu_tables
, next
) {
479 ret
= kvmppc_rm_tce_iommu_map(vcpu
->kvm
,
480 stit
->tbl
, entry
+ i
, ua
,
481 iommu_tce_direction(tce
));
483 if (ret
== H_SUCCESS
)
486 if (ret
== H_TOO_HARD
)
490 kvmppc_rm_clear_tce(stit
->tbl
, entry
);
493 kvmppc_tce_put(stt
, entry
+ i
, tce
);
503 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu
*vcpu
,
504 unsigned long liobn
, unsigned long ioba
,
505 unsigned long tce_value
, unsigned long npages
)
507 struct kvmppc_spapr_tce_table
*stt
;
509 struct kvmppc_spapr_tce_iommu_table
*stit
;
511 stt
= kvmppc_find_table(vcpu
->kvm
, liobn
);
515 ret
= kvmppc_ioba_validate(stt
, ioba
, npages
);
516 if (ret
!= H_SUCCESS
)
519 /* Check permission bits only to allow userspace poison TCE for debug */
520 if (tce_value
& (TCE_PCI_WRITE
| TCE_PCI_READ
))
523 list_for_each_entry_lockless(stit
, &stt
->iommu_tables
, next
) {
524 unsigned long entry
= ioba
>> stit
->tbl
->it_page_shift
;
526 for (i
= 0; i
< npages
; ++i
) {
527 ret
= kvmppc_rm_tce_iommu_unmap(vcpu
->kvm
,
528 stit
->tbl
, entry
+ i
);
530 if (ret
== H_SUCCESS
)
533 if (ret
== H_TOO_HARD
)
537 kvmppc_rm_clear_tce(stit
->tbl
, entry
);
541 for (i
= 0; i
< npages
; ++i
, ioba
+= (1ULL << stt
->page_shift
))
542 kvmppc_tce_put(stt
, ioba
>> stt
->page_shift
, tce_value
);
547 long kvmppc_h_get_tce(struct kvm_vcpu
*vcpu
, unsigned long liobn
,
550 struct kvmppc_spapr_tce_table
*stt
;
556 stt
= kvmppc_find_table(vcpu
->kvm
, liobn
);
560 ret
= kvmppc_ioba_validate(stt
, ioba
, 1);
561 if (ret
!= H_SUCCESS
)
564 idx
= (ioba
>> stt
->page_shift
) - stt
->offset
;
565 page
= stt
->pages
[idx
/ TCES_PER_PAGE
];
566 tbl
= (u64
*)page_address(page
);
568 vcpu
->arch
.gpr
[4] = tbl
[idx
% TCES_PER_PAGE
];
572 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce
);
574 #endif /* KVM_BOOK3S_HV_POSSIBLE */