eal/riscv: add RISC-V specific I/O device memory operations
PATCH 1/1 eal/riscv: add RISC-V specific I/O device memory operations
The current rte_io.h for RISC-V only includes generic/rte_io.h, which uses volatile pointer casts for MMIO read/write. While this prevents compiler optimizations, it does not prevent CPU-level reordering on RISC-V, which is a weak-ordered architecture.
This patch adds a RISC-V specific implementation using explicit load/store instructions:
- lbu/lhu/lwu/ld for reads (zero-extending to avoid sign-bit pollution)
- sb/sh/sw/sd for writes
- lwu is used for 32-bit reads on RV64 for correct zero-extension; lw is used on RV32 where it is naturally full-width
- 64-bit operations are guarded with RTE_ARCH_64 for RV32 compatibility
The "memory" clobber is retained in relaxed variants to prevent the compiler from reordering these accesses with respect to other memory operations, while still omitting the hardware fence instructions. Unlike ARM64 which is a strong-ordered architecture, this additional constraint is necessary on RISC-V.
--- lib/eal/riscv/include/rte_io.h | 185 ++++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 3 deletions(-)
lib/eal/riscv/include/rte_io.h +182 -3
@@ -3,11 +3,190 @@* Copyright(c) 2022 StarFive * Copyright(c) 2022 SiFive * Copyright(c) 2022 Semihalf +* Copyright(c) 2026 Dangshiwei */ -#ifndef RTE_IO_RISCV_H -#define RTE_IO_RISCV_H +#ifndef _RTE_IO_RISCV_H_ +#define _RTE_IO_RISCV_H_ + +#include <stdint.h> + +#define RTE_OVERRIDE_IO_H #include "generic/rte_io.h" +#include <rte_compat.h> +#include "rte_atomic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* +* Unlike ARM64 which is a strong-ordered architecture, RISC-V is a +* weak-ordered architecture. The "memory" clobber is added to the relaxed +* variants to prevent the compiler from reordering these accesses with +* respect to other memory operations, while still omitting the hardware +* fence instructions. +*/ + +/* relaxed read */ + +static __rte_always_inline uint8_t +rte_read8_relaxed (const volatile void *addr) +{ +uint8_t val; + asmvolatile ("lbu %0, 0(%1)" :"=r" (val) :"r" (addr) :"memory" ); +return val; +} + +static __rte_always_inline uint16_t +rte_read16_relaxed (const volatile void *addr) +{ +uint16_t val; + asmvolatile ("lhu %0, 0(%1)" :"=r" (val) :"r" (addr) :"memory" ); +return val; +} + +static __rte_always_inline uint32_t +rte_read32_relaxed (const volatile void *addr) +{ +uint32_t val; +#ifdef RTE_ARCH_64 +/* lwu is RV64-only: zero-extends to avoid sign-bit pollution */ + asmvolatile ("lwu %0, 0(%1)" :"=r" (val) :"r" (addr) :"memory" ); +#else +/* on RV32, lw is full-width, no extension needed */ + asmvolatile ("lw %0, 0(%1)" :"=r" (val) :"r" (addr) :"memory" ); +#endif +return val; +} + +#ifdef RTE_ARCH_64 +static __rte_always_inline uint64_t +rte_read64_relaxed (const volatile void *addr) +{ +uint64_t val; + asmvolatile ("ld %0, 0(%1)" :"=r" (val) :"r" (addr) :"memory" ); +return val; +} +#endif + +/* relaxed write */ + +static __rte_always_inline void +rte_write8_relaxed (uint8_t val,volatile void *addr) +{ + asmvolatile ("sb %1, 0(%0)" : :"r" (addr),"r" (val) :"memory" ); +} + +static __rte_always_inline void +rte_write16_relaxed (uint16_t val,volatile void *addr) +{ + asmvolatile ("sh %1, 0(%0)" : :"r" (addr),"r" (val) :"memory" ); +} + +static __rte_always_inline void +rte_write32_relaxed (uint32_t val,volatile void *addr) +{ + asmvolatile ("sw %1, 0(%0)" : :"r" (addr),"r" (val) :"memory" ); +} + +#ifdef RTE_ARCH_64 +static __rte_always_inline void +rte_write64_relaxed (uint64_t val,volatile void *addr) +{ + asmvolatile ("sd %1, 0(%0)" : :"r" (addr),"r" (val) :"memory" ); +} +#endif + +/* read with I/O memory barrier */ + +static __rte_always_inline uint8_t +rte_read8 (const volatile void *addr) +{ +uint8_t val =rte_read8_relaxed (addr); +rte_io_rmb (); +return val; +} + +static __rte_always_inline uint16_t +rte_read16 (const volatile void *addr) +{ +uint16_t val =rte_read16_relaxed (addr); +rte_io_rmb (); +return val; +} + +static __rte_always_inline uint32_t +rte_read32 (const volatile void *addr) +{ +uint32_t val =rte_read32_relaxed (addr); +rte_io_rmb (); +return val; +} + +#ifdef RTE_ARCH_64 +static __rte_always_inline uint64_t +rte_read64 (const volatile void *addr) +{ +uint64_t val =rte_read64_relaxed (addr); +rte_io_rmb (); +return val; +} +#endif + +/* write with I/O memory barrier */ + +static __rte_always_inline void +rte_write8 (uint8_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write8_relaxed (val, addr); +} + +static __rte_always_inline void +rte_write16 (uint16_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write16_relaxed (val, addr); +} + +static __rte_always_inline void +rte_write32 (uint32_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write32_relaxed (val, addr); +} + +#ifdef RTE_ARCH_64 +static __rte_always_inline void +rte_write64 (uint64_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write64_relaxed (val, addr); +} +#endif + +/* +* RISC-V currently has no write-combining store instructions. +* Fall back to normal write. +*/ +__rte_experimental +static __rte_always_inline void +rte_write32_wc (uint32_t val,volatile void *addr) +{ +rte_write32 (val, addr); +} + +__rte_experimental +static __rte_always_inline void +rte_write32_wc_relaxed (uint32_t val,volatile void *addr) +{ +rte_write32_relaxed (val, addr); +} + +#ifdef __cplusplus +} +#endif -#endif /* RTE_IO_RISCV_H */ +#endif /* _RTE_IO_RISCV_H_ */
Signature · 1 line
2.43.0
Quoted 7 lines
#ifdef
RTE_ARCH_64
/* lwu is RV64-only: zero-extends to avoid sign-bit pollution */ asmvolatile("lwu %0, 0(%1)" : "=r"(val) : "r"(addr) : "memory"); #else
/* on RV32, lw is full-width, no extension needed */ asmvolatile("lw %0, 0(%1)" : "=r"(val) : "r"(addr) : "memory"); #endif
DPDK RISC-V does not support 32-bit. config/riscv/meson.build:
if not dpdk_conf.get('RTE_ARCH_64')
error('Only 64-bit compiles are supported for this platform type')
We can remove all the #ifdef RTE_ARCH_64.
Quoted 1 line
ARM64 which is a strong-ordered architecture
I'm not familiar with ARM. it seems to be weakly-ordered?
"Armv8-A implements a weakly-ordered memory architecture." (https://developer.arm.com/documentation/102336/0100/Memory-ordering)
Quoted 3 lines
/* relaxed read */
/* relaxed write */
..
Other architectures don't use this kind of annotation, so maybe RISC-V can drop it too.
And DPDK requires a real name in the Signed-off-by line.
PATCH v2 2/3 eal/riscv: add RISC-V specific I/O device memory operations
In reply to dangshiweiThe current rte_io.h for RISC-V only includes generic/rte_io.h, which uses volatile pointer casts for MMIO read/write. While this prevents compiler optimizations, it does not prevent CPU-level reordering on RISC-V, which is a weak-ordered architecture.
This patch adds a RISC-V specific implementation using explicit load/store instructions:
- lbu/lhu/lwu/ld for reads (zero-extending to avoid sign-bit pollution)
- sb/sh/sw/sd for writes
--- lib/eal/riscv/include/rte_io.h | 152 ++++++++++++++++++++++++++++++++- 1 file changed, 149 insertions(+), 3 deletions(-)
lib/eal/riscv/include/rte_io.h +149 -3
@@ -5,9 +5,155 @@* Copyright(c) 2022 Semihalf */ -#ifndef RTE_IO_RISCV_H -#define RTE_IO_RISCV_H +#ifndef _RTE_IO_RISCV_H_ +#define _RTE_IO_RISCV_H_ + +#include <stdint.h> + +#define RTE_OVERRIDE_IO_H #include "generic/rte_io.h" +#include <rte_compat.h> +#include "rte_atomic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static __rte_always_inline uint8_t +rte_read8_relaxed (const volatile void *addr) +{ +uint8_t val; + asmvolatile ("lbu %0, 0(%1)" :"=r" (val) :"r" (addr)); +return val; +} + +static __rte_always_inline uint16_t +rte_read16_relaxed (const volatile void *addr) +{ +uint16_t val; + asmvolatile ("lhu %0, 0(%1)" :"=r" (val) :"r" (addr)); +return val; +} + +static __rte_always_inline uint32_t +rte_read32_relaxed (const volatile void *addr) +{ +uint32_t val; + asmvolatile ("lwu %0, 0(%1)" :"=r" (val) :"r" (addr)); +return val; +} + +static __rte_always_inline uint64_t +rte_read64_relaxed (const volatile void *addr) +{ +uint64_t val; + asmvolatile ("ld %0, 0(%1)" :"=r" (val) :"r" (addr)); +return val; +} + +static __rte_always_inline void +rte_write8_relaxed (uint8_t val,volatile void *addr) +{ + asmvolatile ("sb %1, 0(%0)" : :"r" (addr),"r" (val)); +} + +static __rte_always_inline void +rte_write16_relaxed (uint16_t val,volatile void *addr) +{ + asmvolatile ("sh %1, 0(%0)" : :"r" (addr),"r" (val)); +} + +static __rte_always_inline void +rte_write32_relaxed (uint32_t val,volatile void *addr) +{ + asmvolatile ("sw %1, 0(%0)" : :"r" (addr),"r" (val)); +} + +static __rte_always_inline void +rte_write64_relaxed (uint64_t val,volatile void *addr) +{ + asmvolatile ("sd %1, 0(%0)" : :"r" (addr),"r" (val)); +} + + +static __rte_always_inline uint8_t +rte_read8 (const volatile void *addr) +{ +uint8_t val =rte_read8_relaxed (addr); +rte_io_rmb (); +return val; +} + +static __rte_always_inline uint16_t +rte_read16 (const volatile void *addr) +{ +uint16_t val =rte_read16_relaxed (addr); +rte_io_rmb (); +return val; +} + +static __rte_always_inline uint32_t +rte_read32 (const volatile void *addr) +{ +uint32_t val =rte_read32_relaxed (addr); +rte_io_rmb (); +return val; +} + +static __rte_always_inline uint64_t +rte_read64 (const volatile void *addr) +{ +uint64_t val =rte_read64_relaxed (addr); +rte_io_rmb (); +return val; +} + + +static __rte_always_inline void +rte_write8 (uint8_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write8_relaxed (val, addr); +} + +static __rte_always_inline void +rte_write16 (uint16_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write16_relaxed (val, addr); +} + +static __rte_always_inline void +rte_write32 (uint32_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write32_relaxed (val, addr); +} + +static __rte_always_inline void +rte_write64 (uint64_t val,volatile void *addr) +{ +rte_io_wmb (); +rte_write64_relaxed (val, addr); +} + +__rte_experimental +static __rte_always_inline void +rte_write32_wc (uint32_t val,volatile void *addr) +{ +rte_write32 (val, addr); +} + +__rte_experimental +static __rte_always_inline void +rte_write32_wc_relaxed (uint32_t val,volatile void *addr) +{ +rte_write32_relaxed (val, addr); +} + +#ifdef __cplusplus +} +#endif -#endif /* RTE_IO_RISCV_H */ +#endif /* _RTE_IO_RISCV_H_ */
Signature · 1 line
2.43.0
Hi Shiwei,
Ordering is guaranteed by the fence either way, asm or C makes no difference.
For the generated code, the compiler cannot see through inline asm, so the 8/16/32-bit reads end up with an extra extension instruction (tested with gcc 16 and clang 22).
Quoted 1 line
lbu/lhu/lwu/ld for reads (zero-extending to avoid sign-bit pollution)
The compiler picks the right one from the context, there is no pollution.
Overall I don't see a real benefit here and would rather keep the generic implementation. What do you think?