blob: 795ab482333daf3e2521d784c50d571125b3efbb [file] [log] [blame]
Marc Zyngier1a89dd92013-01-21 19:36:12 -05001/*
2 * Copyright (C) 2012 ARM Ltd.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
Marc Zyngier01ac5e32013-01-21 19:36:16 -050019#include <linux/cpu.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050020#include <linux/kvm.h>
21#include <linux/kvm_host.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050024#include <linux/of.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
Christoffer Dall2a2f3e262014-02-02 13:41:02 -080027#include <linux/uaccess.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050028
29#include <linux/irqchip/arm-gic.h>
30
Marc Zyngier1a89dd92013-01-21 19:36:12 -050031#include <asm/kvm_emulate.h>
Marc Zyngier01ac5e32013-01-21 19:36:16 -050032#include <asm/kvm_arm.h>
33#include <asm/kvm_mmu.h>
Marc Zyngier1a89dd92013-01-21 19:36:12 -050034
Marc Zyngierb47ef922013-01-21 19:36:14 -050035/*
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
Christoffer Dall330690c2013-01-21 19:36:13 -050072#define VGIC_ADDR_UNDEF (-1)
73#define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
74
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -070075#define PRODUCT_ID_KVM 0x4b /* ASCII code K */
76#define IMPLEMENTER_ARM 0x43b
77#define GICC_ARCH_VERSION_V2 0x2
78
Marc Zyngier1a89dd92013-01-21 19:36:12 -050079#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
Marc Zyngiera1fcb442013-01-21 19:36:15 -050088static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +010089static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -050090static void vgic_update_state(struct kvm *kvm);
Marc Zyngier5863c2c2013-01-21 19:36:15 -050091static void vgic_kick_vcpus(struct kvm *kvm);
Marc Zyngierb47ef922013-01-21 19:36:14 -050092static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +010093static 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);
Marc Zyngierbeee38b2014-02-04 17:48:10 +000095static 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);
Marc Zyngier01ac5e32013-01-21 19:36:16 -050097
Marc Zyngier8f186d52014-02-04 18:13:03 +000098static const struct vgic_ops *vgic_ops;
99static const struct vgic_params *vgic;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500100
101static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
102 int cpuid, u32 offset)
103{
104 offset >>= 2;
105 if (!offset)
106 return x->percpu[cpuid].reg;
107 else
108 return x->shared.reg + offset - 1;
109}
110
111static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
112 int cpuid, int irq)
113{
114 if (irq < VGIC_NR_PRIVATE_IRQS)
115 return test_bit(irq, x->percpu[cpuid].reg_ul);
116
117 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared.reg_ul);
118}
119
120static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
121 int irq, int val)
122{
123 unsigned long *reg;
124
125 if (irq < VGIC_NR_PRIVATE_IRQS) {
126 reg = x->percpu[cpuid].reg_ul;
127 } else {
128 reg = x->shared.reg_ul;
129 irq -= VGIC_NR_PRIVATE_IRQS;
130 }
131
132 if (val)
133 set_bit(irq, reg);
134 else
135 clear_bit(irq, reg);
136}
137
138static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
139{
140 if (unlikely(cpuid >= VGIC_MAX_CPUS))
141 return NULL;
142 return x->percpu[cpuid].reg_ul;
143}
144
145static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
146{
147 return x->shared.reg_ul;
148}
149
150static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
151{
152 offset >>= 2;
153 BUG_ON(offset > (VGIC_NR_IRQS / 4));
Christoffer Dall8d989152013-08-29 11:08:24 +0100154 if (offset < 8)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500155 return x->percpu[cpuid] + offset;
156 else
157 return x->shared + offset - 8;
158}
159
160#define VGIC_CFG_LEVEL 0
161#define VGIC_CFG_EDGE 1
162
163static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
164{
165 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
166 int irq_val;
167
168 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
169 return irq_val == VGIC_CFG_EDGE;
170}
171
172static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
173{
174 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
175
176 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
177}
178
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500179static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
180{
181 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
182
183 return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
184}
185
186static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
187{
188 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
189
190 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
191}
192
193static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
194{
195 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
196
197 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
198}
199
200static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
201{
202 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
203
204 return vgic_bitmap_get_irq_val(&dist->irq_state, vcpu->vcpu_id, irq);
205}
206
Marc Zyngierb47ef922013-01-21 19:36:14 -0500207static void vgic_dist_irq_set(struct kvm_vcpu *vcpu, int irq)
208{
209 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
210
211 vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 1);
212}
213
214static void vgic_dist_irq_clear(struct kvm_vcpu *vcpu, int irq)
215{
216 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
217
218 vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 0);
219}
220
221static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
222{
223 if (irq < VGIC_NR_PRIVATE_IRQS)
224 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
225 else
226 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
227 vcpu->arch.vgic_cpu.pending_shared);
228}
229
230static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
231{
232 if (irq < VGIC_NR_PRIVATE_IRQS)
233 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
234 else
235 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
236 vcpu->arch.vgic_cpu.pending_shared);
237}
238
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500239static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
240{
241 return *((u32 *)mmio->data) & mask;
242}
243
244static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
245{
246 *((u32 *)mmio->data) = value & mask;
247}
248
249/**
250 * vgic_reg_access - access vgic register
251 * @mmio: pointer to the data describing the mmio access
252 * @reg: pointer to the virtual backing of vgic distributor data
253 * @offset: least significant 2 bits used for word offset
254 * @mode: ACCESS_ mode (see defines above)
255 *
256 * Helper to make vgic register access easier using one of the access
257 * modes defined for vgic register access
258 * (read,raz,write-ignored,setbit,clearbit,write)
259 */
260static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
261 phys_addr_t offset, int mode)
262{
263 int word_offset = (offset & 3) * 8;
264 u32 mask = (1UL << (mmio->len * 8)) - 1;
265 u32 regval;
266
267 /*
268 * Any alignment fault should have been delivered to the guest
269 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
270 */
271
272 if (reg) {
273 regval = *reg;
274 } else {
275 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
276 regval = 0;
277 }
278
279 if (mmio->is_write) {
280 u32 data = mmio_data_read(mmio, mask) << word_offset;
281 switch (ACCESS_WRITE_MASK(mode)) {
282 case ACCESS_WRITE_IGNORED:
283 return;
284
285 case ACCESS_WRITE_SETBIT:
286 regval |= data;
287 break;
288
289 case ACCESS_WRITE_CLEARBIT:
290 regval &= ~data;
291 break;
292
293 case ACCESS_WRITE_VALUE:
294 regval = (regval & ~(mask << word_offset)) | data;
295 break;
296 }
297 *reg = regval;
298 } else {
299 switch (ACCESS_READ_MASK(mode)) {
300 case ACCESS_READ_RAZ:
301 regval = 0;
302 /* fall through */
303
304 case ACCESS_READ_VALUE:
305 mmio_data_write(mmio, mask, regval >> word_offset);
306 }
307 }
308}
309
Marc Zyngierb47ef922013-01-21 19:36:14 -0500310static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
311 struct kvm_exit_mmio *mmio, phys_addr_t offset)
312{
313 u32 reg;
314 u32 word_offset = offset & 3;
315
316 switch (offset & ~3) {
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700317 case 0: /* GICD_CTLR */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500318 reg = vcpu->kvm->arch.vgic.enabled;
319 vgic_reg_access(mmio, &reg, word_offset,
320 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
321 if (mmio->is_write) {
322 vcpu->kvm->arch.vgic.enabled = reg & 1;
323 vgic_update_state(vcpu->kvm);
324 return true;
325 }
326 break;
327
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700328 case 4: /* GICD_TYPER */
Marc Zyngierb47ef922013-01-21 19:36:14 -0500329 reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
330 reg |= (VGIC_NR_IRQS >> 5) - 1;
331 vgic_reg_access(mmio, &reg, word_offset,
332 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
333 break;
334
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -0700335 case 8: /* GICD_IIDR */
336 reg = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500337 vgic_reg_access(mmio, &reg, word_offset,
338 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
339 break;
340 }
341
342 return false;
343}
344
345static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
346 struct kvm_exit_mmio *mmio, phys_addr_t offset)
347{
348 vgic_reg_access(mmio, NULL, offset,
349 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
350 return false;
351}
352
353static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
354 struct kvm_exit_mmio *mmio,
355 phys_addr_t offset)
356{
357 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
358 vcpu->vcpu_id, offset);
359 vgic_reg_access(mmio, reg, offset,
360 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
361 if (mmio->is_write) {
362 vgic_update_state(vcpu->kvm);
363 return true;
364 }
365
366 return false;
367}
368
369static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
370 struct kvm_exit_mmio *mmio,
371 phys_addr_t offset)
372{
373 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
374 vcpu->vcpu_id, offset);
375 vgic_reg_access(mmio, reg, offset,
376 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
377 if (mmio->is_write) {
378 if (offset < 4) /* Force SGI enabled */
379 *reg |= 0xffff;
Marc Zyngiera1fcb442013-01-21 19:36:15 -0500380 vgic_retire_disabled_irqs(vcpu);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500381 vgic_update_state(vcpu->kvm);
382 return true;
383 }
384
385 return false;
386}
387
388static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
389 struct kvm_exit_mmio *mmio,
390 phys_addr_t offset)
391{
392 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
393 vcpu->vcpu_id, offset);
394 vgic_reg_access(mmio, reg, offset,
395 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
396 if (mmio->is_write) {
397 vgic_update_state(vcpu->kvm);
398 return true;
399 }
400
401 return false;
402}
403
404static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
405 struct kvm_exit_mmio *mmio,
406 phys_addr_t offset)
407{
408 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
409 vcpu->vcpu_id, offset);
410 vgic_reg_access(mmio, reg, offset,
411 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
412 if (mmio->is_write) {
413 vgic_update_state(vcpu->kvm);
414 return true;
415 }
416
417 return false;
418}
419
420static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
421 struct kvm_exit_mmio *mmio,
422 phys_addr_t offset)
423{
424 u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
425 vcpu->vcpu_id, offset);
426 vgic_reg_access(mmio, reg, offset,
427 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
428 return false;
429}
430
431#define GICD_ITARGETSR_SIZE 32
432#define GICD_CPUTARGETS_BITS 8
433#define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
434static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
435{
436 struct vgic_dist *dist = &kvm->arch.vgic;
Marc Zyngier986af8e2013-08-29 11:08:22 +0100437 int i;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500438 u32 val = 0;
439
440 irq -= VGIC_NR_PRIVATE_IRQS;
441
Marc Zyngier986af8e2013-08-29 11:08:22 +0100442 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
443 val |= 1 << (dist->irq_spi_cpu[irq + i] + i * 8);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500444
445 return val;
446}
447
448static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
449{
450 struct vgic_dist *dist = &kvm->arch.vgic;
451 struct kvm_vcpu *vcpu;
452 int i, c;
453 unsigned long *bmap;
454 u32 target;
455
456 irq -= VGIC_NR_PRIVATE_IRQS;
457
458 /*
459 * Pick the LSB in each byte. This ensures we target exactly
460 * one vcpu per IRQ. If the byte is null, assume we target
461 * CPU0.
462 */
463 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
464 int shift = i * GICD_CPUTARGETS_BITS;
465 target = ffs((val >> shift) & 0xffU);
466 target = target ? (target - 1) : 0;
467 dist->irq_spi_cpu[irq + i] = target;
468 kvm_for_each_vcpu(c, vcpu, kvm) {
469 bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
470 if (c == target)
471 set_bit(irq + i, bmap);
472 else
473 clear_bit(irq + i, bmap);
474 }
475 }
476}
477
478static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
479 struct kvm_exit_mmio *mmio,
480 phys_addr_t offset)
481{
482 u32 reg;
483
484 /* We treat the banked interrupts targets as read-only */
485 if (offset < 32) {
486 u32 roreg = 1 << vcpu->vcpu_id;
487 roreg |= roreg << 8;
488 roreg |= roreg << 16;
489
490 vgic_reg_access(mmio, &roreg, offset,
491 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
492 return false;
493 }
494
495 reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
496 vgic_reg_access(mmio, &reg, offset,
497 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
498 if (mmio->is_write) {
499 vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
500 vgic_update_state(vcpu->kvm);
501 return true;
502 }
503
504 return false;
505}
506
507static u32 vgic_cfg_expand(u16 val)
508{
509 u32 res = 0;
510 int i;
511
512 /*
513 * Turn a 16bit value like abcd...mnop into a 32bit word
514 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
515 */
516 for (i = 0; i < 16; i++)
517 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
518
519 return res;
520}
521
522static u16 vgic_cfg_compress(u32 val)
523{
524 u16 res = 0;
525 int i;
526
527 /*
528 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
529 * abcd...mnop which is what we really care about.
530 */
531 for (i = 0; i < 16; i++)
532 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
533
534 return res;
535}
536
537/*
538 * The distributor uses 2 bits per IRQ for the CFG register, but the
539 * LSB is always 0. As such, we only keep the upper bit, and use the
540 * two above functions to compress/expand the bits
541 */
542static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
543 struct kvm_exit_mmio *mmio, phys_addr_t offset)
544{
545 u32 val;
Marc Zyngier6545eae2013-08-29 11:08:23 +0100546 u32 *reg;
547
Marc Zyngier6545eae2013-08-29 11:08:23 +0100548 reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200549 vcpu->vcpu_id, offset >> 1);
Marc Zyngier6545eae2013-08-29 11:08:23 +0100550
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200551 if (offset & 4)
Marc Zyngierb47ef922013-01-21 19:36:14 -0500552 val = *reg >> 16;
553 else
554 val = *reg & 0xffff;
555
556 val = vgic_cfg_expand(val);
557 vgic_reg_access(mmio, &val, offset,
558 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
559 if (mmio->is_write) {
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200560 if (offset < 8) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500561 *reg = ~0U; /* Force PPIs/SGIs to 1 */
562 return false;
563 }
564
565 val = vgic_cfg_compress(val);
Andre Przywaraf2ae85b2014-04-11 00:07:18 +0200566 if (offset & 4) {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500567 *reg &= 0xffff;
568 *reg |= val << 16;
569 } else {
570 *reg &= 0xffff << 16;
571 *reg |= val;
572 }
573 }
574
575 return false;
576}
577
578static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
579 struct kvm_exit_mmio *mmio, phys_addr_t offset)
580{
581 u32 reg;
582 vgic_reg_access(mmio, &reg, offset,
583 ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
584 if (mmio->is_write) {
585 vgic_dispatch_sgi(vcpu, reg);
586 vgic_update_state(vcpu->kvm);
587 return true;
588 }
589
590 return false;
591}
592
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800593/**
594 * vgic_unqueue_irqs - move pending IRQs from LRs to the distributor
595 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
596 *
597 * Move any pending IRQs that have already been assigned to LRs back to the
598 * emulated distributor state so that the complete emulated state can be read
599 * from the main emulation structures without investigating the LRs.
600 *
601 * Note that IRQs in the active state in the LRs get their pending state moved
602 * to the distributor but the active state stays in the LRs, because we don't
603 * track the active state on the distributor side.
604 */
605static void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
606{
607 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
608 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
609 int vcpu_id = vcpu->vcpu_id;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100610 int i;
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800611
612 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100613 struct vgic_lr lr = vgic_get_lr(vcpu, i);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800614
615 /*
616 * There are three options for the state bits:
617 *
618 * 01: pending
619 * 10: active
620 * 11: pending and active
621 *
622 * If the LR holds only an active interrupt (not pending) then
623 * just leave it alone.
624 */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100625 if ((lr.state & LR_STATE_MASK) == LR_STATE_ACTIVE)
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800626 continue;
627
628 /*
629 * Reestablish the pending state on the distributor and the
630 * CPU interface. It may have already been pending, but that
631 * is fine, then we are only setting a few bits that were
632 * already set.
633 */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100634 vgic_dist_irq_set(vcpu, lr.irq);
635 if (lr.irq < VGIC_NR_SGIS)
636 dist->irq_sgi_sources[vcpu_id][lr.irq] |= 1 << lr.source;
637 lr.state &= ~LR_STATE_PENDING;
638 vgic_set_lr(vcpu, i, lr);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800639
640 /*
641 * If there's no state left on the LR (it could still be
642 * active), then the LR does not hold any useful info and can
643 * be marked as free for other use.
644 */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100645 if (!(lr.state & LR_STATE_MASK))
646 vgic_retire_lr(i, lr.irq, vcpu);
Christoffer Dallcbd333a2013-11-15 20:51:31 -0800647
648 /* Finally update the VGIC state. */
649 vgic_update_state(vcpu->kvm);
650 }
651}
652
Christoffer Dall90a53552013-10-25 21:22:31 +0100653/* Handle reads of GICD_CPENDSGIRn and GICD_SPENDSGIRn */
654static bool read_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
655 struct kvm_exit_mmio *mmio,
656 phys_addr_t offset)
Christoffer Dallc07a0192013-10-25 21:17:31 +0100657{
Christoffer Dall90a53552013-10-25 21:22:31 +0100658 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
659 int sgi;
660 int min_sgi = (offset & ~0x3) * 4;
661 int max_sgi = min_sgi + 3;
662 int vcpu_id = vcpu->vcpu_id;
663 u32 reg = 0;
664
665 /* Copy source SGIs from distributor side */
666 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
667 int shift = 8 * (sgi - min_sgi);
668 reg |= (u32)dist->irq_sgi_sources[vcpu_id][sgi] << shift;
669 }
670
671 mmio_data_write(mmio, ~0, reg);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100672 return false;
673}
674
Christoffer Dall90a53552013-10-25 21:22:31 +0100675static bool write_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
676 struct kvm_exit_mmio *mmio,
677 phys_addr_t offset, bool set)
678{
679 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
680 int sgi;
681 int min_sgi = (offset & ~0x3) * 4;
682 int max_sgi = min_sgi + 3;
683 int vcpu_id = vcpu->vcpu_id;
684 u32 reg;
685 bool updated = false;
686
687 reg = mmio_data_read(mmio, ~0);
688
689 /* Clear pending SGIs on the distributor */
690 for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
691 u8 mask = reg >> (8 * (sgi - min_sgi));
692 if (set) {
693 if ((dist->irq_sgi_sources[vcpu_id][sgi] & mask) != mask)
694 updated = true;
695 dist->irq_sgi_sources[vcpu_id][sgi] |= mask;
696 } else {
697 if (dist->irq_sgi_sources[vcpu_id][sgi] & mask)
698 updated = true;
699 dist->irq_sgi_sources[vcpu_id][sgi] &= ~mask;
700 }
701 }
702
703 if (updated)
704 vgic_update_state(vcpu->kvm);
705
706 return updated;
707}
708
Christoffer Dallc07a0192013-10-25 21:17:31 +0100709static bool handle_mmio_sgi_set(struct kvm_vcpu *vcpu,
710 struct kvm_exit_mmio *mmio,
711 phys_addr_t offset)
712{
Christoffer Dall90a53552013-10-25 21:22:31 +0100713 if (!mmio->is_write)
714 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
715 else
716 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, true);
717}
718
719static bool handle_mmio_sgi_clear(struct kvm_vcpu *vcpu,
720 struct kvm_exit_mmio *mmio,
721 phys_addr_t offset)
722{
723 if (!mmio->is_write)
724 return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
725 else
726 return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, false);
Christoffer Dallc07a0192013-10-25 21:17:31 +0100727}
728
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500729/*
730 * I would have liked to use the kvm_bus_io_*() API instead, but it
731 * cannot cope with banked registers (only the VM pointer is passed
732 * around, and we need the vcpu). One of these days, someone please
733 * fix it!
734 */
735struct mmio_range {
736 phys_addr_t base;
737 unsigned long len;
738 bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
739 phys_addr_t offset);
740};
741
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700742static const struct mmio_range vgic_dist_ranges[] = {
Marc Zyngierb47ef922013-01-21 19:36:14 -0500743 {
744 .base = GIC_DIST_CTRL,
745 .len = 12,
746 .handle_mmio = handle_mmio_misc,
747 },
748 {
749 .base = GIC_DIST_IGROUP,
750 .len = VGIC_NR_IRQS / 8,
751 .handle_mmio = handle_mmio_raz_wi,
752 },
753 {
754 .base = GIC_DIST_ENABLE_SET,
755 .len = VGIC_NR_IRQS / 8,
756 .handle_mmio = handle_mmio_set_enable_reg,
757 },
758 {
759 .base = GIC_DIST_ENABLE_CLEAR,
760 .len = VGIC_NR_IRQS / 8,
761 .handle_mmio = handle_mmio_clear_enable_reg,
762 },
763 {
764 .base = GIC_DIST_PENDING_SET,
765 .len = VGIC_NR_IRQS / 8,
766 .handle_mmio = handle_mmio_set_pending_reg,
767 },
768 {
769 .base = GIC_DIST_PENDING_CLEAR,
770 .len = VGIC_NR_IRQS / 8,
771 .handle_mmio = handle_mmio_clear_pending_reg,
772 },
773 {
774 .base = GIC_DIST_ACTIVE_SET,
775 .len = VGIC_NR_IRQS / 8,
776 .handle_mmio = handle_mmio_raz_wi,
777 },
778 {
779 .base = GIC_DIST_ACTIVE_CLEAR,
780 .len = VGIC_NR_IRQS / 8,
781 .handle_mmio = handle_mmio_raz_wi,
782 },
783 {
784 .base = GIC_DIST_PRI,
785 .len = VGIC_NR_IRQS,
786 .handle_mmio = handle_mmio_priority_reg,
787 },
788 {
789 .base = GIC_DIST_TARGET,
790 .len = VGIC_NR_IRQS,
791 .handle_mmio = handle_mmio_target_reg,
792 },
793 {
794 .base = GIC_DIST_CONFIG,
795 .len = VGIC_NR_IRQS / 4,
796 .handle_mmio = handle_mmio_cfg_reg,
797 },
798 {
799 .base = GIC_DIST_SOFTINT,
800 .len = 4,
801 .handle_mmio = handle_mmio_sgi_reg,
802 },
Christoffer Dallc07a0192013-10-25 21:17:31 +0100803 {
804 .base = GIC_DIST_SGI_PENDING_CLEAR,
805 .len = VGIC_NR_SGIS,
806 .handle_mmio = handle_mmio_sgi_clear,
807 },
808 {
809 .base = GIC_DIST_SGI_PENDING_SET,
810 .len = VGIC_NR_SGIS,
811 .handle_mmio = handle_mmio_sgi_set,
812 },
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500813 {}
814};
815
816static const
817struct mmio_range *find_matching_range(const struct mmio_range *ranges,
818 struct kvm_exit_mmio *mmio,
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700819 phys_addr_t offset)
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500820{
821 const struct mmio_range *r = ranges;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500822
823 while (r->len) {
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700824 if (offset >= r->base &&
825 (offset + mmio->len) <= (r->base + r->len))
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500826 return r;
827 r++;
828 }
829
830 return NULL;
831}
832
833/**
834 * vgic_handle_mmio - handle an in-kernel MMIO access
835 * @vcpu: pointer to the vcpu performing the access
836 * @run: pointer to the kvm_run structure
837 * @mmio: pointer to the data describing the access
838 *
839 * returns true if the MMIO access has been performed in kernel space,
840 * and false if it needs to be emulated in user space.
841 */
842bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
843 struct kvm_exit_mmio *mmio)
844{
Marc Zyngierb47ef922013-01-21 19:36:14 -0500845 const struct mmio_range *range;
846 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
847 unsigned long base = dist->vgic_dist_base;
848 bool updated_state;
849 unsigned long offset;
850
851 if (!irqchip_in_kernel(vcpu->kvm) ||
852 mmio->phys_addr < base ||
853 (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
854 return false;
855
856 /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
857 if (mmio->len > 4) {
858 kvm_inject_dabt(vcpu, mmio->phys_addr);
859 return true;
860 }
861
Christoffer Dall1006e8c2013-09-23 14:55:56 -0700862 offset = mmio->phys_addr - base;
863 range = find_matching_range(vgic_dist_ranges, mmio, offset);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500864 if (unlikely(!range || !range->handle_mmio)) {
865 pr_warn("Unhandled access %d %08llx %d\n",
866 mmio->is_write, mmio->phys_addr, mmio->len);
867 return false;
868 }
869
870 spin_lock(&vcpu->kvm->arch.vgic.lock);
871 offset = mmio->phys_addr - range->base - base;
872 updated_state = range->handle_mmio(vcpu, mmio, offset);
873 spin_unlock(&vcpu->kvm->arch.vgic.lock);
874 kvm_prepare_mmio(run, mmio);
875 kvm_handle_mmio_return(vcpu, run);
876
Marc Zyngier5863c2c2013-01-21 19:36:15 -0500877 if (updated_state)
878 vgic_kick_vcpus(vcpu->kvm);
879
Marc Zyngierb47ef922013-01-21 19:36:14 -0500880 return true;
881}
882
883static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
884{
885 struct kvm *kvm = vcpu->kvm;
886 struct vgic_dist *dist = &kvm->arch.vgic;
887 int nrcpus = atomic_read(&kvm->online_vcpus);
888 u8 target_cpus;
889 int sgi, mode, c, vcpu_id;
890
891 vcpu_id = vcpu->vcpu_id;
892
893 sgi = reg & 0xf;
894 target_cpus = (reg >> 16) & 0xff;
895 mode = (reg >> 24) & 3;
896
897 switch (mode) {
898 case 0:
899 if (!target_cpus)
900 return;
Haibin Wang91021a62014-04-10 13:14:32 +0100901 break;
Marc Zyngierb47ef922013-01-21 19:36:14 -0500902
903 case 1:
904 target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
905 break;
906
907 case 2:
908 target_cpus = 1 << vcpu_id;
909 break;
910 }
911
912 kvm_for_each_vcpu(c, vcpu, kvm) {
913 if (target_cpus & 1) {
914 /* Flag the SGI as pending */
915 vgic_dist_irq_set(vcpu, sgi);
916 dist->irq_sgi_sources[c][sgi] |= 1 << vcpu_id;
917 kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
918 }
919
920 target_cpus >>= 1;
921 }
922}
923
924static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
925{
Marc Zyngier9d949dc2013-01-21 19:36:14 -0500926 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
927 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
928 unsigned long pending_private, pending_shared;
929 int vcpu_id;
930
931 vcpu_id = vcpu->vcpu_id;
932 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
933 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
934
935 pending = vgic_bitmap_get_cpu_map(&dist->irq_state, vcpu_id);
936 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
937 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
938
939 pending = vgic_bitmap_get_shared_map(&dist->irq_state);
940 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
941 bitmap_and(pend_shared, pending, enabled, VGIC_NR_SHARED_IRQS);
942 bitmap_and(pend_shared, pend_shared,
943 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
944 VGIC_NR_SHARED_IRQS);
945
946 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
947 pending_shared = find_first_bit(pend_shared, VGIC_NR_SHARED_IRQS);
948 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
949 pending_shared < VGIC_NR_SHARED_IRQS);
Marc Zyngierb47ef922013-01-21 19:36:14 -0500950}
951
952/*
953 * Update the interrupt state and determine which CPUs have pending
954 * interrupts. Must be called with distributor lock held.
955 */
956static void vgic_update_state(struct kvm *kvm)
957{
958 struct vgic_dist *dist = &kvm->arch.vgic;
959 struct kvm_vcpu *vcpu;
960 int c;
961
962 if (!dist->enabled) {
963 set_bit(0, &dist->irq_pending_on_cpu);
964 return;
965 }
966
967 kvm_for_each_vcpu(c, vcpu, kvm) {
968 if (compute_pending_for_cpu(vcpu)) {
969 pr_debug("CPU%d has pending interrupts\n", c);
970 set_bit(c, &dist->irq_pending_on_cpu);
971 }
972 }
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500973}
Christoffer Dall330690c2013-01-21 19:36:13 -0500974
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100975static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
976{
Marc Zyngier8f186d52014-02-04 18:13:03 +0000977 return vgic_ops->get_lr(vcpu, lr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100978}
979
980static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
981 struct vgic_lr vlr)
982{
Marc Zyngier8f186d52014-02-04 18:13:03 +0000983 vgic_ops->set_lr(vcpu, lr, vlr);
Marc Zyngier8d5c6b02013-06-03 15:55:02 +0100984}
985
Marc Zyngier69bb2c92013-06-04 10:29:39 +0100986static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
987 struct vgic_lr vlr)
988{
Marc Zyngier8f186d52014-02-04 18:13:03 +0000989 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier69bb2c92013-06-04 10:29:39 +0100990}
991
992static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
993{
Marc Zyngier8f186d52014-02-04 18:13:03 +0000994 return vgic_ops->get_elrsr(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +0100995}
996
Marc Zyngier8d6a0312013-06-04 10:33:43 +0100997static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
998{
Marc Zyngier8f186d52014-02-04 18:13:03 +0000999 return vgic_ops->get_eisr(vcpu);
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001000}
1001
Marc Zyngier495dd852013-06-04 11:02:10 +01001002static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
1003{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001004 return vgic_ops->get_interrupt_status(vcpu);
Marc Zyngier495dd852013-06-04 11:02:10 +01001005}
1006
Marc Zyngier909d9b52013-06-04 11:24:17 +01001007static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
1008{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001009 vgic_ops->enable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001010}
1011
1012static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
1013{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001014 vgic_ops->disable_underflow(vcpu);
Marc Zyngier909d9b52013-06-04 11:24:17 +01001015}
1016
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001017static inline void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1018{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001019 vgic_ops->get_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001020}
1021
1022static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
1023{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001024 vgic_ops->set_vmcr(vcpu, vmcr);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001025}
1026
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001027static inline void vgic_enable(struct kvm_vcpu *vcpu)
1028{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001029 vgic_ops->enable(vcpu);
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001030}
1031
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001032static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1033{
1034 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1035 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1036
1037 vlr.state = 0;
1038 vgic_set_lr(vcpu, lr_nr, vlr);
1039 clear_bit(lr_nr, vgic_cpu->lr_used);
1040 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1041}
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001042
1043/*
1044 * An interrupt may have been disabled after being made pending on the
1045 * CPU interface (the classic case is a timer running while we're
1046 * rebooting the guest - the interrupt would kick as soon as the CPU
1047 * interface gets enabled, with deadly consequences).
1048 *
1049 * The solution is to examine already active LRs, and check the
1050 * interrupt is still enabled. If not, just retire it.
1051 */
1052static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1053{
1054 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1055 int lr;
1056
Marc Zyngier8f186d52014-02-04 18:13:03 +00001057 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001058 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001059
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001060 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
1061 vgic_retire_lr(lr, vlr.irq, vcpu);
1062 if (vgic_irq_is_active(vcpu, vlr.irq))
1063 vgic_irq_clear_active(vcpu, vlr.irq);
Marc Zyngiera1fcb442013-01-21 19:36:15 -05001064 }
1065 }
1066}
1067
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001068/*
1069 * Queue an interrupt to a CPU virtual interface. Return true on success,
1070 * or false if it wasn't possible to queue it.
1071 */
1072static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1073{
1074 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001075 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001076 int lr;
1077
1078 /* Sanitize the input... */
1079 BUG_ON(sgi_source_id & ~7);
1080 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
1081 BUG_ON(irq >= VGIC_NR_IRQS);
1082
1083 kvm_debug("Queue IRQ%d\n", irq);
1084
1085 lr = vgic_cpu->vgic_irq_lr_map[irq];
1086
1087 /* Do we have an active interrupt for the same CPUID? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001088 if (lr != LR_EMPTY) {
1089 vlr = vgic_get_lr(vcpu, lr);
1090 if (vlr.source == sgi_source_id) {
1091 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1092 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1093 vlr.state |= LR_STATE_PENDING;
1094 vgic_set_lr(vcpu, lr, vlr);
1095 return true;
1096 }
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001097 }
1098
1099 /* Try to use another LR for this interrupt */
1100 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001101 vgic->nr_lr);
1102 if (lr >= vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001103 return false;
1104
1105 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001106 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1107 set_bit(lr, vgic_cpu->lr_used);
1108
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001109 vlr.irq = irq;
1110 vlr.source = sgi_source_id;
1111 vlr.state = LR_STATE_PENDING;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001112 if (!vgic_irq_is_edge(vcpu, irq))
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001113 vlr.state |= LR_EOI_INT;
1114
1115 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001116
1117 return true;
1118}
1119
1120static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
1121{
1122 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1123 unsigned long sources;
1124 int vcpu_id = vcpu->vcpu_id;
1125 int c;
1126
1127 sources = dist->irq_sgi_sources[vcpu_id][irq];
1128
1129 for_each_set_bit(c, &sources, VGIC_MAX_CPUS) {
1130 if (vgic_queue_irq(vcpu, c, irq))
1131 clear_bit(c, &sources);
1132 }
1133
1134 dist->irq_sgi_sources[vcpu_id][irq] = sources;
1135
1136 /*
1137 * If the sources bitmap has been cleared it means that we
1138 * could queue all the SGIs onto link registers (see the
1139 * clear_bit above), and therefore we are done with them in
1140 * our emulated gic and can get rid of them.
1141 */
1142 if (!sources) {
1143 vgic_dist_irq_clear(vcpu, irq);
1144 vgic_cpu_irq_clear(vcpu, irq);
1145 return true;
1146 }
1147
1148 return false;
1149}
1150
1151static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
1152{
1153 if (vgic_irq_is_active(vcpu, irq))
1154 return true; /* level interrupt, already queued */
1155
1156 if (vgic_queue_irq(vcpu, 0, irq)) {
1157 if (vgic_irq_is_edge(vcpu, irq)) {
1158 vgic_dist_irq_clear(vcpu, irq);
1159 vgic_cpu_irq_clear(vcpu, irq);
1160 } else {
1161 vgic_irq_set_active(vcpu, irq);
1162 }
1163
1164 return true;
1165 }
1166
1167 return false;
1168}
1169
1170/*
1171 * Fill the list registers with pending interrupts before running the
1172 * guest.
1173 */
1174static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1175{
1176 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1177 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1178 int i, vcpu_id;
1179 int overflow = 0;
1180
1181 vcpu_id = vcpu->vcpu_id;
1182
1183 /*
1184 * We may not have any pending interrupt, or the interrupts
1185 * may have been serviced from another vcpu. In all cases,
1186 * move along.
1187 */
1188 if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
1189 pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
1190 goto epilog;
1191 }
1192
1193 /* SGIs */
1194 for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
1195 if (!vgic_queue_sgi(vcpu, i))
1196 overflow = 1;
1197 }
1198
1199 /* PPIs */
1200 for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
1201 if (!vgic_queue_hwirq(vcpu, i))
1202 overflow = 1;
1203 }
1204
1205 /* SPIs */
1206 for_each_set_bit(i, vgic_cpu->pending_shared, VGIC_NR_SHARED_IRQS) {
1207 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
1208 overflow = 1;
1209 }
1210
1211epilog:
1212 if (overflow) {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001213 vgic_enable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001214 } else {
Marc Zyngier909d9b52013-06-04 11:24:17 +01001215 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001216 /*
1217 * We're about to run this VCPU, and we've consumed
1218 * everything the distributor had in store for
1219 * us. Claim we don't have anything pending. We'll
1220 * adjust that if needed while exiting.
1221 */
1222 clear_bit(vcpu_id, &dist->irq_pending_on_cpu);
1223 }
1224}
1225
1226static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1227{
Marc Zyngier495dd852013-06-04 11:02:10 +01001228 u32 status = vgic_get_interrupt_status(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001229 bool level_pending = false;
1230
Marc Zyngier495dd852013-06-04 11:02:10 +01001231 kvm_debug("STATUS = %08x\n", status);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001232
Marc Zyngier495dd852013-06-04 11:02:10 +01001233 if (status & INT_STATUS_EOI) {
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001234 /*
1235 * Some level interrupts have been EOIed. Clear their
1236 * active bit.
1237 */
Marc Zyngier8d6a0312013-06-04 10:33:43 +01001238 u64 eisr = vgic_get_eisr(vcpu);
1239 unsigned long *eisr_ptr = (unsigned long *)&eisr;
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001240 int lr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001241
Marc Zyngier8f186d52014-02-04 18:13:03 +00001242 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001243 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001244
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001245 vgic_irq_clear_active(vcpu, vlr.irq);
1246 WARN_ON(vlr.state & LR_STATE_MASK);
1247 vlr.state = 0;
1248 vgic_set_lr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001249
1250 /* Any additional pending interrupt? */
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001251 if (vgic_dist_irq_is_pending(vcpu, vlr.irq)) {
1252 vgic_cpu_irq_set(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001253 level_pending = true;
1254 } else {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001255 vgic_cpu_irq_clear(vcpu, vlr.irq);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001256 }
Marc Zyngier75da01e2013-01-31 11:25:52 +00001257
1258 /*
1259 * Despite being EOIed, the LR may not have
1260 * been marked as empty.
1261 */
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001262 vgic_sync_lr_elrsr(vcpu, lr, vlr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001263 }
1264 }
1265
Marc Zyngier495dd852013-06-04 11:02:10 +01001266 if (status & INT_STATUS_UNDERFLOW)
Marc Zyngier909d9b52013-06-04 11:24:17 +01001267 vgic_disable_underflow(vcpu);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001268
1269 return level_pending;
1270}
1271
1272/*
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001273 * Sync back the VGIC state after a guest run. The distributor lock is
1274 * needed so we don't get preempted in the middle of the state processing.
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001275 */
1276static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1277{
1278 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1279 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001280 u64 elrsr;
1281 unsigned long *elrsr_ptr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001282 int lr, pending;
1283 bool level_pending;
1284
1285 level_pending = vgic_process_maintenance(vcpu);
Marc Zyngier69bb2c92013-06-04 10:29:39 +01001286 elrsr = vgic_get_elrsr(vcpu);
1287 elrsr_ptr = (unsigned long *)&elrsr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001288
1289 /* Clear mappings for empty LRs */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001290 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001291 struct vgic_lr vlr;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001292
1293 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1294 continue;
1295
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001296 vlr = vgic_get_lr(vcpu, lr);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001297
Marc Zyngier8d5c6b02013-06-03 15:55:02 +01001298 BUG_ON(vlr.irq >= VGIC_NR_IRQS);
1299 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001300 }
1301
1302 /* Check if we still have something up our sleeve... */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001303 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1304 if (level_pending || pending < vgic->nr_lr)
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001305 set_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
1306}
1307
1308void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1309{
1310 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1311
1312 if (!irqchip_in_kernel(vcpu->kvm))
1313 return;
1314
1315 spin_lock(&dist->lock);
1316 __kvm_vgic_flush_hwstate(vcpu);
1317 spin_unlock(&dist->lock);
1318}
1319
1320void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1321{
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001322 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1323
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001324 if (!irqchip_in_kernel(vcpu->kvm))
1325 return;
1326
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001327 spin_lock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001328 __kvm_vgic_sync_hwstate(vcpu);
Marc Zyngier33c83cb2013-02-01 18:28:30 +00001329 spin_unlock(&dist->lock);
Marc Zyngier9d949dc2013-01-21 19:36:14 -05001330}
1331
1332int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1333{
1334 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1335
1336 if (!irqchip_in_kernel(vcpu->kvm))
1337 return 0;
1338
1339 return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
1340}
1341
Marc Zyngier5863c2c2013-01-21 19:36:15 -05001342static void vgic_kick_vcpus(struct kvm *kvm)
1343{
1344 struct kvm_vcpu *vcpu;
1345 int c;
1346
1347 /*
1348 * We've injected an interrupt, time to find out who deserves
1349 * a good kick...
1350 */
1351 kvm_for_each_vcpu(c, vcpu, kvm) {
1352 if (kvm_vgic_vcpu_pending_irq(vcpu))
1353 kvm_vcpu_kick(vcpu);
1354 }
1355}
1356
1357static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1358{
1359 int is_edge = vgic_irq_is_edge(vcpu, irq);
1360 int state = vgic_dist_irq_is_pending(vcpu, irq);
1361
1362 /*
1363 * Only inject an interrupt if:
1364 * - edge triggered and we have a rising edge
1365 * - level triggered and we change level
1366 */
1367 if (is_edge)
1368 return level > state;
1369 else
1370 return level != state;
1371}
1372
1373static bool vgic_update_irq_state(struct kvm *kvm, int cpuid,
1374 unsigned int irq_num, bool level)
1375{
1376 struct vgic_dist *dist = &kvm->arch.vgic;
1377 struct kvm_vcpu *vcpu;
1378 int is_edge, is_level;
1379 int enabled;
1380 bool ret = true;
1381
1382 spin_lock(&dist->lock);
1383
1384 vcpu = kvm_get_vcpu(kvm, cpuid);
1385 is_edge = vgic_irq_is_edge(vcpu, irq_num);
1386 is_level = !is_edge;
1387
1388 if (!vgic_validate_injection(vcpu, irq_num, level)) {
1389 ret = false;
1390 goto out;
1391 }
1392
1393 if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
1394 cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
1395 vcpu = kvm_get_vcpu(kvm, cpuid);
1396 }
1397
1398 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
1399
1400 if (level)
1401 vgic_dist_irq_set(vcpu, irq_num);
1402 else
1403 vgic_dist_irq_clear(vcpu, irq_num);
1404
1405 enabled = vgic_irq_is_enabled(vcpu, irq_num);
1406
1407 if (!enabled) {
1408 ret = false;
1409 goto out;
1410 }
1411
1412 if (is_level && vgic_irq_is_active(vcpu, irq_num)) {
1413 /*
1414 * Level interrupt in progress, will be picked up
1415 * when EOId.
1416 */
1417 ret = false;
1418 goto out;
1419 }
1420
1421 if (level) {
1422 vgic_cpu_irq_set(vcpu, irq_num);
1423 set_bit(cpuid, &dist->irq_pending_on_cpu);
1424 }
1425
1426out:
1427 spin_unlock(&dist->lock);
1428
1429 return ret;
1430}
1431
1432/**
1433 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1434 * @kvm: The VM structure pointer
1435 * @cpuid: The CPU for PPIs
1436 * @irq_num: The IRQ number that is assigned to the device
1437 * @level: Edge-triggered: true: to trigger the interrupt
1438 * false: to ignore the call
1439 * Level-sensitive true: activates an interrupt
1440 * false: deactivates an interrupt
1441 *
1442 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1443 * level-sensitive interrupts. You can think of the level parameter as 1
1444 * being HIGH and 0 being LOW and all devices being active-HIGH.
1445 */
1446int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1447 bool level)
1448{
1449 if (vgic_update_irq_state(kvm, cpuid, irq_num, level))
1450 vgic_kick_vcpus(kvm);
1451
1452 return 0;
1453}
1454
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001455static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1456{
1457 /*
1458 * We cannot rely on the vgic maintenance interrupt to be
1459 * delivered synchronously. This means we can only use it to
1460 * exit the VM, and we perform the handling of EOIed
1461 * interrupts on the exit path (see vgic_process_maintenance).
1462 */
1463 return IRQ_HANDLED;
1464}
1465
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001466/**
1467 * kvm_vgic_vcpu_init - Initialize per-vcpu VGIC state
1468 * @vcpu: pointer to the vcpu struct
1469 *
1470 * Initialize the vgic_cpu struct and vgic_dist struct fields pertaining to
1471 * this vcpu and enable the VGIC for this VCPU
1472 */
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001473int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
1474{
1475 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1476 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1477 int i;
1478
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001479 if (vcpu->vcpu_id >= VGIC_MAX_CPUS)
1480 return -EBUSY;
1481
1482 for (i = 0; i < VGIC_NR_IRQS; i++) {
1483 if (i < VGIC_NR_PPIS)
1484 vgic_bitmap_set_irq_val(&dist->irq_enabled,
1485 vcpu->vcpu_id, i, 1);
1486 if (i < VGIC_NR_PRIVATE_IRQS)
1487 vgic_bitmap_set_irq_val(&dist->irq_cfg,
1488 vcpu->vcpu_id, i, VGIC_CFG_EDGE);
1489
1490 vgic_cpu->vgic_irq_lr_map[i] = LR_EMPTY;
1491 }
1492
Marc Zyngierca85f622013-06-18 19:17:28 +01001493 /*
1494 * Store the number of LRs per vcpu, so we don't have to go
1495 * all the way to the distributor structure to find out. Only
1496 * assembly code should use this one.
1497 */
Marc Zyngier8f186d52014-02-04 18:13:03 +00001498 vgic_cpu->nr_lr = vgic->nr_lr;
Marc Zyngierda8dafd12013-06-04 11:36:38 +01001499
1500 vgic_enable(vcpu);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001501
1502 return 0;
1503}
1504
1505static void vgic_init_maintenance_interrupt(void *info)
1506{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001507 enable_percpu_irq(vgic->maint_irq, 0);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001508}
1509
1510static int vgic_cpu_notify(struct notifier_block *self,
1511 unsigned long action, void *cpu)
1512{
1513 switch (action) {
1514 case CPU_STARTING:
1515 case CPU_STARTING_FROZEN:
1516 vgic_init_maintenance_interrupt(NULL);
1517 break;
1518 case CPU_DYING:
1519 case CPU_DYING_FROZEN:
Marc Zyngier8f186d52014-02-04 18:13:03 +00001520 disable_percpu_irq(vgic->maint_irq);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001521 break;
1522 }
1523
1524 return NOTIFY_OK;
1525}
1526
1527static struct notifier_block vgic_cpu_nb = {
1528 .notifier_call = vgic_cpu_notify,
1529};
1530
Marc Zyngier8f186d52014-02-04 18:13:03 +00001531static const struct of_device_id vgic_ids[] = {
1532 { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
Marc Zyngier67b2abf2013-07-09 10:45:49 +01001533 { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
Marc Zyngier8f186d52014-02-04 18:13:03 +00001534 {},
1535};
1536
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001537int kvm_vgic_hyp_init(void)
1538{
Marc Zyngier8f186d52014-02-04 18:13:03 +00001539 const struct of_device_id *matched_id;
1540 int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
1541 const struct vgic_params **);
Marc Zyngierca85f622013-06-18 19:17:28 +01001542 struct device_node *vgic_node;
Marc Zyngier8f186d52014-02-04 18:13:03 +00001543 int ret;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001544
Marc Zyngier8f186d52014-02-04 18:13:03 +00001545 vgic_node = of_find_matching_node_and_match(NULL,
1546 vgic_ids, &matched_id);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001547 if (!vgic_node) {
Marc Zyngier8f186d52014-02-04 18:13:03 +00001548 kvm_err("error: no compatible GIC node found\n");
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001549 return -ENODEV;
1550 }
1551
Marc Zyngier8f186d52014-02-04 18:13:03 +00001552 vgic_probe = matched_id->data;
1553 ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
1554 if (ret)
1555 return ret;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001556
Marc Zyngier8f186d52014-02-04 18:13:03 +00001557 ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001558 "vgic", kvm_get_running_vcpus());
1559 if (ret) {
Marc Zyngier8f186d52014-02-04 18:13:03 +00001560 kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
1561 return ret;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001562 }
1563
Ming Lei553f8092014-04-07 01:36:08 +08001564 ret = __register_cpu_notifier(&vgic_cpu_nb);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001565 if (ret) {
1566 kvm_err("Cannot register vgic CPU notifier\n");
1567 goto out_free_irq;
1568 }
1569
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001570 on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
1571
Marc Zyngier1a9b1302013-06-21 11:57:56 +01001572 /* Callback into for arch code for setup */
1573 vgic_arch_setup(vgic);
1574
Marc Zyngier8f186d52014-02-04 18:13:03 +00001575 return 0;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001576
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001577out_free_irq:
Marc Zyngier8f186d52014-02-04 18:13:03 +00001578 free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001579 return ret;
1580}
1581
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001582/**
1583 * kvm_vgic_init - Initialize global VGIC state before running any VCPUs
1584 * @kvm: pointer to the kvm struct
1585 *
1586 * Map the virtual CPU interface into the VM before running any VCPUs. We
1587 * can't do this at creation time, because user space must first set the
1588 * virtual CPU interface address in the guest physical address space. Also
1589 * initialize the ITARGETSRn regs to 0 on the emulated distributor.
1590 */
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001591int kvm_vgic_init(struct kvm *kvm)
1592{
1593 int ret = 0, i;
1594
Christoffer Dalle1ba0202013-09-23 14:55:55 -07001595 if (!irqchip_in_kernel(kvm))
1596 return 0;
1597
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001598 mutex_lock(&kvm->lock);
1599
1600 if (vgic_initialized(kvm))
1601 goto out;
1602
1603 if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
1604 IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
1605 kvm_err("Need to set vgic cpu and dist addresses first\n");
1606 ret = -ENXIO;
1607 goto out;
1608 }
1609
1610 ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
Marc Zyngier8f186d52014-02-04 18:13:03 +00001611 vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE);
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001612 if (ret) {
1613 kvm_err("Unable to remap VGIC CPU to VCPU\n");
1614 goto out;
1615 }
1616
1617 for (i = VGIC_NR_PRIVATE_IRQS; i < VGIC_NR_IRQS; i += 4)
1618 vgic_set_target_reg(kvm, 0, i);
1619
1620 kvm->arch.vgic.ready = true;
1621out:
1622 mutex_unlock(&kvm->lock);
1623 return ret;
1624}
1625
1626int kvm_vgic_create(struct kvm *kvm)
1627{
Christoffer Dall73306722013-10-25 17:29:18 +01001628 int i, vcpu_lock_idx = -1, ret = 0;
1629 struct kvm_vcpu *vcpu;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001630
1631 mutex_lock(&kvm->lock);
1632
Christoffer Dall73306722013-10-25 17:29:18 +01001633 if (kvm->arch.vgic.vctrl_base) {
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001634 ret = -EEXIST;
1635 goto out;
1636 }
1637
Christoffer Dall73306722013-10-25 17:29:18 +01001638 /*
1639 * Any time a vcpu is run, vcpu_load is called which tries to grab the
1640 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
1641 * that no other VCPUs are run while we create the vgic.
1642 */
1643 kvm_for_each_vcpu(i, vcpu, kvm) {
1644 if (!mutex_trylock(&vcpu->mutex))
1645 goto out_unlock;
1646 vcpu_lock_idx = i;
1647 }
1648
1649 kvm_for_each_vcpu(i, vcpu, kvm) {
1650 if (vcpu->arch.has_run_once) {
1651 ret = -EBUSY;
1652 goto out_unlock;
1653 }
1654 }
1655
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001656 spin_lock_init(&kvm->arch.vgic.lock);
Marc Zyngierf982cf42014-05-15 10:03:25 +01001657 kvm->arch.vgic.in_kernel = true;
Marc Zyngier8f186d52014-02-04 18:13:03 +00001658 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001659 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
1660 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
1661
Christoffer Dall73306722013-10-25 17:29:18 +01001662out_unlock:
1663 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1664 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1665 mutex_unlock(&vcpu->mutex);
1666 }
1667
Marc Zyngier01ac5e32013-01-21 19:36:16 -05001668out:
1669 mutex_unlock(&kvm->lock);
1670 return ret;
1671}
1672
Christoffer Dall330690c2013-01-21 19:36:13 -05001673static bool vgic_ioaddr_overlap(struct kvm *kvm)
1674{
1675 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
1676 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
1677
1678 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
1679 return 0;
1680 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
1681 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
1682 return -EBUSY;
1683 return 0;
1684}
1685
1686static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
1687 phys_addr_t addr, phys_addr_t size)
1688{
1689 int ret;
1690
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001691 if (addr & ~KVM_PHYS_MASK)
1692 return -E2BIG;
1693
1694 if (addr & (SZ_4K - 1))
1695 return -EINVAL;
1696
Christoffer Dall330690c2013-01-21 19:36:13 -05001697 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
1698 return -EEXIST;
1699 if (addr + size < addr)
1700 return -EINVAL;
1701
Haibin Wang30c21172014-04-29 14:49:17 +08001702 *ioaddr = addr;
Christoffer Dall330690c2013-01-21 19:36:13 -05001703 ret = vgic_ioaddr_overlap(kvm);
1704 if (ret)
Haibin Wang30c21172014-04-29 14:49:17 +08001705 *ioaddr = VGIC_ADDR_UNDEF;
1706
Christoffer Dall330690c2013-01-21 19:36:13 -05001707 return ret;
1708}
1709
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001710/**
1711 * kvm_vgic_addr - set or get vgic VM base addresses
1712 * @kvm: pointer to the vm struct
1713 * @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
1714 * @addr: pointer to address value
1715 * @write: if true set the address in the VM address space, if false read the
1716 * address
1717 *
1718 * Set or get the vgic base addresses for the distributor and the virtual CPU
1719 * interface in the VM physical address space. These addresses are properties
1720 * of the emulated core/SoC and therefore user space initially knows this
1721 * information.
1722 */
1723int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
Christoffer Dall330690c2013-01-21 19:36:13 -05001724{
1725 int r = 0;
1726 struct vgic_dist *vgic = &kvm->arch.vgic;
1727
Christoffer Dall330690c2013-01-21 19:36:13 -05001728 mutex_lock(&kvm->lock);
1729 switch (type) {
1730 case KVM_VGIC_V2_ADDR_TYPE_DIST:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001731 if (write) {
1732 r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
1733 *addr, KVM_VGIC_V2_DIST_SIZE);
1734 } else {
1735 *addr = vgic->vgic_dist_base;
1736 }
Christoffer Dall330690c2013-01-21 19:36:13 -05001737 break;
1738 case KVM_VGIC_V2_ADDR_TYPE_CPU:
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001739 if (write) {
1740 r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
1741 *addr, KVM_VGIC_V2_CPU_SIZE);
1742 } else {
1743 *addr = vgic->vgic_cpu_base;
1744 }
Christoffer Dall330690c2013-01-21 19:36:13 -05001745 break;
1746 default:
1747 r = -ENODEV;
1748 }
1749
1750 mutex_unlock(&kvm->lock);
1751 return r;
1752}
Christoffer Dall73306722013-10-25 17:29:18 +01001753
Christoffer Dallc07a0192013-10-25 21:17:31 +01001754static bool handle_cpu_mmio_misc(struct kvm_vcpu *vcpu,
1755 struct kvm_exit_mmio *mmio, phys_addr_t offset)
1756{
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001757 bool updated = false;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001758 struct vgic_vmcr vmcr;
1759 u32 *vmcr_field;
1760 u32 reg;
1761
1762 vgic_get_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001763
1764 switch (offset & ~0x3) {
1765 case GIC_CPU_CTRL:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001766 vmcr_field = &vmcr.ctlr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001767 break;
1768 case GIC_CPU_PRIMASK:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001769 vmcr_field = &vmcr.pmr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001770 break;
1771 case GIC_CPU_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001772 vmcr_field = &vmcr.bpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001773 break;
1774 case GIC_CPU_ALIAS_BINPOINT:
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001775 vmcr_field = &vmcr.abpr;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001776 break;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001777 default:
1778 BUG();
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001779 }
1780
1781 if (!mmio->is_write) {
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001782 reg = *vmcr_field;
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001783 mmio_data_write(mmio, ~0, reg);
1784 } else {
1785 reg = mmio_data_read(mmio, ~0);
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001786 if (reg != *vmcr_field) {
1787 *vmcr_field = reg;
1788 vgic_set_vmcr(vcpu, &vmcr);
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001789 updated = true;
Marc Zyngierbeee38b2014-02-04 17:48:10 +00001790 }
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001791 }
1792 return updated;
Christoffer Dallc07a0192013-10-25 21:17:31 +01001793}
1794
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001795static bool handle_mmio_abpr(struct kvm_vcpu *vcpu,
1796 struct kvm_exit_mmio *mmio, phys_addr_t offset)
1797{
1798 return handle_cpu_mmio_misc(vcpu, mmio, GIC_CPU_ALIAS_BINPOINT);
1799}
1800
1801static bool handle_cpu_mmio_ident(struct kvm_vcpu *vcpu,
1802 struct kvm_exit_mmio *mmio,
1803 phys_addr_t offset)
1804{
1805 u32 reg;
1806
1807 if (mmio->is_write)
1808 return false;
1809
1810 /* GICC_IIDR */
1811 reg = (PRODUCT_ID_KVM << 20) |
1812 (GICC_ARCH_VERSION_V2 << 16) |
1813 (IMPLEMENTER_ARM << 0);
1814 mmio_data_write(mmio, ~0, reg);
1815 return false;
1816}
1817
1818/*
1819 * CPU Interface Register accesses - these are not accessed by the VM, but by
1820 * user space for saving and restoring VGIC state.
1821 */
Christoffer Dallc07a0192013-10-25 21:17:31 +01001822static const struct mmio_range vgic_cpu_ranges[] = {
1823 {
1824 .base = GIC_CPU_CTRL,
1825 .len = 12,
1826 .handle_mmio = handle_cpu_mmio_misc,
1827 },
1828 {
1829 .base = GIC_CPU_ALIAS_BINPOINT,
1830 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001831 .handle_mmio = handle_mmio_abpr,
Christoffer Dallc07a0192013-10-25 21:17:31 +01001832 },
1833 {
1834 .base = GIC_CPU_ACTIVEPRIO,
1835 .len = 16,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001836 .handle_mmio = handle_mmio_raz_wi,
Christoffer Dallc07a0192013-10-25 21:17:31 +01001837 },
1838 {
1839 .base = GIC_CPU_IDENT,
1840 .len = 4,
Christoffer Dallfa20f5ae2013-09-23 14:55:57 -07001841 .handle_mmio = handle_cpu_mmio_ident,
Christoffer Dallc07a0192013-10-25 21:17:31 +01001842 },
1843};
1844
1845static int vgic_attr_regs_access(struct kvm_device *dev,
1846 struct kvm_device_attr *attr,
1847 u32 *reg, bool is_write)
1848{
1849 const struct mmio_range *r = NULL, *ranges;
1850 phys_addr_t offset;
1851 int ret, cpuid, c;
1852 struct kvm_vcpu *vcpu, *tmp_vcpu;
1853 struct vgic_dist *vgic;
1854 struct kvm_exit_mmio mmio;
1855
1856 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
1857 cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
1858 KVM_DEV_ARM_VGIC_CPUID_SHIFT;
1859
1860 mutex_lock(&dev->kvm->lock);
1861
1862 if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
1863 ret = -EINVAL;
1864 goto out;
1865 }
1866
1867 vcpu = kvm_get_vcpu(dev->kvm, cpuid);
1868 vgic = &dev->kvm->arch.vgic;
1869
1870 mmio.len = 4;
1871 mmio.is_write = is_write;
1872 if (is_write)
1873 mmio_data_write(&mmio, ~0, *reg);
1874 switch (attr->group) {
1875 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
1876 mmio.phys_addr = vgic->vgic_dist_base + offset;
1877 ranges = vgic_dist_ranges;
1878 break;
1879 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
1880 mmio.phys_addr = vgic->vgic_cpu_base + offset;
1881 ranges = vgic_cpu_ranges;
1882 break;
1883 default:
1884 BUG();
1885 }
1886 r = find_matching_range(ranges, &mmio, offset);
1887
1888 if (unlikely(!r || !r->handle_mmio)) {
1889 ret = -ENXIO;
1890 goto out;
1891 }
1892
1893
1894 spin_lock(&vgic->lock);
1895
1896 /*
1897 * Ensure that no other VCPU is running by checking the vcpu->cpu
1898 * field. If no other VPCUs are running we can safely access the VGIC
1899 * state, because even if another VPU is run after this point, that
1900 * VCPU will not touch the vgic state, because it will block on
1901 * getting the vgic->lock in kvm_vgic_sync_hwstate().
1902 */
1903 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
1904 if (unlikely(tmp_vcpu->cpu != -1)) {
1905 ret = -EBUSY;
1906 goto out_vgic_unlock;
1907 }
1908 }
1909
Christoffer Dallcbd333a2013-11-15 20:51:31 -08001910 /*
1911 * Move all pending IRQs from the LRs on all VCPUs so the pending
1912 * state can be properly represented in the register state accessible
1913 * through this API.
1914 */
1915 kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm)
1916 vgic_unqueue_irqs(tmp_vcpu);
1917
Christoffer Dallc07a0192013-10-25 21:17:31 +01001918 offset -= r->base;
1919 r->handle_mmio(vcpu, &mmio, offset);
1920
1921 if (!is_write)
1922 *reg = mmio_data_read(&mmio, ~0);
1923
1924 ret = 0;
1925out_vgic_unlock:
1926 spin_unlock(&vgic->lock);
1927out:
1928 mutex_unlock(&dev->kvm->lock);
1929 return ret;
1930}
1931
Christoffer Dall73306722013-10-25 17:29:18 +01001932static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1933{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001934 int r;
1935
1936 switch (attr->group) {
1937 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1938 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1939 u64 addr;
1940 unsigned long type = (unsigned long)attr->attr;
1941
1942 if (copy_from_user(&addr, uaddr, sizeof(addr)))
1943 return -EFAULT;
1944
1945 r = kvm_vgic_addr(dev->kvm, type, &addr, true);
1946 return (r == -ENODEV) ? -ENXIO : r;
1947 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01001948
1949 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
1950 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
1951 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
1952 u32 reg;
1953
1954 if (get_user(reg, uaddr))
1955 return -EFAULT;
1956
1957 return vgic_attr_regs_access(dev, attr, &reg, true);
1958 }
1959
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001960 }
1961
Christoffer Dall73306722013-10-25 17:29:18 +01001962 return -ENXIO;
1963}
1964
1965static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1966{
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001967 int r = -ENXIO;
1968
1969 switch (attr->group) {
1970 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1971 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1972 u64 addr;
1973 unsigned long type = (unsigned long)attr->attr;
1974
1975 r = kvm_vgic_addr(dev->kvm, type, &addr, false);
1976 if (r)
1977 return (r == -ENODEV) ? -ENXIO : r;
1978
1979 if (copy_to_user(uaddr, &addr, sizeof(addr)))
1980 return -EFAULT;
Christoffer Dallc07a0192013-10-25 21:17:31 +01001981 break;
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001982 }
Christoffer Dallc07a0192013-10-25 21:17:31 +01001983
1984 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
1985 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
1986 u32 __user *uaddr = (u32 __user *)(long)attr->addr;
1987 u32 reg = 0;
1988
1989 r = vgic_attr_regs_access(dev, attr, &reg, false);
1990 if (r)
1991 return r;
1992 r = put_user(reg, uaddr);
1993 break;
1994 }
1995
Christoffer Dallce01e4e2013-09-23 14:55:56 -07001996 }
1997
1998 return r;
Christoffer Dall73306722013-10-25 17:29:18 +01001999}
2000
Christoffer Dallc07a0192013-10-25 21:17:31 +01002001static int vgic_has_attr_regs(const struct mmio_range *ranges,
2002 phys_addr_t offset)
2003{
2004 struct kvm_exit_mmio dev_attr_mmio;
2005
2006 dev_attr_mmio.len = 4;
2007 if (find_matching_range(ranges, &dev_attr_mmio, offset))
2008 return 0;
2009 else
2010 return -ENXIO;
2011}
2012
Christoffer Dall73306722013-10-25 17:29:18 +01002013static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2014{
Christoffer Dallc07a0192013-10-25 21:17:31 +01002015 phys_addr_t offset;
2016
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002017 switch (attr->group) {
2018 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2019 switch (attr->attr) {
2020 case KVM_VGIC_V2_ADDR_TYPE_DIST:
2021 case KVM_VGIC_V2_ADDR_TYPE_CPU:
2022 return 0;
2023 }
2024 break;
Christoffer Dallc07a0192013-10-25 21:17:31 +01002025 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
2026 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2027 return vgic_has_attr_regs(vgic_dist_ranges, offset);
2028 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
2029 offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
2030 return vgic_has_attr_regs(vgic_cpu_ranges, offset);
Christoffer Dallce01e4e2013-09-23 14:55:56 -07002031 }
Christoffer Dall73306722013-10-25 17:29:18 +01002032 return -ENXIO;
2033}
2034
2035static void vgic_destroy(struct kvm_device *dev)
2036{
2037 kfree(dev);
2038}
2039
2040static int vgic_create(struct kvm_device *dev, u32 type)
2041{
2042 return kvm_vgic_create(dev->kvm);
2043}
2044
2045struct kvm_device_ops kvm_arm_vgic_v2_ops = {
2046 .name = "kvm-arm-vgic",
2047 .create = vgic_create,
2048 .destroy = vgic_destroy,
2049 .set_attr = vgic_set_attr,
2050 .get_attr = vgic_get_attr,
2051 .has_attr = vgic_has_attr,
2052};