blob: 9b98876a8a93891c89e2153c5d4f3b7d120b3427 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Christoffer Dall64a959d2015-11-24 16:51:12 +01002/*
3 * Copyright (C) 2015, 2016 ARM Ltd.
Christoffer Dall64a959d2015-11-24 16:51:12 +01004 */
5
Mark Rutland41b87592018-04-25 17:13:41 +01006#include <linux/interrupt.h>
7#include <linux/irq.h>
Christoffer Dall64a959d2015-11-24 16:51:12 +01008#include <linux/kvm.h>
9#include <linux/kvm_host.h>
Christoffer Dall8e444742015-11-25 10:02:16 -080010#include <linux/list_sort.h>
Mark Rutland41b87592018-04-25 17:13:41 +010011#include <linux/nospec.h>
12
Christoffer Dall771621b2017-10-04 23:42:32 +020013#include <asm/kvm_hyp.h>
Christoffer Dall64a959d2015-11-24 16:51:12 +010014
15#include "vgic.h"
16
Christoffer Dall81eeb952015-11-25 10:02:16 -080017#define CREATE_TRACE_POINTS
Christoffer Dall35d2d5d2017-05-04 13:54:17 +020018#include "trace.h"
Christoffer Dall81eeb952015-11-25 10:02:16 -080019
Ard Biesheuvel63d7c6a2017-03-09 21:51:59 +010020struct vgic_global kvm_vgic_global_state __ro_after_init = {
21 .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
22};
Christoffer Dall64a959d2015-11-24 16:51:12 +010023
Christoffer Dall81eeb952015-11-25 10:02:16 -080024/*
25 * Locking order is always:
Christoffer Dallabd72292017-05-06 20:01:24 +020026 * kvm->lock (mutex)
27 * its->cmd_lock (mutex)
28 * its->its_lock (mutex)
Andre Przywara388d4352018-05-11 15:20:12 +010029 * vgic_cpu->ap_list_lock must be taken with IRQs disabled
30 * kvm->lpi_list_lock must be taken with IRQs disabled
31 * vgic_irq->irq_lock must be taken with IRQs disabled
32 *
33 * As the ap_list_lock might be taken from the timer interrupt handler,
34 * we have to disable IRQs before taking this lock and everything lower
35 * than it.
Christoffer Dall81eeb952015-11-25 10:02:16 -080036 *
Andre Przywara424c3382016-07-15 12:43:32 +010037 * If you need to take multiple locks, always take the upper lock first,
38 * then the lower ones, e.g. first take the its_lock, then the irq_lock.
39 * If you are already holding a lock and need to take a higher one, you
40 * have to drop the lower ranking lock first and re-aquire it after having
41 * taken the upper one.
Christoffer Dall81eeb952015-11-25 10:02:16 -080042 *
43 * When taking more than one ap_list_lock at the same time, always take the
44 * lowest numbered VCPU's ap_list_lock first, so:
45 * vcpuX->vcpu_id < vcpuY->vcpu_id:
Julien Thierrye08d8d22019-01-07 15:06:17 +000046 * raw_spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
47 * raw_spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
Christoffer Dall006df0f2016-10-16 22:19:11 +020048 *
49 * Since the VGIC must support injecting virtual interrupts from ISRs, we have
Julien Thierrye08d8d22019-01-07 15:06:17 +000050 * to use the raw_spin_lock_irqsave/raw_spin_unlock_irqrestore versions of outer
Christoffer Dall006df0f2016-10-16 22:19:11 +020051 * spinlocks for any lock that may be taken while injecting an interrupt.
Christoffer Dall81eeb952015-11-25 10:02:16 -080052 */
53
Andre Przywara38024112016-07-15 12:43:33 +010054/*
55 * Iterate over the VM's list of mapped LPIs to find the one with a
56 * matching interrupt ID and return a reference to the IRQ structure.
57 */
58static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
59{
60 struct vgic_dist *dist = &kvm->arch.vgic;
61 struct vgic_irq *irq = NULL;
Andre Przywara388d4352018-05-11 15:20:12 +010062 unsigned long flags;
Andre Przywara38024112016-07-15 12:43:33 +010063
Julien Thierryfc3bc472019-01-07 15:06:16 +000064 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
Andre Przywara38024112016-07-15 12:43:33 +010065
66 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
67 if (irq->intid != intid)
68 continue;
69
70 /*
71 * This increases the refcount, the caller is expected to
72 * call vgic_put_irq() later once it's finished with the IRQ.
73 */
Marc Zyngierd97594e2016-07-17 11:27:23 +010074 vgic_get_irq_kref(irq);
Andre Przywara38024112016-07-15 12:43:33 +010075 goto out_unlock;
76 }
77 irq = NULL;
78
79out_unlock:
Julien Thierryfc3bc472019-01-07 15:06:16 +000080 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
Andre Przywara38024112016-07-15 12:43:33 +010081
82 return irq;
83}
84
85/*
86 * This looks up the virtual interrupt ID to get the corresponding
87 * struct vgic_irq. It also increases the refcount, so any caller is expected
88 * to call vgic_put_irq() once it's finished with this IRQ.
89 */
Christoffer Dall64a959d2015-11-24 16:51:12 +010090struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
91 u32 intid)
92{
93 /* SGIs and PPIs */
Mark Rutland41b87592018-04-25 17:13:41 +010094 if (intid <= VGIC_MAX_PRIVATE) {
Gustavo A. R. Silvac23b2e6f2018-12-12 14:11:23 -060095 intid = array_index_nospec(intid, VGIC_MAX_PRIVATE + 1);
Christoffer Dall64a959d2015-11-24 16:51:12 +010096 return &vcpu->arch.vgic_cpu.private_irqs[intid];
Mark Rutland41b87592018-04-25 17:13:41 +010097 }
Christoffer Dall64a959d2015-11-24 16:51:12 +010098
99 /* SPIs */
Marc Zyngierbea2ef82018-12-04 17:11:19 +0000100 if (intid < (kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS)) {
101 intid = array_index_nospec(intid, kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS);
Christoffer Dall64a959d2015-11-24 16:51:12 +0100102 return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
Mark Rutland41b87592018-04-25 17:13:41 +0100103 }
Christoffer Dall64a959d2015-11-24 16:51:12 +0100104
Andre Przywara38024112016-07-15 12:43:33 +0100105 /* LPIs */
Christoffer Dall64a959d2015-11-24 16:51:12 +0100106 if (intid >= VGIC_MIN_LPI)
Andre Przywara38024112016-07-15 12:43:33 +0100107 return vgic_get_lpi(kvm, intid);
Christoffer Dall64a959d2015-11-24 16:51:12 +0100108
Christoffer Dall64a959d2015-11-24 16:51:12 +0100109 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
Marc Zyngier1bb36912019-03-18 12:45:22 +0000121/*
122 * Drop the refcount on the LPI. Must be called with lpi_list_lock held.
123 */
124void __vgic_put_lpi_locked(struct kvm *kvm, struct vgic_irq *irq)
125{
126 struct vgic_dist *dist = &kvm->arch.vgic;
127
128 if (!kref_put(&irq->refcount, vgic_irq_release))
129 return;
130
131 list_del(&irq->lpi_list);
132 dist->lpi_list_count--;
133
134 kfree(irq);
135}
136
Andre Przywara5dd4b922016-07-15 12:43:27 +0100137void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
138{
Christoffer Dall2cccbb32016-08-02 22:05:42 +0200139 struct vgic_dist *dist = &kvm->arch.vgic;
Andre Przywara388d4352018-05-11 15:20:12 +0100140 unsigned long flags;
Andre Przywara38024112016-07-15 12:43:33 +0100141
Andre Przywara5dd4b922016-07-15 12:43:27 +0100142 if (irq->intid < VGIC_MIN_LPI)
143 return;
144
Julien Thierryfc3bc472019-01-07 15:06:16 +0000145 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
Marc Zyngier1bb36912019-03-18 12:45:22 +0000146 __vgic_put_lpi_locked(kvm, irq);
Julien Thierryfc3bc472019-01-07 15:06:16 +0000147 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100148}
149
Marc Zyngier96085b92019-04-02 06:36:23 +0100150void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu)
151{
152 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
153 struct vgic_irq *irq, *tmp;
154 unsigned long flags;
155
156 raw_spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
157
158 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
159 if (irq->intid >= VGIC_MIN_LPI) {
160 raw_spin_lock(&irq->irq_lock);
161 list_del(&irq->ap_list);
162 irq->vcpu = NULL;
163 raw_spin_unlock(&irq->irq_lock);
164 vgic_put_irq(vcpu->kvm, irq);
165 }
166 }
167
168 raw_spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
169}
170
Christoffer Dalldf635c52017-09-01 16:25:12 +0200171void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
172{
173 WARN_ON(irq_set_irqchip_state(irq->host_irq,
174 IRQCHIP_STATE_PENDING,
175 pending));
176}
177
Christoffer Dalle40cc572017-08-29 10:40:44 +0200178bool vgic_get_phys_line_level(struct vgic_irq *irq)
179{
180 bool line_level;
181
182 BUG_ON(!irq->hw);
183
Marc Zyngierdb75f1a2021-03-01 17:39:39 +0000184 if (irq->ops && irq->ops->get_input_level)
185 return irq->ops->get_input_level(irq->intid);
Christoffer Dallb6909a62017-10-27 19:30:09 +0200186
Christoffer Dalle40cc572017-08-29 10:40:44 +0200187 WARN_ON(irq_get_irqchip_state(irq->host_irq,
188 IRQCHIP_STATE_PENDING,
189 &line_level));
190 return line_level;
191}
192
193/* Set/Clear the physical active state */
194void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active)
195{
196
197 BUG_ON(!irq->hw);
198 WARN_ON(irq_set_irqchip_state(irq->host_irq,
199 IRQCHIP_STATE_ACTIVE,
200 active));
201}
202
Christoffer Dall81eeb952015-11-25 10:02:16 -0800203/**
204 * kvm_vgic_target_oracle - compute the target vcpu for an irq
205 *
206 * @irq: The irq to route. Must be already locked.
207 *
208 * Based on the current state of the interrupt (enabled, pending,
209 * active, vcpu and target_vcpu), compute the next vcpu this should be
210 * given to. Return NULL if this shouldn't be injected at all.
211 *
212 * Requires the IRQ lock to be held.
213 */
214static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
215{
Lance Royd4d592a2018-10-04 23:45:50 -0700216 lockdep_assert_held(&irq->irq_lock);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800217
218 /* If the interrupt is active, it must stay on the current vcpu */
219 if (irq->active)
220 return irq->vcpu ? : irq->target_vcpu;
221
222 /*
223 * If the IRQ is not active but enabled and pending, we should direct
224 * it to its configured target VCPU.
225 * If the distributor is disabled, pending interrupts shouldn't be
226 * forwarded.
227 */
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100228 if (irq->enabled && irq_is_pending(irq)) {
Christoffer Dall81eeb952015-11-25 10:02:16 -0800229 if (unlikely(irq->target_vcpu &&
230 !irq->target_vcpu->kvm->arch.vgic.enabled))
231 return NULL;
232
233 return irq->target_vcpu;
234 }
235
236 /* If neither active nor pending and enabled, then this IRQ should not
237 * be queued to any VCPU.
238 */
239 return NULL;
240}
241
242/*
Christoffer Dall8e444742015-11-25 10:02:16 -0800243 * The order of items in the ap_lists defines how we'll pack things in LRs as
244 * well, the first items in the list being the first things populated in the
245 * LRs.
246 *
247 * A hard rule is that active interrupts can never be pushed out of the LRs
248 * (and therefore take priority) since we cannot reliably trap on deactivation
249 * of IRQs and therefore they have to be present in the LRs.
250 *
251 * Otherwise things should be sorted by the priority field and the GIC
252 * hardware support will take care of preemption of priority groups etc.
253 *
254 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
255 * to sort "b" before "a".
256 */
Sami Tolvanen4f0f5862021-04-08 11:28:34 -0700257static int vgic_irq_cmp(void *priv, const struct list_head *a,
258 const struct list_head *b)
Christoffer Dall8e444742015-11-25 10:02:16 -0800259{
260 struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
261 struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
262 bool penda, pendb;
263 int ret;
264
Heyi Guod4a80612019-08-27 12:26:50 +0100265 /*
266 * list_sort may call this function with the same element when
267 * the list is fairly long.
268 */
269 if (unlikely(irqa == irqb))
270 return 0;
271
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000272 raw_spin_lock(&irqa->irq_lock);
273 raw_spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
Christoffer Dall8e444742015-11-25 10:02:16 -0800274
275 if (irqa->active || irqb->active) {
276 ret = (int)irqb->active - (int)irqa->active;
277 goto out;
278 }
279
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100280 penda = irqa->enabled && irq_is_pending(irqa);
281 pendb = irqb->enabled && irq_is_pending(irqb);
Christoffer Dall8e444742015-11-25 10:02:16 -0800282
283 if (!penda || !pendb) {
284 ret = (int)pendb - (int)penda;
285 goto out;
286 }
287
288 /* Both pending and enabled, sort by priority */
289 ret = irqa->priority - irqb->priority;
290out:
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000291 raw_spin_unlock(&irqb->irq_lock);
292 raw_spin_unlock(&irqa->irq_lock);
Christoffer Dall8e444742015-11-25 10:02:16 -0800293 return ret;
294}
295
296/* Must be called with the ap_list_lock held */
297static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
298{
299 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
300
Lance Royd4d592a2018-10-04 23:45:50 -0700301 lockdep_assert_held(&vgic_cpu->ap_list_lock);
Christoffer Dall8e444742015-11-25 10:02:16 -0800302
303 list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
304}
305
306/*
Christoffer Dall81eeb952015-11-25 10:02:16 -0800307 * Only valid injection if changing level for level-triggered IRQs or for a
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200308 * rising edge, and in-kernel connected IRQ lines can only be controlled by
309 * their owner.
Christoffer Dall81eeb952015-11-25 10:02:16 -0800310 */
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200311static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800312{
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200313 if (irq->owner != owner)
314 return false;
315
Christoffer Dall81eeb952015-11-25 10:02:16 -0800316 switch (irq->config) {
317 case VGIC_CONFIG_LEVEL:
318 return irq->line_level != level;
319 case VGIC_CONFIG_EDGE:
320 return level;
321 }
322
323 return false;
324}
325
326/*
327 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
328 * Do the queuing if necessary, taking the right locks in the right order.
329 * Returns true when the IRQ was queued, false otherwise.
330 *
331 * Needs to be entered with the IRQ lock already held, but will return
332 * with all locks dropped.
333 */
Christoffer Dall006df0f2016-10-16 22:19:11 +0200334bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
335 unsigned long flags)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800336{
337 struct kvm_vcpu *vcpu;
338
Lance Royd4d592a2018-10-04 23:45:50 -0700339 lockdep_assert_held(&irq->irq_lock);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800340
341retry:
342 vcpu = vgic_target_oracle(irq);
343 if (irq->vcpu || !vcpu) {
344 /*
345 * If this IRQ is already on a VCPU's ap_list, then it
346 * cannot be moved or modified and there is no more work for
347 * us to do.
348 *
349 * Otherwise, if the irq is not pending and enabled, it does
350 * not need to be inserted into an ap_list and there is also
351 * no more work for us to do.
352 */
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000353 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Shih-Wei Lid42c7972016-10-27 15:08:13 +0000354
355 /*
356 * We have to kick the VCPU here, because we could be
357 * queueing an edge-triggered interrupt for which we
358 * get no EOI maintenance interrupt. In that case,
359 * while the IRQ is already on the VCPU's AP list, the
360 * VCPU could have EOI'ed the original interrupt and
361 * won't see this one until it exits for some other
362 * reason.
363 */
Andrew Jones325f9c62017-06-04 14:43:59 +0200364 if (vcpu) {
365 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Shih-Wei Lid42c7972016-10-27 15:08:13 +0000366 kvm_vcpu_kick(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +0200367 }
Christoffer Dall81eeb952015-11-25 10:02:16 -0800368 return false;
369 }
370
371 /*
372 * We must unlock the irq lock to take the ap_list_lock where
373 * we are going to insert this new pending interrupt.
374 */
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000375 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800376
377 /* someone can do stuff here, which we re-check below */
378
Julien Thierrye08d8d22019-01-07 15:06:17 +0000379 raw_spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000380 raw_spin_lock(&irq->irq_lock);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800381
382 /*
383 * Did something change behind our backs?
384 *
385 * There are two cases:
386 * 1) The irq lost its pending state or was disabled behind our
387 * backs and/or it was queued to another VCPU's ap_list.
388 * 2) Someone changed the affinity on this irq behind our
389 * backs and we are now holding the wrong ap_list_lock.
390 *
391 * In both cases, drop the locks and retry.
392 */
393
394 if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000395 raw_spin_unlock(&irq->irq_lock);
Julien Thierrye08d8d22019-01-07 15:06:17 +0000396 raw_spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock,
397 flags);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800398
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000399 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800400 goto retry;
401 }
402
Andre Przywara5dd4b922016-07-15 12:43:27 +0100403 /*
404 * Grab a reference to the irq to reflect the fact that it is
405 * now in the ap_list.
406 */
407 vgic_get_irq_kref(irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800408 list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
409 irq->vcpu = vcpu;
410
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000411 raw_spin_unlock(&irq->irq_lock);
Julien Thierrye08d8d22019-01-07 15:06:17 +0000412 raw_spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800413
Andrew Jones325f9c62017-06-04 14:43:59 +0200414 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800415 kvm_vcpu_kick(vcpu);
416
417 return true;
418}
419
Christoffer Dall11710de2017-02-01 11:03:45 +0100420/**
421 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
422 * @kvm: The VM structure pointer
423 * @cpuid: The CPU for PPIs
424 * @intid: The INTID to inject a new state to.
425 * @level: Edge-triggered: true: to trigger the interrupt
426 * false: to ignore the call
427 * Level-sensitive true: raise the input signal
428 * false: lower the input signal
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200429 * @owner: The opaque pointer to the owner of the IRQ being raised to verify
430 * that the caller is allowed to inject this IRQ. Userspace
431 * injections will have owner == NULL.
Christoffer Dall11710de2017-02-01 11:03:45 +0100432 *
433 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
434 * level-sensitive interrupts. You can think of the level parameter as 1
435 * being HIGH and 0 being LOW and all devices being active-HIGH.
436 */
437int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200438 bool level, void *owner)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800439{
440 struct kvm_vcpu *vcpu;
441 struct vgic_irq *irq;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200442 unsigned long flags;
Christoffer Dall81eeb952015-11-25 10:02:16 -0800443 int ret;
444
445 trace_vgic_update_irq_pending(cpuid, intid, level);
446
Eric Augerad275b8b2015-12-21 18:09:38 +0100447 ret = vgic_lazy_init(kvm);
448 if (ret)
449 return ret;
450
Christoffer Dall81eeb952015-11-25 10:02:16 -0800451 vcpu = kvm_get_vcpu(kvm, cpuid);
452 if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
453 return -EINVAL;
454
455 irq = vgic_get_irq(kvm, vcpu, intid);
456 if (!irq)
457 return -EINVAL;
458
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000459 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800460
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200461 if (!vgic_validate_injection(irq, level, owner)) {
Christoffer Dall81eeb952015-11-25 10:02:16 -0800462 /* Nothing to see here, move along... */
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000463 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100464 vgic_put_irq(kvm, irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800465 return 0;
466 }
467
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100468 if (irq->config == VGIC_CONFIG_LEVEL)
Christoffer Dall81eeb952015-11-25 10:02:16 -0800469 irq->line_level = level;
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100470 else
471 irq->pending_latch = true;
Christoffer Dall81eeb952015-11-25 10:02:16 -0800472
Christoffer Dall006df0f2016-10-16 22:19:11 +0200473 vgic_queue_irq_unlock(kvm, irq, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100474 vgic_put_irq(kvm, irq);
Christoffer Dall81eeb952015-11-25 10:02:16 -0800475
476 return 0;
477}
478
Eric Auger47bbd312017-10-27 15:28:32 +0100479/* @irq->irq_lock must be held */
480static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
Christoffer Dallb6909a62017-10-27 19:30:09 +0200481 unsigned int host_irq,
Marc Zyngierdb75f1a2021-03-01 17:39:39 +0000482 struct irq_ops *ops)
Andre Przywara568e8c92015-12-22 00:52:33 +0000483{
Eric Auger47bbd312017-10-27 15:28:32 +0100484 struct irq_desc *desc;
485 struct irq_data *data;
486
487 /*
488 * Find the physical IRQ number corresponding to @host_irq
489 */
490 desc = irq_to_desc(host_irq);
491 if (!desc) {
492 kvm_err("%s: no interrupt descriptor\n", __func__);
493 return -EINVAL;
494 }
495 data = irq_desc_get_irq_data(desc);
496 while (data->parent_data)
497 data = data->parent_data;
498
499 irq->hw = true;
500 irq->host_irq = host_irq;
501 irq->hwintid = data->hwirq;
Marc Zyngierdb75f1a2021-03-01 17:39:39 +0000502 irq->ops = ops;
Eric Auger47bbd312017-10-27 15:28:32 +0100503 return 0;
504}
505
506/* @irq->irq_lock must be held */
507static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
508{
509 irq->hw = false;
510 irq->hwintid = 0;
Marc Zyngierdb75f1a2021-03-01 17:39:39 +0000511 irq->ops = NULL;
Eric Auger47bbd312017-10-27 15:28:32 +0100512}
513
514int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
Marc Zyngierdb75f1a2021-03-01 17:39:39 +0000515 u32 vintid, struct irq_ops *ops)
Eric Auger47bbd312017-10-27 15:28:32 +0100516{
517 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
Christoffer Dall006df0f2016-10-16 22:19:11 +0200518 unsigned long flags;
Eric Auger47bbd312017-10-27 15:28:32 +0100519 int ret;
Andre Przywara568e8c92015-12-22 00:52:33 +0000520
521 BUG_ON(!irq);
522
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000523 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngierdb75f1a2021-03-01 17:39:39 +0000524 ret = kvm_vgic_map_irq(vcpu, irq, host_irq, ops);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000525 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100526 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000527
Eric Auger47bbd312017-10-27 15:28:32 +0100528 return ret;
Andre Przywara568e8c92015-12-22 00:52:33 +0000529}
530
Christoffer Dall413aa802018-03-05 11:36:38 +0100531/**
532 * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
533 * @vcpu: The VCPU pointer
534 * @vintid: The INTID of the interrupt
535 *
536 * Reset the active and pending states of a mapped interrupt. Kernel
537 * subsystems injecting mapped interrupts should reset their interrupt lines
538 * when we are doing a reset of the VM.
539 */
540void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
541{
542 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
543 unsigned long flags;
544
545 if (!irq->hw)
546 goto out;
547
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000548 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Christoffer Dall413aa802018-03-05 11:36:38 +0100549 irq->active = false;
550 irq->pending_latch = false;
551 irq->line_level = false;
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000552 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Christoffer Dall413aa802018-03-05 11:36:38 +0100553out:
554 vgic_put_irq(vcpu->kvm, irq);
555}
556
Eric Auger47bbd312017-10-27 15:28:32 +0100557int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
Andre Przywara568e8c92015-12-22 00:52:33 +0000558{
Andre Przywara5dd4b922016-07-15 12:43:27 +0100559 struct vgic_irq *irq;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200560 unsigned long flags;
Andre Przywara568e8c92015-12-22 00:52:33 +0000561
562 if (!vgic_initialized(vcpu->kvm))
563 return -EAGAIN;
564
Eric Auger47bbd312017-10-27 15:28:32 +0100565 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100566 BUG_ON(!irq);
567
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000568 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Eric Auger47bbd312017-10-27 15:28:32 +0100569 kvm_vgic_unmap_irq(irq);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000570 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100571 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +0000572
573 return 0;
574}
575
Marc Zyngier0919e842015-11-26 17:19:25 +0000576/**
Christoffer Dallc6ccd302017-05-04 13:24:20 +0200577 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
578 *
579 * @vcpu: Pointer to the VCPU (used for PPIs)
580 * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
581 * @owner: Opaque pointer to the owner
582 *
583 * Returns 0 if intid is not already used by another in-kernel device and the
584 * owner is set, otherwise returns an error code.
585 */
586int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
587{
588 struct vgic_irq *irq;
Marc Zyngier74658942017-11-30 17:00:30 +0000589 unsigned long flags;
Christoffer Dallc6ccd302017-05-04 13:24:20 +0200590 int ret = 0;
591
592 if (!vgic_initialized(vcpu->kvm))
593 return -EAGAIN;
594
595 /* SGIs and LPIs cannot be wired up to any device */
596 if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
597 return -EINVAL;
598
599 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000600 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Christoffer Dallc6ccd302017-05-04 13:24:20 +0200601 if (irq->owner && irq->owner != owner)
602 ret = -EEXIST;
603 else
604 irq->owner = owner;
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000605 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Christoffer Dallc6ccd302017-05-04 13:24:20 +0200606
607 return ret;
608}
609
610/**
Marc Zyngier0919e842015-11-26 17:19:25 +0000611 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
612 *
613 * @vcpu: The VCPU pointer
614 *
615 * Go over the list of "interesting" interrupts, and prune those that we
616 * won't have to consider in the near future.
617 */
618static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
619{
620 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
621 struct vgic_irq *irq, *tmp;
Jia Hed0823cb2018-08-03 21:57:04 +0800622
623 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
Marc Zyngier0919e842015-11-26 17:19:25 +0000624
625retry:
Julien Thierrye08d8d22019-01-07 15:06:17 +0000626 raw_spin_lock(&vgic_cpu->ap_list_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000627
628 list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
629 struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
Andre Przywarabf9a4132018-04-17 11:23:49 +0100630 bool target_vcpu_needs_kick = false;
Marc Zyngier0919e842015-11-26 17:19:25 +0000631
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000632 raw_spin_lock(&irq->irq_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000633
634 BUG_ON(vcpu != irq->vcpu);
635
636 target_vcpu = vgic_target_oracle(irq);
637
638 if (!target_vcpu) {
639 /*
640 * We don't need to process this interrupt any
641 * further, move it off the list.
642 */
643 list_del(&irq->ap_list);
644 irq->vcpu = NULL;
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000645 raw_spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100646
647 /*
648 * This vgic_put_irq call matches the
649 * vgic_get_irq_kref in vgic_queue_irq_unlock,
650 * where we added the LPI to the ap_list. As
651 * we remove the irq from the list, we drop
652 * also drop the refcount.
653 */
654 vgic_put_irq(vcpu->kvm, irq);
Marc Zyngier0919e842015-11-26 17:19:25 +0000655 continue;
656 }
657
658 if (target_vcpu == vcpu) {
659 /* We're on the right CPU */
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000660 raw_spin_unlock(&irq->irq_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000661 continue;
662 }
663
664 /* This interrupt looks like it has to be migrated. */
665
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000666 raw_spin_unlock(&irq->irq_lock);
Julien Thierrye08d8d22019-01-07 15:06:17 +0000667 raw_spin_unlock(&vgic_cpu->ap_list_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000668
669 /*
670 * Ensure locking order by always locking the smallest
671 * ID first.
672 */
673 if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
674 vcpuA = vcpu;
675 vcpuB = target_vcpu;
676 } else {
677 vcpuA = target_vcpu;
678 vcpuB = vcpu;
679 }
680
Julien Thierrye08d8d22019-01-07 15:06:17 +0000681 raw_spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
682 raw_spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
683 SINGLE_DEPTH_NESTING);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000684 raw_spin_lock(&irq->irq_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000685
686 /*
687 * If the affinity has been preserved, move the
688 * interrupt around. Otherwise, it means things have
689 * changed while the interrupt was unlocked, and we
690 * need to replay this.
691 *
692 * In all cases, we cannot trust the list not to have
693 * changed, so we restart from the beginning.
694 */
695 if (target_vcpu == vgic_target_oracle(irq)) {
696 struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
697
698 list_del(&irq->ap_list);
699 irq->vcpu = target_vcpu;
700 list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
Andre Przywarabf9a4132018-04-17 11:23:49 +0100701 target_vcpu_needs_kick = true;
Marc Zyngier0919e842015-11-26 17:19:25 +0000702 }
703
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000704 raw_spin_unlock(&irq->irq_lock);
Julien Thierrye08d8d22019-01-07 15:06:17 +0000705 raw_spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
706 raw_spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
Andre Przywarabf9a4132018-04-17 11:23:49 +0100707
708 if (target_vcpu_needs_kick) {
709 kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
710 kvm_vcpu_kick(target_vcpu);
711 }
712
Marc Zyngier0919e842015-11-26 17:19:25 +0000713 goto retry;
714 }
715
Julien Thierrye08d8d22019-01-07 15:06:17 +0000716 raw_spin_unlock(&vgic_cpu->ap_list_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000717}
718
Marc Zyngier0919e842015-11-26 17:19:25 +0000719static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
720{
Marc Zyngier59529f62015-11-30 13:09:53 +0000721 if (kvm_vgic_global_state.type == VGIC_V2)
722 vgic_v2_fold_lr_state(vcpu);
723 else
724 vgic_v3_fold_lr_state(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000725}
726
727/* Requires the irq_lock to be held. */
728static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
729 struct vgic_irq *irq, int lr)
730{
Lance Royd4d592a2018-10-04 23:45:50 -0700731 lockdep_assert_held(&irq->irq_lock);
Marc Zyngier140b0862015-11-26 17:19:25 +0000732
Marc Zyngier59529f62015-11-30 13:09:53 +0000733 if (kvm_vgic_global_state.type == VGIC_V2)
734 vgic_v2_populate_lr(vcpu, irq, lr);
735 else
736 vgic_v3_populate_lr(vcpu, irq, lr);
Marc Zyngier0919e842015-11-26 17:19:25 +0000737}
738
739static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
740{
Marc Zyngier59529f62015-11-30 13:09:53 +0000741 if (kvm_vgic_global_state.type == VGIC_V2)
742 vgic_v2_clear_lr(vcpu, lr);
743 else
744 vgic_v3_clear_lr(vcpu, lr);
Marc Zyngier0919e842015-11-26 17:19:25 +0000745}
746
747static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
748{
Marc Zyngier59529f62015-11-30 13:09:53 +0000749 if (kvm_vgic_global_state.type == VGIC_V2)
750 vgic_v2_set_underflow(vcpu);
751 else
752 vgic_v3_set_underflow(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000753}
754
755/* Requires the ap_list_lock to be held. */
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000756static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
757 bool *multi_sgi)
Marc Zyngier0919e842015-11-26 17:19:25 +0000758{
759 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
760 struct vgic_irq *irq;
761 int count = 0;
762
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000763 *multi_sgi = false;
764
Lance Royd4d592a2018-10-04 23:45:50 -0700765 lockdep_assert_held(&vgic_cpu->ap_list_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000766
767 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
Marc Zyngier53692902018-04-18 10:39:04 +0100768 int w;
769
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000770 raw_spin_lock(&irq->irq_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000771 /* GICv2 SGIs can count for more than one... */
Marc Zyngier53692902018-04-18 10:39:04 +0100772 w = vgic_irq_get_lr_count(irq);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000773 raw_spin_unlock(&irq->irq_lock);
Marc Zyngier53692902018-04-18 10:39:04 +0100774
775 count += w;
776 *multi_sgi |= (w > 1);
Marc Zyngier0919e842015-11-26 17:19:25 +0000777 }
778 return count;
779}
780
781/* Requires the VCPU's ap_list_lock to be held. */
782static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
783{
784 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
785 struct vgic_irq *irq;
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000786 int count;
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000787 bool multi_sgi;
788 u8 prio = 0xff;
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800789 int i = 0;
Marc Zyngier0919e842015-11-26 17:19:25 +0000790
Lance Royd4d592a2018-10-04 23:45:50 -0700791 lockdep_assert_held(&vgic_cpu->ap_list_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000792
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000793 count = compute_ap_list_depth(vcpu, &multi_sgi);
794 if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
Marc Zyngier0919e842015-11-26 17:19:25 +0000795 vgic_sort_ap_list(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000796
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000797 count = 0;
798
Marc Zyngier0919e842015-11-26 17:19:25 +0000799 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000800 raw_spin_lock(&irq->irq_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000801
Marc Zyngier0919e842015-11-26 17:19:25 +0000802 /*
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000803 * If we have multi-SGIs in the pipeline, we need to
804 * guarantee that they are all seen before any IRQ of
805 * lower priority. In that case, we need to filter out
806 * these interrupts by exiting early. This is easy as
807 * the AP list has been sorted already.
Marc Zyngier0919e842015-11-26 17:19:25 +0000808 */
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000809 if (multi_sgi && irq->priority > prio) {
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000810 _raw_spin_unlock(&irq->irq_lock);
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000811 break;
812 }
Marc Zyngier0919e842015-11-26 17:19:25 +0000813
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000814 if (likely(vgic_target_oracle(irq) == vcpu)) {
815 vgic_populate_lr(vcpu, irq, count++);
816
Marc Zyngier53692902018-04-18 10:39:04 +0100817 if (irq->source)
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000818 prio = irq->priority;
Marc Zyngier16ca6a62018-03-06 21:48:01 +0000819 }
820
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000821 raw_spin_unlock(&irq->irq_lock);
Marc Zyngier0919e842015-11-26 17:19:25 +0000822
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100823 if (count == kvm_vgic_global_state.nr_lr) {
824 if (!list_is_last(&irq->ap_list,
825 &vgic_cpu->ap_list_head))
826 vgic_set_underflow(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000827 break;
Christoffer Dall90cac1f2017-03-21 21:16:12 +0100828 }
Marc Zyngier0919e842015-11-26 17:19:25 +0000829 }
830
Marc Zyngier0919e842015-11-26 17:19:25 +0000831 /* Nuke remaining LRs */
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800832 for (i = count ; i < kvm_vgic_global_state.nr_lr; i++)
833 vgic_clear_lr(vcpu, i);
834
835 if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
836 vcpu->arch.vgic_cpu.vgic_v2.used_lrs = count;
837 else
838 vcpu->arch.vgic_cpu.vgic_v3.used_lrs = count;
Marc Zyngier0919e842015-11-26 17:19:25 +0000839}
840
Christoffer Dall771621b2017-10-04 23:42:32 +0200841static inline bool can_access_vgic_from_kernel(void)
842{
843 /*
844 * GICv2 can always be accessed from the kernel because it is
845 * memory-mapped, and VHE systems can access GICv3 EL2 system
846 * registers.
847 */
848 return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
849}
850
Christoffer Dall75174ba2016-12-22 20:39:10 +0100851static inline void vgic_save_state(struct kvm_vcpu *vcpu)
852{
853 if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
854 vgic_v2_save_state(vcpu);
Christoffer Dall771621b2017-10-04 23:42:32 +0200855 else
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800856 __vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
Christoffer Dall75174ba2016-12-22 20:39:10 +0100857}
858
Marc Zyngier0919e842015-11-26 17:19:25 +0000859/* Sync back the hardware VGIC state into our emulation after a guest's run. */
860void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
861{
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800862 int used_lrs;
Shih-Wei Lif6769582016-10-19 18:12:34 +0000863
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100864 /* An empty ap_list_head implies used_lrs == 0 */
865 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
Christoffer Dall0099b772016-09-27 18:53:35 +0200866 return;
867
Christoffer Dall2d0e63e2017-10-05 17:19:19 +0200868 if (can_access_vgic_from_kernel())
869 vgic_save_state(vcpu);
870
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800871 if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
872 used_lrs = vcpu->arch.vgic_cpu.vgic_v2.used_lrs;
873 else
874 used_lrs = vcpu->arch.vgic_cpu.vgic_v3.used_lrs;
875
876 if (used_lrs)
Christoffer Dall8ac76ef2017-03-18 13:48:42 +0100877 vgic_fold_lr_state(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000878 vgic_prune_ap_list(vcpu);
879}
880
Christoffer Dall75174ba2016-12-22 20:39:10 +0100881static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
882{
883 if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
884 vgic_v2_restore_state(vcpu);
Christoffer Dall771621b2017-10-04 23:42:32 +0200885 else
Christoffer Dallfc5d1f12018-12-01 08:41:28 -0800886 __vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
Christoffer Dall75174ba2016-12-22 20:39:10 +0100887}
888
Marc Zyngier0919e842015-11-26 17:19:25 +0000889/* Flush our emulation state into the GIC hardware before entering the guest. */
890void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
891{
Shih-Wei Lif6769582016-10-19 18:12:34 +0000892 /*
893 * If there are no virtual interrupts active or pending for this
894 * VCPU, then there is no work to do and we can bail out without
895 * taking any lock. There is a potential race with someone injecting
896 * interrupts to the VCPU, but it is a benign race as the VCPU will
897 * either observe the new interrupt before or after doing this check,
898 * and introducing additional synchronization mechanism doesn't change
899 * this.
Marc Zyngierca712282019-03-13 18:07:50 +0000900 *
901 * Note that we still need to go through the whole thing if anything
902 * can be directly injected (GICv4).
Shih-Wei Lif6769582016-10-19 18:12:34 +0000903 */
Marc Zyngierca712282019-03-13 18:07:50 +0000904 if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head) &&
905 !vgic_supports_direct_msis(vcpu->kvm))
Christoffer Dall2d0e63e2017-10-05 17:19:19 +0200906 return;
Christoffer Dall0099b772016-09-27 18:53:35 +0200907
Christoffer Dall006df0f2016-10-16 22:19:11 +0200908 DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
909
Marc Zyngierca712282019-03-13 18:07:50 +0000910 if (!list_empty(&vcpu->arch.vgic_cpu.ap_list_head)) {
911 raw_spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
912 vgic_flush_lr_state(vcpu);
913 raw_spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
914 }
Christoffer Dall75174ba2016-12-22 20:39:10 +0100915
Christoffer Dall771621b2017-10-04 23:42:32 +0200916 if (can_access_vgic_from_kernel())
917 vgic_restore_state(vcpu);
Shenming Lu57e3ceb2020-11-28 22:18:57 +0800918
919 if (vgic_supports_direct_msis(vcpu->kvm))
920 vgic_v4_commit(vcpu);
Marc Zyngier0919e842015-11-26 17:19:25 +0000921}
Eric Auger90eee562015-12-07 15:30:38 +0000922
Christoffer Dall328e5662016-03-24 11:21:04 +0100923void kvm_vgic_load(struct kvm_vcpu *vcpu)
924{
925 if (unlikely(!vgic_initialized(vcpu->kvm)))
926 return;
927
928 if (kvm_vgic_global_state.type == VGIC_V2)
929 vgic_v2_load(vcpu);
930 else
931 vgic_v3_load(vcpu);
932}
933
934void kvm_vgic_put(struct kvm_vcpu *vcpu)
935{
936 if (unlikely(!vgic_initialized(vcpu->kvm)))
937 return;
938
939 if (kvm_vgic_global_state.type == VGIC_V2)
940 vgic_v2_put(vcpu);
941 else
942 vgic_v3_put(vcpu);
943}
944
Marc Zyngier5eeaf102019-08-02 10:28:32 +0100945void kvm_vgic_vmcr_sync(struct kvm_vcpu *vcpu)
946{
947 if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
948 return;
949
950 if (kvm_vgic_global_state.type == VGIC_V2)
951 vgic_v2_vmcr_sync(vcpu);
952 else
953 vgic_v3_vmcr_sync(vcpu);
954}
955
Eric Auger90eee562015-12-07 15:30:38 +0000956int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
957{
958 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
959 struct vgic_irq *irq;
960 bool pending = false;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200961 unsigned long flags;
Christoffer Dall90097822018-12-01 13:21:47 -0800962 struct vgic_vmcr vmcr;
Eric Auger90eee562015-12-07 15:30:38 +0000963
964 if (!vcpu->kvm->arch.vgic.enabled)
965 return false;
966
Marc Zyngierc9719682017-10-27 15:28:47 +0100967 if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
968 return true;
969
Christoffer Dall90097822018-12-01 13:21:47 -0800970 vgic_get_vmcr(vcpu, &vmcr);
971
Julien Thierrye08d8d22019-01-07 15:06:17 +0000972 raw_spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
Eric Auger90eee562015-12-07 15:30:38 +0000973
974 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000975 raw_spin_lock(&irq->irq_lock);
Christoffer Dall90097822018-12-01 13:21:47 -0800976 pending = irq_is_pending(irq) && irq->enabled &&
977 !irq->active &&
978 irq->priority < vmcr.pmr;
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000979 raw_spin_unlock(&irq->irq_lock);
Eric Auger90eee562015-12-07 15:30:38 +0000980
981 if (pending)
982 break;
983 }
984
Julien Thierrye08d8d22019-01-07 15:06:17 +0000985 raw_spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
Eric Auger90eee562015-12-07 15:30:38 +0000986
987 return pending;
988}
Marc Zyngier2b0cda82016-04-26 11:06:47 +0100989
990void vgic_kick_vcpus(struct kvm *kvm)
991{
992 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +0000993 unsigned long c;
Marc Zyngier2b0cda82016-04-26 11:06:47 +0100994
995 /*
996 * We've injected an interrupt, time to find out who deserves
997 * a good kick...
998 */
999 kvm_for_each_vcpu(c, vcpu, kvm) {
Andrew Jones325f9c62017-06-04 14:43:59 +02001000 if (kvm_vgic_vcpu_pending_irq(vcpu)) {
1001 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Marc Zyngier2b0cda82016-04-26 11:06:47 +01001002 kvm_vcpu_kick(vcpu);
Andrew Jones325f9c62017-06-04 14:43:59 +02001003 }
Marc Zyngier2b0cda82016-04-26 11:06:47 +01001004 }
1005}
Andre Przywara568e8c92015-12-22 00:52:33 +00001006
Eric Auger47bbd312017-10-27 15:28:32 +01001007bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
Andre Przywara568e8c92015-12-22 00:52:33 +00001008{
Andre Przywara285a90e2017-11-17 17:58:21 +00001009 struct vgic_irq *irq;
Andre Przywara568e8c92015-12-22 00:52:33 +00001010 bool map_is_active;
Christoffer Dall006df0f2016-10-16 22:19:11 +02001011 unsigned long flags;
Andre Przywara568e8c92015-12-22 00:52:33 +00001012
Christoffer Dallf39d16c2016-10-19 12:40:17 +02001013 if (!vgic_initialized(vcpu->kvm))
1014 return false;
1015
Andre Przywara285a90e2017-11-17 17:58:21 +00001016 irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
Julien Thierry8fa3adb2019-01-07 15:06:15 +00001017 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Andre Przywara568e8c92015-12-22 00:52:33 +00001018 map_is_active = irq->hw && irq->active;
Julien Thierry8fa3adb2019-01-07 15:06:15 +00001019 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Andre Przywara5dd4b922016-07-15 12:43:27 +01001020 vgic_put_irq(vcpu->kvm, irq);
Andre Przywara568e8c92015-12-22 00:52:33 +00001021
1022 return map_is_active;
1023}
Marc Zyngier3134cc82021-08-19 19:03:05 +01001024
1025/*
1026 * Level-triggered mapped IRQs are special because we only observe rising
1027 * edges as input to the VGIC.
1028 *
1029 * If the guest never acked the interrupt we have to sample the physical
1030 * line and set the line level, because the device state could have changed
1031 * or we simply need to process the still pending interrupt later.
1032 *
1033 * We could also have entered the guest with the interrupt active+pending.
1034 * On the next exit, we need to re-evaluate the pending state, as it could
1035 * otherwise result in a spurious interrupt by injecting a now potentially
1036 * stale pending state.
1037 *
1038 * If this causes us to lower the level, we have to also clear the physical
1039 * active state, since we will otherwise never be told when the interrupt
1040 * becomes asserted again.
1041 *
1042 * Another case is when the interrupt requires a helping hand on
1043 * deactivation (no HW deactivation, for example).
1044 */
1045void vgic_irq_handle_resampling(struct vgic_irq *irq,
1046 bool lr_deactivated, bool lr_pending)
1047{
1048 if (vgic_irq_is_mapped_level(irq)) {
1049 bool resample = false;
1050
1051 if (unlikely(vgic_irq_needs_resampling(irq))) {
1052 resample = !(irq->active || irq->pending_latch);
1053 } else if (lr_pending || (lr_deactivated && irq->line_level)) {
1054 irq->line_level = vgic_get_phys_line_level(irq);
1055 resample = !irq->line_level;
1056 }
1057
1058 if (resample)
1059 vgic_irq_set_phys_active(irq, false);
1060 }
1061}