44x.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2008
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #include <linux/kvm_host.h>
  20. #include <linux/err.h>
  21. #include <asm/reg.h>
  22. #include <asm/cputable.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/kvm_44x.h>
  25. #include <asm/kvm_ppc.h>
  26. #include "44x_tlb.h"
  27. /* Note: clearing MSR[DE] just means that the debug interrupt will not be
  28. * delivered *immediately*. Instead, it simply sets the appropriate DBSR bits.
  29. * If those DBSR bits are still set when MSR[DE] is re-enabled, the interrupt
  30. * will be delivered as an "imprecise debug event" (which is indicated by
  31. * DBSR[IDE].
  32. */
  33. static void kvm44x_disable_debug_interrupts(void)
  34. {
  35. mtmsr(mfmsr() & ~MSR_DE);
  36. }
  37. void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
  38. {
  39. kvm44x_disable_debug_interrupts();
  40. mtspr(SPRN_IAC1, vcpu->arch.host_iac[0]);
  41. mtspr(SPRN_IAC2, vcpu->arch.host_iac[1]);
  42. mtspr(SPRN_IAC3, vcpu->arch.host_iac[2]);
  43. mtspr(SPRN_IAC4, vcpu->arch.host_iac[3]);
  44. mtspr(SPRN_DBCR1, vcpu->arch.host_dbcr1);
  45. mtspr(SPRN_DBCR2, vcpu->arch.host_dbcr2);
  46. mtspr(SPRN_DBCR0, vcpu->arch.host_dbcr0);
  47. mtmsr(vcpu->arch.host_msr);
  48. }
  49. void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
  50. {
  51. struct kvm_guest_debug *dbg = &vcpu->guest_debug;
  52. u32 dbcr0 = 0;
  53. vcpu->arch.host_msr = mfmsr();
  54. kvm44x_disable_debug_interrupts();
  55. /* Save host debug register state. */
  56. vcpu->arch.host_iac[0] = mfspr(SPRN_IAC1);
  57. vcpu->arch.host_iac[1] = mfspr(SPRN_IAC2);
  58. vcpu->arch.host_iac[2] = mfspr(SPRN_IAC3);
  59. vcpu->arch.host_iac[3] = mfspr(SPRN_IAC4);
  60. vcpu->arch.host_dbcr0 = mfspr(SPRN_DBCR0);
  61. vcpu->arch.host_dbcr1 = mfspr(SPRN_DBCR1);
  62. vcpu->arch.host_dbcr2 = mfspr(SPRN_DBCR2);
  63. /* set registers up for guest */
  64. if (dbg->bp[0]) {
  65. mtspr(SPRN_IAC1, dbg->bp[0]);
  66. dbcr0 |= DBCR0_IAC1 | DBCR0_IDM;
  67. }
  68. if (dbg->bp[1]) {
  69. mtspr(SPRN_IAC2, dbg->bp[1]);
  70. dbcr0 |= DBCR0_IAC2 | DBCR0_IDM;
  71. }
  72. if (dbg->bp[2]) {
  73. mtspr(SPRN_IAC3, dbg->bp[2]);
  74. dbcr0 |= DBCR0_IAC3 | DBCR0_IDM;
  75. }
  76. if (dbg->bp[3]) {
  77. mtspr(SPRN_IAC4, dbg->bp[3]);
  78. dbcr0 |= DBCR0_IAC4 | DBCR0_IDM;
  79. }
  80. mtspr(SPRN_DBCR0, dbcr0);
  81. mtspr(SPRN_DBCR1, 0);
  82. mtspr(SPRN_DBCR2, 0);
  83. }
  84. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  85. {
  86. kvmppc_44x_tlb_load(vcpu);
  87. }
  88. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  89. {
  90. kvmppc_44x_tlb_put(vcpu);
  91. }
  92. int kvmppc_core_check_processor_compat(void)
  93. {
  94. int r;
  95. if (strcmp(cur_cpu_spec->platform, "ppc440") == 0)
  96. r = 0;
  97. else
  98. r = -ENOTSUPP;
  99. return r;
  100. }
  101. int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
  102. {
  103. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  104. struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[0];
  105. int i;
  106. tlbe->tid = 0;
  107. tlbe->word0 = PPC44x_TLB_16M | PPC44x_TLB_VALID;
  108. tlbe->word1 = 0;
  109. tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR;
  110. tlbe++;
  111. tlbe->tid = 0;
  112. tlbe->word0 = 0xef600000 | PPC44x_TLB_4K | PPC44x_TLB_VALID;
  113. tlbe->word1 = 0xef600000;
  114. tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR
  115. | PPC44x_TLB_I | PPC44x_TLB_G;
  116. /* Since the guest can directly access the timebase, it must know the
  117. * real timebase frequency. Accordingly, it must see the state of
  118. * CCR1[TCS]. */
  119. vcpu->arch.ccr1 = mfspr(SPRN_CCR1);
  120. for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++)
  121. vcpu_44x->shadow_refs[i].gtlb_index = -1;
  122. return 0;
  123. }
  124. /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
  125. int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  126. struct kvm_translation *tr)
  127. {
  128. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  129. struct kvmppc_44x_tlbe *gtlbe;
  130. int index;
  131. gva_t eaddr;
  132. u8 pid;
  133. u8 as;
  134. eaddr = tr->linear_address;
  135. pid = (tr->linear_address >> 32) & 0xff;
  136. as = (tr->linear_address >> 40) & 0x1;
  137. index = kvmppc_44x_tlb_index(vcpu, eaddr, pid, as);
  138. if (index == -1) {
  139. tr->valid = 0;
  140. return 0;
  141. }
  142. gtlbe = &vcpu_44x->guest_tlb[index];
  143. tr->physical_address = tlb_xlate(gtlbe, eaddr);
  144. /* XXX what does "writeable" and "usermode" even mean? */
  145. tr->valid = 1;
  146. return 0;
  147. }
  148. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  149. {
  150. struct kvmppc_vcpu_44x *vcpu_44x;
  151. struct kvm_vcpu *vcpu;
  152. int err;
  153. vcpu_44x = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  154. if (!vcpu_44x) {
  155. err = -ENOMEM;
  156. goto out;
  157. }
  158. vcpu = &vcpu_44x->vcpu;
  159. err = kvm_vcpu_init(vcpu, kvm, id);
  160. if (err)
  161. goto free_vcpu;
  162. return vcpu;
  163. free_vcpu:
  164. kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
  165. out:
  166. return ERR_PTR(err);
  167. }
  168. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  169. {
  170. struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
  171. kvm_vcpu_uninit(vcpu);
  172. kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
  173. }
  174. static int kvmppc_44x_init(void)
  175. {
  176. int r;
  177. r = kvmppc_booke_init();
  178. if (r)
  179. return r;
  180. return kvm_init(NULL, sizeof(struct kvmppc_vcpu_44x), THIS_MODULE);
  181. }
  182. static void kvmppc_44x_exit(void)
  183. {
  184. kvmppc_booke_exit();
  185. }
  186. module_init(kvmppc_44x_init);
  187. module_exit(kvmppc_44x_exit);