Move generic acpi building helpers into dedictated file and this can be shared with other machines.
Signed-off-by: Shannon Zhao <zhaoshengl...@huawei.com> --- hw/acpi/acpi-build-utils.c | 63 ++++++++++++++++++++- hw/i386/acpi-build.c | 111 ++++++++---------------------------- hw/i386/acpi-build.h | 3 + include/hw/acpi/acpi-build-utils.h | 26 ++++++++- 4 files changed, 111 insertions(+), 92 deletions(-) diff --git a/hw/acpi/acpi-build-utils.c b/hw/acpi/acpi-build-utils.c index 59873e3..aa32fe4 100644 --- a/hw/acpi/acpi-build-utils.c +++ b/hw/acpi/acpi-build-utils.c @@ -881,7 +881,7 @@ AcpiAml acpi_qword_memory(acpiDecode dec, acpiMinFixed min_fixed, /* ACPI 5.0: 20.2.1 Table and Table Header Encoding */ AcpiAml acpi_def_block(const char *signature, uint8_t revision, const char *oem_id, const char *oem_table_id, - uint32_t oem_revision) + uint32_t oem_revision, const char *asl_compiler_id) { int len; AcpiAml var = aml_allocate_internal(0, DEF_BLOCK); @@ -903,8 +903,67 @@ AcpiAml acpi_def_block(const char *signature, uint8_t revision, g_array_append_vals(var.buf, "\0\0\0\0\0\0\0\0", 8 - len); build_append_value(var.buf, oem_revision, 4); - g_array_append_vals(var.buf, ACPI_BUILD_APPNAME4, 4); /* asl_compiler_id */ + g_array_append_vals(var.buf, asl_compiler_id, 4); /* asl_compiler_id */ build_append_value(var.buf, 1, 4); /* asl_compiler_revision */ return var; } + +void +build_header(GArray *linker, GArray *table_data, AcpiTableHeader *h, + const char *sig, const char *ome_id, const char *asl_id, + int len, uint8_t rev) +{ + memcpy(&h->signature, sig, 4); + h->length = cpu_to_le32(len); + h->revision = rev; + memcpy(h->oem_id, ome_id, 6); + memcpy(h->oem_table_id, asl_id, 4); + memcpy(h->oem_table_id + 4, sig, 4); + h->oem_revision = cpu_to_le32(1); + memcpy(h->asl_compiler_id, asl_id, 4); + h->asl_compiler_revision = cpu_to_le32(1); + h->checksum = 0; + /* Checksum to be filled in by Guest linker */ + bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE, + table_data->data, h, len, &h->checksum); +} + +void *acpi_data_push(GArray *table_data, uint64_t size) +{ + uint64_t off = table_data->len; + g_array_set_size(table_data, off + size); + return table_data->data + off; +} + +uint64_t acpi_data_len(GArray *table) +{ +#if GLIB_CHECK_VERSION(2, 22, 0) + assert(g_array_get_element_size(table) == 1); +#endif + return table->len; +} + +void acpi_add_table(GArray *table_offsets, GArray *table_data) +{ + uint64_t offset = cpu_to_le64(table_data->len); + g_array_append_val(table_offsets, offset); +} + +void acpi_build_tables_init(AcpiBuildTables *tables) +{ + tables->rsdp = g_array_new(false, true /* clear */, 1); + tables->table_data.buf = g_array_new(false, true /* clear */, 1); + tables->tcpalog = g_array_new(false, true /* clear */, 1); + tables->linker = bios_linker_loader_init(); + tables->table_data.linker = tables->linker; +} + +void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre) +{ + void *linker_data = bios_linker_loader_cleanup(tables->linker); + g_free(linker_data); + g_array_free(tables->rsdp, mfre); + g_array_free(tables->table_data.buf, true); + g_array_free(tables->tcpalog, mfre); +} diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index f22f6d6..2015760 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -70,9 +70,6 @@ #define ACPI_BUILD_TABLE_SIZE 0x20000 -/* Reserve RAM space for tables: add another order of magnitude. */ -#define ACPI_BUILD_TABLE_MAX_SIZE 0x200000 - /* #define DEBUG_ACPI_BUILD */ #ifdef DEBUG_ACPI_BUILD #define ACPI_BUILD_DPRINTF(fmt, ...) \ @@ -261,49 +258,9 @@ static void acpi_get_pci_info(PcPciInfo *info) NULL); } -#define ACPI_BUILD_APPNAME "Bochs" -#define ACPI_BUILD_APPNAME6 "BOCHS " - -#define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp" -#define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log" - -static void -build_header(GArray *linker, GArray *table_data, - AcpiTableHeader *h, const char *sig, int len, uint8_t rev) -{ - memcpy(&h->signature, sig, 4); - h->length = cpu_to_le32(len); - h->revision = rev; - memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6); - memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4); - memcpy(h->oem_table_id + 4, sig, 4); - h->oem_revision = cpu_to_le32(1); - memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4); - h->asl_compiler_revision = cpu_to_le32(1); - h->checksum = 0; - /* Checksum to be filled in by Guest linker */ - bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE, - table_data->data, h, len, &h->checksum); -} - /* End here */ #define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */ -static inline void *acpi_data_push(GArray *table_data, unsigned size) -{ - unsigned off = table_data->len; - g_array_set_size(table_data, off + size); - return table_data->data + off; -} - -static unsigned acpi_data_len(GArray *table) -{ -#if GLIB_CHECK_VERSION(2, 22, 0) - assert(g_array_get_element_size(table) == 1); -#endif - return table->len; -} - static void acpi_align_size(GArray *blob, unsigned align) { /* Align size to multiple of given size. This reduces the chance @@ -312,12 +269,6 @@ static void acpi_align_size(GArray *blob, unsigned align) g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align)); } -static inline void acpi_add_table(GArray *table_offsets, GArray *table_data) -{ - uint32_t offset = cpu_to_le32(table_data->len); - g_array_append_val(table_offsets, offset); -} - /* FACS */ static void build_facs(GArray *table_data, GArray *linker, PcGuestInfo *guest_info) @@ -385,8 +336,9 @@ build_fadt(GArray *table_data, GArray *linker, AcpiPmInfo *pm, fadt_setup(fadt, pm); - build_header(linker, table_data, - (void *)fadt, "FACP", sizeof(*fadt), 1); + build_header(linker, table_data, (void *)fadt, "FACP", + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, + sizeof(*fadt), 1); } static void @@ -456,6 +408,7 @@ build_madt(GArray *table_data, GArray *linker, AcpiCpuInfo *cpu, build_header(linker, table_data, (void *)(table_data->data + madt_start), "APIC", + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, table_data->len - madt_start, 1); } @@ -654,7 +607,8 @@ build_ssdt(AcpiAml *table_aml, GArray *linker, /* Init SSDT Definition Block */ ssdt = - acpi_def_block("SSDT", 1, ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, 1); + acpi_def_block("SSDT", 1, ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, + 1, ACPI_BUILD_APPNAME4); scope = acpi_scope("\\_SB.PCI0"); /* build PCI0._CRS */ @@ -1000,8 +954,9 @@ build_hpet(GArray *table_data, GArray *linker) */ hpet->timer_block_id = cpu_to_le32(0x8086a201); hpet->addr.address = cpu_to_le64(HPET_BASE); - build_header(linker, table_data, - (void *)hpet, "HPET", sizeof(*hpet), 1); + build_header(linker, table_data, (void *)hpet, "HPET", + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, + sizeof(*hpet), 1); } static void @@ -1023,8 +978,9 @@ build_tpm_tcpa(GArray *table_data, GArray *linker, GArray *tcpalog) table_data, &tcpa->log_area_start_address, sizeof(tcpa->log_area_start_address)); - build_header(linker, table_data, - (void *)tcpa, "TCPA", sizeof(*tcpa), 2); + build_header(linker, table_data, (void *)tcpa, "TCPA", + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, + sizeof(*tcpa), 2); acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE); } @@ -1147,8 +1103,8 @@ build_srat(GArray *table_data, GArray *linker, PcGuestInfo *guest_info) } build_header(linker, table_data, - (void *)(table_data->data + srat_start), - "SRAT", + (void *)(table_data->data + srat_start), "SRAT", + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, table_data->len - srat_start, 1); } @@ -1178,7 +1134,9 @@ build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info) } else { sig = "MCFG"; } - build_header(linker, table_data, (void *)mcfg, sig, len, 1); + build_header(linker, table_data, (void *)mcfg, sig, + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, + len, 1); } static void @@ -1202,7 +1160,8 @@ build_dmar_q35(GArray *table_data, GArray *linker) drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR); build_header(linker, table_data, (void *)(table_data->data + dmar_start), - "DMAR", table_data->len - dmar_start, 1); + "DMAR", ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, + table_data->len - dmar_start, 1); } static void @@ -1217,6 +1176,7 @@ build_dsdt(GArray *table_data, GArray *linker, AcpiMiscInfo *misc) memset(dsdt, 0, sizeof *dsdt); build_header(linker, table_data, dsdt, "DSDT", + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, misc->dsdt_size, 1); } @@ -1240,8 +1200,9 @@ build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets) table_data, &rsdt->table_offset_entry[i], sizeof(uint32_t)); } - build_header(linker, table_data, - (void *)rsdt, "RSDT", rsdt_len, 1); + build_header(linker, table_data, (void *)rsdt, "RSDT", + ACPI_BUILD_APPNAME6, ACPI_BUILD_APPNAME4, + rsdt_len, 1); } static GArray * @@ -1269,32 +1230,6 @@ build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt) } typedef -struct AcpiBuildTables { - AcpiAml table_data; - GArray *rsdp; - GArray *tcpalog; - GArray *linker; -} AcpiBuildTables; - -static inline void acpi_build_tables_init(AcpiBuildTables *tables) -{ - tables->rsdp = g_array_new(false, true /* clear */, 1); - tables->table_data.buf = g_array_new(false, true /* clear */, 1); - tables->tcpalog = g_array_new(false, true /* clear */, 1); - tables->linker = bios_linker_loader_init(); - tables->table_data.linker = tables->linker; -} - -static inline void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre) -{ - void *linker_data = bios_linker_loader_cleanup(tables->linker); - g_free(linker_data); - g_array_free(tables->rsdp, mfre); - g_array_free(tables->table_data.buf, true); - g_array_free(tables->tcpalog, mfre); -} - -typedef struct AcpiBuildState { /* Copy of table in RAM (for patching). */ ram_addr_t table_ram; diff --git a/hw/i386/acpi-build.h b/hw/i386/acpi-build.h index e57b1aa..8df495c 100644 --- a/hw/i386/acpi-build.h +++ b/hw/i386/acpi-build.h @@ -4,6 +4,9 @@ #include "qemu/typedefs.h" +#define ACPI_BUILD_APPNAME4 "BXPC" +#define ACPI_BUILD_APPNAME6 "BOCHS " + void acpi_setup(PcGuestInfo *); #endif diff --git a/include/hw/acpi/acpi-build-utils.h b/include/hw/acpi/acpi-build-utils.h index d39b5b1..b504c3a 100644 --- a/include/hw/acpi/acpi-build-utils.h +++ b/include/hw/acpi/acpi-build-utils.h @@ -4,6 +4,7 @@ #include <stdint.h> #include <glib.h> #include "qemu/compiler.h" +#include "hw/acpi/acpi-defs.h" typedef enum { NON_BLOCK, @@ -14,8 +15,12 @@ typedef enum { DEF_BLOCK, } AcpiBlockFlags; +/* Reserve RAM space for tables: add another order of magnitude. */ +#define ACPI_BUILD_TABLE_MAX_SIZE 0x200000 + #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables" -#define ACPI_BUILD_APPNAME4 "BXPC" +#define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp" +#define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log" typedef struct AcpiAml { GArray *buf; @@ -94,6 +99,13 @@ typedef enum { acpi_ReadWrite = 1, } acpiReadAndWrite; +typedef +struct AcpiBuildTables { + AcpiAml table_data; + GArray *rsdp; + GArray *tcpalog; + GArray *linker; +} AcpiBuildTables; void aml_append(AcpiAml *parent_ctx, AcpiAml child); @@ -154,7 +166,7 @@ AcpiAml acpi_qword_memory(acpiDecode dec, acpiMinFixed min_fixed, /* Block ASL object primitives */ AcpiAml acpi_def_block(const char *signature, uint8_t revision, const char *oem_id, const char *oem_table_id, - uint32_t oem_revision); + uint32_t oem_revision, const char *asl_compiler_id); AcpiAml acpi_if(AcpiAml predicate); AcpiAml acpi_method(const char *name, int arg_count); AcpiAml GCC_FMT_ATTR(1, 2) acpi_scope(const char *name_format, ...); @@ -182,4 +194,14 @@ void build_append_value(GArray *table, uint64_t value, int size); void build_append_int(GArray *table, uint64_t value); void build_extop_package(GArray *package, uint8_t op); +void +build_header(GArray *linker, GArray *table_data, AcpiTableHeader *h, + const char *sig, const char *ome_id, const char *asl_id, + int len, uint8_t rev); +void *acpi_data_push(GArray *table_data, uint64_t size); +uint64_t acpi_data_len(GArray *table); +void acpi_add_table(GArray *table_offsets, GArray *table_data); +void acpi_build_tables_init(AcpiBuildTables *tables); +void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre); + #endif -- 1.7.1