public inbox for opensbi@lists.infradead.org
 help / color / mirror / Atom feed
From: Marti Alonso <martialonso11@gmail.com>
To: opensbi@lists.infradead.org
Cc: Marti Alonso <martialonso11@gmail.com>
Subject: [PATCH] lib: Use proper add opcode on RV32 with Zalrsc
Date: Sun,  1 Mar 2026 20:54:21 +0000	[thread overview]
Message-ID: <20260301205421.2074835-1-martialonso11@gmail.com> (raw)

The addw opcode is only defined in RV64, which produces 32-bit results.
On RV32, the default add opcode already produces 32-bit results.

Fixes: 995f226f3f33 ("lib: Emit lr and sc instructions based on -march flags")
Signed-off-by: Marti Alonso <martialonso11@gmail.com>
---
 firmware/payloads/test_head.S | 3 ++-
 lib/sbi/riscv_atomic.c        | 2 +-
 lib/sbi/riscv_locks.c         | 4 ++++
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/firmware/payloads/test_head.S b/firmware/payloads/test_head.S
index 9a87e56fcf5d..e4fcaaca92c9 100644
--- a/firmware/payloads/test_head.S
+++ b/firmware/payloads/test_head.S
@@ -22,6 +22,7 @@
 
 #define REG_L		__REG_SEL(ld, lw)
 #define REG_S		__REG_SEL(sd, sw)
+#define REG_ADDW	__REG_SEL(addw, add)
 
 	.section .entry, "ax", %progbits
 	.align 3
@@ -35,7 +36,7 @@ _start:
 #elif defined(__riscv_zalrsc)
 _sc_fail:
 	lr.w	t0, (a3)
-	addw	t1, t0, a2
+	REG_ADDW t1, t0, a2
 	sc.w	t1, t1, (a3)
 	bnez	t1, _sc_fail
 	move	a3, t0
diff --git a/lib/sbi/riscv_atomic.c b/lib/sbi/riscv_atomic.c
index fcf07f273149..d9abe343cb26 100644
--- a/lib/sbi/riscv_atomic.c
+++ b/lib/sbi/riscv_atomic.c
@@ -48,7 +48,7 @@ long atomic_add_return(atomic_t *atom, long value)
 	long ret, temp;
 #if __SIZEOF_LONG__ == 4
 	__asm__ __volatile__("1:lr.w.aqrl	%1,%0\n"
-			     "  addw	%2,%1,%3\n"
+			     "  add	%2,%1,%3\n"
 			     "  sc.w.aqrl	%2,%2,%0\n"
 			     "  bnez	%2,1b"
 			     : "+A"(atom->counter), "=&r"(ret), "=&r"(temp)
diff --git a/lib/sbi/riscv_locks.c b/lib/sbi/riscv_locks.c
index c29a965907bd..e253b1b723ab 100644
--- a/lib/sbi/riscv_locks.c
+++ b/lib/sbi/riscv_locks.c
@@ -57,7 +57,11 @@ void spin_lock(spinlock_t *lock)
 		"	amoadd.w.aqrl	%0, %4, %3\n"
 #elif defined(__riscv_zalrsc)
 		"3:	lr.w.aqrl	%0, %3\n"
+#if __riscv_xlen == 64
 		"	addw	%1, %0, %4\n"
+#elif __riscv_xlen == 32
+		"	add	%1, %0, %4\n"
+#endif
 		"	sc.w.aqrl	%1, %1, %3\n"
 		"	bnez	%1, 3b\n"
 #else
-- 
2.43.0


-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

             reply	other threads:[~2026-03-01 20:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-01 20:54 Marti Alonso [this message]
2026-03-10  5:20 ` [PATCH] lib: Use proper add opcode on RV32 with Zalrsc Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260301205421.2074835-1-martialonso11@gmail.com \
    --to=martialonso11@gmail.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox