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 __section(.hyp.text) kvm_vgic_global_state;
35 * Locking order is always:
36 * vgic_cpu->ap_list_lock
39 * (that is, always take the ap_list_lock before the struct vgic_irq lock).
41 * When taking more than one ap_list_lock at the same time, always take the
42 * lowest numbered VCPU's ap_list_lock first, so:
43 * vcpuX->vcpu_id < vcpuY->vcpu_id:
44 * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
45 * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
48 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
52 if (intid <= VGIC_MAX_PRIVATE)
53 return &vcpu->arch.vgic_cpu.private_irqs[intid];
56 if (intid <= VGIC_MAX_SPI)
57 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
59 /* LPIs are not yet covered */
60 if (intid >= VGIC_MIN_LPI)
63 WARN(1, "Looking up struct vgic_irq for reserved INTID");
67 static void vgic_get_irq_kref(struct vgic_irq *irq)
69 if (irq->intid < VGIC_MIN_LPI)
72 kref_get(&irq->refcount);
75 /* The refcount should never drop to 0 at the moment. */
76 static void vgic_irq_release(struct kref *ref)
81 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
83 if (irq->intid < VGIC_MIN_LPI)
86 kref_put(&irq->refcount, vgic_irq_release);
90 * kvm_vgic_target_oracle - compute the target vcpu for an irq
92 * @irq: The irq to route. Must be already locked.
94 * Based on the current state of the interrupt (enabled, pending,
95 * active, vcpu and target_vcpu), compute the next vcpu this should be
96 * given to. Return NULL if this shouldn't be injected at all.
98 * Requires the IRQ lock to be held.
100 static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
102 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
104 /* If the interrupt is active, it must stay on the current vcpu */
106 return irq->vcpu ? : irq->target_vcpu;
109 * If the IRQ is not active but enabled and pending, we should direct
110 * it to its configured target VCPU.
111 * If the distributor is disabled, pending interrupts shouldn't be
114 if (irq->enabled && irq->pending) {
115 if (unlikely(irq->target_vcpu &&
116 !irq->target_vcpu->kvm->arch.vgic.enabled))
119 return irq->target_vcpu;
122 /* If neither active nor pending and enabled, then this IRQ should not
123 * be queued to any VCPU.
129 * The order of items in the ap_lists defines how we'll pack things in LRs as
130 * well, the first items in the list being the first things populated in the
133 * A hard rule is that active interrupts can never be pushed out of the LRs
134 * (and therefore take priority) since we cannot reliably trap on deactivation
135 * of IRQs and therefore they have to be present in the LRs.
137 * Otherwise things should be sorted by the priority field and the GIC
138 * hardware support will take care of preemption of priority groups etc.
140 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
141 * to sort "b" before "a".
143 static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
145 struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
146 struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
150 spin_lock(&irqa->irq_lock);
151 spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
153 if (irqa->active || irqb->active) {
154 ret = (int)irqb->active - (int)irqa->active;
158 penda = irqa->enabled && irqa->pending;
159 pendb = irqb->enabled && irqb->pending;
161 if (!penda || !pendb) {
162 ret = (int)pendb - (int)penda;
166 /* Both pending and enabled, sort by priority */
167 ret = irqa->priority - irqb->priority;
169 spin_unlock(&irqb->irq_lock);
170 spin_unlock(&irqa->irq_lock);
174 /* Must be called with the ap_list_lock held */
175 static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
177 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
179 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
181 list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
185 * Only valid injection if changing level for level-triggered IRQs or for a
188 static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
190 switch (irq->config) {
191 case VGIC_CONFIG_LEVEL:
192 return irq->line_level != level;
193 case VGIC_CONFIG_EDGE:
201 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
202 * Do the queuing if necessary, taking the right locks in the right order.
203 * Returns true when the IRQ was queued, false otherwise.
205 * Needs to be entered with the IRQ lock already held, but will return
206 * with all locks dropped.
208 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq)
210 struct kvm_vcpu *vcpu;
212 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
215 vcpu = vgic_target_oracle(irq);
216 if (irq->vcpu || !vcpu) {
218 * If this IRQ is already on a VCPU's ap_list, then it
219 * cannot be moved or modified and there is no more work for
222 * Otherwise, if the irq is not pending and enabled, it does
223 * not need to be inserted into an ap_list and there is also
224 * no more work for us to do.
226 spin_unlock(&irq->irq_lock);
231 * We must unlock the irq lock to take the ap_list_lock where
232 * we are going to insert this new pending interrupt.
234 spin_unlock(&irq->irq_lock);
236 /* someone can do stuff here, which we re-check below */
238 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
239 spin_lock(&irq->irq_lock);
242 * Did something change behind our backs?
244 * There are two cases:
245 * 1) The irq lost its pending state or was disabled behind our
246 * backs and/or it was queued to another VCPU's ap_list.
247 * 2) Someone changed the affinity on this irq behind our
248 * backs and we are now holding the wrong ap_list_lock.
250 * In both cases, drop the locks and retry.
253 if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
254 spin_unlock(&irq->irq_lock);
255 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
257 spin_lock(&irq->irq_lock);
262 * Grab a reference to the irq to reflect the fact that it is
263 * now in the ap_list.
265 vgic_get_irq_kref(irq);
266 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
269 spin_unlock(&irq->irq_lock);
270 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
277 static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
278 unsigned int intid, bool level,
281 struct kvm_vcpu *vcpu;
282 struct vgic_irq *irq;
285 trace_vgic_update_irq_pending(cpuid, intid, level);
287 ret = vgic_lazy_init(kvm);
291 vcpu = kvm_get_vcpu(kvm, cpuid);
292 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
295 irq = vgic_get_irq(kvm, vcpu, intid);
299 if (irq->hw != mapped_irq) {
300 vgic_put_irq(kvm, irq);
304 spin_lock(&irq->irq_lock);
306 if (!vgic_validate_injection(irq, level)) {
307 /* Nothing to see here, move along... */
308 spin_unlock(&irq->irq_lock);
309 vgic_put_irq(kvm, irq);
313 if (irq->config == VGIC_CONFIG_LEVEL) {
314 irq->line_level = level;
315 irq->pending = level || irq->soft_pending;
320 vgic_queue_irq_unlock(kvm, irq);
321 vgic_put_irq(kvm, irq);
327 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
328 * @kvm: The VM structure pointer
329 * @cpuid: The CPU for PPIs
330 * @intid: The INTID to inject a new state to.
331 * @level: Edge-triggered: true: to trigger the interrupt
332 * false: to ignore the call
333 * Level-sensitive true: raise the input signal
334 * false: lower the input signal
336 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
337 * level-sensitive interrupts. You can think of the level parameter as 1
338 * being HIGH and 0 being LOW and all devices being active-HIGH.
340 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
343 return vgic_update_irq_pending(kvm, cpuid, intid, level, false);
346 int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int intid,
349 return vgic_update_irq_pending(kvm, cpuid, intid, level, true);
352 int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq)
354 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
358 spin_lock(&irq->irq_lock);
361 irq->hwintid = phys_irq;
363 spin_unlock(&irq->irq_lock);
364 vgic_put_irq(vcpu->kvm, irq);
369 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq)
371 struct vgic_irq *irq;
373 if (!vgic_initialized(vcpu->kvm))
376 irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
379 spin_lock(&irq->irq_lock);
384 spin_unlock(&irq->irq_lock);
385 vgic_put_irq(vcpu->kvm, irq);
391 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
393 * @vcpu: The VCPU pointer
395 * Go over the list of "interesting" interrupts, and prune those that we
396 * won't have to consider in the near future.
398 static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
400 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
401 struct vgic_irq *irq, *tmp;
404 spin_lock(&vgic_cpu->ap_list_lock);
406 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
407 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
409 spin_lock(&irq->irq_lock);
411 BUG_ON(vcpu != irq->vcpu);
413 target_vcpu = vgic_target_oracle(irq);
417 * We don't need to process this interrupt any
418 * further, move it off the list.
420 list_del(&irq->ap_list);
422 spin_unlock(&irq->irq_lock);
425 * This vgic_put_irq call matches the
426 * vgic_get_irq_kref in vgic_queue_irq_unlock,
427 * where we added the LPI to the ap_list. As
428 * we remove the irq from the list, we drop
429 * also drop the refcount.
431 vgic_put_irq(vcpu->kvm, irq);
435 if (target_vcpu == vcpu) {
436 /* We're on the right CPU */
437 spin_unlock(&irq->irq_lock);
441 /* This interrupt looks like it has to be migrated. */
443 spin_unlock(&irq->irq_lock);
444 spin_unlock(&vgic_cpu->ap_list_lock);
447 * Ensure locking order by always locking the smallest
450 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
458 spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
459 spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
460 SINGLE_DEPTH_NESTING);
461 spin_lock(&irq->irq_lock);
464 * If the affinity has been preserved, move the
465 * interrupt around. Otherwise, it means things have
466 * changed while the interrupt was unlocked, and we
467 * need to replay this.
469 * In all cases, we cannot trust the list not to have
470 * changed, so we restart from the beginning.
472 if (target_vcpu == vgic_target_oracle(irq)) {
473 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
475 list_del(&irq->ap_list);
476 irq->vcpu = target_vcpu;
477 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
480 spin_unlock(&irq->irq_lock);
481 spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
482 spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
486 spin_unlock(&vgic_cpu->ap_list_lock);
489 static inline void vgic_process_maintenance_interrupt(struct kvm_vcpu *vcpu)
491 if (kvm_vgic_global_state.type == VGIC_V2)
492 vgic_v2_process_maintenance(vcpu);
494 vgic_v3_process_maintenance(vcpu);
497 static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
499 if (kvm_vgic_global_state.type == VGIC_V2)
500 vgic_v2_fold_lr_state(vcpu);
502 vgic_v3_fold_lr_state(vcpu);
505 /* Requires the irq_lock to be held. */
506 static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
507 struct vgic_irq *irq, int lr)
509 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
511 if (kvm_vgic_global_state.type == VGIC_V2)
512 vgic_v2_populate_lr(vcpu, irq, lr);
514 vgic_v3_populate_lr(vcpu, irq, lr);
517 static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
519 if (kvm_vgic_global_state.type == VGIC_V2)
520 vgic_v2_clear_lr(vcpu, lr);
522 vgic_v3_clear_lr(vcpu, lr);
525 static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
527 if (kvm_vgic_global_state.type == VGIC_V2)
528 vgic_v2_set_underflow(vcpu);
530 vgic_v3_set_underflow(vcpu);
533 /* Requires the ap_list_lock to be held. */
534 static int compute_ap_list_depth(struct kvm_vcpu *vcpu)
536 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
537 struct vgic_irq *irq;
540 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
542 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
543 spin_lock(&irq->irq_lock);
544 /* GICv2 SGIs can count for more than one... */
545 if (vgic_irq_is_sgi(irq->intid) && irq->source)
546 count += hweight8(irq->source);
549 spin_unlock(&irq->irq_lock);
554 /* Requires the VCPU's ap_list_lock to be held. */
555 static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
557 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
558 struct vgic_irq *irq;
561 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
563 if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr) {
564 vgic_set_underflow(vcpu);
565 vgic_sort_ap_list(vcpu);
568 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
569 spin_lock(&irq->irq_lock);
571 if (unlikely(vgic_target_oracle(irq) != vcpu))
575 * If we get an SGI with multiple sources, try to get
576 * them in all at once.
579 vgic_populate_lr(vcpu, irq, count++);
580 } while (irq->source && count < kvm_vgic_global_state.nr_lr);
583 spin_unlock(&irq->irq_lock);
585 if (count == kvm_vgic_global_state.nr_lr)
589 vcpu->arch.vgic_cpu.used_lrs = count;
591 /* Nuke remaining LRs */
592 for ( ; count < kvm_vgic_global_state.nr_lr; count++)
593 vgic_clear_lr(vcpu, count);
596 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
597 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
599 vgic_process_maintenance_interrupt(vcpu);
600 vgic_fold_lr_state(vcpu);
601 vgic_prune_ap_list(vcpu);
604 /* Flush our emulation state into the GIC hardware before entering the guest. */
605 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
607 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
608 vgic_flush_lr_state(vcpu);
609 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
612 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
614 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
615 struct vgic_irq *irq;
616 bool pending = false;
618 if (!vcpu->kvm->arch.vgic.enabled)
621 spin_lock(&vgic_cpu->ap_list_lock);
623 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
624 spin_lock(&irq->irq_lock);
625 pending = irq->pending && irq->enabled;
626 spin_unlock(&irq->irq_lock);
632 spin_unlock(&vgic_cpu->ap_list_lock);
637 void vgic_kick_vcpus(struct kvm *kvm)
639 struct kvm_vcpu *vcpu;
643 * We've injected an interrupt, time to find out who deserves
646 kvm_for_each_vcpu(c, vcpu, kvm) {
647 if (kvm_vgic_vcpu_pending_irq(vcpu))
652 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)
654 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
657 spin_lock(&irq->irq_lock);
658 map_is_active = irq->hw && irq->active;
659 spin_unlock(&irq->irq_lock);
660 vgic_put_irq(vcpu->kvm, irq);
662 return map_is_active;