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