2 * ARM Generic Interrupt Controller using KVM in-kernel support
4 * Copyright (c) 2012 Linaro Limited
5 * Written by Peter Maydell
6 * Save/Restore logic added by Christoffer Dall.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "hw/sysbus.h"
23 #include "sysemu/kvm.h"
25 #include "gic_internal.h"
27 //#define DEBUG_GIC_KVM
30 static const int debug_gic_kvm
= 1;
32 static const int debug_gic_kvm
= 0;
35 #define DPRINTF(fmt, ...) do { \
36 if (debug_gic_kvm) { \
37 printf("arm_gic: " fmt , ## __VA_ARGS__); \
41 #define TYPE_KVM_ARM_GIC "kvm-arm-gic"
42 #define KVM_ARM_GIC(obj) \
43 OBJECT_CHECK(GICState, (obj), TYPE_KVM_ARM_GIC)
44 #define KVM_ARM_GIC_CLASS(klass) \
45 OBJECT_CLASS_CHECK(KVMARMGICClass, (klass), TYPE_KVM_ARM_GIC)
46 #define KVM_ARM_GIC_GET_CLASS(obj) \
47 OBJECT_GET_CLASS(KVMARMGICClass, (obj), TYPE_KVM_ARM_GIC)
49 typedef struct KVMARMGICClass
{
50 ARMGICCommonClass parent_class
;
51 DeviceRealize parent_realize
;
52 void (*parent_reset
)(DeviceState
*dev
);
55 static void kvm_arm_gic_set_irq(void *opaque
, int irq
, int level
)
57 /* Meaning of the 'irq' parameter:
58 * [0..N-1] : external interrupts
59 * [N..N+31] : PPI (internal) interrupts for CPU 0
60 * [N+32..N+63] : PPI (internal interrupts for CPU 1
62 * Convert this to the kernel's desired encoding, which
63 * has separate fields in the irq number for type,
64 * CPU number and interrupt number.
66 GICState
*s
= (GICState
*)opaque
;
67 int kvm_irq
, irqtype
, cpu
;
69 if (irq
< (s
->num_irq
- GIC_INTERNAL
)) {
70 /* External interrupt. The kernel numbers these like the GIC
71 * hardware, with external interrupt IDs starting after the
74 irqtype
= KVM_ARM_IRQ_TYPE_SPI
;
78 /* Internal interrupt: decode into (cpu, interrupt id) */
79 irqtype
= KVM_ARM_IRQ_TYPE_PPI
;
80 irq
-= (s
->num_irq
- GIC_INTERNAL
);
81 cpu
= irq
/ GIC_INTERNAL
;
84 kvm_irq
= (irqtype
<< KVM_ARM_IRQ_TYPE_SHIFT
)
85 | (cpu
<< KVM_ARM_IRQ_VCPU_SHIFT
) | irq
;
87 kvm_set_irq(kvm_state
, kvm_irq
, !!level
);
90 static bool kvm_arm_gic_can_save_restore(GICState
*s
)
92 return s
->dev_fd
>= 0;
95 static bool kvm_gic_supports_attr(GICState
*s
, int group
, int attrnum
)
97 struct kvm_device_attr attr
= {
103 if (s
->dev_fd
== -1) {
107 return kvm_device_ioctl(s
->dev_fd
, KVM_HAS_DEVICE_ATTR
, &attr
) == 0;
110 static void kvm_gic_access(GICState
*s
, int group
, int offset
,
111 int cpu
, uint32_t *val
, bool write
)
113 struct kvm_device_attr attr
;
121 attr
.attr
= (((uint64_t)cpu
<< KVM_DEV_ARM_VGIC_CPUID_SHIFT
) &
122 KVM_DEV_ARM_VGIC_CPUID_MASK
) |
123 (((uint64_t)offset
<< KVM_DEV_ARM_VGIC_OFFSET_SHIFT
) &
124 KVM_DEV_ARM_VGIC_OFFSET_MASK
);
125 attr
.addr
= (uintptr_t)val
;
128 type
= KVM_SET_DEVICE_ATTR
;
130 type
= KVM_GET_DEVICE_ATTR
;
133 err
= kvm_device_ioctl(s
->dev_fd
, type
, &attr
);
135 fprintf(stderr
, "KVM_{SET/GET}_DEVICE_ATTR failed: %s\n",
141 static void kvm_gicd_access(GICState
*s
, int offset
, int cpu
,
142 uint32_t *val
, bool write
)
144 kvm_gic_access(s
, KVM_DEV_ARM_VGIC_GRP_DIST_REGS
,
145 offset
, cpu
, val
, write
);
148 static void kvm_gicc_access(GICState
*s
, int offset
, int cpu
,
149 uint32_t *val
, bool write
)
151 kvm_gic_access(s
, KVM_DEV_ARM_VGIC_GRP_CPU_REGS
,
152 offset
, cpu
, val
, write
);
155 #define for_each_irq_reg(_ctr, _max_irq, _field_width) \
156 for (_ctr = 0; _ctr < ((_max_irq) / (32 / (_field_width))); _ctr++)
159 * Translate from the in-kernel field for an IRQ value to/from the qemu
162 typedef void (*vgic_translate_fn
)(GICState
*s
, int irq
, int cpu
,
163 uint32_t *field
, bool to_kernel
);
165 /* synthetic translate function used for clear/set registers to completely
166 * clear a setting using a clear-register before setting the remaining bits
167 * using a set-register */
168 static void translate_clear(GICState
*s
, int irq
, int cpu
,
169 uint32_t *field
, bool to_kernel
)
174 /* does not make sense: qemu model doesn't use set/clear regs */
179 static void translate_group(GICState
*s
, int irq
, int cpu
,
180 uint32_t *field
, bool to_kernel
)
182 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
185 *field
= GIC_TEST_GROUP(irq
, cm
);
188 GIC_SET_GROUP(irq
, cm
);
193 static void translate_enabled(GICState
*s
, int irq
, int cpu
,
194 uint32_t *field
, bool to_kernel
)
196 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
199 *field
= GIC_TEST_ENABLED(irq
, cm
);
202 GIC_SET_ENABLED(irq
, cm
);
207 static void translate_pending(GICState
*s
, int irq
, int cpu
,
208 uint32_t *field
, bool to_kernel
)
210 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
213 *field
= gic_test_pending(s
, irq
, cm
);
216 GIC_SET_PENDING(irq
, cm
);
217 /* TODO: Capture is level-line is held high in the kernel */
222 static void translate_active(GICState
*s
, int irq
, int cpu
,
223 uint32_t *field
, bool to_kernel
)
225 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
228 *field
= GIC_TEST_ACTIVE(irq
, cm
);
231 GIC_SET_ACTIVE(irq
, cm
);
236 static void translate_trigger(GICState
*s
, int irq
, int cpu
,
237 uint32_t *field
, bool to_kernel
)
240 *field
= (GIC_TEST_EDGE_TRIGGER(irq
)) ? 0x2 : 0x0;
243 GIC_SET_EDGE_TRIGGER(irq
);
248 static void translate_priority(GICState
*s
, int irq
, int cpu
,
249 uint32_t *field
, bool to_kernel
)
252 *field
= GIC_GET_PRIORITY(irq
, cpu
) & 0xff;
254 gic_set_priority(s
, cpu
, irq
, *field
& 0xff, MEMTXATTRS_UNSPECIFIED
);
258 static void translate_targets(GICState
*s
, int irq
, int cpu
,
259 uint32_t *field
, bool to_kernel
)
262 *field
= s
->irq_target
[irq
] & 0xff;
264 s
->irq_target
[irq
] = *field
& 0xff;
268 static void translate_sgisource(GICState
*s
, int irq
, int cpu
,
269 uint32_t *field
, bool to_kernel
)
272 *field
= s
->sgi_pending
[irq
][cpu
] & 0xff;
274 s
->sgi_pending
[irq
][cpu
] = *field
& 0xff;
278 /* Read a register group from the kernel VGIC */
279 static void kvm_dist_get(GICState
*s
, uint32_t offset
, int width
,
280 int maxirq
, vgic_translate_fn translate_fn
)
287 int regsz
= 32 / width
; /* irqs per kernel register */
290 for_each_irq_reg(i
, maxirq
, width
) {
293 while ((cpu
< s
->num_cpu
&& irq
< GIC_INTERNAL
) || cpu
== 0) {
294 kvm_gicd_access(s
, offset
, cpu
, ®
, false);
295 for (j
= 0; j
< regsz
; j
++) {
296 field
= extract32(reg
, j
* width
, width
);
297 translate_fn(s
, irq
+ j
, cpu
, &field
, false);
306 /* Write a register group to the kernel VGIC */
307 static void kvm_dist_put(GICState
*s
, uint32_t offset
, int width
,
308 int maxirq
, vgic_translate_fn translate_fn
)
315 int regsz
= 32 / width
; /* irqs per kernel register */
318 for_each_irq_reg(i
, maxirq
, width
) {
321 while ((cpu
< s
->num_cpu
&& irq
< GIC_INTERNAL
) || cpu
== 0) {
323 for (j
= 0; j
< regsz
; j
++) {
324 translate_fn(s
, irq
+ j
, cpu
, &field
, true);
325 reg
= deposit32(reg
, j
* width
, width
, field
);
327 kvm_gicd_access(s
, offset
, cpu
, ®
, true);
335 static void kvm_arm_gic_put(GICState
*s
)
343 if (!kvm_arm_gic_can_save_restore(s
)) {
344 DPRINTF("Cannot put kernel gic state, no kernel interface");
348 /* Note: We do the restore in a slightly different order than the save
349 * (where the order doesn't matter and is simply ordered according to the
350 * register offset values */
352 /*****************************************************************
356 /* s->ctlr -> GICD_CTLR */
358 kvm_gicd_access(s
, 0x0, 0, ®
, true);
360 /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
361 kvm_gicd_access(s
, 0x4, 0, ®
, false);
362 num_irq
= ((reg
& 0x1f) + 1) * 32;
363 num_cpu
= ((reg
& 0xe0) >> 5) + 1;
365 if (num_irq
< s
->num_irq
) {
366 fprintf(stderr
, "Restoring %u IRQs, but kernel supports max %d\n",
367 s
->num_irq
, num_irq
);
369 } else if (num_cpu
!= s
->num_cpu
) {
370 fprintf(stderr
, "Restoring %u CPU interfaces, kernel only has %d\n",
371 s
->num_cpu
, num_cpu
);
372 /* Did we not create the VCPUs in the kernel yet? */
376 /* TODO: Consider checking compatibility with the IIDR ? */
378 /* irq_state[n].enabled -> GICD_ISENABLERn */
379 kvm_dist_put(s
, 0x180, 1, s
->num_irq
, translate_clear
);
380 kvm_dist_put(s
, 0x100, 1, s
->num_irq
, translate_enabled
);
382 /* irq_state[n].group -> GICD_IGROUPRn */
383 kvm_dist_put(s
, 0x80, 1, s
->num_irq
, translate_group
);
385 /* s->irq_target[irq] -> GICD_ITARGETSRn
386 * (restore targets before pending to ensure the pending state is set on
387 * the appropriate CPU interfaces in the kernel) */
388 kvm_dist_put(s
, 0x800, 8, s
->num_irq
, translate_targets
);
390 /* irq_state[n].trigger -> GICD_ICFGRn
391 * (restore configuration registers before pending IRQs so we treat
392 * level/edge correctly) */
393 kvm_dist_put(s
, 0xc00, 2, s
->num_irq
, translate_trigger
);
395 /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
396 kvm_dist_put(s
, 0x280, 1, s
->num_irq
, translate_clear
);
397 kvm_dist_put(s
, 0x200, 1, s
->num_irq
, translate_pending
);
399 /* irq_state[n].active -> GICD_ISACTIVERn */
400 kvm_dist_put(s
, 0x380, 1, s
->num_irq
, translate_clear
);
401 kvm_dist_put(s
, 0x300, 1, s
->num_irq
, translate_active
);
404 /* s->priorityX[irq] -> ICD_IPRIORITYRn */
405 kvm_dist_put(s
, 0x400, 8, s
->num_irq
, translate_priority
);
407 /* s->sgi_pending -> ICD_CPENDSGIRn */
408 kvm_dist_put(s
, 0xf10, 8, GIC_NR_SGIS
, translate_clear
);
409 kvm_dist_put(s
, 0xf20, 8, GIC_NR_SGIS
, translate_sgisource
);
412 /*****************************************************************
413 * CPU Interface(s) State
416 for (cpu
= 0; cpu
< s
->num_cpu
; cpu
++) {
417 /* s->cpu_ctlr[cpu] -> GICC_CTLR */
418 reg
= s
->cpu_ctlr
[cpu
];
419 kvm_gicc_access(s
, 0x00, cpu
, ®
, true);
421 /* s->priority_mask[cpu] -> GICC_PMR */
422 reg
= (s
->priority_mask
[cpu
] & 0xff);
423 kvm_gicc_access(s
, 0x04, cpu
, ®
, true);
425 /* s->bpr[cpu] -> GICC_BPR */
426 reg
= (s
->bpr
[cpu
] & 0x7);
427 kvm_gicc_access(s
, 0x08, cpu
, ®
, true);
429 /* s->abpr[cpu] -> GICC_ABPR */
430 reg
= (s
->abpr
[cpu
] & 0x7);
431 kvm_gicc_access(s
, 0x1c, cpu
, ®
, true);
433 /* s->apr[n][cpu] -> GICC_APRn */
434 for (i
= 0; i
< 4; i
++) {
435 reg
= s
->apr
[i
][cpu
];
436 kvm_gicc_access(s
, 0xd0 + i
* 4, cpu
, ®
, true);
441 static void kvm_arm_gic_get(GICState
*s
)
447 if (!kvm_arm_gic_can_save_restore(s
)) {
448 DPRINTF("Cannot get kernel gic state, no kernel interface");
452 /*****************************************************************
456 /* GICD_CTLR -> s->ctlr */
457 kvm_gicd_access(s
, 0x0, 0, ®
, false);
460 /* Sanity checking on GICD_TYPER -> s->num_irq, s->num_cpu */
461 kvm_gicd_access(s
, 0x4, 0, ®
, false);
462 s
->num_irq
= ((reg
& 0x1f) + 1) * 32;
463 s
->num_cpu
= ((reg
& 0xe0) >> 5) + 1;
465 if (s
->num_irq
> GIC_MAXIRQ
) {
466 fprintf(stderr
, "Too many IRQs reported from the kernel: %d\n",
472 kvm_gicd_access(s
, 0x8, 0, ®
, false);
474 /* Clear all the IRQ settings */
475 for (i
= 0; i
< s
->num_irq
; i
++) {
476 memset(&s
->irq_state
[i
], 0, sizeof(s
->irq_state
[0]));
479 /* GICD_IGROUPRn -> irq_state[n].group */
480 kvm_dist_get(s
, 0x80, 1, s
->num_irq
, translate_group
);
482 /* GICD_ISENABLERn -> irq_state[n].enabled */
483 kvm_dist_get(s
, 0x100, 1, s
->num_irq
, translate_enabled
);
485 /* GICD_ISPENDRn -> irq_state[n].pending + irq_state[n].level */
486 kvm_dist_get(s
, 0x200, 1, s
->num_irq
, translate_pending
);
488 /* GICD_ISACTIVERn -> irq_state[n].active */
489 kvm_dist_get(s
, 0x300, 1, s
->num_irq
, translate_active
);
491 /* GICD_ICFRn -> irq_state[n].trigger */
492 kvm_dist_get(s
, 0xc00, 2, s
->num_irq
, translate_trigger
);
494 /* GICD_IPRIORITYRn -> s->priorityX[irq] */
495 kvm_dist_get(s
, 0x400, 8, s
->num_irq
, translate_priority
);
497 /* GICD_ITARGETSRn -> s->irq_target[irq] */
498 kvm_dist_get(s
, 0x800, 8, s
->num_irq
, translate_targets
);
500 /* GICD_CPENDSGIRn -> s->sgi_pending */
501 kvm_dist_get(s
, 0xf10, 8, GIC_NR_SGIS
, translate_sgisource
);
504 /*****************************************************************
505 * CPU Interface(s) State
508 for (cpu
= 0; cpu
< s
->num_cpu
; cpu
++) {
509 /* GICC_CTLR -> s->cpu_ctlr[cpu] */
510 kvm_gicc_access(s
, 0x00, cpu
, ®
, false);
511 s
->cpu_ctlr
[cpu
] = reg
;
513 /* GICC_PMR -> s->priority_mask[cpu] */
514 kvm_gicc_access(s
, 0x04, cpu
, ®
, false);
515 s
->priority_mask
[cpu
] = (reg
& 0xff);
517 /* GICC_BPR -> s->bpr[cpu] */
518 kvm_gicc_access(s
, 0x08, cpu
, ®
, false);
519 s
->bpr
[cpu
] = (reg
& 0x7);
521 /* GICC_ABPR -> s->abpr[cpu] */
522 kvm_gicc_access(s
, 0x1c, cpu
, ®
, false);
523 s
->abpr
[cpu
] = (reg
& 0x7);
525 /* GICC_APRn -> s->apr[n][cpu] */
526 for (i
= 0; i
< 4; i
++) {
527 kvm_gicc_access(s
, 0xd0 + i
* 4, cpu
, ®
, false);
528 s
->apr
[i
][cpu
] = reg
;
533 static void kvm_arm_gic_reset(DeviceState
*dev
)
535 GICState
*s
= ARM_GIC_COMMON(dev
);
536 KVMARMGICClass
*kgc
= KVM_ARM_GIC_GET_CLASS(s
);
538 kgc
->parent_reset(dev
);
542 static void kvm_arm_gic_realize(DeviceState
*dev
, Error
**errp
)
545 GICState
*s
= KVM_ARM_GIC(dev
);
546 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
547 KVMARMGICClass
*kgc
= KVM_ARM_GIC_GET_CLASS(s
);
548 Error
*local_err
= NULL
;
551 kgc
->parent_realize(dev
, &local_err
);
553 error_propagate(errp
, local_err
);
557 if (s
->security_extn
) {
558 error_setg(errp
, "the in-kernel VGIC does not implement the "
559 "security extensions");
563 i
= s
->num_irq
- GIC_INTERNAL
;
564 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
565 * GPIO array layout is thus:
567 * [N..N+31] PPIs for CPU 0
568 * [N+32..N+63] PPIs for CPU 1
571 i
+= (GIC_INTERNAL
* s
->num_cpu
);
572 qdev_init_gpio_in(dev
, kvm_arm_gic_set_irq
, i
);
574 for (i
= 0; i
< s
->num_irq
- GIC_INTERNAL
; i
++) {
575 qemu_irq irq
= qdev_get_gpio_in(dev
, i
);
576 kvm_irqchip_set_qemuirq_gsi(kvm_state
, irq
, i
);
579 /* We never use our outbound IRQ/FIQ lines but provide them so that
580 * we maintain the same interface as the non-KVM GIC.
582 for (i
= 0; i
< s
->num_cpu
; i
++) {
583 sysbus_init_irq(sbd
, &s
->parent_irq
[i
]);
585 for (i
= 0; i
< s
->num_cpu
; i
++) {
586 sysbus_init_irq(sbd
, &s
->parent_fiq
[i
]);
589 /* Try to create the device via the device control API */
591 ret
= kvm_create_device(kvm_state
, KVM_DEV_TYPE_ARM_VGIC_V2
, false);
594 } else if (ret
!= -ENODEV
&& ret
!= -ENOTSUP
) {
595 error_setg_errno(errp
, -ret
, "error creating in-kernel VGIC");
599 if (kvm_gic_supports_attr(s
, KVM_DEV_ARM_VGIC_GRP_NR_IRQS
, 0)) {
600 uint32_t numirqs
= s
->num_irq
;
601 kvm_gic_access(s
, KVM_DEV_ARM_VGIC_GRP_NR_IRQS
, 0, 0, &numirqs
, 1);
604 /* Tell the kernel to complete VGIC initialization now */
605 if (kvm_gic_supports_attr(s
, KVM_DEV_ARM_VGIC_GRP_CTRL
,
606 KVM_DEV_ARM_VGIC_CTRL_INIT
)) {
607 kvm_gic_access(s
, KVM_DEV_ARM_VGIC_GRP_CTRL
,
608 KVM_DEV_ARM_VGIC_CTRL_INIT
, 0, 0, 1);
612 memory_region_init_reservation(&s
->iomem
, OBJECT(s
),
613 "kvm-gic_dist", 0x1000);
614 sysbus_init_mmio(sbd
, &s
->iomem
);
615 kvm_arm_register_device(&s
->iomem
,
616 (KVM_ARM_DEVICE_VGIC_V2
<< KVM_ARM_DEVICE_ID_SHIFT
)
617 | KVM_VGIC_V2_ADDR_TYPE_DIST
,
618 KVM_DEV_ARM_VGIC_GRP_ADDR
,
619 KVM_VGIC_V2_ADDR_TYPE_DIST
,
621 /* CPU interface for current core. Unlike arm_gic, we don't
622 * provide the "interface for core #N" memory regions, because
623 * cores with a VGIC don't have those.
625 memory_region_init_reservation(&s
->cpuiomem
[0], OBJECT(s
),
626 "kvm-gic_cpu", 0x1000);
627 sysbus_init_mmio(sbd
, &s
->cpuiomem
[0]);
628 kvm_arm_register_device(&s
->cpuiomem
[0],
629 (KVM_ARM_DEVICE_VGIC_V2
<< KVM_ARM_DEVICE_ID_SHIFT
)
630 | KVM_VGIC_V2_ADDR_TYPE_CPU
,
631 KVM_DEV_ARM_VGIC_GRP_ADDR
,
632 KVM_VGIC_V2_ADDR_TYPE_CPU
,
636 static void kvm_arm_gic_class_init(ObjectClass
*klass
, void *data
)
638 DeviceClass
*dc
= DEVICE_CLASS(klass
);
639 ARMGICCommonClass
*agcc
= ARM_GIC_COMMON_CLASS(klass
);
640 KVMARMGICClass
*kgc
= KVM_ARM_GIC_CLASS(klass
);
642 agcc
->pre_save
= kvm_arm_gic_get
;
643 agcc
->post_load
= kvm_arm_gic_put
;
644 kgc
->parent_realize
= dc
->realize
;
645 kgc
->parent_reset
= dc
->reset
;
646 dc
->realize
= kvm_arm_gic_realize
;
647 dc
->reset
= kvm_arm_gic_reset
;
650 static const TypeInfo kvm_arm_gic_info
= {
651 .name
= TYPE_KVM_ARM_GIC
,
652 .parent
= TYPE_ARM_GIC_COMMON
,
653 .instance_size
= sizeof(GICState
),
654 .class_init
= kvm_arm_gic_class_init
,
655 .class_size
= sizeof(KVMARMGICClass
),
658 static void kvm_arm_gic_register_types(void)
660 type_register_static(&kvm_arm_gic_info
);
663 type_init(kvm_arm_gic_register_types
)