e500.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 derived from arch/powerpc/kvm/44x.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/kvm_host.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/export.h>
  18. #include <asm/reg.h>
  19. #include <asm/cputable.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/kvm_ppc.h>
  22. #include "../mm/mmu_decl.h"
  23. #include "booke.h"
  24. #include "e500.h"
  25. struct id {
  26. unsigned long val;
  27. struct id **pentry;
  28. };
  29. #define NUM_TIDS 256
  30. /*
  31. * This table provide mappings from:
  32. * (guestAS,guestTID,guestPR) --> ID of physical cpu
  33. * guestAS [0..1]
  34. * guestTID [0..255]
  35. * guestPR [0..1]
  36. * ID [1..255]
  37. * Each vcpu keeps one vcpu_id_table.
  38. */
  39. struct vcpu_id_table {
  40. struct id id[2][NUM_TIDS][2];
  41. };
  42. /*
  43. * This table provide reversed mappings of vcpu_id_table:
  44. * ID --> address of vcpu_id_table item.
  45. * Each physical core has one pcpu_id_table.
  46. */
  47. struct pcpu_id_table {
  48. struct id *entry[NUM_TIDS];
  49. };
  50. static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids);
  51. /* This variable keeps last used shadow ID on local core.
  52. * The valid range of shadow ID is [1..255] */
  53. static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid);
  54. /*
  55. * Allocate a free shadow id and setup a valid sid mapping in given entry.
  56. * A mapping is only valid when vcpu_id_table and pcpu_id_table are match.
  57. *
  58. * The caller must have preemption disabled, and keep it that way until
  59. * it has finished with the returned shadow id (either written into the
  60. * TLB or arch.shadow_pid, or discarded).
  61. */
  62. static inline int local_sid_setup_one(struct id *entry)
  63. {
  64. unsigned long sid;
  65. int ret = -1;
  66. sid = ++(__get_cpu_var(pcpu_last_used_sid));
  67. if (sid < NUM_TIDS) {
  68. __get_cpu_var(pcpu_sids).entry[sid] = entry;
  69. entry->val = sid;
  70. entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid];
  71. ret = sid;
  72. }
  73. /*
  74. * If sid == NUM_TIDS, we've run out of sids. We return -1, and
  75. * the caller will invalidate everything and start over.
  76. *
  77. * sid > NUM_TIDS indicates a race, which we disable preemption to
  78. * avoid.
  79. */
  80. WARN_ON(sid > NUM_TIDS);
  81. return ret;
  82. }
  83. /*
  84. * Check if given entry contain a valid shadow id mapping.
  85. * An ID mapping is considered valid only if
  86. * both vcpu and pcpu know this mapping.
  87. *
  88. * The caller must have preemption disabled, and keep it that way until
  89. * it has finished with the returned shadow id (either written into the
  90. * TLB or arch.shadow_pid, or discarded).
  91. */
  92. static inline int local_sid_lookup(struct id *entry)
  93. {
  94. if (entry && entry->val != 0 &&
  95. __get_cpu_var(pcpu_sids).entry[entry->val] == entry &&
  96. entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val])
  97. return entry->val;
  98. return -1;
  99. }
  100. /* Invalidate all id mappings on local core -- call with preempt disabled */
  101. static inline void local_sid_destroy_all(void)
  102. {
  103. __get_cpu_var(pcpu_last_used_sid) = 0;
  104. memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids)));
  105. }
  106. static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500)
  107. {
  108. vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL);
  109. return vcpu_e500->idt;
  110. }
  111. static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500)
  112. {
  113. kfree(vcpu_e500->idt);
  114. vcpu_e500->idt = NULL;
  115. }
  116. /* Map guest pid to shadow.
  117. * We use PID to keep shadow of current guest non-zero PID,
  118. * and use PID1 to keep shadow of guest zero PID.
  119. * So that guest tlbe with TID=0 can be accessed at any time */
  120. static void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500)
  121. {
  122. preempt_disable();
  123. vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500,
  124. get_cur_as(&vcpu_e500->vcpu),
  125. get_cur_pid(&vcpu_e500->vcpu),
  126. get_cur_pr(&vcpu_e500->vcpu), 1);
  127. vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500,
  128. get_cur_as(&vcpu_e500->vcpu), 0,
  129. get_cur_pr(&vcpu_e500->vcpu), 1);
  130. preempt_enable();
  131. }
  132. /* Invalidate all mappings on vcpu */
  133. static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500)
  134. {
  135. memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table));
  136. /* Update shadow pid when mappings are changed */
  137. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  138. }
  139. /* Invalidate one ID mapping on vcpu */
  140. static inline void kvmppc_e500_id_table_reset_one(
  141. struct kvmppc_vcpu_e500 *vcpu_e500,
  142. int as, int pid, int pr)
  143. {
  144. struct vcpu_id_table *idt = vcpu_e500->idt;
  145. BUG_ON(as >= 2);
  146. BUG_ON(pid >= NUM_TIDS);
  147. BUG_ON(pr >= 2);
  148. idt->id[as][pid][pr].val = 0;
  149. idt->id[as][pid][pr].pentry = NULL;
  150. /* Update shadow pid when mappings are changed */
  151. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  152. }
  153. /*
  154. * Map guest (vcpu,AS,ID,PR) to physical core shadow id.
  155. * This function first lookup if a valid mapping exists,
  156. * if not, then creates a new one.
  157. *
  158. * The caller must have preemption disabled, and keep it that way until
  159. * it has finished with the returned shadow id (either written into the
  160. * TLB or arch.shadow_pid, or discarded).
  161. */
  162. unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500,
  163. unsigned int as, unsigned int gid,
  164. unsigned int pr, int avoid_recursion)
  165. {
  166. struct vcpu_id_table *idt = vcpu_e500->idt;
  167. int sid;
  168. BUG_ON(as >= 2);
  169. BUG_ON(gid >= NUM_TIDS);
  170. BUG_ON(pr >= 2);
  171. sid = local_sid_lookup(&idt->id[as][gid][pr]);
  172. while (sid <= 0) {
  173. /* No mapping yet */
  174. sid = local_sid_setup_one(&idt->id[as][gid][pr]);
  175. if (sid <= 0) {
  176. _tlbil_all();
  177. local_sid_destroy_all();
  178. }
  179. /* Update shadow pid when mappings are changed */
  180. if (!avoid_recursion)
  181. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  182. }
  183. return sid;
  184. }
  185. unsigned int kvmppc_e500_get_tlb_stid(struct kvm_vcpu *vcpu,
  186. struct kvm_book3e_206_tlb_entry *gtlbe)
  187. {
  188. return kvmppc_e500_get_sid(to_e500(vcpu), get_tlb_ts(gtlbe),
  189. get_tlb_tid(gtlbe), get_cur_pr(vcpu), 0);
  190. }
  191. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
  192. {
  193. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  194. if (vcpu->arch.pid != pid) {
  195. vcpu_e500->pid[0] = vcpu->arch.pid = pid;
  196. kvmppc_e500_recalc_shadow_pid(vcpu_e500);
  197. }
  198. }
  199. /* gtlbe must not be mapped by more than one host tlbe */
  200. void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
  201. struct kvm_book3e_206_tlb_entry *gtlbe)
  202. {
  203. struct vcpu_id_table *idt = vcpu_e500->idt;
  204. unsigned int pr, tid, ts, pid;
  205. u32 val, eaddr;
  206. unsigned long flags;
  207. ts = get_tlb_ts(gtlbe);
  208. tid = get_tlb_tid(gtlbe);
  209. preempt_disable();
  210. /* One guest ID may be mapped to two shadow IDs */
  211. for (pr = 0; pr < 2; pr++) {
  212. /*
  213. * The shadow PID can have a valid mapping on at most one
  214. * host CPU. In the common case, it will be valid on this
  215. * CPU, in which case we do a local invalidation of the
  216. * specific address.
  217. *
  218. * If the shadow PID is not valid on the current host CPU,
  219. * we invalidate the entire shadow PID.
  220. */
  221. pid = local_sid_lookup(&idt->id[ts][tid][pr]);
  222. if (pid <= 0) {
  223. kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr);
  224. continue;
  225. }
  226. /*
  227. * The guest is invalidating a 4K entry which is in a PID
  228. * that has a valid shadow mapping on this host CPU. We
  229. * search host TLB to invalidate it's shadow TLB entry,
  230. * similar to __tlbil_va except that we need to look in AS1.
  231. */
  232. val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS;
  233. eaddr = get_tlb_eaddr(gtlbe);
  234. local_irq_save(flags);
  235. mtspr(SPRN_MAS6, val);
  236. asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr));
  237. val = mfspr(SPRN_MAS1);
  238. if (val & MAS1_VALID) {
  239. mtspr(SPRN_MAS1, val & ~MAS1_VALID);
  240. asm volatile("tlbwe");
  241. }
  242. local_irq_restore(flags);
  243. }
  244. preempt_enable();
  245. }
  246. void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500)
  247. {
  248. kvmppc_e500_id_table_reset_all(vcpu_e500);
  249. }
  250. void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
  251. {
  252. /* Recalc shadow pid since MSR changes */
  253. kvmppc_e500_recalc_shadow_pid(to_e500(vcpu));
  254. }
  255. void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
  256. {
  257. }
  258. void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
  259. {
  260. }
  261. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  262. {
  263. kvmppc_booke_vcpu_load(vcpu, cpu);
  264. /* Shadow PID may be expired on local core */
  265. kvmppc_e500_recalc_shadow_pid(to_e500(vcpu));
  266. }
  267. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  268. {
  269. #ifdef CONFIG_SPE
  270. if (vcpu->arch.shadow_msr & MSR_SPE)
  271. kvmppc_vcpu_disable_spe(vcpu);
  272. #endif
  273. kvmppc_booke_vcpu_put(vcpu);
  274. }
  275. int kvmppc_core_check_processor_compat(void)
  276. {
  277. int r;
  278. if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
  279. r = 0;
  280. else
  281. r = -ENOTSUPP;
  282. return r;
  283. }
  284. static void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
  285. {
  286. struct kvm_book3e_206_tlb_entry *tlbe;
  287. /* Insert large initial mapping for guest. */
  288. tlbe = get_entry(vcpu_e500, 1, 0);
  289. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
  290. tlbe->mas2 = 0;
  291. tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK;
  292. /* 4K map for serial output. Used by kernel wrapper. */
  293. tlbe = get_entry(vcpu_e500, 1, 1);
  294. tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  295. tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
  296. tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
  297. }
  298. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  299. {
  300. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  301. kvmppc_e500_tlb_setup(vcpu_e500);
  302. /* Registers init */
  303. vcpu->arch.pvr = mfspr(SPRN_PVR);
  304. vcpu_e500->svr = mfspr(SPRN_SVR);
  305. vcpu->arch.cpu_type = KVM_CPU_E500V2;
  306. return 0;
  307. }
  308. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  309. {
  310. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  311. sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
  312. KVM_SREGS_E_PM;
  313. sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
  314. sregs->u.e.impl.fsl.features = 0;
  315. sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
  316. sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
  317. sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
  318. sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
  319. sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
  320. sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
  321. sregs->u.e.ivor_high[3] =
  322. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
  323. kvmppc_get_sregs_ivor(vcpu, sregs);
  324. kvmppc_get_sregs_e500_tlb(vcpu, sregs);
  325. }
  326. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  327. {
  328. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  329. int ret;
  330. if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
  331. vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
  332. vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
  333. vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
  334. }
  335. ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
  336. if (ret < 0)
  337. return ret;
  338. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  339. return 0;
  340. if (sregs->u.e.features & KVM_SREGS_E_SPE) {
  341. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
  342. sregs->u.e.ivor_high[0];
  343. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
  344. sregs->u.e.ivor_high[1];
  345. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
  346. sregs->u.e.ivor_high[2];
  347. }
  348. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  349. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  350. sregs->u.e.ivor_high[3];
  351. }
  352. return kvmppc_set_sregs_ivor(vcpu, sregs);
  353. }
  354. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  355. {
  356. struct kvmppc_vcpu_e500 *vcpu_e500;
  357. struct kvm_vcpu *vcpu;
  358. int err;
  359. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  360. if (!vcpu_e500) {
  361. err = -ENOMEM;
  362. goto out;
  363. }
  364. vcpu = &vcpu_e500->vcpu;
  365. err = kvm_vcpu_init(vcpu, kvm, id);
  366. if (err)
  367. goto free_vcpu;
  368. if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
  369. goto uninit_vcpu;
  370. err = kvmppc_e500_tlb_init(vcpu_e500);
  371. if (err)
  372. goto uninit_id;
  373. vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  374. if (!vcpu->arch.shared)
  375. goto uninit_tlb;
  376. return vcpu;
  377. uninit_tlb:
  378. kvmppc_e500_tlb_uninit(vcpu_e500);
  379. uninit_id:
  380. kvmppc_e500_id_table_free(vcpu_e500);
  381. uninit_vcpu:
  382. kvm_vcpu_uninit(vcpu);
  383. free_vcpu:
  384. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  385. out:
  386. return ERR_PTR(err);
  387. }
  388. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  389. {
  390. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  391. free_page((unsigned long)vcpu->arch.shared);
  392. kvmppc_e500_tlb_uninit(vcpu_e500);
  393. kvmppc_e500_id_table_free(vcpu_e500);
  394. kvm_vcpu_uninit(vcpu);
  395. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  396. }
  397. int kvmppc_core_init_vm(struct kvm *kvm)
  398. {
  399. return 0;
  400. }
  401. void kvmppc_core_destroy_vm(struct kvm *kvm)
  402. {
  403. }
  404. static int __init kvmppc_e500_init(void)
  405. {
  406. int r, i;
  407. unsigned long ivor[3];
  408. unsigned long max_ivor = 0;
  409. r = kvmppc_core_check_processor_compat();
  410. if (r)
  411. return r;
  412. r = kvmppc_booke_init();
  413. if (r)
  414. return r;
  415. /* copy extra E500 exception handlers */
  416. ivor[0] = mfspr(SPRN_IVOR32);
  417. ivor[1] = mfspr(SPRN_IVOR33);
  418. ivor[2] = mfspr(SPRN_IVOR34);
  419. for (i = 0; i < 3; i++) {
  420. if (ivor[i] > max_ivor)
  421. max_ivor = ivor[i];
  422. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  423. kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
  424. kvmppc_handler_len);
  425. }
  426. flush_icache_range(kvmppc_booke_handlers,
  427. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  428. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  429. }
  430. static void __exit kvmppc_e500_exit(void)
  431. {
  432. kvmppc_booke_exit();
  433. }
  434. module_init(kvmppc_e500_init);
  435. module_exit(kvmppc_e500_exit);