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. */
14 /* Maximum number of possible interrupts, determined by the GIC architecture */
15 #define GIC_MAXIRQ 1020
16 /* First 32 are private to each CPU (SGIs and PPIs). */
17 #define GIC_INTERNAL 32
21 #define DPRINTF(fmt, ...) \
22 do { printf("arm_gic: " fmt , ## __VA_ARGS__); } while (0)
24 #define DPRINTF(fmt, ...) do {} while(0)
28 static const uint8_t gic_id
[] =
29 { 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 };
30 /* The NVIC has 16 internal vectors. However these are not exposed
31 through the normal GIC interface. */
32 #define GIC_BASE_IRQ 32
34 static const uint8_t gic_id
[] =
35 { 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
36 #define GIC_BASE_IRQ 0
39 #define FROM_SYSBUSGIC(type, dev) \
40 DO_UPCAST(type, gic, FROM_SYSBUS(gic_state, dev))
42 typedef struct gic_irq_state
44 /* The enable bits are only banked for per-cpu interrupts. */
45 unsigned enabled
:NCPU
;
46 unsigned pending
:NCPU
;
49 unsigned model
:1; /* 0 = N:N, 1 = 1:N */
50 unsigned trigger
:1; /* nonzero = edge triggered. */
53 #define ALL_CPU_MASK ((1 << NCPU) - 1)
55 #define NUM_CPU(s) ((s)->num_cpu)
60 #define GIC_SET_ENABLED(irq, cm) s->irq_state[irq].enabled |= (cm)
61 #define GIC_CLEAR_ENABLED(irq, cm) s->irq_state[irq].enabled &= ~(cm)
62 #define GIC_TEST_ENABLED(irq, cm) ((s->irq_state[irq].enabled & (cm)) != 0)
63 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
64 #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
65 #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
66 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
67 #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
68 #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
69 #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
70 #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
71 #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
72 #define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
73 #define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
74 #define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
75 #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
76 #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
77 #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
78 #define GIC_GET_PRIORITY(irq, cpu) (((irq) < GIC_INTERNAL) ? \
79 s->priority1[irq][cpu] : \
80 s->priority2[(irq) - GIC_INTERNAL])
82 #define GIC_TARGET(irq) 1
84 #define GIC_TARGET(irq) s->irq_target[irq]
87 typedef struct gic_state
90 qemu_irq parent_irq
[NCPU
];
92 int cpu_enabled
[NCPU
];
94 gic_irq_state irq_state
[GIC_MAXIRQ
];
96 int irq_target
[GIC_MAXIRQ
];
98 int priority1
[GIC_INTERNAL
][NCPU
];
99 int priority2
[GIC_MAXIRQ
- GIC_INTERNAL
];
100 int last_active
[GIC_MAXIRQ
][NCPU
];
102 int priority_mask
[NCPU
];
103 int running_irq
[NCPU
];
104 int running_priority
[NCPU
];
105 int current_pending
[NCPU
];
111 MemoryRegion iomem
; /* Distributor */
113 /* This is just so we can have an opaque pointer which identifies
114 * both this GIC and which CPU interface we should be accessing.
116 struct gic_state
*backref
[NCPU
];
117 MemoryRegion cpuiomem
[NCPU
+1]; /* CPU interfaces */
122 /* TODO: Many places that call this routine could be optimized. */
123 /* Update interrupt status after enabled or pending bits have been changed. */
124 static void gic_update(gic_state
*s
)
133 for (cpu
= 0; cpu
< NUM_CPU(s
); cpu
++) {
135 s
->current_pending
[cpu
] = 1023;
136 if (!s
->enabled
|| !s
->cpu_enabled
[cpu
]) {
137 qemu_irq_lower(s
->parent_irq
[cpu
]);
142 for (irq
= 0; irq
< s
->num_irq
; irq
++) {
143 if (GIC_TEST_ENABLED(irq
, cm
) && GIC_TEST_PENDING(irq
, cm
)) {
144 if (GIC_GET_PRIORITY(irq
, cpu
) < best_prio
) {
145 best_prio
= GIC_GET_PRIORITY(irq
, cpu
);
151 if (best_prio
<= s
->priority_mask
[cpu
]) {
152 s
->current_pending
[cpu
] = best_irq
;
153 if (best_prio
< s
->running_priority
[cpu
]) {
154 DPRINTF("Raised pending IRQ %d\n", best_irq
);
158 qemu_set_irq(s
->parent_irq
[cpu
], level
);
162 static void __attribute__((unused
))
163 gic_set_pending_private(gic_state
*s
, int cpu
, int irq
)
167 if (GIC_TEST_PENDING(irq
, cm
))
170 DPRINTF("Set %d pending cpu %d\n", irq
, cpu
);
171 GIC_SET_PENDING(irq
, cm
);
175 /* Process a change in an external IRQ input. */
176 static void gic_set_irq(void *opaque
, int irq
, int level
)
178 gic_state
*s
= (gic_state
*)opaque
;
179 /* The first external input line is internal interrupt 32. */
181 if (level
== GIC_TEST_LEVEL(irq
, ALL_CPU_MASK
))
185 GIC_SET_LEVEL(irq
, ALL_CPU_MASK
);
186 if (GIC_TEST_TRIGGER(irq
) || GIC_TEST_ENABLED(irq
, ALL_CPU_MASK
)) {
187 DPRINTF("Set %d pending mask %x\n", irq
, GIC_TARGET(irq
));
188 GIC_SET_PENDING(irq
, GIC_TARGET(irq
));
191 GIC_CLEAR_LEVEL(irq
, ALL_CPU_MASK
);
196 static void gic_set_running_irq(gic_state
*s
, int cpu
, int irq
)
198 s
->running_irq
[cpu
] = irq
;
200 s
->running_priority
[cpu
] = 0x100;
202 s
->running_priority
[cpu
] = GIC_GET_PRIORITY(irq
, cpu
);
207 static uint32_t gic_acknowledge_irq(gic_state
*s
, int cpu
)
211 new_irq
= s
->current_pending
[cpu
];
213 || GIC_GET_PRIORITY(new_irq
, cpu
) >= s
->running_priority
[cpu
]) {
214 DPRINTF("ACK no pending IRQ\n");
217 s
->last_active
[new_irq
][cpu
] = s
->running_irq
[cpu
];
218 /* Clear pending flags for both level and edge triggered interrupts.
219 Level triggered IRQs will be reasserted once they become inactive. */
220 GIC_CLEAR_PENDING(new_irq
, GIC_TEST_MODEL(new_irq
) ? ALL_CPU_MASK
: cm
);
221 gic_set_running_irq(s
, cpu
, new_irq
);
222 DPRINTF("ACK %d\n", new_irq
);
226 static void gic_complete_irq(gic_state
* s
, int cpu
, int irq
)
230 DPRINTF("EOI %d\n", irq
);
231 if (irq
>= s
->num_irq
) {
232 /* This handles two cases:
233 * 1. If software writes the ID of a spurious interrupt [ie 1023]
234 * to the GICC_EOIR, the GIC ignores that write.
235 * 2. If software writes the number of a non-existent interrupt
236 * this must be a subcase of "value written does not match the last
237 * valid interrupt value read from the Interrupt Acknowledge
238 * register" and so this is UNPREDICTABLE. We choose to ignore it.
242 if (s
->running_irq
[cpu
] == 1023)
243 return; /* No active IRQ. */
244 /* Mark level triggered interrupts as pending if they are still
246 if (!GIC_TEST_TRIGGER(irq
) && GIC_TEST_ENABLED(irq
, cm
)
247 && GIC_TEST_LEVEL(irq
, cm
) && (GIC_TARGET(irq
) & cm
) != 0) {
248 DPRINTF("Set %d pending mask %x\n", irq
, cm
);
249 GIC_SET_PENDING(irq
, cm
);
252 if (irq
!= s
->running_irq
[cpu
]) {
253 /* Complete an IRQ that is not currently running. */
254 int tmp
= s
->running_irq
[cpu
];
255 while (s
->last_active
[tmp
][cpu
] != 1023) {
256 if (s
->last_active
[tmp
][cpu
] == irq
) {
257 s
->last_active
[tmp
][cpu
] = s
->last_active
[irq
][cpu
];
260 tmp
= s
->last_active
[tmp
][cpu
];
266 /* Complete the current running IRQ. */
267 gic_set_running_irq(s
, cpu
, s
->last_active
[s
->running_irq
[cpu
]][cpu
]);
271 static uint32_t gic_dist_readb(void *opaque
, target_phys_addr_t offset
)
273 gic_state
*s
= (gic_state
*)opaque
;
281 cpu
= gic_get_current_cpu();
283 if (offset
< 0x100) {
288 return ((s
->num_irq
/ 32) - 1) | ((NUM_CPU(s
) - 1) << 5);
291 if (offset
>= 0x80) {
292 /* Interrupt Security , RAZ/WI */
297 } else if (offset
< 0x200) {
298 /* Interrupt Set/Clear Enable. */
300 irq
= (offset
- 0x100) * 8;
302 irq
= (offset
- 0x180) * 8;
304 if (irq
>= s
->num_irq
)
307 for (i
= 0; i
< 8; i
++) {
308 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
312 } else if (offset
< 0x300) {
313 /* Interrupt Set/Clear Pending. */
315 irq
= (offset
- 0x200) * 8;
317 irq
= (offset
- 0x280) * 8;
319 if (irq
>= s
->num_irq
)
322 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
323 for (i
= 0; i
< 8; i
++) {
324 if (GIC_TEST_PENDING(irq
+ i
, mask
)) {
328 } else if (offset
< 0x400) {
329 /* Interrupt Active. */
330 irq
= (offset
- 0x300) * 8 + GIC_BASE_IRQ
;
331 if (irq
>= s
->num_irq
)
334 mask
= (irq
< GIC_INTERNAL
) ? cm
: ALL_CPU_MASK
;
335 for (i
= 0; i
< 8; i
++) {
336 if (GIC_TEST_ACTIVE(irq
+ i
, mask
)) {
340 } else if (offset
< 0x800) {
341 /* Interrupt Priority. */
342 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
343 if (irq
>= s
->num_irq
)
345 res
= GIC_GET_PRIORITY(irq
, cpu
);
347 } else if (offset
< 0xc00) {
348 /* Interrupt CPU Target. */
349 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
350 if (irq
>= s
->num_irq
)
352 if (irq
>= 29 && irq
<= 31) {
355 res
= GIC_TARGET(irq
);
357 } else if (offset
< 0xf00) {
358 /* Interrupt Configuration. */
359 irq
= (offset
- 0xc00) * 2 + GIC_BASE_IRQ
;
360 if (irq
>= s
->num_irq
)
363 for (i
= 0; i
< 4; i
++) {
364 if (GIC_TEST_MODEL(irq
+ i
))
365 res
|= (1 << (i
* 2));
366 if (GIC_TEST_TRIGGER(irq
+ i
))
367 res
|= (2 << (i
* 2));
370 } else if (offset
< 0xfe0) {
372 } else /* offset >= 0xfe0 */ {
376 res
= gic_id
[(offset
- 0xfe0) >> 2];
381 hw_error("gic_dist_readb: Bad offset %x\n", (int)offset
);
385 static uint32_t gic_dist_readw(void *opaque
, target_phys_addr_t offset
)
388 val
= gic_dist_readb(opaque
, offset
);
389 val
|= gic_dist_readb(opaque
, offset
+ 1) << 8;
393 static uint32_t gic_dist_readl(void *opaque
, target_phys_addr_t offset
)
397 gic_state
*s
= (gic_state
*)opaque
;
400 if (addr
< 0x100 || addr
> 0xd00)
401 return nvic_readl(s
, addr
);
403 val
= gic_dist_readw(opaque
, offset
);
404 val
|= gic_dist_readw(opaque
, offset
+ 2) << 16;
408 static void gic_dist_writeb(void *opaque
, target_phys_addr_t offset
,
411 gic_state
*s
= (gic_state
*)opaque
;
416 cpu
= gic_get_current_cpu();
417 if (offset
< 0x100) {
422 s
->enabled
= (value
& 1);
423 DPRINTF("Distribution %sabled\n", s
->enabled
? "En" : "Dis");
424 } else if (offset
< 4) {
426 } else if (offset
>= 0x80) {
427 /* Interrupt Security Registers, RAZ/WI */
432 } else if (offset
< 0x180) {
433 /* Interrupt Set Enable. */
434 irq
= (offset
- 0x100) * 8 + GIC_BASE_IRQ
;
435 if (irq
>= s
->num_irq
)
439 for (i
= 0; i
< 8; i
++) {
440 if (value
& (1 << i
)) {
441 int mask
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : GIC_TARGET(irq
);
442 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
444 if (!GIC_TEST_ENABLED(irq
+ i
, cm
)) {
445 DPRINTF("Enabled IRQ %d\n", irq
+ i
);
447 GIC_SET_ENABLED(irq
+ i
, cm
);
448 /* If a raised level triggered IRQ enabled then mark
450 if (GIC_TEST_LEVEL(irq
+ i
, mask
)
451 && !GIC_TEST_TRIGGER(irq
+ i
)) {
452 DPRINTF("Set %d pending mask %x\n", irq
+ i
, mask
);
453 GIC_SET_PENDING(irq
+ i
, mask
);
457 } else if (offset
< 0x200) {
458 /* Interrupt Clear Enable. */
459 irq
= (offset
- 0x180) * 8 + GIC_BASE_IRQ
;
460 if (irq
>= s
->num_irq
)
464 for (i
= 0; i
< 8; i
++) {
465 if (value
& (1 << i
)) {
466 int cm
= (irq
< GIC_INTERNAL
) ? (1 << cpu
) : ALL_CPU_MASK
;
468 if (GIC_TEST_ENABLED(irq
+ i
, cm
)) {
469 DPRINTF("Disabled IRQ %d\n", irq
+ i
);
471 GIC_CLEAR_ENABLED(irq
+ i
, cm
);
474 } else if (offset
< 0x280) {
475 /* Interrupt Set Pending. */
476 irq
= (offset
- 0x200) * 8 + GIC_BASE_IRQ
;
477 if (irq
>= s
->num_irq
)
482 for (i
= 0; i
< 8; i
++) {
483 if (value
& (1 << i
)) {
484 GIC_SET_PENDING(irq
+ i
, GIC_TARGET(irq
));
487 } else if (offset
< 0x300) {
488 /* Interrupt Clear Pending. */
489 irq
= (offset
- 0x280) * 8 + GIC_BASE_IRQ
;
490 if (irq
>= s
->num_irq
)
492 for (i
= 0; i
< 8; i
++) {
493 /* ??? This currently clears the pending bit for all CPUs, even
494 for per-CPU interrupts. It's unclear whether this is the
496 if (value
& (1 << i
)) {
497 GIC_CLEAR_PENDING(irq
+ i
, ALL_CPU_MASK
);
500 } else if (offset
< 0x400) {
501 /* Interrupt Active. */
503 } else if (offset
< 0x800) {
504 /* Interrupt Priority. */
505 irq
= (offset
- 0x400) + GIC_BASE_IRQ
;
506 if (irq
>= s
->num_irq
)
508 if (irq
< GIC_INTERNAL
) {
509 s
->priority1
[irq
][cpu
] = value
;
511 s
->priority2
[irq
- GIC_INTERNAL
] = value
;
514 } else if (offset
< 0xc00) {
515 /* Interrupt CPU Target. */
516 irq
= (offset
- 0x800) + GIC_BASE_IRQ
;
517 if (irq
>= s
->num_irq
)
521 else if (irq
< GIC_INTERNAL
)
522 value
= ALL_CPU_MASK
;
523 s
->irq_target
[irq
] = value
& ALL_CPU_MASK
;
524 } else if (offset
< 0xf00) {
525 /* Interrupt Configuration. */
526 irq
= (offset
- 0xc00) * 4 + GIC_BASE_IRQ
;
527 if (irq
>= s
->num_irq
)
529 if (irq
< GIC_INTERNAL
)
531 for (i
= 0; i
< 4; i
++) {
532 if (value
& (1 << (i
* 2))) {
533 GIC_SET_MODEL(irq
+ i
);
535 GIC_CLEAR_MODEL(irq
+ i
);
537 if (value
& (2 << (i
* 2))) {
538 GIC_SET_TRIGGER(irq
+ i
);
540 GIC_CLEAR_TRIGGER(irq
+ i
);
545 /* 0xf00 is only handled for 32-bit writes. */
551 hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset
);
554 static void gic_dist_writew(void *opaque
, target_phys_addr_t offset
,
557 gic_dist_writeb(opaque
, offset
, value
& 0xff);
558 gic_dist_writeb(opaque
, offset
+ 1, value
>> 8);
561 static void gic_dist_writel(void *opaque
, target_phys_addr_t offset
,
564 gic_state
*s
= (gic_state
*)opaque
;
568 if (addr
< 0x100 || (addr
> 0xd00 && addr
!= 0xf00)) {
569 nvic_writel(s
, addr
, value
);
573 if (offset
== 0xf00) {
578 cpu
= gic_get_current_cpu();
580 switch ((value
>> 24) & 3) {
582 mask
= (value
>> 16) & ALL_CPU_MASK
;
585 mask
= ALL_CPU_MASK
^ (1 << cpu
);
591 DPRINTF("Bad Soft Int target filter\n");
595 GIC_SET_PENDING(irq
, mask
);
599 gic_dist_writew(opaque
, offset
, value
& 0xffff);
600 gic_dist_writew(opaque
, offset
+ 2, value
>> 16);
603 static const MemoryRegionOps gic_dist_ops
= {
605 .read
= { gic_dist_readb
, gic_dist_readw
, gic_dist_readl
, },
606 .write
= { gic_dist_writeb
, gic_dist_writew
, gic_dist_writel
, },
608 .endianness
= DEVICE_NATIVE_ENDIAN
,
612 static uint32_t gic_cpu_read(gic_state
*s
, int cpu
, int offset
)
615 case 0x00: /* Control */
616 return s
->cpu_enabled
[cpu
];
617 case 0x04: /* Priority mask */
618 return s
->priority_mask
[cpu
];
619 case 0x08: /* Binary Point */
620 /* ??? Not implemented. */
622 case 0x0c: /* Acknowledge */
623 return gic_acknowledge_irq(s
, cpu
);
624 case 0x14: /* Running Priority */
625 return s
->running_priority
[cpu
];
626 case 0x18: /* Highest Pending Interrupt */
627 return s
->current_pending
[cpu
];
629 hw_error("gic_cpu_read: Bad offset %x\n", (int)offset
);
634 static void gic_cpu_write(gic_state
*s
, int cpu
, int offset
, uint32_t value
)
637 case 0x00: /* Control */
638 s
->cpu_enabled
[cpu
] = (value
& 1);
639 DPRINTF("CPU %d %sabled\n", cpu
, s
->cpu_enabled
? "En" : "Dis");
641 case 0x04: /* Priority mask */
642 s
->priority_mask
[cpu
] = (value
& 0xff);
644 case 0x08: /* Binary Point */
645 /* ??? Not implemented. */
647 case 0x10: /* End Of Interrupt */
648 return gic_complete_irq(s
, cpu
, value
& 0x3ff);
650 hw_error("gic_cpu_write: Bad offset %x\n", (int)offset
);
656 /* Wrappers to read/write the GIC CPU interface for the current CPU */
657 static uint64_t gic_thiscpu_read(void *opaque
, target_phys_addr_t addr
,
660 gic_state
*s
= (gic_state
*)opaque
;
661 return gic_cpu_read(s
, gic_get_current_cpu(), addr
& 0xff);
664 static void gic_thiscpu_write(void *opaque
, target_phys_addr_t addr
,
665 uint64_t value
, unsigned size
)
667 gic_state
*s
= (gic_state
*)opaque
;
668 gic_cpu_write(s
, gic_get_current_cpu(), addr
& 0xff, value
);
671 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
672 * These just decode the opaque pointer into gic_state* + cpu id.
674 static uint64_t gic_do_cpu_read(void *opaque
, target_phys_addr_t addr
,
677 gic_state
**backref
= (gic_state
**)opaque
;
678 gic_state
*s
= *backref
;
679 int id
= (backref
- s
->backref
);
680 return gic_cpu_read(s
, id
, addr
& 0xff);
683 static void gic_do_cpu_write(void *opaque
, target_phys_addr_t addr
,
684 uint64_t value
, unsigned size
)
686 gic_state
**backref
= (gic_state
**)opaque
;
687 gic_state
*s
= *backref
;
688 int id
= (backref
- s
->backref
);
689 gic_cpu_write(s
, id
, addr
& 0xff, value
);
692 static const MemoryRegionOps gic_thiscpu_ops
= {
693 .read
= gic_thiscpu_read
,
694 .write
= gic_thiscpu_write
,
695 .endianness
= DEVICE_NATIVE_ENDIAN
,
698 static const MemoryRegionOps gic_cpu_ops
= {
699 .read
= gic_do_cpu_read
,
700 .write
= gic_do_cpu_write
,
701 .endianness
= DEVICE_NATIVE_ENDIAN
,
705 static void gic_reset(gic_state
*s
)
708 memset(s
->irq_state
, 0, GIC_MAXIRQ
* sizeof(gic_irq_state
));
709 for (i
= 0 ; i
< NUM_CPU(s
); i
++) {
710 s
->priority_mask
[i
] = 0xf0;
711 s
->current_pending
[i
] = 1023;
712 s
->running_irq
[i
] = 1023;
713 s
->running_priority
[i
] = 0x100;
715 /* The NVIC doesn't have per-cpu interfaces, so enable by default. */
716 s
->cpu_enabled
[i
] = 1;
718 s
->cpu_enabled
[i
] = 0;
721 for (i
= 0; i
< 16; i
++) {
722 GIC_SET_ENABLED(i
, ALL_CPU_MASK
);
726 /* The NVIC is always enabled. */
733 static void gic_save(QEMUFile
*f
, void *opaque
)
735 gic_state
*s
= (gic_state
*)opaque
;
739 qemu_put_be32(f
, s
->enabled
);
740 for (i
= 0; i
< NUM_CPU(s
); i
++) {
741 qemu_put_be32(f
, s
->cpu_enabled
[i
]);
742 for (j
= 0; j
< GIC_INTERNAL
; j
++)
743 qemu_put_be32(f
, s
->priority1
[j
][i
]);
744 for (j
= 0; j
< s
->num_irq
; j
++)
745 qemu_put_be32(f
, s
->last_active
[j
][i
]);
746 qemu_put_be32(f
, s
->priority_mask
[i
]);
747 qemu_put_be32(f
, s
->running_irq
[i
]);
748 qemu_put_be32(f
, s
->running_priority
[i
]);
749 qemu_put_be32(f
, s
->current_pending
[i
]);
751 for (i
= 0; i
< s
->num_irq
- GIC_INTERNAL
; i
++) {
752 qemu_put_be32(f
, s
->priority2
[i
]);
754 for (i
= 0; i
< s
->num_irq
; i
++) {
756 qemu_put_be32(f
, s
->irq_target
[i
]);
758 qemu_put_byte(f
, s
->irq_state
[i
].enabled
);
759 qemu_put_byte(f
, s
->irq_state
[i
].pending
);
760 qemu_put_byte(f
, s
->irq_state
[i
].active
);
761 qemu_put_byte(f
, s
->irq_state
[i
].level
);
762 qemu_put_byte(f
, s
->irq_state
[i
].model
);
763 qemu_put_byte(f
, s
->irq_state
[i
].trigger
);
767 static int gic_load(QEMUFile
*f
, void *opaque
, int version_id
)
769 gic_state
*s
= (gic_state
*)opaque
;
776 s
->enabled
= qemu_get_be32(f
);
777 for (i
= 0; i
< NUM_CPU(s
); i
++) {
778 s
->cpu_enabled
[i
] = qemu_get_be32(f
);
779 for (j
= 0; j
< GIC_INTERNAL
; j
++)
780 s
->priority1
[j
][i
] = qemu_get_be32(f
);
781 for (j
= 0; j
< s
->num_irq
; j
++)
782 s
->last_active
[j
][i
] = qemu_get_be32(f
);
783 s
->priority_mask
[i
] = qemu_get_be32(f
);
784 s
->running_irq
[i
] = qemu_get_be32(f
);
785 s
->running_priority
[i
] = qemu_get_be32(f
);
786 s
->current_pending
[i
] = qemu_get_be32(f
);
788 for (i
= 0; i
< s
->num_irq
- GIC_INTERNAL
; i
++) {
789 s
->priority2
[i
] = qemu_get_be32(f
);
791 for (i
= 0; i
< s
->num_irq
; i
++) {
793 s
->irq_target
[i
] = qemu_get_be32(f
);
795 s
->irq_state
[i
].enabled
= qemu_get_byte(f
);
796 s
->irq_state
[i
].pending
= qemu_get_byte(f
);
797 s
->irq_state
[i
].active
= qemu_get_byte(f
);
798 s
->irq_state
[i
].level
= qemu_get_byte(f
);
799 s
->irq_state
[i
].model
= qemu_get_byte(f
);
800 s
->irq_state
[i
].trigger
= qemu_get_byte(f
);
807 static void gic_init(gic_state
*s
, int num_cpu
, int num_irq
)
809 static void gic_init(gic_state
*s
, int num_irq
)
815 s
->num_cpu
= num_cpu
;
817 s
->num_irq
= num_irq
+ GIC_BASE_IRQ
;
818 if (s
->num_irq
> GIC_MAXIRQ
) {
819 hw_error("requested %u interrupt lines exceeds GIC maximum %d\n",
820 num_irq
, GIC_MAXIRQ
);
822 /* ITLinesNumber is represented as (N / 32) - 1 (see
823 * gic_dist_readb) so this is an implementation imposed
824 * restriction, not an architectural one:
826 if (s
->num_irq
< 32 || (s
->num_irq
% 32)) {
827 hw_error("%d interrupt lines unsupported: not divisible by 32\n",
831 qdev_init_gpio_in(&s
->busdev
.qdev
, gic_set_irq
, s
->num_irq
- GIC_INTERNAL
);
832 for (i
= 0; i
< NUM_CPU(s
); i
++) {
833 sysbus_init_irq(&s
->busdev
, &s
->parent_irq
[i
]);
835 memory_region_init_io(&s
->iomem
, &gic_dist_ops
, s
, "gic_dist", 0x1000);
837 /* Memory regions for the CPU interfaces (NVIC doesn't have these):
838 * a region for "CPU interface for this core", then a region for
839 * "CPU interface for core 0", "for core 1", ...
840 * NB that the memory region size of 0x100 applies for the 11MPCore
841 * and also cores following the GIC v1 spec (ie A9).
842 * GIC v2 defines a larger memory region (0x1000) so this will need
843 * to be extended when we implement A15.
845 memory_region_init_io(&s
->cpuiomem
[0], &gic_thiscpu_ops
, s
,
847 for (i
= 0; i
< NUM_CPU(s
); i
++) {
849 memory_region_init_io(&s
->cpuiomem
[i
+1], &gic_cpu_ops
, &s
->backref
[i
],
855 register_savevm(NULL
, "arm_gic", -1, 2, gic_save
, gic_load
, s
);