All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v2 10/44] acpi: Support generation of a generic register
Date: Tue,  7 Jul 2020 21:32:11 -0600	[thread overview]
Message-ID: <20200708033246.2626378-8-sjg@chromium.org> (raw)
In-Reply-To: <20200708033246.2626378-1-sjg@chromium.org>

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>
---

(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 <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 @@ 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 <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 @@ 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 <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 1031185b96..d1bdd18ce5 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 @@ 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);
-- 
2.27.0.383.g050319c2ae-goog

  parent reply	other threads:[~2020-07-08  3:32 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-08  3:32 [PATCH v2 00/44] x86: Programmatic generation of ACPI tables (Part C) Simon Glass
2020-07-08  3:32 ` [PATCH v2 01/44] binman: Allow setting the ROM offset Simon Glass
2020-07-13  5:24   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 02/44] binman: Refactor binman_entry_find() to allow other nodes Simon Glass
2020-07-13  5:24   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 03/44] binman: Add way to locate an entry in memory Simon Glass
2020-07-13  5:24   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 04/44] acpi: Allow creating the GNVS to fail Simon Glass
2020-07-08  3:32 ` [PATCH v2 05/44] dtoc: Support ACPI paths in of-platdata Simon Glass
2020-07-08  3:32 ` [PATCH v2 06/44] dm: core: Add a way of overriding the ACPI device path Simon Glass
2020-07-08  3:32 ` [PATCH v2 07/44] dm: acpi: Add support for the NHLT table Simon Glass
2020-07-08  3:32 ` [PATCH v2 08/44] acpi: Export functions to write sized values Simon Glass
2020-07-13  5:29   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 09/44] acpi: Support generation of a scope Simon Glass
2020-07-13  5:29   ` Bin Meng
2020-07-13  7:46   ` Bin Meng
2020-07-08  3:32 ` Simon Glass [this message]
2020-07-08  3:32 ` [PATCH v2 11/44] acpi: mmc: Generate ACPI info for the PCI SD Card Simon Glass
2020-07-13  5:34   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 12/44] x86: Add bindings for NHLT Simon Glass
2020-07-13  5:34   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 13/44] acpi: Support generation of a device Simon Glass
2020-07-08  3:32 ` [PATCH v2 14/44] acpi: Support writing named values Simon Glass
2020-07-08  3:32 ` [PATCH v2 15/44] x86: Add support for building up an NHLT structure Simon Glass
2020-07-08  3:32 ` [PATCH v2 16/44] sound: Add an ACPI driver for Dialog Semicondutor da7219 Simon Glass
2020-07-08  3:32 ` [PATCH v2 17/44] sound: Add an ACPI driver for Maxim MAX98357ac Simon Glass
2020-07-14  1:28   ` Bin Meng
2020-07-14  5:01     ` Bin Meng
2020-07-14 13:25       ` Simon Glass
2020-07-15  1:05         ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 18/44] x86: pinctrl: Add a way to get the pinctrl reg address Simon Glass
2020-07-08  3:32 ` [PATCH v2 19/44] x86: pinctrl: Update comment for intel_pinctrl_get_pad() Simon Glass
2020-07-13  5:39   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 20/44] x86: pinctrl: Add multi-ACPI control Simon Glass
2020-07-08  3:32 ` [PATCH v2 21/44] x86: pinctrl: Set up itss in the probe() method Simon Glass
2020-07-08 11:10   ` Wolfgang Wallner
2020-07-13  5:44   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 22/44] x86: pinctrl: Drop the acpi_path member Simon Glass
2020-07-13  5:44   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 23/44] x86: Add error checking for csrt table generation Simon Glass
2020-07-08  3:32 ` [PATCH v2 24/44] x86: apl: Use memory-mapped access for VBT Simon Glass
2020-07-08  3:32 ` [PATCH v2 25/44] x86: gpio: Add support for obtaining ACPI info for a GPIO Simon Glass
2020-07-13  5:45   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 26/44] i2c: designware_i2c: Add a little more debugging Simon Glass
2020-07-08  3:32 ` [PATCH v2 27/44] i2c: Add log_ret() on error Simon Glass
2020-07-08  3:32 ` [PATCH v2 28/44] i2c: designware_i2c: Support ACPI table generation Simon Glass
2020-07-08 11:11   ` Wolfgang Wallner
2020-07-13  5:54     ` Bin Meng
2020-07-13  6:07       ` Bin Meng
2020-07-14  1:59         ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 29/44] p2sb: Add a method to hide the bus Simon Glass
2020-07-08  3:32 ` [PATCH v2 30/44] x86: apl: Support set_hide() in p2sb driver Simon Glass
2020-07-08  3:32 ` [PATCH v2 31/44] x86: apl: Hide the p2sb on exit from U-Boot Simon Glass
2020-07-08  3:32 ` [PATCH v2 32/44] pmc: Move common registers to the header file Simon Glass
2020-07-13  5:58   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 33/44] x86: irq: Support flags for acpi_gpe Simon Glass
2020-07-08  3:32 ` [PATCH v2 34/44] x86: apl: Fix save/restore of ITSS priorities Simon Glass
2020-07-08 11:11   ` Wolfgang Wallner
2020-07-13  5:58   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 35/44] x86: Add debugging to table writing Simon Glass
2020-07-08  3:32 ` [PATCH v2 36/44] x86: apl: Set the correct boot mode in the FSP-M code Simon Glass
2020-07-13  6:02   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 37/44] x86: apl: Adjust FSP-M code to avoid hard-coded address Simon Glass
2020-07-13  6:03   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 38/44] x86: Store the coreboot table address in global_data Simon Glass
2020-07-08  3:32 ` [PATCH v2 39/44] x86: mp: Allow use of mp_run_on_cpus() without MP Simon Glass
2020-07-13  6:05   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 40/44] x86: Update the comment about booting for FSP2 Simon Glass
2020-07-08  3:32 ` [PATCH v2 41/44] x86: Drop setup_pcat_compatibility() Simon Glass
2020-07-13  6:17   ` Bin Meng
2020-07-08  3:32 ` [PATCH v2 42/44] x86: acpi: Correct the version of the MADT Simon Glass
2020-07-08  3:32 ` [PATCH v2 43/44] x86: Rename board_final_cleanup() to board_final_init() Simon Glass
2020-07-08  3:32 ` [PATCH v2 44/44] acpi: Enable ACPI table generation by default on x86 Simon Glass
2020-07-13  6:06   ` Bin Meng
2020-07-08  7:21 ` [PATCH v2 20/44] x86: pinctrl: Add multi-ACPI control Wolfgang Wallner
2020-07-08 11:11 ` [PATCH v2 38/44] x86: Store the coreboot table address in global_data Wolfgang Wallner
2020-07-13  7:10 ` [PATCH v2 00/44] x86: Programmatic generation of ACPI tables (Part C) Bin Meng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200708033246.2626378-8-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.