e500_tlb.c 37 KB

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