All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v4 04/42] dm: core: Add macros to access the new linker lists
Date: Mon, 15 Mar 2021 17:25:14 +1300	[thread overview]
Message-ID: <20210315172537.v4.4.I203d6c2e1211d979289198ee3410009acd7353e6@changeid> (raw)
In-Reply-To: <20210315042553.1932494-1-sjg@chromium.org>

Add macros which work with instantiated devices and uclasses, as created
at build time by dtoc. Include variants that can be used in data
structures.

These are mostly used by dtoc but it is worth documenting them fully for
the occasional case where they might come up in user code.

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

(no changes since v3)

Changes in v3:
- Drop the decl() versions of the macros
- Add full documentation for the macros

 include/dm/device-internal.h | 75 ++++++++++++++++++++++++++++++++++++
 include/dm/device.h          | 22 +++++++++++
 include/dm/uclass-internal.h | 49 +++++++++++++++++++++++
 include/dm/uclass.h          | 31 +++++++++++++++
 include/linker_lists.h       |  4 +-
 5 files changed, 179 insertions(+), 2 deletions(-)

diff --git a/include/dm/device-internal.h b/include/dm/device-internal.h
index 39406c3f352..71e5c75028d 100644
--- a/include/dm/device-internal.h
+++ b/include/dm/device-internal.h
@@ -10,11 +10,86 @@
 #ifndef _DM_DEVICE_INTERNAL_H
 #define _DM_DEVICE_INTERNAL_H
 
+#include <linker_lists.h>
 #include <dm/ofnode.h>
 
 struct device_node;
 struct udevice;
 
+/*
+ * These two macros DM_DEVICE_INST and DM_DEVICE_REF are only allowed in code
+ * generated by dtoc, because the ordering is important and if other instances
+ * creep in then they may mess up the ordering expected by dtoc.
+ *
+ * It is OK to use them with 'extern' though, since that does not actually
+ * add a new record to the linker_list.
+ */
+
+/**
+ * DM_DEVICE_INST() - Declare a bound device ready for run-time use
+ *
+ * This adds an actual struct udevice to a list which is found by driver model
+ * on start-up.
+ *
+ * For example:
+ *
+ * extern U_BOOT_DRIVER(sandbox_fixed_clock);
+ * extern DM_UCLASS_INST(clk);
+ *
+ * DM_DEVICE_INST(clk_fixed) = {
+ *	.driver		= DM_DRIVER_REF(sandbox_fixed_clock),
+ *	.name		= "sandbox_fixed_clock",
+ *	.plat_		= &_sandbox_fixed_clock_plat_clk_fixed,
+ *	.uclass		= DM_UCLASS_REF(clk),
+ *	...
+ *	.seq_		= 0,
+ * };
+ *
+ * @_name: Name of the udevice. This must be a valid C identifier, used by the
+ *	linker_list.
+ */
+#define DM_DEVICE_INST(_name)						\
+	ll_entry_declare(struct udevice, _name, udevice)
+
+/**
+ * DM_DEVICE_REF() - Get a reference to a device
+ *
+ * This is useful in data structures and code for referencing a udevice at
+ * build time. Before this is used, an extern DM_DEVICE_INST() must have been
+ * declared.
+ *
+ * For example:
+ *
+ * extern DM_DEVICE_INST(clk_fixed);
+ *
+ * struct udevice *devs[] = {
+ *	DM_DEVICE_REF(clk_fixed),
+ * };
+ *
+ * @_name: Name of the udevice. This must be a valid C identifier, used by the
+ *	linker_list
+ * @returns struct udevice * for the device
+ */
+#define DM_DEVICE_REF(_name)						\
+	ll_entry_ref(struct udevice, _name, udevice)
+
+/**
+ * DM_DEVICE_GET() - Get a pointer to a given device
+ *
+ * This is similar to DM_DEVICE_REF() except that it does not need the extern
+ * declaration before it. However it cannot be used in a data structures, only
+ * in code within a function.
+ *
+ * For example:
+ *
+ * void some_function() {
+ *	struct udevice *dev = DM_DEVICE_GET(clk_fixed);
+ * ...
+ * }
+ */
+#define DM_DEVICE_GET(__name)						\
+	ll_entry_get(struct udevice, __name, udevice)
+
 /**
  * device_bind() - Create a device and bind it to a driver
  *
diff --git a/include/dm/device.h b/include/dm/device.h
index 45010b4df92..5b8f27d455b 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -362,6 +362,28 @@ struct driver {
 #define DM_DRIVER_GET(__name)						\
 	ll_entry_get(struct driver, __name, driver)
 
+/**
+ * DM_DRIVER_REF() - Get a reference to a driver
+ *
+ * This is useful in data structures and code for referencing a driver at
+ * build time. Before this is used, an extern U_BOOT_DRIVER() must have been
+ * declared.
+ *
+ * For example:
+ *
+ * extern U_BOOT_DRIVER(sandbox_fixed_clock);
+ *
+ * struct driver *drvs[] = {
+ *	DM_DRIVER_REF(sandbox_fixed_clock),
+ * };
+ *
+ * @_name: Name of the driver. This must be a valid C identifier, used by the
+ *	linker_list
+ * @returns struct driver * for the driver
+ */
+#define DM_DRIVER_REF(_name)					\
+	ll_entry_ref(struct driver, _name, driver)
+
 /**
  * Declare a macro to state a alias for a driver name. This macro will
  * produce no code but its information will be parsed by tools like
diff --git a/include/dm/uclass-internal.h b/include/dm/uclass-internal.h
index c5a464be7c4..f2a780682b4 100644
--- a/include/dm/uclass-internal.h
+++ b/include/dm/uclass-internal.h
@@ -11,6 +11,55 @@
 
 #include <dm/ofnode.h>
 
+/*
+ * These next two macros DM_UCLASS_INST() and DM_UCLASS_REF() are only allowed
+ * in code generated by dtoc, because the ordering is important and if other
+ * instances creep in then they may mess up the ordering expected by dtoc.
+ *
+ * It is OK to use them with 'extern' though, since that does not actually
+ * add a new record to the linker_list.
+ */
+
+/**
+ * DM_UCLASS_INST() - Declare a uclass ready for run-time use
+ *
+ * This adds an actual struct uclass to a list which is found by driver model
+ * on start-up.
+ *
+ * For example:
+ *
+ * DM_UCLASS_INST(clk) = {
+ *	.uc_drv		= DM_UCLASS_DRIVER_REF(clk),
+ * ...
+ * };
+ *
+ * @_name: Name of the uclass. This must be a valid C identifier, used by the
+ *	linker_list.
+ */
+#define DM_UCLASS_INST(_name)						\
+	ll_entry_declare(struct uclass, _name, uclass)
+
+/**
+ * DM_UCLASS_REF() - Get a reference to a uclass
+ *
+ * This is useful for referencing a uclass at build time. Before this is used,
+ * an extern DM_UCLASS_INST() must have been declared.
+ *
+ * For example:
+ *
+ * extern DM_UCLASS_INST(clk);
+ *
+ * struct uclass *ucs[] = {
+ *	DM_UCLASS_REF(clk),
+ * }
+ *
+ * @_name: Name of the uclass. This must be a valid C identifier, used by the
+ *	linker_list
+ * @returns struct uclass * for the device
+ */
+#define DM_UCLASS_REF(_name)						\
+	ll_entry_ref(struct uclass, _name, uclass)
+
 /**
  * uclass_set_priv() - Set the private data for a uclass
  *
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index d95683740cb..6752d8ee0be 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -114,6 +114,37 @@ struct uclass_driver {
 #define UCLASS_DRIVER(__name)						\
 	ll_entry_declare(struct uclass_driver, __name, uclass_driver)
 
+/*
+ * These two macros DM_UCLASS_DRIVER_REF and DM_UCLASS_DRIVER_REF are only
+ * allowed in code generated by dtoc, because the ordering is important and if
+ * other instances creep in then they may mess up the ordering expected by dtoc.
+ *
+ * It is OK to use them with 'extern' though, since that does not actually
+ * add a new record to the linker_list.
+ */
+
+/**
+ * DM_UCLASS_DRIVER_REF() - Get a reference to a uclass driver
+ *
+ * This is useful in data structures and code for referencing a uclass_driver at
+ * build time. Before this is used, an extern UCLASS_DRIVER() must have been
+ * declared.
+ *
+ * For example:
+ *
+ * extern UCLASS_DRIVER(clk);
+ *
+ * struct uclass_driver *drvs[] = {
+ *	DM_UCLASS_DRIVER_REF(clk),
+ * };
+ *
+ * @_name: Name of the uclass_driver. This must be a valid C identifier, used by
+ *	the linker_list.
+ * @returns struct uclass_driver * for the uclass driver
+ */
+#define DM_UCLASS_DRIVER_REF(_name)					\
+	ll_entry_ref(struct uclass_driver, _name, uclass_driver)
+
 /**
  * uclass_get_priv() - Get the private data for a uclass
  *
diff --git a/include/linker_lists.h b/include/linker_lists.h
index 0ca30da4172..81a280a8841 100644
--- a/include/linker_lists.h
+++ b/include/linker_lists.h
@@ -214,8 +214,8 @@
 /**
  * ll_entry_ref() - Get a reference to a linker-generated array entry
  *
- * Once ll_entry_decl() has been used to declare the reference, this macro
- * allows the entry to be accessed.
+ * Once an extern ll_entry_declare() has been used to declare the reference,
+ * this macro allows the entry to be accessed.
  *
  * This is like ll_entry_get(), but without the extra code, so it is suitable
  * for putting into data structures.
-- 
2.31.0.rc2.261.g7f71774620-goog

  parent reply	other threads:[~2021-03-15  4:25 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15  4:25 [PATCH v4 00/42] dm: Implement OF_PLATDATA_INST in driver model (part E) Simon Glass
2021-03-15  4:25 ` [PATCH v4 01/42] dtoc: Drop use of DECL() macros Simon Glass
2021-03-15  4:25 ` [PATCH v4 02/42] sandbox: Drop debug message in os_spl_to_uboot() Simon Glass
2021-03-15  4:25 ` [PATCH v4 03/42] linker_lists: Allow use in data structures Simon Glass
2021-03-15  4:25 ` Simon Glass [this message]
2021-03-15  4:25 ` [PATCH v4 05/42] dm: core: Allow dropping run-time binding of devices Simon Glass
2021-03-15  4:25 ` [PATCH v4 06/42] dm: core: Adjust uclass setup with of-platdata Simon Glass
2021-03-15  4:25 ` [PATCH v4 07/42] dm: core: Set up driver model for OF_PLATDATA_INST Simon Glass
2021-03-15  4:25 ` [PATCH v4 08/42] dm: core: Skip adding uclasses with OF_PLATDATA_INST Simon Glass
2021-03-15  4:25 ` [PATCH v4 09/42] dm: Add the new dtoc-generated files to the build Simon Glass
2021-03-15  4:25 ` [PATCH v4 10/42] dm: core: Include dt-decl.h automatically Simon Glass
2021-03-15  4:25 ` [PATCH v4 11/42] dm: test: Avoid destroying uclasses with of-platdata-inst Simon Glass
2021-03-15  4:25 ` [PATCH v4 12/42] clk: sandbox: Move priv/plat data to a header file Simon Glass
2021-03-15  4:25 ` [PATCH v4 13/42] clk: fixed-rate: Export driver parts for OF_PLATDATA_INST Simon Glass
2021-03-15  4:25 ` [PATCH v4 14/42] clk: sandbox: Create a special fixed-rate driver Simon Glass
2021-03-15  4:25 ` [PATCH v4 15/42] dm: core: Drop device_get_by_driver_info() Simon Glass
2021-03-15  4:25 ` [PATCH v4 16/42] dm: core: Drop uclass_find_device_by_phandle() with of-platdata Simon Glass
2021-03-15  4:25 ` [PATCH v4 17/42] sandbox: i2c: Move platdata structs to header files Simon Glass
2021-03-15  4:25 ` [PATCH v4 18/42] dm: Rename device_get_by_driver_info_idx() Simon Glass
2021-03-15  4:25 ` [PATCH v4 19/42] sandbox_spl: Increase SPL malloc() size Simon Glass
2021-03-15  4:25 ` [PATCH v4 20/42] sandbox: i2c: Support i2c emulation with of-platdata Simon Glass
2021-03-15  4:25 ` [PATCH v4 21/42] Revert "sandbox: Disable I2C emulators in SPL" Simon Glass
2021-03-15  4:25 ` [PATCH v4 22/42] sandbox: Create a new sandbox_noinst build Simon Glass
2021-03-15  4:25 ` [PATCH v4 23/42] test: Run sandbox_spl tests on sandbox_noinst Simon Glass
2021-03-15  4:25 ` [PATCH v4 24/42] azure/gitlab: Add tests for sandbox_noinst Simon Glass
2021-03-15  4:25 ` [PATCH v4 25/42] dm: core: Add an option to support SPL in read-only memory Simon Glass
2021-03-15  4:25 ` [PATCH v4 26/42] dm: core: Create a struct for device runtime info Simon Glass
2021-03-15  4:25 ` [PATCH v4 27/42] dm: core: Move flags to device-runtime info Simon Glass
2021-03-15  4:25 ` [PATCH v4 28/42] dm: core: Allow storing priv/plat data separately Simon Glass
2021-03-15  4:25 ` [PATCH v4 29/42] sandbox: Define a region for device priv/plat data Simon Glass
2021-03-15  4:25 ` [PATCH v4 30/42] dm: core: Use separate priv/plat data region Simon Glass
2021-03-15  4:25 ` [PATCH v4 31/42] dm: core: Add warnings to private / platform setters Simon Glass
2021-03-15  4:25 ` [PATCH v4 32/42] dm: doc: Tidy up of-platdata docs Simon Glass
2021-03-15  4:25 ` [PATCH v4 33/42] dm: doc: Add documentation for of-platdata-inst Simon Glass
2021-03-15  4:25 ` [PATCH v4 34/42] x86: Define a region for device priv/plat data Simon Glass
2021-03-15  4:25 ` [PATCH v4 35/42] x86: apl: Fix the header order in pmc Simon Glass
2021-03-15  4:25 ` [PATCH v4 36/42] x86: apl: Tell of-platdata about a required header file Simon Glass
2021-03-15  4:25 ` [PATCH v4 37/42] x86: itss: Tidy up bind() for of-platdata-inst Simon Glass
2021-03-15  4:25 ` [PATCH v4 38/42] x86: Support a fake PCI device with of-platdata-inst Simon Glass
2021-03-15  4:25 ` [PATCH v4 39/42] x86: Don't include reset driver in SPL Simon Glass
2021-03-15  4:25 ` [PATCH v4 40/42] x86: coral: Drop ACPI properties from of-platdata Simon Glass
2021-03-15  4:25 ` [PATCH v4 41/42] x86: coral: Drop TPM and ACPI interrupts from TPL Simon Glass
2021-03-15  4:25 ` [PATCH v4 42/42] x86: apl: Use read-only SPL and new of-platdata Simon Glass
2021-03-17  1:28 ` Simon Glass
2021-03-17  1:28 ` [PATCH v4 41/42] x86: coral: Drop TPM and ACPI interrupts from TPL Simon Glass
2021-03-17  1:28 ` [PATCH v4 39/42] x86: Don't include reset driver in SPL Simon Glass
2021-03-17  1:28 ` [PATCH v4 40/42] x86: coral: Drop ACPI properties from of-platdata Simon Glass
2021-03-17  1:28 ` [PATCH v4 38/42] x86: Support a fake PCI device with of-platdata-inst Simon Glass
2021-03-17  1:28 ` [PATCH v4 37/42] x86: itss: Tidy up bind() for of-platdata-inst Simon Glass
2021-03-17  1:28 ` [PATCH v4 36/42] x86: apl: Tell of-platdata about a required header file Simon Glass
2021-03-17  1:28 ` [PATCH v4 35/42] x86: apl: Fix the header order in pmc Simon Glass
2021-03-17  1:28 ` [PATCH v4 34/42] x86: Define a region for device priv/plat data Simon Glass
2021-03-17  1:28 ` [PATCH v4 32/42] dm: doc: Tidy up of-platdata docs Simon Glass
2021-03-17  1:28 ` [PATCH v4 33/42] dm: doc: Add documentation for of-platdata-inst Simon Glass
2021-03-17  1:28 ` [PATCH v4 31/42] dm: core: Add warnings to private / platform setters Simon Glass
2021-03-17  1:28 ` [PATCH v4 30/42] dm: core: Use separate priv/plat data region Simon Glass
2021-03-17  1:28 ` [PATCH v4 29/42] sandbox: Define a region for device priv/plat data Simon Glass
2021-03-17  1:28 ` [PATCH v4 28/42] dm: core: Allow storing priv/plat data separately Simon Glass
2021-03-17  1:28 ` [PATCH v4 27/42] dm: core: Move flags to device-runtime info Simon Glass
2021-03-17  1:28 ` [PATCH v4 26/42] dm: core: Create a struct for device runtime info Simon Glass
2021-03-17  1:28 ` [PATCH v4 25/42] dm: core: Add an option to support SPL in read-only memory Simon Glass
2021-03-17  1:28 ` [PATCH v4 24/42] azure/gitlab: Add tests for sandbox_noinst Simon Glass
2021-03-17  1:28 ` [PATCH v4 23/42] test: Run sandbox_spl tests on sandbox_noinst Simon Glass
2021-03-17  1:28 ` [PATCH v4 22/42] sandbox: Create a new sandbox_noinst build Simon Glass
2021-03-17  1:28 ` [PATCH v4 21/42] Revert "sandbox: Disable I2C emulators in SPL" Simon Glass
2021-03-17  1:28 ` [PATCH v4 20/42] sandbox: i2c: Support i2c emulation with of-platdata Simon Glass
2021-03-17  1:28 ` [PATCH v4 19/42] sandbox_spl: Increase SPL malloc() size Simon Glass
2021-03-17  1:28 ` [PATCH v4 18/42] dm: Rename device_get_by_driver_info_idx() Simon Glass
2021-03-17  1:28 ` [PATCH v4 17/42] sandbox: i2c: Move platdata structs to header files Simon Glass
2021-03-17  1:28 ` [PATCH v4 16/42] dm: core: Drop uclass_find_device_by_phandle() with of-platdata Simon Glass
2021-03-17  1:28 ` [PATCH v4 15/42] dm: core: Drop device_get_by_driver_info() Simon Glass
2021-03-17  1:28 ` [PATCH v4 14/42] clk: sandbox: Create a special fixed-rate driver Simon Glass
2021-03-17  1:28 ` [PATCH v4 13/42] clk: fixed-rate: Export driver parts for OF_PLATDATA_INST Simon Glass
2021-03-17  1:28 ` [PATCH v4 12/42] clk: sandbox: Move priv/plat data to a header file Simon Glass
2021-03-17  1:28 ` [PATCH v4 11/42] dm: test: Avoid destroying uclasses with of-platdata-inst Simon Glass
2021-03-17  1:28 ` [PATCH v4 10/42] dm: core: Include dt-decl.h automatically Simon Glass
2021-03-17  1:28 ` [PATCH v4 09/42] dm: Add the new dtoc-generated files to the build Simon Glass
2021-03-17  1:28 ` [PATCH v4 08/42] dm: core: Skip adding uclasses with OF_PLATDATA_INST Simon Glass
2021-03-17  1:28 ` [PATCH v4 07/42] dm: core: Set up driver model for OF_PLATDATA_INST Simon Glass
2021-03-17  1:28 ` [PATCH v4 06/42] dm: core: Adjust uclass setup with of-platdata Simon Glass
2021-03-17  1:28 ` [PATCH v4 05/42] dm: core: Allow dropping run-time binding of devices Simon Glass
2021-03-17  1:28 ` [PATCH v4 04/42] dm: core: Add macros to access the new linker lists Simon Glass
2021-03-17  1:29 ` [PATCH v4 03/42] linker_lists: Allow use in data structures Simon Glass
2021-03-17  1:29 ` [PATCH v4 02/42] sandbox: Drop debug message in os_spl_to_uboot() Simon Glass
2021-03-17  1:29 ` [PATCH v4 01/42] dtoc: Drop use of DECL() macros Simon Glass

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=20210315172537.v4.4.I203d6c2e1211d979289198ee3410009acd7353e6@changeid \
    --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.