e500_mmu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. * Copyright (C) 2008-2013 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, yu.liu@freescale.com
  5. * Scott Wood, scottwood@freescale.com
  6. * Ashish Kalra, ashish.kalra@freescale.com
  7. * Varun Sethi, varun.sethi@freescale.com
  8. * Alexander Graf, agraf@suse.de
  9. *
  10. * Description:
  11. * This file is based on arch/powerpc/kvm/44x_tlb.c,
  12. * by Hollis Blanchard <hollisb@us.ibm.com>.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License, version 2, as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/kvm.h>
  23. #include <linux/kvm_host.h>
  24. #include <linux/highmem.h>
  25. #include <linux/log2.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/sched.h>
  28. #include <linux/rwsem.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/hugetlb.h>
  31. #include <asm/kvm_ppc.h>
  32. #include "e500.h"
  33. #include "trace.h"
  34. #include "timing.h"
  35. #include "e500_mmu_host.h"
  36. static inline unsigned int gtlb0_get_next_victim(
  37. struct kvmppc_vcpu_e500 *vcpu_e500)
  38. {
  39. unsigned int victim;
  40. victim = vcpu_e500->gtlb_nv[0]++;
  41. if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways))
  42. vcpu_e500->gtlb_nv[0] = 0;
  43. return victim;
  44. }
  45. static int tlb0_set_base(gva_t addr, int sets, int ways)
  46. {
  47. int set_base;
  48. set_base = (addr >> PAGE_SHIFT) & (sets - 1);
  49. set_base *= ways;
  50. return set_base;
  51. }
  52. static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
  53. {
  54. return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
  55. vcpu_e500->gtlb_params[0].ways);
  56. }
  57. static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
  58. {
  59. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  60. int esel = get_tlb_esel_bit(vcpu);
  61. if (tlbsel == 0) {
  62. esel &= vcpu_e500->gtlb_params[0].ways - 1;
  63. esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
  64. } else {
  65. esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
  66. }
  67. return esel;
  68. }
  69. /* Search the guest TLB for a matching entry. */
  70. static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
  71. gva_t eaddr, int tlbsel, unsigned int pid, int as)
  72. {
  73. int size = vcpu_e500->gtlb_params[tlbsel].entries;
  74. unsigned int set_base, offset;
  75. int i;
  76. if (tlbsel == 0) {
  77. set_base = gtlb0_set_base(vcpu_e500, eaddr);
  78. size = vcpu_e500->gtlb_params[0].ways;
  79. } else {
  80. if (eaddr < vcpu_e500->tlb1_min_eaddr ||
  81. eaddr > vcpu_e500->tlb1_max_eaddr)
  82. return -1;
  83. set_base = 0;
  84. }
  85. offset = vcpu_e500->gtlb_offset[tlbsel];
  86. for (i = 0; i < size; i++) {
  87. struct kvm_book3e_206_tlb_entry *tlbe =
  88. &vcpu_e500->gtlb_arch[offset + set_base + i];
  89. unsigned int tid;
  90. if (eaddr < get_tlb_eaddr(tlbe))
  91. continue;
  92. if (eaddr > get_tlb_end(tlbe))
  93. continue;
  94. tid = get_tlb_tid(tlbe);
  95. if (tid && (tid != pid))
  96. continue;
  97. if (!get_tlb_v(tlbe))
  98. continue;
  99. if (get_tlb_ts(tlbe) != as && as != -1)
  100. continue;
  101. return set_base + i;
  102. }
  103. return -1;
  104. }
  105. static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
  106. unsigned int eaddr, int as)
  107. {
  108. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  109. unsigned int victim, tsized;
  110. int tlbsel;
  111. /* since we only have two TLBs, only lower bit is used. */
  112. tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
  113. victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
  114. tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
  115. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
  116. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  117. vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
  118. | MAS1_TID(get_tlbmiss_tid(vcpu))
  119. | MAS1_TSIZE(tsized);
  120. vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
  121. | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
  122. vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
  123. vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
  124. | (get_cur_pid(vcpu) << 16)
  125. | (as ? MAS6_SAS : 0);
  126. }
  127. static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500)
  128. {
  129. int size = vcpu_e500->gtlb_params[1].entries;
  130. unsigned int offset;
  131. gva_t eaddr;
  132. int i;
  133. vcpu_e500->tlb1_min_eaddr = ~0UL;
  134. vcpu_e500->tlb1_max_eaddr = 0;
  135. offset = vcpu_e500->gtlb_offset[1];
  136. for (i = 0; i < size; i++) {
  137. struct kvm_book3e_206_tlb_entry *tlbe =
  138. &vcpu_e500->gtlb_arch[offset + i];
  139. if (!get_tlb_v(tlbe))
  140. continue;
  141. eaddr = get_tlb_eaddr(tlbe);
  142. vcpu_e500->tlb1_min_eaddr =
  143. min(vcpu_e500->tlb1_min_eaddr, eaddr);
  144. eaddr = get_tlb_end(tlbe);
  145. vcpu_e500->tlb1_max_eaddr =
  146. max(vcpu_e500->tlb1_max_eaddr, eaddr);
  147. }
  148. }
  149. static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500,
  150. struct kvm_book3e_206_tlb_entry *gtlbe)
  151. {
  152. unsigned long start, end, size;
  153. size = get_tlb_bytes(gtlbe);
  154. start = get_tlb_eaddr(gtlbe) & ~(size - 1);
  155. end = start + size - 1;
  156. return vcpu_e500->tlb1_min_eaddr == start ||
  157. vcpu_e500->tlb1_max_eaddr == end;
  158. }
  159. /* This function is supposed to be called for a adding a new valid tlb entry */
  160. static void kvmppc_set_tlb1map_range(struct kvm_vcpu *vcpu,
  161. struct kvm_book3e_206_tlb_entry *gtlbe)
  162. {
  163. unsigned long start, end, size;
  164. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  165. if (!get_tlb_v(gtlbe))
  166. return;
  167. size = get_tlb_bytes(gtlbe);
  168. start = get_tlb_eaddr(gtlbe) & ~(size - 1);
  169. end = start + size - 1;
  170. vcpu_e500->tlb1_min_eaddr = min(vcpu_e500->tlb1_min_eaddr, start);
  171. vcpu_e500->tlb1_max_eaddr = max(vcpu_e500->tlb1_max_eaddr, end);
  172. }
  173. static inline int kvmppc_e500_gtlbe_invalidate(
  174. struct kvmppc_vcpu_e500 *vcpu_e500,
  175. int tlbsel, int esel)
  176. {
  177. struct kvm_book3e_206_tlb_entry *gtlbe =
  178. get_entry(vcpu_e500, tlbsel, esel);
  179. if (unlikely(get_tlb_iprot(gtlbe)))
  180. return -1;
  181. if (tlbsel == 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
  182. kvmppc_recalc_tlb1map_range(vcpu_e500);
  183. gtlbe->mas1 = 0;
  184. return 0;
  185. }
  186. int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
  187. {
  188. int esel;
  189. if (value & MMUCSR0_TLB0FI)
  190. for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
  191. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
  192. if (value & MMUCSR0_TLB1FI)
  193. for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
  194. kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
  195. /* Invalidate all host shadow mappings */
  196. kvmppc_core_flush_tlb(&vcpu_e500->vcpu);
  197. return EMULATE_DONE;
  198. }
  199. int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, gva_t ea)
  200. {
  201. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  202. unsigned int ia;
  203. int esel, tlbsel;
  204. ia = (ea >> 2) & 0x1;
  205. /* since we only have two TLBs, only lower bit is used. */
  206. tlbsel = (ea >> 3) & 0x1;
  207. if (ia) {
  208. /* invalidate all entries */
  209. for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
  210. esel++)
  211. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  212. } else {
  213. ea &= 0xfffff000;
  214. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
  215. get_cur_pid(vcpu), -1);
  216. if (esel >= 0)
  217. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  218. }
  219. /* Invalidate all host shadow mappings */
  220. kvmppc_core_flush_tlb(&vcpu_e500->vcpu);
  221. return EMULATE_DONE;
  222. }
  223. static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
  224. int pid, int type)
  225. {
  226. struct kvm_book3e_206_tlb_entry *tlbe;
  227. int tid, esel;
  228. /* invalidate all entries */
  229. for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) {
  230. tlbe = get_entry(vcpu_e500, tlbsel, esel);
  231. tid = get_tlb_tid(tlbe);
  232. if (type == 0 || tid == pid) {
  233. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  234. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  235. }
  236. }
  237. }
  238. static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid,
  239. gva_t ea)
  240. {
  241. int tlbsel, esel;
  242. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  243. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
  244. if (esel >= 0) {
  245. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  246. kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
  247. break;
  248. }
  249. }
  250. }
  251. int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int type, gva_t ea)
  252. {
  253. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  254. int pid = get_cur_spid(vcpu);
  255. if (type == 0 || type == 1) {
  256. tlbilx_all(vcpu_e500, 0, pid, type);
  257. tlbilx_all(vcpu_e500, 1, pid, type);
  258. } else if (type == 3) {
  259. tlbilx_one(vcpu_e500, pid, ea);
  260. }
  261. return EMULATE_DONE;
  262. }
  263. int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
  264. {
  265. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  266. int tlbsel, esel;
  267. struct kvm_book3e_206_tlb_entry *gtlbe;
  268. tlbsel = get_tlb_tlbsel(vcpu);
  269. esel = get_tlb_esel(vcpu, tlbsel);
  270. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  271. vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
  272. vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  273. vcpu->arch.shared->mas1 = gtlbe->mas1;
  274. vcpu->arch.shared->mas2 = gtlbe->mas2;
  275. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  276. return EMULATE_DONE;
  277. }
  278. int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, gva_t ea)
  279. {
  280. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  281. int as = !!get_cur_sas(vcpu);
  282. unsigned int pid = get_cur_spid(vcpu);
  283. int esel, tlbsel;
  284. struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
  285. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  286. esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
  287. if (esel >= 0) {
  288. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  289. break;
  290. }
  291. }
  292. if (gtlbe) {
  293. esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
  294. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
  295. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  296. vcpu->arch.shared->mas1 = gtlbe->mas1;
  297. vcpu->arch.shared->mas2 = gtlbe->mas2;
  298. vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
  299. } else {
  300. int victim;
  301. /* since we only have two TLBs, only lower bit is used. */
  302. tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
  303. victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
  304. vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
  305. | MAS0_ESEL(victim)
  306. | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
  307. vcpu->arch.shared->mas1 =
  308. (vcpu->arch.shared->mas6 & MAS6_SPID0)
  309. | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
  310. | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
  311. vcpu->arch.shared->mas2 &= MAS2_EPN;
  312. vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
  313. MAS2_ATTRIB_MASK;
  314. vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
  315. MAS3_U2 | MAS3_U3;
  316. }
  317. kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
  318. return EMULATE_DONE;
  319. }
  320. int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
  321. {
  322. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  323. struct kvm_book3e_206_tlb_entry *gtlbe;
  324. int tlbsel, esel;
  325. int recal = 0;
  326. tlbsel = get_tlb_tlbsel(vcpu);
  327. esel = get_tlb_esel(vcpu, tlbsel);
  328. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  329. if (get_tlb_v(gtlbe)) {
  330. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  331. if ((tlbsel == 1) &&
  332. kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
  333. recal = 1;
  334. }
  335. gtlbe->mas1 = vcpu->arch.shared->mas1;
  336. gtlbe->mas2 = vcpu->arch.shared->mas2;
  337. if (!(vcpu->arch.shared->msr & MSR_CM))
  338. gtlbe->mas2 &= 0xffffffffUL;
  339. gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
  340. trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
  341. gtlbe->mas2, gtlbe->mas7_3);
  342. if (tlbsel == 1) {
  343. /*
  344. * If a valid tlb1 entry is overwritten then recalculate the
  345. * min/max TLB1 map address range otherwise no need to look
  346. * in tlb1 array.
  347. */
  348. if (recal)
  349. kvmppc_recalc_tlb1map_range(vcpu_e500);
  350. else
  351. kvmppc_set_tlb1map_range(vcpu, gtlbe);
  352. }
  353. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  354. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  355. u64 eaddr = get_tlb_eaddr(gtlbe);
  356. u64 raddr = get_tlb_raddr(gtlbe);
  357. if (tlbsel == 0) {
  358. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  359. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  360. }
  361. /* Premap the faulting page */
  362. kvmppc_mmu_map(vcpu, eaddr, raddr, index_of(tlbsel, esel));
  363. }
  364. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  365. return EMULATE_DONE;
  366. }
  367. static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  368. gva_t eaddr, unsigned int pid, int as)
  369. {
  370. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  371. int esel, tlbsel;
  372. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  373. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  374. if (esel >= 0)
  375. return index_of(tlbsel, esel);
  376. }
  377. return -1;
  378. }
  379. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  380. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  381. struct kvm_translation *tr)
  382. {
  383. int index;
  384. gva_t eaddr;
  385. u8 pid;
  386. u8 as;
  387. eaddr = tr->linear_address;
  388. pid = (tr->linear_address >> 32) & 0xff;
  389. as = (tr->linear_address >> 40) & 0x1;
  390. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  391. if (index < 0) {
  392. tr->valid = 0;
  393. return 0;
  394. }
  395. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  396. /* XXX what does "writeable" and "usermode" even mean? */
  397. tr->valid = 1;
  398. return 0;
  399. }
  400. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  401. {
  402. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  403. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  404. }
  405. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  406. {
  407. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  408. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  409. }
  410. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  411. {
  412. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  413. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  414. }
  415. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  416. {
  417. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  418. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  419. }
  420. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  421. gva_t eaddr)
  422. {
  423. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  424. struct kvm_book3e_206_tlb_entry *gtlbe;
  425. u64 pgmask;
  426. gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
  427. pgmask = get_tlb_bytes(gtlbe) - 1;
  428. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  429. }
  430. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  431. {
  432. }
  433. /*****************************************/
  434. static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
  435. {
  436. int i;
  437. kvmppc_core_flush_tlb(&vcpu_e500->vcpu);
  438. kfree(vcpu_e500->g2h_tlb1_map);
  439. kfree(vcpu_e500->gtlb_priv[0]);
  440. kfree(vcpu_e500->gtlb_priv[1]);
  441. if (vcpu_e500->shared_tlb_pages) {
  442. vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
  443. PAGE_SIZE)));
  444. for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
  445. set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
  446. put_page(vcpu_e500->shared_tlb_pages[i]);
  447. }
  448. vcpu_e500->num_shared_tlb_pages = 0;
  449. kfree(vcpu_e500->shared_tlb_pages);
  450. vcpu_e500->shared_tlb_pages = NULL;
  451. } else {
  452. kfree(vcpu_e500->gtlb_arch);
  453. }
  454. vcpu_e500->gtlb_arch = NULL;
  455. }
  456. void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  457. {
  458. sregs->u.e.mas0 = vcpu->arch.shared->mas0;
  459. sregs->u.e.mas1 = vcpu->arch.shared->mas1;
  460. sregs->u.e.mas2 = vcpu->arch.shared->mas2;
  461. sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
  462. sregs->u.e.mas4 = vcpu->arch.shared->mas4;
  463. sregs->u.e.mas6 = vcpu->arch.shared->mas6;
  464. sregs->u.e.mmucfg = vcpu->arch.mmucfg;
  465. sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
  466. sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
  467. sregs->u.e.tlbcfg[2] = 0;
  468. sregs->u.e.tlbcfg[3] = 0;
  469. }
  470. int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  471. {
  472. if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
  473. vcpu->arch.shared->mas0 = sregs->u.e.mas0;
  474. vcpu->arch.shared->mas1 = sregs->u.e.mas1;
  475. vcpu->arch.shared->mas2 = sregs->u.e.mas2;
  476. vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
  477. vcpu->arch.shared->mas4 = sregs->u.e.mas4;
  478. vcpu->arch.shared->mas6 = sregs->u.e.mas6;
  479. }
  480. return 0;
  481. }
  482. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  483. struct kvm_config_tlb *cfg)
  484. {
  485. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  486. struct kvm_book3e_206_tlb_params params;
  487. char *virt;
  488. struct page **pages;
  489. struct tlbe_priv *privs[2] = {};
  490. u64 *g2h_bitmap = NULL;
  491. size_t array_len;
  492. u32 sets;
  493. int num_pages, ret, i;
  494. if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
  495. return -EINVAL;
  496. if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
  497. sizeof(params)))
  498. return -EFAULT;
  499. if (params.tlb_sizes[1] > 64)
  500. return -EINVAL;
  501. if (params.tlb_ways[1] != params.tlb_sizes[1])
  502. return -EINVAL;
  503. if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
  504. return -EINVAL;
  505. if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
  506. return -EINVAL;
  507. if (!is_power_of_2(params.tlb_ways[0]))
  508. return -EINVAL;
  509. sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
  510. if (!is_power_of_2(sets))
  511. return -EINVAL;
  512. array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
  513. array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
  514. if (cfg->array_len < array_len)
  515. return -EINVAL;
  516. num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
  517. cfg->array / PAGE_SIZE;
  518. pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  519. if (!pages)
  520. return -ENOMEM;
  521. ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
  522. if (ret < 0)
  523. goto err_pages;
  524. if (ret != num_pages) {
  525. num_pages = ret;
  526. ret = -EFAULT;
  527. goto err_put_page;
  528. }
  529. virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
  530. if (!virt) {
  531. ret = -ENOMEM;
  532. goto err_put_page;
  533. }
  534. privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
  535. GFP_KERNEL);
  536. privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
  537. GFP_KERNEL);
  538. if (!privs[0] || !privs[1]) {
  539. ret = -ENOMEM;
  540. goto err_privs;
  541. }
  542. g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
  543. GFP_KERNEL);
  544. if (!g2h_bitmap) {
  545. ret = -ENOMEM;
  546. goto err_privs;
  547. }
  548. free_gtlb(vcpu_e500);
  549. vcpu_e500->gtlb_priv[0] = privs[0];
  550. vcpu_e500->gtlb_priv[1] = privs[1];
  551. vcpu_e500->g2h_tlb1_map = g2h_bitmap;
  552. vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
  553. (virt + (cfg->array & (PAGE_SIZE - 1)));
  554. vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
  555. vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
  556. vcpu_e500->gtlb_offset[0] = 0;
  557. vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
  558. vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
  559. vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  560. if (params.tlb_sizes[0] <= 2048)
  561. vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
  562. vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
  563. vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  564. vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
  565. vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
  566. vcpu_e500->shared_tlb_pages = pages;
  567. vcpu_e500->num_shared_tlb_pages = num_pages;
  568. vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
  569. vcpu_e500->gtlb_params[0].sets = sets;
  570. vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
  571. vcpu_e500->gtlb_params[1].sets = 1;
  572. kvmppc_recalc_tlb1map_range(vcpu_e500);
  573. return 0;
  574. err_privs:
  575. kfree(privs[0]);
  576. kfree(privs[1]);
  577. err_put_page:
  578. for (i = 0; i < num_pages; i++)
  579. put_page(pages[i]);
  580. err_pages:
  581. kfree(pages);
  582. return ret;
  583. }
  584. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  585. struct kvm_dirty_tlb *dirty)
  586. {
  587. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  588. kvmppc_recalc_tlb1map_range(vcpu_e500);
  589. kvmppc_core_flush_tlb(vcpu);
  590. return 0;
  591. }
  592. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  593. {
  594. struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
  595. int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
  596. int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
  597. if (e500_mmu_host_init(vcpu_e500))
  598. goto err;
  599. vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
  600. vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
  601. vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
  602. vcpu_e500->gtlb_params[0].sets =
  603. KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
  604. vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
  605. vcpu_e500->gtlb_params[1].sets = 1;
  606. vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
  607. if (!vcpu_e500->gtlb_arch)
  608. return -ENOMEM;
  609. vcpu_e500->gtlb_offset[0] = 0;
  610. vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
  611. vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
  612. vcpu_e500->gtlb_params[0].entries,
  613. GFP_KERNEL);
  614. if (!vcpu_e500->gtlb_priv[0])
  615. goto err;
  616. vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
  617. vcpu_e500->gtlb_params[1].entries,
  618. GFP_KERNEL);
  619. if (!vcpu_e500->gtlb_priv[1])
  620. goto err;
  621. vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(u64) *
  622. vcpu_e500->gtlb_params[1].entries,
  623. GFP_KERNEL);
  624. if (!vcpu_e500->g2h_tlb1_map)
  625. goto err;
  626. /* Init TLB configuration register */
  627. vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
  628. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  629. vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
  630. vcpu->arch.tlbcfg[0] |=
  631. vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
  632. vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
  633. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  634. vcpu->arch.tlbcfg[1] |= vcpu_e500->gtlb_params[1].entries;
  635. vcpu->arch.tlbcfg[1] |=
  636. vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
  637. kvmppc_recalc_tlb1map_range(vcpu_e500);
  638. return 0;
  639. err:
  640. free_gtlb(vcpu_e500);
  641. return -1;
  642. }
  643. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  644. {
  645. free_gtlb(vcpu_e500);
  646. e500_mmu_host_uninit(vcpu_e500);
  647. }