xref: /qemu/hw/intc/arm_gic.c (revision 50e579262d8187ea69a844d9af8e1064200008b3)
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"
2283c9f4caSPaolo Bonzini #include "hw/sysbus.h"
2347b43a1fSPaolo Bonzini #include "gic_internal.h"
24da34e65cSMarkus Armbruster #include "qapi/error.h"
25dfc08079SAndreas Färber #include "qom/cpu.h"
2603dd024fSPaolo Bonzini #include "qemu/log.h"
272531088fSHollis Blanchard #include "trace.h"
285d721b78SAlexander Graf #include "sysemu/kvm.h"
29386e2955SPeter Maydell 
3068bf93ceSAlex Bennée /* #define DEBUG_GIC */
31e69954b9Spbrook 
32e69954b9Spbrook #ifdef DEBUG_GIC
3368bf93ceSAlex Bennée #define DEBUG_GIC_GATE 1
34e69954b9Spbrook #else
3568bf93ceSAlex Bennée #define DEBUG_GIC_GATE 0
36e69954b9Spbrook #endif
37e69954b9Spbrook 
3868bf93ceSAlex Bennée #define DPRINTF(fmt, ...) do {                                          \
3968bf93ceSAlex Bennée         if (DEBUG_GIC_GATE) {                                           \
4068bf93ceSAlex Bennée             fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__);      \
4168bf93ceSAlex Bennée         }                                                               \
4268bf93ceSAlex Bennée     } while (0)
4368bf93ceSAlex Bennée 
443355c360SAlistair Francis static const uint8_t gic_id_11mpcore[] = {
453355c360SAlistair Francis     0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
463355c360SAlistair Francis };
473355c360SAlistair Francis 
483355c360SAlistair Francis static const uint8_t gic_id_gicv1[] = {
493355c360SAlistair Francis     0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
503355c360SAlistair Francis };
513355c360SAlistair Francis 
523355c360SAlistair Francis static const uint8_t gic_id_gicv2[] = {
533355c360SAlistair Francis     0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
542a29ddeeSPeter Maydell };
552a29ddeeSPeter Maydell 
56fae15286SPeter Maydell static inline int gic_get_current_cpu(GICState *s)
57926c4affSPeter Maydell {
58926c4affSPeter Maydell     if (s->num_cpu > 1) {
594917cf44SAndreas Färber         return current_cpu->cpu_index;
60926c4affSPeter Maydell     }
61926c4affSPeter Maydell     return 0;
62926c4affSPeter Maydell }
63926c4affSPeter Maydell 
644a37e0e4SLuc Michel static inline int gic_get_current_vcpu(GICState *s)
654a37e0e4SLuc Michel {
664a37e0e4SLuc Michel     return gic_get_current_cpu(s) + GIC_NCPU;
674a37e0e4SLuc Michel }
684a37e0e4SLuc Michel 
69c27a5ba9SFabian Aggeler /* Return true if this GIC config has interrupt groups, which is
70c27a5ba9SFabian Aggeler  * true if we're a GICv2, or a GICv1 with the security extensions.
71c27a5ba9SFabian Aggeler  */
72c27a5ba9SFabian Aggeler static inline bool gic_has_groups(GICState *s)
73c27a5ba9SFabian Aggeler {
74c27a5ba9SFabian Aggeler     return s->revision == 2 || s->security_extn;
75c27a5ba9SFabian Aggeler }
76c27a5ba9SFabian Aggeler 
773dd0471bSLuc Michel static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs)
783dd0471bSLuc Michel {
793dd0471bSLuc Michel     return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure;
803dd0471bSLuc Michel }
813dd0471bSLuc Michel 
82cbe1282bSLuc Michel static inline void gic_get_best_irq(GICState *s, int cpu,
83cbe1282bSLuc Michel                                     int *best_irq, int *best_prio, int *group)
84cbe1282bSLuc Michel {
85cbe1282bSLuc Michel     int irq;
86cbe1282bSLuc Michel     int cm = 1 << cpu;
87cbe1282bSLuc Michel 
88cbe1282bSLuc Michel     *best_irq = 1023;
89cbe1282bSLuc Michel     *best_prio = 0x100;
90cbe1282bSLuc Michel 
91cbe1282bSLuc Michel     for (irq = 0; irq < s->num_irq; irq++) {
92cbe1282bSLuc Michel         if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
93cbe1282bSLuc Michel             (!GIC_DIST_TEST_ACTIVE(irq, cm)) &&
94cbe1282bSLuc Michel             (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) {
95cbe1282bSLuc Michel             if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) {
96cbe1282bSLuc Michel                 *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu);
97cbe1282bSLuc Michel                 *best_irq = irq;
98cbe1282bSLuc Michel             }
99cbe1282bSLuc Michel         }
100cbe1282bSLuc Michel     }
101cbe1282bSLuc Michel 
102cbe1282bSLuc Michel     if (*best_irq < 1023) {
103cbe1282bSLuc Michel         *group = GIC_DIST_TEST_GROUP(*best_irq, cm);
104cbe1282bSLuc Michel     }
105cbe1282bSLuc Michel }
106cbe1282bSLuc Michel 
107cbe1282bSLuc Michel static inline void gic_get_best_virq(GICState *s, int cpu,
108cbe1282bSLuc Michel                                      int *best_irq, int *best_prio, int *group)
109cbe1282bSLuc Michel {
110cbe1282bSLuc Michel     int lr_idx = 0;
111cbe1282bSLuc Michel 
112cbe1282bSLuc Michel     *best_irq = 1023;
113cbe1282bSLuc Michel     *best_prio = 0x100;
114cbe1282bSLuc Michel 
115cbe1282bSLuc Michel     for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
116cbe1282bSLuc Michel         uint32_t lr_entry = s->h_lr[lr_idx][cpu];
117cbe1282bSLuc Michel         int state = GICH_LR_STATE(lr_entry);
118cbe1282bSLuc Michel 
119cbe1282bSLuc Michel         if (state == GICH_LR_STATE_PENDING) {
120cbe1282bSLuc Michel             int prio = GICH_LR_PRIORITY(lr_entry);
121cbe1282bSLuc Michel 
122cbe1282bSLuc Michel             if (prio < *best_prio) {
123cbe1282bSLuc Michel                 *best_prio = prio;
124cbe1282bSLuc Michel                 *best_irq = GICH_LR_VIRT_ID(lr_entry);
125cbe1282bSLuc Michel                 *group = GICH_LR_GROUP(lr_entry);
126cbe1282bSLuc Michel             }
127cbe1282bSLuc Michel         }
128cbe1282bSLuc Michel     }
129cbe1282bSLuc Michel }
130cbe1282bSLuc Michel 
131cbe1282bSLuc Michel /* Return true if IRQ signaling is enabled for the given cpu and at least one
132cbe1282bSLuc Michel  * of the given groups:
133cbe1282bSLuc Michel  *   - in the non-virt case, the distributor must be enabled for one of the
134cbe1282bSLuc Michel  *   given groups
135cbe1282bSLuc Michel  *   - in the virt case, the virtual interface must be enabled.
136cbe1282bSLuc Michel  *   - in all cases, the (v)CPU interface must be enabled for one of the given
137cbe1282bSLuc Michel  *   groups.
138cbe1282bSLuc Michel  */
139cbe1282bSLuc Michel static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt,
140cbe1282bSLuc Michel                                     int group_mask)
141cbe1282bSLuc Michel {
142cbe1282bSLuc Michel     if (!virt && !(s->ctlr & group_mask)) {
143cbe1282bSLuc Michel         return false;
144cbe1282bSLuc Michel     }
145cbe1282bSLuc Michel 
146cbe1282bSLuc Michel     if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) {
147cbe1282bSLuc Michel         return false;
148cbe1282bSLuc Michel     }
149cbe1282bSLuc Michel 
150cbe1282bSLuc Michel     if (!(s->cpu_ctlr[cpu] & group_mask)) {
151cbe1282bSLuc Michel         return false;
152cbe1282bSLuc Michel     }
153cbe1282bSLuc Michel 
154cbe1282bSLuc Michel     return true;
155cbe1282bSLuc Michel }
156cbe1282bSLuc Michel 
157e69954b9Spbrook /* TODO: Many places that call this routine could be optimized.  */
158e69954b9Spbrook /* Update interrupt status after enabled or pending bits have been changed.  */
159cbe1282bSLuc Michel static inline void gic_update_internal(GICState *s, bool virt)
160e69954b9Spbrook {
161e69954b9Spbrook     int best_irq;
162e69954b9Spbrook     int best_prio;
163dadbb58fSPeter Maydell     int irq_level, fiq_level;
164cbe1282bSLuc Michel     int cpu, cpu_iface;
165cbe1282bSLuc Michel     int group = 0;
166cbe1282bSLuc Michel     qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq;
167cbe1282bSLuc Michel     qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq;
168e69954b9Spbrook 
169b95690c9SWei Huang     for (cpu = 0; cpu < s->num_cpu; cpu++) {
170cbe1282bSLuc Michel         cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
171cbe1282bSLuc Michel 
172cbe1282bSLuc Michel         s->current_pending[cpu_iface] = 1023;
173cbe1282bSLuc Michel         if (!gic_irq_signaling_enabled(s, cpu, virt,
174cbe1282bSLuc Michel                                        GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) {
175cbe1282bSLuc Michel             qemu_irq_lower(irq_lines[cpu]);
176cbe1282bSLuc Michel             qemu_irq_lower(fiq_lines[cpu]);
177235069a3SJohan Karlsson             continue;
178e69954b9Spbrook         }
179cbe1282bSLuc Michel 
180cbe1282bSLuc Michel         if (virt) {
181cbe1282bSLuc Michel             gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group);
182cbe1282bSLuc Michel         } else {
183cbe1282bSLuc Michel             gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group);
184e69954b9Spbrook         }
185dadbb58fSPeter Maydell 
1862531088fSHollis Blanchard         if (best_irq != 1023) {
1872531088fSHollis Blanchard             trace_gic_update_bestirq(cpu, best_irq, best_prio,
188cbe1282bSLuc Michel                 s->priority_mask[cpu_iface], s->running_priority[cpu_iface]);
1892531088fSHollis Blanchard         }
1902531088fSHollis Blanchard 
191dadbb58fSPeter Maydell         irq_level = fiq_level = 0;
192dadbb58fSPeter Maydell 
193cbe1282bSLuc Michel         if (best_prio < s->priority_mask[cpu_iface]) {
194cbe1282bSLuc Michel             s->current_pending[cpu_iface] = best_irq;
195cbe1282bSLuc Michel             if (best_prio < s->running_priority[cpu_iface]) {
196cbe1282bSLuc Michel                 if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) {
197cbe1282bSLuc Michel                     if (group == 0 &&
198cbe1282bSLuc Michel                         s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) {
199dadbb58fSPeter Maydell                         DPRINTF("Raised pending FIQ %d (cpu %d)\n",
200cbe1282bSLuc Michel                                 best_irq, cpu_iface);
201dadbb58fSPeter Maydell                         fiq_level = 1;
202cbe1282bSLuc Michel                         trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq",
203cbe1282bSLuc Michel                                                  fiq_level);
204dadbb58fSPeter Maydell                     } else {
205dadbb58fSPeter Maydell                         DPRINTF("Raised pending IRQ %d (cpu %d)\n",
206cbe1282bSLuc Michel                                 best_irq, cpu_iface);
207dadbb58fSPeter Maydell                         irq_level = 1;
208cbe1282bSLuc Michel                         trace_gic_update_set_irq(cpu, virt ? "virq" : "irq",
209cbe1282bSLuc Michel                                                  irq_level);
210e69954b9Spbrook                     }
211e69954b9Spbrook                 }
212dadbb58fSPeter Maydell             }
213dadbb58fSPeter Maydell         }
214dadbb58fSPeter Maydell 
215cbe1282bSLuc Michel         qemu_set_irq(irq_lines[cpu], irq_level);
216cbe1282bSLuc Michel         qemu_set_irq(fiq_lines[cpu], fiq_level);
2179ee6e8bbSpbrook     }
218e69954b9Spbrook }
219e69954b9Spbrook 
220cbe1282bSLuc Michel static void gic_update(GICState *s)
221cbe1282bSLuc Michel {
222cbe1282bSLuc Michel     gic_update_internal(s, false);
223cbe1282bSLuc Michel }
224cbe1282bSLuc Michel 
225527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit
226527d296fSLuc Michel  * in ELRSR is set.
227527d296fSLuc Michel  */
228527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry)
229527d296fSLuc Michel {
230527d296fSLuc Michel     return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
231527d296fSLuc Michel         && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry));
232527d296fSLuc Michel }
233527d296fSLuc Michel 
234527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the
235527d296fSLuc Michel  * corrsponding bit in EISR is set.
236527d296fSLuc Michel  */
237527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry)
238527d296fSLuc Michel {
239527d296fSLuc Michel     return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
240527d296fSLuc Michel         && !GICH_LR_HW(entry) && GICH_LR_EOI(entry);
241527d296fSLuc Michel }
242527d296fSLuc Michel 
243*50e57926SLuc Michel static inline void gic_extract_lr_info(GICState *s, int cpu,
244*50e57926SLuc Michel                                 int *num_eoi, int *num_valid, int *num_pending)
245*50e57926SLuc Michel {
246*50e57926SLuc Michel     int lr_idx;
247*50e57926SLuc Michel 
248*50e57926SLuc Michel     *num_eoi = 0;
249*50e57926SLuc Michel     *num_valid = 0;
250*50e57926SLuc Michel     *num_pending = 0;
251*50e57926SLuc Michel 
252*50e57926SLuc Michel     for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
253*50e57926SLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
254*50e57926SLuc Michel 
255*50e57926SLuc Michel         if (gic_lr_entry_is_eoi(*entry)) {
256*50e57926SLuc Michel             (*num_eoi)++;
257*50e57926SLuc Michel         }
258*50e57926SLuc Michel 
259*50e57926SLuc Michel         if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) {
260*50e57926SLuc Michel             (*num_valid)++;
261*50e57926SLuc Michel         }
262*50e57926SLuc Michel 
263*50e57926SLuc Michel         if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) {
264*50e57926SLuc Michel             (*num_pending)++;
265*50e57926SLuc Michel         }
266*50e57926SLuc Michel     }
267*50e57926SLuc Michel }
268*50e57926SLuc Michel 
269*50e57926SLuc Michel static void gic_compute_misr(GICState *s, int cpu)
270*50e57926SLuc Michel {
271*50e57926SLuc Michel     uint32_t value = 0;
272*50e57926SLuc Michel     int vcpu = cpu + GIC_NCPU;
273*50e57926SLuc Michel 
274*50e57926SLuc Michel     int num_eoi, num_valid, num_pending;
275*50e57926SLuc Michel 
276*50e57926SLuc Michel     gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending);
277*50e57926SLuc Michel 
278*50e57926SLuc Michel     /* EOI */
279*50e57926SLuc Michel     if (num_eoi) {
280*50e57926SLuc Michel         value |= R_GICH_MISR_EOI_MASK;
281*50e57926SLuc Michel     }
282*50e57926SLuc Michel 
283*50e57926SLuc Michel     /* U: true if only 0 or 1 LR entry is valid */
284*50e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) {
285*50e57926SLuc Michel         value |= R_GICH_MISR_U_MASK;
286*50e57926SLuc Michel     }
287*50e57926SLuc Michel 
288*50e57926SLuc Michel     /* LRENP: EOICount is not 0 */
289*50e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) &&
290*50e57926SLuc Michel         ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) {
291*50e57926SLuc Michel         value |= R_GICH_MISR_LRENP_MASK;
292*50e57926SLuc Michel     }
293*50e57926SLuc Michel 
294*50e57926SLuc Michel     /* NP: no pending interrupts */
295*50e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) {
296*50e57926SLuc Michel         value |= R_GICH_MISR_NP_MASK;
297*50e57926SLuc Michel     }
298*50e57926SLuc Michel 
299*50e57926SLuc Michel     /* VGrp0E: group0 virq signaling enabled */
300*50e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) &&
301*50e57926SLuc Michel         (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
302*50e57926SLuc Michel         value |= R_GICH_MISR_VGrp0E_MASK;
303*50e57926SLuc Michel     }
304*50e57926SLuc Michel 
305*50e57926SLuc Michel     /* VGrp0D: group0 virq signaling disabled */
306*50e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) &&
307*50e57926SLuc Michel         !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
308*50e57926SLuc Michel         value |= R_GICH_MISR_VGrp0D_MASK;
309*50e57926SLuc Michel     }
310*50e57926SLuc Michel 
311*50e57926SLuc Michel     /* VGrp1E: group1 virq signaling enabled */
312*50e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) &&
313*50e57926SLuc Michel         (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
314*50e57926SLuc Michel         value |= R_GICH_MISR_VGrp1E_MASK;
315*50e57926SLuc Michel     }
316*50e57926SLuc Michel 
317*50e57926SLuc Michel     /* VGrp1D: group1 virq signaling disabled */
318*50e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) &&
319*50e57926SLuc Michel         !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
320*50e57926SLuc Michel         value |= R_GICH_MISR_VGrp1D_MASK;
321*50e57926SLuc Michel     }
322*50e57926SLuc Michel 
323*50e57926SLuc Michel     s->h_misr[cpu] = value;
324*50e57926SLuc Michel }
325*50e57926SLuc Michel 
326*50e57926SLuc Michel static void gic_update_maintenance(GICState *s)
327*50e57926SLuc Michel {
328*50e57926SLuc Michel     int cpu = 0;
329*50e57926SLuc Michel     int maint_level;
330*50e57926SLuc Michel 
331*50e57926SLuc Michel     for (cpu = 0; cpu < s->num_cpu; cpu++) {
332*50e57926SLuc Michel         gic_compute_misr(s, cpu);
333*50e57926SLuc Michel         maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu];
334*50e57926SLuc Michel 
335*50e57926SLuc Michel         qemu_set_irq(s->maintenance_irq[cpu], maint_level);
336*50e57926SLuc Michel     }
337*50e57926SLuc Michel }
338*50e57926SLuc Michel 
339cbe1282bSLuc Michel static void gic_update_virt(GICState *s)
340cbe1282bSLuc Michel {
341cbe1282bSLuc Michel     gic_update_internal(s, true);
342*50e57926SLuc Michel     gic_update_maintenance(s);
343cbe1282bSLuc Michel }
344cbe1282bSLuc Michel 
3458d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
3468d999995SChristoffer Dall                                  int cm, int target)
3478d999995SChristoffer Dall {
3488d999995SChristoffer Dall     if (level) {
34967ce697aSLuc Michel         GIC_DIST_SET_LEVEL(irq, cm);
35067ce697aSLuc Michel         if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) {
3518d999995SChristoffer Dall             DPRINTF("Set %d pending mask %x\n", irq, target);
35267ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, target);
3538d999995SChristoffer Dall         }
3548d999995SChristoffer Dall     } else {
35567ce697aSLuc Michel         GIC_DIST_CLEAR_LEVEL(irq, cm);
3568d999995SChristoffer Dall     }
3578d999995SChristoffer Dall }
3588d999995SChristoffer Dall 
3598d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level,
3608d999995SChristoffer Dall                                 int cm, int target)
3618d999995SChristoffer Dall {
3628d999995SChristoffer Dall     if (level) {
36367ce697aSLuc Michel         GIC_DIST_SET_LEVEL(irq, cm);
3648d999995SChristoffer Dall         DPRINTF("Set %d pending mask %x\n", irq, target);
36567ce697aSLuc Michel         if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) {
36667ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, target);
3678d999995SChristoffer Dall         }
3688d999995SChristoffer Dall     } else {
36967ce697aSLuc Michel         GIC_DIST_CLEAR_LEVEL(irq, cm);
3708d999995SChristoffer Dall     }
3718d999995SChristoffer Dall }
3728d999995SChristoffer Dall 
3739ee6e8bbSpbrook /* Process a change in an external IRQ input.  */
374e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level)
375e69954b9Spbrook {
376544d1afaSPeter Maydell     /* Meaning of the 'irq' parameter:
377544d1afaSPeter Maydell      *  [0..N-1] : external interrupts
378544d1afaSPeter Maydell      *  [N..N+31] : PPI (internal) interrupts for CPU 0
379544d1afaSPeter Maydell      *  [N+32..N+63] : PPI (internal interrupts for CPU 1
380544d1afaSPeter Maydell      *  ...
381544d1afaSPeter Maydell      */
382fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
383544d1afaSPeter Maydell     int cm, target;
384544d1afaSPeter Maydell     if (irq < (s->num_irq - GIC_INTERNAL)) {
385e69954b9Spbrook         /* The first external input line is internal interrupt 32.  */
386544d1afaSPeter Maydell         cm = ALL_CPU_MASK;
38769253800SRusty Russell         irq += GIC_INTERNAL;
38867ce697aSLuc Michel         target = GIC_DIST_TARGET(irq);
389544d1afaSPeter Maydell     } else {
390544d1afaSPeter Maydell         int cpu;
391544d1afaSPeter Maydell         irq -= (s->num_irq - GIC_INTERNAL);
392544d1afaSPeter Maydell         cpu = irq / GIC_INTERNAL;
393544d1afaSPeter Maydell         irq %= GIC_INTERNAL;
394544d1afaSPeter Maydell         cm = 1 << cpu;
395544d1afaSPeter Maydell         target = cm;
396544d1afaSPeter Maydell     }
397544d1afaSPeter Maydell 
39840d22500SChristoffer Dall     assert(irq >= GIC_NR_SGIS);
39940d22500SChristoffer Dall 
40067ce697aSLuc Michel     if (level == GIC_DIST_TEST_LEVEL(irq, cm)) {
401e69954b9Spbrook         return;
402544d1afaSPeter Maydell     }
403e69954b9Spbrook 
4043bc4b52cSMarcin Krzeminski     if (s->revision == REV_11MPCORE) {
4058d999995SChristoffer Dall         gic_set_irq_11mpcore(s, irq, level, cm, target);
406e69954b9Spbrook     } else {
4078d999995SChristoffer Dall         gic_set_irq_generic(s, irq, level, cm, target);
408e69954b9Spbrook     }
4092531088fSHollis Blanchard     trace_gic_set_irq(irq, level, cm, target);
4108d999995SChristoffer Dall 
411e69954b9Spbrook     gic_update(s);
412e69954b9Spbrook }
413e69954b9Spbrook 
4147c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
4157c0fa108SFabian Aggeler                                             MemTxAttrs attrs)
4167c0fa108SFabian Aggeler {
4177c0fa108SFabian Aggeler     uint16_t pending_irq = s->current_pending[cpu];
4187c0fa108SFabian Aggeler 
4197c0fa108SFabian Aggeler     if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
42086b350f0SLuc Michel         int group = gic_test_group(s, pending_irq, cpu);
42186b350f0SLuc Michel 
4227c0fa108SFabian Aggeler         /* On a GIC without the security extensions, reading this register
4237c0fa108SFabian Aggeler          * behaves in the same way as a secure access to a GIC with them.
4247c0fa108SFabian Aggeler          */
4253dd0471bSLuc Michel         bool secure = !gic_cpu_ns_access(s, cpu, attrs);
4267c0fa108SFabian Aggeler 
4277c0fa108SFabian Aggeler         if (group == 0 && !secure) {
4287c0fa108SFabian Aggeler             /* Group0 interrupts hidden from Non-secure access */
4297c0fa108SFabian Aggeler             return 1023;
4307c0fa108SFabian Aggeler         }
4317c0fa108SFabian Aggeler         if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
4327c0fa108SFabian Aggeler             /* Group1 interrupts only seen by Secure access if
4337c0fa108SFabian Aggeler              * AckCtl bit set.
4347c0fa108SFabian Aggeler              */
4357c0fa108SFabian Aggeler             return 1022;
4367c0fa108SFabian Aggeler         }
4377c0fa108SFabian Aggeler     }
4387c0fa108SFabian Aggeler     return pending_irq;
4397c0fa108SFabian Aggeler }
4407c0fa108SFabian Aggeler 
441df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq)
442df92cfa6SPeter Maydell {
443df92cfa6SPeter Maydell     /* Return the group priority of the specified interrupt
444df92cfa6SPeter Maydell      * (which is the top bits of its priority, with the number
445df92cfa6SPeter Maydell      * of bits masked determined by the applicable binary point register).
446df92cfa6SPeter Maydell      */
447df92cfa6SPeter Maydell     int bpr;
448df92cfa6SPeter Maydell     uint32_t mask;
449df92cfa6SPeter Maydell 
450df92cfa6SPeter Maydell     if (gic_has_groups(s) &&
451df92cfa6SPeter Maydell         !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
45286b350f0SLuc Michel         gic_test_group(s, irq, cpu)) {
453fc05a6f2SLuc MICHEL         bpr = s->abpr[cpu] - 1;
454fc05a6f2SLuc MICHEL         assert(bpr >= 0);
455df92cfa6SPeter Maydell     } else {
456df92cfa6SPeter Maydell         bpr = s->bpr[cpu];
457df92cfa6SPeter Maydell     }
458df92cfa6SPeter Maydell 
459df92cfa6SPeter Maydell     /* a BPR of 0 means the group priority bits are [7:1];
460df92cfa6SPeter Maydell      * a BPR of 1 means they are [7:2], and so on down to
461df92cfa6SPeter Maydell      * a BPR of 7 meaning no group priority bits at all.
462df92cfa6SPeter Maydell      */
463df92cfa6SPeter Maydell     mask = ~0U << ((bpr & 7) + 1);
464df92cfa6SPeter Maydell 
46586b350f0SLuc Michel     return gic_get_priority(s, irq, cpu) & mask;
466df92cfa6SPeter Maydell }
467df92cfa6SPeter Maydell 
46872889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq)
469e69954b9Spbrook {
47072889c8aSPeter Maydell     /* Set the appropriate Active Priority Register bit for this IRQ,
47172889c8aSPeter Maydell      * and update the running priority.
47272889c8aSPeter Maydell      */
47372889c8aSPeter Maydell     int prio = gic_get_group_priority(s, cpu, irq);
474a1d7b8d8SLuc Michel     int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
475a1d7b8d8SLuc Michel     int preemption_level = prio >> (min_bpr + 1);
47672889c8aSPeter Maydell     int regno = preemption_level / 32;
47772889c8aSPeter Maydell     int bitno = preemption_level % 32;
478a1d7b8d8SLuc Michel     uint32_t *papr = NULL;
47972889c8aSPeter Maydell 
480a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
481a1d7b8d8SLuc Michel         assert(regno == 0);
482a1d7b8d8SLuc Michel         papr = &s->h_apr[gic_get_vcpu_real_id(cpu)];
483a1d7b8d8SLuc Michel     } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) {
484a1d7b8d8SLuc Michel         papr = &s->nsapr[regno][cpu];
4859ee6e8bbSpbrook     } else {
486a1d7b8d8SLuc Michel         papr = &s->apr[regno][cpu];
4879ee6e8bbSpbrook     }
48872889c8aSPeter Maydell 
489a1d7b8d8SLuc Michel     *papr |= (1 << bitno);
490a1d7b8d8SLuc Michel 
49172889c8aSPeter Maydell     s->running_priority[cpu] = prio;
49286b350f0SLuc Michel     gic_set_active(s, irq, cpu);
49372889c8aSPeter Maydell }
49472889c8aSPeter Maydell 
49572889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
49672889c8aSPeter Maydell {
49772889c8aSPeter Maydell     /* Recalculate the current running priority for this CPU based
49872889c8aSPeter Maydell      * on the set bits in the Active Priority Registers.
49972889c8aSPeter Maydell      */
50072889c8aSPeter Maydell     int i;
501a1d7b8d8SLuc Michel 
502a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
503a1d7b8d8SLuc Michel         uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)];
504a1d7b8d8SLuc Michel         if (apr) {
505a1d7b8d8SLuc Michel             return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1);
506a1d7b8d8SLuc Michel         } else {
507a1d7b8d8SLuc Michel             return 0x100;
508a1d7b8d8SLuc Michel         }
509a1d7b8d8SLuc Michel     }
510a1d7b8d8SLuc Michel 
51172889c8aSPeter Maydell     for (i = 0; i < GIC_NR_APRS; i++) {
51272889c8aSPeter Maydell         uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
51372889c8aSPeter Maydell         if (!apr) {
51472889c8aSPeter Maydell             continue;
51572889c8aSPeter Maydell         }
51672889c8aSPeter Maydell         return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
51772889c8aSPeter Maydell     }
51872889c8aSPeter Maydell     return 0x100;
51972889c8aSPeter Maydell }
52072889c8aSPeter Maydell 
52172889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group)
52272889c8aSPeter Maydell {
52372889c8aSPeter Maydell     /* Drop the priority of the currently active interrupt in the
52472889c8aSPeter Maydell      * specified group.
52572889c8aSPeter Maydell      *
52672889c8aSPeter Maydell      * Note that we can guarantee (because of the requirement to nest
52772889c8aSPeter Maydell      * GICC_IAR reads [which activate an interrupt and raise priority]
52872889c8aSPeter Maydell      * with GICC_EOIR writes [which drop the priority for the interrupt])
52972889c8aSPeter Maydell      * that the interrupt we're being called for is the highest priority
53072889c8aSPeter Maydell      * active interrupt, meaning that it has the lowest set bit in the
53172889c8aSPeter Maydell      * APR registers.
53272889c8aSPeter Maydell      *
53372889c8aSPeter Maydell      * If the guest does not honour the ordering constraints then the
53472889c8aSPeter Maydell      * behaviour of the GIC is UNPREDICTABLE, which for us means that
53572889c8aSPeter Maydell      * the values of the APR registers might become incorrect and the
53672889c8aSPeter Maydell      * running priority will be wrong, so interrupts that should preempt
53772889c8aSPeter Maydell      * might not do so, and interrupts that should not preempt might do so.
53872889c8aSPeter Maydell      */
539a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
540a1d7b8d8SLuc Michel         int rcpu = gic_get_vcpu_real_id(cpu);
541a1d7b8d8SLuc Michel 
542a1d7b8d8SLuc Michel         if (s->h_apr[rcpu]) {
543a1d7b8d8SLuc Michel             /* Clear lowest set bit */
544a1d7b8d8SLuc Michel             s->h_apr[rcpu] &= s->h_apr[rcpu] - 1;
545a1d7b8d8SLuc Michel         }
546a1d7b8d8SLuc Michel     } else {
54772889c8aSPeter Maydell         int i;
54872889c8aSPeter Maydell 
54972889c8aSPeter Maydell         for (i = 0; i < GIC_NR_APRS; i++) {
55072889c8aSPeter Maydell             uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
55172889c8aSPeter Maydell             if (!*papr) {
55272889c8aSPeter Maydell                 continue;
55372889c8aSPeter Maydell             }
55472889c8aSPeter Maydell             /* Clear lowest set bit */
55572889c8aSPeter Maydell             *papr &= *papr - 1;
55672889c8aSPeter Maydell             break;
55772889c8aSPeter Maydell         }
558a1d7b8d8SLuc Michel     }
55972889c8aSPeter Maydell 
56072889c8aSPeter Maydell     s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
561e69954b9Spbrook }
562e69954b9Spbrook 
563439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu)
564e69954b9Spbrook {
565439badd6SLuc Michel     int src;
566439badd6SLuc Michel     uint32_t ret;
567c5619bf9SFabian Aggeler 
568439badd6SLuc Michel     if (!gic_is_vcpu(cpu)) {
56940d22500SChristoffer Dall         /* Lookup the source CPU for the SGI and clear this in the
57040d22500SChristoffer Dall          * sgi_pending map.  Return the src and clear the overall pending
57140d22500SChristoffer Dall          * state on this CPU if the SGI is not pending from any CPUs.
57240d22500SChristoffer Dall          */
57340d22500SChristoffer Dall         assert(s->sgi_pending[irq][cpu] != 0);
57440d22500SChristoffer Dall         src = ctz32(s->sgi_pending[irq][cpu]);
57540d22500SChristoffer Dall         s->sgi_pending[irq][cpu] &= ~(1 << src);
57640d22500SChristoffer Dall         if (s->sgi_pending[irq][cpu] == 0) {
57786b350f0SLuc Michel             gic_clear_pending(s, irq, cpu);
57840d22500SChristoffer Dall         }
57940d22500SChristoffer Dall         ret = irq | ((src & 0x7) << 10);
58040d22500SChristoffer Dall     } else {
581439badd6SLuc Michel         uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu);
582439badd6SLuc Michel         src = GICH_LR_CPUID(*lr_entry);
583439badd6SLuc Michel 
584439badd6SLuc Michel         gic_clear_pending(s, irq, cpu);
585439badd6SLuc Michel         ret = irq | (src << 10);
586439badd6SLuc Michel     }
587439badd6SLuc Michel 
588439badd6SLuc Michel     return ret;
589439badd6SLuc Michel }
590439badd6SLuc Michel 
591439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
592439badd6SLuc Michel {
593439badd6SLuc Michel     int ret, irq;
594439badd6SLuc Michel 
595439badd6SLuc Michel     /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
596439badd6SLuc Michel      * for the case where this GIC supports grouping and the pending interrupt
597439badd6SLuc Michel      * is in the wrong group.
59840d22500SChristoffer Dall      */
599439badd6SLuc Michel     irq = gic_get_current_pending_irq(s, cpu, attrs);
600439badd6SLuc Michel     trace_gic_acknowledge_irq(gic_get_vcpu_real_id(cpu), irq);
601439badd6SLuc Michel 
602439badd6SLuc Michel     if (irq >= GIC_MAXIRQ) {
603439badd6SLuc Michel         DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
604439badd6SLuc Michel         return irq;
605439badd6SLuc Michel     }
606439badd6SLuc Michel 
607439badd6SLuc Michel     if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) {
608439badd6SLuc Michel         DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
609439badd6SLuc Michel         return 1023;
610439badd6SLuc Michel     }
611439badd6SLuc Michel 
612439badd6SLuc Michel     gic_activate_irq(s, cpu, irq);
613439badd6SLuc Michel 
614439badd6SLuc Michel     if (s->revision == REV_11MPCORE) {
615439badd6SLuc Michel         /* Clear pending flags for both level and edge triggered interrupts.
616439badd6SLuc Michel          * Level triggered IRQs will be reasserted once they become inactive.
617439badd6SLuc Michel          */
618439badd6SLuc Michel         gic_clear_pending(s, irq, cpu);
619439badd6SLuc Michel         ret = irq;
620439badd6SLuc Michel     } else {
621439badd6SLuc Michel         if (irq < GIC_NR_SGIS) {
622439badd6SLuc Michel             ret = gic_clear_pending_sgi(s, irq, cpu);
623439badd6SLuc Michel         } else {
62486b350f0SLuc Michel             gic_clear_pending(s, irq, cpu);
62540d22500SChristoffer Dall             ret = irq;
62640d22500SChristoffer Dall         }
62740d22500SChristoffer Dall     }
62840d22500SChristoffer Dall 
629cbe1282bSLuc Michel     if (gic_is_vcpu(cpu)) {
630cbe1282bSLuc Michel         gic_update_virt(s);
631cbe1282bSLuc Michel     } else {
63272889c8aSPeter Maydell         gic_update(s);
633cbe1282bSLuc Michel     }
63440d22500SChristoffer Dall     DPRINTF("ACK %d\n", irq);
63540d22500SChristoffer Dall     return ret;
636e69954b9Spbrook }
637e69954b9Spbrook 
63867ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
63981508470SFabian Aggeler                       MemTxAttrs attrs)
6409df90ad0SChristoffer Dall {
64181508470SFabian Aggeler     if (s->security_extn && !attrs.secure) {
64267ce697aSLuc Michel         if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
64381508470SFabian Aggeler             return; /* Ignore Non-secure access of Group0 IRQ */
64481508470SFabian Aggeler         }
64581508470SFabian Aggeler         val = 0x80 | (val >> 1); /* Non-secure view */
64681508470SFabian Aggeler     }
64781508470SFabian Aggeler 
6489df90ad0SChristoffer Dall     if (irq < GIC_INTERNAL) {
6499df90ad0SChristoffer Dall         s->priority1[irq][cpu] = val;
6509df90ad0SChristoffer Dall     } else {
6519df90ad0SChristoffer Dall         s->priority2[(irq) - GIC_INTERNAL] = val;
6529df90ad0SChristoffer Dall     }
6539df90ad0SChristoffer Dall }
6549df90ad0SChristoffer Dall 
65567ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
65681508470SFabian Aggeler                                  MemTxAttrs attrs)
65781508470SFabian Aggeler {
65867ce697aSLuc Michel     uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
65981508470SFabian Aggeler 
66081508470SFabian Aggeler     if (s->security_extn && !attrs.secure) {
66167ce697aSLuc Michel         if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
66281508470SFabian Aggeler             return 0; /* Non-secure access cannot read priority of Group0 IRQ */
66381508470SFabian Aggeler         }
66481508470SFabian Aggeler         prio = (prio << 1) & 0xff; /* Non-secure view */
66581508470SFabian Aggeler     }
66681508470SFabian Aggeler     return prio;
66781508470SFabian Aggeler }
66881508470SFabian Aggeler 
66981508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
67081508470SFabian Aggeler                                   MemTxAttrs attrs)
67181508470SFabian Aggeler {
6723dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
67381508470SFabian Aggeler         if (s->priority_mask[cpu] & 0x80) {
67481508470SFabian Aggeler             /* Priority Mask in upper half */
67581508470SFabian Aggeler             pmask = 0x80 | (pmask >> 1);
67681508470SFabian Aggeler         } else {
67781508470SFabian Aggeler             /* Non-secure write ignored if priority mask is in lower half */
67881508470SFabian Aggeler             return;
67981508470SFabian Aggeler         }
68081508470SFabian Aggeler     }
68181508470SFabian Aggeler     s->priority_mask[cpu] = pmask;
68281508470SFabian Aggeler }
68381508470SFabian Aggeler 
68481508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
68581508470SFabian Aggeler {
68681508470SFabian Aggeler     uint32_t pmask = s->priority_mask[cpu];
68781508470SFabian Aggeler 
6883dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
68981508470SFabian Aggeler         if (pmask & 0x80) {
69081508470SFabian Aggeler             /* Priority Mask in upper half, return Non-secure view */
69181508470SFabian Aggeler             pmask = (pmask << 1) & 0xff;
69281508470SFabian Aggeler         } else {
69381508470SFabian Aggeler             /* Priority Mask in lower half, RAZ */
69481508470SFabian Aggeler             pmask = 0;
69581508470SFabian Aggeler         }
69681508470SFabian Aggeler     }
69781508470SFabian Aggeler     return pmask;
69881508470SFabian Aggeler }
69981508470SFabian Aggeler 
70032951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
70132951860SFabian Aggeler {
70232951860SFabian Aggeler     uint32_t ret = s->cpu_ctlr[cpu];
70332951860SFabian Aggeler 
7043dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
70532951860SFabian Aggeler         /* Construct the NS banked view of GICC_CTLR from the correct
70632951860SFabian Aggeler          * bits of the S banked view. We don't need to move the bypass
70732951860SFabian Aggeler          * control bits because we don't implement that (IMPDEF) part
70832951860SFabian Aggeler          * of the GIC architecture.
70932951860SFabian Aggeler          */
71032951860SFabian Aggeler         ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
71132951860SFabian Aggeler     }
71232951860SFabian Aggeler     return ret;
71332951860SFabian Aggeler }
71432951860SFabian Aggeler 
71532951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
71632951860SFabian Aggeler                                 MemTxAttrs attrs)
71732951860SFabian Aggeler {
71832951860SFabian Aggeler     uint32_t mask;
71932951860SFabian Aggeler 
7203dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
72132951860SFabian Aggeler         /* The NS view can only write certain bits in the register;
72232951860SFabian Aggeler          * the rest are unchanged
72332951860SFabian Aggeler          */
72432951860SFabian Aggeler         mask = GICC_CTLR_EN_GRP1;
72532951860SFabian Aggeler         if (s->revision == 2) {
72632951860SFabian Aggeler             mask |= GICC_CTLR_EOIMODE_NS;
72732951860SFabian Aggeler         }
72832951860SFabian Aggeler         s->cpu_ctlr[cpu] &= ~mask;
72932951860SFabian Aggeler         s->cpu_ctlr[cpu] |= (value << 1) & mask;
73032951860SFabian Aggeler     } else {
73132951860SFabian Aggeler         if (s->revision == 2) {
73232951860SFabian Aggeler             mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
73332951860SFabian Aggeler         } else {
73432951860SFabian Aggeler             mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
73532951860SFabian Aggeler         }
73632951860SFabian Aggeler         s->cpu_ctlr[cpu] = value & mask;
73732951860SFabian Aggeler     }
73832951860SFabian Aggeler     DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
73932951860SFabian Aggeler             "Group1 Interrupts %sabled\n", cpu,
74032951860SFabian Aggeler             (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
74132951860SFabian Aggeler             (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
74232951860SFabian Aggeler }
74332951860SFabian Aggeler 
74408efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
74508efa9f2SFabian Aggeler {
74671aa735bSLuc MICHEL     if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
74771aa735bSLuc MICHEL         /* Idle priority */
74871aa735bSLuc MICHEL         return 0xff;
74971aa735bSLuc MICHEL     }
75071aa735bSLuc MICHEL 
7513dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
75208efa9f2SFabian Aggeler         if (s->running_priority[cpu] & 0x80) {
75308efa9f2SFabian Aggeler             /* Running priority in upper half of range: return the Non-secure
75408efa9f2SFabian Aggeler              * view of the priority.
75508efa9f2SFabian Aggeler              */
75608efa9f2SFabian Aggeler             return s->running_priority[cpu] << 1;
75708efa9f2SFabian Aggeler         } else {
75808efa9f2SFabian Aggeler             /* Running priority in lower half of range: RAZ */
75908efa9f2SFabian Aggeler             return 0;
76008efa9f2SFabian Aggeler         }
76108efa9f2SFabian Aggeler     } else {
76208efa9f2SFabian Aggeler         return s->running_priority[cpu];
76308efa9f2SFabian Aggeler     }
76408efa9f2SFabian Aggeler }
76508efa9f2SFabian Aggeler 
766a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation,
767a55c910eSPeter Maydell  * ie whether the relevant EOIMode bit is set.
768a55c910eSPeter Maydell  */
769a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
770a55c910eSPeter Maydell {
771a55c910eSPeter Maydell     if (s->revision != 2) {
772a55c910eSPeter Maydell         /* Before GICv2 prio-drop and deactivate are not separable */
773a55c910eSPeter Maydell         return false;
774a55c910eSPeter Maydell     }
7753dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
776a55c910eSPeter Maydell         return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
777a55c910eSPeter Maydell     }
778a55c910eSPeter Maydell     return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
779a55c910eSPeter Maydell }
780a55c910eSPeter Maydell 
781a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
782a55c910eSPeter Maydell {
783ee03cca8SPeter Maydell     int group;
784ee03cca8SPeter Maydell 
78502f2e22dSLuc Michel     if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
786ee03cca8SPeter Maydell         /*
787ee03cca8SPeter Maydell          * This handles two cases:
788ee03cca8SPeter Maydell          * 1. If software writes the ID of a spurious interrupt [ie 1023]
789ee03cca8SPeter Maydell          * to the GICC_DIR, the GIC ignores that write.
790ee03cca8SPeter Maydell          * 2. If software writes the number of a non-existent interrupt
791ee03cca8SPeter Maydell          * this must be a subcase of "value written is not an active interrupt"
79202f2e22dSLuc Michel          * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs,
79302f2e22dSLuc Michel          * all IRQs potentially exist, so this limit does not apply.
794ee03cca8SPeter Maydell          */
795ee03cca8SPeter Maydell         return;
796ee03cca8SPeter Maydell     }
797ee03cca8SPeter Maydell 
798a55c910eSPeter Maydell     if (!gic_eoi_split(s, cpu, attrs)) {
799a55c910eSPeter Maydell         /* This is UNPREDICTABLE; we choose to ignore it */
800a55c910eSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
801a55c910eSPeter Maydell                       "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
802a55c910eSPeter Maydell         return;
803a55c910eSPeter Maydell     }
804a55c910eSPeter Maydell 
80502f2e22dSLuc Michel     if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
80602f2e22dSLuc Michel         /* This vIRQ does not have an LR entry which is either active or
80702f2e22dSLuc Michel          * pending and active. Increment EOICount and ignore the write.
80802f2e22dSLuc Michel          */
80902f2e22dSLuc Michel         int rcpu = gic_get_vcpu_real_id(cpu);
81002f2e22dSLuc Michel         s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
811cbe1282bSLuc Michel 
812cbe1282bSLuc Michel         /* Update the virtual interface in case a maintenance interrupt should
813cbe1282bSLuc Michel          * be raised.
814cbe1282bSLuc Michel          */
815cbe1282bSLuc Michel         gic_update_virt(s);
81602f2e22dSLuc Michel         return;
81702f2e22dSLuc Michel     }
81802f2e22dSLuc Michel 
81902f2e22dSLuc Michel     group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
82002f2e22dSLuc Michel 
8213dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
822a55c910eSPeter Maydell         DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
823a55c910eSPeter Maydell         return;
824a55c910eSPeter Maydell     }
825a55c910eSPeter Maydell 
82686b350f0SLuc Michel     gic_clear_active(s, irq, cpu);
827a55c910eSPeter Maydell }
828a55c910eSPeter Maydell 
82950491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
830e69954b9Spbrook {
8319ee6e8bbSpbrook     int cm = 1 << cpu;
83272889c8aSPeter Maydell     int group;
83372889c8aSPeter Maydell 
834df628ff1Spbrook     DPRINTF("EOI %d\n", irq);
83502f2e22dSLuc Michel     if (gic_is_vcpu(cpu)) {
83602f2e22dSLuc Michel         /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the
83702f2e22dSLuc Michel          * running prio is < 0x100.
83802f2e22dSLuc Michel          */
83902f2e22dSLuc Michel         bool prio_drop = s->running_priority[cpu] < 0x100;
84002f2e22dSLuc Michel 
84102f2e22dSLuc Michel         if (irq >= GIC_MAXIRQ) {
84202f2e22dSLuc Michel             /* Ignore spurious interrupt */
84302f2e22dSLuc Michel             return;
84402f2e22dSLuc Michel         }
84502f2e22dSLuc Michel 
84602f2e22dSLuc Michel         gic_drop_prio(s, cpu, 0);
84702f2e22dSLuc Michel 
84802f2e22dSLuc Michel         if (!gic_eoi_split(s, cpu, attrs)) {
84902f2e22dSLuc Michel             bool valid = gic_virq_is_valid(s, irq, cpu);
85002f2e22dSLuc Michel             if (prio_drop && !valid) {
85102f2e22dSLuc Michel                 /* We are in a situation where:
85202f2e22dSLuc Michel                  *   - V_CTRL.EOIMode is false (no EOI split),
85302f2e22dSLuc Michel                  *   - The call to gic_drop_prio() cleared a bit in GICH_APR,
85402f2e22dSLuc Michel                  *   - This vIRQ does not have an LR entry which is either
85502f2e22dSLuc Michel                  *     active or pending and active.
85602f2e22dSLuc Michel                  * In that case, we must increment EOICount.
85702f2e22dSLuc Michel                  */
85802f2e22dSLuc Michel                 int rcpu = gic_get_vcpu_real_id(cpu);
85902f2e22dSLuc Michel                 s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
86002f2e22dSLuc Michel             } else if (valid) {
86102f2e22dSLuc Michel                 gic_clear_active(s, irq, cpu);
86202f2e22dSLuc Michel             }
86302f2e22dSLuc Michel         }
86402f2e22dSLuc Michel 
865cbe1282bSLuc Michel         gic_update_virt(s);
86602f2e22dSLuc Michel         return;
86702f2e22dSLuc Michel     }
86802f2e22dSLuc Michel 
869a32134aaSMark Langsdorf     if (irq >= s->num_irq) {
870217bfb44SPeter Maydell         /* This handles two cases:
871217bfb44SPeter Maydell          * 1. If software writes the ID of a spurious interrupt [ie 1023]
872217bfb44SPeter Maydell          * to the GICC_EOIR, the GIC ignores that write.
873217bfb44SPeter Maydell          * 2. If software writes the number of a non-existent interrupt
874217bfb44SPeter Maydell          * this must be a subcase of "value written does not match the last
875217bfb44SPeter Maydell          * valid interrupt value read from the Interrupt Acknowledge
876217bfb44SPeter Maydell          * register" and so this is UNPREDICTABLE. We choose to ignore it.
877217bfb44SPeter Maydell          */
878217bfb44SPeter Maydell         return;
879217bfb44SPeter Maydell     }
88072889c8aSPeter Maydell     if (s->running_priority[cpu] == 0x100) {
881e69954b9Spbrook         return; /* No active IRQ.  */
88272889c8aSPeter Maydell     }
8838d999995SChristoffer Dall 
8843bc4b52cSMarcin Krzeminski     if (s->revision == REV_11MPCORE) {
885e69954b9Spbrook         /* Mark level triggered interrupts as pending if they are still
886e69954b9Spbrook            raised.  */
88767ce697aSLuc Michel         if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
88867ce697aSLuc Michel             && GIC_DIST_TEST_LEVEL(irq, cm)
88967ce697aSLuc Michel             && (GIC_DIST_TARGET(irq) & cm) != 0) {
8909ee6e8bbSpbrook             DPRINTF("Set %d pending mask %x\n", irq, cm);
89167ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, cm);
892e69954b9Spbrook         }
8938d999995SChristoffer Dall     }
8948d999995SChristoffer Dall 
89586b350f0SLuc Michel     group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
89672889c8aSPeter Maydell 
8973dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
898f9c6a7f1SFabian Aggeler         DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
899f9c6a7f1SFabian Aggeler         return;
900f9c6a7f1SFabian Aggeler     }
901f9c6a7f1SFabian Aggeler 
902f9c6a7f1SFabian Aggeler     /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
903f9c6a7f1SFabian Aggeler      * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
904f9c6a7f1SFabian Aggeler      * i.e. go ahead and complete the irq anyway.
905f9c6a7f1SFabian Aggeler      */
906f9c6a7f1SFabian Aggeler 
90772889c8aSPeter Maydell     gic_drop_prio(s, cpu, group);
908a55c910eSPeter Maydell 
909a55c910eSPeter Maydell     /* In GICv2 the guest can choose to split priority-drop and deactivate */
910a55c910eSPeter Maydell     if (!gic_eoi_split(s, cpu, attrs)) {
91186b350f0SLuc Michel         gic_clear_active(s, irq, cpu);
912a55c910eSPeter Maydell     }
913e69954b9Spbrook     gic_update(s);
914e69954b9Spbrook }
915e69954b9Spbrook 
916a9d85353SPeter Maydell static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
917e69954b9Spbrook {
918fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
919e69954b9Spbrook     uint32_t res;
920e69954b9Spbrook     int irq;
921e69954b9Spbrook     int i;
9229ee6e8bbSpbrook     int cpu;
9239ee6e8bbSpbrook     int cm;
9249ee6e8bbSpbrook     int mask;
925e69954b9Spbrook 
926926c4affSPeter Maydell     cpu = gic_get_current_cpu(s);
9279ee6e8bbSpbrook     cm = 1 << cpu;
928e69954b9Spbrook     if (offset < 0x100) {
929679aa175SFabian Aggeler         if (offset == 0) {      /* GICD_CTLR */
930679aa175SFabian Aggeler             if (s->security_extn && !attrs.secure) {
931679aa175SFabian Aggeler                 /* The NS bank of this register is just an alias of the
932679aa175SFabian Aggeler                  * EnableGrp1 bit in the S bank version.
933679aa175SFabian Aggeler                  */
934679aa175SFabian Aggeler                 return extract32(s->ctlr, 1, 1);
935679aa175SFabian Aggeler             } else {
936679aa175SFabian Aggeler                 return s->ctlr;
937679aa175SFabian Aggeler             }
938679aa175SFabian Aggeler         }
939e69954b9Spbrook         if (offset == 4)
9405543d1abSFabian Aggeler             /* Interrupt Controller Type Register */
9415543d1abSFabian Aggeler             return ((s->num_irq / 32) - 1)
942b95690c9SWei Huang                     | ((s->num_cpu - 1) << 5)
9435543d1abSFabian Aggeler                     | (s->security_extn << 10);
944e69954b9Spbrook         if (offset < 0x08)
945e69954b9Spbrook             return 0;
946b79f2265SRob Herring         if (offset >= 0x80) {
947c27a5ba9SFabian Aggeler             /* Interrupt Group Registers: these RAZ/WI if this is an NS
948c27a5ba9SFabian Aggeler              * access to a GIC with the security extensions, or if the GIC
949c27a5ba9SFabian Aggeler              * doesn't have groups at all.
950c27a5ba9SFabian Aggeler              */
951c27a5ba9SFabian Aggeler             res = 0;
952c27a5ba9SFabian Aggeler             if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
953c27a5ba9SFabian Aggeler                 /* Every byte offset holds 8 group status bits */
954c27a5ba9SFabian Aggeler                 irq = (offset - 0x080) * 8 + GIC_BASE_IRQ;
955c27a5ba9SFabian Aggeler                 if (irq >= s->num_irq) {
956c27a5ba9SFabian Aggeler                     goto bad_reg;
957c27a5ba9SFabian Aggeler                 }
958c27a5ba9SFabian Aggeler                 for (i = 0; i < 8; i++) {
95967ce697aSLuc Michel                     if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
960c27a5ba9SFabian Aggeler                         res |= (1 << i);
961c27a5ba9SFabian Aggeler                     }
962c27a5ba9SFabian Aggeler                 }
963c27a5ba9SFabian Aggeler             }
964c27a5ba9SFabian Aggeler             return res;
965b79f2265SRob Herring         }
966e69954b9Spbrook         goto bad_reg;
967e69954b9Spbrook     } else if (offset < 0x200) {
968e69954b9Spbrook         /* Interrupt Set/Clear Enable.  */
969e69954b9Spbrook         if (offset < 0x180)
970e69954b9Spbrook             irq = (offset - 0x100) * 8;
971e69954b9Spbrook         else
972e69954b9Spbrook             irq = (offset - 0x180) * 8;
9739ee6e8bbSpbrook         irq += GIC_BASE_IRQ;
974a32134aaSMark Langsdorf         if (irq >= s->num_irq)
975e69954b9Spbrook             goto bad_reg;
976e69954b9Spbrook         res = 0;
977e69954b9Spbrook         for (i = 0; i < 8; i++) {
978fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
97967ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
980fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
981fea8a08eSJens Wiklander             }
982fea8a08eSJens Wiklander 
98367ce697aSLuc Michel             if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
984e69954b9Spbrook                 res |= (1 << i);
985e69954b9Spbrook             }
986e69954b9Spbrook         }
987e69954b9Spbrook     } else if (offset < 0x300) {
988e69954b9Spbrook         /* Interrupt Set/Clear Pending.  */
989e69954b9Spbrook         if (offset < 0x280)
990e69954b9Spbrook             irq = (offset - 0x200) * 8;
991e69954b9Spbrook         else
992e69954b9Spbrook             irq = (offset - 0x280) * 8;
9939ee6e8bbSpbrook         irq += GIC_BASE_IRQ;
994a32134aaSMark Langsdorf         if (irq >= s->num_irq)
995e69954b9Spbrook             goto bad_reg;
996e69954b9Spbrook         res = 0;
99769253800SRusty Russell         mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
998e69954b9Spbrook         for (i = 0; i < 8; i++) {
999fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
100067ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1001fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1002fea8a08eSJens Wiklander             }
1003fea8a08eSJens Wiklander 
10048d999995SChristoffer Dall             if (gic_test_pending(s, irq + i, mask)) {
1005e69954b9Spbrook                 res |= (1 << i);
1006e69954b9Spbrook             }
1007e69954b9Spbrook         }
1008e69954b9Spbrook     } else if (offset < 0x400) {
10093bb0b038SLuc Michel         /* Interrupt Set/Clear Active.  */
10103bb0b038SLuc Michel         if (offset < 0x380) {
10113bb0b038SLuc Michel             irq = (offset - 0x300) * 8;
10123bb0b038SLuc Michel         } else if (s->revision == 2) {
10133bb0b038SLuc Michel             irq = (offset - 0x380) * 8;
10143bb0b038SLuc Michel         } else {
10153bb0b038SLuc Michel             goto bad_reg;
10163bb0b038SLuc Michel         }
10173bb0b038SLuc Michel 
10183bb0b038SLuc Michel         irq += GIC_BASE_IRQ;
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 
102967ce697aSLuc Michel             if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
1030e69954b9Spbrook                 res |= (1 << i);
1031e69954b9Spbrook             }
1032e69954b9Spbrook         }
1033e69954b9Spbrook     } else if (offset < 0x800) {
1034e69954b9Spbrook         /* Interrupt Priority.  */
10359ee6e8bbSpbrook         irq = (offset - 0x400) + GIC_BASE_IRQ;
1036a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1037e69954b9Spbrook             goto bad_reg;
103867ce697aSLuc Michel         res = gic_dist_get_priority(s, cpu, irq, attrs);
1039e69954b9Spbrook     } else if (offset < 0xc00) {
1040e69954b9Spbrook         /* Interrupt CPU Target.  */
10416b9680bbSPeter Maydell         if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
10426b9680bbSPeter Maydell             /* For uniprocessor GICs these RAZ/WI */
10436b9680bbSPeter Maydell             res = 0;
10446b9680bbSPeter Maydell         } else {
10459ee6e8bbSpbrook             irq = (offset - 0x800) + GIC_BASE_IRQ;
10466b9680bbSPeter Maydell             if (irq >= s->num_irq) {
1047e69954b9Spbrook                 goto bad_reg;
10486b9680bbSPeter Maydell             }
10497995206dSPeter Maydell             if (irq < 29 && s->revision == REV_11MPCORE) {
10507995206dSPeter Maydell                 res = 0;
10517995206dSPeter Maydell             } else if (irq < GIC_INTERNAL) {
10529ee6e8bbSpbrook                 res = cm;
10539ee6e8bbSpbrook             } else {
105467ce697aSLuc Michel                 res = GIC_DIST_TARGET(irq);
10559ee6e8bbSpbrook             }
10566b9680bbSPeter Maydell         }
1057e69954b9Spbrook     } else if (offset < 0xf00) {
1058e69954b9Spbrook         /* Interrupt Configuration.  */
105971a62046SAdam Lackorzynski         irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
1060a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1061e69954b9Spbrook             goto bad_reg;
1062e69954b9Spbrook         res = 0;
1063e69954b9Spbrook         for (i = 0; i < 4; i++) {
1064fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
106567ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1066fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1067fea8a08eSJens Wiklander             }
1068fea8a08eSJens Wiklander 
106967ce697aSLuc Michel             if (GIC_DIST_TEST_MODEL(irq + i)) {
1070e69954b9Spbrook                 res |= (1 << (i * 2));
107167ce697aSLuc Michel             }
107267ce697aSLuc Michel             if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1073e69954b9Spbrook                 res |= (2 << (i * 2));
1074e69954b9Spbrook             }
107567ce697aSLuc Michel         }
107640d22500SChristoffer Dall     } else if (offset < 0xf10) {
107740d22500SChristoffer Dall         goto bad_reg;
107840d22500SChristoffer Dall     } else if (offset < 0xf30) {
10797c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
108040d22500SChristoffer Dall             goto bad_reg;
108140d22500SChristoffer Dall         }
108240d22500SChristoffer Dall 
108340d22500SChristoffer Dall         if (offset < 0xf20) {
108440d22500SChristoffer Dall             /* GICD_CPENDSGIRn */
108540d22500SChristoffer Dall             irq = (offset - 0xf10);
108640d22500SChristoffer Dall         } else {
108740d22500SChristoffer Dall             irq = (offset - 0xf20);
108840d22500SChristoffer Dall             /* GICD_SPENDSGIRn */
108940d22500SChristoffer Dall         }
109040d22500SChristoffer Dall 
1091fea8a08eSJens Wiklander         if (s->security_extn && !attrs.secure &&
109267ce697aSLuc Michel             !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1093fea8a08eSJens Wiklander             res = 0; /* Ignore Non-secure access of Group0 IRQ */
1094fea8a08eSJens Wiklander         } else {
109540d22500SChristoffer Dall             res = s->sgi_pending[irq][cpu];
1096fea8a08eSJens Wiklander         }
10973355c360SAlistair Francis     } else if (offset < 0xfd0) {
1098e69954b9Spbrook         goto bad_reg;
10993355c360SAlistair Francis     } else if (offset < 0x1000) {
1100e69954b9Spbrook         if (offset & 3) {
1101e69954b9Spbrook             res = 0;
1102e69954b9Spbrook         } else {
11033355c360SAlistair Francis             switch (s->revision) {
11043355c360SAlistair Francis             case REV_11MPCORE:
11053355c360SAlistair Francis                 res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
11063355c360SAlistair Francis                 break;
11073355c360SAlistair Francis             case 1:
11083355c360SAlistair Francis                 res = gic_id_gicv1[(offset - 0xfd0) >> 2];
11093355c360SAlistair Francis                 break;
11103355c360SAlistair Francis             case 2:
11113355c360SAlistair Francis                 res = gic_id_gicv2[(offset - 0xfd0) >> 2];
11123355c360SAlistair Francis                 break;
11133355c360SAlistair Francis             default:
11143355c360SAlistair Francis                 res = 0;
1115e69954b9Spbrook             }
1116e69954b9Spbrook         }
11173355c360SAlistair Francis     } else {
11183355c360SAlistair Francis         g_assert_not_reached();
11193355c360SAlistair Francis     }
1120e69954b9Spbrook     return res;
1121e69954b9Spbrook bad_reg:
11228c8dc39fSPeter Maydell     qemu_log_mask(LOG_GUEST_ERROR,
11238c8dc39fSPeter Maydell                   "gic_dist_readb: Bad offset %x\n", (int)offset);
1124e69954b9Spbrook     return 0;
1125e69954b9Spbrook }
1126e69954b9Spbrook 
1127a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
1128a9d85353SPeter Maydell                                  unsigned size, MemTxAttrs attrs)
1129e69954b9Spbrook {
1130a9d85353SPeter Maydell     switch (size) {
1131a9d85353SPeter Maydell     case 1:
1132a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1133a9d85353SPeter Maydell         return MEMTX_OK;
1134a9d85353SPeter Maydell     case 2:
1135a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1136a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1137a9d85353SPeter Maydell         return MEMTX_OK;
1138a9d85353SPeter Maydell     case 4:
1139a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1140a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1141a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
1142a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
1143a9d85353SPeter Maydell         return MEMTX_OK;
1144a9d85353SPeter Maydell     default:
1145a9d85353SPeter Maydell         return MEMTX_ERROR;
1146e69954b9Spbrook     }
1147e69954b9Spbrook }
1148e69954b9Spbrook 
1149a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset,
1150a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1151e69954b9Spbrook {
1152fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1153e69954b9Spbrook     int irq;
1154e69954b9Spbrook     int i;
11559ee6e8bbSpbrook     int cpu;
1156e69954b9Spbrook 
1157926c4affSPeter Maydell     cpu = gic_get_current_cpu(s);
1158e69954b9Spbrook     if (offset < 0x100) {
1159e69954b9Spbrook         if (offset == 0) {
1160679aa175SFabian Aggeler             if (s->security_extn && !attrs.secure) {
1161679aa175SFabian Aggeler                 /* NS version is just an alias of the S version's bit 1 */
1162679aa175SFabian Aggeler                 s->ctlr = deposit32(s->ctlr, 1, 1, value);
1163679aa175SFabian Aggeler             } else if (gic_has_groups(s)) {
1164679aa175SFabian Aggeler                 s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
1165679aa175SFabian Aggeler             } else {
1166679aa175SFabian Aggeler                 s->ctlr = value & GICD_CTLR_EN_GRP0;
1167679aa175SFabian Aggeler             }
1168679aa175SFabian Aggeler             DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
1169679aa175SFabian Aggeler                     s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
1170679aa175SFabian Aggeler                     s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
1171e69954b9Spbrook         } else if (offset < 4) {
1172e69954b9Spbrook             /* ignored.  */
1173b79f2265SRob Herring         } else if (offset >= 0x80) {
1174c27a5ba9SFabian Aggeler             /* Interrupt Group Registers: RAZ/WI for NS access to secure
1175c27a5ba9SFabian Aggeler              * GIC, or for GICs without groups.
1176c27a5ba9SFabian Aggeler              */
1177c27a5ba9SFabian Aggeler             if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
1178c27a5ba9SFabian Aggeler                 /* Every byte offset holds 8 group status bits */
1179c27a5ba9SFabian Aggeler                 irq = (offset - 0x80) * 8 + GIC_BASE_IRQ;
1180c27a5ba9SFabian Aggeler                 if (irq >= s->num_irq) {
1181c27a5ba9SFabian Aggeler                     goto bad_reg;
1182c27a5ba9SFabian Aggeler                 }
1183c27a5ba9SFabian Aggeler                 for (i = 0; i < 8; i++) {
1184c27a5ba9SFabian Aggeler                     /* Group bits are banked for private interrupts */
1185c27a5ba9SFabian Aggeler                     int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1186c27a5ba9SFabian Aggeler                     if (value & (1 << i)) {
1187c27a5ba9SFabian Aggeler                         /* Group1 (Non-secure) */
118867ce697aSLuc Michel                         GIC_DIST_SET_GROUP(irq + i, cm);
1189c27a5ba9SFabian Aggeler                     } else {
1190c27a5ba9SFabian Aggeler                         /* Group0 (Secure) */
119167ce697aSLuc Michel                         GIC_DIST_CLEAR_GROUP(irq + i, cm);
1192c27a5ba9SFabian Aggeler                     }
1193c27a5ba9SFabian Aggeler                 }
1194c27a5ba9SFabian Aggeler             }
1195e69954b9Spbrook         } else {
1196e69954b9Spbrook             goto bad_reg;
1197e69954b9Spbrook         }
1198e69954b9Spbrook     } else if (offset < 0x180) {
1199e69954b9Spbrook         /* Interrupt Set Enable.  */
12009ee6e8bbSpbrook         irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
1201a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1202e69954b9Spbrook             goto bad_reg;
120341ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12049ee6e8bbSpbrook             value = 0xff;
120541ab7b55SChristoffer Dall         }
120641ab7b55SChristoffer Dall 
1207e69954b9Spbrook         for (i = 0; i < 8; i++) {
1208e69954b9Spbrook             if (value & (1 << i)) {
1209f47b48fbSDaniel Sangorrin                 int mask =
121067ce697aSLuc Michel                     (irq < GIC_INTERNAL) ? (1 << cpu)
121167ce697aSLuc Michel                                          : GIC_DIST_TARGET(irq + i);
121269253800SRusty Russell                 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
121341bf234dSRabin Vincent 
1214fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
121567ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1216fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1217fea8a08eSJens Wiklander                 }
1218fea8a08eSJens Wiklander 
121967ce697aSLuc Michel                 if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1220e69954b9Spbrook                     DPRINTF("Enabled IRQ %d\n", irq + i);
12212531088fSHollis Blanchard                     trace_gic_enable_irq(irq + i);
122241bf234dSRabin Vincent                 }
122367ce697aSLuc Michel                 GIC_DIST_SET_ENABLED(irq + i, cm);
1224e69954b9Spbrook                 /* If a raised level triggered IRQ enabled then mark
1225e69954b9Spbrook                    is as pending.  */
122667ce697aSLuc Michel                 if (GIC_DIST_TEST_LEVEL(irq + i, mask)
122767ce697aSLuc Michel                         && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
12289ee6e8bbSpbrook                     DPRINTF("Set %d pending mask %x\n", irq + i, mask);
122967ce697aSLuc Michel                     GIC_DIST_SET_PENDING(irq + i, mask);
12309ee6e8bbSpbrook                 }
1231e69954b9Spbrook             }
1232e69954b9Spbrook         }
1233e69954b9Spbrook     } else if (offset < 0x200) {
1234e69954b9Spbrook         /* Interrupt Clear Enable.  */
12359ee6e8bbSpbrook         irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
1236a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1237e69954b9Spbrook             goto bad_reg;
123841ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12399ee6e8bbSpbrook             value = 0;
124041ab7b55SChristoffer Dall         }
124141ab7b55SChristoffer Dall 
1242e69954b9Spbrook         for (i = 0; i < 8; i++) {
1243e69954b9Spbrook             if (value & (1 << i)) {
124469253800SRusty Russell                 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
124541bf234dSRabin Vincent 
1246fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
124767ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1248fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1249fea8a08eSJens Wiklander                 }
1250fea8a08eSJens Wiklander 
125167ce697aSLuc Michel                 if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1252e69954b9Spbrook                     DPRINTF("Disabled IRQ %d\n", irq + i);
12532531088fSHollis Blanchard                     trace_gic_disable_irq(irq + i);
125441bf234dSRabin Vincent                 }
125567ce697aSLuc Michel                 GIC_DIST_CLEAR_ENABLED(irq + i, cm);
1256e69954b9Spbrook             }
1257e69954b9Spbrook         }
1258e69954b9Spbrook     } else if (offset < 0x280) {
1259e69954b9Spbrook         /* Interrupt Set Pending.  */
12609ee6e8bbSpbrook         irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
1261a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1262e69954b9Spbrook             goto bad_reg;
126341ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12645b0adce1SChristoffer Dall             value = 0;
126541ab7b55SChristoffer Dall         }
12669ee6e8bbSpbrook 
1267e69954b9Spbrook         for (i = 0; i < 8; i++) {
1268e69954b9Spbrook             if (value & (1 << i)) {
1269fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
127067ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1271fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1272fea8a08eSJens Wiklander                 }
1273fea8a08eSJens Wiklander 
127467ce697aSLuc Michel                 GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i));
1275e69954b9Spbrook             }
1276e69954b9Spbrook         }
1277e69954b9Spbrook     } else if (offset < 0x300) {
1278e69954b9Spbrook         /* Interrupt Clear Pending.  */
12799ee6e8bbSpbrook         irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
1280a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1281e69954b9Spbrook             goto bad_reg;
12825b0adce1SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12835b0adce1SChristoffer Dall             value = 0;
12845b0adce1SChristoffer Dall         }
12855b0adce1SChristoffer Dall 
1286e69954b9Spbrook         for (i = 0; i < 8; i++) {
1287fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
128867ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1289fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1290fea8a08eSJens Wiklander             }
1291fea8a08eSJens Wiklander 
12929ee6e8bbSpbrook             /* ??? This currently clears the pending bit for all CPUs, even
12939ee6e8bbSpbrook                for per-CPU interrupts.  It's unclear whether this is the
12949ee6e8bbSpbrook                corect behavior.  */
1295e69954b9Spbrook             if (value & (1 << i)) {
129667ce697aSLuc Michel                 GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
1297e69954b9Spbrook             }
1298e69954b9Spbrook         }
12993bb0b038SLuc Michel     } else if (offset < 0x380) {
13003bb0b038SLuc Michel         /* Interrupt Set Active.  */
13013bb0b038SLuc Michel         if (s->revision != 2) {
1302e69954b9Spbrook             goto bad_reg;
13033bb0b038SLuc Michel         }
13043bb0b038SLuc Michel 
13053bb0b038SLuc Michel         irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
13063bb0b038SLuc Michel         if (irq >= s->num_irq) {
13073bb0b038SLuc Michel             goto bad_reg;
13083bb0b038SLuc Michel         }
13093bb0b038SLuc Michel 
13103bb0b038SLuc Michel         /* This register is banked per-cpu for PPIs */
13113bb0b038SLuc Michel         int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13123bb0b038SLuc Michel 
13133bb0b038SLuc Michel         for (i = 0; i < 8; i++) {
13143bb0b038SLuc Michel             if (s->security_extn && !attrs.secure &&
13153bb0b038SLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13163bb0b038SLuc Michel                 continue; /* Ignore Non-secure access of Group0 IRQ */
13173bb0b038SLuc Michel             }
13183bb0b038SLuc Michel 
13193bb0b038SLuc Michel             if (value & (1 << i)) {
13203bb0b038SLuc Michel                 GIC_DIST_SET_ACTIVE(irq + i, cm);
13213bb0b038SLuc Michel             }
13223bb0b038SLuc Michel         }
13233bb0b038SLuc Michel     } else if (offset < 0x400) {
13243bb0b038SLuc Michel         /* Interrupt Clear Active.  */
13253bb0b038SLuc Michel         if (s->revision != 2) {
13263bb0b038SLuc Michel             goto bad_reg;
13273bb0b038SLuc Michel         }
13283bb0b038SLuc Michel 
13293bb0b038SLuc Michel         irq = (offset - 0x380) * 8 + GIC_BASE_IRQ;
13303bb0b038SLuc Michel         if (irq >= s->num_irq) {
13313bb0b038SLuc Michel             goto bad_reg;
13323bb0b038SLuc Michel         }
13333bb0b038SLuc Michel 
13343bb0b038SLuc Michel         /* This register is banked per-cpu for PPIs */
13353bb0b038SLuc Michel         int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13363bb0b038SLuc Michel 
13373bb0b038SLuc Michel         for (i = 0; i < 8; i++) {
13383bb0b038SLuc Michel             if (s->security_extn && !attrs.secure &&
13393bb0b038SLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13403bb0b038SLuc Michel                 continue; /* Ignore Non-secure access of Group0 IRQ */
13413bb0b038SLuc Michel             }
13423bb0b038SLuc Michel 
13433bb0b038SLuc Michel             if (value & (1 << i)) {
13443bb0b038SLuc Michel                 GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
13453bb0b038SLuc Michel             }
13463bb0b038SLuc Michel         }
1347e69954b9Spbrook     } else if (offset < 0x800) {
1348e69954b9Spbrook         /* Interrupt Priority.  */
13499ee6e8bbSpbrook         irq = (offset - 0x400) + GIC_BASE_IRQ;
1350a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1351e69954b9Spbrook             goto bad_reg;
135267ce697aSLuc Michel         gic_dist_set_priority(s, cpu, irq, value, attrs);
1353e69954b9Spbrook     } else if (offset < 0xc00) {
13546b9680bbSPeter Maydell         /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
13556b9680bbSPeter Maydell          * annoying exception of the 11MPCore's GIC.
13566b9680bbSPeter Maydell          */
13576b9680bbSPeter Maydell         if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
13589ee6e8bbSpbrook             irq = (offset - 0x800) + GIC_BASE_IRQ;
13596b9680bbSPeter Maydell             if (irq >= s->num_irq) {
1360e69954b9Spbrook                 goto bad_reg;
13616b9680bbSPeter Maydell             }
13627995206dSPeter Maydell             if (irq < 29 && s->revision == REV_11MPCORE) {
13639ee6e8bbSpbrook                 value = 0;
13646b9680bbSPeter Maydell             } else if (irq < GIC_INTERNAL) {
13659ee6e8bbSpbrook                 value = ALL_CPU_MASK;
13666b9680bbSPeter Maydell             }
13679ee6e8bbSpbrook             s->irq_target[irq] = value & ALL_CPU_MASK;
13686b9680bbSPeter Maydell         }
1369e69954b9Spbrook     } else if (offset < 0xf00) {
1370e69954b9Spbrook         /* Interrupt Configuration.  */
13719ee6e8bbSpbrook         irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
1372a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1373e69954b9Spbrook             goto bad_reg;
1374de7a900fSAdam Lackorzynski         if (irq < GIC_NR_SGIS)
13759ee6e8bbSpbrook             value |= 0xaa;
1376e69954b9Spbrook         for (i = 0; i < 4; i++) {
1377fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
137867ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1379fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1380fea8a08eSJens Wiklander             }
1381fea8a08eSJens Wiklander 
13827c14b3acSMichael Davidsaver             if (s->revision == REV_11MPCORE) {
1383e69954b9Spbrook                 if (value & (1 << (i * 2))) {
138467ce697aSLuc Michel                     GIC_DIST_SET_MODEL(irq + i);
1385e69954b9Spbrook                 } else {
138667ce697aSLuc Michel                     GIC_DIST_CLEAR_MODEL(irq + i);
1387e69954b9Spbrook                 }
138824b790dfSAdam Lackorzynski             }
1389e69954b9Spbrook             if (value & (2 << (i * 2))) {
139067ce697aSLuc Michel                 GIC_DIST_SET_EDGE_TRIGGER(irq + i);
1391e69954b9Spbrook             } else {
139267ce697aSLuc Michel                 GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
1393e69954b9Spbrook             }
1394e69954b9Spbrook         }
139540d22500SChristoffer Dall     } else if (offset < 0xf10) {
13969ee6e8bbSpbrook         /* 0xf00 is only handled for 32-bit writes.  */
1397e69954b9Spbrook         goto bad_reg;
139840d22500SChristoffer Dall     } else if (offset < 0xf20) {
139940d22500SChristoffer Dall         /* GICD_CPENDSGIRn */
14007c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
140140d22500SChristoffer Dall             goto bad_reg;
140240d22500SChristoffer Dall         }
140340d22500SChristoffer Dall         irq = (offset - 0xf10);
140440d22500SChristoffer Dall 
1405fea8a08eSJens Wiklander         if (!s->security_extn || attrs.secure ||
140667ce697aSLuc Michel             GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
140740d22500SChristoffer Dall             s->sgi_pending[irq][cpu] &= ~value;
140840d22500SChristoffer Dall             if (s->sgi_pending[irq][cpu] == 0) {
140967ce697aSLuc Michel                 GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
141040d22500SChristoffer Dall             }
1411fea8a08eSJens Wiklander         }
141240d22500SChristoffer Dall     } else if (offset < 0xf30) {
141340d22500SChristoffer Dall         /* GICD_SPENDSGIRn */
14147c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
141540d22500SChristoffer Dall             goto bad_reg;
141640d22500SChristoffer Dall         }
141740d22500SChristoffer Dall         irq = (offset - 0xf20);
141840d22500SChristoffer Dall 
1419fea8a08eSJens Wiklander         if (!s->security_extn || attrs.secure ||
142067ce697aSLuc Michel             GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
142167ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, 1 << cpu);
142240d22500SChristoffer Dall             s->sgi_pending[irq][cpu] |= value;
1423fea8a08eSJens Wiklander         }
142440d22500SChristoffer Dall     } else {
142540d22500SChristoffer Dall         goto bad_reg;
1426e69954b9Spbrook     }
1427e69954b9Spbrook     gic_update(s);
1428e69954b9Spbrook     return;
1429e69954b9Spbrook bad_reg:
14308c8dc39fSPeter Maydell     qemu_log_mask(LOG_GUEST_ERROR,
14318c8dc39fSPeter Maydell                   "gic_dist_writeb: Bad offset %x\n", (int)offset);
1432e69954b9Spbrook }
1433e69954b9Spbrook 
1434a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset,
1435a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1436e69954b9Spbrook {
1437a9d85353SPeter Maydell     gic_dist_writeb(opaque, offset, value & 0xff, attrs);
1438a9d85353SPeter Maydell     gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
1439e69954b9Spbrook }
1440e69954b9Spbrook 
1441a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset,
1442a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1443e69954b9Spbrook {
1444fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
14458da3ff18Spbrook     if (offset == 0xf00) {
14469ee6e8bbSpbrook         int cpu;
14479ee6e8bbSpbrook         int irq;
14489ee6e8bbSpbrook         int mask;
144940d22500SChristoffer Dall         int target_cpu;
14509ee6e8bbSpbrook 
1451926c4affSPeter Maydell         cpu = gic_get_current_cpu(s);
14529ee6e8bbSpbrook         irq = value & 0x3ff;
14539ee6e8bbSpbrook         switch ((value >> 24) & 3) {
14549ee6e8bbSpbrook         case 0:
14559ee6e8bbSpbrook             mask = (value >> 16) & ALL_CPU_MASK;
14569ee6e8bbSpbrook             break;
14579ee6e8bbSpbrook         case 1:
1458fa250144SAdam Lackorzynski             mask = ALL_CPU_MASK ^ (1 << cpu);
14599ee6e8bbSpbrook             break;
14609ee6e8bbSpbrook         case 2:
1461fa250144SAdam Lackorzynski             mask = 1 << cpu;
14629ee6e8bbSpbrook             break;
14639ee6e8bbSpbrook         default:
14649ee6e8bbSpbrook             DPRINTF("Bad Soft Int target filter\n");
14659ee6e8bbSpbrook             mask = ALL_CPU_MASK;
14669ee6e8bbSpbrook             break;
14679ee6e8bbSpbrook         }
146867ce697aSLuc Michel         GIC_DIST_SET_PENDING(irq, mask);
146940d22500SChristoffer Dall         target_cpu = ctz32(mask);
147040d22500SChristoffer Dall         while (target_cpu < GIC_NCPU) {
147140d22500SChristoffer Dall             s->sgi_pending[irq][target_cpu] |= (1 << cpu);
147240d22500SChristoffer Dall             mask &= ~(1 << target_cpu);
147340d22500SChristoffer Dall             target_cpu = ctz32(mask);
147440d22500SChristoffer Dall         }
14759ee6e8bbSpbrook         gic_update(s);
14769ee6e8bbSpbrook         return;
14779ee6e8bbSpbrook     }
1478a9d85353SPeter Maydell     gic_dist_writew(opaque, offset, value & 0xffff, attrs);
1479a9d85353SPeter Maydell     gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
1480a9d85353SPeter Maydell }
1481a9d85353SPeter Maydell 
1482a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
1483a9d85353SPeter Maydell                                   unsigned size, MemTxAttrs attrs)
1484a9d85353SPeter Maydell {
1485a9d85353SPeter Maydell     switch (size) {
1486a9d85353SPeter Maydell     case 1:
1487a9d85353SPeter Maydell         gic_dist_writeb(opaque, offset, data, attrs);
1488a9d85353SPeter Maydell         return MEMTX_OK;
1489a9d85353SPeter Maydell     case 2:
1490a9d85353SPeter Maydell         gic_dist_writew(opaque, offset, data, attrs);
1491a9d85353SPeter Maydell         return MEMTX_OK;
1492a9d85353SPeter Maydell     case 4:
1493a9d85353SPeter Maydell         gic_dist_writel(opaque, offset, data, attrs);
1494a9d85353SPeter Maydell         return MEMTX_OK;
1495a9d85353SPeter Maydell     default:
1496a9d85353SPeter Maydell         return MEMTX_ERROR;
1497a9d85353SPeter Maydell     }
1498e69954b9Spbrook }
1499e69954b9Spbrook 
150051fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
150151fd06e0SPeter Maydell {
150251fd06e0SPeter Maydell     /* Return the Nonsecure view of GICC_APR<regno>. This is the
150351fd06e0SPeter Maydell      * second half of GICC_NSAPR.
150451fd06e0SPeter Maydell      */
150551fd06e0SPeter Maydell     switch (GIC_MIN_BPR) {
150651fd06e0SPeter Maydell     case 0:
150751fd06e0SPeter Maydell         if (regno < 2) {
150851fd06e0SPeter Maydell             return s->nsapr[regno + 2][cpu];
150951fd06e0SPeter Maydell         }
151051fd06e0SPeter Maydell         break;
151151fd06e0SPeter Maydell     case 1:
151251fd06e0SPeter Maydell         if (regno == 0) {
151351fd06e0SPeter Maydell             return s->nsapr[regno + 1][cpu];
151451fd06e0SPeter Maydell         }
151551fd06e0SPeter Maydell         break;
151651fd06e0SPeter Maydell     case 2:
151751fd06e0SPeter Maydell         if (regno == 0) {
151851fd06e0SPeter Maydell             return extract32(s->nsapr[0][cpu], 16, 16);
151951fd06e0SPeter Maydell         }
152051fd06e0SPeter Maydell         break;
152151fd06e0SPeter Maydell     case 3:
152251fd06e0SPeter Maydell         if (regno == 0) {
152351fd06e0SPeter Maydell             return extract32(s->nsapr[0][cpu], 8, 8);
152451fd06e0SPeter Maydell         }
152551fd06e0SPeter Maydell         break;
152651fd06e0SPeter Maydell     default:
152751fd06e0SPeter Maydell         g_assert_not_reached();
152851fd06e0SPeter Maydell     }
152951fd06e0SPeter Maydell     return 0;
153051fd06e0SPeter Maydell }
153151fd06e0SPeter Maydell 
153251fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
153351fd06e0SPeter Maydell                                          uint32_t value)
153451fd06e0SPeter Maydell {
153551fd06e0SPeter Maydell     /* Write the Nonsecure view of GICC_APR<regno>. */
153651fd06e0SPeter Maydell     switch (GIC_MIN_BPR) {
153751fd06e0SPeter Maydell     case 0:
153851fd06e0SPeter Maydell         if (regno < 2) {
153951fd06e0SPeter Maydell             s->nsapr[regno + 2][cpu] = value;
154051fd06e0SPeter Maydell         }
154151fd06e0SPeter Maydell         break;
154251fd06e0SPeter Maydell     case 1:
154351fd06e0SPeter Maydell         if (regno == 0) {
154451fd06e0SPeter Maydell             s->nsapr[regno + 1][cpu] = value;
154551fd06e0SPeter Maydell         }
154651fd06e0SPeter Maydell         break;
154751fd06e0SPeter Maydell     case 2:
154851fd06e0SPeter Maydell         if (regno == 0) {
154951fd06e0SPeter Maydell             s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
155051fd06e0SPeter Maydell         }
155151fd06e0SPeter Maydell         break;
155251fd06e0SPeter Maydell     case 3:
155351fd06e0SPeter Maydell         if (regno == 0) {
155451fd06e0SPeter Maydell             s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
155551fd06e0SPeter Maydell         }
155651fd06e0SPeter Maydell         break;
155751fd06e0SPeter Maydell     default:
155851fd06e0SPeter Maydell         g_assert_not_reached();
155951fd06e0SPeter Maydell     }
156051fd06e0SPeter Maydell }
156151fd06e0SPeter Maydell 
1562a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1563a9d85353SPeter Maydell                                 uint64_t *data, MemTxAttrs attrs)
1564e69954b9Spbrook {
1565e69954b9Spbrook     switch (offset) {
1566e69954b9Spbrook     case 0x00: /* Control */
156732951860SFabian Aggeler         *data = gic_get_cpu_control(s, cpu, attrs);
1568a9d85353SPeter Maydell         break;
1569e69954b9Spbrook     case 0x04: /* Priority mask */
157081508470SFabian Aggeler         *data = gic_get_priority_mask(s, cpu, attrs);
1571a9d85353SPeter Maydell         break;
1572e69954b9Spbrook     case 0x08: /* Binary Point */
15733dd0471bSLuc Michel         if (gic_cpu_ns_access(s, cpu, attrs)) {
1574421a3c22SLuc MICHEL             if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1575421a3c22SLuc MICHEL                 /* NS view of BPR when CBPR is 1 */
1576421a3c22SLuc MICHEL                 *data = MIN(s->bpr[cpu] + 1, 7);
1577421a3c22SLuc MICHEL             } else {
1578822e9cc3SFabian Aggeler                 /* BPR is banked. Non-secure copy stored in ABPR. */
1579822e9cc3SFabian Aggeler                 *data = s->abpr[cpu];
1580421a3c22SLuc MICHEL             }
1581822e9cc3SFabian Aggeler         } else {
1582a9d85353SPeter Maydell             *data = s->bpr[cpu];
1583822e9cc3SFabian Aggeler         }
1584a9d85353SPeter Maydell         break;
1585e69954b9Spbrook     case 0x0c: /* Acknowledge */
1586c5619bf9SFabian Aggeler         *data = gic_acknowledge_irq(s, cpu, attrs);
1587a9d85353SPeter Maydell         break;
158866a0a2cbSDong Xu Wang     case 0x14: /* Running Priority */
158908efa9f2SFabian Aggeler         *data = gic_get_running_priority(s, cpu, attrs);
1590a9d85353SPeter Maydell         break;
1591e69954b9Spbrook     case 0x18: /* Highest Pending Interrupt */
15927c0fa108SFabian Aggeler         *data = gic_get_current_pending_irq(s, cpu, attrs);
1593a9d85353SPeter Maydell         break;
1594aa7d461aSChristoffer Dall     case 0x1c: /* Aliased Binary Point */
1595822e9cc3SFabian Aggeler         /* GIC v2, no security: ABPR
1596822e9cc3SFabian Aggeler          * GIC v1, no security: not implemented (RAZ/WI)
1597822e9cc3SFabian Aggeler          * With security extensions, secure access: ABPR (alias of NS BPR)
1598822e9cc3SFabian Aggeler          * With security extensions, nonsecure access: RAZ/WI
1599822e9cc3SFabian Aggeler          */
16003dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1601822e9cc3SFabian Aggeler             *data = 0;
1602822e9cc3SFabian Aggeler         } else {
1603a9d85353SPeter Maydell             *data = s->abpr[cpu];
1604822e9cc3SFabian Aggeler         }
1605a9d85353SPeter Maydell         break;
1606a9d477c4SChristoffer Dall     case 0xd0: case 0xd4: case 0xd8: case 0xdc:
160751fd06e0SPeter Maydell     {
160851fd06e0SPeter Maydell         int regno = (offset - 0xd0) / 4;
16097eb079ecSLuc Michel         int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
161051fd06e0SPeter Maydell 
16117eb079ecSLuc Michel         if (regno >= nr_aprs || s->revision != 2) {
161251fd06e0SPeter Maydell             *data = 0;
16137eb079ecSLuc Michel         } else if (gic_is_vcpu(cpu)) {
16147eb079ecSLuc Michel             *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
16153dd0471bSLuc Michel         } else if (gic_cpu_ns_access(s, cpu, attrs)) {
161651fd06e0SPeter Maydell             /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
161751fd06e0SPeter Maydell             *data = gic_apr_ns_view(s, regno, cpu);
161851fd06e0SPeter Maydell         } else {
161951fd06e0SPeter Maydell             *data = s->apr[regno][cpu];
162051fd06e0SPeter Maydell         }
1621a9d85353SPeter Maydell         break;
162251fd06e0SPeter Maydell     }
162351fd06e0SPeter Maydell     case 0xe0: case 0xe4: case 0xe8: case 0xec:
162451fd06e0SPeter Maydell     {
162551fd06e0SPeter Maydell         int regno = (offset - 0xe0) / 4;
162651fd06e0SPeter Maydell 
162751fd06e0SPeter Maydell         if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
16287eb079ecSLuc Michel             gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
162951fd06e0SPeter Maydell             *data = 0;
163051fd06e0SPeter Maydell         } else {
163151fd06e0SPeter Maydell             *data = s->nsapr[regno][cpu];
163251fd06e0SPeter Maydell         }
163351fd06e0SPeter Maydell         break;
163451fd06e0SPeter Maydell     }
1635e69954b9Spbrook     default:
16368c8dc39fSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
16378c8dc39fSPeter Maydell                       "gic_cpu_read: Bad offset %x\n", (int)offset);
16380cf09852SPeter Maydell         *data = 0;
16390cf09852SPeter Maydell         break;
1640e69954b9Spbrook     }
1641a9d85353SPeter Maydell     return MEMTX_OK;
1642e69954b9Spbrook }
1643e69954b9Spbrook 
1644a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1645a9d85353SPeter Maydell                                  uint32_t value, MemTxAttrs attrs)
1646e69954b9Spbrook {
1647e69954b9Spbrook     switch (offset) {
1648e69954b9Spbrook     case 0x00: /* Control */
164932951860SFabian Aggeler         gic_set_cpu_control(s, cpu, value, attrs);
1650e69954b9Spbrook         break;
1651e69954b9Spbrook     case 0x04: /* Priority mask */
165281508470SFabian Aggeler         gic_set_priority_mask(s, cpu, value, attrs);
1653e69954b9Spbrook         break;
1654e69954b9Spbrook     case 0x08: /* Binary Point */
16553dd0471bSLuc Michel         if (gic_cpu_ns_access(s, cpu, attrs)) {
1656421a3c22SLuc MICHEL             if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1657421a3c22SLuc MICHEL                 /* WI when CBPR is 1 */
1658421a3c22SLuc MICHEL                 return MEMTX_OK;
1659421a3c22SLuc MICHEL             } else {
1660822e9cc3SFabian Aggeler                 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1661421a3c22SLuc MICHEL             }
1662822e9cc3SFabian Aggeler         } else {
16637eb079ecSLuc Michel             int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
16647eb079ecSLuc Michel             s->bpr[cpu] = MAX(value & 0x7, min_bpr);
1665822e9cc3SFabian Aggeler         }
1666e69954b9Spbrook         break;
1667e69954b9Spbrook     case 0x10: /* End Of Interrupt */
1668f9c6a7f1SFabian Aggeler         gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1669a9d85353SPeter Maydell         return MEMTX_OK;
1670aa7d461aSChristoffer Dall     case 0x1c: /* Aliased Binary Point */
16713dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1672822e9cc3SFabian Aggeler             /* unimplemented, or NS access: RAZ/WI */
1673822e9cc3SFabian Aggeler             return MEMTX_OK;
1674822e9cc3SFabian Aggeler         } else {
1675822e9cc3SFabian Aggeler             s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1676aa7d461aSChristoffer Dall         }
1677aa7d461aSChristoffer Dall         break;
1678a9d477c4SChristoffer Dall     case 0xd0: case 0xd4: case 0xd8: case 0xdc:
167951fd06e0SPeter Maydell     {
168051fd06e0SPeter Maydell         int regno = (offset - 0xd0) / 4;
16817eb079ecSLuc Michel         int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
168251fd06e0SPeter Maydell 
16837eb079ecSLuc Michel         if (regno >= nr_aprs || s->revision != 2) {
168451fd06e0SPeter Maydell             return MEMTX_OK;
168551fd06e0SPeter Maydell         }
16867eb079ecSLuc Michel         if (gic_is_vcpu(cpu)) {
16877eb079ecSLuc Michel             s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
16887eb079ecSLuc Michel         } else if (gic_cpu_ns_access(s, cpu, attrs)) {
168951fd06e0SPeter Maydell             /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
169051fd06e0SPeter Maydell             gic_apr_write_ns_view(s, regno, cpu, value);
169151fd06e0SPeter Maydell         } else {
169251fd06e0SPeter Maydell             s->apr[regno][cpu] = value;
169351fd06e0SPeter Maydell         }
1694a9d477c4SChristoffer Dall         break;
169551fd06e0SPeter Maydell     }
169651fd06e0SPeter Maydell     case 0xe0: case 0xe4: case 0xe8: case 0xec:
169751fd06e0SPeter Maydell     {
169851fd06e0SPeter Maydell         int regno = (offset - 0xe0) / 4;
169951fd06e0SPeter Maydell 
170051fd06e0SPeter Maydell         if (regno >= GIC_NR_APRS || s->revision != 2) {
170151fd06e0SPeter Maydell             return MEMTX_OK;
170251fd06e0SPeter Maydell         }
17037eb079ecSLuc Michel         if (gic_is_vcpu(cpu)) {
17047eb079ecSLuc Michel             return MEMTX_OK;
17057eb079ecSLuc Michel         }
17063dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
170751fd06e0SPeter Maydell             return MEMTX_OK;
170851fd06e0SPeter Maydell         }
170951fd06e0SPeter Maydell         s->nsapr[regno][cpu] = value;
171051fd06e0SPeter Maydell         break;
171151fd06e0SPeter Maydell     }
1712a55c910eSPeter Maydell     case 0x1000:
1713a55c910eSPeter Maydell         /* GICC_DIR */
1714a55c910eSPeter Maydell         gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
1715a55c910eSPeter Maydell         break;
1716e69954b9Spbrook     default:
17178c8dc39fSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
17188c8dc39fSPeter Maydell                       "gic_cpu_write: Bad offset %x\n", (int)offset);
17190cf09852SPeter Maydell         return MEMTX_OK;
1720e69954b9Spbrook     }
1721cbe1282bSLuc Michel 
1722cbe1282bSLuc Michel     if (gic_is_vcpu(cpu)) {
1723cbe1282bSLuc Michel         gic_update_virt(s);
1724cbe1282bSLuc Michel     } else {
1725e69954b9Spbrook         gic_update(s);
1726cbe1282bSLuc Michel     }
1727cbe1282bSLuc Michel 
1728a9d85353SPeter Maydell     return MEMTX_OK;
1729e69954b9Spbrook }
1730e2c56465SPeter Maydell 
1731e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */
1732a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1733a9d85353SPeter Maydell                                     unsigned size, MemTxAttrs attrs)
1734e2c56465SPeter Maydell {
1735fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1736a9d85353SPeter Maydell     return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1737e2c56465SPeter Maydell }
1738e2c56465SPeter Maydell 
1739a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1740a9d85353SPeter Maydell                                      uint64_t value, unsigned size,
1741a9d85353SPeter Maydell                                      MemTxAttrs attrs)
1742e2c56465SPeter Maydell {
1743fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1744a9d85353SPeter Maydell     return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1745e2c56465SPeter Maydell }
1746e2c56465SPeter Maydell 
1747e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU.
1748fae15286SPeter Maydell  * These just decode the opaque pointer into GICState* + cpu id.
1749e2c56465SPeter Maydell  */
1750a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1751a9d85353SPeter Maydell                                    unsigned size, MemTxAttrs attrs)
1752e2c56465SPeter Maydell {
1753fae15286SPeter Maydell     GICState **backref = (GICState **)opaque;
1754fae15286SPeter Maydell     GICState *s = *backref;
1755e2c56465SPeter Maydell     int id = (backref - s->backref);
1756a9d85353SPeter Maydell     return gic_cpu_read(s, id, addr, data, attrs);
1757e2c56465SPeter Maydell }
1758e2c56465SPeter Maydell 
1759a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1760a9d85353SPeter Maydell                                     uint64_t value, unsigned size,
1761a9d85353SPeter Maydell                                     MemTxAttrs attrs)
1762e2c56465SPeter Maydell {
1763fae15286SPeter Maydell     GICState **backref = (GICState **)opaque;
1764fae15286SPeter Maydell     GICState *s = *backref;
1765e2c56465SPeter Maydell     int id = (backref - s->backref);
1766a9d85353SPeter Maydell     return gic_cpu_write(s, id, addr, value, attrs);
1767e2c56465SPeter Maydell }
1768e2c56465SPeter Maydell 
17692c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
17702c679ac7SLuc Michel                                     unsigned size, MemTxAttrs attrs)
17712c679ac7SLuc Michel {
17722c679ac7SLuc Michel     GICState *s = (GICState *)opaque;
17732c679ac7SLuc Michel 
17742c679ac7SLuc Michel     return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
17752c679ac7SLuc Michel }
17762c679ac7SLuc Michel 
17772c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
17782c679ac7SLuc Michel                                      uint64_t value, unsigned size,
17792c679ac7SLuc Michel                                      MemTxAttrs attrs)
17802c679ac7SLuc Michel {
17812c679ac7SLuc Michel     GICState *s = (GICState *)opaque;
17822c679ac7SLuc Michel 
17832c679ac7SLuc Michel     return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
17842c679ac7SLuc Michel }
17852c679ac7SLuc Michel 
1786527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
1787527d296fSLuc Michel {
1788527d296fSLuc Michel     int lr_idx;
1789527d296fSLuc Michel     uint32_t ret = 0;
1790527d296fSLuc Michel 
1791527d296fSLuc Michel     for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1792527d296fSLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
1793527d296fSLuc Michel         ret = deposit32(ret, lr_idx - lr_start, 1,
1794527d296fSLuc Michel                         gic_lr_entry_is_eoi(*entry));
1795527d296fSLuc Michel     }
1796527d296fSLuc Michel 
1797527d296fSLuc Michel     return ret;
1798527d296fSLuc Michel }
1799527d296fSLuc Michel 
1800527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
1801527d296fSLuc Michel {
1802527d296fSLuc Michel     int lr_idx;
1803527d296fSLuc Michel     uint32_t ret = 0;
1804527d296fSLuc Michel 
1805527d296fSLuc Michel     for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1806527d296fSLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
1807527d296fSLuc Michel         ret = deposit32(ret, lr_idx - lr_start, 1,
1808527d296fSLuc Michel                         gic_lr_entry_is_free(*entry));
1809527d296fSLuc Michel     }
1810527d296fSLuc Michel 
1811527d296fSLuc Michel     return ret;
1812527d296fSLuc Michel }
1813527d296fSLuc Michel 
1814527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
1815527d296fSLuc Michel {
1816527d296fSLuc Michel     int vcpu = gic_get_current_vcpu(s);
1817527d296fSLuc Michel     uint32_t ctlr;
1818527d296fSLuc Michel     uint32_t abpr;
1819527d296fSLuc Michel     uint32_t bpr;
1820527d296fSLuc Michel     uint32_t prio_mask;
1821527d296fSLuc Michel 
1822527d296fSLuc Michel     ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
1823527d296fSLuc Michel     abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
1824527d296fSLuc Michel     bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
1825527d296fSLuc Michel     prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
1826527d296fSLuc Michel 
1827527d296fSLuc Michel     gic_set_cpu_control(s, vcpu, ctlr, attrs);
1828527d296fSLuc Michel     s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
1829527d296fSLuc Michel     s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
1830527d296fSLuc Michel     gic_set_priority_mask(s, vcpu, prio_mask, attrs);
1831527d296fSLuc Michel }
1832527d296fSLuc Michel 
1833527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
1834527d296fSLuc Michel                                 uint64_t *data, MemTxAttrs attrs)
1835527d296fSLuc Michel {
1836527d296fSLuc Michel     GICState *s = ARM_GIC(opaque);
1837527d296fSLuc Michel     int vcpu = cpu + GIC_NCPU;
1838527d296fSLuc Michel 
1839527d296fSLuc Michel     switch (addr) {
1840527d296fSLuc Michel     case A_GICH_HCR: /* Hypervisor Control */
1841527d296fSLuc Michel         *data = s->h_hcr[cpu];
1842527d296fSLuc Michel         break;
1843527d296fSLuc Michel 
1844527d296fSLuc Michel     case A_GICH_VTR: /* VGIC Type */
1845527d296fSLuc Michel         *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
1846527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VTR, PREbits,
1847527d296fSLuc Michel                            GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
1848527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
1849527d296fSLuc Michel                            (7 - GIC_VIRT_MIN_BPR) - 1);
1850527d296fSLuc Michel         break;
1851527d296fSLuc Michel 
1852527d296fSLuc Michel     case A_GICH_VMCR: /* Virtual Machine Control */
1853527d296fSLuc Michel         *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
1854527d296fSLuc Michel                            extract32(s->cpu_ctlr[vcpu], 0, 10));
1855527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
1856527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
1857527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
1858527d296fSLuc Michel                            extract32(s->priority_mask[vcpu], 3, 5));
1859527d296fSLuc Michel         break;
1860527d296fSLuc Michel 
1861527d296fSLuc Michel     case A_GICH_MISR: /* Maintenance Interrupt Status */
1862527d296fSLuc Michel         *data = s->h_misr[cpu];
1863527d296fSLuc Michel         break;
1864527d296fSLuc Michel 
1865527d296fSLuc Michel     case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */
1866527d296fSLuc Michel     case A_GICH_EISR1:
1867527d296fSLuc Michel         *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
1868527d296fSLuc Michel         break;
1869527d296fSLuc Michel 
1870527d296fSLuc Michel     case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */
1871527d296fSLuc Michel     case A_GICH_ELRSR1:
1872527d296fSLuc Michel         *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
1873527d296fSLuc Michel         break;
1874527d296fSLuc Michel 
1875527d296fSLuc Michel     case A_GICH_APR: /* Active Priorities */
1876527d296fSLuc Michel         *data = s->h_apr[cpu];
1877527d296fSLuc Michel         break;
1878527d296fSLuc Michel 
1879527d296fSLuc Michel     case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1880527d296fSLuc Michel     {
1881527d296fSLuc Michel         int lr_idx = (addr - A_GICH_LR0) / 4;
1882527d296fSLuc Michel 
1883527d296fSLuc Michel         if (lr_idx > s->num_lrs) {
1884527d296fSLuc Michel             *data = 0;
1885527d296fSLuc Michel         } else {
1886527d296fSLuc Michel             *data = s->h_lr[lr_idx][cpu];
1887527d296fSLuc Michel         }
1888527d296fSLuc Michel         break;
1889527d296fSLuc Michel     }
1890527d296fSLuc Michel 
1891527d296fSLuc Michel     default:
1892527d296fSLuc Michel         qemu_log_mask(LOG_GUEST_ERROR,
1893527d296fSLuc Michel                       "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
1894527d296fSLuc Michel         return MEMTX_OK;
1895527d296fSLuc Michel     }
1896527d296fSLuc Michel 
1897527d296fSLuc Michel     return MEMTX_OK;
1898527d296fSLuc Michel }
1899527d296fSLuc Michel 
1900527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
1901527d296fSLuc Michel                                  uint64_t value, MemTxAttrs attrs)
1902527d296fSLuc Michel {
1903527d296fSLuc Michel     GICState *s = ARM_GIC(opaque);
1904527d296fSLuc Michel     int vcpu = cpu + GIC_NCPU;
1905527d296fSLuc Michel 
1906527d296fSLuc Michel     switch (addr) {
1907527d296fSLuc Michel     case A_GICH_HCR: /* Hypervisor Control */
1908527d296fSLuc Michel         s->h_hcr[cpu] = value & GICH_HCR_MASK;
1909527d296fSLuc Michel         break;
1910527d296fSLuc Michel 
1911527d296fSLuc Michel     case A_GICH_VMCR: /* Virtual Machine Control */
1912527d296fSLuc Michel         gic_vmcr_write(s, value, attrs);
1913527d296fSLuc Michel         break;
1914527d296fSLuc Michel 
1915527d296fSLuc Michel     case A_GICH_APR: /* Active Priorities */
1916527d296fSLuc Michel         s->h_apr[cpu] = value;
1917527d296fSLuc Michel         s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
1918527d296fSLuc Michel         break;
1919527d296fSLuc Michel 
1920527d296fSLuc Michel     case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1921527d296fSLuc Michel     {
1922527d296fSLuc Michel         int lr_idx = (addr - A_GICH_LR0) / 4;
1923527d296fSLuc Michel 
1924527d296fSLuc Michel         if (lr_idx > s->num_lrs) {
1925527d296fSLuc Michel             return MEMTX_OK;
1926527d296fSLuc Michel         }
1927527d296fSLuc Michel 
1928527d296fSLuc Michel         s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
1929527d296fSLuc Michel         break;
1930527d296fSLuc Michel     }
1931527d296fSLuc Michel 
1932527d296fSLuc Michel     default:
1933527d296fSLuc Michel         qemu_log_mask(LOG_GUEST_ERROR,
1934527d296fSLuc Michel                       "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
1935527d296fSLuc Michel         return MEMTX_OK;
1936527d296fSLuc Michel     }
1937527d296fSLuc Michel 
1938cbe1282bSLuc Michel     gic_update_virt(s);
1939527d296fSLuc Michel     return MEMTX_OK;
1940527d296fSLuc Michel }
1941527d296fSLuc Michel 
1942527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1943527d296fSLuc Michel                                     unsigned size, MemTxAttrs attrs)
1944527d296fSLuc Michel {
1945527d296fSLuc Michel     GICState *s = (GICState *)opaque;
1946527d296fSLuc Michel 
1947527d296fSLuc Michel     return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
1948527d296fSLuc Michel }
1949527d296fSLuc Michel 
1950527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
1951527d296fSLuc Michel                                      uint64_t value, unsigned size,
1952527d296fSLuc Michel                                      MemTxAttrs attrs)
1953527d296fSLuc Michel {
1954527d296fSLuc Michel     GICState *s = (GICState *)opaque;
1955527d296fSLuc Michel 
1956527d296fSLuc Michel     return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
1957527d296fSLuc Michel }
1958527d296fSLuc Michel 
1959527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1960527d296fSLuc Michel                                     unsigned size, MemTxAttrs attrs)
1961527d296fSLuc Michel {
1962527d296fSLuc Michel     GICState **backref = (GICState **)opaque;
1963527d296fSLuc Michel     GICState *s = *backref;
1964527d296fSLuc Michel     int id = (backref - s->backref);
1965527d296fSLuc Michel 
1966527d296fSLuc Michel     return gic_hyp_read(s, id, addr, data, attrs);
1967527d296fSLuc Michel }
1968527d296fSLuc Michel 
1969527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
1970527d296fSLuc Michel                                      uint64_t value, unsigned size,
1971527d296fSLuc Michel                                      MemTxAttrs attrs)
1972527d296fSLuc Michel {
1973527d296fSLuc Michel     GICState **backref = (GICState **)opaque;
1974527d296fSLuc Michel     GICState *s = *backref;
1975527d296fSLuc Michel     int id = (backref - s->backref);
1976527d296fSLuc Michel 
1977527d296fSLuc Michel     return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
1978527d296fSLuc Michel 
1979527d296fSLuc Michel }
1980527d296fSLuc Michel 
19817926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = {
19827926c210SPavel Fedin     {
19837926c210SPavel Fedin         .read_with_attrs = gic_dist_read,
19847926c210SPavel Fedin         .write_with_attrs = gic_dist_write,
19857926c210SPavel Fedin         .endianness = DEVICE_NATIVE_ENDIAN,
19867926c210SPavel Fedin     },
19877926c210SPavel Fedin     {
1988a9d85353SPeter Maydell         .read_with_attrs = gic_thiscpu_read,
1989a9d85353SPeter Maydell         .write_with_attrs = gic_thiscpu_write,
1990e2c56465SPeter Maydell         .endianness = DEVICE_NATIVE_ENDIAN,
19917926c210SPavel Fedin     }
1992e2c56465SPeter Maydell };
1993e2c56465SPeter Maydell 
1994e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = {
1995a9d85353SPeter Maydell     .read_with_attrs = gic_do_cpu_read,
1996a9d85353SPeter Maydell     .write_with_attrs = gic_do_cpu_write,
1997e2c56465SPeter Maydell     .endianness = DEVICE_NATIVE_ENDIAN,
1998e2c56465SPeter Maydell };
1999e69954b9Spbrook 
20002c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = {
20012c679ac7SLuc Michel     {
2002527d296fSLuc Michel         .read_with_attrs = gic_thiscpu_hyp_read,
2003527d296fSLuc Michel         .write_with_attrs = gic_thiscpu_hyp_write,
20042c679ac7SLuc Michel         .endianness = DEVICE_NATIVE_ENDIAN,
20052c679ac7SLuc Michel     },
20062c679ac7SLuc Michel     {
20072c679ac7SLuc Michel         .read_with_attrs = gic_thisvcpu_read,
20082c679ac7SLuc Michel         .write_with_attrs = gic_thisvcpu_write,
20092c679ac7SLuc Michel         .endianness = DEVICE_NATIVE_ENDIAN,
20102c679ac7SLuc Michel     }
20112c679ac7SLuc Michel };
20122c679ac7SLuc Michel 
2013527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = {
2014527d296fSLuc Michel     .read_with_attrs = gic_do_hyp_read,
2015527d296fSLuc Michel     .write_with_attrs = gic_do_hyp_write,
2016527d296fSLuc Michel     .endianness = DEVICE_NATIVE_ENDIAN,
2017527d296fSLuc Michel };
2018527d296fSLuc Michel 
201953111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp)
20202b518c56SPeter Maydell {
202153111180SPeter Maydell     /* Device instance realize function for the GIC sysbus device */
20222b518c56SPeter Maydell     int i;
202353111180SPeter Maydell     GICState *s = ARM_GIC(dev);
202453111180SPeter Maydell     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
20251e8cae4dSPeter Maydell     ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
20260175ba10SMarkus Armbruster     Error *local_err = NULL;
20271e8cae4dSPeter Maydell 
20280175ba10SMarkus Armbruster     agc->parent_realize(dev, &local_err);
20290175ba10SMarkus Armbruster     if (local_err) {
20300175ba10SMarkus Armbruster         error_propagate(errp, local_err);
203153111180SPeter Maydell         return;
203253111180SPeter Maydell     }
20331e8cae4dSPeter Maydell 
20345d721b78SAlexander Graf     if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
20355d721b78SAlexander Graf         error_setg(errp, "KVM with user space irqchip only works when the "
20365d721b78SAlexander Graf                          "host kernel supports KVM_CAP_ARM_USER_IRQ");
20375d721b78SAlexander Graf         return;
20385d721b78SAlexander Graf     }
20395d721b78SAlexander Graf 
20402c679ac7SLuc Michel     /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if
20412c679ac7SLuc Michel      * enabled, virtualization extensions related interfaces (main virtual
20422c679ac7SLuc Michel      * interface (s->vifaceiomem[0]) and virtual CPU interface).
20432c679ac7SLuc Michel      */
20442c679ac7SLuc Michel     gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
20452b518c56SPeter Maydell 
20467926c210SPavel Fedin     /* Extra core-specific regions for the CPU interfaces. This is
20477926c210SPavel Fedin      * necessary for "franken-GIC" implementations, for example on
20487926c210SPavel Fedin      * Exynos 4.
2049e2c56465SPeter Maydell      * NB that the memory region size of 0x100 applies for the 11MPCore
2050e2c56465SPeter Maydell      * and also cores following the GIC v1 spec (ie A9).
2051e2c56465SPeter Maydell      * GIC v2 defines a larger memory region (0x1000) so this will need
2052e2c56465SPeter Maydell      * to be extended when we implement A15.
2053e2c56465SPeter Maydell      */
2054b95690c9SWei Huang     for (i = 0; i < s->num_cpu; i++) {
2055e2c56465SPeter Maydell         s->backref[i] = s;
20561437c94bSPaolo Bonzini         memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
20571437c94bSPaolo Bonzini                               &s->backref[i], "gic_cpu", 0x100);
20587926c210SPavel Fedin         sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
2059496dbcd1SPeter Maydell     }
2060527d296fSLuc Michel 
2061527d296fSLuc Michel     /* Extra core-specific regions for virtual interfaces. This is required by
2062527d296fSLuc Michel      * the GICv2 specification.
2063527d296fSLuc Michel      */
2064527d296fSLuc Michel     if (s->virt_extn) {
2065527d296fSLuc Michel         for (i = 0; i < s->num_cpu; i++) {
2066527d296fSLuc Michel             memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
2067527d296fSLuc Michel                                   &gic_viface_ops, &s->backref[i],
2068527d296fSLuc Michel                                   "gic_viface", 0x1000);
2069527d296fSLuc Michel             sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
2070527d296fSLuc Michel         }
2071527d296fSLuc Michel     }
2072527d296fSLuc Michel 
2073496dbcd1SPeter Maydell }
2074496dbcd1SPeter Maydell 
2075496dbcd1SPeter Maydell static void arm_gic_class_init(ObjectClass *klass, void *data)
2076496dbcd1SPeter Maydell {
2077496dbcd1SPeter Maydell     DeviceClass *dc = DEVICE_CLASS(klass);
20781e8cae4dSPeter Maydell     ARMGICClass *agc = ARM_GIC_CLASS(klass);
207953111180SPeter Maydell 
2080bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
2081496dbcd1SPeter Maydell }
2082496dbcd1SPeter Maydell 
20838c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = {
20841e8cae4dSPeter Maydell     .name = TYPE_ARM_GIC,
20851e8cae4dSPeter Maydell     .parent = TYPE_ARM_GIC_COMMON,
2086fae15286SPeter Maydell     .instance_size = sizeof(GICState),
2087496dbcd1SPeter Maydell     .class_init = arm_gic_class_init,
2088998a74bcSPeter Maydell     .class_size = sizeof(ARMGICClass),
2089496dbcd1SPeter Maydell };
2090496dbcd1SPeter Maydell 
2091496dbcd1SPeter Maydell static void arm_gic_register_types(void)
2092496dbcd1SPeter Maydell {
2093496dbcd1SPeter Maydell     type_register_static(&arm_gic_info);
2094496dbcd1SPeter Maydell }
2095496dbcd1SPeter Maydell 
2096496dbcd1SPeter Maydell type_init(arm_gic_register_types)
2097