e500.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, <yu.liu@freescale.com>
  5. *
  6. * Description:
  7. * This file is derived from arch/powerpc/kvm/44x.c,
  8. * by Hollis Blanchard <hollisb@us.ibm.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 <asm/reg.h>
  18. #include <asm/cputable.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/kvm_e500.h>
  21. #include <asm/kvm_ppc.h>
  22. #include "booke.h"
  23. #include "e500_tlb.h"
  24. void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
  25. {
  26. }
  27. void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
  28. {
  29. }
  30. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  31. {
  32. kvmppc_e500_tlb_load(vcpu, cpu);
  33. }
  34. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  35. {
  36. kvmppc_e500_tlb_put(vcpu);
  37. #ifdef CONFIG_SPE
  38. if (vcpu->arch.shadow_msr & MSR_SPE)
  39. kvmppc_vcpu_disable_spe(vcpu);
  40. #endif
  41. }
  42. int kvmppc_core_check_processor_compat(void)
  43. {
  44. int r;
  45. if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
  46. r = 0;
  47. else
  48. r = -ENOTSUPP;
  49. return r;
  50. }
  51. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  52. {
  53. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  54. kvmppc_e500_tlb_setup(vcpu_e500);
  55. /* Registers init */
  56. vcpu->arch.pvr = mfspr(SPRN_PVR);
  57. vcpu_e500->svr = mfspr(SPRN_SVR);
  58. /* Since booke kvm only support one core, update all vcpus' PIR to 0 */
  59. vcpu->vcpu_id = 0;
  60. return 0;
  61. }
  62. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  63. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  64. struct kvm_translation *tr)
  65. {
  66. int index;
  67. gva_t eaddr;
  68. u8 pid;
  69. u8 as;
  70. eaddr = tr->linear_address;
  71. pid = (tr->linear_address >> 32) & 0xff;
  72. as = (tr->linear_address >> 40) & 0x1;
  73. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  74. if (index < 0) {
  75. tr->valid = 0;
  76. return 0;
  77. }
  78. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  79. /* XXX what does "writeable" and "usermode" even mean? */
  80. tr->valid = 1;
  81. return 0;
  82. }
  83. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  84. {
  85. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  86. sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
  87. KVM_SREGS_E_PM;
  88. sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
  89. sregs->u.e.impl.fsl.features = 0;
  90. sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
  91. sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
  92. sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
  93. sregs->u.e.mas0 = vcpu_e500->mas0;
  94. sregs->u.e.mas1 = vcpu_e500->mas1;
  95. sregs->u.e.mas2 = vcpu_e500->mas2;
  96. sregs->u.e.mas7_3 = ((u64)vcpu_e500->mas7 << 32) | vcpu_e500->mas3;
  97. sregs->u.e.mas4 = vcpu_e500->mas4;
  98. sregs->u.e.mas6 = vcpu_e500->mas6;
  99. sregs->u.e.mmucfg = mfspr(SPRN_MMUCFG);
  100. sregs->u.e.tlbcfg[0] = vcpu_e500->tlb0cfg;
  101. sregs->u.e.tlbcfg[1] = vcpu_e500->tlb1cfg;
  102. sregs->u.e.tlbcfg[2] = 0;
  103. sregs->u.e.tlbcfg[3] = 0;
  104. sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
  105. sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
  106. sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
  107. sregs->u.e.ivor_high[3] =
  108. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
  109. kvmppc_get_sregs_ivor(vcpu, sregs);
  110. }
  111. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  112. {
  113. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  114. if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
  115. vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
  116. vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
  117. vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
  118. }
  119. if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
  120. vcpu_e500->mas0 = sregs->u.e.mas0;
  121. vcpu_e500->mas1 = sregs->u.e.mas1;
  122. vcpu_e500->mas2 = sregs->u.e.mas2;
  123. vcpu_e500->mas7 = sregs->u.e.mas7_3 >> 32;
  124. vcpu_e500->mas3 = (u32)sregs->u.e.mas7_3;
  125. vcpu_e500->mas4 = sregs->u.e.mas4;
  126. vcpu_e500->mas6 = sregs->u.e.mas6;
  127. }
  128. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  129. return 0;
  130. if (sregs->u.e.features & KVM_SREGS_E_SPE) {
  131. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
  132. sregs->u.e.ivor_high[0];
  133. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
  134. sregs->u.e.ivor_high[1];
  135. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
  136. sregs->u.e.ivor_high[2];
  137. }
  138. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  139. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  140. sregs->u.e.ivor_high[3];
  141. }
  142. return kvmppc_set_sregs_ivor(vcpu, sregs);
  143. }
  144. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  145. {
  146. struct kvmppc_vcpu_e500 *vcpu_e500;
  147. struct kvm_vcpu *vcpu;
  148. int err;
  149. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  150. if (!vcpu_e500) {
  151. err = -ENOMEM;
  152. goto out;
  153. }
  154. vcpu = &vcpu_e500->vcpu;
  155. err = kvm_vcpu_init(vcpu, kvm, id);
  156. if (err)
  157. goto free_vcpu;
  158. err = kvmppc_e500_tlb_init(vcpu_e500);
  159. if (err)
  160. goto uninit_vcpu;
  161. vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  162. if (!vcpu->arch.shared)
  163. goto uninit_tlb;
  164. return vcpu;
  165. uninit_tlb:
  166. kvmppc_e500_tlb_uninit(vcpu_e500);
  167. uninit_vcpu:
  168. kvm_vcpu_uninit(vcpu);
  169. free_vcpu:
  170. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  171. out:
  172. return ERR_PTR(err);
  173. }
  174. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  175. {
  176. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  177. free_page((unsigned long)vcpu->arch.shared);
  178. kvm_vcpu_uninit(vcpu);
  179. kvmppc_e500_tlb_uninit(vcpu_e500);
  180. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  181. }
  182. static int __init kvmppc_e500_init(void)
  183. {
  184. int r, i;
  185. unsigned long ivor[3];
  186. unsigned long max_ivor = 0;
  187. r = kvmppc_booke_init();
  188. if (r)
  189. return r;
  190. /* copy extra E500 exception handlers */
  191. ivor[0] = mfspr(SPRN_IVOR32);
  192. ivor[1] = mfspr(SPRN_IVOR33);
  193. ivor[2] = mfspr(SPRN_IVOR34);
  194. for (i = 0; i < 3; i++) {
  195. if (ivor[i] > max_ivor)
  196. max_ivor = ivor[i];
  197. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  198. kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
  199. kvmppc_handler_len);
  200. }
  201. flush_icache_range(kvmppc_booke_handlers,
  202. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  203. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  204. }
  205. static void __exit kvmppc_e500_exit(void)
  206. {
  207. kvmppc_booke_exit();
  208. }
  209. module_init(kvmppc_e500_init);
  210. module_exit(kvmppc_e500_exit);