e500mc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 if (strcmp(cur_cpu_spec->cpu_name, "e6500") == 0)
  142. r = 0;
  143. else
  144. r = -ENOTSUPP;
  145. return r;
  146. }
  147. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  148. {
  149. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  150. vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
  151. SPRN_EPCR_DUVD;
  152. #ifdef CONFIG_64BIT
  153. vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
  154. #endif
  155. vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_DEP | MSRP_PMMP;
  156. vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
  157. vcpu->arch.epsc = vcpu->arch.eplc;
  158. vcpu->arch.pvr = mfspr(SPRN_PVR);
  159. vcpu_e500->svr = mfspr(SPRN_SVR);
  160. vcpu->arch.cpu_type = KVM_CPU_E500MC;
  161. return 0;
  162. }
  163. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  164. {
  165. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  166. sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
  167. KVM_SREGS_E_PC;
  168. sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
  169. sregs->u.e.impl.fsl.features = 0;
  170. sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
  171. sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
  172. sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
  173. kvmppc_get_sregs_e500_tlb(vcpu, sregs);
  174. sregs->u.e.ivor_high[3] =
  175. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
  176. sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
  177. sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
  178. kvmppc_get_sregs_ivor(vcpu, sregs);
  179. }
  180. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  181. {
  182. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  183. int ret;
  184. if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
  185. vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
  186. vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
  187. vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
  188. }
  189. ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
  190. if (ret < 0)
  191. return ret;
  192. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  193. return 0;
  194. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  195. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  196. sregs->u.e.ivor_high[3];
  197. }
  198. if (sregs->u.e.features & KVM_SREGS_E_PC) {
  199. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
  200. sregs->u.e.ivor_high[4];
  201. vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
  202. sregs->u.e.ivor_high[5];
  203. }
  204. return kvmppc_set_sregs_ivor(vcpu, sregs);
  205. }
  206. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
  207. union kvmppc_one_reg *val)
  208. {
  209. int r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
  210. return r;
  211. }
  212. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
  213. union kvmppc_one_reg *val)
  214. {
  215. int r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
  216. return r;
  217. }
  218. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  219. {
  220. struct kvmppc_vcpu_e500 *vcpu_e500;
  221. struct kvm_vcpu *vcpu;
  222. int err;
  223. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  224. if (!vcpu_e500) {
  225. err = -ENOMEM;
  226. goto out;
  227. }
  228. vcpu = &vcpu_e500->vcpu;
  229. /* Invalid PIR value -- this LPID dosn't have valid state on any cpu */
  230. vcpu->arch.oldpir = 0xffffffff;
  231. err = kvm_vcpu_init(vcpu, kvm, id);
  232. if (err)
  233. goto free_vcpu;
  234. err = kvmppc_e500_tlb_init(vcpu_e500);
  235. if (err)
  236. goto uninit_vcpu;
  237. vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  238. if (!vcpu->arch.shared)
  239. goto uninit_tlb;
  240. return vcpu;
  241. uninit_tlb:
  242. kvmppc_e500_tlb_uninit(vcpu_e500);
  243. uninit_vcpu:
  244. kvm_vcpu_uninit(vcpu);
  245. free_vcpu:
  246. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  247. out:
  248. return ERR_PTR(err);
  249. }
  250. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  251. {
  252. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  253. free_page((unsigned long)vcpu->arch.shared);
  254. kvmppc_e500_tlb_uninit(vcpu_e500);
  255. kvm_vcpu_uninit(vcpu);
  256. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  257. }
  258. int kvmppc_core_init_vm(struct kvm *kvm)
  259. {
  260. int lpid;
  261. lpid = kvmppc_alloc_lpid();
  262. if (lpid < 0)
  263. return lpid;
  264. kvm->arch.lpid = lpid;
  265. return 0;
  266. }
  267. void kvmppc_core_destroy_vm(struct kvm *kvm)
  268. {
  269. kvmppc_free_lpid(kvm->arch.lpid);
  270. }
  271. static int __init kvmppc_e500mc_init(void)
  272. {
  273. int r;
  274. r = kvmppc_booke_init();
  275. if (r)
  276. return r;
  277. kvmppc_init_lpid(64);
  278. kvmppc_claim_lpid(0); /* host */
  279. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  280. }
  281. static void __exit kvmppc_e500mc_exit(void)
  282. {
  283. kvmppc_booke_exit();
  284. }
  285. module_init(kvmppc_e500mc_init);
  286. module_exit(kvmppc_e500mc_exit);