xref: /qemu/hw/intc/arm_gic.c (revision 4663b72a48fd540cbe16053b01d6839a95656440)
1e69954b9Spbrook /*
29ee6e8bbSpbrook  * ARM Generic/Distributed Interrupt Controller
3e69954b9Spbrook  *
49ee6e8bbSpbrook  * Copyright (c) 2006-2007 CodeSourcery.
5e69954b9Spbrook  * Written by Paul Brook
6e69954b9Spbrook  *
78e31bf38SMatthew Fernandez  * This code is licensed under the GPL.
8e69954b9Spbrook  */
9e69954b9Spbrook 
109ee6e8bbSpbrook /* This file contains implementation code for the RealView EB interrupt
110d256bdcSPeter Maydell  * controller, MPCore distributed interrupt controller and ARMv7-M
120d256bdcSPeter Maydell  * Nested Vectored Interrupt Controller.
130d256bdcSPeter Maydell  * It is compiled in two ways:
140d256bdcSPeter Maydell  *  (1) as a standalone file to produce a sysbus device which is a GIC
150d256bdcSPeter Maydell  *  that can be used on the realview board and as one of the builtin
160d256bdcSPeter Maydell  *  private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
170d256bdcSPeter Maydell  *  (2) by being directly #included into armv7m_nvic.c to produce the
180d256bdcSPeter Maydell  *  armv7m_nvic device.
190d256bdcSPeter Maydell  */
20e69954b9Spbrook 
218ef94f0bSPeter Maydell #include "qemu/osdep.h"
2264552b6bSMarkus Armbruster #include "hw/irq.h"
2383c9f4caSPaolo Bonzini #include "hw/sysbus.h"
2447b43a1fSPaolo Bonzini #include "gic_internal.h"
25da34e65cSMarkus Armbruster #include "qapi/error.h"
262e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
2703dd024fSPaolo Bonzini #include "qemu/log.h"
280b8fa32fSMarkus Armbruster #include "qemu/module.h"
292531088fSHollis Blanchard #include "trace.h"
305d721b78SAlexander Graf #include "sysemu/kvm.h"
31386e2955SPeter Maydell 
3268bf93ceSAlex Bennée /* #define DEBUG_GIC */
33e69954b9Spbrook 
34e69954b9Spbrook #ifdef DEBUG_GIC
3568bf93ceSAlex Bennée #define DEBUG_GIC_GATE 1
36e69954b9Spbrook #else
3768bf93ceSAlex Bennée #define DEBUG_GIC_GATE 0
38e69954b9Spbrook #endif
39e69954b9Spbrook 
4068bf93ceSAlex Bennée #define DPRINTF(fmt, ...) do {                                          \
4168bf93ceSAlex Bennée         if (DEBUG_GIC_GATE) {                                           \
4268bf93ceSAlex Bennée             fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__);      \
4368bf93ceSAlex Bennée         }                                                               \
4468bf93ceSAlex Bennée     } while (0)
4568bf93ceSAlex Bennée 
463355c360SAlistair Francis static const uint8_t gic_id_11mpcore[] = {
473355c360SAlistair Francis     0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
483355c360SAlistair Francis };
493355c360SAlistair Francis 
503355c360SAlistair Francis static const uint8_t gic_id_gicv1[] = {
513355c360SAlistair Francis     0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
523355c360SAlistair Francis };
533355c360SAlistair Francis 
543355c360SAlistair Francis static const uint8_t gic_id_gicv2[] = {
553355c360SAlistair Francis     0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
562a29ddeeSPeter Maydell };
572a29ddeeSPeter Maydell 
58fae15286SPeter Maydell static inline int gic_get_current_cpu(GICState *s)
59926c4affSPeter Maydell {
60926c4affSPeter Maydell     if (s->num_cpu > 1) {
614917cf44SAndreas Färber         return current_cpu->cpu_index;
62926c4affSPeter Maydell     }
63926c4affSPeter Maydell     return 0;
64926c4affSPeter Maydell }
65926c4affSPeter Maydell 
664a37e0e4SLuc Michel static inline int gic_get_current_vcpu(GICState *s)
674a37e0e4SLuc Michel {
684a37e0e4SLuc Michel     return gic_get_current_cpu(s) + GIC_NCPU;
694a37e0e4SLuc Michel }
704a37e0e4SLuc Michel 
71c27a5ba9SFabian Aggeler /* Return true if this GIC config has interrupt groups, which is
72c27a5ba9SFabian Aggeler  * true if we're a GICv2, or a GICv1 with the security extensions.
73c27a5ba9SFabian Aggeler  */
74c27a5ba9SFabian Aggeler static inline bool gic_has_groups(GICState *s)
75c27a5ba9SFabian Aggeler {
76c27a5ba9SFabian Aggeler     return s->revision == 2 || s->security_extn;
77c27a5ba9SFabian Aggeler }
78c27a5ba9SFabian Aggeler 
793dd0471bSLuc Michel static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs)
803dd0471bSLuc Michel {
813dd0471bSLuc Michel     return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure;
823dd0471bSLuc Michel }
833dd0471bSLuc Michel 
84cbe1282bSLuc Michel static inline void gic_get_best_irq(GICState *s, int cpu,
85cbe1282bSLuc Michel                                     int *best_irq, int *best_prio, int *group)
86cbe1282bSLuc Michel {
87cbe1282bSLuc Michel     int irq;
88cbe1282bSLuc Michel     int cm = 1 << cpu;
89cbe1282bSLuc Michel 
90cbe1282bSLuc Michel     *best_irq = 1023;
91cbe1282bSLuc Michel     *best_prio = 0x100;
92cbe1282bSLuc Michel 
93cbe1282bSLuc Michel     for (irq = 0; irq < s->num_irq; irq++) {
94cbe1282bSLuc Michel         if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
95cbe1282bSLuc Michel             (!GIC_DIST_TEST_ACTIVE(irq, cm)) &&
96cbe1282bSLuc Michel             (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) {
97cbe1282bSLuc Michel             if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) {
98cbe1282bSLuc Michel                 *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu);
99cbe1282bSLuc Michel                 *best_irq = irq;
100cbe1282bSLuc Michel             }
101cbe1282bSLuc Michel         }
102cbe1282bSLuc Michel     }
103cbe1282bSLuc Michel 
104cbe1282bSLuc Michel     if (*best_irq < 1023) {
105cbe1282bSLuc Michel         *group = GIC_DIST_TEST_GROUP(*best_irq, cm);
106cbe1282bSLuc Michel     }
107cbe1282bSLuc Michel }
108cbe1282bSLuc Michel 
109cbe1282bSLuc Michel static inline void gic_get_best_virq(GICState *s, int cpu,
110cbe1282bSLuc Michel                                      int *best_irq, int *best_prio, int *group)
111cbe1282bSLuc Michel {
112cbe1282bSLuc Michel     int lr_idx = 0;
113cbe1282bSLuc Michel 
114cbe1282bSLuc Michel     *best_irq = 1023;
115cbe1282bSLuc Michel     *best_prio = 0x100;
116cbe1282bSLuc Michel 
117cbe1282bSLuc Michel     for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
118cbe1282bSLuc Michel         uint32_t lr_entry = s->h_lr[lr_idx][cpu];
119cbe1282bSLuc Michel         int state = GICH_LR_STATE(lr_entry);
120cbe1282bSLuc Michel 
121cbe1282bSLuc Michel         if (state == GICH_LR_STATE_PENDING) {
122cbe1282bSLuc Michel             int prio = GICH_LR_PRIORITY(lr_entry);
123cbe1282bSLuc Michel 
124cbe1282bSLuc Michel             if (prio < *best_prio) {
125cbe1282bSLuc Michel                 *best_prio = prio;
126cbe1282bSLuc Michel                 *best_irq = GICH_LR_VIRT_ID(lr_entry);
127cbe1282bSLuc Michel                 *group = GICH_LR_GROUP(lr_entry);
128cbe1282bSLuc Michel             }
129cbe1282bSLuc Michel         }
130cbe1282bSLuc Michel     }
131cbe1282bSLuc Michel }
132cbe1282bSLuc Michel 
133cbe1282bSLuc Michel /* Return true if IRQ signaling is enabled for the given cpu and at least one
134cbe1282bSLuc Michel  * of the given groups:
135cbe1282bSLuc Michel  *   - in the non-virt case, the distributor must be enabled for one of the
136cbe1282bSLuc Michel  *   given groups
137cbe1282bSLuc Michel  *   - in the virt case, the virtual interface must be enabled.
138cbe1282bSLuc Michel  *   - in all cases, the (v)CPU interface must be enabled for one of the given
139cbe1282bSLuc Michel  *   groups.
140cbe1282bSLuc Michel  */
141cbe1282bSLuc Michel static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt,
142cbe1282bSLuc Michel                                     int group_mask)
143cbe1282bSLuc Michel {
144*4663b72aSEdgar E. Iglesias     int cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
145*4663b72aSEdgar E. Iglesias 
146cbe1282bSLuc Michel     if (!virt && !(s->ctlr & group_mask)) {
147cbe1282bSLuc Michel         return false;
148cbe1282bSLuc Michel     }
149cbe1282bSLuc Michel 
150cbe1282bSLuc Michel     if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) {
151cbe1282bSLuc Michel         return false;
152cbe1282bSLuc Michel     }
153cbe1282bSLuc Michel 
154*4663b72aSEdgar E. Iglesias     if (!(s->cpu_ctlr[cpu_iface] & group_mask)) {
155cbe1282bSLuc Michel         return false;
156cbe1282bSLuc Michel     }
157cbe1282bSLuc Michel 
158cbe1282bSLuc Michel     return true;
159cbe1282bSLuc Michel }
160cbe1282bSLuc Michel 
161e69954b9Spbrook /* TODO: Many places that call this routine could be optimized.  */
162e69954b9Spbrook /* Update interrupt status after enabled or pending bits have been changed.  */
163cbe1282bSLuc Michel static inline void gic_update_internal(GICState *s, bool virt)
164e69954b9Spbrook {
165e69954b9Spbrook     int best_irq;
166e69954b9Spbrook     int best_prio;
167dadbb58fSPeter Maydell     int irq_level, fiq_level;
168cbe1282bSLuc Michel     int cpu, cpu_iface;
169cbe1282bSLuc Michel     int group = 0;
170cbe1282bSLuc Michel     qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq;
171cbe1282bSLuc Michel     qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq;
172e69954b9Spbrook 
173b95690c9SWei Huang     for (cpu = 0; cpu < s->num_cpu; cpu++) {
174cbe1282bSLuc Michel         cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
175cbe1282bSLuc Michel 
176cbe1282bSLuc Michel         s->current_pending[cpu_iface] = 1023;
177cbe1282bSLuc Michel         if (!gic_irq_signaling_enabled(s, cpu, virt,
178cbe1282bSLuc Michel                                        GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) {
179cbe1282bSLuc Michel             qemu_irq_lower(irq_lines[cpu]);
180cbe1282bSLuc Michel             qemu_irq_lower(fiq_lines[cpu]);
181235069a3SJohan Karlsson             continue;
182e69954b9Spbrook         }
183cbe1282bSLuc Michel 
184cbe1282bSLuc Michel         if (virt) {
185cbe1282bSLuc Michel             gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group);
186cbe1282bSLuc Michel         } else {
187cbe1282bSLuc Michel             gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group);
188e69954b9Spbrook         }
189dadbb58fSPeter Maydell 
1902531088fSHollis Blanchard         if (best_irq != 1023) {
191067a2b9cSLuc Michel             trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu,
192067a2b9cSLuc Michel                                      best_irq, best_prio,
193067a2b9cSLuc Michel                                      s->priority_mask[cpu_iface],
194067a2b9cSLuc Michel                                      s->running_priority[cpu_iface]);
1952531088fSHollis Blanchard         }
1962531088fSHollis Blanchard 
197dadbb58fSPeter Maydell         irq_level = fiq_level = 0;
198dadbb58fSPeter Maydell 
199cbe1282bSLuc Michel         if (best_prio < s->priority_mask[cpu_iface]) {
200cbe1282bSLuc Michel             s->current_pending[cpu_iface] = best_irq;
201cbe1282bSLuc Michel             if (best_prio < s->running_priority[cpu_iface]) {
202cbe1282bSLuc Michel                 if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) {
203cbe1282bSLuc Michel                     if (group == 0 &&
204cbe1282bSLuc Michel                         s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) {
205dadbb58fSPeter Maydell                         DPRINTF("Raised pending FIQ %d (cpu %d)\n",
206cbe1282bSLuc Michel                                 best_irq, cpu_iface);
207dadbb58fSPeter Maydell                         fiq_level = 1;
208cbe1282bSLuc Michel                         trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq",
209cbe1282bSLuc Michel                                                  fiq_level);
210dadbb58fSPeter Maydell                     } else {
211dadbb58fSPeter Maydell                         DPRINTF("Raised pending IRQ %d (cpu %d)\n",
212cbe1282bSLuc Michel                                 best_irq, cpu_iface);
213dadbb58fSPeter Maydell                         irq_level = 1;
214cbe1282bSLuc Michel                         trace_gic_update_set_irq(cpu, virt ? "virq" : "irq",
215cbe1282bSLuc Michel                                                  irq_level);
216e69954b9Spbrook                     }
217e69954b9Spbrook                 }
218dadbb58fSPeter Maydell             }
219dadbb58fSPeter Maydell         }
220dadbb58fSPeter Maydell 
221cbe1282bSLuc Michel         qemu_set_irq(irq_lines[cpu], irq_level);
222cbe1282bSLuc Michel         qemu_set_irq(fiq_lines[cpu], fiq_level);
2239ee6e8bbSpbrook     }
224e69954b9Spbrook }
225e69954b9Spbrook 
226cbe1282bSLuc Michel static void gic_update(GICState *s)
227cbe1282bSLuc Michel {
228cbe1282bSLuc Michel     gic_update_internal(s, false);
229cbe1282bSLuc Michel }
230cbe1282bSLuc Michel 
231527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit
232527d296fSLuc Michel  * in ELRSR is set.
233527d296fSLuc Michel  */
234527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry)
235527d296fSLuc Michel {
236527d296fSLuc Michel     return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
237527d296fSLuc Michel         && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry));
238527d296fSLuc Michel }
239527d296fSLuc Michel 
240527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the
241527d296fSLuc Michel  * corrsponding bit in EISR is set.
242527d296fSLuc Michel  */
243527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry)
244527d296fSLuc Michel {
245527d296fSLuc Michel     return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
246527d296fSLuc Michel         && !GICH_LR_HW(entry) && GICH_LR_EOI(entry);
247527d296fSLuc Michel }
248527d296fSLuc Michel 
24950e57926SLuc Michel static inline void gic_extract_lr_info(GICState *s, int cpu,
25050e57926SLuc Michel                                 int *num_eoi, int *num_valid, int *num_pending)
25150e57926SLuc Michel {
25250e57926SLuc Michel     int lr_idx;
25350e57926SLuc Michel 
25450e57926SLuc Michel     *num_eoi = 0;
25550e57926SLuc Michel     *num_valid = 0;
25650e57926SLuc Michel     *num_pending = 0;
25750e57926SLuc Michel 
25850e57926SLuc Michel     for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
25950e57926SLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
26050e57926SLuc Michel 
26150e57926SLuc Michel         if (gic_lr_entry_is_eoi(*entry)) {
26250e57926SLuc Michel             (*num_eoi)++;
26350e57926SLuc Michel         }
26450e57926SLuc Michel 
26550e57926SLuc Michel         if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) {
26650e57926SLuc Michel             (*num_valid)++;
26750e57926SLuc Michel         }
26850e57926SLuc Michel 
26950e57926SLuc Michel         if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) {
27050e57926SLuc Michel             (*num_pending)++;
27150e57926SLuc Michel         }
27250e57926SLuc Michel     }
27350e57926SLuc Michel }
27450e57926SLuc Michel 
27550e57926SLuc Michel static void gic_compute_misr(GICState *s, int cpu)
27650e57926SLuc Michel {
27750e57926SLuc Michel     uint32_t value = 0;
27850e57926SLuc Michel     int vcpu = cpu + GIC_NCPU;
27950e57926SLuc Michel 
28050e57926SLuc Michel     int num_eoi, num_valid, num_pending;
28150e57926SLuc Michel 
28250e57926SLuc Michel     gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending);
28350e57926SLuc Michel 
28450e57926SLuc Michel     /* EOI */
28550e57926SLuc Michel     if (num_eoi) {
28650e57926SLuc Michel         value |= R_GICH_MISR_EOI_MASK;
28750e57926SLuc Michel     }
28850e57926SLuc Michel 
28950e57926SLuc Michel     /* U: true if only 0 or 1 LR entry is valid */
29050e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) {
29150e57926SLuc Michel         value |= R_GICH_MISR_U_MASK;
29250e57926SLuc Michel     }
29350e57926SLuc Michel 
29450e57926SLuc Michel     /* LRENP: EOICount is not 0 */
29550e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) &&
29650e57926SLuc Michel         ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) {
29750e57926SLuc Michel         value |= R_GICH_MISR_LRENP_MASK;
29850e57926SLuc Michel     }
29950e57926SLuc Michel 
30050e57926SLuc Michel     /* NP: no pending interrupts */
30150e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) {
30250e57926SLuc Michel         value |= R_GICH_MISR_NP_MASK;
30350e57926SLuc Michel     }
30450e57926SLuc Michel 
30550e57926SLuc Michel     /* VGrp0E: group0 virq signaling enabled */
30650e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) &&
30750e57926SLuc Michel         (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
30850e57926SLuc Michel         value |= R_GICH_MISR_VGrp0E_MASK;
30950e57926SLuc Michel     }
31050e57926SLuc Michel 
31150e57926SLuc Michel     /* VGrp0D: group0 virq signaling disabled */
31250e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) &&
31350e57926SLuc Michel         !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
31450e57926SLuc Michel         value |= R_GICH_MISR_VGrp0D_MASK;
31550e57926SLuc Michel     }
31650e57926SLuc Michel 
31750e57926SLuc Michel     /* VGrp1E: group1 virq signaling enabled */
31850e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) &&
31950e57926SLuc Michel         (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
32050e57926SLuc Michel         value |= R_GICH_MISR_VGrp1E_MASK;
32150e57926SLuc Michel     }
32250e57926SLuc Michel 
32350e57926SLuc Michel     /* VGrp1D: group1 virq signaling disabled */
32450e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) &&
32550e57926SLuc Michel         !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
32650e57926SLuc Michel         value |= R_GICH_MISR_VGrp1D_MASK;
32750e57926SLuc Michel     }
32850e57926SLuc Michel 
32950e57926SLuc Michel     s->h_misr[cpu] = value;
33050e57926SLuc Michel }
33150e57926SLuc Michel 
33250e57926SLuc Michel static void gic_update_maintenance(GICState *s)
33350e57926SLuc Michel {
33450e57926SLuc Michel     int cpu = 0;
33550e57926SLuc Michel     int maint_level;
33650e57926SLuc Michel 
33750e57926SLuc Michel     for (cpu = 0; cpu < s->num_cpu; cpu++) {
33850e57926SLuc Michel         gic_compute_misr(s, cpu);
33950e57926SLuc Michel         maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu];
34050e57926SLuc Michel 
341067a2b9cSLuc Michel         trace_gic_update_maintenance_irq(cpu, maint_level);
34250e57926SLuc Michel         qemu_set_irq(s->maintenance_irq[cpu], maint_level);
34350e57926SLuc Michel     }
34450e57926SLuc Michel }
34550e57926SLuc Michel 
346cbe1282bSLuc Michel static void gic_update_virt(GICState *s)
347cbe1282bSLuc Michel {
348cbe1282bSLuc Michel     gic_update_internal(s, true);
34950e57926SLuc Michel     gic_update_maintenance(s);
350cbe1282bSLuc Michel }
351cbe1282bSLuc Michel 
3528d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
3538d999995SChristoffer Dall                                  int cm, int target)
3548d999995SChristoffer Dall {
3558d999995SChristoffer Dall     if (level) {
35667ce697aSLuc Michel         GIC_DIST_SET_LEVEL(irq, cm);
35767ce697aSLuc Michel         if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) {
3588d999995SChristoffer Dall             DPRINTF("Set %d pending mask %x\n", irq, target);
35967ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, target);
3608d999995SChristoffer Dall         }
3618d999995SChristoffer Dall     } else {
36267ce697aSLuc Michel         GIC_DIST_CLEAR_LEVEL(irq, cm);
3638d999995SChristoffer Dall     }
3648d999995SChristoffer Dall }
3658d999995SChristoffer Dall 
3668d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level,
3678d999995SChristoffer Dall                                 int cm, int target)
3688d999995SChristoffer Dall {
3698d999995SChristoffer Dall     if (level) {
37067ce697aSLuc Michel         GIC_DIST_SET_LEVEL(irq, cm);
3718d999995SChristoffer Dall         DPRINTF("Set %d pending mask %x\n", irq, target);
37267ce697aSLuc Michel         if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) {
37367ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, target);
3748d999995SChristoffer Dall         }
3758d999995SChristoffer Dall     } else {
37667ce697aSLuc Michel         GIC_DIST_CLEAR_LEVEL(irq, cm);
3778d999995SChristoffer Dall     }
3788d999995SChristoffer Dall }
3798d999995SChristoffer Dall 
3809ee6e8bbSpbrook /* Process a change in an external IRQ input.  */
381e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level)
382e69954b9Spbrook {
383544d1afaSPeter Maydell     /* Meaning of the 'irq' parameter:
384544d1afaSPeter Maydell      *  [0..N-1] : external interrupts
385544d1afaSPeter Maydell      *  [N..N+31] : PPI (internal) interrupts for CPU 0
386544d1afaSPeter Maydell      *  [N+32..N+63] : PPI (internal interrupts for CPU 1
387544d1afaSPeter Maydell      *  ...
388544d1afaSPeter Maydell      */
389fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
390544d1afaSPeter Maydell     int cm, target;
391544d1afaSPeter Maydell     if (irq < (s->num_irq - GIC_INTERNAL)) {
392e69954b9Spbrook         /* The first external input line is internal interrupt 32.  */
393544d1afaSPeter Maydell         cm = ALL_CPU_MASK;
39469253800SRusty Russell         irq += GIC_INTERNAL;
39567ce697aSLuc Michel         target = GIC_DIST_TARGET(irq);
396544d1afaSPeter Maydell     } else {
397544d1afaSPeter Maydell         int cpu;
398544d1afaSPeter Maydell         irq -= (s->num_irq - GIC_INTERNAL);
399544d1afaSPeter Maydell         cpu = irq / GIC_INTERNAL;
400544d1afaSPeter Maydell         irq %= GIC_INTERNAL;
401544d1afaSPeter Maydell         cm = 1 << cpu;
402544d1afaSPeter Maydell         target = cm;
403544d1afaSPeter Maydell     }
404544d1afaSPeter Maydell 
40540d22500SChristoffer Dall     assert(irq >= GIC_NR_SGIS);
40640d22500SChristoffer Dall 
40767ce697aSLuc Michel     if (level == GIC_DIST_TEST_LEVEL(irq, cm)) {
408e69954b9Spbrook         return;
409544d1afaSPeter Maydell     }
410e69954b9Spbrook 
4113bc4b52cSMarcin Krzeminski     if (s->revision == REV_11MPCORE) {
4128d999995SChristoffer Dall         gic_set_irq_11mpcore(s, irq, level, cm, target);
413e69954b9Spbrook     } else {
4148d999995SChristoffer Dall         gic_set_irq_generic(s, irq, level, cm, target);
415e69954b9Spbrook     }
4162531088fSHollis Blanchard     trace_gic_set_irq(irq, level, cm, target);
4178d999995SChristoffer Dall 
418e69954b9Spbrook     gic_update(s);
419e69954b9Spbrook }
420e69954b9Spbrook 
4217c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
4227c0fa108SFabian Aggeler                                             MemTxAttrs attrs)
4237c0fa108SFabian Aggeler {
4247c0fa108SFabian Aggeler     uint16_t pending_irq = s->current_pending[cpu];
4257c0fa108SFabian Aggeler 
4267c0fa108SFabian Aggeler     if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
42786b350f0SLuc Michel         int group = gic_test_group(s, pending_irq, cpu);
42886b350f0SLuc Michel 
4297c0fa108SFabian Aggeler         /* On a GIC without the security extensions, reading this register
4307c0fa108SFabian Aggeler          * behaves in the same way as a secure access to a GIC with them.
4317c0fa108SFabian Aggeler          */
4323dd0471bSLuc Michel         bool secure = !gic_cpu_ns_access(s, cpu, attrs);
4337c0fa108SFabian Aggeler 
4347c0fa108SFabian Aggeler         if (group == 0 && !secure) {
4357c0fa108SFabian Aggeler             /* Group0 interrupts hidden from Non-secure access */
4367c0fa108SFabian Aggeler             return 1023;
4377c0fa108SFabian Aggeler         }
4387c0fa108SFabian Aggeler         if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
4397c0fa108SFabian Aggeler             /* Group1 interrupts only seen by Secure access if
4407c0fa108SFabian Aggeler              * AckCtl bit set.
4417c0fa108SFabian Aggeler              */
4427c0fa108SFabian Aggeler             return 1022;
4437c0fa108SFabian Aggeler         }
4447c0fa108SFabian Aggeler     }
4457c0fa108SFabian Aggeler     return pending_irq;
4467c0fa108SFabian Aggeler }
4477c0fa108SFabian Aggeler 
448df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq)
449df92cfa6SPeter Maydell {
450df92cfa6SPeter Maydell     /* Return the group priority of the specified interrupt
451df92cfa6SPeter Maydell      * (which is the top bits of its priority, with the number
452df92cfa6SPeter Maydell      * of bits masked determined by the applicable binary point register).
453df92cfa6SPeter Maydell      */
454df92cfa6SPeter Maydell     int bpr;
455df92cfa6SPeter Maydell     uint32_t mask;
456df92cfa6SPeter Maydell 
457df92cfa6SPeter Maydell     if (gic_has_groups(s) &&
458df92cfa6SPeter Maydell         !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
45986b350f0SLuc Michel         gic_test_group(s, irq, cpu)) {
460fc05a6f2SLuc MICHEL         bpr = s->abpr[cpu] - 1;
461fc05a6f2SLuc MICHEL         assert(bpr >= 0);
462df92cfa6SPeter Maydell     } else {
463df92cfa6SPeter Maydell         bpr = s->bpr[cpu];
464df92cfa6SPeter Maydell     }
465df92cfa6SPeter Maydell 
466df92cfa6SPeter Maydell     /* a BPR of 0 means the group priority bits are [7:1];
467df92cfa6SPeter Maydell      * a BPR of 1 means they are [7:2], and so on down to
468df92cfa6SPeter Maydell      * a BPR of 7 meaning no group priority bits at all.
469df92cfa6SPeter Maydell      */
470df92cfa6SPeter Maydell     mask = ~0U << ((bpr & 7) + 1);
471df92cfa6SPeter Maydell 
47286b350f0SLuc Michel     return gic_get_priority(s, irq, cpu) & mask;
473df92cfa6SPeter Maydell }
474df92cfa6SPeter Maydell 
47572889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq)
476e69954b9Spbrook {
47772889c8aSPeter Maydell     /* Set the appropriate Active Priority Register bit for this IRQ,
47872889c8aSPeter Maydell      * and update the running priority.
47972889c8aSPeter Maydell      */
48072889c8aSPeter Maydell     int prio = gic_get_group_priority(s, cpu, irq);
481a1d7b8d8SLuc Michel     int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
482a1d7b8d8SLuc Michel     int preemption_level = prio >> (min_bpr + 1);
48372889c8aSPeter Maydell     int regno = preemption_level / 32;
48472889c8aSPeter Maydell     int bitno = preemption_level % 32;
485a1d7b8d8SLuc Michel     uint32_t *papr = NULL;
48672889c8aSPeter Maydell 
487a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
488a1d7b8d8SLuc Michel         assert(regno == 0);
489a1d7b8d8SLuc Michel         papr = &s->h_apr[gic_get_vcpu_real_id(cpu)];
490a1d7b8d8SLuc Michel     } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) {
491a1d7b8d8SLuc Michel         papr = &s->nsapr[regno][cpu];
4929ee6e8bbSpbrook     } else {
493a1d7b8d8SLuc Michel         papr = &s->apr[regno][cpu];
4949ee6e8bbSpbrook     }
49572889c8aSPeter Maydell 
496a1d7b8d8SLuc Michel     *papr |= (1 << bitno);
497a1d7b8d8SLuc Michel 
49872889c8aSPeter Maydell     s->running_priority[cpu] = prio;
49986b350f0SLuc Michel     gic_set_active(s, irq, cpu);
50072889c8aSPeter Maydell }
50172889c8aSPeter Maydell 
50272889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
50372889c8aSPeter Maydell {
50472889c8aSPeter Maydell     /* Recalculate the current running priority for this CPU based
50572889c8aSPeter Maydell      * on the set bits in the Active Priority Registers.
50672889c8aSPeter Maydell      */
50772889c8aSPeter Maydell     int i;
508a1d7b8d8SLuc Michel 
509a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
510a1d7b8d8SLuc Michel         uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)];
511a1d7b8d8SLuc Michel         if (apr) {
512a1d7b8d8SLuc Michel             return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1);
513a1d7b8d8SLuc Michel         } else {
514a1d7b8d8SLuc Michel             return 0x100;
515a1d7b8d8SLuc Michel         }
516a1d7b8d8SLuc Michel     }
517a1d7b8d8SLuc Michel 
51872889c8aSPeter Maydell     for (i = 0; i < GIC_NR_APRS; i++) {
51972889c8aSPeter Maydell         uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
52072889c8aSPeter Maydell         if (!apr) {
52172889c8aSPeter Maydell             continue;
52272889c8aSPeter Maydell         }
52372889c8aSPeter Maydell         return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
52472889c8aSPeter Maydell     }
52572889c8aSPeter Maydell     return 0x100;
52672889c8aSPeter Maydell }
52772889c8aSPeter Maydell 
52872889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group)
52972889c8aSPeter Maydell {
53072889c8aSPeter Maydell     /* Drop the priority of the currently active interrupt in the
53172889c8aSPeter Maydell      * specified group.
53272889c8aSPeter Maydell      *
53372889c8aSPeter Maydell      * Note that we can guarantee (because of the requirement to nest
53472889c8aSPeter Maydell      * GICC_IAR reads [which activate an interrupt and raise priority]
53572889c8aSPeter Maydell      * with GICC_EOIR writes [which drop the priority for the interrupt])
53672889c8aSPeter Maydell      * that the interrupt we're being called for is the highest priority
53772889c8aSPeter Maydell      * active interrupt, meaning that it has the lowest set bit in the
53872889c8aSPeter Maydell      * APR registers.
53972889c8aSPeter Maydell      *
54072889c8aSPeter Maydell      * If the guest does not honour the ordering constraints then the
54172889c8aSPeter Maydell      * behaviour of the GIC is UNPREDICTABLE, which for us means that
54272889c8aSPeter Maydell      * the values of the APR registers might become incorrect and the
54372889c8aSPeter Maydell      * running priority will be wrong, so interrupts that should preempt
54472889c8aSPeter Maydell      * might not do so, and interrupts that should not preempt might do so.
54572889c8aSPeter Maydell      */
546a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
547a1d7b8d8SLuc Michel         int rcpu = gic_get_vcpu_real_id(cpu);
548a1d7b8d8SLuc Michel 
549a1d7b8d8SLuc Michel         if (s->h_apr[rcpu]) {
550a1d7b8d8SLuc Michel             /* Clear lowest set bit */
551a1d7b8d8SLuc Michel             s->h_apr[rcpu] &= s->h_apr[rcpu] - 1;
552a1d7b8d8SLuc Michel         }
553a1d7b8d8SLuc Michel     } else {
55472889c8aSPeter Maydell         int i;
55572889c8aSPeter Maydell 
55672889c8aSPeter Maydell         for (i = 0; i < GIC_NR_APRS; i++) {
55772889c8aSPeter Maydell             uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
55872889c8aSPeter Maydell             if (!*papr) {
55972889c8aSPeter Maydell                 continue;
56072889c8aSPeter Maydell             }
56172889c8aSPeter Maydell             /* Clear lowest set bit */
56272889c8aSPeter Maydell             *papr &= *papr - 1;
56372889c8aSPeter Maydell             break;
56472889c8aSPeter Maydell         }
565a1d7b8d8SLuc Michel     }
56672889c8aSPeter Maydell 
56772889c8aSPeter Maydell     s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
568e69954b9Spbrook }
569e69954b9Spbrook 
570439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu)
571e69954b9Spbrook {
572439badd6SLuc Michel     int src;
573439badd6SLuc Michel     uint32_t ret;
574c5619bf9SFabian Aggeler 
575439badd6SLuc Michel     if (!gic_is_vcpu(cpu)) {
57640d22500SChristoffer Dall         /* Lookup the source CPU for the SGI and clear this in the
57740d22500SChristoffer Dall          * sgi_pending map.  Return the src and clear the overall pending
57840d22500SChristoffer Dall          * state on this CPU if the SGI is not pending from any CPUs.
57940d22500SChristoffer Dall          */
58040d22500SChristoffer Dall         assert(s->sgi_pending[irq][cpu] != 0);
58140d22500SChristoffer Dall         src = ctz32(s->sgi_pending[irq][cpu]);
58240d22500SChristoffer Dall         s->sgi_pending[irq][cpu] &= ~(1 << src);
58340d22500SChristoffer Dall         if (s->sgi_pending[irq][cpu] == 0) {
58486b350f0SLuc Michel             gic_clear_pending(s, irq, cpu);
58540d22500SChristoffer Dall         }
58640d22500SChristoffer Dall         ret = irq | ((src & 0x7) << 10);
58740d22500SChristoffer Dall     } else {
588439badd6SLuc Michel         uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu);
589439badd6SLuc Michel         src = GICH_LR_CPUID(*lr_entry);
590439badd6SLuc Michel 
591439badd6SLuc Michel         gic_clear_pending(s, irq, cpu);
592439badd6SLuc Michel         ret = irq | (src << 10);
593439badd6SLuc Michel     }
594439badd6SLuc Michel 
595439badd6SLuc Michel     return ret;
596439badd6SLuc Michel }
597439badd6SLuc Michel 
598439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
599439badd6SLuc Michel {
600439badd6SLuc Michel     int ret, irq;
601439badd6SLuc Michel 
602439badd6SLuc Michel     /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
603439badd6SLuc Michel      * for the case where this GIC supports grouping and the pending interrupt
604439badd6SLuc Michel      * is in the wrong group.
60540d22500SChristoffer Dall      */
606439badd6SLuc Michel     irq = gic_get_current_pending_irq(s, cpu, attrs);
607067a2b9cSLuc Michel     trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
608067a2b9cSLuc Michel                               gic_get_vcpu_real_id(cpu), irq);
609439badd6SLuc Michel 
610439badd6SLuc Michel     if (irq >= GIC_MAXIRQ) {
611439badd6SLuc Michel         DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
612439badd6SLuc Michel         return irq;
613439badd6SLuc Michel     }
614439badd6SLuc Michel 
615439badd6SLuc Michel     if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) {
616439badd6SLuc Michel         DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
617439badd6SLuc Michel         return 1023;
618439badd6SLuc Michel     }
619439badd6SLuc Michel 
620439badd6SLuc Michel     gic_activate_irq(s, cpu, irq);
621439badd6SLuc Michel 
622439badd6SLuc Michel     if (s->revision == REV_11MPCORE) {
623439badd6SLuc Michel         /* Clear pending flags for both level and edge triggered interrupts.
624439badd6SLuc Michel          * Level triggered IRQs will be reasserted once they become inactive.
625439badd6SLuc Michel          */
626439badd6SLuc Michel         gic_clear_pending(s, irq, cpu);
627439badd6SLuc Michel         ret = irq;
628439badd6SLuc Michel     } else {
629439badd6SLuc Michel         if (irq < GIC_NR_SGIS) {
630439badd6SLuc Michel             ret = gic_clear_pending_sgi(s, irq, cpu);
631439badd6SLuc Michel         } else {
63286b350f0SLuc Michel             gic_clear_pending(s, irq, cpu);
63340d22500SChristoffer Dall             ret = irq;
63440d22500SChristoffer Dall         }
63540d22500SChristoffer Dall     }
63640d22500SChristoffer Dall 
637cbe1282bSLuc Michel     if (gic_is_vcpu(cpu)) {
638cbe1282bSLuc Michel         gic_update_virt(s);
639cbe1282bSLuc Michel     } else {
64072889c8aSPeter Maydell         gic_update(s);
641cbe1282bSLuc Michel     }
64240d22500SChristoffer Dall     DPRINTF("ACK %d\n", irq);
64340d22500SChristoffer Dall     return ret;
644e69954b9Spbrook }
645e69954b9Spbrook 
64611411489SSai Pavan Boddu static uint32_t gic_fullprio_mask(GICState *s, int cpu)
64711411489SSai Pavan Boddu {
64811411489SSai Pavan Boddu     /*
64911411489SSai Pavan Boddu      * Return a mask word which clears the unimplemented priority
65011411489SSai Pavan Boddu      * bits from a priority value for an interrupt. (Not to be
65111411489SSai Pavan Boddu      * confused with the group priority, whose mask depends on BPR.)
65211411489SSai Pavan Boddu      */
65311411489SSai Pavan Boddu     int priBits;
65411411489SSai Pavan Boddu 
65511411489SSai Pavan Boddu     if (gic_is_vcpu(cpu)) {
65611411489SSai Pavan Boddu         priBits = GIC_VIRT_MAX_GROUP_PRIO_BITS;
65711411489SSai Pavan Boddu     } else {
65811411489SSai Pavan Boddu         priBits = s->n_prio_bits;
65911411489SSai Pavan Boddu     }
66011411489SSai Pavan Boddu     return ~0U << (8 - priBits);
66111411489SSai Pavan Boddu }
66211411489SSai Pavan Boddu 
66367ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
66481508470SFabian Aggeler                       MemTxAttrs attrs)
6659df90ad0SChristoffer Dall {
66681508470SFabian Aggeler     if (s->security_extn && !attrs.secure) {
66767ce697aSLuc Michel         if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
66881508470SFabian Aggeler             return; /* Ignore Non-secure access of Group0 IRQ */
66981508470SFabian Aggeler         }
67081508470SFabian Aggeler         val = 0x80 | (val >> 1); /* Non-secure view */
67181508470SFabian Aggeler     }
67281508470SFabian Aggeler 
67311411489SSai Pavan Boddu     val &= gic_fullprio_mask(s, cpu);
67411411489SSai Pavan Boddu 
6759df90ad0SChristoffer Dall     if (irq < GIC_INTERNAL) {
6769df90ad0SChristoffer Dall         s->priority1[irq][cpu] = val;
6779df90ad0SChristoffer Dall     } else {
6789df90ad0SChristoffer Dall         s->priority2[(irq) - GIC_INTERNAL] = val;
6799df90ad0SChristoffer Dall     }
6809df90ad0SChristoffer Dall }
6819df90ad0SChristoffer Dall 
68267ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
68381508470SFabian Aggeler                                  MemTxAttrs attrs)
68481508470SFabian Aggeler {
68567ce697aSLuc Michel     uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
68681508470SFabian Aggeler 
68781508470SFabian Aggeler     if (s->security_extn && !attrs.secure) {
68867ce697aSLuc Michel         if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
68981508470SFabian Aggeler             return 0; /* Non-secure access cannot read priority of Group0 IRQ */
69081508470SFabian Aggeler         }
69181508470SFabian Aggeler         prio = (prio << 1) & 0xff; /* Non-secure view */
69281508470SFabian Aggeler     }
69311411489SSai Pavan Boddu     return prio & gic_fullprio_mask(s, cpu);
69481508470SFabian Aggeler }
69581508470SFabian Aggeler 
69681508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
69781508470SFabian Aggeler                                   MemTxAttrs attrs)
69881508470SFabian Aggeler {
6993dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
70081508470SFabian Aggeler         if (s->priority_mask[cpu] & 0x80) {
70181508470SFabian Aggeler             /* Priority Mask in upper half */
70281508470SFabian Aggeler             pmask = 0x80 | (pmask >> 1);
70381508470SFabian Aggeler         } else {
70481508470SFabian Aggeler             /* Non-secure write ignored if priority mask is in lower half */
70581508470SFabian Aggeler             return;
70681508470SFabian Aggeler         }
70781508470SFabian Aggeler     }
70811411489SSai Pavan Boddu     s->priority_mask[cpu] = pmask & gic_fullprio_mask(s, cpu);
70981508470SFabian Aggeler }
71081508470SFabian Aggeler 
71181508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
71281508470SFabian Aggeler {
71381508470SFabian Aggeler     uint32_t pmask = s->priority_mask[cpu];
71481508470SFabian Aggeler 
7153dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
71681508470SFabian Aggeler         if (pmask & 0x80) {
71781508470SFabian Aggeler             /* Priority Mask in upper half, return Non-secure view */
71881508470SFabian Aggeler             pmask = (pmask << 1) & 0xff;
71981508470SFabian Aggeler         } else {
72081508470SFabian Aggeler             /* Priority Mask in lower half, RAZ */
72181508470SFabian Aggeler             pmask = 0;
72281508470SFabian Aggeler         }
72381508470SFabian Aggeler     }
72481508470SFabian Aggeler     return pmask;
72581508470SFabian Aggeler }
72681508470SFabian Aggeler 
72732951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
72832951860SFabian Aggeler {
72932951860SFabian Aggeler     uint32_t ret = s->cpu_ctlr[cpu];
73032951860SFabian Aggeler 
7313dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
73232951860SFabian Aggeler         /* Construct the NS banked view of GICC_CTLR from the correct
73332951860SFabian Aggeler          * bits of the S banked view. We don't need to move the bypass
73432951860SFabian Aggeler          * control bits because we don't implement that (IMPDEF) part
73532951860SFabian Aggeler          * of the GIC architecture.
73632951860SFabian Aggeler          */
73732951860SFabian Aggeler         ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
73832951860SFabian Aggeler     }
73932951860SFabian Aggeler     return ret;
74032951860SFabian Aggeler }
74132951860SFabian Aggeler 
74232951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
74332951860SFabian Aggeler                                 MemTxAttrs attrs)
74432951860SFabian Aggeler {
74532951860SFabian Aggeler     uint32_t mask;
74632951860SFabian Aggeler 
7473dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
74832951860SFabian Aggeler         /* The NS view can only write certain bits in the register;
74932951860SFabian Aggeler          * the rest are unchanged
75032951860SFabian Aggeler          */
75132951860SFabian Aggeler         mask = GICC_CTLR_EN_GRP1;
75232951860SFabian Aggeler         if (s->revision == 2) {
75332951860SFabian Aggeler             mask |= GICC_CTLR_EOIMODE_NS;
75432951860SFabian Aggeler         }
75532951860SFabian Aggeler         s->cpu_ctlr[cpu] &= ~mask;
75632951860SFabian Aggeler         s->cpu_ctlr[cpu] |= (value << 1) & mask;
75732951860SFabian Aggeler     } else {
75832951860SFabian Aggeler         if (s->revision == 2) {
75932951860SFabian Aggeler             mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
76032951860SFabian Aggeler         } else {
76132951860SFabian Aggeler             mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
76232951860SFabian Aggeler         }
76332951860SFabian Aggeler         s->cpu_ctlr[cpu] = value & mask;
76432951860SFabian Aggeler     }
76532951860SFabian Aggeler     DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
76632951860SFabian Aggeler             "Group1 Interrupts %sabled\n", cpu,
76732951860SFabian Aggeler             (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
76832951860SFabian Aggeler             (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
76932951860SFabian Aggeler }
77032951860SFabian Aggeler 
77108efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
77208efa9f2SFabian Aggeler {
77371aa735bSLuc MICHEL     if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
77471aa735bSLuc MICHEL         /* Idle priority */
77571aa735bSLuc MICHEL         return 0xff;
77671aa735bSLuc MICHEL     }
77771aa735bSLuc MICHEL 
7783dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
77908efa9f2SFabian Aggeler         if (s->running_priority[cpu] & 0x80) {
78008efa9f2SFabian Aggeler             /* Running priority in upper half of range: return the Non-secure
78108efa9f2SFabian Aggeler              * view of the priority.
78208efa9f2SFabian Aggeler              */
78308efa9f2SFabian Aggeler             return s->running_priority[cpu] << 1;
78408efa9f2SFabian Aggeler         } else {
78508efa9f2SFabian Aggeler             /* Running priority in lower half of range: RAZ */
78608efa9f2SFabian Aggeler             return 0;
78708efa9f2SFabian Aggeler         }
78808efa9f2SFabian Aggeler     } else {
78908efa9f2SFabian Aggeler         return s->running_priority[cpu];
79008efa9f2SFabian Aggeler     }
79108efa9f2SFabian Aggeler }
79208efa9f2SFabian Aggeler 
793a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation,
794a55c910eSPeter Maydell  * ie whether the relevant EOIMode bit is set.
795a55c910eSPeter Maydell  */
796a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
797a55c910eSPeter Maydell {
798a55c910eSPeter Maydell     if (s->revision != 2) {
799a55c910eSPeter Maydell         /* Before GICv2 prio-drop and deactivate are not separable */
800a55c910eSPeter Maydell         return false;
801a55c910eSPeter Maydell     }
8023dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
803a55c910eSPeter Maydell         return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
804a55c910eSPeter Maydell     }
805a55c910eSPeter Maydell     return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
806a55c910eSPeter Maydell }
807a55c910eSPeter Maydell 
808a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
809a55c910eSPeter Maydell {
810ee03cca8SPeter Maydell     int group;
811ee03cca8SPeter Maydell 
81202f2e22dSLuc Michel     if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
813ee03cca8SPeter Maydell         /*
814ee03cca8SPeter Maydell          * This handles two cases:
815ee03cca8SPeter Maydell          * 1. If software writes the ID of a spurious interrupt [ie 1023]
816ee03cca8SPeter Maydell          * to the GICC_DIR, the GIC ignores that write.
817ee03cca8SPeter Maydell          * 2. If software writes the number of a non-existent interrupt
818ee03cca8SPeter Maydell          * this must be a subcase of "value written is not an active interrupt"
81902f2e22dSLuc Michel          * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs,
82002f2e22dSLuc Michel          * all IRQs potentially exist, so this limit does not apply.
821ee03cca8SPeter Maydell          */
822ee03cca8SPeter Maydell         return;
823ee03cca8SPeter Maydell     }
824ee03cca8SPeter Maydell 
825a55c910eSPeter Maydell     if (!gic_eoi_split(s, cpu, attrs)) {
826a55c910eSPeter Maydell         /* This is UNPREDICTABLE; we choose to ignore it */
827a55c910eSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
828a55c910eSPeter Maydell                       "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
829a55c910eSPeter Maydell         return;
830a55c910eSPeter Maydell     }
831a55c910eSPeter Maydell 
83202f2e22dSLuc Michel     if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
83302f2e22dSLuc Michel         /* This vIRQ does not have an LR entry which is either active or
83402f2e22dSLuc Michel          * pending and active. Increment EOICount and ignore the write.
83502f2e22dSLuc Michel          */
83602f2e22dSLuc Michel         int rcpu = gic_get_vcpu_real_id(cpu);
83702f2e22dSLuc Michel         s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
838cbe1282bSLuc Michel 
839cbe1282bSLuc Michel         /* Update the virtual interface in case a maintenance interrupt should
840cbe1282bSLuc Michel          * be raised.
841cbe1282bSLuc Michel          */
842cbe1282bSLuc Michel         gic_update_virt(s);
84302f2e22dSLuc Michel         return;
84402f2e22dSLuc Michel     }
84502f2e22dSLuc Michel 
84602f2e22dSLuc Michel     group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
84702f2e22dSLuc Michel 
8483dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
849a55c910eSPeter Maydell         DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
850a55c910eSPeter Maydell         return;
851a55c910eSPeter Maydell     }
852a55c910eSPeter Maydell 
85386b350f0SLuc Michel     gic_clear_active(s, irq, cpu);
854a55c910eSPeter Maydell }
855a55c910eSPeter Maydell 
85650491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
857e69954b9Spbrook {
8589ee6e8bbSpbrook     int cm = 1 << cpu;
85972889c8aSPeter Maydell     int group;
86072889c8aSPeter Maydell 
861df628ff1Spbrook     DPRINTF("EOI %d\n", irq);
86202f2e22dSLuc Michel     if (gic_is_vcpu(cpu)) {
86302f2e22dSLuc Michel         /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the
86402f2e22dSLuc Michel          * running prio is < 0x100.
86502f2e22dSLuc Michel          */
86602f2e22dSLuc Michel         bool prio_drop = s->running_priority[cpu] < 0x100;
86702f2e22dSLuc Michel 
86802f2e22dSLuc Michel         if (irq >= GIC_MAXIRQ) {
86902f2e22dSLuc Michel             /* Ignore spurious interrupt */
87002f2e22dSLuc Michel             return;
87102f2e22dSLuc Michel         }
87202f2e22dSLuc Michel 
87302f2e22dSLuc Michel         gic_drop_prio(s, cpu, 0);
87402f2e22dSLuc Michel 
87502f2e22dSLuc Michel         if (!gic_eoi_split(s, cpu, attrs)) {
87602f2e22dSLuc Michel             bool valid = gic_virq_is_valid(s, irq, cpu);
87702f2e22dSLuc Michel             if (prio_drop && !valid) {
87802f2e22dSLuc Michel                 /* We are in a situation where:
87902f2e22dSLuc Michel                  *   - V_CTRL.EOIMode is false (no EOI split),
88002f2e22dSLuc Michel                  *   - The call to gic_drop_prio() cleared a bit in GICH_APR,
88102f2e22dSLuc Michel                  *   - This vIRQ does not have an LR entry which is either
88202f2e22dSLuc Michel                  *     active or pending and active.
88302f2e22dSLuc Michel                  * In that case, we must increment EOICount.
88402f2e22dSLuc Michel                  */
88502f2e22dSLuc Michel                 int rcpu = gic_get_vcpu_real_id(cpu);
88602f2e22dSLuc Michel                 s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
88702f2e22dSLuc Michel             } else if (valid) {
88802f2e22dSLuc Michel                 gic_clear_active(s, irq, cpu);
88902f2e22dSLuc Michel             }
89002f2e22dSLuc Michel         }
89102f2e22dSLuc Michel 
892cbe1282bSLuc Michel         gic_update_virt(s);
89302f2e22dSLuc Michel         return;
89402f2e22dSLuc Michel     }
89502f2e22dSLuc Michel 
896a32134aaSMark Langsdorf     if (irq >= s->num_irq) {
897217bfb44SPeter Maydell         /* This handles two cases:
898217bfb44SPeter Maydell          * 1. If software writes the ID of a spurious interrupt [ie 1023]
899217bfb44SPeter Maydell          * to the GICC_EOIR, the GIC ignores that write.
900217bfb44SPeter Maydell          * 2. If software writes the number of a non-existent interrupt
901217bfb44SPeter Maydell          * this must be a subcase of "value written does not match the last
902217bfb44SPeter Maydell          * valid interrupt value read from the Interrupt Acknowledge
903217bfb44SPeter Maydell          * register" and so this is UNPREDICTABLE. We choose to ignore it.
904217bfb44SPeter Maydell          */
905217bfb44SPeter Maydell         return;
906217bfb44SPeter Maydell     }
90772889c8aSPeter Maydell     if (s->running_priority[cpu] == 0x100) {
908e69954b9Spbrook         return; /* No active IRQ.  */
90972889c8aSPeter Maydell     }
9108d999995SChristoffer Dall 
9113bc4b52cSMarcin Krzeminski     if (s->revision == REV_11MPCORE) {
912e69954b9Spbrook         /* Mark level triggered interrupts as pending if they are still
913e69954b9Spbrook            raised.  */
91467ce697aSLuc Michel         if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
91567ce697aSLuc Michel             && GIC_DIST_TEST_LEVEL(irq, cm)
91667ce697aSLuc Michel             && (GIC_DIST_TARGET(irq) & cm) != 0) {
9179ee6e8bbSpbrook             DPRINTF("Set %d pending mask %x\n", irq, cm);
91867ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, cm);
919e69954b9Spbrook         }
9208d999995SChristoffer Dall     }
9218d999995SChristoffer Dall 
92286b350f0SLuc Michel     group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
92372889c8aSPeter Maydell 
9243dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
925f9c6a7f1SFabian Aggeler         DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
926f9c6a7f1SFabian Aggeler         return;
927f9c6a7f1SFabian Aggeler     }
928f9c6a7f1SFabian Aggeler 
929f9c6a7f1SFabian Aggeler     /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
930f9c6a7f1SFabian Aggeler      * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
931f9c6a7f1SFabian Aggeler      * i.e. go ahead and complete the irq anyway.
932f9c6a7f1SFabian Aggeler      */
933f9c6a7f1SFabian Aggeler 
93472889c8aSPeter Maydell     gic_drop_prio(s, cpu, group);
935a55c910eSPeter Maydell 
936a55c910eSPeter Maydell     /* In GICv2 the guest can choose to split priority-drop and deactivate */
937a55c910eSPeter Maydell     if (!gic_eoi_split(s, cpu, attrs)) {
93886b350f0SLuc Michel         gic_clear_active(s, irq, cpu);
939a55c910eSPeter Maydell     }
940e69954b9Spbrook     gic_update(s);
941e69954b9Spbrook }
942e69954b9Spbrook 
943a9d85353SPeter Maydell static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
944e69954b9Spbrook {
945fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
946e69954b9Spbrook     uint32_t res;
947e69954b9Spbrook     int irq;
948e69954b9Spbrook     int i;
9499ee6e8bbSpbrook     int cpu;
9509ee6e8bbSpbrook     int cm;
9519ee6e8bbSpbrook     int mask;
952e69954b9Spbrook 
953926c4affSPeter Maydell     cpu = gic_get_current_cpu(s);
9549ee6e8bbSpbrook     cm = 1 << cpu;
955e69954b9Spbrook     if (offset < 0x100) {
956679aa175SFabian Aggeler         if (offset == 0) {      /* GICD_CTLR */
957679aa175SFabian Aggeler             if (s->security_extn && !attrs.secure) {
958679aa175SFabian Aggeler                 /* The NS bank of this register is just an alias of the
959679aa175SFabian Aggeler                  * EnableGrp1 bit in the S bank version.
960679aa175SFabian Aggeler                  */
961679aa175SFabian Aggeler                 return extract32(s->ctlr, 1, 1);
962679aa175SFabian Aggeler             } else {
963679aa175SFabian Aggeler                 return s->ctlr;
964679aa175SFabian Aggeler             }
965679aa175SFabian Aggeler         }
966e69954b9Spbrook         if (offset == 4)
9675543d1abSFabian Aggeler             /* Interrupt Controller Type Register */
9685543d1abSFabian Aggeler             return ((s->num_irq / 32) - 1)
969b95690c9SWei Huang                     | ((s->num_cpu - 1) << 5)
9705543d1abSFabian Aggeler                     | (s->security_extn << 10);
971e69954b9Spbrook         if (offset < 0x08)
972e69954b9Spbrook             return 0;
973b79f2265SRob Herring         if (offset >= 0x80) {
974c27a5ba9SFabian Aggeler             /* Interrupt Group Registers: these RAZ/WI if this is an NS
975c27a5ba9SFabian Aggeler              * access to a GIC with the security extensions, or if the GIC
976c27a5ba9SFabian Aggeler              * doesn't have groups at all.
977c27a5ba9SFabian Aggeler              */
978c27a5ba9SFabian Aggeler             res = 0;
979c27a5ba9SFabian Aggeler             if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
980c27a5ba9SFabian Aggeler                 /* Every byte offset holds 8 group status bits */
981b6e6c651SPeter Maydell                 irq = (offset - 0x080) * 8;
982c27a5ba9SFabian Aggeler                 if (irq >= s->num_irq) {
983c27a5ba9SFabian Aggeler                     goto bad_reg;
984c27a5ba9SFabian Aggeler                 }
985c27a5ba9SFabian Aggeler                 for (i = 0; i < 8; i++) {
98667ce697aSLuc Michel                     if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
987c27a5ba9SFabian Aggeler                         res |= (1 << i);
988c27a5ba9SFabian Aggeler                     }
989c27a5ba9SFabian Aggeler                 }
990c27a5ba9SFabian Aggeler             }
991c27a5ba9SFabian Aggeler             return res;
992b79f2265SRob Herring         }
993e69954b9Spbrook         goto bad_reg;
994e69954b9Spbrook     } else if (offset < 0x200) {
995e69954b9Spbrook         /* Interrupt Set/Clear Enable.  */
996e69954b9Spbrook         if (offset < 0x180)
997e69954b9Spbrook             irq = (offset - 0x100) * 8;
998e69954b9Spbrook         else
999e69954b9Spbrook             irq = (offset - 0x180) * 8;
1000a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1001e69954b9Spbrook             goto bad_reg;
1002e69954b9Spbrook         res = 0;
1003e69954b9Spbrook         for (i = 0; i < 8; i++) {
1004fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
100567ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1006fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1007fea8a08eSJens Wiklander             }
1008fea8a08eSJens Wiklander 
100967ce697aSLuc Michel             if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1010e69954b9Spbrook                 res |= (1 << i);
1011e69954b9Spbrook             }
1012e69954b9Spbrook         }
1013e69954b9Spbrook     } else if (offset < 0x300) {
1014e69954b9Spbrook         /* Interrupt Set/Clear Pending.  */
1015e69954b9Spbrook         if (offset < 0x280)
1016e69954b9Spbrook             irq = (offset - 0x200) * 8;
1017e69954b9Spbrook         else
1018e69954b9Spbrook             irq = (offset - 0x280) * 8;
1019a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1020e69954b9Spbrook             goto bad_reg;
1021e69954b9Spbrook         res = 0;
102269253800SRusty Russell         mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
1023e69954b9Spbrook         for (i = 0; i < 8; i++) {
1024fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
102567ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1026fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1027fea8a08eSJens Wiklander             }
1028fea8a08eSJens Wiklander 
10298d999995SChristoffer Dall             if (gic_test_pending(s, irq + i, mask)) {
1030e69954b9Spbrook                 res |= (1 << i);
1031e69954b9Spbrook             }
1032e69954b9Spbrook         }
1033e69954b9Spbrook     } else if (offset < 0x400) {
10343bb0b038SLuc Michel         /* Interrupt Set/Clear Active.  */
10353bb0b038SLuc Michel         if (offset < 0x380) {
10363bb0b038SLuc Michel             irq = (offset - 0x300) * 8;
10373bb0b038SLuc Michel         } else if (s->revision == 2) {
10383bb0b038SLuc Michel             irq = (offset - 0x380) * 8;
10393bb0b038SLuc Michel         } else {
10403bb0b038SLuc Michel             goto bad_reg;
10413bb0b038SLuc Michel         }
10423bb0b038SLuc Michel 
1043a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1044e69954b9Spbrook             goto bad_reg;
1045e69954b9Spbrook         res = 0;
104669253800SRusty Russell         mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
1047e69954b9Spbrook         for (i = 0; i < 8; i++) {
1048fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
104967ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1050fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1051fea8a08eSJens Wiklander             }
1052fea8a08eSJens Wiklander 
105367ce697aSLuc Michel             if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
1054e69954b9Spbrook                 res |= (1 << i);
1055e69954b9Spbrook             }
1056e69954b9Spbrook         }
1057e69954b9Spbrook     } else if (offset < 0x800) {
1058e69954b9Spbrook         /* Interrupt Priority.  */
1059b6e6c651SPeter Maydell         irq = (offset - 0x400);
1060a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1061e69954b9Spbrook             goto bad_reg;
106267ce697aSLuc Michel         res = gic_dist_get_priority(s, cpu, irq, attrs);
1063e69954b9Spbrook     } else if (offset < 0xc00) {
1064e69954b9Spbrook         /* Interrupt CPU Target.  */
10656b9680bbSPeter Maydell         if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
10666b9680bbSPeter Maydell             /* For uniprocessor GICs these RAZ/WI */
10676b9680bbSPeter Maydell             res = 0;
10686b9680bbSPeter Maydell         } else {
1069b6e6c651SPeter Maydell             irq = (offset - 0x800);
10706b9680bbSPeter Maydell             if (irq >= s->num_irq) {
1071e69954b9Spbrook                 goto bad_reg;
10726b9680bbSPeter Maydell             }
10737995206dSPeter Maydell             if (irq < 29 && s->revision == REV_11MPCORE) {
10747995206dSPeter Maydell                 res = 0;
10757995206dSPeter Maydell             } else if (irq < GIC_INTERNAL) {
10769ee6e8bbSpbrook                 res = cm;
10779ee6e8bbSpbrook             } else {
107867ce697aSLuc Michel                 res = GIC_DIST_TARGET(irq);
10799ee6e8bbSpbrook             }
10806b9680bbSPeter Maydell         }
1081e69954b9Spbrook     } else if (offset < 0xf00) {
1082e69954b9Spbrook         /* Interrupt Configuration.  */
1083b6e6c651SPeter Maydell         irq = (offset - 0xc00) * 4;
1084a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1085e69954b9Spbrook             goto bad_reg;
1086e69954b9Spbrook         res = 0;
1087e69954b9Spbrook         for (i = 0; i < 4; i++) {
1088fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
108967ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1090fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1091fea8a08eSJens Wiklander             }
1092fea8a08eSJens Wiklander 
109367ce697aSLuc Michel             if (GIC_DIST_TEST_MODEL(irq + i)) {
1094e69954b9Spbrook                 res |= (1 << (i * 2));
109567ce697aSLuc Michel             }
109667ce697aSLuc Michel             if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1097e69954b9Spbrook                 res |= (2 << (i * 2));
1098e69954b9Spbrook             }
109967ce697aSLuc Michel         }
110040d22500SChristoffer Dall     } else if (offset < 0xf10) {
110140d22500SChristoffer Dall         goto bad_reg;
110240d22500SChristoffer Dall     } else if (offset < 0xf30) {
11037c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
110440d22500SChristoffer Dall             goto bad_reg;
110540d22500SChristoffer Dall         }
110640d22500SChristoffer Dall 
110740d22500SChristoffer Dall         if (offset < 0xf20) {
110840d22500SChristoffer Dall             /* GICD_CPENDSGIRn */
110940d22500SChristoffer Dall             irq = (offset - 0xf10);
111040d22500SChristoffer Dall         } else {
111140d22500SChristoffer Dall             irq = (offset - 0xf20);
111240d22500SChristoffer Dall             /* GICD_SPENDSGIRn */
111340d22500SChristoffer Dall         }
111440d22500SChristoffer Dall 
1115fea8a08eSJens Wiklander         if (s->security_extn && !attrs.secure &&
111667ce697aSLuc Michel             !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1117fea8a08eSJens Wiklander             res = 0; /* Ignore Non-secure access of Group0 IRQ */
1118fea8a08eSJens Wiklander         } else {
111940d22500SChristoffer Dall             res = s->sgi_pending[irq][cpu];
1120fea8a08eSJens Wiklander         }
11213355c360SAlistair Francis     } else if (offset < 0xfd0) {
1122e69954b9Spbrook         goto bad_reg;
11233355c360SAlistair Francis     } else if (offset < 0x1000) {
1124e69954b9Spbrook         if (offset & 3) {
1125e69954b9Spbrook             res = 0;
1126e69954b9Spbrook         } else {
11273355c360SAlistair Francis             switch (s->revision) {
11283355c360SAlistair Francis             case REV_11MPCORE:
11293355c360SAlistair Francis                 res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
11303355c360SAlistair Francis                 break;
11313355c360SAlistair Francis             case 1:
11323355c360SAlistair Francis                 res = gic_id_gicv1[(offset - 0xfd0) >> 2];
11333355c360SAlistair Francis                 break;
11343355c360SAlistair Francis             case 2:
11353355c360SAlistair Francis                 res = gic_id_gicv2[(offset - 0xfd0) >> 2];
11363355c360SAlistair Francis                 break;
11373355c360SAlistair Francis             default:
11383355c360SAlistair Francis                 res = 0;
1139e69954b9Spbrook             }
1140e69954b9Spbrook         }
11413355c360SAlistair Francis     } else {
11423355c360SAlistair Francis         g_assert_not_reached();
11433355c360SAlistair Francis     }
1144e69954b9Spbrook     return res;
1145e69954b9Spbrook bad_reg:
11468c8dc39fSPeter Maydell     qemu_log_mask(LOG_GUEST_ERROR,
11478c8dc39fSPeter Maydell                   "gic_dist_readb: Bad offset %x\n", (int)offset);
1148e69954b9Spbrook     return 0;
1149e69954b9Spbrook }
1150e69954b9Spbrook 
1151a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
1152a9d85353SPeter Maydell                                  unsigned size, MemTxAttrs attrs)
1153e69954b9Spbrook {
1154a9d85353SPeter Maydell     switch (size) {
1155a9d85353SPeter Maydell     case 1:
1156a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1157067a2b9cSLuc Michel         break;
1158a9d85353SPeter Maydell     case 2:
1159a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1160a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1161067a2b9cSLuc Michel         break;
1162a9d85353SPeter Maydell     case 4:
1163a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1164a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1165a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
1166a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
1167067a2b9cSLuc Michel         break;
1168a9d85353SPeter Maydell     default:
1169a9d85353SPeter Maydell         return MEMTX_ERROR;
1170e69954b9Spbrook     }
1171067a2b9cSLuc Michel 
1172067a2b9cSLuc Michel     trace_gic_dist_read(offset, size, *data);
1173067a2b9cSLuc Michel     return MEMTX_OK;
1174e69954b9Spbrook }
1175e69954b9Spbrook 
1176a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset,
1177a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1178e69954b9Spbrook {
1179fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1180e69954b9Spbrook     int irq;
1181e69954b9Spbrook     int i;
11829ee6e8bbSpbrook     int cpu;
1183e69954b9Spbrook 
1184926c4affSPeter Maydell     cpu = gic_get_current_cpu(s);
1185e69954b9Spbrook     if (offset < 0x100) {
1186e69954b9Spbrook         if (offset == 0) {
1187679aa175SFabian Aggeler             if (s->security_extn && !attrs.secure) {
1188679aa175SFabian Aggeler                 /* NS version is just an alias of the S version's bit 1 */
1189679aa175SFabian Aggeler                 s->ctlr = deposit32(s->ctlr, 1, 1, value);
1190679aa175SFabian Aggeler             } else if (gic_has_groups(s)) {
1191679aa175SFabian Aggeler                 s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
1192679aa175SFabian Aggeler             } else {
1193679aa175SFabian Aggeler                 s->ctlr = value & GICD_CTLR_EN_GRP0;
1194679aa175SFabian Aggeler             }
1195679aa175SFabian Aggeler             DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
1196679aa175SFabian Aggeler                     s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
1197679aa175SFabian Aggeler                     s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
1198e69954b9Spbrook         } else if (offset < 4) {
1199e69954b9Spbrook             /* ignored.  */
1200b79f2265SRob Herring         } else if (offset >= 0x80) {
1201c27a5ba9SFabian Aggeler             /* Interrupt Group Registers: RAZ/WI for NS access to secure
1202c27a5ba9SFabian Aggeler              * GIC, or for GICs without groups.
1203c27a5ba9SFabian Aggeler              */
1204c27a5ba9SFabian Aggeler             if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
1205c27a5ba9SFabian Aggeler                 /* Every byte offset holds 8 group status bits */
1206b6e6c651SPeter Maydell                 irq = (offset - 0x80) * 8;
1207c27a5ba9SFabian Aggeler                 if (irq >= s->num_irq) {
1208c27a5ba9SFabian Aggeler                     goto bad_reg;
1209c27a5ba9SFabian Aggeler                 }
1210c27a5ba9SFabian Aggeler                 for (i = 0; i < 8; i++) {
1211c27a5ba9SFabian Aggeler                     /* Group bits are banked for private interrupts */
1212c27a5ba9SFabian Aggeler                     int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1213c27a5ba9SFabian Aggeler                     if (value & (1 << i)) {
1214c27a5ba9SFabian Aggeler                         /* Group1 (Non-secure) */
121567ce697aSLuc Michel                         GIC_DIST_SET_GROUP(irq + i, cm);
1216c27a5ba9SFabian Aggeler                     } else {
1217c27a5ba9SFabian Aggeler                         /* Group0 (Secure) */
121867ce697aSLuc Michel                         GIC_DIST_CLEAR_GROUP(irq + i, cm);
1219c27a5ba9SFabian Aggeler                     }
1220c27a5ba9SFabian Aggeler                 }
1221c27a5ba9SFabian Aggeler             }
1222e69954b9Spbrook         } else {
1223e69954b9Spbrook             goto bad_reg;
1224e69954b9Spbrook         }
1225e69954b9Spbrook     } else if (offset < 0x180) {
1226e69954b9Spbrook         /* Interrupt Set Enable.  */
1227b6e6c651SPeter Maydell         irq = (offset - 0x100) * 8;
1228a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1229e69954b9Spbrook             goto bad_reg;
123041ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12319ee6e8bbSpbrook             value = 0xff;
123241ab7b55SChristoffer Dall         }
123341ab7b55SChristoffer Dall 
1234e69954b9Spbrook         for (i = 0; i < 8; i++) {
1235e69954b9Spbrook             if (value & (1 << i)) {
1236f47b48fbSDaniel Sangorrin                 int mask =
123767ce697aSLuc Michel                     (irq < GIC_INTERNAL) ? (1 << cpu)
123867ce697aSLuc Michel                                          : GIC_DIST_TARGET(irq + i);
123969253800SRusty Russell                 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
124041bf234dSRabin Vincent 
1241fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
124267ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1243fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1244fea8a08eSJens Wiklander                 }
1245fea8a08eSJens Wiklander 
124667ce697aSLuc Michel                 if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1247e69954b9Spbrook                     DPRINTF("Enabled IRQ %d\n", irq + i);
12482531088fSHollis Blanchard                     trace_gic_enable_irq(irq + i);
124941bf234dSRabin Vincent                 }
125067ce697aSLuc Michel                 GIC_DIST_SET_ENABLED(irq + i, cm);
1251e69954b9Spbrook                 /* If a raised level triggered IRQ enabled then mark
1252e69954b9Spbrook                    is as pending.  */
125367ce697aSLuc Michel                 if (GIC_DIST_TEST_LEVEL(irq + i, mask)
125467ce697aSLuc Michel                         && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
12559ee6e8bbSpbrook                     DPRINTF("Set %d pending mask %x\n", irq + i, mask);
125667ce697aSLuc Michel                     GIC_DIST_SET_PENDING(irq + i, mask);
12579ee6e8bbSpbrook                 }
1258e69954b9Spbrook             }
1259e69954b9Spbrook         }
1260e69954b9Spbrook     } else if (offset < 0x200) {
1261e69954b9Spbrook         /* Interrupt Clear Enable.  */
1262b6e6c651SPeter Maydell         irq = (offset - 0x180) * 8;
1263a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1264e69954b9Spbrook             goto bad_reg;
126541ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12669ee6e8bbSpbrook             value = 0;
126741ab7b55SChristoffer Dall         }
126841ab7b55SChristoffer Dall 
1269e69954b9Spbrook         for (i = 0; i < 8; i++) {
1270e69954b9Spbrook             if (value & (1 << i)) {
127169253800SRusty Russell                 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
127241bf234dSRabin Vincent 
1273fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
127467ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1275fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1276fea8a08eSJens Wiklander                 }
1277fea8a08eSJens Wiklander 
127867ce697aSLuc Michel                 if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1279e69954b9Spbrook                     DPRINTF("Disabled IRQ %d\n", irq + i);
12802531088fSHollis Blanchard                     trace_gic_disable_irq(irq + i);
128141bf234dSRabin Vincent                 }
128267ce697aSLuc Michel                 GIC_DIST_CLEAR_ENABLED(irq + i, cm);
1283e69954b9Spbrook             }
1284e69954b9Spbrook         }
1285e69954b9Spbrook     } else if (offset < 0x280) {
1286e69954b9Spbrook         /* Interrupt Set Pending.  */
1287b6e6c651SPeter Maydell         irq = (offset - 0x200) * 8;
1288a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1289e69954b9Spbrook             goto bad_reg;
129041ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12915b0adce1SChristoffer Dall             value = 0;
129241ab7b55SChristoffer Dall         }
12939ee6e8bbSpbrook 
1294e69954b9Spbrook         for (i = 0; i < 8; i++) {
1295e69954b9Spbrook             if (value & (1 << i)) {
1296fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
129767ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1298fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1299fea8a08eSJens Wiklander                 }
1300fea8a08eSJens Wiklander 
130167ce697aSLuc Michel                 GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i));
1302e69954b9Spbrook             }
1303e69954b9Spbrook         }
1304e69954b9Spbrook     } else if (offset < 0x300) {
1305e69954b9Spbrook         /* Interrupt Clear Pending.  */
1306b6e6c651SPeter Maydell         irq = (offset - 0x280) * 8;
1307a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1308e69954b9Spbrook             goto bad_reg;
13095b0adce1SChristoffer Dall         if (irq < GIC_NR_SGIS) {
13105b0adce1SChristoffer Dall             value = 0;
13115b0adce1SChristoffer Dall         }
13125b0adce1SChristoffer Dall 
1313e69954b9Spbrook         for (i = 0; i < 8; i++) {
1314fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
131567ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1316fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1317fea8a08eSJens Wiklander             }
1318fea8a08eSJens Wiklander 
13199ee6e8bbSpbrook             /* ??? This currently clears the pending bit for all CPUs, even
13209ee6e8bbSpbrook                for per-CPU interrupts.  It's unclear whether this is the
13219ee6e8bbSpbrook                corect behavior.  */
1322e69954b9Spbrook             if (value & (1 << i)) {
132367ce697aSLuc Michel                 GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
1324e69954b9Spbrook             }
1325e69954b9Spbrook         }
13263bb0b038SLuc Michel     } else if (offset < 0x380) {
13273bb0b038SLuc Michel         /* Interrupt Set Active.  */
13283bb0b038SLuc Michel         if (s->revision != 2) {
1329e69954b9Spbrook             goto bad_reg;
13303bb0b038SLuc Michel         }
13313bb0b038SLuc Michel 
1332b6e6c651SPeter Maydell         irq = (offset - 0x300) * 8;
13333bb0b038SLuc Michel         if (irq >= s->num_irq) {
13343bb0b038SLuc Michel             goto bad_reg;
13353bb0b038SLuc Michel         }
13363bb0b038SLuc Michel 
13373bb0b038SLuc Michel         /* This register is banked per-cpu for PPIs */
13383bb0b038SLuc Michel         int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13393bb0b038SLuc Michel 
13403bb0b038SLuc Michel         for (i = 0; i < 8; i++) {
13413bb0b038SLuc Michel             if (s->security_extn && !attrs.secure &&
13423bb0b038SLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13433bb0b038SLuc Michel                 continue; /* Ignore Non-secure access of Group0 IRQ */
13443bb0b038SLuc Michel             }
13453bb0b038SLuc Michel 
13463bb0b038SLuc Michel             if (value & (1 << i)) {
13473bb0b038SLuc Michel                 GIC_DIST_SET_ACTIVE(irq + i, cm);
13483bb0b038SLuc Michel             }
13493bb0b038SLuc Michel         }
13503bb0b038SLuc Michel     } else if (offset < 0x400) {
13513bb0b038SLuc Michel         /* Interrupt Clear Active.  */
13523bb0b038SLuc Michel         if (s->revision != 2) {
13533bb0b038SLuc Michel             goto bad_reg;
13543bb0b038SLuc Michel         }
13553bb0b038SLuc Michel 
1356b6e6c651SPeter Maydell         irq = (offset - 0x380) * 8;
13573bb0b038SLuc Michel         if (irq >= s->num_irq) {
13583bb0b038SLuc Michel             goto bad_reg;
13593bb0b038SLuc Michel         }
13603bb0b038SLuc Michel 
13613bb0b038SLuc Michel         /* This register is banked per-cpu for PPIs */
13623bb0b038SLuc Michel         int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13633bb0b038SLuc Michel 
13643bb0b038SLuc Michel         for (i = 0; i < 8; i++) {
13653bb0b038SLuc Michel             if (s->security_extn && !attrs.secure &&
13663bb0b038SLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13673bb0b038SLuc Michel                 continue; /* Ignore Non-secure access of Group0 IRQ */
13683bb0b038SLuc Michel             }
13693bb0b038SLuc Michel 
13703bb0b038SLuc Michel             if (value & (1 << i)) {
13713bb0b038SLuc Michel                 GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
13723bb0b038SLuc Michel             }
13733bb0b038SLuc Michel         }
1374e69954b9Spbrook     } else if (offset < 0x800) {
1375e69954b9Spbrook         /* Interrupt Priority.  */
1376b6e6c651SPeter Maydell         irq = (offset - 0x400);
1377a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1378e69954b9Spbrook             goto bad_reg;
137967ce697aSLuc Michel         gic_dist_set_priority(s, cpu, irq, value, attrs);
1380e69954b9Spbrook     } else if (offset < 0xc00) {
13816b9680bbSPeter Maydell         /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
13826b9680bbSPeter Maydell          * annoying exception of the 11MPCore's GIC.
13836b9680bbSPeter Maydell          */
13846b9680bbSPeter Maydell         if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
1385b6e6c651SPeter Maydell             irq = (offset - 0x800);
13866b9680bbSPeter Maydell             if (irq >= s->num_irq) {
1387e69954b9Spbrook                 goto bad_reg;
13886b9680bbSPeter Maydell             }
13897995206dSPeter Maydell             if (irq < 29 && s->revision == REV_11MPCORE) {
13909ee6e8bbSpbrook                 value = 0;
13916b9680bbSPeter Maydell             } else if (irq < GIC_INTERNAL) {
13929ee6e8bbSpbrook                 value = ALL_CPU_MASK;
13936b9680bbSPeter Maydell             }
13949ee6e8bbSpbrook             s->irq_target[irq] = value & ALL_CPU_MASK;
13956b9680bbSPeter Maydell         }
1396e69954b9Spbrook     } else if (offset < 0xf00) {
1397e69954b9Spbrook         /* Interrupt Configuration.  */
1398b6e6c651SPeter Maydell         irq = (offset - 0xc00) * 4;
1399a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1400e69954b9Spbrook             goto bad_reg;
1401de7a900fSAdam Lackorzynski         if (irq < GIC_NR_SGIS)
14029ee6e8bbSpbrook             value |= 0xaa;
1403e69954b9Spbrook         for (i = 0; i < 4; i++) {
1404fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
140567ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1406fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1407fea8a08eSJens Wiklander             }
1408fea8a08eSJens Wiklander 
14097c14b3acSMichael Davidsaver             if (s->revision == REV_11MPCORE) {
1410e69954b9Spbrook                 if (value & (1 << (i * 2))) {
141167ce697aSLuc Michel                     GIC_DIST_SET_MODEL(irq + i);
1412e69954b9Spbrook                 } else {
141367ce697aSLuc Michel                     GIC_DIST_CLEAR_MODEL(irq + i);
1414e69954b9Spbrook                 }
141524b790dfSAdam Lackorzynski             }
1416e69954b9Spbrook             if (value & (2 << (i * 2))) {
141767ce697aSLuc Michel                 GIC_DIST_SET_EDGE_TRIGGER(irq + i);
1418e69954b9Spbrook             } else {
141967ce697aSLuc Michel                 GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
1420e69954b9Spbrook             }
1421e69954b9Spbrook         }
142240d22500SChristoffer Dall     } else if (offset < 0xf10) {
14239ee6e8bbSpbrook         /* 0xf00 is only handled for 32-bit writes.  */
1424e69954b9Spbrook         goto bad_reg;
142540d22500SChristoffer Dall     } else if (offset < 0xf20) {
142640d22500SChristoffer Dall         /* GICD_CPENDSGIRn */
14277c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
142840d22500SChristoffer Dall             goto bad_reg;
142940d22500SChristoffer Dall         }
143040d22500SChristoffer Dall         irq = (offset - 0xf10);
143140d22500SChristoffer Dall 
1432fea8a08eSJens Wiklander         if (!s->security_extn || attrs.secure ||
143367ce697aSLuc Michel             GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
143440d22500SChristoffer Dall             s->sgi_pending[irq][cpu] &= ~value;
143540d22500SChristoffer Dall             if (s->sgi_pending[irq][cpu] == 0) {
143667ce697aSLuc Michel                 GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
143740d22500SChristoffer Dall             }
1438fea8a08eSJens Wiklander         }
143940d22500SChristoffer Dall     } else if (offset < 0xf30) {
144040d22500SChristoffer Dall         /* GICD_SPENDSGIRn */
14417c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
144240d22500SChristoffer Dall             goto bad_reg;
144340d22500SChristoffer Dall         }
144440d22500SChristoffer Dall         irq = (offset - 0xf20);
144540d22500SChristoffer Dall 
1446fea8a08eSJens Wiklander         if (!s->security_extn || attrs.secure ||
144767ce697aSLuc Michel             GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
144867ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, 1 << cpu);
144940d22500SChristoffer Dall             s->sgi_pending[irq][cpu] |= value;
1450fea8a08eSJens Wiklander         }
145140d22500SChristoffer Dall     } else {
145240d22500SChristoffer Dall         goto bad_reg;
1453e69954b9Spbrook     }
1454e69954b9Spbrook     gic_update(s);
1455e69954b9Spbrook     return;
1456e69954b9Spbrook bad_reg:
14578c8dc39fSPeter Maydell     qemu_log_mask(LOG_GUEST_ERROR,
14588c8dc39fSPeter Maydell                   "gic_dist_writeb: Bad offset %x\n", (int)offset);
1459e69954b9Spbrook }
1460e69954b9Spbrook 
1461a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset,
1462a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1463e69954b9Spbrook {
1464a9d85353SPeter Maydell     gic_dist_writeb(opaque, offset, value & 0xff, attrs);
1465a9d85353SPeter Maydell     gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
1466e69954b9Spbrook }
1467e69954b9Spbrook 
1468a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset,
1469a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1470e69954b9Spbrook {
1471fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
14728da3ff18Spbrook     if (offset == 0xf00) {
14739ee6e8bbSpbrook         int cpu;
14749ee6e8bbSpbrook         int irq;
14759ee6e8bbSpbrook         int mask;
147640d22500SChristoffer Dall         int target_cpu;
14779ee6e8bbSpbrook 
1478926c4affSPeter Maydell         cpu = gic_get_current_cpu(s);
14799ee6e8bbSpbrook         irq = value & 0x3ff;
14809ee6e8bbSpbrook         switch ((value >> 24) & 3) {
14819ee6e8bbSpbrook         case 0:
14829ee6e8bbSpbrook             mask = (value >> 16) & ALL_CPU_MASK;
14839ee6e8bbSpbrook             break;
14849ee6e8bbSpbrook         case 1:
1485fa250144SAdam Lackorzynski             mask = ALL_CPU_MASK ^ (1 << cpu);
14869ee6e8bbSpbrook             break;
14879ee6e8bbSpbrook         case 2:
1488fa250144SAdam Lackorzynski             mask = 1 << cpu;
14899ee6e8bbSpbrook             break;
14909ee6e8bbSpbrook         default:
14919ee6e8bbSpbrook             DPRINTF("Bad Soft Int target filter\n");
14929ee6e8bbSpbrook             mask = ALL_CPU_MASK;
14939ee6e8bbSpbrook             break;
14949ee6e8bbSpbrook         }
149567ce697aSLuc Michel         GIC_DIST_SET_PENDING(irq, mask);
149640d22500SChristoffer Dall         target_cpu = ctz32(mask);
149740d22500SChristoffer Dall         while (target_cpu < GIC_NCPU) {
149840d22500SChristoffer Dall             s->sgi_pending[irq][target_cpu] |= (1 << cpu);
149940d22500SChristoffer Dall             mask &= ~(1 << target_cpu);
150040d22500SChristoffer Dall             target_cpu = ctz32(mask);
150140d22500SChristoffer Dall         }
15029ee6e8bbSpbrook         gic_update(s);
15039ee6e8bbSpbrook         return;
15049ee6e8bbSpbrook     }
1505a9d85353SPeter Maydell     gic_dist_writew(opaque, offset, value & 0xffff, attrs);
1506a9d85353SPeter Maydell     gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
1507a9d85353SPeter Maydell }
1508a9d85353SPeter Maydell 
1509a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
1510a9d85353SPeter Maydell                                   unsigned size, MemTxAttrs attrs)
1511a9d85353SPeter Maydell {
1512067a2b9cSLuc Michel     trace_gic_dist_write(offset, size, data);
1513067a2b9cSLuc Michel 
1514a9d85353SPeter Maydell     switch (size) {
1515a9d85353SPeter Maydell     case 1:
1516a9d85353SPeter Maydell         gic_dist_writeb(opaque, offset, data, attrs);
1517a9d85353SPeter Maydell         return MEMTX_OK;
1518a9d85353SPeter Maydell     case 2:
1519a9d85353SPeter Maydell         gic_dist_writew(opaque, offset, data, attrs);
1520a9d85353SPeter Maydell         return MEMTX_OK;
1521a9d85353SPeter Maydell     case 4:
1522a9d85353SPeter Maydell         gic_dist_writel(opaque, offset, data, attrs);
1523a9d85353SPeter Maydell         return MEMTX_OK;
1524a9d85353SPeter Maydell     default:
1525a9d85353SPeter Maydell         return MEMTX_ERROR;
1526a9d85353SPeter Maydell     }
1527e69954b9Spbrook }
1528e69954b9Spbrook 
152951fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
153051fd06e0SPeter Maydell {
153151fd06e0SPeter Maydell     /* Return the Nonsecure view of GICC_APR<regno>. This is the
153251fd06e0SPeter Maydell      * second half of GICC_NSAPR.
153351fd06e0SPeter Maydell      */
153451fd06e0SPeter Maydell     switch (GIC_MIN_BPR) {
153551fd06e0SPeter Maydell     case 0:
153651fd06e0SPeter Maydell         if (regno < 2) {
153751fd06e0SPeter Maydell             return s->nsapr[regno + 2][cpu];
153851fd06e0SPeter Maydell         }
153951fd06e0SPeter Maydell         break;
154051fd06e0SPeter Maydell     case 1:
154151fd06e0SPeter Maydell         if (regno == 0) {
154251fd06e0SPeter Maydell             return s->nsapr[regno + 1][cpu];
154351fd06e0SPeter Maydell         }
154451fd06e0SPeter Maydell         break;
154551fd06e0SPeter Maydell     case 2:
154651fd06e0SPeter Maydell         if (regno == 0) {
154751fd06e0SPeter Maydell             return extract32(s->nsapr[0][cpu], 16, 16);
154851fd06e0SPeter Maydell         }
154951fd06e0SPeter Maydell         break;
155051fd06e0SPeter Maydell     case 3:
155151fd06e0SPeter Maydell         if (regno == 0) {
155251fd06e0SPeter Maydell             return extract32(s->nsapr[0][cpu], 8, 8);
155351fd06e0SPeter Maydell         }
155451fd06e0SPeter Maydell         break;
155551fd06e0SPeter Maydell     default:
155651fd06e0SPeter Maydell         g_assert_not_reached();
155751fd06e0SPeter Maydell     }
155851fd06e0SPeter Maydell     return 0;
155951fd06e0SPeter Maydell }
156051fd06e0SPeter Maydell 
156151fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
156251fd06e0SPeter Maydell                                          uint32_t value)
156351fd06e0SPeter Maydell {
156451fd06e0SPeter Maydell     /* Write the Nonsecure view of GICC_APR<regno>. */
156551fd06e0SPeter Maydell     switch (GIC_MIN_BPR) {
156651fd06e0SPeter Maydell     case 0:
156751fd06e0SPeter Maydell         if (regno < 2) {
156851fd06e0SPeter Maydell             s->nsapr[regno + 2][cpu] = value;
156951fd06e0SPeter Maydell         }
157051fd06e0SPeter Maydell         break;
157151fd06e0SPeter Maydell     case 1:
157251fd06e0SPeter Maydell         if (regno == 0) {
157351fd06e0SPeter Maydell             s->nsapr[regno + 1][cpu] = value;
157451fd06e0SPeter Maydell         }
157551fd06e0SPeter Maydell         break;
157651fd06e0SPeter Maydell     case 2:
157751fd06e0SPeter Maydell         if (regno == 0) {
157851fd06e0SPeter Maydell             s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
157951fd06e0SPeter Maydell         }
158051fd06e0SPeter Maydell         break;
158151fd06e0SPeter Maydell     case 3:
158251fd06e0SPeter Maydell         if (regno == 0) {
158351fd06e0SPeter Maydell             s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
158451fd06e0SPeter Maydell         }
158551fd06e0SPeter Maydell         break;
158651fd06e0SPeter Maydell     default:
158751fd06e0SPeter Maydell         g_assert_not_reached();
158851fd06e0SPeter Maydell     }
158951fd06e0SPeter Maydell }
159051fd06e0SPeter Maydell 
1591a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1592a9d85353SPeter Maydell                                 uint64_t *data, MemTxAttrs attrs)
1593e69954b9Spbrook {
1594e69954b9Spbrook     switch (offset) {
1595e69954b9Spbrook     case 0x00: /* Control */
159632951860SFabian Aggeler         *data = gic_get_cpu_control(s, cpu, attrs);
1597a9d85353SPeter Maydell         break;
1598e69954b9Spbrook     case 0x04: /* Priority mask */
159981508470SFabian Aggeler         *data = gic_get_priority_mask(s, cpu, attrs);
1600a9d85353SPeter Maydell         break;
1601e69954b9Spbrook     case 0x08: /* Binary Point */
16023dd0471bSLuc Michel         if (gic_cpu_ns_access(s, cpu, attrs)) {
1603421a3c22SLuc MICHEL             if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1604421a3c22SLuc MICHEL                 /* NS view of BPR when CBPR is 1 */
1605421a3c22SLuc MICHEL                 *data = MIN(s->bpr[cpu] + 1, 7);
1606421a3c22SLuc MICHEL             } else {
1607822e9cc3SFabian Aggeler                 /* BPR is banked. Non-secure copy stored in ABPR. */
1608822e9cc3SFabian Aggeler                 *data = s->abpr[cpu];
1609421a3c22SLuc MICHEL             }
1610822e9cc3SFabian Aggeler         } else {
1611a9d85353SPeter Maydell             *data = s->bpr[cpu];
1612822e9cc3SFabian Aggeler         }
1613a9d85353SPeter Maydell         break;
1614e69954b9Spbrook     case 0x0c: /* Acknowledge */
1615c5619bf9SFabian Aggeler         *data = gic_acknowledge_irq(s, cpu, attrs);
1616a9d85353SPeter Maydell         break;
161766a0a2cbSDong Xu Wang     case 0x14: /* Running Priority */
161808efa9f2SFabian Aggeler         *data = gic_get_running_priority(s, cpu, attrs);
1619a9d85353SPeter Maydell         break;
1620e69954b9Spbrook     case 0x18: /* Highest Pending Interrupt */
16217c0fa108SFabian Aggeler         *data = gic_get_current_pending_irq(s, cpu, attrs);
1622a9d85353SPeter Maydell         break;
1623aa7d461aSChristoffer Dall     case 0x1c: /* Aliased Binary Point */
1624822e9cc3SFabian Aggeler         /* GIC v2, no security: ABPR
1625822e9cc3SFabian Aggeler          * GIC v1, no security: not implemented (RAZ/WI)
1626822e9cc3SFabian Aggeler          * With security extensions, secure access: ABPR (alias of NS BPR)
1627822e9cc3SFabian Aggeler          * With security extensions, nonsecure access: RAZ/WI
1628822e9cc3SFabian Aggeler          */
16293dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1630822e9cc3SFabian Aggeler             *data = 0;
1631822e9cc3SFabian Aggeler         } else {
1632a9d85353SPeter Maydell             *data = s->abpr[cpu];
1633822e9cc3SFabian Aggeler         }
1634a9d85353SPeter Maydell         break;
1635a9d477c4SChristoffer Dall     case 0xd0: case 0xd4: case 0xd8: case 0xdc:
163651fd06e0SPeter Maydell     {
163751fd06e0SPeter Maydell         int regno = (offset - 0xd0) / 4;
16387eb079ecSLuc Michel         int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
163951fd06e0SPeter Maydell 
16407eb079ecSLuc Michel         if (regno >= nr_aprs || s->revision != 2) {
164151fd06e0SPeter Maydell             *data = 0;
16427eb079ecSLuc Michel         } else if (gic_is_vcpu(cpu)) {
16437eb079ecSLuc Michel             *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
16443dd0471bSLuc Michel         } else if (gic_cpu_ns_access(s, cpu, attrs)) {
164551fd06e0SPeter Maydell             /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
164651fd06e0SPeter Maydell             *data = gic_apr_ns_view(s, regno, cpu);
164751fd06e0SPeter Maydell         } else {
164851fd06e0SPeter Maydell             *data = s->apr[regno][cpu];
164951fd06e0SPeter Maydell         }
1650a9d85353SPeter Maydell         break;
165151fd06e0SPeter Maydell     }
165251fd06e0SPeter Maydell     case 0xe0: case 0xe4: case 0xe8: case 0xec:
165351fd06e0SPeter Maydell     {
165451fd06e0SPeter Maydell         int regno = (offset - 0xe0) / 4;
165551fd06e0SPeter Maydell 
165651fd06e0SPeter Maydell         if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
16577eb079ecSLuc Michel             gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
165851fd06e0SPeter Maydell             *data = 0;
165951fd06e0SPeter Maydell         } else {
166051fd06e0SPeter Maydell             *data = s->nsapr[regno][cpu];
166151fd06e0SPeter Maydell         }
166251fd06e0SPeter Maydell         break;
166351fd06e0SPeter Maydell     }
1664e69954b9Spbrook     default:
16658c8dc39fSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
16668c8dc39fSPeter Maydell                       "gic_cpu_read: Bad offset %x\n", (int)offset);
16670cf09852SPeter Maydell         *data = 0;
16680cf09852SPeter Maydell         break;
1669e69954b9Spbrook     }
1670067a2b9cSLuc Michel 
1671067a2b9cSLuc Michel     trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1672067a2b9cSLuc Michel                        gic_get_vcpu_real_id(cpu), offset, *data);
1673a9d85353SPeter Maydell     return MEMTX_OK;
1674e69954b9Spbrook }
1675e69954b9Spbrook 
1676a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1677a9d85353SPeter Maydell                                  uint32_t value, MemTxAttrs attrs)
1678e69954b9Spbrook {
1679067a2b9cSLuc Michel     trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1680067a2b9cSLuc Michel                         gic_get_vcpu_real_id(cpu), offset, value);
1681067a2b9cSLuc Michel 
1682e69954b9Spbrook     switch (offset) {
1683e69954b9Spbrook     case 0x00: /* Control */
168432951860SFabian Aggeler         gic_set_cpu_control(s, cpu, value, attrs);
1685e69954b9Spbrook         break;
1686e69954b9Spbrook     case 0x04: /* Priority mask */
168781508470SFabian Aggeler         gic_set_priority_mask(s, cpu, value, attrs);
1688e69954b9Spbrook         break;
1689e69954b9Spbrook     case 0x08: /* Binary Point */
16903dd0471bSLuc Michel         if (gic_cpu_ns_access(s, cpu, attrs)) {
1691421a3c22SLuc MICHEL             if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1692421a3c22SLuc MICHEL                 /* WI when CBPR is 1 */
1693421a3c22SLuc MICHEL                 return MEMTX_OK;
1694421a3c22SLuc MICHEL             } else {
1695822e9cc3SFabian Aggeler                 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1696421a3c22SLuc MICHEL             }
1697822e9cc3SFabian Aggeler         } else {
16987eb079ecSLuc Michel             int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
16997eb079ecSLuc Michel             s->bpr[cpu] = MAX(value & 0x7, min_bpr);
1700822e9cc3SFabian Aggeler         }
1701e69954b9Spbrook         break;
1702e69954b9Spbrook     case 0x10: /* End Of Interrupt */
1703f9c6a7f1SFabian Aggeler         gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1704a9d85353SPeter Maydell         return MEMTX_OK;
1705aa7d461aSChristoffer Dall     case 0x1c: /* Aliased Binary Point */
17063dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1707822e9cc3SFabian Aggeler             /* unimplemented, or NS access: RAZ/WI */
1708822e9cc3SFabian Aggeler             return MEMTX_OK;
1709822e9cc3SFabian Aggeler         } else {
1710822e9cc3SFabian Aggeler             s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1711aa7d461aSChristoffer Dall         }
1712aa7d461aSChristoffer Dall         break;
1713a9d477c4SChristoffer Dall     case 0xd0: case 0xd4: case 0xd8: case 0xdc:
171451fd06e0SPeter Maydell     {
171551fd06e0SPeter Maydell         int regno = (offset - 0xd0) / 4;
17167eb079ecSLuc Michel         int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
171751fd06e0SPeter Maydell 
17187eb079ecSLuc Michel         if (regno >= nr_aprs || s->revision != 2) {
171951fd06e0SPeter Maydell             return MEMTX_OK;
172051fd06e0SPeter Maydell         }
17217eb079ecSLuc Michel         if (gic_is_vcpu(cpu)) {
17227eb079ecSLuc Michel             s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
17237eb079ecSLuc Michel         } else if (gic_cpu_ns_access(s, cpu, attrs)) {
172451fd06e0SPeter Maydell             /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
172551fd06e0SPeter Maydell             gic_apr_write_ns_view(s, regno, cpu, value);
172651fd06e0SPeter Maydell         } else {
172751fd06e0SPeter Maydell             s->apr[regno][cpu] = value;
172851fd06e0SPeter Maydell         }
1729a9d477c4SChristoffer Dall         break;
173051fd06e0SPeter Maydell     }
173151fd06e0SPeter Maydell     case 0xe0: case 0xe4: case 0xe8: case 0xec:
173251fd06e0SPeter Maydell     {
173351fd06e0SPeter Maydell         int regno = (offset - 0xe0) / 4;
173451fd06e0SPeter Maydell 
173551fd06e0SPeter Maydell         if (regno >= GIC_NR_APRS || s->revision != 2) {
173651fd06e0SPeter Maydell             return MEMTX_OK;
173751fd06e0SPeter Maydell         }
17387eb079ecSLuc Michel         if (gic_is_vcpu(cpu)) {
17397eb079ecSLuc Michel             return MEMTX_OK;
17407eb079ecSLuc Michel         }
17413dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
174251fd06e0SPeter Maydell             return MEMTX_OK;
174351fd06e0SPeter Maydell         }
174451fd06e0SPeter Maydell         s->nsapr[regno][cpu] = value;
174551fd06e0SPeter Maydell         break;
174651fd06e0SPeter Maydell     }
1747a55c910eSPeter Maydell     case 0x1000:
1748a55c910eSPeter Maydell         /* GICC_DIR */
1749a55c910eSPeter Maydell         gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
1750a55c910eSPeter Maydell         break;
1751e69954b9Spbrook     default:
17528c8dc39fSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
17538c8dc39fSPeter Maydell                       "gic_cpu_write: Bad offset %x\n", (int)offset);
17540cf09852SPeter Maydell         return MEMTX_OK;
1755e69954b9Spbrook     }
1756cbe1282bSLuc Michel 
1757cbe1282bSLuc Michel     if (gic_is_vcpu(cpu)) {
1758cbe1282bSLuc Michel         gic_update_virt(s);
1759cbe1282bSLuc Michel     } else {
1760e69954b9Spbrook         gic_update(s);
1761cbe1282bSLuc Michel     }
1762cbe1282bSLuc Michel 
1763a9d85353SPeter Maydell     return MEMTX_OK;
1764e69954b9Spbrook }
1765e2c56465SPeter Maydell 
1766e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */
1767a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1768a9d85353SPeter Maydell                                     unsigned size, MemTxAttrs attrs)
1769e2c56465SPeter Maydell {
1770fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1771a9d85353SPeter Maydell     return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1772e2c56465SPeter Maydell }
1773e2c56465SPeter Maydell 
1774a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1775a9d85353SPeter Maydell                                      uint64_t value, unsigned size,
1776a9d85353SPeter Maydell                                      MemTxAttrs attrs)
1777e2c56465SPeter Maydell {
1778fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1779a9d85353SPeter Maydell     return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1780e2c56465SPeter Maydell }
1781e2c56465SPeter Maydell 
1782e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU.
1783fae15286SPeter Maydell  * These just decode the opaque pointer into GICState* + cpu id.
1784e2c56465SPeter Maydell  */
1785a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1786a9d85353SPeter Maydell                                    unsigned size, MemTxAttrs attrs)
1787e2c56465SPeter Maydell {
1788fae15286SPeter Maydell     GICState **backref = (GICState **)opaque;
1789fae15286SPeter Maydell     GICState *s = *backref;
1790e2c56465SPeter Maydell     int id = (backref - s->backref);
1791a9d85353SPeter Maydell     return gic_cpu_read(s, id, addr, data, attrs);
1792e2c56465SPeter Maydell }
1793e2c56465SPeter Maydell 
1794a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1795a9d85353SPeter Maydell                                     uint64_t value, unsigned size,
1796a9d85353SPeter Maydell                                     MemTxAttrs attrs)
1797e2c56465SPeter Maydell {
1798fae15286SPeter Maydell     GICState **backref = (GICState **)opaque;
1799fae15286SPeter Maydell     GICState *s = *backref;
1800e2c56465SPeter Maydell     int id = (backref - s->backref);
1801a9d85353SPeter Maydell     return gic_cpu_write(s, id, addr, value, attrs);
1802e2c56465SPeter Maydell }
1803e2c56465SPeter Maydell 
18042c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
18052c679ac7SLuc Michel                                     unsigned size, MemTxAttrs attrs)
18062c679ac7SLuc Michel {
18072c679ac7SLuc Michel     GICState *s = (GICState *)opaque;
18082c679ac7SLuc Michel 
18092c679ac7SLuc Michel     return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
18102c679ac7SLuc Michel }
18112c679ac7SLuc Michel 
18122c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
18132c679ac7SLuc Michel                                      uint64_t value, unsigned size,
18142c679ac7SLuc Michel                                      MemTxAttrs attrs)
18152c679ac7SLuc Michel {
18162c679ac7SLuc Michel     GICState *s = (GICState *)opaque;
18172c679ac7SLuc Michel 
18182c679ac7SLuc Michel     return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
18192c679ac7SLuc Michel }
18202c679ac7SLuc Michel 
1821527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
1822527d296fSLuc Michel {
1823527d296fSLuc Michel     int lr_idx;
1824527d296fSLuc Michel     uint32_t ret = 0;
1825527d296fSLuc Michel 
1826527d296fSLuc Michel     for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1827527d296fSLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
1828527d296fSLuc Michel         ret = deposit32(ret, lr_idx - lr_start, 1,
1829527d296fSLuc Michel                         gic_lr_entry_is_eoi(*entry));
1830527d296fSLuc Michel     }
1831527d296fSLuc Michel 
1832527d296fSLuc Michel     return ret;
1833527d296fSLuc Michel }
1834527d296fSLuc Michel 
1835527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
1836527d296fSLuc Michel {
1837527d296fSLuc Michel     int lr_idx;
1838527d296fSLuc Michel     uint32_t ret = 0;
1839527d296fSLuc Michel 
1840527d296fSLuc Michel     for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1841527d296fSLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
1842527d296fSLuc Michel         ret = deposit32(ret, lr_idx - lr_start, 1,
1843527d296fSLuc Michel                         gic_lr_entry_is_free(*entry));
1844527d296fSLuc Michel     }
1845527d296fSLuc Michel 
1846527d296fSLuc Michel     return ret;
1847527d296fSLuc Michel }
1848527d296fSLuc Michel 
1849527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
1850527d296fSLuc Michel {
1851527d296fSLuc Michel     int vcpu = gic_get_current_vcpu(s);
1852527d296fSLuc Michel     uint32_t ctlr;
1853527d296fSLuc Michel     uint32_t abpr;
1854527d296fSLuc Michel     uint32_t bpr;
1855527d296fSLuc Michel     uint32_t prio_mask;
1856527d296fSLuc Michel 
1857527d296fSLuc Michel     ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
1858527d296fSLuc Michel     abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
1859527d296fSLuc Michel     bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
1860527d296fSLuc Michel     prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
1861527d296fSLuc Michel 
1862527d296fSLuc Michel     gic_set_cpu_control(s, vcpu, ctlr, attrs);
1863527d296fSLuc Michel     s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
1864527d296fSLuc Michel     s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
1865527d296fSLuc Michel     gic_set_priority_mask(s, vcpu, prio_mask, attrs);
1866527d296fSLuc Michel }
1867527d296fSLuc Michel 
1868527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
1869527d296fSLuc Michel                                 uint64_t *data, MemTxAttrs attrs)
1870527d296fSLuc Michel {
1871527d296fSLuc Michel     GICState *s = ARM_GIC(opaque);
1872527d296fSLuc Michel     int vcpu = cpu + GIC_NCPU;
1873527d296fSLuc Michel 
1874527d296fSLuc Michel     switch (addr) {
1875527d296fSLuc Michel     case A_GICH_HCR: /* Hypervisor Control */
1876527d296fSLuc Michel         *data = s->h_hcr[cpu];
1877527d296fSLuc Michel         break;
1878527d296fSLuc Michel 
1879527d296fSLuc Michel     case A_GICH_VTR: /* VGIC Type */
1880527d296fSLuc Michel         *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
1881527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VTR, PREbits,
1882527d296fSLuc Michel                            GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
1883527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
1884527d296fSLuc Michel                            (7 - GIC_VIRT_MIN_BPR) - 1);
1885527d296fSLuc Michel         break;
1886527d296fSLuc Michel 
1887527d296fSLuc Michel     case A_GICH_VMCR: /* Virtual Machine Control */
1888527d296fSLuc Michel         *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
1889527d296fSLuc Michel                            extract32(s->cpu_ctlr[vcpu], 0, 10));
1890527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
1891527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
1892527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
1893527d296fSLuc Michel                            extract32(s->priority_mask[vcpu], 3, 5));
1894527d296fSLuc Michel         break;
1895527d296fSLuc Michel 
1896527d296fSLuc Michel     case A_GICH_MISR: /* Maintenance Interrupt Status */
1897527d296fSLuc Michel         *data = s->h_misr[cpu];
1898527d296fSLuc Michel         break;
1899527d296fSLuc Michel 
1900527d296fSLuc Michel     case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */
1901527d296fSLuc Michel     case A_GICH_EISR1:
1902527d296fSLuc Michel         *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
1903527d296fSLuc Michel         break;
1904527d296fSLuc Michel 
1905527d296fSLuc Michel     case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */
1906527d296fSLuc Michel     case A_GICH_ELRSR1:
1907527d296fSLuc Michel         *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
1908527d296fSLuc Michel         break;
1909527d296fSLuc Michel 
1910527d296fSLuc Michel     case A_GICH_APR: /* Active Priorities */
1911527d296fSLuc Michel         *data = s->h_apr[cpu];
1912527d296fSLuc Michel         break;
1913527d296fSLuc Michel 
1914527d296fSLuc Michel     case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1915527d296fSLuc Michel     {
1916527d296fSLuc Michel         int lr_idx = (addr - A_GICH_LR0) / 4;
1917527d296fSLuc Michel 
1918527d296fSLuc Michel         if (lr_idx > s->num_lrs) {
1919527d296fSLuc Michel             *data = 0;
1920527d296fSLuc Michel         } else {
1921527d296fSLuc Michel             *data = s->h_lr[lr_idx][cpu];
1922527d296fSLuc Michel         }
1923527d296fSLuc Michel         break;
1924527d296fSLuc Michel     }
1925527d296fSLuc Michel 
1926527d296fSLuc Michel     default:
1927527d296fSLuc Michel         qemu_log_mask(LOG_GUEST_ERROR,
1928527d296fSLuc Michel                       "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
1929527d296fSLuc Michel         return MEMTX_OK;
1930527d296fSLuc Michel     }
1931527d296fSLuc Michel 
1932067a2b9cSLuc Michel     trace_gic_hyp_read(addr, *data);
1933527d296fSLuc Michel     return MEMTX_OK;
1934527d296fSLuc Michel }
1935527d296fSLuc Michel 
1936527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
1937527d296fSLuc Michel                                  uint64_t value, MemTxAttrs attrs)
1938527d296fSLuc Michel {
1939527d296fSLuc Michel     GICState *s = ARM_GIC(opaque);
1940527d296fSLuc Michel     int vcpu = cpu + GIC_NCPU;
1941527d296fSLuc Michel 
1942067a2b9cSLuc Michel     trace_gic_hyp_write(addr, value);
1943067a2b9cSLuc Michel 
1944527d296fSLuc Michel     switch (addr) {
1945527d296fSLuc Michel     case A_GICH_HCR: /* Hypervisor Control */
1946527d296fSLuc Michel         s->h_hcr[cpu] = value & GICH_HCR_MASK;
1947527d296fSLuc Michel         break;
1948527d296fSLuc Michel 
1949527d296fSLuc Michel     case A_GICH_VMCR: /* Virtual Machine Control */
1950527d296fSLuc Michel         gic_vmcr_write(s, value, attrs);
1951527d296fSLuc Michel         break;
1952527d296fSLuc Michel 
1953527d296fSLuc Michel     case A_GICH_APR: /* Active Priorities */
1954527d296fSLuc Michel         s->h_apr[cpu] = value;
1955527d296fSLuc Michel         s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
1956527d296fSLuc Michel         break;
1957527d296fSLuc Michel 
1958527d296fSLuc Michel     case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1959527d296fSLuc Michel     {
1960527d296fSLuc Michel         int lr_idx = (addr - A_GICH_LR0) / 4;
1961527d296fSLuc Michel 
1962527d296fSLuc Michel         if (lr_idx > s->num_lrs) {
1963527d296fSLuc Michel             return MEMTX_OK;
1964527d296fSLuc Michel         }
1965527d296fSLuc Michel 
1966527d296fSLuc Michel         s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
1967067a2b9cSLuc Michel         trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]);
1968527d296fSLuc Michel         break;
1969527d296fSLuc Michel     }
1970527d296fSLuc Michel 
1971527d296fSLuc Michel     default:
1972527d296fSLuc Michel         qemu_log_mask(LOG_GUEST_ERROR,
1973527d296fSLuc Michel                       "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
1974527d296fSLuc Michel         return MEMTX_OK;
1975527d296fSLuc Michel     }
1976527d296fSLuc Michel 
1977cbe1282bSLuc Michel     gic_update_virt(s);
1978527d296fSLuc Michel     return MEMTX_OK;
1979527d296fSLuc Michel }
1980527d296fSLuc Michel 
1981527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1982527d296fSLuc Michel                                     unsigned size, MemTxAttrs attrs)
1983527d296fSLuc Michel {
1984527d296fSLuc Michel     GICState *s = (GICState *)opaque;
1985527d296fSLuc Michel 
1986527d296fSLuc Michel     return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
1987527d296fSLuc Michel }
1988527d296fSLuc Michel 
1989527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
1990527d296fSLuc Michel                                      uint64_t value, unsigned size,
1991527d296fSLuc Michel                                      MemTxAttrs attrs)
1992527d296fSLuc Michel {
1993527d296fSLuc Michel     GICState *s = (GICState *)opaque;
1994527d296fSLuc Michel 
1995527d296fSLuc Michel     return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
1996527d296fSLuc Michel }
1997527d296fSLuc Michel 
1998527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1999527d296fSLuc Michel                                     unsigned size, MemTxAttrs attrs)
2000527d296fSLuc Michel {
2001527d296fSLuc Michel     GICState **backref = (GICState **)opaque;
2002527d296fSLuc Michel     GICState *s = *backref;
2003527d296fSLuc Michel     int id = (backref - s->backref);
2004527d296fSLuc Michel 
2005527d296fSLuc Michel     return gic_hyp_read(s, id, addr, data, attrs);
2006527d296fSLuc Michel }
2007527d296fSLuc Michel 
2008527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
2009527d296fSLuc Michel                                      uint64_t value, unsigned size,
2010527d296fSLuc Michel                                      MemTxAttrs attrs)
2011527d296fSLuc Michel {
2012527d296fSLuc Michel     GICState **backref = (GICState **)opaque;
2013527d296fSLuc Michel     GICState *s = *backref;
2014527d296fSLuc Michel     int id = (backref - s->backref);
2015527d296fSLuc Michel 
2016527d296fSLuc Michel     return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
2017527d296fSLuc Michel 
2018527d296fSLuc Michel }
2019527d296fSLuc Michel 
20207926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = {
20217926c210SPavel Fedin     {
20227926c210SPavel Fedin         .read_with_attrs = gic_dist_read,
20237926c210SPavel Fedin         .write_with_attrs = gic_dist_write,
20247926c210SPavel Fedin         .endianness = DEVICE_NATIVE_ENDIAN,
20257926c210SPavel Fedin     },
20267926c210SPavel Fedin     {
2027a9d85353SPeter Maydell         .read_with_attrs = gic_thiscpu_read,
2028a9d85353SPeter Maydell         .write_with_attrs = gic_thiscpu_write,
2029e2c56465SPeter Maydell         .endianness = DEVICE_NATIVE_ENDIAN,
20307926c210SPavel Fedin     }
2031e2c56465SPeter Maydell };
2032e2c56465SPeter Maydell 
2033e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = {
2034a9d85353SPeter Maydell     .read_with_attrs = gic_do_cpu_read,
2035a9d85353SPeter Maydell     .write_with_attrs = gic_do_cpu_write,
2036e2c56465SPeter Maydell     .endianness = DEVICE_NATIVE_ENDIAN,
2037e2c56465SPeter Maydell };
2038e69954b9Spbrook 
20392c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = {
20402c679ac7SLuc Michel     {
2041527d296fSLuc Michel         .read_with_attrs = gic_thiscpu_hyp_read,
2042527d296fSLuc Michel         .write_with_attrs = gic_thiscpu_hyp_write,
20432c679ac7SLuc Michel         .endianness = DEVICE_NATIVE_ENDIAN,
20442c679ac7SLuc Michel     },
20452c679ac7SLuc Michel     {
20462c679ac7SLuc Michel         .read_with_attrs = gic_thisvcpu_read,
20472c679ac7SLuc Michel         .write_with_attrs = gic_thisvcpu_write,
20482c679ac7SLuc Michel         .endianness = DEVICE_NATIVE_ENDIAN,
20492c679ac7SLuc Michel     }
20502c679ac7SLuc Michel };
20512c679ac7SLuc Michel 
2052527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = {
2053527d296fSLuc Michel     .read_with_attrs = gic_do_hyp_read,
2054527d296fSLuc Michel     .write_with_attrs = gic_do_hyp_write,
2055527d296fSLuc Michel     .endianness = DEVICE_NATIVE_ENDIAN,
2056527d296fSLuc Michel };
2057527d296fSLuc Michel 
205853111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp)
20592b518c56SPeter Maydell {
206053111180SPeter Maydell     /* Device instance realize function for the GIC sysbus device */
20612b518c56SPeter Maydell     int i;
206253111180SPeter Maydell     GICState *s = ARM_GIC(dev);
206353111180SPeter Maydell     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
20641e8cae4dSPeter Maydell     ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
20650175ba10SMarkus Armbruster     Error *local_err = NULL;
20661e8cae4dSPeter Maydell 
20670175ba10SMarkus Armbruster     agc->parent_realize(dev, &local_err);
20680175ba10SMarkus Armbruster     if (local_err) {
20690175ba10SMarkus Armbruster         error_propagate(errp, local_err);
207053111180SPeter Maydell         return;
207153111180SPeter Maydell     }
20721e8cae4dSPeter Maydell 
20735d721b78SAlexander Graf     if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
20745d721b78SAlexander Graf         error_setg(errp, "KVM with user space irqchip only works when the "
20755d721b78SAlexander Graf                          "host kernel supports KVM_CAP_ARM_USER_IRQ");
20765d721b78SAlexander Graf         return;
20775d721b78SAlexander Graf     }
20785d721b78SAlexander Graf 
207911411489SSai Pavan Boddu     if (s->n_prio_bits > GIC_MAX_PRIORITY_BITS ||
208011411489SSai Pavan Boddu        (s->virt_extn ? s->n_prio_bits < GIC_VIRT_MAX_GROUP_PRIO_BITS :
208111411489SSai Pavan Boddu         s->n_prio_bits < GIC_MIN_PRIORITY_BITS)) {
208211411489SSai Pavan Boddu         error_setg(errp, "num-priority-bits cannot be greater than %d"
208311411489SSai Pavan Boddu                    " or less than %d", GIC_MAX_PRIORITY_BITS,
208411411489SSai Pavan Boddu                    s->virt_extn ? GIC_VIRT_MAX_GROUP_PRIO_BITS :
208511411489SSai Pavan Boddu                    GIC_MIN_PRIORITY_BITS);
208611411489SSai Pavan Boddu         return;
208711411489SSai Pavan Boddu     }
208811411489SSai Pavan Boddu 
20892c679ac7SLuc Michel     /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if
20902c679ac7SLuc Michel      * enabled, virtualization extensions related interfaces (main virtual
20912c679ac7SLuc Michel      * interface (s->vifaceiomem[0]) and virtual CPU interface).
20922c679ac7SLuc Michel      */
20932c679ac7SLuc Michel     gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
20942b518c56SPeter Maydell 
20957926c210SPavel Fedin     /* Extra core-specific regions for the CPU interfaces. This is
20967926c210SPavel Fedin      * necessary for "franken-GIC" implementations, for example on
20977926c210SPavel Fedin      * Exynos 4.
2098e2c56465SPeter Maydell      * NB that the memory region size of 0x100 applies for the 11MPCore
2099e2c56465SPeter Maydell      * and also cores following the GIC v1 spec (ie A9).
2100e2c56465SPeter Maydell      * GIC v2 defines a larger memory region (0x1000) so this will need
2101e2c56465SPeter Maydell      * to be extended when we implement A15.
2102e2c56465SPeter Maydell      */
2103b95690c9SWei Huang     for (i = 0; i < s->num_cpu; i++) {
2104e2c56465SPeter Maydell         s->backref[i] = s;
21051437c94bSPaolo Bonzini         memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
21061437c94bSPaolo Bonzini                               &s->backref[i], "gic_cpu", 0x100);
21077926c210SPavel Fedin         sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
2108496dbcd1SPeter Maydell     }
2109527d296fSLuc Michel 
2110527d296fSLuc Michel     /* Extra core-specific regions for virtual interfaces. This is required by
2111527d296fSLuc Michel      * the GICv2 specification.
2112527d296fSLuc Michel      */
2113527d296fSLuc Michel     if (s->virt_extn) {
2114527d296fSLuc Michel         for (i = 0; i < s->num_cpu; i++) {
2115527d296fSLuc Michel             memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
2116527d296fSLuc Michel                                   &gic_viface_ops, &s->backref[i],
21177210918cSPeter Maydell                                   "gic_viface", 0x200);
2118527d296fSLuc Michel             sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
2119527d296fSLuc Michel         }
2120527d296fSLuc Michel     }
2121527d296fSLuc Michel 
2122496dbcd1SPeter Maydell }
2123496dbcd1SPeter Maydell 
2124496dbcd1SPeter Maydell static void arm_gic_class_init(ObjectClass *klass, void *data)
2125496dbcd1SPeter Maydell {
2126496dbcd1SPeter Maydell     DeviceClass *dc = DEVICE_CLASS(klass);
21271e8cae4dSPeter Maydell     ARMGICClass *agc = ARM_GIC_CLASS(klass);
212853111180SPeter Maydell 
2129bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
2130496dbcd1SPeter Maydell }
2131496dbcd1SPeter Maydell 
21328c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = {
21331e8cae4dSPeter Maydell     .name = TYPE_ARM_GIC,
21341e8cae4dSPeter Maydell     .parent = TYPE_ARM_GIC_COMMON,
2135fae15286SPeter Maydell     .instance_size = sizeof(GICState),
2136496dbcd1SPeter Maydell     .class_init = arm_gic_class_init,
2137998a74bcSPeter Maydell     .class_size = sizeof(ARMGICClass),
2138496dbcd1SPeter Maydell };
2139496dbcd1SPeter Maydell 
2140496dbcd1SPeter Maydell static void arm_gic_register_types(void)
2141496dbcd1SPeter Maydell {
2142496dbcd1SPeter Maydell     type_register_static(&arm_gic_info);
2143496dbcd1SPeter Maydell }
2144496dbcd1SPeter Maydell 
2145496dbcd1SPeter Maydell type_init(arm_gic_register_types)
2146