diff mbox series

[v8,23/35] acpi: Add support for writing a Power Resource

Message ID 20200426152618.125646-21-sjg@chromium.org
State Superseded
Headers show
Series dm: Add programmatic generation of ACPI tables (part B) | expand

Commit Message

Simon Glass April 26, 2020, 3:26 p.m. UTC
These are used in ACPI to disable power to various pats of the system when
in sleep. Add a way to create a power resource, with the caller finishing
off the details.

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

Changes in v8: None
Changes in v3: None
Changes in v2: None

 include/acpi/acpigen.h | 22 ++++++++++++++++++++++
 lib/acpi/acpigen.c     | 22 ++++++++++++++++++++++
 test/dm/acpigen.c      | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)
diff mbox series

Patch

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index c595bb9efa..7d9a02805b 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -45,6 +45,7 @@  enum {
 	AND_OP			= 0x7b,
 	OR_OP			= 0x7d,
 	NOT_OP			= 0x80,
+	POWER_RES_OP		= 0x84,
 	RETURN_OP		= 0xa4,
 };
 
@@ -310,4 +311,25 @@  void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
  */
 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
 
+/**
+ * acpigen_write_power_res() - Write a power resource
+ *
+ * Name (_PRx, Package(One) { name })
+ * ...
+ * PowerResource (name, level, order)
+ *
+ * The caller should fill in the rest of the power resource and then call
+ * acpigen_pop_len() to close it off
+ *
+ * @ctx: ACPI context pointer
+ * @name: Name of power resource (e.g. "PRIC")
+ * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
+ * @order: Order that this must be enabled/disabled (e.g. 0)
+ * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
+ * @dev_states_count: Number of dev states
+ */
+void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
+			     uint order, const char *const dev_states[],
+			     size_t dev_states_count);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 82c8b1e18f..265048e656 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -326,6 +326,28 @@  int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
 	return 0;
 }
 
+void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
+			     uint order, const char *const dev_states[],
+			     size_t dev_states_count)
+{
+	size_t i;
+
+	for (i = 0; i < dev_states_count; i++) {
+		acpigen_write_name(ctx, dev_states[i]);
+		acpigen_write_package(ctx, 1);
+		acpigen_emit_simple_namestring(ctx, name);
+		acpigen_pop_len(ctx);		/* Package */
+	}
+
+	acpigen_emit_ext_op(ctx, POWER_RES_OP);
+
+	acpigen_write_len_f(ctx);
+
+	acpigen_emit_simple_namestring(ctx, name);
+	acpigen_emit_byte(ctx, level);
+	acpigen_emit_word(ctx, order);
+}
+
 /* Sleep (ms) */
 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
 {
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index e059d6f80d..c92f6cdb0b 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -696,3 +696,44 @@  static int dm_test_acpi_misc(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_acpi_misc, 0);
+
+/* Test writing an ACPI power resource */
+static int dm_test_acpi_power_res(struct unit_test_state *uts)
+{
+	const char *const states[] = { "_PR0", "_PR3" };
+	const char *name = "PRIC";
+	const int level = 3;
+	const int order = 2;
+	struct acpi_ctx *ctx;
+	u8 *ptr;
+
+	ut_assertok(alloc_context(&ctx));
+
+	ptr = acpigen_get_current(ctx);
+
+	/* PowerResource (PRIC, 0, 0) */
+	acpigen_write_power_res(ctx, name, level, order, states,
+				ARRAY_SIZE(states));
+	ut_asserteq(0x28, acpigen_get_current(ctx) - ptr);
+	ut_asserteq(NAME_OP, ptr[0]);
+	ut_asserteq_strn(states[0], (char *)ptr + 1);
+	ut_asserteq(8, get_length(ptr + 6));
+	ut_asserteq_strn(name, (char *)ptr + 0xa);
+
+	ut_asserteq_strn(states[1], (char *)ptr + 0xf);
+	ut_asserteq(8, get_length(ptr + 0x14));
+	ut_asserteq_strn(name, (char *)ptr + 0x18);
+
+	ut_asserteq(POWER_RES_OP, ptr[0x1d]);
+	ut_asserteq_strn(name, (char *)ptr + 0x21);
+	ut_asserteq(level, ptr[0x25]);
+	ut_asserteq(order, get_unaligned((u16 *)(ptr + 0x26)));
+
+	/* The length is not set - caller must use acpigen_pop_len() */
+	ut_asserteq(1, ctx->ltop);
+
+	free_context(&ctx);
+
+	return 0;
+}
+DM_TEST(dm_test_acpi_power_res, 0);