Skip to contentSkip to conversation

riscv: start on work to allow big-endian build

PATCH Superseded Ben Dooks 2 participants 6 messages Jun 10, 2025 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

Given we've been doing work on qemu and linux-kernel we started looking at u-boot too. I've done a quick test with our qemu and got a few patches going.

It looks like there are only a few updates to get the build working. The only question I have at the moment is with the read/write io macros which seem to be incosistently endian interested.

project at: https://gitlab.com/CodethinkLabs/riscv_bigendian

author
#1 lore raw

PATCH 1/2 riscv: add build support for big-endian

Add support to build code big-endian if the board supports it. Updates the makefile to pass the correct compiler and elf flags.

---
 arch/riscv/config.mk | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
arch/riscv/config.mk +14 -4
index 9f16dda92a0..2eeeb433748 100644
@@ -10,19 +10,29 @@
 # Rick Chen, Andes Technology Corporation <[email protected]>
 #
 
-32bit-emul		:= elf32lriscv
-64bit-emul		:= elf64lriscv
+ifdef CONFIG_SYS_BIT_ENDIAN
+small-endian		:= b
+large-endian		:= big
+else
+small-endian		:= b
+large-endian		:= big
+PLATFORM_CPPFLAGS       += -mbig-endian
+KBUILD_LDFLAGS          += -mbig-endian
+endif
+
+32bit-emul		:= elf32$(small-endian)riscv
+64bit-emul		:= elf64$(small-endian)riscv
 
 ifdef CONFIG_32BIT
 KBUILD_LDFLAGS		+= -m $(32bit-emul)
 EFI_LDS			:= elf_riscv32_efi.lds
-PLATFORM_ELFFLAGS	+= -B riscv -O elf32-littleriscv
+PLATFORM_ELFFLAGS	+= -B riscv -O elf32-$(large-endian)riscv
 endif
 
 ifdef CONFIG_64BIT
 KBUILD_LDFLAGS		+= -m $(64bit-emul)
 EFI_LDS			:= elf_riscv64_efi.lds
-PLATFORM_ELFFLAGS	+= -B riscv -O elf64-littleriscv
+PLATFORM_ELFFLAGS	+= -B riscv -O elf64-$(large-endian)riscv
 endif
 
 PLATFORM_CPPFLAGS	+= -ffixed-x3 -fpic
Signature · 1 line
2.37.2.352.g3c44437643
#2 lore raw

Re: [PATCH 1/2] riscv: add build support for big-endian

Quoted 14 lines · Ben Dooks

Add support to build code big-endian if the board supports it. Updates the makefile to pass the correct compiler and elf flags.

---
 arch/riscv/config.mk | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
arch/riscv/config.mk +0 -0
index 9f16dda92a0..2eeeb433748 100644

arch/riscv/config.mk line 13

Quoted 6 lines
arch/riscv/config.mk +1 -2
@@ -10,19 +10,29 @@
 # Rick Chen, Andes Technology Corporation <[email protected]>
 #
 
-32bit-emul		:= elf32lriscv
-64bit-emul		:= elf64lriscv
+ifdef CONFIG_SYS_BIT_ENDIAN

Typo, should be BIG not BIT.

arch/riscv/config.mk line 21

Quoted 8 lines
arch/riscv/config.mk +8 -0
+small-endian		:= b
+large-endian		:= big
+else
+small-endian		:= b
+large-endian		:= big
+PLATFORM_CPPFLAGS       += -mbig-endian
+KBUILD_LDFLAGS          += -mbig-endian
+endif

And then fix it so we can go back to little, too :)

Signature · 1 line
Tom
author
#3 lore raw

PATCH 2/2 riscv: byteorder: add test for big-endian

In reply to Ben Dooks

Test for big-endian either via __RISCVEB__ which migth be rather old, or check the BYTE_ORDER if the compiler defines it (which should be any modern gcc like v12)

---
 arch/riscv/include/asm/byteorder.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/byteorder.h +1 -1
index d26ac5688fa..3140c1f585e 100644
@@ -26,7 +26,7 @@
 #  define __SWAB_64_THRU_32__
 #endif
 
-#ifdef __RISCVEB__
+#if  defined(__RISCVEB__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
 #include <linux/byteorder/big_endian.h>
 #else
 #include <linux/byteorder/little_endian.h>
Signature · 1 line
2.37.2.352.g3c44437643
#4 lore raw

Re: [PATCH 2/2] riscv: byteorder: add test for big-endian

Quoted 14 lines · Ben Dooks

Test for big-endian either via __RISCVEB__ which migth be rather old, or check the BYTE_ORDER if the compiler defines it (which should be any modern gcc like v12)

---
 arch/riscv/include/asm/byteorder.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/byteorder.h +0 -0
index d26ac5688fa..3140c1f585e 100644

arch/riscv/include/asm/byteorder.h line 32

Quoted 8 lines
arch/riscv/include/asm/byteorder.h +1 -1
@@ -26,7 +26,7 @@
 #  define __SWAB_64_THRU_32__
 #endif
 
-#ifdef __RISCVEB__
+#if  defined(__RISCVEB__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
 #include <linux/byteorder/big_endian.h>
 #else
 #include <linux/byteorder/little_endian.h>

What's this look like in the linux kernel? We should likely just mirror that.

Signature · 1 line
Tom
author
#5 lore raw

Re: [PATCH 2/2] riscv: byteorder: add test for big-endian

Quoted 19 lines · Tom Rini
Quoted 17 lines · Ben Dooks

Test for big-endian either via __RISCVEB__ which migth be rather old, or check the BYTE_ORDER if the compiler defines it (which should be any modern gcc like v12)

---
  arch/riscv/include/asm/byteorder.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/byteorder.h +0 -0
index d26ac5688fa..3140c1f585e 100644
@@ -26,7 +26,7 @@
  #  define __SWAB_64_THRU_32__
  #endif
 
Quoted 8 lines

arch/riscv/include/asm/byteorder.h line 32

Quoted 5 lines
arch/riscv/include/asm/byteorder.h +1 -1
-#ifdef __RISCVEB__
+#if  defined(__RISCVEB__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  #include <linux/byteorder/big_endian.h>
  #else
  #include <linux/byteorder/little_endian.h>

What's this look like in the linux kernel? We should likely just mirror that.

We're still looking at the kernel too.

I think we could just drop the __RISCVEB__ test, I don't think anything is defining it.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html