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