2 * Copyright (C) 2015, 2016 ARM Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/kvm.h>
18 #include <linux/kvm_host.h>
19 #include <linux/list_sort.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
25 #define CREATE_TRACE_POINTS
28 #ifdef CONFIG_DEBUG_SPINLOCK
29 #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
31 #define DEBUG_SPINLOCK_BUG_ON(p)
34 struct vgic_global kvm_vgic_global_state __ro_after_init
= {
35 .gicv3_cpuif
= STATIC_KEY_FALSE_INIT
,
39 * Locking order is always:
41 * its->cmd_lock (mutex)
42 * its->its_lock (mutex)
43 * vgic_cpu->ap_list_lock
47 * If you need to take multiple locks, always take the upper lock first,
48 * then the lower ones, e.g. first take the its_lock, then the irq_lock.
49 * If you are already holding a lock and need to take a higher one, you
50 * have to drop the lower ranking lock first and re-aquire it after having
51 * taken the upper one.
53 * When taking more than one ap_list_lock at the same time, always take the
54 * lowest numbered VCPU's ap_list_lock first, so:
55 * vcpuX->vcpu_id < vcpuY->vcpu_id:
56 * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
57 * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
59 * Since the VGIC must support injecting virtual interrupts from ISRs, we have
60 * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
61 * spinlocks for any lock that may be taken while injecting an interrupt.
65 * Iterate over the VM's list of mapped LPIs to find the one with a
66 * matching interrupt ID and return a reference to the IRQ structure.
68 static struct vgic_irq
*vgic_get_lpi(struct kvm
*kvm
, u32 intid
)
70 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
71 struct vgic_irq
*irq
= NULL
;
73 spin_lock(&dist
->lpi_list_lock
);
75 list_for_each_entry(irq
, &dist
->lpi_list_head
, lpi_list
) {
76 if (irq
->intid
!= intid
)
80 * This increases the refcount, the caller is expected to
81 * call vgic_put_irq() later once it's finished with the IRQ.
83 vgic_get_irq_kref(irq
);
89 spin_unlock(&dist
->lpi_list_lock
);
95 * This looks up the virtual interrupt ID to get the corresponding
96 * struct vgic_irq. It also increases the refcount, so any caller is expected
97 * to call vgic_put_irq() once it's finished with this IRQ.
99 struct vgic_irq
*vgic_get_irq(struct kvm
*kvm
, struct kvm_vcpu
*vcpu
,
103 if (intid
<= VGIC_MAX_PRIVATE
)
104 return &vcpu
->arch
.vgic_cpu
.private_irqs
[intid
];
107 if (intid
<= VGIC_MAX_SPI
)
108 return &kvm
->arch
.vgic
.spis
[intid
- VGIC_NR_PRIVATE_IRQS
];
111 if (intid
>= VGIC_MIN_LPI
)
112 return vgic_get_lpi(kvm
, intid
);
114 WARN(1, "Looking up struct vgic_irq for reserved INTID");
119 * We can't do anything in here, because we lack the kvm pointer to
120 * lock and remove the item from the lpi_list. So we keep this function
121 * empty and use the return value of kref_put() to trigger the freeing.
123 static void vgic_irq_release(struct kref
*ref
)
127 void vgic_put_irq(struct kvm
*kvm
, struct vgic_irq
*irq
)
129 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
131 if (irq
->intid
< VGIC_MIN_LPI
)
134 spin_lock(&dist
->lpi_list_lock
);
135 if (!kref_put(&irq
->refcount
, vgic_irq_release
)) {
136 spin_unlock(&dist
->lpi_list_lock
);
140 list_del(&irq
->lpi_list
);
141 dist
->lpi_list_count
--;
142 spin_unlock(&dist
->lpi_list_lock
);
147 void vgic_irq_set_phys_pending(struct vgic_irq
*irq
, bool pending
)
149 WARN_ON(irq_set_irqchip_state(irq
->host_irq
,
150 IRQCHIP_STATE_PENDING
,
154 bool vgic_get_phys_line_level(struct vgic_irq
*irq
)
160 if (irq
->get_input_level
)
161 return irq
->get_input_level(irq
->intid
);
163 WARN_ON(irq_get_irqchip_state(irq
->host_irq
,
164 IRQCHIP_STATE_PENDING
,
169 /* Set/Clear the physical active state */
170 void vgic_irq_set_phys_active(struct vgic_irq
*irq
, bool active
)
174 WARN_ON(irq_set_irqchip_state(irq
->host_irq
,
175 IRQCHIP_STATE_ACTIVE
,
180 * kvm_vgic_target_oracle - compute the target vcpu for an irq
182 * @irq: The irq to route. Must be already locked.
184 * Based on the current state of the interrupt (enabled, pending,
185 * active, vcpu and target_vcpu), compute the next vcpu this should be
186 * given to. Return NULL if this shouldn't be injected at all.
188 * Requires the IRQ lock to be held.
190 static struct kvm_vcpu
*vgic_target_oracle(struct vgic_irq
*irq
)
192 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq
->irq_lock
));
194 /* If the interrupt is active, it must stay on the current vcpu */
196 return irq
->vcpu
? : irq
->target_vcpu
;
199 * If the IRQ is not active but enabled and pending, we should direct
200 * it to its configured target VCPU.
201 * If the distributor is disabled, pending interrupts shouldn't be
204 if (irq
->enabled
&& irq_is_pending(irq
)) {
205 if (unlikely(irq
->target_vcpu
&&
206 !irq
->target_vcpu
->kvm
->arch
.vgic
.enabled
))
209 return irq
->target_vcpu
;
212 /* If neither active nor pending and enabled, then this IRQ should not
213 * be queued to any VCPU.
219 * The order of items in the ap_lists defines how we'll pack things in LRs as
220 * well, the first items in the list being the first things populated in the
223 * A hard rule is that active interrupts can never be pushed out of the LRs
224 * (and therefore take priority) since we cannot reliably trap on deactivation
225 * of IRQs and therefore they have to be present in the LRs.
227 * Otherwise things should be sorted by the priority field and the GIC
228 * hardware support will take care of preemption of priority groups etc.
230 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
231 * to sort "b" before "a".
233 static int vgic_irq_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
235 struct vgic_irq
*irqa
= container_of(a
, struct vgic_irq
, ap_list
);
236 struct vgic_irq
*irqb
= container_of(b
, struct vgic_irq
, ap_list
);
240 spin_lock(&irqa
->irq_lock
);
241 spin_lock_nested(&irqb
->irq_lock
, SINGLE_DEPTH_NESTING
);
243 if (irqa
->active
|| irqb
->active
) {
244 ret
= (int)irqb
->active
- (int)irqa
->active
;
248 penda
= irqa
->enabled
&& irq_is_pending(irqa
);
249 pendb
= irqb
->enabled
&& irq_is_pending(irqb
);
251 if (!penda
|| !pendb
) {
252 ret
= (int)pendb
- (int)penda
;
256 /* Both pending and enabled, sort by priority */
257 ret
= irqa
->priority
- irqb
->priority
;
259 spin_unlock(&irqb
->irq_lock
);
260 spin_unlock(&irqa
->irq_lock
);
264 /* Must be called with the ap_list_lock held */
265 static void vgic_sort_ap_list(struct kvm_vcpu
*vcpu
)
267 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
269 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu
->ap_list_lock
));
271 list_sort(NULL
, &vgic_cpu
->ap_list_head
, vgic_irq_cmp
);
275 * Only valid injection if changing level for level-triggered IRQs or for a
276 * rising edge, and in-kernel connected IRQ lines can only be controlled by
279 static bool vgic_validate_injection(struct vgic_irq
*irq
, bool level
, void *owner
)
281 if (irq
->owner
!= owner
)
284 switch (irq
->config
) {
285 case VGIC_CONFIG_LEVEL
:
286 return irq
->line_level
!= level
;
287 case VGIC_CONFIG_EDGE
:
295 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
296 * Do the queuing if necessary, taking the right locks in the right order.
297 * Returns true when the IRQ was queued, false otherwise.
299 * Needs to be entered with the IRQ lock already held, but will return
300 * with all locks dropped.
302 bool vgic_queue_irq_unlock(struct kvm
*kvm
, struct vgic_irq
*irq
,
305 struct kvm_vcpu
*vcpu
;
307 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq
->irq_lock
));
310 vcpu
= vgic_target_oracle(irq
);
311 if (irq
->vcpu
|| !vcpu
) {
313 * If this IRQ is already on a VCPU's ap_list, then it
314 * cannot be moved or modified and there is no more work for
317 * Otherwise, if the irq is not pending and enabled, it does
318 * not need to be inserted into an ap_list and there is also
319 * no more work for us to do.
321 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
324 * We have to kick the VCPU here, because we could be
325 * queueing an edge-triggered interrupt for which we
326 * get no EOI maintenance interrupt. In that case,
327 * while the IRQ is already on the VCPU's AP list, the
328 * VCPU could have EOI'ed the original interrupt and
329 * won't see this one until it exits for some other
333 kvm_make_request(KVM_REQ_IRQ_PENDING
, vcpu
);
340 * We must unlock the irq lock to take the ap_list_lock where
341 * we are going to insert this new pending interrupt.
343 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
345 /* someone can do stuff here, which we re-check below */
347 spin_lock_irqsave(&vcpu
->arch
.vgic_cpu
.ap_list_lock
, flags
);
348 spin_lock(&irq
->irq_lock
);
351 * Did something change behind our backs?
353 * There are two cases:
354 * 1) The irq lost its pending state or was disabled behind our
355 * backs and/or it was queued to another VCPU's ap_list.
356 * 2) Someone changed the affinity on this irq behind our
357 * backs and we are now holding the wrong ap_list_lock.
359 * In both cases, drop the locks and retry.
362 if (unlikely(irq
->vcpu
|| vcpu
!= vgic_target_oracle(irq
))) {
363 spin_unlock(&irq
->irq_lock
);
364 spin_unlock_irqrestore(&vcpu
->arch
.vgic_cpu
.ap_list_lock
, flags
);
366 spin_lock_irqsave(&irq
->irq_lock
, flags
);
371 * Grab a reference to the irq to reflect the fact that it is
372 * now in the ap_list.
374 vgic_get_irq_kref(irq
);
375 list_add_tail(&irq
->ap_list
, &vcpu
->arch
.vgic_cpu
.ap_list_head
);
378 spin_unlock(&irq
->irq_lock
);
379 spin_unlock_irqrestore(&vcpu
->arch
.vgic_cpu
.ap_list_lock
, flags
);
381 kvm_make_request(KVM_REQ_IRQ_PENDING
, vcpu
);
388 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
389 * @kvm: The VM structure pointer
390 * @cpuid: The CPU for PPIs
391 * @intid: The INTID to inject a new state to.
392 * @level: Edge-triggered: true: to trigger the interrupt
393 * false: to ignore the call
394 * Level-sensitive true: raise the input signal
395 * false: lower the input signal
396 * @owner: The opaque pointer to the owner of the IRQ being raised to verify
397 * that the caller is allowed to inject this IRQ. Userspace
398 * injections will have owner == NULL.
400 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
401 * level-sensitive interrupts. You can think of the level parameter as 1
402 * being HIGH and 0 being LOW and all devices being active-HIGH.
404 int kvm_vgic_inject_irq(struct kvm
*kvm
, int cpuid
, unsigned int intid
,
405 bool level
, void *owner
)
407 struct kvm_vcpu
*vcpu
;
408 struct vgic_irq
*irq
;
412 trace_vgic_update_irq_pending(cpuid
, intid
, level
);
414 ret
= vgic_lazy_init(kvm
);
418 vcpu
= kvm_get_vcpu(kvm
, cpuid
);
419 if (!vcpu
&& intid
< VGIC_NR_PRIVATE_IRQS
)
422 irq
= vgic_get_irq(kvm
, vcpu
, intid
);
426 spin_lock_irqsave(&irq
->irq_lock
, flags
);
428 if (!vgic_validate_injection(irq
, level
, owner
)) {
429 /* Nothing to see here, move along... */
430 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
431 vgic_put_irq(kvm
, irq
);
435 if (irq
->config
== VGIC_CONFIG_LEVEL
)
436 irq
->line_level
= level
;
438 irq
->pending_latch
= true;
440 vgic_queue_irq_unlock(kvm
, irq
, flags
);
441 vgic_put_irq(kvm
, irq
);
446 /* @irq->irq_lock must be held */
447 static int kvm_vgic_map_irq(struct kvm_vcpu
*vcpu
, struct vgic_irq
*irq
,
448 unsigned int host_irq
,
449 bool (*get_input_level
)(int vindid
))
451 struct irq_desc
*desc
;
452 struct irq_data
*data
;
455 * Find the physical IRQ number corresponding to @host_irq
457 desc
= irq_to_desc(host_irq
);
459 kvm_err("%s: no interrupt descriptor\n", __func__
);
462 data
= irq_desc_get_irq_data(desc
);
463 while (data
->parent_data
)
464 data
= data
->parent_data
;
467 irq
->host_irq
= host_irq
;
468 irq
->hwintid
= data
->hwirq
;
469 irq
->get_input_level
= get_input_level
;
473 /* @irq->irq_lock must be held */
474 static inline void kvm_vgic_unmap_irq(struct vgic_irq
*irq
)
478 irq
->get_input_level
= NULL
;
481 int kvm_vgic_map_phys_irq(struct kvm_vcpu
*vcpu
, unsigned int host_irq
,
482 u32 vintid
, bool (*get_input_level
)(int vindid
))
484 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
490 spin_lock_irqsave(&irq
->irq_lock
, flags
);
491 ret
= kvm_vgic_map_irq(vcpu
, irq
, host_irq
, get_input_level
);
492 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
493 vgic_put_irq(vcpu
->kvm
, irq
);
499 * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
500 * @vcpu: The VCPU pointer
501 * @vintid: The INTID of the interrupt
503 * Reset the active and pending states of a mapped interrupt. Kernel
504 * subsystems injecting mapped interrupts should reset their interrupt lines
505 * when we are doing a reset of the VM.
507 void kvm_vgic_reset_mapped_irq(struct kvm_vcpu
*vcpu
, u32 vintid
)
509 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
515 spin_lock_irqsave(&irq
->irq_lock
, flags
);
517 irq
->pending_latch
= false;
518 irq
->line_level
= false;
519 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
521 vgic_put_irq(vcpu
->kvm
, irq
);
524 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu
*vcpu
, unsigned int vintid
)
526 struct vgic_irq
*irq
;
529 if (!vgic_initialized(vcpu
->kvm
))
532 irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
535 spin_lock_irqsave(&irq
->irq_lock
, flags
);
536 kvm_vgic_unmap_irq(irq
);
537 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
538 vgic_put_irq(vcpu
->kvm
, irq
);
544 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
546 * @vcpu: Pointer to the VCPU (used for PPIs)
547 * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
548 * @owner: Opaque pointer to the owner
550 * Returns 0 if intid is not already used by another in-kernel device and the
551 * owner is set, otherwise returns an error code.
553 int kvm_vgic_set_owner(struct kvm_vcpu
*vcpu
, unsigned int intid
, void *owner
)
555 struct vgic_irq
*irq
;
559 if (!vgic_initialized(vcpu
->kvm
))
562 /* SGIs and LPIs cannot be wired up to any device */
563 if (!irq_is_ppi(intid
) && !vgic_valid_spi(vcpu
->kvm
, intid
))
566 irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, intid
);
567 spin_lock_irqsave(&irq
->irq_lock
, flags
);
568 if (irq
->owner
&& irq
->owner
!= owner
)
572 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
578 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
580 * @vcpu: The VCPU pointer
582 * Go over the list of "interesting" interrupts, and prune those that we
583 * won't have to consider in the near future.
585 static void vgic_prune_ap_list(struct kvm_vcpu
*vcpu
)
587 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
588 struct vgic_irq
*irq
, *tmp
;
592 spin_lock_irqsave(&vgic_cpu
->ap_list_lock
, flags
);
594 list_for_each_entry_safe(irq
, tmp
, &vgic_cpu
->ap_list_head
, ap_list
) {
595 struct kvm_vcpu
*target_vcpu
, *vcpuA
, *vcpuB
;
597 spin_lock(&irq
->irq_lock
);
599 BUG_ON(vcpu
!= irq
->vcpu
);
601 target_vcpu
= vgic_target_oracle(irq
);
605 * We don't need to process this interrupt any
606 * further, move it off the list.
608 list_del(&irq
->ap_list
);
610 spin_unlock(&irq
->irq_lock
);
613 * This vgic_put_irq call matches the
614 * vgic_get_irq_kref in vgic_queue_irq_unlock,
615 * where we added the LPI to the ap_list. As
616 * we remove the irq from the list, we drop
617 * also drop the refcount.
619 vgic_put_irq(vcpu
->kvm
, irq
);
623 if (target_vcpu
== vcpu
) {
624 /* We're on the right CPU */
625 spin_unlock(&irq
->irq_lock
);
629 /* This interrupt looks like it has to be migrated. */
631 spin_unlock(&irq
->irq_lock
);
632 spin_unlock_irqrestore(&vgic_cpu
->ap_list_lock
, flags
);
635 * Ensure locking order by always locking the smallest
638 if (vcpu
->vcpu_id
< target_vcpu
->vcpu_id
) {
646 spin_lock_irqsave(&vcpuA
->arch
.vgic_cpu
.ap_list_lock
, flags
);
647 spin_lock_nested(&vcpuB
->arch
.vgic_cpu
.ap_list_lock
,
648 SINGLE_DEPTH_NESTING
);
649 spin_lock(&irq
->irq_lock
);
652 * If the affinity has been preserved, move the
653 * interrupt around. Otherwise, it means things have
654 * changed while the interrupt was unlocked, and we
655 * need to replay this.
657 * In all cases, we cannot trust the list not to have
658 * changed, so we restart from the beginning.
660 if (target_vcpu
== vgic_target_oracle(irq
)) {
661 struct vgic_cpu
*new_cpu
= &target_vcpu
->arch
.vgic_cpu
;
663 list_del(&irq
->ap_list
);
664 irq
->vcpu
= target_vcpu
;
665 list_add_tail(&irq
->ap_list
, &new_cpu
->ap_list_head
);
668 spin_unlock(&irq
->irq_lock
);
669 spin_unlock(&vcpuB
->arch
.vgic_cpu
.ap_list_lock
);
670 spin_unlock_irqrestore(&vcpuA
->arch
.vgic_cpu
.ap_list_lock
, flags
);
674 spin_unlock_irqrestore(&vgic_cpu
->ap_list_lock
, flags
);
677 static inline void vgic_fold_lr_state(struct kvm_vcpu
*vcpu
)
679 if (kvm_vgic_global_state
.type
== VGIC_V2
)
680 vgic_v2_fold_lr_state(vcpu
);
682 vgic_v3_fold_lr_state(vcpu
);
685 /* Requires the irq_lock to be held. */
686 static inline void vgic_populate_lr(struct kvm_vcpu
*vcpu
,
687 struct vgic_irq
*irq
, int lr
)
689 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq
->irq_lock
));
691 if (kvm_vgic_global_state
.type
== VGIC_V2
)
692 vgic_v2_populate_lr(vcpu
, irq
, lr
);
694 vgic_v3_populate_lr(vcpu
, irq
, lr
);
697 static inline void vgic_clear_lr(struct kvm_vcpu
*vcpu
, int lr
)
699 if (kvm_vgic_global_state
.type
== VGIC_V2
)
700 vgic_v2_clear_lr(vcpu
, lr
);
702 vgic_v3_clear_lr(vcpu
, lr
);
705 static inline void vgic_set_underflow(struct kvm_vcpu
*vcpu
)
707 if (kvm_vgic_global_state
.type
== VGIC_V2
)
708 vgic_v2_set_underflow(vcpu
);
710 vgic_v3_set_underflow(vcpu
);
713 static inline void vgic_set_npie(struct kvm_vcpu
*vcpu
)
715 if (kvm_vgic_global_state
.type
== VGIC_V2
)
716 vgic_v2_set_npie(vcpu
);
718 vgic_v3_set_npie(vcpu
);
721 /* Requires the ap_list_lock to be held. */
722 static int compute_ap_list_depth(struct kvm_vcpu
*vcpu
,
725 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
726 struct vgic_irq
*irq
;
731 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu
->ap_list_lock
));
733 list_for_each_entry(irq
, &vgic_cpu
->ap_list_head
, ap_list
) {
734 spin_lock(&irq
->irq_lock
);
735 /* GICv2 SGIs can count for more than one... */
736 if (vgic_irq_is_sgi(irq
->intid
) && irq
->source
) {
737 int w
= hweight8(irq
->source
);
740 *multi_sgi
|= (w
> 1);
744 spin_unlock(&irq
->irq_lock
);
749 /* Requires the VCPU's ap_list_lock to be held. */
750 static void vgic_flush_lr_state(struct kvm_vcpu
*vcpu
)
752 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
753 struct vgic_irq
*irq
;
759 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu
->ap_list_lock
));
761 count
= compute_ap_list_depth(vcpu
, &multi_sgi
);
762 if (count
> kvm_vgic_global_state
.nr_lr
|| multi_sgi
)
763 vgic_sort_ap_list(vcpu
);
767 list_for_each_entry(irq
, &vgic_cpu
->ap_list_head
, ap_list
) {
768 spin_lock(&irq
->irq_lock
);
771 * If we have multi-SGIs in the pipeline, we need to
772 * guarantee that they are all seen before any IRQ of
773 * lower priority. In that case, we need to filter out
774 * these interrupts by exiting early. This is easy as
775 * the AP list has been sorted already.
777 if (multi_sgi
&& irq
->priority
> prio
) {
778 spin_unlock(&irq
->irq_lock
);
782 if (likely(vgic_target_oracle(irq
) == vcpu
)) {
783 vgic_populate_lr(vcpu
, irq
, count
++);
787 prio
= irq
->priority
;
791 spin_unlock(&irq
->irq_lock
);
793 if (count
== kvm_vgic_global_state
.nr_lr
) {
794 if (!list_is_last(&irq
->ap_list
,
795 &vgic_cpu
->ap_list_head
))
796 vgic_set_underflow(vcpu
);
804 vcpu
->arch
.vgic_cpu
.used_lrs
= count
;
806 /* Nuke remaining LRs */
807 for ( ; count
< kvm_vgic_global_state
.nr_lr
; count
++)
808 vgic_clear_lr(vcpu
, count
);
811 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
812 void kvm_vgic_sync_hwstate(struct kvm_vcpu
*vcpu
)
814 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
816 WARN_ON(vgic_v4_sync_hwstate(vcpu
));
818 /* An empty ap_list_head implies used_lrs == 0 */
819 if (list_empty(&vcpu
->arch
.vgic_cpu
.ap_list_head
))
822 if (vgic_cpu
->used_lrs
)
823 vgic_fold_lr_state(vcpu
);
824 vgic_prune_ap_list(vcpu
);
827 /* Flush our emulation state into the GIC hardware before entering the guest. */
828 void kvm_vgic_flush_hwstate(struct kvm_vcpu
*vcpu
)
830 WARN_ON(vgic_v4_flush_hwstate(vcpu
));
833 * If there are no virtual interrupts active or pending for this
834 * VCPU, then there is no work to do and we can bail out without
835 * taking any lock. There is a potential race with someone injecting
836 * interrupts to the VCPU, but it is a benign race as the VCPU will
837 * either observe the new interrupt before or after doing this check,
838 * and introducing additional synchronization mechanism doesn't change
841 if (list_empty(&vcpu
->arch
.vgic_cpu
.ap_list_head
))
844 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
846 spin_lock(&vcpu
->arch
.vgic_cpu
.ap_list_lock
);
847 vgic_flush_lr_state(vcpu
);
848 spin_unlock(&vcpu
->arch
.vgic_cpu
.ap_list_lock
);
851 void kvm_vgic_load(struct kvm_vcpu
*vcpu
)
853 if (unlikely(!vgic_initialized(vcpu
->kvm
)))
856 if (kvm_vgic_global_state
.type
== VGIC_V2
)
862 void kvm_vgic_put(struct kvm_vcpu
*vcpu
)
864 if (unlikely(!vgic_initialized(vcpu
->kvm
)))
867 if (kvm_vgic_global_state
.type
== VGIC_V2
)
873 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu
*vcpu
)
875 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
876 struct vgic_irq
*irq
;
877 bool pending
= false;
880 if (!vcpu
->kvm
->arch
.vgic
.enabled
)
883 if (vcpu
->arch
.vgic_cpu
.vgic_v3
.its_vpe
.pending_last
)
886 spin_lock_irqsave(&vgic_cpu
->ap_list_lock
, flags
);
888 list_for_each_entry(irq
, &vgic_cpu
->ap_list_head
, ap_list
) {
889 spin_lock(&irq
->irq_lock
);
890 pending
= irq_is_pending(irq
) && irq
->enabled
;
891 spin_unlock(&irq
->irq_lock
);
897 spin_unlock_irqrestore(&vgic_cpu
->ap_list_lock
, flags
);
902 void vgic_kick_vcpus(struct kvm
*kvm
)
904 struct kvm_vcpu
*vcpu
;
908 * We've injected an interrupt, time to find out who deserves
911 kvm_for_each_vcpu(c
, vcpu
, kvm
) {
912 if (kvm_vgic_vcpu_pending_irq(vcpu
)) {
913 kvm_make_request(KVM_REQ_IRQ_PENDING
, vcpu
);
919 bool kvm_vgic_map_is_active(struct kvm_vcpu
*vcpu
, unsigned int vintid
)
921 struct vgic_irq
*irq
;
925 if (!vgic_initialized(vcpu
->kvm
))
928 irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
929 spin_lock_irqsave(&irq
->irq_lock
, flags
);
930 map_is_active
= irq
->hw
&& irq
->active
;
931 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
932 vgic_put_irq(vcpu
->kvm
, irq
);
934 return map_is_active
;