e500_mmu_host.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. return;
  171. if (tlbsel == 1 && ref->flags & E500_TLB_BITMAP) {
  172. u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
  173. int hw_tlb_indx;
  174. unsigned long flags;
  175. local_irq_save(flags);
  176. while (tmp) {
  177. hw_tlb_indx = __ilog2_u64(tmp & -tmp);
  178. mtspr(SPRN_MAS0,
  179. MAS0_TLBSEL(1) |
  180. MAS0_ESEL(to_htlb1_esel(hw_tlb_indx)));
  181. mtspr(SPRN_MAS1, 0);
  182. asm volatile("tlbwe");
  183. vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0;
  184. tmp &= tmp - 1;
  185. }
  186. mb();
  187. vcpu_e500->g2h_tlb1_map[esel] = 0;
  188. ref->flags &= ~(E500_TLB_BITMAP | E500_TLB_VALID);
  189. local_irq_restore(flags);
  190. return;
  191. }
  192. /* Guest tlbe is backed by at most one host tlbe per shadow pid. */
  193. kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
  194. /* Mark the TLB as not backed by the host anymore */
  195. ref->flags &= ~E500_TLB_VALID;
  196. }
  197. static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
  198. {
  199. return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
  200. }
  201. static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
  202. struct kvm_book3e_206_tlb_entry *gtlbe,
  203. pfn_t pfn)
  204. {
  205. ref->pfn = pfn;
  206. ref->flags = E500_TLB_VALID;
  207. if (tlbe_is_writable(gtlbe))
  208. kvm_set_pfn_dirty(pfn);
  209. }
  210. static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
  211. {
  212. if (ref->flags & E500_TLB_VALID) {
  213. trace_kvm_booke206_ref_release(ref->pfn, ref->flags);
  214. ref->flags = 0;
  215. }
  216. }
  217. void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
  218. {
  219. if (vcpu_e500->g2h_tlb1_map)
  220. memset(vcpu_e500->g2h_tlb1_map, 0,
  221. sizeof(u64) * vcpu_e500->gtlb_params[1].entries);
  222. if (vcpu_e500->h2g_tlb1_rmap)
  223. memset(vcpu_e500->h2g_tlb1_rmap, 0,
  224. sizeof(unsigned int) * host_tlb_params[1].entries);
  225. }
  226. static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
  227. {
  228. int tlbsel = 0;
  229. int i;
  230. for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
  231. struct tlbe_ref *ref =
  232. &vcpu_e500->gtlb_priv[tlbsel][i].ref;
  233. kvmppc_e500_ref_release(ref);
  234. }
  235. }
  236. void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
  237. {
  238. int stlbsel = 1;
  239. int i;
  240. kvmppc_e500_tlbil_all(vcpu_e500);
  241. for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
  242. struct tlbe_ref *ref =
  243. &vcpu_e500->tlb_refs[stlbsel][i];
  244. kvmppc_e500_ref_release(ref);
  245. }
  246. clear_tlb_privs(vcpu_e500);
  247. }
  248. void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu)
  249. {
  250. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  251. clear_tlb_refs(vcpu_e500);
  252. clear_tlb1_bitmap(vcpu_e500);
  253. }
  254. /* TID must be supplied by the caller */
  255. static void kvmppc_e500_setup_stlbe(
  256. struct kvm_vcpu *vcpu,
  257. struct kvm_book3e_206_tlb_entry *gtlbe,
  258. int tsize, struct tlbe_ref *ref, u64 gvaddr,
  259. struct kvm_book3e_206_tlb_entry *stlbe)
  260. {
  261. pfn_t pfn = ref->pfn;
  262. u32 pr = vcpu->arch.shared->msr & MSR_PR;
  263. BUG_ON(!(ref->flags & E500_TLB_VALID));
  264. /* Force IPROT=0 for all guest mappings. */
  265. stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
  266. stlbe->mas2 = (gvaddr & MAS2_EPN) |
  267. e500_shadow_mas2_attrib(gtlbe->mas2, pr);
  268. stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  269. e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
  270. #ifdef CONFIG_KVM_BOOKE_HV
  271. stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid;
  272. #endif
  273. }
  274. static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  275. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  276. int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
  277. struct tlbe_ref *ref)
  278. {
  279. struct kvm_memory_slot *slot;
  280. unsigned long pfn = 0; /* silence GCC warning */
  281. unsigned long hva;
  282. int pfnmap = 0;
  283. int tsize = BOOK3E_PAGESZ_4K;
  284. /*
  285. * Translate guest physical to true physical, acquiring
  286. * a page reference if it is normal, non-reserved memory.
  287. *
  288. * gfn_to_memslot() must succeed because otherwise we wouldn't
  289. * have gotten this far. Eventually we should just pass the slot
  290. * pointer through from the first lookup.
  291. */
  292. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  293. hva = gfn_to_hva_memslot(slot, gfn);
  294. if (tlbsel == 1) {
  295. struct vm_area_struct *vma;
  296. down_read(&current->mm->mmap_sem);
  297. vma = find_vma(current->mm, hva);
  298. if (vma && hva >= vma->vm_start &&
  299. (vma->vm_flags & VM_PFNMAP)) {
  300. /*
  301. * This VMA is a physically contiguous region (e.g.
  302. * /dev/mem) that bypasses normal Linux page
  303. * management. Find the overlap between the
  304. * vma and the memslot.
  305. */
  306. unsigned long start, end;
  307. unsigned long slot_start, slot_end;
  308. pfnmap = 1;
  309. start = vma->vm_pgoff;
  310. end = start +
  311. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  312. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  313. slot_start = pfn - (gfn - slot->base_gfn);
  314. slot_end = slot_start + slot->npages;
  315. if (start < slot_start)
  316. start = slot_start;
  317. if (end > slot_end)
  318. end = slot_end;
  319. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  320. MAS1_TSIZE_SHIFT;
  321. /*
  322. * e500 doesn't implement the lowest tsize bit,
  323. * or 1K pages.
  324. */
  325. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  326. /*
  327. * Now find the largest tsize (up to what the guest
  328. * requested) that will cover gfn, stay within the
  329. * range, and for which gfn and pfn are mutually
  330. * aligned.
  331. */
  332. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  333. unsigned long gfn_start, gfn_end, tsize_pages;
  334. tsize_pages = 1 << (tsize - 2);
  335. gfn_start = gfn & ~(tsize_pages - 1);
  336. gfn_end = gfn_start + tsize_pages;
  337. if (gfn_start + pfn - gfn < start)
  338. continue;
  339. if (gfn_end + pfn - gfn > end)
  340. continue;
  341. if ((gfn & (tsize_pages - 1)) !=
  342. (pfn & (tsize_pages - 1)))
  343. continue;
  344. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  345. pfn &= ~(tsize_pages - 1);
  346. break;
  347. }
  348. } else if (vma && hva >= vma->vm_start &&
  349. (vma->vm_flags & VM_HUGETLB)) {
  350. unsigned long psize = vma_kernel_pagesize(vma);
  351. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  352. MAS1_TSIZE_SHIFT;
  353. /*
  354. * Take the largest page size that satisfies both host
  355. * and guest mapping
  356. */
  357. tsize = min(__ilog2(psize) - 10, tsize);
  358. /*
  359. * e500 doesn't implement the lowest tsize bit,
  360. * or 1K pages.
  361. */
  362. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  363. }
  364. up_read(&current->mm->mmap_sem);
  365. }
  366. if (likely(!pfnmap)) {
  367. unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
  368. pfn = gfn_to_pfn_memslot(slot, gfn);
  369. if (is_error_noslot_pfn(pfn)) {
  370. printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
  371. (long)gfn);
  372. return -EINVAL;
  373. }
  374. /* Align guest and physical address to page map boundaries */
  375. pfn &= ~(tsize_pages - 1);
  376. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  377. }
  378. /* Drop old ref and setup new one. */
  379. kvmppc_e500_ref_release(ref);
  380. kvmppc_e500_ref_setup(ref, gtlbe, pfn);
  381. kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
  382. ref, gvaddr, stlbe);
  383. /* Clear i-cache for new pages */
  384. kvmppc_mmu_flush_icache(pfn);
  385. /* Drop refcount on page, so that mmu notifiers can clear it */
  386. kvm_release_pfn_clean(pfn);
  387. return 0;
  388. }
  389. /* XXX only map the one-one case, for now use TLB0 */
  390. static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, int esel,
  391. struct kvm_book3e_206_tlb_entry *stlbe)
  392. {
  393. struct kvm_book3e_206_tlb_entry *gtlbe;
  394. struct tlbe_ref *ref;
  395. int stlbsel = 0;
  396. int sesel = 0;
  397. int r;
  398. gtlbe = get_entry(vcpu_e500, 0, esel);
  399. ref = &vcpu_e500->gtlb_priv[0][esel].ref;
  400. r = kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  401. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  402. gtlbe, 0, stlbe, ref);
  403. if (r)
  404. return r;
  405. write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
  406. return 0;
  407. }
  408. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  409. * the shadow TLB. */
  410. /* XXX for both one-one and one-to-many , for now use TLB1 */
  411. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  412. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  413. struct kvm_book3e_206_tlb_entry *stlbe, int esel)
  414. {
  415. struct tlbe_ref *ref;
  416. unsigned int sesel;
  417. int r;
  418. int stlbsel = 1;
  419. sesel = vcpu_e500->host_tlb1_nv++;
  420. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  421. vcpu_e500->host_tlb1_nv = 0;
  422. ref = &vcpu_e500->tlb_refs[1][sesel];
  423. r = kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe,
  424. ref);
  425. if (r)
  426. return r;
  427. vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
  428. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
  429. if (vcpu_e500->h2g_tlb1_rmap[sesel]) {
  430. unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel];
  431. vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
  432. }
  433. vcpu_e500->h2g_tlb1_rmap[sesel] = esel;
  434. write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
  435. return 0;
  436. }
  437. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  438. unsigned int index)
  439. {
  440. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  441. struct tlbe_priv *priv;
  442. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  443. int tlbsel = tlbsel_of(index);
  444. int esel = esel_of(index);
  445. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  446. switch (tlbsel) {
  447. case 0:
  448. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  449. /* Triggers after clear_tlb_refs or on initial mapping */
  450. if (!(priv->ref.flags & E500_TLB_VALID)) {
  451. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  452. } else {
  453. kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
  454. &priv->ref, eaddr, &stlbe);
  455. write_stlbe(vcpu_e500, gtlbe, &stlbe, 0, 0);
  456. }
  457. break;
  458. case 1: {
  459. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  460. kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe, &stlbe,
  461. esel);
  462. break;
  463. }
  464. default:
  465. BUG();
  466. break;
  467. }
  468. }
  469. /************* MMU Notifiers *************/
  470. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  471. {
  472. trace_kvm_unmap_hva(hva);
  473. /*
  474. * Flush all shadow tlb entries everywhere. This is slow, but
  475. * we are 100% sure that we catch the to be unmapped page
  476. */
  477. kvm_flush_remote_tlbs(kvm);
  478. return 0;
  479. }
  480. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  481. {
  482. /* kvm_unmap_hva flushes everything anyways */
  483. kvm_unmap_hva(kvm, start);
  484. return 0;
  485. }
  486. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  487. {
  488. /* XXX could be more clever ;) */
  489. return 0;
  490. }
  491. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  492. {
  493. /* XXX could be more clever ;) */
  494. return 0;
  495. }
  496. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  497. {
  498. /* The page will get remapped properly on its next fault */
  499. kvm_unmap_hva(kvm, hva);
  500. }
  501. /*****************************************/
  502. int e500_mmu_host_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  503. {
  504. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  505. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  506. /*
  507. * This should never happen on real e500 hardware, but is
  508. * architecturally possible -- e.g. in some weird nested
  509. * virtualization case.
  510. */
  511. if (host_tlb_params[0].entries == 0 ||
  512. host_tlb_params[1].entries == 0) {
  513. pr_err("%s: need to know host tlb size\n", __func__);
  514. return -ENODEV;
  515. }
  516. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  517. TLBnCFG_ASSOC_SHIFT;
  518. host_tlb_params[1].ways = host_tlb_params[1].entries;
  519. if (!is_power_of_2(host_tlb_params[0].entries) ||
  520. !is_power_of_2(host_tlb_params[0].ways) ||
  521. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  522. host_tlb_params[0].ways == 0) {
  523. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  524. __func__, host_tlb_params[0].entries,
  525. host_tlb_params[0].ways);
  526. return -ENODEV;
  527. }
  528. host_tlb_params[0].sets =
  529. host_tlb_params[0].entries / host_tlb_params[0].ways;
  530. host_tlb_params[1].sets = 1;
  531. vcpu_e500->tlb_refs[0] =
  532. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
  533. GFP_KERNEL);
  534. if (!vcpu_e500->tlb_refs[0])
  535. goto err;
  536. vcpu_e500->tlb_refs[1] =
  537. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
  538. GFP_KERNEL);
  539. if (!vcpu_e500->tlb_refs[1])
  540. goto err;
  541. vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
  542. host_tlb_params[1].entries,
  543. GFP_KERNEL);
  544. if (!vcpu_e500->h2g_tlb1_rmap)
  545. goto err;
  546. return 0;
  547. err:
  548. kfree(vcpu_e500->tlb_refs[0]);
  549. kfree(vcpu_e500->tlb_refs[1]);
  550. return -EINVAL;
  551. }
  552. void e500_mmu_host_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  553. {
  554. kfree(vcpu_e500->h2g_tlb1_rmap);
  555. kfree(vcpu_e500->tlb_refs[0]);
  556. kfree(vcpu_e500->tlb_refs[1]);
  557. }