All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v4 29/31] acpi: Put table-setup code in its own function
Date: Tue,  7 Apr 2020 21:01:05 -0600	[thread overview]
Message-ID: <20200407210023.v4.29.I34e9fcd28119cc2fcb87ad8679efb582a4c611df@changeid> (raw)
In-Reply-To: <20200408030107.168474-1-sjg@chromium.org>

We always write three basic tables to ACPI at the start. Move this into
its own function, along with acpi_fill_header(), so we can write a test
for this code.

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

Changes in v4:
- Put back cast on table_compute_checksum()

Changes in v3:
- Fix 'XDST' typo
- Move acpi_align_large() out of dm_test_acpi_setup_base_tables()
- Beef up the comment explaining how the unaligned address is used

Changes in v2: None

 arch/x86/lib/acpi_table.c | 72 +-----------------------------------
 include/acpi/acpi_table.h | 10 +++++
 lib/acpi/acpi_table.c     | 77 +++++++++++++++++++++++++++++++++++++++
 test/dm/acpi.c            | 58 ++++++++++++++++++++++++++++-
 4 files changed, 144 insertions(+), 73 deletions(-)

diff --git a/arch/x86/lib/acpi_table.c b/arch/x86/lib/acpi_table.c
index d4af56eabf4..4a7b0739394 100644
--- a/arch/x86/lib/acpi_table.c
+++ b/arch/x86/lib/acpi_table.c
@@ -31,58 +31,6 @@ extern const unsigned char AmlCode[];
 /* ACPI RSDP address to be used in boot parameters */
 static ulong acpi_rsdp_addr;
 
-static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
-			    struct acpi_xsdt *xsdt)
-{
-	memset(rsdp, 0, sizeof(struct acpi_rsdp));
-
-	memcpy(rsdp->signature, RSDP_SIG, 8);
-	memcpy(rsdp->oem_id, OEM_ID, 6);
-
-	rsdp->length = sizeof(struct acpi_rsdp);
-	rsdp->rsdt_address = (u32)rsdt;
-
-	rsdp->xsdt_address = (u64)(u32)xsdt;
-	rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
-
-	/* Calculate checksums */
-	rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
-	rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
-			sizeof(struct acpi_rsdp));
-}
-
-static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
-{
-	struct acpi_table_header *header = &(rsdt->header);
-
-	/* Fill out header fields */
-	acpi_fill_header(header, "RSDT");
-	header->length = sizeof(struct acpi_rsdt);
-	header->revision = 1;
-
-	/* Entries are filled in later, we come with an empty set */
-
-	/* Fix checksum */
-	header->checksum = table_compute_checksum((void *)rsdt,
-			sizeof(struct acpi_rsdt));
-}
-
-static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
-{
-	struct acpi_table_header *header = &(xsdt->header);
-
-	/* Fill out header fields */
-	acpi_fill_header(header, "XSDT");
-	header->length = sizeof(struct acpi_xsdt);
-	header->revision = 1;
-
-	/* Entries are filled in later, we come with an empty set */
-
-	/* Fix checksum */
-	header->checksum = table_compute_checksum((void *)xsdt,
-			sizeof(struct acpi_xsdt));
-}
-
 static void acpi_create_facs(struct acpi_facs *facs)
 {
 	memset((void *)facs, 0, sizeof(struct acpi_facs));
@@ -402,7 +350,6 @@ static void acpi_create_spcr(struct acpi_spcr *spcr)
 ulong write_acpi_tables(ulong start_addr)
 {
 	struct acpi_ctx sctx, *ctx = &sctx;
-	struct acpi_xsdt *xsdt;
 	struct acpi_facs *facs;
 	struct acpi_table_header *dsdt;
 	struct acpi_fadt *fadt;
@@ -415,33 +362,16 @@ ulong write_acpi_tables(ulong start_addr)
 	int i;
 
 	start = map_sysmem(start_addr, 0);
-	ctx->current = start;
-
-	/* Align ACPI tables to 16 byte */
-	acpi_align(ctx);
 
 	debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
 
-	/* We need at least an RSDP and an RSDT Table */
-	ctx->rsdp = ctx->current;
-	acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
-	ctx->rsdt = ctx->current;
-	acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
-	xsdt = ctx->current;
-	acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
+	acpi_setup_base_tables(ctx, start);
 	/*
 	 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
 	 * boundary (Windows checks this, but Linux does not).
 	 */
 	acpi_align64(ctx);
 
-	/* clear all table memory */
-	memset((void *)start, 0, ctx->current - start);
-
-	acpi_write_rsdp(ctx->rsdp, ctx->rsdt, xsdt);
-	acpi_write_rsdt(ctx->rsdt);
-	acpi_write_xsdt(xsdt);
-
 	debug("ACPI:    * FACS\n");
 	facs = ctx->current;
 	acpi_inc_align(ctx, sizeof(struct acpi_facs));
diff --git a/include/acpi/acpi_table.h b/include/acpi/acpi_table.h
index 6225a4c5ccb..48bf189c570 100644
--- a/include/acpi/acpi_table.h
+++ b/include/acpi/acpi_table.h
@@ -562,6 +562,16 @@ void acpi_inc_align(struct acpi_ctx *ctx, uint amount);
  */
 int acpi_add_table(struct acpi_ctx *ctx, void *table);
 
+/**
+ * acpi_setup_base_tables() - Set up context along with RSDP, RSDT and XSDT
+ *
+ * Set up the context with the given start position. Some basic tables are
+ * always needed, so set them up as well.
+ *
+ * @ctx: Context to set up
+ */
+void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start);
+
 #endif /* !__ACPI__*/
 
 #include <asm/acpi_table.h>
diff --git a/lib/acpi/acpi_table.c b/lib/acpi/acpi_table.c
index 5311ae9368b..85193c49e45 100644
--- a/lib/acpi/acpi_table.c
+++ b/lib/acpi/acpi_table.c
@@ -181,3 +181,80 @@ int acpi_add_table(struct acpi_ctx *ctx, void *table)
 
 	return 0;
 }
+
+static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
+			    struct acpi_xsdt *xsdt)
+{
+	memset(rsdp, 0, sizeof(struct acpi_rsdp));
+
+	memcpy(rsdp->signature, RSDP_SIG, 8);
+	memcpy(rsdp->oem_id, OEM_ID, 6);
+
+	rsdp->length = sizeof(struct acpi_rsdp);
+	rsdp->rsdt_address = map_to_sysmem(rsdt);
+
+	rsdp->xsdt_address = map_to_sysmem(xsdt);
+	rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
+
+	/* Calculate checksums */
+	rsdp->checksum = table_compute_checksum(rsdp, 20);
+	rsdp->ext_checksum = table_compute_checksum(rsdp,
+						    sizeof(struct acpi_rsdp));
+}
+
+static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
+{
+	struct acpi_table_header *header = &rsdt->header;
+
+	/* Fill out header fields */
+	acpi_fill_header(header, "RSDT");
+	header->length = sizeof(struct acpi_rsdt);
+	header->revision = 1;
+
+	/* Entries are filled in later, we come with an empty set */
+
+	/* Fix checksum */
+	header->checksum = table_compute_checksum(rsdt,
+						  sizeof(struct acpi_rsdt));
+}
+
+static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
+{
+	struct acpi_table_header *header = &xsdt->header;
+
+	/* Fill out header fields */
+	acpi_fill_header(header, "XSDT");
+	header->length = sizeof(struct acpi_xsdt);
+	header->revision = 1;
+
+	/* Entries are filled in later, we come with an empty set */
+
+	/* Fix checksum */
+	header->checksum = table_compute_checksum(xsdt,
+						  sizeof(struct acpi_xsdt));
+}
+
+void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
+{
+	struct acpi_xsdt *xsdt;
+
+	ctx->current = start;
+
+	/* Align ACPI tables to 16 byte */
+	acpi_align(ctx);
+
+	/* We need at least an RSDP and an RSDT Table */
+	ctx->rsdp = ctx->current;
+	acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
+	ctx->rsdt = ctx->current;
+	acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
+	xsdt = ctx->current;
+	acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
+
+	/* clear all table memory */
+	memset((void *)start, '\0', ctx->current - start);
+
+	acpi_write_rsdp(ctx->rsdp, ctx->rsdt, xsdt);
+	acpi_write_rsdt(ctx->rsdt);
+	acpi_write_xsdt(xsdt);
+}
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index 4dd4a540967..5d5e46e2944 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -8,6 +8,9 @@
 
 #include <common.h>
 #include <dm.h>
+#include <malloc.h>
+#include <mapmem.h>
+#include <tables_csum.h>
 #include <version.h>
 #include <acpi/acpi_table.h>
 #include <dm/acpi.h>
@@ -137,9 +140,9 @@ static int dm_test_acpi_write_tables(struct unit_test_state *uts)
 	buf = malloc(BUF_SIZE);
 	ut_assertnonnull(buf);
 
-	ctx.current = buf;
+	acpi_setup_base_tables(&ctx, buf);
+	dmar = ctx.current;
 	ut_assertok(acpi_write_dev_tables(&ctx));
-	dmar = buf;
 
 	/*
 	 * We should have two dmar tables, one for each "denx,u-boot-acpi-test"
@@ -152,6 +155,11 @@ static int dm_test_acpi_write_tables(struct unit_test_state *uts)
 	ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
 	ut_asserteq(32 - 1, dmar[1].host_address_width);
 
+	/* Check that the pointers were added correctly */
+	ut_asserteq(map_to_sysmem(dmar), ctx.rsdt->entry[0]);
+	ut_asserteq(map_to_sysmem(dmar + 1), ctx.rsdt->entry[1]);
+	ut_asserteq(0, ctx.rsdt->entry[2]);
+
 	return 0;
 }
 DM_TEST(dm_test_acpi_write_tables, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
@@ -183,3 +191,49 @@ static int dm_test_acpi_basic(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_acpi_basic, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test acpi_setup_base_tables */
+static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
+{
+	struct acpi_rsdp *rsdp;
+	struct acpi_rsdt *rsdt;
+	struct acpi_xsdt *xsdt;
+	struct acpi_ctx ctx;
+	void *buf, *end;
+
+	/*
+	 * Use an unaligned address deliberately, by allocating an aligned
+	 * address and then adding 4 to it
+	 */
+	buf = memalign(64, BUF_SIZE);
+	ut_assertnonnull(buf);
+	acpi_setup_base_tables(&ctx, buf + 4);
+
+	rsdp = buf + 16;
+	ut_asserteq_ptr(rsdp, ctx.rsdp);
+	ut_assertok(memcmp(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature)));
+	ut_asserteq(sizeof(*rsdp), rsdp->length);
+	ut_assertok(table_compute_checksum(rsdp, 20));
+	ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
+
+	rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
+	ut_asserteq_ptr(rsdt, ctx.rsdt);
+	ut_assertok(memcmp("RSDT", rsdt->header.signature, ACPI_NAME_LEN));
+	ut_asserteq(sizeof(*rsdt), rsdt->header.length);
+	ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
+
+	xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
+	ut_assertok(memcmp("XSDT", xsdt->header.signature, ACPI_NAME_LEN));
+	ut_asserteq(sizeof(*xsdt), xsdt->header.length);
+	ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
+
+	end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
+	ut_asserteq_ptr(end, ctx.current);
+
+	ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
+	ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
+
+	return 0;
+}
+DM_TEST(dm_test_acpi_setup_base_tables,
+	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.26.0.292.g33ef6b2f38-goog

  parent reply	other threads:[~2020-04-08  3:01 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-08  3:00 [PATCH v4 00/31] dm: Add programmatic generation of ACPI tables (part A) Simon Glass
2020-04-08  3:00 ` [PATCH v4 01/31] test: Add the beginnings of some string tests Simon Glass
2020-04-08  3:00 ` [PATCH v4 02/31] lib: Add a function to convert a string to upper case Simon Glass
2020-04-08  4:18   ` Heinrich Schuchardt
2020-04-08  4:28     ` Heinrich Schuchardt
2020-04-08 14:20       ` Simon Glass
2020-04-09  5:32         ` Heinrich Schuchardt
2020-04-08  3:00 ` [PATCH v4 03/31] cpu: Support querying the address width Simon Glass
2020-04-08  3:00 ` [PATCH v4 04/31] spi: Add SPI mode enums Simon Glass
2020-04-08  3:00 ` [PATCH v4 05/31] tpm: cr50: Release locality on exit Simon Glass
2020-04-08  3:00 ` [PATCH v4 06/31] tpm: cr50: Add a comment for cr50_priv Simon Glass
2020-04-08  3:00 ` [PATCH v4 07/31] tpm: cr50: Use the correct GPIO binding Simon Glass
2020-04-08  3:00 ` [PATCH v4 08/31] tpm: Don't cleanup unless an error happens Simon Glass
2020-04-08  3:00 ` [PATCH v4 09/31] dm: pci: Allow disabling auto-config for a device Simon Glass
2020-04-08  3:00 ` [PATCH v4 10/31] x86: Correct wording of coreboot source code Simon Glass
2020-04-08  3:00 ` [PATCH v4 11/31] x86: apl: Move p2sb ofdata reading to the correct method Simon Glass
2020-04-08  3:00 ` [PATCH v4 12/31] pci: Adjust dm_pci_read_bar32() to return errors correctly Simon Glass
2020-04-08  3:00 ` [PATCH v4 13/31] x86: apl: Add Global NVS table header Simon Glass
2020-04-08 17:14   ` Andy Shevchenko
2020-04-08  3:00 ` [PATCH v4 14/31] dm: core: Add basic ACPI support Simon Glass
2020-04-08  3:00 ` [PATCH v4 15/31] dts: Add a binding for hid-over-i2c Simon Glass
2020-04-08  3:00 ` [PATCH v4 16/31] acpi: Add a binding for ACPI settings in the device tree Simon Glass
2020-04-08  3:00 ` [PATCH v4 17/31] acpi: Add a simple sandbox test Simon Glass
2020-04-08  3:00 ` [PATCH v4 18/31] x86: Move acpi_s3.h to include/acpi/ Simon Glass
2020-04-08  3:00 ` [PATCH v4 19/31] x86: Move acpi_table header to main include/ directory Simon Glass
2020-04-08  3:00 ` [PATCH v4 20/31] acpi: Add an __ACPI__ preprocessor symbol Simon Glass
2020-04-08  3:00 ` [PATCH v4 21/31] acpi: Add a central location for table version numbers Simon Glass
2020-04-08 17:20   ` Andy Shevchenko
2020-04-08 19:32     ` Simon Glass
2020-04-08  3:00 ` [PATCH v4 22/31] acpi: Add support for DMAR Simon Glass
2020-04-08  3:00 ` [PATCH v4 23/31] test: Add hexdump.h to the unit test header Simon Glass
2020-04-08  3:01 ` [PATCH v4 24/31] acpi: Add a method to write tables for a device Simon Glass
2020-04-08  3:01 ` [PATCH v4 25/31] acpi: Convert part of acpi_table to use acpi_ctx Simon Glass
2020-04-08  3:01 ` [PATCH v4 26/31] x86: Allow devices to write ACPI tables Simon Glass
2020-04-08  3:01 ` [PATCH v4 27/31] acpi: Drop code for missing XSDT from acpi_write_rsdp() Simon Glass
2020-04-08  3:01 ` [PATCH v4 28/31] acpi: Move acpi_add_table() to generic code Simon Glass
2020-04-08  3:01 ` Simon Glass [this message]
2020-04-08  3:01 ` [PATCH v4 30/31] acpi: Move the xsdt pointer to acpi_ctx Simon Glass
2020-04-08  3:01 ` [PATCH v4 31/31] acpi: Add an acpi command Simon Glass
2020-04-08 17:02 ` [PATCH v4 00/31] dm: Add programmatic generation of ACPI tables (part A) Andy Shevchenko
2020-04-08 22:22   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200407210023.v4.29.I34e9fcd28119cc2fcb87ad8679efb582a4c611df@changeid \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.