Skip to contentSkip to conversation

dm: core: use {s,u}32 instead of int for dev_read_{s,u}32_default

PATCH In review Quentin Schulz 4 participants 8 messages May 28, 2025 – Jun 11, 2025

lore
Share & tools
lore
#0 lore raw

cover dm: core: use {s,u}32 instead of int for dev_read_{s,u}32_default

Out of all the dev_read_*_default functions, only two do not properly use the type as argument and return type: dev_read_u32_default and dev_read_s32_default. They both use int instead of u32/s32.

Considering that it's generally not guaranteed that an int is 4 bytes but also for consistency sake, let's have them use the expected type.

Note that I have not tested this, just stumbled upon that inconsistency by chance.

---
Quentin Schulz (2):
dm: core: use u32 instead of int for dev_read_u32_default
dm: core: use s32 instead of int for dev_read_s32_default

 drivers/core/read.c |  8 ++++----
 include/dm/read.h   | 14 +++++++-------
 2 files changed, 11 insertions(+), 11 deletions(-)

---
base-commit: e04d137231f2e9e14708a32448c879125b8e308f
change-id: 20250528-dev_read_x32_default-73e09c95f64e

Best regards,

Signature · 1 line
Quentin Schulz <[email protected]>
#1 lore raw

PATCH 1/2 dm: core: use u32 instead of int for dev_read_u32_default

From: Quentin Schulz <[email protected]>

dev_read_u32_default is for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.

Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.

This incidentally matches all other dev_read_*_default functions (except dev_read_s32_default which will be tackled in the next commit).

Fixes 47a0fd3bad38 ("dm: core: Implement live tree 'read' functions")
---
 drivers/core/read.c | 4 ++--
 include/dm/read.h   | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/core/read.c +2 -2
index 55c19f335ae12b8d9f83d1cb2a10e2ffc13cf935..981204bffe4c26641ea8b387f00f4efc641d6e47 100644
@@ -38,8 +38,8 @@ int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
 	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
 }
 
-int dev_read_u32_default(const struct udevice *dev, const char *propname,
-			 int def)
+u32 dev_read_u32_default(const struct udevice *dev, const char *propname,
+			 u32 def)
 {
 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
 }
include/dm/read.h +3 -3
index 894bc698bb49209c03e7cf74628a2600ea1be675..87017f076216492af5bb8da3a984ed1e074428cc 100644
@@ -90,8 +90,8 @@ int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
  * @def:	default value to return if the property has no value
  * Return: property value, or @def if not found
  */
-int dev_read_u32_default(const struct udevice *dev, const char *propname,
-			 int def);
+u32 dev_read_u32_default(const struct udevice *dev, const char *propname,
+			 u32 def);
 
 /**
  * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
@@ -896,7 +896,7 @@ static inline int dev_read_u32(const struct udevice *dev,
 }
 
 static inline int dev_read_u32_default(const struct udevice *dev,
-				       const char *propname, int def)
+				       const char *propname, u32 def)
 {
 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
 }
Signature · 1 line
2.49.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] dm: core: use u32 instead of int for dev_read_u32_default

Hi Quentin,

Quoted 14 lines · Quentin Schulz

From: Quentin Schulz <[email protected]>

dev_read_u32_default is for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.

Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.

This incidentally matches all other dev_read_*_default functions (except dev_read_s32_default which will be tackled in the next commit).

Quoted 6 lines
Fixes 47a0fd3bad38 ("dm: core: Implement live tree 'read' functions")
---
 drivers/core/read.c | 4 ++--
 include/dm/read.h   | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

Re your comment above 'int' seems to mean 32-bit on the toolchains we use. For example, in global_data we use int when we want a 32-bit value and long when we want a machine-word-sized value.

In general I try to use natural types (int, long, ulong) where possible, to avoid the compiler masking values before calling a function, which can increase code size. In this case I could make the argument either way.

Regards,
Simon

#3 lore raw

Re: [PATCH 1/2] dm: core: use u32 instead of int for dev_read_u32_default

Hi Simon,

Quoted 24 lines · Simon Glass

Hi Quentin,

Quoted 14 lines · Quentin Schulz

From: Quentin Schulz <[email protected]>

dev_read_u32_default is for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.

Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.

This incidentally matches all other dev_read_*_default functions (except dev_read_s32_default which will be tackled in the next commit).

Quoted 6 lines
Fixes 47a0fd3bad38 ("dm: core: Implement live tree 'read' functions")
---
  drivers/core/read.c | 4 ++--
  include/dm/read.h   | 6 +++---
  2 files changed, 5 insertions(+), 5 deletions(-)
Quoted 4 lines

Re your comment above 'int' seems to mean 32-bit on the toolchains we use. For example, in global_data we use int when we want a 32-bit value and long when we want a machine-word-sized value.

Fair enough, the only thing I can say - without real world experience - is that int isn't guaranteed to be 4 B by the standard? c.f. https://en.wikipedia.org/wiki/C_data_types#Main_types

Quoted 5 lines

In general I try to use natural types (int, long, ulong) where possible, to avoid the compiler masking values before calling a function, which can increase code size. In this case I could make the argument either way.

I'm happy to reword this commit log to be less confusing or alarmist. I can remove the "especially", would that be enough, do you have something else in mind?

Cheers,
Quentin

#4 lore raw

Re: [PATCH 1/2] dm: core: use u32 instead of int for dev_read_u32_default

Hi Quentin,

Quoted 28 lines · Quentin Schulz

Hi Simon,

Quoted 24 lines · Simon Glass

Hi Quentin,

Quoted 20 lines · Quentin Schulz

From: Quentin Schulz <[email protected]>

dev_read_u32_default is for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.

Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.

This incidentally matches all other dev_read_*_default functions (except dev_read_s32_default which will be tackled in the next commit).

Fixes 47a0fd3bad38 ("dm: core: Implement live tree 'read' functions")
---
  drivers/core/read.c | 4 ++--
  include/dm/read.h   | 6 +++---
  2 files changed, 5 insertions(+), 5 deletions(-)
Quoted 8 lines
Quoted 4 lines

Re your comment above 'int' seems to mean 32-bit on the toolchains we use. For example, in global_data we use int when we want a 32-bit value and long when we want a machine-word-sized value.

Fair enough, the only thing I can say - without real world experience - is that int isn't guaranteed to be 4 B by the standard? c.f.
https://en.wikipedia.org/wiki/C_data_types#Main_types

That's right. I suppose we could include the compiler's stdint.h somehow and use int32_t but in practice Linux expects things to work as I described and other embedded projects follow that.

Quoted 10 lines
Quoted 6 lines

In general I try to use natural types (int, long, ulong) where possible, to avoid the compiler masking values before calling a function, which can increase code size. In this case I could make the argument either way.

I'm happy to reword this commit log to be less confusing or alarmist. I can remove the "especially", would that be enough, do you have something else in mind?

No, the patch is fine, I was just adding my comments.

Regards,
Simon

#5 lore raw

PATCH 2/2 dm: core: use s32 instead of int for dev_read_s32_default

In reply to Quentin Schulz

From: Quentin Schulz <[email protected]>

dev_read_s32_default is for getting an s32 from a Device Tree property and allows to take a default value if that property is missing.

Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to s32/u32.

s32 and u32 being the same size, dev_read_s32* functions calling ofnode_read_u32_default shouldn't be an issue (at the type level at least) as the information will be stored appropriately in 4B regardless of the sign.

This incidentally matches all other dev_read_*_default functions.

Fixes a1b17e4f4c82 ("dm: core: Add a function to read into a unsigned int")
---
 drivers/core/read.c | 4 ++--
 include/dm/read.h   | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/core/read.c +2 -2
index 981204bffe4c26641ea8b387f00f4efc641d6e47..c0d7a969db2c9e45cd3b112e9c5712baa4acfea1 100644
@@ -62,8 +62,8 @@ int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
 	return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
 }
 
-int dev_read_s32_default(const struct udevice *dev, const char *propname,
-			 int def)
+s32 dev_read_s32_default(const struct udevice *dev, const char *propname,
+			 s32 def)
 {
 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
 }
include/dm/read.h +4 -4
index 87017f076216492af5bb8da3a984ed1e074428cc..12dcde6645c72a96512225ff8db4760a79eee43a 100644
@@ -137,8 +137,8 @@ int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
  * @def:	default value to return if the property has no value
  * Return: property value, or @def if not found
  */
-int dev_read_s32_default(const struct udevice *dev, const char *propname,
-			 int def);
+s32 dev_read_s32_default(const struct udevice *dev, const char *propname,
+			 s32 def);
 
 /**
  * dev_read_u32u() - read a 32-bit integer from a device's DT property
@@ -921,8 +921,8 @@ static inline int dev_read_s32(const struct udevice *dev,
 	return ofnode_read_s32(dev_ofnode(dev), propname, outp);
 }
 
-static inline int dev_read_s32_default(const struct udevice *dev,
-				       const char *propname, int def)
+static inline s32 dev_read_s32_default(const struct udevice *dev,
+				       const char *propname, s32 def)
 {
 	return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
 }
Signature · 1 line
2.49.0
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] dm: core: use s32 instead of int for dev_read_s32_default

Quoted 16 lines · Quentin Schulz

From: Quentin Schulz <[email protected]>

dev_read_s32_default is for getting an s32 from a Device Tree property and allows to take a default value if that property is missing.

Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to s32/u32.

s32 and u32 being the same size, dev_read_s32* functions calling ofnode_read_u32_default shouldn't be an issue (at the type level at least) as the information will be stored appropriately in 4B regardless of the sign.

Quoted 8 lines

This incidentally matches all other dev_read_*_default functions.

Fixes a1b17e4f4c82 ("dm: core: Add a function to read into a unsigned int")
---
 drivers/core/read.c | 4 ++--
 include/dm/read.h   | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
#7 lore raw
In reply to Quentin Schulz
Quoted 8 lines · Quentin Schulz

Out of all the dev_read_*_default functions, only two do not properly use the type as argument and return type: dev_read_u32_default and dev_read_s32_default. They both use int instead of u32/s32.

Considering that it's generally not guaranteed that an int is 4 bytes but also for consistency sake, let's have them use the expected type.

[...]

Applied to u-boot/next, thanks!

[1/2] dm: core: use u32 instead of int for dev_read_u32_default

      commit: b4b66e2f298e10d53749ce8d3ca086acbfc62f6a
[2/2] dm: core: use s32 instead of int for dev_read_s32_default
      commit: ad03050e26b1a61b3fe10a645a36da11652355e7
Signature · 1 line
Tom