book3s_64_vio_hv.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kvm.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/highmem.h>
  24. #include <linux/gfp.h>
  25. #include <linux/slab.h>
  26. #include <linux/hugetlb.h>
  27. #include <linux/list.h>
  28. #include <linux/stringify.h>
  29. #include <asm/kvm_ppc.h>
  30. #include <asm/kvm_book3s.h>
  31. #include <asm/book3s/64/mmu-hash.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/hvcall.h>
  34. #include <asm/synch.h>
  35. #include <asm/ppc-opcode.h>
  36. #include <asm/kvm_host.h>
  37. #include <asm/udbg.h>
  38. #include <asm/iommu.h>
  39. #include <asm/tce.h>
  40. #include <asm/pte-walk.h>
  41. #ifdef CONFIG_BUG
  42. #define WARN_ON_ONCE_RM(condition) ({ \
  43. static bool __section(.data.unlikely) __warned; \
  44. int __ret_warn_once = !!(condition); \
  45. \
  46. if (unlikely(__ret_warn_once && !__warned)) { \
  47. __warned = true; \
  48. pr_err("WARN_ON_ONCE_RM: (%s) at %s:%u\n", \
  49. __stringify(condition), \
  50. __func__, __LINE__); \
  51. dump_stack(); \
  52. } \
  53. unlikely(__ret_warn_once); \
  54. })
  55. #else
  56. #define WARN_ON_ONCE_RM(condition) ({ \
  57. int __ret_warn_on = !!(condition); \
  58. unlikely(__ret_warn_on); \
  59. })
  60. #endif
  61. #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
  62. /*
  63. * Finds a TCE table descriptor by LIOBN.
  64. *
  65. * WARNING: This will be called in real or virtual mode on HV KVM and virtual
  66. * mode on PR KVM
  67. */
  68. struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
  69. unsigned long liobn)
  70. {
  71. struct kvmppc_spapr_tce_table *stt;
  72. list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
  73. if (stt->liobn == liobn)
  74. return stt;
  75. return NULL;
  76. }
  77. EXPORT_SYMBOL_GPL(kvmppc_find_table);
  78. /*
  79. * Validates TCE address.
  80. * At the moment flags and page mask are validated.
  81. * As the host kernel does not access those addresses (just puts them
  82. * to the table and user space is supposed to process them), we can skip
  83. * checking other things (such as TCE is a guest RAM address or the page
  84. * was actually allocated).
  85. *
  86. * WARNING: This will be called in real-mode on HV KVM and virtual
  87. * mode on PR KVM
  88. */
  89. long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt, unsigned long tce)
  90. {
  91. unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
  92. enum dma_data_direction dir = iommu_tce_direction(tce);
  93. /* Allow userspace to poison TCE table */
  94. if (dir == DMA_NONE)
  95. return H_SUCCESS;
  96. if (iommu_tce_check_gpa(stt->page_shift, gpa))
  97. return H_PARAMETER;
  98. return H_SUCCESS;
  99. }
  100. EXPORT_SYMBOL_GPL(kvmppc_tce_validate);
  101. /* Note on the use of page_address() in real mode,
  102. *
  103. * It is safe to use page_address() in real mode on ppc64 because
  104. * page_address() is always defined as lowmem_page_address()
  105. * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
  106. * operation and does not access page struct.
  107. *
  108. * Theoretically page_address() could be defined different
  109. * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
  110. * would have to be enabled.
  111. * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
  112. * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
  113. * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
  114. * is not expected to be enabled on ppc32, page_address()
  115. * is safe for ppc32 as well.
  116. *
  117. * WARNING: This will be called in real-mode on HV KVM and virtual
  118. * mode on PR KVM
  119. */
  120. static u64 *kvmppc_page_address(struct page *page)
  121. {
  122. #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
  123. #error TODO: fix to avoid page_address() here
  124. #endif
  125. return (u64 *) page_address(page);
  126. }
  127. /*
  128. * Handles TCE requests for emulated devices.
  129. * Puts guest TCE values to the table and expects user space to convert them.
  130. * Called in both real and virtual modes.
  131. * Cannot fail so kvmppc_tce_validate must be called before it.
  132. *
  133. * WARNING: This will be called in real-mode on HV KVM and virtual
  134. * mode on PR KVM
  135. */
  136. void kvmppc_tce_put(struct kvmppc_spapr_tce_table *stt,
  137. unsigned long idx, unsigned long tce)
  138. {
  139. struct page *page;
  140. u64 *tbl;
  141. idx -= stt->offset;
  142. page = stt->pages[idx / TCES_PER_PAGE];
  143. tbl = kvmppc_page_address(page);
  144. tbl[idx % TCES_PER_PAGE] = tce;
  145. }
  146. EXPORT_SYMBOL_GPL(kvmppc_tce_put);
  147. long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
  148. unsigned long *ua, unsigned long **prmap)
  149. {
  150. unsigned long gfn = gpa >> PAGE_SHIFT;
  151. struct kvm_memory_slot *memslot;
  152. memslot = search_memslots(kvm_memslots(kvm), gfn);
  153. if (!memslot)
  154. return -EINVAL;
  155. *ua = __gfn_to_hva_memslot(memslot, gfn) |
  156. (gpa & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
  157. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  158. if (prmap)
  159. *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
  160. #endif
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua);
  164. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  165. static long iommu_tce_xchg_rm(struct mm_struct *mm, struct iommu_table *tbl,
  166. unsigned long entry, unsigned long *hpa,
  167. enum dma_data_direction *direction)
  168. {
  169. long ret;
  170. ret = tbl->it_ops->exchange_rm(tbl, entry, hpa, direction);
  171. if (!ret && ((*direction == DMA_FROM_DEVICE) ||
  172. (*direction == DMA_BIDIRECTIONAL))) {
  173. __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
  174. /*
  175. * kvmppc_rm_tce_iommu_do_map() updates the UA cache after
  176. * calling this so we still get here a valid UA.
  177. */
  178. if (pua && *pua)
  179. mm_iommu_ua_mark_dirty_rm(mm, be64_to_cpu(*pua));
  180. }
  181. return ret;
  182. }
  183. static void kvmppc_rm_clear_tce(struct kvm *kvm, struct iommu_table *tbl,
  184. unsigned long entry)
  185. {
  186. unsigned long hpa = 0;
  187. enum dma_data_direction dir = DMA_NONE;
  188. iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
  189. }
  190. static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm *kvm,
  191. struct iommu_table *tbl, unsigned long entry)
  192. {
  193. struct mm_iommu_table_group_mem_t *mem = NULL;
  194. const unsigned long pgsize = 1ULL << tbl->it_page_shift;
  195. __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
  196. if (!pua)
  197. /* it_userspace allocation might be delayed */
  198. return H_TOO_HARD;
  199. mem = mm_iommu_lookup_rm(kvm->mm, be64_to_cpu(*pua), pgsize);
  200. if (!mem)
  201. return H_TOO_HARD;
  202. mm_iommu_mapped_dec(mem);
  203. *pua = cpu_to_be64(0);
  204. return H_SUCCESS;
  205. }
  206. static long kvmppc_rm_tce_iommu_do_unmap(struct kvm *kvm,
  207. struct iommu_table *tbl, unsigned long entry)
  208. {
  209. enum dma_data_direction dir = DMA_NONE;
  210. unsigned long hpa = 0;
  211. long ret;
  212. if (iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir))
  213. /*
  214. * real mode xchg can fail if struct page crosses
  215. * a page boundary
  216. */
  217. return H_TOO_HARD;
  218. if (dir == DMA_NONE)
  219. return H_SUCCESS;
  220. ret = kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
  221. if (ret)
  222. iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
  223. return ret;
  224. }
  225. static long kvmppc_rm_tce_iommu_unmap(struct kvm *kvm,
  226. struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
  227. unsigned long entry)
  228. {
  229. unsigned long i, ret = H_SUCCESS;
  230. unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
  231. unsigned long io_entry = entry * subpages;
  232. for (i = 0; i < subpages; ++i) {
  233. ret = kvmppc_rm_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
  234. if (ret != H_SUCCESS)
  235. break;
  236. }
  237. return ret;
  238. }
  239. static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
  240. unsigned long entry, unsigned long ua,
  241. enum dma_data_direction dir)
  242. {
  243. long ret;
  244. unsigned long hpa = 0;
  245. __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
  246. struct mm_iommu_table_group_mem_t *mem;
  247. if (!pua)
  248. /* it_userspace allocation might be delayed */
  249. return H_TOO_HARD;
  250. mem = mm_iommu_lookup_rm(kvm->mm, ua, 1ULL << tbl->it_page_shift);
  251. if (!mem)
  252. return H_TOO_HARD;
  253. if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, tbl->it_page_shift,
  254. &hpa)))
  255. return H_TOO_HARD;
  256. if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem)))
  257. return H_TOO_HARD;
  258. ret = iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
  259. if (ret) {
  260. mm_iommu_mapped_dec(mem);
  261. /*
  262. * real mode xchg can fail if struct page crosses
  263. * a page boundary
  264. */
  265. return H_TOO_HARD;
  266. }
  267. if (dir != DMA_NONE)
  268. kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
  269. *pua = cpu_to_be64(ua);
  270. return 0;
  271. }
  272. static long kvmppc_rm_tce_iommu_map(struct kvm *kvm,
  273. struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
  274. unsigned long entry, unsigned long ua,
  275. enum dma_data_direction dir)
  276. {
  277. unsigned long i, pgoff, ret = H_SUCCESS;
  278. unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
  279. unsigned long io_entry = entry * subpages;
  280. for (i = 0, pgoff = 0; i < subpages;
  281. ++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {
  282. ret = kvmppc_rm_tce_iommu_do_map(kvm, tbl,
  283. io_entry + i, ua + pgoff, dir);
  284. if (ret != H_SUCCESS)
  285. break;
  286. }
  287. return ret;
  288. }
  289. long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  290. unsigned long ioba, unsigned long tce)
  291. {
  292. struct kvmppc_spapr_tce_table *stt;
  293. long ret;
  294. struct kvmppc_spapr_tce_iommu_table *stit;
  295. unsigned long entry, ua = 0;
  296. enum dma_data_direction dir;
  297. /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
  298. /* liobn, ioba, tce); */
  299. /* For radix, we might be in virtual mode, so punt */
  300. if (kvm_is_radix(vcpu->kvm))
  301. return H_TOO_HARD;
  302. stt = kvmppc_find_table(vcpu->kvm, liobn);
  303. if (!stt)
  304. return H_TOO_HARD;
  305. ret = kvmppc_ioba_validate(stt, ioba, 1);
  306. if (ret != H_SUCCESS)
  307. return ret;
  308. ret = kvmppc_tce_validate(stt, tce);
  309. if (ret != H_SUCCESS)
  310. return ret;
  311. dir = iommu_tce_direction(tce);
  312. if ((dir != DMA_NONE) && kvmppc_gpa_to_ua(vcpu->kvm,
  313. tce & ~(TCE_PCI_READ | TCE_PCI_WRITE), &ua, NULL))
  314. return H_PARAMETER;
  315. entry = ioba >> stt->page_shift;
  316. list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
  317. if (dir == DMA_NONE)
  318. ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
  319. stit->tbl, entry);
  320. else
  321. ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
  322. stit->tbl, entry, ua, dir);
  323. if (ret == H_SUCCESS)
  324. continue;
  325. if (ret == H_TOO_HARD)
  326. return ret;
  327. WARN_ON_ONCE_RM(1);
  328. kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
  329. }
  330. kvmppc_tce_put(stt, entry, tce);
  331. return H_SUCCESS;
  332. }
  333. static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu,
  334. unsigned long ua, unsigned long *phpa)
  335. {
  336. pte_t *ptep, pte;
  337. unsigned shift = 0;
  338. /*
  339. * Called in real mode with MSR_EE = 0. We are safe here.
  340. * It is ok to do the lookup with arch.pgdir here, because
  341. * we are doing this on secondary cpus and current task there
  342. * is not the hypervisor. Also this is safe against THP in the
  343. * host, because an IPI to primary thread will wait for the secondary
  344. * to exit which will agains result in the below page table walk
  345. * to finish.
  346. */
  347. ptep = __find_linux_pte(vcpu->arch.pgdir, ua, NULL, &shift);
  348. if (!ptep || !pte_present(*ptep))
  349. return -ENXIO;
  350. pte = *ptep;
  351. if (!shift)
  352. shift = PAGE_SHIFT;
  353. /* Avoid handling anything potentially complicated in realmode */
  354. if (shift > PAGE_SHIFT)
  355. return -EAGAIN;
  356. if (!pte_young(pte))
  357. return -EAGAIN;
  358. *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
  359. (ua & ~PAGE_MASK);
  360. return 0;
  361. }
  362. long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
  363. unsigned long liobn, unsigned long ioba,
  364. unsigned long tce_list, unsigned long npages)
  365. {
  366. struct kvmppc_spapr_tce_table *stt;
  367. long i, ret = H_SUCCESS;
  368. unsigned long tces, entry, ua = 0;
  369. unsigned long *rmap = NULL;
  370. bool prereg = false;
  371. struct kvmppc_spapr_tce_iommu_table *stit;
  372. /* For radix, we might be in virtual mode, so punt */
  373. if (kvm_is_radix(vcpu->kvm))
  374. return H_TOO_HARD;
  375. stt = kvmppc_find_table(vcpu->kvm, liobn);
  376. if (!stt)
  377. return H_TOO_HARD;
  378. entry = ioba >> stt->page_shift;
  379. /*
  380. * The spec says that the maximum size of the list is 512 TCEs
  381. * so the whole table addressed resides in 4K page
  382. */
  383. if (npages > 512)
  384. return H_PARAMETER;
  385. if (tce_list & (SZ_4K - 1))
  386. return H_PARAMETER;
  387. ret = kvmppc_ioba_validate(stt, ioba, npages);
  388. if (ret != H_SUCCESS)
  389. return ret;
  390. if (mm_iommu_preregistered(vcpu->kvm->mm)) {
  391. /*
  392. * We get here if guest memory was pre-registered which
  393. * is normally VFIO case and gpa->hpa translation does not
  394. * depend on hpt.
  395. */
  396. struct mm_iommu_table_group_mem_t *mem;
  397. if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL))
  398. return H_TOO_HARD;
  399. mem = mm_iommu_lookup_rm(vcpu->kvm->mm, ua, IOMMU_PAGE_SIZE_4K);
  400. if (mem)
  401. prereg = mm_iommu_ua_to_hpa_rm(mem, ua,
  402. IOMMU_PAGE_SHIFT_4K, &tces) == 0;
  403. }
  404. if (!prereg) {
  405. /*
  406. * This is usually a case of a guest with emulated devices only
  407. * when TCE list is not in preregistered memory.
  408. * We do not require memory to be preregistered in this case
  409. * so lock rmap and do __find_linux_pte_or_hugepte().
  410. */
  411. if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, &rmap))
  412. return H_TOO_HARD;
  413. rmap = (void *) vmalloc_to_phys(rmap);
  414. if (WARN_ON_ONCE_RM(!rmap))
  415. return H_TOO_HARD;
  416. /*
  417. * Synchronize with the MMU notifier callbacks in
  418. * book3s_64_mmu_hv.c (kvm_unmap_hva_range_hv etc.).
  419. * While we have the rmap lock, code running on other CPUs
  420. * cannot finish unmapping the host real page that backs
  421. * this guest real page, so we are OK to access the host
  422. * real page.
  423. */
  424. lock_rmap(rmap);
  425. if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) {
  426. ret = H_TOO_HARD;
  427. goto unlock_exit;
  428. }
  429. }
  430. for (i = 0; i < npages; ++i) {
  431. unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
  432. ret = kvmppc_tce_validate(stt, tce);
  433. if (ret != H_SUCCESS)
  434. goto unlock_exit;
  435. ua = 0;
  436. if (kvmppc_gpa_to_ua(vcpu->kvm,
  437. tce & ~(TCE_PCI_READ | TCE_PCI_WRITE),
  438. &ua, NULL)) {
  439. ret = H_PARAMETER;
  440. goto unlock_exit;
  441. }
  442. list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
  443. ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
  444. stit->tbl, entry + i, ua,
  445. iommu_tce_direction(tce));
  446. if (ret == H_SUCCESS)
  447. continue;
  448. if (ret == H_TOO_HARD)
  449. goto unlock_exit;
  450. WARN_ON_ONCE_RM(1);
  451. kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
  452. }
  453. kvmppc_tce_put(stt, entry + i, tce);
  454. }
  455. unlock_exit:
  456. if (rmap)
  457. unlock_rmap(rmap);
  458. return ret;
  459. }
  460. long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
  461. unsigned long liobn, unsigned long ioba,
  462. unsigned long tce_value, unsigned long npages)
  463. {
  464. struct kvmppc_spapr_tce_table *stt;
  465. long i, ret;
  466. struct kvmppc_spapr_tce_iommu_table *stit;
  467. /* For radix, we might be in virtual mode, so punt */
  468. if (kvm_is_radix(vcpu->kvm))
  469. return H_TOO_HARD;
  470. stt = kvmppc_find_table(vcpu->kvm, liobn);
  471. if (!stt)
  472. return H_TOO_HARD;
  473. ret = kvmppc_ioba_validate(stt, ioba, npages);
  474. if (ret != H_SUCCESS)
  475. return ret;
  476. /* Check permission bits only to allow userspace poison TCE for debug */
  477. if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
  478. return H_PARAMETER;
  479. list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
  480. unsigned long entry = ioba >> stt->page_shift;
  481. for (i = 0; i < npages; ++i) {
  482. ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
  483. stit->tbl, entry + i);
  484. if (ret == H_SUCCESS)
  485. continue;
  486. if (ret == H_TOO_HARD)
  487. return ret;
  488. WARN_ON_ONCE_RM(1);
  489. kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
  490. }
  491. }
  492. for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
  493. kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
  494. return H_SUCCESS;
  495. }
  496. /* This can be called in either virtual mode or real mode */
  497. long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  498. unsigned long ioba)
  499. {
  500. struct kvmppc_spapr_tce_table *stt;
  501. long ret;
  502. unsigned long idx;
  503. struct page *page;
  504. u64 *tbl;
  505. stt = kvmppc_find_table(vcpu->kvm, liobn);
  506. if (!stt)
  507. return H_TOO_HARD;
  508. ret = kvmppc_ioba_validate(stt, ioba, 1);
  509. if (ret != H_SUCCESS)
  510. return ret;
  511. idx = (ioba >> stt->page_shift) - stt->offset;
  512. page = stt->pages[idx / TCES_PER_PAGE];
  513. tbl = (u64 *)page_address(page);
  514. vcpu->arch.regs.gpr[4] = tbl[idx % TCES_PER_PAGE];
  515. return H_SUCCESS;
  516. }
  517. EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
  518. #endif /* KVM_BOOK3S_HV_POSSIBLE */