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