e500_tlb.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, yu.liu@freescale.com
  5. *
  6. * Description:
  7. * This file is based on arch/powerpc/kvm/44x_tlb.c,
  8. * by Hollis Blanchard <hollisb@us.ibm.com>.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, version 2, as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/highmem.h>
  21. #include <linux/log2.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/sched.h>
  24. #include <linux/rwsem.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/hugetlb.h>
  27. #include <asm/kvm_ppc.h>
  28. #include "e500.h"
  29. #include "trace.h"
  30. #include "timing.h"
  31. #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
  32. static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
  33. static inline unsigned int gtlb0_get_next_victim(
  34. struct kvmppc_vcpu_e500 *vcpu_e500)
  35. {
  36. unsigned int victim;
  37. victim = vcpu_e500->gtlb_nv[0]++;
  38. if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways))
  39. vcpu_e500->gtlb_nv[0] = 0;
  40. return victim;
  41. }
  42. static inline unsigned int tlb1_max_shadow_size(void)
  43. {
  44. /* reserve one entry for magic page */
  45. return host_tlb_params[1].entries - tlbcam_index - 1;
  46. }
  47. static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
  48. {
  49. return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
  50. }
  51. static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
  52. {
  53. /* Mask off reserved bits. */
  54. mas3 &= MAS3_ATTRIB_MASK;
  55. if (!usermode) {
  56. /* Guest is in supervisor mode,
  57. * so we need to translate guest
  58. * supervisor permissions into user permissions. */
  59. mas3 &= ~E500_TLB_USER_PERM_MASK;
  60. mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
  61. }
  62. return mas3 | E500_TLB_SUPER_PERM_MASK;
  63. }
  64. static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
  65. {
  66. #ifdef CONFIG_SMP
  67. return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
  68. #else
  69. return mas2 & MAS2_ATTRIB_MASK;
  70. #endif
  71. }
  72. /*
  73. * writing shadow tlb entry to host TLB
  74. */
  75. static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
  76. uint32_t mas0)
  77. {
  78. unsigned long flags;
  79. local_irq_save(flags);
  80. mtspr(SPRN_MAS0, mas0);
  81. mtspr(SPRN_MAS1, stlbe->mas1);
  82. mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
  83. mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
  84. mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
  85. asm volatile("isync; tlbwe" : : : "memory");
  86. local_irq_restore(flags);
  87. trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
  88. stlbe->mas2, stlbe->mas7_3);
  89. }
  90. /*
  91. * Acquire a mas0 with victim hint, as if we just took a TLB miss.
  92. *
  93. * We don't care about the address we're searching for, other than that it's
  94. * in the right set and is not present in the TLB. Using a zero PID and a
  95. * userspace address means we don't have to set and then restore MAS5, or
  96. * calculate a proper MAS6 value.
  97. */
  98. static u32 get_host_mas0(unsigned long eaddr)
  99. {
  100. unsigned long flags;
  101. u32 mas0;
  102. local_irq_save(flags);
  103. mtspr(SPRN_MAS6, 0);
  104. asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
  105. mas0 = mfspr(SPRN_MAS0);
  106. local_irq_restore(flags);
  107. return mas0;
  108. }
  109. /* sesel is for tlb1 only */
  110. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  111. int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
  112. {
  113. u32 mas0;
  114. if (tlbsel == 0) {
  115. mas0 = get_host_mas0(stlbe->mas2);
  116. __write_host_tlbe(stlbe, mas0);
  117. } else {
  118. __write_host_tlbe(stlbe,
  119. MAS0_TLBSEL(1) |
  120. MAS0_ESEL(to_htlb1_esel(sesel)));
  121. }
  122. }
  123. #ifdef CONFIG_KVM_E500
  124. void kvmppc_map_magic(struct kvm_vcpu *vcpu)
  125. {
  126. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  127. struct kvm_book3e_206_tlb_entry magic;
  128. ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
  129. unsigned int stid;
  130. pfn_t pfn;
  131. pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
  132. get_page(pfn_to_page(pfn));
  133. preempt_disable();
  134. stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
  135. magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
  136. MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  137. magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
  138. magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  139. MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
  140. magic.mas8 = 0;
  141. __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
  142. preempt_enable();
  143. }
  144. #endif
  145. static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
  146. int tlbsel, int esel)
  147. {
  148. struct kvm_book3e_206_tlb_entry *gtlbe =
  149. get_entry(vcpu_e500, tlbsel, esel);
  150. if (tlbsel == 1) {
  151. kvmppc_e500_tlbil_all(vcpu_e500);
  152. return;
  153. }
  154. /* Guest tlbe is backed by at most one host tlbe per shadow pid. */
  155. kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
  156. }
  157. static int tlb0_set_base(gva_t addr, int sets, int ways)
  158. {
  159. int set_base;
  160. set_base = (addr >> PAGE_SHIFT) & (sets - 1);
  161. set_base *= ways;
  162. return set_base;
  163. }
  164. static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
  165. {
  166. return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
  167. vcpu_e500->gtlb_params[0].ways);
  168. }
  169. static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
  170. {
  171. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  172. int esel = get_tlb_esel_bit(vcpu);
  173. if (tlbsel == 0) {
  174. esel &= vcpu_e500->gtlb_params[0].ways - 1;
  175. esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
  176. } else {
  177. esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
  178. }
  179. return esel;
  180. }
  181. /* Search the guest TLB for a matching entry. */
  182. static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
  183. gva_t eaddr, int tlbsel, unsigned int pid, int as)
  184. {
  185. int size = vcpu_e500->gtlb_params[tlbsel].entries;
  186. unsigned int set_base, offset;
  187. int i;
  188. if (tlbsel == 0) {
  189. set_base = gtlb0_set_base(vcpu_e500, eaddr);
  190. size = vcpu_e500->gtlb_params[0].ways;
  191. } else {
  192. set_base = 0;
  193. }
  194. offset = vcpu_e500->gtlb_offset[tlbsel];
  195. for (i = 0; i < size; i++) {
  196. struct kvm_book3e_206_tlb_entry *tlbe =
  197. &vcpu_e500->gtlb_arch[offset + set_base + i];
  198. unsigned int tid;
  199. if (eaddr < get_tlb_eaddr(tlbe))
  200. continue;
  201. if (eaddr > get_tlb_end(tlbe))
  202. continue;
  203. tid = get_tlb_tid(tlbe);
  204. if (tid && (tid != pid))
  205. continue;
  206. if (!get_tlb_v(tlbe))
  207. continue;
  208. if (get_tlb_ts(tlbe) != as && as != -1)
  209. continue;
  210. return set_base + i;
  211. }
  212. return -1;
  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. ref->flags |= E500_TLB_DIRTY;
  222. }
  223. static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
  224. {
  225. if (ref->flags & E500_TLB_VALID) {
  226. if (ref->flags & E500_TLB_DIRTY)
  227. kvm_release_pfn_dirty(ref->pfn);
  228. else
  229. kvm_release_pfn_clean(ref->pfn);
  230. ref->flags = 0;
  231. }
  232. }
  233. static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
  234. {
  235. int tlbsel = 0;
  236. int i;
  237. for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
  238. struct tlbe_ref *ref =
  239. &vcpu_e500->gtlb_priv[tlbsel][i].ref;
  240. kvmppc_e500_ref_release(ref);
  241. }
  242. }
  243. static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
  244. {
  245. int stlbsel = 1;
  246. int i;
  247. kvmppc_e500_tlbil_all(vcpu_e500);
  248. for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
  249. struct tlbe_ref *ref =
  250. &vcpu_e500->tlb_refs[stlbsel][i];
  251. kvmppc_e500_ref_release(ref);
  252. }
  253. clear_tlb_privs(vcpu_e500);
  254. }
  255. static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
  256. unsigned int eaddr, int as)
  257. {
  258. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  259. unsigned int victim, tsized;
  260. int tlbsel;
  261. /* since we only have two TLBs, only lower bit is used. */
  262. tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
  263. victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
  264. tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
  265. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  266. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  267. vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
  268. | MAS1_TID(get_tlbmiss_tid(vcpu))
  269. | MAS1_TSIZE(tsized);
  270. vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
  271. | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
  272. vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  273. vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
  274. | (get_cur_pid(vcpu) << 16)
  275. | (as ? MAS6_SAS : 0);
  276. }
  277. /* TID must be supplied by the caller */
  278. static inline void kvmppc_e500_setup_stlbe(
  279. struct kvm_vcpu *vcpu,
  280. struct kvm_book3e_206_tlb_entry *gtlbe,
  281. int tsize, struct tlbe_ref *ref, u64 gvaddr,
  282. struct kvm_book3e_206_tlb_entry *stlbe)
  283. {
  284. pfn_t pfn = ref->pfn;
  285. u32 pr = vcpu->arch.shared->msr & MSR_PR;
  286. BUG_ON(!(ref->flags & E500_TLB_VALID));
  287. /* Force IPROT=0 for all guest mappings. */
  288. stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
  289. stlbe->mas2 = (gvaddr & MAS2_EPN) |
  290. e500_shadow_mas2_attrib(gtlbe->mas2, pr);
  291. stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  292. e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
  293. }
  294. static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  295. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  296. int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
  297. struct tlbe_ref *ref)
  298. {
  299. struct kvm_memory_slot *slot;
  300. unsigned long pfn, hva;
  301. int pfnmap = 0;
  302. int tsize = BOOK3E_PAGESZ_4K;
  303. /*
  304. * Translate guest physical to true physical, acquiring
  305. * a page reference if it is normal, non-reserved memory.
  306. *
  307. * gfn_to_memslot() must succeed because otherwise we wouldn't
  308. * have gotten this far. Eventually we should just pass the slot
  309. * pointer through from the first lookup.
  310. */
  311. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  312. hva = gfn_to_hva_memslot(slot, gfn);
  313. if (tlbsel == 1) {
  314. struct vm_area_struct *vma;
  315. down_read(&current->mm->mmap_sem);
  316. vma = find_vma(current->mm, hva);
  317. if (vma && hva >= vma->vm_start &&
  318. (vma->vm_flags & VM_PFNMAP)) {
  319. /*
  320. * This VMA is a physically contiguous region (e.g.
  321. * /dev/mem) that bypasses normal Linux page
  322. * management. Find the overlap between the
  323. * vma and the memslot.
  324. */
  325. unsigned long start, end;
  326. unsigned long slot_start, slot_end;
  327. pfnmap = 1;
  328. start = vma->vm_pgoff;
  329. end = start +
  330. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  331. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  332. slot_start = pfn - (gfn - slot->base_gfn);
  333. slot_end = slot_start + slot->npages;
  334. if (start < slot_start)
  335. start = slot_start;
  336. if (end > slot_end)
  337. end = slot_end;
  338. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  339. MAS1_TSIZE_SHIFT;
  340. /*
  341. * e500 doesn't implement the lowest tsize bit,
  342. * or 1K pages.
  343. */
  344. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  345. /*
  346. * Now find the largest tsize (up to what the guest
  347. * requested) that will cover gfn, stay within the
  348. * range, and for which gfn and pfn are mutually
  349. * aligned.
  350. */
  351. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  352. unsigned long gfn_start, gfn_end, tsize_pages;
  353. tsize_pages = 1 << (tsize - 2);
  354. gfn_start = gfn & ~(tsize_pages - 1);
  355. gfn_end = gfn_start + tsize_pages;
  356. if (gfn_start + pfn - gfn < start)
  357. continue;
  358. if (gfn_end + pfn - gfn > end)
  359. continue;
  360. if ((gfn & (tsize_pages - 1)) !=
  361. (pfn & (tsize_pages - 1)))
  362. continue;
  363. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  364. pfn &= ~(tsize_pages - 1);
  365. break;
  366. }
  367. } else if (vma && hva >= vma->vm_start &&
  368. (vma->vm_flags & VM_HUGETLB)) {
  369. unsigned long psize = vma_kernel_pagesize(vma);
  370. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  371. MAS1_TSIZE_SHIFT;
  372. /*
  373. * Take the largest page size that satisfies both host
  374. * and guest mapping
  375. */
  376. tsize = min(__ilog2(psize) - 10, tsize);
  377. /*
  378. * e500 doesn't implement the lowest tsize bit,
  379. * or 1K pages.
  380. */
  381. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  382. }
  383. up_read(&current->mm->mmap_sem);
  384. }
  385. if (likely(!pfnmap)) {
  386. unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
  387. pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
  388. if (is_error_pfn(pfn)) {
  389. printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
  390. (long)gfn);
  391. kvm_release_pfn_clean(pfn);
  392. return;
  393. }
  394. /* Align guest and physical address to page map boundaries */
  395. pfn &= ~(tsize_pages - 1);
  396. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  397. }
  398. /* Drop old ref and setup new one. */
  399. kvmppc_e500_ref_release(ref);
  400. kvmppc_e500_ref_setup(ref, gtlbe, pfn);
  401. kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
  402. ref, gvaddr, stlbe);
  403. }
  404. /* XXX only map the one-one case, for now use TLB0 */
  405. static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  406. int esel,
  407. struct kvm_book3e_206_tlb_entry *stlbe)
  408. {
  409. struct kvm_book3e_206_tlb_entry *gtlbe;
  410. struct tlbe_ref *ref;
  411. gtlbe = get_entry(vcpu_e500, 0, esel);
  412. ref = &vcpu_e500->gtlb_priv[0][esel].ref;
  413. kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  414. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  415. gtlbe, 0, stlbe, ref);
  416. }
  417. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  418. * the shadow TLB. */
  419. /* XXX for both one-one and one-to-many , for now use TLB1 */
  420. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  421. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  422. struct kvm_book3e_206_tlb_entry *stlbe)
  423. {
  424. struct tlbe_ref *ref;
  425. unsigned int victim;
  426. victim = vcpu_e500->host_tlb1_nv++;
  427. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  428. vcpu_e500->host_tlb1_nv = 0;
  429. ref = &vcpu_e500->tlb_refs[1][victim];
  430. kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
  431. return victim;
  432. }
  433. static inline int kvmppc_e500_gtlbe_invalidate(
  434. struct kvmppc_vcpu_e500 *vcpu_e500,
  435. int tlbsel, int esel)
  436. {
  437. struct kvm_book3e_206_tlb_entry *gtlbe =
  438. get_entry(vcpu_e500, tlbsel, esel);
  439. if (unlikely(get_tlb_iprot(gtlbe)))
  440. return -1;
  441. gtlbe->mas1 = 0;
  442. return 0;
  443. }
  444. int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  445. {
  446. int esel;
  447. if (value & MMUCSR0_TLB0FI)
  448. for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
  449. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  450. if (value & MMUCSR0_TLB1FI)
  451. for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
  452. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  453. /* Invalidate all vcpu id mappings */
  454. kvmppc_e500_tlbil_all(vcpu_e500);
  455. return EMULATE_DONE;
  456. }
  457. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
  458. {
  459. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  460. unsigned int ia;
  461. int esel, tlbsel;
  462. gva_t ea;
  463. ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
  464. ia = (ea >> 2) & 0x1;
  465. /* since we only have two TLBs, only lower bit is used. */
  466. tlbsel = (ea >> 3) & 0x1;
  467. if (ia) {
  468. /* invalidate all entries */
  469. for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
  470. esel++)
  471. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  472. } else {
  473. ea &= 0xfffff000;
  474. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  475. get_cur_pid(vcpu), -1);
  476. if (esel >= 0)
  477. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  478. }
  479. /* Invalidate all vcpu id mappings */
  480. kvmppc_e500_tlbil_all(vcpu_e500);
  481. return EMULATE_DONE;
  482. }
  483. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  484. {
  485. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  486. int tlbsel, esel;
  487. struct kvm_book3e_206_tlb_entry *gtlbe;
  488. tlbsel = get_tlb_tlbsel(vcpu);
  489. esel = get_tlb_esel(vcpu, tlbsel);
  490. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  491. vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
  492. vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  493. vcpu->arch.shared->mas1 = gtlbe->mas1;
  494. vcpu->arch.shared->mas2 = gtlbe->mas2;
  495. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  496. return EMULATE_DONE;
  497. }
  498. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
  499. {
  500. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  501. int as = !!get_cur_sas(vcpu);
  502. unsigned int pid = get_cur_spid(vcpu);
  503. int esel, tlbsel;
  504. struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
  505. gva_t ea;
  506. ea = kvmppc_get_gpr(vcpu, rb);
  507. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  508. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  509. if (esel >= 0) {
  510. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  511. break;
  512. }
  513. }
  514. if (gtlbe) {
  515. esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
  516. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  517. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  518. vcpu->arch.shared->mas1 = gtlbe->mas1;
  519. vcpu->arch.shared->mas2 = gtlbe->mas2;
  520. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  521. } else {
  522. int victim;
  523. /* since we only have two TLBs, only lower bit is used. */
  524. tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
  525. victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
  526. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
  527. | MAS0_ESEL(victim)
  528. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  529. vcpu->arch.shared->mas1 =
  530. (vcpu->arch.shared->mas6 & MAS6_SPID0)
  531. | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  532. | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
  533. vcpu->arch.shared->mas2 &= MAS2_EPN;
  534. vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
  535. MAS2_ATTRIB_MASK;
  536. vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
  537. MAS3_U2 | MAS3_U3;
  538. }
  539. kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
  540. return EMULATE_DONE;
  541. }
  542. /* sesel is for tlb1 only */
  543. static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  544. struct kvm_book3e_206_tlb_entry *gtlbe,
  545. struct kvm_book3e_206_tlb_entry *stlbe,
  546. int stlbsel, int sesel)
  547. {
  548. int stid;
  549. preempt_disable();
  550. stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
  551. stlbe->mas1 |= MAS1_TID(stid);
  552. write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
  553. preempt_enable();
  554. }
  555. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  556. {
  557. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  558. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  559. int tlbsel, esel, stlbsel, sesel;
  560. tlbsel = get_tlb_tlbsel(vcpu);
  561. esel = get_tlb_esel(vcpu, tlbsel);
  562. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  563. if (get_tlb_v(gtlbe))
  564. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  565. gtlbe->mas1 = vcpu->arch.shared->mas1;
  566. gtlbe->mas2 = vcpu->arch.shared->mas2;
  567. gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
  568. trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
  569. gtlbe->mas2, gtlbe->mas7_3);
  570. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  571. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  572. u64 eaddr;
  573. u64 raddr;
  574. switch (tlbsel) {
  575. case 0:
  576. /* TLB0 */
  577. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  578. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  579. stlbsel = 0;
  580. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  581. sesel = 0; /* unused */
  582. break;
  583. case 1:
  584. /* TLB1 */
  585. eaddr = get_tlb_eaddr(gtlbe);
  586. raddr = get_tlb_raddr(gtlbe);
  587. /* Create a 4KB mapping on the host.
  588. * If the guest wanted a large page,
  589. * only the first 4KB is mapped here and the rest
  590. * are mapped on the fly. */
  591. stlbsel = 1;
  592. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  593. raddr >> PAGE_SHIFT, gtlbe, &stlbe);
  594. break;
  595. default:
  596. BUG();
  597. }
  598. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  599. }
  600. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  601. return EMULATE_DONE;
  602. }
  603. static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  604. gva_t eaddr, unsigned int pid, int as)
  605. {
  606. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  607. int esel, tlbsel;
  608. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  609. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  610. if (esel >= 0)
  611. return index_of(tlbsel, esel);
  612. }
  613. return -1;
  614. }
  615. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  616. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  617. struct kvm_translation *tr)
  618. {
  619. int index;
  620. gva_t eaddr;
  621. u8 pid;
  622. u8 as;
  623. eaddr = tr->linear_address;
  624. pid = (tr->linear_address >> 32) & 0xff;
  625. as = (tr->linear_address >> 40) & 0x1;
  626. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  627. if (index < 0) {
  628. tr->valid = 0;
  629. return 0;
  630. }
  631. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  632. /* XXX what does "writeable" and "usermode" even mean? */
  633. tr->valid = 1;
  634. return 0;
  635. }
  636. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  637. {
  638. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  639. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  640. }
  641. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  642. {
  643. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  644. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  645. }
  646. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  647. {
  648. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  649. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  650. }
  651. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  652. {
  653. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  654. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  655. }
  656. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  657. gva_t eaddr)
  658. {
  659. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  660. struct kvm_book3e_206_tlb_entry *gtlbe;
  661. u64 pgmask;
  662. gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
  663. pgmask = get_tlb_bytes(gtlbe) - 1;
  664. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  665. }
  666. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  667. {
  668. }
  669. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  670. unsigned int index)
  671. {
  672. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  673. struct tlbe_priv *priv;
  674. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  675. int tlbsel = tlbsel_of(index);
  676. int esel = esel_of(index);
  677. int stlbsel, sesel;
  678. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  679. switch (tlbsel) {
  680. case 0:
  681. stlbsel = 0;
  682. sesel = 0; /* unused */
  683. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  684. kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
  685. &priv->ref, eaddr, &stlbe);
  686. break;
  687. case 1: {
  688. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  689. stlbsel = 1;
  690. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
  691. gtlbe, &stlbe);
  692. break;
  693. }
  694. default:
  695. BUG();
  696. break;
  697. }
  698. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  699. }
  700. static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
  701. {
  702. int i;
  703. clear_tlb_refs(vcpu_e500);
  704. kfree(vcpu_e500->gtlb_priv[0]);
  705. kfree(vcpu_e500->gtlb_priv[1]);
  706. if (vcpu_e500->shared_tlb_pages) {
  707. vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
  708. PAGE_SIZE)));
  709. for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
  710. set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
  711. put_page(vcpu_e500->shared_tlb_pages[i]);
  712. }
  713. vcpu_e500->num_shared_tlb_pages = 0;
  714. vcpu_e500->shared_tlb_pages = NULL;
  715. } else {
  716. kfree(vcpu_e500->gtlb_arch);
  717. }
  718. vcpu_e500->gtlb_arch = NULL;
  719. }
  720. void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  721. {
  722. sregs->u.e.mas0 = vcpu->arch.shared->mas0;
  723. sregs->u.e.mas1 = vcpu->arch.shared->mas1;
  724. sregs->u.e.mas2 = vcpu->arch.shared->mas2;
  725. sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
  726. sregs->u.e.mas4 = vcpu->arch.shared->mas4;
  727. sregs->u.e.mas6 = vcpu->arch.shared->mas6;
  728. sregs->u.e.mmucfg = vcpu->arch.mmucfg;
  729. sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
  730. sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
  731. sregs->u.e.tlbcfg[2] = 0;
  732. sregs->u.e.tlbcfg[3] = 0;
  733. }
  734. int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  735. {
  736. if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
  737. vcpu->arch.shared->mas0 = sregs->u.e.mas0;
  738. vcpu->arch.shared->mas1 = sregs->u.e.mas1;
  739. vcpu->arch.shared->mas2 = sregs->u.e.mas2;
  740. vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
  741. vcpu->arch.shared->mas4 = sregs->u.e.mas4;
  742. vcpu->arch.shared->mas6 = sregs->u.e.mas6;
  743. }
  744. return 0;
  745. }
  746. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  747. struct kvm_config_tlb *cfg)
  748. {
  749. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  750. struct kvm_book3e_206_tlb_params params;
  751. char *virt;
  752. struct page **pages;
  753. struct tlbe_priv *privs[2] = {};
  754. size_t array_len;
  755. u32 sets;
  756. int num_pages, ret, i;
  757. if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
  758. return -EINVAL;
  759. if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
  760. sizeof(params)))
  761. return -EFAULT;
  762. if (params.tlb_sizes[1] > 64)
  763. return -EINVAL;
  764. if (params.tlb_ways[1] != params.tlb_sizes[1])
  765. return -EINVAL;
  766. if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
  767. return -EINVAL;
  768. if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
  769. return -EINVAL;
  770. if (!is_power_of_2(params.tlb_ways[0]))
  771. return -EINVAL;
  772. sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
  773. if (!is_power_of_2(sets))
  774. return -EINVAL;
  775. array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
  776. array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
  777. if (cfg->array_len < array_len)
  778. return -EINVAL;
  779. num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
  780. cfg->array / PAGE_SIZE;
  781. pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  782. if (!pages)
  783. return -ENOMEM;
  784. ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
  785. if (ret < 0)
  786. goto err_pages;
  787. if (ret != num_pages) {
  788. num_pages = ret;
  789. ret = -EFAULT;
  790. goto err_put_page;
  791. }
  792. virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
  793. if (!virt)
  794. goto err_put_page;
  795. privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
  796. GFP_KERNEL);
  797. privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
  798. GFP_KERNEL);
  799. if (!privs[0] || !privs[1])
  800. goto err_put_page;
  801. free_gtlb(vcpu_e500);
  802. vcpu_e500->gtlb_priv[0] = privs[0];
  803. vcpu_e500->gtlb_priv[1] = privs[1];
  804. vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
  805. (virt + (cfg->array & (PAGE_SIZE - 1)));
  806. vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
  807. vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
  808. vcpu_e500->gtlb_offset[0] = 0;
  809. vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
  810. vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
  811. vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  812. if (params.tlb_sizes[0] <= 2048)
  813. vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
  814. vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
  815. vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  816. vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
  817. vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
  818. vcpu_e500->shared_tlb_pages = pages;
  819. vcpu_e500->num_shared_tlb_pages = num_pages;
  820. vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
  821. vcpu_e500->gtlb_params[0].sets = sets;
  822. vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
  823. vcpu_e500->gtlb_params[1].sets = 1;
  824. return 0;
  825. err_put_page:
  826. kfree(privs[0]);
  827. kfree(privs[1]);
  828. for (i = 0; i < num_pages; i++)
  829. put_page(pages[i]);
  830. err_pages:
  831. kfree(pages);
  832. return ret;
  833. }
  834. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  835. struct kvm_dirty_tlb *dirty)
  836. {
  837. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  838. clear_tlb_refs(vcpu_e500);
  839. return 0;
  840. }
  841. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  842. {
  843. struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
  844. int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
  845. int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
  846. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  847. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  848. /*
  849. * This should never happen on real e500 hardware, but is
  850. * architecturally possible -- e.g. in some weird nested
  851. * virtualization case.
  852. */
  853. if (host_tlb_params[0].entries == 0 ||
  854. host_tlb_params[1].entries == 0) {
  855. pr_err("%s: need to know host tlb size\n", __func__);
  856. return -ENODEV;
  857. }
  858. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  859. TLBnCFG_ASSOC_SHIFT;
  860. host_tlb_params[1].ways = host_tlb_params[1].entries;
  861. if (!is_power_of_2(host_tlb_params[0].entries) ||
  862. !is_power_of_2(host_tlb_params[0].ways) ||
  863. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  864. host_tlb_params[0].ways == 0) {
  865. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  866. __func__, host_tlb_params[0].entries,
  867. host_tlb_params[0].ways);
  868. return -ENODEV;
  869. }
  870. host_tlb_params[0].sets =
  871. host_tlb_params[0].entries / host_tlb_params[0].ways;
  872. host_tlb_params[1].sets = 1;
  873. vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
  874. vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
  875. vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
  876. vcpu_e500->gtlb_params[0].sets =
  877. KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
  878. vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
  879. vcpu_e500->gtlb_params[1].sets = 1;
  880. vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
  881. if (!vcpu_e500->gtlb_arch)
  882. return -ENOMEM;
  883. vcpu_e500->gtlb_offset[0] = 0;
  884. vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
  885. vcpu_e500->tlb_refs[0] =
  886. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
  887. GFP_KERNEL);
  888. if (!vcpu_e500->tlb_refs[0])
  889. goto err;
  890. vcpu_e500->tlb_refs[1] =
  891. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
  892. GFP_KERNEL);
  893. if (!vcpu_e500->tlb_refs[1])
  894. goto err;
  895. vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
  896. vcpu_e500->gtlb_params[0].entries,
  897. GFP_KERNEL);
  898. if (!vcpu_e500->gtlb_priv[0])
  899. goto err;
  900. vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
  901. vcpu_e500->gtlb_params[1].entries,
  902. GFP_KERNEL);
  903. if (!vcpu_e500->gtlb_priv[1])
  904. goto err;
  905. /* Init TLB configuration register */
  906. vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
  907. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  908. vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
  909. vcpu->arch.tlbcfg[0] |=
  910. vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
  911. vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
  912. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  913. vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[1].entries;
  914. vcpu->arch.tlbcfg[0] |=
  915. vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
  916. return 0;
  917. err:
  918. free_gtlb(vcpu_e500);
  919. kfree(vcpu_e500->tlb_refs[0]);
  920. kfree(vcpu_e500->tlb_refs[1]);
  921. return -1;
  922. }
  923. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  924. {
  925. free_gtlb(vcpu_e500);
  926. kfree(vcpu_e500->tlb_refs[0]);
  927. kfree(vcpu_e500->tlb_refs[1]);
  928. }