e500_mmu_host.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * Copyright (C) 2008-2013 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, yu.liu@freescale.com
  5. * Scott Wood, scottwood@freescale.com
  6. * Ashish Kalra, ashish.kalra@freescale.com
  7. * Varun Sethi, varun.sethi@freescale.com
  8. * Alexander Graf, agraf@suse.de
  9. *
  10. * Description:
  11. * This file is based on arch/powerpc/kvm/44x_tlb.c,
  12. * by Hollis Blanchard <hollisb@us.ibm.com>.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License, version 2, as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/slab.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/log2.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/sched.h>
  28. #include <linux/rwsem.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/hugetlb.h>
  31. #include <asm/kvm_ppc.h>
  32. #include "e500.h"
  33. #include "timing.h"
  34. #include "e500_mmu_host.h"
  35. #include "trace_booke.h"
  36. #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
  37. static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
  38. static inline unsigned int tlb1_max_shadow_size(void)
  39. {
  40. /* reserve one entry for magic page */
  41. return host_tlb_params[1].entries - tlbcam_index - 1;
  42. }
  43. static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
  44. {
  45. /* Mask off reserved bits. */
  46. mas3 &= MAS3_ATTRIB_MASK;
  47. #ifndef CONFIG_KVM_BOOKE_HV
  48. if (!usermode) {
  49. /* Guest is in supervisor mode,
  50. * so we need to translate guest
  51. * supervisor permissions into user permissions. */
  52. mas3 &= ~E500_TLB_USER_PERM_MASK;
  53. mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
  54. }
  55. mas3 |= E500_TLB_SUPER_PERM_MASK;
  56. #endif
  57. return mas3;
  58. }
  59. /*
  60. * writing shadow tlb entry to host TLB
  61. */
  62. static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
  63. uint32_t mas0)
  64. {
  65. unsigned long flags;
  66. local_irq_save(flags);
  67. mtspr(SPRN_MAS0, mas0);
  68. mtspr(SPRN_MAS1, stlbe->mas1);
  69. mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
  70. mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
  71. mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
  72. #ifdef CONFIG_KVM_BOOKE_HV
  73. mtspr(SPRN_MAS8, stlbe->mas8);
  74. #endif
  75. asm volatile("isync; tlbwe" : : : "memory");
  76. #ifdef CONFIG_KVM_BOOKE_HV
  77. /* Must clear mas8 for other host tlbwe's */
  78. mtspr(SPRN_MAS8, 0);
  79. isync();
  80. #endif
  81. local_irq_restore(flags);
  82. trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
  83. stlbe->mas2, stlbe->mas7_3);
  84. }
  85. /*
  86. * Acquire a mas0 with victim hint, as if we just took a TLB miss.
  87. *
  88. * We don't care about the address we're searching for, other than that it's
  89. * in the right set and is not present in the TLB. Using a zero PID and a
  90. * userspace address means we don't have to set and then restore MAS5, or
  91. * calculate a proper MAS6 value.
  92. */
  93. static u32 get_host_mas0(unsigned long eaddr)
  94. {
  95. unsigned long flags;
  96. u32 mas0;
  97. local_irq_save(flags);
  98. mtspr(SPRN_MAS6, 0);
  99. asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
  100. mas0 = mfspr(SPRN_MAS0);
  101. local_irq_restore(flags);
  102. return mas0;
  103. }
  104. /* sesel is for tlb1 only */
  105. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  106. int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
  107. {
  108. u32 mas0;
  109. if (tlbsel == 0) {
  110. mas0 = get_host_mas0(stlbe->mas2);
  111. __write_host_tlbe(stlbe, mas0);
  112. } else {
  113. __write_host_tlbe(stlbe,
  114. MAS0_TLBSEL(1) |
  115. MAS0_ESEL(to_htlb1_esel(sesel)));
  116. }
  117. }
  118. /* sesel is for tlb1 only */
  119. static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  120. struct kvm_book3e_206_tlb_entry *gtlbe,
  121. struct kvm_book3e_206_tlb_entry *stlbe,
  122. int stlbsel, int sesel)
  123. {
  124. int stid;
  125. preempt_disable();
  126. stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
  127. stlbe->mas1 |= MAS1_TID(stid);
  128. write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
  129. preempt_enable();
  130. }
  131. #ifdef CONFIG_KVM_E500V2
  132. /* XXX should be a hook in the gva2hpa translation */
  133. void kvmppc_map_magic(struct kvm_vcpu *vcpu)
  134. {
  135. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  136. struct kvm_book3e_206_tlb_entry magic;
  137. ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
  138. unsigned int stid;
  139. pfn_t pfn;
  140. pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
  141. get_page(pfn_to_page(pfn));
  142. preempt_disable();
  143. stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
  144. magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
  145. MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  146. magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
  147. magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  148. MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
  149. magic.mas8 = 0;
  150. __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
  151. preempt_enable();
  152. }
  153. #endif
  154. void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
  155. int esel)
  156. {
  157. struct kvm_book3e_206_tlb_entry *gtlbe =
  158. get_entry(vcpu_e500, tlbsel, esel);
  159. struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[tlbsel][esel].ref;
  160. /* Don't bother with unmapped entries */
  161. if (!(ref->flags & E500_TLB_VALID)) {
  162. WARN(ref->flags & (E500_TLB_BITMAP | E500_TLB_TLB0),
  163. "%s: flags %x\n", __func__, ref->flags);
  164. WARN_ON(tlbsel == 1 && vcpu_e500->g2h_tlb1_map[esel]);
  165. }
  166. if (tlbsel == 1 && ref->flags & E500_TLB_BITMAP) {
  167. u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
  168. int hw_tlb_indx;
  169. unsigned long flags;
  170. local_irq_save(flags);
  171. while (tmp) {
  172. hw_tlb_indx = __ilog2_u64(tmp & -tmp);
  173. mtspr(SPRN_MAS0,
  174. MAS0_TLBSEL(1) |
  175. MAS0_ESEL(to_htlb1_esel(hw_tlb_indx)));
  176. mtspr(SPRN_MAS1, 0);
  177. asm volatile("tlbwe");
  178. vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0;
  179. tmp &= tmp - 1;
  180. }
  181. mb();
  182. vcpu_e500->g2h_tlb1_map[esel] = 0;
  183. ref->flags &= ~(E500_TLB_BITMAP | E500_TLB_VALID);
  184. local_irq_restore(flags);
  185. }
  186. if (tlbsel == 1 && ref->flags & E500_TLB_TLB0) {
  187. /*
  188. * TLB1 entry is backed by 4k pages. This should happen
  189. * rarely and is not worth optimizing. Invalidate everything.
  190. */
  191. kvmppc_e500_tlbil_all(vcpu_e500);
  192. ref->flags &= ~(E500_TLB_TLB0 | E500_TLB_VALID);
  193. }
  194. /*
  195. * If TLB entry is still valid then it's a TLB0 entry, and thus
  196. * backed by at most one host tlbe per shadow pid
  197. */
  198. if (ref->flags & E500_TLB_VALID)
  199. kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
  200. /* Mark the TLB as not backed by the host anymore */
  201. ref->flags = 0;
  202. }
  203. static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
  204. {
  205. return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
  206. }
  207. static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
  208. struct kvm_book3e_206_tlb_entry *gtlbe,
  209. pfn_t pfn, unsigned int wimg)
  210. {
  211. ref->pfn = pfn;
  212. ref->flags = E500_TLB_VALID;
  213. /* Use guest supplied MAS2_G and MAS2_E */
  214. ref->flags |= (gtlbe->mas2 & MAS2_ATTRIB_MASK) | wimg;
  215. /* Mark the page accessed */
  216. kvm_set_pfn_accessed(pfn);
  217. if (tlbe_is_writable(gtlbe))
  218. kvm_set_pfn_dirty(pfn);
  219. }
  220. static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
  221. {
  222. if (ref->flags & E500_TLB_VALID) {
  223. /* FIXME: don't log bogus pfn for TLB1 */
  224. trace_kvm_booke206_ref_release(ref->pfn, ref->flags);
  225. ref->flags = 0;
  226. }
  227. }
  228. static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
  229. {
  230. if (vcpu_e500->g2h_tlb1_map)
  231. memset(vcpu_e500->g2h_tlb1_map, 0,
  232. sizeof(u64) * vcpu_e500->gtlb_params[1].entries);
  233. if (vcpu_e500->h2g_tlb1_rmap)
  234. memset(vcpu_e500->h2g_tlb1_rmap, 0,
  235. sizeof(unsigned int) * host_tlb_params[1].entries);
  236. }
  237. static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
  238. {
  239. int tlbsel;
  240. int i;
  241. for (tlbsel = 0; tlbsel <= 1; tlbsel++) {
  242. for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
  243. struct tlbe_ref *ref =
  244. &vcpu_e500->gtlb_priv[tlbsel][i].ref;
  245. kvmppc_e500_ref_release(ref);
  246. }
  247. }
  248. }
  249. void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu)
  250. {
  251. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  252. kvmppc_e500_tlbil_all(vcpu_e500);
  253. clear_tlb_privs(vcpu_e500);
  254. clear_tlb1_bitmap(vcpu_e500);
  255. }
  256. /* TID must be supplied by the caller */
  257. static void kvmppc_e500_setup_stlbe(
  258. struct kvm_vcpu *vcpu,
  259. struct kvm_book3e_206_tlb_entry *gtlbe,
  260. int tsize, struct tlbe_ref *ref, u64 gvaddr,
  261. struct kvm_book3e_206_tlb_entry *stlbe)
  262. {
  263. pfn_t pfn = ref->pfn;
  264. u32 pr = vcpu->arch.shared->msr & MSR_PR;
  265. BUG_ON(!(ref->flags & E500_TLB_VALID));
  266. /* Force IPROT=0 for all guest mappings. */
  267. stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
  268. stlbe->mas2 = (gvaddr & MAS2_EPN) | (ref->flags & E500_TLB_MAS2_ATTR);
  269. stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  270. e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
  271. #ifdef CONFIG_KVM_BOOKE_HV
  272. stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid;
  273. #endif
  274. }
  275. static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  276. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  277. int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
  278. struct tlbe_ref *ref)
  279. {
  280. struct kvm_memory_slot *slot;
  281. unsigned long pfn = 0; /* silence GCC warning */
  282. unsigned long hva;
  283. int pfnmap = 0;
  284. int tsize = BOOK3E_PAGESZ_4K;
  285. int ret = 0;
  286. unsigned long mmu_seq;
  287. struct kvm *kvm = vcpu_e500->vcpu.kvm;
  288. unsigned long tsize_pages = 0;
  289. pte_t *ptep;
  290. unsigned int wimg = 0;
  291. pgd_t *pgdir;
  292. /* used to check for invalidations in progress */
  293. mmu_seq = kvm->mmu_notifier_seq;
  294. smp_rmb();
  295. /*
  296. * Translate guest physical to true physical, acquiring
  297. * a page reference if it is normal, non-reserved memory.
  298. *
  299. * gfn_to_memslot() must succeed because otherwise we wouldn't
  300. * have gotten this far. Eventually we should just pass the slot
  301. * pointer through from the first lookup.
  302. */
  303. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  304. hva = gfn_to_hva_memslot(slot, gfn);
  305. if (tlbsel == 1) {
  306. struct vm_area_struct *vma;
  307. down_read(&current->mm->mmap_sem);
  308. vma = find_vma(current->mm, hva);
  309. if (vma && hva >= vma->vm_start &&
  310. (vma->vm_flags & VM_PFNMAP)) {
  311. /*
  312. * This VMA is a physically contiguous region (e.g.
  313. * /dev/mem) that bypasses normal Linux page
  314. * management. Find the overlap between the
  315. * vma and the memslot.
  316. */
  317. unsigned long start, end;
  318. unsigned long slot_start, slot_end;
  319. pfnmap = 1;
  320. start = vma->vm_pgoff;
  321. end = start +
  322. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  323. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  324. slot_start = pfn - (gfn - slot->base_gfn);
  325. slot_end = slot_start + slot->npages;
  326. if (start < slot_start)
  327. start = slot_start;
  328. if (end > slot_end)
  329. end = slot_end;
  330. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  331. MAS1_TSIZE_SHIFT;
  332. /*
  333. * e500 doesn't implement the lowest tsize bit,
  334. * or 1K pages.
  335. */
  336. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  337. /*
  338. * Now find the largest tsize (up to what the guest
  339. * requested) that will cover gfn, stay within the
  340. * range, and for which gfn and pfn are mutually
  341. * aligned.
  342. */
  343. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  344. unsigned long gfn_start, gfn_end;
  345. tsize_pages = 1 << (tsize - 2);
  346. gfn_start = gfn & ~(tsize_pages - 1);
  347. gfn_end = gfn_start + tsize_pages;
  348. if (gfn_start + pfn - gfn < start)
  349. continue;
  350. if (gfn_end + pfn - gfn > end)
  351. continue;
  352. if ((gfn & (tsize_pages - 1)) !=
  353. (pfn & (tsize_pages - 1)))
  354. continue;
  355. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  356. pfn &= ~(tsize_pages - 1);
  357. break;
  358. }
  359. } else if (vma && hva >= vma->vm_start &&
  360. (vma->vm_flags & VM_HUGETLB)) {
  361. unsigned long psize = vma_kernel_pagesize(vma);
  362. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  363. MAS1_TSIZE_SHIFT;
  364. /*
  365. * Take the largest page size that satisfies both host
  366. * and guest mapping
  367. */
  368. tsize = min(__ilog2(psize) - 10, tsize);
  369. /*
  370. * e500 doesn't implement the lowest tsize bit,
  371. * or 1K pages.
  372. */
  373. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  374. }
  375. up_read(&current->mm->mmap_sem);
  376. }
  377. if (likely(!pfnmap)) {
  378. tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
  379. pfn = gfn_to_pfn_memslot(slot, gfn);
  380. if (is_error_noslot_pfn(pfn)) {
  381. if (printk_ratelimit())
  382. pr_err("%s: real page not found for gfn %lx\n",
  383. __func__, (long)gfn);
  384. return -EINVAL;
  385. }
  386. /* Align guest and physical address to page map boundaries */
  387. pfn &= ~(tsize_pages - 1);
  388. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  389. }
  390. spin_lock(&kvm->mmu_lock);
  391. if (mmu_notifier_retry(kvm, mmu_seq)) {
  392. ret = -EAGAIN;
  393. goto out;
  394. }
  395. pgdir = vcpu_e500->vcpu.arch.pgdir;
  396. ptep = lookup_linux_ptep(pgdir, hva, &tsize_pages);
  397. if (pte_present(*ptep))
  398. wimg = (*ptep >> PTE_WIMGE_SHIFT) & MAS2_WIMGE_MASK;
  399. else {
  400. if (printk_ratelimit())
  401. pr_err("%s: pte not present: gfn %lx, pfn %lx\n",
  402. __func__, (long)gfn, pfn);
  403. ret = -EINVAL;
  404. goto out;
  405. }
  406. kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg);
  407. kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
  408. ref, gvaddr, stlbe);
  409. /* Clear i-cache for new pages */
  410. kvmppc_mmu_flush_icache(pfn);
  411. out:
  412. spin_unlock(&kvm->mmu_lock);
  413. /* Drop refcount on page, so that mmu notifiers can clear it */
  414. kvm_release_pfn_clean(pfn);
  415. return ret;
  416. }
  417. /* XXX only map the one-one case, for now use TLB0 */
  418. static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, int esel,
  419. struct kvm_book3e_206_tlb_entry *stlbe)
  420. {
  421. struct kvm_book3e_206_tlb_entry *gtlbe;
  422. struct tlbe_ref *ref;
  423. int stlbsel = 0;
  424. int sesel = 0;
  425. int r;
  426. gtlbe = get_entry(vcpu_e500, 0, esel);
  427. ref = &vcpu_e500->gtlb_priv[0][esel].ref;
  428. r = kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  429. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  430. gtlbe, 0, stlbe, ref);
  431. if (r)
  432. return r;
  433. write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
  434. return 0;
  435. }
  436. static int kvmppc_e500_tlb1_map_tlb1(struct kvmppc_vcpu_e500 *vcpu_e500,
  437. struct tlbe_ref *ref,
  438. int esel)
  439. {
  440. unsigned int sesel = vcpu_e500->host_tlb1_nv++;
  441. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  442. vcpu_e500->host_tlb1_nv = 0;
  443. if (vcpu_e500->h2g_tlb1_rmap[sesel]) {
  444. unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel] - 1;
  445. vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
  446. }
  447. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
  448. vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
  449. vcpu_e500->h2g_tlb1_rmap[sesel] = esel + 1;
  450. WARN_ON(!(ref->flags & E500_TLB_VALID));
  451. return sesel;
  452. }
  453. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  454. * the shadow TLB. */
  455. /* For both one-one and one-to-many */
  456. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  457. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  458. struct kvm_book3e_206_tlb_entry *stlbe, int esel)
  459. {
  460. struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[1][esel].ref;
  461. int sesel;
  462. int r;
  463. r = kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe,
  464. ref);
  465. if (r)
  466. return r;
  467. /* Use TLB0 when we can only map a page with 4k */
  468. if (get_tlb_tsize(stlbe) == BOOK3E_PAGESZ_4K) {
  469. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_TLB0;
  470. write_stlbe(vcpu_e500, gtlbe, stlbe, 0, 0);
  471. return 0;
  472. }
  473. /* Otherwise map into TLB1 */
  474. sesel = kvmppc_e500_tlb1_map_tlb1(vcpu_e500, ref, esel);
  475. write_stlbe(vcpu_e500, gtlbe, stlbe, 1, sesel);
  476. return 0;
  477. }
  478. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  479. unsigned int index)
  480. {
  481. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  482. struct tlbe_priv *priv;
  483. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  484. int tlbsel = tlbsel_of(index);
  485. int esel = esel_of(index);
  486. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  487. switch (tlbsel) {
  488. case 0:
  489. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  490. /* Triggers after clear_tlb_privs or on initial mapping */
  491. if (!(priv->ref.flags & E500_TLB_VALID)) {
  492. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  493. } else {
  494. kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
  495. &priv->ref, eaddr, &stlbe);
  496. write_stlbe(vcpu_e500, gtlbe, &stlbe, 0, 0);
  497. }
  498. break;
  499. case 1: {
  500. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  501. kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe, &stlbe,
  502. esel);
  503. break;
  504. }
  505. default:
  506. BUG();
  507. break;
  508. }
  509. }
  510. /************* MMU Notifiers *************/
  511. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  512. {
  513. trace_kvm_unmap_hva(hva);
  514. /*
  515. * Flush all shadow tlb entries everywhere. This is slow, but
  516. * we are 100% sure that we catch the to be unmapped page
  517. */
  518. kvm_flush_remote_tlbs(kvm);
  519. return 0;
  520. }
  521. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  522. {
  523. /* kvm_unmap_hva flushes everything anyways */
  524. kvm_unmap_hva(kvm, start);
  525. return 0;
  526. }
  527. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  528. {
  529. /* XXX could be more clever ;) */
  530. return 0;
  531. }
  532. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  533. {
  534. /* XXX could be more clever ;) */
  535. return 0;
  536. }
  537. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  538. {
  539. /* The page will get remapped properly on its next fault */
  540. kvm_unmap_hva(kvm, hva);
  541. }
  542. /*****************************************/
  543. int e500_mmu_host_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  544. {
  545. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  546. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  547. /*
  548. * This should never happen on real e500 hardware, but is
  549. * architecturally possible -- e.g. in some weird nested
  550. * virtualization case.
  551. */
  552. if (host_tlb_params[0].entries == 0 ||
  553. host_tlb_params[1].entries == 0) {
  554. pr_err("%s: need to know host tlb size\n", __func__);
  555. return -ENODEV;
  556. }
  557. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  558. TLBnCFG_ASSOC_SHIFT;
  559. host_tlb_params[1].ways = host_tlb_params[1].entries;
  560. if (!is_power_of_2(host_tlb_params[0].entries) ||
  561. !is_power_of_2(host_tlb_params[0].ways) ||
  562. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  563. host_tlb_params[0].ways == 0) {
  564. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  565. __func__, host_tlb_params[0].entries,
  566. host_tlb_params[0].ways);
  567. return -ENODEV;
  568. }
  569. host_tlb_params[0].sets =
  570. host_tlb_params[0].entries / host_tlb_params[0].ways;
  571. host_tlb_params[1].sets = 1;
  572. vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
  573. host_tlb_params[1].entries,
  574. GFP_KERNEL);
  575. if (!vcpu_e500->h2g_tlb1_rmap)
  576. return -EINVAL;
  577. return 0;
  578. }
  579. void e500_mmu_host_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  580. {
  581. kfree(vcpu_e500->h2g_tlb1_rmap);
  582. }