e500mc.c 10 KB

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