All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v3 29/29] acpi: Add an acpi command
Date: Mon, 30 Mar 2020 17:13:05 -0600	[thread overview]
Message-ID: <20200330171226.v3.29.Ia4301e60eeaa06de172c4c6aaf5c6bdda81c0d77@changeid> (raw)
In-Reply-To: <20200330231305.130488-1-sjg@chromium.org>

It is useful to dump ACPI tables in U-Boot to see what has been generated.
Add a command to handle this.

To allow the command to find the tables, add a position into the global
data.

Support subcommands to list and dump the tables.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Wolfgang Wallner <wolfgang.wallner@br-automation.com>
---

Changes in v3: None
Changes in v2: None

 arch/sandbox/include/asm/global_data.h |   1 +
 arch/x86/include/asm/global_data.h     |   1 +
 cmd/Kconfig                            |  14 ++
 cmd/Makefile                           |   1 +
 cmd/acpi.c                             | 179 +++++++++++++++++++++++++
 lib/acpi/acpi_table.c                  |   1 +
 test/dm/acpi.c                         |  73 ++++++++++
 7 files changed, 270 insertions(+)
 create mode 100644 cmd/acpi.c

diff --git a/arch/sandbox/include/asm/global_data.h b/arch/sandbox/include/asm/global_data.h
index f4ce72d5660..f95ddb058a2 100644
--- a/arch/sandbox/include/asm/global_data.h
+++ b/arch/sandbox/include/asm/global_data.h
@@ -13,6 +13,7 @@
 struct arch_global_data {
 	uint8_t		*ram_buf;	/* emulated RAM buffer */
 	void		*text_base;	/* pointer to base of text region */
+	ulong acpi_start;		/* Start address of ACPI tables */
 };
 
 #include <asm-generic/global_data.h>
diff --git a/arch/x86/include/asm/global_data.h b/arch/x86/include/asm/global_data.h
index f4c1839104e..4aee2f3e8c4 100644
--- a/arch/x86/include/asm/global_data.h
+++ b/arch/x86/include/asm/global_data.h
@@ -123,6 +123,7 @@ struct arch_global_data {
 #ifdef CONFIG_FSP_VERSION2
 	struct fsp_header *fsp_s_hdr;	/* Pointer to FSP-S header */
 #endif
+	ulong acpi_start;		/* Start address of ACPI tables */
 };
 
 #endif
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 6403bc45a5e..2d3bfe0ab91 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -190,6 +190,20 @@ comment "Commands"
 
 menu "Info commands"
 
+config CMD_ACPI
+	bool "acpi"
+	default y if ACPIGEN
+	help
+	  List and dump ACPI tables. ACPI (Advanced Configuration and Power
+	  Interface) is used mostly on x86 for providing information to the
+	  Operating System about devices in the system. The tables are set up
+	  by the firmware, typically U-Boot but possibly an earlier firmware
+	  module, if U-Boot is chain-loaded from something else. ACPI tables
+	  can also include code, to perform hardware-specific tasks required
+	  by the Operating Systems. This allows some amount of separation
+	  between the firmware and OS, and is particularly useful when you
+	  want to make hardware changes without the OS needing to be adjusted.
+
 config CMD_BDI
 	bool "bdinfo"
 	default y
diff --git a/cmd/Makefile b/cmd/Makefile
index f1dd513a4b4..15a9693ed0e 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -11,6 +11,7 @@ obj-y += help.o
 obj-y += version.o
 
 # command
+obj-$(CONFIG_CMD_ACPI) += acpi.o
 obj-$(CONFIG_CMD_AES) += aes.o
 obj-$(CONFIG_CMD_AB_SELECT) += ab_select.o
 obj-$(CONFIG_CMD_ADC) += adc.o
diff --git a/cmd/acpi.c b/cmd/acpi.c
new file mode 100644
index 00000000000..8a320435736
--- /dev/null
+++ b/cmd/acpi.c
@@ -0,0 +1,179 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2019 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+#include <common.h>
+#include <command.h>
+#include <mapmem.h>
+#include <acpi/acpi_table.h>
+#include <asm/acpi_table.h>
+#include <dm/acpi.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static void dump_hdr(struct acpi_table_header *hdr)
+{
+	bool has_hdr = memcmp(hdr->signature, "FACS", ACPI_NAME_LEN);
+
+	printf("%.*s %08lx %06x", ACPI_NAME_LEN, hdr->signature,
+	       (ulong)map_to_sysmem(hdr), hdr->length);
+	if (has_hdr) {
+		printf(" (v%02d %.6s %.8s %u %.4s %d)\n", hdr->revision,
+		       hdr->oem_id, hdr->oem_table_id, hdr->oem_revision,
+		       hdr->aslc_id, hdr->aslc_revision);
+	} else {
+		printf("\n");
+	}
+}
+
+/**
+ * find_table() - Look up an ACPI table
+ *
+ * @sig: Signature of table (4 characters, upper case)
+ * @return pointer to table header, or NULL if not found
+ */
+struct acpi_table_header *find_table(const char *sig)
+{
+	struct acpi_rsdp *rsdp;
+	struct acpi_rsdt *rsdt;
+	int len, i, count;
+
+	rsdp = map_sysmem(gd->arch.acpi_start, 0);
+	if (!rsdp)
+		return NULL;
+	rsdt = map_sysmem(rsdp->rsdt_address, 0);
+	len = rsdt->header.length - sizeof(rsdt->header);
+	count = len / sizeof(u32);
+	for (i = 0; i < count; i++) {
+		struct acpi_table_header *hdr;
+
+		hdr = map_sysmem(rsdt->entry[i], 0);
+		if (!memcmp(hdr->signature, sig, ACPI_NAME_LEN))
+			return hdr;
+		if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN)) {
+			struct acpi_fadt *fadt = (struct acpi_fadt *)hdr;
+
+			if (!memcmp(sig, "DSDT", ACPI_NAME_LEN) && fadt->dsdt)
+				return map_sysmem(fadt->dsdt, 0);
+			if (!memcmp(sig, "FACS", ACPI_NAME_LEN) &&
+			    fadt->firmware_ctrl)
+				return map_sysmem(fadt->firmware_ctrl, 0);
+		}
+	}
+
+	return NULL;
+}
+
+static int dump_table_name(const char *sig)
+{
+	struct acpi_table_header *hdr;
+
+	hdr = find_table(sig);
+	if (!hdr)
+		return -ENOENT;
+	printf("%.*s @ %08lx\n", ACPI_NAME_LEN, hdr->signature,
+	       (ulong)map_to_sysmem(hdr));
+	print_buffer(0, hdr, 1, hdr->length, 0);
+
+	return 0;
+}
+
+static void list_fact(struct acpi_fadt *fadt)
+{
+	if (fadt->dsdt)
+		dump_hdr(map_sysmem(fadt->dsdt, 0));
+	if (fadt->firmware_ctrl)
+		dump_hdr(map_sysmem(fadt->firmware_ctrl, 0));
+}
+
+static int list_rsdt(struct acpi_rsdt *rsdt, struct acpi_xsdt *xsdt)
+{
+	int len, i, count;
+
+	dump_hdr(&rsdt->header);
+	if (xsdt)
+		dump_hdr(&xsdt->header);
+	len = rsdt->header.length - sizeof(rsdt->header);
+	count = len / sizeof(u32);
+	for (i = 0; i < count; i++) {
+		struct acpi_table_header *hdr;
+
+		if (!rsdt->entry[i])
+			break;
+		hdr = map_sysmem(rsdt->entry[i], 0);
+		dump_hdr(hdr);
+		if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN))
+			list_fact((struct acpi_fadt *)hdr);
+		if (xsdt) {
+			if (xsdt->entry[i] != rsdt->entry[i]) {
+				printf("   (xsdt mismatch %llx)\n",
+				       xsdt->entry[i]);
+			}
+		}
+	}
+
+	return 0;
+}
+
+static int list_rsdp(struct acpi_rsdp *rsdp)
+{
+	struct acpi_rsdt *rsdt;
+	struct acpi_xsdt *xsdt;
+
+	printf("RSDP %08lx %06x (v%02d %.6s)\n", (ulong)map_to_sysmem(rsdp),
+	       rsdp->length, rsdp->revision, rsdp->oem_id);
+	rsdt = map_sysmem(rsdp->rsdt_address, 0);
+	xsdt = map_sysmem(rsdp->xsdt_address, 0);
+	list_rsdt(rsdt, xsdt);
+
+	return 0;
+}
+
+static int do_acpi_list(cmd_tbl_t *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	struct acpi_rsdp *rsdp;
+
+	rsdp = map_sysmem(gd->arch.acpi_start, 0);
+	if (!rsdp) {
+		printf("No ACPI tables present\n");
+		return 0;
+	}
+	printf("ACPI tables start at %lx\n", gd->arch.acpi_start);
+	list_rsdp(rsdp);
+
+	return 0;
+}
+
+static int do_acpi_dump(cmd_tbl_t *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	const char *name;
+	char sig[ACPI_NAME_LEN];
+	int ret;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+	name = argv[1];
+	if (strlen(name) != ACPI_NAME_LEN) {
+		printf("Table name '%s' must be four characters\n", name);
+		return CMD_RET_FAILURE;
+	}
+	str_to_upper(name, sig, -1);
+	ret = dump_table_name(sig);
+	if (ret) {
+		printf("Table '%.*s' not found\n", ACPI_NAME_LEN, sig);
+		return CMD_RET_FAILURE;
+	}
+
+	return 0;
+}
+
+static char acpi_help_text[] =
+	"list - list ACPI tables\n"
+	"acpi dump <name> - Dump ACPI table";
+
+U_BOOT_CMD_WITH_SUBCMDS(acpi, "ACPI tables", acpi_help_text,
+	U_BOOT_SUBCMD_MKENT(list, 1, 1, do_acpi_list),
+	U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_acpi_dump));
diff --git a/lib/acpi/acpi_table.c b/lib/acpi/acpi_table.c
index 28003dc8f46..3b5479eb85b 100644
--- a/lib/acpi/acpi_table.c
+++ b/lib/acpi/acpi_table.c
@@ -238,6 +238,7 @@ void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
 
 	/* Align ACPI tables to 16 byte */
 	acpi_align(ctx);
+	gd->arch.acpi_start = map_to_sysmem(ctx->current);
 
 	/* We need at least an RSDP and an RSDT Table */
 	ctx->rsdp = ctx->current;
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index 8e160d2cf4e..4eabf53128a 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -7,9 +7,11 @@
  */
 
 #include <common.h>
+#include <console.h>
 #include <dm.h>
 #include <malloc.h>
 #include <mapmem.h>
+#include <version.h>
 #include <tables_csum.h>
 #include <version.h>
 #include <acpi/acpi_table.h>
@@ -212,6 +214,7 @@ static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
 	buf = memalign(64, BUF_SIZE);
 	ut_assertnonnull(buf);
 	acpi_setup_base_tables(&ctx, buf + 4);
+	ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd->arch.acpi_start);
 
 	rsdp = buf + 16;
 	ut_asserteq_ptr(rsdp, ctx.rsdp);
@@ -242,3 +245,73 @@ static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_acpi_setup_base_tables,
 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test 'acpi list' command */
+static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
+{
+	struct acpi_ctx ctx;
+	ulong addr;
+	void *buf;
+
+	buf = memalign(16, BUF_SIZE);
+	ut_assertnonnull(buf);
+	acpi_setup_base_tables(&ctx, buf);
+
+	ut_assertok(acpi_write_dev_tables(&ctx));
+
+	console_record_reset();
+	run_command("acpi list", 0);
+	addr = (ulong)map_to_sysmem(buf);
+	ut_assert_nextline("ACPI tables start at %lx", addr);
+	ut_assert_nextline("RSDP %08lx %06lx (v02 U-BOOT)", addr,
+			   sizeof(struct acpi_rsdp));
+	addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
+	ut_assert_nextline("RSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
+			   addr, sizeof(struct acpi_table_header) +
+			   2 * sizeof(u32), U_BOOT_BUILD_DATE);
+	addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
+	ut_assert_nextline("XSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
+			   addr, sizeof(struct acpi_table_header) +
+			   2 * sizeof(u64), U_BOOT_BUILD_DATE);
+	addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
+	ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
+			   addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
+	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
+	ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
+			   addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
+	ut_assert_console_end();
+
+	return 0;
+}
+DM_TEST(dm_test_acpi_cmd_list, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test 'acpi dump' command */
+static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
+{
+	struct acpi_ctx ctx;
+	ulong addr;
+	void *buf;
+
+	buf = memalign(16, BUF_SIZE);
+	ut_assertnonnull(buf);
+	acpi_setup_base_tables(&ctx, buf);
+
+	ut_assertok(acpi_write_dev_tables(&ctx));
+
+	/* First search for a non-existent table */
+	console_record_reset();
+	run_command("acpi dump rdst", 0);
+	ut_assert_nextline("Table 'RDST' not found");
+	ut_assert_console_end();
+
+	/* Now a real table */
+	console_record_reset();
+	run_command("acpi dump dmar", 0);
+	addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
+	ut_assert_nextline("DMAR @ %08lx", addr);
+	ut_assert_nextlines_are_dump(0x30);
+	ut_assert_console_end();
+
+	return 0;
+}
+DM_TEST(dm_test_acpi_cmd_dump, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.26.0.rc2.310.g2932bb562d-goog

  parent reply	other threads:[~2020-03-30 23:13 UTC|newest]

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

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=20200330171226.v3.29.Ia4301e60eeaa06de172c4c6aaf5c6bdda81c0d77@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.