e500_mmu_host.c 17 KB

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