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.
13 * It is compiled in two ways:
14 * (1) as a standalone file to produce a sysbus device which is a GIC
15 * that can be used on the realview board and as one of the builtin
16 * private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
17 * (2) by being directly #included into armv7m_nvic.c to produce the
23 /* Maximum number of possible interrupts, determined by the GIC architecture */
24 #define GIC_MAXIRQ 1020
25 /* First 32 are private to each CPU (SGIs and PPIs). */
26 #define GIC_INTERNAL 32
27 /* Maximum number of possible CPU interfaces, determined by GIC architecture */
33 #define DPRINTF(fmt, ...) \
34 do { printf("arm_gic: " fmt , ## __VA_ARGS__); } while (0)
36 #define DPRINTF(fmt, ...) do {} while(0)
40 static const uint8_t gic_id[] =
41 { 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 };
42 /* The NVIC has 16 internal vectors. However these are not exposed
43 through the normal GIC interface. */
44 #define GIC_BASE_IRQ 32
46 static const uint8_t gic_id[] =
47 { 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
48 #define GIC_BASE_IRQ 0
51 #define FROM_SYSBUSGIC(type, dev) \
52 DO_UPCAST(type, gic, FROM_SYSBUS(gic_state, dev))
54 typedef struct gic_irq_state
56 /* The enable bits are only banked for per-cpu interrupts. */
57 unsigned enabled:NCPU;
58 unsigned pending:NCPU;
61 unsigned model:1; /* 0 = N:N, 1 = 1:N */
62 unsigned trigger:1; /* nonzero = edge triggered. */
65 #define ALL_CPU_MASK ((unsigned)(((1 << NCPU) - 1)))
66 #define NUM_CPU(s) ((s)->num_cpu)
68 #define GIC_SET_ENABLED(irq, cm) s->irq_state[irq].enabled |= (cm)
69 #define GIC_CLEAR_ENABLED(irq, cm) s->irq_state[irq].enabled &= ~(cm)
70 #define GIC_TEST_ENABLED(irq, cm) ((s->irq_state[irq].enabled & (cm)) != 0)
71 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
72 #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
73 #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
74 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
75 #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
76 #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
77 #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
78 #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
79 #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
80 #define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
81 #define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
82 #define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
83 #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
84 #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
85 #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
86 #define GIC_GET_PRIORITY(irq, cpu) (((irq) < GIC_INTERNAL) ? \
87 s->priority1[irq][cpu] : \
88 s->priority2[(irq) - GIC_INTERNAL])
90 #define GIC_TARGET(irq) 1
92 #define GIC_TARGET(irq) s->irq_target[irq]
95 typedef struct gic_state
98 qemu_irq parent_irq[NCPU];
100 int cpu_enabled[NCPU];
102 gic_irq_state irq_state[GIC_MAXIRQ];
103 int irq_target[GIC_MAXIRQ];
104 int priority1[GIC_INTERNAL][NCPU];
105 int priority2[GIC_MAXIRQ - GIC_INTERNAL];
106 int last_active[GIC_MAXIRQ][NCPU];
108 int priority_mask[NCPU];
109 int running_irq[NCPU];
110 int running_priority[NCPU];
111 int current_pending[NCPU];
115 MemoryRegion iomem; /* Distributor */
116 /* This is just so we can have an opaque pointer which identifies
117 * both this GIC and which CPU interface we should be accessing.
119 struct gic_state *backref[NCPU];
120 MemoryRegion cpuiomem[NCPU+1]; /* CPU interfaces */
124 static inline int gic_get_current_cpu(gic_state *s)
126 if (s->num_cpu > 1) {
127 return cpu_single_env->cpu_index;
132 /* TODO: Many places that call this routine could be optimized. */
133 /* Update interrupt status after enabled or pending bits have been changed. */
134 static void gic_update(gic_state *s)
143 for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
145 s->current_pending[cpu] = 1023;
146 if (!s->enabled || !s->cpu_enabled[cpu]) {
147 qemu_irq_lower(s->parent_irq[cpu]);
152 for (irq = 0; irq < s->num_irq; irq++) {
153 if (GIC_TEST_ENABLED(irq, cm) && GIC_TEST_PENDING(irq, cm)) {
154 if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
155 best_prio = GIC_GET_PRIORITY(irq, cpu);
161 if (best_prio <= s->priority_mask[cpu]) {
162 s->current_pending[cpu] = best_irq;
163 if (best_prio < s->running_priority[cpu]) {
164 DPRINTF("Raised pending IRQ %d\n", best_irq);
168 qemu_set_irq(s->parent_irq[cpu], level);
173 static void gic_set_pending_private(gic_state *s, int cpu, int irq)
177 if (GIC_TEST_PENDING(irq, cm))
180 DPRINTF("Set %d pending cpu %d\n", irq, cpu);
181 GIC_SET_PENDING(irq, cm);
186 /* Process a change in an external IRQ input. */
187 static void gic_set_irq(void *opaque, int irq, int level)
189 /* Meaning of the 'irq' parameter:
190 * [0..N-1] : external interrupts
191 * [N..N+31] : PPI (internal) interrupts for CPU 0
192 * [N+32..N+63] : PPI (internal interrupts for CPU 1
195 gic_state *s = (gic_state *)opaque;
197 if (irq < (s->num_irq - GIC_INTERNAL)) {
198 /* The first external input line is internal interrupt 32. */
201 target = GIC_TARGET(irq);
204 irq -= (s->num_irq - GIC_INTERNAL);
205 cpu = irq / GIC_INTERNAL;
211 if (level == GIC_TEST_LEVEL(irq, cm)) {
216 GIC_SET_LEVEL(irq, cm);
217 if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
218 DPRINTF("Set %d pending mask %x\n", irq, target);
219 GIC_SET_PENDING(irq, target);
222 GIC_CLEAR_LEVEL(irq, cm);
227 static void gic_set_running_irq(gic_state *s, int cpu, int irq)
229 s->running_irq[cpu] = irq;
231 s->running_priority[cpu] = 0x100;
233 s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
238 static uint32_t gic_acknowledge_irq(gic_state *s, int cpu)
242 new_irq = s->current_pending[cpu];
244 || GIC_GET_PRIORITY(new_irq, cpu) >= s->running_priority[cpu]) {
245 DPRINTF("ACK no pending IRQ\n");
248 s->last_active[new_irq][cpu] = s->running_irq[cpu];
249 /* Clear pending flags for both level and edge triggered interrupts.
250 Level triggered IRQs will be reasserted once they become inactive. */
251 GIC_CLEAR_PENDING(new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm);
252 gic_set_running_irq(s, cpu, new_irq);
253 DPRINTF("ACK %d\n", new_irq);
257 static void gic_complete_irq(gic_state * s, int cpu, int irq)
261 DPRINTF("EOI %d\n", irq);
262 if (irq >= s->num_irq) {
263 /* This handles two cases:
264 * 1. If software writes the ID of a spurious interrupt [ie 1023]
265 * to the GICC_EOIR, the GIC ignores that write.
266 * 2. If software writes the number of a non-existent interrupt
267 * this must be a subcase of "value written does not match the last
268 * valid interrupt value read from the Interrupt Acknowledge
269 * register" and so this is UNPREDICTABLE. We choose to ignore it.
273 if (s->running_irq[cpu] == 1023)
274 return; /* No active IRQ. */
275 /* Mark level triggered interrupts as pending if they are still
277 if (!GIC_TEST_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
278 && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
279 DPRINTF("Set %d pending mask %x\n", irq, cm);
280 GIC_SET_PENDING(irq, cm);
283 if (irq != s->running_irq[cpu]) {
284 /* Complete an IRQ that is not currently running. */
285 int tmp = s->running_irq[cpu];
286 while (s->last_active[tmp][cpu] != 1023) {
287 if (s->last_active[tmp][cpu] == irq) {
288 s->last_active[tmp][cpu] = s->last_active[irq][cpu];
291 tmp = s->last_active[tmp][cpu];
297 /* Complete the current running IRQ. */
298 gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
302 static uint32_t gic_dist_readb(void *opaque, target_phys_addr_t offset)
304 gic_state *s = (gic_state *)opaque;
312 cpu = gic_get_current_cpu(s);
314 if (offset < 0x100) {
319 return ((s->num_irq / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
322 if (offset >= 0x80) {
323 /* Interrupt Security , RAZ/WI */
328 } else if (offset < 0x200) {
329 /* Interrupt Set/Clear Enable. */
331 irq = (offset - 0x100) * 8;
333 irq = (offset - 0x180) * 8;
335 if (irq >= s->num_irq)
338 for (i = 0; i < 8; i++) {
339 if (GIC_TEST_ENABLED(irq + i, cm)) {
343 } else if (offset < 0x300) {
344 /* Interrupt Set/Clear Pending. */
346 irq = (offset - 0x200) * 8;
348 irq = (offset - 0x280) * 8;
350 if (irq >= s->num_irq)
353 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
354 for (i = 0; i < 8; i++) {
355 if (GIC_TEST_PENDING(irq + i, mask)) {
359 } else if (offset < 0x400) {
360 /* Interrupt Active. */
361 irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
362 if (irq >= s->num_irq)
365 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
366 for (i = 0; i < 8; i++) {
367 if (GIC_TEST_ACTIVE(irq + i, mask)) {
371 } else if (offset < 0x800) {
372 /* Interrupt Priority. */
373 irq = (offset - 0x400) + GIC_BASE_IRQ;
374 if (irq >= s->num_irq)
376 res = GIC_GET_PRIORITY(irq, cpu);
378 } else if (offset < 0xc00) {
379 /* Interrupt CPU Target. */
380 irq = (offset - 0x800) + GIC_BASE_IRQ;
381 if (irq >= s->num_irq)
383 if (irq >= 29 && irq <= 31) {
386 res = GIC_TARGET(irq);
388 } else if (offset < 0xf00) {
389 /* Interrupt Configuration. */
390 irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
391 if (irq >= s->num_irq)
394 for (i = 0; i < 4; i++) {
395 if (GIC_TEST_MODEL(irq + i))
396 res |= (1 << (i * 2));
397 if (GIC_TEST_TRIGGER(irq + i))
398 res |= (2 << (i * 2));
401 } else if (offset < 0xfe0) {
403 } else /* offset >= 0xfe0 */ {
407 res = gic_id[(offset - 0xfe0) >> 2];
412 hw_error("gic_dist_readb: Bad offset %x\n", (int)offset);
416 static uint32_t gic_dist_readw(void *opaque, target_phys_addr_t offset)
419 val = gic_dist_readb(opaque, offset);
420 val |= gic_dist_readb(opaque, offset + 1) << 8;
424 static uint32_t gic_dist_readl(void *opaque, target_phys_addr_t offset)
428 gic_state *s = (gic_state *)opaque;
431 if (addr < 0x100 || addr > 0xd00)
432 return nvic_readl(s, addr);
434 val = gic_dist_readw(opaque, offset);
435 val |= gic_dist_readw(opaque, offset + 2) << 16;
439 static void gic_dist_writeb(void *opaque, target_phys_addr_t offset,
442 gic_state *s = (gic_state *)opaque;
447 cpu = gic_get_current_cpu(s);
448 if (offset < 0x100) {
453 s->enabled = (value & 1);
454 DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
455 } else if (offset < 4) {
457 } else if (offset >= 0x80) {
458 /* Interrupt Security Registers, RAZ/WI */
463 } else if (offset < 0x180) {
464 /* Interrupt Set Enable. */
465 irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
466 if (irq >= s->num_irq)
470 for (i = 0; i < 8; i++) {
471 if (value & (1 << i)) {
472 int mask = (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq);
473 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
475 if (!GIC_TEST_ENABLED(irq + i, cm)) {
476 DPRINTF("Enabled IRQ %d\n", irq + i);
478 GIC_SET_ENABLED(irq + i, cm);
479 /* If a raised level triggered IRQ enabled then mark
481 if (GIC_TEST_LEVEL(irq + i, mask)
482 && !GIC_TEST_TRIGGER(irq + i)) {
483 DPRINTF("Set %d pending mask %x\n", irq + i, mask);
484 GIC_SET_PENDING(irq + i, mask);
488 } else if (offset < 0x200) {
489 /* Interrupt Clear Enable. */
490 irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
491 if (irq >= s->num_irq)
495 for (i = 0; i < 8; i++) {
496 if (value & (1 << i)) {
497 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
499 if (GIC_TEST_ENABLED(irq + i, cm)) {
500 DPRINTF("Disabled IRQ %d\n", irq + i);
502 GIC_CLEAR_ENABLED(irq + i, cm);
505 } else if (offset < 0x280) {
506 /* Interrupt Set Pending. */
507 irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
508 if (irq >= s->num_irq)
513 for (i = 0; i < 8; i++) {
514 if (value & (1 << i)) {
515 GIC_SET_PENDING(irq + i, GIC_TARGET(irq));
518 } else if (offset < 0x300) {
519 /* Interrupt Clear Pending. */
520 irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
521 if (irq >= s->num_irq)
523 for (i = 0; i < 8; i++) {
524 /* ??? This currently clears the pending bit for all CPUs, even
525 for per-CPU interrupts. It's unclear whether this is the
527 if (value & (1 << i)) {
528 GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
531 } else if (offset < 0x400) {
532 /* Interrupt Active. */
534 } else if (offset < 0x800) {
535 /* Interrupt Priority. */
536 irq = (offset - 0x400) + GIC_BASE_IRQ;
537 if (irq >= s->num_irq)
539 if (irq < GIC_INTERNAL) {
540 s->priority1[irq][cpu] = value;
542 s->priority2[irq - GIC_INTERNAL] = value;
545 } else if (offset < 0xc00) {
546 /* Interrupt CPU Target. */
547 irq = (offset - 0x800) + GIC_BASE_IRQ;
548 if (irq >= s->num_irq)
552 else if (irq < GIC_INTERNAL)
553 value = ALL_CPU_MASK;
554 s->irq_target[irq] = value & ALL_CPU_MASK;
555 } else if (offset < 0xf00) {
556 /* Interrupt Configuration. */
557 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
558 if (irq >= s->num_irq)
560 if (irq < GIC_INTERNAL)
562 for (i = 0; i < 4; i++) {
563 if (value & (1 << (i * 2))) {
564 GIC_SET_MODEL(irq + i);
566 GIC_CLEAR_MODEL(irq + i);
568 if (value & (2 << (i * 2))) {
569 GIC_SET_TRIGGER(irq + i);
571 GIC_CLEAR_TRIGGER(irq + i);
576 /* 0xf00 is only handled for 32-bit writes. */
582 hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset);
585 static void gic_dist_writew(void *opaque, target_phys_addr_t offset,
588 gic_dist_writeb(opaque, offset, value & 0xff);
589 gic_dist_writeb(opaque, offset + 1, value >> 8);
592 static void gic_dist_writel(void *opaque, target_phys_addr_t offset,
595 gic_state *s = (gic_state *)opaque;
599 if (addr < 0x100 || (addr > 0xd00 && addr != 0xf00)) {
600 nvic_writel(s, addr, value);
604 if (offset == 0xf00) {
609 cpu = gic_get_current_cpu(s);
611 switch ((value >> 24) & 3) {
613 mask = (value >> 16) & ALL_CPU_MASK;
616 mask = ALL_CPU_MASK ^ (1 << cpu);
622 DPRINTF("Bad Soft Int target filter\n");
626 GIC_SET_PENDING(irq, mask);
630 gic_dist_writew(opaque, offset, value & 0xffff);
631 gic_dist_writew(opaque, offset + 2, value >> 16);
634 static const MemoryRegionOps gic_dist_ops = {
636 .read = { gic_dist_readb, gic_dist_readw, gic_dist_readl, },
637 .write = { gic_dist_writeb, gic_dist_writew, gic_dist_writel, },
639 .endianness = DEVICE_NATIVE_ENDIAN,
643 static uint32_t gic_cpu_read(gic_state *s, int cpu, int offset)
646 case 0x00: /* Control */
647 return s->cpu_enabled[cpu];
648 case 0x04: /* Priority mask */
649 return s->priority_mask[cpu];
650 case 0x08: /* Binary Point */
651 /* ??? Not implemented. */
653 case 0x0c: /* Acknowledge */
654 return gic_acknowledge_irq(s, cpu);
655 case 0x14: /* Running Priority */
656 return s->running_priority[cpu];
657 case 0x18: /* Highest Pending Interrupt */
658 return s->current_pending[cpu];
660 hw_error("gic_cpu_read: Bad offset %x\n", (int)offset);
665 static void gic_cpu_write(gic_state *s, int cpu, int offset, uint32_t value)
668 case 0x00: /* Control */
669 s->cpu_enabled[cpu] = (value & 1);
670 DPRINTF("CPU %d %sabled\n", cpu, s->cpu_enabled ? "En" : "Dis");
672 case 0x04: /* Priority mask */
673 s->priority_mask[cpu] = (value & 0xff);
675 case 0x08: /* Binary Point */
676 /* ??? Not implemented. */
678 case 0x10: /* End Of Interrupt */
679 return gic_complete_irq(s, cpu, value & 0x3ff);
681 hw_error("gic_cpu_write: Bad offset %x\n", (int)offset);
687 /* Wrappers to read/write the GIC CPU interface for the current CPU */
688 static uint64_t gic_thiscpu_read(void *opaque, target_phys_addr_t addr,
691 gic_state *s = (gic_state *)opaque;
692 return gic_cpu_read(s, gic_get_current_cpu(s), addr);
695 static void gic_thiscpu_write(void *opaque, target_phys_addr_t addr,
696 uint64_t value, unsigned size)
698 gic_state *s = (gic_state *)opaque;
699 gic_cpu_write(s, gic_get_current_cpu(s), addr, value);
702 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
703 * These just decode the opaque pointer into gic_state* + cpu id.
705 static uint64_t gic_do_cpu_read(void *opaque, target_phys_addr_t addr,
708 gic_state **backref = (gic_state **)opaque;
709 gic_state *s = *backref;
710 int id = (backref - s->backref);
711 return gic_cpu_read(s, id, addr);
714 static void gic_do_cpu_write(void *opaque, target_phys_addr_t addr,
715 uint64_t value, unsigned size)
717 gic_state **backref = (gic_state **)opaque;
718 gic_state *s = *backref;
719 int id = (backref - s->backref);
720 gic_cpu_write(s, id, addr, value);
723 static const MemoryRegionOps gic_thiscpu_ops = {
724 .read = gic_thiscpu_read,
725 .write = gic_thiscpu_write,
726 .endianness = DEVICE_NATIVE_ENDIAN,
729 static const MemoryRegionOps gic_cpu_ops = {
730 .read = gic_do_cpu_read,
731 .write = gic_do_cpu_write,
732 .endianness = DEVICE_NATIVE_ENDIAN,
736 static void gic_reset(DeviceState *dev)
738 gic_state *s = FROM_SYSBUS(gic_state, sysbus_from_qdev(dev));
740 memset(s->irq_state, 0, GIC_MAXIRQ * sizeof(gic_irq_state));
741 for (i = 0 ; i < NUM_CPU(s); i++) {
742 s->priority_mask[i] = 0xf0;
743 s->current_pending[i] = 1023;
744 s->running_irq[i] = 1023;
745 s->running_priority[i] = 0x100;
746 s->cpu_enabled[i] = 0;
748 for (i = 0; i < 16; i++) {
749 GIC_SET_ENABLED(i, ALL_CPU_MASK);
755 static void gic_save(QEMUFile *f, void *opaque)
757 gic_state *s = (gic_state *)opaque;
761 qemu_put_be32(f, s->enabled);
762 for (i = 0; i < NUM_CPU(s); i++) {
763 qemu_put_be32(f, s->cpu_enabled[i]);
764 for (j = 0; j < GIC_INTERNAL; j++)
765 qemu_put_be32(f, s->priority1[j][i]);
766 for (j = 0; j < s->num_irq; j++)
767 qemu_put_be32(f, s->last_active[j][i]);
768 qemu_put_be32(f, s->priority_mask[i]);
769 qemu_put_be32(f, s->running_irq[i]);
770 qemu_put_be32(f, s->running_priority[i]);
771 qemu_put_be32(f, s->current_pending[i]);
773 for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) {
774 qemu_put_be32(f, s->priority2[i]);
776 for (i = 0; i < s->num_irq; i++) {
777 qemu_put_be32(f, s->irq_target[i]);
778 qemu_put_byte(f, s->irq_state[i].enabled);
779 qemu_put_byte(f, s->irq_state[i].pending);
780 qemu_put_byte(f, s->irq_state[i].active);
781 qemu_put_byte(f, s->irq_state[i].level);
782 qemu_put_byte(f, s->irq_state[i].model);
783 qemu_put_byte(f, s->irq_state[i].trigger);
787 static int gic_load(QEMUFile *f, void *opaque, int version_id)
789 gic_state *s = (gic_state *)opaque;
793 if (version_id != 3) {
797 s->enabled = qemu_get_be32(f);
798 for (i = 0; i < NUM_CPU(s); i++) {
799 s->cpu_enabled[i] = qemu_get_be32(f);
800 for (j = 0; j < GIC_INTERNAL; j++)
801 s->priority1[j][i] = qemu_get_be32(f);
802 for (j = 0; j < s->num_irq; j++)
803 s->last_active[j][i] = qemu_get_be32(f);
804 s->priority_mask[i] = qemu_get_be32(f);
805 s->running_irq[i] = qemu_get_be32(f);
806 s->running_priority[i] = qemu_get_be32(f);
807 s->current_pending[i] = qemu_get_be32(f);
809 for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) {
810 s->priority2[i] = qemu_get_be32(f);
812 for (i = 0; i < s->num_irq; i++) {
813 s->irq_target[i] = qemu_get_be32(f);
814 s->irq_state[i].enabled = qemu_get_byte(f);
815 s->irq_state[i].pending = qemu_get_byte(f);
816 s->irq_state[i].active = qemu_get_byte(f);
817 s->irq_state[i].level = qemu_get_byte(f);
818 s->irq_state[i].model = qemu_get_byte(f);
819 s->irq_state[i].trigger = qemu_get_byte(f);
825 static void gic_init(gic_state *s, int num_irq)
829 if (s->num_cpu > NCPU) {
830 hw_error("requested %u CPUs exceeds GIC maximum %d\n",
833 s->num_irq = num_irq + GIC_BASE_IRQ;
834 if (s->num_irq > GIC_MAXIRQ) {
835 hw_error("requested %u interrupt lines exceeds GIC maximum %d\n",
836 num_irq, GIC_MAXIRQ);
838 /* ITLinesNumber is represented as (N / 32) - 1 (see
839 * gic_dist_readb) so this is an implementation imposed
840 * restriction, not an architectural one:
842 if (s->num_irq < 32 || (s->num_irq % 32)) {
843 hw_error("%d interrupt lines unsupported: not divisible by 32\n",
847 i = s->num_irq - GIC_INTERNAL;
849 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
850 * GPIO array layout is thus:
852 * [N..N+31] PPIs for CPU 0
853 * [N+32..N+63] PPIs for CPU 1
856 i += (GIC_INTERNAL * s->num_cpu);
858 qdev_init_gpio_in(&s->busdev.qdev, gic_set_irq, i);
859 for (i = 0; i < NUM_CPU(s); i++) {
860 sysbus_init_irq(&s->busdev, &s->parent_irq[i]);
862 memory_region_init_io(&s->iomem, &gic_dist_ops, s, "gic_dist", 0x1000);
864 /* Memory regions for the CPU interfaces (NVIC doesn't have these):
865 * a region for "CPU interface for this core", then a region for
866 * "CPU interface for core 0", "for core 1", ...
867 * NB that the memory region size of 0x100 applies for the 11MPCore
868 * and also cores following the GIC v1 spec (ie A9).
869 * GIC v2 defines a larger memory region (0x1000) so this will need
870 * to be extended when we implement A15.
872 memory_region_init_io(&s->cpuiomem[0], &gic_thiscpu_ops, s,
874 for (i = 0; i < NUM_CPU(s); i++) {
876 memory_region_init_io(&s->cpuiomem[i+1], &gic_cpu_ops, &s->backref[i],
881 register_savevm(NULL, "arm_gic", -1, 3, gic_save, gic_load, s);
886 static int arm_gic_init(SysBusDevice *dev)
888 /* Device instance init function for the GIC sysbus device */
890 gic_state *s = FROM_SYSBUS(gic_state, dev);
891 gic_init(s, s->num_irq);
893 sysbus_init_mmio(dev, &s->iomem);
894 /* cpu interfaces (one for "current cpu" plus one per cpu) */
895 for (i = 0; i <= NUM_CPU(s); i++) {
896 sysbus_init_mmio(dev, &s->cpuiomem[i]);
901 static Property arm_gic_properties[] = {
902 DEFINE_PROP_UINT32("num-cpu", gic_state, num_cpu, 1),
903 DEFINE_PROP_UINT32("num-irq", gic_state, num_irq, 32),
904 DEFINE_PROP_END_OF_LIST(),
907 static void arm_gic_class_init(ObjectClass *klass, void *data)
909 DeviceClass *dc = DEVICE_CLASS(klass);
910 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
911 sbc->init = arm_gic_init;
912 dc->props = arm_gic_properties;
913 dc->reset = gic_reset;
917 static TypeInfo arm_gic_info = {
919 .parent = TYPE_SYS_BUS_DEVICE,
920 .instance_size = sizeof(gic_state),
921 .class_init = arm_gic_class_init,
924 static void arm_gic_register_types(void)
926 type_register_static(&arm_gic_info);
929 type_init(arm_gic_register_types)