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