acpi: Support generation of a generic register
Allow writing out a generic register.
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Wolfgang Wallner <wolfgang.wallner@br-automation.com>
[bmeng: Fix build failures on Sandbox]
Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index 5d94a88..11461e1 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -20,6 +20,7 @@
/* ACPI descriptor values for common descriptors: SERIAL_BUS means I2C */
#define ACPI_DESCRIPTOR_LARGE BIT(7)
+#define ACPI_DESCRIPTOR_REGISTER (ACPI_DESCRIPTOR_LARGE | 2)
#define ACPI_DESCRIPTOR_INTERRUPT (ACPI_DESCRIPTOR_LARGE | 9)
#define ACPI_DESCRIPTOR_GPIO (ACPI_DESCRIPTOR_LARGE | 12)
#define ACPI_DESCRIPTOR_SERIAL_BUS (ACPI_DESCRIPTOR_LARGE | 14)
diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 4a60612..1f37c9c 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -13,6 +13,7 @@
#include <linux/types.h>
struct acpi_ctx;
+struct acpi_gen_regaddr;
struct acpi_gpio;
/* Top 4 bits of the value used to indicate a three-byte length value */
@@ -21,6 +22,8 @@
#define ACPI_METHOD_NARGS_MASK 0x7
#define ACPI_METHOD_SERIALIZED_MASK BIT(3)
+#define ACPI_END_TAG 0x79
+
/* ACPI Op/Prefix codes */
enum {
ZERO_OP = 0x00,
@@ -319,6 +322,31 @@
void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
/**
+ * acpigen_write_resourcetemplate_header() - Write a ResourceTemplate header
+ *
+ * @ctx: ACPI context pointer
+ */
+void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_write_resourcetemplate_footer() - Write a ResourceTemplate footer
+ *
+ * @ctx: ACPI context pointer
+ */
+void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_write_register_resource() - Write a register resource
+ *
+ * This writes a header, the address information and a footer
+ *
+ * @ctx: ACPI context pointer
+ * @addr: Address to write
+ */
+void acpigen_write_register_resource(struct acpi_ctx *ctx,
+ const struct acpi_gen_regaddr *addr);
+
+/**
* acpigen_write_sleep() - Write a sleep operation
*
* @ctx: ACPI context pointer
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 1e0a489..45691b7 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -14,6 +14,7 @@
#include <uuid.h>
#include <acpi/acpigen.h>
#include <acpi/acpi_device.h>
+#include <acpi/acpi_table.h>
#include <dm/acpi.h>
u8 *acpigen_get_current(struct acpi_ctx *ctx)
@@ -299,6 +300,76 @@
acpigen_pop_len(ctx);
}
+static void acpigen_write_register(struct acpi_ctx *ctx,
+ const struct acpi_gen_regaddr *addr)
+{
+ /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */
+ acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER);
+ acpigen_emit_byte(ctx, 0x0c); /* Register Length 7:0 */
+ acpigen_emit_byte(ctx, 0x00); /* Register Length 15:8 */
+ acpigen_emit_byte(ctx, addr->space_id);
+ acpigen_emit_byte(ctx, addr->bit_width);
+ acpigen_emit_byte(ctx, addr->bit_offset);
+ acpigen_emit_byte(ctx, addr->access_size);
+ acpigen_emit_dword(ctx, addr->addrl);
+ acpigen_emit_dword(ctx, addr->addrh);
+}
+
+void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx)
+{
+ /*
+ * A ResourceTemplate() is a Buffer() with a
+ * (Byte|Word|DWord) containing the length, followed by one or more
+ * resource items, terminated by the end tag.
+ * (small item 0xf, len 1)
+ */
+ acpigen_emit_byte(ctx, BUFFER_OP);
+ acpigen_write_len_f(ctx);
+ acpigen_emit_byte(ctx, WORD_PREFIX);
+ ctx->len_stack[ctx->ltop++] = ctx->current;
+
+ /*
+ * Add two dummy bytes for the ACPI word (keep aligned with the
+ * calculation in acpigen_write_resourcetemplate_footer() below)
+ */
+ acpigen_emit_byte(ctx, 0x00);
+ acpigen_emit_byte(ctx, 0x00);
+}
+
+void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx)
+{
+ char *p = ctx->len_stack[--ctx->ltop];
+ int len;
+ /*
+ * See ACPI v6.3 section 6.4.2.9: End Tag
+ * 0x79 <checksum>
+ * 0x00 is treated as a good checksum according to the spec
+ * and is what iasl generates.
+ */
+ acpigen_emit_byte(ctx, ACPI_END_TAG);
+ acpigen_emit_byte(ctx, 0x00);
+
+ /*
+ * Start counting past the 2-bytes length added in
+ * acpigen_write_resourcetemplate_header() above
+ */
+ len = (char *)ctx->current - (p + 2);
+
+ /* patch len word */
+ p[0] = len & 0xff;
+ p[1] = (len >> 8) & 0xff;
+
+ acpigen_pop_len(ctx);
+}
+
+void acpigen_write_register_resource(struct acpi_ctx *ctx,
+ const struct acpi_gen_regaddr *addr)
+{
+ acpigen_write_resourcetemplate_header(ctx);
+ acpigen_write_register(ctx, addr);
+ acpigen_write_resourcetemplate_footer(ctx);
+}
+
/*
* ToUUID(uuid)
*
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index 46792d8..822641a 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -12,6 +12,7 @@
#include <malloc.h>
#include <acpi/acpigen.h>
#include <acpi/acpi_device.h>
+#include <acpi/acpi_table.h>
#include <asm/gpio.h>
#include <asm/unaligned.h>
#include <dm/acpi.h>
@@ -947,3 +948,48 @@
return 0;
}
DM_TEST(dm_test_acpi_scope, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test writing a resource template */
+static int dm_test_acpi_resource_template(struct unit_test_state *uts)
+{
+ struct acpi_gen_regaddr addr;
+ struct acpi_ctx *ctx;
+ u8 *ptr;
+
+ ut_assertok(alloc_context(&ctx));
+ ptr = acpigen_get_current(ctx);
+
+ addr.space_id = ACPI_ADDRESS_SPACE_EC;
+ addr.bit_width = 32;
+ addr.bit_offset = 8;
+ addr.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
+ addr.addrl = TEST_INT64 & 0xffffffff;
+ addr.addrh = TEST_INT64 >> 32;
+ acpigen_write_register_resource(ctx, &addr);
+
+ ut_asserteq(BUFFER_OP, *ptr++);
+ ut_asserteq(0x17, acpi_test_get_length(ptr));
+ ptr += 3;
+ ut_asserteq(WORD_PREFIX, *ptr++);
+ ut_asserteq(0x11, get_unaligned((u16 *)ptr));
+ ptr += 2;
+ ut_asserteq(ACPI_DESCRIPTOR_REGISTER, *ptr++);
+ ut_asserteq(0xc, *ptr++);
+ ut_asserteq(0, *ptr++);
+ ut_asserteq(ACPI_ADDRESS_SPACE_EC, *ptr++);
+ ut_asserteq(32, *ptr++);
+ ut_asserteq(8, *ptr++);
+ ut_asserteq(ACPI_ACCESS_SIZE_DWORD_ACCESS, *ptr++);
+ ut_asserteq(TEST_INT64 & 0xffffffff, get_unaligned((u32 *)ptr));
+ ptr += 4;
+ ut_asserteq(TEST_INT64 >> 32, get_unaligned((u32 *)ptr));
+ ptr += 4;
+ ut_asserteq(ACPI_END_TAG, *ptr++);
+ ut_asserteq(0x00, *ptr++);
+ ut_asserteq_ptr(ptr, ctx->current);
+
+ free_context(&ctx);
+
+ return 0;
+}
+DM_TEST(dm_test_acpi_resource_template, 0);