xref: /qemu/hw/arm/sbsa-ref.c (revision 3f462bf0f6ea6382dd1502d4eb1fcd33c8e774f5)
164580903SHongbo Zhang /*
264580903SHongbo Zhang  * ARM SBSA Reference Platform emulation
364580903SHongbo Zhang  *
464580903SHongbo Zhang  * Copyright (c) 2018 Linaro Limited
564580903SHongbo Zhang  * Written by Hongbo Zhang <hongbo.zhang@linaro.org>
664580903SHongbo Zhang  *
764580903SHongbo Zhang  * This program is free software; you can redistribute it and/or modify it
864580903SHongbo Zhang  * under the terms and conditions of the GNU General Public License,
964580903SHongbo Zhang  * version 2 or later, as published by the Free Software Foundation.
1064580903SHongbo Zhang  *
1164580903SHongbo Zhang  * This program is distributed in the hope it will be useful, but WITHOUT
1264580903SHongbo Zhang  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1364580903SHongbo Zhang  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1464580903SHongbo Zhang  * more details.
1564580903SHongbo Zhang  *
1664580903SHongbo Zhang  * You should have received a copy of the GNU General Public License along with
1764580903SHongbo Zhang  * this program.  If not, see <http://www.gnu.org/licenses/>.
1864580903SHongbo Zhang  */
1964580903SHongbo Zhang 
2064580903SHongbo Zhang #include "qemu/osdep.h"
21e9fdf453SHongbo Zhang #include "qemu-common.h"
2264580903SHongbo Zhang #include "qapi/error.h"
2364580903SHongbo Zhang #include "qemu/error-report.h"
2464580903SHongbo Zhang #include "qemu/units.h"
25e9fdf453SHongbo Zhang #include "sysemu/device_tree.h"
2664580903SHongbo Zhang #include "sysemu/numa.h"
2754d31236SMarkus Armbruster #include "sysemu/runstate.h"
2864580903SHongbo Zhang #include "sysemu/sysemu.h"
2964580903SHongbo Zhang #include "exec/address-spaces.h"
3064580903SHongbo Zhang #include "exec/hwaddr.h"
3164580903SHongbo Zhang #include "kvm_arm.h"
3264580903SHongbo Zhang #include "hw/arm/boot.h"
33e9fdf453SHongbo Zhang #include "hw/block/flash.h"
3464580903SHongbo Zhang #include "hw/boards.h"
35e9fdf453SHongbo Zhang #include "hw/ide/internal.h"
36e9fdf453SHongbo Zhang #include "hw/ide/ahci_internal.h"
3764580903SHongbo Zhang #include "hw/intc/arm_gicv3_common.h"
38e9fdf453SHongbo Zhang #include "hw/loader.h"
39e9fdf453SHongbo Zhang #include "hw/pci-host/gpex.h"
40a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
41e9fdf453SHongbo Zhang #include "hw/usb.h"
42d8f6d15fSGavin Shan #include "hw/char/pl011.h"
43e9fdf453SHongbo Zhang #include "net/net.h"
4464580903SHongbo Zhang 
4564580903SHongbo Zhang #define RAMLIMIT_GB 8192
4664580903SHongbo Zhang #define RAMLIMIT_BYTES (RAMLIMIT_GB * GiB)
4764580903SHongbo Zhang 
48e9fdf453SHongbo Zhang #define NUM_IRQS        256
49e9fdf453SHongbo Zhang #define NUM_SMMU_IRQS   4
50e9fdf453SHongbo Zhang #define NUM_SATA_PORTS  6
51e9fdf453SHongbo Zhang 
52e9fdf453SHongbo Zhang #define VIRTUAL_PMU_IRQ        7
53e9fdf453SHongbo Zhang #define ARCH_GIC_MAINT_IRQ     9
54e9fdf453SHongbo Zhang #define ARCH_TIMER_VIRT_IRQ    11
55e9fdf453SHongbo Zhang #define ARCH_TIMER_S_EL1_IRQ   13
56e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL1_IRQ  14
57e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL2_IRQ  10
58e9fdf453SHongbo Zhang 
5964580903SHongbo Zhang enum {
6064580903SHongbo Zhang     SBSA_FLASH,
6164580903SHongbo Zhang     SBSA_MEM,
6264580903SHongbo Zhang     SBSA_CPUPERIPHS,
6364580903SHongbo Zhang     SBSA_GIC_DIST,
6464580903SHongbo Zhang     SBSA_GIC_REDIST,
65*3f462bf0SGraeme Gregory     SBSA_SECURE_EC,
6664580903SHongbo Zhang     SBSA_SMMU,
6764580903SHongbo Zhang     SBSA_UART,
6864580903SHongbo Zhang     SBSA_RTC,
6964580903SHongbo Zhang     SBSA_PCIE,
7064580903SHongbo Zhang     SBSA_PCIE_MMIO,
7164580903SHongbo Zhang     SBSA_PCIE_MMIO_HIGH,
7264580903SHongbo Zhang     SBSA_PCIE_PIO,
7364580903SHongbo Zhang     SBSA_PCIE_ECAM,
7464580903SHongbo Zhang     SBSA_GPIO,
7564580903SHongbo Zhang     SBSA_SECURE_UART,
7664580903SHongbo Zhang     SBSA_SECURE_UART_MM,
7764580903SHongbo Zhang     SBSA_SECURE_MEM,
7864580903SHongbo Zhang     SBSA_AHCI,
7964580903SHongbo Zhang     SBSA_EHCI,
8064580903SHongbo Zhang };
8164580903SHongbo Zhang 
8264580903SHongbo Zhang typedef struct MemMapEntry {
8364580903SHongbo Zhang     hwaddr base;
8464580903SHongbo Zhang     hwaddr size;
8564580903SHongbo Zhang } MemMapEntry;
8664580903SHongbo Zhang 
8764580903SHongbo Zhang typedef struct {
8864580903SHongbo Zhang     MachineState parent;
8964580903SHongbo Zhang     struct arm_boot_info bootinfo;
9064580903SHongbo Zhang     int smp_cpus;
9164580903SHongbo Zhang     void *fdt;
9264580903SHongbo Zhang     int fdt_size;
9364580903SHongbo Zhang     int psci_conduit;
9448ba18e6SPhilippe Mathieu-Daudé     DeviceState *gic;
95e9fdf453SHongbo Zhang     PFlashCFI01 *flash[2];
9664580903SHongbo Zhang } SBSAMachineState;
9764580903SHongbo Zhang 
9864580903SHongbo Zhang #define TYPE_SBSA_MACHINE   MACHINE_TYPE_NAME("sbsa-ref")
9964580903SHongbo Zhang #define SBSA_MACHINE(obj) \
10064580903SHongbo Zhang     OBJECT_CHECK(SBSAMachineState, (obj), TYPE_SBSA_MACHINE)
10164580903SHongbo Zhang 
10264580903SHongbo Zhang static const MemMapEntry sbsa_ref_memmap[] = {
10364580903SHongbo Zhang     /* 512M boot ROM */
10464580903SHongbo Zhang     [SBSA_FLASH] =              {          0, 0x20000000 },
10564580903SHongbo Zhang     /* 512M secure memory */
10664580903SHongbo Zhang     [SBSA_SECURE_MEM] =         { 0x20000000, 0x20000000 },
10764580903SHongbo Zhang     /* Space reserved for CPU peripheral devices */
10864580903SHongbo Zhang     [SBSA_CPUPERIPHS] =         { 0x40000000, 0x00040000 },
10964580903SHongbo Zhang     [SBSA_GIC_DIST] =           { 0x40060000, 0x00010000 },
11064580903SHongbo Zhang     [SBSA_GIC_REDIST] =         { 0x40080000, 0x04000000 },
111*3f462bf0SGraeme Gregory     [SBSA_SECURE_EC] =          { 0x50000000, 0x00001000 },
11264580903SHongbo Zhang     [SBSA_UART] =               { 0x60000000, 0x00001000 },
11364580903SHongbo Zhang     [SBSA_RTC] =                { 0x60010000, 0x00001000 },
11464580903SHongbo Zhang     [SBSA_GPIO] =               { 0x60020000, 0x00001000 },
11564580903SHongbo Zhang     [SBSA_SECURE_UART] =        { 0x60030000, 0x00001000 },
11664580903SHongbo Zhang     [SBSA_SECURE_UART_MM] =     { 0x60040000, 0x00001000 },
11764580903SHongbo Zhang     [SBSA_SMMU] =               { 0x60050000, 0x00020000 },
11864580903SHongbo Zhang     /* Space here reserved for more SMMUs */
11964580903SHongbo Zhang     [SBSA_AHCI] =               { 0x60100000, 0x00010000 },
12064580903SHongbo Zhang     [SBSA_EHCI] =               { 0x60110000, 0x00010000 },
12164580903SHongbo Zhang     /* Space here reserved for other devices */
12264580903SHongbo Zhang     [SBSA_PCIE_PIO] =           { 0x7fff0000, 0x00010000 },
12364580903SHongbo Zhang     /* 32-bit address PCIE MMIO space */
12464580903SHongbo Zhang     [SBSA_PCIE_MMIO] =          { 0x80000000, 0x70000000 },
12564580903SHongbo Zhang     /* 256M PCIE ECAM space */
12664580903SHongbo Zhang     [SBSA_PCIE_ECAM] =          { 0xf0000000, 0x10000000 },
12764580903SHongbo Zhang     /* ~1TB PCIE MMIO space (4GB to 1024GB boundary) */
12864580903SHongbo Zhang     [SBSA_PCIE_MMIO_HIGH] =     { 0x100000000ULL, 0xFF00000000ULL },
12964580903SHongbo Zhang     [SBSA_MEM] =                { 0x10000000000ULL, RAMLIMIT_BYTES },
13064580903SHongbo Zhang };
13164580903SHongbo Zhang 
132e9fdf453SHongbo Zhang static const int sbsa_ref_irqmap[] = {
133e9fdf453SHongbo Zhang     [SBSA_UART] = 1,
134e9fdf453SHongbo Zhang     [SBSA_RTC] = 2,
135e9fdf453SHongbo Zhang     [SBSA_PCIE] = 3, /* ... to 6 */
136e9fdf453SHongbo Zhang     [SBSA_GPIO] = 7,
137e9fdf453SHongbo Zhang     [SBSA_SECURE_UART] = 8,
138e9fdf453SHongbo Zhang     [SBSA_SECURE_UART_MM] = 9,
139e9fdf453SHongbo Zhang     [SBSA_AHCI] = 10,
140e9fdf453SHongbo Zhang     [SBSA_EHCI] = 11,
141e9fdf453SHongbo Zhang };
142e9fdf453SHongbo Zhang 
143999f6ebdSLeif Lindholm static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx)
144999f6ebdSLeif Lindholm {
145999f6ebdSLeif Lindholm     uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
146999f6ebdSLeif Lindholm     return arm_cpu_mp_affinity(idx, clustersz);
147999f6ebdSLeif Lindholm }
148999f6ebdSLeif Lindholm 
149e9fdf453SHongbo Zhang /*
150e9fdf453SHongbo Zhang  * Firmware on this machine only uses ACPI table to load OS, these limited
151e9fdf453SHongbo Zhang  * device tree nodes are just to let firmware know the info which varies from
152e9fdf453SHongbo Zhang  * command line parameters, so it is not necessary to be fully compatible
153e9fdf453SHongbo Zhang  * with the kernel CPU and NUMA binding rules.
154e9fdf453SHongbo Zhang  */
155e9fdf453SHongbo Zhang static void create_fdt(SBSAMachineState *sms)
156e9fdf453SHongbo Zhang {
157e9fdf453SHongbo Zhang     void *fdt = create_device_tree(&sms->fdt_size);
158e9fdf453SHongbo Zhang     const MachineState *ms = MACHINE(sms);
159aa570207STao Xu     int nb_numa_nodes = ms->numa_state->num_nodes;
160e9fdf453SHongbo Zhang     int cpu;
161e9fdf453SHongbo Zhang 
162e9fdf453SHongbo Zhang     if (!fdt) {
163e9fdf453SHongbo Zhang         error_report("create_device_tree() failed");
164e9fdf453SHongbo Zhang         exit(1);
165e9fdf453SHongbo Zhang     }
166e9fdf453SHongbo Zhang 
167e9fdf453SHongbo Zhang     sms->fdt = fdt;
168e9fdf453SHongbo Zhang 
169e9fdf453SHongbo Zhang     qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,sbsa-ref");
170e9fdf453SHongbo Zhang     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
171e9fdf453SHongbo Zhang     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
172e9fdf453SHongbo Zhang 
173118154b7STao Xu     if (ms->numa_state->have_numa_distance) {
174e9fdf453SHongbo Zhang         int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
175e9fdf453SHongbo Zhang         uint32_t *matrix = g_malloc0(size);
176e9fdf453SHongbo Zhang         int idx, i, j;
177e9fdf453SHongbo Zhang 
178e9fdf453SHongbo Zhang         for (i = 0; i < nb_numa_nodes; i++) {
179e9fdf453SHongbo Zhang             for (j = 0; j < nb_numa_nodes; j++) {
180e9fdf453SHongbo Zhang                 idx = (i * nb_numa_nodes + j) * 3;
181e9fdf453SHongbo Zhang                 matrix[idx + 0] = cpu_to_be32(i);
182e9fdf453SHongbo Zhang                 matrix[idx + 1] = cpu_to_be32(j);
1837e721e7bSTao Xu                 matrix[idx + 2] =
1847e721e7bSTao Xu                     cpu_to_be32(ms->numa_state->nodes[i].distance[j]);
185e9fdf453SHongbo Zhang             }
186e9fdf453SHongbo Zhang         }
187e9fdf453SHongbo Zhang 
188e9fdf453SHongbo Zhang         qemu_fdt_add_subnode(fdt, "/distance-map");
189e9fdf453SHongbo Zhang         qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
190e9fdf453SHongbo Zhang                          matrix, size);
191e9fdf453SHongbo Zhang         g_free(matrix);
192e9fdf453SHongbo Zhang     }
193e9fdf453SHongbo Zhang 
194999f6ebdSLeif Lindholm     /*
195999f6ebdSLeif Lindholm      * From Documentation/devicetree/bindings/arm/cpus.yaml
196999f6ebdSLeif Lindholm      *  On ARM v8 64-bit systems this property is required
197999f6ebdSLeif Lindholm      *    and matches the MPIDR_EL1 register affinity bits.
198999f6ebdSLeif Lindholm      *
199999f6ebdSLeif Lindholm      *    * If cpus node's #address-cells property is set to 2
200999f6ebdSLeif Lindholm      *
201999f6ebdSLeif Lindholm      *      The first reg cell bits [7:0] must be set to
202999f6ebdSLeif Lindholm      *      bits [39:32] of MPIDR_EL1.
203999f6ebdSLeif Lindholm      *
204999f6ebdSLeif Lindholm      *      The second reg cell bits [23:0] must be set to
205999f6ebdSLeif Lindholm      *      bits [23:0] of MPIDR_EL1.
206999f6ebdSLeif Lindholm      */
207e9fdf453SHongbo Zhang     qemu_fdt_add_subnode(sms->fdt, "/cpus");
208999f6ebdSLeif Lindholm     qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#address-cells", 2);
209999f6ebdSLeif Lindholm     qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#size-cells", 0x0);
210e9fdf453SHongbo Zhang 
211e9fdf453SHongbo Zhang     for (cpu = sms->smp_cpus - 1; cpu >= 0; cpu--) {
212e9fdf453SHongbo Zhang         char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
213e9fdf453SHongbo Zhang         ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
214e9fdf453SHongbo Zhang         CPUState *cs = CPU(armcpu);
215999f6ebdSLeif Lindholm         uint64_t mpidr = sbsa_ref_cpu_mp_affinity(sms, cpu);
216e9fdf453SHongbo Zhang 
217e9fdf453SHongbo Zhang         qemu_fdt_add_subnode(sms->fdt, nodename);
218999f6ebdSLeif Lindholm         qemu_fdt_setprop_u64(sms->fdt, nodename, "reg", mpidr);
219e9fdf453SHongbo Zhang 
220e9fdf453SHongbo Zhang         if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
221e9fdf453SHongbo Zhang             qemu_fdt_setprop_cell(sms->fdt, nodename, "numa-node-id",
222e9fdf453SHongbo Zhang                 ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
223e9fdf453SHongbo Zhang         }
224e9fdf453SHongbo Zhang 
225e9fdf453SHongbo Zhang         g_free(nodename);
226e9fdf453SHongbo Zhang     }
227e9fdf453SHongbo Zhang }
228e9fdf453SHongbo Zhang 
229e9fdf453SHongbo Zhang #define SBSA_FLASH_SECTOR_SIZE (256 * KiB)
230e9fdf453SHongbo Zhang 
231e9fdf453SHongbo Zhang static PFlashCFI01 *sbsa_flash_create1(SBSAMachineState *sms,
232e9fdf453SHongbo Zhang                                         const char *name,
233e9fdf453SHongbo Zhang                                         const char *alias_prop_name)
234e9fdf453SHongbo Zhang {
235e9fdf453SHongbo Zhang     /*
236e9fdf453SHongbo Zhang      * Create a single flash device.  We use the same parameters as
237e9fdf453SHongbo Zhang      * the flash devices on the Versatile Express board.
238e9fdf453SHongbo Zhang      */
239df707969SMarkus Armbruster     DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
240e9fdf453SHongbo Zhang 
241e9fdf453SHongbo Zhang     qdev_prop_set_uint64(dev, "sector-length", SBSA_FLASH_SECTOR_SIZE);
242e9fdf453SHongbo Zhang     qdev_prop_set_uint8(dev, "width", 4);
243e9fdf453SHongbo Zhang     qdev_prop_set_uint8(dev, "device-width", 2);
244e9fdf453SHongbo Zhang     qdev_prop_set_bit(dev, "big-endian", false);
245e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id0", 0x89);
246e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id1", 0x18);
247e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id2", 0x00);
248e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id3", 0x00);
249e9fdf453SHongbo Zhang     qdev_prop_set_string(dev, "name", name);
250d2623129SMarkus Armbruster     object_property_add_child(OBJECT(sms), name, OBJECT(dev));
251e9fdf453SHongbo Zhang     object_property_add_alias(OBJECT(sms), alias_prop_name,
252d2623129SMarkus Armbruster                               OBJECT(dev), "drive");
253e9fdf453SHongbo Zhang     return PFLASH_CFI01(dev);
254e9fdf453SHongbo Zhang }
255e9fdf453SHongbo Zhang 
256e9fdf453SHongbo Zhang static void sbsa_flash_create(SBSAMachineState *sms)
257e9fdf453SHongbo Zhang {
258e9fdf453SHongbo Zhang     sms->flash[0] = sbsa_flash_create1(sms, "sbsa.flash0", "pflash0");
259e9fdf453SHongbo Zhang     sms->flash[1] = sbsa_flash_create1(sms, "sbsa.flash1", "pflash1");
260e9fdf453SHongbo Zhang }
261e9fdf453SHongbo Zhang 
262e9fdf453SHongbo Zhang static void sbsa_flash_map1(PFlashCFI01 *flash,
263e9fdf453SHongbo Zhang                             hwaddr base, hwaddr size,
264e9fdf453SHongbo Zhang                             MemoryRegion *sysmem)
265e9fdf453SHongbo Zhang {
266e9fdf453SHongbo Zhang     DeviceState *dev = DEVICE(flash);
267e9fdf453SHongbo Zhang 
2684cdd0a77SPhilippe Mathieu-Daudé     assert(QEMU_IS_ALIGNED(size, SBSA_FLASH_SECTOR_SIZE));
269e9fdf453SHongbo Zhang     assert(size / SBSA_FLASH_SECTOR_SIZE <= UINT32_MAX);
270e9fdf453SHongbo Zhang     qdev_prop_set_uint32(dev, "num-blocks", size / SBSA_FLASH_SECTOR_SIZE);
2713c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
272e9fdf453SHongbo Zhang 
273e9fdf453SHongbo Zhang     memory_region_add_subregion(sysmem, base,
274e9fdf453SHongbo Zhang                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
275e9fdf453SHongbo Zhang                                                        0));
276e9fdf453SHongbo Zhang }
277e9fdf453SHongbo Zhang 
278e9fdf453SHongbo Zhang static void sbsa_flash_map(SBSAMachineState *sms,
279e9fdf453SHongbo Zhang                            MemoryRegion *sysmem,
280e9fdf453SHongbo Zhang                            MemoryRegion *secure_sysmem)
281e9fdf453SHongbo Zhang {
282e9fdf453SHongbo Zhang     /*
283e9fdf453SHongbo Zhang      * Map two flash devices to fill the SBSA_FLASH space in the memmap.
284e9fdf453SHongbo Zhang      * sysmem is the system memory space. secure_sysmem is the secure view
285e9fdf453SHongbo Zhang      * of the system, and the first flash device should be made visible only
286e9fdf453SHongbo Zhang      * there. The second flash device is visible to both secure and nonsecure.
287e9fdf453SHongbo Zhang      */
288e9fdf453SHongbo Zhang     hwaddr flashsize = sbsa_ref_memmap[SBSA_FLASH].size / 2;
289e9fdf453SHongbo Zhang     hwaddr flashbase = sbsa_ref_memmap[SBSA_FLASH].base;
290e9fdf453SHongbo Zhang 
291e9fdf453SHongbo Zhang     sbsa_flash_map1(sms->flash[0], flashbase, flashsize,
292e9fdf453SHongbo Zhang                     secure_sysmem);
293e9fdf453SHongbo Zhang     sbsa_flash_map1(sms->flash[1], flashbase + flashsize, flashsize,
294e9fdf453SHongbo Zhang                     sysmem);
295e9fdf453SHongbo Zhang }
296e9fdf453SHongbo Zhang 
297e9fdf453SHongbo Zhang static bool sbsa_firmware_init(SBSAMachineState *sms,
298e9fdf453SHongbo Zhang                                MemoryRegion *sysmem,
299e9fdf453SHongbo Zhang                                MemoryRegion *secure_sysmem)
300e9fdf453SHongbo Zhang {
301e9fdf453SHongbo Zhang     int i;
302e9fdf453SHongbo Zhang     BlockBackend *pflash_blk0;
303e9fdf453SHongbo Zhang 
304e9fdf453SHongbo Zhang     /* Map legacy -drive if=pflash to machine properties */
305e9fdf453SHongbo Zhang     for (i = 0; i < ARRAY_SIZE(sms->flash); i++) {
306e9fdf453SHongbo Zhang         pflash_cfi01_legacy_drive(sms->flash[i],
307e9fdf453SHongbo Zhang                                   drive_get(IF_PFLASH, 0, i));
308e9fdf453SHongbo Zhang     }
309e9fdf453SHongbo Zhang 
310e9fdf453SHongbo Zhang     sbsa_flash_map(sms, sysmem, secure_sysmem);
311e9fdf453SHongbo Zhang 
312e9fdf453SHongbo Zhang     pflash_blk0 = pflash_cfi01_get_blk(sms->flash[0]);
313e9fdf453SHongbo Zhang 
314e9fdf453SHongbo Zhang     if (bios_name) {
315e9fdf453SHongbo Zhang         char *fname;
316e9fdf453SHongbo Zhang         MemoryRegion *mr;
317e9fdf453SHongbo Zhang         int image_size;
318e9fdf453SHongbo Zhang 
319e9fdf453SHongbo Zhang         if (pflash_blk0) {
320e9fdf453SHongbo Zhang             error_report("The contents of the first flash device may be "
321e9fdf453SHongbo Zhang                          "specified with -bios or with -drive if=pflash... "
322e9fdf453SHongbo Zhang                          "but you cannot use both options at once");
323e9fdf453SHongbo Zhang             exit(1);
324e9fdf453SHongbo Zhang         }
325e9fdf453SHongbo Zhang 
326e9fdf453SHongbo Zhang         /* Fall back to -bios */
327e9fdf453SHongbo Zhang 
328e9fdf453SHongbo Zhang         fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
329e9fdf453SHongbo Zhang         if (!fname) {
330e9fdf453SHongbo Zhang             error_report("Could not find ROM image '%s'", bios_name);
331e9fdf453SHongbo Zhang             exit(1);
332e9fdf453SHongbo Zhang         }
333e9fdf453SHongbo Zhang         mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(sms->flash[0]), 0);
334e9fdf453SHongbo Zhang         image_size = load_image_mr(fname, mr);
335e9fdf453SHongbo Zhang         g_free(fname);
336e9fdf453SHongbo Zhang         if (image_size < 0) {
337e9fdf453SHongbo Zhang             error_report("Could not load ROM image '%s'", bios_name);
338e9fdf453SHongbo Zhang             exit(1);
339e9fdf453SHongbo Zhang         }
340e9fdf453SHongbo Zhang     }
341e9fdf453SHongbo Zhang 
342e9fdf453SHongbo Zhang     return pflash_blk0 || bios_name;
343e9fdf453SHongbo Zhang }
344e9fdf453SHongbo Zhang 
345e9fdf453SHongbo Zhang static void create_secure_ram(SBSAMachineState *sms,
346e9fdf453SHongbo Zhang                               MemoryRegion *secure_sysmem)
347e9fdf453SHongbo Zhang {
348e9fdf453SHongbo Zhang     MemoryRegion *secram = g_new(MemoryRegion, 1);
349e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_SECURE_MEM].base;
350e9fdf453SHongbo Zhang     hwaddr size = sbsa_ref_memmap[SBSA_SECURE_MEM].size;
351e9fdf453SHongbo Zhang 
352e9fdf453SHongbo Zhang     memory_region_init_ram(secram, NULL, "sbsa-ref.secure-ram", size,
353e9fdf453SHongbo Zhang                            &error_fatal);
354e9fdf453SHongbo Zhang     memory_region_add_subregion(secure_sysmem, base, secram);
355e9fdf453SHongbo Zhang }
356e9fdf453SHongbo Zhang 
35748ba18e6SPhilippe Mathieu-Daudé static void create_gic(SBSAMachineState *sms)
358e9fdf453SHongbo Zhang {
359cc7d44c2SLike Xu     unsigned int smp_cpus = MACHINE(sms)->smp.cpus;
360e9fdf453SHongbo Zhang     SysBusDevice *gicbusdev;
361e9fdf453SHongbo Zhang     const char *gictype;
362e9fdf453SHongbo Zhang     uint32_t redist0_capacity, redist0_count;
363e9fdf453SHongbo Zhang     int i;
364e9fdf453SHongbo Zhang 
365e9fdf453SHongbo Zhang     gictype = gicv3_class_name();
366e9fdf453SHongbo Zhang 
3673e80f690SMarkus Armbruster     sms->gic = qdev_new(gictype);
36848ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "revision", 3);
36948ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "num-cpu", smp_cpus);
370e9fdf453SHongbo Zhang     /*
371e9fdf453SHongbo Zhang      * Note that the num-irq property counts both internal and external
372e9fdf453SHongbo Zhang      * interrupts; there are always 32 of the former (mandated by GIC spec).
373e9fdf453SHongbo Zhang      */
37448ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "num-irq", NUM_IRQS + 32);
37548ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_bit(sms->gic, "has-security-extensions", true);
376e9fdf453SHongbo Zhang 
377e9fdf453SHongbo Zhang     redist0_capacity =
378e9fdf453SHongbo Zhang                 sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
379e9fdf453SHongbo Zhang     redist0_count = MIN(smp_cpus, redist0_capacity);
380e9fdf453SHongbo Zhang 
38148ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "len-redist-region-count", 1);
38248ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "redist-region-count[0]", redist0_count);
383e9fdf453SHongbo Zhang 
38448ba18e6SPhilippe Mathieu-Daudé     gicbusdev = SYS_BUS_DEVICE(sms->gic);
3853c6ef471SMarkus Armbruster     sysbus_realize_and_unref(gicbusdev, &error_fatal);
386e9fdf453SHongbo Zhang     sysbus_mmio_map(gicbusdev, 0, sbsa_ref_memmap[SBSA_GIC_DIST].base);
387e9fdf453SHongbo Zhang     sysbus_mmio_map(gicbusdev, 1, sbsa_ref_memmap[SBSA_GIC_REDIST].base);
388e9fdf453SHongbo Zhang 
389e9fdf453SHongbo Zhang     /*
390e9fdf453SHongbo Zhang      * Wire the outputs from each CPU's generic timer and the GICv3
391e9fdf453SHongbo Zhang      * maintenance interrupt signal to the appropriate GIC PPI inputs,
392e9fdf453SHongbo Zhang      * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
393e9fdf453SHongbo Zhang      */
394e9fdf453SHongbo Zhang     for (i = 0; i < smp_cpus; i++) {
395e9fdf453SHongbo Zhang         DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
396e9fdf453SHongbo Zhang         int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
397e9fdf453SHongbo Zhang         int irq;
398e9fdf453SHongbo Zhang         /*
399e9fdf453SHongbo Zhang          * Mapping from the output timer irq lines from the CPU to the
400e9fdf453SHongbo Zhang          * GIC PPI inputs used for this board.
401e9fdf453SHongbo Zhang          */
402e9fdf453SHongbo Zhang         const int timer_irq[] = {
403e9fdf453SHongbo Zhang             [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
404e9fdf453SHongbo Zhang             [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
405e9fdf453SHongbo Zhang             [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
406e9fdf453SHongbo Zhang             [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
407e9fdf453SHongbo Zhang         };
408e9fdf453SHongbo Zhang 
409e9fdf453SHongbo Zhang         for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
410e9fdf453SHongbo Zhang             qdev_connect_gpio_out(cpudev, irq,
41148ba18e6SPhilippe Mathieu-Daudé                                   qdev_get_gpio_in(sms->gic,
412e9fdf453SHongbo Zhang                                                    ppibase + timer_irq[irq]));
413e9fdf453SHongbo Zhang         }
414e9fdf453SHongbo Zhang 
415e9fdf453SHongbo Zhang         qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0,
41648ba18e6SPhilippe Mathieu-Daudé                                     qdev_get_gpio_in(sms->gic, ppibase
417e9fdf453SHongbo Zhang                                                      + ARCH_GIC_MAINT_IRQ));
418e9fdf453SHongbo Zhang         qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
41948ba18e6SPhilippe Mathieu-Daudé                                     qdev_get_gpio_in(sms->gic, ppibase
420e9fdf453SHongbo Zhang                                                      + VIRTUAL_PMU_IRQ));
421e9fdf453SHongbo Zhang 
422e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
423e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + smp_cpus,
424e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
425e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
426e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
427e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
428e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
429e9fdf453SHongbo Zhang     }
430e9fdf453SHongbo Zhang }
431e9fdf453SHongbo Zhang 
43248ba18e6SPhilippe Mathieu-Daudé static void create_uart(const SBSAMachineState *sms, int uart,
433e9fdf453SHongbo Zhang                         MemoryRegion *mem, Chardev *chr)
434e9fdf453SHongbo Zhang {
435e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[uart].base;
436e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[uart];
4373e80f690SMarkus Armbruster     DeviceState *dev = qdev_new(TYPE_PL011);
438e9fdf453SHongbo Zhang     SysBusDevice *s = SYS_BUS_DEVICE(dev);
439e9fdf453SHongbo Zhang 
440e9fdf453SHongbo Zhang     qdev_prop_set_chr(dev, "chardev", chr);
4413c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
442e9fdf453SHongbo Zhang     memory_region_add_subregion(mem, base,
443e9fdf453SHongbo Zhang                                 sysbus_mmio_get_region(s, 0));
44448ba18e6SPhilippe Mathieu-Daudé     sysbus_connect_irq(s, 0, qdev_get_gpio_in(sms->gic, irq));
445e9fdf453SHongbo Zhang }
446e9fdf453SHongbo Zhang 
44748ba18e6SPhilippe Mathieu-Daudé static void create_rtc(const SBSAMachineState *sms)
448e9fdf453SHongbo Zhang {
449e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_RTC].base;
450e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_RTC];
451e9fdf453SHongbo Zhang 
45248ba18e6SPhilippe Mathieu-Daudé     sysbus_create_simple("pl031", base, qdev_get_gpio_in(sms->gic, irq));
453e9fdf453SHongbo Zhang }
454e9fdf453SHongbo Zhang 
455e9fdf453SHongbo Zhang static DeviceState *gpio_key_dev;
456e9fdf453SHongbo Zhang static void sbsa_ref_powerdown_req(Notifier *n, void *opaque)
457e9fdf453SHongbo Zhang {
458e9fdf453SHongbo Zhang     /* use gpio Pin 3 for power button event */
459e9fdf453SHongbo Zhang     qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
460e9fdf453SHongbo Zhang }
461e9fdf453SHongbo Zhang 
462e9fdf453SHongbo Zhang static Notifier sbsa_ref_powerdown_notifier = {
463e9fdf453SHongbo Zhang     .notify = sbsa_ref_powerdown_req
464e9fdf453SHongbo Zhang };
465e9fdf453SHongbo Zhang 
46648ba18e6SPhilippe Mathieu-Daudé static void create_gpio(const SBSAMachineState *sms)
467e9fdf453SHongbo Zhang {
468e9fdf453SHongbo Zhang     DeviceState *pl061_dev;
469e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_GPIO].base;
470e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_GPIO];
471e9fdf453SHongbo Zhang 
47248ba18e6SPhilippe Mathieu-Daudé     pl061_dev = sysbus_create_simple("pl061", base,
47348ba18e6SPhilippe Mathieu-Daudé                                      qdev_get_gpio_in(sms->gic, irq));
474e9fdf453SHongbo Zhang 
475e9fdf453SHongbo Zhang     gpio_key_dev = sysbus_create_simple("gpio-key", -1,
476e9fdf453SHongbo Zhang                                         qdev_get_gpio_in(pl061_dev, 3));
477e9fdf453SHongbo Zhang 
478e9fdf453SHongbo Zhang     /* connect powerdown request */
479e9fdf453SHongbo Zhang     qemu_register_powerdown_notifier(&sbsa_ref_powerdown_notifier);
480e9fdf453SHongbo Zhang }
481e9fdf453SHongbo Zhang 
48248ba18e6SPhilippe Mathieu-Daudé static void create_ahci(const SBSAMachineState *sms)
483e9fdf453SHongbo Zhang {
484e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_AHCI].base;
485e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_AHCI];
486e9fdf453SHongbo Zhang     DeviceState *dev;
487e9fdf453SHongbo Zhang     DriveInfo *hd[NUM_SATA_PORTS];
488e9fdf453SHongbo Zhang     SysbusAHCIState *sysahci;
489e9fdf453SHongbo Zhang     AHCIState *ahci;
490e9fdf453SHongbo Zhang     int i;
491e9fdf453SHongbo Zhang 
4923e80f690SMarkus Armbruster     dev = qdev_new("sysbus-ahci");
493e9fdf453SHongbo Zhang     qdev_prop_set_uint32(dev, "num-ports", NUM_SATA_PORTS);
4943c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
495e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
49648ba18e6SPhilippe Mathieu-Daudé     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(sms->gic, irq));
497e9fdf453SHongbo Zhang 
498e9fdf453SHongbo Zhang     sysahci = SYSBUS_AHCI(dev);
499e9fdf453SHongbo Zhang     ahci = &sysahci->ahci;
500e9fdf453SHongbo Zhang     ide_drive_get(hd, ARRAY_SIZE(hd));
501e9fdf453SHongbo Zhang     for (i = 0; i < ahci->ports; i++) {
502e9fdf453SHongbo Zhang         if (hd[i] == NULL) {
503e9fdf453SHongbo Zhang             continue;
504e9fdf453SHongbo Zhang         }
505e9fdf453SHongbo Zhang         ide_create_drive(&ahci->dev[i].port, 0, hd[i]);
506e9fdf453SHongbo Zhang     }
507e9fdf453SHongbo Zhang }
508e9fdf453SHongbo Zhang 
50948ba18e6SPhilippe Mathieu-Daudé static void create_ehci(const SBSAMachineState *sms)
510e9fdf453SHongbo Zhang {
511e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_EHCI].base;
512e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_EHCI];
513e9fdf453SHongbo Zhang 
51448ba18e6SPhilippe Mathieu-Daudé     sysbus_create_simple("platform-ehci-usb", base,
51548ba18e6SPhilippe Mathieu-Daudé                          qdev_get_gpio_in(sms->gic, irq));
516e9fdf453SHongbo Zhang }
517e9fdf453SHongbo Zhang 
51848ba18e6SPhilippe Mathieu-Daudé static void create_smmu(const SBSAMachineState *sms, PCIBus *bus)
519e9fdf453SHongbo Zhang {
520e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_SMMU].base;
521e9fdf453SHongbo Zhang     int irq =  sbsa_ref_irqmap[SBSA_SMMU];
522e9fdf453SHongbo Zhang     DeviceState *dev;
523e9fdf453SHongbo Zhang     int i;
524e9fdf453SHongbo Zhang 
5253e80f690SMarkus Armbruster     dev = qdev_new("arm-smmuv3");
526e9fdf453SHongbo Zhang 
5275325cc34SMarkus Armbruster     object_property_set_link(OBJECT(dev), "primary-bus", OBJECT(bus),
528e9fdf453SHongbo Zhang                              &error_abort);
5293c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
530e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
531e9fdf453SHongbo Zhang     for (i = 0; i < NUM_SMMU_IRQS; i++) {
53248ba18e6SPhilippe Mathieu-Daudé         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
53348ba18e6SPhilippe Mathieu-Daudé                            qdev_get_gpio_in(sms->gic, irq + 1));
534e9fdf453SHongbo Zhang     }
535e9fdf453SHongbo Zhang }
536e9fdf453SHongbo Zhang 
53748ba18e6SPhilippe Mathieu-Daudé static void create_pcie(SBSAMachineState *sms)
538e9fdf453SHongbo Zhang {
539e9fdf453SHongbo Zhang     hwaddr base_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].base;
540e9fdf453SHongbo Zhang     hwaddr size_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].size;
541e9fdf453SHongbo Zhang     hwaddr base_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].base;
542e9fdf453SHongbo Zhang     hwaddr size_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].size;
543e9fdf453SHongbo Zhang     hwaddr base_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].base;
544e9fdf453SHongbo Zhang     hwaddr size_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].size;
545e9fdf453SHongbo Zhang     hwaddr base_pio = sbsa_ref_memmap[SBSA_PCIE_PIO].base;
546e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_PCIE];
547e9fdf453SHongbo Zhang     MemoryRegion *mmio_alias, *mmio_alias_high, *mmio_reg;
548e9fdf453SHongbo Zhang     MemoryRegion *ecam_alias, *ecam_reg;
549e9fdf453SHongbo Zhang     DeviceState *dev;
550e9fdf453SHongbo Zhang     PCIHostState *pci;
551e9fdf453SHongbo Zhang     int i;
552e9fdf453SHongbo Zhang 
5533e80f690SMarkus Armbruster     dev = qdev_new(TYPE_GPEX_HOST);
5543c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
555e9fdf453SHongbo Zhang 
556e9fdf453SHongbo Zhang     /* Map ECAM space */
557e9fdf453SHongbo Zhang     ecam_alias = g_new0(MemoryRegion, 1);
558e9fdf453SHongbo Zhang     ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
559e9fdf453SHongbo Zhang     memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
560e9fdf453SHongbo Zhang                              ecam_reg, 0, size_ecam);
561e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
562e9fdf453SHongbo Zhang 
563e9fdf453SHongbo Zhang     /* Map the MMIO space */
564e9fdf453SHongbo Zhang     mmio_alias = g_new0(MemoryRegion, 1);
565e9fdf453SHongbo Zhang     mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
566e9fdf453SHongbo Zhang     memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
567e9fdf453SHongbo Zhang                              mmio_reg, base_mmio, size_mmio);
568e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
569e9fdf453SHongbo Zhang 
570e9fdf453SHongbo Zhang     /* Map the MMIO_HIGH space */
571e9fdf453SHongbo Zhang     mmio_alias_high = g_new0(MemoryRegion, 1);
572e9fdf453SHongbo Zhang     memory_region_init_alias(mmio_alias_high, OBJECT(dev), "pcie-mmio-high",
573e9fdf453SHongbo Zhang                              mmio_reg, base_mmio_high, size_mmio_high);
574e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_mmio_high,
575e9fdf453SHongbo Zhang                                 mmio_alias_high);
576e9fdf453SHongbo Zhang 
577e9fdf453SHongbo Zhang     /* Map IO port space */
578e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
579e9fdf453SHongbo Zhang 
580e9fdf453SHongbo Zhang     for (i = 0; i < GPEX_NUM_IRQS; i++) {
58148ba18e6SPhilippe Mathieu-Daudé         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
582870f0051SGraeme Gregory                            qdev_get_gpio_in(sms->gic, irq + i));
583e9fdf453SHongbo Zhang         gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
584e9fdf453SHongbo Zhang     }
585e9fdf453SHongbo Zhang 
586e9fdf453SHongbo Zhang     pci = PCI_HOST_BRIDGE(dev);
587e9fdf453SHongbo Zhang     if (pci->bus) {
588e9fdf453SHongbo Zhang         for (i = 0; i < nb_nics; i++) {
589e9fdf453SHongbo Zhang             NICInfo *nd = &nd_table[i];
590e9fdf453SHongbo Zhang 
591e9fdf453SHongbo Zhang             if (!nd->model) {
592e9fdf453SHongbo Zhang                 nd->model = g_strdup("e1000e");
593e9fdf453SHongbo Zhang             }
594e9fdf453SHongbo Zhang 
595e9fdf453SHongbo Zhang             pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
596e9fdf453SHongbo Zhang         }
597e9fdf453SHongbo Zhang     }
598e9fdf453SHongbo Zhang 
599e9fdf453SHongbo Zhang     pci_create_simple(pci->bus, -1, "VGA");
600e9fdf453SHongbo Zhang 
60148ba18e6SPhilippe Mathieu-Daudé     create_smmu(sms, pci->bus);
602e9fdf453SHongbo Zhang }
603e9fdf453SHongbo Zhang 
604e9fdf453SHongbo Zhang static void *sbsa_ref_dtb(const struct arm_boot_info *binfo, int *fdt_size)
605e9fdf453SHongbo Zhang {
606e9fdf453SHongbo Zhang     const SBSAMachineState *board = container_of(binfo, SBSAMachineState,
607e9fdf453SHongbo Zhang                                                  bootinfo);
608e9fdf453SHongbo Zhang 
609e9fdf453SHongbo Zhang     *fdt_size = board->fdt_size;
610e9fdf453SHongbo Zhang     return board->fdt;
611e9fdf453SHongbo Zhang }
612e9fdf453SHongbo Zhang 
613*3f462bf0SGraeme Gregory static void create_secure_ec(MemoryRegion *mem)
614*3f462bf0SGraeme Gregory {
615*3f462bf0SGraeme Gregory     hwaddr base = sbsa_ref_memmap[SBSA_SECURE_EC].base;
616*3f462bf0SGraeme Gregory     DeviceState *dev = qdev_new("sbsa-ec");
617*3f462bf0SGraeme Gregory     SysBusDevice *s = SYS_BUS_DEVICE(dev);
618*3f462bf0SGraeme Gregory 
619*3f462bf0SGraeme Gregory     memory_region_add_subregion(mem, base,
620*3f462bf0SGraeme Gregory                                 sysbus_mmio_get_region(s, 0));
621*3f462bf0SGraeme Gregory }
622*3f462bf0SGraeme Gregory 
62364580903SHongbo Zhang static void sbsa_ref_init(MachineState *machine)
62464580903SHongbo Zhang {
625cc7d44c2SLike Xu     unsigned int smp_cpus = machine->smp.cpus;
626cc7d44c2SLike Xu     unsigned int max_cpus = machine->smp.max_cpus;
62764580903SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(machine);
62864580903SHongbo Zhang     MachineClass *mc = MACHINE_GET_CLASS(machine);
62964580903SHongbo Zhang     MemoryRegion *sysmem = get_system_memory();
630c8ead571SPeter Maydell     MemoryRegion *secure_sysmem = g_new(MemoryRegion, 1);
631e9fdf453SHongbo Zhang     bool firmware_loaded;
63264580903SHongbo Zhang     const CPUArchIdList *possible_cpus;
63364580903SHongbo Zhang     int n, sbsa_max_cpus;
63464580903SHongbo Zhang 
63564580903SHongbo Zhang     if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a57"))) {
63664580903SHongbo Zhang         error_report("sbsa-ref: CPU type other than the built-in "
63764580903SHongbo Zhang                      "cortex-a57 not supported");
63864580903SHongbo Zhang         exit(1);
63964580903SHongbo Zhang     }
64064580903SHongbo Zhang 
64164580903SHongbo Zhang     if (kvm_enabled()) {
64264580903SHongbo Zhang         error_report("sbsa-ref: KVM is not supported for this machine");
64364580903SHongbo Zhang         exit(1);
64464580903SHongbo Zhang     }
64564580903SHongbo Zhang 
64664580903SHongbo Zhang     /*
647e9fdf453SHongbo Zhang      * The Secure view of the world is the same as the NonSecure,
648e9fdf453SHongbo Zhang      * but with a few extra devices. Create it as a container region
649e9fdf453SHongbo Zhang      * containing the system memory at low priority; any secure-only
650e9fdf453SHongbo Zhang      * devices go in at higher priority and take precedence.
651e9fdf453SHongbo Zhang      */
652e9fdf453SHongbo Zhang     memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
653e9fdf453SHongbo Zhang                        UINT64_MAX);
654e9fdf453SHongbo Zhang     memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
655e9fdf453SHongbo Zhang 
656c8ead571SPeter Maydell     firmware_loaded = sbsa_firmware_init(sms, sysmem, secure_sysmem);
657e9fdf453SHongbo Zhang 
658e9fdf453SHongbo Zhang     if (machine->kernel_filename && firmware_loaded) {
659e9fdf453SHongbo Zhang         error_report("sbsa-ref: No fw_cfg device on this machine, "
660e9fdf453SHongbo Zhang                      "so -kernel option is not supported when firmware loaded, "
661e9fdf453SHongbo Zhang                      "please load OS from hard disk instead");
662e9fdf453SHongbo Zhang         exit(1);
663e9fdf453SHongbo Zhang     }
664e9fdf453SHongbo Zhang 
665e9fdf453SHongbo Zhang     /*
66664580903SHongbo Zhang      * This machine has EL3 enabled, external firmware should supply PSCI
66764580903SHongbo Zhang      * implementation, so the QEMU's internal PSCI is disabled.
66864580903SHongbo Zhang      */
66964580903SHongbo Zhang     sms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
67064580903SHongbo Zhang 
67164580903SHongbo Zhang     sbsa_max_cpus = sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
67264580903SHongbo Zhang 
67364580903SHongbo Zhang     if (max_cpus > sbsa_max_cpus) {
67464580903SHongbo Zhang         error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
67564580903SHongbo Zhang                      "supported by machine 'sbsa-ref' (%d)",
67664580903SHongbo Zhang                      max_cpus, sbsa_max_cpus);
67764580903SHongbo Zhang         exit(1);
67864580903SHongbo Zhang     }
67964580903SHongbo Zhang 
68064580903SHongbo Zhang     sms->smp_cpus = smp_cpus;
68164580903SHongbo Zhang 
68264580903SHongbo Zhang     if (machine->ram_size > sbsa_ref_memmap[SBSA_MEM].size) {
68364580903SHongbo Zhang         error_report("sbsa-ref: cannot model more than %dGB RAM", RAMLIMIT_GB);
68464580903SHongbo Zhang         exit(1);
68564580903SHongbo Zhang     }
68664580903SHongbo Zhang 
68764580903SHongbo Zhang     possible_cpus = mc->possible_cpu_arch_ids(machine);
68864580903SHongbo Zhang     for (n = 0; n < possible_cpus->len; n++) {
68964580903SHongbo Zhang         Object *cpuobj;
69064580903SHongbo Zhang         CPUState *cs;
69164580903SHongbo Zhang 
69264580903SHongbo Zhang         if (n >= smp_cpus) {
69364580903SHongbo Zhang             break;
69464580903SHongbo Zhang         }
69564580903SHongbo Zhang 
69664580903SHongbo Zhang         cpuobj = object_new(possible_cpus->cpus[n].type);
6975325cc34SMarkus Armbruster         object_property_set_int(cpuobj, "mp-affinity",
6985325cc34SMarkus Armbruster                                 possible_cpus->cpus[n].arch_id, NULL);
69964580903SHongbo Zhang 
70064580903SHongbo Zhang         cs = CPU(cpuobj);
70164580903SHongbo Zhang         cs->cpu_index = n;
70264580903SHongbo Zhang 
70364580903SHongbo Zhang         numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
70464580903SHongbo Zhang                           &error_fatal);
70564580903SHongbo Zhang 
70664580903SHongbo Zhang         if (object_property_find(cpuobj, "reset-cbar", NULL)) {
7075325cc34SMarkus Armbruster             object_property_set_int(cpuobj, "reset-cbar",
70864580903SHongbo Zhang                                     sbsa_ref_memmap[SBSA_CPUPERIPHS].base,
7095325cc34SMarkus Armbruster                                     &error_abort);
71064580903SHongbo Zhang         }
71164580903SHongbo Zhang 
7125325cc34SMarkus Armbruster         object_property_set_link(cpuobj, "memory", OBJECT(sysmem),
71364580903SHongbo Zhang                                  &error_abort);
71464580903SHongbo Zhang 
7155325cc34SMarkus Armbruster         object_property_set_link(cpuobj, "secure-memory",
7165325cc34SMarkus Armbruster                                  OBJECT(secure_sysmem), &error_abort);
71764580903SHongbo Zhang 
718ce189ab2SMarkus Armbruster         qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
71964580903SHongbo Zhang         object_unref(cpuobj);
72064580903SHongbo Zhang     }
72164580903SHongbo Zhang 
7223818ed92SIgor Mammedov     memory_region_add_subregion(sysmem, sbsa_ref_memmap[SBSA_MEM].base,
7233818ed92SIgor Mammedov                                 machine->ram);
72464580903SHongbo Zhang 
725e9fdf453SHongbo Zhang     create_fdt(sms);
726e9fdf453SHongbo Zhang 
727e9fdf453SHongbo Zhang     create_secure_ram(sms, secure_sysmem);
728e9fdf453SHongbo Zhang 
72948ba18e6SPhilippe Mathieu-Daudé     create_gic(sms);
730e9fdf453SHongbo Zhang 
73148ba18e6SPhilippe Mathieu-Daudé     create_uart(sms, SBSA_UART, sysmem, serial_hd(0));
73248ba18e6SPhilippe Mathieu-Daudé     create_uart(sms, SBSA_SECURE_UART, secure_sysmem, serial_hd(1));
733e9fdf453SHongbo Zhang     /* Second secure UART for RAS and MM from EL0 */
73448ba18e6SPhilippe Mathieu-Daudé     create_uart(sms, SBSA_SECURE_UART_MM, secure_sysmem, serial_hd(2));
735e9fdf453SHongbo Zhang 
73648ba18e6SPhilippe Mathieu-Daudé     create_rtc(sms);
737e9fdf453SHongbo Zhang 
73848ba18e6SPhilippe Mathieu-Daudé     create_gpio(sms);
739e9fdf453SHongbo Zhang 
74048ba18e6SPhilippe Mathieu-Daudé     create_ahci(sms);
741e9fdf453SHongbo Zhang 
74248ba18e6SPhilippe Mathieu-Daudé     create_ehci(sms);
743e9fdf453SHongbo Zhang 
74448ba18e6SPhilippe Mathieu-Daudé     create_pcie(sms);
745e9fdf453SHongbo Zhang 
746*3f462bf0SGraeme Gregory     create_secure_ec(secure_sysmem);
747*3f462bf0SGraeme Gregory 
74864580903SHongbo Zhang     sms->bootinfo.ram_size = machine->ram_size;
74964580903SHongbo Zhang     sms->bootinfo.nb_cpus = smp_cpus;
75064580903SHongbo Zhang     sms->bootinfo.board_id = -1;
75164580903SHongbo Zhang     sms->bootinfo.loader_start = sbsa_ref_memmap[SBSA_MEM].base;
752e9fdf453SHongbo Zhang     sms->bootinfo.get_dtb = sbsa_ref_dtb;
753e9fdf453SHongbo Zhang     sms->bootinfo.firmware_loaded = firmware_loaded;
7542744ece8STao Xu     arm_load_kernel(ARM_CPU(first_cpu), machine, &sms->bootinfo);
75564580903SHongbo Zhang }
75664580903SHongbo Zhang 
75764580903SHongbo Zhang static const CPUArchIdList *sbsa_ref_possible_cpu_arch_ids(MachineState *ms)
75864580903SHongbo Zhang {
759cc7d44c2SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
76064580903SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(ms);
76164580903SHongbo Zhang     int n;
76264580903SHongbo Zhang 
76364580903SHongbo Zhang     if (ms->possible_cpus) {
76464580903SHongbo Zhang         assert(ms->possible_cpus->len == max_cpus);
76564580903SHongbo Zhang         return ms->possible_cpus;
76664580903SHongbo Zhang     }
76764580903SHongbo Zhang 
76864580903SHongbo Zhang     ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
76964580903SHongbo Zhang                                   sizeof(CPUArchId) * max_cpus);
77064580903SHongbo Zhang     ms->possible_cpus->len = max_cpus;
77164580903SHongbo Zhang     for (n = 0; n < ms->possible_cpus->len; n++) {
77264580903SHongbo Zhang         ms->possible_cpus->cpus[n].type = ms->cpu_type;
77364580903SHongbo Zhang         ms->possible_cpus->cpus[n].arch_id =
77464580903SHongbo Zhang             sbsa_ref_cpu_mp_affinity(sms, n);
77564580903SHongbo Zhang         ms->possible_cpus->cpus[n].props.has_thread_id = true;
77664580903SHongbo Zhang         ms->possible_cpus->cpus[n].props.thread_id = n;
77764580903SHongbo Zhang     }
77864580903SHongbo Zhang     return ms->possible_cpus;
77964580903SHongbo Zhang }
78064580903SHongbo Zhang 
78164580903SHongbo Zhang static CpuInstanceProperties
78264580903SHongbo Zhang sbsa_ref_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
78364580903SHongbo Zhang {
78464580903SHongbo Zhang     MachineClass *mc = MACHINE_GET_CLASS(ms);
78564580903SHongbo Zhang     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
78664580903SHongbo Zhang 
78764580903SHongbo Zhang     assert(cpu_index < possible_cpus->len);
78864580903SHongbo Zhang     return possible_cpus->cpus[cpu_index].props;
78964580903SHongbo Zhang }
79064580903SHongbo Zhang 
79164580903SHongbo Zhang static int64_t
79264580903SHongbo Zhang sbsa_ref_get_default_cpu_node_id(const MachineState *ms, int idx)
79364580903SHongbo Zhang {
794aa570207STao Xu     return idx % ms->numa_state->num_nodes;
79564580903SHongbo Zhang }
79664580903SHongbo Zhang 
797e9fdf453SHongbo Zhang static void sbsa_ref_instance_init(Object *obj)
798e9fdf453SHongbo Zhang {
799e9fdf453SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(obj);
800e9fdf453SHongbo Zhang 
801e9fdf453SHongbo Zhang     sbsa_flash_create(sms);
802e9fdf453SHongbo Zhang }
803e9fdf453SHongbo Zhang 
80464580903SHongbo Zhang static void sbsa_ref_class_init(ObjectClass *oc, void *data)
80564580903SHongbo Zhang {
80664580903SHongbo Zhang     MachineClass *mc = MACHINE_CLASS(oc);
80764580903SHongbo Zhang 
80864580903SHongbo Zhang     mc->init = sbsa_ref_init;
80964580903SHongbo Zhang     mc->desc = "QEMU 'SBSA Reference' ARM Virtual Machine";
81064580903SHongbo Zhang     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a57");
81164580903SHongbo Zhang     mc->max_cpus = 512;
81264580903SHongbo Zhang     mc->pci_allow_0_address = true;
81364580903SHongbo Zhang     mc->minimum_page_bits = 12;
81464580903SHongbo Zhang     mc->block_default_type = IF_IDE;
81564580903SHongbo Zhang     mc->no_cdrom = 1;
81664580903SHongbo Zhang     mc->default_ram_size = 1 * GiB;
8173818ed92SIgor Mammedov     mc->default_ram_id = "sbsa-ref.ram";
81864580903SHongbo Zhang     mc->default_cpus = 4;
81964580903SHongbo Zhang     mc->possible_cpu_arch_ids = sbsa_ref_possible_cpu_arch_ids;
82064580903SHongbo Zhang     mc->cpu_index_to_instance_props = sbsa_ref_cpu_index_to_props;
82164580903SHongbo Zhang     mc->get_default_cpu_node_id = sbsa_ref_get_default_cpu_node_id;
82264580903SHongbo Zhang }
82364580903SHongbo Zhang 
82464580903SHongbo Zhang static const TypeInfo sbsa_ref_info = {
82564580903SHongbo Zhang     .name          = TYPE_SBSA_MACHINE,
82664580903SHongbo Zhang     .parent        = TYPE_MACHINE,
827e9fdf453SHongbo Zhang     .instance_init = sbsa_ref_instance_init,
82864580903SHongbo Zhang     .class_init    = sbsa_ref_class_init,
82964580903SHongbo Zhang     .instance_size = sizeof(SBSAMachineState),
83064580903SHongbo Zhang };
83164580903SHongbo Zhang 
83264580903SHongbo Zhang static void sbsa_ref_machine_init(void)
83364580903SHongbo Zhang {
83464580903SHongbo Zhang     type_register_static(&sbsa_ref_info);
83564580903SHongbo Zhang }
83664580903SHongbo Zhang 
83764580903SHongbo Zhang type_init(sbsa_ref_machine_init);
838