coproc_a15.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Authors: Rusty Russell <rusty@rustcorp.au>
  4. * Christoffer Dall <c.dall@virtualopensystems.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include <linux/kvm_host.h>
  20. #include <asm/cputype.h>
  21. #include <asm/kvm_arm.h>
  22. #include <asm/kvm_host.h>
  23. #include <asm/kvm_emulate.h>
  24. #include <asm/kvm_coproc.h>
  25. #include <linux/init.h>
  26. static void reset_mpidr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
  27. {
  28. /*
  29. * Compute guest MPIDR:
  30. * (Even if we present only one VCPU to the guest on an SMP
  31. * host we don't set the U bit in the MPIDR, or vice versa, as
  32. * revealing the underlying hardware properties is likely to
  33. * be the best choice).
  34. */
  35. vcpu->arch.cp15[c0_MPIDR] = (read_cpuid_mpidr() & ~MPIDR_LEVEL_MASK)
  36. | (vcpu->vcpu_id & MPIDR_LEVEL_MASK);
  37. }
  38. #include "coproc.h"
  39. /* A15 TRM 4.3.28: RO WI */
  40. static bool access_actlr(struct kvm_vcpu *vcpu,
  41. const struct coproc_params *p,
  42. const struct coproc_reg *r)
  43. {
  44. if (p->is_write)
  45. return ignore_write(vcpu, p);
  46. *vcpu_reg(vcpu, p->Rt1) = vcpu->arch.cp15[c1_ACTLR];
  47. return true;
  48. }
  49. /* A15 TRM 4.3.60: R/O. */
  50. static bool access_cbar(struct kvm_vcpu *vcpu,
  51. const struct coproc_params *p,
  52. const struct coproc_reg *r)
  53. {
  54. if (p->is_write)
  55. return write_to_read_only(vcpu, p);
  56. return read_zero(vcpu, p);
  57. }
  58. /* A15 TRM 4.3.48: R/O WI. */
  59. static bool access_l2ctlr(struct kvm_vcpu *vcpu,
  60. const struct coproc_params *p,
  61. const struct coproc_reg *r)
  62. {
  63. if (p->is_write)
  64. return ignore_write(vcpu, p);
  65. *vcpu_reg(vcpu, p->Rt1) = vcpu->arch.cp15[c9_L2CTLR];
  66. return true;
  67. }
  68. static void reset_l2ctlr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
  69. {
  70. u32 l2ctlr, ncores;
  71. asm volatile("mrc p15, 1, %0, c9, c0, 2\n" : "=r" (l2ctlr));
  72. l2ctlr &= ~(3 << 24);
  73. ncores = atomic_read(&vcpu->kvm->online_vcpus) - 1;
  74. l2ctlr |= (ncores & 3) << 24;
  75. vcpu->arch.cp15[c9_L2CTLR] = l2ctlr;
  76. }
  77. static void reset_actlr(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
  78. {
  79. u32 actlr;
  80. /* ACTLR contains SMP bit: make sure you create all cpus first! */
  81. asm volatile("mrc p15, 0, %0, c1, c0, 1\n" : "=r" (actlr));
  82. /* Make the SMP bit consistent with the guest configuration */
  83. if (atomic_read(&vcpu->kvm->online_vcpus) > 1)
  84. actlr |= 1U << 6;
  85. else
  86. actlr &= ~(1U << 6);
  87. vcpu->arch.cp15[c1_ACTLR] = actlr;
  88. }
  89. /* A15 TRM 4.3.49: R/O WI (even if NSACR.NS_L2ERR, a write of 1 is ignored). */
  90. static bool access_l2ectlr(struct kvm_vcpu *vcpu,
  91. const struct coproc_params *p,
  92. const struct coproc_reg *r)
  93. {
  94. if (p->is_write)
  95. return ignore_write(vcpu, p);
  96. *vcpu_reg(vcpu, p->Rt1) = 0;
  97. return true;
  98. }
  99. /*
  100. * A15-specific CP15 registers.
  101. * Important: Must be sorted ascending by CRn, CRM, Op1, Op2
  102. */
  103. static const struct coproc_reg a15_regs[] = {
  104. /* MPIDR: we use VMPIDR for guest access. */
  105. { CRn( 0), CRm( 0), Op1( 0), Op2( 5), is32,
  106. NULL, reset_mpidr, c0_MPIDR },
  107. /* SCTLR: swapped by interrupt.S. */
  108. { CRn( 1), CRm( 0), Op1( 0), Op2( 0), is32,
  109. NULL, reset_val, c1_SCTLR, 0x00C50078 },
  110. /* ACTLR: trapped by HCR.TAC bit. */
  111. { CRn( 1), CRm( 0), Op1( 0), Op2( 1), is32,
  112. access_actlr, reset_actlr, c1_ACTLR },
  113. /* CPACR: swapped by interrupt.S. */
  114. { CRn( 1), CRm( 0), Op1( 0), Op2( 2), is32,
  115. NULL, reset_val, c1_CPACR, 0x00000000 },
  116. /*
  117. * L2CTLR access (guest wants to know #CPUs).
  118. */
  119. { CRn( 9), CRm( 0), Op1( 1), Op2( 2), is32,
  120. access_l2ctlr, reset_l2ctlr, c9_L2CTLR },
  121. { CRn( 9), CRm( 0), Op1( 1), Op2( 3), is32, access_l2ectlr},
  122. /* The Configuration Base Address Register. */
  123. { CRn(15), CRm( 0), Op1( 4), Op2( 0), is32, access_cbar},
  124. };
  125. static struct kvm_coproc_target_table a15_target_table = {
  126. .target = KVM_ARM_TARGET_CORTEX_A15,
  127. .table = a15_regs,
  128. .num = ARRAY_SIZE(a15_regs),
  129. };
  130. static int __init coproc_a15_init(void)
  131. {
  132. unsigned int i;
  133. for (i = 1; i < ARRAY_SIZE(a15_regs); i++)
  134. BUG_ON(cmp_reg(&a15_regs[i-1],
  135. &a15_regs[i]) >= 0);
  136. kvm_register_target_coproc_table(&a15_target_table);
  137. return 0;
  138. }
  139. late_initcall(coproc_a15_init);