Skip to contentSkip to conversation

riscv: Add Zbb support for building U-Boot

PATCH In review Yu Chien Peter Lin 3 participants 4 messages Aug 9, 2023 – Oct 19, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Add Zbb support for building U-Boot

This patch adds ISA string to the -march to generate zbb instructions for U-Boot binaries, along with optimized string functions introduced from Linux kernel.

---
 arch/riscv/Kconfig              |  92 +++++++++++++++++++++++++++++
 arch/riscv/Makefile             |   5 +-
 arch/riscv/include/asm/string.h |  18 ++++++
 arch/riscv/lib/Makefile         |   3 +
 arch/riscv/lib/strcmp_zbb.S     |  81 +++++++++++++++++++++++++
 arch/riscv/lib/strlen_zbb.S     | 101 ++++++++++++++++++++++++++++++++
 arch/riscv/lib/strncmp_zbb.S    |  94 +++++++++++++++++++++++++++++
 7 files changed, 393 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/lib/strcmp_zbb.S
 create mode 100644 arch/riscv/lib/strlen_zbb.S
 create mode 100644 arch/riscv/lib/strncmp_zbb.S
arch/riscv/Kconfig +92 -0
index 867cbcbe74..49dd714e99 100644
@@ -177,6 +177,98 @@ config RISCV_ISA_D
 	  riscv32 ABI from ilp32 to ilp32d and the riscv64 ABI from lp64 to
 	  lp64d.
 
+config RISCV_ISA_ZBB
+	bool "Zbb extension support for bit manipulation instructions"
+	default n
+	help
+	  Adds ZBB extension (basic bit manipulation) to the ISA subsets
+	  that the toolchain is allowed to emit when building U-Boot.
+	  The Zbb extension provides instructions to accelerate a number
+	  of bit-specific operations (count bit population, sign extending,
+	  bitrotation, etc) and enables optimized string routines.
+
+menu "Use assembly optimized implementation of string routines"
+
+config USE_ARCH_STRLEN
+	bool "Use an assembly optimized implementation of strlen"
+	default y
+	depends on RISCV_ISA_ZBB
+	help
+	  Enable the generation of an optimized version of strlen using
+	  Zbb extension.
+
+config SPL_USE_ARCH_STRLEN
+	bool "Use an assembly optimized implementation of strlen for SPL"
+	default y if USE_ARCH_STRLEN
+	depends on RISCV_ISA_ZBB
+	depends on SPL
+	help
+	  Enable the generation of an optimized version of strlen using
+	  Zbb extension.
+
+config TPL_USE_ARCH_STRLEN
+	bool "Use an assembly optimized implementation of strlen for TPL"
+	default y if USE_ARCH_STRLEN
+	depends on RISCV_ISA_ZBB
+	depends on TPL
+	help
+	  Enable the generation of an optimized version of strlen using
+	  Zbb extension.
+
+config USE_ARCH_STRCMP
+	bool "Use an assembly optimized implementation of strcmp"
+	default y
+	depends on RISCV_ISA_ZBB
+	help
+	  Enable the generation of an optimized version of strcmp using
+	  Zbb extension.
+
+config SPL_USE_ARCH_STRCMP
+	bool "Use an assembly optimized implementation of strcmp for SPL"
+	default y if USE_ARCH_STRCMP
+	depends on RISCV_ISA_ZBB
+	depends on SPL
+	help
+	  Enable the generation of an optimized version of strcmp using
+	  Zbb extension.
+
+config TPL_USE_ARCH_STRCMP
+	bool "Use an assembly optimized implementation of strcmp for TPL"
+	default y if USE_ARCH_STRCMP
+	depends on RISCV_ISA_ZBB
+	depends on TPL
+	help
+	  Enable the generation of an optimized version of strcmp using
+	  Zbb extension.
+
+config USE_ARCH_STRNCMP
+	bool "Use an assembly optimized implementation of strncmp"
+	default y
+	depends on RISCV_ISA_ZBB
+	help
+	  Enable the generation of an optimized version of strncmp using
+	  Zbb extension.
+
+config SPL_USE_ARCH_STRNCMP
+	bool "Use an assembly optimized implementation of strncmp for SPL"
+	default y if USE_ARCH_STRNCMP
+	depends on RISCV_ISA_ZBB
+	depends on SPL
+	help
+	  Enable the generation of an optimized version of strncmp using
+	  Zbb extension.
+
+config TPL_USE_ARCH_STRNCMP
+	bool "Use an assembly optimized implementation of strncmp for TPL"
+	default y if USE_ARCH_STRNCMP
+	depends on RISCV_ISA_ZBB
+	depends on TPL
+	help
+	  Enable the generation of an optimized version of strncmp using
+	  Zbb extension.
+
+endmenu
+
 config RISCV_ISA_A
 	def_bool y
 
arch/riscv/Makefile +4 -1
index 4963b5109b..b3ef87078b 100644
@@ -24,6 +24,9 @@ endif
 ifeq ($(CONFIG_RISCV_ISA_C),y)
 	ARCH_C = c
 endif
+ifeq ($(CONFIG_RISCV_ISA_ZBB),y)
+	ARCH_ZBB = _zbb
+endif
 ifeq ($(CONFIG_CMODEL_MEDLOW),y)
 	CMODEL = medlow
 endif
@@ -32,7 +35,7 @@ ifeq ($(CONFIG_CMODEL_MEDANY),y)
 endif
 
 
-RISCV_MARCH = $(ARCH_BASE)$(ARCH_A)$(ARCH_F)$(ARCH_D)$(ARCH_C)
+RISCV_MARCH = $(ARCH_BASE)$(ARCH_A)$(ARCH_F)$(ARCH_D)$(ARCH_C)$(ARCH_ZBB)
 ABI = $(ABI_BASE)$(ABI_D)
 
 # Newer binutils versions default to ISA spec version 20191213 which moves some
arch/riscv/include/asm/string.h +18 -0
index 7dee3e4c9f..38ad85fe3b 100644
@@ -40,4 +40,22 @@ extern void *memmove(void *, const void *, __kernel_size_t);
 #endif
 extern void *memset(void *, int, __kernel_size_t);
 
+#undef __HAVE_ARCH_STRLEN
+#if CONFIG_IS_ENABLED(USE_ARCH_STRLEN)
+#define __HAVE_ARCH_STRLEN
+#endif
+extern __kernel_size_t strlen(const char *);
+
+#undef __HAVE_ARCH_STRCMP
+#if CONFIG_IS_ENABLED(USE_ARCH_STRCMP)
+#define __HAVE_ARCH_STRCMP
+#endif
+extern int strcmp(const char *, const char *);
+
+#undef __HAVE_ARCH_STRNCMP
+#if CONFIG_IS_ENABLED(USE_ARCH_STRNCMP)
+#define __HAVE_ARCH_STRNCMP
+#endif
+extern int strncmp(const char *, const char *, size_t __kernel_size_t);
+
 #endif /* __ASM_RISCV_STRING_H */
arch/riscv/lib/Makefile +3 -0
index 02c4d8fcc6..9a05b662fd 100644
@@ -42,5 +42,8 @@ extra-$(CONFIG_EFI) += $(EFI_CRT0) $(EFI_RELOC)
 obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMSET) += memset.o
 obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMMOVE) += memmove.o
 obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMCPY) += memcpy.o
+obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_STRLEN) += strlen_zbb.o
+obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_STRCMP) += strcmp_zbb.o
+obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_STRNCMP) += strncmp_zbb.o
 
 obj-$(CONFIG_$(SPL_TPL_)SEMIHOSTING) += semihosting.o
arch/riscv/lib/strcmp_zbb.S new file +81 -0

81 lines not on this page · raw message

arch/riscv/lib/strlen_zbb.S new file +101 -0

101 lines not on this page · raw message

arch/riscv/lib/strncmp_zbb.S new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Yu Chien Peter Lin

This patch adds ISA string to the -march to generate zbb instructions for U-Boot binaries, along with optimized string functions introduced from Linux kernel.

Quoted 12 lines
---
 arch/riscv/Kconfig              |  92 +++++++++++++++++++++++++++++
 arch/riscv/Makefile             |   5 +-
 arch/riscv/include/asm/string.h |  18 ++++++
 arch/riscv/lib/Makefile         |   3 +
 arch/riscv/lib/strcmp_zbb.S     |  81 +++++++++++++++++++++++++
 arch/riscv/lib/strlen_zbb.S     | 101 ++++++++++++++++++++++++++++++++
 arch/riscv/lib/strncmp_zbb.S    |  94 +++++++++++++++++++++++++++++
 7 files changed, 393 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/lib/strcmp_zbb.S
 create mode 100644 arch/riscv/lib/strlen_zbb.S
 create mode 100644 arch/riscv/lib/strncmp_zbb.S
#2 lore raw
In reply to Yu Chien Peter Lin
Quoted 5 lines · Yu Chien Peter Lin

This patch adds ISA string to the -march to generate zbb instructions for U-Boot binaries, along with optimized string functions introduced from Linux kernel.

[snip]

arch/riscv/Kconfig config RISCV_ISA_D · line 182

Quoted 3 lines
arch/riscv/Kconfig +3 -0
+config RISCV_ISA_ZBB
+	bool "Zbb extension support for bit manipulation instructions"
+	default n

The default is 'n' so we don't need this line. This can be fixed in-line while applying if there's no other feedback, thanks.

--
Tom

#3 lore raw
Quoted 6 lines · Tom Rini
Quoted 4 lines · Yu Chien Peter Lin

This patch adds ISA string to the -march to generate zbb instructions for U-Boot binaries, along with optimized string functions introduced from Linux kernel.

Quoted 9 lines
Quoted 1 line

[snip]

arch/riscv/Kconfig config RISCV_ISA_D · line 182

Quoted 3 lines
arch/riscv/Kconfig +3 -0
+config RISCV_ISA_ZBB
+	bool "Zbb extension support for bit manipulation instructions"
+	default n

The default is 'n' so we don't need this line. This can be fixed in-line while applying if there's no other feedback, thanks.

Hi Tom,

Got it! Thanks for the heads-up!

Best regards,
Leo

Quoted 2 lines

--
Tom