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
63 arm_pic_handler handler
;
70 gic_irq_state irq_state
[GIC_NIRQ
];
71 int irq_target
[GIC_NIRQ
];
72 int priority
[GIC_NIRQ
];
73 int last_active
[GIC_NIRQ
];
81 /* TODO: Many places that call this routine could be optimized. */
82 /* Update interrupt status after enabled or pending bits have been changed. */
83 static void gic_update(gic_state
*s
)
89 s
->current_pending
= 1023;
90 if (!s
->enabled
|| !s
->cpu_enabled
) {
91 pic_set_irq_new(s
->parent
, s
->parent_irq
, 0);
96 for (irq
= 0; irq
< 96; irq
++) {
97 if (GIC_TEST_ENABLED(irq
) && GIC_TEST_PENDING(irq
)) {
98 if (s
->priority
[irq
] < best_prio
) {
99 best_prio
= s
->priority
[irq
];
104 if (best_prio
> s
->priority_mask
) {
105 pic_set_irq_new(s
->parent
, s
->parent_irq
, 0);
107 s
->current_pending
= best_irq
;
108 if (best_prio
< s
->running_priority
) {
109 DPRINTF("Raised pending IRQ %d\n", best_irq
);
110 pic_set_irq_new(s
->parent
, s
->parent_irq
, 1);
115 static void gic_set_irq(void *opaque
, int irq
, int level
)
117 gic_state
*s
= (gic_state
*)opaque
;
118 /* The first external input line is internal interrupt 32. */
120 if (level
== GIC_TEST_LEVEL(irq
))
125 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
)) {
126 DPRINTF("Set %d pending\n", irq
);
127 GIC_SET_PENDING(irq
);
130 GIC_CLEAR_LEVEL(irq
);
135 static void gic_set_running_irq(gic_state
*s
, int irq
)
137 s
->running_irq
= irq
;
139 s
->running_priority
= 0x100;
141 s
->running_priority
= s
->priority
[irq
];
145 static uint32_t gic_acknowledge_irq(gic_state
*s
)
148 new_irq
= s
->current_pending
;
149 if (new_irq
== 1023 || s
->priority
[new_irq
] >= s
->running_priority
) {
150 DPRINTF("ACK no pending IRQ\n");
153 pic_set_irq_new(s
->parent
, s
->parent_irq
, 0);
154 s
->last_active
[new_irq
] = s
->running_irq
;
155 /* For level triggered interrupts we clear the pending bit while
156 the interrupt is active. */
157 GIC_CLEAR_PENDING(new_irq
);
158 gic_set_running_irq(s
, new_irq
);
159 DPRINTF("ACK %d\n", new_irq
);
163 static void gic_complete_irq(gic_state
* s
, int irq
)
166 DPRINTF("EOI %d\n", irq
);
167 if (s
->running_irq
== 1023)
168 return; /* No active IRQ. */
170 /* Mark level triggered interrupts as pending if they are still
172 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
)
173 && GIC_TEST_LEVEL(irq
)) {
174 GIC_SET_PENDING(irq
);
178 if (irq
!= s
->running_irq
) {
179 /* Complete an IRQ that is not currently running. */
180 int tmp
= s
->running_irq
;
181 while (s
->last_active
[tmp
] != 1023) {
182 if (s
->last_active
[tmp
] == irq
) {
183 s
->last_active
[tmp
] = s
->last_active
[irq
];
186 tmp
= s
->last_active
[tmp
];
192 /* Complete the current running IRQ. */
193 gic_set_running_irq(s
, s
->last_active
[s
->running_irq
]);
197 static uint32_t gic_dist_readb(void *opaque
, target_phys_addr_t offset
)
199 gic_state
*s
= (gic_state
*)opaque
;
204 offset
-= s
->base
+ 0x1000;
205 if (offset
< 0x100) {
209 return (GIC_NIRQ
/ 32) - 1;
213 } else if (offset
< 0x200) {
214 /* Interrupt Set/Clear Enable. */
216 irq
= (offset
- 0x100) * 8;
218 irq
= (offset
- 0x180) * 8;
222 for (i
= 0; i
< 8; i
++) {
223 if (GIC_TEST_ENABLED(irq
+ i
)) {
227 } else if (offset
< 0x300) {
228 /* Interrupt Set/Clear Pending. */
230 irq
= (offset
- 0x200) * 8;
232 irq
= (offset
- 0x280) * 8;
236 for (i
= 0; i
< 8; i
++) {
237 if (GIC_TEST_PENDING(irq
+ i
)) {
241 } else if (offset
< 0x400) {
242 /* Interrupt Active. */
243 irq
= (offset
- 0x300) * 8;
247 for (i
= 0; i
< 8; i
++) {
248 if (GIC_TEST_ACTIVE(irq
+ i
)) {
252 } else if (offset
< 0x800) {
253 /* Interrupt Priority. */
254 irq
= offset
- 0x400;
257 res
= s
->priority
[irq
];
258 } else if (offset
< 0xc00) {
259 /* Interrupt CPU Target. */
260 irq
= offset
- 0x800;
263 res
= s
->irq_target
[irq
];
264 } else if (offset
< 0xf00) {
265 /* Interrupt Configuration. */
266 irq
= (offset
- 0xc00) * 2;
270 for (i
= 0; i
< 4; i
++) {
271 if (GIC_TEST_MODEL(irq
+ i
))
272 res
|= (1 << (i
* 2));
273 if (GIC_TEST_TRIGGER(irq
+ i
))
274 res
|= (2 << (i
* 2));
276 } else if (offset
< 0xfe0) {
278 } else /* offset >= 0xfe0 */ {
282 res
= gic_id
[(offset
- 0xfe0) >> 2];
287 cpu_abort (cpu_single_env
, "gic_dist_readb: Bad offset %x\n", offset
);
291 static uint32_t gic_dist_readw(void *opaque
, target_phys_addr_t offset
)
294 val
= gic_dist_readb(opaque
, offset
);
295 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
299 static uint32_t gic_dist_readl(void *opaque
, target_phys_addr_t offset
)
302 val
= gic_dist_readw(opaque
, offset
);
303 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
307 static void gic_dist_writeb(void *opaque
, target_phys_addr_t offset
,
310 gic_state
*s
= (gic_state
*)opaque
;
314 offset
-= s
->base
+ 0x1000;
315 if (offset
< 0x100) {
317 s
->enabled
= (value
& 1);
318 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
319 } else if (offset
< 4) {
324 } else if (offset
< 0x180) {
325 /* Interrupt Set Enable. */
326 irq
= (offset
- 0x100) * 8;
329 for (i
= 0; i
< 8; i
++) {
330 if (value
& (1 << i
)) {
331 if (!GIC_TEST_ENABLED(irq
+ i
))
332 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
333 GIC_SET_ENABLED(irq
+ i
);
334 /* If a raised level triggered IRQ enabled then mark
336 if (GIC_TEST_LEVEL(irq
+ i
) && !GIC_TEST_TRIGGER(irq
+ i
))
337 GIC_SET_PENDING(irq
+ i
);
340 } else if (offset
< 0x200) {
341 /* Interrupt Clear Enable. */
342 irq
= (offset
- 0x180) * 8;
345 for (i
= 0; i
< 8; i
++) {
346 if (value
& (1 << i
)) {
347 if (GIC_TEST_ENABLED(irq
+ i
))
348 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
349 GIC_CLEAR_ENABLED(irq
+ i
);
352 } else if (offset
< 0x280) {
353 /* Interrupt Set Pending. */
354 irq
= (offset
- 0x200) * 8;
357 for (i
= 0; i
< 8; i
++) {
358 if (value
& (1 << i
)) {
359 GIC_SET_PENDING(irq
+ i
);
362 } else if (offset
< 0x300) {
363 /* Interrupt Clear Pending. */
364 irq
= (offset
- 0x280) * 8;
367 for (i
= 0; i
< 8; i
++) {
368 if (value
& (1 << i
)) {
369 GIC_CLEAR_PENDING(irq
+ i
);
372 } else if (offset
< 0x400) {
373 /* Interrupt Active. */
375 } else if (offset
< 0x800) {
376 /* Interrupt Priority. */
377 irq
= offset
- 0x400;
380 s
->priority
[irq
] = value
;
381 } else if (offset
< 0xc00) {
382 /* Interrupt CPU Target. */
383 irq
= offset
- 0x800;
386 s
->irq_target
[irq
] = value
;
387 } else if (offset
< 0xf00) {
388 /* Interrupt Configuration. */
389 irq
= (offset
- 0xc00) * 4;
392 for (i
= 0; i
< 4; i
++) {
393 if (value
& (1 << (i
* 2))) {
394 GIC_SET_MODEL(irq
+ i
);
396 GIC_CLEAR_MODEL(irq
+ i
);
398 if (value
& (2 << (i
* 2))) {
399 GIC_SET_TRIGGER(irq
+ i
);
401 GIC_CLEAR_TRIGGER(irq
+ i
);
405 /* 0xf00 is only handled for word writes. */
411 cpu_abort (cpu_single_env
, "gic_dist_writeb: Bad offset %x\n", offset
);
414 static void gic_dist_writew(void *opaque
, target_phys_addr_t offset
,
417 gic_state
*s
= (gic_state
*)opaque
;
418 if (offset
- s
->base
== 0xf00) {
419 GIC_SET_PENDING(value
& 0x3ff);
423 gic_dist_writeb(opaque
, offset
, value
& 0xff);
424 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
427 static void gic_dist_writel(void *opaque
, target_phys_addr_t offset
,
430 gic_dist_writew(opaque
, offset
, value
& 0xffff);
431 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
434 static CPUReadMemoryFunc
*gic_dist_readfn
[] = {
440 static CPUWriteMemoryFunc
*gic_dist_writefn
[] = {
446 static uint32_t gic_cpu_read(void *opaque
, target_phys_addr_t offset
)
448 gic_state
*s
= (gic_state
*)opaque
;
451 case 0x00: /* Control */
452 return s
->cpu_enabled
;
453 case 0x04: /* Priority mask */
454 return s
->priority_mask
;
455 case 0x08: /* Binary Point */
456 /* ??? Not implemented. */
458 case 0x0c: /* Acknowledge */
459 return gic_acknowledge_irq(s
);
460 case 0x14: /* Runing Priority */
461 return s
->running_priority
;
462 case 0x18: /* Highest Pending Interrupt */
463 return s
->current_pending
;
465 cpu_abort (cpu_single_env
, "gic_cpu_writeb: Bad offset %x\n", offset
);
470 static void gic_cpu_write(void *opaque
, target_phys_addr_t offset
,
473 gic_state
*s
= (gic_state
*)opaque
;
476 case 0x00: /* Control */
477 s
->cpu_enabled
= (value
& 1);
478 DPRINTF("CPU %sabled\n", s
->cpu_enabled
? "En" : "Dis");
480 case 0x04: /* Priority mask */
481 s
->priority_mask
= (value
& 0x3ff);
483 case 0x08: /* Binary Point */
484 /* ??? Not implemented. */
486 case 0x10: /* End Of Interrupt */
487 return gic_complete_irq(s
, value
& 0x3ff);
489 cpu_abort (cpu_single_env
, "gic_cpu_writeb: Bad offset %x\n", offset
);
495 static CPUReadMemoryFunc
*gic_cpu_readfn
[] = {
501 static CPUWriteMemoryFunc
*gic_cpu_writefn
[] = {
507 static void gic_reset(gic_state
*s
)
510 memset(s
->irq_state
, 0, GIC_NIRQ
* sizeof(gic_irq_state
));
511 s
->priority_mask
= 0xf0;
512 s
->current_pending
= 1023;
513 s
->running_irq
= 1023;
514 s
->running_priority
= 0x100;
515 for (i
= 0; i
< 15; i
++) {
523 void *arm_gic_init(uint32_t base
, void *parent
, int parent_irq
)
528 s
= (gic_state
*)qemu_mallocz(sizeof(gic_state
));
531 s
->handler
= gic_set_irq
;
533 s
->parent_irq
= parent_irq
;
534 if (base
!= 0xffffffff) {
535 iomemtype
= cpu_register_io_memory(0, gic_cpu_readfn
,
537 cpu_register_physical_memory(base
, 0x00000fff, iomemtype
);
538 iomemtype
= cpu_register_io_memory(0, gic_dist_readfn
,
539 gic_dist_writefn
, s
);
540 cpu_register_physical_memory(base
+ 0x1000, 0x00000fff, iomemtype
);