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 enable bits are only banked for per-cpu interrupts. */
41 unsigned enabled
:NCPU
;
42 unsigned pending
:NCPU
;
45 unsigned model
:1; /* 0 = N:N, 1 = 1:N */
46 unsigned trigger
:1; /* nonzero = edge triggered. */
49 #define ALL_CPU_MASK ((1 << NCPU) - 1)
51 #define NUM_CPU(s) ((s)->num_cpu)
56 #define GIC_SET_ENABLED(irq, cm) s->irq_state[irq].enabled |= (cm)
57 #define GIC_CLEAR_ENABLED(irq, cm) s->irq_state[irq].enabled &= ~(cm)
58 #define GIC_TEST_ENABLED(irq, cm) ((s->irq_state[irq].enabled & (cm)) != 0)
59 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
60 #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
61 #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
62 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
63 #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
64 #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
65 #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
66 #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
67 #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
68 #define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
69 #define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
70 #define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
71 #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
72 #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
73 #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
74 #define GIC_GET_PRIORITY(irq, cpu) \
75 (((irq) < 32) ? s->priority1[irq][cpu] : s->priority2[(irq) - 32])
77 #define GIC_TARGET(irq) 1
79 #define GIC_TARGET(irq) s->irq_target[irq]
82 typedef struct gic_state
85 qemu_irq parent_irq
[NCPU
];
87 int cpu_enabled
[NCPU
];
89 gic_irq_state irq_state
[GIC_NIRQ
];
91 int irq_target
[GIC_NIRQ
];
93 int priority1
[32][NCPU
];
94 int priority2
[GIC_NIRQ
- 32];
95 int last_active
[GIC_NIRQ
][NCPU
];
97 int priority_mask
[NCPU
];
98 int running_irq
[NCPU
];
99 int running_priority
[NCPU
];
100 int current_pending
[NCPU
];
109 /* TODO: Many places that call this routine could be optimized. */
110 /* Update interrupt status after enabled or pending bits have been changed. */
111 static void gic_update(gic_state
*s
)
120 for (cpu
= 0; cpu
< NUM_CPU(s
); cpu
++) {
122 s
->current_pending
[cpu
] = 1023;
123 if (!s
->enabled
|| !s
->cpu_enabled
[cpu
]) {
124 qemu_irq_lower(s
->parent_irq
[cpu
]);
129 for (irq
= 0; irq
< GIC_NIRQ
; irq
++) {
130 if (GIC_TEST_ENABLED(irq
, cm
) && GIC_TEST_PENDING(irq
, cm
)) {
131 if (GIC_GET_PRIORITY(irq
, cpu
) < best_prio
) {
132 best_prio
= GIC_GET_PRIORITY(irq
, cpu
);
138 if (best_prio
<= s
->priority_mask
[cpu
]) {
139 s
->current_pending
[cpu
] = best_irq
;
140 if (best_prio
< s
->running_priority
[cpu
]) {
141 DPRINTF("Raised pending IRQ %d\n", best_irq
);
145 qemu_set_irq(s
->parent_irq
[cpu
], level
);
149 static void __attribute__((unused
))
150 gic_set_pending_private(gic_state
*s
, int cpu
, int irq
)
154 if (GIC_TEST_PENDING(irq
, cm
))
157 DPRINTF("Set %d pending cpu %d\n", irq
, cpu
);
158 GIC_SET_PENDING(irq
, cm
);
162 /* Process a change in an external IRQ input. */
163 static void gic_set_irq(void *opaque
, int irq
, int level
)
165 gic_state
*s
= (gic_state
*)opaque
;
166 /* The first external input line is internal interrupt 32. */
168 if (level
== GIC_TEST_LEVEL(irq
, ALL_CPU_MASK
))
172 GIC_SET_LEVEL(irq
, ALL_CPU_MASK
);
173 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
, ALL_CPU_MASK
)) {
174 DPRINTF("Set %d pending mask %x\n", irq
, GIC_TARGET(irq
));
175 GIC_SET_PENDING(irq
, GIC_TARGET(irq
));
178 GIC_CLEAR_LEVEL(irq
, ALL_CPU_MASK
);
183 static void gic_set_running_irq(gic_state
*s
, int cpu
, int irq
)
185 s
->running_irq
[cpu
] = irq
;
187 s
->running_priority
[cpu
] = 0x100;
189 s
->running_priority
[cpu
] = GIC_GET_PRIORITY(irq
, cpu
);
194 static uint32_t gic_acknowledge_irq(gic_state
*s
, int cpu
)
198 new_irq
= s
->current_pending
[cpu
];
200 || GIC_GET_PRIORITY(new_irq
, cpu
) >= s
->running_priority
[cpu
]) {
201 DPRINTF("ACK no pending IRQ\n");
204 s
->last_active
[new_irq
][cpu
] = s
->running_irq
[cpu
];
205 /* Clear pending flags for both level and edge triggered interrupts.
206 Level triggered IRQs will be reasserted once they become inactive. */
207 GIC_CLEAR_PENDING(new_irq
, GIC_TEST_MODEL(new_irq
) ? ALL_CPU_MASK
: cm
);
208 gic_set_running_irq(s
, cpu
, new_irq
);
209 DPRINTF("ACK %d\n", new_irq
);
213 static void gic_complete_irq(gic_state
* s
, int cpu
, int irq
)
217 DPRINTF("EOI %d\n", irq
);
218 if (irq
>= GIC_NIRQ
) {
219 /* This handles two cases:
220 * 1. If software writes the ID of a spurious interrupt [ie 1023]
221 * to the GICC_EOIR, the GIC ignores that write.
222 * 2. If software writes the number of a non-existent interrupt
223 * this must be a subcase of "value written does not match the last
224 * valid interrupt value read from the Interrupt Acknowledge
225 * register" and so this is UNPREDICTABLE. We choose to ignore it.
229 if (s
->running_irq
[cpu
] == 1023)
230 return; /* No active IRQ. */
231 /* Mark level triggered interrupts as pending if they are still
233 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
, cm
)
234 && GIC_TEST_LEVEL(irq
, cm
) && (GIC_TARGET(irq
) & cm
) != 0) {
235 DPRINTF("Set %d pending mask %x\n", irq
, cm
);
236 GIC_SET_PENDING(irq
, cm
);
239 if (irq
!= s
->running_irq
[cpu
]) {
240 /* Complete an IRQ that is not currently running. */
241 int tmp
= s
->running_irq
[cpu
];
242 while (s
->last_active
[tmp
][cpu
] != 1023) {
243 if (s
->last_active
[tmp
][cpu
] == irq
) {
244 s
->last_active
[tmp
][cpu
] = s
->last_active
[irq
][cpu
];
247 tmp
= s
->last_active
[tmp
][cpu
];
253 /* Complete the current running IRQ. */
254 gic_set_running_irq(s
, cpu
, s
->last_active
[s
->running_irq
[cpu
]][cpu
]);
258 static uint32_t gic_dist_readb(void *opaque
, target_phys_addr_t offset
)
260 gic_state
*s
= (gic_state
*)opaque
;
268 cpu
= gic_get_current_cpu();
270 if (offset
< 0x100) {
275 return ((GIC_NIRQ
/ 32) - 1) | ((NUM_CPU(s
) - 1) << 5);
280 } else if (offset
< 0x200) {
281 /* Interrupt Set/Clear Enable. */
283 irq
= (offset
- 0x100) * 8;
285 irq
= (offset
- 0x180) * 8;
290 for (i
= 0; i
< 8; i
++) {
291 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
295 } else if (offset
< 0x300) {
296 /* Interrupt Set/Clear Pending. */
298 irq
= (offset
- 0x200) * 8;
300 irq
= (offset
- 0x280) * 8;
305 mask
= (irq
< 32) ? cm
: ALL_CPU_MASK
;
306 for (i
= 0; i
< 8; i
++) {
307 if (GIC_TEST_PENDING(irq
+ i
, mask
)) {
311 } else if (offset
< 0x400) {
312 /* Interrupt Active. */
313 irq
= (offset
- 0x300) * 8 + GIC_BASE_IRQ
;
317 mask
= (irq
< 32) ? cm
: ALL_CPU_MASK
;
318 for (i
= 0; i
< 8; i
++) {
319 if (GIC_TEST_ACTIVE(irq
+ i
, mask
)) {
323 } else if (offset
< 0x800) {
324 /* Interrupt Priority. */
325 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
328 res
= GIC_GET_PRIORITY(irq
, cpu
);
330 } else if (offset
< 0xc00) {
331 /* Interrupt CPU Target. */
332 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
335 if (irq
>= 29 && irq
<= 31) {
338 res
= GIC_TARGET(irq
);
340 } else if (offset
< 0xf00) {
341 /* Interrupt Configuration. */
342 irq
= (offset
- 0xc00) * 2 + GIC_BASE_IRQ
;
346 for (i
= 0; i
< 4; i
++) {
347 if (GIC_TEST_MODEL(irq
+ i
))
348 res
|= (1 << (i
* 2));
349 if (GIC_TEST_TRIGGER(irq
+ i
))
350 res
|= (2 << (i
* 2));
353 } else if (offset
< 0xfe0) {
355 } else /* offset >= 0xfe0 */ {
359 res
= gic_id
[(offset
- 0xfe0) >> 2];
364 hw_error("gic_dist_readb: Bad offset %x\n", (int)offset
);
368 static uint32_t gic_dist_readw(void *opaque
, target_phys_addr_t offset
)
371 val
= gic_dist_readb(opaque
, offset
);
372 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
376 static uint32_t gic_dist_readl(void *opaque
, target_phys_addr_t offset
)
380 gic_state
*s
= (gic_state
*)opaque
;
383 if (addr
< 0x100 || addr
> 0xd00)
384 return nvic_readl(s
, addr
);
386 val
= gic_dist_readw(opaque
, offset
);
387 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
391 static void gic_dist_writeb(void *opaque
, target_phys_addr_t offset
,
394 gic_state
*s
= (gic_state
*)opaque
;
399 cpu
= gic_get_current_cpu();
400 if (offset
< 0x100) {
405 s
->enabled
= (value
& 1);
406 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
407 } else if (offset
< 4) {
413 } else if (offset
< 0x180) {
414 /* Interrupt Set Enable. */
415 irq
= (offset
- 0x100) * 8 + GIC_BASE_IRQ
;
420 for (i
= 0; i
< 8; i
++) {
421 if (value
& (1 << i
)) {
422 int mask
= (irq
< 32) ? (1 << cpu
) : GIC_TARGET(irq
);
423 int cm
= (irq
< 32) ? (1 << cpu
) : ALL_CPU_MASK
;
425 if (!GIC_TEST_ENABLED(irq
+ i
, cm
)) {
426 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
428 GIC_SET_ENABLED(irq
+ i
, cm
);
429 /* If a raised level triggered IRQ enabled then mark
431 if (GIC_TEST_LEVEL(irq
+ i
, mask
)
432 && !GIC_TEST_TRIGGER(irq
+ i
)) {
433 DPRINTF("Set %d pending mask %x\n", irq
+ i
, mask
);
434 GIC_SET_PENDING(irq
+ i
, mask
);
438 } else if (offset
< 0x200) {
439 /* Interrupt Clear Enable. */
440 irq
= (offset
- 0x180) * 8 + GIC_BASE_IRQ
;
445 for (i
= 0; i
< 8; i
++) {
446 if (value
& (1 << i
)) {
447 int cm
= (irq
< 32) ? (1 << cpu
) : ALL_CPU_MASK
;
449 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
450 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
452 GIC_CLEAR_ENABLED(irq
+ i
, cm
);
455 } else if (offset
< 0x280) {
456 /* Interrupt Set Pending. */
457 irq
= (offset
- 0x200) * 8 + GIC_BASE_IRQ
;
463 for (i
= 0; i
< 8; i
++) {
464 if (value
& (1 << i
)) {
465 GIC_SET_PENDING(irq
+ i
, GIC_TARGET(irq
));
468 } else if (offset
< 0x300) {
469 /* Interrupt Clear Pending. */
470 irq
= (offset
- 0x280) * 8 + GIC_BASE_IRQ
;
473 for (i
= 0; i
< 8; i
++) {
474 /* ??? This currently clears the pending bit for all CPUs, even
475 for per-CPU interrupts. It's unclear whether this is the
477 if (value
& (1 << i
)) {
478 GIC_CLEAR_PENDING(irq
+ i
, ALL_CPU_MASK
);
481 } else if (offset
< 0x400) {
482 /* Interrupt Active. */
484 } else if (offset
< 0x800) {
485 /* Interrupt Priority. */
486 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
490 s
->priority1
[irq
][cpu
] = value
;
492 s
->priority2
[irq
- 32] = value
;
495 } else if (offset
< 0xc00) {
496 /* Interrupt CPU Target. */
497 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
503 value
= ALL_CPU_MASK
;
504 s
->irq_target
[irq
] = value
& ALL_CPU_MASK
;
505 } else if (offset
< 0xf00) {
506 /* Interrupt Configuration. */
507 irq
= (offset
- 0xc00) * 4 + GIC_BASE_IRQ
;
512 for (i
= 0; i
< 4; i
++) {
513 if (value
& (1 << (i
* 2))) {
514 GIC_SET_MODEL(irq
+ i
);
516 GIC_CLEAR_MODEL(irq
+ i
);
518 if (value
& (2 << (i
* 2))) {
519 GIC_SET_TRIGGER(irq
+ i
);
521 GIC_CLEAR_TRIGGER(irq
+ i
);
526 /* 0xf00 is only handled for 32-bit writes. */
532 hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset
);
535 static void gic_dist_writew(void *opaque
, target_phys_addr_t offset
,
538 gic_dist_writeb(opaque
, offset
, value
& 0xff);
539 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
542 static void gic_dist_writel(void *opaque
, target_phys_addr_t offset
,
545 gic_state
*s
= (gic_state
*)opaque
;
549 if (addr
< 0x100 || (addr
> 0xd00 && addr
!= 0xf00)) {
550 nvic_writel(s
, addr
, value
);
554 if (offset
== 0xf00) {
559 cpu
= gic_get_current_cpu();
561 switch ((value
>> 24) & 3) {
563 mask
= (value
>> 16) & ALL_CPU_MASK
;
566 mask
= ALL_CPU_MASK
^ (1 << cpu
);
572 DPRINTF("Bad Soft Int target filter\n");
576 GIC_SET_PENDING(irq
, mask
);
580 gic_dist_writew(opaque
, offset
, value
& 0xffff);
581 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
584 static const MemoryRegionOps gic_dist_ops
= {
586 .read
= { gic_dist_readb
, gic_dist_readw
, gic_dist_readl
, },
587 .write
= { gic_dist_writeb
, gic_dist_writew
, gic_dist_writel
, },
589 .endianness
= DEVICE_NATIVE_ENDIAN
,
593 static uint32_t gic_cpu_read(gic_state
*s
, int cpu
, int offset
)
596 case 0x00: /* Control */
597 return s
->cpu_enabled
[cpu
];
598 case 0x04: /* Priority mask */
599 return s
->priority_mask
[cpu
];
600 case 0x08: /* Binary Point */
601 /* ??? Not implemented. */
603 case 0x0c: /* Acknowledge */
604 return gic_acknowledge_irq(s
, cpu
);
605 case 0x14: /* Runing Priority */
606 return s
->running_priority
[cpu
];
607 case 0x18: /* Highest Pending Interrupt */
608 return s
->current_pending
[cpu
];
610 hw_error("gic_cpu_read: Bad offset %x\n", (int)offset
);
615 static void gic_cpu_write(gic_state
*s
, int cpu
, int offset
, uint32_t value
)
618 case 0x00: /* Control */
619 s
->cpu_enabled
[cpu
] = (value
& 1);
620 DPRINTF("CPU %d %sabled\n", cpu
, s
->cpu_enabled
? "En" : "Dis");
622 case 0x04: /* Priority mask */
623 s
->priority_mask
[cpu
] = (value
& 0xff);
625 case 0x08: /* Binary Point */
626 /* ??? Not implemented. */
628 case 0x10: /* End Of Interrupt */
629 return gic_complete_irq(s
, cpu
, value
& 0x3ff);
631 hw_error("gic_cpu_write: Bad offset %x\n", (int)offset
);
638 static void gic_reset(gic_state
*s
)
641 memset(s
->irq_state
, 0, GIC_NIRQ
* sizeof(gic_irq_state
));
642 for (i
= 0 ; i
< NUM_CPU(s
); i
++) {
643 s
->priority_mask
[i
] = 0xf0;
644 s
->current_pending
[i
] = 1023;
645 s
->running_irq
[i
] = 1023;
646 s
->running_priority
[i
] = 0x100;
648 /* The NVIC doesn't have per-cpu interfaces, so enable by default. */
649 s
->cpu_enabled
[i
] = 1;
651 s
->cpu_enabled
[i
] = 0;
654 for (i
= 0; i
< 16; i
++) {
655 GIC_SET_ENABLED(i
, ALL_CPU_MASK
);
659 /* The NVIC is always enabled. */
666 static void gic_save(QEMUFile
*f
, void *opaque
)
668 gic_state
*s
= (gic_state
*)opaque
;
672 qemu_put_be32(f
, s
->enabled
);
673 for (i
= 0; i
< NUM_CPU(s
); i
++) {
674 qemu_put_be32(f
, s
->cpu_enabled
[i
]);
675 for (j
= 0; j
< 32; j
++)
676 qemu_put_be32(f
, s
->priority1
[j
][i
]);
677 for (j
= 0; j
< GIC_NIRQ
; j
++)
678 qemu_put_be32(f
, s
->last_active
[j
][i
]);
679 qemu_put_be32(f
, s
->priority_mask
[i
]);
680 qemu_put_be32(f
, s
->running_irq
[i
]);
681 qemu_put_be32(f
, s
->running_priority
[i
]);
682 qemu_put_be32(f
, s
->current_pending
[i
]);
684 for (i
= 0; i
< GIC_NIRQ
- 32; i
++) {
685 qemu_put_be32(f
, s
->priority2
[i
]);
687 for (i
= 0; i
< GIC_NIRQ
; i
++) {
689 qemu_put_be32(f
, s
->irq_target
[i
]);
691 qemu_put_byte(f
, s
->irq_state
[i
].enabled
);
692 qemu_put_byte(f
, s
->irq_state
[i
].pending
);
693 qemu_put_byte(f
, s
->irq_state
[i
].active
);
694 qemu_put_byte(f
, s
->irq_state
[i
].level
);
695 qemu_put_byte(f
, s
->irq_state
[i
].model
);
696 qemu_put_byte(f
, s
->irq_state
[i
].trigger
);
700 static int gic_load(QEMUFile
*f
, void *opaque
, int version_id
)
702 gic_state
*s
= (gic_state
*)opaque
;
709 s
->enabled
= qemu_get_be32(f
);
710 for (i
= 0; i
< NUM_CPU(s
); i
++) {
711 s
->cpu_enabled
[i
] = qemu_get_be32(f
);
712 for (j
= 0; j
< 32; j
++)
713 s
->priority1
[j
][i
] = qemu_get_be32(f
);
714 for (j
= 0; j
< GIC_NIRQ
; j
++)
715 s
->last_active
[j
][i
] = qemu_get_be32(f
);
716 s
->priority_mask
[i
] = qemu_get_be32(f
);
717 s
->running_irq
[i
] = qemu_get_be32(f
);
718 s
->running_priority
[i
] = qemu_get_be32(f
);
719 s
->current_pending
[i
] = qemu_get_be32(f
);
721 for (i
= 0; i
< GIC_NIRQ
- 32; i
++) {
722 s
->priority2
[i
] = qemu_get_be32(f
);
724 for (i
= 0; i
< GIC_NIRQ
; i
++) {
726 s
->irq_target
[i
] = qemu_get_be32(f
);
728 s
->irq_state
[i
].enabled
= qemu_get_byte(f
);
729 s
->irq_state
[i
].pending
= qemu_get_byte(f
);
730 s
->irq_state
[i
].active
= qemu_get_byte(f
);
731 s
->irq_state
[i
].level
= qemu_get_byte(f
);
732 s
->irq_state
[i
].model
= qemu_get_byte(f
);
733 s
->irq_state
[i
].trigger
= qemu_get_byte(f
);
740 static void gic_init(gic_state
*s
, int num_cpu
)
742 static void gic_init(gic_state
*s
)
748 s
->num_cpu
= num_cpu
;
750 qdev_init_gpio_in(&s
->busdev
.qdev
, gic_set_irq
, GIC_NIRQ
- 32);
751 for (i
= 0; i
< NUM_CPU(s
); i
++) {
752 sysbus_init_irq(&s
->busdev
, &s
->parent_irq
[i
]);
754 memory_region_init_io(&s
->iomem
, &gic_dist_ops
, s
, "gic_dist", 0x1000);
756 register_savevm(NULL
, "arm_gic", -1, 2, gic_save
, gic_load
, s
);