ARM: KVM: VGIC virtual CPU interface management
[deliverable/linux.git] / arch / arm / kvm / 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
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <asm/kvm_emulate.h>
24
b47ef92a
MZ
25/*
26 * How the whole thing works (courtesy of Christoffer Dall):
27 *
28 * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
29 * something is pending
30 * - VGIC pending interrupts are stored on the vgic.irq_state vgic
31 * bitmap (this bitmap is updated by both user land ioctls and guest
32 * mmio ops, and other in-kernel peripherals such as the
33 * arch. timers) and indicate the 'wire' state.
34 * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
35 * recalculated
36 * - To calculate the oracle, we need info for each cpu from
37 * compute_pending_for_cpu, which considers:
38 * - PPI: dist->irq_state & dist->irq_enable
39 * - SPI: dist->irq_state & dist->irq_enable & dist->irq_spi_target
40 * - irq_spi_target is a 'formatted' version of the GICD_ICFGR
41 * registers, stored on each vcpu. We only keep one bit of
42 * information per interrupt, making sure that only one vcpu can
43 * accept the interrupt.
44 * - The same is true when injecting an interrupt, except that we only
45 * consider a single interrupt at a time. The irq_spi_cpu array
46 * contains the target CPU for each SPI.
47 *
48 * The handling of level interrupts adds some extra complexity. We
49 * need to track when the interrupt has been EOIed, so we can sample
50 * the 'line' again. This is achieved as such:
51 *
52 * - When a level interrupt is moved onto a vcpu, the corresponding
53 * bit in irq_active is set. As long as this bit is set, the line
54 * will be ignored for further interrupts. The interrupt is injected
55 * into the vcpu with the GICH_LR_EOI bit set (generate a
56 * maintenance interrupt on EOI).
57 * - When the interrupt is EOIed, the maintenance interrupt fires,
58 * and clears the corresponding bit in irq_active. This allow the
59 * interrupt line to be sampled again.
60 */
61
330690cd
CD
62#define VGIC_ADDR_UNDEF (-1)
63#define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
64
1a89dd91
MZ
65#define ACCESS_READ_VALUE (1 << 0)
66#define ACCESS_READ_RAZ (0 << 0)
67#define ACCESS_READ_MASK(x) ((x) & (1 << 0))
68#define ACCESS_WRITE_IGNORED (0 << 1)
69#define ACCESS_WRITE_SETBIT (1 << 1)
70#define ACCESS_WRITE_CLEARBIT (2 << 1)
71#define ACCESS_WRITE_VALUE (3 << 1)
72#define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
73
b47ef92a
MZ
74static void vgic_update_state(struct kvm *kvm);
75static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
76
77static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
78 int cpuid, u32 offset)
79{
80 offset >>= 2;
81 if (!offset)
82 return x->percpu[cpuid].reg;
83 else
84 return x->shared.reg + offset - 1;
85}
86
87static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
88 int cpuid, int irq)
89{
90 if (irq < VGIC_NR_PRIVATE_IRQS)
91 return test_bit(irq, x->percpu[cpuid].reg_ul);
92
93 return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared.reg_ul);
94}
95
96static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
97 int irq, int val)
98{
99 unsigned long *reg;
100
101 if (irq < VGIC_NR_PRIVATE_IRQS) {
102 reg = x->percpu[cpuid].reg_ul;
103 } else {
104 reg = x->shared.reg_ul;
105 irq -= VGIC_NR_PRIVATE_IRQS;
106 }
107
108 if (val)
109 set_bit(irq, reg);
110 else
111 clear_bit(irq, reg);
112}
113
114static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
115{
116 if (unlikely(cpuid >= VGIC_MAX_CPUS))
117 return NULL;
118 return x->percpu[cpuid].reg_ul;
119}
120
121static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
122{
123 return x->shared.reg_ul;
124}
125
126static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
127{
128 offset >>= 2;
129 BUG_ON(offset > (VGIC_NR_IRQS / 4));
130 if (offset < 4)
131 return x->percpu[cpuid] + offset;
132 else
133 return x->shared + offset - 8;
134}
135
136#define VGIC_CFG_LEVEL 0
137#define VGIC_CFG_EDGE 1
138
139static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
140{
141 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
142 int irq_val;
143
144 irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
145 return irq_val == VGIC_CFG_EDGE;
146}
147
148static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
149{
150 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
151
152 return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
153}
154
9d949dce
MZ
155static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
156{
157 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
158
159 return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
160}
161
162static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
163{
164 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
165
166 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
167}
168
169static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
170{
171 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
172
173 vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
174}
175
176static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
177{
178 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
179
180 return vgic_bitmap_get_irq_val(&dist->irq_state, vcpu->vcpu_id, irq);
181}
182
b47ef92a
MZ
183static void vgic_dist_irq_set(struct kvm_vcpu *vcpu, int irq)
184{
185 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
186
187 vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 1);
188}
189
190static void vgic_dist_irq_clear(struct kvm_vcpu *vcpu, int irq)
191{
192 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
193
194 vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 0);
195}
196
197static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
198{
199 if (irq < VGIC_NR_PRIVATE_IRQS)
200 set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
201 else
202 set_bit(irq - VGIC_NR_PRIVATE_IRQS,
203 vcpu->arch.vgic_cpu.pending_shared);
204}
205
206static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
207{
208 if (irq < VGIC_NR_PRIVATE_IRQS)
209 clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
210 else
211 clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
212 vcpu->arch.vgic_cpu.pending_shared);
213}
214
1a89dd91
MZ
215static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
216{
217 return *((u32 *)mmio->data) & mask;
218}
219
220static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
221{
222 *((u32 *)mmio->data) = value & mask;
223}
224
225/**
226 * vgic_reg_access - access vgic register
227 * @mmio: pointer to the data describing the mmio access
228 * @reg: pointer to the virtual backing of vgic distributor data
229 * @offset: least significant 2 bits used for word offset
230 * @mode: ACCESS_ mode (see defines above)
231 *
232 * Helper to make vgic register access easier using one of the access
233 * modes defined for vgic register access
234 * (read,raz,write-ignored,setbit,clearbit,write)
235 */
236static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
237 phys_addr_t offset, int mode)
238{
239 int word_offset = (offset & 3) * 8;
240 u32 mask = (1UL << (mmio->len * 8)) - 1;
241 u32 regval;
242
243 /*
244 * Any alignment fault should have been delivered to the guest
245 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
246 */
247
248 if (reg) {
249 regval = *reg;
250 } else {
251 BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
252 regval = 0;
253 }
254
255 if (mmio->is_write) {
256 u32 data = mmio_data_read(mmio, mask) << word_offset;
257 switch (ACCESS_WRITE_MASK(mode)) {
258 case ACCESS_WRITE_IGNORED:
259 return;
260
261 case ACCESS_WRITE_SETBIT:
262 regval |= data;
263 break;
264
265 case ACCESS_WRITE_CLEARBIT:
266 regval &= ~data;
267 break;
268
269 case ACCESS_WRITE_VALUE:
270 regval = (regval & ~(mask << word_offset)) | data;
271 break;
272 }
273 *reg = regval;
274 } else {
275 switch (ACCESS_READ_MASK(mode)) {
276 case ACCESS_READ_RAZ:
277 regval = 0;
278 /* fall through */
279
280 case ACCESS_READ_VALUE:
281 mmio_data_write(mmio, mask, regval >> word_offset);
282 }
283 }
284}
285
b47ef92a
MZ
286static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
287 struct kvm_exit_mmio *mmio, phys_addr_t offset)
288{
289 u32 reg;
290 u32 word_offset = offset & 3;
291
292 switch (offset & ~3) {
293 case 0: /* CTLR */
294 reg = vcpu->kvm->arch.vgic.enabled;
295 vgic_reg_access(mmio, &reg, word_offset,
296 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
297 if (mmio->is_write) {
298 vcpu->kvm->arch.vgic.enabled = reg & 1;
299 vgic_update_state(vcpu->kvm);
300 return true;
301 }
302 break;
303
304 case 4: /* TYPER */
305 reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
306 reg |= (VGIC_NR_IRQS >> 5) - 1;
307 vgic_reg_access(mmio, &reg, word_offset,
308 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
309 break;
310
311 case 8: /* IIDR */
312 reg = 0x4B00043B;
313 vgic_reg_access(mmio, &reg, word_offset,
314 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
315 break;
316 }
317
318 return false;
319}
320
321static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
322 struct kvm_exit_mmio *mmio, phys_addr_t offset)
323{
324 vgic_reg_access(mmio, NULL, offset,
325 ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
326 return false;
327}
328
329static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
330 struct kvm_exit_mmio *mmio,
331 phys_addr_t offset)
332{
333 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
334 vcpu->vcpu_id, offset);
335 vgic_reg_access(mmio, reg, offset,
336 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
337 if (mmio->is_write) {
338 vgic_update_state(vcpu->kvm);
339 return true;
340 }
341
342 return false;
343}
344
345static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
346 struct kvm_exit_mmio *mmio,
347 phys_addr_t offset)
348{
349 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
350 vcpu->vcpu_id, offset);
351 vgic_reg_access(mmio, reg, offset,
352 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
353 if (mmio->is_write) {
354 if (offset < 4) /* Force SGI enabled */
355 *reg |= 0xffff;
356 vgic_update_state(vcpu->kvm);
357 return true;
358 }
359
360 return false;
361}
362
363static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
364 struct kvm_exit_mmio *mmio,
365 phys_addr_t offset)
366{
367 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
368 vcpu->vcpu_id, offset);
369 vgic_reg_access(mmio, reg, offset,
370 ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
371 if (mmio->is_write) {
372 vgic_update_state(vcpu->kvm);
373 return true;
374 }
375
376 return false;
377}
378
379static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
380 struct kvm_exit_mmio *mmio,
381 phys_addr_t offset)
382{
383 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
384 vcpu->vcpu_id, offset);
385 vgic_reg_access(mmio, reg, offset,
386 ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
387 if (mmio->is_write) {
388 vgic_update_state(vcpu->kvm);
389 return true;
390 }
391
392 return false;
393}
394
395static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
396 struct kvm_exit_mmio *mmio,
397 phys_addr_t offset)
398{
399 u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
400 vcpu->vcpu_id, offset);
401 vgic_reg_access(mmio, reg, offset,
402 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
403 return false;
404}
405
406#define GICD_ITARGETSR_SIZE 32
407#define GICD_CPUTARGETS_BITS 8
408#define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
409static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
410{
411 struct vgic_dist *dist = &kvm->arch.vgic;
412 struct kvm_vcpu *vcpu;
413 int i, c;
414 unsigned long *bmap;
415 u32 val = 0;
416
417 irq -= VGIC_NR_PRIVATE_IRQS;
418
419 kvm_for_each_vcpu(c, vcpu, kvm) {
420 bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
421 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
422 if (test_bit(irq + i, bmap))
423 val |= 1 << (c + i * 8);
424 }
425
426 return val;
427}
428
429static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
430{
431 struct vgic_dist *dist = &kvm->arch.vgic;
432 struct kvm_vcpu *vcpu;
433 int i, c;
434 unsigned long *bmap;
435 u32 target;
436
437 irq -= VGIC_NR_PRIVATE_IRQS;
438
439 /*
440 * Pick the LSB in each byte. This ensures we target exactly
441 * one vcpu per IRQ. If the byte is null, assume we target
442 * CPU0.
443 */
444 for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
445 int shift = i * GICD_CPUTARGETS_BITS;
446 target = ffs((val >> shift) & 0xffU);
447 target = target ? (target - 1) : 0;
448 dist->irq_spi_cpu[irq + i] = target;
449 kvm_for_each_vcpu(c, vcpu, kvm) {
450 bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
451 if (c == target)
452 set_bit(irq + i, bmap);
453 else
454 clear_bit(irq + i, bmap);
455 }
456 }
457}
458
459static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
460 struct kvm_exit_mmio *mmio,
461 phys_addr_t offset)
462{
463 u32 reg;
464
465 /* We treat the banked interrupts targets as read-only */
466 if (offset < 32) {
467 u32 roreg = 1 << vcpu->vcpu_id;
468 roreg |= roreg << 8;
469 roreg |= roreg << 16;
470
471 vgic_reg_access(mmio, &roreg, offset,
472 ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
473 return false;
474 }
475
476 reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
477 vgic_reg_access(mmio, &reg, offset,
478 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
479 if (mmio->is_write) {
480 vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
481 vgic_update_state(vcpu->kvm);
482 return true;
483 }
484
485 return false;
486}
487
488static u32 vgic_cfg_expand(u16 val)
489{
490 u32 res = 0;
491 int i;
492
493 /*
494 * Turn a 16bit value like abcd...mnop into a 32bit word
495 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
496 */
497 for (i = 0; i < 16; i++)
498 res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
499
500 return res;
501}
502
503static u16 vgic_cfg_compress(u32 val)
504{
505 u16 res = 0;
506 int i;
507
508 /*
509 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
510 * abcd...mnop which is what we really care about.
511 */
512 for (i = 0; i < 16; i++)
513 res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
514
515 return res;
516}
517
518/*
519 * The distributor uses 2 bits per IRQ for the CFG register, but the
520 * LSB is always 0. As such, we only keep the upper bit, and use the
521 * two above functions to compress/expand the bits
522 */
523static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
524 struct kvm_exit_mmio *mmio, phys_addr_t offset)
525{
526 u32 val;
527 u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
528 vcpu->vcpu_id, offset >> 1);
529 if (offset & 2)
530 val = *reg >> 16;
531 else
532 val = *reg & 0xffff;
533
534 val = vgic_cfg_expand(val);
535 vgic_reg_access(mmio, &val, offset,
536 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
537 if (mmio->is_write) {
538 if (offset < 4) {
539 *reg = ~0U; /* Force PPIs/SGIs to 1 */
540 return false;
541 }
542
543 val = vgic_cfg_compress(val);
544 if (offset & 2) {
545 *reg &= 0xffff;
546 *reg |= val << 16;
547 } else {
548 *reg &= 0xffff << 16;
549 *reg |= val;
550 }
551 }
552
553 return false;
554}
555
556static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
557 struct kvm_exit_mmio *mmio, phys_addr_t offset)
558{
559 u32 reg;
560 vgic_reg_access(mmio, &reg, offset,
561 ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
562 if (mmio->is_write) {
563 vgic_dispatch_sgi(vcpu, reg);
564 vgic_update_state(vcpu->kvm);
565 return true;
566 }
567
568 return false;
569}
570
1a89dd91
MZ
571/*
572 * I would have liked to use the kvm_bus_io_*() API instead, but it
573 * cannot cope with banked registers (only the VM pointer is passed
574 * around, and we need the vcpu). One of these days, someone please
575 * fix it!
576 */
577struct mmio_range {
578 phys_addr_t base;
579 unsigned long len;
580 bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
581 phys_addr_t offset);
582};
583
584static const struct mmio_range vgic_ranges[] = {
b47ef92a
MZ
585 {
586 .base = GIC_DIST_CTRL,
587 .len = 12,
588 .handle_mmio = handle_mmio_misc,
589 },
590 {
591 .base = GIC_DIST_IGROUP,
592 .len = VGIC_NR_IRQS / 8,
593 .handle_mmio = handle_mmio_raz_wi,
594 },
595 {
596 .base = GIC_DIST_ENABLE_SET,
597 .len = VGIC_NR_IRQS / 8,
598 .handle_mmio = handle_mmio_set_enable_reg,
599 },
600 {
601 .base = GIC_DIST_ENABLE_CLEAR,
602 .len = VGIC_NR_IRQS / 8,
603 .handle_mmio = handle_mmio_clear_enable_reg,
604 },
605 {
606 .base = GIC_DIST_PENDING_SET,
607 .len = VGIC_NR_IRQS / 8,
608 .handle_mmio = handle_mmio_set_pending_reg,
609 },
610 {
611 .base = GIC_DIST_PENDING_CLEAR,
612 .len = VGIC_NR_IRQS / 8,
613 .handle_mmio = handle_mmio_clear_pending_reg,
614 },
615 {
616 .base = GIC_DIST_ACTIVE_SET,
617 .len = VGIC_NR_IRQS / 8,
618 .handle_mmio = handle_mmio_raz_wi,
619 },
620 {
621 .base = GIC_DIST_ACTIVE_CLEAR,
622 .len = VGIC_NR_IRQS / 8,
623 .handle_mmio = handle_mmio_raz_wi,
624 },
625 {
626 .base = GIC_DIST_PRI,
627 .len = VGIC_NR_IRQS,
628 .handle_mmio = handle_mmio_priority_reg,
629 },
630 {
631 .base = GIC_DIST_TARGET,
632 .len = VGIC_NR_IRQS,
633 .handle_mmio = handle_mmio_target_reg,
634 },
635 {
636 .base = GIC_DIST_CONFIG,
637 .len = VGIC_NR_IRQS / 4,
638 .handle_mmio = handle_mmio_cfg_reg,
639 },
640 {
641 .base = GIC_DIST_SOFTINT,
642 .len = 4,
643 .handle_mmio = handle_mmio_sgi_reg,
644 },
1a89dd91
MZ
645 {}
646};
647
648static const
649struct mmio_range *find_matching_range(const struct mmio_range *ranges,
650 struct kvm_exit_mmio *mmio,
651 phys_addr_t base)
652{
653 const struct mmio_range *r = ranges;
654 phys_addr_t addr = mmio->phys_addr - base;
655
656 while (r->len) {
657 if (addr >= r->base &&
658 (addr + mmio->len) <= (r->base + r->len))
659 return r;
660 r++;
661 }
662
663 return NULL;
664}
665
666/**
667 * vgic_handle_mmio - handle an in-kernel MMIO access
668 * @vcpu: pointer to the vcpu performing the access
669 * @run: pointer to the kvm_run structure
670 * @mmio: pointer to the data describing the access
671 *
672 * returns true if the MMIO access has been performed in kernel space,
673 * and false if it needs to be emulated in user space.
674 */
675bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
676 struct kvm_exit_mmio *mmio)
677{
b47ef92a
MZ
678 const struct mmio_range *range;
679 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
680 unsigned long base = dist->vgic_dist_base;
681 bool updated_state;
682 unsigned long offset;
683
684 if (!irqchip_in_kernel(vcpu->kvm) ||
685 mmio->phys_addr < base ||
686 (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
687 return false;
688
689 /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
690 if (mmio->len > 4) {
691 kvm_inject_dabt(vcpu, mmio->phys_addr);
692 return true;
693 }
694
695 range = find_matching_range(vgic_ranges, mmio, base);
696 if (unlikely(!range || !range->handle_mmio)) {
697 pr_warn("Unhandled access %d %08llx %d\n",
698 mmio->is_write, mmio->phys_addr, mmio->len);
699 return false;
700 }
701
702 spin_lock(&vcpu->kvm->arch.vgic.lock);
703 offset = mmio->phys_addr - range->base - base;
704 updated_state = range->handle_mmio(vcpu, mmio, offset);
705 spin_unlock(&vcpu->kvm->arch.vgic.lock);
706 kvm_prepare_mmio(run, mmio);
707 kvm_handle_mmio_return(vcpu, run);
708
709 return true;
710}
711
712static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
713{
714 struct kvm *kvm = vcpu->kvm;
715 struct vgic_dist *dist = &kvm->arch.vgic;
716 int nrcpus = atomic_read(&kvm->online_vcpus);
717 u8 target_cpus;
718 int sgi, mode, c, vcpu_id;
719
720 vcpu_id = vcpu->vcpu_id;
721
722 sgi = reg & 0xf;
723 target_cpus = (reg >> 16) & 0xff;
724 mode = (reg >> 24) & 3;
725
726 switch (mode) {
727 case 0:
728 if (!target_cpus)
729 return;
730
731 case 1:
732 target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
733 break;
734
735 case 2:
736 target_cpus = 1 << vcpu_id;
737 break;
738 }
739
740 kvm_for_each_vcpu(c, vcpu, kvm) {
741 if (target_cpus & 1) {
742 /* Flag the SGI as pending */
743 vgic_dist_irq_set(vcpu, sgi);
744 dist->irq_sgi_sources[c][sgi] |= 1 << vcpu_id;
745 kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
746 }
747
748 target_cpus >>= 1;
749 }
750}
751
752static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
753{
9d949dce
MZ
754 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
755 unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
756 unsigned long pending_private, pending_shared;
757 int vcpu_id;
758
759 vcpu_id = vcpu->vcpu_id;
760 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
761 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
762
763 pending = vgic_bitmap_get_cpu_map(&dist->irq_state, vcpu_id);
764 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
765 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
766
767 pending = vgic_bitmap_get_shared_map(&dist->irq_state);
768 enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
769 bitmap_and(pend_shared, pending, enabled, VGIC_NR_SHARED_IRQS);
770 bitmap_and(pend_shared, pend_shared,
771 vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
772 VGIC_NR_SHARED_IRQS);
773
774 pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
775 pending_shared = find_first_bit(pend_shared, VGIC_NR_SHARED_IRQS);
776 return (pending_private < VGIC_NR_PRIVATE_IRQS ||
777 pending_shared < VGIC_NR_SHARED_IRQS);
b47ef92a
MZ
778}
779
780/*
781 * Update the interrupt state and determine which CPUs have pending
782 * interrupts. Must be called with distributor lock held.
783 */
784static void vgic_update_state(struct kvm *kvm)
785{
786 struct vgic_dist *dist = &kvm->arch.vgic;
787 struct kvm_vcpu *vcpu;
788 int c;
789
790 if (!dist->enabled) {
791 set_bit(0, &dist->irq_pending_on_cpu);
792 return;
793 }
794
795 kvm_for_each_vcpu(c, vcpu, kvm) {
796 if (compute_pending_for_cpu(vcpu)) {
797 pr_debug("CPU%d has pending interrupts\n", c);
798 set_bit(c, &dist->irq_pending_on_cpu);
799 }
800 }
1a89dd91 801}
330690cd 802
9d949dce
MZ
803#define LR_CPUID(lr) \
804 (((lr) & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT)
805#define MK_LR_PEND(src, irq) \
806 (GICH_LR_PENDING_BIT | ((src) << GICH_LR_PHYSID_CPUID_SHIFT) | (irq))
807/*
808 * Queue an interrupt to a CPU virtual interface. Return true on success,
809 * or false if it wasn't possible to queue it.
810 */
811static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
812{
813 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
814 int lr;
815
816 /* Sanitize the input... */
817 BUG_ON(sgi_source_id & ~7);
818 BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
819 BUG_ON(irq >= VGIC_NR_IRQS);
820
821 kvm_debug("Queue IRQ%d\n", irq);
822
823 lr = vgic_cpu->vgic_irq_lr_map[irq];
824
825 /* Do we have an active interrupt for the same CPUID? */
826 if (lr != LR_EMPTY &&
827 (LR_CPUID(vgic_cpu->vgic_lr[lr]) == sgi_source_id)) {
828 kvm_debug("LR%d piggyback for IRQ%d %x\n",
829 lr, irq, vgic_cpu->vgic_lr[lr]);
830 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
831 vgic_cpu->vgic_lr[lr] |= GICH_LR_PENDING_BIT;
832
833 goto out;
834 }
835
836 /* Try to use another LR for this interrupt */
837 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
838 vgic_cpu->nr_lr);
839 if (lr >= vgic_cpu->nr_lr)
840 return false;
841
842 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
843 vgic_cpu->vgic_lr[lr] = MK_LR_PEND(sgi_source_id, irq);
844 vgic_cpu->vgic_irq_lr_map[irq] = lr;
845 set_bit(lr, vgic_cpu->lr_used);
846
847out:
848 if (!vgic_irq_is_edge(vcpu, irq))
849 vgic_cpu->vgic_lr[lr] |= GICH_LR_EOI;
850
851 return true;
852}
853
854static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
855{
856 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
857 unsigned long sources;
858 int vcpu_id = vcpu->vcpu_id;
859 int c;
860
861 sources = dist->irq_sgi_sources[vcpu_id][irq];
862
863 for_each_set_bit(c, &sources, VGIC_MAX_CPUS) {
864 if (vgic_queue_irq(vcpu, c, irq))
865 clear_bit(c, &sources);
866 }
867
868 dist->irq_sgi_sources[vcpu_id][irq] = sources;
869
870 /*
871 * If the sources bitmap has been cleared it means that we
872 * could queue all the SGIs onto link registers (see the
873 * clear_bit above), and therefore we are done with them in
874 * our emulated gic and can get rid of them.
875 */
876 if (!sources) {
877 vgic_dist_irq_clear(vcpu, irq);
878 vgic_cpu_irq_clear(vcpu, irq);
879 return true;
880 }
881
882 return false;
883}
884
885static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
886{
887 if (vgic_irq_is_active(vcpu, irq))
888 return true; /* level interrupt, already queued */
889
890 if (vgic_queue_irq(vcpu, 0, irq)) {
891 if (vgic_irq_is_edge(vcpu, irq)) {
892 vgic_dist_irq_clear(vcpu, irq);
893 vgic_cpu_irq_clear(vcpu, irq);
894 } else {
895 vgic_irq_set_active(vcpu, irq);
896 }
897
898 return true;
899 }
900
901 return false;
902}
903
904/*
905 * Fill the list registers with pending interrupts before running the
906 * guest.
907 */
908static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
909{
910 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
911 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
912 int i, vcpu_id;
913 int overflow = 0;
914
915 vcpu_id = vcpu->vcpu_id;
916
917 /*
918 * We may not have any pending interrupt, or the interrupts
919 * may have been serviced from another vcpu. In all cases,
920 * move along.
921 */
922 if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
923 pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
924 goto epilog;
925 }
926
927 /* SGIs */
928 for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
929 if (!vgic_queue_sgi(vcpu, i))
930 overflow = 1;
931 }
932
933 /* PPIs */
934 for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
935 if (!vgic_queue_hwirq(vcpu, i))
936 overflow = 1;
937 }
938
939 /* SPIs */
940 for_each_set_bit(i, vgic_cpu->pending_shared, VGIC_NR_SHARED_IRQS) {
941 if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
942 overflow = 1;
943 }
944
945epilog:
946 if (overflow) {
947 vgic_cpu->vgic_hcr |= GICH_HCR_UIE;
948 } else {
949 vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
950 /*
951 * We're about to run this VCPU, and we've consumed
952 * everything the distributor had in store for
953 * us. Claim we don't have anything pending. We'll
954 * adjust that if needed while exiting.
955 */
956 clear_bit(vcpu_id, &dist->irq_pending_on_cpu);
957 }
958}
959
960static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
961{
962 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
963 bool level_pending = false;
964
965 kvm_debug("MISR = %08x\n", vgic_cpu->vgic_misr);
966
967 /*
968 * We do not need to take the distributor lock here, since the only
969 * action we perform is clearing the irq_active_bit for an EOIed
970 * level interrupt. There is a potential race with
971 * the queuing of an interrupt in __kvm_vgic_flush_hwstate(), where we
972 * check if the interrupt is already active. Two possibilities:
973 *
974 * - The queuing is occurring on the same vcpu: cannot happen,
975 * as we're already in the context of this vcpu, and
976 * executing the handler
977 * - The interrupt has been migrated to another vcpu, and we
978 * ignore this interrupt for this run. Big deal. It is still
979 * pending though, and will get considered when this vcpu
980 * exits.
981 */
982 if (vgic_cpu->vgic_misr & GICH_MISR_EOI) {
983 /*
984 * Some level interrupts have been EOIed. Clear their
985 * active bit.
986 */
987 int lr, irq;
988
989 for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_eisr,
990 vgic_cpu->nr_lr) {
991 irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
992
993 vgic_irq_clear_active(vcpu, irq);
994 vgic_cpu->vgic_lr[lr] &= ~GICH_LR_EOI;
995
996 /* Any additional pending interrupt? */
997 if (vgic_dist_irq_is_pending(vcpu, irq)) {
998 vgic_cpu_irq_set(vcpu, irq);
999 level_pending = true;
1000 } else {
1001 vgic_cpu_irq_clear(vcpu, irq);
1002 }
1003 }
1004 }
1005
1006 if (vgic_cpu->vgic_misr & GICH_MISR_U)
1007 vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
1008
1009 return level_pending;
1010}
1011
1012/*
1013 * Sync back the VGIC state after a guest run. We do not really touch
1014 * the distributor here (the irq_pending_on_cpu bit is safe to set),
1015 * so there is no need for taking its lock.
1016 */
1017static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1018{
1019 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1020 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1021 int lr, pending;
1022 bool level_pending;
1023
1024 level_pending = vgic_process_maintenance(vcpu);
1025
1026 /* Clear mappings for empty LRs */
1027 for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_elrsr,
1028 vgic_cpu->nr_lr) {
1029 int irq;
1030
1031 if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
1032 continue;
1033
1034 irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
1035
1036 BUG_ON(irq >= VGIC_NR_IRQS);
1037 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1038 }
1039
1040 /* Check if we still have something up our sleeve... */
1041 pending = find_first_zero_bit((unsigned long *)vgic_cpu->vgic_elrsr,
1042 vgic_cpu->nr_lr);
1043 if (level_pending || pending < vgic_cpu->nr_lr)
1044 set_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
1045}
1046
1047void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1048{
1049 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1050
1051 if (!irqchip_in_kernel(vcpu->kvm))
1052 return;
1053
1054 spin_lock(&dist->lock);
1055 __kvm_vgic_flush_hwstate(vcpu);
1056 spin_unlock(&dist->lock);
1057}
1058
1059void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1060{
1061 if (!irqchip_in_kernel(vcpu->kvm))
1062 return;
1063
1064 __kvm_vgic_sync_hwstate(vcpu);
1065}
1066
1067int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
1068{
1069 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1070
1071 if (!irqchip_in_kernel(vcpu->kvm))
1072 return 0;
1073
1074 return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
1075}
1076
330690cd
CD
1077static bool vgic_ioaddr_overlap(struct kvm *kvm)
1078{
1079 phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
1080 phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
1081
1082 if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
1083 return 0;
1084 if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
1085 (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
1086 return -EBUSY;
1087 return 0;
1088}
1089
1090static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
1091 phys_addr_t addr, phys_addr_t size)
1092{
1093 int ret;
1094
1095 if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
1096 return -EEXIST;
1097 if (addr + size < addr)
1098 return -EINVAL;
1099
1100 ret = vgic_ioaddr_overlap(kvm);
1101 if (ret)
1102 return ret;
1103 *ioaddr = addr;
1104 return ret;
1105}
1106
1107int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
1108{
1109 int r = 0;
1110 struct vgic_dist *vgic = &kvm->arch.vgic;
1111
1112 if (addr & ~KVM_PHYS_MASK)
1113 return -E2BIG;
1114
1115 if (addr & ~PAGE_MASK)
1116 return -EINVAL;
1117
1118 mutex_lock(&kvm->lock);
1119 switch (type) {
1120 case KVM_VGIC_V2_ADDR_TYPE_DIST:
1121 r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
1122 addr, KVM_VGIC_V2_DIST_SIZE);
1123 break;
1124 case KVM_VGIC_V2_ADDR_TYPE_CPU:
1125 r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
1126 addr, KVM_VGIC_V2_CPU_SIZE);
1127 break;
1128 default:
1129 r = -ENODEV;
1130 }
1131
1132 mutex_unlock(&kvm->lock);
1133 return r;
1134}
This page took 0.117956 seconds and 5 git commands to generate.