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. */
17 #define DPRINTF(fmt, ...) \
18 do { printf("arm_gic: " fmt , ## __VA_ARGS__); } while (0)
20 #define DPRINTF(fmt, ...) do {} while(0)
24 static const uint8_t gic_id
[] =
25 { 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 };
26 /* The NVIC has 16 internal vectors. However these are not exposed
27 through the normal GIC interface. */
28 #define GIC_BASE_IRQ 32
30 static const uint8_t gic_id
[] =
31 { 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
32 #define GIC_BASE_IRQ 0
35 #define FROM_SYSBUSGIC(type, dev) \
36 DO_UPCAST(type, gic, FROM_SYSBUS(gic_state, dev))
38 typedef struct gic_irq_state
40 /* ??? The documentation seems to imply the enable bits are global, even
41 for per-cpu interrupts. This seems strange. */
43 unsigned pending
:NCPU
;
46 unsigned model
:1; /* 0 = N:N, 1 = 1:N */
47 unsigned trigger
:1; /* nonzero = edge triggered. */
50 #define ALL_CPU_MASK ((1 << NCPU) - 1)
52 #define NUM_CPU(s) ((s)->num_cpu)
57 #define GIC_SET_ENABLED(irq) s->irq_state[irq].enabled = 1
58 #define GIC_CLEAR_ENABLED(irq) s->irq_state[irq].enabled = 0
59 #define GIC_TEST_ENABLED(irq) s->irq_state[irq].enabled
60 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
61 #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
62 #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
63 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
64 #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
65 #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
66 #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
67 #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
68 #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
69 #define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
70 #define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
71 #define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
72 #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
73 #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
74 #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
75 #define GIC_GET_PRIORITY(irq, cpu) \
76 (((irq) < 32) ? s->priority1[irq][cpu] : s->priority2[(irq) - 32])
78 #define GIC_TARGET(irq) 1
80 #define GIC_TARGET(irq) s->irq_target[irq]
83 typedef struct gic_state
86 qemu_irq parent_irq
[NCPU
];
88 int cpu_enabled
[NCPU
];
90 gic_irq_state irq_state
[GIC_NIRQ
];
92 int irq_target
[GIC_NIRQ
];
94 int priority1
[32][NCPU
];
95 int priority2
[GIC_NIRQ
- 32];
96 int last_active
[GIC_NIRQ
][NCPU
];
98 int priority_mask
[NCPU
];
99 int running_irq
[NCPU
];
100 int running_priority
[NCPU
];
101 int current_pending
[NCPU
];
110 /* TODO: Many places that call this routine could be optimized. */
111 /* Update interrupt status after enabled or pending bits have been changed. */
112 static void gic_update(gic_state
*s
)
121 for (cpu
= 0; cpu
< NUM_CPU(s
); cpu
++) {
123 s
->current_pending
[cpu
] = 1023;
124 if (!s
->enabled
|| !s
->cpu_enabled
[cpu
]) {
125 qemu_irq_lower(s
->parent_irq
[cpu
]);
130 for (irq
= 0; irq
< GIC_NIRQ
; irq
++) {
131 if (GIC_TEST_ENABLED(irq
) && GIC_TEST_PENDING(irq
, cm
)) {
132 if (GIC_GET_PRIORITY(irq
, cpu
) < best_prio
) {
133 best_prio
= GIC_GET_PRIORITY(irq
, cpu
);
139 if (best_prio
<= s
->priority_mask
[cpu
]) {
140 s
->current_pending
[cpu
] = best_irq
;
141 if (best_prio
< s
->running_priority
[cpu
]) {
142 DPRINTF("Raised pending IRQ %d\n", best_irq
);
146 qemu_set_irq(s
->parent_irq
[cpu
], level
);
150 static void __attribute__((unused
))
151 gic_set_pending_private(gic_state
*s
, int cpu
, int irq
)
155 if (GIC_TEST_PENDING(irq
, cm
))
158 DPRINTF("Set %d pending cpu %d\n", irq
, cpu
);
159 GIC_SET_PENDING(irq
, cm
);
163 /* Process a change in an external IRQ input. */
164 static void gic_set_irq(void *opaque
, int irq
, int level
)
166 gic_state
*s
= (gic_state
*)opaque
;
167 /* The first external input line is internal interrupt 32. */
169 if (level
== GIC_TEST_LEVEL(irq
, ALL_CPU_MASK
))
173 GIC_SET_LEVEL(irq
, ALL_CPU_MASK
);
174 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
)) {
175 DPRINTF("Set %d pending mask %x\n", irq
, GIC_TARGET(irq
));
176 GIC_SET_PENDING(irq
, GIC_TARGET(irq
));
179 GIC_CLEAR_LEVEL(irq
, ALL_CPU_MASK
);
184 static void gic_set_running_irq(gic_state
*s
, int cpu
, int irq
)
186 s
->running_irq
[cpu
] = irq
;
188 s
->running_priority
[cpu
] = 0x100;
190 s
->running_priority
[cpu
] = GIC_GET_PRIORITY(irq
, cpu
);
195 static uint32_t gic_acknowledge_irq(gic_state
*s
, int cpu
)
199 new_irq
= s
->current_pending
[cpu
];
201 || GIC_GET_PRIORITY(new_irq
, cpu
) >= s
->running_priority
[cpu
]) {
202 DPRINTF("ACK no pending IRQ\n");
205 s
->last_active
[new_irq
][cpu
] = s
->running_irq
[cpu
];
206 /* Clear pending flags for both level and edge triggered interrupts.
207 Level triggered IRQs will be reasserted once they become inactive. */
208 GIC_CLEAR_PENDING(new_irq
, GIC_TEST_MODEL(new_irq
) ? ALL_CPU_MASK
: cm
);
209 gic_set_running_irq(s
, cpu
, new_irq
);
210 DPRINTF("ACK %d\n", new_irq
);
214 static void gic_complete_irq(gic_state
* s
, int cpu
, int irq
)
218 DPRINTF("EOI %d\n", irq
);
219 if (s
->running_irq
[cpu
] == 1023)
220 return; /* No active IRQ. */
222 /* Mark level triggered interrupts as pending if they are still
224 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
)
225 && GIC_TEST_LEVEL(irq
, cm
) && (GIC_TARGET(irq
) & cm
) != 0) {
226 DPRINTF("Set %d pending mask %x\n", irq
, cm
);
227 GIC_SET_PENDING(irq
, cm
);
231 if (irq
!= s
->running_irq
[cpu
]) {
232 /* Complete an IRQ that is not currently running. */
233 int tmp
= s
->running_irq
[cpu
];
234 while (s
->last_active
[tmp
][cpu
] != 1023) {
235 if (s
->last_active
[tmp
][cpu
] == irq
) {
236 s
->last_active
[tmp
][cpu
] = s
->last_active
[irq
][cpu
];
239 tmp
= s
->last_active
[tmp
][cpu
];
245 /* Complete the current running IRQ. */
246 gic_set_running_irq(s
, cpu
, s
->last_active
[s
->running_irq
[cpu
]][cpu
]);
250 static uint32_t gic_dist_readb(void *opaque
, target_phys_addr_t offset
)
252 gic_state
*s
= (gic_state
*)opaque
;
260 cpu
= gic_get_current_cpu();
262 if (offset
< 0x100) {
267 return ((GIC_NIRQ
/ 32) - 1) | ((NUM_CPU(s
) - 1) << 5);
272 } else if (offset
< 0x200) {
273 /* Interrupt Set/Clear Enable. */
275 irq
= (offset
- 0x100) * 8;
277 irq
= (offset
- 0x180) * 8;
282 for (i
= 0; i
< 8; i
++) {
283 if (GIC_TEST_ENABLED(irq
+ i
)) {
287 } else if (offset
< 0x300) {
288 /* Interrupt Set/Clear Pending. */
290 irq
= (offset
- 0x200) * 8;
292 irq
= (offset
- 0x280) * 8;
297 mask
= (irq
< 32) ? cm
: ALL_CPU_MASK
;
298 for (i
= 0; i
< 8; i
++) {
299 if (GIC_TEST_PENDING(irq
+ i
, mask
)) {
303 } else if (offset
< 0x400) {
304 /* Interrupt Active. */
305 irq
= (offset
- 0x300) * 8 + GIC_BASE_IRQ
;
309 mask
= (irq
< 32) ? cm
: ALL_CPU_MASK
;
310 for (i
= 0; i
< 8; i
++) {
311 if (GIC_TEST_ACTIVE(irq
+ i
, mask
)) {
315 } else if (offset
< 0x800) {
316 /* Interrupt Priority. */
317 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
320 res
= GIC_GET_PRIORITY(irq
, cpu
);
322 } else if (offset
< 0xc00) {
323 /* Interrupt CPU Target. */
324 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
327 if (irq
>= 29 && irq
<= 31) {
330 res
= GIC_TARGET(irq
);
332 } else if (offset
< 0xf00) {
333 /* Interrupt Configuration. */
334 irq
= (offset
- 0xc00) * 2 + GIC_BASE_IRQ
;
338 for (i
= 0; i
< 4; i
++) {
339 if (GIC_TEST_MODEL(irq
+ i
))
340 res
|= (1 << (i
* 2));
341 if (GIC_TEST_TRIGGER(irq
+ i
))
342 res
|= (2 << (i
* 2));
345 } else if (offset
< 0xfe0) {
347 } else /* offset >= 0xfe0 */ {
351 res
= gic_id
[(offset
- 0xfe0) >> 2];
356 hw_error("gic_dist_readb: Bad offset %x\n", (int)offset
);
360 static uint32_t gic_dist_readw(void *opaque
, target_phys_addr_t offset
)
363 val
= gic_dist_readb(opaque
, offset
);
364 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
368 static uint32_t gic_dist_readl(void *opaque
, target_phys_addr_t offset
)
372 gic_state
*s
= (gic_state
*)opaque
;
375 if (addr
< 0x100 || addr
> 0xd00)
376 return nvic_readl(s
, addr
);
378 val
= gic_dist_readw(opaque
, offset
);
379 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
383 static void gic_dist_writeb(void *opaque
, target_phys_addr_t offset
,
386 gic_state
*s
= (gic_state
*)opaque
;
391 cpu
= gic_get_current_cpu();
392 if (offset
< 0x100) {
397 s
->enabled
= (value
& 1);
398 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
399 } else if (offset
< 4) {
405 } else if (offset
< 0x180) {
406 /* Interrupt Set Enable. */
407 irq
= (offset
- 0x100) * 8 + GIC_BASE_IRQ
;
412 for (i
= 0; i
< 8; i
++) {
413 if (value
& (1 << i
)) {
414 int mask
= (irq
< 32) ? (1 << cpu
) : GIC_TARGET(irq
);
415 if (!GIC_TEST_ENABLED(irq
+ i
))
416 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
417 GIC_SET_ENABLED(irq
+ i
);
418 /* If a raised level triggered IRQ enabled then mark
420 if (GIC_TEST_LEVEL(irq
+ i
, mask
)
421 && !GIC_TEST_TRIGGER(irq
+ i
)) {
422 DPRINTF("Set %d pending mask %x\n", irq
+ i
, mask
);
423 GIC_SET_PENDING(irq
+ i
, mask
);
427 } else if (offset
< 0x200) {
428 /* Interrupt Clear Enable. */
429 irq
= (offset
- 0x180) * 8 + GIC_BASE_IRQ
;
434 for (i
= 0; i
< 8; i
++) {
435 if (value
& (1 << i
)) {
436 if (GIC_TEST_ENABLED(irq
+ i
))
437 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
438 GIC_CLEAR_ENABLED(irq
+ i
);
441 } else if (offset
< 0x280) {
442 /* Interrupt Set Pending. */
443 irq
= (offset
- 0x200) * 8 + GIC_BASE_IRQ
;
449 for (i
= 0; i
< 8; i
++) {
450 if (value
& (1 << i
)) {
451 GIC_SET_PENDING(irq
+ i
, GIC_TARGET(irq
));
454 } else if (offset
< 0x300) {
455 /* Interrupt Clear Pending. */
456 irq
= (offset
- 0x280) * 8 + GIC_BASE_IRQ
;
459 for (i
= 0; i
< 8; i
++) {
460 /* ??? This currently clears the pending bit for all CPUs, even
461 for per-CPU interrupts. It's unclear whether this is the
463 if (value
& (1 << i
)) {
464 GIC_CLEAR_PENDING(irq
+ i
, ALL_CPU_MASK
);
467 } else if (offset
< 0x400) {
468 /* Interrupt Active. */
470 } else if (offset
< 0x800) {
471 /* Interrupt Priority. */
472 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
476 s
->priority1
[irq
][cpu
] = value
;
478 s
->priority2
[irq
- 32] = value
;
481 } else if (offset
< 0xc00) {
482 /* Interrupt CPU Target. */
483 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
489 value
= ALL_CPU_MASK
;
490 s
->irq_target
[irq
] = value
& ALL_CPU_MASK
;
491 } else if (offset
< 0xf00) {
492 /* Interrupt Configuration. */
493 irq
= (offset
- 0xc00) * 4 + GIC_BASE_IRQ
;
498 for (i
= 0; i
< 4; i
++) {
499 if (value
& (1 << (i
* 2))) {
500 GIC_SET_MODEL(irq
+ i
);
502 GIC_CLEAR_MODEL(irq
+ i
);
504 if (value
& (2 << (i
* 2))) {
505 GIC_SET_TRIGGER(irq
+ i
);
507 GIC_CLEAR_TRIGGER(irq
+ i
);
512 /* 0xf00 is only handled for 32-bit writes. */
518 hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset
);
521 static void gic_dist_writew(void *opaque
, target_phys_addr_t offset
,
524 gic_dist_writeb(opaque
, offset
, value
& 0xff);
525 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
528 static void gic_dist_writel(void *opaque
, target_phys_addr_t offset
,
531 gic_state
*s
= (gic_state
*)opaque
;
535 if (addr
< 0x100 || (addr
> 0xd00 && addr
!= 0xf00)) {
536 nvic_writel(s
, addr
, value
);
540 if (offset
== 0xf00) {
545 cpu
= gic_get_current_cpu();
547 switch ((value
>> 24) & 3) {
549 mask
= (value
>> 16) & ALL_CPU_MASK
;
552 mask
= ALL_CPU_MASK
^ (1 << cpu
);
558 DPRINTF("Bad Soft Int target filter\n");
562 GIC_SET_PENDING(irq
, mask
);
566 gic_dist_writew(opaque
, offset
, value
& 0xffff);
567 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
570 static CPUReadMemoryFunc
* const gic_dist_readfn
[] = {
576 static CPUWriteMemoryFunc
* const gic_dist_writefn
[] = {
583 static uint32_t gic_cpu_read(gic_state
*s
, int cpu
, int offset
)
586 case 0x00: /* Control */
587 return s
->cpu_enabled
[cpu
];
588 case 0x04: /* Priority mask */
589 return s
->priority_mask
[cpu
];
590 case 0x08: /* Binary Point */
591 /* ??? Not implemented. */
593 case 0x0c: /* Acknowledge */
594 return gic_acknowledge_irq(s
, cpu
);
595 case 0x14: /* Runing Priority */
596 return s
->running_priority
[cpu
];
597 case 0x18: /* Highest Pending Interrupt */
598 return s
->current_pending
[cpu
];
600 hw_error("gic_cpu_read: Bad offset %x\n", (int)offset
);
605 static void gic_cpu_write(gic_state
*s
, int cpu
, int offset
, uint32_t value
)
608 case 0x00: /* Control */
609 s
->cpu_enabled
[cpu
] = (value
& 1);
610 DPRINTF("CPU %d %sabled\n", cpu
, s
->cpu_enabled
? "En" : "Dis");
612 case 0x04: /* Priority mask */
613 s
->priority_mask
[cpu
] = (value
& 0xff);
615 case 0x08: /* Binary Point */
616 /* ??? Not implemented. */
618 case 0x10: /* End Of Interrupt */
619 return gic_complete_irq(s
, cpu
, value
& 0x3ff);
621 hw_error("gic_cpu_write: Bad offset %x\n", (int)offset
);
628 static void gic_reset(gic_state
*s
)
631 memset(s
->irq_state
, 0, GIC_NIRQ
* sizeof(gic_irq_state
));
632 for (i
= 0 ; i
< NUM_CPU(s
); i
++) {
633 s
->priority_mask
[i
] = 0xf0;
634 s
->current_pending
[i
] = 1023;
635 s
->running_irq
[i
] = 1023;
636 s
->running_priority
[i
] = 0x100;
638 /* The NVIC doesn't have per-cpu interfaces, so enable by default. */
639 s
->cpu_enabled
[i
] = 1;
641 s
->cpu_enabled
[i
] = 0;
644 for (i
= 0; i
< 16; i
++) {
649 /* The NVIC is always enabled. */
656 static void gic_save(QEMUFile
*f
, void *opaque
)
658 gic_state
*s
= (gic_state
*)opaque
;
662 qemu_put_be32(f
, s
->enabled
);
663 for (i
= 0; i
< NUM_CPU(s
); i
++) {
664 qemu_put_be32(f
, s
->cpu_enabled
[i
]);
666 qemu_put_be32(f
, s
->irq_target
[i
]);
668 for (j
= 0; j
< 32; j
++)
669 qemu_put_be32(f
, s
->priority1
[j
][i
]);
670 for (j
= 0; j
< GIC_NIRQ
; j
++)
671 qemu_put_be32(f
, s
->last_active
[j
][i
]);
672 qemu_put_be32(f
, s
->priority_mask
[i
]);
673 qemu_put_be32(f
, s
->running_irq
[i
]);
674 qemu_put_be32(f
, s
->running_priority
[i
]);
675 qemu_put_be32(f
, s
->current_pending
[i
]);
677 for (i
= 0; i
< GIC_NIRQ
- 32; i
++) {
678 qemu_put_be32(f
, s
->priority2
[i
]);
680 for (i
= 0; i
< GIC_NIRQ
; i
++) {
681 qemu_put_byte(f
, s
->irq_state
[i
].enabled
);
682 qemu_put_byte(f
, s
->irq_state
[i
].pending
);
683 qemu_put_byte(f
, s
->irq_state
[i
].active
);
684 qemu_put_byte(f
, s
->irq_state
[i
].level
);
685 qemu_put_byte(f
, s
->irq_state
[i
].model
);
686 qemu_put_byte(f
, s
->irq_state
[i
].trigger
);
690 static int gic_load(QEMUFile
*f
, void *opaque
, int version_id
)
692 gic_state
*s
= (gic_state
*)opaque
;
699 s
->enabled
= qemu_get_be32(f
);
700 for (i
= 0; i
< NUM_CPU(s
); i
++) {
701 s
->cpu_enabled
[i
] = qemu_get_be32(f
);
703 s
->irq_target
[i
] = qemu_get_be32(f
);
705 for (j
= 0; j
< 32; j
++)
706 s
->priority1
[j
][i
] = qemu_get_be32(f
);
707 for (j
= 0; j
< GIC_NIRQ
; j
++)
708 s
->last_active
[j
][i
] = qemu_get_be32(f
);
709 s
->priority_mask
[i
] = qemu_get_be32(f
);
710 s
->running_irq
[i
] = qemu_get_be32(f
);
711 s
->running_priority
[i
] = qemu_get_be32(f
);
712 s
->current_pending
[i
] = qemu_get_be32(f
);
714 for (i
= 0; i
< GIC_NIRQ
- 32; i
++) {
715 s
->priority2
[i
] = qemu_get_be32(f
);
717 for (i
= 0; i
< GIC_NIRQ
; i
++) {
718 s
->irq_state
[i
].enabled
= qemu_get_byte(f
);
719 s
->irq_state
[i
].pending
= qemu_get_byte(f
);
720 s
->irq_state
[i
].active
= qemu_get_byte(f
);
721 s
->irq_state
[i
].level
= qemu_get_byte(f
);
722 s
->irq_state
[i
].model
= qemu_get_byte(f
);
723 s
->irq_state
[i
].trigger
= qemu_get_byte(f
);
730 static void gic_init(gic_state
*s
, int num_cpu
)
732 static void gic_init(gic_state
*s
)
738 s
->num_cpu
= num_cpu
;
740 qdev_init_gpio_in(&s
->busdev
.qdev
, gic_set_irq
, GIC_NIRQ
- 32);
741 for (i
= 0; i
< NUM_CPU(s
); i
++) {
742 sysbus_init_irq(&s
->busdev
, &s
->parent_irq
[i
]);
744 s
->iomemtype
= cpu_register_io_memory(gic_dist_readfn
,
746 DEVICE_NATIVE_ENDIAN
);
748 register_savevm(NULL
, "arm_gic", -1, 1, gic_save
, gic_load
, s
);