xref: /qemu/hw/i386/acpi-microvm.c (revision 8045df14bccaeec39291c3d83fea2f638656b5d0)
1 /* Support for generating ACPI tables and passing them to Guests
2  *
3  * Copyright (C) 2008-2010  Kevin O'Connor <kevin@koconnor.net>
4  * Copyright (C) 2006 Fabrice Bellard
5  * Copyright (C) 2013 Red Hat Inc
6  *
7  * Author: Michael S. Tsirkin <mst@redhat.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "qemu/osdep.h"
24 #include "qapi/error.h"
25 
26 #include "exec/memory.h"
27 #include "hw/acpi/acpi.h"
28 #include "hw/acpi/aml-build.h"
29 #include "hw/acpi/bios-linker-loader.h"
30 #include "hw/acpi/generic_event_device.h"
31 #include "hw/acpi/utils.h"
32 #include "hw/boards.h"
33 #include "hw/i386/fw_cfg.h"
34 #include "hw/i386/microvm.h"
35 
36 #include "acpi-common.h"
37 #include "acpi-microvm.h"
38 
39 static void
40 build_dsdt_microvm(GArray *table_data, BIOSLinker *linker,
41                    MicrovmMachineState *mms)
42 {
43     X86MachineState *x86ms = X86_MACHINE(mms);
44     Aml *dsdt, *sb_scope, *scope, *pkg;
45     bool ambiguous;
46     Object *isabus;
47 
48     isabus = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
49     assert(isabus);
50     assert(!ambiguous);
51 
52     dsdt = init_aml_allocator();
53 
54     /* Reserve space for header */
55     acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
56 
57     sb_scope = aml_scope("_SB");
58     fw_cfg_add_acpi_dsdt(sb_scope, x86ms->fw_cfg);
59     isa_build_aml(ISA_BUS(isabus), sb_scope);
60     build_ged_aml(sb_scope, GED_DEVICE, HOTPLUG_HANDLER(mms->acpi_dev),
61                   GED_MMIO_IRQ, AML_SYSTEM_MEMORY, GED_MMIO_BASE);
62     acpi_dsdt_add_power_button(sb_scope);
63     aml_append(dsdt, sb_scope);
64 
65     /* ACPI 5.0: Table 7-209 System State Package */
66     scope = aml_scope("\\");
67     pkg = aml_package(4);
68     aml_append(pkg, aml_int(ACPI_GED_SLP_TYP_S5));
69     aml_append(pkg, aml_int(0)); /* ignored */
70     aml_append(pkg, aml_int(0)); /* reserved */
71     aml_append(pkg, aml_int(0)); /* reserved */
72     aml_append(scope, aml_name_decl("_S5", pkg));
73     aml_append(dsdt, scope);
74 
75     /* copy AML table into ACPI tables blob and patch header there */
76     g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
77     build_header(linker, table_data,
78         (void *)(table_data->data + table_data->len - dsdt->buf->len),
79         "DSDT", dsdt->buf->len, 2, NULL, NULL);
80     free_aml_allocator();
81 }
82 
83 static void acpi_build_microvm(AcpiBuildTables *tables,
84                                MicrovmMachineState *mms)
85 {
86     MachineState *machine = MACHINE(mms);
87     GArray *table_offsets;
88     GArray *tables_blob = tables->table_data;
89     unsigned dsdt, xsdt;
90     AcpiFadtData pmfadt = {
91         /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */
92         .rev = 5,
93         .flags = ((1 << ACPI_FADT_F_HW_REDUCED_ACPI) |
94                   (1 << ACPI_FADT_F_RESET_REG_SUP)),
95 
96         /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */
97         .sleep_ctl = {
98             .space_id = AML_AS_SYSTEM_MEMORY,
99             .bit_width = 8,
100             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_CTL,
101         },
102         .sleep_sts = {
103             .space_id = AML_AS_SYSTEM_MEMORY,
104             .bit_width = 8,
105             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_STS,
106         },
107 
108         /* ACPI 5.0: 4.8.3.6 Reset Register */
109         .reset_reg = {
110             .space_id = AML_AS_SYSTEM_MEMORY,
111             .bit_width = 8,
112             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_RESET,
113         },
114         .reset_val = ACPI_GED_RESET_VALUE,
115     };
116 
117     table_offsets = g_array_new(false, true /* clear */,
118                                         sizeof(uint32_t));
119     bios_linker_loader_alloc(tables->linker,
120                              ACPI_BUILD_TABLE_FILE, tables_blob,
121                              64 /* Ensure FACS is aligned */,
122                              false /* high memory */);
123 
124     dsdt = tables_blob->len;
125     build_dsdt_microvm(tables_blob, tables->linker, mms);
126 
127     pmfadt.dsdt_tbl_offset = &dsdt;
128     pmfadt.xdsdt_tbl_offset = &dsdt;
129     acpi_add_table(table_offsets, tables_blob);
130     build_fadt(tables_blob, tables->linker, &pmfadt, NULL, NULL);
131 
132     acpi_add_table(table_offsets, tables_blob);
133     acpi_build_madt(tables_blob, tables->linker, X86_MACHINE(machine),
134                     mms->acpi_dev, false);
135 
136     xsdt = tables_blob->len;
137     build_xsdt(tables_blob, tables->linker, table_offsets, NULL, NULL);
138 
139     /* RSDP is in FSEG memory, so allocate it separately */
140     {
141         AcpiRsdpData rsdp_data = {
142             /* ACPI 2.0: 5.2.4.3 RSDP Structure */
143             .revision = 2, /* xsdt needs v2 */
144             .oem_id = ACPI_BUILD_APPNAME6,
145             .xsdt_tbl_offset = &xsdt,
146             .rsdt_tbl_offset = NULL,
147         };
148         build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
149     }
150 
151     /* Cleanup memory that's no longer used. */
152     g_array_free(table_offsets, true);
153 }
154 
155 static void acpi_build_no_update(void *build_opaque)
156 {
157     /* nothing, microvm tables don't change at runtime */
158 }
159 
160 void acpi_setup_microvm(MicrovmMachineState *mms)
161 {
162     X86MachineState *x86ms = X86_MACHINE(mms);
163     AcpiBuildTables tables;
164 
165     assert(x86ms->fw_cfg);
166 
167     if (!x86_machine_is_acpi_enabled(x86ms)) {
168         return;
169     }
170 
171     acpi_build_tables_init(&tables);
172     acpi_build_microvm(&tables, mms);
173 
174     /* Now expose it all to Guest */
175     acpi_add_rom_blob(acpi_build_no_update, NULL,
176                       tables.table_data,
177                       ACPI_BUILD_TABLE_FILE,
178                       ACPI_BUILD_TABLE_MAX_SIZE);
179     acpi_add_rom_blob(acpi_build_no_update, NULL,
180                       tables.linker->cmd_blob,
181                       "etc/table-loader", 0);
182     acpi_add_rom_blob(acpi_build_no_update, NULL,
183                       tables.rsdp,
184                       ACPI_BUILD_RSDP_FILE, 0);
185 
186     acpi_build_tables_cleanup(&tables, false);
187 }
188