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