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