[PATCH v2 1/4] rust: io: Add big-endian read and write functions

Link Mauve
2026-02-04T04:05:25Z

Another option would be to call u32::swap_bytes() on the data being
read/written, but these helpers make the Rust code as ergonomic as the C
code.

Signed-off-by: Link Mauve <linkmauve@linkmauve.fr>
---
rust/helpers/io.c | 34 ++++++++++++++++++++++++++++++++++
rust/kernel/io.rs | 18 ++++++++++++++++++
2 files changed, 52 insertions(+)

diff --git a/rust/helpers/io.c b/rust/helpers/io.c
index c475913c69e6..514ad0377327 100644
--- a/rust/helpers/io.c
+++ b/rust/helpers/io.c
@@ -40,6 +40,23 @@ u64 rust_helper_readq(const void __iomem *addr)
}
#endif

+u16 rust_helper_ioread16be(const void __iomem *addr)
+{
+ return ioread16be(addr);
+}
+
+u32 rust_helper_ioread32be(const void __iomem *addr)
+{
+ return ioread32be(addr);
+}
+
+#ifdef CONFIG_64BIT
+u64 rust_helper_ioread64be(const void __iomem *addr)
+{
+ return ioread64be(addr);
+}
+#endif
+
void rust_helper_writeb(u8 value, void __iomem *addr)
{
writeb(value, addr);
@@ -62,6 +79,23 @@ void rust_helper_writeq(u64 value, void __iomem *addr)
}
#endif

+void rust_helper_iowrite16be(u16 value, void __iomem *addr)
+{
+ iowrite16be(value, addr);
+}
+
+void rust_helper_iowrite32be(u32 value, void __iomem *addr)
+{
+ iowrite32be(value, addr);
+}
+
+#ifdef CONFIG_64BIT
+void rust_helper_iowrite64be(u64 value, void __iomem *addr)
+{
+ iowrite64be(value, addr);
+}
+#endif
+
u8 rust_helper_readb_relaxed(const void __iomem *addr)
{
return readb_relaxed(addr);
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 98e8b84e68d1..e6912e7ff036 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -256,6 +256,15 @@ fn io_addr_assert(&self, offset: usize) -> usize {
readq -> u64
);

+ define_read!(read16be, try_read16be, ioread16be -> u16);
+ define_read!(read32be, try_read32be, ioread32be -> u32);
+ define_read!(
+ #[cfg(CONFIG_64BIT)]
+ read64be,
+ try_read64be,
+ ioread64be -> u64
+ );
+
define_read!(read8_relaxed, try_read8_relaxed, readb_relaxed -> u8);
define_read!(read16_relaxed, try_read16_relaxed, readw_relaxed -> u16);
define_read!(read32_relaxed, try_read32_relaxed, readl_relaxed -> u32);
@@ -276,6 +285,15 @@ fn io_addr_assert(&self, offset: usize) -> usize {
writeq <- u64
);

+ define_write!(write16be, try_write16be, iowrite16be <- u16);
+ define_write!(write32be, try_write32be, iowrite32be <- u32);
+ define_write!(
+ #[cfg(CONFIG_64BIT)]
+ write64be,
+ try_write64be,
+ iowrite64be <- u64
+ );
+
define_write!(write8_relaxed, try_write8_relaxed, writeb_relaxed <- u8);
define_write!(write16_relaxed, try_write16_relaxed, writew_relaxed <- u16);
define_write!(write32_relaxed, try_write32_relaxed, writel_relaxed <- u32);
--
2.52.0