e500mc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright (C) 2010,2012 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Varun Sethi, <varun.sethi@freescale.com>
  5. *
  6. * Description:
  7. * This file is derived from arch/powerpc/kvm/e500.c,
  8. * by Yu Liu <yu.liu@freescale.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 <linux/miscdevice.h>
  19. #include <linux/module.h>
  20. #include <asm/reg.h>
  21. #include <asm/cputable.h>
  22. #include <asm/tlbflush.h>
  23. #include <asm/kvm_ppc.h>
  24. #include <asm/dbell.h>
  25. #include "booke.h"
  26. #include "e500.h"
  27. void kvmppc_set_pending_interrupt(struct kvm_vcpu *vcpu, enum int_class type)
  28. {
  29. enum ppc_dbell dbell_type;
  30. unsigned long tag;
  31. switch (type) {
  32. case INT_CLASS_NONCRIT:
  33. dbell_type = PPC_G_DBELL;
  34. break;
  35. case INT_CLASS_CRIT:
  36. dbell_type = PPC_G_DBELL_CRIT;
  37. break;
  38. case INT_CLASS_MC:
  39. dbell_type = PPC_G_DBELL_MC;
  40. break;
  41. default:
  42. WARN_ONCE(1, "%s: unknown int type %d\n", __func__, type);
  43. return;
  44. }
  45. preempt_disable();
  46. tag = PPC_DBELL_LPID(get_lpid(vcpu)) | vcpu->vcpu_id;
  47. mb();
  48. ppc_msgsnd(dbell_type, 0, tag);
  49. preempt_enable();
  50. }
  51. /* gtlbe must not be mapped by more than one host tlb entry */
  52. void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
  53. struct kvm_book3e_206_tlb_entry *gtlbe)
  54. {
  55. unsigned int tid, ts;
  56. gva_t eaddr;
  57. u32 val;
  58. unsigned long flags;
  59. ts = get_tlb_ts(gtlbe);
  60. tid = get_tlb_tid(gtlbe);
  61. /* We search the host TLB to invalidate its shadow TLB entry */
  62. val = (tid << 16) | ts;
  63. eaddr = get_tlb_eaddr(gtlbe);
  64. local_irq_save(flags);
  65. mtspr(SPRN_MAS6, val);
  66. mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu));
  67. asm volatile("tlbsx 0, %[eaddr]\n" : : [eaddr] "r" (eaddr));
  68. val = mfspr(SPRN_MAS1);
  69. if (val & MAS1_VALID) {
  70. mtspr(SPRN_MAS1, val & ~MAS1_VALID);
  71. asm volatile("tlbwe");
  72. }
  73. mtspr(SPRN_MAS5, 0);
  74. /* NOTE: tlbsx also updates mas8, so clear it for host tlbwe */
  75. mtspr(SPRN_MAS8, 0);
  76. isync();
  77. local_irq_restore(flags);
  78. }
  79. void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500)
  80. {
  81. unsigned long flags;
  82. local_irq_save(flags);
  83. mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu));
  84. asm volatile("tlbilxlpid");
  85. mtspr(SPRN_MAS5, 0);
  86. local_irq_restore(flags);
  87. }
  88. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
  89. {
  90. vcpu->arch.pid = pid;
  91. }
  92. void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
  93. {
  94. }
  95. /* We use two lpids per VM */
  96. static DEFINE_PER_CPU(struct kvm_vcpu *[KVMPPC_NR_LPIDS], last_vcpu_of_lpid);
  97. static void kvmppc_core_vcpu_load_e500mc(struct kvm_vcpu *vcpu, int cpu)
  98. {
  99. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  100. kvmppc_booke_vcpu_load(vcpu, cpu);
  101. mtspr(SPRN_LPID, get_lpid(vcpu));
  102. mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
  103. mtspr(SPRN_GPIR, vcpu->vcpu_id);
  104. mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
  105. vcpu->arch.eplc = EPC_EGS | (get_lpid(vcpu) << EPC_ELPID_SHIFT);
  106. vcpu->arch.epsc = vcpu->arch.eplc;
  107. mtspr(SPRN_EPLC, vcpu->arch.eplc);
  108. mtspr(SPRN_EPSC, vcpu->arch.epsc);
  109. mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
  110. mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
  111. mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
  112. mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
  113. mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
  114. mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
  115. mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
  116. mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
  117. mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
  118. mtspr(SPRN_GEPR, vcpu->arch.epr);
  119. mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
  120. mtspr(SPRN_GESR, vcpu->arch.shared->esr);
  121. if (vcpu->arch.oldpir != mfspr(SPRN_PIR) ||
  122. __this_cpu_read(last_vcpu_of_lpid[get_lpid(vcpu)]) != vcpu) {
  123. kvmppc_e500_tlbil_all(vcpu_e500);
  124. __this_cpu_write(last_vcpu_of_lpid[get_lpid(vcpu)], vcpu);
  125. }
  126. }
  127. static void kvmppc_core_vcpu_put_e500mc(struct kvm_vcpu *vcpu)
  128. {
  129. vcpu->arch.eplc = mfspr(SPRN_EPLC);
  130. vcpu->arch.epsc = mfspr(SPRN_EPSC);
  131. vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
  132. vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
  133. vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
  134. vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
  135. vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
  136. vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
  137. vcpu->arch.epr = mfspr(SPRN_GEPR);
  138. vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
  139. vcpu->arch.shared->esr = mfspr(SPRN_GESR);
  140. vcpu->arch.oldpir = mfspr(SPRN_PIR);
  141. kvmppc_booke_vcpu_put(vcpu);
  142. }
  143. int kvmppc_core_check_processor_compat(void)
  144. {
  145. int r;
  146. if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
  147. r = 0;
  148. else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
  149. r = 0;
  150. #ifdef CONFIG_ALTIVEC
  151. /*
  152. * Since guests have the priviledge to enable AltiVec, we need AltiVec
  153. * support in the host to save/restore their context.
  154. * Don't use CPU_FTR_ALTIVEC to identify cores with AltiVec unit
  155. * because it's cleared in the absence of CONFIG_ALTIVEC!
  156. */
  157. else if (strcmp(cur_cpu_spec->cpu_name, "e6500") == 0)
  158. r = 0;
  159. #endif
  160. else
  161. r = -ENOTSUPP;
  162. return r;
  163. }
  164. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  165. {
  166. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  167. vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
  168. SPRN_EPCR_DUVD;
  169. #ifdef CONFIG_64BIT
  170. vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
  171. #endif
  172. vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_PMMP;
  173. vcpu->arch.pvr = mfspr(SPRN_PVR);
  174. vcpu_e500->svr = mfspr(SPRN_SVR);
  175. vcpu->arch.cpu_type = KVM_CPU_E500MC;
  176. return 0;
  177. }
  178. static int kvmppc_core_get_sregs_e500mc(struct kvm_vcpu *vcpu,
  179. struct kvm_sregs *sregs)
  180. {
  181. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  182. sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
  183. KVM_SREGS_E_PC;
  184. sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
  185. sregs->u.e.impl.fsl.features = 0;
  186. sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
  187. sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
  188. sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
  189. kvmppc_get_sregs_e500_tlb(vcpu, sregs);
  190. sregs->u.e.ivor_high[3] =
  191. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
  192. sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
  193. sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
  194. return kvmppc_get_sregs_ivor(vcpu, sregs);
  195. }
  196. static int kvmppc_core_set_sregs_e500mc(struct kvm_vcpu *vcpu,
  197. struct kvm_sregs *sregs)
  198. {
  199. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  200. int ret;
  201. if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
  202. vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
  203. vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
  204. vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
  205. }
  206. ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
  207. if (ret < 0)
  208. return ret;
  209. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  210. return 0;
  211. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  212. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  213. sregs->u.e.ivor_high[3];
  214. }
  215. if (sregs->u.e.features & KVM_SREGS_E_PC) {
  216. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
  217. sregs->u.e.ivor_high[4];
  218. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
  219. sregs->u.e.ivor_high[5];
  220. }
  221. return kvmppc_set_sregs_ivor(vcpu, sregs);
  222. }
  223. static int kvmppc_get_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
  224. union kvmppc_one_reg *val)
  225. {
  226. int r = 0;
  227. switch (id) {
  228. case KVM_REG_PPC_SPRG9:
  229. *val = get_reg_val(id, vcpu->arch.sprg9);
  230. break;
  231. default:
  232. r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
  233. }
  234. return r;
  235. }
  236. static int kvmppc_set_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
  237. union kvmppc_one_reg *val)
  238. {
  239. int r = 0;
  240. switch (id) {
  241. case KVM_REG_PPC_SPRG9:
  242. vcpu->arch.sprg9 = set_reg_val(id, *val);
  243. break;
  244. default:
  245. r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
  246. }
  247. return r;
  248. }
  249. static struct kvm_vcpu *kvmppc_core_vcpu_create_e500mc(struct kvm *kvm,
  250. unsigned int id)
  251. {
  252. struct kvmppc_vcpu_e500 *vcpu_e500;
  253. struct kvm_vcpu *vcpu;
  254. int err;
  255. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  256. if (!vcpu_e500) {
  257. err = -ENOMEM;
  258. goto out;
  259. }
  260. vcpu = &vcpu_e500->vcpu;
  261. /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
  262. vcpu->arch.oldpir = 0xffffffff;
  263. err = kvm_vcpu_init(vcpu, kvm, id);
  264. if (err)
  265. goto free_vcpu;
  266. err = kvmppc_e500_tlb_init(vcpu_e500);
  267. if (err)
  268. goto uninit_vcpu;
  269. vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  270. if (!vcpu->arch.shared)
  271. goto uninit_tlb;
  272. return vcpu;
  273. uninit_tlb:
  274. kvmppc_e500_tlb_uninit(vcpu_e500);
  275. uninit_vcpu:
  276. kvm_vcpu_uninit(vcpu);
  277. free_vcpu:
  278. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  279. out:
  280. return ERR_PTR(err);
  281. }
  282. static void kvmppc_core_vcpu_free_e500mc(struct kvm_vcpu *vcpu)
  283. {
  284. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  285. free_page((unsigned long)vcpu->arch.shared);
  286. kvmppc_e500_tlb_uninit(vcpu_e500);
  287. kvm_vcpu_uninit(vcpu);
  288. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  289. }
  290. static int kvmppc_core_init_vm_e500mc(struct kvm *kvm)
  291. {
  292. int lpid;
  293. lpid = kvmppc_alloc_lpid();
  294. if (lpid < 0)
  295. return lpid;
  296. /*
  297. * Use two lpids per VM on cores with two threads like e6500. Use
  298. * even numbers to speedup vcpu lpid computation with consecutive lpids
  299. * per VM. vm1 will use lpids 2 and 3, vm2 lpids 4 and 5, and so on.
  300. */
  301. if (threads_per_core == 2)
  302. lpid <<= 1;
  303. kvm->arch.lpid = lpid;
  304. return 0;
  305. }
  306. static void kvmppc_core_destroy_vm_e500mc(struct kvm *kvm)
  307. {
  308. int lpid = kvm->arch.lpid;
  309. if (threads_per_core == 2)
  310. lpid >>= 1;
  311. kvmppc_free_lpid(lpid);
  312. }
  313. static struct kvmppc_ops kvm_ops_e500mc = {
  314. .get_sregs = kvmppc_core_get_sregs_e500mc,
  315. .set_sregs = kvmppc_core_set_sregs_e500mc,
  316. .get_one_reg = kvmppc_get_one_reg_e500mc,
  317. .set_one_reg = kvmppc_set_one_reg_e500mc,
  318. .vcpu_load = kvmppc_core_vcpu_load_e500mc,
  319. .vcpu_put = kvmppc_core_vcpu_put_e500mc,
  320. .vcpu_create = kvmppc_core_vcpu_create_e500mc,
  321. .vcpu_free = kvmppc_core_vcpu_free_e500mc,
  322. .mmu_destroy = kvmppc_mmu_destroy_e500,
  323. .init_vm = kvmppc_core_init_vm_e500mc,
  324. .destroy_vm = kvmppc_core_destroy_vm_e500mc,
  325. .emulate_op = kvmppc_core_emulate_op_e500,
  326. .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
  327. .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
  328. };
  329. static int __init kvmppc_e500mc_init(void)
  330. {
  331. int r;
  332. r = kvmppc_booke_init();
  333. if (r)
  334. goto err_out;
  335. /*
  336. * Use two lpids per VM on dual threaded processors like e6500
  337. * to workarround the lack of tlb write conditional instruction.
  338. * Expose half the number of available hardware lpids to the lpid
  339. * allocator.
  340. */
  341. kvmppc_init_lpid(KVMPPC_NR_LPIDS/threads_per_core);
  342. kvmppc_claim_lpid(0); /* host */
  343. r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  344. if (r)
  345. goto err_out;
  346. kvm_ops_e500mc.owner = THIS_MODULE;
  347. kvmppc_pr_ops = &kvm_ops_e500mc;
  348. err_out:
  349. return r;
  350. }
  351. static void __exit kvmppc_e500mc_exit(void)
  352. {
  353. kvmppc_pr_ops = NULL;
  354. kvmppc_booke_exit();
  355. }
  356. module_init(kvmppc_e500mc_init);
  357. module_exit(kvmppc_e500mc_exit);
  358. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  359. MODULE_ALIAS("devname:kvm");