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