vgic-v3.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (C) 2013 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/cpu.h>
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/irqchip/arm-gic-v3.h>
  26. #include <asm/kvm_emulate.h>
  27. #include <asm/kvm_arm.h>
  28. #include <asm/kvm_mmu.h>
  29. /* These are for GICv2 emulation only */
  30. #define GICH_LR_VIRTUALID (0x3ffUL << 0)
  31. #define GICH_LR_PHYSID_CPUID_SHIFT (10)
  32. #define GICH_LR_PHYSID_CPUID (7UL << GICH_LR_PHYSID_CPUID_SHIFT)
  33. #define ICH_LR_VIRTUALID_MASK (BIT_ULL(32) - 1)
  34. /*
  35. * LRs are stored in reverse order in memory. make sure we index them
  36. * correctly.
  37. */
  38. #define LR_INDEX(lr) (VGIC_V3_MAX_LRS - 1 - lr)
  39. static u32 ich_vtr_el2;
  40. static struct vgic_lr vgic_v3_get_lr(const struct kvm_vcpu *vcpu, int lr)
  41. {
  42. struct vgic_lr lr_desc;
  43. u64 val = vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)];
  44. if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3)
  45. lr_desc.irq = val & ICH_LR_VIRTUALID_MASK;
  46. else
  47. lr_desc.irq = val & GICH_LR_VIRTUALID;
  48. lr_desc.source = 0;
  49. if (lr_desc.irq <= 15 &&
  50. vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
  51. lr_desc.source = (val >> GICH_LR_PHYSID_CPUID_SHIFT) & 0x7;
  52. lr_desc.state = 0;
  53. if (val & ICH_LR_PENDING_BIT)
  54. lr_desc.state |= LR_STATE_PENDING;
  55. if (val & ICH_LR_ACTIVE_BIT)
  56. lr_desc.state |= LR_STATE_ACTIVE;
  57. if (val & ICH_LR_EOI)
  58. lr_desc.state |= LR_EOI_INT;
  59. if (val & ICH_LR_HW) {
  60. lr_desc.state |= LR_HW;
  61. lr_desc.hwirq = (val >> ICH_LR_PHYS_ID_SHIFT) & GENMASK(9, 0);
  62. }
  63. return lr_desc;
  64. }
  65. static void vgic_v3_set_lr(struct kvm_vcpu *vcpu, int lr,
  66. struct vgic_lr lr_desc)
  67. {
  68. u64 lr_val;
  69. lr_val = lr_desc.irq;
  70. /*
  71. * Currently all guest IRQs are Group1, as Group0 would result
  72. * in a FIQ in the guest, which it wouldn't expect.
  73. * Eventually we want to make this configurable, so we may revisit
  74. * this in the future.
  75. */
  76. switch (vcpu->kvm->arch.vgic.vgic_model) {
  77. case KVM_DEV_TYPE_ARM_VGIC_V3:
  78. lr_val |= ICH_LR_GROUP;
  79. break;
  80. case KVM_DEV_TYPE_ARM_VGIC_V2:
  81. if (lr_desc.irq < VGIC_NR_SGIS)
  82. lr_val |= (u32)lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT;
  83. break;
  84. default:
  85. BUG();
  86. }
  87. if (lr_desc.state & LR_STATE_PENDING)
  88. lr_val |= ICH_LR_PENDING_BIT;
  89. if (lr_desc.state & LR_STATE_ACTIVE)
  90. lr_val |= ICH_LR_ACTIVE_BIT;
  91. if (lr_desc.state & LR_EOI_INT)
  92. lr_val |= ICH_LR_EOI;
  93. if (lr_desc.state & LR_HW) {
  94. lr_val |= ICH_LR_HW;
  95. lr_val |= ((u64)lr_desc.hwirq) << ICH_LR_PHYS_ID_SHIFT;
  96. }
  97. vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)] = lr_val;
  98. }
  99. static void vgic_v3_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
  100. struct vgic_lr lr_desc)
  101. {
  102. if (!(lr_desc.state & LR_STATE_MASK))
  103. vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr |= (1U << lr);
  104. else
  105. vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr &= ~(1U << lr);
  106. }
  107. static u64 vgic_v3_get_elrsr(const struct kvm_vcpu *vcpu)
  108. {
  109. return vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr;
  110. }
  111. static u64 vgic_v3_get_eisr(const struct kvm_vcpu *vcpu)
  112. {
  113. return vcpu->arch.vgic_cpu.vgic_v3.vgic_eisr;
  114. }
  115. static void vgic_v3_clear_eisr(struct kvm_vcpu *vcpu)
  116. {
  117. vcpu->arch.vgic_cpu.vgic_v3.vgic_eisr = 0;
  118. }
  119. static u32 vgic_v3_get_interrupt_status(const struct kvm_vcpu *vcpu)
  120. {
  121. u32 misr = vcpu->arch.vgic_cpu.vgic_v3.vgic_misr;
  122. u32 ret = 0;
  123. if (misr & ICH_MISR_EOI)
  124. ret |= INT_STATUS_EOI;
  125. if (misr & ICH_MISR_U)
  126. ret |= INT_STATUS_UNDERFLOW;
  127. return ret;
  128. }
  129. static void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  130. {
  131. u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr;
  132. vmcrp->ctlr = (vmcr & ICH_VMCR_CTLR_MASK) >> ICH_VMCR_CTLR_SHIFT;
  133. vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
  134. vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
  135. vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
  136. }
  137. static void vgic_v3_enable_underflow(struct kvm_vcpu *vcpu)
  138. {
  139. vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr |= ICH_HCR_UIE;
  140. }
  141. static void vgic_v3_disable_underflow(struct kvm_vcpu *vcpu)
  142. {
  143. vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr &= ~ICH_HCR_UIE;
  144. }
  145. static void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  146. {
  147. u32 vmcr;
  148. vmcr = (vmcrp->ctlr << ICH_VMCR_CTLR_SHIFT) & ICH_VMCR_CTLR_MASK;
  149. vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
  150. vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
  151. vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
  152. vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr;
  153. }
  154. static void vgic_v3_enable(struct kvm_vcpu *vcpu)
  155. {
  156. struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3;
  157. /*
  158. * By forcing VMCR to zero, the GIC will restore the binary
  159. * points to their reset values. Anything else resets to zero
  160. * anyway.
  161. */
  162. vgic_v3->vgic_vmcr = 0;
  163. /*
  164. * If we are emulating a GICv3, we do it in an non-GICv2-compatible
  165. * way, so we force SRE to 1 to demonstrate this to the guest.
  166. * This goes with the spec allowing the value to be RAO/WI.
  167. */
  168. if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3)
  169. vgic_v3->vgic_sre = ICC_SRE_EL1_SRE;
  170. else
  171. vgic_v3->vgic_sre = 0;
  172. /* Get the show on the road... */
  173. vgic_v3->vgic_hcr = ICH_HCR_EN;
  174. }
  175. static const struct vgic_ops vgic_v3_ops = {
  176. .get_lr = vgic_v3_get_lr,
  177. .set_lr = vgic_v3_set_lr,
  178. .sync_lr_elrsr = vgic_v3_sync_lr_elrsr,
  179. .get_elrsr = vgic_v3_get_elrsr,
  180. .get_eisr = vgic_v3_get_eisr,
  181. .clear_eisr = vgic_v3_clear_eisr,
  182. .get_interrupt_status = vgic_v3_get_interrupt_status,
  183. .enable_underflow = vgic_v3_enable_underflow,
  184. .disable_underflow = vgic_v3_disable_underflow,
  185. .get_vmcr = vgic_v3_get_vmcr,
  186. .set_vmcr = vgic_v3_set_vmcr,
  187. .enable = vgic_v3_enable,
  188. };
  189. static struct vgic_params vgic_v3_params;
  190. /**
  191. * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
  192. * @node: pointer to the DT node
  193. * @ops: address of a pointer to the GICv3 operations
  194. * @params: address of a pointer to HW-specific parameters
  195. *
  196. * Returns 0 if a GICv3 has been found, with the low level operations
  197. * in *ops and the HW parameters in *params. Returns an error code
  198. * otherwise.
  199. */
  200. int vgic_v3_probe(struct device_node *vgic_node,
  201. const struct vgic_ops **ops,
  202. const struct vgic_params **params)
  203. {
  204. int ret = 0;
  205. u32 gicv_idx;
  206. struct resource vcpu_res;
  207. struct vgic_params *vgic = &vgic_v3_params;
  208. vgic->maint_irq = irq_of_parse_and_map(vgic_node, 0);
  209. if (!vgic->maint_irq) {
  210. kvm_err("error getting vgic maintenance irq from DT\n");
  211. ret = -ENXIO;
  212. goto out;
  213. }
  214. ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2);
  215. /*
  216. * The ListRegs field is 5 bits, but there is a architectural
  217. * maximum of 16 list registers. Just ignore bit 4...
  218. */
  219. vgic->nr_lr = (ich_vtr_el2 & 0xf) + 1;
  220. vgic->can_emulate_gicv2 = false;
  221. if (of_property_read_u32(vgic_node, "#redistributor-regions", &gicv_idx))
  222. gicv_idx = 1;
  223. gicv_idx += 3; /* Also skip GICD, GICC, GICH */
  224. if (of_address_to_resource(vgic_node, gicv_idx, &vcpu_res)) {
  225. kvm_info("GICv3: no GICV resource entry\n");
  226. vgic->vcpu_base = 0;
  227. } else if (!PAGE_ALIGNED(vcpu_res.start)) {
  228. pr_warn("GICV physical address 0x%llx not page aligned\n",
  229. (unsigned long long)vcpu_res.start);
  230. vgic->vcpu_base = 0;
  231. } else if (!PAGE_ALIGNED(resource_size(&vcpu_res))) {
  232. pr_warn("GICV size 0x%llx not a multiple of page size 0x%lx\n",
  233. (unsigned long long)resource_size(&vcpu_res),
  234. PAGE_SIZE);
  235. vgic->vcpu_base = 0;
  236. } else {
  237. vgic->vcpu_base = vcpu_res.start;
  238. vgic->can_emulate_gicv2 = true;
  239. kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
  240. KVM_DEV_TYPE_ARM_VGIC_V2);
  241. }
  242. if (vgic->vcpu_base == 0)
  243. kvm_info("disabling GICv2 emulation\n");
  244. kvm_register_device_ops(&kvm_arm_vgic_v3_ops, KVM_DEV_TYPE_ARM_VGIC_V3);
  245. vgic->vctrl_base = NULL;
  246. vgic->type = VGIC_V3;
  247. vgic->max_gic_vcpus = VGIC_V3_MAX_CPUS;
  248. kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
  249. vcpu_res.start, vgic->maint_irq);
  250. *ops = &vgic_v3_ops;
  251. *params = vgic;
  252. out:
  253. of_node_put(vgic_node);
  254. return ret;
  255. }