Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 2 | |
| 3 | #include <linux/irqchip/arm-gic-v3.h> |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 4 | #include <linux/irq.h> |
| 5 | #include <linux/irqdomain.h> |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 6 | #include <linux/kvm.h> |
| 7 | #include <linux/kvm_host.h> |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 8 | #include <kvm/arm_vgic.h> |
Christoffer Dall | 923a2e3 | 2017-10-05 00:18:07 +0200 | [diff] [blame] | 9 | #include <asm/kvm_hyp.h> |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 10 | #include <asm/kvm_mmu.h> |
| 11 | #include <asm/kvm_asm.h> |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 12 | |
| 13 | #include "vgic.h" |
| 14 | |
Marc Zyngier | abf5576 | 2017-06-09 12:49:45 +0100 | [diff] [blame] | 15 | static bool group0_trap; |
Marc Zyngier | 9c7bfc2 | 2017-06-09 12:49:40 +0100 | [diff] [blame] | 16 | static bool group1_trap; |
Marc Zyngier | ff89511 | 2017-06-09 12:49:53 +0100 | [diff] [blame] | 17 | static bool common_trap; |
Marc Zyngier | a754605 | 2017-10-27 15:28:54 +0100 | [diff] [blame] | 18 | static bool gicv4_enable; |
Marc Zyngier | 9c7bfc2 | 2017-06-09 12:49:40 +0100 | [diff] [blame] | 19 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 20 | void vgic_v3_set_underflow(struct kvm_vcpu *vcpu) |
| 21 | { |
| 22 | struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3; |
| 23 | |
| 24 | cpuif->vgic_hcr |= ICH_HCR_UIE; |
| 25 | } |
| 26 | |
Christoffer Dall | af06149 | 2016-12-29 15:44:27 +0100 | [diff] [blame] | 27 | static bool lr_signals_eoi_mi(u64 lr_val) |
| 28 | { |
| 29 | return !(lr_val & ICH_LR_STATE) && (lr_val & ICH_LR_EOI) && |
| 30 | !(lr_val & ICH_LR_HW); |
| 31 | } |
| 32 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 33 | void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu) |
| 34 | { |
Christoffer Dall | 8ac76ef | 2017-03-18 13:48:42 +0100 | [diff] [blame] | 35 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 36 | struct vgic_v3_cpu_if *cpuif = &vgic_cpu->vgic_v3; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 37 | u32 model = vcpu->kvm->arch.vgic.vgic_model; |
| 38 | int lr; |
Jia He | d0823cb | 2018-08-03 21:57:04 +0800 | [diff] [blame] | 39 | |
| 40 | DEBUG_SPINLOCK_BUG_ON(!irqs_disabled()); |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 41 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 42 | cpuif->vgic_hcr &= ~ICH_HCR_UIE; |
Christoffer Dall | af06149 | 2016-12-29 15:44:27 +0100 | [diff] [blame] | 43 | |
Christoffer Dall | fc5d1f1 | 2018-12-01 08:41:28 -0800 | [diff] [blame] | 44 | for (lr = 0; lr < cpuif->used_lrs; lr++) { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 45 | u64 val = cpuif->vgic_lr[lr]; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 46 | u32 intid, cpuid; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 47 | struct vgic_irq *irq; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 48 | bool is_v2_sgi = false; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 49 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 50 | cpuid = val & GICH_LR_PHYSID_CPUID; |
| 51 | cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; |
| 52 | |
| 53 | if (model == KVM_DEV_TYPE_ARM_VGIC_V3) { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 54 | intid = val & ICH_LR_VIRTUAL_ID_MASK; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 55 | } else { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 56 | intid = val & GICH_LR_VIRTUALID; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 57 | is_v2_sgi = vgic_irq_is_sgi(intid); |
| 58 | } |
Christoffer Dall | af06149 | 2016-12-29 15:44:27 +0100 | [diff] [blame] | 59 | |
| 60 | /* Notify fds when the guest EOI'ed a level-triggered IRQ */ |
| 61 | if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid)) |
| 62 | kvm_notify_acked_irq(vcpu->kvm, 0, |
| 63 | intid - VGIC_NR_PRIVATE_IRQS); |
| 64 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 65 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid); |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 66 | if (!irq) /* An LPI could have been unmapped. */ |
| 67 | continue; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 68 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 69 | raw_spin_lock(&irq->irq_lock); |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 70 | |
| 71 | /* Always preserve the active bit */ |
| 72 | irq->active = !!(val & ICH_LR_ACTIVE_BIT); |
| 73 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 74 | if (irq->active && is_v2_sgi) |
| 75 | irq->active_source = cpuid; |
| 76 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 77 | /* Edge is the only case where we preserve the pending bit */ |
| 78 | if (irq->config == VGIC_CONFIG_EDGE && |
| 79 | (val & ICH_LR_PENDING_BIT)) { |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 80 | irq->pending_latch = true; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 81 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 82 | if (is_v2_sgi) |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 83 | irq->source |= (1 << cpuid); |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Marc Zyngier | 637d122 | 2016-05-25 15:26:36 +0100 | [diff] [blame] | 86 | /* |
| 87 | * Clear soft pending state when level irqs have been acked. |
Marc Zyngier | 637d122 | 2016-05-25 15:26:36 +0100 | [diff] [blame] | 88 | */ |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 89 | if (irq->config == VGIC_CONFIG_LEVEL && !(val & ICH_LR_STATE)) |
| 90 | irq->pending_latch = false; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 91 | |
Christoffer Dall | e40cc57 | 2017-08-29 10:40:44 +0200 | [diff] [blame] | 92 | /* |
| 93 | * Level-triggered mapped IRQs are special because we only |
| 94 | * observe rising edges as input to the VGIC. |
| 95 | * |
| 96 | * If the guest never acked the interrupt we have to sample |
| 97 | * the physical line and set the line level, because the |
| 98 | * device state could have changed or we simply need to |
| 99 | * process the still pending interrupt later. |
| 100 | * |
| 101 | * If this causes us to lower the level, we have to also clear |
| 102 | * the physical active state, since we will otherwise never be |
| 103 | * told when the interrupt becomes asserted again. |
| 104 | */ |
| 105 | if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT)) { |
| 106 | irq->line_level = vgic_get_phys_line_level(irq); |
| 107 | |
| 108 | if (!irq->line_level) |
| 109 | vgic_irq_set_phys_active(irq, false); |
| 110 | } |
| 111 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 112 | raw_spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 113 | vgic_put_irq(vcpu->kvm, irq); |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 114 | } |
Christoffer Dall | 8ac76ef | 2017-03-18 13:48:42 +0100 | [diff] [blame] | 115 | |
Christoffer Dall | fc5d1f1 | 2018-12-01 08:41:28 -0800 | [diff] [blame] | 116 | cpuif->used_lrs = 0; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* Requires the irq to be locked already */ |
| 120 | void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr) |
| 121 | { |
| 122 | u32 model = vcpu->kvm->arch.vgic.vgic_model; |
| 123 | u64 val = irq->intid; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 124 | bool allow_pending = true, is_v2_sgi; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 125 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 126 | is_v2_sgi = (vgic_irq_is_sgi(irq->intid) && |
| 127 | model == KVM_DEV_TYPE_ARM_VGIC_V2); |
| 128 | |
| 129 | if (irq->active) { |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 130 | val |= ICH_LR_ACTIVE_BIT; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 131 | if (is_v2_sgi) |
| 132 | val |= irq->active_source << GICH_LR_PHYSID_CPUID_SHIFT; |
| 133 | if (vgic_irq_is_multi_sgi(irq)) { |
| 134 | allow_pending = false; |
| 135 | val |= ICH_LR_EOI; |
| 136 | } |
| 137 | } |
Marc Zyngier | 67b5b67 | 2018-03-09 14:59:40 +0000 | [diff] [blame] | 138 | |
| 139 | if (irq->hw) { |
| 140 | val |= ICH_LR_HW; |
| 141 | val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT; |
| 142 | /* |
| 143 | * Never set pending+active on a HW interrupt, as the |
| 144 | * pending state is kept at the physical distributor |
| 145 | * level. |
| 146 | */ |
| 147 | if (irq->active) |
| 148 | allow_pending = false; |
| 149 | } else { |
| 150 | if (irq->config == VGIC_CONFIG_LEVEL) { |
| 151 | val |= ICH_LR_EOI; |
| 152 | |
| 153 | /* |
| 154 | * Software resampling doesn't work very well |
| 155 | * if we allow P+A, so let's not do that. |
| 156 | */ |
| 157 | if (irq->active) |
| 158 | allow_pending = false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (allow_pending && irq_is_pending(irq)) { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 163 | val |= ICH_LR_PENDING_BIT; |
| 164 | |
| 165 | if (irq->config == VGIC_CONFIG_EDGE) |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 166 | irq->pending_latch = false; |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 167 | |
| 168 | if (vgic_irq_is_sgi(irq->intid) && |
| 169 | model == KVM_DEV_TYPE_ARM_VGIC_V2) { |
| 170 | u32 src = ffs(irq->source); |
| 171 | |
Marc Zyngier | 82e40f5 | 2019-08-28 11:10:16 +0100 | [diff] [blame] | 172 | if (WARN_RATELIMIT(!src, "No SGI source for INTID %d\n", |
| 173 | irq->intid)) |
| 174 | return; |
| 175 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 176 | val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT; |
| 177 | irq->source &= ~(1 << (src - 1)); |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 178 | if (irq->source) { |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 179 | irq->pending_latch = true; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 180 | val |= ICH_LR_EOI; |
| 181 | } |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 185 | /* |
Christoffer Dall | e40cc57 | 2017-08-29 10:40:44 +0200 | [diff] [blame] | 186 | * Level-triggered mapped IRQs are special because we only observe |
| 187 | * rising edges as input to the VGIC. We therefore lower the line |
| 188 | * level here, so that we can take new virtual IRQs. See |
| 189 | * vgic_v3_fold_lr_state for more info. |
| 190 | */ |
| 191 | if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT)) |
| 192 | irq->line_level = false; |
| 193 | |
Christoffer Dall | 8732209 | 2018-07-16 15:06:22 +0200 | [diff] [blame] | 194 | if (irq->group) |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 195 | val |= ICH_LR_GROUP; |
| 196 | |
| 197 | val |= (u64)irq->priority << ICH_LR_PRIORITY_SHIFT; |
| 198 | |
| 199 | vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = val; |
| 200 | } |
| 201 | |
| 202 | void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr) |
| 203 | { |
| 204 | vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = 0; |
| 205 | } |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 206 | |
| 207 | void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) |
| 208 | { |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 209 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
Christoffer Dall | 28232a4 | 2017-05-20 14:12:34 +0200 | [diff] [blame] | 210 | u32 model = vcpu->kvm->arch.vgic.vgic_model; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 211 | u32 vmcr; |
| 212 | |
Christoffer Dall | 28232a4 | 2017-05-20 14:12:34 +0200 | [diff] [blame] | 213 | if (model == KVM_DEV_TYPE_ARM_VGIC_V2) { |
| 214 | vmcr = (vmcrp->ackctl << ICH_VMCR_ACK_CTL_SHIFT) & |
| 215 | ICH_VMCR_ACK_CTL_MASK; |
| 216 | vmcr |= (vmcrp->fiqen << ICH_VMCR_FIQ_EN_SHIFT) & |
| 217 | ICH_VMCR_FIQ_EN_MASK; |
| 218 | } else { |
| 219 | /* |
| 220 | * When emulating GICv3 on GICv3 with SRE=1 on the |
| 221 | * VFIQEn bit is RES1 and the VAckCtl bit is RES0. |
| 222 | */ |
| 223 | vmcr = ICH_VMCR_FIQ_EN_MASK; |
| 224 | } |
| 225 | |
| 226 | vmcr |= (vmcrp->cbpr << ICH_VMCR_CBPR_SHIFT) & ICH_VMCR_CBPR_MASK; |
| 227 | vmcr |= (vmcrp->eoim << ICH_VMCR_EOIM_SHIFT) & ICH_VMCR_EOIM_MASK; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 228 | vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK; |
| 229 | vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK; |
| 230 | vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK; |
Vijaya Kumar K | 5fb247d | 2017-01-26 19:50:50 +0530 | [diff] [blame] | 231 | vmcr |= (vmcrp->grpen0 << ICH_VMCR_ENG0_SHIFT) & ICH_VMCR_ENG0_MASK; |
| 232 | vmcr |= (vmcrp->grpen1 << ICH_VMCR_ENG1_SHIFT) & ICH_VMCR_ENG1_MASK; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 233 | |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 234 | cpu_if->vgic_vmcr = vmcr; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) |
| 238 | { |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 239 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
Christoffer Dall | 28232a4 | 2017-05-20 14:12:34 +0200 | [diff] [blame] | 240 | u32 model = vcpu->kvm->arch.vgic.vgic_model; |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 241 | u32 vmcr; |
| 242 | |
| 243 | vmcr = cpu_if->vgic_vmcr; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 244 | |
Christoffer Dall | 28232a4 | 2017-05-20 14:12:34 +0200 | [diff] [blame] | 245 | if (model == KVM_DEV_TYPE_ARM_VGIC_V2) { |
| 246 | vmcrp->ackctl = (vmcr & ICH_VMCR_ACK_CTL_MASK) >> |
| 247 | ICH_VMCR_ACK_CTL_SHIFT; |
| 248 | vmcrp->fiqen = (vmcr & ICH_VMCR_FIQ_EN_MASK) >> |
| 249 | ICH_VMCR_FIQ_EN_SHIFT; |
| 250 | } else { |
| 251 | /* |
| 252 | * When emulating GICv3 on GICv3 with SRE=1 on the |
| 253 | * VFIQEn bit is RES1 and the VAckCtl bit is RES0. |
| 254 | */ |
| 255 | vmcrp->fiqen = 1; |
| 256 | vmcrp->ackctl = 0; |
| 257 | } |
| 258 | |
| 259 | vmcrp->cbpr = (vmcr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT; |
| 260 | vmcrp->eoim = (vmcr & ICH_VMCR_EOIM_MASK) >> ICH_VMCR_EOIM_SHIFT; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 261 | vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT; |
| 262 | vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT; |
| 263 | vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT; |
Vijaya Kumar K | 5fb247d | 2017-01-26 19:50:50 +0530 | [diff] [blame] | 264 | vmcrp->grpen0 = (vmcr & ICH_VMCR_ENG0_MASK) >> ICH_VMCR_ENG0_SHIFT; |
| 265 | vmcrp->grpen1 = (vmcr & ICH_VMCR_ENG1_MASK) >> ICH_VMCR_ENG1_SHIFT; |
Andre Przywara | e4823a7 | 2015-12-03 11:47:37 +0000 | [diff] [blame] | 266 | } |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 267 | |
Andre Przywara | 0aa1de5 | 2016-07-15 12:43:29 +0100 | [diff] [blame] | 268 | #define INITIAL_PENDBASER_VALUE \ |
| 269 | (GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWb) | \ |
| 270 | GIC_BASER_CACHEABILITY(GICR_PENDBASER, OUTER, SameAsInner) | \ |
| 271 | GIC_BASER_SHAREABILITY(GICR_PENDBASER, InnerShareable)) |
| 272 | |
Eric Auger | ad275b8b | 2015-12-21 18:09:38 +0100 | [diff] [blame] | 273 | void vgic_v3_enable(struct kvm_vcpu *vcpu) |
| 274 | { |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 275 | struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3; |
| 276 | |
| 277 | /* |
| 278 | * By forcing VMCR to zero, the GIC will restore the binary |
| 279 | * points to their reset values. Anything else resets to zero |
| 280 | * anyway. |
| 281 | */ |
| 282 | vgic_v3->vgic_vmcr = 0; |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * If we are emulating a GICv3, we do it in an non-GICv2-compatible |
| 286 | * way, so we force SRE to 1 to demonstrate this to the guest. |
Marc Zyngier | 4dfc050 | 2017-02-21 11:32:47 +0000 | [diff] [blame] | 287 | * Also, we don't support any form of IRQ/FIQ bypass. |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 288 | * This goes with the spec allowing the value to be RAO/WI. |
| 289 | */ |
Andre Przywara | 0aa1de5 | 2016-07-15 12:43:29 +0100 | [diff] [blame] | 290 | if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) { |
Marc Zyngier | 4dfc050 | 2017-02-21 11:32:47 +0000 | [diff] [blame] | 291 | vgic_v3->vgic_sre = (ICC_SRE_EL1_DIB | |
| 292 | ICC_SRE_EL1_DFB | |
| 293 | ICC_SRE_EL1_SRE); |
Andre Przywara | 0aa1de5 | 2016-07-15 12:43:29 +0100 | [diff] [blame] | 294 | vcpu->arch.vgic_cpu.pendbaser = INITIAL_PENDBASER_VALUE; |
| 295 | } else { |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 296 | vgic_v3->vgic_sre = 0; |
Andre Przywara | 0aa1de5 | 2016-07-15 12:43:29 +0100 | [diff] [blame] | 297 | } |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 298 | |
Vijaya Kumar K | d017d7b | 2017-01-26 19:50:51 +0530 | [diff] [blame] | 299 | vcpu->arch.vgic_cpu.num_id_bits = (kvm_vgic_global_state.ich_vtr_el2 & |
| 300 | ICH_VTR_ID_BITS_MASK) >> |
| 301 | ICH_VTR_ID_BITS_SHIFT; |
| 302 | vcpu->arch.vgic_cpu.num_pri_bits = ((kvm_vgic_global_state.ich_vtr_el2 & |
| 303 | ICH_VTR_PRI_BITS_MASK) >> |
| 304 | ICH_VTR_PRI_BITS_SHIFT) + 1; |
| 305 | |
Eric Auger | f7b6985 | 2015-12-02 10:30:13 +0100 | [diff] [blame] | 306 | /* Get the show on the road... */ |
| 307 | vgic_v3->vgic_hcr = ICH_HCR_EN; |
Marc Zyngier | abf5576 | 2017-06-09 12:49:45 +0100 | [diff] [blame] | 308 | if (group0_trap) |
| 309 | vgic_v3->vgic_hcr |= ICH_HCR_TALL0; |
Marc Zyngier | 9c7bfc2 | 2017-06-09 12:49:40 +0100 | [diff] [blame] | 310 | if (group1_trap) |
| 311 | vgic_v3->vgic_hcr |= ICH_HCR_TALL1; |
Marc Zyngier | ff89511 | 2017-06-09 12:49:53 +0100 | [diff] [blame] | 312 | if (common_trap) |
| 313 | vgic_v3->vgic_hcr |= ICH_HCR_TC; |
Eric Auger | ad275b8b | 2015-12-21 18:09:38 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 316 | int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq) |
| 317 | { |
| 318 | struct kvm_vcpu *vcpu; |
| 319 | int byte_offset, bit_nr; |
| 320 | gpa_t pendbase, ptr; |
| 321 | bool status; |
| 322 | u8 val; |
| 323 | int ret; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 324 | unsigned long flags; |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 325 | |
| 326 | retry: |
| 327 | vcpu = irq->target_vcpu; |
| 328 | if (!vcpu) |
| 329 | return 0; |
| 330 | |
| 331 | pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); |
| 332 | |
| 333 | byte_offset = irq->intid / BITS_PER_BYTE; |
| 334 | bit_nr = irq->intid % BITS_PER_BYTE; |
| 335 | ptr = pendbase + byte_offset; |
| 336 | |
Andre Przywara | 711702b | 2018-05-11 15:20:15 +0100 | [diff] [blame] | 337 | ret = kvm_read_guest_lock(kvm, ptr, &val, 1); |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 338 | if (ret) |
| 339 | return ret; |
| 340 | |
| 341 | status = val & (1 << bit_nr); |
| 342 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 343 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 344 | if (irq->target_vcpu != vcpu) { |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 345 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 346 | goto retry; |
| 347 | } |
| 348 | irq->pending_latch = status; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 349 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 350 | |
| 351 | if (status) { |
| 352 | /* clear consumed data */ |
| 353 | val &= ~(1 << bit_nr); |
Marc Zyngier | a6ecfb1 | 2019-03-19 12:47:11 +0000 | [diff] [blame] | 354 | ret = kvm_write_guest_lock(kvm, ptr, &val, 1); |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 355 | if (ret) |
| 356 | return ret; |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 361 | /* |
| 362 | * The deactivation of the doorbell interrupt will trigger the |
| 363 | * unmapping of the associated vPE. |
| 364 | */ |
| 365 | static void unmap_all_vpes(struct vgic_dist *dist) |
| 366 | { |
| 367 | struct irq_desc *desc; |
| 368 | int i; |
| 369 | |
| 370 | for (i = 0; i < dist->its_vm.nr_vpes; i++) { |
| 371 | desc = irq_to_desc(dist->its_vm.vpes[i]->irq); |
| 372 | irq_domain_deactivate_irq(irq_desc_get_irq_data(desc)); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | static void map_all_vpes(struct vgic_dist *dist) |
| 377 | { |
| 378 | struct irq_desc *desc; |
| 379 | int i; |
| 380 | |
| 381 | for (i = 0; i < dist->its_vm.nr_vpes; i++) { |
| 382 | desc = irq_to_desc(dist->its_vm.vpes[i]->irq); |
| 383 | irq_domain_activate_irq(irq_desc_get_irq_data(desc), false); |
| 384 | } |
| 385 | } |
| 386 | |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 387 | /** |
Zenghui Yu | bad36e4 | 2019-10-29 15:19:18 +0800 | [diff] [blame] | 388 | * vgic_v3_save_pending_tables - Save the pending tables into guest RAM |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 389 | * kvm lock and all vcpu lock must be held |
| 390 | */ |
| 391 | int vgic_v3_save_pending_tables(struct kvm *kvm) |
| 392 | { |
| 393 | struct vgic_dist *dist = &kvm->arch.vgic; |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 394 | struct vgic_irq *irq; |
Zenghui Yu | ca185b2 | 2019-10-29 15:19:19 +0800 | [diff] [blame] | 395 | gpa_t last_ptr = ~(gpa_t)0; |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 396 | bool vlpi_avail = false; |
| 397 | int ret = 0; |
Marc Zyngier | ddb4b01 | 2017-11-16 17:58:16 +0000 | [diff] [blame] | 398 | u8 val; |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 399 | |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 400 | if (unlikely(!vgic_initialized(kvm))) |
| 401 | return -ENXIO; |
| 402 | |
| 403 | /* |
| 404 | * A preparation for getting any VLPI states. |
| 405 | * The above vgic initialized check also ensures that the allocation |
| 406 | * and enabling of the doorbells have already been done. |
| 407 | */ |
| 408 | if (kvm_vgic_global_state.has_gicv4_1) { |
| 409 | unmap_all_vpes(dist); |
| 410 | vlpi_avail = true; |
| 411 | } |
| 412 | |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 413 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { |
| 414 | int byte_offset, bit_nr; |
| 415 | struct kvm_vcpu *vcpu; |
| 416 | gpa_t pendbase, ptr; |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 417 | bool is_pending; |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 418 | bool stored; |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 419 | |
| 420 | vcpu = irq->target_vcpu; |
| 421 | if (!vcpu) |
| 422 | continue; |
| 423 | |
| 424 | pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); |
| 425 | |
| 426 | byte_offset = irq->intid / BITS_PER_BYTE; |
| 427 | bit_nr = irq->intid % BITS_PER_BYTE; |
| 428 | ptr = pendbase + byte_offset; |
| 429 | |
Zenghui Yu | ca185b2 | 2019-10-29 15:19:19 +0800 | [diff] [blame] | 430 | if (ptr != last_ptr) { |
Andre Przywara | 711702b | 2018-05-11 15:20:15 +0100 | [diff] [blame] | 431 | ret = kvm_read_guest_lock(kvm, ptr, &val, 1); |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 432 | if (ret) |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 433 | goto out; |
Zenghui Yu | ca185b2 | 2019-10-29 15:19:19 +0800 | [diff] [blame] | 434 | last_ptr = ptr; |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | stored = val & (1U << bit_nr); |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 438 | |
| 439 | is_pending = irq->pending_latch; |
| 440 | |
| 441 | if (irq->hw && vlpi_avail) |
| 442 | vgic_v4_get_vlpi_state(irq, &is_pending); |
| 443 | |
| 444 | if (stored == is_pending) |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 445 | continue; |
| 446 | |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 447 | if (is_pending) |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 448 | val |= 1 << bit_nr; |
| 449 | else |
| 450 | val &= ~(1 << bit_nr); |
| 451 | |
Marc Zyngier | a6ecfb1 | 2019-03-19 12:47:11 +0000 | [diff] [blame] | 452 | ret = kvm_write_guest_lock(kvm, ptr, &val, 1); |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 453 | if (ret) |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 454 | goto out; |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 455 | } |
Shenming Lu | f66b7b1 | 2021-03-22 14:01:56 +0800 | [diff] [blame^] | 456 | |
| 457 | out: |
| 458 | if (vlpi_avail) |
| 459 | map_all_vpes(dist); |
| 460 | |
| 461 | return ret; |
Eric Auger | 2807712 | 2017-01-09 16:28:27 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Eric Auger | 028bf27 | 2018-05-22 09:55:11 +0200 | [diff] [blame] | 464 | /** |
| 465 | * vgic_v3_rdist_overlap - check if a region overlaps with any |
| 466 | * existing redistributor region |
| 467 | * |
| 468 | * @kvm: kvm handle |
| 469 | * @base: base of the region |
| 470 | * @size: size of region |
| 471 | * |
| 472 | * Return: true if there is an overlap |
| 473 | */ |
| 474 | bool vgic_v3_rdist_overlap(struct kvm *kvm, gpa_t base, size_t size) |
| 475 | { |
| 476 | struct vgic_dist *d = &kvm->arch.vgic; |
| 477 | struct vgic_redist_region *rdreg; |
| 478 | |
| 479 | list_for_each_entry(rdreg, &d->rd_regions, list) { |
| 480 | if ((base + size > rdreg->base) && |
| 481 | (base < rdreg->base + vgic_v3_rd_region_size(kvm, rdreg))) |
| 482 | return true; |
| 483 | } |
| 484 | return false; |
| 485 | } |
| 486 | |
Christoffer Dall | 9a746d7 | 2017-05-08 12:23:51 +0200 | [diff] [blame] | 487 | /* |
| 488 | * Check for overlapping regions and for regions crossing the end of memory |
| 489 | * for base addresses which have already been set. |
| 490 | */ |
| 491 | bool vgic_v3_check_base(struct kvm *kvm) |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 492 | { |
| 493 | struct vgic_dist *d = &kvm->arch.vgic; |
Eric Auger | 028bf27 | 2018-05-22 09:55:11 +0200 | [diff] [blame] | 494 | struct vgic_redist_region *rdreg; |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 495 | |
Christoffer Dall | 9a746d7 | 2017-05-08 12:23:51 +0200 | [diff] [blame] | 496 | if (!IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) && |
| 497 | d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base) |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 498 | return false; |
Christoffer Dall | 9a746d7 | 2017-05-08 12:23:51 +0200 | [diff] [blame] | 499 | |
Eric Auger | 028bf27 | 2018-05-22 09:55:11 +0200 | [diff] [blame] | 500 | list_for_each_entry(rdreg, &d->rd_regions, list) { |
| 501 | if (rdreg->base + vgic_v3_rd_region_size(kvm, rdreg) < |
| 502 | rdreg->base) |
| 503 | return false; |
| 504 | } |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 505 | |
Eric Auger | 028bf27 | 2018-05-22 09:55:11 +0200 | [diff] [blame] | 506 | if (IS_VGIC_ADDR_UNDEF(d->vgic_dist_base)) |
Christoffer Dall | 9a746d7 | 2017-05-08 12:23:51 +0200 | [diff] [blame] | 507 | return true; |
| 508 | |
Eric Auger | 028bf27 | 2018-05-22 09:55:11 +0200 | [diff] [blame] | 509 | return !vgic_v3_rdist_overlap(kvm, d->vgic_dist_base, |
| 510 | KVM_VGIC_V3_DIST_SIZE); |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Eric Auger | dc52461 | 2018-05-22 09:55:09 +0200 | [diff] [blame] | 513 | /** |
| 514 | * vgic_v3_rdist_free_slot - Look up registered rdist regions and identify one |
| 515 | * which has free space to put a new rdist region. |
| 516 | * |
| 517 | * @rd_regions: redistributor region list head |
| 518 | * |
| 519 | * A redistributor regions maps n redistributors, n = region size / (2 x 64kB). |
| 520 | * Stride between redistributors is 0 and regions are filled in the index order. |
| 521 | * |
| 522 | * Return: the redist region handle, if any, that has space to map a new rdist |
| 523 | * region. |
| 524 | */ |
| 525 | struct vgic_redist_region *vgic_v3_rdist_free_slot(struct list_head *rd_regions) |
| 526 | { |
| 527 | struct vgic_redist_region *rdreg; |
| 528 | |
| 529 | list_for_each_entry(rdreg, rd_regions, list) { |
| 530 | if (!vgic_v3_redist_region_full(rdreg)) |
| 531 | return rdreg; |
| 532 | } |
| 533 | return NULL; |
| 534 | } |
| 535 | |
Eric Auger | 04c1109 | 2018-05-22 09:55:17 +0200 | [diff] [blame] | 536 | struct vgic_redist_region *vgic_v3_rdist_region_from_index(struct kvm *kvm, |
| 537 | u32 index) |
| 538 | { |
| 539 | struct list_head *rd_regions = &kvm->arch.vgic.rd_regions; |
| 540 | struct vgic_redist_region *rdreg; |
| 541 | |
| 542 | list_for_each_entry(rdreg, rd_regions, list) { |
| 543 | if (rdreg->index == index) |
| 544 | return rdreg; |
| 545 | } |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 550 | int vgic_v3_map_resources(struct kvm *kvm) |
| 551 | { |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 552 | struct vgic_dist *dist = &kvm->arch.vgic; |
Eric Auger | c957a6d | 2018-05-22 09:55:15 +0200 | [diff] [blame] | 553 | struct kvm_vcpu *vcpu; |
| 554 | int ret = 0; |
| 555 | int c; |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 556 | |
Eric Auger | c957a6d | 2018-05-22 09:55:15 +0200 | [diff] [blame] | 557 | kvm_for_each_vcpu(c, vcpu, kvm) { |
| 558 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 559 | |
| 560 | if (IS_VGIC_ADDR_UNDEF(vgic_cpu->rd_iodev.base_addr)) { |
| 561 | kvm_debug("vcpu %d redistributor base not set\n", c); |
Marc Zyngier | 101068b | 2020-12-27 14:28:34 +0000 | [diff] [blame] | 562 | return -ENXIO; |
Eric Auger | c957a6d | 2018-05-22 09:55:15 +0200 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base)) { |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 567 | kvm_err("Need to set vgic distributor addresses first\n"); |
Marc Zyngier | 101068b | 2020-12-27 14:28:34 +0000 | [diff] [blame] | 568 | return -ENXIO; |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | if (!vgic_v3_check_base(kvm)) { |
| 572 | kvm_err("VGIC redist and dist frames overlap\n"); |
Marc Zyngier | 101068b | 2020-12-27 14:28:34 +0000 | [diff] [blame] | 573 | return -EINVAL; |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * For a VGICv3 we require the userland to explicitly initialize |
| 578 | * the VGIC before we need to use it. |
| 579 | */ |
| 580 | if (!vgic_initialized(kvm)) { |
Marc Zyngier | 101068b | 2020-12-27 14:28:34 +0000 | [diff] [blame] | 581 | return -EBUSY; |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V3); |
| 585 | if (ret) { |
| 586 | kvm_err("Unable to register VGICv3 dist MMIO regions\n"); |
Marc Zyngier | 101068b | 2020-12-27 14:28:34 +0000 | [diff] [blame] | 587 | return ret; |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Marc Zyngier | 2291ff2 | 2020-03-04 20:33:27 +0000 | [diff] [blame] | 590 | if (kvm_vgic_global_state.has_gicv4_1) |
| 591 | vgic_v4_configure_vsgis(kvm); |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 592 | |
Marc Zyngier | 101068b | 2020-12-27 14:28:34 +0000 | [diff] [blame] | 593 | return 0; |
Eric Auger | b0442ee | 2015-12-21 15:04:42 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Marc Zyngier | 59da1cb | 2017-06-09 12:49:33 +0100 | [diff] [blame] | 596 | DEFINE_STATIC_KEY_FALSE(vgic_v3_cpuif_trap); |
| 597 | |
Marc Zyngier | e23f62f | 2017-06-09 12:49:46 +0100 | [diff] [blame] | 598 | static int __init early_group0_trap_cfg(char *buf) |
| 599 | { |
| 600 | return strtobool(buf, &group0_trap); |
| 601 | } |
| 602 | early_param("kvm-arm.vgic_v3_group0_trap", early_group0_trap_cfg); |
| 603 | |
Marc Zyngier | 182936e | 2017-06-09 12:49:41 +0100 | [diff] [blame] | 604 | static int __init early_group1_trap_cfg(char *buf) |
| 605 | { |
| 606 | return strtobool(buf, &group1_trap); |
| 607 | } |
| 608 | early_param("kvm-arm.vgic_v3_group1_trap", early_group1_trap_cfg); |
| 609 | |
Marc Zyngier | ff89511 | 2017-06-09 12:49:53 +0100 | [diff] [blame] | 610 | static int __init early_common_trap_cfg(char *buf) |
| 611 | { |
| 612 | return strtobool(buf, &common_trap); |
| 613 | } |
| 614 | early_param("kvm-arm.vgic_v3_common_trap", early_common_trap_cfg); |
| 615 | |
Marc Zyngier | a754605 | 2017-10-27 15:28:54 +0100 | [diff] [blame] | 616 | static int __init early_gicv4_enable(char *buf) |
| 617 | { |
| 618 | return strtobool(buf, &gicv4_enable); |
| 619 | } |
| 620 | early_param("kvm-arm.vgic_v4_enable", early_gicv4_enable); |
| 621 | |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 622 | /** |
Alexandru Elisei | 0ed5f5d | 2019-08-15 10:56:22 +0100 | [diff] [blame] | 623 | * vgic_v3_probe - probe for a VGICv3 compatible interrupt controller |
| 624 | * @info: pointer to the GIC description |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 625 | * |
Alexandru Elisei | 0ed5f5d | 2019-08-15 10:56:22 +0100 | [diff] [blame] | 626 | * Returns 0 if the VGICv3 has been probed successfully, returns an error code |
| 627 | * otherwise |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 628 | */ |
| 629 | int vgic_v3_probe(const struct gic_kvm_info *info) |
| 630 | { |
Marc Zyngier | b9d699e | 2021-03-05 18:52:52 +0000 | [diff] [blame] | 631 | u64 ich_vtr_el2 = kvm_call_hyp_ret(__vgic_v3_get_gic_config); |
Marc Zyngier | 9739f6e | 2021-03-05 18:52:53 +0000 | [diff] [blame] | 632 | bool has_v2; |
Andre Przywara | 42c8870 | 2016-07-15 12:43:23 +0100 | [diff] [blame] | 633 | int ret; |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 634 | |
Marc Zyngier | 9739f6e | 2021-03-05 18:52:53 +0000 | [diff] [blame] | 635 | has_v2 = ich_vtr_el2 >> 63; |
Marc Zyngier | b9d699e | 2021-03-05 18:52:52 +0000 | [diff] [blame] | 636 | ich_vtr_el2 = (u32)ich_vtr_el2; |
| 637 | |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 638 | /* |
Fuad Tabba | 656012c | 2020-04-01 15:03:10 +0100 | [diff] [blame] | 639 | * The ListRegs field is 5 bits, but there is an architectural |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 640 | * maximum of 16 list registers. Just ignore bit 4... |
| 641 | */ |
| 642 | kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1; |
| 643 | kvm_vgic_global_state.can_emulate_gicv2 = false; |
Vijaya Kumar K | d017d7b | 2017-01-26 19:50:51 +0530 | [diff] [blame] | 644 | kvm_vgic_global_state.ich_vtr_el2 = ich_vtr_el2; |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 645 | |
Marc Zyngier | a754605 | 2017-10-27 15:28:54 +0100 | [diff] [blame] | 646 | /* GICv4 support? */ |
| 647 | if (info->has_v4) { |
| 648 | kvm_vgic_global_state.has_gicv4 = gicv4_enable; |
Marc Zyngier | ae699ad | 2020-03-04 20:33:20 +0000 | [diff] [blame] | 649 | kvm_vgic_global_state.has_gicv4_1 = info->has_v4_1 && gicv4_enable; |
| 650 | kvm_info("GICv4%s support %sabled\n", |
| 651 | kvm_vgic_global_state.has_gicv4_1 ? ".1" : "", |
Marc Zyngier | a754605 | 2017-10-27 15:28:54 +0100 | [diff] [blame] | 652 | gicv4_enable ? "en" : "dis"); |
| 653 | } |
| 654 | |
Marc Zyngier | 9739f6e | 2021-03-05 18:52:53 +0000 | [diff] [blame] | 655 | kvm_vgic_global_state.vcpu_base = 0; |
| 656 | |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 657 | if (!info->vcpu.start) { |
| 658 | kvm_info("GICv3: no GICV resource entry\n"); |
Marc Zyngier | 9739f6e | 2021-03-05 18:52:53 +0000 | [diff] [blame] | 659 | } else if (!has_v2) { |
| 660 | pr_warn(FW_BUG "CPU interface incapable of MMIO access\n"); |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 661 | } else if (!PAGE_ALIGNED(info->vcpu.start)) { |
| 662 | pr_warn("GICV physical address 0x%llx not page aligned\n", |
| 663 | (unsigned long long)info->vcpu.start); |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 664 | } else { |
| 665 | kvm_vgic_global_state.vcpu_base = info->vcpu.start; |
| 666 | kvm_vgic_global_state.can_emulate_gicv2 = true; |
Andre Przywara | 42c8870 | 2016-07-15 12:43:23 +0100 | [diff] [blame] | 667 | ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2); |
| 668 | if (ret) { |
| 669 | kvm_err("Cannot register GICv2 KVM device.\n"); |
| 670 | return ret; |
| 671 | } |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 672 | kvm_info("vgic-v2@%llx\n", info->vcpu.start); |
| 673 | } |
Andre Przywara | 42c8870 | 2016-07-15 12:43:23 +0100 | [diff] [blame] | 674 | ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3); |
| 675 | if (ret) { |
| 676 | kvm_err("Cannot register GICv3 KVM device.\n"); |
| 677 | kvm_unregister_device_ops(KVM_DEV_TYPE_ARM_VGIC_V2); |
| 678 | return ret; |
| 679 | } |
| 680 | |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 681 | if (kvm_vgic_global_state.vcpu_base == 0) |
| 682 | kvm_info("disabling GICv2 emulation\n"); |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 683 | |
David Daney | 690a341 | 2017-06-09 12:49:48 +0100 | [diff] [blame] | 684 | if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_30115)) { |
| 685 | group0_trap = true; |
| 686 | group1_trap = true; |
| 687 | } |
David Daney | 690a341 | 2017-06-09 12:49:48 +0100 | [diff] [blame] | 688 | |
Marc Zyngier | ff89511 | 2017-06-09 12:49:53 +0100 | [diff] [blame] | 689 | if (group0_trap || group1_trap || common_trap) { |
Marc Zyngier | 2873b50 | 2017-06-09 12:49:54 +0100 | [diff] [blame] | 690 | kvm_info("GICv3 sysreg trapping enabled ([%s%s%s], reduced performance)\n", |
| 691 | group0_trap ? "G0" : "", |
| 692 | group1_trap ? "G1" : "", |
| 693 | common_trap ? "C" : ""); |
Marc Zyngier | 182936e | 2017-06-09 12:49:41 +0100 | [diff] [blame] | 694 | static_branch_enable(&vgic_v3_cpuif_trap); |
| 695 | } |
| 696 | |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 697 | kvm_vgic_global_state.vctrl_base = NULL; |
| 698 | kvm_vgic_global_state.type = VGIC_V3; |
| 699 | kvm_vgic_global_state.max_gic_vcpus = VGIC_V3_MAX_CPUS; |
| 700 | |
| 701 | return 0; |
| 702 | } |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 703 | |
| 704 | void vgic_v3_load(struct kvm_vcpu *vcpu) |
| 705 | { |
| 706 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
| 707 | |
Marc Zyngier | ff56761 | 2017-04-19 12:15:26 +0100 | [diff] [blame] | 708 | /* |
| 709 | * If dealing with a GICv2 emulation on GICv3, VMCR_EL2.VFIQen |
| 710 | * is dependent on ICC_SRE_EL1.SRE, and we have to perform the |
| 711 | * VMCR_EL2 save/restore in the world switch. |
| 712 | */ |
| 713 | if (likely(cpu_if->vgic_sre)) |
| 714 | kvm_call_hyp(__vgic_v3_write_vmcr, cpu_if->vgic_vmcr); |
Christoffer Dall | 923a2e3 | 2017-10-05 00:18:07 +0200 | [diff] [blame] | 715 | |
Andrew Scull | a071261 | 2020-09-15 11:46:43 +0100 | [diff] [blame] | 716 | kvm_call_hyp(__vgic_v3_restore_aprs, cpu_if); |
Christoffer Dall | 2d0e63e | 2017-10-05 17:19:19 +0200 | [diff] [blame] | 717 | |
| 718 | if (has_vhe()) |
Christoffer Dall | fc5d1f1 | 2018-12-01 08:41:28 -0800 | [diff] [blame] | 719 | __vgic_v3_activate_traps(cpu_if); |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 720 | |
| 721 | WARN_ON(vgic_v4_load(vcpu)); |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Marc Zyngier | 5eeaf10 | 2019-08-02 10:28:32 +0100 | [diff] [blame] | 724 | void vgic_v3_vmcr_sync(struct kvm_vcpu *vcpu) |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 725 | { |
| 726 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
| 727 | |
Marc Zyngier | ff56761 | 2017-04-19 12:15:26 +0100 | [diff] [blame] | 728 | if (likely(cpu_if->vgic_sre)) |
Marc Zyngier | 7aa8d14 | 2019-01-05 15:49:50 +0000 | [diff] [blame] | 729 | cpu_if->vgic_vmcr = kvm_call_hyp_ret(__vgic_v3_read_vmcr); |
Marc Zyngier | 5eeaf10 | 2019-08-02 10:28:32 +0100 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | void vgic_v3_put(struct kvm_vcpu *vcpu) |
| 733 | { |
Christoffer Dall | fc5d1f1 | 2018-12-01 08:41:28 -0800 | [diff] [blame] | 734 | struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; |
| 735 | |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 736 | WARN_ON(vgic_v4_put(vcpu, false)); |
| 737 | |
Marc Zyngier | 5eeaf10 | 2019-08-02 10:28:32 +0100 | [diff] [blame] | 738 | vgic_v3_vmcr_sync(vcpu); |
Christoffer Dall | 923a2e3 | 2017-10-05 00:18:07 +0200 | [diff] [blame] | 739 | |
Andrew Scull | a071261 | 2020-09-15 11:46:43 +0100 | [diff] [blame] | 740 | kvm_call_hyp(__vgic_v3_save_aprs, cpu_if); |
Christoffer Dall | 2d0e63e | 2017-10-05 17:19:19 +0200 | [diff] [blame] | 741 | |
| 742 | if (has_vhe()) |
Christoffer Dall | fc5d1f1 | 2018-12-01 08:41:28 -0800 | [diff] [blame] | 743 | __vgic_v3_deactivate_traps(cpu_if); |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 744 | } |