1e69954b9Spbrook /* 29ee6e8bbSpbrook * ARM Generic/Distributed Interrupt Controller 3e69954b9Spbrook * 49ee6e8bbSpbrook * Copyright (c) 2006-2007 CodeSourcery. 5e69954b9Spbrook * Written by Paul Brook 6e69954b9Spbrook * 78e31bf38SMatthew Fernandez * This code is licensed under the GPL. 8e69954b9Spbrook */ 9e69954b9Spbrook 109ee6e8bbSpbrook /* This file contains implementation code for the RealView EB interrupt 110d256bdcSPeter Maydell * controller, MPCore distributed interrupt controller and ARMv7-M 120d256bdcSPeter Maydell * Nested Vectored Interrupt Controller. 130d256bdcSPeter Maydell * It is compiled in two ways: 140d256bdcSPeter Maydell * (1) as a standalone file to produce a sysbus device which is a GIC 150d256bdcSPeter Maydell * that can be used on the realview board and as one of the builtin 160d256bdcSPeter Maydell * private peripherals for the ARM MP CPUs (11MPCore, A9, etc) 170d256bdcSPeter Maydell * (2) by being directly #included into armv7m_nvic.c to produce the 180d256bdcSPeter Maydell * armv7m_nvic device. 190d256bdcSPeter Maydell */ 20e69954b9Spbrook 218ef94f0bSPeter Maydell #include "qemu/osdep.h" 2283c9f4caSPaolo Bonzini #include "hw/sysbus.h" 2347b43a1fSPaolo Bonzini #include "gic_internal.h" 24da34e65cSMarkus Armbruster #include "qapi/error.h" 25dfc08079SAndreas Färber #include "qom/cpu.h" 2603dd024fSPaolo Bonzini #include "qemu/log.h" 272531088fSHollis Blanchard #include "trace.h" 285d721b78SAlexander Graf #include "sysemu/kvm.h" 29386e2955SPeter Maydell 3068bf93ceSAlex Bennée /* #define DEBUG_GIC */ 31e69954b9Spbrook 32e69954b9Spbrook #ifdef DEBUG_GIC 3368bf93ceSAlex Bennée #define DEBUG_GIC_GATE 1 34e69954b9Spbrook #else 3568bf93ceSAlex Bennée #define DEBUG_GIC_GATE 0 36e69954b9Spbrook #endif 37e69954b9Spbrook 3868bf93ceSAlex Bennée #define DPRINTF(fmt, ...) do { \ 3968bf93ceSAlex Bennée if (DEBUG_GIC_GATE) { \ 4068bf93ceSAlex Bennée fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ 4168bf93ceSAlex Bennée } \ 4268bf93ceSAlex Bennée } while (0) 4368bf93ceSAlex Bennée 443355c360SAlistair Francis static const uint8_t gic_id_11mpcore[] = { 453355c360SAlistair Francis 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 463355c360SAlistair Francis }; 473355c360SAlistair Francis 483355c360SAlistair Francis static const uint8_t gic_id_gicv1[] = { 493355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 503355c360SAlistair Francis }; 513355c360SAlistair Francis 523355c360SAlistair Francis static const uint8_t gic_id_gicv2[] = { 533355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 542a29ddeeSPeter Maydell }; 552a29ddeeSPeter Maydell 56fae15286SPeter Maydell static inline int gic_get_current_cpu(GICState *s) 57926c4affSPeter Maydell { 58926c4affSPeter Maydell if (s->num_cpu > 1) { 594917cf44SAndreas Färber return current_cpu->cpu_index; 60926c4affSPeter Maydell } 61926c4affSPeter Maydell return 0; 62926c4affSPeter Maydell } 63926c4affSPeter Maydell 644a37e0e4SLuc Michel static inline int gic_get_current_vcpu(GICState *s) 654a37e0e4SLuc Michel { 664a37e0e4SLuc Michel return gic_get_current_cpu(s) + GIC_NCPU; 674a37e0e4SLuc Michel } 684a37e0e4SLuc Michel 69c27a5ba9SFabian Aggeler /* Return true if this GIC config has interrupt groups, which is 70c27a5ba9SFabian Aggeler * true if we're a GICv2, or a GICv1 with the security extensions. 71c27a5ba9SFabian Aggeler */ 72c27a5ba9SFabian Aggeler static inline bool gic_has_groups(GICState *s) 73c27a5ba9SFabian Aggeler { 74c27a5ba9SFabian Aggeler return s->revision == 2 || s->security_extn; 75c27a5ba9SFabian Aggeler } 76c27a5ba9SFabian Aggeler 773dd0471bSLuc Michel static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs) 783dd0471bSLuc Michel { 793dd0471bSLuc Michel return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure; 803dd0471bSLuc Michel } 813dd0471bSLuc Michel 82*cbe1282bSLuc Michel static inline void gic_get_best_irq(GICState *s, int cpu, 83*cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 84*cbe1282bSLuc Michel { 85*cbe1282bSLuc Michel int irq; 86*cbe1282bSLuc Michel int cm = 1 << cpu; 87*cbe1282bSLuc Michel 88*cbe1282bSLuc Michel *best_irq = 1023; 89*cbe1282bSLuc Michel *best_prio = 0x100; 90*cbe1282bSLuc Michel 91*cbe1282bSLuc Michel for (irq = 0; irq < s->num_irq; irq++) { 92*cbe1282bSLuc Michel if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) && 93*cbe1282bSLuc Michel (!GIC_DIST_TEST_ACTIVE(irq, cm)) && 94*cbe1282bSLuc Michel (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) { 95*cbe1282bSLuc Michel if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) { 96*cbe1282bSLuc Michel *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu); 97*cbe1282bSLuc Michel *best_irq = irq; 98*cbe1282bSLuc Michel } 99*cbe1282bSLuc Michel } 100*cbe1282bSLuc Michel } 101*cbe1282bSLuc Michel 102*cbe1282bSLuc Michel if (*best_irq < 1023) { 103*cbe1282bSLuc Michel *group = GIC_DIST_TEST_GROUP(*best_irq, cm); 104*cbe1282bSLuc Michel } 105*cbe1282bSLuc Michel } 106*cbe1282bSLuc Michel 107*cbe1282bSLuc Michel static inline void gic_get_best_virq(GICState *s, int cpu, 108*cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 109*cbe1282bSLuc Michel { 110*cbe1282bSLuc Michel int lr_idx = 0; 111*cbe1282bSLuc Michel 112*cbe1282bSLuc Michel *best_irq = 1023; 113*cbe1282bSLuc Michel *best_prio = 0x100; 114*cbe1282bSLuc Michel 115*cbe1282bSLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) { 116*cbe1282bSLuc Michel uint32_t lr_entry = s->h_lr[lr_idx][cpu]; 117*cbe1282bSLuc Michel int state = GICH_LR_STATE(lr_entry); 118*cbe1282bSLuc Michel 119*cbe1282bSLuc Michel if (state == GICH_LR_STATE_PENDING) { 120*cbe1282bSLuc Michel int prio = GICH_LR_PRIORITY(lr_entry); 121*cbe1282bSLuc Michel 122*cbe1282bSLuc Michel if (prio < *best_prio) { 123*cbe1282bSLuc Michel *best_prio = prio; 124*cbe1282bSLuc Michel *best_irq = GICH_LR_VIRT_ID(lr_entry); 125*cbe1282bSLuc Michel *group = GICH_LR_GROUP(lr_entry); 126*cbe1282bSLuc Michel } 127*cbe1282bSLuc Michel } 128*cbe1282bSLuc Michel } 129*cbe1282bSLuc Michel } 130*cbe1282bSLuc Michel 131*cbe1282bSLuc Michel /* Return true if IRQ signaling is enabled for the given cpu and at least one 132*cbe1282bSLuc Michel * of the given groups: 133*cbe1282bSLuc Michel * - in the non-virt case, the distributor must be enabled for one of the 134*cbe1282bSLuc Michel * given groups 135*cbe1282bSLuc Michel * - in the virt case, the virtual interface must be enabled. 136*cbe1282bSLuc Michel * - in all cases, the (v)CPU interface must be enabled for one of the given 137*cbe1282bSLuc Michel * groups. 138*cbe1282bSLuc Michel */ 139*cbe1282bSLuc Michel static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt, 140*cbe1282bSLuc Michel int group_mask) 141*cbe1282bSLuc Michel { 142*cbe1282bSLuc Michel if (!virt && !(s->ctlr & group_mask)) { 143*cbe1282bSLuc Michel return false; 144*cbe1282bSLuc Michel } 145*cbe1282bSLuc Michel 146*cbe1282bSLuc Michel if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) { 147*cbe1282bSLuc Michel return false; 148*cbe1282bSLuc Michel } 149*cbe1282bSLuc Michel 150*cbe1282bSLuc Michel if (!(s->cpu_ctlr[cpu] & group_mask)) { 151*cbe1282bSLuc Michel return false; 152*cbe1282bSLuc Michel } 153*cbe1282bSLuc Michel 154*cbe1282bSLuc Michel return true; 155*cbe1282bSLuc Michel } 156*cbe1282bSLuc Michel 157e69954b9Spbrook /* TODO: Many places that call this routine could be optimized. */ 158e69954b9Spbrook /* Update interrupt status after enabled or pending bits have been changed. */ 159*cbe1282bSLuc Michel static inline void gic_update_internal(GICState *s, bool virt) 160e69954b9Spbrook { 161e69954b9Spbrook int best_irq; 162e69954b9Spbrook int best_prio; 163dadbb58fSPeter Maydell int irq_level, fiq_level; 164*cbe1282bSLuc Michel int cpu, cpu_iface; 165*cbe1282bSLuc Michel int group = 0; 166*cbe1282bSLuc Michel qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq; 167*cbe1282bSLuc Michel qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq; 168e69954b9Spbrook 169b95690c9SWei Huang for (cpu = 0; cpu < s->num_cpu; cpu++) { 170*cbe1282bSLuc Michel cpu_iface = virt ? (cpu + GIC_NCPU) : cpu; 171*cbe1282bSLuc Michel 172*cbe1282bSLuc Michel s->current_pending[cpu_iface] = 1023; 173*cbe1282bSLuc Michel if (!gic_irq_signaling_enabled(s, cpu, virt, 174*cbe1282bSLuc Michel GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) { 175*cbe1282bSLuc Michel qemu_irq_lower(irq_lines[cpu]); 176*cbe1282bSLuc Michel qemu_irq_lower(fiq_lines[cpu]); 177235069a3SJohan Karlsson continue; 178e69954b9Spbrook } 179*cbe1282bSLuc Michel 180*cbe1282bSLuc Michel if (virt) { 181*cbe1282bSLuc Michel gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group); 182*cbe1282bSLuc Michel } else { 183*cbe1282bSLuc Michel gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group); 184e69954b9Spbrook } 185dadbb58fSPeter Maydell 1862531088fSHollis Blanchard if (best_irq != 1023) { 1872531088fSHollis Blanchard trace_gic_update_bestirq(cpu, best_irq, best_prio, 188*cbe1282bSLuc Michel s->priority_mask[cpu_iface], s->running_priority[cpu_iface]); 1892531088fSHollis Blanchard } 1902531088fSHollis Blanchard 191dadbb58fSPeter Maydell irq_level = fiq_level = 0; 192dadbb58fSPeter Maydell 193*cbe1282bSLuc Michel if (best_prio < s->priority_mask[cpu_iface]) { 194*cbe1282bSLuc Michel s->current_pending[cpu_iface] = best_irq; 195*cbe1282bSLuc Michel if (best_prio < s->running_priority[cpu_iface]) { 196*cbe1282bSLuc Michel if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) { 197*cbe1282bSLuc Michel if (group == 0 && 198*cbe1282bSLuc Michel s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) { 199dadbb58fSPeter Maydell DPRINTF("Raised pending FIQ %d (cpu %d)\n", 200*cbe1282bSLuc Michel best_irq, cpu_iface); 201dadbb58fSPeter Maydell fiq_level = 1; 202*cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq", 203*cbe1282bSLuc Michel fiq_level); 204dadbb58fSPeter Maydell } else { 205dadbb58fSPeter Maydell DPRINTF("Raised pending IRQ %d (cpu %d)\n", 206*cbe1282bSLuc Michel best_irq, cpu_iface); 207dadbb58fSPeter Maydell irq_level = 1; 208*cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "virq" : "irq", 209*cbe1282bSLuc Michel irq_level); 210e69954b9Spbrook } 211e69954b9Spbrook } 212dadbb58fSPeter Maydell } 213dadbb58fSPeter Maydell } 214dadbb58fSPeter Maydell 215*cbe1282bSLuc Michel qemu_set_irq(irq_lines[cpu], irq_level); 216*cbe1282bSLuc Michel qemu_set_irq(fiq_lines[cpu], fiq_level); 2179ee6e8bbSpbrook } 218e69954b9Spbrook } 219e69954b9Spbrook 220*cbe1282bSLuc Michel static void gic_update(GICState *s) 221*cbe1282bSLuc Michel { 222*cbe1282bSLuc Michel gic_update_internal(s, false); 223*cbe1282bSLuc Michel } 224*cbe1282bSLuc Michel 225527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit 226527d296fSLuc Michel * in ELRSR is set. 227527d296fSLuc Michel */ 228527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry) 229527d296fSLuc Michel { 230527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 231527d296fSLuc Michel && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry)); 232527d296fSLuc Michel } 233527d296fSLuc Michel 234527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the 235527d296fSLuc Michel * corrsponding bit in EISR is set. 236527d296fSLuc Michel */ 237527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry) 238527d296fSLuc Michel { 239527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 240527d296fSLuc Michel && !GICH_LR_HW(entry) && GICH_LR_EOI(entry); 241527d296fSLuc Michel } 242527d296fSLuc Michel 243*cbe1282bSLuc Michel static void gic_update_virt(GICState *s) 244*cbe1282bSLuc Michel { 245*cbe1282bSLuc Michel gic_update_internal(s, true); 246*cbe1282bSLuc Michel } 247*cbe1282bSLuc Michel 2488d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level, 2498d999995SChristoffer Dall int cm, int target) 2508d999995SChristoffer Dall { 2518d999995SChristoffer Dall if (level) { 25267ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 25367ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) { 2548d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 25567ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 2568d999995SChristoffer Dall } 2578d999995SChristoffer Dall } else { 25867ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 2598d999995SChristoffer Dall } 2608d999995SChristoffer Dall } 2618d999995SChristoffer Dall 2628d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level, 2638d999995SChristoffer Dall int cm, int target) 2648d999995SChristoffer Dall { 2658d999995SChristoffer Dall if (level) { 26667ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 2678d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 26867ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) { 26967ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 2708d999995SChristoffer Dall } 2718d999995SChristoffer Dall } else { 27267ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 2738d999995SChristoffer Dall } 2748d999995SChristoffer Dall } 2758d999995SChristoffer Dall 2769ee6e8bbSpbrook /* Process a change in an external IRQ input. */ 277e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level) 278e69954b9Spbrook { 279544d1afaSPeter Maydell /* Meaning of the 'irq' parameter: 280544d1afaSPeter Maydell * [0..N-1] : external interrupts 281544d1afaSPeter Maydell * [N..N+31] : PPI (internal) interrupts for CPU 0 282544d1afaSPeter Maydell * [N+32..N+63] : PPI (internal interrupts for CPU 1 283544d1afaSPeter Maydell * ... 284544d1afaSPeter Maydell */ 285fae15286SPeter Maydell GICState *s = (GICState *)opaque; 286544d1afaSPeter Maydell int cm, target; 287544d1afaSPeter Maydell if (irq < (s->num_irq - GIC_INTERNAL)) { 288e69954b9Spbrook /* The first external input line is internal interrupt 32. */ 289544d1afaSPeter Maydell cm = ALL_CPU_MASK; 29069253800SRusty Russell irq += GIC_INTERNAL; 29167ce697aSLuc Michel target = GIC_DIST_TARGET(irq); 292544d1afaSPeter Maydell } else { 293544d1afaSPeter Maydell int cpu; 294544d1afaSPeter Maydell irq -= (s->num_irq - GIC_INTERNAL); 295544d1afaSPeter Maydell cpu = irq / GIC_INTERNAL; 296544d1afaSPeter Maydell irq %= GIC_INTERNAL; 297544d1afaSPeter Maydell cm = 1 << cpu; 298544d1afaSPeter Maydell target = cm; 299544d1afaSPeter Maydell } 300544d1afaSPeter Maydell 30140d22500SChristoffer Dall assert(irq >= GIC_NR_SGIS); 30240d22500SChristoffer Dall 30367ce697aSLuc Michel if (level == GIC_DIST_TEST_LEVEL(irq, cm)) { 304e69954b9Spbrook return; 305544d1afaSPeter Maydell } 306e69954b9Spbrook 3073bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 3088d999995SChristoffer Dall gic_set_irq_11mpcore(s, irq, level, cm, target); 309e69954b9Spbrook } else { 3108d999995SChristoffer Dall gic_set_irq_generic(s, irq, level, cm, target); 311e69954b9Spbrook } 3122531088fSHollis Blanchard trace_gic_set_irq(irq, level, cm, target); 3138d999995SChristoffer Dall 314e69954b9Spbrook gic_update(s); 315e69954b9Spbrook } 316e69954b9Spbrook 3177c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu, 3187c0fa108SFabian Aggeler MemTxAttrs attrs) 3197c0fa108SFabian Aggeler { 3207c0fa108SFabian Aggeler uint16_t pending_irq = s->current_pending[cpu]; 3217c0fa108SFabian Aggeler 3227c0fa108SFabian Aggeler if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) { 32386b350f0SLuc Michel int group = gic_test_group(s, pending_irq, cpu); 32486b350f0SLuc Michel 3257c0fa108SFabian Aggeler /* On a GIC without the security extensions, reading this register 3267c0fa108SFabian Aggeler * behaves in the same way as a secure access to a GIC with them. 3277c0fa108SFabian Aggeler */ 3283dd0471bSLuc Michel bool secure = !gic_cpu_ns_access(s, cpu, attrs); 3297c0fa108SFabian Aggeler 3307c0fa108SFabian Aggeler if (group == 0 && !secure) { 3317c0fa108SFabian Aggeler /* Group0 interrupts hidden from Non-secure access */ 3327c0fa108SFabian Aggeler return 1023; 3337c0fa108SFabian Aggeler } 3347c0fa108SFabian Aggeler if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) { 3357c0fa108SFabian Aggeler /* Group1 interrupts only seen by Secure access if 3367c0fa108SFabian Aggeler * AckCtl bit set. 3377c0fa108SFabian Aggeler */ 3387c0fa108SFabian Aggeler return 1022; 3397c0fa108SFabian Aggeler } 3407c0fa108SFabian Aggeler } 3417c0fa108SFabian Aggeler return pending_irq; 3427c0fa108SFabian Aggeler } 3437c0fa108SFabian Aggeler 344df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq) 345df92cfa6SPeter Maydell { 346df92cfa6SPeter Maydell /* Return the group priority of the specified interrupt 347df92cfa6SPeter Maydell * (which is the top bits of its priority, with the number 348df92cfa6SPeter Maydell * of bits masked determined by the applicable binary point register). 349df92cfa6SPeter Maydell */ 350df92cfa6SPeter Maydell int bpr; 351df92cfa6SPeter Maydell uint32_t mask; 352df92cfa6SPeter Maydell 353df92cfa6SPeter Maydell if (gic_has_groups(s) && 354df92cfa6SPeter Maydell !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) && 35586b350f0SLuc Michel gic_test_group(s, irq, cpu)) { 356fc05a6f2SLuc MICHEL bpr = s->abpr[cpu] - 1; 357fc05a6f2SLuc MICHEL assert(bpr >= 0); 358df92cfa6SPeter Maydell } else { 359df92cfa6SPeter Maydell bpr = s->bpr[cpu]; 360df92cfa6SPeter Maydell } 361df92cfa6SPeter Maydell 362df92cfa6SPeter Maydell /* a BPR of 0 means the group priority bits are [7:1]; 363df92cfa6SPeter Maydell * a BPR of 1 means they are [7:2], and so on down to 364df92cfa6SPeter Maydell * a BPR of 7 meaning no group priority bits at all. 365df92cfa6SPeter Maydell */ 366df92cfa6SPeter Maydell mask = ~0U << ((bpr & 7) + 1); 367df92cfa6SPeter Maydell 36886b350f0SLuc Michel return gic_get_priority(s, irq, cpu) & mask; 369df92cfa6SPeter Maydell } 370df92cfa6SPeter Maydell 37172889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq) 372e69954b9Spbrook { 37372889c8aSPeter Maydell /* Set the appropriate Active Priority Register bit for this IRQ, 37472889c8aSPeter Maydell * and update the running priority. 37572889c8aSPeter Maydell */ 37672889c8aSPeter Maydell int prio = gic_get_group_priority(s, cpu, irq); 377a1d7b8d8SLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 378a1d7b8d8SLuc Michel int preemption_level = prio >> (min_bpr + 1); 37972889c8aSPeter Maydell int regno = preemption_level / 32; 38072889c8aSPeter Maydell int bitno = preemption_level % 32; 381a1d7b8d8SLuc Michel uint32_t *papr = NULL; 38272889c8aSPeter Maydell 383a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 384a1d7b8d8SLuc Michel assert(regno == 0); 385a1d7b8d8SLuc Michel papr = &s->h_apr[gic_get_vcpu_real_id(cpu)]; 386a1d7b8d8SLuc Michel } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) { 387a1d7b8d8SLuc Michel papr = &s->nsapr[regno][cpu]; 3889ee6e8bbSpbrook } else { 389a1d7b8d8SLuc Michel papr = &s->apr[regno][cpu]; 3909ee6e8bbSpbrook } 39172889c8aSPeter Maydell 392a1d7b8d8SLuc Michel *papr |= (1 << bitno); 393a1d7b8d8SLuc Michel 39472889c8aSPeter Maydell s->running_priority[cpu] = prio; 39586b350f0SLuc Michel gic_set_active(s, irq, cpu); 39672889c8aSPeter Maydell } 39772889c8aSPeter Maydell 39872889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu) 39972889c8aSPeter Maydell { 40072889c8aSPeter Maydell /* Recalculate the current running priority for this CPU based 40172889c8aSPeter Maydell * on the set bits in the Active Priority Registers. 40272889c8aSPeter Maydell */ 40372889c8aSPeter Maydell int i; 404a1d7b8d8SLuc Michel 405a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 406a1d7b8d8SLuc Michel uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)]; 407a1d7b8d8SLuc Michel if (apr) { 408a1d7b8d8SLuc Michel return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1); 409a1d7b8d8SLuc Michel } else { 410a1d7b8d8SLuc Michel return 0x100; 411a1d7b8d8SLuc Michel } 412a1d7b8d8SLuc Michel } 413a1d7b8d8SLuc Michel 41472889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 41572889c8aSPeter Maydell uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu]; 41672889c8aSPeter Maydell if (!apr) { 41772889c8aSPeter Maydell continue; 41872889c8aSPeter Maydell } 41972889c8aSPeter Maydell return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1); 42072889c8aSPeter Maydell } 42172889c8aSPeter Maydell return 0x100; 42272889c8aSPeter Maydell } 42372889c8aSPeter Maydell 42472889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group) 42572889c8aSPeter Maydell { 42672889c8aSPeter Maydell /* Drop the priority of the currently active interrupt in the 42772889c8aSPeter Maydell * specified group. 42872889c8aSPeter Maydell * 42972889c8aSPeter Maydell * Note that we can guarantee (because of the requirement to nest 43072889c8aSPeter Maydell * GICC_IAR reads [which activate an interrupt and raise priority] 43172889c8aSPeter Maydell * with GICC_EOIR writes [which drop the priority for the interrupt]) 43272889c8aSPeter Maydell * that the interrupt we're being called for is the highest priority 43372889c8aSPeter Maydell * active interrupt, meaning that it has the lowest set bit in the 43472889c8aSPeter Maydell * APR registers. 43572889c8aSPeter Maydell * 43672889c8aSPeter Maydell * If the guest does not honour the ordering constraints then the 43772889c8aSPeter Maydell * behaviour of the GIC is UNPREDICTABLE, which for us means that 43872889c8aSPeter Maydell * the values of the APR registers might become incorrect and the 43972889c8aSPeter Maydell * running priority will be wrong, so interrupts that should preempt 44072889c8aSPeter Maydell * might not do so, and interrupts that should not preempt might do so. 44172889c8aSPeter Maydell */ 442a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 443a1d7b8d8SLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 444a1d7b8d8SLuc Michel 445a1d7b8d8SLuc Michel if (s->h_apr[rcpu]) { 446a1d7b8d8SLuc Michel /* Clear lowest set bit */ 447a1d7b8d8SLuc Michel s->h_apr[rcpu] &= s->h_apr[rcpu] - 1; 448a1d7b8d8SLuc Michel } 449a1d7b8d8SLuc Michel } else { 45072889c8aSPeter Maydell int i; 45172889c8aSPeter Maydell 45272889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 45372889c8aSPeter Maydell uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu]; 45472889c8aSPeter Maydell if (!*papr) { 45572889c8aSPeter Maydell continue; 45672889c8aSPeter Maydell } 45772889c8aSPeter Maydell /* Clear lowest set bit */ 45872889c8aSPeter Maydell *papr &= *papr - 1; 45972889c8aSPeter Maydell break; 46072889c8aSPeter Maydell } 461a1d7b8d8SLuc Michel } 46272889c8aSPeter Maydell 46372889c8aSPeter Maydell s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu); 464e69954b9Spbrook } 465e69954b9Spbrook 466439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu) 467e69954b9Spbrook { 468439badd6SLuc Michel int src; 469439badd6SLuc Michel uint32_t ret; 470c5619bf9SFabian Aggeler 471439badd6SLuc Michel if (!gic_is_vcpu(cpu)) { 47240d22500SChristoffer Dall /* Lookup the source CPU for the SGI and clear this in the 47340d22500SChristoffer Dall * sgi_pending map. Return the src and clear the overall pending 47440d22500SChristoffer Dall * state on this CPU if the SGI is not pending from any CPUs. 47540d22500SChristoffer Dall */ 47640d22500SChristoffer Dall assert(s->sgi_pending[irq][cpu] != 0); 47740d22500SChristoffer Dall src = ctz32(s->sgi_pending[irq][cpu]); 47840d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~(1 << src); 47940d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 48086b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 48140d22500SChristoffer Dall } 48240d22500SChristoffer Dall ret = irq | ((src & 0x7) << 10); 48340d22500SChristoffer Dall } else { 484439badd6SLuc Michel uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu); 485439badd6SLuc Michel src = GICH_LR_CPUID(*lr_entry); 486439badd6SLuc Michel 487439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 488439badd6SLuc Michel ret = irq | (src << 10); 489439badd6SLuc Michel } 490439badd6SLuc Michel 491439badd6SLuc Michel return ret; 492439badd6SLuc Michel } 493439badd6SLuc Michel 494439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs) 495439badd6SLuc Michel { 496439badd6SLuc Michel int ret, irq; 497439badd6SLuc Michel 498439badd6SLuc Michel /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately 499439badd6SLuc Michel * for the case where this GIC supports grouping and the pending interrupt 500439badd6SLuc Michel * is in the wrong group. 50140d22500SChristoffer Dall */ 502439badd6SLuc Michel irq = gic_get_current_pending_irq(s, cpu, attrs); 503439badd6SLuc Michel trace_gic_acknowledge_irq(gic_get_vcpu_real_id(cpu), irq); 504439badd6SLuc Michel 505439badd6SLuc Michel if (irq >= GIC_MAXIRQ) { 506439badd6SLuc Michel DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq); 507439badd6SLuc Michel return irq; 508439badd6SLuc Michel } 509439badd6SLuc Michel 510439badd6SLuc Michel if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) { 511439badd6SLuc Michel DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq); 512439badd6SLuc Michel return 1023; 513439badd6SLuc Michel } 514439badd6SLuc Michel 515439badd6SLuc Michel gic_activate_irq(s, cpu, irq); 516439badd6SLuc Michel 517439badd6SLuc Michel if (s->revision == REV_11MPCORE) { 518439badd6SLuc Michel /* Clear pending flags for both level and edge triggered interrupts. 519439badd6SLuc Michel * Level triggered IRQs will be reasserted once they become inactive. 520439badd6SLuc Michel */ 521439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 522439badd6SLuc Michel ret = irq; 523439badd6SLuc Michel } else { 524439badd6SLuc Michel if (irq < GIC_NR_SGIS) { 525439badd6SLuc Michel ret = gic_clear_pending_sgi(s, irq, cpu); 526439badd6SLuc Michel } else { 52786b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 52840d22500SChristoffer Dall ret = irq; 52940d22500SChristoffer Dall } 53040d22500SChristoffer Dall } 53140d22500SChristoffer Dall 532*cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 533*cbe1282bSLuc Michel gic_update_virt(s); 534*cbe1282bSLuc Michel } else { 53572889c8aSPeter Maydell gic_update(s); 536*cbe1282bSLuc Michel } 53740d22500SChristoffer Dall DPRINTF("ACK %d\n", irq); 53840d22500SChristoffer Dall return ret; 539e69954b9Spbrook } 540e69954b9Spbrook 54167ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val, 54281508470SFabian Aggeler MemTxAttrs attrs) 5439df90ad0SChristoffer Dall { 54481508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 54567ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 54681508470SFabian Aggeler return; /* Ignore Non-secure access of Group0 IRQ */ 54781508470SFabian Aggeler } 54881508470SFabian Aggeler val = 0x80 | (val >> 1); /* Non-secure view */ 54981508470SFabian Aggeler } 55081508470SFabian Aggeler 5519df90ad0SChristoffer Dall if (irq < GIC_INTERNAL) { 5529df90ad0SChristoffer Dall s->priority1[irq][cpu] = val; 5539df90ad0SChristoffer Dall } else { 5549df90ad0SChristoffer Dall s->priority2[(irq) - GIC_INTERNAL] = val; 5559df90ad0SChristoffer Dall } 5569df90ad0SChristoffer Dall } 5579df90ad0SChristoffer Dall 55867ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq, 55981508470SFabian Aggeler MemTxAttrs attrs) 56081508470SFabian Aggeler { 56167ce697aSLuc Michel uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu); 56281508470SFabian Aggeler 56381508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 56467ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 56581508470SFabian Aggeler return 0; /* Non-secure access cannot read priority of Group0 IRQ */ 56681508470SFabian Aggeler } 56781508470SFabian Aggeler prio = (prio << 1) & 0xff; /* Non-secure view */ 56881508470SFabian Aggeler } 56981508470SFabian Aggeler return prio; 57081508470SFabian Aggeler } 57181508470SFabian Aggeler 57281508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask, 57381508470SFabian Aggeler MemTxAttrs attrs) 57481508470SFabian Aggeler { 5753dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 57681508470SFabian Aggeler if (s->priority_mask[cpu] & 0x80) { 57781508470SFabian Aggeler /* Priority Mask in upper half */ 57881508470SFabian Aggeler pmask = 0x80 | (pmask >> 1); 57981508470SFabian Aggeler } else { 58081508470SFabian Aggeler /* Non-secure write ignored if priority mask is in lower half */ 58181508470SFabian Aggeler return; 58281508470SFabian Aggeler } 58381508470SFabian Aggeler } 58481508470SFabian Aggeler s->priority_mask[cpu] = pmask; 58581508470SFabian Aggeler } 58681508470SFabian Aggeler 58781508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs) 58881508470SFabian Aggeler { 58981508470SFabian Aggeler uint32_t pmask = s->priority_mask[cpu]; 59081508470SFabian Aggeler 5913dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 59281508470SFabian Aggeler if (pmask & 0x80) { 59381508470SFabian Aggeler /* Priority Mask in upper half, return Non-secure view */ 59481508470SFabian Aggeler pmask = (pmask << 1) & 0xff; 59581508470SFabian Aggeler } else { 59681508470SFabian Aggeler /* Priority Mask in lower half, RAZ */ 59781508470SFabian Aggeler pmask = 0; 59881508470SFabian Aggeler } 59981508470SFabian Aggeler } 60081508470SFabian Aggeler return pmask; 60181508470SFabian Aggeler } 60281508470SFabian Aggeler 60332951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs) 60432951860SFabian Aggeler { 60532951860SFabian Aggeler uint32_t ret = s->cpu_ctlr[cpu]; 60632951860SFabian Aggeler 6073dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 60832951860SFabian Aggeler /* Construct the NS banked view of GICC_CTLR from the correct 60932951860SFabian Aggeler * bits of the S banked view. We don't need to move the bypass 61032951860SFabian Aggeler * control bits because we don't implement that (IMPDEF) part 61132951860SFabian Aggeler * of the GIC architecture. 61232951860SFabian Aggeler */ 61332951860SFabian Aggeler ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1; 61432951860SFabian Aggeler } 61532951860SFabian Aggeler return ret; 61632951860SFabian Aggeler } 61732951860SFabian Aggeler 61832951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value, 61932951860SFabian Aggeler MemTxAttrs attrs) 62032951860SFabian Aggeler { 62132951860SFabian Aggeler uint32_t mask; 62232951860SFabian Aggeler 6233dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 62432951860SFabian Aggeler /* The NS view can only write certain bits in the register; 62532951860SFabian Aggeler * the rest are unchanged 62632951860SFabian Aggeler */ 62732951860SFabian Aggeler mask = GICC_CTLR_EN_GRP1; 62832951860SFabian Aggeler if (s->revision == 2) { 62932951860SFabian Aggeler mask |= GICC_CTLR_EOIMODE_NS; 63032951860SFabian Aggeler } 63132951860SFabian Aggeler s->cpu_ctlr[cpu] &= ~mask; 63232951860SFabian Aggeler s->cpu_ctlr[cpu] |= (value << 1) & mask; 63332951860SFabian Aggeler } else { 63432951860SFabian Aggeler if (s->revision == 2) { 63532951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK; 63632951860SFabian Aggeler } else { 63732951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK; 63832951860SFabian Aggeler } 63932951860SFabian Aggeler s->cpu_ctlr[cpu] = value & mask; 64032951860SFabian Aggeler } 64132951860SFabian Aggeler DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, " 64232951860SFabian Aggeler "Group1 Interrupts %sabled\n", cpu, 64332951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis", 64432951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis"); 64532951860SFabian Aggeler } 64632951860SFabian Aggeler 64708efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs) 64808efa9f2SFabian Aggeler { 64971aa735bSLuc MICHEL if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) { 65071aa735bSLuc MICHEL /* Idle priority */ 65171aa735bSLuc MICHEL return 0xff; 65271aa735bSLuc MICHEL } 65371aa735bSLuc MICHEL 6543dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 65508efa9f2SFabian Aggeler if (s->running_priority[cpu] & 0x80) { 65608efa9f2SFabian Aggeler /* Running priority in upper half of range: return the Non-secure 65708efa9f2SFabian Aggeler * view of the priority. 65808efa9f2SFabian Aggeler */ 65908efa9f2SFabian Aggeler return s->running_priority[cpu] << 1; 66008efa9f2SFabian Aggeler } else { 66108efa9f2SFabian Aggeler /* Running priority in lower half of range: RAZ */ 66208efa9f2SFabian Aggeler return 0; 66308efa9f2SFabian Aggeler } 66408efa9f2SFabian Aggeler } else { 66508efa9f2SFabian Aggeler return s->running_priority[cpu]; 66608efa9f2SFabian Aggeler } 66708efa9f2SFabian Aggeler } 66808efa9f2SFabian Aggeler 669a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation, 670a55c910eSPeter Maydell * ie whether the relevant EOIMode bit is set. 671a55c910eSPeter Maydell */ 672a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs) 673a55c910eSPeter Maydell { 674a55c910eSPeter Maydell if (s->revision != 2) { 675a55c910eSPeter Maydell /* Before GICv2 prio-drop and deactivate are not separable */ 676a55c910eSPeter Maydell return false; 677a55c910eSPeter Maydell } 6783dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 679a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS; 680a55c910eSPeter Maydell } 681a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE; 682a55c910eSPeter Maydell } 683a55c910eSPeter Maydell 684a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 685a55c910eSPeter Maydell { 686ee03cca8SPeter Maydell int group; 687ee03cca8SPeter Maydell 68802f2e22dSLuc Michel if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) { 689ee03cca8SPeter Maydell /* 690ee03cca8SPeter Maydell * This handles two cases: 691ee03cca8SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 692ee03cca8SPeter Maydell * to the GICC_DIR, the GIC ignores that write. 693ee03cca8SPeter Maydell * 2. If software writes the number of a non-existent interrupt 694ee03cca8SPeter Maydell * this must be a subcase of "value written is not an active interrupt" 69502f2e22dSLuc Michel * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs, 69602f2e22dSLuc Michel * all IRQs potentially exist, so this limit does not apply. 697ee03cca8SPeter Maydell */ 698ee03cca8SPeter Maydell return; 699ee03cca8SPeter Maydell } 700ee03cca8SPeter Maydell 701a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 702a55c910eSPeter Maydell /* This is UNPREDICTABLE; we choose to ignore it */ 703a55c910eSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 704a55c910eSPeter Maydell "gic_deactivate_irq: GICC_DIR write when EOIMode clear"); 705a55c910eSPeter Maydell return; 706a55c910eSPeter Maydell } 707a55c910eSPeter Maydell 70802f2e22dSLuc Michel if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) { 70902f2e22dSLuc Michel /* This vIRQ does not have an LR entry which is either active or 71002f2e22dSLuc Michel * pending and active. Increment EOICount and ignore the write. 71102f2e22dSLuc Michel */ 71202f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 71302f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 714*cbe1282bSLuc Michel 715*cbe1282bSLuc Michel /* Update the virtual interface in case a maintenance interrupt should 716*cbe1282bSLuc Michel * be raised. 717*cbe1282bSLuc Michel */ 718*cbe1282bSLuc Michel gic_update_virt(s); 71902f2e22dSLuc Michel return; 72002f2e22dSLuc Michel } 72102f2e22dSLuc Michel 72202f2e22dSLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 72302f2e22dSLuc Michel 7243dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 725a55c910eSPeter Maydell DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq); 726a55c910eSPeter Maydell return; 727a55c910eSPeter Maydell } 728a55c910eSPeter Maydell 72986b350f0SLuc Michel gic_clear_active(s, irq, cpu); 730a55c910eSPeter Maydell } 731a55c910eSPeter Maydell 73250491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 733e69954b9Spbrook { 7349ee6e8bbSpbrook int cm = 1 << cpu; 73572889c8aSPeter Maydell int group; 73672889c8aSPeter Maydell 737df628ff1Spbrook DPRINTF("EOI %d\n", irq); 73802f2e22dSLuc Michel if (gic_is_vcpu(cpu)) { 73902f2e22dSLuc Michel /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the 74002f2e22dSLuc Michel * running prio is < 0x100. 74102f2e22dSLuc Michel */ 74202f2e22dSLuc Michel bool prio_drop = s->running_priority[cpu] < 0x100; 74302f2e22dSLuc Michel 74402f2e22dSLuc Michel if (irq >= GIC_MAXIRQ) { 74502f2e22dSLuc Michel /* Ignore spurious interrupt */ 74602f2e22dSLuc Michel return; 74702f2e22dSLuc Michel } 74802f2e22dSLuc Michel 74902f2e22dSLuc Michel gic_drop_prio(s, cpu, 0); 75002f2e22dSLuc Michel 75102f2e22dSLuc Michel if (!gic_eoi_split(s, cpu, attrs)) { 75202f2e22dSLuc Michel bool valid = gic_virq_is_valid(s, irq, cpu); 75302f2e22dSLuc Michel if (prio_drop && !valid) { 75402f2e22dSLuc Michel /* We are in a situation where: 75502f2e22dSLuc Michel * - V_CTRL.EOIMode is false (no EOI split), 75602f2e22dSLuc Michel * - The call to gic_drop_prio() cleared a bit in GICH_APR, 75702f2e22dSLuc Michel * - This vIRQ does not have an LR entry which is either 75802f2e22dSLuc Michel * active or pending and active. 75902f2e22dSLuc Michel * In that case, we must increment EOICount. 76002f2e22dSLuc Michel */ 76102f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 76202f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 76302f2e22dSLuc Michel } else if (valid) { 76402f2e22dSLuc Michel gic_clear_active(s, irq, cpu); 76502f2e22dSLuc Michel } 76602f2e22dSLuc Michel } 76702f2e22dSLuc Michel 768*cbe1282bSLuc Michel gic_update_virt(s); 76902f2e22dSLuc Michel return; 77002f2e22dSLuc Michel } 77102f2e22dSLuc Michel 772a32134aaSMark Langsdorf if (irq >= s->num_irq) { 773217bfb44SPeter Maydell /* This handles two cases: 774217bfb44SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 775217bfb44SPeter Maydell * to the GICC_EOIR, the GIC ignores that write. 776217bfb44SPeter Maydell * 2. If software writes the number of a non-existent interrupt 777217bfb44SPeter Maydell * this must be a subcase of "value written does not match the last 778217bfb44SPeter Maydell * valid interrupt value read from the Interrupt Acknowledge 779217bfb44SPeter Maydell * register" and so this is UNPREDICTABLE. We choose to ignore it. 780217bfb44SPeter Maydell */ 781217bfb44SPeter Maydell return; 782217bfb44SPeter Maydell } 78372889c8aSPeter Maydell if (s->running_priority[cpu] == 0x100) { 784e69954b9Spbrook return; /* No active IRQ. */ 78572889c8aSPeter Maydell } 7868d999995SChristoffer Dall 7873bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 788e69954b9Spbrook /* Mark level triggered interrupts as pending if they are still 789e69954b9Spbrook raised. */ 79067ce697aSLuc Michel if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm) 79167ce697aSLuc Michel && GIC_DIST_TEST_LEVEL(irq, cm) 79267ce697aSLuc Michel && (GIC_DIST_TARGET(irq) & cm) != 0) { 7939ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq, cm); 79467ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, cm); 795e69954b9Spbrook } 7968d999995SChristoffer Dall } 7978d999995SChristoffer Dall 79886b350f0SLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 79972889c8aSPeter Maydell 8003dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 801f9c6a7f1SFabian Aggeler DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq); 802f9c6a7f1SFabian Aggeler return; 803f9c6a7f1SFabian Aggeler } 804f9c6a7f1SFabian Aggeler 805f9c6a7f1SFabian Aggeler /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1 806f9c6a7f1SFabian Aggeler * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1, 807f9c6a7f1SFabian Aggeler * i.e. go ahead and complete the irq anyway. 808f9c6a7f1SFabian Aggeler */ 809f9c6a7f1SFabian Aggeler 81072889c8aSPeter Maydell gic_drop_prio(s, cpu, group); 811a55c910eSPeter Maydell 812a55c910eSPeter Maydell /* In GICv2 the guest can choose to split priority-drop and deactivate */ 813a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 81486b350f0SLuc Michel gic_clear_active(s, irq, cpu); 815a55c910eSPeter Maydell } 816e69954b9Spbrook gic_update(s); 817e69954b9Spbrook } 818e69954b9Spbrook 819a9d85353SPeter Maydell static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs) 820e69954b9Spbrook { 821fae15286SPeter Maydell GICState *s = (GICState *)opaque; 822e69954b9Spbrook uint32_t res; 823e69954b9Spbrook int irq; 824e69954b9Spbrook int i; 8259ee6e8bbSpbrook int cpu; 8269ee6e8bbSpbrook int cm; 8279ee6e8bbSpbrook int mask; 828e69954b9Spbrook 829926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 8309ee6e8bbSpbrook cm = 1 << cpu; 831e69954b9Spbrook if (offset < 0x100) { 832679aa175SFabian Aggeler if (offset == 0) { /* GICD_CTLR */ 833679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 834679aa175SFabian Aggeler /* The NS bank of this register is just an alias of the 835679aa175SFabian Aggeler * EnableGrp1 bit in the S bank version. 836679aa175SFabian Aggeler */ 837679aa175SFabian Aggeler return extract32(s->ctlr, 1, 1); 838679aa175SFabian Aggeler } else { 839679aa175SFabian Aggeler return s->ctlr; 840679aa175SFabian Aggeler } 841679aa175SFabian Aggeler } 842e69954b9Spbrook if (offset == 4) 8435543d1abSFabian Aggeler /* Interrupt Controller Type Register */ 8445543d1abSFabian Aggeler return ((s->num_irq / 32) - 1) 845b95690c9SWei Huang | ((s->num_cpu - 1) << 5) 8465543d1abSFabian Aggeler | (s->security_extn << 10); 847e69954b9Spbrook if (offset < 0x08) 848e69954b9Spbrook return 0; 849b79f2265SRob Herring if (offset >= 0x80) { 850c27a5ba9SFabian Aggeler /* Interrupt Group Registers: these RAZ/WI if this is an NS 851c27a5ba9SFabian Aggeler * access to a GIC with the security extensions, or if the GIC 852c27a5ba9SFabian Aggeler * doesn't have groups at all. 853c27a5ba9SFabian Aggeler */ 854c27a5ba9SFabian Aggeler res = 0; 855c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 856c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 857c27a5ba9SFabian Aggeler irq = (offset - 0x080) * 8 + GIC_BASE_IRQ; 858c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 859c27a5ba9SFabian Aggeler goto bad_reg; 860c27a5ba9SFabian Aggeler } 861c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 86267ce697aSLuc Michel if (GIC_DIST_TEST_GROUP(irq + i, cm)) { 863c27a5ba9SFabian Aggeler res |= (1 << i); 864c27a5ba9SFabian Aggeler } 865c27a5ba9SFabian Aggeler } 866c27a5ba9SFabian Aggeler } 867c27a5ba9SFabian Aggeler return res; 868b79f2265SRob Herring } 869e69954b9Spbrook goto bad_reg; 870e69954b9Spbrook } else if (offset < 0x200) { 871e69954b9Spbrook /* Interrupt Set/Clear Enable. */ 872e69954b9Spbrook if (offset < 0x180) 873e69954b9Spbrook irq = (offset - 0x100) * 8; 874e69954b9Spbrook else 875e69954b9Spbrook irq = (offset - 0x180) * 8; 8769ee6e8bbSpbrook irq += GIC_BASE_IRQ; 877a32134aaSMark Langsdorf if (irq >= s->num_irq) 878e69954b9Spbrook goto bad_reg; 879e69954b9Spbrook res = 0; 880e69954b9Spbrook for (i = 0; i < 8; i++) { 881fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 88267ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 883fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 884fea8a08eSJens Wiklander } 885fea8a08eSJens Wiklander 88667ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 887e69954b9Spbrook res |= (1 << i); 888e69954b9Spbrook } 889e69954b9Spbrook } 890e69954b9Spbrook } else if (offset < 0x300) { 891e69954b9Spbrook /* Interrupt Set/Clear Pending. */ 892e69954b9Spbrook if (offset < 0x280) 893e69954b9Spbrook irq = (offset - 0x200) * 8; 894e69954b9Spbrook else 895e69954b9Spbrook irq = (offset - 0x280) * 8; 8969ee6e8bbSpbrook irq += GIC_BASE_IRQ; 897a32134aaSMark Langsdorf if (irq >= s->num_irq) 898e69954b9Spbrook goto bad_reg; 899e69954b9Spbrook res = 0; 90069253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 901e69954b9Spbrook for (i = 0; i < 8; i++) { 902fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 90367ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 904fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 905fea8a08eSJens Wiklander } 906fea8a08eSJens Wiklander 9078d999995SChristoffer Dall if (gic_test_pending(s, irq + i, mask)) { 908e69954b9Spbrook res |= (1 << i); 909e69954b9Spbrook } 910e69954b9Spbrook } 911e69954b9Spbrook } else if (offset < 0x400) { 9123bb0b038SLuc Michel /* Interrupt Set/Clear Active. */ 9133bb0b038SLuc Michel if (offset < 0x380) { 9143bb0b038SLuc Michel irq = (offset - 0x300) * 8; 9153bb0b038SLuc Michel } else if (s->revision == 2) { 9163bb0b038SLuc Michel irq = (offset - 0x380) * 8; 9173bb0b038SLuc Michel } else { 9183bb0b038SLuc Michel goto bad_reg; 9193bb0b038SLuc Michel } 9203bb0b038SLuc Michel 9213bb0b038SLuc Michel irq += GIC_BASE_IRQ; 922a32134aaSMark Langsdorf if (irq >= s->num_irq) 923e69954b9Spbrook goto bad_reg; 924e69954b9Spbrook res = 0; 92569253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 926e69954b9Spbrook for (i = 0; i < 8; i++) { 927fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 92867ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 929fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 930fea8a08eSJens Wiklander } 931fea8a08eSJens Wiklander 93267ce697aSLuc Michel if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) { 933e69954b9Spbrook res |= (1 << i); 934e69954b9Spbrook } 935e69954b9Spbrook } 936e69954b9Spbrook } else if (offset < 0x800) { 937e69954b9Spbrook /* Interrupt Priority. */ 9389ee6e8bbSpbrook irq = (offset - 0x400) + GIC_BASE_IRQ; 939a32134aaSMark Langsdorf if (irq >= s->num_irq) 940e69954b9Spbrook goto bad_reg; 94167ce697aSLuc Michel res = gic_dist_get_priority(s, cpu, irq, attrs); 942e69954b9Spbrook } else if (offset < 0xc00) { 943e69954b9Spbrook /* Interrupt CPU Target. */ 9446b9680bbSPeter Maydell if (s->num_cpu == 1 && s->revision != REV_11MPCORE) { 9456b9680bbSPeter Maydell /* For uniprocessor GICs these RAZ/WI */ 9466b9680bbSPeter Maydell res = 0; 9476b9680bbSPeter Maydell } else { 9489ee6e8bbSpbrook irq = (offset - 0x800) + GIC_BASE_IRQ; 9496b9680bbSPeter Maydell if (irq >= s->num_irq) { 950e69954b9Spbrook goto bad_reg; 9516b9680bbSPeter Maydell } 9527995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 9537995206dSPeter Maydell res = 0; 9547995206dSPeter Maydell } else if (irq < GIC_INTERNAL) { 9559ee6e8bbSpbrook res = cm; 9569ee6e8bbSpbrook } else { 95767ce697aSLuc Michel res = GIC_DIST_TARGET(irq); 9589ee6e8bbSpbrook } 9596b9680bbSPeter Maydell } 960e69954b9Spbrook } else if (offset < 0xf00) { 961e69954b9Spbrook /* Interrupt Configuration. */ 96271a62046SAdam Lackorzynski irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ; 963a32134aaSMark Langsdorf if (irq >= s->num_irq) 964e69954b9Spbrook goto bad_reg; 965e69954b9Spbrook res = 0; 966e69954b9Spbrook for (i = 0; i < 4; i++) { 967fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 96867ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 969fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 970fea8a08eSJens Wiklander } 971fea8a08eSJens Wiklander 97267ce697aSLuc Michel if (GIC_DIST_TEST_MODEL(irq + i)) { 973e69954b9Spbrook res |= (1 << (i * 2)); 97467ce697aSLuc Michel } 97567ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 976e69954b9Spbrook res |= (2 << (i * 2)); 977e69954b9Spbrook } 97867ce697aSLuc Michel } 97940d22500SChristoffer Dall } else if (offset < 0xf10) { 98040d22500SChristoffer Dall goto bad_reg; 98140d22500SChristoffer Dall } else if (offset < 0xf30) { 9827c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 98340d22500SChristoffer Dall goto bad_reg; 98440d22500SChristoffer Dall } 98540d22500SChristoffer Dall 98640d22500SChristoffer Dall if (offset < 0xf20) { 98740d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 98840d22500SChristoffer Dall irq = (offset - 0xf10); 98940d22500SChristoffer Dall } else { 99040d22500SChristoffer Dall irq = (offset - 0xf20); 99140d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 99240d22500SChristoffer Dall } 99340d22500SChristoffer Dall 994fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 99567ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 996fea8a08eSJens Wiklander res = 0; /* Ignore Non-secure access of Group0 IRQ */ 997fea8a08eSJens Wiklander } else { 99840d22500SChristoffer Dall res = s->sgi_pending[irq][cpu]; 999fea8a08eSJens Wiklander } 10003355c360SAlistair Francis } else if (offset < 0xfd0) { 1001e69954b9Spbrook goto bad_reg; 10023355c360SAlistair Francis } else if (offset < 0x1000) { 1003e69954b9Spbrook if (offset & 3) { 1004e69954b9Spbrook res = 0; 1005e69954b9Spbrook } else { 10063355c360SAlistair Francis switch (s->revision) { 10073355c360SAlistair Francis case REV_11MPCORE: 10083355c360SAlistair Francis res = gic_id_11mpcore[(offset - 0xfd0) >> 2]; 10093355c360SAlistair Francis break; 10103355c360SAlistair Francis case 1: 10113355c360SAlistair Francis res = gic_id_gicv1[(offset - 0xfd0) >> 2]; 10123355c360SAlistair Francis break; 10133355c360SAlistair Francis case 2: 10143355c360SAlistair Francis res = gic_id_gicv2[(offset - 0xfd0) >> 2]; 10153355c360SAlistair Francis break; 10163355c360SAlistair Francis default: 10173355c360SAlistair Francis res = 0; 1018e69954b9Spbrook } 1019e69954b9Spbrook } 10203355c360SAlistair Francis } else { 10213355c360SAlistair Francis g_assert_not_reached(); 10223355c360SAlistair Francis } 1023e69954b9Spbrook return res; 1024e69954b9Spbrook bad_reg: 10258c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 10268c8dc39fSPeter Maydell "gic_dist_readb: Bad offset %x\n", (int)offset); 1027e69954b9Spbrook return 0; 1028e69954b9Spbrook } 1029e69954b9Spbrook 1030a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data, 1031a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1032e69954b9Spbrook { 1033a9d85353SPeter Maydell switch (size) { 1034a9d85353SPeter Maydell case 1: 1035a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1036a9d85353SPeter Maydell return MEMTX_OK; 1037a9d85353SPeter Maydell case 2: 1038a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1039a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1040a9d85353SPeter Maydell return MEMTX_OK; 1041a9d85353SPeter Maydell case 4: 1042a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1043a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1044a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16; 1045a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24; 1046a9d85353SPeter Maydell return MEMTX_OK; 1047a9d85353SPeter Maydell default: 1048a9d85353SPeter Maydell return MEMTX_ERROR; 1049e69954b9Spbrook } 1050e69954b9Spbrook } 1051e69954b9Spbrook 1052a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset, 1053a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1054e69954b9Spbrook { 1055fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1056e69954b9Spbrook int irq; 1057e69954b9Spbrook int i; 10589ee6e8bbSpbrook int cpu; 1059e69954b9Spbrook 1060926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 1061e69954b9Spbrook if (offset < 0x100) { 1062e69954b9Spbrook if (offset == 0) { 1063679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 1064679aa175SFabian Aggeler /* NS version is just an alias of the S version's bit 1 */ 1065679aa175SFabian Aggeler s->ctlr = deposit32(s->ctlr, 1, 1, value); 1066679aa175SFabian Aggeler } else if (gic_has_groups(s)) { 1067679aa175SFabian Aggeler s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1); 1068679aa175SFabian Aggeler } else { 1069679aa175SFabian Aggeler s->ctlr = value & GICD_CTLR_EN_GRP0; 1070679aa175SFabian Aggeler } 1071679aa175SFabian Aggeler DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n", 1072679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis", 1073679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis"); 1074e69954b9Spbrook } else if (offset < 4) { 1075e69954b9Spbrook /* ignored. */ 1076b79f2265SRob Herring } else if (offset >= 0x80) { 1077c27a5ba9SFabian Aggeler /* Interrupt Group Registers: RAZ/WI for NS access to secure 1078c27a5ba9SFabian Aggeler * GIC, or for GICs without groups. 1079c27a5ba9SFabian Aggeler */ 1080c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 1081c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 1082c27a5ba9SFabian Aggeler irq = (offset - 0x80) * 8 + GIC_BASE_IRQ; 1083c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 1084c27a5ba9SFabian Aggeler goto bad_reg; 1085c27a5ba9SFabian Aggeler } 1086c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 1087c27a5ba9SFabian Aggeler /* Group bits are banked for private interrupts */ 1088c27a5ba9SFabian Aggeler int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 1089c27a5ba9SFabian Aggeler if (value & (1 << i)) { 1090c27a5ba9SFabian Aggeler /* Group1 (Non-secure) */ 109167ce697aSLuc Michel GIC_DIST_SET_GROUP(irq + i, cm); 1092c27a5ba9SFabian Aggeler } else { 1093c27a5ba9SFabian Aggeler /* Group0 (Secure) */ 109467ce697aSLuc Michel GIC_DIST_CLEAR_GROUP(irq + i, cm); 1095c27a5ba9SFabian Aggeler } 1096c27a5ba9SFabian Aggeler } 1097c27a5ba9SFabian Aggeler } 1098e69954b9Spbrook } else { 1099e69954b9Spbrook goto bad_reg; 1100e69954b9Spbrook } 1101e69954b9Spbrook } else if (offset < 0x180) { 1102e69954b9Spbrook /* Interrupt Set Enable. */ 11039ee6e8bbSpbrook irq = (offset - 0x100) * 8 + GIC_BASE_IRQ; 1104a32134aaSMark Langsdorf if (irq >= s->num_irq) 1105e69954b9Spbrook goto bad_reg; 110641ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 11079ee6e8bbSpbrook value = 0xff; 110841ab7b55SChristoffer Dall } 110941ab7b55SChristoffer Dall 1110e69954b9Spbrook for (i = 0; i < 8; i++) { 1111e69954b9Spbrook if (value & (1 << i)) { 1112f47b48fbSDaniel Sangorrin int mask = 111367ce697aSLuc Michel (irq < GIC_INTERNAL) ? (1 << cpu) 111467ce697aSLuc Michel : GIC_DIST_TARGET(irq + i); 111569253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 111641bf234dSRabin Vincent 1117fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 111867ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1119fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1120fea8a08eSJens Wiklander } 1121fea8a08eSJens Wiklander 112267ce697aSLuc Michel if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1123e69954b9Spbrook DPRINTF("Enabled IRQ %d\n", irq + i); 11242531088fSHollis Blanchard trace_gic_enable_irq(irq + i); 112541bf234dSRabin Vincent } 112667ce697aSLuc Michel GIC_DIST_SET_ENABLED(irq + i, cm); 1127e69954b9Spbrook /* If a raised level triggered IRQ enabled then mark 1128e69954b9Spbrook is as pending. */ 112967ce697aSLuc Michel if (GIC_DIST_TEST_LEVEL(irq + i, mask) 113067ce697aSLuc Michel && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 11319ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq + i, mask); 113267ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, mask); 11339ee6e8bbSpbrook } 1134e69954b9Spbrook } 1135e69954b9Spbrook } 1136e69954b9Spbrook } else if (offset < 0x200) { 1137e69954b9Spbrook /* Interrupt Clear Enable. */ 11389ee6e8bbSpbrook irq = (offset - 0x180) * 8 + GIC_BASE_IRQ; 1139a32134aaSMark Langsdorf if (irq >= s->num_irq) 1140e69954b9Spbrook goto bad_reg; 114141ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 11429ee6e8bbSpbrook value = 0; 114341ab7b55SChristoffer Dall } 114441ab7b55SChristoffer Dall 1145e69954b9Spbrook for (i = 0; i < 8; i++) { 1146e69954b9Spbrook if (value & (1 << i)) { 114769253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 114841bf234dSRabin Vincent 1149fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 115067ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1151fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1152fea8a08eSJens Wiklander } 1153fea8a08eSJens Wiklander 115467ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1155e69954b9Spbrook DPRINTF("Disabled IRQ %d\n", irq + i); 11562531088fSHollis Blanchard trace_gic_disable_irq(irq + i); 115741bf234dSRabin Vincent } 115867ce697aSLuc Michel GIC_DIST_CLEAR_ENABLED(irq + i, cm); 1159e69954b9Spbrook } 1160e69954b9Spbrook } 1161e69954b9Spbrook } else if (offset < 0x280) { 1162e69954b9Spbrook /* Interrupt Set Pending. */ 11639ee6e8bbSpbrook irq = (offset - 0x200) * 8 + GIC_BASE_IRQ; 1164a32134aaSMark Langsdorf if (irq >= s->num_irq) 1165e69954b9Spbrook goto bad_reg; 116641ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 11675b0adce1SChristoffer Dall value = 0; 116841ab7b55SChristoffer Dall } 11699ee6e8bbSpbrook 1170e69954b9Spbrook for (i = 0; i < 8; i++) { 1171e69954b9Spbrook if (value & (1 << i)) { 1172fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 117367ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1174fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1175fea8a08eSJens Wiklander } 1176fea8a08eSJens Wiklander 117767ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i)); 1178e69954b9Spbrook } 1179e69954b9Spbrook } 1180e69954b9Spbrook } else if (offset < 0x300) { 1181e69954b9Spbrook /* Interrupt Clear Pending. */ 11829ee6e8bbSpbrook irq = (offset - 0x280) * 8 + GIC_BASE_IRQ; 1183a32134aaSMark Langsdorf if (irq >= s->num_irq) 1184e69954b9Spbrook goto bad_reg; 11855b0adce1SChristoffer Dall if (irq < GIC_NR_SGIS) { 11865b0adce1SChristoffer Dall value = 0; 11875b0adce1SChristoffer Dall } 11885b0adce1SChristoffer Dall 1189e69954b9Spbrook for (i = 0; i < 8; i++) { 1190fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 119167ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1192fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1193fea8a08eSJens Wiklander } 1194fea8a08eSJens Wiklander 11959ee6e8bbSpbrook /* ??? This currently clears the pending bit for all CPUs, even 11969ee6e8bbSpbrook for per-CPU interrupts. It's unclear whether this is the 11979ee6e8bbSpbrook corect behavior. */ 1198e69954b9Spbrook if (value & (1 << i)) { 119967ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK); 1200e69954b9Spbrook } 1201e69954b9Spbrook } 12023bb0b038SLuc Michel } else if (offset < 0x380) { 12033bb0b038SLuc Michel /* Interrupt Set Active. */ 12043bb0b038SLuc Michel if (s->revision != 2) { 1205e69954b9Spbrook goto bad_reg; 12063bb0b038SLuc Michel } 12073bb0b038SLuc Michel 12083bb0b038SLuc Michel irq = (offset - 0x300) * 8 + GIC_BASE_IRQ; 12093bb0b038SLuc Michel if (irq >= s->num_irq) { 12103bb0b038SLuc Michel goto bad_reg; 12113bb0b038SLuc Michel } 12123bb0b038SLuc Michel 12133bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 12143bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 12153bb0b038SLuc Michel 12163bb0b038SLuc Michel for (i = 0; i < 8; i++) { 12173bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 12183bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 12193bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 12203bb0b038SLuc Michel } 12213bb0b038SLuc Michel 12223bb0b038SLuc Michel if (value & (1 << i)) { 12233bb0b038SLuc Michel GIC_DIST_SET_ACTIVE(irq + i, cm); 12243bb0b038SLuc Michel } 12253bb0b038SLuc Michel } 12263bb0b038SLuc Michel } else if (offset < 0x400) { 12273bb0b038SLuc Michel /* Interrupt Clear Active. */ 12283bb0b038SLuc Michel if (s->revision != 2) { 12293bb0b038SLuc Michel goto bad_reg; 12303bb0b038SLuc Michel } 12313bb0b038SLuc Michel 12323bb0b038SLuc Michel irq = (offset - 0x380) * 8 + GIC_BASE_IRQ; 12333bb0b038SLuc Michel if (irq >= s->num_irq) { 12343bb0b038SLuc Michel goto bad_reg; 12353bb0b038SLuc Michel } 12363bb0b038SLuc Michel 12373bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 12383bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 12393bb0b038SLuc Michel 12403bb0b038SLuc Michel for (i = 0; i < 8; i++) { 12413bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 12423bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 12433bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 12443bb0b038SLuc Michel } 12453bb0b038SLuc Michel 12463bb0b038SLuc Michel if (value & (1 << i)) { 12473bb0b038SLuc Michel GIC_DIST_CLEAR_ACTIVE(irq + i, cm); 12483bb0b038SLuc Michel } 12493bb0b038SLuc Michel } 1250e69954b9Spbrook } else if (offset < 0x800) { 1251e69954b9Spbrook /* Interrupt Priority. */ 12529ee6e8bbSpbrook irq = (offset - 0x400) + GIC_BASE_IRQ; 1253a32134aaSMark Langsdorf if (irq >= s->num_irq) 1254e69954b9Spbrook goto bad_reg; 125567ce697aSLuc Michel gic_dist_set_priority(s, cpu, irq, value, attrs); 1256e69954b9Spbrook } else if (offset < 0xc00) { 12576b9680bbSPeter Maydell /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the 12586b9680bbSPeter Maydell * annoying exception of the 11MPCore's GIC. 12596b9680bbSPeter Maydell */ 12606b9680bbSPeter Maydell if (s->num_cpu != 1 || s->revision == REV_11MPCORE) { 12619ee6e8bbSpbrook irq = (offset - 0x800) + GIC_BASE_IRQ; 12626b9680bbSPeter Maydell if (irq >= s->num_irq) { 1263e69954b9Spbrook goto bad_reg; 12646b9680bbSPeter Maydell } 12657995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 12669ee6e8bbSpbrook value = 0; 12676b9680bbSPeter Maydell } else if (irq < GIC_INTERNAL) { 12689ee6e8bbSpbrook value = ALL_CPU_MASK; 12696b9680bbSPeter Maydell } 12709ee6e8bbSpbrook s->irq_target[irq] = value & ALL_CPU_MASK; 12716b9680bbSPeter Maydell } 1272e69954b9Spbrook } else if (offset < 0xf00) { 1273e69954b9Spbrook /* Interrupt Configuration. */ 12749ee6e8bbSpbrook irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ; 1275a32134aaSMark Langsdorf if (irq >= s->num_irq) 1276e69954b9Spbrook goto bad_reg; 1277de7a900fSAdam Lackorzynski if (irq < GIC_NR_SGIS) 12789ee6e8bbSpbrook value |= 0xaa; 1279e69954b9Spbrook for (i = 0; i < 4; i++) { 1280fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 128167ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1282fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1283fea8a08eSJens Wiklander } 1284fea8a08eSJens Wiklander 12857c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 1286e69954b9Spbrook if (value & (1 << (i * 2))) { 128767ce697aSLuc Michel GIC_DIST_SET_MODEL(irq + i); 1288e69954b9Spbrook } else { 128967ce697aSLuc Michel GIC_DIST_CLEAR_MODEL(irq + i); 1290e69954b9Spbrook } 129124b790dfSAdam Lackorzynski } 1292e69954b9Spbrook if (value & (2 << (i * 2))) { 129367ce697aSLuc Michel GIC_DIST_SET_EDGE_TRIGGER(irq + i); 1294e69954b9Spbrook } else { 129567ce697aSLuc Michel GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i); 1296e69954b9Spbrook } 1297e69954b9Spbrook } 129840d22500SChristoffer Dall } else if (offset < 0xf10) { 12999ee6e8bbSpbrook /* 0xf00 is only handled for 32-bit writes. */ 1300e69954b9Spbrook goto bad_reg; 130140d22500SChristoffer Dall } else if (offset < 0xf20) { 130240d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 13037c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 130440d22500SChristoffer Dall goto bad_reg; 130540d22500SChristoffer Dall } 130640d22500SChristoffer Dall irq = (offset - 0xf10); 130740d22500SChristoffer Dall 1308fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 130967ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 131040d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~value; 131140d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 131267ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq, 1 << cpu); 131340d22500SChristoffer Dall } 1314fea8a08eSJens Wiklander } 131540d22500SChristoffer Dall } else if (offset < 0xf30) { 131640d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 13177c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 131840d22500SChristoffer Dall goto bad_reg; 131940d22500SChristoffer Dall } 132040d22500SChristoffer Dall irq = (offset - 0xf20); 132140d22500SChristoffer Dall 1322fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 132367ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 132467ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, 1 << cpu); 132540d22500SChristoffer Dall s->sgi_pending[irq][cpu] |= value; 1326fea8a08eSJens Wiklander } 132740d22500SChristoffer Dall } else { 132840d22500SChristoffer Dall goto bad_reg; 1329e69954b9Spbrook } 1330e69954b9Spbrook gic_update(s); 1331e69954b9Spbrook return; 1332e69954b9Spbrook bad_reg: 13338c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 13348c8dc39fSPeter Maydell "gic_dist_writeb: Bad offset %x\n", (int)offset); 1335e69954b9Spbrook } 1336e69954b9Spbrook 1337a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset, 1338a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1339e69954b9Spbrook { 1340a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, value & 0xff, attrs); 1341a9d85353SPeter Maydell gic_dist_writeb(opaque, offset + 1, value >> 8, attrs); 1342e69954b9Spbrook } 1343e69954b9Spbrook 1344a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset, 1345a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1346e69954b9Spbrook { 1347fae15286SPeter Maydell GICState *s = (GICState *)opaque; 13488da3ff18Spbrook if (offset == 0xf00) { 13499ee6e8bbSpbrook int cpu; 13509ee6e8bbSpbrook int irq; 13519ee6e8bbSpbrook int mask; 135240d22500SChristoffer Dall int target_cpu; 13539ee6e8bbSpbrook 1354926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 13559ee6e8bbSpbrook irq = value & 0x3ff; 13569ee6e8bbSpbrook switch ((value >> 24) & 3) { 13579ee6e8bbSpbrook case 0: 13589ee6e8bbSpbrook mask = (value >> 16) & ALL_CPU_MASK; 13599ee6e8bbSpbrook break; 13609ee6e8bbSpbrook case 1: 1361fa250144SAdam Lackorzynski mask = ALL_CPU_MASK ^ (1 << cpu); 13629ee6e8bbSpbrook break; 13639ee6e8bbSpbrook case 2: 1364fa250144SAdam Lackorzynski mask = 1 << cpu; 13659ee6e8bbSpbrook break; 13669ee6e8bbSpbrook default: 13679ee6e8bbSpbrook DPRINTF("Bad Soft Int target filter\n"); 13689ee6e8bbSpbrook mask = ALL_CPU_MASK; 13699ee6e8bbSpbrook break; 13709ee6e8bbSpbrook } 137167ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, mask); 137240d22500SChristoffer Dall target_cpu = ctz32(mask); 137340d22500SChristoffer Dall while (target_cpu < GIC_NCPU) { 137440d22500SChristoffer Dall s->sgi_pending[irq][target_cpu] |= (1 << cpu); 137540d22500SChristoffer Dall mask &= ~(1 << target_cpu); 137640d22500SChristoffer Dall target_cpu = ctz32(mask); 137740d22500SChristoffer Dall } 13789ee6e8bbSpbrook gic_update(s); 13799ee6e8bbSpbrook return; 13809ee6e8bbSpbrook } 1381a9d85353SPeter Maydell gic_dist_writew(opaque, offset, value & 0xffff, attrs); 1382a9d85353SPeter Maydell gic_dist_writew(opaque, offset + 2, value >> 16, attrs); 1383a9d85353SPeter Maydell } 1384a9d85353SPeter Maydell 1385a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data, 1386a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1387a9d85353SPeter Maydell { 1388a9d85353SPeter Maydell switch (size) { 1389a9d85353SPeter Maydell case 1: 1390a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, data, attrs); 1391a9d85353SPeter Maydell return MEMTX_OK; 1392a9d85353SPeter Maydell case 2: 1393a9d85353SPeter Maydell gic_dist_writew(opaque, offset, data, attrs); 1394a9d85353SPeter Maydell return MEMTX_OK; 1395a9d85353SPeter Maydell case 4: 1396a9d85353SPeter Maydell gic_dist_writel(opaque, offset, data, attrs); 1397a9d85353SPeter Maydell return MEMTX_OK; 1398a9d85353SPeter Maydell default: 1399a9d85353SPeter Maydell return MEMTX_ERROR; 1400a9d85353SPeter Maydell } 1401e69954b9Spbrook } 1402e69954b9Spbrook 140351fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno) 140451fd06e0SPeter Maydell { 140551fd06e0SPeter Maydell /* Return the Nonsecure view of GICC_APR<regno>. This is the 140651fd06e0SPeter Maydell * second half of GICC_NSAPR. 140751fd06e0SPeter Maydell */ 140851fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 140951fd06e0SPeter Maydell case 0: 141051fd06e0SPeter Maydell if (regno < 2) { 141151fd06e0SPeter Maydell return s->nsapr[regno + 2][cpu]; 141251fd06e0SPeter Maydell } 141351fd06e0SPeter Maydell break; 141451fd06e0SPeter Maydell case 1: 141551fd06e0SPeter Maydell if (regno == 0) { 141651fd06e0SPeter Maydell return s->nsapr[regno + 1][cpu]; 141751fd06e0SPeter Maydell } 141851fd06e0SPeter Maydell break; 141951fd06e0SPeter Maydell case 2: 142051fd06e0SPeter Maydell if (regno == 0) { 142151fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 16, 16); 142251fd06e0SPeter Maydell } 142351fd06e0SPeter Maydell break; 142451fd06e0SPeter Maydell case 3: 142551fd06e0SPeter Maydell if (regno == 0) { 142651fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 8, 8); 142751fd06e0SPeter Maydell } 142851fd06e0SPeter Maydell break; 142951fd06e0SPeter Maydell default: 143051fd06e0SPeter Maydell g_assert_not_reached(); 143151fd06e0SPeter Maydell } 143251fd06e0SPeter Maydell return 0; 143351fd06e0SPeter Maydell } 143451fd06e0SPeter Maydell 143551fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno, 143651fd06e0SPeter Maydell uint32_t value) 143751fd06e0SPeter Maydell { 143851fd06e0SPeter Maydell /* Write the Nonsecure view of GICC_APR<regno>. */ 143951fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 144051fd06e0SPeter Maydell case 0: 144151fd06e0SPeter Maydell if (regno < 2) { 144251fd06e0SPeter Maydell s->nsapr[regno + 2][cpu] = value; 144351fd06e0SPeter Maydell } 144451fd06e0SPeter Maydell break; 144551fd06e0SPeter Maydell case 1: 144651fd06e0SPeter Maydell if (regno == 0) { 144751fd06e0SPeter Maydell s->nsapr[regno + 1][cpu] = value; 144851fd06e0SPeter Maydell } 144951fd06e0SPeter Maydell break; 145051fd06e0SPeter Maydell case 2: 145151fd06e0SPeter Maydell if (regno == 0) { 145251fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value); 145351fd06e0SPeter Maydell } 145451fd06e0SPeter Maydell break; 145551fd06e0SPeter Maydell case 3: 145651fd06e0SPeter Maydell if (regno == 0) { 145751fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value); 145851fd06e0SPeter Maydell } 145951fd06e0SPeter Maydell break; 146051fd06e0SPeter Maydell default: 146151fd06e0SPeter Maydell g_assert_not_reached(); 146251fd06e0SPeter Maydell } 146351fd06e0SPeter Maydell } 146451fd06e0SPeter Maydell 1465a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, 1466a9d85353SPeter Maydell uint64_t *data, MemTxAttrs attrs) 1467e69954b9Spbrook { 1468e69954b9Spbrook switch (offset) { 1469e69954b9Spbrook case 0x00: /* Control */ 147032951860SFabian Aggeler *data = gic_get_cpu_control(s, cpu, attrs); 1471a9d85353SPeter Maydell break; 1472e69954b9Spbrook case 0x04: /* Priority mask */ 147381508470SFabian Aggeler *data = gic_get_priority_mask(s, cpu, attrs); 1474a9d85353SPeter Maydell break; 1475e69954b9Spbrook case 0x08: /* Binary Point */ 14763dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1477421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1478421a3c22SLuc MICHEL /* NS view of BPR when CBPR is 1 */ 1479421a3c22SLuc MICHEL *data = MIN(s->bpr[cpu] + 1, 7); 1480421a3c22SLuc MICHEL } else { 1481822e9cc3SFabian Aggeler /* BPR is banked. Non-secure copy stored in ABPR. */ 1482822e9cc3SFabian Aggeler *data = s->abpr[cpu]; 1483421a3c22SLuc MICHEL } 1484822e9cc3SFabian Aggeler } else { 1485a9d85353SPeter Maydell *data = s->bpr[cpu]; 1486822e9cc3SFabian Aggeler } 1487a9d85353SPeter Maydell break; 1488e69954b9Spbrook case 0x0c: /* Acknowledge */ 1489c5619bf9SFabian Aggeler *data = gic_acknowledge_irq(s, cpu, attrs); 1490a9d85353SPeter Maydell break; 149166a0a2cbSDong Xu Wang case 0x14: /* Running Priority */ 149208efa9f2SFabian Aggeler *data = gic_get_running_priority(s, cpu, attrs); 1493a9d85353SPeter Maydell break; 1494e69954b9Spbrook case 0x18: /* Highest Pending Interrupt */ 14957c0fa108SFabian Aggeler *data = gic_get_current_pending_irq(s, cpu, attrs); 1496a9d85353SPeter Maydell break; 1497aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 1498822e9cc3SFabian Aggeler /* GIC v2, no security: ABPR 1499822e9cc3SFabian Aggeler * GIC v1, no security: not implemented (RAZ/WI) 1500822e9cc3SFabian Aggeler * With security extensions, secure access: ABPR (alias of NS BPR) 1501822e9cc3SFabian Aggeler * With security extensions, nonsecure access: RAZ/WI 1502822e9cc3SFabian Aggeler */ 15033dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1504822e9cc3SFabian Aggeler *data = 0; 1505822e9cc3SFabian Aggeler } else { 1506a9d85353SPeter Maydell *data = s->abpr[cpu]; 1507822e9cc3SFabian Aggeler } 1508a9d85353SPeter Maydell break; 1509a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 151051fd06e0SPeter Maydell { 151151fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 15127eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 151351fd06e0SPeter Maydell 15147eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 151551fd06e0SPeter Maydell *data = 0; 15167eb079ecSLuc Michel } else if (gic_is_vcpu(cpu)) { 15177eb079ecSLuc Michel *data = s->h_apr[gic_get_vcpu_real_id(cpu)]; 15183dd0471bSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 151951fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 152051fd06e0SPeter Maydell *data = gic_apr_ns_view(s, regno, cpu); 152151fd06e0SPeter Maydell } else { 152251fd06e0SPeter Maydell *data = s->apr[regno][cpu]; 152351fd06e0SPeter Maydell } 1524a9d85353SPeter Maydell break; 152551fd06e0SPeter Maydell } 152651fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 152751fd06e0SPeter Maydell { 152851fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 152951fd06e0SPeter Maydell 153051fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) || 15317eb079ecSLuc Michel gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) { 153251fd06e0SPeter Maydell *data = 0; 153351fd06e0SPeter Maydell } else { 153451fd06e0SPeter Maydell *data = s->nsapr[regno][cpu]; 153551fd06e0SPeter Maydell } 153651fd06e0SPeter Maydell break; 153751fd06e0SPeter Maydell } 1538e69954b9Spbrook default: 15398c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 15408c8dc39fSPeter Maydell "gic_cpu_read: Bad offset %x\n", (int)offset); 15410cf09852SPeter Maydell *data = 0; 15420cf09852SPeter Maydell break; 1543e69954b9Spbrook } 1544a9d85353SPeter Maydell return MEMTX_OK; 1545e69954b9Spbrook } 1546e69954b9Spbrook 1547a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset, 1548a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1549e69954b9Spbrook { 1550e69954b9Spbrook switch (offset) { 1551e69954b9Spbrook case 0x00: /* Control */ 155232951860SFabian Aggeler gic_set_cpu_control(s, cpu, value, attrs); 1553e69954b9Spbrook break; 1554e69954b9Spbrook case 0x04: /* Priority mask */ 155581508470SFabian Aggeler gic_set_priority_mask(s, cpu, value, attrs); 1556e69954b9Spbrook break; 1557e69954b9Spbrook case 0x08: /* Binary Point */ 15583dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1559421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1560421a3c22SLuc MICHEL /* WI when CBPR is 1 */ 1561421a3c22SLuc MICHEL return MEMTX_OK; 1562421a3c22SLuc MICHEL } else { 1563822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1564421a3c22SLuc MICHEL } 1565822e9cc3SFabian Aggeler } else { 15667eb079ecSLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 15677eb079ecSLuc Michel s->bpr[cpu] = MAX(value & 0x7, min_bpr); 1568822e9cc3SFabian Aggeler } 1569e69954b9Spbrook break; 1570e69954b9Spbrook case 0x10: /* End Of Interrupt */ 1571f9c6a7f1SFabian Aggeler gic_complete_irq(s, cpu, value & 0x3ff, attrs); 1572a9d85353SPeter Maydell return MEMTX_OK; 1573aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 15743dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1575822e9cc3SFabian Aggeler /* unimplemented, or NS access: RAZ/WI */ 1576822e9cc3SFabian Aggeler return MEMTX_OK; 1577822e9cc3SFabian Aggeler } else { 1578822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1579aa7d461aSChristoffer Dall } 1580aa7d461aSChristoffer Dall break; 1581a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 158251fd06e0SPeter Maydell { 158351fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 15847eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 158551fd06e0SPeter Maydell 15867eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 158751fd06e0SPeter Maydell return MEMTX_OK; 158851fd06e0SPeter Maydell } 15897eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 15907eb079ecSLuc Michel s->h_apr[gic_get_vcpu_real_id(cpu)] = value; 15917eb079ecSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 159251fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 159351fd06e0SPeter Maydell gic_apr_write_ns_view(s, regno, cpu, value); 159451fd06e0SPeter Maydell } else { 159551fd06e0SPeter Maydell s->apr[regno][cpu] = value; 159651fd06e0SPeter Maydell } 1597a9d477c4SChristoffer Dall break; 159851fd06e0SPeter Maydell } 159951fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 160051fd06e0SPeter Maydell { 160151fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 160251fd06e0SPeter Maydell 160351fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2) { 160451fd06e0SPeter Maydell return MEMTX_OK; 160551fd06e0SPeter Maydell } 16067eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 16077eb079ecSLuc Michel return MEMTX_OK; 16087eb079ecSLuc Michel } 16093dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 161051fd06e0SPeter Maydell return MEMTX_OK; 161151fd06e0SPeter Maydell } 161251fd06e0SPeter Maydell s->nsapr[regno][cpu] = value; 161351fd06e0SPeter Maydell break; 161451fd06e0SPeter Maydell } 1615a55c910eSPeter Maydell case 0x1000: 1616a55c910eSPeter Maydell /* GICC_DIR */ 1617a55c910eSPeter Maydell gic_deactivate_irq(s, cpu, value & 0x3ff, attrs); 1618a55c910eSPeter Maydell break; 1619e69954b9Spbrook default: 16208c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 16218c8dc39fSPeter Maydell "gic_cpu_write: Bad offset %x\n", (int)offset); 16220cf09852SPeter Maydell return MEMTX_OK; 1623e69954b9Spbrook } 1624*cbe1282bSLuc Michel 1625*cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 1626*cbe1282bSLuc Michel gic_update_virt(s); 1627*cbe1282bSLuc Michel } else { 1628e69954b9Spbrook gic_update(s); 1629*cbe1282bSLuc Michel } 1630*cbe1282bSLuc Michel 1631a9d85353SPeter Maydell return MEMTX_OK; 1632e69954b9Spbrook } 1633e2c56465SPeter Maydell 1634e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */ 1635a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data, 1636a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1637e2c56465SPeter Maydell { 1638fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1639a9d85353SPeter Maydell return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs); 1640e2c56465SPeter Maydell } 1641e2c56465SPeter Maydell 1642a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr, 1643a9d85353SPeter Maydell uint64_t value, unsigned size, 1644a9d85353SPeter Maydell MemTxAttrs attrs) 1645e2c56465SPeter Maydell { 1646fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1647a9d85353SPeter Maydell return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs); 1648e2c56465SPeter Maydell } 1649e2c56465SPeter Maydell 1650e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU. 1651fae15286SPeter Maydell * These just decode the opaque pointer into GICState* + cpu id. 1652e2c56465SPeter Maydell */ 1653a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data, 1654a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1655e2c56465SPeter Maydell { 1656fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1657fae15286SPeter Maydell GICState *s = *backref; 1658e2c56465SPeter Maydell int id = (backref - s->backref); 1659a9d85353SPeter Maydell return gic_cpu_read(s, id, addr, data, attrs); 1660e2c56465SPeter Maydell } 1661e2c56465SPeter Maydell 1662a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr, 1663a9d85353SPeter Maydell uint64_t value, unsigned size, 1664a9d85353SPeter Maydell MemTxAttrs attrs) 1665e2c56465SPeter Maydell { 1666fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1667fae15286SPeter Maydell GICState *s = *backref; 1668e2c56465SPeter Maydell int id = (backref - s->backref); 1669a9d85353SPeter Maydell return gic_cpu_write(s, id, addr, value, attrs); 1670e2c56465SPeter Maydell } 1671e2c56465SPeter Maydell 16722c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data, 16732c679ac7SLuc Michel unsigned size, MemTxAttrs attrs) 16742c679ac7SLuc Michel { 16752c679ac7SLuc Michel GICState *s = (GICState *)opaque; 16762c679ac7SLuc Michel 16772c679ac7SLuc Michel return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs); 16782c679ac7SLuc Michel } 16792c679ac7SLuc Michel 16802c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr, 16812c679ac7SLuc Michel uint64_t value, unsigned size, 16822c679ac7SLuc Michel MemTxAttrs attrs) 16832c679ac7SLuc Michel { 16842c679ac7SLuc Michel GICState *s = (GICState *)opaque; 16852c679ac7SLuc Michel 16862c679ac7SLuc Michel return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs); 16872c679ac7SLuc Michel } 16882c679ac7SLuc Michel 1689527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start) 1690527d296fSLuc Michel { 1691527d296fSLuc Michel int lr_idx; 1692527d296fSLuc Michel uint32_t ret = 0; 1693527d296fSLuc Michel 1694527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1695527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1696527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1697527d296fSLuc Michel gic_lr_entry_is_eoi(*entry)); 1698527d296fSLuc Michel } 1699527d296fSLuc Michel 1700527d296fSLuc Michel return ret; 1701527d296fSLuc Michel } 1702527d296fSLuc Michel 1703527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start) 1704527d296fSLuc Michel { 1705527d296fSLuc Michel int lr_idx; 1706527d296fSLuc Michel uint32_t ret = 0; 1707527d296fSLuc Michel 1708527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1709527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1710527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1711527d296fSLuc Michel gic_lr_entry_is_free(*entry)); 1712527d296fSLuc Michel } 1713527d296fSLuc Michel 1714527d296fSLuc Michel return ret; 1715527d296fSLuc Michel } 1716527d296fSLuc Michel 1717527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs) 1718527d296fSLuc Michel { 1719527d296fSLuc Michel int vcpu = gic_get_current_vcpu(s); 1720527d296fSLuc Michel uint32_t ctlr; 1721527d296fSLuc Michel uint32_t abpr; 1722527d296fSLuc Michel uint32_t bpr; 1723527d296fSLuc Michel uint32_t prio_mask; 1724527d296fSLuc Michel 1725527d296fSLuc Michel ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr); 1726527d296fSLuc Michel abpr = FIELD_EX32(value, GICH_VMCR, VMABP); 1727527d296fSLuc Michel bpr = FIELD_EX32(value, GICH_VMCR, VMBP); 1728527d296fSLuc Michel prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3; 1729527d296fSLuc Michel 1730527d296fSLuc Michel gic_set_cpu_control(s, vcpu, ctlr, attrs); 1731527d296fSLuc Michel s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR); 1732527d296fSLuc Michel s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR); 1733527d296fSLuc Michel gic_set_priority_mask(s, vcpu, prio_mask, attrs); 1734527d296fSLuc Michel } 1735527d296fSLuc Michel 1736527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr, 1737527d296fSLuc Michel uint64_t *data, MemTxAttrs attrs) 1738527d296fSLuc Michel { 1739527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1740527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1741527d296fSLuc Michel 1742527d296fSLuc Michel switch (addr) { 1743527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1744527d296fSLuc Michel *data = s->h_hcr[cpu]; 1745527d296fSLuc Michel break; 1746527d296fSLuc Michel 1747527d296fSLuc Michel case A_GICH_VTR: /* VGIC Type */ 1748527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1); 1749527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PREbits, 1750527d296fSLuc Michel GIC_VIRT_MAX_GROUP_PRIO_BITS - 1); 1751527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PRIbits, 1752527d296fSLuc Michel (7 - GIC_VIRT_MIN_BPR) - 1); 1753527d296fSLuc Michel break; 1754527d296fSLuc Michel 1755527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1756527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr, 1757527d296fSLuc Michel extract32(s->cpu_ctlr[vcpu], 0, 10)); 1758527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]); 1759527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]); 1760527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask, 1761527d296fSLuc Michel extract32(s->priority_mask[vcpu], 3, 5)); 1762527d296fSLuc Michel break; 1763527d296fSLuc Michel 1764527d296fSLuc Michel case A_GICH_MISR: /* Maintenance Interrupt Status */ 1765527d296fSLuc Michel *data = s->h_misr[cpu]; 1766527d296fSLuc Michel break; 1767527d296fSLuc Michel 1768527d296fSLuc Michel case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */ 1769527d296fSLuc Michel case A_GICH_EISR1: 1770527d296fSLuc Michel *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8); 1771527d296fSLuc Michel break; 1772527d296fSLuc Michel 1773527d296fSLuc Michel case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */ 1774527d296fSLuc Michel case A_GICH_ELRSR1: 1775527d296fSLuc Michel *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8); 1776527d296fSLuc Michel break; 1777527d296fSLuc Michel 1778527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1779527d296fSLuc Michel *data = s->h_apr[cpu]; 1780527d296fSLuc Michel break; 1781527d296fSLuc Michel 1782527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1783527d296fSLuc Michel { 1784527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1785527d296fSLuc Michel 1786527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1787527d296fSLuc Michel *data = 0; 1788527d296fSLuc Michel } else { 1789527d296fSLuc Michel *data = s->h_lr[lr_idx][cpu]; 1790527d296fSLuc Michel } 1791527d296fSLuc Michel break; 1792527d296fSLuc Michel } 1793527d296fSLuc Michel 1794527d296fSLuc Michel default: 1795527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1796527d296fSLuc Michel "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr); 1797527d296fSLuc Michel return MEMTX_OK; 1798527d296fSLuc Michel } 1799527d296fSLuc Michel 1800527d296fSLuc Michel return MEMTX_OK; 1801527d296fSLuc Michel } 1802527d296fSLuc Michel 1803527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr, 1804527d296fSLuc Michel uint64_t value, MemTxAttrs attrs) 1805527d296fSLuc Michel { 1806527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1807527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1808527d296fSLuc Michel 1809527d296fSLuc Michel switch (addr) { 1810527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1811527d296fSLuc Michel s->h_hcr[cpu] = value & GICH_HCR_MASK; 1812527d296fSLuc Michel break; 1813527d296fSLuc Michel 1814527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1815527d296fSLuc Michel gic_vmcr_write(s, value, attrs); 1816527d296fSLuc Michel break; 1817527d296fSLuc Michel 1818527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1819527d296fSLuc Michel s->h_apr[cpu] = value; 1820527d296fSLuc Michel s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu); 1821527d296fSLuc Michel break; 1822527d296fSLuc Michel 1823527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1824527d296fSLuc Michel { 1825527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1826527d296fSLuc Michel 1827527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1828527d296fSLuc Michel return MEMTX_OK; 1829527d296fSLuc Michel } 1830527d296fSLuc Michel 1831527d296fSLuc Michel s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK; 1832527d296fSLuc Michel break; 1833527d296fSLuc Michel } 1834527d296fSLuc Michel 1835527d296fSLuc Michel default: 1836527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1837527d296fSLuc Michel "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr); 1838527d296fSLuc Michel return MEMTX_OK; 1839527d296fSLuc Michel } 1840527d296fSLuc Michel 1841*cbe1282bSLuc Michel gic_update_virt(s); 1842527d296fSLuc Michel return MEMTX_OK; 1843527d296fSLuc Michel } 1844527d296fSLuc Michel 1845527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 1846527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 1847527d296fSLuc Michel { 1848527d296fSLuc Michel GICState *s = (GICState *)opaque; 1849527d296fSLuc Michel 1850527d296fSLuc Michel return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs); 1851527d296fSLuc Michel } 1852527d296fSLuc Michel 1853527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr, 1854527d296fSLuc Michel uint64_t value, unsigned size, 1855527d296fSLuc Michel MemTxAttrs attrs) 1856527d296fSLuc Michel { 1857527d296fSLuc Michel GICState *s = (GICState *)opaque; 1858527d296fSLuc Michel 1859527d296fSLuc Michel return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs); 1860527d296fSLuc Michel } 1861527d296fSLuc Michel 1862527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 1863527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 1864527d296fSLuc Michel { 1865527d296fSLuc Michel GICState **backref = (GICState **)opaque; 1866527d296fSLuc Michel GICState *s = *backref; 1867527d296fSLuc Michel int id = (backref - s->backref); 1868527d296fSLuc Michel 1869527d296fSLuc Michel return gic_hyp_read(s, id, addr, data, attrs); 1870527d296fSLuc Michel } 1871527d296fSLuc Michel 1872527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr, 1873527d296fSLuc Michel uint64_t value, unsigned size, 1874527d296fSLuc Michel MemTxAttrs attrs) 1875527d296fSLuc Michel { 1876527d296fSLuc Michel GICState **backref = (GICState **)opaque; 1877527d296fSLuc Michel GICState *s = *backref; 1878527d296fSLuc Michel int id = (backref - s->backref); 1879527d296fSLuc Michel 1880527d296fSLuc Michel return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs); 1881527d296fSLuc Michel 1882527d296fSLuc Michel } 1883527d296fSLuc Michel 18847926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = { 18857926c210SPavel Fedin { 18867926c210SPavel Fedin .read_with_attrs = gic_dist_read, 18877926c210SPavel Fedin .write_with_attrs = gic_dist_write, 18887926c210SPavel Fedin .endianness = DEVICE_NATIVE_ENDIAN, 18897926c210SPavel Fedin }, 18907926c210SPavel Fedin { 1891a9d85353SPeter Maydell .read_with_attrs = gic_thiscpu_read, 1892a9d85353SPeter Maydell .write_with_attrs = gic_thiscpu_write, 1893e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 18947926c210SPavel Fedin } 1895e2c56465SPeter Maydell }; 1896e2c56465SPeter Maydell 1897e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = { 1898a9d85353SPeter Maydell .read_with_attrs = gic_do_cpu_read, 1899a9d85353SPeter Maydell .write_with_attrs = gic_do_cpu_write, 1900e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 1901e2c56465SPeter Maydell }; 1902e69954b9Spbrook 19032c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = { 19042c679ac7SLuc Michel { 1905527d296fSLuc Michel .read_with_attrs = gic_thiscpu_hyp_read, 1906527d296fSLuc Michel .write_with_attrs = gic_thiscpu_hyp_write, 19072c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 19082c679ac7SLuc Michel }, 19092c679ac7SLuc Michel { 19102c679ac7SLuc Michel .read_with_attrs = gic_thisvcpu_read, 19112c679ac7SLuc Michel .write_with_attrs = gic_thisvcpu_write, 19122c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 19132c679ac7SLuc Michel } 19142c679ac7SLuc Michel }; 19152c679ac7SLuc Michel 1916527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = { 1917527d296fSLuc Michel .read_with_attrs = gic_do_hyp_read, 1918527d296fSLuc Michel .write_with_attrs = gic_do_hyp_write, 1919527d296fSLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 1920527d296fSLuc Michel }; 1921527d296fSLuc Michel 192253111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp) 19232b518c56SPeter Maydell { 192453111180SPeter Maydell /* Device instance realize function for the GIC sysbus device */ 19252b518c56SPeter Maydell int i; 192653111180SPeter Maydell GICState *s = ARM_GIC(dev); 192753111180SPeter Maydell SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 19281e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_GET_CLASS(s); 19290175ba10SMarkus Armbruster Error *local_err = NULL; 19301e8cae4dSPeter Maydell 19310175ba10SMarkus Armbruster agc->parent_realize(dev, &local_err); 19320175ba10SMarkus Armbruster if (local_err) { 19330175ba10SMarkus Armbruster error_propagate(errp, local_err); 193453111180SPeter Maydell return; 193553111180SPeter Maydell } 19361e8cae4dSPeter Maydell 19375d721b78SAlexander Graf if (kvm_enabled() && !kvm_arm_supports_user_irq()) { 19385d721b78SAlexander Graf error_setg(errp, "KVM with user space irqchip only works when the " 19395d721b78SAlexander Graf "host kernel supports KVM_CAP_ARM_USER_IRQ"); 19405d721b78SAlexander Graf return; 19415d721b78SAlexander Graf } 19425d721b78SAlexander Graf 19432c679ac7SLuc Michel /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if 19442c679ac7SLuc Michel * enabled, virtualization extensions related interfaces (main virtual 19452c679ac7SLuc Michel * interface (s->vifaceiomem[0]) and virtual CPU interface). 19462c679ac7SLuc Michel */ 19472c679ac7SLuc Michel gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops); 19482b518c56SPeter Maydell 19497926c210SPavel Fedin /* Extra core-specific regions for the CPU interfaces. This is 19507926c210SPavel Fedin * necessary for "franken-GIC" implementations, for example on 19517926c210SPavel Fedin * Exynos 4. 1952e2c56465SPeter Maydell * NB that the memory region size of 0x100 applies for the 11MPCore 1953e2c56465SPeter Maydell * and also cores following the GIC v1 spec (ie A9). 1954e2c56465SPeter Maydell * GIC v2 defines a larger memory region (0x1000) so this will need 1955e2c56465SPeter Maydell * to be extended when we implement A15. 1956e2c56465SPeter Maydell */ 1957b95690c9SWei Huang for (i = 0; i < s->num_cpu; i++) { 1958e2c56465SPeter Maydell s->backref[i] = s; 19591437c94bSPaolo Bonzini memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops, 19601437c94bSPaolo Bonzini &s->backref[i], "gic_cpu", 0x100); 19617926c210SPavel Fedin sysbus_init_mmio(sbd, &s->cpuiomem[i+1]); 1962496dbcd1SPeter Maydell } 1963527d296fSLuc Michel 1964527d296fSLuc Michel /* Extra core-specific regions for virtual interfaces. This is required by 1965527d296fSLuc Michel * the GICv2 specification. 1966527d296fSLuc Michel */ 1967527d296fSLuc Michel if (s->virt_extn) { 1968527d296fSLuc Michel for (i = 0; i < s->num_cpu; i++) { 1969527d296fSLuc Michel memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s), 1970527d296fSLuc Michel &gic_viface_ops, &s->backref[i], 1971527d296fSLuc Michel "gic_viface", 0x1000); 1972527d296fSLuc Michel sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]); 1973527d296fSLuc Michel } 1974527d296fSLuc Michel } 1975527d296fSLuc Michel 1976496dbcd1SPeter Maydell } 1977496dbcd1SPeter Maydell 1978496dbcd1SPeter Maydell static void arm_gic_class_init(ObjectClass *klass, void *data) 1979496dbcd1SPeter Maydell { 1980496dbcd1SPeter Maydell DeviceClass *dc = DEVICE_CLASS(klass); 19811e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_CLASS(klass); 198253111180SPeter Maydell 1983bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize); 1984496dbcd1SPeter Maydell } 1985496dbcd1SPeter Maydell 19868c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = { 19871e8cae4dSPeter Maydell .name = TYPE_ARM_GIC, 19881e8cae4dSPeter Maydell .parent = TYPE_ARM_GIC_COMMON, 1989fae15286SPeter Maydell .instance_size = sizeof(GICState), 1990496dbcd1SPeter Maydell .class_init = arm_gic_class_init, 1991998a74bcSPeter Maydell .class_size = sizeof(ARMGICClass), 1992496dbcd1SPeter Maydell }; 1993496dbcd1SPeter Maydell 1994496dbcd1SPeter Maydell static void arm_gic_register_types(void) 1995496dbcd1SPeter Maydell { 1996496dbcd1SPeter Maydell type_register_static(&arm_gic_info); 1997496dbcd1SPeter Maydell } 1998496dbcd1SPeter Maydell 1999496dbcd1SPeter Maydell type_init(arm_gic_register_types) 2000