3 Commits
Author SHA1 Message Date
John Hubbard f4231eb25c rust: sizes: add SizeConstants trait for device address space constants
The SZ_* constants are usize, matching the CPU pointer width. But
device address spaces have their own widths (32-bit MMIO windows,
64-bit GPU framebuffers, etc.), so drivers end up casting these
constants with SZ_1M as u64 or helper functions. This adds
boilerplate with no safety benefit.

Add a SizeConstants trait with associated SZ_* constants, implemented
for u32, u64, and usize. With the trait in scope, callers write
u64::SZ_1M or u32::SZ_4K to get the constant in their device's
native width. All SZ_* values fit in a u32, so every implementation
is lossless. Each impl has a const assert to catch any future
constant that would overflow.

A define_sizes! macro generates everything from a single internal
list of names. The macro takes the target types as arguments, so
adding a new target type requires changing only the call site.

Suggested-by: Danilo Krummrich <[email protected]>
Link: https://lore.kernel.org/all/[email protected]/
Link: https://lore.kernel.org/all/[email protected]/
Reviewed-by: Eliot Courtney <[email protected]>
Reviewed-by: Alexandre Courbot <[email protected]>
Acked-by: Gary Guo <[email protected]>
Signed-off-by: John Hubbard <[email protected]>
Link: https://patch.msgid.link/[email protected]
[ Applied the "kernel vertical" imports style. - Miguel ]
Signed-off-by: Miguel Ojeda <[email protected]>
2026-04-07 12:57:50 +02:00
Alexandre Courbot 06a93197e2 rust: sizes: add constants up to SZ_2G
nova-core will need to use SZ_1M, so make the remaining constants
available.

Reviewed-by: Boqun Feng <[email protected]>
Signed-off-by: Alexandre Courbot <[email protected]>
Acked-by: Miguel Ojeda <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Danilo Krummrich <[email protected]>
2025-06-23 18:12:30 +02:00
FUJITA Tomonori 4d080a029d rust: sizes: add commonly used constants
Add rust equivalent to include/linux/sizes.h, makes code more
readable. Only SZ_*K that QT2025 PHY driver uses are added.

Make generated constants accessible with a proper type.

Reviewed-by: Alice Ryhl <[email protected]>
Reviewed-by: Andrew Lunn <[email protected]>
Reviewed-by: Benno Lossin <[email protected]>
Reviewed-by: Trevor Gross <[email protected]>
Signed-off-by: FUJITA Tomonori <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
2024-08-30 10:27:34 +01:00