164580903SHongbo Zhang /* 264580903SHongbo Zhang * ARM SBSA Reference Platform emulation 364580903SHongbo Zhang * 464580903SHongbo Zhang * Copyright (c) 2018 Linaro Limited 564580903SHongbo Zhang * Written by Hongbo Zhang <hongbo.zhang@linaro.org> 664580903SHongbo Zhang * 764580903SHongbo Zhang * This program is free software; you can redistribute it and/or modify it 864580903SHongbo Zhang * under the terms and conditions of the GNU General Public License, 964580903SHongbo Zhang * version 2 or later, as published by the Free Software Foundation. 1064580903SHongbo Zhang * 1164580903SHongbo Zhang * This program is distributed in the hope it will be useful, but WITHOUT 1264580903SHongbo Zhang * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1364580903SHongbo Zhang * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1464580903SHongbo Zhang * more details. 1564580903SHongbo Zhang * 1664580903SHongbo Zhang * You should have received a copy of the GNU General Public License along with 1764580903SHongbo Zhang * this program. If not, see <http://www.gnu.org/licenses/>. 1864580903SHongbo Zhang */ 1964580903SHongbo Zhang 2064580903SHongbo Zhang #include "qemu/osdep.h" 212c65db5eSPaolo Bonzini #include "qemu/datadir.h" 2264580903SHongbo Zhang #include "qapi/error.h" 2364580903SHongbo Zhang #include "qemu/error-report.h" 2464580903SHongbo Zhang #include "qemu/units.h" 25e9fdf453SHongbo Zhang #include "sysemu/device_tree.h" 2694522562SPhilippe Mathieu-Daudé #include "sysemu/kvm.h" 2764580903SHongbo Zhang #include "sysemu/numa.h" 2854d31236SMarkus Armbruster #include "sysemu/runstate.h" 2964580903SHongbo Zhang #include "sysemu/sysemu.h" 3064580903SHongbo Zhang #include "exec/hwaddr.h" 3164580903SHongbo Zhang #include "kvm_arm.h" 3264580903SHongbo Zhang #include "hw/arm/boot.h" 330c08d4f3SMarcin Juszkiewicz #include "hw/arm/fdt.h" 34a431ab0eSRichard Henderson #include "hw/arm/smmuv3.h" 35e9fdf453SHongbo Zhang #include "hw/block/flash.h" 3664580903SHongbo Zhang #include "hw/boards.h" 37e9fdf453SHongbo Zhang #include "hw/ide/internal.h" 38e9fdf453SHongbo Zhang #include "hw/ide/ahci_internal.h" 3964580903SHongbo Zhang #include "hw/intc/arm_gicv3_common.h" 400c40daf0SPhilippe Mathieu-Daudé #include "hw/intc/arm_gicv3_its_common.h" 41e9fdf453SHongbo Zhang #include "hw/loader.h" 42e9fdf453SHongbo Zhang #include "hw/pci-host/gpex.h" 43a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 44e9fdf453SHongbo Zhang #include "hw/usb.h" 4562c2b876SYuquan Wang #include "hw/usb/xhci.h" 46d8f6d15fSGavin Shan #include "hw/char/pl011.h" 47baabe7d0SShashi Mallela #include "hw/watchdog/sbsa_gwdt.h" 48e9fdf453SHongbo Zhang #include "net/net.h" 49db1015e9SEduardo Habkost #include "qom/object.h" 5064580903SHongbo Zhang 5164580903SHongbo Zhang #define RAMLIMIT_GB 8192 5264580903SHongbo Zhang #define RAMLIMIT_BYTES (RAMLIMIT_GB * GiB) 5364580903SHongbo Zhang 54e9fdf453SHongbo Zhang #define NUM_IRQS 256 55e9fdf453SHongbo Zhang #define NUM_SMMU_IRQS 4 56e9fdf453SHongbo Zhang #define NUM_SATA_PORTS 6 57e9fdf453SHongbo Zhang 58e9fdf453SHongbo Zhang #define VIRTUAL_PMU_IRQ 7 59e9fdf453SHongbo Zhang #define ARCH_GIC_MAINT_IRQ 9 60e9fdf453SHongbo Zhang #define ARCH_TIMER_VIRT_IRQ 11 61e9fdf453SHongbo Zhang #define ARCH_TIMER_S_EL1_IRQ 13 62e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL1_IRQ 14 63e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL2_IRQ 10 64e9fdf453SHongbo Zhang 6564580903SHongbo Zhang enum { 6664580903SHongbo Zhang SBSA_FLASH, 6764580903SHongbo Zhang SBSA_MEM, 6864580903SHongbo Zhang SBSA_CPUPERIPHS, 6964580903SHongbo Zhang SBSA_GIC_DIST, 7064580903SHongbo Zhang SBSA_GIC_REDIST, 719fe2b4a2SShashi Mallela SBSA_GIC_ITS, 723f462bf0SGraeme Gregory SBSA_SECURE_EC, 7380d60a6dSEduardo Habkost SBSA_GWDT_WS0, 74baabe7d0SShashi Mallela SBSA_GWDT_REFRESH, 75baabe7d0SShashi Mallela SBSA_GWDT_CONTROL, 7664580903SHongbo Zhang SBSA_SMMU, 7764580903SHongbo Zhang SBSA_UART, 7864580903SHongbo Zhang SBSA_RTC, 7964580903SHongbo Zhang SBSA_PCIE, 8064580903SHongbo Zhang SBSA_PCIE_MMIO, 8164580903SHongbo Zhang SBSA_PCIE_MMIO_HIGH, 8264580903SHongbo Zhang SBSA_PCIE_PIO, 8364580903SHongbo Zhang SBSA_PCIE_ECAM, 8464580903SHongbo Zhang SBSA_GPIO, 8564580903SHongbo Zhang SBSA_SECURE_UART, 8664580903SHongbo Zhang SBSA_SECURE_UART_MM, 8764580903SHongbo Zhang SBSA_SECURE_MEM, 8864580903SHongbo Zhang SBSA_AHCI, 8962c2b876SYuquan Wang SBSA_XHCI, 9064580903SHongbo Zhang }; 9164580903SHongbo Zhang 92db1015e9SEduardo Habkost struct SBSAMachineState { 9364580903SHongbo Zhang MachineState parent; 9464580903SHongbo Zhang struct arm_boot_info bootinfo; 9564580903SHongbo Zhang int smp_cpus; 9664580903SHongbo Zhang void *fdt; 9764580903SHongbo Zhang int fdt_size; 9864580903SHongbo Zhang int psci_conduit; 9948ba18e6SPhilippe Mathieu-Daudé DeviceState *gic; 100e9fdf453SHongbo Zhang PFlashCFI01 *flash[2]; 101db1015e9SEduardo Habkost }; 10264580903SHongbo Zhang 10364580903SHongbo Zhang #define TYPE_SBSA_MACHINE MACHINE_TYPE_NAME("sbsa-ref") 1048063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(SBSAMachineState, SBSA_MACHINE) 10564580903SHongbo Zhang 10664580903SHongbo Zhang static const MemMapEntry sbsa_ref_memmap[] = { 10764580903SHongbo Zhang /* 512M boot ROM */ 10864580903SHongbo Zhang [SBSA_FLASH] = { 0, 0x20000000 }, 10964580903SHongbo Zhang /* 512M secure memory */ 11064580903SHongbo Zhang [SBSA_SECURE_MEM] = { 0x20000000, 0x20000000 }, 11164580903SHongbo Zhang /* Space reserved for CPU peripheral devices */ 11264580903SHongbo Zhang [SBSA_CPUPERIPHS] = { 0x40000000, 0x00040000 }, 11364580903SHongbo Zhang [SBSA_GIC_DIST] = { 0x40060000, 0x00010000 }, 11464580903SHongbo Zhang [SBSA_GIC_REDIST] = { 0x40080000, 0x04000000 }, 1159fe2b4a2SShashi Mallela [SBSA_GIC_ITS] = { 0x44081000, 0x00020000 }, 1163f462bf0SGraeme Gregory [SBSA_SECURE_EC] = { 0x50000000, 0x00001000 }, 117baabe7d0SShashi Mallela [SBSA_GWDT_REFRESH] = { 0x50010000, 0x00001000 }, 118baabe7d0SShashi Mallela [SBSA_GWDT_CONTROL] = { 0x50011000, 0x00001000 }, 11964580903SHongbo Zhang [SBSA_UART] = { 0x60000000, 0x00001000 }, 12064580903SHongbo Zhang [SBSA_RTC] = { 0x60010000, 0x00001000 }, 12164580903SHongbo Zhang [SBSA_GPIO] = { 0x60020000, 0x00001000 }, 12264580903SHongbo Zhang [SBSA_SECURE_UART] = { 0x60030000, 0x00001000 }, 12364580903SHongbo Zhang [SBSA_SECURE_UART_MM] = { 0x60040000, 0x00001000 }, 12464580903SHongbo Zhang [SBSA_SMMU] = { 0x60050000, 0x00020000 }, 12564580903SHongbo Zhang /* Space here reserved for more SMMUs */ 12664580903SHongbo Zhang [SBSA_AHCI] = { 0x60100000, 0x00010000 }, 12762c2b876SYuquan Wang [SBSA_XHCI] = { 0x60110000, 0x00010000 }, 12864580903SHongbo Zhang /* Space here reserved for other devices */ 12964580903SHongbo Zhang [SBSA_PCIE_PIO] = { 0x7fff0000, 0x00010000 }, 13064580903SHongbo Zhang /* 32-bit address PCIE MMIO space */ 13164580903SHongbo Zhang [SBSA_PCIE_MMIO] = { 0x80000000, 0x70000000 }, 13264580903SHongbo Zhang /* 256M PCIE ECAM space */ 13364580903SHongbo Zhang [SBSA_PCIE_ECAM] = { 0xf0000000, 0x10000000 }, 13464580903SHongbo Zhang /* ~1TB PCIE MMIO space (4GB to 1024GB boundary) */ 13564580903SHongbo Zhang [SBSA_PCIE_MMIO_HIGH] = { 0x100000000ULL, 0xFF00000000ULL }, 13664580903SHongbo Zhang [SBSA_MEM] = { 0x10000000000ULL, RAMLIMIT_BYTES }, 13764580903SHongbo Zhang }; 13864580903SHongbo Zhang 139e9fdf453SHongbo Zhang static const int sbsa_ref_irqmap[] = { 140e9fdf453SHongbo Zhang [SBSA_UART] = 1, 141e9fdf453SHongbo Zhang [SBSA_RTC] = 2, 142e9fdf453SHongbo Zhang [SBSA_PCIE] = 3, /* ... to 6 */ 143e9fdf453SHongbo Zhang [SBSA_GPIO] = 7, 144e9fdf453SHongbo Zhang [SBSA_SECURE_UART] = 8, 145e9fdf453SHongbo Zhang [SBSA_SECURE_UART_MM] = 9, 146e9fdf453SHongbo Zhang [SBSA_AHCI] = 10, 14762c2b876SYuquan Wang [SBSA_XHCI] = 11, 14804788fd5SGraeme Gregory [SBSA_SMMU] = 12, /* ... to 15 */ 14980d60a6dSEduardo Habkost [SBSA_GWDT_WS0] = 16, 150e9fdf453SHongbo Zhang }; 151e9fdf453SHongbo Zhang 152ce3adffcSMarcin Juszkiewicz static const char * const valid_cpus[] = { 153ce3adffcSMarcin Juszkiewicz ARM_CPU_TYPE_NAME("cortex-a57"), 154ce3adffcSMarcin Juszkiewicz ARM_CPU_TYPE_NAME("cortex-a72"), 1555db6de80SRichard Henderson ARM_CPU_TYPE_NAME("neoverse-n1"), 156c74138c6SPeter Maydell ARM_CPU_TYPE_NAME("neoverse-v1"), 157cecc0962SMarcin Juszkiewicz ARM_CPU_TYPE_NAME("max"), 158ce3adffcSMarcin Juszkiewicz }; 159ce3adffcSMarcin Juszkiewicz 160ce3adffcSMarcin Juszkiewicz static bool cpu_type_valid(const char *cpu) 161ce3adffcSMarcin Juszkiewicz { 162ce3adffcSMarcin Juszkiewicz int i; 163ce3adffcSMarcin Juszkiewicz 164ce3adffcSMarcin Juszkiewicz for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) { 165ce3adffcSMarcin Juszkiewicz if (strcmp(cpu, valid_cpus[i]) == 0) { 166ce3adffcSMarcin Juszkiewicz return true; 167ce3adffcSMarcin Juszkiewicz } 168ce3adffcSMarcin Juszkiewicz } 169ce3adffcSMarcin Juszkiewicz return false; 170ce3adffcSMarcin Juszkiewicz } 171ce3adffcSMarcin Juszkiewicz 172999f6ebdSLeif Lindholm static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx) 173999f6ebdSLeif Lindholm { 174999f6ebdSLeif Lindholm uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER; 175999f6ebdSLeif Lindholm return arm_cpu_mp_affinity(idx, clustersz); 176999f6ebdSLeif Lindholm } 177999f6ebdSLeif Lindholm 1780c08d4f3SMarcin Juszkiewicz static void sbsa_fdt_add_gic_node(SBSAMachineState *sms) 1790c08d4f3SMarcin Juszkiewicz { 1800c08d4f3SMarcin Juszkiewicz char *nodename; 1810c08d4f3SMarcin Juszkiewicz 1820c08d4f3SMarcin Juszkiewicz nodename = g_strdup_printf("/intc"); 1830c08d4f3SMarcin Juszkiewicz qemu_fdt_add_subnode(sms->fdt, nodename); 1840c08d4f3SMarcin Juszkiewicz qemu_fdt_setprop_sized_cells(sms->fdt, nodename, "reg", 1850c08d4f3SMarcin Juszkiewicz 2, sbsa_ref_memmap[SBSA_GIC_DIST].base, 1860c08d4f3SMarcin Juszkiewicz 2, sbsa_ref_memmap[SBSA_GIC_DIST].size, 1870c08d4f3SMarcin Juszkiewicz 2, sbsa_ref_memmap[SBSA_GIC_REDIST].base, 1880c08d4f3SMarcin Juszkiewicz 2, sbsa_ref_memmap[SBSA_GIC_REDIST].size); 1890c08d4f3SMarcin Juszkiewicz 1909fe2b4a2SShashi Mallela nodename = g_strdup_printf("/intc/its"); 1919fe2b4a2SShashi Mallela qemu_fdt_add_subnode(sms->fdt, nodename); 1929fe2b4a2SShashi Mallela qemu_fdt_setprop_sized_cells(sms->fdt, nodename, "reg", 1939fe2b4a2SShashi Mallela 2, sbsa_ref_memmap[SBSA_GIC_ITS].base, 1949fe2b4a2SShashi Mallela 2, sbsa_ref_memmap[SBSA_GIC_ITS].size); 1959fe2b4a2SShashi Mallela 1960c08d4f3SMarcin Juszkiewicz g_free(nodename); 1970c08d4f3SMarcin Juszkiewicz } 1989fe2b4a2SShashi Mallela 199e9fdf453SHongbo Zhang /* 200e9fdf453SHongbo Zhang * Firmware on this machine only uses ACPI table to load OS, these limited 201e9fdf453SHongbo Zhang * device tree nodes are just to let firmware know the info which varies from 202e9fdf453SHongbo Zhang * command line parameters, so it is not necessary to be fully compatible 203e9fdf453SHongbo Zhang * with the kernel CPU and NUMA binding rules. 204e9fdf453SHongbo Zhang */ 205e9fdf453SHongbo Zhang static void create_fdt(SBSAMachineState *sms) 206e9fdf453SHongbo Zhang { 207e9fdf453SHongbo Zhang void *fdt = create_device_tree(&sms->fdt_size); 208e9fdf453SHongbo Zhang const MachineState *ms = MACHINE(sms); 209aa570207STao Xu int nb_numa_nodes = ms->numa_state->num_nodes; 210e9fdf453SHongbo Zhang int cpu; 211e9fdf453SHongbo Zhang 212e9fdf453SHongbo Zhang if (!fdt) { 213e9fdf453SHongbo Zhang error_report("create_device_tree() failed"); 214e9fdf453SHongbo Zhang exit(1); 215e9fdf453SHongbo Zhang } 216e9fdf453SHongbo Zhang 217e9fdf453SHongbo Zhang sms->fdt = fdt; 218e9fdf453SHongbo Zhang 219e9fdf453SHongbo Zhang qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,sbsa-ref"); 220e9fdf453SHongbo Zhang qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 221e9fdf453SHongbo Zhang qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 222e9fdf453SHongbo Zhang 22390ea2cceSLeif Lindholm /* 22490ea2cceSLeif Lindholm * This versioning scheme is for informing platform fw only. It is neither: 22590ea2cceSLeif Lindholm * - A QEMU versioned machine type; a given version of QEMU will emulate 22690ea2cceSLeif Lindholm * a given version of the platform. 22790ea2cceSLeif Lindholm * - A reflection of level of SBSA (now SystemReady SR) support provided. 22890ea2cceSLeif Lindholm * 22990ea2cceSLeif Lindholm * machine-version-major: updated when changes breaking fw compatibility 23090ea2cceSLeif Lindholm * are introduced. 23190ea2cceSLeif Lindholm * machine-version-minor: updated when features are added that don't break 23290ea2cceSLeif Lindholm * fw compatibility. 23390ea2cceSLeif Lindholm */ 23490ea2cceSLeif Lindholm qemu_fdt_setprop_cell(fdt, "/", "machine-version-major", 0); 23562c2b876SYuquan Wang qemu_fdt_setprop_cell(fdt, "/", "machine-version-minor", 3); 23690ea2cceSLeif Lindholm 237118154b7STao Xu if (ms->numa_state->have_numa_distance) { 238e9fdf453SHongbo Zhang int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t); 239e9fdf453SHongbo Zhang uint32_t *matrix = g_malloc0(size); 240e9fdf453SHongbo Zhang int idx, i, j; 241e9fdf453SHongbo Zhang 242e9fdf453SHongbo Zhang for (i = 0; i < nb_numa_nodes; i++) { 243e9fdf453SHongbo Zhang for (j = 0; j < nb_numa_nodes; j++) { 244e9fdf453SHongbo Zhang idx = (i * nb_numa_nodes + j) * 3; 245e9fdf453SHongbo Zhang matrix[idx + 0] = cpu_to_be32(i); 246e9fdf453SHongbo Zhang matrix[idx + 1] = cpu_to_be32(j); 2477e721e7bSTao Xu matrix[idx + 2] = 2487e721e7bSTao Xu cpu_to_be32(ms->numa_state->nodes[i].distance[j]); 249e9fdf453SHongbo Zhang } 250e9fdf453SHongbo Zhang } 251e9fdf453SHongbo Zhang 252e9fdf453SHongbo Zhang qemu_fdt_add_subnode(fdt, "/distance-map"); 253e9fdf453SHongbo Zhang qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix", 254e9fdf453SHongbo Zhang matrix, size); 255e9fdf453SHongbo Zhang g_free(matrix); 256e9fdf453SHongbo Zhang } 257e9fdf453SHongbo Zhang 258999f6ebdSLeif Lindholm /* 259999f6ebdSLeif Lindholm * From Documentation/devicetree/bindings/arm/cpus.yaml 260999f6ebdSLeif Lindholm * On ARM v8 64-bit systems this property is required 261999f6ebdSLeif Lindholm * and matches the MPIDR_EL1 register affinity bits. 262999f6ebdSLeif Lindholm * 263999f6ebdSLeif Lindholm * * If cpus node's #address-cells property is set to 2 264999f6ebdSLeif Lindholm * 265999f6ebdSLeif Lindholm * The first reg cell bits [7:0] must be set to 266999f6ebdSLeif Lindholm * bits [39:32] of MPIDR_EL1. 267999f6ebdSLeif Lindholm * 268999f6ebdSLeif Lindholm * The second reg cell bits [23:0] must be set to 269999f6ebdSLeif Lindholm * bits [23:0] of MPIDR_EL1. 270999f6ebdSLeif Lindholm */ 271e9fdf453SHongbo Zhang qemu_fdt_add_subnode(sms->fdt, "/cpus"); 272999f6ebdSLeif Lindholm qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#address-cells", 2); 273999f6ebdSLeif Lindholm qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#size-cells", 0x0); 274e9fdf453SHongbo Zhang 275e9fdf453SHongbo Zhang for (cpu = sms->smp_cpus - 1; cpu >= 0; cpu--) { 276e9fdf453SHongbo Zhang char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 277e9fdf453SHongbo Zhang ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 278e9fdf453SHongbo Zhang CPUState *cs = CPU(armcpu); 279999f6ebdSLeif Lindholm uint64_t mpidr = sbsa_ref_cpu_mp_affinity(sms, cpu); 280e9fdf453SHongbo Zhang 281e9fdf453SHongbo Zhang qemu_fdt_add_subnode(sms->fdt, nodename); 282999f6ebdSLeif Lindholm qemu_fdt_setprop_u64(sms->fdt, nodename, "reg", mpidr); 283e9fdf453SHongbo Zhang 284e9fdf453SHongbo Zhang if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) { 285e9fdf453SHongbo Zhang qemu_fdt_setprop_cell(sms->fdt, nodename, "numa-node-id", 286e9fdf453SHongbo Zhang ms->possible_cpus->cpus[cs->cpu_index].props.node_id); 287e9fdf453SHongbo Zhang } 288e9fdf453SHongbo Zhang 289e9fdf453SHongbo Zhang g_free(nodename); 290e9fdf453SHongbo Zhang } 2910c08d4f3SMarcin Juszkiewicz 2920c08d4f3SMarcin Juszkiewicz sbsa_fdt_add_gic_node(sms); 293e9fdf453SHongbo Zhang } 294e9fdf453SHongbo Zhang 295e9fdf453SHongbo Zhang #define SBSA_FLASH_SECTOR_SIZE (256 * KiB) 296e9fdf453SHongbo Zhang 297e9fdf453SHongbo Zhang static PFlashCFI01 *sbsa_flash_create1(SBSAMachineState *sms, 298e9fdf453SHongbo Zhang const char *name, 299e9fdf453SHongbo Zhang const char *alias_prop_name) 300e9fdf453SHongbo Zhang { 301e9fdf453SHongbo Zhang /* 302e9fdf453SHongbo Zhang * Create a single flash device. We use the same parameters as 303e9fdf453SHongbo Zhang * the flash devices on the Versatile Express board. 304e9fdf453SHongbo Zhang */ 305df707969SMarkus Armbruster DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01); 306e9fdf453SHongbo Zhang 307e9fdf453SHongbo Zhang qdev_prop_set_uint64(dev, "sector-length", SBSA_FLASH_SECTOR_SIZE); 308e9fdf453SHongbo Zhang qdev_prop_set_uint8(dev, "width", 4); 309e9fdf453SHongbo Zhang qdev_prop_set_uint8(dev, "device-width", 2); 310e9fdf453SHongbo Zhang qdev_prop_set_bit(dev, "big-endian", false); 311e9fdf453SHongbo Zhang qdev_prop_set_uint16(dev, "id0", 0x89); 312e9fdf453SHongbo Zhang qdev_prop_set_uint16(dev, "id1", 0x18); 313e9fdf453SHongbo Zhang qdev_prop_set_uint16(dev, "id2", 0x00); 314e9fdf453SHongbo Zhang qdev_prop_set_uint16(dev, "id3", 0x00); 315e9fdf453SHongbo Zhang qdev_prop_set_string(dev, "name", name); 316d2623129SMarkus Armbruster object_property_add_child(OBJECT(sms), name, OBJECT(dev)); 317e9fdf453SHongbo Zhang object_property_add_alias(OBJECT(sms), alias_prop_name, 318d2623129SMarkus Armbruster OBJECT(dev), "drive"); 319e9fdf453SHongbo Zhang return PFLASH_CFI01(dev); 320e9fdf453SHongbo Zhang } 321e9fdf453SHongbo Zhang 322e9fdf453SHongbo Zhang static void sbsa_flash_create(SBSAMachineState *sms) 323e9fdf453SHongbo Zhang { 324e9fdf453SHongbo Zhang sms->flash[0] = sbsa_flash_create1(sms, "sbsa.flash0", "pflash0"); 325e9fdf453SHongbo Zhang sms->flash[1] = sbsa_flash_create1(sms, "sbsa.flash1", "pflash1"); 326e9fdf453SHongbo Zhang } 327e9fdf453SHongbo Zhang 328e9fdf453SHongbo Zhang static void sbsa_flash_map1(PFlashCFI01 *flash, 329e9fdf453SHongbo Zhang hwaddr base, hwaddr size, 330e9fdf453SHongbo Zhang MemoryRegion *sysmem) 331e9fdf453SHongbo Zhang { 332e9fdf453SHongbo Zhang DeviceState *dev = DEVICE(flash); 333e9fdf453SHongbo Zhang 3344cdd0a77SPhilippe Mathieu-Daudé assert(QEMU_IS_ALIGNED(size, SBSA_FLASH_SECTOR_SIZE)); 335e9fdf453SHongbo Zhang assert(size / SBSA_FLASH_SECTOR_SIZE <= UINT32_MAX); 336e9fdf453SHongbo Zhang qdev_prop_set_uint32(dev, "num-blocks", size / SBSA_FLASH_SECTOR_SIZE); 3373c6ef471SMarkus Armbruster sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 338e9fdf453SHongbo Zhang 339e9fdf453SHongbo Zhang memory_region_add_subregion(sysmem, base, 340e9fdf453SHongbo Zhang sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 341e9fdf453SHongbo Zhang 0)); 342e9fdf453SHongbo Zhang } 343e9fdf453SHongbo Zhang 344e9fdf453SHongbo Zhang static void sbsa_flash_map(SBSAMachineState *sms, 345e9fdf453SHongbo Zhang MemoryRegion *sysmem, 346e9fdf453SHongbo Zhang MemoryRegion *secure_sysmem) 347e9fdf453SHongbo Zhang { 348e9fdf453SHongbo Zhang /* 349e9fdf453SHongbo Zhang * Map two flash devices to fill the SBSA_FLASH space in the memmap. 350e9fdf453SHongbo Zhang * sysmem is the system memory space. secure_sysmem is the secure view 351e9fdf453SHongbo Zhang * of the system, and the first flash device should be made visible only 352e9fdf453SHongbo Zhang * there. The second flash device is visible to both secure and nonsecure. 353e9fdf453SHongbo Zhang */ 354e9fdf453SHongbo Zhang hwaddr flashsize = sbsa_ref_memmap[SBSA_FLASH].size / 2; 355e9fdf453SHongbo Zhang hwaddr flashbase = sbsa_ref_memmap[SBSA_FLASH].base; 356e9fdf453SHongbo Zhang 357e9fdf453SHongbo Zhang sbsa_flash_map1(sms->flash[0], flashbase, flashsize, 358e9fdf453SHongbo Zhang secure_sysmem); 359e9fdf453SHongbo Zhang sbsa_flash_map1(sms->flash[1], flashbase + flashsize, flashsize, 360e9fdf453SHongbo Zhang sysmem); 361e9fdf453SHongbo Zhang } 362e9fdf453SHongbo Zhang 363e9fdf453SHongbo Zhang static bool sbsa_firmware_init(SBSAMachineState *sms, 364e9fdf453SHongbo Zhang MemoryRegion *sysmem, 365e9fdf453SHongbo Zhang MemoryRegion *secure_sysmem) 366e9fdf453SHongbo Zhang { 3670ad3b5d3SPaolo Bonzini const char *bios_name; 368e9fdf453SHongbo Zhang int i; 369e9fdf453SHongbo Zhang BlockBackend *pflash_blk0; 370e9fdf453SHongbo Zhang 371e9fdf453SHongbo Zhang /* Map legacy -drive if=pflash to machine properties */ 372e9fdf453SHongbo Zhang for (i = 0; i < ARRAY_SIZE(sms->flash); i++) { 373e9fdf453SHongbo Zhang pflash_cfi01_legacy_drive(sms->flash[i], 374e9fdf453SHongbo Zhang drive_get(IF_PFLASH, 0, i)); 375e9fdf453SHongbo Zhang } 376e9fdf453SHongbo Zhang 377e9fdf453SHongbo Zhang sbsa_flash_map(sms, sysmem, secure_sysmem); 378e9fdf453SHongbo Zhang 379e9fdf453SHongbo Zhang pflash_blk0 = pflash_cfi01_get_blk(sms->flash[0]); 380e9fdf453SHongbo Zhang 3810ad3b5d3SPaolo Bonzini bios_name = MACHINE(sms)->firmware; 382e9fdf453SHongbo Zhang if (bios_name) { 383e9fdf453SHongbo Zhang char *fname; 384e9fdf453SHongbo Zhang MemoryRegion *mr; 385e9fdf453SHongbo Zhang int image_size; 386e9fdf453SHongbo Zhang 387e9fdf453SHongbo Zhang if (pflash_blk0) { 388e9fdf453SHongbo Zhang error_report("The contents of the first flash device may be " 389e9fdf453SHongbo Zhang "specified with -bios or with -drive if=pflash... " 390e9fdf453SHongbo Zhang "but you cannot use both options at once"); 391e9fdf453SHongbo Zhang exit(1); 392e9fdf453SHongbo Zhang } 393e9fdf453SHongbo Zhang 394e9fdf453SHongbo Zhang /* Fall back to -bios */ 395e9fdf453SHongbo Zhang 396e9fdf453SHongbo Zhang fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 397e9fdf453SHongbo Zhang if (!fname) { 398e9fdf453SHongbo Zhang error_report("Could not find ROM image '%s'", bios_name); 399e9fdf453SHongbo Zhang exit(1); 400e9fdf453SHongbo Zhang } 401e9fdf453SHongbo Zhang mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(sms->flash[0]), 0); 402e9fdf453SHongbo Zhang image_size = load_image_mr(fname, mr); 403e9fdf453SHongbo Zhang g_free(fname); 404e9fdf453SHongbo Zhang if (image_size < 0) { 405e9fdf453SHongbo Zhang error_report("Could not load ROM image '%s'", bios_name); 406e9fdf453SHongbo Zhang exit(1); 407e9fdf453SHongbo Zhang } 408e9fdf453SHongbo Zhang } 409e9fdf453SHongbo Zhang 410e9fdf453SHongbo Zhang return pflash_blk0 || bios_name; 411e9fdf453SHongbo Zhang } 412e9fdf453SHongbo Zhang 413e9fdf453SHongbo Zhang static void create_secure_ram(SBSAMachineState *sms, 414e9fdf453SHongbo Zhang MemoryRegion *secure_sysmem) 415e9fdf453SHongbo Zhang { 416e9fdf453SHongbo Zhang MemoryRegion *secram = g_new(MemoryRegion, 1); 417e9fdf453SHongbo Zhang hwaddr base = sbsa_ref_memmap[SBSA_SECURE_MEM].base; 418e9fdf453SHongbo Zhang hwaddr size = sbsa_ref_memmap[SBSA_SECURE_MEM].size; 419e9fdf453SHongbo Zhang 420e9fdf453SHongbo Zhang memory_region_init_ram(secram, NULL, "sbsa-ref.secure-ram", size, 421e9fdf453SHongbo Zhang &error_fatal); 422e9fdf453SHongbo Zhang memory_region_add_subregion(secure_sysmem, base, secram); 423e9fdf453SHongbo Zhang } 424e9fdf453SHongbo Zhang 4259fe2b4a2SShashi Mallela static void create_its(SBSAMachineState *sms) 4269fe2b4a2SShashi Mallela { 4279fe2b4a2SShashi Mallela const char *itsclass = its_class_name(); 4289fe2b4a2SShashi Mallela DeviceState *dev; 4299fe2b4a2SShashi Mallela 4309fe2b4a2SShashi Mallela dev = qdev_new(itsclass); 4319fe2b4a2SShashi Mallela 4329fe2b4a2SShashi Mallela object_property_set_link(OBJECT(dev), "parent-gicv3", OBJECT(sms->gic), 4339fe2b4a2SShashi Mallela &error_abort); 4349fe2b4a2SShashi Mallela sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 4359fe2b4a2SShashi Mallela sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, sbsa_ref_memmap[SBSA_GIC_ITS].base); 4369fe2b4a2SShashi Mallela } 4379fe2b4a2SShashi Mallela 4389fe2b4a2SShashi Mallela static void create_gic(SBSAMachineState *sms, MemoryRegion *mem) 439e9fdf453SHongbo Zhang { 440cc7d44c2SLike Xu unsigned int smp_cpus = MACHINE(sms)->smp.cpus; 441e9fdf453SHongbo Zhang SysBusDevice *gicbusdev; 442e9fdf453SHongbo Zhang const char *gictype; 443e9fdf453SHongbo Zhang uint32_t redist0_capacity, redist0_count; 444e9fdf453SHongbo Zhang int i; 445e9fdf453SHongbo Zhang 446e9fdf453SHongbo Zhang gictype = gicv3_class_name(); 447e9fdf453SHongbo Zhang 4483e80f690SMarkus Armbruster sms->gic = qdev_new(gictype); 44948ba18e6SPhilippe Mathieu-Daudé qdev_prop_set_uint32(sms->gic, "revision", 3); 45048ba18e6SPhilippe Mathieu-Daudé qdev_prop_set_uint32(sms->gic, "num-cpu", smp_cpus); 451e9fdf453SHongbo Zhang /* 452e9fdf453SHongbo Zhang * Note that the num-irq property counts both internal and external 453e9fdf453SHongbo Zhang * interrupts; there are always 32 of the former (mandated by GIC spec). 454e9fdf453SHongbo Zhang */ 45548ba18e6SPhilippe Mathieu-Daudé qdev_prop_set_uint32(sms->gic, "num-irq", NUM_IRQS + 32); 45648ba18e6SPhilippe Mathieu-Daudé qdev_prop_set_bit(sms->gic, "has-security-extensions", true); 457e9fdf453SHongbo Zhang 458e9fdf453SHongbo Zhang redist0_capacity = 459e9fdf453SHongbo Zhang sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE; 460e9fdf453SHongbo Zhang redist0_count = MIN(smp_cpus, redist0_capacity); 461e9fdf453SHongbo Zhang 46248ba18e6SPhilippe Mathieu-Daudé qdev_prop_set_uint32(sms->gic, "len-redist-region-count", 1); 46348ba18e6SPhilippe Mathieu-Daudé qdev_prop_set_uint32(sms->gic, "redist-region-count[0]", redist0_count); 464e9fdf453SHongbo Zhang 4659fe2b4a2SShashi Mallela object_property_set_link(OBJECT(sms->gic), "sysmem", 4669fe2b4a2SShashi Mallela OBJECT(mem), &error_fatal); 4679fe2b4a2SShashi Mallela qdev_prop_set_bit(sms->gic, "has-lpi", true); 4689fe2b4a2SShashi Mallela 46948ba18e6SPhilippe Mathieu-Daudé gicbusdev = SYS_BUS_DEVICE(sms->gic); 4703c6ef471SMarkus Armbruster sysbus_realize_and_unref(gicbusdev, &error_fatal); 471e9fdf453SHongbo Zhang sysbus_mmio_map(gicbusdev, 0, sbsa_ref_memmap[SBSA_GIC_DIST].base); 472e9fdf453SHongbo Zhang sysbus_mmio_map(gicbusdev, 1, sbsa_ref_memmap[SBSA_GIC_REDIST].base); 473e9fdf453SHongbo Zhang 474e9fdf453SHongbo Zhang /* 475e9fdf453SHongbo Zhang * Wire the outputs from each CPU's generic timer and the GICv3 476e9fdf453SHongbo Zhang * maintenance interrupt signal to the appropriate GIC PPI inputs, 477e9fdf453SHongbo Zhang * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs. 478e9fdf453SHongbo Zhang */ 479e9fdf453SHongbo Zhang for (i = 0; i < smp_cpus; i++) { 480e9fdf453SHongbo Zhang DeviceState *cpudev = DEVICE(qemu_get_cpu(i)); 481e9fdf453SHongbo Zhang int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS; 482e9fdf453SHongbo Zhang int irq; 483e9fdf453SHongbo Zhang /* 484e9fdf453SHongbo Zhang * Mapping from the output timer irq lines from the CPU to the 485e9fdf453SHongbo Zhang * GIC PPI inputs used for this board. 486e9fdf453SHongbo Zhang */ 487e9fdf453SHongbo Zhang const int timer_irq[] = { 488e9fdf453SHongbo Zhang [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ, 489e9fdf453SHongbo Zhang [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ, 490e9fdf453SHongbo Zhang [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ, 491e9fdf453SHongbo Zhang [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ, 492e9fdf453SHongbo Zhang }; 493e9fdf453SHongbo Zhang 494e9fdf453SHongbo Zhang for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) { 495e9fdf453SHongbo Zhang qdev_connect_gpio_out(cpudev, irq, 49648ba18e6SPhilippe Mathieu-Daudé qdev_get_gpio_in(sms->gic, 497e9fdf453SHongbo Zhang ppibase + timer_irq[irq])); 498e9fdf453SHongbo Zhang } 499e9fdf453SHongbo Zhang 500e9fdf453SHongbo Zhang qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0, 50148ba18e6SPhilippe Mathieu-Daudé qdev_get_gpio_in(sms->gic, ppibase 502e9fdf453SHongbo Zhang + ARCH_GIC_MAINT_IRQ)); 503e9fdf453SHongbo Zhang qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0, 50448ba18e6SPhilippe Mathieu-Daudé qdev_get_gpio_in(sms->gic, ppibase 505e9fdf453SHongbo Zhang + VIRTUAL_PMU_IRQ)); 506e9fdf453SHongbo Zhang 507e9fdf453SHongbo Zhang sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ)); 508e9fdf453SHongbo Zhang sysbus_connect_irq(gicbusdev, i + smp_cpus, 509e9fdf453SHongbo Zhang qdev_get_gpio_in(cpudev, ARM_CPU_FIQ)); 510e9fdf453SHongbo Zhang sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus, 511e9fdf453SHongbo Zhang qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ)); 512e9fdf453SHongbo Zhang sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus, 513e9fdf453SHongbo Zhang qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ)); 514e9fdf453SHongbo Zhang } 5159fe2b4a2SShashi Mallela create_its(sms); 516e9fdf453SHongbo Zhang } 517e9fdf453SHongbo Zhang 51848ba18e6SPhilippe Mathieu-Daudé static void create_uart(const SBSAMachineState *sms, int uart, 519e9fdf453SHongbo Zhang MemoryRegion *mem, Chardev *chr) 520e9fdf453SHongbo Zhang { 521e9fdf453SHongbo Zhang hwaddr base = sbsa_ref_memmap[uart].base; 522e9fdf453SHongbo Zhang int irq = sbsa_ref_irqmap[uart]; 5233e80f690SMarkus Armbruster DeviceState *dev = qdev_new(TYPE_PL011); 524e9fdf453SHongbo Zhang SysBusDevice *s = SYS_BUS_DEVICE(dev); 525e9fdf453SHongbo Zhang 526e9fdf453SHongbo Zhang qdev_prop_set_chr(dev, "chardev", chr); 5273c6ef471SMarkus Armbruster sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 528e9fdf453SHongbo Zhang memory_region_add_subregion(mem, base, 529e9fdf453SHongbo Zhang sysbus_mmio_get_region(s, 0)); 53048ba18e6SPhilippe Mathieu-Daudé sysbus_connect_irq(s, 0, qdev_get_gpio_in(sms->gic, irq)); 531e9fdf453SHongbo Zhang } 532e9fdf453SHongbo Zhang 53348ba18e6SPhilippe Mathieu-Daudé static void create_rtc(const SBSAMachineState *sms) 534e9fdf453SHongbo Zhang { 535e9fdf453SHongbo Zhang hwaddr base = sbsa_ref_memmap[SBSA_RTC].base; 536e9fdf453SHongbo Zhang int irq = sbsa_ref_irqmap[SBSA_RTC]; 537e9fdf453SHongbo Zhang 53848ba18e6SPhilippe Mathieu-Daudé sysbus_create_simple("pl031", base, qdev_get_gpio_in(sms->gic, irq)); 539e9fdf453SHongbo Zhang } 540e9fdf453SHongbo Zhang 541baabe7d0SShashi Mallela static void create_wdt(const SBSAMachineState *sms) 542baabe7d0SShashi Mallela { 543baabe7d0SShashi Mallela hwaddr rbase = sbsa_ref_memmap[SBSA_GWDT_REFRESH].base; 544baabe7d0SShashi Mallela hwaddr cbase = sbsa_ref_memmap[SBSA_GWDT_CONTROL].base; 545baabe7d0SShashi Mallela DeviceState *dev = qdev_new(TYPE_WDT_SBSA); 546baabe7d0SShashi Mallela SysBusDevice *s = SYS_BUS_DEVICE(dev); 54780d60a6dSEduardo Habkost int irq = sbsa_ref_irqmap[SBSA_GWDT_WS0]; 548baabe7d0SShashi Mallela 549baabe7d0SShashi Mallela sysbus_realize_and_unref(s, &error_fatal); 550baabe7d0SShashi Mallela sysbus_mmio_map(s, 0, rbase); 551baabe7d0SShashi Mallela sysbus_mmio_map(s, 1, cbase); 552baabe7d0SShashi Mallela sysbus_connect_irq(s, 0, qdev_get_gpio_in(sms->gic, irq)); 553baabe7d0SShashi Mallela } 554baabe7d0SShashi Mallela 555e9fdf453SHongbo Zhang static DeviceState *gpio_key_dev; 556e9fdf453SHongbo Zhang static void sbsa_ref_powerdown_req(Notifier *n, void *opaque) 557e9fdf453SHongbo Zhang { 558e9fdf453SHongbo Zhang /* use gpio Pin 3 for power button event */ 559e9fdf453SHongbo Zhang qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1); 560e9fdf453SHongbo Zhang } 561e9fdf453SHongbo Zhang 562e9fdf453SHongbo Zhang static Notifier sbsa_ref_powerdown_notifier = { 563e9fdf453SHongbo Zhang .notify = sbsa_ref_powerdown_req 564e9fdf453SHongbo Zhang }; 565e9fdf453SHongbo Zhang 56648ba18e6SPhilippe Mathieu-Daudé static void create_gpio(const SBSAMachineState *sms) 567e9fdf453SHongbo Zhang { 568e9fdf453SHongbo Zhang DeviceState *pl061_dev; 569e9fdf453SHongbo Zhang hwaddr base = sbsa_ref_memmap[SBSA_GPIO].base; 570e9fdf453SHongbo Zhang int irq = sbsa_ref_irqmap[SBSA_GPIO]; 571e9fdf453SHongbo Zhang 57248ba18e6SPhilippe Mathieu-Daudé pl061_dev = sysbus_create_simple("pl061", base, 57348ba18e6SPhilippe Mathieu-Daudé qdev_get_gpio_in(sms->gic, irq)); 574e9fdf453SHongbo Zhang 575e9fdf453SHongbo Zhang gpio_key_dev = sysbus_create_simple("gpio-key", -1, 576e9fdf453SHongbo Zhang qdev_get_gpio_in(pl061_dev, 3)); 577e9fdf453SHongbo Zhang 578e9fdf453SHongbo Zhang /* connect powerdown request */ 579e9fdf453SHongbo Zhang qemu_register_powerdown_notifier(&sbsa_ref_powerdown_notifier); 580e9fdf453SHongbo Zhang } 581e9fdf453SHongbo Zhang 58248ba18e6SPhilippe Mathieu-Daudé static void create_ahci(const SBSAMachineState *sms) 583e9fdf453SHongbo Zhang { 584e9fdf453SHongbo Zhang hwaddr base = sbsa_ref_memmap[SBSA_AHCI].base; 585e9fdf453SHongbo Zhang int irq = sbsa_ref_irqmap[SBSA_AHCI]; 586e9fdf453SHongbo Zhang DeviceState *dev; 587e9fdf453SHongbo Zhang DriveInfo *hd[NUM_SATA_PORTS]; 588e9fdf453SHongbo Zhang SysbusAHCIState *sysahci; 589e9fdf453SHongbo Zhang AHCIState *ahci; 590e9fdf453SHongbo Zhang int i; 591e9fdf453SHongbo Zhang 5923e80f690SMarkus Armbruster dev = qdev_new("sysbus-ahci"); 593e9fdf453SHongbo Zhang qdev_prop_set_uint32(dev, "num-ports", NUM_SATA_PORTS); 5943c6ef471SMarkus Armbruster sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 595e9fdf453SHongbo Zhang sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 59648ba18e6SPhilippe Mathieu-Daudé sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(sms->gic, irq)); 597e9fdf453SHongbo Zhang 598e9fdf453SHongbo Zhang sysahci = SYSBUS_AHCI(dev); 599e9fdf453SHongbo Zhang ahci = &sysahci->ahci; 600e9fdf453SHongbo Zhang ide_drive_get(hd, ARRAY_SIZE(hd)); 601e9fdf453SHongbo Zhang for (i = 0; i < ahci->ports; i++) { 602e9fdf453SHongbo Zhang if (hd[i] == NULL) { 603e9fdf453SHongbo Zhang continue; 604e9fdf453SHongbo Zhang } 605b6a5ab27SPhilippe Mathieu-Daudé ide_bus_create_drive(&ahci->dev[i].port, 0, hd[i]); 606e9fdf453SHongbo Zhang } 607e9fdf453SHongbo Zhang } 608e9fdf453SHongbo Zhang 60962c2b876SYuquan Wang static void create_xhci(const SBSAMachineState *sms) 610e9fdf453SHongbo Zhang { 61162c2b876SYuquan Wang hwaddr base = sbsa_ref_memmap[SBSA_XHCI].base; 61262c2b876SYuquan Wang int irq = sbsa_ref_irqmap[SBSA_XHCI]; 61362c2b876SYuquan Wang DeviceState *dev = qdev_new(TYPE_XHCI_SYSBUS); 614*e65ecb66SYuquan Wang qdev_prop_set_uint32(dev, "slots", XHCI_MAXSLOTS); 615e9fdf453SHongbo Zhang 61662c2b876SYuquan Wang sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 61762c2b876SYuquan Wang sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 61862c2b876SYuquan Wang sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(sms->gic, irq)); 619e9fdf453SHongbo Zhang } 620e9fdf453SHongbo Zhang 62148ba18e6SPhilippe Mathieu-Daudé static void create_smmu(const SBSAMachineState *sms, PCIBus *bus) 622e9fdf453SHongbo Zhang { 623e9fdf453SHongbo Zhang hwaddr base = sbsa_ref_memmap[SBSA_SMMU].base; 624e9fdf453SHongbo Zhang int irq = sbsa_ref_irqmap[SBSA_SMMU]; 625e9fdf453SHongbo Zhang DeviceState *dev; 626e9fdf453SHongbo Zhang int i; 627e9fdf453SHongbo Zhang 628a431ab0eSRichard Henderson dev = qdev_new(TYPE_ARM_SMMUV3); 629e9fdf453SHongbo Zhang 6305325cc34SMarkus Armbruster object_property_set_link(OBJECT(dev), "primary-bus", OBJECT(bus), 631e9fdf453SHongbo Zhang &error_abort); 6323c6ef471SMarkus Armbruster sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 633e9fdf453SHongbo Zhang sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 634e9fdf453SHongbo Zhang for (i = 0; i < NUM_SMMU_IRQS; i++) { 63548ba18e6SPhilippe Mathieu-Daudé sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 636b8bf3472SGraeme Gregory qdev_get_gpio_in(sms->gic, irq + i)); 637e9fdf453SHongbo Zhang } 638e9fdf453SHongbo Zhang } 639e9fdf453SHongbo Zhang 64048ba18e6SPhilippe Mathieu-Daudé static void create_pcie(SBSAMachineState *sms) 641e9fdf453SHongbo Zhang { 642e9fdf453SHongbo Zhang hwaddr base_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].base; 643e9fdf453SHongbo Zhang hwaddr size_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].size; 644e9fdf453SHongbo Zhang hwaddr base_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].base; 645e9fdf453SHongbo Zhang hwaddr size_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].size; 646e9fdf453SHongbo Zhang hwaddr base_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].base; 647e9fdf453SHongbo Zhang hwaddr size_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].size; 648e9fdf453SHongbo Zhang hwaddr base_pio = sbsa_ref_memmap[SBSA_PCIE_PIO].base; 649e9fdf453SHongbo Zhang int irq = sbsa_ref_irqmap[SBSA_PCIE]; 650611eda59SThomas Huth MachineClass *mc = MACHINE_GET_CLASS(sms); 651e9fdf453SHongbo Zhang MemoryRegion *mmio_alias, *mmio_alias_high, *mmio_reg; 652e9fdf453SHongbo Zhang MemoryRegion *ecam_alias, *ecam_reg; 653e9fdf453SHongbo Zhang DeviceState *dev; 654e9fdf453SHongbo Zhang PCIHostState *pci; 655e9fdf453SHongbo Zhang int i; 656e9fdf453SHongbo Zhang 6573e80f690SMarkus Armbruster dev = qdev_new(TYPE_GPEX_HOST); 6583c6ef471SMarkus Armbruster sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 659e9fdf453SHongbo Zhang 660e9fdf453SHongbo Zhang /* Map ECAM space */ 661e9fdf453SHongbo Zhang ecam_alias = g_new0(MemoryRegion, 1); 662e9fdf453SHongbo Zhang ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 663e9fdf453SHongbo Zhang memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam", 664e9fdf453SHongbo Zhang ecam_reg, 0, size_ecam); 665e9fdf453SHongbo Zhang memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias); 666e9fdf453SHongbo Zhang 667e9fdf453SHongbo Zhang /* Map the MMIO space */ 668e9fdf453SHongbo Zhang mmio_alias = g_new0(MemoryRegion, 1); 669e9fdf453SHongbo Zhang mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 670e9fdf453SHongbo Zhang memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio", 671e9fdf453SHongbo Zhang mmio_reg, base_mmio, size_mmio); 672e9fdf453SHongbo Zhang memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias); 673e9fdf453SHongbo Zhang 674e9fdf453SHongbo Zhang /* Map the MMIO_HIGH space */ 675e9fdf453SHongbo Zhang mmio_alias_high = g_new0(MemoryRegion, 1); 676e9fdf453SHongbo Zhang memory_region_init_alias(mmio_alias_high, OBJECT(dev), "pcie-mmio-high", 677e9fdf453SHongbo Zhang mmio_reg, base_mmio_high, size_mmio_high); 678e9fdf453SHongbo Zhang memory_region_add_subregion(get_system_memory(), base_mmio_high, 679e9fdf453SHongbo Zhang mmio_alias_high); 680e9fdf453SHongbo Zhang 681e9fdf453SHongbo Zhang /* Map IO port space */ 682e9fdf453SHongbo Zhang sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio); 683e9fdf453SHongbo Zhang 684e9fdf453SHongbo Zhang for (i = 0; i < GPEX_NUM_IRQS; i++) { 68548ba18e6SPhilippe Mathieu-Daudé sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 686870f0051SGraeme Gregory qdev_get_gpio_in(sms->gic, irq + i)); 687e9fdf453SHongbo Zhang gpex_set_irq_num(GPEX_HOST(dev), i, irq + i); 688e9fdf453SHongbo Zhang } 689e9fdf453SHongbo Zhang 690e9fdf453SHongbo Zhang pci = PCI_HOST_BRIDGE(dev); 691e9fdf453SHongbo Zhang if (pci->bus) { 692e9fdf453SHongbo Zhang for (i = 0; i < nb_nics; i++) { 693b697a489SThomas Huth pci_nic_init_nofail(&nd_table[i], pci->bus, mc->default_nic, NULL); 694e9fdf453SHongbo Zhang } 695e9fdf453SHongbo Zhang } 696e9fdf453SHongbo Zhang 6979162ac6bSMarcin Juszkiewicz pci_create_simple(pci->bus, -1, "bochs-display"); 698e9fdf453SHongbo Zhang 69948ba18e6SPhilippe Mathieu-Daudé create_smmu(sms, pci->bus); 700e9fdf453SHongbo Zhang } 701e9fdf453SHongbo Zhang 702e9fdf453SHongbo Zhang static void *sbsa_ref_dtb(const struct arm_boot_info *binfo, int *fdt_size) 703e9fdf453SHongbo Zhang { 704e9fdf453SHongbo Zhang const SBSAMachineState *board = container_of(binfo, SBSAMachineState, 705e9fdf453SHongbo Zhang bootinfo); 706e9fdf453SHongbo Zhang 707e9fdf453SHongbo Zhang *fdt_size = board->fdt_size; 708e9fdf453SHongbo Zhang return board->fdt; 709e9fdf453SHongbo Zhang } 710e9fdf453SHongbo Zhang 7113f462bf0SGraeme Gregory static void create_secure_ec(MemoryRegion *mem) 7123f462bf0SGraeme Gregory { 7133f462bf0SGraeme Gregory hwaddr base = sbsa_ref_memmap[SBSA_SECURE_EC].base; 7143f462bf0SGraeme Gregory DeviceState *dev = qdev_new("sbsa-ec"); 7153f462bf0SGraeme Gregory SysBusDevice *s = SYS_BUS_DEVICE(dev); 7163f462bf0SGraeme Gregory 7173f462bf0SGraeme Gregory memory_region_add_subregion(mem, base, 7183f462bf0SGraeme Gregory sysbus_mmio_get_region(s, 0)); 7193f462bf0SGraeme Gregory } 7203f462bf0SGraeme Gregory 72164580903SHongbo Zhang static void sbsa_ref_init(MachineState *machine) 72264580903SHongbo Zhang { 723cc7d44c2SLike Xu unsigned int smp_cpus = machine->smp.cpus; 724cc7d44c2SLike Xu unsigned int max_cpus = machine->smp.max_cpus; 72564580903SHongbo Zhang SBSAMachineState *sms = SBSA_MACHINE(machine); 72664580903SHongbo Zhang MachineClass *mc = MACHINE_GET_CLASS(machine); 72764580903SHongbo Zhang MemoryRegion *sysmem = get_system_memory(); 728c8ead571SPeter Maydell MemoryRegion *secure_sysmem = g_new(MemoryRegion, 1); 729e9fdf453SHongbo Zhang bool firmware_loaded; 73064580903SHongbo Zhang const CPUArchIdList *possible_cpus; 73164580903SHongbo Zhang int n, sbsa_max_cpus; 73264580903SHongbo Zhang 733ce3adffcSMarcin Juszkiewicz if (!cpu_type_valid(machine->cpu_type)) { 734b84722cfSShuuichirou Ishii error_report("sbsa-ref: CPU type %s not supported", machine->cpu_type); 73564580903SHongbo Zhang exit(1); 73664580903SHongbo Zhang } 73764580903SHongbo Zhang 73864580903SHongbo Zhang if (kvm_enabled()) { 73964580903SHongbo Zhang error_report("sbsa-ref: KVM is not supported for this machine"); 74064580903SHongbo Zhang exit(1); 74164580903SHongbo Zhang } 74264580903SHongbo Zhang 74364580903SHongbo Zhang /* 744e9fdf453SHongbo Zhang * The Secure view of the world is the same as the NonSecure, 745e9fdf453SHongbo Zhang * but with a few extra devices. Create it as a container region 746e9fdf453SHongbo Zhang * containing the system memory at low priority; any secure-only 747e9fdf453SHongbo Zhang * devices go in at higher priority and take precedence. 748e9fdf453SHongbo Zhang */ 749e9fdf453SHongbo Zhang memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory", 750e9fdf453SHongbo Zhang UINT64_MAX); 751e9fdf453SHongbo Zhang memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1); 752e9fdf453SHongbo Zhang 753c8ead571SPeter Maydell firmware_loaded = sbsa_firmware_init(sms, sysmem, secure_sysmem); 754e9fdf453SHongbo Zhang 755e9fdf453SHongbo Zhang /* 75664580903SHongbo Zhang * This machine has EL3 enabled, external firmware should supply PSCI 75764580903SHongbo Zhang * implementation, so the QEMU's internal PSCI is disabled. 75864580903SHongbo Zhang */ 75964580903SHongbo Zhang sms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED; 76064580903SHongbo Zhang 76164580903SHongbo Zhang sbsa_max_cpus = sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE; 76264580903SHongbo Zhang 76364580903SHongbo Zhang if (max_cpus > sbsa_max_cpus) { 76464580903SHongbo Zhang error_report("Number of SMP CPUs requested (%d) exceeds max CPUs " 76564580903SHongbo Zhang "supported by machine 'sbsa-ref' (%d)", 76664580903SHongbo Zhang max_cpus, sbsa_max_cpus); 76764580903SHongbo Zhang exit(1); 76864580903SHongbo Zhang } 76964580903SHongbo Zhang 77064580903SHongbo Zhang sms->smp_cpus = smp_cpus; 77164580903SHongbo Zhang 77264580903SHongbo Zhang if (machine->ram_size > sbsa_ref_memmap[SBSA_MEM].size) { 77364580903SHongbo Zhang error_report("sbsa-ref: cannot model more than %dGB RAM", RAMLIMIT_GB); 77464580903SHongbo Zhang exit(1); 77564580903SHongbo Zhang } 77664580903SHongbo Zhang 77764580903SHongbo Zhang possible_cpus = mc->possible_cpu_arch_ids(machine); 77864580903SHongbo Zhang for (n = 0; n < possible_cpus->len; n++) { 77964580903SHongbo Zhang Object *cpuobj; 78064580903SHongbo Zhang CPUState *cs; 78164580903SHongbo Zhang 78264580903SHongbo Zhang if (n >= smp_cpus) { 78364580903SHongbo Zhang break; 78464580903SHongbo Zhang } 78564580903SHongbo Zhang 78664580903SHongbo Zhang cpuobj = object_new(possible_cpus->cpus[n].type); 7875325cc34SMarkus Armbruster object_property_set_int(cpuobj, "mp-affinity", 7885325cc34SMarkus Armbruster possible_cpus->cpus[n].arch_id, NULL); 78964580903SHongbo Zhang 79064580903SHongbo Zhang cs = CPU(cpuobj); 79164580903SHongbo Zhang cs->cpu_index = n; 79264580903SHongbo Zhang 79364580903SHongbo Zhang numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj), 79464580903SHongbo Zhang &error_fatal); 79564580903SHongbo Zhang 796efba1595SDaniel P. Berrangé if (object_property_find(cpuobj, "reset-cbar")) { 7975325cc34SMarkus Armbruster object_property_set_int(cpuobj, "reset-cbar", 79864580903SHongbo Zhang sbsa_ref_memmap[SBSA_CPUPERIPHS].base, 7995325cc34SMarkus Armbruster &error_abort); 80064580903SHongbo Zhang } 80164580903SHongbo Zhang 8025325cc34SMarkus Armbruster object_property_set_link(cpuobj, "memory", OBJECT(sysmem), 80364580903SHongbo Zhang &error_abort); 80464580903SHongbo Zhang 8055325cc34SMarkus Armbruster object_property_set_link(cpuobj, "secure-memory", 8065325cc34SMarkus Armbruster OBJECT(secure_sysmem), &error_abort); 80764580903SHongbo Zhang 808ce189ab2SMarkus Armbruster qdev_realize(DEVICE(cpuobj), NULL, &error_fatal); 80964580903SHongbo Zhang object_unref(cpuobj); 81064580903SHongbo Zhang } 81164580903SHongbo Zhang 8123818ed92SIgor Mammedov memory_region_add_subregion(sysmem, sbsa_ref_memmap[SBSA_MEM].base, 8133818ed92SIgor Mammedov machine->ram); 81464580903SHongbo Zhang 815e9fdf453SHongbo Zhang create_fdt(sms); 816e9fdf453SHongbo Zhang 817e9fdf453SHongbo Zhang create_secure_ram(sms, secure_sysmem); 818e9fdf453SHongbo Zhang 8199fe2b4a2SShashi Mallela create_gic(sms, sysmem); 820e9fdf453SHongbo Zhang 82148ba18e6SPhilippe Mathieu-Daudé create_uart(sms, SBSA_UART, sysmem, serial_hd(0)); 82248ba18e6SPhilippe Mathieu-Daudé create_uart(sms, SBSA_SECURE_UART, secure_sysmem, serial_hd(1)); 823e9fdf453SHongbo Zhang /* Second secure UART for RAS and MM from EL0 */ 82448ba18e6SPhilippe Mathieu-Daudé create_uart(sms, SBSA_SECURE_UART_MM, secure_sysmem, serial_hd(2)); 825e9fdf453SHongbo Zhang 82648ba18e6SPhilippe Mathieu-Daudé create_rtc(sms); 827e9fdf453SHongbo Zhang 828baabe7d0SShashi Mallela create_wdt(sms); 829baabe7d0SShashi Mallela 83048ba18e6SPhilippe Mathieu-Daudé create_gpio(sms); 831e9fdf453SHongbo Zhang 83248ba18e6SPhilippe Mathieu-Daudé create_ahci(sms); 833e9fdf453SHongbo Zhang 83462c2b876SYuquan Wang create_xhci(sms); 835e9fdf453SHongbo Zhang 83648ba18e6SPhilippe Mathieu-Daudé create_pcie(sms); 837e9fdf453SHongbo Zhang 8383f462bf0SGraeme Gregory create_secure_ec(secure_sysmem); 8393f462bf0SGraeme Gregory 84064580903SHongbo Zhang sms->bootinfo.ram_size = machine->ram_size; 84164580903SHongbo Zhang sms->bootinfo.board_id = -1; 84264580903SHongbo Zhang sms->bootinfo.loader_start = sbsa_ref_memmap[SBSA_MEM].base; 843e9fdf453SHongbo Zhang sms->bootinfo.get_dtb = sbsa_ref_dtb; 844e9fdf453SHongbo Zhang sms->bootinfo.firmware_loaded = firmware_loaded; 8452744ece8STao Xu arm_load_kernel(ARM_CPU(first_cpu), machine, &sms->bootinfo); 84664580903SHongbo Zhang } 84764580903SHongbo Zhang 84864580903SHongbo Zhang static const CPUArchIdList *sbsa_ref_possible_cpu_arch_ids(MachineState *ms) 84964580903SHongbo Zhang { 850cc7d44c2SLike Xu unsigned int max_cpus = ms->smp.max_cpus; 85164580903SHongbo Zhang SBSAMachineState *sms = SBSA_MACHINE(ms); 85264580903SHongbo Zhang int n; 85364580903SHongbo Zhang 85464580903SHongbo Zhang if (ms->possible_cpus) { 85564580903SHongbo Zhang assert(ms->possible_cpus->len == max_cpus); 85664580903SHongbo Zhang return ms->possible_cpus; 85764580903SHongbo Zhang } 85864580903SHongbo Zhang 85964580903SHongbo Zhang ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 86064580903SHongbo Zhang sizeof(CPUArchId) * max_cpus); 86164580903SHongbo Zhang ms->possible_cpus->len = max_cpus; 86264580903SHongbo Zhang for (n = 0; n < ms->possible_cpus->len; n++) { 86364580903SHongbo Zhang ms->possible_cpus->cpus[n].type = ms->cpu_type; 86464580903SHongbo Zhang ms->possible_cpus->cpus[n].arch_id = 86564580903SHongbo Zhang sbsa_ref_cpu_mp_affinity(sms, n); 86664580903SHongbo Zhang ms->possible_cpus->cpus[n].props.has_thread_id = true; 86764580903SHongbo Zhang ms->possible_cpus->cpus[n].props.thread_id = n; 86864580903SHongbo Zhang } 86964580903SHongbo Zhang return ms->possible_cpus; 87064580903SHongbo Zhang } 87164580903SHongbo Zhang 87264580903SHongbo Zhang static CpuInstanceProperties 87364580903SHongbo Zhang sbsa_ref_cpu_index_to_props(MachineState *ms, unsigned cpu_index) 87464580903SHongbo Zhang { 87564580903SHongbo Zhang MachineClass *mc = MACHINE_GET_CLASS(ms); 87664580903SHongbo Zhang const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 87764580903SHongbo Zhang 87864580903SHongbo Zhang assert(cpu_index < possible_cpus->len); 87964580903SHongbo Zhang return possible_cpus->cpus[cpu_index].props; 88064580903SHongbo Zhang } 88164580903SHongbo Zhang 88264580903SHongbo Zhang static int64_t 88364580903SHongbo Zhang sbsa_ref_get_default_cpu_node_id(const MachineState *ms, int idx) 88464580903SHongbo Zhang { 885aa570207STao Xu return idx % ms->numa_state->num_nodes; 88664580903SHongbo Zhang } 88764580903SHongbo Zhang 888e9fdf453SHongbo Zhang static void sbsa_ref_instance_init(Object *obj) 889e9fdf453SHongbo Zhang { 890e9fdf453SHongbo Zhang SBSAMachineState *sms = SBSA_MACHINE(obj); 891e9fdf453SHongbo Zhang 892e9fdf453SHongbo Zhang sbsa_flash_create(sms); 893e9fdf453SHongbo Zhang } 894e9fdf453SHongbo Zhang 89564580903SHongbo Zhang static void sbsa_ref_class_init(ObjectClass *oc, void *data) 89664580903SHongbo Zhang { 89764580903SHongbo Zhang MachineClass *mc = MACHINE_CLASS(oc); 89864580903SHongbo Zhang 89964580903SHongbo Zhang mc->init = sbsa_ref_init; 90064580903SHongbo Zhang mc->desc = "QEMU 'SBSA Reference' ARM Virtual Machine"; 9011877272bSMarcin Juszkiewicz mc->default_cpu_type = ARM_CPU_TYPE_NAME("neoverse-n1"); 90264580903SHongbo Zhang mc->max_cpus = 512; 90364580903SHongbo Zhang mc->pci_allow_0_address = true; 90464580903SHongbo Zhang mc->minimum_page_bits = 12; 90564580903SHongbo Zhang mc->block_default_type = IF_IDE; 90664580903SHongbo Zhang mc->no_cdrom = 1; 907611eda59SThomas Huth mc->default_nic = "e1000e"; 90864580903SHongbo Zhang mc->default_ram_size = 1 * GiB; 9093818ed92SIgor Mammedov mc->default_ram_id = "sbsa-ref.ram"; 91064580903SHongbo Zhang mc->default_cpus = 4; 91164580903SHongbo Zhang mc->possible_cpu_arch_ids = sbsa_ref_possible_cpu_arch_ids; 91264580903SHongbo Zhang mc->cpu_index_to_instance_props = sbsa_ref_cpu_index_to_props; 91364580903SHongbo Zhang mc->get_default_cpu_node_id = sbsa_ref_get_default_cpu_node_id; 914fecff672SGavin Shan /* platform instead of architectural choice */ 915fecff672SGavin Shan mc->cpu_cluster_has_numa_boundary = true; 91664580903SHongbo Zhang } 91764580903SHongbo Zhang 91864580903SHongbo Zhang static const TypeInfo sbsa_ref_info = { 91964580903SHongbo Zhang .name = TYPE_SBSA_MACHINE, 92064580903SHongbo Zhang .parent = TYPE_MACHINE, 921e9fdf453SHongbo Zhang .instance_init = sbsa_ref_instance_init, 92264580903SHongbo Zhang .class_init = sbsa_ref_class_init, 92364580903SHongbo Zhang .instance_size = sizeof(SBSAMachineState), 92464580903SHongbo Zhang }; 92564580903SHongbo Zhang 92664580903SHongbo Zhang static void sbsa_ref_machine_init(void) 92764580903SHongbo Zhang { 92864580903SHongbo Zhang type_register_static(&sbsa_ref_info); 92964580903SHongbo Zhang } 93064580903SHongbo Zhang 93164580903SHongbo Zhang type_init(sbsa_ref_machine_init); 932