e500_tlb.c 36 KB

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