xref: /openbmc/qemu/hw/i386/acpi-microvm.c (revision effd60c8)
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 "qemu/cutils.h"
25 #include "qapi/error.h"
26 
27 #include "exec/memory.h"
28 #include "hw/acpi/acpi.h"
29 #include "hw/acpi/acpi_aml_interface.h"
30 #include "hw/acpi/aml-build.h"
31 #include "hw/acpi/bios-linker-loader.h"
32 #include "hw/acpi/generic_event_device.h"
33 #include "hw/acpi/utils.h"
34 #include "hw/acpi/erst.h"
35 #include "hw/i386/fw_cfg.h"
36 #include "hw/i386/microvm.h"
37 #include "hw/pci/pci.h"
38 #include "hw/pci/pcie_host.h"
39 #include "hw/usb/xhci.h"
40 #include "hw/virtio/virtio-acpi.h"
41 #include "hw/virtio/virtio-mmio.h"
42 #include "hw/input/i8042.h"
43 
44 #include "acpi-common.h"
45 #include "acpi-microvm.h"
46 
47 #include CONFIG_DEVICES
48 
49 static void acpi_dsdt_add_virtio(Aml *scope,
50                                  MicrovmMachineState *mms)
51 {
52     gchar *separator;
53     long int index;
54     BusState *bus;
55     BusChild *kid;
56 
57     bus = sysbus_get_default();
58     QTAILQ_FOREACH(kid, &bus->children, sibling) {
59         Object *obj = object_dynamic_cast(OBJECT(kid->child),
60                                           TYPE_VIRTIO_MMIO);
61 
62         if (obj) {
63             VirtIOMMIOProxy *mmio = VIRTIO_MMIO(obj);
64             VirtioBusState *mmio_virtio_bus = &mmio->bus;
65             BusState *mmio_bus = &mmio_virtio_bus->parent_obj;
66 
67             if (QTAILQ_EMPTY(&mmio_bus->children)) {
68                 continue;
69             }
70             separator = g_strrstr(mmio_bus->name, ".");
71             if (!separator) {
72                 continue;
73             }
74             if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) {
75                 continue;
76             }
77 
78             uint32_t irq = mms->virtio_irq_base + index;
79             hwaddr base = VIRTIO_MMIO_BASE + index * 512;
80             hwaddr size = 512;
81             virtio_acpi_dsdt_add(scope, base, size, irq, index, 1);
82         }
83     }
84 }
85 
86 static void acpi_dsdt_add_xhci(Aml *scope, MicrovmMachineState *mms)
87 {
88     if (machine_usb(MACHINE(mms))) {
89         xhci_sysbus_build_aml(scope, MICROVM_XHCI_BASE, MICROVM_XHCI_IRQ);
90     }
91 }
92 
93 static void acpi_dsdt_add_pci(Aml *scope, MicrovmMachineState *mms)
94 {
95     if (mms->pcie != ON_OFF_AUTO_ON) {
96         return;
97     }
98 
99     acpi_dsdt_add_gpex(scope, &mms->gpex);
100 }
101 
102 static void
103 build_dsdt_microvm(GArray *table_data, BIOSLinker *linker,
104                    MicrovmMachineState *mms)
105 {
106     X86MachineState *x86ms = X86_MACHINE(mms);
107     Aml *dsdt, *sb_scope, *scope, *pkg;
108     bool ambiguous;
109     Object *isabus;
110     AcpiTable table = { .sig = "DSDT", .rev = 2, .oem_id = x86ms->oem_id,
111                         .oem_table_id = x86ms->oem_table_id };
112 
113     isabus = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
114     assert(isabus);
115     assert(!ambiguous);
116 
117     acpi_table_begin(&table, table_data);
118     dsdt = init_aml_allocator();
119 
120     sb_scope = aml_scope("_SB");
121     fw_cfg_add_acpi_dsdt(sb_scope, x86ms->fw_cfg);
122     qbus_build_aml(BUS(isabus), sb_scope);
123     build_ged_aml(sb_scope, GED_DEVICE, x86ms->acpi_dev,
124                   GED_MMIO_IRQ, AML_SYSTEM_MEMORY, GED_MMIO_BASE);
125     acpi_dsdt_add_power_button(sb_scope);
126     acpi_dsdt_add_virtio(sb_scope, mms);
127     acpi_dsdt_add_xhci(sb_scope, mms);
128     acpi_dsdt_add_pci(sb_scope, mms);
129     aml_append(dsdt, sb_scope);
130 
131     /* ACPI 5.0: Table 7-209 System State Package */
132     scope = aml_scope("\\");
133     pkg = aml_package(4);
134     aml_append(pkg, aml_int(ACPI_GED_SLP_TYP_S5));
135     aml_append(pkg, aml_int(0)); /* ignored */
136     aml_append(pkg, aml_int(0)); /* reserved */
137     aml_append(pkg, aml_int(0)); /* reserved */
138     aml_append(scope, aml_name_decl("_S5", pkg));
139     aml_append(dsdt, scope);
140 
141     /* copy AML bytecode into ACPI tables blob */
142     g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
143 
144     acpi_table_end(linker, &table);
145     free_aml_allocator();
146 }
147 
148 static void acpi_build_microvm(AcpiBuildTables *tables,
149                                MicrovmMachineState *mms)
150 {
151     MachineState *machine = MACHINE(mms);
152     X86MachineState *x86ms = X86_MACHINE(mms);
153     GArray *table_offsets;
154     GArray *tables_blob = tables->table_data;
155     unsigned dsdt, xsdt;
156     AcpiFadtData pmfadt = {
157         /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */
158         .rev = 5,
159         .flags = ((1 << ACPI_FADT_F_HW_REDUCED_ACPI) |
160                   (1 << ACPI_FADT_F_RESET_REG_SUP)),
161 
162         /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */
163         .sleep_ctl = {
164             .space_id = AML_AS_SYSTEM_MEMORY,
165             .bit_width = 8,
166             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_CTL,
167         },
168         .sleep_sts = {
169             .space_id = AML_AS_SYSTEM_MEMORY,
170             .bit_width = 8,
171             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_STS,
172         },
173 
174         /* ACPI 5.0: 4.8.3.6 Reset Register */
175         .reset_reg = {
176             .space_id = AML_AS_SYSTEM_MEMORY,
177             .bit_width = 8,
178             .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_RESET,
179         },
180         .reset_val = ACPI_GED_RESET_VALUE,
181         /*
182          * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture
183          * Flags, bit offset 1 - 8042.
184          */
185         .iapc_boot_arch = iapc_boot_arch_8042(),
186     };
187 
188     table_offsets = g_array_new(false, true /* clear */,
189                                         sizeof(uint32_t));
190     bios_linker_loader_alloc(tables->linker,
191                              ACPI_BUILD_TABLE_FILE, tables_blob,
192                              64 /* Ensure FACS is aligned */,
193                              false /* high memory */);
194 
195     dsdt = tables_blob->len;
196     build_dsdt_microvm(tables_blob, tables->linker, mms);
197 
198     pmfadt.dsdt_tbl_offset = &dsdt;
199     pmfadt.xdsdt_tbl_offset = &dsdt;
200     acpi_add_table(table_offsets, tables_blob);
201     build_fadt(tables_blob, tables->linker, &pmfadt, x86ms->oem_id,
202                x86ms->oem_table_id);
203 
204     acpi_add_table(table_offsets, tables_blob);
205     acpi_build_madt(tables_blob, tables->linker, X86_MACHINE(machine),
206                     x86ms->oem_id, x86ms->oem_table_id);
207 
208 #ifdef CONFIG_ACPI_ERST
209     {
210         Object *erst_dev;
211         erst_dev = find_erst_dev();
212         if (erst_dev) {
213             acpi_add_table(table_offsets, tables_blob);
214             build_erst(tables_blob, tables->linker, erst_dev,
215                        x86ms->oem_id, x86ms->oem_table_id);
216         }
217     }
218 #endif
219 
220     xsdt = tables_blob->len;
221     build_xsdt(tables_blob, tables->linker, table_offsets, x86ms->oem_id,
222                x86ms->oem_table_id);
223 
224     /* RSDP is in FSEG memory, so allocate it separately */
225     {
226         AcpiRsdpData rsdp_data = {
227             /* ACPI 2.0: 5.2.4.3 RSDP Structure */
228             .revision = 2, /* xsdt needs v2 */
229             .oem_id = x86ms->oem_id,
230             .xsdt_tbl_offset = &xsdt,
231             .rsdt_tbl_offset = NULL,
232         };
233         build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
234     }
235 
236     /* Cleanup memory that's no longer used. */
237     g_array_free(table_offsets, true);
238 }
239 
240 static void acpi_build_no_update(void *build_opaque)
241 {
242     /* nothing, microvm tables don't change at runtime */
243 }
244 
245 void acpi_setup_microvm(MicrovmMachineState *mms)
246 {
247     X86MachineState *x86ms = X86_MACHINE(mms);
248     AcpiBuildTables tables;
249 
250     assert(x86ms->fw_cfg);
251 
252     if (!x86_machine_is_acpi_enabled(x86ms)) {
253         return;
254     }
255 
256     acpi_build_tables_init(&tables);
257     acpi_build_microvm(&tables, mms);
258 
259     /* Now expose it all to Guest */
260     acpi_add_rom_blob(acpi_build_no_update, NULL, tables.table_data,
261                       ACPI_BUILD_TABLE_FILE);
262     acpi_add_rom_blob(acpi_build_no_update, NULL, tables.linker->cmd_blob,
263                       ACPI_BUILD_LOADER_FILE);
264     acpi_add_rom_blob(acpi_build_no_update, NULL, tables.rsdp,
265                       ACPI_BUILD_RSDP_FILE);
266 
267     acpi_build_tables_cleanup(&tables, false);
268 }
269