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/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_host.h>
21 #include <linux/list_sort.h>
22 #include <linux/nospec.h>
24 #include <asm/kvm_hyp.h>
28 #define CREATE_TRACE_POINTS
31 struct vgic_global kvm_vgic_global_state __ro_after_init = {
32 .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
36 * Locking order is always:
38 * its->cmd_lock (mutex)
39 * its->its_lock (mutex)
40 * vgic_cpu->ap_list_lock must be taken with IRQs disabled
41 * kvm->lpi_list_lock must be taken with IRQs disabled
42 * vgic_irq->irq_lock must be taken with IRQs disabled
44 * As the ap_list_lock might be taken from the timer interrupt handler,
45 * we have to disable IRQs before taking this lock and everything lower
48 * If you need to take multiple locks, always take the upper lock first,
49 * then the lower ones, e.g. first take the its_lock, then the irq_lock.
50 * If you are already holding a lock and need to take a higher one, you
51 * have to drop the lower ranking lock first and re-aquire it after having
52 * taken the upper one.
54 * When taking more than one ap_list_lock at the same time, always take the
55 * lowest numbered VCPU's ap_list_lock first, so:
56 * vcpuX->vcpu_id < vcpuY->vcpu_id:
57 * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
58 * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
60 * Since the VGIC must support injecting virtual interrupts from ISRs, we have
61 * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
62 * spinlocks for any lock that may be taken while injecting an interrupt.
66 * Iterate over the VM's list of mapped LPIs to find the one with a
67 * matching interrupt ID and return a reference to the IRQ structure.
69 static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
71 struct vgic_dist *dist = &kvm->arch.vgic;
72 struct vgic_irq *irq = NULL;
75 spin_lock_irqsave(&dist->lpi_list_lock, flags);
77 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
78 if (irq->intid != intid)
82 * This increases the refcount, the caller is expected to
83 * call vgic_put_irq() later once it's finished with the IRQ.
85 vgic_get_irq_kref(irq);
91 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
97 * This looks up the virtual interrupt ID to get the corresponding
98 * struct vgic_irq. It also increases the refcount, so any caller is expected
99 * to call vgic_put_irq() once it's finished with this IRQ.
101 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
105 if (intid <= VGIC_MAX_PRIVATE) {
106 intid = array_index_nospec(intid, VGIC_MAX_PRIVATE);
107 return &vcpu->arch.vgic_cpu.private_irqs[intid];
111 if (intid <= VGIC_MAX_SPI) {
112 intid = array_index_nospec(intid, VGIC_MAX_SPI);
113 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
117 if (intid >= VGIC_MIN_LPI)
118 return vgic_get_lpi(kvm, intid);
120 WARN(1, "Looking up struct vgic_irq for reserved INTID");
125 * We can't do anything in here, because we lack the kvm pointer to
126 * lock and remove the item from the lpi_list. So we keep this function
127 * empty and use the return value of kref_put() to trigger the freeing.
129 static void vgic_irq_release(struct kref *ref)
133 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
135 struct vgic_dist *dist = &kvm->arch.vgic;
138 if (irq->intid < VGIC_MIN_LPI)
141 spin_lock_irqsave(&dist->lpi_list_lock, flags);
142 if (!kref_put(&irq->refcount, vgic_irq_release)) {
143 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
147 list_del(&irq->lpi_list);
148 dist->lpi_list_count--;
149 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
154 void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
156 WARN_ON(irq_set_irqchip_state(irq->host_irq,
157 IRQCHIP_STATE_PENDING,
161 bool vgic_get_phys_line_level(struct vgic_irq *irq)
167 if (irq->get_input_level)
168 return irq->get_input_level(irq->intid);
170 WARN_ON(irq_get_irqchip_state(irq->host_irq,
171 IRQCHIP_STATE_PENDING,
176 /* Set/Clear the physical active state */
177 void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active)
181 WARN_ON(irq_set_irqchip_state(irq->host_irq,
182 IRQCHIP_STATE_ACTIVE,
187 * kvm_vgic_target_oracle - compute the target vcpu for an irq
189 * @irq: The irq to route. Must be already locked.
191 * Based on the current state of the interrupt (enabled, pending,
192 * active, vcpu and target_vcpu), compute the next vcpu this should be
193 * given to. Return NULL if this shouldn't be injected at all.
195 * Requires the IRQ lock to be held.
197 static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
199 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
201 /* If the interrupt is active, it must stay on the current vcpu */
203 return irq->vcpu ? : irq->target_vcpu;
206 * If the IRQ is not active but enabled and pending, we should direct
207 * it to its configured target VCPU.
208 * If the distributor is disabled, pending interrupts shouldn't be
211 if (irq->enabled && irq_is_pending(irq)) {
212 if (unlikely(irq->target_vcpu &&
213 !irq->target_vcpu->kvm->arch.vgic.enabled))
216 return irq->target_vcpu;
219 /* If neither active nor pending and enabled, then this IRQ should not
220 * be queued to any VCPU.
226 * The order of items in the ap_lists defines how we'll pack things in LRs as
227 * well, the first items in the list being the first things populated in the
230 * A hard rule is that active interrupts can never be pushed out of the LRs
231 * (and therefore take priority) since we cannot reliably trap on deactivation
232 * of IRQs and therefore they have to be present in the LRs.
234 * Otherwise things should be sorted by the priority field and the GIC
235 * hardware support will take care of preemption of priority groups etc.
237 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
238 * to sort "b" before "a".
240 static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
242 struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
243 struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
247 spin_lock(&irqa->irq_lock);
248 spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
250 if (irqa->active || irqb->active) {
251 ret = (int)irqb->active - (int)irqa->active;
255 penda = irqa->enabled && irq_is_pending(irqa);
256 pendb = irqb->enabled && irq_is_pending(irqb);
258 if (!penda || !pendb) {
259 ret = (int)pendb - (int)penda;
263 /* Both pending and enabled, sort by priority */
264 ret = irqa->priority - irqb->priority;
266 spin_unlock(&irqb->irq_lock);
267 spin_unlock(&irqa->irq_lock);
271 /* Must be called with the ap_list_lock held */
272 static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
274 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
276 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
278 list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
282 * Only valid injection if changing level for level-triggered IRQs or for a
283 * rising edge, and in-kernel connected IRQ lines can only be controlled by
286 static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
288 if (irq->owner != owner)
291 switch (irq->config) {
292 case VGIC_CONFIG_LEVEL:
293 return irq->line_level != level;
294 case VGIC_CONFIG_EDGE:
302 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
303 * Do the queuing if necessary, taking the right locks in the right order.
304 * Returns true when the IRQ was queued, false otherwise.
306 * Needs to be entered with the IRQ lock already held, but will return
307 * with all locks dropped.
309 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
312 struct kvm_vcpu *vcpu;
314 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
317 vcpu = vgic_target_oracle(irq);
318 if (irq->vcpu || !vcpu) {
320 * If this IRQ is already on a VCPU's ap_list, then it
321 * cannot be moved or modified and there is no more work for
324 * Otherwise, if the irq is not pending and enabled, it does
325 * not need to be inserted into an ap_list and there is also
326 * no more work for us to do.
328 spin_unlock_irqrestore(&irq->irq_lock, flags);
331 * We have to kick the VCPU here, because we could be
332 * queueing an edge-triggered interrupt for which we
333 * get no EOI maintenance interrupt. In that case,
334 * while the IRQ is already on the VCPU's AP list, the
335 * VCPU could have EOI'ed the original interrupt and
336 * won't see this one until it exits for some other
340 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
347 * We must unlock the irq lock to take the ap_list_lock where
348 * we are going to insert this new pending interrupt.
350 spin_unlock_irqrestore(&irq->irq_lock, flags);
352 /* someone can do stuff here, which we re-check below */
354 spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
355 spin_lock(&irq->irq_lock);
358 * Did something change behind our backs?
360 * There are two cases:
361 * 1) The irq lost its pending state or was disabled behind our
362 * backs and/or it was queued to another VCPU's ap_list.
363 * 2) Someone changed the affinity on this irq behind our
364 * backs and we are now holding the wrong ap_list_lock.
366 * In both cases, drop the locks and retry.
369 if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
370 spin_unlock(&irq->irq_lock);
371 spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
373 spin_lock_irqsave(&irq->irq_lock, flags);
378 * Grab a reference to the irq to reflect the fact that it is
379 * now in the ap_list.
381 vgic_get_irq_kref(irq);
382 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
385 spin_unlock(&irq->irq_lock);
386 spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
388 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
395 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
396 * @kvm: The VM structure pointer
397 * @cpuid: The CPU for PPIs
398 * @intid: The INTID to inject a new state to.
399 * @level: Edge-triggered: true: to trigger the interrupt
400 * false: to ignore the call
401 * Level-sensitive true: raise the input signal
402 * false: lower the input signal
403 * @owner: The opaque pointer to the owner of the IRQ being raised to verify
404 * that the caller is allowed to inject this IRQ. Userspace
405 * injections will have owner == NULL.
407 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
408 * level-sensitive interrupts. You can think of the level parameter as 1
409 * being HIGH and 0 being LOW and all devices being active-HIGH.
411 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
412 bool level, void *owner)
414 struct kvm_vcpu *vcpu;
415 struct vgic_irq *irq;
419 trace_vgic_update_irq_pending(cpuid, intid, level);
421 ret = vgic_lazy_init(kvm);
425 vcpu = kvm_get_vcpu(kvm, cpuid);
426 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
429 irq = vgic_get_irq(kvm, vcpu, intid);
433 spin_lock_irqsave(&irq->irq_lock, flags);
435 if (!vgic_validate_injection(irq, level, owner)) {
436 /* Nothing to see here, move along... */
437 spin_unlock_irqrestore(&irq->irq_lock, flags);
438 vgic_put_irq(kvm, irq);
442 if (irq->config == VGIC_CONFIG_LEVEL)
443 irq->line_level = level;
445 irq->pending_latch = true;
447 vgic_queue_irq_unlock(kvm, irq, flags);
448 vgic_put_irq(kvm, irq);
453 /* @irq->irq_lock must be held */
454 static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
455 unsigned int host_irq,
456 bool (*get_input_level)(int vindid))
458 struct irq_desc *desc;
459 struct irq_data *data;
462 * Find the physical IRQ number corresponding to @host_irq
464 desc = irq_to_desc(host_irq);
466 kvm_err("%s: no interrupt descriptor\n", __func__);
469 data = irq_desc_get_irq_data(desc);
470 while (data->parent_data)
471 data = data->parent_data;
474 irq->host_irq = host_irq;
475 irq->hwintid = data->hwirq;
476 irq->get_input_level = get_input_level;
480 /* @irq->irq_lock must be held */
481 static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
485 irq->get_input_level = NULL;
488 int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
489 u32 vintid, bool (*get_input_level)(int vindid))
491 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
497 spin_lock_irqsave(&irq->irq_lock, flags);
498 ret = kvm_vgic_map_irq(vcpu, irq, host_irq, get_input_level);
499 spin_unlock_irqrestore(&irq->irq_lock, flags);
500 vgic_put_irq(vcpu->kvm, irq);
506 * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
507 * @vcpu: The VCPU pointer
508 * @vintid: The INTID of the interrupt
510 * Reset the active and pending states of a mapped interrupt. Kernel
511 * subsystems injecting mapped interrupts should reset their interrupt lines
512 * when we are doing a reset of the VM.
514 void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
516 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
522 spin_lock_irqsave(&irq->irq_lock, flags);
524 irq->pending_latch = false;
525 irq->line_level = false;
526 spin_unlock_irqrestore(&irq->irq_lock, flags);
528 vgic_put_irq(vcpu->kvm, irq);
531 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
533 struct vgic_irq *irq;
536 if (!vgic_initialized(vcpu->kvm))
539 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
542 spin_lock_irqsave(&irq->irq_lock, flags);
543 kvm_vgic_unmap_irq(irq);
544 spin_unlock_irqrestore(&irq->irq_lock, flags);
545 vgic_put_irq(vcpu->kvm, irq);
551 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
553 * @vcpu: Pointer to the VCPU (used for PPIs)
554 * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
555 * @owner: Opaque pointer to the owner
557 * Returns 0 if intid is not already used by another in-kernel device and the
558 * owner is set, otherwise returns an error code.
560 int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
562 struct vgic_irq *irq;
566 if (!vgic_initialized(vcpu->kvm))
569 /* SGIs and LPIs cannot be wired up to any device */
570 if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
573 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
574 spin_lock_irqsave(&irq->irq_lock, flags);
575 if (irq->owner && irq->owner != owner)
579 spin_unlock_irqrestore(&irq->irq_lock, flags);
585 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
587 * @vcpu: The VCPU pointer
589 * Go over the list of "interesting" interrupts, and prune those that we
590 * won't have to consider in the near future.
592 static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
594 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
595 struct vgic_irq *irq, *tmp;
597 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
600 spin_lock(&vgic_cpu->ap_list_lock);
602 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
603 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
604 bool target_vcpu_needs_kick = false;
606 spin_lock(&irq->irq_lock);
608 BUG_ON(vcpu != irq->vcpu);
610 target_vcpu = vgic_target_oracle(irq);
614 * We don't need to process this interrupt any
615 * further, move it off the list.
617 list_del(&irq->ap_list);
619 spin_unlock(&irq->irq_lock);
622 * This vgic_put_irq call matches the
623 * vgic_get_irq_kref in vgic_queue_irq_unlock,
624 * where we added the LPI to the ap_list. As
625 * we remove the irq from the list, we drop
626 * also drop the refcount.
628 vgic_put_irq(vcpu->kvm, irq);
632 if (target_vcpu == vcpu) {
633 /* We're on the right CPU */
634 spin_unlock(&irq->irq_lock);
638 /* This interrupt looks like it has to be migrated. */
640 spin_unlock(&irq->irq_lock);
641 spin_unlock(&vgic_cpu->ap_list_lock);
644 * Ensure locking order by always locking the smallest
647 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
655 spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
656 spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
657 SINGLE_DEPTH_NESTING);
658 spin_lock(&irq->irq_lock);
661 * If the affinity has been preserved, move the
662 * interrupt around. Otherwise, it means things have
663 * changed while the interrupt was unlocked, and we
664 * need to replay this.
666 * In all cases, we cannot trust the list not to have
667 * changed, so we restart from the beginning.
669 if (target_vcpu == vgic_target_oracle(irq)) {
670 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
672 list_del(&irq->ap_list);
673 irq->vcpu = target_vcpu;
674 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
675 target_vcpu_needs_kick = true;
678 spin_unlock(&irq->irq_lock);
679 spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
680 spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
682 if (target_vcpu_needs_kick) {
683 kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
684 kvm_vcpu_kick(target_vcpu);
690 spin_unlock(&vgic_cpu->ap_list_lock);
693 static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
695 if (kvm_vgic_global_state.type == VGIC_V2)
696 vgic_v2_fold_lr_state(vcpu);
698 vgic_v3_fold_lr_state(vcpu);
701 /* Requires the irq_lock to be held. */
702 static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
703 struct vgic_irq *irq, int lr)
705 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
707 if (kvm_vgic_global_state.type == VGIC_V2)
708 vgic_v2_populate_lr(vcpu, irq, lr);
710 vgic_v3_populate_lr(vcpu, irq, lr);
713 static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
715 if (kvm_vgic_global_state.type == VGIC_V2)
716 vgic_v2_clear_lr(vcpu, lr);
718 vgic_v3_clear_lr(vcpu, lr);
721 static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
723 if (kvm_vgic_global_state.type == VGIC_V2)
724 vgic_v2_set_underflow(vcpu);
726 vgic_v3_set_underflow(vcpu);
729 /* Requires the ap_list_lock to be held. */
730 static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
733 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
734 struct vgic_irq *irq;
739 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
741 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
744 spin_lock(&irq->irq_lock);
745 /* GICv2 SGIs can count for more than one... */
746 w = vgic_irq_get_lr_count(irq);
747 spin_unlock(&irq->irq_lock);
750 *multi_sgi |= (w > 1);
755 /* Requires the VCPU's ap_list_lock to be held. */
756 static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
758 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
759 struct vgic_irq *irq;
764 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
766 count = compute_ap_list_depth(vcpu, &multi_sgi);
767 if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
768 vgic_sort_ap_list(vcpu);
772 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
773 spin_lock(&irq->irq_lock);
776 * If we have multi-SGIs in the pipeline, we need to
777 * guarantee that they are all seen before any IRQ of
778 * lower priority. In that case, we need to filter out
779 * these interrupts by exiting early. This is easy as
780 * the AP list has been sorted already.
782 if (multi_sgi && irq->priority > prio) {
783 spin_unlock(&irq->irq_lock);
787 if (likely(vgic_target_oracle(irq) == vcpu)) {
788 vgic_populate_lr(vcpu, irq, count++);
791 prio = irq->priority;
794 spin_unlock(&irq->irq_lock);
796 if (count == kvm_vgic_global_state.nr_lr) {
797 if (!list_is_last(&irq->ap_list,
798 &vgic_cpu->ap_list_head))
799 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 static inline bool can_access_vgic_from_kernel(void)
814 * GICv2 can always be accessed from the kernel because it is
815 * memory-mapped, and VHE systems can access GICv3 EL2 system
818 return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
821 static inline void vgic_save_state(struct kvm_vcpu *vcpu)
823 if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
824 vgic_v2_save_state(vcpu);
826 __vgic_v3_save_state(vcpu);
829 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
830 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
832 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
834 WARN_ON(vgic_v4_sync_hwstate(vcpu));
836 /* An empty ap_list_head implies used_lrs == 0 */
837 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
840 if (can_access_vgic_from_kernel())
841 vgic_save_state(vcpu);
843 if (vgic_cpu->used_lrs)
844 vgic_fold_lr_state(vcpu);
845 vgic_prune_ap_list(vcpu);
848 static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
850 if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
851 vgic_v2_restore_state(vcpu);
853 __vgic_v3_restore_state(vcpu);
856 /* Flush our emulation state into the GIC hardware before entering the guest. */
857 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
859 WARN_ON(vgic_v4_flush_hwstate(vcpu));
862 * If there are no virtual interrupts active or pending for this
863 * VCPU, then there is no work to do and we can bail out without
864 * taking any lock. There is a potential race with someone injecting
865 * interrupts to the VCPU, but it is a benign race as the VCPU will
866 * either observe the new interrupt before or after doing this check,
867 * and introducing additional synchronization mechanism doesn't change
870 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
873 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
875 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
876 vgic_flush_lr_state(vcpu);
877 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
879 if (can_access_vgic_from_kernel())
880 vgic_restore_state(vcpu);
883 void kvm_vgic_load(struct kvm_vcpu *vcpu)
885 if (unlikely(!vgic_initialized(vcpu->kvm)))
888 if (kvm_vgic_global_state.type == VGIC_V2)
894 void kvm_vgic_put(struct kvm_vcpu *vcpu)
896 if (unlikely(!vgic_initialized(vcpu->kvm)))
899 if (kvm_vgic_global_state.type == VGIC_V2)
905 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
907 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
908 struct vgic_irq *irq;
909 bool pending = false;
912 if (!vcpu->kvm->arch.vgic.enabled)
915 if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
918 spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
920 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
921 spin_lock(&irq->irq_lock);
922 pending = irq_is_pending(irq) && irq->enabled;
923 spin_unlock(&irq->irq_lock);
929 spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
934 void vgic_kick_vcpus(struct kvm *kvm)
936 struct kvm_vcpu *vcpu;
940 * We've injected an interrupt, time to find out who deserves
943 kvm_for_each_vcpu(c, vcpu, kvm) {
944 if (kvm_vgic_vcpu_pending_irq(vcpu)) {
945 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
951 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
953 struct vgic_irq *irq;
957 if (!vgic_initialized(vcpu->kvm))
960 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
961 spin_lock_irqsave(&irq->irq_lock, flags);
962 map_is_active = irq->hw && irq->active;
963 spin_unlock_irqrestore(&irq->irq_lock, flags);
964 vgic_put_irq(vcpu->kvm, irq);
966 return map_is_active;