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