e500.c 3.5 KB

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