e500mc.c 11 KB

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