e500.c 6.1 KB

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