Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 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 | |
| 18 | #include <linux/cpu.h> |
| 19 | #include <linux/kvm.h> |
| 20 | #include <linux/kvm_host.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/io.h> |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 23 | |
| 24 | #include <linux/irqchip/arm-gic-v3.h> |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 25 | #include <linux/irqchip/arm-gic-common.h> |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include <asm/kvm_emulate.h> |
| 28 | #include <asm/kvm_arm.h> |
Marc Zyngier | 9d8415d | 2015-10-25 19:57:11 +0000 | [diff] [blame] | 29 | #include <asm/kvm_asm.h> |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 30 | #include <asm/kvm_mmu.h> |
| 31 | |
| 32 | /* These are for GICv2 emulation only */ |
| 33 | #define GICH_LR_VIRTUALID (0x3ffUL << 0) |
| 34 | #define GICH_LR_PHYSID_CPUID_SHIFT (10) |
| 35 | #define GICH_LR_PHYSID_CPUID (7UL << GICH_LR_PHYSID_CPUID_SHIFT) |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 36 | #define ICH_LR_VIRTUALID_MASK (BIT_ULL(32) - 1) |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 37 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 38 | static u32 ich_vtr_el2; |
| 39 | |
| 40 | static struct vgic_lr vgic_v3_get_lr(const struct kvm_vcpu *vcpu, int lr) |
| 41 | { |
| 42 | struct vgic_lr lr_desc; |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 43 | u64 val = vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr]; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 44 | |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 45 | if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) |
| 46 | lr_desc.irq = val & ICH_LR_VIRTUALID_MASK; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 47 | else |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 48 | lr_desc.irq = val & GICH_LR_VIRTUALID; |
| 49 | |
| 50 | lr_desc.source = 0; |
| 51 | if (lr_desc.irq <= 15 && |
| 52 | vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2) |
| 53 | lr_desc.source = (val >> GICH_LR_PHYSID_CPUID_SHIFT) & 0x7; |
| 54 | |
| 55 | lr_desc.state = 0; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 56 | |
| 57 | if (val & ICH_LR_PENDING_BIT) |
| 58 | lr_desc.state |= LR_STATE_PENDING; |
| 59 | if (val & ICH_LR_ACTIVE_BIT) |
| 60 | lr_desc.state |= LR_STATE_ACTIVE; |
| 61 | if (val & ICH_LR_EOI) |
| 62 | lr_desc.state |= LR_EOI_INT; |
Marc Zyngier | fb182cf | 2015-06-08 15:37:26 +0100 | [diff] [blame] | 63 | if (val & ICH_LR_HW) { |
| 64 | lr_desc.state |= LR_HW; |
| 65 | lr_desc.hwirq = (val >> ICH_LR_PHYS_ID_SHIFT) & GENMASK(9, 0); |
| 66 | } |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 67 | |
| 68 | return lr_desc; |
| 69 | } |
| 70 | |
| 71 | static void vgic_v3_set_lr(struct kvm_vcpu *vcpu, int lr, |
| 72 | struct vgic_lr lr_desc) |
| 73 | { |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 74 | u64 lr_val; |
| 75 | |
| 76 | lr_val = lr_desc.irq; |
| 77 | |
| 78 | /* |
| 79 | * Currently all guest IRQs are Group1, as Group0 would result |
| 80 | * in a FIQ in the guest, which it wouldn't expect. |
| 81 | * Eventually we want to make this configurable, so we may revisit |
| 82 | * this in the future. |
| 83 | */ |
Marc Zyngier | fb182cf | 2015-06-08 15:37:26 +0100 | [diff] [blame] | 84 | switch (vcpu->kvm->arch.vgic.vgic_model) { |
| 85 | case KVM_DEV_TYPE_ARM_VGIC_V3: |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 86 | lr_val |= ICH_LR_GROUP; |
Marc Zyngier | fb182cf | 2015-06-08 15:37:26 +0100 | [diff] [blame] | 87 | break; |
| 88 | case KVM_DEV_TYPE_ARM_VGIC_V2: |
| 89 | if (lr_desc.irq < VGIC_NR_SGIS) |
| 90 | lr_val |= (u32)lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT; |
| 91 | break; |
| 92 | default: |
| 93 | BUG(); |
| 94 | } |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 95 | |
| 96 | if (lr_desc.state & LR_STATE_PENDING) |
| 97 | lr_val |= ICH_LR_PENDING_BIT; |
| 98 | if (lr_desc.state & LR_STATE_ACTIVE) |
| 99 | lr_val |= ICH_LR_ACTIVE_BIT; |
| 100 | if (lr_desc.state & LR_EOI_INT) |
| 101 | lr_val |= ICH_LR_EOI; |
Marc Zyngier | fb182cf | 2015-06-08 15:37:26 +0100 | [diff] [blame] | 102 | if (lr_desc.state & LR_HW) { |
| 103 | lr_val |= ICH_LR_HW; |
| 104 | lr_val |= ((u64)lr_desc.hwirq) << ICH_LR_PHYS_ID_SHIFT; |
| 105 | } |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 106 | |
Marc Zyngier | 1b8e83c | 2016-02-17 10:25:05 +0000 | [diff] [blame] | 107 | vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = lr_val; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 108 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 109 | if (!(lr_desc.state & LR_STATE_MASK)) |
| 110 | vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr |= (1U << lr); |
Christoffer Dall | ae70593 | 2015-03-13 17:02:56 +0000 | [diff] [blame] | 111 | else |
| 112 | vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr &= ~(1U << lr); |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static u64 vgic_v3_get_elrsr(const struct kvm_vcpu *vcpu) |
| 116 | { |
| 117 | return vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr; |
| 118 | } |
| 119 | |
| 120 | static u64 vgic_v3_get_eisr(const struct kvm_vcpu *vcpu) |
| 121 | { |
| 122 | return vcpu->arch.vgic_cpu.vgic_v3.vgic_eisr; |
| 123 | } |
| 124 | |
Christoffer Dall | ae70593 | 2015-03-13 17:02:56 +0000 | [diff] [blame] | 125 | static void vgic_v3_clear_eisr(struct kvm_vcpu *vcpu) |
| 126 | { |
| 127 | vcpu->arch.vgic_cpu.vgic_v3.vgic_eisr = 0; |
| 128 | } |
| 129 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 130 | static u32 vgic_v3_get_interrupt_status(const struct kvm_vcpu *vcpu) |
| 131 | { |
| 132 | u32 misr = vcpu->arch.vgic_cpu.vgic_v3.vgic_misr; |
| 133 | u32 ret = 0; |
| 134 | |
| 135 | if (misr & ICH_MISR_EOI) |
| 136 | ret |= INT_STATUS_EOI; |
| 137 | if (misr & ICH_MISR_U) |
| 138 | ret |= INT_STATUS_UNDERFLOW; |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) |
| 144 | { |
| 145 | u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr; |
| 146 | |
| 147 | vmcrp->ctlr = (vmcr & ICH_VMCR_CTLR_MASK) >> ICH_VMCR_CTLR_SHIFT; |
| 148 | vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT; |
| 149 | vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT; |
| 150 | vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT; |
| 151 | } |
| 152 | |
| 153 | static void vgic_v3_enable_underflow(struct kvm_vcpu *vcpu) |
| 154 | { |
| 155 | vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr |= ICH_HCR_UIE; |
| 156 | } |
| 157 | |
| 158 | static void vgic_v3_disable_underflow(struct kvm_vcpu *vcpu) |
| 159 | { |
| 160 | vcpu->arch.vgic_cpu.vgic_v3.vgic_hcr &= ~ICH_HCR_UIE; |
| 161 | } |
| 162 | |
| 163 | static void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) |
| 164 | { |
| 165 | u32 vmcr; |
| 166 | |
| 167 | vmcr = (vmcrp->ctlr << ICH_VMCR_CTLR_SHIFT) & ICH_VMCR_CTLR_MASK; |
| 168 | vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK; |
| 169 | vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK; |
| 170 | vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK; |
| 171 | |
| 172 | vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr; |
| 173 | } |
| 174 | |
| 175 | static void vgic_v3_enable(struct kvm_vcpu *vcpu) |
| 176 | { |
Andre Przywara | 2f5fa41 | 2014-06-03 08:58:15 +0200 | [diff] [blame] | 177 | struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3; |
| 178 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 179 | /* |
| 180 | * By forcing VMCR to zero, the GIC will restore the binary |
| 181 | * points to their reset values. Anything else resets to zero |
| 182 | * anyway. |
| 183 | */ |
Andre Przywara | 2f5fa41 | 2014-06-03 08:58:15 +0200 | [diff] [blame] | 184 | vgic_v3->vgic_vmcr = 0; |
Pavel Fedin | c4cd4c1 | 2015-10-27 11:37:29 +0300 | [diff] [blame] | 185 | vgic_v3->vgic_elrsr = ~0; |
Andre Przywara | 2f5fa41 | 2014-06-03 08:58:15 +0200 | [diff] [blame] | 186 | |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 187 | /* |
| 188 | * If we are emulating a GICv3, we do it in an non-GICv2-compatible |
| 189 | * way, so we force SRE to 1 to demonstrate this to the guest. |
| 190 | * This goes with the spec allowing the value to be RAO/WI. |
| 191 | */ |
| 192 | if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) |
| 193 | vgic_v3->vgic_sre = ICC_SRE_EL1_SRE; |
| 194 | else |
| 195 | vgic_v3->vgic_sre = 0; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 196 | |
| 197 | /* Get the show on the road... */ |
Andre Przywara | 2f5fa41 | 2014-06-03 08:58:15 +0200 | [diff] [blame] | 198 | vgic_v3->vgic_hcr = ICH_HCR_EN; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static const struct vgic_ops vgic_v3_ops = { |
| 202 | .get_lr = vgic_v3_get_lr, |
| 203 | .set_lr = vgic_v3_set_lr, |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 204 | .get_elrsr = vgic_v3_get_elrsr, |
| 205 | .get_eisr = vgic_v3_get_eisr, |
Christoffer Dall | ae70593 | 2015-03-13 17:02:56 +0000 | [diff] [blame] | 206 | .clear_eisr = vgic_v3_clear_eisr, |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 207 | .get_interrupt_status = vgic_v3_get_interrupt_status, |
| 208 | .enable_underflow = vgic_v3_enable_underflow, |
| 209 | .disable_underflow = vgic_v3_disable_underflow, |
| 210 | .get_vmcr = vgic_v3_get_vmcr, |
| 211 | .set_vmcr = vgic_v3_set_vmcr, |
| 212 | .enable = vgic_v3_enable, |
| 213 | }; |
| 214 | |
| 215 | static struct vgic_params vgic_v3_params; |
| 216 | |
Marc Zyngier | 0d98d00 | 2016-03-03 15:43:58 +0000 | [diff] [blame] | 217 | static void vgic_cpu_init_lrs(void *params) |
| 218 | { |
| 219 | kvm_call_hyp(__vgic_v3_init_lrs); |
| 220 | } |
| 221 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 222 | /** |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 223 | * vgic_v3_probe - probe for a GICv3 compatible interrupt controller |
| 224 | * @gic_kvm_info: pointer to the GIC description |
| 225 | * @ops: address of a pointer to the GICv3 operations |
| 226 | * @params: address of a pointer to HW-specific parameters |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 227 | * |
| 228 | * Returns 0 if a GICv3 has been found, with the low level operations |
| 229 | * in *ops and the HW parameters in *params. Returns an error code |
| 230 | * otherwise. |
| 231 | */ |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 232 | int vgic_v3_probe(const struct gic_kvm_info *gic_kvm_info, |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 233 | const struct vgic_ops **ops, |
| 234 | const struct vgic_params **params) |
| 235 | { |
| 236 | int ret = 0; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 237 | struct vgic_params *vgic = &vgic_v3_params; |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 238 | const struct resource *vcpu_res = &gic_kvm_info->vcpu; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 239 | |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 240 | vgic->maint_irq = gic_kvm_info->maint_irq; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 241 | |
| 242 | ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2); |
| 243 | |
| 244 | /* |
| 245 | * The ListRegs field is 5 bits, but there is a architectural |
| 246 | * maximum of 16 list registers. Just ignore bit 4... |
| 247 | */ |
| 248 | vgic->nr_lr = (ich_vtr_el2 & 0xf) + 1; |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 249 | vgic->can_emulate_gicv2 = false; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 250 | |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 251 | if (!vcpu_res->start) { |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 252 | kvm_info("GICv3: no GICV resource entry\n"); |
| 253 | vgic->vcpu_base = 0; |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 254 | } else if (!PAGE_ALIGNED(vcpu_res->start)) { |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 255 | pr_warn("GICV physical address 0x%llx not page aligned\n", |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 256 | (unsigned long long)vcpu_res->start); |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 257 | vgic->vcpu_base = 0; |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 258 | } else if (!PAGE_ALIGNED(resource_size(vcpu_res))) { |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 259 | pr_warn("GICV size 0x%llx not a multiple of page size 0x%lx\n", |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 260 | (unsigned long long)resource_size(vcpu_res), |
Marc Zyngier | fb3ec67 | 2014-07-31 11:42:18 +0100 | [diff] [blame] | 261 | PAGE_SIZE); |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 262 | } else { |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 263 | vgic->vcpu_base = vcpu_res->start; |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 264 | vgic->can_emulate_gicv2 = true; |
| 265 | kvm_register_device_ops(&kvm_arm_vgic_v2_ops, |
| 266 | KVM_DEV_TYPE_ARM_VGIC_V2); |
Marc Zyngier | fb3ec67 | 2014-07-31 11:42:18 +0100 | [diff] [blame] | 267 | } |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 268 | if (vgic->vcpu_base == 0) |
| 269 | kvm_info("disabling GICv2 emulation\n"); |
| 270 | kvm_register_device_ops(&kvm_arm_vgic_v3_ops, KVM_DEV_TYPE_ARM_VGIC_V3); |
Marc Zyngier | fb3ec67 | 2014-07-31 11:42:18 +0100 | [diff] [blame] | 271 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 272 | vgic->vctrl_base = NULL; |
| 273 | vgic->type = VGIC_V3; |
Ming Lei | ef74891 | 2015-09-02 14:31:21 +0800 | [diff] [blame] | 274 | vgic->max_gic_vcpus = VGIC_V3_MAX_CPUS; |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 275 | |
Julien Grall | 503a628 | 2016-04-11 16:32:59 +0100 | [diff] [blame^] | 276 | kvm_info("GICV base=0x%llx, IRQ=%d\n", |
| 277 | vgic->vcpu_base, vgic->maint_irq); |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 278 | |
Marc Zyngier | 0d98d00 | 2016-03-03 15:43:58 +0000 | [diff] [blame] | 279 | on_each_cpu(vgic_cpu_init_lrs, vgic, 1); |
| 280 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 281 | *ops = &vgic_v3_ops; |
| 282 | *params = vgic; |
| 283 | |
Marc Zyngier | b2fb1c0 | 2013-07-12 15:15:23 +0100 | [diff] [blame] | 284 | return ret; |
| 285 | } |