blob: a44ecf9eca4e9f4024f7ae84da430a4f9e73999f [file] [log] [blame]
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001/*
2 * Copyright (C) 2012 ARM Ltd.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
Marc Zyngier01ac5e32013-01-21 19:36:16 -050019#include <linux/cpu.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050020#include <linux/kvm.h>
21#include <linux/kvm_host.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050024#include <linux/of.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
Marc Zyngier6c3d63c2014-06-23 17:37:18 +010027#include <linux/rculist.h>
Christoffer Dall2a2f3e262014-02-02 13:41:02 -080028#include <linux/uaccess.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050029
Marc Zyngier1a89dd92013-01-21 19:36:12 -050030#include <asm/kvm_emulate.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050031#include <asm/kvm_arm.h>
32#include <asm/kvm_mmu.h>
Eric Auger174178f2015-03-04 11:14:36 +010033#include <trace/events/kvm.h>
Andre Przywara6777f772015-03-26 14:39:34 +000034#include <asm/kvm.h>
35#include <kvm/iodev.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050036
Marc Zyngierb47ef922013-01-21 19:36:14 -050037/*
38 * How the whole thing works (courtesy of Christoffer Dall):
39 *
40 * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
Christoffer Dall7e362912014-06-14 22:34:04 +020041 * something is pending on the CPU interface.
42 * - Interrupts that are pending on the distributor are stored on the
43 * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
44 * ioctls and guest mmio ops, and other in-kernel peripherals such as the
45 * arch. timers).
Marc Zyngierb47ef922013-01-21 19:36:14 -050046 * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
47 * recalculated
48 * - To calculate the oracle, we need info for each cpu from
49 * compute_pending_for_cpu, which considers:
Christoffer Dall227844f2014-06-09 12:27:18 +020050 * - PPI: dist->irq_pending & dist->irq_enable
51 * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
Christoffer Dall7e362912014-06-14 22:34:04 +020052 * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
Marc Zyngierb47ef922013-01-21 19:36:14 -050053 * registers, stored on each vcpu. We only keep one bit of
54 * information per interrupt, making sure that only one vcpu can
55 * accept the interrupt.
Christoffer Dall7e362912014-06-14 22:34:04 +020056 * - If any of the above state changes, we must recalculate the oracle.
Marc Zyngierb47ef922013-01-21 19:36:14 -050057 * - The same is true when injecting an interrupt, except that we only
58 * consider a single interrupt at a time. The irq_spi_cpu array
59 * contains the target CPU for each SPI.
60 *
61 * The handling of level interrupts adds some extra complexity. We
62 * need to track when the interrupt has been EOIed, so we can sample
63 * the 'line' again. This is achieved as such:
64 *
65 * - When a level interrupt is moved onto a vcpu, the corresponding
Christoffer Dalldbf20f92014-06-09 12:55:13 +020066 * bit in irq_queued is set. As long as this bit is set, the line
Marc Zyngierb47ef922013-01-21 19:36:14 -050067 * will be ignored for further interrupts. The interrupt is injected
68 * into the vcpu with the GICH_LR_EOI bit set (generate a
69 * maintenance interrupt on EOI).
70 * - When the interrupt is EOIed, the maintenance interrupt fires,
Christoffer Dalldbf20f92014-06-09 12:55:13 +020071 * and clears the corresponding bit in irq_queued. This allows the
Marc Zyngierb47ef922013-01-21 19:36:14 -050072 * interrupt line to be sampled again.
Christoffer Dallfaa1b462014-06-14 21:54:51 +020073 * - Note that level-triggered interrupts can also be set to pending from
74 * writes to GICD_ISPENDRn and lowering the external input line does not
75 * cause the interrupt to become inactive in such a situation.
76 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
77 * inactive as long as the external input line is held high.
Marc Zyngier6c3d63c2014-06-23 17:37:18 +010078 *
79 *
80 * Initialization rules: there are multiple stages to the vgic
81 * initialization, both for the distributor and the CPU interfaces.
82 *
83 * Distributor:
84 *
85 * - kvm_vgic_early_init(): initialization of static data that doesn't
86 * depend on any sizing information or emulation type. No allocation
87 * is allowed there.
88 *
89 * - vgic_init(): allocation and initialization of the generic data
90 * structures that depend on sizing information (number of CPUs,
91 * number of interrupts). Also initializes the vcpu specific data
92 * structures. Can be executed lazily for GICv2.
93 * [to be renamed to kvm_vgic_init??]
94 *
95 * CPU Interface:
96 *
97 * - kvm_vgic_cpu_early_init(): initialization of static data that
98 * doesn't depend on any sizing information or emulation type. No
99 * allocation is allowed there.
Marc Zyngierb47ef922013-01-21 19:36:14 -0500100 */
101
Andre Przywara83215812014-06-07 00:53:08 +0200102#include "vgic.h"
Christoffer Dall330690c2013-01-21 19:36:13 -0500103
Marc Zyngiera1fcb442013-01-21 19:36:15 -0500104static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100105static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100106static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
107static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +0100108static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
109 int virt_irq);
Christoffer Dall91036172015-08-25 22:50:57 +0200110static int compute_pending_for_cpu(struct kvm_vcpu *vcpu);
Marc Zyngier01ac5e32013-01-21 19:36:16 -0500111
Marc Zyngier8f186d52014-02-04 18:13:03 +0000112static const struct vgic_ops *vgic_ops;
113static const struct vgic_params *vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500114
Andre Przywarab26e5fd2014-06-02 16:19:12 +0200115static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source)
116{
117 vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source);
118}
119
120static bool queue_sgi(struct kvm_vcpu *vcpu, int irq)
121{
122 return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq);
123}
124
125int kvm_vgic_map_resources(struct kvm *kvm)
126{
127 return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic);
128}
129
Victor Kamensky9662fb42014-06-12 09:30:10 -0700130/*
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100131 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
132 * extracts u32s out of them.
Victor Kamensky9662fb42014-06-12 09:30:10 -0700133 *
134 * This does not work on 64-bit BE systems, because the bitmap access
135 * will store two consecutive 32-bit words with the higher-addressed
136 * register's bits at the lower index and the lower-addressed register's
137 * bits at the higher index.
138 *
139 * Therefore, swizzle the register index when accessing the 32-bit word
140 * registers to access the right register's value.
141 */
142#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
143#define REG_OFFSET_SWIZZLE 1
144#else
145#define REG_OFFSET_SWIZZLE 0
146#endif
Marc Zyngierb47ef922013-01-21 19:36:14 -0500147
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100148static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
149{
150 int nr_longs;
151
152 nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
153
154 b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
155 if (!b->private)
156 return -ENOMEM;
157
158 b->shared = b->private + nr_cpus;
159
160 return 0;
161}
162
163static void vgic_free_bitmap(struct vgic_bitmap *b)
164{
165 kfree(b->private);
166 b->private = NULL;
167 b->shared = NULL;
168}
169
Christoffer Dall2df36a52014-09-28 16:04:26 +0200170/*
171 * Call this function to convert a u64 value to an unsigned long * bitmask
172 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
173 *
174 * Warning: Calling this function may modify *val.
175 */
176static unsigned long *u64_to_bitmask(u64 *val)
177{
178#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
179 *val = (*val >> 32) | (*val << 32);
180#endif
181 return (unsigned long *)val;
182}
183
Andre Przywara83215812014-06-07 00:53:08 +0200184u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500185{
186 offset >>= 2;
187 if (!offset)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100188 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500189 else
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100190 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500191}
192
193static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
194 int cpuid, int irq)
195{
196 if (irq < VGIC_NR_PRIVATE_IRQS)
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100197 return test_bit(irq, x->private + cpuid);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500198
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100199 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500200}
201
Andre Przywara83215812014-06-07 00:53:08 +0200202void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
203 int irq, int val)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500204{
205 unsigned long *reg;
206
207 if (irq < VGIC_NR_PRIVATE_IRQS) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100208 reg = x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500209 } else {
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100210 reg = x->shared;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500211 irq -= VGIC_NR_PRIVATE_IRQS;
212 }
213
214 if (val)
215 set_bit(irq, reg);
216 else
217 clear_bit(irq, reg);
218}
219
220static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
221{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100222 return x->private + cpuid;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500223}
224
Andre Przywara83215812014-06-07 00:53:08 +0200225unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500226{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100227 return x->shared;
228}
229
230static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
231{
232 int size;
233
234 size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
235 size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
236
237 x->private = kzalloc(size, GFP_KERNEL);
238 if (!x->private)
239 return -ENOMEM;
240
241 x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
242 return 0;
243}
244
245static void vgic_free_bytemap(struct vgic_bytemap *b)
246{
247 kfree(b->private);
248 b->private = NULL;
249 b->shared = NULL;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500250}
251
Andre Przywara83215812014-06-07 00:53:08 +0200252u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500253{
Marc Zyngierc1bfb572014-07-08 12:09:01 +0100254 u32 *reg;
255
256 if (offset < VGIC_NR_PRIVATE_IRQS) {
257 reg = x->private;
258 offset += cpuid * VGIC_NR_PRIVATE_IRQS;
259 } else {
260 reg = x->shared;
261 offset -= VGIC_NR_PRIVATE_IRQS;
262 }
263
264 return reg + (offset / sizeof(u32));
Marc Zyngierb47ef922013-01-21 19:36:14 -0500265}
266
267#define VGIC_CFG_LEVEL 0
268#define VGIC_CFG_EDGE 1
269
270static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
271{
272 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
273 int irq_val;
274
275 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
276 return irq_val == VGIC_CFG_EDGE;
277}
278
279static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
280{
281 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
282
283 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
284}
285
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200286static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500287{
288 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
289
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200290 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500291}
292
Christoffer Dall47a98b12015-03-13 17:02:54 +0000293static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
294{
295 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
296
297 return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
298}
299
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200300static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500301{
302 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
303
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200304 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500305}
306
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200307static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500308{
309 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
310
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200311 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500312}
313
Christoffer Dall47a98b12015-03-13 17:02:54 +0000314static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
315{
316 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
317
318 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
319}
320
321static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
322{
323 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
324
325 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
326}
327
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200328static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
329{
330 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
331
332 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
333}
334
335static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
336{
337 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
338
339 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
340}
341
342static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
343{
344 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
345
346 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
347}
348
349static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
350{
351 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
352
353 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
354}
355
356static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
357{
358 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
359
360 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
Christoffer Dall91036172015-08-25 22:50:57 +0200361 if (!vgic_dist_irq_get_level(vcpu, irq)) {
362 vgic_dist_irq_clear_pending(vcpu, irq);
363 if (!compute_pending_for_cpu(vcpu))
364 clear_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
365 }
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200366}
367
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500368static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
369{
370 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
371
Christoffer Dall227844f2014-06-09 12:27:18 +0200372 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500373}
374
Andre Przywara83215812014-06-07 00:53:08 +0200375void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500376{
377 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
378
Christoffer Dall227844f2014-06-09 12:27:18 +0200379 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500380}
381
Andre Przywara83215812014-06-07 00:53:08 +0200382void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500383{
384 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
385
Christoffer Dall227844f2014-06-09 12:27:18 +0200386 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500387}
388
389static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
390{
391 if (irq < VGIC_NR_PRIVATE_IRQS)
392 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
393 else
394 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
395 vcpu->arch.vgic_cpu.pending_shared);
396}
397
Andre Przywara83215812014-06-07 00:53:08 +0200398void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500399{
400 if (irq < VGIC_NR_PRIVATE_IRQS)
401 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
402 else
403 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
404 vcpu->arch.vgic_cpu.pending_shared);
405}
406
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200407static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
408{
Marc Zyngier7a67b4b2015-06-05 16:45:29 +0100409 return !vgic_irq_is_queued(vcpu, irq);
Christoffer Dalldbf20f92014-06-09 12:55:13 +0200410}
411
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500412/**
413 * vgic_reg_access - access vgic register
414 * @mmio: pointer to the data describing the mmio access
415 * @reg: pointer to the virtual backing of vgic distributor data
416 * @offset: least significant 2 bits used for word offset
417 * @mode: ACCESS_ mode (see defines above)
418 *
419 * Helper to make vgic register access easier using one of the access
420 * modes defined for vgic register access
421 * (read,raz,write-ignored,setbit,clearbit,write)
422 */
Andre Przywara83215812014-06-07 00:53:08 +0200423void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
424 phys_addr_t offset, int mode)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500425{
426 int word_offset = (offset & 3) * 8;
427 u32 mask = (1UL << (mmio->len * 8)) - 1;
428 u32 regval;
429
430 /*
431 * Any alignment fault should have been delivered to the guest
432 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
433 */
434
435 if (reg) {
436 regval = *reg;
437 } else {
438 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
439 regval = 0;
440 }
441
442 if (mmio->is_write) {
443 u32 data = mmio_data_read(mmio, mask) << word_offset;
444 switch (ACCESS_WRITE_MASK(mode)) {
445 case ACCESS_WRITE_IGNORED:
446 return;
447
448 case ACCESS_WRITE_SETBIT:
449 regval |= data;
450 break;
451
452 case ACCESS_WRITE_CLEARBIT:
453 regval &= ~data;
454 break;
455
456 case ACCESS_WRITE_VALUE:
457 regval = (regval & ~(mask << word_offset)) | data;
458 break;
459 }
460 *reg = regval;
461 } else {
462 switch (ACCESS_READ_MASK(mode)) {
463 case ACCESS_READ_RAZ:
464 regval = 0;
465 /* fall through */
466
467 case ACCESS_READ_VALUE:
468 mmio_data_write(mmio, mask, regval >> word_offset);
469 }
470 }
471}
472
Andre Przywara83215812014-06-07 00:53:08 +0200473bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
474 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500475{
476 vgic_reg_access(mmio, NULL, offset,
477 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
478 return false;
479}
480
Andre Przywara83215812014-06-07 00:53:08 +0200481bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio,
482 phys_addr_t offset, int vcpu_id, int access)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500483{
Andre Przywarad97f6832014-06-11 14:11:49 +0200484 u32 *reg;
485 int mode = ACCESS_READ_VALUE | access;
486 struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id);
487
488 reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset);
489 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500490 if (mmio->is_write) {
Andre Przywarad97f6832014-06-11 14:11:49 +0200491 if (access & ACCESS_WRITE_CLEARBIT) {
492 if (offset < 4) /* Force SGI enabled */
493 *reg |= 0xffff;
494 vgic_retire_disabled_irqs(target_vcpu);
495 }
496 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500497 return true;
498 }
499
500 return false;
501}
502
Andre Przywara83215812014-06-07 00:53:08 +0200503bool vgic_handle_set_pending_reg(struct kvm *kvm,
504 struct kvm_exit_mmio *mmio,
505 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500506{
Christoffer Dall9da48b52014-06-14 22:30:45 +0200507 u32 *reg, orig;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200508 u32 level_mask;
Andre Przywarad97f6832014-06-11 14:11:49 +0200509 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT;
510 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200511
Andre Przywarad97f6832014-06-11 14:11:49 +0200512 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200513 level_mask = (~(*reg));
514
515 /* Mark both level and edge triggered irqs as pending */
Andre Przywarad97f6832014-06-11 14:11:49 +0200516 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200517 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200518 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200519
Marc Zyngierb47ef922013-01-21 19:36:14 -0500520 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200521 /* Set the soft-pending flag only for level-triggered irqs */
522 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200523 vcpu_id, offset);
524 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200525 *reg &= level_mask;
526
Christoffer Dall9da48b52014-06-14 22:30:45 +0200527 /* Ignore writes to SGIs */
528 if (offset < 2) {
529 *reg &= ~0xffff;
530 *reg |= orig & 0xffff;
531 }
532
Andre Przywarad97f6832014-06-11 14:11:49 +0200533 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500534 return true;
535 }
536
537 return false;
538}
539
Christoffer Dall544c5722015-10-17 17:55:12 +0200540/*
541 * If a mapped interrupt's state has been modified by the guest such that it
542 * is no longer active or pending, without it have gone through the sync path,
543 * then the map->active field must be cleared so the interrupt can be taken
544 * again.
545 */
546static void vgic_handle_clear_mapped_irq(struct kvm_vcpu *vcpu)
547{
548 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
549 struct list_head *root;
550 struct irq_phys_map_entry *entry;
551 struct irq_phys_map *map;
552
553 rcu_read_lock();
554
555 /* Check for PPIs */
556 root = &vgic_cpu->irq_phys_map_list;
557 list_for_each_entry_rcu(entry, root, entry) {
558 map = &entry->map;
559
560 if (!vgic_dist_irq_is_pending(vcpu, map->virt_irq) &&
561 !vgic_irq_is_active(vcpu, map->virt_irq))
562 map->active = false;
563 }
564
565 rcu_read_unlock();
566}
567
Andre Przywara83215812014-06-07 00:53:08 +0200568bool vgic_handle_clear_pending_reg(struct kvm *kvm,
569 struct kvm_exit_mmio *mmio,
570 phys_addr_t offset, int vcpu_id)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500571{
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200572 u32 *level_active;
Christoffer Dall9da48b52014-06-14 22:30:45 +0200573 u32 *reg, orig;
Andre Przywarad97f6832014-06-11 14:11:49 +0200574 int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
575 struct vgic_dist *dist = &kvm->arch.vgic;
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200576
Andre Przywarad97f6832014-06-11 14:11:49 +0200577 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dall9da48b52014-06-14 22:30:45 +0200578 orig = *reg;
Andre Przywarad97f6832014-06-11 14:11:49 +0200579 vgic_reg_access(mmio, reg, offset, mode);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500580 if (mmio->is_write) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200581 /* Re-set level triggered level-active interrupts */
582 level_active = vgic_bitmap_get_reg(&dist->irq_level,
Andre Przywarad97f6832014-06-11 14:11:49 +0200583 vcpu_id, offset);
584 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200585 *reg |= *level_active;
586
Christoffer Dall9da48b52014-06-14 22:30:45 +0200587 /* Ignore writes to SGIs */
588 if (offset < 2) {
589 *reg &= ~0xffff;
590 *reg |= orig & 0xffff;
591 }
592
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200593 /* Clear soft-pending flags */
594 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
Andre Przywarad97f6832014-06-11 14:11:49 +0200595 vcpu_id, offset);
596 vgic_reg_access(mmio, reg, offset, mode);
Christoffer Dallfaa1b462014-06-14 21:54:51 +0200597
Christoffer Dall544c5722015-10-17 17:55:12 +0200598 vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id));
Andre Przywarad97f6832014-06-11 14:11:49 +0200599 vgic_update_state(kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500600 return true;
601 }
Marc Zyngierb47ef922013-01-21 19:36:14 -0500602 return false;
603}
604
Christoffer Dall47a98b12015-03-13 17:02:54 +0000605bool vgic_handle_set_active_reg(struct kvm *kvm,
606 struct kvm_exit_mmio *mmio,
607 phys_addr_t offset, int vcpu_id)
608{
609 u32 *reg;
610 struct vgic_dist *dist = &kvm->arch.vgic;
611
612 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
613 vgic_reg_access(mmio, reg, offset,
614 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
615
616 if (mmio->is_write) {
617 vgic_update_state(kvm);
618 return true;
619 }
620
621 return false;
622}
623
624bool vgic_handle_clear_active_reg(struct kvm *kvm,
625 struct kvm_exit_mmio *mmio,
626 phys_addr_t offset, int vcpu_id)
627{
628 u32 *reg;
629 struct vgic_dist *dist = &kvm->arch.vgic;
630
631 reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
632 vgic_reg_access(mmio, reg, offset,
633 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
634
635 if (mmio->is_write) {
Christoffer Dall544c5722015-10-17 17:55:12 +0200636 vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id));
Christoffer Dall47a98b12015-03-13 17:02:54 +0000637 vgic_update_state(kvm);
638 return true;
639 }
640
641 return false;
642}
643
Marc Zyngierb47ef922013-01-21 19:36:14 -0500644static u32 vgic_cfg_expand(u16 val)
645{
646 u32 res = 0;
647 int i;
648
649 /*
650 * Turn a 16bit value like abcd...mnop into a 32bit word
651 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
652 */
653 for (i = 0; i < 16; i++)
654 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
655
656 return res;
657}
658
659static u16 vgic_cfg_compress(u32 val)
660{
661 u16 res = 0;
662 int i;
663
664 /*
665 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
666 * abcd...mnop which is what we really care about.
667 */
668 for (i = 0; i < 16; i++)
669 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
670
671 return res;
672}
673
674/*
675 * The distributor uses 2 bits per IRQ for the CFG register, but the
676 * LSB is always 0. As such, we only keep the upper bit, and use the
677 * two above functions to compress/expand the bits
678 */
Andre Przywara83215812014-06-07 00:53:08 +0200679bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
680 phys_addr_t offset)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500681{
682 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100683
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200684 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500685 val = *reg >> 16;
686 else
687 val = *reg & 0xffff;
688
689 val = vgic_cfg_expand(val);
690 vgic_reg_access(mmio, &val, offset,
691 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
692 if (mmio->is_write) {
Christoffer Dall8bf9a702015-08-30 14:42:16 +0200693 /* Ignore writes to read-only SGI and PPI bits */
694 if (offset < 8)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500695 return false;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500696
697 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200698 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500699 *reg &= 0xffff;
700 *reg |= val << 16;
701 } else {
702 *reg &= 0xffff << 16;
703 *reg |= val;
704 }
705 }
706
707 return false;
708}
709
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800710/**
Christoffer Dall47a98b12015-03-13 17:02:54 +0000711 * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800712 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
713 *
Christoffer Dall47a98b12015-03-13 17:02:54 +0000714 * Move any IRQs that have already been assigned to LRs back to the
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800715 * emulated distributor state so that the complete emulated state can be read
716 * from the main emulation structures without investigating the LRs.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800717 */
Andre Przywara83215812014-06-07 00:53:08 +0200718void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800719{
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800720 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100721 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800722
723 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100724 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800725
726 /*
727 * There are three options for the state bits:
728 *
729 * 01: pending
730 * 10: active
731 * 11: pending and active
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800732 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000733 BUG_ON(!(lr.state & LR_STATE_MASK));
734
735 /* Reestablish SGI source for pending and active IRQs */
736 if (lr.irq < VGIC_NR_SGIS)
737 add_sgi_source(vcpu, lr.irq, lr.source);
738
739 /*
740 * If the LR holds an active (10) or a pending and active (11)
741 * interrupt then move the active state to the
742 * distributor tracking bit.
743 */
744 if (lr.state & LR_STATE_ACTIVE) {
745 vgic_irq_set_active(vcpu, lr.irq);
746 lr.state &= ~LR_STATE_ACTIVE;
747 }
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800748
749 /*
750 * Reestablish the pending state on the distributor and the
751 * CPU interface. It may have already been pending, but that
752 * is fine, then we are only setting a few bits that were
753 * already set.
754 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000755 if (lr.state & LR_STATE_PENDING) {
756 vgic_dist_irq_set_pending(vcpu, lr.irq);
757 lr.state &= ~LR_STATE_PENDING;
758 }
759
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100760 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800761
762 /*
Christoffer Dall47a98b12015-03-13 17:02:54 +0000763 * Mark the LR as free for other use.
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800764 */
Christoffer Dall47a98b12015-03-13 17:02:54 +0000765 BUG_ON(lr.state & LR_STATE_MASK);
766 vgic_retire_lr(i, lr.irq, vcpu);
767 vgic_irq_clear_queued(vcpu, lr.irq);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800768
769 /* Finally update the VGIC state. */
770 vgic_update_state(vcpu->kvm);
771 }
772}
773
Andre Przywara83215812014-06-07 00:53:08 +0200774const
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000775struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges,
Andre Przywara9f199d02015-03-26 14:39:33 +0000776 int len, gpa_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500777{
Andre Przywara9f199d02015-03-26 14:39:33 +0000778 while (ranges->len) {
779 if (offset >= ranges->base &&
780 (offset + len) <= (ranges->base + ranges->len))
781 return ranges;
782 ranges++;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500783 }
784
785 return NULL;
786}
787
Marc Zyngierc3c91832014-07-08 12:09:04 +0100788static bool vgic_validate_access(const struct vgic_dist *dist,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000789 const struct vgic_io_range *range,
Marc Zyngierc3c91832014-07-08 12:09:04 +0100790 unsigned long offset)
791{
792 int irq;
793
794 if (!range->bits_per_irq)
795 return true; /* Not an irq-based access */
796
797 irq = offset * 8 / range->bits_per_irq;
798 if (irq >= dist->nr_irqs)
799 return false;
800
801 return true;
802}
803
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200804/*
805 * Call the respective handler function for the given range.
806 * We split up any 64 bit accesses into two consecutive 32 bit
807 * handler calls and merge the result afterwards.
808 * We do this in a little endian fashion regardless of the host's
809 * or guest's endianness, because the GIC is always LE and the rest of
810 * the code (vgic_reg_access) also puts it in a LE fashion already.
811 * At this point we have already identified the handle function, so
812 * range points to that one entry and offset is relative to this.
813 */
814static bool call_range_handler(struct kvm_vcpu *vcpu,
815 struct kvm_exit_mmio *mmio,
816 unsigned long offset,
Andre Przywaracf50a1e2015-03-26 14:39:32 +0000817 const struct vgic_io_range *range)
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200818{
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200819 struct kvm_exit_mmio mmio32;
820 bool ret;
821
822 if (likely(mmio->len <= 4))
823 return range->handle_mmio(vcpu, mmio, offset);
824
825 /*
826 * Any access bigger than 4 bytes (that we currently handle in KVM)
827 * is actually 8 bytes long, caused by a 64-bit access
828 */
829
830 mmio32.len = 4;
831 mmio32.is_write = mmio->is_write;
Andre Przywara9fedf142014-11-13 16:21:35 +0000832 mmio32.private = mmio->private;
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200833
834 mmio32.phys_addr = mmio->phys_addr + 4;
Andre Przywara950324a2015-03-28 01:13:13 +0000835 mmio32.data = &((u32 *)mmio->data)[1];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200836 ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200837
838 mmio32.phys_addr = mmio->phys_addr;
Andre Przywara950324a2015-03-28 01:13:13 +0000839 mmio32.data = &((u32 *)mmio->data)[0];
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200840 ret |= range->handle_mmio(vcpu, &mmio32, offset);
Andre Przywara05bc8aa2014-06-05 16:07:50 +0200841
842 return ret;
843}
844
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500845/**
Andre Przywara6777f772015-03-26 14:39:34 +0000846 * vgic_handle_mmio_access - handle an in-kernel MMIO access
847 * This is called by the read/write KVM IO device wrappers below.
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500848 * @vcpu: pointer to the vcpu performing the access
Andre Przywara6777f772015-03-26 14:39:34 +0000849 * @this: pointer to the KVM IO device in charge
850 * @addr: guest physical address of the access
851 * @len: size of the access
852 * @val: pointer to the data region
853 * @is_write: read or write access
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500854 *
Andre Przywara96415252014-06-02 22:44:37 +0200855 * returns true if the MMIO access could be performed
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500856 */
Andre Przywara6777f772015-03-26 14:39:34 +0000857static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,
858 struct kvm_io_device *this, gpa_t addr,
859 int len, void *val, bool is_write)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500860{
Marc Zyngierb47ef922013-01-21 19:36:14 -0500861 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Andre Przywara6777f772015-03-26 14:39:34 +0000862 struct vgic_io_device *iodev = container_of(this,
863 struct vgic_io_device, dev);
864 struct kvm_run *run = vcpu->run;
865 const struct vgic_io_range *range;
866 struct kvm_exit_mmio mmio;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500867 bool updated_state;
Andre Przywara6777f772015-03-26 14:39:34 +0000868 gpa_t offset;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500869
Andre Przywara6777f772015-03-26 14:39:34 +0000870 offset = addr - iodev->addr;
871 range = vgic_find_range(iodev->reg_ranges, len, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500872 if (unlikely(!range || !range->handle_mmio)) {
Andre Przywara6777f772015-03-26 14:39:34 +0000873 pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len);
874 return -ENXIO;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500875 }
876
Andre Przywara6777f772015-03-26 14:39:34 +0000877 mmio.phys_addr = addr;
878 mmio.len = len;
879 mmio.is_write = is_write;
Andre Przywara950324a2015-03-28 01:13:13 +0000880 mmio.data = val;
Andre Przywara6777f772015-03-26 14:39:34 +0000881 mmio.private = iodev->redist_vcpu;
882
883 spin_lock(&dist->lock);
Andre Przywara96415252014-06-02 22:44:37 +0200884 offset -= range->base;
Marc Zyngierc3c91832014-07-08 12:09:04 +0100885 if (vgic_validate_access(dist, range, offset)) {
Andre Przywara6777f772015-03-26 14:39:34 +0000886 updated_state = call_range_handler(vcpu, &mmio, offset, range);
Marc Zyngierc3c91832014-07-08 12:09:04 +0100887 } else {
Andre Przywara6777f772015-03-26 14:39:34 +0000888 if (!is_write)
889 memset(val, 0, len);
Marc Zyngierc3c91832014-07-08 12:09:04 +0100890 updated_state = false;
891 }
Andre Przywara6777f772015-03-26 14:39:34 +0000892 spin_unlock(&dist->lock);
Andre Przywara950324a2015-03-28 01:13:13 +0000893 run->mmio.is_write = is_write;
894 run->mmio.len = len;
895 run->mmio.phys_addr = addr;
896 memcpy(run->mmio.data, val, len);
897
Marc Zyngierb47ef922013-01-21 19:36:14 -0500898 kvm_handle_mmio_return(vcpu, run);
899
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500900 if (updated_state)
901 vgic_kick_vcpus(vcpu->kvm);
902
Andre Przywara6777f772015-03-26 14:39:34 +0000903 return 0;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500904}
905
Andre Przywara6777f772015-03-26 14:39:34 +0000906static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu,
907 struct kvm_io_device *this,
908 gpa_t addr, int len, void *val)
Andre Przywara96415252014-06-02 22:44:37 +0200909{
Andre Przywara6777f772015-03-26 14:39:34 +0000910 return vgic_handle_mmio_access(vcpu, this, addr, len, val, false);
911}
Andre Przywara96415252014-06-02 22:44:37 +0200912
Andre Przywara6777f772015-03-26 14:39:34 +0000913static int vgic_handle_mmio_write(struct kvm_vcpu *vcpu,
914 struct kvm_io_device *this,
915 gpa_t addr, int len, const void *val)
916{
917 return vgic_handle_mmio_access(vcpu, this, addr, len, (void *)val,
918 true);
919}
920
921struct kvm_io_device_ops vgic_io_ops = {
922 .read = vgic_handle_mmio_read,
923 .write = vgic_handle_mmio_write,
924};
925
926/**
927 * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus
928 * @kvm: The VM structure pointer
929 * @base: The (guest) base address for the register frame
930 * @len: Length of the register frame window
931 * @ranges: Describing the handler functions for each register
932 * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call
933 * @iodev: Points to memory to be passed on to the handler
934 *
935 * @iodev stores the parameters of this function to be usable by the handler
936 * respectively the dispatcher function (since the KVM I/O bus framework lacks
937 * an opaque parameter). Initialization is done in this function, but the
938 * reference should be valid and unique for the whole VGIC lifetime.
939 * If the register frame is not mapped for a specific VCPU, pass -1 to
940 * @redist_vcpu_id.
941 */
942int vgic_register_kvm_io_dev(struct kvm *kvm, gpa_t base, int len,
943 const struct vgic_io_range *ranges,
944 int redist_vcpu_id,
945 struct vgic_io_device *iodev)
946{
947 struct kvm_vcpu *vcpu = NULL;
948 int ret;
949
950 if (redist_vcpu_id >= 0)
951 vcpu = kvm_get_vcpu(kvm, redist_vcpu_id);
952
953 iodev->addr = base;
954 iodev->len = len;
955 iodev->reg_ranges = ranges;
956 iodev->redist_vcpu = vcpu;
957
958 kvm_iodevice_init(&iodev->dev, &vgic_io_ops);
959
960 mutex_lock(&kvm->slots_lock);
961
962 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, base, len,
963 &iodev->dev);
964 mutex_unlock(&kvm->slots_lock);
965
966 /* Mark the iodev as invalid if registration fails. */
967 if (ret)
968 iodev->dev.ops = NULL;
969
970 return ret;
Andre Przywara96415252014-06-02 22:44:37 +0200971}
972
Marc Zyngierfb65ab62014-07-08 12:09:02 +0100973static int vgic_nr_shared_irqs(struct vgic_dist *dist)
974{
975 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
976}
977
Christoffer Dall47a98b12015-03-13 17:02:54 +0000978static int compute_active_for_cpu(struct kvm_vcpu *vcpu)
979{
980 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
981 unsigned long *active, *enabled, *act_percpu, *act_shared;
982 unsigned long active_private, active_shared;
983 int nr_shared = vgic_nr_shared_irqs(dist);
984 int vcpu_id;
985
986 vcpu_id = vcpu->vcpu_id;
987 act_percpu = vcpu->arch.vgic_cpu.active_percpu;
988 act_shared = vcpu->arch.vgic_cpu.active_shared;
989
990 active = vgic_bitmap_get_cpu_map(&dist->irq_active, vcpu_id);
991 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
992 bitmap_and(act_percpu, active, enabled, VGIC_NR_PRIVATE_IRQS);
993
994 active = vgic_bitmap_get_shared_map(&dist->irq_active);
995 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
996 bitmap_and(act_shared, active, enabled, nr_shared);
997 bitmap_and(act_shared, act_shared,
998 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
999 nr_shared);
1000
1001 active_private = find_first_bit(act_percpu, VGIC_NR_PRIVATE_IRQS);
1002 active_shared = find_first_bit(act_shared, nr_shared);
1003
1004 return (active_private < VGIC_NR_PRIVATE_IRQS ||
1005 active_shared < nr_shared);
1006}
1007
Marc Zyngierb47ef922013-01-21 19:36:14 -05001008static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1009{
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001010 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1011 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1012 unsigned long pending_private, pending_shared;
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001013 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001014 int vcpu_id;
1015
1016 vcpu_id = vcpu->vcpu_id;
1017 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1018 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1019
Christoffer Dall0d997492015-10-17 19:05:27 +02001020 if (!dist->enabled) {
1021 bitmap_zero(pend_percpu, VGIC_NR_PRIVATE_IRQS);
1022 bitmap_zero(pend_shared, nr_shared);
1023 return 0;
1024 }
1025
Christoffer Dall227844f2014-06-09 12:27:18 +02001026 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001027 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1028 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1029
Christoffer Dall227844f2014-06-09 12:27:18 +02001030 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001031 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001032 bitmap_and(pend_shared, pending, enabled, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001033 bitmap_and(pend_shared, pend_shared,
1034 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001035 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001036
1037 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001038 pending_shared = find_first_bit(pend_shared, nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001039 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
Marc Zyngierfb65ab62014-07-08 12:09:02 +01001040 pending_shared < vgic_nr_shared_irqs(dist));
Marc Zyngierb47ef922013-01-21 19:36:14 -05001041}
1042
1043/*
1044 * Update the interrupt state and determine which CPUs have pending
Christoffer Dall47a98b12015-03-13 17:02:54 +00001045 * or active interrupts. Must be called with distributor lock held.
Marc Zyngierb47ef922013-01-21 19:36:14 -05001046 */
Andre Przywara83215812014-06-07 00:53:08 +02001047void vgic_update_state(struct kvm *kvm)
Marc Zyngierb47ef922013-01-21 19:36:14 -05001048{
1049 struct vgic_dist *dist = &kvm->arch.vgic;
1050 struct kvm_vcpu *vcpu;
1051 int c;
1052
Marc Zyngierb47ef922013-01-21 19:36:14 -05001053 kvm_for_each_vcpu(c, vcpu, kvm) {
Christoffer Dall47a98b12015-03-13 17:02:54 +00001054 if (compute_pending_for_cpu(vcpu))
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001055 set_bit(c, dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001056
1057 if (compute_active_for_cpu(vcpu))
1058 set_bit(c, dist->irq_active_on_cpu);
1059 else
1060 clear_bit(c, dist->irq_active_on_cpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -05001061 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001062}
Christoffer Dall330690c2013-01-21 19:36:13 -05001063
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001064static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1065{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001066 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001067}
1068
1069static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1070 struct vgic_lr vlr)
1071{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001072 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001073}
1074
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001075static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1076 struct vgic_lr vlr)
1077{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001078 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001079}
1080
1081static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1082{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001083 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001084}
1085
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001086static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1087{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001088 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001089}
1090
Christoffer Dallae705932015-03-13 17:02:56 +00001091static inline void vgic_clear_eisr(struct kvm_vcpu *vcpu)
1092{
1093 vgic_ops->clear_eisr(vcpu);
1094}
1095
Marc Zyngier495dd852013-06-04 11:02:10 +01001096static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1097{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001098 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001099}
1100
Marc Zyngier909d9b52013-06-04 11:24:17 +01001101static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1102{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001103 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001104}
1105
1106static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1107{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001108 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001109}
1110
Andre Przywara83215812014-06-07 00:53:08 +02001111void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001112{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001113 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001114}
1115
Andre Przywara83215812014-06-07 00:53:08 +02001116void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001117{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001118 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001119}
1120
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001121static inline void vgic_enable(struct kvm_vcpu *vcpu)
1122{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001123 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001124}
1125
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001126static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1127{
1128 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1129 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1130
Christoffer Dallcff92112015-10-16 12:41:21 +02001131 /*
1132 * We must transfer the pending state back to the distributor before
1133 * retiring the LR, otherwise we may loose edge-triggered interrupts.
1134 */
1135 if (vlr.state & LR_STATE_PENDING) {
1136 vgic_dist_irq_set_pending(vcpu, irq);
1137 vlr.hwirq = 0;
1138 }
1139
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001140 vlr.state = 0;
1141 vgic_set_lr(vcpu, lr_nr, vlr);
1142 clear_bit(lr_nr, vgic_cpu->lr_used);
1143 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
Christoffer Dallae705932015-03-13 17:02:56 +00001144 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001145}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001146
1147/*
1148 * An interrupt may have been disabled after being made pending on the
1149 * CPU interface (the classic case is a timer running while we're
1150 * rebooting the guest - the interrupt would kick as soon as the CPU
1151 * interface gets enabled, with deadly consequences).
1152 *
1153 * The solution is to examine already active LRs, and check the
1154 * interrupt is still enabled. If not, just retire it.
1155 */
1156static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1157{
1158 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1159 int lr;
1160
Marc Zyngier8f186d52014-02-04 18:13:03 +00001161 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001162 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001163
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001164 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1165 vgic_retire_lr(lr, vlr.irq, vcpu);
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001166 if (vgic_irq_is_queued(vcpu, vlr.irq))
1167 vgic_irq_clear_queued(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001168 }
1169 }
1170}
1171
Alex Bennée71760952015-03-13 17:02:53 +00001172static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1173 int lr_nr, struct vgic_lr vlr)
1174{
Christoffer Dall47a98b12015-03-13 17:02:54 +00001175 if (vgic_irq_is_active(vcpu, irq)) {
1176 vlr.state |= LR_STATE_ACTIVE;
1177 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
1178 vgic_irq_clear_active(vcpu, irq);
1179 vgic_update_state(vcpu->kvm);
Pavel Fedin437f9962015-09-25 17:00:29 +03001180 } else {
1181 WARN_ON(!vgic_dist_irq_is_pending(vcpu, irq));
Alex Bennée71760952015-03-13 17:02:53 +00001182 vlr.state |= LR_STATE_PENDING;
1183 kvm_debug("Set pending: 0x%x\n", vlr.state);
1184 }
1185
1186 if (!vgic_irq_is_edge(vcpu, irq))
1187 vlr.state |= LR_EOI_INT;
1188
Marc Zyngier08fd6462015-06-08 16:06:13 +01001189 if (vlr.irq >= VGIC_NR_SGIS) {
1190 struct irq_phys_map *map;
1191 map = vgic_irq_map_search(vcpu, irq);
1192
Marc Zyngier08fd6462015-06-08 16:06:13 +01001193 if (map) {
Marc Zyngier08fd6462015-06-08 16:06:13 +01001194 vlr.hwirq = map->phys_irq;
1195 vlr.state |= LR_HW;
1196 vlr.state &= ~LR_EOI_INT;
1197
Marc Zyngier08fd6462015-06-08 16:06:13 +01001198 /*
1199 * Make sure we're not going to sample this
1200 * again, as a HW-backed interrupt cannot be
1201 * in the PENDING_ACTIVE stage.
1202 */
1203 vgic_irq_set_queued(vcpu, irq);
1204 }
1205 }
1206
Alex Bennée71760952015-03-13 17:02:53 +00001207 vgic_set_lr(vcpu, lr_nr, vlr);
Paolo Bonzinibf0fb672015-04-07 18:09:20 +02001208 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
Alex Bennée71760952015-03-13 17:02:53 +00001209}
1210
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001211/*
1212 * Queue an interrupt to a CPU virtual interface. Return true on success,
1213 * or false if it wasn't possible to queue it.
Andre Przywara1d916222014-06-07 00:53:08 +02001214 * sgi_source must be zero for any non-SGI interrupts.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001215 */
Andre Przywara83215812014-06-07 00:53:08 +02001216bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001217{
1218 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001219 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001220 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001221 int lr;
1222
1223 /* Sanitize the input... */
1224 BUG_ON(sgi_source_id & ~7);
1225 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001226 BUG_ON(irq >= dist->nr_irqs);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001227
1228 kvm_debug("Queue IRQ%d\n", irq);
1229
1230 lr = vgic_cpu->vgic_irq_lr_map[irq];
1231
1232 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001233 if (lr != LR_EMPTY) {
1234 vlr = vgic_get_lr(vcpu, lr);
1235 if (vlr.source == sgi_source_id) {
1236 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1237 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
Alex Bennée71760952015-03-13 17:02:53 +00001238 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001239 return true;
1240 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001241 }
1242
1243 /* Try to use another LR for this interrupt */
1244 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001245 vgic->nr_lr);
1246 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001247 return false;
1248
1249 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001250 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1251 set_bit(lr, vgic_cpu->lr_used);
1252
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001253 vlr.irq = irq;
1254 vlr.source = sgi_source_id;
Alex Bennée71760952015-03-13 17:02:53 +00001255 vlr.state = 0;
1256 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001257
1258 return true;
1259}
1260
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001261static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1262{
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001263 if (!vgic_can_sample_irq(vcpu, irq))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001264 return true; /* level interrupt, already queued */
1265
1266 if (vgic_queue_irq(vcpu, 0, irq)) {
1267 if (vgic_irq_is_edge(vcpu, irq)) {
Christoffer Dall227844f2014-06-09 12:27:18 +02001268 vgic_dist_irq_clear_pending(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001269 vgic_cpu_irq_clear(vcpu, irq);
1270 } else {
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001271 vgic_irq_set_queued(vcpu, irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001272 }
1273
1274 return true;
1275 }
1276
1277 return false;
1278}
1279
1280/*
1281 * Fill the list registers with pending interrupts before running the
1282 * guest.
1283 */
1284static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1285{
1286 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1287 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001288 unsigned long *pa_percpu, *pa_shared;
Christoffer Dallcff92112015-10-16 12:41:21 +02001289 int i, vcpu_id;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001290 int overflow = 0;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001291 int nr_shared = vgic_nr_shared_irqs(dist);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001292
1293 vcpu_id = vcpu->vcpu_id;
1294
Christoffer Dall47a98b12015-03-13 17:02:54 +00001295 pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu;
1296 pa_shared = vcpu->arch.vgic_cpu.pend_act_shared;
1297
1298 bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu,
1299 VGIC_NR_PRIVATE_IRQS);
1300 bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared,
1301 nr_shared);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001302 /*
1303 * We may not have any pending interrupt, or the interrupts
1304 * may have been serviced from another vcpu. In all cases,
1305 * move along.
1306 */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001307 if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001308 goto epilog;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001309
1310 /* SGIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001311 for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) {
Andre Przywarab26e5fd2014-06-02 16:19:12 +02001312 if (!queue_sgi(vcpu, i))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001313 overflow = 1;
1314 }
1315
1316 /* PPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001317 for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001318 if (!vgic_queue_hwirq(vcpu, i))
1319 overflow = 1;
1320 }
1321
1322 /* SPIs */
Christoffer Dall47a98b12015-03-13 17:02:54 +00001323 for_each_set_bit(i, pa_shared, nr_shared) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001324 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1325 overflow = 1;
1326 }
1327
Christoffer Dall47a98b12015-03-13 17:02:54 +00001328
1329
1330
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001331epilog:
1332 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001333 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001334 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001335 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001336 /*
1337 * We're about to run this VCPU, and we've consumed
1338 * everything the distributor had in store for
1339 * us. Claim we don't have anything pending. We'll
1340 * adjust that if needed while exiting.
1341 */
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001342 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001343 }
1344}
1345
Christoffer Dall91036172015-08-25 22:50:57 +02001346static int process_level_irq(struct kvm_vcpu *vcpu, int lr, struct vgic_lr vlr)
1347{
1348 int level_pending = 0;
1349
1350 vlr.state = 0;
1351 vlr.hwirq = 0;
1352 vgic_set_lr(vcpu, lr, vlr);
1353
1354 /*
1355 * If the IRQ was EOIed (called from vgic_process_maintenance) or it
1356 * went from active to non-active (called from vgic_sync_hwirq) it was
1357 * also ACKed and we we therefore assume we can clear the soft pending
1358 * state (should it had been set) for this interrupt.
1359 *
1360 * Note: if the IRQ soft pending state was set after the IRQ was
1361 * acked, it actually shouldn't be cleared, but we have no way of
1362 * knowing that unless we start trapping ACKs when the soft-pending
1363 * state is set.
1364 */
1365 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1366
1367 /*
1368 * Tell the gic to start sampling the line of this interrupt again.
1369 */
1370 vgic_irq_clear_queued(vcpu, vlr.irq);
1371
1372 /* Any additional pending interrupt? */
1373 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
1374 vgic_cpu_irq_set(vcpu, vlr.irq);
1375 level_pending = 1;
1376 } else {
1377 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
1378 vgic_cpu_irq_clear(vcpu, vlr.irq);
1379 }
1380
1381 /*
1382 * Despite being EOIed, the LR may not have
1383 * been marked as empty.
1384 */
1385 vgic_sync_lr_elrsr(vcpu, lr, vlr);
1386
1387 return level_pending;
1388}
1389
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001390static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1391{
Marc Zyngier495dd852013-06-04 11:02:10 +01001392 u32 status = vgic_get_interrupt_status(vcpu);
Eric Auger649cf732015-03-04 11:14:35 +01001393 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Eric Auger174178f2015-03-04 11:14:36 +01001394 struct kvm *kvm = vcpu->kvm;
Christoffer Dall91036172015-08-25 22:50:57 +02001395 int level_pending = 0;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001396
Marc Zyngier495dd852013-06-04 11:02:10 +01001397 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001398
Marc Zyngier495dd852013-06-04 11:02:10 +01001399 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001400 /*
1401 * Some level interrupts have been EOIed. Clear their
1402 * active bit.
1403 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001404 u64 eisr = vgic_get_eisr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001405 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001406 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001407
Marc Zyngier8f186d52014-02-04 18:13:03 +00001408 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001409 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Christoffer Dall91036172015-08-25 22:50:57 +02001410
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001411 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001412 WARN_ON(vlr.state & LR_STATE_MASK);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001413
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001414
Eric Auger174178f2015-03-04 11:14:36 +01001415 /*
1416 * kvm_notify_acked_irq calls kvm_set_irq()
Christoffer Dall91036172015-08-25 22:50:57 +02001417 * to reset the IRQ level, which grabs the dist->lock
1418 * so we call this before taking the dist->lock.
Eric Auger174178f2015-03-04 11:14:36 +01001419 */
Eric Auger174178f2015-03-04 11:14:36 +01001420 kvm_notify_acked_irq(kvm, 0,
1421 vlr.irq - VGIC_NR_PRIVATE_IRQS);
Christoffer Dall91036172015-08-25 22:50:57 +02001422
Eric Auger174178f2015-03-04 11:14:36 +01001423 spin_lock(&dist->lock);
Christoffer Dall91036172015-08-25 22:50:57 +02001424 level_pending |= process_level_irq(vcpu, lr, vlr);
Eric Auger649cf732015-03-04 11:14:35 +01001425 spin_unlock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001426 }
1427 }
1428
Marc Zyngier495dd852013-06-04 11:02:10 +01001429 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001430 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001431
Christoffer Dallae705932015-03-13 17:02:56 +00001432 /*
1433 * In the next iterations of the vcpu loop, if we sync the vgic state
1434 * after flushing it, but before entering the guest (this happens for
1435 * pending signals and vmid rollovers), then make sure we don't pick
1436 * up any old maintenance interrupts here.
1437 */
1438 vgic_clear_eisr(vcpu);
1439
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001440 return level_pending;
1441}
1442
Marc Zyngier08fd6462015-06-08 16:06:13 +01001443/*
1444 * Save the physical active state, and reset it to inactive.
1445 *
1446 * Return 1 if HW interrupt went from active to inactive, and 0 otherwise.
1447 */
1448static int vgic_sync_hwirq(struct kvm_vcpu *vcpu, struct vgic_lr vlr)
1449{
1450 struct irq_phys_map *map;
1451 int ret;
1452
1453 if (!(vlr.state & LR_HW))
1454 return 0;
1455
1456 map = vgic_irq_map_search(vcpu, vlr.irq);
Christoffer Dall544c5722015-10-17 17:55:12 +02001457 BUG_ON(!map);
Marc Zyngier08fd6462015-06-08 16:06:13 +01001458
1459 ret = irq_get_irqchip_state(map->irq,
1460 IRQCHIP_STATE_ACTIVE,
1461 &map->active);
1462
1463 WARN_ON(ret);
1464
Christoffer Dallcff92112015-10-16 12:41:21 +02001465 if (map->active)
Marc Zyngier08fd6462015-06-08 16:06:13 +01001466 return 0;
Marc Zyngier08fd6462015-06-08 16:06:13 +01001467
1468 return 1;
1469}
1470
Eric Auger649cf732015-03-04 11:14:35 +01001471/* Sync back the VGIC state after a guest run */
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001472static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1473{
1474 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1475 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001476 u64 elrsr;
1477 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001478 int lr, pending;
1479 bool level_pending;
1480
1481 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001482 elrsr = vgic_get_elrsr(vcpu);
Christoffer Dall2df36a52014-09-28 16:04:26 +02001483 elrsr_ptr = u64_to_bitmask(&elrsr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001484
Marc Zyngier08fd6462015-06-08 16:06:13 +01001485 /* Deal with HW interrupts, and clear mappings for empty LRs */
1486 for (lr = 0; lr < vgic->nr_lr; lr++) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001487 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001488
Marc Zyngier08fd6462015-06-08 16:06:13 +01001489 if (!test_bit(lr, vgic_cpu->lr_used))
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001490 continue;
1491
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001492 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier08fd6462015-06-08 16:06:13 +01001493 if (vgic_sync_hwirq(vcpu, vlr)) {
1494 /*
1495 * So this is a HW interrupt that the guest
1496 * EOI-ed. Clean the LR state and allow the
1497 * interrupt to be sampled again.
1498 */
1499 vlr.state = 0;
1500 vlr.hwirq = 0;
1501 vgic_set_lr(vcpu, lr, vlr);
1502 vgic_irq_clear_queued(vcpu, vlr.irq);
1503 set_bit(lr, elrsr_ptr);
1504 }
1505
1506 if (!test_bit(lr, elrsr_ptr))
1507 continue;
1508
1509 clear_bit(lr, vgic_cpu->lr_used);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001510
Marc Zyngier5fb66da2014-07-08 12:09:05 +01001511 BUG_ON(vlr.irq >= dist->nr_irqs);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001512 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001513 }
1514
1515 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001516 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1517 if (level_pending || pending < vgic->nr_lr)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001518 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001519}
1520
1521void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1522{
1523 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1524
1525 if (!irqchip_in_kernel(vcpu->kvm))
1526 return;
1527
1528 spin_lock(&dist->lock);
1529 __kvm_vgic_flush_hwstate(vcpu);
1530 spin_unlock(&dist->lock);
1531}
1532
1533void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1534{
1535 if (!irqchip_in_kernel(vcpu->kvm))
1536 return;
1537
1538 __kvm_vgic_sync_hwstate(vcpu);
1539}
1540
1541int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1542{
1543 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1544
1545 if (!irqchip_in_kernel(vcpu->kvm))
1546 return 0;
1547
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001548 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001549}
1550
Christoffer Dall47a98b12015-03-13 17:02:54 +00001551int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu)
1552{
1553 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1554
1555 if (!irqchip_in_kernel(vcpu->kvm))
1556 return 0;
1557
1558 return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu);
1559}
1560
1561
Andre Przywara83215812014-06-07 00:53:08 +02001562void vgic_kick_vcpus(struct kvm *kvm)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001563{
1564 struct kvm_vcpu *vcpu;
1565 int c;
1566
1567 /*
1568 * We've injected an interrupt, time to find out who deserves
1569 * a good kick...
1570 */
1571 kvm_for_each_vcpu(c, vcpu, kvm) {
1572 if (kvm_vgic_vcpu_pending_irq(vcpu))
1573 kvm_vcpu_kick(vcpu);
1574 }
1575}
1576
1577static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1578{
Christoffer Dall227844f2014-06-09 12:27:18 +02001579 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001580
1581 /*
1582 * Only inject an interrupt if:
1583 * - edge triggered and we have a rising edge
1584 * - level triggered and we change level
1585 */
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001586 if (edge_triggered) {
1587 int state = vgic_dist_irq_is_pending(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001588 return level > state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001589 } else {
1590 int state = vgic_dist_irq_get_level(vcpu, irq);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001591 return level != state;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001592 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001593}
1594
Shannon Zhao016ed392014-11-19 10:11:25 +00001595static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
Marc Zyngier773299a2015-07-24 11:30:43 +01001596 struct irq_phys_map *map,
1597 unsigned int irq_num, bool level)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001598{
1599 struct vgic_dist *dist = &kvm->arch.vgic;
1600 struct kvm_vcpu *vcpu;
Christoffer Dall227844f2014-06-09 12:27:18 +02001601 int edge_triggered, level_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001602 int enabled;
Andre Przywaraa0675c22014-06-07 00:54:51 +02001603 bool ret = true, can_inject = true;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001604
Marc Zyngier773299a2015-07-24 11:30:43 +01001605 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020))
1606 return -EINVAL;
1607
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001608 spin_lock(&dist->lock);
1609
1610 vcpu = kvm_get_vcpu(kvm, cpuid);
Christoffer Dall227844f2014-06-09 12:27:18 +02001611 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1612 level_triggered = !edge_triggered;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001613
1614 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1615 ret = false;
1616 goto out;
1617 }
1618
1619 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1620 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
Andre Przywaraa0675c22014-06-07 00:54:51 +02001621 if (cpuid == VCPU_NOT_ALLOCATED) {
1622 /* Pretend we use CPU0, and prevent injection */
1623 cpuid = 0;
1624 can_inject = false;
1625 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001626 vcpu = kvm_get_vcpu(kvm, cpuid);
1627 }
1628
1629 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1630
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001631 if (level) {
1632 if (level_triggered)
1633 vgic_dist_irq_set_level(vcpu, irq_num);
Christoffer Dall227844f2014-06-09 12:27:18 +02001634 vgic_dist_irq_set_pending(vcpu, irq_num);
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001635 } else {
1636 if (level_triggered) {
1637 vgic_dist_irq_clear_level(vcpu, irq_num);
Pavel Fedin437f9962015-09-25 17:00:29 +03001638 if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) {
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001639 vgic_dist_irq_clear_pending(vcpu, irq_num);
Pavel Fedin437f9962015-09-25 17:00:29 +03001640 vgic_cpu_irq_clear(vcpu, irq_num);
1641 if (!compute_pending_for_cpu(vcpu))
1642 clear_bit(cpuid, dist->irq_pending_on_cpu);
1643 }
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001644 }
wanghaibin7d39f9e32014-11-17 09:27:37 +00001645
1646 ret = false;
1647 goto out;
Christoffer Dallfaa1b462014-06-14 21:54:51 +02001648 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001649
1650 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1651
Andre Przywaraa0675c22014-06-07 00:54:51 +02001652 if (!enabled || !can_inject) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001653 ret = false;
1654 goto out;
1655 }
1656
Christoffer Dalldbf20f92014-06-09 12:55:13 +02001657 if (!vgic_can_sample_irq(vcpu, irq_num)) {
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001658 /*
1659 * Level interrupt in progress, will be picked up
1660 * when EOId.
1661 */
1662 ret = false;
1663 goto out;
1664 }
1665
1666 if (level) {
1667 vgic_cpu_irq_set(vcpu, irq_num);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001668 set_bit(cpuid, dist->irq_pending_on_cpu);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001669 }
1670
1671out:
1672 spin_unlock(&dist->lock);
1673
Marc Zyngier773299a2015-07-24 11:30:43 +01001674 if (ret) {
1675 /* kick the specified vcpu */
1676 kvm_vcpu_kick(kvm_get_vcpu(kvm, cpuid));
1677 }
1678
1679 return 0;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001680}
1681
Marc Zyngier773299a2015-07-24 11:30:43 +01001682static int vgic_lazy_init(struct kvm *kvm)
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001683{
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001684 int ret = 0;
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001685
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001686 if (unlikely(!vgic_initialized(kvm))) {
Andre Przywara598921362014-06-03 09:33:10 +02001687 /*
1688 * We only provide the automatic initialization of the VGIC
1689 * for the legacy case of a GICv2. Any other type must
1690 * be explicitly initialized once setup with the respective
1691 * KVM device call.
1692 */
Marc Zyngier773299a2015-07-24 11:30:43 +01001693 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
1694 return -EBUSY;
1695
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001696 mutex_lock(&kvm->lock);
1697 ret = vgic_init(kvm);
1698 mutex_unlock(&kvm->lock);
Shannon Zhao016ed392014-11-19 10:11:25 +00001699 }
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001700
Marc Zyngier773299a2015-07-24 11:30:43 +01001701 return ret;
1702}
1703
1704/**
1705 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1706 * @kvm: The VM structure pointer
1707 * @cpuid: The CPU for PPIs
1708 * @irq_num: The IRQ number that is assigned to the device. This IRQ
1709 * must not be mapped to a HW interrupt.
1710 * @level: Edge-triggered: true: to trigger the interrupt
1711 * false: to ignore the call
1712 * Level-sensitive true: raise the input signal
1713 * false: lower the input signal
1714 *
1715 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1716 * level-sensitive interrupts. You can think of the level parameter as 1
1717 * being HIGH and 0 being LOW and all devices being active-HIGH.
1718 */
1719int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1720 bool level)
1721{
1722 struct irq_phys_map *map;
1723 int ret;
1724
1725 ret = vgic_lazy_init(kvm);
1726 if (ret)
1727 return ret;
1728
1729 map = vgic_irq_map_search(kvm_get_vcpu(kvm, cpuid), irq_num);
1730 if (map)
Andre Przywarafd1d0dd2015-04-10 16:17:59 +01001731 return -EINVAL;
1732
Marc Zyngier773299a2015-07-24 11:30:43 +01001733 return vgic_update_irq_pending(kvm, cpuid, NULL, irq_num, level);
1734}
Christoffer Dallca7d9c82014-12-09 14:35:33 +01001735
Marc Zyngier773299a2015-07-24 11:30:43 +01001736/**
1737 * kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic
1738 * @kvm: The VM structure pointer
1739 * @cpuid: The CPU for PPIs
1740 * @map: Pointer to a irq_phys_map structure describing the mapping
1741 * @level: Edge-triggered: true: to trigger the interrupt
1742 * false: to ignore the call
1743 * Level-sensitive true: raise the input signal
1744 * false: lower the input signal
1745 *
1746 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1747 * level-sensitive interrupts. You can think of the level parameter as 1
1748 * being HIGH and 0 being LOW and all devices being active-HIGH.
1749 */
1750int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,
1751 struct irq_phys_map *map, bool level)
1752{
1753 int ret;
1754
1755 ret = vgic_lazy_init(kvm);
1756 if (ret)
1757 return ret;
1758
1759 return vgic_update_irq_pending(kvm, cpuid, map, map->virt_irq, level);
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001760}
1761
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001762static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1763{
1764 /*
1765 * We cannot rely on the vgic maintenance interrupt to be
1766 * delivered synchronously. This means we can only use it to
1767 * exit the VM, and we perform the handling of EOIed
1768 * interrupts on the exit path (see vgic_process_maintenance).
1769 */
1770 return IRQ_HANDLED;
1771}
1772
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001773static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,
1774 int virt_irq)
1775{
1776 if (virt_irq < VGIC_NR_PRIVATE_IRQS)
1777 return &vcpu->arch.vgic_cpu.irq_phys_map_list;
1778 else
1779 return &vcpu->kvm->arch.vgic.irq_phys_map_list;
1780}
1781
1782/**
1783 * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ
1784 * @vcpu: The VCPU pointer
1785 * @virt_irq: The virtual irq number
1786 * @irq: The Linux IRQ number
1787 *
1788 * Establish a mapping between a guest visible irq (@virt_irq) and a
1789 * Linux irq (@irq). On injection, @virt_irq will be associated with
1790 * the physical interrupt represented by @irq. This mapping can be
1791 * established multiple times as long as the parameters are the same.
1792 *
1793 * Returns a valid pointer on success, and an error pointer otherwise
1794 */
1795struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
1796 int virt_irq, int irq)
1797{
1798 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1799 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1800 struct irq_phys_map *map;
1801 struct irq_phys_map_entry *entry;
1802 struct irq_desc *desc;
1803 struct irq_data *data;
1804 int phys_irq;
1805
1806 desc = irq_to_desc(irq);
1807 if (!desc) {
1808 kvm_err("%s: no interrupt descriptor\n", __func__);
1809 return ERR_PTR(-EINVAL);
1810 }
1811
1812 data = irq_desc_get_irq_data(desc);
1813 while (data->parent_data)
1814 data = data->parent_data;
1815
1816 phys_irq = data->hwirq;
1817
1818 /* Create a new mapping */
1819 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1820 if (!entry)
1821 return ERR_PTR(-ENOMEM);
1822
1823 spin_lock(&dist->irq_phys_map_lock);
1824
1825 /* Try to match an existing mapping */
1826 map = vgic_irq_map_search(vcpu, virt_irq);
1827 if (map) {
1828 /* Make sure this mapping matches */
1829 if (map->phys_irq != phys_irq ||
1830 map->irq != irq)
1831 map = ERR_PTR(-EINVAL);
1832
1833 /* Found an existing, valid mapping */
1834 goto out;
1835 }
1836
1837 map = &entry->map;
1838 map->virt_irq = virt_irq;
1839 map->phys_irq = phys_irq;
1840 map->irq = irq;
1841
1842 list_add_tail_rcu(&entry->entry, root);
1843
1844out:
1845 spin_unlock(&dist->irq_phys_map_lock);
1846 /* If we've found a hit in the existing list, free the useless
1847 * entry */
1848 if (IS_ERR(map) || map != &entry->map)
1849 kfree(entry);
1850 return map;
1851}
1852
1853static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
1854 int virt_irq)
1855{
1856 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1857 struct irq_phys_map_entry *entry;
1858 struct irq_phys_map *map;
1859
1860 rcu_read_lock();
1861
1862 list_for_each_entry_rcu(entry, root, entry) {
1863 map = &entry->map;
1864 if (map->virt_irq == virt_irq) {
1865 rcu_read_unlock();
1866 return map;
1867 }
1868 }
1869
1870 rcu_read_unlock();
1871
1872 return NULL;
1873}
1874
1875static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)
1876{
1877 struct irq_phys_map_entry *entry;
1878
1879 entry = container_of(rcu, struct irq_phys_map_entry, rcu);
1880 kfree(entry);
1881}
1882
1883/**
Marc Zyngier6e84e0e2015-06-08 16:13:30 +01001884 * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ
1885 *
1886 * Return the logical active state of a mapped interrupt. This doesn't
1887 * necessarily reflects the current HW state.
1888 */
1889bool kvm_vgic_get_phys_irq_active(struct irq_phys_map *map)
1890{
1891 BUG_ON(!map);
1892 return map->active;
1893}
1894
1895/**
1896 * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ
1897 *
1898 * Set the logical active state of a mapped interrupt. This doesn't
1899 * immediately affects the HW state.
1900 */
1901void kvm_vgic_set_phys_irq_active(struct irq_phys_map *map, bool active)
1902{
1903 BUG_ON(!map);
1904 map->active = active;
1905}
1906
1907/**
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001908 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1909 * @vcpu: The VCPU pointer
1910 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
1911 *
1912 * Remove an existing mapping between virtual and physical interrupts.
1913 */
1914int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map)
1915{
1916 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1917 struct irq_phys_map_entry *entry;
1918 struct list_head *root;
1919
1920 if (!map)
1921 return -EINVAL;
1922
1923 root = vgic_get_irq_phys_map_list(vcpu, map->virt_irq);
1924
1925 spin_lock(&dist->irq_phys_map_lock);
1926
1927 list_for_each_entry(entry, root, entry) {
1928 if (&entry->map == map) {
1929 list_del_rcu(&entry->entry);
1930 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1931 break;
1932 }
1933 }
1934
1935 spin_unlock(&dist->irq_phys_map_lock);
1936
1937 return 0;
1938}
1939
1940static void vgic_destroy_irq_phys_map(struct kvm *kvm, struct list_head *root)
1941{
1942 struct vgic_dist *dist = &kvm->arch.vgic;
1943 struct irq_phys_map_entry *entry;
1944
1945 spin_lock(&dist->irq_phys_map_lock);
1946
1947 list_for_each_entry(entry, root, entry) {
1948 list_del_rcu(&entry->entry);
1949 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1950 }
1951
1952 spin_unlock(&dist->irq_phys_map_lock);
1953}
1954
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001955void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1956{
1957 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1958
1959 kfree(vgic_cpu->pending_shared);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001960 kfree(vgic_cpu->active_shared);
1961 kfree(vgic_cpu->pend_act_shared);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001962 kfree(vgic_cpu->vgic_irq_lr_map);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01001963 vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001964 vgic_cpu->pending_shared = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00001965 vgic_cpu->active_shared = NULL;
1966 vgic_cpu->pend_act_shared = NULL;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001967 vgic_cpu->vgic_irq_lr_map = NULL;
1968}
1969
1970static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1971{
1972 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1973
1974 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1975 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00001976 vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
1977 vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001978 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001979
Christoffer Dall47a98b12015-03-13 17:02:54 +00001980 if (!vgic_cpu->pending_shared
1981 || !vgic_cpu->active_shared
1982 || !vgic_cpu->pend_act_shared
1983 || !vgic_cpu->vgic_irq_lr_map) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01001984 kvm_vgic_vcpu_destroy(vcpu);
1985 return -ENOMEM;
1986 }
1987
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001988 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001989
1990 /*
Marc Zyngierca85f622013-06-18 19:17:28 +01001991 * Store the number of LRs per vcpu, so we don't have to go
1992 * all the way to the distributor structure to find out. Only
1993 * assembly code should use this one.
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001994 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001995 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001996
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00001997 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001998}
1999
Andre Przywara3caa2d82014-06-02 16:26:01 +02002000/**
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01002001 * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage
2002 *
2003 * No memory allocation should be performed here, only static init.
2004 */
2005void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
2006{
2007 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
2008 INIT_LIST_HEAD(&vgic_cpu->irq_phys_map_list);
2009}
2010
2011/**
Andre Przywara3caa2d82014-06-02 16:26:01 +02002012 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
2013 *
2014 * The host's GIC naturally limits the maximum amount of VCPUs a guest
2015 * can use.
2016 */
2017int kvm_vgic_get_max_vcpus(void)
2018{
2019 return vgic->max_gic_vcpus;
2020}
2021
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002022void kvm_vgic_destroy(struct kvm *kvm)
2023{
2024 struct vgic_dist *dist = &kvm->arch.vgic;
2025 struct kvm_vcpu *vcpu;
2026 int i;
2027
2028 kvm_for_each_vcpu(i, vcpu, kvm)
2029 kvm_vgic_vcpu_destroy(vcpu);
2030
2031 vgic_free_bitmap(&dist->irq_enabled);
2032 vgic_free_bitmap(&dist->irq_level);
2033 vgic_free_bitmap(&dist->irq_pending);
2034 vgic_free_bitmap(&dist->irq_soft_pend);
2035 vgic_free_bitmap(&dist->irq_queued);
2036 vgic_free_bitmap(&dist->irq_cfg);
2037 vgic_free_bytemap(&dist->irq_priority);
2038 if (dist->irq_spi_target) {
2039 for (i = 0; i < dist->nr_cpus; i++)
2040 vgic_free_bitmap(&dist->irq_spi_target[i]);
2041 }
2042 kfree(dist->irq_sgi_sources);
2043 kfree(dist->irq_spi_cpu);
Andre Przywaraa0675c22014-06-07 00:54:51 +02002044 kfree(dist->irq_spi_mpidr);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002045 kfree(dist->irq_spi_target);
2046 kfree(dist->irq_pending_on_cpu);
Christoffer Dall47a98b12015-03-13 17:02:54 +00002047 kfree(dist->irq_active_on_cpu);
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01002048 vgic_destroy_irq_phys_map(kvm, &dist->irq_phys_map_list);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002049 dist->irq_sgi_sources = NULL;
2050 dist->irq_spi_cpu = NULL;
2051 dist->irq_spi_target = NULL;
2052 dist->irq_pending_on_cpu = NULL;
Christoffer Dall47a98b12015-03-13 17:02:54 +00002053 dist->irq_active_on_cpu = NULL;
Christoffer Dall1f57be22014-12-09 14:30:36 +01002054 dist->nr_cpus = 0;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002055}
2056
2057/*
2058 * Allocate and initialize the various data structures. Must be called
2059 * with kvm->lock held!
2060 */
Andre Przywara83215812014-06-07 00:53:08 +02002061int vgic_init(struct kvm *kvm)
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002062{
2063 struct vgic_dist *dist = &kvm->arch.vgic;
2064 struct kvm_vcpu *vcpu;
2065 int nr_cpus, nr_irqs;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002066 int ret, i, vcpu_id;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002067
Christoffer Dall1f57be22014-12-09 14:30:36 +01002068 if (vgic_initialized(kvm))
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002069 return 0;
Marc Zyngier5fb66da2014-07-08 12:09:05 +01002070
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002071 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
2072 if (!nr_cpus) /* No vcpus? Can't be good... */
Eric Auger66b030e2014-12-15 18:43:32 +01002073 return -ENODEV;
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002074
2075 /*
2076 * If nobody configured the number of interrupts, use the
2077 * legacy one.
2078 */
Marc Zyngier5fb66da2014-07-08 12:09:05 +01002079 if (!dist->nr_irqs)
2080 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
2081
2082 nr_irqs = dist->nr_irqs;
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002083
2084 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
2085 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
2086 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
2087 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
2088 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
Christoffer Dall47a98b12015-03-13 17:02:54 +00002089 ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002090 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
2091 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
2092
2093 if (ret)
2094 goto out;
2095
2096 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
2097 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
2098 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
2099 GFP_KERNEL);
2100 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2101 GFP_KERNEL);
Christoffer Dall47a98b12015-03-13 17:02:54 +00002102 dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
2103 GFP_KERNEL);
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002104 if (!dist->irq_sgi_sources ||
2105 !dist->irq_spi_cpu ||
2106 !dist->irq_spi_target ||
Christoffer Dall47a98b12015-03-13 17:02:54 +00002107 !dist->irq_pending_on_cpu ||
2108 !dist->irq_active_on_cpu) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002109 ret = -ENOMEM;
2110 goto out;
2111 }
2112
2113 for (i = 0; i < nr_cpus; i++)
2114 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
2115 nr_cpus, nr_irqs);
2116
2117 if (ret)
2118 goto out;
2119
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002120 ret = kvm->arch.vgic.vm_ops.init_model(kvm);
2121 if (ret)
2122 goto out;
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002123
2124 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002125 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
2126 if (ret) {
2127 kvm_err("VGIC: Failed to allocate vcpu memory\n");
2128 break;
2129 }
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002130
Christoffer Dall54723bb2015-08-30 14:45:20 +02002131 /*
2132 * Enable all SGIs and configure all private IRQs as
2133 * edge-triggered.
2134 */
2135 for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
2136 if (i < VGIC_NR_SGIS)
Peter Maydell6d3cfbe2014-12-04 15:02:24 +00002137 vgic_bitmap_set_irq_val(&dist->irq_enabled,
2138 vcpu->vcpu_id, i, 1);
2139 if (i < VGIC_NR_PRIVATE_IRQS)
2140 vgic_bitmap_set_irq_val(&dist->irq_cfg,
2141 vcpu->vcpu_id, i,
2142 VGIC_CFG_EDGE);
2143 }
2144
2145 vgic_enable(vcpu);
2146 }
Marc Zyngier4956f2b2014-07-08 12:09:06 +01002147
Marc Zyngierc1bfb572014-07-08 12:09:01 +01002148out:
2149 if (ret)
2150 kvm_vgic_destroy(kvm);
2151
2152 return ret;
2153}
2154
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002155static int init_vgic_model(struct kvm *kvm, int type)
2156{
2157 switch (type) {
2158 case KVM_DEV_TYPE_ARM_VGIC_V2:
2159 vgic_v2_init_emulation(kvm);
2160 break;
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002161#ifdef CONFIG_ARM_GIC_V3
2162 case KVM_DEV_TYPE_ARM_VGIC_V3:
2163 vgic_v3_init_emulation(kvm);
2164 break;
2165#endif
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002166 default:
2167 return -ENODEV;
2168 }
2169
Andre Przywara3caa2d82014-06-02 16:26:01 +02002170 if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
2171 return -E2BIG;
2172
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002173 return 0;
2174}
2175
Marc Zyngier6c3d63c2014-06-23 17:37:18 +01002176/**
2177 * kvm_vgic_early_init - Earliest possible vgic initialization stage
2178 *
2179 * No memory allocation should be performed here, only static init.
2180 */
2181void kvm_vgic_early_init(struct kvm *kvm)
2182{
2183 spin_lock_init(&kvm->arch.vgic.lock);
2184 spin_lock_init(&kvm->arch.vgic.irq_phys_map_lock);
2185 INIT_LIST_HEAD(&kvm->arch.vgic.irq_phys_map_list);
2186}
2187
Andre Przywara598921362014-06-03 09:33:10 +02002188int kvm_vgic_create(struct kvm *kvm, u32 type)
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002189{
Christoffer Dall6b50f542014-11-06 11:47:39 +00002190 int i, vcpu_lock_idx = -1, ret;
Christoffer Dall73306722013-10-25 17:29:18 +01002191 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002192
2193 mutex_lock(&kvm->lock);
2194
Andre Przywara4ce7ebd2014-10-26 23:18:14 +00002195 if (irqchip_in_kernel(kvm)) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002196 ret = -EEXIST;
2197 goto out;
2198 }
2199
Christoffer Dall73306722013-10-25 17:29:18 +01002200 /*
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002201 * This function is also called by the KVM_CREATE_IRQCHIP handler,
2202 * which had no chance yet to check the availability of the GICv2
2203 * emulation. So check this here again. KVM_CREATE_DEVICE does
2204 * the proper checks already.
2205 */
Wei Yongjunb52104e2015-02-27 19:41:45 +08002206 if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) {
2207 ret = -ENODEV;
2208 goto out;
2209 }
Andre Przywarab5d84ff2014-06-03 10:26:03 +02002210
2211 /*
Christoffer Dall73306722013-10-25 17:29:18 +01002212 * Any time a vcpu is run, vcpu_load is called which tries to grab the
2213 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
2214 * that no other VCPUs are run while we create the vgic.
2215 */
Christoffer Dall6b50f542014-11-06 11:47:39 +00002216 ret = -EBUSY;
Christoffer Dall73306722013-10-25 17:29:18 +01002217 kvm_for_each_vcpu(i, vcpu, kvm) {
2218 if (!mutex_trylock(&vcpu->mutex))
2219 goto out_unlock;
2220 vcpu_lock_idx = i;
2221 }
2222
2223 kvm_for_each_vcpu(i, vcpu, kvm) {
Christoffer Dall6b50f542014-11-06 11:47:39 +00002224 if (vcpu->arch.has_run_once)
Christoffer Dall73306722013-10-25 17:29:18 +01002225 goto out_unlock;
Christoffer Dall73306722013-10-25 17:29:18 +01002226 }
Christoffer Dall6b50f542014-11-06 11:47:39 +00002227 ret = 0;
Christoffer Dall73306722013-10-25 17:29:18 +01002228
Andre Przywarab26e5fd2014-06-02 16:19:12 +02002229 ret = init_vgic_model(kvm, type);
2230 if (ret)
2231 goto out_unlock;
2232
Marc Zyngierf982cf42014-05-15 10:03:25 +01002233 kvm->arch.vgic.in_kernel = true;
Andre Przywara598921362014-06-03 09:33:10 +02002234 kvm->arch.vgic.vgic_model = type;
Marc Zyngier8f186d52014-02-04 18:13:03 +00002235 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002236 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
2237 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
Andre Przywaraa0675c22014-06-07 00:54:51 +02002238 kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002239
Christoffer Dall73306722013-10-25 17:29:18 +01002240out_unlock:
2241 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
2242 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
2243 mutex_unlock(&vcpu->mutex);
2244 }
2245
Marc Zyngier01ac5e32013-01-21 19:36:16 -05002246out:
2247 mutex_unlock(&kvm->lock);
2248 return ret;
2249}
2250
Will Deacon1fa451b2014-08-26 15:13:24 +01002251static int vgic_ioaddr_overlap(struct kvm *kvm)
Christoffer Dall330690c2013-01-21 19:36:13 -05002252{
2253 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
2254 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2255
2256 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2257 return 0;
2258 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2259 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2260 return -EBUSY;
2261 return 0;
2262}
2263
2264static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2265 phys_addr_t addr, phys_addr_t size)
2266{
2267 int ret;
2268
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002269 if (addr & ~KVM_PHYS_MASK)
2270 return -E2BIG;
2271
2272 if (addr & (SZ_4K - 1))
2273 return -EINVAL;
2274
Christoffer Dall330690c2013-01-21 19:36:13 -05002275 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2276 return -EEXIST;
2277 if (addr + size < addr)
2278 return -EINVAL;
2279
Haibin Wang30c21172014-04-29 14:49:17 +08002280 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05002281 ret = vgic_ioaddr_overlap(kvm);
2282 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08002283 *ioaddr = VGIC_ADDR_UNDEF;
2284
Christoffer Dall330690c2013-01-21 19:36:13 -05002285 return ret;
2286}
2287
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002288/**
2289 * kvm_vgic_addr - set or get vgic VM base addresses
2290 * @kvm: pointer to the vm struct
Andre Przywaraac3d3732014-06-03 10:26:30 +02002291 * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002292 * @addr: pointer to address value
2293 * @write: if true set the address in the VM address space, if false read the
2294 * address
2295 *
2296 * Set or get the vgic base addresses for the distributor and the virtual CPU
2297 * interface in the VM physical address space. These addresses are properties
2298 * of the emulated core/SoC and therefore user space initially knows this
2299 * information.
2300 */
2301int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05002302{
2303 int r = 0;
2304 struct vgic_dist *vgic = &kvm->arch.vgic;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002305 int type_needed;
2306 phys_addr_t *addr_ptr, block_size;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002307 phys_addr_t alignment;
Christoffer Dall330690c2013-01-21 19:36:13 -05002308
Christoffer Dall330690c2013-01-21 19:36:13 -05002309 mutex_lock(&kvm->lock);
2310 switch (type) {
2311 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Andre Przywaraac3d3732014-06-03 10:26:30 +02002312 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2313 addr_ptr = &vgic->vgic_dist_base;
2314 block_size = KVM_VGIC_V2_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002315 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05002316 break;
2317 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Andre Przywaraac3d3732014-06-03 10:26:30 +02002318 type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
2319 addr_ptr = &vgic->vgic_cpu_base;
2320 block_size = KVM_VGIC_V2_CPU_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002321 alignment = SZ_4K;
Christoffer Dall330690c2013-01-21 19:36:13 -05002322 break;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002323#ifdef CONFIG_ARM_GIC_V3
2324 case KVM_VGIC_V3_ADDR_TYPE_DIST:
2325 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2326 addr_ptr = &vgic->vgic_dist_base;
2327 block_size = KVM_VGIC_V3_DIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002328 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002329 break;
2330 case KVM_VGIC_V3_ADDR_TYPE_REDIST:
2331 type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
2332 addr_ptr = &vgic->vgic_redist_base;
2333 block_size = KVM_VGIC_V3_REDIST_SIZE;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002334 alignment = SZ_64K;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002335 break;
2336#endif
Christoffer Dall330690c2013-01-21 19:36:13 -05002337 default:
2338 r = -ENODEV;
Andre Przywaraac3d3732014-06-03 10:26:30 +02002339 goto out;
Christoffer Dall330690c2013-01-21 19:36:13 -05002340 }
2341
Andre Przywaraac3d3732014-06-03 10:26:30 +02002342 if (vgic->vgic_model != type_needed) {
2343 r = -ENODEV;
2344 goto out;
2345 }
2346
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002347 if (write) {
2348 if (!IS_ALIGNED(*addr, alignment))
2349 r = -EINVAL;
2350 else
2351 r = vgic_ioaddr_assign(kvm, addr_ptr, *addr,
2352 block_size);
2353 } else {
Andre Przywaraac3d3732014-06-03 10:26:30 +02002354 *addr = *addr_ptr;
Andre Przywara4fa96afd2015-01-13 12:02:13 +00002355 }
Andre Przywaraac3d3732014-06-03 10:26:30 +02002356
2357out:
Christoffer Dall330690c2013-01-21 19:36:13 -05002358 mutex_unlock(&kvm->lock);
2359 return r;
2360}
Christoffer Dall73306722013-10-25 17:29:18 +01002361
Andre Przywara83215812014-06-07 00:53:08 +02002362int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002363{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002364 int r;
2365
2366 switch (attr->group) {
2367 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2368 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2369 u64 addr;
2370 unsigned long type = (unsigned long)attr->attr;
2371
2372 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2373 return -EFAULT;
2374
2375 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2376 return (r == -ENODEV) ? -ENXIO : r;
2377 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002378 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2379 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2380 u32 val;
2381 int ret = 0;
2382
2383 if (get_user(val, uaddr))
2384 return -EFAULT;
2385
2386 /*
2387 * We require:
2388 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2389 * - at most 1024 interrupts
2390 * - a multiple of 32 interrupts
2391 */
2392 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2393 val > VGIC_MAX_IRQS ||
2394 (val & 31))
2395 return -EINVAL;
2396
2397 mutex_lock(&dev->kvm->lock);
2398
Christoffer Dallc52edf52014-12-09 14:28:09 +01002399 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002400 ret = -EBUSY;
2401 else
2402 dev->kvm->arch.vgic.nr_irqs = val;
2403
2404 mutex_unlock(&dev->kvm->lock);
2405
2406 return ret;
2407 }
Eric Auger065c0032014-12-15 18:43:33 +01002408 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2409 switch (attr->attr) {
2410 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2411 r = vgic_init(dev->kvm);
2412 return r;
2413 }
2414 break;
2415 }
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002416 }
2417
Christoffer Dall73306722013-10-25 17:29:18 +01002418 return -ENXIO;
2419}
2420
Andre Przywara83215812014-06-07 00:53:08 +02002421int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Christoffer Dall73306722013-10-25 17:29:18 +01002422{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002423 int r = -ENXIO;
2424
2425 switch (attr->group) {
2426 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2427 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2428 u64 addr;
2429 unsigned long type = (unsigned long)attr->attr;
2430
2431 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2432 if (r)
2433 return (r == -ENODEV) ? -ENXIO : r;
2434
2435 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2436 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002437 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002438 }
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002439 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2440 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
Andre Przywarab60da142014-08-21 11:08:27 +01002441
Marc Zyngiera98f26f2014-07-08 12:09:07 +01002442 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2443 break;
2444 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01002445
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002446 }
2447
2448 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01002449}
2450
Andre Przywaracf50a1e2015-03-26 14:39:32 +00002451int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +01002452{
Andre Przywara9f199d02015-03-26 14:39:33 +00002453 if (vgic_find_range(ranges, 4, offset))
Christoffer Dallc07a0192013-10-25 21:17:31 +01002454 return 0;
2455 else
2456 return -ENXIO;
2457}
2458
Will Deaconc06a8412014-09-02 10:27:34 +01002459static void vgic_init_maintenance_interrupt(void *info)
2460{
2461 enable_percpu_irq(vgic->maint_irq, 0);
2462}
2463
2464static int vgic_cpu_notify(struct notifier_block *self,
2465 unsigned long action, void *cpu)
2466{
2467 switch (action) {
2468 case CPU_STARTING:
2469 case CPU_STARTING_FROZEN:
2470 vgic_init_maintenance_interrupt(NULL);
2471 break;
2472 case CPU_DYING:
2473 case CPU_DYING_FROZEN:
2474 disable_percpu_irq(vgic->maint_irq);
2475 break;
2476 }
2477
2478 return NOTIFY_OK;
2479}
2480
2481static struct notifier_block vgic_cpu_nb = {
2482 .notifier_call = vgic_cpu_notify,
2483};
2484
2485static const struct of_device_id vgic_ids[] = {
Mark Rutland0f3724752015-03-05 14:47:44 +00002486 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2487 { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, },
2488 { .compatible = "arm,gic-400", .data = vgic_v2_probe, },
2489 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
Will Deaconc06a8412014-09-02 10:27:34 +01002490 {},
2491};
2492
2493int kvm_vgic_hyp_init(void)
2494{
2495 const struct of_device_id *matched_id;
Christoffer Dalla875daf2014-09-18 18:15:32 -07002496 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2497 const struct vgic_params **);
Will Deaconc06a8412014-09-02 10:27:34 +01002498 struct device_node *vgic_node;
2499 int ret;
2500
2501 vgic_node = of_find_matching_node_and_match(NULL,
2502 vgic_ids, &matched_id);
2503 if (!vgic_node) {
2504 kvm_err("error: no compatible GIC node found\n");
2505 return -ENODEV;
2506 }
2507
2508 vgic_probe = matched_id->data;
2509 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2510 if (ret)
2511 return ret;
2512
2513 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2514 "vgic", kvm_get_running_vcpus());
2515 if (ret) {
2516 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2517 return ret;
2518 }
2519
2520 ret = __register_cpu_notifier(&vgic_cpu_nb);
2521 if (ret) {
2522 kvm_err("Cannot register vgic CPU notifier\n");
2523 goto out_free_irq;
2524 }
2525
Will Deaconc06a8412014-09-02 10:27:34 +01002526 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2527
Andre Przywaraea2f83a2014-10-26 23:17:00 +00002528 return 0;
Will Deaconc06a8412014-09-02 10:27:34 +01002529
2530out_free_irq:
2531 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2532 return ret;
2533}
Eric Auger174178f2015-03-04 11:14:36 +01002534
2535int kvm_irq_map_gsi(struct kvm *kvm,
2536 struct kvm_kernel_irq_routing_entry *entries,
2537 int gsi)
2538{
Eric Auger0b3289e2015-04-13 15:01:59 +02002539 return 0;
Eric Auger174178f2015-03-04 11:14:36 +01002540}
2541
2542int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
2543{
2544 return pin;
2545}
2546
2547int kvm_set_irq(struct kvm *kvm, int irq_source_id,
2548 u32 irq, int level, bool line_status)
2549{
2550 unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS;
2551
2552 trace_kvm_set_irq(irq, level, irq_source_id);
2553
2554 BUG_ON(!vgic_initialized(kvm));
2555
Eric Auger174178f2015-03-04 11:14:36 +01002556 return kvm_vgic_inject_irq(kvm, 0, spi, level);
Eric Auger174178f2015-03-04 11:14:36 +01002557}
2558
2559/* MSI not implemented yet */
2560int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
2561 struct kvm *kvm, int irq_source_id,
2562 int level, bool line_status)
2563{
2564 return 0;
2565}