e500_mmu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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_booke.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. gva_t 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. int idx;
  327. tlbsel = get_tlb_tlbsel(vcpu);
  328. esel = get_tlb_esel(vcpu, tlbsel);
  329. gtlbe = get_entry(vcpu_e500, tlbsel, esel);
  330. if (get_tlb_v(gtlbe)) {
  331. inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
  332. if ((tlbsel == 1) &&
  333. kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
  334. recal = 1;
  335. }
  336. gtlbe->mas1 = vcpu->arch.shared->mas1;
  337. gtlbe->mas2 = vcpu->arch.shared->mas2;
  338. if (!(vcpu->arch.shared->msr & MSR_CM))
  339. gtlbe->mas2 &= 0xffffffffUL;
  340. gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
  341. trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
  342. gtlbe->mas2, gtlbe->mas7_3);
  343. if (tlbsel == 1) {
  344. /*
  345. * If a valid tlb1 entry is overwritten then recalculate the
  346. * min/max TLB1 map address range otherwise no need to look
  347. * in tlb1 array.
  348. */
  349. if (recal)
  350. kvmppc_recalc_tlb1map_range(vcpu_e500);
  351. else
  352. kvmppc_set_tlb1map_range(vcpu, gtlbe);
  353. }
  354. idx = srcu_read_lock(&vcpu->kvm->srcu);
  355. /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
  356. if (tlbe_is_host_safe(vcpu, gtlbe)) {
  357. u64 eaddr = get_tlb_eaddr(gtlbe);
  358. u64 raddr = get_tlb_raddr(gtlbe);
  359. if (tlbsel == 0) {
  360. gtlbe->mas1 &= ~MAS1_TSIZE(~0);
  361. gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  362. }
  363. /* Premap the faulting page */
  364. kvmppc_mmu_map(vcpu, eaddr, raddr, index_of(tlbsel, esel));
  365. }
  366. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  367. kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
  368. return EMULATE_DONE;
  369. }
  370. static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
  371. gva_t eaddr, unsigned int pid, int as)
  372. {
  373. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  374. int esel, tlbsel;
  375. for (tlbsel = 0; tlbsel < 2; tlbsel++) {
  376. esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
  377. if (esel >= 0)
  378. return index_of(tlbsel, esel);
  379. }
  380. return -1;
  381. }
  382. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  383. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  384. struct kvm_translation *tr)
  385. {
  386. int index;
  387. gva_t eaddr;
  388. u8 pid;
  389. u8 as;
  390. eaddr = tr->linear_address;
  391. pid = (tr->linear_address >> 32) & 0xff;
  392. as = (tr->linear_address >> 40) & 0x1;
  393. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  394. if (index < 0) {
  395. tr->valid = 0;
  396. return 0;
  397. }
  398. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  399. /* XXX what does "writeable" and "usermode" even mean? */
  400. tr->valid = 1;
  401. return 0;
  402. }
  403. int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  404. {
  405. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  406. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  407. }
  408. int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
  409. {
  410. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  411. return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
  412. }
  413. void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
  414. {
  415. unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
  416. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
  417. }
  418. void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
  419. {
  420. unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
  421. kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
  422. }
  423. gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
  424. gva_t eaddr)
  425. {
  426. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  427. struct kvm_book3e_206_tlb_entry *gtlbe;
  428. u64 pgmask;
  429. gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
  430. pgmask = get_tlb_bytes(gtlbe) - 1;
  431. return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
  432. }
  433. void kvmppc_mmu_destroy_e500(struct kvm_vcpu *vcpu)
  434. {
  435. }
  436. /*****************************************/
  437. static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
  438. {
  439. int i;
  440. kvmppc_core_flush_tlb(&vcpu_e500->vcpu);
  441. kfree(vcpu_e500->g2h_tlb1_map);
  442. kfree(vcpu_e500->gtlb_priv[0]);
  443. kfree(vcpu_e500->gtlb_priv[1]);
  444. if (vcpu_e500->shared_tlb_pages) {
  445. vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
  446. PAGE_SIZE)));
  447. for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
  448. set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
  449. put_page(vcpu_e500->shared_tlb_pages[i]);
  450. }
  451. vcpu_e500->num_shared_tlb_pages = 0;
  452. kfree(vcpu_e500->shared_tlb_pages);
  453. vcpu_e500->shared_tlb_pages = NULL;
  454. } else {
  455. kfree(vcpu_e500->gtlb_arch);
  456. }
  457. vcpu_e500->gtlb_arch = NULL;
  458. }
  459. void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  460. {
  461. sregs->u.e.mas0 = vcpu->arch.shared->mas0;
  462. sregs->u.e.mas1 = vcpu->arch.shared->mas1;
  463. sregs->u.e.mas2 = vcpu->arch.shared->mas2;
  464. sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
  465. sregs->u.e.mas4 = vcpu->arch.shared->mas4;
  466. sregs->u.e.mas6 = vcpu->arch.shared->mas6;
  467. sregs->u.e.mmucfg = vcpu->arch.mmucfg;
  468. sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
  469. sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
  470. sregs->u.e.tlbcfg[2] = 0;
  471. sregs->u.e.tlbcfg[3] = 0;
  472. }
  473. int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  474. {
  475. if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
  476. vcpu->arch.shared->mas0 = sregs->u.e.mas0;
  477. vcpu->arch.shared->mas1 = sregs->u.e.mas1;
  478. vcpu->arch.shared->mas2 = sregs->u.e.mas2;
  479. vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
  480. vcpu->arch.shared->mas4 = sregs->u.e.mas4;
  481. vcpu->arch.shared->mas6 = sregs->u.e.mas6;
  482. }
  483. return 0;
  484. }
  485. int kvmppc_get_one_reg_e500_tlb(struct kvm_vcpu *vcpu, u64 id,
  486. union kvmppc_one_reg *val)
  487. {
  488. int r = 0;
  489. long int i;
  490. switch (id) {
  491. case KVM_REG_PPC_MAS0:
  492. *val = get_reg_val(id, vcpu->arch.shared->mas0);
  493. break;
  494. case KVM_REG_PPC_MAS1:
  495. *val = get_reg_val(id, vcpu->arch.shared->mas1);
  496. break;
  497. case KVM_REG_PPC_MAS2:
  498. *val = get_reg_val(id, vcpu->arch.shared->mas2);
  499. break;
  500. case KVM_REG_PPC_MAS7_3:
  501. *val = get_reg_val(id, vcpu->arch.shared->mas7_3);
  502. break;
  503. case KVM_REG_PPC_MAS4:
  504. *val = get_reg_val(id, vcpu->arch.shared->mas4);
  505. break;
  506. case KVM_REG_PPC_MAS6:
  507. *val = get_reg_val(id, vcpu->arch.shared->mas6);
  508. break;
  509. case KVM_REG_PPC_MMUCFG:
  510. *val = get_reg_val(id, vcpu->arch.mmucfg);
  511. break;
  512. case KVM_REG_PPC_EPTCFG:
  513. *val = get_reg_val(id, vcpu->arch.eptcfg);
  514. break;
  515. case KVM_REG_PPC_TLB0CFG:
  516. case KVM_REG_PPC_TLB1CFG:
  517. case KVM_REG_PPC_TLB2CFG:
  518. case KVM_REG_PPC_TLB3CFG:
  519. i = id - KVM_REG_PPC_TLB0CFG;
  520. *val = get_reg_val(id, vcpu->arch.tlbcfg[i]);
  521. break;
  522. case KVM_REG_PPC_TLB0PS:
  523. case KVM_REG_PPC_TLB1PS:
  524. case KVM_REG_PPC_TLB2PS:
  525. case KVM_REG_PPC_TLB3PS:
  526. i = id - KVM_REG_PPC_TLB0PS;
  527. *val = get_reg_val(id, vcpu->arch.tlbps[i]);
  528. break;
  529. default:
  530. r = -EINVAL;
  531. break;
  532. }
  533. return r;
  534. }
  535. int kvmppc_set_one_reg_e500_tlb(struct kvm_vcpu *vcpu, u64 id,
  536. union kvmppc_one_reg *val)
  537. {
  538. int r = 0;
  539. long int i;
  540. switch (id) {
  541. case KVM_REG_PPC_MAS0:
  542. vcpu->arch.shared->mas0 = set_reg_val(id, *val);
  543. break;
  544. case KVM_REG_PPC_MAS1:
  545. vcpu->arch.shared->mas1 = set_reg_val(id, *val);
  546. break;
  547. case KVM_REG_PPC_MAS2:
  548. vcpu->arch.shared->mas2 = set_reg_val(id, *val);
  549. break;
  550. case KVM_REG_PPC_MAS7_3:
  551. vcpu->arch.shared->mas7_3 = set_reg_val(id, *val);
  552. break;
  553. case KVM_REG_PPC_MAS4:
  554. vcpu->arch.shared->mas4 = set_reg_val(id, *val);
  555. break;
  556. case KVM_REG_PPC_MAS6:
  557. vcpu->arch.shared->mas6 = set_reg_val(id, *val);
  558. break;
  559. /* Only allow MMU registers to be set to the config supported by KVM */
  560. case KVM_REG_PPC_MMUCFG: {
  561. u32 reg = set_reg_val(id, *val);
  562. if (reg != vcpu->arch.mmucfg)
  563. r = -EINVAL;
  564. break;
  565. }
  566. case KVM_REG_PPC_EPTCFG: {
  567. u32 reg = set_reg_val(id, *val);
  568. if (reg != vcpu->arch.eptcfg)
  569. r = -EINVAL;
  570. break;
  571. }
  572. case KVM_REG_PPC_TLB0CFG:
  573. case KVM_REG_PPC_TLB1CFG:
  574. case KVM_REG_PPC_TLB2CFG:
  575. case KVM_REG_PPC_TLB3CFG: {
  576. /* MMU geometry (N_ENTRY/ASSOC) can be set only using SW_TLB */
  577. u32 reg = set_reg_val(id, *val);
  578. i = id - KVM_REG_PPC_TLB0CFG;
  579. if (reg != vcpu->arch.tlbcfg[i])
  580. r = -EINVAL;
  581. break;
  582. }
  583. case KVM_REG_PPC_TLB0PS:
  584. case KVM_REG_PPC_TLB1PS:
  585. case KVM_REG_PPC_TLB2PS:
  586. case KVM_REG_PPC_TLB3PS: {
  587. u32 reg = set_reg_val(id, *val);
  588. i = id - KVM_REG_PPC_TLB0PS;
  589. if (reg != vcpu->arch.tlbps[i])
  590. r = -EINVAL;
  591. break;
  592. }
  593. default:
  594. r = -EINVAL;
  595. break;
  596. }
  597. return r;
  598. }
  599. static int vcpu_mmu_geometry_update(struct kvm_vcpu *vcpu,
  600. struct kvm_book3e_206_tlb_params *params)
  601. {
  602. vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  603. if (params->tlb_sizes[0] <= 2048)
  604. vcpu->arch.tlbcfg[0] |= params->tlb_sizes[0];
  605. vcpu->arch.tlbcfg[0] |= params->tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
  606. vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  607. vcpu->arch.tlbcfg[1] |= params->tlb_sizes[1];
  608. vcpu->arch.tlbcfg[1] |= params->tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
  609. return 0;
  610. }
  611. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  612. struct kvm_config_tlb *cfg)
  613. {
  614. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  615. struct kvm_book3e_206_tlb_params params;
  616. char *virt;
  617. struct page **pages;
  618. struct tlbe_priv *privs[2] = {};
  619. u64 *g2h_bitmap = NULL;
  620. size_t array_len;
  621. u32 sets;
  622. int num_pages, ret, i;
  623. if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
  624. return -EINVAL;
  625. if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
  626. sizeof(params)))
  627. return -EFAULT;
  628. if (params.tlb_sizes[1] > 64)
  629. return -EINVAL;
  630. if (params.tlb_ways[1] != params.tlb_sizes[1])
  631. return -EINVAL;
  632. if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
  633. return -EINVAL;
  634. if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
  635. return -EINVAL;
  636. if (!is_power_of_2(params.tlb_ways[0]))
  637. return -EINVAL;
  638. sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
  639. if (!is_power_of_2(sets))
  640. return -EINVAL;
  641. array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
  642. array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
  643. if (cfg->array_len < array_len)
  644. return -EINVAL;
  645. num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
  646. cfg->array / PAGE_SIZE;
  647. pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  648. if (!pages)
  649. return -ENOMEM;
  650. ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
  651. if (ret < 0)
  652. goto err_pages;
  653. if (ret != num_pages) {
  654. num_pages = ret;
  655. ret = -EFAULT;
  656. goto err_put_page;
  657. }
  658. virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
  659. if (!virt) {
  660. ret = -ENOMEM;
  661. goto err_put_page;
  662. }
  663. privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
  664. GFP_KERNEL);
  665. privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
  666. GFP_KERNEL);
  667. if (!privs[0] || !privs[1]) {
  668. ret = -ENOMEM;
  669. goto err_privs;
  670. }
  671. g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
  672. GFP_KERNEL);
  673. if (!g2h_bitmap) {
  674. ret = -ENOMEM;
  675. goto err_privs;
  676. }
  677. free_gtlb(vcpu_e500);
  678. vcpu_e500->gtlb_priv[0] = privs[0];
  679. vcpu_e500->gtlb_priv[1] = privs[1];
  680. vcpu_e500->g2h_tlb1_map = g2h_bitmap;
  681. vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
  682. (virt + (cfg->array & (PAGE_SIZE - 1)));
  683. vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
  684. vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
  685. vcpu_e500->gtlb_offset[0] = 0;
  686. vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
  687. /* Update vcpu's MMU geometry based on SW_TLB input */
  688. vcpu_mmu_geometry_update(vcpu, &params);
  689. vcpu_e500->shared_tlb_pages = pages;
  690. vcpu_e500->num_shared_tlb_pages = num_pages;
  691. vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
  692. vcpu_e500->gtlb_params[0].sets = sets;
  693. vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
  694. vcpu_e500->gtlb_params[1].sets = 1;
  695. kvmppc_recalc_tlb1map_range(vcpu_e500);
  696. return 0;
  697. err_privs:
  698. kfree(privs[0]);
  699. kfree(privs[1]);
  700. err_put_page:
  701. for (i = 0; i < num_pages; i++)
  702. put_page(pages[i]);
  703. err_pages:
  704. kfree(pages);
  705. return ret;
  706. }
  707. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  708. struct kvm_dirty_tlb *dirty)
  709. {
  710. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  711. kvmppc_recalc_tlb1map_range(vcpu_e500);
  712. kvmppc_core_flush_tlb(vcpu);
  713. return 0;
  714. }
  715. /* Vcpu's MMU default configuration */
  716. static int vcpu_mmu_init(struct kvm_vcpu *vcpu,
  717. struct kvmppc_e500_tlb_params *params)
  718. {
  719. /* Initialize RASIZE, PIDSIZE, NTLBS and MAVN fields with host values*/
  720. vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
  721. /* Initialize TLBnCFG fields with host values and SW_TLB geometry*/
  722. vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
  723. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  724. vcpu->arch.tlbcfg[0] |= params[0].entries;
  725. vcpu->arch.tlbcfg[0] |= params[0].ways << TLBnCFG_ASSOC_SHIFT;
  726. vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
  727. ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
  728. vcpu->arch.tlbcfg[1] |= params[1].entries;
  729. vcpu->arch.tlbcfg[1] |= params[1].ways << TLBnCFG_ASSOC_SHIFT;
  730. if (has_feature(vcpu, VCPU_FTR_MMU_V2)) {
  731. vcpu->arch.tlbps[0] = mfspr(SPRN_TLB0PS);
  732. vcpu->arch.tlbps[1] = mfspr(SPRN_TLB1PS);
  733. vcpu->arch.mmucfg &= ~MMUCFG_LRAT;
  734. /* Guest mmu emulation currently doesn't handle E.PT */
  735. vcpu->arch.eptcfg = 0;
  736. vcpu->arch.tlbcfg[0] &= ~TLBnCFG_PT;
  737. vcpu->arch.tlbcfg[1] &= ~TLBnCFG_IND;
  738. }
  739. return 0;
  740. }
  741. int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
  742. {
  743. struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
  744. int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
  745. int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
  746. if (e500_mmu_host_init(vcpu_e500))
  747. goto err;
  748. vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
  749. vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
  750. vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
  751. vcpu_e500->gtlb_params[0].sets =
  752. KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
  753. vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
  754. vcpu_e500->gtlb_params[1].sets = 1;
  755. vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
  756. if (!vcpu_e500->gtlb_arch)
  757. return -ENOMEM;
  758. vcpu_e500->gtlb_offset[0] = 0;
  759. vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
  760. vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
  761. vcpu_e500->gtlb_params[0].entries,
  762. GFP_KERNEL);
  763. if (!vcpu_e500->gtlb_priv[0])
  764. goto err;
  765. vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
  766. vcpu_e500->gtlb_params[1].entries,
  767. GFP_KERNEL);
  768. if (!vcpu_e500->gtlb_priv[1])
  769. goto err;
  770. vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(u64) *
  771. vcpu_e500->gtlb_params[1].entries,
  772. GFP_KERNEL);
  773. if (!vcpu_e500->g2h_tlb1_map)
  774. goto err;
  775. vcpu_mmu_init(vcpu, vcpu_e500->gtlb_params);
  776. kvmppc_recalc_tlb1map_range(vcpu_e500);
  777. return 0;
  778. err:
  779. free_gtlb(vcpu_e500);
  780. return -1;
  781. }
  782. void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
  783. {
  784. free_gtlb(vcpu_e500);
  785. e500_mmu_host_uninit(vcpu_e500);
  786. }