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 must be taken with IRQs disabled
44 * kvm->lpi_list_lock must be taken with IRQs disabled
45 * vgic_irq->irq_lock must be taken with IRQs disabled
47 * As the ap_list_lock might be taken from the timer interrupt handler,
48 * we have to disable IRQs before taking this lock and everything lower
51 * If you need to take multiple locks, always take the upper lock first,
52 * then the lower ones, e.g. first take the its_lock, then the irq_lock.
53 * If you are already holding a lock and need to take a higher one, you
54 * have to drop the lower ranking lock first and re-aquire it after having
55 * taken the upper one.
57 * When taking more than one ap_list_lock at the same time, always take the
58 * lowest numbered VCPU's ap_list_lock first, so:
59 * vcpuX->vcpu_id < vcpuY->vcpu_id:
60 * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
61 * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
63 * Since the VGIC must support injecting virtual interrupts from ISRs, we have
64 * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
65 * spinlocks for any lock that may be taken while injecting an interrupt.
69 * Iterate over the VM's list of mapped LPIs to find the one with a
70 * matching interrupt ID and return a reference to the IRQ structure.
72 static struct vgic_irq
*vgic_get_lpi(struct kvm
*kvm
, u32 intid
)
74 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
75 struct vgic_irq
*irq
= NULL
;
78 spin_lock_irqsave(&dist
->lpi_list_lock
, flags
);
80 list_for_each_entry(irq
, &dist
->lpi_list_head
, lpi_list
) {
81 if (irq
->intid
!= intid
)
85 * This increases the refcount, the caller is expected to
86 * call vgic_put_irq() later once it's finished with the IRQ.
88 vgic_get_irq_kref(irq
);
94 spin_unlock_irqrestore(&dist
->lpi_list_lock
, flags
);
100 * This looks up the virtual interrupt ID to get the corresponding
101 * struct vgic_irq. It also increases the refcount, so any caller is expected
102 * to call vgic_put_irq() once it's finished with this IRQ.
104 struct vgic_irq
*vgic_get_irq(struct kvm
*kvm
, struct kvm_vcpu
*vcpu
,
108 if (intid
<= VGIC_MAX_PRIVATE
)
109 return &vcpu
->arch
.vgic_cpu
.private_irqs
[intid
];
112 if (intid
<= VGIC_MAX_SPI
)
113 return &kvm
->arch
.vgic
.spis
[intid
- VGIC_NR_PRIVATE_IRQS
];
116 if (intid
>= VGIC_MIN_LPI
)
117 return vgic_get_lpi(kvm
, intid
);
119 WARN(1, "Looking up struct vgic_irq for reserved INTID");
124 * We can't do anything in here, because we lack the kvm pointer to
125 * lock and remove the item from the lpi_list. So we keep this function
126 * empty and use the return value of kref_put() to trigger the freeing.
128 static void vgic_irq_release(struct kref
*ref
)
132 void vgic_put_irq(struct kvm
*kvm
, struct vgic_irq
*irq
)
134 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
137 if (irq
->intid
< VGIC_MIN_LPI
)
140 spin_lock_irqsave(&dist
->lpi_list_lock
, flags
);
141 if (!kref_put(&irq
->refcount
, vgic_irq_release
)) {
142 spin_unlock_irqrestore(&dist
->lpi_list_lock
, flags
);
146 list_del(&irq
->lpi_list
);
147 dist
->lpi_list_count
--;
148 spin_unlock_irqrestore(&dist
->lpi_list_lock
, flags
);
154 * kvm_vgic_target_oracle - compute the target vcpu for an irq
156 * @irq: The irq to route. Must be already locked.
158 * Based on the current state of the interrupt (enabled, pending,
159 * active, vcpu and target_vcpu), compute the next vcpu this should be
160 * given to. Return NULL if this shouldn't be injected at all.
162 * Requires the IRQ lock to be held.
164 static struct kvm_vcpu
*vgic_target_oracle(struct vgic_irq
*irq
)
166 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq
->irq_lock
));
168 /* If the interrupt is active, it must stay on the current vcpu */
170 return irq
->vcpu
? : irq
->target_vcpu
;
173 * If the IRQ is not active but enabled and pending, we should direct
174 * it to its configured target VCPU.
175 * If the distributor is disabled, pending interrupts shouldn't be
178 if (irq
->enabled
&& irq_is_pending(irq
)) {
179 if (unlikely(irq
->target_vcpu
&&
180 !irq
->target_vcpu
->kvm
->arch
.vgic
.enabled
))
183 return irq
->target_vcpu
;
186 /* If neither active nor pending and enabled, then this IRQ should not
187 * be queued to any VCPU.
193 * The order of items in the ap_lists defines how we'll pack things in LRs as
194 * well, the first items in the list being the first things populated in the
197 * A hard rule is that active interrupts can never be pushed out of the LRs
198 * (and therefore take priority) since we cannot reliably trap on deactivation
199 * of IRQs and therefore they have to be present in the LRs.
201 * Otherwise things should be sorted by the priority field and the GIC
202 * hardware support will take care of preemption of priority groups etc.
204 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
205 * to sort "b" before "a".
207 static int vgic_irq_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
209 struct vgic_irq
*irqa
= container_of(a
, struct vgic_irq
, ap_list
);
210 struct vgic_irq
*irqb
= container_of(b
, struct vgic_irq
, ap_list
);
214 spin_lock(&irqa
->irq_lock
);
215 spin_lock_nested(&irqb
->irq_lock
, SINGLE_DEPTH_NESTING
);
217 if (irqa
->active
|| irqb
->active
) {
218 ret
= (int)irqb
->active
- (int)irqa
->active
;
222 penda
= irqa
->enabled
&& irq_is_pending(irqa
);
223 pendb
= irqb
->enabled
&& irq_is_pending(irqb
);
225 if (!penda
|| !pendb
) {
226 ret
= (int)pendb
- (int)penda
;
230 /* Both pending and enabled, sort by priority */
231 ret
= irqa
->priority
- irqb
->priority
;
233 spin_unlock(&irqb
->irq_lock
);
234 spin_unlock(&irqa
->irq_lock
);
238 /* Must be called with the ap_list_lock held */
239 static void vgic_sort_ap_list(struct kvm_vcpu
*vcpu
)
241 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
243 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu
->ap_list_lock
));
245 list_sort(NULL
, &vgic_cpu
->ap_list_head
, vgic_irq_cmp
);
249 * Only valid injection if changing level for level-triggered IRQs or for a
250 * rising edge, and in-kernel connected IRQ lines can only be controlled by
253 static bool vgic_validate_injection(struct vgic_irq
*irq
, bool level
, void *owner
)
255 if (irq
->owner
!= owner
)
258 switch (irq
->config
) {
259 case VGIC_CONFIG_LEVEL
:
260 return irq
->line_level
!= level
;
261 case VGIC_CONFIG_EDGE
:
269 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
270 * Do the queuing if necessary, taking the right locks in the right order.
271 * Returns true when the IRQ was queued, false otherwise.
273 * Needs to be entered with the IRQ lock already held, but will return
274 * with all locks dropped.
276 bool vgic_queue_irq_unlock(struct kvm
*kvm
, struct vgic_irq
*irq
,
279 struct kvm_vcpu
*vcpu
;
281 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq
->irq_lock
));
284 vcpu
= vgic_target_oracle(irq
);
285 if (irq
->vcpu
|| !vcpu
) {
287 * If this IRQ is already on a VCPU's ap_list, then it
288 * cannot be moved or modified and there is no more work for
291 * Otherwise, if the irq is not pending and enabled, it does
292 * not need to be inserted into an ap_list and there is also
293 * no more work for us to do.
295 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
298 * We have to kick the VCPU here, because we could be
299 * queueing an edge-triggered interrupt for which we
300 * get no EOI maintenance interrupt. In that case,
301 * while the IRQ is already on the VCPU's AP list, the
302 * VCPU could have EOI'ed the original interrupt and
303 * won't see this one until it exits for some other
307 kvm_make_request(KVM_REQ_IRQ_PENDING
, vcpu
);
314 * We must unlock the irq lock to take the ap_list_lock where
315 * we are going to insert this new pending interrupt.
317 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
319 /* someone can do stuff here, which we re-check below */
321 spin_lock_irqsave(&vcpu
->arch
.vgic_cpu
.ap_list_lock
, flags
);
322 spin_lock(&irq
->irq_lock
);
325 * Did something change behind our backs?
327 * There are two cases:
328 * 1) The irq lost its pending state or was disabled behind our
329 * backs and/or it was queued to another VCPU's ap_list.
330 * 2) Someone changed the affinity on this irq behind our
331 * backs and we are now holding the wrong ap_list_lock.
333 * In both cases, drop the locks and retry.
336 if (unlikely(irq
->vcpu
|| vcpu
!= vgic_target_oracle(irq
))) {
337 spin_unlock(&irq
->irq_lock
);
338 spin_unlock_irqrestore(&vcpu
->arch
.vgic_cpu
.ap_list_lock
, flags
);
340 spin_lock_irqsave(&irq
->irq_lock
, flags
);
345 * Grab a reference to the irq to reflect the fact that it is
346 * now in the ap_list.
348 vgic_get_irq_kref(irq
);
349 list_add_tail(&irq
->ap_list
, &vcpu
->arch
.vgic_cpu
.ap_list_head
);
352 spin_unlock(&irq
->irq_lock
);
353 spin_unlock_irqrestore(&vcpu
->arch
.vgic_cpu
.ap_list_lock
, flags
);
355 kvm_make_request(KVM_REQ_IRQ_PENDING
, vcpu
);
362 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
363 * @kvm: The VM structure pointer
364 * @cpuid: The CPU for PPIs
365 * @intid: The INTID to inject a new state to.
366 * @level: Edge-triggered: true: to trigger the interrupt
367 * false: to ignore the call
368 * Level-sensitive true: raise the input signal
369 * false: lower the input signal
370 * @owner: The opaque pointer to the owner of the IRQ being raised to verify
371 * that the caller is allowed to inject this IRQ. Userspace
372 * injections will have owner == NULL.
374 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
375 * level-sensitive interrupts. You can think of the level parameter as 1
376 * being HIGH and 0 being LOW and all devices being active-HIGH.
378 int kvm_vgic_inject_irq(struct kvm
*kvm
, int cpuid
, unsigned int intid
,
379 bool level
, void *owner
)
381 struct kvm_vcpu
*vcpu
;
382 struct vgic_irq
*irq
;
386 trace_vgic_update_irq_pending(cpuid
, intid
, level
);
388 ret
= vgic_lazy_init(kvm
);
392 vcpu
= kvm_get_vcpu(kvm
, cpuid
);
393 if (!vcpu
&& intid
< VGIC_NR_PRIVATE_IRQS
)
396 irq
= vgic_get_irq(kvm
, vcpu
, intid
);
400 spin_lock_irqsave(&irq
->irq_lock
, flags
);
402 if (!vgic_validate_injection(irq
, level
, owner
)) {
403 /* Nothing to see here, move along... */
404 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
405 vgic_put_irq(kvm
, irq
);
409 if (irq
->config
== VGIC_CONFIG_LEVEL
)
410 irq
->line_level
= level
;
412 irq
->pending_latch
= true;
414 vgic_queue_irq_unlock(kvm
, irq
, flags
);
415 vgic_put_irq(kvm
, irq
);
420 /* @irq->irq_lock must be held */
421 static int kvm_vgic_map_irq(struct kvm_vcpu
*vcpu
, struct vgic_irq
*irq
,
422 unsigned int host_irq
)
424 struct irq_desc
*desc
;
425 struct irq_data
*data
;
428 * Find the physical IRQ number corresponding to @host_irq
430 desc
= irq_to_desc(host_irq
);
432 kvm_err("%s: no interrupt descriptor\n", __func__
);
435 data
= irq_desc_get_irq_data(desc
);
436 while (data
->parent_data
)
437 data
= data
->parent_data
;
440 irq
->host_irq
= host_irq
;
441 irq
->hwintid
= data
->hwirq
;
445 /* @irq->irq_lock must be held */
446 static inline void kvm_vgic_unmap_irq(struct vgic_irq
*irq
)
452 int kvm_vgic_map_phys_irq(struct kvm_vcpu
*vcpu
, unsigned int host_irq
,
455 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
461 spin_lock_irqsave(&irq
->irq_lock
, flags
);
462 ret
= kvm_vgic_map_irq(vcpu
, irq
, host_irq
);
463 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
464 vgic_put_irq(vcpu
->kvm
, irq
);
470 * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
471 * @vcpu: The VCPU pointer
472 * @vintid: The INTID of the interrupt
474 * Reset the active and pending states of a mapped interrupt. Kernel
475 * subsystems injecting mapped interrupts should reset their interrupt lines
476 * when we are doing a reset of the VM.
478 void kvm_vgic_reset_mapped_irq(struct kvm_vcpu
*vcpu
, u32 vintid
)
480 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
486 spin_lock_irqsave(&irq
->irq_lock
, flags
);
488 irq
->pending_latch
= false;
489 irq
->line_level
= false;
490 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
492 vgic_put_irq(vcpu
->kvm
, irq
);
495 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu
*vcpu
, unsigned int vintid
)
497 struct vgic_irq
*irq
;
500 if (!vgic_initialized(vcpu
->kvm
))
503 irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
506 spin_lock_irqsave(&irq
->irq_lock
, flags
);
507 kvm_vgic_unmap_irq(irq
);
508 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
509 vgic_put_irq(vcpu
->kvm
, irq
);
515 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
517 * @vcpu: Pointer to the VCPU (used for PPIs)
518 * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
519 * @owner: Opaque pointer to the owner
521 * Returns 0 if intid is not already used by another in-kernel device and the
522 * owner is set, otherwise returns an error code.
524 int kvm_vgic_set_owner(struct kvm_vcpu
*vcpu
, unsigned int intid
, void *owner
)
526 struct vgic_irq
*irq
;
530 if (!vgic_initialized(vcpu
->kvm
))
533 /* SGIs and LPIs cannot be wired up to any device */
534 if (!irq_is_ppi(intid
) && !vgic_valid_spi(vcpu
->kvm
, intid
))
537 irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, intid
);
538 spin_lock_irqsave(&irq
->irq_lock
, flags
);
539 if (irq
->owner
&& irq
->owner
!= owner
)
543 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
549 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
551 * @vcpu: The VCPU pointer
553 * Go over the list of "interesting" interrupts, and prune those that we
554 * won't have to consider in the near future.
556 static void vgic_prune_ap_list(struct kvm_vcpu
*vcpu
)
558 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
559 struct vgic_irq
*irq
, *tmp
;
563 spin_lock_irqsave(&vgic_cpu
->ap_list_lock
, flags
);
565 list_for_each_entry_safe(irq
, tmp
, &vgic_cpu
->ap_list_head
, ap_list
) {
566 struct kvm_vcpu
*target_vcpu
, *vcpuA
, *vcpuB
;
568 spin_lock(&irq
->irq_lock
);
570 BUG_ON(vcpu
!= irq
->vcpu
);
572 target_vcpu
= vgic_target_oracle(irq
);
576 * We don't need to process this interrupt any
577 * further, move it off the list.
579 list_del(&irq
->ap_list
);
581 spin_unlock(&irq
->irq_lock
);
584 * This vgic_put_irq call matches the
585 * vgic_get_irq_kref in vgic_queue_irq_unlock,
586 * where we added the LPI to the ap_list. As
587 * we remove the irq from the list, we drop
588 * also drop the refcount.
590 vgic_put_irq(vcpu
->kvm
, irq
);
594 if (target_vcpu
== vcpu
) {
595 /* We're on the right CPU */
596 spin_unlock(&irq
->irq_lock
);
600 /* This interrupt looks like it has to be migrated. */
602 spin_unlock(&irq
->irq_lock
);
603 spin_unlock_irqrestore(&vgic_cpu
->ap_list_lock
, flags
);
606 * Ensure locking order by always locking the smallest
609 if (vcpu
->vcpu_id
< target_vcpu
->vcpu_id
) {
617 spin_lock_irqsave(&vcpuA
->arch
.vgic_cpu
.ap_list_lock
, flags
);
618 spin_lock_nested(&vcpuB
->arch
.vgic_cpu
.ap_list_lock
,
619 SINGLE_DEPTH_NESTING
);
620 spin_lock(&irq
->irq_lock
);
623 * If the affinity has been preserved, move the
624 * interrupt around. Otherwise, it means things have
625 * changed while the interrupt was unlocked, and we
626 * need to replay this.
628 * In all cases, we cannot trust the list not to have
629 * changed, so we restart from the beginning.
631 if (target_vcpu
== vgic_target_oracle(irq
)) {
632 struct vgic_cpu
*new_cpu
= &target_vcpu
->arch
.vgic_cpu
;
634 list_del(&irq
->ap_list
);
635 irq
->vcpu
= target_vcpu
;
636 list_add_tail(&irq
->ap_list
, &new_cpu
->ap_list_head
);
639 spin_unlock(&irq
->irq_lock
);
640 spin_unlock(&vcpuB
->arch
.vgic_cpu
.ap_list_lock
);
641 spin_unlock_irqrestore(&vcpuA
->arch
.vgic_cpu
.ap_list_lock
, flags
);
645 spin_unlock_irqrestore(&vgic_cpu
->ap_list_lock
, flags
);
648 static inline void vgic_fold_lr_state(struct kvm_vcpu
*vcpu
)
650 if (kvm_vgic_global_state
.type
== VGIC_V2
)
651 vgic_v2_fold_lr_state(vcpu
);
653 vgic_v3_fold_lr_state(vcpu
);
656 /* Requires the irq_lock to be held. */
657 static inline void vgic_populate_lr(struct kvm_vcpu
*vcpu
,
658 struct vgic_irq
*irq
, int lr
)
660 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq
->irq_lock
));
662 if (kvm_vgic_global_state
.type
== VGIC_V2
)
663 vgic_v2_populate_lr(vcpu
, irq
, lr
);
665 vgic_v3_populate_lr(vcpu
, irq
, lr
);
668 static inline void vgic_clear_lr(struct kvm_vcpu
*vcpu
, int lr
)
670 if (kvm_vgic_global_state
.type
== VGIC_V2
)
671 vgic_v2_clear_lr(vcpu
, lr
);
673 vgic_v3_clear_lr(vcpu
, lr
);
676 static inline void vgic_set_underflow(struct kvm_vcpu
*vcpu
)
678 if (kvm_vgic_global_state
.type
== VGIC_V2
)
679 vgic_v2_set_underflow(vcpu
);
681 vgic_v3_set_underflow(vcpu
);
684 static inline void vgic_set_npie(struct kvm_vcpu
*vcpu
)
686 if (kvm_vgic_global_state
.type
== VGIC_V2
)
687 vgic_v2_set_npie(vcpu
);
689 vgic_v3_set_npie(vcpu
);
692 /* Requires the ap_list_lock to be held. */
693 static int compute_ap_list_depth(struct kvm_vcpu
*vcpu
,
696 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
697 struct vgic_irq
*irq
;
702 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu
->ap_list_lock
));
704 list_for_each_entry(irq
, &vgic_cpu
->ap_list_head
, ap_list
) {
705 spin_lock(&irq
->irq_lock
);
706 /* GICv2 SGIs can count for more than one... */
707 if (vgic_irq_is_sgi(irq
->intid
) && irq
->source
) {
708 int w
= hweight8(irq
->source
);
711 *multi_sgi
|= (w
> 1);
715 spin_unlock(&irq
->irq_lock
);
720 /* Requires the VCPU's ap_list_lock to be held. */
721 static void vgic_flush_lr_state(struct kvm_vcpu
*vcpu
)
723 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
724 struct vgic_irq
*irq
;
730 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu
->ap_list_lock
));
732 count
= compute_ap_list_depth(vcpu
, &multi_sgi
);
733 if (count
> kvm_vgic_global_state
.nr_lr
|| multi_sgi
)
734 vgic_sort_ap_list(vcpu
);
738 list_for_each_entry(irq
, &vgic_cpu
->ap_list_head
, ap_list
) {
739 spin_lock(&irq
->irq_lock
);
742 * If we have multi-SGIs in the pipeline, we need to
743 * guarantee that they are all seen before any IRQ of
744 * lower priority. In that case, we need to filter out
745 * these interrupts by exiting early. This is easy as
746 * the AP list has been sorted already.
748 if (multi_sgi
&& irq
->priority
> prio
) {
749 spin_unlock(&irq
->irq_lock
);
753 if (likely(vgic_target_oracle(irq
) == vcpu
)) {
754 vgic_populate_lr(vcpu
, irq
, count
++);
758 prio
= irq
->priority
;
762 spin_unlock(&irq
->irq_lock
);
764 if (count
== kvm_vgic_global_state
.nr_lr
) {
765 if (!list_is_last(&irq
->ap_list
,
766 &vgic_cpu
->ap_list_head
))
767 vgic_set_underflow(vcpu
);
775 vcpu
->arch
.vgic_cpu
.used_lrs
= count
;
777 /* Nuke remaining LRs */
778 for ( ; count
< kvm_vgic_global_state
.nr_lr
; count
++)
779 vgic_clear_lr(vcpu
, count
);
782 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
783 void kvm_vgic_sync_hwstate(struct kvm_vcpu
*vcpu
)
785 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
787 WARN_ON(vgic_v4_sync_hwstate(vcpu
));
789 /* An empty ap_list_head implies used_lrs == 0 */
790 if (list_empty(&vcpu
->arch
.vgic_cpu
.ap_list_head
))
793 if (vgic_cpu
->used_lrs
)
794 vgic_fold_lr_state(vcpu
);
795 vgic_prune_ap_list(vcpu
);
798 /* Flush our emulation state into the GIC hardware before entering the guest. */
799 void kvm_vgic_flush_hwstate(struct kvm_vcpu
*vcpu
)
801 WARN_ON(vgic_v4_flush_hwstate(vcpu
));
804 * If there are no virtual interrupts active or pending for this
805 * VCPU, then there is no work to do and we can bail out without
806 * taking any lock. There is a potential race with someone injecting
807 * interrupts to the VCPU, but it is a benign race as the VCPU will
808 * either observe the new interrupt before or after doing this check,
809 * and introducing additional synchronization mechanism doesn't change
812 if (list_empty(&vcpu
->arch
.vgic_cpu
.ap_list_head
))
815 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
817 spin_lock(&vcpu
->arch
.vgic_cpu
.ap_list_lock
);
818 vgic_flush_lr_state(vcpu
);
819 spin_unlock(&vcpu
->arch
.vgic_cpu
.ap_list_lock
);
822 void kvm_vgic_load(struct kvm_vcpu
*vcpu
)
824 if (unlikely(!vgic_initialized(vcpu
->kvm
)))
827 if (kvm_vgic_global_state
.type
== VGIC_V2
)
833 void kvm_vgic_put(struct kvm_vcpu
*vcpu
)
835 if (unlikely(!vgic_initialized(vcpu
->kvm
)))
838 if (kvm_vgic_global_state
.type
== VGIC_V2
)
844 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu
*vcpu
)
846 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
847 struct vgic_irq
*irq
;
848 bool pending
= false;
851 if (!vcpu
->kvm
->arch
.vgic
.enabled
)
854 if (vcpu
->arch
.vgic_cpu
.vgic_v3
.its_vpe
.pending_last
)
857 spin_lock_irqsave(&vgic_cpu
->ap_list_lock
, flags
);
859 list_for_each_entry(irq
, &vgic_cpu
->ap_list_head
, ap_list
) {
860 spin_lock(&irq
->irq_lock
);
861 pending
= irq_is_pending(irq
) && irq
->enabled
;
862 spin_unlock(&irq
->irq_lock
);
868 spin_unlock_irqrestore(&vgic_cpu
->ap_list_lock
, flags
);
873 void vgic_kick_vcpus(struct kvm
*kvm
)
875 struct kvm_vcpu
*vcpu
;
879 * We've injected an interrupt, time to find out who deserves
882 kvm_for_each_vcpu(c
, vcpu
, kvm
) {
883 if (kvm_vgic_vcpu_pending_irq(vcpu
)) {
884 kvm_make_request(KVM_REQ_IRQ_PENDING
, vcpu
);
890 bool kvm_vgic_map_is_active(struct kvm_vcpu
*vcpu
, unsigned int vintid
)
892 struct vgic_irq
*irq
;
896 if (!vgic_initialized(vcpu
->kvm
))
899 irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, vintid
);
900 spin_lock_irqsave(&irq
->irq_lock
, flags
);
901 map_is_active
= irq
->hw
&& irq
->active
;
902 spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
903 vgic_put_irq(vcpu
->kvm
, irq
);
905 return map_is_active
;