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" 30910e2048SShannon Zhao #include "sysemu/kvm.h" 31ff8f06eeSShlomo Pongratz 3244b1ff31SDr. David Alan Gilbert static int gicv3_pre_save(void *opaque) 33ff8f06eeSShlomo Pongratz { 34ff8f06eeSShlomo Pongratz GICv3State *s = (GICv3State *)opaque; 35ff8f06eeSShlomo Pongratz ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s); 36ff8f06eeSShlomo Pongratz 37ff8f06eeSShlomo Pongratz if (c->pre_save) { 38ff8f06eeSShlomo Pongratz c->pre_save(s); 39ff8f06eeSShlomo Pongratz } 4044b1ff31SDr. David Alan Gilbert 4144b1ff31SDr. David Alan Gilbert return 0; 42ff8f06eeSShlomo Pongratz } 43ff8f06eeSShlomo Pongratz 44ff8f06eeSShlomo Pongratz static int gicv3_post_load(void *opaque, int version_id) 45ff8f06eeSShlomo Pongratz { 46ff8f06eeSShlomo Pongratz GICv3State *s = (GICv3State *)opaque; 47ff8f06eeSShlomo Pongratz ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s); 48ff8f06eeSShlomo Pongratz 49ff8f06eeSShlomo Pongratz if (c->post_load) { 50ff8f06eeSShlomo Pongratz c->post_load(s); 51ff8f06eeSShlomo Pongratz } 52ff8f06eeSShlomo Pongratz return 0; 53ff8f06eeSShlomo Pongratz } 54ff8f06eeSShlomo Pongratz 554eb833b5SPeter Maydell static bool virt_state_needed(void *opaque) 564eb833b5SPeter Maydell { 574eb833b5SPeter Maydell GICv3CPUState *cs = opaque; 584eb833b5SPeter Maydell 594eb833b5SPeter Maydell return cs->num_list_regs != 0; 604eb833b5SPeter Maydell } 614eb833b5SPeter Maydell 624eb833b5SPeter Maydell static const VMStateDescription vmstate_gicv3_cpu_virt = { 634eb833b5SPeter Maydell .name = "arm_gicv3_cpu/virt", 644eb833b5SPeter Maydell .version_id = 1, 654eb833b5SPeter Maydell .minimum_version_id = 1, 664eb833b5SPeter Maydell .needed = virt_state_needed, 674eb833b5SPeter Maydell .fields = (VMStateField[]) { 684eb833b5SPeter Maydell VMSTATE_UINT64_2DARRAY(ich_apr, GICv3CPUState, 3, 4), 694eb833b5SPeter Maydell VMSTATE_UINT64(ich_hcr_el2, GICv3CPUState), 704eb833b5SPeter Maydell VMSTATE_UINT64_ARRAY(ich_lr_el2, GICv3CPUState, GICV3_LR_MAX), 714eb833b5SPeter Maydell VMSTATE_UINT64(ich_vmcr_el2, GICv3CPUState), 724eb833b5SPeter Maydell VMSTATE_END_OF_LIST() 734eb833b5SPeter Maydell } 744eb833b5SPeter Maydell }; 754eb833b5SPeter Maydell 766692aac4SVijaya Kumar K static int icc_sre_el1_reg_pre_load(void *opaque) 776692aac4SVijaya Kumar K { 786692aac4SVijaya Kumar K GICv3CPUState *cs = opaque; 796692aac4SVijaya Kumar K 806692aac4SVijaya Kumar K /* 816692aac4SVijaya Kumar K * If the sre_el1 subsection is not transferred this 826692aac4SVijaya Kumar K * means SRE_EL1 is 0x7 (which might not be the same as 836692aac4SVijaya Kumar K * our reset value). 846692aac4SVijaya Kumar K */ 856692aac4SVijaya Kumar K cs->icc_sre_el1 = 0x7; 866692aac4SVijaya Kumar K return 0; 876692aac4SVijaya Kumar K } 886692aac4SVijaya Kumar K 896692aac4SVijaya Kumar K static bool icc_sre_el1_reg_needed(void *opaque) 906692aac4SVijaya Kumar K { 916692aac4SVijaya Kumar K GICv3CPUState *cs = opaque; 926692aac4SVijaya Kumar K 936692aac4SVijaya Kumar K return cs->icc_sre_el1 != 7; 946692aac4SVijaya Kumar K } 956692aac4SVijaya Kumar K 966692aac4SVijaya Kumar K const VMStateDescription vmstate_gicv3_cpu_sre_el1 = { 976692aac4SVijaya Kumar K .name = "arm_gicv3_cpu/sre_el1", 986692aac4SVijaya Kumar K .version_id = 1, 996692aac4SVijaya Kumar K .minimum_version_id = 1, 1006692aac4SVijaya Kumar K .pre_load = icc_sre_el1_reg_pre_load, 1016692aac4SVijaya Kumar K .needed = icc_sre_el1_reg_needed, 1026692aac4SVijaya Kumar K .fields = (VMStateField[]) { 1036692aac4SVijaya Kumar K VMSTATE_UINT64(icc_sre_el1, GICv3CPUState), 1046692aac4SVijaya Kumar K VMSTATE_END_OF_LIST() 1056692aac4SVijaya Kumar K } 1066692aac4SVijaya Kumar K }; 1076692aac4SVijaya Kumar K 108757caeedSPavel Fedin static const VMStateDescription vmstate_gicv3_cpu = { 109757caeedSPavel Fedin .name = "arm_gicv3_cpu", 110757caeedSPavel Fedin .version_id = 1, 111757caeedSPavel Fedin .minimum_version_id = 1, 112757caeedSPavel Fedin .fields = (VMStateField[]) { 113757caeedSPavel Fedin VMSTATE_UINT32(level, GICv3CPUState), 114757caeedSPavel Fedin VMSTATE_UINT32(gicr_ctlr, GICv3CPUState), 115757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(gicr_statusr, GICv3CPUState, 2), 116757caeedSPavel Fedin VMSTATE_UINT32(gicr_waker, GICv3CPUState), 117757caeedSPavel Fedin VMSTATE_UINT64(gicr_propbaser, GICv3CPUState), 118757caeedSPavel Fedin VMSTATE_UINT64(gicr_pendbaser, GICv3CPUState), 119757caeedSPavel Fedin VMSTATE_UINT32(gicr_igroupr0, GICv3CPUState), 120757caeedSPavel Fedin VMSTATE_UINT32(gicr_ienabler0, GICv3CPUState), 121757caeedSPavel Fedin VMSTATE_UINT32(gicr_ipendr0, GICv3CPUState), 122757caeedSPavel Fedin VMSTATE_UINT32(gicr_iactiver0, GICv3CPUState), 123757caeedSPavel Fedin VMSTATE_UINT32(edge_trigger, GICv3CPUState), 124757caeedSPavel Fedin VMSTATE_UINT32(gicr_igrpmodr0, GICv3CPUState), 125757caeedSPavel Fedin VMSTATE_UINT32(gicr_nsacr, GICv3CPUState), 126757caeedSPavel Fedin VMSTATE_UINT8_ARRAY(gicr_ipriorityr, GICv3CPUState, GIC_INTERNAL), 127757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(icc_ctlr_el1, GICv3CPUState, 2), 128757caeedSPavel Fedin VMSTATE_UINT64(icc_pmr_el1, GICv3CPUState), 129757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(icc_bpr, GICv3CPUState, 3), 130757caeedSPavel Fedin VMSTATE_UINT64_2DARRAY(icc_apr, GICv3CPUState, 3, 4), 131757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(icc_igrpen, GICv3CPUState, 3), 132757caeedSPavel Fedin VMSTATE_UINT64(icc_ctlr_el3, GICv3CPUState), 133757caeedSPavel Fedin VMSTATE_END_OF_LIST() 1344eb833b5SPeter Maydell }, 1354eb833b5SPeter Maydell .subsections = (const VMStateDescription * []) { 1364eb833b5SPeter Maydell &vmstate_gicv3_cpu_virt, 1374eb833b5SPeter Maydell NULL 1386692aac4SVijaya Kumar K }, 1396692aac4SVijaya Kumar K .subsections = (const VMStateDescription * []) { 1406692aac4SVijaya Kumar K &vmstate_gicv3_cpu_sre_el1, 1416692aac4SVijaya Kumar K NULL 142757caeedSPavel Fedin } 143757caeedSPavel Fedin }; 144757caeedSPavel Fedin 145910e2048SShannon Zhao static int gicv3_gicd_no_migration_shift_bug_pre_load(void *opaque) 146910e2048SShannon Zhao { 147910e2048SShannon Zhao GICv3State *cs = opaque; 148910e2048SShannon Zhao 149910e2048SShannon Zhao /* 150910e2048SShannon Zhao * The gicd_no_migration_shift_bug flag is used for migration compatibility 151910e2048SShannon Zhao * for old version QEMU which may have the GICD bmp shift bug under KVM mode. 152910e2048SShannon Zhao * Strictly, what we want to know is whether the migration source is using 153910e2048SShannon Zhao * KVM. Since we don't have any way to determine that, we look at whether the 154910e2048SShannon Zhao * destination is using KVM; this is close enough because for the older QEMU 155910e2048SShannon Zhao * versions with this bug KVM -> TCG migration didn't work anyway. If the 156910e2048SShannon Zhao * source is a newer QEMU without this bug it will transmit the migration 157910e2048SShannon Zhao * subsection which sets the flag to true; otherwise it will remain set to 158910e2048SShannon Zhao * the value we select here. 159910e2048SShannon Zhao */ 160910e2048SShannon Zhao if (kvm_enabled()) { 161910e2048SShannon Zhao cs->gicd_no_migration_shift_bug = false; 162910e2048SShannon Zhao } 163910e2048SShannon Zhao 164910e2048SShannon Zhao return 0; 165910e2048SShannon Zhao } 166910e2048SShannon Zhao 167910e2048SShannon Zhao static int gicv3_gicd_no_migration_shift_bug_post_load(void *opaque, 168910e2048SShannon Zhao int version_id) 169910e2048SShannon Zhao { 170910e2048SShannon Zhao GICv3State *cs = opaque; 171910e2048SShannon Zhao 172910e2048SShannon Zhao if (cs->gicd_no_migration_shift_bug) { 173910e2048SShannon Zhao return 0; 174910e2048SShannon Zhao } 175910e2048SShannon Zhao 176910e2048SShannon Zhao /* Older versions of QEMU had a bug in the handling of state save/restore 177910e2048SShannon Zhao * to the KVM GICv3: they got the offset in the bitmap arrays wrong, 178910e2048SShannon Zhao * so that instead of the data for external interrupts 32 and up 179910e2048SShannon Zhao * starting at bit position 32 in the bitmap, it started at bit 180910e2048SShannon Zhao * position 64. If we're receiving data from a QEMU with that bug, 181910e2048SShannon Zhao * we must move the data down into the right place. 182910e2048SShannon Zhao */ 183910e2048SShannon Zhao memmove(cs->group, (uint8_t *)cs->group + GIC_INTERNAL / 8, 184910e2048SShannon Zhao sizeof(cs->group) - GIC_INTERNAL / 8); 185910e2048SShannon Zhao memmove(cs->grpmod, (uint8_t *)cs->grpmod + GIC_INTERNAL / 8, 186910e2048SShannon Zhao sizeof(cs->grpmod) - GIC_INTERNAL / 8); 187910e2048SShannon Zhao memmove(cs->enabled, (uint8_t *)cs->enabled + GIC_INTERNAL / 8, 188910e2048SShannon Zhao sizeof(cs->enabled) - GIC_INTERNAL / 8); 189910e2048SShannon Zhao memmove(cs->pending, (uint8_t *)cs->pending + GIC_INTERNAL / 8, 190910e2048SShannon Zhao sizeof(cs->pending) - GIC_INTERNAL / 8); 191910e2048SShannon Zhao memmove(cs->active, (uint8_t *)cs->active + GIC_INTERNAL / 8, 192910e2048SShannon Zhao sizeof(cs->active) - GIC_INTERNAL / 8); 193910e2048SShannon Zhao memmove(cs->edge_trigger, (uint8_t *)cs->edge_trigger + GIC_INTERNAL / 8, 194910e2048SShannon Zhao sizeof(cs->edge_trigger) - GIC_INTERNAL / 8); 195910e2048SShannon Zhao 196910e2048SShannon Zhao /* 197910e2048SShannon Zhao * While this new version QEMU doesn't have this kind of bug as we fix it, 198910e2048SShannon Zhao * so it needs to set the flag to true to indicate that and it's necessary 199910e2048SShannon Zhao * for next migration to work from this new version QEMU. 200910e2048SShannon Zhao */ 201910e2048SShannon Zhao cs->gicd_no_migration_shift_bug = true; 202910e2048SShannon Zhao 203910e2048SShannon Zhao return 0; 204910e2048SShannon Zhao } 205910e2048SShannon Zhao 206*78e9ddd7SPeter Maydell static bool needed_always(void *opaque) 207*78e9ddd7SPeter Maydell { 208*78e9ddd7SPeter Maydell return true; 209*78e9ddd7SPeter Maydell } 210*78e9ddd7SPeter Maydell 211910e2048SShannon Zhao const VMStateDescription vmstate_gicv3_gicd_no_migration_shift_bug = { 212910e2048SShannon Zhao .name = "arm_gicv3/gicd_no_migration_shift_bug", 213910e2048SShannon Zhao .version_id = 1, 214910e2048SShannon Zhao .minimum_version_id = 1, 215*78e9ddd7SPeter Maydell .needed = needed_always, 216910e2048SShannon Zhao .pre_load = gicv3_gicd_no_migration_shift_bug_pre_load, 217910e2048SShannon Zhao .post_load = gicv3_gicd_no_migration_shift_bug_post_load, 218910e2048SShannon Zhao .fields = (VMStateField[]) { 219910e2048SShannon Zhao VMSTATE_BOOL(gicd_no_migration_shift_bug, GICv3State), 220910e2048SShannon Zhao VMSTATE_END_OF_LIST() 221910e2048SShannon Zhao } 222910e2048SShannon Zhao }; 223910e2048SShannon Zhao 224ff8f06eeSShlomo Pongratz static const VMStateDescription vmstate_gicv3 = { 225ff8f06eeSShlomo Pongratz .name = "arm_gicv3", 226757caeedSPavel Fedin .version_id = 1, 227757caeedSPavel Fedin .minimum_version_id = 1, 228ff8f06eeSShlomo Pongratz .pre_save = gicv3_pre_save, 229ff8f06eeSShlomo Pongratz .post_load = gicv3_post_load, 230252a7a6aSEric Auger .priority = MIG_PRI_GICV3, 231757caeedSPavel Fedin .fields = (VMStateField[]) { 232757caeedSPavel Fedin VMSTATE_UINT32(gicd_ctlr, GICv3State), 233757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(gicd_statusr, GICv3State, 2), 234757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(group, GICv3State, GICV3_BMP_SIZE), 235757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(grpmod, GICv3State, GICV3_BMP_SIZE), 236757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(enabled, GICv3State, GICV3_BMP_SIZE), 237757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(pending, GICv3State, GICV3_BMP_SIZE), 238757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(active, GICv3State, GICV3_BMP_SIZE), 239757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(level, GICv3State, GICV3_BMP_SIZE), 240757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(edge_trigger, GICv3State, GICV3_BMP_SIZE), 241757caeedSPavel Fedin VMSTATE_UINT8_ARRAY(gicd_ipriority, GICv3State, GICV3_MAXIRQ), 242757caeedSPavel Fedin VMSTATE_UINT64_ARRAY(gicd_irouter, GICv3State, GICV3_MAXIRQ), 243757caeedSPavel Fedin VMSTATE_UINT32_ARRAY(gicd_nsacr, GICv3State, 244757caeedSPavel Fedin DIV_ROUND_UP(GICV3_MAXIRQ, 16)), 245757caeedSPavel Fedin VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, GICv3State, num_cpu, 246757caeedSPavel Fedin vmstate_gicv3_cpu, GICv3CPUState), 247757caeedSPavel Fedin VMSTATE_END_OF_LIST() 248910e2048SShannon Zhao }, 249910e2048SShannon Zhao .subsections = (const VMStateDescription * []) { 250910e2048SShannon Zhao &vmstate_gicv3_gicd_no_migration_shift_bug, 251910e2048SShannon Zhao NULL 252757caeedSPavel Fedin } 253ff8f06eeSShlomo Pongratz }; 254ff8f06eeSShlomo Pongratz 255ff8f06eeSShlomo Pongratz void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler, 2561e575b66SEric Auger const MemoryRegionOps *ops, Error **errp) 257ff8f06eeSShlomo Pongratz { 258ff8f06eeSShlomo Pongratz SysBusDevice *sbd = SYS_BUS_DEVICE(s); 2591e575b66SEric Auger int rdist_capacity = 0; 260ff8f06eeSShlomo Pongratz int i; 261ff8f06eeSShlomo Pongratz 2621e575b66SEric Auger for (i = 0; i < s->nb_redist_regions; i++) { 2631e575b66SEric Auger rdist_capacity += s->redist_region_count[i]; 2641e575b66SEric Auger } 2651e575b66SEric Auger if (rdist_capacity < s->num_cpu) { 2661e575b66SEric Auger error_setg(errp, "Capacity of the redist regions(%d) " 2671e575b66SEric Auger "is less than number of vcpus(%d)", 2681e575b66SEric Auger rdist_capacity, s->num_cpu); 2691e575b66SEric Auger return; 2701e575b66SEric Auger } 2711e575b66SEric Auger 272ff8f06eeSShlomo Pongratz /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU. 273ff8f06eeSShlomo Pongratz * GPIO array layout is thus: 274ff8f06eeSShlomo Pongratz * [0..N-1] spi 275ff8f06eeSShlomo Pongratz * [N..N+31] PPIs for CPU 0 276ff8f06eeSShlomo Pongratz * [N+32..N+63] PPIs for CPU 1 277ff8f06eeSShlomo Pongratz * ... 278ff8f06eeSShlomo Pongratz */ 279ff8f06eeSShlomo Pongratz i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu; 280ff8f06eeSShlomo Pongratz qdev_init_gpio_in(DEVICE(s), handler, i); 281ff8f06eeSShlomo Pongratz 282ff8f06eeSShlomo Pongratz for (i = 0; i < s->num_cpu; i++) { 2833faf2b0cSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_irq); 284ff8f06eeSShlomo Pongratz } 285ff8f06eeSShlomo Pongratz for (i = 0; i < s->num_cpu; i++) { 2863faf2b0cSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_fiq); 287ff8f06eeSShlomo Pongratz } 288b53db42bSPeter Maydell for (i = 0; i < s->num_cpu; i++) { 289b53db42bSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_virq); 290b53db42bSPeter Maydell } 291b53db42bSPeter Maydell for (i = 0; i < s->num_cpu; i++) { 292b53db42bSPeter Maydell sysbus_init_irq(sbd, &s->cpu[i].parent_vfiq); 293b53db42bSPeter Maydell } 294ff8f06eeSShlomo Pongratz 295ff8f06eeSShlomo Pongratz memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s, 296ff8f06eeSShlomo Pongratz "gicv3_dist", 0x10000); 297ff8f06eeSShlomo Pongratz sysbus_init_mmio(sbd, &s->iomem_dist); 2981e575b66SEric Auger 2991e575b66SEric Auger s->iomem_redist = g_new0(MemoryRegion, s->nb_redist_regions); 3001e575b66SEric Auger for (i = 0; i < s->nb_redist_regions; i++) { 3011e575b66SEric Auger char *name = g_strdup_printf("gicv3_redist_region[%d]", i); 3021e575b66SEric Auger 3031e575b66SEric Auger memory_region_init_io(&s->iomem_redist[i], OBJECT(s), 3041e575b66SEric Auger ops ? &ops[1] : NULL, s, name, 3051e575b66SEric Auger s->redist_region_count[i] * GICV3_REDIST_SIZE); 3061e575b66SEric Auger sysbus_init_mmio(sbd, &s->iomem_redist[i]); 3071e575b66SEric Auger g_free(name); 3081e575b66SEric Auger } 309ff8f06eeSShlomo Pongratz } 310ff8f06eeSShlomo Pongratz 311ff8f06eeSShlomo Pongratz static void arm_gicv3_common_realize(DeviceState *dev, Error **errp) 312ff8f06eeSShlomo Pongratz { 313ff8f06eeSShlomo Pongratz GICv3State *s = ARM_GICV3_COMMON(dev); 31407e2034dSPavel Fedin int i; 315ff8f06eeSShlomo Pongratz 316ff8f06eeSShlomo Pongratz /* revision property is actually reserved and currently used only in order 317ff8f06eeSShlomo Pongratz * to keep the interface compatible with GICv2 code, avoiding extra 318ff8f06eeSShlomo Pongratz * conditions. However, in future it could be used, for example, if we 319ff8f06eeSShlomo Pongratz * implement GICv4. 320ff8f06eeSShlomo Pongratz */ 321ff8f06eeSShlomo Pongratz if (s->revision != 3) { 322ff8f06eeSShlomo Pongratz error_setg(errp, "unsupported GIC revision %d", s->revision); 323ff8f06eeSShlomo Pongratz return; 324ff8f06eeSShlomo Pongratz } 32507e2034dSPavel Fedin 32607e2034dSPavel Fedin if (s->num_irq > GICV3_MAXIRQ) { 32707e2034dSPavel Fedin error_setg(errp, 32807e2034dSPavel Fedin "requested %u interrupt lines exceeds GIC maximum %d", 32907e2034dSPavel Fedin s->num_irq, GICV3_MAXIRQ); 33007e2034dSPavel Fedin return; 33107e2034dSPavel Fedin } 33207e2034dSPavel Fedin if (s->num_irq < GIC_INTERNAL) { 33307e2034dSPavel Fedin error_setg(errp, 33407e2034dSPavel Fedin "requested %u interrupt lines is below GIC minimum %d", 33507e2034dSPavel Fedin s->num_irq, GIC_INTERNAL); 33607e2034dSPavel Fedin return; 33707e2034dSPavel Fedin } 33807e2034dSPavel Fedin 33907e2034dSPavel Fedin /* ITLinesNumber is represented as (N / 32) - 1, so this is an 34007e2034dSPavel Fedin * implementation imposed restriction, not an architectural one, 34107e2034dSPavel Fedin * so we don't have to deal with bitfields where only some of the 34207e2034dSPavel Fedin * bits in a 32-bit word should be valid. 34307e2034dSPavel Fedin */ 34407e2034dSPavel Fedin if (s->num_irq % 32) { 34507e2034dSPavel Fedin error_setg(errp, 34607e2034dSPavel Fedin "%d interrupt lines unsupported: not divisible by 32", 34707e2034dSPavel Fedin s->num_irq); 34807e2034dSPavel Fedin return; 34907e2034dSPavel Fedin } 35007e2034dSPavel Fedin 35107e2034dSPavel Fedin s->cpu = g_new0(GICv3CPUState, s->num_cpu); 35207e2034dSPavel Fedin 35307e2034dSPavel Fedin for (i = 0; i < s->num_cpu; i++) { 35407e2034dSPavel Fedin CPUState *cpu = qemu_get_cpu(i); 35507e2034dSPavel Fedin uint64_t cpu_affid; 35607e2034dSPavel Fedin int last; 35707e2034dSPavel Fedin 35807e2034dSPavel Fedin s->cpu[i].cpu = cpu; 35907e2034dSPavel Fedin s->cpu[i].gic = s; 360d3a3e529SVijaya Kumar K /* Store GICv3CPUState in CPUARMState gicv3state pointer */ 361d3a3e529SVijaya Kumar K gicv3_set_gicv3state(cpu, &s->cpu[i]); 36207e2034dSPavel Fedin 36307e2034dSPavel Fedin /* Pre-construct the GICR_TYPER: 36407e2034dSPavel Fedin * For our implementation: 36507e2034dSPavel Fedin * Top 32 bits are the affinity value of the associated CPU 36607e2034dSPavel Fedin * CommonLPIAff == 01 (redistributors with same Aff3 share LPI table) 36707e2034dSPavel Fedin * Processor_Number == CPU index starting from 0 36807e2034dSPavel Fedin * DPGS == 0 (GICR_CTLR.DPG* not supported) 36907e2034dSPavel Fedin * Last == 1 if this is the last redistributor in a series of 37007e2034dSPavel Fedin * contiguous redistributor pages 37107e2034dSPavel Fedin * DirectLPI == 0 (direct injection of LPIs not supported) 37207e2034dSPavel Fedin * VLPIS == 0 (virtual LPIs not supported) 37307e2034dSPavel Fedin * PLPIS == 0 (physical LPIs not supported) 37407e2034dSPavel Fedin */ 37577a7a367SMarc-André Lureau cpu_affid = object_property_get_uint(OBJECT(cpu), "mp-affinity", NULL); 37607e2034dSPavel Fedin last = (i == s->num_cpu - 1); 37707e2034dSPavel Fedin 37807e2034dSPavel Fedin /* The CPU mp-affinity property is in MPIDR register format; squash 37907e2034dSPavel Fedin * the affinity bytes into 32 bits as the GICR_TYPER has them. 38007e2034dSPavel Fedin */ 38192204403SAndrew Jones cpu_affid = ((cpu_affid & 0xFF00000000ULL) >> 8) | 38292204403SAndrew Jones (cpu_affid & 0xFFFFFF); 38307e2034dSPavel Fedin s->cpu[i].gicr_typer = (cpu_affid << 32) | 38407e2034dSPavel Fedin (1 << 24) | 38507e2034dSPavel Fedin (i << 8) | 38607e2034dSPavel Fedin (last << 4); 38707e2034dSPavel Fedin } 388ff8f06eeSShlomo Pongratz } 389ff8f06eeSShlomo Pongratz 3901e575b66SEric Auger static void arm_gicv3_finalize(Object *obj) 3911e575b66SEric Auger { 3921e575b66SEric Auger GICv3State *s = ARM_GICV3_COMMON(obj); 3931e575b66SEric Auger 3941e575b66SEric Auger g_free(s->redist_region_count); 3951e575b66SEric Auger } 3961e575b66SEric Auger 397ff8f06eeSShlomo Pongratz static void arm_gicv3_common_reset(DeviceState *dev) 398ff8f06eeSShlomo Pongratz { 39907e2034dSPavel Fedin GICv3State *s = ARM_GICV3_COMMON(dev); 40007e2034dSPavel Fedin int i; 40107e2034dSPavel Fedin 40207e2034dSPavel Fedin for (i = 0; i < s->num_cpu; i++) { 40307e2034dSPavel Fedin GICv3CPUState *cs = &s->cpu[i]; 40407e2034dSPavel Fedin 40507e2034dSPavel Fedin cs->level = 0; 40607e2034dSPavel Fedin cs->gicr_ctlr = 0; 40707e2034dSPavel Fedin cs->gicr_statusr[GICV3_S] = 0; 40807e2034dSPavel Fedin cs->gicr_statusr[GICV3_NS] = 0; 40907e2034dSPavel Fedin cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep; 41007e2034dSPavel Fedin cs->gicr_propbaser = 0; 41107e2034dSPavel Fedin cs->gicr_pendbaser = 0; 41207e2034dSPavel Fedin /* If we're resetting a TZ-aware GIC as if secure firmware 41307e2034dSPavel Fedin * had set it up ready to start a kernel in non-secure, we 41407e2034dSPavel Fedin * need to set interrupts to group 1 so the kernel can use them. 41507e2034dSPavel Fedin * Otherwise they reset to group 0 like the hardware. 41607e2034dSPavel Fedin */ 41707e2034dSPavel Fedin if (s->irq_reset_nonsecure) { 41807e2034dSPavel Fedin cs->gicr_igroupr0 = 0xffffffff; 41907e2034dSPavel Fedin } else { 42007e2034dSPavel Fedin cs->gicr_igroupr0 = 0; 42107e2034dSPavel Fedin } 42207e2034dSPavel Fedin 42307e2034dSPavel Fedin cs->gicr_ienabler0 = 0; 42407e2034dSPavel Fedin cs->gicr_ipendr0 = 0; 42507e2034dSPavel Fedin cs->gicr_iactiver0 = 0; 42607e2034dSPavel Fedin cs->edge_trigger = 0xffff; 42707e2034dSPavel Fedin cs->gicr_igrpmodr0 = 0; 42807e2034dSPavel Fedin cs->gicr_nsacr = 0; 42907e2034dSPavel Fedin memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr)); 43007e2034dSPavel Fedin 431ce187c3cSPeter Maydell cs->hppi.prio = 0xff; 432ce187c3cSPeter Maydell 43307e2034dSPavel Fedin /* State in the CPU interface must *not* be reset here, because it 43407e2034dSPavel Fedin * is part of the CPU's reset domain, not the GIC device's. 43507e2034dSPavel Fedin */ 43607e2034dSPavel Fedin } 43707e2034dSPavel Fedin 43807e2034dSPavel Fedin /* For our implementation affinity routing is always enabled */ 43907e2034dSPavel Fedin if (s->security_extn) { 44007e2034dSPavel Fedin s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS; 44107e2034dSPavel Fedin } else { 44207e2034dSPavel Fedin s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE; 44307e2034dSPavel Fedin } 44407e2034dSPavel Fedin 44507e2034dSPavel Fedin s->gicd_statusr[GICV3_S] = 0; 44607e2034dSPavel Fedin s->gicd_statusr[GICV3_NS] = 0; 44707e2034dSPavel Fedin 44807e2034dSPavel Fedin memset(s->group, 0, sizeof(s->group)); 44907e2034dSPavel Fedin memset(s->grpmod, 0, sizeof(s->grpmod)); 45007e2034dSPavel Fedin memset(s->enabled, 0, sizeof(s->enabled)); 45107e2034dSPavel Fedin memset(s->pending, 0, sizeof(s->pending)); 45207e2034dSPavel Fedin memset(s->active, 0, sizeof(s->active)); 45307e2034dSPavel Fedin memset(s->level, 0, sizeof(s->level)); 45407e2034dSPavel Fedin memset(s->edge_trigger, 0, sizeof(s->edge_trigger)); 45507e2034dSPavel Fedin memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority)); 45607e2034dSPavel Fedin memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter)); 45707e2034dSPavel Fedin memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr)); 458ce187c3cSPeter Maydell /* GICD_IROUTER are UNKNOWN at reset so in theory the guest must 459ce187c3cSPeter Maydell * write these to get sane behaviour and we need not populate the 460ce187c3cSPeter Maydell * pointer cache here; however having the cache be different for 461ce187c3cSPeter Maydell * "happened to be 0 from reset" and "guest wrote 0" would be 462ce187c3cSPeter Maydell * too confusing. 463ce187c3cSPeter Maydell */ 464ce187c3cSPeter Maydell gicv3_cache_all_target_cpustates(s); 46507e2034dSPavel Fedin 46607e2034dSPavel Fedin if (s->irq_reset_nonsecure) { 46707e2034dSPavel Fedin /* If we're resetting a TZ-aware GIC as if secure firmware 46807e2034dSPavel Fedin * had set it up ready to start a kernel in non-secure, we 46907e2034dSPavel Fedin * need to set interrupts to group 1 so the kernel can use them. 47007e2034dSPavel Fedin * Otherwise they reset to group 0 like the hardware. 47107e2034dSPavel Fedin */ 47207e2034dSPavel Fedin for (i = GIC_INTERNAL; i < s->num_irq; i++) { 47307e2034dSPavel Fedin gicv3_gicd_group_set(s, i); 47407e2034dSPavel Fedin } 47507e2034dSPavel Fedin } 476910e2048SShannon Zhao s->gicd_no_migration_shift_bug = true; 47707e2034dSPavel Fedin } 47807e2034dSPavel Fedin 47907e2034dSPavel Fedin static void arm_gic_common_linux_init(ARMLinuxBootIf *obj, 48007e2034dSPavel Fedin bool secure_boot) 48107e2034dSPavel Fedin { 48207e2034dSPavel Fedin GICv3State *s = ARM_GICV3_COMMON(obj); 48307e2034dSPavel Fedin 48407e2034dSPavel Fedin if (s->security_extn && !secure_boot) { 48507e2034dSPavel Fedin /* We're directly booting a kernel into NonSecure. If this GIC 48607e2034dSPavel Fedin * implements the security extensions then we must configure it 48707e2034dSPavel Fedin * to have all the interrupts be NonSecure (this is a job that 48807e2034dSPavel Fedin * is done by the Secure boot firmware in real hardware, and in 48907e2034dSPavel Fedin * this mode QEMU is acting as a minimalist firmware-and-bootloader 49007e2034dSPavel Fedin * equivalent). 49107e2034dSPavel Fedin */ 49207e2034dSPavel Fedin s->irq_reset_nonsecure = true; 49307e2034dSPavel Fedin } 494ff8f06eeSShlomo Pongratz } 495ff8f06eeSShlomo Pongratz 496ff8f06eeSShlomo Pongratz static Property arm_gicv3_common_properties[] = { 497ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1), 498ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32), 499ff8f06eeSShlomo Pongratz DEFINE_PROP_UINT32("revision", GICv3State, revision, 3), 500ff8f06eeSShlomo Pongratz DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0), 5011e575b66SEric Auger DEFINE_PROP_ARRAY("redist-region-count", GICv3State, nb_redist_regions, 5021e575b66SEric Auger redist_region_count, qdev_prop_uint32, uint32_t), 503ff8f06eeSShlomo Pongratz DEFINE_PROP_END_OF_LIST(), 504ff8f06eeSShlomo Pongratz }; 505ff8f06eeSShlomo Pongratz 506ff8f06eeSShlomo Pongratz static void arm_gicv3_common_class_init(ObjectClass *klass, void *data) 507ff8f06eeSShlomo Pongratz { 508ff8f06eeSShlomo Pongratz DeviceClass *dc = DEVICE_CLASS(klass); 50907e2034dSPavel Fedin ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass); 510ff8f06eeSShlomo Pongratz 511ff8f06eeSShlomo Pongratz dc->reset = arm_gicv3_common_reset; 512ff8f06eeSShlomo Pongratz dc->realize = arm_gicv3_common_realize; 513ff8f06eeSShlomo Pongratz dc->props = arm_gicv3_common_properties; 514ff8f06eeSShlomo Pongratz dc->vmsd = &vmstate_gicv3; 51507e2034dSPavel Fedin albifc->arm_linux_init = arm_gic_common_linux_init; 516ff8f06eeSShlomo Pongratz } 517ff8f06eeSShlomo Pongratz 518ff8f06eeSShlomo Pongratz static const TypeInfo arm_gicv3_common_type = { 519ff8f06eeSShlomo Pongratz .name = TYPE_ARM_GICV3_COMMON, 520ff8f06eeSShlomo Pongratz .parent = TYPE_SYS_BUS_DEVICE, 521ff8f06eeSShlomo Pongratz .instance_size = sizeof(GICv3State), 522ff8f06eeSShlomo Pongratz .class_size = sizeof(ARMGICv3CommonClass), 523ff8f06eeSShlomo Pongratz .class_init = arm_gicv3_common_class_init, 5241e575b66SEric Auger .instance_finalize = arm_gicv3_finalize, 525ff8f06eeSShlomo Pongratz .abstract = true, 52607e2034dSPavel Fedin .interfaces = (InterfaceInfo []) { 52707e2034dSPavel Fedin { TYPE_ARM_LINUX_BOOT_IF }, 52807e2034dSPavel Fedin { }, 52907e2034dSPavel Fedin }, 530ff8f06eeSShlomo Pongratz }; 531ff8f06eeSShlomo Pongratz 532ff8f06eeSShlomo Pongratz static void register_types(void) 533ff8f06eeSShlomo Pongratz { 534ff8f06eeSShlomo Pongratz type_register_static(&arm_gicv3_common_type); 535ff8f06eeSShlomo Pongratz } 536ff8f06eeSShlomo Pongratz 537ff8f06eeSShlomo Pongratz type_init(register_types) 538