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