2 * Copyright (C) 2012 ARM Ltd.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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.
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.
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
19 #include <linux/cpu.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_host.h>
22 #include <linux/interrupt.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
30 #include <asm/kvm_emulate.h>
31 #include <asm/kvm_arm.h>
32 #include <asm/kvm_mmu.h>
33 #include <trace/events/kvm.h>
35 #include <kvm/iodev.h>
38 * How the whole thing works (courtesy of Christoffer Dall):
40 * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
41 * something is pending on the CPU interface.
42 * - Interrupts that are pending on the distributor are stored on the
43 * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
44 * ioctls and guest mmio ops, and other in-kernel peripherals such as the
46 * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
48 * - To calculate the oracle, we need info for each cpu from
49 * compute_pending_for_cpu, which considers:
50 * - PPI: dist->irq_pending & dist->irq_enable
51 * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
52 * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
53 * registers, stored on each vcpu. We only keep one bit of
54 * information per interrupt, making sure that only one vcpu can
55 * accept the interrupt.
56 * - If any of the above state changes, we must recalculate the oracle.
57 * - The same is true when injecting an interrupt, except that we only
58 * consider a single interrupt at a time. The irq_spi_cpu array
59 * contains the target CPU for each SPI.
61 * The handling of level interrupts adds some extra complexity. We
62 * need to track when the interrupt has been EOIed, so we can sample
63 * the 'line' again. This is achieved as such:
65 * - When a level interrupt is moved onto a vcpu, the corresponding
66 * bit in irq_queued is set. As long as this bit is set, the line
67 * will be ignored for further interrupts. The interrupt is injected
68 * into the vcpu with the GICH_LR_EOI bit set (generate a
69 * maintenance interrupt on EOI).
70 * - When the interrupt is EOIed, the maintenance interrupt fires,
71 * and clears the corresponding bit in irq_queued. This allows the
72 * interrupt line to be sampled again.
73 * - Note that level-triggered interrupts can also be set to pending from
74 * writes to GICD_ISPENDRn and lowering the external input line does not
75 * cause the interrupt to become inactive in such a situation.
76 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
77 * inactive as long as the external input line is held high.
80 * Initialization rules: there are multiple stages to the vgic
81 * initialization, both for the distributor and the CPU interfaces.
85 * - kvm_vgic_early_init(): initialization of static data that doesn't
86 * depend on any sizing information or emulation type. No allocation
89 * - vgic_init(): allocation and initialization of the generic data
90 * structures that depend on sizing information (number of CPUs,
91 * number of interrupts). Also initializes the vcpu specific data
92 * structures. Can be executed lazily for GICv2.
93 * [to be renamed to kvm_vgic_init??]
97 * - kvm_vgic_cpu_early_init(): initialization of static data that
98 * doesn't depend on any sizing information or emulation type. No
99 * allocation is allowed there.
104 static void vgic_retire_disabled_irqs(struct kvm_vcpu
*vcpu
);
105 static void vgic_retire_lr(int lr_nr
, int irq
, struct kvm_vcpu
*vcpu
);
106 static struct vgic_lr
vgic_get_lr(const struct kvm_vcpu
*vcpu
, int lr
);
107 static void vgic_set_lr(struct kvm_vcpu
*vcpu
, int lr
, struct vgic_lr lr_desc
);
108 static struct irq_phys_map
*vgic_irq_map_search(struct kvm_vcpu
*vcpu
,
111 static const struct vgic_ops
*vgic_ops
;
112 static const struct vgic_params
*vgic
;
114 static void add_sgi_source(struct kvm_vcpu
*vcpu
, int irq
, int source
)
116 vcpu
->kvm
->arch
.vgic
.vm_ops
.add_sgi_source(vcpu
, irq
, source
);
119 static bool queue_sgi(struct kvm_vcpu
*vcpu
, int irq
)
121 return vcpu
->kvm
->arch
.vgic
.vm_ops
.queue_sgi(vcpu
, irq
);
124 int kvm_vgic_map_resources(struct kvm
*kvm
)
126 return kvm
->arch
.vgic
.vm_ops
.map_resources(kvm
, vgic
);
130 * struct vgic_bitmap contains a bitmap made of unsigned longs, but
131 * extracts u32s out of them.
133 * This does not work on 64-bit BE systems, because the bitmap access
134 * will store two consecutive 32-bit words with the higher-addressed
135 * register's bits at the lower index and the lower-addressed register's
136 * bits at the higher index.
138 * Therefore, swizzle the register index when accessing the 32-bit word
139 * registers to access the right register's value.
141 #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
142 #define REG_OFFSET_SWIZZLE 1
144 #define REG_OFFSET_SWIZZLE 0
147 static int vgic_init_bitmap(struct vgic_bitmap
*b
, int nr_cpus
, int nr_irqs
)
151 nr_longs
= nr_cpus
+ BITS_TO_LONGS(nr_irqs
- VGIC_NR_PRIVATE_IRQS
);
153 b
->private = kzalloc(sizeof(unsigned long) * nr_longs
, GFP_KERNEL
);
157 b
->shared
= b
->private + nr_cpus
;
162 static void vgic_free_bitmap(struct vgic_bitmap
*b
)
170 * Call this function to convert a u64 value to an unsigned long * bitmask
171 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
173 * Warning: Calling this function may modify *val.
175 static unsigned long *u64_to_bitmask(u64
*val
)
177 #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
178 *val
= (*val
>> 32) | (*val
<< 32);
180 return (unsigned long *)val
;
183 u32
*vgic_bitmap_get_reg(struct vgic_bitmap
*x
, int cpuid
, u32 offset
)
187 return (u32
*)(x
->private + cpuid
) + REG_OFFSET_SWIZZLE
;
189 return (u32
*)(x
->shared
) + ((offset
- 1) ^ REG_OFFSET_SWIZZLE
);
192 static int vgic_bitmap_get_irq_val(struct vgic_bitmap
*x
,
195 if (irq
< VGIC_NR_PRIVATE_IRQS
)
196 return test_bit(irq
, x
->private + cpuid
);
198 return test_bit(irq
- VGIC_NR_PRIVATE_IRQS
, x
->shared
);
201 void vgic_bitmap_set_irq_val(struct vgic_bitmap
*x
, int cpuid
,
206 if (irq
< VGIC_NR_PRIVATE_IRQS
) {
207 reg
= x
->private + cpuid
;
210 irq
-= VGIC_NR_PRIVATE_IRQS
;
219 static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap
*x
, int cpuid
)
221 return x
->private + cpuid
;
224 unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap
*x
)
229 static int vgic_init_bytemap(struct vgic_bytemap
*x
, int nr_cpus
, int nr_irqs
)
233 size
= nr_cpus
* VGIC_NR_PRIVATE_IRQS
;
234 size
+= nr_irqs
- VGIC_NR_PRIVATE_IRQS
;
236 x
->private = kzalloc(size
, GFP_KERNEL
);
240 x
->shared
= x
->private + nr_cpus
* VGIC_NR_PRIVATE_IRQS
/ sizeof(u32
);
244 static void vgic_free_bytemap(struct vgic_bytemap
*b
)
251 u32
*vgic_bytemap_get_reg(struct vgic_bytemap
*x
, int cpuid
, u32 offset
)
255 if (offset
< VGIC_NR_PRIVATE_IRQS
) {
257 offset
+= cpuid
* VGIC_NR_PRIVATE_IRQS
;
260 offset
-= VGIC_NR_PRIVATE_IRQS
;
263 return reg
+ (offset
/ sizeof(u32
));
266 #define VGIC_CFG_LEVEL 0
267 #define VGIC_CFG_EDGE 1
269 static bool vgic_irq_is_edge(struct kvm_vcpu
*vcpu
, int irq
)
271 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
274 irq_val
= vgic_bitmap_get_irq_val(&dist
->irq_cfg
, vcpu
->vcpu_id
, irq
);
275 return irq_val
== VGIC_CFG_EDGE
;
278 static int vgic_irq_is_enabled(struct kvm_vcpu
*vcpu
, int irq
)
280 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
282 return vgic_bitmap_get_irq_val(&dist
->irq_enabled
, vcpu
->vcpu_id
, irq
);
285 static int vgic_irq_is_queued(struct kvm_vcpu
*vcpu
, int irq
)
287 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
289 return vgic_bitmap_get_irq_val(&dist
->irq_queued
, vcpu
->vcpu_id
, irq
);
292 static int vgic_irq_is_active(struct kvm_vcpu
*vcpu
, int irq
)
294 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
296 return vgic_bitmap_get_irq_val(&dist
->irq_active
, vcpu
->vcpu_id
, irq
);
299 static void vgic_irq_set_queued(struct kvm_vcpu
*vcpu
, int irq
)
301 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
303 vgic_bitmap_set_irq_val(&dist
->irq_queued
, vcpu
->vcpu_id
, irq
, 1);
306 static void vgic_irq_clear_queued(struct kvm_vcpu
*vcpu
, int irq
)
308 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
310 vgic_bitmap_set_irq_val(&dist
->irq_queued
, vcpu
->vcpu_id
, irq
, 0);
313 static void vgic_irq_set_active(struct kvm_vcpu
*vcpu
, int irq
)
315 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
317 vgic_bitmap_set_irq_val(&dist
->irq_active
, vcpu
->vcpu_id
, irq
, 1);
320 static void vgic_irq_clear_active(struct kvm_vcpu
*vcpu
, int irq
)
322 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
324 vgic_bitmap_set_irq_val(&dist
->irq_active
, vcpu
->vcpu_id
, irq
, 0);
327 static int vgic_dist_irq_get_level(struct kvm_vcpu
*vcpu
, int irq
)
329 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
331 return vgic_bitmap_get_irq_val(&dist
->irq_level
, vcpu
->vcpu_id
, irq
);
334 static void vgic_dist_irq_set_level(struct kvm_vcpu
*vcpu
, int irq
)
336 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
338 vgic_bitmap_set_irq_val(&dist
->irq_level
, vcpu
->vcpu_id
, irq
, 1);
341 static void vgic_dist_irq_clear_level(struct kvm_vcpu
*vcpu
, int irq
)
343 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
345 vgic_bitmap_set_irq_val(&dist
->irq_level
, vcpu
->vcpu_id
, irq
, 0);
348 static int vgic_dist_irq_soft_pend(struct kvm_vcpu
*vcpu
, int irq
)
350 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
352 return vgic_bitmap_get_irq_val(&dist
->irq_soft_pend
, vcpu
->vcpu_id
, irq
);
355 static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu
*vcpu
, int irq
)
357 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
359 vgic_bitmap_set_irq_val(&dist
->irq_soft_pend
, vcpu
->vcpu_id
, irq
, 0);
362 static int vgic_dist_irq_is_pending(struct kvm_vcpu
*vcpu
, int irq
)
364 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
366 return vgic_bitmap_get_irq_val(&dist
->irq_pending
, vcpu
->vcpu_id
, irq
);
369 void vgic_dist_irq_set_pending(struct kvm_vcpu
*vcpu
, int irq
)
371 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
373 vgic_bitmap_set_irq_val(&dist
->irq_pending
, vcpu
->vcpu_id
, irq
, 1);
376 void vgic_dist_irq_clear_pending(struct kvm_vcpu
*vcpu
, int irq
)
378 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
380 vgic_bitmap_set_irq_val(&dist
->irq_pending
, vcpu
->vcpu_id
, irq
, 0);
383 static void vgic_cpu_irq_set(struct kvm_vcpu
*vcpu
, int irq
)
385 if (irq
< VGIC_NR_PRIVATE_IRQS
)
386 set_bit(irq
, vcpu
->arch
.vgic_cpu
.pending_percpu
);
388 set_bit(irq
- VGIC_NR_PRIVATE_IRQS
,
389 vcpu
->arch
.vgic_cpu
.pending_shared
);
392 void vgic_cpu_irq_clear(struct kvm_vcpu
*vcpu
, int irq
)
394 if (irq
< VGIC_NR_PRIVATE_IRQS
)
395 clear_bit(irq
, vcpu
->arch
.vgic_cpu
.pending_percpu
);
397 clear_bit(irq
- VGIC_NR_PRIVATE_IRQS
,
398 vcpu
->arch
.vgic_cpu
.pending_shared
);
401 static bool vgic_can_sample_irq(struct kvm_vcpu
*vcpu
, int irq
)
403 return !vgic_irq_is_queued(vcpu
, irq
);
407 * vgic_reg_access - access vgic register
408 * @mmio: pointer to the data describing the mmio access
409 * @reg: pointer to the virtual backing of vgic distributor data
410 * @offset: least significant 2 bits used for word offset
411 * @mode: ACCESS_ mode (see defines above)
413 * Helper to make vgic register access easier using one of the access
414 * modes defined for vgic register access
415 * (read,raz,write-ignored,setbit,clearbit,write)
417 void vgic_reg_access(struct kvm_exit_mmio
*mmio
, u32
*reg
,
418 phys_addr_t offset
, int mode
)
420 int word_offset
= (offset
& 3) * 8;
421 u32 mask
= (1UL << (mmio
->len
* 8)) - 1;
425 * Any alignment fault should have been delivered to the guest
426 * directly (ARM ARM B3.12.7 "Prioritization of aborts").
432 BUG_ON(mode
!= (ACCESS_READ_RAZ
| ACCESS_WRITE_IGNORED
));
436 if (mmio
->is_write
) {
437 u32 data
= mmio_data_read(mmio
, mask
) << word_offset
;
438 switch (ACCESS_WRITE_MASK(mode
)) {
439 case ACCESS_WRITE_IGNORED
:
442 case ACCESS_WRITE_SETBIT
:
446 case ACCESS_WRITE_CLEARBIT
:
450 case ACCESS_WRITE_VALUE
:
451 regval
= (regval
& ~(mask
<< word_offset
)) | data
;
456 switch (ACCESS_READ_MASK(mode
)) {
457 case ACCESS_READ_RAZ
:
461 case ACCESS_READ_VALUE
:
462 mmio_data_write(mmio
, mask
, regval
>> word_offset
);
467 bool handle_mmio_raz_wi(struct kvm_vcpu
*vcpu
, struct kvm_exit_mmio
*mmio
,
470 vgic_reg_access(mmio
, NULL
, offset
,
471 ACCESS_READ_RAZ
| ACCESS_WRITE_IGNORED
);
475 bool vgic_handle_enable_reg(struct kvm
*kvm
, struct kvm_exit_mmio
*mmio
,
476 phys_addr_t offset
, int vcpu_id
, int access
)
479 int mode
= ACCESS_READ_VALUE
| access
;
480 struct kvm_vcpu
*target_vcpu
= kvm_get_vcpu(kvm
, vcpu_id
);
482 reg
= vgic_bitmap_get_reg(&kvm
->arch
.vgic
.irq_enabled
, vcpu_id
, offset
);
483 vgic_reg_access(mmio
, reg
, offset
, mode
);
484 if (mmio
->is_write
) {
485 if (access
& ACCESS_WRITE_CLEARBIT
) {
486 if (offset
< 4) /* Force SGI enabled */
488 vgic_retire_disabled_irqs(target_vcpu
);
490 vgic_update_state(kvm
);
497 bool vgic_handle_set_pending_reg(struct kvm
*kvm
,
498 struct kvm_exit_mmio
*mmio
,
499 phys_addr_t offset
, int vcpu_id
)
503 int mode
= ACCESS_READ_VALUE
| ACCESS_WRITE_SETBIT
;
504 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
506 reg
= vgic_bitmap_get_reg(&dist
->irq_cfg
, vcpu_id
, offset
);
507 level_mask
= (~(*reg
));
509 /* Mark both level and edge triggered irqs as pending */
510 reg
= vgic_bitmap_get_reg(&dist
->irq_pending
, vcpu_id
, offset
);
512 vgic_reg_access(mmio
, reg
, offset
, mode
);
514 if (mmio
->is_write
) {
515 /* Set the soft-pending flag only for level-triggered irqs */
516 reg
= vgic_bitmap_get_reg(&dist
->irq_soft_pend
,
518 vgic_reg_access(mmio
, reg
, offset
, mode
);
521 /* Ignore writes to SGIs */
524 *reg
|= orig
& 0xffff;
527 vgic_update_state(kvm
);
534 bool vgic_handle_clear_pending_reg(struct kvm
*kvm
,
535 struct kvm_exit_mmio
*mmio
,
536 phys_addr_t offset
, int vcpu_id
)
540 int mode
= ACCESS_READ_VALUE
| ACCESS_WRITE_CLEARBIT
;
541 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
543 reg
= vgic_bitmap_get_reg(&dist
->irq_pending
, vcpu_id
, offset
);
545 vgic_reg_access(mmio
, reg
, offset
, mode
);
546 if (mmio
->is_write
) {
547 /* Re-set level triggered level-active interrupts */
548 level_active
= vgic_bitmap_get_reg(&dist
->irq_level
,
550 reg
= vgic_bitmap_get_reg(&dist
->irq_pending
, vcpu_id
, offset
);
551 *reg
|= *level_active
;
553 /* Ignore writes to SGIs */
556 *reg
|= orig
& 0xffff;
559 /* Clear soft-pending flags */
560 reg
= vgic_bitmap_get_reg(&dist
->irq_soft_pend
,
562 vgic_reg_access(mmio
, reg
, offset
, mode
);
564 vgic_update_state(kvm
);
570 bool vgic_handle_set_active_reg(struct kvm
*kvm
,
571 struct kvm_exit_mmio
*mmio
,
572 phys_addr_t offset
, int vcpu_id
)
575 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
577 reg
= vgic_bitmap_get_reg(&dist
->irq_active
, vcpu_id
, offset
);
578 vgic_reg_access(mmio
, reg
, offset
,
579 ACCESS_READ_VALUE
| ACCESS_WRITE_SETBIT
);
581 if (mmio
->is_write
) {
582 vgic_update_state(kvm
);
589 bool vgic_handle_clear_active_reg(struct kvm
*kvm
,
590 struct kvm_exit_mmio
*mmio
,
591 phys_addr_t offset
, int vcpu_id
)
594 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
596 reg
= vgic_bitmap_get_reg(&dist
->irq_active
, vcpu_id
, offset
);
597 vgic_reg_access(mmio
, reg
, offset
,
598 ACCESS_READ_VALUE
| ACCESS_WRITE_CLEARBIT
);
600 if (mmio
->is_write
) {
601 vgic_update_state(kvm
);
608 static u32
vgic_cfg_expand(u16 val
)
614 * Turn a 16bit value like abcd...mnop into a 32bit word
615 * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
617 for (i
= 0; i
< 16; i
++)
618 res
|= ((val
>> i
) & VGIC_CFG_EDGE
) << (2 * i
+ 1);
623 static u16
vgic_cfg_compress(u32 val
)
629 * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
630 * abcd...mnop which is what we really care about.
632 for (i
= 0; i
< 16; i
++)
633 res
|= ((val
>> (i
* 2 + 1)) & VGIC_CFG_EDGE
) << i
;
639 * The distributor uses 2 bits per IRQ for the CFG register, but the
640 * LSB is always 0. As such, we only keep the upper bit, and use the
641 * two above functions to compress/expand the bits
643 bool vgic_handle_cfg_reg(u32
*reg
, struct kvm_exit_mmio
*mmio
,
653 val
= vgic_cfg_expand(val
);
654 vgic_reg_access(mmio
, &val
, offset
,
655 ACCESS_READ_VALUE
| ACCESS_WRITE_VALUE
);
656 if (mmio
->is_write
) {
658 *reg
= ~0U; /* Force PPIs/SGIs to 1 */
662 val
= vgic_cfg_compress(val
);
667 *reg
&= 0xffff << 16;
676 * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
677 * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
679 * Move any IRQs that have already been assigned to LRs back to the
680 * emulated distributor state so that the complete emulated state can be read
681 * from the main emulation structures without investigating the LRs.
683 void vgic_unqueue_irqs(struct kvm_vcpu
*vcpu
)
685 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
688 for_each_set_bit(i
, vgic_cpu
->lr_used
, vgic_cpu
->nr_lr
) {
689 struct vgic_lr lr
= vgic_get_lr(vcpu
, i
);
692 * There are three options for the state bits:
696 * 11: pending and active
698 BUG_ON(!(lr
.state
& LR_STATE_MASK
));
700 /* Reestablish SGI source for pending and active IRQs */
701 if (lr
.irq
< VGIC_NR_SGIS
)
702 add_sgi_source(vcpu
, lr
.irq
, lr
.source
);
705 * If the LR holds an active (10) or a pending and active (11)
706 * interrupt then move the active state to the
707 * distributor tracking bit.
709 if (lr
.state
& LR_STATE_ACTIVE
) {
710 vgic_irq_set_active(vcpu
, lr
.irq
);
711 lr
.state
&= ~LR_STATE_ACTIVE
;
715 * Reestablish the pending state on the distributor and the
716 * CPU interface. It may have already been pending, but that
717 * is fine, then we are only setting a few bits that were
720 if (lr
.state
& LR_STATE_PENDING
) {
721 vgic_dist_irq_set_pending(vcpu
, lr
.irq
);
722 lr
.state
&= ~LR_STATE_PENDING
;
725 vgic_set_lr(vcpu
, i
, lr
);
728 * Mark the LR as free for other use.
730 BUG_ON(lr
.state
& LR_STATE_MASK
);
731 vgic_retire_lr(i
, lr
.irq
, vcpu
);
732 vgic_irq_clear_queued(vcpu
, lr
.irq
);
734 /* Finally update the VGIC state. */
735 vgic_update_state(vcpu
->kvm
);
740 struct vgic_io_range
*vgic_find_range(const struct vgic_io_range
*ranges
,
741 int len
, gpa_t offset
)
743 while (ranges
->len
) {
744 if (offset
>= ranges
->base
&&
745 (offset
+ len
) <= (ranges
->base
+ ranges
->len
))
753 static bool vgic_validate_access(const struct vgic_dist
*dist
,
754 const struct vgic_io_range
*range
,
755 unsigned long offset
)
759 if (!range
->bits_per_irq
)
760 return true; /* Not an irq-based access */
762 irq
= offset
* 8 / range
->bits_per_irq
;
763 if (irq
>= dist
->nr_irqs
)
770 * Call the respective handler function for the given range.
771 * We split up any 64 bit accesses into two consecutive 32 bit
772 * handler calls and merge the result afterwards.
773 * We do this in a little endian fashion regardless of the host's
774 * or guest's endianness, because the GIC is always LE and the rest of
775 * the code (vgic_reg_access) also puts it in a LE fashion already.
776 * At this point we have already identified the handle function, so
777 * range points to that one entry and offset is relative to this.
779 static bool call_range_handler(struct kvm_vcpu
*vcpu
,
780 struct kvm_exit_mmio
*mmio
,
781 unsigned long offset
,
782 const struct vgic_io_range
*range
)
784 struct kvm_exit_mmio mmio32
;
787 if (likely(mmio
->len
<= 4))
788 return range
->handle_mmio(vcpu
, mmio
, offset
);
791 * Any access bigger than 4 bytes (that we currently handle in KVM)
792 * is actually 8 bytes long, caused by a 64-bit access
796 mmio32
.is_write
= mmio
->is_write
;
797 mmio32
.private = mmio
->private;
799 mmio32
.phys_addr
= mmio
->phys_addr
+ 4;
800 mmio32
.data
= &((u32
*)mmio
->data
)[1];
801 ret
= range
->handle_mmio(vcpu
, &mmio32
, offset
+ 4);
803 mmio32
.phys_addr
= mmio
->phys_addr
;
804 mmio32
.data
= &((u32
*)mmio
->data
)[0];
805 ret
|= range
->handle_mmio(vcpu
, &mmio32
, offset
);
811 * vgic_handle_mmio_access - handle an in-kernel MMIO access
812 * This is called by the read/write KVM IO device wrappers below.
813 * @vcpu: pointer to the vcpu performing the access
814 * @this: pointer to the KVM IO device in charge
815 * @addr: guest physical address of the access
816 * @len: size of the access
817 * @val: pointer to the data region
818 * @is_write: read or write access
820 * returns true if the MMIO access could be performed
822 static int vgic_handle_mmio_access(struct kvm_vcpu
*vcpu
,
823 struct kvm_io_device
*this, gpa_t addr
,
824 int len
, void *val
, bool is_write
)
826 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
827 struct vgic_io_device
*iodev
= container_of(this,
828 struct vgic_io_device
, dev
);
829 struct kvm_run
*run
= vcpu
->run
;
830 const struct vgic_io_range
*range
;
831 struct kvm_exit_mmio mmio
;
835 offset
= addr
- iodev
->addr
;
836 range
= vgic_find_range(iodev
->reg_ranges
, len
, offset
);
837 if (unlikely(!range
|| !range
->handle_mmio
)) {
838 pr_warn("Unhandled access %d %08llx %d\n", is_write
, addr
, len
);
842 mmio
.phys_addr
= addr
;
844 mmio
.is_write
= is_write
;
846 mmio
.private = iodev
->redist_vcpu
;
848 spin_lock(&dist
->lock
);
849 offset
-= range
->base
;
850 if (vgic_validate_access(dist
, range
, offset
)) {
851 updated_state
= call_range_handler(vcpu
, &mmio
, offset
, range
);
855 updated_state
= false;
857 spin_unlock(&dist
->lock
);
858 run
->mmio
.is_write
= is_write
;
860 run
->mmio
.phys_addr
= addr
;
861 memcpy(run
->mmio
.data
, val
, len
);
863 kvm_handle_mmio_return(vcpu
, run
);
866 vgic_kick_vcpus(vcpu
->kvm
);
871 static int vgic_handle_mmio_read(struct kvm_vcpu
*vcpu
,
872 struct kvm_io_device
*this,
873 gpa_t addr
, int len
, void *val
)
875 return vgic_handle_mmio_access(vcpu
, this, addr
, len
, val
, false);
878 static int vgic_handle_mmio_write(struct kvm_vcpu
*vcpu
,
879 struct kvm_io_device
*this,
880 gpa_t addr
, int len
, const void *val
)
882 return vgic_handle_mmio_access(vcpu
, this, addr
, len
, (void *)val
,
886 struct kvm_io_device_ops vgic_io_ops
= {
887 .read
= vgic_handle_mmio_read
,
888 .write
= vgic_handle_mmio_write
,
892 * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus
893 * @kvm: The VM structure pointer
894 * @base: The (guest) base address for the register frame
895 * @len: Length of the register frame window
896 * @ranges: Describing the handler functions for each register
897 * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call
898 * @iodev: Points to memory to be passed on to the handler
900 * @iodev stores the parameters of this function to be usable by the handler
901 * respectively the dispatcher function (since the KVM I/O bus framework lacks
902 * an opaque parameter). Initialization is done in this function, but the
903 * reference should be valid and unique for the whole VGIC lifetime.
904 * If the register frame is not mapped for a specific VCPU, pass -1 to
907 int vgic_register_kvm_io_dev(struct kvm
*kvm
, gpa_t base
, int len
,
908 const struct vgic_io_range
*ranges
,
910 struct vgic_io_device
*iodev
)
912 struct kvm_vcpu
*vcpu
= NULL
;
915 if (redist_vcpu_id
>= 0)
916 vcpu
= kvm_get_vcpu(kvm
, redist_vcpu_id
);
920 iodev
->reg_ranges
= ranges
;
921 iodev
->redist_vcpu
= vcpu
;
923 kvm_iodevice_init(&iodev
->dev
, &vgic_io_ops
);
925 mutex_lock(&kvm
->slots_lock
);
927 ret
= kvm_io_bus_register_dev(kvm
, KVM_MMIO_BUS
, base
, len
,
929 mutex_unlock(&kvm
->slots_lock
);
931 /* Mark the iodev as invalid if registration fails. */
933 iodev
->dev
.ops
= NULL
;
938 static int vgic_nr_shared_irqs(struct vgic_dist
*dist
)
940 return dist
->nr_irqs
- VGIC_NR_PRIVATE_IRQS
;
943 static int compute_active_for_cpu(struct kvm_vcpu
*vcpu
)
945 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
946 unsigned long *active
, *enabled
, *act_percpu
, *act_shared
;
947 unsigned long active_private
, active_shared
;
948 int nr_shared
= vgic_nr_shared_irqs(dist
);
951 vcpu_id
= vcpu
->vcpu_id
;
952 act_percpu
= vcpu
->arch
.vgic_cpu
.active_percpu
;
953 act_shared
= vcpu
->arch
.vgic_cpu
.active_shared
;
955 active
= vgic_bitmap_get_cpu_map(&dist
->irq_active
, vcpu_id
);
956 enabled
= vgic_bitmap_get_cpu_map(&dist
->irq_enabled
, vcpu_id
);
957 bitmap_and(act_percpu
, active
, enabled
, VGIC_NR_PRIVATE_IRQS
);
959 active
= vgic_bitmap_get_shared_map(&dist
->irq_active
);
960 enabled
= vgic_bitmap_get_shared_map(&dist
->irq_enabled
);
961 bitmap_and(act_shared
, active
, enabled
, nr_shared
);
962 bitmap_and(act_shared
, act_shared
,
963 vgic_bitmap_get_shared_map(&dist
->irq_spi_target
[vcpu_id
]),
966 active_private
= find_first_bit(act_percpu
, VGIC_NR_PRIVATE_IRQS
);
967 active_shared
= find_first_bit(act_shared
, nr_shared
);
969 return (active_private
< VGIC_NR_PRIVATE_IRQS
||
970 active_shared
< nr_shared
);
973 static int compute_pending_for_cpu(struct kvm_vcpu
*vcpu
)
975 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
976 unsigned long *pending
, *enabled
, *pend_percpu
, *pend_shared
;
977 unsigned long pending_private
, pending_shared
;
978 int nr_shared
= vgic_nr_shared_irqs(dist
);
981 vcpu_id
= vcpu
->vcpu_id
;
982 pend_percpu
= vcpu
->arch
.vgic_cpu
.pending_percpu
;
983 pend_shared
= vcpu
->arch
.vgic_cpu
.pending_shared
;
985 pending
= vgic_bitmap_get_cpu_map(&dist
->irq_pending
, vcpu_id
);
986 enabled
= vgic_bitmap_get_cpu_map(&dist
->irq_enabled
, vcpu_id
);
987 bitmap_and(pend_percpu
, pending
, enabled
, VGIC_NR_PRIVATE_IRQS
);
989 pending
= vgic_bitmap_get_shared_map(&dist
->irq_pending
);
990 enabled
= vgic_bitmap_get_shared_map(&dist
->irq_enabled
);
991 bitmap_and(pend_shared
, pending
, enabled
, nr_shared
);
992 bitmap_and(pend_shared
, pend_shared
,
993 vgic_bitmap_get_shared_map(&dist
->irq_spi_target
[vcpu_id
]),
996 pending_private
= find_first_bit(pend_percpu
, VGIC_NR_PRIVATE_IRQS
);
997 pending_shared
= find_first_bit(pend_shared
, nr_shared
);
998 return (pending_private
< VGIC_NR_PRIVATE_IRQS
||
999 pending_shared
< vgic_nr_shared_irqs(dist
));
1003 * Update the interrupt state and determine which CPUs have pending
1004 * or active interrupts. Must be called with distributor lock held.
1006 void vgic_update_state(struct kvm
*kvm
)
1008 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
1009 struct kvm_vcpu
*vcpu
;
1012 if (!dist
->enabled
) {
1013 set_bit(0, dist
->irq_pending_on_cpu
);
1017 kvm_for_each_vcpu(c
, vcpu
, kvm
) {
1018 if (compute_pending_for_cpu(vcpu
))
1019 set_bit(c
, dist
->irq_pending_on_cpu
);
1021 if (compute_active_for_cpu(vcpu
))
1022 set_bit(c
, dist
->irq_active_on_cpu
);
1024 clear_bit(c
, dist
->irq_active_on_cpu
);
1028 static struct vgic_lr
vgic_get_lr(const struct kvm_vcpu
*vcpu
, int lr
)
1030 return vgic_ops
->get_lr(vcpu
, lr
);
1033 static void vgic_set_lr(struct kvm_vcpu
*vcpu
, int lr
,
1036 vgic_ops
->set_lr(vcpu
, lr
, vlr
);
1039 static void vgic_sync_lr_elrsr(struct kvm_vcpu
*vcpu
, int lr
,
1042 vgic_ops
->sync_lr_elrsr(vcpu
, lr
, vlr
);
1045 static inline u64
vgic_get_elrsr(struct kvm_vcpu
*vcpu
)
1047 return vgic_ops
->get_elrsr(vcpu
);
1050 static inline u64
vgic_get_eisr(struct kvm_vcpu
*vcpu
)
1052 return vgic_ops
->get_eisr(vcpu
);
1055 static inline void vgic_clear_eisr(struct kvm_vcpu
*vcpu
)
1057 vgic_ops
->clear_eisr(vcpu
);
1060 static inline u32
vgic_get_interrupt_status(struct kvm_vcpu
*vcpu
)
1062 return vgic_ops
->get_interrupt_status(vcpu
);
1065 static inline void vgic_enable_underflow(struct kvm_vcpu
*vcpu
)
1067 vgic_ops
->enable_underflow(vcpu
);
1070 static inline void vgic_disable_underflow(struct kvm_vcpu
*vcpu
)
1072 vgic_ops
->disable_underflow(vcpu
);
1075 void vgic_get_vmcr(struct kvm_vcpu
*vcpu
, struct vgic_vmcr
*vmcr
)
1077 vgic_ops
->get_vmcr(vcpu
, vmcr
);
1080 void vgic_set_vmcr(struct kvm_vcpu
*vcpu
, struct vgic_vmcr
*vmcr
)
1082 vgic_ops
->set_vmcr(vcpu
, vmcr
);
1085 static inline void vgic_enable(struct kvm_vcpu
*vcpu
)
1087 vgic_ops
->enable(vcpu
);
1090 static void vgic_retire_lr(int lr_nr
, int irq
, struct kvm_vcpu
*vcpu
)
1092 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1093 struct vgic_lr vlr
= vgic_get_lr(vcpu
, lr_nr
);
1096 vgic_set_lr(vcpu
, lr_nr
, vlr
);
1097 clear_bit(lr_nr
, vgic_cpu
->lr_used
);
1098 vgic_cpu
->vgic_irq_lr_map
[irq
] = LR_EMPTY
;
1099 vgic_sync_lr_elrsr(vcpu
, lr_nr
, vlr
);
1103 * An interrupt may have been disabled after being made pending on the
1104 * CPU interface (the classic case is a timer running while we're
1105 * rebooting the guest - the interrupt would kick as soon as the CPU
1106 * interface gets enabled, with deadly consequences).
1108 * The solution is to examine already active LRs, and check the
1109 * interrupt is still enabled. If not, just retire it.
1111 static void vgic_retire_disabled_irqs(struct kvm_vcpu
*vcpu
)
1113 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1116 for_each_set_bit(lr
, vgic_cpu
->lr_used
, vgic
->nr_lr
) {
1117 struct vgic_lr vlr
= vgic_get_lr(vcpu
, lr
);
1119 if (!vgic_irq_is_enabled(vcpu
, vlr
.irq
)) {
1120 vgic_retire_lr(lr
, vlr
.irq
, vcpu
);
1121 if (vgic_irq_is_queued(vcpu
, vlr
.irq
))
1122 vgic_irq_clear_queued(vcpu
, vlr
.irq
);
1127 static void vgic_queue_irq_to_lr(struct kvm_vcpu
*vcpu
, int irq
,
1128 int lr_nr
, struct vgic_lr vlr
)
1130 if (vgic_irq_is_active(vcpu
, irq
)) {
1131 vlr
.state
|= LR_STATE_ACTIVE
;
1132 kvm_debug("Set active, clear distributor: 0x%x\n", vlr
.state
);
1133 vgic_irq_clear_active(vcpu
, irq
);
1134 vgic_update_state(vcpu
->kvm
);
1136 WARN_ON(!vgic_dist_irq_is_pending(vcpu
, irq
));
1137 vlr
.state
|= LR_STATE_PENDING
;
1138 kvm_debug("Set pending: 0x%x\n", vlr
.state
);
1141 if (!vgic_irq_is_edge(vcpu
, irq
))
1142 vlr
.state
|= LR_EOI_INT
;
1144 if (vlr
.irq
>= VGIC_NR_SGIS
) {
1145 struct irq_phys_map
*map
;
1146 map
= vgic_irq_map_search(vcpu
, irq
);
1149 vlr
.hwirq
= map
->phys_irq
;
1151 vlr
.state
&= ~LR_EOI_INT
;
1154 * Make sure we're not going to sample this
1155 * again, as a HW-backed interrupt cannot be
1156 * in the PENDING_ACTIVE stage.
1158 vgic_irq_set_queued(vcpu
, irq
);
1162 vgic_set_lr(vcpu
, lr_nr
, vlr
);
1163 vgic_sync_lr_elrsr(vcpu
, lr_nr
, vlr
);
1167 * Queue an interrupt to a CPU virtual interface. Return true on success,
1168 * or false if it wasn't possible to queue it.
1169 * sgi_source must be zero for any non-SGI interrupts.
1171 bool vgic_queue_irq(struct kvm_vcpu
*vcpu
, u8 sgi_source_id
, int irq
)
1173 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1174 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1178 /* Sanitize the input... */
1179 BUG_ON(sgi_source_id
& ~7);
1180 BUG_ON(sgi_source_id
&& irq
>= VGIC_NR_SGIS
);
1181 BUG_ON(irq
>= dist
->nr_irqs
);
1183 kvm_debug("Queue IRQ%d\n", irq
);
1185 lr
= vgic_cpu
->vgic_irq_lr_map
[irq
];
1187 /* Do we have an active interrupt for the same CPUID? */
1188 if (lr
!= LR_EMPTY
) {
1189 vlr
= vgic_get_lr(vcpu
, lr
);
1190 if (vlr
.source
== sgi_source_id
) {
1191 kvm_debug("LR%d piggyback for IRQ%d\n", lr
, vlr
.irq
);
1192 BUG_ON(!test_bit(lr
, vgic_cpu
->lr_used
));
1193 vgic_queue_irq_to_lr(vcpu
, irq
, lr
, vlr
);
1198 /* Try to use another LR for this interrupt */
1199 lr
= find_first_zero_bit((unsigned long *)vgic_cpu
->lr_used
,
1201 if (lr
>= vgic
->nr_lr
)
1204 kvm_debug("LR%d allocated for IRQ%d %x\n", lr
, irq
, sgi_source_id
);
1205 vgic_cpu
->vgic_irq_lr_map
[irq
] = lr
;
1206 set_bit(lr
, vgic_cpu
->lr_used
);
1209 vlr
.source
= sgi_source_id
;
1211 vgic_queue_irq_to_lr(vcpu
, irq
, lr
, vlr
);
1216 static bool vgic_queue_hwirq(struct kvm_vcpu
*vcpu
, int irq
)
1218 if (!vgic_can_sample_irq(vcpu
, irq
))
1219 return true; /* level interrupt, already queued */
1221 if (vgic_queue_irq(vcpu
, 0, irq
)) {
1222 if (vgic_irq_is_edge(vcpu
, irq
)) {
1223 vgic_dist_irq_clear_pending(vcpu
, irq
);
1224 vgic_cpu_irq_clear(vcpu
, irq
);
1226 vgic_irq_set_queued(vcpu
, irq
);
1236 * Fill the list registers with pending interrupts before running the
1239 static void __kvm_vgic_flush_hwstate(struct kvm_vcpu
*vcpu
)
1241 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1242 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1243 unsigned long *pa_percpu
, *pa_shared
;
1244 int i
, vcpu_id
, lr
, ret
;
1246 int nr_shared
= vgic_nr_shared_irqs(dist
);
1248 vcpu_id
= vcpu
->vcpu_id
;
1250 pa_percpu
= vcpu
->arch
.vgic_cpu
.pend_act_percpu
;
1251 pa_shared
= vcpu
->arch
.vgic_cpu
.pend_act_shared
;
1253 bitmap_or(pa_percpu
, vgic_cpu
->pending_percpu
, vgic_cpu
->active_percpu
,
1254 VGIC_NR_PRIVATE_IRQS
);
1255 bitmap_or(pa_shared
, vgic_cpu
->pending_shared
, vgic_cpu
->active_shared
,
1258 * We may not have any pending interrupt, or the interrupts
1259 * may have been serviced from another vcpu. In all cases,
1262 if (!kvm_vgic_vcpu_pending_irq(vcpu
) && !kvm_vgic_vcpu_active_irq(vcpu
))
1266 for_each_set_bit(i
, pa_percpu
, VGIC_NR_SGIS
) {
1267 if (!queue_sgi(vcpu
, i
))
1272 for_each_set_bit_from(i
, pa_percpu
, VGIC_NR_PRIVATE_IRQS
) {
1273 if (!vgic_queue_hwirq(vcpu
, i
))
1278 for_each_set_bit(i
, pa_shared
, nr_shared
) {
1279 if (!vgic_queue_hwirq(vcpu
, i
+ VGIC_NR_PRIVATE_IRQS
))
1288 vgic_enable_underflow(vcpu
);
1290 vgic_disable_underflow(vcpu
);
1292 * We're about to run this VCPU, and we've consumed
1293 * everything the distributor had in store for
1294 * us. Claim we don't have anything pending. We'll
1295 * adjust that if needed while exiting.
1297 clear_bit(vcpu_id
, dist
->irq_pending_on_cpu
);
1300 for (lr
= 0; lr
< vgic
->nr_lr
; lr
++) {
1303 if (!test_bit(lr
, vgic_cpu
->lr_used
))
1306 vlr
= vgic_get_lr(vcpu
, lr
);
1309 * If we have a mapping, and the virtual interrupt is
1310 * presented to the guest (as pending or active), then we must
1311 * set the state to active in the physical world. See
1312 * Documentation/virtual/kvm/arm/vgic-mapped-irqs.txt.
1314 if (vlr
.state
& LR_HW
) {
1315 struct irq_phys_map
*map
;
1316 map
= vgic_irq_map_search(vcpu
, vlr
.irq
);
1318 ret
= irq_set_irqchip_state(map
->irq
,
1319 IRQCHIP_STATE_ACTIVE
,
1326 static bool vgic_process_maintenance(struct kvm_vcpu
*vcpu
)
1328 u32 status
= vgic_get_interrupt_status(vcpu
);
1329 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1330 bool level_pending
= false;
1331 struct kvm
*kvm
= vcpu
->kvm
;
1333 kvm_debug("STATUS = %08x\n", status
);
1335 if (status
& INT_STATUS_EOI
) {
1337 * Some level interrupts have been EOIed. Clear their
1340 u64 eisr
= vgic_get_eisr(vcpu
);
1341 unsigned long *eisr_ptr
= u64_to_bitmask(&eisr
);
1344 for_each_set_bit(lr
, eisr_ptr
, vgic
->nr_lr
) {
1345 struct vgic_lr vlr
= vgic_get_lr(vcpu
, lr
);
1346 WARN_ON(vgic_irq_is_edge(vcpu
, vlr
.irq
));
1348 spin_lock(&dist
->lock
);
1349 vgic_irq_clear_queued(vcpu
, vlr
.irq
);
1350 WARN_ON(vlr
.state
& LR_STATE_MASK
);
1352 vgic_set_lr(vcpu
, lr
, vlr
);
1355 * If the IRQ was EOIed it was also ACKed and we we
1356 * therefore assume we can clear the soft pending
1357 * state (should it had been set) for this interrupt.
1359 * Note: if the IRQ soft pending state was set after
1360 * the IRQ was acked, it actually shouldn't be
1361 * cleared, but we have no way of knowing that unless
1362 * we start trapping ACKs when the soft-pending state
1365 vgic_dist_irq_clear_soft_pend(vcpu
, vlr
.irq
);
1368 * kvm_notify_acked_irq calls kvm_set_irq()
1369 * to reset the IRQ level. Need to release the
1370 * lock for kvm_set_irq to grab it.
1372 spin_unlock(&dist
->lock
);
1374 kvm_notify_acked_irq(kvm
, 0,
1375 vlr
.irq
- VGIC_NR_PRIVATE_IRQS
);
1376 spin_lock(&dist
->lock
);
1378 /* Any additional pending interrupt? */
1379 if (vgic_dist_irq_get_level(vcpu
, vlr
.irq
)) {
1380 vgic_cpu_irq_set(vcpu
, vlr
.irq
);
1381 level_pending
= true;
1383 vgic_dist_irq_clear_pending(vcpu
, vlr
.irq
);
1384 vgic_cpu_irq_clear(vcpu
, vlr
.irq
);
1387 spin_unlock(&dist
->lock
);
1390 * Despite being EOIed, the LR may not have
1391 * been marked as empty.
1393 vgic_sync_lr_elrsr(vcpu
, lr
, vlr
);
1397 if (status
& INT_STATUS_UNDERFLOW
)
1398 vgic_disable_underflow(vcpu
);
1401 * In the next iterations of the vcpu loop, if we sync the vgic state
1402 * after flushing it, but before entering the guest (this happens for
1403 * pending signals and vmid rollovers), then make sure we don't pick
1404 * up any old maintenance interrupts here.
1406 vgic_clear_eisr(vcpu
);
1408 return level_pending
;
1412 * Save the physical active state, and reset it to inactive.
1414 * Return 1 if HW interrupt went from active to inactive, and 0 otherwise.
1416 static int vgic_sync_hwirq(struct kvm_vcpu
*vcpu
, struct vgic_lr vlr
)
1418 struct irq_phys_map
*map
;
1421 if (!(vlr
.state
& LR_HW
))
1424 map
= vgic_irq_map_search(vcpu
, vlr
.irq
);
1425 BUG_ON(!map
|| !map
->active
);
1427 ret
= irq_get_irqchip_state(map
->irq
,
1428 IRQCHIP_STATE_ACTIVE
,
1434 ret
= irq_set_irqchip_state(map
->irq
,
1435 IRQCHIP_STATE_ACTIVE
,
1444 /* Sync back the VGIC state after a guest run */
1445 static void __kvm_vgic_sync_hwstate(struct kvm_vcpu
*vcpu
)
1447 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1448 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1450 unsigned long *elrsr_ptr
;
1454 level_pending
= vgic_process_maintenance(vcpu
);
1455 elrsr
= vgic_get_elrsr(vcpu
);
1456 elrsr_ptr
= u64_to_bitmask(&elrsr
);
1458 /* Deal with HW interrupts, and clear mappings for empty LRs */
1459 for (lr
= 0; lr
< vgic
->nr_lr
; lr
++) {
1462 if (!test_bit(lr
, vgic_cpu
->lr_used
))
1465 vlr
= vgic_get_lr(vcpu
, lr
);
1466 if (vgic_sync_hwirq(vcpu
, vlr
)) {
1468 * So this is a HW interrupt that the guest
1469 * EOI-ed. Clean the LR state and allow the
1470 * interrupt to be sampled again.
1474 vgic_set_lr(vcpu
, lr
, vlr
);
1475 vgic_irq_clear_queued(vcpu
, vlr
.irq
);
1476 set_bit(lr
, elrsr_ptr
);
1479 if (!test_bit(lr
, elrsr_ptr
))
1482 clear_bit(lr
, vgic_cpu
->lr_used
);
1484 BUG_ON(vlr
.irq
>= dist
->nr_irqs
);
1485 vgic_cpu
->vgic_irq_lr_map
[vlr
.irq
] = LR_EMPTY
;
1488 /* Check if we still have something up our sleeve... */
1489 pending
= find_first_zero_bit(elrsr_ptr
, vgic
->nr_lr
);
1490 if (level_pending
|| pending
< vgic
->nr_lr
)
1491 set_bit(vcpu
->vcpu_id
, dist
->irq_pending_on_cpu
);
1494 void kvm_vgic_flush_hwstate(struct kvm_vcpu
*vcpu
)
1496 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1498 if (!irqchip_in_kernel(vcpu
->kvm
))
1501 spin_lock(&dist
->lock
);
1502 __kvm_vgic_flush_hwstate(vcpu
);
1503 spin_unlock(&dist
->lock
);
1506 void kvm_vgic_sync_hwstate(struct kvm_vcpu
*vcpu
)
1508 if (!irqchip_in_kernel(vcpu
->kvm
))
1511 __kvm_vgic_sync_hwstate(vcpu
);
1514 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu
*vcpu
)
1516 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1518 if (!irqchip_in_kernel(vcpu
->kvm
))
1521 return test_bit(vcpu
->vcpu_id
, dist
->irq_pending_on_cpu
);
1524 int kvm_vgic_vcpu_active_irq(struct kvm_vcpu
*vcpu
)
1526 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1528 if (!irqchip_in_kernel(vcpu
->kvm
))
1531 return test_bit(vcpu
->vcpu_id
, dist
->irq_active_on_cpu
);
1535 void vgic_kick_vcpus(struct kvm
*kvm
)
1537 struct kvm_vcpu
*vcpu
;
1541 * We've injected an interrupt, time to find out who deserves
1544 kvm_for_each_vcpu(c
, vcpu
, kvm
) {
1545 if (kvm_vgic_vcpu_pending_irq(vcpu
))
1546 kvm_vcpu_kick(vcpu
);
1550 static int vgic_validate_injection(struct kvm_vcpu
*vcpu
, int irq
, int level
)
1552 int edge_triggered
= vgic_irq_is_edge(vcpu
, irq
);
1555 * Only inject an interrupt if:
1556 * - edge triggered and we have a rising edge
1557 * - level triggered and we change level
1559 if (edge_triggered
) {
1560 int state
= vgic_dist_irq_is_pending(vcpu
, irq
);
1561 return level
> state
;
1563 int state
= vgic_dist_irq_get_level(vcpu
, irq
);
1564 return level
!= state
;
1568 static int vgic_update_irq_pending(struct kvm
*kvm
, int cpuid
,
1569 struct irq_phys_map
*map
,
1570 unsigned int irq_num
, bool level
)
1572 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
1573 struct kvm_vcpu
*vcpu
;
1574 int edge_triggered
, level_triggered
;
1576 bool ret
= true, can_inject
= true;
1578 if (irq_num
>= min(kvm
->arch
.vgic
.nr_irqs
, 1020))
1581 spin_lock(&dist
->lock
);
1583 vcpu
= kvm_get_vcpu(kvm
, cpuid
);
1584 edge_triggered
= vgic_irq_is_edge(vcpu
, irq_num
);
1585 level_triggered
= !edge_triggered
;
1587 if (!vgic_validate_injection(vcpu
, irq_num
, level
)) {
1592 if (irq_num
>= VGIC_NR_PRIVATE_IRQS
) {
1593 cpuid
= dist
->irq_spi_cpu
[irq_num
- VGIC_NR_PRIVATE_IRQS
];
1594 if (cpuid
== VCPU_NOT_ALLOCATED
) {
1595 /* Pretend we use CPU0, and prevent injection */
1599 vcpu
= kvm_get_vcpu(kvm
, cpuid
);
1602 kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num
, level
, cpuid
);
1605 if (level_triggered
)
1606 vgic_dist_irq_set_level(vcpu
, irq_num
);
1607 vgic_dist_irq_set_pending(vcpu
, irq_num
);
1609 if (level_triggered
) {
1610 vgic_dist_irq_clear_level(vcpu
, irq_num
);
1611 if (!vgic_dist_irq_soft_pend(vcpu
, irq_num
)) {
1612 vgic_dist_irq_clear_pending(vcpu
, irq_num
);
1613 vgic_cpu_irq_clear(vcpu
, irq_num
);
1614 if (!compute_pending_for_cpu(vcpu
))
1615 clear_bit(cpuid
, dist
->irq_pending_on_cpu
);
1623 enabled
= vgic_irq_is_enabled(vcpu
, irq_num
);
1625 if (!enabled
|| !can_inject
) {
1630 if (!vgic_can_sample_irq(vcpu
, irq_num
)) {
1632 * Level interrupt in progress, will be picked up
1640 vgic_cpu_irq_set(vcpu
, irq_num
);
1641 set_bit(cpuid
, dist
->irq_pending_on_cpu
);
1645 spin_unlock(&dist
->lock
);
1648 /* kick the specified vcpu */
1649 kvm_vcpu_kick(kvm_get_vcpu(kvm
, cpuid
));
1655 static int vgic_lazy_init(struct kvm
*kvm
)
1659 if (unlikely(!vgic_initialized(kvm
))) {
1661 * We only provide the automatic initialization of the VGIC
1662 * for the legacy case of a GICv2. Any other type must
1663 * be explicitly initialized once setup with the respective
1666 if (kvm
->arch
.vgic
.vgic_model
!= KVM_DEV_TYPE_ARM_VGIC_V2
)
1669 mutex_lock(&kvm
->lock
);
1670 ret
= vgic_init(kvm
);
1671 mutex_unlock(&kvm
->lock
);
1678 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1679 * @kvm: The VM structure pointer
1680 * @cpuid: The CPU for PPIs
1681 * @irq_num: The IRQ number that is assigned to the device. This IRQ
1682 * must not be mapped to a HW interrupt.
1683 * @level: Edge-triggered: true: to trigger the interrupt
1684 * false: to ignore the call
1685 * Level-sensitive true: raise the input signal
1686 * false: lower the input signal
1688 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1689 * level-sensitive interrupts. You can think of the level parameter as 1
1690 * being HIGH and 0 being LOW and all devices being active-HIGH.
1692 int kvm_vgic_inject_irq(struct kvm
*kvm
, int cpuid
, unsigned int irq_num
,
1695 struct irq_phys_map
*map
;
1698 ret
= vgic_lazy_init(kvm
);
1702 map
= vgic_irq_map_search(kvm_get_vcpu(kvm
, cpuid
), irq_num
);
1706 return vgic_update_irq_pending(kvm
, cpuid
, NULL
, irq_num
, level
);
1710 * kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic
1711 * @kvm: The VM structure pointer
1712 * @cpuid: The CPU for PPIs
1713 * @map: Pointer to a irq_phys_map structure describing the mapping
1714 * @level: Edge-triggered: true: to trigger the interrupt
1715 * false: to ignore the call
1716 * Level-sensitive true: raise the input signal
1717 * false: lower the input signal
1719 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1720 * level-sensitive interrupts. You can think of the level parameter as 1
1721 * being HIGH and 0 being LOW and all devices being active-HIGH.
1723 int kvm_vgic_inject_mapped_irq(struct kvm
*kvm
, int cpuid
,
1724 struct irq_phys_map
*map
, bool level
)
1728 ret
= vgic_lazy_init(kvm
);
1732 return vgic_update_irq_pending(kvm
, cpuid
, map
, map
->virt_irq
, level
);
1735 static irqreturn_t
vgic_maintenance_handler(int irq
, void *data
)
1738 * We cannot rely on the vgic maintenance interrupt to be
1739 * delivered synchronously. This means we can only use it to
1740 * exit the VM, and we perform the handling of EOIed
1741 * interrupts on the exit path (see vgic_process_maintenance).
1746 static struct list_head
*vgic_get_irq_phys_map_list(struct kvm_vcpu
*vcpu
,
1749 if (virt_irq
< VGIC_NR_PRIVATE_IRQS
)
1750 return &vcpu
->arch
.vgic_cpu
.irq_phys_map_list
;
1752 return &vcpu
->kvm
->arch
.vgic
.irq_phys_map_list
;
1756 * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ
1757 * @vcpu: The VCPU pointer
1758 * @virt_irq: The virtual irq number
1759 * @irq: The Linux IRQ number
1761 * Establish a mapping between a guest visible irq (@virt_irq) and a
1762 * Linux irq (@irq). On injection, @virt_irq will be associated with
1763 * the physical interrupt represented by @irq. This mapping can be
1764 * established multiple times as long as the parameters are the same.
1766 * Returns a valid pointer on success, and an error pointer otherwise
1768 struct irq_phys_map
*kvm_vgic_map_phys_irq(struct kvm_vcpu
*vcpu
,
1769 int virt_irq
, int irq
)
1771 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1772 struct list_head
*root
= vgic_get_irq_phys_map_list(vcpu
, virt_irq
);
1773 struct irq_phys_map
*map
;
1774 struct irq_phys_map_entry
*entry
;
1775 struct irq_desc
*desc
;
1776 struct irq_data
*data
;
1779 desc
= irq_to_desc(irq
);
1781 kvm_err("%s: no interrupt descriptor\n", __func__
);
1782 return ERR_PTR(-EINVAL
);
1785 data
= irq_desc_get_irq_data(desc
);
1786 while (data
->parent_data
)
1787 data
= data
->parent_data
;
1789 phys_irq
= data
->hwirq
;
1791 /* Create a new mapping */
1792 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
1794 return ERR_PTR(-ENOMEM
);
1796 spin_lock(&dist
->irq_phys_map_lock
);
1798 /* Try to match an existing mapping */
1799 map
= vgic_irq_map_search(vcpu
, virt_irq
);
1801 /* Make sure this mapping matches */
1802 if (map
->phys_irq
!= phys_irq
||
1804 map
= ERR_PTR(-EINVAL
);
1806 /* Found an existing, valid mapping */
1811 map
->virt_irq
= virt_irq
;
1812 map
->phys_irq
= phys_irq
;
1815 list_add_tail_rcu(&entry
->entry
, root
);
1818 spin_unlock(&dist
->irq_phys_map_lock
);
1819 /* If we've found a hit in the existing list, free the useless
1821 if (IS_ERR(map
) || map
!= &entry
->map
)
1826 static struct irq_phys_map
*vgic_irq_map_search(struct kvm_vcpu
*vcpu
,
1829 struct list_head
*root
= vgic_get_irq_phys_map_list(vcpu
, virt_irq
);
1830 struct irq_phys_map_entry
*entry
;
1831 struct irq_phys_map
*map
;
1835 list_for_each_entry_rcu(entry
, root
, entry
) {
1837 if (map
->virt_irq
== virt_irq
) {
1848 static void vgic_free_phys_irq_map_rcu(struct rcu_head
*rcu
)
1850 struct irq_phys_map_entry
*entry
;
1852 entry
= container_of(rcu
, struct irq_phys_map_entry
, rcu
);
1857 * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ
1859 * Return the logical active state of a mapped interrupt. This doesn't
1860 * necessarily reflects the current HW state.
1862 bool kvm_vgic_get_phys_irq_active(struct irq_phys_map
*map
)
1869 * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ
1871 * Set the logical active state of a mapped interrupt. This doesn't
1872 * immediately affects the HW state.
1874 void kvm_vgic_set_phys_irq_active(struct irq_phys_map
*map
, bool active
)
1877 map
->active
= active
;
1881 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1882 * @vcpu: The VCPU pointer
1883 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
1885 * Remove an existing mapping between virtual and physical interrupts.
1887 int kvm_vgic_unmap_phys_irq(struct kvm_vcpu
*vcpu
, struct irq_phys_map
*map
)
1889 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
1890 struct irq_phys_map_entry
*entry
;
1891 struct list_head
*root
;
1896 root
= vgic_get_irq_phys_map_list(vcpu
, map
->virt_irq
);
1898 spin_lock(&dist
->irq_phys_map_lock
);
1900 list_for_each_entry(entry
, root
, entry
) {
1901 if (&entry
->map
== map
) {
1902 list_del_rcu(&entry
->entry
);
1903 call_rcu(&entry
->rcu
, vgic_free_phys_irq_map_rcu
);
1908 spin_unlock(&dist
->irq_phys_map_lock
);
1913 static void vgic_destroy_irq_phys_map(struct kvm
*kvm
, struct list_head
*root
)
1915 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
1916 struct irq_phys_map_entry
*entry
;
1918 spin_lock(&dist
->irq_phys_map_lock
);
1920 list_for_each_entry(entry
, root
, entry
) {
1921 list_del_rcu(&entry
->entry
);
1922 call_rcu(&entry
->rcu
, vgic_free_phys_irq_map_rcu
);
1925 spin_unlock(&dist
->irq_phys_map_lock
);
1928 void kvm_vgic_vcpu_destroy(struct kvm_vcpu
*vcpu
)
1930 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1932 kfree(vgic_cpu
->pending_shared
);
1933 kfree(vgic_cpu
->active_shared
);
1934 kfree(vgic_cpu
->pend_act_shared
);
1935 kfree(vgic_cpu
->vgic_irq_lr_map
);
1936 vgic_destroy_irq_phys_map(vcpu
->kvm
, &vgic_cpu
->irq_phys_map_list
);
1937 vgic_cpu
->pending_shared
= NULL
;
1938 vgic_cpu
->active_shared
= NULL
;
1939 vgic_cpu
->pend_act_shared
= NULL
;
1940 vgic_cpu
->vgic_irq_lr_map
= NULL
;
1943 static int vgic_vcpu_init_maps(struct kvm_vcpu
*vcpu
, int nr_irqs
)
1945 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1947 int sz
= (nr_irqs
- VGIC_NR_PRIVATE_IRQS
) / 8;
1948 vgic_cpu
->pending_shared
= kzalloc(sz
, GFP_KERNEL
);
1949 vgic_cpu
->active_shared
= kzalloc(sz
, GFP_KERNEL
);
1950 vgic_cpu
->pend_act_shared
= kzalloc(sz
, GFP_KERNEL
);
1951 vgic_cpu
->vgic_irq_lr_map
= kmalloc(nr_irqs
, GFP_KERNEL
);
1953 if (!vgic_cpu
->pending_shared
1954 || !vgic_cpu
->active_shared
1955 || !vgic_cpu
->pend_act_shared
1956 || !vgic_cpu
->vgic_irq_lr_map
) {
1957 kvm_vgic_vcpu_destroy(vcpu
);
1961 memset(vgic_cpu
->vgic_irq_lr_map
, LR_EMPTY
, nr_irqs
);
1964 * Store the number of LRs per vcpu, so we don't have to go
1965 * all the way to the distributor structure to find out. Only
1966 * assembly code should use this one.
1968 vgic_cpu
->nr_lr
= vgic
->nr_lr
;
1974 * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage
1976 * No memory allocation should be performed here, only static init.
1978 void kvm_vgic_vcpu_early_init(struct kvm_vcpu
*vcpu
)
1980 struct vgic_cpu
*vgic_cpu
= &vcpu
->arch
.vgic_cpu
;
1981 INIT_LIST_HEAD(&vgic_cpu
->irq_phys_map_list
);
1985 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
1987 * The host's GIC naturally limits the maximum amount of VCPUs a guest
1990 int kvm_vgic_get_max_vcpus(void)
1992 return vgic
->max_gic_vcpus
;
1995 void kvm_vgic_destroy(struct kvm
*kvm
)
1997 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
1998 struct kvm_vcpu
*vcpu
;
2001 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2002 kvm_vgic_vcpu_destroy(vcpu
);
2004 vgic_free_bitmap(&dist
->irq_enabled
);
2005 vgic_free_bitmap(&dist
->irq_level
);
2006 vgic_free_bitmap(&dist
->irq_pending
);
2007 vgic_free_bitmap(&dist
->irq_soft_pend
);
2008 vgic_free_bitmap(&dist
->irq_queued
);
2009 vgic_free_bitmap(&dist
->irq_cfg
);
2010 vgic_free_bytemap(&dist
->irq_priority
);
2011 if (dist
->irq_spi_target
) {
2012 for (i
= 0; i
< dist
->nr_cpus
; i
++)
2013 vgic_free_bitmap(&dist
->irq_spi_target
[i
]);
2015 kfree(dist
->irq_sgi_sources
);
2016 kfree(dist
->irq_spi_cpu
);
2017 kfree(dist
->irq_spi_mpidr
);
2018 kfree(dist
->irq_spi_target
);
2019 kfree(dist
->irq_pending_on_cpu
);
2020 kfree(dist
->irq_active_on_cpu
);
2021 vgic_destroy_irq_phys_map(kvm
, &dist
->irq_phys_map_list
);
2022 dist
->irq_sgi_sources
= NULL
;
2023 dist
->irq_spi_cpu
= NULL
;
2024 dist
->irq_spi_target
= NULL
;
2025 dist
->irq_pending_on_cpu
= NULL
;
2026 dist
->irq_active_on_cpu
= NULL
;
2031 * Allocate and initialize the various data structures. Must be called
2032 * with kvm->lock held!
2034 int vgic_init(struct kvm
*kvm
)
2036 struct vgic_dist
*dist
= &kvm
->arch
.vgic
;
2037 struct kvm_vcpu
*vcpu
;
2038 int nr_cpus
, nr_irqs
;
2039 int ret
, i
, vcpu_id
;
2041 if (vgic_initialized(kvm
))
2044 nr_cpus
= dist
->nr_cpus
= atomic_read(&kvm
->online_vcpus
);
2045 if (!nr_cpus
) /* No vcpus? Can't be good... */
2049 * If nobody configured the number of interrupts, use the
2053 dist
->nr_irqs
= VGIC_NR_IRQS_LEGACY
;
2055 nr_irqs
= dist
->nr_irqs
;
2057 ret
= vgic_init_bitmap(&dist
->irq_enabled
, nr_cpus
, nr_irqs
);
2058 ret
|= vgic_init_bitmap(&dist
->irq_level
, nr_cpus
, nr_irqs
);
2059 ret
|= vgic_init_bitmap(&dist
->irq_pending
, nr_cpus
, nr_irqs
);
2060 ret
|= vgic_init_bitmap(&dist
->irq_soft_pend
, nr_cpus
, nr_irqs
);
2061 ret
|= vgic_init_bitmap(&dist
->irq_queued
, nr_cpus
, nr_irqs
);
2062 ret
|= vgic_init_bitmap(&dist
->irq_active
, nr_cpus
, nr_irqs
);
2063 ret
|= vgic_init_bitmap(&dist
->irq_cfg
, nr_cpus
, nr_irqs
);
2064 ret
|= vgic_init_bytemap(&dist
->irq_priority
, nr_cpus
, nr_irqs
);
2069 dist
->irq_sgi_sources
= kzalloc(nr_cpus
* VGIC_NR_SGIS
, GFP_KERNEL
);
2070 dist
->irq_spi_cpu
= kzalloc(nr_irqs
- VGIC_NR_PRIVATE_IRQS
, GFP_KERNEL
);
2071 dist
->irq_spi_target
= kzalloc(sizeof(*dist
->irq_spi_target
) * nr_cpus
,
2073 dist
->irq_pending_on_cpu
= kzalloc(BITS_TO_LONGS(nr_cpus
) * sizeof(long),
2075 dist
->irq_active_on_cpu
= kzalloc(BITS_TO_LONGS(nr_cpus
) * sizeof(long),
2077 if (!dist
->irq_sgi_sources
||
2078 !dist
->irq_spi_cpu
||
2079 !dist
->irq_spi_target
||
2080 !dist
->irq_pending_on_cpu
||
2081 !dist
->irq_active_on_cpu
) {
2086 for (i
= 0; i
< nr_cpus
; i
++)
2087 ret
|= vgic_init_bitmap(&dist
->irq_spi_target
[i
],
2093 ret
= kvm
->arch
.vgic
.vm_ops
.init_model(kvm
);
2097 kvm_for_each_vcpu(vcpu_id
, vcpu
, kvm
) {
2098 ret
= vgic_vcpu_init_maps(vcpu
, nr_irqs
);
2100 kvm_err("VGIC: Failed to allocate vcpu memory\n");
2104 for (i
= 0; i
< dist
->nr_irqs
; i
++) {
2105 if (i
< VGIC_NR_PPIS
)
2106 vgic_bitmap_set_irq_val(&dist
->irq_enabled
,
2107 vcpu
->vcpu_id
, i
, 1);
2108 if (i
< VGIC_NR_PRIVATE_IRQS
)
2109 vgic_bitmap_set_irq_val(&dist
->irq_cfg
,
2119 kvm_vgic_destroy(kvm
);
2124 static int init_vgic_model(struct kvm
*kvm
, int type
)
2127 case KVM_DEV_TYPE_ARM_VGIC_V2
:
2128 vgic_v2_init_emulation(kvm
);
2130 #ifdef CONFIG_ARM_GIC_V3
2131 case KVM_DEV_TYPE_ARM_VGIC_V3
:
2132 vgic_v3_init_emulation(kvm
);
2139 if (atomic_read(&kvm
->online_vcpus
) > kvm
->arch
.max_vcpus
)
2146 * kvm_vgic_early_init - Earliest possible vgic initialization stage
2148 * No memory allocation should be performed here, only static init.
2150 void kvm_vgic_early_init(struct kvm
*kvm
)
2152 spin_lock_init(&kvm
->arch
.vgic
.lock
);
2153 spin_lock_init(&kvm
->arch
.vgic
.irq_phys_map_lock
);
2154 INIT_LIST_HEAD(&kvm
->arch
.vgic
.irq_phys_map_list
);
2157 int kvm_vgic_create(struct kvm
*kvm
, u32 type
)
2159 int i
, vcpu_lock_idx
= -1, ret
;
2160 struct kvm_vcpu
*vcpu
;
2162 mutex_lock(&kvm
->lock
);
2164 if (irqchip_in_kernel(kvm
)) {
2170 * This function is also called by the KVM_CREATE_IRQCHIP handler,
2171 * which had no chance yet to check the availability of the GICv2
2172 * emulation. So check this here again. KVM_CREATE_DEVICE does
2173 * the proper checks already.
2175 if (type
== KVM_DEV_TYPE_ARM_VGIC_V2
&& !vgic
->can_emulate_gicv2
) {
2181 * Any time a vcpu is run, vcpu_load is called which tries to grab the
2182 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
2183 * that no other VCPUs are run while we create the vgic.
2186 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
2187 if (!mutex_trylock(&vcpu
->mutex
))
2192 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
2193 if (vcpu
->arch
.has_run_once
)
2198 ret
= init_vgic_model(kvm
, type
);
2202 kvm
->arch
.vgic
.in_kernel
= true;
2203 kvm
->arch
.vgic
.vgic_model
= type
;
2204 kvm
->arch
.vgic
.vctrl_base
= vgic
->vctrl_base
;
2205 kvm
->arch
.vgic
.vgic_dist_base
= VGIC_ADDR_UNDEF
;
2206 kvm
->arch
.vgic
.vgic_cpu_base
= VGIC_ADDR_UNDEF
;
2207 kvm
->arch
.vgic
.vgic_redist_base
= VGIC_ADDR_UNDEF
;
2210 for (; vcpu_lock_idx
>= 0; vcpu_lock_idx
--) {
2211 vcpu
= kvm_get_vcpu(kvm
, vcpu_lock_idx
);
2212 mutex_unlock(&vcpu
->mutex
);
2216 mutex_unlock(&kvm
->lock
);
2220 static int vgic_ioaddr_overlap(struct kvm
*kvm
)
2222 phys_addr_t dist
= kvm
->arch
.vgic
.vgic_dist_base
;
2223 phys_addr_t cpu
= kvm
->arch
.vgic
.vgic_cpu_base
;
2225 if (IS_VGIC_ADDR_UNDEF(dist
) || IS_VGIC_ADDR_UNDEF(cpu
))
2227 if ((dist
<= cpu
&& dist
+ KVM_VGIC_V2_DIST_SIZE
> cpu
) ||
2228 (cpu
<= dist
&& cpu
+ KVM_VGIC_V2_CPU_SIZE
> dist
))
2233 static int vgic_ioaddr_assign(struct kvm
*kvm
, phys_addr_t
*ioaddr
,
2234 phys_addr_t addr
, phys_addr_t size
)
2238 if (addr
& ~KVM_PHYS_MASK
)
2241 if (addr
& (SZ_4K
- 1))
2244 if (!IS_VGIC_ADDR_UNDEF(*ioaddr
))
2246 if (addr
+ size
< addr
)
2250 ret
= vgic_ioaddr_overlap(kvm
);
2252 *ioaddr
= VGIC_ADDR_UNDEF
;
2258 * kvm_vgic_addr - set or get vgic VM base addresses
2259 * @kvm: pointer to the vm struct
2260 * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
2261 * @addr: pointer to address value
2262 * @write: if true set the address in the VM address space, if false read the
2265 * Set or get the vgic base addresses for the distributor and the virtual CPU
2266 * interface in the VM physical address space. These addresses are properties
2267 * of the emulated core/SoC and therefore user space initially knows this
2270 int kvm_vgic_addr(struct kvm
*kvm
, unsigned long type
, u64
*addr
, bool write
)
2273 struct vgic_dist
*vgic
= &kvm
->arch
.vgic
;
2275 phys_addr_t
*addr_ptr
, block_size
;
2276 phys_addr_t alignment
;
2278 mutex_lock(&kvm
->lock
);
2280 case KVM_VGIC_V2_ADDR_TYPE_DIST
:
2281 type_needed
= KVM_DEV_TYPE_ARM_VGIC_V2
;
2282 addr_ptr
= &vgic
->vgic_dist_base
;
2283 block_size
= KVM_VGIC_V2_DIST_SIZE
;
2286 case KVM_VGIC_V2_ADDR_TYPE_CPU
:
2287 type_needed
= KVM_DEV_TYPE_ARM_VGIC_V2
;
2288 addr_ptr
= &vgic
->vgic_cpu_base
;
2289 block_size
= KVM_VGIC_V2_CPU_SIZE
;
2292 #ifdef CONFIG_ARM_GIC_V3
2293 case KVM_VGIC_V3_ADDR_TYPE_DIST
:
2294 type_needed
= KVM_DEV_TYPE_ARM_VGIC_V3
;
2295 addr_ptr
= &vgic
->vgic_dist_base
;
2296 block_size
= KVM_VGIC_V3_DIST_SIZE
;
2299 case KVM_VGIC_V3_ADDR_TYPE_REDIST
:
2300 type_needed
= KVM_DEV_TYPE_ARM_VGIC_V3
;
2301 addr_ptr
= &vgic
->vgic_redist_base
;
2302 block_size
= KVM_VGIC_V3_REDIST_SIZE
;
2311 if (vgic
->vgic_model
!= type_needed
) {
2317 if (!IS_ALIGNED(*addr
, alignment
))
2320 r
= vgic_ioaddr_assign(kvm
, addr_ptr
, *addr
,
2327 mutex_unlock(&kvm
->lock
);
2331 int vgic_set_common_attr(struct kvm_device
*dev
, struct kvm_device_attr
*attr
)
2335 switch (attr
->group
) {
2336 case KVM_DEV_ARM_VGIC_GRP_ADDR
: {
2337 u64 __user
*uaddr
= (u64 __user
*)(long)attr
->addr
;
2339 unsigned long type
= (unsigned long)attr
->attr
;
2341 if (copy_from_user(&addr
, uaddr
, sizeof(addr
)))
2344 r
= kvm_vgic_addr(dev
->kvm
, type
, &addr
, true);
2345 return (r
== -ENODEV
) ? -ENXIO
: r
;
2347 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS
: {
2348 u32 __user
*uaddr
= (u32 __user
*)(long)attr
->addr
;
2352 if (get_user(val
, uaddr
))
2357 * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
2358 * - at most 1024 interrupts
2359 * - a multiple of 32 interrupts
2361 if (val
< (VGIC_NR_PRIVATE_IRQS
+ 32) ||
2362 val
> VGIC_MAX_IRQS
||
2366 mutex_lock(&dev
->kvm
->lock
);
2368 if (vgic_ready(dev
->kvm
) || dev
->kvm
->arch
.vgic
.nr_irqs
)
2371 dev
->kvm
->arch
.vgic
.nr_irqs
= val
;
2373 mutex_unlock(&dev
->kvm
->lock
);
2377 case KVM_DEV_ARM_VGIC_GRP_CTRL
: {
2378 switch (attr
->attr
) {
2379 case KVM_DEV_ARM_VGIC_CTRL_INIT
:
2380 r
= vgic_init(dev
->kvm
);
2390 int vgic_get_common_attr(struct kvm_device
*dev
, struct kvm_device_attr
*attr
)
2394 switch (attr
->group
) {
2395 case KVM_DEV_ARM_VGIC_GRP_ADDR
: {
2396 u64 __user
*uaddr
= (u64 __user
*)(long)attr
->addr
;
2398 unsigned long type
= (unsigned long)attr
->attr
;
2400 r
= kvm_vgic_addr(dev
->kvm
, type
, &addr
, false);
2402 return (r
== -ENODEV
) ? -ENXIO
: r
;
2404 if (copy_to_user(uaddr
, &addr
, sizeof(addr
)))
2408 case KVM_DEV_ARM_VGIC_GRP_NR_IRQS
: {
2409 u32 __user
*uaddr
= (u32 __user
*)(long)attr
->addr
;
2411 r
= put_user(dev
->kvm
->arch
.vgic
.nr_irqs
, uaddr
);
2420 int vgic_has_attr_regs(const struct vgic_io_range
*ranges
, phys_addr_t offset
)
2422 if (vgic_find_range(ranges
, 4, offset
))
2428 static void vgic_init_maintenance_interrupt(void *info
)
2430 enable_percpu_irq(vgic
->maint_irq
, 0);
2433 static int vgic_cpu_notify(struct notifier_block
*self
,
2434 unsigned long action
, void *cpu
)
2438 case CPU_STARTING_FROZEN
:
2439 vgic_init_maintenance_interrupt(NULL
);
2442 case CPU_DYING_FROZEN
:
2443 disable_percpu_irq(vgic
->maint_irq
);
2450 static struct notifier_block vgic_cpu_nb
= {
2451 .notifier_call
= vgic_cpu_notify
,
2454 static const struct of_device_id vgic_ids
[] = {
2455 { .compatible
= "arm,cortex-a15-gic", .data
= vgic_v2_probe
, },
2456 { .compatible
= "arm,cortex-a7-gic", .data
= vgic_v2_probe
, },
2457 { .compatible
= "arm,gic-400", .data
= vgic_v2_probe
, },
2458 { .compatible
= "arm,gic-v3", .data
= vgic_v3_probe
, },
2462 int kvm_vgic_hyp_init(void)
2464 const struct of_device_id
*matched_id
;
2465 const int (*vgic_probe
)(struct device_node
*,const struct vgic_ops
**,
2466 const struct vgic_params
**);
2467 struct device_node
*vgic_node
;
2470 vgic_node
= of_find_matching_node_and_match(NULL
,
2471 vgic_ids
, &matched_id
);
2473 kvm_err("error: no compatible GIC node found\n");
2477 vgic_probe
= matched_id
->data
;
2478 ret
= vgic_probe(vgic_node
, &vgic_ops
, &vgic
);
2482 ret
= request_percpu_irq(vgic
->maint_irq
, vgic_maintenance_handler
,
2483 "vgic", kvm_get_running_vcpus());
2485 kvm_err("Cannot register interrupt %d\n", vgic
->maint_irq
);
2489 ret
= __register_cpu_notifier(&vgic_cpu_nb
);
2491 kvm_err("Cannot register vgic CPU notifier\n");
2495 on_each_cpu(vgic_init_maintenance_interrupt
, NULL
, 1);
2500 free_percpu_irq(vgic
->maint_irq
, kvm_get_running_vcpus());
2504 int kvm_irq_map_gsi(struct kvm
*kvm
,
2505 struct kvm_kernel_irq_routing_entry
*entries
,
2511 int kvm_irq_map_chip_pin(struct kvm
*kvm
, unsigned irqchip
, unsigned pin
)
2516 int kvm_set_irq(struct kvm
*kvm
, int irq_source_id
,
2517 u32 irq
, int level
, bool line_status
)
2519 unsigned int spi
= irq
+ VGIC_NR_PRIVATE_IRQS
;
2521 trace_kvm_set_irq(irq
, level
, irq_source_id
);
2523 BUG_ON(!vgic_initialized(kvm
));
2525 return kvm_vgic_inject_irq(kvm
, 0, spi
, level
);
2528 /* MSI not implemented yet */
2529 int kvm_set_msi(struct kvm_kernel_irq_routing_entry
*e
,
2530 struct kvm
*kvm
, int irq_source_id
,
2531 int level
, bool line_status
)