Move this table over to use a writer function, moving the code from the
x86 implementation.

Signed-off-by: Simon Glass <s...@chromium.org>
---

 arch/x86/lib/acpi_table.c | 93 +++++++++++++--------------------------
 lib/acpi/Makefile         |  1 +
 lib/acpi/ssdt.c           | 49 +++++++++++++++++++++
 3 files changed, 80 insertions(+), 63 deletions(-)
 create mode 100644 lib/acpi/ssdt.c

diff --git a/arch/x86/lib/acpi_table.c b/arch/x86/lib/acpi_table.c
index c1011a48d0c..18b284789d7 100644
--- a/arch/x86/lib/acpi_table.c
+++ b/arch/x86/lib/acpi_table.c
@@ -176,26 +176,6 @@ __weak u32 acpi_fill_mcfg(u32 current)
        return current;
 }
 
-/* MCFG is defined in the PCI Firmware Specification 3.0 */
-static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
-{
-       struct acpi_table_header *header = &(mcfg->header);
-       u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
-
-       memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
-
-       /* Fill out header fields */
-       acpi_fill_header(header, "MCFG");
-       header->length = sizeof(struct acpi_mcfg);
-       header->revision = 1;
-
-       current = acpi_fill_mcfg(current);
-
-       /* (Re)calculate length and checksum */
-       header->length = current - (u32)mcfg;
-       header->checksum = table_compute_checksum((void *)mcfg, header->length);
-}
-
 /**
  * acpi_create_tcpa() - Create a TCPA table
  *
@@ -449,36 +429,6 @@ static void acpi_create_spcr(struct acpi_spcr *spcr)
        header->checksum = table_compute_checksum((void *)spcr, header->length);
 }
 
-static int acpi_create_ssdt(struct acpi_ctx *ctx,
-                           struct acpi_table_header *ssdt,
-                           const char *oem_table_id)
-{
-       memset((void *)ssdt, '\0', sizeof(struct acpi_table_header));
-
-       acpi_fill_header(ssdt, "SSDT");
-       ssdt->revision = acpi_get_table_revision(ACPITAB_SSDT);
-       ssdt->aslc_revision = 1;
-       ssdt->length = sizeof(struct acpi_table_header);
-
-       acpi_inc(ctx, sizeof(struct acpi_table_header));
-
-       acpi_fill_ssdt(ctx);
-
-       /* (Re)calculate length and checksum */
-       ssdt->length = ctx->current - (void *)ssdt;
-       ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
-       log_debug("SSDT at %p, length %x\n", ssdt, ssdt->length);
-
-       /* Drop the table if it is empty */
-       if (ssdt->length == sizeof(struct acpi_table_header)) {
-               ctx->current = ssdt;
-               return -ENOENT;
-       }
-       acpi_align(ctx);
-
-       return 0;
-}
-
 int acpi_write_gnvs(struct acpi_ctx *ctx, const struct acpi_writer *entry)
 {
        ulong addr;
@@ -523,31 +473,48 @@ int acpi_write_gnvs(struct acpi_ctx *ctx, const struct 
acpi_writer *entry)
 }
 ACPI_WRITER(4gnvs, "GNVS", acpi_write_gnvs, 0);
 
+/* MCFG is defined in the PCI Firmware Specification 3.0 */
+int acpi_write_mcfg(struct acpi_ctx *ctx, const struct acpi_writer *entry)
+{
+       struct acpi_table_header *header;
+       struct acpi_mcfg *mcfg;
+       u32 current;
+
+       mcfg = ctx->current;
+       header = &mcfg->header;
+
+       current = (u32)mcfg + sizeof(struct acpi_mcfg);
+
+       memset(mcfg, '\0', sizeof(struct acpi_mcfg));
+
+       /* Fill out header fields */
+       acpi_fill_header(header, "MCFG");
+       header->length = sizeof(struct acpi_mcfg);
+       header->revision = 1;
+
+       /* (Re)calculate length and checksum */
+       header->length = current - (u32)mcfg;
+       header->checksum = table_compute_checksum(mcfg, header->length);
+
+       acpi_inc(ctx, mcfg->header.length);
+       acpi_add_table(ctx, mcfg);
+
+       return 0;
+}
+ACPI_WRITER(5mcfg, "MCFG", acpi_write_mcfg, 0);
+
 /*
  * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  */
 int write_acpi_tables_x86(struct acpi_ctx *ctx,
                          const struct acpi_writer *entry)
 {
-       struct acpi_table_header *ssdt;
-       struct acpi_mcfg *mcfg;
        struct acpi_tcpa *tcpa;
        struct acpi_madt *madt;
        struct acpi_csrt *csrt;
        struct acpi_spcr *spcr;
        int ret;
 
-       debug("ACPI:     * SSDT\n");
-       ssdt = (struct acpi_table_header *)ctx->current;
-       if (!acpi_create_ssdt(ctx, ssdt, OEM_TABLE_ID))
-               acpi_add_table(ctx, ssdt);
-
-       debug("ACPI:    * MCFG\n");
-       mcfg = ctx->current;
-       acpi_create_mcfg(mcfg);
-       acpi_inc_align(ctx, mcfg->header.length);
-       acpi_add_table(ctx, mcfg);
-
        if (IS_ENABLED(CONFIG_TPM_V2)) {
                struct acpi_tpm2 *tpm2;
 
diff --git a/lib/acpi/Makefile b/lib/acpi/Makefile
index ccdf42896dc..552d499fde4 100644
--- a/lib/acpi/Makefile
+++ b/lib/acpi/Makefile
@@ -17,4 +17,5 @@ obj-y += dsdt.o
 endif
 
 obj-y += facs.o
+obj-y += ssdt.o
 endif
diff --git a/lib/acpi/ssdt.c b/lib/acpi/ssdt.c
new file mode 100644
index 00000000000..659c1aad406
--- /dev/null
+++ b/lib/acpi/ssdt.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Write an ACPI Secondary System Descriptor Table (SSDT) table
+ *
+ * Copyright 2021 Google LLC
+ */
+
+#define LOG_CATEGORY LOGC_ACPI
+
+#include <common.h>
+#include <acpi/acpi_table.h>
+#include <dm/acpi.h>
+#include <tables_csum.h>
+
+int acpi_write_ssdt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
+{
+       struct acpi_table_header *ssdt;
+       int ret;
+
+       ssdt = ctx->current;
+       memset((void *)ssdt, '\0', sizeof(struct acpi_table_header));
+
+       acpi_fill_header(ssdt, "SSDT");
+       memcpy(ssdt->oem_table_id, OEM_TABLE_ID, sizeof(ssdt->oem_table_id));
+       ssdt->revision = acpi_get_table_revision(ACPITAB_SSDT);
+       ssdt->aslc_revision = 1;
+       ssdt->length = sizeof(struct acpi_table_header);
+
+       acpi_inc(ctx, sizeof(struct acpi_table_header));
+
+       ret = acpi_fill_ssdt(ctx);
+       if (ret) {
+               ctx->current = ssdt;
+               return log_msg_ret("fill", ret);
+       }
+
+       /* (Re)calculate length and checksum */
+       ssdt->length = ctx->current - (void *)ssdt;
+       ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
+       log_debug("SSDT at %p, length %x\n", ssdt, ssdt->length);
+
+       /* Drop the table if it is empty */
+       if (ssdt->length == sizeof(struct acpi_table_header))
+               return log_msg_ret("fill", -ENOENT);
+       acpi_add_table(ctx, ssdt);
+
+       return 0;
+}
+ACPI_WRITER(6ssdt, "SSDT", acpi_write_ssdt, 0);
-- 
2.34.0.rc2.393.gf8c9666880-goog

Reply via email to