* [PATCH v2 1/3] riscv: Add Kconfig options to distinguish Zaamo and Zalrsc
2025-09-02 8:19 [PATCH v2 0/3] Support SMP on RISC-V cores with Zalrsc only Yao Zi
@ 2025-09-02 8:19 ` Yao Zi
2025-09-18 12:07 ` Leo Liang
2025-09-02 8:19 ` [PATCH v2 2/3] configs: ibex-ast2700: Explicitly disable Zaamo and Zalrsc extension Yao Zi
2025-09-02 8:19 ` [PATCH v2 3/3] riscv: Add a Zalrsc-only alternative for synchronization in start.S Yao Zi
2 siblings, 1 reply; 7+ messages in thread
From: Yao Zi @ 2025-09-02 8:19 UTC (permalink / raw)
To: Rick Chen, Leo, Tom Rini, Chia-Wei, Wang, Simon Glass; +Cc: u-boot, 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 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.
Link: https://github.com/riscv/riscv-zaamo-zalrsc/commit/d94c64c63e9120d56bdeb540caf2e5dae60a8126 # [1]
Link: https://lore.kernel.org/u-boot/20250729162035.209849-9-uros.stajic@htecgroup.com/ # [2]
Signed-off-by: Yao Zi <ziyao@disroot.org>
---
arch/riscv/Kconfig | 17 +++++++++++++++++
arch/riscv/Makefile | 7 ++++++-
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 8c6feae5735..8b5a4074724 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -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
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 6f80f4a7108..fdda6da1df3 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -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)
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/3] configs: ibex-ast2700: Explicitly disable Zaamo and Zalrsc extension
2025-09-02 8:19 [PATCH v2 0/3] Support SMP on RISC-V cores with Zalrsc only Yao Zi
2025-09-02 8:19 ` [PATCH v2 1/3] riscv: Add Kconfig options to distinguish Zaamo and Zalrsc Yao Zi
@ 2025-09-02 8:19 ` Yao Zi
2025-09-18 12:07 ` Leo Liang
2025-09-02 8:19 ` [PATCH v2 3/3] riscv: Add a Zalrsc-only alternative for synchronization in start.S Yao Zi
2 siblings, 1 reply; 7+ messages in thread
From: Yao Zi @ 2025-09-02 8:19 UTC (permalink / raw)
To: Rick Chen, Leo, Tom Rini, Chia-Wei, Wang, Simon Glass; +Cc: u-boot, 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_ZAAMO and
RISCV_ISA_ZALRSC Kconfig options introduced, we must explicitly disable
both of them to achieve this.
Signed-off-by: Yao Zi <ziyao@disroot.org>
---
configs/ibex-ast2700_defconfig | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/configs/ibex-ast2700_defconfig b/configs/ibex-ast2700_defconfig
index 6cb2a21ed2d..ec30fa1ae10 100644
--- a/configs/ibex-ast2700_defconfig
+++ b/configs/ibex-ast2700_defconfig
@@ -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
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/3] riscv: Add a Zalrsc-only alternative for synchronization in start.S
2025-09-02 8:19 [PATCH v2 0/3] Support SMP on RISC-V cores with Zalrsc only Yao Zi
2025-09-02 8:19 ` [PATCH v2 1/3] riscv: Add Kconfig options to distinguish Zaamo and Zalrsc Yao Zi
2025-09-02 8:19 ` [PATCH v2 2/3] configs: ibex-ast2700: Explicitly disable Zaamo and Zalrsc extension Yao Zi
@ 2025-09-02 8:19 ` Yao Zi
2025-09-18 12:08 ` Leo Liang
2 siblings, 1 reply; 7+ messages in thread
From: Yao Zi @ 2025-09-02 8:19 UTC (permalink / raw)
To: Rick Chen, Leo, Tom Rini, Chia-Wei, Wang, Simon Glass; +Cc: u-boot, 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 <ziyao@disroot.org>
---
arch/riscv/cpu/start.S | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/cpu/start.S b/arch/riscv/cpu/start.S
index 7bafdfd390a..6324ff585d4 100644
--- a/arch/riscv/cpu/start.S
+++ b/arch/riscv/cpu/start.S
@@ -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
/*
--
2.50.1
^ permalink raw reply related [flat|nested] 7+ messages in thread