1e69954b9Spbrook /* 29ee6e8bbSpbrook * ARM Generic/Distributed Interrupt Controller 3e69954b9Spbrook * 49ee6e8bbSpbrook * Copyright (c) 2006-2007 CodeSourcery. 5e69954b9Spbrook * Written by Paul Brook 6e69954b9Spbrook * 78e31bf38SMatthew Fernandez * This code is licensed under the GPL. 8e69954b9Spbrook */ 9e69954b9Spbrook 109ee6e8bbSpbrook /* This file contains implementation code for the RealView EB interrupt 110d256bdcSPeter Maydell * controller, MPCore distributed interrupt controller and ARMv7-M 120d256bdcSPeter Maydell * Nested Vectored Interrupt Controller. 130d256bdcSPeter Maydell * It is compiled in two ways: 140d256bdcSPeter Maydell * (1) as a standalone file to produce a sysbus device which is a GIC 150d256bdcSPeter Maydell * that can be used on the realview board and as one of the builtin 160d256bdcSPeter Maydell * private peripherals for the ARM MP CPUs (11MPCore, A9, etc) 170d256bdcSPeter Maydell * (2) by being directly #included into armv7m_nvic.c to produce the 180d256bdcSPeter Maydell * armv7m_nvic device. 190d256bdcSPeter Maydell */ 20e69954b9Spbrook 218ef94f0bSPeter Maydell #include "qemu/osdep.h" 2283c9f4caSPaolo Bonzini #include "hw/sysbus.h" 2347b43a1fSPaolo Bonzini #include "gic_internal.h" 24da34e65cSMarkus Armbruster #include "qapi/error.h" 25dfc08079SAndreas Färber #include "qom/cpu.h" 2603dd024fSPaolo Bonzini #include "qemu/log.h" 272531088fSHollis Blanchard #include "trace.h" 285d721b78SAlexander Graf #include "sysemu/kvm.h" 29386e2955SPeter Maydell 3068bf93ceSAlex Bennée /* #define DEBUG_GIC */ 31e69954b9Spbrook 32e69954b9Spbrook #ifdef DEBUG_GIC 3368bf93ceSAlex Bennée #define DEBUG_GIC_GATE 1 34e69954b9Spbrook #else 3568bf93ceSAlex Bennée #define DEBUG_GIC_GATE 0 36e69954b9Spbrook #endif 37e69954b9Spbrook 3868bf93ceSAlex Bennée #define DPRINTF(fmt, ...) do { \ 3968bf93ceSAlex Bennée if (DEBUG_GIC_GATE) { \ 4068bf93ceSAlex Bennée fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ 4168bf93ceSAlex Bennée } \ 4268bf93ceSAlex Bennée } while (0) 4368bf93ceSAlex Bennée 443355c360SAlistair Francis static const uint8_t gic_id_11mpcore[] = { 453355c360SAlistair Francis 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 463355c360SAlistair Francis }; 473355c360SAlistair Francis 483355c360SAlistair Francis static const uint8_t gic_id_gicv1[] = { 493355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 503355c360SAlistair Francis }; 513355c360SAlistair Francis 523355c360SAlistair Francis static const uint8_t gic_id_gicv2[] = { 533355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 542a29ddeeSPeter Maydell }; 552a29ddeeSPeter Maydell 56fae15286SPeter Maydell static inline int gic_get_current_cpu(GICState *s) 57926c4affSPeter Maydell { 58926c4affSPeter Maydell if (s->num_cpu > 1) { 594917cf44SAndreas Färber return current_cpu->cpu_index; 60926c4affSPeter Maydell } 61926c4affSPeter Maydell return 0; 62926c4affSPeter Maydell } 63926c4affSPeter Maydell 644a37e0e4SLuc Michel static inline int gic_get_current_vcpu(GICState *s) 654a37e0e4SLuc Michel { 664a37e0e4SLuc Michel return gic_get_current_cpu(s) + GIC_NCPU; 674a37e0e4SLuc Michel } 684a37e0e4SLuc Michel 69c27a5ba9SFabian Aggeler /* Return true if this GIC config has interrupt groups, which is 70c27a5ba9SFabian Aggeler * true if we're a GICv2, or a GICv1 with the security extensions. 71c27a5ba9SFabian Aggeler */ 72c27a5ba9SFabian Aggeler static inline bool gic_has_groups(GICState *s) 73c27a5ba9SFabian Aggeler { 74c27a5ba9SFabian Aggeler return s->revision == 2 || s->security_extn; 75c27a5ba9SFabian Aggeler } 76c27a5ba9SFabian Aggeler 773dd0471bSLuc Michel static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs) 783dd0471bSLuc Michel { 793dd0471bSLuc Michel return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure; 803dd0471bSLuc Michel } 813dd0471bSLuc Michel 82cbe1282bSLuc Michel static inline void gic_get_best_irq(GICState *s, int cpu, 83cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 84cbe1282bSLuc Michel { 85cbe1282bSLuc Michel int irq; 86cbe1282bSLuc Michel int cm = 1 << cpu; 87cbe1282bSLuc Michel 88cbe1282bSLuc Michel *best_irq = 1023; 89cbe1282bSLuc Michel *best_prio = 0x100; 90cbe1282bSLuc Michel 91cbe1282bSLuc Michel for (irq = 0; irq < s->num_irq; irq++) { 92cbe1282bSLuc Michel if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) && 93cbe1282bSLuc Michel (!GIC_DIST_TEST_ACTIVE(irq, cm)) && 94cbe1282bSLuc Michel (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) { 95cbe1282bSLuc Michel if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) { 96cbe1282bSLuc Michel *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu); 97cbe1282bSLuc Michel *best_irq = irq; 98cbe1282bSLuc Michel } 99cbe1282bSLuc Michel } 100cbe1282bSLuc Michel } 101cbe1282bSLuc Michel 102cbe1282bSLuc Michel if (*best_irq < 1023) { 103cbe1282bSLuc Michel *group = GIC_DIST_TEST_GROUP(*best_irq, cm); 104cbe1282bSLuc Michel } 105cbe1282bSLuc Michel } 106cbe1282bSLuc Michel 107cbe1282bSLuc Michel static inline void gic_get_best_virq(GICState *s, int cpu, 108cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 109cbe1282bSLuc Michel { 110cbe1282bSLuc Michel int lr_idx = 0; 111cbe1282bSLuc Michel 112cbe1282bSLuc Michel *best_irq = 1023; 113cbe1282bSLuc Michel *best_prio = 0x100; 114cbe1282bSLuc Michel 115cbe1282bSLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) { 116cbe1282bSLuc Michel uint32_t lr_entry = s->h_lr[lr_idx][cpu]; 117cbe1282bSLuc Michel int state = GICH_LR_STATE(lr_entry); 118cbe1282bSLuc Michel 119cbe1282bSLuc Michel if (state == GICH_LR_STATE_PENDING) { 120cbe1282bSLuc Michel int prio = GICH_LR_PRIORITY(lr_entry); 121cbe1282bSLuc Michel 122cbe1282bSLuc Michel if (prio < *best_prio) { 123cbe1282bSLuc Michel *best_prio = prio; 124cbe1282bSLuc Michel *best_irq = GICH_LR_VIRT_ID(lr_entry); 125cbe1282bSLuc Michel *group = GICH_LR_GROUP(lr_entry); 126cbe1282bSLuc Michel } 127cbe1282bSLuc Michel } 128cbe1282bSLuc Michel } 129cbe1282bSLuc Michel } 130cbe1282bSLuc Michel 131cbe1282bSLuc Michel /* Return true if IRQ signaling is enabled for the given cpu and at least one 132cbe1282bSLuc Michel * of the given groups: 133cbe1282bSLuc Michel * - in the non-virt case, the distributor must be enabled for one of the 134cbe1282bSLuc Michel * given groups 135cbe1282bSLuc Michel * - in the virt case, the virtual interface must be enabled. 136cbe1282bSLuc Michel * - in all cases, the (v)CPU interface must be enabled for one of the given 137cbe1282bSLuc Michel * groups. 138cbe1282bSLuc Michel */ 139cbe1282bSLuc Michel static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt, 140cbe1282bSLuc Michel int group_mask) 141cbe1282bSLuc Michel { 142cbe1282bSLuc Michel if (!virt && !(s->ctlr & group_mask)) { 143cbe1282bSLuc Michel return false; 144cbe1282bSLuc Michel } 145cbe1282bSLuc Michel 146cbe1282bSLuc Michel if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) { 147cbe1282bSLuc Michel return false; 148cbe1282bSLuc Michel } 149cbe1282bSLuc Michel 150cbe1282bSLuc Michel if (!(s->cpu_ctlr[cpu] & group_mask)) { 151cbe1282bSLuc Michel return false; 152cbe1282bSLuc Michel } 153cbe1282bSLuc Michel 154cbe1282bSLuc Michel return true; 155cbe1282bSLuc Michel } 156cbe1282bSLuc Michel 157e69954b9Spbrook /* TODO: Many places that call this routine could be optimized. */ 158e69954b9Spbrook /* Update interrupt status after enabled or pending bits have been changed. */ 159cbe1282bSLuc Michel static inline void gic_update_internal(GICState *s, bool virt) 160e69954b9Spbrook { 161e69954b9Spbrook int best_irq; 162e69954b9Spbrook int best_prio; 163dadbb58fSPeter Maydell int irq_level, fiq_level; 164cbe1282bSLuc Michel int cpu, cpu_iface; 165cbe1282bSLuc Michel int group = 0; 166cbe1282bSLuc Michel qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq; 167cbe1282bSLuc Michel qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq; 168e69954b9Spbrook 169b95690c9SWei Huang for (cpu = 0; cpu < s->num_cpu; cpu++) { 170cbe1282bSLuc Michel cpu_iface = virt ? (cpu + GIC_NCPU) : cpu; 171cbe1282bSLuc Michel 172cbe1282bSLuc Michel s->current_pending[cpu_iface] = 1023; 173cbe1282bSLuc Michel if (!gic_irq_signaling_enabled(s, cpu, virt, 174cbe1282bSLuc Michel GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) { 175cbe1282bSLuc Michel qemu_irq_lower(irq_lines[cpu]); 176cbe1282bSLuc Michel qemu_irq_lower(fiq_lines[cpu]); 177235069a3SJohan Karlsson continue; 178e69954b9Spbrook } 179cbe1282bSLuc Michel 180cbe1282bSLuc Michel if (virt) { 181cbe1282bSLuc Michel gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group); 182cbe1282bSLuc Michel } else { 183cbe1282bSLuc Michel gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group); 184e69954b9Spbrook } 185dadbb58fSPeter Maydell 1862531088fSHollis Blanchard if (best_irq != 1023) { 187*067a2b9cSLuc Michel trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu, 188*067a2b9cSLuc Michel best_irq, best_prio, 189*067a2b9cSLuc Michel s->priority_mask[cpu_iface], 190*067a2b9cSLuc Michel s->running_priority[cpu_iface]); 1912531088fSHollis Blanchard } 1922531088fSHollis Blanchard 193dadbb58fSPeter Maydell irq_level = fiq_level = 0; 194dadbb58fSPeter Maydell 195cbe1282bSLuc Michel if (best_prio < s->priority_mask[cpu_iface]) { 196cbe1282bSLuc Michel s->current_pending[cpu_iface] = best_irq; 197cbe1282bSLuc Michel if (best_prio < s->running_priority[cpu_iface]) { 198cbe1282bSLuc Michel if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) { 199cbe1282bSLuc Michel if (group == 0 && 200cbe1282bSLuc Michel s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) { 201dadbb58fSPeter Maydell DPRINTF("Raised pending FIQ %d (cpu %d)\n", 202cbe1282bSLuc Michel best_irq, cpu_iface); 203dadbb58fSPeter Maydell fiq_level = 1; 204cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq", 205cbe1282bSLuc Michel fiq_level); 206dadbb58fSPeter Maydell } else { 207dadbb58fSPeter Maydell DPRINTF("Raised pending IRQ %d (cpu %d)\n", 208cbe1282bSLuc Michel best_irq, cpu_iface); 209dadbb58fSPeter Maydell irq_level = 1; 210cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "virq" : "irq", 211cbe1282bSLuc Michel irq_level); 212e69954b9Spbrook } 213e69954b9Spbrook } 214dadbb58fSPeter Maydell } 215dadbb58fSPeter Maydell } 216dadbb58fSPeter Maydell 217cbe1282bSLuc Michel qemu_set_irq(irq_lines[cpu], irq_level); 218cbe1282bSLuc Michel qemu_set_irq(fiq_lines[cpu], fiq_level); 2199ee6e8bbSpbrook } 220e69954b9Spbrook } 221e69954b9Spbrook 222cbe1282bSLuc Michel static void gic_update(GICState *s) 223cbe1282bSLuc Michel { 224cbe1282bSLuc Michel gic_update_internal(s, false); 225cbe1282bSLuc Michel } 226cbe1282bSLuc Michel 227527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit 228527d296fSLuc Michel * in ELRSR is set. 229527d296fSLuc Michel */ 230527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry) 231527d296fSLuc Michel { 232527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 233527d296fSLuc Michel && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry)); 234527d296fSLuc Michel } 235527d296fSLuc Michel 236527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the 237527d296fSLuc Michel * corrsponding bit in EISR is set. 238527d296fSLuc Michel */ 239527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry) 240527d296fSLuc Michel { 241527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 242527d296fSLuc Michel && !GICH_LR_HW(entry) && GICH_LR_EOI(entry); 243527d296fSLuc Michel } 244527d296fSLuc Michel 24550e57926SLuc Michel static inline void gic_extract_lr_info(GICState *s, int cpu, 24650e57926SLuc Michel int *num_eoi, int *num_valid, int *num_pending) 24750e57926SLuc Michel { 24850e57926SLuc Michel int lr_idx; 24950e57926SLuc Michel 25050e57926SLuc Michel *num_eoi = 0; 25150e57926SLuc Michel *num_valid = 0; 25250e57926SLuc Michel *num_pending = 0; 25350e57926SLuc Michel 25450e57926SLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) { 25550e57926SLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 25650e57926SLuc Michel 25750e57926SLuc Michel if (gic_lr_entry_is_eoi(*entry)) { 25850e57926SLuc Michel (*num_eoi)++; 25950e57926SLuc Michel } 26050e57926SLuc Michel 26150e57926SLuc Michel if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) { 26250e57926SLuc Michel (*num_valid)++; 26350e57926SLuc Michel } 26450e57926SLuc Michel 26550e57926SLuc Michel if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) { 26650e57926SLuc Michel (*num_pending)++; 26750e57926SLuc Michel } 26850e57926SLuc Michel } 26950e57926SLuc Michel } 27050e57926SLuc Michel 27150e57926SLuc Michel static void gic_compute_misr(GICState *s, int cpu) 27250e57926SLuc Michel { 27350e57926SLuc Michel uint32_t value = 0; 27450e57926SLuc Michel int vcpu = cpu + GIC_NCPU; 27550e57926SLuc Michel 27650e57926SLuc Michel int num_eoi, num_valid, num_pending; 27750e57926SLuc Michel 27850e57926SLuc Michel gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending); 27950e57926SLuc Michel 28050e57926SLuc Michel /* EOI */ 28150e57926SLuc Michel if (num_eoi) { 28250e57926SLuc Michel value |= R_GICH_MISR_EOI_MASK; 28350e57926SLuc Michel } 28450e57926SLuc Michel 28550e57926SLuc Michel /* U: true if only 0 or 1 LR entry is valid */ 28650e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) { 28750e57926SLuc Michel value |= R_GICH_MISR_U_MASK; 28850e57926SLuc Michel } 28950e57926SLuc Michel 29050e57926SLuc Michel /* LRENP: EOICount is not 0 */ 29150e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) && 29250e57926SLuc Michel ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) { 29350e57926SLuc Michel value |= R_GICH_MISR_LRENP_MASK; 29450e57926SLuc Michel } 29550e57926SLuc Michel 29650e57926SLuc Michel /* NP: no pending interrupts */ 29750e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) { 29850e57926SLuc Michel value |= R_GICH_MISR_NP_MASK; 29950e57926SLuc Michel } 30050e57926SLuc Michel 30150e57926SLuc Michel /* VGrp0E: group0 virq signaling enabled */ 30250e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) && 30350e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) { 30450e57926SLuc Michel value |= R_GICH_MISR_VGrp0E_MASK; 30550e57926SLuc Michel } 30650e57926SLuc Michel 30750e57926SLuc Michel /* VGrp0D: group0 virq signaling disabled */ 30850e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) && 30950e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) { 31050e57926SLuc Michel value |= R_GICH_MISR_VGrp0D_MASK; 31150e57926SLuc Michel } 31250e57926SLuc Michel 31350e57926SLuc Michel /* VGrp1E: group1 virq signaling enabled */ 31450e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) && 31550e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) { 31650e57926SLuc Michel value |= R_GICH_MISR_VGrp1E_MASK; 31750e57926SLuc Michel } 31850e57926SLuc Michel 31950e57926SLuc Michel /* VGrp1D: group1 virq signaling disabled */ 32050e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) && 32150e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) { 32250e57926SLuc Michel value |= R_GICH_MISR_VGrp1D_MASK; 32350e57926SLuc Michel } 32450e57926SLuc Michel 32550e57926SLuc Michel s->h_misr[cpu] = value; 32650e57926SLuc Michel } 32750e57926SLuc Michel 32850e57926SLuc Michel static void gic_update_maintenance(GICState *s) 32950e57926SLuc Michel { 33050e57926SLuc Michel int cpu = 0; 33150e57926SLuc Michel int maint_level; 33250e57926SLuc Michel 33350e57926SLuc Michel for (cpu = 0; cpu < s->num_cpu; cpu++) { 33450e57926SLuc Michel gic_compute_misr(s, cpu); 33550e57926SLuc Michel maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu]; 33650e57926SLuc Michel 337*067a2b9cSLuc Michel trace_gic_update_maintenance_irq(cpu, maint_level); 33850e57926SLuc Michel qemu_set_irq(s->maintenance_irq[cpu], maint_level); 33950e57926SLuc Michel } 34050e57926SLuc Michel } 34150e57926SLuc Michel 342cbe1282bSLuc Michel static void gic_update_virt(GICState *s) 343cbe1282bSLuc Michel { 344cbe1282bSLuc Michel gic_update_internal(s, true); 34550e57926SLuc Michel gic_update_maintenance(s); 346cbe1282bSLuc Michel } 347cbe1282bSLuc Michel 3488d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level, 3498d999995SChristoffer Dall int cm, int target) 3508d999995SChristoffer Dall { 3518d999995SChristoffer Dall if (level) { 35267ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 35367ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) { 3548d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 35567ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 3568d999995SChristoffer Dall } 3578d999995SChristoffer Dall } else { 35867ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 3598d999995SChristoffer Dall } 3608d999995SChristoffer Dall } 3618d999995SChristoffer Dall 3628d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level, 3638d999995SChristoffer Dall int cm, int target) 3648d999995SChristoffer Dall { 3658d999995SChristoffer Dall if (level) { 36667ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 3678d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 36867ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) { 36967ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 3708d999995SChristoffer Dall } 3718d999995SChristoffer Dall } else { 37267ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 3738d999995SChristoffer Dall } 3748d999995SChristoffer Dall } 3758d999995SChristoffer Dall 3769ee6e8bbSpbrook /* Process a change in an external IRQ input. */ 377e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level) 378e69954b9Spbrook { 379544d1afaSPeter Maydell /* Meaning of the 'irq' parameter: 380544d1afaSPeter Maydell * [0..N-1] : external interrupts 381544d1afaSPeter Maydell * [N..N+31] : PPI (internal) interrupts for CPU 0 382544d1afaSPeter Maydell * [N+32..N+63] : PPI (internal interrupts for CPU 1 383544d1afaSPeter Maydell * ... 384544d1afaSPeter Maydell */ 385fae15286SPeter Maydell GICState *s = (GICState *)opaque; 386544d1afaSPeter Maydell int cm, target; 387544d1afaSPeter Maydell if (irq < (s->num_irq - GIC_INTERNAL)) { 388e69954b9Spbrook /* The first external input line is internal interrupt 32. */ 389544d1afaSPeter Maydell cm = ALL_CPU_MASK; 39069253800SRusty Russell irq += GIC_INTERNAL; 39167ce697aSLuc Michel target = GIC_DIST_TARGET(irq); 392544d1afaSPeter Maydell } else { 393544d1afaSPeter Maydell int cpu; 394544d1afaSPeter Maydell irq -= (s->num_irq - GIC_INTERNAL); 395544d1afaSPeter Maydell cpu = irq / GIC_INTERNAL; 396544d1afaSPeter Maydell irq %= GIC_INTERNAL; 397544d1afaSPeter Maydell cm = 1 << cpu; 398544d1afaSPeter Maydell target = cm; 399544d1afaSPeter Maydell } 400544d1afaSPeter Maydell 40140d22500SChristoffer Dall assert(irq >= GIC_NR_SGIS); 40240d22500SChristoffer Dall 40367ce697aSLuc Michel if (level == GIC_DIST_TEST_LEVEL(irq, cm)) { 404e69954b9Spbrook return; 405544d1afaSPeter Maydell } 406e69954b9Spbrook 4073bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 4088d999995SChristoffer Dall gic_set_irq_11mpcore(s, irq, level, cm, target); 409e69954b9Spbrook } else { 4108d999995SChristoffer Dall gic_set_irq_generic(s, irq, level, cm, target); 411e69954b9Spbrook } 4122531088fSHollis Blanchard trace_gic_set_irq(irq, level, cm, target); 4138d999995SChristoffer Dall 414e69954b9Spbrook gic_update(s); 415e69954b9Spbrook } 416e69954b9Spbrook 4177c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu, 4187c0fa108SFabian Aggeler MemTxAttrs attrs) 4197c0fa108SFabian Aggeler { 4207c0fa108SFabian Aggeler uint16_t pending_irq = s->current_pending[cpu]; 4217c0fa108SFabian Aggeler 4227c0fa108SFabian Aggeler if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) { 42386b350f0SLuc Michel int group = gic_test_group(s, pending_irq, cpu); 42486b350f0SLuc Michel 4257c0fa108SFabian Aggeler /* On a GIC without the security extensions, reading this register 4267c0fa108SFabian Aggeler * behaves in the same way as a secure access to a GIC with them. 4277c0fa108SFabian Aggeler */ 4283dd0471bSLuc Michel bool secure = !gic_cpu_ns_access(s, cpu, attrs); 4297c0fa108SFabian Aggeler 4307c0fa108SFabian Aggeler if (group == 0 && !secure) { 4317c0fa108SFabian Aggeler /* Group0 interrupts hidden from Non-secure access */ 4327c0fa108SFabian Aggeler return 1023; 4337c0fa108SFabian Aggeler } 4347c0fa108SFabian Aggeler if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) { 4357c0fa108SFabian Aggeler /* Group1 interrupts only seen by Secure access if 4367c0fa108SFabian Aggeler * AckCtl bit set. 4377c0fa108SFabian Aggeler */ 4387c0fa108SFabian Aggeler return 1022; 4397c0fa108SFabian Aggeler } 4407c0fa108SFabian Aggeler } 4417c0fa108SFabian Aggeler return pending_irq; 4427c0fa108SFabian Aggeler } 4437c0fa108SFabian Aggeler 444df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq) 445df92cfa6SPeter Maydell { 446df92cfa6SPeter Maydell /* Return the group priority of the specified interrupt 447df92cfa6SPeter Maydell * (which is the top bits of its priority, with the number 448df92cfa6SPeter Maydell * of bits masked determined by the applicable binary point register). 449df92cfa6SPeter Maydell */ 450df92cfa6SPeter Maydell int bpr; 451df92cfa6SPeter Maydell uint32_t mask; 452df92cfa6SPeter Maydell 453df92cfa6SPeter Maydell if (gic_has_groups(s) && 454df92cfa6SPeter Maydell !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) && 45586b350f0SLuc Michel gic_test_group(s, irq, cpu)) { 456fc05a6f2SLuc MICHEL bpr = s->abpr[cpu] - 1; 457fc05a6f2SLuc MICHEL assert(bpr >= 0); 458df92cfa6SPeter Maydell } else { 459df92cfa6SPeter Maydell bpr = s->bpr[cpu]; 460df92cfa6SPeter Maydell } 461df92cfa6SPeter Maydell 462df92cfa6SPeter Maydell /* a BPR of 0 means the group priority bits are [7:1]; 463df92cfa6SPeter Maydell * a BPR of 1 means they are [7:2], and so on down to 464df92cfa6SPeter Maydell * a BPR of 7 meaning no group priority bits at all. 465df92cfa6SPeter Maydell */ 466df92cfa6SPeter Maydell mask = ~0U << ((bpr & 7) + 1); 467df92cfa6SPeter Maydell 46886b350f0SLuc Michel return gic_get_priority(s, irq, cpu) & mask; 469df92cfa6SPeter Maydell } 470df92cfa6SPeter Maydell 47172889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq) 472e69954b9Spbrook { 47372889c8aSPeter Maydell /* Set the appropriate Active Priority Register bit for this IRQ, 47472889c8aSPeter Maydell * and update the running priority. 47572889c8aSPeter Maydell */ 47672889c8aSPeter Maydell int prio = gic_get_group_priority(s, cpu, irq); 477a1d7b8d8SLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 478a1d7b8d8SLuc Michel int preemption_level = prio >> (min_bpr + 1); 47972889c8aSPeter Maydell int regno = preemption_level / 32; 48072889c8aSPeter Maydell int bitno = preemption_level % 32; 481a1d7b8d8SLuc Michel uint32_t *papr = NULL; 48272889c8aSPeter Maydell 483a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 484a1d7b8d8SLuc Michel assert(regno == 0); 485a1d7b8d8SLuc Michel papr = &s->h_apr[gic_get_vcpu_real_id(cpu)]; 486a1d7b8d8SLuc Michel } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) { 487a1d7b8d8SLuc Michel papr = &s->nsapr[regno][cpu]; 4889ee6e8bbSpbrook } else { 489a1d7b8d8SLuc Michel papr = &s->apr[regno][cpu]; 4909ee6e8bbSpbrook } 49172889c8aSPeter Maydell 492a1d7b8d8SLuc Michel *papr |= (1 << bitno); 493a1d7b8d8SLuc Michel 49472889c8aSPeter Maydell s->running_priority[cpu] = prio; 49586b350f0SLuc Michel gic_set_active(s, irq, cpu); 49672889c8aSPeter Maydell } 49772889c8aSPeter Maydell 49872889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu) 49972889c8aSPeter Maydell { 50072889c8aSPeter Maydell /* Recalculate the current running priority for this CPU based 50172889c8aSPeter Maydell * on the set bits in the Active Priority Registers. 50272889c8aSPeter Maydell */ 50372889c8aSPeter Maydell int i; 504a1d7b8d8SLuc Michel 505a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 506a1d7b8d8SLuc Michel uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)]; 507a1d7b8d8SLuc Michel if (apr) { 508a1d7b8d8SLuc Michel return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1); 509a1d7b8d8SLuc Michel } else { 510a1d7b8d8SLuc Michel return 0x100; 511a1d7b8d8SLuc Michel } 512a1d7b8d8SLuc Michel } 513a1d7b8d8SLuc Michel 51472889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 51572889c8aSPeter Maydell uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu]; 51672889c8aSPeter Maydell if (!apr) { 51772889c8aSPeter Maydell continue; 51872889c8aSPeter Maydell } 51972889c8aSPeter Maydell return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1); 52072889c8aSPeter Maydell } 52172889c8aSPeter Maydell return 0x100; 52272889c8aSPeter Maydell } 52372889c8aSPeter Maydell 52472889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group) 52572889c8aSPeter Maydell { 52672889c8aSPeter Maydell /* Drop the priority of the currently active interrupt in the 52772889c8aSPeter Maydell * specified group. 52872889c8aSPeter Maydell * 52972889c8aSPeter Maydell * Note that we can guarantee (because of the requirement to nest 53072889c8aSPeter Maydell * GICC_IAR reads [which activate an interrupt and raise priority] 53172889c8aSPeter Maydell * with GICC_EOIR writes [which drop the priority for the interrupt]) 53272889c8aSPeter Maydell * that the interrupt we're being called for is the highest priority 53372889c8aSPeter Maydell * active interrupt, meaning that it has the lowest set bit in the 53472889c8aSPeter Maydell * APR registers. 53572889c8aSPeter Maydell * 53672889c8aSPeter Maydell * If the guest does not honour the ordering constraints then the 53772889c8aSPeter Maydell * behaviour of the GIC is UNPREDICTABLE, which for us means that 53872889c8aSPeter Maydell * the values of the APR registers might become incorrect and the 53972889c8aSPeter Maydell * running priority will be wrong, so interrupts that should preempt 54072889c8aSPeter Maydell * might not do so, and interrupts that should not preempt might do so. 54172889c8aSPeter Maydell */ 542a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 543a1d7b8d8SLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 544a1d7b8d8SLuc Michel 545a1d7b8d8SLuc Michel if (s->h_apr[rcpu]) { 546a1d7b8d8SLuc Michel /* Clear lowest set bit */ 547a1d7b8d8SLuc Michel s->h_apr[rcpu] &= s->h_apr[rcpu] - 1; 548a1d7b8d8SLuc Michel } 549a1d7b8d8SLuc Michel } else { 55072889c8aSPeter Maydell int i; 55172889c8aSPeter Maydell 55272889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 55372889c8aSPeter Maydell uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu]; 55472889c8aSPeter Maydell if (!*papr) { 55572889c8aSPeter Maydell continue; 55672889c8aSPeter Maydell } 55772889c8aSPeter Maydell /* Clear lowest set bit */ 55872889c8aSPeter Maydell *papr &= *papr - 1; 55972889c8aSPeter Maydell break; 56072889c8aSPeter Maydell } 561a1d7b8d8SLuc Michel } 56272889c8aSPeter Maydell 56372889c8aSPeter Maydell s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu); 564e69954b9Spbrook } 565e69954b9Spbrook 566439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu) 567e69954b9Spbrook { 568439badd6SLuc Michel int src; 569439badd6SLuc Michel uint32_t ret; 570c5619bf9SFabian Aggeler 571439badd6SLuc Michel if (!gic_is_vcpu(cpu)) { 57240d22500SChristoffer Dall /* Lookup the source CPU for the SGI and clear this in the 57340d22500SChristoffer Dall * sgi_pending map. Return the src and clear the overall pending 57440d22500SChristoffer Dall * state on this CPU if the SGI is not pending from any CPUs. 57540d22500SChristoffer Dall */ 57640d22500SChristoffer Dall assert(s->sgi_pending[irq][cpu] != 0); 57740d22500SChristoffer Dall src = ctz32(s->sgi_pending[irq][cpu]); 57840d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~(1 << src); 57940d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 58086b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 58140d22500SChristoffer Dall } 58240d22500SChristoffer Dall ret = irq | ((src & 0x7) << 10); 58340d22500SChristoffer Dall } else { 584439badd6SLuc Michel uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu); 585439badd6SLuc Michel src = GICH_LR_CPUID(*lr_entry); 586439badd6SLuc Michel 587439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 588439badd6SLuc Michel ret = irq | (src << 10); 589439badd6SLuc Michel } 590439badd6SLuc Michel 591439badd6SLuc Michel return ret; 592439badd6SLuc Michel } 593439badd6SLuc Michel 594439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs) 595439badd6SLuc Michel { 596439badd6SLuc Michel int ret, irq; 597439badd6SLuc Michel 598439badd6SLuc Michel /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately 599439badd6SLuc Michel * for the case where this GIC supports grouping and the pending interrupt 600439badd6SLuc Michel * is in the wrong group. 60140d22500SChristoffer Dall */ 602439badd6SLuc Michel irq = gic_get_current_pending_irq(s, cpu, attrs); 603*067a2b9cSLuc Michel trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 604*067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), irq); 605439badd6SLuc Michel 606439badd6SLuc Michel if (irq >= GIC_MAXIRQ) { 607439badd6SLuc Michel DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq); 608439badd6SLuc Michel return irq; 609439badd6SLuc Michel } 610439badd6SLuc Michel 611439badd6SLuc Michel if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) { 612439badd6SLuc Michel DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq); 613439badd6SLuc Michel return 1023; 614439badd6SLuc Michel } 615439badd6SLuc Michel 616439badd6SLuc Michel gic_activate_irq(s, cpu, irq); 617439badd6SLuc Michel 618439badd6SLuc Michel if (s->revision == REV_11MPCORE) { 619439badd6SLuc Michel /* Clear pending flags for both level and edge triggered interrupts. 620439badd6SLuc Michel * Level triggered IRQs will be reasserted once they become inactive. 621439badd6SLuc Michel */ 622439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 623439badd6SLuc Michel ret = irq; 624439badd6SLuc Michel } else { 625439badd6SLuc Michel if (irq < GIC_NR_SGIS) { 626439badd6SLuc Michel ret = gic_clear_pending_sgi(s, irq, cpu); 627439badd6SLuc Michel } else { 62886b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 62940d22500SChristoffer Dall ret = irq; 63040d22500SChristoffer Dall } 63140d22500SChristoffer Dall } 63240d22500SChristoffer Dall 633cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 634cbe1282bSLuc Michel gic_update_virt(s); 635cbe1282bSLuc Michel } else { 63672889c8aSPeter Maydell gic_update(s); 637cbe1282bSLuc Michel } 63840d22500SChristoffer Dall DPRINTF("ACK %d\n", irq); 63940d22500SChristoffer Dall return ret; 640e69954b9Spbrook } 641e69954b9Spbrook 64267ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val, 64381508470SFabian Aggeler MemTxAttrs attrs) 6449df90ad0SChristoffer Dall { 64581508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 64667ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 64781508470SFabian Aggeler return; /* Ignore Non-secure access of Group0 IRQ */ 64881508470SFabian Aggeler } 64981508470SFabian Aggeler val = 0x80 | (val >> 1); /* Non-secure view */ 65081508470SFabian Aggeler } 65181508470SFabian Aggeler 6529df90ad0SChristoffer Dall if (irq < GIC_INTERNAL) { 6539df90ad0SChristoffer Dall s->priority1[irq][cpu] = val; 6549df90ad0SChristoffer Dall } else { 6559df90ad0SChristoffer Dall s->priority2[(irq) - GIC_INTERNAL] = val; 6569df90ad0SChristoffer Dall } 6579df90ad0SChristoffer Dall } 6589df90ad0SChristoffer Dall 65967ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq, 66081508470SFabian Aggeler MemTxAttrs attrs) 66181508470SFabian Aggeler { 66267ce697aSLuc Michel uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu); 66381508470SFabian Aggeler 66481508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 66567ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 66681508470SFabian Aggeler return 0; /* Non-secure access cannot read priority of Group0 IRQ */ 66781508470SFabian Aggeler } 66881508470SFabian Aggeler prio = (prio << 1) & 0xff; /* Non-secure view */ 66981508470SFabian Aggeler } 67081508470SFabian Aggeler return prio; 67181508470SFabian Aggeler } 67281508470SFabian Aggeler 67381508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask, 67481508470SFabian Aggeler MemTxAttrs attrs) 67581508470SFabian Aggeler { 6763dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 67781508470SFabian Aggeler if (s->priority_mask[cpu] & 0x80) { 67881508470SFabian Aggeler /* Priority Mask in upper half */ 67981508470SFabian Aggeler pmask = 0x80 | (pmask >> 1); 68081508470SFabian Aggeler } else { 68181508470SFabian Aggeler /* Non-secure write ignored if priority mask is in lower half */ 68281508470SFabian Aggeler return; 68381508470SFabian Aggeler } 68481508470SFabian Aggeler } 68581508470SFabian Aggeler s->priority_mask[cpu] = pmask; 68681508470SFabian Aggeler } 68781508470SFabian Aggeler 68881508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs) 68981508470SFabian Aggeler { 69081508470SFabian Aggeler uint32_t pmask = s->priority_mask[cpu]; 69181508470SFabian Aggeler 6923dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 69381508470SFabian Aggeler if (pmask & 0x80) { 69481508470SFabian Aggeler /* Priority Mask in upper half, return Non-secure view */ 69581508470SFabian Aggeler pmask = (pmask << 1) & 0xff; 69681508470SFabian Aggeler } else { 69781508470SFabian Aggeler /* Priority Mask in lower half, RAZ */ 69881508470SFabian Aggeler pmask = 0; 69981508470SFabian Aggeler } 70081508470SFabian Aggeler } 70181508470SFabian Aggeler return pmask; 70281508470SFabian Aggeler } 70381508470SFabian Aggeler 70432951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs) 70532951860SFabian Aggeler { 70632951860SFabian Aggeler uint32_t ret = s->cpu_ctlr[cpu]; 70732951860SFabian Aggeler 7083dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 70932951860SFabian Aggeler /* Construct the NS banked view of GICC_CTLR from the correct 71032951860SFabian Aggeler * bits of the S banked view. We don't need to move the bypass 71132951860SFabian Aggeler * control bits because we don't implement that (IMPDEF) part 71232951860SFabian Aggeler * of the GIC architecture. 71332951860SFabian Aggeler */ 71432951860SFabian Aggeler ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1; 71532951860SFabian Aggeler } 71632951860SFabian Aggeler return ret; 71732951860SFabian Aggeler } 71832951860SFabian Aggeler 71932951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value, 72032951860SFabian Aggeler MemTxAttrs attrs) 72132951860SFabian Aggeler { 72232951860SFabian Aggeler uint32_t mask; 72332951860SFabian Aggeler 7243dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 72532951860SFabian Aggeler /* The NS view can only write certain bits in the register; 72632951860SFabian Aggeler * the rest are unchanged 72732951860SFabian Aggeler */ 72832951860SFabian Aggeler mask = GICC_CTLR_EN_GRP1; 72932951860SFabian Aggeler if (s->revision == 2) { 73032951860SFabian Aggeler mask |= GICC_CTLR_EOIMODE_NS; 73132951860SFabian Aggeler } 73232951860SFabian Aggeler s->cpu_ctlr[cpu] &= ~mask; 73332951860SFabian Aggeler s->cpu_ctlr[cpu] |= (value << 1) & mask; 73432951860SFabian Aggeler } else { 73532951860SFabian Aggeler if (s->revision == 2) { 73632951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK; 73732951860SFabian Aggeler } else { 73832951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK; 73932951860SFabian Aggeler } 74032951860SFabian Aggeler s->cpu_ctlr[cpu] = value & mask; 74132951860SFabian Aggeler } 74232951860SFabian Aggeler DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, " 74332951860SFabian Aggeler "Group1 Interrupts %sabled\n", cpu, 74432951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis", 74532951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis"); 74632951860SFabian Aggeler } 74732951860SFabian Aggeler 74808efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs) 74908efa9f2SFabian Aggeler { 75071aa735bSLuc MICHEL if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) { 75171aa735bSLuc MICHEL /* Idle priority */ 75271aa735bSLuc MICHEL return 0xff; 75371aa735bSLuc MICHEL } 75471aa735bSLuc MICHEL 7553dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 75608efa9f2SFabian Aggeler if (s->running_priority[cpu] & 0x80) { 75708efa9f2SFabian Aggeler /* Running priority in upper half of range: return the Non-secure 75808efa9f2SFabian Aggeler * view of the priority. 75908efa9f2SFabian Aggeler */ 76008efa9f2SFabian Aggeler return s->running_priority[cpu] << 1; 76108efa9f2SFabian Aggeler } else { 76208efa9f2SFabian Aggeler /* Running priority in lower half of range: RAZ */ 76308efa9f2SFabian Aggeler return 0; 76408efa9f2SFabian Aggeler } 76508efa9f2SFabian Aggeler } else { 76608efa9f2SFabian Aggeler return s->running_priority[cpu]; 76708efa9f2SFabian Aggeler } 76808efa9f2SFabian Aggeler } 76908efa9f2SFabian Aggeler 770a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation, 771a55c910eSPeter Maydell * ie whether the relevant EOIMode bit is set. 772a55c910eSPeter Maydell */ 773a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs) 774a55c910eSPeter Maydell { 775a55c910eSPeter Maydell if (s->revision != 2) { 776a55c910eSPeter Maydell /* Before GICv2 prio-drop and deactivate are not separable */ 777a55c910eSPeter Maydell return false; 778a55c910eSPeter Maydell } 7793dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 780a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS; 781a55c910eSPeter Maydell } 782a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE; 783a55c910eSPeter Maydell } 784a55c910eSPeter Maydell 785a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 786a55c910eSPeter Maydell { 787ee03cca8SPeter Maydell int group; 788ee03cca8SPeter Maydell 78902f2e22dSLuc Michel if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) { 790ee03cca8SPeter Maydell /* 791ee03cca8SPeter Maydell * This handles two cases: 792ee03cca8SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 793ee03cca8SPeter Maydell * to the GICC_DIR, the GIC ignores that write. 794ee03cca8SPeter Maydell * 2. If software writes the number of a non-existent interrupt 795ee03cca8SPeter Maydell * this must be a subcase of "value written is not an active interrupt" 79602f2e22dSLuc Michel * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs, 79702f2e22dSLuc Michel * all IRQs potentially exist, so this limit does not apply. 798ee03cca8SPeter Maydell */ 799ee03cca8SPeter Maydell return; 800ee03cca8SPeter Maydell } 801ee03cca8SPeter Maydell 802a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 803a55c910eSPeter Maydell /* This is UNPREDICTABLE; we choose to ignore it */ 804a55c910eSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 805a55c910eSPeter Maydell "gic_deactivate_irq: GICC_DIR write when EOIMode clear"); 806a55c910eSPeter Maydell return; 807a55c910eSPeter Maydell } 808a55c910eSPeter Maydell 80902f2e22dSLuc Michel if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) { 81002f2e22dSLuc Michel /* This vIRQ does not have an LR entry which is either active or 81102f2e22dSLuc Michel * pending and active. Increment EOICount and ignore the write. 81202f2e22dSLuc Michel */ 81302f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 81402f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 815cbe1282bSLuc Michel 816cbe1282bSLuc Michel /* Update the virtual interface in case a maintenance interrupt should 817cbe1282bSLuc Michel * be raised. 818cbe1282bSLuc Michel */ 819cbe1282bSLuc Michel gic_update_virt(s); 82002f2e22dSLuc Michel return; 82102f2e22dSLuc Michel } 82202f2e22dSLuc Michel 82302f2e22dSLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 82402f2e22dSLuc Michel 8253dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 826a55c910eSPeter Maydell DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq); 827a55c910eSPeter Maydell return; 828a55c910eSPeter Maydell } 829a55c910eSPeter Maydell 83086b350f0SLuc Michel gic_clear_active(s, irq, cpu); 831a55c910eSPeter Maydell } 832a55c910eSPeter Maydell 83350491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 834e69954b9Spbrook { 8359ee6e8bbSpbrook int cm = 1 << cpu; 83672889c8aSPeter Maydell int group; 83772889c8aSPeter Maydell 838df628ff1Spbrook DPRINTF("EOI %d\n", irq); 83902f2e22dSLuc Michel if (gic_is_vcpu(cpu)) { 84002f2e22dSLuc Michel /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the 84102f2e22dSLuc Michel * running prio is < 0x100. 84202f2e22dSLuc Michel */ 84302f2e22dSLuc Michel bool prio_drop = s->running_priority[cpu] < 0x100; 84402f2e22dSLuc Michel 84502f2e22dSLuc Michel if (irq >= GIC_MAXIRQ) { 84602f2e22dSLuc Michel /* Ignore spurious interrupt */ 84702f2e22dSLuc Michel return; 84802f2e22dSLuc Michel } 84902f2e22dSLuc Michel 85002f2e22dSLuc Michel gic_drop_prio(s, cpu, 0); 85102f2e22dSLuc Michel 85202f2e22dSLuc Michel if (!gic_eoi_split(s, cpu, attrs)) { 85302f2e22dSLuc Michel bool valid = gic_virq_is_valid(s, irq, cpu); 85402f2e22dSLuc Michel if (prio_drop && !valid) { 85502f2e22dSLuc Michel /* We are in a situation where: 85602f2e22dSLuc Michel * - V_CTRL.EOIMode is false (no EOI split), 85702f2e22dSLuc Michel * - The call to gic_drop_prio() cleared a bit in GICH_APR, 85802f2e22dSLuc Michel * - This vIRQ does not have an LR entry which is either 85902f2e22dSLuc Michel * active or pending and active. 86002f2e22dSLuc Michel * In that case, we must increment EOICount. 86102f2e22dSLuc Michel */ 86202f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 86302f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 86402f2e22dSLuc Michel } else if (valid) { 86502f2e22dSLuc Michel gic_clear_active(s, irq, cpu); 86602f2e22dSLuc Michel } 86702f2e22dSLuc Michel } 86802f2e22dSLuc Michel 869cbe1282bSLuc Michel gic_update_virt(s); 87002f2e22dSLuc Michel return; 87102f2e22dSLuc Michel } 87202f2e22dSLuc Michel 873a32134aaSMark Langsdorf if (irq >= s->num_irq) { 874217bfb44SPeter Maydell /* This handles two cases: 875217bfb44SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 876217bfb44SPeter Maydell * to the GICC_EOIR, the GIC ignores that write. 877217bfb44SPeter Maydell * 2. If software writes the number of a non-existent interrupt 878217bfb44SPeter Maydell * this must be a subcase of "value written does not match the last 879217bfb44SPeter Maydell * valid interrupt value read from the Interrupt Acknowledge 880217bfb44SPeter Maydell * register" and so this is UNPREDICTABLE. We choose to ignore it. 881217bfb44SPeter Maydell */ 882217bfb44SPeter Maydell return; 883217bfb44SPeter Maydell } 88472889c8aSPeter Maydell if (s->running_priority[cpu] == 0x100) { 885e69954b9Spbrook return; /* No active IRQ. */ 88672889c8aSPeter Maydell } 8878d999995SChristoffer Dall 8883bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 889e69954b9Spbrook /* Mark level triggered interrupts as pending if they are still 890e69954b9Spbrook raised. */ 89167ce697aSLuc Michel if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm) 89267ce697aSLuc Michel && GIC_DIST_TEST_LEVEL(irq, cm) 89367ce697aSLuc Michel && (GIC_DIST_TARGET(irq) & cm) != 0) { 8949ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq, cm); 89567ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, cm); 896e69954b9Spbrook } 8978d999995SChristoffer Dall } 8988d999995SChristoffer Dall 89986b350f0SLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 90072889c8aSPeter Maydell 9013dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 902f9c6a7f1SFabian Aggeler DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq); 903f9c6a7f1SFabian Aggeler return; 904f9c6a7f1SFabian Aggeler } 905f9c6a7f1SFabian Aggeler 906f9c6a7f1SFabian Aggeler /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1 907f9c6a7f1SFabian Aggeler * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1, 908f9c6a7f1SFabian Aggeler * i.e. go ahead and complete the irq anyway. 909f9c6a7f1SFabian Aggeler */ 910f9c6a7f1SFabian Aggeler 91172889c8aSPeter Maydell gic_drop_prio(s, cpu, group); 912a55c910eSPeter Maydell 913a55c910eSPeter Maydell /* In GICv2 the guest can choose to split priority-drop and deactivate */ 914a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 91586b350f0SLuc Michel gic_clear_active(s, irq, cpu); 916a55c910eSPeter Maydell } 917e69954b9Spbrook gic_update(s); 918e69954b9Spbrook } 919e69954b9Spbrook 920a9d85353SPeter Maydell static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs) 921e69954b9Spbrook { 922fae15286SPeter Maydell GICState *s = (GICState *)opaque; 923e69954b9Spbrook uint32_t res; 924e69954b9Spbrook int irq; 925e69954b9Spbrook int i; 9269ee6e8bbSpbrook int cpu; 9279ee6e8bbSpbrook int cm; 9289ee6e8bbSpbrook int mask; 929e69954b9Spbrook 930926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 9319ee6e8bbSpbrook cm = 1 << cpu; 932e69954b9Spbrook if (offset < 0x100) { 933679aa175SFabian Aggeler if (offset == 0) { /* GICD_CTLR */ 934679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 935679aa175SFabian Aggeler /* The NS bank of this register is just an alias of the 936679aa175SFabian Aggeler * EnableGrp1 bit in the S bank version. 937679aa175SFabian Aggeler */ 938679aa175SFabian Aggeler return extract32(s->ctlr, 1, 1); 939679aa175SFabian Aggeler } else { 940679aa175SFabian Aggeler return s->ctlr; 941679aa175SFabian Aggeler } 942679aa175SFabian Aggeler } 943e69954b9Spbrook if (offset == 4) 9445543d1abSFabian Aggeler /* Interrupt Controller Type Register */ 9455543d1abSFabian Aggeler return ((s->num_irq / 32) - 1) 946b95690c9SWei Huang | ((s->num_cpu - 1) << 5) 9475543d1abSFabian Aggeler | (s->security_extn << 10); 948e69954b9Spbrook if (offset < 0x08) 949e69954b9Spbrook return 0; 950b79f2265SRob Herring if (offset >= 0x80) { 951c27a5ba9SFabian Aggeler /* Interrupt Group Registers: these RAZ/WI if this is an NS 952c27a5ba9SFabian Aggeler * access to a GIC with the security extensions, or if the GIC 953c27a5ba9SFabian Aggeler * doesn't have groups at all. 954c27a5ba9SFabian Aggeler */ 955c27a5ba9SFabian Aggeler res = 0; 956c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 957c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 958c27a5ba9SFabian Aggeler irq = (offset - 0x080) * 8 + GIC_BASE_IRQ; 959c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 960c27a5ba9SFabian Aggeler goto bad_reg; 961c27a5ba9SFabian Aggeler } 962c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 96367ce697aSLuc Michel if (GIC_DIST_TEST_GROUP(irq + i, cm)) { 964c27a5ba9SFabian Aggeler res |= (1 << i); 965c27a5ba9SFabian Aggeler } 966c27a5ba9SFabian Aggeler } 967c27a5ba9SFabian Aggeler } 968c27a5ba9SFabian Aggeler return res; 969b79f2265SRob Herring } 970e69954b9Spbrook goto bad_reg; 971e69954b9Spbrook } else if (offset < 0x200) { 972e69954b9Spbrook /* Interrupt Set/Clear Enable. */ 973e69954b9Spbrook if (offset < 0x180) 974e69954b9Spbrook irq = (offset - 0x100) * 8; 975e69954b9Spbrook else 976e69954b9Spbrook irq = (offset - 0x180) * 8; 9779ee6e8bbSpbrook irq += GIC_BASE_IRQ; 978a32134aaSMark Langsdorf if (irq >= s->num_irq) 979e69954b9Spbrook goto bad_reg; 980e69954b9Spbrook res = 0; 981e69954b9Spbrook for (i = 0; i < 8; i++) { 982fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 98367ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 984fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 985fea8a08eSJens Wiklander } 986fea8a08eSJens Wiklander 98767ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 988e69954b9Spbrook res |= (1 << i); 989e69954b9Spbrook } 990e69954b9Spbrook } 991e69954b9Spbrook } else if (offset < 0x300) { 992e69954b9Spbrook /* Interrupt Set/Clear Pending. */ 993e69954b9Spbrook if (offset < 0x280) 994e69954b9Spbrook irq = (offset - 0x200) * 8; 995e69954b9Spbrook else 996e69954b9Spbrook irq = (offset - 0x280) * 8; 9979ee6e8bbSpbrook irq += GIC_BASE_IRQ; 998a32134aaSMark Langsdorf if (irq >= s->num_irq) 999e69954b9Spbrook goto bad_reg; 1000e69954b9Spbrook res = 0; 100169253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 1002e69954b9Spbrook for (i = 0; i < 8; i++) { 1003fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 100467ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1005fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1006fea8a08eSJens Wiklander } 1007fea8a08eSJens Wiklander 10088d999995SChristoffer Dall if (gic_test_pending(s, irq + i, mask)) { 1009e69954b9Spbrook res |= (1 << i); 1010e69954b9Spbrook } 1011e69954b9Spbrook } 1012e69954b9Spbrook } else if (offset < 0x400) { 10133bb0b038SLuc Michel /* Interrupt Set/Clear Active. */ 10143bb0b038SLuc Michel if (offset < 0x380) { 10153bb0b038SLuc Michel irq = (offset - 0x300) * 8; 10163bb0b038SLuc Michel } else if (s->revision == 2) { 10173bb0b038SLuc Michel irq = (offset - 0x380) * 8; 10183bb0b038SLuc Michel } else { 10193bb0b038SLuc Michel goto bad_reg; 10203bb0b038SLuc Michel } 10213bb0b038SLuc Michel 10223bb0b038SLuc Michel irq += GIC_BASE_IRQ; 1023a32134aaSMark Langsdorf if (irq >= s->num_irq) 1024e69954b9Spbrook goto bad_reg; 1025e69954b9Spbrook res = 0; 102669253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 1027e69954b9Spbrook for (i = 0; i < 8; i++) { 1028fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 102967ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1030fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1031fea8a08eSJens Wiklander } 1032fea8a08eSJens Wiklander 103367ce697aSLuc Michel if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) { 1034e69954b9Spbrook res |= (1 << i); 1035e69954b9Spbrook } 1036e69954b9Spbrook } 1037e69954b9Spbrook } else if (offset < 0x800) { 1038e69954b9Spbrook /* Interrupt Priority. */ 10399ee6e8bbSpbrook irq = (offset - 0x400) + GIC_BASE_IRQ; 1040a32134aaSMark Langsdorf if (irq >= s->num_irq) 1041e69954b9Spbrook goto bad_reg; 104267ce697aSLuc Michel res = gic_dist_get_priority(s, cpu, irq, attrs); 1043e69954b9Spbrook } else if (offset < 0xc00) { 1044e69954b9Spbrook /* Interrupt CPU Target. */ 10456b9680bbSPeter Maydell if (s->num_cpu == 1 && s->revision != REV_11MPCORE) { 10466b9680bbSPeter Maydell /* For uniprocessor GICs these RAZ/WI */ 10476b9680bbSPeter Maydell res = 0; 10486b9680bbSPeter Maydell } else { 10499ee6e8bbSpbrook irq = (offset - 0x800) + GIC_BASE_IRQ; 10506b9680bbSPeter Maydell if (irq >= s->num_irq) { 1051e69954b9Spbrook goto bad_reg; 10526b9680bbSPeter Maydell } 10537995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 10547995206dSPeter Maydell res = 0; 10557995206dSPeter Maydell } else if (irq < GIC_INTERNAL) { 10569ee6e8bbSpbrook res = cm; 10579ee6e8bbSpbrook } else { 105867ce697aSLuc Michel res = GIC_DIST_TARGET(irq); 10599ee6e8bbSpbrook } 10606b9680bbSPeter Maydell } 1061e69954b9Spbrook } else if (offset < 0xf00) { 1062e69954b9Spbrook /* Interrupt Configuration. */ 106371a62046SAdam Lackorzynski irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ; 1064a32134aaSMark Langsdorf if (irq >= s->num_irq) 1065e69954b9Spbrook goto bad_reg; 1066e69954b9Spbrook res = 0; 1067e69954b9Spbrook for (i = 0; i < 4; i++) { 1068fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 106967ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1070fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1071fea8a08eSJens Wiklander } 1072fea8a08eSJens Wiklander 107367ce697aSLuc Michel if (GIC_DIST_TEST_MODEL(irq + i)) { 1074e69954b9Spbrook res |= (1 << (i * 2)); 107567ce697aSLuc Michel } 107667ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 1077e69954b9Spbrook res |= (2 << (i * 2)); 1078e69954b9Spbrook } 107967ce697aSLuc Michel } 108040d22500SChristoffer Dall } else if (offset < 0xf10) { 108140d22500SChristoffer Dall goto bad_reg; 108240d22500SChristoffer Dall } else if (offset < 0xf30) { 10837c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 108440d22500SChristoffer Dall goto bad_reg; 108540d22500SChristoffer Dall } 108640d22500SChristoffer Dall 108740d22500SChristoffer Dall if (offset < 0xf20) { 108840d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 108940d22500SChristoffer Dall irq = (offset - 0xf10); 109040d22500SChristoffer Dall } else { 109140d22500SChristoffer Dall irq = (offset - 0xf20); 109240d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 109340d22500SChristoffer Dall } 109440d22500SChristoffer Dall 1095fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 109667ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 1097fea8a08eSJens Wiklander res = 0; /* Ignore Non-secure access of Group0 IRQ */ 1098fea8a08eSJens Wiklander } else { 109940d22500SChristoffer Dall res = s->sgi_pending[irq][cpu]; 1100fea8a08eSJens Wiklander } 11013355c360SAlistair Francis } else if (offset < 0xfd0) { 1102e69954b9Spbrook goto bad_reg; 11033355c360SAlistair Francis } else if (offset < 0x1000) { 1104e69954b9Spbrook if (offset & 3) { 1105e69954b9Spbrook res = 0; 1106e69954b9Spbrook } else { 11073355c360SAlistair Francis switch (s->revision) { 11083355c360SAlistair Francis case REV_11MPCORE: 11093355c360SAlistair Francis res = gic_id_11mpcore[(offset - 0xfd0) >> 2]; 11103355c360SAlistair Francis break; 11113355c360SAlistair Francis case 1: 11123355c360SAlistair Francis res = gic_id_gicv1[(offset - 0xfd0) >> 2]; 11133355c360SAlistair Francis break; 11143355c360SAlistair Francis case 2: 11153355c360SAlistair Francis res = gic_id_gicv2[(offset - 0xfd0) >> 2]; 11163355c360SAlistair Francis break; 11173355c360SAlistair Francis default: 11183355c360SAlistair Francis res = 0; 1119e69954b9Spbrook } 1120e69954b9Spbrook } 11213355c360SAlistair Francis } else { 11223355c360SAlistair Francis g_assert_not_reached(); 11233355c360SAlistair Francis } 1124e69954b9Spbrook return res; 1125e69954b9Spbrook bad_reg: 11268c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 11278c8dc39fSPeter Maydell "gic_dist_readb: Bad offset %x\n", (int)offset); 1128e69954b9Spbrook return 0; 1129e69954b9Spbrook } 1130e69954b9Spbrook 1131a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data, 1132a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1133e69954b9Spbrook { 1134a9d85353SPeter Maydell switch (size) { 1135a9d85353SPeter Maydell case 1: 1136a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1137*067a2b9cSLuc Michel break; 1138a9d85353SPeter Maydell case 2: 1139a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1140a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1141*067a2b9cSLuc Michel break; 1142a9d85353SPeter Maydell case 4: 1143a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1144a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1145a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16; 1146a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24; 1147*067a2b9cSLuc Michel break; 1148a9d85353SPeter Maydell default: 1149a9d85353SPeter Maydell return MEMTX_ERROR; 1150e69954b9Spbrook } 1151*067a2b9cSLuc Michel 1152*067a2b9cSLuc Michel trace_gic_dist_read(offset, size, *data); 1153*067a2b9cSLuc Michel return MEMTX_OK; 1154e69954b9Spbrook } 1155e69954b9Spbrook 1156a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset, 1157a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1158e69954b9Spbrook { 1159fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1160e69954b9Spbrook int irq; 1161e69954b9Spbrook int i; 11629ee6e8bbSpbrook int cpu; 1163e69954b9Spbrook 1164926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 1165e69954b9Spbrook if (offset < 0x100) { 1166e69954b9Spbrook if (offset == 0) { 1167679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 1168679aa175SFabian Aggeler /* NS version is just an alias of the S version's bit 1 */ 1169679aa175SFabian Aggeler s->ctlr = deposit32(s->ctlr, 1, 1, value); 1170679aa175SFabian Aggeler } else if (gic_has_groups(s)) { 1171679aa175SFabian Aggeler s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1); 1172679aa175SFabian Aggeler } else { 1173679aa175SFabian Aggeler s->ctlr = value & GICD_CTLR_EN_GRP0; 1174679aa175SFabian Aggeler } 1175679aa175SFabian Aggeler DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n", 1176679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis", 1177679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis"); 1178e69954b9Spbrook } else if (offset < 4) { 1179e69954b9Spbrook /* ignored. */ 1180b79f2265SRob Herring } else if (offset >= 0x80) { 1181c27a5ba9SFabian Aggeler /* Interrupt Group Registers: RAZ/WI for NS access to secure 1182c27a5ba9SFabian Aggeler * GIC, or for GICs without groups. 1183c27a5ba9SFabian Aggeler */ 1184c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 1185c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 1186c27a5ba9SFabian Aggeler irq = (offset - 0x80) * 8 + GIC_BASE_IRQ; 1187c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 1188c27a5ba9SFabian Aggeler goto bad_reg; 1189c27a5ba9SFabian Aggeler } 1190c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 1191c27a5ba9SFabian Aggeler /* Group bits are banked for private interrupts */ 1192c27a5ba9SFabian Aggeler int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 1193c27a5ba9SFabian Aggeler if (value & (1 << i)) { 1194c27a5ba9SFabian Aggeler /* Group1 (Non-secure) */ 119567ce697aSLuc Michel GIC_DIST_SET_GROUP(irq + i, cm); 1196c27a5ba9SFabian Aggeler } else { 1197c27a5ba9SFabian Aggeler /* Group0 (Secure) */ 119867ce697aSLuc Michel GIC_DIST_CLEAR_GROUP(irq + i, cm); 1199c27a5ba9SFabian Aggeler } 1200c27a5ba9SFabian Aggeler } 1201c27a5ba9SFabian Aggeler } 1202e69954b9Spbrook } else { 1203e69954b9Spbrook goto bad_reg; 1204e69954b9Spbrook } 1205e69954b9Spbrook } else if (offset < 0x180) { 1206e69954b9Spbrook /* Interrupt Set Enable. */ 12079ee6e8bbSpbrook irq = (offset - 0x100) * 8 + GIC_BASE_IRQ; 1208a32134aaSMark Langsdorf if (irq >= s->num_irq) 1209e69954b9Spbrook goto bad_reg; 121041ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12119ee6e8bbSpbrook value = 0xff; 121241ab7b55SChristoffer Dall } 121341ab7b55SChristoffer Dall 1214e69954b9Spbrook for (i = 0; i < 8; i++) { 1215e69954b9Spbrook if (value & (1 << i)) { 1216f47b48fbSDaniel Sangorrin int mask = 121767ce697aSLuc Michel (irq < GIC_INTERNAL) ? (1 << cpu) 121867ce697aSLuc Michel : GIC_DIST_TARGET(irq + i); 121969253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 122041bf234dSRabin Vincent 1221fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 122267ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1223fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1224fea8a08eSJens Wiklander } 1225fea8a08eSJens Wiklander 122667ce697aSLuc Michel if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1227e69954b9Spbrook DPRINTF("Enabled IRQ %d\n", irq + i); 12282531088fSHollis Blanchard trace_gic_enable_irq(irq + i); 122941bf234dSRabin Vincent } 123067ce697aSLuc Michel GIC_DIST_SET_ENABLED(irq + i, cm); 1231e69954b9Spbrook /* If a raised level triggered IRQ enabled then mark 1232e69954b9Spbrook is as pending. */ 123367ce697aSLuc Michel if (GIC_DIST_TEST_LEVEL(irq + i, mask) 123467ce697aSLuc Michel && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 12359ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq + i, mask); 123667ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, mask); 12379ee6e8bbSpbrook } 1238e69954b9Spbrook } 1239e69954b9Spbrook } 1240e69954b9Spbrook } else if (offset < 0x200) { 1241e69954b9Spbrook /* Interrupt Clear Enable. */ 12429ee6e8bbSpbrook irq = (offset - 0x180) * 8 + GIC_BASE_IRQ; 1243a32134aaSMark Langsdorf if (irq >= s->num_irq) 1244e69954b9Spbrook goto bad_reg; 124541ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12469ee6e8bbSpbrook value = 0; 124741ab7b55SChristoffer Dall } 124841ab7b55SChristoffer Dall 1249e69954b9Spbrook for (i = 0; i < 8; i++) { 1250e69954b9Spbrook if (value & (1 << i)) { 125169253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 125241bf234dSRabin Vincent 1253fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 125467ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1255fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1256fea8a08eSJens Wiklander } 1257fea8a08eSJens Wiklander 125867ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1259e69954b9Spbrook DPRINTF("Disabled IRQ %d\n", irq + i); 12602531088fSHollis Blanchard trace_gic_disable_irq(irq + i); 126141bf234dSRabin Vincent } 126267ce697aSLuc Michel GIC_DIST_CLEAR_ENABLED(irq + i, cm); 1263e69954b9Spbrook } 1264e69954b9Spbrook } 1265e69954b9Spbrook } else if (offset < 0x280) { 1266e69954b9Spbrook /* Interrupt Set Pending. */ 12679ee6e8bbSpbrook irq = (offset - 0x200) * 8 + GIC_BASE_IRQ; 1268a32134aaSMark Langsdorf if (irq >= s->num_irq) 1269e69954b9Spbrook goto bad_reg; 127041ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12715b0adce1SChristoffer Dall value = 0; 127241ab7b55SChristoffer Dall } 12739ee6e8bbSpbrook 1274e69954b9Spbrook for (i = 0; i < 8; i++) { 1275e69954b9Spbrook if (value & (1 << i)) { 1276fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 127767ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1278fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1279fea8a08eSJens Wiklander } 1280fea8a08eSJens Wiklander 128167ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i)); 1282e69954b9Spbrook } 1283e69954b9Spbrook } 1284e69954b9Spbrook } else if (offset < 0x300) { 1285e69954b9Spbrook /* Interrupt Clear Pending. */ 12869ee6e8bbSpbrook irq = (offset - 0x280) * 8 + GIC_BASE_IRQ; 1287a32134aaSMark Langsdorf if (irq >= s->num_irq) 1288e69954b9Spbrook goto bad_reg; 12895b0adce1SChristoffer Dall if (irq < GIC_NR_SGIS) { 12905b0adce1SChristoffer Dall value = 0; 12915b0adce1SChristoffer Dall } 12925b0adce1SChristoffer Dall 1293e69954b9Spbrook for (i = 0; i < 8; i++) { 1294fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 129567ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1296fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1297fea8a08eSJens Wiklander } 1298fea8a08eSJens Wiklander 12999ee6e8bbSpbrook /* ??? This currently clears the pending bit for all CPUs, even 13009ee6e8bbSpbrook for per-CPU interrupts. It's unclear whether this is the 13019ee6e8bbSpbrook corect behavior. */ 1302e69954b9Spbrook if (value & (1 << i)) { 130367ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK); 1304e69954b9Spbrook } 1305e69954b9Spbrook } 13063bb0b038SLuc Michel } else if (offset < 0x380) { 13073bb0b038SLuc Michel /* Interrupt Set Active. */ 13083bb0b038SLuc Michel if (s->revision != 2) { 1309e69954b9Spbrook goto bad_reg; 13103bb0b038SLuc Michel } 13113bb0b038SLuc Michel 13123bb0b038SLuc Michel irq = (offset - 0x300) * 8 + GIC_BASE_IRQ; 13133bb0b038SLuc Michel if (irq >= s->num_irq) { 13143bb0b038SLuc Michel goto bad_reg; 13153bb0b038SLuc Michel } 13163bb0b038SLuc Michel 13173bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 13183bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 13193bb0b038SLuc Michel 13203bb0b038SLuc Michel for (i = 0; i < 8; i++) { 13213bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 13223bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 13233bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 13243bb0b038SLuc Michel } 13253bb0b038SLuc Michel 13263bb0b038SLuc Michel if (value & (1 << i)) { 13273bb0b038SLuc Michel GIC_DIST_SET_ACTIVE(irq + i, cm); 13283bb0b038SLuc Michel } 13293bb0b038SLuc Michel } 13303bb0b038SLuc Michel } else if (offset < 0x400) { 13313bb0b038SLuc Michel /* Interrupt Clear Active. */ 13323bb0b038SLuc Michel if (s->revision != 2) { 13333bb0b038SLuc Michel goto bad_reg; 13343bb0b038SLuc Michel } 13353bb0b038SLuc Michel 13363bb0b038SLuc Michel irq = (offset - 0x380) * 8 + GIC_BASE_IRQ; 13373bb0b038SLuc Michel if (irq >= s->num_irq) { 13383bb0b038SLuc Michel goto bad_reg; 13393bb0b038SLuc Michel } 13403bb0b038SLuc Michel 13413bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 13423bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 13433bb0b038SLuc Michel 13443bb0b038SLuc Michel for (i = 0; i < 8; i++) { 13453bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 13463bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 13473bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 13483bb0b038SLuc Michel } 13493bb0b038SLuc Michel 13503bb0b038SLuc Michel if (value & (1 << i)) { 13513bb0b038SLuc Michel GIC_DIST_CLEAR_ACTIVE(irq + i, cm); 13523bb0b038SLuc Michel } 13533bb0b038SLuc Michel } 1354e69954b9Spbrook } else if (offset < 0x800) { 1355e69954b9Spbrook /* Interrupt Priority. */ 13569ee6e8bbSpbrook irq = (offset - 0x400) + GIC_BASE_IRQ; 1357a32134aaSMark Langsdorf if (irq >= s->num_irq) 1358e69954b9Spbrook goto bad_reg; 135967ce697aSLuc Michel gic_dist_set_priority(s, cpu, irq, value, attrs); 1360e69954b9Spbrook } else if (offset < 0xc00) { 13616b9680bbSPeter Maydell /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the 13626b9680bbSPeter Maydell * annoying exception of the 11MPCore's GIC. 13636b9680bbSPeter Maydell */ 13646b9680bbSPeter Maydell if (s->num_cpu != 1 || s->revision == REV_11MPCORE) { 13659ee6e8bbSpbrook irq = (offset - 0x800) + GIC_BASE_IRQ; 13666b9680bbSPeter Maydell if (irq >= s->num_irq) { 1367e69954b9Spbrook goto bad_reg; 13686b9680bbSPeter Maydell } 13697995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 13709ee6e8bbSpbrook value = 0; 13716b9680bbSPeter Maydell } else if (irq < GIC_INTERNAL) { 13729ee6e8bbSpbrook value = ALL_CPU_MASK; 13736b9680bbSPeter Maydell } 13749ee6e8bbSpbrook s->irq_target[irq] = value & ALL_CPU_MASK; 13756b9680bbSPeter Maydell } 1376e69954b9Spbrook } else if (offset < 0xf00) { 1377e69954b9Spbrook /* Interrupt Configuration. */ 13789ee6e8bbSpbrook irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ; 1379a32134aaSMark Langsdorf if (irq >= s->num_irq) 1380e69954b9Spbrook goto bad_reg; 1381de7a900fSAdam Lackorzynski if (irq < GIC_NR_SGIS) 13829ee6e8bbSpbrook value |= 0xaa; 1383e69954b9Spbrook for (i = 0; i < 4; i++) { 1384fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 138567ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1386fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1387fea8a08eSJens Wiklander } 1388fea8a08eSJens Wiklander 13897c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 1390e69954b9Spbrook if (value & (1 << (i * 2))) { 139167ce697aSLuc Michel GIC_DIST_SET_MODEL(irq + i); 1392e69954b9Spbrook } else { 139367ce697aSLuc Michel GIC_DIST_CLEAR_MODEL(irq + i); 1394e69954b9Spbrook } 139524b790dfSAdam Lackorzynski } 1396e69954b9Spbrook if (value & (2 << (i * 2))) { 139767ce697aSLuc Michel GIC_DIST_SET_EDGE_TRIGGER(irq + i); 1398e69954b9Spbrook } else { 139967ce697aSLuc Michel GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i); 1400e69954b9Spbrook } 1401e69954b9Spbrook } 140240d22500SChristoffer Dall } else if (offset < 0xf10) { 14039ee6e8bbSpbrook /* 0xf00 is only handled for 32-bit writes. */ 1404e69954b9Spbrook goto bad_reg; 140540d22500SChristoffer Dall } else if (offset < 0xf20) { 140640d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 14077c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 140840d22500SChristoffer Dall goto bad_reg; 140940d22500SChristoffer Dall } 141040d22500SChristoffer Dall irq = (offset - 0xf10); 141140d22500SChristoffer Dall 1412fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 141367ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 141440d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~value; 141540d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 141667ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq, 1 << cpu); 141740d22500SChristoffer Dall } 1418fea8a08eSJens Wiklander } 141940d22500SChristoffer Dall } else if (offset < 0xf30) { 142040d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 14217c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 142240d22500SChristoffer Dall goto bad_reg; 142340d22500SChristoffer Dall } 142440d22500SChristoffer Dall irq = (offset - 0xf20); 142540d22500SChristoffer Dall 1426fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 142767ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 142867ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, 1 << cpu); 142940d22500SChristoffer Dall s->sgi_pending[irq][cpu] |= value; 1430fea8a08eSJens Wiklander } 143140d22500SChristoffer Dall } else { 143240d22500SChristoffer Dall goto bad_reg; 1433e69954b9Spbrook } 1434e69954b9Spbrook gic_update(s); 1435e69954b9Spbrook return; 1436e69954b9Spbrook bad_reg: 14378c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 14388c8dc39fSPeter Maydell "gic_dist_writeb: Bad offset %x\n", (int)offset); 1439e69954b9Spbrook } 1440e69954b9Spbrook 1441a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset, 1442a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1443e69954b9Spbrook { 1444a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, value & 0xff, attrs); 1445a9d85353SPeter Maydell gic_dist_writeb(opaque, offset + 1, value >> 8, attrs); 1446e69954b9Spbrook } 1447e69954b9Spbrook 1448a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset, 1449a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1450e69954b9Spbrook { 1451fae15286SPeter Maydell GICState *s = (GICState *)opaque; 14528da3ff18Spbrook if (offset == 0xf00) { 14539ee6e8bbSpbrook int cpu; 14549ee6e8bbSpbrook int irq; 14559ee6e8bbSpbrook int mask; 145640d22500SChristoffer Dall int target_cpu; 14579ee6e8bbSpbrook 1458926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 14599ee6e8bbSpbrook irq = value & 0x3ff; 14609ee6e8bbSpbrook switch ((value >> 24) & 3) { 14619ee6e8bbSpbrook case 0: 14629ee6e8bbSpbrook mask = (value >> 16) & ALL_CPU_MASK; 14639ee6e8bbSpbrook break; 14649ee6e8bbSpbrook case 1: 1465fa250144SAdam Lackorzynski mask = ALL_CPU_MASK ^ (1 << cpu); 14669ee6e8bbSpbrook break; 14679ee6e8bbSpbrook case 2: 1468fa250144SAdam Lackorzynski mask = 1 << cpu; 14699ee6e8bbSpbrook break; 14709ee6e8bbSpbrook default: 14719ee6e8bbSpbrook DPRINTF("Bad Soft Int target filter\n"); 14729ee6e8bbSpbrook mask = ALL_CPU_MASK; 14739ee6e8bbSpbrook break; 14749ee6e8bbSpbrook } 147567ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, mask); 147640d22500SChristoffer Dall target_cpu = ctz32(mask); 147740d22500SChristoffer Dall while (target_cpu < GIC_NCPU) { 147840d22500SChristoffer Dall s->sgi_pending[irq][target_cpu] |= (1 << cpu); 147940d22500SChristoffer Dall mask &= ~(1 << target_cpu); 148040d22500SChristoffer Dall target_cpu = ctz32(mask); 148140d22500SChristoffer Dall } 14829ee6e8bbSpbrook gic_update(s); 14839ee6e8bbSpbrook return; 14849ee6e8bbSpbrook } 1485a9d85353SPeter Maydell gic_dist_writew(opaque, offset, value & 0xffff, attrs); 1486a9d85353SPeter Maydell gic_dist_writew(opaque, offset + 2, value >> 16, attrs); 1487a9d85353SPeter Maydell } 1488a9d85353SPeter Maydell 1489a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data, 1490a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1491a9d85353SPeter Maydell { 1492*067a2b9cSLuc Michel trace_gic_dist_write(offset, size, data); 1493*067a2b9cSLuc Michel 1494a9d85353SPeter Maydell switch (size) { 1495a9d85353SPeter Maydell case 1: 1496a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, data, attrs); 1497a9d85353SPeter Maydell return MEMTX_OK; 1498a9d85353SPeter Maydell case 2: 1499a9d85353SPeter Maydell gic_dist_writew(opaque, offset, data, attrs); 1500a9d85353SPeter Maydell return MEMTX_OK; 1501a9d85353SPeter Maydell case 4: 1502a9d85353SPeter Maydell gic_dist_writel(opaque, offset, data, attrs); 1503a9d85353SPeter Maydell return MEMTX_OK; 1504a9d85353SPeter Maydell default: 1505a9d85353SPeter Maydell return MEMTX_ERROR; 1506a9d85353SPeter Maydell } 1507e69954b9Spbrook } 1508e69954b9Spbrook 150951fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno) 151051fd06e0SPeter Maydell { 151151fd06e0SPeter Maydell /* Return the Nonsecure view of GICC_APR<regno>. This is the 151251fd06e0SPeter Maydell * second half of GICC_NSAPR. 151351fd06e0SPeter Maydell */ 151451fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 151551fd06e0SPeter Maydell case 0: 151651fd06e0SPeter Maydell if (regno < 2) { 151751fd06e0SPeter Maydell return s->nsapr[regno + 2][cpu]; 151851fd06e0SPeter Maydell } 151951fd06e0SPeter Maydell break; 152051fd06e0SPeter Maydell case 1: 152151fd06e0SPeter Maydell if (regno == 0) { 152251fd06e0SPeter Maydell return s->nsapr[regno + 1][cpu]; 152351fd06e0SPeter Maydell } 152451fd06e0SPeter Maydell break; 152551fd06e0SPeter Maydell case 2: 152651fd06e0SPeter Maydell if (regno == 0) { 152751fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 16, 16); 152851fd06e0SPeter Maydell } 152951fd06e0SPeter Maydell break; 153051fd06e0SPeter Maydell case 3: 153151fd06e0SPeter Maydell if (regno == 0) { 153251fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 8, 8); 153351fd06e0SPeter Maydell } 153451fd06e0SPeter Maydell break; 153551fd06e0SPeter Maydell default: 153651fd06e0SPeter Maydell g_assert_not_reached(); 153751fd06e0SPeter Maydell } 153851fd06e0SPeter Maydell return 0; 153951fd06e0SPeter Maydell } 154051fd06e0SPeter Maydell 154151fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno, 154251fd06e0SPeter Maydell uint32_t value) 154351fd06e0SPeter Maydell { 154451fd06e0SPeter Maydell /* Write the Nonsecure view of GICC_APR<regno>. */ 154551fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 154651fd06e0SPeter Maydell case 0: 154751fd06e0SPeter Maydell if (regno < 2) { 154851fd06e0SPeter Maydell s->nsapr[regno + 2][cpu] = value; 154951fd06e0SPeter Maydell } 155051fd06e0SPeter Maydell break; 155151fd06e0SPeter Maydell case 1: 155251fd06e0SPeter Maydell if (regno == 0) { 155351fd06e0SPeter Maydell s->nsapr[regno + 1][cpu] = value; 155451fd06e0SPeter Maydell } 155551fd06e0SPeter Maydell break; 155651fd06e0SPeter Maydell case 2: 155751fd06e0SPeter Maydell if (regno == 0) { 155851fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value); 155951fd06e0SPeter Maydell } 156051fd06e0SPeter Maydell break; 156151fd06e0SPeter Maydell case 3: 156251fd06e0SPeter Maydell if (regno == 0) { 156351fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value); 156451fd06e0SPeter Maydell } 156551fd06e0SPeter Maydell break; 156651fd06e0SPeter Maydell default: 156751fd06e0SPeter Maydell g_assert_not_reached(); 156851fd06e0SPeter Maydell } 156951fd06e0SPeter Maydell } 157051fd06e0SPeter Maydell 1571a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, 1572a9d85353SPeter Maydell uint64_t *data, MemTxAttrs attrs) 1573e69954b9Spbrook { 1574e69954b9Spbrook switch (offset) { 1575e69954b9Spbrook case 0x00: /* Control */ 157632951860SFabian Aggeler *data = gic_get_cpu_control(s, cpu, attrs); 1577a9d85353SPeter Maydell break; 1578e69954b9Spbrook case 0x04: /* Priority mask */ 157981508470SFabian Aggeler *data = gic_get_priority_mask(s, cpu, attrs); 1580a9d85353SPeter Maydell break; 1581e69954b9Spbrook case 0x08: /* Binary Point */ 15823dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1583421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1584421a3c22SLuc MICHEL /* NS view of BPR when CBPR is 1 */ 1585421a3c22SLuc MICHEL *data = MIN(s->bpr[cpu] + 1, 7); 1586421a3c22SLuc MICHEL } else { 1587822e9cc3SFabian Aggeler /* BPR is banked. Non-secure copy stored in ABPR. */ 1588822e9cc3SFabian Aggeler *data = s->abpr[cpu]; 1589421a3c22SLuc MICHEL } 1590822e9cc3SFabian Aggeler } else { 1591a9d85353SPeter Maydell *data = s->bpr[cpu]; 1592822e9cc3SFabian Aggeler } 1593a9d85353SPeter Maydell break; 1594e69954b9Spbrook case 0x0c: /* Acknowledge */ 1595c5619bf9SFabian Aggeler *data = gic_acknowledge_irq(s, cpu, attrs); 1596a9d85353SPeter Maydell break; 159766a0a2cbSDong Xu Wang case 0x14: /* Running Priority */ 159808efa9f2SFabian Aggeler *data = gic_get_running_priority(s, cpu, attrs); 1599a9d85353SPeter Maydell break; 1600e69954b9Spbrook case 0x18: /* Highest Pending Interrupt */ 16017c0fa108SFabian Aggeler *data = gic_get_current_pending_irq(s, cpu, attrs); 1602a9d85353SPeter Maydell break; 1603aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 1604822e9cc3SFabian Aggeler /* GIC v2, no security: ABPR 1605822e9cc3SFabian Aggeler * GIC v1, no security: not implemented (RAZ/WI) 1606822e9cc3SFabian Aggeler * With security extensions, secure access: ABPR (alias of NS BPR) 1607822e9cc3SFabian Aggeler * With security extensions, nonsecure access: RAZ/WI 1608822e9cc3SFabian Aggeler */ 16093dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1610822e9cc3SFabian Aggeler *data = 0; 1611822e9cc3SFabian Aggeler } else { 1612a9d85353SPeter Maydell *data = s->abpr[cpu]; 1613822e9cc3SFabian Aggeler } 1614a9d85353SPeter Maydell break; 1615a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 161651fd06e0SPeter Maydell { 161751fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 16187eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 161951fd06e0SPeter Maydell 16207eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 162151fd06e0SPeter Maydell *data = 0; 16227eb079ecSLuc Michel } else if (gic_is_vcpu(cpu)) { 16237eb079ecSLuc Michel *data = s->h_apr[gic_get_vcpu_real_id(cpu)]; 16243dd0471bSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 162551fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 162651fd06e0SPeter Maydell *data = gic_apr_ns_view(s, regno, cpu); 162751fd06e0SPeter Maydell } else { 162851fd06e0SPeter Maydell *data = s->apr[regno][cpu]; 162951fd06e0SPeter Maydell } 1630a9d85353SPeter Maydell break; 163151fd06e0SPeter Maydell } 163251fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 163351fd06e0SPeter Maydell { 163451fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 163551fd06e0SPeter Maydell 163651fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) || 16377eb079ecSLuc Michel gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) { 163851fd06e0SPeter Maydell *data = 0; 163951fd06e0SPeter Maydell } else { 164051fd06e0SPeter Maydell *data = s->nsapr[regno][cpu]; 164151fd06e0SPeter Maydell } 164251fd06e0SPeter Maydell break; 164351fd06e0SPeter Maydell } 1644e69954b9Spbrook default: 16458c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 16468c8dc39fSPeter Maydell "gic_cpu_read: Bad offset %x\n", (int)offset); 16470cf09852SPeter Maydell *data = 0; 16480cf09852SPeter Maydell break; 1649e69954b9Spbrook } 1650*067a2b9cSLuc Michel 1651*067a2b9cSLuc Michel trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 1652*067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, *data); 1653a9d85353SPeter Maydell return MEMTX_OK; 1654e69954b9Spbrook } 1655e69954b9Spbrook 1656a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset, 1657a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1658e69954b9Spbrook { 1659*067a2b9cSLuc Michel trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 1660*067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, value); 1661*067a2b9cSLuc Michel 1662e69954b9Spbrook switch (offset) { 1663e69954b9Spbrook case 0x00: /* Control */ 166432951860SFabian Aggeler gic_set_cpu_control(s, cpu, value, attrs); 1665e69954b9Spbrook break; 1666e69954b9Spbrook case 0x04: /* Priority mask */ 166781508470SFabian Aggeler gic_set_priority_mask(s, cpu, value, attrs); 1668e69954b9Spbrook break; 1669e69954b9Spbrook case 0x08: /* Binary Point */ 16703dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1671421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1672421a3c22SLuc MICHEL /* WI when CBPR is 1 */ 1673421a3c22SLuc MICHEL return MEMTX_OK; 1674421a3c22SLuc MICHEL } else { 1675822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1676421a3c22SLuc MICHEL } 1677822e9cc3SFabian Aggeler } else { 16787eb079ecSLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 16797eb079ecSLuc Michel s->bpr[cpu] = MAX(value & 0x7, min_bpr); 1680822e9cc3SFabian Aggeler } 1681e69954b9Spbrook break; 1682e69954b9Spbrook case 0x10: /* End Of Interrupt */ 1683f9c6a7f1SFabian Aggeler gic_complete_irq(s, cpu, value & 0x3ff, attrs); 1684a9d85353SPeter Maydell return MEMTX_OK; 1685aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 16863dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1687822e9cc3SFabian Aggeler /* unimplemented, or NS access: RAZ/WI */ 1688822e9cc3SFabian Aggeler return MEMTX_OK; 1689822e9cc3SFabian Aggeler } else { 1690822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1691aa7d461aSChristoffer Dall } 1692aa7d461aSChristoffer Dall break; 1693a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 169451fd06e0SPeter Maydell { 169551fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 16967eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 169751fd06e0SPeter Maydell 16987eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 169951fd06e0SPeter Maydell return MEMTX_OK; 170051fd06e0SPeter Maydell } 17017eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 17027eb079ecSLuc Michel s->h_apr[gic_get_vcpu_real_id(cpu)] = value; 17037eb079ecSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 170451fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 170551fd06e0SPeter Maydell gic_apr_write_ns_view(s, regno, cpu, value); 170651fd06e0SPeter Maydell } else { 170751fd06e0SPeter Maydell s->apr[regno][cpu] = value; 170851fd06e0SPeter Maydell } 1709a9d477c4SChristoffer Dall break; 171051fd06e0SPeter Maydell } 171151fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 171251fd06e0SPeter Maydell { 171351fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 171451fd06e0SPeter Maydell 171551fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2) { 171651fd06e0SPeter Maydell return MEMTX_OK; 171751fd06e0SPeter Maydell } 17187eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 17197eb079ecSLuc Michel return MEMTX_OK; 17207eb079ecSLuc Michel } 17213dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 172251fd06e0SPeter Maydell return MEMTX_OK; 172351fd06e0SPeter Maydell } 172451fd06e0SPeter Maydell s->nsapr[regno][cpu] = value; 172551fd06e0SPeter Maydell break; 172651fd06e0SPeter Maydell } 1727a55c910eSPeter Maydell case 0x1000: 1728a55c910eSPeter Maydell /* GICC_DIR */ 1729a55c910eSPeter Maydell gic_deactivate_irq(s, cpu, value & 0x3ff, attrs); 1730a55c910eSPeter Maydell break; 1731e69954b9Spbrook default: 17328c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 17338c8dc39fSPeter Maydell "gic_cpu_write: Bad offset %x\n", (int)offset); 17340cf09852SPeter Maydell return MEMTX_OK; 1735e69954b9Spbrook } 1736cbe1282bSLuc Michel 1737cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 1738cbe1282bSLuc Michel gic_update_virt(s); 1739cbe1282bSLuc Michel } else { 1740e69954b9Spbrook gic_update(s); 1741cbe1282bSLuc Michel } 1742cbe1282bSLuc Michel 1743a9d85353SPeter Maydell return MEMTX_OK; 1744e69954b9Spbrook } 1745e2c56465SPeter Maydell 1746e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */ 1747a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data, 1748a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1749e2c56465SPeter Maydell { 1750fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1751a9d85353SPeter Maydell return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs); 1752e2c56465SPeter Maydell } 1753e2c56465SPeter Maydell 1754a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr, 1755a9d85353SPeter Maydell uint64_t value, unsigned size, 1756a9d85353SPeter Maydell MemTxAttrs attrs) 1757e2c56465SPeter Maydell { 1758fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1759a9d85353SPeter Maydell return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs); 1760e2c56465SPeter Maydell } 1761e2c56465SPeter Maydell 1762e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU. 1763fae15286SPeter Maydell * These just decode the opaque pointer into GICState* + cpu id. 1764e2c56465SPeter Maydell */ 1765a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data, 1766a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1767e2c56465SPeter Maydell { 1768fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1769fae15286SPeter Maydell GICState *s = *backref; 1770e2c56465SPeter Maydell int id = (backref - s->backref); 1771a9d85353SPeter Maydell return gic_cpu_read(s, id, addr, data, attrs); 1772e2c56465SPeter Maydell } 1773e2c56465SPeter Maydell 1774a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr, 1775a9d85353SPeter Maydell uint64_t value, unsigned size, 1776a9d85353SPeter Maydell MemTxAttrs attrs) 1777e2c56465SPeter Maydell { 1778fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1779fae15286SPeter Maydell GICState *s = *backref; 1780e2c56465SPeter Maydell int id = (backref - s->backref); 1781a9d85353SPeter Maydell return gic_cpu_write(s, id, addr, value, attrs); 1782e2c56465SPeter Maydell } 1783e2c56465SPeter Maydell 17842c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data, 17852c679ac7SLuc Michel unsigned size, MemTxAttrs attrs) 17862c679ac7SLuc Michel { 17872c679ac7SLuc Michel GICState *s = (GICState *)opaque; 17882c679ac7SLuc Michel 17892c679ac7SLuc Michel return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs); 17902c679ac7SLuc Michel } 17912c679ac7SLuc Michel 17922c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr, 17932c679ac7SLuc Michel uint64_t value, unsigned size, 17942c679ac7SLuc Michel MemTxAttrs attrs) 17952c679ac7SLuc Michel { 17962c679ac7SLuc Michel GICState *s = (GICState *)opaque; 17972c679ac7SLuc Michel 17982c679ac7SLuc Michel return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs); 17992c679ac7SLuc Michel } 18002c679ac7SLuc Michel 1801527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start) 1802527d296fSLuc Michel { 1803527d296fSLuc Michel int lr_idx; 1804527d296fSLuc Michel uint32_t ret = 0; 1805527d296fSLuc Michel 1806527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1807527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1808527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1809527d296fSLuc Michel gic_lr_entry_is_eoi(*entry)); 1810527d296fSLuc Michel } 1811527d296fSLuc Michel 1812527d296fSLuc Michel return ret; 1813527d296fSLuc Michel } 1814527d296fSLuc Michel 1815527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start) 1816527d296fSLuc Michel { 1817527d296fSLuc Michel int lr_idx; 1818527d296fSLuc Michel uint32_t ret = 0; 1819527d296fSLuc Michel 1820527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1821527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1822527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1823527d296fSLuc Michel gic_lr_entry_is_free(*entry)); 1824527d296fSLuc Michel } 1825527d296fSLuc Michel 1826527d296fSLuc Michel return ret; 1827527d296fSLuc Michel } 1828527d296fSLuc Michel 1829527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs) 1830527d296fSLuc Michel { 1831527d296fSLuc Michel int vcpu = gic_get_current_vcpu(s); 1832527d296fSLuc Michel uint32_t ctlr; 1833527d296fSLuc Michel uint32_t abpr; 1834527d296fSLuc Michel uint32_t bpr; 1835527d296fSLuc Michel uint32_t prio_mask; 1836527d296fSLuc Michel 1837527d296fSLuc Michel ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr); 1838527d296fSLuc Michel abpr = FIELD_EX32(value, GICH_VMCR, VMABP); 1839527d296fSLuc Michel bpr = FIELD_EX32(value, GICH_VMCR, VMBP); 1840527d296fSLuc Michel prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3; 1841527d296fSLuc Michel 1842527d296fSLuc Michel gic_set_cpu_control(s, vcpu, ctlr, attrs); 1843527d296fSLuc Michel s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR); 1844527d296fSLuc Michel s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR); 1845527d296fSLuc Michel gic_set_priority_mask(s, vcpu, prio_mask, attrs); 1846527d296fSLuc Michel } 1847527d296fSLuc Michel 1848527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr, 1849527d296fSLuc Michel uint64_t *data, MemTxAttrs attrs) 1850527d296fSLuc Michel { 1851527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1852527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1853527d296fSLuc Michel 1854527d296fSLuc Michel switch (addr) { 1855527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1856527d296fSLuc Michel *data = s->h_hcr[cpu]; 1857527d296fSLuc Michel break; 1858527d296fSLuc Michel 1859527d296fSLuc Michel case A_GICH_VTR: /* VGIC Type */ 1860527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1); 1861527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PREbits, 1862527d296fSLuc Michel GIC_VIRT_MAX_GROUP_PRIO_BITS - 1); 1863527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PRIbits, 1864527d296fSLuc Michel (7 - GIC_VIRT_MIN_BPR) - 1); 1865527d296fSLuc Michel break; 1866527d296fSLuc Michel 1867527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1868527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr, 1869527d296fSLuc Michel extract32(s->cpu_ctlr[vcpu], 0, 10)); 1870527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]); 1871527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]); 1872527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask, 1873527d296fSLuc Michel extract32(s->priority_mask[vcpu], 3, 5)); 1874527d296fSLuc Michel break; 1875527d296fSLuc Michel 1876527d296fSLuc Michel case A_GICH_MISR: /* Maintenance Interrupt Status */ 1877527d296fSLuc Michel *data = s->h_misr[cpu]; 1878527d296fSLuc Michel break; 1879527d296fSLuc Michel 1880527d296fSLuc Michel case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */ 1881527d296fSLuc Michel case A_GICH_EISR1: 1882527d296fSLuc Michel *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8); 1883527d296fSLuc Michel break; 1884527d296fSLuc Michel 1885527d296fSLuc Michel case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */ 1886527d296fSLuc Michel case A_GICH_ELRSR1: 1887527d296fSLuc Michel *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8); 1888527d296fSLuc Michel break; 1889527d296fSLuc Michel 1890527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1891527d296fSLuc Michel *data = s->h_apr[cpu]; 1892527d296fSLuc Michel break; 1893527d296fSLuc Michel 1894527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1895527d296fSLuc Michel { 1896527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1897527d296fSLuc Michel 1898527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1899527d296fSLuc Michel *data = 0; 1900527d296fSLuc Michel } else { 1901527d296fSLuc Michel *data = s->h_lr[lr_idx][cpu]; 1902527d296fSLuc Michel } 1903527d296fSLuc Michel break; 1904527d296fSLuc Michel } 1905527d296fSLuc Michel 1906527d296fSLuc Michel default: 1907527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1908527d296fSLuc Michel "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr); 1909527d296fSLuc Michel return MEMTX_OK; 1910527d296fSLuc Michel } 1911527d296fSLuc Michel 1912*067a2b9cSLuc Michel trace_gic_hyp_read(addr, *data); 1913527d296fSLuc Michel return MEMTX_OK; 1914527d296fSLuc Michel } 1915527d296fSLuc Michel 1916527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr, 1917527d296fSLuc Michel uint64_t value, MemTxAttrs attrs) 1918527d296fSLuc Michel { 1919527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1920527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1921527d296fSLuc Michel 1922*067a2b9cSLuc Michel trace_gic_hyp_write(addr, value); 1923*067a2b9cSLuc Michel 1924527d296fSLuc Michel switch (addr) { 1925527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1926527d296fSLuc Michel s->h_hcr[cpu] = value & GICH_HCR_MASK; 1927527d296fSLuc Michel break; 1928527d296fSLuc Michel 1929527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1930527d296fSLuc Michel gic_vmcr_write(s, value, attrs); 1931527d296fSLuc Michel break; 1932527d296fSLuc Michel 1933527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1934527d296fSLuc Michel s->h_apr[cpu] = value; 1935527d296fSLuc Michel s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu); 1936527d296fSLuc Michel break; 1937527d296fSLuc Michel 1938527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1939527d296fSLuc Michel { 1940527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1941527d296fSLuc Michel 1942527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1943527d296fSLuc Michel return MEMTX_OK; 1944527d296fSLuc Michel } 1945527d296fSLuc Michel 1946527d296fSLuc Michel s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK; 1947*067a2b9cSLuc Michel trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]); 1948527d296fSLuc Michel break; 1949527d296fSLuc Michel } 1950527d296fSLuc Michel 1951527d296fSLuc Michel default: 1952527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1953527d296fSLuc Michel "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr); 1954527d296fSLuc Michel return MEMTX_OK; 1955527d296fSLuc Michel } 1956527d296fSLuc Michel 1957cbe1282bSLuc Michel gic_update_virt(s); 1958527d296fSLuc Michel return MEMTX_OK; 1959527d296fSLuc Michel } 1960527d296fSLuc Michel 1961527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 1962527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 1963527d296fSLuc Michel { 1964527d296fSLuc Michel GICState *s = (GICState *)opaque; 1965527d296fSLuc Michel 1966527d296fSLuc Michel return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs); 1967527d296fSLuc Michel } 1968527d296fSLuc Michel 1969527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr, 1970527d296fSLuc Michel uint64_t value, unsigned size, 1971527d296fSLuc Michel MemTxAttrs attrs) 1972527d296fSLuc Michel { 1973527d296fSLuc Michel GICState *s = (GICState *)opaque; 1974527d296fSLuc Michel 1975527d296fSLuc Michel return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs); 1976527d296fSLuc Michel } 1977527d296fSLuc Michel 1978527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 1979527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 1980527d296fSLuc Michel { 1981527d296fSLuc Michel GICState **backref = (GICState **)opaque; 1982527d296fSLuc Michel GICState *s = *backref; 1983527d296fSLuc Michel int id = (backref - s->backref); 1984527d296fSLuc Michel 1985527d296fSLuc Michel return gic_hyp_read(s, id, addr, data, attrs); 1986527d296fSLuc Michel } 1987527d296fSLuc Michel 1988527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr, 1989527d296fSLuc Michel uint64_t value, unsigned size, 1990527d296fSLuc Michel MemTxAttrs attrs) 1991527d296fSLuc Michel { 1992527d296fSLuc Michel GICState **backref = (GICState **)opaque; 1993527d296fSLuc Michel GICState *s = *backref; 1994527d296fSLuc Michel int id = (backref - s->backref); 1995527d296fSLuc Michel 1996527d296fSLuc Michel return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs); 1997527d296fSLuc Michel 1998527d296fSLuc Michel } 1999527d296fSLuc Michel 20007926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = { 20017926c210SPavel Fedin { 20027926c210SPavel Fedin .read_with_attrs = gic_dist_read, 20037926c210SPavel Fedin .write_with_attrs = gic_dist_write, 20047926c210SPavel Fedin .endianness = DEVICE_NATIVE_ENDIAN, 20057926c210SPavel Fedin }, 20067926c210SPavel Fedin { 2007a9d85353SPeter Maydell .read_with_attrs = gic_thiscpu_read, 2008a9d85353SPeter Maydell .write_with_attrs = gic_thiscpu_write, 2009e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 20107926c210SPavel Fedin } 2011e2c56465SPeter Maydell }; 2012e2c56465SPeter Maydell 2013e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = { 2014a9d85353SPeter Maydell .read_with_attrs = gic_do_cpu_read, 2015a9d85353SPeter Maydell .write_with_attrs = gic_do_cpu_write, 2016e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 2017e2c56465SPeter Maydell }; 2018e69954b9Spbrook 20192c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = { 20202c679ac7SLuc Michel { 2021527d296fSLuc Michel .read_with_attrs = gic_thiscpu_hyp_read, 2022527d296fSLuc Michel .write_with_attrs = gic_thiscpu_hyp_write, 20232c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 20242c679ac7SLuc Michel }, 20252c679ac7SLuc Michel { 20262c679ac7SLuc Michel .read_with_attrs = gic_thisvcpu_read, 20272c679ac7SLuc Michel .write_with_attrs = gic_thisvcpu_write, 20282c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 20292c679ac7SLuc Michel } 20302c679ac7SLuc Michel }; 20312c679ac7SLuc Michel 2032527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = { 2033527d296fSLuc Michel .read_with_attrs = gic_do_hyp_read, 2034527d296fSLuc Michel .write_with_attrs = gic_do_hyp_write, 2035527d296fSLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 2036527d296fSLuc Michel }; 2037527d296fSLuc Michel 203853111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp) 20392b518c56SPeter Maydell { 204053111180SPeter Maydell /* Device instance realize function for the GIC sysbus device */ 20412b518c56SPeter Maydell int i; 204253111180SPeter Maydell GICState *s = ARM_GIC(dev); 204353111180SPeter Maydell SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 20441e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_GET_CLASS(s); 20450175ba10SMarkus Armbruster Error *local_err = NULL; 20461e8cae4dSPeter Maydell 20470175ba10SMarkus Armbruster agc->parent_realize(dev, &local_err); 20480175ba10SMarkus Armbruster if (local_err) { 20490175ba10SMarkus Armbruster error_propagate(errp, local_err); 205053111180SPeter Maydell return; 205153111180SPeter Maydell } 20521e8cae4dSPeter Maydell 20535d721b78SAlexander Graf if (kvm_enabled() && !kvm_arm_supports_user_irq()) { 20545d721b78SAlexander Graf error_setg(errp, "KVM with user space irqchip only works when the " 20555d721b78SAlexander Graf "host kernel supports KVM_CAP_ARM_USER_IRQ"); 20565d721b78SAlexander Graf return; 20575d721b78SAlexander Graf } 20585d721b78SAlexander Graf 20592c679ac7SLuc Michel /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if 20602c679ac7SLuc Michel * enabled, virtualization extensions related interfaces (main virtual 20612c679ac7SLuc Michel * interface (s->vifaceiomem[0]) and virtual CPU interface). 20622c679ac7SLuc Michel */ 20632c679ac7SLuc Michel gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops); 20642b518c56SPeter Maydell 20657926c210SPavel Fedin /* Extra core-specific regions for the CPU interfaces. This is 20667926c210SPavel Fedin * necessary for "franken-GIC" implementations, for example on 20677926c210SPavel Fedin * Exynos 4. 2068e2c56465SPeter Maydell * NB that the memory region size of 0x100 applies for the 11MPCore 2069e2c56465SPeter Maydell * and also cores following the GIC v1 spec (ie A9). 2070e2c56465SPeter Maydell * GIC v2 defines a larger memory region (0x1000) so this will need 2071e2c56465SPeter Maydell * to be extended when we implement A15. 2072e2c56465SPeter Maydell */ 2073b95690c9SWei Huang for (i = 0; i < s->num_cpu; i++) { 2074e2c56465SPeter Maydell s->backref[i] = s; 20751437c94bSPaolo Bonzini memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops, 20761437c94bSPaolo Bonzini &s->backref[i], "gic_cpu", 0x100); 20777926c210SPavel Fedin sysbus_init_mmio(sbd, &s->cpuiomem[i+1]); 2078496dbcd1SPeter Maydell } 2079527d296fSLuc Michel 2080527d296fSLuc Michel /* Extra core-specific regions for virtual interfaces. This is required by 2081527d296fSLuc Michel * the GICv2 specification. 2082527d296fSLuc Michel */ 2083527d296fSLuc Michel if (s->virt_extn) { 2084527d296fSLuc Michel for (i = 0; i < s->num_cpu; i++) { 2085527d296fSLuc Michel memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s), 2086527d296fSLuc Michel &gic_viface_ops, &s->backref[i], 2087527d296fSLuc Michel "gic_viface", 0x1000); 2088527d296fSLuc Michel sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]); 2089527d296fSLuc Michel } 2090527d296fSLuc Michel } 2091527d296fSLuc Michel 2092496dbcd1SPeter Maydell } 2093496dbcd1SPeter Maydell 2094496dbcd1SPeter Maydell static void arm_gic_class_init(ObjectClass *klass, void *data) 2095496dbcd1SPeter Maydell { 2096496dbcd1SPeter Maydell DeviceClass *dc = DEVICE_CLASS(klass); 20971e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_CLASS(klass); 209853111180SPeter Maydell 2099bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize); 2100496dbcd1SPeter Maydell } 2101496dbcd1SPeter Maydell 21028c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = { 21031e8cae4dSPeter Maydell .name = TYPE_ARM_GIC, 21041e8cae4dSPeter Maydell .parent = TYPE_ARM_GIC_COMMON, 2105fae15286SPeter Maydell .instance_size = sizeof(GICState), 2106496dbcd1SPeter Maydell .class_init = arm_gic_class_init, 2107998a74bcSPeter Maydell .class_size = sizeof(ARMGICClass), 2108496dbcd1SPeter Maydell }; 2109496dbcd1SPeter Maydell 2110496dbcd1SPeter Maydell static void arm_gic_register_types(void) 2111496dbcd1SPeter Maydell { 2112496dbcd1SPeter Maydell type_register_static(&arm_gic_info); 2113496dbcd1SPeter Maydell } 2114496dbcd1SPeter Maydell 2115496dbcd1SPeter Maydell type_init(arm_gic_register_types) 2116