xref: /linux/arch/arm64/kvm/vgic/vgic.c (revision 439d8f9cb01cb04f9b31910efd0ae8dbdfa4ad97)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015, 2016 ARM Ltd.
4  */
5 
6 #include <linux/interrupt.h>
7 #include <linux/irq.h>
8 #include <linux/kvm.h>
9 #include <linux/kvm_host.h>
10 #include <linux/list_sort.h>
11 #include <linux/nospec.h>
12 
13 #include <asm/kvm_hyp.h>
14 
15 #include "vgic.h"
16 
17 #define CREATE_TRACE_POINTS
18 #include "trace.h"
19 
20 struct vgic_global kvm_vgic_global_state __ro_after_init = {
21 	.gicv3_cpuif = STATIC_KEY_FALSE_INIT,
22 };
23 
24 /*
25  * Locking order is always:
26  * kvm->lock (mutex)
27  *   vcpu->mutex (mutex)
28  *     kvm->arch.config_lock (mutex)
29  *       its->cmd_lock (mutex)
30  *         its->its_lock (mutex)
31  *           vgic_dist->lpi_xa.xa_lock		must be taken with IRQs disabled
32  *             vgic_cpu->ap_list_lock		must be taken with IRQs disabled
33  *               vgic_irq->irq_lock		must be taken with IRQs disabled
34  *
35  * As the ap_list_lock might be taken from the timer interrupt handler,
36  * we have to disable IRQs before taking this lock and everything lower
37  * than it.
38  *
39  * The config_lock has additional ordering requirements:
40  * kvm->slots_lock
41  *   kvm->srcu
42  *     kvm->arch.config_lock
43  *
44  * If you need to take multiple locks, always take the upper lock first,
45  * then the lower ones, e.g. first take the its_lock, then the irq_lock.
46  * If you are already holding a lock and need to take a higher one, you
47  * have to drop the lower ranking lock first and re-acquire it after having
48  * taken the upper one.
49  *
50  * When taking more than one ap_list_lock at the same time, always take the
51  * lowest numbered VCPU's ap_list_lock first, so:
52  *   vcpuX->vcpu_id < vcpuY->vcpu_id:
53  *     raw_spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
54  *     raw_spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
55  *
56  * Since the VGIC must support injecting virtual interrupts from ISRs, we have
57  * to use the raw_spin_lock_irqsave/raw_spin_unlock_irqrestore versions of outer
58  * spinlocks for any lock that may be taken while injecting an interrupt.
59  */
60 
61 /*
62  * Index the VM's xarray of mapped LPIs and return a reference to the IRQ
63  * structure. The caller is expected to call vgic_put_irq() later once it's
64  * finished with the IRQ.
65  */
66 static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
67 {
68 	struct vgic_dist *dist = &kvm->arch.vgic;
69 	struct vgic_irq *irq = NULL;
70 
71 	rcu_read_lock();
72 
73 	irq = xa_load(&dist->lpi_xa, intid);
74 	if (!vgic_try_get_irq_ref(irq))
75 		irq = NULL;
76 
77 	rcu_read_unlock();
78 
79 	return irq;
80 }
81 
82 /*
83  * This looks up the virtual interrupt ID to get the corresponding
84  * struct vgic_irq. It also increases the refcount, so any caller is expected
85  * to call vgic_put_irq() once it's finished with this IRQ.
86  */
87 struct vgic_irq *vgic_get_irq(struct kvm *kvm, u32 intid)
88 {
89 	enum kvm_device_type type = kvm->arch.vgic.vgic_model;
90 
91 	/* SPIs */
92 	if (__irq_is_spi(type, intid)) {
93 		switch (type) {
94 		case KVM_DEV_TYPE_ARM_VGIC_V5:
95 			intid = vgic_v5_get_hwirq_id(intid);
96 
97 			if (intid >= kvm->arch.vgic.nr_spis)
98 				return NULL;
99 
100 			intid = array_index_nospec(intid, kvm->arch.vgic.nr_spis);
101 			return &kvm->arch.vgic.spis[intid];
102 		default: {
103 			u32 max_intid = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
104 
105 			if (intid < max_intid) {
106 				intid -= VGIC_NR_PRIVATE_IRQS;
107 				intid = array_index_nospec(intid, kvm->arch.vgic.nr_spis);
108 				return &kvm->arch.vgic.spis[intid];
109 			}
110 		}}
111 	}
112 
113 	/* LPIs */
114 	if (__irq_is_lpi(type, intid))
115 		return vgic_get_lpi(kvm, intid);
116 
117 	return NULL;
118 }
119 
120 struct vgic_irq *vgic_get_vcpu_irq(struct kvm_vcpu *vcpu, u32 intid)
121 {
122 	enum kvm_device_type type;
123 
124 	if (WARN_ON(!vcpu))
125 		return NULL;
126 
127 	type = vcpu->kvm->arch.vgic.vgic_model;
128 
129 	if (__irq_is_sgi(type, intid) || __irq_is_ppi(type, intid)) {
130 		switch (type) {
131 		case KVM_DEV_TYPE_ARM_VGIC_V5:
132 			intid = vgic_v5_get_hwirq_id(intid);
133 			if (intid >= VGIC_V5_NR_PRIVATE_IRQS)
134 				return NULL;
135 			intid = array_index_nospec(intid, VGIC_V5_NR_PRIVATE_IRQS);
136 			break;
137 		default:
138 			intid = array_index_nospec(intid, VGIC_NR_PRIVATE_IRQS);
139 		}
140 
141 		return &vcpu->arch.vgic_cpu.private_irqs[intid];
142 	}
143 
144 	return vgic_get_irq(vcpu->kvm, intid);
145 }
146 
147 static void vgic_release_lpi_locked(struct vgic_dist *dist, struct vgic_irq *irq)
148 {
149 	lockdep_assert_held(&dist->lpi_xa.xa_lock);
150 	__xa_erase(&dist->lpi_xa, irq->intid);
151 	kfree_rcu(irq, rcu);
152 }
153 
154 static __must_check bool __vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
155 {
156 	if (!irq_is_lpi(kvm, irq->intid))
157 		return false;
158 
159 	return refcount_dec_and_test(&irq->refcount);
160 }
161 
162 static __must_check bool vgic_put_irq_norelease(struct kvm *kvm, struct vgic_irq *irq)
163 {
164 	return __vgic_put_irq(kvm, irq);
165 }
166 
167 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
168 {
169 	struct vgic_dist *dist = &kvm->arch.vgic;
170 	unsigned long flags;
171 
172 	/*
173 	 * Normally the lock is only taken when the refcount drops to 0.
174 	 * Acquire/release it early on lockdep kernels to make locking issues
175 	 * in rare release paths a bit more obvious.
176 	 */
177 	if (IS_ENABLED(CONFIG_LOCKDEP) && irq_is_lpi(kvm, irq->intid)) {
178 		guard(spinlock_irqsave)(&dist->lpi_xa.xa_lock);
179 	}
180 
181 	if (!irq_is_lpi(kvm, irq->intid))
182 		return;
183 
184 	if (refcount_dec_and_lock_irqsave(&irq->refcount,
185 					  &dist->lpi_xa.xa_lock, &flags)) {
186 		vgic_release_lpi_locked(dist, irq);
187 		xa_unlock_irqrestore(&dist->lpi_xa, flags);
188 	}
189 }
190 
191 static void vgic_release_deleted_lpis(struct kvm *kvm)
192 {
193 	struct vgic_dist *dist = &kvm->arch.vgic;
194 	unsigned long flags, intid;
195 	struct vgic_irq *irq;
196 
197 	xa_lock_irqsave(&dist->lpi_xa, flags);
198 
199 	xa_for_each(&dist->lpi_xa, intid, irq) {
200 		if (!refcount_read(&irq->refcount))
201 			vgic_release_lpi_locked(dist, irq);
202 	}
203 
204 	xa_unlock_irqrestore(&dist->lpi_xa, flags);
205 }
206 
207 void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu)
208 {
209 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
210 	struct vgic_irq *irq, *tmp;
211 	bool deleted = false;
212 	unsigned long flags;
213 
214 	raw_spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
215 
216 	list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
217 		if (irq_is_lpi(vcpu->kvm, irq->intid)) {
218 			raw_spin_lock(&irq->irq_lock);
219 			irq->pending_latch = false;
220 			list_del(&irq->ap_list);
221 			irq->vcpu = NULL;
222 			raw_spin_unlock(&irq->irq_lock);
223 			deleted |= vgic_put_irq_norelease(vcpu->kvm, irq);
224 		}
225 	}
226 
227 	raw_spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
228 
229 	if (deleted)
230 		vgic_release_deleted_lpis(vcpu->kvm);
231 }
232 
233 void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
234 {
235 	WARN_ON(irq_set_irqchip_state(irq->host_irq,
236 				      IRQCHIP_STATE_PENDING,
237 				      pending));
238 }
239 
240 bool vgic_get_phys_line_level(struct vgic_irq *irq)
241 {
242 	bool line_level;
243 
244 	BUG_ON(!irq->hw);
245 
246 	if (irq->ops && irq->ops->get_input_level)
247 		return irq->ops->get_input_level(irq->intid);
248 
249 	WARN_ON(irq_get_irqchip_state(irq->host_irq,
250 				      IRQCHIP_STATE_PENDING,
251 				      &line_level));
252 	return line_level;
253 }
254 
255 /* Set/Clear the physical active state */
256 void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active)
257 {
258 
259 	BUG_ON(!irq->hw);
260 	WARN_ON(irq_set_irqchip_state(irq->host_irq,
261 				      IRQCHIP_STATE_ACTIVE,
262 				      active));
263 }
264 
265 /**
266  * vgic_target_oracle - compute the target vcpu for an irq
267  *
268  * @irq:	The irq to route. Must be already locked.
269  *
270  * Based on the current state of the interrupt (enabled, pending,
271  * active, vcpu and target_vcpu), compute the next vcpu this should be
272  * given to. Return NULL if this shouldn't be injected at all.
273  *
274  * Requires the IRQ lock to be held.
275  */
276 struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
277 {
278 	lockdep_assert_held(&irq->irq_lock);
279 
280 	/* If the interrupt is active, it must stay on the current vcpu */
281 	if (irq->active)
282 		return irq->vcpu ? : irq->target_vcpu;
283 
284 	/*
285 	 * If the IRQ is not active but enabled and pending, we should direct
286 	 * it to its configured target VCPU.
287 	 * If the distributor is disabled, pending interrupts shouldn't be
288 	 * forwarded.
289 	 */
290 	if (irq->enabled && irq_is_pending(irq)) {
291 		if (unlikely(irq->target_vcpu &&
292 			     !irq->target_vcpu->kvm->arch.vgic.enabled))
293 			return NULL;
294 
295 		return irq->target_vcpu;
296 	}
297 
298 	/* If neither active nor pending and enabled, then this IRQ should not
299 	 * be queued to any VCPU.
300 	 */
301 	return NULL;
302 }
303 
304 struct vgic_sort_info {
305 	struct kvm_vcpu *vcpu;
306 	struct vgic_vmcr vmcr;
307 };
308 
309 /*
310  * The order of items in the ap_lists defines how we'll pack things in LRs as
311  * well, the first items in the list being the first things populated in the
312  * LRs.
313  *
314  * Pending, non-active interrupts must be placed at the head of the list.
315  * Otherwise things should be sorted by the priority field and the GIC
316  * hardware support will take care of preemption of priority groups etc.
317  * Interrupts that are not deliverable should be at the end of the list.
318  *
319  * Return negative if "a" sorts before "b", 0 to preserve order, and positive
320  * to sort "b" before "a".
321  */
322 static int vgic_irq_cmp(void *priv, const struct list_head *a,
323 			const struct list_head *b)
324 {
325 	struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
326 	struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
327 	struct vgic_sort_info *info = priv;
328 	struct kvm_vcpu *vcpu = info->vcpu;
329 	bool penda, pendb;
330 	int ret;
331 
332 	/*
333 	 * list_sort may call this function with the same element when
334 	 * the list is fairly long.
335 	 */
336 	if (unlikely(irqa == irqb))
337 		return 0;
338 
339 	raw_spin_lock(&irqa->irq_lock);
340 	raw_spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
341 
342 	/* Undeliverable interrupts should be last */
343 	ret = (int)(vgic_target_oracle(irqb) == vcpu) - (int)(vgic_target_oracle(irqa) == vcpu);
344 	if (ret)
345 		goto out;
346 
347 	/* Same thing for interrupts targeting a disabled group */
348 	ret =  (int)(irqb->group ? info->vmcr.grpen1 : info->vmcr.grpen0);
349 	ret -= (int)(irqa->group ? info->vmcr.grpen1 : info->vmcr.grpen0);
350 	if (ret)
351 		goto out;
352 
353 	penda = irqa->enabled && irq_is_pending(irqa) && !irqa->active;
354 	pendb = irqb->enabled && irq_is_pending(irqb) && !irqb->active;
355 
356 	ret = (int)pendb - (int)penda;
357 	if (ret)
358 		goto out;
359 
360 	/* Both pending and enabled, sort by priority (lower number first) */
361 	ret = (int)irqa->priority - (int)irqb->priority;
362 	if (ret)
363 		goto out;
364 
365 	/* Finally, HW bit active interrupts have priority over non-HW ones */
366 	ret = (int)irqb->hw - (int)irqa->hw;
367 
368 out:
369 	raw_spin_unlock(&irqb->irq_lock);
370 	raw_spin_unlock(&irqa->irq_lock);
371 	return ret;
372 }
373 
374 /* Must be called with the ap_list_lock held */
375 static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
376 {
377 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
378 	struct vgic_sort_info info = { .vcpu = vcpu, };
379 
380 	lockdep_assert_held(&vgic_cpu->ap_list_lock);
381 
382 	vgic_get_vmcr(vcpu, &info.vmcr);
383 	list_sort(&info, &vgic_cpu->ap_list_head, vgic_irq_cmp);
384 }
385 
386 /*
387  * Only valid injection if changing level for level-triggered IRQs or for a
388  * rising edge, and in-kernel connected IRQ lines can only be controlled by
389  * their owner.
390  */
391 static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
392 {
393 	if (irq->owner != owner)
394 		return false;
395 
396 	switch (irq->config) {
397 	case VGIC_CONFIG_LEVEL:
398 		return irq->line_level != level;
399 	case VGIC_CONFIG_EDGE:
400 		return level;
401 	}
402 
403 	return false;
404 }
405 
406 static bool vgic_model_needs_bcst_kick(struct kvm *kvm)
407 {
408 	/*
409 	 * A GICv3 (or GICv3-like) system exposing a GICv3 to the guest
410 	 * needs a broadcast kick to set TDIR globally.
411 	 *
412 	 * For systems that do not have TDIR (ARM's own v8.0 CPUs), the
413 	 * shadow TDIR bit is always set, and so is the register's TC bit,
414 	 * so no need to kick the CPUs.
415 	 */
416 	return (cpus_have_final_cap(ARM64_HAS_ICH_HCR_EL2_TDIR) &&
417 		kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3);
418 }
419 
420 /*
421  * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
422  * Do the queuing if necessary, taking the right locks in the right order.
423  * Returns true when the IRQ was queued, false otherwise.
424  *
425  * Needs to be entered with the IRQ lock already held, but will return
426  * with all locks dropped.
427  */
428 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
429 			   unsigned long flags) __releases(&irq->irq_lock)
430 {
431 	struct kvm_vcpu *vcpu;
432 	bool bcast;
433 
434 	lockdep_assert_held(&irq->irq_lock);
435 
436 	if (irq->ops && irq->ops->queue_irq_unlock)
437 		return irq->ops->queue_irq_unlock(kvm, irq, flags);
438 
439 retry:
440 	vcpu = vgic_target_oracle(irq);
441 	if (irq->vcpu || !vcpu) {
442 		/*
443 		 * If this IRQ is already on a VCPU's ap_list, then it
444 		 * cannot be moved or modified and there is no more work for
445 		 * us to do.
446 		 *
447 		 * Otherwise, if the irq is not pending and enabled, it does
448 		 * not need to be inserted into an ap_list and there is also
449 		 * no more work for us to do.
450 		 */
451 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
452 
453 		/*
454 		 * We have to kick the VCPU here, because we could be
455 		 * queueing an edge-triggered interrupt for which we
456 		 * get no EOI maintenance interrupt. In that case,
457 		 * while the IRQ is already on the VCPU's AP list, the
458 		 * VCPU could have EOI'ed the original interrupt and
459 		 * won't see this one until it exits for some other
460 		 * reason.
461 		 */
462 		if (vcpu) {
463 			kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
464 			kvm_vcpu_kick(vcpu);
465 		}
466 		return false;
467 	}
468 
469 	/*
470 	 * We must unlock the irq lock to take the ap_list_lock where
471 	 * we are going to insert this new pending interrupt.
472 	 */
473 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
474 
475 	/* someone can do stuff here, which we re-check below */
476 
477 	raw_spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
478 	raw_spin_lock(&irq->irq_lock);
479 
480 	/*
481 	 * Did something change behind our backs?
482 	 *
483 	 * There are two cases:
484 	 * 1) The irq lost its pending state or was disabled behind our
485 	 *    backs and/or it was queued to another VCPU's ap_list.
486 	 * 2) Someone changed the affinity on this irq behind our
487 	 *    backs and we are now holding the wrong ap_list_lock.
488 	 *
489 	 * In both cases, drop the locks and retry.
490 	 */
491 
492 	if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
493 		raw_spin_unlock(&irq->irq_lock);
494 		raw_spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock,
495 					   flags);
496 
497 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
498 		goto retry;
499 	}
500 
501 	/*
502 	 * Grab a reference to the irq to reflect the fact that it is
503 	 * now in the ap_list. This is safe as the caller must already hold a
504 	 * reference on the irq.
505 	 */
506 	vgic_get_irq_ref(irq);
507 	list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
508 	irq->vcpu = vcpu;
509 
510 	/* A new SPI may result in deactivation trapping on all vcpus */
511 	bcast = (vgic_model_needs_bcst_kick(vcpu->kvm) &&
512 		 vgic_valid_spi(vcpu->kvm, irq->intid) &&
513 		 atomic_fetch_inc(&vcpu->kvm->arch.vgic.active_spis) == 0);
514 
515 	raw_spin_unlock(&irq->irq_lock);
516 	raw_spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
517 
518 	if (!bcast) {
519 		kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
520 		kvm_vcpu_kick(vcpu);
521 	} else {
522 		kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_IRQ_PENDING);
523 	}
524 
525 	return true;
526 }
527 
528 /**
529  * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
530  * @kvm:     The VM structure pointer
531  * @vcpu:    The CPU for PPIs or NULL for global interrupts
532  * @intid:   The INTID to inject a new state to.
533  * @level:   Edge-triggered:  true:  to trigger the interrupt
534  *			      false: to ignore the call
535  *	     Level-sensitive  true:  raise the input signal
536  *			      false: lower the input signal
537  * @owner:   The opaque pointer to the owner of the IRQ being raised to verify
538  *           that the caller is allowed to inject this IRQ.  Userspace
539  *           injections will have owner == NULL.
540  *
541  * The VGIC is not concerned with devices being active-LOW or active-HIGH for
542  * level-sensitive interrupts.  You can think of the level parameter as 1
543  * being HIGH and 0 being LOW and all devices being active-HIGH.
544  */
545 int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
546 			unsigned int intid, bool level, void *owner)
547 {
548 	struct vgic_irq *irq;
549 	unsigned long flags;
550 
551 	if (unlikely(!vgic_initialized(kvm)))
552 		return 0;
553 
554 	if (!vcpu && irq_is_private(kvm, intid))
555 		return -EINVAL;
556 
557 	trace_vgic_update_irq_pending(vcpu ? vcpu->vcpu_idx : 0, intid, level);
558 
559 	if (irq_is_private(kvm, intid))
560 		irq = vgic_get_vcpu_irq(vcpu, intid);
561 	else
562 		irq = vgic_get_irq(kvm, intid);
563 	if (!irq)
564 		return -EINVAL;
565 
566 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
567 
568 	if (!vgic_validate_injection(irq, level, owner)) {
569 		/* Nothing to see here, move along... */
570 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
571 		vgic_put_irq(kvm, irq);
572 		return 0;
573 	}
574 
575 	if (irq->config == VGIC_CONFIG_LEVEL)
576 		irq->line_level = level;
577 	else
578 		irq->pending_latch = true;
579 
580 	if (irq->ops && irq->ops->set_pending_state)
581 		WARN_ON_ONCE(!irq->ops->set_pending_state(vcpu, irq));
582 
583 	vgic_queue_irq_unlock(kvm, irq, flags);
584 	vgic_put_irq(kvm, irq);
585 
586 	return 0;
587 }
588 
589 void kvm_vgic_set_irq_ops(struct kvm_vcpu *vcpu, u32 vintid,
590 			  const struct irq_ops *ops)
591 {
592 	struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, vintid);
593 
594 	BUG_ON(!irq);
595 
596 	scoped_guard(raw_spinlock_irqsave, &irq->irq_lock)
597 		irq->ops = ops;
598 
599 	vgic_put_irq(vcpu->kvm, irq);
600 }
601 
602 void kvm_vgic_clear_irq_ops(struct kvm_vcpu *vcpu, u32 vintid)
603 {
604 	kvm_vgic_set_irq_ops(vcpu, vintid, NULL);
605 }
606 
607 /* @irq->irq_lock must be held */
608 static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
609 			    unsigned int host_irq)
610 {
611 	struct irq_desc *desc;
612 	struct irq_data *data;
613 
614 	/*
615 	 * Find the physical IRQ number corresponding to @host_irq
616 	 */
617 	desc = irq_to_desc(host_irq);
618 	if (!desc) {
619 		kvm_err("%s: no interrupt descriptor\n", __func__);
620 		return -EINVAL;
621 	}
622 	data = irq_desc_get_irq_data(desc);
623 	while (data->parent_data)
624 		data = data->parent_data;
625 
626 	irq->hw = true;
627 	irq->host_irq = host_irq;
628 	irq->hwintid = data->hwirq;
629 
630 	if (irq->ops && irq->ops->set_direct_injection)
631 		irq->ops->set_direct_injection(vcpu, irq, true);
632 
633 	return 0;
634 }
635 
636 /* @irq->irq_lock must be held */
637 static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
638 {
639 	if (irq->ops && irq->ops->set_direct_injection)
640 		irq->ops->set_direct_injection(irq->target_vcpu, irq, false);
641 
642 	irq->hw = false;
643 	irq->hwintid = 0;
644 }
645 
646 int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
647 			  u32 vintid)
648 {
649 	struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, vintid);
650 	unsigned long flags;
651 	int ret;
652 
653 	BUG_ON(!irq);
654 
655 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
656 	ret = kvm_vgic_map_irq(vcpu, irq, host_irq);
657 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
658 	vgic_put_irq(vcpu->kvm, irq);
659 
660 	return ret;
661 }
662 
663 /**
664  * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
665  * @vcpu: The VCPU pointer
666  * @vintid: The INTID of the interrupt
667  *
668  * Reset the active and pending states of a mapped interrupt.  Kernel
669  * subsystems injecting mapped interrupts should reset their interrupt lines
670  * when we are doing a reset of the VM.
671  */
672 void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
673 {
674 	struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, vintid);
675 	unsigned long flags;
676 
677 	if (!irq->hw)
678 		goto out;
679 
680 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
681 	irq->active = false;
682 	irq->pending_latch = false;
683 	irq->line_level = false;
684 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
685 out:
686 	vgic_put_irq(vcpu->kvm, irq);
687 }
688 
689 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
690 {
691 	struct vgic_irq *irq;
692 	unsigned long flags;
693 
694 	if (!vgic_initialized(vcpu->kvm))
695 		return -EAGAIN;
696 
697 	irq = vgic_get_vcpu_irq(vcpu, vintid);
698 	BUG_ON(!irq);
699 
700 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
701 	kvm_vgic_unmap_irq(irq);
702 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
703 	vgic_put_irq(vcpu->kvm, irq);
704 
705 	return 0;
706 }
707 
708 int kvm_vgic_get_map(struct kvm_vcpu *vcpu, unsigned int vintid)
709 {
710 	struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, vintid);
711 	unsigned long flags;
712 	int ret = -1;
713 
714 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
715 	if (irq->hw)
716 		ret = irq->hwintid;
717 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
718 
719 	vgic_put_irq(vcpu->kvm, irq);
720 	return ret;
721 }
722 
723 /**
724  * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
725  *
726  * @vcpu:   Pointer to the VCPU (used for PPIs)
727  * @intid:  The virtual INTID identifying the interrupt (PPI or SPI)
728  * @owner:  Opaque pointer to the owner
729  *
730  * Returns 0 if intid is not already used by another in-kernel device and the
731  * owner is set, otherwise returns an error code.
732  */
733 int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
734 {
735 	struct vgic_irq *irq;
736 	unsigned long flags;
737 	int ret = 0;
738 
739 	if (!vgic_initialized(vcpu->kvm))
740 		return -EAGAIN;
741 
742 	/* SGIs and LPIs cannot be wired up to any device */
743 	if (!irq_is_ppi(vcpu->kvm, intid) && !vgic_valid_spi(vcpu->kvm, intid))
744 		return -EINVAL;
745 
746 	irq = vgic_get_vcpu_irq(vcpu, intid);
747 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
748 	if (irq->owner && irq->owner != owner)
749 		ret = -EEXIST;
750 	else
751 		irq->owner = owner;
752 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
753 
754 	return ret;
755 }
756 
757 /**
758  * vgic_prune_ap_list - Remove non-relevant interrupts from the list
759  *
760  * @vcpu: The VCPU pointer
761  *
762  * Go over the list of "interesting" interrupts, and prune those that we
763  * won't have to consider in the near future.
764  */
765 static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
766 {
767 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
768 	struct vgic_irq *irq, *tmp;
769 	bool deleted_lpis = false;
770 
771 	DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
772 
773 retry:
774 	raw_spin_lock(&vgic_cpu->ap_list_lock);
775 
776 	list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
777 		struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
778 		bool target_vcpu_needs_kick = false;
779 
780 		raw_spin_lock(&irq->irq_lock);
781 
782 		BUG_ON(vcpu != irq->vcpu);
783 
784 		target_vcpu = vgic_target_oracle(irq);
785 
786 		if (!target_vcpu) {
787 			/*
788 			 * We don't need to process this interrupt any
789 			 * further, move it off the list.
790 			 */
791 			list_del(&irq->ap_list);
792 			irq->vcpu = NULL;
793 			raw_spin_unlock(&irq->irq_lock);
794 
795 			/*
796 			 * This vgic_put_irq call matches the
797 			 * vgic_get_irq_ref in vgic_queue_irq_unlock,
798 			 * where we added the LPI to the ap_list. As
799 			 * we remove the irq from the list, we drop
800 			 * also drop the refcount.
801 			 */
802 			deleted_lpis |= vgic_put_irq_norelease(vcpu->kvm, irq);
803 			continue;
804 		}
805 
806 		if (target_vcpu == vcpu) {
807 			/* We're on the right CPU */
808 			raw_spin_unlock(&irq->irq_lock);
809 			continue;
810 		}
811 
812 		/*
813 		 * This interrupt looks like it has to be migrated,
814 		 * make sure it is kept alive while locks are dropped.
815 		 */
816 		vgic_get_irq_ref(irq);
817 
818 		raw_spin_unlock(&irq->irq_lock);
819 		raw_spin_unlock(&vgic_cpu->ap_list_lock);
820 
821 		/*
822 		 * Ensure locking order by always locking the smallest
823 		 * ID first.
824 		 */
825 		if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
826 			vcpuA = vcpu;
827 			vcpuB = target_vcpu;
828 		} else {
829 			vcpuA = target_vcpu;
830 			vcpuB = vcpu;
831 		}
832 
833 		raw_spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
834 		raw_spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
835 				      SINGLE_DEPTH_NESTING);
836 		raw_spin_lock(&irq->irq_lock);
837 
838 		/*
839 		 * If the interrupt is still ours and its affinity has
840 		 * been preserved, move it around. Otherwise, it means
841 		 * things have changed while the interrupt was unlocked
842 		 * (it may even have been taken off the list with its
843 		 * affinity left untouched), and we need to replay this.
844 		 *
845 		 * In all cases, we cannot trust the list not to have
846 		 * changed, so we restart from the beginning.
847 		 */
848 		if (irq->vcpu == vcpu && target_vcpu == vgic_target_oracle(irq)) {
849 			struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
850 
851 			list_del(&irq->ap_list);
852 			irq->vcpu = target_vcpu;
853 			list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
854 			target_vcpu_needs_kick = true;
855 		}
856 
857 		raw_spin_unlock(&irq->irq_lock);
858 		raw_spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
859 		raw_spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
860 
861 		deleted_lpis |= vgic_put_irq_norelease(vcpu->kvm, irq);
862 
863 		if (target_vcpu_needs_kick) {
864 			kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
865 			kvm_vcpu_kick(target_vcpu);
866 		}
867 
868 		goto retry;
869 	}
870 
871 	/*
872 	 * Fix the last_lr_irq refcount which was obtained while
873 	 * populating the LRs. This can also result in the LPI being
874 	 * deleted.
875 	 */
876 	irq = *host_data_ptr(last_lr_irq);
877 	if (irq) {
878 		deleted_lpis |= vgic_put_irq_norelease(vcpu->kvm, irq);
879 		*host_data_ptr(last_lr_irq) = NULL;
880 	}
881 
882 	raw_spin_unlock(&vgic_cpu->ap_list_lock);
883 
884 	if (unlikely(deleted_lpis))
885 		vgic_release_deleted_lpis(vcpu->kvm);
886 }
887 
888 static void vgic_fold_state(struct kvm_vcpu *vcpu)
889 {
890 	if (vgic_is_v5(vcpu->kvm)) {
891 		vgic_v5_fold_ppi_state(vcpu);
892 		return;
893 	}
894 
895 	if (kvm_vgic_global_state.type == VGIC_V2)
896 		vgic_v2_fold_lr_state(vcpu);
897 	else
898 		vgic_v3_fold_lr_state(vcpu);
899 }
900 
901 /* Requires the irq_lock to be held. */
902 static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
903 				    struct vgic_irq *irq, int lr)
904 {
905 	lockdep_assert_held(&irq->irq_lock);
906 
907 	if (kvm_vgic_global_state.type == VGIC_V2)
908 		vgic_v2_populate_lr(vcpu, irq, lr);
909 	else
910 		vgic_v3_populate_lr(vcpu, irq, lr);
911 }
912 
913 static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
914 {
915 	if (kvm_vgic_global_state.type == VGIC_V2)
916 		vgic_v2_clear_lr(vcpu, lr);
917 	else
918 		vgic_v3_clear_lr(vcpu, lr);
919 }
920 
921 static void summarize_ap_list(struct kvm_vcpu *vcpu,
922 			      struct ap_list_summary *als)
923 {
924 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
925 	struct vgic_irq *irq;
926 
927 	lockdep_assert_held(&vgic_cpu->ap_list_lock);
928 
929 	*als = (typeof(*als)){};
930 
931 	list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
932 		guard(raw_spinlock)(&irq->irq_lock);
933 
934 		if (unlikely(vgic_target_oracle(irq) != vcpu))
935 			continue;
936 
937 		if (!irq->active)
938 			als->nr_pend++;
939 		else
940 			als->nr_act++;
941 
942 		if (irq->intid < VGIC_NR_SGIS)
943 			als->nr_sgi++;
944 	}
945 }
946 
947 /*
948  * Dealing with LR overflow is close to black magic -- dress accordingly.
949  *
950  * We have to present an almost infinite number of interrupts through a very
951  * limited number of registers. Therefore crucial decisions must be made to
952  * ensure we feed the most relevant interrupts into the LRs, and yet have
953  * some facilities to let the guest interact with those that are not there.
954  *
955  * All considerations below are in the context of interrupts targeting a
956  * single vcpu with non-idle state (either pending, active, or both),
957  * colloquially called the ap_list:
958  *
959  * - Pending interrupts must have priority over active interrupts. This also
960  *   excludes pending+active interrupts. This ensures that a guest can
961  *   perform priority drops on any number of interrupts, and yet be
962  *   presented the next pending one.
963  *
964  * - Deactivation of interrupts outside of the LRs must be tracked by using
965  *   either the EOIcount-driven maintenance interrupt, and sometimes by
966  *   trapping the DIR register.
967  *
968  * - For EOImode=0, a non-zero EOIcount means walking the ap_list past the
969  *   point that made it into the LRs, and deactivate interrupts that would
970  *   have made it onto the LRs if we had the space.
971  *
972  * - The MI-generation bits must be used to try and force an exit when the
973  *   guest has done enough changes to the LRs that we want to reevaluate the
974  *   situation:
975  *
976  *	- if the total number of pending interrupts exceeds the number of
977  *	  LR, NPIE must be set in order to exit once no pending interrupts
978  *	  are present in the LRs, allowing us to populate the next batch.
979  *
980  *	- if there are active interrupts outside of the LRs, then LRENPIE
981  *	  must be set so that we exit on deactivation of one of these, and
982  *	  work out which one is to be deactivated.  Note that this is not
983  *	  enough to deal with EOImode=1, see below.
984  *
985  *	- if the overall number of interrupts exceeds the number of LRs,
986  *	  then UIE must be set to allow refilling of the LRs once the
987  *	  majority of them has been processed.
988  *
989  *	- as usual, MI triggers are only an optimisation, since we cannot
990  *        rely on the MI being delivered in timely manner...
991  *
992  * - EOImode=1 creates some additional problems:
993  *
994  *      - deactivation can happen in any order, and we cannot rely on
995  *	  EOImode=0's coupling of priority-drop and deactivation which
996  *	  imposes strict reverse Ack order. This means that DIR must
997  *	  trap if we have active interrupts outside of the LRs.
998  *
999  *      - deactivation of SPIs can occur on any CPU, while the SPI is only
1000  *	  present in the ap_list of the CPU that actually ack-ed it. In that
1001  *	  case, EOIcount doesn't provide enough information, and we must
1002  *	  resort to trapping DIR even if we don't overflow the LRs. Bonus
1003  *	  point for not trapping DIR when no SPIs are pending or active in
1004  *	  the whole VM.
1005  *
1006  *	- LPIs do not suffer the same problem as SPIs on deactivation, as we
1007  *	  have to essentially discard the active state, see below.
1008  *
1009  * - Virtual LPIs have an active state (surprise!), which gets removed on
1010  *   priority drop (EOI). However, EOIcount doesn't get bumped when the LPI
1011  *   is not present in the LR (surprise again!). Special care must therefore
1012  *   be taken to remove the active state from any activated LPI when exiting
1013  *   from the guest. This is in a way no different from what happens on the
1014  *   physical side. We still rely on the running priority to have been
1015  *   removed from the APRs, irrespective of the LPI being present in the LRs
1016  *   or not.
1017  *
1018  * - Virtual SGIs directly injected via GICv4.1 must not affect EOIcount, as
1019  *   they are not managed in SW and don't have a true active state. So only
1020  *   set vSGIEOICount when no SGIs are in the ap_list.
1021  *
1022  * - GICv2 SGIs with multiple sources are injected one source at a time, as
1023  *   if they were made pending sequentially. This may mean that we don't
1024  *   always present the HPPI if other interrupts with lower priority are
1025  *   pending in the LRs. Big deal.
1026  */
1027 static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
1028 {
1029 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1030 	struct ap_list_summary als;
1031 	struct vgic_irq *irq;
1032 	int count = 0;
1033 
1034 	lockdep_assert_held(&vgic_cpu->ap_list_lock);
1035 
1036 	summarize_ap_list(vcpu, &als);
1037 
1038 	if (irqs_outside_lrs(&als))
1039 		vgic_sort_ap_list(vcpu);
1040 
1041 	*host_data_ptr(last_lr_irq) = NULL;
1042 
1043 	list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
1044 		scoped_guard(raw_spinlock,  &irq->irq_lock) {
1045 			if (likely(vgic_target_oracle(irq) == vcpu)) {
1046 				vgic_populate_lr(vcpu, irq, count++);
1047 				if (count == kvm_vgic_global_state.nr_lr) {
1048 					vgic_get_irq_ref(irq);
1049 					*host_data_ptr(last_lr_irq) = irq;
1050 				}
1051 			}
1052 		}
1053 
1054 		if (*host_data_ptr(last_lr_irq))
1055 			break;
1056 	}
1057 
1058 	/* Nuke remaining LRs */
1059 	for (int i = count ; i < kvm_vgic_global_state.nr_lr; i++)
1060 		vgic_clear_lr(vcpu, i);
1061 
1062 	if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
1063 		vcpu->arch.vgic_cpu.vgic_v2.used_lrs = count;
1064 		vgic_v2_configure_hcr(vcpu, &als);
1065 	} else {
1066 		vcpu->arch.vgic_cpu.vgic_v3.used_lrs = count;
1067 		vgic_v3_configure_hcr(vcpu, &als);
1068 	}
1069 }
1070 
1071 static inline bool can_access_vgic_from_kernel(void)
1072 {
1073 	/*
1074 	 * GICv2 can always be accessed from the kernel because it is
1075 	 * memory-mapped, and VHE systems can access GICv3 EL2 system
1076 	 * registers.
1077 	 */
1078 	return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
1079 }
1080 
1081 static inline void vgic_save_state(struct kvm_vcpu *vcpu)
1082 {
1083 	/* No switch statement here. See comment in vgic_restore_state() */
1084 	if (vgic_is_v5(vcpu->kvm))
1085 		vgic_v5_save_state(vcpu);
1086 	else if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
1087 		vgic_v2_save_state(vcpu);
1088 	else
1089 		__vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
1090 }
1091 
1092 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
1093 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1094 {
1095 	if (vgic_is_v3(vcpu->kvm)) {
1096 		/* If nesting, emulate the HW effect from L0 to L1 */
1097 		if (vgic_state_is_nested(vcpu)) {
1098 			vgic_v3_sync_nested(vcpu);
1099 			return;
1100 		}
1101 
1102 		if (vcpu_has_nv(vcpu))
1103 			vgic_v3_nested_update_mi(vcpu);
1104 	}
1105 
1106 	if (can_access_vgic_from_kernel())
1107 		vgic_save_state(vcpu);
1108 
1109 	vgic_fold_state(vcpu);
1110 
1111 	if (!vgic_is_v5(vcpu->kvm))
1112 		vgic_prune_ap_list(vcpu);
1113 }
1114 
1115 /* Sync interrupts that were deactivated through a DIR trap */
1116 void kvm_vgic_process_async_update(struct kvm_vcpu *vcpu)
1117 {
1118 	unsigned long flags;
1119 
1120 	/* Make sure we're in the same context as LR handling */
1121 	local_irq_save(flags);
1122 	vgic_prune_ap_list(vcpu);
1123 	local_irq_restore(flags);
1124 }
1125 
1126 static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
1127 {
1128 	/*
1129 	 * As nice as it would be to restructure this code into a switch
1130 	 * statement as can be found elsewhere, the logic quickly gets ugly.
1131 	 *
1132 	 * __vgic_v3_restore_state() is doing a lot of heavy lifting here. It is
1133 	 * required for GICv3-on-GICv3, GICv2-on-GICv3, GICv3-on-GICv5, and the
1134 	 * no-in-kernel-irqchip case on GICv3 hardware. Hence, adding a switch
1135 	 * here results in much more complex code.
1136 	 */
1137 	if (vgic_is_v5(vcpu->kvm))
1138 		vgic_v5_restore_state(vcpu);
1139 	else if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
1140 		vgic_v2_restore_state(vcpu);
1141 	else
1142 		__vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
1143 }
1144 
1145 static void vgic_flush_state(struct kvm_vcpu *vcpu)
1146 {
1147 	if (vgic_is_v5(vcpu->kvm)) {
1148 		vgic_v5_flush_ppi_state(vcpu);
1149 		return;
1150 	}
1151 
1152 	scoped_guard(raw_spinlock, &vcpu->arch.vgic_cpu.ap_list_lock)
1153 		vgic_flush_lr_state(vcpu);
1154 }
1155 
1156 /* Flush our emulation state into the GIC hardware before entering the guest. */
1157 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1158 {
1159 	/*
1160 	 * If in a nested state, we must return early. Two possibilities:
1161 	 *
1162 	 * - If we have any pending IRQ for the guest and the guest
1163 	 *   expects IRQs to be handled in its virtual EL2 mode (the
1164 	 *   virtual IMO bit is set) and it is not already running in
1165 	 *   virtual EL2 mode, then we have to emulate an IRQ
1166 	 *   exception to virtual EL2.
1167 	 *
1168 	 *   We do that by placing a request to ourselves which will
1169 	 *   abort the entry procedure and inject the exception at the
1170 	 *   beginning of the run loop.
1171 	 *
1172 	 * - Otherwise, do exactly *NOTHING* apart from enabling the virtual
1173 	 *   CPU interface. The guest state is already loaded, and we can
1174 	 *   carry on with running it.
1175 	 *
1176 	 * If we have NV, but are not in a nested state, compute the
1177 	 * maintenance interrupt state, as it may fire.
1178 	 */
1179 	if (vgic_state_is_nested(vcpu)) {
1180 		if (kvm_vgic_vcpu_pending_irq(vcpu))
1181 			kvm_make_request(KVM_REQ_GUEST_HYP_IRQ_PENDING, vcpu);
1182 
1183 		vgic_v3_flush_nested(vcpu);
1184 		return;
1185 	}
1186 
1187 	if (vcpu_has_nv(vcpu))
1188 		vgic_v3_nested_update_mi(vcpu);
1189 
1190 	DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
1191 
1192 	vgic_flush_state(vcpu);
1193 
1194 	if (can_access_vgic_from_kernel())
1195 		vgic_restore_state(vcpu);
1196 
1197 	if (vgic_supports_direct_irqs(vcpu->kvm) && kvm_vgic_global_state.has_gicv4)
1198 		vgic_v4_commit(vcpu);
1199 }
1200 
1201 void kvm_vgic_load(struct kvm_vcpu *vcpu)
1202 {
1203 	const struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1204 
1205 	if (unlikely(!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm))) {
1206 		if (has_vhe() && static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
1207 			__vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
1208 		return;
1209 	}
1210 
1211 	switch (dist->vgic_model) {
1212 	case KVM_DEV_TYPE_ARM_VGIC_V5:
1213 		vgic_v5_load(vcpu);
1214 		break;
1215 	case KVM_DEV_TYPE_ARM_VGIC_V3:
1216 		vgic_v3_load(vcpu);
1217 		break;
1218 	case KVM_DEV_TYPE_ARM_VGIC_V2:
1219 		if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
1220 			vgic_v3_load(vcpu);
1221 		else
1222 			vgic_v2_load(vcpu);
1223 		break;
1224 	default:
1225 		BUG();
1226 	}
1227 }
1228 
1229 void kvm_vgic_put(struct kvm_vcpu *vcpu)
1230 {
1231 	const struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1232 
1233 	if (unlikely(!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm))) {
1234 		if (has_vhe() && static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
1235 			__vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
1236 		return;
1237 	}
1238 
1239 	switch (dist->vgic_model) {
1240 	case KVM_DEV_TYPE_ARM_VGIC_V5:
1241 		vgic_v5_put(vcpu);
1242 		break;
1243 	case KVM_DEV_TYPE_ARM_VGIC_V3:
1244 		vgic_v3_put(vcpu);
1245 		break;
1246 	case KVM_DEV_TYPE_ARM_VGIC_V2:
1247 		if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
1248 			vgic_v3_put(vcpu);
1249 		else
1250 			vgic_v2_put(vcpu);
1251 		break;
1252 	default:
1253 		BUG();
1254 	}
1255 }
1256 
1257 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1258 {
1259 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1260 	struct vgic_irq *irq;
1261 	bool pending = false;
1262 	unsigned long flags;
1263 	struct vgic_vmcr vmcr;
1264 
1265 	if (vgic_is_v5(vcpu->kvm)) {
1266 		if (READ_ONCE(vcpu->arch.vgic_cpu.vgic_v5.gicv5_vpe.db_fired))
1267 			return true;
1268 
1269 		return vgic_v5_has_pending_ppi(vcpu);
1270 	}
1271 
1272 	if (!vcpu->kvm->arch.vgic.enabled)
1273 		return false;
1274 
1275 	if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
1276 		return true;
1277 
1278 	vgic_get_vmcr(vcpu, &vmcr);
1279 
1280 	raw_spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
1281 
1282 	list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
1283 		raw_spin_lock(&irq->irq_lock);
1284 		pending = irq_is_pending(irq) && irq->enabled &&
1285 			  !irq->active &&
1286 			  irq->priority < vmcr.pmr;
1287 		raw_spin_unlock(&irq->irq_lock);
1288 
1289 		if (pending)
1290 			break;
1291 	}
1292 
1293 	raw_spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
1294 
1295 	return pending;
1296 }
1297 
1298 void vgic_kick_vcpus(struct kvm *kvm)
1299 {
1300 	struct kvm_vcpu *vcpu;
1301 	unsigned long c;
1302 
1303 	/*
1304 	 * We've injected an interrupt, time to find out who deserves
1305 	 * a good kick...
1306 	 */
1307 	kvm_for_each_vcpu(c, vcpu, kvm) {
1308 		if (kvm_vgic_vcpu_pending_irq(vcpu)) {
1309 			kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
1310 			kvm_vcpu_kick(vcpu);
1311 		}
1312 	}
1313 }
1314 
1315 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
1316 {
1317 	struct vgic_irq *irq;
1318 	bool map_is_active;
1319 	unsigned long flags;
1320 
1321 	if (!vgic_initialized(vcpu->kvm))
1322 		return false;
1323 
1324 	irq = vgic_get_vcpu_irq(vcpu, vintid);
1325 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
1326 	map_is_active = irq->hw && irq->active;
1327 	raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
1328 	vgic_put_irq(vcpu->kvm, irq);
1329 
1330 	return map_is_active;
1331 }
1332 
1333 /*
1334  * Level-triggered mapped IRQs are special because we only observe rising
1335  * edges as input to the VGIC.
1336  *
1337  * If the guest never acked the interrupt we have to sample the physical
1338  * line and set the line level, because the device state could have changed
1339  * or we simply need to process the still pending interrupt later.
1340  *
1341  * We could also have entered the guest with the interrupt active+pending.
1342  * On the next exit, we need to re-evaluate the pending state, as it could
1343  * otherwise result in a spurious interrupt by injecting a now potentially
1344  * stale pending state.
1345  *
1346  * If this causes us to lower the level, we have to also clear the physical
1347  * active state, since we will otherwise never be told when the interrupt
1348  * becomes asserted again.
1349  *
1350  * Another case is when the interrupt requires a helping hand on
1351  * deactivation (no HW deactivation, for example).
1352  */
1353 void vgic_irq_handle_resampling(struct vgic_irq *irq,
1354 				bool lr_deactivated, bool lr_pending)
1355 {
1356 	if (vgic_irq_is_mapped_level(irq)) {
1357 		bool resample = false;
1358 
1359 		if (unlikely(vgic_irq_needs_resampling(irq))) {
1360 			resample = !(irq->active || irq->pending_latch);
1361 		} else if (lr_pending || (lr_deactivated && irq->line_level)) {
1362 			irq->line_level = vgic_get_phys_line_level(irq);
1363 			resample = !irq->line_level;
1364 		}
1365 
1366 		if (resample)
1367 			vgic_irq_set_phys_active(irq, false);
1368 	}
1369 }
1370