e500.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. return vcpu;
  96. uninit_vcpu:
  97. kvm_vcpu_uninit(vcpu);
  98. free_vcpu:
  99. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  100. out:
  101. return ERR_PTR(err);
  102. }
  103. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  104. {
  105. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  106. kvmppc_e500_tlb_uninit(vcpu_e500);
  107. kvm_vcpu_uninit(vcpu);
  108. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  109. }
  110. static int __init kvmppc_e500_init(void)
  111. {
  112. int r, i;
  113. unsigned long ivor[3];
  114. unsigned long max_ivor = 0;
  115. r = kvmppc_booke_init();
  116. if (r)
  117. return r;
  118. /* copy extra E500 exception handlers */
  119. ivor[0] = mfspr(SPRN_IVOR32);
  120. ivor[1] = mfspr(SPRN_IVOR33);
  121. ivor[2] = mfspr(SPRN_IVOR34);
  122. for (i = 0; i < 3; i++) {
  123. if (ivor[i] > max_ivor)
  124. max_ivor = ivor[i];
  125. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  126. kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
  127. kvmppc_handler_len);
  128. }
  129. flush_icache_range(kvmppc_booke_handlers,
  130. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  131. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
  132. }
  133. static void __exit kvmppc_e500_exit(void)
  134. {
  135. kvmppc_booke_exit();
  136. }
  137. module_init(kvmppc_e500_init);
  138. module_exit(kvmppc_e500_exit);