e500_tlb.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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 = 0; /* silence GCC warning */
  351. unsigned long hva;
  352. int pfnmap = 0;
  353. int tsize = BOOK3E_PAGESZ_4K;
  354. /*
  355. * Translate guest physical to true physical, acquiring
  356. * a page reference if it is normal, non-reserved memory.
  357. *
  358. * gfn_to_memslot() must succeed because otherwise we wouldn't
  359. * have gotten this far. Eventually we should just pass the slot
  360. * pointer through from the first lookup.
  361. */
  362. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  363. hva = gfn_to_hva_memslot(slot, gfn);
  364. if (tlbsel == 1) {
  365. struct vm_area_struct *vma;
  366. down_read(&current->mm->mmap_sem);
  367. vma = find_vma(current->mm, hva);
  368. if (vma && hva >= vma->vm_start &&
  369. (vma->vm_flags & VM_PFNMAP)) {
  370. /*
  371. * This VMA is a physically contiguous region (e.g.
  372. * /dev/mem) that bypasses normal Linux page
  373. * management. Find the overlap between the
  374. * vma and the memslot.
  375. */
  376. unsigned long start, end;
  377. unsigned long slot_start, slot_end;
  378. pfnmap = 1;
  379. start = vma->vm_pgoff;
  380. end = start +
  381. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  382. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  383. slot_start = pfn - (gfn - slot->base_gfn);
  384. slot_end = slot_start + slot->npages;
  385. if (start < slot_start)
  386. start = slot_start;
  387. if (end > slot_end)
  388. end = slot_end;
  389. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  390. MAS1_TSIZE_SHIFT;
  391. /*
  392. * e500 doesn't implement the lowest tsize bit,
  393. * or 1K pages.
  394. */
  395. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  396. /*
  397. * Now find the largest tsize (up to what the guest
  398. * requested) that will cover gfn, stay within the
  399. * range, and for which gfn and pfn are mutually
  400. * aligned.
  401. */
  402. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  403. unsigned long gfn_start, gfn_end, tsize_pages;
  404. tsize_pages = 1 << (tsize - 2);
  405. gfn_start = gfn & ~(tsize_pages - 1);
  406. gfn_end = gfn_start + tsize_pages;
  407. if (gfn_start + pfn - gfn < start)
  408. continue;
  409. if (gfn_end + pfn - gfn > end)
  410. continue;
  411. if ((gfn & (tsize_pages - 1)) !=
  412. (pfn & (tsize_pages - 1)))
  413. continue;
  414. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  415. pfn &= ~(tsize_pages - 1);
  416. break;
  417. }
  418. } else if (vma && hva >= vma->vm_start &&
  419. (vma->vm_flags & VM_HUGETLB)) {
  420. unsigned long psize = vma_kernel_pagesize(vma);
  421. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  422. MAS1_TSIZE_SHIFT;
  423. /*
  424. * Take the largest page size that satisfies both host
  425. * and guest mapping
  426. */
  427. tsize = min(__ilog2(psize) - 10, tsize);
  428. /*
  429. * e500 doesn't implement the lowest tsize bit,
  430. * or 1K pages.
  431. */
  432. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  433. }
  434. up_read(&current->mm->mmap_sem);
  435. }
  436. if (likely(!pfnmap)) {
  437. unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
  438. pfn = gfn_to_pfn_memslot(slot, gfn);
  439. if (is_error_noslot_pfn(pfn)) {
  440. printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
  441. (long)gfn);
  442. return;
  443. }
  444. /* Align guest and physical address to page map boundaries */
  445. pfn &= ~(tsize_pages - 1);
  446. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  447. }
  448. /* Drop old ref and setup new one. */
  449. kvmppc_e500_ref_release(ref);
  450. kvmppc_e500_ref_setup(ref, gtlbe, pfn);
  451. kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
  452. ref, gvaddr, stlbe);
  453. /* Clear i-cache for new pages */
  454. kvmppc_mmu_flush_icache(pfn);
  455. /* Drop refcount on page, so that mmu notifiers can clear it */
  456. kvm_release_pfn_clean(pfn);
  457. }
  458. /* XXX only map the one-one case, for now use TLB0 */
  459. static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  460. int esel,
  461. struct kvm_book3e_206_tlb_entry *stlbe)
  462. {
  463. struct kvm_book3e_206_tlb_entry *gtlbe;
  464. struct tlbe_ref *ref;
  465. gtlbe = get_entry(vcpu_e500, 0, esel);
  466. ref = &vcpu_e500->gtlb_priv[0][esel].ref;
  467. kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  468. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  469. gtlbe, 0, stlbe, ref);
  470. }
  471. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  472. * the shadow TLB. */
  473. /* XXX for both one-one and one-to-many , for now use TLB1 */
  474. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  475. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  476. struct kvm_book3e_206_tlb_entry *stlbe, int esel)
  477. {
  478. struct tlbe_ref *ref;
  479. unsigned int victim;
  480. victim = vcpu_e500->host_tlb1_nv++;
  481. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  482. vcpu_e500->host_tlb1_nv = 0;
  483. ref = &vcpu_e500->tlb_refs[1][victim];
  484. kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
  485. vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim;
  486. vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
  487. if (vcpu_e500->h2g_tlb1_rmap[victim]) {
  488. unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim];
  489. vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim);
  490. }
  491. vcpu_e500->h2g_tlb1_rmap[victim] = esel;
  492. return victim;
  493. }
  494. static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500)
  495. {
  496. int size = vcpu_e500->gtlb_params[1].entries;
  497. unsigned int offset;
  498. gva_t eaddr;
  499. int i;
  500. vcpu_e500->tlb1_min_eaddr = ~0UL;
  501. vcpu_e500->tlb1_max_eaddr = 0;
  502. offset = vcpu_e500->gtlb_offset[1];
  503. for (i = 0; i < size; i++) {
  504. struct kvm_book3e_206_tlb_entry *tlbe =
  505. &vcpu_e500->gtlb_arch[offset + i];
  506. if (!get_tlb_v(tlbe))
  507. continue;
  508. eaddr = get_tlb_eaddr(tlbe);
  509. vcpu_e500->tlb1_min_eaddr =
  510. min(vcpu_e500->tlb1_min_eaddr, eaddr);
  511. eaddr = get_tlb_end(tlbe);
  512. vcpu_e500->tlb1_max_eaddr =
  513. max(vcpu_e500->tlb1_max_eaddr, eaddr);
  514. }
  515. }
  516. static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500,
  517. struct kvm_book3e_206_tlb_entry *gtlbe)
  518. {
  519. unsigned long start, end, size;
  520. size = get_tlb_bytes(gtlbe);
  521. start = get_tlb_eaddr(gtlbe) & ~(size - 1);
  522. end = start + size - 1;
  523. return vcpu_e500->tlb1_min_eaddr == start ||
  524. vcpu_e500->tlb1_max_eaddr == end;
  525. }
  526. /* This function is supposed to be called for a adding a new valid tlb entry */
  527. static void kvmppc_set_tlb1map_range(struct kvm_vcpu *vcpu,
  528. struct kvm_book3e_206_tlb_entry *gtlbe)
  529. {
  530. unsigned long start, end, size;
  531. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  532. if (!get_tlb_v(gtlbe))
  533. return;
  534. size = get_tlb_bytes(gtlbe);
  535. start = get_tlb_eaddr(gtlbe) & ~(size - 1);
  536. end = start + size - 1;
  537. vcpu_e500->tlb1_min_eaddr = min(vcpu_e500->tlb1_min_eaddr, start);
  538. vcpu_e500->tlb1_max_eaddr = max(vcpu_e500->tlb1_max_eaddr, end);
  539. }
  540. static inline int kvmppc_e500_gtlbe_invalidate(
  541. struct kvmppc_vcpu_e500 *vcpu_e500,
  542. int tlbsel, int esel)
  543. {
  544. struct kvm_book3e_206_tlb_entry *gtlbe =
  545. get_entry(vcpu_e500, tlbsel, esel);
  546. if (unlikely(get_tlb_iprot(gtlbe)))
  547. return -1;
  548. if (tlbsel == 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
  549. kvmppc_recalc_tlb1map_range(vcpu_e500);
  550. gtlbe->mas1 = 0;
  551. return 0;
  552. }
  553. int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  554. {
  555. int esel;
  556. if (value & MMUCSR0_TLB0FI)
  557. for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
  558. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  559. if (value & MMUCSR0_TLB1FI)
  560. for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
  561. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  562. /* Invalidate all vcpu id mappings */
  563. kvmppc_e500_tlbil_all(vcpu_e500);
  564. return EMULATE_DONE;
  565. }
  566. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, gva_t ea)
  567. {
  568. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  569. unsigned int ia;
  570. int esel, tlbsel;
  571. ia = (ea >> 2) & 0x1;
  572. /* since we only have two TLBs, only lower bit is used. */
  573. tlbsel = (ea >> 3) & 0x1;
  574. if (ia) {
  575. /* invalidate all entries */
  576. for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
  577. esel++)
  578. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  579. } else {
  580. ea &= 0xfffff000;
  581. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  582. get_cur_pid(vcpu), -1);
  583. if (esel >= 0)
  584. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  585. }
  586. /* Invalidate all vcpu id mappings */
  587. kvmppc_e500_tlbil_all(vcpu_e500);
  588. return EMULATE_DONE;
  589. }
  590. static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
  591. int pid, int type)
  592. {
  593. struct kvm_book3e_206_tlb_entry *tlbe;
  594. int tid, esel;
  595. /* invalidate all entries */
  596. for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) {
  597. tlbe = get_entry(vcpu_e500, tlbsel, esel);
  598. tid = get_tlb_tid(tlbe);
  599. if (type == 0 || tid == pid) {
  600. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  601. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  602. }
  603. }
  604. }
  605. static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid,
  606. gva_t ea)
  607. {
  608. int tlbsel, esel;
  609. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  610. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
  611. if (esel >= 0) {
  612. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  613. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  614. break;
  615. }
  616. }
  617. }
  618. int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int type, gva_t ea)
  619. {
  620. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  621. int pid = get_cur_spid(vcpu);
  622. if (type == 0 || type == 1) {
  623. tlbilx_all(vcpu_e500, 0, pid, type);
  624. tlbilx_all(vcpu_e500, 1, pid, type);
  625. } else if (type == 3) {
  626. tlbilx_one(vcpu_e500, pid, ea);
  627. }
  628. return EMULATE_DONE;
  629. }
  630. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  631. {
  632. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  633. int tlbsel, esel;
  634. struct kvm_book3e_206_tlb_entry *gtlbe;
  635. tlbsel = get_tlb_tlbsel(vcpu);
  636. esel = get_tlb_esel(vcpu, tlbsel);
  637. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  638. vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
  639. vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  640. vcpu->arch.shared->mas1 = gtlbe->mas1;
  641. vcpu->arch.shared->mas2 = gtlbe->mas2;
  642. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  643. return EMULATE_DONE;
  644. }
  645. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, gva_t ea)
  646. {
  647. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  648. int as = !!get_cur_sas(vcpu);
  649. unsigned int pid = get_cur_spid(vcpu);
  650. int esel, tlbsel;
  651. struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
  652. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  653. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  654. if (esel >= 0) {
  655. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  656. break;
  657. }
  658. }
  659. if (gtlbe) {
  660. esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
  661. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  662. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  663. vcpu->arch.shared->mas1 = gtlbe->mas1;
  664. vcpu->arch.shared->mas2 = gtlbe->mas2;
  665. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  666. } else {
  667. int victim;
  668. /* since we only have two TLBs, only lower bit is used. */
  669. tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
  670. victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
  671. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
  672. | MAS0_ESEL(victim)
  673. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  674. vcpu->arch.shared->mas1 =
  675. (vcpu->arch.shared->mas6 & MAS6_SPID0)
  676. | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  677. | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
  678. vcpu->arch.shared->mas2 &= MAS2_EPN;
  679. vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
  680. MAS2_ATTRIB_MASK;
  681. vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
  682. MAS3_U2 | MAS3_U3;
  683. }
  684. kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
  685. return EMULATE_DONE;
  686. }
  687. /* sesel is for tlb1 only */
  688. static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  689. struct kvm_book3e_206_tlb_entry *gtlbe,
  690. struct kvm_book3e_206_tlb_entry *stlbe,
  691. int stlbsel, int sesel)
  692. {
  693. int stid;
  694. preempt_disable();
  695. stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
  696. stlbe->mas1 |= MAS1_TID(stid);
  697. write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
  698. preempt_enable();
  699. }
  700. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  701. {
  702. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  703. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  704. int tlbsel, esel, stlbsel, sesel;
  705. int recal = 0;
  706. tlbsel = get_tlb_tlbsel(vcpu);
  707. esel = get_tlb_esel(vcpu, tlbsel);
  708. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  709. if (get_tlb_v(gtlbe)) {
  710. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  711. if ((tlbsel == 1) &&
  712. kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
  713. recal = 1;
  714. }
  715. gtlbe->mas1 = vcpu->arch.shared->mas1;
  716. gtlbe->mas2 = vcpu->arch.shared->mas2;
  717. if (!(vcpu->arch.shared->msr & MSR_CM))
  718. gtlbe->mas2 &= 0xffffffffUL;
  719. gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
  720. trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
  721. gtlbe->mas2, gtlbe->mas7_3);
  722. if (tlbsel == 1) {
  723. /*
  724. * If a valid tlb1 entry is overwritten then recalculate the
  725. * min/max TLB1 map address range otherwise no need to look
  726. * in tlb1 array.
  727. */
  728. if (recal)
  729. kvmppc_recalc_tlb1map_range(vcpu_e500);
  730. else
  731. kvmppc_set_tlb1map_range(vcpu, gtlbe);
  732. }
  733. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  734. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  735. u64 eaddr;
  736. u64 raddr;
  737. switch (tlbsel) {
  738. case 0:
  739. /* TLB0 */
  740. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  741. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  742. stlbsel = 0;
  743. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  744. sesel = 0; /* unused */
  745. break;
  746. case 1:
  747. /* TLB1 */
  748. eaddr = get_tlb_eaddr(gtlbe);
  749. raddr = get_tlb_raddr(gtlbe);
  750. /* Create a 4KB mapping on the host.
  751. * If the guest wanted a large page,
  752. * only the first 4KB is mapped here and the rest
  753. * are mapped on the fly. */
  754. stlbsel = 1;
  755. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  756. raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
  757. break;
  758. default:
  759. BUG();
  760. }
  761. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  762. }
  763. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  764. return EMULATE_DONE;
  765. }
  766. static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  767. gva_t eaddr, unsigned int pid, int as)
  768. {
  769. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  770. int esel, tlbsel;
  771. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  772. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  773. if (esel >= 0)
  774. return index_of(tlbsel, esel);
  775. }
  776. return -1;
  777. }
  778. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  779. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  780. struct kvm_translation *tr)
  781. {
  782. int index;
  783. gva_t eaddr;
  784. u8 pid;
  785. u8 as;
  786. eaddr = tr->linear_address;
  787. pid = (tr->linear_address >> 32) & 0xff;
  788. as = (tr->linear_address >> 40) & 0x1;
  789. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  790. if (index < 0) {
  791. tr->valid = 0;
  792. return 0;
  793. }
  794. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  795. /* XXX what does "writeable" and "usermode" even mean? */
  796. tr->valid = 1;
  797. return 0;
  798. }
  799. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  800. {
  801. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  802. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  803. }
  804. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  805. {
  806. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  807. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  808. }
  809. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  810. {
  811. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  812. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  813. }
  814. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  815. {
  816. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  817. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  818. }
  819. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  820. gva_t eaddr)
  821. {
  822. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  823. struct kvm_book3e_206_tlb_entry *gtlbe;
  824. u64 pgmask;
  825. gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
  826. pgmask = get_tlb_bytes(gtlbe) - 1;
  827. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  828. }
  829. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  830. {
  831. }
  832. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  833. unsigned int index)
  834. {
  835. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  836. struct tlbe_priv *priv;
  837. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  838. int tlbsel = tlbsel_of(index);
  839. int esel = esel_of(index);
  840. int stlbsel, sesel;
  841. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  842. switch (tlbsel) {
  843. case 0:
  844. stlbsel = 0;
  845. sesel = 0; /* unused */
  846. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  847. /* Only triggers after clear_tlb_refs */
  848. if (unlikely(!(priv->ref.flags & E500_TLB_VALID)))
  849. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  850. else
  851. kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
  852. &priv->ref, eaddr, &stlbe);
  853. break;
  854. case 1: {
  855. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  856. stlbsel = 1;
  857. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
  858. gtlbe, &stlbe, esel);
  859. break;
  860. }
  861. default:
  862. BUG();
  863. break;
  864. }
  865. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  866. }
  867. /************* MMU Notifiers *************/
  868. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  869. {
  870. trace_kvm_unmap_hva(hva);
  871. /*
  872. * Flush all shadow tlb entries everywhere. This is slow, but
  873. * we are 100% sure that we catch the to be unmapped page
  874. */
  875. kvm_flush_remote_tlbs(kvm);
  876. return 0;
  877. }
  878. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  879. {
  880. /* kvm_unmap_hva flushes everything anyways */
  881. kvm_unmap_hva(kvm, start);
  882. return 0;
  883. }
  884. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  885. {
  886. /* XXX could be more clever ;) */
  887. return 0;
  888. }
  889. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  890. {
  891. /* XXX could be more clever ;) */
  892. return 0;
  893. }
  894. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  895. {
  896. /* The page will get remapped properly on its next fault */
  897. kvm_unmap_hva(kvm, hva);
  898. }
  899. /*****************************************/
  900. static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
  901. {
  902. int i;
  903. clear_tlb1_bitmap(vcpu_e500);
  904. kfree(vcpu_e500->g2h_tlb1_map);
  905. clear_tlb_refs(vcpu_e500);
  906. kfree(vcpu_e500->gtlb_priv[0]);
  907. kfree(vcpu_e500->gtlb_priv[1]);
  908. if (vcpu_e500->shared_tlb_pages) {
  909. vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
  910. PAGE_SIZE)));
  911. for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
  912. set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
  913. put_page(vcpu_e500->shared_tlb_pages[i]);
  914. }
  915. vcpu_e500->num_shared_tlb_pages = 0;
  916. kfree(vcpu_e500->shared_tlb_pages);
  917. vcpu_e500->shared_tlb_pages = NULL;
  918. } else {
  919. kfree(vcpu_e500->gtlb_arch);
  920. }
  921. vcpu_e500->gtlb_arch = NULL;
  922. }
  923. void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  924. {
  925. sregs->u.e.mas0 = vcpu->arch.shared->mas0;
  926. sregs->u.e.mas1 = vcpu->arch.shared->mas1;
  927. sregs->u.e.mas2 = vcpu->arch.shared->mas2;
  928. sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
  929. sregs->u.e.mas4 = vcpu->arch.shared->mas4;
  930. sregs->u.e.mas6 = vcpu->arch.shared->mas6;
  931. sregs->u.e.mmucfg = vcpu->arch.mmucfg;
  932. sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
  933. sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
  934. sregs->u.e.tlbcfg[2] = 0;
  935. sregs->u.e.tlbcfg[3] = 0;
  936. }
  937. int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  938. {
  939. if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
  940. vcpu->arch.shared->mas0 = sregs->u.e.mas0;
  941. vcpu->arch.shared->mas1 = sregs->u.e.mas1;
  942. vcpu->arch.shared->mas2 = sregs->u.e.mas2;
  943. vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
  944. vcpu->arch.shared->mas4 = sregs->u.e.mas4;
  945. vcpu->arch.shared->mas6 = sregs->u.e.mas6;
  946. }
  947. return 0;
  948. }
  949. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  950. struct kvm_config_tlb *cfg)
  951. {
  952. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  953. struct kvm_book3e_206_tlb_params params;
  954. char *virt;
  955. struct page **pages;
  956. struct tlbe_priv *privs[2] = {};
  957. u64 *g2h_bitmap = NULL;
  958. size_t array_len;
  959. u32 sets;
  960. int num_pages, ret, i;
  961. if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
  962. return -EINVAL;
  963. if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
  964. sizeof(params)))
  965. return -EFAULT;
  966. if (params.tlb_sizes[1] > 64)
  967. return -EINVAL;
  968. if (params.tlb_ways[1] != params.tlb_sizes[1])
  969. return -EINVAL;
  970. if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
  971. return -EINVAL;
  972. if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
  973. return -EINVAL;
  974. if (!is_power_of_2(params.tlb_ways[0]))
  975. return -EINVAL;
  976. sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
  977. if (!is_power_of_2(sets))
  978. return -EINVAL;
  979. array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
  980. array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
  981. if (cfg->array_len < array_len)
  982. return -EINVAL;
  983. num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
  984. cfg->array / PAGE_SIZE;
  985. pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  986. if (!pages)
  987. return -ENOMEM;
  988. ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
  989. if (ret < 0)
  990. goto err_pages;
  991. if (ret != num_pages) {
  992. num_pages = ret;
  993. ret = -EFAULT;
  994. goto err_put_page;
  995. }
  996. virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
  997. if (!virt) {
  998. ret = -ENOMEM;
  999. goto err_put_page;
  1000. }
  1001. privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
  1002. GFP_KERNEL);
  1003. privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
  1004. GFP_KERNEL);
  1005. if (!privs[0] || !privs[1]) {
  1006. ret = -ENOMEM;
  1007. goto err_privs;
  1008. }
  1009. g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
  1010. GFP_KERNEL);
  1011. if (!g2h_bitmap) {
  1012. ret = -ENOMEM;
  1013. goto err_privs;
  1014. }
  1015. free_gtlb(vcpu_e500);
  1016. vcpu_e500->gtlb_priv[0] = privs[0];
  1017. vcpu_e500->gtlb_priv[1] = privs[1];
  1018. vcpu_e500->g2h_tlb1_map = g2h_bitmap;
  1019. vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
  1020. (virt + (cfg->array & (PAGE_SIZE - 1)));
  1021. vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
  1022. vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
  1023. vcpu_e500->gtlb_offset[0] = 0;
  1024. vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
  1025. vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
  1026. vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1027. if (params.tlb_sizes[0] <= 2048)
  1028. vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
  1029. vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
  1030. vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1031. vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
  1032. vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
  1033. vcpu_e500->shared_tlb_pages = pages;
  1034. vcpu_e500->num_shared_tlb_pages = num_pages;
  1035. vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
  1036. vcpu_e500->gtlb_params[0].sets = sets;
  1037. vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
  1038. vcpu_e500->gtlb_params[1].sets = 1;
  1039. kvmppc_recalc_tlb1map_range(vcpu_e500);
  1040. return 0;
  1041. err_privs:
  1042. kfree(privs[0]);
  1043. kfree(privs[1]);
  1044. err_put_page:
  1045. for (i = 0; i < num_pages; i++)
  1046. put_page(pages[i]);
  1047. err_pages:
  1048. kfree(pages);
  1049. return ret;
  1050. }
  1051. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  1052. struct kvm_dirty_tlb *dirty)
  1053. {
  1054. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  1055. kvmppc_recalc_tlb1map_range(vcpu_e500);
  1056. clear_tlb_refs(vcpu_e500);
  1057. return 0;
  1058. }
  1059. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  1060. {
  1061. struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
  1062. int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
  1063. int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
  1064. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  1065. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  1066. /*
  1067. * This should never happen on real e500 hardware, but is
  1068. * architecturally possible -- e.g. in some weird nested
  1069. * virtualization case.
  1070. */
  1071. if (host_tlb_params[0].entries == 0 ||
  1072. host_tlb_params[1].entries == 0) {
  1073. pr_err("%s: need to know host tlb size\n", __func__);
  1074. return -ENODEV;
  1075. }
  1076. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  1077. TLBnCFG_ASSOC_SHIFT;
  1078. host_tlb_params[1].ways = host_tlb_params[1].entries;
  1079. if (!is_power_of_2(host_tlb_params[0].entries) ||
  1080. !is_power_of_2(host_tlb_params[0].ways) ||
  1081. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  1082. host_tlb_params[0].ways == 0) {
  1083. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  1084. __func__, host_tlb_params[0].entries,
  1085. host_tlb_params[0].ways);
  1086. return -ENODEV;
  1087. }
  1088. host_tlb_params[0].sets =
  1089. host_tlb_params[0].entries / host_tlb_params[0].ways;
  1090. host_tlb_params[1].sets = 1;
  1091. vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
  1092. vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
  1093. vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
  1094. vcpu_e500->gtlb_params[0].sets =
  1095. KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
  1096. vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
  1097. vcpu_e500->gtlb_params[1].sets = 1;
  1098. vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
  1099. if (!vcpu_e500->gtlb_arch)
  1100. return -ENOMEM;
  1101. vcpu_e500->gtlb_offset[0] = 0;
  1102. vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
  1103. vcpu_e500->tlb_refs[0] =
  1104. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
  1105. GFP_KERNEL);
  1106. if (!vcpu_e500->tlb_refs[0])
  1107. goto err;
  1108. vcpu_e500->tlb_refs[1] =
  1109. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
  1110. GFP_KERNEL);
  1111. if (!vcpu_e500->tlb_refs[1])
  1112. goto err;
  1113. vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
  1114. vcpu_e500->gtlb_params[0].entries,
  1115. GFP_KERNEL);
  1116. if (!vcpu_e500->gtlb_priv[0])
  1117. goto err;
  1118. vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
  1119. vcpu_e500->gtlb_params[1].entries,
  1120. GFP_KERNEL);
  1121. if (!vcpu_e500->gtlb_priv[1])
  1122. goto err;
  1123. vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(u64) *
  1124. vcpu_e500->gtlb_params[1].entries,
  1125. GFP_KERNEL);
  1126. if (!vcpu_e500->g2h_tlb1_map)
  1127. goto err;
  1128. vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
  1129. host_tlb_params[1].entries,
  1130. GFP_KERNEL);
  1131. if (!vcpu_e500->h2g_tlb1_rmap)
  1132. goto err;
  1133. /* Init TLB configuration register */
  1134. vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
  1135. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1136. vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
  1137. vcpu->arch.tlbcfg[0] |=
  1138. vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
  1139. vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
  1140. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1141. vcpu->arch.tlbcfg[1] |= vcpu_e500->gtlb_params[1].entries;
  1142. vcpu->arch.tlbcfg[1] |=
  1143. vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
  1144. kvmppc_recalc_tlb1map_range(vcpu_e500);
  1145. return 0;
  1146. err:
  1147. free_gtlb(vcpu_e500);
  1148. kfree(vcpu_e500->tlb_refs[0]);
  1149. kfree(vcpu_e500->tlb_refs[1]);
  1150. return -1;
  1151. }
  1152. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  1153. {
  1154. free_gtlb(vcpu_e500);
  1155. kfree(vcpu_e500->h2g_tlb1_rmap);
  1156. kfree(vcpu_e500->tlb_refs[0]);
  1157. kfree(vcpu_e500->tlb_refs[1]);
  1158. }