e500mc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (C) 2010 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 <asm/reg.h>
  19. #include <asm/cputable.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/kvm_ppc.h>
  22. #include <asm/dbell.h>
  23. #include "booke.h"
  24. #include "e500.h"
  25. void kvmppc_set_pending_interrupt(struct kvm_vcpu *vcpu, enum int_class type)
  26. {
  27. enum ppc_dbell dbell_type;
  28. unsigned long tag;
  29. switch (type) {
  30. case INT_CLASS_NONCRIT:
  31. dbell_type = PPC_G_DBELL;
  32. break;
  33. case INT_CLASS_CRIT:
  34. dbell_type = PPC_G_DBELL_CRIT;
  35. break;
  36. case INT_CLASS_MC:
  37. dbell_type = PPC_G_DBELL_MC;
  38. break;
  39. default:
  40. WARN_ONCE(1, "%s: unknown int type %d\n", __func__, type);
  41. return;
  42. }
  43. tag = PPC_DBELL_LPID(vcpu->kvm->arch.lpid) | vcpu->vcpu_id;
  44. mb();
  45. ppc_msgsnd(dbell_type, 0, tag);
  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. u32 val, eaddr, lpid;
  53. unsigned long flags;
  54. ts = get_tlb_ts(gtlbe);
  55. tid = get_tlb_tid(gtlbe);
  56. lpid = vcpu_e500->vcpu.kvm->arch.lpid;
  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 | lpid);
  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 | vcpu_e500->vcpu.kvm->arch.lpid);
  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. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  92. {
  93. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  94. kvmppc_booke_vcpu_load(vcpu, cpu);
  95. mtspr(SPRN_LPID, vcpu->kvm->arch.lpid);
  96. mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
  97. mtspr(SPRN_GPIR, vcpu->vcpu_id);
  98. mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
  99. mtspr(SPRN_EPLC, vcpu->arch.eplc);
  100. mtspr(SPRN_EPSC, vcpu->arch.epsc);
  101. mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
  102. mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
  103. mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
  104. mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
  105. mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
  106. mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
  107. mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
  108. mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
  109. mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
  110. mtspr(SPRN_GEPR, vcpu->arch.epr);
  111. mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
  112. mtspr(SPRN_GESR, vcpu->arch.shared->esr);
  113. if (vcpu->arch.oldpir != mfspr(SPRN_PIR))
  114. kvmppc_e500_tlbil_all(vcpu_e500);
  115. kvmppc_load_guest_fp(vcpu);
  116. }
  117. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  118. {
  119. vcpu->arch.eplc = mfspr(SPRN_EPLC);
  120. vcpu->arch.epsc = mfspr(SPRN_EPSC);
  121. vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
  122. vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
  123. vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
  124. vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
  125. vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
  126. vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
  127. vcpu->arch.epr = mfspr(SPRN_GEPR);
  128. vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
  129. vcpu->arch.shared->esr = mfspr(SPRN_GESR);
  130. vcpu->arch.oldpir = mfspr(SPRN_PIR);
  131. kvmppc_booke_vcpu_put(vcpu);
  132. }
  133. int kvmppc_core_check_processor_compat(void)
  134. {
  135. int r;
  136. if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
  137. r = 0;
  138. else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
  139. r = 0;
  140. else
  141. r = -ENOTSUPP;
  142. return r;
  143. }
  144. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  145. {
  146. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  147. vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
  148. SPRN_EPCR_DUVD;
  149. vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_DEP | MSRP_PMMP;
  150. vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
  151. vcpu->arch.epsc = vcpu->arch.eplc;
  152. vcpu->arch.pvr = mfspr(SPRN_PVR);
  153. vcpu_e500->svr = mfspr(SPRN_SVR);
  154. vcpu->arch.cpu_type = KVM_CPU_E500MC;
  155. return 0;
  156. }
  157. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  158. {
  159. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  160. sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
  161. KVM_SREGS_E_PC;
  162. sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
  163. sregs->u.e.impl.fsl.features = 0;
  164. sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
  165. sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
  166. sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
  167. kvmppc_get_sregs_e500_tlb(vcpu, sregs);
  168. sregs->u.e.ivor_high[3] =
  169. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
  170. sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
  171. sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
  172. kvmppc_get_sregs_ivor(vcpu, sregs);
  173. }
  174. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  175. {
  176. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  177. int ret;
  178. if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
  179. vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
  180. vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
  181. vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
  182. }
  183. ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
  184. if (ret < 0)
  185. return ret;
  186. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  187. return 0;
  188. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  189. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  190. sregs->u.e.ivor_high[3];
  191. }
  192. if (sregs->u.e.features & KVM_SREGS_E_PC) {
  193. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
  194. sregs->u.e.ivor_high[4];
  195. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
  196. sregs->u.e.ivor_high[5];
  197. }
  198. return kvmppc_set_sregs_ivor(vcpu, sregs);
  199. }
  200. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  201. {
  202. struct kvmppc_vcpu_e500 *vcpu_e500;
  203. struct kvm_vcpu *vcpu;
  204. int err;
  205. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  206. if (!vcpu_e500) {
  207. err = -ENOMEM;
  208. goto out;
  209. }
  210. vcpu = &vcpu_e500->vcpu;
  211. /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
  212. vcpu->arch.oldpir = 0xffffffff;
  213. err = kvm_vcpu_init(vcpu, kvm, id);
  214. if (err)
  215. goto free_vcpu;
  216. err = kvmppc_e500_tlb_init(vcpu_e500);
  217. if (err)
  218. goto uninit_vcpu;
  219. vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  220. if (!vcpu->arch.shared)
  221. goto uninit_tlb;
  222. return vcpu;
  223. uninit_tlb:
  224. kvmppc_e500_tlb_uninit(vcpu_e500);
  225. uninit_vcpu:
  226. kvm_vcpu_uninit(vcpu);
  227. free_vcpu:
  228. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  229. out:
  230. return ERR_PTR(err);
  231. }
  232. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  233. {
  234. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  235. free_page((unsigned long)vcpu->arch.shared);
  236. kvmppc_e500_tlb_uninit(vcpu_e500);
  237. kvm_vcpu_uninit(vcpu);
  238. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  239. }
  240. int kvmppc_core_init_vm(struct kvm *kvm)
  241. {
  242. int lpid;
  243. lpid = kvmppc_alloc_lpid();
  244. if (lpid < 0)
  245. return lpid;
  246. kvm->arch.lpid = lpid;
  247. return 0;
  248. }
  249. void kvmppc_core_destroy_vm(struct kvm *kvm)
  250. {
  251. kvmppc_free_lpid(kvm->arch.lpid);
  252. }
  253. static int __init kvmppc_e500mc_init(void)
  254. {
  255. int r;
  256. r = kvmppc_booke_init();
  257. if (r)
  258. return r;
  259. kvmppc_init_lpid(64);
  260. kvmppc_claim_lpid(0); /* host */
  261. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  262. }
  263. static void __exit kvmppc_e500mc_exit(void)
  264. {
  265. kvmppc_booke_exit();
  266. }
  267. module_init(kvmppc_e500mc_init);
  268. module_exit(kvmppc_e500mc_exit);