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" 27*0b8fa32fSMarkus Armbruster #include "qemu/module.h" 282531088fSHollis Blanchard #include "trace.h" 295d721b78SAlexander Graf #include "sysemu/kvm.h" 30386e2955SPeter Maydell 3168bf93ceSAlex Bennée /* #define DEBUG_GIC */ 32e69954b9Spbrook 33e69954b9Spbrook #ifdef DEBUG_GIC 3468bf93ceSAlex Bennée #define DEBUG_GIC_GATE 1 35e69954b9Spbrook #else 3668bf93ceSAlex Bennée #define DEBUG_GIC_GATE 0 37e69954b9Spbrook #endif 38e69954b9Spbrook 3968bf93ceSAlex Bennée #define DPRINTF(fmt, ...) do { \ 4068bf93ceSAlex Bennée if (DEBUG_GIC_GATE) { \ 4168bf93ceSAlex Bennée fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ 4268bf93ceSAlex Bennée } \ 4368bf93ceSAlex Bennée } while (0) 4468bf93ceSAlex Bennée 453355c360SAlistair Francis static const uint8_t gic_id_11mpcore[] = { 463355c360SAlistair Francis 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 473355c360SAlistair Francis }; 483355c360SAlistair Francis 493355c360SAlistair Francis static const uint8_t gic_id_gicv1[] = { 503355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 513355c360SAlistair Francis }; 523355c360SAlistair Francis 533355c360SAlistair Francis static const uint8_t gic_id_gicv2[] = { 543355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 552a29ddeeSPeter Maydell }; 562a29ddeeSPeter Maydell 57fae15286SPeter Maydell static inline int gic_get_current_cpu(GICState *s) 58926c4affSPeter Maydell { 59926c4affSPeter Maydell if (s->num_cpu > 1) { 604917cf44SAndreas Färber return current_cpu->cpu_index; 61926c4affSPeter Maydell } 62926c4affSPeter Maydell return 0; 63926c4affSPeter Maydell } 64926c4affSPeter Maydell 654a37e0e4SLuc Michel static inline int gic_get_current_vcpu(GICState *s) 664a37e0e4SLuc Michel { 674a37e0e4SLuc Michel return gic_get_current_cpu(s) + GIC_NCPU; 684a37e0e4SLuc Michel } 694a37e0e4SLuc Michel 70c27a5ba9SFabian Aggeler /* Return true if this GIC config has interrupt groups, which is 71c27a5ba9SFabian Aggeler * true if we're a GICv2, or a GICv1 with the security extensions. 72c27a5ba9SFabian Aggeler */ 73c27a5ba9SFabian Aggeler static inline bool gic_has_groups(GICState *s) 74c27a5ba9SFabian Aggeler { 75c27a5ba9SFabian Aggeler return s->revision == 2 || s->security_extn; 76c27a5ba9SFabian Aggeler } 77c27a5ba9SFabian Aggeler 783dd0471bSLuc Michel static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs) 793dd0471bSLuc Michel { 803dd0471bSLuc Michel return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure; 813dd0471bSLuc Michel } 823dd0471bSLuc Michel 83cbe1282bSLuc Michel static inline void gic_get_best_irq(GICState *s, int cpu, 84cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 85cbe1282bSLuc Michel { 86cbe1282bSLuc Michel int irq; 87cbe1282bSLuc Michel int cm = 1 << cpu; 88cbe1282bSLuc Michel 89cbe1282bSLuc Michel *best_irq = 1023; 90cbe1282bSLuc Michel *best_prio = 0x100; 91cbe1282bSLuc Michel 92cbe1282bSLuc Michel for (irq = 0; irq < s->num_irq; irq++) { 93cbe1282bSLuc Michel if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) && 94cbe1282bSLuc Michel (!GIC_DIST_TEST_ACTIVE(irq, cm)) && 95cbe1282bSLuc Michel (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) { 96cbe1282bSLuc Michel if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) { 97cbe1282bSLuc Michel *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu); 98cbe1282bSLuc Michel *best_irq = irq; 99cbe1282bSLuc Michel } 100cbe1282bSLuc Michel } 101cbe1282bSLuc Michel } 102cbe1282bSLuc Michel 103cbe1282bSLuc Michel if (*best_irq < 1023) { 104cbe1282bSLuc Michel *group = GIC_DIST_TEST_GROUP(*best_irq, cm); 105cbe1282bSLuc Michel } 106cbe1282bSLuc Michel } 107cbe1282bSLuc Michel 108cbe1282bSLuc Michel static inline void gic_get_best_virq(GICState *s, int cpu, 109cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 110cbe1282bSLuc Michel { 111cbe1282bSLuc Michel int lr_idx = 0; 112cbe1282bSLuc Michel 113cbe1282bSLuc Michel *best_irq = 1023; 114cbe1282bSLuc Michel *best_prio = 0x100; 115cbe1282bSLuc Michel 116cbe1282bSLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) { 117cbe1282bSLuc Michel uint32_t lr_entry = s->h_lr[lr_idx][cpu]; 118cbe1282bSLuc Michel int state = GICH_LR_STATE(lr_entry); 119cbe1282bSLuc Michel 120cbe1282bSLuc Michel if (state == GICH_LR_STATE_PENDING) { 121cbe1282bSLuc Michel int prio = GICH_LR_PRIORITY(lr_entry); 122cbe1282bSLuc Michel 123cbe1282bSLuc Michel if (prio < *best_prio) { 124cbe1282bSLuc Michel *best_prio = prio; 125cbe1282bSLuc Michel *best_irq = GICH_LR_VIRT_ID(lr_entry); 126cbe1282bSLuc Michel *group = GICH_LR_GROUP(lr_entry); 127cbe1282bSLuc Michel } 128cbe1282bSLuc Michel } 129cbe1282bSLuc Michel } 130cbe1282bSLuc Michel } 131cbe1282bSLuc Michel 132cbe1282bSLuc Michel /* Return true if IRQ signaling is enabled for the given cpu and at least one 133cbe1282bSLuc Michel * of the given groups: 134cbe1282bSLuc Michel * - in the non-virt case, the distributor must be enabled for one of the 135cbe1282bSLuc Michel * given groups 136cbe1282bSLuc Michel * - in the virt case, the virtual interface must be enabled. 137cbe1282bSLuc Michel * - in all cases, the (v)CPU interface must be enabled for one of the given 138cbe1282bSLuc Michel * groups. 139cbe1282bSLuc Michel */ 140cbe1282bSLuc Michel static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt, 141cbe1282bSLuc Michel int group_mask) 142cbe1282bSLuc Michel { 143cbe1282bSLuc Michel if (!virt && !(s->ctlr & group_mask)) { 144cbe1282bSLuc Michel return false; 145cbe1282bSLuc Michel } 146cbe1282bSLuc Michel 147cbe1282bSLuc Michel if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) { 148cbe1282bSLuc Michel return false; 149cbe1282bSLuc Michel } 150cbe1282bSLuc Michel 151cbe1282bSLuc Michel if (!(s->cpu_ctlr[cpu] & group_mask)) { 152cbe1282bSLuc Michel return false; 153cbe1282bSLuc Michel } 154cbe1282bSLuc Michel 155cbe1282bSLuc Michel return true; 156cbe1282bSLuc Michel } 157cbe1282bSLuc Michel 158e69954b9Spbrook /* TODO: Many places that call this routine could be optimized. */ 159e69954b9Spbrook /* Update interrupt status after enabled or pending bits have been changed. */ 160cbe1282bSLuc Michel static inline void gic_update_internal(GICState *s, bool virt) 161e69954b9Spbrook { 162e69954b9Spbrook int best_irq; 163e69954b9Spbrook int best_prio; 164dadbb58fSPeter Maydell int irq_level, fiq_level; 165cbe1282bSLuc Michel int cpu, cpu_iface; 166cbe1282bSLuc Michel int group = 0; 167cbe1282bSLuc Michel qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq; 168cbe1282bSLuc Michel qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq; 169e69954b9Spbrook 170b95690c9SWei Huang for (cpu = 0; cpu < s->num_cpu; cpu++) { 171cbe1282bSLuc Michel cpu_iface = virt ? (cpu + GIC_NCPU) : cpu; 172cbe1282bSLuc Michel 173cbe1282bSLuc Michel s->current_pending[cpu_iface] = 1023; 174cbe1282bSLuc Michel if (!gic_irq_signaling_enabled(s, cpu, virt, 175cbe1282bSLuc Michel GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) { 176cbe1282bSLuc Michel qemu_irq_lower(irq_lines[cpu]); 177cbe1282bSLuc Michel qemu_irq_lower(fiq_lines[cpu]); 178235069a3SJohan Karlsson continue; 179e69954b9Spbrook } 180cbe1282bSLuc Michel 181cbe1282bSLuc Michel if (virt) { 182cbe1282bSLuc Michel gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group); 183cbe1282bSLuc Michel } else { 184cbe1282bSLuc Michel gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group); 185e69954b9Spbrook } 186dadbb58fSPeter Maydell 1872531088fSHollis Blanchard if (best_irq != 1023) { 188067a2b9cSLuc Michel trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu, 189067a2b9cSLuc Michel best_irq, best_prio, 190067a2b9cSLuc Michel s->priority_mask[cpu_iface], 191067a2b9cSLuc Michel s->running_priority[cpu_iface]); 1922531088fSHollis Blanchard } 1932531088fSHollis Blanchard 194dadbb58fSPeter Maydell irq_level = fiq_level = 0; 195dadbb58fSPeter Maydell 196cbe1282bSLuc Michel if (best_prio < s->priority_mask[cpu_iface]) { 197cbe1282bSLuc Michel s->current_pending[cpu_iface] = best_irq; 198cbe1282bSLuc Michel if (best_prio < s->running_priority[cpu_iface]) { 199cbe1282bSLuc Michel if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) { 200cbe1282bSLuc Michel if (group == 0 && 201cbe1282bSLuc Michel s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) { 202dadbb58fSPeter Maydell DPRINTF("Raised pending FIQ %d (cpu %d)\n", 203cbe1282bSLuc Michel best_irq, cpu_iface); 204dadbb58fSPeter Maydell fiq_level = 1; 205cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq", 206cbe1282bSLuc Michel fiq_level); 207dadbb58fSPeter Maydell } else { 208dadbb58fSPeter Maydell DPRINTF("Raised pending IRQ %d (cpu %d)\n", 209cbe1282bSLuc Michel best_irq, cpu_iface); 210dadbb58fSPeter Maydell irq_level = 1; 211cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "virq" : "irq", 212cbe1282bSLuc Michel irq_level); 213e69954b9Spbrook } 214e69954b9Spbrook } 215dadbb58fSPeter Maydell } 216dadbb58fSPeter Maydell } 217dadbb58fSPeter Maydell 218cbe1282bSLuc Michel qemu_set_irq(irq_lines[cpu], irq_level); 219cbe1282bSLuc Michel qemu_set_irq(fiq_lines[cpu], fiq_level); 2209ee6e8bbSpbrook } 221e69954b9Spbrook } 222e69954b9Spbrook 223cbe1282bSLuc Michel static void gic_update(GICState *s) 224cbe1282bSLuc Michel { 225cbe1282bSLuc Michel gic_update_internal(s, false); 226cbe1282bSLuc Michel } 227cbe1282bSLuc Michel 228527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit 229527d296fSLuc Michel * in ELRSR is set. 230527d296fSLuc Michel */ 231527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry) 232527d296fSLuc Michel { 233527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 234527d296fSLuc Michel && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry)); 235527d296fSLuc Michel } 236527d296fSLuc Michel 237527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the 238527d296fSLuc Michel * corrsponding bit in EISR is set. 239527d296fSLuc Michel */ 240527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry) 241527d296fSLuc Michel { 242527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 243527d296fSLuc Michel && !GICH_LR_HW(entry) && GICH_LR_EOI(entry); 244527d296fSLuc Michel } 245527d296fSLuc Michel 24650e57926SLuc Michel static inline void gic_extract_lr_info(GICState *s, int cpu, 24750e57926SLuc Michel int *num_eoi, int *num_valid, int *num_pending) 24850e57926SLuc Michel { 24950e57926SLuc Michel int lr_idx; 25050e57926SLuc Michel 25150e57926SLuc Michel *num_eoi = 0; 25250e57926SLuc Michel *num_valid = 0; 25350e57926SLuc Michel *num_pending = 0; 25450e57926SLuc Michel 25550e57926SLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) { 25650e57926SLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 25750e57926SLuc Michel 25850e57926SLuc Michel if (gic_lr_entry_is_eoi(*entry)) { 25950e57926SLuc Michel (*num_eoi)++; 26050e57926SLuc Michel } 26150e57926SLuc Michel 26250e57926SLuc Michel if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) { 26350e57926SLuc Michel (*num_valid)++; 26450e57926SLuc Michel } 26550e57926SLuc Michel 26650e57926SLuc Michel if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) { 26750e57926SLuc Michel (*num_pending)++; 26850e57926SLuc Michel } 26950e57926SLuc Michel } 27050e57926SLuc Michel } 27150e57926SLuc Michel 27250e57926SLuc Michel static void gic_compute_misr(GICState *s, int cpu) 27350e57926SLuc Michel { 27450e57926SLuc Michel uint32_t value = 0; 27550e57926SLuc Michel int vcpu = cpu + GIC_NCPU; 27650e57926SLuc Michel 27750e57926SLuc Michel int num_eoi, num_valid, num_pending; 27850e57926SLuc Michel 27950e57926SLuc Michel gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending); 28050e57926SLuc Michel 28150e57926SLuc Michel /* EOI */ 28250e57926SLuc Michel if (num_eoi) { 28350e57926SLuc Michel value |= R_GICH_MISR_EOI_MASK; 28450e57926SLuc Michel } 28550e57926SLuc Michel 28650e57926SLuc Michel /* U: true if only 0 or 1 LR entry is valid */ 28750e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) { 28850e57926SLuc Michel value |= R_GICH_MISR_U_MASK; 28950e57926SLuc Michel } 29050e57926SLuc Michel 29150e57926SLuc Michel /* LRENP: EOICount is not 0 */ 29250e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) && 29350e57926SLuc Michel ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) { 29450e57926SLuc Michel value |= R_GICH_MISR_LRENP_MASK; 29550e57926SLuc Michel } 29650e57926SLuc Michel 29750e57926SLuc Michel /* NP: no pending interrupts */ 29850e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) { 29950e57926SLuc Michel value |= R_GICH_MISR_NP_MASK; 30050e57926SLuc Michel } 30150e57926SLuc Michel 30250e57926SLuc Michel /* VGrp0E: group0 virq signaling enabled */ 30350e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) && 30450e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) { 30550e57926SLuc Michel value |= R_GICH_MISR_VGrp0E_MASK; 30650e57926SLuc Michel } 30750e57926SLuc Michel 30850e57926SLuc Michel /* VGrp0D: group0 virq signaling disabled */ 30950e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) && 31050e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) { 31150e57926SLuc Michel value |= R_GICH_MISR_VGrp0D_MASK; 31250e57926SLuc Michel } 31350e57926SLuc Michel 31450e57926SLuc Michel /* VGrp1E: group1 virq signaling enabled */ 31550e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) && 31650e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) { 31750e57926SLuc Michel value |= R_GICH_MISR_VGrp1E_MASK; 31850e57926SLuc Michel } 31950e57926SLuc Michel 32050e57926SLuc Michel /* VGrp1D: group1 virq signaling disabled */ 32150e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) && 32250e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) { 32350e57926SLuc Michel value |= R_GICH_MISR_VGrp1D_MASK; 32450e57926SLuc Michel } 32550e57926SLuc Michel 32650e57926SLuc Michel s->h_misr[cpu] = value; 32750e57926SLuc Michel } 32850e57926SLuc Michel 32950e57926SLuc Michel static void gic_update_maintenance(GICState *s) 33050e57926SLuc Michel { 33150e57926SLuc Michel int cpu = 0; 33250e57926SLuc Michel int maint_level; 33350e57926SLuc Michel 33450e57926SLuc Michel for (cpu = 0; cpu < s->num_cpu; cpu++) { 33550e57926SLuc Michel gic_compute_misr(s, cpu); 33650e57926SLuc Michel maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu]; 33750e57926SLuc Michel 338067a2b9cSLuc Michel trace_gic_update_maintenance_irq(cpu, maint_level); 33950e57926SLuc Michel qemu_set_irq(s->maintenance_irq[cpu], maint_level); 34050e57926SLuc Michel } 34150e57926SLuc Michel } 34250e57926SLuc Michel 343cbe1282bSLuc Michel static void gic_update_virt(GICState *s) 344cbe1282bSLuc Michel { 345cbe1282bSLuc Michel gic_update_internal(s, true); 34650e57926SLuc Michel gic_update_maintenance(s); 347cbe1282bSLuc Michel } 348cbe1282bSLuc Michel 3498d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level, 3508d999995SChristoffer Dall int cm, int target) 3518d999995SChristoffer Dall { 3528d999995SChristoffer Dall if (level) { 35367ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 35467ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) { 3558d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 35667ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 3578d999995SChristoffer Dall } 3588d999995SChristoffer Dall } else { 35967ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 3608d999995SChristoffer Dall } 3618d999995SChristoffer Dall } 3628d999995SChristoffer Dall 3638d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level, 3648d999995SChristoffer Dall int cm, int target) 3658d999995SChristoffer Dall { 3668d999995SChristoffer Dall if (level) { 36767ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 3688d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 36967ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) { 37067ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 3718d999995SChristoffer Dall } 3728d999995SChristoffer Dall } else { 37367ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 3748d999995SChristoffer Dall } 3758d999995SChristoffer Dall } 3768d999995SChristoffer Dall 3779ee6e8bbSpbrook /* Process a change in an external IRQ input. */ 378e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level) 379e69954b9Spbrook { 380544d1afaSPeter Maydell /* Meaning of the 'irq' parameter: 381544d1afaSPeter Maydell * [0..N-1] : external interrupts 382544d1afaSPeter Maydell * [N..N+31] : PPI (internal) interrupts for CPU 0 383544d1afaSPeter Maydell * [N+32..N+63] : PPI (internal interrupts for CPU 1 384544d1afaSPeter Maydell * ... 385544d1afaSPeter Maydell */ 386fae15286SPeter Maydell GICState *s = (GICState *)opaque; 387544d1afaSPeter Maydell int cm, target; 388544d1afaSPeter Maydell if (irq < (s->num_irq - GIC_INTERNAL)) { 389e69954b9Spbrook /* The first external input line is internal interrupt 32. */ 390544d1afaSPeter Maydell cm = ALL_CPU_MASK; 39169253800SRusty Russell irq += GIC_INTERNAL; 39267ce697aSLuc Michel target = GIC_DIST_TARGET(irq); 393544d1afaSPeter Maydell } else { 394544d1afaSPeter Maydell int cpu; 395544d1afaSPeter Maydell irq -= (s->num_irq - GIC_INTERNAL); 396544d1afaSPeter Maydell cpu = irq / GIC_INTERNAL; 397544d1afaSPeter Maydell irq %= GIC_INTERNAL; 398544d1afaSPeter Maydell cm = 1 << cpu; 399544d1afaSPeter Maydell target = cm; 400544d1afaSPeter Maydell } 401544d1afaSPeter Maydell 40240d22500SChristoffer Dall assert(irq >= GIC_NR_SGIS); 40340d22500SChristoffer Dall 40467ce697aSLuc Michel if (level == GIC_DIST_TEST_LEVEL(irq, cm)) { 405e69954b9Spbrook return; 406544d1afaSPeter Maydell } 407e69954b9Spbrook 4083bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 4098d999995SChristoffer Dall gic_set_irq_11mpcore(s, irq, level, cm, target); 410e69954b9Spbrook } else { 4118d999995SChristoffer Dall gic_set_irq_generic(s, irq, level, cm, target); 412e69954b9Spbrook } 4132531088fSHollis Blanchard trace_gic_set_irq(irq, level, cm, target); 4148d999995SChristoffer Dall 415e69954b9Spbrook gic_update(s); 416e69954b9Spbrook } 417e69954b9Spbrook 4187c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu, 4197c0fa108SFabian Aggeler MemTxAttrs attrs) 4207c0fa108SFabian Aggeler { 4217c0fa108SFabian Aggeler uint16_t pending_irq = s->current_pending[cpu]; 4227c0fa108SFabian Aggeler 4237c0fa108SFabian Aggeler if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) { 42486b350f0SLuc Michel int group = gic_test_group(s, pending_irq, cpu); 42586b350f0SLuc Michel 4267c0fa108SFabian Aggeler /* On a GIC without the security extensions, reading this register 4277c0fa108SFabian Aggeler * behaves in the same way as a secure access to a GIC with them. 4287c0fa108SFabian Aggeler */ 4293dd0471bSLuc Michel bool secure = !gic_cpu_ns_access(s, cpu, attrs); 4307c0fa108SFabian Aggeler 4317c0fa108SFabian Aggeler if (group == 0 && !secure) { 4327c0fa108SFabian Aggeler /* Group0 interrupts hidden from Non-secure access */ 4337c0fa108SFabian Aggeler return 1023; 4347c0fa108SFabian Aggeler } 4357c0fa108SFabian Aggeler if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) { 4367c0fa108SFabian Aggeler /* Group1 interrupts only seen by Secure access if 4377c0fa108SFabian Aggeler * AckCtl bit set. 4387c0fa108SFabian Aggeler */ 4397c0fa108SFabian Aggeler return 1022; 4407c0fa108SFabian Aggeler } 4417c0fa108SFabian Aggeler } 4427c0fa108SFabian Aggeler return pending_irq; 4437c0fa108SFabian Aggeler } 4447c0fa108SFabian Aggeler 445df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq) 446df92cfa6SPeter Maydell { 447df92cfa6SPeter Maydell /* Return the group priority of the specified interrupt 448df92cfa6SPeter Maydell * (which is the top bits of its priority, with the number 449df92cfa6SPeter Maydell * of bits masked determined by the applicable binary point register). 450df92cfa6SPeter Maydell */ 451df92cfa6SPeter Maydell int bpr; 452df92cfa6SPeter Maydell uint32_t mask; 453df92cfa6SPeter Maydell 454df92cfa6SPeter Maydell if (gic_has_groups(s) && 455df92cfa6SPeter Maydell !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) && 45686b350f0SLuc Michel gic_test_group(s, irq, cpu)) { 457fc05a6f2SLuc MICHEL bpr = s->abpr[cpu] - 1; 458fc05a6f2SLuc MICHEL assert(bpr >= 0); 459df92cfa6SPeter Maydell } else { 460df92cfa6SPeter Maydell bpr = s->bpr[cpu]; 461df92cfa6SPeter Maydell } 462df92cfa6SPeter Maydell 463df92cfa6SPeter Maydell /* a BPR of 0 means the group priority bits are [7:1]; 464df92cfa6SPeter Maydell * a BPR of 1 means they are [7:2], and so on down to 465df92cfa6SPeter Maydell * a BPR of 7 meaning no group priority bits at all. 466df92cfa6SPeter Maydell */ 467df92cfa6SPeter Maydell mask = ~0U << ((bpr & 7) + 1); 468df92cfa6SPeter Maydell 46986b350f0SLuc Michel return gic_get_priority(s, irq, cpu) & mask; 470df92cfa6SPeter Maydell } 471df92cfa6SPeter Maydell 47272889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq) 473e69954b9Spbrook { 47472889c8aSPeter Maydell /* Set the appropriate Active Priority Register bit for this IRQ, 47572889c8aSPeter Maydell * and update the running priority. 47672889c8aSPeter Maydell */ 47772889c8aSPeter Maydell int prio = gic_get_group_priority(s, cpu, irq); 478a1d7b8d8SLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 479a1d7b8d8SLuc Michel int preemption_level = prio >> (min_bpr + 1); 48072889c8aSPeter Maydell int regno = preemption_level / 32; 48172889c8aSPeter Maydell int bitno = preemption_level % 32; 482a1d7b8d8SLuc Michel uint32_t *papr = NULL; 48372889c8aSPeter Maydell 484a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 485a1d7b8d8SLuc Michel assert(regno == 0); 486a1d7b8d8SLuc Michel papr = &s->h_apr[gic_get_vcpu_real_id(cpu)]; 487a1d7b8d8SLuc Michel } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) { 488a1d7b8d8SLuc Michel papr = &s->nsapr[regno][cpu]; 4899ee6e8bbSpbrook } else { 490a1d7b8d8SLuc Michel papr = &s->apr[regno][cpu]; 4919ee6e8bbSpbrook } 49272889c8aSPeter Maydell 493a1d7b8d8SLuc Michel *papr |= (1 << bitno); 494a1d7b8d8SLuc Michel 49572889c8aSPeter Maydell s->running_priority[cpu] = prio; 49686b350f0SLuc Michel gic_set_active(s, irq, cpu); 49772889c8aSPeter Maydell } 49872889c8aSPeter Maydell 49972889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu) 50072889c8aSPeter Maydell { 50172889c8aSPeter Maydell /* Recalculate the current running priority for this CPU based 50272889c8aSPeter Maydell * on the set bits in the Active Priority Registers. 50372889c8aSPeter Maydell */ 50472889c8aSPeter Maydell int i; 505a1d7b8d8SLuc Michel 506a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 507a1d7b8d8SLuc Michel uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)]; 508a1d7b8d8SLuc Michel if (apr) { 509a1d7b8d8SLuc Michel return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1); 510a1d7b8d8SLuc Michel } else { 511a1d7b8d8SLuc Michel return 0x100; 512a1d7b8d8SLuc Michel } 513a1d7b8d8SLuc Michel } 514a1d7b8d8SLuc Michel 51572889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 51672889c8aSPeter Maydell uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu]; 51772889c8aSPeter Maydell if (!apr) { 51872889c8aSPeter Maydell continue; 51972889c8aSPeter Maydell } 52072889c8aSPeter Maydell return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1); 52172889c8aSPeter Maydell } 52272889c8aSPeter Maydell return 0x100; 52372889c8aSPeter Maydell } 52472889c8aSPeter Maydell 52572889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group) 52672889c8aSPeter Maydell { 52772889c8aSPeter Maydell /* Drop the priority of the currently active interrupt in the 52872889c8aSPeter Maydell * specified group. 52972889c8aSPeter Maydell * 53072889c8aSPeter Maydell * Note that we can guarantee (because of the requirement to nest 53172889c8aSPeter Maydell * GICC_IAR reads [which activate an interrupt and raise priority] 53272889c8aSPeter Maydell * with GICC_EOIR writes [which drop the priority for the interrupt]) 53372889c8aSPeter Maydell * that the interrupt we're being called for is the highest priority 53472889c8aSPeter Maydell * active interrupt, meaning that it has the lowest set bit in the 53572889c8aSPeter Maydell * APR registers. 53672889c8aSPeter Maydell * 53772889c8aSPeter Maydell * If the guest does not honour the ordering constraints then the 53872889c8aSPeter Maydell * behaviour of the GIC is UNPREDICTABLE, which for us means that 53972889c8aSPeter Maydell * the values of the APR registers might become incorrect and the 54072889c8aSPeter Maydell * running priority will be wrong, so interrupts that should preempt 54172889c8aSPeter Maydell * might not do so, and interrupts that should not preempt might do so. 54272889c8aSPeter Maydell */ 543a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 544a1d7b8d8SLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 545a1d7b8d8SLuc Michel 546a1d7b8d8SLuc Michel if (s->h_apr[rcpu]) { 547a1d7b8d8SLuc Michel /* Clear lowest set bit */ 548a1d7b8d8SLuc Michel s->h_apr[rcpu] &= s->h_apr[rcpu] - 1; 549a1d7b8d8SLuc Michel } 550a1d7b8d8SLuc Michel } else { 55172889c8aSPeter Maydell int i; 55272889c8aSPeter Maydell 55372889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 55472889c8aSPeter Maydell uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu]; 55572889c8aSPeter Maydell if (!*papr) { 55672889c8aSPeter Maydell continue; 55772889c8aSPeter Maydell } 55872889c8aSPeter Maydell /* Clear lowest set bit */ 55972889c8aSPeter Maydell *papr &= *papr - 1; 56072889c8aSPeter Maydell break; 56172889c8aSPeter Maydell } 562a1d7b8d8SLuc Michel } 56372889c8aSPeter Maydell 56472889c8aSPeter Maydell s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu); 565e69954b9Spbrook } 566e69954b9Spbrook 567439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu) 568e69954b9Spbrook { 569439badd6SLuc Michel int src; 570439badd6SLuc Michel uint32_t ret; 571c5619bf9SFabian Aggeler 572439badd6SLuc Michel if (!gic_is_vcpu(cpu)) { 57340d22500SChristoffer Dall /* Lookup the source CPU for the SGI and clear this in the 57440d22500SChristoffer Dall * sgi_pending map. Return the src and clear the overall pending 57540d22500SChristoffer Dall * state on this CPU if the SGI is not pending from any CPUs. 57640d22500SChristoffer Dall */ 57740d22500SChristoffer Dall assert(s->sgi_pending[irq][cpu] != 0); 57840d22500SChristoffer Dall src = ctz32(s->sgi_pending[irq][cpu]); 57940d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~(1 << src); 58040d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 58186b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 58240d22500SChristoffer Dall } 58340d22500SChristoffer Dall ret = irq | ((src & 0x7) << 10); 58440d22500SChristoffer Dall } else { 585439badd6SLuc Michel uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu); 586439badd6SLuc Michel src = GICH_LR_CPUID(*lr_entry); 587439badd6SLuc Michel 588439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 589439badd6SLuc Michel ret = irq | (src << 10); 590439badd6SLuc Michel } 591439badd6SLuc Michel 592439badd6SLuc Michel return ret; 593439badd6SLuc Michel } 594439badd6SLuc Michel 595439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs) 596439badd6SLuc Michel { 597439badd6SLuc Michel int ret, irq; 598439badd6SLuc Michel 599439badd6SLuc Michel /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately 600439badd6SLuc Michel * for the case where this GIC supports grouping and the pending interrupt 601439badd6SLuc Michel * is in the wrong group. 60240d22500SChristoffer Dall */ 603439badd6SLuc Michel irq = gic_get_current_pending_irq(s, cpu, attrs); 604067a2b9cSLuc Michel trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 605067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), irq); 606439badd6SLuc Michel 607439badd6SLuc Michel if (irq >= GIC_MAXIRQ) { 608439badd6SLuc Michel DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq); 609439badd6SLuc Michel return irq; 610439badd6SLuc Michel } 611439badd6SLuc Michel 612439badd6SLuc Michel if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) { 613439badd6SLuc Michel DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq); 614439badd6SLuc Michel return 1023; 615439badd6SLuc Michel } 616439badd6SLuc Michel 617439badd6SLuc Michel gic_activate_irq(s, cpu, irq); 618439badd6SLuc Michel 619439badd6SLuc Michel if (s->revision == REV_11MPCORE) { 620439badd6SLuc Michel /* Clear pending flags for both level and edge triggered interrupts. 621439badd6SLuc Michel * Level triggered IRQs will be reasserted once they become inactive. 622439badd6SLuc Michel */ 623439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 624439badd6SLuc Michel ret = irq; 625439badd6SLuc Michel } else { 626439badd6SLuc Michel if (irq < GIC_NR_SGIS) { 627439badd6SLuc Michel ret = gic_clear_pending_sgi(s, irq, cpu); 628439badd6SLuc Michel } else { 62986b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 63040d22500SChristoffer Dall ret = irq; 63140d22500SChristoffer Dall } 63240d22500SChristoffer Dall } 63340d22500SChristoffer Dall 634cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 635cbe1282bSLuc Michel gic_update_virt(s); 636cbe1282bSLuc Michel } else { 63772889c8aSPeter Maydell gic_update(s); 638cbe1282bSLuc Michel } 63940d22500SChristoffer Dall DPRINTF("ACK %d\n", irq); 64040d22500SChristoffer Dall return ret; 641e69954b9Spbrook } 642e69954b9Spbrook 64367ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val, 64481508470SFabian Aggeler MemTxAttrs attrs) 6459df90ad0SChristoffer Dall { 64681508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 64767ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 64881508470SFabian Aggeler return; /* Ignore Non-secure access of Group0 IRQ */ 64981508470SFabian Aggeler } 65081508470SFabian Aggeler val = 0x80 | (val >> 1); /* Non-secure view */ 65181508470SFabian Aggeler } 65281508470SFabian Aggeler 6539df90ad0SChristoffer Dall if (irq < GIC_INTERNAL) { 6549df90ad0SChristoffer Dall s->priority1[irq][cpu] = val; 6559df90ad0SChristoffer Dall } else { 6569df90ad0SChristoffer Dall s->priority2[(irq) - GIC_INTERNAL] = val; 6579df90ad0SChristoffer Dall } 6589df90ad0SChristoffer Dall } 6599df90ad0SChristoffer Dall 66067ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq, 66181508470SFabian Aggeler MemTxAttrs attrs) 66281508470SFabian Aggeler { 66367ce697aSLuc Michel uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu); 66481508470SFabian Aggeler 66581508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 66667ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 66781508470SFabian Aggeler return 0; /* Non-secure access cannot read priority of Group0 IRQ */ 66881508470SFabian Aggeler } 66981508470SFabian Aggeler prio = (prio << 1) & 0xff; /* Non-secure view */ 67081508470SFabian Aggeler } 67181508470SFabian Aggeler return prio; 67281508470SFabian Aggeler } 67381508470SFabian Aggeler 67481508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask, 67581508470SFabian Aggeler MemTxAttrs attrs) 67681508470SFabian Aggeler { 6773dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 67881508470SFabian Aggeler if (s->priority_mask[cpu] & 0x80) { 67981508470SFabian Aggeler /* Priority Mask in upper half */ 68081508470SFabian Aggeler pmask = 0x80 | (pmask >> 1); 68181508470SFabian Aggeler } else { 68281508470SFabian Aggeler /* Non-secure write ignored if priority mask is in lower half */ 68381508470SFabian Aggeler return; 68481508470SFabian Aggeler } 68581508470SFabian Aggeler } 68681508470SFabian Aggeler s->priority_mask[cpu] = pmask; 68781508470SFabian Aggeler } 68881508470SFabian Aggeler 68981508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs) 69081508470SFabian Aggeler { 69181508470SFabian Aggeler uint32_t pmask = s->priority_mask[cpu]; 69281508470SFabian Aggeler 6933dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 69481508470SFabian Aggeler if (pmask & 0x80) { 69581508470SFabian Aggeler /* Priority Mask in upper half, return Non-secure view */ 69681508470SFabian Aggeler pmask = (pmask << 1) & 0xff; 69781508470SFabian Aggeler } else { 69881508470SFabian Aggeler /* Priority Mask in lower half, RAZ */ 69981508470SFabian Aggeler pmask = 0; 70081508470SFabian Aggeler } 70181508470SFabian Aggeler } 70281508470SFabian Aggeler return pmask; 70381508470SFabian Aggeler } 70481508470SFabian Aggeler 70532951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs) 70632951860SFabian Aggeler { 70732951860SFabian Aggeler uint32_t ret = s->cpu_ctlr[cpu]; 70832951860SFabian Aggeler 7093dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 71032951860SFabian Aggeler /* Construct the NS banked view of GICC_CTLR from the correct 71132951860SFabian Aggeler * bits of the S banked view. We don't need to move the bypass 71232951860SFabian Aggeler * control bits because we don't implement that (IMPDEF) part 71332951860SFabian Aggeler * of the GIC architecture. 71432951860SFabian Aggeler */ 71532951860SFabian Aggeler ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1; 71632951860SFabian Aggeler } 71732951860SFabian Aggeler return ret; 71832951860SFabian Aggeler } 71932951860SFabian Aggeler 72032951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value, 72132951860SFabian Aggeler MemTxAttrs attrs) 72232951860SFabian Aggeler { 72332951860SFabian Aggeler uint32_t mask; 72432951860SFabian Aggeler 7253dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 72632951860SFabian Aggeler /* The NS view can only write certain bits in the register; 72732951860SFabian Aggeler * the rest are unchanged 72832951860SFabian Aggeler */ 72932951860SFabian Aggeler mask = GICC_CTLR_EN_GRP1; 73032951860SFabian Aggeler if (s->revision == 2) { 73132951860SFabian Aggeler mask |= GICC_CTLR_EOIMODE_NS; 73232951860SFabian Aggeler } 73332951860SFabian Aggeler s->cpu_ctlr[cpu] &= ~mask; 73432951860SFabian Aggeler s->cpu_ctlr[cpu] |= (value << 1) & mask; 73532951860SFabian Aggeler } else { 73632951860SFabian Aggeler if (s->revision == 2) { 73732951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK; 73832951860SFabian Aggeler } else { 73932951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK; 74032951860SFabian Aggeler } 74132951860SFabian Aggeler s->cpu_ctlr[cpu] = value & mask; 74232951860SFabian Aggeler } 74332951860SFabian Aggeler DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, " 74432951860SFabian Aggeler "Group1 Interrupts %sabled\n", cpu, 74532951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis", 74632951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis"); 74732951860SFabian Aggeler } 74832951860SFabian Aggeler 74908efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs) 75008efa9f2SFabian Aggeler { 75171aa735bSLuc MICHEL if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) { 75271aa735bSLuc MICHEL /* Idle priority */ 75371aa735bSLuc MICHEL return 0xff; 75471aa735bSLuc MICHEL } 75571aa735bSLuc MICHEL 7563dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 75708efa9f2SFabian Aggeler if (s->running_priority[cpu] & 0x80) { 75808efa9f2SFabian Aggeler /* Running priority in upper half of range: return the Non-secure 75908efa9f2SFabian Aggeler * view of the priority. 76008efa9f2SFabian Aggeler */ 76108efa9f2SFabian Aggeler return s->running_priority[cpu] << 1; 76208efa9f2SFabian Aggeler } else { 76308efa9f2SFabian Aggeler /* Running priority in lower half of range: RAZ */ 76408efa9f2SFabian Aggeler return 0; 76508efa9f2SFabian Aggeler } 76608efa9f2SFabian Aggeler } else { 76708efa9f2SFabian Aggeler return s->running_priority[cpu]; 76808efa9f2SFabian Aggeler } 76908efa9f2SFabian Aggeler } 77008efa9f2SFabian Aggeler 771a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation, 772a55c910eSPeter Maydell * ie whether the relevant EOIMode bit is set. 773a55c910eSPeter Maydell */ 774a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs) 775a55c910eSPeter Maydell { 776a55c910eSPeter Maydell if (s->revision != 2) { 777a55c910eSPeter Maydell /* Before GICv2 prio-drop and deactivate are not separable */ 778a55c910eSPeter Maydell return false; 779a55c910eSPeter Maydell } 7803dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 781a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS; 782a55c910eSPeter Maydell } 783a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE; 784a55c910eSPeter Maydell } 785a55c910eSPeter Maydell 786a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 787a55c910eSPeter Maydell { 788ee03cca8SPeter Maydell int group; 789ee03cca8SPeter Maydell 79002f2e22dSLuc Michel if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) { 791ee03cca8SPeter Maydell /* 792ee03cca8SPeter Maydell * This handles two cases: 793ee03cca8SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 794ee03cca8SPeter Maydell * to the GICC_DIR, the GIC ignores that write. 795ee03cca8SPeter Maydell * 2. If software writes the number of a non-existent interrupt 796ee03cca8SPeter Maydell * this must be a subcase of "value written is not an active interrupt" 79702f2e22dSLuc Michel * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs, 79802f2e22dSLuc Michel * all IRQs potentially exist, so this limit does not apply. 799ee03cca8SPeter Maydell */ 800ee03cca8SPeter Maydell return; 801ee03cca8SPeter Maydell } 802ee03cca8SPeter Maydell 803a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 804a55c910eSPeter Maydell /* This is UNPREDICTABLE; we choose to ignore it */ 805a55c910eSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 806a55c910eSPeter Maydell "gic_deactivate_irq: GICC_DIR write when EOIMode clear"); 807a55c910eSPeter Maydell return; 808a55c910eSPeter Maydell } 809a55c910eSPeter Maydell 81002f2e22dSLuc Michel if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) { 81102f2e22dSLuc Michel /* This vIRQ does not have an LR entry which is either active or 81202f2e22dSLuc Michel * pending and active. Increment EOICount and ignore the write. 81302f2e22dSLuc Michel */ 81402f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 81502f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 816cbe1282bSLuc Michel 817cbe1282bSLuc Michel /* Update the virtual interface in case a maintenance interrupt should 818cbe1282bSLuc Michel * be raised. 819cbe1282bSLuc Michel */ 820cbe1282bSLuc Michel gic_update_virt(s); 82102f2e22dSLuc Michel return; 82202f2e22dSLuc Michel } 82302f2e22dSLuc Michel 82402f2e22dSLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 82502f2e22dSLuc Michel 8263dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 827a55c910eSPeter Maydell DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq); 828a55c910eSPeter Maydell return; 829a55c910eSPeter Maydell } 830a55c910eSPeter Maydell 83186b350f0SLuc Michel gic_clear_active(s, irq, cpu); 832a55c910eSPeter Maydell } 833a55c910eSPeter Maydell 83450491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 835e69954b9Spbrook { 8369ee6e8bbSpbrook int cm = 1 << cpu; 83772889c8aSPeter Maydell int group; 83872889c8aSPeter Maydell 839df628ff1Spbrook DPRINTF("EOI %d\n", irq); 84002f2e22dSLuc Michel if (gic_is_vcpu(cpu)) { 84102f2e22dSLuc Michel /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the 84202f2e22dSLuc Michel * running prio is < 0x100. 84302f2e22dSLuc Michel */ 84402f2e22dSLuc Michel bool prio_drop = s->running_priority[cpu] < 0x100; 84502f2e22dSLuc Michel 84602f2e22dSLuc Michel if (irq >= GIC_MAXIRQ) { 84702f2e22dSLuc Michel /* Ignore spurious interrupt */ 84802f2e22dSLuc Michel return; 84902f2e22dSLuc Michel } 85002f2e22dSLuc Michel 85102f2e22dSLuc Michel gic_drop_prio(s, cpu, 0); 85202f2e22dSLuc Michel 85302f2e22dSLuc Michel if (!gic_eoi_split(s, cpu, attrs)) { 85402f2e22dSLuc Michel bool valid = gic_virq_is_valid(s, irq, cpu); 85502f2e22dSLuc Michel if (prio_drop && !valid) { 85602f2e22dSLuc Michel /* We are in a situation where: 85702f2e22dSLuc Michel * - V_CTRL.EOIMode is false (no EOI split), 85802f2e22dSLuc Michel * - The call to gic_drop_prio() cleared a bit in GICH_APR, 85902f2e22dSLuc Michel * - This vIRQ does not have an LR entry which is either 86002f2e22dSLuc Michel * active or pending and active. 86102f2e22dSLuc Michel * In that case, we must increment EOICount. 86202f2e22dSLuc Michel */ 86302f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 86402f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 86502f2e22dSLuc Michel } else if (valid) { 86602f2e22dSLuc Michel gic_clear_active(s, irq, cpu); 86702f2e22dSLuc Michel } 86802f2e22dSLuc Michel } 86902f2e22dSLuc Michel 870cbe1282bSLuc Michel gic_update_virt(s); 87102f2e22dSLuc Michel return; 87202f2e22dSLuc Michel } 87302f2e22dSLuc Michel 874a32134aaSMark Langsdorf if (irq >= s->num_irq) { 875217bfb44SPeter Maydell /* This handles two cases: 876217bfb44SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 877217bfb44SPeter Maydell * to the GICC_EOIR, the GIC ignores that write. 878217bfb44SPeter Maydell * 2. If software writes the number of a non-existent interrupt 879217bfb44SPeter Maydell * this must be a subcase of "value written does not match the last 880217bfb44SPeter Maydell * valid interrupt value read from the Interrupt Acknowledge 881217bfb44SPeter Maydell * register" and so this is UNPREDICTABLE. We choose to ignore it. 882217bfb44SPeter Maydell */ 883217bfb44SPeter Maydell return; 884217bfb44SPeter Maydell } 88572889c8aSPeter Maydell if (s->running_priority[cpu] == 0x100) { 886e69954b9Spbrook return; /* No active IRQ. */ 88772889c8aSPeter Maydell } 8888d999995SChristoffer Dall 8893bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 890e69954b9Spbrook /* Mark level triggered interrupts as pending if they are still 891e69954b9Spbrook raised. */ 89267ce697aSLuc Michel if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm) 89367ce697aSLuc Michel && GIC_DIST_TEST_LEVEL(irq, cm) 89467ce697aSLuc Michel && (GIC_DIST_TARGET(irq) & cm) != 0) { 8959ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq, cm); 89667ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, cm); 897e69954b9Spbrook } 8988d999995SChristoffer Dall } 8998d999995SChristoffer Dall 90086b350f0SLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 90172889c8aSPeter Maydell 9023dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 903f9c6a7f1SFabian Aggeler DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq); 904f9c6a7f1SFabian Aggeler return; 905f9c6a7f1SFabian Aggeler } 906f9c6a7f1SFabian Aggeler 907f9c6a7f1SFabian Aggeler /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1 908f9c6a7f1SFabian Aggeler * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1, 909f9c6a7f1SFabian Aggeler * i.e. go ahead and complete the irq anyway. 910f9c6a7f1SFabian Aggeler */ 911f9c6a7f1SFabian Aggeler 91272889c8aSPeter Maydell gic_drop_prio(s, cpu, group); 913a55c910eSPeter Maydell 914a55c910eSPeter Maydell /* In GICv2 the guest can choose to split priority-drop and deactivate */ 915a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 91686b350f0SLuc Michel gic_clear_active(s, irq, cpu); 917a55c910eSPeter Maydell } 918e69954b9Spbrook gic_update(s); 919e69954b9Spbrook } 920e69954b9Spbrook 921a9d85353SPeter Maydell static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs) 922e69954b9Spbrook { 923fae15286SPeter Maydell GICState *s = (GICState *)opaque; 924e69954b9Spbrook uint32_t res; 925e69954b9Spbrook int irq; 926e69954b9Spbrook int i; 9279ee6e8bbSpbrook int cpu; 9289ee6e8bbSpbrook int cm; 9299ee6e8bbSpbrook int mask; 930e69954b9Spbrook 931926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 9329ee6e8bbSpbrook cm = 1 << cpu; 933e69954b9Spbrook if (offset < 0x100) { 934679aa175SFabian Aggeler if (offset == 0) { /* GICD_CTLR */ 935679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 936679aa175SFabian Aggeler /* The NS bank of this register is just an alias of the 937679aa175SFabian Aggeler * EnableGrp1 bit in the S bank version. 938679aa175SFabian Aggeler */ 939679aa175SFabian Aggeler return extract32(s->ctlr, 1, 1); 940679aa175SFabian Aggeler } else { 941679aa175SFabian Aggeler return s->ctlr; 942679aa175SFabian Aggeler } 943679aa175SFabian Aggeler } 944e69954b9Spbrook if (offset == 4) 9455543d1abSFabian Aggeler /* Interrupt Controller Type Register */ 9465543d1abSFabian Aggeler return ((s->num_irq / 32) - 1) 947b95690c9SWei Huang | ((s->num_cpu - 1) << 5) 9485543d1abSFabian Aggeler | (s->security_extn << 10); 949e69954b9Spbrook if (offset < 0x08) 950e69954b9Spbrook return 0; 951b79f2265SRob Herring if (offset >= 0x80) { 952c27a5ba9SFabian Aggeler /* Interrupt Group Registers: these RAZ/WI if this is an NS 953c27a5ba9SFabian Aggeler * access to a GIC with the security extensions, or if the GIC 954c27a5ba9SFabian Aggeler * doesn't have groups at all. 955c27a5ba9SFabian Aggeler */ 956c27a5ba9SFabian Aggeler res = 0; 957c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 958c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 959b6e6c651SPeter Maydell irq = (offset - 0x080) * 8; 960c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 961c27a5ba9SFabian Aggeler goto bad_reg; 962c27a5ba9SFabian Aggeler } 963c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 96467ce697aSLuc Michel if (GIC_DIST_TEST_GROUP(irq + i, cm)) { 965c27a5ba9SFabian Aggeler res |= (1 << i); 966c27a5ba9SFabian Aggeler } 967c27a5ba9SFabian Aggeler } 968c27a5ba9SFabian Aggeler } 969c27a5ba9SFabian Aggeler return res; 970b79f2265SRob Herring } 971e69954b9Spbrook goto bad_reg; 972e69954b9Spbrook } else if (offset < 0x200) { 973e69954b9Spbrook /* Interrupt Set/Clear Enable. */ 974e69954b9Spbrook if (offset < 0x180) 975e69954b9Spbrook irq = (offset - 0x100) * 8; 976e69954b9Spbrook else 977e69954b9Spbrook irq = (offset - 0x180) * 8; 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; 997a32134aaSMark Langsdorf if (irq >= s->num_irq) 998e69954b9Spbrook goto bad_reg; 999e69954b9Spbrook res = 0; 100069253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 1001e69954b9Spbrook for (i = 0; i < 8; i++) { 1002fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 100367ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1004fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1005fea8a08eSJens Wiklander } 1006fea8a08eSJens Wiklander 10078d999995SChristoffer Dall if (gic_test_pending(s, irq + i, mask)) { 1008e69954b9Spbrook res |= (1 << i); 1009e69954b9Spbrook } 1010e69954b9Spbrook } 1011e69954b9Spbrook } else if (offset < 0x400) { 10123bb0b038SLuc Michel /* Interrupt Set/Clear Active. */ 10133bb0b038SLuc Michel if (offset < 0x380) { 10143bb0b038SLuc Michel irq = (offset - 0x300) * 8; 10153bb0b038SLuc Michel } else if (s->revision == 2) { 10163bb0b038SLuc Michel irq = (offset - 0x380) * 8; 10173bb0b038SLuc Michel } else { 10183bb0b038SLuc Michel goto bad_reg; 10193bb0b038SLuc Michel } 10203bb0b038SLuc Michel 1021a32134aaSMark Langsdorf if (irq >= s->num_irq) 1022e69954b9Spbrook goto bad_reg; 1023e69954b9Spbrook res = 0; 102469253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 1025e69954b9Spbrook for (i = 0; i < 8; i++) { 1026fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 102767ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1028fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1029fea8a08eSJens Wiklander } 1030fea8a08eSJens Wiklander 103167ce697aSLuc Michel if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) { 1032e69954b9Spbrook res |= (1 << i); 1033e69954b9Spbrook } 1034e69954b9Spbrook } 1035e69954b9Spbrook } else if (offset < 0x800) { 1036e69954b9Spbrook /* Interrupt Priority. */ 1037b6e6c651SPeter Maydell irq = (offset - 0x400); 1038a32134aaSMark Langsdorf if (irq >= s->num_irq) 1039e69954b9Spbrook goto bad_reg; 104067ce697aSLuc Michel res = gic_dist_get_priority(s, cpu, irq, attrs); 1041e69954b9Spbrook } else if (offset < 0xc00) { 1042e69954b9Spbrook /* Interrupt CPU Target. */ 10436b9680bbSPeter Maydell if (s->num_cpu == 1 && s->revision != REV_11MPCORE) { 10446b9680bbSPeter Maydell /* For uniprocessor GICs these RAZ/WI */ 10456b9680bbSPeter Maydell res = 0; 10466b9680bbSPeter Maydell } else { 1047b6e6c651SPeter Maydell irq = (offset - 0x800); 10486b9680bbSPeter Maydell if (irq >= s->num_irq) { 1049e69954b9Spbrook goto bad_reg; 10506b9680bbSPeter Maydell } 10517995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 10527995206dSPeter Maydell res = 0; 10537995206dSPeter Maydell } else if (irq < GIC_INTERNAL) { 10549ee6e8bbSpbrook res = cm; 10559ee6e8bbSpbrook } else { 105667ce697aSLuc Michel res = GIC_DIST_TARGET(irq); 10579ee6e8bbSpbrook } 10586b9680bbSPeter Maydell } 1059e69954b9Spbrook } else if (offset < 0xf00) { 1060e69954b9Spbrook /* Interrupt Configuration. */ 1061b6e6c651SPeter Maydell irq = (offset - 0xc00) * 4; 1062a32134aaSMark Langsdorf if (irq >= s->num_irq) 1063e69954b9Spbrook goto bad_reg; 1064e69954b9Spbrook res = 0; 1065e69954b9Spbrook for (i = 0; i < 4; i++) { 1066fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 106767ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1068fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1069fea8a08eSJens Wiklander } 1070fea8a08eSJens Wiklander 107167ce697aSLuc Michel if (GIC_DIST_TEST_MODEL(irq + i)) { 1072e69954b9Spbrook res |= (1 << (i * 2)); 107367ce697aSLuc Michel } 107467ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 1075e69954b9Spbrook res |= (2 << (i * 2)); 1076e69954b9Spbrook } 107767ce697aSLuc Michel } 107840d22500SChristoffer Dall } else if (offset < 0xf10) { 107940d22500SChristoffer Dall goto bad_reg; 108040d22500SChristoffer Dall } else if (offset < 0xf30) { 10817c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 108240d22500SChristoffer Dall goto bad_reg; 108340d22500SChristoffer Dall } 108440d22500SChristoffer Dall 108540d22500SChristoffer Dall if (offset < 0xf20) { 108640d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 108740d22500SChristoffer Dall irq = (offset - 0xf10); 108840d22500SChristoffer Dall } else { 108940d22500SChristoffer Dall irq = (offset - 0xf20); 109040d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 109140d22500SChristoffer Dall } 109240d22500SChristoffer Dall 1093fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 109467ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 1095fea8a08eSJens Wiklander res = 0; /* Ignore Non-secure access of Group0 IRQ */ 1096fea8a08eSJens Wiklander } else { 109740d22500SChristoffer Dall res = s->sgi_pending[irq][cpu]; 1098fea8a08eSJens Wiklander } 10993355c360SAlistair Francis } else if (offset < 0xfd0) { 1100e69954b9Spbrook goto bad_reg; 11013355c360SAlistair Francis } else if (offset < 0x1000) { 1102e69954b9Spbrook if (offset & 3) { 1103e69954b9Spbrook res = 0; 1104e69954b9Spbrook } else { 11053355c360SAlistair Francis switch (s->revision) { 11063355c360SAlistair Francis case REV_11MPCORE: 11073355c360SAlistair Francis res = gic_id_11mpcore[(offset - 0xfd0) >> 2]; 11083355c360SAlistair Francis break; 11093355c360SAlistair Francis case 1: 11103355c360SAlistair Francis res = gic_id_gicv1[(offset - 0xfd0) >> 2]; 11113355c360SAlistair Francis break; 11123355c360SAlistair Francis case 2: 11133355c360SAlistair Francis res = gic_id_gicv2[(offset - 0xfd0) >> 2]; 11143355c360SAlistair Francis break; 11153355c360SAlistair Francis default: 11163355c360SAlistair Francis res = 0; 1117e69954b9Spbrook } 1118e69954b9Spbrook } 11193355c360SAlistair Francis } else { 11203355c360SAlistair Francis g_assert_not_reached(); 11213355c360SAlistair Francis } 1122e69954b9Spbrook return res; 1123e69954b9Spbrook bad_reg: 11248c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 11258c8dc39fSPeter Maydell "gic_dist_readb: Bad offset %x\n", (int)offset); 1126e69954b9Spbrook return 0; 1127e69954b9Spbrook } 1128e69954b9Spbrook 1129a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data, 1130a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1131e69954b9Spbrook { 1132a9d85353SPeter Maydell switch (size) { 1133a9d85353SPeter Maydell case 1: 1134a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1135067a2b9cSLuc Michel break; 1136a9d85353SPeter Maydell case 2: 1137a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1138a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1139067a2b9cSLuc Michel break; 1140a9d85353SPeter Maydell case 4: 1141a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1142a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1143a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16; 1144a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24; 1145067a2b9cSLuc Michel break; 1146a9d85353SPeter Maydell default: 1147a9d85353SPeter Maydell return MEMTX_ERROR; 1148e69954b9Spbrook } 1149067a2b9cSLuc Michel 1150067a2b9cSLuc Michel trace_gic_dist_read(offset, size, *data); 1151067a2b9cSLuc Michel return MEMTX_OK; 1152e69954b9Spbrook } 1153e69954b9Spbrook 1154a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset, 1155a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1156e69954b9Spbrook { 1157fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1158e69954b9Spbrook int irq; 1159e69954b9Spbrook int i; 11609ee6e8bbSpbrook int cpu; 1161e69954b9Spbrook 1162926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 1163e69954b9Spbrook if (offset < 0x100) { 1164e69954b9Spbrook if (offset == 0) { 1165679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 1166679aa175SFabian Aggeler /* NS version is just an alias of the S version's bit 1 */ 1167679aa175SFabian Aggeler s->ctlr = deposit32(s->ctlr, 1, 1, value); 1168679aa175SFabian Aggeler } else if (gic_has_groups(s)) { 1169679aa175SFabian Aggeler s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1); 1170679aa175SFabian Aggeler } else { 1171679aa175SFabian Aggeler s->ctlr = value & GICD_CTLR_EN_GRP0; 1172679aa175SFabian Aggeler } 1173679aa175SFabian Aggeler DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n", 1174679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis", 1175679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis"); 1176e69954b9Spbrook } else if (offset < 4) { 1177e69954b9Spbrook /* ignored. */ 1178b79f2265SRob Herring } else if (offset >= 0x80) { 1179c27a5ba9SFabian Aggeler /* Interrupt Group Registers: RAZ/WI for NS access to secure 1180c27a5ba9SFabian Aggeler * GIC, or for GICs without groups. 1181c27a5ba9SFabian Aggeler */ 1182c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 1183c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 1184b6e6c651SPeter Maydell irq = (offset - 0x80) * 8; 1185c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 1186c27a5ba9SFabian Aggeler goto bad_reg; 1187c27a5ba9SFabian Aggeler } 1188c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 1189c27a5ba9SFabian Aggeler /* Group bits are banked for private interrupts */ 1190c27a5ba9SFabian Aggeler int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 1191c27a5ba9SFabian Aggeler if (value & (1 << i)) { 1192c27a5ba9SFabian Aggeler /* Group1 (Non-secure) */ 119367ce697aSLuc Michel GIC_DIST_SET_GROUP(irq + i, cm); 1194c27a5ba9SFabian Aggeler } else { 1195c27a5ba9SFabian Aggeler /* Group0 (Secure) */ 119667ce697aSLuc Michel GIC_DIST_CLEAR_GROUP(irq + i, cm); 1197c27a5ba9SFabian Aggeler } 1198c27a5ba9SFabian Aggeler } 1199c27a5ba9SFabian Aggeler } 1200e69954b9Spbrook } else { 1201e69954b9Spbrook goto bad_reg; 1202e69954b9Spbrook } 1203e69954b9Spbrook } else if (offset < 0x180) { 1204e69954b9Spbrook /* Interrupt Set Enable. */ 1205b6e6c651SPeter Maydell irq = (offset - 0x100) * 8; 1206a32134aaSMark Langsdorf if (irq >= s->num_irq) 1207e69954b9Spbrook goto bad_reg; 120841ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12099ee6e8bbSpbrook value = 0xff; 121041ab7b55SChristoffer Dall } 121141ab7b55SChristoffer Dall 1212e69954b9Spbrook for (i = 0; i < 8; i++) { 1213e69954b9Spbrook if (value & (1 << i)) { 1214f47b48fbSDaniel Sangorrin int mask = 121567ce697aSLuc Michel (irq < GIC_INTERNAL) ? (1 << cpu) 121667ce697aSLuc Michel : GIC_DIST_TARGET(irq + i); 121769253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 121841bf234dSRabin Vincent 1219fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 122067ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1221fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1222fea8a08eSJens Wiklander } 1223fea8a08eSJens Wiklander 122467ce697aSLuc Michel if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1225e69954b9Spbrook DPRINTF("Enabled IRQ %d\n", irq + i); 12262531088fSHollis Blanchard trace_gic_enable_irq(irq + i); 122741bf234dSRabin Vincent } 122867ce697aSLuc Michel GIC_DIST_SET_ENABLED(irq + i, cm); 1229e69954b9Spbrook /* If a raised level triggered IRQ enabled then mark 1230e69954b9Spbrook is as pending. */ 123167ce697aSLuc Michel if (GIC_DIST_TEST_LEVEL(irq + i, mask) 123267ce697aSLuc Michel && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 12339ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq + i, mask); 123467ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, mask); 12359ee6e8bbSpbrook } 1236e69954b9Spbrook } 1237e69954b9Spbrook } 1238e69954b9Spbrook } else if (offset < 0x200) { 1239e69954b9Spbrook /* Interrupt Clear Enable. */ 1240b6e6c651SPeter Maydell irq = (offset - 0x180) * 8; 1241a32134aaSMark Langsdorf if (irq >= s->num_irq) 1242e69954b9Spbrook goto bad_reg; 124341ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12449ee6e8bbSpbrook value = 0; 124541ab7b55SChristoffer Dall } 124641ab7b55SChristoffer Dall 1247e69954b9Spbrook for (i = 0; i < 8; i++) { 1248e69954b9Spbrook if (value & (1 << i)) { 124969253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 125041bf234dSRabin Vincent 1251fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 125267ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1253fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1254fea8a08eSJens Wiklander } 1255fea8a08eSJens Wiklander 125667ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1257e69954b9Spbrook DPRINTF("Disabled IRQ %d\n", irq + i); 12582531088fSHollis Blanchard trace_gic_disable_irq(irq + i); 125941bf234dSRabin Vincent } 126067ce697aSLuc Michel GIC_DIST_CLEAR_ENABLED(irq + i, cm); 1261e69954b9Spbrook } 1262e69954b9Spbrook } 1263e69954b9Spbrook } else if (offset < 0x280) { 1264e69954b9Spbrook /* Interrupt Set Pending. */ 1265b6e6c651SPeter Maydell irq = (offset - 0x200) * 8; 1266a32134aaSMark Langsdorf if (irq >= s->num_irq) 1267e69954b9Spbrook goto bad_reg; 126841ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12695b0adce1SChristoffer Dall value = 0; 127041ab7b55SChristoffer Dall } 12719ee6e8bbSpbrook 1272e69954b9Spbrook for (i = 0; i < 8; i++) { 1273e69954b9Spbrook if (value & (1 << i)) { 1274fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 127567ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1276fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1277fea8a08eSJens Wiklander } 1278fea8a08eSJens Wiklander 127967ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i)); 1280e69954b9Spbrook } 1281e69954b9Spbrook } 1282e69954b9Spbrook } else if (offset < 0x300) { 1283e69954b9Spbrook /* Interrupt Clear Pending. */ 1284b6e6c651SPeter Maydell irq = (offset - 0x280) * 8; 1285a32134aaSMark Langsdorf if (irq >= s->num_irq) 1286e69954b9Spbrook goto bad_reg; 12875b0adce1SChristoffer Dall if (irq < GIC_NR_SGIS) { 12885b0adce1SChristoffer Dall value = 0; 12895b0adce1SChristoffer Dall } 12905b0adce1SChristoffer Dall 1291e69954b9Spbrook for (i = 0; i < 8; i++) { 1292fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 129367ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1294fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1295fea8a08eSJens Wiklander } 1296fea8a08eSJens Wiklander 12979ee6e8bbSpbrook /* ??? This currently clears the pending bit for all CPUs, even 12989ee6e8bbSpbrook for per-CPU interrupts. It's unclear whether this is the 12999ee6e8bbSpbrook corect behavior. */ 1300e69954b9Spbrook if (value & (1 << i)) { 130167ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK); 1302e69954b9Spbrook } 1303e69954b9Spbrook } 13043bb0b038SLuc Michel } else if (offset < 0x380) { 13053bb0b038SLuc Michel /* Interrupt Set Active. */ 13063bb0b038SLuc Michel if (s->revision != 2) { 1307e69954b9Spbrook goto bad_reg; 13083bb0b038SLuc Michel } 13093bb0b038SLuc Michel 1310b6e6c651SPeter Maydell irq = (offset - 0x300) * 8; 13113bb0b038SLuc Michel if (irq >= s->num_irq) { 13123bb0b038SLuc Michel goto bad_reg; 13133bb0b038SLuc Michel } 13143bb0b038SLuc Michel 13153bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 13163bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 13173bb0b038SLuc Michel 13183bb0b038SLuc Michel for (i = 0; i < 8; i++) { 13193bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 13203bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 13213bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 13223bb0b038SLuc Michel } 13233bb0b038SLuc Michel 13243bb0b038SLuc Michel if (value & (1 << i)) { 13253bb0b038SLuc Michel GIC_DIST_SET_ACTIVE(irq + i, cm); 13263bb0b038SLuc Michel } 13273bb0b038SLuc Michel } 13283bb0b038SLuc Michel } else if (offset < 0x400) { 13293bb0b038SLuc Michel /* Interrupt Clear Active. */ 13303bb0b038SLuc Michel if (s->revision != 2) { 13313bb0b038SLuc Michel goto bad_reg; 13323bb0b038SLuc Michel } 13333bb0b038SLuc Michel 1334b6e6c651SPeter Maydell irq = (offset - 0x380) * 8; 13353bb0b038SLuc Michel if (irq >= s->num_irq) { 13363bb0b038SLuc Michel goto bad_reg; 13373bb0b038SLuc Michel } 13383bb0b038SLuc Michel 13393bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 13403bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 13413bb0b038SLuc Michel 13423bb0b038SLuc Michel for (i = 0; i < 8; i++) { 13433bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 13443bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 13453bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 13463bb0b038SLuc Michel } 13473bb0b038SLuc Michel 13483bb0b038SLuc Michel if (value & (1 << i)) { 13493bb0b038SLuc Michel GIC_DIST_CLEAR_ACTIVE(irq + i, cm); 13503bb0b038SLuc Michel } 13513bb0b038SLuc Michel } 1352e69954b9Spbrook } else if (offset < 0x800) { 1353e69954b9Spbrook /* Interrupt Priority. */ 1354b6e6c651SPeter Maydell irq = (offset - 0x400); 1355a32134aaSMark Langsdorf if (irq >= s->num_irq) 1356e69954b9Spbrook goto bad_reg; 135767ce697aSLuc Michel gic_dist_set_priority(s, cpu, irq, value, attrs); 1358e69954b9Spbrook } else if (offset < 0xc00) { 13596b9680bbSPeter Maydell /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the 13606b9680bbSPeter Maydell * annoying exception of the 11MPCore's GIC. 13616b9680bbSPeter Maydell */ 13626b9680bbSPeter Maydell if (s->num_cpu != 1 || s->revision == REV_11MPCORE) { 1363b6e6c651SPeter Maydell irq = (offset - 0x800); 13646b9680bbSPeter Maydell if (irq >= s->num_irq) { 1365e69954b9Spbrook goto bad_reg; 13666b9680bbSPeter Maydell } 13677995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 13689ee6e8bbSpbrook value = 0; 13696b9680bbSPeter Maydell } else if (irq < GIC_INTERNAL) { 13709ee6e8bbSpbrook value = ALL_CPU_MASK; 13716b9680bbSPeter Maydell } 13729ee6e8bbSpbrook s->irq_target[irq] = value & ALL_CPU_MASK; 13736b9680bbSPeter Maydell } 1374e69954b9Spbrook } else if (offset < 0xf00) { 1375e69954b9Spbrook /* Interrupt Configuration. */ 1376b6e6c651SPeter Maydell irq = (offset - 0xc00) * 4; 1377a32134aaSMark Langsdorf if (irq >= s->num_irq) 1378e69954b9Spbrook goto bad_reg; 1379de7a900fSAdam Lackorzynski if (irq < GIC_NR_SGIS) 13809ee6e8bbSpbrook value |= 0xaa; 1381e69954b9Spbrook for (i = 0; i < 4; i++) { 1382fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 138367ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1384fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1385fea8a08eSJens Wiklander } 1386fea8a08eSJens Wiklander 13877c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 1388e69954b9Spbrook if (value & (1 << (i * 2))) { 138967ce697aSLuc Michel GIC_DIST_SET_MODEL(irq + i); 1390e69954b9Spbrook } else { 139167ce697aSLuc Michel GIC_DIST_CLEAR_MODEL(irq + i); 1392e69954b9Spbrook } 139324b790dfSAdam Lackorzynski } 1394e69954b9Spbrook if (value & (2 << (i * 2))) { 139567ce697aSLuc Michel GIC_DIST_SET_EDGE_TRIGGER(irq + i); 1396e69954b9Spbrook } else { 139767ce697aSLuc Michel GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i); 1398e69954b9Spbrook } 1399e69954b9Spbrook } 140040d22500SChristoffer Dall } else if (offset < 0xf10) { 14019ee6e8bbSpbrook /* 0xf00 is only handled for 32-bit writes. */ 1402e69954b9Spbrook goto bad_reg; 140340d22500SChristoffer Dall } else if (offset < 0xf20) { 140440d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 14057c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 140640d22500SChristoffer Dall goto bad_reg; 140740d22500SChristoffer Dall } 140840d22500SChristoffer Dall irq = (offset - 0xf10); 140940d22500SChristoffer Dall 1410fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 141167ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 141240d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~value; 141340d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 141467ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq, 1 << cpu); 141540d22500SChristoffer Dall } 1416fea8a08eSJens Wiklander } 141740d22500SChristoffer Dall } else if (offset < 0xf30) { 141840d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 14197c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 142040d22500SChristoffer Dall goto bad_reg; 142140d22500SChristoffer Dall } 142240d22500SChristoffer Dall irq = (offset - 0xf20); 142340d22500SChristoffer Dall 1424fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 142567ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 142667ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, 1 << cpu); 142740d22500SChristoffer Dall s->sgi_pending[irq][cpu] |= value; 1428fea8a08eSJens Wiklander } 142940d22500SChristoffer Dall } else { 143040d22500SChristoffer Dall goto bad_reg; 1431e69954b9Spbrook } 1432e69954b9Spbrook gic_update(s); 1433e69954b9Spbrook return; 1434e69954b9Spbrook bad_reg: 14358c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 14368c8dc39fSPeter Maydell "gic_dist_writeb: Bad offset %x\n", (int)offset); 1437e69954b9Spbrook } 1438e69954b9Spbrook 1439a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset, 1440a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1441e69954b9Spbrook { 1442a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, value & 0xff, attrs); 1443a9d85353SPeter Maydell gic_dist_writeb(opaque, offset + 1, value >> 8, attrs); 1444e69954b9Spbrook } 1445e69954b9Spbrook 1446a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset, 1447a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1448e69954b9Spbrook { 1449fae15286SPeter Maydell GICState *s = (GICState *)opaque; 14508da3ff18Spbrook if (offset == 0xf00) { 14519ee6e8bbSpbrook int cpu; 14529ee6e8bbSpbrook int irq; 14539ee6e8bbSpbrook int mask; 145440d22500SChristoffer Dall int target_cpu; 14559ee6e8bbSpbrook 1456926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 14579ee6e8bbSpbrook irq = value & 0x3ff; 14589ee6e8bbSpbrook switch ((value >> 24) & 3) { 14599ee6e8bbSpbrook case 0: 14609ee6e8bbSpbrook mask = (value >> 16) & ALL_CPU_MASK; 14619ee6e8bbSpbrook break; 14629ee6e8bbSpbrook case 1: 1463fa250144SAdam Lackorzynski mask = ALL_CPU_MASK ^ (1 << cpu); 14649ee6e8bbSpbrook break; 14659ee6e8bbSpbrook case 2: 1466fa250144SAdam Lackorzynski mask = 1 << cpu; 14679ee6e8bbSpbrook break; 14689ee6e8bbSpbrook default: 14699ee6e8bbSpbrook DPRINTF("Bad Soft Int target filter\n"); 14709ee6e8bbSpbrook mask = ALL_CPU_MASK; 14719ee6e8bbSpbrook break; 14729ee6e8bbSpbrook } 147367ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, mask); 147440d22500SChristoffer Dall target_cpu = ctz32(mask); 147540d22500SChristoffer Dall while (target_cpu < GIC_NCPU) { 147640d22500SChristoffer Dall s->sgi_pending[irq][target_cpu] |= (1 << cpu); 147740d22500SChristoffer Dall mask &= ~(1 << target_cpu); 147840d22500SChristoffer Dall target_cpu = ctz32(mask); 147940d22500SChristoffer Dall } 14809ee6e8bbSpbrook gic_update(s); 14819ee6e8bbSpbrook return; 14829ee6e8bbSpbrook } 1483a9d85353SPeter Maydell gic_dist_writew(opaque, offset, value & 0xffff, attrs); 1484a9d85353SPeter Maydell gic_dist_writew(opaque, offset + 2, value >> 16, attrs); 1485a9d85353SPeter Maydell } 1486a9d85353SPeter Maydell 1487a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data, 1488a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1489a9d85353SPeter Maydell { 1490067a2b9cSLuc Michel trace_gic_dist_write(offset, size, data); 1491067a2b9cSLuc Michel 1492a9d85353SPeter Maydell switch (size) { 1493a9d85353SPeter Maydell case 1: 1494a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, data, attrs); 1495a9d85353SPeter Maydell return MEMTX_OK; 1496a9d85353SPeter Maydell case 2: 1497a9d85353SPeter Maydell gic_dist_writew(opaque, offset, data, attrs); 1498a9d85353SPeter Maydell return MEMTX_OK; 1499a9d85353SPeter Maydell case 4: 1500a9d85353SPeter Maydell gic_dist_writel(opaque, offset, data, attrs); 1501a9d85353SPeter Maydell return MEMTX_OK; 1502a9d85353SPeter Maydell default: 1503a9d85353SPeter Maydell return MEMTX_ERROR; 1504a9d85353SPeter Maydell } 1505e69954b9Spbrook } 1506e69954b9Spbrook 150751fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno) 150851fd06e0SPeter Maydell { 150951fd06e0SPeter Maydell /* Return the Nonsecure view of GICC_APR<regno>. This is the 151051fd06e0SPeter Maydell * second half of GICC_NSAPR. 151151fd06e0SPeter Maydell */ 151251fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 151351fd06e0SPeter Maydell case 0: 151451fd06e0SPeter Maydell if (regno < 2) { 151551fd06e0SPeter Maydell return s->nsapr[regno + 2][cpu]; 151651fd06e0SPeter Maydell } 151751fd06e0SPeter Maydell break; 151851fd06e0SPeter Maydell case 1: 151951fd06e0SPeter Maydell if (regno == 0) { 152051fd06e0SPeter Maydell return s->nsapr[regno + 1][cpu]; 152151fd06e0SPeter Maydell } 152251fd06e0SPeter Maydell break; 152351fd06e0SPeter Maydell case 2: 152451fd06e0SPeter Maydell if (regno == 0) { 152551fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 16, 16); 152651fd06e0SPeter Maydell } 152751fd06e0SPeter Maydell break; 152851fd06e0SPeter Maydell case 3: 152951fd06e0SPeter Maydell if (regno == 0) { 153051fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 8, 8); 153151fd06e0SPeter Maydell } 153251fd06e0SPeter Maydell break; 153351fd06e0SPeter Maydell default: 153451fd06e0SPeter Maydell g_assert_not_reached(); 153551fd06e0SPeter Maydell } 153651fd06e0SPeter Maydell return 0; 153751fd06e0SPeter Maydell } 153851fd06e0SPeter Maydell 153951fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno, 154051fd06e0SPeter Maydell uint32_t value) 154151fd06e0SPeter Maydell { 154251fd06e0SPeter Maydell /* Write the Nonsecure view of GICC_APR<regno>. */ 154351fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 154451fd06e0SPeter Maydell case 0: 154551fd06e0SPeter Maydell if (regno < 2) { 154651fd06e0SPeter Maydell s->nsapr[regno + 2][cpu] = value; 154751fd06e0SPeter Maydell } 154851fd06e0SPeter Maydell break; 154951fd06e0SPeter Maydell case 1: 155051fd06e0SPeter Maydell if (regno == 0) { 155151fd06e0SPeter Maydell s->nsapr[regno + 1][cpu] = value; 155251fd06e0SPeter Maydell } 155351fd06e0SPeter Maydell break; 155451fd06e0SPeter Maydell case 2: 155551fd06e0SPeter Maydell if (regno == 0) { 155651fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value); 155751fd06e0SPeter Maydell } 155851fd06e0SPeter Maydell break; 155951fd06e0SPeter Maydell case 3: 156051fd06e0SPeter Maydell if (regno == 0) { 156151fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value); 156251fd06e0SPeter Maydell } 156351fd06e0SPeter Maydell break; 156451fd06e0SPeter Maydell default: 156551fd06e0SPeter Maydell g_assert_not_reached(); 156651fd06e0SPeter Maydell } 156751fd06e0SPeter Maydell } 156851fd06e0SPeter Maydell 1569a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, 1570a9d85353SPeter Maydell uint64_t *data, MemTxAttrs attrs) 1571e69954b9Spbrook { 1572e69954b9Spbrook switch (offset) { 1573e69954b9Spbrook case 0x00: /* Control */ 157432951860SFabian Aggeler *data = gic_get_cpu_control(s, cpu, attrs); 1575a9d85353SPeter Maydell break; 1576e69954b9Spbrook case 0x04: /* Priority mask */ 157781508470SFabian Aggeler *data = gic_get_priority_mask(s, cpu, attrs); 1578a9d85353SPeter Maydell break; 1579e69954b9Spbrook case 0x08: /* Binary Point */ 15803dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1581421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1582421a3c22SLuc MICHEL /* NS view of BPR when CBPR is 1 */ 1583421a3c22SLuc MICHEL *data = MIN(s->bpr[cpu] + 1, 7); 1584421a3c22SLuc MICHEL } else { 1585822e9cc3SFabian Aggeler /* BPR is banked. Non-secure copy stored in ABPR. */ 1586822e9cc3SFabian Aggeler *data = s->abpr[cpu]; 1587421a3c22SLuc MICHEL } 1588822e9cc3SFabian Aggeler } else { 1589a9d85353SPeter Maydell *data = s->bpr[cpu]; 1590822e9cc3SFabian Aggeler } 1591a9d85353SPeter Maydell break; 1592e69954b9Spbrook case 0x0c: /* Acknowledge */ 1593c5619bf9SFabian Aggeler *data = gic_acknowledge_irq(s, cpu, attrs); 1594a9d85353SPeter Maydell break; 159566a0a2cbSDong Xu Wang case 0x14: /* Running Priority */ 159608efa9f2SFabian Aggeler *data = gic_get_running_priority(s, cpu, attrs); 1597a9d85353SPeter Maydell break; 1598e69954b9Spbrook case 0x18: /* Highest Pending Interrupt */ 15997c0fa108SFabian Aggeler *data = gic_get_current_pending_irq(s, cpu, attrs); 1600a9d85353SPeter Maydell break; 1601aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 1602822e9cc3SFabian Aggeler /* GIC v2, no security: ABPR 1603822e9cc3SFabian Aggeler * GIC v1, no security: not implemented (RAZ/WI) 1604822e9cc3SFabian Aggeler * With security extensions, secure access: ABPR (alias of NS BPR) 1605822e9cc3SFabian Aggeler * With security extensions, nonsecure access: RAZ/WI 1606822e9cc3SFabian Aggeler */ 16073dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1608822e9cc3SFabian Aggeler *data = 0; 1609822e9cc3SFabian Aggeler } else { 1610a9d85353SPeter Maydell *data = s->abpr[cpu]; 1611822e9cc3SFabian Aggeler } 1612a9d85353SPeter Maydell break; 1613a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 161451fd06e0SPeter Maydell { 161551fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 16167eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 161751fd06e0SPeter Maydell 16187eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 161951fd06e0SPeter Maydell *data = 0; 16207eb079ecSLuc Michel } else if (gic_is_vcpu(cpu)) { 16217eb079ecSLuc Michel *data = s->h_apr[gic_get_vcpu_real_id(cpu)]; 16223dd0471bSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 162351fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 162451fd06e0SPeter Maydell *data = gic_apr_ns_view(s, regno, cpu); 162551fd06e0SPeter Maydell } else { 162651fd06e0SPeter Maydell *data = s->apr[regno][cpu]; 162751fd06e0SPeter Maydell } 1628a9d85353SPeter Maydell break; 162951fd06e0SPeter Maydell } 163051fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 163151fd06e0SPeter Maydell { 163251fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 163351fd06e0SPeter Maydell 163451fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) || 16357eb079ecSLuc Michel gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) { 163651fd06e0SPeter Maydell *data = 0; 163751fd06e0SPeter Maydell } else { 163851fd06e0SPeter Maydell *data = s->nsapr[regno][cpu]; 163951fd06e0SPeter Maydell } 164051fd06e0SPeter Maydell break; 164151fd06e0SPeter Maydell } 1642e69954b9Spbrook default: 16438c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 16448c8dc39fSPeter Maydell "gic_cpu_read: Bad offset %x\n", (int)offset); 16450cf09852SPeter Maydell *data = 0; 16460cf09852SPeter Maydell break; 1647e69954b9Spbrook } 1648067a2b9cSLuc Michel 1649067a2b9cSLuc Michel trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 1650067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, *data); 1651a9d85353SPeter Maydell return MEMTX_OK; 1652e69954b9Spbrook } 1653e69954b9Spbrook 1654a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset, 1655a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1656e69954b9Spbrook { 1657067a2b9cSLuc Michel trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 1658067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, value); 1659067a2b9cSLuc Michel 1660e69954b9Spbrook switch (offset) { 1661e69954b9Spbrook case 0x00: /* Control */ 166232951860SFabian Aggeler gic_set_cpu_control(s, cpu, value, attrs); 1663e69954b9Spbrook break; 1664e69954b9Spbrook case 0x04: /* Priority mask */ 166581508470SFabian Aggeler gic_set_priority_mask(s, cpu, value, attrs); 1666e69954b9Spbrook break; 1667e69954b9Spbrook case 0x08: /* Binary Point */ 16683dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1669421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1670421a3c22SLuc MICHEL /* WI when CBPR is 1 */ 1671421a3c22SLuc MICHEL return MEMTX_OK; 1672421a3c22SLuc MICHEL } else { 1673822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1674421a3c22SLuc MICHEL } 1675822e9cc3SFabian Aggeler } else { 16767eb079ecSLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 16777eb079ecSLuc Michel s->bpr[cpu] = MAX(value & 0x7, min_bpr); 1678822e9cc3SFabian Aggeler } 1679e69954b9Spbrook break; 1680e69954b9Spbrook case 0x10: /* End Of Interrupt */ 1681f9c6a7f1SFabian Aggeler gic_complete_irq(s, cpu, value & 0x3ff, attrs); 1682a9d85353SPeter Maydell return MEMTX_OK; 1683aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 16843dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1685822e9cc3SFabian Aggeler /* unimplemented, or NS access: RAZ/WI */ 1686822e9cc3SFabian Aggeler return MEMTX_OK; 1687822e9cc3SFabian Aggeler } else { 1688822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1689aa7d461aSChristoffer Dall } 1690aa7d461aSChristoffer Dall break; 1691a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 169251fd06e0SPeter Maydell { 169351fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 16947eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 169551fd06e0SPeter Maydell 16967eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 169751fd06e0SPeter Maydell return MEMTX_OK; 169851fd06e0SPeter Maydell } 16997eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 17007eb079ecSLuc Michel s->h_apr[gic_get_vcpu_real_id(cpu)] = value; 17017eb079ecSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 170251fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 170351fd06e0SPeter Maydell gic_apr_write_ns_view(s, regno, cpu, value); 170451fd06e0SPeter Maydell } else { 170551fd06e0SPeter Maydell s->apr[regno][cpu] = value; 170651fd06e0SPeter Maydell } 1707a9d477c4SChristoffer Dall break; 170851fd06e0SPeter Maydell } 170951fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 171051fd06e0SPeter Maydell { 171151fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 171251fd06e0SPeter Maydell 171351fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2) { 171451fd06e0SPeter Maydell return MEMTX_OK; 171551fd06e0SPeter Maydell } 17167eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 17177eb079ecSLuc Michel return MEMTX_OK; 17187eb079ecSLuc Michel } 17193dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 172051fd06e0SPeter Maydell return MEMTX_OK; 172151fd06e0SPeter Maydell } 172251fd06e0SPeter Maydell s->nsapr[regno][cpu] = value; 172351fd06e0SPeter Maydell break; 172451fd06e0SPeter Maydell } 1725a55c910eSPeter Maydell case 0x1000: 1726a55c910eSPeter Maydell /* GICC_DIR */ 1727a55c910eSPeter Maydell gic_deactivate_irq(s, cpu, value & 0x3ff, attrs); 1728a55c910eSPeter Maydell break; 1729e69954b9Spbrook default: 17308c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 17318c8dc39fSPeter Maydell "gic_cpu_write: Bad offset %x\n", (int)offset); 17320cf09852SPeter Maydell return MEMTX_OK; 1733e69954b9Spbrook } 1734cbe1282bSLuc Michel 1735cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 1736cbe1282bSLuc Michel gic_update_virt(s); 1737cbe1282bSLuc Michel } else { 1738e69954b9Spbrook gic_update(s); 1739cbe1282bSLuc Michel } 1740cbe1282bSLuc Michel 1741a9d85353SPeter Maydell return MEMTX_OK; 1742e69954b9Spbrook } 1743e2c56465SPeter Maydell 1744e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */ 1745a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data, 1746a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1747e2c56465SPeter Maydell { 1748fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1749a9d85353SPeter Maydell return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs); 1750e2c56465SPeter Maydell } 1751e2c56465SPeter Maydell 1752a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr, 1753a9d85353SPeter Maydell uint64_t value, unsigned size, 1754a9d85353SPeter Maydell MemTxAttrs attrs) 1755e2c56465SPeter Maydell { 1756fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1757a9d85353SPeter Maydell return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs); 1758e2c56465SPeter Maydell } 1759e2c56465SPeter Maydell 1760e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU. 1761fae15286SPeter Maydell * These just decode the opaque pointer into GICState* + cpu id. 1762e2c56465SPeter Maydell */ 1763a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data, 1764a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1765e2c56465SPeter Maydell { 1766fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1767fae15286SPeter Maydell GICState *s = *backref; 1768e2c56465SPeter Maydell int id = (backref - s->backref); 1769a9d85353SPeter Maydell return gic_cpu_read(s, id, addr, data, attrs); 1770e2c56465SPeter Maydell } 1771e2c56465SPeter Maydell 1772a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr, 1773a9d85353SPeter Maydell uint64_t value, unsigned size, 1774a9d85353SPeter Maydell MemTxAttrs attrs) 1775e2c56465SPeter Maydell { 1776fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1777fae15286SPeter Maydell GICState *s = *backref; 1778e2c56465SPeter Maydell int id = (backref - s->backref); 1779a9d85353SPeter Maydell return gic_cpu_write(s, id, addr, value, attrs); 1780e2c56465SPeter Maydell } 1781e2c56465SPeter Maydell 17822c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data, 17832c679ac7SLuc Michel unsigned size, MemTxAttrs attrs) 17842c679ac7SLuc Michel { 17852c679ac7SLuc Michel GICState *s = (GICState *)opaque; 17862c679ac7SLuc Michel 17872c679ac7SLuc Michel return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs); 17882c679ac7SLuc Michel } 17892c679ac7SLuc Michel 17902c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr, 17912c679ac7SLuc Michel uint64_t value, unsigned size, 17922c679ac7SLuc Michel MemTxAttrs attrs) 17932c679ac7SLuc Michel { 17942c679ac7SLuc Michel GICState *s = (GICState *)opaque; 17952c679ac7SLuc Michel 17962c679ac7SLuc Michel return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs); 17972c679ac7SLuc Michel } 17982c679ac7SLuc Michel 1799527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start) 1800527d296fSLuc Michel { 1801527d296fSLuc Michel int lr_idx; 1802527d296fSLuc Michel uint32_t ret = 0; 1803527d296fSLuc Michel 1804527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1805527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1806527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1807527d296fSLuc Michel gic_lr_entry_is_eoi(*entry)); 1808527d296fSLuc Michel } 1809527d296fSLuc Michel 1810527d296fSLuc Michel return ret; 1811527d296fSLuc Michel } 1812527d296fSLuc Michel 1813527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start) 1814527d296fSLuc Michel { 1815527d296fSLuc Michel int lr_idx; 1816527d296fSLuc Michel uint32_t ret = 0; 1817527d296fSLuc Michel 1818527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1819527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1820527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1821527d296fSLuc Michel gic_lr_entry_is_free(*entry)); 1822527d296fSLuc Michel } 1823527d296fSLuc Michel 1824527d296fSLuc Michel return ret; 1825527d296fSLuc Michel } 1826527d296fSLuc Michel 1827527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs) 1828527d296fSLuc Michel { 1829527d296fSLuc Michel int vcpu = gic_get_current_vcpu(s); 1830527d296fSLuc Michel uint32_t ctlr; 1831527d296fSLuc Michel uint32_t abpr; 1832527d296fSLuc Michel uint32_t bpr; 1833527d296fSLuc Michel uint32_t prio_mask; 1834527d296fSLuc Michel 1835527d296fSLuc Michel ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr); 1836527d296fSLuc Michel abpr = FIELD_EX32(value, GICH_VMCR, VMABP); 1837527d296fSLuc Michel bpr = FIELD_EX32(value, GICH_VMCR, VMBP); 1838527d296fSLuc Michel prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3; 1839527d296fSLuc Michel 1840527d296fSLuc Michel gic_set_cpu_control(s, vcpu, ctlr, attrs); 1841527d296fSLuc Michel s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR); 1842527d296fSLuc Michel s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR); 1843527d296fSLuc Michel gic_set_priority_mask(s, vcpu, prio_mask, attrs); 1844527d296fSLuc Michel } 1845527d296fSLuc Michel 1846527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr, 1847527d296fSLuc Michel uint64_t *data, MemTxAttrs attrs) 1848527d296fSLuc Michel { 1849527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1850527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1851527d296fSLuc Michel 1852527d296fSLuc Michel switch (addr) { 1853527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1854527d296fSLuc Michel *data = s->h_hcr[cpu]; 1855527d296fSLuc Michel break; 1856527d296fSLuc Michel 1857527d296fSLuc Michel case A_GICH_VTR: /* VGIC Type */ 1858527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1); 1859527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PREbits, 1860527d296fSLuc Michel GIC_VIRT_MAX_GROUP_PRIO_BITS - 1); 1861527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PRIbits, 1862527d296fSLuc Michel (7 - GIC_VIRT_MIN_BPR) - 1); 1863527d296fSLuc Michel break; 1864527d296fSLuc Michel 1865527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1866527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr, 1867527d296fSLuc Michel extract32(s->cpu_ctlr[vcpu], 0, 10)); 1868527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]); 1869527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]); 1870527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask, 1871527d296fSLuc Michel extract32(s->priority_mask[vcpu], 3, 5)); 1872527d296fSLuc Michel break; 1873527d296fSLuc Michel 1874527d296fSLuc Michel case A_GICH_MISR: /* Maintenance Interrupt Status */ 1875527d296fSLuc Michel *data = s->h_misr[cpu]; 1876527d296fSLuc Michel break; 1877527d296fSLuc Michel 1878527d296fSLuc Michel case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */ 1879527d296fSLuc Michel case A_GICH_EISR1: 1880527d296fSLuc Michel *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8); 1881527d296fSLuc Michel break; 1882527d296fSLuc Michel 1883527d296fSLuc Michel case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */ 1884527d296fSLuc Michel case A_GICH_ELRSR1: 1885527d296fSLuc Michel *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8); 1886527d296fSLuc Michel break; 1887527d296fSLuc Michel 1888527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1889527d296fSLuc Michel *data = s->h_apr[cpu]; 1890527d296fSLuc Michel break; 1891527d296fSLuc Michel 1892527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1893527d296fSLuc Michel { 1894527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1895527d296fSLuc Michel 1896527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1897527d296fSLuc Michel *data = 0; 1898527d296fSLuc Michel } else { 1899527d296fSLuc Michel *data = s->h_lr[lr_idx][cpu]; 1900527d296fSLuc Michel } 1901527d296fSLuc Michel break; 1902527d296fSLuc Michel } 1903527d296fSLuc Michel 1904527d296fSLuc Michel default: 1905527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1906527d296fSLuc Michel "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr); 1907527d296fSLuc Michel return MEMTX_OK; 1908527d296fSLuc Michel } 1909527d296fSLuc Michel 1910067a2b9cSLuc Michel trace_gic_hyp_read(addr, *data); 1911527d296fSLuc Michel return MEMTX_OK; 1912527d296fSLuc Michel } 1913527d296fSLuc Michel 1914527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr, 1915527d296fSLuc Michel uint64_t value, MemTxAttrs attrs) 1916527d296fSLuc Michel { 1917527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1918527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1919527d296fSLuc Michel 1920067a2b9cSLuc Michel trace_gic_hyp_write(addr, value); 1921067a2b9cSLuc Michel 1922527d296fSLuc Michel switch (addr) { 1923527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1924527d296fSLuc Michel s->h_hcr[cpu] = value & GICH_HCR_MASK; 1925527d296fSLuc Michel break; 1926527d296fSLuc Michel 1927527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1928527d296fSLuc Michel gic_vmcr_write(s, value, attrs); 1929527d296fSLuc Michel break; 1930527d296fSLuc Michel 1931527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1932527d296fSLuc Michel s->h_apr[cpu] = value; 1933527d296fSLuc Michel s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu); 1934527d296fSLuc Michel break; 1935527d296fSLuc Michel 1936527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1937527d296fSLuc Michel { 1938527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1939527d296fSLuc Michel 1940527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1941527d296fSLuc Michel return MEMTX_OK; 1942527d296fSLuc Michel } 1943527d296fSLuc Michel 1944527d296fSLuc Michel s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK; 1945067a2b9cSLuc Michel trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]); 1946527d296fSLuc Michel break; 1947527d296fSLuc Michel } 1948527d296fSLuc Michel 1949527d296fSLuc Michel default: 1950527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1951527d296fSLuc Michel "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr); 1952527d296fSLuc Michel return MEMTX_OK; 1953527d296fSLuc Michel } 1954527d296fSLuc Michel 1955cbe1282bSLuc Michel gic_update_virt(s); 1956527d296fSLuc Michel return MEMTX_OK; 1957527d296fSLuc Michel } 1958527d296fSLuc Michel 1959527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 1960527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 1961527d296fSLuc Michel { 1962527d296fSLuc Michel GICState *s = (GICState *)opaque; 1963527d296fSLuc Michel 1964527d296fSLuc Michel return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs); 1965527d296fSLuc Michel } 1966527d296fSLuc Michel 1967527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr, 1968527d296fSLuc Michel uint64_t value, unsigned size, 1969527d296fSLuc Michel MemTxAttrs attrs) 1970527d296fSLuc Michel { 1971527d296fSLuc Michel GICState *s = (GICState *)opaque; 1972527d296fSLuc Michel 1973527d296fSLuc Michel return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs); 1974527d296fSLuc Michel } 1975527d296fSLuc Michel 1976527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 1977527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 1978527d296fSLuc Michel { 1979527d296fSLuc Michel GICState **backref = (GICState **)opaque; 1980527d296fSLuc Michel GICState *s = *backref; 1981527d296fSLuc Michel int id = (backref - s->backref); 1982527d296fSLuc Michel 1983527d296fSLuc Michel return gic_hyp_read(s, id, addr, data, attrs); 1984527d296fSLuc Michel } 1985527d296fSLuc Michel 1986527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr, 1987527d296fSLuc Michel uint64_t value, unsigned size, 1988527d296fSLuc Michel MemTxAttrs attrs) 1989527d296fSLuc Michel { 1990527d296fSLuc Michel GICState **backref = (GICState **)opaque; 1991527d296fSLuc Michel GICState *s = *backref; 1992527d296fSLuc Michel int id = (backref - s->backref); 1993527d296fSLuc Michel 1994527d296fSLuc Michel return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs); 1995527d296fSLuc Michel 1996527d296fSLuc Michel } 1997527d296fSLuc Michel 19987926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = { 19997926c210SPavel Fedin { 20007926c210SPavel Fedin .read_with_attrs = gic_dist_read, 20017926c210SPavel Fedin .write_with_attrs = gic_dist_write, 20027926c210SPavel Fedin .endianness = DEVICE_NATIVE_ENDIAN, 20037926c210SPavel Fedin }, 20047926c210SPavel Fedin { 2005a9d85353SPeter Maydell .read_with_attrs = gic_thiscpu_read, 2006a9d85353SPeter Maydell .write_with_attrs = gic_thiscpu_write, 2007e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 20087926c210SPavel Fedin } 2009e2c56465SPeter Maydell }; 2010e2c56465SPeter Maydell 2011e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = { 2012a9d85353SPeter Maydell .read_with_attrs = gic_do_cpu_read, 2013a9d85353SPeter Maydell .write_with_attrs = gic_do_cpu_write, 2014e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 2015e2c56465SPeter Maydell }; 2016e69954b9Spbrook 20172c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = { 20182c679ac7SLuc Michel { 2019527d296fSLuc Michel .read_with_attrs = gic_thiscpu_hyp_read, 2020527d296fSLuc Michel .write_with_attrs = gic_thiscpu_hyp_write, 20212c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 20222c679ac7SLuc Michel }, 20232c679ac7SLuc Michel { 20242c679ac7SLuc Michel .read_with_attrs = gic_thisvcpu_read, 20252c679ac7SLuc Michel .write_with_attrs = gic_thisvcpu_write, 20262c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 20272c679ac7SLuc Michel } 20282c679ac7SLuc Michel }; 20292c679ac7SLuc Michel 2030527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = { 2031527d296fSLuc Michel .read_with_attrs = gic_do_hyp_read, 2032527d296fSLuc Michel .write_with_attrs = gic_do_hyp_write, 2033527d296fSLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 2034527d296fSLuc Michel }; 2035527d296fSLuc Michel 203653111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp) 20372b518c56SPeter Maydell { 203853111180SPeter Maydell /* Device instance realize function for the GIC sysbus device */ 20392b518c56SPeter Maydell int i; 204053111180SPeter Maydell GICState *s = ARM_GIC(dev); 204153111180SPeter Maydell SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 20421e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_GET_CLASS(s); 20430175ba10SMarkus Armbruster Error *local_err = NULL; 20441e8cae4dSPeter Maydell 20450175ba10SMarkus Armbruster agc->parent_realize(dev, &local_err); 20460175ba10SMarkus Armbruster if (local_err) { 20470175ba10SMarkus Armbruster error_propagate(errp, local_err); 204853111180SPeter Maydell return; 204953111180SPeter Maydell } 20501e8cae4dSPeter Maydell 20515d721b78SAlexander Graf if (kvm_enabled() && !kvm_arm_supports_user_irq()) { 20525d721b78SAlexander Graf error_setg(errp, "KVM with user space irqchip only works when the " 20535d721b78SAlexander Graf "host kernel supports KVM_CAP_ARM_USER_IRQ"); 20545d721b78SAlexander Graf return; 20555d721b78SAlexander Graf } 20565d721b78SAlexander Graf 20572c679ac7SLuc Michel /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if 20582c679ac7SLuc Michel * enabled, virtualization extensions related interfaces (main virtual 20592c679ac7SLuc Michel * interface (s->vifaceiomem[0]) and virtual CPU interface). 20602c679ac7SLuc Michel */ 20612c679ac7SLuc Michel gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops); 20622b518c56SPeter Maydell 20637926c210SPavel Fedin /* Extra core-specific regions for the CPU interfaces. This is 20647926c210SPavel Fedin * necessary for "franken-GIC" implementations, for example on 20657926c210SPavel Fedin * Exynos 4. 2066e2c56465SPeter Maydell * NB that the memory region size of 0x100 applies for the 11MPCore 2067e2c56465SPeter Maydell * and also cores following the GIC v1 spec (ie A9). 2068e2c56465SPeter Maydell * GIC v2 defines a larger memory region (0x1000) so this will need 2069e2c56465SPeter Maydell * to be extended when we implement A15. 2070e2c56465SPeter Maydell */ 2071b95690c9SWei Huang for (i = 0; i < s->num_cpu; i++) { 2072e2c56465SPeter Maydell s->backref[i] = s; 20731437c94bSPaolo Bonzini memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops, 20741437c94bSPaolo Bonzini &s->backref[i], "gic_cpu", 0x100); 20757926c210SPavel Fedin sysbus_init_mmio(sbd, &s->cpuiomem[i+1]); 2076496dbcd1SPeter Maydell } 2077527d296fSLuc Michel 2078527d296fSLuc Michel /* Extra core-specific regions for virtual interfaces. This is required by 2079527d296fSLuc Michel * the GICv2 specification. 2080527d296fSLuc Michel */ 2081527d296fSLuc Michel if (s->virt_extn) { 2082527d296fSLuc Michel for (i = 0; i < s->num_cpu; i++) { 2083527d296fSLuc Michel memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s), 2084527d296fSLuc Michel &gic_viface_ops, &s->backref[i], 20857210918cSPeter Maydell "gic_viface", 0x200); 2086527d296fSLuc Michel sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]); 2087527d296fSLuc Michel } 2088527d296fSLuc Michel } 2089527d296fSLuc Michel 2090496dbcd1SPeter Maydell } 2091496dbcd1SPeter Maydell 2092496dbcd1SPeter Maydell static void arm_gic_class_init(ObjectClass *klass, void *data) 2093496dbcd1SPeter Maydell { 2094496dbcd1SPeter Maydell DeviceClass *dc = DEVICE_CLASS(klass); 20951e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_CLASS(klass); 209653111180SPeter Maydell 2097bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize); 2098496dbcd1SPeter Maydell } 2099496dbcd1SPeter Maydell 21008c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = { 21011e8cae4dSPeter Maydell .name = TYPE_ARM_GIC, 21021e8cae4dSPeter Maydell .parent = TYPE_ARM_GIC_COMMON, 2103fae15286SPeter Maydell .instance_size = sizeof(GICState), 2104496dbcd1SPeter Maydell .class_init = arm_gic_class_init, 2105998a74bcSPeter Maydell .class_size = sizeof(ARMGICClass), 2106496dbcd1SPeter Maydell }; 2107496dbcd1SPeter Maydell 2108496dbcd1SPeter Maydell static void arm_gic_register_types(void) 2109496dbcd1SPeter Maydell { 2110496dbcd1SPeter Maydell type_register_static(&arm_gic_info); 2111496dbcd1SPeter Maydell } 2112496dbcd1SPeter Maydell 2113496dbcd1SPeter Maydell type_init(arm_gic_register_types) 2114