xref: /openbmc/qemu/hw/m68k/virt.c (revision b14df228)
1 /*
2  * SPDX-License-Identifier: GPL-2.0-or-later
3  *
4  * QEMU Vitual M68K Machine
5  *
6  * (c) 2020 Laurent Vivier <laurent@vivier.eu>
7  *
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qemu/units.h"
12 #include "qemu/guest-random.h"
13 #include "sysemu/sysemu.h"
14 #include "cpu.h"
15 #include "hw/boards.h"
16 #include "hw/qdev-properties.h"
17 #include "elf.h"
18 #include "hw/loader.h"
19 #include "ui/console.h"
20 #include "hw/sysbus.h"
21 #include "standard-headers/asm-m68k/bootinfo.h"
22 #include "standard-headers/asm-m68k/bootinfo-virt.h"
23 #include "bootinfo.h"
24 #include "net/net.h"
25 #include "qapi/error.h"
26 #include "sysemu/qtest.h"
27 #include "sysemu/runstate.h"
28 #include "sysemu/reset.h"
29 
30 #include "hw/intc/m68k_irqc.h"
31 #include "hw/misc/virt_ctrl.h"
32 #include "hw/char/goldfish_tty.h"
33 #include "hw/rtc/goldfish_rtc.h"
34 #include "hw/intc/goldfish_pic.h"
35 #include "hw/virtio/virtio-mmio.h"
36 #include "hw/virtio/virtio-blk.h"
37 
38 /*
39  * 6 goldfish-pic for CPU IRQ #1 to IRQ #6
40  * CPU IRQ #1 -> PIC #1
41  *               IRQ #1 to IRQ #31 -> unused
42  *               IRQ #32 -> goldfish-tty
43  * CPU IRQ #2 -> PIC #2
44  *               IRQ #1 to IRQ #32 -> virtio-mmio from 1 to 32
45  * CPU IRQ #3 -> PIC #3
46  *               IRQ #1 to IRQ #32 -> virtio-mmio from 33 to 64
47  * CPU IRQ #4 -> PIC #4
48  *               IRQ #1 to IRQ #32 -> virtio-mmio from 65 to 96
49  * CPU IRQ #5 -> PIC #5
50  *               IRQ #1 to IRQ #32 -> virtio-mmio from 97 to 128
51  * CPU IRQ #6 -> PIC #6
52  *               IRQ #1 -> goldfish-rtc
53  *               IRQ #2 to IRQ #32 -> unused
54  * CPU IRQ #7 -> NMI
55  */
56 
57 #define PIC_IRQ_BASE(num)     (8 + (num - 1) * 32)
58 #define PIC_IRQ(num, irq)     (PIC_IRQ_BASE(num) + irq - 1)
59 #define PIC_GPIO(pic_irq)     (qdev_get_gpio_in(pic_dev[(pic_irq - 8) / 32], \
60                                                 (pic_irq - 8) % 32))
61 
62 #define VIRT_GF_PIC_MMIO_BASE 0xff000000     /* MMIO: 0xff000000 - 0xff005fff */
63 #define VIRT_GF_PIC_IRQ_BASE  1              /* IRQ: #1 -> #6 */
64 #define VIRT_GF_PIC_NB        6
65 
66 /* 2 goldfish-rtc (and timer) */
67 #define VIRT_GF_RTC_MMIO_BASE 0xff006000     /* MMIO: 0xff006000 - 0xff007fff */
68 #define VIRT_GF_RTC_IRQ_BASE  PIC_IRQ(6, 1)  /* PIC: #6, IRQ: #1 */
69 #define VIRT_GF_RTC_NB        2
70 
71 /* 1 goldfish-tty */
72 #define VIRT_GF_TTY_MMIO_BASE 0xff008000     /* MMIO: 0xff008000 - 0xff008fff */
73 #define VIRT_GF_TTY_IRQ_BASE  PIC_IRQ(1, 32) /* PIC: #1, IRQ: #32 */
74 
75 /* 1 virt-ctrl */
76 #define VIRT_CTRL_MMIO_BASE 0xff009000    /* MMIO: 0xff009000 - 0xff009fff */
77 #define VIRT_CTRL_IRQ_BASE  PIC_IRQ(1, 1) /* PIC: #1, IRQ: #1 */
78 
79 /*
80  * virtio-mmio size is 0x200 bytes
81  * we use 4 goldfish-pic to attach them,
82  * we can attach 32 virtio devices / goldfish-pic
83  * -> we can manage 32 * 4 = 128 virtio devices
84  */
85 #define VIRT_VIRTIO_MMIO_BASE 0xff010000     /* MMIO: 0xff010000 - 0xff01ffff */
86 #define VIRT_VIRTIO_IRQ_BASE  PIC_IRQ(2, 1)  /* PIC: 2, 3, 4, 5, IRQ: ALL */
87 
88 typedef struct {
89     M68kCPU *cpu;
90     hwaddr initial_pc;
91     hwaddr initial_stack;
92 } ResetInfo;
93 
94 static void main_cpu_reset(void *opaque)
95 {
96     ResetInfo *reset_info = opaque;
97     M68kCPU *cpu = reset_info->cpu;
98     CPUState *cs = CPU(cpu);
99 
100     cpu_reset(cs);
101     cpu->env.aregs[7] = reset_info->initial_stack;
102     cpu->env.pc = reset_info->initial_pc;
103 }
104 
105 static void virt_init(MachineState *machine)
106 {
107     M68kCPU *cpu = NULL;
108     int32_t kernel_size;
109     uint64_t elf_entry;
110     ram_addr_t initrd_base;
111     int32_t initrd_size;
112     ram_addr_t ram_size = machine->ram_size;
113     const char *kernel_filename = machine->kernel_filename;
114     const char *initrd_filename = machine->initrd_filename;
115     const char *kernel_cmdline = machine->kernel_cmdline;
116     hwaddr parameters_base;
117     DeviceState *dev;
118     DeviceState *irqc_dev;
119     DeviceState *pic_dev[VIRT_GF_PIC_NB];
120     SysBusDevice *sysbus;
121     hwaddr io_base;
122     int i;
123     ResetInfo *reset_info;
124     uint8_t rng_seed[32];
125 
126     if (ram_size > 3399672 * KiB) {
127         /*
128          * The physical memory can be up to 4 GiB - 16 MiB, but linux
129          * kernel crashes after this limit (~ 3.2 GiB)
130          */
131         error_report("Too much memory for this machine: %" PRId64 " KiB, "
132                      "maximum 3399672 KiB", ram_size / KiB);
133         exit(1);
134     }
135 
136     reset_info = g_new0(ResetInfo, 1);
137 
138     /* init CPUs */
139     cpu = M68K_CPU(cpu_create(machine->cpu_type));
140 
141     reset_info->cpu = cpu;
142     qemu_register_reset(main_cpu_reset, reset_info);
143 
144     /* RAM */
145     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
146 
147     /* IRQ Controller */
148 
149     irqc_dev = qdev_new(TYPE_M68K_IRQC);
150     sysbus_realize_and_unref(SYS_BUS_DEVICE(irqc_dev), &error_fatal);
151 
152     /*
153      * 6 goldfish-pic
154      *
155      * map: 0xff000000 - 0xff006fff = 28 KiB
156      * IRQ: #1 (lower priority) -> #6 (higher priority)
157      *
158      */
159     io_base = VIRT_GF_PIC_MMIO_BASE;
160     for (i = 0; i < VIRT_GF_PIC_NB; i++) {
161         pic_dev[i] = qdev_new(TYPE_GOLDFISH_PIC);
162         sysbus = SYS_BUS_DEVICE(pic_dev[i]);
163         qdev_prop_set_uint8(pic_dev[i], "index", i);
164         sysbus_realize_and_unref(sysbus, &error_fatal);
165 
166         sysbus_mmio_map(sysbus, 0, io_base);
167         sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(irqc_dev, i));
168 
169         io_base += 0x1000;
170     }
171 
172     /* goldfish-rtc */
173     io_base = VIRT_GF_RTC_MMIO_BASE;
174     for (i = 0; i < VIRT_GF_RTC_NB; i++) {
175         dev = qdev_new(TYPE_GOLDFISH_RTC);
176         qdev_prop_set_bit(dev, "big-endian", true);
177         sysbus = SYS_BUS_DEVICE(dev);
178         sysbus_realize_and_unref(sysbus, &error_fatal);
179         sysbus_mmio_map(sysbus, 0, io_base);
180         sysbus_connect_irq(sysbus, 0, PIC_GPIO(VIRT_GF_RTC_IRQ_BASE + i));
181 
182         io_base += 0x1000;
183     }
184 
185     /* goldfish-tty */
186     dev = qdev_new(TYPE_GOLDFISH_TTY);
187     sysbus = SYS_BUS_DEVICE(dev);
188     qdev_prop_set_chr(dev, "chardev", serial_hd(0));
189     sysbus_realize_and_unref(sysbus, &error_fatal);
190     sysbus_mmio_map(sysbus, 0, VIRT_GF_TTY_MMIO_BASE);
191     sysbus_connect_irq(sysbus, 0, PIC_GPIO(VIRT_GF_TTY_IRQ_BASE));
192 
193     /* virt controller */
194     dev = qdev_new(TYPE_VIRT_CTRL);
195     sysbus = SYS_BUS_DEVICE(dev);
196     sysbus_realize_and_unref(sysbus, &error_fatal);
197     sysbus_mmio_map(sysbus, 0, VIRT_CTRL_MMIO_BASE);
198     sysbus_connect_irq(sysbus, 0, PIC_GPIO(VIRT_CTRL_IRQ_BASE));
199 
200     /* virtio-mmio */
201     io_base = VIRT_VIRTIO_MMIO_BASE;
202     for (i = 0; i < 128; i++) {
203         dev = qdev_new(TYPE_VIRTIO_MMIO);
204         qdev_prop_set_bit(dev, "force-legacy", false);
205         sysbus = SYS_BUS_DEVICE(dev);
206         sysbus_realize_and_unref(sysbus, &error_fatal);
207         sysbus_connect_irq(sysbus, 0, PIC_GPIO(VIRT_VIRTIO_IRQ_BASE + i));
208         sysbus_mmio_map(sysbus, 0, io_base);
209         io_base += 0x200;
210     }
211 
212     if (kernel_filename) {
213         CPUState *cs = CPU(cpu);
214         uint64_t high;
215 
216         kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
217                                &elf_entry, NULL, &high, NULL, 1,
218                                EM_68K, 0, 0);
219         if (kernel_size < 0) {
220             error_report("could not load kernel '%s'", kernel_filename);
221             exit(1);
222         }
223         reset_info->initial_pc = elf_entry;
224         parameters_base = (high + 1) & ~1;
225 
226         BOOTINFO1(cs->as, parameters_base, BI_MACHTYPE, MACH_VIRT);
227         BOOTINFO1(cs->as, parameters_base, BI_FPUTYPE, FPU_68040);
228         BOOTINFO1(cs->as, parameters_base, BI_MMUTYPE, MMU_68040);
229         BOOTINFO1(cs->as, parameters_base, BI_CPUTYPE, CPU_68040);
230         BOOTINFO2(cs->as, parameters_base, BI_MEMCHUNK, 0, ram_size);
231 
232         BOOTINFO1(cs->as, parameters_base, BI_VIRT_QEMU_VERSION,
233                   ((QEMU_VERSION_MAJOR << 24) | (QEMU_VERSION_MINOR << 16) |
234                    (QEMU_VERSION_MICRO << 8)));
235         BOOTINFO2(cs->as, parameters_base, BI_VIRT_GF_PIC_BASE,
236                   VIRT_GF_PIC_MMIO_BASE, VIRT_GF_PIC_IRQ_BASE);
237         BOOTINFO2(cs->as, parameters_base, BI_VIRT_GF_RTC_BASE,
238                   VIRT_GF_RTC_MMIO_BASE, VIRT_GF_RTC_IRQ_BASE);
239         BOOTINFO2(cs->as, parameters_base, BI_VIRT_GF_TTY_BASE,
240                   VIRT_GF_TTY_MMIO_BASE, VIRT_GF_TTY_IRQ_BASE);
241         BOOTINFO2(cs->as, parameters_base, BI_VIRT_CTRL_BASE,
242                   VIRT_CTRL_MMIO_BASE, VIRT_CTRL_IRQ_BASE);
243         BOOTINFO2(cs->as, parameters_base, BI_VIRT_VIRTIO_BASE,
244                   VIRT_VIRTIO_MMIO_BASE, VIRT_VIRTIO_IRQ_BASE);
245 
246         if (kernel_cmdline) {
247             BOOTINFOSTR(cs->as, parameters_base, BI_COMMAND_LINE,
248                         kernel_cmdline);
249         }
250 
251 	/* Pass seed to RNG. */
252 	qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
253 	BOOTINFODATA(cs->as, parameters_base, BI_VIRT_RNG_SEED,
254 		     rng_seed, sizeof(rng_seed));
255 
256         /* load initrd */
257         if (initrd_filename) {
258             initrd_size = get_image_size(initrd_filename);
259             if (initrd_size < 0) {
260                 error_report("could not load initial ram disk '%s'",
261                              initrd_filename);
262                 exit(1);
263             }
264 
265             initrd_base = (ram_size - initrd_size) & TARGET_PAGE_MASK;
266             load_image_targphys(initrd_filename, initrd_base,
267                                 ram_size - initrd_base);
268             BOOTINFO2(cs->as, parameters_base, BI_RAMDISK, initrd_base,
269                       initrd_size);
270         } else {
271             initrd_base = 0;
272             initrd_size = 0;
273         }
274         BOOTINFO0(cs->as, parameters_base, BI_LAST);
275     }
276 }
277 
278 static void virt_machine_class_init(ObjectClass *oc, void *data)
279 {
280     MachineClass *mc = MACHINE_CLASS(oc);
281     mc->desc = "QEMU M68K Virtual Machine";
282     mc->init = virt_init;
283     mc->default_cpu_type = M68K_CPU_TYPE_NAME("m68040");
284     mc->max_cpus = 1;
285     mc->no_floppy = 1;
286     mc->no_parallel = 1;
287     mc->default_ram_id = "m68k_virt.ram";
288 }
289 
290 static const TypeInfo virt_machine_info = {
291     .name       = MACHINE_TYPE_NAME("virt"),
292     .parent     = TYPE_MACHINE,
293     .abstract   = true,
294     .class_init = virt_machine_class_init,
295 };
296 
297 static void virt_machine_register_types(void)
298 {
299     type_register_static(&virt_machine_info);
300 }
301 
302 type_init(virt_machine_register_types)
303 
304 #define DEFINE_VIRT_MACHINE(major, minor, latest) \
305     static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
306                                                     void *data) \
307     { \
308         MachineClass *mc = MACHINE_CLASS(oc); \
309         virt_machine_##major##_##minor##_options(mc); \
310         mc->desc = "QEMU " # major "." # minor " M68K Virtual Machine"; \
311         if (latest) { \
312             mc->alias = "virt"; \
313         } \
314     } \
315     static const TypeInfo machvirt_##major##_##minor##_info = { \
316         .name = MACHINE_TYPE_NAME("virt-" # major "." # minor), \
317         .parent = MACHINE_TYPE_NAME("virt"), \
318         .class_init = virt_##major##_##minor##_class_init, \
319     }; \
320     static void machvirt_machine_##major##_##minor##_init(void) \
321     { \
322         type_register_static(&machvirt_##major##_##minor##_info); \
323     } \
324     type_init(machvirt_machine_##major##_##minor##_init);
325 
326 static void virt_machine_7_2_options(MachineClass *mc)
327 {
328 }
329 DEFINE_VIRT_MACHINE(7, 2, true)
330 
331 static void virt_machine_7_1_options(MachineClass *mc)
332 {
333     virt_machine_7_2_options(mc);
334     compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len);
335 }
336 DEFINE_VIRT_MACHINE(7, 1, false)
337 
338 static void virt_machine_7_0_options(MachineClass *mc)
339 {
340     virt_machine_7_1_options(mc);
341     compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len);
342 }
343 DEFINE_VIRT_MACHINE(7, 0, false)
344 
345 static void virt_machine_6_2_options(MachineClass *mc)
346 {
347     virt_machine_7_0_options(mc);
348     compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len);
349 }
350 DEFINE_VIRT_MACHINE(6, 2, false)
351 
352 static void virt_machine_6_1_options(MachineClass *mc)
353 {
354     virt_machine_6_2_options(mc);
355     compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len);
356 }
357 DEFINE_VIRT_MACHINE(6, 1, false)
358 
359 static void virt_machine_6_0_options(MachineClass *mc)
360 {
361     virt_machine_6_1_options(mc);
362     compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len);
363 }
364 DEFINE_VIRT_MACHINE(6, 0, false)
365