e500_tlb.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
  255. stlbe->mas2, stlbe->mas7_3);
  256. }
  257. /*
  258. * Acquire a mas0 with victim hint, as if we just took a TLB miss.
  259. *
  260. * We don't care about the address we're searching for, other than that it's
  261. * in the right set and is not present in the TLB. Using a zero PID and a
  262. * userspace address means we don't have to set and then restore MAS5, or
  263. * calculate a proper MAS6 value.
  264. */
  265. static u32 get_host_mas0(unsigned long eaddr)
  266. {
  267. unsigned long flags;
  268. u32 mas0;
  269. local_irq_save(flags);
  270. mtspr(SPRN_MAS6, 0);
  271. asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
  272. mas0 = mfspr(SPRN_MAS0);
  273. local_irq_restore(flags);
  274. return mas0;
  275. }
  276. /* sesel is for tlb1 only */
  277. static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  278. int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
  279. {
  280. u32 mas0;
  281. if (tlbsel == 0) {
  282. mas0 = get_host_mas0(stlbe->mas2);
  283. __write_host_tlbe(stlbe, mas0);
  284. } else {
  285. __write_host_tlbe(stlbe,
  286. MAS0_TLBSEL(1) |
  287. MAS0_ESEL(to_htlb1_esel(sesel)));
  288. }
  289. }
  290. void kvmppc_map_magic(struct kvm_vcpu *vcpu)
  291. {
  292. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  293. struct kvm_book3e_206_tlb_entry magic;
  294. ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
  295. unsigned int stid;
  296. pfn_t pfn;
  297. pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
  298. get_page(pfn_to_page(pfn));
  299. preempt_disable();
  300. stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
  301. magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
  302. MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  303. magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
  304. magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
  305. MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
  306. magic.mas8 = 0;
  307. __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
  308. preempt_enable();
  309. }
  310. void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
  311. {
  312. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  313. /* Shadow PID may be expired on local core */
  314. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  315. }
  316. void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
  317. {
  318. }
  319. static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
  320. int tlbsel, int esel)
  321. {
  322. struct kvm_book3e_206_tlb_entry *gtlbe =
  323. get_entry(vcpu_e500, tlbsel, esel);
  324. struct vcpu_id_table *idt = vcpu_e500->idt;
  325. unsigned int pr, tid, ts, pid;
  326. u32 val, eaddr;
  327. unsigned long flags;
  328. ts = get_tlb_ts(gtlbe);
  329. tid = get_tlb_tid(gtlbe);
  330. preempt_disable();
  331. /* One guest ID may be mapped to two shadow IDs */
  332. for (pr = 0; pr < 2; pr++) {
  333. /*
  334. * The shadow PID can have a valid mapping on at most one
  335. * host CPU. In the common case, it will be valid on this
  336. * CPU, in which case (for TLB0) we do a local invalidation
  337. * of the specific address.
  338. *
  339. * If the shadow PID is not valid on the current host CPU, or
  340. * if we're invalidating a TLB1 entry, we invalidate the
  341. * entire shadow PID.
  342. */
  343. if (tlbsel == 1 ||
  344. (pid = local_sid_lookup(&idt->id[ts][tid][pr])) <= 0) {
  345. kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr);
  346. continue;
  347. }
  348. /*
  349. * The guest is invalidating a TLB0 entry which is in a PID
  350. * that has a valid shadow mapping on this host CPU. We
  351. * search host TLB0 to invalidate it's shadow TLB entry,
  352. * similar to __tlbil_va except that we need to look in AS1.
  353. */
  354. val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS;
  355. eaddr = get_tlb_eaddr(gtlbe);
  356. local_irq_save(flags);
  357. mtspr(SPRN_MAS6, val);
  358. asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr));
  359. val = mfspr(SPRN_MAS1);
  360. if (val & MAS1_VALID) {
  361. mtspr(SPRN_MAS1, val & ~MAS1_VALID);
  362. asm volatile("tlbwe");
  363. }
  364. local_irq_restore(flags);
  365. }
  366. preempt_enable();
  367. }
  368. static int tlb0_set_base(gva_t addr, int sets, int ways)
  369. {
  370. int set_base;
  371. set_base = (addr >> PAGE_SHIFT) & (sets - 1);
  372. set_base *= ways;
  373. return set_base;
  374. }
  375. static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
  376. {
  377. return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
  378. vcpu_e500->gtlb_params[0].ways);
  379. }
  380. static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
  381. {
  382. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  383. int esel = get_tlb_esel_bit(vcpu);
  384. if (tlbsel == 0) {
  385. esel &= vcpu_e500->gtlb_params[0].ways - 1;
  386. esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
  387. } else {
  388. esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
  389. }
  390. return esel;
  391. }
  392. /* Search the guest TLB for a matching entry. */
  393. static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
  394. gva_t eaddr, int tlbsel, unsigned int pid, int as)
  395. {
  396. int size = vcpu_e500->gtlb_params[tlbsel].entries;
  397. unsigned int set_base, offset;
  398. int i;
  399. if (tlbsel == 0) {
  400. set_base = gtlb0_set_base(vcpu_e500, eaddr);
  401. size = vcpu_e500->gtlb_params[0].ways;
  402. } else {
  403. set_base = 0;
  404. }
  405. offset = vcpu_e500->gtlb_offset[tlbsel];
  406. for (i = 0; i < size; i++) {
  407. struct kvm_book3e_206_tlb_entry *tlbe =
  408. &vcpu_e500->gtlb_arch[offset + set_base + i];
  409. unsigned int tid;
  410. if (eaddr < get_tlb_eaddr(tlbe))
  411. continue;
  412. if (eaddr > get_tlb_end(tlbe))
  413. continue;
  414. tid = get_tlb_tid(tlbe);
  415. if (tid && (tid != pid))
  416. continue;
  417. if (!get_tlb_v(tlbe))
  418. continue;
  419. if (get_tlb_ts(tlbe) != as && as != -1)
  420. continue;
  421. return set_base + i;
  422. }
  423. return -1;
  424. }
  425. static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
  426. struct kvm_book3e_206_tlb_entry *gtlbe,
  427. pfn_t pfn)
  428. {
  429. ref->pfn = pfn;
  430. ref->flags = E500_TLB_VALID;
  431. if (tlbe_is_writable(gtlbe))
  432. ref->flags |= E500_TLB_DIRTY;
  433. }
  434. static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
  435. {
  436. if (ref->flags & E500_TLB_VALID) {
  437. if (ref->flags & E500_TLB_DIRTY)
  438. kvm_release_pfn_dirty(ref->pfn);
  439. else
  440. kvm_release_pfn_clean(ref->pfn);
  441. ref->flags = 0;
  442. }
  443. }
  444. static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
  445. {
  446. int tlbsel = 0;
  447. int i;
  448. for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
  449. struct tlbe_ref *ref =
  450. &vcpu_e500->gtlb_priv[tlbsel][i].ref;
  451. kvmppc_e500_ref_release(ref);
  452. }
  453. }
  454. static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
  455. {
  456. int stlbsel = 1;
  457. int i;
  458. kvmppc_e500_id_table_reset_all(vcpu_e500);
  459. for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
  460. struct tlbe_ref *ref =
  461. &vcpu_e500->tlb_refs[stlbsel][i];
  462. kvmppc_e500_ref_release(ref);
  463. }
  464. clear_tlb_privs(vcpu_e500);
  465. }
  466. static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
  467. unsigned int eaddr, int as)
  468. {
  469. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  470. unsigned int victim, pidsel, tsized;
  471. int tlbsel;
  472. /* since we only have two TLBs, only lower bit is used. */
  473. tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
  474. victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
  475. pidsel = (vcpu->arch.shared->mas4 >> 16) & 0xf;
  476. tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
  477. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  478. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  479. vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
  480. | MAS1_TID(vcpu_e500->pid[pidsel])
  481. | MAS1_TSIZE(tsized);
  482. vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
  483. | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
  484. vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  485. vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
  486. | (get_cur_pid(vcpu) << 16)
  487. | (as ? MAS6_SAS : 0);
  488. }
  489. /* TID must be supplied by the caller */
  490. static inline void kvmppc_e500_setup_stlbe(
  491. struct kvmppc_vcpu_e500 *vcpu_e500,
  492. struct kvm_book3e_206_tlb_entry *gtlbe,
  493. int tsize, struct tlbe_ref *ref, u64 gvaddr,
  494. struct kvm_book3e_206_tlb_entry *stlbe)
  495. {
  496. pfn_t pfn = ref->pfn;
  497. BUG_ON(!(ref->flags & E500_TLB_VALID));
  498. /* Force TS=1 IPROT=0 for all guest mappings. */
  499. stlbe->mas1 = MAS1_TSIZE(tsize) | MAS1_TS | MAS1_VALID;
  500. stlbe->mas2 = (gvaddr & MAS2_EPN)
  501. | e500_shadow_mas2_attrib(gtlbe->mas2,
  502. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  503. stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT)
  504. | e500_shadow_mas3_attrib(gtlbe->mas7_3,
  505. vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
  506. }
  507. static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  508. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  509. int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
  510. struct tlbe_ref *ref)
  511. {
  512. struct kvm_memory_slot *slot;
  513. unsigned long pfn, hva;
  514. int pfnmap = 0;
  515. int tsize = BOOK3E_PAGESZ_4K;
  516. /*
  517. * Translate guest physical to true physical, acquiring
  518. * a page reference if it is normal, non-reserved memory.
  519. *
  520. * gfn_to_memslot() must succeed because otherwise we wouldn't
  521. * have gotten this far. Eventually we should just pass the slot
  522. * pointer through from the first lookup.
  523. */
  524. slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
  525. hva = gfn_to_hva_memslot(slot, gfn);
  526. if (tlbsel == 1) {
  527. struct vm_area_struct *vma;
  528. down_read(&current->mm->mmap_sem);
  529. vma = find_vma(current->mm, hva);
  530. if (vma && hva >= vma->vm_start &&
  531. (vma->vm_flags & VM_PFNMAP)) {
  532. /*
  533. * This VMA is a physically contiguous region (e.g.
  534. * /dev/mem) that bypasses normal Linux page
  535. * management. Find the overlap between the
  536. * vma and the memslot.
  537. */
  538. unsigned long start, end;
  539. unsigned long slot_start, slot_end;
  540. pfnmap = 1;
  541. start = vma->vm_pgoff;
  542. end = start +
  543. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  544. pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
  545. slot_start = pfn - (gfn - slot->base_gfn);
  546. slot_end = slot_start + slot->npages;
  547. if (start < slot_start)
  548. start = slot_start;
  549. if (end > slot_end)
  550. end = slot_end;
  551. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  552. MAS1_TSIZE_SHIFT;
  553. /*
  554. * e500 doesn't implement the lowest tsize bit,
  555. * or 1K pages.
  556. */
  557. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  558. /*
  559. * Now find the largest tsize (up to what the guest
  560. * requested) that will cover gfn, stay within the
  561. * range, and for which gfn and pfn are mutually
  562. * aligned.
  563. */
  564. for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
  565. unsigned long gfn_start, gfn_end, tsize_pages;
  566. tsize_pages = 1 << (tsize - 2);
  567. gfn_start = gfn & ~(tsize_pages - 1);
  568. gfn_end = gfn_start + tsize_pages;
  569. if (gfn_start + pfn - gfn < start)
  570. continue;
  571. if (gfn_end + pfn - gfn > end)
  572. continue;
  573. if ((gfn & (tsize_pages - 1)) !=
  574. (pfn & (tsize_pages - 1)))
  575. continue;
  576. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  577. pfn &= ~(tsize_pages - 1);
  578. break;
  579. }
  580. } else if (vma && hva >= vma->vm_start &&
  581. (vma->vm_flags & VM_HUGETLB)) {
  582. unsigned long psize = vma_kernel_pagesize(vma);
  583. tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
  584. MAS1_TSIZE_SHIFT;
  585. /*
  586. * Take the largest page size that satisfies both host
  587. * and guest mapping
  588. */
  589. tsize = min(__ilog2(psize) - 10, tsize);
  590. /*
  591. * e500 doesn't implement the lowest tsize bit,
  592. * or 1K pages.
  593. */
  594. tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
  595. }
  596. up_read(&current->mm->mmap_sem);
  597. }
  598. if (likely(!pfnmap)) {
  599. unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
  600. pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
  601. if (is_error_pfn(pfn)) {
  602. printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
  603. (long)gfn);
  604. kvm_release_pfn_clean(pfn);
  605. return;
  606. }
  607. /* Align guest and physical address to page map boundaries */
  608. pfn &= ~(tsize_pages - 1);
  609. gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
  610. }
  611. /* Drop old ref and setup new one. */
  612. kvmppc_e500_ref_release(ref);
  613. kvmppc_e500_ref_setup(ref, gtlbe, pfn);
  614. kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, ref, gvaddr, stlbe);
  615. }
  616. /* XXX only map the one-one case, for now use TLB0 */
  617. static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  618. int esel,
  619. struct kvm_book3e_206_tlb_entry *stlbe)
  620. {
  621. struct kvm_book3e_206_tlb_entry *gtlbe;
  622. struct tlbe_ref *ref;
  623. gtlbe = get_entry(vcpu_e500, 0, esel);
  624. ref = &vcpu_e500->gtlb_priv[0][esel].ref;
  625. kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
  626. get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
  627. gtlbe, 0, stlbe, ref);
  628. }
  629. /* Caller must ensure that the specified guest TLB entry is safe to insert into
  630. * the shadow TLB. */
  631. /* XXX for both one-one and one-to-many , for now use TLB1 */
  632. static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
  633. u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
  634. struct kvm_book3e_206_tlb_entry *stlbe)
  635. {
  636. struct tlbe_ref *ref;
  637. unsigned int victim;
  638. victim = vcpu_e500->host_tlb1_nv++;
  639. if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
  640. vcpu_e500->host_tlb1_nv = 0;
  641. ref = &vcpu_e500->tlb_refs[1][victim];
  642. kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
  643. return victim;
  644. }
  645. void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
  646. {
  647. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  648. /* Recalc shadow pid since MSR changes */
  649. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  650. }
  651. static inline int kvmppc_e500_gtlbe_invalidate(
  652. struct kvmppc_vcpu_e500 *vcpu_e500,
  653. int tlbsel, int esel)
  654. {
  655. struct kvm_book3e_206_tlb_entry *gtlbe =
  656. get_entry(vcpu_e500, tlbsel, esel);
  657. if (unlikely(get_tlb_iprot(gtlbe)))
  658. return -1;
  659. gtlbe->mas1 = 0;
  660. return 0;
  661. }
  662. int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  663. {
  664. int esel;
  665. if (value & MMUCSR0_TLB0FI)
  666. for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
  667. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  668. if (value & MMUCSR0_TLB1FI)
  669. for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
  670. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  671. /* Invalidate all vcpu id mappings */
  672. kvmppc_e500_id_table_reset_all(vcpu_e500);
  673. return EMULATE_DONE;
  674. }
  675. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
  676. {
  677. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  678. unsigned int ia;
  679. int esel, tlbsel;
  680. gva_t ea;
  681. ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
  682. ia = (ea >> 2) & 0x1;
  683. /* since we only have two TLBs, only lower bit is used. */
  684. tlbsel = (ea >> 3) & 0x1;
  685. if (ia) {
  686. /* invalidate all entries */
  687. for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
  688. esel++)
  689. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  690. } else {
  691. ea &= 0xfffff000;
  692. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  693. get_cur_pid(vcpu), -1);
  694. if (esel >= 0)
  695. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  696. }
  697. /* Invalidate all vcpu id mappings */
  698. kvmppc_e500_id_table_reset_all(vcpu_e500);
  699. return EMULATE_DONE;
  700. }
  701. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  702. {
  703. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  704. int tlbsel, esel;
  705. struct kvm_book3e_206_tlb_entry *gtlbe;
  706. tlbsel = get_tlb_tlbsel(vcpu);
  707. esel = get_tlb_esel(vcpu, tlbsel);
  708. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  709. vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
  710. vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  711. vcpu->arch.shared->mas1 = gtlbe->mas1;
  712. vcpu->arch.shared->mas2 = gtlbe->mas2;
  713. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  714. return EMULATE_DONE;
  715. }
  716. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
  717. {
  718. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  719. int as = !!get_cur_sas(vcpu);
  720. unsigned int pid = get_cur_spid(vcpu);
  721. int esel, tlbsel;
  722. struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
  723. gva_t ea;
  724. ea = kvmppc_get_gpr(vcpu, rb);
  725. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  726. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  727. if (esel >= 0) {
  728. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  729. break;
  730. }
  731. }
  732. if (gtlbe) {
  733. esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
  734. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  735. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  736. vcpu->arch.shared->mas1 = gtlbe->mas1;
  737. vcpu->arch.shared->mas2 = gtlbe->mas2;
  738. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  739. } else {
  740. int victim;
  741. /* since we only have two TLBs, only lower bit is used. */
  742. tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
  743. victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
  744. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
  745. | MAS0_ESEL(victim)
  746. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  747. vcpu->arch.shared->mas1 =
  748. (vcpu->arch.shared->mas6 & MAS6_SPID0)
  749. | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  750. | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
  751. vcpu->arch.shared->mas2 &= MAS2_EPN;
  752. vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
  753. MAS2_ATTRIB_MASK;
  754. vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
  755. MAS3_U2 | MAS3_U3;
  756. }
  757. kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
  758. return EMULATE_DONE;
  759. }
  760. /* sesel is for tlb1 only */
  761. static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
  762. struct kvm_book3e_206_tlb_entry *gtlbe,
  763. struct kvm_book3e_206_tlb_entry *stlbe,
  764. int stlbsel, int sesel)
  765. {
  766. int stid;
  767. preempt_disable();
  768. stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe),
  769. get_tlb_tid(gtlbe),
  770. get_cur_pr(&vcpu_e500->vcpu), 0);
  771. stlbe->mas1 |= MAS1_TID(stid);
  772. write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
  773. preempt_enable();
  774. }
  775. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  776. {
  777. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  778. struct kvm_book3e_206_tlb_entry *gtlbe;
  779. int tlbsel, esel;
  780. tlbsel = get_tlb_tlbsel(vcpu);
  781. esel = get_tlb_esel(vcpu, tlbsel);
  782. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  783. if (get_tlb_v(gtlbe))
  784. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  785. gtlbe->mas1 = vcpu->arch.shared->mas1;
  786. gtlbe->mas2 = vcpu->arch.shared->mas2;
  787. gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
  788. trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
  789. gtlbe->mas2, gtlbe->mas7_3);
  790. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  791. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  792. struct kvm_book3e_206_tlb_entry stlbe;
  793. int stlbsel, sesel;
  794. u64 eaddr;
  795. u64 raddr;
  796. switch (tlbsel) {
  797. case 0:
  798. /* TLB0 */
  799. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  800. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  801. stlbsel = 0;
  802. kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
  803. sesel = 0; /* unused */
  804. break;
  805. case 1:
  806. /* TLB1 */
  807. eaddr = get_tlb_eaddr(gtlbe);
  808. raddr = get_tlb_raddr(gtlbe);
  809. /* Create a 4KB mapping on the host.
  810. * If the guest wanted a large page,
  811. * only the first 4KB is mapped here and the rest
  812. * are mapped on the fly. */
  813. stlbsel = 1;
  814. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
  815. raddr >> PAGE_SHIFT, gtlbe, &stlbe);
  816. break;
  817. default:
  818. BUG();
  819. }
  820. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  821. }
  822. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  823. return EMULATE_DONE;
  824. }
  825. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  826. {
  827. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  828. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  829. }
  830. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  831. {
  832. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  833. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  834. }
  835. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  836. {
  837. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  838. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  839. }
  840. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  841. {
  842. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  843. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  844. }
  845. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  846. gva_t eaddr)
  847. {
  848. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  849. struct kvm_book3e_206_tlb_entry *gtlbe;
  850. u64 pgmask;
  851. gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
  852. pgmask = get_tlb_bytes(gtlbe) - 1;
  853. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  854. }
  855. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  856. {
  857. }
  858. void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
  859. unsigned int index)
  860. {
  861. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  862. struct tlbe_priv *priv;
  863. struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
  864. int tlbsel = tlbsel_of(index);
  865. int esel = esel_of(index);
  866. int stlbsel, sesel;
  867. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  868. switch (tlbsel) {
  869. case 0:
  870. stlbsel = 0;
  871. sesel = 0; /* unused */
  872. priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
  873. kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
  874. &priv->ref, eaddr, &stlbe);
  875. break;
  876. case 1: {
  877. gfn_t gfn = gpaddr >> PAGE_SHIFT;
  878. stlbsel = 1;
  879. sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
  880. gtlbe, &stlbe);
  881. break;
  882. }
  883. default:
  884. BUG();
  885. break;
  886. }
  887. write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
  888. }
  889. int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  890. gva_t eaddr, unsigned int pid, int as)
  891. {
  892. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  893. int esel, tlbsel;
  894. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  895. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  896. if (esel >= 0)
  897. return index_of(tlbsel, esel);
  898. }
  899. return -1;
  900. }
  901. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
  902. {
  903. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  904. if (vcpu->arch.pid != pid) {
  905. vcpu_e500->pid[0] = vcpu->arch.pid = pid;
  906. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  907. }
  908. }
  909. void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
  910. {
  911. struct kvm_book3e_206_tlb_entry *tlbe;
  912. /* Insert large initial mapping for guest. */
  913. tlbe = get_entry(vcpu_e500, 1, 0);
  914. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
  915. tlbe->mas2 = 0;
  916. tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK;
  917. /* 4K map for serial output. Used by kernel wrapper. */
  918. tlbe = get_entry(vcpu_e500, 1, 1);
  919. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  920. tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
  921. tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
  922. }
  923. static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
  924. {
  925. int i;
  926. clear_tlb_refs(vcpu_e500);
  927. kfree(vcpu_e500->gtlb_priv[0]);
  928. kfree(vcpu_e500->gtlb_priv[1]);
  929. if (vcpu_e500->shared_tlb_pages) {
  930. vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
  931. PAGE_SIZE)));
  932. for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
  933. set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
  934. put_page(vcpu_e500->shared_tlb_pages[i]);
  935. }
  936. vcpu_e500->num_shared_tlb_pages = 0;
  937. vcpu_e500->shared_tlb_pages = NULL;
  938. } else {
  939. kfree(vcpu_e500->gtlb_arch);
  940. }
  941. vcpu_e500->gtlb_arch = NULL;
  942. }
  943. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  944. struct kvm_config_tlb *cfg)
  945. {
  946. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  947. struct kvm_book3e_206_tlb_params params;
  948. char *virt;
  949. struct page **pages;
  950. struct tlbe_priv *privs[2] = {};
  951. size_t array_len;
  952. u32 sets;
  953. int num_pages, ret, i;
  954. if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
  955. return -EINVAL;
  956. if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
  957. sizeof(params)))
  958. return -EFAULT;
  959. if (params.tlb_sizes[1] > 64)
  960. return -EINVAL;
  961. if (params.tlb_ways[1] != params.tlb_sizes[1])
  962. return -EINVAL;
  963. if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
  964. return -EINVAL;
  965. if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
  966. return -EINVAL;
  967. if (!is_power_of_2(params.tlb_ways[0]))
  968. return -EINVAL;
  969. sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
  970. if (!is_power_of_2(sets))
  971. return -EINVAL;
  972. array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
  973. array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
  974. if (cfg->array_len < array_len)
  975. return -EINVAL;
  976. num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
  977. cfg->array / PAGE_SIZE;
  978. pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  979. if (!pages)
  980. return -ENOMEM;
  981. ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
  982. if (ret < 0)
  983. goto err_pages;
  984. if (ret != num_pages) {
  985. num_pages = ret;
  986. ret = -EFAULT;
  987. goto err_put_page;
  988. }
  989. virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
  990. if (!virt)
  991. goto err_put_page;
  992. privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
  993. GFP_KERNEL);
  994. privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
  995. GFP_KERNEL);
  996. if (!privs[0] || !privs[1])
  997. goto err_put_page;
  998. free_gtlb(vcpu_e500);
  999. vcpu_e500->gtlb_priv[0] = privs[0];
  1000. vcpu_e500->gtlb_priv[1] = privs[1];
  1001. vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
  1002. (virt + (cfg->array & (PAGE_SIZE - 1)));
  1003. vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
  1004. vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
  1005. vcpu_e500->gtlb_offset[0] = 0;
  1006. vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
  1007. vcpu_e500->tlb0cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1008. if (params.tlb_sizes[0] <= 2048)
  1009. vcpu_e500->tlb0cfg |= params.tlb_sizes[0];
  1010. vcpu_e500->tlb0cfg |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
  1011. vcpu_e500->tlb1cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1012. vcpu_e500->tlb1cfg |= params.tlb_sizes[1];
  1013. vcpu_e500->tlb1cfg |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
  1014. vcpu_e500->shared_tlb_pages = pages;
  1015. vcpu_e500->num_shared_tlb_pages = num_pages;
  1016. vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
  1017. vcpu_e500->gtlb_params[0].sets = sets;
  1018. vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
  1019. vcpu_e500->gtlb_params[1].sets = 1;
  1020. return 0;
  1021. err_put_page:
  1022. kfree(privs[0]);
  1023. kfree(privs[1]);
  1024. for (i = 0; i < num_pages; i++)
  1025. put_page(pages[i]);
  1026. err_pages:
  1027. kfree(pages);
  1028. return ret;
  1029. }
  1030. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  1031. struct kvm_dirty_tlb *dirty)
  1032. {
  1033. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  1034. clear_tlb_refs(vcpu_e500);
  1035. return 0;
  1036. }
  1037. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  1038. {
  1039. int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
  1040. int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
  1041. host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
  1042. host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  1043. /*
  1044. * This should never happen on real e500 hardware, but is
  1045. * architecturally possible -- e.g. in some weird nested
  1046. * virtualization case.
  1047. */
  1048. if (host_tlb_params[0].entries == 0 ||
  1049. host_tlb_params[1].entries == 0) {
  1050. pr_err("%s: need to know host tlb size\n", __func__);
  1051. return -ENODEV;
  1052. }
  1053. host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
  1054. TLBnCFG_ASSOC_SHIFT;
  1055. host_tlb_params[1].ways = host_tlb_params[1].entries;
  1056. if (!is_power_of_2(host_tlb_params[0].entries) ||
  1057. !is_power_of_2(host_tlb_params[0].ways) ||
  1058. host_tlb_params[0].entries < host_tlb_params[0].ways ||
  1059. host_tlb_params[0].ways == 0) {
  1060. pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
  1061. __func__, host_tlb_params[0].entries,
  1062. host_tlb_params[0].ways);
  1063. return -ENODEV;
  1064. }
  1065. host_tlb_params[0].sets =
  1066. host_tlb_params[0].entries / host_tlb_params[0].ways;
  1067. host_tlb_params[1].sets = 1;
  1068. vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
  1069. vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
  1070. vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
  1071. vcpu_e500->gtlb_params[0].sets =
  1072. KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
  1073. vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
  1074. vcpu_e500->gtlb_params[1].sets = 1;
  1075. vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
  1076. if (!vcpu_e500->gtlb_arch)
  1077. return -ENOMEM;
  1078. vcpu_e500->gtlb_offset[0] = 0;
  1079. vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
  1080. vcpu_e500->tlb_refs[0] =
  1081. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
  1082. GFP_KERNEL);
  1083. if (!vcpu_e500->tlb_refs[0])
  1084. goto err;
  1085. vcpu_e500->tlb_refs[1] =
  1086. kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
  1087. GFP_KERNEL);
  1088. if (!vcpu_e500->tlb_refs[1])
  1089. goto err;
  1090. vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
  1091. vcpu_e500->gtlb_params[0].entries,
  1092. GFP_KERNEL);
  1093. if (!vcpu_e500->gtlb_priv[0])
  1094. goto err;
  1095. vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
  1096. vcpu_e500->gtlb_params[1].entries,
  1097. GFP_KERNEL);
  1098. if (!vcpu_e500->gtlb_priv[1])
  1099. goto err;
  1100. if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
  1101. goto err;
  1102. /* Init TLB configuration register */
  1103. vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) &
  1104. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1105. vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[0].entries;
  1106. vcpu_e500->tlb0cfg |=
  1107. vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
  1108. vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) &
  1109. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  1110. vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[1].entries;
  1111. vcpu_e500->tlb0cfg |=
  1112. vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
  1113. return 0;
  1114. err:
  1115. free_gtlb(vcpu_e500);
  1116. kfree(vcpu_e500->tlb_refs[0]);
  1117. kfree(vcpu_e500->tlb_refs[1]);
  1118. return -1;
  1119. }
  1120. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  1121. {
  1122. free_gtlb(vcpu_e500);
  1123. kvmppc_e500_id_table_free(vcpu_e500);
  1124. kfree(vcpu_e500->tlb_refs[0]);
  1125. kfree(vcpu_e500->tlb_refs[1]);
  1126. }