blob: aea080a2c4437ed2ddd7c7b7d53148a2b2d7a6e3 [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 */
289 if (vcpu)
290 kvm_vcpu_kick(vcpu);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800291 return false;
292 }
293
294 /*
295 * We must unlock the irq lock to take the ap_list_lock where
296 * we are going to insert this new pending interrupt.
297 */
298 spin_unlock(&irq->irq_lock);
299
300 /* someone can do stuff here, which we re-check below */
301
302 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
303 spin_lock(&irq->irq_lock);
304
305 /*
306 * Did something change behind our backs?
307 *
308 * There are two cases:
309 * 1) The irq lost its pending state or was disabled behind our
310 * backs and/or it was queued to another VCPU's ap_list.
311 * 2) Someone changed the affinity on this irq behind our
312 * backs and we are now holding the wrong ap_list_lock.
313 *
314 * In both cases, drop the locks and retry.
315 */
316
317 if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
318 spin_unlock(&irq->irq_lock);
319 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
320
321 spin_lock(&irq->irq_lock);
322 goto retry;
323 }
324
Andre Przywara5dd4b922016-07-15 12:43:27 +0100325 /*
326 * Grab a reference to the irq to reflect the fact that it is
327 * now in the ap_list.
328 */
329 vgic_get_irq_kref(irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800330 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
331 irq->vcpu = vcpu;
332
333 spin_unlock(&irq->irq_lock);
334 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
335
336 kvm_vcpu_kick(vcpu);
337
338 return true;
339}
340
Christoffer Dall11710de2017-02-01 11:03:45 +0100341/**
342 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
343 * @kvm: The VM structure pointer
344 * @cpuid: The CPU for PPIs
345 * @intid: The INTID to inject a new state to.
346 * @level: Edge-triggered: true: to trigger the interrupt
347 * false: to ignore the call
348 * Level-sensitive true: raise the input signal
349 * false: lower the input signal
350 *
351 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
352 * level-sensitive interrupts. You can think of the level parameter as 1
353 * being HIGH and 0 being LOW and all devices being active-HIGH.
354 */
355int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
356 bool level)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800357{
358 struct kvm_vcpu *vcpu;
359 struct vgic_irq *irq;
360 int ret;
361
362 trace_vgic_update_irq_pending(cpuid, intid, level);
363
Eric Augerad275b8b2015-12-21 18:09:38 +0100364 ret = vgic_lazy_init(kvm);
365 if (ret)
366 return ret;
367
Christoffer Dall81eeb952015-11-25 10:02:16 -0800368 vcpu = kvm_get_vcpu(kvm, cpuid);
369 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
370 return -EINVAL;
371
372 irq = vgic_get_irq(kvm, vcpu, intid);
373 if (!irq)
374 return -EINVAL;
375
Christoffer Dall81eeb952015-11-25 10:02:16 -0800376 spin_lock(&irq->irq_lock);
377
378 if (!vgic_validate_injection(irq, level)) {
379 /* Nothing to see here, move along... */
380 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100381 vgic_put_irq(kvm, irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800382 return 0;
383 }
384
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100385 if (irq->config == VGIC_CONFIG_LEVEL)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800386 irq->line_level = level;
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100387 else
388 irq->pending_latch = true;
Christoffer Dall81eeb952015-11-25 10:02:16 -0800389
390 vgic_queue_irq_unlock(kvm, irq);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100391 vgic_put_irq(kvm, irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800392
393 return 0;
394}
395
Andre Przywara568e8c92015-12-22 00:52:33 +0000396int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq)
397{
398 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
399
400 BUG_ON(!irq);
401
402 spin_lock(&irq->irq_lock);
403
404 irq->hw = true;
405 irq->hwintid = phys_irq;
406
407 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100408 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000409
410 return 0;
411}
412
413int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq)
414{
Andre Przywara5dd4b922016-07-15 12:43:27 +0100415 struct vgic_irq *irq;
Andre Przywara568e8c92015-12-22 00:52:33 +0000416
417 if (!vgic_initialized(vcpu->kvm))
418 return -EAGAIN;
419
Andre Przywara5dd4b922016-07-15 12:43:27 +0100420 irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
421 BUG_ON(!irq);
422
Andre Przywara568e8c92015-12-22 00:52:33 +0000423 spin_lock(&irq->irq_lock);
424
425 irq->hw = false;
426 irq->hwintid = 0;
427
428 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100429 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000430
431 return 0;
432}
433
Marc Zyngier0919e842015-11-26 17:19:25 +0000434/**
435 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
436 *
437 * @vcpu: The VCPU pointer
438 *
439 * Go over the list of "interesting" interrupts, and prune those that we
440 * won't have to consider in the near future.
441 */
442static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
443{
444 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
445 struct vgic_irq *irq, *tmp;
446
447retry:
448 spin_lock(&vgic_cpu->ap_list_lock);
449
450 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
451 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
452
453 spin_lock(&irq->irq_lock);
454
455 BUG_ON(vcpu != irq->vcpu);
456
457 target_vcpu = vgic_target_oracle(irq);
458
459 if (!target_vcpu) {
460 /*
461 * We don't need to process this interrupt any
462 * further, move it off the list.
463 */
464 list_del(&irq->ap_list);
465 irq->vcpu = NULL;
466 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100467
468 /*
469 * This vgic_put_irq call matches the
470 * vgic_get_irq_kref in vgic_queue_irq_unlock,
471 * where we added the LPI to the ap_list. As
472 * we remove the irq from the list, we drop
473 * also drop the refcount.
474 */
475 vgic_put_irq(vcpu->kvm, irq);
Marc Zyngier0919e842015-11-26 17:19:25 +0000476 continue;
477 }
478
479 if (target_vcpu == vcpu) {
480 /* We're on the right CPU */
481 spin_unlock(&irq->irq_lock);
482 continue;
483 }
484
485 /* This interrupt looks like it has to be migrated. */
486
487 spin_unlock(&irq->irq_lock);
488 spin_unlock(&vgic_cpu->ap_list_lock);
489
490 /*
491 * Ensure locking order by always locking the smallest
492 * ID first.
493 */
494 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
495 vcpuA = vcpu;
496 vcpuB = target_vcpu;
497 } else {
498 vcpuA = target_vcpu;
499 vcpuB = vcpu;
500 }
501
502 spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
503 spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
504 SINGLE_DEPTH_NESTING);
505 spin_lock(&irq->irq_lock);
506
507 /*
508 * If the affinity has been preserved, move the
509 * interrupt around. Otherwise, it means things have
510 * changed while the interrupt was unlocked, and we
511 * need to replay this.
512 *
513 * In all cases, we cannot trust the list not to have
514 * changed, so we restart from the beginning.
515 */
516 if (target_vcpu == vgic_target_oracle(irq)) {
517 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
518
519 list_del(&irq->ap_list);
520 irq->vcpu = target_vcpu;
521 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
522 }
523
524 spin_unlock(&irq->irq_lock);
525 spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
526 spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
527 goto retry;
528 }
529
530 spin_unlock(&vgic_cpu->ap_list_lock);
531}
532
Marc Zyngier0919e842015-11-26 17:19:25 +0000533static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
534{
Marc Zyngier59529f62015-11-30 13:09:53 +0000535 if (kvm_vgic_global_state.type == VGIC_V2)
536 vgic_v2_fold_lr_state(vcpu);
537 else
538 vgic_v3_fold_lr_state(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000539}
540
541/* Requires the irq_lock to be held. */
542static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
543 struct vgic_irq *irq, int lr)
544{
545 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
Marc Zyngier140b0862015-11-26 17:19:25 +0000546
Marc Zyngier59529f62015-11-30 13:09:53 +0000547 if (kvm_vgic_global_state.type == VGIC_V2)
548 vgic_v2_populate_lr(vcpu, irq, lr);
549 else
550 vgic_v3_populate_lr(vcpu, irq, lr);
Marc Zyngier0919e842015-11-26 17:19:25 +0000551}
552
553static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
554{
Marc Zyngier59529f62015-11-30 13:09:53 +0000555 if (kvm_vgic_global_state.type == VGIC_V2)
556 vgic_v2_clear_lr(vcpu, lr);
557 else
558 vgic_v3_clear_lr(vcpu, lr);
Marc Zyngier0919e842015-11-26 17:19:25 +0000559}
560
561static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
562{
Marc Zyngier59529f62015-11-30 13:09:53 +0000563 if (kvm_vgic_global_state.type == VGIC_V2)
564 vgic_v2_set_underflow(vcpu);
565 else
566 vgic_v3_set_underflow(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000567}
568
569/* Requires the ap_list_lock to be held. */
570static int compute_ap_list_depth(struct kvm_vcpu *vcpu)
571{
572 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
573 struct vgic_irq *irq;
574 int count = 0;
575
576 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
577
578 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
579 spin_lock(&irq->irq_lock);
580 /* GICv2 SGIs can count for more than one... */
581 if (vgic_irq_is_sgi(irq->intid) && irq->source)
582 count += hweight8(irq->source);
583 else
584 count++;
585 spin_unlock(&irq->irq_lock);
586 }
587 return count;
588}
589
590/* Requires the VCPU's ap_list_lock to be held. */
591static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
592{
593 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
594 struct vgic_irq *irq;
595 int count = 0;
596
597 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
598
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100599 if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr)
Marc Zyngier0919e842015-11-26 17:19:25 +0000600 vgic_sort_ap_list(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000601
602 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
603 spin_lock(&irq->irq_lock);
604
605 if (unlikely(vgic_target_oracle(irq) != vcpu))
606 goto next;
607
608 /*
609 * If we get an SGI with multiple sources, try to get
610 * them in all at once.
611 */
612 do {
613 vgic_populate_lr(vcpu, irq, count++);
614 } while (irq->source && count < kvm_vgic_global_state.nr_lr);
615
616next:
617 spin_unlock(&irq->irq_lock);
618
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100619 if (count == kvm_vgic_global_state.nr_lr) {
620 if (!list_is_last(&irq->ap_list,
621 &vgic_cpu->ap_list_head))
622 vgic_set_underflow(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000623 break;
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100624 }
Marc Zyngier0919e842015-11-26 17:19:25 +0000625 }
626
627 vcpu->arch.vgic_cpu.used_lrs = count;
628
629 /* Nuke remaining LRs */
630 for ( ; count < kvm_vgic_global_state.nr_lr; count++)
631 vgic_clear_lr(vcpu, count);
632}
633
634/* Sync back the hardware VGIC state into our emulation after a guest's run. */
635void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
636{
Shih-Wei Lif6769582016-10-19 18:12:34 +0000637 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
638
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100639 /* An empty ap_list_head implies used_lrs == 0 */
640 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
Christoffer Dall0099b772016-09-27 18:53:35 +0200641 return;
642
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100643 if (vgic_cpu->used_lrs)
644 vgic_fold_lr_state(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000645 vgic_prune_ap_list(vcpu);
646}
647
648/* Flush our emulation state into the GIC hardware before entering the guest. */
649void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
650{
Shih-Wei Lif6769582016-10-19 18:12:34 +0000651 /*
652 * If there are no virtual interrupts active or pending for this
653 * VCPU, then there is no work to do and we can bail out without
654 * taking any lock. There is a potential race with someone injecting
655 * interrupts to the VCPU, but it is a benign race as the VCPU will
656 * either observe the new interrupt before or after doing this check,
657 * and introducing additional synchronization mechanism doesn't change
658 * this.
659 */
660 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
Christoffer Dall0099b772016-09-27 18:53:35 +0200661 return;
662
Marc Zyngier0919e842015-11-26 17:19:25 +0000663 spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
664 vgic_flush_lr_state(vcpu);
665 spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
666}
Eric Auger90eee562015-12-07 15:30:38 +0000667
Christoffer Dall328e5662016-03-24 11:21:04 +0100668void kvm_vgic_load(struct kvm_vcpu *vcpu)
669{
670 if (unlikely(!vgic_initialized(vcpu->kvm)))
671 return;
672
673 if (kvm_vgic_global_state.type == VGIC_V2)
674 vgic_v2_load(vcpu);
675 else
676 vgic_v3_load(vcpu);
677}
678
679void kvm_vgic_put(struct kvm_vcpu *vcpu)
680{
681 if (unlikely(!vgic_initialized(vcpu->kvm)))
682 return;
683
684 if (kvm_vgic_global_state.type == VGIC_V2)
685 vgic_v2_put(vcpu);
686 else
687 vgic_v3_put(vcpu);
688}
689
Eric Auger90eee562015-12-07 15:30:38 +0000690int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
691{
692 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
693 struct vgic_irq *irq;
694 bool pending = false;
695
696 if (!vcpu->kvm->arch.vgic.enabled)
697 return false;
698
699 spin_lock(&vgic_cpu->ap_list_lock);
700
701 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
702 spin_lock(&irq->irq_lock);
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100703 pending = irq_is_pending(irq) && irq->enabled;
Eric Auger90eee562015-12-07 15:30:38 +0000704 spin_unlock(&irq->irq_lock);
705
706 if (pending)
707 break;
708 }
709
710 spin_unlock(&vgic_cpu->ap_list_lock);
711
712 return pending;
713}
Marc Zyngier2b0cda82016-04-26 11:06:47 +0100714
715void vgic_kick_vcpus(struct kvm *kvm)
716{
717 struct kvm_vcpu *vcpu;
718 int c;
719
720 /*
721 * We've injected an interrupt, time to find out who deserves
722 * a good kick...
723 */
724 kvm_for_each_vcpu(c, vcpu, kvm) {
725 if (kvm_vgic_vcpu_pending_irq(vcpu))
726 kvm_vcpu_kick(vcpu);
727 }
728}
Andre Przywara568e8c92015-12-22 00:52:33 +0000729
730bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)
731{
732 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
733 bool map_is_active;
734
735 spin_lock(&irq->irq_lock);
736 map_is_active = irq->hw && irq->active;
737 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100738 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000739
740 return map_is_active;
741}
Andre Przywara0e4e82f2016-07-15 12:43:38 +0100742