1e69954b9Spbrook /*
29ee6e8bbSpbrook * ARM Generic/Distributed Interrupt Controller
3e69954b9Spbrook *
49ee6e8bbSpbrook * Copyright (c) 2006-2007 CodeSourcery.
5e69954b9Spbrook * Written by Paul Brook
6e69954b9Spbrook *
78e31bf38SMatthew Fernandez * This code is licensed under the GPL.
8e69954b9Spbrook */
9e69954b9Spbrook
109ee6e8bbSpbrook /* This file contains implementation code for the RealView EB interrupt
110d256bdcSPeter Maydell * controller, MPCore distributed interrupt controller and ARMv7-M
120d256bdcSPeter Maydell * Nested Vectored Interrupt Controller.
130d256bdcSPeter Maydell * It is compiled in two ways:
140d256bdcSPeter Maydell * (1) as a standalone file to produce a sysbus device which is a GIC
150d256bdcSPeter Maydell * that can be used on the realview board and as one of the builtin
160d256bdcSPeter Maydell * private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
170d256bdcSPeter Maydell * (2) by being directly #included into armv7m_nvic.c to produce the
180d256bdcSPeter Maydell * armv7m_nvic device.
190d256bdcSPeter Maydell */
20e69954b9Spbrook
218ef94f0bSPeter Maydell #include "qemu/osdep.h"
2264552b6bSMarkus Armbruster #include "hw/irq.h"
2383c9f4caSPaolo Bonzini #include "hw/sysbus.h"
2447b43a1fSPaolo Bonzini #include "gic_internal.h"
25da34e65cSMarkus Armbruster #include "qapi/error.h"
262e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
2703dd024fSPaolo Bonzini #include "qemu/log.h"
280b8fa32fSMarkus Armbruster #include "qemu/module.h"
292531088fSHollis Blanchard #include "trace.h"
3032cad1ffSPhilippe Mathieu-Daudé #include "system/kvm.h"
3132cad1ffSPhilippe Mathieu-Daudé #include "system/qtest.h"
32386e2955SPeter Maydell
3368bf93ceSAlex Bennée /* #define DEBUG_GIC */
34e69954b9Spbrook
35e69954b9Spbrook #ifdef DEBUG_GIC
3668bf93ceSAlex Bennée #define DEBUG_GIC_GATE 1
37e69954b9Spbrook #else
3868bf93ceSAlex Bennée #define DEBUG_GIC_GATE 0
39e69954b9Spbrook #endif
40e69954b9Spbrook
4168bf93ceSAlex Bennée #define DPRINTF(fmt, ...) do { \
4268bf93ceSAlex Bennée if (DEBUG_GIC_GATE) { \
4368bf93ceSAlex Bennée fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
4468bf93ceSAlex Bennée } \
4568bf93ceSAlex Bennée } while (0)
4668bf93ceSAlex Bennée
473355c360SAlistair Francis static const uint8_t gic_id_11mpcore[] = {
483355c360SAlistair Francis 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
493355c360SAlistair Francis };
503355c360SAlistair Francis
513355c360SAlistair Francis static const uint8_t gic_id_gicv1[] = {
523355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
533355c360SAlistair Francis };
543355c360SAlistair Francis
553355c360SAlistair Francis static const uint8_t gic_id_gicv2[] = {
563355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
572a29ddeeSPeter Maydell };
582a29ddeeSPeter Maydell
gic_get_current_cpu(GICState * s)59fae15286SPeter Maydell static inline int gic_get_current_cpu(GICState *s)
60926c4affSPeter Maydell {
6109bbdb89SPhilippe Mathieu-Daudé if (!qtest_enabled() && s->num_cpu > 1) {
624917cf44SAndreas Färber return current_cpu->cpu_index;
63926c4affSPeter Maydell }
64926c4affSPeter Maydell return 0;
65926c4affSPeter Maydell }
66926c4affSPeter Maydell
gic_get_current_vcpu(GICState * s)674a37e0e4SLuc Michel static inline int gic_get_current_vcpu(GICState *s)
684a37e0e4SLuc Michel {
694a37e0e4SLuc Michel return gic_get_current_cpu(s) + GIC_NCPU;
704a37e0e4SLuc Michel }
714a37e0e4SLuc Michel
72c27a5ba9SFabian Aggeler /* Return true if this GIC config has interrupt groups, which is
73c27a5ba9SFabian Aggeler * true if we're a GICv2, or a GICv1 with the security extensions.
74c27a5ba9SFabian Aggeler */
gic_has_groups(GICState * s)75c27a5ba9SFabian Aggeler static inline bool gic_has_groups(GICState *s)
76c27a5ba9SFabian Aggeler {
77c27a5ba9SFabian Aggeler return s->revision == 2 || s->security_extn;
78c27a5ba9SFabian Aggeler }
79c27a5ba9SFabian Aggeler
gic_cpu_ns_access(GICState * s,int cpu,MemTxAttrs attrs)803dd0471bSLuc Michel static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs)
813dd0471bSLuc Michel {
823dd0471bSLuc Michel return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure;
833dd0471bSLuc Michel }
843dd0471bSLuc Michel
gic_get_best_irq(GICState * s,int cpu,int * best_irq,int * best_prio,int * group)85cbe1282bSLuc Michel static inline void gic_get_best_irq(GICState *s, int cpu,
86cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group)
87cbe1282bSLuc Michel {
88cbe1282bSLuc Michel int irq;
89cbe1282bSLuc Michel int cm = 1 << cpu;
90cbe1282bSLuc Michel
91cbe1282bSLuc Michel *best_irq = 1023;
92cbe1282bSLuc Michel *best_prio = 0x100;
93cbe1282bSLuc Michel
94cbe1282bSLuc Michel for (irq = 0; irq < s->num_irq; irq++) {
95cbe1282bSLuc Michel if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
96cbe1282bSLuc Michel (!GIC_DIST_TEST_ACTIVE(irq, cm)) &&
97cbe1282bSLuc Michel (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) {
98cbe1282bSLuc Michel if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) {
99cbe1282bSLuc Michel *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu);
100cbe1282bSLuc Michel *best_irq = irq;
101cbe1282bSLuc Michel }
102cbe1282bSLuc Michel }
103cbe1282bSLuc Michel }
104cbe1282bSLuc Michel
105cbe1282bSLuc Michel if (*best_irq < 1023) {
106cbe1282bSLuc Michel *group = GIC_DIST_TEST_GROUP(*best_irq, cm);
107cbe1282bSLuc Michel }
108cbe1282bSLuc Michel }
109cbe1282bSLuc Michel
gic_get_best_virq(GICState * s,int cpu,int * best_irq,int * best_prio,int * group)110cbe1282bSLuc Michel static inline void gic_get_best_virq(GICState *s, int cpu,
111cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group)
112cbe1282bSLuc Michel {
113cbe1282bSLuc Michel int lr_idx = 0;
114cbe1282bSLuc Michel
115cbe1282bSLuc Michel *best_irq = 1023;
116cbe1282bSLuc Michel *best_prio = 0x100;
117cbe1282bSLuc Michel
118cbe1282bSLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
119cbe1282bSLuc Michel uint32_t lr_entry = s->h_lr[lr_idx][cpu];
120cbe1282bSLuc Michel int state = GICH_LR_STATE(lr_entry);
121cbe1282bSLuc Michel
122cbe1282bSLuc Michel if (state == GICH_LR_STATE_PENDING) {
123cbe1282bSLuc Michel int prio = GICH_LR_PRIORITY(lr_entry);
124cbe1282bSLuc Michel
125cbe1282bSLuc Michel if (prio < *best_prio) {
126cbe1282bSLuc Michel *best_prio = prio;
127cbe1282bSLuc Michel *best_irq = GICH_LR_VIRT_ID(lr_entry);
128cbe1282bSLuc Michel *group = GICH_LR_GROUP(lr_entry);
129cbe1282bSLuc Michel }
130cbe1282bSLuc Michel }
131cbe1282bSLuc Michel }
132cbe1282bSLuc Michel }
133cbe1282bSLuc Michel
134cbe1282bSLuc Michel /* Return true if IRQ signaling is enabled for the given cpu and at least one
135cbe1282bSLuc Michel * of the given groups:
136cbe1282bSLuc Michel * - in the non-virt case, the distributor must be enabled for one of the
137cbe1282bSLuc Michel * given groups
138cbe1282bSLuc Michel * - in the virt case, the virtual interface must be enabled.
139cbe1282bSLuc Michel * - in all cases, the (v)CPU interface must be enabled for one of the given
140cbe1282bSLuc Michel * groups.
141cbe1282bSLuc Michel */
gic_irq_signaling_enabled(GICState * s,int cpu,bool virt,int group_mask)142cbe1282bSLuc Michel static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt,
143cbe1282bSLuc Michel int group_mask)
144cbe1282bSLuc Michel {
1454663b72aSEdgar E. Iglesias int cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
1464663b72aSEdgar E. Iglesias
147cbe1282bSLuc Michel if (!virt && !(s->ctlr & group_mask)) {
148cbe1282bSLuc Michel return false;
149cbe1282bSLuc Michel }
150cbe1282bSLuc Michel
151cbe1282bSLuc Michel if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) {
152cbe1282bSLuc Michel return false;
153cbe1282bSLuc Michel }
154cbe1282bSLuc Michel
1554663b72aSEdgar E. Iglesias if (!(s->cpu_ctlr[cpu_iface] & group_mask)) {
156cbe1282bSLuc Michel return false;
157cbe1282bSLuc Michel }
158cbe1282bSLuc Michel
159cbe1282bSLuc Michel return true;
160cbe1282bSLuc Michel }
161cbe1282bSLuc Michel
162e69954b9Spbrook /* TODO: Many places that call this routine could be optimized. */
163e69954b9Spbrook /* Update interrupt status after enabled or pending bits have been changed. */
gic_update_internal(GICState * s,bool virt)164cbe1282bSLuc Michel static inline void gic_update_internal(GICState *s, bool virt)
165e69954b9Spbrook {
166e69954b9Spbrook int best_irq;
167e69954b9Spbrook int best_prio;
168dadbb58fSPeter Maydell int irq_level, fiq_level;
169cbe1282bSLuc Michel int cpu, cpu_iface;
170cbe1282bSLuc Michel int group = 0;
171cbe1282bSLuc Michel qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq;
172cbe1282bSLuc Michel qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq;
173e69954b9Spbrook
174b95690c9SWei Huang for (cpu = 0; cpu < s->num_cpu; cpu++) {
175cbe1282bSLuc Michel cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
176cbe1282bSLuc Michel
177cbe1282bSLuc Michel s->current_pending[cpu_iface] = 1023;
178cbe1282bSLuc Michel if (!gic_irq_signaling_enabled(s, cpu, virt,
179cbe1282bSLuc Michel GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) {
180cbe1282bSLuc Michel qemu_irq_lower(irq_lines[cpu]);
181cbe1282bSLuc Michel qemu_irq_lower(fiq_lines[cpu]);
182235069a3SJohan Karlsson continue;
183e69954b9Spbrook }
184cbe1282bSLuc Michel
185cbe1282bSLuc Michel if (virt) {
186cbe1282bSLuc Michel gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group);
187cbe1282bSLuc Michel } else {
188cbe1282bSLuc Michel gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group);
189e69954b9Spbrook }
190dadbb58fSPeter Maydell
1912531088fSHollis Blanchard if (best_irq != 1023) {
192067a2b9cSLuc Michel trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu,
193067a2b9cSLuc Michel best_irq, best_prio,
194067a2b9cSLuc Michel s->priority_mask[cpu_iface],
195067a2b9cSLuc Michel s->running_priority[cpu_iface]);
1962531088fSHollis Blanchard }
1972531088fSHollis Blanchard
198dadbb58fSPeter Maydell irq_level = fiq_level = 0;
199dadbb58fSPeter Maydell
200cbe1282bSLuc Michel if (best_prio < s->priority_mask[cpu_iface]) {
201cbe1282bSLuc Michel s->current_pending[cpu_iface] = best_irq;
202cbe1282bSLuc Michel if (best_prio < s->running_priority[cpu_iface]) {
203cbe1282bSLuc Michel if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) {
204cbe1282bSLuc Michel if (group == 0 &&
205cbe1282bSLuc Michel s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) {
206dadbb58fSPeter Maydell DPRINTF("Raised pending FIQ %d (cpu %d)\n",
207cbe1282bSLuc Michel best_irq, cpu_iface);
208dadbb58fSPeter Maydell fiq_level = 1;
209cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq",
210cbe1282bSLuc Michel fiq_level);
211dadbb58fSPeter Maydell } else {
212dadbb58fSPeter Maydell DPRINTF("Raised pending IRQ %d (cpu %d)\n",
213cbe1282bSLuc Michel best_irq, cpu_iface);
214dadbb58fSPeter Maydell irq_level = 1;
215cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "virq" : "irq",
216cbe1282bSLuc Michel irq_level);
217e69954b9Spbrook }
218e69954b9Spbrook }
219dadbb58fSPeter Maydell }
220dadbb58fSPeter Maydell }
221dadbb58fSPeter Maydell
222cbe1282bSLuc Michel qemu_set_irq(irq_lines[cpu], irq_level);
223cbe1282bSLuc Michel qemu_set_irq(fiq_lines[cpu], fiq_level);
2249ee6e8bbSpbrook }
225e69954b9Spbrook }
226e69954b9Spbrook
gic_update(GICState * s)227cbe1282bSLuc Michel static void gic_update(GICState *s)
228cbe1282bSLuc Michel {
229cbe1282bSLuc Michel gic_update_internal(s, false);
230cbe1282bSLuc Michel }
231cbe1282bSLuc Michel
232527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit
233527d296fSLuc Michel * in ELRSR is set.
234527d296fSLuc Michel */
gic_lr_entry_is_free(uint32_t entry)235527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry)
236527d296fSLuc Michel {
237527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
238527d296fSLuc Michel && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry));
239527d296fSLuc Michel }
240527d296fSLuc Michel
241527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the
242673d8215SMichael Tokarev * corresponding bit in EISR is set.
243527d296fSLuc Michel */
gic_lr_entry_is_eoi(uint32_t entry)244527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry)
245527d296fSLuc Michel {
246527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
247527d296fSLuc Michel && !GICH_LR_HW(entry) && GICH_LR_EOI(entry);
248527d296fSLuc Michel }
249527d296fSLuc Michel
gic_extract_lr_info(GICState * s,int cpu,int * num_eoi,int * num_valid,int * num_pending)25050e57926SLuc Michel static inline void gic_extract_lr_info(GICState *s, int cpu,
25150e57926SLuc Michel int *num_eoi, int *num_valid, int *num_pending)
25250e57926SLuc Michel {
25350e57926SLuc Michel int lr_idx;
25450e57926SLuc Michel
25550e57926SLuc Michel *num_eoi = 0;
25650e57926SLuc Michel *num_valid = 0;
25750e57926SLuc Michel *num_pending = 0;
25850e57926SLuc Michel
25950e57926SLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
26050e57926SLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu];
26150e57926SLuc Michel
26250e57926SLuc Michel if (gic_lr_entry_is_eoi(*entry)) {
26350e57926SLuc Michel (*num_eoi)++;
26450e57926SLuc Michel }
26550e57926SLuc Michel
26650e57926SLuc Michel if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) {
26750e57926SLuc Michel (*num_valid)++;
26850e57926SLuc Michel }
26950e57926SLuc Michel
27050e57926SLuc Michel if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) {
27150e57926SLuc Michel (*num_pending)++;
27250e57926SLuc Michel }
27350e57926SLuc Michel }
27450e57926SLuc Michel }
27550e57926SLuc Michel
gic_compute_misr(GICState * s,int cpu)27650e57926SLuc Michel static void gic_compute_misr(GICState *s, int cpu)
27750e57926SLuc Michel {
27850e57926SLuc Michel uint32_t value = 0;
27950e57926SLuc Michel int vcpu = cpu + GIC_NCPU;
28050e57926SLuc Michel
28150e57926SLuc Michel int num_eoi, num_valid, num_pending;
28250e57926SLuc Michel
28350e57926SLuc Michel gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending);
28450e57926SLuc Michel
28550e57926SLuc Michel /* EOI */
28650e57926SLuc Michel if (num_eoi) {
28750e57926SLuc Michel value |= R_GICH_MISR_EOI_MASK;
28850e57926SLuc Michel }
28950e57926SLuc Michel
29050e57926SLuc Michel /* U: true if only 0 or 1 LR entry is valid */
29150e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) {
29250e57926SLuc Michel value |= R_GICH_MISR_U_MASK;
29350e57926SLuc Michel }
29450e57926SLuc Michel
29550e57926SLuc Michel /* LRENP: EOICount is not 0 */
29650e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) &&
29750e57926SLuc Michel ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) {
29850e57926SLuc Michel value |= R_GICH_MISR_LRENP_MASK;
29950e57926SLuc Michel }
30050e57926SLuc Michel
30150e57926SLuc Michel /* NP: no pending interrupts */
30250e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) {
30350e57926SLuc Michel value |= R_GICH_MISR_NP_MASK;
30450e57926SLuc Michel }
30550e57926SLuc Michel
30650e57926SLuc Michel /* VGrp0E: group0 virq signaling enabled */
30750e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) &&
30850e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
30950e57926SLuc Michel value |= R_GICH_MISR_VGrp0E_MASK;
31050e57926SLuc Michel }
31150e57926SLuc Michel
31250e57926SLuc Michel /* VGrp0D: group0 virq signaling disabled */
31350e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) &&
31450e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
31550e57926SLuc Michel value |= R_GICH_MISR_VGrp0D_MASK;
31650e57926SLuc Michel }
31750e57926SLuc Michel
31850e57926SLuc Michel /* VGrp1E: group1 virq signaling enabled */
31950e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) &&
32050e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
32150e57926SLuc Michel value |= R_GICH_MISR_VGrp1E_MASK;
32250e57926SLuc Michel }
32350e57926SLuc Michel
32450e57926SLuc Michel /* VGrp1D: group1 virq signaling disabled */
32550e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) &&
32650e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
32750e57926SLuc Michel value |= R_GICH_MISR_VGrp1D_MASK;
32850e57926SLuc Michel }
32950e57926SLuc Michel
33050e57926SLuc Michel s->h_misr[cpu] = value;
33150e57926SLuc Michel }
33250e57926SLuc Michel
gic_update_maintenance(GICState * s)33350e57926SLuc Michel static void gic_update_maintenance(GICState *s)
33450e57926SLuc Michel {
33550e57926SLuc Michel int cpu = 0;
33650e57926SLuc Michel int maint_level;
33750e57926SLuc Michel
33850e57926SLuc Michel for (cpu = 0; cpu < s->num_cpu; cpu++) {
33950e57926SLuc Michel gic_compute_misr(s, cpu);
34050e57926SLuc Michel maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu];
34150e57926SLuc Michel
342067a2b9cSLuc Michel trace_gic_update_maintenance_irq(cpu, maint_level);
34350e57926SLuc Michel qemu_set_irq(s->maintenance_irq[cpu], maint_level);
34450e57926SLuc Michel }
34550e57926SLuc Michel }
34650e57926SLuc Michel
gic_update_virt(GICState * s)347cbe1282bSLuc Michel static void gic_update_virt(GICState *s)
348cbe1282bSLuc Michel {
349cbe1282bSLuc Michel gic_update_internal(s, true);
35050e57926SLuc Michel gic_update_maintenance(s);
351cbe1282bSLuc Michel }
352cbe1282bSLuc Michel
gic_set_irq_11mpcore(GICState * s,int irq,int level,int cm,int target)3538d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
3548d999995SChristoffer Dall int cm, int target)
3558d999995SChristoffer Dall {
3568d999995SChristoffer Dall if (level) {
35767ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm);
35867ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) {
3598d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target);
36067ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target);
3618d999995SChristoffer Dall }
3628d999995SChristoffer Dall } else {
36367ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm);
3648d999995SChristoffer Dall }
3658d999995SChristoffer Dall }
3668d999995SChristoffer Dall
gic_set_irq_generic(GICState * s,int irq,int level,int cm,int target)3678d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level,
3688d999995SChristoffer Dall int cm, int target)
3698d999995SChristoffer Dall {
3708d999995SChristoffer Dall if (level) {
37167ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm);
3728d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target);
37367ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) {
37467ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target);
3758d999995SChristoffer Dall }
3768d999995SChristoffer Dall } else {
37767ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm);
3788d999995SChristoffer Dall }
3798d999995SChristoffer Dall }
3808d999995SChristoffer Dall
3819ee6e8bbSpbrook /* Process a change in an external IRQ input. */
gic_set_irq(void * opaque,int irq,int level)382e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level)
383e69954b9Spbrook {
384544d1afaSPeter Maydell /* Meaning of the 'irq' parameter:
385544d1afaSPeter Maydell * [0..N-1] : external interrupts
386544d1afaSPeter Maydell * [N..N+31] : PPI (internal) interrupts for CPU 0
387544d1afaSPeter Maydell * [N+32..N+63] : PPI (internal interrupts for CPU 1
388544d1afaSPeter Maydell * ...
389544d1afaSPeter Maydell */
390fae15286SPeter Maydell GICState *s = (GICState *)opaque;
391544d1afaSPeter Maydell int cm, target;
392544d1afaSPeter Maydell if (irq < (s->num_irq - GIC_INTERNAL)) {
393e69954b9Spbrook /* The first external input line is internal interrupt 32. */
394544d1afaSPeter Maydell cm = ALL_CPU_MASK;
39569253800SRusty Russell irq += GIC_INTERNAL;
39667ce697aSLuc Michel target = GIC_DIST_TARGET(irq);
397544d1afaSPeter Maydell } else {
398544d1afaSPeter Maydell int cpu;
399544d1afaSPeter Maydell irq -= (s->num_irq - GIC_INTERNAL);
400544d1afaSPeter Maydell cpu = irq / GIC_INTERNAL;
401544d1afaSPeter Maydell irq %= GIC_INTERNAL;
402544d1afaSPeter Maydell cm = 1 << cpu;
403544d1afaSPeter Maydell target = cm;
404544d1afaSPeter Maydell }
405544d1afaSPeter Maydell
40640d22500SChristoffer Dall assert(irq >= GIC_NR_SGIS);
40740d22500SChristoffer Dall
40867ce697aSLuc Michel if (level == GIC_DIST_TEST_LEVEL(irq, cm)) {
409e69954b9Spbrook return;
410544d1afaSPeter Maydell }
411e69954b9Spbrook
4123bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) {
4138d999995SChristoffer Dall gic_set_irq_11mpcore(s, irq, level, cm, target);
414e69954b9Spbrook } else {
4158d999995SChristoffer Dall gic_set_irq_generic(s, irq, level, cm, target);
416e69954b9Spbrook }
4172531088fSHollis Blanchard trace_gic_set_irq(irq, level, cm, target);
4188d999995SChristoffer Dall
419e69954b9Spbrook gic_update(s);
420e69954b9Spbrook }
421e69954b9Spbrook
gic_get_current_pending_irq(GICState * s,int cpu,MemTxAttrs attrs)4227c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
4237c0fa108SFabian Aggeler MemTxAttrs attrs)
4247c0fa108SFabian Aggeler {
4257c0fa108SFabian Aggeler uint16_t pending_irq = s->current_pending[cpu];
4267c0fa108SFabian Aggeler
4277c0fa108SFabian Aggeler if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
42886b350f0SLuc Michel int group = gic_test_group(s, pending_irq, cpu);
42986b350f0SLuc Michel
4307c0fa108SFabian Aggeler /* On a GIC without the security extensions, reading this register
4317c0fa108SFabian Aggeler * behaves in the same way as a secure access to a GIC with them.
4327c0fa108SFabian Aggeler */
4333dd0471bSLuc Michel bool secure = !gic_cpu_ns_access(s, cpu, attrs);
4347c0fa108SFabian Aggeler
4357c0fa108SFabian Aggeler if (group == 0 && !secure) {
4367c0fa108SFabian Aggeler /* Group0 interrupts hidden from Non-secure access */
4377c0fa108SFabian Aggeler return 1023;
4387c0fa108SFabian Aggeler }
4397c0fa108SFabian Aggeler if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
4407c0fa108SFabian Aggeler /* Group1 interrupts only seen by Secure access if
4417c0fa108SFabian Aggeler * AckCtl bit set.
4427c0fa108SFabian Aggeler */
4437c0fa108SFabian Aggeler return 1022;
4447c0fa108SFabian Aggeler }
4457c0fa108SFabian Aggeler }
4467c0fa108SFabian Aggeler return pending_irq;
4477c0fa108SFabian Aggeler }
4487c0fa108SFabian Aggeler
gic_get_group_priority(GICState * s,int cpu,int irq)449df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq)
450df92cfa6SPeter Maydell {
451df92cfa6SPeter Maydell /* Return the group priority of the specified interrupt
452df92cfa6SPeter Maydell * (which is the top bits of its priority, with the number
453df92cfa6SPeter Maydell * of bits masked determined by the applicable binary point register).
454df92cfa6SPeter Maydell */
455df92cfa6SPeter Maydell int bpr;
456df92cfa6SPeter Maydell uint32_t mask;
457df92cfa6SPeter Maydell
458df92cfa6SPeter Maydell if (gic_has_groups(s) &&
459df92cfa6SPeter Maydell !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
46086b350f0SLuc Michel gic_test_group(s, irq, cpu)) {
461fc05a6f2SLuc MICHEL bpr = s->abpr[cpu] - 1;
462fc05a6f2SLuc MICHEL assert(bpr >= 0);
463df92cfa6SPeter Maydell } else {
464df92cfa6SPeter Maydell bpr = s->bpr[cpu];
465df92cfa6SPeter Maydell }
466df92cfa6SPeter Maydell
467df92cfa6SPeter Maydell /* a BPR of 0 means the group priority bits are [7:1];
468df92cfa6SPeter Maydell * a BPR of 1 means they are [7:2], and so on down to
469df92cfa6SPeter Maydell * a BPR of 7 meaning no group priority bits at all.
470df92cfa6SPeter Maydell */
471df92cfa6SPeter Maydell mask = ~0U << ((bpr & 7) + 1);
472df92cfa6SPeter Maydell
47386b350f0SLuc Michel return gic_get_priority(s, irq, cpu) & mask;
474df92cfa6SPeter Maydell }
475df92cfa6SPeter Maydell
gic_activate_irq(GICState * s,int cpu,int irq)47672889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq)
477e69954b9Spbrook {
47872889c8aSPeter Maydell /* Set the appropriate Active Priority Register bit for this IRQ,
47972889c8aSPeter Maydell * and update the running priority.
48072889c8aSPeter Maydell */
48172889c8aSPeter Maydell int prio = gic_get_group_priority(s, cpu, irq);
482a1d7b8d8SLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
483a1d7b8d8SLuc Michel int preemption_level = prio >> (min_bpr + 1);
48472889c8aSPeter Maydell int regno = preemption_level / 32;
48572889c8aSPeter Maydell int bitno = preemption_level % 32;
486a1d7b8d8SLuc Michel uint32_t *papr = NULL;
48772889c8aSPeter Maydell
488a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) {
489a1d7b8d8SLuc Michel assert(regno == 0);
490a1d7b8d8SLuc Michel papr = &s->h_apr[gic_get_vcpu_real_id(cpu)];
491a1d7b8d8SLuc Michel } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) {
492a1d7b8d8SLuc Michel papr = &s->nsapr[regno][cpu];
4939ee6e8bbSpbrook } else {
494a1d7b8d8SLuc Michel papr = &s->apr[regno][cpu];
4959ee6e8bbSpbrook }
49672889c8aSPeter Maydell
497a1d7b8d8SLuc Michel *papr |= (1 << bitno);
498a1d7b8d8SLuc Michel
49972889c8aSPeter Maydell s->running_priority[cpu] = prio;
50086b350f0SLuc Michel gic_set_active(s, irq, cpu);
50172889c8aSPeter Maydell }
50272889c8aSPeter Maydell
gic_get_prio_from_apr_bits(GICState * s,int cpu)50372889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
50472889c8aSPeter Maydell {
50572889c8aSPeter Maydell /* Recalculate the current running priority for this CPU based
50672889c8aSPeter Maydell * on the set bits in the Active Priority Registers.
50772889c8aSPeter Maydell */
50872889c8aSPeter Maydell int i;
509a1d7b8d8SLuc Michel
510a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) {
511a1d7b8d8SLuc Michel uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)];
512a1d7b8d8SLuc Michel if (apr) {
513a1d7b8d8SLuc Michel return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1);
514a1d7b8d8SLuc Michel } else {
515a1d7b8d8SLuc Michel return 0x100;
516a1d7b8d8SLuc Michel }
517a1d7b8d8SLuc Michel }
518a1d7b8d8SLuc Michel
51972889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) {
52072889c8aSPeter Maydell uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
52172889c8aSPeter Maydell if (!apr) {
52272889c8aSPeter Maydell continue;
52372889c8aSPeter Maydell }
52472889c8aSPeter Maydell return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
52572889c8aSPeter Maydell }
52672889c8aSPeter Maydell return 0x100;
52772889c8aSPeter Maydell }
52872889c8aSPeter Maydell
gic_drop_prio(GICState * s,int cpu,int group)52972889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group)
53072889c8aSPeter Maydell {
53172889c8aSPeter Maydell /* Drop the priority of the currently active interrupt in the
53272889c8aSPeter Maydell * specified group.
53372889c8aSPeter Maydell *
53472889c8aSPeter Maydell * Note that we can guarantee (because of the requirement to nest
53572889c8aSPeter Maydell * GICC_IAR reads [which activate an interrupt and raise priority]
53672889c8aSPeter Maydell * with GICC_EOIR writes [which drop the priority for the interrupt])
53772889c8aSPeter Maydell * that the interrupt we're being called for is the highest priority
53872889c8aSPeter Maydell * active interrupt, meaning that it has the lowest set bit in the
53972889c8aSPeter Maydell * APR registers.
54072889c8aSPeter Maydell *
54172889c8aSPeter Maydell * If the guest does not honour the ordering constraints then the
54272889c8aSPeter Maydell * behaviour of the GIC is UNPREDICTABLE, which for us means that
54372889c8aSPeter Maydell * the values of the APR registers might become incorrect and the
54472889c8aSPeter Maydell * running priority will be wrong, so interrupts that should preempt
54572889c8aSPeter Maydell * might not do so, and interrupts that should not preempt might do so.
54672889c8aSPeter Maydell */
547a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) {
548a1d7b8d8SLuc Michel int rcpu = gic_get_vcpu_real_id(cpu);
549a1d7b8d8SLuc Michel
550a1d7b8d8SLuc Michel if (s->h_apr[rcpu]) {
551a1d7b8d8SLuc Michel /* Clear lowest set bit */
552a1d7b8d8SLuc Michel s->h_apr[rcpu] &= s->h_apr[rcpu] - 1;
553a1d7b8d8SLuc Michel }
554a1d7b8d8SLuc Michel } else {
55572889c8aSPeter Maydell int i;
55672889c8aSPeter Maydell
55772889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) {
55872889c8aSPeter Maydell uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
55972889c8aSPeter Maydell if (!*papr) {
56072889c8aSPeter Maydell continue;
56172889c8aSPeter Maydell }
56272889c8aSPeter Maydell /* Clear lowest set bit */
56372889c8aSPeter Maydell *papr &= *papr - 1;
56472889c8aSPeter Maydell break;
56572889c8aSPeter Maydell }
566a1d7b8d8SLuc Michel }
56772889c8aSPeter Maydell
56872889c8aSPeter Maydell s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
569e69954b9Spbrook }
570e69954b9Spbrook
gic_clear_pending_sgi(GICState * s,int irq,int cpu)571439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu)
572e69954b9Spbrook {
573439badd6SLuc Michel int src;
574439badd6SLuc Michel uint32_t ret;
575c5619bf9SFabian Aggeler
576439badd6SLuc Michel if (!gic_is_vcpu(cpu)) {
57740d22500SChristoffer Dall /* Lookup the source CPU for the SGI and clear this in the
57840d22500SChristoffer Dall * sgi_pending map. Return the src and clear the overall pending
57940d22500SChristoffer Dall * state on this CPU if the SGI is not pending from any CPUs.
58040d22500SChristoffer Dall */
58140d22500SChristoffer Dall assert(s->sgi_pending[irq][cpu] != 0);
58240d22500SChristoffer Dall src = ctz32(s->sgi_pending[irq][cpu]);
58340d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~(1 << src);
58440d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) {
58586b350f0SLuc Michel gic_clear_pending(s, irq, cpu);
58640d22500SChristoffer Dall }
58740d22500SChristoffer Dall ret = irq | ((src & 0x7) << 10);
58840d22500SChristoffer Dall } else {
589439badd6SLuc Michel uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu);
590439badd6SLuc Michel src = GICH_LR_CPUID(*lr_entry);
591439badd6SLuc Michel
592439badd6SLuc Michel gic_clear_pending(s, irq, cpu);
593439badd6SLuc Michel ret = irq | (src << 10);
594439badd6SLuc Michel }
595439badd6SLuc Michel
596439badd6SLuc Michel return ret;
597439badd6SLuc Michel }
598439badd6SLuc Michel
gic_acknowledge_irq(GICState * s,int cpu,MemTxAttrs attrs)599439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
600439badd6SLuc Michel {
601439badd6SLuc Michel int ret, irq;
602439badd6SLuc Michel
603439badd6SLuc Michel /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
604439badd6SLuc Michel * for the case where this GIC supports grouping and the pending interrupt
605439badd6SLuc Michel * is in the wrong group.
60640d22500SChristoffer Dall */
607439badd6SLuc Michel irq = gic_get_current_pending_irq(s, cpu, attrs);
608067a2b9cSLuc Michel trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
609067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), irq);
610439badd6SLuc Michel
611439badd6SLuc Michel if (irq >= GIC_MAXIRQ) {
612439badd6SLuc Michel DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
613439badd6SLuc Michel return irq;
614439badd6SLuc Michel }
615439badd6SLuc Michel
616439badd6SLuc Michel if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) {
617439badd6SLuc Michel DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
618439badd6SLuc Michel return 1023;
619439badd6SLuc Michel }
620439badd6SLuc Michel
621439badd6SLuc Michel gic_activate_irq(s, cpu, irq);
622439badd6SLuc Michel
623439badd6SLuc Michel if (s->revision == REV_11MPCORE) {
624439badd6SLuc Michel /* Clear pending flags for both level and edge triggered interrupts.
625439badd6SLuc Michel * Level triggered IRQs will be reasserted once they become inactive.
626439badd6SLuc Michel */
627439badd6SLuc Michel gic_clear_pending(s, irq, cpu);
628439badd6SLuc Michel ret = irq;
629439badd6SLuc Michel } else {
630439badd6SLuc Michel if (irq < GIC_NR_SGIS) {
631439badd6SLuc Michel ret = gic_clear_pending_sgi(s, irq, cpu);
632439badd6SLuc Michel } else {
63386b350f0SLuc Michel gic_clear_pending(s, irq, cpu);
63440d22500SChristoffer Dall ret = irq;
63540d22500SChristoffer Dall }
63640d22500SChristoffer Dall }
63740d22500SChristoffer Dall
638cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) {
639cbe1282bSLuc Michel gic_update_virt(s);
640cbe1282bSLuc Michel } else {
64172889c8aSPeter Maydell gic_update(s);
642cbe1282bSLuc Michel }
64340d22500SChristoffer Dall DPRINTF("ACK %d\n", irq);
64440d22500SChristoffer Dall return ret;
645e69954b9Spbrook }
646e69954b9Spbrook
gic_fullprio_mask(GICState * s,int cpu)64711411489SSai Pavan Boddu static uint32_t gic_fullprio_mask(GICState *s, int cpu)
64811411489SSai Pavan Boddu {
64911411489SSai Pavan Boddu /*
65011411489SSai Pavan Boddu * Return a mask word which clears the unimplemented priority
65111411489SSai Pavan Boddu * bits from a priority value for an interrupt. (Not to be
65211411489SSai Pavan Boddu * confused with the group priority, whose mask depends on BPR.)
65311411489SSai Pavan Boddu */
65411411489SSai Pavan Boddu int priBits;
65511411489SSai Pavan Boddu
65611411489SSai Pavan Boddu if (gic_is_vcpu(cpu)) {
65711411489SSai Pavan Boddu priBits = GIC_VIRT_MAX_GROUP_PRIO_BITS;
65811411489SSai Pavan Boddu } else {
65911411489SSai Pavan Boddu priBits = s->n_prio_bits;
66011411489SSai Pavan Boddu }
66111411489SSai Pavan Boddu return ~0U << (8 - priBits);
66211411489SSai Pavan Boddu }
66311411489SSai Pavan Boddu
gic_dist_set_priority(GICState * s,int cpu,int irq,uint8_t val,MemTxAttrs attrs)66467ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
66581508470SFabian Aggeler MemTxAttrs attrs)
6669df90ad0SChristoffer Dall {
66781508470SFabian Aggeler if (s->security_extn && !attrs.secure) {
66867ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
66981508470SFabian Aggeler return; /* Ignore Non-secure access of Group0 IRQ */
67081508470SFabian Aggeler }
67181508470SFabian Aggeler val = 0x80 | (val >> 1); /* Non-secure view */
67281508470SFabian Aggeler }
67381508470SFabian Aggeler
67411411489SSai Pavan Boddu val &= gic_fullprio_mask(s, cpu);
67511411489SSai Pavan Boddu
6769df90ad0SChristoffer Dall if (irq < GIC_INTERNAL) {
6779df90ad0SChristoffer Dall s->priority1[irq][cpu] = val;
6789df90ad0SChristoffer Dall } else {
6799df90ad0SChristoffer Dall s->priority2[(irq) - GIC_INTERNAL] = val;
6809df90ad0SChristoffer Dall }
6819df90ad0SChristoffer Dall }
6829df90ad0SChristoffer Dall
gic_dist_get_priority(GICState * s,int cpu,int irq,MemTxAttrs attrs)68367ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
68481508470SFabian Aggeler MemTxAttrs attrs)
68581508470SFabian Aggeler {
68667ce697aSLuc Michel uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
68781508470SFabian Aggeler
68881508470SFabian Aggeler if (s->security_extn && !attrs.secure) {
68967ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
69081508470SFabian Aggeler return 0; /* Non-secure access cannot read priority of Group0 IRQ */
69181508470SFabian Aggeler }
69281508470SFabian Aggeler prio = (prio << 1) & 0xff; /* Non-secure view */
69381508470SFabian Aggeler }
69411411489SSai Pavan Boddu return prio & gic_fullprio_mask(s, cpu);
69581508470SFabian Aggeler }
69681508470SFabian Aggeler
gic_set_priority_mask(GICState * s,int cpu,uint8_t pmask,MemTxAttrs attrs)69781508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
69881508470SFabian Aggeler MemTxAttrs attrs)
69981508470SFabian Aggeler {
7003dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
70181508470SFabian Aggeler if (s->priority_mask[cpu] & 0x80) {
70281508470SFabian Aggeler /* Priority Mask in upper half */
70381508470SFabian Aggeler pmask = 0x80 | (pmask >> 1);
70481508470SFabian Aggeler } else {
70581508470SFabian Aggeler /* Non-secure write ignored if priority mask is in lower half */
70681508470SFabian Aggeler return;
70781508470SFabian Aggeler }
70881508470SFabian Aggeler }
70911411489SSai Pavan Boddu s->priority_mask[cpu] = pmask & gic_fullprio_mask(s, cpu);
71081508470SFabian Aggeler }
71181508470SFabian Aggeler
gic_get_priority_mask(GICState * s,int cpu,MemTxAttrs attrs)71281508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
71381508470SFabian Aggeler {
71481508470SFabian Aggeler uint32_t pmask = s->priority_mask[cpu];
71581508470SFabian Aggeler
7163dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
71781508470SFabian Aggeler if (pmask & 0x80) {
71881508470SFabian Aggeler /* Priority Mask in upper half, return Non-secure view */
71981508470SFabian Aggeler pmask = (pmask << 1) & 0xff;
72081508470SFabian Aggeler } else {
72181508470SFabian Aggeler /* Priority Mask in lower half, RAZ */
72281508470SFabian Aggeler pmask = 0;
72381508470SFabian Aggeler }
72481508470SFabian Aggeler }
72581508470SFabian Aggeler return pmask;
72681508470SFabian Aggeler }
72781508470SFabian Aggeler
gic_get_cpu_control(GICState * s,int cpu,MemTxAttrs attrs)72832951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
72932951860SFabian Aggeler {
73032951860SFabian Aggeler uint32_t ret = s->cpu_ctlr[cpu];
73132951860SFabian Aggeler
7323dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
73332951860SFabian Aggeler /* Construct the NS banked view of GICC_CTLR from the correct
73432951860SFabian Aggeler * bits of the S banked view. We don't need to move the bypass
73532951860SFabian Aggeler * control bits because we don't implement that (IMPDEF) part
73632951860SFabian Aggeler * of the GIC architecture.
73732951860SFabian Aggeler */
73832951860SFabian Aggeler ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
73932951860SFabian Aggeler }
74032951860SFabian Aggeler return ret;
74132951860SFabian Aggeler }
74232951860SFabian Aggeler
gic_set_cpu_control(GICState * s,int cpu,uint32_t value,MemTxAttrs attrs)74332951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
74432951860SFabian Aggeler MemTxAttrs attrs)
74532951860SFabian Aggeler {
74632951860SFabian Aggeler uint32_t mask;
74732951860SFabian Aggeler
7483dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
74932951860SFabian Aggeler /* The NS view can only write certain bits in the register;
75032951860SFabian Aggeler * the rest are unchanged
75132951860SFabian Aggeler */
75232951860SFabian Aggeler mask = GICC_CTLR_EN_GRP1;
75332951860SFabian Aggeler if (s->revision == 2) {
75432951860SFabian Aggeler mask |= GICC_CTLR_EOIMODE_NS;
75532951860SFabian Aggeler }
75632951860SFabian Aggeler s->cpu_ctlr[cpu] &= ~mask;
75732951860SFabian Aggeler s->cpu_ctlr[cpu] |= (value << 1) & mask;
75832951860SFabian Aggeler } else {
75932951860SFabian Aggeler if (s->revision == 2) {
76032951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
76132951860SFabian Aggeler } else {
76232951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
76332951860SFabian Aggeler }
76432951860SFabian Aggeler s->cpu_ctlr[cpu] = value & mask;
76532951860SFabian Aggeler }
76632951860SFabian Aggeler DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
76732951860SFabian Aggeler "Group1 Interrupts %sabled\n", cpu,
76832951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
76932951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
77032951860SFabian Aggeler }
77132951860SFabian Aggeler
gic_get_running_priority(GICState * s,int cpu,MemTxAttrs attrs)77208efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
77308efa9f2SFabian Aggeler {
77471aa735bSLuc MICHEL if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
77571aa735bSLuc MICHEL /* Idle priority */
77671aa735bSLuc MICHEL return 0xff;
77771aa735bSLuc MICHEL }
77871aa735bSLuc MICHEL
7793dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
78008efa9f2SFabian Aggeler if (s->running_priority[cpu] & 0x80) {
78108efa9f2SFabian Aggeler /* Running priority in upper half of range: return the Non-secure
78208efa9f2SFabian Aggeler * view of the priority.
78308efa9f2SFabian Aggeler */
78408efa9f2SFabian Aggeler return s->running_priority[cpu] << 1;
78508efa9f2SFabian Aggeler } else {
78608efa9f2SFabian Aggeler /* Running priority in lower half of range: RAZ */
78708efa9f2SFabian Aggeler return 0;
78808efa9f2SFabian Aggeler }
78908efa9f2SFabian Aggeler } else {
79008efa9f2SFabian Aggeler return s->running_priority[cpu];
79108efa9f2SFabian Aggeler }
79208efa9f2SFabian Aggeler }
79308efa9f2SFabian Aggeler
794a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation,
795a55c910eSPeter Maydell * ie whether the relevant EOIMode bit is set.
796a55c910eSPeter Maydell */
gic_eoi_split(GICState * s,int cpu,MemTxAttrs attrs)797a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
798a55c910eSPeter Maydell {
799a55c910eSPeter Maydell if (s->revision != 2) {
800a55c910eSPeter Maydell /* Before GICv2 prio-drop and deactivate are not separable */
801a55c910eSPeter Maydell return false;
802a55c910eSPeter Maydell }
8033dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
804a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
805a55c910eSPeter Maydell }
806a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
807a55c910eSPeter Maydell }
808a55c910eSPeter Maydell
gic_deactivate_irq(GICState * s,int cpu,int irq,MemTxAttrs attrs)809a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
810a55c910eSPeter Maydell {
811ee03cca8SPeter Maydell int group;
812ee03cca8SPeter Maydell
81302f2e22dSLuc Michel if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
814ee03cca8SPeter Maydell /*
815ee03cca8SPeter Maydell * This handles two cases:
816ee03cca8SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023]
817ee03cca8SPeter Maydell * to the GICC_DIR, the GIC ignores that write.
818ee03cca8SPeter Maydell * 2. If software writes the number of a non-existent interrupt
819ee03cca8SPeter Maydell * this must be a subcase of "value written is not an active interrupt"
82002f2e22dSLuc Michel * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs,
82102f2e22dSLuc Michel * all IRQs potentially exist, so this limit does not apply.
822ee03cca8SPeter Maydell */
823ee03cca8SPeter Maydell return;
824ee03cca8SPeter Maydell }
825ee03cca8SPeter Maydell
826a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) {
827a55c910eSPeter Maydell /* This is UNPREDICTABLE; we choose to ignore it */
828a55c910eSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR,
829a55c910eSPeter Maydell "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
830a55c910eSPeter Maydell return;
831a55c910eSPeter Maydell }
832a55c910eSPeter Maydell
83302f2e22dSLuc Michel if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
83402f2e22dSLuc Michel /* This vIRQ does not have an LR entry which is either active or
83502f2e22dSLuc Michel * pending and active. Increment EOICount and ignore the write.
83602f2e22dSLuc Michel */
83702f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu);
83802f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
839cbe1282bSLuc Michel
840cbe1282bSLuc Michel /* Update the virtual interface in case a maintenance interrupt should
841cbe1282bSLuc Michel * be raised.
842cbe1282bSLuc Michel */
843cbe1282bSLuc Michel gic_update_virt(s);
84402f2e22dSLuc Michel return;
84502f2e22dSLuc Michel }
84602f2e22dSLuc Michel
84702f2e22dSLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
84802f2e22dSLuc Michel
8493dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
850a55c910eSPeter Maydell DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
851a55c910eSPeter Maydell return;
852a55c910eSPeter Maydell }
853a55c910eSPeter Maydell
85486b350f0SLuc Michel gic_clear_active(s, irq, cpu);
855a55c910eSPeter Maydell }
856a55c910eSPeter Maydell
gic_complete_irq(GICState * s,int cpu,int irq,MemTxAttrs attrs)85750491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
858e69954b9Spbrook {
8599ee6e8bbSpbrook int cm = 1 << cpu;
86072889c8aSPeter Maydell int group;
86172889c8aSPeter Maydell
862df628ff1Spbrook DPRINTF("EOI %d\n", irq);
86302f2e22dSLuc Michel if (gic_is_vcpu(cpu)) {
86402f2e22dSLuc Michel /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the
86502f2e22dSLuc Michel * running prio is < 0x100.
86602f2e22dSLuc Michel */
86702f2e22dSLuc Michel bool prio_drop = s->running_priority[cpu] < 0x100;
86802f2e22dSLuc Michel
86902f2e22dSLuc Michel if (irq >= GIC_MAXIRQ) {
87002f2e22dSLuc Michel /* Ignore spurious interrupt */
87102f2e22dSLuc Michel return;
87202f2e22dSLuc Michel }
87302f2e22dSLuc Michel
87402f2e22dSLuc Michel gic_drop_prio(s, cpu, 0);
87502f2e22dSLuc Michel
87602f2e22dSLuc Michel if (!gic_eoi_split(s, cpu, attrs)) {
87702f2e22dSLuc Michel bool valid = gic_virq_is_valid(s, irq, cpu);
87802f2e22dSLuc Michel if (prio_drop && !valid) {
87902f2e22dSLuc Michel /* We are in a situation where:
88002f2e22dSLuc Michel * - V_CTRL.EOIMode is false (no EOI split),
88102f2e22dSLuc Michel * - The call to gic_drop_prio() cleared a bit in GICH_APR,
88202f2e22dSLuc Michel * - This vIRQ does not have an LR entry which is either
88302f2e22dSLuc Michel * active or pending and active.
88402f2e22dSLuc Michel * In that case, we must increment EOICount.
88502f2e22dSLuc Michel */
88602f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu);
88702f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
88802f2e22dSLuc Michel } else if (valid) {
88902f2e22dSLuc Michel gic_clear_active(s, irq, cpu);
89002f2e22dSLuc Michel }
89102f2e22dSLuc Michel }
89202f2e22dSLuc Michel
893cbe1282bSLuc Michel gic_update_virt(s);
89402f2e22dSLuc Michel return;
89502f2e22dSLuc Michel }
89602f2e22dSLuc Michel
897a32134aaSMark Langsdorf if (irq >= s->num_irq) {
898217bfb44SPeter Maydell /* This handles two cases:
899217bfb44SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023]
900217bfb44SPeter Maydell * to the GICC_EOIR, the GIC ignores that write.
901217bfb44SPeter Maydell * 2. If software writes the number of a non-existent interrupt
902217bfb44SPeter Maydell * this must be a subcase of "value written does not match the last
903217bfb44SPeter Maydell * valid interrupt value read from the Interrupt Acknowledge
904217bfb44SPeter Maydell * register" and so this is UNPREDICTABLE. We choose to ignore it.
905217bfb44SPeter Maydell */
906217bfb44SPeter Maydell return;
907217bfb44SPeter Maydell }
90872889c8aSPeter Maydell if (s->running_priority[cpu] == 0x100) {
909e69954b9Spbrook return; /* No active IRQ. */
91072889c8aSPeter Maydell }
9118d999995SChristoffer Dall
9123bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) {
913e69954b9Spbrook /* Mark level triggered interrupts as pending if they are still
914e69954b9Spbrook raised. */
91567ce697aSLuc Michel if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
91667ce697aSLuc Michel && GIC_DIST_TEST_LEVEL(irq, cm)
91767ce697aSLuc Michel && (GIC_DIST_TARGET(irq) & cm) != 0) {
9189ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq, cm);
91967ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, cm);
920e69954b9Spbrook }
9218d999995SChristoffer Dall }
9228d999995SChristoffer Dall
92386b350f0SLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
92472889c8aSPeter Maydell
9253dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
926f9c6a7f1SFabian Aggeler DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
927f9c6a7f1SFabian Aggeler return;
928f9c6a7f1SFabian Aggeler }
929f9c6a7f1SFabian Aggeler
930f9c6a7f1SFabian Aggeler /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
931f9c6a7f1SFabian Aggeler * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
932f9c6a7f1SFabian Aggeler * i.e. go ahead and complete the irq anyway.
933f9c6a7f1SFabian Aggeler */
934f9c6a7f1SFabian Aggeler
93572889c8aSPeter Maydell gic_drop_prio(s, cpu, group);
936a55c910eSPeter Maydell
937a55c910eSPeter Maydell /* In GICv2 the guest can choose to split priority-drop and deactivate */
938a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) {
93986b350f0SLuc Michel gic_clear_active(s, irq, cpu);
940a55c910eSPeter Maydell }
941e69954b9Spbrook gic_update(s);
942e69954b9Spbrook }
943e69954b9Spbrook
gic_dist_readb(void * opaque,hwaddr offset,MemTxAttrs attrs)94469e7e60dSAlex Bennée static uint8_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
945e69954b9Spbrook {
946fae15286SPeter Maydell GICState *s = (GICState *)opaque;
947e69954b9Spbrook uint32_t res;
948e69954b9Spbrook int irq;
949e69954b9Spbrook int i;
9509ee6e8bbSpbrook int cpu;
9519ee6e8bbSpbrook int cm;
9529ee6e8bbSpbrook int mask;
953e69954b9Spbrook
954926c4affSPeter Maydell cpu = gic_get_current_cpu(s);
9559ee6e8bbSpbrook cm = 1 << cpu;
956e69954b9Spbrook if (offset < 0x100) {
957679aa175SFabian Aggeler if (offset == 0) { /* GICD_CTLR */
95869e7e60dSAlex Bennée /* We rely here on the only non-zero bits being in byte 0 */
959679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) {
960679aa175SFabian Aggeler /* The NS bank of this register is just an alias of the
961679aa175SFabian Aggeler * EnableGrp1 bit in the S bank version.
962679aa175SFabian Aggeler */
963679aa175SFabian Aggeler return extract32(s->ctlr, 1, 1);
964679aa175SFabian Aggeler } else {
965679aa175SFabian Aggeler return s->ctlr;
966679aa175SFabian Aggeler }
967679aa175SFabian Aggeler }
96869e7e60dSAlex Bennée if (offset == 4) {
96969e7e60dSAlex Bennée /* GICD_TYPER byte 0 */
97069e7e60dSAlex Bennée return ((s->num_irq / 32) - 1) | ((s->num_cpu - 1) << 5);
97169e7e60dSAlex Bennée }
97269e7e60dSAlex Bennée if (offset == 5) {
97369e7e60dSAlex Bennée /* GICD_TYPER byte 1 */
97469e7e60dSAlex Bennée return (s->security_extn << 2);
97569e7e60dSAlex Bennée }
9763d5af538SAlex Bennée if (offset == 8) {
9773d5af538SAlex Bennée /* GICD_IIDR byte 0 */
9783d5af538SAlex Bennée return 0x3b; /* Arm JEP106 identity */
9793d5af538SAlex Bennée }
9803d5af538SAlex Bennée if (offset == 9) {
9813d5af538SAlex Bennée /* GICD_IIDR byte 1 */
9823d5af538SAlex Bennée return 0x04; /* Arm JEP106 identity */
9833d5af538SAlex Bennée }
9843d5af538SAlex Bennée if (offset < 0x0c) {
9853d5af538SAlex Bennée /* All other bytes in this range are RAZ */
986e69954b9Spbrook return 0;
9873d5af538SAlex Bennée }
988b79f2265SRob Herring if (offset >= 0x80) {
989c27a5ba9SFabian Aggeler /* Interrupt Group Registers: these RAZ/WI if this is an NS
990c27a5ba9SFabian Aggeler * access to a GIC with the security extensions, or if the GIC
991c27a5ba9SFabian Aggeler * doesn't have groups at all.
992c27a5ba9SFabian Aggeler */
993c27a5ba9SFabian Aggeler res = 0;
994c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
995c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */
996b6e6c651SPeter Maydell irq = (offset - 0x080) * 8;
997c27a5ba9SFabian Aggeler if (irq >= s->num_irq) {
998c27a5ba9SFabian Aggeler goto bad_reg;
999c27a5ba9SFabian Aggeler }
1000c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) {
100167ce697aSLuc Michel if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
1002c27a5ba9SFabian Aggeler res |= (1 << i);
1003c27a5ba9SFabian Aggeler }
1004c27a5ba9SFabian Aggeler }
1005c27a5ba9SFabian Aggeler }
1006c27a5ba9SFabian Aggeler return res;
1007b79f2265SRob Herring }
1008e69954b9Spbrook goto bad_reg;
1009e69954b9Spbrook } else if (offset < 0x200) {
1010e69954b9Spbrook /* Interrupt Set/Clear Enable. */
1011e69954b9Spbrook if (offset < 0x180)
1012e69954b9Spbrook irq = (offset - 0x100) * 8;
1013e69954b9Spbrook else
1014e69954b9Spbrook irq = (offset - 0x180) * 8;
1015a32134aaSMark Langsdorf if (irq >= s->num_irq)
1016e69954b9Spbrook goto bad_reg;
1017e69954b9Spbrook res = 0;
1018e69954b9Spbrook for (i = 0; i < 8; i++) {
1019fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
102067ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1021fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1022fea8a08eSJens Wiklander }
1023fea8a08eSJens Wiklander
102467ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1025e69954b9Spbrook res |= (1 << i);
1026e69954b9Spbrook }
1027e69954b9Spbrook }
1028e69954b9Spbrook } else if (offset < 0x300) {
1029e69954b9Spbrook /* Interrupt Set/Clear Pending. */
1030e69954b9Spbrook if (offset < 0x280)
1031e69954b9Spbrook irq = (offset - 0x200) * 8;
1032e69954b9Spbrook else
1033e69954b9Spbrook irq = (offset - 0x280) * 8;
1034a32134aaSMark Langsdorf if (irq >= s->num_irq)
1035e69954b9Spbrook goto bad_reg;
1036e69954b9Spbrook res = 0;
103769253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
1038e69954b9Spbrook for (i = 0; i < 8; i++) {
1039fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
104067ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1041fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1042fea8a08eSJens Wiklander }
1043fea8a08eSJens Wiklander
10448d999995SChristoffer Dall if (gic_test_pending(s, irq + i, mask)) {
1045e69954b9Spbrook res |= (1 << i);
1046e69954b9Spbrook }
1047e69954b9Spbrook }
1048e69954b9Spbrook } else if (offset < 0x400) {
10493bb0b038SLuc Michel /* Interrupt Set/Clear Active. */
10503bb0b038SLuc Michel if (offset < 0x380) {
10513bb0b038SLuc Michel irq = (offset - 0x300) * 8;
10523bb0b038SLuc Michel } else if (s->revision == 2) {
10533bb0b038SLuc Michel irq = (offset - 0x380) * 8;
10543bb0b038SLuc Michel } else {
10553bb0b038SLuc Michel goto bad_reg;
10563bb0b038SLuc Michel }
10573bb0b038SLuc Michel
1058a32134aaSMark Langsdorf if (irq >= s->num_irq)
1059e69954b9Spbrook goto bad_reg;
1060e69954b9Spbrook res = 0;
106169253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
1062e69954b9Spbrook for (i = 0; i < 8; i++) {
1063fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
106467ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1065fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1066fea8a08eSJens Wiklander }
1067fea8a08eSJens Wiklander
106867ce697aSLuc Michel if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
1069e69954b9Spbrook res |= (1 << i);
1070e69954b9Spbrook }
1071e69954b9Spbrook }
1072e69954b9Spbrook } else if (offset < 0x800) {
1073e69954b9Spbrook /* Interrupt Priority. */
1074b6e6c651SPeter Maydell irq = (offset - 0x400);
1075a32134aaSMark Langsdorf if (irq >= s->num_irq)
1076e69954b9Spbrook goto bad_reg;
107767ce697aSLuc Michel res = gic_dist_get_priority(s, cpu, irq, attrs);
1078e69954b9Spbrook } else if (offset < 0xc00) {
1079e69954b9Spbrook /* Interrupt CPU Target. */
10806b9680bbSPeter Maydell if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
10816b9680bbSPeter Maydell /* For uniprocessor GICs these RAZ/WI */
10826b9680bbSPeter Maydell res = 0;
10836b9680bbSPeter Maydell } else {
1084b6e6c651SPeter Maydell irq = (offset - 0x800);
10856b9680bbSPeter Maydell if (irq >= s->num_irq) {
1086e69954b9Spbrook goto bad_reg;
10876b9680bbSPeter Maydell }
10887995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) {
10897995206dSPeter Maydell res = 0;
10907995206dSPeter Maydell } else if (irq < GIC_INTERNAL) {
10919ee6e8bbSpbrook res = cm;
10929ee6e8bbSpbrook } else {
109367ce697aSLuc Michel res = GIC_DIST_TARGET(irq);
10949ee6e8bbSpbrook }
10956b9680bbSPeter Maydell }
1096e69954b9Spbrook } else if (offset < 0xf00) {
1097e69954b9Spbrook /* Interrupt Configuration. */
1098b6e6c651SPeter Maydell irq = (offset - 0xc00) * 4;
1099a32134aaSMark Langsdorf if (irq >= s->num_irq)
1100e69954b9Spbrook goto bad_reg;
1101e69954b9Spbrook res = 0;
1102e69954b9Spbrook for (i = 0; i < 4; i++) {
1103fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
110467ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1105fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1106fea8a08eSJens Wiklander }
1107fea8a08eSJens Wiklander
110867ce697aSLuc Michel if (GIC_DIST_TEST_MODEL(irq + i)) {
1109e69954b9Spbrook res |= (1 << (i * 2));
111067ce697aSLuc Michel }
111167ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1112e69954b9Spbrook res |= (2 << (i * 2));
1113e69954b9Spbrook }
111467ce697aSLuc Michel }
111540d22500SChristoffer Dall } else if (offset < 0xf10) {
111640d22500SChristoffer Dall goto bad_reg;
111740d22500SChristoffer Dall } else if (offset < 0xf30) {
11187c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) {
111940d22500SChristoffer Dall goto bad_reg;
112040d22500SChristoffer Dall }
112140d22500SChristoffer Dall
112240d22500SChristoffer Dall if (offset < 0xf20) {
112340d22500SChristoffer Dall /* GICD_CPENDSGIRn */
112440d22500SChristoffer Dall irq = (offset - 0xf10);
112540d22500SChristoffer Dall } else {
112640d22500SChristoffer Dall irq = (offset - 0xf20);
112740d22500SChristoffer Dall /* GICD_SPENDSGIRn */
112840d22500SChristoffer Dall }
112940d22500SChristoffer Dall
1130fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
113167ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1132fea8a08eSJens Wiklander res = 0; /* Ignore Non-secure access of Group0 IRQ */
1133fea8a08eSJens Wiklander } else {
113440d22500SChristoffer Dall res = s->sgi_pending[irq][cpu];
1135fea8a08eSJens Wiklander }
11363355c360SAlistair Francis } else if (offset < 0xfd0) {
1137e69954b9Spbrook goto bad_reg;
11383355c360SAlistair Francis } else if (offset < 0x1000) {
1139e69954b9Spbrook if (offset & 3) {
1140e69954b9Spbrook res = 0;
1141e69954b9Spbrook } else {
11423355c360SAlistair Francis switch (s->revision) {
11433355c360SAlistair Francis case REV_11MPCORE:
11443355c360SAlistair Francis res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
11453355c360SAlistair Francis break;
11463355c360SAlistair Francis case 1:
11473355c360SAlistair Francis res = gic_id_gicv1[(offset - 0xfd0) >> 2];
11483355c360SAlistair Francis break;
11493355c360SAlistair Francis case 2:
11503355c360SAlistair Francis res = gic_id_gicv2[(offset - 0xfd0) >> 2];
11513355c360SAlistair Francis break;
11523355c360SAlistair Francis default:
11533355c360SAlistair Francis res = 0;
1154e69954b9Spbrook }
1155e69954b9Spbrook }
11563355c360SAlistair Francis } else {
11573355c360SAlistair Francis g_assert_not_reached();
11583355c360SAlistair Francis }
1159e69954b9Spbrook return res;
1160e69954b9Spbrook bad_reg:
11618c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR,
11628c8dc39fSPeter Maydell "gic_dist_readb: Bad offset %x\n", (int)offset);
1163e69954b9Spbrook return 0;
1164e69954b9Spbrook }
1165e69954b9Spbrook
gic_dist_read(void * opaque,hwaddr offset,uint64_t * data,unsigned size,MemTxAttrs attrs)1166a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
1167a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs)
1168e69954b9Spbrook {
1169a9d85353SPeter Maydell switch (size) {
1170a9d85353SPeter Maydell case 1:
1171a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs);
1172067a2b9cSLuc Michel break;
1173a9d85353SPeter Maydell case 2:
1174a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs);
1175a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1176067a2b9cSLuc Michel break;
1177a9d85353SPeter Maydell case 4:
1178a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs);
1179a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1180a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
1181a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
1182067a2b9cSLuc Michel break;
1183a9d85353SPeter Maydell default:
1184a9d85353SPeter Maydell return MEMTX_ERROR;
1185e69954b9Spbrook }
1186067a2b9cSLuc Michel
1187067a2b9cSLuc Michel trace_gic_dist_read(offset, size, *data);
1188067a2b9cSLuc Michel return MEMTX_OK;
1189e69954b9Spbrook }
1190e69954b9Spbrook
gic_dist_writeb(void * opaque,hwaddr offset,uint32_t value,MemTxAttrs attrs)1191a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset,
1192a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs)
1193e69954b9Spbrook {
1194fae15286SPeter Maydell GICState *s = (GICState *)opaque;
1195e69954b9Spbrook int irq;
1196e69954b9Spbrook int i;
11979ee6e8bbSpbrook int cpu;
1198e69954b9Spbrook
1199926c4affSPeter Maydell cpu = gic_get_current_cpu(s);
1200e69954b9Spbrook if (offset < 0x100) {
1201e69954b9Spbrook if (offset == 0) {
1202679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) {
1203679aa175SFabian Aggeler /* NS version is just an alias of the S version's bit 1 */
1204679aa175SFabian Aggeler s->ctlr = deposit32(s->ctlr, 1, 1, value);
1205679aa175SFabian Aggeler } else if (gic_has_groups(s)) {
1206679aa175SFabian Aggeler s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
1207679aa175SFabian Aggeler } else {
1208679aa175SFabian Aggeler s->ctlr = value & GICD_CTLR_EN_GRP0;
1209679aa175SFabian Aggeler }
1210679aa175SFabian Aggeler DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
1211679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
1212679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
1213e69954b9Spbrook } else if (offset < 4) {
1214e69954b9Spbrook /* ignored. */
1215b79f2265SRob Herring } else if (offset >= 0x80) {
1216c27a5ba9SFabian Aggeler /* Interrupt Group Registers: RAZ/WI for NS access to secure
1217c27a5ba9SFabian Aggeler * GIC, or for GICs without groups.
1218c27a5ba9SFabian Aggeler */
1219c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
1220c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */
1221b6e6c651SPeter Maydell irq = (offset - 0x80) * 8;
1222c27a5ba9SFabian Aggeler if (irq >= s->num_irq) {
1223c27a5ba9SFabian Aggeler goto bad_reg;
1224c27a5ba9SFabian Aggeler }
1225c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) {
1226c27a5ba9SFabian Aggeler /* Group bits are banked for private interrupts */
1227c27a5ba9SFabian Aggeler int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1228c27a5ba9SFabian Aggeler if (value & (1 << i)) {
1229c27a5ba9SFabian Aggeler /* Group1 (Non-secure) */
123067ce697aSLuc Michel GIC_DIST_SET_GROUP(irq + i, cm);
1231c27a5ba9SFabian Aggeler } else {
1232c27a5ba9SFabian Aggeler /* Group0 (Secure) */
123367ce697aSLuc Michel GIC_DIST_CLEAR_GROUP(irq + i, cm);
1234c27a5ba9SFabian Aggeler }
1235c27a5ba9SFabian Aggeler }
1236c27a5ba9SFabian Aggeler }
1237e69954b9Spbrook } else {
1238e69954b9Spbrook goto bad_reg;
1239e69954b9Spbrook }
1240e69954b9Spbrook } else if (offset < 0x180) {
1241e69954b9Spbrook /* Interrupt Set Enable. */
1242b6e6c651SPeter Maydell irq = (offset - 0x100) * 8;
1243a32134aaSMark Langsdorf if (irq >= s->num_irq)
1244e69954b9Spbrook goto bad_reg;
124541ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) {
12469ee6e8bbSpbrook value = 0xff;
124741ab7b55SChristoffer Dall }
124841ab7b55SChristoffer Dall
1249e69954b9Spbrook for (i = 0; i < 8; i++) {
1250e69954b9Spbrook if (value & (1 << i)) {
1251f47b48fbSDaniel Sangorrin int mask =
125267ce697aSLuc Michel (irq < GIC_INTERNAL) ? (1 << cpu)
125367ce697aSLuc Michel : GIC_DIST_TARGET(irq + i);
125469253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
125541bf234dSRabin Vincent
1256fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
125767ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1258fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1259fea8a08eSJens Wiklander }
1260fea8a08eSJens Wiklander
126167ce697aSLuc Michel if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1262e69954b9Spbrook DPRINTF("Enabled IRQ %d\n", irq + i);
12632531088fSHollis Blanchard trace_gic_enable_irq(irq + i);
126441bf234dSRabin Vincent }
126567ce697aSLuc Michel GIC_DIST_SET_ENABLED(irq + i, cm);
1266110684c9SJan Klötzke /*
1267110684c9SJan Klötzke * If a raised level triggered IRQ enabled then mark
1268110684c9SJan Klötzke * it as pending on 11MPCore. For other GIC revisions we
1269110684c9SJan Klötzke * handle the "level triggered and line asserted" check
1270110684c9SJan Klötzke * at the other end in gic_test_pending().
1271110684c9SJan Klötzke */
1272110684c9SJan Klötzke if (s->revision == REV_11MPCORE
1273110684c9SJan Klötzke && GIC_DIST_TEST_LEVEL(irq + i, mask)
127467ce697aSLuc Michel && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
12759ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq + i, mask);
127667ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, mask);
12779ee6e8bbSpbrook }
1278e69954b9Spbrook }
1279e69954b9Spbrook }
1280e69954b9Spbrook } else if (offset < 0x200) {
1281e69954b9Spbrook /* Interrupt Clear Enable. */
1282b6e6c651SPeter Maydell irq = (offset - 0x180) * 8;
1283a32134aaSMark Langsdorf if (irq >= s->num_irq)
1284e69954b9Spbrook goto bad_reg;
128541ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) {
12869ee6e8bbSpbrook value = 0;
128741ab7b55SChristoffer Dall }
128841ab7b55SChristoffer Dall
1289e69954b9Spbrook for (i = 0; i < 8; i++) {
1290e69954b9Spbrook if (value & (1 << i)) {
129169253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
129241bf234dSRabin Vincent
1293fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
129467ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1295fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1296fea8a08eSJens Wiklander }
1297fea8a08eSJens Wiklander
129867ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1299e69954b9Spbrook DPRINTF("Disabled IRQ %d\n", irq + i);
13002531088fSHollis Blanchard trace_gic_disable_irq(irq + i);
130141bf234dSRabin Vincent }
130267ce697aSLuc Michel GIC_DIST_CLEAR_ENABLED(irq + i, cm);
1303e69954b9Spbrook }
1304e69954b9Spbrook }
1305e69954b9Spbrook } else if (offset < 0x280) {
1306e69954b9Spbrook /* Interrupt Set Pending. */
1307b6e6c651SPeter Maydell irq = (offset - 0x200) * 8;
1308a32134aaSMark Langsdorf if (irq >= s->num_irq)
1309e69954b9Spbrook goto bad_reg;
131041ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) {
13115b0adce1SChristoffer Dall value = 0;
131241ab7b55SChristoffer Dall }
13139ee6e8bbSpbrook
1314e69954b9Spbrook for (i = 0; i < 8; i++) {
1315e69954b9Spbrook if (value & (1 << i)) {
1316f5e328feSSebastian Huber int mask = (irq < GIC_INTERNAL) ? (1 << cpu)
1317f5e328feSSebastian Huber : GIC_DIST_TARGET(irq + i);
1318f5e328feSSebastian Huber
1319fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
132067ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1321fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1322fea8a08eSJens Wiklander }
1323fea8a08eSJens Wiklander
1324f5e328feSSebastian Huber GIC_DIST_SET_PENDING(irq + i, mask);
1325e69954b9Spbrook }
1326e69954b9Spbrook }
1327e69954b9Spbrook } else if (offset < 0x300) {
1328e69954b9Spbrook /* Interrupt Clear Pending. */
1329b6e6c651SPeter Maydell irq = (offset - 0x280) * 8;
1330a32134aaSMark Langsdorf if (irq >= s->num_irq)
1331e69954b9Spbrook goto bad_reg;
13325b0adce1SChristoffer Dall if (irq < GIC_NR_SGIS) {
13335b0adce1SChristoffer Dall value = 0;
13345b0adce1SChristoffer Dall }
13355b0adce1SChristoffer Dall
1336e69954b9Spbrook for (i = 0; i < 8; i++) {
1337fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
133867ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1339fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1340fea8a08eSJens Wiklander }
1341fea8a08eSJens Wiklander
13429ee6e8bbSpbrook /* ??? This currently clears the pending bit for all CPUs, even
13439ee6e8bbSpbrook for per-CPU interrupts. It's unclear whether this is the
1344673d8215SMichael Tokarev correct behavior. */
1345e69954b9Spbrook if (value & (1 << i)) {
134667ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
1347e69954b9Spbrook }
1348e69954b9Spbrook }
13493bb0b038SLuc Michel } else if (offset < 0x380) {
13503bb0b038SLuc Michel /* Interrupt Set Active. */
13513bb0b038SLuc Michel if (s->revision != 2) {
1352e69954b9Spbrook goto bad_reg;
13533bb0b038SLuc Michel }
13543bb0b038SLuc Michel
1355b6e6c651SPeter Maydell irq = (offset - 0x300) * 8;
13563bb0b038SLuc Michel if (irq >= s->num_irq) {
13573bb0b038SLuc Michel goto bad_reg;
13583bb0b038SLuc Michel }
13593bb0b038SLuc Michel
13603bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */
13613bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13623bb0b038SLuc Michel
13633bb0b038SLuc Michel for (i = 0; i < 8; i++) {
13643bb0b038SLuc Michel if (s->security_extn && !attrs.secure &&
13653bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13663bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */
13673bb0b038SLuc Michel }
13683bb0b038SLuc Michel
13693bb0b038SLuc Michel if (value & (1 << i)) {
13703bb0b038SLuc Michel GIC_DIST_SET_ACTIVE(irq + i, cm);
13713bb0b038SLuc Michel }
13723bb0b038SLuc Michel }
13733bb0b038SLuc Michel } else if (offset < 0x400) {
13743bb0b038SLuc Michel /* Interrupt Clear Active. */
13753bb0b038SLuc Michel if (s->revision != 2) {
13763bb0b038SLuc Michel goto bad_reg;
13773bb0b038SLuc Michel }
13783bb0b038SLuc Michel
1379b6e6c651SPeter Maydell irq = (offset - 0x380) * 8;
13803bb0b038SLuc Michel if (irq >= s->num_irq) {
13813bb0b038SLuc Michel goto bad_reg;
13823bb0b038SLuc Michel }
13833bb0b038SLuc Michel
13843bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */
13853bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
13863bb0b038SLuc Michel
13873bb0b038SLuc Michel for (i = 0; i < 8; i++) {
13883bb0b038SLuc Michel if (s->security_extn && !attrs.secure &&
13893bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
13903bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */
13913bb0b038SLuc Michel }
13923bb0b038SLuc Michel
13933bb0b038SLuc Michel if (value & (1 << i)) {
13943bb0b038SLuc Michel GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
13953bb0b038SLuc Michel }
13963bb0b038SLuc Michel }
1397e69954b9Spbrook } else if (offset < 0x800) {
1398e69954b9Spbrook /* Interrupt Priority. */
1399b6e6c651SPeter Maydell irq = (offset - 0x400);
1400a32134aaSMark Langsdorf if (irq >= s->num_irq)
1401e69954b9Spbrook goto bad_reg;
140267ce697aSLuc Michel gic_dist_set_priority(s, cpu, irq, value, attrs);
1403e69954b9Spbrook } else if (offset < 0xc00) {
14046b9680bbSPeter Maydell /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
14056b9680bbSPeter Maydell * annoying exception of the 11MPCore's GIC.
14066b9680bbSPeter Maydell */
14076b9680bbSPeter Maydell if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
1408b6e6c651SPeter Maydell irq = (offset - 0x800);
14096b9680bbSPeter Maydell if (irq >= s->num_irq) {
1410e69954b9Spbrook goto bad_reg;
14116b9680bbSPeter Maydell }
14127995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) {
14139ee6e8bbSpbrook value = 0;
14146b9680bbSPeter Maydell } else if (irq < GIC_INTERNAL) {
14159ee6e8bbSpbrook value = ALL_CPU_MASK;
14166b9680bbSPeter Maydell }
14179ee6e8bbSpbrook s->irq_target[irq] = value & ALL_CPU_MASK;
1418d9aff83aSSebastian Huber if (irq >= GIC_INTERNAL && s->irq_state[irq].pending) {
1419d9aff83aSSebastian Huber /*
1420d9aff83aSSebastian Huber * Changing the target of an interrupt that is currently
1421d9aff83aSSebastian Huber * pending updates the set of CPUs it is pending on.
1422d9aff83aSSebastian Huber */
1423d9aff83aSSebastian Huber s->irq_state[irq].pending = value & ALL_CPU_MASK;
1424d9aff83aSSebastian Huber }
14256b9680bbSPeter Maydell }
1426e69954b9Spbrook } else if (offset < 0xf00) {
1427e69954b9Spbrook /* Interrupt Configuration. */
1428b6e6c651SPeter Maydell irq = (offset - 0xc00) * 4;
1429a32134aaSMark Langsdorf if (irq >= s->num_irq)
1430e69954b9Spbrook goto bad_reg;
1431de7a900fSAdam Lackorzynski if (irq < GIC_NR_SGIS)
14329ee6e8bbSpbrook value |= 0xaa;
1433e69954b9Spbrook for (i = 0; i < 4; i++) {
1434fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure &&
143567ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1436fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */
1437fea8a08eSJens Wiklander }
1438fea8a08eSJens Wiklander
14397c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) {
1440e69954b9Spbrook if (value & (1 << (i * 2))) {
144167ce697aSLuc Michel GIC_DIST_SET_MODEL(irq + i);
1442e69954b9Spbrook } else {
144367ce697aSLuc Michel GIC_DIST_CLEAR_MODEL(irq + i);
1444e69954b9Spbrook }
144524b790dfSAdam Lackorzynski }
1446e69954b9Spbrook if (value & (2 << (i * 2))) {
144767ce697aSLuc Michel GIC_DIST_SET_EDGE_TRIGGER(irq + i);
1448e69954b9Spbrook } else {
144967ce697aSLuc Michel GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
1450e69954b9Spbrook }
1451e69954b9Spbrook }
145240d22500SChristoffer Dall } else if (offset < 0xf10) {
14539ee6e8bbSpbrook /* 0xf00 is only handled for 32-bit writes. */
1454e69954b9Spbrook goto bad_reg;
145540d22500SChristoffer Dall } else if (offset < 0xf20) {
145640d22500SChristoffer Dall /* GICD_CPENDSGIRn */
14577c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) {
145840d22500SChristoffer Dall goto bad_reg;
145940d22500SChristoffer Dall }
146040d22500SChristoffer Dall irq = (offset - 0xf10);
146140d22500SChristoffer Dall
1462fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure ||
146367ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
146440d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~value;
146540d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) {
146667ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
146740d22500SChristoffer Dall }
1468fea8a08eSJens Wiklander }
146940d22500SChristoffer Dall } else if (offset < 0xf30) {
147040d22500SChristoffer Dall /* GICD_SPENDSGIRn */
14717c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) {
147240d22500SChristoffer Dall goto bad_reg;
147340d22500SChristoffer Dall }
147440d22500SChristoffer Dall irq = (offset - 0xf20);
147540d22500SChristoffer Dall
1476fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure ||
147767ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
147867ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, 1 << cpu);
147940d22500SChristoffer Dall s->sgi_pending[irq][cpu] |= value;
1480fea8a08eSJens Wiklander }
148140d22500SChristoffer Dall } else {
148240d22500SChristoffer Dall goto bad_reg;
1483e69954b9Spbrook }
1484e69954b9Spbrook gic_update(s);
1485e69954b9Spbrook return;
1486e69954b9Spbrook bad_reg:
14878c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR,
14888c8dc39fSPeter Maydell "gic_dist_writeb: Bad offset %x\n", (int)offset);
1489e69954b9Spbrook }
1490e69954b9Spbrook
gic_dist_writew(void * opaque,hwaddr offset,uint32_t value,MemTxAttrs attrs)1491a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset,
1492a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs)
1493e69954b9Spbrook {
1494a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, value & 0xff, attrs);
1495a9d85353SPeter Maydell gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
1496e69954b9Spbrook }
1497e69954b9Spbrook
gic_dist_writel(void * opaque,hwaddr offset,uint32_t value,MemTxAttrs attrs)1498a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset,
1499a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs)
1500e69954b9Spbrook {
1501fae15286SPeter Maydell GICState *s = (GICState *)opaque;
15028da3ff18Spbrook if (offset == 0xf00) {
15039ee6e8bbSpbrook int cpu;
15049ee6e8bbSpbrook int irq;
15059ee6e8bbSpbrook int mask;
150640d22500SChristoffer Dall int target_cpu;
15079ee6e8bbSpbrook
1508926c4affSPeter Maydell cpu = gic_get_current_cpu(s);
1509edfe2eb4SPhilippe Mathieu-Daudé irq = value & 0xf;
15109ee6e8bbSpbrook switch ((value >> 24) & 3) {
15119ee6e8bbSpbrook case 0:
15129ee6e8bbSpbrook mask = (value >> 16) & ALL_CPU_MASK;
15139ee6e8bbSpbrook break;
15149ee6e8bbSpbrook case 1:
1515fa250144SAdam Lackorzynski mask = ALL_CPU_MASK ^ (1 << cpu);
15169ee6e8bbSpbrook break;
15179ee6e8bbSpbrook case 2:
1518fa250144SAdam Lackorzynski mask = 1 << cpu;
15199ee6e8bbSpbrook break;
15209ee6e8bbSpbrook default:
15219ee6e8bbSpbrook DPRINTF("Bad Soft Int target filter\n");
15229ee6e8bbSpbrook mask = ALL_CPU_MASK;
15239ee6e8bbSpbrook break;
15249ee6e8bbSpbrook }
152567ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, mask);
152640d22500SChristoffer Dall target_cpu = ctz32(mask);
152740d22500SChristoffer Dall while (target_cpu < GIC_NCPU) {
152840d22500SChristoffer Dall s->sgi_pending[irq][target_cpu] |= (1 << cpu);
152940d22500SChristoffer Dall mask &= ~(1 << target_cpu);
153040d22500SChristoffer Dall target_cpu = ctz32(mask);
153140d22500SChristoffer Dall }
15329ee6e8bbSpbrook gic_update(s);
15339ee6e8bbSpbrook return;
15349ee6e8bbSpbrook }
1535a9d85353SPeter Maydell gic_dist_writew(opaque, offset, value & 0xffff, attrs);
1536a9d85353SPeter Maydell gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
1537a9d85353SPeter Maydell }
1538a9d85353SPeter Maydell
gic_dist_write(void * opaque,hwaddr offset,uint64_t data,unsigned size,MemTxAttrs attrs)1539a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
1540a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs)
1541a9d85353SPeter Maydell {
1542067a2b9cSLuc Michel trace_gic_dist_write(offset, size, data);
1543067a2b9cSLuc Michel
1544a9d85353SPeter Maydell switch (size) {
1545a9d85353SPeter Maydell case 1:
1546a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, data, attrs);
1547a9d85353SPeter Maydell return MEMTX_OK;
1548a9d85353SPeter Maydell case 2:
1549a9d85353SPeter Maydell gic_dist_writew(opaque, offset, data, attrs);
1550a9d85353SPeter Maydell return MEMTX_OK;
1551a9d85353SPeter Maydell case 4:
1552a9d85353SPeter Maydell gic_dist_writel(opaque, offset, data, attrs);
1553a9d85353SPeter Maydell return MEMTX_OK;
1554a9d85353SPeter Maydell default:
1555a9d85353SPeter Maydell return MEMTX_ERROR;
1556a9d85353SPeter Maydell }
1557e69954b9Spbrook }
1558e69954b9Spbrook
gic_apr_ns_view(GICState * s,int cpu,int regno)155951fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
156051fd06e0SPeter Maydell {
156151fd06e0SPeter Maydell /* Return the Nonsecure view of GICC_APR<regno>. This is the
156251fd06e0SPeter Maydell * second half of GICC_NSAPR.
156351fd06e0SPeter Maydell */
156451fd06e0SPeter Maydell switch (GIC_MIN_BPR) {
156551fd06e0SPeter Maydell case 0:
156651fd06e0SPeter Maydell if (regno < 2) {
156751fd06e0SPeter Maydell return s->nsapr[regno + 2][cpu];
156851fd06e0SPeter Maydell }
156951fd06e0SPeter Maydell break;
157051fd06e0SPeter Maydell case 1:
157151fd06e0SPeter Maydell if (regno == 0) {
157251fd06e0SPeter Maydell return s->nsapr[regno + 1][cpu];
157351fd06e0SPeter Maydell }
157451fd06e0SPeter Maydell break;
157551fd06e0SPeter Maydell case 2:
157651fd06e0SPeter Maydell if (regno == 0) {
157751fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 16, 16);
157851fd06e0SPeter Maydell }
157951fd06e0SPeter Maydell break;
158051fd06e0SPeter Maydell case 3:
158151fd06e0SPeter Maydell if (regno == 0) {
158251fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 8, 8);
158351fd06e0SPeter Maydell }
158451fd06e0SPeter Maydell break;
158551fd06e0SPeter Maydell default:
158651fd06e0SPeter Maydell g_assert_not_reached();
158751fd06e0SPeter Maydell }
158851fd06e0SPeter Maydell return 0;
158951fd06e0SPeter Maydell }
159051fd06e0SPeter Maydell
gic_apr_write_ns_view(GICState * s,int cpu,int regno,uint32_t value)159151fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
159251fd06e0SPeter Maydell uint32_t value)
159351fd06e0SPeter Maydell {
159451fd06e0SPeter Maydell /* Write the Nonsecure view of GICC_APR<regno>. */
159551fd06e0SPeter Maydell switch (GIC_MIN_BPR) {
159651fd06e0SPeter Maydell case 0:
159751fd06e0SPeter Maydell if (regno < 2) {
159851fd06e0SPeter Maydell s->nsapr[regno + 2][cpu] = value;
159951fd06e0SPeter Maydell }
160051fd06e0SPeter Maydell break;
160151fd06e0SPeter Maydell case 1:
160251fd06e0SPeter Maydell if (regno == 0) {
160351fd06e0SPeter Maydell s->nsapr[regno + 1][cpu] = value;
160451fd06e0SPeter Maydell }
160551fd06e0SPeter Maydell break;
160651fd06e0SPeter Maydell case 2:
160751fd06e0SPeter Maydell if (regno == 0) {
160851fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
160951fd06e0SPeter Maydell }
161051fd06e0SPeter Maydell break;
161151fd06e0SPeter Maydell case 3:
161251fd06e0SPeter Maydell if (regno == 0) {
161351fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
161451fd06e0SPeter Maydell }
161551fd06e0SPeter Maydell break;
161651fd06e0SPeter Maydell default:
161751fd06e0SPeter Maydell g_assert_not_reached();
161851fd06e0SPeter Maydell }
161951fd06e0SPeter Maydell }
162051fd06e0SPeter Maydell
gic_cpu_read(GICState * s,int cpu,int offset,uint64_t * data,MemTxAttrs attrs)1621a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1622a9d85353SPeter Maydell uint64_t *data, MemTxAttrs attrs)
1623e69954b9Spbrook {
1624e69954b9Spbrook switch (offset) {
1625e69954b9Spbrook case 0x00: /* Control */
162632951860SFabian Aggeler *data = gic_get_cpu_control(s, cpu, attrs);
1627a9d85353SPeter Maydell break;
1628e69954b9Spbrook case 0x04: /* Priority mask */
162981508470SFabian Aggeler *data = gic_get_priority_mask(s, cpu, attrs);
1630a9d85353SPeter Maydell break;
1631e69954b9Spbrook case 0x08: /* Binary Point */
16323dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
1633421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1634421a3c22SLuc MICHEL /* NS view of BPR when CBPR is 1 */
1635421a3c22SLuc MICHEL *data = MIN(s->bpr[cpu] + 1, 7);
1636421a3c22SLuc MICHEL } else {
1637822e9cc3SFabian Aggeler /* BPR is banked. Non-secure copy stored in ABPR. */
1638822e9cc3SFabian Aggeler *data = s->abpr[cpu];
1639421a3c22SLuc MICHEL }
1640822e9cc3SFabian Aggeler } else {
1641a9d85353SPeter Maydell *data = s->bpr[cpu];
1642822e9cc3SFabian Aggeler }
1643a9d85353SPeter Maydell break;
1644e69954b9Spbrook case 0x0c: /* Acknowledge */
1645c5619bf9SFabian Aggeler *data = gic_acknowledge_irq(s, cpu, attrs);
1646a9d85353SPeter Maydell break;
164766a0a2cbSDong Xu Wang case 0x14: /* Running Priority */
164808efa9f2SFabian Aggeler *data = gic_get_running_priority(s, cpu, attrs);
1649a9d85353SPeter Maydell break;
1650e69954b9Spbrook case 0x18: /* Highest Pending Interrupt */
16517c0fa108SFabian Aggeler *data = gic_get_current_pending_irq(s, cpu, attrs);
1652a9d85353SPeter Maydell break;
1653aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */
1654822e9cc3SFabian Aggeler /* GIC v2, no security: ABPR
1655822e9cc3SFabian Aggeler * GIC v1, no security: not implemented (RAZ/WI)
1656822e9cc3SFabian Aggeler * With security extensions, secure access: ABPR (alias of NS BPR)
1657822e9cc3SFabian Aggeler * With security extensions, nonsecure access: RAZ/WI
1658822e9cc3SFabian Aggeler */
16593dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1660822e9cc3SFabian Aggeler *data = 0;
1661822e9cc3SFabian Aggeler } else {
1662a9d85353SPeter Maydell *data = s->abpr[cpu];
1663822e9cc3SFabian Aggeler }
1664a9d85353SPeter Maydell break;
1665a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc:
166651fd06e0SPeter Maydell {
166751fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4;
16687eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
166951fd06e0SPeter Maydell
16707eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) {
167151fd06e0SPeter Maydell *data = 0;
16727eb079ecSLuc Michel } else if (gic_is_vcpu(cpu)) {
16737eb079ecSLuc Michel *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
16743dd0471bSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) {
167551fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1676daafa78bSAndrey Shumilin *data = gic_apr_ns_view(s, cpu, regno);
167751fd06e0SPeter Maydell } else {
167851fd06e0SPeter Maydell *data = s->apr[regno][cpu];
167951fd06e0SPeter Maydell }
1680a9d85353SPeter Maydell break;
168151fd06e0SPeter Maydell }
168251fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec:
168351fd06e0SPeter Maydell {
168451fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4;
168551fd06e0SPeter Maydell
168651fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
16877eb079ecSLuc Michel gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
168851fd06e0SPeter Maydell *data = 0;
168951fd06e0SPeter Maydell } else {
169051fd06e0SPeter Maydell *data = s->nsapr[regno][cpu];
169151fd06e0SPeter Maydell }
169251fd06e0SPeter Maydell break;
169351fd06e0SPeter Maydell }
1694a66a2458SPetr Pavlu case 0xfc:
1695a66a2458SPetr Pavlu if (s->revision == REV_11MPCORE) {
1696a66a2458SPetr Pavlu /* Reserved on 11MPCore */
1697a66a2458SPetr Pavlu *data = 0;
1698a66a2458SPetr Pavlu } else {
1699a66a2458SPetr Pavlu /* GICv1 or v2; Arm implementation */
1700a66a2458SPetr Pavlu *data = (s->revision << 16) | 0x43b;
1701a66a2458SPetr Pavlu }
1702a66a2458SPetr Pavlu break;
1703e69954b9Spbrook default:
17048c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR,
17058c8dc39fSPeter Maydell "gic_cpu_read: Bad offset %x\n", (int)offset);
17060cf09852SPeter Maydell *data = 0;
17070cf09852SPeter Maydell break;
1708e69954b9Spbrook }
1709067a2b9cSLuc Michel
1710067a2b9cSLuc Michel trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1711067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, *data);
1712a9d85353SPeter Maydell return MEMTX_OK;
1713e69954b9Spbrook }
1714e69954b9Spbrook
gic_cpu_write(GICState * s,int cpu,int offset,uint32_t value,MemTxAttrs attrs)1715a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1716a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs)
1717e69954b9Spbrook {
1718067a2b9cSLuc Michel trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1719067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, value);
1720067a2b9cSLuc Michel
1721e69954b9Spbrook switch (offset) {
1722e69954b9Spbrook case 0x00: /* Control */
172332951860SFabian Aggeler gic_set_cpu_control(s, cpu, value, attrs);
1724e69954b9Spbrook break;
1725e69954b9Spbrook case 0x04: /* Priority mask */
172681508470SFabian Aggeler gic_set_priority_mask(s, cpu, value, attrs);
1727e69954b9Spbrook break;
1728e69954b9Spbrook case 0x08: /* Binary Point */
17293dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) {
1730421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1731421a3c22SLuc MICHEL /* WI when CBPR is 1 */
1732421a3c22SLuc MICHEL return MEMTX_OK;
1733421a3c22SLuc MICHEL } else {
1734822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1735421a3c22SLuc MICHEL }
1736822e9cc3SFabian Aggeler } else {
17377eb079ecSLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
17387eb079ecSLuc Michel s->bpr[cpu] = MAX(value & 0x7, min_bpr);
1739822e9cc3SFabian Aggeler }
1740e69954b9Spbrook break;
1741e69954b9Spbrook case 0x10: /* End Of Interrupt */
1742f9c6a7f1SFabian Aggeler gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1743a9d85353SPeter Maydell return MEMTX_OK;
1744aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */
17453dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1746822e9cc3SFabian Aggeler /* unimplemented, or NS access: RAZ/WI */
1747822e9cc3SFabian Aggeler return MEMTX_OK;
1748822e9cc3SFabian Aggeler } else {
1749822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1750aa7d461aSChristoffer Dall }
1751aa7d461aSChristoffer Dall break;
1752a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc:
175351fd06e0SPeter Maydell {
175451fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4;
17557eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
175651fd06e0SPeter Maydell
17577eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) {
175851fd06e0SPeter Maydell return MEMTX_OK;
175951fd06e0SPeter Maydell }
17607eb079ecSLuc Michel if (gic_is_vcpu(cpu)) {
17617eb079ecSLuc Michel s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
17627eb079ecSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) {
176351fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1764daafa78bSAndrey Shumilin gic_apr_write_ns_view(s, cpu, regno, value);
176551fd06e0SPeter Maydell } else {
176651fd06e0SPeter Maydell s->apr[regno][cpu] = value;
176751fd06e0SPeter Maydell }
17685e66daecSPetr Pavlu s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
1769a9d477c4SChristoffer Dall break;
177051fd06e0SPeter Maydell }
177151fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec:
177251fd06e0SPeter Maydell {
177351fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4;
177451fd06e0SPeter Maydell
177551fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2) {
177651fd06e0SPeter Maydell return MEMTX_OK;
177751fd06e0SPeter Maydell }
17787eb079ecSLuc Michel if (gic_is_vcpu(cpu)) {
17797eb079ecSLuc Michel return MEMTX_OK;
17807eb079ecSLuc Michel }
17813dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
178251fd06e0SPeter Maydell return MEMTX_OK;
178351fd06e0SPeter Maydell }
178451fd06e0SPeter Maydell s->nsapr[regno][cpu] = value;
17855e66daecSPetr Pavlu s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
178651fd06e0SPeter Maydell break;
178751fd06e0SPeter Maydell }
1788a55c910eSPeter Maydell case 0x1000:
1789a55c910eSPeter Maydell /* GICC_DIR */
1790a55c910eSPeter Maydell gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
1791a55c910eSPeter Maydell break;
1792e69954b9Spbrook default:
17938c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR,
17948c8dc39fSPeter Maydell "gic_cpu_write: Bad offset %x\n", (int)offset);
17950cf09852SPeter Maydell return MEMTX_OK;
1796e69954b9Spbrook }
1797cbe1282bSLuc Michel
1798cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) {
1799cbe1282bSLuc Michel gic_update_virt(s);
1800cbe1282bSLuc Michel } else {
1801e69954b9Spbrook gic_update(s);
1802cbe1282bSLuc Michel }
1803cbe1282bSLuc Michel
1804a9d85353SPeter Maydell return MEMTX_OK;
1805e69954b9Spbrook }
1806e2c56465SPeter Maydell
1807e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */
gic_thiscpu_read(void * opaque,hwaddr addr,uint64_t * data,unsigned size,MemTxAttrs attrs)1808a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1809a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs)
1810e2c56465SPeter Maydell {
1811fae15286SPeter Maydell GICState *s = (GICState *)opaque;
1812a9d85353SPeter Maydell return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1813e2c56465SPeter Maydell }
1814e2c56465SPeter Maydell
gic_thiscpu_write(void * opaque,hwaddr addr,uint64_t value,unsigned size,MemTxAttrs attrs)1815a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1816a9d85353SPeter Maydell uint64_t value, unsigned size,
1817a9d85353SPeter Maydell MemTxAttrs attrs)
1818e2c56465SPeter Maydell {
1819fae15286SPeter Maydell GICState *s = (GICState *)opaque;
1820a9d85353SPeter Maydell return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1821e2c56465SPeter Maydell }
1822e2c56465SPeter Maydell
1823e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU.
1824fae15286SPeter Maydell * These just decode the opaque pointer into GICState* + cpu id.
1825e2c56465SPeter Maydell */
gic_do_cpu_read(void * opaque,hwaddr addr,uint64_t * data,unsigned size,MemTxAttrs attrs)1826a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1827a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs)
1828e2c56465SPeter Maydell {
1829fae15286SPeter Maydell GICState **backref = (GICState **)opaque;
1830fae15286SPeter Maydell GICState *s = *backref;
1831e2c56465SPeter Maydell int id = (backref - s->backref);
1832a9d85353SPeter Maydell return gic_cpu_read(s, id, addr, data, attrs);
1833e2c56465SPeter Maydell }
1834e2c56465SPeter Maydell
gic_do_cpu_write(void * opaque,hwaddr addr,uint64_t value,unsigned size,MemTxAttrs attrs)1835a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1836a9d85353SPeter Maydell uint64_t value, unsigned size,
1837a9d85353SPeter Maydell MemTxAttrs attrs)
1838e2c56465SPeter Maydell {
1839fae15286SPeter Maydell GICState **backref = (GICState **)opaque;
1840fae15286SPeter Maydell GICState *s = *backref;
1841e2c56465SPeter Maydell int id = (backref - s->backref);
1842a9d85353SPeter Maydell return gic_cpu_write(s, id, addr, value, attrs);
1843e2c56465SPeter Maydell }
1844e2c56465SPeter Maydell
gic_thisvcpu_read(void * opaque,hwaddr addr,uint64_t * data,unsigned size,MemTxAttrs attrs)18452c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
18462c679ac7SLuc Michel unsigned size, MemTxAttrs attrs)
18472c679ac7SLuc Michel {
18482c679ac7SLuc Michel GICState *s = (GICState *)opaque;
18492c679ac7SLuc Michel
18502c679ac7SLuc Michel return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
18512c679ac7SLuc Michel }
18522c679ac7SLuc Michel
gic_thisvcpu_write(void * opaque,hwaddr addr,uint64_t value,unsigned size,MemTxAttrs attrs)18532c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
18542c679ac7SLuc Michel uint64_t value, unsigned size,
18552c679ac7SLuc Michel MemTxAttrs attrs)
18562c679ac7SLuc Michel {
18572c679ac7SLuc Michel GICState *s = (GICState *)opaque;
18582c679ac7SLuc Michel
18592c679ac7SLuc Michel return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
18602c679ac7SLuc Michel }
18612c679ac7SLuc Michel
gic_compute_eisr(GICState * s,int cpu,int lr_start)1862527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
1863527d296fSLuc Michel {
1864527d296fSLuc Michel int lr_idx;
1865527d296fSLuc Michel uint32_t ret = 0;
1866527d296fSLuc Michel
1867527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1868527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu];
1869527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1,
1870527d296fSLuc Michel gic_lr_entry_is_eoi(*entry));
1871527d296fSLuc Michel }
1872527d296fSLuc Michel
1873527d296fSLuc Michel return ret;
1874527d296fSLuc Michel }
1875527d296fSLuc Michel
gic_compute_elrsr(GICState * s,int cpu,int lr_start)1876527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
1877527d296fSLuc Michel {
1878527d296fSLuc Michel int lr_idx;
1879527d296fSLuc Michel uint32_t ret = 0;
1880527d296fSLuc Michel
1881527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1882527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu];
1883527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1,
1884527d296fSLuc Michel gic_lr_entry_is_free(*entry));
1885527d296fSLuc Michel }
1886527d296fSLuc Michel
1887527d296fSLuc Michel return ret;
1888527d296fSLuc Michel }
1889527d296fSLuc Michel
gic_vmcr_write(GICState * s,uint32_t value,MemTxAttrs attrs)1890527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
1891527d296fSLuc Michel {
1892527d296fSLuc Michel int vcpu = gic_get_current_vcpu(s);
1893527d296fSLuc Michel uint32_t ctlr;
1894527d296fSLuc Michel uint32_t abpr;
1895527d296fSLuc Michel uint32_t bpr;
1896527d296fSLuc Michel uint32_t prio_mask;
1897527d296fSLuc Michel
1898527d296fSLuc Michel ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
1899527d296fSLuc Michel abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
1900527d296fSLuc Michel bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
1901527d296fSLuc Michel prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
1902527d296fSLuc Michel
1903527d296fSLuc Michel gic_set_cpu_control(s, vcpu, ctlr, attrs);
1904527d296fSLuc Michel s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
1905527d296fSLuc Michel s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
1906527d296fSLuc Michel gic_set_priority_mask(s, vcpu, prio_mask, attrs);
1907527d296fSLuc Michel }
1908527d296fSLuc Michel
gic_hyp_read(void * opaque,int cpu,hwaddr addr,uint64_t * data,MemTxAttrs attrs)1909527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
1910527d296fSLuc Michel uint64_t *data, MemTxAttrs attrs)
1911527d296fSLuc Michel {
1912527d296fSLuc Michel GICState *s = ARM_GIC(opaque);
1913527d296fSLuc Michel int vcpu = cpu + GIC_NCPU;
1914527d296fSLuc Michel
1915527d296fSLuc Michel switch (addr) {
1916527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */
1917527d296fSLuc Michel *data = s->h_hcr[cpu];
1918527d296fSLuc Michel break;
1919527d296fSLuc Michel
1920527d296fSLuc Michel case A_GICH_VTR: /* VGIC Type */
1921527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
1922527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PREbits,
1923527d296fSLuc Michel GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
1924527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
1925527d296fSLuc Michel (7 - GIC_VIRT_MIN_BPR) - 1);
1926527d296fSLuc Michel break;
1927527d296fSLuc Michel
1928527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */
1929527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
1930527d296fSLuc Michel extract32(s->cpu_ctlr[vcpu], 0, 10));
1931527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
1932527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
1933527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
1934527d296fSLuc Michel extract32(s->priority_mask[vcpu], 3, 5));
1935527d296fSLuc Michel break;
1936527d296fSLuc Michel
1937527d296fSLuc Michel case A_GICH_MISR: /* Maintenance Interrupt Status */
1938527d296fSLuc Michel *data = s->h_misr[cpu];
1939527d296fSLuc Michel break;
1940527d296fSLuc Michel
1941527d296fSLuc Michel case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */
1942527d296fSLuc Michel case A_GICH_EISR1:
1943527d296fSLuc Michel *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
1944527d296fSLuc Michel break;
1945527d296fSLuc Michel
1946527d296fSLuc Michel case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */
1947527d296fSLuc Michel case A_GICH_ELRSR1:
1948527d296fSLuc Michel *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
1949527d296fSLuc Michel break;
1950527d296fSLuc Michel
1951527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */
1952527d296fSLuc Michel *data = s->h_apr[cpu];
1953527d296fSLuc Michel break;
1954527d296fSLuc Michel
1955527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1956527d296fSLuc Michel {
1957527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4;
1958527d296fSLuc Michel
1959527d296fSLuc Michel if (lr_idx > s->num_lrs) {
1960527d296fSLuc Michel *data = 0;
1961527d296fSLuc Michel } else {
1962527d296fSLuc Michel *data = s->h_lr[lr_idx][cpu];
1963527d296fSLuc Michel }
1964527d296fSLuc Michel break;
1965527d296fSLuc Michel }
1966527d296fSLuc Michel
1967527d296fSLuc Michel default:
1968527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR,
1969527d296fSLuc Michel "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
1970527d296fSLuc Michel return MEMTX_OK;
1971527d296fSLuc Michel }
1972527d296fSLuc Michel
1973067a2b9cSLuc Michel trace_gic_hyp_read(addr, *data);
1974527d296fSLuc Michel return MEMTX_OK;
1975527d296fSLuc Michel }
1976527d296fSLuc Michel
gic_hyp_write(void * opaque,int cpu,hwaddr addr,uint64_t value,MemTxAttrs attrs)1977527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
1978527d296fSLuc Michel uint64_t value, MemTxAttrs attrs)
1979527d296fSLuc Michel {
1980527d296fSLuc Michel GICState *s = ARM_GIC(opaque);
1981527d296fSLuc Michel int vcpu = cpu + GIC_NCPU;
1982527d296fSLuc Michel
1983067a2b9cSLuc Michel trace_gic_hyp_write(addr, value);
1984067a2b9cSLuc Michel
1985527d296fSLuc Michel switch (addr) {
1986527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */
1987527d296fSLuc Michel s->h_hcr[cpu] = value & GICH_HCR_MASK;
1988527d296fSLuc Michel break;
1989527d296fSLuc Michel
1990527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */
1991527d296fSLuc Michel gic_vmcr_write(s, value, attrs);
1992527d296fSLuc Michel break;
1993527d296fSLuc Michel
1994527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */
1995527d296fSLuc Michel s->h_apr[cpu] = value;
1996527d296fSLuc Michel s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
1997527d296fSLuc Michel break;
1998527d296fSLuc Michel
1999527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
2000527d296fSLuc Michel {
2001527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4;
2002527d296fSLuc Michel
2003527d296fSLuc Michel if (lr_idx > s->num_lrs) {
2004527d296fSLuc Michel return MEMTX_OK;
2005527d296fSLuc Michel }
2006527d296fSLuc Michel
2007527d296fSLuc Michel s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
2008067a2b9cSLuc Michel trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]);
2009527d296fSLuc Michel break;
2010527d296fSLuc Michel }
2011527d296fSLuc Michel
2012527d296fSLuc Michel default:
2013527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR,
2014527d296fSLuc Michel "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
2015527d296fSLuc Michel return MEMTX_OK;
2016527d296fSLuc Michel }
2017527d296fSLuc Michel
2018cbe1282bSLuc Michel gic_update_virt(s);
2019527d296fSLuc Michel return MEMTX_OK;
2020527d296fSLuc Michel }
2021527d296fSLuc Michel
gic_thiscpu_hyp_read(void * opaque,hwaddr addr,uint64_t * data,unsigned size,MemTxAttrs attrs)2022527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
2023527d296fSLuc Michel unsigned size, MemTxAttrs attrs)
2024527d296fSLuc Michel {
2025527d296fSLuc Michel GICState *s = (GICState *)opaque;
2026527d296fSLuc Michel
2027527d296fSLuc Michel return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
2028527d296fSLuc Michel }
2029527d296fSLuc Michel
gic_thiscpu_hyp_write(void * opaque,hwaddr addr,uint64_t value,unsigned size,MemTxAttrs attrs)2030527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
2031527d296fSLuc Michel uint64_t value, unsigned size,
2032527d296fSLuc Michel MemTxAttrs attrs)
2033527d296fSLuc Michel {
2034527d296fSLuc Michel GICState *s = (GICState *)opaque;
2035527d296fSLuc Michel
2036527d296fSLuc Michel return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
2037527d296fSLuc Michel }
2038527d296fSLuc Michel
gic_do_hyp_read(void * opaque,hwaddr addr,uint64_t * data,unsigned size,MemTxAttrs attrs)2039527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
2040527d296fSLuc Michel unsigned size, MemTxAttrs attrs)
2041527d296fSLuc Michel {
2042527d296fSLuc Michel GICState **backref = (GICState **)opaque;
2043527d296fSLuc Michel GICState *s = *backref;
2044527d296fSLuc Michel int id = (backref - s->backref);
2045527d296fSLuc Michel
2046527d296fSLuc Michel return gic_hyp_read(s, id, addr, data, attrs);
2047527d296fSLuc Michel }
2048527d296fSLuc Michel
gic_do_hyp_write(void * opaque,hwaddr addr,uint64_t value,unsigned size,MemTxAttrs attrs)2049527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
2050527d296fSLuc Michel uint64_t value, unsigned size,
2051527d296fSLuc Michel MemTxAttrs attrs)
2052527d296fSLuc Michel {
2053527d296fSLuc Michel GICState **backref = (GICState **)opaque;
2054527d296fSLuc Michel GICState *s = *backref;
2055527d296fSLuc Michel int id = (backref - s->backref);
2056527d296fSLuc Michel
2057527d296fSLuc Michel return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
2058527d296fSLuc Michel
2059527d296fSLuc Michel }
2060527d296fSLuc Michel
20617926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = {
20627926c210SPavel Fedin {
20637926c210SPavel Fedin .read_with_attrs = gic_dist_read,
20647926c210SPavel Fedin .write_with_attrs = gic_dist_write,
20657926c210SPavel Fedin .endianness = DEVICE_NATIVE_ENDIAN,
20667926c210SPavel Fedin },
20677926c210SPavel Fedin {
2068a9d85353SPeter Maydell .read_with_attrs = gic_thiscpu_read,
2069a9d85353SPeter Maydell .write_with_attrs = gic_thiscpu_write,
2070e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN,
20717926c210SPavel Fedin }
2072e2c56465SPeter Maydell };
2073e2c56465SPeter Maydell
2074e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = {
2075a9d85353SPeter Maydell .read_with_attrs = gic_do_cpu_read,
2076a9d85353SPeter Maydell .write_with_attrs = gic_do_cpu_write,
2077e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN,
2078e2c56465SPeter Maydell };
2079e69954b9Spbrook
20802c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = {
20812c679ac7SLuc Michel {
2082527d296fSLuc Michel .read_with_attrs = gic_thiscpu_hyp_read,
2083527d296fSLuc Michel .write_with_attrs = gic_thiscpu_hyp_write,
20842c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN,
20852c679ac7SLuc Michel },
20862c679ac7SLuc Michel {
20872c679ac7SLuc Michel .read_with_attrs = gic_thisvcpu_read,
20882c679ac7SLuc Michel .write_with_attrs = gic_thisvcpu_write,
20892c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN,
20902c679ac7SLuc Michel }
20912c679ac7SLuc Michel };
20922c679ac7SLuc Michel
2093527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = {
2094527d296fSLuc Michel .read_with_attrs = gic_do_hyp_read,
2095527d296fSLuc Michel .write_with_attrs = gic_do_hyp_write,
2096527d296fSLuc Michel .endianness = DEVICE_NATIVE_ENDIAN,
2097527d296fSLuc Michel };
2098527d296fSLuc Michel
arm_gic_realize(DeviceState * dev,Error ** errp)209953111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp)
21002b518c56SPeter Maydell {
210153111180SPeter Maydell /* Device instance realize function for the GIC sysbus device */
21022b518c56SPeter Maydell int i;
210353111180SPeter Maydell GICState *s = ARM_GIC(dev);
210453111180SPeter Maydell SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
21051e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
21060175ba10SMarkus Armbruster Error *local_err = NULL;
21071e8cae4dSPeter Maydell
21080175ba10SMarkus Armbruster agc->parent_realize(dev, &local_err);
21090175ba10SMarkus Armbruster if (local_err) {
21100175ba10SMarkus Armbruster error_propagate(errp, local_err);
211153111180SPeter Maydell return;
211253111180SPeter Maydell }
21131e8cae4dSPeter Maydell
21145d721b78SAlexander Graf if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
21155d721b78SAlexander Graf error_setg(errp, "KVM with user space irqchip only works when the "
21165d721b78SAlexander Graf "host kernel supports KVM_CAP_ARM_USER_IRQ");
21175d721b78SAlexander Graf return;
21185d721b78SAlexander Graf }
21195d721b78SAlexander Graf
212011411489SSai Pavan Boddu if (s->n_prio_bits > GIC_MAX_PRIORITY_BITS ||
212111411489SSai Pavan Boddu (s->virt_extn ? s->n_prio_bits < GIC_VIRT_MAX_GROUP_PRIO_BITS :
212211411489SSai Pavan Boddu s->n_prio_bits < GIC_MIN_PRIORITY_BITS)) {
212311411489SSai Pavan Boddu error_setg(errp, "num-priority-bits cannot be greater than %d"
212411411489SSai Pavan Boddu " or less than %d", GIC_MAX_PRIORITY_BITS,
212511411489SSai Pavan Boddu s->virt_extn ? GIC_VIRT_MAX_GROUP_PRIO_BITS :
212611411489SSai Pavan Boddu GIC_MIN_PRIORITY_BITS);
212711411489SSai Pavan Boddu return;
212811411489SSai Pavan Boddu }
212911411489SSai Pavan Boddu
21302c679ac7SLuc Michel /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if
21312c679ac7SLuc Michel * enabled, virtualization extensions related interfaces (main virtual
21322c679ac7SLuc Michel * interface (s->vifaceiomem[0]) and virtual CPU interface).
21332c679ac7SLuc Michel */
21342c679ac7SLuc Michel gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
21352b518c56SPeter Maydell
21367926c210SPavel Fedin /* Extra core-specific regions for the CPU interfaces. This is
21377926c210SPavel Fedin * necessary for "franken-GIC" implementations, for example on
21387926c210SPavel Fedin * Exynos 4.
2139e2c56465SPeter Maydell * NB that the memory region size of 0x100 applies for the 11MPCore
2140e2c56465SPeter Maydell * and also cores following the GIC v1 spec (ie A9).
2141e2c56465SPeter Maydell * GIC v2 defines a larger memory region (0x1000) so this will need
2142e2c56465SPeter Maydell * to be extended when we implement A15.
2143e2c56465SPeter Maydell */
2144b95690c9SWei Huang for (i = 0; i < s->num_cpu; i++) {
2145e2c56465SPeter Maydell s->backref[i] = s;
21461437c94bSPaolo Bonzini memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
21471437c94bSPaolo Bonzini &s->backref[i], "gic_cpu", 0x100);
21487926c210SPavel Fedin sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
2149496dbcd1SPeter Maydell }
2150527d296fSLuc Michel
2151527d296fSLuc Michel /* Extra core-specific regions for virtual interfaces. This is required by
2152527d296fSLuc Michel * the GICv2 specification.
2153527d296fSLuc Michel */
2154527d296fSLuc Michel if (s->virt_extn) {
2155527d296fSLuc Michel for (i = 0; i < s->num_cpu; i++) {
2156527d296fSLuc Michel memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
2157527d296fSLuc Michel &gic_viface_ops, &s->backref[i],
21587210918cSPeter Maydell "gic_viface", 0x200);
2159527d296fSLuc Michel sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
2160527d296fSLuc Michel }
2161527d296fSLuc Michel }
2162527d296fSLuc Michel
2163496dbcd1SPeter Maydell }
2164496dbcd1SPeter Maydell
arm_gic_class_init(ObjectClass * klass,const void * data)2165*12d1a768SPhilippe Mathieu-Daudé static void arm_gic_class_init(ObjectClass *klass, const void *data)
2166496dbcd1SPeter Maydell {
2167496dbcd1SPeter Maydell DeviceClass *dc = DEVICE_CLASS(klass);
21681e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_CLASS(klass);
216953111180SPeter Maydell
2170bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
2171496dbcd1SPeter Maydell }
2172496dbcd1SPeter Maydell
21738c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = {
21741e8cae4dSPeter Maydell .name = TYPE_ARM_GIC,
21751e8cae4dSPeter Maydell .parent = TYPE_ARM_GIC_COMMON,
2176fae15286SPeter Maydell .instance_size = sizeof(GICState),
2177496dbcd1SPeter Maydell .class_init = arm_gic_class_init,
2178998a74bcSPeter Maydell .class_size = sizeof(ARMGICClass),
2179496dbcd1SPeter Maydell };
2180496dbcd1SPeter Maydell
arm_gic_register_types(void)2181496dbcd1SPeter Maydell static void arm_gic_register_types(void)
2182496dbcd1SPeter Maydell {
2183496dbcd1SPeter Maydell type_register_static(&arm_gic_info);
2184496dbcd1SPeter Maydell }
2185496dbcd1SPeter Maydell
2186496dbcd1SPeter Maydell type_init(arm_gic_register_types)
2187