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