1 // SPDX-License-Identifier: GPL-2.0-only
3 * VGICv2 MMIO handling functions
6 #include <linux/irqchip/arm-gic.h>
8 #include <linux/kvm_host.h>
9 #include <linux/nospec.h>
11 #include <kvm/iodev.h>
12 #include <kvm/arm_vgic.h>
15 #include "vgic-mmio.h"
18 * The Revision field in the IIDR have the following meanings:
20 * Revision 1: Report GICv2 interrupts as group 0 instead of group 1
21 * Revision 2: Interrupt groups are guest-configurable and signaled using
22 * their configured groups.
25 static unsigned long vgic_mmio_read_v2_misc(struct kvm_vcpu *vcpu,
26 gpa_t addr, unsigned int len)
28 struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;
31 switch (addr & 0x0c) {
33 value = vgic->enabled ? GICD_ENABLE : 0;
36 value = vgic->nr_spis + VGIC_NR_PRIVATE_IRQS;
37 value = (value >> 5) - 1;
38 value |= (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
41 value = (PRODUCT_ID_KVM << GICD_IIDR_PRODUCT_ID_SHIFT) |
42 (vgic->implementation_rev << GICD_IIDR_REVISION_SHIFT) |
43 (IMPLEMENTER_ARM << GICD_IIDR_IMPLEMENTER_SHIFT);
52 static void vgic_mmio_write_v2_misc(struct kvm_vcpu *vcpu,
53 gpa_t addr, unsigned int len,
56 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
57 bool was_enabled = dist->enabled;
59 switch (addr & 0x0c) {
61 dist->enabled = val & GICD_ENABLE;
62 if (!was_enabled && dist->enabled)
63 vgic_kick_vcpus(vcpu->kvm);
72 static int vgic_mmio_uaccess_write_v2_misc(struct kvm_vcpu *vcpu,
73 gpa_t addr, unsigned int len,
76 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
79 switch (addr & 0x0c) {
81 reg = vgic_mmio_read_v2_misc(vcpu, addr, len);
82 if ((reg ^ val) & ~GICD_IIDR_REVISION_MASK)
86 * If we observe a write to GICD_IIDR we know that userspace
87 * has been updated and has had a chance to cope with older
88 * kernels (VGICv2 IIDR.Revision == 0) incorrectly reporting
89 * interrupts as group 1, and therefore we now allow groups to
90 * be user writable. Doing this by default would break
91 * migration from old kernels to new kernels with legacy
94 reg = FIELD_GET(GICD_IIDR_REVISION_MASK, reg);
96 case KVM_VGIC_IMP_REV_2:
97 case KVM_VGIC_IMP_REV_3:
98 vcpu->kvm->arch.vgic.v2_groups_user_writable = true;
99 dist->implementation_rev = reg;
106 vgic_mmio_write_v2_misc(vcpu, addr, len, val);
110 static int vgic_mmio_uaccess_write_v2_group(struct kvm_vcpu *vcpu,
111 gpa_t addr, unsigned int len,
114 if (vcpu->kvm->arch.vgic.v2_groups_user_writable)
115 vgic_mmio_write_group(vcpu, addr, len, val);
120 static void vgic_mmio_write_sgir(struct kvm_vcpu *source_vcpu,
121 gpa_t addr, unsigned int len,
124 int nr_vcpus = atomic_read(&source_vcpu->kvm->online_vcpus);
125 int intid = val & 0xf;
126 int targets = (val >> 16) & 0xff;
127 int mode = (val >> 24) & 0x03;
128 struct kvm_vcpu *vcpu;
129 unsigned long flags, c;
132 case 0x0: /* as specified by targets */
135 targets = (1U << nr_vcpus) - 1; /* all, ... */
136 targets &= ~(1U << source_vcpu->vcpu_id); /* but self */
138 case 0x2: /* this very vCPU only */
139 targets = (1U << source_vcpu->vcpu_id);
141 case 0x3: /* reserved */
145 kvm_for_each_vcpu(c, vcpu, source_vcpu->kvm) {
146 struct vgic_irq *irq;
148 if (!(targets & (1U << c)))
151 irq = vgic_get_irq(source_vcpu->kvm, vcpu, intid);
153 raw_spin_lock_irqsave(&irq->irq_lock, flags);
154 irq->pending_latch = true;
155 irq->source |= 1U << source_vcpu->vcpu_id;
157 vgic_queue_irq_unlock(source_vcpu->kvm, irq, flags);
158 vgic_put_irq(source_vcpu->kvm, irq);
162 static unsigned long vgic_mmio_read_target(struct kvm_vcpu *vcpu,
163 gpa_t addr, unsigned int len)
165 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
169 for (i = 0; i < len; i++) {
170 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
172 val |= (u64)irq->targets << (i * 8);
174 vgic_put_irq(vcpu->kvm, irq);
180 static void vgic_mmio_write_target(struct kvm_vcpu *vcpu,
181 gpa_t addr, unsigned int len,
184 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
185 u8 cpu_mask = GENMASK(atomic_read(&vcpu->kvm->online_vcpus) - 1, 0);
189 /* GICD_ITARGETSR[0-7] are read-only */
190 if (intid < VGIC_NR_PRIVATE_IRQS)
193 for (i = 0; i < len; i++) {
194 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, NULL, intid + i);
197 raw_spin_lock_irqsave(&irq->irq_lock, flags);
199 irq->targets = (val >> (i * 8)) & cpu_mask;
200 target = irq->targets ? __ffs(irq->targets) : 0;
201 irq->target_vcpu = kvm_get_vcpu(vcpu->kvm, target);
203 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
204 vgic_put_irq(vcpu->kvm, irq);
208 static unsigned long vgic_mmio_read_sgipend(struct kvm_vcpu *vcpu,
209 gpa_t addr, unsigned int len)
211 u32 intid = addr & 0x0f;
215 for (i = 0; i < len; i++) {
216 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
218 val |= (u64)irq->source << (i * 8);
220 vgic_put_irq(vcpu->kvm, irq);
225 static void vgic_mmio_write_sgipendc(struct kvm_vcpu *vcpu,
226 gpa_t addr, unsigned int len,
229 u32 intid = addr & 0x0f;
233 for (i = 0; i < len; i++) {
234 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
236 raw_spin_lock_irqsave(&irq->irq_lock, flags);
238 irq->source &= ~((val >> (i * 8)) & 0xff);
240 irq->pending_latch = false;
242 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
243 vgic_put_irq(vcpu->kvm, irq);
247 static void vgic_mmio_write_sgipends(struct kvm_vcpu *vcpu,
248 gpa_t addr, unsigned int len,
251 u32 intid = addr & 0x0f;
255 for (i = 0; i < len; i++) {
256 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
258 raw_spin_lock_irqsave(&irq->irq_lock, flags);
260 irq->source |= (val >> (i * 8)) & 0xff;
263 irq->pending_latch = true;
264 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
266 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
268 vgic_put_irq(vcpu->kvm, irq);
272 #define GICC_ARCH_VERSION_V2 0x2
274 /* These are for userland accesses only, there is no guest-facing emulation. */
275 static unsigned long vgic_mmio_read_vcpuif(struct kvm_vcpu *vcpu,
276 gpa_t addr, unsigned int len)
278 struct vgic_vmcr vmcr;
281 vgic_get_vmcr(vcpu, &vmcr);
283 switch (addr & 0xff) {
285 val = vmcr.grpen0 << GIC_CPU_CTRL_EnableGrp0_SHIFT;
286 val |= vmcr.grpen1 << GIC_CPU_CTRL_EnableGrp1_SHIFT;
287 val |= vmcr.ackctl << GIC_CPU_CTRL_AckCtl_SHIFT;
288 val |= vmcr.fiqen << GIC_CPU_CTRL_FIQEn_SHIFT;
289 val |= vmcr.cbpr << GIC_CPU_CTRL_CBPR_SHIFT;
290 val |= vmcr.eoim << GIC_CPU_CTRL_EOImodeNS_SHIFT;
293 case GIC_CPU_PRIMASK:
295 * Our KVM_DEV_TYPE_ARM_VGIC_V2 device ABI exports the
296 * PMR field as GICH_VMCR.VMPriMask rather than
297 * GICC_PMR.Priority, so we expose the upper five bits of
298 * priority mask to userspace using the lower bits in the
301 val = (vmcr.pmr & GICV_PMR_PRIORITY_MASK) >>
302 GICV_PMR_PRIORITY_SHIFT;
304 case GIC_CPU_BINPOINT:
307 case GIC_CPU_ALIAS_BINPOINT:
311 val = ((PRODUCT_ID_KVM << 20) |
312 (GICC_ARCH_VERSION_V2 << 16) |
322 static void vgic_mmio_write_vcpuif(struct kvm_vcpu *vcpu,
323 gpa_t addr, unsigned int len,
326 struct vgic_vmcr vmcr;
328 vgic_get_vmcr(vcpu, &vmcr);
330 switch (addr & 0xff) {
332 vmcr.grpen0 = !!(val & GIC_CPU_CTRL_EnableGrp0);
333 vmcr.grpen1 = !!(val & GIC_CPU_CTRL_EnableGrp1);
334 vmcr.ackctl = !!(val & GIC_CPU_CTRL_AckCtl);
335 vmcr.fiqen = !!(val & GIC_CPU_CTRL_FIQEn);
336 vmcr.cbpr = !!(val & GIC_CPU_CTRL_CBPR);
337 vmcr.eoim = !!(val & GIC_CPU_CTRL_EOImodeNS);
340 case GIC_CPU_PRIMASK:
342 * Our KVM_DEV_TYPE_ARM_VGIC_V2 device ABI exports the
343 * PMR field as GICH_VMCR.VMPriMask rather than
344 * GICC_PMR.Priority, so we expose the upper five bits of
345 * priority mask to userspace using the lower bits in the
348 vmcr.pmr = (val << GICV_PMR_PRIORITY_SHIFT) &
349 GICV_PMR_PRIORITY_MASK;
351 case GIC_CPU_BINPOINT:
354 case GIC_CPU_ALIAS_BINPOINT:
359 vgic_set_vmcr(vcpu, &vmcr);
362 static unsigned long vgic_mmio_read_apr(struct kvm_vcpu *vcpu,
363 gpa_t addr, unsigned int len)
365 int n; /* which APRn is this */
367 n = (addr >> 2) & 0x3;
369 if (kvm_vgic_global_state.type == VGIC_V2) {
370 /* GICv2 hardware systems support max. 32 groups */
373 return vcpu->arch.vgic_cpu.vgic_v2.vgic_apr;
375 struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
377 if (n > vgic_v3_max_apr_idx(vcpu))
380 n = array_index_nospec(n, 4);
382 /* GICv3 only uses ICH_AP1Rn for memory mapped (GICv2) guests */
383 return vgicv3->vgic_ap1r[n];
387 static void vgic_mmio_write_apr(struct kvm_vcpu *vcpu,
388 gpa_t addr, unsigned int len,
391 int n; /* which APRn is this */
393 n = (addr >> 2) & 0x3;
395 if (kvm_vgic_global_state.type == VGIC_V2) {
396 /* GICv2 hardware systems support max. 32 groups */
399 vcpu->arch.vgic_cpu.vgic_v2.vgic_apr = val;
401 struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
403 if (n > vgic_v3_max_apr_idx(vcpu))
406 n = array_index_nospec(n, 4);
408 /* GICv3 only uses ICH_AP1Rn for memory mapped (GICv2) guests */
409 vgicv3->vgic_ap1r[n] = val;
413 static const struct vgic_register_region vgic_v2_dist_registers[] = {
414 REGISTER_DESC_WITH_LENGTH_UACCESS(GIC_DIST_CTRL,
415 vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc,
416 NULL, vgic_mmio_uaccess_write_v2_misc,
417 12, VGIC_ACCESS_32bit),
418 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_IGROUP,
419 vgic_mmio_read_group, vgic_mmio_write_group,
420 NULL, vgic_mmio_uaccess_write_v2_group, 1,
422 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_SET,
423 vgic_mmio_read_enable, vgic_mmio_write_senable,
424 NULL, vgic_uaccess_write_senable, 1,
426 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_CLEAR,
427 vgic_mmio_read_enable, vgic_mmio_write_cenable,
428 NULL, vgic_uaccess_write_cenable, 1,
430 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_SET,
431 vgic_mmio_read_pending, vgic_mmio_write_spending,
432 NULL, vgic_uaccess_write_spending, 1,
434 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_CLEAR,
435 vgic_mmio_read_pending, vgic_mmio_write_cpending,
436 NULL, vgic_uaccess_write_cpending, 1,
438 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_SET,
439 vgic_mmio_read_active, vgic_mmio_write_sactive,
440 vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive, 1,
442 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_CLEAR,
443 vgic_mmio_read_active, vgic_mmio_write_cactive,
444 vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive, 1,
446 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI,
447 vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,
448 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
449 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_TARGET,
450 vgic_mmio_read_target, vgic_mmio_write_target, NULL, NULL, 8,
451 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
452 REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_CONFIG,
453 vgic_mmio_read_config, vgic_mmio_write_config, NULL, NULL, 2,
455 REGISTER_DESC_WITH_LENGTH(GIC_DIST_SOFTINT,
456 vgic_mmio_read_raz, vgic_mmio_write_sgir, 4,
458 REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_CLEAR,
459 vgic_mmio_read_sgipend, vgic_mmio_write_sgipendc, 16,
460 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
461 REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_SET,
462 vgic_mmio_read_sgipend, vgic_mmio_write_sgipends, 16,
463 VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
466 static const struct vgic_register_region vgic_v2_cpu_registers[] = {
467 REGISTER_DESC_WITH_LENGTH(GIC_CPU_CTRL,
468 vgic_mmio_read_vcpuif, vgic_mmio_write_vcpuif, 4,
470 REGISTER_DESC_WITH_LENGTH(GIC_CPU_PRIMASK,
471 vgic_mmio_read_vcpuif, vgic_mmio_write_vcpuif, 4,
473 REGISTER_DESC_WITH_LENGTH(GIC_CPU_BINPOINT,
474 vgic_mmio_read_vcpuif, vgic_mmio_write_vcpuif, 4,
476 REGISTER_DESC_WITH_LENGTH(GIC_CPU_ALIAS_BINPOINT,
477 vgic_mmio_read_vcpuif, vgic_mmio_write_vcpuif, 4,
479 REGISTER_DESC_WITH_LENGTH(GIC_CPU_ACTIVEPRIO,
480 vgic_mmio_read_apr, vgic_mmio_write_apr, 16,
482 REGISTER_DESC_WITH_LENGTH(GIC_CPU_IDENT,
483 vgic_mmio_read_vcpuif, vgic_mmio_write_vcpuif, 4,
487 unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev)
489 dev->regions = vgic_v2_dist_registers;
490 dev->nr_regions = ARRAY_SIZE(vgic_v2_dist_registers);
492 kvm_iodevice_init(&dev->dev, &kvm_io_gic_ops);
497 int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
499 const struct vgic_register_region *region;
500 struct vgic_io_device iodev;
501 struct vgic_reg_attr reg_attr;
502 struct kvm_vcpu *vcpu;
506 ret = vgic_v2_parse_attr(dev, attr, ®_attr);
510 vcpu = reg_attr.vcpu;
511 addr = reg_attr.addr;
513 switch (attr->group) {
514 case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
515 iodev.regions = vgic_v2_dist_registers;
516 iodev.nr_regions = ARRAY_SIZE(vgic_v2_dist_registers);
519 case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
520 iodev.regions = vgic_v2_cpu_registers;
521 iodev.nr_regions = ARRAY_SIZE(vgic_v2_cpu_registers);
528 /* We only support aligned 32-bit accesses. */
532 region = vgic_get_mmio_region(vcpu, &iodev, addr, sizeof(u32));
539 int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write,
540 int offset, u32 *val)
542 struct vgic_io_device dev = {
543 .regions = vgic_v2_cpu_registers,
544 .nr_regions = ARRAY_SIZE(vgic_v2_cpu_registers),
545 .iodev_type = IODEV_CPUIF,
548 return vgic_uaccess(vcpu, &dev, is_write, offset, val);
551 int vgic_v2_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
552 int offset, u32 *val)
554 struct vgic_io_device dev = {
555 .regions = vgic_v2_dist_registers,
556 .nr_regions = ARRAY_SIZE(vgic_v2_dist_registers),
557 .iodev_type = IODEV_DIST,
560 return vgic_uaccess(vcpu, &dev, is_write, offset, val);