xref: /openbmc/qemu/hw/arm/virt.c (revision 228aa992)
1 /*
2  * ARM mach-virt emulation
3  *
4  * Copyright (c) 2013 Linaro Limited
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2 or later, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Emulate a virtual board which works by passing Linux all the information
19  * it needs about what devices are present via the device tree.
20  * There are some restrictions about what we can do here:
21  *  + we can only present devices whose Linux drivers will work based
22  *    purely on the device tree with no platform data at all
23  *  + we want to present a very stripped-down minimalist platform,
24  *    both because this reduces the security attack surface from the guest
25  *    and also because it reduces our exposure to being broken when
26  *    the kernel updates its device tree bindings and requires further
27  *    information in a device binding that we aren't providing.
28  * This is essentially the same approach kvmtool uses.
29  */
30 
31 #include "hw/sysbus.h"
32 #include "hw/arm/arm.h"
33 #include "hw/arm/primecell.h"
34 #include "hw/devices.h"
35 #include "net/net.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/device_tree.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/kvm.h"
40 #include "hw/boards.h"
41 #include "hw/loader.h"
42 #include "exec/address-spaces.h"
43 #include "qemu/bitops.h"
44 #include "qemu/error-report.h"
45 
46 #define NUM_VIRTIO_TRANSPORTS 32
47 
48 /* Number of external interrupt lines to configure the GIC with */
49 #define NUM_IRQS 128
50 
51 #define GIC_FDT_IRQ_TYPE_SPI 0
52 #define GIC_FDT_IRQ_TYPE_PPI 1
53 
54 #define GIC_FDT_IRQ_FLAGS_EDGE_LO_HI 1
55 #define GIC_FDT_IRQ_FLAGS_EDGE_HI_LO 2
56 #define GIC_FDT_IRQ_FLAGS_LEVEL_HI 4
57 #define GIC_FDT_IRQ_FLAGS_LEVEL_LO 8
58 
59 #define GIC_FDT_IRQ_PPI_CPU_START 8
60 #define GIC_FDT_IRQ_PPI_CPU_WIDTH 8
61 
62 enum {
63     VIRT_FLASH,
64     VIRT_MEM,
65     VIRT_CPUPERIPHS,
66     VIRT_GIC_DIST,
67     VIRT_GIC_CPU,
68     VIRT_UART,
69     VIRT_MMIO,
70     VIRT_RTC,
71 };
72 
73 typedef struct MemMapEntry {
74     hwaddr base;
75     hwaddr size;
76 } MemMapEntry;
77 
78 typedef struct VirtBoardInfo {
79     struct arm_boot_info bootinfo;
80     const char *cpu_model;
81     const MemMapEntry *memmap;
82     const int *irqmap;
83     int smp_cpus;
84     void *fdt;
85     int fdt_size;
86     uint32_t clock_phandle;
87 } VirtBoardInfo;
88 
89 /* Addresses and sizes of our components.
90  * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
91  * 128MB..256MB is used for miscellaneous device I/O.
92  * 256MB..1GB is reserved for possible future PCI support (ie where the
93  * PCI memory window will go if we add a PCI host controller).
94  * 1GB and up is RAM (which may happily spill over into the
95  * high memory region beyond 4GB).
96  * This represents a compromise between how much RAM can be given to
97  * a 32 bit VM and leaving space for expansion and in particular for PCI.
98  * Note that devices should generally be placed at multiples of 0x10000,
99  * to accommodate guests using 64K pages.
100  */
101 static const MemMapEntry a15memmap[] = {
102     /* Space up to 0x8000000 is reserved for a boot ROM */
103     [VIRT_FLASH] =      {          0, 0x08000000 },
104     [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
105     /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
106     [VIRT_GIC_DIST] =   { 0x08000000, 0x00010000 },
107     [VIRT_GIC_CPU] =    { 0x08010000, 0x00010000 },
108     [VIRT_UART] =       { 0x09000000, 0x00001000 },
109     [VIRT_RTC] =        { 0x09010000, 0x00001000 },
110     [VIRT_MMIO] =       { 0x0a000000, 0x00000200 },
111     /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
112     /* 0x10000000 .. 0x40000000 reserved for PCI */
113     [VIRT_MEM] =        { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
114 };
115 
116 static const int a15irqmap[] = {
117     [VIRT_UART] = 1,
118     [VIRT_RTC] = 2,
119     [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
120 };
121 
122 static VirtBoardInfo machines[] = {
123     {
124         .cpu_model = "cortex-a15",
125         .memmap = a15memmap,
126         .irqmap = a15irqmap,
127     },
128     {
129         .cpu_model = "cortex-a57",
130         .memmap = a15memmap,
131         .irqmap = a15irqmap,
132     },
133     {
134         .cpu_model = "host",
135         .memmap = a15memmap,
136         .irqmap = a15irqmap,
137     },
138 };
139 
140 static VirtBoardInfo *find_machine_info(const char *cpu)
141 {
142     int i;
143 
144     for (i = 0; i < ARRAY_SIZE(machines); i++) {
145         if (strcmp(cpu, machines[i].cpu_model) == 0) {
146             return &machines[i];
147         }
148     }
149     return NULL;
150 }
151 
152 static void create_fdt(VirtBoardInfo *vbi)
153 {
154     void *fdt = create_device_tree(&vbi->fdt_size);
155 
156     if (!fdt) {
157         error_report("create_device_tree() failed");
158         exit(1);
159     }
160 
161     vbi->fdt = fdt;
162 
163     /* Header */
164     qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
165     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
166     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
167 
168     /*
169      * /chosen and /memory nodes must exist for load_dtb
170      * to fill in necessary properties later
171      */
172     qemu_fdt_add_subnode(fdt, "/chosen");
173     qemu_fdt_add_subnode(fdt, "/memory");
174     qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
175 
176     /* Clock node, for the benefit of the UART. The kernel device tree
177      * binding documentation claims the PL011 node clock properties are
178      * optional but in practice if you omit them the kernel refuses to
179      * probe for the device.
180      */
181     vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt);
182     qemu_fdt_add_subnode(fdt, "/apb-pclk");
183     qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
184     qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
185     qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
186     qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
187                                 "clk24mhz");
188     qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle);
189 
190 }
191 
192 static void fdt_add_psci_node(const VirtBoardInfo *vbi)
193 {
194     uint32_t cpu_suspend_fn;
195     uint32_t cpu_off_fn;
196     uint32_t cpu_on_fn;
197     uint32_t migrate_fn;
198     void *fdt = vbi->fdt;
199     ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
200 
201     qemu_fdt_add_subnode(fdt, "/psci");
202     if (armcpu->psci_version == 2) {
203         const char comp[] = "arm,psci-0.2\0arm,psci";
204         qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
205 
206         cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
207         if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
208             cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
209             cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
210             migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
211         } else {
212             cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
213             cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
214             migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
215         }
216     } else {
217         qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
218 
219         cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
220         cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
221         cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
222         migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
223     }
224 
225     /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
226      * to the instruction that should be used to invoke PSCI functions.
227      * However, the device tree binding uses 'method' instead, so that is
228      * what we should use here.
229      */
230     qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
231 
232     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
233     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
234     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
235     qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
236 }
237 
238 static void fdt_add_timer_nodes(const VirtBoardInfo *vbi)
239 {
240     /* Note that on A15 h/w these interrupts are level-triggered,
241      * but for the GIC implementation provided by both QEMU and KVM
242      * they are edge-triggered.
243      */
244     ARMCPU *armcpu;
245     uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
246 
247     irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
248                          GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << vbi->smp_cpus) - 1);
249 
250     qemu_fdt_add_subnode(vbi->fdt, "/timer");
251 
252     armcpu = ARM_CPU(qemu_get_cpu(0));
253     if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
254         const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
255         qemu_fdt_setprop(vbi->fdt, "/timer", "compatible",
256                          compat, sizeof(compat));
257     } else {
258         qemu_fdt_setprop_string(vbi->fdt, "/timer", "compatible",
259                                 "arm,armv7-timer");
260     }
261     qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts",
262                                GIC_FDT_IRQ_TYPE_PPI, 13, irqflags,
263                                GIC_FDT_IRQ_TYPE_PPI, 14, irqflags,
264                                GIC_FDT_IRQ_TYPE_PPI, 11, irqflags,
265                                GIC_FDT_IRQ_TYPE_PPI, 10, irqflags);
266 }
267 
268 static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
269 {
270     int cpu;
271 
272     qemu_fdt_add_subnode(vbi->fdt, "/cpus");
273     qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", 0x1);
274     qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0);
275 
276     for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) {
277         char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
278         ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
279 
280         qemu_fdt_add_subnode(vbi->fdt, nodename);
281         qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu");
282         qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible",
283                                     armcpu->dtb_compatible);
284 
285         if (vbi->smp_cpus > 1) {
286             qemu_fdt_setprop_string(vbi->fdt, nodename,
287                                         "enable-method", "psci");
288         }
289 
290         qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu);
291         g_free(nodename);
292     }
293 }
294 
295 static void fdt_add_gic_node(const VirtBoardInfo *vbi)
296 {
297     uint32_t gic_phandle;
298 
299     gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
300     qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", gic_phandle);
301 
302     qemu_fdt_add_subnode(vbi->fdt, "/intc");
303     /* 'cortex-a15-gic' means 'GIC v2' */
304     qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
305                             "arm,cortex-a15-gic");
306     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);
307     qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);
308     qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
309                                      2, vbi->memmap[VIRT_GIC_DIST].base,
310                                      2, vbi->memmap[VIRT_GIC_DIST].size,
311                                      2, vbi->memmap[VIRT_GIC_CPU].base,
312                                      2, vbi->memmap[VIRT_GIC_CPU].size);
313     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", gic_phandle);
314 }
315 
316 static void create_gic(const VirtBoardInfo *vbi, qemu_irq *pic)
317 {
318     /* We create a standalone GIC v2 */
319     DeviceState *gicdev;
320     SysBusDevice *gicbusdev;
321     const char *gictype = "arm_gic";
322     int i;
323 
324     if (kvm_irqchip_in_kernel()) {
325         gictype = "kvm-arm-gic";
326     }
327 
328     gicdev = qdev_create(NULL, gictype);
329     qdev_prop_set_uint32(gicdev, "revision", 2);
330     qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
331     /* Note that the num-irq property counts both internal and external
332      * interrupts; there are always 32 of the former (mandated by GIC spec).
333      */
334     qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
335     qdev_init_nofail(gicdev);
336     gicbusdev = SYS_BUS_DEVICE(gicdev);
337     sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
338     sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base);
339 
340     /* Wire the outputs from each CPU's generic timer to the
341      * appropriate GIC PPI inputs, and the GIC's IRQ output to
342      * the CPU's IRQ input.
343      */
344     for (i = 0; i < smp_cpus; i++) {
345         DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
346         int ppibase = NUM_IRQS + i * 32;
347         /* physical timer; we wire it up to the non-secure timer's ID,
348          * since a real A15 always has TrustZone but QEMU doesn't.
349          */
350         qdev_connect_gpio_out(cpudev, 0,
351                               qdev_get_gpio_in(gicdev, ppibase + 30));
352         /* virtual timer */
353         qdev_connect_gpio_out(cpudev, 1,
354                               qdev_get_gpio_in(gicdev, ppibase + 27));
355 
356         sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
357     }
358 
359     for (i = 0; i < NUM_IRQS; i++) {
360         pic[i] = qdev_get_gpio_in(gicdev, i);
361     }
362 
363     fdt_add_gic_node(vbi);
364 }
365 
366 static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic)
367 {
368     char *nodename;
369     hwaddr base = vbi->memmap[VIRT_UART].base;
370     hwaddr size = vbi->memmap[VIRT_UART].size;
371     int irq = vbi->irqmap[VIRT_UART];
372     const char compat[] = "arm,pl011\0arm,primecell";
373     const char clocknames[] = "uartclk\0apb_pclk";
374 
375     sysbus_create_simple("pl011", base, pic[irq]);
376 
377     nodename = g_strdup_printf("/pl011@%" PRIx64, base);
378     qemu_fdt_add_subnode(vbi->fdt, nodename);
379     /* Note that we can't use setprop_string because of the embedded NUL */
380     qemu_fdt_setprop(vbi->fdt, nodename, "compatible",
381                          compat, sizeof(compat));
382     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
383                                      2, base, 2, size);
384     qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
385                                GIC_FDT_IRQ_TYPE_SPI, irq,
386                                GIC_FDT_IRQ_FLAGS_LEVEL_HI);
387     qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks",
388                                vbi->clock_phandle, vbi->clock_phandle);
389     qemu_fdt_setprop(vbi->fdt, nodename, "clock-names",
390                          clocknames, sizeof(clocknames));
391 
392     qemu_fdt_setprop_string(vbi->fdt, "/chosen", "linux,stdout-path", nodename);
393     g_free(nodename);
394 }
395 
396 static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic)
397 {
398     char *nodename;
399     hwaddr base = vbi->memmap[VIRT_RTC].base;
400     hwaddr size = vbi->memmap[VIRT_RTC].size;
401     int irq = vbi->irqmap[VIRT_RTC];
402     const char compat[] = "arm,pl031\0arm,primecell";
403 
404     sysbus_create_simple("pl031", base, pic[irq]);
405 
406     nodename = g_strdup_printf("/pl031@%" PRIx64, base);
407     qemu_fdt_add_subnode(vbi->fdt, nodename);
408     qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
409     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
410                                  2, base, 2, size);
411     qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
412                            GIC_FDT_IRQ_TYPE_SPI, irq,
413                            GIC_FDT_IRQ_FLAGS_LEVEL_HI);
414     qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
415     qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
416     g_free(nodename);
417 }
418 
419 static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic)
420 {
421     int i;
422     hwaddr size = vbi->memmap[VIRT_MMIO].size;
423 
424     /* Note that we have to create the transports in forwards order
425      * so that command line devices are inserted lowest address first,
426      * and then add dtb nodes in reverse order so that they appear in
427      * the finished device tree lowest address first.
428      */
429     for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
430         int irq = vbi->irqmap[VIRT_MMIO] + i;
431         hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
432 
433         sysbus_create_simple("virtio-mmio", base, pic[irq]);
434     }
435 
436     for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
437         char *nodename;
438         int irq = vbi->irqmap[VIRT_MMIO] + i;
439         hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
440 
441         nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
442         qemu_fdt_add_subnode(vbi->fdt, nodename);
443         qemu_fdt_setprop_string(vbi->fdt, nodename,
444                                 "compatible", "virtio,mmio");
445         qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
446                                      2, base, 2, size);
447         qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
448                                GIC_FDT_IRQ_TYPE_SPI, irq,
449                                GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
450         g_free(nodename);
451     }
452 }
453 
454 static void create_one_flash(const char *name, hwaddr flashbase,
455                              hwaddr flashsize)
456 {
457     /* Create and map a single flash device. We use the same
458      * parameters as the flash devices on the Versatile Express board.
459      */
460     DriveInfo *dinfo = drive_get_next(IF_PFLASH);
461     DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
462     const uint64_t sectorlength = 256 * 1024;
463 
464     if (dinfo && qdev_prop_set_drive(dev, "drive",
465                                      blk_by_legacy_dinfo(dinfo))) {
466         abort();
467     }
468 
469     qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
470     qdev_prop_set_uint64(dev, "sector-length", sectorlength);
471     qdev_prop_set_uint8(dev, "width", 4);
472     qdev_prop_set_uint8(dev, "device-width", 2);
473     qdev_prop_set_uint8(dev, "big-endian", 0);
474     qdev_prop_set_uint16(dev, "id0", 0x89);
475     qdev_prop_set_uint16(dev, "id1", 0x18);
476     qdev_prop_set_uint16(dev, "id2", 0x00);
477     qdev_prop_set_uint16(dev, "id3", 0x00);
478     qdev_prop_set_string(dev, "name", name);
479     qdev_init_nofail(dev);
480 
481     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase);
482 }
483 
484 static void create_flash(const VirtBoardInfo *vbi)
485 {
486     /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
487      * Any file passed via -bios goes in the first of these.
488      */
489     hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2;
490     hwaddr flashbase = vbi->memmap[VIRT_FLASH].base;
491     char *nodename;
492 
493     if (bios_name) {
494         const char *fn;
495 
496         if (drive_get(IF_PFLASH, 0, 0)) {
497             error_report("The contents of the first flash device may be "
498                          "specified with -bios or with -drive if=pflash... "
499                          "but you cannot use both options at once");
500             exit(1);
501         }
502         fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
503         if (!fn || load_image_targphys(fn, flashbase, flashsize) < 0) {
504             error_report("Could not load ROM image '%s'", bios_name);
505             exit(1);
506         }
507     }
508 
509     create_one_flash("virt.flash0", flashbase, flashsize);
510     create_one_flash("virt.flash1", flashbase + flashsize, flashsize);
511 
512     nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
513     qemu_fdt_add_subnode(vbi->fdt, nodename);
514     qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash");
515     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
516                                  2, flashbase, 2, flashsize,
517                                  2, flashbase + flashsize, 2, flashsize);
518     qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4);
519     g_free(nodename);
520 }
521 
522 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
523 {
524     const VirtBoardInfo *board = (const VirtBoardInfo *)binfo;
525 
526     *fdt_size = board->fdt_size;
527     return board->fdt;
528 }
529 
530 static void machvirt_init(MachineState *machine)
531 {
532     qemu_irq pic[NUM_IRQS];
533     MemoryRegion *sysmem = get_system_memory();
534     int n;
535     MemoryRegion *ram = g_new(MemoryRegion, 1);
536     const char *cpu_model = machine->cpu_model;
537     VirtBoardInfo *vbi;
538 
539     if (!cpu_model) {
540         cpu_model = "cortex-a15";
541     }
542 
543     vbi = find_machine_info(cpu_model);
544 
545     if (!vbi) {
546         error_report("mach-virt: CPU %s not supported", cpu_model);
547         exit(1);
548     }
549 
550     vbi->smp_cpus = smp_cpus;
551 
552     if (machine->ram_size > vbi->memmap[VIRT_MEM].size) {
553         error_report("mach-virt: cannot model more than 30GB RAM");
554         exit(1);
555     }
556 
557     create_fdt(vbi);
558 
559     for (n = 0; n < smp_cpus; n++) {
560         ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
561         Object *cpuobj;
562 
563         if (!oc) {
564             fprintf(stderr, "Unable to find CPU definition\n");
565             exit(1);
566         }
567         cpuobj = object_new(object_class_get_name(oc));
568 
569         object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit",
570                                 NULL);
571 
572         /* Secondary CPUs start in PSCI powered-down state */
573         if (n > 0) {
574             object_property_set_bool(cpuobj, true, "start-powered-off", NULL);
575         }
576 
577         if (object_property_find(cpuobj, "reset-cbar", NULL)) {
578             object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base,
579                                     "reset-cbar", &error_abort);
580         }
581 
582         object_property_set_bool(cpuobj, true, "realized", NULL);
583     }
584     fdt_add_timer_nodes(vbi);
585     fdt_add_cpu_nodes(vbi);
586     fdt_add_psci_node(vbi);
587 
588     memory_region_init_ram(ram, NULL, "mach-virt.ram", machine->ram_size,
589                            &error_abort);
590     vmstate_register_ram_global(ram);
591     memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram);
592 
593     create_flash(vbi);
594 
595     create_gic(vbi, pic);
596 
597     create_uart(vbi, pic);
598 
599     create_rtc(vbi, pic);
600 
601     /* Create mmio transports, so the user can create virtio backends
602      * (which will be automatically plugged in to the transports). If
603      * no backend is created the transport will just sit harmlessly idle.
604      */
605     create_virtio_devices(vbi, pic);
606 
607     vbi->bootinfo.ram_size = machine->ram_size;
608     vbi->bootinfo.kernel_filename = machine->kernel_filename;
609     vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline;
610     vbi->bootinfo.initrd_filename = machine->initrd_filename;
611     vbi->bootinfo.nb_cpus = smp_cpus;
612     vbi->bootinfo.board_id = -1;
613     vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
614     vbi->bootinfo.get_dtb = machvirt_dtb;
615     arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
616 }
617 
618 static QEMUMachine machvirt_a15_machine = {
619     .name = "virt",
620     .desc = "ARM Virtual Machine",
621     .init = machvirt_init,
622     .max_cpus = 8,
623 };
624 
625 static void machvirt_machine_init(void)
626 {
627     qemu_register_machine(&machvirt_a15_machine);
628 }
629 
630 machine_init(machvirt_machine_init);
631