18045df14SGerd Hoffmann /* Support for generating ACPI tables and passing them to Guests 28045df14SGerd Hoffmann * 38045df14SGerd Hoffmann * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net> 48045df14SGerd Hoffmann * Copyright (C) 2006 Fabrice Bellard 58045df14SGerd Hoffmann * Copyright (C) 2013 Red Hat Inc 68045df14SGerd Hoffmann * 78045df14SGerd Hoffmann * Author: Michael S. Tsirkin <mst@redhat.com> 88045df14SGerd Hoffmann * 98045df14SGerd Hoffmann * This program is free software; you can redistribute it and/or modify 108045df14SGerd Hoffmann * it under the terms of the GNU General Public License as published by 118045df14SGerd Hoffmann * the Free Software Foundation; either version 2 of the License, or 128045df14SGerd Hoffmann * (at your option) any later version. 138045df14SGerd Hoffmann 148045df14SGerd Hoffmann * This program is distributed in the hope that it will be useful, 158045df14SGerd Hoffmann * but WITHOUT ANY WARRANTY; without even the implied warranty of 168045df14SGerd Hoffmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 178045df14SGerd Hoffmann * GNU General Public License for more details. 188045df14SGerd Hoffmann 198045df14SGerd Hoffmann * You should have received a copy of the GNU General Public License along 208045df14SGerd Hoffmann * with this program; if not, see <http://www.gnu.org/licenses/>. 218045df14SGerd Hoffmann */ 228045df14SGerd Hoffmann 238045df14SGerd Hoffmann #include "qemu/osdep.h" 243b98c65fSGerd Hoffmann #include "qemu/cutils.h" 258045df14SGerd Hoffmann #include "qapi/error.h" 268045df14SGerd Hoffmann 278045df14SGerd Hoffmann #include "exec/memory.h" 288045df14SGerd Hoffmann #include "hw/acpi/acpi.h" 299c6c0aeaSBernhard Beschow #include "hw/acpi/acpi_aml_interface.h" 308045df14SGerd Hoffmann #include "hw/acpi/aml-build.h" 318045df14SGerd Hoffmann #include "hw/acpi/bios-linker-loader.h" 328045df14SGerd Hoffmann #include "hw/acpi/generic_event_device.h" 338045df14SGerd Hoffmann #include "hw/acpi/utils.h" 348486f12fSEric DeVolder #include "hw/acpi/erst.h" 358045df14SGerd Hoffmann #include "hw/i386/fw_cfg.h" 368045df14SGerd Hoffmann #include "hw/i386/microvm.h" 3724db877aSGerd Hoffmann #include "hw/pci/pci.h" 3824db877aSGerd Hoffmann #include "hw/pci/pcie_host.h" 39d4a42e85SGerd Hoffmann #include "hw/usb/xhci.h" 403b98c65fSGerd Hoffmann #include "hw/virtio/virtio-mmio.h" 41128e050dSAni Sinha #include "hw/input/i8042.h" 428045df14SGerd Hoffmann 438045df14SGerd Hoffmann #include "acpi-common.h" 448045df14SGerd Hoffmann #include "acpi-microvm.h" 458045df14SGerd Hoffmann 468486f12fSEric DeVolder #include CONFIG_DEVICES 478486f12fSEric DeVolder 483b98c65fSGerd Hoffmann static void acpi_dsdt_add_virtio(Aml *scope, 493b98c65fSGerd Hoffmann MicrovmMachineState *mms) 503b98c65fSGerd Hoffmann { 513b98c65fSGerd Hoffmann gchar *separator; 523b98c65fSGerd Hoffmann long int index; 533b98c65fSGerd Hoffmann BusState *bus; 543b98c65fSGerd Hoffmann BusChild *kid; 553b98c65fSGerd Hoffmann 563b98c65fSGerd Hoffmann bus = sysbus_get_default(); 573b98c65fSGerd Hoffmann QTAILQ_FOREACH(kid, &bus->children, sibling) { 583b98c65fSGerd Hoffmann DeviceState *dev = kid->child; 593b98c65fSGerd Hoffmann Object *obj = object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MMIO); 603b98c65fSGerd Hoffmann 613b98c65fSGerd Hoffmann if (obj) { 623b98c65fSGerd Hoffmann VirtIOMMIOProxy *mmio = VIRTIO_MMIO(obj); 633b98c65fSGerd Hoffmann VirtioBusState *mmio_virtio_bus = &mmio->bus; 643b98c65fSGerd Hoffmann BusState *mmio_bus = &mmio_virtio_bus->parent_obj; 653b98c65fSGerd Hoffmann 663b98c65fSGerd Hoffmann if (QTAILQ_EMPTY(&mmio_bus->children)) { 673b98c65fSGerd Hoffmann continue; 683b98c65fSGerd Hoffmann } 693b98c65fSGerd Hoffmann separator = g_strrstr(mmio_bus->name, "."); 703b98c65fSGerd Hoffmann if (!separator) { 713b98c65fSGerd Hoffmann continue; 723b98c65fSGerd Hoffmann } 733b98c65fSGerd Hoffmann if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) { 743b98c65fSGerd Hoffmann continue; 753b98c65fSGerd Hoffmann } 763b98c65fSGerd Hoffmann 773b98c65fSGerd Hoffmann uint32_t irq = mms->virtio_irq_base + index; 783b98c65fSGerd Hoffmann hwaddr base = VIRTIO_MMIO_BASE + index * 512; 793b98c65fSGerd Hoffmann hwaddr size = 512; 803b98c65fSGerd Hoffmann 813b98c65fSGerd Hoffmann Aml *dev = aml_device("VR%02u", (unsigned)index); 823b98c65fSGerd Hoffmann aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0005"))); 833b98c65fSGerd Hoffmann aml_append(dev, aml_name_decl("_UID", aml_int(index))); 843b98c65fSGerd Hoffmann aml_append(dev, aml_name_decl("_CCA", aml_int(1))); 853b98c65fSGerd Hoffmann 863b98c65fSGerd Hoffmann Aml *crs = aml_resource_template(); 873b98c65fSGerd Hoffmann aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE)); 883b98c65fSGerd Hoffmann aml_append(crs, 893b98c65fSGerd Hoffmann aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, 903b98c65fSGerd Hoffmann AML_EXCLUSIVE, &irq, 1)); 913b98c65fSGerd Hoffmann aml_append(dev, aml_name_decl("_CRS", crs)); 923b98c65fSGerd Hoffmann aml_append(scope, dev); 933b98c65fSGerd Hoffmann } 943b98c65fSGerd Hoffmann } 953b98c65fSGerd Hoffmann } 963b98c65fSGerd Hoffmann 97d4a42e85SGerd Hoffmann static void acpi_dsdt_add_xhci(Aml *scope, MicrovmMachineState *mms) 98d4a42e85SGerd Hoffmann { 99d4a42e85SGerd Hoffmann if (machine_usb(MACHINE(mms))) { 100d4a42e85SGerd Hoffmann xhci_sysbus_build_aml(scope, MICROVM_XHCI_BASE, MICROVM_XHCI_IRQ); 101d4a42e85SGerd Hoffmann } 102d4a42e85SGerd Hoffmann } 103d4a42e85SGerd Hoffmann 10424db877aSGerd Hoffmann static void acpi_dsdt_add_pci(Aml *scope, MicrovmMachineState *mms) 10524db877aSGerd Hoffmann { 10624db877aSGerd Hoffmann if (mms->pcie != ON_OFF_AUTO_ON) { 10724db877aSGerd Hoffmann return; 10824db877aSGerd Hoffmann } 10924db877aSGerd Hoffmann 11024db877aSGerd Hoffmann acpi_dsdt_add_gpex(scope, &mms->gpex); 11124db877aSGerd Hoffmann } 11224db877aSGerd Hoffmann 1138045df14SGerd Hoffmann static void 1148045df14SGerd Hoffmann build_dsdt_microvm(GArray *table_data, BIOSLinker *linker, 1158045df14SGerd Hoffmann MicrovmMachineState *mms) 1168045df14SGerd Hoffmann { 1178045df14SGerd Hoffmann X86MachineState *x86ms = X86_MACHINE(mms); 1188045df14SGerd Hoffmann Aml *dsdt, *sb_scope, *scope, *pkg; 1198045df14SGerd Hoffmann bool ambiguous; 1208045df14SGerd Hoffmann Object *isabus; 1218f20f9a7SIgor Mammedov AcpiTable table = { .sig = "DSDT", .rev = 2, .oem_id = x86ms->oem_id, 1228f20f9a7SIgor Mammedov .oem_table_id = x86ms->oem_table_id }; 1238045df14SGerd Hoffmann 1248045df14SGerd Hoffmann isabus = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous); 1258045df14SGerd Hoffmann assert(isabus); 1268045df14SGerd Hoffmann assert(!ambiguous); 1278045df14SGerd Hoffmann 1288f20f9a7SIgor Mammedov acpi_table_begin(&table, table_data); 1298045df14SGerd Hoffmann dsdt = init_aml_allocator(); 1308045df14SGerd Hoffmann 1318045df14SGerd Hoffmann sb_scope = aml_scope("_SB"); 1328045df14SGerd Hoffmann fw_cfg_add_acpi_dsdt(sb_scope, x86ms->fw_cfg); 1339c6c0aeaSBernhard Beschow qbus_build_aml(BUS(isabus), sb_scope); 13450aef131SGerd Hoffmann build_ged_aml(sb_scope, GED_DEVICE, x86ms->acpi_dev, 1358045df14SGerd Hoffmann GED_MMIO_IRQ, AML_SYSTEM_MEMORY, GED_MMIO_BASE); 1368045df14SGerd Hoffmann acpi_dsdt_add_power_button(sb_scope); 1373b98c65fSGerd Hoffmann acpi_dsdt_add_virtio(sb_scope, mms); 138d4a42e85SGerd Hoffmann acpi_dsdt_add_xhci(sb_scope, mms); 13924db877aSGerd Hoffmann acpi_dsdt_add_pci(sb_scope, mms); 1408045df14SGerd Hoffmann aml_append(dsdt, sb_scope); 1418045df14SGerd Hoffmann 1428045df14SGerd Hoffmann /* ACPI 5.0: Table 7-209 System State Package */ 1438045df14SGerd Hoffmann scope = aml_scope("\\"); 1448045df14SGerd Hoffmann pkg = aml_package(4); 1458045df14SGerd Hoffmann aml_append(pkg, aml_int(ACPI_GED_SLP_TYP_S5)); 1468045df14SGerd Hoffmann aml_append(pkg, aml_int(0)); /* ignored */ 1478045df14SGerd Hoffmann aml_append(pkg, aml_int(0)); /* reserved */ 1488045df14SGerd Hoffmann aml_append(pkg, aml_int(0)); /* reserved */ 1498045df14SGerd Hoffmann aml_append(scope, aml_name_decl("_S5", pkg)); 1508045df14SGerd Hoffmann aml_append(dsdt, scope); 1518045df14SGerd Hoffmann 1528f20f9a7SIgor Mammedov /* copy AML bytecode into ACPI tables blob */ 1538045df14SGerd Hoffmann g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len); 1548f20f9a7SIgor Mammedov 1558f20f9a7SIgor Mammedov acpi_table_end(linker, &table); 1568045df14SGerd Hoffmann free_aml_allocator(); 1578045df14SGerd Hoffmann } 1588045df14SGerd Hoffmann 1598045df14SGerd Hoffmann static void acpi_build_microvm(AcpiBuildTables *tables, 1608045df14SGerd Hoffmann MicrovmMachineState *mms) 1618045df14SGerd Hoffmann { 1628045df14SGerd Hoffmann MachineState *machine = MACHINE(mms); 16350aef131SGerd Hoffmann X86MachineState *x86ms = X86_MACHINE(mms); 1648045df14SGerd Hoffmann GArray *table_offsets; 1658045df14SGerd Hoffmann GArray *tables_blob = tables->table_data; 1668045df14SGerd Hoffmann unsigned dsdt, xsdt; 1678045df14SGerd Hoffmann AcpiFadtData pmfadt = { 1688045df14SGerd Hoffmann /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */ 1698045df14SGerd Hoffmann .rev = 5, 1708045df14SGerd Hoffmann .flags = ((1 << ACPI_FADT_F_HW_REDUCED_ACPI) | 1718045df14SGerd Hoffmann (1 << ACPI_FADT_F_RESET_REG_SUP)), 1728045df14SGerd Hoffmann 1738045df14SGerd Hoffmann /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */ 1748045df14SGerd Hoffmann .sleep_ctl = { 1758045df14SGerd Hoffmann .space_id = AML_AS_SYSTEM_MEMORY, 1768045df14SGerd Hoffmann .bit_width = 8, 1778045df14SGerd Hoffmann .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_CTL, 1788045df14SGerd Hoffmann }, 1798045df14SGerd Hoffmann .sleep_sts = { 1808045df14SGerd Hoffmann .space_id = AML_AS_SYSTEM_MEMORY, 1818045df14SGerd Hoffmann .bit_width = 8, 1828045df14SGerd Hoffmann .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_STS, 1838045df14SGerd Hoffmann }, 1848045df14SGerd Hoffmann 1858045df14SGerd Hoffmann /* ACPI 5.0: 4.8.3.6 Reset Register */ 1868045df14SGerd Hoffmann .reset_reg = { 1878045df14SGerd Hoffmann .space_id = AML_AS_SYSTEM_MEMORY, 1888045df14SGerd Hoffmann .bit_width = 8, 1898045df14SGerd Hoffmann .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_RESET, 1908045df14SGerd Hoffmann }, 1918045df14SGerd Hoffmann .reset_val = ACPI_GED_RESET_VALUE, 192128e050dSAni Sinha /* 193128e050dSAni Sinha * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture 194128e050dSAni Sinha * Flags, bit offset 1 - 8042. 195128e050dSAni Sinha */ 196128e050dSAni Sinha .iapc_boot_arch = iapc_boot_arch_8042(), 1978045df14SGerd Hoffmann }; 1988045df14SGerd Hoffmann 1998045df14SGerd Hoffmann table_offsets = g_array_new(false, true /* clear */, 2008045df14SGerd Hoffmann sizeof(uint32_t)); 2018045df14SGerd Hoffmann bios_linker_loader_alloc(tables->linker, 2028045df14SGerd Hoffmann ACPI_BUILD_TABLE_FILE, tables_blob, 2038045df14SGerd Hoffmann 64 /* Ensure FACS is aligned */, 2048045df14SGerd Hoffmann false /* high memory */); 2058045df14SGerd Hoffmann 2068045df14SGerd Hoffmann dsdt = tables_blob->len; 2078045df14SGerd Hoffmann build_dsdt_microvm(tables_blob, tables->linker, mms); 2088045df14SGerd Hoffmann 2098045df14SGerd Hoffmann pmfadt.dsdt_tbl_offset = &dsdt; 2108045df14SGerd Hoffmann pmfadt.xdsdt_tbl_offset = &dsdt; 2118045df14SGerd Hoffmann acpi_add_table(table_offsets, tables_blob); 212d07b2286SMarian Postevca build_fadt(tables_blob, tables->linker, &pmfadt, x86ms->oem_id, 213d07b2286SMarian Postevca x86ms->oem_table_id); 2148045df14SGerd Hoffmann 2158045df14SGerd Hoffmann acpi_add_table(table_offsets, tables_blob); 2168045df14SGerd Hoffmann acpi_build_madt(tables_blob, tables->linker, X86_MACHINE(machine), 217*f4a06e59SBernhard Beschow x86ms->oem_id, x86ms->oem_table_id); 2188045df14SGerd Hoffmann 2198486f12fSEric DeVolder #ifdef CONFIG_ACPI_ERST 2208486f12fSEric DeVolder { 2218486f12fSEric DeVolder Object *erst_dev; 2228486f12fSEric DeVolder erst_dev = find_erst_dev(); 2238486f12fSEric DeVolder if (erst_dev) { 2248486f12fSEric DeVolder acpi_add_table(table_offsets, tables_blob); 2258486f12fSEric DeVolder build_erst(tables_blob, tables->linker, erst_dev, 2268486f12fSEric DeVolder x86ms->oem_id, x86ms->oem_table_id); 2278486f12fSEric DeVolder } 2288486f12fSEric DeVolder } 2298486f12fSEric DeVolder #endif 2308486f12fSEric DeVolder 2318045df14SGerd Hoffmann xsdt = tables_blob->len; 232d07b2286SMarian Postevca build_xsdt(tables_blob, tables->linker, table_offsets, x86ms->oem_id, 233d07b2286SMarian Postevca x86ms->oem_table_id); 2348045df14SGerd Hoffmann 2358045df14SGerd Hoffmann /* RSDP is in FSEG memory, so allocate it separately */ 2368045df14SGerd Hoffmann { 2378045df14SGerd Hoffmann AcpiRsdpData rsdp_data = { 2388045df14SGerd Hoffmann /* ACPI 2.0: 5.2.4.3 RSDP Structure */ 2398045df14SGerd Hoffmann .revision = 2, /* xsdt needs v2 */ 240d07b2286SMarian Postevca .oem_id = x86ms->oem_id, 2418045df14SGerd Hoffmann .xsdt_tbl_offset = &xsdt, 2428045df14SGerd Hoffmann .rsdt_tbl_offset = NULL, 2438045df14SGerd Hoffmann }; 2448045df14SGerd Hoffmann build_rsdp(tables->rsdp, tables->linker, &rsdp_data); 2458045df14SGerd Hoffmann } 2468045df14SGerd Hoffmann 2478045df14SGerd Hoffmann /* Cleanup memory that's no longer used. */ 2488045df14SGerd Hoffmann g_array_free(table_offsets, true); 2498045df14SGerd Hoffmann } 2508045df14SGerd Hoffmann 2518045df14SGerd Hoffmann static void acpi_build_no_update(void *build_opaque) 2528045df14SGerd Hoffmann { 2538045df14SGerd Hoffmann /* nothing, microvm tables don't change at runtime */ 2548045df14SGerd Hoffmann } 2558045df14SGerd Hoffmann 2568045df14SGerd Hoffmann void acpi_setup_microvm(MicrovmMachineState *mms) 2578045df14SGerd Hoffmann { 2588045df14SGerd Hoffmann X86MachineState *x86ms = X86_MACHINE(mms); 2598045df14SGerd Hoffmann AcpiBuildTables tables; 2608045df14SGerd Hoffmann 2618045df14SGerd Hoffmann assert(x86ms->fw_cfg); 2628045df14SGerd Hoffmann 2638045df14SGerd Hoffmann if (!x86_machine_is_acpi_enabled(x86ms)) { 2648045df14SGerd Hoffmann return; 2658045df14SGerd Hoffmann } 2668045df14SGerd Hoffmann 2678045df14SGerd Hoffmann acpi_build_tables_init(&tables); 2688045df14SGerd Hoffmann acpi_build_microvm(&tables, mms); 2698045df14SGerd Hoffmann 2708045df14SGerd Hoffmann /* Now expose it all to Guest */ 2716930ba0dSDavid Hildenbrand acpi_add_rom_blob(acpi_build_no_update, NULL, tables.table_data, 2726930ba0dSDavid Hildenbrand ACPI_BUILD_TABLE_FILE); 2736930ba0dSDavid Hildenbrand acpi_add_rom_blob(acpi_build_no_update, NULL, tables.linker->cmd_blob, 2746930ba0dSDavid Hildenbrand ACPI_BUILD_LOADER_FILE); 2756930ba0dSDavid Hildenbrand acpi_add_rom_blob(acpi_build_no_update, NULL, tables.rsdp, 2766930ba0dSDavid Hildenbrand ACPI_BUILD_RSDP_FILE); 2778045df14SGerd Hoffmann 2788045df14SGerd Hoffmann acpi_build_tables_cleanup(&tables, false); 2798045df14SGerd Hoffmann } 280