2 * ARM Generic/Distributed Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced 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, args...) \
18 do { printf("arm_gic: " fmt , ##args); } while (0)
20 #define DPRINTF(fmt, args...) 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 typedef struct gic_irq_state
37 /* ??? The documentation seems to imply the enable bits are global, even
38 for per-cpu interrupts. This seems strange. */
40 unsigned pending
:NCPU
;
43 unsigned model
:1; /* 0 = N:N, 1 = 1:N */
44 unsigned trigger
:1; /* nonzero = edge triggered. */
47 #define ALL_CPU_MASK ((1 << NCPU) - 1)
49 #define GIC_SET_ENABLED(irq) s->irq_state[irq].enabled = 1
50 #define GIC_CLEAR_ENABLED(irq) s->irq_state[irq].enabled = 0
51 #define GIC_TEST_ENABLED(irq) s->irq_state[irq].enabled
52 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
53 #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
54 #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
55 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
56 #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
57 #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
58 #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
59 #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
60 #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
61 #define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
62 #define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
63 #define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
64 #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
65 #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
66 #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
67 #define GIC_GET_PRIORITY(irq, cpu) \
68 (((irq) < 32) ? s->priority1[irq][cpu] : s->priority2[(irq) - 32])
70 #define GIC_TARGET(irq) 1
72 #define GIC_TARGET(irq) s->irq_target[irq]
75 typedef struct gic_state
77 qemu_irq parent_irq
[NCPU
];
79 int cpu_enabled
[NCPU
];
81 gic_irq_state irq_state
[GIC_NIRQ
];
83 int irq_target
[GIC_NIRQ
];
85 int priority1
[32][NCPU
];
86 int priority2
[GIC_NIRQ
- 32];
87 int last_active
[GIC_NIRQ
][NCPU
];
89 int priority_mask
[NCPU
];
90 int running_irq
[NCPU
];
91 int running_priority
[NCPU
];
92 int current_pending
[NCPU
];
100 /* TODO: Many places that call this routine could be optimized. */
101 /* Update interrupt status after enabled or pending bits have been changed. */
102 static void gic_update(gic_state
*s
)
111 for (cpu
= 0; cpu
< NCPU
; cpu
++) {
113 s
->current_pending
[cpu
] = 1023;
114 if (!s
->enabled
|| !s
->cpu_enabled
[cpu
]) {
115 qemu_irq_lower(s
->parent_irq
[cpu
]);
120 for (irq
= 0; irq
< GIC_NIRQ
; irq
++) {
121 if (GIC_TEST_ENABLED(irq
) && GIC_TEST_PENDING(irq
, cm
)) {
122 if (GIC_GET_PRIORITY(irq
, cpu
) < best_prio
) {
123 best_prio
= GIC_GET_PRIORITY(irq
, cpu
);
129 if (best_prio
<= s
->priority_mask
[cpu
]) {
130 s
->current_pending
[cpu
] = best_irq
;
131 if (best_prio
< s
->running_priority
[cpu
]) {
132 DPRINTF("Raised pending IRQ %d\n", best_irq
);
136 qemu_set_irq(s
->parent_irq
[cpu
], level
);
140 static void __attribute__((unused
))
141 gic_set_pending_private(gic_state
*s
, int cpu
, int irq
)
145 if (GIC_TEST_PENDING(irq
, cm
))
148 DPRINTF("Set %d pending cpu %d\n", irq
, cpu
);
149 GIC_SET_PENDING(irq
, cm
);
153 /* Process a change in an external IRQ input. */
154 static void gic_set_irq(void *opaque
, int irq
, int level
)
156 gic_state
*s
= (gic_state
*)opaque
;
157 /* The first external input line is internal interrupt 32. */
159 if (level
== GIC_TEST_LEVEL(irq
, ALL_CPU_MASK
))
163 GIC_SET_LEVEL(irq
, ALL_CPU_MASK
);
164 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
)) {
165 DPRINTF("Set %d pending mask %x\n", irq
, GIC_TARGET(irq
));
166 GIC_SET_PENDING(irq
, GIC_TARGET(irq
));
169 GIC_CLEAR_LEVEL(irq
, ALL_CPU_MASK
);
174 static void gic_set_running_irq(gic_state
*s
, int cpu
, int irq
)
176 s
->running_irq
[cpu
] = irq
;
178 s
->running_priority
[cpu
] = 0x100;
180 s
->running_priority
[cpu
] = GIC_GET_PRIORITY(irq
, cpu
);
185 static uint32_t gic_acknowledge_irq(gic_state
*s
, int cpu
)
189 new_irq
= s
->current_pending
[cpu
];
191 || GIC_GET_PRIORITY(new_irq
, cpu
) >= s
->running_priority
[cpu
]) {
192 DPRINTF("ACK no pending IRQ\n");
195 s
->last_active
[new_irq
][cpu
] = s
->running_irq
[cpu
];
196 /* Clear pending flags for both level and edge triggered interrupts.
197 Level triggered IRQs will be reasserted once they become inactive. */
198 GIC_CLEAR_PENDING(new_irq
, GIC_TEST_MODEL(new_irq
) ? ALL_CPU_MASK
: cm
);
199 gic_set_running_irq(s
, cpu
, new_irq
);
200 DPRINTF("ACK %d\n", new_irq
);
204 static void gic_complete_irq(gic_state
* s
, int cpu
, int irq
)
208 DPRINTF("EOI %d\n", irq
);
209 if (s
->running_irq
[cpu
] == 1023)
210 return; /* No active IRQ. */
212 /* Mark level triggered interrupts as pending if they are still
214 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
)
215 && GIC_TEST_LEVEL(irq
, cm
) && (GIC_TARGET(irq
) & cm
) != 0) {
216 DPRINTF("Set %d pending mask %x\n", irq
, cm
);
217 GIC_SET_PENDING(irq
, cm
);
221 if (irq
!= s
->running_irq
[cpu
]) {
222 /* Complete an IRQ that is not currently running. */
223 int tmp
= s
->running_irq
[cpu
];
224 while (s
->last_active
[tmp
][cpu
] != 1023) {
225 if (s
->last_active
[tmp
][cpu
] == irq
) {
226 s
->last_active
[tmp
][cpu
] = s
->last_active
[irq
][cpu
];
229 tmp
= s
->last_active
[tmp
][cpu
];
235 /* Complete the current running IRQ. */
236 gic_set_running_irq(s
, cpu
, s
->last_active
[s
->running_irq
[cpu
]][cpu
]);
240 static uint32_t gic_dist_readb(void *opaque
, target_phys_addr_t offset
)
242 gic_state
*s
= (gic_state
*)opaque
;
250 cpu
= gic_get_current_cpu();
252 if (offset
< 0x100) {
257 return ((GIC_NIRQ
/ 32) - 1) | ((NCPU
- 1) << 5);
262 } else if (offset
< 0x200) {
263 /* Interrupt Set/Clear Enable. */
265 irq
= (offset
- 0x100) * 8;
267 irq
= (offset
- 0x180) * 8;
272 for (i
= 0; i
< 8; i
++) {
273 if (GIC_TEST_ENABLED(irq
+ i
)) {
277 } else if (offset
< 0x300) {
278 /* Interrupt Set/Clear Pending. */
280 irq
= (offset
- 0x200) * 8;
282 irq
= (offset
- 0x280) * 8;
287 mask
= (irq
< 32) ? cm
: ALL_CPU_MASK
;
288 for (i
= 0; i
< 8; i
++) {
289 if (GIC_TEST_PENDING(irq
+ i
, mask
)) {
293 } else if (offset
< 0x400) {
294 /* Interrupt Active. */
295 irq
= (offset
- 0x300) * 8 + GIC_BASE_IRQ
;
299 mask
= (irq
< 32) ? cm
: ALL_CPU_MASK
;
300 for (i
= 0; i
< 8; i
++) {
301 if (GIC_TEST_ACTIVE(irq
+ i
, mask
)) {
305 } else if (offset
< 0x800) {
306 /* Interrupt Priority. */
307 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
310 res
= GIC_GET_PRIORITY(irq
, cpu
);
312 } else if (offset
< 0xc00) {
313 /* Interrupt CPU Target. */
314 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
317 if (irq
>= 29 && irq
<= 31) {
320 res
= GIC_TARGET(irq
);
322 } else if (offset
< 0xf00) {
323 /* Interrupt Configuration. */
324 irq
= (offset
- 0xc00) * 2 + GIC_BASE_IRQ
;
328 for (i
= 0; i
< 4; i
++) {
329 if (GIC_TEST_MODEL(irq
+ i
))
330 res
|= (1 << (i
* 2));
331 if (GIC_TEST_TRIGGER(irq
+ i
))
332 res
|= (2 << (i
* 2));
335 } else if (offset
< 0xfe0) {
337 } else /* offset >= 0xfe0 */ {
341 res
= gic_id
[(offset
- 0xfe0) >> 2];
346 cpu_abort(cpu_single_env
, "gic_dist_readb: Bad offset %x\n", (int)offset
);
350 static uint32_t gic_dist_readw(void *opaque
, target_phys_addr_t offset
)
353 val
= gic_dist_readb(opaque
, offset
);
354 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
358 static uint32_t gic_dist_readl(void *opaque
, target_phys_addr_t offset
)
362 gic_state
*s
= (gic_state
*)opaque
;
365 if (addr
< 0x100 || addr
> 0xd00)
366 return nvic_readl(s
->nvic
, addr
);
368 val
= gic_dist_readw(opaque
, offset
);
369 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
373 static void gic_dist_writeb(void *opaque
, target_phys_addr_t offset
,
376 gic_state
*s
= (gic_state
*)opaque
;
381 cpu
= gic_get_current_cpu();
382 if (offset
< 0x100) {
387 s
->enabled
= (value
& 1);
388 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
389 } else if (offset
< 4) {
395 } else if (offset
< 0x180) {
396 /* Interrupt Set Enable. */
397 irq
= (offset
- 0x100) * 8 + GIC_BASE_IRQ
;
402 for (i
= 0; i
< 8; i
++) {
403 if (value
& (1 << i
)) {
404 int mask
= (irq
< 32) ? (1 << cpu
) : GIC_TARGET(irq
);
405 if (!GIC_TEST_ENABLED(irq
+ i
))
406 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
407 GIC_SET_ENABLED(irq
+ i
);
408 /* If a raised level triggered IRQ enabled then mark
410 if (GIC_TEST_LEVEL(irq
+ i
, mask
)
411 && !GIC_TEST_TRIGGER(irq
+ i
)) {
412 DPRINTF("Set %d pending mask %x\n", irq
+ i
, mask
);
413 GIC_SET_PENDING(irq
+ i
, mask
);
417 } else if (offset
< 0x200) {
418 /* Interrupt Clear Enable. */
419 irq
= (offset
- 0x180) * 8 + GIC_BASE_IRQ
;
424 for (i
= 0; i
< 8; i
++) {
425 if (value
& (1 << i
)) {
426 if (GIC_TEST_ENABLED(irq
+ i
))
427 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
428 GIC_CLEAR_ENABLED(irq
+ i
);
431 } else if (offset
< 0x280) {
432 /* Interrupt Set Pending. */
433 irq
= (offset
- 0x200) * 8 + GIC_BASE_IRQ
;
439 for (i
= 0; i
< 8; i
++) {
440 if (value
& (1 << i
)) {
441 GIC_SET_PENDING(irq
+ i
, GIC_TARGET(irq
));
444 } else if (offset
< 0x300) {
445 /* Interrupt Clear Pending. */
446 irq
= (offset
- 0x280) * 8 + GIC_BASE_IRQ
;
449 for (i
= 0; i
< 8; i
++) {
450 /* ??? This currently clears the pending bit for all CPUs, even
451 for per-CPU interrupts. It's unclear whether this is the
453 if (value
& (1 << i
)) {
454 GIC_CLEAR_PENDING(irq
+ i
, ALL_CPU_MASK
);
457 } else if (offset
< 0x400) {
458 /* Interrupt Active. */
460 } else if (offset
< 0x800) {
461 /* Interrupt Priority. */
462 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
466 s
->priority1
[irq
][cpu
] = value
;
468 s
->priority2
[irq
- 32] = value
;
471 } else if (offset
< 0xc00) {
472 /* Interrupt CPU Target. */
473 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
479 value
= ALL_CPU_MASK
;
480 s
->irq_target
[irq
] = value
& ALL_CPU_MASK
;
481 } else if (offset
< 0xf00) {
482 /* Interrupt Configuration. */
483 irq
= (offset
- 0xc00) * 4 + GIC_BASE_IRQ
;
488 for (i
= 0; i
< 4; i
++) {
489 if (value
& (1 << (i
* 2))) {
490 GIC_SET_MODEL(irq
+ i
);
492 GIC_CLEAR_MODEL(irq
+ i
);
494 if (value
& (2 << (i
* 2))) {
495 GIC_SET_TRIGGER(irq
+ i
);
497 GIC_CLEAR_TRIGGER(irq
+ i
);
502 /* 0xf00 is only handled for 32-bit writes. */
508 cpu_abort(cpu_single_env
, "gic_dist_writeb: Bad offset %x\n", (int)offset
);
511 static void gic_dist_writew(void *opaque
, target_phys_addr_t offset
,
514 gic_dist_writeb(opaque
, offset
, value
& 0xff);
515 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
518 static void gic_dist_writel(void *opaque
, target_phys_addr_t offset
,
521 gic_state
*s
= (gic_state
*)opaque
;
525 if (addr
< 0x100 || (addr
> 0xd00 && addr
!= 0xf00)) {
526 nvic_writel(s
->nvic
, addr
, value
);
530 if (offset
== 0xf00) {
535 cpu
= gic_get_current_cpu();
537 switch ((value
>> 24) & 3) {
539 mask
= (value
>> 16) & ALL_CPU_MASK
;
545 mask
= ALL_CPU_MASK
^ (1 << cpu
);
548 DPRINTF("Bad Soft Int target filter\n");
552 GIC_SET_PENDING(irq
, mask
);
556 gic_dist_writew(opaque
, offset
, value
& 0xffff);
557 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
560 static CPUReadMemoryFunc
*gic_dist_readfn
[] = {
566 static CPUWriteMemoryFunc
*gic_dist_writefn
[] = {
573 static uint32_t gic_cpu_read(gic_state
*s
, int cpu
, int offset
)
576 case 0x00: /* Control */
577 return s
->cpu_enabled
[cpu
];
578 case 0x04: /* Priority mask */
579 return s
->priority_mask
[cpu
];
580 case 0x08: /* Binary Point */
581 /* ??? Not implemented. */
583 case 0x0c: /* Acknowledge */
584 return gic_acknowledge_irq(s
, cpu
);
585 case 0x14: /* Runing Priority */
586 return s
->running_priority
[cpu
];
587 case 0x18: /* Highest Pending Interrupt */
588 return s
->current_pending
[cpu
];
590 cpu_abort(cpu_single_env
, "gic_cpu_read: Bad offset %x\n",
596 static void gic_cpu_write(gic_state
*s
, int cpu
, int offset
, uint32_t value
)
599 case 0x00: /* Control */
600 s
->cpu_enabled
[cpu
] = (value
& 1);
601 DPRINTF("CPU %sabled\n", s
->cpu_enabled
? "En" : "Dis");
603 case 0x04: /* Priority mask */
604 s
->priority_mask
[cpu
] = (value
& 0xff);
606 case 0x08: /* Binary Point */
607 /* ??? Not implemented. */
609 case 0x10: /* End Of Interrupt */
610 return gic_complete_irq(s
, cpu
, value
& 0x3ff);
612 cpu_abort(cpu_single_env
, "gic_cpu_write: Bad offset %x\n",
620 static void gic_reset(gic_state
*s
)
623 memset(s
->irq_state
, 0, GIC_NIRQ
* sizeof(gic_irq_state
));
624 for (i
= 0 ; i
< NCPU
; i
++) {
625 s
->priority_mask
[i
] = 0xf0;
626 s
->current_pending
[i
] = 1023;
627 s
->running_irq
[i
] = 1023;
628 s
->running_priority
[i
] = 0x100;
630 /* The NVIC doesn't have per-cpu interfaces, so enable by default. */
631 s
->cpu_enabled
[i
] = 1;
633 s
->cpu_enabled
[i
] = 0;
636 for (i
= 0; i
< 16; i
++) {
641 /* The NVIC is always enabled. */
648 static void gic_save(QEMUFile
*f
, void *opaque
)
650 gic_state
*s
= (gic_state
*)opaque
;
654 qemu_put_be32(f
, s
->enabled
);
655 for (i
= 0; i
< NCPU
; i
++) {
656 qemu_put_be32(f
, s
->cpu_enabled
[i
]);
658 qemu_put_be32(f
, s
->irq_target
[i
]);
660 for (j
= 0; j
< 32; j
++)
661 qemu_put_be32(f
, s
->priority1
[j
][i
]);
662 for (j
= 0; j
< GIC_NIRQ
; j
++)
663 qemu_put_be32(f
, s
->last_active
[j
][i
]);
664 qemu_put_be32(f
, s
->priority_mask
[i
]);
665 qemu_put_be32(f
, s
->running_irq
[i
]);
666 qemu_put_be32(f
, s
->running_priority
[i
]);
667 qemu_put_be32(f
, s
->current_pending
[i
]);
669 for (i
= 0; i
< GIC_NIRQ
- 32; i
++) {
670 qemu_put_be32(f
, s
->priority2
[i
]);
672 for (i
= 0; i
< GIC_NIRQ
; i
++) {
673 qemu_put_byte(f
, s
->irq_state
[i
].enabled
);
674 qemu_put_byte(f
, s
->irq_state
[i
].pending
);
675 qemu_put_byte(f
, s
->irq_state
[i
].active
);
676 qemu_put_byte(f
, s
->irq_state
[i
].level
);
677 qemu_put_byte(f
, s
->irq_state
[i
].model
);
678 qemu_put_byte(f
, s
->irq_state
[i
].trigger
);
682 static int gic_load(QEMUFile
*f
, void *opaque
, int version_id
)
684 gic_state
*s
= (gic_state
*)opaque
;
691 s
->enabled
= qemu_get_be32(f
);
692 for (i
= 0; i
< NCPU
; i
++) {
693 s
->cpu_enabled
[i
] = qemu_get_be32(f
);
695 s
->irq_target
[i
] = qemu_get_be32(f
);
697 for (j
= 0; j
< 32; j
++)
698 s
->priority1
[j
][i
] = qemu_get_be32(f
);
699 for (j
= 0; j
< GIC_NIRQ
; j
++)
700 s
->last_active
[j
][i
] = qemu_get_be32(f
);
701 s
->priority_mask
[i
] = qemu_get_be32(f
);
702 s
->running_irq
[i
] = qemu_get_be32(f
);
703 s
->running_priority
[i
] = qemu_get_be32(f
);
704 s
->current_pending
[i
] = qemu_get_be32(f
);
706 for (i
= 0; i
< GIC_NIRQ
- 32; i
++) {
707 s
->priority2
[i
] = qemu_get_be32(f
);
709 for (i
= 0; i
< GIC_NIRQ
; i
++) {
710 s
->irq_state
[i
].enabled
= qemu_get_byte(f
);
711 s
->irq_state
[i
].pending
= qemu_get_byte(f
);
712 s
->irq_state
[i
].active
= qemu_get_byte(f
);
713 s
->irq_state
[i
].level
= qemu_get_byte(f
);
714 s
->irq_state
[i
].model
= qemu_get_byte(f
);
715 s
->irq_state
[i
].trigger
= qemu_get_byte(f
);
721 static gic_state
*gic_init(uint32_t dist_base
, qemu_irq
*parent_irq
)
727 s
= (gic_state
*)qemu_mallocz(sizeof(gic_state
));
728 s
->in
= qemu_allocate_irqs(gic_set_irq
, s
, GIC_NIRQ
);
729 for (i
= 0; i
< NCPU
; i
++) {
730 s
->parent_irq
[i
] = parent_irq
[i
];
732 iomemtype
= cpu_register_io_memory(0, gic_dist_readfn
,
733 gic_dist_writefn
, s
);
734 cpu_register_physical_memory(dist_base
, 0x00001000,
737 register_savevm("arm_gic", -1, 1, gic_save
, gic_load
, s
);