arm/arm64: KVM: pass down user space provided GIC type into vGIC code
[linux-2.6-block.git] / virt / kvm / arm / vgic.c
CommitLineData
1a89dd91
MZ
1/*
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
01ac5e34 19#include <linux/cpu.h>
1a89dd91
MZ
20#include <linux/kvm.h>
21#include <linux/kvm_host.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
01ac5e34
MZ
24#include <linux/of.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
2a2f3e26 27#include <linux/uaccess.h>
01ac5e34
MZ
28
29#include <linux/irqchip/arm-gic.h>
30
1a89dd91 31#include <asm/kvm_emulate.h>
01ac5e34
MZ
32#include <asm/kvm_arm.h>
33#include <asm/kvm_mmu.h>
1a89dd91 34
b47ef92a
MZ
35/*
36 * How the whole thing works (courtesy of Christoffer Dall):
37 *
38 * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
7e362919
CD
39 * something is pending on the CPU interface.
40 * - Interrupts that are pending on the distributor are stored on the
41 * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
42 * ioctls and guest mmio ops, and other in-kernel peripherals such as the
43 * arch. timers).
b47ef92a
MZ
44 * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
45 * recalculated
46 * - To calculate the oracle, we need info for each cpu from
47 * compute_pending_for_cpu, which considers:
227844f5
CD
48 * - PPI: dist->irq_pending & dist->irq_enable
49 * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
7e362919 50 * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
b47ef92a
MZ
51 * registers, stored on each vcpu. We only keep one bit of
52 * information per interrupt, making sure that only one vcpu can
53 * accept the interrupt.
7e362919 54 * - If any of the above state changes, we must recalculate the oracle.
b47ef92a
MZ
55 * - The same is true when injecting an interrupt, except that we only
56 * consider a single interrupt at a time. The irq_spi_cpu array
57 * contains the target CPU for each SPI.
58 *
59 * The handling of level interrupts adds some extra complexity. We
60 * need to track when the interrupt has been EOIed, so we can sample
61 * the 'line' again. This is achieved as such:
62 *
63 * - When a level interrupt is moved onto a vcpu, the corresponding
dbf20f9d 64 * bit in irq_queued is set. As long as this bit is set, the line
b47ef92a
MZ
65 * will be ignored for further interrupts. The interrupt is injected
66 * into the vcpu with the GICH_LR_EOI bit set (generate a
67 * maintenance interrupt on EOI).
68 * - When the interrupt is EOIed, the maintenance interrupt fires,
dbf20f9d 69 * and clears the corresponding bit in irq_queued. This allows the
b47ef92a 70 * interrupt line to be sampled again.
faa1b46c
CD
71 * - Note that level-triggered interrupts can also be set to pending from
72 * writes to GICD_ISPENDRn and lowering the external input line does not
73 * cause the interrupt to become inactive in such a situation.
74 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
75 * inactive as long as the external input line is held high.
b47ef92a
MZ
76 */
77
330690cd
CD
78#define VGIC_ADDR_UNDEF (-1)
79#define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
80
fa20f5ae
CD
81#define PRODUCT_ID_KVM 0x4b /* ASCII code K */
82#define IMPLEMENTER_ARM 0x43b
83#define GICC_ARCH_VERSION_V2 0x2
84
1a89dd91
MZ
85#define ACCESS_READ_VALUE (1 << 0)
86#define ACCESS_READ_RAZ (0 << 0)
87#define ACCESS_READ_MASK(x) ((x) & (1 << 0))
88#define ACCESS_WRITE_IGNORED (0 << 1)
89#define ACCESS_WRITE_SETBIT (1 << 1)
90#define ACCESS_WRITE_CLEARBIT (2 << 1)
91#define ACCESS_WRITE_VALUE (3 << 1)
92#define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
93
6d3cfbe2 94static int vgic_init(struct kvm *kvm);
a1fcb44e 95static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
8d5c6b06 96static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
b47ef92a 97static void vgic_update_state(struct kvm *kvm);
5863c2ce 98static void vgic_kick_vcpus(struct kvm *kvm);
c1bfb577 99static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi);
b47ef92a 100static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
8d5c6b06
MZ
101static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
102static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
beee38b9
MZ
103static void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
104static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
01ac5e34 105
8f186d52
MZ
106static const struct vgic_ops *vgic_ops;
107static const struct vgic_params *vgic;
b47ef92a 108
9662fb48 109/*
c1bfb577
MZ
110 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
111 * extracts u32s out of them.
9662fb48
VK
112 *
113 * This does not work on 64-bit BE systems, because the bitmap access
114 * will store two consecutive 32-bit words with the higher-addressed
115 * register's bits at the lower index and the lower-addressed register's
116 * bits at the higher index.
117 *
118 * Therefore, swizzle the register index when accessing the 32-bit word
119 * registers to access the right register's value.
120 */
121#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
122#define REG_OFFSET_SWIZZLE 1
123#else
124#define REG_OFFSET_SWIZZLE 0
125#endif
b47ef92a 126
c1bfb577
MZ
127static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
128{
129 int nr_longs;
130
131 nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
132
133 b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
134 if (!b->private)
135 return -ENOMEM;
136
137 b->shared = b->private + nr_cpus;
138
139 return 0;
140}
141
142static void vgic_free_bitmap(struct vgic_bitmap *b)
143{
144 kfree(b->private);
145 b->private = NULL;
146 b->shared = NULL;
147}
148
2df36a5d
CD
149/*
150 * Call this function to convert a u64 value to an unsigned long * bitmask
151 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
152 *
153 * Warning: Calling this function may modify *val.
154 */
155static unsigned long *u64_to_bitmask(u64 *val)
156{
157#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
158 *val = (*val >> 32) | (*val << 32);
159#endif
160 return (unsigned long *)val;
161}
162
b47ef92a
MZ
163static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
164 int cpuid, u32 offset)
165{
166 offset >>= 2;
167 if (!offset)
c1bfb577 168 return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
b47ef92a 169 else
c1bfb577 170 return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
b47ef92a
MZ
171}
172
173static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
174 int cpuid, int irq)
175{
176 if (irq < VGIC_NR_PRIVATE_IRQS)
c1bfb577 177 return test_bit(irq, x->private + cpuid);
b47ef92a 178
c1bfb577 179 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
b47ef92a
MZ
180}
181
182static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
183 int irq, int val)
184{
185 unsigned long *reg;
186
187 if (irq < VGIC_NR_PRIVATE_IRQS) {
c1bfb577 188 reg = x->private + cpuid;
b47ef92a 189 } else {
c1bfb577 190 reg = x->shared;
b47ef92a
MZ
191 irq -= VGIC_NR_PRIVATE_IRQS;
192 }
193
194 if (val)
195 set_bit(irq, reg);
196 else
197 clear_bit(irq, reg);
198}
199
200static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
201{
c1bfb577 202 return x->private + cpuid;
b47ef92a
MZ
203}
204
205static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
206{
c1bfb577
MZ
207 return x->shared;
208}
209
210static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
211{
212 int size;
213
214 size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
215 size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
216
217 x->private = kzalloc(size, GFP_KERNEL);
218 if (!x->private)
219 return -ENOMEM;
220
221 x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
222 return 0;
223}
224
225static void vgic_free_bytemap(struct vgic_bytemap *b)
226{
227 kfree(b->private);
228 b->private = NULL;
229 b->shared = NULL;
b47ef92a
MZ
230}
231
232static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
233{
c1bfb577
MZ
234 u32 *reg;
235
236 if (offset < VGIC_NR_PRIVATE_IRQS) {
237 reg = x->private;
238 offset += cpuid * VGIC_NR_PRIVATE_IRQS;
239 } else {
240 reg = x->shared;
241 offset -= VGIC_NR_PRIVATE_IRQS;
242 }
243
244 return reg + (offset / sizeof(u32));
b47ef92a
MZ
245}
246
247#define VGIC_CFG_LEVEL 0
248#define VGIC_CFG_EDGE 1
249
250static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
251{
252 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
253 int irq_val;
254
255 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
256 return irq_val == VGIC_CFG_EDGE;
257}
258
259static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
260{
261 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
262
263 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
264}
265
dbf20f9d 266static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
9d949dce
MZ
267{
268 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
269
dbf20f9d 270 return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
9d949dce
MZ
271}
272
dbf20f9d 273static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
9d949dce
MZ
274{
275 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
276
dbf20f9d 277 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
9d949dce
MZ
278}
279
dbf20f9d 280static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
9d949dce
MZ
281{
282 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
283
dbf20f9d 284 vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
9d949dce
MZ
285}
286
faa1b46c
CD
287static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
288{
289 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
290
291 return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
292}
293
294static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
295{
296 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
297
298 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
299}
300
301static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
302{
303 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
304
305 vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
306}
307
308static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
309{
310 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
311
312 return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
313}
314
315static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
316{
317 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
318
319 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
320}
321
9d949dce
MZ
322static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
323{
324 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
325
227844f5 326 return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
9d949dce
MZ
327}
328
227844f5 329static void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
b47ef92a
MZ
330{
331 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
332
227844f5 333 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
b47ef92a
MZ
334}
335
227844f5 336static void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
b47ef92a
MZ
337{
338 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
339
227844f5 340 vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
b47ef92a
MZ
341}
342
343static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
344{
345 if (irq < VGIC_NR_PRIVATE_IRQS)
346 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
347 else
348 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
349 vcpu->arch.vgic_cpu.pending_shared);
350}
351
352static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
353{
354 if (irq < VGIC_NR_PRIVATE_IRQS)
355 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
356 else
357 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
358 vcpu->arch.vgic_cpu.pending_shared);
359}
360
dbf20f9d
CD
361static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
362{
363 return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq);
364}
365
1a89dd91
MZ
366static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
367{
1c9f0471 368 return le32_to_cpu(*((u32 *)mmio->data)) & mask;
1a89dd91
MZ
369}
370
371static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
372{
1c9f0471 373 *((u32 *)mmio->data) = cpu_to_le32(value) & mask;
1a89dd91
MZ
374}
375
376/**
377 * vgic_reg_access - access vgic register
378 * @mmio: pointer to the data describing the mmio access
379 * @reg: pointer to the virtual backing of vgic distributor data
380 * @offset: least significant 2 bits used for word offset
381 * @mode: ACCESS_ mode (see defines above)
382 *
383 * Helper to make vgic register access easier using one of the access
384 * modes defined for vgic register access
385 * (read,raz,write-ignored,setbit,clearbit,write)
386 */
387static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
388 phys_addr_t offset, int mode)
389{
390 int word_offset = (offset & 3) * 8;
391 u32 mask = (1UL << (mmio->len * 8)) - 1;
392 u32 regval;
393
394 /*
395 * Any alignment fault should have been delivered to the guest
396 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
397 */
398
399 if (reg) {
400 regval = *reg;
401 } else {
402 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
403 regval = 0;
404 }
405
406 if (mmio->is_write) {
407 u32 data = mmio_data_read(mmio, mask) << word_offset;
408 switch (ACCESS_WRITE_MASK(mode)) {
409 case ACCESS_WRITE_IGNORED:
410 return;
411
412 case ACCESS_WRITE_SETBIT:
413 regval |= data;
414 break;
415
416 case ACCESS_WRITE_CLEARBIT:
417 regval &= ~data;
418 break;
419
420 case ACCESS_WRITE_VALUE:
421 regval = (regval & ~(mask << word_offset)) | data;
422 break;
423 }
424 *reg = regval;
425 } else {
426 switch (ACCESS_READ_MASK(mode)) {
427 case ACCESS_READ_RAZ:
428 regval = 0;
429 /* fall through */
430
431 case ACCESS_READ_VALUE:
432 mmio_data_write(mmio, mask, regval >> word_offset);
433 }
434 }
435}
436
b47ef92a
MZ
437static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
438 struct kvm_exit_mmio *mmio, phys_addr_t offset)
439{
440 u32 reg;
441 u32 word_offset = offset & 3;
442
443 switch (offset & ~3) {
fa20f5ae 444 case 0: /* GICD_CTLR */
b47ef92a
MZ
445 reg = vcpu->kvm->arch.vgic.enabled;
446 vgic_reg_access(mmio, &reg, word_offset,
447 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
448 if (mmio->is_write) {
449 vcpu->kvm->arch.vgic.enabled = reg & 1;
450 vgic_update_state(vcpu->kvm);
451 return true;
452 }
453 break;
454
fa20f5ae 455 case 4: /* GICD_TYPER */
b47ef92a 456 reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
5fb66da6 457 reg |= (vcpu->kvm->arch.vgic.nr_irqs >> 5) - 1;
b47ef92a
MZ
458 vgic_reg_access(mmio, &reg, word_offset,
459 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
460 break;
461
fa20f5ae
CD
462 case 8: /* GICD_IIDR */
463 reg = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
b47ef92a
MZ
464 vgic_reg_access(mmio, &reg, word_offset,
465 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
466 break;
467 }
468
469 return false;
470}
471
472static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
473 struct kvm_exit_mmio *mmio, phys_addr_t offset)
474{
475 vgic_reg_access(mmio, NULL, offset,
476 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
477 return false;
478}
479
480static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
481 struct kvm_exit_mmio *mmio,
482 phys_addr_t offset)
483{
484 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
485 vcpu->vcpu_id, offset);
486 vgic_reg_access(mmio, reg, offset,
487 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
488 if (mmio->is_write) {
489 vgic_update_state(vcpu->kvm);
490 return true;
491 }
492
493 return false;
494}
495
496static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
497 struct kvm_exit_mmio *mmio,
498 phys_addr_t offset)
499{
500 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
501 vcpu->vcpu_id, offset);
502 vgic_reg_access(mmio, reg, offset,
503 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
504 if (mmio->is_write) {
505 if (offset < 4) /* Force SGI enabled */
506 *reg |= 0xffff;
a1fcb44e 507 vgic_retire_disabled_irqs(vcpu);
b47ef92a
MZ
508 vgic_update_state(vcpu->kvm);
509 return true;
510 }
511
512 return false;
513}
514
515static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
516 struct kvm_exit_mmio *mmio,
517 phys_addr_t offset)
518{
9da48b55 519 u32 *reg, orig;
faa1b46c
CD
520 u32 level_mask;
521 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
522
523 reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu->vcpu_id, offset);
524 level_mask = (~(*reg));
525
526 /* Mark both level and edge triggered irqs as pending */
527 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
9da48b55 528 orig = *reg;
b47ef92a
MZ
529 vgic_reg_access(mmio, reg, offset,
530 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
faa1b46c 531
b47ef92a 532 if (mmio->is_write) {
faa1b46c
CD
533 /* Set the soft-pending flag only for level-triggered irqs */
534 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
535 vcpu->vcpu_id, offset);
536 vgic_reg_access(mmio, reg, offset,
537 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
538 *reg &= level_mask;
539
9da48b55
CD
540 /* Ignore writes to SGIs */
541 if (offset < 2) {
542 *reg &= ~0xffff;
543 *reg |= orig & 0xffff;
544 }
545
b47ef92a
MZ
546 vgic_update_state(vcpu->kvm);
547 return true;
548 }
549
550 return false;
551}
552
553static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
554 struct kvm_exit_mmio *mmio,
555 phys_addr_t offset)
556{
faa1b46c 557 u32 *level_active;
9da48b55 558 u32 *reg, orig;
faa1b46c
CD
559 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
560
561 reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
9da48b55 562 orig = *reg;
b47ef92a
MZ
563 vgic_reg_access(mmio, reg, offset,
564 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
565 if (mmio->is_write) {
faa1b46c
CD
566 /* Re-set level triggered level-active interrupts */
567 level_active = vgic_bitmap_get_reg(&dist->irq_level,
568 vcpu->vcpu_id, offset);
569 reg = vgic_bitmap_get_reg(&dist->irq_pending,
570 vcpu->vcpu_id, offset);
571 *reg |= *level_active;
572
9da48b55
CD
573 /* Ignore writes to SGIs */
574 if (offset < 2) {
575 *reg &= ~0xffff;
576 *reg |= orig & 0xffff;
577 }
578
faa1b46c
CD
579 /* Clear soft-pending flags */
580 reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
581 vcpu->vcpu_id, offset);
582 vgic_reg_access(mmio, reg, offset,
583 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
584
b47ef92a
MZ
585 vgic_update_state(vcpu->kvm);
586 return true;
587 }
588
589 return false;
590}
591
592static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
593 struct kvm_exit_mmio *mmio,
594 phys_addr_t offset)
595{
596 u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
597 vcpu->vcpu_id, offset);
598 vgic_reg_access(mmio, reg, offset,
599 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
600 return false;
601}
602
603#define GICD_ITARGETSR_SIZE 32
604#define GICD_CPUTARGETS_BITS 8
605#define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
606static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
607{
608 struct vgic_dist *dist = &kvm->arch.vgic;
986af8e0 609 int i;
b47ef92a
MZ
610 u32 val = 0;
611
612 irq -= VGIC_NR_PRIVATE_IRQS;
613
986af8e0
MZ
614 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
615 val |= 1 << (dist->irq_spi_cpu[irq + i] + i * 8);
b47ef92a
MZ
616
617 return val;
618}
619
620static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
621{
622 struct vgic_dist *dist = &kvm->arch.vgic;
623 struct kvm_vcpu *vcpu;
624 int i, c;
625 unsigned long *bmap;
626 u32 target;
627
628 irq -= VGIC_NR_PRIVATE_IRQS;
629
630 /*
631 * Pick the LSB in each byte. This ensures we target exactly
632 * one vcpu per IRQ. If the byte is null, assume we target
633 * CPU0.
634 */
635 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
636 int shift = i * GICD_CPUTARGETS_BITS;
637 target = ffs((val >> shift) & 0xffU);
638 target = target ? (target - 1) : 0;
639 dist->irq_spi_cpu[irq + i] = target;
640 kvm_for_each_vcpu(c, vcpu, kvm) {
641 bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
642 if (c == target)
643 set_bit(irq + i, bmap);
644 else
645 clear_bit(irq + i, bmap);
646 }
647 }
648}
649
650static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
651 struct kvm_exit_mmio *mmio,
652 phys_addr_t offset)
653{
654 u32 reg;
655
656 /* We treat the banked interrupts targets as read-only */
657 if (offset < 32) {
658 u32 roreg = 1 << vcpu->vcpu_id;
659 roreg |= roreg << 8;
660 roreg |= roreg << 16;
661
662 vgic_reg_access(mmio, &roreg, offset,
663 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
664 return false;
665 }
666
667 reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
668 vgic_reg_access(mmio, &reg, offset,
669 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
670 if (mmio->is_write) {
671 vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
672 vgic_update_state(vcpu->kvm);
673 return true;
674 }
675
676 return false;
677}
678
679static u32 vgic_cfg_expand(u16 val)
680{
681 u32 res = 0;
682 int i;
683
684 /*
685 * Turn a 16bit value like abcd...mnop into a 32bit word
686 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
687 */
688 for (i = 0; i < 16; i++)
689 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
690
691 return res;
692}
693
694static u16 vgic_cfg_compress(u32 val)
695{
696 u16 res = 0;
697 int i;
698
699 /*
700 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
701 * abcd...mnop which is what we really care about.
702 */
703 for (i = 0; i < 16; i++)
704 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
705
706 return res;
707}
708
709/*
710 * The distributor uses 2 bits per IRQ for the CFG register, but the
711 * LSB is always 0. As such, we only keep the upper bit, and use the
712 * two above functions to compress/expand the bits
713 */
714static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
715 struct kvm_exit_mmio *mmio, phys_addr_t offset)
716{
717 u32 val;
6545eae3
MZ
718 u32 *reg;
719
6545eae3 720 reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
f2ae85b2 721 vcpu->vcpu_id, offset >> 1);
6545eae3 722
f2ae85b2 723 if (offset & 4)
b47ef92a
MZ
724 val = *reg >> 16;
725 else
726 val = *reg & 0xffff;
727
728 val = vgic_cfg_expand(val);
729 vgic_reg_access(mmio, &val, offset,
730 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
731 if (mmio->is_write) {
f2ae85b2 732 if (offset < 8) {
b47ef92a
MZ
733 *reg = ~0U; /* Force PPIs/SGIs to 1 */
734 return false;
735 }
736
737 val = vgic_cfg_compress(val);
f2ae85b2 738 if (offset & 4) {
b47ef92a
MZ
739 *reg &= 0xffff;
740 *reg |= val << 16;
741 } else {
742 *reg &= 0xffff << 16;
743 *reg |= val;
744 }
745 }
746
747 return false;
748}
749
750static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
751 struct kvm_exit_mmio *mmio, phys_addr_t offset)
752{
753 u32 reg;
754 vgic_reg_access(mmio, &reg, offset,
755 ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
756 if (mmio->is_write) {
757 vgic_dispatch_sgi(vcpu, reg);
758 vgic_update_state(vcpu->kvm);
759 return true;
760 }
761
762 return false;
763}
764
cbd333a4
CD
765/**
766 * vgic_unqueue_irqs - move pending IRQs from LRs to the distributor
767 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
768 *
769 * Move any pending IRQs that have already been assigned to LRs back to the
770 * emulated distributor state so that the complete emulated state can be read
771 * from the main emulation structures without investigating the LRs.
772 *
773 * Note that IRQs in the active state in the LRs get their pending state moved
774 * to the distributor but the active state stays in the LRs, because we don't
775 * track the active state on the distributor side.
776 */
777static void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
778{
779 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
780 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
781 int vcpu_id = vcpu->vcpu_id;
8d5c6b06 782 int i;
cbd333a4
CD
783
784 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
8d5c6b06 785 struct vgic_lr lr = vgic_get_lr(vcpu, i);
cbd333a4
CD
786
787 /*
788 * There are three options for the state bits:
789 *
790 * 01: pending
791 * 10: active
792 * 11: pending and active
793 *
794 * If the LR holds only an active interrupt (not pending) then
795 * just leave it alone.
796 */
8d5c6b06 797 if ((lr.state & LR_STATE_MASK) == LR_STATE_ACTIVE)
cbd333a4
CD
798 continue;
799
800 /*
801 * Reestablish the pending state on the distributor and the
802 * CPU interface. It may have already been pending, but that
803 * is fine, then we are only setting a few bits that were
804 * already set.
805 */
227844f5 806 vgic_dist_irq_set_pending(vcpu, lr.irq);
8d5c6b06 807 if (lr.irq < VGIC_NR_SGIS)
c1bfb577 808 *vgic_get_sgi_sources(dist, vcpu_id, lr.irq) |= 1 << lr.source;
8d5c6b06
MZ
809 lr.state &= ~LR_STATE_PENDING;
810 vgic_set_lr(vcpu, i, lr);
cbd333a4
CD
811
812 /*
813 * If there's no state left on the LR (it could still be
814 * active), then the LR does not hold any useful info and can
815 * be marked as free for other use.
816 */
cced50c9 817 if (!(lr.state & LR_STATE_MASK)) {
8d5c6b06 818 vgic_retire_lr(i, lr.irq, vcpu);
cced50c9
CD
819 vgic_irq_clear_queued(vcpu, lr.irq);
820 }
cbd333a4
CD
821
822 /* Finally update the VGIC state. */
823 vgic_update_state(vcpu->kvm);
824 }
825}
826
90a5355e
CD
827/* Handle reads of GICD_CPENDSGIRn and GICD_SPENDSGIRn */
828static bool read_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
829 struct kvm_exit_mmio *mmio,
830 phys_addr_t offset)
c07a0191 831{
90a5355e
CD
832 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
833 int sgi;
0fea6d76 834 int min_sgi = (offset & ~0x3);
90a5355e
CD
835 int max_sgi = min_sgi + 3;
836 int vcpu_id = vcpu->vcpu_id;
837 u32 reg = 0;
838
839 /* Copy source SGIs from distributor side */
840 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
841 int shift = 8 * (sgi - min_sgi);
c1bfb577 842 reg |= ((u32)*vgic_get_sgi_sources(dist, vcpu_id, sgi)) << shift;
90a5355e
CD
843 }
844
845 mmio_data_write(mmio, ~0, reg);
c07a0191
CD
846 return false;
847}
848
90a5355e
CD
849static bool write_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
850 struct kvm_exit_mmio *mmio,
851 phys_addr_t offset, bool set)
852{
853 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
854 int sgi;
0fea6d76 855 int min_sgi = (offset & ~0x3);
90a5355e
CD
856 int max_sgi = min_sgi + 3;
857 int vcpu_id = vcpu->vcpu_id;
858 u32 reg;
859 bool updated = false;
860
861 reg = mmio_data_read(mmio, ~0);
862
863 /* Clear pending SGIs on the distributor */
864 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
865 u8 mask = reg >> (8 * (sgi - min_sgi));
c1bfb577 866 u8 *src = vgic_get_sgi_sources(dist, vcpu_id, sgi);
90a5355e 867 if (set) {
c1bfb577 868 if ((*src & mask) != mask)
90a5355e 869 updated = true;
c1bfb577 870 *src |= mask;
90a5355e 871 } else {
c1bfb577 872 if (*src & mask)
90a5355e 873 updated = true;
c1bfb577 874 *src &= ~mask;
90a5355e
CD
875 }
876 }
877
878 if (updated)
879 vgic_update_state(vcpu->kvm);
880
881 return updated;
882}
883
c07a0191
CD
884static bool handle_mmio_sgi_set(struct kvm_vcpu *vcpu,
885 struct kvm_exit_mmio *mmio,
886 phys_addr_t offset)
887{
90a5355e
CD
888 if (!mmio->is_write)
889 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
890 else
891 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, true);
892}
893
894static bool handle_mmio_sgi_clear(struct kvm_vcpu *vcpu,
895 struct kvm_exit_mmio *mmio,
896 phys_addr_t offset)
897{
898 if (!mmio->is_write)
899 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
900 else
901 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, false);
c07a0191
CD
902}
903
1a89dd91
MZ
904/*
905 * I would have liked to use the kvm_bus_io_*() API instead, but it
906 * cannot cope with banked registers (only the VM pointer is passed
907 * around, and we need the vcpu). One of these days, someone please
908 * fix it!
909 */
910struct mmio_range {
911 phys_addr_t base;
912 unsigned long len;
c3c91836 913 int bits_per_irq;
1a89dd91
MZ
914 bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
915 phys_addr_t offset);
916};
917
1006e8cb 918static const struct mmio_range vgic_dist_ranges[] = {
b47ef92a
MZ
919 {
920 .base = GIC_DIST_CTRL,
921 .len = 12,
c3c91836 922 .bits_per_irq = 0,
b47ef92a
MZ
923 .handle_mmio = handle_mmio_misc,
924 },
925 {
926 .base = GIC_DIST_IGROUP,
c3c91836
MZ
927 .len = VGIC_MAX_IRQS / 8,
928 .bits_per_irq = 1,
b47ef92a
MZ
929 .handle_mmio = handle_mmio_raz_wi,
930 },
931 {
932 .base = GIC_DIST_ENABLE_SET,
c3c91836
MZ
933 .len = VGIC_MAX_IRQS / 8,
934 .bits_per_irq = 1,
b47ef92a
MZ
935 .handle_mmio = handle_mmio_set_enable_reg,
936 },
937 {
938 .base = GIC_DIST_ENABLE_CLEAR,
c3c91836
MZ
939 .len = VGIC_MAX_IRQS / 8,
940 .bits_per_irq = 1,
b47ef92a
MZ
941 .handle_mmio = handle_mmio_clear_enable_reg,
942 },
943 {
944 .base = GIC_DIST_PENDING_SET,
c3c91836
MZ
945 .len = VGIC_MAX_IRQS / 8,
946 .bits_per_irq = 1,
b47ef92a
MZ
947 .handle_mmio = handle_mmio_set_pending_reg,
948 },
949 {
950 .base = GIC_DIST_PENDING_CLEAR,
c3c91836
MZ
951 .len = VGIC_MAX_IRQS / 8,
952 .bits_per_irq = 1,
b47ef92a
MZ
953 .handle_mmio = handle_mmio_clear_pending_reg,
954 },
955 {
956 .base = GIC_DIST_ACTIVE_SET,
c3c91836
MZ
957 .len = VGIC_MAX_IRQS / 8,
958 .bits_per_irq = 1,
b47ef92a
MZ
959 .handle_mmio = handle_mmio_raz_wi,
960 },
961 {
962 .base = GIC_DIST_ACTIVE_CLEAR,
c3c91836
MZ
963 .len = VGIC_MAX_IRQS / 8,
964 .bits_per_irq = 1,
b47ef92a
MZ
965 .handle_mmio = handle_mmio_raz_wi,
966 },
967 {
968 .base = GIC_DIST_PRI,
c3c91836
MZ
969 .len = VGIC_MAX_IRQS,
970 .bits_per_irq = 8,
b47ef92a
MZ
971 .handle_mmio = handle_mmio_priority_reg,
972 },
973 {
974 .base = GIC_DIST_TARGET,
c3c91836
MZ
975 .len = VGIC_MAX_IRQS,
976 .bits_per_irq = 8,
b47ef92a
MZ
977 .handle_mmio = handle_mmio_target_reg,
978 },
979 {
980 .base = GIC_DIST_CONFIG,
c3c91836
MZ
981 .len = VGIC_MAX_IRQS / 4,
982 .bits_per_irq = 2,
b47ef92a
MZ
983 .handle_mmio = handle_mmio_cfg_reg,
984 },
985 {
986 .base = GIC_DIST_SOFTINT,
987 .len = 4,
988 .handle_mmio = handle_mmio_sgi_reg,
989 },
c07a0191
CD
990 {
991 .base = GIC_DIST_SGI_PENDING_CLEAR,
992 .len = VGIC_NR_SGIS,
993 .handle_mmio = handle_mmio_sgi_clear,
994 },
995 {
996 .base = GIC_DIST_SGI_PENDING_SET,
997 .len = VGIC_NR_SGIS,
998 .handle_mmio = handle_mmio_sgi_set,
999 },
1a89dd91
MZ
1000 {}
1001};
1002
1003static const
1004struct mmio_range *find_matching_range(const struct mmio_range *ranges,
1005 struct kvm_exit_mmio *mmio,
1006e8cb 1006 phys_addr_t offset)
1a89dd91
MZ
1007{
1008 const struct mmio_range *r = ranges;
1a89dd91
MZ
1009
1010 while (r->len) {
1006e8cb
CD
1011 if (offset >= r->base &&
1012 (offset + mmio->len) <= (r->base + r->len))
1a89dd91
MZ
1013 return r;
1014 r++;
1015 }
1016
1017 return NULL;
1018}
1019
c3c91836
MZ
1020static bool vgic_validate_access(const struct vgic_dist *dist,
1021 const struct mmio_range *range,
1022 unsigned long offset)
1023{
1024 int irq;
1025
1026 if (!range->bits_per_irq)
1027 return true; /* Not an irq-based access */
1028
1029 irq = offset * 8 / range->bits_per_irq;
1030 if (irq >= dist->nr_irqs)
1031 return false;
1032
1033 return true;
1034}
1035
1a89dd91
MZ
1036/**
1037 * vgic_handle_mmio - handle an in-kernel MMIO access
1038 * @vcpu: pointer to the vcpu performing the access
1039 * @run: pointer to the kvm_run structure
1040 * @mmio: pointer to the data describing the access
1041 *
1042 * returns true if the MMIO access has been performed in kernel space,
1043 * and false if it needs to be emulated in user space.
1044 */
1045bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
1046 struct kvm_exit_mmio *mmio)
1047{
b47ef92a
MZ
1048 const struct mmio_range *range;
1049 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1050 unsigned long base = dist->vgic_dist_base;
1051 bool updated_state;
1052 unsigned long offset;
1053
1054 if (!irqchip_in_kernel(vcpu->kvm) ||
1055 mmio->phys_addr < base ||
1056 (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
1057 return false;
1058
1059 /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
1060 if (mmio->len > 4) {
1061 kvm_inject_dabt(vcpu, mmio->phys_addr);
1062 return true;
1063 }
1064
1006e8cb
CD
1065 offset = mmio->phys_addr - base;
1066 range = find_matching_range(vgic_dist_ranges, mmio, offset);
b47ef92a
MZ
1067 if (unlikely(!range || !range->handle_mmio)) {
1068 pr_warn("Unhandled access %d %08llx %d\n",
1069 mmio->is_write, mmio->phys_addr, mmio->len);
1070 return false;
1071 }
1072
1073 spin_lock(&vcpu->kvm->arch.vgic.lock);
1074 offset = mmio->phys_addr - range->base - base;
c3c91836
MZ
1075 if (vgic_validate_access(dist, range, offset)) {
1076 updated_state = range->handle_mmio(vcpu, mmio, offset);
1077 } else {
1078 vgic_reg_access(mmio, NULL, offset,
1079 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
1080 updated_state = false;
1081 }
b47ef92a
MZ
1082 spin_unlock(&vcpu->kvm->arch.vgic.lock);
1083 kvm_prepare_mmio(run, mmio);
1084 kvm_handle_mmio_return(vcpu, run);
1085
5863c2ce
MZ
1086 if (updated_state)
1087 vgic_kick_vcpus(vcpu->kvm);
1088
b47ef92a
MZ
1089 return true;
1090}
1091
c1bfb577
MZ
1092static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi)
1093{
1094 return dist->irq_sgi_sources + vcpu_id * VGIC_NR_SGIS + sgi;
1095}
1096
b47ef92a
MZ
1097static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
1098{
1099 struct kvm *kvm = vcpu->kvm;
1100 struct vgic_dist *dist = &kvm->arch.vgic;
1101 int nrcpus = atomic_read(&kvm->online_vcpus);
1102 u8 target_cpus;
1103 int sgi, mode, c, vcpu_id;
1104
1105 vcpu_id = vcpu->vcpu_id;
1106
1107 sgi = reg & 0xf;
1108 target_cpus = (reg >> 16) & 0xff;
1109 mode = (reg >> 24) & 3;
1110
1111 switch (mode) {
1112 case 0:
1113 if (!target_cpus)
1114 return;
91021a6c 1115 break;
b47ef92a
MZ
1116
1117 case 1:
1118 target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
1119 break;
1120
1121 case 2:
1122 target_cpus = 1 << vcpu_id;
1123 break;
1124 }
1125
1126 kvm_for_each_vcpu(c, vcpu, kvm) {
1127 if (target_cpus & 1) {
1128 /* Flag the SGI as pending */
227844f5 1129 vgic_dist_irq_set_pending(vcpu, sgi);
c1bfb577 1130 *vgic_get_sgi_sources(dist, c, sgi) |= 1 << vcpu_id;
b47ef92a
MZ
1131 kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
1132 }
1133
1134 target_cpus >>= 1;
1135 }
1136}
1137
fb65ab63
MZ
1138static int vgic_nr_shared_irqs(struct vgic_dist *dist)
1139{
1140 return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
1141}
1142
b47ef92a
MZ
1143static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
1144{
9d949dce
MZ
1145 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1146 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
1147 unsigned long pending_private, pending_shared;
fb65ab63 1148 int nr_shared = vgic_nr_shared_irqs(dist);
9d949dce
MZ
1149 int vcpu_id;
1150
1151 vcpu_id = vcpu->vcpu_id;
1152 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
1153 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
1154
227844f5 1155 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
9d949dce
MZ
1156 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
1157 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
1158
227844f5 1159 pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
9d949dce 1160 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
fb65ab63 1161 bitmap_and(pend_shared, pending, enabled, nr_shared);
9d949dce
MZ
1162 bitmap_and(pend_shared, pend_shared,
1163 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
fb65ab63 1164 nr_shared);
9d949dce
MZ
1165
1166 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
fb65ab63 1167 pending_shared = find_first_bit(pend_shared, nr_shared);
9d949dce 1168 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
fb65ab63 1169 pending_shared < vgic_nr_shared_irqs(dist));
b47ef92a
MZ
1170}
1171
1172/*
1173 * Update the interrupt state and determine which CPUs have pending
1174 * interrupts. Must be called with distributor lock held.
1175 */
1176static void vgic_update_state(struct kvm *kvm)
1177{
1178 struct vgic_dist *dist = &kvm->arch.vgic;
1179 struct kvm_vcpu *vcpu;
1180 int c;
1181
1182 if (!dist->enabled) {
c1bfb577 1183 set_bit(0, dist->irq_pending_on_cpu);
b47ef92a
MZ
1184 return;
1185 }
1186
1187 kvm_for_each_vcpu(c, vcpu, kvm) {
1188 if (compute_pending_for_cpu(vcpu)) {
1189 pr_debug("CPU%d has pending interrupts\n", c);
c1bfb577 1190 set_bit(c, dist->irq_pending_on_cpu);
b47ef92a
MZ
1191 }
1192 }
1a89dd91 1193}
330690cd 1194
8d5c6b06
MZ
1195static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
1196{
8f186d52 1197 return vgic_ops->get_lr(vcpu, lr);
8d5c6b06
MZ
1198}
1199
1200static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1201 struct vgic_lr vlr)
1202{
8f186d52 1203 vgic_ops->set_lr(vcpu, lr, vlr);
8d5c6b06
MZ
1204}
1205
69bb2c9f
MZ
1206static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1207 struct vgic_lr vlr)
1208{
8f186d52 1209 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
69bb2c9f
MZ
1210}
1211
1212static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1213{
8f186d52 1214 return vgic_ops->get_elrsr(vcpu);
69bb2c9f
MZ
1215}
1216
8d6a0313
MZ
1217static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
1218{
8f186d52 1219 return vgic_ops->get_eisr(vcpu);
8d6a0313
MZ
1220}
1221
495dd859
MZ
1222static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1223{
8f186d52 1224 return vgic_ops->get_interrupt_status(vcpu);
495dd859
MZ
1225}
1226
909d9b50
MZ
1227static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1228{
8f186d52 1229 vgic_ops->enable_underflow(vcpu);
909d9b50
MZ
1230}
1231
1232static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1233{
8f186d52 1234 vgic_ops->disable_underflow(vcpu);
909d9b50
MZ
1235}
1236
beee38b9
MZ
1237static inline void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1238{
8f186d52 1239 vgic_ops->get_vmcr(vcpu, vmcr);
beee38b9
MZ
1240}
1241
1242static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1243{
8f186d52 1244 vgic_ops->set_vmcr(vcpu, vmcr);
beee38b9
MZ
1245}
1246
da8dafd1
MZ
1247static inline void vgic_enable(struct kvm_vcpu *vcpu)
1248{
8f186d52 1249 vgic_ops->enable(vcpu);
da8dafd1
MZ
1250}
1251
8d5c6b06
MZ
1252static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1253{
1254 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1255 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1256
1257 vlr.state = 0;
1258 vgic_set_lr(vcpu, lr_nr, vlr);
1259 clear_bit(lr_nr, vgic_cpu->lr_used);
1260 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1261}
a1fcb44e
MZ
1262
1263/*
1264 * An interrupt may have been disabled after being made pending on the
1265 * CPU interface (the classic case is a timer running while we're
1266 * rebooting the guest - the interrupt would kick as soon as the CPU
1267 * interface gets enabled, with deadly consequences).
1268 *
1269 * The solution is to examine already active LRs, and check the
1270 * interrupt is still enabled. If not, just retire it.
1271 */
1272static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1273{
1274 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1275 int lr;
1276
8f186d52 1277 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
8d5c6b06 1278 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
a1fcb44e 1279
8d5c6b06
MZ
1280 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1281 vgic_retire_lr(lr, vlr.irq, vcpu);
dbf20f9d
CD
1282 if (vgic_irq_is_queued(vcpu, vlr.irq))
1283 vgic_irq_clear_queued(vcpu, vlr.irq);
a1fcb44e
MZ
1284 }
1285 }
1286}
1287
9d949dce
MZ
1288/*
1289 * Queue an interrupt to a CPU virtual interface. Return true on success,
1290 * or false if it wasn't possible to queue it.
1291 */
1292static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1293{
1294 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
5fb66da6 1295 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
8d5c6b06 1296 struct vgic_lr vlr;
9d949dce
MZ
1297 int lr;
1298
1299 /* Sanitize the input... */
1300 BUG_ON(sgi_source_id & ~7);
1301 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
5fb66da6 1302 BUG_ON(irq >= dist->nr_irqs);
9d949dce
MZ
1303
1304 kvm_debug("Queue IRQ%d\n", irq);
1305
1306 lr = vgic_cpu->vgic_irq_lr_map[irq];
1307
1308 /* Do we have an active interrupt for the same CPUID? */
8d5c6b06
MZ
1309 if (lr != LR_EMPTY) {
1310 vlr = vgic_get_lr(vcpu, lr);
1311 if (vlr.source == sgi_source_id) {
1312 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1313 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1314 vlr.state |= LR_STATE_PENDING;
1315 vgic_set_lr(vcpu, lr, vlr);
1316 return true;
1317 }
9d949dce
MZ
1318 }
1319
1320 /* Try to use another LR for this interrupt */
1321 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
8f186d52
MZ
1322 vgic->nr_lr);
1323 if (lr >= vgic->nr_lr)
9d949dce
MZ
1324 return false;
1325
1326 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
9d949dce
MZ
1327 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1328 set_bit(lr, vgic_cpu->lr_used);
1329
8d5c6b06
MZ
1330 vlr.irq = irq;
1331 vlr.source = sgi_source_id;
1332 vlr.state = LR_STATE_PENDING;
9d949dce 1333 if (!vgic_irq_is_edge(vcpu, irq))
8d5c6b06
MZ
1334 vlr.state |= LR_EOI_INT;
1335
1336 vgic_set_lr(vcpu, lr, vlr);
9d949dce
MZ
1337
1338 return true;
1339}
1340
1341static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
1342{
1343 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1344 unsigned long sources;
1345 int vcpu_id = vcpu->vcpu_id;
1346 int c;
1347
c1bfb577 1348 sources = *vgic_get_sgi_sources(dist, vcpu_id, irq);
9d949dce 1349
fc675e35 1350 for_each_set_bit(c, &sources, dist->nr_cpus) {
9d949dce
MZ
1351 if (vgic_queue_irq(vcpu, c, irq))
1352 clear_bit(c, &sources);
1353 }
1354
c1bfb577 1355 *vgic_get_sgi_sources(dist, vcpu_id, irq) = sources;
9d949dce
MZ
1356
1357 /*
1358 * If the sources bitmap has been cleared it means that we
1359 * could queue all the SGIs onto link registers (see the
1360 * clear_bit above), and therefore we are done with them in
1361 * our emulated gic and can get rid of them.
1362 */
1363 if (!sources) {
227844f5 1364 vgic_dist_irq_clear_pending(vcpu, irq);
9d949dce
MZ
1365 vgic_cpu_irq_clear(vcpu, irq);
1366 return true;
1367 }
1368
1369 return false;
1370}
1371
1372static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1373{
dbf20f9d 1374 if (!vgic_can_sample_irq(vcpu, irq))
9d949dce
MZ
1375 return true; /* level interrupt, already queued */
1376
1377 if (vgic_queue_irq(vcpu, 0, irq)) {
1378 if (vgic_irq_is_edge(vcpu, irq)) {
227844f5 1379 vgic_dist_irq_clear_pending(vcpu, irq);
9d949dce
MZ
1380 vgic_cpu_irq_clear(vcpu, irq);
1381 } else {
dbf20f9d 1382 vgic_irq_set_queued(vcpu, irq);
9d949dce
MZ
1383 }
1384
1385 return true;
1386 }
1387
1388 return false;
1389}
1390
1391/*
1392 * Fill the list registers with pending interrupts before running the
1393 * guest.
1394 */
1395static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1396{
1397 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1398 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1399 int i, vcpu_id;
1400 int overflow = 0;
1401
1402 vcpu_id = vcpu->vcpu_id;
1403
1404 /*
1405 * We may not have any pending interrupt, or the interrupts
1406 * may have been serviced from another vcpu. In all cases,
1407 * move along.
1408 */
1409 if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
1410 pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
1411 goto epilog;
1412 }
1413
1414 /* SGIs */
1415 for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
1416 if (!vgic_queue_sgi(vcpu, i))
1417 overflow = 1;
1418 }
1419
1420 /* PPIs */
1421 for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
1422 if (!vgic_queue_hwirq(vcpu, i))
1423 overflow = 1;
1424 }
1425
1426 /* SPIs */
fb65ab63 1427 for_each_set_bit(i, vgic_cpu->pending_shared, vgic_nr_shared_irqs(dist)) {
9d949dce
MZ
1428 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1429 overflow = 1;
1430 }
1431
1432epilog:
1433 if (overflow) {
909d9b50 1434 vgic_enable_underflow(vcpu);
9d949dce 1435 } else {
909d9b50 1436 vgic_disable_underflow(vcpu);
9d949dce
MZ
1437 /*
1438 * We're about to run this VCPU, and we've consumed
1439 * everything the distributor had in store for
1440 * us. Claim we don't have anything pending. We'll
1441 * adjust that if needed while exiting.
1442 */
c1bfb577 1443 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
9d949dce
MZ
1444 }
1445}
1446
1447static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1448{
495dd859 1449 u32 status = vgic_get_interrupt_status(vcpu);
9d949dce
MZ
1450 bool level_pending = false;
1451
495dd859 1452 kvm_debug("STATUS = %08x\n", status);
9d949dce 1453
495dd859 1454 if (status & INT_STATUS_EOI) {
9d949dce
MZ
1455 /*
1456 * Some level interrupts have been EOIed. Clear their
1457 * active bit.
1458 */
8d6a0313 1459 u64 eisr = vgic_get_eisr(vcpu);
2df36a5d 1460 unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
8d5c6b06 1461 int lr;
9d949dce 1462
8f186d52 1463 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
8d5c6b06 1464 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
faa1b46c 1465 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
9d949dce 1466
dbf20f9d 1467 vgic_irq_clear_queued(vcpu, vlr.irq);
8d5c6b06
MZ
1468 WARN_ON(vlr.state & LR_STATE_MASK);
1469 vlr.state = 0;
1470 vgic_set_lr(vcpu, lr, vlr);
9d949dce 1471
faa1b46c
CD
1472 /*
1473 * If the IRQ was EOIed it was also ACKed and we we
1474 * therefore assume we can clear the soft pending
1475 * state (should it had been set) for this interrupt.
1476 *
1477 * Note: if the IRQ soft pending state was set after
1478 * the IRQ was acked, it actually shouldn't be
1479 * cleared, but we have no way of knowing that unless
1480 * we start trapping ACKs when the soft-pending state
1481 * is set.
1482 */
1483 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1484
9d949dce 1485 /* Any additional pending interrupt? */
faa1b46c 1486 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
8d5c6b06 1487 vgic_cpu_irq_set(vcpu, vlr.irq);
9d949dce
MZ
1488 level_pending = true;
1489 } else {
faa1b46c 1490 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
8d5c6b06 1491 vgic_cpu_irq_clear(vcpu, vlr.irq);
9d949dce 1492 }
75da01e1
MZ
1493
1494 /*
1495 * Despite being EOIed, the LR may not have
1496 * been marked as empty.
1497 */
69bb2c9f 1498 vgic_sync_lr_elrsr(vcpu, lr, vlr);
9d949dce
MZ
1499 }
1500 }
1501
495dd859 1502 if (status & INT_STATUS_UNDERFLOW)
909d9b50 1503 vgic_disable_underflow(vcpu);
9d949dce
MZ
1504
1505 return level_pending;
1506}
1507
1508/*
33c83cb3
MZ
1509 * Sync back the VGIC state after a guest run. The distributor lock is
1510 * needed so we don't get preempted in the middle of the state processing.
9d949dce
MZ
1511 */
1512static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1513{
1514 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1515 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
69bb2c9f
MZ
1516 u64 elrsr;
1517 unsigned long *elrsr_ptr;
9d949dce
MZ
1518 int lr, pending;
1519 bool level_pending;
1520
1521 level_pending = vgic_process_maintenance(vcpu);
69bb2c9f 1522 elrsr = vgic_get_elrsr(vcpu);
2df36a5d 1523 elrsr_ptr = u64_to_bitmask(&elrsr);
9d949dce
MZ
1524
1525 /* Clear mappings for empty LRs */
8f186d52 1526 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
8d5c6b06 1527 struct vgic_lr vlr;
9d949dce
MZ
1528
1529 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1530 continue;
1531
8d5c6b06 1532 vlr = vgic_get_lr(vcpu, lr);
9d949dce 1533
5fb66da6 1534 BUG_ON(vlr.irq >= dist->nr_irqs);
8d5c6b06 1535 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
9d949dce
MZ
1536 }
1537
1538 /* Check if we still have something up our sleeve... */
8f186d52
MZ
1539 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1540 if (level_pending || pending < vgic->nr_lr)
c1bfb577 1541 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
9d949dce
MZ
1542}
1543
1544void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1545{
1546 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1547
1548 if (!irqchip_in_kernel(vcpu->kvm))
1549 return;
1550
1551 spin_lock(&dist->lock);
1552 __kvm_vgic_flush_hwstate(vcpu);
1553 spin_unlock(&dist->lock);
1554}
1555
1556void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1557{
33c83cb3
MZ
1558 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1559
9d949dce
MZ
1560 if (!irqchip_in_kernel(vcpu->kvm))
1561 return;
1562
33c83cb3 1563 spin_lock(&dist->lock);
9d949dce 1564 __kvm_vgic_sync_hwstate(vcpu);
33c83cb3 1565 spin_unlock(&dist->lock);
9d949dce
MZ
1566}
1567
1568int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1569{
1570 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1571
1572 if (!irqchip_in_kernel(vcpu->kvm))
1573 return 0;
1574
c1bfb577 1575 return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
9d949dce
MZ
1576}
1577
5863c2ce
MZ
1578static void vgic_kick_vcpus(struct kvm *kvm)
1579{
1580 struct kvm_vcpu *vcpu;
1581 int c;
1582
1583 /*
1584 * We've injected an interrupt, time to find out who deserves
1585 * a good kick...
1586 */
1587 kvm_for_each_vcpu(c, vcpu, kvm) {
1588 if (kvm_vgic_vcpu_pending_irq(vcpu))
1589 kvm_vcpu_kick(vcpu);
1590 }
1591}
1592
1593static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1594{
227844f5 1595 int edge_triggered = vgic_irq_is_edge(vcpu, irq);
5863c2ce
MZ
1596
1597 /*
1598 * Only inject an interrupt if:
1599 * - edge triggered and we have a rising edge
1600 * - level triggered and we change level
1601 */
faa1b46c
CD
1602 if (edge_triggered) {
1603 int state = vgic_dist_irq_is_pending(vcpu, irq);
5863c2ce 1604 return level > state;
faa1b46c
CD
1605 } else {
1606 int state = vgic_dist_irq_get_level(vcpu, irq);
5863c2ce 1607 return level != state;
faa1b46c 1608 }
5863c2ce
MZ
1609}
1610
016ed39c 1611static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
5863c2ce
MZ
1612 unsigned int irq_num, bool level)
1613{
1614 struct vgic_dist *dist = &kvm->arch.vgic;
1615 struct kvm_vcpu *vcpu;
227844f5 1616 int edge_triggered, level_triggered;
5863c2ce
MZ
1617 int enabled;
1618 bool ret = true;
1619
1620 spin_lock(&dist->lock);
1621
1622 vcpu = kvm_get_vcpu(kvm, cpuid);
227844f5
CD
1623 edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
1624 level_triggered = !edge_triggered;
5863c2ce
MZ
1625
1626 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1627 ret = false;
1628 goto out;
1629 }
1630
1631 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1632 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1633 vcpu = kvm_get_vcpu(kvm, cpuid);
1634 }
1635
1636 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1637
faa1b46c
CD
1638 if (level) {
1639 if (level_triggered)
1640 vgic_dist_irq_set_level(vcpu, irq_num);
227844f5 1641 vgic_dist_irq_set_pending(vcpu, irq_num);
faa1b46c
CD
1642 } else {
1643 if (level_triggered) {
1644 vgic_dist_irq_clear_level(vcpu, irq_num);
1645 if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1646 vgic_dist_irq_clear_pending(vcpu, irq_num);
faa1b46c 1647 }
7d39f9e3 1648
1649 ret = false;
1650 goto out;
faa1b46c 1651 }
5863c2ce
MZ
1652
1653 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1654
1655 if (!enabled) {
1656 ret = false;
1657 goto out;
1658 }
1659
dbf20f9d 1660 if (!vgic_can_sample_irq(vcpu, irq_num)) {
5863c2ce
MZ
1661 /*
1662 * Level interrupt in progress, will be picked up
1663 * when EOId.
1664 */
1665 ret = false;
1666 goto out;
1667 }
1668
1669 if (level) {
1670 vgic_cpu_irq_set(vcpu, irq_num);
c1bfb577 1671 set_bit(cpuid, dist->irq_pending_on_cpu);
5863c2ce
MZ
1672 }
1673
1674out:
1675 spin_unlock(&dist->lock);
1676
016ed39c 1677 return ret ? cpuid : -EINVAL;
5863c2ce
MZ
1678}
1679
1680/**
1681 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1682 * @kvm: The VM structure pointer
1683 * @cpuid: The CPU for PPIs
1684 * @irq_num: The IRQ number that is assigned to the device
1685 * @level: Edge-triggered: true: to trigger the interrupt
1686 * false: to ignore the call
1687 * Level-sensitive true: activates an interrupt
1688 * false: deactivates an interrupt
1689 *
1690 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1691 * level-sensitive interrupts. You can think of the level parameter as 1
1692 * being HIGH and 0 being LOW and all devices being active-HIGH.
1693 */
1694int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1695 bool level)
1696{
ca7d9c82 1697 int ret = 0;
016ed39c 1698 int vcpu_id;
5863c2ce 1699
ca7d9c82 1700 if (unlikely(!vgic_initialized(kvm))) {
59892136
AP
1701 /*
1702 * We only provide the automatic initialization of the VGIC
1703 * for the legacy case of a GICv2. Any other type must
1704 * be explicitly initialized once setup with the respective
1705 * KVM device call.
1706 */
1707 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) {
1708 ret = -EBUSY;
1709 goto out;
1710 }
ca7d9c82
CD
1711 mutex_lock(&kvm->lock);
1712 ret = vgic_init(kvm);
1713 mutex_unlock(&kvm->lock);
1714
1715 if (ret)
1716 goto out;
016ed39c 1717 }
5863c2ce 1718
ca7d9c82
CD
1719 vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level);
1720 if (vcpu_id >= 0) {
1721 /* kick the specified vcpu */
1722 kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
1723 }
1724
1725out:
1726 return ret;
5863c2ce
MZ
1727}
1728
01ac5e34
MZ
1729static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1730{
1731 /*
1732 * We cannot rely on the vgic maintenance interrupt to be
1733 * delivered synchronously. This means we can only use it to
1734 * exit the VM, and we perform the handling of EOIed
1735 * interrupts on the exit path (see vgic_process_maintenance).
1736 */
1737 return IRQ_HANDLED;
1738}
1739
c1bfb577
MZ
1740void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1741{
1742 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1743
1744 kfree(vgic_cpu->pending_shared);
1745 kfree(vgic_cpu->vgic_irq_lr_map);
1746 vgic_cpu->pending_shared = NULL;
1747 vgic_cpu->vgic_irq_lr_map = NULL;
1748}
1749
1750static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1751{
1752 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1753
1754 int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
1755 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
6d3cfbe2 1756 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
c1bfb577
MZ
1757
1758 if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) {
1759 kvm_vgic_vcpu_destroy(vcpu);
1760 return -ENOMEM;
1761 }
1762
6d3cfbe2 1763 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
01ac5e34
MZ
1764
1765 /*
ca85f623
MZ
1766 * Store the number of LRs per vcpu, so we don't have to go
1767 * all the way to the distributor structure to find out. Only
1768 * assembly code should use this one.
01ac5e34 1769 */
8f186d52 1770 vgic_cpu->nr_lr = vgic->nr_lr;
01ac5e34 1771
6d3cfbe2 1772 return 0;
01ac5e34
MZ
1773}
1774
c1bfb577
MZ
1775void kvm_vgic_destroy(struct kvm *kvm)
1776{
1777 struct vgic_dist *dist = &kvm->arch.vgic;
1778 struct kvm_vcpu *vcpu;
1779 int i;
1780
1781 kvm_for_each_vcpu(i, vcpu, kvm)
1782 kvm_vgic_vcpu_destroy(vcpu);
1783
1784 vgic_free_bitmap(&dist->irq_enabled);
1785 vgic_free_bitmap(&dist->irq_level);
1786 vgic_free_bitmap(&dist->irq_pending);
1787 vgic_free_bitmap(&dist->irq_soft_pend);
1788 vgic_free_bitmap(&dist->irq_queued);
1789 vgic_free_bitmap(&dist->irq_cfg);
1790 vgic_free_bytemap(&dist->irq_priority);
1791 if (dist->irq_spi_target) {
1792 for (i = 0; i < dist->nr_cpus; i++)
1793 vgic_free_bitmap(&dist->irq_spi_target[i]);
1794 }
1795 kfree(dist->irq_sgi_sources);
1796 kfree(dist->irq_spi_cpu);
1797 kfree(dist->irq_spi_target);
1798 kfree(dist->irq_pending_on_cpu);
1799 dist->irq_sgi_sources = NULL;
1800 dist->irq_spi_cpu = NULL;
1801 dist->irq_spi_target = NULL;
1802 dist->irq_pending_on_cpu = NULL;
1f57be28 1803 dist->nr_cpus = 0;
c1bfb577
MZ
1804}
1805
1806/*
1807 * Allocate and initialize the various data structures. Must be called
1808 * with kvm->lock held!
1809 */
6d3cfbe2 1810static int vgic_init(struct kvm *kvm)
c1bfb577
MZ
1811{
1812 struct vgic_dist *dist = &kvm->arch.vgic;
1813 struct kvm_vcpu *vcpu;
1814 int nr_cpus, nr_irqs;
6d3cfbe2 1815 int ret, i, vcpu_id;
c1bfb577 1816
1f57be28 1817 if (vgic_initialized(kvm))
4956f2bc
MZ
1818 return 0;
1819
1820 nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
1821 if (!nr_cpus) /* No vcpus? Can't be good... */
66b030e4 1822 return -ENODEV;
5fb66da6 1823
4956f2bc
MZ
1824 /*
1825 * If nobody configured the number of interrupts, use the
1826 * legacy one.
1827 */
5fb66da6
MZ
1828 if (!dist->nr_irqs)
1829 dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
1830
1831 nr_irqs = dist->nr_irqs;
c1bfb577
MZ
1832
1833 ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
1834 ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
1835 ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
1836 ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
1837 ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
1838 ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
1839 ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
1840
1841 if (ret)
1842 goto out;
1843
1844 dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
1845 dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
1846 dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
1847 GFP_KERNEL);
1848 dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
1849 GFP_KERNEL);
1850 if (!dist->irq_sgi_sources ||
1851 !dist->irq_spi_cpu ||
1852 !dist->irq_spi_target ||
1853 !dist->irq_pending_on_cpu) {
1854 ret = -ENOMEM;
1855 goto out;
1856 }
1857
1858 for (i = 0; i < nr_cpus; i++)
1859 ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
1860 nr_cpus, nr_irqs);
1861
1862 if (ret)
1863 goto out;
1864
6d3cfbe2
PM
1865 for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1866 vgic_set_target_reg(kvm, 0, i);
1867
1868 kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
c1bfb577
MZ
1869 ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
1870 if (ret) {
1871 kvm_err("VGIC: Failed to allocate vcpu memory\n");
1872 break;
1873 }
c1bfb577 1874
6d3cfbe2
PM
1875 for (i = 0; i < dist->nr_irqs; i++) {
1876 if (i < VGIC_NR_PPIS)
1877 vgic_bitmap_set_irq_val(&dist->irq_enabled,
1878 vcpu->vcpu_id, i, 1);
1879 if (i < VGIC_NR_PRIVATE_IRQS)
1880 vgic_bitmap_set_irq_val(&dist->irq_cfg,
1881 vcpu->vcpu_id, i,
1882 VGIC_CFG_EDGE);
1883 }
1884
1885 vgic_enable(vcpu);
1886 }
4956f2bc 1887
c1bfb577
MZ
1888out:
1889 if (ret)
1890 kvm_vgic_destroy(kvm);
1891
1892 return ret;
1893}
1894
e1ba0207 1895/**
6d3cfbe2 1896 * kvm_vgic_map_resources - Configure global VGIC state before running any VCPUs
e1ba0207
CD
1897 * @kvm: pointer to the kvm struct
1898 *
1899 * Map the virtual CPU interface into the VM before running any VCPUs. We
1900 * can't do this at creation time, because user space must first set the
6d3cfbe2 1901 * virtual CPU interface address in the guest physical address space.
e1ba0207 1902 */
6d3cfbe2 1903int kvm_vgic_map_resources(struct kvm *kvm)
01ac5e34 1904{
6d3cfbe2 1905 int ret = 0;
01ac5e34 1906
e1ba0207
CD
1907 if (!irqchip_in_kernel(kvm))
1908 return 0;
1909
01ac5e34
MZ
1910 mutex_lock(&kvm->lock);
1911
c52edf5f 1912 if (vgic_ready(kvm))
01ac5e34
MZ
1913 goto out;
1914
1915 if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
1916 IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
1917 kvm_err("Need to set vgic cpu and dist addresses first\n");
1918 ret = -ENXIO;
1919 goto out;
1920 }
1921
6d3cfbe2
PM
1922 /*
1923 * Initialize the vgic if this hasn't already been done on demand by
1924 * accessing the vgic state from userspace.
1925 */
1926 ret = vgic_init(kvm);
4956f2bc
MZ
1927 if (ret) {
1928 kvm_err("Unable to allocate maps\n");
1929 goto out;
1930 }
1931
01ac5e34 1932 ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
c40f2f8f
AB
1933 vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE,
1934 true);
01ac5e34
MZ
1935 if (ret) {
1936 kvm_err("Unable to remap VGIC CPU to VCPU\n");
1937 goto out;
1938 }
1939
01ac5e34
MZ
1940 kvm->arch.vgic.ready = true;
1941out:
4956f2bc
MZ
1942 if (ret)
1943 kvm_vgic_destroy(kvm);
01ac5e34
MZ
1944 mutex_unlock(&kvm->lock);
1945 return ret;
1946}
1947
59892136 1948int kvm_vgic_create(struct kvm *kvm, u32 type)
01ac5e34 1949{
6b50f540 1950 int i, vcpu_lock_idx = -1, ret;
7330672b 1951 struct kvm_vcpu *vcpu;
01ac5e34
MZ
1952
1953 mutex_lock(&kvm->lock);
1954
7330672b 1955 if (kvm->arch.vgic.vctrl_base) {
01ac5e34
MZ
1956 ret = -EEXIST;
1957 goto out;
1958 }
1959
7330672b
CD
1960 /*
1961 * Any time a vcpu is run, vcpu_load is called which tries to grab the
1962 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
1963 * that no other VCPUs are run while we create the vgic.
1964 */
6b50f540 1965 ret = -EBUSY;
7330672b
CD
1966 kvm_for_each_vcpu(i, vcpu, kvm) {
1967 if (!mutex_trylock(&vcpu->mutex))
1968 goto out_unlock;
1969 vcpu_lock_idx = i;
1970 }
1971
1972 kvm_for_each_vcpu(i, vcpu, kvm) {
6b50f540 1973 if (vcpu->arch.has_run_once)
7330672b 1974 goto out_unlock;
7330672b 1975 }
6b50f540 1976 ret = 0;
7330672b 1977
01ac5e34 1978 spin_lock_init(&kvm->arch.vgic.lock);
f982cf4e 1979 kvm->arch.vgic.in_kernel = true;
59892136 1980 kvm->arch.vgic.vgic_model = type;
8f186d52 1981 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
01ac5e34
MZ
1982 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
1983 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
1984
7330672b
CD
1985out_unlock:
1986 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1987 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1988 mutex_unlock(&vcpu->mutex);
1989 }
1990
01ac5e34
MZ
1991out:
1992 mutex_unlock(&kvm->lock);
1993 return ret;
1994}
1995
1fa451bc 1996static int vgic_ioaddr_overlap(struct kvm *kvm)
330690cd
CD
1997{
1998 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
1999 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
2000
2001 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
2002 return 0;
2003 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
2004 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
2005 return -EBUSY;
2006 return 0;
2007}
2008
2009static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
2010 phys_addr_t addr, phys_addr_t size)
2011{
2012 int ret;
2013
ce01e4e8
CD
2014 if (addr & ~KVM_PHYS_MASK)
2015 return -E2BIG;
2016
2017 if (addr & (SZ_4K - 1))
2018 return -EINVAL;
2019
330690cd
CD
2020 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
2021 return -EEXIST;
2022 if (addr + size < addr)
2023 return -EINVAL;
2024
30c21170 2025 *ioaddr = addr;
330690cd
CD
2026 ret = vgic_ioaddr_overlap(kvm);
2027 if (ret)
30c21170
HW
2028 *ioaddr = VGIC_ADDR_UNDEF;
2029
330690cd
CD
2030 return ret;
2031}
2032
ce01e4e8
CD
2033/**
2034 * kvm_vgic_addr - set or get vgic VM base addresses
2035 * @kvm: pointer to the vm struct
2036 * @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
2037 * @addr: pointer to address value
2038 * @write: if true set the address in the VM address space, if false read the
2039 * address
2040 *
2041 * Set or get the vgic base addresses for the distributor and the virtual CPU
2042 * interface in the VM physical address space. These addresses are properties
2043 * of the emulated core/SoC and therefore user space initially knows this
2044 * information.
2045 */
2046int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
330690cd
CD
2047{
2048 int r = 0;
2049 struct vgic_dist *vgic = &kvm->arch.vgic;
2050
330690cd
CD
2051 mutex_lock(&kvm->lock);
2052 switch (type) {
2053 case KVM_VGIC_V2_ADDR_TYPE_DIST:
ce01e4e8
CD
2054 if (write) {
2055 r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
2056 *addr, KVM_VGIC_V2_DIST_SIZE);
2057 } else {
2058 *addr = vgic->vgic_dist_base;
2059 }
330690cd
CD
2060 break;
2061 case KVM_VGIC_V2_ADDR_TYPE_CPU:
ce01e4e8
CD
2062 if (write) {
2063 r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
2064 *addr, KVM_VGIC_V2_CPU_SIZE);
2065 } else {
2066 *addr = vgic->vgic_cpu_base;
2067 }
330690cd
CD
2068 break;
2069 default:
2070 r = -ENODEV;
2071 }
2072
2073 mutex_unlock(&kvm->lock);
2074 return r;
2075}
7330672b 2076
c07a0191
CD
2077static bool handle_cpu_mmio_misc(struct kvm_vcpu *vcpu,
2078 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2079{
fa20f5ae 2080 bool updated = false;
beee38b9
MZ
2081 struct vgic_vmcr vmcr;
2082 u32 *vmcr_field;
2083 u32 reg;
2084
2085 vgic_get_vmcr(vcpu, &vmcr);
fa20f5ae
CD
2086
2087 switch (offset & ~0x3) {
2088 case GIC_CPU_CTRL:
beee38b9 2089 vmcr_field = &vmcr.ctlr;
fa20f5ae
CD
2090 break;
2091 case GIC_CPU_PRIMASK:
beee38b9 2092 vmcr_field = &vmcr.pmr;
fa20f5ae
CD
2093 break;
2094 case GIC_CPU_BINPOINT:
beee38b9 2095 vmcr_field = &vmcr.bpr;
fa20f5ae
CD
2096 break;
2097 case GIC_CPU_ALIAS_BINPOINT:
beee38b9 2098 vmcr_field = &vmcr.abpr;
fa20f5ae 2099 break;
beee38b9
MZ
2100 default:
2101 BUG();
fa20f5ae
CD
2102 }
2103
2104 if (!mmio->is_write) {
beee38b9 2105 reg = *vmcr_field;
fa20f5ae
CD
2106 mmio_data_write(mmio, ~0, reg);
2107 } else {
2108 reg = mmio_data_read(mmio, ~0);
beee38b9
MZ
2109 if (reg != *vmcr_field) {
2110 *vmcr_field = reg;
2111 vgic_set_vmcr(vcpu, &vmcr);
fa20f5ae 2112 updated = true;
beee38b9 2113 }
fa20f5ae
CD
2114 }
2115 return updated;
2116}
2117
2118static bool handle_mmio_abpr(struct kvm_vcpu *vcpu,
2119 struct kvm_exit_mmio *mmio, phys_addr_t offset)
2120{
2121 return handle_cpu_mmio_misc(vcpu, mmio, GIC_CPU_ALIAS_BINPOINT);
c07a0191
CD
2122}
2123
fa20f5ae
CD
2124static bool handle_cpu_mmio_ident(struct kvm_vcpu *vcpu,
2125 struct kvm_exit_mmio *mmio,
2126 phys_addr_t offset)
2127{
2128 u32 reg;
2129
2130 if (mmio->is_write)
2131 return false;
2132
2133 /* GICC_IIDR */
2134 reg = (PRODUCT_ID_KVM << 20) |
2135 (GICC_ARCH_VERSION_V2 << 16) |
2136 (IMPLEMENTER_ARM << 0);
2137 mmio_data_write(mmio, ~0, reg);
2138 return false;
2139}
2140
2141/*
2142 * CPU Interface Register accesses - these are not accessed by the VM, but by
2143 * user space for saving and restoring VGIC state.
2144 */
c07a0191
CD
2145static const struct mmio_range vgic_cpu_ranges[] = {
2146 {
2147 .base = GIC_CPU_CTRL,
2148 .len = 12,
2149 .handle_mmio = handle_cpu_mmio_misc,
2150 },
2151 {
2152 .base = GIC_CPU_ALIAS_BINPOINT,
2153 .len = 4,
fa20f5ae 2154 .handle_mmio = handle_mmio_abpr,
c07a0191
CD
2155 },
2156 {
2157 .base = GIC_CPU_ACTIVEPRIO,
2158 .len = 16,
fa20f5ae 2159 .handle_mmio = handle_mmio_raz_wi,
c07a0191
CD
2160 },
2161 {
2162 .base = GIC_CPU_IDENT,
2163 .len = 4,
fa20f5ae 2164 .handle_mmio = handle_cpu_mmio_ident,
c07a0191
CD
2165 },
2166};
2167
2168static int vgic_attr_regs_access(struct kvm_device *dev,
2169 struct kvm_device_attr *attr,
2170 u32 *reg, bool is_write)
2171{
2172 const struct mmio_range *r = NULL, *ranges;
2173 phys_addr_t offset;
2174 int ret, cpuid, c;
2175 struct kvm_vcpu *vcpu, *tmp_vcpu;
2176 struct vgic_dist *vgic;
2177 struct kvm_exit_mmio mmio;
2178
2179 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2180 cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
2181 KVM_DEV_ARM_VGIC_CPUID_SHIFT;
2182
2183 mutex_lock(&dev->kvm->lock);
2184
6d3cfbe2 2185 ret = vgic_init(dev->kvm);
4956f2bc
MZ
2186 if (ret)
2187 goto out;
2188
c07a0191
CD
2189 if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
2190 ret = -EINVAL;
2191 goto out;
2192 }
2193
2194 vcpu = kvm_get_vcpu(dev->kvm, cpuid);
2195 vgic = &dev->kvm->arch.vgic;
2196
2197 mmio.len = 4;
2198 mmio.is_write = is_write;
2199 if (is_write)
2200 mmio_data_write(&mmio, ~0, *reg);
2201 switch (attr->group) {
2202 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2203 mmio.phys_addr = vgic->vgic_dist_base + offset;
2204 ranges = vgic_dist_ranges;
2205 break;
2206 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2207 mmio.phys_addr = vgic->vgic_cpu_base + offset;
2208 ranges = vgic_cpu_ranges;
2209 break;
2210 default:
2211 BUG();
2212 }
2213 r = find_matching_range(ranges, &mmio, offset);
2214
2215 if (unlikely(!r || !r->handle_mmio)) {
2216 ret = -ENXIO;
2217 goto out;
2218 }
2219
2220
2221 spin_lock(&vgic->lock);
2222
2223 /*
2224 * Ensure that no other VCPU is running by checking the vcpu->cpu
2225 * field. If no other VPCUs are running we can safely access the VGIC
2226 * state, because even if another VPU is run after this point, that
2227 * VCPU will not touch the vgic state, because it will block on
2228 * getting the vgic->lock in kvm_vgic_sync_hwstate().
2229 */
2230 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
2231 if (unlikely(tmp_vcpu->cpu != -1)) {
2232 ret = -EBUSY;
2233 goto out_vgic_unlock;
2234 }
2235 }
2236
cbd333a4
CD
2237 /*
2238 * Move all pending IRQs from the LRs on all VCPUs so the pending
2239 * state can be properly represented in the register state accessible
2240 * through this API.
2241 */
2242 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm)
2243 vgic_unqueue_irqs(tmp_vcpu);
2244
c07a0191
CD
2245 offset -= r->base;
2246 r->handle_mmio(vcpu, &mmio, offset);
2247
2248 if (!is_write)
2249 *reg = mmio_data_read(&mmio, ~0);
2250
2251 ret = 0;
2252out_vgic_unlock:
2253 spin_unlock(&vgic->lock);
2254out:
2255 mutex_unlock(&dev->kvm->lock);
2256 return ret;
2257}
2258
7330672b
CD
2259static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2260{
ce01e4e8
CD
2261 int r;
2262
2263 switch (attr->group) {
2264 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2265 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2266 u64 addr;
2267 unsigned long type = (unsigned long)attr->attr;
2268
2269 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2270 return -EFAULT;
2271
2272 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
2273 return (r == -ENODEV) ? -ENXIO : r;
2274 }
c07a0191
CD
2275
2276 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2277 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2278 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2279 u32 reg;
2280
2281 if (get_user(reg, uaddr))
2282 return -EFAULT;
2283
2284 return vgic_attr_regs_access(dev, attr, &reg, true);
2285 }
a98f26f1
MZ
2286 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2287 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2288 u32 val;
2289 int ret = 0;
2290
2291 if (get_user(val, uaddr))
2292 return -EFAULT;
2293
2294 /*
2295 * We require:
2296 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2297 * - at most 1024 interrupts
2298 * - a multiple of 32 interrupts
2299 */
2300 if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
2301 val > VGIC_MAX_IRQS ||
2302 (val & 31))
2303 return -EINVAL;
2304
2305 mutex_lock(&dev->kvm->lock);
2306
c52edf5f 2307 if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
a98f26f1
MZ
2308 ret = -EBUSY;
2309 else
2310 dev->kvm->arch.vgic.nr_irqs = val;
2311
2312 mutex_unlock(&dev->kvm->lock);
2313
2314 return ret;
2315 }
065c0034
EA
2316 case KVM_DEV_ARM_VGIC_GRP_CTRL: {
2317 switch (attr->attr) {
2318 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2319 r = vgic_init(dev->kvm);
2320 return r;
2321 }
2322 break;
2323 }
ce01e4e8
CD
2324 }
2325
7330672b
CD
2326 return -ENXIO;
2327}
2328
2329static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2330{
ce01e4e8
CD
2331 int r = -ENXIO;
2332
2333 switch (attr->group) {
2334 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2335 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2336 u64 addr;
2337 unsigned long type = (unsigned long)attr->attr;
2338
2339 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
2340 if (r)
2341 return (r == -ENODEV) ? -ENXIO : r;
2342
2343 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2344 return -EFAULT;
c07a0191
CD
2345 break;
2346 }
2347
2348 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2349 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
2350 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2351 u32 reg = 0;
2352
2353 r = vgic_attr_regs_access(dev, attr, &reg, false);
2354 if (r)
2355 return r;
2356 r = put_user(reg, uaddr);
2357 break;
ce01e4e8 2358 }
a98f26f1
MZ
2359 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
2360 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
2361 r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
2362 break;
2363 }
c07a0191 2364
ce01e4e8
CD
2365 }
2366
2367 return r;
7330672b
CD
2368}
2369
c07a0191
CD
2370static int vgic_has_attr_regs(const struct mmio_range *ranges,
2371 phys_addr_t offset)
2372{
2373 struct kvm_exit_mmio dev_attr_mmio;
2374
2375 dev_attr_mmio.len = 4;
2376 if (find_matching_range(ranges, &dev_attr_mmio, offset))
2377 return 0;
2378 else
2379 return -ENXIO;
2380}
2381
7330672b
CD
2382static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2383{
c07a0191
CD
2384 phys_addr_t offset;
2385
ce01e4e8
CD
2386 switch (attr->group) {
2387 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2388 switch (attr->attr) {
2389 case KVM_VGIC_V2_ADDR_TYPE_DIST:
2390 case KVM_VGIC_V2_ADDR_TYPE_CPU:
2391 return 0;
2392 }
2393 break;
c07a0191
CD
2394 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2395 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2396 return vgic_has_attr_regs(vgic_dist_ranges, offset);
2397 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2398 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2399 return vgic_has_attr_regs(vgic_cpu_ranges, offset);
a98f26f1
MZ
2400 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
2401 return 0;
065c0034
EA
2402 case KVM_DEV_ARM_VGIC_GRP_CTRL:
2403 switch (attr->attr) {
2404 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2405 return 0;
2406 }
ce01e4e8 2407 }
7330672b
CD
2408 return -ENXIO;
2409}
2410
2411static void vgic_destroy(struct kvm_device *dev)
2412{
2413 kfree(dev);
2414}
2415
2416static int vgic_create(struct kvm_device *dev, u32 type)
2417{
59892136 2418 return kvm_vgic_create(dev->kvm, type);
7330672b
CD
2419}
2420
c06a841b 2421static struct kvm_device_ops kvm_arm_vgic_v2_ops = {
7330672b
CD
2422 .name = "kvm-arm-vgic",
2423 .create = vgic_create,
2424 .destroy = vgic_destroy,
2425 .set_attr = vgic_set_attr,
2426 .get_attr = vgic_get_attr,
2427 .has_attr = vgic_has_attr,
2428};
c06a841b
WD
2429
2430static void vgic_init_maintenance_interrupt(void *info)
2431{
2432 enable_percpu_irq(vgic->maint_irq, 0);
2433}
2434
2435static int vgic_cpu_notify(struct notifier_block *self,
2436 unsigned long action, void *cpu)
2437{
2438 switch (action) {
2439 case CPU_STARTING:
2440 case CPU_STARTING_FROZEN:
2441 vgic_init_maintenance_interrupt(NULL);
2442 break;
2443 case CPU_DYING:
2444 case CPU_DYING_FROZEN:
2445 disable_percpu_irq(vgic->maint_irq);
2446 break;
2447 }
2448
2449 return NOTIFY_OK;
2450}
2451
2452static struct notifier_block vgic_cpu_nb = {
2453 .notifier_call = vgic_cpu_notify,
2454};
2455
2456static const struct of_device_id vgic_ids[] = {
2457 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2458 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
2459 {},
2460};
2461
2462int kvm_vgic_hyp_init(void)
2463{
2464 const struct of_device_id *matched_id;
a875dafc
CD
2465 const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2466 const struct vgic_params **);
c06a841b
WD
2467 struct device_node *vgic_node;
2468 int ret;
2469
2470 vgic_node = of_find_matching_node_and_match(NULL,
2471 vgic_ids, &matched_id);
2472 if (!vgic_node) {
2473 kvm_err("error: no compatible GIC node found\n");
2474 return -ENODEV;
2475 }
2476
2477 vgic_probe = matched_id->data;
2478 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2479 if (ret)
2480 return ret;
2481
2482 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
2483 "vgic", kvm_get_running_vcpus());
2484 if (ret) {
2485 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
2486 return ret;
2487 }
2488
2489 ret = __register_cpu_notifier(&vgic_cpu_nb);
2490 if (ret) {
2491 kvm_err("Cannot register vgic CPU notifier\n");
2492 goto out_free_irq;
2493 }
2494
2495 /* Callback into for arch code for setup */
2496 vgic_arch_setup(vgic);
2497
2498 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
2499
2500 return kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
2501 KVM_DEV_TYPE_ARM_VGIC_V2);
2502
2503out_free_irq:
2504 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
2505 return ret;
2506}