1e69954b9Spbrook /* 29ee6e8bbSpbrook * ARM Generic/Distributed Interrupt Controller 3e69954b9Spbrook * 49ee6e8bbSpbrook * Copyright (c) 2006-2007 CodeSourcery. 5e69954b9Spbrook * Written by Paul Brook 6e69954b9Spbrook * 78e31bf38SMatthew Fernandez * This code is licensed under the GPL. 8e69954b9Spbrook */ 9e69954b9Spbrook 109ee6e8bbSpbrook /* This file contains implementation code for the RealView EB interrupt 110d256bdcSPeter Maydell * controller, MPCore distributed interrupt controller and ARMv7-M 120d256bdcSPeter Maydell * Nested Vectored Interrupt Controller. 130d256bdcSPeter Maydell * It is compiled in two ways: 140d256bdcSPeter Maydell * (1) as a standalone file to produce a sysbus device which is a GIC 150d256bdcSPeter Maydell * that can be used on the realview board and as one of the builtin 160d256bdcSPeter Maydell * private peripherals for the ARM MP CPUs (11MPCore, A9, etc) 170d256bdcSPeter Maydell * (2) by being directly #included into armv7m_nvic.c to produce the 180d256bdcSPeter Maydell * armv7m_nvic device. 190d256bdcSPeter Maydell */ 20e69954b9Spbrook 218ef94f0bSPeter Maydell #include "qemu/osdep.h" 2264552b6bSMarkus Armbruster #include "hw/irq.h" 2383c9f4caSPaolo Bonzini #include "hw/sysbus.h" 2447b43a1fSPaolo Bonzini #include "gic_internal.h" 25da34e65cSMarkus Armbruster #include "qapi/error.h" 262e5b09fdSMarkus Armbruster #include "hw/core/cpu.h" 2703dd024fSPaolo Bonzini #include "qemu/log.h" 280b8fa32fSMarkus Armbruster #include "qemu/module.h" 292531088fSHollis Blanchard #include "trace.h" 305d721b78SAlexander Graf #include "sysemu/kvm.h" 31*09bbdb89SPhilippe Mathieu-Daudé #include "sysemu/qtest.h" 32386e2955SPeter Maydell 3368bf93ceSAlex Bennée /* #define DEBUG_GIC */ 34e69954b9Spbrook 35e69954b9Spbrook #ifdef DEBUG_GIC 3668bf93ceSAlex Bennée #define DEBUG_GIC_GATE 1 37e69954b9Spbrook #else 3868bf93ceSAlex Bennée #define DEBUG_GIC_GATE 0 39e69954b9Spbrook #endif 40e69954b9Spbrook 4168bf93ceSAlex Bennée #define DPRINTF(fmt, ...) do { \ 4268bf93ceSAlex Bennée if (DEBUG_GIC_GATE) { \ 4368bf93ceSAlex Bennée fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ 4468bf93ceSAlex Bennée } \ 4568bf93ceSAlex Bennée } while (0) 4668bf93ceSAlex Bennée 473355c360SAlistair Francis static const uint8_t gic_id_11mpcore[] = { 483355c360SAlistair Francis 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 493355c360SAlistair Francis }; 503355c360SAlistair Francis 513355c360SAlistair Francis static const uint8_t gic_id_gicv1[] = { 523355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 533355c360SAlistair Francis }; 543355c360SAlistair Francis 553355c360SAlistair Francis static const uint8_t gic_id_gicv2[] = { 563355c360SAlistair Francis 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 572a29ddeeSPeter Maydell }; 582a29ddeeSPeter Maydell 59fae15286SPeter Maydell static inline int gic_get_current_cpu(GICState *s) 60926c4affSPeter Maydell { 61*09bbdb89SPhilippe Mathieu-Daudé if (!qtest_enabled() && s->num_cpu > 1) { 624917cf44SAndreas Färber return current_cpu->cpu_index; 63926c4affSPeter Maydell } 64926c4affSPeter Maydell return 0; 65926c4affSPeter Maydell } 66926c4affSPeter Maydell 674a37e0e4SLuc Michel static inline int gic_get_current_vcpu(GICState *s) 684a37e0e4SLuc Michel { 694a37e0e4SLuc Michel return gic_get_current_cpu(s) + GIC_NCPU; 704a37e0e4SLuc Michel } 714a37e0e4SLuc Michel 72c27a5ba9SFabian Aggeler /* Return true if this GIC config has interrupt groups, which is 73c27a5ba9SFabian Aggeler * true if we're a GICv2, or a GICv1 with the security extensions. 74c27a5ba9SFabian Aggeler */ 75c27a5ba9SFabian Aggeler static inline bool gic_has_groups(GICState *s) 76c27a5ba9SFabian Aggeler { 77c27a5ba9SFabian Aggeler return s->revision == 2 || s->security_extn; 78c27a5ba9SFabian Aggeler } 79c27a5ba9SFabian Aggeler 803dd0471bSLuc Michel static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs) 813dd0471bSLuc Michel { 823dd0471bSLuc Michel return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure; 833dd0471bSLuc Michel } 843dd0471bSLuc Michel 85cbe1282bSLuc Michel static inline void gic_get_best_irq(GICState *s, int cpu, 86cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 87cbe1282bSLuc Michel { 88cbe1282bSLuc Michel int irq; 89cbe1282bSLuc Michel int cm = 1 << cpu; 90cbe1282bSLuc Michel 91cbe1282bSLuc Michel *best_irq = 1023; 92cbe1282bSLuc Michel *best_prio = 0x100; 93cbe1282bSLuc Michel 94cbe1282bSLuc Michel for (irq = 0; irq < s->num_irq; irq++) { 95cbe1282bSLuc Michel if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) && 96cbe1282bSLuc Michel (!GIC_DIST_TEST_ACTIVE(irq, cm)) && 97cbe1282bSLuc Michel (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) { 98cbe1282bSLuc Michel if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) { 99cbe1282bSLuc Michel *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu); 100cbe1282bSLuc Michel *best_irq = irq; 101cbe1282bSLuc Michel } 102cbe1282bSLuc Michel } 103cbe1282bSLuc Michel } 104cbe1282bSLuc Michel 105cbe1282bSLuc Michel if (*best_irq < 1023) { 106cbe1282bSLuc Michel *group = GIC_DIST_TEST_GROUP(*best_irq, cm); 107cbe1282bSLuc Michel } 108cbe1282bSLuc Michel } 109cbe1282bSLuc Michel 110cbe1282bSLuc Michel static inline void gic_get_best_virq(GICState *s, int cpu, 111cbe1282bSLuc Michel int *best_irq, int *best_prio, int *group) 112cbe1282bSLuc Michel { 113cbe1282bSLuc Michel int lr_idx = 0; 114cbe1282bSLuc Michel 115cbe1282bSLuc Michel *best_irq = 1023; 116cbe1282bSLuc Michel *best_prio = 0x100; 117cbe1282bSLuc Michel 118cbe1282bSLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) { 119cbe1282bSLuc Michel uint32_t lr_entry = s->h_lr[lr_idx][cpu]; 120cbe1282bSLuc Michel int state = GICH_LR_STATE(lr_entry); 121cbe1282bSLuc Michel 122cbe1282bSLuc Michel if (state == GICH_LR_STATE_PENDING) { 123cbe1282bSLuc Michel int prio = GICH_LR_PRIORITY(lr_entry); 124cbe1282bSLuc Michel 125cbe1282bSLuc Michel if (prio < *best_prio) { 126cbe1282bSLuc Michel *best_prio = prio; 127cbe1282bSLuc Michel *best_irq = GICH_LR_VIRT_ID(lr_entry); 128cbe1282bSLuc Michel *group = GICH_LR_GROUP(lr_entry); 129cbe1282bSLuc Michel } 130cbe1282bSLuc Michel } 131cbe1282bSLuc Michel } 132cbe1282bSLuc Michel } 133cbe1282bSLuc Michel 134cbe1282bSLuc Michel /* Return true if IRQ signaling is enabled for the given cpu and at least one 135cbe1282bSLuc Michel * of the given groups: 136cbe1282bSLuc Michel * - in the non-virt case, the distributor must be enabled for one of the 137cbe1282bSLuc Michel * given groups 138cbe1282bSLuc Michel * - in the virt case, the virtual interface must be enabled. 139cbe1282bSLuc Michel * - in all cases, the (v)CPU interface must be enabled for one of the given 140cbe1282bSLuc Michel * groups. 141cbe1282bSLuc Michel */ 142cbe1282bSLuc Michel static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt, 143cbe1282bSLuc Michel int group_mask) 144cbe1282bSLuc Michel { 1454663b72aSEdgar E. Iglesias int cpu_iface = virt ? (cpu + GIC_NCPU) : cpu; 1464663b72aSEdgar E. Iglesias 147cbe1282bSLuc Michel if (!virt && !(s->ctlr & group_mask)) { 148cbe1282bSLuc Michel return false; 149cbe1282bSLuc Michel } 150cbe1282bSLuc Michel 151cbe1282bSLuc Michel if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) { 152cbe1282bSLuc Michel return false; 153cbe1282bSLuc Michel } 154cbe1282bSLuc Michel 1554663b72aSEdgar E. Iglesias if (!(s->cpu_ctlr[cpu_iface] & group_mask)) { 156cbe1282bSLuc Michel return false; 157cbe1282bSLuc Michel } 158cbe1282bSLuc Michel 159cbe1282bSLuc Michel return true; 160cbe1282bSLuc Michel } 161cbe1282bSLuc Michel 162e69954b9Spbrook /* TODO: Many places that call this routine could be optimized. */ 163e69954b9Spbrook /* Update interrupt status after enabled or pending bits have been changed. */ 164cbe1282bSLuc Michel static inline void gic_update_internal(GICState *s, bool virt) 165e69954b9Spbrook { 166e69954b9Spbrook int best_irq; 167e69954b9Spbrook int best_prio; 168dadbb58fSPeter Maydell int irq_level, fiq_level; 169cbe1282bSLuc Michel int cpu, cpu_iface; 170cbe1282bSLuc Michel int group = 0; 171cbe1282bSLuc Michel qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq; 172cbe1282bSLuc Michel qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq; 173e69954b9Spbrook 174b95690c9SWei Huang for (cpu = 0; cpu < s->num_cpu; cpu++) { 175cbe1282bSLuc Michel cpu_iface = virt ? (cpu + GIC_NCPU) : cpu; 176cbe1282bSLuc Michel 177cbe1282bSLuc Michel s->current_pending[cpu_iface] = 1023; 178cbe1282bSLuc Michel if (!gic_irq_signaling_enabled(s, cpu, virt, 179cbe1282bSLuc Michel GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) { 180cbe1282bSLuc Michel qemu_irq_lower(irq_lines[cpu]); 181cbe1282bSLuc Michel qemu_irq_lower(fiq_lines[cpu]); 182235069a3SJohan Karlsson continue; 183e69954b9Spbrook } 184cbe1282bSLuc Michel 185cbe1282bSLuc Michel if (virt) { 186cbe1282bSLuc Michel gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group); 187cbe1282bSLuc Michel } else { 188cbe1282bSLuc Michel gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group); 189e69954b9Spbrook } 190dadbb58fSPeter Maydell 1912531088fSHollis Blanchard if (best_irq != 1023) { 192067a2b9cSLuc Michel trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu, 193067a2b9cSLuc Michel best_irq, best_prio, 194067a2b9cSLuc Michel s->priority_mask[cpu_iface], 195067a2b9cSLuc Michel s->running_priority[cpu_iface]); 1962531088fSHollis Blanchard } 1972531088fSHollis Blanchard 198dadbb58fSPeter Maydell irq_level = fiq_level = 0; 199dadbb58fSPeter Maydell 200cbe1282bSLuc Michel if (best_prio < s->priority_mask[cpu_iface]) { 201cbe1282bSLuc Michel s->current_pending[cpu_iface] = best_irq; 202cbe1282bSLuc Michel if (best_prio < s->running_priority[cpu_iface]) { 203cbe1282bSLuc Michel if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) { 204cbe1282bSLuc Michel if (group == 0 && 205cbe1282bSLuc Michel s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) { 206dadbb58fSPeter Maydell DPRINTF("Raised pending FIQ %d (cpu %d)\n", 207cbe1282bSLuc Michel best_irq, cpu_iface); 208dadbb58fSPeter Maydell fiq_level = 1; 209cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq", 210cbe1282bSLuc Michel fiq_level); 211dadbb58fSPeter Maydell } else { 212dadbb58fSPeter Maydell DPRINTF("Raised pending IRQ %d (cpu %d)\n", 213cbe1282bSLuc Michel best_irq, cpu_iface); 214dadbb58fSPeter Maydell irq_level = 1; 215cbe1282bSLuc Michel trace_gic_update_set_irq(cpu, virt ? "virq" : "irq", 216cbe1282bSLuc Michel irq_level); 217e69954b9Spbrook } 218e69954b9Spbrook } 219dadbb58fSPeter Maydell } 220dadbb58fSPeter Maydell } 221dadbb58fSPeter Maydell 222cbe1282bSLuc Michel qemu_set_irq(irq_lines[cpu], irq_level); 223cbe1282bSLuc Michel qemu_set_irq(fiq_lines[cpu], fiq_level); 2249ee6e8bbSpbrook } 225e69954b9Spbrook } 226e69954b9Spbrook 227cbe1282bSLuc Michel static void gic_update(GICState *s) 228cbe1282bSLuc Michel { 229cbe1282bSLuc Michel gic_update_internal(s, false); 230cbe1282bSLuc Michel } 231cbe1282bSLuc Michel 232527d296fSLuc Michel /* Return true if this LR is empty, i.e. the corresponding bit 233527d296fSLuc Michel * in ELRSR is set. 234527d296fSLuc Michel */ 235527d296fSLuc Michel static inline bool gic_lr_entry_is_free(uint32_t entry) 236527d296fSLuc Michel { 237527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 238527d296fSLuc Michel && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry)); 239527d296fSLuc Michel } 240527d296fSLuc Michel 241527d296fSLuc Michel /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the 242527d296fSLuc Michel * corrsponding bit in EISR is set. 243527d296fSLuc Michel */ 244527d296fSLuc Michel static inline bool gic_lr_entry_is_eoi(uint32_t entry) 245527d296fSLuc Michel { 246527d296fSLuc Michel return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID) 247527d296fSLuc Michel && !GICH_LR_HW(entry) && GICH_LR_EOI(entry); 248527d296fSLuc Michel } 249527d296fSLuc Michel 25050e57926SLuc Michel static inline void gic_extract_lr_info(GICState *s, int cpu, 25150e57926SLuc Michel int *num_eoi, int *num_valid, int *num_pending) 25250e57926SLuc Michel { 25350e57926SLuc Michel int lr_idx; 25450e57926SLuc Michel 25550e57926SLuc Michel *num_eoi = 0; 25650e57926SLuc Michel *num_valid = 0; 25750e57926SLuc Michel *num_pending = 0; 25850e57926SLuc Michel 25950e57926SLuc Michel for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) { 26050e57926SLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 26150e57926SLuc Michel 26250e57926SLuc Michel if (gic_lr_entry_is_eoi(*entry)) { 26350e57926SLuc Michel (*num_eoi)++; 26450e57926SLuc Michel } 26550e57926SLuc Michel 26650e57926SLuc Michel if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) { 26750e57926SLuc Michel (*num_valid)++; 26850e57926SLuc Michel } 26950e57926SLuc Michel 27050e57926SLuc Michel if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) { 27150e57926SLuc Michel (*num_pending)++; 27250e57926SLuc Michel } 27350e57926SLuc Michel } 27450e57926SLuc Michel } 27550e57926SLuc Michel 27650e57926SLuc Michel static void gic_compute_misr(GICState *s, int cpu) 27750e57926SLuc Michel { 27850e57926SLuc Michel uint32_t value = 0; 27950e57926SLuc Michel int vcpu = cpu + GIC_NCPU; 28050e57926SLuc Michel 28150e57926SLuc Michel int num_eoi, num_valid, num_pending; 28250e57926SLuc Michel 28350e57926SLuc Michel gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending); 28450e57926SLuc Michel 28550e57926SLuc Michel /* EOI */ 28650e57926SLuc Michel if (num_eoi) { 28750e57926SLuc Michel value |= R_GICH_MISR_EOI_MASK; 28850e57926SLuc Michel } 28950e57926SLuc Michel 29050e57926SLuc Michel /* U: true if only 0 or 1 LR entry is valid */ 29150e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) { 29250e57926SLuc Michel value |= R_GICH_MISR_U_MASK; 29350e57926SLuc Michel } 29450e57926SLuc Michel 29550e57926SLuc Michel /* LRENP: EOICount is not 0 */ 29650e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) && 29750e57926SLuc Michel ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) { 29850e57926SLuc Michel value |= R_GICH_MISR_LRENP_MASK; 29950e57926SLuc Michel } 30050e57926SLuc Michel 30150e57926SLuc Michel /* NP: no pending interrupts */ 30250e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) { 30350e57926SLuc Michel value |= R_GICH_MISR_NP_MASK; 30450e57926SLuc Michel } 30550e57926SLuc Michel 30650e57926SLuc Michel /* VGrp0E: group0 virq signaling enabled */ 30750e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) && 30850e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) { 30950e57926SLuc Michel value |= R_GICH_MISR_VGrp0E_MASK; 31050e57926SLuc Michel } 31150e57926SLuc Michel 31250e57926SLuc Michel /* VGrp0D: group0 virq signaling disabled */ 31350e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) && 31450e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) { 31550e57926SLuc Michel value |= R_GICH_MISR_VGrp0D_MASK; 31650e57926SLuc Michel } 31750e57926SLuc Michel 31850e57926SLuc Michel /* VGrp1E: group1 virq signaling enabled */ 31950e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) && 32050e57926SLuc Michel (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) { 32150e57926SLuc Michel value |= R_GICH_MISR_VGrp1E_MASK; 32250e57926SLuc Michel } 32350e57926SLuc Michel 32450e57926SLuc Michel /* VGrp1D: group1 virq signaling disabled */ 32550e57926SLuc Michel if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) && 32650e57926SLuc Michel !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) { 32750e57926SLuc Michel value |= R_GICH_MISR_VGrp1D_MASK; 32850e57926SLuc Michel } 32950e57926SLuc Michel 33050e57926SLuc Michel s->h_misr[cpu] = value; 33150e57926SLuc Michel } 33250e57926SLuc Michel 33350e57926SLuc Michel static void gic_update_maintenance(GICState *s) 33450e57926SLuc Michel { 33550e57926SLuc Michel int cpu = 0; 33650e57926SLuc Michel int maint_level; 33750e57926SLuc Michel 33850e57926SLuc Michel for (cpu = 0; cpu < s->num_cpu; cpu++) { 33950e57926SLuc Michel gic_compute_misr(s, cpu); 34050e57926SLuc Michel maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu]; 34150e57926SLuc Michel 342067a2b9cSLuc Michel trace_gic_update_maintenance_irq(cpu, maint_level); 34350e57926SLuc Michel qemu_set_irq(s->maintenance_irq[cpu], maint_level); 34450e57926SLuc Michel } 34550e57926SLuc Michel } 34650e57926SLuc Michel 347cbe1282bSLuc Michel static void gic_update_virt(GICState *s) 348cbe1282bSLuc Michel { 349cbe1282bSLuc Michel gic_update_internal(s, true); 35050e57926SLuc Michel gic_update_maintenance(s); 351cbe1282bSLuc Michel } 352cbe1282bSLuc Michel 3538d999995SChristoffer Dall static void gic_set_irq_11mpcore(GICState *s, int irq, int level, 3548d999995SChristoffer Dall int cm, int target) 3558d999995SChristoffer Dall { 3568d999995SChristoffer Dall if (level) { 35767ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 35867ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) { 3598d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 36067ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 3618d999995SChristoffer Dall } 3628d999995SChristoffer Dall } else { 36367ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 3648d999995SChristoffer Dall } 3658d999995SChristoffer Dall } 3668d999995SChristoffer Dall 3678d999995SChristoffer Dall static void gic_set_irq_generic(GICState *s, int irq, int level, 3688d999995SChristoffer Dall int cm, int target) 3698d999995SChristoffer Dall { 3708d999995SChristoffer Dall if (level) { 37167ce697aSLuc Michel GIC_DIST_SET_LEVEL(irq, cm); 3728d999995SChristoffer Dall DPRINTF("Set %d pending mask %x\n", irq, target); 37367ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) { 37467ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, target); 3758d999995SChristoffer Dall } 3768d999995SChristoffer Dall } else { 37767ce697aSLuc Michel GIC_DIST_CLEAR_LEVEL(irq, cm); 3788d999995SChristoffer Dall } 3798d999995SChristoffer Dall } 3808d999995SChristoffer Dall 3819ee6e8bbSpbrook /* Process a change in an external IRQ input. */ 382e69954b9Spbrook static void gic_set_irq(void *opaque, int irq, int level) 383e69954b9Spbrook { 384544d1afaSPeter Maydell /* Meaning of the 'irq' parameter: 385544d1afaSPeter Maydell * [0..N-1] : external interrupts 386544d1afaSPeter Maydell * [N..N+31] : PPI (internal) interrupts for CPU 0 387544d1afaSPeter Maydell * [N+32..N+63] : PPI (internal interrupts for CPU 1 388544d1afaSPeter Maydell * ... 389544d1afaSPeter Maydell */ 390fae15286SPeter Maydell GICState *s = (GICState *)opaque; 391544d1afaSPeter Maydell int cm, target; 392544d1afaSPeter Maydell if (irq < (s->num_irq - GIC_INTERNAL)) { 393e69954b9Spbrook /* The first external input line is internal interrupt 32. */ 394544d1afaSPeter Maydell cm = ALL_CPU_MASK; 39569253800SRusty Russell irq += GIC_INTERNAL; 39667ce697aSLuc Michel target = GIC_DIST_TARGET(irq); 397544d1afaSPeter Maydell } else { 398544d1afaSPeter Maydell int cpu; 399544d1afaSPeter Maydell irq -= (s->num_irq - GIC_INTERNAL); 400544d1afaSPeter Maydell cpu = irq / GIC_INTERNAL; 401544d1afaSPeter Maydell irq %= GIC_INTERNAL; 402544d1afaSPeter Maydell cm = 1 << cpu; 403544d1afaSPeter Maydell target = cm; 404544d1afaSPeter Maydell } 405544d1afaSPeter Maydell 40640d22500SChristoffer Dall assert(irq >= GIC_NR_SGIS); 40740d22500SChristoffer Dall 40867ce697aSLuc Michel if (level == GIC_DIST_TEST_LEVEL(irq, cm)) { 409e69954b9Spbrook return; 410544d1afaSPeter Maydell } 411e69954b9Spbrook 4123bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 4138d999995SChristoffer Dall gic_set_irq_11mpcore(s, irq, level, cm, target); 414e69954b9Spbrook } else { 4158d999995SChristoffer Dall gic_set_irq_generic(s, irq, level, cm, target); 416e69954b9Spbrook } 4172531088fSHollis Blanchard trace_gic_set_irq(irq, level, cm, target); 4188d999995SChristoffer Dall 419e69954b9Spbrook gic_update(s); 420e69954b9Spbrook } 421e69954b9Spbrook 4227c0fa108SFabian Aggeler static uint16_t gic_get_current_pending_irq(GICState *s, int cpu, 4237c0fa108SFabian Aggeler MemTxAttrs attrs) 4247c0fa108SFabian Aggeler { 4257c0fa108SFabian Aggeler uint16_t pending_irq = s->current_pending[cpu]; 4267c0fa108SFabian Aggeler 4277c0fa108SFabian Aggeler if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) { 42886b350f0SLuc Michel int group = gic_test_group(s, pending_irq, cpu); 42986b350f0SLuc Michel 4307c0fa108SFabian Aggeler /* On a GIC without the security extensions, reading this register 4317c0fa108SFabian Aggeler * behaves in the same way as a secure access to a GIC with them. 4327c0fa108SFabian Aggeler */ 4333dd0471bSLuc Michel bool secure = !gic_cpu_ns_access(s, cpu, attrs); 4347c0fa108SFabian Aggeler 4357c0fa108SFabian Aggeler if (group == 0 && !secure) { 4367c0fa108SFabian Aggeler /* Group0 interrupts hidden from Non-secure access */ 4377c0fa108SFabian Aggeler return 1023; 4387c0fa108SFabian Aggeler } 4397c0fa108SFabian Aggeler if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) { 4407c0fa108SFabian Aggeler /* Group1 interrupts only seen by Secure access if 4417c0fa108SFabian Aggeler * AckCtl bit set. 4427c0fa108SFabian Aggeler */ 4437c0fa108SFabian Aggeler return 1022; 4447c0fa108SFabian Aggeler } 4457c0fa108SFabian Aggeler } 4467c0fa108SFabian Aggeler return pending_irq; 4477c0fa108SFabian Aggeler } 4487c0fa108SFabian Aggeler 449df92cfa6SPeter Maydell static int gic_get_group_priority(GICState *s, int cpu, int irq) 450df92cfa6SPeter Maydell { 451df92cfa6SPeter Maydell /* Return the group priority of the specified interrupt 452df92cfa6SPeter Maydell * (which is the top bits of its priority, with the number 453df92cfa6SPeter Maydell * of bits masked determined by the applicable binary point register). 454df92cfa6SPeter Maydell */ 455df92cfa6SPeter Maydell int bpr; 456df92cfa6SPeter Maydell uint32_t mask; 457df92cfa6SPeter Maydell 458df92cfa6SPeter Maydell if (gic_has_groups(s) && 459df92cfa6SPeter Maydell !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) && 46086b350f0SLuc Michel gic_test_group(s, irq, cpu)) { 461fc05a6f2SLuc MICHEL bpr = s->abpr[cpu] - 1; 462fc05a6f2SLuc MICHEL assert(bpr >= 0); 463df92cfa6SPeter Maydell } else { 464df92cfa6SPeter Maydell bpr = s->bpr[cpu]; 465df92cfa6SPeter Maydell } 466df92cfa6SPeter Maydell 467df92cfa6SPeter Maydell /* a BPR of 0 means the group priority bits are [7:1]; 468df92cfa6SPeter Maydell * a BPR of 1 means they are [7:2], and so on down to 469df92cfa6SPeter Maydell * a BPR of 7 meaning no group priority bits at all. 470df92cfa6SPeter Maydell */ 471df92cfa6SPeter Maydell mask = ~0U << ((bpr & 7) + 1); 472df92cfa6SPeter Maydell 47386b350f0SLuc Michel return gic_get_priority(s, irq, cpu) & mask; 474df92cfa6SPeter Maydell } 475df92cfa6SPeter Maydell 47672889c8aSPeter Maydell static void gic_activate_irq(GICState *s, int cpu, int irq) 477e69954b9Spbrook { 47872889c8aSPeter Maydell /* Set the appropriate Active Priority Register bit for this IRQ, 47972889c8aSPeter Maydell * and update the running priority. 48072889c8aSPeter Maydell */ 48172889c8aSPeter Maydell int prio = gic_get_group_priority(s, cpu, irq); 482a1d7b8d8SLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 483a1d7b8d8SLuc Michel int preemption_level = prio >> (min_bpr + 1); 48472889c8aSPeter Maydell int regno = preemption_level / 32; 48572889c8aSPeter Maydell int bitno = preemption_level % 32; 486a1d7b8d8SLuc Michel uint32_t *papr = NULL; 48772889c8aSPeter Maydell 488a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 489a1d7b8d8SLuc Michel assert(regno == 0); 490a1d7b8d8SLuc Michel papr = &s->h_apr[gic_get_vcpu_real_id(cpu)]; 491a1d7b8d8SLuc Michel } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) { 492a1d7b8d8SLuc Michel papr = &s->nsapr[regno][cpu]; 4939ee6e8bbSpbrook } else { 494a1d7b8d8SLuc Michel papr = &s->apr[regno][cpu]; 4959ee6e8bbSpbrook } 49672889c8aSPeter Maydell 497a1d7b8d8SLuc Michel *papr |= (1 << bitno); 498a1d7b8d8SLuc Michel 49972889c8aSPeter Maydell s->running_priority[cpu] = prio; 50086b350f0SLuc Michel gic_set_active(s, irq, cpu); 50172889c8aSPeter Maydell } 50272889c8aSPeter Maydell 50372889c8aSPeter Maydell static int gic_get_prio_from_apr_bits(GICState *s, int cpu) 50472889c8aSPeter Maydell { 50572889c8aSPeter Maydell /* Recalculate the current running priority for this CPU based 50672889c8aSPeter Maydell * on the set bits in the Active Priority Registers. 50772889c8aSPeter Maydell */ 50872889c8aSPeter Maydell int i; 509a1d7b8d8SLuc Michel 510a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 511a1d7b8d8SLuc Michel uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)]; 512a1d7b8d8SLuc Michel if (apr) { 513a1d7b8d8SLuc Michel return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1); 514a1d7b8d8SLuc Michel } else { 515a1d7b8d8SLuc Michel return 0x100; 516a1d7b8d8SLuc Michel } 517a1d7b8d8SLuc Michel } 518a1d7b8d8SLuc Michel 51972889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 52072889c8aSPeter Maydell uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu]; 52172889c8aSPeter Maydell if (!apr) { 52272889c8aSPeter Maydell continue; 52372889c8aSPeter Maydell } 52472889c8aSPeter Maydell return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1); 52572889c8aSPeter Maydell } 52672889c8aSPeter Maydell return 0x100; 52772889c8aSPeter Maydell } 52872889c8aSPeter Maydell 52972889c8aSPeter Maydell static void gic_drop_prio(GICState *s, int cpu, int group) 53072889c8aSPeter Maydell { 53172889c8aSPeter Maydell /* Drop the priority of the currently active interrupt in the 53272889c8aSPeter Maydell * specified group. 53372889c8aSPeter Maydell * 53472889c8aSPeter Maydell * Note that we can guarantee (because of the requirement to nest 53572889c8aSPeter Maydell * GICC_IAR reads [which activate an interrupt and raise priority] 53672889c8aSPeter Maydell * with GICC_EOIR writes [which drop the priority for the interrupt]) 53772889c8aSPeter Maydell * that the interrupt we're being called for is the highest priority 53872889c8aSPeter Maydell * active interrupt, meaning that it has the lowest set bit in the 53972889c8aSPeter Maydell * APR registers. 54072889c8aSPeter Maydell * 54172889c8aSPeter Maydell * If the guest does not honour the ordering constraints then the 54272889c8aSPeter Maydell * behaviour of the GIC is UNPREDICTABLE, which for us means that 54372889c8aSPeter Maydell * the values of the APR registers might become incorrect and the 54472889c8aSPeter Maydell * running priority will be wrong, so interrupts that should preempt 54572889c8aSPeter Maydell * might not do so, and interrupts that should not preempt might do so. 54672889c8aSPeter Maydell */ 547a1d7b8d8SLuc Michel if (gic_is_vcpu(cpu)) { 548a1d7b8d8SLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 549a1d7b8d8SLuc Michel 550a1d7b8d8SLuc Michel if (s->h_apr[rcpu]) { 551a1d7b8d8SLuc Michel /* Clear lowest set bit */ 552a1d7b8d8SLuc Michel s->h_apr[rcpu] &= s->h_apr[rcpu] - 1; 553a1d7b8d8SLuc Michel } 554a1d7b8d8SLuc Michel } else { 55572889c8aSPeter Maydell int i; 55672889c8aSPeter Maydell 55772889c8aSPeter Maydell for (i = 0; i < GIC_NR_APRS; i++) { 55872889c8aSPeter Maydell uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu]; 55972889c8aSPeter Maydell if (!*papr) { 56072889c8aSPeter Maydell continue; 56172889c8aSPeter Maydell } 56272889c8aSPeter Maydell /* Clear lowest set bit */ 56372889c8aSPeter Maydell *papr &= *papr - 1; 56472889c8aSPeter Maydell break; 56572889c8aSPeter Maydell } 566a1d7b8d8SLuc Michel } 56772889c8aSPeter Maydell 56872889c8aSPeter Maydell s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu); 569e69954b9Spbrook } 570e69954b9Spbrook 571439badd6SLuc Michel static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu) 572e69954b9Spbrook { 573439badd6SLuc Michel int src; 574439badd6SLuc Michel uint32_t ret; 575c5619bf9SFabian Aggeler 576439badd6SLuc Michel if (!gic_is_vcpu(cpu)) { 57740d22500SChristoffer Dall /* Lookup the source CPU for the SGI and clear this in the 57840d22500SChristoffer Dall * sgi_pending map. Return the src and clear the overall pending 57940d22500SChristoffer Dall * state on this CPU if the SGI is not pending from any CPUs. 58040d22500SChristoffer Dall */ 58140d22500SChristoffer Dall assert(s->sgi_pending[irq][cpu] != 0); 58240d22500SChristoffer Dall src = ctz32(s->sgi_pending[irq][cpu]); 58340d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~(1 << src); 58440d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 58586b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 58640d22500SChristoffer Dall } 58740d22500SChristoffer Dall ret = irq | ((src & 0x7) << 10); 58840d22500SChristoffer Dall } else { 589439badd6SLuc Michel uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu); 590439badd6SLuc Michel src = GICH_LR_CPUID(*lr_entry); 591439badd6SLuc Michel 592439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 593439badd6SLuc Michel ret = irq | (src << 10); 594439badd6SLuc Michel } 595439badd6SLuc Michel 596439badd6SLuc Michel return ret; 597439badd6SLuc Michel } 598439badd6SLuc Michel 599439badd6SLuc Michel uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs) 600439badd6SLuc Michel { 601439badd6SLuc Michel int ret, irq; 602439badd6SLuc Michel 603439badd6SLuc Michel /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately 604439badd6SLuc Michel * for the case where this GIC supports grouping and the pending interrupt 605439badd6SLuc Michel * is in the wrong group. 60640d22500SChristoffer Dall */ 607439badd6SLuc Michel irq = gic_get_current_pending_irq(s, cpu, attrs); 608067a2b9cSLuc Michel trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 609067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), irq); 610439badd6SLuc Michel 611439badd6SLuc Michel if (irq >= GIC_MAXIRQ) { 612439badd6SLuc Michel DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq); 613439badd6SLuc Michel return irq; 614439badd6SLuc Michel } 615439badd6SLuc Michel 616439badd6SLuc Michel if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) { 617439badd6SLuc Michel DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq); 618439badd6SLuc Michel return 1023; 619439badd6SLuc Michel } 620439badd6SLuc Michel 621439badd6SLuc Michel gic_activate_irq(s, cpu, irq); 622439badd6SLuc Michel 623439badd6SLuc Michel if (s->revision == REV_11MPCORE) { 624439badd6SLuc Michel /* Clear pending flags for both level and edge triggered interrupts. 625439badd6SLuc Michel * Level triggered IRQs will be reasserted once they become inactive. 626439badd6SLuc Michel */ 627439badd6SLuc Michel gic_clear_pending(s, irq, cpu); 628439badd6SLuc Michel ret = irq; 629439badd6SLuc Michel } else { 630439badd6SLuc Michel if (irq < GIC_NR_SGIS) { 631439badd6SLuc Michel ret = gic_clear_pending_sgi(s, irq, cpu); 632439badd6SLuc Michel } else { 63386b350f0SLuc Michel gic_clear_pending(s, irq, cpu); 63440d22500SChristoffer Dall ret = irq; 63540d22500SChristoffer Dall } 63640d22500SChristoffer Dall } 63740d22500SChristoffer Dall 638cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 639cbe1282bSLuc Michel gic_update_virt(s); 640cbe1282bSLuc Michel } else { 64172889c8aSPeter Maydell gic_update(s); 642cbe1282bSLuc Michel } 64340d22500SChristoffer Dall DPRINTF("ACK %d\n", irq); 64440d22500SChristoffer Dall return ret; 645e69954b9Spbrook } 646e69954b9Spbrook 64711411489SSai Pavan Boddu static uint32_t gic_fullprio_mask(GICState *s, int cpu) 64811411489SSai Pavan Boddu { 64911411489SSai Pavan Boddu /* 65011411489SSai Pavan Boddu * Return a mask word which clears the unimplemented priority 65111411489SSai Pavan Boddu * bits from a priority value for an interrupt. (Not to be 65211411489SSai Pavan Boddu * confused with the group priority, whose mask depends on BPR.) 65311411489SSai Pavan Boddu */ 65411411489SSai Pavan Boddu int priBits; 65511411489SSai Pavan Boddu 65611411489SSai Pavan Boddu if (gic_is_vcpu(cpu)) { 65711411489SSai Pavan Boddu priBits = GIC_VIRT_MAX_GROUP_PRIO_BITS; 65811411489SSai Pavan Boddu } else { 65911411489SSai Pavan Boddu priBits = s->n_prio_bits; 66011411489SSai Pavan Boddu } 66111411489SSai Pavan Boddu return ~0U << (8 - priBits); 66211411489SSai Pavan Boddu } 66311411489SSai Pavan Boddu 66467ce697aSLuc Michel void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val, 66581508470SFabian Aggeler MemTxAttrs attrs) 6669df90ad0SChristoffer Dall { 66781508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 66867ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 66981508470SFabian Aggeler return; /* Ignore Non-secure access of Group0 IRQ */ 67081508470SFabian Aggeler } 67181508470SFabian Aggeler val = 0x80 | (val >> 1); /* Non-secure view */ 67281508470SFabian Aggeler } 67381508470SFabian Aggeler 67411411489SSai Pavan Boddu val &= gic_fullprio_mask(s, cpu); 67511411489SSai Pavan Boddu 6769df90ad0SChristoffer Dall if (irq < GIC_INTERNAL) { 6779df90ad0SChristoffer Dall s->priority1[irq][cpu] = val; 6789df90ad0SChristoffer Dall } else { 6799df90ad0SChristoffer Dall s->priority2[(irq) - GIC_INTERNAL] = val; 6809df90ad0SChristoffer Dall } 6819df90ad0SChristoffer Dall } 6829df90ad0SChristoffer Dall 68367ce697aSLuc Michel static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq, 68481508470SFabian Aggeler MemTxAttrs attrs) 68581508470SFabian Aggeler { 68667ce697aSLuc Michel uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu); 68781508470SFabian Aggeler 68881508470SFabian Aggeler if (s->security_extn && !attrs.secure) { 68967ce697aSLuc Michel if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 69081508470SFabian Aggeler return 0; /* Non-secure access cannot read priority of Group0 IRQ */ 69181508470SFabian Aggeler } 69281508470SFabian Aggeler prio = (prio << 1) & 0xff; /* Non-secure view */ 69381508470SFabian Aggeler } 69411411489SSai Pavan Boddu return prio & gic_fullprio_mask(s, cpu); 69581508470SFabian Aggeler } 69681508470SFabian Aggeler 69781508470SFabian Aggeler static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask, 69881508470SFabian Aggeler MemTxAttrs attrs) 69981508470SFabian Aggeler { 7003dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 70181508470SFabian Aggeler if (s->priority_mask[cpu] & 0x80) { 70281508470SFabian Aggeler /* Priority Mask in upper half */ 70381508470SFabian Aggeler pmask = 0x80 | (pmask >> 1); 70481508470SFabian Aggeler } else { 70581508470SFabian Aggeler /* Non-secure write ignored if priority mask is in lower half */ 70681508470SFabian Aggeler return; 70781508470SFabian Aggeler } 70881508470SFabian Aggeler } 70911411489SSai Pavan Boddu s->priority_mask[cpu] = pmask & gic_fullprio_mask(s, cpu); 71081508470SFabian Aggeler } 71181508470SFabian Aggeler 71281508470SFabian Aggeler static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs) 71381508470SFabian Aggeler { 71481508470SFabian Aggeler uint32_t pmask = s->priority_mask[cpu]; 71581508470SFabian Aggeler 7163dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 71781508470SFabian Aggeler if (pmask & 0x80) { 71881508470SFabian Aggeler /* Priority Mask in upper half, return Non-secure view */ 71981508470SFabian Aggeler pmask = (pmask << 1) & 0xff; 72081508470SFabian Aggeler } else { 72181508470SFabian Aggeler /* Priority Mask in lower half, RAZ */ 72281508470SFabian Aggeler pmask = 0; 72381508470SFabian Aggeler } 72481508470SFabian Aggeler } 72581508470SFabian Aggeler return pmask; 72681508470SFabian Aggeler } 72781508470SFabian Aggeler 72832951860SFabian Aggeler static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs) 72932951860SFabian Aggeler { 73032951860SFabian Aggeler uint32_t ret = s->cpu_ctlr[cpu]; 73132951860SFabian Aggeler 7323dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 73332951860SFabian Aggeler /* Construct the NS banked view of GICC_CTLR from the correct 73432951860SFabian Aggeler * bits of the S banked view. We don't need to move the bypass 73532951860SFabian Aggeler * control bits because we don't implement that (IMPDEF) part 73632951860SFabian Aggeler * of the GIC architecture. 73732951860SFabian Aggeler */ 73832951860SFabian Aggeler ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1; 73932951860SFabian Aggeler } 74032951860SFabian Aggeler return ret; 74132951860SFabian Aggeler } 74232951860SFabian Aggeler 74332951860SFabian Aggeler static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value, 74432951860SFabian Aggeler MemTxAttrs attrs) 74532951860SFabian Aggeler { 74632951860SFabian Aggeler uint32_t mask; 74732951860SFabian Aggeler 7483dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 74932951860SFabian Aggeler /* The NS view can only write certain bits in the register; 75032951860SFabian Aggeler * the rest are unchanged 75132951860SFabian Aggeler */ 75232951860SFabian Aggeler mask = GICC_CTLR_EN_GRP1; 75332951860SFabian Aggeler if (s->revision == 2) { 75432951860SFabian Aggeler mask |= GICC_CTLR_EOIMODE_NS; 75532951860SFabian Aggeler } 75632951860SFabian Aggeler s->cpu_ctlr[cpu] &= ~mask; 75732951860SFabian Aggeler s->cpu_ctlr[cpu] |= (value << 1) & mask; 75832951860SFabian Aggeler } else { 75932951860SFabian Aggeler if (s->revision == 2) { 76032951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK; 76132951860SFabian Aggeler } else { 76232951860SFabian Aggeler mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK; 76332951860SFabian Aggeler } 76432951860SFabian Aggeler s->cpu_ctlr[cpu] = value & mask; 76532951860SFabian Aggeler } 76632951860SFabian Aggeler DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, " 76732951860SFabian Aggeler "Group1 Interrupts %sabled\n", cpu, 76832951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis", 76932951860SFabian Aggeler (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis"); 77032951860SFabian Aggeler } 77132951860SFabian Aggeler 77208efa9f2SFabian Aggeler static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs) 77308efa9f2SFabian Aggeler { 77471aa735bSLuc MICHEL if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) { 77571aa735bSLuc MICHEL /* Idle priority */ 77671aa735bSLuc MICHEL return 0xff; 77771aa735bSLuc MICHEL } 77871aa735bSLuc MICHEL 7793dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 78008efa9f2SFabian Aggeler if (s->running_priority[cpu] & 0x80) { 78108efa9f2SFabian Aggeler /* Running priority in upper half of range: return the Non-secure 78208efa9f2SFabian Aggeler * view of the priority. 78308efa9f2SFabian Aggeler */ 78408efa9f2SFabian Aggeler return s->running_priority[cpu] << 1; 78508efa9f2SFabian Aggeler } else { 78608efa9f2SFabian Aggeler /* Running priority in lower half of range: RAZ */ 78708efa9f2SFabian Aggeler return 0; 78808efa9f2SFabian Aggeler } 78908efa9f2SFabian Aggeler } else { 79008efa9f2SFabian Aggeler return s->running_priority[cpu]; 79108efa9f2SFabian Aggeler } 79208efa9f2SFabian Aggeler } 79308efa9f2SFabian Aggeler 794a55c910eSPeter Maydell /* Return true if we should split priority drop and interrupt deactivation, 795a55c910eSPeter Maydell * ie whether the relevant EOIMode bit is set. 796a55c910eSPeter Maydell */ 797a55c910eSPeter Maydell static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs) 798a55c910eSPeter Maydell { 799a55c910eSPeter Maydell if (s->revision != 2) { 800a55c910eSPeter Maydell /* Before GICv2 prio-drop and deactivate are not separable */ 801a55c910eSPeter Maydell return false; 802a55c910eSPeter Maydell } 8033dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 804a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS; 805a55c910eSPeter Maydell } 806a55c910eSPeter Maydell return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE; 807a55c910eSPeter Maydell } 808a55c910eSPeter Maydell 809a55c910eSPeter Maydell static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 810a55c910eSPeter Maydell { 811ee03cca8SPeter Maydell int group; 812ee03cca8SPeter Maydell 81302f2e22dSLuc Michel if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) { 814ee03cca8SPeter Maydell /* 815ee03cca8SPeter Maydell * This handles two cases: 816ee03cca8SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 817ee03cca8SPeter Maydell * to the GICC_DIR, the GIC ignores that write. 818ee03cca8SPeter Maydell * 2. If software writes the number of a non-existent interrupt 819ee03cca8SPeter Maydell * this must be a subcase of "value written is not an active interrupt" 82002f2e22dSLuc Michel * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs, 82102f2e22dSLuc Michel * all IRQs potentially exist, so this limit does not apply. 822ee03cca8SPeter Maydell */ 823ee03cca8SPeter Maydell return; 824ee03cca8SPeter Maydell } 825ee03cca8SPeter Maydell 826a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 827a55c910eSPeter Maydell /* This is UNPREDICTABLE; we choose to ignore it */ 828a55c910eSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 829a55c910eSPeter Maydell "gic_deactivate_irq: GICC_DIR write when EOIMode clear"); 830a55c910eSPeter Maydell return; 831a55c910eSPeter Maydell } 832a55c910eSPeter Maydell 83302f2e22dSLuc Michel if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) { 83402f2e22dSLuc Michel /* This vIRQ does not have an LR entry which is either active or 83502f2e22dSLuc Michel * pending and active. Increment EOICount and ignore the write. 83602f2e22dSLuc Michel */ 83702f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 83802f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 839cbe1282bSLuc Michel 840cbe1282bSLuc Michel /* Update the virtual interface in case a maintenance interrupt should 841cbe1282bSLuc Michel * be raised. 842cbe1282bSLuc Michel */ 843cbe1282bSLuc Michel gic_update_virt(s); 84402f2e22dSLuc Michel return; 84502f2e22dSLuc Michel } 84602f2e22dSLuc Michel 84702f2e22dSLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 84802f2e22dSLuc Michel 8493dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 850a55c910eSPeter Maydell DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq); 851a55c910eSPeter Maydell return; 852a55c910eSPeter Maydell } 853a55c910eSPeter Maydell 85486b350f0SLuc Michel gic_clear_active(s, irq, cpu); 855a55c910eSPeter Maydell } 856a55c910eSPeter Maydell 85750491c56SLuc Michel static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 858e69954b9Spbrook { 8599ee6e8bbSpbrook int cm = 1 << cpu; 86072889c8aSPeter Maydell int group; 86172889c8aSPeter Maydell 862df628ff1Spbrook DPRINTF("EOI %d\n", irq); 86302f2e22dSLuc Michel if (gic_is_vcpu(cpu)) { 86402f2e22dSLuc Michel /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the 86502f2e22dSLuc Michel * running prio is < 0x100. 86602f2e22dSLuc Michel */ 86702f2e22dSLuc Michel bool prio_drop = s->running_priority[cpu] < 0x100; 86802f2e22dSLuc Michel 86902f2e22dSLuc Michel if (irq >= GIC_MAXIRQ) { 87002f2e22dSLuc Michel /* Ignore spurious interrupt */ 87102f2e22dSLuc Michel return; 87202f2e22dSLuc Michel } 87302f2e22dSLuc Michel 87402f2e22dSLuc Michel gic_drop_prio(s, cpu, 0); 87502f2e22dSLuc Michel 87602f2e22dSLuc Michel if (!gic_eoi_split(s, cpu, attrs)) { 87702f2e22dSLuc Michel bool valid = gic_virq_is_valid(s, irq, cpu); 87802f2e22dSLuc Michel if (prio_drop && !valid) { 87902f2e22dSLuc Michel /* We are in a situation where: 88002f2e22dSLuc Michel * - V_CTRL.EOIMode is false (no EOI split), 88102f2e22dSLuc Michel * - The call to gic_drop_prio() cleared a bit in GICH_APR, 88202f2e22dSLuc Michel * - This vIRQ does not have an LR entry which is either 88302f2e22dSLuc Michel * active or pending and active. 88402f2e22dSLuc Michel * In that case, we must increment EOICount. 88502f2e22dSLuc Michel */ 88602f2e22dSLuc Michel int rcpu = gic_get_vcpu_real_id(cpu); 88702f2e22dSLuc Michel s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT; 88802f2e22dSLuc Michel } else if (valid) { 88902f2e22dSLuc Michel gic_clear_active(s, irq, cpu); 89002f2e22dSLuc Michel } 89102f2e22dSLuc Michel } 89202f2e22dSLuc Michel 893cbe1282bSLuc Michel gic_update_virt(s); 89402f2e22dSLuc Michel return; 89502f2e22dSLuc Michel } 89602f2e22dSLuc Michel 897a32134aaSMark Langsdorf if (irq >= s->num_irq) { 898217bfb44SPeter Maydell /* This handles two cases: 899217bfb44SPeter Maydell * 1. If software writes the ID of a spurious interrupt [ie 1023] 900217bfb44SPeter Maydell * to the GICC_EOIR, the GIC ignores that write. 901217bfb44SPeter Maydell * 2. If software writes the number of a non-existent interrupt 902217bfb44SPeter Maydell * this must be a subcase of "value written does not match the last 903217bfb44SPeter Maydell * valid interrupt value read from the Interrupt Acknowledge 904217bfb44SPeter Maydell * register" and so this is UNPREDICTABLE. We choose to ignore it. 905217bfb44SPeter Maydell */ 906217bfb44SPeter Maydell return; 907217bfb44SPeter Maydell } 90872889c8aSPeter Maydell if (s->running_priority[cpu] == 0x100) { 909e69954b9Spbrook return; /* No active IRQ. */ 91072889c8aSPeter Maydell } 9118d999995SChristoffer Dall 9123bc4b52cSMarcin Krzeminski if (s->revision == REV_11MPCORE) { 913e69954b9Spbrook /* Mark level triggered interrupts as pending if they are still 914e69954b9Spbrook raised. */ 91567ce697aSLuc Michel if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm) 91667ce697aSLuc Michel && GIC_DIST_TEST_LEVEL(irq, cm) 91767ce697aSLuc Michel && (GIC_DIST_TARGET(irq) & cm) != 0) { 9189ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq, cm); 91967ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, cm); 920e69954b9Spbrook } 9218d999995SChristoffer Dall } 9228d999995SChristoffer Dall 92386b350f0SLuc Michel group = gic_has_groups(s) && gic_test_group(s, irq, cpu); 92472889c8aSPeter Maydell 9253dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs) && !group) { 926f9c6a7f1SFabian Aggeler DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq); 927f9c6a7f1SFabian Aggeler return; 928f9c6a7f1SFabian Aggeler } 929f9c6a7f1SFabian Aggeler 930f9c6a7f1SFabian Aggeler /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1 931f9c6a7f1SFabian Aggeler * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1, 932f9c6a7f1SFabian Aggeler * i.e. go ahead and complete the irq anyway. 933f9c6a7f1SFabian Aggeler */ 934f9c6a7f1SFabian Aggeler 93572889c8aSPeter Maydell gic_drop_prio(s, cpu, group); 936a55c910eSPeter Maydell 937a55c910eSPeter Maydell /* In GICv2 the guest can choose to split priority-drop and deactivate */ 938a55c910eSPeter Maydell if (!gic_eoi_split(s, cpu, attrs)) { 93986b350f0SLuc Michel gic_clear_active(s, irq, cpu); 940a55c910eSPeter Maydell } 941e69954b9Spbrook gic_update(s); 942e69954b9Spbrook } 943e69954b9Spbrook 944a9d85353SPeter Maydell static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs) 945e69954b9Spbrook { 946fae15286SPeter Maydell GICState *s = (GICState *)opaque; 947e69954b9Spbrook uint32_t res; 948e69954b9Spbrook int irq; 949e69954b9Spbrook int i; 9509ee6e8bbSpbrook int cpu; 9519ee6e8bbSpbrook int cm; 9529ee6e8bbSpbrook int mask; 953e69954b9Spbrook 954926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 9559ee6e8bbSpbrook cm = 1 << cpu; 956e69954b9Spbrook if (offset < 0x100) { 957679aa175SFabian Aggeler if (offset == 0) { /* GICD_CTLR */ 958679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 959679aa175SFabian Aggeler /* The NS bank of this register is just an alias of the 960679aa175SFabian Aggeler * EnableGrp1 bit in the S bank version. 961679aa175SFabian Aggeler */ 962679aa175SFabian Aggeler return extract32(s->ctlr, 1, 1); 963679aa175SFabian Aggeler } else { 964679aa175SFabian Aggeler return s->ctlr; 965679aa175SFabian Aggeler } 966679aa175SFabian Aggeler } 967e69954b9Spbrook if (offset == 4) 9685543d1abSFabian Aggeler /* Interrupt Controller Type Register */ 9695543d1abSFabian Aggeler return ((s->num_irq / 32) - 1) 970b95690c9SWei Huang | ((s->num_cpu - 1) << 5) 9715543d1abSFabian Aggeler | (s->security_extn << 10); 972e69954b9Spbrook if (offset < 0x08) 973e69954b9Spbrook return 0; 974b79f2265SRob Herring if (offset >= 0x80) { 975c27a5ba9SFabian Aggeler /* Interrupt Group Registers: these RAZ/WI if this is an NS 976c27a5ba9SFabian Aggeler * access to a GIC with the security extensions, or if the GIC 977c27a5ba9SFabian Aggeler * doesn't have groups at all. 978c27a5ba9SFabian Aggeler */ 979c27a5ba9SFabian Aggeler res = 0; 980c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 981c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 982b6e6c651SPeter Maydell irq = (offset - 0x080) * 8; 983c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 984c27a5ba9SFabian Aggeler goto bad_reg; 985c27a5ba9SFabian Aggeler } 986c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 98767ce697aSLuc Michel if (GIC_DIST_TEST_GROUP(irq + i, cm)) { 988c27a5ba9SFabian Aggeler res |= (1 << i); 989c27a5ba9SFabian Aggeler } 990c27a5ba9SFabian Aggeler } 991c27a5ba9SFabian Aggeler } 992c27a5ba9SFabian Aggeler return res; 993b79f2265SRob Herring } 994e69954b9Spbrook goto bad_reg; 995e69954b9Spbrook } else if (offset < 0x200) { 996e69954b9Spbrook /* Interrupt Set/Clear Enable. */ 997e69954b9Spbrook if (offset < 0x180) 998e69954b9Spbrook irq = (offset - 0x100) * 8; 999e69954b9Spbrook else 1000e69954b9Spbrook irq = (offset - 0x180) * 8; 1001a32134aaSMark Langsdorf if (irq >= s->num_irq) 1002e69954b9Spbrook goto bad_reg; 1003e69954b9Spbrook res = 0; 1004e69954b9Spbrook for (i = 0; i < 8; i++) { 1005fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 100667ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1007fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1008fea8a08eSJens Wiklander } 1009fea8a08eSJens Wiklander 101067ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1011e69954b9Spbrook res |= (1 << i); 1012e69954b9Spbrook } 1013e69954b9Spbrook } 1014e69954b9Spbrook } else if (offset < 0x300) { 1015e69954b9Spbrook /* Interrupt Set/Clear Pending. */ 1016e69954b9Spbrook if (offset < 0x280) 1017e69954b9Spbrook irq = (offset - 0x200) * 8; 1018e69954b9Spbrook else 1019e69954b9Spbrook irq = (offset - 0x280) * 8; 1020a32134aaSMark Langsdorf if (irq >= s->num_irq) 1021e69954b9Spbrook goto bad_reg; 1022e69954b9Spbrook res = 0; 102369253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 1024e69954b9Spbrook for (i = 0; i < 8; i++) { 1025fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 102667ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1027fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1028fea8a08eSJens Wiklander } 1029fea8a08eSJens Wiklander 10308d999995SChristoffer Dall if (gic_test_pending(s, irq + i, mask)) { 1031e69954b9Spbrook res |= (1 << i); 1032e69954b9Spbrook } 1033e69954b9Spbrook } 1034e69954b9Spbrook } else if (offset < 0x400) { 10353bb0b038SLuc Michel /* Interrupt Set/Clear Active. */ 10363bb0b038SLuc Michel if (offset < 0x380) { 10373bb0b038SLuc Michel irq = (offset - 0x300) * 8; 10383bb0b038SLuc Michel } else if (s->revision == 2) { 10393bb0b038SLuc Michel irq = (offset - 0x380) * 8; 10403bb0b038SLuc Michel } else { 10413bb0b038SLuc Michel goto bad_reg; 10423bb0b038SLuc Michel } 10433bb0b038SLuc Michel 1044a32134aaSMark Langsdorf if (irq >= s->num_irq) 1045e69954b9Spbrook goto bad_reg; 1046e69954b9Spbrook res = 0; 104769253800SRusty Russell mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 1048e69954b9Spbrook for (i = 0; i < 8; i++) { 1049fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 105067ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1051fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1052fea8a08eSJens Wiklander } 1053fea8a08eSJens Wiklander 105467ce697aSLuc Michel if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) { 1055e69954b9Spbrook res |= (1 << i); 1056e69954b9Spbrook } 1057e69954b9Spbrook } 1058e69954b9Spbrook } else if (offset < 0x800) { 1059e69954b9Spbrook /* Interrupt Priority. */ 1060b6e6c651SPeter Maydell irq = (offset - 0x400); 1061a32134aaSMark Langsdorf if (irq >= s->num_irq) 1062e69954b9Spbrook goto bad_reg; 106367ce697aSLuc Michel res = gic_dist_get_priority(s, cpu, irq, attrs); 1064e69954b9Spbrook } else if (offset < 0xc00) { 1065e69954b9Spbrook /* Interrupt CPU Target. */ 10666b9680bbSPeter Maydell if (s->num_cpu == 1 && s->revision != REV_11MPCORE) { 10676b9680bbSPeter Maydell /* For uniprocessor GICs these RAZ/WI */ 10686b9680bbSPeter Maydell res = 0; 10696b9680bbSPeter Maydell } else { 1070b6e6c651SPeter Maydell irq = (offset - 0x800); 10716b9680bbSPeter Maydell if (irq >= s->num_irq) { 1072e69954b9Spbrook goto bad_reg; 10736b9680bbSPeter Maydell } 10747995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 10757995206dSPeter Maydell res = 0; 10767995206dSPeter Maydell } else if (irq < GIC_INTERNAL) { 10779ee6e8bbSpbrook res = cm; 10789ee6e8bbSpbrook } else { 107967ce697aSLuc Michel res = GIC_DIST_TARGET(irq); 10809ee6e8bbSpbrook } 10816b9680bbSPeter Maydell } 1082e69954b9Spbrook } else if (offset < 0xf00) { 1083e69954b9Spbrook /* Interrupt Configuration. */ 1084b6e6c651SPeter Maydell irq = (offset - 0xc00) * 4; 1085a32134aaSMark Langsdorf if (irq >= s->num_irq) 1086e69954b9Spbrook goto bad_reg; 1087e69954b9Spbrook res = 0; 1088e69954b9Spbrook for (i = 0; i < 4; i++) { 1089fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 109067ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1091fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1092fea8a08eSJens Wiklander } 1093fea8a08eSJens Wiklander 109467ce697aSLuc Michel if (GIC_DIST_TEST_MODEL(irq + i)) { 1095e69954b9Spbrook res |= (1 << (i * 2)); 109667ce697aSLuc Michel } 109767ce697aSLuc Michel if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 1098e69954b9Spbrook res |= (2 << (i * 2)); 1099e69954b9Spbrook } 110067ce697aSLuc Michel } 110140d22500SChristoffer Dall } else if (offset < 0xf10) { 110240d22500SChristoffer Dall goto bad_reg; 110340d22500SChristoffer Dall } else if (offset < 0xf30) { 11047c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 110540d22500SChristoffer Dall goto bad_reg; 110640d22500SChristoffer Dall } 110740d22500SChristoffer Dall 110840d22500SChristoffer Dall if (offset < 0xf20) { 110940d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 111040d22500SChristoffer Dall irq = (offset - 0xf10); 111140d22500SChristoffer Dall } else { 111240d22500SChristoffer Dall irq = (offset - 0xf20); 111340d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 111440d22500SChristoffer Dall } 111540d22500SChristoffer Dall 1116fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 111767ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 1118fea8a08eSJens Wiklander res = 0; /* Ignore Non-secure access of Group0 IRQ */ 1119fea8a08eSJens Wiklander } else { 112040d22500SChristoffer Dall res = s->sgi_pending[irq][cpu]; 1121fea8a08eSJens Wiklander } 11223355c360SAlistair Francis } else if (offset < 0xfd0) { 1123e69954b9Spbrook goto bad_reg; 11243355c360SAlistair Francis } else if (offset < 0x1000) { 1125e69954b9Spbrook if (offset & 3) { 1126e69954b9Spbrook res = 0; 1127e69954b9Spbrook } else { 11283355c360SAlistair Francis switch (s->revision) { 11293355c360SAlistair Francis case REV_11MPCORE: 11303355c360SAlistair Francis res = gic_id_11mpcore[(offset - 0xfd0) >> 2]; 11313355c360SAlistair Francis break; 11323355c360SAlistair Francis case 1: 11333355c360SAlistair Francis res = gic_id_gicv1[(offset - 0xfd0) >> 2]; 11343355c360SAlistair Francis break; 11353355c360SAlistair Francis case 2: 11363355c360SAlistair Francis res = gic_id_gicv2[(offset - 0xfd0) >> 2]; 11373355c360SAlistair Francis break; 11383355c360SAlistair Francis default: 11393355c360SAlistair Francis res = 0; 1140e69954b9Spbrook } 1141e69954b9Spbrook } 11423355c360SAlistair Francis } else { 11433355c360SAlistair Francis g_assert_not_reached(); 11443355c360SAlistair Francis } 1145e69954b9Spbrook return res; 1146e69954b9Spbrook bad_reg: 11478c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 11488c8dc39fSPeter Maydell "gic_dist_readb: Bad offset %x\n", (int)offset); 1149e69954b9Spbrook return 0; 1150e69954b9Spbrook } 1151e69954b9Spbrook 1152a9d85353SPeter Maydell static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data, 1153a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1154e69954b9Spbrook { 1155a9d85353SPeter Maydell switch (size) { 1156a9d85353SPeter Maydell case 1: 1157a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1158067a2b9cSLuc Michel break; 1159a9d85353SPeter Maydell case 2: 1160a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1161a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1162067a2b9cSLuc Michel break; 1163a9d85353SPeter Maydell case 4: 1164a9d85353SPeter Maydell *data = gic_dist_readb(opaque, offset, attrs); 1165a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 1166a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16; 1167a9d85353SPeter Maydell *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24; 1168067a2b9cSLuc Michel break; 1169a9d85353SPeter Maydell default: 1170a9d85353SPeter Maydell return MEMTX_ERROR; 1171e69954b9Spbrook } 1172067a2b9cSLuc Michel 1173067a2b9cSLuc Michel trace_gic_dist_read(offset, size, *data); 1174067a2b9cSLuc Michel return MEMTX_OK; 1175e69954b9Spbrook } 1176e69954b9Spbrook 1177a8170e5eSAvi Kivity static void gic_dist_writeb(void *opaque, hwaddr offset, 1178a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1179e69954b9Spbrook { 1180fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1181e69954b9Spbrook int irq; 1182e69954b9Spbrook int i; 11839ee6e8bbSpbrook int cpu; 1184e69954b9Spbrook 1185926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 1186e69954b9Spbrook if (offset < 0x100) { 1187e69954b9Spbrook if (offset == 0) { 1188679aa175SFabian Aggeler if (s->security_extn && !attrs.secure) { 1189679aa175SFabian Aggeler /* NS version is just an alias of the S version's bit 1 */ 1190679aa175SFabian Aggeler s->ctlr = deposit32(s->ctlr, 1, 1, value); 1191679aa175SFabian Aggeler } else if (gic_has_groups(s)) { 1192679aa175SFabian Aggeler s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1); 1193679aa175SFabian Aggeler } else { 1194679aa175SFabian Aggeler s->ctlr = value & GICD_CTLR_EN_GRP0; 1195679aa175SFabian Aggeler } 1196679aa175SFabian Aggeler DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n", 1197679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis", 1198679aa175SFabian Aggeler s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis"); 1199e69954b9Spbrook } else if (offset < 4) { 1200e69954b9Spbrook /* ignored. */ 1201b79f2265SRob Herring } else if (offset >= 0x80) { 1202c27a5ba9SFabian Aggeler /* Interrupt Group Registers: RAZ/WI for NS access to secure 1203c27a5ba9SFabian Aggeler * GIC, or for GICs without groups. 1204c27a5ba9SFabian Aggeler */ 1205c27a5ba9SFabian Aggeler if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 1206c27a5ba9SFabian Aggeler /* Every byte offset holds 8 group status bits */ 1207b6e6c651SPeter Maydell irq = (offset - 0x80) * 8; 1208c27a5ba9SFabian Aggeler if (irq >= s->num_irq) { 1209c27a5ba9SFabian Aggeler goto bad_reg; 1210c27a5ba9SFabian Aggeler } 1211c27a5ba9SFabian Aggeler for (i = 0; i < 8; i++) { 1212c27a5ba9SFabian Aggeler /* Group bits are banked for private interrupts */ 1213c27a5ba9SFabian Aggeler int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 1214c27a5ba9SFabian Aggeler if (value & (1 << i)) { 1215c27a5ba9SFabian Aggeler /* Group1 (Non-secure) */ 121667ce697aSLuc Michel GIC_DIST_SET_GROUP(irq + i, cm); 1217c27a5ba9SFabian Aggeler } else { 1218c27a5ba9SFabian Aggeler /* Group0 (Secure) */ 121967ce697aSLuc Michel GIC_DIST_CLEAR_GROUP(irq + i, cm); 1220c27a5ba9SFabian Aggeler } 1221c27a5ba9SFabian Aggeler } 1222c27a5ba9SFabian Aggeler } 1223e69954b9Spbrook } else { 1224e69954b9Spbrook goto bad_reg; 1225e69954b9Spbrook } 1226e69954b9Spbrook } else if (offset < 0x180) { 1227e69954b9Spbrook /* Interrupt Set Enable. */ 1228b6e6c651SPeter Maydell irq = (offset - 0x100) * 8; 1229a32134aaSMark Langsdorf if (irq >= s->num_irq) 1230e69954b9Spbrook goto bad_reg; 123141ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12329ee6e8bbSpbrook value = 0xff; 123341ab7b55SChristoffer Dall } 123441ab7b55SChristoffer Dall 1235e69954b9Spbrook for (i = 0; i < 8; i++) { 1236e69954b9Spbrook if (value & (1 << i)) { 1237f47b48fbSDaniel Sangorrin int mask = 123867ce697aSLuc Michel (irq < GIC_INTERNAL) ? (1 << cpu) 123967ce697aSLuc Michel : GIC_DIST_TARGET(irq + i); 124069253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 124141bf234dSRabin Vincent 1242fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 124367ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1244fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1245fea8a08eSJens Wiklander } 1246fea8a08eSJens Wiklander 124767ce697aSLuc Michel if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1248e69954b9Spbrook DPRINTF("Enabled IRQ %d\n", irq + i); 12492531088fSHollis Blanchard trace_gic_enable_irq(irq + i); 125041bf234dSRabin Vincent } 125167ce697aSLuc Michel GIC_DIST_SET_ENABLED(irq + i, cm); 1252e69954b9Spbrook /* If a raised level triggered IRQ enabled then mark 1253e69954b9Spbrook is as pending. */ 125467ce697aSLuc Michel if (GIC_DIST_TEST_LEVEL(irq + i, mask) 125567ce697aSLuc Michel && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 12569ee6e8bbSpbrook DPRINTF("Set %d pending mask %x\n", irq + i, mask); 125767ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, mask); 12589ee6e8bbSpbrook } 1259e69954b9Spbrook } 1260e69954b9Spbrook } 1261e69954b9Spbrook } else if (offset < 0x200) { 1262e69954b9Spbrook /* Interrupt Clear Enable. */ 1263b6e6c651SPeter Maydell irq = (offset - 0x180) * 8; 1264a32134aaSMark Langsdorf if (irq >= s->num_irq) 1265e69954b9Spbrook goto bad_reg; 126641ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12679ee6e8bbSpbrook value = 0; 126841ab7b55SChristoffer Dall } 126941ab7b55SChristoffer Dall 1270e69954b9Spbrook for (i = 0; i < 8; i++) { 1271e69954b9Spbrook if (value & (1 << i)) { 127269253800SRusty Russell int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 127341bf234dSRabin Vincent 1274fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 127567ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1276fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1277fea8a08eSJens Wiklander } 1278fea8a08eSJens Wiklander 127967ce697aSLuc Michel if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 1280e69954b9Spbrook DPRINTF("Disabled IRQ %d\n", irq + i); 12812531088fSHollis Blanchard trace_gic_disable_irq(irq + i); 128241bf234dSRabin Vincent } 128367ce697aSLuc Michel GIC_DIST_CLEAR_ENABLED(irq + i, cm); 1284e69954b9Spbrook } 1285e69954b9Spbrook } 1286e69954b9Spbrook } else if (offset < 0x280) { 1287e69954b9Spbrook /* Interrupt Set Pending. */ 1288b6e6c651SPeter Maydell irq = (offset - 0x200) * 8; 1289a32134aaSMark Langsdorf if (irq >= s->num_irq) 1290e69954b9Spbrook goto bad_reg; 129141ab7b55SChristoffer Dall if (irq < GIC_NR_SGIS) { 12925b0adce1SChristoffer Dall value = 0; 129341ab7b55SChristoffer Dall } 12949ee6e8bbSpbrook 1295e69954b9Spbrook for (i = 0; i < 8; i++) { 1296e69954b9Spbrook if (value & (1 << i)) { 1297fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 129867ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1299fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1300fea8a08eSJens Wiklander } 1301fea8a08eSJens Wiklander 130267ce697aSLuc Michel GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i)); 1303e69954b9Spbrook } 1304e69954b9Spbrook } 1305e69954b9Spbrook } else if (offset < 0x300) { 1306e69954b9Spbrook /* Interrupt Clear Pending. */ 1307b6e6c651SPeter Maydell irq = (offset - 0x280) * 8; 1308a32134aaSMark Langsdorf if (irq >= s->num_irq) 1309e69954b9Spbrook goto bad_reg; 13105b0adce1SChristoffer Dall if (irq < GIC_NR_SGIS) { 13115b0adce1SChristoffer Dall value = 0; 13125b0adce1SChristoffer Dall } 13135b0adce1SChristoffer Dall 1314e69954b9Spbrook for (i = 0; i < 8; i++) { 1315fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 131667ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1317fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1318fea8a08eSJens Wiklander } 1319fea8a08eSJens Wiklander 13209ee6e8bbSpbrook /* ??? This currently clears the pending bit for all CPUs, even 13219ee6e8bbSpbrook for per-CPU interrupts. It's unclear whether this is the 13229ee6e8bbSpbrook corect behavior. */ 1323e69954b9Spbrook if (value & (1 << i)) { 132467ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK); 1325e69954b9Spbrook } 1326e69954b9Spbrook } 13273bb0b038SLuc Michel } else if (offset < 0x380) { 13283bb0b038SLuc Michel /* Interrupt Set Active. */ 13293bb0b038SLuc Michel if (s->revision != 2) { 1330e69954b9Spbrook goto bad_reg; 13313bb0b038SLuc Michel } 13323bb0b038SLuc Michel 1333b6e6c651SPeter Maydell irq = (offset - 0x300) * 8; 13343bb0b038SLuc Michel if (irq >= s->num_irq) { 13353bb0b038SLuc Michel goto bad_reg; 13363bb0b038SLuc Michel } 13373bb0b038SLuc Michel 13383bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 13393bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 13403bb0b038SLuc Michel 13413bb0b038SLuc Michel for (i = 0; i < 8; i++) { 13423bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 13433bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 13443bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 13453bb0b038SLuc Michel } 13463bb0b038SLuc Michel 13473bb0b038SLuc Michel if (value & (1 << i)) { 13483bb0b038SLuc Michel GIC_DIST_SET_ACTIVE(irq + i, cm); 13493bb0b038SLuc Michel } 13503bb0b038SLuc Michel } 13513bb0b038SLuc Michel } else if (offset < 0x400) { 13523bb0b038SLuc Michel /* Interrupt Clear Active. */ 13533bb0b038SLuc Michel if (s->revision != 2) { 13543bb0b038SLuc Michel goto bad_reg; 13553bb0b038SLuc Michel } 13563bb0b038SLuc Michel 1357b6e6c651SPeter Maydell irq = (offset - 0x380) * 8; 13583bb0b038SLuc Michel if (irq >= s->num_irq) { 13593bb0b038SLuc Michel goto bad_reg; 13603bb0b038SLuc Michel } 13613bb0b038SLuc Michel 13623bb0b038SLuc Michel /* This register is banked per-cpu for PPIs */ 13633bb0b038SLuc Michel int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 13643bb0b038SLuc Michel 13653bb0b038SLuc Michel for (i = 0; i < 8; i++) { 13663bb0b038SLuc Michel if (s->security_extn && !attrs.secure && 13673bb0b038SLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 13683bb0b038SLuc Michel continue; /* Ignore Non-secure access of Group0 IRQ */ 13693bb0b038SLuc Michel } 13703bb0b038SLuc Michel 13713bb0b038SLuc Michel if (value & (1 << i)) { 13723bb0b038SLuc Michel GIC_DIST_CLEAR_ACTIVE(irq + i, cm); 13733bb0b038SLuc Michel } 13743bb0b038SLuc Michel } 1375e69954b9Spbrook } else if (offset < 0x800) { 1376e69954b9Spbrook /* Interrupt Priority. */ 1377b6e6c651SPeter Maydell irq = (offset - 0x400); 1378a32134aaSMark Langsdorf if (irq >= s->num_irq) 1379e69954b9Spbrook goto bad_reg; 138067ce697aSLuc Michel gic_dist_set_priority(s, cpu, irq, value, attrs); 1381e69954b9Spbrook } else if (offset < 0xc00) { 13826b9680bbSPeter Maydell /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the 13836b9680bbSPeter Maydell * annoying exception of the 11MPCore's GIC. 13846b9680bbSPeter Maydell */ 13856b9680bbSPeter Maydell if (s->num_cpu != 1 || s->revision == REV_11MPCORE) { 1386b6e6c651SPeter Maydell irq = (offset - 0x800); 13876b9680bbSPeter Maydell if (irq >= s->num_irq) { 1388e69954b9Spbrook goto bad_reg; 13896b9680bbSPeter Maydell } 13907995206dSPeter Maydell if (irq < 29 && s->revision == REV_11MPCORE) { 13919ee6e8bbSpbrook value = 0; 13926b9680bbSPeter Maydell } else if (irq < GIC_INTERNAL) { 13939ee6e8bbSpbrook value = ALL_CPU_MASK; 13946b9680bbSPeter Maydell } 13959ee6e8bbSpbrook s->irq_target[irq] = value & ALL_CPU_MASK; 13966b9680bbSPeter Maydell } 1397e69954b9Spbrook } else if (offset < 0xf00) { 1398e69954b9Spbrook /* Interrupt Configuration. */ 1399b6e6c651SPeter Maydell irq = (offset - 0xc00) * 4; 1400a32134aaSMark Langsdorf if (irq >= s->num_irq) 1401e69954b9Spbrook goto bad_reg; 1402de7a900fSAdam Lackorzynski if (irq < GIC_NR_SGIS) 14039ee6e8bbSpbrook value |= 0xaa; 1404e69954b9Spbrook for (i = 0; i < 4; i++) { 1405fea8a08eSJens Wiklander if (s->security_extn && !attrs.secure && 140667ce697aSLuc Michel !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1407fea8a08eSJens Wiklander continue; /* Ignore Non-secure access of Group0 IRQ */ 1408fea8a08eSJens Wiklander } 1409fea8a08eSJens Wiklander 14107c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 1411e69954b9Spbrook if (value & (1 << (i * 2))) { 141267ce697aSLuc Michel GIC_DIST_SET_MODEL(irq + i); 1413e69954b9Spbrook } else { 141467ce697aSLuc Michel GIC_DIST_CLEAR_MODEL(irq + i); 1415e69954b9Spbrook } 141624b790dfSAdam Lackorzynski } 1417e69954b9Spbrook if (value & (2 << (i * 2))) { 141867ce697aSLuc Michel GIC_DIST_SET_EDGE_TRIGGER(irq + i); 1419e69954b9Spbrook } else { 142067ce697aSLuc Michel GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i); 1421e69954b9Spbrook } 1422e69954b9Spbrook } 142340d22500SChristoffer Dall } else if (offset < 0xf10) { 14249ee6e8bbSpbrook /* 0xf00 is only handled for 32-bit writes. */ 1425e69954b9Spbrook goto bad_reg; 142640d22500SChristoffer Dall } else if (offset < 0xf20) { 142740d22500SChristoffer Dall /* GICD_CPENDSGIRn */ 14287c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 142940d22500SChristoffer Dall goto bad_reg; 143040d22500SChristoffer Dall } 143140d22500SChristoffer Dall irq = (offset - 0xf10); 143240d22500SChristoffer Dall 1433fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 143467ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 143540d22500SChristoffer Dall s->sgi_pending[irq][cpu] &= ~value; 143640d22500SChristoffer Dall if (s->sgi_pending[irq][cpu] == 0) { 143767ce697aSLuc Michel GIC_DIST_CLEAR_PENDING(irq, 1 << cpu); 143840d22500SChristoffer Dall } 1439fea8a08eSJens Wiklander } 144040d22500SChristoffer Dall } else if (offset < 0xf30) { 144140d22500SChristoffer Dall /* GICD_SPENDSGIRn */ 14427c14b3acSMichael Davidsaver if (s->revision == REV_11MPCORE) { 144340d22500SChristoffer Dall goto bad_reg; 144440d22500SChristoffer Dall } 144540d22500SChristoffer Dall irq = (offset - 0xf20); 144640d22500SChristoffer Dall 1447fea8a08eSJens Wiklander if (!s->security_extn || attrs.secure || 144867ce697aSLuc Michel GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 144967ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, 1 << cpu); 145040d22500SChristoffer Dall s->sgi_pending[irq][cpu] |= value; 1451fea8a08eSJens Wiklander } 145240d22500SChristoffer Dall } else { 145340d22500SChristoffer Dall goto bad_reg; 1454e69954b9Spbrook } 1455e69954b9Spbrook gic_update(s); 1456e69954b9Spbrook return; 1457e69954b9Spbrook bad_reg: 14588c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 14598c8dc39fSPeter Maydell "gic_dist_writeb: Bad offset %x\n", (int)offset); 1460e69954b9Spbrook } 1461e69954b9Spbrook 1462a8170e5eSAvi Kivity static void gic_dist_writew(void *opaque, hwaddr offset, 1463a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1464e69954b9Spbrook { 1465a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, value & 0xff, attrs); 1466a9d85353SPeter Maydell gic_dist_writeb(opaque, offset + 1, value >> 8, attrs); 1467e69954b9Spbrook } 1468e69954b9Spbrook 1469a8170e5eSAvi Kivity static void gic_dist_writel(void *opaque, hwaddr offset, 1470a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1471e69954b9Spbrook { 1472fae15286SPeter Maydell GICState *s = (GICState *)opaque; 14738da3ff18Spbrook if (offset == 0xf00) { 14749ee6e8bbSpbrook int cpu; 14759ee6e8bbSpbrook int irq; 14769ee6e8bbSpbrook int mask; 147740d22500SChristoffer Dall int target_cpu; 14789ee6e8bbSpbrook 1479926c4affSPeter Maydell cpu = gic_get_current_cpu(s); 14809ee6e8bbSpbrook irq = value & 0x3ff; 14819ee6e8bbSpbrook switch ((value >> 24) & 3) { 14829ee6e8bbSpbrook case 0: 14839ee6e8bbSpbrook mask = (value >> 16) & ALL_CPU_MASK; 14849ee6e8bbSpbrook break; 14859ee6e8bbSpbrook case 1: 1486fa250144SAdam Lackorzynski mask = ALL_CPU_MASK ^ (1 << cpu); 14879ee6e8bbSpbrook break; 14889ee6e8bbSpbrook case 2: 1489fa250144SAdam Lackorzynski mask = 1 << cpu; 14909ee6e8bbSpbrook break; 14919ee6e8bbSpbrook default: 14929ee6e8bbSpbrook DPRINTF("Bad Soft Int target filter\n"); 14939ee6e8bbSpbrook mask = ALL_CPU_MASK; 14949ee6e8bbSpbrook break; 14959ee6e8bbSpbrook } 149667ce697aSLuc Michel GIC_DIST_SET_PENDING(irq, mask); 149740d22500SChristoffer Dall target_cpu = ctz32(mask); 149840d22500SChristoffer Dall while (target_cpu < GIC_NCPU) { 149940d22500SChristoffer Dall s->sgi_pending[irq][target_cpu] |= (1 << cpu); 150040d22500SChristoffer Dall mask &= ~(1 << target_cpu); 150140d22500SChristoffer Dall target_cpu = ctz32(mask); 150240d22500SChristoffer Dall } 15039ee6e8bbSpbrook gic_update(s); 15049ee6e8bbSpbrook return; 15059ee6e8bbSpbrook } 1506a9d85353SPeter Maydell gic_dist_writew(opaque, offset, value & 0xffff, attrs); 1507a9d85353SPeter Maydell gic_dist_writew(opaque, offset + 2, value >> 16, attrs); 1508a9d85353SPeter Maydell } 1509a9d85353SPeter Maydell 1510a9d85353SPeter Maydell static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data, 1511a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1512a9d85353SPeter Maydell { 1513067a2b9cSLuc Michel trace_gic_dist_write(offset, size, data); 1514067a2b9cSLuc Michel 1515a9d85353SPeter Maydell switch (size) { 1516a9d85353SPeter Maydell case 1: 1517a9d85353SPeter Maydell gic_dist_writeb(opaque, offset, data, attrs); 1518a9d85353SPeter Maydell return MEMTX_OK; 1519a9d85353SPeter Maydell case 2: 1520a9d85353SPeter Maydell gic_dist_writew(opaque, offset, data, attrs); 1521a9d85353SPeter Maydell return MEMTX_OK; 1522a9d85353SPeter Maydell case 4: 1523a9d85353SPeter Maydell gic_dist_writel(opaque, offset, data, attrs); 1524a9d85353SPeter Maydell return MEMTX_OK; 1525a9d85353SPeter Maydell default: 1526a9d85353SPeter Maydell return MEMTX_ERROR; 1527a9d85353SPeter Maydell } 1528e69954b9Spbrook } 1529e69954b9Spbrook 153051fd06e0SPeter Maydell static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno) 153151fd06e0SPeter Maydell { 153251fd06e0SPeter Maydell /* Return the Nonsecure view of GICC_APR<regno>. This is the 153351fd06e0SPeter Maydell * second half of GICC_NSAPR. 153451fd06e0SPeter Maydell */ 153551fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 153651fd06e0SPeter Maydell case 0: 153751fd06e0SPeter Maydell if (regno < 2) { 153851fd06e0SPeter Maydell return s->nsapr[regno + 2][cpu]; 153951fd06e0SPeter Maydell } 154051fd06e0SPeter Maydell break; 154151fd06e0SPeter Maydell case 1: 154251fd06e0SPeter Maydell if (regno == 0) { 154351fd06e0SPeter Maydell return s->nsapr[regno + 1][cpu]; 154451fd06e0SPeter Maydell } 154551fd06e0SPeter Maydell break; 154651fd06e0SPeter Maydell case 2: 154751fd06e0SPeter Maydell if (regno == 0) { 154851fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 16, 16); 154951fd06e0SPeter Maydell } 155051fd06e0SPeter Maydell break; 155151fd06e0SPeter Maydell case 3: 155251fd06e0SPeter Maydell if (regno == 0) { 155351fd06e0SPeter Maydell return extract32(s->nsapr[0][cpu], 8, 8); 155451fd06e0SPeter Maydell } 155551fd06e0SPeter Maydell break; 155651fd06e0SPeter Maydell default: 155751fd06e0SPeter Maydell g_assert_not_reached(); 155851fd06e0SPeter Maydell } 155951fd06e0SPeter Maydell return 0; 156051fd06e0SPeter Maydell } 156151fd06e0SPeter Maydell 156251fd06e0SPeter Maydell static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno, 156351fd06e0SPeter Maydell uint32_t value) 156451fd06e0SPeter Maydell { 156551fd06e0SPeter Maydell /* Write the Nonsecure view of GICC_APR<regno>. */ 156651fd06e0SPeter Maydell switch (GIC_MIN_BPR) { 156751fd06e0SPeter Maydell case 0: 156851fd06e0SPeter Maydell if (regno < 2) { 156951fd06e0SPeter Maydell s->nsapr[regno + 2][cpu] = value; 157051fd06e0SPeter Maydell } 157151fd06e0SPeter Maydell break; 157251fd06e0SPeter Maydell case 1: 157351fd06e0SPeter Maydell if (regno == 0) { 157451fd06e0SPeter Maydell s->nsapr[regno + 1][cpu] = value; 157551fd06e0SPeter Maydell } 157651fd06e0SPeter Maydell break; 157751fd06e0SPeter Maydell case 2: 157851fd06e0SPeter Maydell if (regno == 0) { 157951fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value); 158051fd06e0SPeter Maydell } 158151fd06e0SPeter Maydell break; 158251fd06e0SPeter Maydell case 3: 158351fd06e0SPeter Maydell if (regno == 0) { 158451fd06e0SPeter Maydell s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value); 158551fd06e0SPeter Maydell } 158651fd06e0SPeter Maydell break; 158751fd06e0SPeter Maydell default: 158851fd06e0SPeter Maydell g_assert_not_reached(); 158951fd06e0SPeter Maydell } 159051fd06e0SPeter Maydell } 159151fd06e0SPeter Maydell 1592a9d85353SPeter Maydell static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, 1593a9d85353SPeter Maydell uint64_t *data, MemTxAttrs attrs) 1594e69954b9Spbrook { 1595e69954b9Spbrook switch (offset) { 1596e69954b9Spbrook case 0x00: /* Control */ 159732951860SFabian Aggeler *data = gic_get_cpu_control(s, cpu, attrs); 1598a9d85353SPeter Maydell break; 1599e69954b9Spbrook case 0x04: /* Priority mask */ 160081508470SFabian Aggeler *data = gic_get_priority_mask(s, cpu, attrs); 1601a9d85353SPeter Maydell break; 1602e69954b9Spbrook case 0x08: /* Binary Point */ 16033dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1604421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1605421a3c22SLuc MICHEL /* NS view of BPR when CBPR is 1 */ 1606421a3c22SLuc MICHEL *data = MIN(s->bpr[cpu] + 1, 7); 1607421a3c22SLuc MICHEL } else { 1608822e9cc3SFabian Aggeler /* BPR is banked. Non-secure copy stored in ABPR. */ 1609822e9cc3SFabian Aggeler *data = s->abpr[cpu]; 1610421a3c22SLuc MICHEL } 1611822e9cc3SFabian Aggeler } else { 1612a9d85353SPeter Maydell *data = s->bpr[cpu]; 1613822e9cc3SFabian Aggeler } 1614a9d85353SPeter Maydell break; 1615e69954b9Spbrook case 0x0c: /* Acknowledge */ 1616c5619bf9SFabian Aggeler *data = gic_acknowledge_irq(s, cpu, attrs); 1617a9d85353SPeter Maydell break; 161866a0a2cbSDong Xu Wang case 0x14: /* Running Priority */ 161908efa9f2SFabian Aggeler *data = gic_get_running_priority(s, cpu, attrs); 1620a9d85353SPeter Maydell break; 1621e69954b9Spbrook case 0x18: /* Highest Pending Interrupt */ 16227c0fa108SFabian Aggeler *data = gic_get_current_pending_irq(s, cpu, attrs); 1623a9d85353SPeter Maydell break; 1624aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 1625822e9cc3SFabian Aggeler /* GIC v2, no security: ABPR 1626822e9cc3SFabian Aggeler * GIC v1, no security: not implemented (RAZ/WI) 1627822e9cc3SFabian Aggeler * With security extensions, secure access: ABPR (alias of NS BPR) 1628822e9cc3SFabian Aggeler * With security extensions, nonsecure access: RAZ/WI 1629822e9cc3SFabian Aggeler */ 16303dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1631822e9cc3SFabian Aggeler *data = 0; 1632822e9cc3SFabian Aggeler } else { 1633a9d85353SPeter Maydell *data = s->abpr[cpu]; 1634822e9cc3SFabian Aggeler } 1635a9d85353SPeter Maydell break; 1636a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 163751fd06e0SPeter Maydell { 163851fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 16397eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 164051fd06e0SPeter Maydell 16417eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 164251fd06e0SPeter Maydell *data = 0; 16437eb079ecSLuc Michel } else if (gic_is_vcpu(cpu)) { 16447eb079ecSLuc Michel *data = s->h_apr[gic_get_vcpu_real_id(cpu)]; 16453dd0471bSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 164651fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 164751fd06e0SPeter Maydell *data = gic_apr_ns_view(s, regno, cpu); 164851fd06e0SPeter Maydell } else { 164951fd06e0SPeter Maydell *data = s->apr[regno][cpu]; 165051fd06e0SPeter Maydell } 1651a9d85353SPeter Maydell break; 165251fd06e0SPeter Maydell } 165351fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 165451fd06e0SPeter Maydell { 165551fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 165651fd06e0SPeter Maydell 165751fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) || 16587eb079ecSLuc Michel gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) { 165951fd06e0SPeter Maydell *data = 0; 166051fd06e0SPeter Maydell } else { 166151fd06e0SPeter Maydell *data = s->nsapr[regno][cpu]; 166251fd06e0SPeter Maydell } 166351fd06e0SPeter Maydell break; 166451fd06e0SPeter Maydell } 1665e69954b9Spbrook default: 16668c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 16678c8dc39fSPeter Maydell "gic_cpu_read: Bad offset %x\n", (int)offset); 16680cf09852SPeter Maydell *data = 0; 16690cf09852SPeter Maydell break; 1670e69954b9Spbrook } 1671067a2b9cSLuc Michel 1672067a2b9cSLuc Michel trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 1673067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, *data); 1674a9d85353SPeter Maydell return MEMTX_OK; 1675e69954b9Spbrook } 1676e69954b9Spbrook 1677a9d85353SPeter Maydell static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset, 1678a9d85353SPeter Maydell uint32_t value, MemTxAttrs attrs) 1679e69954b9Spbrook { 1680067a2b9cSLuc Michel trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu", 1681067a2b9cSLuc Michel gic_get_vcpu_real_id(cpu), offset, value); 1682067a2b9cSLuc Michel 1683e69954b9Spbrook switch (offset) { 1684e69954b9Spbrook case 0x00: /* Control */ 168532951860SFabian Aggeler gic_set_cpu_control(s, cpu, value, attrs); 1686e69954b9Spbrook break; 1687e69954b9Spbrook case 0x04: /* Priority mask */ 168881508470SFabian Aggeler gic_set_priority_mask(s, cpu, value, attrs); 1689e69954b9Spbrook break; 1690e69954b9Spbrook case 0x08: /* Binary Point */ 16913dd0471bSLuc Michel if (gic_cpu_ns_access(s, cpu, attrs)) { 1692421a3c22SLuc MICHEL if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1693421a3c22SLuc MICHEL /* WI when CBPR is 1 */ 1694421a3c22SLuc MICHEL return MEMTX_OK; 1695421a3c22SLuc MICHEL } else { 1696822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1697421a3c22SLuc MICHEL } 1698822e9cc3SFabian Aggeler } else { 16997eb079ecSLuc Michel int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR; 17007eb079ecSLuc Michel s->bpr[cpu] = MAX(value & 0x7, min_bpr); 1701822e9cc3SFabian Aggeler } 1702e69954b9Spbrook break; 1703e69954b9Spbrook case 0x10: /* End Of Interrupt */ 1704f9c6a7f1SFabian Aggeler gic_complete_irq(s, cpu, value & 0x3ff, attrs); 1705a9d85353SPeter Maydell return MEMTX_OK; 1706aa7d461aSChristoffer Dall case 0x1c: /* Aliased Binary Point */ 17073dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 1708822e9cc3SFabian Aggeler /* unimplemented, or NS access: RAZ/WI */ 1709822e9cc3SFabian Aggeler return MEMTX_OK; 1710822e9cc3SFabian Aggeler } else { 1711822e9cc3SFabian Aggeler s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1712aa7d461aSChristoffer Dall } 1713aa7d461aSChristoffer Dall break; 1714a9d477c4SChristoffer Dall case 0xd0: case 0xd4: case 0xd8: case 0xdc: 171551fd06e0SPeter Maydell { 171651fd06e0SPeter Maydell int regno = (offset - 0xd0) / 4; 17177eb079ecSLuc Michel int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS; 171851fd06e0SPeter Maydell 17197eb079ecSLuc Michel if (regno >= nr_aprs || s->revision != 2) { 172051fd06e0SPeter Maydell return MEMTX_OK; 172151fd06e0SPeter Maydell } 17227eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 17237eb079ecSLuc Michel s->h_apr[gic_get_vcpu_real_id(cpu)] = value; 17247eb079ecSLuc Michel } else if (gic_cpu_ns_access(s, cpu, attrs)) { 172551fd06e0SPeter Maydell /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 172651fd06e0SPeter Maydell gic_apr_write_ns_view(s, regno, cpu, value); 172751fd06e0SPeter Maydell } else { 172851fd06e0SPeter Maydell s->apr[regno][cpu] = value; 172951fd06e0SPeter Maydell } 1730a9d477c4SChristoffer Dall break; 173151fd06e0SPeter Maydell } 173251fd06e0SPeter Maydell case 0xe0: case 0xe4: case 0xe8: case 0xec: 173351fd06e0SPeter Maydell { 173451fd06e0SPeter Maydell int regno = (offset - 0xe0) / 4; 173551fd06e0SPeter Maydell 173651fd06e0SPeter Maydell if (regno >= GIC_NR_APRS || s->revision != 2) { 173751fd06e0SPeter Maydell return MEMTX_OK; 173851fd06e0SPeter Maydell } 17397eb079ecSLuc Michel if (gic_is_vcpu(cpu)) { 17407eb079ecSLuc Michel return MEMTX_OK; 17417eb079ecSLuc Michel } 17423dd0471bSLuc Michel if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) { 174351fd06e0SPeter Maydell return MEMTX_OK; 174451fd06e0SPeter Maydell } 174551fd06e0SPeter Maydell s->nsapr[regno][cpu] = value; 174651fd06e0SPeter Maydell break; 174751fd06e0SPeter Maydell } 1748a55c910eSPeter Maydell case 0x1000: 1749a55c910eSPeter Maydell /* GICC_DIR */ 1750a55c910eSPeter Maydell gic_deactivate_irq(s, cpu, value & 0x3ff, attrs); 1751a55c910eSPeter Maydell break; 1752e69954b9Spbrook default: 17538c8dc39fSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 17548c8dc39fSPeter Maydell "gic_cpu_write: Bad offset %x\n", (int)offset); 17550cf09852SPeter Maydell return MEMTX_OK; 1756e69954b9Spbrook } 1757cbe1282bSLuc Michel 1758cbe1282bSLuc Michel if (gic_is_vcpu(cpu)) { 1759cbe1282bSLuc Michel gic_update_virt(s); 1760cbe1282bSLuc Michel } else { 1761e69954b9Spbrook gic_update(s); 1762cbe1282bSLuc Michel } 1763cbe1282bSLuc Michel 1764a9d85353SPeter Maydell return MEMTX_OK; 1765e69954b9Spbrook } 1766e2c56465SPeter Maydell 1767e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for the current CPU */ 1768a9d85353SPeter Maydell static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data, 1769a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1770e2c56465SPeter Maydell { 1771fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1772a9d85353SPeter Maydell return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs); 1773e2c56465SPeter Maydell } 1774e2c56465SPeter Maydell 1775a9d85353SPeter Maydell static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr, 1776a9d85353SPeter Maydell uint64_t value, unsigned size, 1777a9d85353SPeter Maydell MemTxAttrs attrs) 1778e2c56465SPeter Maydell { 1779fae15286SPeter Maydell GICState *s = (GICState *)opaque; 1780a9d85353SPeter Maydell return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs); 1781e2c56465SPeter Maydell } 1782e2c56465SPeter Maydell 1783e2c56465SPeter Maydell /* Wrappers to read/write the GIC CPU interface for a specific CPU. 1784fae15286SPeter Maydell * These just decode the opaque pointer into GICState* + cpu id. 1785e2c56465SPeter Maydell */ 1786a9d85353SPeter Maydell static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data, 1787a9d85353SPeter Maydell unsigned size, MemTxAttrs attrs) 1788e2c56465SPeter Maydell { 1789fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1790fae15286SPeter Maydell GICState *s = *backref; 1791e2c56465SPeter Maydell int id = (backref - s->backref); 1792a9d85353SPeter Maydell return gic_cpu_read(s, id, addr, data, attrs); 1793e2c56465SPeter Maydell } 1794e2c56465SPeter Maydell 1795a9d85353SPeter Maydell static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr, 1796a9d85353SPeter Maydell uint64_t value, unsigned size, 1797a9d85353SPeter Maydell MemTxAttrs attrs) 1798e2c56465SPeter Maydell { 1799fae15286SPeter Maydell GICState **backref = (GICState **)opaque; 1800fae15286SPeter Maydell GICState *s = *backref; 1801e2c56465SPeter Maydell int id = (backref - s->backref); 1802a9d85353SPeter Maydell return gic_cpu_write(s, id, addr, value, attrs); 1803e2c56465SPeter Maydell } 1804e2c56465SPeter Maydell 18052c679ac7SLuc Michel static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data, 18062c679ac7SLuc Michel unsigned size, MemTxAttrs attrs) 18072c679ac7SLuc Michel { 18082c679ac7SLuc Michel GICState *s = (GICState *)opaque; 18092c679ac7SLuc Michel 18102c679ac7SLuc Michel return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs); 18112c679ac7SLuc Michel } 18122c679ac7SLuc Michel 18132c679ac7SLuc Michel static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr, 18142c679ac7SLuc Michel uint64_t value, unsigned size, 18152c679ac7SLuc Michel MemTxAttrs attrs) 18162c679ac7SLuc Michel { 18172c679ac7SLuc Michel GICState *s = (GICState *)opaque; 18182c679ac7SLuc Michel 18192c679ac7SLuc Michel return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs); 18202c679ac7SLuc Michel } 18212c679ac7SLuc Michel 1822527d296fSLuc Michel static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start) 1823527d296fSLuc Michel { 1824527d296fSLuc Michel int lr_idx; 1825527d296fSLuc Michel uint32_t ret = 0; 1826527d296fSLuc Michel 1827527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1828527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1829527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1830527d296fSLuc Michel gic_lr_entry_is_eoi(*entry)); 1831527d296fSLuc Michel } 1832527d296fSLuc Michel 1833527d296fSLuc Michel return ret; 1834527d296fSLuc Michel } 1835527d296fSLuc Michel 1836527d296fSLuc Michel static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start) 1837527d296fSLuc Michel { 1838527d296fSLuc Michel int lr_idx; 1839527d296fSLuc Michel uint32_t ret = 0; 1840527d296fSLuc Michel 1841527d296fSLuc Michel for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) { 1842527d296fSLuc Michel uint32_t *entry = &s->h_lr[lr_idx][cpu]; 1843527d296fSLuc Michel ret = deposit32(ret, lr_idx - lr_start, 1, 1844527d296fSLuc Michel gic_lr_entry_is_free(*entry)); 1845527d296fSLuc Michel } 1846527d296fSLuc Michel 1847527d296fSLuc Michel return ret; 1848527d296fSLuc Michel } 1849527d296fSLuc Michel 1850527d296fSLuc Michel static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs) 1851527d296fSLuc Michel { 1852527d296fSLuc Michel int vcpu = gic_get_current_vcpu(s); 1853527d296fSLuc Michel uint32_t ctlr; 1854527d296fSLuc Michel uint32_t abpr; 1855527d296fSLuc Michel uint32_t bpr; 1856527d296fSLuc Michel uint32_t prio_mask; 1857527d296fSLuc Michel 1858527d296fSLuc Michel ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr); 1859527d296fSLuc Michel abpr = FIELD_EX32(value, GICH_VMCR, VMABP); 1860527d296fSLuc Michel bpr = FIELD_EX32(value, GICH_VMCR, VMBP); 1861527d296fSLuc Michel prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3; 1862527d296fSLuc Michel 1863527d296fSLuc Michel gic_set_cpu_control(s, vcpu, ctlr, attrs); 1864527d296fSLuc Michel s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR); 1865527d296fSLuc Michel s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR); 1866527d296fSLuc Michel gic_set_priority_mask(s, vcpu, prio_mask, attrs); 1867527d296fSLuc Michel } 1868527d296fSLuc Michel 1869527d296fSLuc Michel static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr, 1870527d296fSLuc Michel uint64_t *data, MemTxAttrs attrs) 1871527d296fSLuc Michel { 1872527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1873527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1874527d296fSLuc Michel 1875527d296fSLuc Michel switch (addr) { 1876527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1877527d296fSLuc Michel *data = s->h_hcr[cpu]; 1878527d296fSLuc Michel break; 1879527d296fSLuc Michel 1880527d296fSLuc Michel case A_GICH_VTR: /* VGIC Type */ 1881527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1); 1882527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PREbits, 1883527d296fSLuc Michel GIC_VIRT_MAX_GROUP_PRIO_BITS - 1); 1884527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VTR, PRIbits, 1885527d296fSLuc Michel (7 - GIC_VIRT_MIN_BPR) - 1); 1886527d296fSLuc Michel break; 1887527d296fSLuc Michel 1888527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1889527d296fSLuc Michel *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr, 1890527d296fSLuc Michel extract32(s->cpu_ctlr[vcpu], 0, 10)); 1891527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]); 1892527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]); 1893527d296fSLuc Michel *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask, 1894527d296fSLuc Michel extract32(s->priority_mask[vcpu], 3, 5)); 1895527d296fSLuc Michel break; 1896527d296fSLuc Michel 1897527d296fSLuc Michel case A_GICH_MISR: /* Maintenance Interrupt Status */ 1898527d296fSLuc Michel *data = s->h_misr[cpu]; 1899527d296fSLuc Michel break; 1900527d296fSLuc Michel 1901527d296fSLuc Michel case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */ 1902527d296fSLuc Michel case A_GICH_EISR1: 1903527d296fSLuc Michel *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8); 1904527d296fSLuc Michel break; 1905527d296fSLuc Michel 1906527d296fSLuc Michel case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */ 1907527d296fSLuc Michel case A_GICH_ELRSR1: 1908527d296fSLuc Michel *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8); 1909527d296fSLuc Michel break; 1910527d296fSLuc Michel 1911527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1912527d296fSLuc Michel *data = s->h_apr[cpu]; 1913527d296fSLuc Michel break; 1914527d296fSLuc Michel 1915527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1916527d296fSLuc Michel { 1917527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1918527d296fSLuc Michel 1919527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1920527d296fSLuc Michel *data = 0; 1921527d296fSLuc Michel } else { 1922527d296fSLuc Michel *data = s->h_lr[lr_idx][cpu]; 1923527d296fSLuc Michel } 1924527d296fSLuc Michel break; 1925527d296fSLuc Michel } 1926527d296fSLuc Michel 1927527d296fSLuc Michel default: 1928527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1929527d296fSLuc Michel "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr); 1930527d296fSLuc Michel return MEMTX_OK; 1931527d296fSLuc Michel } 1932527d296fSLuc Michel 1933067a2b9cSLuc Michel trace_gic_hyp_read(addr, *data); 1934527d296fSLuc Michel return MEMTX_OK; 1935527d296fSLuc Michel } 1936527d296fSLuc Michel 1937527d296fSLuc Michel static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr, 1938527d296fSLuc Michel uint64_t value, MemTxAttrs attrs) 1939527d296fSLuc Michel { 1940527d296fSLuc Michel GICState *s = ARM_GIC(opaque); 1941527d296fSLuc Michel int vcpu = cpu + GIC_NCPU; 1942527d296fSLuc Michel 1943067a2b9cSLuc Michel trace_gic_hyp_write(addr, value); 1944067a2b9cSLuc Michel 1945527d296fSLuc Michel switch (addr) { 1946527d296fSLuc Michel case A_GICH_HCR: /* Hypervisor Control */ 1947527d296fSLuc Michel s->h_hcr[cpu] = value & GICH_HCR_MASK; 1948527d296fSLuc Michel break; 1949527d296fSLuc Michel 1950527d296fSLuc Michel case A_GICH_VMCR: /* Virtual Machine Control */ 1951527d296fSLuc Michel gic_vmcr_write(s, value, attrs); 1952527d296fSLuc Michel break; 1953527d296fSLuc Michel 1954527d296fSLuc Michel case A_GICH_APR: /* Active Priorities */ 1955527d296fSLuc Michel s->h_apr[cpu] = value; 1956527d296fSLuc Michel s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu); 1957527d296fSLuc Michel break; 1958527d296fSLuc Michel 1959527d296fSLuc Michel case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */ 1960527d296fSLuc Michel { 1961527d296fSLuc Michel int lr_idx = (addr - A_GICH_LR0) / 4; 1962527d296fSLuc Michel 1963527d296fSLuc Michel if (lr_idx > s->num_lrs) { 1964527d296fSLuc Michel return MEMTX_OK; 1965527d296fSLuc Michel } 1966527d296fSLuc Michel 1967527d296fSLuc Michel s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK; 1968067a2b9cSLuc Michel trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]); 1969527d296fSLuc Michel break; 1970527d296fSLuc Michel } 1971527d296fSLuc Michel 1972527d296fSLuc Michel default: 1973527d296fSLuc Michel qemu_log_mask(LOG_GUEST_ERROR, 1974527d296fSLuc Michel "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr); 1975527d296fSLuc Michel return MEMTX_OK; 1976527d296fSLuc Michel } 1977527d296fSLuc Michel 1978cbe1282bSLuc Michel gic_update_virt(s); 1979527d296fSLuc Michel return MEMTX_OK; 1980527d296fSLuc Michel } 1981527d296fSLuc Michel 1982527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 1983527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 1984527d296fSLuc Michel { 1985527d296fSLuc Michel GICState *s = (GICState *)opaque; 1986527d296fSLuc Michel 1987527d296fSLuc Michel return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs); 1988527d296fSLuc Michel } 1989527d296fSLuc Michel 1990527d296fSLuc Michel static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr, 1991527d296fSLuc Michel uint64_t value, unsigned size, 1992527d296fSLuc Michel MemTxAttrs attrs) 1993527d296fSLuc Michel { 1994527d296fSLuc Michel GICState *s = (GICState *)opaque; 1995527d296fSLuc Michel 1996527d296fSLuc Michel return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs); 1997527d296fSLuc Michel } 1998527d296fSLuc Michel 1999527d296fSLuc Michel static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data, 2000527d296fSLuc Michel unsigned size, MemTxAttrs attrs) 2001527d296fSLuc Michel { 2002527d296fSLuc Michel GICState **backref = (GICState **)opaque; 2003527d296fSLuc Michel GICState *s = *backref; 2004527d296fSLuc Michel int id = (backref - s->backref); 2005527d296fSLuc Michel 2006527d296fSLuc Michel return gic_hyp_read(s, id, addr, data, attrs); 2007527d296fSLuc Michel } 2008527d296fSLuc Michel 2009527d296fSLuc Michel static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr, 2010527d296fSLuc Michel uint64_t value, unsigned size, 2011527d296fSLuc Michel MemTxAttrs attrs) 2012527d296fSLuc Michel { 2013527d296fSLuc Michel GICState **backref = (GICState **)opaque; 2014527d296fSLuc Michel GICState *s = *backref; 2015527d296fSLuc Michel int id = (backref - s->backref); 2016527d296fSLuc Michel 2017527d296fSLuc Michel return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs); 2018527d296fSLuc Michel 2019527d296fSLuc Michel } 2020527d296fSLuc Michel 20217926c210SPavel Fedin static const MemoryRegionOps gic_ops[2] = { 20227926c210SPavel Fedin { 20237926c210SPavel Fedin .read_with_attrs = gic_dist_read, 20247926c210SPavel Fedin .write_with_attrs = gic_dist_write, 20257926c210SPavel Fedin .endianness = DEVICE_NATIVE_ENDIAN, 20267926c210SPavel Fedin }, 20277926c210SPavel Fedin { 2028a9d85353SPeter Maydell .read_with_attrs = gic_thiscpu_read, 2029a9d85353SPeter Maydell .write_with_attrs = gic_thiscpu_write, 2030e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 20317926c210SPavel Fedin } 2032e2c56465SPeter Maydell }; 2033e2c56465SPeter Maydell 2034e2c56465SPeter Maydell static const MemoryRegionOps gic_cpu_ops = { 2035a9d85353SPeter Maydell .read_with_attrs = gic_do_cpu_read, 2036a9d85353SPeter Maydell .write_with_attrs = gic_do_cpu_write, 2037e2c56465SPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN, 2038e2c56465SPeter Maydell }; 2039e69954b9Spbrook 20402c679ac7SLuc Michel static const MemoryRegionOps gic_virt_ops[2] = { 20412c679ac7SLuc Michel { 2042527d296fSLuc Michel .read_with_attrs = gic_thiscpu_hyp_read, 2043527d296fSLuc Michel .write_with_attrs = gic_thiscpu_hyp_write, 20442c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 20452c679ac7SLuc Michel }, 20462c679ac7SLuc Michel { 20472c679ac7SLuc Michel .read_with_attrs = gic_thisvcpu_read, 20482c679ac7SLuc Michel .write_with_attrs = gic_thisvcpu_write, 20492c679ac7SLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 20502c679ac7SLuc Michel } 20512c679ac7SLuc Michel }; 20522c679ac7SLuc Michel 2053527d296fSLuc Michel static const MemoryRegionOps gic_viface_ops = { 2054527d296fSLuc Michel .read_with_attrs = gic_do_hyp_read, 2055527d296fSLuc Michel .write_with_attrs = gic_do_hyp_write, 2056527d296fSLuc Michel .endianness = DEVICE_NATIVE_ENDIAN, 2057527d296fSLuc Michel }; 2058527d296fSLuc Michel 205953111180SPeter Maydell static void arm_gic_realize(DeviceState *dev, Error **errp) 20602b518c56SPeter Maydell { 206153111180SPeter Maydell /* Device instance realize function for the GIC sysbus device */ 20622b518c56SPeter Maydell int i; 206353111180SPeter Maydell GICState *s = ARM_GIC(dev); 206453111180SPeter Maydell SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 20651e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_GET_CLASS(s); 20660175ba10SMarkus Armbruster Error *local_err = NULL; 20671e8cae4dSPeter Maydell 20680175ba10SMarkus Armbruster agc->parent_realize(dev, &local_err); 20690175ba10SMarkus Armbruster if (local_err) { 20700175ba10SMarkus Armbruster error_propagate(errp, local_err); 207153111180SPeter Maydell return; 207253111180SPeter Maydell } 20731e8cae4dSPeter Maydell 20745d721b78SAlexander Graf if (kvm_enabled() && !kvm_arm_supports_user_irq()) { 20755d721b78SAlexander Graf error_setg(errp, "KVM with user space irqchip only works when the " 20765d721b78SAlexander Graf "host kernel supports KVM_CAP_ARM_USER_IRQ"); 20775d721b78SAlexander Graf return; 20785d721b78SAlexander Graf } 20795d721b78SAlexander Graf 208011411489SSai Pavan Boddu if (s->n_prio_bits > GIC_MAX_PRIORITY_BITS || 208111411489SSai Pavan Boddu (s->virt_extn ? s->n_prio_bits < GIC_VIRT_MAX_GROUP_PRIO_BITS : 208211411489SSai Pavan Boddu s->n_prio_bits < GIC_MIN_PRIORITY_BITS)) { 208311411489SSai Pavan Boddu error_setg(errp, "num-priority-bits cannot be greater than %d" 208411411489SSai Pavan Boddu " or less than %d", GIC_MAX_PRIORITY_BITS, 208511411489SSai Pavan Boddu s->virt_extn ? GIC_VIRT_MAX_GROUP_PRIO_BITS : 208611411489SSai Pavan Boddu GIC_MIN_PRIORITY_BITS); 208711411489SSai Pavan Boddu return; 208811411489SSai Pavan Boddu } 208911411489SSai Pavan Boddu 20902c679ac7SLuc Michel /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if 20912c679ac7SLuc Michel * enabled, virtualization extensions related interfaces (main virtual 20922c679ac7SLuc Michel * interface (s->vifaceiomem[0]) and virtual CPU interface). 20932c679ac7SLuc Michel */ 20942c679ac7SLuc Michel gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops); 20952b518c56SPeter Maydell 20967926c210SPavel Fedin /* Extra core-specific regions for the CPU interfaces. This is 20977926c210SPavel Fedin * necessary for "franken-GIC" implementations, for example on 20987926c210SPavel Fedin * Exynos 4. 2099e2c56465SPeter Maydell * NB that the memory region size of 0x100 applies for the 11MPCore 2100e2c56465SPeter Maydell * and also cores following the GIC v1 spec (ie A9). 2101e2c56465SPeter Maydell * GIC v2 defines a larger memory region (0x1000) so this will need 2102e2c56465SPeter Maydell * to be extended when we implement A15. 2103e2c56465SPeter Maydell */ 2104b95690c9SWei Huang for (i = 0; i < s->num_cpu; i++) { 2105e2c56465SPeter Maydell s->backref[i] = s; 21061437c94bSPaolo Bonzini memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops, 21071437c94bSPaolo Bonzini &s->backref[i], "gic_cpu", 0x100); 21087926c210SPavel Fedin sysbus_init_mmio(sbd, &s->cpuiomem[i+1]); 2109496dbcd1SPeter Maydell } 2110527d296fSLuc Michel 2111527d296fSLuc Michel /* Extra core-specific regions for virtual interfaces. This is required by 2112527d296fSLuc Michel * the GICv2 specification. 2113527d296fSLuc Michel */ 2114527d296fSLuc Michel if (s->virt_extn) { 2115527d296fSLuc Michel for (i = 0; i < s->num_cpu; i++) { 2116527d296fSLuc Michel memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s), 2117527d296fSLuc Michel &gic_viface_ops, &s->backref[i], 21187210918cSPeter Maydell "gic_viface", 0x200); 2119527d296fSLuc Michel sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]); 2120527d296fSLuc Michel } 2121527d296fSLuc Michel } 2122527d296fSLuc Michel 2123496dbcd1SPeter Maydell } 2124496dbcd1SPeter Maydell 2125496dbcd1SPeter Maydell static void arm_gic_class_init(ObjectClass *klass, void *data) 2126496dbcd1SPeter Maydell { 2127496dbcd1SPeter Maydell DeviceClass *dc = DEVICE_CLASS(klass); 21281e8cae4dSPeter Maydell ARMGICClass *agc = ARM_GIC_CLASS(klass); 212953111180SPeter Maydell 2130bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize); 2131496dbcd1SPeter Maydell } 2132496dbcd1SPeter Maydell 21338c43a6f0SAndreas Färber static const TypeInfo arm_gic_info = { 21341e8cae4dSPeter Maydell .name = TYPE_ARM_GIC, 21351e8cae4dSPeter Maydell .parent = TYPE_ARM_GIC_COMMON, 2136fae15286SPeter Maydell .instance_size = sizeof(GICState), 2137496dbcd1SPeter Maydell .class_init = arm_gic_class_init, 2138998a74bcSPeter Maydell .class_size = sizeof(ARMGICClass), 2139496dbcd1SPeter Maydell }; 2140496dbcd1SPeter Maydell 2141496dbcd1SPeter Maydell static void arm_gic_register_types(void) 2142496dbcd1SPeter Maydell { 2143496dbcd1SPeter Maydell type_register_static(&arm_gic_info); 2144496dbcd1SPeter Maydell } 2145496dbcd1SPeter Maydell 2146496dbcd1SPeter Maydell type_init(arm_gic_register_types) 2147