blob: 9628945234e46aac045c00fcd0b2531a106760c8 [file] [log] [blame]
Christoffer Dall64a959d2015-11-24 16:51:12 +01001/*
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>
Christoffer Dall8e444742015-11-25 10:02:16 -080019#include <linux/list_sort.h>
Christoffer Dall64a959d2015-11-24 16:51:12 +010020
21#include "vgic.h"
22
Christoffer Dall81eeb952015-11-25 10:02:16 -080023#define CREATE_TRACE_POINTS
Christoffer Dall35d2d5d2017-05-04 13:54:17 +020024#include "trace.h"
Christoffer Dall81eeb952015-11-25 10:02:16 -080025
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
Ard Biesheuvel63d7c6a2017-03-09 21:51:59 +010032struct vgic_global kvm_vgic_global_state __ro_after_init = {
33 .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
34};
Christoffer Dall64a959d2015-11-24 16:51:12 +010035
Christoffer Dall81eeb952015-11-25 10:02:16 -080036/*
37 * Locking order is always:
Christoffer Dallabd72292017-05-06 20:01:24 +020038 * kvm->lock (mutex)
39 * its->cmd_lock (mutex)
40 * its->its_lock (mutex)
41 * vgic_cpu->ap_list_lock
42 * kvm->lpi_list_lock
43 * vgic_irq->irq_lock
Christoffer Dall81eeb952015-11-25 10:02:16 -080044 *
Andre Przywara424c3382016-07-15 12:43:32 +010045 * If you need to take multiple locks, always take the upper lock first,
46 * then the lower ones, e.g. first take the its_lock, then the irq_lock.
47 * If you are already holding a lock and need to take a higher one, you
48 * have to drop the lower ranking lock first and re-aquire it after having
49 * taken the upper one.
Christoffer Dall81eeb952015-11-25 10:02:16 -080050 *
51 * When taking more than one ap_list_lock at the same time, always take the
52 * lowest numbered VCPU's ap_list_lock first, so:
53 * vcpuX->vcpu_id < vcpuY->vcpu_id:
54 * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
55 * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
56 */
57
Andre Przywara38024112016-07-15 12:43:33 +010058/*
59 * Iterate over the VM's list of mapped LPIs to find the one with a
60 * matching interrupt ID and return a reference to the IRQ structure.
61 */
62static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
63{
64 struct vgic_dist *dist = &kvm->arch.vgic;
65 struct vgic_irq *irq = NULL;
66
67 spin_lock(&dist->lpi_list_lock);
68
69 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
70 if (irq->intid != intid)
71 continue;
72
73 /*
74 * This increases the refcount, the caller is expected to
75 * call vgic_put_irq() later once it's finished with the IRQ.
76 */
Marc Zyngierd97594e2016-07-17 11:27:23 +010077 vgic_get_irq_kref(irq);
Andre Przywara38024112016-07-15 12:43:33 +010078 goto out_unlock;
79 }
80 irq = NULL;
81
82out_unlock:
83 spin_unlock(&dist->lpi_list_lock);
84
85 return irq;
86}
87
88/*
89 * This looks up the virtual interrupt ID to get the corresponding
90 * struct vgic_irq. It also increases the refcount, so any caller is expected
91 * to call vgic_put_irq() once it's finished with this IRQ.
92 */
Christoffer Dall64a959d2015-11-24 16:51:12 +010093struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
94 u32 intid)
95{
96 /* SGIs and PPIs */
97 if (intid <= VGIC_MAX_PRIVATE)
98 return &vcpu->arch.vgic_cpu.private_irqs[intid];
99
100 /* SPIs */
101 if (intid <= VGIC_MAX_SPI)
102 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
103
Andre Przywara38024112016-07-15 12:43:33 +0100104 /* LPIs */
Christoffer Dall64a959d2015-11-24 16:51:12 +0100105 if (intid >= VGIC_MIN_LPI)
Andre Przywara38024112016-07-15 12:43:33 +0100106 return vgic_get_lpi(kvm, intid);
Christoffer Dall64a959d2015-11-24 16:51:12 +0100107
108 WARN(1, "Looking up struct vgic_irq for reserved INTID");
109 return NULL;
110}
Christoffer Dall81eeb952015-11-25 10:02:16 -0800111
Andre Przywara38024112016-07-15 12:43:33 +0100112/*
113 * We can't do anything in here, because we lack the kvm pointer to
114 * lock and remove the item from the lpi_list. So we keep this function
115 * empty and use the return value of kref_put() to trigger the freeing.
116 */
Andre Przywara5dd4b922016-07-15 12:43:27 +0100117static void vgic_irq_release(struct kref *ref)
118{
Andre Przywara5dd4b922016-07-15 12:43:27 +0100119}
120
121void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
122{
Christoffer Dall2cccbb32016-08-02 22:05:42 +0200123 struct vgic_dist *dist = &kvm->arch.vgic;
Andre Przywara38024112016-07-15 12:43:33 +0100124
Andre Przywara5dd4b922016-07-15 12:43:27 +0100125 if (irq->intid < VGIC_MIN_LPI)
126 return;
127
Andre Przywara38024112016-07-15 12:43:33 +0100128 spin_lock(&dist->lpi_list_lock);
Christoffer Dall2cccbb32016-08-02 22:05:42 +0200129 if (!kref_put(&irq->refcount, vgic_irq_release)) {
130 spin_unlock(&dist->lpi_list_lock);
131 return;
132 };
133
Andre Przywara38024112016-07-15 12:43:33 +0100134 list_del(&irq->lpi_list);
135 dist->lpi_list_count--;
136 spin_unlock(&dist->lpi_list_lock);
137
138 kfree(irq);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100139}
140
Christoffer Dall81eeb952015-11-25 10:02:16 -0800141/**
142 * kvm_vgic_target_oracle - compute the target vcpu for an irq
143 *
144 * @irq: The irq to route. Must be already locked.
145 *
146 * Based on the current state of the interrupt (enabled, pending,
147 * active, vcpu and target_vcpu), compute the next vcpu this should be
148 * given to. Return NULL if this shouldn't be injected at all.
149 *
150 * Requires the IRQ lock to be held.
151 */
152static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
153{
154 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
155
156 /* If the interrupt is active, it must stay on the current vcpu */
157 if (irq->active)
158 return irq->vcpu ? : irq->target_vcpu;
159
160 /*
161 * If the IRQ is not active but enabled and pending, we should direct
162 * it to its configured target VCPU.
163 * If the distributor is disabled, pending interrupts shouldn't be
164 * forwarded.
165 */
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100166 if (irq->enabled && irq_is_pending(irq)) {
Christoffer Dall81eeb952015-11-25 10:02:16 -0800167 if (unlikely(irq->target_vcpu &&
168 !irq->target_vcpu->kvm->arch.vgic.enabled))
169 return NULL;
170
171 return irq->target_vcpu;
172 }
173
174 /* If neither active nor pending and enabled, then this IRQ should not
175 * be queued to any VCPU.
176 */
177 return NULL;
178}
179
180/*
Christoffer Dall8e444742015-11-25 10:02:16 -0800181 * The order of items in the ap_lists defines how we'll pack things in LRs as
182 * well, the first items in the list being the first things populated in the
183 * LRs.
184 *
185 * A hard rule is that active interrupts can never be pushed out of the LRs
186 * (and therefore take priority) since we cannot reliably trap on deactivation
187 * of IRQs and therefore they have to be present in the LRs.
188 *
189 * Otherwise things should be sorted by the priority field and the GIC
190 * hardware support will take care of preemption of priority groups etc.
191 *
192 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
193 * to sort "b" before "a".
194 */
195static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
196{
197 struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
198 struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
199 bool penda, pendb;
200 int ret;
201
202 spin_lock(&irqa->irq_lock);
203 spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
204
205 if (irqa->active || irqb->active) {
206 ret = (int)irqb->active - (int)irqa->active;
207 goto out;
208 }
209
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100210 penda = irqa->enabled && irq_is_pending(irqa);
211 pendb = irqb->enabled && irq_is_pending(irqb);
Christoffer Dall8e444742015-11-25 10:02:16 -0800212
213 if (!penda || !pendb) {
214 ret = (int)pendb - (int)penda;
215 goto out;
216 }
217
218 /* Both pending and enabled, sort by priority */
219 ret = irqa->priority - irqb->priority;
220out:
221 spin_unlock(&irqb->irq_lock);
222 spin_unlock(&irqa->irq_lock);
223 return ret;
224}
225
226/* Must be called with the ap_list_lock held */
227static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
228{
229 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
230
231 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
232
233 list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
234}
235
236/*
Christoffer Dall81eeb952015-11-25 10:02:16 -0800237 * Only valid injection if changing level for level-triggered IRQs or for a
238 * rising edge.
239 */
240static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
241{
242 switch (irq->config) {
243 case VGIC_CONFIG_LEVEL:
244 return irq->line_level != level;
245 case VGIC_CONFIG_EDGE:
246 return level;
247 }
248
249 return false;
250}
251
252/*
253 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
254 * Do the queuing if necessary, taking the right locks in the right order.
255 * Returns true when the IRQ was queued, false otherwise.
256 *
257 * Needs to be entered with the IRQ lock already held, but will return
258 * with all locks dropped.
259 */
260bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq)
261{
262 struct kvm_vcpu *vcpu;
263
264 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
265
266retry:
267 vcpu = vgic_target_oracle(irq);
268 if (irq->vcpu || !vcpu) {
269 /*
270 * If this IRQ is already on a VCPU's ap_list, then it
271 * cannot be moved or modified and there is no more work for
272 * us to do.
273 *
274 * Otherwise, if the irq is not pending and enabled, it does
275 * not need to be inserted into an ap_list and there is also
276 * no more work for us to do.
277 */
278 spin_unlock(&irq->irq_lock);
Shih-Wei Lid42c7972016-10-27 15:08:13 +0000279
280 /*
281 * We have to kick the VCPU here, because we could be
282 * queueing an edge-triggered interrupt for which we
283 * get no EOI maintenance interrupt. In that case,
284 * while the IRQ is already on the VCPU's AP list, the
285 * VCPU could have EOI'ed the original interrupt and
286 * won't see this one until it exits for some other
287 * reason.
288 */
Andrew Jones325f9c62017-06-04 14:43:59 +0200289 if (vcpu) {
290 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Shih-Wei Lid42c7972016-10-27 15:08:13 +0000291 kvm_vcpu_kick(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +0200292 }
Christoffer Dall81eeb952015-11-25 10:02:16 -0800293 return false;
294 }
295
296 /*
297 * We must unlock the irq lock to take the ap_list_lock where
298 * we are going to insert this new pending interrupt.
299 */
300 spin_unlock(&irq->irq_lock);
301
302 /* someone can do stuff here, which we re-check below */
303
304 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
305 spin_lock(&irq->irq_lock);
306
307 /*
308 * Did something change behind our backs?
309 *
310 * There are two cases:
311 * 1) The irq lost its pending state or was disabled behind our
312 * backs and/or it was queued to another VCPU's ap_list.
313 * 2) Someone changed the affinity on this irq behind our
314 * backs and we are now holding the wrong ap_list_lock.
315 *
316 * In both cases, drop the locks and retry.
317 */
318
319 if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
320 spin_unlock(&irq->irq_lock);
321 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
322
323 spin_lock(&irq->irq_lock);
324 goto retry;
325 }
326
Andre Przywara5dd4b922016-07-15 12:43:27 +0100327 /*
328 * Grab a reference to the irq to reflect the fact that it is
329 * now in the ap_list.
330 */
331 vgic_get_irq_kref(irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800332 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
333 irq->vcpu = vcpu;
334
335 spin_unlock(&irq->irq_lock);
336 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
337
Andrew Jones325f9c62017-06-04 14:43:59 +0200338 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800339 kvm_vcpu_kick(vcpu);
340
341 return true;
342}
343
Christoffer Dall11710de2017-02-01 11:03:45 +0100344/**
345 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
346 * @kvm: The VM structure pointer
347 * @cpuid: The CPU for PPIs
348 * @intid: The INTID to inject a new state to.
349 * @level: Edge-triggered: true: to trigger the interrupt
350 * false: to ignore the call
351 * Level-sensitive true: raise the input signal
352 * false: lower the input signal
353 *
354 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
355 * level-sensitive interrupts. You can think of the level parameter as 1
356 * being HIGH and 0 being LOW and all devices being active-HIGH.
357 */
358int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
359 bool level)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800360{
361 struct kvm_vcpu *vcpu;
362 struct vgic_irq *irq;
363 int ret;
364
365 trace_vgic_update_irq_pending(cpuid, intid, level);
366
Eric Augerad275b8b2015-12-21 18:09:38 +0100367 ret = vgic_lazy_init(kvm);
368 if (ret)
369 return ret;
370
Christoffer Dall81eeb952015-11-25 10:02:16 -0800371 vcpu = kvm_get_vcpu(kvm, cpuid);
372 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
373 return -EINVAL;
374
375 irq = vgic_get_irq(kvm, vcpu, intid);
376 if (!irq)
377 return -EINVAL;
378
Christoffer Dall81eeb952015-11-25 10:02:16 -0800379 spin_lock(&irq->irq_lock);
380
381 if (!vgic_validate_injection(irq, level)) {
382 /* Nothing to see here, move along... */
383 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100384 vgic_put_irq(kvm, irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800385 return 0;
386 }
387
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100388 if (irq->config == VGIC_CONFIG_LEVEL)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800389 irq->line_level = level;
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100390 else
391 irq->pending_latch = true;
Christoffer Dall81eeb952015-11-25 10:02:16 -0800392
393 vgic_queue_irq_unlock(kvm, irq);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100394 vgic_put_irq(kvm, irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800395
396 return 0;
397}
398
Andre Przywara568e8c92015-12-22 00:52:33 +0000399int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq)
400{
401 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
402
403 BUG_ON(!irq);
404
405 spin_lock(&irq->irq_lock);
406
407 irq->hw = true;
408 irq->hwintid = phys_irq;
409
410 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100411 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000412
413 return 0;
414}
415
416int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq)
417{
Andre Przywara5dd4b922016-07-15 12:43:27 +0100418 struct vgic_irq *irq;
Andre Przywara568e8c92015-12-22 00:52:33 +0000419
420 if (!vgic_initialized(vcpu->kvm))
421 return -EAGAIN;
422
Andre Przywara5dd4b922016-07-15 12:43:27 +0100423 irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
424 BUG_ON(!irq);
425
Andre Przywara568e8c92015-12-22 00:52:33 +0000426 spin_lock(&irq->irq_lock);
427
428 irq->hw = false;
429 irq->hwintid = 0;
430
431 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100432 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000433
434 return 0;
435}
436
Marc Zyngier0919e842015-11-26 17:19:25 +0000437/**
Christoffer Dallc6ccd302017-05-04 13:24:20 +0200438 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
439 *
440 * @vcpu: Pointer to the VCPU (used for PPIs)
441 * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
442 * @owner: Opaque pointer to the owner
443 *
444 * Returns 0 if intid is not already used by another in-kernel device and the
445 * owner is set, otherwise returns an error code.
446 */
447int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
448{
449 struct vgic_irq *irq;
450 int ret = 0;
451
452 if (!vgic_initialized(vcpu->kvm))
453 return -EAGAIN;
454
455 /* SGIs and LPIs cannot be wired up to any device */
456 if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
457 return -EINVAL;
458
459 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
460 spin_lock(&irq->irq_lock);
461 if (irq->owner && irq->owner != owner)
462 ret = -EEXIST;
463 else
464 irq->owner = owner;
465 spin_unlock(&irq->irq_lock);
466
467 return ret;
468}
469
470/**
Marc Zyngier0919e842015-11-26 17:19:25 +0000471 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
472 *
473 * @vcpu: The VCPU pointer
474 *
475 * Go over the list of "interesting" interrupts, and prune those that we
476 * won't have to consider in the near future.
477 */
478static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
479{
480 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
481 struct vgic_irq *irq, *tmp;
482
483retry:
484 spin_lock(&vgic_cpu->ap_list_lock);
485
486 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
487 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
488
489 spin_lock(&irq->irq_lock);
490
491 BUG_ON(vcpu != irq->vcpu);
492
493 target_vcpu = vgic_target_oracle(irq);
494
495 if (!target_vcpu) {
496 /*
497 * We don't need to process this interrupt any
498 * further, move it off the list.
499 */
500 list_del(&irq->ap_list);
501 irq->vcpu = NULL;
502 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100503
504 /*
505 * This vgic_put_irq call matches the
506 * vgic_get_irq_kref in vgic_queue_irq_unlock,
507 * where we added the LPI to the ap_list. As
508 * we remove the irq from the list, we drop
509 * also drop the refcount.
510 */
511 vgic_put_irq(vcpu->kvm, irq);
Marc Zyngier0919e842015-11-26 17:19:25 +0000512 continue;
513 }
514
515 if (target_vcpu == vcpu) {
516 /* We're on the right CPU */
517 spin_unlock(&irq->irq_lock);
518 continue;
519 }
520
521 /* This interrupt looks like it has to be migrated. */
522
523 spin_unlock(&irq->irq_lock);
524 spin_unlock(&vgic_cpu->ap_list_lock);
525
526 /*
527 * Ensure locking order by always locking the smallest
528 * ID first.
529 */
530 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
531 vcpuA = vcpu;
532 vcpuB = target_vcpu;
533 } else {
534 vcpuA = target_vcpu;
535 vcpuB = vcpu;
536 }
537
538 spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
539 spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
540 SINGLE_DEPTH_NESTING);
541 spin_lock(&irq->irq_lock);
542
543 /*
544 * If the affinity has been preserved, move the
545 * interrupt around. Otherwise, it means things have
546 * changed while the interrupt was unlocked, and we
547 * need to replay this.
548 *
549 * In all cases, we cannot trust the list not to have
550 * changed, so we restart from the beginning.
551 */
552 if (target_vcpu == vgic_target_oracle(irq)) {
553 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
554
555 list_del(&irq->ap_list);
556 irq->vcpu = target_vcpu;
557 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
558 }
559
560 spin_unlock(&irq->irq_lock);
561 spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
562 spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
563 goto retry;
564 }
565
566 spin_unlock(&vgic_cpu->ap_list_lock);
567}
568
Marc Zyngier0919e842015-11-26 17:19:25 +0000569static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
570{
Marc Zyngier59529f62015-11-30 13:09:53 +0000571 if (kvm_vgic_global_state.type == VGIC_V2)
572 vgic_v2_fold_lr_state(vcpu);
573 else
574 vgic_v3_fold_lr_state(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000575}
576
577/* Requires the irq_lock to be held. */
578static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
579 struct vgic_irq *irq, int lr)
580{
581 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
Marc Zyngier140b0862015-11-26 17:19:25 +0000582
Marc Zyngier59529f62015-11-30 13:09:53 +0000583 if (kvm_vgic_global_state.type == VGIC_V2)
584 vgic_v2_populate_lr(vcpu, irq, lr);
585 else
586 vgic_v3_populate_lr(vcpu, irq, lr);
Marc Zyngier0919e842015-11-26 17:19:25 +0000587}
588
589static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
590{
Marc Zyngier59529f62015-11-30 13:09:53 +0000591 if (kvm_vgic_global_state.type == VGIC_V2)
592 vgic_v2_clear_lr(vcpu, lr);
593 else
594 vgic_v3_clear_lr(vcpu, lr);
Marc Zyngier0919e842015-11-26 17:19:25 +0000595}
596
597static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
598{
Marc Zyngier59529f62015-11-30 13:09:53 +0000599 if (kvm_vgic_global_state.type == VGIC_V2)
600 vgic_v2_set_underflow(vcpu);
601 else
602 vgic_v3_set_underflow(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000603}
604
605/* Requires the ap_list_lock to be held. */
606static int compute_ap_list_depth(struct kvm_vcpu *vcpu)
607{
608 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
609 struct vgic_irq *irq;
610 int count = 0;
611
612 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
613
614 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
615 spin_lock(&irq->irq_lock);
616 /* GICv2 SGIs can count for more than one... */
617 if (vgic_irq_is_sgi(irq->intid) && irq->source)
618 count += hweight8(irq->source);
619 else
620 count++;
621 spin_unlock(&irq->irq_lock);
622 }
623 return count;
624}
625
626/* Requires the VCPU's ap_list_lock to be held. */
627static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
628{
629 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
630 struct vgic_irq *irq;
631 int count = 0;
632
633 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
634
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100635 if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr)
Marc Zyngier0919e842015-11-26 17:19:25 +0000636 vgic_sort_ap_list(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000637
638 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
639 spin_lock(&irq->irq_lock);
640
641 if (unlikely(vgic_target_oracle(irq) != vcpu))
642 goto next;
643
644 /*
645 * If we get an SGI with multiple sources, try to get
646 * them in all at once.
647 */
648 do {
649 vgic_populate_lr(vcpu, irq, count++);
650 } while (irq->source && count < kvm_vgic_global_state.nr_lr);
651
652next:
653 spin_unlock(&irq->irq_lock);
654
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100655 if (count == kvm_vgic_global_state.nr_lr) {
656 if (!list_is_last(&irq->ap_list,
657 &vgic_cpu->ap_list_head))
658 vgic_set_underflow(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000659 break;
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100660 }
Marc Zyngier0919e842015-11-26 17:19:25 +0000661 }
662
663 vcpu->arch.vgic_cpu.used_lrs = count;
664
665 /* Nuke remaining LRs */
666 for ( ; count < kvm_vgic_global_state.nr_lr; count++)
667 vgic_clear_lr(vcpu, count);
668}
669
670/* Sync back the hardware VGIC state into our emulation after a guest's run. */
671void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
672{
Shih-Wei Lif6769582016-10-19 18:12:34 +0000673 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
674
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100675 /* An empty ap_list_head implies used_lrs == 0 */
676 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
Christoffer Dall0099b772016-09-27 18:53:35 +0200677 return;
678
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100679 if (vgic_cpu->used_lrs)
680 vgic_fold_lr_state(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000681 vgic_prune_ap_list(vcpu);
682}
683
684/* Flush our emulation state into the GIC hardware before entering the guest. */
685void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
686{
Shih-Wei Lif6769582016-10-19 18:12:34 +0000687 /*
688 * If there are no virtual interrupts active or pending for this
689 * VCPU, then there is no work to do and we can bail out without
690 * taking any lock. There is a potential race with someone injecting
691 * interrupts to the VCPU, but it is a benign race as the VCPU will
692 * either observe the new interrupt before or after doing this check,
693 * and introducing additional synchronization mechanism doesn't change
694 * this.
695 */
696 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
Christoffer Dall0099b772016-09-27 18:53:35 +0200697 return;
698
Marc Zyngier0919e842015-11-26 17:19:25 +0000699 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
700 vgic_flush_lr_state(vcpu);
701 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
702}
Eric Auger90eee562015-12-07 15:30:38 +0000703
Christoffer Dall328e5662016-03-24 11:21:04 +0100704void kvm_vgic_load(struct kvm_vcpu *vcpu)
705{
706 if (unlikely(!vgic_initialized(vcpu->kvm)))
707 return;
708
709 if (kvm_vgic_global_state.type == VGIC_V2)
710 vgic_v2_load(vcpu);
711 else
712 vgic_v3_load(vcpu);
713}
714
715void kvm_vgic_put(struct kvm_vcpu *vcpu)
716{
717 if (unlikely(!vgic_initialized(vcpu->kvm)))
718 return;
719
720 if (kvm_vgic_global_state.type == VGIC_V2)
721 vgic_v2_put(vcpu);
722 else
723 vgic_v3_put(vcpu);
724}
725
Eric Auger90eee562015-12-07 15:30:38 +0000726int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
727{
728 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
729 struct vgic_irq *irq;
730 bool pending = false;
731
732 if (!vcpu->kvm->arch.vgic.enabled)
733 return false;
734
735 spin_lock(&vgic_cpu->ap_list_lock);
736
737 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
738 spin_lock(&irq->irq_lock);
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100739 pending = irq_is_pending(irq) && irq->enabled;
Eric Auger90eee562015-12-07 15:30:38 +0000740 spin_unlock(&irq->irq_lock);
741
742 if (pending)
743 break;
744 }
745
746 spin_unlock(&vgic_cpu->ap_list_lock);
747
748 return pending;
749}
Marc Zyngier2b0cda82016-04-26 11:06:47 +0100750
751void vgic_kick_vcpus(struct kvm *kvm)
752{
753 struct kvm_vcpu *vcpu;
754 int c;
755
756 /*
757 * We've injected an interrupt, time to find out who deserves
758 * a good kick...
759 */
760 kvm_for_each_vcpu(c, vcpu, kvm) {
Andrew Jones325f9c62017-06-04 14:43:59 +0200761 if (kvm_vgic_vcpu_pending_irq(vcpu)) {
762 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Marc Zyngier2b0cda82016-04-26 11:06:47 +0100763 kvm_vcpu_kick(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +0200764 }
Marc Zyngier2b0cda82016-04-26 11:06:47 +0100765 }
766}
Andre Przywara568e8c92015-12-22 00:52:33 +0000767
768bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)
769{
770 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
771 bool map_is_active;
772
773 spin_lock(&irq->irq_lock);
774 map_is_active = irq->hw && irq->active;
775 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100776 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000777
778 return map_is_active;
779}
Andre Przywara0e4e82f2016-07-15 12:43:38 +0100780