diff mbox series

[033/108] acpi: Add support for SSDT generation

Message ID 20200126220508.33.I5ed669af183c547e044a628e7c670f1ef73bfa3d@changeid
State New
Headers show
Series RFC: dm: Add programatic generation of ACPI tables | expand

Commit Message

Simon Glass Jan. 27, 2020, 5:05 a.m. UTC
Some devices need to generate code for the Secondary System Descriptor
Table (SSDT). Add a method to handle this.

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

 arch/sandbox/dts/test.dts |  2 ++
 drivers/core/acpi.c       | 38 ++++++++++++++++++++++++++++++++++++
 include/dm/acpi.h         | 23 ++++++++++++++++++++++
 test/dm/acpi.c            | 41 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 103 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 82208de5a8..3351f92165 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -208,10 +208,12 @@ 
 
 	acpi-test {
 		compatible = "denx,u-boot-acpi-test";
+		acpi-ssdt-test-data = "ab";
 	};
 
 	acpi-test2 {
 		compatible = "denx,u-boot-acpi-test";
+		acpi-ssdt-test-data = "cd";
 	};
 
 	clocks {
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index f748aa3f33..18567682f2 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -11,6 +11,7 @@ 
 #include <common.h>
 #include <dm.h>
 #include <dm/acpi.h>
+#include <dm/device-internal.h>
 #include <dm/root.h>
 
 int acpi_return_name(char *out_name, const char *name)
@@ -31,6 +32,43 @@  int acpi_get_name(const struct udevice *dev, char *out_name)
 	return -ENOSYS;
 }
 
+int _acpi_fill_ssdt(struct acpi_ctx *ctx, struct udevice *parent)
+{
+	struct acpi_ops *aops;
+	struct udevice *dev;
+	int ret;
+
+	aops = device_get_acpi_ops(parent);
+	if (aops && aops->fill_ssdt) {
+		log_debug("\n- %s %p\n", parent->name,
+			  aops->fill_ssdt);
+		ret = device_ofdata_to_platdata(parent);
+		if (ret)
+			return log_msg_ret("ofdata", ret);
+		ret = aops->fill_ssdt(parent, ctx);
+		if (ret)
+			return log_msg_ret("ssdt", ret);
+	}
+	device_foreach_child(dev, parent) {
+		ret = _acpi_fill_ssdt(ctx, dev);
+		if (ret)
+			return log_msg_ret("recurse", ret);
+	}
+
+	return 0;
+}
+
+int acpi_fill_ssdt(struct acpi_ctx *ctx)
+{
+	int ret;
+
+	log_debug("Writing SSDT tables\n");
+	ret = _acpi_fill_ssdt(ctx, dm_root());
+	log_debug("Writing SSDT finished, err=%d\n", ret);
+
+	return ret;
+}
+
 int _acpi_write_dev_tables(struct acpi_ctx *ctx, const struct udevice *parent)
 {
 	struct acpi_ops *aops;
diff --git a/include/dm/acpi.h b/include/dm/acpi.h
index 0f78a506da..c340c21685 100644
--- a/include/dm/acpi.h
+++ b/include/dm/acpi.h
@@ -72,6 +72,19 @@  struct acpi_ops {
 	 * @return 0 if OK, -ve on error
 	 */
 	int (*write_tables)(const struct udevice *dev, struct acpi_ctx *ctx);
+
+	/**
+	 * fill_ssdt() - Generate SSDT code for a device
+	 *
+	 * This is called to create the SSDT code. THe method should write out
+	 * whatever ACPI code is needed by this device. It will end up in the
+	 * SSDT table.
+	 *
+	 * @dev: Device to write
+	 * @ctx: ACPI context to use
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx);
 };
 
 #define device_get_acpi_ops(dev)	((dev)->driver->acpi_ops)
@@ -116,6 +129,16 @@  int acpi_return_name(char *out_name, const char *name);
  */
 int acpi_write_dev_tables(struct acpi_ctx *ctx);
 
+/**
+ * acpi_fill_ssdt() - Generate ACPI tables for SSDT
+ *
+ * This is called to create the SSDT code for all devices.
+ *
+ * @ctx: ACPI context to use
+ * @return 0 if OK, -ve on error
+ */
+int acpi_fill_ssdt(struct acpi_ctx *ctx);
+
 #endif /* __ACPI__ */
 
 #endif
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index feb380b26c..50d6ec91f7 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -7,6 +7,7 @@ 
  */
 
 #include <common.h>
+#include <acpigen.h>
 #include <acpi_table.h>
 #include <console.h>
 #include <dm.h>
@@ -14,7 +15,6 @@ 
 #include <mapmem.h>
 #include <version.h>
 #include <tables_csum.h>
-#include <version.h>
 #include <dm/acpi.h>
 #include <dm/test.h>
 #include <test/ut.h>
@@ -43,9 +43,21 @@  static int testacpi_get_name(const struct udevice *dev, char *out_name)
 	return acpi_return_name(out_name, ACPI_TEST_DEV_NAME);
 }
 
+static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
+{
+	const char *data;
+
+	data = dev_read_string(dev, "acpi-ssdt-test-data");
+	while (*data)
+		acpigen_emit_byte(ctx, *data++);
+
+	return 0;
+}
+
 struct acpi_ops testacpi_ops = {
 	.get_name	= testacpi_get_name,
 	.write_tables	= testacpi_write_tables,
+	.fill_ssdt	= testacpi_fill_ssdt,
 };
 
 static const struct udevice_id testacpi_ids[] = {
@@ -313,3 +325,30 @@  static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_acpi_cmd_dump, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test acpi_fill_ssdt() */
+static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
+{
+	struct acpi_ctx ctx;
+	u8 *buf;
+
+	buf = malloc(BUF_SIZE);
+	ut_assertnonnull(buf);
+
+	ctx.current = buf;
+	buf[4] = 'z';	/* sentinal */
+	ut_assertok(acpi_fill_ssdt(&ctx));
+
+	/* These values come from acpi-test's acpi-ssdt-test-data property */
+	ut_asserteq('a', buf[0]);
+	ut_asserteq('b', buf[1]);
+
+	/* These values come from acpi-test2's acpi-ssdt-test-data property */
+	ut_asserteq('c', buf[2]);
+	ut_asserteq('d', buf[3]);
+
+	ut_asserteq('z', buf[4]);
+
+	return 0;
+}
+`DM_TEST(dm_test_acpi_fill_ssdt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);