KVM: arm/arm64: vgic: Add refcounting for IRQs
[cascardo/linux.git] / virt / kvm / arm / vgic / vgic.c
1 /*
2  * Copyright (C) 2015, 2016 ARM Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16
17 #include <linux/kvm.h>
18 #include <linux/kvm_host.h>
19 #include <linux/list_sort.h>
20
21 #include "vgic.h"
22
23 #define CREATE_TRACE_POINTS
24 #include "../trace.h"
25
26 #ifdef CONFIG_DEBUG_SPINLOCK
27 #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
28 #else
29 #define DEBUG_SPINLOCK_BUG_ON(p)
30 #endif
31
32 struct vgic_global __section(.hyp.text) kvm_vgic_global_state;
33
34 /*
35  * Locking order is always:
36  *   vgic_cpu->ap_list_lock
37  *     vgic_irq->irq_lock
38  *
39  * (that is, always take the ap_list_lock before the struct vgic_irq lock).
40  *
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);
46  */
47
48 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
49                               u32 intid)
50 {
51         /* SGIs and PPIs */
52         if (intid <= VGIC_MAX_PRIVATE)
53                 return &vcpu->arch.vgic_cpu.private_irqs[intid];
54
55         /* SPIs */
56         if (intid <= VGIC_MAX_SPI)
57                 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
58
59         /* LPIs are not yet covered */
60         if (intid >= VGIC_MIN_LPI)
61                 return NULL;
62
63         WARN(1, "Looking up struct vgic_irq for reserved INTID");
64         return NULL;
65 }
66
67 static void vgic_get_irq_kref(struct vgic_irq *irq)
68 {
69         if (irq->intid < VGIC_MIN_LPI)
70                 return;
71
72         kref_get(&irq->refcount);
73 }
74
75 /* The refcount should never drop to 0 at the moment. */
76 static void vgic_irq_release(struct kref *ref)
77 {
78         WARN_ON(1);
79 }
80
81 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
82 {
83         if (irq->intid < VGIC_MIN_LPI)
84                 return;
85
86         kref_put(&irq->refcount, vgic_irq_release);
87 }
88
89 /**
90  * kvm_vgic_target_oracle - compute the target vcpu for an irq
91  *
92  * @irq:        The irq to route. Must be already locked.
93  *
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.
97  *
98  * Requires the IRQ lock to be held.
99  */
100 static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
101 {
102         DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
103
104         /* If the interrupt is active, it must stay on the current vcpu */
105         if (irq->active)
106                 return irq->vcpu ? : irq->target_vcpu;
107
108         /*
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
112          * forwarded.
113          */
114         if (irq->enabled && irq->pending) {
115                 if (unlikely(irq->target_vcpu &&
116                              !irq->target_vcpu->kvm->arch.vgic.enabled))
117                         return NULL;
118
119                 return irq->target_vcpu;
120         }
121
122         /* If neither active nor pending and enabled, then this IRQ should not
123          * be queued to any VCPU.
124          */
125         return NULL;
126 }
127
128 /*
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
131  * LRs.
132  *
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.
136  *
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.
139  *
140  * Return negative if "a" sorts before "b", 0 to preserve order, and positive
141  * to sort "b" before "a".
142  */
143 static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
144 {
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);
147         bool penda, pendb;
148         int ret;
149
150         spin_lock(&irqa->irq_lock);
151         spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
152
153         if (irqa->active || irqb->active) {
154                 ret = (int)irqb->active - (int)irqa->active;
155                 goto out;
156         }
157
158         penda = irqa->enabled && irqa->pending;
159         pendb = irqb->enabled && irqb->pending;
160
161         if (!penda || !pendb) {
162                 ret = (int)pendb - (int)penda;
163                 goto out;
164         }
165
166         /* Both pending and enabled, sort by priority */
167         ret = irqa->priority - irqb->priority;
168 out:
169         spin_unlock(&irqb->irq_lock);
170         spin_unlock(&irqa->irq_lock);
171         return ret;
172 }
173
174 /* Must be called with the ap_list_lock held */
175 static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
176 {
177         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
178
179         DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
180
181         list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
182 }
183
184 /*
185  * Only valid injection if changing level for level-triggered IRQs or for a
186  * rising edge.
187  */
188 static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
189 {
190         switch (irq->config) {
191         case VGIC_CONFIG_LEVEL:
192                 return irq->line_level != level;
193         case VGIC_CONFIG_EDGE:
194                 return level;
195         }
196
197         return false;
198 }
199
200 /*
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.
204  *
205  * Needs to be entered with the IRQ lock already held, but will return
206  * with all locks dropped.
207  */
208 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq)
209 {
210         struct kvm_vcpu *vcpu;
211
212         DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
213
214 retry:
215         vcpu = vgic_target_oracle(irq);
216         if (irq->vcpu || !vcpu) {
217                 /*
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
220                  * us to do.
221                  *
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.
225                  */
226                 spin_unlock(&irq->irq_lock);
227                 return false;
228         }
229
230         /*
231          * We must unlock the irq lock to take the ap_list_lock where
232          * we are going to insert this new pending interrupt.
233          */
234         spin_unlock(&irq->irq_lock);
235
236         /* someone can do stuff here, which we re-check below */
237
238         spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
239         spin_lock(&irq->irq_lock);
240
241         /*
242          * Did something change behind our backs?
243          *
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.
249          *
250          * In both cases, drop the locks and retry.
251          */
252
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);
256
257                 spin_lock(&irq->irq_lock);
258                 goto retry;
259         }
260
261         /*
262          * Grab a reference to the irq to reflect the fact that it is
263          * now in the ap_list.
264          */
265         vgic_get_irq_kref(irq);
266         list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
267         irq->vcpu = vcpu;
268
269         spin_unlock(&irq->irq_lock);
270         spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
271
272         kvm_vcpu_kick(vcpu);
273
274         return true;
275 }
276
277 static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
278                                    unsigned int intid, bool level,
279                                    bool mapped_irq)
280 {
281         struct kvm_vcpu *vcpu;
282         struct vgic_irq *irq;
283         int ret;
284
285         trace_vgic_update_irq_pending(cpuid, intid, level);
286
287         ret = vgic_lazy_init(kvm);
288         if (ret)
289                 return ret;
290
291         vcpu = kvm_get_vcpu(kvm, cpuid);
292         if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
293                 return -EINVAL;
294
295         irq = vgic_get_irq(kvm, vcpu, intid);
296         if (!irq)
297                 return -EINVAL;
298
299         if (irq->hw != mapped_irq) {
300                 vgic_put_irq(kvm, irq);
301                 return -EINVAL;
302         }
303
304         spin_lock(&irq->irq_lock);
305
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);
310                 return 0;
311         }
312
313         if (irq->config == VGIC_CONFIG_LEVEL) {
314                 irq->line_level = level;
315                 irq->pending = level || irq->soft_pending;
316         } else {
317                 irq->pending = true;
318         }
319
320         vgic_queue_irq_unlock(kvm, irq);
321         vgic_put_irq(kvm, irq);
322
323         return 0;
324 }
325
326 /**
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
335  *
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.
339  */
340 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
341                         bool level)
342 {
343         return vgic_update_irq_pending(kvm, cpuid, intid, level, false);
344 }
345
346 int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int intid,
347                                bool level)
348 {
349         return vgic_update_irq_pending(kvm, cpuid, intid, level, true);
350 }
351
352 int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq)
353 {
354         struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
355
356         BUG_ON(!irq);
357
358         spin_lock(&irq->irq_lock);
359
360         irq->hw = true;
361         irq->hwintid = phys_irq;
362
363         spin_unlock(&irq->irq_lock);
364         vgic_put_irq(vcpu->kvm, irq);
365
366         return 0;
367 }
368
369 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq)
370 {
371         struct vgic_irq *irq;
372
373         if (!vgic_initialized(vcpu->kvm))
374                 return -EAGAIN;
375
376         irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
377         BUG_ON(!irq);
378
379         spin_lock(&irq->irq_lock);
380
381         irq->hw = false;
382         irq->hwintid = 0;
383
384         spin_unlock(&irq->irq_lock);
385         vgic_put_irq(vcpu->kvm, irq);
386
387         return 0;
388 }
389
390 /**
391  * vgic_prune_ap_list - Remove non-relevant interrupts from the list
392  *
393  * @vcpu: The VCPU pointer
394  *
395  * Go over the list of "interesting" interrupts, and prune those that we
396  * won't have to consider in the near future.
397  */
398 static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
399 {
400         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
401         struct vgic_irq *irq, *tmp;
402
403 retry:
404         spin_lock(&vgic_cpu->ap_list_lock);
405
406         list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
407                 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
408
409                 spin_lock(&irq->irq_lock);
410
411                 BUG_ON(vcpu != irq->vcpu);
412
413                 target_vcpu = vgic_target_oracle(irq);
414
415                 if (!target_vcpu) {
416                         /*
417                          * We don't need to process this interrupt any
418                          * further, move it off the list.
419                          */
420                         list_del(&irq->ap_list);
421                         irq->vcpu = NULL;
422                         spin_unlock(&irq->irq_lock);
423
424                         /*
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.
430                          */
431                         vgic_put_irq(vcpu->kvm, irq);
432                         continue;
433                 }
434
435                 if (target_vcpu == vcpu) {
436                         /* We're on the right CPU */
437                         spin_unlock(&irq->irq_lock);
438                         continue;
439                 }
440
441                 /* This interrupt looks like it has to be migrated. */
442
443                 spin_unlock(&irq->irq_lock);
444                 spin_unlock(&vgic_cpu->ap_list_lock);
445
446                 /*
447                  * Ensure locking order by always locking the smallest
448                  * ID first.
449                  */
450                 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
451                         vcpuA = vcpu;
452                         vcpuB = target_vcpu;
453                 } else {
454                         vcpuA = target_vcpu;
455                         vcpuB = vcpu;
456                 }
457
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);
462
463                 /*
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.
468                  *
469                  * In all cases, we cannot trust the list not to have
470                  * changed, so we restart from the beginning.
471                  */
472                 if (target_vcpu == vgic_target_oracle(irq)) {
473                         struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
474
475                         list_del(&irq->ap_list);
476                         irq->vcpu = target_vcpu;
477                         list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
478                 }
479
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);
483                 goto retry;
484         }
485
486         spin_unlock(&vgic_cpu->ap_list_lock);
487 }
488
489 static inline void vgic_process_maintenance_interrupt(struct kvm_vcpu *vcpu)
490 {
491         if (kvm_vgic_global_state.type == VGIC_V2)
492                 vgic_v2_process_maintenance(vcpu);
493         else
494                 vgic_v3_process_maintenance(vcpu);
495 }
496
497 static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
498 {
499         if (kvm_vgic_global_state.type == VGIC_V2)
500                 vgic_v2_fold_lr_state(vcpu);
501         else
502                 vgic_v3_fold_lr_state(vcpu);
503 }
504
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)
508 {
509         DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
510
511         if (kvm_vgic_global_state.type == VGIC_V2)
512                 vgic_v2_populate_lr(vcpu, irq, lr);
513         else
514                 vgic_v3_populate_lr(vcpu, irq, lr);
515 }
516
517 static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
518 {
519         if (kvm_vgic_global_state.type == VGIC_V2)
520                 vgic_v2_clear_lr(vcpu, lr);
521         else
522                 vgic_v3_clear_lr(vcpu, lr);
523 }
524
525 static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
526 {
527         if (kvm_vgic_global_state.type == VGIC_V2)
528                 vgic_v2_set_underflow(vcpu);
529         else
530                 vgic_v3_set_underflow(vcpu);
531 }
532
533 /* Requires the ap_list_lock to be held. */
534 static int compute_ap_list_depth(struct kvm_vcpu *vcpu)
535 {
536         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
537         struct vgic_irq *irq;
538         int count = 0;
539
540         DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
541
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);
547                 else
548                         count++;
549                 spin_unlock(&irq->irq_lock);
550         }
551         return count;
552 }
553
554 /* Requires the VCPU's ap_list_lock to be held. */
555 static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
556 {
557         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
558         struct vgic_irq *irq;
559         int count = 0;
560
561         DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
562
563         if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr) {
564                 vgic_set_underflow(vcpu);
565                 vgic_sort_ap_list(vcpu);
566         }
567
568         list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
569                 spin_lock(&irq->irq_lock);
570
571                 if (unlikely(vgic_target_oracle(irq) != vcpu))
572                         goto next;
573
574                 /*
575                  * If we get an SGI with multiple sources, try to get
576                  * them in all at once.
577                  */
578                 do {
579                         vgic_populate_lr(vcpu, irq, count++);
580                 } while (irq->source && count < kvm_vgic_global_state.nr_lr);
581
582 next:
583                 spin_unlock(&irq->irq_lock);
584
585                 if (count == kvm_vgic_global_state.nr_lr)
586                         break;
587         }
588
589         vcpu->arch.vgic_cpu.used_lrs = count;
590
591         /* Nuke remaining LRs */
592         for ( ; count < kvm_vgic_global_state.nr_lr; count++)
593                 vgic_clear_lr(vcpu, count);
594 }
595
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)
598 {
599         vgic_process_maintenance_interrupt(vcpu);
600         vgic_fold_lr_state(vcpu);
601         vgic_prune_ap_list(vcpu);
602 }
603
604 /* Flush our emulation state into the GIC hardware before entering the guest. */
605 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
606 {
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);
610 }
611
612 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
613 {
614         struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
615         struct vgic_irq *irq;
616         bool pending = false;
617
618         if (!vcpu->kvm->arch.vgic.enabled)
619                 return false;
620
621         spin_lock(&vgic_cpu->ap_list_lock);
622
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);
627
628                 if (pending)
629                         break;
630         }
631
632         spin_unlock(&vgic_cpu->ap_list_lock);
633
634         return pending;
635 }
636
637 void vgic_kick_vcpus(struct kvm *kvm)
638 {
639         struct kvm_vcpu *vcpu;
640         int c;
641
642         /*
643          * We've injected an interrupt, time to find out who deserves
644          * a good kick...
645          */
646         kvm_for_each_vcpu(c, vcpu, kvm) {
647                 if (kvm_vgic_vcpu_pending_irq(vcpu))
648                         kvm_vcpu_kick(vcpu);
649         }
650 }
651
652 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)
653 {
654         struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
655         bool map_is_active;
656
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);
661
662         return map_is_active;
663 }