xref: /openbmc/qemu/hw/i386/acpi-microvm.c (revision 34eac35f893664eb8545b98142e23d9954722766)
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"
40*8199bf48SSunil V L #include "hw/virtio/virtio-acpi.h"
413b98c65fSGerd Hoffmann #include "hw/virtio/virtio-mmio.h"
42128e050dSAni Sinha #include "hw/input/i8042.h"
438045df14SGerd Hoffmann 
448045df14SGerd Hoffmann #include "acpi-common.h"
458045df14SGerd Hoffmann #include "acpi-microvm.h"
468045df14SGerd Hoffmann 
478486f12fSEric DeVolder #include CONFIG_DEVICES
488486f12fSEric DeVolder 
acpi_dsdt_add_virtio(Aml * scope,MicrovmMachineState * mms)493b98c65fSGerd Hoffmann static void acpi_dsdt_add_virtio(Aml *scope,
503b98c65fSGerd Hoffmann                                  MicrovmMachineState *mms)
513b98c65fSGerd Hoffmann {
523b98c65fSGerd Hoffmann     gchar *separator;
533b98c65fSGerd Hoffmann     long int index;
543b98c65fSGerd Hoffmann     BusState *bus;
553b98c65fSGerd Hoffmann     BusChild *kid;
563b98c65fSGerd Hoffmann 
573b98c65fSGerd Hoffmann     bus = sysbus_get_default();
583b98c65fSGerd Hoffmann     QTAILQ_FOREACH(kid, &bus->children, sibling) {
5936363678SAni Sinha         Object *obj = object_dynamic_cast(OBJECT(kid->child),
6036363678SAni Sinha                                           TYPE_VIRTIO_MMIO);
613b98c65fSGerd Hoffmann 
623b98c65fSGerd Hoffmann         if (obj) {
633b98c65fSGerd Hoffmann             VirtIOMMIOProxy *mmio = VIRTIO_MMIO(obj);
643b98c65fSGerd Hoffmann             VirtioBusState *mmio_virtio_bus = &mmio->bus;
653b98c65fSGerd Hoffmann             BusState *mmio_bus = &mmio_virtio_bus->parent_obj;
663b98c65fSGerd Hoffmann 
673b98c65fSGerd Hoffmann             if (QTAILQ_EMPTY(&mmio_bus->children)) {
683b98c65fSGerd Hoffmann                 continue;
693b98c65fSGerd Hoffmann             }
703b98c65fSGerd Hoffmann             separator = g_strrstr(mmio_bus->name, ".");
713b98c65fSGerd Hoffmann             if (!separator) {
723b98c65fSGerd Hoffmann                 continue;
733b98c65fSGerd Hoffmann             }
743b98c65fSGerd Hoffmann             if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) {
753b98c65fSGerd Hoffmann                 continue;
763b98c65fSGerd Hoffmann             }
773b98c65fSGerd Hoffmann 
783b98c65fSGerd Hoffmann             uint32_t irq = mms->virtio_irq_base + index;
793b98c65fSGerd Hoffmann             hwaddr base = VIRTIO_MMIO_BASE + index * 512;
803b98c65fSGerd Hoffmann             hwaddr size = 512;
81*8199bf48SSunil V L             virtio_acpi_dsdt_add(scope, base, size, irq, index, 1);
823b98c65fSGerd Hoffmann         }
833b98c65fSGerd Hoffmann     }
843b98c65fSGerd Hoffmann }
853b98c65fSGerd Hoffmann 
acpi_dsdt_add_xhci(Aml * scope,MicrovmMachineState * mms)86d4a42e85SGerd Hoffmann static void acpi_dsdt_add_xhci(Aml *scope, MicrovmMachineState *mms)
87d4a42e85SGerd Hoffmann {
88d4a42e85SGerd Hoffmann     if (machine_usb(MACHINE(mms))) {
89d4a42e85SGerd Hoffmann         xhci_sysbus_build_aml(scope, MICROVM_XHCI_BASE, MICROVM_XHCI_IRQ);
90d4a42e85SGerd Hoffmann     }
91d4a42e85SGerd Hoffmann }
92d4a42e85SGerd Hoffmann 
acpi_dsdt_add_pci(Aml * scope,MicrovmMachineState * mms)9324db877aSGerd Hoffmann static void acpi_dsdt_add_pci(Aml *scope, MicrovmMachineState *mms)
9424db877aSGerd Hoffmann {
9524db877aSGerd Hoffmann     if (mms->pcie != ON_OFF_AUTO_ON) {
9624db877aSGerd Hoffmann         return;
9724db877aSGerd Hoffmann     }
9824db877aSGerd Hoffmann 
9924db877aSGerd Hoffmann     acpi_dsdt_add_gpex(scope, &mms->gpex);
10024db877aSGerd Hoffmann }
10124db877aSGerd Hoffmann 
1028045df14SGerd Hoffmann static void
build_dsdt_microvm(GArray * table_data,BIOSLinker * linker,MicrovmMachineState * mms)1038045df14SGerd Hoffmann build_dsdt_microvm(GArray *table_data, BIOSLinker *linker,
1048045df14SGerd Hoffmann                    MicrovmMachineState *mms)
1058045df14SGerd Hoffmann {
1068045df14SGerd Hoffmann     X86MachineState *x86ms = X86_MACHINE(mms);
1078045df14SGerd Hoffmann     Aml *dsdt, *sb_scope, *scope, *pkg;
1088045df14SGerd Hoffmann     bool ambiguous;
1098045df14SGerd Hoffmann     Object *isabus;
1108f20f9a7SIgor Mammedov     AcpiTable table = { .sig = "DSDT", .rev = 2, .oem_id = x86ms->oem_id,
1118f20f9a7SIgor Mammedov                         .oem_table_id = x86ms->oem_table_id };
1128045df14SGerd Hoffmann 
1138045df14SGerd Hoffmann     isabus = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
1148045df14SGerd Hoffmann     assert(isabus);
1158045df14SGerd Hoffmann     assert(!ambiguous);
1168045df14SGerd Hoffmann 
1178f20f9a7SIgor Mammedov     acpi_table_begin(&table, table_data);
1188045df14SGerd Hoffmann     dsdt = init_aml_allocator();
1198045df14SGerd Hoffmann 
1208045df14SGerd Hoffmann     sb_scope = aml_scope("_SB");
1218045df14SGerd Hoffmann     fw_cfg_add_acpi_dsdt(sb_scope, x86ms->fw_cfg);
1229c6c0aeaSBernhard Beschow     qbus_build_aml(BUS(isabus), sb_scope);
12350aef131SGerd Hoffmann     build_ged_aml(sb_scope, GED_DEVICE, x86ms->acpi_dev,
1248045df14SGerd Hoffmann                   GED_MMIO_IRQ, AML_SYSTEM_MEMORY, GED_MMIO_BASE);
1258045df14SGerd Hoffmann     acpi_dsdt_add_power_button(sb_scope);
1263b98c65fSGerd Hoffmann     acpi_dsdt_add_virtio(sb_scope, mms);
127d4a42e85SGerd Hoffmann     acpi_dsdt_add_xhci(sb_scope, mms);
12824db877aSGerd Hoffmann     acpi_dsdt_add_pci(sb_scope, mms);
1298045df14SGerd Hoffmann     aml_append(dsdt, sb_scope);
1308045df14SGerd Hoffmann 
1318045df14SGerd Hoffmann     /* ACPI 5.0: Table 7-209 System State Package */
1328045df14SGerd Hoffmann     scope = aml_scope("\\");
1338045df14SGerd Hoffmann     pkg = aml_package(4);
1348045df14SGerd Hoffmann     aml_append(pkg, aml_int(ACPI_GED_SLP_TYP_S5));
1358045df14SGerd Hoffmann     aml_append(pkg, aml_int(0)); /* ignored */
1368045df14SGerd Hoffmann     aml_append(pkg, aml_int(0)); /* reserved */
1378045df14SGerd Hoffmann     aml_append(pkg, aml_int(0)); /* reserved */
1388045df14SGerd Hoffmann     aml_append(scope, aml_name_decl("_S5", pkg));
1398045df14SGerd Hoffmann     aml_append(dsdt, scope);
1408045df14SGerd Hoffmann 
1418f20f9a7SIgor Mammedov     /* copy AML bytecode into ACPI tables blob */
1428045df14SGerd Hoffmann     g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
1438f20f9a7SIgor Mammedov 
1448f20f9a7SIgor Mammedov     acpi_table_end(linker, &table);
1458045df14SGerd Hoffmann     free_aml_allocator();
1468045df14SGerd Hoffmann }
1478045df14SGerd Hoffmann 
acpi_build_microvm(AcpiBuildTables * tables,MicrovmMachineState * mms)1488045df14SGerd Hoffmann static void acpi_build_microvm(AcpiBuildTables *tables,
1498045df14SGerd Hoffmann                                MicrovmMachineState *mms)
1508045df14SGerd Hoffmann {
1518045df14SGerd Hoffmann     MachineState *machine = MACHINE(mms);
15250aef131SGerd Hoffmann     X86MachineState *x86ms = X86_MACHINE(mms);
1538045df14SGerd Hoffmann     GArray *table_offsets;
1548045df14SGerd Hoffmann     GArray *tables_blob = tables->table_data;
1558045df14SGerd Hoffmann     unsigned dsdt, xsdt;
1568045df14SGerd Hoffmann     AcpiFadtData pmfadt = {
1578045df14SGerd Hoffmann         /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */
1588045df14SGerd Hoffmann         .rev = 5,
1598045df14SGerd Hoffmann         .flags = ((1 << ACPI_FADT_F_HW_REDUCED_ACPI) |
1608045df14SGerd Hoffmann                   (1 << ACPI_FADT_F_RESET_REG_SUP)),
1618045df14SGerd Hoffmann 
1628045df14SGerd Hoffmann         /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */
1638045df14SGerd Hoffmann         .sleep_ctl = {
1648045df14SGerd Hoffmann             .space_id = AML_AS_SYSTEM_MEMORY,
1658045df14SGerd Hoffmann             .bit_width = 8,
1668045df14SGerd Hoffmann             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_CTL,
1678045df14SGerd Hoffmann         },
1688045df14SGerd Hoffmann         .sleep_sts = {
1698045df14SGerd Hoffmann             .space_id = AML_AS_SYSTEM_MEMORY,
1708045df14SGerd Hoffmann             .bit_width = 8,
1718045df14SGerd Hoffmann             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_STS,
1728045df14SGerd Hoffmann         },
1738045df14SGerd Hoffmann 
1748045df14SGerd Hoffmann         /* ACPI 5.0: 4.8.3.6 Reset Register */
1758045df14SGerd Hoffmann         .reset_reg = {
1768045df14SGerd Hoffmann             .space_id = AML_AS_SYSTEM_MEMORY,
1778045df14SGerd Hoffmann             .bit_width = 8,
1788045df14SGerd Hoffmann             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_RESET,
1798045df14SGerd Hoffmann         },
1808045df14SGerd Hoffmann         .reset_val = ACPI_GED_RESET_VALUE,
181128e050dSAni Sinha         /*
182128e050dSAni Sinha          * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture
183128e050dSAni Sinha          * Flags, bit offset 1 - 8042.
184128e050dSAni Sinha          */
185128e050dSAni Sinha         .iapc_boot_arch = iapc_boot_arch_8042(),
1868045df14SGerd Hoffmann     };
1878045df14SGerd Hoffmann 
1888045df14SGerd Hoffmann     table_offsets = g_array_new(false, true /* clear */,
1898045df14SGerd Hoffmann                                         sizeof(uint32_t));
1908045df14SGerd Hoffmann     bios_linker_loader_alloc(tables->linker,
1918045df14SGerd Hoffmann                              ACPI_BUILD_TABLE_FILE, tables_blob,
1928045df14SGerd Hoffmann                              64 /* Ensure FACS is aligned */,
1938045df14SGerd Hoffmann                              false /* high memory */);
1948045df14SGerd Hoffmann 
1958045df14SGerd Hoffmann     dsdt = tables_blob->len;
1968045df14SGerd Hoffmann     build_dsdt_microvm(tables_blob, tables->linker, mms);
1978045df14SGerd Hoffmann 
1988045df14SGerd Hoffmann     pmfadt.dsdt_tbl_offset = &dsdt;
1998045df14SGerd Hoffmann     pmfadt.xdsdt_tbl_offset = &dsdt;
2008045df14SGerd Hoffmann     acpi_add_table(table_offsets, tables_blob);
201d07b2286SMarian Postevca     build_fadt(tables_blob, tables->linker, &pmfadt, x86ms->oem_id,
202d07b2286SMarian Postevca                x86ms->oem_table_id);
2038045df14SGerd Hoffmann 
2048045df14SGerd Hoffmann     acpi_add_table(table_offsets, tables_blob);
2058045df14SGerd Hoffmann     acpi_build_madt(tables_blob, tables->linker, X86_MACHINE(machine),
206f4a06e59SBernhard Beschow                     x86ms->oem_id, x86ms->oem_table_id);
2078045df14SGerd Hoffmann 
2088486f12fSEric DeVolder #ifdef CONFIG_ACPI_ERST
2098486f12fSEric DeVolder     {
2108486f12fSEric DeVolder         Object *erst_dev;
2118486f12fSEric DeVolder         erst_dev = find_erst_dev();
2128486f12fSEric DeVolder         if (erst_dev) {
2138486f12fSEric DeVolder             acpi_add_table(table_offsets, tables_blob);
2148486f12fSEric DeVolder             build_erst(tables_blob, tables->linker, erst_dev,
2158486f12fSEric DeVolder                        x86ms->oem_id, x86ms->oem_table_id);
2168486f12fSEric DeVolder         }
2178486f12fSEric DeVolder     }
2188486f12fSEric DeVolder #endif
2198486f12fSEric DeVolder 
2208045df14SGerd Hoffmann     xsdt = tables_blob->len;
221d07b2286SMarian Postevca     build_xsdt(tables_blob, tables->linker, table_offsets, x86ms->oem_id,
222d07b2286SMarian Postevca                x86ms->oem_table_id);
2238045df14SGerd Hoffmann 
2248045df14SGerd Hoffmann     /* RSDP is in FSEG memory, so allocate it separately */
2258045df14SGerd Hoffmann     {
2268045df14SGerd Hoffmann         AcpiRsdpData rsdp_data = {
2278045df14SGerd Hoffmann             /* ACPI 2.0: 5.2.4.3 RSDP Structure */
2288045df14SGerd Hoffmann             .revision = 2, /* xsdt needs v2 */
229d07b2286SMarian Postevca             .oem_id = x86ms->oem_id,
2308045df14SGerd Hoffmann             .xsdt_tbl_offset = &xsdt,
2318045df14SGerd Hoffmann             .rsdt_tbl_offset = NULL,
2328045df14SGerd Hoffmann         };
2338045df14SGerd Hoffmann         build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2348045df14SGerd Hoffmann     }
2358045df14SGerd Hoffmann 
2368045df14SGerd Hoffmann     /* Cleanup memory that's no longer used. */
2378045df14SGerd Hoffmann     g_array_free(table_offsets, true);
2388045df14SGerd Hoffmann }
2398045df14SGerd Hoffmann 
acpi_build_no_update(void * build_opaque)2408045df14SGerd Hoffmann static void acpi_build_no_update(void *build_opaque)
2418045df14SGerd Hoffmann {
2428045df14SGerd Hoffmann     /* nothing, microvm tables don't change at runtime */
2438045df14SGerd Hoffmann }
2448045df14SGerd Hoffmann 
acpi_setup_microvm(MicrovmMachineState * mms)2458045df14SGerd Hoffmann void acpi_setup_microvm(MicrovmMachineState *mms)
2468045df14SGerd Hoffmann {
2478045df14SGerd Hoffmann     X86MachineState *x86ms = X86_MACHINE(mms);
2488045df14SGerd Hoffmann     AcpiBuildTables tables;
2498045df14SGerd Hoffmann 
2508045df14SGerd Hoffmann     assert(x86ms->fw_cfg);
2518045df14SGerd Hoffmann 
2528045df14SGerd Hoffmann     if (!x86_machine_is_acpi_enabled(x86ms)) {
2538045df14SGerd Hoffmann         return;
2548045df14SGerd Hoffmann     }
2558045df14SGerd Hoffmann 
2568045df14SGerd Hoffmann     acpi_build_tables_init(&tables);
2578045df14SGerd Hoffmann     acpi_build_microvm(&tables, mms);
2588045df14SGerd Hoffmann 
2598045df14SGerd Hoffmann     /* Now expose it all to Guest */
2606930ba0dSDavid Hildenbrand     acpi_add_rom_blob(acpi_build_no_update, NULL, tables.table_data,
2616930ba0dSDavid Hildenbrand                       ACPI_BUILD_TABLE_FILE);
2626930ba0dSDavid Hildenbrand     acpi_add_rom_blob(acpi_build_no_update, NULL, tables.linker->cmd_blob,
2636930ba0dSDavid Hildenbrand                       ACPI_BUILD_LOADER_FILE);
2646930ba0dSDavid Hildenbrand     acpi_add_rom_blob(acpi_build_no_update, NULL, tables.rsdp,
2656930ba0dSDavid Hildenbrand                       ACPI_BUILD_RSDP_FILE);
2668045df14SGerd Hoffmann 
2678045df14SGerd Hoffmann     acpi_build_tables_cleanup(&tables, false);
2688045df14SGerd Hoffmann }
269