diff mbox series

[038/108] acpi: Add support for DSDT generation

Message ID 20200126220508.38.I2e68300c24666745bcb7ecb874b13329538265e0@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 inject extra code into the Differentiated System
Descriptor Table (DSDT). 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       | 50 ++++++++++++++++++++++++++++++++++++++-
 include/dm/acpi.h         | 23 ++++++++++++++++++
 test/dm/acpi.c            | 44 +++++++++++++++++++++++++++++++++-
 4 files changed, 117 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 100953452a..f60f0adc05 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -209,11 +209,13 @@ 
 	acpi_test1: acpi-test {
 		compatible = "denx,u-boot-acpi-test";
 		acpi-ssdt-test-data = "ab";
+		acpi-dsdt-test-data = "hi";
 	};
 
 	acpi_test2: acpi-test2 {
 		compatible = "denx,u-boot-acpi-test";
 		acpi-ssdt-test-data = "cd";
+		acpi-dsdt-test-data = "jk";
 	};
 
 	clocks {
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index b8e192dce5..657753ebb3 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -18,6 +18,7 @@ 
 
 /* Type of table that we collected */
 enum gen_type_t {
+	TYPE_DSDT,
 	TYPE_SSDT,
 };
 
@@ -118,7 +119,9 @@  static int build_type(struct acpi_ctx *ctx, void *start, enum gen_type_t type)
 	void *end = ctx->current;
 
 	ptr = start;
-	order = ofnode_get_chosen_prop("u-boot,acpi-ssdt-order", &size);
+	order = ofnode_get_chosen_prop(type == TYPE_DSDT ?
+				       "u-boot,acpi-dsdt-order" :
+				       "u-boot,acpi-ssdt-order", &size);
 	if (!order) {
 		log_warning("Failed to find ordering, leaving as is\n");
 		return 0;
@@ -200,6 +203,51 @@  int acpi_fill_ssdt(struct acpi_ctx *ctx)
 	return ret;
 }
 
+int _acpi_inject_dsdt(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->inject_dsdt) {
+		void *start = ctx->current;
+
+		log_debug("- %s %p\n", parent->name, aops->inject_dsdt);
+		ret = device_ofdata_to_platdata(parent);
+		if (ret)
+			return log_msg_ret("ofdata", ret);
+		ret = aops->inject_dsdt(parent, ctx);
+		if (ret)
+			return ret;
+		ret = acpi_add_item(ctx, parent, TYPE_DSDT, start);
+		if (ret)
+			return ret;
+	}
+	device_foreach_child(dev, parent) {
+		ret = _acpi_inject_dsdt(ctx, dev);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+int acpi_inject_dsdt(struct acpi_ctx *ctx)
+{
+	void *start = ctx->current;
+	int ret;
+
+	log_debug("Writing DSDT tables\n");
+	ret = _acpi_inject_dsdt(ctx, dm_root());
+	log_debug("Writing DSDT finished, err=%d\n", ret);
+	ret = build_type(ctx, start, TYPE_DSDT);
+	if (ret)
+		return log_msg_ret("build", 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 c340c21685..8de9296e71 100644
--- a/include/dm/acpi.h
+++ b/include/dm/acpi.h
@@ -85,6 +85,19 @@  struct acpi_ops {
 	 * @return 0 if OK, -ve on error
 	 */
 	int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx);
+
+	/**
+	 * inject_dsdt() - Generate DSDT code for a device
+	 *
+	 * This is called to create the DSDT code. THe method should write out
+	 * whatever ACPI code is needed by this device. It will end up in the
+	 * DSDT table.
+	 *
+	 * @dev: Device to write
+	 * @ctx: ACPI context to use
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*inject_dsdt)(const struct udevice *dev, struct acpi_ctx *ctx);
 };
 
 #define device_get_acpi_ops(dev)	((dev)->driver->acpi_ops)
@@ -139,6 +152,16 @@  int acpi_write_dev_tables(struct acpi_ctx *ctx);
  */
 int acpi_fill_ssdt(struct acpi_ctx *ctx);
 
+/**
+ * acpi_inject_dsdt() - Generate ACPI tables for DSDT
+ *
+ * This is called to create the DSDT code for all devices.
+ *
+ * @ctx: ACPI context to use
+ * @return 0 if OK, -ve on error
+ */
+int acpi_inject_dsdt(struct acpi_ctx *ctx);
+
 #endif /* __ACPI__ */
 
 #endif
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index 3221563ca6..16dc0955be 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -54,10 +54,22 @@  static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
 	return 0;
 }
 
+static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
+{
+	const char *data;
+
+	data = dev_read_string(dev, "acpi-dsdt-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,
+	.inject_dsdt	= testacpi_inject_dsdt,
 };
 
 static const struct udevice_id testacpi_ids[] = {
@@ -354,4 +366,34 @@  static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
 
 	return 0;
 }
-`DM_TEST(dm_test_acpi_fill_ssdt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+DM_TEST(dm_test_acpi_fill_ssdt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test acpi_inject_dsdt() */
+static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
+{
+	struct acpi_ctx ctx;
+	u8 *buf;
+
+	buf = malloc(BUF_SIZE);
+	ut_assertnonnull(buf);
+
+	ctx.current = buf;
+	buf[4] = 'z';	/* sentinel */
+	ut_assertok(acpi_inject_dsdt(&ctx));
+
+	/*
+	 * These values come from acpi-test's acpi-dsdt-test-data property.
+	 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
+	 */
+	ut_asserteq('h', buf[0]);
+	ut_asserteq('i', buf[1]);
+
+	/* These values come from acpi-test's acpi-dsdt-test-data property */
+	ut_asserteq('j', buf[2]);
+	ut_asserteq('k', buf[3]);
+
+	ut_asserteq('z', buf[4]);
+
+	return 0;
+}
+DM_TEST(dm_test_acpi_inject_dsdt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);