Support SMP on RISC-V cores with Zalrsc only
v2 cover Support SMP on RISC-V cores with Zalrsc only
Previously, we use AMO instructions to synchronize cores at start when SMP is used, which requires Zaamo extension to present. This series implements an alternative implementation of synchronization with LR/SC instructions and add Kconfig options to use them, make it possible to enable SMP on cores without Zaamo extension.
This is tested on both TH1520 Lichee Pi 4A board and QEMU. With QEMU, building and testing looks lile,
$ make qemu-riscv64_defconfig $ ./scripts/config -d CONFIG_RISCV_ISA_ZAAMO $ make $ qemu-system-riscv64 -M virt \ -cpu rv64,a=off,zaamo=off,zawrs=off,zalrsc=on \ -nographic -bios u-boot-nodtb.bin -smp 8
U-Boot 2025.07-rc4-00456-g2a74e084d720-dirty (Sep 02 2025 - 08:07:58 +0000)
CPU: riscv
Model: riscv-virtio,qemu
DRAM: 128 MiB
using memory 0x86ea1000-0x876c1000 for malloc()
Core: 34 devices, 13 uclasses, devicetree: board
Flash: 32 MiB
Loading Environment from nowhere... OK
In: serial,usbkbd
Out: serial,vidconsole
Err: serial,vidconsole
No USB controllers found
Net: No ethernet found.
Changed from v1
- Split changes to ibex-ast2700_defconfig into a separate patch
- Fix several typos
- Improve help text of CONFIG_RISCV_ISA_{ZAAMO,ZALRSC}
- Improve commit message for PATCH 3
- Link to v1: https://lore.kernel.org/all/[email protected]/
Yao Zi (3):
riscv: Add Kconfig options to distinguish Zaamo and Zalrsc
configs: ibex-ast2700: Explicitly disable Zaamo and Zalrsc extension
riscv: Add a Zalrsc-only alternative for synchronization in start.S
arch/riscv/Kconfig | 17 +++++++++++++++++ arch/riscv/Makefile | 7 ++++++- arch/riscv/cpu/start.S | 26 +++++++++++++++++++++++++- configs/ibex-ast2700_defconfig | 3 ++- 4 files changed, 50 insertions(+), 3 deletions(-)
Signature · 1 line
2.50.1
PATCH v2 1/3 riscv: Add Kconfig options to distinguish Zaamo and Zalrsc
Ratified on Apr. 2024, the original RISC-V "A" extension is now split into two separate extensions, "Zaamo" for atomic operations and "Zalrsc" for load-reserved/store-conditional instructions.
For now, we've already seen real-world designs implement the Zalrsc extension only[2]. As U-Boot mainly runs with only one HART, we could easily support these designs by not using AMO instructions in the hard-written assembly if necessary, for which this patch introduces two new Kconfig options to indicate the availability of "Zaamo" and "Zalrsc".
Note that even with this patch, "A" extension is specified in the ISA string passed to the compiler as long as one of "Zaamo" or "Zalrsc" is available, since they're only recognized with a quite recent version of GCC/Clang. The compiler usually doesn't automatically generate atomic instructions unless the source explicitly instructs it to do so, thus this should be safe.
--- arch/riscv/Kconfig | 17 +++++++++++++++++ arch/riscv/Makefile | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +17 -0
@@ -339,10 +339,27 @@ endmenu config RISCV_ISA_A bool "Standard extension for Atomic Instructions" + depends on RISCV_ISA_ZAAMO && RISCV_ISA_ZALRSC default y help Adds "A" to the ISA string passed to the compiler. +config RISCV_ISA_ZAAMO + bool "Standard extension for Atomic Memory Operations" + default y + help + Indicates the platform supports Zaamo extension for atomic memory + operations. Hand-written Assembly routines won't use AMO + instructions if set to n. + +config RISCV_ISA_ZALRSC + bool "Standard extension for LR/SC instructions" + default y + help + Indicates the platform supports Zalrsc extension for load-reserved + and store-conditional isntructions. Hand-written assembly routines + won't use LR/SC instructions if set to n. + config RISCV_ISA_ZICBOM bool "Zicbom support" depends on !SYS_DISABLE_DCACHE_OPS
arch/riscv/Makefile +6 -1
@@ -11,7 +11,12 @@ ifeq ($(CONFIG_ARCH_RV32I),y) ARCH_BASE = rv32im ABI_BASE = ilp32 endif -ifeq ($(CONFIG_RISCV_ISA_A),y) +# GCC starts to recognize "Zaamo" and "Zalrsc" from version 15, which is quite +# recent. We don't bother checking the exact compiler version, but pass "A" +# extension for -march as long as one of "Zaamo" or "Zalrsc" is available. +ifeq ($(findstring y,$(CONFIG_RISCV_ISA_A) \ + $(CONFIG_RISCV_ISA_ZAAMO) \ + $(CONFIG_RISCV_ISA_ZALRSC)),y) ARCH_A = a endif ifeq ($(CONFIG_RISCV_ISA_F),y)
Signature · 1 line
2.50.1
Re: [PATCH v2 1/3] riscv: Add Kconfig options to distinguish Zaamo and Zalrsc
Quoted 17 lines · Yao Zi
Ratified on Apr. 2024, the original RISC-V "A" extension is now split into two separate extensions, "Zaamo" for atomic operations and "Zalrsc" for load-reserved/store-conditional instructions.
For now, we've already seen real-world designs implement the Zalrsc extension only[2]. As U-Boot mainly runs with only one HART, we could easily support these designs by not using AMO instructions in the hard-written assembly if necessary, for which this patch introduces two new
Kconfigoptions to indicate the availability of "Zaamo" and "Zalrsc".Note that even with this patch, "A" extension is specified in the ISA string passed to the compiler as long as one of "Zaamo" or "Zalrsc" is available, since they're only recognized with a quite recent version of GCC/Clang. The compiler usually doesn't automatically generate atomic instructions unless the source explicitly instructs it to do so, thus this should be safe.
Quoted 7 lines
Link https://github.com/riscv/riscv-zaamo-zalrsc/commit/d94c64c63e9120d56bdeb540caf2e5dae60a8126 # [1]Signed-off-by Yao Zi <[email protected]>--- arch/riscv/Kconfig | 17 +++++++++++++++++ arch/riscv/Makefile | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-)
PATCH v2 2/3 configs: ibex-ast2700: Explicitly disable Zaamo and Zalrsc extension
In reply to Yao ZiThis board supports neither Zaamo nor Zalrsc extension, thus we want to build it without "a" specified in the ISA string passed to compiler in case of misused A-extension instructions. With RISCV_ISA_ZAAMO and RISCV_ISA_ZALRSC Kconfig options introduced, we must explicitly disable both of them to achieve this.
--- configs/ibex-ast2700_defconfig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
configs/ibex-ast2700_defconfig +2 -1
@@ -23,7 +23,8 @@ CONFIG_SYS_MEM_TOP_HIDE=0x10000000 CONFIG_BUILD_TARGET="" CONFIG_TARGET_ASPEED_AST2700_IBEX=y # CONFIG_RISCV_ISA_F is not set -# CONFIG_RISCV_ISA_A is not set +# CONFIG_RISCV_ISA_ZAAMO is not set +# CONFIG_RISCV_ISA_ZALRSC is not set # CONFIG_SPL_SMP is not set CONFIG_XIP=y CONFIG_SPL_XIP=y
Signature · 1 line
2.50.1
Re: [PATCH v2 2/3] configs: ibex-ast2700: Explicitly disable Zaamo and Zalrsc extension
Quoted 10 lines · Yao Zi
This board supports neither Zaamo nor Zalrsc extension, thus we want to build it without "a" specified in the ISA string passed to compiler in case of misused A-extension instructions. With
RISCV_ISA_ZAAMOandRISCV_ISA_ZALRSCKconfigoptions introduced, we must explicitly disable both of them to achieve this.Signed-off-by Yao Zi <[email protected]>--- configs/ibex-ast2700_defconfig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
PATCH v2 3/3 riscv: Add a Zalrsc-only alternative for synchronization in start.S
In reply to Yao ZiAdd an alternative implementation that use Zalrsc extension only for HART lottery and SMP locking to support SMP on cores without "Zaamo" extension available. The Zaamo implementation is still prioritized if both of them are available, since it takes fewer instructions.
--- arch/riscv/cpu/start.S | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
arch/riscv/cpu/start.S +25 -1
@@ -151,8 +151,15 @@ call_harts_early_init: */ la t0, hart_lottery li t1, 1 +#if CONFIG_IS_ENABLED(RISCV_ISA_ZAAMO) amoswap.w s2, t1, 0(t0) bnez s2, wait_for_gd_init +#else + lr.w s2, (t0) + bnez s2, wait_for_gd_init + sc.w s2, t1, (t0) + bnez s2, wait_for_gd_init +#endif #else /* * FIXME: gp is set before it is initialized. If an XIP U-Boot ever
@@ -177,7 +184,12 @@ call_harts_early_init: #if !CONFIG_IS_ENABLED(XIP) #ifdef CONFIG_AVAILABLE_HARTS la t0, available_harts_lock +#if CONFIG_IS_ENABLED(RISCV_ISA_ZAAMO) amoswap.w.rl zero, zero, 0(t0) +#else + fence rw, w + sw zero, 0(t0) +#endif #endif wait_for_gd_init:
@@ -190,7 +202,14 @@ wait_for_gd_init: #ifdef CONFIG_AVAILABLE_HARTS la t0, available_harts_lock li t1, 1 -1: amoswap.w.aq t1, t1, 0(t0) +1: +#if CONFIG_IS_ENABLED(RISCV_ISA_ZAAMO) + amoswap.w.aq t1, t1, 0(t0) +#else + lr.w.aq t1, 0(t0) + bnez t1, 1b + sc.w.rl t1, t1, 0(t0) +#endif bnez t1, 1b /* register available harts in the available_harts mask */
@@ -200,7 +219,12 @@ wait_for_gd_init: or t2, t2, t1 SREG t2, GD_AVAILABLE_HARTS(gp) +#if CONFIG_IS_ENABLED(RISCV_ISA_ZAAMO) amoswap.w.rl zero, zero, 0(t0) +#else + fence rw, w + sw zero, 0(t0) +#endif #endif /*
Signature · 1 line
2.50.1
Re: [PATCH v2 3/3] riscv: Add a Zalrsc-only alternative for synchronization in start.S
Quoted 9 lines · Yao Zi
Add an alternative implementation that use Zalrsc extension only for HART lottery and SMP locking to support SMP on cores without "Zaamo" extension available. The Zaamo implementation is still prioritized if both of them are available, since it takes fewer instructions.
Signed-off-by Yao Zi <[email protected]>--- arch/riscv/cpu/start.S | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)