10ebf007dSSergio Lopez /* 20ebf007dSSergio Lopez * Copyright (c) 2018 Intel Corporation 30ebf007dSSergio Lopez * Copyright (c) 2019 Red Hat, Inc. 40ebf007dSSergio Lopez * 50ebf007dSSergio Lopez * This program is free software; you can redistribute it and/or modify it 60ebf007dSSergio Lopez * under the terms and conditions of the GNU General Public License, 70ebf007dSSergio Lopez * version 2 or later, as published by the Free Software Foundation. 80ebf007dSSergio Lopez * 90ebf007dSSergio Lopez * This program is distributed in the hope it will be useful, but WITHOUT 100ebf007dSSergio Lopez * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 110ebf007dSSergio Lopez * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 120ebf007dSSergio Lopez * more details. 130ebf007dSSergio Lopez * 140ebf007dSSergio Lopez * You should have received a copy of the GNU General Public License along with 150ebf007dSSergio Lopez * this program. If not, see <http://www.gnu.org/licenses/>. 160ebf007dSSergio Lopez */ 170ebf007dSSergio Lopez 180ebf007dSSergio Lopez #include "qemu/osdep.h" 190ebf007dSSergio Lopez #include "qemu/error-report.h" 200ebf007dSSergio Lopez #include "qemu/cutils.h" 210ebf007dSSergio Lopez #include "qemu/units.h" 220ebf007dSSergio Lopez #include "qapi/error.h" 230ebf007dSSergio Lopez #include "qapi/visitor.h" 240ebf007dSSergio Lopez #include "qapi/qapi-visit-common.h" 250ebf007dSSergio Lopez #include "sysemu/sysemu.h" 260ebf007dSSergio Lopez #include "sysemu/cpus.h" 270ebf007dSSergio Lopez #include "sysemu/numa.h" 280ebf007dSSergio Lopez #include "sysemu/reset.h" 298045df14SGerd Hoffmann #include "sysemu/runstate.h" 308045df14SGerd Hoffmann #include "acpi-microvm.h" 310ebf007dSSergio Lopez 320ebf007dSSergio Lopez #include "hw/loader.h" 330ebf007dSSergio Lopez #include "hw/irq.h" 340ebf007dSSergio Lopez #include "hw/kvm/clock.h" 350ebf007dSSergio Lopez #include "hw/i386/microvm.h" 360ebf007dSSergio Lopez #include "hw/i386/x86.h" 370ebf007dSSergio Lopez #include "target/i386/cpu.h" 38852c27e2SPaolo Bonzini #include "hw/intc/i8259.h" 390ebf007dSSergio Lopez #include "hw/timer/i8254.h" 40673652a7SPaolo Bonzini #include "hw/rtc/mc146818rtc.h" 410ebf007dSSergio Lopez #include "hw/char/serial.h" 4263bcfe7bSGerd Hoffmann #include "hw/display/ramfb.h" 430ebf007dSSergio Lopez #include "hw/i386/topology.h" 440ebf007dSSergio Lopez #include "hw/i386/e820_memory_layout.h" 450ebf007dSSergio Lopez #include "hw/i386/fw_cfg.h" 460ebf007dSSergio Lopez #include "hw/virtio/virtio-mmio.h" 478045df14SGerd Hoffmann #include "hw/acpi/acpi.h" 488045df14SGerd Hoffmann #include "hw/acpi/generic_event_device.h" 4924db877aSGerd Hoffmann #include "hw/pci-host/gpex.h" 50d4a42e85SGerd Hoffmann #include "hw/usb/xhci.h" 510ebf007dSSergio Lopez 520ebf007dSSergio Lopez #include "cpu.h" 530ebf007dSSergio Lopez #include "elf.h" 54*a9dc68d9SClaudio Fontana #include "kvm/kvm_i386.h" 550ebf007dSSergio Lopez #include "hw/xen/start_info.h" 560ebf007dSSergio Lopez 573bee1d1dSGerd Hoffmann #define MICROVM_QBOOT_FILENAME "qboot.rom" 5867eb6a40SGerd Hoffmann #define MICROVM_BIOS_FILENAME "bios-microvm.bin" 590ebf007dSSergio Lopez 600ebf007dSSergio Lopez static void microvm_set_rtc(MicrovmMachineState *mms, ISADevice *s) 610ebf007dSSergio Lopez { 620ebf007dSSergio Lopez X86MachineState *x86ms = X86_MACHINE(mms); 630ebf007dSSergio Lopez int val; 640ebf007dSSergio Lopez 650ebf007dSSergio Lopez val = MIN(x86ms->below_4g_mem_size / KiB, 640); 660ebf007dSSergio Lopez rtc_set_memory(s, 0x15, val); 670ebf007dSSergio Lopez rtc_set_memory(s, 0x16, val >> 8); 680ebf007dSSergio Lopez /* extended memory (next 64MiB) */ 690ebf007dSSergio Lopez if (x86ms->below_4g_mem_size > 1 * MiB) { 700ebf007dSSergio Lopez val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB; 710ebf007dSSergio Lopez } else { 720ebf007dSSergio Lopez val = 0; 730ebf007dSSergio Lopez } 740ebf007dSSergio Lopez if (val > 65535) { 750ebf007dSSergio Lopez val = 65535; 760ebf007dSSergio Lopez } 770ebf007dSSergio Lopez rtc_set_memory(s, 0x17, val); 780ebf007dSSergio Lopez rtc_set_memory(s, 0x18, val >> 8); 790ebf007dSSergio Lopez rtc_set_memory(s, 0x30, val); 800ebf007dSSergio Lopez rtc_set_memory(s, 0x31, val >> 8); 810ebf007dSSergio Lopez /* memory between 16MiB and 4GiB */ 820ebf007dSSergio Lopez if (x86ms->below_4g_mem_size > 16 * MiB) { 830ebf007dSSergio Lopez val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB); 840ebf007dSSergio Lopez } else { 850ebf007dSSergio Lopez val = 0; 860ebf007dSSergio Lopez } 870ebf007dSSergio Lopez if (val > 65535) { 880ebf007dSSergio Lopez val = 65535; 890ebf007dSSergio Lopez } 900ebf007dSSergio Lopez rtc_set_memory(s, 0x34, val); 910ebf007dSSergio Lopez rtc_set_memory(s, 0x35, val >> 8); 920ebf007dSSergio Lopez /* memory above 4GiB */ 930ebf007dSSergio Lopez val = x86ms->above_4g_mem_size / 65536; 940ebf007dSSergio Lopez rtc_set_memory(s, 0x5b, val); 950ebf007dSSergio Lopez rtc_set_memory(s, 0x5c, val >> 8); 960ebf007dSSergio Lopez rtc_set_memory(s, 0x5d, val >> 16); 970ebf007dSSergio Lopez } 980ebf007dSSergio Lopez 9924db877aSGerd Hoffmann static void create_gpex(MicrovmMachineState *mms) 10024db877aSGerd Hoffmann { 10124db877aSGerd Hoffmann X86MachineState *x86ms = X86_MACHINE(mms); 10224db877aSGerd Hoffmann MemoryRegion *mmio32_alias; 10324db877aSGerd Hoffmann MemoryRegion *mmio64_alias; 10424db877aSGerd Hoffmann MemoryRegion *mmio_reg; 10524db877aSGerd Hoffmann MemoryRegion *ecam_alias; 10624db877aSGerd Hoffmann MemoryRegion *ecam_reg; 10724db877aSGerd Hoffmann DeviceState *dev; 10824db877aSGerd Hoffmann int i; 10924db877aSGerd Hoffmann 11024db877aSGerd Hoffmann dev = qdev_new(TYPE_GPEX_HOST); 11124db877aSGerd Hoffmann sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 11224db877aSGerd Hoffmann 11324db877aSGerd Hoffmann /* Map only the first size_ecam bytes of ECAM space */ 11424db877aSGerd Hoffmann ecam_alias = g_new0(MemoryRegion, 1); 11524db877aSGerd Hoffmann ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 11624db877aSGerd Hoffmann memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam", 11724db877aSGerd Hoffmann ecam_reg, 0, mms->gpex.ecam.size); 11824db877aSGerd Hoffmann memory_region_add_subregion(get_system_memory(), 11924db877aSGerd Hoffmann mms->gpex.ecam.base, ecam_alias); 12024db877aSGerd Hoffmann 12124db877aSGerd Hoffmann /* Map the MMIO window into system address space so as to expose 12224db877aSGerd Hoffmann * the section of PCI MMIO space which starts at the same base address 12324db877aSGerd Hoffmann * (ie 1:1 mapping for that part of PCI MMIO space visible through 12424db877aSGerd Hoffmann * the window). 12524db877aSGerd Hoffmann */ 12624db877aSGerd Hoffmann mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 12724db877aSGerd Hoffmann if (mms->gpex.mmio32.size) { 12824db877aSGerd Hoffmann mmio32_alias = g_new0(MemoryRegion, 1); 12924db877aSGerd Hoffmann memory_region_init_alias(mmio32_alias, OBJECT(dev), "pcie-mmio32", mmio_reg, 13024db877aSGerd Hoffmann mms->gpex.mmio32.base, mms->gpex.mmio32.size); 13124db877aSGerd Hoffmann memory_region_add_subregion(get_system_memory(), 13224db877aSGerd Hoffmann mms->gpex.mmio32.base, mmio32_alias); 13324db877aSGerd Hoffmann } 13424db877aSGerd Hoffmann if (mms->gpex.mmio64.size) { 13524db877aSGerd Hoffmann mmio64_alias = g_new0(MemoryRegion, 1); 13624db877aSGerd Hoffmann memory_region_init_alias(mmio64_alias, OBJECT(dev), "pcie-mmio64", mmio_reg, 13724db877aSGerd Hoffmann mms->gpex.mmio64.base, mms->gpex.mmio64.size); 13824db877aSGerd Hoffmann memory_region_add_subregion(get_system_memory(), 13924db877aSGerd Hoffmann mms->gpex.mmio64.base, mmio64_alias); 14024db877aSGerd Hoffmann } 14124db877aSGerd Hoffmann 14224db877aSGerd Hoffmann for (i = 0; i < GPEX_NUM_IRQS; i++) { 14324db877aSGerd Hoffmann sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 14424db877aSGerd Hoffmann x86ms->gsi[mms->gpex.irq + i]); 14524db877aSGerd Hoffmann } 14624db877aSGerd Hoffmann } 14724db877aSGerd Hoffmann 1484d01b899SGerd Hoffmann static int microvm_ioapics(MicrovmMachineState *mms) 1494d01b899SGerd Hoffmann { 1504d01b899SGerd Hoffmann if (!x86_machine_is_acpi_enabled(X86_MACHINE(mms))) { 1514d01b899SGerd Hoffmann return 1; 1524d01b899SGerd Hoffmann } 1534d01b899SGerd Hoffmann if (mms->ioapic2 == ON_OFF_AUTO_OFF) { 1544d01b899SGerd Hoffmann return 1; 1554d01b899SGerd Hoffmann } 1564d01b899SGerd Hoffmann return 2; 1574d01b899SGerd Hoffmann } 1584d01b899SGerd Hoffmann 1590ebf007dSSergio Lopez static void microvm_devices_init(MicrovmMachineState *mms) 1600ebf007dSSergio Lopez { 1617d435078SPaolo Bonzini const char *default_firmware; 1620ebf007dSSergio Lopez X86MachineState *x86ms = X86_MACHINE(mms); 1630ebf007dSSergio Lopez ISABus *isa_bus; 1640ebf007dSSergio Lopez ISADevice *rtc_state; 1650ebf007dSSergio Lopez GSIState *gsi_state; 1664d01b899SGerd Hoffmann int ioapics; 1670ebf007dSSergio Lopez int i; 1680ebf007dSSergio Lopez 1690ebf007dSSergio Lopez /* Core components */ 1704d01b899SGerd Hoffmann ioapics = microvm_ioapics(mms); 1710ebf007dSSergio Lopez gsi_state = g_malloc0(sizeof(*gsi_state)); 1724d01b899SGerd Hoffmann x86ms->gsi = qemu_allocate_irqs(gsi_handler, gsi_state, 1734d01b899SGerd Hoffmann IOAPIC_NUM_PINS * ioapics); 1740ebf007dSSergio Lopez 1750ebf007dSSergio Lopez isa_bus = isa_bus_new(NULL, get_system_memory(), get_system_io(), 1760ebf007dSSergio Lopez &error_abort); 1770ebf007dSSergio Lopez isa_bus_irqs(isa_bus, x86ms->gsi); 1780ebf007dSSergio Lopez 1790ebf007dSSergio Lopez ioapic_init_gsi(gsi_state, "machine"); 1804d01b899SGerd Hoffmann if (ioapics > 1) { 1814d01b899SGerd Hoffmann x86ms->ioapic2 = ioapic_init_secondary(gsi_state); 1824d01b899SGerd Hoffmann } 1830ebf007dSSergio Lopez 1848700a984SVitaly Kuznetsov kvmclock_create(true); 1850ebf007dSSergio Lopez 186c214a7bcSGerd Hoffmann mms->virtio_irq_base = 5; 187c214a7bcSGerd Hoffmann mms->virtio_num_transports = 8; 1884d01b899SGerd Hoffmann if (x86ms->ioapic2) { 1894d01b899SGerd Hoffmann mms->pcie_irq_base = 16; /* 16 -> 19 */ 1904d01b899SGerd Hoffmann /* use second ioapic (24 -> 47) for virtio-mmio irq lines */ 1914d01b899SGerd Hoffmann mms->virtio_irq_base = IO_APIC_SECONDARY_IRQBASE; 1924d01b899SGerd Hoffmann mms->virtio_num_transports = IOAPIC_NUM_PINS; 1934d01b899SGerd Hoffmann } else if (x86_machine_is_acpi_enabled(x86ms)) { 1944d01b899SGerd Hoffmann mms->pcie_irq_base = 12; /* 12 -> 15 */ 1954d01b899SGerd Hoffmann mms->virtio_irq_base = 16; /* 16 -> 23 */ 196c214a7bcSGerd Hoffmann } 197c214a7bcSGerd Hoffmann 198c214a7bcSGerd Hoffmann for (i = 0; i < mms->virtio_num_transports; i++) { 1990ebf007dSSergio Lopez sysbus_create_simple("virtio-mmio", 2000ebf007dSSergio Lopez VIRTIO_MMIO_BASE + i * 512, 201d4e9d577SGerd Hoffmann x86ms->gsi[mms->virtio_irq_base + i]); 2020ebf007dSSergio Lopez } 2030ebf007dSSergio Lopez 2040ebf007dSSergio Lopez /* Optional and legacy devices */ 2058045df14SGerd Hoffmann if (x86_machine_is_acpi_enabled(x86ms)) { 2068045df14SGerd Hoffmann DeviceState *dev = qdev_new(TYPE_ACPI_GED_X86); 2078045df14SGerd Hoffmann qdev_prop_set_uint32(dev, "ged-event", ACPI_GED_PWR_DOWN_EVT); 2088045df14SGerd Hoffmann sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, GED_MMIO_BASE); 2098045df14SGerd Hoffmann /* sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, GED_MMIO_BASE_MEMHP); */ 2108045df14SGerd Hoffmann sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, GED_MMIO_BASE_REGS); 2118045df14SGerd Hoffmann sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, 2128045df14SGerd Hoffmann x86ms->gsi[GED_MMIO_IRQ]); 2138045df14SGerd Hoffmann sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal); 21450aef131SGerd Hoffmann x86ms->acpi_dev = HOTPLUG_HANDLER(dev); 2158045df14SGerd Hoffmann } 2160ebf007dSSergio Lopez 217d4a42e85SGerd Hoffmann if (x86_machine_is_acpi_enabled(x86ms) && machine_usb(MACHINE(mms))) { 218d4a42e85SGerd Hoffmann DeviceState *dev = qdev_new(TYPE_XHCI_SYSBUS); 219d4a42e85SGerd Hoffmann qdev_prop_set_uint32(dev, "intrs", 1); 220d4a42e85SGerd Hoffmann qdev_prop_set_uint32(dev, "slots", XHCI_MAXSLOTS); 221d4a42e85SGerd Hoffmann qdev_prop_set_uint32(dev, "p2", 8); 222d4a42e85SGerd Hoffmann qdev_prop_set_uint32(dev, "p3", 8); 223d4a42e85SGerd Hoffmann sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal); 224d4a42e85SGerd Hoffmann sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, MICROVM_XHCI_BASE); 225d4a42e85SGerd Hoffmann sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, 226d4a42e85SGerd Hoffmann x86ms->gsi[MICROVM_XHCI_IRQ]); 227d4a42e85SGerd Hoffmann } 228d4a42e85SGerd Hoffmann 22924db877aSGerd Hoffmann if (x86_machine_is_acpi_enabled(x86ms) && mms->pcie == ON_OFF_AUTO_ON) { 2308c2d9f9aSGerd Hoffmann /* use topmost 25% of the address space available */ 2318c2d9f9aSGerd Hoffmann hwaddr phys_size = (hwaddr)1 << X86_CPU(first_cpu)->phys_bits; 2328c2d9f9aSGerd Hoffmann if (phys_size > 0x1000000ll) { 2338c2d9f9aSGerd Hoffmann mms->gpex.mmio64.size = phys_size / 4; 2348c2d9f9aSGerd Hoffmann mms->gpex.mmio64.base = phys_size - mms->gpex.mmio64.size; 2358c2d9f9aSGerd Hoffmann } 23624db877aSGerd Hoffmann mms->gpex.mmio32.base = PCIE_MMIO_BASE; 23724db877aSGerd Hoffmann mms->gpex.mmio32.size = PCIE_MMIO_SIZE; 23824db877aSGerd Hoffmann mms->gpex.ecam.base = PCIE_ECAM_BASE; 23924db877aSGerd Hoffmann mms->gpex.ecam.size = PCIE_ECAM_SIZE; 2403d09c007SGerd Hoffmann mms->gpex.irq = mms->pcie_irq_base; 24124db877aSGerd Hoffmann create_gpex(mms); 2423d09c007SGerd Hoffmann x86ms->pci_irq_mask = ((1 << (mms->pcie_irq_base + 0)) | 2433d09c007SGerd Hoffmann (1 << (mms->pcie_irq_base + 1)) | 2443d09c007SGerd Hoffmann (1 << (mms->pcie_irq_base + 2)) | 2453d09c007SGerd Hoffmann (1 << (mms->pcie_irq_base + 3))); 24664b070daSGerd Hoffmann } else { 24764b070daSGerd Hoffmann x86ms->pci_irq_mask = 0; 24824db877aSGerd Hoffmann } 24924db877aSGerd Hoffmann 2500ebf007dSSergio Lopez if (mms->pic == ON_OFF_AUTO_ON || mms->pic == ON_OFF_AUTO_AUTO) { 2510ebf007dSSergio Lopez qemu_irq *i8259; 2520ebf007dSSergio Lopez 25389a289c7SPaolo Bonzini i8259 = i8259_init(isa_bus, x86_allocate_cpu_irq()); 2540ebf007dSSergio Lopez for (i = 0; i < ISA_NUM_IRQS; i++) { 2550ebf007dSSergio Lopez gsi_state->i8259_irq[i] = i8259[i]; 2560ebf007dSSergio Lopez } 2570ebf007dSSergio Lopez g_free(i8259); 2580ebf007dSSergio Lopez } 2590ebf007dSSergio Lopez 2600ebf007dSSergio Lopez if (mms->pit == ON_OFF_AUTO_ON || mms->pit == ON_OFF_AUTO_AUTO) { 2610ebf007dSSergio Lopez if (kvm_pit_in_kernel()) { 2620ebf007dSSergio Lopez kvm_pit_init(isa_bus, 0x40); 2630ebf007dSSergio Lopez } else { 2640ebf007dSSergio Lopez i8254_pit_init(isa_bus, 0x40, 0, NULL); 2650ebf007dSSergio Lopez } 2660ebf007dSSergio Lopez } 2670ebf007dSSergio Lopez 2680ebf007dSSergio Lopez if (mms->rtc == ON_OFF_AUTO_ON || 2690ebf007dSSergio Lopez (mms->rtc == ON_OFF_AUTO_AUTO && !kvm_enabled())) { 2700ebf007dSSergio Lopez rtc_state = mc146818_rtc_init(isa_bus, 2000, NULL); 2710ebf007dSSergio Lopez microvm_set_rtc(mms, rtc_state); 2720ebf007dSSergio Lopez } 2730ebf007dSSergio Lopez 2740ebf007dSSergio Lopez if (mms->isa_serial) { 2750ebf007dSSergio Lopez serial_hds_isa_init(isa_bus, 0, 1); 2760ebf007dSSergio Lopez } 2770ebf007dSSergio Lopez 2787d435078SPaolo Bonzini default_firmware = x86_machine_is_acpi_enabled(x86ms) 27967eb6a40SGerd Hoffmann ? MICROVM_BIOS_FILENAME 28067eb6a40SGerd Hoffmann : MICROVM_QBOOT_FILENAME; 2817d435078SPaolo Bonzini x86_bios_rom_init(MACHINE(mms), default_firmware, get_system_memory(), true); 2820ebf007dSSergio Lopez } 2830ebf007dSSergio Lopez 2840ebf007dSSergio Lopez static void microvm_memory_init(MicrovmMachineState *mms) 2850ebf007dSSergio Lopez { 2860ebf007dSSergio Lopez MachineState *machine = MACHINE(mms); 2870ebf007dSSergio Lopez X86MachineState *x86ms = X86_MACHINE(mms); 2889ad54686SIgor Mammedov MemoryRegion *ram_below_4g, *ram_above_4g; 2890ebf007dSSergio Lopez MemoryRegion *system_memory = get_system_memory(); 2900ebf007dSSergio Lopez FWCfgState *fw_cfg; 291e289655cSGerd Hoffmann ram_addr_t lowmem = 0xc0000000; /* 3G */ 2920ebf007dSSergio Lopez int i; 2930ebf007dSSergio Lopez 2940ebf007dSSergio Lopez if (machine->ram_size > lowmem) { 2950ebf007dSSergio Lopez x86ms->above_4g_mem_size = machine->ram_size - lowmem; 2960ebf007dSSergio Lopez x86ms->below_4g_mem_size = lowmem; 2970ebf007dSSergio Lopez } else { 2980ebf007dSSergio Lopez x86ms->above_4g_mem_size = 0; 2990ebf007dSSergio Lopez x86ms->below_4g_mem_size = machine->ram_size; 3000ebf007dSSergio Lopez } 3010ebf007dSSergio Lopez 3020ebf007dSSergio Lopez ram_below_4g = g_malloc(sizeof(*ram_below_4g)); 3039ad54686SIgor Mammedov memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram, 3040ebf007dSSergio Lopez 0, x86ms->below_4g_mem_size); 3050ebf007dSSergio Lopez memory_region_add_subregion(system_memory, 0, ram_below_4g); 3060ebf007dSSergio Lopez 3070ebf007dSSergio Lopez e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM); 3080ebf007dSSergio Lopez 3090ebf007dSSergio Lopez if (x86ms->above_4g_mem_size > 0) { 3100ebf007dSSergio Lopez ram_above_4g = g_malloc(sizeof(*ram_above_4g)); 3119ad54686SIgor Mammedov memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", 3129ad54686SIgor Mammedov machine->ram, 3130ebf007dSSergio Lopez x86ms->below_4g_mem_size, 3140ebf007dSSergio Lopez x86ms->above_4g_mem_size); 3150ebf007dSSergio Lopez memory_region_add_subregion(system_memory, 0x100000000ULL, 3160ebf007dSSergio Lopez ram_above_4g); 3170ebf007dSSergio Lopez e820_add_entry(0x100000000ULL, x86ms->above_4g_mem_size, E820_RAM); 3180ebf007dSSergio Lopez } 3190ebf007dSSergio Lopez 3200ebf007dSSergio Lopez fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4, 3210ebf007dSSergio Lopez &address_space_memory); 3220ebf007dSSergio Lopez 3230ebf007dSSergio Lopez fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, machine->smp.cpus); 3240ebf007dSSergio Lopez fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, machine->smp.max_cpus); 3250ebf007dSSergio Lopez fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size); 326eafa0868SEduardo Habkost fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1); 3270ebf007dSSergio Lopez fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE, 3280ebf007dSSergio Lopez &e820_reserve, sizeof(e820_reserve)); 3290ebf007dSSergio Lopez fw_cfg_add_file(fw_cfg, "etc/e820", e820_table, 3300ebf007dSSergio Lopez sizeof(struct e820_entry) * e820_get_num_entries()); 3310ebf007dSSergio Lopez 3320ebf007dSSergio Lopez rom_set_fw(fw_cfg); 3330ebf007dSSergio Lopez 3340ebf007dSSergio Lopez if (machine->kernel_filename != NULL) { 3350ebf007dSSergio Lopez x86_load_linux(x86ms, fw_cfg, 0, true, true); 3360ebf007dSSergio Lopez } 3370ebf007dSSergio Lopez 3380ebf007dSSergio Lopez if (mms->option_roms) { 3390ebf007dSSergio Lopez for (i = 0; i < nb_option_roms; i++) { 3400ebf007dSSergio Lopez rom_add_option(option_rom[i].name, option_rom[i].bootindex); 3410ebf007dSSergio Lopez } 3420ebf007dSSergio Lopez } 3430ebf007dSSergio Lopez 3440ebf007dSSergio Lopez x86ms->fw_cfg = fw_cfg; 3450ebf007dSSergio Lopez x86ms->ioapic_as = &address_space_memory; 3460ebf007dSSergio Lopez } 3470ebf007dSSergio Lopez 348d4e9d577SGerd Hoffmann static gchar *microvm_get_mmio_cmdline(gchar *name, uint32_t virtio_irq_base) 3490ebf007dSSergio Lopez { 3500ebf007dSSergio Lopez gchar *cmdline; 3510ebf007dSSergio Lopez gchar *separator; 3520ebf007dSSergio Lopez long int index; 3530ebf007dSSergio Lopez int ret; 3540ebf007dSSergio Lopez 3550ebf007dSSergio Lopez separator = g_strrstr(name, "."); 3560ebf007dSSergio Lopez if (!separator) { 3570ebf007dSSergio Lopez return NULL; 3580ebf007dSSergio Lopez } 3590ebf007dSSergio Lopez 3600ebf007dSSergio Lopez if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) { 3610ebf007dSSergio Lopez return NULL; 3620ebf007dSSergio Lopez } 3630ebf007dSSergio Lopez 3640ebf007dSSergio Lopez cmdline = g_malloc0(VIRTIO_CMDLINE_MAXLEN); 3650ebf007dSSergio Lopez ret = g_snprintf(cmdline, VIRTIO_CMDLINE_MAXLEN, 3660ebf007dSSergio Lopez " virtio_mmio.device=512@0x%lx:%ld", 3670ebf007dSSergio Lopez VIRTIO_MMIO_BASE + index * 512, 368d4e9d577SGerd Hoffmann virtio_irq_base + index); 3690ebf007dSSergio Lopez if (ret < 0 || ret >= VIRTIO_CMDLINE_MAXLEN) { 3700ebf007dSSergio Lopez g_free(cmdline); 3710ebf007dSSergio Lopez return NULL; 3720ebf007dSSergio Lopez } 3730ebf007dSSergio Lopez 3740ebf007dSSergio Lopez return cmdline; 3750ebf007dSSergio Lopez } 3760ebf007dSSergio Lopez 3770ebf007dSSergio Lopez static void microvm_fix_kernel_cmdline(MachineState *machine) 3780ebf007dSSergio Lopez { 3790ebf007dSSergio Lopez X86MachineState *x86ms = X86_MACHINE(machine); 380d4e9d577SGerd Hoffmann MicrovmMachineState *mms = MICROVM_MACHINE(machine); 3810ebf007dSSergio Lopez BusState *bus; 3820ebf007dSSergio Lopez BusChild *kid; 3830ebf007dSSergio Lopez char *cmdline; 3840ebf007dSSergio Lopez 3850ebf007dSSergio Lopez /* 3860ebf007dSSergio Lopez * Find MMIO transports with attached devices, and add them to the kernel 3870ebf007dSSergio Lopez * command line. 3880ebf007dSSergio Lopez * 3890ebf007dSSergio Lopez * Yes, this is a hack, but one that heavily improves the UX without 3900ebf007dSSergio Lopez * introducing any significant issues. 3910ebf007dSSergio Lopez */ 3920ebf007dSSergio Lopez cmdline = g_strdup(machine->kernel_cmdline); 3930ebf007dSSergio Lopez bus = sysbus_get_default(); 3940ebf007dSSergio Lopez QTAILQ_FOREACH(kid, &bus->children, sibling) { 3950ebf007dSSergio Lopez DeviceState *dev = kid->child; 3960ebf007dSSergio Lopez ObjectClass *class = object_get_class(OBJECT(dev)); 3970ebf007dSSergio Lopez 3980ebf007dSSergio Lopez if (class == object_class_by_name(TYPE_VIRTIO_MMIO)) { 3990ebf007dSSergio Lopez VirtIOMMIOProxy *mmio = VIRTIO_MMIO(OBJECT(dev)); 4000ebf007dSSergio Lopez VirtioBusState *mmio_virtio_bus = &mmio->bus; 4010ebf007dSSergio Lopez BusState *mmio_bus = &mmio_virtio_bus->parent_obj; 4020ebf007dSSergio Lopez 4030ebf007dSSergio Lopez if (!QTAILQ_EMPTY(&mmio_bus->children)) { 404d4e9d577SGerd Hoffmann gchar *mmio_cmdline = microvm_get_mmio_cmdline 405d4e9d577SGerd Hoffmann (mmio_bus->name, mms->virtio_irq_base); 4060ebf007dSSergio Lopez if (mmio_cmdline) { 4070ebf007dSSergio Lopez char *newcmd = g_strjoin(NULL, cmdline, mmio_cmdline, NULL); 4080ebf007dSSergio Lopez g_free(mmio_cmdline); 4090ebf007dSSergio Lopez g_free(cmdline); 4100ebf007dSSergio Lopez cmdline = newcmd; 4110ebf007dSSergio Lopez } 4120ebf007dSSergio Lopez } 4130ebf007dSSergio Lopez } 4140ebf007dSSergio Lopez } 4150ebf007dSSergio Lopez 4160ebf007dSSergio Lopez fw_cfg_modify_i32(x86ms->fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(cmdline) + 1); 4170ebf007dSSergio Lopez fw_cfg_modify_string(x86ms->fw_cfg, FW_CFG_CMDLINE_DATA, cmdline); 418c3157b74SSergio Lopez 419c3157b74SSergio Lopez g_free(cmdline); 4200ebf007dSSergio Lopez } 4210ebf007dSSergio Lopez 422e3ab9873SGerd Hoffmann static void microvm_device_pre_plug_cb(HotplugHandler *hotplug_dev, 423e3ab9873SGerd Hoffmann DeviceState *dev, Error **errp) 424e3ab9873SGerd Hoffmann { 4258c2d9f9aSGerd Hoffmann X86CPU *cpu = X86_CPU(dev); 4268c2d9f9aSGerd Hoffmann 4278c2d9f9aSGerd Hoffmann cpu->host_phys_bits = true; /* need reliable phys-bits */ 428e3ab9873SGerd Hoffmann x86_cpu_pre_plug(hotplug_dev, dev, errp); 429e3ab9873SGerd Hoffmann } 430e3ab9873SGerd Hoffmann 431e3ab9873SGerd Hoffmann static void microvm_device_plug_cb(HotplugHandler *hotplug_dev, 432e3ab9873SGerd Hoffmann DeviceState *dev, Error **errp) 433e3ab9873SGerd Hoffmann { 434e3ab9873SGerd Hoffmann x86_cpu_plug(hotplug_dev, dev, errp); 435e3ab9873SGerd Hoffmann } 436e3ab9873SGerd Hoffmann 437e3ab9873SGerd Hoffmann static void microvm_device_unplug_request_cb(HotplugHandler *hotplug_dev, 438e3ab9873SGerd Hoffmann DeviceState *dev, Error **errp) 439e3ab9873SGerd Hoffmann { 440e3ab9873SGerd Hoffmann error_setg(errp, "unplug not supported by microvm"); 441e3ab9873SGerd Hoffmann } 442e3ab9873SGerd Hoffmann 443e3ab9873SGerd Hoffmann static void microvm_device_unplug_cb(HotplugHandler *hotplug_dev, 444e3ab9873SGerd Hoffmann DeviceState *dev, Error **errp) 445e3ab9873SGerd Hoffmann { 446e3ab9873SGerd Hoffmann error_setg(errp, "unplug not supported by microvm"); 447e3ab9873SGerd Hoffmann } 448e3ab9873SGerd Hoffmann 449e3ab9873SGerd Hoffmann static HotplugHandler *microvm_get_hotplug_handler(MachineState *machine, 450e3ab9873SGerd Hoffmann DeviceState *dev) 451e3ab9873SGerd Hoffmann { 452e3ab9873SGerd Hoffmann if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 453e3ab9873SGerd Hoffmann return HOTPLUG_HANDLER(machine); 454e3ab9873SGerd Hoffmann } 455e3ab9873SGerd Hoffmann return NULL; 456e3ab9873SGerd Hoffmann } 457e3ab9873SGerd Hoffmann 4580ebf007dSSergio Lopez static void microvm_machine_state_init(MachineState *machine) 4590ebf007dSSergio Lopez { 4600ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(machine); 4610ebf007dSSergio Lopez X86MachineState *x86ms = X86_MACHINE(machine); 4620ebf007dSSergio Lopez Error *local_err = NULL; 4630ebf007dSSergio Lopez 4640ebf007dSSergio Lopez microvm_memory_init(mms); 4650ebf007dSSergio Lopez 4660ebf007dSSergio Lopez x86_cpus_init(x86ms, CPU_VERSION_LATEST); 4670ebf007dSSergio Lopez if (local_err) { 4680ebf007dSSergio Lopez error_report_err(local_err); 4690ebf007dSSergio Lopez exit(1); 4700ebf007dSSergio Lopez } 4710ebf007dSSergio Lopez 4720ebf007dSSergio Lopez microvm_devices_init(mms); 4730ebf007dSSergio Lopez } 4740ebf007dSSergio Lopez 4750ebf007dSSergio Lopez static void microvm_machine_reset(MachineState *machine) 4760ebf007dSSergio Lopez { 4770ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(machine); 4780ebf007dSSergio Lopez CPUState *cs; 4790ebf007dSSergio Lopez X86CPU *cpu; 4800ebf007dSSergio Lopez 481f6f7e2d8SGerd Hoffmann if (!x86_machine_is_acpi_enabled(X86_MACHINE(machine)) && 482f6f7e2d8SGerd Hoffmann machine->kernel_filename != NULL && 4830ebf007dSSergio Lopez mms->auto_kernel_cmdline && !mms->kernel_cmdline_fixed) { 4840ebf007dSSergio Lopez microvm_fix_kernel_cmdline(machine); 4850ebf007dSSergio Lopez mms->kernel_cmdline_fixed = true; 4860ebf007dSSergio Lopez } 4870ebf007dSSergio Lopez 4880ebf007dSSergio Lopez qemu_devices_reset(); 4890ebf007dSSergio Lopez 4900ebf007dSSergio Lopez CPU_FOREACH(cs) { 4910ebf007dSSergio Lopez cpu = X86_CPU(cs); 4920ebf007dSSergio Lopez 4930ebf007dSSergio Lopez if (cpu->apic_state) { 494f703a04cSDamien Hedde device_legacy_reset(cpu->apic_state); 4950ebf007dSSergio Lopez } 4960ebf007dSSergio Lopez } 4970ebf007dSSergio Lopez } 4980ebf007dSSergio Lopez 4990ebf007dSSergio Lopez static void microvm_machine_get_pic(Object *obj, Visitor *v, const char *name, 5000ebf007dSSergio Lopez void *opaque, Error **errp) 5010ebf007dSSergio Lopez { 5020ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5030ebf007dSSergio Lopez OnOffAuto pic = mms->pic; 5040ebf007dSSergio Lopez 5050ebf007dSSergio Lopez visit_type_OnOffAuto(v, name, &pic, errp); 5060ebf007dSSergio Lopez } 5070ebf007dSSergio Lopez 5080ebf007dSSergio Lopez static void microvm_machine_set_pic(Object *obj, Visitor *v, const char *name, 5090ebf007dSSergio Lopez void *opaque, Error **errp) 5100ebf007dSSergio Lopez { 5110ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5120ebf007dSSergio Lopez 5130ebf007dSSergio Lopez visit_type_OnOffAuto(v, name, &mms->pic, errp); 5140ebf007dSSergio Lopez } 5150ebf007dSSergio Lopez 5160ebf007dSSergio Lopez static void microvm_machine_get_pit(Object *obj, Visitor *v, const char *name, 5170ebf007dSSergio Lopez void *opaque, Error **errp) 5180ebf007dSSergio Lopez { 5190ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5200ebf007dSSergio Lopez OnOffAuto pit = mms->pit; 5210ebf007dSSergio Lopez 5220ebf007dSSergio Lopez visit_type_OnOffAuto(v, name, &pit, errp); 5230ebf007dSSergio Lopez } 5240ebf007dSSergio Lopez 5250ebf007dSSergio Lopez static void microvm_machine_set_pit(Object *obj, Visitor *v, const char *name, 5260ebf007dSSergio Lopez void *opaque, Error **errp) 5270ebf007dSSergio Lopez { 5280ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5290ebf007dSSergio Lopez 5300ebf007dSSergio Lopez visit_type_OnOffAuto(v, name, &mms->pit, errp); 5310ebf007dSSergio Lopez } 5320ebf007dSSergio Lopez 5330ebf007dSSergio Lopez static void microvm_machine_get_rtc(Object *obj, Visitor *v, const char *name, 5340ebf007dSSergio Lopez void *opaque, Error **errp) 5350ebf007dSSergio Lopez { 5360ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5370ebf007dSSergio Lopez OnOffAuto rtc = mms->rtc; 5380ebf007dSSergio Lopez 5390ebf007dSSergio Lopez visit_type_OnOffAuto(v, name, &rtc, errp); 5400ebf007dSSergio Lopez } 5410ebf007dSSergio Lopez 5420ebf007dSSergio Lopez static void microvm_machine_set_rtc(Object *obj, Visitor *v, const char *name, 5430ebf007dSSergio Lopez void *opaque, Error **errp) 5440ebf007dSSergio Lopez { 5450ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5460ebf007dSSergio Lopez 5470ebf007dSSergio Lopez visit_type_OnOffAuto(v, name, &mms->rtc, errp); 5480ebf007dSSergio Lopez } 5490ebf007dSSergio Lopez 55024db877aSGerd Hoffmann static void microvm_machine_get_pcie(Object *obj, Visitor *v, const char *name, 55124db877aSGerd Hoffmann void *opaque, Error **errp) 55224db877aSGerd Hoffmann { 55324db877aSGerd Hoffmann MicrovmMachineState *mms = MICROVM_MACHINE(obj); 55424db877aSGerd Hoffmann OnOffAuto pcie = mms->pcie; 55524db877aSGerd Hoffmann 55624db877aSGerd Hoffmann visit_type_OnOffAuto(v, name, &pcie, errp); 55724db877aSGerd Hoffmann } 55824db877aSGerd Hoffmann 55924db877aSGerd Hoffmann static void microvm_machine_set_pcie(Object *obj, Visitor *v, const char *name, 56024db877aSGerd Hoffmann void *opaque, Error **errp) 56124db877aSGerd Hoffmann { 56224db877aSGerd Hoffmann MicrovmMachineState *mms = MICROVM_MACHINE(obj); 56324db877aSGerd Hoffmann 56424db877aSGerd Hoffmann visit_type_OnOffAuto(v, name, &mms->pcie, errp); 56524db877aSGerd Hoffmann } 56624db877aSGerd Hoffmann 5674d01b899SGerd Hoffmann static void microvm_machine_get_ioapic2(Object *obj, Visitor *v, const char *name, 5684d01b899SGerd Hoffmann void *opaque, Error **errp) 5694d01b899SGerd Hoffmann { 5704d01b899SGerd Hoffmann MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5714d01b899SGerd Hoffmann OnOffAuto ioapic2 = mms->ioapic2; 5724d01b899SGerd Hoffmann 5734d01b899SGerd Hoffmann visit_type_OnOffAuto(v, name, &ioapic2, errp); 5744d01b899SGerd Hoffmann } 5754d01b899SGerd Hoffmann 5764d01b899SGerd Hoffmann static void microvm_machine_set_ioapic2(Object *obj, Visitor *v, const char *name, 5774d01b899SGerd Hoffmann void *opaque, Error **errp) 5784d01b899SGerd Hoffmann { 5794d01b899SGerd Hoffmann MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5804d01b899SGerd Hoffmann 5814d01b899SGerd Hoffmann visit_type_OnOffAuto(v, name, &mms->ioapic2, errp); 5824d01b899SGerd Hoffmann } 5834d01b899SGerd Hoffmann 5840ebf007dSSergio Lopez static bool microvm_machine_get_isa_serial(Object *obj, Error **errp) 5850ebf007dSSergio Lopez { 5860ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5870ebf007dSSergio Lopez 5880ebf007dSSergio Lopez return mms->isa_serial; 5890ebf007dSSergio Lopez } 5900ebf007dSSergio Lopez 5910ebf007dSSergio Lopez static void microvm_machine_set_isa_serial(Object *obj, bool value, 5920ebf007dSSergio Lopez Error **errp) 5930ebf007dSSergio Lopez { 5940ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 5950ebf007dSSergio Lopez 5960ebf007dSSergio Lopez mms->isa_serial = value; 5970ebf007dSSergio Lopez } 5980ebf007dSSergio Lopez 5990ebf007dSSergio Lopez static bool microvm_machine_get_option_roms(Object *obj, Error **errp) 6000ebf007dSSergio Lopez { 6010ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 6020ebf007dSSergio Lopez 6030ebf007dSSergio Lopez return mms->option_roms; 6040ebf007dSSergio Lopez } 6050ebf007dSSergio Lopez 6060ebf007dSSergio Lopez static void microvm_machine_set_option_roms(Object *obj, bool value, 6070ebf007dSSergio Lopez Error **errp) 6080ebf007dSSergio Lopez { 6090ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 6100ebf007dSSergio Lopez 6110ebf007dSSergio Lopez mms->option_roms = value; 6120ebf007dSSergio Lopez } 6130ebf007dSSergio Lopez 6140ebf007dSSergio Lopez static bool microvm_machine_get_auto_kernel_cmdline(Object *obj, Error **errp) 6150ebf007dSSergio Lopez { 6160ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 6170ebf007dSSergio Lopez 6180ebf007dSSergio Lopez return mms->auto_kernel_cmdline; 6190ebf007dSSergio Lopez } 6200ebf007dSSergio Lopez 6210ebf007dSSergio Lopez static void microvm_machine_set_auto_kernel_cmdline(Object *obj, bool value, 6220ebf007dSSergio Lopez Error **errp) 6230ebf007dSSergio Lopez { 6240ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 6250ebf007dSSergio Lopez 6260ebf007dSSergio Lopez mms->auto_kernel_cmdline = value; 6270ebf007dSSergio Lopez } 6280ebf007dSSergio Lopez 6298045df14SGerd Hoffmann static void microvm_machine_done(Notifier *notifier, void *data) 6308045df14SGerd Hoffmann { 6318045df14SGerd Hoffmann MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState, 6328045df14SGerd Hoffmann machine_done); 6338045df14SGerd Hoffmann 6348045df14SGerd Hoffmann acpi_setup_microvm(mms); 6358045df14SGerd Hoffmann } 6368045df14SGerd Hoffmann 6378045df14SGerd Hoffmann static void microvm_powerdown_req(Notifier *notifier, void *data) 6388045df14SGerd Hoffmann { 6398045df14SGerd Hoffmann MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState, 6408045df14SGerd Hoffmann powerdown_req); 64150aef131SGerd Hoffmann X86MachineState *x86ms = X86_MACHINE(mms); 6428045df14SGerd Hoffmann 64350aef131SGerd Hoffmann if (x86ms->acpi_dev) { 64450aef131SGerd Hoffmann Object *obj = OBJECT(x86ms->acpi_dev); 6458045df14SGerd Hoffmann AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj); 64650aef131SGerd Hoffmann adevc->send_event(ACPI_DEVICE_IF(x86ms->acpi_dev), 64750aef131SGerd Hoffmann ACPI_POWER_DOWN_STATUS); 6488045df14SGerd Hoffmann } 6498045df14SGerd Hoffmann } 6508045df14SGerd Hoffmann 6510ebf007dSSergio Lopez static void microvm_machine_initfn(Object *obj) 6520ebf007dSSergio Lopez { 6530ebf007dSSergio Lopez MicrovmMachineState *mms = MICROVM_MACHINE(obj); 6540ebf007dSSergio Lopez 6550ebf007dSSergio Lopez /* Configuration */ 6560ebf007dSSergio Lopez mms->pic = ON_OFF_AUTO_AUTO; 6570ebf007dSSergio Lopez mms->pit = ON_OFF_AUTO_AUTO; 6580ebf007dSSergio Lopez mms->rtc = ON_OFF_AUTO_AUTO; 65924db877aSGerd Hoffmann mms->pcie = ON_OFF_AUTO_AUTO; 6604d01b899SGerd Hoffmann mms->ioapic2 = ON_OFF_AUTO_AUTO; 6610ebf007dSSergio Lopez mms->isa_serial = true; 6620ebf007dSSergio Lopez mms->option_roms = true; 6630ebf007dSSergio Lopez mms->auto_kernel_cmdline = true; 6640ebf007dSSergio Lopez 6650ebf007dSSergio Lopez /* State */ 6660ebf007dSSergio Lopez mms->kernel_cmdline_fixed = false; 6678045df14SGerd Hoffmann 6688045df14SGerd Hoffmann mms->machine_done.notify = microvm_machine_done; 6698045df14SGerd Hoffmann qemu_add_machine_init_done_notifier(&mms->machine_done); 6708045df14SGerd Hoffmann mms->powerdown_req.notify = microvm_powerdown_req; 6718045df14SGerd Hoffmann qemu_register_powerdown_notifier(&mms->powerdown_req); 6720ebf007dSSergio Lopez } 6730ebf007dSSergio Lopez 6740ebf007dSSergio Lopez static void microvm_class_init(ObjectClass *oc, void *data) 6750ebf007dSSergio Lopez { 6760ebf007dSSergio Lopez MachineClass *mc = MACHINE_CLASS(oc); 677e3ab9873SGerd Hoffmann HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); 6780ebf007dSSergio Lopez 6790ebf007dSSergio Lopez mc->init = microvm_machine_state_init; 6800ebf007dSSergio Lopez 6810ebf007dSSergio Lopez mc->family = "microvm_i386"; 6820ebf007dSSergio Lopez mc->desc = "microvm (i386)"; 6830ebf007dSSergio Lopez mc->units_per_default_bus = 1; 6840ebf007dSSergio Lopez mc->no_floppy = 1; 6850ebf007dSSergio Lopez mc->max_cpus = 288; 6860ebf007dSSergio Lopez mc->has_hotpluggable_cpus = false; 6870ebf007dSSergio Lopez mc->auto_enable_numa_with_memhp = false; 688195784a0SDavid Hildenbrand mc->auto_enable_numa_with_memdev = false; 6890ebf007dSSergio Lopez mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE; 6900ebf007dSSergio Lopez mc->nvdimm_supported = false; 6919ad54686SIgor Mammedov mc->default_ram_id = "microvm.ram"; 6920ebf007dSSergio Lopez 6930ebf007dSSergio Lopez /* Avoid relying too much on kernel components */ 6940ebf007dSSergio Lopez mc->default_kernel_irqchip_split = true; 6950ebf007dSSergio Lopez 6960ebf007dSSergio Lopez /* Machine class handlers */ 6970ebf007dSSergio Lopez mc->reset = microvm_machine_reset; 6980ebf007dSSergio Lopez 699e3ab9873SGerd Hoffmann /* hotplug (for cpu coldplug) */ 700e3ab9873SGerd Hoffmann mc->get_hotplug_handler = microvm_get_hotplug_handler; 701e3ab9873SGerd Hoffmann hc->pre_plug = microvm_device_pre_plug_cb; 702e3ab9873SGerd Hoffmann hc->plug = microvm_device_plug_cb; 703e3ab9873SGerd Hoffmann hc->unplug_request = microvm_device_unplug_request_cb; 704e3ab9873SGerd Hoffmann hc->unplug = microvm_device_unplug_cb; 705e3ab9873SGerd Hoffmann 7060ebf007dSSergio Lopez object_class_property_add(oc, MICROVM_MACHINE_PIC, "OnOffAuto", 7070ebf007dSSergio Lopez microvm_machine_get_pic, 7080ebf007dSSergio Lopez microvm_machine_set_pic, 709d2623129SMarkus Armbruster NULL, NULL); 7100ebf007dSSergio Lopez object_class_property_set_description(oc, MICROVM_MACHINE_PIC, 7117eecec7dSMarkus Armbruster "Enable i8259 PIC"); 7120ebf007dSSergio Lopez 7130ebf007dSSergio Lopez object_class_property_add(oc, MICROVM_MACHINE_PIT, "OnOffAuto", 7140ebf007dSSergio Lopez microvm_machine_get_pit, 7150ebf007dSSergio Lopez microvm_machine_set_pit, 716d2623129SMarkus Armbruster NULL, NULL); 7170ebf007dSSergio Lopez object_class_property_set_description(oc, MICROVM_MACHINE_PIT, 7187eecec7dSMarkus Armbruster "Enable i8254 PIT"); 7190ebf007dSSergio Lopez 7200ebf007dSSergio Lopez object_class_property_add(oc, MICROVM_MACHINE_RTC, "OnOffAuto", 7210ebf007dSSergio Lopez microvm_machine_get_rtc, 7220ebf007dSSergio Lopez microvm_machine_set_rtc, 723d2623129SMarkus Armbruster NULL, NULL); 7240ebf007dSSergio Lopez object_class_property_set_description(oc, MICROVM_MACHINE_RTC, 7257eecec7dSMarkus Armbruster "Enable MC146818 RTC"); 7260ebf007dSSergio Lopez 72724db877aSGerd Hoffmann object_class_property_add(oc, MICROVM_MACHINE_PCIE, "OnOffAuto", 72824db877aSGerd Hoffmann microvm_machine_get_pcie, 72924db877aSGerd Hoffmann microvm_machine_set_pcie, 73024db877aSGerd Hoffmann NULL, NULL); 73124db877aSGerd Hoffmann object_class_property_set_description(oc, MICROVM_MACHINE_PCIE, 73224db877aSGerd Hoffmann "Enable PCIe"); 73324db877aSGerd Hoffmann 7344d01b899SGerd Hoffmann object_class_property_add(oc, MICROVM_MACHINE_IOAPIC2, "OnOffAuto", 7354d01b899SGerd Hoffmann microvm_machine_get_ioapic2, 7364d01b899SGerd Hoffmann microvm_machine_set_ioapic2, 7374d01b899SGerd Hoffmann NULL, NULL); 7384d01b899SGerd Hoffmann object_class_property_set_description(oc, MICROVM_MACHINE_IOAPIC2, 7394d01b899SGerd Hoffmann "Enable second IO-APIC"); 7404d01b899SGerd Hoffmann 7410ebf007dSSergio Lopez object_class_property_add_bool(oc, MICROVM_MACHINE_ISA_SERIAL, 7420ebf007dSSergio Lopez microvm_machine_get_isa_serial, 743d2623129SMarkus Armbruster microvm_machine_set_isa_serial); 7440ebf007dSSergio Lopez object_class_property_set_description(oc, MICROVM_MACHINE_ISA_SERIAL, 7457eecec7dSMarkus Armbruster "Set off to disable the instantiation an ISA serial port"); 7460ebf007dSSergio Lopez 7470ebf007dSSergio Lopez object_class_property_add_bool(oc, MICROVM_MACHINE_OPTION_ROMS, 7480ebf007dSSergio Lopez microvm_machine_get_option_roms, 749d2623129SMarkus Armbruster microvm_machine_set_option_roms); 7500ebf007dSSergio Lopez object_class_property_set_description(oc, MICROVM_MACHINE_OPTION_ROMS, 7517eecec7dSMarkus Armbruster "Set off to disable loading option ROMs"); 7520ebf007dSSergio Lopez 7530ebf007dSSergio Lopez object_class_property_add_bool(oc, MICROVM_MACHINE_AUTO_KERNEL_CMDLINE, 7540ebf007dSSergio Lopez microvm_machine_get_auto_kernel_cmdline, 755d2623129SMarkus Armbruster microvm_machine_set_auto_kernel_cmdline); 7560ebf007dSSergio Lopez object_class_property_set_description(oc, 7570ebf007dSSergio Lopez MICROVM_MACHINE_AUTO_KERNEL_CMDLINE, 7587eecec7dSMarkus Armbruster "Set off to disable adding virtio-mmio devices to the kernel cmdline"); 75963bcfe7bSGerd Hoffmann 76063bcfe7bSGerd Hoffmann machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE); 7610ebf007dSSergio Lopez } 7620ebf007dSSergio Lopez 7630ebf007dSSergio Lopez static const TypeInfo microvm_machine_info = { 7640ebf007dSSergio Lopez .name = TYPE_MICROVM_MACHINE, 7650ebf007dSSergio Lopez .parent = TYPE_X86_MACHINE, 7660ebf007dSSergio Lopez .instance_size = sizeof(MicrovmMachineState), 7670ebf007dSSergio Lopez .instance_init = microvm_machine_initfn, 7680ebf007dSSergio Lopez .class_size = sizeof(MicrovmMachineClass), 7690ebf007dSSergio Lopez .class_init = microvm_class_init, 7700ebf007dSSergio Lopez .interfaces = (InterfaceInfo[]) { 771e3ab9873SGerd Hoffmann { TYPE_HOTPLUG_HANDLER }, 7720ebf007dSSergio Lopez { } 7730ebf007dSSergio Lopez }, 7740ebf007dSSergio Lopez }; 7750ebf007dSSergio Lopez 7760ebf007dSSergio Lopez static void microvm_machine_init(void) 7770ebf007dSSergio Lopez { 7780ebf007dSSergio Lopez type_register_static(µvm_machine_info); 7790ebf007dSSergio Lopez } 7800ebf007dSSergio Lopez type_init(microvm_machine_init); 781