xref: /linux/arch/arm64/kvm/vgic/vgic-v5.c (revision fc2d791a43d3880496d1c729b8bd74d2c19cb4e7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2025, 2026 Arm Ltd.
4  */
5 
6 #include <kvm/arm_vgic.h>
7 
8 #include <linux/bitops.h>
9 #include <linux/irqchip/arm-vgic-info.h>
10 
11 #include "vgic.h"
12 
13 static struct vgic_v5_ppi_caps ppi_caps;
14 
15 /*
16  * Not all PPIs are guaranteed to be implemented for GICv5. Deterermine which
17  * ones are, and generate a mask.
18  */
19 static void vgic_v5_get_implemented_ppis(void)
20 {
21 	if (!cpus_have_final_cap(ARM64_HAS_GICV5_CPUIF))
22 		return;
23 
24 	/*
25 	 * If we have KVM, we have EL2, which means that we have support for the
26 	 * EL1 and EL2 Physical & Virtual timers.
27 	 */
28 	__assign_bit(GICV5_ARCH_PPI_CNTHP, ppi_caps.impl_ppi_mask, 1);
29 	__assign_bit(GICV5_ARCH_PPI_CNTV, ppi_caps.impl_ppi_mask, 1);
30 	__assign_bit(GICV5_ARCH_PPI_CNTHV, ppi_caps.impl_ppi_mask, 1);
31 	__assign_bit(GICV5_ARCH_PPI_CNTP, ppi_caps.impl_ppi_mask, 1);
32 
33 	/* The SW_PPI should be available */
34 	__assign_bit(GICV5_ARCH_PPI_SW_PPI, ppi_caps.impl_ppi_mask, 1);
35 
36 	/* The PMUIRQ is available if we have the PMU */
37 	__assign_bit(GICV5_ARCH_PPI_PMUIRQ, ppi_caps.impl_ppi_mask, system_supports_pmuv3());
38 }
39 
40 /*
41  * Probe for a vGICv5 compatible interrupt controller, returning 0 on success.
42  */
43 int vgic_v5_probe(const struct gic_kvm_info *info)
44 {
45 	bool v5_registered = false;
46 	u64 ich_vtr_el2;
47 	int ret;
48 
49 	kvm_vgic_global_state.type = VGIC_V5;
50 
51 	kvm_vgic_global_state.vcpu_base = 0;
52 	kvm_vgic_global_state.vctrl_base = NULL;
53 	kvm_vgic_global_state.can_emulate_gicv2 = false;
54 	kvm_vgic_global_state.has_gicv4 = false;
55 	kvm_vgic_global_state.has_gicv4_1 = false;
56 
57 	/*
58 	 * GICv5 is currently not supported in Protected mode. Skip the
59 	 * registration of GICv5 completely to make sure no guests can create a
60 	 * GICv5-based guest.
61 	 */
62 	if (is_protected_kvm_enabled()) {
63 		kvm_info("GICv5-based guests are not supported with pKVM\n");
64 		goto skip_v5;
65 	}
66 
67 	kvm_vgic_global_state.max_gic_vcpus = VGIC_V5_MAX_CPUS;
68 
69 	vgic_v5_get_implemented_ppis();
70 
71 	ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V5);
72 	if (ret) {
73 		kvm_err("Cannot register GICv5 KVM device.\n");
74 		goto skip_v5;
75 	}
76 
77 	v5_registered = true;
78 	kvm_info("GCIE system register CPU interface\n");
79 
80 skip_v5:
81 	/* If we don't support the GICv3 compat mode we're done. */
82 	if (!cpus_have_final_cap(ARM64_HAS_GICV5_LEGACY)) {
83 		if (!v5_registered)
84 			return -ENODEV;
85 		return 0;
86 	}
87 
88 	kvm_vgic_global_state.has_gcie_v3_compat = true;
89 	ich_vtr_el2 =  kvm_call_hyp_ret(__vgic_v3_get_gic_config);
90 	kvm_vgic_global_state.ich_vtr_el2 = (u32)ich_vtr_el2;
91 
92 	/*
93 	 * The ListRegs field is 5 bits, but there is an architectural
94 	 * maximum of 16 list registers. Just ignore bit 4...
95 	 */
96 	kvm_vgic_global_state.nr_lr = (ich_vtr_el2 & 0xf) + 1;
97 
98 	ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V3);
99 	if (ret) {
100 		kvm_err("Cannot register GICv3-legacy KVM device.\n");
101 		return ret;
102 	}
103 
104 	/* We potentially limit the max VCPUs further than we need to here */
105 	kvm_vgic_global_state.max_gic_vcpus = min(VGIC_V3_MAX_CPUS,
106 						  VGIC_V5_MAX_CPUS);
107 
108 	static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
109 	kvm_info("GCIE legacy system register CPU interface\n");
110 
111 	vgic_v3_enable_cpuif_traps();
112 
113 	return 0;
114 }
115 
116 void vgic_v5_reset(struct kvm_vcpu *vcpu)
117 {
118 	/*
119 	 * We always present 16-bits of ID space to the guest, irrespective of
120 	 * the host allowing more.
121 	 */
122 	vcpu->arch.vgic_cpu.num_id_bits = ICC_IDR0_EL1_ID_BITS_16BITS;
123 
124 	/*
125 	 * The GICv5 architeture only supports 5-bits of priority in the
126 	 * CPUIF (but potentially fewer in the IRS).
127 	 */
128 	vcpu->arch.vgic_cpu.num_pri_bits = 5;
129 }
130 
131 int vgic_v5_init(struct kvm *kvm)
132 {
133 	struct kvm_vcpu *vcpu;
134 	unsigned long idx;
135 
136 	if (vgic_initialized(kvm))
137 		return 0;
138 
139 	kvm_for_each_vcpu(idx, vcpu, kvm) {
140 		if (vcpu_has_nv(vcpu)) {
141 			kvm_err("Nested GICv5 VMs are currently unsupported\n");
142 			return -EINVAL;
143 		}
144 	}
145 
146 	/* We only allow userspace to drive the SW_PPI, if it is implemented. */
147 	bitmap_zero(kvm->arch.vgic.gicv5_vm.userspace_ppis,
148 		    VGIC_V5_NR_PRIVATE_IRQS);
149 	__assign_bit(GICV5_ARCH_PPI_SW_PPI,
150 		     kvm->arch.vgic.gicv5_vm.userspace_ppis,
151 		     VGIC_V5_NR_PRIVATE_IRQS);
152 	bitmap_and(kvm->arch.vgic.gicv5_vm.userspace_ppis,
153 		   kvm->arch.vgic.gicv5_vm.userspace_ppis,
154 		   ppi_caps.impl_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS);
155 
156 	return 0;
157 }
158 
159 int vgic_v5_map_resources(struct kvm *kvm)
160 {
161 	if (!vgic_initialized(kvm))
162 		return -EBUSY;
163 
164 	return 0;
165 }
166 
167 int vgic_v5_finalize_ppi_state(struct kvm *kvm)
168 {
169 	struct kvm_vcpu *vcpu0;
170 	int i;
171 
172 	if (!vgic_is_v5(kvm))
173 		return 0;
174 
175 	guard(mutex)(&kvm->arch.config_lock);
176 
177 	/*
178 	 * If SW_PPI has been advertised, then we know we already
179 	 * initialised the whole thing, and we can return early. Yes,
180 	 * this is pretty hackish as far as state tracking goes...
181 	 */
182 	if (test_bit(GICV5_ARCH_PPI_SW_PPI, kvm->arch.vgic.gicv5_vm.vgic_ppi_mask))
183 		return 0;
184 
185 	/* The PPI state for all VCPUs should be the same. Pick the first. */
186 	vcpu0 = kvm_get_vcpu(kvm, 0);
187 
188 	bitmap_zero(kvm->arch.vgic.gicv5_vm.vgic_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS);
189 	bitmap_zero(kvm->arch.vgic.gicv5_vm.vgic_ppi_hmr, VGIC_V5_NR_PRIVATE_IRQS);
190 
191 	for_each_set_bit(i, ppi_caps.impl_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS) {
192 		const u32 intid = vgic_v5_make_ppi(i);
193 		struct vgic_irq *irq;
194 
195 		irq = vgic_get_vcpu_irq(vcpu0, intid);
196 
197 		/* Expose PPIs with an owner or the SW_PPI, only */
198 		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) {
199 			if (irq->owner || i == GICV5_ARCH_PPI_SW_PPI) {
200 				__assign_bit(i, kvm->arch.vgic.gicv5_vm.vgic_ppi_mask, 1);
201 				__assign_bit(i, kvm->arch.vgic.gicv5_vm.vgic_ppi_hmr,
202 					     irq->config == VGIC_CONFIG_LEVEL);
203 			}
204 		}
205 
206 		vgic_put_irq(vcpu0->kvm, irq);
207 	}
208 
209 	return 0;
210 }
211 
212 static u32 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu)
213 {
214 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
215 	u32 highest_ap, priority_mask, apr;
216 
217 	/*
218 	 * If the guest's CPU has not opted to receive interrupts, then the
219 	 * effective running priority is the highest priority. Just return 0
220 	 * (the highest priority).
221 	 */
222 	if (!FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_EN, cpu_if->vgic_vmcr))
223 		return 0;
224 
225 	/*
226 	 * Counting the number of trailing zeros gives the current active
227 	 * priority. Explicitly use the 32-bit version here as we have 32
228 	 * priorities. 32 then means that there are no active priorities.
229 	 */
230 	apr = cpu_if->vgic_apr;
231 	highest_ap = apr ? __builtin_ctz(apr) : 32;
232 
233 	/*
234 	 * An interrupt is of sufficient priority if it is equal to or
235 	 * greater than the priority mask. Add 1 to the priority mask
236 	 * (i.e., lower priority) to match the APR logic before taking
237 	 * the min. This gives us the lowest priority that is masked.
238 	 */
239 	priority_mask = FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_VPMR, cpu_if->vgic_vmcr);
240 
241 	return min(highest_ap, priority_mask + 1);
242 }
243 
244 /*
245  * For GICv5, the PPIs are mostly directly managed by the hardware. We (the
246  * hypervisor) handle the pending, active, enable state save/restore, but don't
247  * need the PPIs to be queued on a per-VCPU AP list. Therefore, sanity check the
248  * state, unlock, and return.
249  */
250 bool vgic_v5_ppi_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
251 				  unsigned long flags)
252 	__releases(&irq->irq_lock)
253 {
254 	struct kvm_vcpu *vcpu;
255 
256 	lockdep_assert_held(&irq->irq_lock);
257 
258 	if (WARN_ON_ONCE(!__irq_is_ppi(KVM_DEV_TYPE_ARM_VGIC_V5, irq->intid)))
259 		goto out_unlock_fail;
260 
261 	vcpu = irq->target_vcpu;
262 	if (WARN_ON_ONCE(!vcpu))
263 		goto out_unlock_fail;
264 
265 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
266 
267 	/* Directly kick the target VCPU to make sure it sees the IRQ */
268 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
269 	kvm_vcpu_kick(vcpu);
270 
271 	return true;
272 
273 out_unlock_fail:
274 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
275 
276 	return false;
277 }
278 
279 /*
280  * Sets/clears the corresponding bit in the ICH_PPI_DVIR register.
281  */
282 void vgic_v5_set_ppi_dvi(struct kvm_vcpu *vcpu, struct vgic_irq *irq, bool dvi)
283 {
284 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
285 	u32 ppi;
286 
287 	lockdep_assert_held(&irq->irq_lock);
288 
289 	ppi = vgic_v5_get_hwirq_id(irq->intid);
290 	__assign_bit(ppi, cpu_if->vgic_ppi_dvir, dvi);
291 }
292 
293 static struct irq_ops vgic_v5_ppi_irq_ops = {
294 	.queue_irq_unlock = vgic_v5_ppi_queue_irq_unlock,
295 	.set_direct_injection = vgic_v5_set_ppi_dvi,
296 };
297 
298 void vgic_v5_set_ppi_ops(struct kvm_vcpu *vcpu, u32 vintid)
299 {
300 	kvm_vgic_set_irq_ops(vcpu, vintid, &vgic_v5_ppi_irq_ops);
301 }
302 
303 /*
304  * Sync back the PPI priorities to the vgic_irq shadow state for any interrupts
305  * exposed to the guest (skipping all others).
306  */
307 static void vgic_v5_sync_ppi_priorities(struct kvm_vcpu *vcpu)
308 {
309 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
310 	u64 priorityr;
311 	int i;
312 
313 	/*
314 	 * We have up to 16 PPI Priority regs, but only have a few interrupts
315 	 * that the guest is allowed to use. Limit our sync of PPI priorities to
316 	 * those actually exposed to the guest by first iterating over the mask
317 	 * of exposed PPIs.
318 	 */
319 	for_each_set_bit(i, vcpu->kvm->arch.vgic.gicv5_vm.vgic_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS) {
320 		u32 intid = vgic_v5_make_ppi(i);
321 		struct vgic_irq *irq;
322 		int pri_idx, pri_reg, pri_bit;
323 		u8 priority;
324 
325 		/*
326 		 * Determine which priority register and the field within it to
327 		 * extract.
328 		 */
329 		pri_reg = i / 8;
330 		pri_idx = i % 8;
331 		pri_bit = pri_idx * 8;
332 
333 		priorityr = cpu_if->vgic_ppi_priorityr[pri_reg];
334 		priority = field_get(GENMASK(pri_bit + 4, pri_bit), priorityr);
335 
336 		irq = vgic_get_vcpu_irq(vcpu, intid);
337 
338 		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock)
339 			irq->priority = priority;
340 
341 		vgic_put_irq(vcpu->kvm, irq);
342 	}
343 }
344 
345 bool vgic_v5_has_pending_ppi(struct kvm_vcpu *vcpu)
346 {
347 	unsigned int priority_mask;
348 	int i;
349 
350 	priority_mask = vgic_v5_get_effective_priority_mask(vcpu);
351 
352 	/*
353 	 * If the combined priority mask is 0, nothing can be signalled! In the
354 	 * case where the guest has disabled interrupt delivery for the vcpu
355 	 * (via ICV_CR0_EL1.EN->ICH_VMCR_EL2.EN), we calculate the priority mask
356 	 * as 0 too (the highest possible priority).
357 	 */
358 	if (!priority_mask)
359 		return false;
360 
361 	for_each_set_bit(i, vcpu->kvm->arch.vgic.gicv5_vm.vgic_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS) {
362 		u32 intid = vgic_v5_make_ppi(i);
363 		bool has_pending = false;
364 		struct vgic_irq *irq;
365 
366 		irq = vgic_get_vcpu_irq(vcpu, intid);
367 
368 		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock)
369 			if (irq->enabled && irq->priority < priority_mask)
370 				has_pending = irq->hw ? vgic_get_phys_line_level(irq) : irq_is_pending(irq);
371 
372 		vgic_put_irq(vcpu->kvm, irq);
373 
374 		if (has_pending)
375 			return true;
376 	}
377 
378 	return false;
379 }
380 
381 /*
382  * Detect any PPIs state changes, and propagate the state with KVM's
383  * shadow structures.
384  */
385 void vgic_v5_fold_ppi_state(struct kvm_vcpu *vcpu)
386 {
387 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
388 	unsigned long *activer, *pendr;
389 	int i;
390 
391 	activer = host_data_ptr(vgic_v5_ppi_state)->activer_exit;
392 	pendr = host_data_ptr(vgic_v5_ppi_state)->pendr;
393 
394 	for_each_set_bit(i, vcpu->kvm->arch.vgic.gicv5_vm.vgic_ppi_mask,
395 			 VGIC_V5_NR_PRIVATE_IRQS) {
396 		u32 intid = vgic_v5_make_ppi(i);
397 		struct vgic_irq *irq;
398 
399 		irq = vgic_get_vcpu_irq(vcpu, intid);
400 
401 		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) {
402 			irq->active = test_bit(i, activer);
403 
404 			/* This is an OR to avoid losing incoming edges! */
405 			if (irq->config == VGIC_CONFIG_EDGE)
406 				irq->pending_latch |= test_bit(i, pendr);
407 		}
408 
409 		vgic_put_irq(vcpu->kvm, irq);
410 	}
411 
412 	/*
413 	 * Re-inject the exit state as entry state next time!
414 	 *
415 	 * Note that the write of the Enable state is trapped, and hence there
416 	 * is nothing to explcitly sync back here as we already have the latest
417 	 * copy by definition.
418 	 */
419 	bitmap_copy(cpu_if->vgic_ppi_activer, activer, VGIC_V5_NR_PRIVATE_IRQS);
420 }
421 
422 void vgic_v5_flush_ppi_state(struct kvm_vcpu *vcpu)
423 {
424 	DECLARE_BITMAP(pendr, VGIC_V5_NR_PRIVATE_IRQS);
425 	int i;
426 
427 	/*
428 	 * Time to enter the guest - we first need to build the guest's
429 	 * ICC_PPI_PENDRx_EL1, however.
430 	 */
431 	bitmap_zero(pendr, VGIC_V5_NR_PRIVATE_IRQS);
432 	for_each_set_bit(i, vcpu->kvm->arch.vgic.gicv5_vm.vgic_ppi_mask,
433 			 VGIC_V5_NR_PRIVATE_IRQS) {
434 		u32 intid = vgic_v5_make_ppi(i);
435 		struct vgic_irq *irq;
436 
437 		irq = vgic_get_vcpu_irq(vcpu, intid);
438 
439 		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) {
440 			__assign_bit(i, pendr, irq_is_pending(irq));
441 			if (irq->config == VGIC_CONFIG_EDGE)
442 				irq->pending_latch = false;
443 		}
444 
445 		vgic_put_irq(vcpu->kvm, irq);
446 	}
447 
448 	/*
449 	 * Copy the shadow state to the pending reg that will be written to the
450 	 * ICH_PPI_PENDRx_EL2 regs. While the guest is running we track any
451 	 * incoming changes to the pending state in the vgic_irq structures. The
452 	 * incoming changes are merged with the outgoing changes on the return
453 	 * path.
454 	 */
455 	bitmap_copy(host_data_ptr(vgic_v5_ppi_state)->pendr, pendr,
456 		    VGIC_V5_NR_PRIVATE_IRQS);
457 }
458 
459 void vgic_v5_load(struct kvm_vcpu *vcpu)
460 {
461 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
462 
463 	/*
464 	 * On the WFI path, vgic_load is called a second time. The first is when
465 	 * scheduling in the vcpu thread again, and the second is when leaving
466 	 * WFI. Skip the second instance as it serves no purpose and just
467 	 * restores the same state again.
468 	 */
469 	if (cpu_if->gicv5_vpe.resident)
470 		return;
471 
472 	kvm_call_hyp(__vgic_v5_restore_vmcr_apr, cpu_if);
473 
474 	cpu_if->gicv5_vpe.resident = true;
475 }
476 
477 void vgic_v5_put(struct kvm_vcpu *vcpu)
478 {
479 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
480 
481 	/*
482 	 * Do nothing if we're not resident. This can happen in the WFI path
483 	 * where we do a vgic_put in the WFI path and again later when
484 	 * descheduling the thread. We risk losing VMCR state if we sync it
485 	 * twice, so instead return early in this case.
486 	 */
487 	if (!cpu_if->gicv5_vpe.resident)
488 		return;
489 
490 	kvm_call_hyp(__vgic_v5_save_apr, cpu_if);
491 
492 	cpu_if->gicv5_vpe.resident = false;
493 
494 	/* The shadow priority is only updated on entering WFI */
495 	if (vcpu_get_flag(vcpu, IN_WFI))
496 		vgic_v5_sync_ppi_priorities(vcpu);
497 }
498 
499 void vgic_v5_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
500 {
501 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
502 	u64 vmcr = cpu_if->vgic_vmcr;
503 
504 	vmcrp->en = FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_EN, vmcr);
505 	vmcrp->pmr = FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_VPMR, vmcr);
506 }
507 
508 void vgic_v5_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
509 {
510 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
511 	u64 vmcr;
512 
513 	vmcr = FIELD_PREP(FEAT_GCIE_ICH_VMCR_EL2_VPMR, vmcrp->pmr) |
514 	       FIELD_PREP(FEAT_GCIE_ICH_VMCR_EL2_EN, vmcrp->en);
515 
516 	cpu_if->vgic_vmcr = vmcr;
517 }
518 
519 void vgic_v5_restore_state(struct kvm_vcpu *vcpu)
520 {
521 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
522 
523 	__vgic_v5_restore_state(cpu_if);
524 	__vgic_v5_restore_ppi_state(cpu_if);
525 	dsb(sy);
526 }
527 
528 void vgic_v5_save_state(struct kvm_vcpu *vcpu)
529 {
530 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
531 
532 	__vgic_v5_save_state(cpu_if);
533 	__vgic_v5_save_ppi_state(cpu_if);
534 	dsb(sy);
535 }
536