xref: /openbmc/qemu/hw/arm/sbsa-ref.c (revision 90ea2cce)
164580903SHongbo Zhang /*
264580903SHongbo Zhang  * ARM SBSA Reference Platform emulation
364580903SHongbo Zhang  *
464580903SHongbo Zhang  * Copyright (c) 2018 Linaro Limited
564580903SHongbo Zhang  * Written by Hongbo Zhang <hongbo.zhang@linaro.org>
664580903SHongbo Zhang  *
764580903SHongbo Zhang  * This program is free software; you can redistribute it and/or modify it
864580903SHongbo Zhang  * under the terms and conditions of the GNU General Public License,
964580903SHongbo Zhang  * version 2 or later, as published by the Free Software Foundation.
1064580903SHongbo Zhang  *
1164580903SHongbo Zhang  * This program is distributed in the hope it will be useful, but WITHOUT
1264580903SHongbo Zhang  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1364580903SHongbo Zhang  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1464580903SHongbo Zhang  * more details.
1564580903SHongbo Zhang  *
1664580903SHongbo Zhang  * You should have received a copy of the GNU General Public License along with
1764580903SHongbo Zhang  * this program.  If not, see <http://www.gnu.org/licenses/>.
1864580903SHongbo Zhang  */
1964580903SHongbo Zhang 
2064580903SHongbo Zhang #include "qemu/osdep.h"
212c65db5eSPaolo Bonzini #include "qemu/datadir.h"
2264580903SHongbo Zhang #include "qapi/error.h"
2364580903SHongbo Zhang #include "qemu/error-report.h"
2464580903SHongbo Zhang #include "qemu/units.h"
25e9fdf453SHongbo Zhang #include "sysemu/device_tree.h"
2664580903SHongbo Zhang #include "sysemu/numa.h"
2754d31236SMarkus Armbruster #include "sysemu/runstate.h"
2864580903SHongbo Zhang #include "sysemu/sysemu.h"
2964580903SHongbo Zhang #include "exec/hwaddr.h"
3064580903SHongbo Zhang #include "kvm_arm.h"
3164580903SHongbo Zhang #include "hw/arm/boot.h"
32e9fdf453SHongbo Zhang #include "hw/block/flash.h"
3364580903SHongbo Zhang #include "hw/boards.h"
34e9fdf453SHongbo Zhang #include "hw/ide/internal.h"
35e9fdf453SHongbo Zhang #include "hw/ide/ahci_internal.h"
3664580903SHongbo Zhang #include "hw/intc/arm_gicv3_common.h"
37e9fdf453SHongbo Zhang #include "hw/loader.h"
38e9fdf453SHongbo Zhang #include "hw/pci-host/gpex.h"
39a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
40e9fdf453SHongbo Zhang #include "hw/usb.h"
41d8f6d15fSGavin Shan #include "hw/char/pl011.h"
42baabe7d0SShashi Mallela #include "hw/watchdog/sbsa_gwdt.h"
43e9fdf453SHongbo Zhang #include "net/net.h"
44db1015e9SEduardo Habkost #include "qom/object.h"
4564580903SHongbo Zhang 
4664580903SHongbo Zhang #define RAMLIMIT_GB 8192
4764580903SHongbo Zhang #define RAMLIMIT_BYTES (RAMLIMIT_GB * GiB)
4864580903SHongbo Zhang 
49e9fdf453SHongbo Zhang #define NUM_IRQS        256
50e9fdf453SHongbo Zhang #define NUM_SMMU_IRQS   4
51e9fdf453SHongbo Zhang #define NUM_SATA_PORTS  6
52e9fdf453SHongbo Zhang 
53e9fdf453SHongbo Zhang #define VIRTUAL_PMU_IRQ        7
54e9fdf453SHongbo Zhang #define ARCH_GIC_MAINT_IRQ     9
55e9fdf453SHongbo Zhang #define ARCH_TIMER_VIRT_IRQ    11
56e9fdf453SHongbo Zhang #define ARCH_TIMER_S_EL1_IRQ   13
57e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL1_IRQ  14
58e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL2_IRQ  10
59e9fdf453SHongbo Zhang 
6064580903SHongbo Zhang enum {
6164580903SHongbo Zhang     SBSA_FLASH,
6264580903SHongbo Zhang     SBSA_MEM,
6364580903SHongbo Zhang     SBSA_CPUPERIPHS,
6464580903SHongbo Zhang     SBSA_GIC_DIST,
6564580903SHongbo Zhang     SBSA_GIC_REDIST,
663f462bf0SGraeme Gregory     SBSA_SECURE_EC,
6780d60a6dSEduardo Habkost     SBSA_GWDT_WS0,
68baabe7d0SShashi Mallela     SBSA_GWDT_REFRESH,
69baabe7d0SShashi Mallela     SBSA_GWDT_CONTROL,
7064580903SHongbo Zhang     SBSA_SMMU,
7164580903SHongbo Zhang     SBSA_UART,
7264580903SHongbo Zhang     SBSA_RTC,
7364580903SHongbo Zhang     SBSA_PCIE,
7464580903SHongbo Zhang     SBSA_PCIE_MMIO,
7564580903SHongbo Zhang     SBSA_PCIE_MMIO_HIGH,
7664580903SHongbo Zhang     SBSA_PCIE_PIO,
7764580903SHongbo Zhang     SBSA_PCIE_ECAM,
7864580903SHongbo Zhang     SBSA_GPIO,
7964580903SHongbo Zhang     SBSA_SECURE_UART,
8064580903SHongbo Zhang     SBSA_SECURE_UART_MM,
8164580903SHongbo Zhang     SBSA_SECURE_MEM,
8264580903SHongbo Zhang     SBSA_AHCI,
8364580903SHongbo Zhang     SBSA_EHCI,
8464580903SHongbo Zhang };
8564580903SHongbo Zhang 
86db1015e9SEduardo Habkost struct SBSAMachineState {
8764580903SHongbo Zhang     MachineState parent;
8864580903SHongbo Zhang     struct arm_boot_info bootinfo;
8964580903SHongbo Zhang     int smp_cpus;
9064580903SHongbo Zhang     void *fdt;
9164580903SHongbo Zhang     int fdt_size;
9264580903SHongbo Zhang     int psci_conduit;
9348ba18e6SPhilippe Mathieu-Daudé     DeviceState *gic;
94e9fdf453SHongbo Zhang     PFlashCFI01 *flash[2];
95db1015e9SEduardo Habkost };
9664580903SHongbo Zhang 
9764580903SHongbo Zhang #define TYPE_SBSA_MACHINE   MACHINE_TYPE_NAME("sbsa-ref")
988063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(SBSAMachineState, SBSA_MACHINE)
9964580903SHongbo Zhang 
10064580903SHongbo Zhang static const MemMapEntry sbsa_ref_memmap[] = {
10164580903SHongbo Zhang     /* 512M boot ROM */
10264580903SHongbo Zhang     [SBSA_FLASH] =              {          0, 0x20000000 },
10364580903SHongbo Zhang     /* 512M secure memory */
10464580903SHongbo Zhang     [SBSA_SECURE_MEM] =         { 0x20000000, 0x20000000 },
10564580903SHongbo Zhang     /* Space reserved for CPU peripheral devices */
10664580903SHongbo Zhang     [SBSA_CPUPERIPHS] =         { 0x40000000, 0x00040000 },
10764580903SHongbo Zhang     [SBSA_GIC_DIST] =           { 0x40060000, 0x00010000 },
10864580903SHongbo Zhang     [SBSA_GIC_REDIST] =         { 0x40080000, 0x04000000 },
1093f462bf0SGraeme Gregory     [SBSA_SECURE_EC] =          { 0x50000000, 0x00001000 },
110baabe7d0SShashi Mallela     [SBSA_GWDT_REFRESH] =       { 0x50010000, 0x00001000 },
111baabe7d0SShashi Mallela     [SBSA_GWDT_CONTROL] =       { 0x50011000, 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,
14104788fd5SGraeme Gregory     [SBSA_SMMU] = 12, /* ... to 15 */
14280d60a6dSEduardo Habkost     [SBSA_GWDT_WS0] = 16,
143e9fdf453SHongbo Zhang };
144e9fdf453SHongbo Zhang 
145ce3adffcSMarcin Juszkiewicz static const char * const valid_cpus[] = {
146ce3adffcSMarcin Juszkiewicz     ARM_CPU_TYPE_NAME("cortex-a57"),
147ce3adffcSMarcin Juszkiewicz     ARM_CPU_TYPE_NAME("cortex-a72"),
1482f6283fcSRichard Henderson     ARM_CPU_TYPE_NAME("cortex-a76"),
1495db6de80SRichard Henderson     ARM_CPU_TYPE_NAME("neoverse-n1"),
150cecc0962SMarcin Juszkiewicz     ARM_CPU_TYPE_NAME("max"),
151ce3adffcSMarcin Juszkiewicz };
152ce3adffcSMarcin Juszkiewicz 
153ce3adffcSMarcin Juszkiewicz static bool cpu_type_valid(const char *cpu)
154ce3adffcSMarcin Juszkiewicz {
155ce3adffcSMarcin Juszkiewicz     int i;
156ce3adffcSMarcin Juszkiewicz 
157ce3adffcSMarcin Juszkiewicz     for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
158ce3adffcSMarcin Juszkiewicz         if (strcmp(cpu, valid_cpus[i]) == 0) {
159ce3adffcSMarcin Juszkiewicz             return true;
160ce3adffcSMarcin Juszkiewicz         }
161ce3adffcSMarcin Juszkiewicz     }
162ce3adffcSMarcin Juszkiewicz     return false;
163ce3adffcSMarcin Juszkiewicz }
164ce3adffcSMarcin Juszkiewicz 
165999f6ebdSLeif Lindholm static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx)
166999f6ebdSLeif Lindholm {
167999f6ebdSLeif Lindholm     uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
168999f6ebdSLeif Lindholm     return arm_cpu_mp_affinity(idx, clustersz);
169999f6ebdSLeif Lindholm }
170999f6ebdSLeif Lindholm 
171e9fdf453SHongbo Zhang /*
172e9fdf453SHongbo Zhang  * Firmware on this machine only uses ACPI table to load OS, these limited
173e9fdf453SHongbo Zhang  * device tree nodes are just to let firmware know the info which varies from
174e9fdf453SHongbo Zhang  * command line parameters, so it is not necessary to be fully compatible
175e9fdf453SHongbo Zhang  * with the kernel CPU and NUMA binding rules.
176e9fdf453SHongbo Zhang  */
177e9fdf453SHongbo Zhang static void create_fdt(SBSAMachineState *sms)
178e9fdf453SHongbo Zhang {
179e9fdf453SHongbo Zhang     void *fdt = create_device_tree(&sms->fdt_size);
180e9fdf453SHongbo Zhang     const MachineState *ms = MACHINE(sms);
181aa570207STao Xu     int nb_numa_nodes = ms->numa_state->num_nodes;
182e9fdf453SHongbo Zhang     int cpu;
183e9fdf453SHongbo Zhang 
184e9fdf453SHongbo Zhang     if (!fdt) {
185e9fdf453SHongbo Zhang         error_report("create_device_tree() failed");
186e9fdf453SHongbo Zhang         exit(1);
187e9fdf453SHongbo Zhang     }
188e9fdf453SHongbo Zhang 
189e9fdf453SHongbo Zhang     sms->fdt = fdt;
190e9fdf453SHongbo Zhang 
191e9fdf453SHongbo Zhang     qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,sbsa-ref");
192e9fdf453SHongbo Zhang     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
193e9fdf453SHongbo Zhang     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
194e9fdf453SHongbo Zhang 
195*90ea2cceSLeif Lindholm     /*
196*90ea2cceSLeif Lindholm      * This versioning scheme is for informing platform fw only. It is neither:
197*90ea2cceSLeif Lindholm      * - A QEMU versioned machine type; a given version of QEMU will emulate
198*90ea2cceSLeif Lindholm      *   a given version of the platform.
199*90ea2cceSLeif Lindholm      * - A reflection of level of SBSA (now SystemReady SR) support provided.
200*90ea2cceSLeif Lindholm      *
201*90ea2cceSLeif Lindholm      * machine-version-major: updated when changes breaking fw compatibility
202*90ea2cceSLeif Lindholm      *                        are introduced.
203*90ea2cceSLeif Lindholm      * machine-version-minor: updated when features are added that don't break
204*90ea2cceSLeif Lindholm      *                        fw compatibility.
205*90ea2cceSLeif Lindholm      */
206*90ea2cceSLeif Lindholm     qemu_fdt_setprop_cell(fdt, "/", "machine-version-major", 0);
207*90ea2cceSLeif Lindholm     qemu_fdt_setprop_cell(fdt, "/", "machine-version-minor", 0);
208*90ea2cceSLeif Lindholm 
209118154b7STao Xu     if (ms->numa_state->have_numa_distance) {
210e9fdf453SHongbo Zhang         int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
211e9fdf453SHongbo Zhang         uint32_t *matrix = g_malloc0(size);
212e9fdf453SHongbo Zhang         int idx, i, j;
213e9fdf453SHongbo Zhang 
214e9fdf453SHongbo Zhang         for (i = 0; i < nb_numa_nodes; i++) {
215e9fdf453SHongbo Zhang             for (j = 0; j < nb_numa_nodes; j++) {
216e9fdf453SHongbo Zhang                 idx = (i * nb_numa_nodes + j) * 3;
217e9fdf453SHongbo Zhang                 matrix[idx + 0] = cpu_to_be32(i);
218e9fdf453SHongbo Zhang                 matrix[idx + 1] = cpu_to_be32(j);
2197e721e7bSTao Xu                 matrix[idx + 2] =
2207e721e7bSTao Xu                     cpu_to_be32(ms->numa_state->nodes[i].distance[j]);
221e9fdf453SHongbo Zhang             }
222e9fdf453SHongbo Zhang         }
223e9fdf453SHongbo Zhang 
224e9fdf453SHongbo Zhang         qemu_fdt_add_subnode(fdt, "/distance-map");
225e9fdf453SHongbo Zhang         qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
226e9fdf453SHongbo Zhang                          matrix, size);
227e9fdf453SHongbo Zhang         g_free(matrix);
228e9fdf453SHongbo Zhang     }
229e9fdf453SHongbo Zhang 
230999f6ebdSLeif Lindholm     /*
231999f6ebdSLeif Lindholm      * From Documentation/devicetree/bindings/arm/cpus.yaml
232999f6ebdSLeif Lindholm      *  On ARM v8 64-bit systems this property is required
233999f6ebdSLeif Lindholm      *    and matches the MPIDR_EL1 register affinity bits.
234999f6ebdSLeif Lindholm      *
235999f6ebdSLeif Lindholm      *    * If cpus node's #address-cells property is set to 2
236999f6ebdSLeif Lindholm      *
237999f6ebdSLeif Lindholm      *      The first reg cell bits [7:0] must be set to
238999f6ebdSLeif Lindholm      *      bits [39:32] of MPIDR_EL1.
239999f6ebdSLeif Lindholm      *
240999f6ebdSLeif Lindholm      *      The second reg cell bits [23:0] must be set to
241999f6ebdSLeif Lindholm      *      bits [23:0] of MPIDR_EL1.
242999f6ebdSLeif Lindholm      */
243e9fdf453SHongbo Zhang     qemu_fdt_add_subnode(sms->fdt, "/cpus");
244999f6ebdSLeif Lindholm     qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#address-cells", 2);
245999f6ebdSLeif Lindholm     qemu_fdt_setprop_cell(sms->fdt, "/cpus", "#size-cells", 0x0);
246e9fdf453SHongbo Zhang 
247e9fdf453SHongbo Zhang     for (cpu = sms->smp_cpus - 1; cpu >= 0; cpu--) {
248e9fdf453SHongbo Zhang         char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
249e9fdf453SHongbo Zhang         ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
250e9fdf453SHongbo Zhang         CPUState *cs = CPU(armcpu);
251999f6ebdSLeif Lindholm         uint64_t mpidr = sbsa_ref_cpu_mp_affinity(sms, cpu);
252e9fdf453SHongbo Zhang 
253e9fdf453SHongbo Zhang         qemu_fdt_add_subnode(sms->fdt, nodename);
254999f6ebdSLeif Lindholm         qemu_fdt_setprop_u64(sms->fdt, nodename, "reg", mpidr);
255e9fdf453SHongbo Zhang 
256e9fdf453SHongbo Zhang         if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
257e9fdf453SHongbo Zhang             qemu_fdt_setprop_cell(sms->fdt, nodename, "numa-node-id",
258e9fdf453SHongbo Zhang                 ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
259e9fdf453SHongbo Zhang         }
260e9fdf453SHongbo Zhang 
261e9fdf453SHongbo Zhang         g_free(nodename);
262e9fdf453SHongbo Zhang     }
263e9fdf453SHongbo Zhang }
264e9fdf453SHongbo Zhang 
265e9fdf453SHongbo Zhang #define SBSA_FLASH_SECTOR_SIZE (256 * KiB)
266e9fdf453SHongbo Zhang 
267e9fdf453SHongbo Zhang static PFlashCFI01 *sbsa_flash_create1(SBSAMachineState *sms,
268e9fdf453SHongbo Zhang                                         const char *name,
269e9fdf453SHongbo Zhang                                         const char *alias_prop_name)
270e9fdf453SHongbo Zhang {
271e9fdf453SHongbo Zhang     /*
272e9fdf453SHongbo Zhang      * Create a single flash device.  We use the same parameters as
273e9fdf453SHongbo Zhang      * the flash devices on the Versatile Express board.
274e9fdf453SHongbo Zhang      */
275df707969SMarkus Armbruster     DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
276e9fdf453SHongbo Zhang 
277e9fdf453SHongbo Zhang     qdev_prop_set_uint64(dev, "sector-length", SBSA_FLASH_SECTOR_SIZE);
278e9fdf453SHongbo Zhang     qdev_prop_set_uint8(dev, "width", 4);
279e9fdf453SHongbo Zhang     qdev_prop_set_uint8(dev, "device-width", 2);
280e9fdf453SHongbo Zhang     qdev_prop_set_bit(dev, "big-endian", false);
281e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id0", 0x89);
282e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id1", 0x18);
283e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id2", 0x00);
284e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id3", 0x00);
285e9fdf453SHongbo Zhang     qdev_prop_set_string(dev, "name", name);
286d2623129SMarkus Armbruster     object_property_add_child(OBJECT(sms), name, OBJECT(dev));
287e9fdf453SHongbo Zhang     object_property_add_alias(OBJECT(sms), alias_prop_name,
288d2623129SMarkus Armbruster                               OBJECT(dev), "drive");
289e9fdf453SHongbo Zhang     return PFLASH_CFI01(dev);
290e9fdf453SHongbo Zhang }
291e9fdf453SHongbo Zhang 
292e9fdf453SHongbo Zhang static void sbsa_flash_create(SBSAMachineState *sms)
293e9fdf453SHongbo Zhang {
294e9fdf453SHongbo Zhang     sms->flash[0] = sbsa_flash_create1(sms, "sbsa.flash0", "pflash0");
295e9fdf453SHongbo Zhang     sms->flash[1] = sbsa_flash_create1(sms, "sbsa.flash1", "pflash1");
296e9fdf453SHongbo Zhang }
297e9fdf453SHongbo Zhang 
298e9fdf453SHongbo Zhang static void sbsa_flash_map1(PFlashCFI01 *flash,
299e9fdf453SHongbo Zhang                             hwaddr base, hwaddr size,
300e9fdf453SHongbo Zhang                             MemoryRegion *sysmem)
301e9fdf453SHongbo Zhang {
302e9fdf453SHongbo Zhang     DeviceState *dev = DEVICE(flash);
303e9fdf453SHongbo Zhang 
3044cdd0a77SPhilippe Mathieu-Daudé     assert(QEMU_IS_ALIGNED(size, SBSA_FLASH_SECTOR_SIZE));
305e9fdf453SHongbo Zhang     assert(size / SBSA_FLASH_SECTOR_SIZE <= UINT32_MAX);
306e9fdf453SHongbo Zhang     qdev_prop_set_uint32(dev, "num-blocks", size / SBSA_FLASH_SECTOR_SIZE);
3073c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
308e9fdf453SHongbo Zhang 
309e9fdf453SHongbo Zhang     memory_region_add_subregion(sysmem, base,
310e9fdf453SHongbo Zhang                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
311e9fdf453SHongbo Zhang                                                        0));
312e9fdf453SHongbo Zhang }
313e9fdf453SHongbo Zhang 
314e9fdf453SHongbo Zhang static void sbsa_flash_map(SBSAMachineState *sms,
315e9fdf453SHongbo Zhang                            MemoryRegion *sysmem,
316e9fdf453SHongbo Zhang                            MemoryRegion *secure_sysmem)
317e9fdf453SHongbo Zhang {
318e9fdf453SHongbo Zhang     /*
319e9fdf453SHongbo Zhang      * Map two flash devices to fill the SBSA_FLASH space in the memmap.
320e9fdf453SHongbo Zhang      * sysmem is the system memory space. secure_sysmem is the secure view
321e9fdf453SHongbo Zhang      * of the system, and the first flash device should be made visible only
322e9fdf453SHongbo Zhang      * there. The second flash device is visible to both secure and nonsecure.
323e9fdf453SHongbo Zhang      */
324e9fdf453SHongbo Zhang     hwaddr flashsize = sbsa_ref_memmap[SBSA_FLASH].size / 2;
325e9fdf453SHongbo Zhang     hwaddr flashbase = sbsa_ref_memmap[SBSA_FLASH].base;
326e9fdf453SHongbo Zhang 
327e9fdf453SHongbo Zhang     sbsa_flash_map1(sms->flash[0], flashbase, flashsize,
328e9fdf453SHongbo Zhang                     secure_sysmem);
329e9fdf453SHongbo Zhang     sbsa_flash_map1(sms->flash[1], flashbase + flashsize, flashsize,
330e9fdf453SHongbo Zhang                     sysmem);
331e9fdf453SHongbo Zhang }
332e9fdf453SHongbo Zhang 
333e9fdf453SHongbo Zhang static bool sbsa_firmware_init(SBSAMachineState *sms,
334e9fdf453SHongbo Zhang                                MemoryRegion *sysmem,
335e9fdf453SHongbo Zhang                                MemoryRegion *secure_sysmem)
336e9fdf453SHongbo Zhang {
3370ad3b5d3SPaolo Bonzini     const char *bios_name;
338e9fdf453SHongbo Zhang     int i;
339e9fdf453SHongbo Zhang     BlockBackend *pflash_blk0;
340e9fdf453SHongbo Zhang 
341e9fdf453SHongbo Zhang     /* Map legacy -drive if=pflash to machine properties */
342e9fdf453SHongbo Zhang     for (i = 0; i < ARRAY_SIZE(sms->flash); i++) {
343e9fdf453SHongbo Zhang         pflash_cfi01_legacy_drive(sms->flash[i],
344e9fdf453SHongbo Zhang                                   drive_get(IF_PFLASH, 0, i));
345e9fdf453SHongbo Zhang     }
346e9fdf453SHongbo Zhang 
347e9fdf453SHongbo Zhang     sbsa_flash_map(sms, sysmem, secure_sysmem);
348e9fdf453SHongbo Zhang 
349e9fdf453SHongbo Zhang     pflash_blk0 = pflash_cfi01_get_blk(sms->flash[0]);
350e9fdf453SHongbo Zhang 
3510ad3b5d3SPaolo Bonzini     bios_name = MACHINE(sms)->firmware;
352e9fdf453SHongbo Zhang     if (bios_name) {
353e9fdf453SHongbo Zhang         char *fname;
354e9fdf453SHongbo Zhang         MemoryRegion *mr;
355e9fdf453SHongbo Zhang         int image_size;
356e9fdf453SHongbo Zhang 
357e9fdf453SHongbo Zhang         if (pflash_blk0) {
358e9fdf453SHongbo Zhang             error_report("The contents of the first flash device may be "
359e9fdf453SHongbo Zhang                          "specified with -bios or with -drive if=pflash... "
360e9fdf453SHongbo Zhang                          "but you cannot use both options at once");
361e9fdf453SHongbo Zhang             exit(1);
362e9fdf453SHongbo Zhang         }
363e9fdf453SHongbo Zhang 
364e9fdf453SHongbo Zhang         /* Fall back to -bios */
365e9fdf453SHongbo Zhang 
366e9fdf453SHongbo Zhang         fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
367e9fdf453SHongbo Zhang         if (!fname) {
368e9fdf453SHongbo Zhang             error_report("Could not find ROM image '%s'", bios_name);
369e9fdf453SHongbo Zhang             exit(1);
370e9fdf453SHongbo Zhang         }
371e9fdf453SHongbo Zhang         mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(sms->flash[0]), 0);
372e9fdf453SHongbo Zhang         image_size = load_image_mr(fname, mr);
373e9fdf453SHongbo Zhang         g_free(fname);
374e9fdf453SHongbo Zhang         if (image_size < 0) {
375e9fdf453SHongbo Zhang             error_report("Could not load ROM image '%s'", bios_name);
376e9fdf453SHongbo Zhang             exit(1);
377e9fdf453SHongbo Zhang         }
378e9fdf453SHongbo Zhang     }
379e9fdf453SHongbo Zhang 
380e9fdf453SHongbo Zhang     return pflash_blk0 || bios_name;
381e9fdf453SHongbo Zhang }
382e9fdf453SHongbo Zhang 
383e9fdf453SHongbo Zhang static void create_secure_ram(SBSAMachineState *sms,
384e9fdf453SHongbo Zhang                               MemoryRegion *secure_sysmem)
385e9fdf453SHongbo Zhang {
386e9fdf453SHongbo Zhang     MemoryRegion *secram = g_new(MemoryRegion, 1);
387e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_SECURE_MEM].base;
388e9fdf453SHongbo Zhang     hwaddr size = sbsa_ref_memmap[SBSA_SECURE_MEM].size;
389e9fdf453SHongbo Zhang 
390e9fdf453SHongbo Zhang     memory_region_init_ram(secram, NULL, "sbsa-ref.secure-ram", size,
391e9fdf453SHongbo Zhang                            &error_fatal);
392e9fdf453SHongbo Zhang     memory_region_add_subregion(secure_sysmem, base, secram);
393e9fdf453SHongbo Zhang }
394e9fdf453SHongbo Zhang 
39548ba18e6SPhilippe Mathieu-Daudé static void create_gic(SBSAMachineState *sms)
396e9fdf453SHongbo Zhang {
397cc7d44c2SLike Xu     unsigned int smp_cpus = MACHINE(sms)->smp.cpus;
398e9fdf453SHongbo Zhang     SysBusDevice *gicbusdev;
399e9fdf453SHongbo Zhang     const char *gictype;
400e9fdf453SHongbo Zhang     uint32_t redist0_capacity, redist0_count;
401e9fdf453SHongbo Zhang     int i;
402e9fdf453SHongbo Zhang 
403e9fdf453SHongbo Zhang     gictype = gicv3_class_name();
404e9fdf453SHongbo Zhang 
4053e80f690SMarkus Armbruster     sms->gic = qdev_new(gictype);
40648ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "revision", 3);
40748ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "num-cpu", smp_cpus);
408e9fdf453SHongbo Zhang     /*
409e9fdf453SHongbo Zhang      * Note that the num-irq property counts both internal and external
410e9fdf453SHongbo Zhang      * interrupts; there are always 32 of the former (mandated by GIC spec).
411e9fdf453SHongbo Zhang      */
41248ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "num-irq", NUM_IRQS + 32);
41348ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_bit(sms->gic, "has-security-extensions", true);
414e9fdf453SHongbo Zhang 
415e9fdf453SHongbo Zhang     redist0_capacity =
416e9fdf453SHongbo Zhang                 sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
417e9fdf453SHongbo Zhang     redist0_count = MIN(smp_cpus, redist0_capacity);
418e9fdf453SHongbo Zhang 
41948ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "len-redist-region-count", 1);
42048ba18e6SPhilippe Mathieu-Daudé     qdev_prop_set_uint32(sms->gic, "redist-region-count[0]", redist0_count);
421e9fdf453SHongbo Zhang 
42248ba18e6SPhilippe Mathieu-Daudé     gicbusdev = SYS_BUS_DEVICE(sms->gic);
4233c6ef471SMarkus Armbruster     sysbus_realize_and_unref(gicbusdev, &error_fatal);
424e9fdf453SHongbo Zhang     sysbus_mmio_map(gicbusdev, 0, sbsa_ref_memmap[SBSA_GIC_DIST].base);
425e9fdf453SHongbo Zhang     sysbus_mmio_map(gicbusdev, 1, sbsa_ref_memmap[SBSA_GIC_REDIST].base);
426e9fdf453SHongbo Zhang 
427e9fdf453SHongbo Zhang     /*
428e9fdf453SHongbo Zhang      * Wire the outputs from each CPU's generic timer and the GICv3
429e9fdf453SHongbo Zhang      * maintenance interrupt signal to the appropriate GIC PPI inputs,
430e9fdf453SHongbo Zhang      * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
431e9fdf453SHongbo Zhang      */
432e9fdf453SHongbo Zhang     for (i = 0; i < smp_cpus; i++) {
433e9fdf453SHongbo Zhang         DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
434e9fdf453SHongbo Zhang         int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
435e9fdf453SHongbo Zhang         int irq;
436e9fdf453SHongbo Zhang         /*
437e9fdf453SHongbo Zhang          * Mapping from the output timer irq lines from the CPU to the
438e9fdf453SHongbo Zhang          * GIC PPI inputs used for this board.
439e9fdf453SHongbo Zhang          */
440e9fdf453SHongbo Zhang         const int timer_irq[] = {
441e9fdf453SHongbo Zhang             [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
442e9fdf453SHongbo Zhang             [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
443e9fdf453SHongbo Zhang             [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
444e9fdf453SHongbo Zhang             [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
445e9fdf453SHongbo Zhang         };
446e9fdf453SHongbo Zhang 
447e9fdf453SHongbo Zhang         for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
448e9fdf453SHongbo Zhang             qdev_connect_gpio_out(cpudev, irq,
44948ba18e6SPhilippe Mathieu-Daudé                                   qdev_get_gpio_in(sms->gic,
450e9fdf453SHongbo Zhang                                                    ppibase + timer_irq[irq]));
451e9fdf453SHongbo Zhang         }
452e9fdf453SHongbo Zhang 
453e9fdf453SHongbo Zhang         qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0,
45448ba18e6SPhilippe Mathieu-Daudé                                     qdev_get_gpio_in(sms->gic, ppibase
455e9fdf453SHongbo Zhang                                                      + ARCH_GIC_MAINT_IRQ));
456e9fdf453SHongbo Zhang         qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
45748ba18e6SPhilippe Mathieu-Daudé                                     qdev_get_gpio_in(sms->gic, ppibase
458e9fdf453SHongbo Zhang                                                      + VIRTUAL_PMU_IRQ));
459e9fdf453SHongbo Zhang 
460e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
461e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + smp_cpus,
462e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
463e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
464e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
465e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
466e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
467e9fdf453SHongbo Zhang     }
468e9fdf453SHongbo Zhang }
469e9fdf453SHongbo Zhang 
47048ba18e6SPhilippe Mathieu-Daudé static void create_uart(const SBSAMachineState *sms, int uart,
471e9fdf453SHongbo Zhang                         MemoryRegion *mem, Chardev *chr)
472e9fdf453SHongbo Zhang {
473e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[uart].base;
474e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[uart];
4753e80f690SMarkus Armbruster     DeviceState *dev = qdev_new(TYPE_PL011);
476e9fdf453SHongbo Zhang     SysBusDevice *s = SYS_BUS_DEVICE(dev);
477e9fdf453SHongbo Zhang 
478e9fdf453SHongbo Zhang     qdev_prop_set_chr(dev, "chardev", chr);
4793c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
480e9fdf453SHongbo Zhang     memory_region_add_subregion(mem, base,
481e9fdf453SHongbo Zhang                                 sysbus_mmio_get_region(s, 0));
48248ba18e6SPhilippe Mathieu-Daudé     sysbus_connect_irq(s, 0, qdev_get_gpio_in(sms->gic, irq));
483e9fdf453SHongbo Zhang }
484e9fdf453SHongbo Zhang 
48548ba18e6SPhilippe Mathieu-Daudé static void create_rtc(const SBSAMachineState *sms)
486e9fdf453SHongbo Zhang {
487e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_RTC].base;
488e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_RTC];
489e9fdf453SHongbo Zhang 
49048ba18e6SPhilippe Mathieu-Daudé     sysbus_create_simple("pl031", base, qdev_get_gpio_in(sms->gic, irq));
491e9fdf453SHongbo Zhang }
492e9fdf453SHongbo Zhang 
493baabe7d0SShashi Mallela static void create_wdt(const SBSAMachineState *sms)
494baabe7d0SShashi Mallela {
495baabe7d0SShashi Mallela     hwaddr rbase = sbsa_ref_memmap[SBSA_GWDT_REFRESH].base;
496baabe7d0SShashi Mallela     hwaddr cbase = sbsa_ref_memmap[SBSA_GWDT_CONTROL].base;
497baabe7d0SShashi Mallela     DeviceState *dev = qdev_new(TYPE_WDT_SBSA);
498baabe7d0SShashi Mallela     SysBusDevice *s = SYS_BUS_DEVICE(dev);
49980d60a6dSEduardo Habkost     int irq = sbsa_ref_irqmap[SBSA_GWDT_WS0];
500baabe7d0SShashi Mallela 
501baabe7d0SShashi Mallela     sysbus_realize_and_unref(s, &error_fatal);
502baabe7d0SShashi Mallela     sysbus_mmio_map(s, 0, rbase);
503baabe7d0SShashi Mallela     sysbus_mmio_map(s, 1, cbase);
504baabe7d0SShashi Mallela     sysbus_connect_irq(s, 0, qdev_get_gpio_in(sms->gic, irq));
505baabe7d0SShashi Mallela }
506baabe7d0SShashi Mallela 
507e9fdf453SHongbo Zhang static DeviceState *gpio_key_dev;
508e9fdf453SHongbo Zhang static void sbsa_ref_powerdown_req(Notifier *n, void *opaque)
509e9fdf453SHongbo Zhang {
510e9fdf453SHongbo Zhang     /* use gpio Pin 3 for power button event */
511e9fdf453SHongbo Zhang     qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
512e9fdf453SHongbo Zhang }
513e9fdf453SHongbo Zhang 
514e9fdf453SHongbo Zhang static Notifier sbsa_ref_powerdown_notifier = {
515e9fdf453SHongbo Zhang     .notify = sbsa_ref_powerdown_req
516e9fdf453SHongbo Zhang };
517e9fdf453SHongbo Zhang 
51848ba18e6SPhilippe Mathieu-Daudé static void create_gpio(const SBSAMachineState *sms)
519e9fdf453SHongbo Zhang {
520e9fdf453SHongbo Zhang     DeviceState *pl061_dev;
521e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_GPIO].base;
522e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_GPIO];
523e9fdf453SHongbo Zhang 
52448ba18e6SPhilippe Mathieu-Daudé     pl061_dev = sysbus_create_simple("pl061", base,
52548ba18e6SPhilippe Mathieu-Daudé                                      qdev_get_gpio_in(sms->gic, irq));
526e9fdf453SHongbo Zhang 
527e9fdf453SHongbo Zhang     gpio_key_dev = sysbus_create_simple("gpio-key", -1,
528e9fdf453SHongbo Zhang                                         qdev_get_gpio_in(pl061_dev, 3));
529e9fdf453SHongbo Zhang 
530e9fdf453SHongbo Zhang     /* connect powerdown request */
531e9fdf453SHongbo Zhang     qemu_register_powerdown_notifier(&sbsa_ref_powerdown_notifier);
532e9fdf453SHongbo Zhang }
533e9fdf453SHongbo Zhang 
53448ba18e6SPhilippe Mathieu-Daudé static void create_ahci(const SBSAMachineState *sms)
535e9fdf453SHongbo Zhang {
536e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_AHCI].base;
537e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_AHCI];
538e9fdf453SHongbo Zhang     DeviceState *dev;
539e9fdf453SHongbo Zhang     DriveInfo *hd[NUM_SATA_PORTS];
540e9fdf453SHongbo Zhang     SysbusAHCIState *sysahci;
541e9fdf453SHongbo Zhang     AHCIState *ahci;
542e9fdf453SHongbo Zhang     int i;
543e9fdf453SHongbo Zhang 
5443e80f690SMarkus Armbruster     dev = qdev_new("sysbus-ahci");
545e9fdf453SHongbo Zhang     qdev_prop_set_uint32(dev, "num-ports", NUM_SATA_PORTS);
5463c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
547e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
54848ba18e6SPhilippe Mathieu-Daudé     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(sms->gic, irq));
549e9fdf453SHongbo Zhang 
550e9fdf453SHongbo Zhang     sysahci = SYSBUS_AHCI(dev);
551e9fdf453SHongbo Zhang     ahci = &sysahci->ahci;
552e9fdf453SHongbo Zhang     ide_drive_get(hd, ARRAY_SIZE(hd));
553e9fdf453SHongbo Zhang     for (i = 0; i < ahci->ports; i++) {
554e9fdf453SHongbo Zhang         if (hd[i] == NULL) {
555e9fdf453SHongbo Zhang             continue;
556e9fdf453SHongbo Zhang         }
557e9fdf453SHongbo Zhang         ide_create_drive(&ahci->dev[i].port, 0, hd[i]);
558e9fdf453SHongbo Zhang     }
559e9fdf453SHongbo Zhang }
560e9fdf453SHongbo Zhang 
56148ba18e6SPhilippe Mathieu-Daudé static void create_ehci(const SBSAMachineState *sms)
562e9fdf453SHongbo Zhang {
563e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_EHCI].base;
564e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_EHCI];
565e9fdf453SHongbo Zhang 
56648ba18e6SPhilippe Mathieu-Daudé     sysbus_create_simple("platform-ehci-usb", base,
56748ba18e6SPhilippe Mathieu-Daudé                          qdev_get_gpio_in(sms->gic, irq));
568e9fdf453SHongbo Zhang }
569e9fdf453SHongbo Zhang 
57048ba18e6SPhilippe Mathieu-Daudé static void create_smmu(const SBSAMachineState *sms, PCIBus *bus)
571e9fdf453SHongbo Zhang {
572e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_SMMU].base;
573e9fdf453SHongbo Zhang     int irq =  sbsa_ref_irqmap[SBSA_SMMU];
574e9fdf453SHongbo Zhang     DeviceState *dev;
575e9fdf453SHongbo Zhang     int i;
576e9fdf453SHongbo Zhang 
5773e80f690SMarkus Armbruster     dev = qdev_new("arm-smmuv3");
578e9fdf453SHongbo Zhang 
5795325cc34SMarkus Armbruster     object_property_set_link(OBJECT(dev), "primary-bus", OBJECT(bus),
580e9fdf453SHongbo Zhang                              &error_abort);
5813c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
582e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
583e9fdf453SHongbo Zhang     for (i = 0; i < NUM_SMMU_IRQS; i++) {
58448ba18e6SPhilippe Mathieu-Daudé         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
585b8bf3472SGraeme Gregory                            qdev_get_gpio_in(sms->gic, irq + i));
586e9fdf453SHongbo Zhang     }
587e9fdf453SHongbo Zhang }
588e9fdf453SHongbo Zhang 
58948ba18e6SPhilippe Mathieu-Daudé static void create_pcie(SBSAMachineState *sms)
590e9fdf453SHongbo Zhang {
591e9fdf453SHongbo Zhang     hwaddr base_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].base;
592e9fdf453SHongbo Zhang     hwaddr size_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].size;
593e9fdf453SHongbo Zhang     hwaddr base_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].base;
594e9fdf453SHongbo Zhang     hwaddr size_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].size;
595e9fdf453SHongbo Zhang     hwaddr base_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].base;
596e9fdf453SHongbo Zhang     hwaddr size_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].size;
597e9fdf453SHongbo Zhang     hwaddr base_pio = sbsa_ref_memmap[SBSA_PCIE_PIO].base;
598e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_PCIE];
599e9fdf453SHongbo Zhang     MemoryRegion *mmio_alias, *mmio_alias_high, *mmio_reg;
600e9fdf453SHongbo Zhang     MemoryRegion *ecam_alias, *ecam_reg;
601e9fdf453SHongbo Zhang     DeviceState *dev;
602e9fdf453SHongbo Zhang     PCIHostState *pci;
603e9fdf453SHongbo Zhang     int i;
604e9fdf453SHongbo Zhang 
6053e80f690SMarkus Armbruster     dev = qdev_new(TYPE_GPEX_HOST);
6063c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
607e9fdf453SHongbo Zhang 
608e9fdf453SHongbo Zhang     /* Map ECAM space */
609e9fdf453SHongbo Zhang     ecam_alias = g_new0(MemoryRegion, 1);
610e9fdf453SHongbo Zhang     ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
611e9fdf453SHongbo Zhang     memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
612e9fdf453SHongbo Zhang                              ecam_reg, 0, size_ecam);
613e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
614e9fdf453SHongbo Zhang 
615e9fdf453SHongbo Zhang     /* Map the MMIO space */
616e9fdf453SHongbo Zhang     mmio_alias = g_new0(MemoryRegion, 1);
617e9fdf453SHongbo Zhang     mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
618e9fdf453SHongbo Zhang     memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
619e9fdf453SHongbo Zhang                              mmio_reg, base_mmio, size_mmio);
620e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
621e9fdf453SHongbo Zhang 
622e9fdf453SHongbo Zhang     /* Map the MMIO_HIGH space */
623e9fdf453SHongbo Zhang     mmio_alias_high = g_new0(MemoryRegion, 1);
624e9fdf453SHongbo Zhang     memory_region_init_alias(mmio_alias_high, OBJECT(dev), "pcie-mmio-high",
625e9fdf453SHongbo Zhang                              mmio_reg, base_mmio_high, size_mmio_high);
626e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_mmio_high,
627e9fdf453SHongbo Zhang                                 mmio_alias_high);
628e9fdf453SHongbo Zhang 
629e9fdf453SHongbo Zhang     /* Map IO port space */
630e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
631e9fdf453SHongbo Zhang 
632e9fdf453SHongbo Zhang     for (i = 0; i < GPEX_NUM_IRQS; i++) {
63348ba18e6SPhilippe Mathieu-Daudé         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
634870f0051SGraeme Gregory                            qdev_get_gpio_in(sms->gic, irq + i));
635e9fdf453SHongbo Zhang         gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
636e9fdf453SHongbo Zhang     }
637e9fdf453SHongbo Zhang 
638e9fdf453SHongbo Zhang     pci = PCI_HOST_BRIDGE(dev);
639e9fdf453SHongbo Zhang     if (pci->bus) {
640e9fdf453SHongbo Zhang         for (i = 0; i < nb_nics; i++) {
641e9fdf453SHongbo Zhang             NICInfo *nd = &nd_table[i];
642e9fdf453SHongbo Zhang 
643e9fdf453SHongbo Zhang             if (!nd->model) {
644e9fdf453SHongbo Zhang                 nd->model = g_strdup("e1000e");
645e9fdf453SHongbo Zhang             }
646e9fdf453SHongbo Zhang 
647e9fdf453SHongbo Zhang             pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
648e9fdf453SHongbo Zhang         }
649e9fdf453SHongbo Zhang     }
650e9fdf453SHongbo Zhang 
651e9fdf453SHongbo Zhang     pci_create_simple(pci->bus, -1, "VGA");
652e9fdf453SHongbo Zhang 
65348ba18e6SPhilippe Mathieu-Daudé     create_smmu(sms, pci->bus);
654e9fdf453SHongbo Zhang }
655e9fdf453SHongbo Zhang 
656e9fdf453SHongbo Zhang static void *sbsa_ref_dtb(const struct arm_boot_info *binfo, int *fdt_size)
657e9fdf453SHongbo Zhang {
658e9fdf453SHongbo Zhang     const SBSAMachineState *board = container_of(binfo, SBSAMachineState,
659e9fdf453SHongbo Zhang                                                  bootinfo);
660e9fdf453SHongbo Zhang 
661e9fdf453SHongbo Zhang     *fdt_size = board->fdt_size;
662e9fdf453SHongbo Zhang     return board->fdt;
663e9fdf453SHongbo Zhang }
664e9fdf453SHongbo Zhang 
6653f462bf0SGraeme Gregory static void create_secure_ec(MemoryRegion *mem)
6663f462bf0SGraeme Gregory {
6673f462bf0SGraeme Gregory     hwaddr base = sbsa_ref_memmap[SBSA_SECURE_EC].base;
6683f462bf0SGraeme Gregory     DeviceState *dev = qdev_new("sbsa-ec");
6693f462bf0SGraeme Gregory     SysBusDevice *s = SYS_BUS_DEVICE(dev);
6703f462bf0SGraeme Gregory 
6713f462bf0SGraeme Gregory     memory_region_add_subregion(mem, base,
6723f462bf0SGraeme Gregory                                 sysbus_mmio_get_region(s, 0));
6733f462bf0SGraeme Gregory }
6743f462bf0SGraeme Gregory 
67564580903SHongbo Zhang static void sbsa_ref_init(MachineState *machine)
67664580903SHongbo Zhang {
677cc7d44c2SLike Xu     unsigned int smp_cpus = machine->smp.cpus;
678cc7d44c2SLike Xu     unsigned int max_cpus = machine->smp.max_cpus;
67964580903SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(machine);
68064580903SHongbo Zhang     MachineClass *mc = MACHINE_GET_CLASS(machine);
68164580903SHongbo Zhang     MemoryRegion *sysmem = get_system_memory();
682c8ead571SPeter Maydell     MemoryRegion *secure_sysmem = g_new(MemoryRegion, 1);
683e9fdf453SHongbo Zhang     bool firmware_loaded;
68464580903SHongbo Zhang     const CPUArchIdList *possible_cpus;
68564580903SHongbo Zhang     int n, sbsa_max_cpus;
68664580903SHongbo Zhang 
687ce3adffcSMarcin Juszkiewicz     if (!cpu_type_valid(machine->cpu_type)) {
688b84722cfSShuuichirou Ishii         error_report("sbsa-ref: CPU type %s not supported", machine->cpu_type);
68964580903SHongbo Zhang         exit(1);
69064580903SHongbo Zhang     }
69164580903SHongbo Zhang 
69264580903SHongbo Zhang     if (kvm_enabled()) {
69364580903SHongbo Zhang         error_report("sbsa-ref: KVM is not supported for this machine");
69464580903SHongbo Zhang         exit(1);
69564580903SHongbo Zhang     }
69664580903SHongbo Zhang 
69764580903SHongbo Zhang     /*
698e9fdf453SHongbo Zhang      * The Secure view of the world is the same as the NonSecure,
699e9fdf453SHongbo Zhang      * but with a few extra devices. Create it as a container region
700e9fdf453SHongbo Zhang      * containing the system memory at low priority; any secure-only
701e9fdf453SHongbo Zhang      * devices go in at higher priority and take precedence.
702e9fdf453SHongbo Zhang      */
703e9fdf453SHongbo Zhang     memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
704e9fdf453SHongbo Zhang                        UINT64_MAX);
705e9fdf453SHongbo Zhang     memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
706e9fdf453SHongbo Zhang 
707c8ead571SPeter Maydell     firmware_loaded = sbsa_firmware_init(sms, sysmem, secure_sysmem);
708e9fdf453SHongbo Zhang 
709e9fdf453SHongbo Zhang     /*
71064580903SHongbo Zhang      * This machine has EL3 enabled, external firmware should supply PSCI
71164580903SHongbo Zhang      * implementation, so the QEMU's internal PSCI is disabled.
71264580903SHongbo Zhang      */
71364580903SHongbo Zhang     sms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
71464580903SHongbo Zhang 
71564580903SHongbo Zhang     sbsa_max_cpus = sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
71664580903SHongbo Zhang 
71764580903SHongbo Zhang     if (max_cpus > sbsa_max_cpus) {
71864580903SHongbo Zhang         error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
71964580903SHongbo Zhang                      "supported by machine 'sbsa-ref' (%d)",
72064580903SHongbo Zhang                      max_cpus, sbsa_max_cpus);
72164580903SHongbo Zhang         exit(1);
72264580903SHongbo Zhang     }
72364580903SHongbo Zhang 
72464580903SHongbo Zhang     sms->smp_cpus = smp_cpus;
72564580903SHongbo Zhang 
72664580903SHongbo Zhang     if (machine->ram_size > sbsa_ref_memmap[SBSA_MEM].size) {
72764580903SHongbo Zhang         error_report("sbsa-ref: cannot model more than %dGB RAM", RAMLIMIT_GB);
72864580903SHongbo Zhang         exit(1);
72964580903SHongbo Zhang     }
73064580903SHongbo Zhang 
73164580903SHongbo Zhang     possible_cpus = mc->possible_cpu_arch_ids(machine);
73264580903SHongbo Zhang     for (n = 0; n < possible_cpus->len; n++) {
73364580903SHongbo Zhang         Object *cpuobj;
73464580903SHongbo Zhang         CPUState *cs;
73564580903SHongbo Zhang 
73664580903SHongbo Zhang         if (n >= smp_cpus) {
73764580903SHongbo Zhang             break;
73864580903SHongbo Zhang         }
73964580903SHongbo Zhang 
74064580903SHongbo Zhang         cpuobj = object_new(possible_cpus->cpus[n].type);
7415325cc34SMarkus Armbruster         object_property_set_int(cpuobj, "mp-affinity",
7425325cc34SMarkus Armbruster                                 possible_cpus->cpus[n].arch_id, NULL);
74364580903SHongbo Zhang 
74464580903SHongbo Zhang         cs = CPU(cpuobj);
74564580903SHongbo Zhang         cs->cpu_index = n;
74664580903SHongbo Zhang 
74764580903SHongbo Zhang         numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
74864580903SHongbo Zhang                           &error_fatal);
74964580903SHongbo Zhang 
750efba1595SDaniel P. Berrangé         if (object_property_find(cpuobj, "reset-cbar")) {
7515325cc34SMarkus Armbruster             object_property_set_int(cpuobj, "reset-cbar",
75264580903SHongbo Zhang                                     sbsa_ref_memmap[SBSA_CPUPERIPHS].base,
7535325cc34SMarkus Armbruster                                     &error_abort);
75464580903SHongbo Zhang         }
75564580903SHongbo Zhang 
7565325cc34SMarkus Armbruster         object_property_set_link(cpuobj, "memory", OBJECT(sysmem),
75764580903SHongbo Zhang                                  &error_abort);
75864580903SHongbo Zhang 
7595325cc34SMarkus Armbruster         object_property_set_link(cpuobj, "secure-memory",
7605325cc34SMarkus Armbruster                                  OBJECT(secure_sysmem), &error_abort);
76164580903SHongbo Zhang 
762ce189ab2SMarkus Armbruster         qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
76364580903SHongbo Zhang         object_unref(cpuobj);
76464580903SHongbo Zhang     }
76564580903SHongbo Zhang 
7663818ed92SIgor Mammedov     memory_region_add_subregion(sysmem, sbsa_ref_memmap[SBSA_MEM].base,
7673818ed92SIgor Mammedov                                 machine->ram);
76864580903SHongbo Zhang 
769e9fdf453SHongbo Zhang     create_fdt(sms);
770e9fdf453SHongbo Zhang 
771e9fdf453SHongbo Zhang     create_secure_ram(sms, secure_sysmem);
772e9fdf453SHongbo Zhang 
77348ba18e6SPhilippe Mathieu-Daudé     create_gic(sms);
774e9fdf453SHongbo Zhang 
77548ba18e6SPhilippe Mathieu-Daudé     create_uart(sms, SBSA_UART, sysmem, serial_hd(0));
77648ba18e6SPhilippe Mathieu-Daudé     create_uart(sms, SBSA_SECURE_UART, secure_sysmem, serial_hd(1));
777e9fdf453SHongbo Zhang     /* Second secure UART for RAS and MM from EL0 */
77848ba18e6SPhilippe Mathieu-Daudé     create_uart(sms, SBSA_SECURE_UART_MM, secure_sysmem, serial_hd(2));
779e9fdf453SHongbo Zhang 
78048ba18e6SPhilippe Mathieu-Daudé     create_rtc(sms);
781e9fdf453SHongbo Zhang 
782baabe7d0SShashi Mallela     create_wdt(sms);
783baabe7d0SShashi Mallela 
78448ba18e6SPhilippe Mathieu-Daudé     create_gpio(sms);
785e9fdf453SHongbo Zhang 
78648ba18e6SPhilippe Mathieu-Daudé     create_ahci(sms);
787e9fdf453SHongbo Zhang 
78848ba18e6SPhilippe Mathieu-Daudé     create_ehci(sms);
789e9fdf453SHongbo Zhang 
79048ba18e6SPhilippe Mathieu-Daudé     create_pcie(sms);
791e9fdf453SHongbo Zhang 
7923f462bf0SGraeme Gregory     create_secure_ec(secure_sysmem);
7933f462bf0SGraeme Gregory 
79464580903SHongbo Zhang     sms->bootinfo.ram_size = machine->ram_size;
79564580903SHongbo Zhang     sms->bootinfo.board_id = -1;
79664580903SHongbo Zhang     sms->bootinfo.loader_start = sbsa_ref_memmap[SBSA_MEM].base;
797e9fdf453SHongbo Zhang     sms->bootinfo.get_dtb = sbsa_ref_dtb;
798e9fdf453SHongbo Zhang     sms->bootinfo.firmware_loaded = firmware_loaded;
7992744ece8STao Xu     arm_load_kernel(ARM_CPU(first_cpu), machine, &sms->bootinfo);
80064580903SHongbo Zhang }
80164580903SHongbo Zhang 
80264580903SHongbo Zhang static const CPUArchIdList *sbsa_ref_possible_cpu_arch_ids(MachineState *ms)
80364580903SHongbo Zhang {
804cc7d44c2SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
80564580903SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(ms);
80664580903SHongbo Zhang     int n;
80764580903SHongbo Zhang 
80864580903SHongbo Zhang     if (ms->possible_cpus) {
80964580903SHongbo Zhang         assert(ms->possible_cpus->len == max_cpus);
81064580903SHongbo Zhang         return ms->possible_cpus;
81164580903SHongbo Zhang     }
81264580903SHongbo Zhang 
81364580903SHongbo Zhang     ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
81464580903SHongbo Zhang                                   sizeof(CPUArchId) * max_cpus);
81564580903SHongbo Zhang     ms->possible_cpus->len = max_cpus;
81664580903SHongbo Zhang     for (n = 0; n < ms->possible_cpus->len; n++) {
81764580903SHongbo Zhang         ms->possible_cpus->cpus[n].type = ms->cpu_type;
81864580903SHongbo Zhang         ms->possible_cpus->cpus[n].arch_id =
81964580903SHongbo Zhang             sbsa_ref_cpu_mp_affinity(sms, n);
82064580903SHongbo Zhang         ms->possible_cpus->cpus[n].props.has_thread_id = true;
82164580903SHongbo Zhang         ms->possible_cpus->cpus[n].props.thread_id = n;
82264580903SHongbo Zhang     }
82364580903SHongbo Zhang     return ms->possible_cpus;
82464580903SHongbo Zhang }
82564580903SHongbo Zhang 
82664580903SHongbo Zhang static CpuInstanceProperties
82764580903SHongbo Zhang sbsa_ref_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
82864580903SHongbo Zhang {
82964580903SHongbo Zhang     MachineClass *mc = MACHINE_GET_CLASS(ms);
83064580903SHongbo Zhang     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
83164580903SHongbo Zhang 
83264580903SHongbo Zhang     assert(cpu_index < possible_cpus->len);
83364580903SHongbo Zhang     return possible_cpus->cpus[cpu_index].props;
83464580903SHongbo Zhang }
83564580903SHongbo Zhang 
83664580903SHongbo Zhang static int64_t
83764580903SHongbo Zhang sbsa_ref_get_default_cpu_node_id(const MachineState *ms, int idx)
83864580903SHongbo Zhang {
839aa570207STao Xu     return idx % ms->numa_state->num_nodes;
84064580903SHongbo Zhang }
84164580903SHongbo Zhang 
842e9fdf453SHongbo Zhang static void sbsa_ref_instance_init(Object *obj)
843e9fdf453SHongbo Zhang {
844e9fdf453SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(obj);
845e9fdf453SHongbo Zhang 
846e9fdf453SHongbo Zhang     sbsa_flash_create(sms);
847e9fdf453SHongbo Zhang }
848e9fdf453SHongbo Zhang 
84964580903SHongbo Zhang static void sbsa_ref_class_init(ObjectClass *oc, void *data)
85064580903SHongbo Zhang {
85164580903SHongbo Zhang     MachineClass *mc = MACHINE_CLASS(oc);
85264580903SHongbo Zhang 
85364580903SHongbo Zhang     mc->init = sbsa_ref_init;
85464580903SHongbo Zhang     mc->desc = "QEMU 'SBSA Reference' ARM Virtual Machine";
85564580903SHongbo Zhang     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a57");
85664580903SHongbo Zhang     mc->max_cpus = 512;
85764580903SHongbo Zhang     mc->pci_allow_0_address = true;
85864580903SHongbo Zhang     mc->minimum_page_bits = 12;
85964580903SHongbo Zhang     mc->block_default_type = IF_IDE;
86064580903SHongbo Zhang     mc->no_cdrom = 1;
86164580903SHongbo Zhang     mc->default_ram_size = 1 * GiB;
8623818ed92SIgor Mammedov     mc->default_ram_id = "sbsa-ref.ram";
86364580903SHongbo Zhang     mc->default_cpus = 4;
86464580903SHongbo Zhang     mc->possible_cpu_arch_ids = sbsa_ref_possible_cpu_arch_ids;
86564580903SHongbo Zhang     mc->cpu_index_to_instance_props = sbsa_ref_cpu_index_to_props;
86664580903SHongbo Zhang     mc->get_default_cpu_node_id = sbsa_ref_get_default_cpu_node_id;
86764580903SHongbo Zhang }
86864580903SHongbo Zhang 
86964580903SHongbo Zhang static const TypeInfo sbsa_ref_info = {
87064580903SHongbo Zhang     .name          = TYPE_SBSA_MACHINE,
87164580903SHongbo Zhang     .parent        = TYPE_MACHINE,
872e9fdf453SHongbo Zhang     .instance_init = sbsa_ref_instance_init,
87364580903SHongbo Zhang     .class_init    = sbsa_ref_class_init,
87464580903SHongbo Zhang     .instance_size = sizeof(SBSAMachineState),
87564580903SHongbo Zhang };
87664580903SHongbo Zhang 
87764580903SHongbo Zhang static void sbsa_ref_machine_init(void)
87864580903SHongbo Zhang {
87964580903SHongbo Zhang     type_register_static(&sbsa_ref_info);
88064580903SHongbo Zhang }
88164580903SHongbo Zhang 
88264580903SHongbo Zhang type_init(sbsa_ref_machine_init);
883