[PATCH v2 3/5] sysinfo: Require that sysinfo_detect be called before other methods

Sean Anderson sean.anderson at seco.com
Fri Mar 5 21:12:23 CET 2021


This has the uclass enforce calling detect() before other methods.  This
allows drivers to cache information in detect() and perform (cheaper)
retrieval in the other accessors. This also modifies the only instance
where this sequencing was not followed.

Signed-off-by: Sean Anderson <sean.anderson at seco.com>
---

Changes in v2:
- Enforce sysinfo detect ordering in uclass. Users must still call
  sysinfo_detect beforehand.
- Modify sysinfo test to check for detect() ordering.

 common/spl/spl_fit.c             |  4 ++++
 drivers/sysinfo/sysinfo-uclass.c | 25 ++++++++++++++++++++++++-
 include/sysinfo.h                | 26 +++++++++++++++++---------
 test/dm/sysinfo.c                | 23 ++++++++++++++---------
 4 files changed, 59 insertions(+), 19 deletions(-)

diff --git a/common/spl/spl_fit.c b/common/spl/spl_fit.c
index a6ad094e91..4d17582af5 100644
--- a/common/spl/spl_fit.c
+++ b/common/spl/spl_fit.c
@@ -116,6 +116,10 @@ static int spl_fit_get_image_name(const void *fit, int images,
 		 * no string in the property for this index. Check if the
 		 * sysinfo-level code can supply one.
 		 */
+		rc = sysinfo_detect(sysinfo);
+		if (rc)
+			return rc;
+
 		rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
 					      &str);
 		if (rc && rc != -ENOENT)
diff --git a/drivers/sysinfo/sysinfo-uclass.c b/drivers/sysinfo/sysinfo-uclass.c
index 6df58fe160..f034ee0870 100644
--- a/drivers/sysinfo/sysinfo-uclass.c
+++ b/drivers/sysinfo/sysinfo-uclass.c
@@ -15,19 +15,29 @@ int sysinfo_get(struct udevice **devp)
 
 int sysinfo_detect(struct udevice *dev)
 {
+	int ret;
+	bool *detected = dev_get_uclass_priv(dev);
 	struct sysinfo_ops *ops = sysinfo_get_ops(dev);
 
 	if (!ops->detect)
 		return -ENOSYS;
 
-	return ops->detect(dev);
+	ret = ops->detect(dev);
+	if (!ret)
+		*detected = true;
+
+	return ret;
 }
 
 int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
 			     const char **strp)
 {
+	bool *detected = dev_get_uclass_priv(dev);
 	struct sysinfo_ops *ops = sysinfo_get_ops(dev);
 
+	if (!*detected)
+		return -EPERM;
+
 	if (!ops->get_fit_loadable)
 		return -ENOSYS;
 
@@ -36,8 +46,12 @@ int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
 
 int sysinfo_get_bool(struct udevice *dev, int id, bool *val)
 {
+	bool *detected = dev_get_uclass_priv(dev);
 	struct sysinfo_ops *ops = sysinfo_get_ops(dev);
 
+	if (!*detected)
+		return -EPERM;
+
 	if (!ops->get_bool)
 		return -ENOSYS;
 
@@ -46,8 +60,12 @@ int sysinfo_get_bool(struct udevice *dev, int id, bool *val)
 
 int sysinfo_get_int(struct udevice *dev, int id, int *val)
 {
+	bool *detected = dev_get_uclass_priv(dev);
 	struct sysinfo_ops *ops = sysinfo_get_ops(dev);
 
+	if (!*detected)
+		return -EPERM;
+
 	if (!ops->get_int)
 		return -ENOSYS;
 
@@ -56,8 +74,12 @@ int sysinfo_get_int(struct udevice *dev, int id, int *val)
 
 int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val)
 {
+	bool *detected = dev_get_uclass_priv(dev);
 	struct sysinfo_ops *ops = sysinfo_get_ops(dev);
 
+	if (!*detected)
+		return -EPERM;
+
 	if (!ops->get_str)
 		return -ENOSYS;
 
@@ -68,4 +90,5 @@ UCLASS_DRIVER(sysinfo) = {
 	.id		= UCLASS_SYSINFO,
 	.name		= "sysinfo",
 	.post_bind	= dm_scan_fdt_dev,
+	.per_device_auto	= sizeof(bool),
 };
diff --git a/include/sysinfo.h b/include/sysinfo.h
index 9386bdf49a..e2aef4abe9 100644
--- a/include/sysinfo.h
+++ b/include/sysinfo.h
@@ -54,7 +54,8 @@ struct sysinfo_ops {
 	 * This operation might take a long time (e.g. read from EEPROM,
 	 * check the presence of a device on a bus etc.), hence this is not
 	 * done in the probe() method, but later during operation in this
-	 * dedicated method.
+	 * dedicated method. This method will be called before any other
+	 * methods.
 	 *
 	 * Return: 0 if OK, -ve on error.
 	 */
@@ -98,7 +99,7 @@ struct sysinfo_ops {
 	 * get_fit_loadable - Get the name of an image to load from FIT
 	 * This function can be used to provide the image names based on runtime
 	 * detection. A classic use-case would when DTBOs are used to describe
-	 * additionnal daughter cards.
+	 * additional daughter cards.
 	 *
 	 * @dev:	The sysinfo instance to gather the data.
 	 * @index:	Index of the image. Starts at 0 and gets incremented
@@ -120,6 +121,9 @@ struct sysinfo_ops {
  *
  * @dev:	The device containing the information
  *
+ * This function must be called before any other accessor function for this
+ * device.
+ *
  * Return: 0 if OK, -ve on error.
  */
 int sysinfo_detect(struct udevice *dev);
@@ -131,7 +135,8 @@ int sysinfo_detect(struct udevice *dev);
  * @id:		A unique identifier for the bool value to be read.
  * @val:	Pointer to a buffer that receives the value read.
  *
- * Return: 0 if OK, -ve on error.
+ * Return: 0 if OK, -EPERM if called before sysinfo_detect(), else -ve on
+ * error.
  */
 int sysinfo_get_bool(struct udevice *dev, int id, bool *val);
 
@@ -142,7 +147,8 @@ int sysinfo_get_bool(struct udevice *dev, int id, bool *val);
  * @id:		A unique identifier for the int value to be read.
  * @val:	Pointer to a buffer that receives the value read.
  *
- * Return: 0 if OK, -ve on error.
+ * Return: 0 if OK, -EPERM if called before sysinfo_detect(), else -ve on
+ * error.
  */
 int sysinfo_get_int(struct udevice *dev, int id, int *val);
 
@@ -154,7 +160,8 @@ int sysinfo_get_int(struct udevice *dev, int id, int *val);
  * @size:	The size of the buffer to receive the string data.
  * @val:	Pointer to a buffer that receives the value read.
  *
- * Return: 0 if OK, -ve on error.
+ * Return: 0 if OK, -EPERM if called before sysinfo_detect(), else -ve on
+ * error.
  */
 int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val);
 
@@ -166,7 +173,8 @@ int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val);
  * function that returns the unique device. This is especially useful for use
  * in sysinfo files.
  *
- * Return: 0 if OK, -ve on error.
+ * Return: 0 if OK, -EPERM if called before sysinfo_detect(), else -ve on
+ * error.
  */
 int sysinfo_get(struct udevice **devp);
 
@@ -174,7 +182,7 @@ int sysinfo_get(struct udevice **devp);
  * sysinfo_get_fit_loadable - Get the name of an image to load from FIT
  * This function can be used to provide the image names based on runtime
  * detection. A classic use-case would when DTBOs are used to describe
- * additionnal daughter cards.
+ * additional daughter cards.
  *
  * @dev:	The sysinfo instance to gather the data.
  * @index:	Index of the image. Starts at 0 and gets incremented
@@ -183,8 +191,8 @@ int sysinfo_get(struct udevice **devp);
  * @strp:	A pointer to string. Untouched if the function fails
  *
  *
- * Return: 0 if OK, -ENOENT if no loadable is available else -ve on
- * error.
+ * Return: 0 if OK, -EPERM if called before sysinfo_detect(), -ENOENT if no
+ * loadable is available else -ve on error.
  */
 int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
 			     const char **strp);
diff --git a/test/dm/sysinfo.c b/test/dm/sysinfo.c
index 4aaa9e85bc..fd13f6aac5 100644
--- a/test/dm/sysinfo.c
+++ b/test/dm/sysinfo.c
@@ -24,33 +24,38 @@ static int dm_test_sysinfo(struct unit_test_state *uts)
 	ut_assertok(sysinfo_get(&sysinfo));
 	ut_assert(sysinfo);
 
-	sysinfo_get_bool(sysinfo, BOOL_CALLED_DETECT, &called_detect);
+	ut_asserteq(-EPERM, sysinfo_get_bool(sysinfo, BOOL_CALLED_DETECT,
+					     &called_detect));
 	ut_assert(!called_detect);
 
 	sysinfo_detect(sysinfo);
 
-	sysinfo_get_bool(sysinfo, BOOL_CALLED_DETECT, &called_detect);
+	ut_assertok(sysinfo_get_bool(sysinfo, BOOL_CALLED_DETECT,
+				     &called_detect));
 	ut_assert(called_detect);
 
-	sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str), str);
+	ut_assertok(sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
+				    str));
 	ut_assertok(strcmp(str, "R'lyeh"));
 
-	sysinfo_get_int(sysinfo, INT_TEST1, &i);
+	ut_assertok(sysinfo_get_int(sysinfo, INT_TEST1, &i));
 	ut_asserteq(0, i);
 
-	sysinfo_get_int(sysinfo, INT_TEST2, &i);
+	ut_assertok(sysinfo_get_int(sysinfo, INT_TEST2, &i));
 	ut_asserteq(100, i);
 
-	sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str), str);
+	ut_assertok(sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
+				    str));
 	ut_assertok(strcmp(str, "Carcosa"));
 
-	sysinfo_get_int(sysinfo, INT_TEST1, &i);
+	ut_assertok(sysinfo_get_int(sysinfo, INT_TEST1, &i));
 	ut_asserteq(1, i);
 
-	sysinfo_get_int(sysinfo, INT_TEST2, &i);
+	ut_assertok(sysinfo_get_int(sysinfo, INT_TEST2, &i));
 	ut_asserteq(99, i);
 
-	sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str), str);
+	ut_assertok(sysinfo_get_str(sysinfo, STR_VACATIONSPOT, sizeof(str),
+				    str));
 	ut_assertok(strcmp(str, "Yuggoth"));
 
 	return 0;
-- 
2.25.1



More information about the U-Boot mailing list