• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
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>
7  */
8 
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>
19 
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/udbg.h>
28 #include <asm/iommu.h>
29 #include <asm/tce.h>
30 #include <asm/pte-walk.h>
31 
32 #ifdef CONFIG_BUG
33 
34 #define WARN_ON_ONCE_RM(condition)	({			\
35 	static bool __section(".data.unlikely") __warned;	\
36 	int __ret_warn_once = !!(condition);			\
37 								\
38 	if (unlikely(__ret_warn_once && !__warned)) {		\
39 		__warned = true;				\
40 		pr_err("WARN_ON_ONCE_RM: (%s) at %s:%u\n",	\
41 				__stringify(condition),		\
42 				__func__, __LINE__);		\
43 		dump_stack();					\
44 	}							\
45 	unlikely(__ret_warn_once);				\
46 })
47 
48 #else
49 
50 #define WARN_ON_ONCE_RM(condition) ({				\
51 	int __ret_warn_on = !!(condition);			\
52 	unlikely(__ret_warn_on);				\
53 })
54 
55 #endif
56 
57 /*
58  * Finds a TCE table descriptor by LIOBN.
59  *
60  * WARNING: This will be called in real or virtual mode on HV KVM and virtual
61  *          mode on PR KVM
62  */
kvmppc_find_table(struct kvm * kvm,unsigned long liobn)63 struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
64 		unsigned long liobn)
65 {
66 	struct kvmppc_spapr_tce_table *stt;
67 
68 	list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
69 		if (stt->liobn == liobn)
70 			return stt;
71 
72 	return NULL;
73 }
74 EXPORT_SYMBOL_GPL(kvmppc_find_table);
75 
76 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
kvmppc_rm_tce_to_ua(struct kvm * kvm,unsigned long tce,unsigned long * ua)77 static long kvmppc_rm_tce_to_ua(struct kvm *kvm,
78 				unsigned long tce, unsigned long *ua)
79 {
80 	unsigned long gfn = tce >> PAGE_SHIFT;
81 	struct kvm_memory_slot *memslot;
82 
83 	memslot = search_memslots(kvm_memslots_raw(kvm), gfn);
84 	if (!memslot)
85 		return -EINVAL;
86 
87 	*ua = __gfn_to_hva_memslot(memslot, gfn) |
88 		(tce & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
89 
90 	return 0;
91 }
92 
93 /*
94  * Validates TCE address.
95  * At the moment flags and page mask are validated.
96  * As the host kernel does not access those addresses (just puts them
97  * to the table and user space is supposed to process them), we can skip
98  * checking other things (such as TCE is a guest RAM address or the page
99  * was actually allocated).
100  */
kvmppc_rm_tce_validate(struct kvmppc_spapr_tce_table * stt,unsigned long tce)101 static long kvmppc_rm_tce_validate(struct kvmppc_spapr_tce_table *stt,
102 		unsigned long tce)
103 {
104 	unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
105 	enum dma_data_direction dir = iommu_tce_direction(tce);
106 	struct kvmppc_spapr_tce_iommu_table *stit;
107 	unsigned long ua = 0;
108 
109 	/* Allow userspace to poison TCE table */
110 	if (dir == DMA_NONE)
111 		return H_SUCCESS;
112 
113 	if (iommu_tce_check_gpa(stt->page_shift, gpa))
114 		return H_PARAMETER;
115 
116 	if (kvmppc_rm_tce_to_ua(stt->kvm, tce, &ua))
117 		return H_TOO_HARD;
118 
119 	list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
120 		unsigned long hpa = 0;
121 		struct mm_iommu_table_group_mem_t *mem;
122 		long shift = stit->tbl->it_page_shift;
123 
124 		mem = mm_iommu_lookup_rm(stt->kvm->mm, ua, 1ULL << shift);
125 		if (!mem)
126 			return H_TOO_HARD;
127 
128 		if (mm_iommu_ua_to_hpa_rm(mem, ua, shift, &hpa))
129 			return H_TOO_HARD;
130 	}
131 
132 	return H_SUCCESS;
133 }
134 
135 /* Note on the use of page_address() in real mode,
136  *
137  * It is safe to use page_address() in real mode on ppc64 because
138  * page_address() is always defined as lowmem_page_address()
139  * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
140  * operation and does not access page struct.
141  *
142  * Theoretically page_address() could be defined different
143  * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
144  * would have to be enabled.
145  * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
146  * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
147  * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
148  * is not expected to be enabled on ppc32, page_address()
149  * is safe for ppc32 as well.
150  *
151  * WARNING: This will be called in real-mode on HV KVM and virtual
152  *          mode on PR KVM
153  */
kvmppc_page_address(struct page * page)154 static u64 *kvmppc_page_address(struct page *page)
155 {
156 #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
157 #error TODO: fix to avoid page_address() here
158 #endif
159 	return (u64 *) page_address(page);
160 }
161 
162 /*
163  * Handles TCE requests for emulated devices.
164  * Puts guest TCE values to the table and expects user space to convert them.
165  * Cannot fail so kvmppc_rm_tce_validate must be called before it.
166  */
kvmppc_rm_tce_put(struct kvmppc_spapr_tce_table * stt,unsigned long idx,unsigned long tce)167 static void kvmppc_rm_tce_put(struct kvmppc_spapr_tce_table *stt,
168 		unsigned long idx, unsigned long tce)
169 {
170 	struct page *page;
171 	u64 *tbl;
172 
173 	idx -= stt->offset;
174 	page = stt->pages[idx / TCES_PER_PAGE];
175 	/*
176 	 * kvmppc_rm_ioba_validate() allows pages not be allocated if TCE is
177 	 * being cleared, otherwise it returns H_TOO_HARD and we skip this.
178 	 */
179 	if (!page) {
180 		WARN_ON_ONCE_RM(tce != 0);
181 		return;
182 	}
183 	tbl = kvmppc_page_address(page);
184 
185 	tbl[idx % TCES_PER_PAGE] = tce;
186 }
187 
188 /*
189  * TCEs pages are allocated in kvmppc_rm_tce_put() which won't be able to do so
190  * in real mode.
191  * Check if kvmppc_rm_tce_put() can succeed in real mode, i.e. a TCEs page is
192  * allocated or not required (when clearing a tce entry).
193  */
kvmppc_rm_ioba_validate(struct kvmppc_spapr_tce_table * stt,unsigned long ioba,unsigned long npages,bool clearing)194 static long kvmppc_rm_ioba_validate(struct kvmppc_spapr_tce_table *stt,
195 		unsigned long ioba, unsigned long npages, bool clearing)
196 {
197 	unsigned long i, idx, sttpage, sttpages;
198 	unsigned long ret = kvmppc_ioba_validate(stt, ioba, npages);
199 
200 	if (ret)
201 		return ret;
202 	/*
203 	 * clearing==true says kvmppc_rm_tce_put won't be allocating pages
204 	 * for empty tces.
205 	 */
206 	if (clearing)
207 		return H_SUCCESS;
208 
209 	idx = (ioba >> stt->page_shift) - stt->offset;
210 	sttpage = idx / TCES_PER_PAGE;
211 	sttpages = ALIGN(idx % TCES_PER_PAGE + npages, TCES_PER_PAGE) /
212 			TCES_PER_PAGE;
213 	for (i = sttpage; i < sttpage + sttpages; ++i)
214 		if (!stt->pages[i])
215 			return H_TOO_HARD;
216 
217 	return H_SUCCESS;
218 }
219 
iommu_tce_xchg_no_kill_rm(struct mm_struct * mm,struct iommu_table * tbl,unsigned long entry,unsigned long * hpa,enum dma_data_direction * direction)220 static long iommu_tce_xchg_no_kill_rm(struct mm_struct *mm,
221 		struct iommu_table *tbl,
222 		unsigned long entry, unsigned long *hpa,
223 		enum dma_data_direction *direction)
224 {
225 	long ret;
226 
227 	ret = tbl->it_ops->xchg_no_kill(tbl, entry, hpa, direction, true);
228 
229 	if (!ret && ((*direction == DMA_FROM_DEVICE) ||
230 				(*direction == DMA_BIDIRECTIONAL))) {
231 		__be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RO(tbl, entry);
232 		/*
233 		 * kvmppc_rm_tce_iommu_do_map() updates the UA cache after
234 		 * calling this so we still get here a valid UA.
235 		 */
236 		if (pua && *pua)
237 			mm_iommu_ua_mark_dirty_rm(mm, be64_to_cpu(*pua));
238 	}
239 
240 	return ret;
241 }
242 
iommu_tce_kill_rm(struct iommu_table * tbl,unsigned long entry,unsigned long pages)243 static void iommu_tce_kill_rm(struct iommu_table *tbl,
244 		unsigned long entry, unsigned long pages)
245 {
246 	if (tbl->it_ops->tce_kill)
247 		tbl->it_ops->tce_kill(tbl, entry, pages, true);
248 }
249 
kvmppc_rm_clear_tce(struct kvm * kvm,struct kvmppc_spapr_tce_table * stt,struct iommu_table * tbl,unsigned long entry)250 static void kvmppc_rm_clear_tce(struct kvm *kvm, struct kvmppc_spapr_tce_table *stt,
251 		struct iommu_table *tbl, unsigned long entry)
252 {
253 	unsigned long i;
254 	unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
255 	unsigned long io_entry = entry << (stt->page_shift - tbl->it_page_shift);
256 
257 	for (i = 0; i < subpages; ++i) {
258 		unsigned long hpa = 0;
259 		enum dma_data_direction dir = DMA_NONE;
260 
261 		iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, io_entry + i, &hpa, &dir);
262 	}
263 }
264 
kvmppc_rm_tce_iommu_mapped_dec(struct kvm * kvm,struct iommu_table * tbl,unsigned long entry)265 static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm *kvm,
266 		struct iommu_table *tbl, unsigned long entry)
267 {
268 	struct mm_iommu_table_group_mem_t *mem = NULL;
269 	const unsigned long pgsize = 1ULL << tbl->it_page_shift;
270 	__be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RO(tbl, entry);
271 
272 	if (!pua)
273 		/* it_userspace allocation might be delayed */
274 		return H_TOO_HARD;
275 
276 	mem = mm_iommu_lookup_rm(kvm->mm, be64_to_cpu(*pua), pgsize);
277 	if (!mem)
278 		return H_TOO_HARD;
279 
280 	mm_iommu_mapped_dec(mem);
281 
282 	*pua = cpu_to_be64(0);
283 
284 	return H_SUCCESS;
285 }
286 
kvmppc_rm_tce_iommu_do_unmap(struct kvm * kvm,struct iommu_table * tbl,unsigned long entry)287 static long kvmppc_rm_tce_iommu_do_unmap(struct kvm *kvm,
288 		struct iommu_table *tbl, unsigned long entry)
289 {
290 	enum dma_data_direction dir = DMA_NONE;
291 	unsigned long hpa = 0;
292 	long ret;
293 
294 	if (iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, entry, &hpa, &dir))
295 		/*
296 		 * real mode xchg can fail if struct page crosses
297 		 * a page boundary
298 		 */
299 		return H_TOO_HARD;
300 
301 	if (dir == DMA_NONE)
302 		return H_SUCCESS;
303 
304 	ret = kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
305 	if (ret)
306 		iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, entry, &hpa, &dir);
307 
308 	return ret;
309 }
310 
kvmppc_rm_tce_iommu_unmap(struct kvm * kvm,struct kvmppc_spapr_tce_table * stt,struct iommu_table * tbl,unsigned long entry)311 static long kvmppc_rm_tce_iommu_unmap(struct kvm *kvm,
312 		struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
313 		unsigned long entry)
314 {
315 	unsigned long i, ret = H_SUCCESS;
316 	unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
317 	unsigned long io_entry = entry * subpages;
318 
319 	for (i = 0; i < subpages; ++i) {
320 		ret = kvmppc_rm_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
321 		if (ret != H_SUCCESS)
322 			break;
323 	}
324 
325 	iommu_tce_kill_rm(tbl, io_entry, subpages);
326 
327 	return ret;
328 }
329 
kvmppc_rm_tce_iommu_do_map(struct kvm * kvm,struct iommu_table * tbl,unsigned long entry,unsigned long ua,enum dma_data_direction dir)330 static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
331 		unsigned long entry, unsigned long ua,
332 		enum dma_data_direction dir)
333 {
334 	long ret;
335 	unsigned long hpa = 0;
336 	__be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RO(tbl, entry);
337 	struct mm_iommu_table_group_mem_t *mem;
338 
339 	if (!pua)
340 		/* it_userspace allocation might be delayed */
341 		return H_TOO_HARD;
342 
343 	mem = mm_iommu_lookup_rm(kvm->mm, ua, 1ULL << tbl->it_page_shift);
344 	if (!mem)
345 		return H_TOO_HARD;
346 
347 	if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, tbl->it_page_shift,
348 			&hpa)))
349 		return H_TOO_HARD;
350 
351 	if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem)))
352 		return H_TOO_HARD;
353 
354 	ret = iommu_tce_xchg_no_kill_rm(kvm->mm, tbl, entry, &hpa, &dir);
355 	if (ret) {
356 		mm_iommu_mapped_dec(mem);
357 		/*
358 		 * real mode xchg can fail if struct page crosses
359 		 * a page boundary
360 		 */
361 		return H_TOO_HARD;
362 	}
363 
364 	if (dir != DMA_NONE)
365 		kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
366 
367 	*pua = cpu_to_be64(ua);
368 
369 	return 0;
370 }
371 
kvmppc_rm_tce_iommu_map(struct kvm * kvm,struct kvmppc_spapr_tce_table * stt,struct iommu_table * tbl,unsigned long entry,unsigned long ua,enum dma_data_direction dir)372 static long kvmppc_rm_tce_iommu_map(struct kvm *kvm,
373 		struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
374 		unsigned long entry, unsigned long ua,
375 		enum dma_data_direction dir)
376 {
377 	unsigned long i, pgoff, ret = H_SUCCESS;
378 	unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
379 	unsigned long io_entry = entry * subpages;
380 
381 	for (i = 0, pgoff = 0; i < subpages;
382 			++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {
383 
384 		ret = kvmppc_rm_tce_iommu_do_map(kvm, tbl,
385 				io_entry + i, ua + pgoff, dir);
386 		if (ret != H_SUCCESS)
387 			break;
388 	}
389 
390 	iommu_tce_kill_rm(tbl, io_entry, subpages);
391 
392 	return ret;
393 }
394 
kvmppc_rm_h_put_tce(struct kvm_vcpu * vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce)395 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
396 		unsigned long ioba, unsigned long tce)
397 {
398 	struct kvmppc_spapr_tce_table *stt;
399 	long ret;
400 	struct kvmppc_spapr_tce_iommu_table *stit;
401 	unsigned long entry, ua = 0;
402 	enum dma_data_direction dir;
403 
404 	/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
405 	/* 	    liobn, ioba, tce); */
406 
407 	/* For radix, we might be in virtual mode, so punt */
408 	if (kvm_is_radix(vcpu->kvm))
409 		return H_TOO_HARD;
410 
411 	stt = kvmppc_find_table(vcpu->kvm, liobn);
412 	if (!stt)
413 		return H_TOO_HARD;
414 
415 	ret = kvmppc_rm_ioba_validate(stt, ioba, 1, tce == 0);
416 	if (ret != H_SUCCESS)
417 		return ret;
418 
419 	ret = kvmppc_rm_tce_validate(stt, tce);
420 	if (ret != H_SUCCESS)
421 		return ret;
422 
423 	dir = iommu_tce_direction(tce);
424 	if ((dir != DMA_NONE) && kvmppc_rm_tce_to_ua(vcpu->kvm, tce, &ua))
425 		return H_PARAMETER;
426 
427 	entry = ioba >> stt->page_shift;
428 
429 	list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
430 		if (dir == DMA_NONE)
431 			ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
432 					stit->tbl, entry);
433 		else
434 			ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
435 					stit->tbl, entry, ua, dir);
436 
437 		if (ret != H_SUCCESS) {
438 			kvmppc_rm_clear_tce(vcpu->kvm, stt, stit->tbl, entry);
439 			return ret;
440 		}
441 	}
442 
443 	kvmppc_rm_tce_put(stt, entry, tce);
444 
445 	return H_SUCCESS;
446 }
447 
kvmppc_rm_ua_to_hpa(struct kvm_vcpu * vcpu,unsigned long mmu_seq,unsigned long ua,unsigned long * phpa)448 static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu, unsigned long mmu_seq,
449 				unsigned long ua, unsigned long *phpa)
450 {
451 	pte_t *ptep, pte;
452 	unsigned shift = 0;
453 
454 	/*
455 	 * Called in real mode with MSR_EE = 0. We are safe here.
456 	 * It is ok to do the lookup with arch.pgdir here, because
457 	 * we are doing this on secondary cpus and current task there
458 	 * is not the hypervisor. Also this is safe against THP in the
459 	 * host, because an IPI to primary thread will wait for the secondary
460 	 * to exit which will agains result in the below page table walk
461 	 * to finish.
462 	 */
463 	/* an rmap lock won't make it safe. because that just ensure hash
464 	 * page table entries are removed with rmap lock held. After that
465 	 * mmu notifier returns and we go ahead and removing ptes from Qemu page table.
466 	 */
467 	ptep = find_kvm_host_pte(vcpu->kvm, mmu_seq, ua, &shift);
468 	if (!ptep)
469 		return -ENXIO;
470 
471 	pte = READ_ONCE(*ptep);
472 	if (!pte_present(pte))
473 		return -ENXIO;
474 
475 	if (!shift)
476 		shift = PAGE_SHIFT;
477 
478 	/* Avoid handling anything potentially complicated in realmode */
479 	if (shift > PAGE_SHIFT)
480 		return -EAGAIN;
481 
482 	if (!pte_young(pte))
483 		return -EAGAIN;
484 
485 	*phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
486 			(ua & ~PAGE_MASK);
487 
488 	return 0;
489 }
490 
kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu * vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce_list,unsigned long npages)491 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
492 		unsigned long liobn, unsigned long ioba,
493 		unsigned long tce_list,	unsigned long npages)
494 {
495 	struct kvm *kvm = vcpu->kvm;
496 	struct kvmppc_spapr_tce_table *stt;
497 	long i, ret = H_SUCCESS;
498 	unsigned long tces, entry, ua = 0;
499 	unsigned long mmu_seq;
500 	bool prereg = false;
501 	struct kvmppc_spapr_tce_iommu_table *stit;
502 
503 	/* For radix, we might be in virtual mode, so punt */
504 	if (kvm_is_radix(vcpu->kvm))
505 		return H_TOO_HARD;
506 
507 	/*
508 	 * used to check for invalidations in progress
509 	 */
510 	mmu_seq = kvm->mmu_notifier_seq;
511 	smp_rmb();
512 
513 	stt = kvmppc_find_table(vcpu->kvm, liobn);
514 	if (!stt)
515 		return H_TOO_HARD;
516 
517 	entry = ioba >> stt->page_shift;
518 	/*
519 	 * The spec says that the maximum size of the list is 512 TCEs
520 	 * so the whole table addressed resides in 4K page
521 	 */
522 	if (npages > 512)
523 		return H_PARAMETER;
524 
525 	if (tce_list & (SZ_4K - 1))
526 		return H_PARAMETER;
527 
528 	ret = kvmppc_rm_ioba_validate(stt, ioba, npages, false);
529 	if (ret != H_SUCCESS)
530 		return ret;
531 
532 	if (mm_iommu_preregistered(vcpu->kvm->mm)) {
533 		/*
534 		 * We get here if guest memory was pre-registered which
535 		 * is normally VFIO case and gpa->hpa translation does not
536 		 * depend on hpt.
537 		 */
538 		struct mm_iommu_table_group_mem_t *mem;
539 
540 		if (kvmppc_rm_tce_to_ua(vcpu->kvm, tce_list, &ua))
541 			return H_TOO_HARD;
542 
543 		mem = mm_iommu_lookup_rm(vcpu->kvm->mm, ua, IOMMU_PAGE_SIZE_4K);
544 		if (mem)
545 			prereg = mm_iommu_ua_to_hpa_rm(mem, ua,
546 					IOMMU_PAGE_SHIFT_4K, &tces) == 0;
547 	}
548 
549 	if (!prereg) {
550 		/*
551 		 * This is usually a case of a guest with emulated devices only
552 		 * when TCE list is not in preregistered memory.
553 		 * We do not require memory to be preregistered in this case
554 		 * so lock rmap and do __find_linux_pte_or_hugepte().
555 		 */
556 		if (kvmppc_rm_tce_to_ua(vcpu->kvm, tce_list, &ua))
557 			return H_TOO_HARD;
558 
559 		arch_spin_lock(&kvm->mmu_lock.rlock.raw_lock);
560 		if (kvmppc_rm_ua_to_hpa(vcpu, mmu_seq, ua, &tces)) {
561 			ret = H_TOO_HARD;
562 			goto unlock_exit;
563 		}
564 	}
565 
566 	for (i = 0; i < npages; ++i) {
567 		unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
568 
569 		ret = kvmppc_rm_tce_validate(stt, tce);
570 		if (ret != H_SUCCESS)
571 			goto unlock_exit;
572 	}
573 
574 	for (i = 0; i < npages; ++i) {
575 		unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
576 
577 		ua = 0;
578 		if (kvmppc_rm_tce_to_ua(vcpu->kvm, tce, &ua)) {
579 			ret = H_PARAMETER;
580 			goto unlock_exit;
581 		}
582 
583 		list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
584 			ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
585 					stit->tbl, entry + i, ua,
586 					iommu_tce_direction(tce));
587 
588 			if (ret != H_SUCCESS) {
589 				kvmppc_rm_clear_tce(vcpu->kvm, stt, stit->tbl,
590 						entry + i);
591 				goto unlock_exit;
592 			}
593 		}
594 
595 		kvmppc_rm_tce_put(stt, entry + i, tce);
596 	}
597 
598 unlock_exit:
599 	if (!prereg)
600 		arch_spin_unlock(&kvm->mmu_lock.rlock.raw_lock);
601 	return ret;
602 }
603 
kvmppc_rm_h_stuff_tce(struct kvm_vcpu * vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce_value,unsigned long npages)604 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
605 		unsigned long liobn, unsigned long ioba,
606 		unsigned long tce_value, unsigned long npages)
607 {
608 	struct kvmppc_spapr_tce_table *stt;
609 	long i, ret;
610 	struct kvmppc_spapr_tce_iommu_table *stit;
611 
612 	/* For radix, we might be in virtual mode, so punt */
613 	if (kvm_is_radix(vcpu->kvm))
614 		return H_TOO_HARD;
615 
616 	stt = kvmppc_find_table(vcpu->kvm, liobn);
617 	if (!stt)
618 		return H_TOO_HARD;
619 
620 	ret = kvmppc_rm_ioba_validate(stt, ioba, npages, tce_value == 0);
621 	if (ret != H_SUCCESS)
622 		return ret;
623 
624 	/* Check permission bits only to allow userspace poison TCE for debug */
625 	if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
626 		return H_PARAMETER;
627 
628 	list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
629 		unsigned long entry = ioba >> stt->page_shift;
630 
631 		for (i = 0; i < npages; ++i) {
632 			ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
633 					stit->tbl, entry + i);
634 
635 			if (ret == H_SUCCESS)
636 				continue;
637 
638 			if (ret == H_TOO_HARD)
639 				return ret;
640 
641 			WARN_ON_ONCE_RM(1);
642 			kvmppc_rm_clear_tce(vcpu->kvm, stt, stit->tbl, entry + i);
643 		}
644 	}
645 
646 	for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
647 		kvmppc_rm_tce_put(stt, ioba >> stt->page_shift, tce_value);
648 
649 	return ret;
650 }
651 
652 /* This can be called in either virtual mode or real mode */
kvmppc_h_get_tce(struct kvm_vcpu * vcpu,unsigned long liobn,unsigned long ioba)653 long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
654 		      unsigned long ioba)
655 {
656 	struct kvmppc_spapr_tce_table *stt;
657 	long ret;
658 	unsigned long idx;
659 	struct page *page;
660 	u64 *tbl;
661 
662 	stt = kvmppc_find_table(vcpu->kvm, liobn);
663 	if (!stt)
664 		return H_TOO_HARD;
665 
666 	ret = kvmppc_ioba_validate(stt, ioba, 1);
667 	if (ret != H_SUCCESS)
668 		return ret;
669 
670 	idx = (ioba >> stt->page_shift) - stt->offset;
671 	page = stt->pages[idx / TCES_PER_PAGE];
672 	if (!page) {
673 		vcpu->arch.regs.gpr[4] = 0;
674 		return H_SUCCESS;
675 	}
676 	tbl = (u64 *)page_address(page);
677 
678 	vcpu->arch.regs.gpr[4] = tbl[idx % TCES_PER_PAGE];
679 
680 	return H_SUCCESS;
681 }
682 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
683 
684 #endif /* KVM_BOOK3S_HV_POSSIBLE */
685