xref: /openbmc/qemu/hw/i386/microvm.c (revision 0d0f2a45)
1 /*
2  * Copyright (c) 2018 Intel Corporation
3  * Copyright (c) 2019 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2 or later, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "qemu/osdep.h"
19 #include "qemu/error-report.h"
20 #include "qemu/cutils.h"
21 #include "qemu/units.h"
22 #include "qapi/error.h"
23 #include "qapi/visitor.h"
24 #include "qapi/qapi-visit-common.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/numa.h"
28 #include "sysemu/reset.h"
29 #include "sysemu/runstate.h"
30 #include "acpi-microvm.h"
31 
32 #include "hw/loader.h"
33 #include "hw/irq.h"
34 #include "hw/kvm/clock.h"
35 #include "hw/i386/microvm.h"
36 #include "hw/i386/x86.h"
37 #include "target/i386/cpu.h"
38 #include "hw/intc/i8259.h"
39 #include "hw/timer/i8254.h"
40 #include "hw/rtc/mc146818rtc.h"
41 #include "hw/char/serial.h"
42 #include "hw/display/ramfb.h"
43 #include "hw/i386/topology.h"
44 #include "hw/i386/e820_memory_layout.h"
45 #include "hw/i386/fw_cfg.h"
46 #include "hw/virtio/virtio-mmio.h"
47 #include "hw/acpi/acpi.h"
48 #include "hw/acpi/generic_event_device.h"
49 #include "hw/pci-host/gpex.h"
50 #include "hw/usb/xhci.h"
51 
52 #include "cpu.h"
53 #include "elf.h"
54 #include "kvm_i386.h"
55 #include "hw/xen/start_info.h"
56 
57 #define MICROVM_QBOOT_FILENAME "qboot.rom"
58 #define MICROVM_BIOS_FILENAME  "bios-microvm.bin"
59 
60 static void microvm_set_rtc(MicrovmMachineState *mms, ISADevice *s)
61 {
62     X86MachineState *x86ms = X86_MACHINE(mms);
63     int val;
64 
65     val = MIN(x86ms->below_4g_mem_size / KiB, 640);
66     rtc_set_memory(s, 0x15, val);
67     rtc_set_memory(s, 0x16, val >> 8);
68     /* extended memory (next 64MiB) */
69     if (x86ms->below_4g_mem_size > 1 * MiB) {
70         val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB;
71     } else {
72         val = 0;
73     }
74     if (val > 65535) {
75         val = 65535;
76     }
77     rtc_set_memory(s, 0x17, val);
78     rtc_set_memory(s, 0x18, val >> 8);
79     rtc_set_memory(s, 0x30, val);
80     rtc_set_memory(s, 0x31, val >> 8);
81     /* memory between 16MiB and 4GiB */
82     if (x86ms->below_4g_mem_size > 16 * MiB) {
83         val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
84     } else {
85         val = 0;
86     }
87     if (val > 65535) {
88         val = 65535;
89     }
90     rtc_set_memory(s, 0x34, val);
91     rtc_set_memory(s, 0x35, val >> 8);
92     /* memory above 4GiB */
93     val = x86ms->above_4g_mem_size / 65536;
94     rtc_set_memory(s, 0x5b, val);
95     rtc_set_memory(s, 0x5c, val >> 8);
96     rtc_set_memory(s, 0x5d, val >> 16);
97 }
98 
99 static void create_gpex(MicrovmMachineState *mms)
100 {
101     X86MachineState *x86ms = X86_MACHINE(mms);
102     MemoryRegion *mmio32_alias;
103     MemoryRegion *mmio64_alias;
104     MemoryRegion *mmio_reg;
105     MemoryRegion *ecam_alias;
106     MemoryRegion *ecam_reg;
107     DeviceState *dev;
108     int i;
109 
110     dev = qdev_new(TYPE_GPEX_HOST);
111     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
112 
113     /* Map only the first size_ecam bytes of ECAM space */
114     ecam_alias = g_new0(MemoryRegion, 1);
115     ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
116     memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
117                              ecam_reg, 0, mms->gpex.ecam.size);
118     memory_region_add_subregion(get_system_memory(),
119                                 mms->gpex.ecam.base, ecam_alias);
120 
121     /* Map the MMIO window into system address space so as to expose
122      * the section of PCI MMIO space which starts at the same base address
123      * (ie 1:1 mapping for that part of PCI MMIO space visible through
124      * the window).
125      */
126     mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
127     if (mms->gpex.mmio32.size) {
128         mmio32_alias = g_new0(MemoryRegion, 1);
129         memory_region_init_alias(mmio32_alias, OBJECT(dev), "pcie-mmio32", mmio_reg,
130                                  mms->gpex.mmio32.base, mms->gpex.mmio32.size);
131         memory_region_add_subregion(get_system_memory(),
132                                     mms->gpex.mmio32.base, mmio32_alias);
133     }
134     if (mms->gpex.mmio64.size) {
135         mmio64_alias = g_new0(MemoryRegion, 1);
136         memory_region_init_alias(mmio64_alias, OBJECT(dev), "pcie-mmio64", mmio_reg,
137                                  mms->gpex.mmio64.base, mms->gpex.mmio64.size);
138         memory_region_add_subregion(get_system_memory(),
139                                     mms->gpex.mmio64.base, mmio64_alias);
140     }
141 
142     for (i = 0; i < GPEX_NUM_IRQS; i++) {
143         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
144                            x86ms->gsi[mms->gpex.irq + i]);
145     }
146 }
147 
148 static int microvm_ioapics(MicrovmMachineState *mms)
149 {
150     if (!x86_machine_is_acpi_enabled(X86_MACHINE(mms))) {
151         return 1;
152     }
153     if (mms->ioapic2 == ON_OFF_AUTO_OFF) {
154         return 1;
155     }
156     return 2;
157 }
158 
159 static void microvm_devices_init(MicrovmMachineState *mms)
160 {
161     X86MachineState *x86ms = X86_MACHINE(mms);
162     ISABus *isa_bus;
163     ISADevice *rtc_state;
164     GSIState *gsi_state;
165     int ioapics;
166     int i;
167 
168     /* Core components */
169     ioapics = microvm_ioapics(mms);
170     gsi_state = g_malloc0(sizeof(*gsi_state));
171     x86ms->gsi = qemu_allocate_irqs(gsi_handler, gsi_state,
172                                     IOAPIC_NUM_PINS * ioapics);
173 
174     isa_bus = isa_bus_new(NULL, get_system_memory(), get_system_io(),
175                           &error_abort);
176     isa_bus_irqs(isa_bus, x86ms->gsi);
177 
178     ioapic_init_gsi(gsi_state, "machine");
179     if (ioapics > 1) {
180         x86ms->ioapic2 = ioapic_init_secondary(gsi_state);
181     }
182 
183     kvmclock_create(true);
184 
185     mms->virtio_irq_base = 5;
186     mms->virtio_num_transports = 8;
187     if (x86ms->ioapic2) {
188         mms->pcie_irq_base = 16;    /* 16 -> 19 */
189         /* use second ioapic (24 -> 47) for virtio-mmio irq lines */
190         mms->virtio_irq_base = IO_APIC_SECONDARY_IRQBASE;
191         mms->virtio_num_transports = IOAPIC_NUM_PINS;
192     } else if (x86_machine_is_acpi_enabled(x86ms)) {
193         mms->pcie_irq_base = 12;    /* 12 -> 15 */
194         mms->virtio_irq_base = 16;  /* 16 -> 23 */
195     }
196 
197     for (i = 0; i < mms->virtio_num_transports; i++) {
198         sysbus_create_simple("virtio-mmio",
199                              VIRTIO_MMIO_BASE + i * 512,
200                              x86ms->gsi[mms->virtio_irq_base + i]);
201     }
202 
203     /* Optional and legacy devices */
204     if (x86_machine_is_acpi_enabled(x86ms)) {
205         DeviceState *dev = qdev_new(TYPE_ACPI_GED_X86);
206         qdev_prop_set_uint32(dev, "ged-event", ACPI_GED_PWR_DOWN_EVT);
207         sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, GED_MMIO_BASE);
208         /* sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, GED_MMIO_BASE_MEMHP); */
209         sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, GED_MMIO_BASE_REGS);
210         sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
211                            x86ms->gsi[GED_MMIO_IRQ]);
212         sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
213         x86ms->acpi_dev = HOTPLUG_HANDLER(dev);
214     }
215 
216     if (x86_machine_is_acpi_enabled(x86ms) && machine_usb(MACHINE(mms))) {
217         DeviceState *dev = qdev_new(TYPE_XHCI_SYSBUS);
218         qdev_prop_set_uint32(dev, "intrs", 1);
219         qdev_prop_set_uint32(dev, "slots", XHCI_MAXSLOTS);
220         qdev_prop_set_uint32(dev, "p2", 8);
221         qdev_prop_set_uint32(dev, "p3", 8);
222         sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
223         sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, MICROVM_XHCI_BASE);
224         sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
225                            x86ms->gsi[MICROVM_XHCI_IRQ]);
226     }
227 
228     if (x86_machine_is_acpi_enabled(x86ms) && mms->pcie == ON_OFF_AUTO_ON) {
229         /* use topmost 25% of the address space available */
230         hwaddr phys_size = (hwaddr)1 << X86_CPU(first_cpu)->phys_bits;
231         if (phys_size > 0x1000000ll) {
232             mms->gpex.mmio64.size = phys_size / 4;
233             mms->gpex.mmio64.base = phys_size - mms->gpex.mmio64.size;
234         }
235         mms->gpex.mmio32.base = PCIE_MMIO_BASE;
236         mms->gpex.mmio32.size = PCIE_MMIO_SIZE;
237         mms->gpex.ecam.base   = PCIE_ECAM_BASE;
238         mms->gpex.ecam.size   = PCIE_ECAM_SIZE;
239         mms->gpex.irq         = mms->pcie_irq_base;
240         create_gpex(mms);
241         x86ms->pci_irq_mask = ((1 << (mms->pcie_irq_base + 0)) |
242                                (1 << (mms->pcie_irq_base + 1)) |
243                                (1 << (mms->pcie_irq_base + 2)) |
244                                (1 << (mms->pcie_irq_base + 3)));
245     } else {
246         x86ms->pci_irq_mask = 0;
247     }
248 
249     if (mms->pic == ON_OFF_AUTO_ON || mms->pic == ON_OFF_AUTO_AUTO) {
250         qemu_irq *i8259;
251 
252         i8259 = i8259_init(isa_bus, x86_allocate_cpu_irq());
253         for (i = 0; i < ISA_NUM_IRQS; i++) {
254             gsi_state->i8259_irq[i] = i8259[i];
255         }
256         g_free(i8259);
257     }
258 
259     if (mms->pit == ON_OFF_AUTO_ON || mms->pit == ON_OFF_AUTO_AUTO) {
260         if (kvm_pit_in_kernel()) {
261             kvm_pit_init(isa_bus, 0x40);
262         } else {
263             i8254_pit_init(isa_bus, 0x40, 0, NULL);
264         }
265     }
266 
267     if (mms->rtc == ON_OFF_AUTO_ON ||
268         (mms->rtc == ON_OFF_AUTO_AUTO && !kvm_enabled())) {
269         rtc_state = mc146818_rtc_init(isa_bus, 2000, NULL);
270         microvm_set_rtc(mms, rtc_state);
271     }
272 
273     if (mms->isa_serial) {
274         serial_hds_isa_init(isa_bus, 0, 1);
275     }
276 
277     if (bios_name == NULL) {
278         bios_name = x86_machine_is_acpi_enabled(x86ms)
279             ? MICROVM_BIOS_FILENAME
280             : MICROVM_QBOOT_FILENAME;
281     }
282     x86_bios_rom_init(get_system_memory(), true);
283 }
284 
285 static void microvm_memory_init(MicrovmMachineState *mms)
286 {
287     MachineState *machine = MACHINE(mms);
288     X86MachineState *x86ms = X86_MACHINE(mms);
289     MemoryRegion *ram_below_4g, *ram_above_4g;
290     MemoryRegion *system_memory = get_system_memory();
291     FWCfgState *fw_cfg;
292     ram_addr_t lowmem = 0xc0000000; /* 3G */
293     int i;
294 
295     if (machine->ram_size > lowmem) {
296         x86ms->above_4g_mem_size = machine->ram_size - lowmem;
297         x86ms->below_4g_mem_size = lowmem;
298     } else {
299         x86ms->above_4g_mem_size = 0;
300         x86ms->below_4g_mem_size = machine->ram_size;
301     }
302 
303     ram_below_4g = g_malloc(sizeof(*ram_below_4g));
304     memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram,
305                              0, x86ms->below_4g_mem_size);
306     memory_region_add_subregion(system_memory, 0, ram_below_4g);
307 
308     e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM);
309 
310     if (x86ms->above_4g_mem_size > 0) {
311         ram_above_4g = g_malloc(sizeof(*ram_above_4g));
312         memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g",
313                                  machine->ram,
314                                  x86ms->below_4g_mem_size,
315                                  x86ms->above_4g_mem_size);
316         memory_region_add_subregion(system_memory, 0x100000000ULL,
317                                     ram_above_4g);
318         e820_add_entry(0x100000000ULL, x86ms->above_4g_mem_size, E820_RAM);
319     }
320 
321     fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4,
322                                 &address_space_memory);
323 
324     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, machine->smp.cpus);
325     fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, machine->smp.max_cpus);
326     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size);
327     fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1);
328     fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE,
329                      &e820_reserve, sizeof(e820_reserve));
330     fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
331                     sizeof(struct e820_entry) * e820_get_num_entries());
332 
333     rom_set_fw(fw_cfg);
334 
335     if (machine->kernel_filename != NULL) {
336         x86_load_linux(x86ms, fw_cfg, 0, true, true);
337     }
338 
339     if (mms->option_roms) {
340         for (i = 0; i < nb_option_roms; i++) {
341             rom_add_option(option_rom[i].name, option_rom[i].bootindex);
342         }
343     }
344 
345     x86ms->fw_cfg = fw_cfg;
346     x86ms->ioapic_as = &address_space_memory;
347 }
348 
349 static gchar *microvm_get_mmio_cmdline(gchar *name, uint32_t virtio_irq_base)
350 {
351     gchar *cmdline;
352     gchar *separator;
353     long int index;
354     int ret;
355 
356     separator = g_strrstr(name, ".");
357     if (!separator) {
358         return NULL;
359     }
360 
361     if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) {
362         return NULL;
363     }
364 
365     cmdline = g_malloc0(VIRTIO_CMDLINE_MAXLEN);
366     ret = g_snprintf(cmdline, VIRTIO_CMDLINE_MAXLEN,
367                      " virtio_mmio.device=512@0x%lx:%ld",
368                      VIRTIO_MMIO_BASE + index * 512,
369                      virtio_irq_base + index);
370     if (ret < 0 || ret >= VIRTIO_CMDLINE_MAXLEN) {
371         g_free(cmdline);
372         return NULL;
373     }
374 
375     return cmdline;
376 }
377 
378 static void microvm_fix_kernel_cmdline(MachineState *machine)
379 {
380     X86MachineState *x86ms = X86_MACHINE(machine);
381     MicrovmMachineState *mms = MICROVM_MACHINE(machine);
382     BusState *bus;
383     BusChild *kid;
384     char *cmdline;
385 
386     /*
387      * Find MMIO transports with attached devices, and add them to the kernel
388      * command line.
389      *
390      * Yes, this is a hack, but one that heavily improves the UX without
391      * introducing any significant issues.
392      */
393     cmdline = g_strdup(machine->kernel_cmdline);
394     bus = sysbus_get_default();
395     QTAILQ_FOREACH(kid, &bus->children, sibling) {
396         DeviceState *dev = kid->child;
397         ObjectClass *class = object_get_class(OBJECT(dev));
398 
399         if (class == object_class_by_name(TYPE_VIRTIO_MMIO)) {
400             VirtIOMMIOProxy *mmio = VIRTIO_MMIO(OBJECT(dev));
401             VirtioBusState *mmio_virtio_bus = &mmio->bus;
402             BusState *mmio_bus = &mmio_virtio_bus->parent_obj;
403 
404             if (!QTAILQ_EMPTY(&mmio_bus->children)) {
405                 gchar *mmio_cmdline = microvm_get_mmio_cmdline
406                     (mmio_bus->name, mms->virtio_irq_base);
407                 if (mmio_cmdline) {
408                     char *newcmd = g_strjoin(NULL, cmdline, mmio_cmdline, NULL);
409                     g_free(mmio_cmdline);
410                     g_free(cmdline);
411                     cmdline = newcmd;
412                 }
413             }
414         }
415     }
416 
417     fw_cfg_modify_i32(x86ms->fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(cmdline) + 1);
418     fw_cfg_modify_string(x86ms->fw_cfg, FW_CFG_CMDLINE_DATA, cmdline);
419 
420     g_free(cmdline);
421 }
422 
423 static void microvm_device_pre_plug_cb(HotplugHandler *hotplug_dev,
424                                        DeviceState *dev, Error **errp)
425 {
426     X86CPU *cpu = X86_CPU(dev);
427 
428     cpu->host_phys_bits = true; /* need reliable phys-bits */
429     x86_cpu_pre_plug(hotplug_dev, dev, errp);
430 }
431 
432 static void microvm_device_plug_cb(HotplugHandler *hotplug_dev,
433                                    DeviceState *dev, Error **errp)
434 {
435     x86_cpu_plug(hotplug_dev, dev, errp);
436 }
437 
438 static void microvm_device_unplug_request_cb(HotplugHandler *hotplug_dev,
439                                              DeviceState *dev, Error **errp)
440 {
441     error_setg(errp, "unplug not supported by microvm");
442 }
443 
444 static void microvm_device_unplug_cb(HotplugHandler *hotplug_dev,
445                                      DeviceState *dev, Error **errp)
446 {
447     error_setg(errp, "unplug not supported by microvm");
448 }
449 
450 static HotplugHandler *microvm_get_hotplug_handler(MachineState *machine,
451                                                    DeviceState *dev)
452 {
453     if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
454         return HOTPLUG_HANDLER(machine);
455     }
456     return NULL;
457 }
458 
459 static void microvm_machine_state_init(MachineState *machine)
460 {
461     MicrovmMachineState *mms = MICROVM_MACHINE(machine);
462     X86MachineState *x86ms = X86_MACHINE(machine);
463     Error *local_err = NULL;
464 
465     microvm_memory_init(mms);
466 
467     x86_cpus_init(x86ms, CPU_VERSION_LATEST);
468     if (local_err) {
469         error_report_err(local_err);
470         exit(1);
471     }
472 
473     microvm_devices_init(mms);
474 }
475 
476 static void microvm_machine_reset(MachineState *machine)
477 {
478     MicrovmMachineState *mms = MICROVM_MACHINE(machine);
479     CPUState *cs;
480     X86CPU *cpu;
481 
482     if (!x86_machine_is_acpi_enabled(X86_MACHINE(machine)) &&
483         machine->kernel_filename != NULL &&
484         mms->auto_kernel_cmdline && !mms->kernel_cmdline_fixed) {
485         microvm_fix_kernel_cmdline(machine);
486         mms->kernel_cmdline_fixed = true;
487     }
488 
489     qemu_devices_reset();
490 
491     CPU_FOREACH(cs) {
492         cpu = X86_CPU(cs);
493 
494         if (cpu->apic_state) {
495             device_legacy_reset(cpu->apic_state);
496         }
497     }
498 }
499 
500 static void microvm_machine_get_pic(Object *obj, Visitor *v, const char *name,
501                                     void *opaque, Error **errp)
502 {
503     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
504     OnOffAuto pic = mms->pic;
505 
506     visit_type_OnOffAuto(v, name, &pic, errp);
507 }
508 
509 static void microvm_machine_set_pic(Object *obj, Visitor *v, const char *name,
510                                     void *opaque, Error **errp)
511 {
512     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
513 
514     visit_type_OnOffAuto(v, name, &mms->pic, errp);
515 }
516 
517 static void microvm_machine_get_pit(Object *obj, Visitor *v, const char *name,
518                                     void *opaque, Error **errp)
519 {
520     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
521     OnOffAuto pit = mms->pit;
522 
523     visit_type_OnOffAuto(v, name, &pit, errp);
524 }
525 
526 static void microvm_machine_set_pit(Object *obj, Visitor *v, const char *name,
527                                     void *opaque, Error **errp)
528 {
529     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
530 
531     visit_type_OnOffAuto(v, name, &mms->pit, errp);
532 }
533 
534 static void microvm_machine_get_rtc(Object *obj, Visitor *v, const char *name,
535                                     void *opaque, Error **errp)
536 {
537     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
538     OnOffAuto rtc = mms->rtc;
539 
540     visit_type_OnOffAuto(v, name, &rtc, errp);
541 }
542 
543 static void microvm_machine_set_rtc(Object *obj, Visitor *v, const char *name,
544                                     void *opaque, Error **errp)
545 {
546     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
547 
548     visit_type_OnOffAuto(v, name, &mms->rtc, errp);
549 }
550 
551 static void microvm_machine_get_pcie(Object *obj, Visitor *v, const char *name,
552                                      void *opaque, Error **errp)
553 {
554     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
555     OnOffAuto pcie = mms->pcie;
556 
557     visit_type_OnOffAuto(v, name, &pcie, errp);
558 }
559 
560 static void microvm_machine_set_pcie(Object *obj, Visitor *v, const char *name,
561                                      void *opaque, Error **errp)
562 {
563     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
564 
565     visit_type_OnOffAuto(v, name, &mms->pcie, errp);
566 }
567 
568 static void microvm_machine_get_ioapic2(Object *obj, Visitor *v, const char *name,
569                                         void *opaque, Error **errp)
570 {
571     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
572     OnOffAuto ioapic2 = mms->ioapic2;
573 
574     visit_type_OnOffAuto(v, name, &ioapic2, errp);
575 }
576 
577 static void microvm_machine_set_ioapic2(Object *obj, Visitor *v, const char *name,
578                                         void *opaque, Error **errp)
579 {
580     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
581 
582     visit_type_OnOffAuto(v, name, &mms->ioapic2, errp);
583 }
584 
585 static bool microvm_machine_get_isa_serial(Object *obj, Error **errp)
586 {
587     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
588 
589     return mms->isa_serial;
590 }
591 
592 static void microvm_machine_set_isa_serial(Object *obj, bool value,
593                                            Error **errp)
594 {
595     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
596 
597     mms->isa_serial = value;
598 }
599 
600 static bool microvm_machine_get_option_roms(Object *obj, Error **errp)
601 {
602     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
603 
604     return mms->option_roms;
605 }
606 
607 static void microvm_machine_set_option_roms(Object *obj, bool value,
608                                             Error **errp)
609 {
610     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
611 
612     mms->option_roms = value;
613 }
614 
615 static bool microvm_machine_get_auto_kernel_cmdline(Object *obj, Error **errp)
616 {
617     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
618 
619     return mms->auto_kernel_cmdline;
620 }
621 
622 static void microvm_machine_set_auto_kernel_cmdline(Object *obj, bool value,
623                                                     Error **errp)
624 {
625     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
626 
627     mms->auto_kernel_cmdline = value;
628 }
629 
630 static void microvm_machine_done(Notifier *notifier, void *data)
631 {
632     MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState,
633                                             machine_done);
634 
635     acpi_setup_microvm(mms);
636 }
637 
638 static void microvm_powerdown_req(Notifier *notifier, void *data)
639 {
640     MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState,
641                                             powerdown_req);
642     X86MachineState *x86ms = X86_MACHINE(mms);
643 
644     if (x86ms->acpi_dev) {
645         Object *obj = OBJECT(x86ms->acpi_dev);
646         AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
647         adevc->send_event(ACPI_DEVICE_IF(x86ms->acpi_dev),
648                           ACPI_POWER_DOWN_STATUS);
649     }
650 }
651 
652 static void microvm_machine_initfn(Object *obj)
653 {
654     MicrovmMachineState *mms = MICROVM_MACHINE(obj);
655 
656     /* Configuration */
657     mms->pic = ON_OFF_AUTO_AUTO;
658     mms->pit = ON_OFF_AUTO_AUTO;
659     mms->rtc = ON_OFF_AUTO_AUTO;
660     mms->pcie = ON_OFF_AUTO_AUTO;
661     mms->ioapic2 = ON_OFF_AUTO_AUTO;
662     mms->isa_serial = true;
663     mms->option_roms = true;
664     mms->auto_kernel_cmdline = true;
665 
666     /* State */
667     mms->kernel_cmdline_fixed = false;
668 
669     mms->machine_done.notify = microvm_machine_done;
670     qemu_add_machine_init_done_notifier(&mms->machine_done);
671     mms->powerdown_req.notify = microvm_powerdown_req;
672     qemu_register_powerdown_notifier(&mms->powerdown_req);
673 }
674 
675 static void microvm_class_init(ObjectClass *oc, void *data)
676 {
677     MachineClass *mc = MACHINE_CLASS(oc);
678     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
679 
680     mc->init = microvm_machine_state_init;
681 
682     mc->family = "microvm_i386";
683     mc->desc = "microvm (i386)";
684     mc->units_per_default_bus = 1;
685     mc->no_floppy = 1;
686     mc->max_cpus = 288;
687     mc->has_hotpluggable_cpus = false;
688     mc->auto_enable_numa_with_memhp = false;
689     mc->auto_enable_numa_with_memdev = false;
690     mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
691     mc->nvdimm_supported = false;
692     mc->default_ram_id = "microvm.ram";
693 
694     /* Avoid relying too much on kernel components */
695     mc->default_kernel_irqchip_split = true;
696 
697     /* Machine class handlers */
698     mc->reset = microvm_machine_reset;
699 
700     /* hotplug (for cpu coldplug) */
701     mc->get_hotplug_handler = microvm_get_hotplug_handler;
702     hc->pre_plug = microvm_device_pre_plug_cb;
703     hc->plug = microvm_device_plug_cb;
704     hc->unplug_request = microvm_device_unplug_request_cb;
705     hc->unplug = microvm_device_unplug_cb;
706 
707     object_class_property_add(oc, MICROVM_MACHINE_PIC, "OnOffAuto",
708                               microvm_machine_get_pic,
709                               microvm_machine_set_pic,
710                               NULL, NULL);
711     object_class_property_set_description(oc, MICROVM_MACHINE_PIC,
712         "Enable i8259 PIC");
713 
714     object_class_property_add(oc, MICROVM_MACHINE_PIT, "OnOffAuto",
715                               microvm_machine_get_pit,
716                               microvm_machine_set_pit,
717                               NULL, NULL);
718     object_class_property_set_description(oc, MICROVM_MACHINE_PIT,
719         "Enable i8254 PIT");
720 
721     object_class_property_add(oc, MICROVM_MACHINE_RTC, "OnOffAuto",
722                               microvm_machine_get_rtc,
723                               microvm_machine_set_rtc,
724                               NULL, NULL);
725     object_class_property_set_description(oc, MICROVM_MACHINE_RTC,
726         "Enable MC146818 RTC");
727 
728     object_class_property_add(oc, MICROVM_MACHINE_PCIE, "OnOffAuto",
729                               microvm_machine_get_pcie,
730                               microvm_machine_set_pcie,
731                               NULL, NULL);
732     object_class_property_set_description(oc, MICROVM_MACHINE_PCIE,
733         "Enable PCIe");
734 
735     object_class_property_add(oc, MICROVM_MACHINE_IOAPIC2, "OnOffAuto",
736                               microvm_machine_get_ioapic2,
737                               microvm_machine_set_ioapic2,
738                               NULL, NULL);
739     object_class_property_set_description(oc, MICROVM_MACHINE_IOAPIC2,
740         "Enable second IO-APIC");
741 
742     object_class_property_add_bool(oc, MICROVM_MACHINE_ISA_SERIAL,
743                                    microvm_machine_get_isa_serial,
744                                    microvm_machine_set_isa_serial);
745     object_class_property_set_description(oc, MICROVM_MACHINE_ISA_SERIAL,
746         "Set off to disable the instantiation an ISA serial port");
747 
748     object_class_property_add_bool(oc, MICROVM_MACHINE_OPTION_ROMS,
749                                    microvm_machine_get_option_roms,
750                                    microvm_machine_set_option_roms);
751     object_class_property_set_description(oc, MICROVM_MACHINE_OPTION_ROMS,
752         "Set off to disable loading option ROMs");
753 
754     object_class_property_add_bool(oc, MICROVM_MACHINE_AUTO_KERNEL_CMDLINE,
755                                    microvm_machine_get_auto_kernel_cmdline,
756                                    microvm_machine_set_auto_kernel_cmdline);
757     object_class_property_set_description(oc,
758         MICROVM_MACHINE_AUTO_KERNEL_CMDLINE,
759         "Set off to disable adding virtio-mmio devices to the kernel cmdline");
760 
761     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
762 }
763 
764 static const TypeInfo microvm_machine_info = {
765     .name          = TYPE_MICROVM_MACHINE,
766     .parent        = TYPE_X86_MACHINE,
767     .instance_size = sizeof(MicrovmMachineState),
768     .instance_init = microvm_machine_initfn,
769     .class_size    = sizeof(MicrovmMachineClass),
770     .class_init    = microvm_class_init,
771     .interfaces = (InterfaceInfo[]) {
772          { TYPE_HOTPLUG_HANDLER },
773          { }
774     },
775 };
776 
777 static void microvm_machine_init(void)
778 {
779     type_register_static(&microvm_machine_info);
780 }
781 type_init(microvm_machine_init);
782