2 * VGICv2 MMIO handling functions
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/irqchip/arm-gic.h>
15 #include <linux/kvm.h>
16 #include <linux/kvm_host.h>
17 #include <kvm/iodev.h>
18 #include <kvm/arm_vgic.h>
21 #include "vgic-mmio.h"
23 static unsigned long vgic_mmio_read_v2_misc(struct kvm_vcpu
*vcpu
,
24 gpa_t addr
, unsigned int len
)
28 switch (addr
& 0x0c) {
30 value
= vcpu
->kvm
->arch
.vgic
.enabled
? GICD_ENABLE
: 0;
33 value
= vcpu
->kvm
->arch
.vgic
.nr_spis
+ VGIC_NR_PRIVATE_IRQS
;
34 value
= (value
>> 5) - 1;
35 value
|= (atomic_read(&vcpu
->kvm
->online_vcpus
) - 1) << 5;
38 value
= (PRODUCT_ID_KVM
<< 24) | (IMPLEMENTER_ARM
<< 0);
47 static void vgic_mmio_write_v2_misc(struct kvm_vcpu
*vcpu
,
48 gpa_t addr
, unsigned int len
,
51 struct vgic_dist
*dist
= &vcpu
->kvm
->arch
.vgic
;
52 bool was_enabled
= dist
->enabled
;
54 switch (addr
& 0x0c) {
56 dist
->enabled
= val
& GICD_ENABLE
;
57 if (!was_enabled
&& dist
->enabled
)
58 vgic_kick_vcpus(vcpu
->kvm
);
67 static void vgic_mmio_write_sgir(struct kvm_vcpu
*source_vcpu
,
68 gpa_t addr
, unsigned int len
,
71 int nr_vcpus
= atomic_read(&source_vcpu
->kvm
->online_vcpus
);
72 int intid
= val
& 0xf;
73 int targets
= (val
>> 16) & 0xff;
74 int mode
= (val
>> 24) & 0x03;
76 struct kvm_vcpu
*vcpu
;
79 case 0x0: /* as specified by targets */
82 targets
= (1U << nr_vcpus
) - 1; /* all, ... */
83 targets
&= ~(1U << source_vcpu
->vcpu_id
); /* but self */
85 case 0x2: /* this very vCPU only */
86 targets
= (1U << source_vcpu
->vcpu_id
);
88 case 0x3: /* reserved */
92 kvm_for_each_vcpu(c
, vcpu
, source_vcpu
->kvm
) {
95 if (!(targets
& (1U << c
)))
98 irq
= vgic_get_irq(source_vcpu
->kvm
, vcpu
, intid
);
100 spin_lock(&irq
->irq_lock
);
102 irq
->source
|= 1U << source_vcpu
->vcpu_id
;
104 vgic_queue_irq_unlock(source_vcpu
->kvm
, irq
);
108 static unsigned long vgic_mmio_read_target(struct kvm_vcpu
*vcpu
,
109 gpa_t addr
, unsigned int len
)
111 u32 intid
= VGIC_ADDR_TO_INTID(addr
, 8);
115 for (i
= 0; i
< len
; i
++) {
116 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, intid
+ i
);
118 val
|= (u64
)irq
->targets
<< (i
* 8);
124 static void vgic_mmio_write_target(struct kvm_vcpu
*vcpu
,
125 gpa_t addr
, unsigned int len
,
128 u32 intid
= VGIC_ADDR_TO_INTID(addr
, 8);
131 /* GICD_ITARGETSR[0-7] are read-only */
132 if (intid
< VGIC_NR_PRIVATE_IRQS
)
135 for (i
= 0; i
< len
; i
++) {
136 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, NULL
, intid
+ i
);
139 spin_lock(&irq
->irq_lock
);
141 irq
->targets
= (val
>> (i
* 8)) & 0xff;
142 target
= irq
->targets
? __ffs(irq
->targets
) : 0;
143 irq
->target_vcpu
= kvm_get_vcpu(vcpu
->kvm
, target
);
145 spin_unlock(&irq
->irq_lock
);
149 static unsigned long vgic_mmio_read_sgipend(struct kvm_vcpu
*vcpu
,
150 gpa_t addr
, unsigned int len
)
152 u32 intid
= addr
& 0x0f;
156 for (i
= 0; i
< len
; i
++) {
157 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, intid
+ i
);
159 val
|= (u64
)irq
->source
<< (i
* 8);
164 static void vgic_mmio_write_sgipendc(struct kvm_vcpu
*vcpu
,
165 gpa_t addr
, unsigned int len
,
168 u32 intid
= addr
& 0x0f;
171 for (i
= 0; i
< len
; i
++) {
172 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, intid
+ i
);
174 spin_lock(&irq
->irq_lock
);
176 irq
->source
&= ~((val
>> (i
* 8)) & 0xff);
178 irq
->pending
= false;
180 spin_unlock(&irq
->irq_lock
);
184 static void vgic_mmio_write_sgipends(struct kvm_vcpu
*vcpu
,
185 gpa_t addr
, unsigned int len
,
188 u32 intid
= addr
& 0x0f;
191 for (i
= 0; i
< len
; i
++) {
192 struct vgic_irq
*irq
= vgic_get_irq(vcpu
->kvm
, vcpu
, intid
+ i
);
194 spin_lock(&irq
->irq_lock
);
196 irq
->source
|= (val
>> (i
* 8)) & 0xff;
200 vgic_queue_irq_unlock(vcpu
->kvm
, irq
);
202 spin_unlock(&irq
->irq_lock
);
207 static const struct vgic_register_region vgic_v2_dist_registers
[] = {
208 REGISTER_DESC_WITH_LENGTH(GIC_DIST_CTRL
,
209 vgic_mmio_read_v2_misc
, vgic_mmio_write_v2_misc
, 12,
211 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_IGROUP
,
212 vgic_mmio_read_rao
, vgic_mmio_write_wi
, 1,
214 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_SET
,
215 vgic_mmio_read_enable
, vgic_mmio_write_senable
, 1,
217 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_CLEAR
,
218 vgic_mmio_read_enable
, vgic_mmio_write_cenable
, 1,
220 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_SET
,
221 vgic_mmio_read_pending
, vgic_mmio_write_spending
, 1,
223 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_CLEAR
,
224 vgic_mmio_read_pending
, vgic_mmio_write_cpending
, 1,
226 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_SET
,
227 vgic_mmio_read_active
, vgic_mmio_write_sactive
, 1,
229 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_CLEAR
,
230 vgic_mmio_read_active
, vgic_mmio_write_cactive
, 1,
232 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI
,
233 vgic_mmio_read_priority
, vgic_mmio_write_priority
, 8,
234 VGIC_ACCESS_32bit
| VGIC_ACCESS_8bit
),
235 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_TARGET
,
236 vgic_mmio_read_target
, vgic_mmio_write_target
, 8,
237 VGIC_ACCESS_32bit
| VGIC_ACCESS_8bit
),
238 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_CONFIG
,
239 vgic_mmio_read_config
, vgic_mmio_write_config
, 2,
241 REGISTER_DESC_WITH_LENGTH(GIC_DIST_SOFTINT
,
242 vgic_mmio_read_raz
, vgic_mmio_write_sgir
, 4,
244 REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_CLEAR
,
245 vgic_mmio_read_sgipend
, vgic_mmio_write_sgipendc
, 16,
246 VGIC_ACCESS_32bit
| VGIC_ACCESS_8bit
),
247 REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_SET
,
248 vgic_mmio_read_sgipend
, vgic_mmio_write_sgipends
, 16,
249 VGIC_ACCESS_32bit
| VGIC_ACCESS_8bit
),
252 unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device
*dev
)
254 dev
->regions
= vgic_v2_dist_registers
;
255 dev
->nr_regions
= ARRAY_SIZE(vgic_v2_dist_registers
);
257 kvm_iodevice_init(&dev
->dev
, &kvm_io_gic_ops
);