2 * ARM AMBA Generic/Distributed Interrupt Controller
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
10 /* TODO: Some variants of this controller can handle multiple CPUs.
11 Currently only single CPU operation is implemented. */
19 #define DPRINTF(fmt, args...) \
20 do { printf("arm_gic: " fmt , ##args); } while (0)
22 #define DPRINTF(fmt, args...) do {} while(0)
25 /* Distributed interrupt controller. */
27 static const uint8_t gic_id
[] =
28 { 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
32 typedef struct gic_irq_state
38 unsigned model
:1; /* 0 = 1:N, 1 = N:N */
39 unsigned trigger
:1; /* nonzero = edge triggered. */
42 #define GIC_SET_ENABLED(irq) s->irq_state[irq].enabled = 1
43 #define GIC_CLEAR_ENABLED(irq) s->irq_state[irq].enabled = 0
44 #define GIC_TEST_ENABLED(irq) s->irq_state[irq].enabled
45 #define GIC_SET_PENDING(irq) s->irq_state[irq].pending = 1
46 #define GIC_CLEAR_PENDING(irq) s->irq_state[irq].pending = 0
47 #define GIC_TEST_PENDING(irq) s->irq_state[irq].pending
48 #define GIC_SET_ACTIVE(irq) s->irq_state[irq].active = 1
49 #define GIC_CLEAR_ACTIVE(irq) s->irq_state[irq].active = 0
50 #define GIC_TEST_ACTIVE(irq) s->irq_state[irq].active
51 #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
52 #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
53 #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
54 #define GIC_SET_LEVEL(irq) s->irq_state[irq].level = 1
55 #define GIC_CLEAR_LEVEL(irq) s->irq_state[irq].level = 0
56 #define GIC_TEST_LEVEL(irq) s->irq_state[irq].level
57 #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
58 #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
59 #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
61 typedef struct gic_state
68 gic_irq_state irq_state
[GIC_NIRQ
];
69 int irq_target
[GIC_NIRQ
];
70 int priority
[GIC_NIRQ
];
71 int last_active
[GIC_NIRQ
];
79 /* TODO: Many places that call this routine could be optimized. */
80 /* Update interrupt status after enabled or pending bits have been changed. */
81 static void gic_update(gic_state
*s
)
87 s
->current_pending
= 1023;
88 if (!s
->enabled
|| !s
->cpu_enabled
) {
89 qemu_irq_lower(s
->parent_irq
);
94 for (irq
= 0; irq
< 96; irq
++) {
95 if (GIC_TEST_ENABLED(irq
) && GIC_TEST_PENDING(irq
)) {
96 if (s
->priority
[irq
] < best_prio
) {
97 best_prio
= s
->priority
[irq
];
102 if (best_prio
> s
->priority_mask
) {
103 qemu_irq_lower(s
->parent_irq
);
105 s
->current_pending
= best_irq
;
106 if (best_prio
< s
->running_priority
) {
107 DPRINTF("Raised pending IRQ %d\n", best_irq
);
108 qemu_irq_raise(s
->parent_irq
);
113 static void gic_set_irq(void *opaque
, int irq
, int level
)
115 gic_state
*s
= (gic_state
*)opaque
;
116 /* The first external input line is internal interrupt 32. */
118 if (level
== GIC_TEST_LEVEL(irq
))
123 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
)) {
124 DPRINTF("Set %d pending\n", irq
);
125 GIC_SET_PENDING(irq
);
128 GIC_CLEAR_LEVEL(irq
);
133 static void gic_set_running_irq(gic_state
*s
, int irq
)
135 s
->running_irq
= irq
;
137 s
->running_priority
= 0x100;
139 s
->running_priority
= s
->priority
[irq
];
143 static uint32_t gic_acknowledge_irq(gic_state
*s
)
146 new_irq
= s
->current_pending
;
147 if (new_irq
== 1023 || s
->priority
[new_irq
] >= s
->running_priority
) {
148 DPRINTF("ACK no pending IRQ\n");
151 qemu_irq_lower(s
->parent_irq
);
152 s
->last_active
[new_irq
] = s
->running_irq
;
153 /* For level triggered interrupts we clear the pending bit while
154 the interrupt is active. */
155 GIC_CLEAR_PENDING(new_irq
);
156 gic_set_running_irq(s
, new_irq
);
157 DPRINTF("ACK %d\n", new_irq
);
161 static void gic_complete_irq(gic_state
* s
, int irq
)
164 DPRINTF("EOI %d\n", irq
);
165 if (s
->running_irq
== 1023)
166 return; /* No active IRQ. */
168 /* Mark level triggered interrupts as pending if they are still
170 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
)
171 && GIC_TEST_LEVEL(irq
)) {
172 GIC_SET_PENDING(irq
);
176 if (irq
!= s
->running_irq
) {
177 /* Complete an IRQ that is not currently running. */
178 int tmp
= s
->running_irq
;
179 while (s
->last_active
[tmp
] != 1023) {
180 if (s
->last_active
[tmp
] == irq
) {
181 s
->last_active
[tmp
] = s
->last_active
[irq
];
184 tmp
= s
->last_active
[tmp
];
190 /* Complete the current running IRQ. */
191 gic_set_running_irq(s
, s
->last_active
[s
->running_irq
]);
195 static uint32_t gic_dist_readb(void *opaque
, target_phys_addr_t offset
)
197 gic_state
*s
= (gic_state
*)opaque
;
202 offset
-= s
->base
+ 0x1000;
203 if (offset
< 0x100) {
207 return (GIC_NIRQ
/ 32) - 1;
211 } else if (offset
< 0x200) {
212 /* Interrupt Set/Clear Enable. */
214 irq
= (offset
- 0x100) * 8;
216 irq
= (offset
- 0x180) * 8;
220 for (i
= 0; i
< 8; i
++) {
221 if (GIC_TEST_ENABLED(irq
+ i
)) {
225 } else if (offset
< 0x300) {
226 /* Interrupt Set/Clear Pending. */
228 irq
= (offset
- 0x200) * 8;
230 irq
= (offset
- 0x280) * 8;
234 for (i
= 0; i
< 8; i
++) {
235 if (GIC_TEST_PENDING(irq
+ i
)) {
239 } else if (offset
< 0x400) {
240 /* Interrupt Active. */
241 irq
= (offset
- 0x300) * 8;
245 for (i
= 0; i
< 8; i
++) {
246 if (GIC_TEST_ACTIVE(irq
+ i
)) {
250 } else if (offset
< 0x800) {
251 /* Interrupt Priority. */
252 irq
= offset
- 0x400;
255 res
= s
->priority
[irq
];
256 } else if (offset
< 0xc00) {
257 /* Interrupt CPU Target. */
258 irq
= offset
- 0x800;
261 res
= s
->irq_target
[irq
];
262 } else if (offset
< 0xf00) {
263 /* Interrupt Configuration. */
264 irq
= (offset
- 0xc00) * 2;
268 for (i
= 0; i
< 4; i
++) {
269 if (GIC_TEST_MODEL(irq
+ i
))
270 res
|= (1 << (i
* 2));
271 if (GIC_TEST_TRIGGER(irq
+ i
))
272 res
|= (2 << (i
* 2));
274 } else if (offset
< 0xfe0) {
276 } else /* offset >= 0xfe0 */ {
280 res
= gic_id
[(offset
- 0xfe0) >> 2];
285 cpu_abort (cpu_single_env
, "gic_dist_readb: Bad offset %x\n", offset
);
289 static uint32_t gic_dist_readw(void *opaque
, target_phys_addr_t offset
)
292 val
= gic_dist_readb(opaque
, offset
);
293 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
297 static uint32_t gic_dist_readl(void *opaque
, target_phys_addr_t offset
)
300 val
= gic_dist_readw(opaque
, offset
);
301 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
305 static void gic_dist_writeb(void *opaque
, target_phys_addr_t offset
,
308 gic_state
*s
= (gic_state
*)opaque
;
312 offset
-= s
->base
+ 0x1000;
313 if (offset
< 0x100) {
315 s
->enabled
= (value
& 1);
316 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
317 } else if (offset
< 4) {
322 } else if (offset
< 0x180) {
323 /* Interrupt Set Enable. */
324 irq
= (offset
- 0x100) * 8;
327 for (i
= 0; i
< 8; i
++) {
328 if (value
& (1 << i
)) {
329 if (!GIC_TEST_ENABLED(irq
+ i
))
330 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
331 GIC_SET_ENABLED(irq
+ i
);
332 /* If a raised level triggered IRQ enabled then mark
334 if (GIC_TEST_LEVEL(irq
+ i
) && !GIC_TEST_TRIGGER(irq
+ i
))
335 GIC_SET_PENDING(irq
+ i
);
338 } else if (offset
< 0x200) {
339 /* Interrupt Clear Enable. */
340 irq
= (offset
- 0x180) * 8;
343 for (i
= 0; i
< 8; i
++) {
344 if (value
& (1 << i
)) {
345 if (GIC_TEST_ENABLED(irq
+ i
))
346 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
347 GIC_CLEAR_ENABLED(irq
+ i
);
350 } else if (offset
< 0x280) {
351 /* Interrupt Set Pending. */
352 irq
= (offset
- 0x200) * 8;
355 for (i
= 0; i
< 8; i
++) {
356 if (value
& (1 << i
)) {
357 GIC_SET_PENDING(irq
+ i
);
360 } else if (offset
< 0x300) {
361 /* Interrupt Clear Pending. */
362 irq
= (offset
- 0x280) * 8;
365 for (i
= 0; i
< 8; i
++) {
366 if (value
& (1 << i
)) {
367 GIC_CLEAR_PENDING(irq
+ i
);
370 } else if (offset
< 0x400) {
371 /* Interrupt Active. */
373 } else if (offset
< 0x800) {
374 /* Interrupt Priority. */
375 irq
= offset
- 0x400;
378 s
->priority
[irq
] = value
;
379 } else if (offset
< 0xc00) {
380 /* Interrupt CPU Target. */
381 irq
= offset
- 0x800;
384 s
->irq_target
[irq
] = value
;
385 } else if (offset
< 0xf00) {
386 /* Interrupt Configuration. */
387 irq
= (offset
- 0xc00) * 4;
390 for (i
= 0; i
< 4; i
++) {
391 if (value
& (1 << (i
* 2))) {
392 GIC_SET_MODEL(irq
+ i
);
394 GIC_CLEAR_MODEL(irq
+ i
);
396 if (value
& (2 << (i
* 2))) {
397 GIC_SET_TRIGGER(irq
+ i
);
399 GIC_CLEAR_TRIGGER(irq
+ i
);
403 /* 0xf00 is only handled for word writes. */
409 cpu_abort (cpu_single_env
, "gic_dist_writeb: Bad offset %x\n", offset
);
412 static void gic_dist_writew(void *opaque
, target_phys_addr_t offset
,
415 gic_state
*s
= (gic_state
*)opaque
;
416 if (offset
- s
->base
== 0xf00) {
417 GIC_SET_PENDING(value
& 0x3ff);
421 gic_dist_writeb(opaque
, offset
, value
& 0xff);
422 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
425 static void gic_dist_writel(void *opaque
, target_phys_addr_t offset
,
428 gic_dist_writew(opaque
, offset
, value
& 0xffff);
429 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
432 static CPUReadMemoryFunc
*gic_dist_readfn
[] = {
438 static CPUWriteMemoryFunc
*gic_dist_writefn
[] = {
444 static uint32_t gic_cpu_read(void *opaque
, target_phys_addr_t offset
)
446 gic_state
*s
= (gic_state
*)opaque
;
449 case 0x00: /* Control */
450 return s
->cpu_enabled
;
451 case 0x04: /* Priority mask */
452 return s
->priority_mask
;
453 case 0x08: /* Binary Point */
454 /* ??? Not implemented. */
456 case 0x0c: /* Acknowledge */
457 return gic_acknowledge_irq(s
);
458 case 0x14: /* Runing Priority */
459 return s
->running_priority
;
460 case 0x18: /* Highest Pending Interrupt */
461 return s
->current_pending
;
463 cpu_abort (cpu_single_env
, "gic_cpu_read: Bad offset %x\n", offset
);
468 static void gic_cpu_write(void *opaque
, target_phys_addr_t offset
,
471 gic_state
*s
= (gic_state
*)opaque
;
474 case 0x00: /* Control */
475 s
->cpu_enabled
= (value
& 1);
476 DPRINTF("CPU %sabled\n", s
->cpu_enabled
? "En" : "Dis");
478 case 0x04: /* Priority mask */
479 s
->priority_mask
= (value
& 0x3ff);
481 case 0x08: /* Binary Point */
482 /* ??? Not implemented. */
484 case 0x10: /* End Of Interrupt */
485 return gic_complete_irq(s
, value
& 0x3ff);
487 cpu_abort (cpu_single_env
, "gic_cpu_write: Bad offset %x\n", offset
);
493 static CPUReadMemoryFunc
*gic_cpu_readfn
[] = {
499 static CPUWriteMemoryFunc
*gic_cpu_writefn
[] = {
505 static void gic_reset(gic_state
*s
)
508 memset(s
->irq_state
, 0, GIC_NIRQ
* sizeof(gic_irq_state
));
509 s
->priority_mask
= 0xf0;
510 s
->current_pending
= 1023;
511 s
->running_irq
= 1023;
512 s
->running_priority
= 0x100;
513 for (i
= 0; i
< 15; i
++) {
521 qemu_irq
*arm_gic_init(uint32_t base
, qemu_irq parent_irq
)
527 s
= (gic_state
*)qemu_mallocz(sizeof(gic_state
));
530 qi
= qemu_allocate_irqs(gic_set_irq
, s
, GIC_NIRQ
);
531 s
->parent_irq
= parent_irq
;
532 if (base
!= 0xffffffff) {
533 iomemtype
= cpu_register_io_memory(0, gic_cpu_readfn
,
535 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
536 iomemtype
= cpu_register_io_memory(0, gic_dist_readfn
,
537 gic_dist_writefn
, s
);
538 cpu_register_physical_memory(base
+ 0x1000, 0x00001000, iomemtype
);