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