xref: /qemu/hw/intc/arm_gic.c (revision b6e6c65151efb72063a9dd62fbd5baa744d4ab30)
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) {
187067a2b9cSLuc Michel             trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu,
188067a2b9cSLuc Michel                                      best_irq, best_prio,
189067a2b9cSLuc Michel                                      s->priority_mask[cpu_iface],
190067a2b9cSLuc Michel                                      s->running_priority[cpu_iface]);
1912531088fSHollis Blanchard         }
1922531088fSHollis Blanchard 
193dadbb58fSPeter Maydell         irq_level = fiq_level = 0;
194dadbb58fSPeter Maydell 
195cbe1282bSLuc Michel         if (best_prio < s->priority_mask[cpu_iface]) {
196cbe1282bSLuc Michel             s->current_pending[cpu_iface] = best_irq;
197cbe1282bSLuc Michel             if (best_prio < s->running_priority[cpu_iface]) {
198cbe1282bSLuc Michel                 if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) {
199cbe1282bSLuc Michel                     if (group == 0 &&
200cbe1282bSLuc Michel                         s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) {
201dadbb58fSPeter Maydell                         DPRINTF("Raised pending FIQ %d (cpu %d)\n",
202cbe1282bSLuc Michel                                 best_irq, cpu_iface);
203dadbb58fSPeter Maydell                         fiq_level = 1;
204cbe1282bSLuc Michel                         trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq",
205cbe1282bSLuc Michel                                                  fiq_level);
206dadbb58fSPeter Maydell                     } else {
207dadbb58fSPeter Maydell                         DPRINTF("Raised pending IRQ %d (cpu %d)\n",
208cbe1282bSLuc Michel                                 best_irq, cpu_iface);
209dadbb58fSPeter Maydell                         irq_level = 1;
210cbe1282bSLuc Michel                         trace_gic_update_set_irq(cpu, virt ? "virq" : "irq",
211cbe1282bSLuc Michel                                                  irq_level);
212e69954b9Spbrook                     }
213e69954b9Spbrook                 }
214dadbb58fSPeter Maydell             }
215dadbb58fSPeter Maydell         }
216dadbb58fSPeter Maydell 
217cbe1282bSLuc Michel         qemu_set_irq(irq_lines[cpu], irq_level);
218cbe1282bSLuc Michel         qemu_set_irq(fiq_lines[cpu], fiq_level);
2199ee6e8bbSpbrook     }
220e69954b9Spbrook }
221e69954b9Spbrook 
222cbe1282bSLuc Michel static void gic_update(GICState *s)
223cbe1282bSLuc Michel {
224cbe1282bSLuc Michel     gic_update_internal(s, false);
225cbe1282bSLuc Michel }
226cbe1282bSLuc Michel 
227527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit
228527d296fSLuc Michel  * in ELRSR is set.
229527d296fSLuc Michel  */
230527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry)
231527d296fSLuc Michel {
232527d296fSLuc Michel     return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
233527d296fSLuc Michel         && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry));
234527d296fSLuc Michel }
235527d296fSLuc Michel 
236527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the
237527d296fSLuc Michel  * corrsponding bit in EISR is set.
238527d296fSLuc Michel  */
239527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry)
240527d296fSLuc Michel {
241527d296fSLuc Michel     return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
242527d296fSLuc Michel         && !GICH_LR_HW(entry) && GICH_LR_EOI(entry);
243527d296fSLuc Michel }
244527d296fSLuc Michel 
24550e57926SLuc Michel static inline void gic_extract_lr_info(GICState *s, int cpu,
24650e57926SLuc Michel                                 int *num_eoi, int *num_valid, int *num_pending)
24750e57926SLuc Michel {
24850e57926SLuc Michel     int lr_idx;
24950e57926SLuc Michel 
25050e57926SLuc Michel     *num_eoi = 0;
25150e57926SLuc Michel     *num_valid = 0;
25250e57926SLuc Michel     *num_pending = 0;
25350e57926SLuc Michel 
25450e57926SLuc Michel     for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
25550e57926SLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
25650e57926SLuc Michel 
25750e57926SLuc Michel         if (gic_lr_entry_is_eoi(*entry)) {
25850e57926SLuc Michel             (*num_eoi)++;
25950e57926SLuc Michel         }
26050e57926SLuc Michel 
26150e57926SLuc Michel         if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) {
26250e57926SLuc Michel             (*num_valid)++;
26350e57926SLuc Michel         }
26450e57926SLuc Michel 
26550e57926SLuc Michel         if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) {
26650e57926SLuc Michel             (*num_pending)++;
26750e57926SLuc Michel         }
26850e57926SLuc Michel     }
26950e57926SLuc Michel }
27050e57926SLuc Michel 
27150e57926SLuc Michel static void gic_compute_misr(GICState *s, int cpu)
27250e57926SLuc Michel {
27350e57926SLuc Michel     uint32_t value = 0;
27450e57926SLuc Michel     int vcpu = cpu + GIC_NCPU;
27550e57926SLuc Michel 
27650e57926SLuc Michel     int num_eoi, num_valid, num_pending;
27750e57926SLuc Michel 
27850e57926SLuc Michel     gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending);
27950e57926SLuc Michel 
28050e57926SLuc Michel     /* EOI */
28150e57926SLuc Michel     if (num_eoi) {
28250e57926SLuc Michel         value |= R_GICH_MISR_EOI_MASK;
28350e57926SLuc Michel     }
28450e57926SLuc Michel 
28550e57926SLuc Michel     /* U: true if only 0 or 1 LR entry is valid */
28650e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) {
28750e57926SLuc Michel         value |= R_GICH_MISR_U_MASK;
28850e57926SLuc Michel     }
28950e57926SLuc Michel 
29050e57926SLuc Michel     /* LRENP: EOICount is not 0 */
29150e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) &&
29250e57926SLuc Michel         ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) {
29350e57926SLuc Michel         value |= R_GICH_MISR_LRENP_MASK;
29450e57926SLuc Michel     }
29550e57926SLuc Michel 
29650e57926SLuc Michel     /* NP: no pending interrupts */
29750e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) {
29850e57926SLuc Michel         value |= R_GICH_MISR_NP_MASK;
29950e57926SLuc Michel     }
30050e57926SLuc Michel 
30150e57926SLuc Michel     /* VGrp0E: group0 virq signaling enabled */
30250e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) &&
30350e57926SLuc Michel         (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
30450e57926SLuc Michel         value |= R_GICH_MISR_VGrp0E_MASK;
30550e57926SLuc Michel     }
30650e57926SLuc Michel 
30750e57926SLuc Michel     /* VGrp0D: group0 virq signaling disabled */
30850e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) &&
30950e57926SLuc Michel         !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
31050e57926SLuc Michel         value |= R_GICH_MISR_VGrp0D_MASK;
31150e57926SLuc Michel     }
31250e57926SLuc Michel 
31350e57926SLuc Michel     /* VGrp1E: group1 virq signaling enabled */
31450e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) &&
31550e57926SLuc Michel         (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
31650e57926SLuc Michel         value |= R_GICH_MISR_VGrp1E_MASK;
31750e57926SLuc Michel     }
31850e57926SLuc Michel 
31950e57926SLuc Michel     /* VGrp1D: group1 virq signaling disabled */
32050e57926SLuc Michel     if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) &&
32150e57926SLuc Michel         !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
32250e57926SLuc Michel         value |= R_GICH_MISR_VGrp1D_MASK;
32350e57926SLuc Michel     }
32450e57926SLuc Michel 
32550e57926SLuc Michel     s->h_misr[cpu] = value;
32650e57926SLuc Michel }
32750e57926SLuc Michel 
32850e57926SLuc Michel static void gic_update_maintenance(GICState *s)
32950e57926SLuc Michel {
33050e57926SLuc Michel     int cpu = 0;
33150e57926SLuc Michel     int maint_level;
33250e57926SLuc Michel 
33350e57926SLuc Michel     for (cpu = 0; cpu < s->num_cpu; cpu++) {
33450e57926SLuc Michel         gic_compute_misr(s, cpu);
33550e57926SLuc Michel         maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu];
33650e57926SLuc Michel 
337067a2b9cSLuc Michel         trace_gic_update_maintenance_irq(cpu, maint_level);
33850e57926SLuc Michel         qemu_set_irq(s->maintenance_irq[cpu], maint_level);
33950e57926SLuc Michel     }
34050e57926SLuc Michel }
34150e57926SLuc Michel 
342cbe1282bSLuc Michel static void gic_update_virt(GICState *s)
343cbe1282bSLuc Michel {
344cbe1282bSLuc Michel     gic_update_internal(s, true);
34550e57926SLuc Michel     gic_update_maintenance(s);
346cbe1282bSLuc Michel }
347cbe1282bSLuc Michel 
3488d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
3498d999995SChristoffer Dall                                  int cm, int target)
3508d999995SChristoffer Dall {
3518d999995SChristoffer Dall     if (level) {
35267ce697aSLuc Michel         GIC_DIST_SET_LEVEL(irq, cm);
35367ce697aSLuc Michel         if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) {
3548d999995SChristoffer Dall             DPRINTF("Set %d pending mask %x\n", irq, target);
35567ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, target);
3568d999995SChristoffer Dall         }
3578d999995SChristoffer Dall     } else {
35867ce697aSLuc Michel         GIC_DIST_CLEAR_LEVEL(irq, cm);
3598d999995SChristoffer Dall     }
3608d999995SChristoffer Dall }
3618d999995SChristoffer Dall 
3628d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level,
3638d999995SChristoffer Dall                                 int cm, int target)
3648d999995SChristoffer Dall {
3658d999995SChristoffer Dall     if (level) {
36667ce697aSLuc Michel         GIC_DIST_SET_LEVEL(irq, cm);
3678d999995SChristoffer Dall         DPRINTF("Set %d pending mask %x\n", irq, target);
36867ce697aSLuc Michel         if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) {
36967ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, target);
3708d999995SChristoffer Dall         }
3718d999995SChristoffer Dall     } else {
37267ce697aSLuc Michel         GIC_DIST_CLEAR_LEVEL(irq, cm);
3738d999995SChristoffer Dall     }
3748d999995SChristoffer Dall }
3758d999995SChristoffer Dall 
3769ee6e8bbSpbrook /* Process a change in an external IRQ input.  */
377e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level)
378e69954b9Spbrook {
379544d1afaSPeter Maydell     /* Meaning of the 'irq' parameter:
380544d1afaSPeter Maydell      *  [0..N-1] : external interrupts
381544d1afaSPeter Maydell      *  [N..N+31] : PPI (internal) interrupts for CPU 0
382544d1afaSPeter Maydell      *  [N+32..N+63] : PPI (internal interrupts for CPU 1
383544d1afaSPeter Maydell      *  ...
384544d1afaSPeter Maydell      */
385fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
386544d1afaSPeter Maydell     int cm, target;
387544d1afaSPeter Maydell     if (irq < (s->num_irq - GIC_INTERNAL)) {
388e69954b9Spbrook         /* The first external input line is internal interrupt 32.  */
389544d1afaSPeter Maydell         cm = ALL_CPU_MASK;
39069253800SRusty Russell         irq += GIC_INTERNAL;
39167ce697aSLuc Michel         target = GIC_DIST_TARGET(irq);
392544d1afaSPeter Maydell     } else {
393544d1afaSPeter Maydell         int cpu;
394544d1afaSPeter Maydell         irq -= (s->num_irq - GIC_INTERNAL);
395544d1afaSPeter Maydell         cpu = irq / GIC_INTERNAL;
396544d1afaSPeter Maydell         irq %= GIC_INTERNAL;
397544d1afaSPeter Maydell         cm = 1 << cpu;
398544d1afaSPeter Maydell         target = cm;
399544d1afaSPeter Maydell     }
400544d1afaSPeter Maydell 
40140d22500SChristoffer Dall     assert(irq >= GIC_NR_SGIS);
40240d22500SChristoffer Dall 
40367ce697aSLuc Michel     if (level == GIC_DIST_TEST_LEVEL(irq, cm)) {
404e69954b9Spbrook         return;
405544d1afaSPeter Maydell     }
406e69954b9Spbrook 
4073bc4b52cSMarcin Krzeminski     if (s->revision == REV_11MPCORE) {
4088d999995SChristoffer Dall         gic_set_irq_11mpcore(s, irq, level, cm, target);
409e69954b9Spbrook     } else {
4108d999995SChristoffer Dall         gic_set_irq_generic(s, irq, level, cm, target);
411e69954b9Spbrook     }
4122531088fSHollis Blanchard     trace_gic_set_irq(irq, level, cm, target);
4138d999995SChristoffer Dall 
414e69954b9Spbrook     gic_update(s);
415e69954b9Spbrook }
416e69954b9Spbrook 
4177c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
4187c0fa108SFabian Aggeler                                             MemTxAttrs attrs)
4197c0fa108SFabian Aggeler {
4207c0fa108SFabian Aggeler     uint16_t pending_irq = s->current_pending[cpu];
4217c0fa108SFabian Aggeler 
4227c0fa108SFabian Aggeler     if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
42386b350f0SLuc Michel         int group = gic_test_group(s, pending_irq, cpu);
42486b350f0SLuc Michel 
4257c0fa108SFabian Aggeler         /* On a GIC without the security extensions, reading this register
4267c0fa108SFabian Aggeler          * behaves in the same way as a secure access to a GIC with them.
4277c0fa108SFabian Aggeler          */
4283dd0471bSLuc Michel         bool secure = !gic_cpu_ns_access(s, cpu, attrs);
4297c0fa108SFabian Aggeler 
4307c0fa108SFabian Aggeler         if (group == 0 && !secure) {
4317c0fa108SFabian Aggeler             /* Group0 interrupts hidden from Non-secure access */
4327c0fa108SFabian Aggeler             return 1023;
4337c0fa108SFabian Aggeler         }
4347c0fa108SFabian Aggeler         if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
4357c0fa108SFabian Aggeler             /* Group1 interrupts only seen by Secure access if
4367c0fa108SFabian Aggeler              * AckCtl bit set.
4377c0fa108SFabian Aggeler              */
4387c0fa108SFabian Aggeler             return 1022;
4397c0fa108SFabian Aggeler         }
4407c0fa108SFabian Aggeler     }
4417c0fa108SFabian Aggeler     return pending_irq;
4427c0fa108SFabian Aggeler }
4437c0fa108SFabian Aggeler 
444df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq)
445df92cfa6SPeter Maydell {
446df92cfa6SPeter Maydell     /* Return the group priority of the specified interrupt
447df92cfa6SPeter Maydell      * (which is the top bits of its priority, with the number
448df92cfa6SPeter Maydell      * of bits masked determined by the applicable binary point register).
449df92cfa6SPeter Maydell      */
450df92cfa6SPeter Maydell     int bpr;
451df92cfa6SPeter Maydell     uint32_t mask;
452df92cfa6SPeter Maydell 
453df92cfa6SPeter Maydell     if (gic_has_groups(s) &&
454df92cfa6SPeter Maydell         !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
45586b350f0SLuc Michel         gic_test_group(s, irq, cpu)) {
456fc05a6f2SLuc MICHEL         bpr = s->abpr[cpu] - 1;
457fc05a6f2SLuc MICHEL         assert(bpr >= 0);
458df92cfa6SPeter Maydell     } else {
459df92cfa6SPeter Maydell         bpr = s->bpr[cpu];
460df92cfa6SPeter Maydell     }
461df92cfa6SPeter Maydell 
462df92cfa6SPeter Maydell     /* a BPR of 0 means the group priority bits are [7:1];
463df92cfa6SPeter Maydell      * a BPR of 1 means they are [7:2], and so on down to
464df92cfa6SPeter Maydell      * a BPR of 7 meaning no group priority bits at all.
465df92cfa6SPeter Maydell      */
466df92cfa6SPeter Maydell     mask = ~0U << ((bpr & 7) + 1);
467df92cfa6SPeter Maydell 
46886b350f0SLuc Michel     return gic_get_priority(s, irq, cpu) & mask;
469df92cfa6SPeter Maydell }
470df92cfa6SPeter Maydell 
47172889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq)
472e69954b9Spbrook {
47372889c8aSPeter Maydell     /* Set the appropriate Active Priority Register bit for this IRQ,
47472889c8aSPeter Maydell      * and update the running priority.
47572889c8aSPeter Maydell      */
47672889c8aSPeter Maydell     int prio = gic_get_group_priority(s, cpu, irq);
477a1d7b8d8SLuc Michel     int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
478a1d7b8d8SLuc Michel     int preemption_level = prio >> (min_bpr + 1);
47972889c8aSPeter Maydell     int regno = preemption_level / 32;
48072889c8aSPeter Maydell     int bitno = preemption_level % 32;
481a1d7b8d8SLuc Michel     uint32_t *papr = NULL;
48272889c8aSPeter Maydell 
483a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
484a1d7b8d8SLuc Michel         assert(regno == 0);
485a1d7b8d8SLuc Michel         papr = &s->h_apr[gic_get_vcpu_real_id(cpu)];
486a1d7b8d8SLuc Michel     } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) {
487a1d7b8d8SLuc Michel         papr = &s->nsapr[regno][cpu];
4889ee6e8bbSpbrook     } else {
489a1d7b8d8SLuc Michel         papr = &s->apr[regno][cpu];
4909ee6e8bbSpbrook     }
49172889c8aSPeter Maydell 
492a1d7b8d8SLuc Michel     *papr |= (1 << bitno);
493a1d7b8d8SLuc Michel 
49472889c8aSPeter Maydell     s->running_priority[cpu] = prio;
49586b350f0SLuc Michel     gic_set_active(s, irq, cpu);
49672889c8aSPeter Maydell }
49772889c8aSPeter Maydell 
49872889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
49972889c8aSPeter Maydell {
50072889c8aSPeter Maydell     /* Recalculate the current running priority for this CPU based
50172889c8aSPeter Maydell      * on the set bits in the Active Priority Registers.
50272889c8aSPeter Maydell      */
50372889c8aSPeter Maydell     int i;
504a1d7b8d8SLuc Michel 
505a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
506a1d7b8d8SLuc Michel         uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)];
507a1d7b8d8SLuc Michel         if (apr) {
508a1d7b8d8SLuc Michel             return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1);
509a1d7b8d8SLuc Michel         } else {
510a1d7b8d8SLuc Michel             return 0x100;
511a1d7b8d8SLuc Michel         }
512a1d7b8d8SLuc Michel     }
513a1d7b8d8SLuc Michel 
51472889c8aSPeter Maydell     for (i = 0; i < GIC_NR_APRS; i++) {
51572889c8aSPeter Maydell         uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
51672889c8aSPeter Maydell         if (!apr) {
51772889c8aSPeter Maydell             continue;
51872889c8aSPeter Maydell         }
51972889c8aSPeter Maydell         return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
52072889c8aSPeter Maydell     }
52172889c8aSPeter Maydell     return 0x100;
52272889c8aSPeter Maydell }
52372889c8aSPeter Maydell 
52472889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group)
52572889c8aSPeter Maydell {
52672889c8aSPeter Maydell     /* Drop the priority of the currently active interrupt in the
52772889c8aSPeter Maydell      * specified group.
52872889c8aSPeter Maydell      *
52972889c8aSPeter Maydell      * Note that we can guarantee (because of the requirement to nest
53072889c8aSPeter Maydell      * GICC_IAR reads [which activate an interrupt and raise priority]
53172889c8aSPeter Maydell      * with GICC_EOIR writes [which drop the priority for the interrupt])
53272889c8aSPeter Maydell      * that the interrupt we're being called for is the highest priority
53372889c8aSPeter Maydell      * active interrupt, meaning that it has the lowest set bit in the
53472889c8aSPeter Maydell      * APR registers.
53572889c8aSPeter Maydell      *
53672889c8aSPeter Maydell      * If the guest does not honour the ordering constraints then the
53772889c8aSPeter Maydell      * behaviour of the GIC is UNPREDICTABLE, which for us means that
53872889c8aSPeter Maydell      * the values of the APR registers might become incorrect and the
53972889c8aSPeter Maydell      * running priority will be wrong, so interrupts that should preempt
54072889c8aSPeter Maydell      * might not do so, and interrupts that should not preempt might do so.
54172889c8aSPeter Maydell      */
542a1d7b8d8SLuc Michel     if (gic_is_vcpu(cpu)) {
543a1d7b8d8SLuc Michel         int rcpu = gic_get_vcpu_real_id(cpu);
544a1d7b8d8SLuc Michel 
545a1d7b8d8SLuc Michel         if (s->h_apr[rcpu]) {
546a1d7b8d8SLuc Michel             /* Clear lowest set bit */
547a1d7b8d8SLuc Michel             s->h_apr[rcpu] &= s->h_apr[rcpu] - 1;
548a1d7b8d8SLuc Michel         }
549a1d7b8d8SLuc Michel     } else {
55072889c8aSPeter Maydell         int i;
55172889c8aSPeter Maydell 
55272889c8aSPeter Maydell         for (i = 0; i < GIC_NR_APRS; i++) {
55372889c8aSPeter Maydell             uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
55472889c8aSPeter Maydell             if (!*papr) {
55572889c8aSPeter Maydell                 continue;
55672889c8aSPeter Maydell             }
55772889c8aSPeter Maydell             /* Clear lowest set bit */
55872889c8aSPeter Maydell             *papr &= *papr - 1;
55972889c8aSPeter Maydell             break;
56072889c8aSPeter Maydell         }
561a1d7b8d8SLuc Michel     }
56272889c8aSPeter Maydell 
56372889c8aSPeter Maydell     s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
564e69954b9Spbrook }
565e69954b9Spbrook 
566439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu)
567e69954b9Spbrook {
568439badd6SLuc Michel     int src;
569439badd6SLuc Michel     uint32_t ret;
570c5619bf9SFabian Aggeler 
571439badd6SLuc Michel     if (!gic_is_vcpu(cpu)) {
57240d22500SChristoffer Dall         /* Lookup the source CPU for the SGI and clear this in the
57340d22500SChristoffer Dall          * sgi_pending map.  Return the src and clear the overall pending
57440d22500SChristoffer Dall          * state on this CPU if the SGI is not pending from any CPUs.
57540d22500SChristoffer Dall          */
57640d22500SChristoffer Dall         assert(s->sgi_pending[irq][cpu] != 0);
57740d22500SChristoffer Dall         src = ctz32(s->sgi_pending[irq][cpu]);
57840d22500SChristoffer Dall         s->sgi_pending[irq][cpu] &= ~(1 << src);
57940d22500SChristoffer Dall         if (s->sgi_pending[irq][cpu] == 0) {
58086b350f0SLuc Michel             gic_clear_pending(s, irq, cpu);
58140d22500SChristoffer Dall         }
58240d22500SChristoffer Dall         ret = irq | ((src & 0x7) << 10);
58340d22500SChristoffer Dall     } else {
584439badd6SLuc Michel         uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu);
585439badd6SLuc Michel         src = GICH_LR_CPUID(*lr_entry);
586439badd6SLuc Michel 
587439badd6SLuc Michel         gic_clear_pending(s, irq, cpu);
588439badd6SLuc Michel         ret = irq | (src << 10);
589439badd6SLuc Michel     }
590439badd6SLuc Michel 
591439badd6SLuc Michel     return ret;
592439badd6SLuc Michel }
593439badd6SLuc Michel 
594439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
595439badd6SLuc Michel {
596439badd6SLuc Michel     int ret, irq;
597439badd6SLuc Michel 
598439badd6SLuc Michel     /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
599439badd6SLuc Michel      * for the case where this GIC supports grouping and the pending interrupt
600439badd6SLuc Michel      * is in the wrong group.
60140d22500SChristoffer Dall      */
602439badd6SLuc Michel     irq = gic_get_current_pending_irq(s, cpu, attrs);
603067a2b9cSLuc Michel     trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
604067a2b9cSLuc Michel                               gic_get_vcpu_real_id(cpu), irq);
605439badd6SLuc Michel 
606439badd6SLuc Michel     if (irq >= GIC_MAXIRQ) {
607439badd6SLuc Michel         DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
608439badd6SLuc Michel         return irq;
609439badd6SLuc Michel     }
610439badd6SLuc Michel 
611439badd6SLuc Michel     if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) {
612439badd6SLuc Michel         DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
613439badd6SLuc Michel         return 1023;
614439badd6SLuc Michel     }
615439badd6SLuc Michel 
616439badd6SLuc Michel     gic_activate_irq(s, cpu, irq);
617439badd6SLuc Michel 
618439badd6SLuc Michel     if (s->revision == REV_11MPCORE) {
619439badd6SLuc Michel         /* Clear pending flags for both level and edge triggered interrupts.
620439badd6SLuc Michel          * Level triggered IRQs will be reasserted once they become inactive.
621439badd6SLuc Michel          */
622439badd6SLuc Michel         gic_clear_pending(s, irq, cpu);
623439badd6SLuc Michel         ret = irq;
624439badd6SLuc Michel     } else {
625439badd6SLuc Michel         if (irq < GIC_NR_SGIS) {
626439badd6SLuc Michel             ret = gic_clear_pending_sgi(s, irq, cpu);
627439badd6SLuc Michel         } else {
62886b350f0SLuc Michel             gic_clear_pending(s, irq, cpu);
62940d22500SChristoffer Dall             ret = irq;
63040d22500SChristoffer Dall         }
63140d22500SChristoffer Dall     }
63240d22500SChristoffer Dall 
633cbe1282bSLuc Michel     if (gic_is_vcpu(cpu)) {
634cbe1282bSLuc Michel         gic_update_virt(s);
635cbe1282bSLuc Michel     } else {
63672889c8aSPeter Maydell         gic_update(s);
637cbe1282bSLuc Michel     }
63840d22500SChristoffer Dall     DPRINTF("ACK %d\n", irq);
63940d22500SChristoffer Dall     return ret;
640e69954b9Spbrook }
641e69954b9Spbrook 
64267ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
64381508470SFabian Aggeler                       MemTxAttrs attrs)
6449df90ad0SChristoffer Dall {
64581508470SFabian Aggeler     if (s->security_extn && !attrs.secure) {
64667ce697aSLuc Michel         if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
64781508470SFabian Aggeler             return; /* Ignore Non-secure access of Group0 IRQ */
64881508470SFabian Aggeler         }
64981508470SFabian Aggeler         val = 0x80 | (val >> 1); /* Non-secure view */
65081508470SFabian Aggeler     }
65181508470SFabian Aggeler 
6529df90ad0SChristoffer Dall     if (irq < GIC_INTERNAL) {
6539df90ad0SChristoffer Dall         s->priority1[irq][cpu] = val;
6549df90ad0SChristoffer Dall     } else {
6559df90ad0SChristoffer Dall         s->priority2[(irq) - GIC_INTERNAL] = val;
6569df90ad0SChristoffer Dall     }
6579df90ad0SChristoffer Dall }
6589df90ad0SChristoffer Dall 
65967ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
66081508470SFabian Aggeler                                  MemTxAttrs attrs)
66181508470SFabian Aggeler {
66267ce697aSLuc Michel     uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
66381508470SFabian Aggeler 
66481508470SFabian Aggeler     if (s->security_extn && !attrs.secure) {
66567ce697aSLuc Michel         if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
66681508470SFabian Aggeler             return 0; /* Non-secure access cannot read priority of Group0 IRQ */
66781508470SFabian Aggeler         }
66881508470SFabian Aggeler         prio = (prio << 1) & 0xff; /* Non-secure view */
66981508470SFabian Aggeler     }
67081508470SFabian Aggeler     return prio;
67181508470SFabian Aggeler }
67281508470SFabian Aggeler 
67381508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
67481508470SFabian Aggeler                                   MemTxAttrs attrs)
67581508470SFabian Aggeler {
6763dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
67781508470SFabian Aggeler         if (s->priority_mask[cpu] & 0x80) {
67881508470SFabian Aggeler             /* Priority Mask in upper half */
67981508470SFabian Aggeler             pmask = 0x80 | (pmask >> 1);
68081508470SFabian Aggeler         } else {
68181508470SFabian Aggeler             /* Non-secure write ignored if priority mask is in lower half */
68281508470SFabian Aggeler             return;
68381508470SFabian Aggeler         }
68481508470SFabian Aggeler     }
68581508470SFabian Aggeler     s->priority_mask[cpu] = pmask;
68681508470SFabian Aggeler }
68781508470SFabian Aggeler 
68881508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
68981508470SFabian Aggeler {
69081508470SFabian Aggeler     uint32_t pmask = s->priority_mask[cpu];
69181508470SFabian Aggeler 
6923dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
69381508470SFabian Aggeler         if (pmask & 0x80) {
69481508470SFabian Aggeler             /* Priority Mask in upper half, return Non-secure view */
69581508470SFabian Aggeler             pmask = (pmask << 1) & 0xff;
69681508470SFabian Aggeler         } else {
69781508470SFabian Aggeler             /* Priority Mask in lower half, RAZ */
69881508470SFabian Aggeler             pmask = 0;
69981508470SFabian Aggeler         }
70081508470SFabian Aggeler     }
70181508470SFabian Aggeler     return pmask;
70281508470SFabian Aggeler }
70381508470SFabian Aggeler 
70432951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
70532951860SFabian Aggeler {
70632951860SFabian Aggeler     uint32_t ret = s->cpu_ctlr[cpu];
70732951860SFabian Aggeler 
7083dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
70932951860SFabian Aggeler         /* Construct the NS banked view of GICC_CTLR from the correct
71032951860SFabian Aggeler          * bits of the S banked view. We don't need to move the bypass
71132951860SFabian Aggeler          * control bits because we don't implement that (IMPDEF) part
71232951860SFabian Aggeler          * of the GIC architecture.
71332951860SFabian Aggeler          */
71432951860SFabian Aggeler         ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
71532951860SFabian Aggeler     }
71632951860SFabian Aggeler     return ret;
71732951860SFabian Aggeler }
71832951860SFabian Aggeler 
71932951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
72032951860SFabian Aggeler                                 MemTxAttrs attrs)
72132951860SFabian Aggeler {
72232951860SFabian Aggeler     uint32_t mask;
72332951860SFabian Aggeler 
7243dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
72532951860SFabian Aggeler         /* The NS view can only write certain bits in the register;
72632951860SFabian Aggeler          * the rest are unchanged
72732951860SFabian Aggeler          */
72832951860SFabian Aggeler         mask = GICC_CTLR_EN_GRP1;
72932951860SFabian Aggeler         if (s->revision == 2) {
73032951860SFabian Aggeler             mask |= GICC_CTLR_EOIMODE_NS;
73132951860SFabian Aggeler         }
73232951860SFabian Aggeler         s->cpu_ctlr[cpu] &= ~mask;
73332951860SFabian Aggeler         s->cpu_ctlr[cpu] |= (value << 1) & mask;
73432951860SFabian Aggeler     } else {
73532951860SFabian Aggeler         if (s->revision == 2) {
73632951860SFabian Aggeler             mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
73732951860SFabian Aggeler         } else {
73832951860SFabian Aggeler             mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
73932951860SFabian Aggeler         }
74032951860SFabian Aggeler         s->cpu_ctlr[cpu] = value & mask;
74132951860SFabian Aggeler     }
74232951860SFabian Aggeler     DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
74332951860SFabian Aggeler             "Group1 Interrupts %sabled\n", cpu,
74432951860SFabian Aggeler             (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
74532951860SFabian Aggeler             (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
74632951860SFabian Aggeler }
74732951860SFabian Aggeler 
74808efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
74908efa9f2SFabian Aggeler {
75071aa735bSLuc MICHEL     if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
75171aa735bSLuc MICHEL         /* Idle priority */
75271aa735bSLuc MICHEL         return 0xff;
75371aa735bSLuc MICHEL     }
75471aa735bSLuc MICHEL 
7553dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
75608efa9f2SFabian Aggeler         if (s->running_priority[cpu] & 0x80) {
75708efa9f2SFabian Aggeler             /* Running priority in upper half of range: return the Non-secure
75808efa9f2SFabian Aggeler              * view of the priority.
75908efa9f2SFabian Aggeler              */
76008efa9f2SFabian Aggeler             return s->running_priority[cpu] << 1;
76108efa9f2SFabian Aggeler         } else {
76208efa9f2SFabian Aggeler             /* Running priority in lower half of range: RAZ */
76308efa9f2SFabian Aggeler             return 0;
76408efa9f2SFabian Aggeler         }
76508efa9f2SFabian Aggeler     } else {
76608efa9f2SFabian Aggeler         return s->running_priority[cpu];
76708efa9f2SFabian Aggeler     }
76808efa9f2SFabian Aggeler }
76908efa9f2SFabian Aggeler 
770a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation,
771a55c910eSPeter Maydell  * ie whether the relevant EOIMode bit is set.
772a55c910eSPeter Maydell  */
773a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
774a55c910eSPeter Maydell {
775a55c910eSPeter Maydell     if (s->revision != 2) {
776a55c910eSPeter Maydell         /* Before GICv2 prio-drop and deactivate are not separable */
777a55c910eSPeter Maydell         return false;
778a55c910eSPeter Maydell     }
7793dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs)) {
780a55c910eSPeter Maydell         return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
781a55c910eSPeter Maydell     }
782a55c910eSPeter Maydell     return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
783a55c910eSPeter Maydell }
784a55c910eSPeter Maydell 
785a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
786a55c910eSPeter Maydell {
787ee03cca8SPeter Maydell     int group;
788ee03cca8SPeter Maydell 
78902f2e22dSLuc Michel     if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
790ee03cca8SPeter Maydell         /*
791ee03cca8SPeter Maydell          * This handles two cases:
792ee03cca8SPeter Maydell          * 1. If software writes the ID of a spurious interrupt [ie 1023]
793ee03cca8SPeter Maydell          * to the GICC_DIR, the GIC ignores that write.
794ee03cca8SPeter Maydell          * 2. If software writes the number of a non-existent interrupt
795ee03cca8SPeter Maydell          * this must be a subcase of "value written is not an active interrupt"
79602f2e22dSLuc Michel          * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs,
79702f2e22dSLuc Michel          * all IRQs potentially exist, so this limit does not apply.
798ee03cca8SPeter Maydell          */
799ee03cca8SPeter Maydell         return;
800ee03cca8SPeter Maydell     }
801ee03cca8SPeter Maydell 
802a55c910eSPeter Maydell     if (!gic_eoi_split(s, cpu, attrs)) {
803a55c910eSPeter Maydell         /* This is UNPREDICTABLE; we choose to ignore it */
804a55c910eSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
805a55c910eSPeter Maydell                       "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
806a55c910eSPeter Maydell         return;
807a55c910eSPeter Maydell     }
808a55c910eSPeter Maydell 
80902f2e22dSLuc Michel     if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
81002f2e22dSLuc Michel         /* This vIRQ does not have an LR entry which is either active or
81102f2e22dSLuc Michel          * pending and active. Increment EOICount and ignore the write.
81202f2e22dSLuc Michel          */
81302f2e22dSLuc Michel         int rcpu = gic_get_vcpu_real_id(cpu);
81402f2e22dSLuc Michel         s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
815cbe1282bSLuc Michel 
816cbe1282bSLuc Michel         /* Update the virtual interface in case a maintenance interrupt should
817cbe1282bSLuc Michel          * be raised.
818cbe1282bSLuc Michel          */
819cbe1282bSLuc Michel         gic_update_virt(s);
82002f2e22dSLuc Michel         return;
82102f2e22dSLuc Michel     }
82202f2e22dSLuc Michel 
82302f2e22dSLuc Michel     group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
82402f2e22dSLuc Michel 
8253dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
826a55c910eSPeter Maydell         DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
827a55c910eSPeter Maydell         return;
828a55c910eSPeter Maydell     }
829a55c910eSPeter Maydell 
83086b350f0SLuc Michel     gic_clear_active(s, irq, cpu);
831a55c910eSPeter Maydell }
832a55c910eSPeter Maydell 
83350491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
834e69954b9Spbrook {
8359ee6e8bbSpbrook     int cm = 1 << cpu;
83672889c8aSPeter Maydell     int group;
83772889c8aSPeter Maydell 
838df628ff1Spbrook     DPRINTF("EOI %d\n", irq);
83902f2e22dSLuc Michel     if (gic_is_vcpu(cpu)) {
84002f2e22dSLuc Michel         /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the
84102f2e22dSLuc Michel          * running prio is < 0x100.
84202f2e22dSLuc Michel          */
84302f2e22dSLuc Michel         bool prio_drop = s->running_priority[cpu] < 0x100;
84402f2e22dSLuc Michel 
84502f2e22dSLuc Michel         if (irq >= GIC_MAXIRQ) {
84602f2e22dSLuc Michel             /* Ignore spurious interrupt */
84702f2e22dSLuc Michel             return;
84802f2e22dSLuc Michel         }
84902f2e22dSLuc Michel 
85002f2e22dSLuc Michel         gic_drop_prio(s, cpu, 0);
85102f2e22dSLuc Michel 
85202f2e22dSLuc Michel         if (!gic_eoi_split(s, cpu, attrs)) {
85302f2e22dSLuc Michel             bool valid = gic_virq_is_valid(s, irq, cpu);
85402f2e22dSLuc Michel             if (prio_drop && !valid) {
85502f2e22dSLuc Michel                 /* We are in a situation where:
85602f2e22dSLuc Michel                  *   - V_CTRL.EOIMode is false (no EOI split),
85702f2e22dSLuc Michel                  *   - The call to gic_drop_prio() cleared a bit in GICH_APR,
85802f2e22dSLuc Michel                  *   - This vIRQ does not have an LR entry which is either
85902f2e22dSLuc Michel                  *     active or pending and active.
86002f2e22dSLuc Michel                  * In that case, we must increment EOICount.
86102f2e22dSLuc Michel                  */
86202f2e22dSLuc Michel                 int rcpu = gic_get_vcpu_real_id(cpu);
86302f2e22dSLuc Michel                 s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
86402f2e22dSLuc Michel             } else if (valid) {
86502f2e22dSLuc Michel                 gic_clear_active(s, irq, cpu);
86602f2e22dSLuc Michel             }
86702f2e22dSLuc Michel         }
86802f2e22dSLuc Michel 
869cbe1282bSLuc Michel         gic_update_virt(s);
87002f2e22dSLuc Michel         return;
87102f2e22dSLuc Michel     }
87202f2e22dSLuc Michel 
873a32134aaSMark Langsdorf     if (irq >= s->num_irq) {
874217bfb44SPeter Maydell         /* This handles two cases:
875217bfb44SPeter Maydell          * 1. If software writes the ID of a spurious interrupt [ie 1023]
876217bfb44SPeter Maydell          * to the GICC_EOIR, the GIC ignores that write.
877217bfb44SPeter Maydell          * 2. If software writes the number of a non-existent interrupt
878217bfb44SPeter Maydell          * this must be a subcase of "value written does not match the last
879217bfb44SPeter Maydell          * valid interrupt value read from the Interrupt Acknowledge
880217bfb44SPeter Maydell          * register" and so this is UNPREDICTABLE. We choose to ignore it.
881217bfb44SPeter Maydell          */
882217bfb44SPeter Maydell         return;
883217bfb44SPeter Maydell     }
88472889c8aSPeter Maydell     if (s->running_priority[cpu] == 0x100) {
885e69954b9Spbrook         return; /* No active IRQ.  */
88672889c8aSPeter Maydell     }
8878d999995SChristoffer Dall 
8883bc4b52cSMarcin Krzeminski     if (s->revision == REV_11MPCORE) {
889e69954b9Spbrook         /* Mark level triggered interrupts as pending if they are still
890e69954b9Spbrook            raised.  */
89167ce697aSLuc Michel         if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
89267ce697aSLuc Michel             && GIC_DIST_TEST_LEVEL(irq, cm)
89367ce697aSLuc Michel             && (GIC_DIST_TARGET(irq) & cm) != 0) {
8949ee6e8bbSpbrook             DPRINTF("Set %d pending mask %x\n", irq, cm);
89567ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, cm);
896e69954b9Spbrook         }
8978d999995SChristoffer Dall     }
8988d999995SChristoffer Dall 
89986b350f0SLuc Michel     group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
90072889c8aSPeter Maydell 
9013dd0471bSLuc Michel     if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
902f9c6a7f1SFabian Aggeler         DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
903f9c6a7f1SFabian Aggeler         return;
904f9c6a7f1SFabian Aggeler     }
905f9c6a7f1SFabian Aggeler 
906f9c6a7f1SFabian Aggeler     /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
907f9c6a7f1SFabian Aggeler      * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
908f9c6a7f1SFabian Aggeler      * i.e. go ahead and complete the irq anyway.
909f9c6a7f1SFabian Aggeler      */
910f9c6a7f1SFabian Aggeler 
91172889c8aSPeter Maydell     gic_drop_prio(s, cpu, group);
912a55c910eSPeter Maydell 
913a55c910eSPeter Maydell     /* In GICv2 the guest can choose to split priority-drop and deactivate */
914a55c910eSPeter Maydell     if (!gic_eoi_split(s, cpu, attrs)) {
91586b350f0SLuc Michel         gic_clear_active(s, irq, cpu);
916a55c910eSPeter Maydell     }
917e69954b9Spbrook     gic_update(s);
918e69954b9Spbrook }
919e69954b9Spbrook 
920a9d85353SPeter Maydell static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
921e69954b9Spbrook {
922fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
923e69954b9Spbrook     uint32_t res;
924e69954b9Spbrook     int irq;
925e69954b9Spbrook     int i;
9269ee6e8bbSpbrook     int cpu;
9279ee6e8bbSpbrook     int cm;
9289ee6e8bbSpbrook     int mask;
929e69954b9Spbrook 
930926c4affSPeter Maydell     cpu = gic_get_current_cpu(s);
9319ee6e8bbSpbrook     cm = 1 << cpu;
932e69954b9Spbrook     if (offset < 0x100) {
933679aa175SFabian Aggeler         if (offset == 0) {      /* GICD_CTLR */
934679aa175SFabian Aggeler             if (s->security_extn && !attrs.secure) {
935679aa175SFabian Aggeler                 /* The NS bank of this register is just an alias of the
936679aa175SFabian Aggeler                  * EnableGrp1 bit in the S bank version.
937679aa175SFabian Aggeler                  */
938679aa175SFabian Aggeler                 return extract32(s->ctlr, 1, 1);
939679aa175SFabian Aggeler             } else {
940679aa175SFabian Aggeler                 return s->ctlr;
941679aa175SFabian Aggeler             }
942679aa175SFabian Aggeler         }
943e69954b9Spbrook         if (offset == 4)
9445543d1abSFabian Aggeler             /* Interrupt Controller Type Register */
9455543d1abSFabian Aggeler             return ((s->num_irq / 32) - 1)
946b95690c9SWei Huang                     | ((s->num_cpu - 1) << 5)
9475543d1abSFabian Aggeler                     | (s->security_extn << 10);
948e69954b9Spbrook         if (offset < 0x08)
949e69954b9Spbrook             return 0;
950b79f2265SRob Herring         if (offset >= 0x80) {
951c27a5ba9SFabian Aggeler             /* Interrupt Group Registers: these RAZ/WI if this is an NS
952c27a5ba9SFabian Aggeler              * access to a GIC with the security extensions, or if the GIC
953c27a5ba9SFabian Aggeler              * doesn't have groups at all.
954c27a5ba9SFabian Aggeler              */
955c27a5ba9SFabian Aggeler             res = 0;
956c27a5ba9SFabian Aggeler             if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
957c27a5ba9SFabian Aggeler                 /* Every byte offset holds 8 group status bits */
958*b6e6c651SPeter Maydell                 irq = (offset - 0x080) * 8;
959c27a5ba9SFabian Aggeler                 if (irq >= s->num_irq) {
960c27a5ba9SFabian Aggeler                     goto bad_reg;
961c27a5ba9SFabian Aggeler                 }
962c27a5ba9SFabian Aggeler                 for (i = 0; i < 8; i++) {
96367ce697aSLuc Michel                     if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
964c27a5ba9SFabian Aggeler                         res |= (1 << i);
965c27a5ba9SFabian Aggeler                     }
966c27a5ba9SFabian Aggeler                 }
967c27a5ba9SFabian Aggeler             }
968c27a5ba9SFabian Aggeler             return res;
969b79f2265SRob Herring         }
970e69954b9Spbrook         goto bad_reg;
971e69954b9Spbrook     } else if (offset < 0x200) {
972e69954b9Spbrook         /* Interrupt Set/Clear Enable.  */
973e69954b9Spbrook         if (offset < 0x180)
974e69954b9Spbrook             irq = (offset - 0x100) * 8;
975e69954b9Spbrook         else
976e69954b9Spbrook             irq = (offset - 0x180) * 8;
977a32134aaSMark Langsdorf         if (irq >= s->num_irq)
978e69954b9Spbrook             goto bad_reg;
979e69954b9Spbrook         res = 0;
980e69954b9Spbrook         for (i = 0; i < 8; i++) {
981fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
98267ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
983fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
984fea8a08eSJens Wiklander             }
985fea8a08eSJens Wiklander 
98667ce697aSLuc Michel             if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
987e69954b9Spbrook                 res |= (1 << i);
988e69954b9Spbrook             }
989e69954b9Spbrook         }
990e69954b9Spbrook     } else if (offset < 0x300) {
991e69954b9Spbrook         /* Interrupt Set/Clear Pending.  */
992e69954b9Spbrook         if (offset < 0x280)
993e69954b9Spbrook             irq = (offset - 0x200) * 8;
994e69954b9Spbrook         else
995e69954b9Spbrook             irq = (offset - 0x280) * 8;
996a32134aaSMark Langsdorf         if (irq >= s->num_irq)
997e69954b9Spbrook             goto bad_reg;
998e69954b9Spbrook         res = 0;
99969253800SRusty Russell         mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
1000e69954b9Spbrook         for (i = 0; i < 8; i++) {
1001fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
100267ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1003fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1004fea8a08eSJens Wiklander             }
1005fea8a08eSJens Wiklander 
10068d999995SChristoffer Dall             if (gic_test_pending(s, irq + i, mask)) {
1007e69954b9Spbrook                 res |= (1 << i);
1008e69954b9Spbrook             }
1009e69954b9Spbrook         }
1010e69954b9Spbrook     } else if (offset < 0x400) {
10113bb0b038SLuc Michel         /* Interrupt Set/Clear Active.  */
10123bb0b038SLuc Michel         if (offset < 0x380) {
10133bb0b038SLuc Michel             irq = (offset - 0x300) * 8;
10143bb0b038SLuc Michel         } else if (s->revision == 2) {
10153bb0b038SLuc Michel             irq = (offset - 0x380) * 8;
10163bb0b038SLuc Michel         } else {
10173bb0b038SLuc Michel             goto bad_reg;
10183bb0b038SLuc Michel         }
10193bb0b038SLuc Michel 
1020a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1021e69954b9Spbrook             goto bad_reg;
1022e69954b9Spbrook         res = 0;
102369253800SRusty Russell         mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
1024e69954b9Spbrook         for (i = 0; i < 8; i++) {
1025fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
102667ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1027fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1028fea8a08eSJens Wiklander             }
1029fea8a08eSJens Wiklander 
103067ce697aSLuc Michel             if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
1031e69954b9Spbrook                 res |= (1 << i);
1032e69954b9Spbrook             }
1033e69954b9Spbrook         }
1034e69954b9Spbrook     } else if (offset < 0x800) {
1035e69954b9Spbrook         /* Interrupt Priority.  */
1036*b6e6c651SPeter Maydell         irq = (offset - 0x400);
1037a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1038e69954b9Spbrook             goto bad_reg;
103967ce697aSLuc Michel         res = gic_dist_get_priority(s, cpu, irq, attrs);
1040e69954b9Spbrook     } else if (offset < 0xc00) {
1041e69954b9Spbrook         /* Interrupt CPU Target.  */
10426b9680bbSPeter Maydell         if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
10436b9680bbSPeter Maydell             /* For uniprocessor GICs these RAZ/WI */
10446b9680bbSPeter Maydell             res = 0;
10456b9680bbSPeter Maydell         } else {
1046*b6e6c651SPeter Maydell             irq = (offset - 0x800);
10476b9680bbSPeter Maydell             if (irq >= s->num_irq) {
1048e69954b9Spbrook                 goto bad_reg;
10496b9680bbSPeter Maydell             }
10507995206dSPeter Maydell             if (irq < 29 && s->revision == REV_11MPCORE) {
10517995206dSPeter Maydell                 res = 0;
10527995206dSPeter Maydell             } else if (irq < GIC_INTERNAL) {
10539ee6e8bbSpbrook                 res = cm;
10549ee6e8bbSpbrook             } else {
105567ce697aSLuc Michel                 res = GIC_DIST_TARGET(irq);
10569ee6e8bbSpbrook             }
10576b9680bbSPeter Maydell         }
1058e69954b9Spbrook     } else if (offset < 0xf00) {
1059e69954b9Spbrook         /* Interrupt Configuration.  */
1060*b6e6c651SPeter Maydell         irq = (offset - 0xc00) * 4;
1061a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1062e69954b9Spbrook             goto bad_reg;
1063e69954b9Spbrook         res = 0;
1064e69954b9Spbrook         for (i = 0; i < 4; i++) {
1065fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
106667ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1067fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1068fea8a08eSJens Wiklander             }
1069fea8a08eSJens Wiklander 
107067ce697aSLuc Michel             if (GIC_DIST_TEST_MODEL(irq + i)) {
1071e69954b9Spbrook                 res |= (1 << (i * 2));
107267ce697aSLuc Michel             }
107367ce697aSLuc Michel             if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1074e69954b9Spbrook                 res |= (2 << (i * 2));
1075e69954b9Spbrook             }
107667ce697aSLuc Michel         }
107740d22500SChristoffer Dall     } else if (offset < 0xf10) {
107840d22500SChristoffer Dall         goto bad_reg;
107940d22500SChristoffer Dall     } else if (offset < 0xf30) {
10807c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
108140d22500SChristoffer Dall             goto bad_reg;
108240d22500SChristoffer Dall         }
108340d22500SChristoffer Dall 
108440d22500SChristoffer Dall         if (offset < 0xf20) {
108540d22500SChristoffer Dall             /* GICD_CPENDSGIRn */
108640d22500SChristoffer Dall             irq = (offset - 0xf10);
108740d22500SChristoffer Dall         } else {
108840d22500SChristoffer Dall             irq = (offset - 0xf20);
108940d22500SChristoffer Dall             /* GICD_SPENDSGIRn */
109040d22500SChristoffer Dall         }
109140d22500SChristoffer Dall 
1092fea8a08eSJens Wiklander         if (s->security_extn && !attrs.secure &&
109367ce697aSLuc Michel             !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1094fea8a08eSJens Wiklander             res = 0; /* Ignore Non-secure access of Group0 IRQ */
1095fea8a08eSJens Wiklander         } else {
109640d22500SChristoffer Dall             res = s->sgi_pending[irq][cpu];
1097fea8a08eSJens Wiklander         }
10983355c360SAlistair Francis     } else if (offset < 0xfd0) {
1099e69954b9Spbrook         goto bad_reg;
11003355c360SAlistair Francis     } else if (offset < 0x1000) {
1101e69954b9Spbrook         if (offset & 3) {
1102e69954b9Spbrook             res = 0;
1103e69954b9Spbrook         } else {
11043355c360SAlistair Francis             switch (s->revision) {
11053355c360SAlistair Francis             case REV_11MPCORE:
11063355c360SAlistair Francis                 res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
11073355c360SAlistair Francis                 break;
11083355c360SAlistair Francis             case 1:
11093355c360SAlistair Francis                 res = gic_id_gicv1[(offset - 0xfd0) >> 2];
11103355c360SAlistair Francis                 break;
11113355c360SAlistair Francis             case 2:
11123355c360SAlistair Francis                 res = gic_id_gicv2[(offset - 0xfd0) >> 2];
11133355c360SAlistair Francis                 break;
11143355c360SAlistair Francis             default:
11153355c360SAlistair Francis                 res = 0;
1116e69954b9Spbrook             }
1117e69954b9Spbrook         }
11183355c360SAlistair Francis     } else {
11193355c360SAlistair Francis         g_assert_not_reached();
11203355c360SAlistair Francis     }
1121e69954b9Spbrook     return res;
1122e69954b9Spbrook bad_reg:
11238c8dc39fSPeter Maydell     qemu_log_mask(LOG_GUEST_ERROR,
11248c8dc39fSPeter Maydell                   "gic_dist_readb: Bad offset %x\n", (int)offset);
1125e69954b9Spbrook     return 0;
1126e69954b9Spbrook }
1127e69954b9Spbrook 
1128a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
1129a9d85353SPeter Maydell                                  unsigned size, MemTxAttrs attrs)
1130e69954b9Spbrook {
1131a9d85353SPeter Maydell     switch (size) {
1132a9d85353SPeter Maydell     case 1:
1133a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1134067a2b9cSLuc Michel         break;
1135a9d85353SPeter Maydell     case 2:
1136a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1137a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1138067a2b9cSLuc Michel         break;
1139a9d85353SPeter Maydell     case 4:
1140a9d85353SPeter Maydell         *data = gic_dist_readb(opaque, offset, attrs);
1141a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1142a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
1143a9d85353SPeter Maydell         *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
1144067a2b9cSLuc Michel         break;
1145a9d85353SPeter Maydell     default:
1146a9d85353SPeter Maydell         return MEMTX_ERROR;
1147e69954b9Spbrook     }
1148067a2b9cSLuc Michel 
1149067a2b9cSLuc Michel     trace_gic_dist_read(offset, size, *data);
1150067a2b9cSLuc Michel     return MEMTX_OK;
1151e69954b9Spbrook }
1152e69954b9Spbrook 
1153a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset,
1154a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1155e69954b9Spbrook {
1156fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1157e69954b9Spbrook     int irq;
1158e69954b9Spbrook     int i;
11599ee6e8bbSpbrook     int cpu;
1160e69954b9Spbrook 
1161926c4affSPeter Maydell     cpu = gic_get_current_cpu(s);
1162e69954b9Spbrook     if (offset < 0x100) {
1163e69954b9Spbrook         if (offset == 0) {
1164679aa175SFabian Aggeler             if (s->security_extn && !attrs.secure) {
1165679aa175SFabian Aggeler                 /* NS version is just an alias of the S version's bit 1 */
1166679aa175SFabian Aggeler                 s->ctlr = deposit32(s->ctlr, 1, 1, value);
1167679aa175SFabian Aggeler             } else if (gic_has_groups(s)) {
1168679aa175SFabian Aggeler                 s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
1169679aa175SFabian Aggeler             } else {
1170679aa175SFabian Aggeler                 s->ctlr = value & GICD_CTLR_EN_GRP0;
1171679aa175SFabian Aggeler             }
1172679aa175SFabian Aggeler             DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
1173679aa175SFabian Aggeler                     s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
1174679aa175SFabian Aggeler                     s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
1175e69954b9Spbrook         } else if (offset < 4) {
1176e69954b9Spbrook             /* ignored.  */
1177b79f2265SRob Herring         } else if (offset >= 0x80) {
1178c27a5ba9SFabian Aggeler             /* Interrupt Group Registers: RAZ/WI for NS access to secure
1179c27a5ba9SFabian Aggeler              * GIC, or for GICs without groups.
1180c27a5ba9SFabian Aggeler              */
1181c27a5ba9SFabian Aggeler             if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
1182c27a5ba9SFabian Aggeler                 /* Every byte offset holds 8 group status bits */
1183*b6e6c651SPeter Maydell                 irq = (offset - 0x80) * 8;
1184c27a5ba9SFabian Aggeler                 if (irq >= s->num_irq) {
1185c27a5ba9SFabian Aggeler                     goto bad_reg;
1186c27a5ba9SFabian Aggeler                 }
1187c27a5ba9SFabian Aggeler                 for (i = 0; i < 8; i++) {
1188c27a5ba9SFabian Aggeler                     /* Group bits are banked for private interrupts */
1189c27a5ba9SFabian Aggeler                     int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1190c27a5ba9SFabian Aggeler                     if (value & (1 << i)) {
1191c27a5ba9SFabian Aggeler                         /* Group1 (Non-secure) */
119267ce697aSLuc Michel                         GIC_DIST_SET_GROUP(irq + i, cm);
1193c27a5ba9SFabian Aggeler                     } else {
1194c27a5ba9SFabian Aggeler                         /* Group0 (Secure) */
119567ce697aSLuc Michel                         GIC_DIST_CLEAR_GROUP(irq + i, cm);
1196c27a5ba9SFabian Aggeler                     }
1197c27a5ba9SFabian Aggeler                 }
1198c27a5ba9SFabian Aggeler             }
1199e69954b9Spbrook         } else {
1200e69954b9Spbrook             goto bad_reg;
1201e69954b9Spbrook         }
1202e69954b9Spbrook     } else if (offset < 0x180) {
1203e69954b9Spbrook         /* Interrupt Set Enable.  */
1204*b6e6c651SPeter Maydell         irq = (offset - 0x100) * 8;
1205a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1206e69954b9Spbrook             goto bad_reg;
120741ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12089ee6e8bbSpbrook             value = 0xff;
120941ab7b55SChristoffer Dall         }
121041ab7b55SChristoffer Dall 
1211e69954b9Spbrook         for (i = 0; i < 8; i++) {
1212e69954b9Spbrook             if (value & (1 << i)) {
1213f47b48fbSDaniel Sangorrin                 int mask =
121467ce697aSLuc Michel                     (irq < GIC_INTERNAL) ? (1 << cpu)
121567ce697aSLuc Michel                                          : GIC_DIST_TARGET(irq + i);
121669253800SRusty Russell                 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
121741bf234dSRabin Vincent 
1218fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
121967ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1220fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1221fea8a08eSJens Wiklander                 }
1222fea8a08eSJens Wiklander 
122367ce697aSLuc Michel                 if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1224e69954b9Spbrook                     DPRINTF("Enabled IRQ %d\n", irq + i);
12252531088fSHollis Blanchard                     trace_gic_enable_irq(irq + i);
122641bf234dSRabin Vincent                 }
122767ce697aSLuc Michel                 GIC_DIST_SET_ENABLED(irq + i, cm);
1228e69954b9Spbrook                 /* If a raised level triggered IRQ enabled then mark
1229e69954b9Spbrook                    is as pending.  */
123067ce697aSLuc Michel                 if (GIC_DIST_TEST_LEVEL(irq + i, mask)
123167ce697aSLuc Michel                         && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
12329ee6e8bbSpbrook                     DPRINTF("Set %d pending mask %x\n", irq + i, mask);
123367ce697aSLuc Michel                     GIC_DIST_SET_PENDING(irq + i, mask);
12349ee6e8bbSpbrook                 }
1235e69954b9Spbrook             }
1236e69954b9Spbrook         }
1237e69954b9Spbrook     } else if (offset < 0x200) {
1238e69954b9Spbrook         /* Interrupt Clear Enable.  */
1239*b6e6c651SPeter Maydell         irq = (offset - 0x180) * 8;
1240a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1241e69954b9Spbrook             goto bad_reg;
124241ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12439ee6e8bbSpbrook             value = 0;
124441ab7b55SChristoffer Dall         }
124541ab7b55SChristoffer Dall 
1246e69954b9Spbrook         for (i = 0; i < 8; i++) {
1247e69954b9Spbrook             if (value & (1 << i)) {
124869253800SRusty Russell                 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
124941bf234dSRabin Vincent 
1250fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
125167ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1252fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1253fea8a08eSJens Wiklander                 }
1254fea8a08eSJens Wiklander 
125567ce697aSLuc Michel                 if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1256e69954b9Spbrook                     DPRINTF("Disabled IRQ %d\n", irq + i);
12572531088fSHollis Blanchard                     trace_gic_disable_irq(irq + i);
125841bf234dSRabin Vincent                 }
125967ce697aSLuc Michel                 GIC_DIST_CLEAR_ENABLED(irq + i, cm);
1260e69954b9Spbrook             }
1261e69954b9Spbrook         }
1262e69954b9Spbrook     } else if (offset < 0x280) {
1263e69954b9Spbrook         /* Interrupt Set Pending.  */
1264*b6e6c651SPeter Maydell         irq = (offset - 0x200) * 8;
1265a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1266e69954b9Spbrook             goto bad_reg;
126741ab7b55SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12685b0adce1SChristoffer Dall             value = 0;
126941ab7b55SChristoffer Dall         }
12709ee6e8bbSpbrook 
1271e69954b9Spbrook         for (i = 0; i < 8; i++) {
1272e69954b9Spbrook             if (value & (1 << i)) {
1273fea8a08eSJens Wiklander                 if (s->security_extn && !attrs.secure &&
127467ce697aSLuc Michel                     !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1275fea8a08eSJens Wiklander                     continue; /* Ignore Non-secure access of Group0 IRQ */
1276fea8a08eSJens Wiklander                 }
1277fea8a08eSJens Wiklander 
127867ce697aSLuc Michel                 GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i));
1279e69954b9Spbrook             }
1280e69954b9Spbrook         }
1281e69954b9Spbrook     } else if (offset < 0x300) {
1282e69954b9Spbrook         /* Interrupt Clear Pending.  */
1283*b6e6c651SPeter Maydell         irq = (offset - 0x280) * 8;
1284a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1285e69954b9Spbrook             goto bad_reg;
12865b0adce1SChristoffer Dall         if (irq < GIC_NR_SGIS) {
12875b0adce1SChristoffer Dall             value = 0;
12885b0adce1SChristoffer Dall         }
12895b0adce1SChristoffer Dall 
1290e69954b9Spbrook         for (i = 0; i < 8; i++) {
1291fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
129267ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1293fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1294fea8a08eSJens Wiklander             }
1295fea8a08eSJens Wiklander 
12969ee6e8bbSpbrook             /* ??? This currently clears the pending bit for all CPUs, even
12979ee6e8bbSpbrook                for per-CPU interrupts.  It's unclear whether this is the
12989ee6e8bbSpbrook                corect behavior.  */
1299e69954b9Spbrook             if (value & (1 << i)) {
130067ce697aSLuc Michel                 GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
1301e69954b9Spbrook             }
1302e69954b9Spbrook         }
13033bb0b038SLuc Michel     } else if (offset < 0x380) {
13043bb0b038SLuc Michel         /* Interrupt Set Active.  */
13053bb0b038SLuc Michel         if (s->revision != 2) {
1306e69954b9Spbrook             goto bad_reg;
13073bb0b038SLuc Michel         }
13083bb0b038SLuc Michel 
1309*b6e6c651SPeter Maydell         irq = (offset - 0x300) * 8;
13103bb0b038SLuc Michel         if (irq >= s->num_irq) {
13113bb0b038SLuc Michel             goto bad_reg;
13123bb0b038SLuc Michel         }
13133bb0b038SLuc Michel 
13143bb0b038SLuc Michel         /* This register is banked per-cpu for PPIs */
13153bb0b038SLuc Michel         int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13163bb0b038SLuc Michel 
13173bb0b038SLuc Michel         for (i = 0; i < 8; i++) {
13183bb0b038SLuc Michel             if (s->security_extn && !attrs.secure &&
13193bb0b038SLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13203bb0b038SLuc Michel                 continue; /* Ignore Non-secure access of Group0 IRQ */
13213bb0b038SLuc Michel             }
13223bb0b038SLuc Michel 
13233bb0b038SLuc Michel             if (value & (1 << i)) {
13243bb0b038SLuc Michel                 GIC_DIST_SET_ACTIVE(irq + i, cm);
13253bb0b038SLuc Michel             }
13263bb0b038SLuc Michel         }
13273bb0b038SLuc Michel     } else if (offset < 0x400) {
13283bb0b038SLuc Michel         /* Interrupt Clear Active.  */
13293bb0b038SLuc Michel         if (s->revision != 2) {
13303bb0b038SLuc Michel             goto bad_reg;
13313bb0b038SLuc Michel         }
13323bb0b038SLuc Michel 
1333*b6e6c651SPeter Maydell         irq = (offset - 0x380) * 8;
13343bb0b038SLuc Michel         if (irq >= s->num_irq) {
13353bb0b038SLuc Michel             goto bad_reg;
13363bb0b038SLuc Michel         }
13373bb0b038SLuc Michel 
13383bb0b038SLuc Michel         /* This register is banked per-cpu for PPIs */
13393bb0b038SLuc Michel         int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13403bb0b038SLuc Michel 
13413bb0b038SLuc Michel         for (i = 0; i < 8; i++) {
13423bb0b038SLuc Michel             if (s->security_extn && !attrs.secure &&
13433bb0b038SLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13443bb0b038SLuc Michel                 continue; /* Ignore Non-secure access of Group0 IRQ */
13453bb0b038SLuc Michel             }
13463bb0b038SLuc Michel 
13473bb0b038SLuc Michel             if (value & (1 << i)) {
13483bb0b038SLuc Michel                 GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
13493bb0b038SLuc Michel             }
13503bb0b038SLuc Michel         }
1351e69954b9Spbrook     } else if (offset < 0x800) {
1352e69954b9Spbrook         /* Interrupt Priority.  */
1353*b6e6c651SPeter Maydell         irq = (offset - 0x400);
1354a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1355e69954b9Spbrook             goto bad_reg;
135667ce697aSLuc Michel         gic_dist_set_priority(s, cpu, irq, value, attrs);
1357e69954b9Spbrook     } else if (offset < 0xc00) {
13586b9680bbSPeter Maydell         /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
13596b9680bbSPeter Maydell          * annoying exception of the 11MPCore's GIC.
13606b9680bbSPeter Maydell          */
13616b9680bbSPeter Maydell         if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
1362*b6e6c651SPeter Maydell             irq = (offset - 0x800);
13636b9680bbSPeter Maydell             if (irq >= s->num_irq) {
1364e69954b9Spbrook                 goto bad_reg;
13656b9680bbSPeter Maydell             }
13667995206dSPeter Maydell             if (irq < 29 && s->revision == REV_11MPCORE) {
13679ee6e8bbSpbrook                 value = 0;
13686b9680bbSPeter Maydell             } else if (irq < GIC_INTERNAL) {
13699ee6e8bbSpbrook                 value = ALL_CPU_MASK;
13706b9680bbSPeter Maydell             }
13719ee6e8bbSpbrook             s->irq_target[irq] = value & ALL_CPU_MASK;
13726b9680bbSPeter Maydell         }
1373e69954b9Spbrook     } else if (offset < 0xf00) {
1374e69954b9Spbrook         /* Interrupt Configuration.  */
1375*b6e6c651SPeter Maydell         irq = (offset - 0xc00) * 4;
1376a32134aaSMark Langsdorf         if (irq >= s->num_irq)
1377e69954b9Spbrook             goto bad_reg;
1378de7a900fSAdam Lackorzynski         if (irq < GIC_NR_SGIS)
13799ee6e8bbSpbrook             value |= 0xaa;
1380e69954b9Spbrook         for (i = 0; i < 4; i++) {
1381fea8a08eSJens Wiklander             if (s->security_extn && !attrs.secure &&
138267ce697aSLuc Michel                 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1383fea8a08eSJens Wiklander                 continue; /* Ignore Non-secure access of Group0 IRQ */
1384fea8a08eSJens Wiklander             }
1385fea8a08eSJens Wiklander 
13867c14b3acSMichael Davidsaver             if (s->revision == REV_11MPCORE) {
1387e69954b9Spbrook                 if (value & (1 << (i * 2))) {
138867ce697aSLuc Michel                     GIC_DIST_SET_MODEL(irq + i);
1389e69954b9Spbrook                 } else {
139067ce697aSLuc Michel                     GIC_DIST_CLEAR_MODEL(irq + i);
1391e69954b9Spbrook                 }
139224b790dfSAdam Lackorzynski             }
1393e69954b9Spbrook             if (value & (2 << (i * 2))) {
139467ce697aSLuc Michel                 GIC_DIST_SET_EDGE_TRIGGER(irq + i);
1395e69954b9Spbrook             } else {
139667ce697aSLuc Michel                 GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
1397e69954b9Spbrook             }
1398e69954b9Spbrook         }
139940d22500SChristoffer Dall     } else if (offset < 0xf10) {
14009ee6e8bbSpbrook         /* 0xf00 is only handled for 32-bit writes.  */
1401e69954b9Spbrook         goto bad_reg;
140240d22500SChristoffer Dall     } else if (offset < 0xf20) {
140340d22500SChristoffer Dall         /* GICD_CPENDSGIRn */
14047c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
140540d22500SChristoffer Dall             goto bad_reg;
140640d22500SChristoffer Dall         }
140740d22500SChristoffer Dall         irq = (offset - 0xf10);
140840d22500SChristoffer Dall 
1409fea8a08eSJens Wiklander         if (!s->security_extn || attrs.secure ||
141067ce697aSLuc Michel             GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
141140d22500SChristoffer Dall             s->sgi_pending[irq][cpu] &= ~value;
141240d22500SChristoffer Dall             if (s->sgi_pending[irq][cpu] == 0) {
141367ce697aSLuc Michel                 GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
141440d22500SChristoffer Dall             }
1415fea8a08eSJens Wiklander         }
141640d22500SChristoffer Dall     } else if (offset < 0xf30) {
141740d22500SChristoffer Dall         /* GICD_SPENDSGIRn */
14187c14b3acSMichael Davidsaver         if (s->revision == REV_11MPCORE) {
141940d22500SChristoffer Dall             goto bad_reg;
142040d22500SChristoffer Dall         }
142140d22500SChristoffer Dall         irq = (offset - 0xf20);
142240d22500SChristoffer Dall 
1423fea8a08eSJens Wiklander         if (!s->security_extn || attrs.secure ||
142467ce697aSLuc Michel             GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
142567ce697aSLuc Michel             GIC_DIST_SET_PENDING(irq, 1 << cpu);
142640d22500SChristoffer Dall             s->sgi_pending[irq][cpu] |= value;
1427fea8a08eSJens Wiklander         }
142840d22500SChristoffer Dall     } else {
142940d22500SChristoffer Dall         goto bad_reg;
1430e69954b9Spbrook     }
1431e69954b9Spbrook     gic_update(s);
1432e69954b9Spbrook     return;
1433e69954b9Spbrook bad_reg:
14348c8dc39fSPeter Maydell     qemu_log_mask(LOG_GUEST_ERROR,
14358c8dc39fSPeter Maydell                   "gic_dist_writeb: Bad offset %x\n", (int)offset);
1436e69954b9Spbrook }
1437e69954b9Spbrook 
1438a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset,
1439a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1440e69954b9Spbrook {
1441a9d85353SPeter Maydell     gic_dist_writeb(opaque, offset, value & 0xff, attrs);
1442a9d85353SPeter Maydell     gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
1443e69954b9Spbrook }
1444e69954b9Spbrook 
1445a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset,
1446a9d85353SPeter Maydell                             uint32_t value, MemTxAttrs attrs)
1447e69954b9Spbrook {
1448fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
14498da3ff18Spbrook     if (offset == 0xf00) {
14509ee6e8bbSpbrook         int cpu;
14519ee6e8bbSpbrook         int irq;
14529ee6e8bbSpbrook         int mask;
145340d22500SChristoffer Dall         int target_cpu;
14549ee6e8bbSpbrook 
1455926c4affSPeter Maydell         cpu = gic_get_current_cpu(s);
14569ee6e8bbSpbrook         irq = value & 0x3ff;
14579ee6e8bbSpbrook         switch ((value >> 24) & 3) {
14589ee6e8bbSpbrook         case 0:
14599ee6e8bbSpbrook             mask = (value >> 16) & ALL_CPU_MASK;
14609ee6e8bbSpbrook             break;
14619ee6e8bbSpbrook         case 1:
1462fa250144SAdam Lackorzynski             mask = ALL_CPU_MASK ^ (1 << cpu);
14639ee6e8bbSpbrook             break;
14649ee6e8bbSpbrook         case 2:
1465fa250144SAdam Lackorzynski             mask = 1 << cpu;
14669ee6e8bbSpbrook             break;
14679ee6e8bbSpbrook         default:
14689ee6e8bbSpbrook             DPRINTF("Bad Soft Int target filter\n");
14699ee6e8bbSpbrook             mask = ALL_CPU_MASK;
14709ee6e8bbSpbrook             break;
14719ee6e8bbSpbrook         }
147267ce697aSLuc Michel         GIC_DIST_SET_PENDING(irq, mask);
147340d22500SChristoffer Dall         target_cpu = ctz32(mask);
147440d22500SChristoffer Dall         while (target_cpu < GIC_NCPU) {
147540d22500SChristoffer Dall             s->sgi_pending[irq][target_cpu] |= (1 << cpu);
147640d22500SChristoffer Dall             mask &= ~(1 << target_cpu);
147740d22500SChristoffer Dall             target_cpu = ctz32(mask);
147840d22500SChristoffer Dall         }
14799ee6e8bbSpbrook         gic_update(s);
14809ee6e8bbSpbrook         return;
14819ee6e8bbSpbrook     }
1482a9d85353SPeter Maydell     gic_dist_writew(opaque, offset, value & 0xffff, attrs);
1483a9d85353SPeter Maydell     gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
1484a9d85353SPeter Maydell }
1485a9d85353SPeter Maydell 
1486a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
1487a9d85353SPeter Maydell                                   unsigned size, MemTxAttrs attrs)
1488a9d85353SPeter Maydell {
1489067a2b9cSLuc Michel     trace_gic_dist_write(offset, size, data);
1490067a2b9cSLuc Michel 
1491a9d85353SPeter Maydell     switch (size) {
1492a9d85353SPeter Maydell     case 1:
1493a9d85353SPeter Maydell         gic_dist_writeb(opaque, offset, data, attrs);
1494a9d85353SPeter Maydell         return MEMTX_OK;
1495a9d85353SPeter Maydell     case 2:
1496a9d85353SPeter Maydell         gic_dist_writew(opaque, offset, data, attrs);
1497a9d85353SPeter Maydell         return MEMTX_OK;
1498a9d85353SPeter Maydell     case 4:
1499a9d85353SPeter Maydell         gic_dist_writel(opaque, offset, data, attrs);
1500a9d85353SPeter Maydell         return MEMTX_OK;
1501a9d85353SPeter Maydell     default:
1502a9d85353SPeter Maydell         return MEMTX_ERROR;
1503a9d85353SPeter Maydell     }
1504e69954b9Spbrook }
1505e69954b9Spbrook 
150651fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
150751fd06e0SPeter Maydell {
150851fd06e0SPeter Maydell     /* Return the Nonsecure view of GICC_APR<regno>. This is the
150951fd06e0SPeter Maydell      * second half of GICC_NSAPR.
151051fd06e0SPeter Maydell      */
151151fd06e0SPeter Maydell     switch (GIC_MIN_BPR) {
151251fd06e0SPeter Maydell     case 0:
151351fd06e0SPeter Maydell         if (regno < 2) {
151451fd06e0SPeter Maydell             return s->nsapr[regno + 2][cpu];
151551fd06e0SPeter Maydell         }
151651fd06e0SPeter Maydell         break;
151751fd06e0SPeter Maydell     case 1:
151851fd06e0SPeter Maydell         if (regno == 0) {
151951fd06e0SPeter Maydell             return s->nsapr[regno + 1][cpu];
152051fd06e0SPeter Maydell         }
152151fd06e0SPeter Maydell         break;
152251fd06e0SPeter Maydell     case 2:
152351fd06e0SPeter Maydell         if (regno == 0) {
152451fd06e0SPeter Maydell             return extract32(s->nsapr[0][cpu], 16, 16);
152551fd06e0SPeter Maydell         }
152651fd06e0SPeter Maydell         break;
152751fd06e0SPeter Maydell     case 3:
152851fd06e0SPeter Maydell         if (regno == 0) {
152951fd06e0SPeter Maydell             return extract32(s->nsapr[0][cpu], 8, 8);
153051fd06e0SPeter Maydell         }
153151fd06e0SPeter Maydell         break;
153251fd06e0SPeter Maydell     default:
153351fd06e0SPeter Maydell         g_assert_not_reached();
153451fd06e0SPeter Maydell     }
153551fd06e0SPeter Maydell     return 0;
153651fd06e0SPeter Maydell }
153751fd06e0SPeter Maydell 
153851fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
153951fd06e0SPeter Maydell                                          uint32_t value)
154051fd06e0SPeter Maydell {
154151fd06e0SPeter Maydell     /* Write the Nonsecure view of GICC_APR<regno>. */
154251fd06e0SPeter Maydell     switch (GIC_MIN_BPR) {
154351fd06e0SPeter Maydell     case 0:
154451fd06e0SPeter Maydell         if (regno < 2) {
154551fd06e0SPeter Maydell             s->nsapr[regno + 2][cpu] = value;
154651fd06e0SPeter Maydell         }
154751fd06e0SPeter Maydell         break;
154851fd06e0SPeter Maydell     case 1:
154951fd06e0SPeter Maydell         if (regno == 0) {
155051fd06e0SPeter Maydell             s->nsapr[regno + 1][cpu] = value;
155151fd06e0SPeter Maydell         }
155251fd06e0SPeter Maydell         break;
155351fd06e0SPeter Maydell     case 2:
155451fd06e0SPeter Maydell         if (regno == 0) {
155551fd06e0SPeter Maydell             s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
155651fd06e0SPeter Maydell         }
155751fd06e0SPeter Maydell         break;
155851fd06e0SPeter Maydell     case 3:
155951fd06e0SPeter Maydell         if (regno == 0) {
156051fd06e0SPeter Maydell             s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
156151fd06e0SPeter Maydell         }
156251fd06e0SPeter Maydell         break;
156351fd06e0SPeter Maydell     default:
156451fd06e0SPeter Maydell         g_assert_not_reached();
156551fd06e0SPeter Maydell     }
156651fd06e0SPeter Maydell }
156751fd06e0SPeter Maydell 
1568a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1569a9d85353SPeter Maydell                                 uint64_t *data, MemTxAttrs attrs)
1570e69954b9Spbrook {
1571e69954b9Spbrook     switch (offset) {
1572e69954b9Spbrook     case 0x00: /* Control */
157332951860SFabian Aggeler         *data = gic_get_cpu_control(s, cpu, attrs);
1574a9d85353SPeter Maydell         break;
1575e69954b9Spbrook     case 0x04: /* Priority mask */
157681508470SFabian Aggeler         *data = gic_get_priority_mask(s, cpu, attrs);
1577a9d85353SPeter Maydell         break;
1578e69954b9Spbrook     case 0x08: /* Binary Point */
15793dd0471bSLuc Michel         if (gic_cpu_ns_access(s, cpu, attrs)) {
1580421a3c22SLuc MICHEL             if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1581421a3c22SLuc MICHEL                 /* NS view of BPR when CBPR is 1 */
1582421a3c22SLuc MICHEL                 *data = MIN(s->bpr[cpu] + 1, 7);
1583421a3c22SLuc MICHEL             } else {
1584822e9cc3SFabian Aggeler                 /* BPR is banked. Non-secure copy stored in ABPR. */
1585822e9cc3SFabian Aggeler                 *data = s->abpr[cpu];
1586421a3c22SLuc MICHEL             }
1587822e9cc3SFabian Aggeler         } else {
1588a9d85353SPeter Maydell             *data = s->bpr[cpu];
1589822e9cc3SFabian Aggeler         }
1590a9d85353SPeter Maydell         break;
1591e69954b9Spbrook     case 0x0c: /* Acknowledge */
1592c5619bf9SFabian Aggeler         *data = gic_acknowledge_irq(s, cpu, attrs);
1593a9d85353SPeter Maydell         break;
159466a0a2cbSDong Xu Wang     case 0x14: /* Running Priority */
159508efa9f2SFabian Aggeler         *data = gic_get_running_priority(s, cpu, attrs);
1596a9d85353SPeter Maydell         break;
1597e69954b9Spbrook     case 0x18: /* Highest Pending Interrupt */
15987c0fa108SFabian Aggeler         *data = gic_get_current_pending_irq(s, cpu, attrs);
1599a9d85353SPeter Maydell         break;
1600aa7d461aSChristoffer Dall     case 0x1c: /* Aliased Binary Point */
1601822e9cc3SFabian Aggeler         /* GIC v2, no security: ABPR
1602822e9cc3SFabian Aggeler          * GIC v1, no security: not implemented (RAZ/WI)
1603822e9cc3SFabian Aggeler          * With security extensions, secure access: ABPR (alias of NS BPR)
1604822e9cc3SFabian Aggeler          * With security extensions, nonsecure access: RAZ/WI
1605822e9cc3SFabian Aggeler          */
16063dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1607822e9cc3SFabian Aggeler             *data = 0;
1608822e9cc3SFabian Aggeler         } else {
1609a9d85353SPeter Maydell             *data = s->abpr[cpu];
1610822e9cc3SFabian Aggeler         }
1611a9d85353SPeter Maydell         break;
1612a9d477c4SChristoffer Dall     case 0xd0: case 0xd4: case 0xd8: case 0xdc:
161351fd06e0SPeter Maydell     {
161451fd06e0SPeter Maydell         int regno = (offset - 0xd0) / 4;
16157eb079ecSLuc Michel         int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
161651fd06e0SPeter Maydell 
16177eb079ecSLuc Michel         if (regno >= nr_aprs || s->revision != 2) {
161851fd06e0SPeter Maydell             *data = 0;
16197eb079ecSLuc Michel         } else if (gic_is_vcpu(cpu)) {
16207eb079ecSLuc Michel             *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
16213dd0471bSLuc Michel         } else if (gic_cpu_ns_access(s, cpu, attrs)) {
162251fd06e0SPeter Maydell             /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
162351fd06e0SPeter Maydell             *data = gic_apr_ns_view(s, regno, cpu);
162451fd06e0SPeter Maydell         } else {
162551fd06e0SPeter Maydell             *data = s->apr[regno][cpu];
162651fd06e0SPeter Maydell         }
1627a9d85353SPeter Maydell         break;
162851fd06e0SPeter Maydell     }
162951fd06e0SPeter Maydell     case 0xe0: case 0xe4: case 0xe8: case 0xec:
163051fd06e0SPeter Maydell     {
163151fd06e0SPeter Maydell         int regno = (offset - 0xe0) / 4;
163251fd06e0SPeter Maydell 
163351fd06e0SPeter Maydell         if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
16347eb079ecSLuc Michel             gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
163551fd06e0SPeter Maydell             *data = 0;
163651fd06e0SPeter Maydell         } else {
163751fd06e0SPeter Maydell             *data = s->nsapr[regno][cpu];
163851fd06e0SPeter Maydell         }
163951fd06e0SPeter Maydell         break;
164051fd06e0SPeter Maydell     }
1641e69954b9Spbrook     default:
16428c8dc39fSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
16438c8dc39fSPeter Maydell                       "gic_cpu_read: Bad offset %x\n", (int)offset);
16440cf09852SPeter Maydell         *data = 0;
16450cf09852SPeter Maydell         break;
1646e69954b9Spbrook     }
1647067a2b9cSLuc Michel 
1648067a2b9cSLuc Michel     trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1649067a2b9cSLuc Michel                        gic_get_vcpu_real_id(cpu), offset, *data);
1650a9d85353SPeter Maydell     return MEMTX_OK;
1651e69954b9Spbrook }
1652e69954b9Spbrook 
1653a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1654a9d85353SPeter Maydell                                  uint32_t value, MemTxAttrs attrs)
1655e69954b9Spbrook {
1656067a2b9cSLuc Michel     trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1657067a2b9cSLuc Michel                         gic_get_vcpu_real_id(cpu), offset, value);
1658067a2b9cSLuc Michel 
1659e69954b9Spbrook     switch (offset) {
1660e69954b9Spbrook     case 0x00: /* Control */
166132951860SFabian Aggeler         gic_set_cpu_control(s, cpu, value, attrs);
1662e69954b9Spbrook         break;
1663e69954b9Spbrook     case 0x04: /* Priority mask */
166481508470SFabian Aggeler         gic_set_priority_mask(s, cpu, value, attrs);
1665e69954b9Spbrook         break;
1666e69954b9Spbrook     case 0x08: /* Binary Point */
16673dd0471bSLuc Michel         if (gic_cpu_ns_access(s, cpu, attrs)) {
1668421a3c22SLuc MICHEL             if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1669421a3c22SLuc MICHEL                 /* WI when CBPR is 1 */
1670421a3c22SLuc MICHEL                 return MEMTX_OK;
1671421a3c22SLuc MICHEL             } else {
1672822e9cc3SFabian Aggeler                 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1673421a3c22SLuc MICHEL             }
1674822e9cc3SFabian Aggeler         } else {
16757eb079ecSLuc Michel             int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
16767eb079ecSLuc Michel             s->bpr[cpu] = MAX(value & 0x7, min_bpr);
1677822e9cc3SFabian Aggeler         }
1678e69954b9Spbrook         break;
1679e69954b9Spbrook     case 0x10: /* End Of Interrupt */
1680f9c6a7f1SFabian Aggeler         gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1681a9d85353SPeter Maydell         return MEMTX_OK;
1682aa7d461aSChristoffer Dall     case 0x1c: /* Aliased Binary Point */
16833dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1684822e9cc3SFabian Aggeler             /* unimplemented, or NS access: RAZ/WI */
1685822e9cc3SFabian Aggeler             return MEMTX_OK;
1686822e9cc3SFabian Aggeler         } else {
1687822e9cc3SFabian Aggeler             s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1688aa7d461aSChristoffer Dall         }
1689aa7d461aSChristoffer Dall         break;
1690a9d477c4SChristoffer Dall     case 0xd0: case 0xd4: case 0xd8: case 0xdc:
169151fd06e0SPeter Maydell     {
169251fd06e0SPeter Maydell         int regno = (offset - 0xd0) / 4;
16937eb079ecSLuc Michel         int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
169451fd06e0SPeter Maydell 
16957eb079ecSLuc Michel         if (regno >= nr_aprs || s->revision != 2) {
169651fd06e0SPeter Maydell             return MEMTX_OK;
169751fd06e0SPeter Maydell         }
16987eb079ecSLuc Michel         if (gic_is_vcpu(cpu)) {
16997eb079ecSLuc Michel             s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
17007eb079ecSLuc Michel         } else if (gic_cpu_ns_access(s, cpu, attrs)) {
170151fd06e0SPeter Maydell             /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
170251fd06e0SPeter Maydell             gic_apr_write_ns_view(s, regno, cpu, value);
170351fd06e0SPeter Maydell         } else {
170451fd06e0SPeter Maydell             s->apr[regno][cpu] = value;
170551fd06e0SPeter Maydell         }
1706a9d477c4SChristoffer Dall         break;
170751fd06e0SPeter Maydell     }
170851fd06e0SPeter Maydell     case 0xe0: case 0xe4: case 0xe8: case 0xec:
170951fd06e0SPeter Maydell     {
171051fd06e0SPeter Maydell         int regno = (offset - 0xe0) / 4;
171151fd06e0SPeter Maydell 
171251fd06e0SPeter Maydell         if (regno >= GIC_NR_APRS || s->revision != 2) {
171351fd06e0SPeter Maydell             return MEMTX_OK;
171451fd06e0SPeter Maydell         }
17157eb079ecSLuc Michel         if (gic_is_vcpu(cpu)) {
17167eb079ecSLuc Michel             return MEMTX_OK;
17177eb079ecSLuc Michel         }
17183dd0471bSLuc Michel         if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
171951fd06e0SPeter Maydell             return MEMTX_OK;
172051fd06e0SPeter Maydell         }
172151fd06e0SPeter Maydell         s->nsapr[regno][cpu] = value;
172251fd06e0SPeter Maydell         break;
172351fd06e0SPeter Maydell     }
1724a55c910eSPeter Maydell     case 0x1000:
1725a55c910eSPeter Maydell         /* GICC_DIR */
1726a55c910eSPeter Maydell         gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
1727a55c910eSPeter Maydell         break;
1728e69954b9Spbrook     default:
17298c8dc39fSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
17308c8dc39fSPeter Maydell                       "gic_cpu_write: Bad offset %x\n", (int)offset);
17310cf09852SPeter Maydell         return MEMTX_OK;
1732e69954b9Spbrook     }
1733cbe1282bSLuc Michel 
1734cbe1282bSLuc Michel     if (gic_is_vcpu(cpu)) {
1735cbe1282bSLuc Michel         gic_update_virt(s);
1736cbe1282bSLuc Michel     } else {
1737e69954b9Spbrook         gic_update(s);
1738cbe1282bSLuc Michel     }
1739cbe1282bSLuc Michel 
1740a9d85353SPeter Maydell     return MEMTX_OK;
1741e69954b9Spbrook }
1742e2c56465SPeter Maydell 
1743e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */
1744a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1745a9d85353SPeter Maydell                                     unsigned size, MemTxAttrs attrs)
1746e2c56465SPeter Maydell {
1747fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1748a9d85353SPeter Maydell     return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1749e2c56465SPeter Maydell }
1750e2c56465SPeter Maydell 
1751a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1752a9d85353SPeter Maydell                                      uint64_t value, unsigned size,
1753a9d85353SPeter Maydell                                      MemTxAttrs attrs)
1754e2c56465SPeter Maydell {
1755fae15286SPeter Maydell     GICState *s = (GICState *)opaque;
1756a9d85353SPeter Maydell     return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1757e2c56465SPeter Maydell }
1758e2c56465SPeter Maydell 
1759e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU.
1760fae15286SPeter Maydell  * These just decode the opaque pointer into GICState* + cpu id.
1761e2c56465SPeter Maydell  */
1762a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1763a9d85353SPeter Maydell                                    unsigned size, MemTxAttrs attrs)
1764e2c56465SPeter Maydell {
1765fae15286SPeter Maydell     GICState **backref = (GICState **)opaque;
1766fae15286SPeter Maydell     GICState *s = *backref;
1767e2c56465SPeter Maydell     int id = (backref - s->backref);
1768a9d85353SPeter Maydell     return gic_cpu_read(s, id, addr, data, attrs);
1769e2c56465SPeter Maydell }
1770e2c56465SPeter Maydell 
1771a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1772a9d85353SPeter Maydell                                     uint64_t value, unsigned size,
1773a9d85353SPeter Maydell                                     MemTxAttrs attrs)
1774e2c56465SPeter Maydell {
1775fae15286SPeter Maydell     GICState **backref = (GICState **)opaque;
1776fae15286SPeter Maydell     GICState *s = *backref;
1777e2c56465SPeter Maydell     int id = (backref - s->backref);
1778a9d85353SPeter Maydell     return gic_cpu_write(s, id, addr, value, attrs);
1779e2c56465SPeter Maydell }
1780e2c56465SPeter Maydell 
17812c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
17822c679ac7SLuc Michel                                     unsigned size, MemTxAttrs attrs)
17832c679ac7SLuc Michel {
17842c679ac7SLuc Michel     GICState *s = (GICState *)opaque;
17852c679ac7SLuc Michel 
17862c679ac7SLuc Michel     return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
17872c679ac7SLuc Michel }
17882c679ac7SLuc Michel 
17892c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
17902c679ac7SLuc Michel                                      uint64_t value, unsigned size,
17912c679ac7SLuc Michel                                      MemTxAttrs attrs)
17922c679ac7SLuc Michel {
17932c679ac7SLuc Michel     GICState *s = (GICState *)opaque;
17942c679ac7SLuc Michel 
17952c679ac7SLuc Michel     return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
17962c679ac7SLuc Michel }
17972c679ac7SLuc Michel 
1798527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
1799527d296fSLuc Michel {
1800527d296fSLuc Michel     int lr_idx;
1801527d296fSLuc Michel     uint32_t ret = 0;
1802527d296fSLuc Michel 
1803527d296fSLuc Michel     for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1804527d296fSLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
1805527d296fSLuc Michel         ret = deposit32(ret, lr_idx - lr_start, 1,
1806527d296fSLuc Michel                         gic_lr_entry_is_eoi(*entry));
1807527d296fSLuc Michel     }
1808527d296fSLuc Michel 
1809527d296fSLuc Michel     return ret;
1810527d296fSLuc Michel }
1811527d296fSLuc Michel 
1812527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
1813527d296fSLuc Michel {
1814527d296fSLuc Michel     int lr_idx;
1815527d296fSLuc Michel     uint32_t ret = 0;
1816527d296fSLuc Michel 
1817527d296fSLuc Michel     for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1818527d296fSLuc Michel         uint32_t *entry = &s->h_lr[lr_idx][cpu];
1819527d296fSLuc Michel         ret = deposit32(ret, lr_idx - lr_start, 1,
1820527d296fSLuc Michel                         gic_lr_entry_is_free(*entry));
1821527d296fSLuc Michel     }
1822527d296fSLuc Michel 
1823527d296fSLuc Michel     return ret;
1824527d296fSLuc Michel }
1825527d296fSLuc Michel 
1826527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
1827527d296fSLuc Michel {
1828527d296fSLuc Michel     int vcpu = gic_get_current_vcpu(s);
1829527d296fSLuc Michel     uint32_t ctlr;
1830527d296fSLuc Michel     uint32_t abpr;
1831527d296fSLuc Michel     uint32_t bpr;
1832527d296fSLuc Michel     uint32_t prio_mask;
1833527d296fSLuc Michel 
1834527d296fSLuc Michel     ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
1835527d296fSLuc Michel     abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
1836527d296fSLuc Michel     bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
1837527d296fSLuc Michel     prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
1838527d296fSLuc Michel 
1839527d296fSLuc Michel     gic_set_cpu_control(s, vcpu, ctlr, attrs);
1840527d296fSLuc Michel     s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
1841527d296fSLuc Michel     s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
1842527d296fSLuc Michel     gic_set_priority_mask(s, vcpu, prio_mask, attrs);
1843527d296fSLuc Michel }
1844527d296fSLuc Michel 
1845527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
1846527d296fSLuc Michel                                 uint64_t *data, MemTxAttrs attrs)
1847527d296fSLuc Michel {
1848527d296fSLuc Michel     GICState *s = ARM_GIC(opaque);
1849527d296fSLuc Michel     int vcpu = cpu + GIC_NCPU;
1850527d296fSLuc Michel 
1851527d296fSLuc Michel     switch (addr) {
1852527d296fSLuc Michel     case A_GICH_HCR: /* Hypervisor Control */
1853527d296fSLuc Michel         *data = s->h_hcr[cpu];
1854527d296fSLuc Michel         break;
1855527d296fSLuc Michel 
1856527d296fSLuc Michel     case A_GICH_VTR: /* VGIC Type */
1857527d296fSLuc Michel         *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
1858527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VTR, PREbits,
1859527d296fSLuc Michel                            GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
1860527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
1861527d296fSLuc Michel                            (7 - GIC_VIRT_MIN_BPR) - 1);
1862527d296fSLuc Michel         break;
1863527d296fSLuc Michel 
1864527d296fSLuc Michel     case A_GICH_VMCR: /* Virtual Machine Control */
1865527d296fSLuc Michel         *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
1866527d296fSLuc Michel                            extract32(s->cpu_ctlr[vcpu], 0, 10));
1867527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
1868527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
1869527d296fSLuc Michel         *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
1870527d296fSLuc Michel                            extract32(s->priority_mask[vcpu], 3, 5));
1871527d296fSLuc Michel         break;
1872527d296fSLuc Michel 
1873527d296fSLuc Michel     case A_GICH_MISR: /* Maintenance Interrupt Status */
1874527d296fSLuc Michel         *data = s->h_misr[cpu];
1875527d296fSLuc Michel         break;
1876527d296fSLuc Michel 
1877527d296fSLuc Michel     case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */
1878527d296fSLuc Michel     case A_GICH_EISR1:
1879527d296fSLuc Michel         *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
1880527d296fSLuc Michel         break;
1881527d296fSLuc Michel 
1882527d296fSLuc Michel     case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */
1883527d296fSLuc Michel     case A_GICH_ELRSR1:
1884527d296fSLuc Michel         *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
1885527d296fSLuc Michel         break;
1886527d296fSLuc Michel 
1887527d296fSLuc Michel     case A_GICH_APR: /* Active Priorities */
1888527d296fSLuc Michel         *data = s->h_apr[cpu];
1889527d296fSLuc Michel         break;
1890527d296fSLuc Michel 
1891527d296fSLuc Michel     case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1892527d296fSLuc Michel     {
1893527d296fSLuc Michel         int lr_idx = (addr - A_GICH_LR0) / 4;
1894527d296fSLuc Michel 
1895527d296fSLuc Michel         if (lr_idx > s->num_lrs) {
1896527d296fSLuc Michel             *data = 0;
1897527d296fSLuc Michel         } else {
1898527d296fSLuc Michel             *data = s->h_lr[lr_idx][cpu];
1899527d296fSLuc Michel         }
1900527d296fSLuc Michel         break;
1901527d296fSLuc Michel     }
1902527d296fSLuc Michel 
1903527d296fSLuc Michel     default:
1904527d296fSLuc Michel         qemu_log_mask(LOG_GUEST_ERROR,
1905527d296fSLuc Michel                       "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
1906527d296fSLuc Michel         return MEMTX_OK;
1907527d296fSLuc Michel     }
1908527d296fSLuc Michel 
1909067a2b9cSLuc Michel     trace_gic_hyp_read(addr, *data);
1910527d296fSLuc Michel     return MEMTX_OK;
1911527d296fSLuc Michel }
1912527d296fSLuc Michel 
1913527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
1914527d296fSLuc Michel                                  uint64_t value, MemTxAttrs attrs)
1915527d296fSLuc Michel {
1916527d296fSLuc Michel     GICState *s = ARM_GIC(opaque);
1917527d296fSLuc Michel     int vcpu = cpu + GIC_NCPU;
1918527d296fSLuc Michel 
1919067a2b9cSLuc Michel     trace_gic_hyp_write(addr, value);
1920067a2b9cSLuc Michel 
1921527d296fSLuc Michel     switch (addr) {
1922527d296fSLuc Michel     case A_GICH_HCR: /* Hypervisor Control */
1923527d296fSLuc Michel         s->h_hcr[cpu] = value & GICH_HCR_MASK;
1924527d296fSLuc Michel         break;
1925527d296fSLuc Michel 
1926527d296fSLuc Michel     case A_GICH_VMCR: /* Virtual Machine Control */
1927527d296fSLuc Michel         gic_vmcr_write(s, value, attrs);
1928527d296fSLuc Michel         break;
1929527d296fSLuc Michel 
1930527d296fSLuc Michel     case A_GICH_APR: /* Active Priorities */
1931527d296fSLuc Michel         s->h_apr[cpu] = value;
1932527d296fSLuc Michel         s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
1933527d296fSLuc Michel         break;
1934527d296fSLuc Michel 
1935527d296fSLuc Michel     case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1936527d296fSLuc Michel     {
1937527d296fSLuc Michel         int lr_idx = (addr - A_GICH_LR0) / 4;
1938527d296fSLuc Michel 
1939527d296fSLuc Michel         if (lr_idx > s->num_lrs) {
1940527d296fSLuc Michel             return MEMTX_OK;
1941527d296fSLuc Michel         }
1942527d296fSLuc Michel 
1943527d296fSLuc Michel         s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
1944067a2b9cSLuc Michel         trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]);
1945527d296fSLuc Michel         break;
1946527d296fSLuc Michel     }
1947527d296fSLuc Michel 
1948527d296fSLuc Michel     default:
1949527d296fSLuc Michel         qemu_log_mask(LOG_GUEST_ERROR,
1950527d296fSLuc Michel                       "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
1951527d296fSLuc Michel         return MEMTX_OK;
1952527d296fSLuc Michel     }
1953527d296fSLuc Michel 
1954cbe1282bSLuc Michel     gic_update_virt(s);
1955527d296fSLuc Michel     return MEMTX_OK;
1956527d296fSLuc Michel }
1957527d296fSLuc Michel 
1958527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1959527d296fSLuc Michel                                     unsigned size, MemTxAttrs attrs)
1960527d296fSLuc Michel {
1961527d296fSLuc Michel     GICState *s = (GICState *)opaque;
1962527d296fSLuc Michel 
1963527d296fSLuc Michel     return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
1964527d296fSLuc Michel }
1965527d296fSLuc Michel 
1966527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
1967527d296fSLuc Michel                                      uint64_t value, unsigned size,
1968527d296fSLuc Michel                                      MemTxAttrs attrs)
1969527d296fSLuc Michel {
1970527d296fSLuc Michel     GICState *s = (GICState *)opaque;
1971527d296fSLuc Michel 
1972527d296fSLuc Michel     return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
1973527d296fSLuc Michel }
1974527d296fSLuc Michel 
1975527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1976527d296fSLuc Michel                                     unsigned size, MemTxAttrs attrs)
1977527d296fSLuc Michel {
1978527d296fSLuc Michel     GICState **backref = (GICState **)opaque;
1979527d296fSLuc Michel     GICState *s = *backref;
1980527d296fSLuc Michel     int id = (backref - s->backref);
1981527d296fSLuc Michel 
1982527d296fSLuc Michel     return gic_hyp_read(s, id, addr, data, attrs);
1983527d296fSLuc Michel }
1984527d296fSLuc Michel 
1985527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
1986527d296fSLuc Michel                                      uint64_t value, unsigned size,
1987527d296fSLuc Michel                                      MemTxAttrs attrs)
1988527d296fSLuc Michel {
1989527d296fSLuc Michel     GICState **backref = (GICState **)opaque;
1990527d296fSLuc Michel     GICState *s = *backref;
1991527d296fSLuc Michel     int id = (backref - s->backref);
1992527d296fSLuc Michel 
1993527d296fSLuc Michel     return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
1994527d296fSLuc Michel 
1995527d296fSLuc Michel }
1996527d296fSLuc Michel 
19977926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = {
19987926c210SPavel Fedin     {
19997926c210SPavel Fedin         .read_with_attrs = gic_dist_read,
20007926c210SPavel Fedin         .write_with_attrs = gic_dist_write,
20017926c210SPavel Fedin         .endianness = DEVICE_NATIVE_ENDIAN,
20027926c210SPavel Fedin     },
20037926c210SPavel Fedin     {
2004a9d85353SPeter Maydell         .read_with_attrs = gic_thiscpu_read,
2005a9d85353SPeter Maydell         .write_with_attrs = gic_thiscpu_write,
2006e2c56465SPeter Maydell         .endianness = DEVICE_NATIVE_ENDIAN,
20077926c210SPavel Fedin     }
2008e2c56465SPeter Maydell };
2009e2c56465SPeter Maydell 
2010e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = {
2011a9d85353SPeter Maydell     .read_with_attrs = gic_do_cpu_read,
2012a9d85353SPeter Maydell     .write_with_attrs = gic_do_cpu_write,
2013e2c56465SPeter Maydell     .endianness = DEVICE_NATIVE_ENDIAN,
2014e2c56465SPeter Maydell };
2015e69954b9Spbrook 
20162c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = {
20172c679ac7SLuc Michel     {
2018527d296fSLuc Michel         .read_with_attrs = gic_thiscpu_hyp_read,
2019527d296fSLuc Michel         .write_with_attrs = gic_thiscpu_hyp_write,
20202c679ac7SLuc Michel         .endianness = DEVICE_NATIVE_ENDIAN,
20212c679ac7SLuc Michel     },
20222c679ac7SLuc Michel     {
20232c679ac7SLuc Michel         .read_with_attrs = gic_thisvcpu_read,
20242c679ac7SLuc Michel         .write_with_attrs = gic_thisvcpu_write,
20252c679ac7SLuc Michel         .endianness = DEVICE_NATIVE_ENDIAN,
20262c679ac7SLuc Michel     }
20272c679ac7SLuc Michel };
20282c679ac7SLuc Michel 
2029527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = {
2030527d296fSLuc Michel     .read_with_attrs = gic_do_hyp_read,
2031527d296fSLuc Michel     .write_with_attrs = gic_do_hyp_write,
2032527d296fSLuc Michel     .endianness = DEVICE_NATIVE_ENDIAN,
2033527d296fSLuc Michel };
2034527d296fSLuc Michel 
203553111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp)
20362b518c56SPeter Maydell {
203753111180SPeter Maydell     /* Device instance realize function for the GIC sysbus device */
20382b518c56SPeter Maydell     int i;
203953111180SPeter Maydell     GICState *s = ARM_GIC(dev);
204053111180SPeter Maydell     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
20411e8cae4dSPeter Maydell     ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
20420175ba10SMarkus Armbruster     Error *local_err = NULL;
20431e8cae4dSPeter Maydell 
20440175ba10SMarkus Armbruster     agc->parent_realize(dev, &local_err);
20450175ba10SMarkus Armbruster     if (local_err) {
20460175ba10SMarkus Armbruster         error_propagate(errp, local_err);
204753111180SPeter Maydell         return;
204853111180SPeter Maydell     }
20491e8cae4dSPeter Maydell 
20505d721b78SAlexander Graf     if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
20515d721b78SAlexander Graf         error_setg(errp, "KVM with user space irqchip only works when the "
20525d721b78SAlexander Graf                          "host kernel supports KVM_CAP_ARM_USER_IRQ");
20535d721b78SAlexander Graf         return;
20545d721b78SAlexander Graf     }
20555d721b78SAlexander Graf 
20562c679ac7SLuc Michel     /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if
20572c679ac7SLuc Michel      * enabled, virtualization extensions related interfaces (main virtual
20582c679ac7SLuc Michel      * interface (s->vifaceiomem[0]) and virtual CPU interface).
20592c679ac7SLuc Michel      */
20602c679ac7SLuc Michel     gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
20612b518c56SPeter Maydell 
20627926c210SPavel Fedin     /* Extra core-specific regions for the CPU interfaces. This is
20637926c210SPavel Fedin      * necessary for "franken-GIC" implementations, for example on
20647926c210SPavel Fedin      * Exynos 4.
2065e2c56465SPeter Maydell      * NB that the memory region size of 0x100 applies for the 11MPCore
2066e2c56465SPeter Maydell      * and also cores following the GIC v1 spec (ie A9).
2067e2c56465SPeter Maydell      * GIC v2 defines a larger memory region (0x1000) so this will need
2068e2c56465SPeter Maydell      * to be extended when we implement A15.
2069e2c56465SPeter Maydell      */
2070b95690c9SWei Huang     for (i = 0; i < s->num_cpu; i++) {
2071e2c56465SPeter Maydell         s->backref[i] = s;
20721437c94bSPaolo Bonzini         memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
20731437c94bSPaolo Bonzini                               &s->backref[i], "gic_cpu", 0x100);
20747926c210SPavel Fedin         sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
2075496dbcd1SPeter Maydell     }
2076527d296fSLuc Michel 
2077527d296fSLuc Michel     /* Extra core-specific regions for virtual interfaces. This is required by
2078527d296fSLuc Michel      * the GICv2 specification.
2079527d296fSLuc Michel      */
2080527d296fSLuc Michel     if (s->virt_extn) {
2081527d296fSLuc Michel         for (i = 0; i < s->num_cpu; i++) {
2082527d296fSLuc Michel             memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
2083527d296fSLuc Michel                                   &gic_viface_ops, &s->backref[i],
20847210918cSPeter Maydell                                   "gic_viface", 0x200);
2085527d296fSLuc Michel             sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
2086527d296fSLuc Michel         }
2087527d296fSLuc Michel     }
2088527d296fSLuc Michel 
2089496dbcd1SPeter Maydell }
2090496dbcd1SPeter Maydell 
2091496dbcd1SPeter Maydell static void arm_gic_class_init(ObjectClass *klass, void *data)
2092496dbcd1SPeter Maydell {
2093496dbcd1SPeter Maydell     DeviceClass *dc = DEVICE_CLASS(klass);
20941e8cae4dSPeter Maydell     ARMGICClass *agc = ARM_GIC_CLASS(klass);
209553111180SPeter Maydell 
2096bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
2097496dbcd1SPeter Maydell }
2098496dbcd1SPeter Maydell 
20998c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = {
21001e8cae4dSPeter Maydell     .name = TYPE_ARM_GIC,
21011e8cae4dSPeter Maydell     .parent = TYPE_ARM_GIC_COMMON,
2102fae15286SPeter Maydell     .instance_size = sizeof(GICState),
2103496dbcd1SPeter Maydell     .class_init = arm_gic_class_init,
2104998a74bcSPeter Maydell     .class_size = sizeof(ARMGICClass),
2105496dbcd1SPeter Maydell };
2106496dbcd1SPeter Maydell 
2107496dbcd1SPeter Maydell static void arm_gic_register_types(void)
2108496dbcd1SPeter Maydell {
2109496dbcd1SPeter Maydell     type_register_static(&arm_gic_info);
2110496dbcd1SPeter Maydell }
2111496dbcd1SPeter Maydell 
2112496dbcd1SPeter Maydell type_init(arm_gic_register_types)
2113