e500mc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 <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. gva_t eaddr;
  53. u32 val, lpid;
  54. unsigned long flags;
  55. ts = get_tlb_ts(gtlbe);
  56. tid = get_tlb_tid(gtlbe);
  57. lpid = vcpu_e500->vcpu.kvm->arch.lpid;
  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 | lpid);
  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 | vcpu_e500->vcpu.kvm->arch.lpid);
  81. asm volatile("tlbilxlpid");
  82. mtspr(SPRN_MAS5, 0);
  83. local_irq_restore(flags);
  84. }
  85. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
  86. {
  87. vcpu->arch.pid = pid;
  88. }
  89. void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
  90. {
  91. }
  92. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  93. {
  94. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  95. kvmppc_booke_vcpu_load(vcpu, cpu);
  96. mtspr(SPRN_LPID, vcpu->kvm->arch.lpid);
  97. mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
  98. mtspr(SPRN_GPIR, vcpu->vcpu_id);
  99. mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
  100. mtspr(SPRN_EPLC, vcpu->arch.eplc);
  101. mtspr(SPRN_EPSC, vcpu->arch.epsc);
  102. mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
  103. mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
  104. mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
  105. mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
  106. mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
  107. mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
  108. mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
  109. mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
  110. mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
  111. mtspr(SPRN_GEPR, vcpu->arch.epr);
  112. mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
  113. mtspr(SPRN_GESR, vcpu->arch.shared->esr);
  114. if (vcpu->arch.oldpir != mfspr(SPRN_PIR))
  115. kvmppc_e500_tlbil_all(vcpu_e500);
  116. kvmppc_load_guest_fp(vcpu);
  117. }
  118. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  119. {
  120. vcpu->arch.eplc = mfspr(SPRN_EPLC);
  121. vcpu->arch.epsc = mfspr(SPRN_EPSC);
  122. vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
  123. vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
  124. vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
  125. vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
  126. vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
  127. vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
  128. vcpu->arch.epr = mfspr(SPRN_GEPR);
  129. vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
  130. vcpu->arch.shared->esr = mfspr(SPRN_GESR);
  131. vcpu->arch.oldpir = mfspr(SPRN_PIR);
  132. kvmppc_booke_vcpu_put(vcpu);
  133. }
  134. int kvmppc_core_check_processor_compat(void)
  135. {
  136. int r;
  137. if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
  138. r = 0;
  139. else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
  140. r = 0;
  141. else
  142. r = -ENOTSUPP;
  143. return r;
  144. }
  145. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  146. {
  147. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  148. vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
  149. SPRN_EPCR_DUVD;
  150. #ifdef CONFIG_64BIT
  151. vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
  152. #endif
  153. vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_DEP | MSRP_PMMP;
  154. vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
  155. vcpu->arch.epsc = vcpu->arch.eplc;
  156. vcpu->arch.pvr = mfspr(SPRN_PVR);
  157. vcpu_e500->svr = mfspr(SPRN_SVR);
  158. vcpu->arch.cpu_type = KVM_CPU_E500MC;
  159. return 0;
  160. }
  161. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  162. {
  163. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  164. sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
  165. KVM_SREGS_E_PC;
  166. sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
  167. sregs->u.e.impl.fsl.features = 0;
  168. sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
  169. sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
  170. sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
  171. kvmppc_get_sregs_e500_tlb(vcpu, sregs);
  172. sregs->u.e.ivor_high[3] =
  173. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
  174. sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
  175. sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
  176. kvmppc_get_sregs_ivor(vcpu, sregs);
  177. }
  178. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  179. {
  180. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  181. int ret;
  182. if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
  183. vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
  184. vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
  185. vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
  186. }
  187. ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
  188. if (ret < 0)
  189. return ret;
  190. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  191. return 0;
  192. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  193. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  194. sregs->u.e.ivor_high[3];
  195. }
  196. if (sregs->u.e.features & KVM_SREGS_E_PC) {
  197. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
  198. sregs->u.e.ivor_high[4];
  199. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
  200. sregs->u.e.ivor_high[5];
  201. }
  202. return kvmppc_set_sregs_ivor(vcpu, sregs);
  203. }
  204. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  205. {
  206. struct kvmppc_vcpu_e500 *vcpu_e500;
  207. struct kvm_vcpu *vcpu;
  208. int err;
  209. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  210. if (!vcpu_e500) {
  211. err = -ENOMEM;
  212. goto out;
  213. }
  214. vcpu = &vcpu_e500->vcpu;
  215. /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
  216. vcpu->arch.oldpir = 0xffffffff;
  217. err = kvm_vcpu_init(vcpu, kvm, id);
  218. if (err)
  219. goto free_vcpu;
  220. err = kvmppc_e500_tlb_init(vcpu_e500);
  221. if (err)
  222. goto uninit_vcpu;
  223. vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  224. if (!vcpu->arch.shared)
  225. goto uninit_tlb;
  226. return vcpu;
  227. uninit_tlb:
  228. kvmppc_e500_tlb_uninit(vcpu_e500);
  229. uninit_vcpu:
  230. kvm_vcpu_uninit(vcpu);
  231. free_vcpu:
  232. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  233. out:
  234. return ERR_PTR(err);
  235. }
  236. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  237. {
  238. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  239. free_page((unsigned long)vcpu->arch.shared);
  240. kvmppc_e500_tlb_uninit(vcpu_e500);
  241. kvm_vcpu_uninit(vcpu);
  242. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  243. }
  244. int kvmppc_core_init_vm(struct kvm *kvm)
  245. {
  246. int lpid;
  247. lpid = kvmppc_alloc_lpid();
  248. if (lpid < 0)
  249. return lpid;
  250. kvm->arch.lpid = lpid;
  251. return 0;
  252. }
  253. void kvmppc_core_destroy_vm(struct kvm *kvm)
  254. {
  255. kvmppc_free_lpid(kvm->arch.lpid);
  256. }
  257. static int __init kvmppc_e500mc_init(void)
  258. {
  259. int r;
  260. r = kvmppc_booke_init();
  261. if (r)
  262. return r;
  263. kvmppc_init_lpid(64);
  264. kvmppc_claim_lpid(0); /* host */
  265. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  266. }
  267. static void __exit kvmppc_e500mc_exit(void)
  268. {
  269. kvmppc_booke_exit();
  270. }
  271. module_init(kvmppc_e500mc_init);
  272. module_exit(kvmppc_e500mc_exit);