This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
reduced.c
328 lines (268 loc) · 10.6 KB
/
reduced.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* HW reduced ACPI support
*
* Copyright (c) 2018 Intel Corportation
* Copyright (C) 2013 Red Hat Inc
* Copyright (c) 2015 HUAWEI TECHNOLOGIES CO.,LTD.
* Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
* Copyright (C) 2006 Fabrice Bellard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu-common.h"
#include "hw/acpi/acpi-defs.h"
#include "hw/acpi/acpi.h"
#include "hw/acpi/aml-build.h"
#include "hw/acpi/bios-linker-loader.h"
#include "hw/acpi/cpu.h"
#include "hw/acpi/pc-hotplug.h"
#include "hw/acpi/reduced.h"
#include "hw/acpi/memory_hotplug.h"
#include "hw/acpi/ged.h"
#include "qemu/range.h"
#include "hw/nvram/fw_cfg.h"
#include "hw/pci/pcie_host.h"
#include "hw/pci/pci.h"
#include "hw/loader.h"
#include "hw/hw.h"
#include "sysemu/numa.h"
#include "migration/vmstate.h"
static void acpi_dsdt_add_memory_hotplug(MachineState *ms, Aml *dsdt)
{
uint32_t nr_mem = ms->ram_slots;
build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB", NULL);
}
static void acpi_dsdt_add_cpus(MachineState *ms, Aml *dsdt, int smp_cpus, AcpiConfiguration *conf)
{
CPUHotplugFeatures opts = {
.acpi_1_compatible = false,
.has_legacy_cphp = false,
};
build_cpus_aml(dsdt, ms, opts, conf->cpu_hotplug_io_base,
"\\_SB", NULL);
}
static void acpi_dsdt_add_ged(Aml *scope, AcpiConfiguration *conf)
{
if (!conf->ged_events || !conf->ged_events_size) {
return;
}
build_ged_aml(scope, "\\_SB."GED_DEVICE, conf->ged_irq,
conf->ged_events, conf->ged_events_size);
}
static void acpi_dsdt_add_sleep_state(Aml *scope)
{
Aml *pkg = aml_package(1);
aml_append(pkg, aml_int(ACPI_REDUCED_SLEEP_LEVEL));
aml_append(scope, aml_name_decl("\\_S5_", pkg));
}
/* DSDT */
static void build_dsdt(MachineState *ms, GArray *table_data, BIOSLinker *linker, AcpiPciBus *pci_host, AcpiConfiguration *conf)
{
Aml *dsdt;
dsdt = init_aml_allocator();
/* Reserve space for header */
acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
if (pci_host->pci_bus) {
acpi_dsdt_add_pci_bus(dsdt, pci_host);
}
acpi_dsdt_add_memory_hotplug(ms, dsdt);
acpi_dsdt_add_cpus(ms, dsdt, smp_cpus, conf);
acpi_dsdt_add_ged(dsdt, conf);
acpi_dsdt_add_sleep_state(dsdt);
/* copy AML table into ACPI tables blob and patch header there */
g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
build_header(linker, table_data,
(void *)(table_data->data + table_data->len - dsdt->buf->len),
"DSDT", dsdt->buf->len, 2, NULL, NULL);
free_aml_allocator();
}
static void build_fadt_reduced(GArray *table_data, BIOSLinker *linker,
unsigned dsdt_tbl_offset)
{
/* ACPI v5.1 */
AcpiFadtData fadt = {
.rev = 5,
.minor_ver = 1,
.flags = (1 << ACPI_FADT_F_HW_REDUCED_ACPI) |
(1 << ACPI_FADT_F_RESET_REG_SUP),
.dsdt_tbl_offset = &dsdt_tbl_offset,
.xdsdt_tbl_offset = &dsdt_tbl_offset,
.arm_boot_arch = 0,
.reset_reg = { .space_id = AML_AS_SYSTEM_IO,
.bit_width = 8, .bit_offset = 0,
.address = ACPI_REDUCED_RESET_IOPORT },
.reset_val = ACPI_REDUCED_RESET_VALUE,
.sleep_control_reg = { .space_id = AML_AS_SYSTEM_IO,
.bit_width = 8, .bit_offset = 0,
.address = ACPI_REDUCED_SLEEP_CONTROL_IOPORT },
.sleep_status_reg = { .space_id = AML_AS_SYSTEM_IO,
.bit_width = 8, .bit_offset = 0,
.address = ACPI_REDUCED_SLEEP_STATUS_IOPORT },
.pm_tmr = { .space_id = AML_AS_SYSTEM_IO,
.bit_width = 32, .bit_offset = 0,
.address = ACPI_REDUCED_PMTIMER_IOPORT },
};
build_fadt(table_data, linker, &fadt, NULL, NULL);
}
static void acpi_reduced_build(MachineState *ms, AcpiBuildTables *tables, AcpiConfiguration *conf)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
GArray *table_offsets;
unsigned dsdt, xsdt;
Object *pci_host;
PCIBus *bus = NULL;
Range pci_hole, pci_hole64;
AcpiMcfgInfo mcfg;
GArray *tables_blob = tables->table_data;
acpi_get_pci_holes(&pci_hole, &pci_hole64);
table_offsets = g_array_new(false, true /* clear */,
sizeof(uint32_t));
bios_linker_loader_alloc(tables->linker,
ACPI_BUILD_TABLE_FILE, tables_blob,
64, false /* high memory */);
pci_host = acpi_get_pci_host();
if (pci_host) {
bus = PCI_HOST_BRIDGE(pci_host)->bus;
}
AcpiPciBus acpi_pci_host = {
.pci_bus = bus,
.pci_hole = &pci_hole,
.pci_hole64 = &pci_hole64,
.pci_segment = 0,
.acpi_iobase_addr = VIRT_ACPI_PCI_HOTPLUG_IO_BASE,
};
/* DSDT is pointed to by FADT */
dsdt = tables_blob->len;
build_dsdt(ms, tables_blob, tables->linker, &acpi_pci_host, conf);
/* FADT pointed to by RSDT */
acpi_add_table(table_offsets, tables_blob);
build_fadt_reduced(tables_blob, tables->linker, dsdt);
/* MADT pointed to by RSDT */
acpi_add_table(table_offsets, tables_blob);
mc->firmware_build_methods.acpi.madt(tables_blob, tables->linker, ms, conf);
if (conf->numa_nodes) {
acpi_add_table(table_offsets, tables_blob);
mc->firmware_build_methods.acpi.srat(tables_blob, tables->linker, ms, conf);
if (have_numa_distance) {
acpi_add_table(table_offsets, tables_blob);
mc->firmware_build_methods.acpi.slit(tables_blob, tables->linker);
}
}
if (acpi_get_mcfg(&mcfg)) {
acpi_add_table(table_offsets, tables_blob);
mc->firmware_build_methods.acpi.mcfg(tables_blob, tables->linker, &mcfg);
}
if (ms->nvdimms_state->is_enabled) {
nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
ms->nvdimms_state, ms->ram_slots);
}
/* RSDT is pointed to by RSDP */
xsdt = tables_blob->len;
build_xsdt(tables_blob, tables->linker, table_offsets, NULL, NULL);
AcpiRsdpData rsdp_data = {
.revision = 2,
.oem_id = ACPI_BUILD_APPNAME6,
.xsdt_tbl_offset = &xsdt,
.rsdt_tbl_offset = NULL,
};
/* RSDP is in FSEG memory, so allocate it separately */
mc->firmware_build_methods.acpi.rsdp(tables->rsdp, tables->linker, &rsdp_data);
acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
/* Cleanup memory that's no longer used. */
g_array_free(table_offsets, true);
}
static void acpi_ram_update(MemoryRegion *mr, GArray *data)
{
uint32_t size = acpi_data_len(data);
/* Make sure RAM size is correct - in case it got changed
* e.g. by migration */
memory_region_ram_resize(mr, size, &error_abort);
memcpy(memory_region_get_ram_ptr(mr), data->data, size);
memory_region_set_dirty(mr, 0, size);
}
static void acpi_reduced_build_update(void *build_opaque)
{
MachineState *ms = MACHINE(build_opaque);
AcpiBuildState *build_state = ms->firmware_build_state.acpi.state;
AcpiConfiguration *conf = ms->firmware_build_state.acpi.conf;
AcpiBuildTables tables;
/* No ACPI configuration? Nothing to do. */
if (!conf) {
return;
}
/* No state to update or already patched? Nothing to do. */
if (!build_state || build_state->patched) {
return;
}
build_state->patched = true;
acpi_build_tables_init(&tables);
acpi_reduced_build(ms, &tables, conf);
acpi_ram_update(build_state->table_mr, tables.table_data);
acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
acpi_build_tables_cleanup(&tables, true);
}
static void acpi_reduced_build_reset(void *build_opaque)
{
MachineState *ms = MACHINE(build_opaque);
AcpiBuildState *build_state = ms->firmware_build_state.acpi.state;
build_state->patched = false;
}
static MemoryRegion *acpi_add_rom_blob(MachineState *ms,
GArray *blob, const char *name,
uint64_t max_size)
{
return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
name, acpi_reduced_build_update, ms, NULL, true);
}
static const VMStateDescription vmstate_acpi_reduced_build = {
.name = "acpi_reduced_build",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_BOOL(patched, AcpiBuildState),
VMSTATE_END_OF_LIST()
},
};
void acpi_reduced_setup(MachineState *machine, AcpiConfiguration *conf)
{
AcpiBuildTables tables;
AcpiBuildState *build_state;
build_state = g_malloc0(sizeof(*build_state));
machine->firmware_build_state.acpi.state = build_state;
machine->firmware_build_state.acpi.conf = conf;
acpi_build_tables_init(&tables);
acpi_reduced_build(machine, &tables, conf);
if (conf->fw_cfg) {
/* Now expose it all to Guest */
build_state->table_mr = acpi_add_rom_blob(machine, tables.table_data,
ACPI_BUILD_TABLE_FILE,
ACPI_BUILD_TABLE_MAX_SIZE);
assert(build_state->table_mr != NULL);
build_state->linker_mr =
acpi_add_rom_blob(machine, tables.linker->cmd_blob,
"etc/table-loader", 0);
fw_cfg_add_file(conf->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
acpi_data_len(tables.tcpalog));
build_state->rsdp_mr = acpi_add_rom_blob(machine, tables.rsdp,
ACPI_BUILD_RSDP_FILE, 0);
}
qemu_register_reset(acpi_reduced_build_reset, machine);
acpi_reduced_build_reset(machine);
vmstate_register(NULL, 0, &vmstate_acpi_reduced_build, machine);
/* Cleanup tables but don't free the memory: we track it
* in build_state.
*/
acpi_build_tables_cleanup(&tables, false);
}