From patchwork Wed Jul 8 03:32:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 240972 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Tue, 7 Jul 2020 21:32:11 -0600 Subject: [PATCH v2 10/44] acpi: Support generation of a generic register In-Reply-To: <20200708033246.2626378-1-sjg@chromium.org> References: <20200708033246.2626378-1-sjg@chromium.org> Message-ID: <20200708033246.2626378-8-sjg@chromium.org> Allow writing out a generic register. Signed-off-by: Simon Glass Reviewed-by: Bin Meng Reviewed-by: Wolfgang Wallner --- (no changes since v1) include/acpi/acpi_device.h | 1 + include/acpi/acpigen.h | 28 +++++++++++++++ lib/acpi/acpigen.c | 71 ++++++++++++++++++++++++++++++++++++++ test/dm/acpigen.c | 46 ++++++++++++++++++++++++ 4 files changed, 146 insertions(+) diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h index 0bb9a4eec1..15800b2e24 100644 --- a/include/acpi/acpi_device.h +++ b/include/acpi/acpi_device.h @@ -20,6 +20,7 @@ struct udevice; /* 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 4a606125de..1f37c9c31c 100644 --- a/include/acpi/acpigen.h +++ b/include/acpi/acpigen.h @@ -13,6 +13,7 @@ #include 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 @@ struct acpi_gpio; #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, @@ -318,6 +321,31 @@ void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name, */ 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 * diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c index 1e0a489d7b..45691b7961 100644 --- a/lib/acpi/acpigen.c +++ b/lib/acpi/acpigen.c @@ -14,6 +14,7 @@ #include #include #include +#include #include u8 *acpigen_get_current(struct acpi_ctx *ctx) @@ -299,6 +300,76 @@ void acpigen_write_sta(struct acpi_ctx *ctx, uint status) 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 + * 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 1031185b96..d1bdd18ce5 100644 --- a/test/dm/acpigen.c +++ b/test/dm/acpigen.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -947,3 +948,48 @@ static int dm_test_acpi_scope(struct unit_test_state *uts) 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, 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);