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 31ff8f06eeSShlomo Pongratz static void 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 } 39ff8f06eeSShlomo Pongratz } 40ff8f06eeSShlomo Pongratz 41ff8f06eeSShlomo Pongratz static int gicv3_post_load(void *opaque, int version_id) 42ff8f06eeSShlomo Pongratz { 43ff8f06eeSShlomo Pongratz GICv3State *s = (GICv3State *)opaque; 44ff8f06eeSShlomo Pongratz ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s); 45ff8f06eeSShlomo Pongratz 46ff8f06eeSShlomo Pongratz if (c->post_load) { 47ff8f06eeSShlomo Pongratz c->post_load(s); 48ff8f06eeSShlomo Pongratz } 49ff8f06eeSShlomo Pongratz return 0; 50ff8f06eeSShlomo Pongratz } 51ff8f06eeSShlomo Pongratz 52ff8f06eeSShlomo Pongratz static const VMStateDescription vmstate_gicv3 = { 53ff8f06eeSShlomo Pongratz .name = "arm_gicv3", 54ff8f06eeSShlomo Pongratz .unmigratable = 1, 55ff8f06eeSShlomo Pongratz .pre_save = gicv3_pre_save, 56ff8f06eeSShlomo Pongratz .post_load = gicv3_post_load, 57ff8f06eeSShlomo Pongratz }; 58ff8f06eeSShlomo Pongratz 59ff8f06eeSShlomo Pongratz void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler, 60ff8f06eeSShlomo Pongratz const MemoryRegionOps *ops) 61ff8f06eeSShlomo Pongratz { 62ff8f06eeSShlomo Pongratz SysBusDevice *sbd = SYS_BUS_DEVICE(s); 63ff8f06eeSShlomo Pongratz int i; 64ff8f06eeSShlomo Pongratz 65ff8f06eeSShlomo Pongratz /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU. 66ff8f06eeSShlomo Pongratz * GPIO array layout is thus: 67ff8f06eeSShlomo Pongratz * [0..N-1] spi 68ff8f06eeSShlomo Pongratz * [N..N+31] PPIs for CPU 0 69ff8f06eeSShlomo Pongratz * [N+32..N+63] PPIs for CPU 1 70ff8f06eeSShlomo Pongratz * ... 71ff8f06eeSShlomo Pongratz */ 72ff8f06eeSShlomo Pongratz i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu; 73ff8f06eeSShlomo Pongratz qdev_init_gpio_in(DEVICE(s), handler, i); 74ff8f06eeSShlomo Pongratz 75ff8f06eeSShlomo Pongratz for (i = 0; i < s->num_cpu; i++) { 76*3faf2b0cSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_irq); 77ff8f06eeSShlomo Pongratz } 78ff8f06eeSShlomo Pongratz for (i = 0; i < s->num_cpu; i++) { 79*3faf2b0cSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_fiq); 80ff8f06eeSShlomo Pongratz } 81ff8f06eeSShlomo Pongratz 82ff8f06eeSShlomo Pongratz memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s, 83ff8f06eeSShlomo Pongratz "gicv3_dist", 0x10000); 84ff8f06eeSShlomo Pongratz memory_region_init_io(&s->iomem_redist, OBJECT(s), ops ? &ops[1] : NULL, s, 85ff8f06eeSShlomo Pongratz "gicv3_redist", 0x20000 * s->num_cpu); 86ff8f06eeSShlomo Pongratz 87ff8f06eeSShlomo Pongratz sysbus_init_mmio(sbd, &s->iomem_dist); 88ff8f06eeSShlomo Pongratz sysbus_init_mmio(sbd, &s->iomem_redist); 89ff8f06eeSShlomo Pongratz } 90ff8f06eeSShlomo Pongratz 91ff8f06eeSShlomo Pongratz static void arm_gicv3_common_realize(DeviceState *dev, Error **errp) 92ff8f06eeSShlomo Pongratz { 93ff8f06eeSShlomo Pongratz GICv3State *s = ARM_GICV3_COMMON(dev); 9407e2034dSPavel Fedin int i; 95ff8f06eeSShlomo Pongratz 96ff8f06eeSShlomo Pongratz /* revision property is actually reserved and currently used only in order 97ff8f06eeSShlomo Pongratz * to keep the interface compatible with GICv2 code, avoiding extra 98ff8f06eeSShlomo Pongratz * conditions. However, in future it could be used, for example, if we 99ff8f06eeSShlomo Pongratz * implement GICv4. 100ff8f06eeSShlomo Pongratz */ 101ff8f06eeSShlomo Pongratz if (s->revision != 3) { 102ff8f06eeSShlomo Pongratz error_setg(errp, "unsupported GIC revision %d", s->revision); 103ff8f06eeSShlomo Pongratz return; 104ff8f06eeSShlomo Pongratz } 10507e2034dSPavel Fedin 10607e2034dSPavel Fedin if (s->num_irq > GICV3_MAXIRQ) { 10707e2034dSPavel Fedin error_setg(errp, 10807e2034dSPavel Fedin "requested %u interrupt lines exceeds GIC maximum %d", 10907e2034dSPavel Fedin s->num_irq, GICV3_MAXIRQ); 11007e2034dSPavel Fedin return; 11107e2034dSPavel Fedin } 11207e2034dSPavel Fedin if (s->num_irq < GIC_INTERNAL) { 11307e2034dSPavel Fedin error_setg(errp, 11407e2034dSPavel Fedin "requested %u interrupt lines is below GIC minimum %d", 11507e2034dSPavel Fedin s->num_irq, GIC_INTERNAL); 11607e2034dSPavel Fedin return; 11707e2034dSPavel Fedin } 11807e2034dSPavel Fedin 11907e2034dSPavel Fedin /* ITLinesNumber is represented as (N / 32) - 1, so this is an 12007e2034dSPavel Fedin * implementation imposed restriction, not an architectural one, 12107e2034dSPavel Fedin * so we don't have to deal with bitfields where only some of the 12207e2034dSPavel Fedin * bits in a 32-bit word should be valid. 12307e2034dSPavel Fedin */ 12407e2034dSPavel Fedin if (s->num_irq % 32) { 12507e2034dSPavel Fedin error_setg(errp, 12607e2034dSPavel Fedin "%d interrupt lines unsupported: not divisible by 32", 12707e2034dSPavel Fedin s->num_irq); 12807e2034dSPavel Fedin return; 12907e2034dSPavel Fedin } 13007e2034dSPavel Fedin 13107e2034dSPavel Fedin s->cpu = g_new0(GICv3CPUState, s->num_cpu); 13207e2034dSPavel Fedin 13307e2034dSPavel Fedin for (i = 0; i < s->num_cpu; i++) { 13407e2034dSPavel Fedin CPUState *cpu = qemu_get_cpu(i); 13507e2034dSPavel Fedin uint64_t cpu_affid; 13607e2034dSPavel Fedin int last; 13707e2034dSPavel Fedin 13807e2034dSPavel Fedin s->cpu[i].cpu = cpu; 13907e2034dSPavel Fedin s->cpu[i].gic = s; 14007e2034dSPavel Fedin 14107e2034dSPavel Fedin /* Pre-construct the GICR_TYPER: 14207e2034dSPavel Fedin * For our implementation: 14307e2034dSPavel Fedin * Top 32 bits are the affinity value of the associated CPU 14407e2034dSPavel Fedin * CommonLPIAff == 01 (redistributors with same Aff3 share LPI table) 14507e2034dSPavel Fedin * Processor_Number == CPU index starting from 0 14607e2034dSPavel Fedin * DPGS == 0 (GICR_CTLR.DPG* not supported) 14707e2034dSPavel Fedin * Last == 1 if this is the last redistributor in a series of 14807e2034dSPavel Fedin * contiguous redistributor pages 14907e2034dSPavel Fedin * DirectLPI == 0 (direct injection of LPIs not supported) 15007e2034dSPavel Fedin * VLPIS == 0 (virtual LPIs not supported) 15107e2034dSPavel Fedin * PLPIS == 0 (physical LPIs not supported) 15207e2034dSPavel Fedin */ 15307e2034dSPavel Fedin cpu_affid = object_property_get_int(OBJECT(cpu), "mp-affinity", NULL); 15407e2034dSPavel Fedin last = (i == s->num_cpu - 1); 15507e2034dSPavel Fedin 15607e2034dSPavel Fedin /* The CPU mp-affinity property is in MPIDR register format; squash 15707e2034dSPavel Fedin * the affinity bytes into 32 bits as the GICR_TYPER has them. 15807e2034dSPavel Fedin */ 15907e2034dSPavel Fedin cpu_affid = (cpu_affid & 0xFF00000000ULL >> 8) | (cpu_affid & 0xFFFFFF); 16007e2034dSPavel Fedin s->cpu[i].gicr_typer = (cpu_affid << 32) | 16107e2034dSPavel Fedin (1 << 24) | 16207e2034dSPavel Fedin (i << 8) | 16307e2034dSPavel Fedin (last << 4); 16407e2034dSPavel Fedin } 165ff8f06eeSShlomo Pongratz } 166ff8f06eeSShlomo Pongratz 167ff8f06eeSShlomo Pongratz static void arm_gicv3_common_reset(DeviceState *dev) 168ff8f06eeSShlomo Pongratz { 16907e2034dSPavel Fedin GICv3State *s = ARM_GICV3_COMMON(dev); 17007e2034dSPavel Fedin int i; 17107e2034dSPavel Fedin 17207e2034dSPavel Fedin for (i = 0; i < s->num_cpu; i++) { 17307e2034dSPavel Fedin GICv3CPUState *cs = &s->cpu[i]; 17407e2034dSPavel Fedin 17507e2034dSPavel Fedin cs->level = 0; 17607e2034dSPavel Fedin cs->gicr_ctlr = 0; 17707e2034dSPavel Fedin cs->gicr_statusr[GICV3_S] = 0; 17807e2034dSPavel Fedin cs->gicr_statusr[GICV3_NS] = 0; 17907e2034dSPavel Fedin cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep; 18007e2034dSPavel Fedin cs->gicr_propbaser = 0; 18107e2034dSPavel Fedin cs->gicr_pendbaser = 0; 18207e2034dSPavel Fedin /* If we're resetting a TZ-aware GIC as if secure firmware 18307e2034dSPavel Fedin * had set it up ready to start a kernel in non-secure, we 18407e2034dSPavel Fedin * need to set interrupts to group 1 so the kernel can use them. 18507e2034dSPavel Fedin * Otherwise they reset to group 0 like the hardware. 18607e2034dSPavel Fedin */ 18707e2034dSPavel Fedin if (s->irq_reset_nonsecure) { 18807e2034dSPavel Fedin cs->gicr_igroupr0 = 0xffffffff; 18907e2034dSPavel Fedin } else { 19007e2034dSPavel Fedin cs->gicr_igroupr0 = 0; 19107e2034dSPavel Fedin } 19207e2034dSPavel Fedin 19307e2034dSPavel Fedin cs->gicr_ienabler0 = 0; 19407e2034dSPavel Fedin cs->gicr_ipendr0 = 0; 19507e2034dSPavel Fedin cs->gicr_iactiver0 = 0; 19607e2034dSPavel Fedin cs->edge_trigger = 0xffff; 19707e2034dSPavel Fedin cs->gicr_igrpmodr0 = 0; 19807e2034dSPavel Fedin cs->gicr_nsacr = 0; 19907e2034dSPavel Fedin memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr)); 20007e2034dSPavel Fedin 20107e2034dSPavel Fedin /* State in the CPU interface must *not* be reset here, because it 20207e2034dSPavel Fedin * is part of the CPU's reset domain, not the GIC device's. 20307e2034dSPavel Fedin */ 20407e2034dSPavel Fedin } 20507e2034dSPavel Fedin 20607e2034dSPavel Fedin /* For our implementation affinity routing is always enabled */ 20707e2034dSPavel Fedin if (s->security_extn) { 20807e2034dSPavel Fedin s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS; 20907e2034dSPavel Fedin } else { 21007e2034dSPavel Fedin s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE; 21107e2034dSPavel Fedin } 21207e2034dSPavel Fedin 21307e2034dSPavel Fedin s->gicd_statusr[GICV3_S] = 0; 21407e2034dSPavel Fedin s->gicd_statusr[GICV3_NS] = 0; 21507e2034dSPavel Fedin 21607e2034dSPavel Fedin memset(s->group, 0, sizeof(s->group)); 21707e2034dSPavel Fedin memset(s->grpmod, 0, sizeof(s->grpmod)); 21807e2034dSPavel Fedin memset(s->enabled, 0, sizeof(s->enabled)); 21907e2034dSPavel Fedin memset(s->pending, 0, sizeof(s->pending)); 22007e2034dSPavel Fedin memset(s->active, 0, sizeof(s->active)); 22107e2034dSPavel Fedin memset(s->level, 0, sizeof(s->level)); 22207e2034dSPavel Fedin memset(s->edge_trigger, 0, sizeof(s->edge_trigger)); 22307e2034dSPavel Fedin memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority)); 22407e2034dSPavel Fedin memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter)); 22507e2034dSPavel Fedin memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr)); 22607e2034dSPavel Fedin 22707e2034dSPavel Fedin if (s->irq_reset_nonsecure) { 22807e2034dSPavel Fedin /* If we're resetting a TZ-aware GIC as if secure firmware 22907e2034dSPavel Fedin * had set it up ready to start a kernel in non-secure, we 23007e2034dSPavel Fedin * need to set interrupts to group 1 so the kernel can use them. 23107e2034dSPavel Fedin * Otherwise they reset to group 0 like the hardware. 23207e2034dSPavel Fedin */ 23307e2034dSPavel Fedin for (i = GIC_INTERNAL; i < s->num_irq; i++) { 23407e2034dSPavel Fedin gicv3_gicd_group_set(s, i); 23507e2034dSPavel Fedin } 23607e2034dSPavel Fedin } 23707e2034dSPavel Fedin } 23807e2034dSPavel Fedin 23907e2034dSPavel Fedin static void arm_gic_common_linux_init(ARMLinuxBootIf *obj, 24007e2034dSPavel Fedin bool secure_boot) 24107e2034dSPavel Fedin { 24207e2034dSPavel Fedin GICv3State *s = ARM_GICV3_COMMON(obj); 24307e2034dSPavel Fedin 24407e2034dSPavel Fedin if (s->security_extn && !secure_boot) { 24507e2034dSPavel Fedin /* We're directly booting a kernel into NonSecure. If this GIC 24607e2034dSPavel Fedin * implements the security extensions then we must configure it 24707e2034dSPavel Fedin * to have all the interrupts be NonSecure (this is a job that 24807e2034dSPavel Fedin * is done by the Secure boot firmware in real hardware, and in 24907e2034dSPavel Fedin * this mode QEMU is acting as a minimalist firmware-and-bootloader 25007e2034dSPavel Fedin * equivalent). 25107e2034dSPavel Fedin */ 25207e2034dSPavel Fedin s->irq_reset_nonsecure = true; 25307e2034dSPavel Fedin } 254ff8f06eeSShlomo Pongratz } 255ff8f06eeSShlomo Pongratz 256ff8f06eeSShlomo Pongratz static Property arm_gicv3_common_properties[] = { 257ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1), 258ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32), 259ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("revision", GICv3State, revision, 3), 260ff8f06eeSShlomo Pongratz DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0), 261ff8f06eeSShlomo Pongratz DEFINE_PROP_END_OF_LIST(), 262ff8f06eeSShlomo Pongratz }; 263ff8f06eeSShlomo Pongratz 264ff8f06eeSShlomo Pongratz static void arm_gicv3_common_class_init(ObjectClass *klass, void *data) 265ff8f06eeSShlomo Pongratz { 266ff8f06eeSShlomo Pongratz DeviceClass *dc = DEVICE_CLASS(klass); 26707e2034dSPavel Fedin ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass); 268ff8f06eeSShlomo Pongratz 269ff8f06eeSShlomo Pongratz dc->reset = arm_gicv3_common_reset; 270ff8f06eeSShlomo Pongratz dc->realize = arm_gicv3_common_realize; 271ff8f06eeSShlomo Pongratz dc->props = arm_gicv3_common_properties; 272ff8f06eeSShlomo Pongratz dc->vmsd = &vmstate_gicv3; 27307e2034dSPavel Fedin albifc->arm_linux_init = arm_gic_common_linux_init; 274ff8f06eeSShlomo Pongratz } 275ff8f06eeSShlomo Pongratz 276ff8f06eeSShlomo Pongratz static const TypeInfo arm_gicv3_common_type = { 277ff8f06eeSShlomo Pongratz .name = TYPE_ARM_GICV3_COMMON, 278ff8f06eeSShlomo Pongratz .parent = TYPE_SYS_BUS_DEVICE, 279ff8f06eeSShlomo Pongratz .instance_size = sizeof(GICv3State), 280ff8f06eeSShlomo Pongratz .class_size = sizeof(ARMGICv3CommonClass), 281ff8f06eeSShlomo Pongratz .class_init = arm_gicv3_common_class_init, 282ff8f06eeSShlomo Pongratz .abstract = true, 28307e2034dSPavel Fedin .interfaces = (InterfaceInfo []) { 28407e2034dSPavel Fedin { TYPE_ARM_LINUX_BOOT_IF }, 28507e2034dSPavel Fedin { }, 28607e2034dSPavel Fedin }, 287ff8f06eeSShlomo Pongratz }; 288ff8f06eeSShlomo Pongratz 289ff8f06eeSShlomo Pongratz static void register_types(void) 290ff8f06eeSShlomo Pongratz { 291ff8f06eeSShlomo Pongratz type_register_static(&arm_gicv3_common_type); 292ff8f06eeSShlomo Pongratz } 293ff8f06eeSShlomo Pongratz 294ff8f06eeSShlomo Pongratz type_init(register_types) 295