vgic-v2.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2012,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.h>
  26. #include <asm/kvm_emulate.h>
  27. #include <asm/kvm_arm.h>
  28. #include <asm/kvm_mmu.h>
  29. static struct vgic_lr vgic_v2_get_lr(const struct kvm_vcpu *vcpu, int lr)
  30. {
  31. struct vgic_lr lr_desc;
  32. u32 val = vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr];
  33. lr_desc.irq = val & GICH_LR_VIRTUALID;
  34. if (lr_desc.irq <= 15)
  35. lr_desc.source = (val >> GICH_LR_PHYSID_CPUID_SHIFT) & 0x7;
  36. else
  37. lr_desc.source = 0;
  38. lr_desc.state = 0;
  39. if (val & GICH_LR_PENDING_BIT)
  40. lr_desc.state |= LR_STATE_PENDING;
  41. if (val & GICH_LR_ACTIVE_BIT)
  42. lr_desc.state |= LR_STATE_ACTIVE;
  43. if (val & GICH_LR_EOI)
  44. lr_desc.state |= LR_EOI_INT;
  45. if (val & GICH_LR_HW) {
  46. lr_desc.state |= LR_HW;
  47. lr_desc.hwirq = (val & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT;
  48. }
  49. return lr_desc;
  50. }
  51. static void vgic_v2_set_lr(struct kvm_vcpu *vcpu, int lr,
  52. struct vgic_lr lr_desc)
  53. {
  54. u32 lr_val;
  55. lr_val = lr_desc.irq;
  56. if (lr_desc.state & LR_STATE_PENDING)
  57. lr_val |= GICH_LR_PENDING_BIT;
  58. if (lr_desc.state & LR_STATE_ACTIVE)
  59. lr_val |= GICH_LR_ACTIVE_BIT;
  60. if (lr_desc.state & LR_EOI_INT)
  61. lr_val |= GICH_LR_EOI;
  62. if (lr_desc.state & LR_HW) {
  63. lr_val |= GICH_LR_HW;
  64. lr_val |= (u32)lr_desc.hwirq << GICH_LR_PHYSID_CPUID_SHIFT;
  65. }
  66. if (lr_desc.irq < VGIC_NR_SGIS)
  67. lr_val |= (lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT);
  68. vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = lr_val;
  69. if (!(lr_desc.state & LR_STATE_MASK))
  70. vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr |= (1ULL << lr);
  71. else
  72. vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr &= ~(1ULL << lr);
  73. }
  74. static u64 vgic_v2_get_elrsr(const struct kvm_vcpu *vcpu)
  75. {
  76. return vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr;
  77. }
  78. static u64 vgic_v2_get_eisr(const struct kvm_vcpu *vcpu)
  79. {
  80. return vcpu->arch.vgic_cpu.vgic_v2.vgic_eisr;
  81. }
  82. static void vgic_v2_clear_eisr(struct kvm_vcpu *vcpu)
  83. {
  84. vcpu->arch.vgic_cpu.vgic_v2.vgic_eisr = 0;
  85. }
  86. static u32 vgic_v2_get_interrupt_status(const struct kvm_vcpu *vcpu)
  87. {
  88. u32 misr = vcpu->arch.vgic_cpu.vgic_v2.vgic_misr;
  89. u32 ret = 0;
  90. if (misr & GICH_MISR_EOI)
  91. ret |= INT_STATUS_EOI;
  92. if (misr & GICH_MISR_U)
  93. ret |= INT_STATUS_UNDERFLOW;
  94. return ret;
  95. }
  96. static void vgic_v2_enable_underflow(struct kvm_vcpu *vcpu)
  97. {
  98. vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr |= GICH_HCR_UIE;
  99. }
  100. static void vgic_v2_disable_underflow(struct kvm_vcpu *vcpu)
  101. {
  102. vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr &= ~GICH_HCR_UIE;
  103. }
  104. static void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  105. {
  106. u32 vmcr = vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr;
  107. vmcrp->ctlr = (vmcr & GICH_VMCR_CTRL_MASK) >> GICH_VMCR_CTRL_SHIFT;
  108. vmcrp->abpr = (vmcr & GICH_VMCR_ALIAS_BINPOINT_MASK) >> GICH_VMCR_ALIAS_BINPOINT_SHIFT;
  109. vmcrp->bpr = (vmcr & GICH_VMCR_BINPOINT_MASK) >> GICH_VMCR_BINPOINT_SHIFT;
  110. vmcrp->pmr = (vmcr & GICH_VMCR_PRIMASK_MASK) >> GICH_VMCR_PRIMASK_SHIFT;
  111. }
  112. static void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
  113. {
  114. u32 vmcr;
  115. vmcr = (vmcrp->ctlr << GICH_VMCR_CTRL_SHIFT) & GICH_VMCR_CTRL_MASK;
  116. vmcr |= (vmcrp->abpr << GICH_VMCR_ALIAS_BINPOINT_SHIFT) & GICH_VMCR_ALIAS_BINPOINT_MASK;
  117. vmcr |= (vmcrp->bpr << GICH_VMCR_BINPOINT_SHIFT) & GICH_VMCR_BINPOINT_MASK;
  118. vmcr |= (vmcrp->pmr << GICH_VMCR_PRIMASK_SHIFT) & GICH_VMCR_PRIMASK_MASK;
  119. vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = vmcr;
  120. }
  121. static void vgic_v2_enable(struct kvm_vcpu *vcpu)
  122. {
  123. /*
  124. * By forcing VMCR to zero, the GIC will restore the binary
  125. * points to their reset values. Anything else resets to zero
  126. * anyway.
  127. */
  128. vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0;
  129. vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr = ~0;
  130. /* Get the show on the road... */
  131. vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN;
  132. }
  133. static const struct vgic_ops vgic_v2_ops = {
  134. .get_lr = vgic_v2_get_lr,
  135. .set_lr = vgic_v2_set_lr,
  136. .get_elrsr = vgic_v2_get_elrsr,
  137. .get_eisr = vgic_v2_get_eisr,
  138. .clear_eisr = vgic_v2_clear_eisr,
  139. .get_interrupt_status = vgic_v2_get_interrupt_status,
  140. .enable_underflow = vgic_v2_enable_underflow,
  141. .disable_underflow = vgic_v2_disable_underflow,
  142. .get_vmcr = vgic_v2_get_vmcr,
  143. .set_vmcr = vgic_v2_set_vmcr,
  144. .enable = vgic_v2_enable,
  145. };
  146. static struct vgic_params vgic_v2_params;
  147. /**
  148. * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT
  149. * @node: pointer to the DT node
  150. * @ops: address of a pointer to the GICv2 operations
  151. * @params: address of a pointer to HW-specific parameters
  152. *
  153. * Returns 0 if a GICv2 has been found, with the low level operations
  154. * in *ops and the HW parameters in *params. Returns an error code
  155. * otherwise.
  156. */
  157. int vgic_v2_probe(struct device_node *vgic_node,
  158. const struct vgic_ops **ops,
  159. const struct vgic_params **params)
  160. {
  161. int ret;
  162. struct resource vctrl_res;
  163. struct resource vcpu_res;
  164. struct vgic_params *vgic = &vgic_v2_params;
  165. vgic->maint_irq = irq_of_parse_and_map(vgic_node, 0);
  166. if (!vgic->maint_irq) {
  167. kvm_err("error getting vgic maintenance irq from DT\n");
  168. ret = -ENXIO;
  169. goto out;
  170. }
  171. ret = of_address_to_resource(vgic_node, 2, &vctrl_res);
  172. if (ret) {
  173. kvm_err("Cannot obtain GICH resource\n");
  174. goto out;
  175. }
  176. vgic->vctrl_base = of_iomap(vgic_node, 2);
  177. if (!vgic->vctrl_base) {
  178. kvm_err("Cannot ioremap GICH\n");
  179. ret = -ENOMEM;
  180. goto out;
  181. }
  182. vgic->nr_lr = readl_relaxed(vgic->vctrl_base + GICH_VTR);
  183. vgic->nr_lr = (vgic->nr_lr & 0x3f) + 1;
  184. ret = create_hyp_io_mappings(vgic->vctrl_base,
  185. vgic->vctrl_base + resource_size(&vctrl_res),
  186. vctrl_res.start);
  187. if (ret) {
  188. kvm_err("Cannot map VCTRL into hyp\n");
  189. goto out_unmap;
  190. }
  191. if (of_address_to_resource(vgic_node, 3, &vcpu_res)) {
  192. kvm_err("Cannot obtain GICV resource\n");
  193. ret = -ENXIO;
  194. goto out_unmap;
  195. }
  196. if (!PAGE_ALIGNED(vcpu_res.start)) {
  197. kvm_err("GICV physical address 0x%llx not page aligned\n",
  198. (unsigned long long)vcpu_res.start);
  199. ret = -ENXIO;
  200. goto out_unmap;
  201. }
  202. if (!PAGE_ALIGNED(resource_size(&vcpu_res))) {
  203. kvm_err("GICV size 0x%llx not a multiple of page size 0x%lx\n",
  204. (unsigned long long)resource_size(&vcpu_res),
  205. PAGE_SIZE);
  206. ret = -ENXIO;
  207. goto out_unmap;
  208. }
  209. vgic->can_emulate_gicv2 = true;
  210. kvm_register_device_ops(&kvm_arm_vgic_v2_ops, KVM_DEV_TYPE_ARM_VGIC_V2);
  211. vgic->vcpu_base = vcpu_res.start;
  212. kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
  213. vctrl_res.start, vgic->maint_irq);
  214. vgic->type = VGIC_V2;
  215. vgic->max_gic_vcpus = VGIC_V2_MAX_CPUS;
  216. *ops = &vgic_v2_ops;
  217. *params = vgic;
  218. goto out;
  219. out_unmap:
  220. iounmap(vgic->vctrl_base);
  221. out:
  222. of_node_put(vgic_node);
  223. return ret;
  224. }