blob: a33d4366b326ba1ab7be7d21983f2b2110d953d9 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngier59529f62015-11-30 13:09:53 +00002
3#include <linux/irqchip/arm-gic-v3.h>
Shenming Luf66b7b12021-03-22 14:01:56 +08004#include <linux/irq.h>
5#include <linux/irqdomain.h>
Marc Zyngier59529f62015-11-30 13:09:53 +00006#include <linux/kvm.h>
7#include <linux/kvm_host.h>
Eric Auger90977732015-12-01 15:02:35 +01008#include <kvm/arm_vgic.h>
Christoffer Dall923a2e32017-10-05 00:18:07 +02009#include <asm/kvm_hyp.h>
Eric Auger90977732015-12-01 15:02:35 +010010#include <asm/kvm_mmu.h>
11#include <asm/kvm_asm.h>
Marc Zyngier59529f62015-11-30 13:09:53 +000012
13#include "vgic.h"
14
Marc Zyngierabf55762017-06-09 12:49:45 +010015static bool group0_trap;
Marc Zyngier9c7bfc22017-06-09 12:49:40 +010016static bool group1_trap;
Marc Zyngierff895112017-06-09 12:49:53 +010017static bool common_trap;
Marc Zyngier09247292021-10-10 16:09:08 +010018static bool dir_trap;
Marc Zyngiera7546052017-10-27 15:28:54 +010019static bool gicv4_enable;
Marc Zyngier9c7bfc22017-06-09 12:49:40 +010020
Marc Zyngier59529f62015-11-30 13:09:53 +000021void vgic_v3_set_underflow(struct kvm_vcpu *vcpu)
22{
23 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3;
24
25 cpuif->vgic_hcr |= ICH_HCR_UIE;
26}
27
Christoffer Dallaf061492016-12-29 15:44:27 +010028static bool lr_signals_eoi_mi(u64 lr_val)
29{
30 return !(lr_val & ICH_LR_STATE) && (lr_val & ICH_LR_EOI) &&
31 !(lr_val & ICH_LR_HW);
32}
33
Marc Zyngier59529f62015-11-30 13:09:53 +000034void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu)
35{
Christoffer Dall8ac76ef2017-03-18 13:48:42 +010036 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
37 struct vgic_v3_cpu_if *cpuif = &vgic_cpu->vgic_v3;
Marc Zyngier59529f62015-11-30 13:09:53 +000038 u32 model = vcpu->kvm->arch.vgic.vgic_model;
39 int lr;
Jia Hed0823cb2018-08-03 21:57:04 +080040
41 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
Marc Zyngier59529f62015-11-30 13:09:53 +000042
Marc Zyngier53692902018-04-18 10:39:04 +010043 cpuif->vgic_hcr &= ~ICH_HCR_UIE;
Christoffer Dallaf061492016-12-29 15:44:27 +010044
Christoffer Dallfc5d1f12018-12-01 08:41:28 -080045 for (lr = 0; lr < cpuif->used_lrs; lr++) {
Marc Zyngier59529f62015-11-30 13:09:53 +000046 u64 val = cpuif->vgic_lr[lr];
Marc Zyngier53692902018-04-18 10:39:04 +010047 u32 intid, cpuid;
Marc Zyngier59529f62015-11-30 13:09:53 +000048 struct vgic_irq *irq;
Marc Zyngier53692902018-04-18 10:39:04 +010049 bool is_v2_sgi = false;
Marc Zyngier3134cc82021-08-19 19:03:05 +010050 bool deactivated;
Marc Zyngier59529f62015-11-30 13:09:53 +000051
Marc Zyngier53692902018-04-18 10:39:04 +010052 cpuid = val & GICH_LR_PHYSID_CPUID;
53 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
54
55 if (model == KVM_DEV_TYPE_ARM_VGIC_V3) {
Marc Zyngier59529f62015-11-30 13:09:53 +000056 intid = val & ICH_LR_VIRTUAL_ID_MASK;
Marc Zyngier53692902018-04-18 10:39:04 +010057 } else {
Marc Zyngier59529f62015-11-30 13:09:53 +000058 intid = val & GICH_LR_VIRTUALID;
Marc Zyngier53692902018-04-18 10:39:04 +010059 is_v2_sgi = vgic_irq_is_sgi(intid);
60 }
Christoffer Dallaf061492016-12-29 15:44:27 +010061
62 /* Notify fds when the guest EOI'ed a level-triggered IRQ */
63 if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid))
64 kvm_notify_acked_irq(vcpu->kvm, 0,
65 intid - VGIC_NR_PRIVATE_IRQS);
66
Marc Zyngier59529f62015-11-30 13:09:53 +000067 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
Andre Przywara38024112016-07-15 12:43:33 +010068 if (!irq) /* An LPI could have been unmapped. */
69 continue;
Marc Zyngier59529f62015-11-30 13:09:53 +000070
Julien Thierry8fa3adb2019-01-07 15:06:15 +000071 raw_spin_lock(&irq->irq_lock);
Marc Zyngier59529f62015-11-30 13:09:53 +000072
Marc Zyngier3134cc82021-08-19 19:03:05 +010073 /* Always preserve the active bit, note deactivation */
74 deactivated = irq->active && !(val & ICH_LR_ACTIVE_BIT);
Marc Zyngier59529f62015-11-30 13:09:53 +000075 irq->active = !!(val & ICH_LR_ACTIVE_BIT);
76
Marc Zyngier53692902018-04-18 10:39:04 +010077 if (irq->active && is_v2_sgi)
78 irq->active_source = cpuid;
79
Marc Zyngier59529f62015-11-30 13:09:53 +000080 /* Edge is the only case where we preserve the pending bit */
81 if (irq->config == VGIC_CONFIG_EDGE &&
82 (val & ICH_LR_PENDING_BIT)) {
Christoffer Dall8694e4d2017-01-23 14:07:18 +010083 irq->pending_latch = true;
Marc Zyngier59529f62015-11-30 13:09:53 +000084
Marc Zyngier53692902018-04-18 10:39:04 +010085 if (is_v2_sgi)
Marc Zyngier59529f62015-11-30 13:09:53 +000086 irq->source |= (1 << cpuid);
Marc Zyngier59529f62015-11-30 13:09:53 +000087 }
88
Marc Zyngier637d1222016-05-25 15:26:36 +010089 /*
90 * Clear soft pending state when level irqs have been acked.
Marc Zyngier637d1222016-05-25 15:26:36 +010091 */
Marc Zyngier67b5b672018-03-09 14:59:40 +000092 if (irq->config == VGIC_CONFIG_LEVEL && !(val & ICH_LR_STATE))
93 irq->pending_latch = false;
Marc Zyngier59529f62015-11-30 13:09:53 +000094
Marc Zyngier3134cc82021-08-19 19:03:05 +010095 /* Handle resampling for mapped interrupts if required */
96 vgic_irq_handle_resampling(irq, deactivated, val & ICH_LR_PENDING_BIT);
Christoffer Dalle40cc572017-08-29 10:40:44 +020097
Julien Thierry8fa3adb2019-01-07 15:06:15 +000098 raw_spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +010099 vgic_put_irq(vcpu->kvm, irq);
Marc Zyngier59529f62015-11-30 13:09:53 +0000100 }
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100101
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800102 cpuif->used_lrs = 0;
Marc Zyngier59529f62015-11-30 13:09:53 +0000103}
104
105/* Requires the irq to be locked already */
106void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
107{
108 u32 model = vcpu->kvm->arch.vgic.vgic_model;
109 u64 val = irq->intid;
Marc Zyngier53692902018-04-18 10:39:04 +0100110 bool allow_pending = true, is_v2_sgi;
Marc Zyngier59529f62015-11-30 13:09:53 +0000111
Marc Zyngier53692902018-04-18 10:39:04 +0100112 is_v2_sgi = (vgic_irq_is_sgi(irq->intid) &&
113 model == KVM_DEV_TYPE_ARM_VGIC_V2);
114
115 if (irq->active) {
Marc Zyngier67b5b672018-03-09 14:59:40 +0000116 val |= ICH_LR_ACTIVE_BIT;
Marc Zyngier53692902018-04-18 10:39:04 +0100117 if (is_v2_sgi)
118 val |= irq->active_source << GICH_LR_PHYSID_CPUID_SHIFT;
119 if (vgic_irq_is_multi_sgi(irq)) {
120 allow_pending = false;
121 val |= ICH_LR_EOI;
122 }
123 }
Marc Zyngier67b5b672018-03-09 14:59:40 +0000124
Marc Zyngier354920e2021-03-15 13:11:58 +0000125 if (irq->hw && !vgic_irq_needs_resampling(irq)) {
Marc Zyngier67b5b672018-03-09 14:59:40 +0000126 val |= ICH_LR_HW;
127 val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT;
128 /*
129 * Never set pending+active on a HW interrupt, as the
130 * pending state is kept at the physical distributor
131 * level.
132 */
133 if (irq->active)
134 allow_pending = false;
135 } else {
136 if (irq->config == VGIC_CONFIG_LEVEL) {
137 val |= ICH_LR_EOI;
138
139 /*
140 * Software resampling doesn't work very well
141 * if we allow P+A, so let's not do that.
142 */
143 if (irq->active)
144 allow_pending = false;
145 }
146 }
147
148 if (allow_pending && irq_is_pending(irq)) {
Marc Zyngier59529f62015-11-30 13:09:53 +0000149 val |= ICH_LR_PENDING_BIT;
150
151 if (irq->config == VGIC_CONFIG_EDGE)
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100152 irq->pending_latch = false;
Marc Zyngier59529f62015-11-30 13:09:53 +0000153
154 if (vgic_irq_is_sgi(irq->intid) &&
155 model == KVM_DEV_TYPE_ARM_VGIC_V2) {
156 u32 src = ffs(irq->source);
157
Marc Zyngier82e40f52019-08-28 11:10:16 +0100158 if (WARN_RATELIMIT(!src, "No SGI source for INTID %d\n",
159 irq->intid))
160 return;
161
Marc Zyngier59529f62015-11-30 13:09:53 +0000162 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
163 irq->source &= ~(1 << (src - 1));
Marc Zyngier53692902018-04-18 10:39:04 +0100164 if (irq->source) {
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100165 irq->pending_latch = true;
Marc Zyngier53692902018-04-18 10:39:04 +0100166 val |= ICH_LR_EOI;
167 }
Marc Zyngier59529f62015-11-30 13:09:53 +0000168 }
169 }
170
Marc Zyngier59529f62015-11-30 13:09:53 +0000171 /*
Christoffer Dalle40cc572017-08-29 10:40:44 +0200172 * Level-triggered mapped IRQs are special because we only observe
173 * rising edges as input to the VGIC. We therefore lower the line
174 * level here, so that we can take new virtual IRQs. See
175 * vgic_v3_fold_lr_state for more info.
176 */
177 if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT))
178 irq->line_level = false;
179
Christoffer Dall87322092018-07-16 15:06:22 +0200180 if (irq->group)
Marc Zyngier59529f62015-11-30 13:09:53 +0000181 val |= ICH_LR_GROUP;
182
183 val |= (u64)irq->priority << ICH_LR_PRIORITY_SHIFT;
184
185 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = val;
186}
187
188void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)
189{
190 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] = 0;
191}
Andre Przywarae4823a72015-12-03 11:47:37 +0000192
193void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
194{
Christoffer Dall328e5662016-03-24 11:21:04 +0100195 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
Christoffer Dall28232a42017-05-20 14:12:34 +0200196 u32 model = vcpu->kvm->arch.vgic.vgic_model;
Andre Przywarae4823a72015-12-03 11:47:37 +0000197 u32 vmcr;
198
Christoffer Dall28232a42017-05-20 14:12:34 +0200199 if (model == KVM_DEV_TYPE_ARM_VGIC_V2) {
200 vmcr = (vmcrp->ackctl << ICH_VMCR_ACK_CTL_SHIFT) &
201 ICH_VMCR_ACK_CTL_MASK;
202 vmcr |= (vmcrp->fiqen << ICH_VMCR_FIQ_EN_SHIFT) &
203 ICH_VMCR_FIQ_EN_MASK;
204 } else {
205 /*
206 * When emulating GICv3 on GICv3 with SRE=1 on the
207 * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
208 */
209 vmcr = ICH_VMCR_FIQ_EN_MASK;
210 }
211
212 vmcr |= (vmcrp->cbpr << ICH_VMCR_CBPR_SHIFT) & ICH_VMCR_CBPR_MASK;
213 vmcr |= (vmcrp->eoim << ICH_VMCR_EOIM_SHIFT) & ICH_VMCR_EOIM_MASK;
Andre Przywarae4823a72015-12-03 11:47:37 +0000214 vmcr |= (vmcrp->abpr << ICH_VMCR_BPR1_SHIFT) & ICH_VMCR_BPR1_MASK;
215 vmcr |= (vmcrp->bpr << ICH_VMCR_BPR0_SHIFT) & ICH_VMCR_BPR0_MASK;
216 vmcr |= (vmcrp->pmr << ICH_VMCR_PMR_SHIFT) & ICH_VMCR_PMR_MASK;
Vijaya Kumar K5fb247d2017-01-26 19:50:50 +0530217 vmcr |= (vmcrp->grpen0 << ICH_VMCR_ENG0_SHIFT) & ICH_VMCR_ENG0_MASK;
218 vmcr |= (vmcrp->grpen1 << ICH_VMCR_ENG1_SHIFT) & ICH_VMCR_ENG1_MASK;
Andre Przywarae4823a72015-12-03 11:47:37 +0000219
Christoffer Dall328e5662016-03-24 11:21:04 +0100220 cpu_if->vgic_vmcr = vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000221}
222
223void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
224{
Christoffer Dall328e5662016-03-24 11:21:04 +0100225 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
Christoffer Dall28232a42017-05-20 14:12:34 +0200226 u32 model = vcpu->kvm->arch.vgic.vgic_model;
Christoffer Dall328e5662016-03-24 11:21:04 +0100227 u32 vmcr;
228
229 vmcr = cpu_if->vgic_vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000230
Christoffer Dall28232a42017-05-20 14:12:34 +0200231 if (model == KVM_DEV_TYPE_ARM_VGIC_V2) {
232 vmcrp->ackctl = (vmcr & ICH_VMCR_ACK_CTL_MASK) >>
233 ICH_VMCR_ACK_CTL_SHIFT;
234 vmcrp->fiqen = (vmcr & ICH_VMCR_FIQ_EN_MASK) >>
235 ICH_VMCR_FIQ_EN_SHIFT;
236 } else {
237 /*
238 * When emulating GICv3 on GICv3 with SRE=1 on the
239 * VFIQEn bit is RES1 and the VAckCtl bit is RES0.
240 */
241 vmcrp->fiqen = 1;
242 vmcrp->ackctl = 0;
243 }
244
245 vmcrp->cbpr = (vmcr & ICH_VMCR_CBPR_MASK) >> ICH_VMCR_CBPR_SHIFT;
246 vmcrp->eoim = (vmcr & ICH_VMCR_EOIM_MASK) >> ICH_VMCR_EOIM_SHIFT;
Andre Przywarae4823a72015-12-03 11:47:37 +0000247 vmcrp->abpr = (vmcr & ICH_VMCR_BPR1_MASK) >> ICH_VMCR_BPR1_SHIFT;
248 vmcrp->bpr = (vmcr & ICH_VMCR_BPR0_MASK) >> ICH_VMCR_BPR0_SHIFT;
249 vmcrp->pmr = (vmcr & ICH_VMCR_PMR_MASK) >> ICH_VMCR_PMR_SHIFT;
Vijaya Kumar K5fb247d2017-01-26 19:50:50 +0530250 vmcrp->grpen0 = (vmcr & ICH_VMCR_ENG0_MASK) >> ICH_VMCR_ENG0_SHIFT;
251 vmcrp->grpen1 = (vmcr & ICH_VMCR_ENG1_MASK) >> ICH_VMCR_ENG1_SHIFT;
Andre Przywarae4823a72015-12-03 11:47:37 +0000252}
Eric Auger90977732015-12-01 15:02:35 +0100253
Andre Przywara0aa1de52016-07-15 12:43:29 +0100254#define INITIAL_PENDBASER_VALUE \
255 (GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWb) | \
256 GIC_BASER_CACHEABILITY(GICR_PENDBASER, OUTER, SameAsInner) | \
257 GIC_BASER_SHAREABILITY(GICR_PENDBASER, InnerShareable))
258
Eric Augerad275b8b2015-12-21 18:09:38 +0100259void vgic_v3_enable(struct kvm_vcpu *vcpu)
260{
Eric Augerf7b69852015-12-02 10:30:13 +0100261 struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3;
262
263 /*
264 * By forcing VMCR to zero, the GIC will restore the binary
265 * points to their reset values. Anything else resets to zero
266 * anyway.
267 */
268 vgic_v3->vgic_vmcr = 0;
Eric Augerf7b69852015-12-02 10:30:13 +0100269
270 /*
271 * If we are emulating a GICv3, we do it in an non-GICv2-compatible
272 * way, so we force SRE to 1 to demonstrate this to the guest.
Marc Zyngier4dfc0502017-02-21 11:32:47 +0000273 * Also, we don't support any form of IRQ/FIQ bypass.
Eric Augerf7b69852015-12-02 10:30:13 +0100274 * This goes with the spec allowing the value to be RAO/WI.
275 */
Andre Przywara0aa1de52016-07-15 12:43:29 +0100276 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
Marc Zyngier4dfc0502017-02-21 11:32:47 +0000277 vgic_v3->vgic_sre = (ICC_SRE_EL1_DIB |
278 ICC_SRE_EL1_DFB |
279 ICC_SRE_EL1_SRE);
Andre Przywara0aa1de52016-07-15 12:43:29 +0100280 vcpu->arch.vgic_cpu.pendbaser = INITIAL_PENDBASER_VALUE;
281 } else {
Eric Augerf7b69852015-12-02 10:30:13 +0100282 vgic_v3->vgic_sre = 0;
Andre Przywara0aa1de52016-07-15 12:43:29 +0100283 }
Eric Augerf7b69852015-12-02 10:30:13 +0100284
Vijaya Kumar Kd017d7b2017-01-26 19:50:51 +0530285 vcpu->arch.vgic_cpu.num_id_bits = (kvm_vgic_global_state.ich_vtr_el2 &
286 ICH_VTR_ID_BITS_MASK) >>
287 ICH_VTR_ID_BITS_SHIFT;
288 vcpu->arch.vgic_cpu.num_pri_bits = ((kvm_vgic_global_state.ich_vtr_el2 &
289 ICH_VTR_PRI_BITS_MASK) >>
290 ICH_VTR_PRI_BITS_SHIFT) + 1;
291
Eric Augerf7b69852015-12-02 10:30:13 +0100292 /* Get the show on the road... */
293 vgic_v3->vgic_hcr = ICH_HCR_EN;
Marc Zyngierabf55762017-06-09 12:49:45 +0100294 if (group0_trap)
295 vgic_v3->vgic_hcr |= ICH_HCR_TALL0;
Marc Zyngier9c7bfc22017-06-09 12:49:40 +0100296 if (group1_trap)
297 vgic_v3->vgic_hcr |= ICH_HCR_TALL1;
Marc Zyngierff895112017-06-09 12:49:53 +0100298 if (common_trap)
299 vgic_v3->vgic_hcr |= ICH_HCR_TC;
Marc Zyngier09247292021-10-10 16:09:08 +0100300 if (dir_trap)
301 vgic_v3->vgic_hcr |= ICH_HCR_TDIR;
Eric Augerad275b8b2015-12-21 18:09:38 +0100302}
303
Eric Auger44de9d62017-05-04 11:19:52 +0200304int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq)
305{
306 struct kvm_vcpu *vcpu;
307 int byte_offset, bit_nr;
308 gpa_t pendbase, ptr;
309 bool status;
310 u8 val;
311 int ret;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200312 unsigned long flags;
Eric Auger44de9d62017-05-04 11:19:52 +0200313
314retry:
315 vcpu = irq->target_vcpu;
316 if (!vcpu)
317 return 0;
318
319 pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
320
321 byte_offset = irq->intid / BITS_PER_BYTE;
322 bit_nr = irq->intid % BITS_PER_BYTE;
323 ptr = pendbase + byte_offset;
324
Andre Przywara711702b2018-05-11 15:20:15 +0100325 ret = kvm_read_guest_lock(kvm, ptr, &val, 1);
Eric Auger44de9d62017-05-04 11:19:52 +0200326 if (ret)
327 return ret;
328
329 status = val & (1 << bit_nr);
330
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000331 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Eric Auger44de9d62017-05-04 11:19:52 +0200332 if (irq->target_vcpu != vcpu) {
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000333 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Eric Auger44de9d62017-05-04 11:19:52 +0200334 goto retry;
335 }
336 irq->pending_latch = status;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200337 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
Eric Auger44de9d62017-05-04 11:19:52 +0200338
339 if (status) {
340 /* clear consumed data */
341 val &= ~(1 << bit_nr);
Marc Zyngiera6ecfb12019-03-19 12:47:11 +0000342 ret = kvm_write_guest_lock(kvm, ptr, &val, 1);
Eric Auger44de9d62017-05-04 11:19:52 +0200343 if (ret)
344 return ret;
345 }
346 return 0;
347}
348
Shenming Luf66b7b12021-03-22 14:01:56 +0800349/*
350 * The deactivation of the doorbell interrupt will trigger the
351 * unmapping of the associated vPE.
352 */
353static void unmap_all_vpes(struct vgic_dist *dist)
354{
355 struct irq_desc *desc;
356 int i;
357
358 for (i = 0; i < dist->its_vm.nr_vpes; i++) {
359 desc = irq_to_desc(dist->its_vm.vpes[i]->irq);
360 irq_domain_deactivate_irq(irq_desc_get_irq_data(desc));
361 }
362}
363
364static void map_all_vpes(struct vgic_dist *dist)
365{
366 struct irq_desc *desc;
367 int i;
368
369 for (i = 0; i < dist->its_vm.nr_vpes; i++) {
370 desc = irq_to_desc(dist->its_vm.vpes[i]->irq);
371 irq_domain_activate_irq(irq_desc_get_irq_data(desc), false);
372 }
373}
374
Eric Auger28077122017-01-09 16:28:27 +0100375/**
Zenghui Yubad36e42019-10-29 15:19:18 +0800376 * vgic_v3_save_pending_tables - Save the pending tables into guest RAM
Eric Auger28077122017-01-09 16:28:27 +0100377 * kvm lock and all vcpu lock must be held
378 */
379int vgic_v3_save_pending_tables(struct kvm *kvm)
380{
381 struct vgic_dist *dist = &kvm->arch.vgic;
Eric Auger28077122017-01-09 16:28:27 +0100382 struct vgic_irq *irq;
Zenghui Yuca185b22019-10-29 15:19:19 +0800383 gpa_t last_ptr = ~(gpa_t)0;
Shenming Luf66b7b12021-03-22 14:01:56 +0800384 bool vlpi_avail = false;
385 int ret = 0;
Marc Zyngierddb4b012017-11-16 17:58:16 +0000386 u8 val;
Eric Auger28077122017-01-09 16:28:27 +0100387
Shenming Luf66b7b12021-03-22 14:01:56 +0800388 if (unlikely(!vgic_initialized(kvm)))
389 return -ENXIO;
390
391 /*
392 * A preparation for getting any VLPI states.
393 * The above vgic initialized check also ensures that the allocation
394 * and enabling of the doorbells have already been done.
395 */
396 if (kvm_vgic_global_state.has_gicv4_1) {
397 unmap_all_vpes(dist);
398 vlpi_avail = true;
399 }
400
Eric Auger28077122017-01-09 16:28:27 +0100401 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
402 int byte_offset, bit_nr;
403 struct kvm_vcpu *vcpu;
404 gpa_t pendbase, ptr;
Shenming Luf66b7b12021-03-22 14:01:56 +0800405 bool is_pending;
Eric Auger28077122017-01-09 16:28:27 +0100406 bool stored;
Eric Auger28077122017-01-09 16:28:27 +0100407
408 vcpu = irq->target_vcpu;
409 if (!vcpu)
410 continue;
411
412 pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
413
414 byte_offset = irq->intid / BITS_PER_BYTE;
415 bit_nr = irq->intid % BITS_PER_BYTE;
416 ptr = pendbase + byte_offset;
417
Zenghui Yuca185b22019-10-29 15:19:19 +0800418 if (ptr != last_ptr) {
Andre Przywara711702b2018-05-11 15:20:15 +0100419 ret = kvm_read_guest_lock(kvm, ptr, &val, 1);
Eric Auger28077122017-01-09 16:28:27 +0100420 if (ret)
Shenming Luf66b7b12021-03-22 14:01:56 +0800421 goto out;
Zenghui Yuca185b22019-10-29 15:19:19 +0800422 last_ptr = ptr;
Eric Auger28077122017-01-09 16:28:27 +0100423 }
424
425 stored = val & (1U << bit_nr);
Shenming Luf66b7b12021-03-22 14:01:56 +0800426
427 is_pending = irq->pending_latch;
428
429 if (irq->hw && vlpi_avail)
430 vgic_v4_get_vlpi_state(irq, &is_pending);
431
432 if (stored == is_pending)
Eric Auger28077122017-01-09 16:28:27 +0100433 continue;
434
Shenming Luf66b7b12021-03-22 14:01:56 +0800435 if (is_pending)
Eric Auger28077122017-01-09 16:28:27 +0100436 val |= 1 << bit_nr;
437 else
438 val &= ~(1 << bit_nr);
439
Marc Zyngiera6ecfb12019-03-19 12:47:11 +0000440 ret = kvm_write_guest_lock(kvm, ptr, &val, 1);
Eric Auger28077122017-01-09 16:28:27 +0100441 if (ret)
Shenming Luf66b7b12021-03-22 14:01:56 +0800442 goto out;
Eric Auger28077122017-01-09 16:28:27 +0100443 }
Shenming Luf66b7b12021-03-22 14:01:56 +0800444
445out:
446 if (vlpi_avail)
447 map_all_vpes(dist);
448
449 return ret;
Eric Auger28077122017-01-09 16:28:27 +0100450}
451
Eric Auger028bf272018-05-22 09:55:11 +0200452/**
453 * vgic_v3_rdist_overlap - check if a region overlaps with any
454 * existing redistributor region
455 *
456 * @kvm: kvm handle
457 * @base: base of the region
458 * @size: size of region
459 *
460 * Return: true if there is an overlap
461 */
462bool vgic_v3_rdist_overlap(struct kvm *kvm, gpa_t base, size_t size)
463{
464 struct vgic_dist *d = &kvm->arch.vgic;
465 struct vgic_redist_region *rdreg;
466
467 list_for_each_entry(rdreg, &d->rd_regions, list) {
468 if ((base + size > rdreg->base) &&
469 (base < rdreg->base + vgic_v3_rd_region_size(kvm, rdreg)))
470 return true;
471 }
472 return false;
473}
474
Christoffer Dall9a746d72017-05-08 12:23:51 +0200475/*
476 * Check for overlapping regions and for regions crossing the end of memory
477 * for base addresses which have already been set.
478 */
479bool vgic_v3_check_base(struct kvm *kvm)
Eric Augerb0442ee2015-12-21 15:04:42 +0100480{
481 struct vgic_dist *d = &kvm->arch.vgic;
Eric Auger028bf272018-05-22 09:55:11 +0200482 struct vgic_redist_region *rdreg;
Eric Augerb0442ee2015-12-21 15:04:42 +0100483
Christoffer Dall9a746d72017-05-08 12:23:51 +0200484 if (!IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) &&
485 d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base)
Eric Augerb0442ee2015-12-21 15:04:42 +0100486 return false;
Christoffer Dall9a746d72017-05-08 12:23:51 +0200487
Eric Auger028bf272018-05-22 09:55:11 +0200488 list_for_each_entry(rdreg, &d->rd_regions, list) {
Ricardo Koller4612d982021-10-04 18:19:12 -0700489 size_t sz = vgic_v3_rd_region_size(kvm, rdreg);
490
491 if (vgic_check_iorange(kvm, VGIC_ADDR_UNDEF,
492 rdreg->base, SZ_64K, sz))
Eric Auger028bf272018-05-22 09:55:11 +0200493 return false;
494 }
Eric Augerb0442ee2015-12-21 15:04:42 +0100495
Eric Auger028bf272018-05-22 09:55:11 +0200496 if (IS_VGIC_ADDR_UNDEF(d->vgic_dist_base))
Christoffer Dall9a746d72017-05-08 12:23:51 +0200497 return true;
498
Eric Auger028bf272018-05-22 09:55:11 +0200499 return !vgic_v3_rdist_overlap(kvm, d->vgic_dist_base,
500 KVM_VGIC_V3_DIST_SIZE);
Eric Augerb0442ee2015-12-21 15:04:42 +0100501}
502
Eric Augerdc524612018-05-22 09:55:09 +0200503/**
504 * vgic_v3_rdist_free_slot - Look up registered rdist regions and identify one
505 * which has free space to put a new rdist region.
506 *
507 * @rd_regions: redistributor region list head
508 *
509 * A redistributor regions maps n redistributors, n = region size / (2 x 64kB).
510 * Stride between redistributors is 0 and regions are filled in the index order.
511 *
512 * Return: the redist region handle, if any, that has space to map a new rdist
513 * region.
514 */
515struct vgic_redist_region *vgic_v3_rdist_free_slot(struct list_head *rd_regions)
516{
517 struct vgic_redist_region *rdreg;
518
519 list_for_each_entry(rdreg, rd_regions, list) {
520 if (!vgic_v3_redist_region_full(rdreg))
521 return rdreg;
522 }
523 return NULL;
524}
525
Eric Auger04c11092018-05-22 09:55:17 +0200526struct vgic_redist_region *vgic_v3_rdist_region_from_index(struct kvm *kvm,
527 u32 index)
528{
529 struct list_head *rd_regions = &kvm->arch.vgic.rd_regions;
530 struct vgic_redist_region *rdreg;
531
532 list_for_each_entry(rdreg, rd_regions, list) {
533 if (rdreg->index == index)
534 return rdreg;
535 }
536 return NULL;
537}
538
539
Eric Augerb0442ee2015-12-21 15:04:42 +0100540int vgic_v3_map_resources(struct kvm *kvm)
541{
Eric Augerb0442ee2015-12-21 15:04:42 +0100542 struct vgic_dist *dist = &kvm->arch.vgic;
Eric Augerc957a6d2018-05-22 09:55:15 +0200543 struct kvm_vcpu *vcpu;
544 int ret = 0;
Marc Zyngier46808a42021-11-16 16:04:02 +0000545 unsigned long c;
Eric Augerb0442ee2015-12-21 15:04:42 +0100546
Eric Augerc957a6d2018-05-22 09:55:15 +0200547 kvm_for_each_vcpu(c, vcpu, kvm) {
548 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
549
550 if (IS_VGIC_ADDR_UNDEF(vgic_cpu->rd_iodev.base_addr)) {
Marc Zyngier46808a42021-11-16 16:04:02 +0000551 kvm_debug("vcpu %ld redistributor base not set\n", c);
Marc Zyngier101068b2020-12-27 14:28:34 +0000552 return -ENXIO;
Eric Augerc957a6d2018-05-22 09:55:15 +0200553 }
554 }
555
556 if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base)) {
Marc Zyngier440523b2021-12-16 10:45:07 +0000557 kvm_debug("Need to set vgic distributor addresses first\n");
Marc Zyngier101068b2020-12-27 14:28:34 +0000558 return -ENXIO;
Eric Augerb0442ee2015-12-21 15:04:42 +0100559 }
560
561 if (!vgic_v3_check_base(kvm)) {
Marc Zyngier440523b2021-12-16 10:45:07 +0000562 kvm_debug("VGIC redist and dist frames overlap\n");
Marc Zyngier101068b2020-12-27 14:28:34 +0000563 return -EINVAL;
Eric Augerb0442ee2015-12-21 15:04:42 +0100564 }
565
566 /*
567 * For a VGICv3 we require the userland to explicitly initialize
568 * the VGIC before we need to use it.
569 */
570 if (!vgic_initialized(kvm)) {
Marc Zyngier101068b2020-12-27 14:28:34 +0000571 return -EBUSY;
Eric Augerb0442ee2015-12-21 15:04:42 +0100572 }
573
574 ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V3);
575 if (ret) {
576 kvm_err("Unable to register VGICv3 dist MMIO regions\n");
Marc Zyngier101068b2020-12-27 14:28:34 +0000577 return ret;
Eric Augerb0442ee2015-12-21 15:04:42 +0100578 }
579
Marc Zyngier2291ff22020-03-04 20:33:27 +0000580 if (kvm_vgic_global_state.has_gicv4_1)
581 vgic_v4_configure_vsgis(kvm);
Eric Augerb0442ee2015-12-21 15:04:42 +0100582
Marc Zyngier101068b2020-12-27 14:28:34 +0000583 return 0;
Eric Augerb0442ee2015-12-21 15:04:42 +0100584}
585
Marc Zyngier59da1cb2017-06-09 12:49:33 +0100586DEFINE_STATIC_KEY_FALSE(vgic_v3_cpuif_trap);
587
Marc Zyngiere23f62f2017-06-09 12:49:46 +0100588static int __init early_group0_trap_cfg(char *buf)
589{
590 return strtobool(buf, &group0_trap);
591}
592early_param("kvm-arm.vgic_v3_group0_trap", early_group0_trap_cfg);
593
Marc Zyngier182936e2017-06-09 12:49:41 +0100594static int __init early_group1_trap_cfg(char *buf)
595{
596 return strtobool(buf, &group1_trap);
597}
598early_param("kvm-arm.vgic_v3_group1_trap", early_group1_trap_cfg);
599
Marc Zyngierff895112017-06-09 12:49:53 +0100600static int __init early_common_trap_cfg(char *buf)
601{
602 return strtobool(buf, &common_trap);
603}
604early_param("kvm-arm.vgic_v3_common_trap", early_common_trap_cfg);
605
Marc Zyngiera7546052017-10-27 15:28:54 +0100606static int __init early_gicv4_enable(char *buf)
607{
608 return strtobool(buf, &gicv4_enable);
609}
610early_param("kvm-arm.vgic_v4_enable", early_gicv4_enable);
611
Eric Auger90977732015-12-01 15:02:35 +0100612/**
Alexandru Elisei0ed5f5d2019-08-15 10:56:22 +0100613 * vgic_v3_probe - probe for a VGICv3 compatible interrupt controller
614 * @info: pointer to the GIC description
Eric Auger90977732015-12-01 15:02:35 +0100615 *
Alexandru Elisei0ed5f5d2019-08-15 10:56:22 +0100616 * Returns 0 if the VGICv3 has been probed successfully, returns an error code
617 * otherwise
Eric Auger90977732015-12-01 15:02:35 +0100618 */
619int vgic_v3_probe(const struct gic_kvm_info *info)
620{
Marc Zyngierb9d699e2021-03-05 18:52:52 +0000621 u64 ich_vtr_el2 = kvm_call_hyp_ret(__vgic_v3_get_gic_config);
Marc Zyngier9739f6e2021-03-05 18:52:53 +0000622 bool has_v2;
Andre Przywara42c88702016-07-15 12:43:23 +0100623 int ret;
Eric Auger90977732015-12-01 15:02:35 +0100624
Marc Zyngier9739f6e2021-03-05 18:52:53 +0000625 has_v2 = ich_vtr_el2 >> 63;
Marc Zyngierb9d699e2021-03-05 18:52:52 +0000626 ich_vtr_el2 = (u32)ich_vtr_el2;
627
Eric Auger90977732015-12-01 15:02:35 +0100628 /*
Fuad Tabba656012c2020-04-01 15:03:10 +0100629 * The ListRegs field is 5 bits, but there is an architectural
Eric Auger90977732015-12-01 15:02:35 +0100630 * maximum of 16 list registers. Just ignore bit 4...
631 */
632 kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1;
633 kvm_vgic_global_state.can_emulate_gicv2 = false;
Vijaya Kumar Kd017d7b2017-01-26 19:50:51 +0530634 kvm_vgic_global_state.ich_vtr_el2 = ich_vtr_el2;
Eric Auger90977732015-12-01 15:02:35 +0100635
Marc Zyngiera7546052017-10-27 15:28:54 +0100636 /* GICv4 support? */
637 if (info->has_v4) {
638 kvm_vgic_global_state.has_gicv4 = gicv4_enable;
Marc Zyngierae699ad2020-03-04 20:33:20 +0000639 kvm_vgic_global_state.has_gicv4_1 = info->has_v4_1 && gicv4_enable;
640 kvm_info("GICv4%s support %sabled\n",
641 kvm_vgic_global_state.has_gicv4_1 ? ".1" : "",
Marc Zyngiera7546052017-10-27 15:28:54 +0100642 gicv4_enable ? "en" : "dis");
643 }
644
Marc Zyngier9739f6e2021-03-05 18:52:53 +0000645 kvm_vgic_global_state.vcpu_base = 0;
646
Eric Auger90977732015-12-01 15:02:35 +0100647 if (!info->vcpu.start) {
648 kvm_info("GICv3: no GICV resource entry\n");
Marc Zyngier9739f6e2021-03-05 18:52:53 +0000649 } else if (!has_v2) {
650 pr_warn(FW_BUG "CPU interface incapable of MMIO access\n");
Eric Auger90977732015-12-01 15:02:35 +0100651 } else if (!PAGE_ALIGNED(info->vcpu.start)) {
652 pr_warn("GICV physical address 0x%llx not page aligned\n",
653 (unsigned long long)info->vcpu.start);
Quentin Perreta770ee82021-12-08 15:22:55 +0000654 } else if (kvm_get_mode() != KVM_MODE_PROTECTED) {
Eric Auger90977732015-12-01 15:02:35 +0100655 kvm_vgic_global_state.vcpu_base = info->vcpu.start;
656 kvm_vgic_global_state.can_emulate_gicv2 = true;
Andre Przywara42c88702016-07-15 12:43:23 +0100657 ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
658 if (ret) {
659 kvm_err("Cannot register GICv2 KVM device.\n");
660 return ret;
661 }
Eric Auger90977732015-12-01 15:02:35 +0100662 kvm_info("vgic-v2@%llx\n", info->vcpu.start);
663 }
Andre Przywara42c88702016-07-15 12:43:23 +0100664 ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3);
665 if (ret) {
666 kvm_err("Cannot register GICv3 KVM device.\n");
667 kvm_unregister_device_ops(KVM_DEV_TYPE_ARM_VGIC_V2);
668 return ret;
669 }
670
Eric Auger90977732015-12-01 15:02:35 +0100671 if (kvm_vgic_global_state.vcpu_base == 0)
672 kvm_info("disabling GICv2 emulation\n");
Eric Auger90977732015-12-01 15:02:35 +0100673
David Daney690a3412017-06-09 12:49:48 +0100674 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_30115)) {
675 group0_trap = true;
676 group1_trap = true;
677 }
David Daney690a3412017-06-09 12:49:48 +0100678
Marc Zyngierdf652bc2021-10-10 16:09:07 +0100679 if (kvm_vgic_global_state.ich_vtr_el2 & ICH_VTR_SEIS_MASK) {
680 kvm_info("GICv3 with locally generated SEI\n");
681
682 group0_trap = true;
683 group1_trap = true;
Marc Zyngier09247292021-10-10 16:09:08 +0100684 if (ich_vtr_el2 & ICH_VTR_TDS_MASK)
685 dir_trap = true;
686 else
687 common_trap = true;
Marc Zyngierdf652bc2021-10-10 16:09:07 +0100688 }
689
Marc Zyngier09247292021-10-10 16:09:08 +0100690 if (group0_trap || group1_trap || common_trap | dir_trap) {
691 kvm_info("GICv3 sysreg trapping enabled ([%s%s%s%s], reduced performance)\n",
Marc Zyngier2873b502017-06-09 12:49:54 +0100692 group0_trap ? "G0" : "",
693 group1_trap ? "G1" : "",
Marc Zyngier09247292021-10-10 16:09:08 +0100694 common_trap ? "C" : "",
695 dir_trap ? "D" : "");
Marc Zyngier182936e2017-06-09 12:49:41 +0100696 static_branch_enable(&vgic_v3_cpuif_trap);
697 }
698
Eric Auger90977732015-12-01 15:02:35 +0100699 kvm_vgic_global_state.vctrl_base = NULL;
700 kvm_vgic_global_state.type = VGIC_V3;
701 kvm_vgic_global_state.max_gic_vcpus = VGIC_V3_MAX_CPUS;
702
703 return 0;
704}
Christoffer Dall328e5662016-03-24 11:21:04 +0100705
706void vgic_v3_load(struct kvm_vcpu *vcpu)
707{
708 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
709
Marc Zyngierff567612017-04-19 12:15:26 +0100710 /*
711 * If dealing with a GICv2 emulation on GICv3, VMCR_EL2.VFIQen
712 * is dependent on ICC_SRE_EL1.SRE, and we have to perform the
713 * VMCR_EL2 save/restore in the world switch.
714 */
715 if (likely(cpu_if->vgic_sre))
716 kvm_call_hyp(__vgic_v3_write_vmcr, cpu_if->vgic_vmcr);
Christoffer Dall923a2e32017-10-05 00:18:07 +0200717
Andrew Sculla0712612020-09-15 11:46:43 +0100718 kvm_call_hyp(__vgic_v3_restore_aprs, cpu_if);
Christoffer Dall2d0e63e2017-10-05 17:19:19 +0200719
720 if (has_vhe())
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800721 __vgic_v3_activate_traps(cpu_if);
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000722
723 WARN_ON(vgic_v4_load(vcpu));
Christoffer Dall328e5662016-03-24 11:21:04 +0100724}
725
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100726void vgic_v3_vmcr_sync(struct kvm_vcpu *vcpu)
Christoffer Dall328e5662016-03-24 11:21:04 +0100727{
728 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
729
Marc Zyngierff567612017-04-19 12:15:26 +0100730 if (likely(cpu_if->vgic_sre))
Marc Zyngier7aa8d142019-01-05 15:49:50 +0000731 cpu_if->vgic_vmcr = kvm_call_hyp_ret(__vgic_v3_read_vmcr);
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100732}
733
734void vgic_v3_put(struct kvm_vcpu *vcpu)
735{
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800736 struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
737
Marc Zyngier8e01d9a2019-10-27 14:41:59 +0000738 WARN_ON(vgic_v4_put(vcpu, false));
739
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100740 vgic_v3_vmcr_sync(vcpu);
Christoffer Dall923a2e32017-10-05 00:18:07 +0200741
Andrew Sculla0712612020-09-15 11:46:43 +0100742 kvm_call_hyp(__vgic_v3_save_aprs, cpu_if);
Christoffer Dall2d0e63e2017-10-05 17:19:19 +0200743
744 if (has_vhe())
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800745 __vgic_v3_deactivate_traps(cpu_if);
Christoffer Dall328e5662016-03-24 11:21:04 +0100746}