Skip to contentSkip to conversation

RFC: big endian support for riscv64/opensbi

RFC New Ben Dooks 2 participants 13 messages Dec 20, 2024 – Feb 11, 2025

Subsystems (2)
lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

This is part of our project to investigate big endian on RISC-V. The full information is documented on our gitlab[1] which includes source repositories, build information and project documentation.

We have a minimal buildroot, qemu and kernel working on QEMU.

As this is a work in progress any review or help is appreciated.

[1] https://gitlab.com/CodethinkLabs/riscv_bigendian

author
#1 lore raw

RFC 1/7 opensbi: use .insn for instrcutions to fix endian

Using .word for creating instructions means they are written in the data endian, not the instruction endian. Change .word to .insn to tell the assembler it is creating custom instructions.

---
 lib/sbi/sbi_hfence.S | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
lib/sbi/sbi_hfence.S +8 -8
index e11e650..6f91372 100644
@@ -32,7 +32,7 @@ __sbi_hfence_gvma_vmid_gpa:
 	 * HFENCE.GVMA a0, a1
 	 * 0110001 01011 01010 000 00000 1110011
 	 */
-	.word 0x62b50073
+	.insn 0x62b50073
 	ret
 
 	.align 3
@@ -44,7 +44,7 @@ __sbi_hfence_gvma_vmid:
 	 * HFENCE.GVMA zero, a0
 	 * 0110001 01010 00000 000 00000 1110011
 	 */
-	.word 0x62a00073
+	.insn 0x62a00073
 	ret
 
 	.align 3
@@ -56,7 +56,7 @@ __sbi_hfence_gvma_gpa:
 	 * HFENCE.GVMA a0
 	 * 0110001 00000 01010 000 00000 1110011
 	 */
-	.word 0x62050073
+	.insn 0x62050073
 	ret
 
 	.align 3
@@ -68,7 +68,7 @@ __sbi_hfence_gvma_all:
 	 * HFENCE.GVMA
 	 * 0110001 00000 00000 000 00000 1110011
 	 */
-	.word 0x62000073
+	.insn 0x62000073
 	ret
 
 	/*
@@ -95,7 +95,7 @@ __sbi_hfence_vvma_asid_va:
 	 * HFENCE.VVMA a0, a1
 	 * 0010001 01011 01010 000 00000 1110011
 	 */
-	.word 0x22b50073
+	.insn 0x22b50073
 	ret
 
 	.align 3
@@ -107,7 +107,7 @@ __sbi_hfence_vvma_asid:
 	 * HFENCE.VVMA zero, a0
 	 * 0010001 01010 00000 000 00000 1110011
 	 */
-	.word 0x22a00073
+	.insn 0x22a00073
 	ret
 
 	.align 3
@@ -119,7 +119,7 @@ __sbi_hfence_vvma_va:
 	 * HFENCE.VVMA zero, a0
 	 * 0010001 00000 01010 000 00000 1110011
 	 */
-	.word 0x22050073
+	.insn 0x22050073
 	ret
 
 	.align 3
@@ -131,5 +131,5 @@ __sbi_hfence_vvma_all:
 	 * HFENCE.VVMA
 	 * 0010001 00000 00000 000 00000 1110011
 	 */
-	.word 0x22000073
+	.insn 0x22000073
 	ret
Signature · 1 line
2.37.2.352.g3c44437643
#2 lore raw

RFC 1/7 opensbi: use .insn for instrcutions to fix endian

Quoted 8 lines · On Fri, Dec 20, 2024 at 10:40?PM Ben Dooks <[email protected]> wrote:

Using .word for creating instructions means they are written in the data endian, not the instruction endian. Change .word to .insn to tell the assembler it is creating custom instructions.

LGTM.

Regards,
Anup

Quoted 86 lines
---
 lib/sbi/sbi_hfence.S | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
lib/sbi/sbi_hfence.S +8 -9
index e11e650..6f91372 100644
@@ -32,7 +32,7 @@ __sbi_hfence_gvma_vmid_gpa:
         * HFENCE.GVMA a0, a1
         * 0110001 01011 01010 000 00000 1110011
         */
-       .word 0x62b50073
+       .insn 0x62b50073
        ret
 
        .align 3
@@ -44,7 +44,7 @@ __sbi_hfence_gvma_vmid:
         * HFENCE.GVMA zero, a0
         * 0110001 01010 00000 000 00000 1110011
         */
-       .word 0x62a00073
+       .insn 0x62a00073
        ret
 
        .align 3
@@ -56,7 +56,7 @@ __sbi_hfence_gvma_gpa:
         * HFENCE.GVMA a0
         * 0110001 00000 01010 000 00000 1110011
         */
-       .word 0x62050073
+       .insn 0x62050073
        ret
 
        .align 3
@@ -68,7 +68,7 @@ __sbi_hfence_gvma_all:
         * HFENCE.GVMA
         * 0110001 00000 00000 000 00000 1110011
         */
-       .word 0x62000073
+       .insn 0x62000073
        ret
 
        /*
@@ -95,7 +95,7 @@ __sbi_hfence_vvma_asid_va:
         * HFENCE.VVMA a0, a1
         * 0010001 01011 01010 000 00000 1110011
         */
-       .word 0x22b50073
+       .insn 0x22b50073
        ret
 
        .align 3
@@ -107,7 +107,7 @@ __sbi_hfence_vvma_asid:
         * HFENCE.VVMA zero, a0
         * 0010001 01010 00000 000 00000 1110011
         */
-       .word 0x22a00073
+       .insn 0x22a00073
        ret
 
        .align 3
@@ -119,7 +119,7 @@ __sbi_hfence_vvma_va:
         * HFENCE.VVMA zero, a0
         * 0010001 00000 01010 000 00000 1110011
         */
-       .word 0x22050073
+       .insn 0x22050073
        ret
 
        .align 3
@@ -131,5 +131,5 @@ __sbi_hfence_vvma_all:
         * HFENCE.VVMA
         * 0010001 00000 00000 000 00000 1110011
         */
-       .word 0x22000073
+       .insn 0x22000073
        ret
--

2.37.2.352.g3c44437643

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

author
#3 lore raw

RFC 2/7 opensbi: add build config for building big-endian

In reply to Ben Dooks

Add a build option for big-endian OpenSBI build.

---
 Kconfig  |  5 +++++
 Makefile | 18 ++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)
Kconfig +5 -0
index acfc138..cc7b5bc 100644
@@ -2,6 +2,11 @@
 
 mainmenu "OpenSBI $(OPENSBI_PLATFORM) Configuration"
 
+config OPENSBI_BE
+	bool "Build OpenSBI big-endian"
+	help
+	  Say Y here if you like fun challenges
+
 config OPENSBI_SRC_DIR
 	string
 	option env="OPENSBI_SRC_DIR"
Makefile +16 -2
index d9cee49..3b6d981 100644
@@ -357,6 +357,9 @@ GENFLAGS	+=	$(firmware-genflags-y)
 
 CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections
 CFLAGS		+=	-fno-omit-frame-pointer -fno-optimize-sibling-calls
+ifeq ($(CONFIG_OPENSBI_BE),y)
+CFLAGS		+=	-mbig-endian
+endif
 # Optionally supported flags
 ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
 CFLAGS		+=	-mno-save-restore
@@ -397,6 +400,9 @@ endif
 ASFLAGS		+=	$(GENFLAGS)
 ASFLAGS		+=	$(platform-asflags-y)
 ASFLAGS		+=	$(firmware-asflags-y)
+ifeq ($(CONFIG_OPENSBI_BE),y)
+ASFLAGS		+=	-mbig-endian
+endif
 
 ARFLAGS		=	rcs
 
@@ -410,13 +416,21 @@ ELFFLAGS	+=	-Wl,--no-dynamic-linker -Wl,-pie
 ELFFLAGS	+=	$(platform-ldflags-y)
 ELFFLAGS	+=	$(firmware-ldflags-y)
 
+ifeq ($(CONFIG_OPENSBI_BE),y)
+FULL_ENDIAN=big
+SMALL_ENDIAN=b
+else
+FULL_ENDIAN=little
+SMALL_ENDIAN=l
+endif
+
 MERGEFLAGS	+=	-r
 ifeq ($(LD_IS_LLD),y)
 MERGEFLAGS	+=	-b elf
 else
-MERGEFLAGS	+=	-b elf$(PLATFORM_RISCV_XLEN)-littleriscv
+MERGEFLAGS	+=	-b elf$(PLATFORM_RISCV_XLEN)-$(FULL_ENDIAN)riscv
 endif
-MERGEFLAGS	+=	-m elf$(PLATFORM_RISCV_XLEN)lriscv
+MERGEFLAGS	+=	-m elf$(PLATFORM_RISCV_XLEN)$(SMALL_ENDIAN)riscv
 
 DTSCPPFLAGS	=	$(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp
 
Signature · 1 line
2.37.2.352.g3c44437643
#4 lore raw

RFC 2/7 opensbi: add build config for building big-endian

Quoted 16 lines · On Fri, Dec 20, 2024 at 10:40?PM Ben Dooks <[email protected]> wrote:

Add a build option for big-endian OpenSBI build.

---
 Kconfig  |  5 +++++
 Makefile | 18 ++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)
Kconfig +0 -0
index acfc138..cc7b5bc 100644

Kconfig line 9

Quoted 8 lines
Kconfig +5 -0
@@ -2,6 +2,11 @@
 
 mainmenu "OpenSBI $(OPENSBI_PLATFORM) Configuration"
 
+config OPENSBI_BE
+       bool "Build OpenSBI big-endian"
+       help
+         Say Y here if you like fun challenges
+

Instead of this kconfig option, I would suggest using the toplevel make parameter "PLATFORM_RISCV_BE=1" to be consistent with other PLATFORM_RISCV_xyz make parameters.

Quoted 55 lines
Kconfig +0 -0
 config OPENSBI_SRC_DIR
        string
        option env="OPENSBI_SRC_DIR"
Makefile +16 -3
index d9cee49..3b6d981 100644
@@ -357,6 +357,9 @@ GENFLAGS    +=      $(firmware-genflags-y)
 
 CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections
 CFLAGS         +=      -fno-omit-frame-pointer -fno-optimize-sibling-calls
+ifeq ($(CONFIG_OPENSBI_BE),y)
+CFLAGS         +=      -mbig-endian
+endif
 # Optionally supported flags
 ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
 CFLAGS         +=      -mno-save-restore
@@ -397,6 +400,9 @@ endif
 ASFLAGS                +=      $(GENFLAGS)
 ASFLAGS                +=      $(platform-asflags-y)
 ASFLAGS                +=      $(firmware-asflags-y)
+ifeq ($(CONFIG_OPENSBI_BE),y)
+ASFLAGS                +=      -mbig-endian
+endif
 
 ARFLAGS                =       rcs
 
@@ -410,13 +416,21 @@ ELFFLAGS  +=      -Wl,--no-dynamic-linker -Wl,-pie
 ELFFLAGS       +=      $(platform-ldflags-y)
 ELFFLAGS       +=      $(firmware-ldflags-y)
 
+ifeq ($(CONFIG_OPENSBI_BE),y)
+FULL_ENDIAN=big
+SMALL_ENDIAN=b
+else
+FULL_ENDIAN=little
+SMALL_ENDIAN=l
+endif
+
 MERGEFLAGS     +=      -r
 ifeq ($(LD_IS_LLD),y)
 MERGEFLAGS     +=      -b elf
 else
-MERGEFLAGS     +=      -b elf$(PLATFORM_RISCV_XLEN)-littleriscv
+MERGEFLAGS     +=      -b elf$(PLATFORM_RISCV_XLEN)-$(FULL_ENDIAN)riscv
 endif
-MERGEFLAGS     +=      -m elf$(PLATFORM_RISCV_XLEN)lriscv
+MERGEFLAGS     +=      -m elf$(PLATFORM_RISCV_XLEN)$(SMALL_ENDIAN)riscv
 
 DTSCPPFLAGS    =       $(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp
 
--

2.37.2.352.g3c44437643

Quoted 4 lines

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

Regards,
Anup

author
#5 lore raw

RFC 3/7 opensbi: add option to set big-endian mode at run-time

In reply to Ben Dooks

Add option to switch M-mode to BE at start by setting the MSTATUS.MBE bit early on in the initialisation code and ensure it stays set when hart is reinitialised.

---
 Kconfig            | 4 ++++
 firmware/fw_base.S | 8 ++++++++
 lib/sbi/sbi_hart.c | 4 ++++
 3 files changed, 16 insertions(+)
Kconfig +4 -0
index cc7b5bc..6c6236f 100644
@@ -7,6 +7,10 @@ config OPENSBI_BE
 	help
 	  Say Y here if you like fun challenges
 
+config OPENSBI_BE_SET
+	bool "Set M-Mode to be big-endian in startup code"
+	depends on OPENSBI_BE
+
 config OPENSBI_SRC_DIR
 	string
 	option env="OPENSBI_SRC_DIR"
firmware/fw_base.S +8 -0
index d027e5e..f8ad6db 100644
@@ -46,6 +46,14 @@
 	.globl _start
 	.globl _start_warm
 _start:
+#ifdef CONFIG_OPENSBI_BE_SET
+#if __riscv_xlen == 64
+	li	s0, MSTATUS_MBE
+	csrs	CSR_MSTATUS, s0
+#else
+#error not done for 32bit
+#endif
+#endif
 	/* Find preferred boot HART id */
 	MOV_3R	s0, a0, s1, a1, s2, a2
 	call	fw_boot_hart
lib/sbi/sbi_hart.c +4 -0
index 0451fcb..39311c4 100644
@@ -39,6 +39,10 @@ static void mstatus_init(struct sbi_scratch *scratch)
 	uint64_t mhpmevent_init_val = 0;
 	uint64_t menvcfg_val, mstateen_val;
 
+#ifdef CONFIG_OPENSBI_BE_SET
+	mstatus_val |= MSTATUS_MBE;
+#endif
+
 	/* Enable FPU */
 	if (misa_extension('D') || misa_extension('F'))
 		mstatus_val |=  MSTATUS_FS;
Signature · 1 line
2.37.2.352.g3c44437643
#6 lore raw

RFC 3/7 opensbi: add option to set big-endian mode at run-time

Quoted 34 lines · On Fri, Dec 20, 2024 at 10:40?PM Ben Dooks <[email protected]> wrote:

Add option to switch M-mode to BE at start by setting the MSTATUS.MBE bit early on in the initialisation code and ensure it stays set when hart is reinitialised.

---
 Kconfig            | 4 ++++
 firmware/fw_base.S | 8 ++++++++
 lib/sbi/sbi_hart.c | 4 ++++
 3 files changed, 16 insertions(+)
Kconfig +4 -0
index cc7b5bc..6c6236f 100644
@@ -7,6 +7,10 @@ config OPENSBI_BE
        help
          Say Y here if you like fun challenges
 
+config OPENSBI_BE_SET
+       bool "Set M-Mode to be big-endian in startup code"
+       depends on OPENSBI_BE
+
 config OPENSBI_SRC_DIR
        string
        option env="OPENSBI_SRC_DIR"
firmware/fw_base.S +0 -0
index d027e5e..f8ad6db 100644

firmware/fw_base.S line 49

Quoted 4 lines
firmware/fw_base.S +1 -0
@@ -46,6 +46,14 @@
        .globl _start
        .globl _start_warm
 _start:
+#ifdef CONFIG_OPENSBI_BE_SET

Instead of kconfig option you can simply use
"#if __BYTE_ORDER == __BIG_ENDIAN"

Quoted 15 lines
firmware/fw_base.S +7 -0
+#if __riscv_xlen == 64
+       li      s0, MSTATUS_MBE
+       csrs    CSR_MSTATUS, s0
+#else
+#error not done for 32bit
+#endif
+#endif
        /* Find preferred boot HART id */
        MOV_3R  s0, a0, s1, a1, s2, a2
        call    fw_boot_hart
lib/sbi/sbi_hart.c +0 -0
index 0451fcb..39311c4 100644

lib/sbi/sbi_hart.c static void mstatus_init(struct sbi_scratch *scratch) · line 45

Quoted 7 lines
lib/sbi/sbi_hart.c +4 -0
@@ -39,6 +39,10 @@ static void mstatus_init(struct sbi_scratch *scratch)
        uint64_t mhpmevent_init_val = 0;
        uint64_t menvcfg_val, mstateen_val;
 
+#ifdef CONFIG_OPENSBI_BE_SET
+       mstatus_val |= MSTATUS_MBE;
+#endif
+

Same as above.

Quoted 11 lines
lib/sbi/sbi_hart.c +0 -1
        /* Enable FPU */
        if (misa_extension('D') || misa_extension('F'))
                mstatus_val |=  MSTATUS_FS;
--

2.37.2.352.g3c44437643

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

Regards,
Anup

author
#7 lore raw

RFC 4/7 opensbi: make io little-endian for big endian build

In reply to Ben Dooks

Generally IO is little endian, if we're running in big-endian mode then swap the byte ordering in the IO accessors.

---
 include/sbi/riscv_io.h | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
include/sbi/riscv_io.h +21 -10
index 4913232..a8fa579 100644
@@ -12,6 +12,7 @@
 
 #include <sbi/riscv_barrier.h>
 #include <sbi/sbi_types.h>
+#include <sbi/sbi_byteorder.h>
 
 static inline void __raw_writeb(u8 val, volatile void *addr)
 {
@@ -73,22 +74,32 @@ static inline u64 __raw_readq(const volatile void *addr)
 
 /* clang-format off */
 
+#ifdef CONFIG_OPENSBI_BE
+#define io_conv16(__x) cpu_to_le16(__x)
+#define io_conv32(__x) cpu_to_le32(__x)
+#define io_conv64(__x) cpu_to_le64(__x)
+#else
+#define io_conv16(__x) (__x)
+#define io_conv32(__x) (__x)
+#define io_conv64(__x) (__x)
+#endif
+
 #define __io_rbr()		do {} while (0)
 #define __io_rar()		do {} while (0)
 #define __io_rbw()		do {} while (0)
 #define __io_raw()		do {} while (0)
 
 #define readb_relaxed(c)	({ u8  __v; __io_rbr(); __v = __raw_readb(c); __io_rar(); __v; })
-#define readw_relaxed(c)	({ u16 __v; __io_rbr(); __v = __raw_readw(c); __io_rar(); __v; })
-#define readl_relaxed(c)	({ u32 __v; __io_rbr(); __v = __raw_readl(c); __io_rar(); __v; })
+#define readw_relaxed(c)	({ u16 __v; __io_rbr(); __v = __raw_readw(c); __io_rar(); io_conv16(__v); })
+#define readl_relaxed(c)	({ u32 __v; __io_rbr(); __v = __raw_readl(c); __io_rar(); io_conv32(__v); })
 
 #define writeb_relaxed(v,c)	({ __io_rbw(); __raw_writeb((v),(c)); __io_raw(); })
-#define writew_relaxed(v,c)	({ __io_rbw(); __raw_writew((v),(c)); __io_raw(); })
-#define writel_relaxed(v,c)	({ __io_rbw(); __raw_writel((v),(c)); __io_raw(); })
+#define writew_relaxed(v,c)	({ __io_rbw(); __raw_writew(io_conv16(v),(c)); __io_raw(); })
+#define writel_relaxed(v,c)	({ __io_rbw(); __raw_writel(io_conv32(v),(c)); __io_raw(); })
 
 #if __riscv_xlen != 32
-#define readq_relaxed(c)	({ u64 __v; __io_rbr(); __v = __raw_readq(c); __io_rar(); __v; })
-#define writeq_relaxed(v,c)	({ __io_rbw(); __raw_writeq((v),(c)); __io_raw(); })
+#define readq_relaxed(c)	({ u64 __v; __io_rbr(); __v = __raw_readq(c); __io_rar(); io_conv64(__v); })
+#define writeq_relaxed(v,c)	({ __io_rbw(); __raw_writeq(io_conv64(v),(c)); __io_raw(); })
 #endif
 
 #define __io_br()	do {} while (0)
@@ -97,12 +108,12 @@ static inline u64 __raw_readq(const volatile void *addr)
 #define __io_aw()	do {} while (0)
 
 #define readb(c)	({ u8  __v; __io_br(); __v = __raw_readb(c); __io_ar(); __v; })
-#define readw(c)	({ u16 __v; __io_br(); __v = __raw_readw(c); __io_ar(); __v; })
-#define readl(c)	({ u32 __v; __io_br(); __v = __raw_readl(c); __io_ar(); __v; })
+#define readw(c)	({ u16 __v; __io_br(); __v = __raw_readw(c); __io_ar(); io_conv16(__v); })
+#define readl(c)	({ u32 __v; __io_br(); __v = __raw_readl(c); __io_ar(); io_conv32(__v); })
 
 #define writeb(v,c)	({ __io_bw(); __raw_writeb((v),(c)); __io_aw(); })
-#define writew(v,c)	({ __io_bw(); __raw_writew((v),(c)); __io_aw(); })
-#define writel(v,c)	({ __io_bw(); __raw_writel((v),(c)); __io_aw(); })
+#define writew(v,c)	({ __io_bw(); __raw_writew(io_conv16(v),(c)); __io_aw(); })
+#define writel(v,c)	({ __io_bw(); __raw_writel(io_conv32(v),(c)); __io_aw(); })
 
 #if __riscv_xlen != 32
 #define readq(c)	({ u64 __v; __io_br(); __v = __raw_readq(c); __io_ar(); __v; })
Signature · 1 line
2.37.2.352.g3c44437643
#8 lore raw

RFC 4/7 opensbi: make io little-endian for big endian build

Quoted 26 lines · On Fri, Dec 20, 2024 at 9:32?PM Ben Dooks <[email protected]> wrote:

Generally IO is little endian, if we're running in big-endian mode then swap the byte ordering in the IO accessors.

---
 include/sbi/riscv_io.h | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
include/sbi/riscv_io.h +1 -0
index 4913232..a8fa579 100644
@@ -12,6 +12,7 @@
 
 #include <sbi/riscv_barrier.h>
 #include <sbi/sbi_types.h>
+#include <sbi/sbi_byteorder.h>
 
 static inline void __raw_writeb(u8 val, volatile void *addr)
 {
@@ -73,22 +74,32 @@ static inline u64 __raw_readq(const volatile void *addr)
 
 /* clang-format off */

include/sbi/riscv_io.h static inline u64 __raw_readq(const volatile void *addr) · line 85

Quoted 10 lines
include/sbi/riscv_io.h +9 -0
 
+#ifdef CONFIG_OPENSBI_BE
+#define io_conv16(__x) cpu_to_le16(__x)
+#define io_conv32(__x) cpu_to_le32(__x)
+#define io_conv64(__x) cpu_to_le64(__x)
+#else
+#define io_conv16(__x) (__x)
+#define io_conv32(__x) (__x)
+#define io_conv64(__x) (__x)
+#endif

No need for these io_convXY() macros. You can directly use cpu_to_leXY() macros because these macros won't change byte ordering on a LE system.

Quoted 47 lines
include/sbi/riscv_io.h +11 -11
+
 #define __io_rbr()             do {} while (0)
 #define __io_rar()             do {} while (0)
 #define __io_rbw()             do {} while (0)
 #define __io_raw()             do {} while (0)
 
 #define readb_relaxed(c)       ({ u8  __v; __io_rbr(); __v = __raw_readb(c); __io_rar(); __v; })
-#define readw_relaxed(c)       ({ u16 __v; __io_rbr(); __v = __raw_readw(c); __io_rar(); __v; })
-#define readl_relaxed(c)       ({ u32 __v; __io_rbr(); __v = __raw_readl(c); __io_rar(); __v; })
+#define readw_relaxed(c)       ({ u16 __v; __io_rbr(); __v = __raw_readw(c); __io_rar(); io_conv16(__v); })
+#define readl_relaxed(c)       ({ u32 __v; __io_rbr(); __v = __raw_readl(c); __io_rar(); io_conv32(__v); })
 
 #define writeb_relaxed(v,c)    ({ __io_rbw(); __raw_writeb((v),(c)); __io_raw(); })
-#define writew_relaxed(v,c)    ({ __io_rbw(); __raw_writew((v),(c)); __io_raw(); })
-#define writel_relaxed(v,c)    ({ __io_rbw(); __raw_writel((v),(c)); __io_raw(); })
+#define writew_relaxed(v,c)    ({ __io_rbw(); __raw_writew(io_conv16(v),(c)); __io_raw(); })
+#define writel_relaxed(v,c)    ({ __io_rbw(); __raw_writel(io_conv32(v),(c)); __io_raw(); })
 
 #if __riscv_xlen != 32
-#define readq_relaxed(c)       ({ u64 __v; __io_rbr(); __v = __raw_readq(c); __io_rar(); __v; })
-#define writeq_relaxed(v,c)    ({ __io_rbw(); __raw_writeq((v),(c)); __io_raw(); })
+#define readq_relaxed(c)       ({ u64 __v; __io_rbr(); __v = __raw_readq(c); __io_rar(); io_conv64(__v); })
+#define writeq_relaxed(v,c)    ({ __io_rbw(); __raw_writeq(io_conv64(v),(c)); __io_raw(); })
 #endif
 
 #define __io_br()      do {} while (0)
@@ -97,12 +108,12 @@ static inline u64 __raw_readq(const volatile void *addr)
 #define __io_aw()      do {} while (0)
 
 #define readb(c)       ({ u8  __v; __io_br(); __v = __raw_readb(c); __io_ar(); __v; })
-#define readw(c)       ({ u16 __v; __io_br(); __v = __raw_readw(c); __io_ar(); __v; })
-#define readl(c)       ({ u32 __v; __io_br(); __v = __raw_readl(c); __io_ar(); __v; })
+#define readw(c)       ({ u16 __v; __io_br(); __v = __raw_readw(c); __io_ar(); io_conv16(__v); })
+#define readl(c)       ({ u32 __v; __io_br(); __v = __raw_readl(c); __io_ar(); io_conv32(__v); })
 
 #define writeb(v,c)    ({ __io_bw(); __raw_writeb((v),(c)); __io_aw(); })
-#define writew(v,c)    ({ __io_bw(); __raw_writew((v),(c)); __io_aw(); })
-#define writel(v,c)    ({ __io_bw(); __raw_writel((v),(c)); __io_aw(); })
+#define writew(v,c)    ({ __io_bw(); __raw_writew(io_conv16(v),(c)); __io_aw(); })
+#define writel(v,c)    ({ __io_bw(); __raw_writel(io_conv32(v),(c)); __io_aw(); })
 
 #if __riscv_xlen != 32
 #define readq(c)       ({ u64 __v; __io_br(); __v = __raw_readq(c); __io_ar(); __v; })
--

2.37.2.352.g3c44437643

Quoted 4 lines

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

Regards,
Anup

author
#9 lore raw

RFC 5/7 internal: add big/little endian to boot print

In reply to Ben Dooks

Make a nice new banner for our big endian system.

---
 lib/sbi/sbi_init.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
lib/sbi/sbi_init.c +15 -2
index 8a2cfae..9009f78 100644
@@ -32,6 +32,17 @@
 #include <sbi/sbi_version.h>
 #include <sbi/sbi_unit_test.h>
 
+#ifdef CONFIG_OPENSBI_BE
+#define ENDIAN "big-endian "
+#define BANNER	\
+        "   ___                   ____  ____ ___    ____  _____ \n"	\
+        "  / _ \\ _ __   ___ _ __ / ___|| __ )_ _|  | __ )| ____|\n"	\
+        " | | | | '_ \\ / _ \\ '_ \\ \\ ___ \\|  _ \\| |   |  _ \\|  _|  \n"	\
+        " | |_| | |_) |  __/ | | |___) | |_) | |   | |_) | |___ \n"	\
+        "  \\___/| .__/ \\___|_| |_|____/|____/___|  |____/|_____|\n"	\
+        "       |_|                                             \n\n"
+#else
+#define ENDIAN "little-endian "
 #define BANNER                                              \
 	"   ____                    _____ ____ _____\n"     \
 	"  / __ \\                  / ____|  _ \\_   _|\n"  \
@@ -41,6 +52,8 @@
 	"  \\____/| .__/ \\___|_| |_|_____/|____/_____|\n"  \
 	"        | |\n"                                     \
 	"        |_|\n\n"
+#endif
+
 
 static void sbi_boot_print_banner(struct sbi_scratch *scratch)
 {
@@ -48,9 +61,9 @@ static void sbi_boot_print_banner(struct sbi_scratch *scratch)
 		return;
 
 #ifdef OPENSBI_VERSION_GIT
-	sbi_printf("\nOpenSBI %s\n", OPENSBI_VERSION_GIT);
+	sbi_printf("\nOpenSBI %s%s\n", ENDIAN, OPENSBI_VERSION_GIT);
 #else
-	sbi_printf("\nOpenSBI v%d.%d\n", OPENSBI_VERSION_MAJOR,
+	sbi_printf("\nOpenSBI %sv%d.%d\n", ENDIAN, OPENSBI_VERSION_MAJOR,
 		   OPENSBI_VERSION_MINOR);
 #endif
 
Signature · 1 line
2.37.2.352.g3c44437643
#10 lore raw

RFC 5/7 internal: add big/little endian to boot print

Quoted 38 lines · On Fri, Dec 20, 2024 at 10:40?PM Ben Dooks <[email protected]> wrote:

Make a nice new banner for our big endian system.

---
 lib/sbi/sbi_init.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
lib/sbi/sbi_init.c +13 -0
index 8a2cfae..9009f78 100644
@@ -32,6 +32,17 @@
 #include <sbi/sbi_version.h>
 #include <sbi/sbi_unit_test.h>
 
+#ifdef CONFIG_OPENSBI_BE
+#define ENDIAN "big-endian "
+#define BANNER \
+        "   ___                   ____  ____ ___    ____  _____ \n"    \
+        "  / _ \\ _ __   ___ _ __ / ___|| __ )_ _|  | __ )| ____|\n"   \
+        " | | | | '_ \\ / _ \\ '_ \\ \\ ___ \\|  _ \\| |   |  _ \\|  _|  \n"   \
+        " | |_| | |_) |  __/ | | |___) | |_) | |   | |_) | |___ \n"    \
+        "  \\___/| .__/ \\___|_| |_|____/|____/___|  |____/|_____|\n"  \
+        "       |_|                                             \n\n"
+#else
+#define ENDIAN "little-endian "
 #define BANNER                                              \
        "   ____                    _____ ____ _____\n"     \
        "  / __ \\                  / ____|  _ \\_   _|\n"  \
@@ -41,6 +52,8 @@
        "  \\____/| .__/ \\___|_| |_|_____/|____/_____|\n"  \
        "        | |\n"                                     \
        "        |_|\n\n"
+#endif
+
 
 static void sbi_boot_print_banner(struct sbi_scratch *scratch)
 {

lib/sbi/sbi_init.c static void sbi_boot_print_banner(struct sbi_scratch *scratch) · line 67

Quoted 9 lines
lib/sbi/sbi_init.c +2 -2
@@ -48,9 +61,9 @@ static void sbi_boot_print_banner(struct sbi_scratch *scratch)
                return;
 
 #ifdef OPENSBI_VERSION_GIT
-       sbi_printf("\nOpenSBI %s\n", OPENSBI_VERSION_GIT);
+       sbi_printf("\nOpenSBI %s%s\n", ENDIAN, OPENSBI_VERSION_GIT);
 #else
-       sbi_printf("\nOpenSBI v%d.%d\n", OPENSBI_VERSION_MAJOR,
+       sbi_printf("\nOpenSBI %sv%d.%d\n", ENDIAN, OPENSBI_VERSION_MAJOR,
                   OPENSBI_VERSION_MINOR);

Endianness is a property of the underlying platform or RISC-V implementation so it is better to print it as:
"Platform Endianness : little"
or something similar.

Quoted 10 lines
lib/sbi/sbi_init.c +0 -1
 #endif
 
--

2.37.2.352.g3c44437643

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

Regards,
Anup

author
#11 lore raw

RFC 6/7 opensbi: fixup endian access for fw_info

In reply to Ben Dooks

QEMU passes this as le32/le64, so add a new accessor for LE and use that to get the fields from the fw_dynamic_info.

---
 firmware/fw_dynamic.S    | 17 +++++++++--------
 include/sbi/fw_dynamic.h |  8 ++++++++
 include/sbi/riscv_asm.h  | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 8 deletions(-)
firmware/fw_dynamic.S +9 -8
index 5ee2883..0f8958d 100644
@@ -22,17 +22,17 @@
 	 */
 fw_boot_hart:
 	/* Sanity checks */
-	li	a1, FW_DYNAMIC_INFO_MAGIC_VALUE
 	REG_L	a0, FW_DYNAMIC_INFO_MAGIC_OFFSET(a2)
+	li	a1, FW_DYNAMIC_INFO_MAGIC_VALUE
 	bne	a0, a1, _start_hang
+	REG_L_LE a1, a0, FW_DYNAMIC_INFO_VERSION_OFFSET(a2)
 	li	a1, FW_DYNAMIC_INFO_VERSION_MAX
-	REG_L	a0, FW_DYNAMIC_INFO_VERSION_OFFSET(a2)
 	bgt	a0, a1, _start_hang
 
 	/* Read boot HART id */
 	li	a1, FW_DYNAMIC_INFO_VERSION_2
 	blt	a0, a1, 2f
-	REG_L	a0, FW_DYNAMIC_INFO_BOOT_HART_OFFSET(a2)
+	REG_L_LE a1, a0, FW_DYNAMIC_INFO_BOOT_HART_OFFSET(a2)
 	ret
 2:	li	a0, -1
 	ret
@@ -53,23 +53,24 @@ fw_save_info:
 
 	/* Save version == 0x1 fields */
 	lla	a4, _dynamic_next_addr
-	REG_L	a3, FW_DYNAMIC_INFO_NEXT_ADDR_OFFSET(a2)
+	REG_L_LE a1, a3, FW_DYNAMIC_INFO_NEXT_ADDR_OFFSET(a2)
 	REG_S	a3, (a4)
 	lla	a4, _dynamic_next_mode
-	REG_L	a3, FW_DYNAMIC_INFO_NEXT_MODE_OFFSET(a2)
+	REG_L_LE a1, a3, FW_DYNAMIC_INFO_NEXT_MODE_OFFSET(a2)
 	REG_S	a3, (a4)
 	lla	a4, _dynamic_options
-	REG_L	a3, FW_DYNAMIC_INFO_OPTIONS_OFFSET(a2)
+	REG_L_LE a1, a3, FW_DYNAMIC_INFO_OPTIONS_OFFSET(a2)
 	REG_S	a3, (a4)
 
 	/* Save version == 0x2 fields */
 	li	a4, FW_DYNAMIC_INFO_VERSION_2
-	REG_L	a3, FW_DYNAMIC_INFO_VERSION_OFFSET(a2)
+	REG_L_LE a1, a3, FW_DYNAMIC_INFO_VERSION_OFFSET(a2)
 	blt	a3, a4, 2f
 	lla	a4, _dynamic_boot_hart
-	REG_L	a3, FW_DYNAMIC_INFO_BOOT_HART_OFFSET(a2)
+	REG_L_LE a1, a3, FW_DYNAMIC_INFO_BOOT_HART_OFFSET(a2)
 	REG_S	a3, (a4)
 2:
+	REG_L	a1, (a4)
 	ret
 
 	.section .entry, "ax", %progbits
include/sbi/fw_dynamic.h +8 -0
index 45c4d5f..94e30e1 100644
@@ -28,7 +28,15 @@
 #define FW_DYNAMIC_INFO_BOOT_HART_OFFSET	(5 * __SIZEOF_LONG__)
 
 /** Expected value of info magic ('OSBI' ascii string in hex) */
+#ifdef CONFIG_OPENSBI_BE
+#if __riscv_xlen == 64
+#define FW_DYNAMIC_INFO_MAGIC_VALUE		0x4f53424900000000
+#else
+#define FW_DYNAMIC_INFO_MAGIC_VALUE		0x4f534249
+#endif
+#else
 #define FW_DYNAMIC_INFO_MAGIC_VALUE		0x4942534f
+#endif
 
 /** Maximum supported info version */
 #define FW_DYNAMIC_INFO_VERSION_2		0x2
include/sbi/riscv_asm.h +35 -0
index 4605db2..434b586 100644
@@ -37,6 +37,41 @@
 #define SZREG		__REG_SEL(8, 4)
 #define LGREG		__REG_SEL(3, 2)
 
+#ifdef  __ASSEMBLER__
+#ifdef CONFIG_OPENSBI_BE
+.macro REG_L_HELPER rtmp, rto, shift, offset
+	lbu	\rtmp, \offset
+	slli	\rtmp, \rtmp, \shift
+	or	\rto, \rto, \rtmp
+.endm
+#if __riscv_xlen == 64
+.macro  REG_L_LE  rtmp, rto, offset
+	lbu	\rto, 0 + \offset
+	REG_L_HELPER \rtmp, \rto, 8, 1 + \offset
+	REG_L_HELPER \rtmp, \rto, 16, 2 + \offset
+	REG_L_HELPER \rtmp, \rto, 24, 3 + \offset
+	REG_L_HELPER \rtmp, \rto, 32, 4 + \offset
+	REG_L_HELPER \rtmp, \rto, 40, 5 + \offset
+	REG_L_HELPER \rtmp, \rto, 48, 6 + \offset
+	REG_L_HELPER \rtmp, \rto, 56, 7 + \offset
+.endm
+#elif __riscv_xlen == 32
+.macro  REG_L_LE  rtmp, rto, offset
+	lbu	\rto, 0 + \offset
+	REG_L_HELPER \rtmp, \rto, 8, 1 + \offset
+	REG_L_HELPER \rtmp, \rto, 16, 2 + \offset
+	REG_L_HELPER \rtmp, \rto, 24, 3 + \offset
+.endm
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+#else
+.macro  REG_L_LE  rtmp, rto, offset
+  REG_L \rto, \offset
+.endm
+#endif
+#endif
+
 #if __SIZEOF_POINTER__ == 8
 #ifdef __ASSEMBLER__
 #define RISCV_PTR		.dword
Signature · 1 line
2.37.2.352.g3c44437643
author
#12 lore raw

RFC 7/7 opensbi: add u/s big endian init option

In reply to Ben Dooks

Add an option to se the UBE and SBE flags when the hart is initialised to set them into big endian S and U modes.

---
 Kconfig            | 4 ++++
 lib/sbi/sbi_hart.c | 4 ++++
 2 files changed, 8 insertions(+)
Kconfig +4 -0
index 6c6236f..6fccacf 100644
@@ -11,6 +11,10 @@ config OPENSBI_BE_SET
 	bool "Set M-Mode to be big-endian in startup code"
 	depends on OPENSBI_BE
 
+config OPENSBI_SUBE_SET
+	bool "Set S and U mode to be big-endian in startup code"
+	depends on OPENSBI_BE
+
 config OPENSBI_SRC_DIR
 	string
 	option env="OPENSBI_SRC_DIR"
lib/sbi/sbi_hart.c +4 -0
index 39311c4..9bd1cfd 100644
@@ -42,6 +42,10 @@ static void mstatus_init(struct sbi_scratch *scratch)
 #ifdef CONFIG_OPENSBI_BE_SET
 	mstatus_val |= MSTATUS_MBE;
 #endif
+#ifdef CONFIG_OPENSBI_SUBE_SET
+	mstatus_val |= MSTATUS_SBE;
+	mstatus_val |= MSTATUS_UBE;
+#endif
 
 	/* Enable FPU */
 	if (misa_extension('D') || misa_extension('F'))
Signature · 1 line
2.37.2.352.g3c44437643