]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - virt/kvm/arm/vgic/vgic.c
96a498871fa0fb19bbbe84b4439c04e8d9642105
[mirror_ubuntu-bionic-kernel.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 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22
23 #include "vgic.h"
24
25 #define CREATE_TRACE_POINTS
26 #include "trace.h"
27
28 #ifdef CONFIG_DEBUG_SPINLOCK
29 #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
30 #else
31 #define DEBUG_SPINLOCK_BUG_ON(p)
32 #endif
33
34 struct vgic_global kvm_vgic_global_state __ro_after_init = {
35 .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
36 };
37
38 /*
39 * Locking order is always:
40 * kvm->lock (mutex)
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
46 *
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
49 * than it.
50 *
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.
56 *
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);
62 *
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.
66 */
67
68 /*
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.
71 */
72 static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
73 {
74 struct vgic_dist *dist = &kvm->arch.vgic;
75 struct vgic_irq *irq = NULL;
76 unsigned long flags;
77
78 spin_lock_irqsave(&dist->lpi_list_lock, flags);
79
80 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
81 if (irq->intid != intid)
82 continue;
83
84 /*
85 * This increases the refcount, the caller is expected to
86 * call vgic_put_irq() later once it's finished with the IRQ.
87 */
88 vgic_get_irq_kref(irq);
89 goto out_unlock;
90 }
91 irq = NULL;
92
93 out_unlock:
94 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
95
96 return irq;
97 }
98
99 /*
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.
103 */
104 struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
105 u32 intid)
106 {
107 /* SGIs and PPIs */
108 if (intid <= VGIC_MAX_PRIVATE)
109 return &vcpu->arch.vgic_cpu.private_irqs[intid];
110
111 /* SPIs */
112 if (intid <= VGIC_MAX_SPI)
113 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
114
115 /* LPIs */
116 if (intid >= VGIC_MIN_LPI)
117 return vgic_get_lpi(kvm, intid);
118
119 WARN(1, "Looking up struct vgic_irq for reserved INTID");
120 return NULL;
121 }
122
123 /*
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.
127 */
128 static void vgic_irq_release(struct kref *ref)
129 {
130 }
131
132 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
133 {
134 struct vgic_dist *dist = &kvm->arch.vgic;
135 unsigned long flags;
136
137 if (irq->intid < VGIC_MIN_LPI)
138 return;
139
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);
143 return;
144 };
145
146 list_del(&irq->lpi_list);
147 dist->lpi_list_count--;
148 spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
149
150 kfree(irq);
151 }
152
153 /**
154 * kvm_vgic_target_oracle - compute the target vcpu for an irq
155 *
156 * @irq: The irq to route. Must be already locked.
157 *
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.
161 *
162 * Requires the IRQ lock to be held.
163 */
164 static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
165 {
166 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
167
168 /* If the interrupt is active, it must stay on the current vcpu */
169 if (irq->active)
170 return irq->vcpu ? : irq->target_vcpu;
171
172 /*
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
176 * forwarded.
177 */
178 if (irq->enabled && irq_is_pending(irq)) {
179 if (unlikely(irq->target_vcpu &&
180 !irq->target_vcpu->kvm->arch.vgic.enabled))
181 return NULL;
182
183 return irq->target_vcpu;
184 }
185
186 /* If neither active nor pending and enabled, then this IRQ should not
187 * be queued to any VCPU.
188 */
189 return NULL;
190 }
191
192 /*
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
195 * LRs.
196 *
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.
200 *
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.
203 *
204 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
205 * to sort "b" before "a".
206 */
207 static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
208 {
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);
211 bool penda, pendb;
212 int ret;
213
214 spin_lock(&irqa->irq_lock);
215 spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
216
217 if (irqa->active || irqb->active) {
218 ret = (int)irqb->active - (int)irqa->active;
219 goto out;
220 }
221
222 penda = irqa->enabled && irq_is_pending(irqa);
223 pendb = irqb->enabled && irq_is_pending(irqb);
224
225 if (!penda || !pendb) {
226 ret = (int)pendb - (int)penda;
227 goto out;
228 }
229
230 /* Both pending and enabled, sort by priority */
231 ret = irqa->priority - irqb->priority;
232 out:
233 spin_unlock(&irqb->irq_lock);
234 spin_unlock(&irqa->irq_lock);
235 return ret;
236 }
237
238 /* Must be called with the ap_list_lock held */
239 static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
240 {
241 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
242
243 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
244
245 list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
246 }
247
248 /*
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
251 * their owner.
252 */
253 static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
254 {
255 if (irq->owner != owner)
256 return false;
257
258 switch (irq->config) {
259 case VGIC_CONFIG_LEVEL:
260 return irq->line_level != level;
261 case VGIC_CONFIG_EDGE:
262 return level;
263 }
264
265 return false;
266 }
267
268 /*
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.
272 *
273 * Needs to be entered with the IRQ lock already held, but will return
274 * with all locks dropped.
275 */
276 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
277 unsigned long flags)
278 {
279 struct kvm_vcpu *vcpu;
280
281 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
282
283 retry:
284 vcpu = vgic_target_oracle(irq);
285 if (irq->vcpu || !vcpu) {
286 /*
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
289 * us to do.
290 *
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.
294 */
295 spin_unlock_irqrestore(&irq->irq_lock, flags);
296
297 /*
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
304 * reason.
305 */
306 if (vcpu) {
307 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
308 kvm_vcpu_kick(vcpu);
309 }
310 return false;
311 }
312
313 /*
314 * We must unlock the irq lock to take the ap_list_lock where
315 * we are going to insert this new pending interrupt.
316 */
317 spin_unlock_irqrestore(&irq->irq_lock, flags);
318
319 /* someone can do stuff here, which we re-check below */
320
321 spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
322 spin_lock(&irq->irq_lock);
323
324 /*
325 * Did something change behind our backs?
326 *
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.
332 *
333 * In both cases, drop the locks and retry.
334 */
335
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);
339
340 spin_lock_irqsave(&irq->irq_lock, flags);
341 goto retry;
342 }
343
344 /*
345 * Grab a reference to the irq to reflect the fact that it is
346 * now in the ap_list.
347 */
348 vgic_get_irq_kref(irq);
349 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
350 irq->vcpu = vcpu;
351
352 spin_unlock(&irq->irq_lock);
353 spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
354
355 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
356 kvm_vcpu_kick(vcpu);
357
358 return true;
359 }
360
361 /**
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.
373 *
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.
377 */
378 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
379 bool level, void *owner)
380 {
381 struct kvm_vcpu *vcpu;
382 struct vgic_irq *irq;
383 unsigned long flags;
384 int ret;
385
386 trace_vgic_update_irq_pending(cpuid, intid, level);
387
388 ret = vgic_lazy_init(kvm);
389 if (ret)
390 return ret;
391
392 vcpu = kvm_get_vcpu(kvm, cpuid);
393 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
394 return -EINVAL;
395
396 irq = vgic_get_irq(kvm, vcpu, intid);
397 if (!irq)
398 return -EINVAL;
399
400 spin_lock_irqsave(&irq->irq_lock, flags);
401
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);
406 return 0;
407 }
408
409 if (irq->config == VGIC_CONFIG_LEVEL)
410 irq->line_level = level;
411 else
412 irq->pending_latch = true;
413
414 vgic_queue_irq_unlock(kvm, irq, flags);
415 vgic_put_irq(kvm, irq);
416
417 return 0;
418 }
419
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)
423 {
424 struct irq_desc *desc;
425 struct irq_data *data;
426
427 /*
428 * Find the physical IRQ number corresponding to @host_irq
429 */
430 desc = irq_to_desc(host_irq);
431 if (!desc) {
432 kvm_err("%s: no interrupt descriptor\n", __func__);
433 return -EINVAL;
434 }
435 data = irq_desc_get_irq_data(desc);
436 while (data->parent_data)
437 data = data->parent_data;
438
439 irq->hw = true;
440 irq->host_irq = host_irq;
441 irq->hwintid = data->hwirq;
442 return 0;
443 }
444
445 /* @irq->irq_lock must be held */
446 static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
447 {
448 irq->hw = false;
449 irq->hwintid = 0;
450 }
451
452 int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
453 u32 vintid)
454 {
455 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
456 unsigned long flags;
457 int ret;
458
459 BUG_ON(!irq);
460
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);
465
466 return ret;
467 }
468
469 /**
470 * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
471 * @vcpu: The VCPU pointer
472 * @vintid: The INTID of the interrupt
473 *
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.
477 */
478 void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
479 {
480 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
481 unsigned long flags;
482
483 if (!irq->hw)
484 goto out;
485
486 spin_lock_irqsave(&irq->irq_lock, flags);
487 irq->active = false;
488 irq->pending_latch = false;
489 irq->line_level = false;
490 spin_unlock_irqrestore(&irq->irq_lock, flags);
491 out:
492 vgic_put_irq(vcpu->kvm, irq);
493 }
494
495 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
496 {
497 struct vgic_irq *irq;
498 unsigned long flags;
499
500 if (!vgic_initialized(vcpu->kvm))
501 return -EAGAIN;
502
503 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
504 BUG_ON(!irq);
505
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);
510
511 return 0;
512 }
513
514 /**
515 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
516 *
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
520 *
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.
523 */
524 int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
525 {
526 struct vgic_irq *irq;
527 unsigned long flags;
528 int ret = 0;
529
530 if (!vgic_initialized(vcpu->kvm))
531 return -EAGAIN;
532
533 /* SGIs and LPIs cannot be wired up to any device */
534 if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
535 return -EINVAL;
536
537 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
538 spin_lock_irqsave(&irq->irq_lock, flags);
539 if (irq->owner && irq->owner != owner)
540 ret = -EEXIST;
541 else
542 irq->owner = owner;
543 spin_unlock_irqrestore(&irq->irq_lock, flags);
544
545 return ret;
546 }
547
548 /**
549 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
550 *
551 * @vcpu: The VCPU pointer
552 *
553 * Go over the list of "interesting" interrupts, and prune those that we
554 * won't have to consider in the near future.
555 */
556 static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
557 {
558 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
559 struct vgic_irq *irq, *tmp;
560 unsigned long flags;
561
562 retry:
563 spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
564
565 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
566 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
567 bool target_vcpu_needs_kick = false;
568
569 spin_lock(&irq->irq_lock);
570
571 BUG_ON(vcpu != irq->vcpu);
572
573 target_vcpu = vgic_target_oracle(irq);
574
575 if (!target_vcpu) {
576 /*
577 * We don't need to process this interrupt any
578 * further, move it off the list.
579 */
580 list_del(&irq->ap_list);
581 irq->vcpu = NULL;
582 spin_unlock(&irq->irq_lock);
583
584 /*
585 * This vgic_put_irq call matches the
586 * vgic_get_irq_kref in vgic_queue_irq_unlock,
587 * where we added the LPI to the ap_list. As
588 * we remove the irq from the list, we drop
589 * also drop the refcount.
590 */
591 vgic_put_irq(vcpu->kvm, irq);
592 continue;
593 }
594
595 if (target_vcpu == vcpu) {
596 /* We're on the right CPU */
597 spin_unlock(&irq->irq_lock);
598 continue;
599 }
600
601 /* This interrupt looks like it has to be migrated. */
602
603 spin_unlock(&irq->irq_lock);
604 spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
605
606 /*
607 * Ensure locking order by always locking the smallest
608 * ID first.
609 */
610 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
611 vcpuA = vcpu;
612 vcpuB = target_vcpu;
613 } else {
614 vcpuA = target_vcpu;
615 vcpuB = vcpu;
616 }
617
618 spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
619 spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
620 SINGLE_DEPTH_NESTING);
621 spin_lock(&irq->irq_lock);
622
623 /*
624 * If the affinity has been preserved, move the
625 * interrupt around. Otherwise, it means things have
626 * changed while the interrupt was unlocked, and we
627 * need to replay this.
628 *
629 * In all cases, we cannot trust the list not to have
630 * changed, so we restart from the beginning.
631 */
632 if (target_vcpu == vgic_target_oracle(irq)) {
633 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
634
635 list_del(&irq->ap_list);
636 irq->vcpu = target_vcpu;
637 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
638 target_vcpu_needs_kick = true;
639 }
640
641 spin_unlock(&irq->irq_lock);
642 spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
643 spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
644
645 if (target_vcpu_needs_kick) {
646 kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
647 kvm_vcpu_kick(target_vcpu);
648 }
649
650 goto retry;
651 }
652
653 spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
654 }
655
656 static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
657 {
658 if (kvm_vgic_global_state.type == VGIC_V2)
659 vgic_v2_fold_lr_state(vcpu);
660 else
661 vgic_v3_fold_lr_state(vcpu);
662 }
663
664 /* Requires the irq_lock to be held. */
665 static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
666 struct vgic_irq *irq, int lr)
667 {
668 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
669
670 if (kvm_vgic_global_state.type == VGIC_V2)
671 vgic_v2_populate_lr(vcpu, irq, lr);
672 else
673 vgic_v3_populate_lr(vcpu, irq, lr);
674 }
675
676 static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
677 {
678 if (kvm_vgic_global_state.type == VGIC_V2)
679 vgic_v2_clear_lr(vcpu, lr);
680 else
681 vgic_v3_clear_lr(vcpu, lr);
682 }
683
684 static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
685 {
686 if (kvm_vgic_global_state.type == VGIC_V2)
687 vgic_v2_set_underflow(vcpu);
688 else
689 vgic_v3_set_underflow(vcpu);
690 }
691
692 static inline void vgic_set_npie(struct kvm_vcpu *vcpu)
693 {
694 if (kvm_vgic_global_state.type == VGIC_V2)
695 vgic_v2_set_npie(vcpu);
696 else
697 vgic_v3_set_npie(vcpu);
698 }
699
700 /* Requires the ap_list_lock to be held. */
701 static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
702 bool *multi_sgi)
703 {
704 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
705 struct vgic_irq *irq;
706 int count = 0;
707
708 *multi_sgi = false;
709
710 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
711
712 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
713 spin_lock(&irq->irq_lock);
714 /* GICv2 SGIs can count for more than one... */
715 if (vgic_irq_is_sgi(irq->intid) && irq->source) {
716 int w = hweight8(irq->source);
717
718 count += w;
719 *multi_sgi |= (w > 1);
720 } else {
721 count++;
722 }
723 spin_unlock(&irq->irq_lock);
724 }
725 return count;
726 }
727
728 /* Requires the VCPU's ap_list_lock to be held. */
729 static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
730 {
731 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
732 struct vgic_irq *irq;
733 int count;
734 bool npie = false;
735 bool multi_sgi;
736 u8 prio = 0xff;
737
738 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
739
740 count = compute_ap_list_depth(vcpu, &multi_sgi);
741 if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
742 vgic_sort_ap_list(vcpu);
743
744 count = 0;
745
746 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
747 spin_lock(&irq->irq_lock);
748
749 /*
750 * If we have multi-SGIs in the pipeline, we need to
751 * guarantee that they are all seen before any IRQ of
752 * lower priority. In that case, we need to filter out
753 * these interrupts by exiting early. This is easy as
754 * the AP list has been sorted already.
755 */
756 if (multi_sgi && irq->priority > prio) {
757 spin_unlock(&irq->irq_lock);
758 break;
759 }
760
761 if (likely(vgic_target_oracle(irq) == vcpu)) {
762 vgic_populate_lr(vcpu, irq, count++);
763
764 if (irq->source) {
765 npie = true;
766 prio = irq->priority;
767 }
768 }
769
770 spin_unlock(&irq->irq_lock);
771
772 if (count == kvm_vgic_global_state.nr_lr) {
773 if (!list_is_last(&irq->ap_list,
774 &vgic_cpu->ap_list_head))
775 vgic_set_underflow(vcpu);
776 break;
777 }
778 }
779
780 if (npie)
781 vgic_set_npie(vcpu);
782
783 vcpu->arch.vgic_cpu.used_lrs = count;
784
785 /* Nuke remaining LRs */
786 for ( ; count < kvm_vgic_global_state.nr_lr; count++)
787 vgic_clear_lr(vcpu, count);
788 }
789
790 /* Sync back the hardware VGIC state into our emulation after a guest's run. */
791 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
792 {
793 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
794
795 WARN_ON(vgic_v4_sync_hwstate(vcpu));
796
797 /* An empty ap_list_head implies used_lrs == 0 */
798 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
799 return;
800
801 if (vgic_cpu->used_lrs)
802 vgic_fold_lr_state(vcpu);
803 vgic_prune_ap_list(vcpu);
804 }
805
806 /* Flush our emulation state into the GIC hardware before entering the guest. */
807 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
808 {
809 WARN_ON(vgic_v4_flush_hwstate(vcpu));
810
811 /*
812 * If there are no virtual interrupts active or pending for this
813 * VCPU, then there is no work to do and we can bail out without
814 * taking any lock. There is a potential race with someone injecting
815 * interrupts to the VCPU, but it is a benign race as the VCPU will
816 * either observe the new interrupt before or after doing this check,
817 * and introducing additional synchronization mechanism doesn't change
818 * this.
819 */
820 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
821 return;
822
823 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
824
825 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
826 vgic_flush_lr_state(vcpu);
827 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
828 }
829
830 void kvm_vgic_load(struct kvm_vcpu *vcpu)
831 {
832 if (unlikely(!vgic_initialized(vcpu->kvm)))
833 return;
834
835 if (kvm_vgic_global_state.type == VGIC_V2)
836 vgic_v2_load(vcpu);
837 else
838 vgic_v3_load(vcpu);
839 }
840
841 void kvm_vgic_put(struct kvm_vcpu *vcpu)
842 {
843 if (unlikely(!vgic_initialized(vcpu->kvm)))
844 return;
845
846 if (kvm_vgic_global_state.type == VGIC_V2)
847 vgic_v2_put(vcpu);
848 else
849 vgic_v3_put(vcpu);
850 }
851
852 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
853 {
854 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
855 struct vgic_irq *irq;
856 bool pending = false;
857 unsigned long flags;
858
859 if (!vcpu->kvm->arch.vgic.enabled)
860 return false;
861
862 if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
863 return true;
864
865 spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
866
867 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
868 spin_lock(&irq->irq_lock);
869 pending = irq_is_pending(irq) && irq->enabled;
870 spin_unlock(&irq->irq_lock);
871
872 if (pending)
873 break;
874 }
875
876 spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
877
878 return pending;
879 }
880
881 void vgic_kick_vcpus(struct kvm *kvm)
882 {
883 struct kvm_vcpu *vcpu;
884 int c;
885
886 /*
887 * We've injected an interrupt, time to find out who deserves
888 * a good kick...
889 */
890 kvm_for_each_vcpu(c, vcpu, kvm) {
891 if (kvm_vgic_vcpu_pending_irq(vcpu)) {
892 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
893 kvm_vcpu_kick(vcpu);
894 }
895 }
896 }
897
898 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
899 {
900 struct vgic_irq *irq;
901 bool map_is_active;
902 unsigned long flags;
903
904 if (!vgic_initialized(vcpu->kvm))
905 return false;
906
907 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
908 spin_lock_irqsave(&irq->irq_lock, flags);
909 map_is_active = irq->hw && irq->active;
910 spin_unlock_irqrestore(&irq->irq_lock, flags);
911 vgic_put_irq(vcpu->kvm, irq);
912
913 return map_is_active;
914 }
915