blob: d985d2e56cbbb560aec6e464858644fed4e44cb9 [file] [log] [blame]
Peter Maydellf5fdcd62013-11-22 17:17:14 +00001/*
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 "exec/address-spaces.h"
41#include "qemu/bitops.h"
42#include "qemu/error-report.h"
43
44#define NUM_VIRTIO_TRANSPORTS 32
45
46/* Number of external interrupt lines to configure the GIC with */
47#define NUM_IRQS 128
48
49#define GIC_FDT_IRQ_TYPE_SPI 0
50#define GIC_FDT_IRQ_TYPE_PPI 1
51
52#define GIC_FDT_IRQ_FLAGS_EDGE_LO_HI 1
53#define GIC_FDT_IRQ_FLAGS_EDGE_HI_LO 2
54#define GIC_FDT_IRQ_FLAGS_LEVEL_HI 4
55#define GIC_FDT_IRQ_FLAGS_LEVEL_LO 8
56
57#define GIC_FDT_IRQ_PPI_CPU_START 8
58#define GIC_FDT_IRQ_PPI_CPU_WIDTH 8
59
60enum {
61 VIRT_FLASH,
62 VIRT_MEM,
63 VIRT_CPUPERIPHS,
64 VIRT_GIC_DIST,
65 VIRT_GIC_CPU,
66 VIRT_UART,
67 VIRT_MMIO,
68};
69
70typedef struct MemMapEntry {
71 hwaddr base;
72 hwaddr size;
73} MemMapEntry;
74
75typedef struct VirtBoardInfo {
76 struct arm_boot_info bootinfo;
77 const char *cpu_model;
78 const char *qdevname;
79 const char *gic_compatible;
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 */
98static const MemMapEntry a15memmap[] = {
99 /* Space up to 0x8000000 is reserved for a boot ROM */
100 [VIRT_FLASH] = { 0, 0x8000000 },
101 [VIRT_CPUPERIPHS] = { 0x8000000, 0x8000 },
102 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
103 [VIRT_GIC_DIST] = { 0x8001000, 0x1000 },
104 [VIRT_GIC_CPU] = { 0x8002000, 0x1000 },
105 [VIRT_UART] = { 0x9000000, 0x1000 },
106 [VIRT_MMIO] = { 0xa000000, 0x200 },
107 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
108 /* 0x10000000 .. 0x40000000 reserved for PCI */
109 [VIRT_MEM] = { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
110};
111
112static const int a15irqmap[] = {
113 [VIRT_UART] = 1,
114 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
115};
116
117static VirtBoardInfo machines[] = {
118 {
119 .cpu_model = "cortex-a15",
120 .qdevname = "a15mpcore_priv",
121 .gic_compatible = "arm,cortex-a15-gic",
122 .memmap = a15memmap,
123 .irqmap = a15irqmap,
124 },
Peter Maydell198aa062013-11-22 17:17:18 +0000125 {
Peter Maydell405a2c82014-02-11 18:41:38 +0000126 .cpu_model = "cortex-a57",
127 /* Use the A15 private peripheral model for now: probably wrong! */
128 .qdevname = "a15mpcore_priv",
129 .gic_compatible = "arm,cortex-a15-gic",
130 .memmap = a15memmap,
131 .irqmap = a15irqmap,
132 },
133 {
Peter Maydell198aa062013-11-22 17:17:18 +0000134 .cpu_model = "host",
135 /* We use the A15 private peripheral model to get a V2 GIC */
136 .qdevname = "a15mpcore_priv",
137 .gic_compatible = "arm,cortex-a15-gic",
138 .memmap = a15memmap,
139 .irqmap = a15irqmap,
140 },
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000141};
142
143static VirtBoardInfo *find_machine_info(const char *cpu)
144{
145 int i;
146
147 for (i = 0; i < ARRAY_SIZE(machines); i++) {
148 if (strcmp(cpu, machines[i].cpu_model) == 0) {
149 return &machines[i];
150 }
151 }
152 return NULL;
153}
154
155static void create_fdt(VirtBoardInfo *vbi)
156{
157 void *fdt = create_device_tree(&vbi->fdt_size);
158
159 if (!fdt) {
160 error_report("create_device_tree() failed");
161 exit(1);
162 }
163
164 vbi->fdt = fdt;
165
166 /* Header */
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000167 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
168 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
169 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000170
171 /*
172 * /chosen and /memory nodes must exist for load_dtb
173 * to fill in necessary properties later
174 */
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000175 qemu_fdt_add_subnode(fdt, "/chosen");
176 qemu_fdt_add_subnode(fdt, "/memory");
177 qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000178
179 /* Clock node, for the benefit of the UART. The kernel device tree
180 * binding documentation claims the PL011 node clock properties are
181 * optional but in practice if you omit them the kernel refuses to
182 * probe for the device.
183 */
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000184 vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt);
185 qemu_fdt_add_subnode(fdt, "/apb-pclk");
186 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
187 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
188 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
189 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000190 "clk24mhz");
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000191 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000192
193 /* No PSCI for TCG yet */
194 if (kvm_enabled()) {
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000195 qemu_fdt_add_subnode(fdt, "/psci");
196 qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
197 qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
198 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000199 PSCI_FN_CPU_SUSPEND);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000200 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", PSCI_FN_CPU_OFF);
201 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", PSCI_FN_CPU_ON);
202 qemu_fdt_setprop_cell(fdt, "/psci", "migrate", PSCI_FN_MIGRATE);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000203 }
204}
205
206static void fdt_add_timer_nodes(const VirtBoardInfo *vbi)
207{
208 /* Note that on A15 h/w these interrupts are level-triggered,
209 * but for the GIC implementation provided by both QEMU and KVM
210 * they are edge-triggered.
211 */
212 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
213
214 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
215 GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << vbi->smp_cpus) - 1);
216
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000217 qemu_fdt_add_subnode(vbi->fdt, "/timer");
218 qemu_fdt_setprop_string(vbi->fdt, "/timer",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000219 "compatible", "arm,armv7-timer");
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000220 qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000221 GIC_FDT_IRQ_TYPE_PPI, 13, irqflags,
222 GIC_FDT_IRQ_TYPE_PPI, 14, irqflags,
223 GIC_FDT_IRQ_TYPE_PPI, 11, irqflags,
224 GIC_FDT_IRQ_TYPE_PPI, 10, irqflags);
225}
226
227static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
228{
229 int cpu;
230
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000231 qemu_fdt_add_subnode(vbi->fdt, "/cpus");
232 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", 0x1);
233 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000234
235 for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) {
236 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
237 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
238
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000239 qemu_fdt_add_subnode(vbi->fdt, nodename);
240 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu");
241 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000242 armcpu->dtb_compatible);
243
244 if (vbi->smp_cpus > 1) {
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000245 qemu_fdt_setprop_string(vbi->fdt, nodename,
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000246 "enable-method", "psci");
247 }
248
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000249 qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000250 g_free(nodename);
251 }
252}
253
254static void fdt_add_gic_node(const VirtBoardInfo *vbi)
255{
256 uint32_t gic_phandle;
257
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000258 gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
259 qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", gic_phandle);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000260
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000261 qemu_fdt_add_subnode(vbi->fdt, "/intc");
262 qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000263 vbi->gic_compatible);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000264 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);
265 qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);
266 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000267 2, vbi->memmap[VIRT_GIC_DIST].base,
268 2, vbi->memmap[VIRT_GIC_DIST].size,
269 2, vbi->memmap[VIRT_GIC_CPU].base,
270 2, vbi->memmap[VIRT_GIC_CPU].size);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000271 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", gic_phandle);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000272}
273
274static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic)
275{
276 char *nodename;
277 hwaddr base = vbi->memmap[VIRT_UART].base;
278 hwaddr size = vbi->memmap[VIRT_UART].size;
279 int irq = vbi->irqmap[VIRT_UART];
280 const char compat[] = "arm,pl011\0arm,primecell";
281 const char clocknames[] = "uartclk\0apb_pclk";
282
283 sysbus_create_simple("pl011", base, pic[irq]);
284
285 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000286 qemu_fdt_add_subnode(vbi->fdt, nodename);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000287 /* Note that we can't use setprop_string because of the embedded NUL */
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000288 qemu_fdt_setprop(vbi->fdt, nodename, "compatible",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000289 compat, sizeof(compat));
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000290 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000291 2, base, 2, size);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000292 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000293 GIC_FDT_IRQ_TYPE_SPI, irq,
294 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000295 qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000296 vbi->clock_phandle, vbi->clock_phandle);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000297 qemu_fdt_setprop(vbi->fdt, nodename, "clock-names",
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000298 clocknames, sizeof(clocknames));
299 g_free(nodename);
300}
301
302static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic)
303{
304 int i;
305 hwaddr size = vbi->memmap[VIRT_MMIO].size;
306
307 /* Note that we have to create the transports in forwards order
308 * so that command line devices are inserted lowest address first,
309 * and then add dtb nodes in reverse order so that they appear in
310 * the finished device tree lowest address first.
311 */
312 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
313 int irq = vbi->irqmap[VIRT_MMIO] + i;
314 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
315
316 sysbus_create_simple("virtio-mmio", base, pic[irq]);
317 }
318
319 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
320 char *nodename;
321 int irq = vbi->irqmap[VIRT_MMIO] + i;
322 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
323
324 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000325 qemu_fdt_add_subnode(vbi->fdt, nodename);
326 qemu_fdt_setprop_string(vbi->fdt, nodename,
327 "compatible", "virtio,mmio");
328 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
329 2, base, 2, size);
330 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
331 GIC_FDT_IRQ_TYPE_SPI, irq,
332 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
Peter Maydellf5fdcd62013-11-22 17:17:14 +0000333 g_free(nodename);
334 }
335}
336
337static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
338{
339 const VirtBoardInfo *board = (const VirtBoardInfo *)binfo;
340
341 *fdt_size = board->fdt_size;
342 return board->fdt;
343}
344
345static void machvirt_init(QEMUMachineInitArgs *args)
346{
347 qemu_irq pic[NUM_IRQS];
348 MemoryRegion *sysmem = get_system_memory();
349 int n;
350 MemoryRegion *ram = g_new(MemoryRegion, 1);
351 DeviceState *dev;
352 SysBusDevice *busdev;
353 const char *cpu_model = args->cpu_model;
354 VirtBoardInfo *vbi;
355
356 if (!cpu_model) {
357 cpu_model = "cortex-a15";
358 }
359
360 vbi = find_machine_info(cpu_model);
361
362 if (!vbi) {
363 error_report("mach-virt: CPU %s not supported", cpu_model);
364 exit(1);
365 }
366
367 vbi->smp_cpus = smp_cpus;
368
369 /*
370 * Only supported method of starting secondary CPUs is PSCI and
371 * PSCI is not yet supported with TCG, so limit smp_cpus to 1
372 * if we're not using KVM.
373 */
374 if (!kvm_enabled() && smp_cpus > 1) {
375 error_report("mach-virt: must enable KVM to use multiple CPUs");
376 exit(1);
377 }
378
379 if (args->ram_size > vbi->memmap[VIRT_MEM].size) {
380 error_report("mach-virt: cannot model more than 30GB RAM");
381 exit(1);
382 }
383
384 create_fdt(vbi);
385 fdt_add_timer_nodes(vbi);
386
387 for (n = 0; n < smp_cpus; n++) {
388 ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
389 Object *cpuobj;
390
391 if (!oc) {
392 fprintf(stderr, "Unable to find CPU definition\n");
393 exit(1);
394 }
395 cpuobj = object_new(object_class_get_name(oc));
396
397 /* Secondary CPUs start in PSCI powered-down state */
398 if (n > 0) {
399 object_property_set_bool(cpuobj, true, "start-powered-off", NULL);
400 }
401 object_property_set_bool(cpuobj, true, "realized", NULL);
402 }
403 fdt_add_cpu_nodes(vbi);
404
405 memory_region_init_ram(ram, NULL, "mach-virt.ram", args->ram_size);
406 vmstate_register_ram_global(ram);
407 memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram);
408
409 dev = qdev_create(NULL, vbi->qdevname);
410 qdev_prop_set_uint32(dev, "num-cpu", smp_cpus);
411 /* Note that the num-irq property counts both internal and external
412 * interrupts; there are always 32 of the former (mandated by GIC spec).
413 */
414 qdev_prop_set_uint32(dev, "num-irq", NUM_IRQS + 32);
415 qdev_init_nofail(dev);
416 busdev = SYS_BUS_DEVICE(dev);
417 sysbus_mmio_map(busdev, 0, vbi->memmap[VIRT_CPUPERIPHS].base);
418 fdt_add_gic_node(vbi);
419 for (n = 0; n < smp_cpus; n++) {
420 DeviceState *cpudev = DEVICE(qemu_get_cpu(n));
421
422 sysbus_connect_irq(busdev, n, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
423 }
424
425 for (n = 0; n < NUM_IRQS; n++) {
426 pic[n] = qdev_get_gpio_in(dev, n);
427 }
428
429 create_uart(vbi, pic);
430
431 /* Create mmio transports, so the user can create virtio backends
432 * (which will be automatically plugged in to the transports). If
433 * no backend is created the transport will just sit harmlessly idle.
434 */
435 create_virtio_devices(vbi, pic);
436
437 vbi->bootinfo.ram_size = args->ram_size;
438 vbi->bootinfo.kernel_filename = args->kernel_filename;
439 vbi->bootinfo.kernel_cmdline = args->kernel_cmdline;
440 vbi->bootinfo.initrd_filename = args->initrd_filename;
441 vbi->bootinfo.nb_cpus = smp_cpus;
442 vbi->bootinfo.board_id = -1;
443 vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
444 vbi->bootinfo.get_dtb = machvirt_dtb;
445 arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
446}
447
448static QEMUMachine machvirt_a15_machine = {
449 .name = "virt",
450 .desc = "ARM Virtual Machine",
451 .init = machvirt_init,
452 .max_cpus = 4,
453};
454
455static void machvirt_machine_init(void)
456{
457 qemu_register_machine(&machvirt_a15_machine);
458}
459
460machine_init(machvirt_machine_init);