xref: /qemu/hw/arm/sbsa-ref.c (revision e9fdf453240ebb57894f5382bb2da6a97cdb2998)
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"
21*e9fdf453SHongbo Zhang #include "qemu-common.h"
2264580903SHongbo Zhang #include "qapi/error.h"
2364580903SHongbo Zhang #include "qemu/error-report.h"
2464580903SHongbo Zhang #include "qemu/units.h"
25*e9fdf453SHongbo Zhang #include "sysemu/device_tree.h"
2664580903SHongbo Zhang #include "sysemu/numa.h"
2764580903SHongbo Zhang #include "sysemu/sysemu.h"
2864580903SHongbo Zhang #include "exec/address-spaces.h"
2964580903SHongbo Zhang #include "exec/hwaddr.h"
3064580903SHongbo Zhang #include "kvm_arm.h"
3164580903SHongbo Zhang #include "hw/arm/boot.h"
32*e9fdf453SHongbo Zhang #include "hw/block/flash.h"
3364580903SHongbo Zhang #include "hw/boards.h"
34*e9fdf453SHongbo Zhang #include "hw/ide/internal.h"
35*e9fdf453SHongbo Zhang #include "hw/ide/ahci_internal.h"
3664580903SHongbo Zhang #include "hw/intc/arm_gicv3_common.h"
37*e9fdf453SHongbo Zhang #include "hw/loader.h"
38*e9fdf453SHongbo Zhang #include "hw/pci-host/gpex.h"
39*e9fdf453SHongbo Zhang #include "hw/usb.h"
40*e9fdf453SHongbo Zhang #include "net/net.h"
4164580903SHongbo Zhang 
4264580903SHongbo Zhang #define RAMLIMIT_GB 8192
4364580903SHongbo Zhang #define RAMLIMIT_BYTES (RAMLIMIT_GB * GiB)
4464580903SHongbo Zhang 
45*e9fdf453SHongbo Zhang #define NUM_IRQS        256
46*e9fdf453SHongbo Zhang #define NUM_SMMU_IRQS   4
47*e9fdf453SHongbo Zhang #define NUM_SATA_PORTS  6
48*e9fdf453SHongbo Zhang 
49*e9fdf453SHongbo Zhang #define VIRTUAL_PMU_IRQ        7
50*e9fdf453SHongbo Zhang #define ARCH_GIC_MAINT_IRQ     9
51*e9fdf453SHongbo Zhang #define ARCH_TIMER_VIRT_IRQ    11
52*e9fdf453SHongbo Zhang #define ARCH_TIMER_S_EL1_IRQ   13
53*e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL1_IRQ  14
54*e9fdf453SHongbo Zhang #define ARCH_TIMER_NS_EL2_IRQ  10
55*e9fdf453SHongbo Zhang 
5664580903SHongbo Zhang enum {
5764580903SHongbo Zhang     SBSA_FLASH,
5864580903SHongbo Zhang     SBSA_MEM,
5964580903SHongbo Zhang     SBSA_CPUPERIPHS,
6064580903SHongbo Zhang     SBSA_GIC_DIST,
6164580903SHongbo Zhang     SBSA_GIC_REDIST,
6264580903SHongbo Zhang     SBSA_SMMU,
6364580903SHongbo Zhang     SBSA_UART,
6464580903SHongbo Zhang     SBSA_RTC,
6564580903SHongbo Zhang     SBSA_PCIE,
6664580903SHongbo Zhang     SBSA_PCIE_MMIO,
6764580903SHongbo Zhang     SBSA_PCIE_MMIO_HIGH,
6864580903SHongbo Zhang     SBSA_PCIE_PIO,
6964580903SHongbo Zhang     SBSA_PCIE_ECAM,
7064580903SHongbo Zhang     SBSA_GPIO,
7164580903SHongbo Zhang     SBSA_SECURE_UART,
7264580903SHongbo Zhang     SBSA_SECURE_UART_MM,
7364580903SHongbo Zhang     SBSA_SECURE_MEM,
7464580903SHongbo Zhang     SBSA_AHCI,
7564580903SHongbo Zhang     SBSA_EHCI,
7664580903SHongbo Zhang };
7764580903SHongbo Zhang 
7864580903SHongbo Zhang typedef struct MemMapEntry {
7964580903SHongbo Zhang     hwaddr base;
8064580903SHongbo Zhang     hwaddr size;
8164580903SHongbo Zhang } MemMapEntry;
8264580903SHongbo Zhang 
8364580903SHongbo Zhang typedef struct {
8464580903SHongbo Zhang     MachineState parent;
8564580903SHongbo Zhang     struct arm_boot_info bootinfo;
8664580903SHongbo Zhang     int smp_cpus;
8764580903SHongbo Zhang     void *fdt;
8864580903SHongbo Zhang     int fdt_size;
8964580903SHongbo Zhang     int psci_conduit;
90*e9fdf453SHongbo Zhang     PFlashCFI01 *flash[2];
9164580903SHongbo Zhang } SBSAMachineState;
9264580903SHongbo Zhang 
9364580903SHongbo Zhang #define TYPE_SBSA_MACHINE   MACHINE_TYPE_NAME("sbsa-ref")
9464580903SHongbo Zhang #define SBSA_MACHINE(obj) \
9564580903SHongbo Zhang     OBJECT_CHECK(SBSAMachineState, (obj), TYPE_SBSA_MACHINE)
9664580903SHongbo Zhang 
9764580903SHongbo Zhang static const MemMapEntry sbsa_ref_memmap[] = {
9864580903SHongbo Zhang     /* 512M boot ROM */
9964580903SHongbo Zhang     [SBSA_FLASH] =              {          0, 0x20000000 },
10064580903SHongbo Zhang     /* 512M secure memory */
10164580903SHongbo Zhang     [SBSA_SECURE_MEM] =         { 0x20000000, 0x20000000 },
10264580903SHongbo Zhang     /* Space reserved for CPU peripheral devices */
10364580903SHongbo Zhang     [SBSA_CPUPERIPHS] =         { 0x40000000, 0x00040000 },
10464580903SHongbo Zhang     [SBSA_GIC_DIST] =           { 0x40060000, 0x00010000 },
10564580903SHongbo Zhang     [SBSA_GIC_REDIST] =         { 0x40080000, 0x04000000 },
10664580903SHongbo Zhang     [SBSA_UART] =               { 0x60000000, 0x00001000 },
10764580903SHongbo Zhang     [SBSA_RTC] =                { 0x60010000, 0x00001000 },
10864580903SHongbo Zhang     [SBSA_GPIO] =               { 0x60020000, 0x00001000 },
10964580903SHongbo Zhang     [SBSA_SECURE_UART] =        { 0x60030000, 0x00001000 },
11064580903SHongbo Zhang     [SBSA_SECURE_UART_MM] =     { 0x60040000, 0x00001000 },
11164580903SHongbo Zhang     [SBSA_SMMU] =               { 0x60050000, 0x00020000 },
11264580903SHongbo Zhang     /* Space here reserved for more SMMUs */
11364580903SHongbo Zhang     [SBSA_AHCI] =               { 0x60100000, 0x00010000 },
11464580903SHongbo Zhang     [SBSA_EHCI] =               { 0x60110000, 0x00010000 },
11564580903SHongbo Zhang     /* Space here reserved for other devices */
11664580903SHongbo Zhang     [SBSA_PCIE_PIO] =           { 0x7fff0000, 0x00010000 },
11764580903SHongbo Zhang     /* 32-bit address PCIE MMIO space */
11864580903SHongbo Zhang     [SBSA_PCIE_MMIO] =          { 0x80000000, 0x70000000 },
11964580903SHongbo Zhang     /* 256M PCIE ECAM space */
12064580903SHongbo Zhang     [SBSA_PCIE_ECAM] =          { 0xf0000000, 0x10000000 },
12164580903SHongbo Zhang     /* ~1TB PCIE MMIO space (4GB to 1024GB boundary) */
12264580903SHongbo Zhang     [SBSA_PCIE_MMIO_HIGH] =     { 0x100000000ULL, 0xFF00000000ULL },
12364580903SHongbo Zhang     [SBSA_MEM] =                { 0x10000000000ULL, RAMLIMIT_BYTES },
12464580903SHongbo Zhang };
12564580903SHongbo Zhang 
126*e9fdf453SHongbo Zhang static const int sbsa_ref_irqmap[] = {
127*e9fdf453SHongbo Zhang     [SBSA_UART] = 1,
128*e9fdf453SHongbo Zhang     [SBSA_RTC] = 2,
129*e9fdf453SHongbo Zhang     [SBSA_PCIE] = 3, /* ... to 6 */
130*e9fdf453SHongbo Zhang     [SBSA_GPIO] = 7,
131*e9fdf453SHongbo Zhang     [SBSA_SECURE_UART] = 8,
132*e9fdf453SHongbo Zhang     [SBSA_SECURE_UART_MM] = 9,
133*e9fdf453SHongbo Zhang     [SBSA_AHCI] = 10,
134*e9fdf453SHongbo Zhang     [SBSA_EHCI] = 11,
135*e9fdf453SHongbo Zhang };
136*e9fdf453SHongbo Zhang 
137*e9fdf453SHongbo Zhang /*
138*e9fdf453SHongbo Zhang  * Firmware on this machine only uses ACPI table to load OS, these limited
139*e9fdf453SHongbo Zhang  * device tree nodes are just to let firmware know the info which varies from
140*e9fdf453SHongbo Zhang  * command line parameters, so it is not necessary to be fully compatible
141*e9fdf453SHongbo Zhang  * with the kernel CPU and NUMA binding rules.
142*e9fdf453SHongbo Zhang  */
143*e9fdf453SHongbo Zhang static void create_fdt(SBSAMachineState *sms)
144*e9fdf453SHongbo Zhang {
145*e9fdf453SHongbo Zhang     void *fdt = create_device_tree(&sms->fdt_size);
146*e9fdf453SHongbo Zhang     const MachineState *ms = MACHINE(sms);
147*e9fdf453SHongbo Zhang     int cpu;
148*e9fdf453SHongbo Zhang 
149*e9fdf453SHongbo Zhang     if (!fdt) {
150*e9fdf453SHongbo Zhang         error_report("create_device_tree() failed");
151*e9fdf453SHongbo Zhang         exit(1);
152*e9fdf453SHongbo Zhang     }
153*e9fdf453SHongbo Zhang 
154*e9fdf453SHongbo Zhang     sms->fdt = fdt;
155*e9fdf453SHongbo Zhang 
156*e9fdf453SHongbo Zhang     qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,sbsa-ref");
157*e9fdf453SHongbo Zhang     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
158*e9fdf453SHongbo Zhang     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
159*e9fdf453SHongbo Zhang 
160*e9fdf453SHongbo Zhang     if (have_numa_distance) {
161*e9fdf453SHongbo Zhang         int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
162*e9fdf453SHongbo Zhang         uint32_t *matrix = g_malloc0(size);
163*e9fdf453SHongbo Zhang         int idx, i, j;
164*e9fdf453SHongbo Zhang 
165*e9fdf453SHongbo Zhang         for (i = 0; i < nb_numa_nodes; i++) {
166*e9fdf453SHongbo Zhang             for (j = 0; j < nb_numa_nodes; j++) {
167*e9fdf453SHongbo Zhang                 idx = (i * nb_numa_nodes + j) * 3;
168*e9fdf453SHongbo Zhang                 matrix[idx + 0] = cpu_to_be32(i);
169*e9fdf453SHongbo Zhang                 matrix[idx + 1] = cpu_to_be32(j);
170*e9fdf453SHongbo Zhang                 matrix[idx + 2] = cpu_to_be32(numa_info[i].distance[j]);
171*e9fdf453SHongbo Zhang             }
172*e9fdf453SHongbo Zhang         }
173*e9fdf453SHongbo Zhang 
174*e9fdf453SHongbo Zhang         qemu_fdt_add_subnode(fdt, "/distance-map");
175*e9fdf453SHongbo Zhang         qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
176*e9fdf453SHongbo Zhang                          matrix, size);
177*e9fdf453SHongbo Zhang         g_free(matrix);
178*e9fdf453SHongbo Zhang     }
179*e9fdf453SHongbo Zhang 
180*e9fdf453SHongbo Zhang     qemu_fdt_add_subnode(sms->fdt, "/cpus");
181*e9fdf453SHongbo Zhang 
182*e9fdf453SHongbo Zhang     for (cpu = sms->smp_cpus - 1; cpu >= 0; cpu--) {
183*e9fdf453SHongbo Zhang         char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
184*e9fdf453SHongbo Zhang         ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
185*e9fdf453SHongbo Zhang         CPUState *cs = CPU(armcpu);
186*e9fdf453SHongbo Zhang 
187*e9fdf453SHongbo Zhang         qemu_fdt_add_subnode(sms->fdt, nodename);
188*e9fdf453SHongbo Zhang 
189*e9fdf453SHongbo Zhang         if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
190*e9fdf453SHongbo Zhang             qemu_fdt_setprop_cell(sms->fdt, nodename, "numa-node-id",
191*e9fdf453SHongbo Zhang                 ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
192*e9fdf453SHongbo Zhang         }
193*e9fdf453SHongbo Zhang 
194*e9fdf453SHongbo Zhang         g_free(nodename);
195*e9fdf453SHongbo Zhang     }
196*e9fdf453SHongbo Zhang }
197*e9fdf453SHongbo Zhang 
198*e9fdf453SHongbo Zhang #define SBSA_FLASH_SECTOR_SIZE (256 * KiB)
199*e9fdf453SHongbo Zhang 
200*e9fdf453SHongbo Zhang static PFlashCFI01 *sbsa_flash_create1(SBSAMachineState *sms,
201*e9fdf453SHongbo Zhang                                         const char *name,
202*e9fdf453SHongbo Zhang                                         const char *alias_prop_name)
203*e9fdf453SHongbo Zhang {
204*e9fdf453SHongbo Zhang     /*
205*e9fdf453SHongbo Zhang      * Create a single flash device.  We use the same parameters as
206*e9fdf453SHongbo Zhang      * the flash devices on the Versatile Express board.
207*e9fdf453SHongbo Zhang      */
208*e9fdf453SHongbo Zhang     DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI01);
209*e9fdf453SHongbo Zhang 
210*e9fdf453SHongbo Zhang     qdev_prop_set_uint64(dev, "sector-length", SBSA_FLASH_SECTOR_SIZE);
211*e9fdf453SHongbo Zhang     qdev_prop_set_uint8(dev, "width", 4);
212*e9fdf453SHongbo Zhang     qdev_prop_set_uint8(dev, "device-width", 2);
213*e9fdf453SHongbo Zhang     qdev_prop_set_bit(dev, "big-endian", false);
214*e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id0", 0x89);
215*e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id1", 0x18);
216*e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id2", 0x00);
217*e9fdf453SHongbo Zhang     qdev_prop_set_uint16(dev, "id3", 0x00);
218*e9fdf453SHongbo Zhang     qdev_prop_set_string(dev, "name", name);
219*e9fdf453SHongbo Zhang     object_property_add_child(OBJECT(sms), name, OBJECT(dev),
220*e9fdf453SHongbo Zhang                               &error_abort);
221*e9fdf453SHongbo Zhang     object_property_add_alias(OBJECT(sms), alias_prop_name,
222*e9fdf453SHongbo Zhang                               OBJECT(dev), "drive", &error_abort);
223*e9fdf453SHongbo Zhang     return PFLASH_CFI01(dev);
224*e9fdf453SHongbo Zhang }
225*e9fdf453SHongbo Zhang 
226*e9fdf453SHongbo Zhang static void sbsa_flash_create(SBSAMachineState *sms)
227*e9fdf453SHongbo Zhang {
228*e9fdf453SHongbo Zhang     sms->flash[0] = sbsa_flash_create1(sms, "sbsa.flash0", "pflash0");
229*e9fdf453SHongbo Zhang     sms->flash[1] = sbsa_flash_create1(sms, "sbsa.flash1", "pflash1");
230*e9fdf453SHongbo Zhang }
231*e9fdf453SHongbo Zhang 
232*e9fdf453SHongbo Zhang static void sbsa_flash_map1(PFlashCFI01 *flash,
233*e9fdf453SHongbo Zhang                             hwaddr base, hwaddr size,
234*e9fdf453SHongbo Zhang                             MemoryRegion *sysmem)
235*e9fdf453SHongbo Zhang {
236*e9fdf453SHongbo Zhang     DeviceState *dev = DEVICE(flash);
237*e9fdf453SHongbo Zhang 
238*e9fdf453SHongbo Zhang     assert(size % SBSA_FLASH_SECTOR_SIZE == 0);
239*e9fdf453SHongbo Zhang     assert(size / SBSA_FLASH_SECTOR_SIZE <= UINT32_MAX);
240*e9fdf453SHongbo Zhang     qdev_prop_set_uint32(dev, "num-blocks", size / SBSA_FLASH_SECTOR_SIZE);
241*e9fdf453SHongbo Zhang     qdev_init_nofail(dev);
242*e9fdf453SHongbo Zhang 
243*e9fdf453SHongbo Zhang     memory_region_add_subregion(sysmem, base,
244*e9fdf453SHongbo Zhang                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
245*e9fdf453SHongbo Zhang                                                        0));
246*e9fdf453SHongbo Zhang }
247*e9fdf453SHongbo Zhang 
248*e9fdf453SHongbo Zhang static void sbsa_flash_map(SBSAMachineState *sms,
249*e9fdf453SHongbo Zhang                            MemoryRegion *sysmem,
250*e9fdf453SHongbo Zhang                            MemoryRegion *secure_sysmem)
251*e9fdf453SHongbo Zhang {
252*e9fdf453SHongbo Zhang     /*
253*e9fdf453SHongbo Zhang      * Map two flash devices to fill the SBSA_FLASH space in the memmap.
254*e9fdf453SHongbo Zhang      * sysmem is the system memory space. secure_sysmem is the secure view
255*e9fdf453SHongbo Zhang      * of the system, and the first flash device should be made visible only
256*e9fdf453SHongbo Zhang      * there. The second flash device is visible to both secure and nonsecure.
257*e9fdf453SHongbo Zhang      * If sysmem == secure_sysmem this means there is no separate Secure
258*e9fdf453SHongbo Zhang      * address space and both flash devices are generally visible.
259*e9fdf453SHongbo Zhang      */
260*e9fdf453SHongbo Zhang     hwaddr flashsize = sbsa_ref_memmap[SBSA_FLASH].size / 2;
261*e9fdf453SHongbo Zhang     hwaddr flashbase = sbsa_ref_memmap[SBSA_FLASH].base;
262*e9fdf453SHongbo Zhang 
263*e9fdf453SHongbo Zhang     sbsa_flash_map1(sms->flash[0], flashbase, flashsize,
264*e9fdf453SHongbo Zhang                     secure_sysmem);
265*e9fdf453SHongbo Zhang     sbsa_flash_map1(sms->flash[1], flashbase + flashsize, flashsize,
266*e9fdf453SHongbo Zhang                     sysmem);
267*e9fdf453SHongbo Zhang }
268*e9fdf453SHongbo Zhang 
269*e9fdf453SHongbo Zhang static bool sbsa_firmware_init(SBSAMachineState *sms,
270*e9fdf453SHongbo Zhang                                MemoryRegion *sysmem,
271*e9fdf453SHongbo Zhang                                MemoryRegion *secure_sysmem)
272*e9fdf453SHongbo Zhang {
273*e9fdf453SHongbo Zhang     int i;
274*e9fdf453SHongbo Zhang     BlockBackend *pflash_blk0;
275*e9fdf453SHongbo Zhang 
276*e9fdf453SHongbo Zhang     /* Map legacy -drive if=pflash to machine properties */
277*e9fdf453SHongbo Zhang     for (i = 0; i < ARRAY_SIZE(sms->flash); i++) {
278*e9fdf453SHongbo Zhang         pflash_cfi01_legacy_drive(sms->flash[i],
279*e9fdf453SHongbo Zhang                                   drive_get(IF_PFLASH, 0, i));
280*e9fdf453SHongbo Zhang     }
281*e9fdf453SHongbo Zhang 
282*e9fdf453SHongbo Zhang     sbsa_flash_map(sms, sysmem, secure_sysmem);
283*e9fdf453SHongbo Zhang 
284*e9fdf453SHongbo Zhang     pflash_blk0 = pflash_cfi01_get_blk(sms->flash[0]);
285*e9fdf453SHongbo Zhang 
286*e9fdf453SHongbo Zhang     if (bios_name) {
287*e9fdf453SHongbo Zhang         char *fname;
288*e9fdf453SHongbo Zhang         MemoryRegion *mr;
289*e9fdf453SHongbo Zhang         int image_size;
290*e9fdf453SHongbo Zhang 
291*e9fdf453SHongbo Zhang         if (pflash_blk0) {
292*e9fdf453SHongbo Zhang             error_report("The contents of the first flash device may be "
293*e9fdf453SHongbo Zhang                          "specified with -bios or with -drive if=pflash... "
294*e9fdf453SHongbo Zhang                          "but you cannot use both options at once");
295*e9fdf453SHongbo Zhang             exit(1);
296*e9fdf453SHongbo Zhang         }
297*e9fdf453SHongbo Zhang 
298*e9fdf453SHongbo Zhang         /* Fall back to -bios */
299*e9fdf453SHongbo Zhang 
300*e9fdf453SHongbo Zhang         fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
301*e9fdf453SHongbo Zhang         if (!fname) {
302*e9fdf453SHongbo Zhang             error_report("Could not find ROM image '%s'", bios_name);
303*e9fdf453SHongbo Zhang             exit(1);
304*e9fdf453SHongbo Zhang         }
305*e9fdf453SHongbo Zhang         mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(sms->flash[0]), 0);
306*e9fdf453SHongbo Zhang         image_size = load_image_mr(fname, mr);
307*e9fdf453SHongbo Zhang         g_free(fname);
308*e9fdf453SHongbo Zhang         if (image_size < 0) {
309*e9fdf453SHongbo Zhang             error_report("Could not load ROM image '%s'", bios_name);
310*e9fdf453SHongbo Zhang             exit(1);
311*e9fdf453SHongbo Zhang         }
312*e9fdf453SHongbo Zhang     }
313*e9fdf453SHongbo Zhang 
314*e9fdf453SHongbo Zhang     return pflash_blk0 || bios_name;
315*e9fdf453SHongbo Zhang }
316*e9fdf453SHongbo Zhang 
317*e9fdf453SHongbo Zhang static void create_secure_ram(SBSAMachineState *sms,
318*e9fdf453SHongbo Zhang                               MemoryRegion *secure_sysmem)
319*e9fdf453SHongbo Zhang {
320*e9fdf453SHongbo Zhang     MemoryRegion *secram = g_new(MemoryRegion, 1);
321*e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_SECURE_MEM].base;
322*e9fdf453SHongbo Zhang     hwaddr size = sbsa_ref_memmap[SBSA_SECURE_MEM].size;
323*e9fdf453SHongbo Zhang 
324*e9fdf453SHongbo Zhang     memory_region_init_ram(secram, NULL, "sbsa-ref.secure-ram", size,
325*e9fdf453SHongbo Zhang                            &error_fatal);
326*e9fdf453SHongbo Zhang     memory_region_add_subregion(secure_sysmem, base, secram);
327*e9fdf453SHongbo Zhang }
328*e9fdf453SHongbo Zhang 
329*e9fdf453SHongbo Zhang static void create_gic(SBSAMachineState *sms, qemu_irq *pic)
330*e9fdf453SHongbo Zhang {
331*e9fdf453SHongbo Zhang     DeviceState *gicdev;
332*e9fdf453SHongbo Zhang     SysBusDevice *gicbusdev;
333*e9fdf453SHongbo Zhang     const char *gictype;
334*e9fdf453SHongbo Zhang     uint32_t redist0_capacity, redist0_count;
335*e9fdf453SHongbo Zhang     int i;
336*e9fdf453SHongbo Zhang 
337*e9fdf453SHongbo Zhang     gictype = gicv3_class_name();
338*e9fdf453SHongbo Zhang 
339*e9fdf453SHongbo Zhang     gicdev = qdev_create(NULL, gictype);
340*e9fdf453SHongbo Zhang     qdev_prop_set_uint32(gicdev, "revision", 3);
341*e9fdf453SHongbo Zhang     qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
342*e9fdf453SHongbo Zhang     /*
343*e9fdf453SHongbo Zhang      * Note that the num-irq property counts both internal and external
344*e9fdf453SHongbo Zhang      * interrupts; there are always 32 of the former (mandated by GIC spec).
345*e9fdf453SHongbo Zhang      */
346*e9fdf453SHongbo Zhang     qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
347*e9fdf453SHongbo Zhang     qdev_prop_set_bit(gicdev, "has-security-extensions", true);
348*e9fdf453SHongbo Zhang 
349*e9fdf453SHongbo Zhang     redist0_capacity =
350*e9fdf453SHongbo Zhang                 sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
351*e9fdf453SHongbo Zhang     redist0_count = MIN(smp_cpus, redist0_capacity);
352*e9fdf453SHongbo Zhang 
353*e9fdf453SHongbo Zhang     qdev_prop_set_uint32(gicdev, "len-redist-region-count", 1);
354*e9fdf453SHongbo Zhang     qdev_prop_set_uint32(gicdev, "redist-region-count[0]", redist0_count);
355*e9fdf453SHongbo Zhang 
356*e9fdf453SHongbo Zhang     qdev_init_nofail(gicdev);
357*e9fdf453SHongbo Zhang     gicbusdev = SYS_BUS_DEVICE(gicdev);
358*e9fdf453SHongbo Zhang     sysbus_mmio_map(gicbusdev, 0, sbsa_ref_memmap[SBSA_GIC_DIST].base);
359*e9fdf453SHongbo Zhang     sysbus_mmio_map(gicbusdev, 1, sbsa_ref_memmap[SBSA_GIC_REDIST].base);
360*e9fdf453SHongbo Zhang 
361*e9fdf453SHongbo Zhang     /*
362*e9fdf453SHongbo Zhang      * Wire the outputs from each CPU's generic timer and the GICv3
363*e9fdf453SHongbo Zhang      * maintenance interrupt signal to the appropriate GIC PPI inputs,
364*e9fdf453SHongbo Zhang      * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
365*e9fdf453SHongbo Zhang      */
366*e9fdf453SHongbo Zhang     for (i = 0; i < smp_cpus; i++) {
367*e9fdf453SHongbo Zhang         DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
368*e9fdf453SHongbo Zhang         int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
369*e9fdf453SHongbo Zhang         int irq;
370*e9fdf453SHongbo Zhang         /*
371*e9fdf453SHongbo Zhang          * Mapping from the output timer irq lines from the CPU to the
372*e9fdf453SHongbo Zhang          * GIC PPI inputs used for this board.
373*e9fdf453SHongbo Zhang          */
374*e9fdf453SHongbo Zhang         const int timer_irq[] = {
375*e9fdf453SHongbo Zhang             [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
376*e9fdf453SHongbo Zhang             [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
377*e9fdf453SHongbo Zhang             [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
378*e9fdf453SHongbo Zhang             [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
379*e9fdf453SHongbo Zhang         };
380*e9fdf453SHongbo Zhang 
381*e9fdf453SHongbo Zhang         for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
382*e9fdf453SHongbo Zhang             qdev_connect_gpio_out(cpudev, irq,
383*e9fdf453SHongbo Zhang                                   qdev_get_gpio_in(gicdev,
384*e9fdf453SHongbo Zhang                                                    ppibase + timer_irq[irq]));
385*e9fdf453SHongbo Zhang         }
386*e9fdf453SHongbo Zhang 
387*e9fdf453SHongbo Zhang         qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0,
388*e9fdf453SHongbo Zhang                                     qdev_get_gpio_in(gicdev, ppibase
389*e9fdf453SHongbo Zhang                                                      + ARCH_GIC_MAINT_IRQ));
390*e9fdf453SHongbo Zhang         qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
391*e9fdf453SHongbo Zhang                                     qdev_get_gpio_in(gicdev, ppibase
392*e9fdf453SHongbo Zhang                                                      + VIRTUAL_PMU_IRQ));
393*e9fdf453SHongbo Zhang 
394*e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
395*e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + smp_cpus,
396*e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
397*e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
398*e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
399*e9fdf453SHongbo Zhang         sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
400*e9fdf453SHongbo Zhang                            qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
401*e9fdf453SHongbo Zhang     }
402*e9fdf453SHongbo Zhang 
403*e9fdf453SHongbo Zhang     for (i = 0; i < NUM_IRQS; i++) {
404*e9fdf453SHongbo Zhang         pic[i] = qdev_get_gpio_in(gicdev, i);
405*e9fdf453SHongbo Zhang     }
406*e9fdf453SHongbo Zhang }
407*e9fdf453SHongbo Zhang 
408*e9fdf453SHongbo Zhang static void create_uart(const SBSAMachineState *sms, qemu_irq *pic, int uart,
409*e9fdf453SHongbo Zhang                         MemoryRegion *mem, Chardev *chr)
410*e9fdf453SHongbo Zhang {
411*e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[uart].base;
412*e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[uart];
413*e9fdf453SHongbo Zhang     DeviceState *dev = qdev_create(NULL, "pl011");
414*e9fdf453SHongbo Zhang     SysBusDevice *s = SYS_BUS_DEVICE(dev);
415*e9fdf453SHongbo Zhang 
416*e9fdf453SHongbo Zhang     qdev_prop_set_chr(dev, "chardev", chr);
417*e9fdf453SHongbo Zhang     qdev_init_nofail(dev);
418*e9fdf453SHongbo Zhang     memory_region_add_subregion(mem, base,
419*e9fdf453SHongbo Zhang                                 sysbus_mmio_get_region(s, 0));
420*e9fdf453SHongbo Zhang     sysbus_connect_irq(s, 0, pic[irq]);
421*e9fdf453SHongbo Zhang }
422*e9fdf453SHongbo Zhang 
423*e9fdf453SHongbo Zhang static void create_rtc(const SBSAMachineState *sms, qemu_irq *pic)
424*e9fdf453SHongbo Zhang {
425*e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_RTC].base;
426*e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_RTC];
427*e9fdf453SHongbo Zhang 
428*e9fdf453SHongbo Zhang     sysbus_create_simple("pl031", base, pic[irq]);
429*e9fdf453SHongbo Zhang }
430*e9fdf453SHongbo Zhang 
431*e9fdf453SHongbo Zhang static DeviceState *gpio_key_dev;
432*e9fdf453SHongbo Zhang static void sbsa_ref_powerdown_req(Notifier *n, void *opaque)
433*e9fdf453SHongbo Zhang {
434*e9fdf453SHongbo Zhang     /* use gpio Pin 3 for power button event */
435*e9fdf453SHongbo Zhang     qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
436*e9fdf453SHongbo Zhang }
437*e9fdf453SHongbo Zhang 
438*e9fdf453SHongbo Zhang static Notifier sbsa_ref_powerdown_notifier = {
439*e9fdf453SHongbo Zhang     .notify = sbsa_ref_powerdown_req
440*e9fdf453SHongbo Zhang };
441*e9fdf453SHongbo Zhang 
442*e9fdf453SHongbo Zhang static void create_gpio(const SBSAMachineState *sms, qemu_irq *pic)
443*e9fdf453SHongbo Zhang {
444*e9fdf453SHongbo Zhang     DeviceState *pl061_dev;
445*e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_GPIO].base;
446*e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_GPIO];
447*e9fdf453SHongbo Zhang 
448*e9fdf453SHongbo Zhang     pl061_dev = sysbus_create_simple("pl061", base, pic[irq]);
449*e9fdf453SHongbo Zhang 
450*e9fdf453SHongbo Zhang     gpio_key_dev = sysbus_create_simple("gpio-key", -1,
451*e9fdf453SHongbo Zhang                                         qdev_get_gpio_in(pl061_dev, 3));
452*e9fdf453SHongbo Zhang 
453*e9fdf453SHongbo Zhang     /* connect powerdown request */
454*e9fdf453SHongbo Zhang     qemu_register_powerdown_notifier(&sbsa_ref_powerdown_notifier);
455*e9fdf453SHongbo Zhang }
456*e9fdf453SHongbo Zhang 
457*e9fdf453SHongbo Zhang static void create_ahci(const SBSAMachineState *sms, qemu_irq *pic)
458*e9fdf453SHongbo Zhang {
459*e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_AHCI].base;
460*e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_AHCI];
461*e9fdf453SHongbo Zhang     DeviceState *dev;
462*e9fdf453SHongbo Zhang     DriveInfo *hd[NUM_SATA_PORTS];
463*e9fdf453SHongbo Zhang     SysbusAHCIState *sysahci;
464*e9fdf453SHongbo Zhang     AHCIState *ahci;
465*e9fdf453SHongbo Zhang     int i;
466*e9fdf453SHongbo Zhang 
467*e9fdf453SHongbo Zhang     dev = qdev_create(NULL, "sysbus-ahci");
468*e9fdf453SHongbo Zhang     qdev_prop_set_uint32(dev, "num-ports", NUM_SATA_PORTS);
469*e9fdf453SHongbo Zhang     qdev_init_nofail(dev);
470*e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
471*e9fdf453SHongbo Zhang     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, pic[irq]);
472*e9fdf453SHongbo Zhang 
473*e9fdf453SHongbo Zhang     sysahci = SYSBUS_AHCI(dev);
474*e9fdf453SHongbo Zhang     ahci = &sysahci->ahci;
475*e9fdf453SHongbo Zhang     ide_drive_get(hd, ARRAY_SIZE(hd));
476*e9fdf453SHongbo Zhang     for (i = 0; i < ahci->ports; i++) {
477*e9fdf453SHongbo Zhang         if (hd[i] == NULL) {
478*e9fdf453SHongbo Zhang             continue;
479*e9fdf453SHongbo Zhang         }
480*e9fdf453SHongbo Zhang         ide_create_drive(&ahci->dev[i].port, 0, hd[i]);
481*e9fdf453SHongbo Zhang     }
482*e9fdf453SHongbo Zhang }
483*e9fdf453SHongbo Zhang 
484*e9fdf453SHongbo Zhang static void create_ehci(const SBSAMachineState *sms, qemu_irq *pic)
485*e9fdf453SHongbo Zhang {
486*e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_EHCI].base;
487*e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_EHCI];
488*e9fdf453SHongbo Zhang 
489*e9fdf453SHongbo Zhang     sysbus_create_simple("platform-ehci-usb", base, pic[irq]);
490*e9fdf453SHongbo Zhang }
491*e9fdf453SHongbo Zhang 
492*e9fdf453SHongbo Zhang static void create_smmu(const SBSAMachineState *sms, qemu_irq *pic,
493*e9fdf453SHongbo Zhang                         PCIBus *bus)
494*e9fdf453SHongbo Zhang {
495*e9fdf453SHongbo Zhang     hwaddr base = sbsa_ref_memmap[SBSA_SMMU].base;
496*e9fdf453SHongbo Zhang     int irq =  sbsa_ref_irqmap[SBSA_SMMU];
497*e9fdf453SHongbo Zhang     DeviceState *dev;
498*e9fdf453SHongbo Zhang     int i;
499*e9fdf453SHongbo Zhang 
500*e9fdf453SHongbo Zhang     dev = qdev_create(NULL, "arm-smmuv3");
501*e9fdf453SHongbo Zhang 
502*e9fdf453SHongbo Zhang     object_property_set_link(OBJECT(dev), OBJECT(bus), "primary-bus",
503*e9fdf453SHongbo Zhang                              &error_abort);
504*e9fdf453SHongbo Zhang     qdev_init_nofail(dev);
505*e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
506*e9fdf453SHongbo Zhang     for (i = 0; i < NUM_SMMU_IRQS; i++) {
507*e9fdf453SHongbo Zhang         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
508*e9fdf453SHongbo Zhang     }
509*e9fdf453SHongbo Zhang }
510*e9fdf453SHongbo Zhang 
511*e9fdf453SHongbo Zhang static void create_pcie(SBSAMachineState *sms, qemu_irq *pic)
512*e9fdf453SHongbo Zhang {
513*e9fdf453SHongbo Zhang     hwaddr base_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].base;
514*e9fdf453SHongbo Zhang     hwaddr size_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].size;
515*e9fdf453SHongbo Zhang     hwaddr base_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].base;
516*e9fdf453SHongbo Zhang     hwaddr size_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].size;
517*e9fdf453SHongbo Zhang     hwaddr base_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].base;
518*e9fdf453SHongbo Zhang     hwaddr size_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].size;
519*e9fdf453SHongbo Zhang     hwaddr base_pio = sbsa_ref_memmap[SBSA_PCIE_PIO].base;
520*e9fdf453SHongbo Zhang     int irq = sbsa_ref_irqmap[SBSA_PCIE];
521*e9fdf453SHongbo Zhang     MemoryRegion *mmio_alias, *mmio_alias_high, *mmio_reg;
522*e9fdf453SHongbo Zhang     MemoryRegion *ecam_alias, *ecam_reg;
523*e9fdf453SHongbo Zhang     DeviceState *dev;
524*e9fdf453SHongbo Zhang     PCIHostState *pci;
525*e9fdf453SHongbo Zhang     int i;
526*e9fdf453SHongbo Zhang 
527*e9fdf453SHongbo Zhang     dev = qdev_create(NULL, TYPE_GPEX_HOST);
528*e9fdf453SHongbo Zhang     qdev_init_nofail(dev);
529*e9fdf453SHongbo Zhang 
530*e9fdf453SHongbo Zhang     /* Map ECAM space */
531*e9fdf453SHongbo Zhang     ecam_alias = g_new0(MemoryRegion, 1);
532*e9fdf453SHongbo Zhang     ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
533*e9fdf453SHongbo Zhang     memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
534*e9fdf453SHongbo Zhang                              ecam_reg, 0, size_ecam);
535*e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
536*e9fdf453SHongbo Zhang 
537*e9fdf453SHongbo Zhang     /* Map the MMIO space */
538*e9fdf453SHongbo Zhang     mmio_alias = g_new0(MemoryRegion, 1);
539*e9fdf453SHongbo Zhang     mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
540*e9fdf453SHongbo Zhang     memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
541*e9fdf453SHongbo Zhang                              mmio_reg, base_mmio, size_mmio);
542*e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
543*e9fdf453SHongbo Zhang 
544*e9fdf453SHongbo Zhang     /* Map the MMIO_HIGH space */
545*e9fdf453SHongbo Zhang     mmio_alias_high = g_new0(MemoryRegion, 1);
546*e9fdf453SHongbo Zhang     memory_region_init_alias(mmio_alias_high, OBJECT(dev), "pcie-mmio-high",
547*e9fdf453SHongbo Zhang                              mmio_reg, base_mmio_high, size_mmio_high);
548*e9fdf453SHongbo Zhang     memory_region_add_subregion(get_system_memory(), base_mmio_high,
549*e9fdf453SHongbo Zhang                                 mmio_alias_high);
550*e9fdf453SHongbo Zhang 
551*e9fdf453SHongbo Zhang     /* Map IO port space */
552*e9fdf453SHongbo Zhang     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
553*e9fdf453SHongbo Zhang 
554*e9fdf453SHongbo Zhang     for (i = 0; i < GPEX_NUM_IRQS; i++) {
555*e9fdf453SHongbo Zhang         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
556*e9fdf453SHongbo Zhang         gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
557*e9fdf453SHongbo Zhang     }
558*e9fdf453SHongbo Zhang 
559*e9fdf453SHongbo Zhang     pci = PCI_HOST_BRIDGE(dev);
560*e9fdf453SHongbo Zhang     if (pci->bus) {
561*e9fdf453SHongbo Zhang         for (i = 0; i < nb_nics; i++) {
562*e9fdf453SHongbo Zhang             NICInfo *nd = &nd_table[i];
563*e9fdf453SHongbo Zhang 
564*e9fdf453SHongbo Zhang             if (!nd->model) {
565*e9fdf453SHongbo Zhang                 nd->model = g_strdup("e1000e");
566*e9fdf453SHongbo Zhang             }
567*e9fdf453SHongbo Zhang 
568*e9fdf453SHongbo Zhang             pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
569*e9fdf453SHongbo Zhang         }
570*e9fdf453SHongbo Zhang     }
571*e9fdf453SHongbo Zhang 
572*e9fdf453SHongbo Zhang     pci_create_simple(pci->bus, -1, "VGA");
573*e9fdf453SHongbo Zhang 
574*e9fdf453SHongbo Zhang     create_smmu(sms, pic, pci->bus);
575*e9fdf453SHongbo Zhang }
576*e9fdf453SHongbo Zhang 
577*e9fdf453SHongbo Zhang static void *sbsa_ref_dtb(const struct arm_boot_info *binfo, int *fdt_size)
578*e9fdf453SHongbo Zhang {
579*e9fdf453SHongbo Zhang     const SBSAMachineState *board = container_of(binfo, SBSAMachineState,
580*e9fdf453SHongbo Zhang                                                  bootinfo);
581*e9fdf453SHongbo Zhang 
582*e9fdf453SHongbo Zhang     *fdt_size = board->fdt_size;
583*e9fdf453SHongbo Zhang     return board->fdt;
584*e9fdf453SHongbo Zhang }
585*e9fdf453SHongbo Zhang 
58664580903SHongbo Zhang static void sbsa_ref_init(MachineState *machine)
58764580903SHongbo Zhang {
58864580903SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(machine);
58964580903SHongbo Zhang     MachineClass *mc = MACHINE_GET_CLASS(machine);
59064580903SHongbo Zhang     MemoryRegion *sysmem = get_system_memory();
59164580903SHongbo Zhang     MemoryRegion *secure_sysmem = NULL;
59264580903SHongbo Zhang     MemoryRegion *ram = g_new(MemoryRegion, 1);
593*e9fdf453SHongbo Zhang     bool firmware_loaded;
59464580903SHongbo Zhang     const CPUArchIdList *possible_cpus;
59564580903SHongbo Zhang     int n, sbsa_max_cpus;
596*e9fdf453SHongbo Zhang     qemu_irq pic[NUM_IRQS];
59764580903SHongbo Zhang 
59864580903SHongbo Zhang     if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a57"))) {
59964580903SHongbo Zhang         error_report("sbsa-ref: CPU type other than the built-in "
60064580903SHongbo Zhang                      "cortex-a57 not supported");
60164580903SHongbo Zhang         exit(1);
60264580903SHongbo Zhang     }
60364580903SHongbo Zhang 
60464580903SHongbo Zhang     if (kvm_enabled()) {
60564580903SHongbo Zhang         error_report("sbsa-ref: KVM is not supported for this machine");
60664580903SHongbo Zhang         exit(1);
60764580903SHongbo Zhang     }
60864580903SHongbo Zhang 
60964580903SHongbo Zhang     /*
610*e9fdf453SHongbo Zhang      * The Secure view of the world is the same as the NonSecure,
611*e9fdf453SHongbo Zhang      * but with a few extra devices. Create it as a container region
612*e9fdf453SHongbo Zhang      * containing the system memory at low priority; any secure-only
613*e9fdf453SHongbo Zhang      * devices go in at higher priority and take precedence.
614*e9fdf453SHongbo Zhang      */
615*e9fdf453SHongbo Zhang     secure_sysmem = g_new(MemoryRegion, 1);
616*e9fdf453SHongbo Zhang     memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
617*e9fdf453SHongbo Zhang                        UINT64_MAX);
618*e9fdf453SHongbo Zhang     memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
619*e9fdf453SHongbo Zhang 
620*e9fdf453SHongbo Zhang     firmware_loaded = sbsa_firmware_init(sms, sysmem,
621*e9fdf453SHongbo Zhang                                          secure_sysmem ?: sysmem);
622*e9fdf453SHongbo Zhang 
623*e9fdf453SHongbo Zhang     if (machine->kernel_filename && firmware_loaded) {
624*e9fdf453SHongbo Zhang         error_report("sbsa-ref: No fw_cfg device on this machine, "
625*e9fdf453SHongbo Zhang                      "so -kernel option is not supported when firmware loaded, "
626*e9fdf453SHongbo Zhang                      "please load OS from hard disk instead");
627*e9fdf453SHongbo Zhang         exit(1);
628*e9fdf453SHongbo Zhang     }
629*e9fdf453SHongbo Zhang 
630*e9fdf453SHongbo Zhang     /*
63164580903SHongbo Zhang      * This machine has EL3 enabled, external firmware should supply PSCI
63264580903SHongbo Zhang      * implementation, so the QEMU's internal PSCI is disabled.
63364580903SHongbo Zhang      */
63464580903SHongbo Zhang     sms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
63564580903SHongbo Zhang 
63664580903SHongbo Zhang     sbsa_max_cpus = sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
63764580903SHongbo Zhang 
63864580903SHongbo Zhang     if (max_cpus > sbsa_max_cpus) {
63964580903SHongbo Zhang         error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
64064580903SHongbo Zhang                      "supported by machine 'sbsa-ref' (%d)",
64164580903SHongbo Zhang                      max_cpus, sbsa_max_cpus);
64264580903SHongbo Zhang         exit(1);
64364580903SHongbo Zhang     }
64464580903SHongbo Zhang 
64564580903SHongbo Zhang     sms->smp_cpus = smp_cpus;
64664580903SHongbo Zhang 
64764580903SHongbo Zhang     if (machine->ram_size > sbsa_ref_memmap[SBSA_MEM].size) {
64864580903SHongbo Zhang         error_report("sbsa-ref: cannot model more than %dGB RAM", RAMLIMIT_GB);
64964580903SHongbo Zhang         exit(1);
65064580903SHongbo Zhang     }
65164580903SHongbo Zhang 
65264580903SHongbo Zhang     possible_cpus = mc->possible_cpu_arch_ids(machine);
65364580903SHongbo Zhang     for (n = 0; n < possible_cpus->len; n++) {
65464580903SHongbo Zhang         Object *cpuobj;
65564580903SHongbo Zhang         CPUState *cs;
65664580903SHongbo Zhang 
65764580903SHongbo Zhang         if (n >= smp_cpus) {
65864580903SHongbo Zhang             break;
65964580903SHongbo Zhang         }
66064580903SHongbo Zhang 
66164580903SHongbo Zhang         cpuobj = object_new(possible_cpus->cpus[n].type);
66264580903SHongbo Zhang         object_property_set_int(cpuobj, possible_cpus->cpus[n].arch_id,
66364580903SHongbo Zhang                                 "mp-affinity", NULL);
66464580903SHongbo Zhang 
66564580903SHongbo Zhang         cs = CPU(cpuobj);
66664580903SHongbo Zhang         cs->cpu_index = n;
66764580903SHongbo Zhang 
66864580903SHongbo Zhang         numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
66964580903SHongbo Zhang                           &error_fatal);
67064580903SHongbo Zhang 
67164580903SHongbo Zhang         if (object_property_find(cpuobj, "reset-cbar", NULL)) {
67264580903SHongbo Zhang             object_property_set_int(cpuobj,
67364580903SHongbo Zhang                                     sbsa_ref_memmap[SBSA_CPUPERIPHS].base,
67464580903SHongbo Zhang                                     "reset-cbar", &error_abort);
67564580903SHongbo Zhang         }
67664580903SHongbo Zhang 
67764580903SHongbo Zhang         object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
67864580903SHongbo Zhang                                  &error_abort);
67964580903SHongbo Zhang 
68064580903SHongbo Zhang         object_property_set_link(cpuobj, OBJECT(secure_sysmem),
68164580903SHongbo Zhang                                  "secure-memory", &error_abort);
68264580903SHongbo Zhang 
68364580903SHongbo Zhang         object_property_set_bool(cpuobj, true, "realized", &error_fatal);
68464580903SHongbo Zhang         object_unref(cpuobj);
68564580903SHongbo Zhang     }
68664580903SHongbo Zhang 
68764580903SHongbo Zhang     memory_region_allocate_system_memory(ram, NULL, "sbsa-ref.ram",
68864580903SHongbo Zhang                                          machine->ram_size);
68964580903SHongbo Zhang     memory_region_add_subregion(sysmem, sbsa_ref_memmap[SBSA_MEM].base, ram);
69064580903SHongbo Zhang 
691*e9fdf453SHongbo Zhang     create_fdt(sms);
692*e9fdf453SHongbo Zhang 
693*e9fdf453SHongbo Zhang     create_secure_ram(sms, secure_sysmem);
694*e9fdf453SHongbo Zhang 
695*e9fdf453SHongbo Zhang     create_gic(sms, pic);
696*e9fdf453SHongbo Zhang 
697*e9fdf453SHongbo Zhang     create_uart(sms, pic, SBSA_UART, sysmem, serial_hd(0));
698*e9fdf453SHongbo Zhang     create_uart(sms, pic, SBSA_SECURE_UART, secure_sysmem, serial_hd(1));
699*e9fdf453SHongbo Zhang     /* Second secure UART for RAS and MM from EL0 */
700*e9fdf453SHongbo Zhang     create_uart(sms, pic, SBSA_SECURE_UART_MM, secure_sysmem, serial_hd(2));
701*e9fdf453SHongbo Zhang 
702*e9fdf453SHongbo Zhang     create_rtc(sms, pic);
703*e9fdf453SHongbo Zhang 
704*e9fdf453SHongbo Zhang     create_gpio(sms, pic);
705*e9fdf453SHongbo Zhang 
706*e9fdf453SHongbo Zhang     create_ahci(sms, pic);
707*e9fdf453SHongbo Zhang 
708*e9fdf453SHongbo Zhang     create_ehci(sms, pic);
709*e9fdf453SHongbo Zhang 
710*e9fdf453SHongbo Zhang     create_pcie(sms, pic);
711*e9fdf453SHongbo Zhang 
71264580903SHongbo Zhang     sms->bootinfo.ram_size = machine->ram_size;
71364580903SHongbo Zhang     sms->bootinfo.kernel_filename = machine->kernel_filename;
71464580903SHongbo Zhang     sms->bootinfo.nb_cpus = smp_cpus;
71564580903SHongbo Zhang     sms->bootinfo.board_id = -1;
71664580903SHongbo Zhang     sms->bootinfo.loader_start = sbsa_ref_memmap[SBSA_MEM].base;
717*e9fdf453SHongbo Zhang     sms->bootinfo.get_dtb = sbsa_ref_dtb;
718*e9fdf453SHongbo Zhang     sms->bootinfo.firmware_loaded = firmware_loaded;
71964580903SHongbo Zhang     arm_load_kernel(ARM_CPU(first_cpu), &sms->bootinfo);
72064580903SHongbo Zhang }
72164580903SHongbo Zhang 
72264580903SHongbo Zhang static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx)
72364580903SHongbo Zhang {
72464580903SHongbo Zhang     uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
72564580903SHongbo Zhang     return arm_cpu_mp_affinity(idx, clustersz);
72664580903SHongbo Zhang }
72764580903SHongbo Zhang 
72864580903SHongbo Zhang static const CPUArchIdList *sbsa_ref_possible_cpu_arch_ids(MachineState *ms)
72964580903SHongbo Zhang {
73064580903SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(ms);
73164580903SHongbo Zhang     int n;
73264580903SHongbo Zhang 
73364580903SHongbo Zhang     if (ms->possible_cpus) {
73464580903SHongbo Zhang         assert(ms->possible_cpus->len == max_cpus);
73564580903SHongbo Zhang         return ms->possible_cpus;
73664580903SHongbo Zhang     }
73764580903SHongbo Zhang 
73864580903SHongbo Zhang     ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
73964580903SHongbo Zhang                                   sizeof(CPUArchId) * max_cpus);
74064580903SHongbo Zhang     ms->possible_cpus->len = max_cpus;
74164580903SHongbo Zhang     for (n = 0; n < ms->possible_cpus->len; n++) {
74264580903SHongbo Zhang         ms->possible_cpus->cpus[n].type = ms->cpu_type;
74364580903SHongbo Zhang         ms->possible_cpus->cpus[n].arch_id =
74464580903SHongbo Zhang             sbsa_ref_cpu_mp_affinity(sms, n);
74564580903SHongbo Zhang         ms->possible_cpus->cpus[n].props.has_thread_id = true;
74664580903SHongbo Zhang         ms->possible_cpus->cpus[n].props.thread_id = n;
74764580903SHongbo Zhang     }
74864580903SHongbo Zhang     return ms->possible_cpus;
74964580903SHongbo Zhang }
75064580903SHongbo Zhang 
75164580903SHongbo Zhang static CpuInstanceProperties
75264580903SHongbo Zhang sbsa_ref_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
75364580903SHongbo Zhang {
75464580903SHongbo Zhang     MachineClass *mc = MACHINE_GET_CLASS(ms);
75564580903SHongbo Zhang     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
75664580903SHongbo Zhang 
75764580903SHongbo Zhang     assert(cpu_index < possible_cpus->len);
75864580903SHongbo Zhang     return possible_cpus->cpus[cpu_index].props;
75964580903SHongbo Zhang }
76064580903SHongbo Zhang 
76164580903SHongbo Zhang static int64_t
76264580903SHongbo Zhang sbsa_ref_get_default_cpu_node_id(const MachineState *ms, int idx)
76364580903SHongbo Zhang {
76464580903SHongbo Zhang     return idx % nb_numa_nodes;
76564580903SHongbo Zhang }
76664580903SHongbo Zhang 
767*e9fdf453SHongbo Zhang static void sbsa_ref_instance_init(Object *obj)
768*e9fdf453SHongbo Zhang {
769*e9fdf453SHongbo Zhang     SBSAMachineState *sms = SBSA_MACHINE(obj);
770*e9fdf453SHongbo Zhang 
771*e9fdf453SHongbo Zhang     sbsa_flash_create(sms);
772*e9fdf453SHongbo Zhang }
773*e9fdf453SHongbo Zhang 
77464580903SHongbo Zhang static void sbsa_ref_class_init(ObjectClass *oc, void *data)
77564580903SHongbo Zhang {
77664580903SHongbo Zhang     MachineClass *mc = MACHINE_CLASS(oc);
77764580903SHongbo Zhang 
77864580903SHongbo Zhang     mc->init = sbsa_ref_init;
77964580903SHongbo Zhang     mc->desc = "QEMU 'SBSA Reference' ARM Virtual Machine";
78064580903SHongbo Zhang     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a57");
78164580903SHongbo Zhang     mc->max_cpus = 512;
78264580903SHongbo Zhang     mc->pci_allow_0_address = true;
78364580903SHongbo Zhang     mc->minimum_page_bits = 12;
78464580903SHongbo Zhang     mc->block_default_type = IF_IDE;
78564580903SHongbo Zhang     mc->no_cdrom = 1;
78664580903SHongbo Zhang     mc->default_ram_size = 1 * GiB;
78764580903SHongbo Zhang     mc->default_cpus = 4;
78864580903SHongbo Zhang     mc->possible_cpu_arch_ids = sbsa_ref_possible_cpu_arch_ids;
78964580903SHongbo Zhang     mc->cpu_index_to_instance_props = sbsa_ref_cpu_index_to_props;
79064580903SHongbo Zhang     mc->get_default_cpu_node_id = sbsa_ref_get_default_cpu_node_id;
79164580903SHongbo Zhang }
79264580903SHongbo Zhang 
79364580903SHongbo Zhang static const TypeInfo sbsa_ref_info = {
79464580903SHongbo Zhang     .name          = TYPE_SBSA_MACHINE,
79564580903SHongbo Zhang     .parent        = TYPE_MACHINE,
796*e9fdf453SHongbo Zhang     .instance_init = sbsa_ref_instance_init,
79764580903SHongbo Zhang     .class_init    = sbsa_ref_class_init,
79864580903SHongbo Zhang     .instance_size = sizeof(SBSAMachineState),
79964580903SHongbo Zhang };
80064580903SHongbo Zhang 
80164580903SHongbo Zhang static void sbsa_ref_machine_init(void)
80264580903SHongbo Zhang {
80364580903SHongbo Zhang     type_register_static(&sbsa_ref_info);
80464580903SHongbo Zhang }
80564580903SHongbo Zhang 
80664580903SHongbo Zhang type_init(sbsa_ref_machine_init);
807