2 * Support for generating ACPI tables and passing them to Guests
4 * RISC-V virt ACPI generation
6 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
7 * Copyright (C) 2006 Fabrice Bellard
8 * Copyright (C) 2013 Red Hat Inc
9 * Copyright (c) 2015 HUAWEI TECHNOLOGIES CO.,LTD.
10 * Copyright (C) 2021-2023 Ventana Micro Systems Inc
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "qemu/osdep.h"
27 #include "hw/acpi/acpi-defs.h"
28 #include "hw/acpi/acpi.h"
29 #include "hw/acpi/aml-build.h"
30 #include "hw/acpi/utils.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "sysemu/reset.h"
34 #include "migration/vmstate.h"
35 #include "hw/riscv/virt.h"
36 #include "hw/riscv/numa.h"
37 #include "hw/intc/riscv_aclint.h"
39 #define ACPI_BUILD_TABLE_SIZE 0x20000
41 typedef struct AcpiBuildState
{
42 /* Copy of table in RAM (for patching) */
43 MemoryRegion
*table_mr
;
44 MemoryRegion
*rsdp_mr
;
45 MemoryRegion
*linker_mr
;
46 /* Is table patched? */
50 static void acpi_align_size(GArray
*blob
, unsigned align
)
53 * Align size to multiple of given size. This reduces the chance
54 * we need to change size in the future (breaking cross version migration).
56 g_array_set_size(blob
, ROUND_UP(acpi_data_len(blob
), align
));
59 static void riscv_acpi_madt_add_rintc(uint32_t uid
,
60 const CPUArchIdList
*arch_ids
,
63 uint64_t hart_id
= arch_ids
->cpus
[uid
].arch_id
;
65 build_append_int_noprefix(entry
, 0x18, 1); /* Type */
66 build_append_int_noprefix(entry
, 20, 1); /* Length */
67 build_append_int_noprefix(entry
, 1, 1); /* Version */
68 build_append_int_noprefix(entry
, 0, 1); /* Reserved */
69 build_append_int_noprefix(entry
, 0x1, 4); /* Flags */
70 build_append_int_noprefix(entry
, hart_id
, 8); /* Hart ID */
71 build_append_int_noprefix(entry
, uid
, 4); /* ACPI Processor UID */
74 static void acpi_dsdt_add_cpus(Aml
*scope
, RISCVVirtState
*s
)
76 MachineClass
*mc
= MACHINE_GET_CLASS(s
);
77 MachineState
*ms
= MACHINE(s
);
78 const CPUArchIdList
*arch_ids
= mc
->possible_cpu_arch_ids(ms
);
80 for (int i
= 0; i
< arch_ids
->len
; i
++) {
82 GArray
*madt_buf
= g_array_new(0, 1, 1);
84 dev
= aml_device("C%.03X", i
);
85 aml_append(dev
, aml_name_decl("_HID", aml_string("ACPI0007")));
86 aml_append(dev
, aml_name_decl("_UID",
87 aml_int(arch_ids
->cpus
[i
].arch_id
)));
89 /* build _MAT object */
90 riscv_acpi_madt_add_rintc(i
, arch_ids
, madt_buf
);
91 aml_append(dev
, aml_name_decl("_MAT",
92 aml_buffer(madt_buf
->len
,
93 (uint8_t *)madt_buf
->data
)));
94 g_array_free(madt_buf
, true);
96 aml_append(scope
, dev
);
100 static void acpi_dsdt_add_fw_cfg(Aml
*scope
, const MemMapEntry
*fw_cfg_memmap
)
102 Aml
*dev
= aml_device("FWCF");
103 aml_append(dev
, aml_name_decl("_HID", aml_string("QEMU0002")));
105 /* device present, functioning, decoding, not shown in UI */
106 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
107 aml_append(dev
, aml_name_decl("_CCA", aml_int(1)));
109 Aml
*crs
= aml_resource_template();
110 aml_append(crs
, aml_memory32_fixed(fw_cfg_memmap
->base
,
111 fw_cfg_memmap
->size
, AML_READ_WRITE
));
112 aml_append(dev
, aml_name_decl("_CRS", crs
));
113 aml_append(scope
, dev
);
116 /* RHCT Node[N] starts at offset 56 */
117 #define RHCT_NODE_ARRAY_OFFSET 56
120 * ACPI spec, Revision 6.5+
121 * 5.2.36 RISC-V Hart Capabilities Table (RHCT)
122 * REF: https://github.com/riscv-non-isa/riscv-acpi/issues/16
123 * https://drive.google.com/file/d/1nP3nFiH4jkPMp6COOxP6123DCZKR-tia/view
125 static void build_rhct(GArray
*table_data
,
129 MachineClass
*mc
= MACHINE_GET_CLASS(s
);
130 MachineState
*ms
= MACHINE(s
);
131 const CPUArchIdList
*arch_ids
= mc
->possible_cpu_arch_ids(ms
);
132 size_t len
, aligned_len
;
133 uint32_t isa_offset
, num_rhct_nodes
;
137 AcpiTable table
= { .sig
= "RHCT", .rev
= 1, .oem_id
= s
->oem_id
,
138 .oem_table_id
= s
->oem_table_id
};
140 acpi_table_begin(&table
, table_data
);
142 build_append_int_noprefix(table_data
, 0x0, 4); /* Reserved */
144 /* Time Base Frequency */
145 build_append_int_noprefix(table_data
,
146 RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ
, 8);
148 /* ISA + N hart info */
149 num_rhct_nodes
= 1 + ms
->smp
.cpus
;
151 /* Number of RHCT nodes*/
152 build_append_int_noprefix(table_data
, num_rhct_nodes
, 4);
154 /* Offset to the RHCT node array */
155 build_append_int_noprefix(table_data
, RHCT_NODE_ARRAY_OFFSET
, 4);
157 /* ISA String Node */
158 isa_offset
= table_data
->len
- table
.table_offset
;
159 build_append_int_noprefix(table_data
, 0, 2); /* Type 0 */
161 cpu
= &s
->soc
[0].harts
[0];
162 isa
= riscv_isa_string(cpu
);
163 len
= 8 + strlen(isa
) + 1;
164 aligned_len
= (len
% 2) ? (len
+ 1) : len
;
166 build_append_int_noprefix(table_data
, aligned_len
, 2); /* Length */
167 build_append_int_noprefix(table_data
, 0x1, 2); /* Revision */
169 /* ISA string length including NUL */
170 build_append_int_noprefix(table_data
, strlen(isa
) + 1, 2);
171 g_array_append_vals(table_data
, isa
, strlen(isa
) + 1); /* ISA string */
173 if (aligned_len
!= len
) {
174 build_append_int_noprefix(table_data
, 0x0, 1); /* Optional Padding */
178 for (int i
= 0; i
< arch_ids
->len
; i
++) {
179 build_append_int_noprefix(table_data
, 0xFFFF, 2); /* Type */
180 build_append_int_noprefix(table_data
, 16, 2); /* Length */
181 build_append_int_noprefix(table_data
, 0x1, 2); /* Revision */
182 build_append_int_noprefix(table_data
, 1, 2); /* Number of offsets */
183 build_append_int_noprefix(table_data
, i
, 4); /* ACPI Processor UID */
184 build_append_int_noprefix(table_data
, isa_offset
, 4); /* Offsets[0] */
187 acpi_table_end(linker
, &table
);
191 static void build_fadt_rev6(GArray
*table_data
,
194 unsigned dsdt_tbl_offset
)
196 AcpiFadtData fadt
= {
199 .flags
= 1 << ACPI_FADT_F_HW_REDUCED_ACPI
,
200 .xdsdt_tbl_offset
= &dsdt_tbl_offset
,
203 build_fadt(table_data
, linker
, &fadt
, s
->oem_id
, s
->oem_table_id
);
207 static void build_dsdt(GArray
*table_data
,
212 const MemMapEntry
*memmap
= s
->memmap
;
213 AcpiTable table
= { .sig
= "DSDT", .rev
= 2, .oem_id
= s
->oem_id
,
214 .oem_table_id
= s
->oem_table_id
};
217 acpi_table_begin(&table
, table_data
);
218 dsdt
= init_aml_allocator();
221 * When booting the VM with UEFI, UEFI takes ownership of the RTC hardware.
222 * While UEFI can use libfdt to disable the RTC device node in the DTB that
223 * it passes to the OS, it cannot modify AML. Therefore, we won't generate
224 * the RTC ACPI device at all when using UEFI.
226 scope
= aml_scope("\\_SB");
227 acpi_dsdt_add_cpus(scope
, s
);
229 acpi_dsdt_add_fw_cfg(scope
, &memmap
[VIRT_FW_CFG
]);
231 aml_append(dsdt
, scope
);
233 /* copy AML table into ACPI tables blob and patch header there */
234 g_array_append_vals(table_data
, dsdt
->buf
->data
, dsdt
->buf
->len
);
236 acpi_table_end(linker
, &table
);
237 free_aml_allocator();
241 * ACPI spec, Revision 6.5+
242 * 5.2.12 Multiple APIC Description Table (MADT)
243 * REF: https://github.com/riscv-non-isa/riscv-acpi/issues/15
244 * https://drive.google.com/file/d/1R6k4MshhN3WTT-hwqAquu5nX6xSEqK2l/view
246 static void build_madt(GArray
*table_data
,
250 MachineClass
*mc
= MACHINE_GET_CLASS(s
);
251 MachineState
*ms
= MACHINE(s
);
252 const CPUArchIdList
*arch_ids
= mc
->possible_cpu_arch_ids(ms
);
254 AcpiTable table
= { .sig
= "APIC", .rev
= 6, .oem_id
= s
->oem_id
,
255 .oem_table_id
= s
->oem_table_id
};
257 acpi_table_begin(&table
, table_data
);
258 /* Local Interrupt Controller Address */
259 build_append_int_noprefix(table_data
, 0, 4);
260 build_append_int_noprefix(table_data
, 0, 4); /* MADT Flags */
262 /* RISC-V Local INTC structures per HART */
263 for (int i
= 0; i
< arch_ids
->len
; i
++) {
264 riscv_acpi_madt_add_rintc(i
, arch_ids
, table_data
);
267 acpi_table_end(linker
, &table
);
270 static void virt_acpi_build(RISCVVirtState
*s
, AcpiBuildTables
*tables
)
272 GArray
*table_offsets
;
274 GArray
*tables_blob
= tables
->table_data
;
276 table_offsets
= g_array_new(false, true,
279 bios_linker_loader_alloc(tables
->linker
,
280 ACPI_BUILD_TABLE_FILE
, tables_blob
,
283 /* DSDT is pointed to by FADT */
284 dsdt
= tables_blob
->len
;
285 build_dsdt(tables_blob
, tables
->linker
, s
);
287 /* FADT and others pointed to by XSDT */
288 acpi_add_table(table_offsets
, tables_blob
);
289 build_fadt_rev6(tables_blob
, tables
->linker
, s
, dsdt
);
291 acpi_add_table(table_offsets
, tables_blob
);
292 build_madt(tables_blob
, tables
->linker
, s
);
294 acpi_add_table(table_offsets
, tables_blob
);
295 build_rhct(tables_blob
, tables
->linker
, s
);
297 /* XSDT is pointed to by RSDP */
298 xsdt
= tables_blob
->len
;
299 build_xsdt(tables_blob
, tables
->linker
, table_offsets
, s
->oem_id
,
302 /* RSDP is in FSEG memory, so allocate it separately */
304 AcpiRsdpData rsdp_data
= {
307 .xsdt_tbl_offset
= &xsdt
,
308 .rsdt_tbl_offset
= NULL
,
310 build_rsdp(tables
->rsdp
, tables
->linker
, &rsdp_data
);
314 * The align size is 128, warn if 64k is not enough therefore
315 * the align size could be resized.
317 if (tables_blob
->len
> ACPI_BUILD_TABLE_SIZE
/ 2) {
318 warn_report("ACPI table size %u exceeds %d bytes,"
319 " migration may not work",
320 tables_blob
->len
, ACPI_BUILD_TABLE_SIZE
/ 2);
321 error_printf("Try removing some objects.");
324 acpi_align_size(tables_blob
, ACPI_BUILD_TABLE_SIZE
);
326 /* Clean up memory that's no longer used */
327 g_array_free(table_offsets
, true);
330 static void acpi_ram_update(MemoryRegion
*mr
, GArray
*data
)
332 uint32_t size
= acpi_data_len(data
);
335 * Make sure RAM size is correct - in case it got changed
338 memory_region_ram_resize(mr
, size
, &error_abort
);
340 memcpy(memory_region_get_ram_ptr(mr
), data
->data
, size
);
341 memory_region_set_dirty(mr
, 0, size
);
344 static void virt_acpi_build_update(void *build_opaque
)
346 AcpiBuildState
*build_state
= build_opaque
;
347 AcpiBuildTables tables
;
349 /* No state to update or already patched? Nothing to do. */
350 if (!build_state
|| build_state
->patched
) {
354 build_state
->patched
= true;
356 acpi_build_tables_init(&tables
);
358 virt_acpi_build(RISCV_VIRT_MACHINE(qdev_get_machine()), &tables
);
360 acpi_ram_update(build_state
->table_mr
, tables
.table_data
);
361 acpi_ram_update(build_state
->rsdp_mr
, tables
.rsdp
);
362 acpi_ram_update(build_state
->linker_mr
, tables
.linker
->cmd_blob
);
364 acpi_build_tables_cleanup(&tables
, true);
367 static void virt_acpi_build_reset(void *build_opaque
)
369 AcpiBuildState
*build_state
= build_opaque
;
370 build_state
->patched
= false;
373 static const VMStateDescription vmstate_virt_acpi_build
= {
374 .name
= "virt_acpi_build",
376 .minimum_version_id
= 1,
377 .fields
= (const VMStateField
[]) {
378 VMSTATE_BOOL(patched
, AcpiBuildState
),
379 VMSTATE_END_OF_LIST()
383 void virt_acpi_setup(RISCVVirtState
*s
)
385 AcpiBuildTables tables
;
386 AcpiBuildState
*build_state
;
388 build_state
= g_malloc0(sizeof *build_state
);
390 acpi_build_tables_init(&tables
);
391 virt_acpi_build(s
, &tables
);
393 /* Now expose it all to Guest */
394 build_state
->table_mr
= acpi_add_rom_blob(virt_acpi_build_update
,
395 build_state
, tables
.table_data
,
396 ACPI_BUILD_TABLE_FILE
);
397 assert(build_state
->table_mr
!= NULL
);
399 build_state
->linker_mr
= acpi_add_rom_blob(virt_acpi_build_update
,
401 tables
.linker
->cmd_blob
,
402 ACPI_BUILD_LOADER_FILE
);
404 build_state
->rsdp_mr
= acpi_add_rom_blob(virt_acpi_build_update
,
405 build_state
, tables
.rsdp
,
406 ACPI_BUILD_RSDP_FILE
);
408 qemu_register_reset(virt_acpi_build_reset
, build_state
);
409 virt_acpi_build_reset(build_state
);
410 vmstate_register(NULL
, 0, &vmstate_virt_acpi_build
, build_state
);
413 * Clean up tables but don't free the memory: we track it
416 acpi_build_tables_cleanup(&tables
, false);