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
21 #include "qemu/osdep.h"
22 #include "hw/sysbus.h"
23 #include "gic_internal.h"
24 #include "qapi/error.h"
30 #define DPRINTF(fmt, ...) \
31 do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
33 #define DPRINTF(fmt, ...) do {} while(0)
36 static const uint8_t gic_id_11mpcore[] = {
37 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
40 static const uint8_t gic_id_gicv1[] = {
41 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
44 static const uint8_t gic_id_gicv2[] = {
45 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
48 static inline int gic_get_current_cpu(GICState *s)
51 return current_cpu->cpu_index;
56 /* Return true if this GIC config has interrupt groups, which is
57 * true if we're a GICv2, or a GICv1 with the security extensions.
59 static inline bool gic_has_groups(GICState *s)
61 return s->revision == 2 || s->security_extn;
64 /* TODO: Many places that call this routine could be optimized. */
65 /* Update interrupt status after enabled or pending bits have been changed. */
66 void gic_update(GICState *s)
71 int irq_level, fiq_level;
75 for (cpu = 0; cpu < s->num_cpu; cpu++) {
77 s->current_pending[cpu] = 1023;
78 if (!(s->ctlr & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1))
79 || !(s->cpu_ctlr[cpu] & (GICC_CTLR_EN_GRP0 | GICC_CTLR_EN_GRP1))) {
80 qemu_irq_lower(s->parent_irq[cpu]);
81 qemu_irq_lower(s->parent_fiq[cpu]);
86 for (irq = 0; irq < s->num_irq; irq++) {
87 if (GIC_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
88 (irq < GIC_INTERNAL || GIC_TARGET(irq) & cm)) {
89 if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
90 best_prio = GIC_GET_PRIORITY(irq, cpu);
96 irq_level = fiq_level = 0;
98 if (best_prio < s->priority_mask[cpu]) {
99 s->current_pending[cpu] = best_irq;
100 if (best_prio < s->running_priority[cpu]) {
101 int group = GIC_TEST_GROUP(best_irq, cm);
103 if (extract32(s->ctlr, group, 1) &&
104 extract32(s->cpu_ctlr[cpu], group, 1)) {
105 if (group == 0 && s->cpu_ctlr[cpu] & GICC_CTLR_FIQ_EN) {
106 DPRINTF("Raised pending FIQ %d (cpu %d)\n",
110 DPRINTF("Raised pending IRQ %d (cpu %d)\n",
118 qemu_set_irq(s->parent_irq[cpu], irq_level);
119 qemu_set_irq(s->parent_fiq[cpu], fiq_level);
123 void gic_set_pending_private(GICState *s, int cpu, int irq)
127 if (gic_test_pending(s, irq, cm)) {
131 DPRINTF("Set %d pending cpu %d\n", irq, cpu);
132 GIC_SET_PENDING(irq, cm);
136 static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
140 GIC_SET_LEVEL(irq, cm);
141 if (GIC_TEST_EDGE_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
142 DPRINTF("Set %d pending mask %x\n", irq, target);
143 GIC_SET_PENDING(irq, target);
146 GIC_CLEAR_LEVEL(irq, cm);
150 static void gic_set_irq_generic(GICState *s, int irq, int level,
154 GIC_SET_LEVEL(irq, cm);
155 DPRINTF("Set %d pending mask %x\n", irq, target);
156 if (GIC_TEST_EDGE_TRIGGER(irq)) {
157 GIC_SET_PENDING(irq, target);
160 GIC_CLEAR_LEVEL(irq, cm);
164 /* Process a change in an external IRQ input. */
165 static void gic_set_irq(void *opaque, int irq, int level)
167 /* Meaning of the 'irq' parameter:
168 * [0..N-1] : external interrupts
169 * [N..N+31] : PPI (internal) interrupts for CPU 0
170 * [N+32..N+63] : PPI (internal interrupts for CPU 1
173 GICState *s = (GICState *)opaque;
175 if (irq < (s->num_irq - GIC_INTERNAL)) {
176 /* The first external input line is internal interrupt 32. */
179 target = GIC_TARGET(irq);
182 irq -= (s->num_irq - GIC_INTERNAL);
183 cpu = irq / GIC_INTERNAL;
189 assert(irq >= GIC_NR_SGIS);
191 if (level == GIC_TEST_LEVEL(irq, cm)) {
195 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
196 gic_set_irq_11mpcore(s, irq, level, cm, target);
198 gic_set_irq_generic(s, irq, level, cm, target);
204 static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
207 uint16_t pending_irq = s->current_pending[cpu];
209 if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
210 int group = GIC_TEST_GROUP(pending_irq, (1 << cpu));
211 /* On a GIC without the security extensions, reading this register
212 * behaves in the same way as a secure access to a GIC with them.
214 bool secure = !s->security_extn || attrs.secure;
216 if (group == 0 && !secure) {
217 /* Group0 interrupts hidden from Non-secure access */
220 if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
221 /* Group1 interrupts only seen by Secure access if
230 static int gic_get_group_priority(GICState *s, int cpu, int irq)
232 /* Return the group priority of the specified interrupt
233 * (which is the top bits of its priority, with the number
234 * of bits masked determined by the applicable binary point register).
239 if (gic_has_groups(s) &&
240 !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
241 GIC_TEST_GROUP(irq, (1 << cpu))) {
247 /* a BPR of 0 means the group priority bits are [7:1];
248 * a BPR of 1 means they are [7:2], and so on down to
249 * a BPR of 7 meaning no group priority bits at all.
251 mask = ~0U << ((bpr & 7) + 1);
253 return GIC_GET_PRIORITY(irq, cpu) & mask;
256 static void gic_activate_irq(GICState *s, int cpu, int irq)
258 /* Set the appropriate Active Priority Register bit for this IRQ,
259 * and update the running priority.
261 int prio = gic_get_group_priority(s, cpu, irq);
262 int preemption_level = prio >> (GIC_MIN_BPR + 1);
263 int regno = preemption_level / 32;
264 int bitno = preemption_level % 32;
266 if (gic_has_groups(s) && GIC_TEST_GROUP(irq, (1 << cpu))) {
267 s->nsapr[regno][cpu] |= (1 << bitno);
269 s->apr[regno][cpu] |= (1 << bitno);
272 s->running_priority[cpu] = prio;
273 GIC_SET_ACTIVE(irq, 1 << cpu);
276 static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
278 /* Recalculate the current running priority for this CPU based
279 * on the set bits in the Active Priority Registers.
282 for (i = 0; i < GIC_NR_APRS; i++) {
283 uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
287 return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
292 static void gic_drop_prio(GICState *s, int cpu, int group)
294 /* Drop the priority of the currently active interrupt in the
297 * Note that we can guarantee (because of the requirement to nest
298 * GICC_IAR reads [which activate an interrupt and raise priority]
299 * with GICC_EOIR writes [which drop the priority for the interrupt])
300 * that the interrupt we're being called for is the highest priority
301 * active interrupt, meaning that it has the lowest set bit in the
304 * If the guest does not honour the ordering constraints then the
305 * behaviour of the GIC is UNPREDICTABLE, which for us means that
306 * the values of the APR registers might become incorrect and the
307 * running priority will be wrong, so interrupts that should preempt
308 * might not do so, and interrupts that should not preempt might do so.
312 for (i = 0; i < GIC_NR_APRS; i++) {
313 uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
317 /* Clear lowest set bit */
322 s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
325 uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
330 /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
331 * for the case where this GIC supports grouping and the pending interrupt
332 * is in the wrong group.
334 irq = gic_get_current_pending_irq(s, cpu, attrs);
336 if (irq >= GIC_MAXIRQ) {
337 DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
341 if (GIC_GET_PRIORITY(irq, cpu) >= s->running_priority[cpu]) {
342 DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
346 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
347 /* Clear pending flags for both level and edge triggered interrupts.
348 * Level triggered IRQs will be reasserted once they become inactive.
350 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
353 if (irq < GIC_NR_SGIS) {
354 /* Lookup the source CPU for the SGI and clear this in the
355 * sgi_pending map. Return the src and clear the overall pending
356 * state on this CPU if the SGI is not pending from any CPUs.
358 assert(s->sgi_pending[irq][cpu] != 0);
359 src = ctz32(s->sgi_pending[irq][cpu]);
360 s->sgi_pending[irq][cpu] &= ~(1 << src);
361 if (s->sgi_pending[irq][cpu] == 0) {
362 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
364 ret = irq | ((src & 0x7) << 10);
366 /* Clear pending state for both level and edge triggered
367 * interrupts. (level triggered interrupts with an active line
368 * remain pending, see gic_test_pending)
370 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
375 gic_activate_irq(s, cpu, irq);
377 DPRINTF("ACK %d\n", irq);
381 void gic_set_priority(GICState *s, int cpu, int irq, uint8_t val,
384 if (s->security_extn && !attrs.secure) {
385 if (!GIC_TEST_GROUP(irq, (1 << cpu))) {
386 return; /* Ignore Non-secure access of Group0 IRQ */
388 val = 0x80 | (val >> 1); /* Non-secure view */
391 if (irq < GIC_INTERNAL) {
392 s->priority1[irq][cpu] = val;
394 s->priority2[(irq) - GIC_INTERNAL] = val;
398 static uint32_t gic_get_priority(GICState *s, int cpu, int irq,
401 uint32_t prio = GIC_GET_PRIORITY(irq, cpu);
403 if (s->security_extn && !attrs.secure) {
404 if (!GIC_TEST_GROUP(irq, (1 << cpu))) {
405 return 0; /* Non-secure access cannot read priority of Group0 IRQ */
407 prio = (prio << 1) & 0xff; /* Non-secure view */
412 static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
415 if (s->security_extn && !attrs.secure) {
416 if (s->priority_mask[cpu] & 0x80) {
417 /* Priority Mask in upper half */
418 pmask = 0x80 | (pmask >> 1);
420 /* Non-secure write ignored if priority mask is in lower half */
424 s->priority_mask[cpu] = pmask;
427 static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
429 uint32_t pmask = s->priority_mask[cpu];
431 if (s->security_extn && !attrs.secure) {
433 /* Priority Mask in upper half, return Non-secure view */
434 pmask = (pmask << 1) & 0xff;
436 /* Priority Mask in lower half, RAZ */
443 static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
445 uint32_t ret = s->cpu_ctlr[cpu];
447 if (s->security_extn && !attrs.secure) {
448 /* Construct the NS banked view of GICC_CTLR from the correct
449 * bits of the S banked view. We don't need to move the bypass
450 * control bits because we don't implement that (IMPDEF) part
451 * of the GIC architecture.
453 ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
458 static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
463 if (s->security_extn && !attrs.secure) {
464 /* The NS view can only write certain bits in the register;
465 * the rest are unchanged
467 mask = GICC_CTLR_EN_GRP1;
468 if (s->revision == 2) {
469 mask |= GICC_CTLR_EOIMODE_NS;
471 s->cpu_ctlr[cpu] &= ~mask;
472 s->cpu_ctlr[cpu] |= (value << 1) & mask;
474 if (s->revision == 2) {
475 mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
477 mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
479 s->cpu_ctlr[cpu] = value & mask;
481 DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
482 "Group1 Interrupts %sabled\n", cpu,
483 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
484 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
487 static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
489 if (s->security_extn && !attrs.secure) {
490 if (s->running_priority[cpu] & 0x80) {
491 /* Running priority in upper half of range: return the Non-secure
492 * view of the priority.
494 return s->running_priority[cpu] << 1;
496 /* Running priority in lower half of range: RAZ */
500 return s->running_priority[cpu];
504 /* Return true if we should split priority drop and interrupt deactivation,
505 * ie whether the relevant EOIMode bit is set.
507 static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
509 if (s->revision != 2) {
510 /* Before GICv2 prio-drop and deactivate are not separable */
513 if (s->security_extn && !attrs.secure) {
514 return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
516 return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
519 static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
522 int group = gic_has_groups(s) && GIC_TEST_GROUP(irq, cm);
524 if (!gic_eoi_split(s, cpu, attrs)) {
525 /* This is UNPREDICTABLE; we choose to ignore it */
526 qemu_log_mask(LOG_GUEST_ERROR,
527 "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
531 if (s->security_extn && !attrs.secure && !group) {
532 DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
536 GIC_CLEAR_ACTIVE(irq, cm);
539 void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
544 DPRINTF("EOI %d\n", irq);
545 if (irq >= s->num_irq) {
546 /* This handles two cases:
547 * 1. If software writes the ID of a spurious interrupt [ie 1023]
548 * to the GICC_EOIR, the GIC ignores that write.
549 * 2. If software writes the number of a non-existent interrupt
550 * this must be a subcase of "value written does not match the last
551 * valid interrupt value read from the Interrupt Acknowledge
552 * register" and so this is UNPREDICTABLE. We choose to ignore it.
556 if (s->running_priority[cpu] == 0x100) {
557 return; /* No active IRQ. */
560 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
561 /* Mark level triggered interrupts as pending if they are still
563 if (!GIC_TEST_EDGE_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
564 && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
565 DPRINTF("Set %d pending mask %x\n", irq, cm);
566 GIC_SET_PENDING(irq, cm);
570 group = gic_has_groups(s) && GIC_TEST_GROUP(irq, cm);
572 if (s->security_extn && !attrs.secure && !group) {
573 DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
577 /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
578 * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
579 * i.e. go ahead and complete the irq anyway.
582 gic_drop_prio(s, cpu, group);
584 /* In GICv2 the guest can choose to split priority-drop and deactivate */
585 if (!gic_eoi_split(s, cpu, attrs)) {
586 GIC_CLEAR_ACTIVE(irq, cm);
591 static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
593 GICState *s = (GICState *)opaque;
601 cpu = gic_get_current_cpu(s);
603 if (offset < 0x100) {
604 if (offset == 0) { /* GICD_CTLR */
605 if (s->security_extn && !attrs.secure) {
606 /* The NS bank of this register is just an alias of the
607 * EnableGrp1 bit in the S bank version.
609 return extract32(s->ctlr, 1, 1);
615 /* Interrupt Controller Type Register */
616 return ((s->num_irq / 32) - 1)
617 | ((s->num_cpu - 1) << 5)
618 | (s->security_extn << 10);
621 if (offset >= 0x80) {
622 /* Interrupt Group Registers: these RAZ/WI if this is an NS
623 * access to a GIC with the security extensions, or if the GIC
624 * doesn't have groups at all.
627 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
628 /* Every byte offset holds 8 group status bits */
629 irq = (offset - 0x080) * 8 + GIC_BASE_IRQ;
630 if (irq >= s->num_irq) {
633 for (i = 0; i < 8; i++) {
634 if (GIC_TEST_GROUP(irq + i, cm)) {
642 } else if (offset < 0x200) {
643 /* Interrupt Set/Clear Enable. */
645 irq = (offset - 0x100) * 8;
647 irq = (offset - 0x180) * 8;
649 if (irq >= s->num_irq)
652 for (i = 0; i < 8; i++) {
653 if (GIC_TEST_ENABLED(irq + i, cm)) {
657 } else if (offset < 0x300) {
658 /* Interrupt Set/Clear Pending. */
660 irq = (offset - 0x200) * 8;
662 irq = (offset - 0x280) * 8;
664 if (irq >= s->num_irq)
667 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
668 for (i = 0; i < 8; i++) {
669 if (gic_test_pending(s, irq + i, mask)) {
673 } else if (offset < 0x400) {
674 /* Interrupt Active. */
675 irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
676 if (irq >= s->num_irq)
679 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
680 for (i = 0; i < 8; i++) {
681 if (GIC_TEST_ACTIVE(irq + i, mask)) {
685 } else if (offset < 0x800) {
686 /* Interrupt Priority. */
687 irq = (offset - 0x400) + GIC_BASE_IRQ;
688 if (irq >= s->num_irq)
690 res = gic_get_priority(s, cpu, irq, attrs);
691 } else if (offset < 0xc00) {
692 /* Interrupt CPU Target. */
693 if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
694 /* For uniprocessor GICs these RAZ/WI */
697 irq = (offset - 0x800) + GIC_BASE_IRQ;
698 if (irq >= s->num_irq) {
701 if (irq >= 29 && irq <= 31) {
704 res = GIC_TARGET(irq);
707 } else if (offset < 0xf00) {
708 /* Interrupt Configuration. */
709 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
710 if (irq >= s->num_irq)
713 for (i = 0; i < 4; i++) {
714 if (GIC_TEST_MODEL(irq + i))
715 res |= (1 << (i * 2));
716 if (GIC_TEST_EDGE_TRIGGER(irq + i))
717 res |= (2 << (i * 2));
719 } else if (offset < 0xf10) {
721 } else if (offset < 0xf30) {
722 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
726 if (offset < 0xf20) {
727 /* GICD_CPENDSGIRn */
728 irq = (offset - 0xf10);
730 irq = (offset - 0xf20);
731 /* GICD_SPENDSGIRn */
734 res = s->sgi_pending[irq][cpu];
735 } else if (offset < 0xfd0) {
737 } else if (offset < 0x1000) {
741 switch (s->revision) {
743 res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
746 res = gic_id_gicv1[(offset - 0xfd0) >> 2];
749 res = gic_id_gicv2[(offset - 0xfd0) >> 2];
752 /* Shouldn't be able to get here */
759 g_assert_not_reached();
763 qemu_log_mask(LOG_GUEST_ERROR,
764 "gic_dist_readb: Bad offset %x\n", (int)offset);
768 static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
769 unsigned size, MemTxAttrs attrs)
773 *data = gic_dist_readb(opaque, offset, attrs);
776 *data = gic_dist_readb(opaque, offset, attrs);
777 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
780 *data = gic_dist_readb(opaque, offset, attrs);
781 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
782 *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
783 *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
790 static void gic_dist_writeb(void *opaque, hwaddr offset,
791 uint32_t value, MemTxAttrs attrs)
793 GICState *s = (GICState *)opaque;
798 cpu = gic_get_current_cpu(s);
799 if (offset < 0x100) {
801 if (s->security_extn && !attrs.secure) {
802 /* NS version is just an alias of the S version's bit 1 */
803 s->ctlr = deposit32(s->ctlr, 1, 1, value);
804 } else if (gic_has_groups(s)) {
805 s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
807 s->ctlr = value & GICD_CTLR_EN_GRP0;
809 DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
810 s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
811 s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
812 } else if (offset < 4) {
814 } else if (offset >= 0x80) {
815 /* Interrupt Group Registers: RAZ/WI for NS access to secure
816 * GIC, or for GICs without groups.
818 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
819 /* Every byte offset holds 8 group status bits */
820 irq = (offset - 0x80) * 8 + GIC_BASE_IRQ;
821 if (irq >= s->num_irq) {
824 for (i = 0; i < 8; i++) {
825 /* Group bits are banked for private interrupts */
826 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
827 if (value & (1 << i)) {
828 /* Group1 (Non-secure) */
829 GIC_SET_GROUP(irq + i, cm);
831 /* Group0 (Secure) */
832 GIC_CLEAR_GROUP(irq + i, cm);
839 } else if (offset < 0x180) {
840 /* Interrupt Set Enable. */
841 irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
842 if (irq >= s->num_irq)
844 if (irq < GIC_NR_SGIS) {
848 for (i = 0; i < 8; i++) {
849 if (value & (1 << i)) {
851 (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq + i);
852 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
854 if (!GIC_TEST_ENABLED(irq + i, cm)) {
855 DPRINTF("Enabled IRQ %d\n", irq + i);
857 GIC_SET_ENABLED(irq + i, cm);
858 /* If a raised level triggered IRQ enabled then mark
860 if (GIC_TEST_LEVEL(irq + i, mask)
861 && !GIC_TEST_EDGE_TRIGGER(irq + i)) {
862 DPRINTF("Set %d pending mask %x\n", irq + i, mask);
863 GIC_SET_PENDING(irq + i, mask);
867 } else if (offset < 0x200) {
868 /* Interrupt Clear Enable. */
869 irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
870 if (irq >= s->num_irq)
872 if (irq < GIC_NR_SGIS) {
876 for (i = 0; i < 8; i++) {
877 if (value & (1 << i)) {
878 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
880 if (GIC_TEST_ENABLED(irq + i, cm)) {
881 DPRINTF("Disabled IRQ %d\n", irq + i);
883 GIC_CLEAR_ENABLED(irq + i, cm);
886 } else if (offset < 0x280) {
887 /* Interrupt Set Pending. */
888 irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
889 if (irq >= s->num_irq)
891 if (irq < GIC_NR_SGIS) {
895 for (i = 0; i < 8; i++) {
896 if (value & (1 << i)) {
897 GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
900 } else if (offset < 0x300) {
901 /* Interrupt Clear Pending. */
902 irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
903 if (irq >= s->num_irq)
905 if (irq < GIC_NR_SGIS) {
909 for (i = 0; i < 8; i++) {
910 /* ??? This currently clears the pending bit for all CPUs, even
911 for per-CPU interrupts. It's unclear whether this is the
913 if (value & (1 << i)) {
914 GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
917 } else if (offset < 0x400) {
918 /* Interrupt Active. */
920 } else if (offset < 0x800) {
921 /* Interrupt Priority. */
922 irq = (offset - 0x400) + GIC_BASE_IRQ;
923 if (irq >= s->num_irq)
925 gic_set_priority(s, cpu, irq, value, attrs);
926 } else if (offset < 0xc00) {
927 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
928 * annoying exception of the 11MPCore's GIC.
930 if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
931 irq = (offset - 0x800) + GIC_BASE_IRQ;
932 if (irq >= s->num_irq) {
937 } else if (irq < GIC_INTERNAL) {
938 value = ALL_CPU_MASK;
940 s->irq_target[irq] = value & ALL_CPU_MASK;
942 } else if (offset < 0xf00) {
943 /* Interrupt Configuration. */
944 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
945 if (irq >= s->num_irq)
947 if (irq < GIC_NR_SGIS)
949 for (i = 0; i < 4; i++) {
950 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
951 if (value & (1 << (i * 2))) {
952 GIC_SET_MODEL(irq + i);
954 GIC_CLEAR_MODEL(irq + i);
957 if (value & (2 << (i * 2))) {
958 GIC_SET_EDGE_TRIGGER(irq + i);
960 GIC_CLEAR_EDGE_TRIGGER(irq + i);
963 } else if (offset < 0xf10) {
964 /* 0xf00 is only handled for 32-bit writes. */
966 } else if (offset < 0xf20) {
967 /* GICD_CPENDSGIRn */
968 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
971 irq = (offset - 0xf10);
973 s->sgi_pending[irq][cpu] &= ~value;
974 if (s->sgi_pending[irq][cpu] == 0) {
975 GIC_CLEAR_PENDING(irq, 1 << cpu);
977 } else if (offset < 0xf30) {
978 /* GICD_SPENDSGIRn */
979 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
982 irq = (offset - 0xf20);
984 GIC_SET_PENDING(irq, 1 << cpu);
985 s->sgi_pending[irq][cpu] |= value;
992 qemu_log_mask(LOG_GUEST_ERROR,
993 "gic_dist_writeb: Bad offset %x\n", (int)offset);
996 static void gic_dist_writew(void *opaque, hwaddr offset,
997 uint32_t value, MemTxAttrs attrs)
999 gic_dist_writeb(opaque, offset, value & 0xff, attrs);
1000 gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
1003 static void gic_dist_writel(void *opaque, hwaddr offset,
1004 uint32_t value, MemTxAttrs attrs)
1006 GICState *s = (GICState *)opaque;
1007 if (offset == 0xf00) {
1013 cpu = gic_get_current_cpu(s);
1014 irq = value & 0x3ff;
1015 switch ((value >> 24) & 3) {
1017 mask = (value >> 16) & ALL_CPU_MASK;
1020 mask = ALL_CPU_MASK ^ (1 << cpu);
1026 DPRINTF("Bad Soft Int target filter\n");
1027 mask = ALL_CPU_MASK;
1030 GIC_SET_PENDING(irq, mask);
1031 target_cpu = ctz32(mask);
1032 while (target_cpu < GIC_NCPU) {
1033 s->sgi_pending[irq][target_cpu] |= (1 << cpu);
1034 mask &= ~(1 << target_cpu);
1035 target_cpu = ctz32(mask);
1040 gic_dist_writew(opaque, offset, value & 0xffff, attrs);
1041 gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
1044 static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
1045 unsigned size, MemTxAttrs attrs)
1049 gic_dist_writeb(opaque, offset, data, attrs);
1052 gic_dist_writew(opaque, offset, data, attrs);
1055 gic_dist_writel(opaque, offset, data, attrs);
1062 static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
1064 /* Return the Nonsecure view of GICC_APR<regno>. This is the
1065 * second half of GICC_NSAPR.
1067 switch (GIC_MIN_BPR) {
1070 return s->nsapr[regno + 2][cpu];
1075 return s->nsapr[regno + 1][cpu];
1080 return extract32(s->nsapr[0][cpu], 16, 16);
1085 return extract32(s->nsapr[0][cpu], 8, 8);
1089 g_assert_not_reached();
1094 static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
1097 /* Write the Nonsecure view of GICC_APR<regno>. */
1098 switch (GIC_MIN_BPR) {
1101 s->nsapr[regno + 2][cpu] = value;
1106 s->nsapr[regno + 1][cpu] = value;
1111 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
1116 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
1120 g_assert_not_reached();
1124 static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1125 uint64_t *data, MemTxAttrs attrs)
1128 case 0x00: /* Control */
1129 *data = gic_get_cpu_control(s, cpu, attrs);
1131 case 0x04: /* Priority mask */
1132 *data = gic_get_priority_mask(s, cpu, attrs);
1134 case 0x08: /* Binary Point */
1135 if (s->security_extn && !attrs.secure) {
1136 /* BPR is banked. Non-secure copy stored in ABPR. */
1137 *data = s->abpr[cpu];
1139 *data = s->bpr[cpu];
1142 case 0x0c: /* Acknowledge */
1143 *data = gic_acknowledge_irq(s, cpu, attrs);
1145 case 0x14: /* Running Priority */
1146 *data = gic_get_running_priority(s, cpu, attrs);
1148 case 0x18: /* Highest Pending Interrupt */
1149 *data = gic_get_current_pending_irq(s, cpu, attrs);
1151 case 0x1c: /* Aliased Binary Point */
1152 /* GIC v2, no security: ABPR
1153 * GIC v1, no security: not implemented (RAZ/WI)
1154 * With security extensions, secure access: ABPR (alias of NS BPR)
1155 * With security extensions, nonsecure access: RAZ/WI
1157 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
1160 *data = s->abpr[cpu];
1163 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1165 int regno = (offset - 0xd0) / 4;
1167 if (regno >= GIC_NR_APRS || s->revision != 2) {
1169 } else if (s->security_extn && !attrs.secure) {
1170 /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1171 *data = gic_apr_ns_view(s, regno, cpu);
1173 *data = s->apr[regno][cpu];
1177 case 0xe0: case 0xe4: case 0xe8: case 0xec:
1179 int regno = (offset - 0xe0) / 4;
1181 if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
1182 (s->security_extn && !attrs.secure)) {
1185 *data = s->nsapr[regno][cpu];
1190 qemu_log_mask(LOG_GUEST_ERROR,
1191 "gic_cpu_read: Bad offset %x\n", (int)offset);
1197 static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1198 uint32_t value, MemTxAttrs attrs)
1201 case 0x00: /* Control */
1202 gic_set_cpu_control(s, cpu, value, attrs);
1204 case 0x04: /* Priority mask */
1205 gic_set_priority_mask(s, cpu, value, attrs);
1207 case 0x08: /* Binary Point */
1208 if (s->security_extn && !attrs.secure) {
1209 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1211 s->bpr[cpu] = MAX(value & 0x7, GIC_MIN_BPR);
1214 case 0x10: /* End Of Interrupt */
1215 gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1217 case 0x1c: /* Aliased Binary Point */
1218 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
1219 /* unimplemented, or NS access: RAZ/WI */
1222 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1225 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1227 int regno = (offset - 0xd0) / 4;
1229 if (regno >= GIC_NR_APRS || s->revision != 2) {
1232 if (s->security_extn && !attrs.secure) {
1233 /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1234 gic_apr_write_ns_view(s, regno, cpu, value);
1236 s->apr[regno][cpu] = value;
1240 case 0xe0: case 0xe4: case 0xe8: case 0xec:
1242 int regno = (offset - 0xe0) / 4;
1244 if (regno >= GIC_NR_APRS || s->revision != 2) {
1247 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
1250 s->nsapr[regno][cpu] = value;
1255 gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
1258 qemu_log_mask(LOG_GUEST_ERROR,
1259 "gic_cpu_write: Bad offset %x\n", (int)offset);
1266 /* Wrappers to read/write the GIC CPU interface for the current CPU */
1267 static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1268 unsigned size, MemTxAttrs attrs)
1270 GICState *s = (GICState *)opaque;
1271 return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1274 static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1275 uint64_t value, unsigned size,
1278 GICState *s = (GICState *)opaque;
1279 return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1282 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
1283 * These just decode the opaque pointer into GICState* + cpu id.
1285 static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1286 unsigned size, MemTxAttrs attrs)
1288 GICState **backref = (GICState **)opaque;
1289 GICState *s = *backref;
1290 int id = (backref - s->backref);
1291 return gic_cpu_read(s, id, addr, data, attrs);
1294 static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1295 uint64_t value, unsigned size,
1298 GICState **backref = (GICState **)opaque;
1299 GICState *s = *backref;
1300 int id = (backref - s->backref);
1301 return gic_cpu_write(s, id, addr, value, attrs);
1304 static const MemoryRegionOps gic_ops[2] = {
1306 .read_with_attrs = gic_dist_read,
1307 .write_with_attrs = gic_dist_write,
1308 .endianness = DEVICE_NATIVE_ENDIAN,
1311 .read_with_attrs = gic_thiscpu_read,
1312 .write_with_attrs = gic_thiscpu_write,
1313 .endianness = DEVICE_NATIVE_ENDIAN,
1317 static const MemoryRegionOps gic_cpu_ops = {
1318 .read_with_attrs = gic_do_cpu_read,
1319 .write_with_attrs = gic_do_cpu_write,
1320 .endianness = DEVICE_NATIVE_ENDIAN,
1323 /* This function is used by nvic model */
1324 void gic_init_irqs_and_distributor(GICState *s)
1326 gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops);
1329 static void arm_gic_realize(DeviceState *dev, Error **errp)
1331 /* Device instance realize function for the GIC sysbus device */
1333 GICState *s = ARM_GIC(dev);
1334 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1335 ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
1336 Error *local_err = NULL;
1338 agc->parent_realize(dev, &local_err);
1340 error_propagate(errp, local_err);
1344 /* This creates distributor and main CPU interface (s->cpuiomem[0]) */
1345 gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops);
1347 /* Extra core-specific regions for the CPU interfaces. This is
1348 * necessary for "franken-GIC" implementations, for example on
1350 * NB that the memory region size of 0x100 applies for the 11MPCore
1351 * and also cores following the GIC v1 spec (ie A9).
1352 * GIC v2 defines a larger memory region (0x1000) so this will need
1353 * to be extended when we implement A15.
1355 for (i = 0; i < s->num_cpu; i++) {
1357 memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
1358 &s->backref[i], "gic_cpu", 0x100);
1359 sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
1363 static void arm_gic_class_init(ObjectClass *klass, void *data)
1365 DeviceClass *dc = DEVICE_CLASS(klass);
1366 ARMGICClass *agc = ARM_GIC_CLASS(klass);
1368 agc->parent_realize = dc->realize;
1369 dc->realize = arm_gic_realize;
1372 static const TypeInfo arm_gic_info = {
1373 .name = TYPE_ARM_GIC,
1374 .parent = TYPE_ARM_GIC_COMMON,
1375 .instance_size = sizeof(GICState),
1376 .class_init = arm_gic_class_init,
1377 .class_size = sizeof(ARMGICClass),
1380 static void arm_gic_register_types(void)
1382 type_register_static(&arm_gic_info);
1385 type_init(arm_gic_register_types)