1 /* 2 * ARM SBSA Reference Platform emulation 3 * 4 * Copyright (c) 2018 Linaro Limited 5 * Written by Hongbo Zhang <hongbo.zhang@linaro.org> 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms and conditions of the GNU General Public License, 9 * version 2 or later, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 * You should have received a copy of the GNU General Public License along with 17 * this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qemu/datadir.h" 22 #include "qapi/error.h" 23 #include "qemu/error-report.h" 24 #include "qemu/units.h" 25 #include "sysemu/device_tree.h" 26 #include "sysemu/numa.h" 27 #include "sysemu/runstate.h" 28 #include "sysemu/sysemu.h" 29 #include "exec/hwaddr.h" 30 #include "kvm_arm.h" 31 #include "hw/arm/boot.h" 32 #include "hw/arm/smmuv3.h" 33 #include "hw/block/flash.h" 34 #include "hw/boards.h" 35 #include "hw/ide/internal.h" 36 #include "hw/ide/ahci_internal.h" 37 #include "hw/intc/arm_gicv3_common.h" 38 #include "hw/loader.h" 39 #include "hw/pci-host/gpex.h" 40 #include "hw/qdev-properties.h" 41 #include "hw/usb.h" 42 #include "hw/char/pl011.h" 43 #include "hw/watchdog/sbsa_gwdt.h" 44 #include "net/net.h" 45 #include "qom/object.h" 46 47 #define RAMLIMIT_GB 8192 48 #define RAMLIMIT_BYTES (RAMLIMIT_GB * GiB) 49 50 #define NUM_IRQS 256 51 #define NUM_SMMU_IRQS 4 52 #define NUM_SATA_PORTS 6 53 54 #define VIRTUAL_PMU_IRQ 7 55 #define ARCH_GIC_MAINT_IRQ 9 56 #define ARCH_TIMER_VIRT_IRQ 11 57 #define ARCH_TIMER_S_EL1_IRQ 13 58 #define ARCH_TIMER_NS_EL1_IRQ 14 59 #define ARCH_TIMER_NS_EL2_IRQ 10 60 61 enum { 62 SBSA_FLASH, 63 SBSA_MEM, 64 SBSA_CPUPERIPHS, 65 SBSA_GIC_DIST, 66 SBSA_GIC_REDIST, 67 SBSA_SECURE_EC, 68 SBSA_GWDT_WS0, 69 SBSA_GWDT_REFRESH, 70 SBSA_GWDT_CONTROL, 71 SBSA_SMMU, 72 SBSA_UART, 73 SBSA_RTC, 74 SBSA_PCIE, 75 SBSA_PCIE_MMIO, 76 SBSA_PCIE_MMIO_HIGH, 77 SBSA_PCIE_PIO, 78 SBSA_PCIE_ECAM, 79 SBSA_GPIO, 80 SBSA_SECURE_UART, 81 SBSA_SECURE_UART_MM, 82 SBSA_SECURE_MEM, 83 SBSA_AHCI, 84 SBSA_EHCI, 85 }; 86 87 struct SBSAMachineState { 88 MachineState parent; 89 struct arm_boot_info bootinfo; 90 int smp_cpus; 91 void *fdt; 92 int fdt_size; 93 int psci_conduit; 94 DeviceState *gic; 95 PFlashCFI01 *flash[2]; 96 }; 97 98 #define TYPE_SBSA_MACHINE MACHINE_TYPE_NAME("sbsa-ref") 99 OBJECT_DECLARE_SIMPLE_TYPE(SBSAMachineState, SBSA_MACHINE) 100 101 static const MemMapEntry sbsa_ref_memmap[] = { 102 /* 512M boot ROM */ 103 [SBSA_FLASH] = { 0, 0x20000000 }, 104 /* 512M secure memory */ 105 [SBSA_SECURE_MEM] = { 0x20000000, 0x20000000 }, 106 /* Space reserved for CPU peripheral devices */ 107 [SBSA_CPUPERIPHS] = { 0x40000000, 0x00040000 }, 108 [SBSA_GIC_DIST] = { 0x40060000, 0x00010000 }, 109 [SBSA_GIC_REDIST] = { 0x40080000, 0x04000000 }, 110 [SBSA_SECURE_EC] = { 0x50000000, 0x00001000 }, 111 [SBSA_GWDT_REFRESH] = { 0x50010000, 0x00001000 }, 112 [SBSA_GWDT_CONTROL] = { 0x50011000, 0x00001000 }, 113 [SBSA_UART] = { 0x60000000, 0x00001000 }, 114 [SBSA_RTC] = { 0x60010000, 0x00001000 }, 115 [SBSA_GPIO] = { 0x60020000, 0x00001000 }, 116 [SBSA_SECURE_UART] = { 0x60030000, 0x00001000 }, 117 [SBSA_SECURE_UART_MM] = { 0x60040000, 0x00001000 }, 118 [SBSA_SMMU] = { 0x60050000, 0x00020000 }, 119 /* Space here reserved for more SMMUs */ 120 [SBSA_AHCI] = { 0x60100000, 0x00010000 }, 121 [SBSA_EHCI] = { 0x60110000, 0x00010000 }, 122 /* Space here reserved for other devices */ 123 [SBSA_PCIE_PIO] = { 0x7fff0000, 0x00010000 }, 124 /* 32-bit address PCIE MMIO space */ 125 [SBSA_PCIE_MMIO] = { 0x80000000, 0x70000000 }, 126 /* 256M PCIE ECAM space */ 127 [SBSA_PCIE_ECAM] = { 0xf0000000, 0x10000000 }, 128 /* ~1TB PCIE MMIO space (4GB to 1024GB boundary) */ 129 [SBSA_PCIE_MMIO_HIGH] = { 0x100000000ULL, 0xFF00000000ULL }, 130 [SBSA_MEM] = { 0x10000000000ULL, RAMLIMIT_BYTES }, 131 }; 132 133 static const int sbsa_ref_irqmap[] = { 134 [SBSA_UART] = 1, 135 [SBSA_RTC] = 2, 136 [SBSA_PCIE] = 3, /* ... to 6 */ 137 [SBSA_GPIO] = 7, 138 [SBSA_SECURE_UART] = 8, 139 [SBSA_SECURE_UART_MM] = 9, 140 [SBSA_AHCI] = 10, 141 [SBSA_EHCI] = 11, 142 [SBSA_SMMU] = 12, /* ... to 15 */ 143 [SBSA_GWDT_WS0] = 16, 144 }; 145 146 static const char * const valid_cpus[] = { 147 ARM_CPU_TYPE_NAME("cortex-a57"), 148 ARM_CPU_TYPE_NAME("cortex-a72"), 149 ARM_CPU_TYPE_NAME("cortex-a76"), 150 ARM_CPU_TYPE_NAME("neoverse-n1"), 151 ARM_CPU_TYPE_NAME("max"), 152 }; 153 154 static bool cpu_type_valid(const char *cpu) 155 { 156 int i; 157 158 for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) { 159 if (strcmp(cpu, valid_cpus[i]) == 0) { 160 return true; 161 } 162 } 163 return false; 164 } 165 166 static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx) 167 { 168 uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER; 169 return arm_cpu_mp_affinity(idx, clustersz); 170 } 171 172 /* 173 * Firmware on this machine only uses ACPI table to load OS, these limited 174 * device tree nodes are just to let firmware know the info which varies from 175 * command line parameters, so it is not necessary to be fully compatible 176 * with the kernel CPU and NUMA binding rules. 177 */ 178 static void create_fdt(SBSAMachineState *sms) 179 { 180 void *fdt = create_device_tree(&sms->fdt_size); 181 const MachineState *ms = MACHINE(sms); 182 int nb_numa_nodes = ms->numa_state->num_nodes; 183 int cpu; 184 185 if (!fdt) { 186 error_report("create_device_tree() failed"); 187 exit(1); 188 } 189 190 sms->fdt = fdt; 191 192 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,sbsa-ref"); 193 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 194 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 195 196 /* 197 * This versioning scheme is for informing platform fw only. It is neither: 198 * - A QEMU versioned machine type; a given version of QEMU will emulate 199 * a given version of the platform. 200 * - A reflection of level of SBSA (now SystemReady SR) support provided. 201 * 202 * machine-version-major: updated when changes breaking fw compatibility 203 * are introduced. 204 * machine-version-minor: updated when features are added that don't break 205 * fw compatibility. 206 */ 207 qemu_fdt_setprop_cell(fdt, "/", "machine-version-major", 0); 208 qemu_fdt_setprop_cell(fdt, "/", "machine-version-minor", 0); 209 210 if (ms->numa_state->have_numa_distance) { 211 int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t); 212 uint32_t *matrix = g_malloc0(size); 213 int idx, i, j; 214 215 for (i = 0; i < nb_numa_nodes; i++) { 216 for (j = 0; j < nb_numa_nodes; j++) { 217 idx = (i * nb_numa_nodes + j) * 3; 218 matrix[idx + 0] = cpu_to_be32(i); 219 matrix[idx + 1] = cpu_to_be32(j); 220 matrix[idx + 2] = 221 cpu_to_be32(ms->numa_state->nodes[i].distance[j]); 222 } 223 } 224 225 qemu_fdt_add_subnode(fdt, "/distance-map"); 226 qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix", 227 matrix, size); 228 g_free(matrix); 229 } 230 231 /* 232 * From Documentation/devicetree/bindings/arm/cpus.yaml 233 * On ARM v8 64-bit systems this property is required 234 * and matches the MPIDR_EL1 register affinity bits. 235 * 236 * * If cpus node's #address-cells property is set to 2 237 * 238 * The first reg cell bits [7:0] must be set to 239 * bits [39:32] of MPIDR_EL1. 240 * 241 * The second reg cell bits [23:0] must be set to 242 * bits [23:0] of MPIDR_EL1. 243 */ 244 qemu_fdt_add_subnode(sms->fdt, "/cpus"); 245 qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#address-cells", 2); 246 qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#size-cells", 0x0); 247 248 for (cpu = sms->smp_cpus - 1; cpu >= 0; cpu--) { 249 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 250 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 251 CPUState *cs = CPU(armcpu); 252 uint64_t mpidr = sbsa_ref_cpu_mp_affinity(sms, cpu); 253 254 qemu_fdt_add_subnode(sms->fdt, nodename); 255 qemu_fdt_setprop_u64(sms->fdt, nodename, "reg", mpidr); 256 257 if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) { 258 qemu_fdt_setprop_cell(sms->fdt, nodename, "numa-node-id", 259 ms->possible_cpus->cpus[cs->cpu_index].props.node_id); 260 } 261 262 g_free(nodename); 263 } 264 } 265 266 #define SBSA_FLASH_SECTOR_SIZE (256 * KiB) 267 268 static PFlashCFI01 *sbsa_flash_create1(SBSAMachineState *sms, 269 const char *name, 270 const char *alias_prop_name) 271 { 272 /* 273 * Create a single flash device. We use the same parameters as 274 * the flash devices on the Versatile Express board. 275 */ 276 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01); 277 278 qdev_prop_set_uint64(dev, "sector-length", SBSA_FLASH_SECTOR_SIZE); 279 qdev_prop_set_uint8(dev, "width", 4); 280 qdev_prop_set_uint8(dev, "device-width", 2); 281 qdev_prop_set_bit(dev, "big-endian", false); 282 qdev_prop_set_uint16(dev, "id0", 0x89); 283 qdev_prop_set_uint16(dev, "id1", 0x18); 284 qdev_prop_set_uint16(dev, "id2", 0x00); 285 qdev_prop_set_uint16(dev, "id3", 0x00); 286 qdev_prop_set_string(dev, "name", name); 287 object_property_add_child(OBJECT(sms), name, OBJECT(dev)); 288 object_property_add_alias(OBJECT(sms), alias_prop_name, 289 OBJECT(dev), "drive"); 290 return PFLASH_CFI01(dev); 291 } 292 293 static void sbsa_flash_create(SBSAMachineState *sms) 294 { 295 sms->flash[0] = sbsa_flash_create1(sms, "sbsa.flash0", "pflash0"); 296 sms->flash[1] = sbsa_flash_create1(sms, "sbsa.flash1", "pflash1"); 297 } 298 299 static void sbsa_flash_map1(PFlashCFI01 *flash, 300 hwaddr base, hwaddr size, 301 MemoryRegion *sysmem) 302 { 303 DeviceState *dev = DEVICE(flash); 304 305 assert(QEMU_IS_ALIGNED(size, SBSA_FLASH_SECTOR_SIZE)); 306 assert(size / SBSA_FLASH_SECTOR_SIZE <= UINT32_MAX); 307 qdev_prop_set_uint32(dev, "num-blocks", size / SBSA_FLASH_SECTOR_SIZE); 308 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 309 310 memory_region_add_subregion(sysmem, base, 311 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 312 0)); 313 } 314 315 static void sbsa_flash_map(SBSAMachineState *sms, 316 MemoryRegion *sysmem, 317 MemoryRegion *secure_sysmem) 318 { 319 /* 320 * Map two flash devices to fill the SBSA_FLASH space in the memmap. 321 * sysmem is the system memory space. secure_sysmem is the secure view 322 * of the system, and the first flash device should be made visible only 323 * there. The second flash device is visible to both secure and nonsecure. 324 */ 325 hwaddr flashsize = sbsa_ref_memmap[SBSA_FLASH].size / 2; 326 hwaddr flashbase = sbsa_ref_memmap[SBSA_FLASH].base; 327 328 sbsa_flash_map1(sms->flash[0], flashbase, flashsize, 329 secure_sysmem); 330 sbsa_flash_map1(sms->flash[1], flashbase + flashsize, flashsize, 331 sysmem); 332 } 333 334 static bool sbsa_firmware_init(SBSAMachineState *sms, 335 MemoryRegion *sysmem, 336 MemoryRegion *secure_sysmem) 337 { 338 const char *bios_name; 339 int i; 340 BlockBackend *pflash_blk0; 341 342 /* Map legacy -drive if=pflash to machine properties */ 343 for (i = 0; i < ARRAY_SIZE(sms->flash); i++) { 344 pflash_cfi01_legacy_drive(sms->flash[i], 345 drive_get(IF_PFLASH, 0, i)); 346 } 347 348 sbsa_flash_map(sms, sysmem, secure_sysmem); 349 350 pflash_blk0 = pflash_cfi01_get_blk(sms->flash[0]); 351 352 bios_name = MACHINE(sms)->firmware; 353 if (bios_name) { 354 char *fname; 355 MemoryRegion *mr; 356 int image_size; 357 358 if (pflash_blk0) { 359 error_report("The contents of the first flash device may be " 360 "specified with -bios or with -drive if=pflash... " 361 "but you cannot use both options at once"); 362 exit(1); 363 } 364 365 /* Fall back to -bios */ 366 367 fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 368 if (!fname) { 369 error_report("Could not find ROM image '%s'", bios_name); 370 exit(1); 371 } 372 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(sms->flash[0]), 0); 373 image_size = load_image_mr(fname, mr); 374 g_free(fname); 375 if (image_size < 0) { 376 error_report("Could not load ROM image '%s'", bios_name); 377 exit(1); 378 } 379 } 380 381 return pflash_blk0 || bios_name; 382 } 383 384 static void create_secure_ram(SBSAMachineState *sms, 385 MemoryRegion *secure_sysmem) 386 { 387 MemoryRegion *secram = g_new(MemoryRegion, 1); 388 hwaddr base = sbsa_ref_memmap[SBSA_SECURE_MEM].base; 389 hwaddr size = sbsa_ref_memmap[SBSA_SECURE_MEM].size; 390 391 memory_region_init_ram(secram, NULL, "sbsa-ref.secure-ram", size, 392 &error_fatal); 393 memory_region_add_subregion(secure_sysmem, base, secram); 394 } 395 396 static void create_gic(SBSAMachineState *sms) 397 { 398 unsigned int smp_cpus = MACHINE(sms)->smp.cpus; 399 SysBusDevice *gicbusdev; 400 const char *gictype; 401 uint32_t redist0_capacity, redist0_count; 402 int i; 403 404 gictype = gicv3_class_name(); 405 406 sms->gic = qdev_new(gictype); 407 qdev_prop_set_uint32(sms->gic, "revision", 3); 408 qdev_prop_set_uint32(sms->gic, "num-cpu", smp_cpus); 409 /* 410 * Note that the num-irq property counts both internal and external 411 * interrupts; there are always 32 of the former (mandated by GIC spec). 412 */ 413 qdev_prop_set_uint32(sms->gic, "num-irq", NUM_IRQS + 32); 414 qdev_prop_set_bit(sms->gic, "has-security-extensions", true); 415 416 redist0_capacity = 417 sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE; 418 redist0_count = MIN(smp_cpus, redist0_capacity); 419 420 qdev_prop_set_uint32(sms->gic, "len-redist-region-count", 1); 421 qdev_prop_set_uint32(sms->gic, "redist-region-count[0]", redist0_count); 422 423 gicbusdev = SYS_BUS_DEVICE(sms->gic); 424 sysbus_realize_and_unref(gicbusdev, &error_fatal); 425 sysbus_mmio_map(gicbusdev, 0, sbsa_ref_memmap[SBSA_GIC_DIST].base); 426 sysbus_mmio_map(gicbusdev, 1, sbsa_ref_memmap[SBSA_GIC_REDIST].base); 427 428 /* 429 * Wire the outputs from each CPU's generic timer and the GICv3 430 * maintenance interrupt signal to the appropriate GIC PPI inputs, 431 * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs. 432 */ 433 for (i = 0; i < smp_cpus; i++) { 434 DeviceState *cpudev = DEVICE(qemu_get_cpu(i)); 435 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS; 436 int irq; 437 /* 438 * Mapping from the output timer irq lines from the CPU to the 439 * GIC PPI inputs used for this board. 440 */ 441 const int timer_irq[] = { 442 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ, 443 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ, 444 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ, 445 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ, 446 }; 447 448 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) { 449 qdev_connect_gpio_out(cpudev, irq, 450 qdev_get_gpio_in(sms->gic, 451 ppibase + timer_irq[irq])); 452 } 453 454 qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0, 455 qdev_get_gpio_in(sms->gic, ppibase 456 + ARCH_GIC_MAINT_IRQ)); 457 qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0, 458 qdev_get_gpio_in(sms->gic, ppibase 459 + VIRTUAL_PMU_IRQ)); 460 461 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ)); 462 sysbus_connect_irq(gicbusdev, i + smp_cpus, 463 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ)); 464 sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus, 465 qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ)); 466 sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus, 467 qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ)); 468 } 469 } 470 471 static void create_uart(const SBSAMachineState *sms, int uart, 472 MemoryRegion *mem, Chardev *chr) 473 { 474 hwaddr base = sbsa_ref_memmap[uart].base; 475 int irq = sbsa_ref_irqmap[uart]; 476 DeviceState *dev = qdev_new(TYPE_PL011); 477 SysBusDevice *s = SYS_BUS_DEVICE(dev); 478 479 qdev_prop_set_chr(dev, "chardev", chr); 480 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 481 memory_region_add_subregion(mem, base, 482 sysbus_mmio_get_region(s, 0)); 483 sysbus_connect_irq(s, 0, qdev_get_gpio_in(sms->gic, irq)); 484 } 485 486 static void create_rtc(const SBSAMachineState *sms) 487 { 488 hwaddr base = sbsa_ref_memmap[SBSA_RTC].base; 489 int irq = sbsa_ref_irqmap[SBSA_RTC]; 490 491 sysbus_create_simple("pl031", base, qdev_get_gpio_in(sms->gic, irq)); 492 } 493 494 static void create_wdt(const SBSAMachineState *sms) 495 { 496 hwaddr rbase = sbsa_ref_memmap[SBSA_GWDT_REFRESH].base; 497 hwaddr cbase = sbsa_ref_memmap[SBSA_GWDT_CONTROL].base; 498 DeviceState *dev = qdev_new(TYPE_WDT_SBSA); 499 SysBusDevice *s = SYS_BUS_DEVICE(dev); 500 int irq = sbsa_ref_irqmap[SBSA_GWDT_WS0]; 501 502 sysbus_realize_and_unref(s, &error_fatal); 503 sysbus_mmio_map(s, 0, rbase); 504 sysbus_mmio_map(s, 1, cbase); 505 sysbus_connect_irq(s, 0, qdev_get_gpio_in(sms->gic, irq)); 506 } 507 508 static DeviceState *gpio_key_dev; 509 static void sbsa_ref_powerdown_req(Notifier *n, void *opaque) 510 { 511 /* use gpio Pin 3 for power button event */ 512 qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1); 513 } 514 515 static Notifier sbsa_ref_powerdown_notifier = { 516 .notify = sbsa_ref_powerdown_req 517 }; 518 519 static void create_gpio(const SBSAMachineState *sms) 520 { 521 DeviceState *pl061_dev; 522 hwaddr base = sbsa_ref_memmap[SBSA_GPIO].base; 523 int irq = sbsa_ref_irqmap[SBSA_GPIO]; 524 525 pl061_dev = sysbus_create_simple("pl061", base, 526 qdev_get_gpio_in(sms->gic, irq)); 527 528 gpio_key_dev = sysbus_create_simple("gpio-key", -1, 529 qdev_get_gpio_in(pl061_dev, 3)); 530 531 /* connect powerdown request */ 532 qemu_register_powerdown_notifier(&sbsa_ref_powerdown_notifier); 533 } 534 535 static void create_ahci(const SBSAMachineState *sms) 536 { 537 hwaddr base = sbsa_ref_memmap[SBSA_AHCI].base; 538 int irq = sbsa_ref_irqmap[SBSA_AHCI]; 539 DeviceState *dev; 540 DriveInfo *hd[NUM_SATA_PORTS]; 541 SysbusAHCIState *sysahci; 542 AHCIState *ahci; 543 int i; 544 545 dev = qdev_new("sysbus-ahci"); 546 qdev_prop_set_uint32(dev, "num-ports", NUM_SATA_PORTS); 547 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 548 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 549 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(sms->gic, irq)); 550 551 sysahci = SYSBUS_AHCI(dev); 552 ahci = &sysahci->ahci; 553 ide_drive_get(hd, ARRAY_SIZE(hd)); 554 for (i = 0; i < ahci->ports; i++) { 555 if (hd[i] == NULL) { 556 continue; 557 } 558 ide_create_drive(&ahci->dev[i].port, 0, hd[i]); 559 } 560 } 561 562 static void create_ehci(const SBSAMachineState *sms) 563 { 564 hwaddr base = sbsa_ref_memmap[SBSA_EHCI].base; 565 int irq = sbsa_ref_irqmap[SBSA_EHCI]; 566 567 sysbus_create_simple("platform-ehci-usb", base, 568 qdev_get_gpio_in(sms->gic, irq)); 569 } 570 571 static void create_smmu(const SBSAMachineState *sms, PCIBus *bus) 572 { 573 hwaddr base = sbsa_ref_memmap[SBSA_SMMU].base; 574 int irq = sbsa_ref_irqmap[SBSA_SMMU]; 575 DeviceState *dev; 576 int i; 577 578 dev = qdev_new(TYPE_ARM_SMMUV3); 579 580 object_property_set_link(OBJECT(dev), "primary-bus", OBJECT(bus), 581 &error_abort); 582 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 583 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 584 for (i = 0; i < NUM_SMMU_IRQS; i++) { 585 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 586 qdev_get_gpio_in(sms->gic, irq + i)); 587 } 588 } 589 590 static void create_pcie(SBSAMachineState *sms) 591 { 592 hwaddr base_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].base; 593 hwaddr size_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].size; 594 hwaddr base_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].base; 595 hwaddr size_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].size; 596 hwaddr base_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].base; 597 hwaddr size_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].size; 598 hwaddr base_pio = sbsa_ref_memmap[SBSA_PCIE_PIO].base; 599 int irq = sbsa_ref_irqmap[SBSA_PCIE]; 600 MemoryRegion *mmio_alias, *mmio_alias_high, *mmio_reg; 601 MemoryRegion *ecam_alias, *ecam_reg; 602 DeviceState *dev; 603 PCIHostState *pci; 604 int i; 605 606 dev = qdev_new(TYPE_GPEX_HOST); 607 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 608 609 /* Map ECAM space */ 610 ecam_alias = g_new0(MemoryRegion, 1); 611 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 612 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam", 613 ecam_reg, 0, size_ecam); 614 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias); 615 616 /* Map the MMIO space */ 617 mmio_alias = g_new0(MemoryRegion, 1); 618 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 619 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio", 620 mmio_reg, base_mmio, size_mmio); 621 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias); 622 623 /* Map the MMIO_HIGH space */ 624 mmio_alias_high = g_new0(MemoryRegion, 1); 625 memory_region_init_alias(mmio_alias_high, OBJECT(dev), "pcie-mmio-high", 626 mmio_reg, base_mmio_high, size_mmio_high); 627 memory_region_add_subregion(get_system_memory(), base_mmio_high, 628 mmio_alias_high); 629 630 /* Map IO port space */ 631 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio); 632 633 for (i = 0; i < GPEX_NUM_IRQS; i++) { 634 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 635 qdev_get_gpio_in(sms->gic, irq + i)); 636 gpex_set_irq_num(GPEX_HOST(dev), i, irq + i); 637 } 638 639 pci = PCI_HOST_BRIDGE(dev); 640 if (pci->bus) { 641 for (i = 0; i < nb_nics; i++) { 642 NICInfo *nd = &nd_table[i]; 643 644 if (!nd->model) { 645 nd->model = g_strdup("e1000e"); 646 } 647 648 pci_nic_init_nofail(nd, pci->bus, nd->model, NULL); 649 } 650 } 651 652 pci_create_simple(pci->bus, -1, "VGA"); 653 654 create_smmu(sms, pci->bus); 655 } 656 657 static void *sbsa_ref_dtb(const struct arm_boot_info *binfo, int *fdt_size) 658 { 659 const SBSAMachineState *board = container_of(binfo, SBSAMachineState, 660 bootinfo); 661 662 *fdt_size = board->fdt_size; 663 return board->fdt; 664 } 665 666 static void create_secure_ec(MemoryRegion *mem) 667 { 668 hwaddr base = sbsa_ref_memmap[SBSA_SECURE_EC].base; 669 DeviceState *dev = qdev_new("sbsa-ec"); 670 SysBusDevice *s = SYS_BUS_DEVICE(dev); 671 672 memory_region_add_subregion(mem, base, 673 sysbus_mmio_get_region(s, 0)); 674 } 675 676 static void sbsa_ref_init(MachineState *machine) 677 { 678 unsigned int smp_cpus = machine->smp.cpus; 679 unsigned int max_cpus = machine->smp.max_cpus; 680 SBSAMachineState *sms = SBSA_MACHINE(machine); 681 MachineClass *mc = MACHINE_GET_CLASS(machine); 682 MemoryRegion *sysmem = get_system_memory(); 683 MemoryRegion *secure_sysmem = g_new(MemoryRegion, 1); 684 bool firmware_loaded; 685 const CPUArchIdList *possible_cpus; 686 int n, sbsa_max_cpus; 687 688 if (!cpu_type_valid(machine->cpu_type)) { 689 error_report("sbsa-ref: CPU type %s not supported", machine->cpu_type); 690 exit(1); 691 } 692 693 if (kvm_enabled()) { 694 error_report("sbsa-ref: KVM is not supported for this machine"); 695 exit(1); 696 } 697 698 /* 699 * The Secure view of the world is the same as the NonSecure, 700 * but with a few extra devices. Create it as a container region 701 * containing the system memory at low priority; any secure-only 702 * devices go in at higher priority and take precedence. 703 */ 704 memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory", 705 UINT64_MAX); 706 memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1); 707 708 firmware_loaded = sbsa_firmware_init(sms, sysmem, secure_sysmem); 709 710 /* 711 * This machine has EL3 enabled, external firmware should supply PSCI 712 * implementation, so the QEMU's internal PSCI is disabled. 713 */ 714 sms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED; 715 716 sbsa_max_cpus = sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE; 717 718 if (max_cpus > sbsa_max_cpus) { 719 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs " 720 "supported by machine 'sbsa-ref' (%d)", 721 max_cpus, sbsa_max_cpus); 722 exit(1); 723 } 724 725 sms->smp_cpus = smp_cpus; 726 727 if (machine->ram_size > sbsa_ref_memmap[SBSA_MEM].size) { 728 error_report("sbsa-ref: cannot model more than %dGB RAM", RAMLIMIT_GB); 729 exit(1); 730 } 731 732 possible_cpus = mc->possible_cpu_arch_ids(machine); 733 for (n = 0; n < possible_cpus->len; n++) { 734 Object *cpuobj; 735 CPUState *cs; 736 737 if (n >= smp_cpus) { 738 break; 739 } 740 741 cpuobj = object_new(possible_cpus->cpus[n].type); 742 object_property_set_int(cpuobj, "mp-affinity", 743 possible_cpus->cpus[n].arch_id, NULL); 744 745 cs = CPU(cpuobj); 746 cs->cpu_index = n; 747 748 numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj), 749 &error_fatal); 750 751 if (object_property_find(cpuobj, "reset-cbar")) { 752 object_property_set_int(cpuobj, "reset-cbar", 753 sbsa_ref_memmap[SBSA_CPUPERIPHS].base, 754 &error_abort); 755 } 756 757 object_property_set_link(cpuobj, "memory", OBJECT(sysmem), 758 &error_abort); 759 760 object_property_set_link(cpuobj, "secure-memory", 761 OBJECT(secure_sysmem), &error_abort); 762 763 qdev_realize(DEVICE(cpuobj), NULL, &error_fatal); 764 object_unref(cpuobj); 765 } 766 767 memory_region_add_subregion(sysmem, sbsa_ref_memmap[SBSA_MEM].base, 768 machine->ram); 769 770 create_fdt(sms); 771 772 create_secure_ram(sms, secure_sysmem); 773 774 create_gic(sms); 775 776 create_uart(sms, SBSA_UART, sysmem, serial_hd(0)); 777 create_uart(sms, SBSA_SECURE_UART, secure_sysmem, serial_hd(1)); 778 /* Second secure UART for RAS and MM from EL0 */ 779 create_uart(sms, SBSA_SECURE_UART_MM, secure_sysmem, serial_hd(2)); 780 781 create_rtc(sms); 782 783 create_wdt(sms); 784 785 create_gpio(sms); 786 787 create_ahci(sms); 788 789 create_ehci(sms); 790 791 create_pcie(sms); 792 793 create_secure_ec(secure_sysmem); 794 795 sms->bootinfo.ram_size = machine->ram_size; 796 sms->bootinfo.board_id = -1; 797 sms->bootinfo.loader_start = sbsa_ref_memmap[SBSA_MEM].base; 798 sms->bootinfo.get_dtb = sbsa_ref_dtb; 799 sms->bootinfo.firmware_loaded = firmware_loaded; 800 arm_load_kernel(ARM_CPU(first_cpu), machine, &sms->bootinfo); 801 } 802 803 static const CPUArchIdList *sbsa_ref_possible_cpu_arch_ids(MachineState *ms) 804 { 805 unsigned int max_cpus = ms->smp.max_cpus; 806 SBSAMachineState *sms = SBSA_MACHINE(ms); 807 int n; 808 809 if (ms->possible_cpus) { 810 assert(ms->possible_cpus->len == max_cpus); 811 return ms->possible_cpus; 812 } 813 814 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 815 sizeof(CPUArchId) * max_cpus); 816 ms->possible_cpus->len = max_cpus; 817 for (n = 0; n < ms->possible_cpus->len; n++) { 818 ms->possible_cpus->cpus[n].type = ms->cpu_type; 819 ms->possible_cpus->cpus[n].arch_id = 820 sbsa_ref_cpu_mp_affinity(sms, n); 821 ms->possible_cpus->cpus[n].props.has_thread_id = true; 822 ms->possible_cpus->cpus[n].props.thread_id = n; 823 } 824 return ms->possible_cpus; 825 } 826 827 static CpuInstanceProperties 828 sbsa_ref_cpu_index_to_props(MachineState *ms, unsigned cpu_index) 829 { 830 MachineClass *mc = MACHINE_GET_CLASS(ms); 831 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 832 833 assert(cpu_index < possible_cpus->len); 834 return possible_cpus->cpus[cpu_index].props; 835 } 836 837 static int64_t 838 sbsa_ref_get_default_cpu_node_id(const MachineState *ms, int idx) 839 { 840 return idx % ms->numa_state->num_nodes; 841 } 842 843 static void sbsa_ref_instance_init(Object *obj) 844 { 845 SBSAMachineState *sms = SBSA_MACHINE(obj); 846 847 sbsa_flash_create(sms); 848 } 849 850 static void sbsa_ref_class_init(ObjectClass *oc, void *data) 851 { 852 MachineClass *mc = MACHINE_CLASS(oc); 853 854 mc->init = sbsa_ref_init; 855 mc->desc = "QEMU 'SBSA Reference' ARM Virtual Machine"; 856 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a57"); 857 mc->max_cpus = 512; 858 mc->pci_allow_0_address = true; 859 mc->minimum_page_bits = 12; 860 mc->block_default_type = IF_IDE; 861 mc->no_cdrom = 1; 862 mc->default_ram_size = 1 * GiB; 863 mc->default_ram_id = "sbsa-ref.ram"; 864 mc->default_cpus = 4; 865 mc->possible_cpu_arch_ids = sbsa_ref_possible_cpu_arch_ids; 866 mc->cpu_index_to_instance_props = sbsa_ref_cpu_index_to_props; 867 mc->get_default_cpu_node_id = sbsa_ref_get_default_cpu_node_id; 868 } 869 870 static const TypeInfo sbsa_ref_info = { 871 .name = TYPE_SBSA_MACHINE, 872 .parent = TYPE_MACHINE, 873 .instance_init = sbsa_ref_instance_init, 874 .class_init = sbsa_ref_class_init, 875 .instance_size = sizeof(SBSAMachineState), 876 }; 877 878 static void sbsa_ref_machine_init(void) 879 { 880 type_register_static(&sbsa_ref_info); 881 } 882 883 type_init(sbsa_ref_machine_init); 884