1ff8f06eeSShlomo Pongratz /* 2ff8f06eeSShlomo Pongratz * ARM GICv3 support - common bits of emulated and KVM kernel model 3ff8f06eeSShlomo Pongratz * 4ff8f06eeSShlomo Pongratz * Copyright (c) 2012 Linaro Limited 5ff8f06eeSShlomo Pongratz * Copyright (c) 2015 Huawei. 607e2034dSPavel Fedin * Copyright (c) 2015 Samsung Electronics Co., Ltd. 7ff8f06eeSShlomo Pongratz * Written by Peter Maydell 807e2034dSPavel Fedin * Reworked for GICv3 by Shlomo Pongratz and Pavel Fedin 9ff8f06eeSShlomo Pongratz * 10ff8f06eeSShlomo Pongratz * This program is free software; you can redistribute it and/or modify 11ff8f06eeSShlomo Pongratz * it under the terms of the GNU General Public License as published by 12ff8f06eeSShlomo Pongratz * the Free Software Foundation, either version 2 of the License, or 13ff8f06eeSShlomo Pongratz * (at your option) any later version. 14ff8f06eeSShlomo Pongratz * 15ff8f06eeSShlomo Pongratz * This program is distributed in the hope that it will be useful, 16ff8f06eeSShlomo Pongratz * but WITHOUT ANY WARRANTY; without even the implied warranty of 17ff8f06eeSShlomo Pongratz * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18ff8f06eeSShlomo Pongratz * GNU General Public License for more details. 19ff8f06eeSShlomo Pongratz * 20ff8f06eeSShlomo Pongratz * You should have received a copy of the GNU General Public License along 21ff8f06eeSShlomo Pongratz * with this program; if not, see <http://www.gnu.org/licenses/>. 22ff8f06eeSShlomo Pongratz */ 23ff8f06eeSShlomo Pongratz 248ef94f0bSPeter Maydell #include "qemu/osdep.h" 25da34e65cSMarkus Armbruster #include "qapi/error.h" 2607e2034dSPavel Fedin #include "qom/cpu.h" 27ff8f06eeSShlomo Pongratz #include "hw/intc/arm_gicv3_common.h" 2807e2034dSPavel Fedin #include "gicv3_internal.h" 2907e2034dSPavel Fedin #include "hw/arm/linux-boot-if.h" 30ff8f06eeSShlomo Pongratz 31*44b1ff31SDr. David Alan Gilbert static int gicv3_pre_save(void *opaque) 32ff8f06eeSShlomo Pongratz { 33ff8f06eeSShlomo Pongratz GICv3State *s = (GICv3State *)opaque; 34ff8f06eeSShlomo Pongratz ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s); 35ff8f06eeSShlomo Pongratz 36ff8f06eeSShlomo Pongratz if (c->pre_save) { 37ff8f06eeSShlomo Pongratz c->pre_save(s); 38ff8f06eeSShlomo Pongratz } 39*44b1ff31SDr. David Alan Gilbert 40*44b1ff31SDr. David Alan Gilbert return 0; 41ff8f06eeSShlomo Pongratz } 42ff8f06eeSShlomo Pongratz 43ff8f06eeSShlomo Pongratz static int gicv3_post_load(void *opaque, int version_id) 44ff8f06eeSShlomo Pongratz { 45ff8f06eeSShlomo Pongratz GICv3State *s = (GICv3State *)opaque; 46ff8f06eeSShlomo Pongratz ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s); 47ff8f06eeSShlomo Pongratz 48ff8f06eeSShlomo Pongratz if (c->post_load) { 49ff8f06eeSShlomo Pongratz c->post_load(s); 50ff8f06eeSShlomo Pongratz } 51ff8f06eeSShlomo Pongratz return 0; 52ff8f06eeSShlomo Pongratz } 53ff8f06eeSShlomo Pongratz 544eb833b5SPeter Maydell static bool virt_state_needed(void *opaque) 554eb833b5SPeter Maydell { 564eb833b5SPeter Maydell GICv3CPUState *cs = opaque; 574eb833b5SPeter Maydell 584eb833b5SPeter Maydell return cs->num_list_regs != 0; 594eb833b5SPeter Maydell } 604eb833b5SPeter Maydell 614eb833b5SPeter Maydell static const VMStateDescription vmstate_gicv3_cpu_virt = { 624eb833b5SPeter Maydell .name = "arm_gicv3_cpu/virt", 634eb833b5SPeter Maydell .version_id = 1, 644eb833b5SPeter Maydell .minimum_version_id = 1, 654eb833b5SPeter Maydell .needed = virt_state_needed, 664eb833b5SPeter Maydell .fields = (VMStateField[]) { 674eb833b5SPeter Maydell VMSTATE_UINT64_2DARRAY(ich_apr, GICv3CPUState, 3, 4), 684eb833b5SPeter Maydell VMSTATE_UINT64(ich_hcr_el2, GICv3CPUState), 694eb833b5SPeter Maydell VMSTATE_UINT64_ARRAY(ich_lr_el2, GICv3CPUState, GICV3_LR_MAX), 704eb833b5SPeter Maydell VMSTATE_UINT64(ich_vmcr_el2, GICv3CPUState), 714eb833b5SPeter Maydell VMSTATE_END_OF_LIST() 724eb833b5SPeter Maydell } 734eb833b5SPeter Maydell }; 744eb833b5SPeter Maydell 756692aac4SVijaya Kumar K static int icc_sre_el1_reg_pre_load(void *opaque) 766692aac4SVijaya Kumar K { 776692aac4SVijaya Kumar K GICv3CPUState *cs = opaque; 786692aac4SVijaya Kumar K 796692aac4SVijaya Kumar K /* 806692aac4SVijaya Kumar K * If the sre_el1 subsection is not transferred this 816692aac4SVijaya Kumar K * means SRE_EL1 is 0x7 (which might not be the same as 826692aac4SVijaya Kumar K * our reset value). 836692aac4SVijaya Kumar K */ 846692aac4SVijaya Kumar K cs->icc_sre_el1 = 0x7; 856692aac4SVijaya Kumar K return 0; 866692aac4SVijaya Kumar K } 876692aac4SVijaya Kumar K 886692aac4SVijaya Kumar K static bool icc_sre_el1_reg_needed(void *opaque) 896692aac4SVijaya Kumar K { 906692aac4SVijaya Kumar K GICv3CPUState *cs = opaque; 916692aac4SVijaya Kumar K 926692aac4SVijaya Kumar K return cs->icc_sre_el1 != 7; 936692aac4SVijaya Kumar K } 946692aac4SVijaya Kumar K 956692aac4SVijaya Kumar K const VMStateDescription vmstate_gicv3_cpu_sre_el1 = { 966692aac4SVijaya Kumar K .name = "arm_gicv3_cpu/sre_el1", 976692aac4SVijaya Kumar K .version_id = 1, 986692aac4SVijaya Kumar K .minimum_version_id = 1, 996692aac4SVijaya Kumar K .pre_load = icc_sre_el1_reg_pre_load, 1006692aac4SVijaya Kumar K .needed = icc_sre_el1_reg_needed, 1016692aac4SVijaya Kumar K .fields = (VMStateField[]) { 1026692aac4SVijaya Kumar K VMSTATE_UINT64(icc_sre_el1, GICv3CPUState), 1036692aac4SVijaya Kumar K VMSTATE_END_OF_LIST() 1046692aac4SVijaya Kumar K } 1056692aac4SVijaya Kumar K }; 1066692aac4SVijaya Kumar K 107757caeedSPavel Fedin static const VMStateDescription vmstate_gicv3_cpu = { 108757caeedSPavel Fedin .name = "arm_gicv3_cpu", 109757caeedSPavel Fedin .version_id = 1, 110757caeedSPavel Fedin .minimum_version_id = 1, 111757caeedSPavel Fedin .fields = (VMStateField[]) { 112757caeedSPavel Fedin VMSTATE_UINT32(level, GICv3CPUState), 113757caeedSPavel Fedin VMSTATE_UINT32(gicr_ctlr, GICv3CPUState), 114757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(gicr_statusr, GICv3CPUState, 2), 115757caeedSPavel Fedin VMSTATE_UINT32(gicr_waker, GICv3CPUState), 116757caeedSPavel Fedin VMSTATE_UINT64(gicr_propbaser, GICv3CPUState), 117757caeedSPavel Fedin VMSTATE_UINT64(gicr_pendbaser, GICv3CPUState), 118757caeedSPavel Fedin VMSTATE_UINT32(gicr_igroupr0, GICv3CPUState), 119757caeedSPavel Fedin VMSTATE_UINT32(gicr_ienabler0, GICv3CPUState), 120757caeedSPavel Fedin VMSTATE_UINT32(gicr_ipendr0, GICv3CPUState), 121757caeedSPavel Fedin VMSTATE_UINT32(gicr_iactiver0, GICv3CPUState), 122757caeedSPavel Fedin VMSTATE_UINT32(edge_trigger, GICv3CPUState), 123757caeedSPavel Fedin VMSTATE_UINT32(gicr_igrpmodr0, GICv3CPUState), 124757caeedSPavel Fedin VMSTATE_UINT32(gicr_nsacr, GICv3CPUState), 125757caeedSPavel Fedin VMSTATE_UINT8_ARRAY(gicr_ipriorityr, GICv3CPUState, GIC_INTERNAL), 126757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(icc_ctlr_el1, GICv3CPUState, 2), 127757caeedSPavel Fedin VMSTATE_UINT64(icc_pmr_el1, GICv3CPUState), 128757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(icc_bpr, GICv3CPUState, 3), 129757caeedSPavel Fedin VMSTATE_UINT64_2DARRAY(icc_apr, GICv3CPUState, 3, 4), 130757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(icc_igrpen, GICv3CPUState, 3), 131757caeedSPavel Fedin VMSTATE_UINT64(icc_ctlr_el3, GICv3CPUState), 132757caeedSPavel Fedin VMSTATE_END_OF_LIST() 1334eb833b5SPeter Maydell }, 1344eb833b5SPeter Maydell .subsections = (const VMStateDescription * []) { 1354eb833b5SPeter Maydell &vmstate_gicv3_cpu_virt, 1364eb833b5SPeter Maydell NULL 1376692aac4SVijaya Kumar K }, 1386692aac4SVijaya Kumar K .subsections = (const VMStateDescription * []) { 1396692aac4SVijaya Kumar K &vmstate_gicv3_cpu_sre_el1, 1406692aac4SVijaya Kumar K NULL 141757caeedSPavel Fedin } 142757caeedSPavel Fedin }; 143757caeedSPavel Fedin 144ff8f06eeSShlomo Pongratz static const VMStateDescription vmstate_gicv3 = { 145ff8f06eeSShlomo Pongratz .name = "arm_gicv3", 146757caeedSPavel Fedin .version_id = 1, 147757caeedSPavel Fedin .minimum_version_id = 1, 148ff8f06eeSShlomo Pongratz .pre_save = gicv3_pre_save, 149ff8f06eeSShlomo Pongratz .post_load = gicv3_post_load, 150252a7a6aSEric Auger .priority = MIG_PRI_GICV3, 151757caeedSPavel Fedin .fields = (VMStateField[]) { 152757caeedSPavel Fedin VMSTATE_UINT32(gicd_ctlr, GICv3State), 153757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(gicd_statusr, GICv3State, 2), 154757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(group, GICv3State, GICV3_BMP_SIZE), 155757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(grpmod, GICv3State, GICV3_BMP_SIZE), 156757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(enabled, GICv3State, GICV3_BMP_SIZE), 157757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(pending, GICv3State, GICV3_BMP_SIZE), 158757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(active, GICv3State, GICV3_BMP_SIZE), 159757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(level, GICv3State, GICV3_BMP_SIZE), 160757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(edge_trigger, GICv3State, GICV3_BMP_SIZE), 161757caeedSPavel Fedin VMSTATE_UINT8_ARRAY(gicd_ipriority, GICv3State, GICV3_MAXIRQ), 162757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(gicd_irouter, GICv3State, GICV3_MAXIRQ), 163757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(gicd_nsacr, GICv3State, 164757caeedSPavel Fedin DIV_ROUND_UP(GICV3_MAXIRQ, 16)), 165757caeedSPavel Fedin VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, GICv3State, num_cpu, 166757caeedSPavel Fedin vmstate_gicv3_cpu, GICv3CPUState), 167757caeedSPavel Fedin VMSTATE_END_OF_LIST() 168757caeedSPavel Fedin } 169ff8f06eeSShlomo Pongratz }; 170ff8f06eeSShlomo Pongratz 171ff8f06eeSShlomo Pongratz void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler, 172ff8f06eeSShlomo Pongratz const MemoryRegionOps *ops) 173ff8f06eeSShlomo Pongratz { 174ff8f06eeSShlomo Pongratz SysBusDevice *sbd = SYS_BUS_DEVICE(s); 175ff8f06eeSShlomo Pongratz int i; 176ff8f06eeSShlomo Pongratz 177ff8f06eeSShlomo Pongratz /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU. 178ff8f06eeSShlomo Pongratz * GPIO array layout is thus: 179ff8f06eeSShlomo Pongratz * [0..N-1] spi 180ff8f06eeSShlomo Pongratz * [N..N+31] PPIs for CPU 0 181ff8f06eeSShlomo Pongratz * [N+32..N+63] PPIs for CPU 1 182ff8f06eeSShlomo Pongratz * ... 183ff8f06eeSShlomo Pongratz */ 184ff8f06eeSShlomo Pongratz i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu; 185ff8f06eeSShlomo Pongratz qdev_init_gpio_in(DEVICE(s), handler, i); 186ff8f06eeSShlomo Pongratz 187ff8f06eeSShlomo Pongratz for (i = 0; i < s->num_cpu; i++) { 1883faf2b0cSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_irq); 189ff8f06eeSShlomo Pongratz } 190ff8f06eeSShlomo Pongratz for (i = 0; i < s->num_cpu; i++) { 1913faf2b0cSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_fiq); 192ff8f06eeSShlomo Pongratz } 193b53db42bSPeter Maydell for (i = 0; i < s->num_cpu; i++) { 194b53db42bSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_virq); 195b53db42bSPeter Maydell } 196b53db42bSPeter Maydell for (i = 0; i < s->num_cpu; i++) { 197b53db42bSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_vfiq); 198b53db42bSPeter Maydell } 199ff8f06eeSShlomo Pongratz 200ff8f06eeSShlomo Pongratz memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s, 201ff8f06eeSShlomo Pongratz "gicv3_dist", 0x10000); 202ff8f06eeSShlomo Pongratz memory_region_init_io(&s->iomem_redist, OBJECT(s), ops ? &ops[1] : NULL, s, 203ff8f06eeSShlomo Pongratz "gicv3_redist", 0x20000 * s->num_cpu); 204ff8f06eeSShlomo Pongratz 205ff8f06eeSShlomo Pongratz sysbus_init_mmio(sbd, &s->iomem_dist); 206ff8f06eeSShlomo Pongratz sysbus_init_mmio(sbd, &s->iomem_redist); 207ff8f06eeSShlomo Pongratz } 208ff8f06eeSShlomo Pongratz 209ff8f06eeSShlomo Pongratz static void arm_gicv3_common_realize(DeviceState *dev, Error **errp) 210ff8f06eeSShlomo Pongratz { 211ff8f06eeSShlomo Pongratz GICv3State *s = ARM_GICV3_COMMON(dev); 21207e2034dSPavel Fedin int i; 213ff8f06eeSShlomo Pongratz 214ff8f06eeSShlomo Pongratz /* revision property is actually reserved and currently used only in order 215ff8f06eeSShlomo Pongratz * to keep the interface compatible with GICv2 code, avoiding extra 216ff8f06eeSShlomo Pongratz * conditions. However, in future it could be used, for example, if we 217ff8f06eeSShlomo Pongratz * implement GICv4. 218ff8f06eeSShlomo Pongratz */ 219ff8f06eeSShlomo Pongratz if (s->revision != 3) { 220ff8f06eeSShlomo Pongratz error_setg(errp, "unsupported GIC revision %d", s->revision); 221ff8f06eeSShlomo Pongratz return; 222ff8f06eeSShlomo Pongratz } 22307e2034dSPavel Fedin 22407e2034dSPavel Fedin if (s->num_irq > GICV3_MAXIRQ) { 22507e2034dSPavel Fedin error_setg(errp, 22607e2034dSPavel Fedin "requested %u interrupt lines exceeds GIC maximum %d", 22707e2034dSPavel Fedin s->num_irq, GICV3_MAXIRQ); 22807e2034dSPavel Fedin return; 22907e2034dSPavel Fedin } 23007e2034dSPavel Fedin if (s->num_irq < GIC_INTERNAL) { 23107e2034dSPavel Fedin error_setg(errp, 23207e2034dSPavel Fedin "requested %u interrupt lines is below GIC minimum %d", 23307e2034dSPavel Fedin s->num_irq, GIC_INTERNAL); 23407e2034dSPavel Fedin return; 23507e2034dSPavel Fedin } 23607e2034dSPavel Fedin 23707e2034dSPavel Fedin /* ITLinesNumber is represented as (N / 32) - 1, so this is an 23807e2034dSPavel Fedin * implementation imposed restriction, not an architectural one, 23907e2034dSPavel Fedin * so we don't have to deal with bitfields where only some of the 24007e2034dSPavel Fedin * bits in a 32-bit word should be valid. 24107e2034dSPavel Fedin */ 24207e2034dSPavel Fedin if (s->num_irq % 32) { 24307e2034dSPavel Fedin error_setg(errp, 24407e2034dSPavel Fedin "%d interrupt lines unsupported: not divisible by 32", 24507e2034dSPavel Fedin s->num_irq); 24607e2034dSPavel Fedin return; 24707e2034dSPavel Fedin } 24807e2034dSPavel Fedin 24907e2034dSPavel Fedin s->cpu = g_new0(GICv3CPUState, s->num_cpu); 25007e2034dSPavel Fedin 25107e2034dSPavel Fedin for (i = 0; i < s->num_cpu; i++) { 25207e2034dSPavel Fedin CPUState *cpu = qemu_get_cpu(i); 25307e2034dSPavel Fedin uint64_t cpu_affid; 25407e2034dSPavel Fedin int last; 25507e2034dSPavel Fedin 25607e2034dSPavel Fedin s->cpu[i].cpu = cpu; 25707e2034dSPavel Fedin s->cpu[i].gic = s; 258d3a3e529SVijaya Kumar K /* Store GICv3CPUState in CPUARMState gicv3state pointer */ 259d3a3e529SVijaya Kumar K gicv3_set_gicv3state(cpu, &s->cpu[i]); 26007e2034dSPavel Fedin 26107e2034dSPavel Fedin /* Pre-construct the GICR_TYPER: 26207e2034dSPavel Fedin * For our implementation: 26307e2034dSPavel Fedin * Top 32 bits are the affinity value of the associated CPU 26407e2034dSPavel Fedin * CommonLPIAff == 01 (redistributors with same Aff3 share LPI table) 26507e2034dSPavel Fedin * Processor_Number == CPU index starting from 0 26607e2034dSPavel Fedin * DPGS == 0 (GICR_CTLR.DPG* not supported) 26707e2034dSPavel Fedin * Last == 1 if this is the last redistributor in a series of 26807e2034dSPavel Fedin * contiguous redistributor pages 26907e2034dSPavel Fedin * DirectLPI == 0 (direct injection of LPIs not supported) 27007e2034dSPavel Fedin * VLPIS == 0 (virtual LPIs not supported) 27107e2034dSPavel Fedin * PLPIS == 0 (physical LPIs not supported) 27207e2034dSPavel Fedin */ 27377a7a367SMarc-André Lureau cpu_affid = object_property_get_uint(OBJECT(cpu), "mp-affinity", NULL); 27407e2034dSPavel Fedin last = (i == s->num_cpu - 1); 27507e2034dSPavel Fedin 27607e2034dSPavel Fedin /* The CPU mp-affinity property is in MPIDR register format; squash 27707e2034dSPavel Fedin * the affinity bytes into 32 bits as the GICR_TYPER has them. 27807e2034dSPavel Fedin */ 27992204403SAndrew Jones cpu_affid = ((cpu_affid & 0xFF00000000ULL) >> 8) | 28092204403SAndrew Jones (cpu_affid & 0xFFFFFF); 28107e2034dSPavel Fedin s->cpu[i].gicr_typer = (cpu_affid << 32) | 28207e2034dSPavel Fedin (1 << 24) | 28307e2034dSPavel Fedin (i << 8) | 28407e2034dSPavel Fedin (last << 4); 28507e2034dSPavel Fedin } 286ff8f06eeSShlomo Pongratz } 287ff8f06eeSShlomo Pongratz 288ff8f06eeSShlomo Pongratz static void arm_gicv3_common_reset(DeviceState *dev) 289ff8f06eeSShlomo Pongratz { 29007e2034dSPavel Fedin GICv3State *s = ARM_GICV3_COMMON(dev); 29107e2034dSPavel Fedin int i; 29207e2034dSPavel Fedin 29307e2034dSPavel Fedin for (i = 0; i < s->num_cpu; i++) { 29407e2034dSPavel Fedin GICv3CPUState *cs = &s->cpu[i]; 29507e2034dSPavel Fedin 29607e2034dSPavel Fedin cs->level = 0; 29707e2034dSPavel Fedin cs->gicr_ctlr = 0; 29807e2034dSPavel Fedin cs->gicr_statusr[GICV3_S] = 0; 29907e2034dSPavel Fedin cs->gicr_statusr[GICV3_NS] = 0; 30007e2034dSPavel Fedin cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep; 30107e2034dSPavel Fedin cs->gicr_propbaser = 0; 30207e2034dSPavel Fedin cs->gicr_pendbaser = 0; 30307e2034dSPavel Fedin /* If we're resetting a TZ-aware GIC as if secure firmware 30407e2034dSPavel Fedin * had set it up ready to start a kernel in non-secure, we 30507e2034dSPavel Fedin * need to set interrupts to group 1 so the kernel can use them. 30607e2034dSPavel Fedin * Otherwise they reset to group 0 like the hardware. 30707e2034dSPavel Fedin */ 30807e2034dSPavel Fedin if (s->irq_reset_nonsecure) { 30907e2034dSPavel Fedin cs->gicr_igroupr0 = 0xffffffff; 31007e2034dSPavel Fedin } else { 31107e2034dSPavel Fedin cs->gicr_igroupr0 = 0; 31207e2034dSPavel Fedin } 31307e2034dSPavel Fedin 31407e2034dSPavel Fedin cs->gicr_ienabler0 = 0; 31507e2034dSPavel Fedin cs->gicr_ipendr0 = 0; 31607e2034dSPavel Fedin cs->gicr_iactiver0 = 0; 31707e2034dSPavel Fedin cs->edge_trigger = 0xffff; 31807e2034dSPavel Fedin cs->gicr_igrpmodr0 = 0; 31907e2034dSPavel Fedin cs->gicr_nsacr = 0; 32007e2034dSPavel Fedin memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr)); 32107e2034dSPavel Fedin 322ce187c3cSPeter Maydell cs->hppi.prio = 0xff; 323ce187c3cSPeter Maydell 32407e2034dSPavel Fedin /* State in the CPU interface must *not* be reset here, because it 32507e2034dSPavel Fedin * is part of the CPU's reset domain, not the GIC device's. 32607e2034dSPavel Fedin */ 32707e2034dSPavel Fedin } 32807e2034dSPavel Fedin 32907e2034dSPavel Fedin /* For our implementation affinity routing is always enabled */ 33007e2034dSPavel Fedin if (s->security_extn) { 33107e2034dSPavel Fedin s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS; 33207e2034dSPavel Fedin } else { 33307e2034dSPavel Fedin s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE; 33407e2034dSPavel Fedin } 33507e2034dSPavel Fedin 33607e2034dSPavel Fedin s->gicd_statusr[GICV3_S] = 0; 33707e2034dSPavel Fedin s->gicd_statusr[GICV3_NS] = 0; 33807e2034dSPavel Fedin 33907e2034dSPavel Fedin memset(s->group, 0, sizeof(s->group)); 34007e2034dSPavel Fedin memset(s->grpmod, 0, sizeof(s->grpmod)); 34107e2034dSPavel Fedin memset(s->enabled, 0, sizeof(s->enabled)); 34207e2034dSPavel Fedin memset(s->pending, 0, sizeof(s->pending)); 34307e2034dSPavel Fedin memset(s->active, 0, sizeof(s->active)); 34407e2034dSPavel Fedin memset(s->level, 0, sizeof(s->level)); 34507e2034dSPavel Fedin memset(s->edge_trigger, 0, sizeof(s->edge_trigger)); 34607e2034dSPavel Fedin memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority)); 34707e2034dSPavel Fedin memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter)); 34807e2034dSPavel Fedin memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr)); 349ce187c3cSPeter Maydell /* GICD_IROUTER are UNKNOWN at reset so in theory the guest must 350ce187c3cSPeter Maydell * write these to get sane behaviour and we need not populate the 351ce187c3cSPeter Maydell * pointer cache here; however having the cache be different for 352ce187c3cSPeter Maydell * "happened to be 0 from reset" and "guest wrote 0" would be 353ce187c3cSPeter Maydell * too confusing. 354ce187c3cSPeter Maydell */ 355ce187c3cSPeter Maydell gicv3_cache_all_target_cpustates(s); 35607e2034dSPavel Fedin 35707e2034dSPavel Fedin if (s->irq_reset_nonsecure) { 35807e2034dSPavel Fedin /* If we're resetting a TZ-aware GIC as if secure firmware 35907e2034dSPavel Fedin * had set it up ready to start a kernel in non-secure, we 36007e2034dSPavel Fedin * need to set interrupts to group 1 so the kernel can use them. 36107e2034dSPavel Fedin * Otherwise they reset to group 0 like the hardware. 36207e2034dSPavel Fedin */ 36307e2034dSPavel Fedin for (i = GIC_INTERNAL; i < s->num_irq; i++) { 36407e2034dSPavel Fedin gicv3_gicd_group_set(s, i); 36507e2034dSPavel Fedin } 36607e2034dSPavel Fedin } 36707e2034dSPavel Fedin } 36807e2034dSPavel Fedin 36907e2034dSPavel Fedin static void arm_gic_common_linux_init(ARMLinuxBootIf *obj, 37007e2034dSPavel Fedin bool secure_boot) 37107e2034dSPavel Fedin { 37207e2034dSPavel Fedin GICv3State *s = ARM_GICV3_COMMON(obj); 37307e2034dSPavel Fedin 37407e2034dSPavel Fedin if (s->security_extn && !secure_boot) { 37507e2034dSPavel Fedin /* We're directly booting a kernel into NonSecure. If this GIC 37607e2034dSPavel Fedin * implements the security extensions then we must configure it 37707e2034dSPavel Fedin * to have all the interrupts be NonSecure (this is a job that 37807e2034dSPavel Fedin * is done by the Secure boot firmware in real hardware, and in 37907e2034dSPavel Fedin * this mode QEMU is acting as a minimalist firmware-and-bootloader 38007e2034dSPavel Fedin * equivalent). 38107e2034dSPavel Fedin */ 38207e2034dSPavel Fedin s->irq_reset_nonsecure = true; 38307e2034dSPavel Fedin } 384ff8f06eeSShlomo Pongratz } 385ff8f06eeSShlomo Pongratz 386ff8f06eeSShlomo Pongratz static Property arm_gicv3_common_properties[] = { 387ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1), 388ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32), 389ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("revision", GICv3State, revision, 3), 390ff8f06eeSShlomo Pongratz DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0), 391ff8f06eeSShlomo Pongratz DEFINE_PROP_END_OF_LIST(), 392ff8f06eeSShlomo Pongratz }; 393ff8f06eeSShlomo Pongratz 394ff8f06eeSShlomo Pongratz static void arm_gicv3_common_class_init(ObjectClass *klass, void *data) 395ff8f06eeSShlomo Pongratz { 396ff8f06eeSShlomo Pongratz DeviceClass *dc = DEVICE_CLASS(klass); 39707e2034dSPavel Fedin ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass); 398ff8f06eeSShlomo Pongratz 399ff8f06eeSShlomo Pongratz dc->reset = arm_gicv3_common_reset; 400ff8f06eeSShlomo Pongratz dc->realize = arm_gicv3_common_realize; 401ff8f06eeSShlomo Pongratz dc->props = arm_gicv3_common_properties; 402ff8f06eeSShlomo Pongratz dc->vmsd = &vmstate_gicv3; 40307e2034dSPavel Fedin albifc->arm_linux_init = arm_gic_common_linux_init; 404ff8f06eeSShlomo Pongratz } 405ff8f06eeSShlomo Pongratz 406ff8f06eeSShlomo Pongratz static const TypeInfo arm_gicv3_common_type = { 407ff8f06eeSShlomo Pongratz .name = TYPE_ARM_GICV3_COMMON, 408ff8f06eeSShlomo Pongratz .parent = TYPE_SYS_BUS_DEVICE, 409ff8f06eeSShlomo Pongratz .instance_size = sizeof(GICv3State), 410ff8f06eeSShlomo Pongratz .class_size = sizeof(ARMGICv3CommonClass), 411ff8f06eeSShlomo Pongratz .class_init = arm_gicv3_common_class_init, 412ff8f06eeSShlomo Pongratz .abstract = true, 41307e2034dSPavel Fedin .interfaces = (InterfaceInfo []) { 41407e2034dSPavel Fedin { TYPE_ARM_LINUX_BOOT_IF }, 41507e2034dSPavel Fedin { }, 41607e2034dSPavel Fedin }, 417ff8f06eeSShlomo Pongratz }; 418ff8f06eeSShlomo Pongratz 419ff8f06eeSShlomo Pongratz static void register_types(void) 420ff8f06eeSShlomo Pongratz { 421ff8f06eeSShlomo Pongratz type_register_static(&arm_gicv3_common_type); 422ff8f06eeSShlomo Pongratz } 423ff8f06eeSShlomo Pongratz 424ff8f06eeSShlomo Pongratz type_init(register_types) 425