e500mc.c 11 KB

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