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