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>
23 #define CREATE_TRACE_POINTS
26 #ifdef CONFIG_DEBUG_SPINLOCK
27 #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
29 #define DEBUG_SPINLOCK_BUG_ON(p)
32 struct vgic_global kvm_vgic_global_state __ro_after_init = {
33 .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
37 * Locking order is always:
38 * its->cmd_lock (mutex)
39 * its->its_lock (mutex)
40 * vgic_cpu->ap_list_lock
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-aquire it after having
48 * taken the upper one.
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 * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
54 * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
58 * Iterate over the VM's list of mapped LPIs to find the one with a
59 * matching interrupt ID and return a reference to the IRQ structure.
61 static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
63 struct vgic_dist *dist = &kvm->arch.vgic;
64 struct vgic_irq *irq = NULL;
66 spin_lock(&dist->lpi_list_lock);
68 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
69 if (irq->intid != intid)
73 * This increases the refcount, the caller is expected to
74 * call vgic_put_irq() later once it's finished with the IRQ.
76 vgic_get_irq_kref(irq);
82 spin_unlock(&dist->lpi_list_lock);
88 * This looks up the virtual interrupt ID to get the corresponding
89 * struct vgic_irq. It also increases the refcount, so any caller is expected
90 * to call vgic_put_irq() once it's finished with this IRQ.
92 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
96 if (intid <= VGIC_MAX_PRIVATE)
97 return &vcpu->arch.vgic_cpu.private_irqs[intid];
100 if (intid <= VGIC_MAX_SPI)
101 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
104 if (intid >= VGIC_MIN_LPI)
105 return vgic_get_lpi(kvm, intid);
107 WARN(1, "Looking up struct vgic_irq for reserved INTID");
112 * We can't do anything in here, because we lack the kvm pointer to
113 * lock and remove the item from the lpi_list. So we keep this function
114 * empty and use the return value of kref_put() to trigger the freeing.
116 static void vgic_irq_release(struct kref *ref)
120 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
122 struct vgic_dist *dist = &kvm->arch.vgic;
124 if (irq->intid < VGIC_MIN_LPI)
127 spin_lock(&dist->lpi_list_lock);
128 if (!kref_put(&irq->refcount, vgic_irq_release)) {
129 spin_unlock(&dist->lpi_list_lock);
133 list_del(&irq->lpi_list);
134 dist->lpi_list_count--;
135 spin_unlock(&dist->lpi_list_lock);
141 * kvm_vgic_target_oracle - compute the target vcpu for an irq
143 * @irq: The irq to route. Must be already locked.
145 * Based on the current state of the interrupt (enabled, pending,
146 * active, vcpu and target_vcpu), compute the next vcpu this should be
147 * given to. Return NULL if this shouldn't be injected at all.
149 * Requires the IRQ lock to be held.
151 static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
153 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
155 /* If the interrupt is active, it must stay on the current vcpu */
157 return irq->vcpu ? : irq->target_vcpu;
160 * If the IRQ is not active but enabled and pending, we should direct
161 * it to its configured target VCPU.
162 * If the distributor is disabled, pending interrupts shouldn't be
165 if (irq->enabled && irq_is_pending(irq)) {
166 if (unlikely(irq->target_vcpu &&
167 !irq->target_vcpu->kvm->arch.vgic.enabled))
170 return irq->target_vcpu;
173 /* If neither active nor pending and enabled, then this IRQ should not
174 * be queued to any VCPU.
180 * The order of items in the ap_lists defines how we'll pack things in LRs as
181 * well, the first items in the list being the first things populated in the
184 * A hard rule is that active interrupts can never be pushed out of the LRs
185 * (and therefore take priority) since we cannot reliably trap on deactivation
186 * of IRQs and therefore they have to be present in the LRs.
188 * Otherwise things should be sorted by the priority field and the GIC
189 * hardware support will take care of preemption of priority groups etc.
191 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
192 * to sort "b" before "a".
194 static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
196 struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
197 struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
201 spin_lock(&irqa->irq_lock);
202 spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
204 if (irqa->active || irqb->active) {
205 ret = (int)irqb->active - (int)irqa->active;
209 penda = irqa->enabled && irq_is_pending(irqa);
210 pendb = irqb->enabled && irq_is_pending(irqb);
212 if (!penda || !pendb) {
213 ret = (int)pendb - (int)penda;
217 /* Both pending and enabled, sort by priority */
218 ret = irqa->priority - irqb->priority;
220 spin_unlock(&irqb->irq_lock);
221 spin_unlock(&irqa->irq_lock);
225 /* Must be called with the ap_list_lock held */
226 static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
228 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
230 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
232 list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
236 * Only valid injection if changing level for level-triggered IRQs or for a
239 static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
241 switch (irq->config) {
242 case VGIC_CONFIG_LEVEL:
243 return irq->line_level != level;
244 case VGIC_CONFIG_EDGE:
252 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
253 * Do the queuing if necessary, taking the right locks in the right order.
254 * Returns true when the IRQ was queued, false otherwise.
256 * Needs to be entered with the IRQ lock already held, but will return
257 * with all locks dropped.
259 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq)
261 struct kvm_vcpu *vcpu;
263 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
266 vcpu = vgic_target_oracle(irq);
267 if (irq->vcpu || !vcpu) {
269 * If this IRQ is already on a VCPU's ap_list, then it
270 * cannot be moved or modified and there is no more work for
273 * Otherwise, if the irq is not pending and enabled, it does
274 * not need to be inserted into an ap_list and there is also
275 * no more work for us to do.
277 spin_unlock(&irq->irq_lock);
280 * We have to kick the VCPU here, because we could be
281 * queueing an edge-triggered interrupt for which we
282 * get no EOI maintenance interrupt. In that case,
283 * while the IRQ is already on the VCPU's AP list, the
284 * VCPU could have EOI'ed the original interrupt and
285 * won't see this one until it exits for some other
294 * We must unlock the irq lock to take the ap_list_lock where
295 * we are going to insert this new pending interrupt.
297 spin_unlock(&irq->irq_lock);
299 /* someone can do stuff here, which we re-check below */
301 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
302 spin_lock(&irq->irq_lock);
305 * Did something change behind our backs?
307 * There are two cases:
308 * 1) The irq lost its pending state or was disabled behind our
309 * backs and/or it was queued to another VCPU's ap_list.
310 * 2) Someone changed the affinity on this irq behind our
311 * backs and we are now holding the wrong ap_list_lock.
313 * In both cases, drop the locks and retry.
316 if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
317 spin_unlock(&irq->irq_lock);
318 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
320 spin_lock(&irq->irq_lock);
325 * Grab a reference to the irq to reflect the fact that it is
326 * now in the ap_list.
328 vgic_get_irq_kref(irq);
329 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
332 spin_unlock(&irq->irq_lock);
333 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
341 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
342 * @kvm: The VM structure pointer
343 * @cpuid: The CPU for PPIs
344 * @intid: The INTID to inject a new state to.
345 * @level: Edge-triggered: true: to trigger the interrupt
346 * false: to ignore the call
347 * Level-sensitive true: raise the input signal
348 * false: lower the input signal
350 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
351 * level-sensitive interrupts. You can think of the level parameter as 1
352 * being HIGH and 0 being LOW and all devices being active-HIGH.
354 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
357 struct kvm_vcpu *vcpu;
358 struct vgic_irq *irq;
361 trace_vgic_update_irq_pending(cpuid, intid, level);
363 ret = vgic_lazy_init(kvm);
367 vcpu = kvm_get_vcpu(kvm, cpuid);
368 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
371 irq = vgic_get_irq(kvm, vcpu, intid);
375 spin_lock(&irq->irq_lock);
377 if (!vgic_validate_injection(irq, level)) {
378 /* Nothing to see here, move along... */
379 spin_unlock(&irq->irq_lock);
380 vgic_put_irq(kvm, irq);
384 if (irq->config == VGIC_CONFIG_LEVEL)
385 irq->line_level = level;
387 irq->pending_latch = true;
389 vgic_queue_irq_unlock(kvm, irq);
390 vgic_put_irq(kvm, irq);
395 int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq)
397 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
401 spin_lock(&irq->irq_lock);
404 irq->hwintid = phys_irq;
406 spin_unlock(&irq->irq_lock);
407 vgic_put_irq(vcpu->kvm, irq);
412 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq)
414 struct vgic_irq *irq;
416 if (!vgic_initialized(vcpu->kvm))
419 irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
422 spin_lock(&irq->irq_lock);
427 spin_unlock(&irq->irq_lock);
428 vgic_put_irq(vcpu->kvm, irq);
434 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
436 * @vcpu: The VCPU pointer
438 * Go over the list of "interesting" interrupts, and prune those that we
439 * won't have to consider in the near future.
441 static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
443 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
444 struct vgic_irq *irq, *tmp;
447 spin_lock(&vgic_cpu->ap_list_lock);
449 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
450 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
452 spin_lock(&irq->irq_lock);
454 BUG_ON(vcpu != irq->vcpu);
456 target_vcpu = vgic_target_oracle(irq);
460 * We don't need to process this interrupt any
461 * further, move it off the list.
463 list_del(&irq->ap_list);
465 spin_unlock(&irq->irq_lock);
468 * This vgic_put_irq call matches the
469 * vgic_get_irq_kref in vgic_queue_irq_unlock,
470 * where we added the LPI to the ap_list. As
471 * we remove the irq from the list, we drop
472 * also drop the refcount.
474 vgic_put_irq(vcpu->kvm, irq);
478 if (target_vcpu == vcpu) {
479 /* We're on the right CPU */
480 spin_unlock(&irq->irq_lock);
484 /* This interrupt looks like it has to be migrated. */
486 spin_unlock(&irq->irq_lock);
487 spin_unlock(&vgic_cpu->ap_list_lock);
490 * Ensure locking order by always locking the smallest
493 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
501 spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
502 spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
503 SINGLE_DEPTH_NESTING);
504 spin_lock(&irq->irq_lock);
507 * If the affinity has been preserved, move the
508 * interrupt around. Otherwise, it means things have
509 * changed while the interrupt was unlocked, and we
510 * need to replay this.
512 * In all cases, we cannot trust the list not to have
513 * changed, so we restart from the beginning.
515 if (target_vcpu == vgic_target_oracle(irq)) {
516 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
518 list_del(&irq->ap_list);
519 irq->vcpu = target_vcpu;
520 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
523 spin_unlock(&irq->irq_lock);
524 spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
525 spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
529 spin_unlock(&vgic_cpu->ap_list_lock);
532 static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
534 if (kvm_vgic_global_state.type == VGIC_V2)
535 vgic_v2_fold_lr_state(vcpu);
537 vgic_v3_fold_lr_state(vcpu);
540 /* Requires the irq_lock to be held. */
541 static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
542 struct vgic_irq *irq, int lr)
544 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
546 if (kvm_vgic_global_state.type == VGIC_V2)
547 vgic_v2_populate_lr(vcpu, irq, lr);
549 vgic_v3_populate_lr(vcpu, irq, lr);
552 static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
554 if (kvm_vgic_global_state.type == VGIC_V2)
555 vgic_v2_clear_lr(vcpu, lr);
557 vgic_v3_clear_lr(vcpu, lr);
560 static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
562 if (kvm_vgic_global_state.type == VGIC_V2)
563 vgic_v2_set_underflow(vcpu);
565 vgic_v3_set_underflow(vcpu);
568 /* Requires the ap_list_lock to be held. */
569 static int compute_ap_list_depth(struct kvm_vcpu *vcpu)
571 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
572 struct vgic_irq *irq;
575 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
577 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
578 spin_lock(&irq->irq_lock);
579 /* GICv2 SGIs can count for more than one... */
580 if (vgic_irq_is_sgi(irq->intid) && irq->source)
581 count += hweight8(irq->source);
584 spin_unlock(&irq->irq_lock);
589 /* Requires the VCPU's ap_list_lock to be held. */
590 static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
592 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
593 struct vgic_irq *irq;
596 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
598 if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr)
599 vgic_sort_ap_list(vcpu);
601 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
602 spin_lock(&irq->irq_lock);
604 if (unlikely(vgic_target_oracle(irq) != vcpu))
608 * If we get an SGI with multiple sources, try to get
609 * them in all at once.
612 vgic_populate_lr(vcpu, irq, count++);
613 } while (irq->source && count < kvm_vgic_global_state.nr_lr);
616 spin_unlock(&irq->irq_lock);
618 if (count == kvm_vgic_global_state.nr_lr) {
619 if (!list_is_last(&irq->ap_list,
620 &vgic_cpu->ap_list_head))
621 vgic_set_underflow(vcpu);
626 vcpu->arch.vgic_cpu.used_lrs = count;
628 /* Nuke remaining LRs */
629 for ( ; count < kvm_vgic_global_state.nr_lr; count++)
630 vgic_clear_lr(vcpu, count);
633 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
634 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
636 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
638 /* An empty ap_list_head implies used_lrs == 0 */
639 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
642 if (vgic_cpu->used_lrs)
643 vgic_fold_lr_state(vcpu);
644 vgic_prune_ap_list(vcpu);
647 /* Flush our emulation state into the GIC hardware before entering the guest. */
648 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
651 * If there are no virtual interrupts active or pending for this
652 * VCPU, then there is no work to do and we can bail out without
653 * taking any lock. There is a potential race with someone injecting
654 * interrupts to the VCPU, but it is a benign race as the VCPU will
655 * either observe the new interrupt before or after doing this check,
656 * and introducing additional synchronization mechanism doesn't change
659 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
662 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
663 vgic_flush_lr_state(vcpu);
664 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
667 void kvm_vgic_load(struct kvm_vcpu *vcpu)
669 if (unlikely(!vgic_initialized(vcpu->kvm)))
672 if (kvm_vgic_global_state.type == VGIC_V2)
678 void kvm_vgic_put(struct kvm_vcpu *vcpu)
680 if (unlikely(!vgic_initialized(vcpu->kvm)))
683 if (kvm_vgic_global_state.type == VGIC_V2)
689 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
691 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
692 struct vgic_irq *irq;
693 bool pending = false;
695 if (!vcpu->kvm->arch.vgic.enabled)
698 spin_lock(&vgic_cpu->ap_list_lock);
700 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
701 spin_lock(&irq->irq_lock);
702 pending = irq_is_pending(irq) && irq->enabled;
703 spin_unlock(&irq->irq_lock);
709 spin_unlock(&vgic_cpu->ap_list_lock);
714 void vgic_kick_vcpus(struct kvm *kvm)
716 struct kvm_vcpu *vcpu;
720 * We've injected an interrupt, time to find out who deserves
723 kvm_for_each_vcpu(c, vcpu, kvm) {
724 if (kvm_vgic_vcpu_pending_irq(vcpu))
729 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)
731 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
734 spin_lock(&irq->irq_lock);
735 map_is_active = irq->hw && irq->active;
736 spin_unlock(&irq->irq_lock);
737 vgic_put_irq(vcpu->kvm, irq);
739 return map_is_active;