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 "gic_internal.h"
28 #define DPRINTF(fmt, ...) \
29 do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
31 #define DPRINTF(fmt, ...) do {} while(0)
34 static const uint8_t gic_id
[] = {
35 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
38 #define NUM_CPU(s) ((s)->num_cpu)
40 static inline int gic_get_current_cpu(GICState
*s
)
43 return current_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_EDGE_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_set_priority(GICState
*s
, int cpu
, int irq
, uint8_t val
)
173 if (irq
< GIC_INTERNAL
) {
174 s
->priority1
[irq
][cpu
] = val
;
176 s
->priority2
[(irq
) - GIC_INTERNAL
] = val
;
180 void gic_complete_irq(GICState
*s
, int cpu
, int irq
)
184 DPRINTF("EOI %d\n", irq
);
185 if (irq
>= s
->num_irq
) {
186 /* This handles two cases:
187 * 1. If software writes the ID of a spurious interrupt [ie 1023]
188 * to the GICC_EOIR, the GIC ignores that write.
189 * 2. If software writes the number of a non-existent interrupt
190 * this must be a subcase of "value written does not match the last
191 * valid interrupt value read from the Interrupt Acknowledge
192 * register" and so this is UNPREDICTABLE. We choose to ignore it.
196 if (s
->running_irq
[cpu
] == 1023)
197 return; /* No active IRQ. */
198 /* Mark level triggered interrupts as pending if they are still
200 if (!GIC_TEST_EDGE_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
, cm
)
201 && GIC_TEST_LEVEL(irq
, cm
) && (GIC_TARGET(irq
) & cm
) != 0) {
202 DPRINTF("Set %d pending mask %x\n", irq
, cm
);
203 GIC_SET_PENDING(irq
, cm
);
206 if (irq
!= s
->running_irq
[cpu
]) {
207 /* Complete an IRQ that is not currently running. */
208 int tmp
= s
->running_irq
[cpu
];
209 while (s
->last_active
[tmp
][cpu
] != 1023) {
210 if (s
->last_active
[tmp
][cpu
] == irq
) {
211 s
->last_active
[tmp
][cpu
] = s
->last_active
[irq
][cpu
];
214 tmp
= s
->last_active
[tmp
][cpu
];
220 /* Complete the current running IRQ. */
221 gic_set_running_irq(s
, cpu
, s
->last_active
[s
->running_irq
[cpu
]][cpu
]);
225 static uint32_t gic_dist_readb(void *opaque
, hwaddr offset
)
227 GICState
*s
= (GICState
*)opaque
;
235 cpu
= gic_get_current_cpu(s
);
237 if (offset
< 0x100) {
241 return ((s
->num_irq
/ 32) - 1) | ((NUM_CPU(s
) - 1) << 5);
244 if (offset
>= 0x80) {
245 /* Interrupt Security , RAZ/WI */
249 } else if (offset
< 0x200) {
250 /* Interrupt Set/Clear Enable. */
252 irq
= (offset
- 0x100) * 8;
254 irq
= (offset
- 0x180) * 8;
256 if (irq
>= s
->num_irq
)
259 for (i
= 0; i
< 8; i
++) {
260 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
264 } else if (offset
< 0x300) {
265 /* Interrupt Set/Clear Pending. */
267 irq
= (offset
- 0x200) * 8;
269 irq
= (offset
- 0x280) * 8;
271 if (irq
>= s
->num_irq
)
274 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
275 for (i
= 0; i
< 8; i
++) {
276 if (GIC_TEST_PENDING(irq
+ i
, mask
)) {
280 } else if (offset
< 0x400) {
281 /* Interrupt Active. */
282 irq
= (offset
- 0x300) * 8 + GIC_BASE_IRQ
;
283 if (irq
>= s
->num_irq
)
286 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
287 for (i
= 0; i
< 8; i
++) {
288 if (GIC_TEST_ACTIVE(irq
+ i
, mask
)) {
292 } else if (offset
< 0x800) {
293 /* Interrupt Priority. */
294 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
295 if (irq
>= s
->num_irq
)
297 res
= GIC_GET_PRIORITY(irq
, cpu
);
298 } else if (offset
< 0xc00) {
299 /* Interrupt CPU Target. */
300 if (s
->num_cpu
== 1 && s
->revision
!= REV_11MPCORE
) {
301 /* For uniprocessor GICs these RAZ/WI */
304 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
305 if (irq
>= s
->num_irq
) {
308 if (irq
>= 29 && irq
<= 31) {
311 res
= GIC_TARGET(irq
);
314 } else if (offset
< 0xf00) {
315 /* Interrupt Configuration. */
316 irq
= (offset
- 0xc00) * 2 + GIC_BASE_IRQ
;
317 if (irq
>= s
->num_irq
)
320 for (i
= 0; i
< 4; i
++) {
321 if (GIC_TEST_MODEL(irq
+ i
))
322 res
|= (1 << (i
* 2));
323 if (GIC_TEST_EDGE_TRIGGER(irq
+ i
))
324 res
|= (2 << (i
* 2));
326 } else if (offset
< 0xfe0) {
328 } else /* offset >= 0xfe0 */ {
332 res
= gic_id
[(offset
- 0xfe0) >> 2];
337 qemu_log_mask(LOG_GUEST_ERROR
,
338 "gic_dist_readb: Bad offset %x\n", (int)offset
);
342 static uint32_t gic_dist_readw(void *opaque
, hwaddr offset
)
345 val
= gic_dist_readb(opaque
, offset
);
346 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
350 static uint32_t gic_dist_readl(void *opaque
, hwaddr offset
)
353 val
= gic_dist_readw(opaque
, offset
);
354 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
358 static void gic_dist_writeb(void *opaque
, hwaddr offset
,
361 GICState
*s
= (GICState
*)opaque
;
366 cpu
= gic_get_current_cpu(s
);
367 if (offset
< 0x100) {
369 s
->enabled
= (value
& 1);
370 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
371 } else if (offset
< 4) {
373 } else if (offset
>= 0x80) {
374 /* Interrupt Security Registers, RAZ/WI */
378 } else if (offset
< 0x180) {
379 /* Interrupt Set Enable. */
380 irq
= (offset
- 0x100) * 8 + GIC_BASE_IRQ
;
381 if (irq
>= s
->num_irq
)
383 if (irq
< GIC_NR_SGIS
) {
387 for (i
= 0; i
< 8; i
++) {
388 if (value
& (1 << i
)) {
390 (irq
< GIC_INTERNAL
) ? (1 << cpu
) : GIC_TARGET(irq
+ i
);
391 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
393 if (!GIC_TEST_ENABLED(irq
+ i
, cm
)) {
394 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
396 GIC_SET_ENABLED(irq
+ i
, cm
);
397 /* If a raised level triggered IRQ enabled then mark
399 if (GIC_TEST_LEVEL(irq
+ i
, mask
)
400 && !GIC_TEST_EDGE_TRIGGER(irq
+ i
)) {
401 DPRINTF("Set %d pending mask %x\n", irq
+ i
, mask
);
402 GIC_SET_PENDING(irq
+ i
, mask
);
406 } else if (offset
< 0x200) {
407 /* Interrupt Clear Enable. */
408 irq
= (offset
- 0x180) * 8 + GIC_BASE_IRQ
;
409 if (irq
>= s
->num_irq
)
411 if (irq
< GIC_NR_SGIS
) {
415 for (i
= 0; i
< 8; i
++) {
416 if (value
& (1 << i
)) {
417 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
419 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
420 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
422 GIC_CLEAR_ENABLED(irq
+ i
, cm
);
425 } else if (offset
< 0x280) {
426 /* Interrupt Set Pending. */
427 irq
= (offset
- 0x200) * 8 + GIC_BASE_IRQ
;
428 if (irq
>= s
->num_irq
)
430 if (irq
< GIC_NR_SGIS
) {
434 for (i
= 0; i
< 8; i
++) {
435 if (value
& (1 << i
)) {
436 GIC_SET_PENDING(irq
+ i
, GIC_TARGET(irq
+ i
));
439 } else if (offset
< 0x300) {
440 /* Interrupt Clear Pending. */
441 irq
= (offset
- 0x280) * 8 + GIC_BASE_IRQ
;
442 if (irq
>= s
->num_irq
)
444 for (i
= 0; i
< 8; i
++) {
445 /* ??? This currently clears the pending bit for all CPUs, even
446 for per-CPU interrupts. It's unclear whether this is the
448 if (value
& (1 << i
)) {
449 GIC_CLEAR_PENDING(irq
+ i
, ALL_CPU_MASK
);
452 } else if (offset
< 0x400) {
453 /* Interrupt Active. */
455 } else if (offset
< 0x800) {
456 /* Interrupt Priority. */
457 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
458 if (irq
>= s
->num_irq
)
460 gic_set_priority(s
, cpu
, irq
, value
);
461 } else if (offset
< 0xc00) {
462 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
463 * annoying exception of the 11MPCore's GIC.
465 if (s
->num_cpu
!= 1 || s
->revision
== REV_11MPCORE
) {
466 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
467 if (irq
>= s
->num_irq
) {
472 } else if (irq
< GIC_INTERNAL
) {
473 value
= ALL_CPU_MASK
;
475 s
->irq_target
[irq
] = value
& ALL_CPU_MASK
;
477 } else if (offset
< 0xf00) {
478 /* Interrupt Configuration. */
479 irq
= (offset
- 0xc00) * 4 + GIC_BASE_IRQ
;
480 if (irq
>= s
->num_irq
)
482 if (irq
< GIC_INTERNAL
)
484 for (i
= 0; i
< 4; i
++) {
485 if (value
& (1 << (i
* 2))) {
486 GIC_SET_MODEL(irq
+ i
);
488 GIC_CLEAR_MODEL(irq
+ i
);
490 if (value
& (2 << (i
* 2))) {
491 GIC_SET_EDGE_TRIGGER(irq
+ i
);
493 GIC_CLEAR_EDGE_TRIGGER(irq
+ i
);
497 /* 0xf00 is only handled for 32-bit writes. */
503 qemu_log_mask(LOG_GUEST_ERROR
,
504 "gic_dist_writeb: Bad offset %x\n", (int)offset
);
507 static void gic_dist_writew(void *opaque
, hwaddr offset
,
510 gic_dist_writeb(opaque
, offset
, value
& 0xff);
511 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
514 static void gic_dist_writel(void *opaque
, hwaddr offset
,
517 GICState
*s
= (GICState
*)opaque
;
518 if (offset
== 0xf00) {
523 cpu
= gic_get_current_cpu(s
);
525 switch ((value
>> 24) & 3) {
527 mask
= (value
>> 16) & ALL_CPU_MASK
;
530 mask
= ALL_CPU_MASK
^ (1 << cpu
);
536 DPRINTF("Bad Soft Int target filter\n");
540 GIC_SET_PENDING(irq
, mask
);
544 gic_dist_writew(opaque
, offset
, value
& 0xffff);
545 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
548 static const MemoryRegionOps gic_dist_ops
= {
550 .read
= { gic_dist_readb
, gic_dist_readw
, gic_dist_readl
, },
551 .write
= { gic_dist_writeb
, gic_dist_writew
, gic_dist_writel
, },
553 .endianness
= DEVICE_NATIVE_ENDIAN
,
556 static uint32_t gic_cpu_read(GICState
*s
, int cpu
, int offset
)
559 case 0x00: /* Control */
560 return s
->cpu_enabled
[cpu
];
561 case 0x04: /* Priority mask */
562 return s
->priority_mask
[cpu
];
563 case 0x08: /* Binary Point */
564 /* ??? Not implemented. */
566 case 0x0c: /* Acknowledge */
567 return gic_acknowledge_irq(s
, cpu
);
568 case 0x14: /* Running Priority */
569 return s
->running_priority
[cpu
];
570 case 0x18: /* Highest Pending Interrupt */
571 return s
->current_pending
[cpu
];
573 qemu_log_mask(LOG_GUEST_ERROR
,
574 "gic_cpu_read: Bad offset %x\n", (int)offset
);
579 static void gic_cpu_write(GICState
*s
, int cpu
, int offset
, uint32_t value
)
582 case 0x00: /* Control */
583 s
->cpu_enabled
[cpu
] = (value
& 1);
584 DPRINTF("CPU %d %sabled\n", cpu
, s
->cpu_enabled
[cpu
] ? "En" : "Dis");
586 case 0x04: /* Priority mask */
587 s
->priority_mask
[cpu
] = (value
& 0xff);
589 case 0x08: /* Binary Point */
590 /* ??? Not implemented. */
592 case 0x10: /* End Of Interrupt */
593 return gic_complete_irq(s
, cpu
, value
& 0x3ff);
595 qemu_log_mask(LOG_GUEST_ERROR
,
596 "gic_cpu_write: Bad offset %x\n", (int)offset
);
602 /* Wrappers to read/write the GIC CPU interface for the current CPU */
603 static uint64_t gic_thiscpu_read(void *opaque
, hwaddr addr
,
606 GICState
*s
= (GICState
*)opaque
;
607 return gic_cpu_read(s
, gic_get_current_cpu(s
), addr
);
610 static void gic_thiscpu_write(void *opaque
, hwaddr addr
,
611 uint64_t value
, unsigned size
)
613 GICState
*s
= (GICState
*)opaque
;
614 gic_cpu_write(s
, gic_get_current_cpu(s
), addr
, value
);
617 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
618 * These just decode the opaque pointer into GICState* + cpu id.
620 static uint64_t gic_do_cpu_read(void *opaque
, hwaddr addr
,
623 GICState
**backref
= (GICState
**)opaque
;
624 GICState
*s
= *backref
;
625 int id
= (backref
- s
->backref
);
626 return gic_cpu_read(s
, id
, addr
);
629 static void gic_do_cpu_write(void *opaque
, hwaddr addr
,
630 uint64_t value
, unsigned size
)
632 GICState
**backref
= (GICState
**)opaque
;
633 GICState
*s
= *backref
;
634 int id
= (backref
- s
->backref
);
635 gic_cpu_write(s
, id
, addr
, value
);
638 static const MemoryRegionOps gic_thiscpu_ops
= {
639 .read
= gic_thiscpu_read
,
640 .write
= gic_thiscpu_write
,
641 .endianness
= DEVICE_NATIVE_ENDIAN
,
644 static const MemoryRegionOps gic_cpu_ops
= {
645 .read
= gic_do_cpu_read
,
646 .write
= gic_do_cpu_write
,
647 .endianness
= DEVICE_NATIVE_ENDIAN
,
650 void gic_init_irqs_and_distributor(GICState
*s
, int num_irq
)
652 SysBusDevice
*sbd
= SYS_BUS_DEVICE(s
);
655 i
= s
->num_irq
- GIC_INTERNAL
;
656 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
657 * GPIO array layout is thus:
659 * [N..N+31] PPIs for CPU 0
660 * [N+32..N+63] PPIs for CPU 1
663 if (s
->revision
!= REV_NVIC
) {
664 i
+= (GIC_INTERNAL
* s
->num_cpu
);
666 qdev_init_gpio_in(DEVICE(s
), gic_set_irq
, i
);
667 for (i
= 0; i
< NUM_CPU(s
); i
++) {
668 sysbus_init_irq(sbd
, &s
->parent_irq
[i
]);
670 memory_region_init_io(&s
->iomem
, OBJECT(s
), &gic_dist_ops
, s
,
674 static void arm_gic_realize(DeviceState
*dev
, Error
**errp
)
676 /* Device instance realize function for the GIC sysbus device */
678 GICState
*s
= ARM_GIC(dev
);
679 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
680 ARMGICClass
*agc
= ARM_GIC_GET_CLASS(s
);
682 agc
->parent_realize(dev
, errp
);
683 if (error_is_set(errp
)) {
687 gic_init_irqs_and_distributor(s
, s
->num_irq
);
689 /* Memory regions for the CPU interfaces (NVIC doesn't have these):
690 * a region for "CPU interface for this core", then a region for
691 * "CPU interface for core 0", "for core 1", ...
692 * NB that the memory region size of 0x100 applies for the 11MPCore
693 * and also cores following the GIC v1 spec (ie A9).
694 * GIC v2 defines a larger memory region (0x1000) so this will need
695 * to be extended when we implement A15.
697 memory_region_init_io(&s
->cpuiomem
[0], OBJECT(s
), &gic_thiscpu_ops
, s
,
699 for (i
= 0; i
< NUM_CPU(s
); i
++) {
701 memory_region_init_io(&s
->cpuiomem
[i
+1], OBJECT(s
), &gic_cpu_ops
,
702 &s
->backref
[i
], "gic_cpu", 0x100);
705 sysbus_init_mmio(sbd
, &s
->iomem
);
706 /* cpu interfaces (one for "current cpu" plus one per cpu) */
707 for (i
= 0; i
<= NUM_CPU(s
); i
++) {
708 sysbus_init_mmio(sbd
, &s
->cpuiomem
[i
]);
712 static void arm_gic_class_init(ObjectClass
*klass
, void *data
)
714 DeviceClass
*dc
= DEVICE_CLASS(klass
);
715 ARMGICClass
*agc
= ARM_GIC_CLASS(klass
);
717 agc
->parent_realize
= dc
->realize
;
718 dc
->realize
= arm_gic_realize
;
721 static const TypeInfo arm_gic_info
= {
722 .name
= TYPE_ARM_GIC
,
723 .parent
= TYPE_ARM_GIC_COMMON
,
724 .instance_size
= sizeof(GICState
),
725 .class_init
= arm_gic_class_init
,
726 .class_size
= sizeof(ARMGICClass
),
729 static void arm_gic_register_types(void)
731 type_register_static(&arm_gic_info
);
734 type_init(arm_gic_register_types
)