e500.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* Since booke kvm only support one core, update all vcpus' PIR to 0 */
  54. vcpu->vcpu_id = 0;
  55. return 0;
  56. }
  57. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  58. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  59. struct kvm_translation *tr)
  60. {
  61. int index;
  62. gva_t eaddr;
  63. u8 pid;
  64. u8 as;
  65. eaddr = tr->linear_address;
  66. pid = (tr->linear_address >> 32) & 0xff;
  67. as = (tr->linear_address >> 40) & 0x1;
  68. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  69. if (index < 0) {
  70. tr->valid = 0;
  71. return 0;
  72. }
  73. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  74. /* XXX what does "writeable" and "usermode" even mean? */
  75. tr->valid = 1;
  76. return 0;
  77. }
  78. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  79. {
  80. struct kvmppc_vcpu_e500 *vcpu_e500;
  81. struct kvm_vcpu *vcpu;
  82. int err;
  83. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  84. if (!vcpu_e500) {
  85. err = -ENOMEM;
  86. goto out;
  87. }
  88. vcpu = &vcpu_e500->vcpu;
  89. err = kvm_vcpu_init(vcpu, kvm, id);
  90. if (err)
  91. goto free_vcpu;
  92. err = kvmppc_e500_tlb_init(vcpu_e500);
  93. if (err)
  94. goto uninit_vcpu;
  95. vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  96. if (!vcpu->arch.shared)
  97. goto uninit_tlb;
  98. return vcpu;
  99. uninit_tlb:
  100. kvmppc_e500_tlb_uninit(vcpu_e500);
  101. uninit_vcpu:
  102. kvm_vcpu_uninit(vcpu);
  103. free_vcpu:
  104. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  105. out:
  106. return ERR_PTR(err);
  107. }
  108. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  109. {
  110. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  111. free_page((unsigned long)vcpu->arch.shared);
  112. kvm_vcpu_uninit(vcpu);
  113. kvmppc_e500_tlb_uninit(vcpu_e500);
  114. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  115. }
  116. static int __init kvmppc_e500_init(void)
  117. {
  118. int r, i;
  119. unsigned long ivor[3];
  120. unsigned long max_ivor = 0;
  121. r = kvmppc_booke_init();
  122. if (r)
  123. return r;
  124. /* copy extra E500 exception handlers */
  125. ivor[0] = mfspr(SPRN_IVOR32);
  126. ivor[1] = mfspr(SPRN_IVOR33);
  127. ivor[2] = mfspr(SPRN_IVOR34);
  128. for (i = 0; i < 3; i++) {
  129. if (ivor[i] > max_ivor)
  130. max_ivor = ivor[i];
  131. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  132. kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
  133. kvmppc_handler_len);
  134. }
  135. flush_icache_range(kvmppc_booke_handlers,
  136. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  137. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  138. }
  139. static void __exit kvmppc_e500_exit(void)
  140. {
  141. kvmppc_booke_exit();
  142. }
  143. module_init(kvmppc_e500_init);
  144. module_exit(kvmppc_e500_exit);