OSDN Git Service

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