e500.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. vcpu->arch.cpu_type = KVM_CPU_E500V2;
  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->arch.shared->mas0;
  94. sregs->u.e.mas1 = vcpu->arch.shared->mas1;
  95. sregs->u.e.mas2 = vcpu->arch.shared->mas2;
  96. sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
  97. sregs->u.e.mas4 = vcpu->arch.shared->mas4;
  98. sregs->u.e.mas6 = vcpu->arch.shared->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->arch.shared->mas0 = sregs->u.e.mas0;
  121. vcpu->arch.shared->mas1 = sregs->u.e.mas1;
  122. vcpu->arch.shared->mas2 = sregs->u.e.mas2;
  123. vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
  124. vcpu->arch.shared->mas4 = sregs->u.e.mas4;
  125. vcpu->arch.shared->mas6 = sregs->u.e.mas6;
  126. }
  127. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  128. return 0;
  129. if (sregs->u.e.features & KVM_SREGS_E_SPE) {
  130. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] =
  131. sregs->u.e.ivor_high[0];
  132. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] =
  133. sregs->u.e.ivor_high[1];
  134. vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] =
  135. sregs->u.e.ivor_high[2];
  136. }
  137. if (sregs->u.e.features & KVM_SREGS_E_PM) {
  138. vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
  139. sregs->u.e.ivor_high[3];
  140. }
  141. return kvmppc_set_sregs_ivor(vcpu, sregs);
  142. }
  143. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  144. {
  145. struct kvmppc_vcpu_e500 *vcpu_e500;
  146. struct kvm_vcpu *vcpu;
  147. int err;
  148. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  149. if (!vcpu_e500) {
  150. err = -ENOMEM;
  151. goto out;
  152. }
  153. vcpu = &vcpu_e500->vcpu;
  154. err = kvm_vcpu_init(vcpu, kvm, id);
  155. if (err)
  156. goto free_vcpu;
  157. err = kvmppc_e500_tlb_init(vcpu_e500);
  158. if (err)
  159. goto uninit_vcpu;
  160. vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  161. if (!vcpu->arch.shared)
  162. goto uninit_tlb;
  163. return vcpu;
  164. uninit_tlb:
  165. kvmppc_e500_tlb_uninit(vcpu_e500);
  166. uninit_vcpu:
  167. kvm_vcpu_uninit(vcpu);
  168. free_vcpu:
  169. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  170. out:
  171. return ERR_PTR(err);
  172. }
  173. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  174. {
  175. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  176. free_page((unsigned long)vcpu->arch.shared);
  177. kvm_vcpu_uninit(vcpu);
  178. kvmppc_e500_tlb_uninit(vcpu_e500);
  179. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  180. }
  181. static int __init kvmppc_e500_init(void)
  182. {
  183. int r, i;
  184. unsigned long ivor[3];
  185. unsigned long max_ivor = 0;
  186. r = kvmppc_core_check_processor_compat();
  187. if (r)
  188. return r;
  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);