e500.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/err.h>
  16. #include <asm/reg.h>
  17. #include <asm/cputable.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/kvm_e500.h>
  20. #include <asm/kvm_ppc.h>
  21. #include "booke.h"
  22. #include "e500_tlb.h"
  23. void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
  24. {
  25. }
  26. void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
  27. {
  28. }
  29. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  30. {
  31. kvmppc_e500_tlb_load(vcpu, cpu);
  32. }
  33. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  34. {
  35. kvmppc_e500_tlb_put(vcpu);
  36. }
  37. int kvmppc_core_check_processor_compat(void)
  38. {
  39. int r;
  40. if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
  41. r = 0;
  42. else
  43. r = -ENOTSUPP;
  44. return r;
  45. }
  46. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  47. {
  48. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  49. kvmppc_e500_tlb_setup(vcpu_e500);
  50. /* Use the same core vertion as host's */
  51. vcpu->arch.pvr = mfspr(SPRN_PVR);
  52. return 0;
  53. }
  54. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  55. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  56. struct kvm_translation *tr)
  57. {
  58. int index;
  59. gva_t eaddr;
  60. u8 pid;
  61. u8 as;
  62. eaddr = tr->linear_address;
  63. pid = (tr->linear_address >> 32) & 0xff;
  64. as = (tr->linear_address >> 40) & 0x1;
  65. index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
  66. if (index < 0) {
  67. tr->valid = 0;
  68. return 0;
  69. }
  70. tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
  71. /* XXX what does "writeable" and "usermode" even mean? */
  72. tr->valid = 1;
  73. return 0;
  74. }
  75. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  76. {
  77. struct kvmppc_vcpu_e500 *vcpu_e500;
  78. struct kvm_vcpu *vcpu;
  79. int err;
  80. vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  81. if (!vcpu_e500) {
  82. err = -ENOMEM;
  83. goto out;
  84. }
  85. vcpu = &vcpu_e500->vcpu;
  86. err = kvm_vcpu_init(vcpu, kvm, id);
  87. if (err)
  88. goto free_vcpu;
  89. err = kvmppc_e500_tlb_init(vcpu_e500);
  90. if (err)
  91. goto uninit_vcpu;
  92. return vcpu;
  93. uninit_vcpu:
  94. kvm_vcpu_uninit(vcpu);
  95. free_vcpu:
  96. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  97. out:
  98. return ERR_PTR(err);
  99. }
  100. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  101. {
  102. struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
  103. kvmppc_e500_tlb_uninit(vcpu_e500);
  104. kvm_vcpu_uninit(vcpu);
  105. kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
  106. }
  107. static int kvmppc_e500_init(void)
  108. {
  109. int r, i;
  110. unsigned long ivor[3];
  111. unsigned long max_ivor = 0;
  112. r = kvmppc_booke_init();
  113. if (r)
  114. return r;
  115. /* copy extra E500 exception handlers */
  116. ivor[0] = mfspr(SPRN_IVOR32);
  117. ivor[1] = mfspr(SPRN_IVOR33);
  118. ivor[2] = mfspr(SPRN_IVOR34);
  119. for (i = 0; i < 3; i++) {
  120. if (ivor[i] > max_ivor)
  121. max_ivor = ivor[i];
  122. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  123. kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
  124. kvmppc_handler_len);
  125. }
  126. flush_icache_range(kvmppc_booke_handlers,
  127. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  128. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), THIS_MODULE);
  129. }
  130. static void kvmppc_e500_exit(void)
  131. {
  132. kvmppc_booke_exit();
  133. }
  134. module_init(kvmppc_e500_init);
  135. module_exit(kvmppc_e500_exit);