e500.c 6.3 KB

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