[PATCH v3 12/57] acpi: Add support for conditions and return values

Simon Glass
2020-09-06T21:43:20Z

Add functions to support generating ACPI code for condition checks and
return values.

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

(no changes since v1)

include/acpi/acpigen.h | 93 ++++++++++++++++++++++++++++++++++++++++++
lib/acpi/acpigen.c | 68 ++++++++++++++++++++++++++++++
test/dm/acpigen.c | 93 +++++++++++++++++++++++++++++++++++++++++-
3 files changed, 253 insertions(+), 1 deletion(-)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index a9b70123c0a..fa9409e3528 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -52,12 +52,24 @@ enum {
LOCAL5_OP = 0x65,
LOCAL6_OP = 0x66,
LOCAL7_OP = 0x67,
+ ARG0_OP = 0x68,
+ ARG1_OP = 0x69,
+ ARG2_OP = 0x6a,
+ ARG3_OP = 0x6b,
+ ARG4_OP = 0x6c,
+ ARG5_OP = 0x6d,
+ ARG6_OP = 0x6e,
STORE_OP = 0x70,
AND_OP = 0x7b,
OR_OP = 0x7d,
NOT_OP = 0x80,
DEVICE_OP = 0x82,
POWER_RES_OP = 0x84,
+ LEQUAL_OP = 0x93,
+ TO_BUFFER_OP = 0x96,
+ TO_INTEGER_OP = 0x99,
+ IF_OP = 0xa0,
+ ELSE_OP = 0xa1,
RETURN_OP = 0xa4,
};

@@ -573,4 +585,85 @@ int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
*/
void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level);

+/**
+ * acpigen_write_if() - Write an If block
+ *
+ * This requires a call to acpigen_pop_len() to complete the block
+ *
+ * @ctx: ACPI context pointer
+ */
+void acpigen_write_if(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_write_if_lequal_op_int() - Write comparison between op and integer
+ *
+ * Generates ACPI code for checking if operand1 and operand2 are equal
+ *
+ * If (Lequal (op, val))
+ *
+ * @ctx: ACPI context pointer
+ * @op: Operand to check
+ * @val: Value to check against
+ */
+void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val);
+
+/**
+ * acpigen_write_else() - Write an Ef block
+ *
+ * This requires a call to acpigen_pop_len() to complete the block
+ *
+ * @ctx: ACPI context pointer
+ */
+void acpigen_write_else(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_write_to_buffer() - Write a ToBuffer operation
+ *
+ * E.g.: to generate: ToBuffer (Arg0, Local0)
+ * use acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP)
+ *
+ * @ctx: ACPI context pointer
+ * @src: Source argument
+ * @dst: Destination argument
+ */
+void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst);
+
+/**
+ * acpigen_write_to_integer() - Write a ToInteger operation
+ *
+ * E.g.: to generate: ToInteger (Arg0, Local0)
+ * use acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP)
+ *
+ * @ctx: ACPI context pointer
+ * @src: Source argument
+ * @dst: Destination argument
+ */
+void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst);
+
+/**
+ * acpigen_write_return_byte_buffer() - Write a return of a byte buffer
+ *
+ * @ctx: ACPI context pointer
+ * @arr: Array of bytes to return
+ * @size: Number of bytes
+ */
+void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
+ size_t size);
+
+/**
+ * acpigen_write_return_singleton_buffer() - Write a return of a 1-byte buffer
+ *
+ * @ctx: ACPI context pointer
+ * @arg: Byte to return
+ */
+void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg);
+
+/**
+ * acpigen_write_return_byte() - Write a return of a byte
+ *
+ * @ctx: ACPI context pointer
+ * @arg: Byte to return
+ */
+void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg);
+
#endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 527de89b1e1..2518bf83dda 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -541,6 +541,74 @@ void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
acpigen_emit_ext_op(ctx, DEBUG_OP);
}

+void acpigen_write_if(struct acpi_ctx *ctx)
+{
+ acpigen_emit_byte(ctx, IF_OP);
+ acpigen_write_len_f(ctx);
+}
+
+void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val)
+{
+ acpigen_write_if(ctx);
+ acpigen_emit_byte(ctx, LEQUAL_OP);
+ acpigen_emit_byte(ctx, op);
+ acpigen_write_integer(ctx, val);
+}
+
+void acpigen_write_else(struct acpi_ctx *ctx)
+{
+ acpigen_emit_byte(ctx, ELSE_OP);
+ acpigen_write_len_f(ctx);
+}
+
+void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst)
+{
+ acpigen_emit_byte(ctx, TO_BUFFER_OP);
+ acpigen_emit_byte(ctx, src);
+ acpigen_emit_byte(ctx, dst);
+}
+
+void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst)
+{
+ acpigen_emit_byte(ctx, TO_INTEGER_OP);
+ acpigen_emit_byte(ctx, src);
+ acpigen_emit_byte(ctx, dst);
+}
+
+void acpigen_write_byte_buffer(struct acpi_ctx *ctx, u8 *arr, size_t size)
+{
+ size_t i;
+
+ acpigen_emit_byte(ctx, BUFFER_OP);
+ acpigen_write_len_f(ctx);
+ acpigen_write_integer(ctx, size);
+
+ for (i = 0; i < size; i++)
+ acpigen_emit_byte(ctx, arr[i]);
+
+ acpigen_pop_len(ctx);
+}
+
+void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
+ size_t size)
+{
+ acpigen_emit_byte(ctx, RETURN_OP);
+ acpigen_write_byte_buffer(ctx, arr, size);
+}
+
+void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg)
+{
+ u8 buf = arg;
+
+ acpigen_write_return_byte_buffer(ctx, &buf, 1);
+}
+
+void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg)
+{
+ acpigen_emit_byte(ctx, RETURN_OP);
+ acpigen_write_byte(ctx, arg);
+}
+
/**
* acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
*
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index 16ae7025856..cce19f11209 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -1098,7 +1098,7 @@ static int dm_test_acpi_write_name(struct unit_test_state *uts)
}
DM_TEST(dm_test_acpi_write_name, 0);

-/* Test emitting a string */
+/* Test emitting a _PRW component */
static int dm_test_acpi_write_prw(struct unit_test_state *uts)
{
struct acpi_ctx *ctx;
@@ -1127,3 +1127,94 @@ static int dm_test_acpi_write_prw(struct unit_test_state *uts)
return 0;
}
DM_TEST(dm_test_acpi_write_prw, 0);
+
+/* Test emitting writing conditionals */
+static int dm_test_acpi_write_cond(struct unit_test_state *uts)
+{
+ struct acpi_ctx *ctx;
+ u8 *ptr;
+
+ ut_assertok(alloc_context(&ctx));
+
+ ptr = acpigen_get_current(ctx);
+ acpigen_write_if(ctx);
+ acpigen_pop_len(ctx);
+ ut_asserteq(IF_OP, *ptr++);
+ ut_asserteq(3, acpi_test_get_length(ptr));
+ ptr += 3;
+
+ acpigen_write_else(ctx);
+ acpigen_pop_len(ctx);
+ ut_asserteq(ELSE_OP, *ptr++);
+ ut_asserteq(3, acpi_test_get_length(ptr));
+ ptr += 3;
+
+ acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, 5);
+ acpigen_pop_len(ctx);
+ ut_asserteq(IF_OP, *ptr++);
+ ut_asserteq(7, acpi_test_get_length(ptr));
+ ptr += 3;
+ ut_asserteq(LEQUAL_OP, *ptr++);
+ ut_asserteq(LOCAL1_OP, *ptr++);
+ ut_asserteq(BYTE_PREFIX, *ptr++);
+ ut_asserteq(5, *ptr++);
+
+ ut_asserteq_ptr(ptr, ctx->current);
+
+ free_context(&ctx);
+
+ return 0;
+}
+DM_TEST(dm_test_acpi_write_cond, 0);
+
+/* Test emitting writing return values and ToBuffer/ToInteger */
+static int dm_test_acpi_write_return(struct unit_test_state *uts)
+{
+ int len = sizeof(TEST_STRING);
+ struct acpi_ctx *ctx;
+ u8 *ptr;
+
+ ut_assertok(alloc_context(&ctx));
+
+ ptr = acpigen_get_current(ctx);
+ acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
+ ut_asserteq(TO_BUFFER_OP, *ptr++);
+ ut_asserteq(ARG0_OP, *ptr++);
+ ut_asserteq(LOCAL0_OP, *ptr++);
+
+ acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP);
+ ut_asserteq(TO_INTEGER_OP, *ptr++);
+ ut_asserteq(ARG0_OP, *ptr++);
+ ut_asserteq(LOCAL0_OP, *ptr++);
+
+ acpigen_write_return_byte_buffer(ctx, (u8 *)TEST_STRING, len);
+ ut_asserteq(RETURN_OP, *ptr++);
+ ut_asserteq(BUFFER_OP, *ptr++);
+ ut_asserteq(5 + len, acpi_test_get_length(ptr));
+ ptr += 3;
+ ut_asserteq(BYTE_PREFIX, *ptr++);
+ ut_asserteq(len, *ptr++);
+ ut_asserteq_mem(TEST_STRING, ptr, len);
+ ptr += len;
+
+ acpigen_write_return_singleton_buffer(ctx, 123);
+ len = 1;
+ ut_asserteq(RETURN_OP, *ptr++);
+ ut_asserteq(BUFFER_OP, *ptr++);
+ ut_asserteq(4 + len, acpi_test_get_length(ptr));
+ ptr += 3;
+ ut_asserteq(ONE_OP, *ptr++);
+ ut_asserteq(123, *ptr++);
+
+ acpigen_write_return_byte(ctx, 43);
+ ut_asserteq(RETURN_OP, *ptr++);
+ ut_asserteq(BYTE_PREFIX, *ptr++);
+ ut_asserteq(43, *ptr++);
+
+ ut_asserteq_ptr(ptr, ctx->current);
+
+ free_context(&ctx);
+
+ return 0;
+}
+DM_TEST(dm_test_acpi_write_return, 0);
--
2.28.0.526.ge36021eeef-goog