e500.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. vcpu->arch.cpu_type = KVM_CPU_E500V2;
  61. return 0;
  62. }
  63. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  64. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  65. struct kvm_translation *tr)
  66. {
  67. int index;
  68. gva_t eaddr;
  69. u8 pid;
  70. u8 as;
  71. eaddr = tr->linear_address;
  72. pid = (tr->linear_address >> 32) & 0xff;
  73. as = (tr->linear_address >> 40) & 0x1;
  74. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  75. if (index < 0) {
  76. tr->valid = 0;
  77. return 0;
  78. }
  79. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  80. /* XXX what does "writeable" and "usermode" even mean? */
  81. tr->valid = 1;
  82. return 0;
  83. }
  84. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  85. {
  86. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  87. sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_SPE |
  88. KVM_SREGS_E_PM;
  89. sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
  90. sregs->u.e.impl.fsl.features = 0;
  91. sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
  92. sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
  93. sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
  94. sregs->u.e.mas0 = vcpu_e500->mas0;
  95. sregs->u.e.mas1 = vcpu_e500->mas1;
  96. sregs->u.e.mas2 = vcpu_e500->mas2;
  97. sregs->u.e.mas7_3 = ((u64)vcpu_e500->mas7 << 32) | vcpu_e500->mas3;
  98. sregs->u.e.mas4 = vcpu_e500->mas4;
  99. sregs->u.e.mas6 = vcpu_e500->mas6;
  100. sregs->u.e.mmucfg = mfspr(SPRN_MMUCFG);
  101. sregs->u.e.tlbcfg[0] = vcpu_e500->tlb0cfg;
  102. sregs->u.e.tlbcfg[1] = vcpu_e500->tlb1cfg;
  103. sregs->u.e.tlbcfg[2] = 0;
  104. sregs->u.e.tlbcfg[3] = 0;
  105. sregs->u.e.ivor_high[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
  106. sregs->u.e.ivor_high[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
  107. sregs->u.e.ivor_high[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
  108. sregs->u.e.ivor_high[3] =
  109. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
  110. kvmppc_get_sregs_ivor(vcpu, sregs);
  111. }
  112. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  113. {
  114. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  115. if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
  116. vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
  117. vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
  118. vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
  119. }
  120. if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
  121. vcpu_e500->mas0 = sregs->u.e.mas0;
  122. vcpu_e500->mas1 = sregs->u.e.mas1;
  123. vcpu_e500->mas2 = sregs->u.e.mas2;
  124. vcpu_e500->mas7 = sregs->u.e.mas7_3 >> 32;
  125. vcpu_e500->mas3 = (u32)sregs->u.e.mas7_3;
  126. vcpu_e500->mas4 = sregs->u.e.mas4;
  127. vcpu_e500->mas6 = sregs->u.e.mas6;
  128. }
  129. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  130. return 0;
  131. if (sregs->u.e.features & KVM_SREGS_E_SPE) {
  132. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
  133. sregs->u.e.ivor_high[0];
  134. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
  135. sregs->u.e.ivor_high[1];
  136. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
  137. sregs->u.e.ivor_high[2];
  138. }
  139. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  140. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  141. sregs->u.e.ivor_high[3];
  142. }
  143. return kvmppc_set_sregs_ivor(vcpu, sregs);
  144. }
  145. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  146. {
  147. struct kvmppc_vcpu_e500 *vcpu_e500;
  148. struct kvm_vcpu *vcpu;
  149. int err;
  150. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  151. if (!vcpu_e500) {
  152. err = -ENOMEM;
  153. goto out;
  154. }
  155. vcpu = &vcpu_e500->vcpu;
  156. err = kvm_vcpu_init(vcpu, kvm, id);
  157. if (err)
  158. goto free_vcpu;
  159. err = kvmppc_e500_tlb_init(vcpu_e500);
  160. if (err)
  161. goto uninit_vcpu;
  162. vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  163. if (!vcpu->arch.shared)
  164. goto uninit_tlb;
  165. return vcpu;
  166. uninit_tlb:
  167. kvmppc_e500_tlb_uninit(vcpu_e500);
  168. uninit_vcpu:
  169. kvm_vcpu_uninit(vcpu);
  170. free_vcpu:
  171. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  172. out:
  173. return ERR_PTR(err);
  174. }
  175. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  176. {
  177. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  178. free_page((unsigned long)vcpu->arch.shared);
  179. kvm_vcpu_uninit(vcpu);
  180. kvmppc_e500_tlb_uninit(vcpu_e500);
  181. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  182. }
  183. static int __init kvmppc_e500_init(void)
  184. {
  185. int r, i;
  186. unsigned long ivor[3];
  187. unsigned long max_ivor = 0;
  188. r = kvmppc_booke_init();
  189. if (r)
  190. return r;
  191. /* copy extra E500 exception handlers */
  192. ivor[0] = mfspr(SPRN_IVOR32);
  193. ivor[1] = mfspr(SPRN_IVOR33);
  194. ivor[2] = mfspr(SPRN_IVOR34);
  195. for (i = 0; i < 3; i++) {
  196. if (ivor[i] > max_ivor)
  197. max_ivor = ivor[i];
  198. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  199. kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
  200. kvmppc_handler_len);
  201. }
  202. flush_icache_range(kvmppc_booke_handlers,
  203. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  204. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  205. }
  206. static void __exit kvmppc_e500_exit(void)
  207. {
  208. kvmppc_booke_exit();
  209. }
  210. module_init(kvmppc_e500_init);
  211. module_exit(kvmppc_e500_exit);