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
22 #include "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 return cpu_single_env
->cpu_index
;
47 /* TODO: Many places that call this routine could be optimized. */
48 /* Update interrupt status after enabled or pending bits have been changed. */
49 void gic_update(GICState
*s
)
58 for (cpu
= 0; cpu
< NUM_CPU(s
); cpu
++) {
60 s
->current_pending
[cpu
] = 1023;
61 if (!s
->enabled
|| !s
->cpu_enabled
[cpu
]) {
62 qemu_irq_lower(s
->parent_irq
[cpu
]);
67 for (irq
= 0; irq
< s
->num_irq
; irq
++) {
68 if (GIC_TEST_ENABLED(irq
, cm
) && GIC_TEST_PENDING(irq
, cm
)) {
69 if (GIC_GET_PRIORITY(irq
, cpu
) < best_prio
) {
70 best_prio
= GIC_GET_PRIORITY(irq
, cpu
);
76 if (best_prio
<= s
->priority_mask
[cpu
]) {
77 s
->current_pending
[cpu
] = best_irq
;
78 if (best_prio
< s
->running_priority
[cpu
]) {
79 DPRINTF("Raised pending IRQ %d\n", best_irq
);
83 qemu_set_irq(s
->parent_irq
[cpu
], level
);
87 void gic_set_pending_private(GICState
*s
, int cpu
, int irq
)
91 if (GIC_TEST_PENDING(irq
, cm
))
94 DPRINTF("Set %d pending cpu %d\n", irq
, cpu
);
95 GIC_SET_PENDING(irq
, cm
);
99 /* Process a change in an external IRQ input. */
100 static void gic_set_irq(void *opaque
, int irq
, int level
)
102 /* Meaning of the 'irq' parameter:
103 * [0..N-1] : external interrupts
104 * [N..N+31] : PPI (internal) interrupts for CPU 0
105 * [N+32..N+63] : PPI (internal interrupts for CPU 1
108 GICState
*s
= (GICState
*)opaque
;
110 if (irq
< (s
->num_irq
- GIC_INTERNAL
)) {
111 /* The first external input line is internal interrupt 32. */
114 target
= GIC_TARGET(irq
);
117 irq
-= (s
->num_irq
- GIC_INTERNAL
);
118 cpu
= irq
/ GIC_INTERNAL
;
124 if (level
== GIC_TEST_LEVEL(irq
, cm
)) {
129 GIC_SET_LEVEL(irq
, cm
);
130 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
, cm
)) {
131 DPRINTF("Set %d pending mask %x\n", irq
, target
);
132 GIC_SET_PENDING(irq
, target
);
135 GIC_CLEAR_LEVEL(irq
, cm
);
140 static void gic_set_running_irq(GICState
*s
, int cpu
, int irq
)
142 s
->running_irq
[cpu
] = irq
;
144 s
->running_priority
[cpu
] = 0x100;
146 s
->running_priority
[cpu
] = GIC_GET_PRIORITY(irq
, cpu
);
151 uint32_t gic_acknowledge_irq(GICState
*s
, int cpu
)
155 new_irq
= s
->current_pending
[cpu
];
157 || GIC_GET_PRIORITY(new_irq
, cpu
) >= s
->running_priority
[cpu
]) {
158 DPRINTF("ACK no pending IRQ\n");
161 s
->last_active
[new_irq
][cpu
] = s
->running_irq
[cpu
];
162 /* Clear pending flags for both level and edge triggered interrupts.
163 Level triggered IRQs will be reasserted once they become inactive. */
164 GIC_CLEAR_PENDING(new_irq
, GIC_TEST_MODEL(new_irq
) ? ALL_CPU_MASK
: cm
);
165 gic_set_running_irq(s
, cpu
, new_irq
);
166 DPRINTF("ACK %d\n", new_irq
);
170 void gic_complete_irq(GICState
*s
, int cpu
, int irq
)
174 DPRINTF("EOI %d\n", irq
);
175 if (irq
>= s
->num_irq
) {
176 /* This handles two cases:
177 * 1. If software writes the ID of a spurious interrupt [ie 1023]
178 * to the GICC_EOIR, the GIC ignores that write.
179 * 2. If software writes the number of a non-existent interrupt
180 * this must be a subcase of "value written does not match the last
181 * valid interrupt value read from the Interrupt Acknowledge
182 * register" and so this is UNPREDICTABLE. We choose to ignore it.
186 if (s
->running_irq
[cpu
] == 1023)
187 return; /* No active IRQ. */
188 /* Mark level triggered interrupts as pending if they are still
190 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
, cm
)
191 && GIC_TEST_LEVEL(irq
, cm
) && (GIC_TARGET(irq
) & cm
) != 0) {
192 DPRINTF("Set %d pending mask %x\n", irq
, cm
);
193 GIC_SET_PENDING(irq
, cm
);
196 if (irq
!= s
->running_irq
[cpu
]) {
197 /* Complete an IRQ that is not currently running. */
198 int tmp
= s
->running_irq
[cpu
];
199 while (s
->last_active
[tmp
][cpu
] != 1023) {
200 if (s
->last_active
[tmp
][cpu
] == irq
) {
201 s
->last_active
[tmp
][cpu
] = s
->last_active
[irq
][cpu
];
204 tmp
= s
->last_active
[tmp
][cpu
];
210 /* Complete the current running IRQ. */
211 gic_set_running_irq(s
, cpu
, s
->last_active
[s
->running_irq
[cpu
]][cpu
]);
215 static uint32_t gic_dist_readb(void *opaque
, hwaddr offset
)
217 GICState
*s
= (GICState
*)opaque
;
225 cpu
= gic_get_current_cpu(s
);
227 if (offset
< 0x100) {
231 return ((s
->num_irq
/ 32) - 1) | ((NUM_CPU(s
) - 1) << 5);
234 if (offset
>= 0x80) {
235 /* Interrupt Security , RAZ/WI */
239 } else if (offset
< 0x200) {
240 /* Interrupt Set/Clear Enable. */
242 irq
= (offset
- 0x100) * 8;
244 irq
= (offset
- 0x180) * 8;
246 if (irq
>= s
->num_irq
)
249 for (i
= 0; i
< 8; i
++) {
250 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
254 } else if (offset
< 0x300) {
255 /* Interrupt Set/Clear Pending. */
257 irq
= (offset
- 0x200) * 8;
259 irq
= (offset
- 0x280) * 8;
261 if (irq
>= s
->num_irq
)
264 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
265 for (i
= 0; i
< 8; i
++) {
266 if (GIC_TEST_PENDING(irq
+ i
, mask
)) {
270 } else if (offset
< 0x400) {
271 /* Interrupt Active. */
272 irq
= (offset
- 0x300) * 8 + GIC_BASE_IRQ
;
273 if (irq
>= s
->num_irq
)
276 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
277 for (i
= 0; i
< 8; i
++) {
278 if (GIC_TEST_ACTIVE(irq
+ i
, mask
)) {
282 } else if (offset
< 0x800) {
283 /* Interrupt Priority. */
284 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
285 if (irq
>= s
->num_irq
)
287 res
= GIC_GET_PRIORITY(irq
, cpu
);
288 } else if (offset
< 0xc00) {
289 /* Interrupt CPU Target. */
290 if (s
->num_cpu
== 1 && s
->revision
!= REV_11MPCORE
) {
291 /* For uniprocessor GICs these RAZ/WI */
294 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
295 if (irq
>= s
->num_irq
) {
298 if (irq
>= 29 && irq
<= 31) {
301 res
= GIC_TARGET(irq
);
304 } else if (offset
< 0xf00) {
305 /* Interrupt Configuration. */
306 irq
= (offset
- 0xc00) * 2 + GIC_BASE_IRQ
;
307 if (irq
>= s
->num_irq
)
310 for (i
= 0; i
< 4; i
++) {
311 if (GIC_TEST_MODEL(irq
+ i
))
312 res
|= (1 << (i
* 2));
313 if (GIC_TEST_TRIGGER(irq
+ i
))
314 res
|= (2 << (i
* 2));
316 } else if (offset
< 0xfe0) {
318 } else /* offset >= 0xfe0 */ {
322 res
= gic_id
[(offset
- 0xfe0) >> 2];
327 hw_error("gic_dist_readb: Bad offset %x\n", (int)offset
);
331 static uint32_t gic_dist_readw(void *opaque
, hwaddr offset
)
334 val
= gic_dist_readb(opaque
, offset
);
335 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
339 static uint32_t gic_dist_readl(void *opaque
, hwaddr offset
)
342 val
= gic_dist_readw(opaque
, offset
);
343 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
347 static void gic_dist_writeb(void *opaque
, hwaddr offset
,
350 GICState
*s
= (GICState
*)opaque
;
355 cpu
= gic_get_current_cpu(s
);
356 if (offset
< 0x100) {
358 s
->enabled
= (value
& 1);
359 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
360 } else if (offset
< 4) {
362 } else if (offset
>= 0x80) {
363 /* Interrupt Security Registers, RAZ/WI */
367 } else if (offset
< 0x180) {
368 /* Interrupt Set Enable. */
369 irq
= (offset
- 0x100) * 8 + GIC_BASE_IRQ
;
370 if (irq
>= s
->num_irq
)
374 for (i
= 0; i
< 8; i
++) {
375 if (value
& (1 << i
)) {
376 int mask
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : GIC_TARGET(irq
);
377 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
379 if (!GIC_TEST_ENABLED(irq
+ i
, cm
)) {
380 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
382 GIC_SET_ENABLED(irq
+ i
, cm
);
383 /* If a raised level triggered IRQ enabled then mark
385 if (GIC_TEST_LEVEL(irq
+ i
, mask
)
386 && !GIC_TEST_TRIGGER(irq
+ i
)) {
387 DPRINTF("Set %d pending mask %x\n", irq
+ i
, mask
);
388 GIC_SET_PENDING(irq
+ i
, mask
);
392 } else if (offset
< 0x200) {
393 /* Interrupt Clear Enable. */
394 irq
= (offset
- 0x180) * 8 + GIC_BASE_IRQ
;
395 if (irq
>= s
->num_irq
)
399 for (i
= 0; i
< 8; i
++) {
400 if (value
& (1 << i
)) {
401 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
403 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
404 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
406 GIC_CLEAR_ENABLED(irq
+ i
, cm
);
409 } else if (offset
< 0x280) {
410 /* Interrupt Set Pending. */
411 irq
= (offset
- 0x200) * 8 + GIC_BASE_IRQ
;
412 if (irq
>= s
->num_irq
)
417 for (i
= 0; i
< 8; i
++) {
418 if (value
& (1 << i
)) {
419 GIC_SET_PENDING(irq
+ i
, GIC_TARGET(irq
));
422 } else if (offset
< 0x300) {
423 /* Interrupt Clear Pending. */
424 irq
= (offset
- 0x280) * 8 + GIC_BASE_IRQ
;
425 if (irq
>= s
->num_irq
)
427 for (i
= 0; i
< 8; i
++) {
428 /* ??? This currently clears the pending bit for all CPUs, even
429 for per-CPU interrupts. It's unclear whether this is the
431 if (value
& (1 << i
)) {
432 GIC_CLEAR_PENDING(irq
+ i
, ALL_CPU_MASK
);
435 } else if (offset
< 0x400) {
436 /* Interrupt Active. */
438 } else if (offset
< 0x800) {
439 /* Interrupt Priority. */
440 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
441 if (irq
>= s
->num_irq
)
443 if (irq
< GIC_INTERNAL
) {
444 s
->priority1
[irq
][cpu
] = value
;
446 s
->priority2
[irq
- GIC_INTERNAL
] = value
;
448 } else if (offset
< 0xc00) {
449 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
450 * annoying exception of the 11MPCore's GIC.
452 if (s
->num_cpu
!= 1 || s
->revision
== REV_11MPCORE
) {
453 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
454 if (irq
>= s
->num_irq
) {
459 } else if (irq
< GIC_INTERNAL
) {
460 value
= ALL_CPU_MASK
;
462 s
->irq_target
[irq
] = value
& ALL_CPU_MASK
;
464 } else if (offset
< 0xf00) {
465 /* Interrupt Configuration. */
466 irq
= (offset
- 0xc00) * 4 + GIC_BASE_IRQ
;
467 if (irq
>= s
->num_irq
)
469 if (irq
< GIC_INTERNAL
)
471 for (i
= 0; i
< 4; i
++) {
472 if (value
& (1 << (i
* 2))) {
473 GIC_SET_MODEL(irq
+ i
);
475 GIC_CLEAR_MODEL(irq
+ i
);
477 if (value
& (2 << (i
* 2))) {
478 GIC_SET_TRIGGER(irq
+ i
);
480 GIC_CLEAR_TRIGGER(irq
+ i
);
484 /* 0xf00 is only handled for 32-bit writes. */
490 hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset
);
493 static void gic_dist_writew(void *opaque
, hwaddr offset
,
496 gic_dist_writeb(opaque
, offset
, value
& 0xff);
497 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
500 static void gic_dist_writel(void *opaque
, hwaddr offset
,
503 GICState
*s
= (GICState
*)opaque
;
504 if (offset
== 0xf00) {
509 cpu
= gic_get_current_cpu(s
);
511 switch ((value
>> 24) & 3) {
513 mask
= (value
>> 16) & ALL_CPU_MASK
;
516 mask
= ALL_CPU_MASK
^ (1 << cpu
);
522 DPRINTF("Bad Soft Int target filter\n");
526 GIC_SET_PENDING(irq
, mask
);
530 gic_dist_writew(opaque
, offset
, value
& 0xffff);
531 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
534 static const MemoryRegionOps gic_dist_ops
= {
536 .read
= { gic_dist_readb
, gic_dist_readw
, gic_dist_readl
, },
537 .write
= { gic_dist_writeb
, gic_dist_writew
, gic_dist_writel
, },
539 .endianness
= DEVICE_NATIVE_ENDIAN
,
542 static uint32_t gic_cpu_read(GICState
*s
, int cpu
, int offset
)
545 case 0x00: /* Control */
546 return s
->cpu_enabled
[cpu
];
547 case 0x04: /* Priority mask */
548 return s
->priority_mask
[cpu
];
549 case 0x08: /* Binary Point */
550 /* ??? Not implemented. */
552 case 0x0c: /* Acknowledge */
553 return gic_acknowledge_irq(s
, cpu
);
554 case 0x14: /* Running Priority */
555 return s
->running_priority
[cpu
];
556 case 0x18: /* Highest Pending Interrupt */
557 return s
->current_pending
[cpu
];
559 hw_error("gic_cpu_read: Bad offset %x\n", (int)offset
);
564 static void gic_cpu_write(GICState
*s
, int cpu
, int offset
, uint32_t value
)
567 case 0x00: /* Control */
568 s
->cpu_enabled
[cpu
] = (value
& 1);
569 DPRINTF("CPU %d %sabled\n", cpu
, s
->cpu_enabled
[cpu
] ? "En" : "Dis");
571 case 0x04: /* Priority mask */
572 s
->priority_mask
[cpu
] = (value
& 0xff);
574 case 0x08: /* Binary Point */
575 /* ??? Not implemented. */
577 case 0x10: /* End Of Interrupt */
578 return gic_complete_irq(s
, cpu
, value
& 0x3ff);
580 hw_error("gic_cpu_write: Bad offset %x\n", (int)offset
);
586 /* Wrappers to read/write the GIC CPU interface for the current CPU */
587 static uint64_t gic_thiscpu_read(void *opaque
, hwaddr addr
,
590 GICState
*s
= (GICState
*)opaque
;
591 return gic_cpu_read(s
, gic_get_current_cpu(s
), addr
);
594 static void gic_thiscpu_write(void *opaque
, hwaddr addr
,
595 uint64_t value
, unsigned size
)
597 GICState
*s
= (GICState
*)opaque
;
598 gic_cpu_write(s
, gic_get_current_cpu(s
), addr
, value
);
601 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
602 * These just decode the opaque pointer into GICState* + cpu id.
604 static uint64_t gic_do_cpu_read(void *opaque
, hwaddr addr
,
607 GICState
**backref
= (GICState
**)opaque
;
608 GICState
*s
= *backref
;
609 int id
= (backref
- s
->backref
);
610 return gic_cpu_read(s
, id
, addr
);
613 static void gic_do_cpu_write(void *opaque
, hwaddr addr
,
614 uint64_t value
, unsigned size
)
616 GICState
**backref
= (GICState
**)opaque
;
617 GICState
*s
= *backref
;
618 int id
= (backref
- s
->backref
);
619 gic_cpu_write(s
, id
, addr
, value
);
622 static const MemoryRegionOps gic_thiscpu_ops
= {
623 .read
= gic_thiscpu_read
,
624 .write
= gic_thiscpu_write
,
625 .endianness
= DEVICE_NATIVE_ENDIAN
,
628 static const MemoryRegionOps gic_cpu_ops
= {
629 .read
= gic_do_cpu_read
,
630 .write
= gic_do_cpu_write
,
631 .endianness
= DEVICE_NATIVE_ENDIAN
,
634 void gic_init_irqs_and_distributor(GICState
*s
, int num_irq
)
638 i
= s
->num_irq
- GIC_INTERNAL
;
639 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
640 * GPIO array layout is thus:
642 * [N..N+31] PPIs for CPU 0
643 * [N+32..N+63] PPIs for CPU 1
646 if (s
->revision
!= REV_NVIC
) {
647 i
+= (GIC_INTERNAL
* s
->num_cpu
);
649 qdev_init_gpio_in(&s
->busdev
.qdev
, gic_set_irq
, i
);
650 for (i
= 0; i
< NUM_CPU(s
); i
++) {
651 sysbus_init_irq(&s
->busdev
, &s
->parent_irq
[i
]);
653 memory_region_init_io(&s
->iomem
, &gic_dist_ops
, s
, "gic_dist", 0x1000);
656 static int arm_gic_init(SysBusDevice
*dev
)
658 /* Device instance init function for the GIC sysbus device */
660 GICState
*s
= FROM_SYSBUS(GICState
, dev
);
661 ARMGICClass
*agc
= ARM_GIC_GET_CLASS(s
);
663 agc
->parent_init(dev
);
665 gic_init_irqs_and_distributor(s
, s
->num_irq
);
667 /* Memory regions for the CPU interfaces (NVIC doesn't have these):
668 * a region for "CPU interface for this core", then a region for
669 * "CPU interface for core 0", "for core 1", ...
670 * NB that the memory region size of 0x100 applies for the 11MPCore
671 * and also cores following the GIC v1 spec (ie A9).
672 * GIC v2 defines a larger memory region (0x1000) so this will need
673 * to be extended when we implement A15.
675 memory_region_init_io(&s
->cpuiomem
[0], &gic_thiscpu_ops
, s
,
677 for (i
= 0; i
< NUM_CPU(s
); i
++) {
679 memory_region_init_io(&s
->cpuiomem
[i
+1], &gic_cpu_ops
, &s
->backref
[i
],
683 sysbus_init_mmio(dev
, &s
->iomem
);
684 /* cpu interfaces (one for "current cpu" plus one per cpu) */
685 for (i
= 0; i
<= NUM_CPU(s
); i
++) {
686 sysbus_init_mmio(dev
, &s
->cpuiomem
[i
]);
691 static void arm_gic_class_init(ObjectClass
*klass
, void *data
)
693 DeviceClass
*dc
= DEVICE_CLASS(klass
);
694 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(klass
);
695 ARMGICClass
*agc
= ARM_GIC_CLASS(klass
);
696 agc
->parent_init
= sbc
->init
;
697 sbc
->init
= arm_gic_init
;
701 static TypeInfo arm_gic_info
= {
702 .name
= TYPE_ARM_GIC
,
703 .parent
= TYPE_ARM_GIC_COMMON
,
704 .instance_size
= sizeof(GICState
),
705 .class_init
= arm_gic_class_init
,
706 .class_size
= sizeof(ARMGICClass
),
709 static void arm_gic_register_types(void)
711 type_register_static(&arm_gic_info
);
714 type_init(arm_gic_register_types
)