2 * ARM Generic/Distributed Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 /* This file contains implementation code for the RealView EB interrupt
11 * controller, MPCore distributed interrupt controller and ARMv7-M
12 * Nested Vectored Interrupt Controller.
13 * It is compiled in two ways:
14 * (1) as a standalone file to produce a sysbus device which is a GIC
15 * that can be used on the realview board and as one of the builtin
16 * private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
17 * (2) by being directly #included into armv7m_nvic.c to produce the
21 #include "hw/sysbus.h"
22 #include "hw/arm_gic_internal.h"
27 #define DPRINTF(fmt, ...) \
28 do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
30 #define DPRINTF(fmt, ...) do {} while(0)
33 static const uint8_t gic_id
[] = {
34 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
37 #define NUM_CPU(s) ((s)->num_cpu)
39 static inline int gic_get_current_cpu(GICState
*s
)
42 CPUState
*cpu
= ENV_GET_CPU(cpu_single_env
);
43 return cpu
->cpu_index
;
48 /* TODO: Many places that call this routine could be optimized. */
49 /* Update interrupt status after enabled or pending bits have been changed. */
50 void gic_update(GICState
*s
)
59 for (cpu
= 0; cpu
< NUM_CPU(s
); cpu
++) {
61 s
->current_pending
[cpu
] = 1023;
62 if (!s
->enabled
|| !s
->cpu_enabled
[cpu
]) {
63 qemu_irq_lower(s
->parent_irq
[cpu
]);
68 for (irq
= 0; irq
< s
->num_irq
; irq
++) {
69 if (GIC_TEST_ENABLED(irq
, cm
) && GIC_TEST_PENDING(irq
, cm
)) {
70 if (GIC_GET_PRIORITY(irq
, cpu
) < best_prio
) {
71 best_prio
= GIC_GET_PRIORITY(irq
, cpu
);
77 if (best_prio
< s
->priority_mask
[cpu
]) {
78 s
->current_pending
[cpu
] = best_irq
;
79 if (best_prio
< s
->running_priority
[cpu
]) {
80 DPRINTF("Raised pending IRQ %d (cpu %d)\n", best_irq
, cpu
);
84 qemu_set_irq(s
->parent_irq
[cpu
], level
);
88 void gic_set_pending_private(GICState
*s
, int cpu
, int irq
)
92 if (GIC_TEST_PENDING(irq
, cm
))
95 DPRINTF("Set %d pending cpu %d\n", irq
, cpu
);
96 GIC_SET_PENDING(irq
, cm
);
100 /* Process a change in an external IRQ input. */
101 static void gic_set_irq(void *opaque
, int irq
, int level
)
103 /* Meaning of the 'irq' parameter:
104 * [0..N-1] : external interrupts
105 * [N..N+31] : PPI (internal) interrupts for CPU 0
106 * [N+32..N+63] : PPI (internal interrupts for CPU 1
109 GICState
*s
= (GICState
*)opaque
;
111 if (irq
< (s
->num_irq
- GIC_INTERNAL
)) {
112 /* The first external input line is internal interrupt 32. */
115 target
= GIC_TARGET(irq
);
118 irq
-= (s
->num_irq
- GIC_INTERNAL
);
119 cpu
= irq
/ GIC_INTERNAL
;
125 if (level
== GIC_TEST_LEVEL(irq
, cm
)) {
130 GIC_SET_LEVEL(irq
, cm
);
131 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
, cm
)) {
132 DPRINTF("Set %d pending mask %x\n", irq
, target
);
133 GIC_SET_PENDING(irq
, target
);
136 GIC_CLEAR_LEVEL(irq
, cm
);
141 static void gic_set_running_irq(GICState
*s
, int cpu
, int irq
)
143 s
->running_irq
[cpu
] = irq
;
145 s
->running_priority
[cpu
] = 0x100;
147 s
->running_priority
[cpu
] = GIC_GET_PRIORITY(irq
, cpu
);
152 uint32_t gic_acknowledge_irq(GICState
*s
, int cpu
)
156 new_irq
= s
->current_pending
[cpu
];
158 || GIC_GET_PRIORITY(new_irq
, cpu
) >= s
->running_priority
[cpu
]) {
159 DPRINTF("ACK no pending IRQ\n");
162 s
->last_active
[new_irq
][cpu
] = s
->running_irq
[cpu
];
163 /* Clear pending flags for both level and edge triggered interrupts.
164 Level triggered IRQs will be reasserted once they become inactive. */
165 GIC_CLEAR_PENDING(new_irq
, GIC_TEST_MODEL(new_irq
) ? ALL_CPU_MASK
: cm
);
166 gic_set_running_irq(s
, cpu
, new_irq
);
167 DPRINTF("ACK %d\n", new_irq
);
171 void gic_complete_irq(GICState
*s
, int cpu
, int irq
)
175 DPRINTF("EOI %d\n", irq
);
176 if (irq
>= s
->num_irq
) {
177 /* This handles two cases:
178 * 1. If software writes the ID of a spurious interrupt [ie 1023]
179 * to the GICC_EOIR, the GIC ignores that write.
180 * 2. If software writes the number of a non-existent interrupt
181 * this must be a subcase of "value written does not match the last
182 * valid interrupt value read from the Interrupt Acknowledge
183 * register" and so this is UNPREDICTABLE. We choose to ignore it.
187 if (s
->running_irq
[cpu
] == 1023)
188 return; /* No active IRQ. */
189 /* Mark level triggered interrupts as pending if they are still
191 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
, cm
)
192 && GIC_TEST_LEVEL(irq
, cm
) && (GIC_TARGET(irq
) & cm
) != 0) {
193 DPRINTF("Set %d pending mask %x\n", irq
, cm
);
194 GIC_SET_PENDING(irq
, cm
);
197 if (irq
!= s
->running_irq
[cpu
]) {
198 /* Complete an IRQ that is not currently running. */
199 int tmp
= s
->running_irq
[cpu
];
200 while (s
->last_active
[tmp
][cpu
] != 1023) {
201 if (s
->last_active
[tmp
][cpu
] == irq
) {
202 s
->last_active
[tmp
][cpu
] = s
->last_active
[irq
][cpu
];
205 tmp
= s
->last_active
[tmp
][cpu
];
211 /* Complete the current running IRQ. */
212 gic_set_running_irq(s
, cpu
, s
->last_active
[s
->running_irq
[cpu
]][cpu
]);
216 static uint32_t gic_dist_readb(void *opaque
, hwaddr offset
)
218 GICState
*s
= (GICState
*)opaque
;
226 cpu
= gic_get_current_cpu(s
);
228 if (offset
< 0x100) {
232 return ((s
->num_irq
/ 32) - 1) | ((NUM_CPU(s
) - 1) << 5);
235 if (offset
>= 0x80) {
236 /* Interrupt Security , RAZ/WI */
240 } else if (offset
< 0x200) {
241 /* Interrupt Set/Clear Enable. */
243 irq
= (offset
- 0x100) * 8;
245 irq
= (offset
- 0x180) * 8;
247 if (irq
>= s
->num_irq
)
250 for (i
= 0; i
< 8; i
++) {
251 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
255 } else if (offset
< 0x300) {
256 /* Interrupt Set/Clear Pending. */
258 irq
= (offset
- 0x200) * 8;
260 irq
= (offset
- 0x280) * 8;
262 if (irq
>= s
->num_irq
)
265 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
266 for (i
= 0; i
< 8; i
++) {
267 if (GIC_TEST_PENDING(irq
+ i
, mask
)) {
271 } else if (offset
< 0x400) {
272 /* Interrupt Active. */
273 irq
= (offset
- 0x300) * 8 + GIC_BASE_IRQ
;
274 if (irq
>= s
->num_irq
)
277 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
278 for (i
= 0; i
< 8; i
++) {
279 if (GIC_TEST_ACTIVE(irq
+ i
, mask
)) {
283 } else if (offset
< 0x800) {
284 /* Interrupt Priority. */
285 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
286 if (irq
>= s
->num_irq
)
288 res
= GIC_GET_PRIORITY(irq
, cpu
);
289 } else if (offset
< 0xc00) {
290 /* Interrupt CPU Target. */
291 if (s
->num_cpu
== 1 && s
->revision
!= REV_11MPCORE
) {
292 /* For uniprocessor GICs these RAZ/WI */
295 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
296 if (irq
>= s
->num_irq
) {
299 if (irq
>= 29 && irq
<= 31) {
302 res
= GIC_TARGET(irq
);
305 } else if (offset
< 0xf00) {
306 /* Interrupt Configuration. */
307 irq
= (offset
- 0xc00) * 2 + GIC_BASE_IRQ
;
308 if (irq
>= s
->num_irq
)
311 for (i
= 0; i
< 4; i
++) {
312 if (GIC_TEST_MODEL(irq
+ i
))
313 res
|= (1 << (i
* 2));
314 if (GIC_TEST_TRIGGER(irq
+ i
))
315 res
|= (2 << (i
* 2));
317 } else if (offset
< 0xfe0) {
319 } else /* offset >= 0xfe0 */ {
323 res
= gic_id
[(offset
- 0xfe0) >> 2];
328 qemu_log_mask(LOG_GUEST_ERROR
,
329 "gic_dist_readb: Bad offset %x\n", (int)offset
);
333 static uint32_t gic_dist_readw(void *opaque
, hwaddr offset
)
336 val
= gic_dist_readb(opaque
, offset
);
337 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
341 static uint32_t gic_dist_readl(void *opaque
, hwaddr offset
)
344 val
= gic_dist_readw(opaque
, offset
);
345 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
349 static void gic_dist_writeb(void *opaque
, hwaddr offset
,
352 GICState
*s
= (GICState
*)opaque
;
357 cpu
= gic_get_current_cpu(s
);
358 if (offset
< 0x100) {
360 s
->enabled
= (value
& 1);
361 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
362 } else if (offset
< 4) {
364 } else if (offset
>= 0x80) {
365 /* Interrupt Security Registers, RAZ/WI */
369 } else if (offset
< 0x180) {
370 /* Interrupt Set Enable. */
371 irq
= (offset
- 0x100) * 8 + GIC_BASE_IRQ
;
372 if (irq
>= s
->num_irq
)
376 for (i
= 0; i
< 8; i
++) {
377 if (value
& (1 << i
)) {
379 (irq
< GIC_INTERNAL
) ? (1 << cpu
) : GIC_TARGET(irq
+ i
);
380 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
382 if (!GIC_TEST_ENABLED(irq
+ i
, cm
)) {
383 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
385 GIC_SET_ENABLED(irq
+ i
, cm
);
386 /* If a raised level triggered IRQ enabled then mark
388 if (GIC_TEST_LEVEL(irq
+ i
, mask
)
389 && !GIC_TEST_TRIGGER(irq
+ i
)) {
390 DPRINTF("Set %d pending mask %x\n", irq
+ i
, mask
);
391 GIC_SET_PENDING(irq
+ i
, mask
);
395 } else if (offset
< 0x200) {
396 /* Interrupt Clear Enable. */
397 irq
= (offset
- 0x180) * 8 + GIC_BASE_IRQ
;
398 if (irq
>= s
->num_irq
)
402 for (i
= 0; i
< 8; i
++) {
403 if (value
& (1 << i
)) {
404 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
406 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
407 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
409 GIC_CLEAR_ENABLED(irq
+ i
, cm
);
412 } else if (offset
< 0x280) {
413 /* Interrupt Set Pending. */
414 irq
= (offset
- 0x200) * 8 + GIC_BASE_IRQ
;
415 if (irq
>= s
->num_irq
)
420 for (i
= 0; i
< 8; i
++) {
421 if (value
& (1 << i
)) {
422 GIC_SET_PENDING(irq
+ i
, GIC_TARGET(irq
+ i
));
425 } else if (offset
< 0x300) {
426 /* Interrupt Clear Pending. */
427 irq
= (offset
- 0x280) * 8 + GIC_BASE_IRQ
;
428 if (irq
>= s
->num_irq
)
430 for (i
= 0; i
< 8; i
++) {
431 /* ??? This currently clears the pending bit for all CPUs, even
432 for per-CPU interrupts. It's unclear whether this is the
434 if (value
& (1 << i
)) {
435 GIC_CLEAR_PENDING(irq
+ i
, ALL_CPU_MASK
);
438 } else if (offset
< 0x400) {
439 /* Interrupt Active. */
441 } else if (offset
< 0x800) {
442 /* Interrupt Priority. */
443 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
444 if (irq
>= s
->num_irq
)
446 if (irq
< GIC_INTERNAL
) {
447 s
->priority1
[irq
][cpu
] = value
;
449 s
->priority2
[irq
- GIC_INTERNAL
] = value
;
451 } else if (offset
< 0xc00) {
452 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
453 * annoying exception of the 11MPCore's GIC.
455 if (s
->num_cpu
!= 1 || s
->revision
== REV_11MPCORE
) {
456 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
457 if (irq
>= s
->num_irq
) {
462 } else if (irq
< GIC_INTERNAL
) {
463 value
= ALL_CPU_MASK
;
465 s
->irq_target
[irq
] = value
& ALL_CPU_MASK
;
467 } else if (offset
< 0xf00) {
468 /* Interrupt Configuration. */
469 irq
= (offset
- 0xc00) * 4 + GIC_BASE_IRQ
;
470 if (irq
>= s
->num_irq
)
472 if (irq
< GIC_INTERNAL
)
474 for (i
= 0; i
< 4; i
++) {
475 if (value
& (1 << (i
* 2))) {
476 GIC_SET_MODEL(irq
+ i
);
478 GIC_CLEAR_MODEL(irq
+ i
);
480 if (value
& (2 << (i
* 2))) {
481 GIC_SET_TRIGGER(irq
+ i
);
483 GIC_CLEAR_TRIGGER(irq
+ i
);
487 /* 0xf00 is only handled for 32-bit writes. */
493 qemu_log_mask(LOG_GUEST_ERROR
,
494 "gic_dist_writeb: Bad offset %x\n", (int)offset
);
497 static void gic_dist_writew(void *opaque
, hwaddr offset
,
500 gic_dist_writeb(opaque
, offset
, value
& 0xff);
501 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
504 static void gic_dist_writel(void *opaque
, hwaddr offset
,
507 GICState
*s
= (GICState
*)opaque
;
508 if (offset
== 0xf00) {
513 cpu
= gic_get_current_cpu(s
);
515 switch ((value
>> 24) & 3) {
517 mask
= (value
>> 16) & ALL_CPU_MASK
;
520 mask
= ALL_CPU_MASK
^ (1 << cpu
);
526 DPRINTF("Bad Soft Int target filter\n");
530 GIC_SET_PENDING(irq
, mask
);
534 gic_dist_writew(opaque
, offset
, value
& 0xffff);
535 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
538 static const MemoryRegionOps gic_dist_ops
= {
540 .read
= { gic_dist_readb
, gic_dist_readw
, gic_dist_readl
, },
541 .write
= { gic_dist_writeb
, gic_dist_writew
, gic_dist_writel
, },
543 .endianness
= DEVICE_NATIVE_ENDIAN
,
546 static uint32_t gic_cpu_read(GICState
*s
, int cpu
, int offset
)
549 case 0x00: /* Control */
550 return s
->cpu_enabled
[cpu
];
551 case 0x04: /* Priority mask */
552 return s
->priority_mask
[cpu
];
553 case 0x08: /* Binary Point */
554 /* ??? Not implemented. */
556 case 0x0c: /* Acknowledge */
557 return gic_acknowledge_irq(s
, cpu
);
558 case 0x14: /* Running Priority */
559 return s
->running_priority
[cpu
];
560 case 0x18: /* Highest Pending Interrupt */
561 return s
->current_pending
[cpu
];
563 qemu_log_mask(LOG_GUEST_ERROR
,
564 "gic_cpu_read: Bad offset %x\n", (int)offset
);
569 static void gic_cpu_write(GICState
*s
, int cpu
, int offset
, uint32_t value
)
572 case 0x00: /* Control */
573 s
->cpu_enabled
[cpu
] = (value
& 1);
574 DPRINTF("CPU %d %sabled\n", cpu
, s
->cpu_enabled
[cpu
] ? "En" : "Dis");
576 case 0x04: /* Priority mask */
577 s
->priority_mask
[cpu
] = (value
& 0xff);
579 case 0x08: /* Binary Point */
580 /* ??? Not implemented. */
582 case 0x10: /* End Of Interrupt */
583 return gic_complete_irq(s
, cpu
, value
& 0x3ff);
585 qemu_log_mask(LOG_GUEST_ERROR
,
586 "gic_cpu_write: Bad offset %x\n", (int)offset
);
592 /* Wrappers to read/write the GIC CPU interface for the current CPU */
593 static uint64_t gic_thiscpu_read(void *opaque
, hwaddr addr
,
596 GICState
*s
= (GICState
*)opaque
;
597 return gic_cpu_read(s
, gic_get_current_cpu(s
), addr
);
600 static void gic_thiscpu_write(void *opaque
, hwaddr addr
,
601 uint64_t value
, unsigned size
)
603 GICState
*s
= (GICState
*)opaque
;
604 gic_cpu_write(s
, gic_get_current_cpu(s
), addr
, value
);
607 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
608 * These just decode the opaque pointer into GICState* + cpu id.
610 static uint64_t gic_do_cpu_read(void *opaque
, hwaddr addr
,
613 GICState
**backref
= (GICState
**)opaque
;
614 GICState
*s
= *backref
;
615 int id
= (backref
- s
->backref
);
616 return gic_cpu_read(s
, id
, addr
);
619 static void gic_do_cpu_write(void *opaque
, hwaddr addr
,
620 uint64_t value
, unsigned size
)
622 GICState
**backref
= (GICState
**)opaque
;
623 GICState
*s
= *backref
;
624 int id
= (backref
- s
->backref
);
625 gic_cpu_write(s
, id
, addr
, value
);
628 static const MemoryRegionOps gic_thiscpu_ops
= {
629 .read
= gic_thiscpu_read
,
630 .write
= gic_thiscpu_write
,
631 .endianness
= DEVICE_NATIVE_ENDIAN
,
634 static const MemoryRegionOps gic_cpu_ops
= {
635 .read
= gic_do_cpu_read
,
636 .write
= gic_do_cpu_write
,
637 .endianness
= DEVICE_NATIVE_ENDIAN
,
640 void gic_init_irqs_and_distributor(GICState
*s
, int num_irq
)
644 i
= s
->num_irq
- GIC_INTERNAL
;
645 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
646 * GPIO array layout is thus:
648 * [N..N+31] PPIs for CPU 0
649 * [N+32..N+63] PPIs for CPU 1
652 if (s
->revision
!= REV_NVIC
) {
653 i
+= (GIC_INTERNAL
* s
->num_cpu
);
655 qdev_init_gpio_in(&s
->busdev
.qdev
, gic_set_irq
, i
);
656 for (i
= 0; i
< NUM_CPU(s
); i
++) {
657 sysbus_init_irq(&s
->busdev
, &s
->parent_irq
[i
]);
659 memory_region_init_io(&s
->iomem
, &gic_dist_ops
, s
, "gic_dist", 0x1000);
662 static void arm_gic_realize(DeviceState
*dev
, Error
**errp
)
664 /* Device instance realize function for the GIC sysbus device */
666 GICState
*s
= ARM_GIC(dev
);
667 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
668 ARMGICClass
*agc
= ARM_GIC_GET_CLASS(s
);
670 agc
->parent_realize(dev
, errp
);
671 if (error_is_set(errp
)) {
675 gic_init_irqs_and_distributor(s
, s
->num_irq
);
677 /* Memory regions for the CPU interfaces (NVIC doesn't have these):
678 * a region for "CPU interface for this core", then a region for
679 * "CPU interface for core 0", "for core 1", ...
680 * NB that the memory region size of 0x100 applies for the 11MPCore
681 * and also cores following the GIC v1 spec (ie A9).
682 * GIC v2 defines a larger memory region (0x1000) so this will need
683 * to be extended when we implement A15.
685 memory_region_init_io(&s
->cpuiomem
[0], &gic_thiscpu_ops
, s
,
687 for (i
= 0; i
< NUM_CPU(s
); i
++) {
689 memory_region_init_io(&s
->cpuiomem
[i
+1], &gic_cpu_ops
, &s
->backref
[i
],
693 sysbus_init_mmio(sbd
, &s
->iomem
);
694 /* cpu interfaces (one for "current cpu" plus one per cpu) */
695 for (i
= 0; i
<= NUM_CPU(s
); i
++) {
696 sysbus_init_mmio(sbd
, &s
->cpuiomem
[i
]);
700 static void arm_gic_class_init(ObjectClass
*klass
, void *data
)
702 DeviceClass
*dc
= DEVICE_CLASS(klass
);
703 ARMGICClass
*agc
= ARM_GIC_CLASS(klass
);
706 agc
->parent_realize
= dc
->realize
;
707 dc
->realize
= arm_gic_realize
;
710 static const TypeInfo arm_gic_info
= {
711 .name
= TYPE_ARM_GIC
,
712 .parent
= TYPE_ARM_GIC_COMMON
,
713 .instance_size
= sizeof(GICState
),
714 .class_init
= arm_gic_class_init
,
715 .class_size
= sizeof(ARMGICClass
),
718 static void arm_gic_register_types(void)
720 type_register_static(&arm_gic_info
);
723 type_init(arm_gic_register_types
)