Switch RISC-V to declarative entry points
Subsystems (8)
cover Switch RISC-V to declarative entry points
Every board currently open-codes its PBL entry point: set up the stack, relocate, call setup_c(), do the lowlevel init and so on, all in C code that runs before the C environment is ready.
This is error-prone, because board code authors need to be aware of some very subtle details: global_variable_offset vs get_runtime_offset, not accessing global variables before relocation, using noinline functions as compiler barriers, no spilling to stack when ENTRY_FUNCTION is used with no stack set up...
For the last point, we already had a solution in the form of ENTRY_FUNCTION_WITHSTACK, which takes a stack pointer and places it into a well-known location that a generic assembly entry point can read and use to set up the stack before branching into C code.
That approach is not generic enough however: It only accepts a single stack pointer when we really want one per execution level and even on ARM32, it's still implemented using __naked, which precludes use with clang as clang doesn't allow arbitrary C code there.
Address all of this by generalizing the idea behind ENTRY_FUNCTION_WITHSTACK into a new ENTRY_POINT macro:
Instead of a board having one board-specific entry point function and a single stack, it now has a struct of different callbacks and metadata:
struct pbl_stage {
unsigned long stacktop; /* stack top or PBL_STACKTOP_* */
unsigned long relocaddr; /* 0: don't relocate to specific addr */
/*
* Memory the stage runs from: barebox proper starts out with
* mem_minsz, mem_maxsz (default: mem_minsz) is only used to find
* the stage by PC.
*/
unsigned long mem_start;
unsigned long mem_minsz;
unsigned long mem_maxsz;
void *board_data;
pbl_board_fn_t prereloc_cpu_init;
pbl_board_fn_t soc_init;
void (*board_init)(unsigned long entry_data);
void (*soc_chainload)(void *board_data);
};struct pbl_board_info {
unsigned int soc_type; /* see pbl_set_soc_type() */
/* Called without a stack, so it must not spill */ pbl_locate_pc_fn_t assembly_locate_pc;
struct pbl_stage rom_stage; /* PBL_STAGE_ROM */
struct pbl_stage first_stage; /* PBL_STAGE_OCRAM */
struct pbl_stage second_stage; /* PBL_STAGE_DRAM */
};
A board can now fill this out, define its own callbacks. Common functionality like the soc_chainload can be shared between boards of the same SoC. The hairy parts that can not be robustly done in C can be defined declaratively as data that will be acted upon by the assembly code. Code that must be written in assembly or run pre-relocation is explicitly marked as such.
This can get quite verbose, so the idea is that SoC support will define a macro with DEFAULTS that fills out the common bits that can be extended (and overridden if needed) by the board code. See the last few patches for how this looks like.
To make discussion easier, I am switching over all of RISC-V first in this series. If we reach consensus and this is merged, I will follow-up by doing the same on ARM.
This applies on top of next (PR #93 is prerequisite)
Ahmad Fatoum (17):
asm-generic: add registers.h with a pc_inside() helper
RISC-V: add call_with_stack() helper
RISC-V: implement relocate_to_adr_full()
PBL: add SoC type declaration and handoff support
PBL: introduce board info based entry point framework
PBL: describe the console in the board info and report the stage
RISC-V: make barebox_riscv_entry() non-naked
RISC-V: add PBL heads for the board info stage dispatch
RISC-V: board-dt-2nd: migrate to board info entry point
RISC-V: hifive: migrate to board info entry point
RISC-V: allow relocating the PBL more than once
RISC-V: nmon: preserve s1-s3 for the caller
RISC-V: beaglev: migrate to board info entry point
RISC-V: allwinner-d1: migrate to board info entry point
RISC-V: erizo: migrate to board info entry point
RISC-V: litex-linux: migrate to board info entry point
Documentation: porting: describe the board info entry point
Documentation/devel/porting.rst | 124 +++++++++++- Documentation/devel/troubleshooting.rst | 6 + arch/Kconfig | 28 +++ arch/riscv/Kconfig | 2 + arch/riscv/boards/allwinner-d1/lowlevel.c | 18 +- arch/riscv/boards/beaglev/lowlevel.c | 30 ++- arch/riscv/boards/erizo/lowlevel.c | 32 +-- arch/riscv/boards/hifive/lowlevel.c | 30 +-- arch/riscv/boards/litex-linux/lowlevel.c | 30 +-- arch/riscv/boot/Makefile | 4 +- arch/riscv/boot/board-dt-2nd-head.S | 36 ++++ arch/riscv/boot/board-dt-2nd.c | 89 ++++----- arch/riscv/boot/entry.c | 33 +--- arch/riscv/boot/entry_ll.S | 15 ++ arch/riscv/boot/head.S | 198 +++++++++++++++++++ arch/riscv/boot/nmon_entry.S | 7 + arch/riscv/include/asm/barebox-riscv.h | 36 +++- arch/riscv/include/asm/reloc.h | 1 + arch/riscv/lib/Makefile | 1 + arch/riscv/lib/asm-offsets.c | 27 +++ arch/riscv/lib/call_with_stack.S | 12 ++ arch/riscv/lib/pbl.lds.S | 13 +- arch/riscv/lib/reloc.c | 6 +- arch/riscv/lib/setupc.S | 16 +- include/asm-generic/registers.h | 33 ++++ include/linux/compiler-clang.h | 21 ++ include/linux/compiler-gcc.h | 3 + include/linux/compiler_types.h | 10 + include/mach/sifive/entry.h | 32 +++ include/pbl.h | 6 + include/pbl/entry.h | 175 ++++++++++++++++ include/pbl/handoff-data.h | 15 ++ include/pbl/soc-type.h | 170 ++++++++++++++++ pbl/Makefile | 2 + pbl/entry.c | 230 ++++++++++++++++++++++ pbl/handoff-data.c | 2 + pbl/soc-type.c | 71 +++++++ 37 files changed, 1418 insertions(+), 146 deletions(-) create mode 100644 arch/riscv/boot/board-dt-2nd-head.S create mode 100644 arch/riscv/boot/entry_ll.S create mode 100644 arch/riscv/boot/head.S create mode 100644 arch/riscv/lib/call_with_stack.S create mode 100644 include/asm-generic/registers.h create mode 100644 include/mach/sifive/entry.h create mode 100644 include/pbl/entry.h create mode 100644 include/pbl/soc-type.h create mode 100644 pbl/entry.c create mode 100644 pbl/soc-type.c
Signature · 1 line
2.47.3
RFC PATCH 1/17 asm-generic: add registers.h with a pc_inside() helper
The upcoming board info stage dispatch determines the stage it runs in by checking which stage's memory region covers the program counter.
Add asm-generic/registers.h with pc_inside(), built on the get_pc() that ARM, RISC-V and PowerPC provide, as the home for such register level helpers.
--- include/asm-generic/registers.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 include/asm-generic/registers.h
include/asm-generic/registers.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* SPDX-FileCopyrightText: 2026 Ahmad Fatoum */ + +#ifndef __ASM_GENERIC_REGISTERS_H +#define __ASM_GENERIC_REGISTERS_H + +#include <linux/compiler.h> +#include <asm/common.h> + +/* +* pc_inside - check if the program counter is in [start, start + size) +* +* A single unsigned compare, so it also works for a region ending at the +* top of the address space. +*/ +static __always_inline bool pc_inside(unsigned long start, unsigned long size) +{ +unsigned long pc =get_pc (); + +return pc - start < size; +} + +#endif
Signature · 1 line
2.47.3
RFC PATCH 2/17 RISC-V: add call_with_stack() helper
In reply to Ahmad FatoumSwitching stacks in C requires inline assembly inside a naked function and even then, the compiler is free to keep using the old stack pointer.
Let's add call_with_stack() instead, which takes the new stack top, a function and two arguments for it. Everything is passed in registers, so it's safe to call before a stack has been set up, as the upcoming PBL stage dispatch will do.
The prototype goes into asm-generic/registers.h, as other architectures will implement the same helper.
--- arch/riscv/lib/Makefile | 1 + arch/riscv/lib/call_with_stack.S | 12 ++++++++++++ include/asm-generic/registers.h | 10 ++++++++++ 3 files changed, 23 insertions(+) create mode 100644 arch/riscv/lib/call_with_stack.S
arch/riscv/lib/Makefile +1 -0
@@ -4,6 +4,7 @@ extra-y += barebox.lds obj-y += dtb.o obj-pbl-y += sections.o setupc.o reloc.o sections.o runtime-offset.o +obj-pbl-y += call_with_stack.o obj-$(CONFIG_ARCH_HAS_SJLJ) += setjmp.o longjmp.o obj-$(CONFIG_RISCV_OPTIMZED_STRING_FUNCTIONS) += memcpy.o memset.o memmove.o obj-$(CONFIG_RISCV_SBI) += sbi.o
arch/riscv/lib/call_with_stack.S new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <linux/linkage.h> + +.section .text.call_with_stack +ENTRY(call_with_stack) + mv t1, a1 + mv sp, a0 + mv a0, a2 + mv a1, a3 + jr t1 +ENDPROC(call_with_stack)
include/asm-generic/registers.h +10 -0
@@ -20,4 +20,14 @@ static __always_inline bool pc_inside(unsigned long start, unsigned long size) return pc - start < size; } +/* +* call_with_stack - switch to the stack ending at stack_top and call fn +* +* stack_top must be aligned as the architecture requires and fn must not +* return. No memory is accessed, so there needn't be a stack yet. +*/ +void __noreturn call_with_stack (unsigned long stack_top, +void (*fn)(unsigned long ,unsigned long ), +unsigned long arg0,unsigned long arg1); +#endif
Signature · 1 line
2.47.3
RFC PATCH 3/17 RISC-V: implement relocate_to_adr_full()
In reply to Ahmad FatoumIn preparation for the new common PBL entry code that will make use of relocate_to_adr_full(), implement it for RISC-V as on ARM.
Unlike relocate_to_adr(), which stops copying at __bss_start, the _full variant copies up to __image_end, so barebox proper can still be decompressed after the move.
--- arch/riscv/include/asm/reloc.h | 1 + arch/riscv/lib/setupc.S | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-)
arch/riscv/include/asm/reloc.h +1 -0
@@ -7,6 +7,7 @@ unsigned long get_runtime_offset(void);void relocate_to_current_adr (void );void relocate_to_adr (unsigned long target); +void relocate_to_adr_full (unsigned long target);void setup_c (void );
arch/riscv/lib/setupc.S +12 -4
@@ -39,13 +39,22 @@ bss_cleared: /* * void relocate_to_adr(unsigned long targetadr) + * void relocate_to_adr_full(unsigned long targetadr) * * Copy binary to targetadr, relocate code and continue - * executing at new address. + * executing at new address. The _full variant takes the piggy data + * along, so barebox proper can still be decompressed afterwards. */ .section .text.relocate_to_adr -ENTRY(relocate_to_adr) /* a0: target address */ +ENTRY(relocate_to_adr_full) + lla a3, __image_end /* include piggy data */ + j 1f +ENDPROC(relocate_to_adr_full) + +ENTRY(relocate_to_adr) + lla a3, __bss_start /* skip piggy data */ +1: addi sp, sp, -SZREG * 2 lla a1, _text /* a1: source address */
@@ -61,8 +70,7 @@ ENTRY(relocate_to_adr) add a2, a2, a0 REG_S a2, (sp) - lla a2, __bss_start - sub a2, a2, a1 /* a2: size */ + sub a2, a3, a1 /* a2: size */ jal __memcpy
Signature · 1 line
2.47.3
RFC PATCH 4/17 PBL: add SoC type declaration and handoff support
In reply to Ahmad FatoumA PBL often supports multiple SoC variants, but barebox proper has no cheap way to find out which one it's running on before the device tree is available.
Let's add an enum soc_type, which the upcoming entry point framework will declare per entry point and hand off to barebox proper, where barebox_soc_type() returns it. Images not built for a particular SoC, like barebox-dt-2nd.img, use the SOC_TYPE_GENERIC_DT/EFI pseudo types.
A PBL that only detects the SoC at runtime can still update the type with pbl_set_soc_type() and barebox proper complains if an entry point forgot to set it.
--- include/pbl/handoff-data.h | 15 ++++ include/pbl/soc-type.h | 170 +++++++++++++++++++++++++++++++++++++ pbl/Makefile | 1 + pbl/handoff-data.c | 2 + pbl/soc-type.c | 71 ++++++++++++++++ 5 files changed, 259 insertions(+) create mode 100644 include/pbl/soc-type.h create mode 100644 pbl/soc-type.c
include/pbl/handoff-data.h +15 -0
@@ -15,6 +15,7 @@ struct handoff_data {#define HANDOFF_DATA_ARM_MACHINE HANDOFF_DATA_BAREBOX(3)#define HANDOFF_DATA_EFI HANDOFF_DATA_BAREBOX(4)#define HANDOFF_DATA_TEE_DT_OVL HANDOFF_DATA_BAREBOX(5) +#define HANDOFF_DATA_SOC_TYPE HANDOFF_DATA_BAREBOX(6)#define HANDOFF_DATA_BOARD (n) (0x951726fb + (n))
@@ -66,4 +67,18 @@ static inline void *handoff_data_get_entry(unsigned int cookie, size_t *size)#endif void handoff_data_add_dt (void *fdt); +#ifdef __PBL__ +/* +* The stage dispatch hands off the type declared in the board info. An +* image running on more than one SoC may still override it later. +*/ +void pbl_set_soc_type (unsigned int type); +unsigned int pbl_get_soc_type (void ); +void pbl_handoff_soc_type (void ); +#elif defined(CONFIG_PBL_IMAGE ) +unsigned int barebox_soc_type (void ); +#else +static inline unsigned int barebox_soc_type (void ) {return 0 ; } +#endif +#endif /* __PBL_HANDOFF_DATA_H */
include/pbl/soc-type.h new file +170 -0
170 lines not on this page · raw message
pbl/Makefile +1 -0
@@ -13,5 +13,6 @@ pbl-$(CONFIG_LIBFDT) += fdt.o pbl-$(CONFIG_PBL_CONSOLE) += console.o obj-pbl-y += handoff-data.o obj-pbl-$(CONFIG_PBL_IMAGE_ELF) += layout.o +obj-pbl-y += soc-type.o obj-pbl-$(CONFIG_MMU) += mmu.o obj-$(CONFIG_PBL_IMAGE_ELF) += barebox-proper.o
pbl/handoff-data.c +2 -0
@@ -205,6 +205,8 @@ static const char *handoff_data_entry_name(struct handoff_data_entry *hde)return "ARM machine number" ;case HANDOFF_DATA_TEE_DT_OVL :return "handoff TEE FDTO" ; +case HANDOFF_DATA_SOC_TYPE : +return "handoff SoC type" ;default :sprintf (name,"handoff %08x" , hde->cookie);return name;
pbl/soc-type.c new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.47.3
RFC PATCH 5/17 PBL: introduce board info based entry point framework
In reply to Ahmad FatoumEvery board open-codes its PBL entry point: set up the stack, relocate, call setup_c(), do the lowlevel init and so on, all in code that runs before the C environment is ready. This is easy to get wrong and leaves common code with no idea of what the boot flow looks like.
Let's have boards describe it instead: struct pbl_board_info has up to three stages (ROM, OCRAM, DRAM), each with a memory region, a stack, an optional relocation address and callbacks for CPU, SoC and board init and for chainloading. ENTRY_POINT() defines it with the image head and defaults of the given SoC, which the board may override, hence the __diag_ignore_all() imported from Linux.
The image head branches to pbl_head_start_v1(), which determines the stage from the PC, sets up its stack and runs the CPU init callback, before relocating, clearing BSS and running the remaining callbacks.
As this runs before there is a stack, which C can't guarantee, architectures are meant to implement pbl_head_start_v1() in assembly and select ARCH_HAS_PBL_HEAD_START_ASM. The C implementation added here is weak and serves as reference. With COMPILE_TEST, PBL_HEAD_START_ASM can be disabled to test it. It's naked where the compiler allows C in naked functions, which GCC does for ARM32 and RISC-V, but clang doesn't.
Everything is versioned, so we can add a v2 alongside should the scheme ever need to change incompatibly.
--- arch/Kconfig | 28 ++++++ include/linux/compiler-clang.h | 21 +++++ include/linux/compiler-gcc.h | 3 + include/linux/compiler_types.h | 10 ++ include/pbl/entry.h | 158 +++++++++++++++++++++++++++++++ pbl/Makefile | 1 + pbl/entry.c | 165 +++++++++++++++++++++++++++++++++ 7 files changed, 386 insertions(+) create mode 100644 include/pbl/entry.h create mode 100644 pbl/entry.c
arch/Kconfig +28 -0
@@ -95,6 +95,34 @@ config ARCH_HAS_DATA_ABORT_MASK_PBL config ARCH_HAS_ZERO_PAGE bool +config ARCH_HAS_NAKED + def_bool y + depends on (ARM && CPU_32) || RISCV + # clang allows only basic asm statements in naked functions + depends on !CC_IS_CLANG + help + The compiler supports C in naked functions: it emits neither + prologue nor epilogue, but may still spill to the stack. + +config ARCH_HAS_PBL_ENTRY + bool + help + The architecture has PBL image heads for ENTRY_POINT() boards. + +config ARCH_HAS_PBL_HEAD_START_ASM + bool + help + The architecture implements pbl_head_start_v1() in assembly. + +config PBL_HEAD_START_ASM + bool "Use the assembly PBL stage dispatch" if COMPILE_TEST + depends on ARCH_HAS_PBL_HEAD_START_ASM + default y + help + Say N to use the C pbl_head_start_v1() in pbl/entry.c instead, + which may spill to the stack before it is set up. Only useful + for testing. + config ARCH_HAS_MALLOC_SIZE bool help
include/linux/compiler-clang.h +21 -0
@@ -55,3 +55,24 @@#define __must_be_array (a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))#define __assume_aligned (a, ...) \ __attribute__((__assume_aligned__(a, ## __VA_ARGS__))) + +/* +* Turn individual warnings and errors on and off locally, depending +* on version. +*/ +#define __diag_str1 (s) #s +#define __diag_str (s) __diag_str1(s) +#define __diag (s) _Pragma(__diag_str(clang diagnostic s)) + +#define __diag_clang (version, severity, s) \ + __diag_clang_ ## version(__diag_clang_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_clang_ignore ignored +#define __diag_clang_warn warning +#define __diag_clang_error error + +#define __diag_clang_13 (s) __diag(s) + +#define __diag_ignore_all (option, comment) \ + __diag_clang(13, ignore, option)
include/linux/compiler-gcc.h +3 -0
@@ -235,6 +235,9 @@#define __diag_GCC_8 (s)#endif +#define __diag_ignore_all (option, comment) \ + __diag(__diag_GCC_ignore option) +/* * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size" * attribute) do not work, and must be disabled.
include/linux/compiler_types.h +10 -0
@@ -313,6 +313,10 @@ struct ftrace_likely_data {#define __diag_GCC (version, severity, string)#endif +#ifndef __diag_ignore_all +#define __diag_ignore_all (option, comment) +#endif +#define __diag_push () __diag(push)#define __diag_pop () __diag(pop)
@@ -452,4 +456,10 @@ struct ftrace_likely_data {#define __prereloc \ notrace __no_sanitize_address __no_stack_protector +/* +* Marks functions that may run before the stack is set up and thus must +* not spill. Documentation only, nothing enforces it. +*/ +#define __unsafe_prestack +#endif /* __LINUX_COMPILER_TYPES_H */
include/pbl/entry.h new file +158 -0
158 lines not on this page · raw message
pbl/Makefile +1 -0
@@ -4,6 +4,7 @@ # only unsed by the pbl # pbl-$(CONFIG_PBL_CONSTRUCTORS) += ctors.o +pbl-$(CONFIG_ARCH_HAS_PBL_ENTRY) += entry.o pbl-y += misc.o pbl-y += string.o pbl-y += malloc.o
pbl/entry.c new file +165 -0
165 lines not on this page · raw message
Signature · 1 line
2.47.3
RFC PATCH 6/17 PBL: describe the console in the board info and report the stage
In reply to Ahmad FatoumBoard entry points usually set up the console UART first thing and print a '>' with putc_ll() to show that they have been reached.
Let's make the console part of the board info instead and have the stage dispatch print the stage number followed by '>', e.g. "2>", once it's up. As the UART may need clocks enabled by SoC init or a description only filled in by board init, the console is tried before SoC init and again after each of the two.
Without PBL console support or a console description, the stage number goes to DEBUG_LL. With DEBUG, each callback run is reported as well.
--- Documentation/devel/troubleshooting.rst | 6 +++ include/pbl.h | 6 +++ include/pbl/entry.h | 19 ++++++- pbl/entry.c | 67 ++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 2 deletions(-)
Documentation/devel/troubleshooting.rst +6 -0
@@ -62,6 +62,12 @@ options: the serial console needs to be done in software in some situations before the UART is accessible from the outside. + Boards using the board info entry point (``ENTRY_POINT()``) will + also indicate the boot stage before the ``>`` character, e.g. ``1>2>`` + means the prebootloader entered its 1st stage followed by entering the + 2nd stage. ``CONFIG_DEBUG_PBL`` would provide more information on what + callback is being executed. + .. note:: Make sure the correct UART index or address is selected under **Kernel low-level debugging port** in ``menuconfig``.
include/pbl.h +6 -0
@@ -57,4 +57,10 @@ void __noreturn barebox_pbl_entry(ulong, ulong, void *);#define pbl_puts puts_ll#endif +#ifdef DEBUG +#define pbl_debug pbl_puts +#else +#define pbl_debug (void) +#endif +#endif /* __PBL_H__ */
include/pbl/entry.h +18 -1
@@ -26,12 +26,27 @@ enum pbl_stage_idx {typedef void (*pbl_board_fn_t )(void );typedef enum pbl_stage_idx (*pbl_locate_pc_fn_t )(void ); +struct pbl_console_driver { +void (*putc)(void *arg,int c);/* as accepted by pbl_set_putc() */ +}; + +/* +* arg is usually the UART base address. A console without setup callback +* is registered before soc_init, else after. Without a console, the PBL +* prints through DEBUG_LL. +*/ +struct pbl_console { +void (*setup)(void *arg); +void *arg; +const struct pbl_console_driver *driver; +}; +/* * Only the callbacks of the stage the PBL was entered in are run. * * prereloc_cpu_init runs on the stage's stack, but unrelocated and before * BSS is cleared, so it must be __prereloc and may not use globals. -* The other callbacks run relocated with BSS cleared. +* The console setup and the other callbacks run relocated with BSS cleared. */ struct pbl_stage {unsigned long stacktop;/* stack top or PBL_STACKTOP_* */
@@ -60,6 +75,8 @@ struct pbl_board_info {/* Called without a stack, so it must not spill */ pbl_locate_pc_fn_t assembly_locate_pc; +struct pbl_console console; +struct pbl_stage rom_stage;/* PBL_STAGE_ROM */ struct pbl_stage first_stage;/* PBL_STAGE_OCRAM */ struct pbl_stage second_stage;/* PBL_STAGE_DRAM */
pbl/entry.c +66 -1
91 lines not on this page · raw message
Signature · 1 line
2.47.3
RFC PATCH 7/17 RISC-V: make barebox_riscv_entry() non-naked
In reply to Ahmad Fatoumbarebox_riscv_entry() is a naked function that switches the stack with inline assembly and then continues in C. GCC only supports basic asm in naked functions and clang rejects anything else outright.
Let's move the stack switch into an assembly __barebox_riscv_entry() like on ARM. barebox_riscv_entry() becomes an always inlined function, so the naked ENTRY_FUNCTION()s, which may run without a stack, can still call it.
--- arch/riscv/boot/Makefile | 2 +- arch/riscv/boot/entry.c | 31 -------------------------- arch/riscv/boot/entry_ll.S | 15 +++++++++++++ arch/riscv/include/asm/barebox-riscv.h | 30 ++++++++++++++++++++++--- 4 files changed, 43 insertions(+), 35 deletions(-) delete mode 100644 arch/riscv/boot/entry.c create mode 100644 arch/riscv/boot/entry_ll.S
arch/riscv/boot/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 -pbl-y += entry.o uncompress.o +pbl-y += entry_ll.o uncompress.o pbl-y += nmon_entry.o pbl-$(CONFIG_BOARD_GENERIC_DT) += board-dt-2nd.o
arch/riscv/boot/entry.c deleted +0 -31
@@ -1,31 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#include <types.h> - -#include <asm/barebox-riscv.h> - -#include "entry.h" -#include <debug_ll.h> - -/* -* Main RISC-V entry point. Call this with the memory region you can -* spare for barebox. This doesn't necessarily have to be the full -* SDRAM. The currently running binary can be inside or outside of -* this region. The PBL can be running inside or outside of this -* region. -* -* -> membase + memsize -* STACK_SIZE - stack -* 128KiB - early memory space -* -> maximum end of barebox binary -*/ - -void __noreturn __naked barebox_riscv_entry (unsigned long membase, -unsigned long memsize,void *boarddata, -unsigned flags) -{ -unsigned long stack_top =riscv_mem_stack_top (membase, membase + memsize); - asmvolatile ("move sp, %0" : :"r" (stack_top)); -riscv_set_flags (flags); -barebox_pbl_start (membase, memsize, boarddata); -} -
arch/riscv/boot/entry_ll.S new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <linux/linkage.h> + +/* + * a0: memory base + * a1: memory size + * a2: board data + * a3: new value for SP + */ +.section .text.__barebox_riscv_entry +ENTRY(__barebox_riscv_entry) + mv sp, a3 + tail barebox_pbl_start +ENDPROC(__barebox_riscv_entry)
arch/riscv/include/asm/barebox-riscv.h +27 -3
@@ -25,9 +25,6 @@#include <asm/reloc.h> #include <pbl/handoff-data.h> -void __noreturn __naked barebox_riscv_entry (unsigned long membase,unsigned long memsize, -void *boarddata,unsigned int flags); -#define barebox_riscv_machine_entry (membase, memsize, boarddata) \ barebox_riscv_entry(membase, memsize, boarddata, RISCV_M_MODE)
@@ -82,6 +79,33 @@ static inline unsigned long riscv_mem_barebox_image(unsigned long membase,return ALIGN_DOWN (endmem - size,SZ_1M ); } +void __noreturn __barebox_riscv_entry (unsigned long membase, +unsigned long memsize,void *boarddata, +unsigned long sp); + +/* +* Main RISC-V entry point. Call this with the memory region you can +* spare for barebox. This doesn't necessarily have to be the full +* SDRAM. The currently running binary can be inside or outside of +* this region. The PBL can be running inside or outside of this +* region. +* +* -> membase + memsize +* STACK_SIZE - stack +* 128KiB - early memory space +* -> maximum end of barebox binary +* +* Inline, so naked ENTRY_FUNCTION()s can call it without a stack. +*/ +static __always_inline void __noreturn +barebox_riscv_entry (unsigned long membase,unsigned long memsize, +void *boarddata,unsigned int flags) +{ +riscv_set_flags (flags); +__barebox_riscv_entry (membase, memsize, boarddata, +riscv_mem_stack_top (membase, membase + memsize)); +} +#define ENTRY_FUNCTION (name, arg0, arg1, arg2) \ void name (ulong r0, ulong r1, ulong r2); \ static void __##name(ulong, ulong, ulong); \
Signature · 1 line
2.47.3
RFC PATCH 8/17 RISC-V: add PBL heads for the board info stage dispatch
In reply to Ahmad FatoumUnlike on ARM, the privilege mode is part of the RISC-V boot protocol and even the cache maintenance during relocation depends on it, so the head needs to know which mode it was started in.
Let's add two heads: one for images that firmware enters in supervisor mode like a Linux kernel, with the hart ID in a0 and the device tree in a1, and one for images entered in machine mode without a boot protocol, like Erizo and LiteX. Both lay out the same header as __barebox_riscv_head(), so the image tools keep working, and branch to pbl_head_start_v1(), which RISC-V implements in assembly.
Boards whose stage describes the memory for barebox proper can use the new pbl_stage_barebox_entry() as chainload callback.
--- arch/riscv/Kconfig | 2 + arch/riscv/boot/Makefile | 2 +- arch/riscv/boot/entry.c | 16 ++ arch/riscv/boot/head.S | 198 +++++++++++++++++++++++++ arch/riscv/include/asm/barebox-riscv.h | 6 + arch/riscv/lib/asm-offsets.c | 27 ++++ arch/riscv/lib/pbl.lds.S | 13 +- 7 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/boot/entry.c create mode 100644 arch/riscv/boot/head.S
arch/riscv/Kconfig +2 -0
@@ -20,6 +20,8 @@ config RISCV select RISCV_TIMER if RISCV_SBI select HW_HAS_PCI select PBL_CONSTRUCTORS + select ARCH_HAS_PBL_ENTRY + select ARCH_HAS_PBL_HEAD_START_ASM select PBL_IMAGE_ELF select HAVE_ARCH_BOARD_GENERIC_DT select HAVE_ARCH_BOOTM_OFTREE
arch/riscv/boot/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 -pbl-y += entry_ll.o uncompress.o +pbl-y += head.o entry.o entry_ll.o uncompress.o pbl-y += nmon_entry.o pbl-$(CONFIG_BOARD_GENERIC_DT) += board-dt-2nd.o
arch/riscv/boot/entry.c new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <pbl/entry.h> +#include <asm/barebox-riscv.h> + +/* +* soc_chainload callback for boards whose stage describes the memory for +* barebox proper. The head has already recorded the mode in the flags. +*/ +void __noreturn pbl_stage_barebox_entry (void *boarddata) +{ +struct pbl_stage *stage =pbl_get_stage (); + +barebox_riscv_entry (stage->mem_start, stage->mem_minsz, boarddata, +riscv_get_flags ()); +}
arch/riscv/boot/head.S new file +198 -0
198 lines not on this page · raw message
arch/riscv/include/asm/barebox-riscv.h +6 -0
@@ -33,6 +33,12 @@barebox_riscv_entry (membase, memsize, boarddata,RISCV_S_MODE ); \ }while (0 ) +/* PBL image heads for ENTRY_POINT_HEAD(), see arch/riscv/boot/head.S */ +extern char __barebox_pbl_head_v1_supervisor[]; +extern char __barebox_pbl_head_v1_machine[]; + +void __noreturn pbl_stage_barebox_entry (void *boarddata); +static inline unsigned long riscv_mem_stack_top (unsigned long membase,unsigned long endmem) {
arch/riscv/lib/asm-offsets.c +27 -0
@@ -9,6 +9,8 @@#include <linux/kbuild.h> #include <linux/kernel.h> #include <asm/ptrace.h> +#include <asm/system.h> +#include <pbl/entry.h> #define STACK_ALIGN 16
@@ -56,5 +58,30 @@ int main(void)* We allocate a pt_regs on the stack. This ensures the alignment is sane. */ DEFINE (PT_SIZE_ON_STACK ,ALIGN (sizeof (struct pt_regs ),STACK_ALIGN )); +BLANK (); +COMMENT ("struct pbl_board_info offsets" ); +OFFSET (PBI_LOCATE_PC , pbl_board_info, assembly_locate_pc); +OFFSET (PBI_ROM_STAGE , pbl_board_info, rom_stage); +OFFSET (PBI_FIRST_STAGE , pbl_board_info, first_stage); +OFFSET (PBI_SECOND_STAGE , pbl_board_info, second_stage); +DEFINE (PBI_SIZEOF ,sizeof (struct pbl_board_info )); +BLANK (); +COMMENT ("struct pbl_stage offsets" ); +OFFSET (PBS_STACKTOP , pbl_stage, stacktop); +OFFSET (PBS_MEM_START , pbl_stage, mem_start); +OFFSET (PBS_MEM_MINSZ , pbl_stage, mem_minsz); +OFFSET (PBS_MEM_MAXSZ , pbl_stage, mem_maxsz); +OFFSET (PBS_CPU_INIT , pbl_stage, prereloc_cpu_init); +OFFSET (PBS_SOC_INIT , pbl_stage, soc_init); +OFFSET (PBS_BOARD_INIT , pbl_stage, board_init); +OFFSET (PBS_SOC_CHAINLOAD , pbl_stage, soc_chainload); +BLANK (); +DEFINE (PBL_STAGE_ROM_ASM ,PBL_STAGE_ROM ); +DEFINE (PBL_STAGE_OCRAM_ASM ,PBL_STAGE_OCRAM ); +DEFINE (PBL_STAGE_DRAM_ASM ,PBL_STAGE_DRAM ); +DEFINE (PBS_STACKTOP_BEFORE_BAREBOX ,PBL_STACKTOP_BEFORE_BAREBOX ); +DEFINE (PBS_STACKTOP_AFTER_BAREBOX ,PBL_STACKTOP_AFTER_BAREBOX ); +DEFINE (RISCV_S_MODE_ASM ,RISCV_S_MODE ); +DEFINE (RISCV_M_MODE_ASM ,RISCV_M_MODE );return 0 ; }
arch/riscv/lib/pbl.lds.S +12 -1
@@ -5,6 +5,7 @@ #include <asm/barebox.lds.h> #include <asm-generic/memory_layout.h> #include <asm-generic/pointer.h> +#include <asm/asm-offsets.h> OUTPUT_ARCH(BAREBOX_OUTPUT_ARCH) OUTPUT_FORMAT(BAREBOX_OUTPUT_FORMAT)
@@ -18,6 +19,9 @@ SECTIONS .text : { _stext = .; + /* Custom image headers go before the common heads */ + *(.text_image_header*) + *(.text_head_prologue*) *(.text_head_entry*) __bare_init_start = .; *(.text_bare_init*)
@@ -28,8 +32,15 @@ SECTIONS BAREBOX_BARE_INIT_SIZE BAREBOX_PBL_SIZE - . = ALIGN(4); + . = ALIGN(ASM_SZPTR); __start_rodata = .; + __pbl_board_info = .; + .rodata.pbl_board_info : { + *(.pbl_board_info_*) + } + ASSERT(. - __pbl_board_info <= PBI_SIZEOF, "Only one PBL per image allowed") + + . = ALIGN(4); .rodata : { *(.rodata*) } . = ALIGN(ASM_SZPTR);
Signature · 1 line
2.47.3
RFC PATCH 9/17 RISC-V: board-dt-2nd: migrate to board info entry point
In reply to Ahmad FatoumThe Linux image header moves into a head of its own, which branches into the supervisor mode PBL head. That head records the mode and stashes the hart ID already, so the entry point no longer needs to.
The generic image can't know its UART in advance, so the board init callback still looks it up by root compatible. The chainload callback keeps barebox proper clear of QEMU's FDT as before.
--- arch/riscv/boot/Makefile | 2 +- arch/riscv/boot/board-dt-2nd-head.S | 36 ++++++++++++ arch/riscv/boot/board-dt-2nd.c | 89 +++++++++++++---------------- 3 files changed, 78 insertions(+), 49 deletions(-) create mode 100644 arch/riscv/boot/board-dt-2nd-head.S
arch/riscv/boot/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 pbl-y += head.o entry.o entry_ll.o uncompress.o pbl-y += nmon_entry.o -pbl-$(CONFIG_BOARD_GENERIC_DT) += board-dt-2nd.o +pbl-$(CONFIG_BOARD_GENERIC_DT) += board-dt-2nd.o board-dt-2nd-head.o
arch/riscv/boot/board-dt-2nd-head.S new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <linux/linkage.h> +#include <asm/image.h> + +#if __riscv_xlen == 64 +#define IMAGE_LOAD_OFFSET 0x200000 /* Image load offset (2MB) from start of RAM */ +#else +#define IMAGE_LOAD_OFFSET 0x400000 /* Image load offset (4MB) from start of RAM */ +#endif + +/* + * Linux RISC-V image header, so barebox-dt-2nd.img boots like a kernel. + * The stack grows down from the image start into the load offset. + */ +.section .text_image_header_dt_2nd, "ax" +ENTRY(__dt_2nd_head) + /* Relaxation and compressed instructions would shift the fields */ + .option push + .option norelax + .option norvc + nop /* code0 */ + j 1f /* code1 */ + .balign 8 + .dword IMAGE_LOAD_OFFSET /* Image load offset from start of RAM */ + .dword _barebox_image_size /* Effective Image size */ + .dword __HEAD_FLAGS /* Kernel flags */ + .word RISCV_HEADER_VERSION /* version */ + .word 0 /* reserved */ + .dword 0 /* reserved */ + .ascii RISCV_IMAGE_MAGIC /* magic 1 */ + .ascii RISCV_IMAGE_MAGIC2 /* magic 2 */ + .word 0 /* reserved (PE-COFF offset) */ + .option pop +1: + j __barebox_pbl_head_v1_supervisor +ENDPROC(__dt_2nd_head)
arch/riscv/boot/board-dt-2nd.c +41 -48
124 lines not on this page · raw message
Signature · 1 line
2.47.3
RFC PATCH 10/17 RISC-V: hifive: migrate to board info entry point
In reply to Ahmad FatoumOpenSBI starts the HiFive images like a kernel, so they use the supervisor mode head. Both boards hand the first 128M of DRAM to barebox proper, so that goes into the new SiFive SoC defaults and the boards only add their built-in device tree.
The '>' is now printed by the stage dispatch.
--- arch/riscv/boards/hifive/lowlevel.c | 30 ++++++++------------------- include/mach/sifive/entry.h | 32 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 include/mach/sifive/entry.h
arch/riscv/boards/hifive/lowlevel.c +9 -21
@@ -1,26 +1,14 @@// SPDX-License-Identifier: GPL-2.0-only -#include <common.h> -#include <asm/barebox-riscv.h> -#include <debug_ll.h> +#include <mach/sifive/entry.h> -static __always_inline void start_hifive(unsigned long hartid, void *fdt) -{ -putc_ll ('>' ); +extern char __dtb_z_hifive_unmatched_a00_start[]; +extern char __dtb_z_hifive_unleashed_a00_start[]; -barebox_riscv_supervisor_entry (0x80000000 ,SZ_128M , hartid, fdt); -} +ENTRY_POINT (FU740 , start_hifive_unmatched) + .second_stage.board_data = __dtb_z_hifive_unmatched_a00_start, +ENTRY_POINT_END -ENTRY_FUNCTION(start_hifive_unmatched, a0, a1, a2) -{ -extern char __dtb_z_hifive_unmatched_a00_start[]; - -start_hifive (a0, __dtb_z_hifive_unmatched_a00_start +get_runtime_offset ()); -} - -ENTRY_FUNCTION(start_hifive_unleashed, a0, a1, a2) -{ -extern char __dtb_z_hifive_unleashed_a00_start[]; - -start_hifive (a0, __dtb_z_hifive_unleashed_a00_start +get_runtime_offset ()); -} +ENTRY_POINT (FU540 , start_hifive_unleashed) + .second_stage.board_data = __dtb_z_hifive_unleashed_a00_start, +ENTRY_POINT_END
include/mach/sifive/entry.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __MACH_SIFIVE_ENTRY_H +#define __MACH_SIFIVE_ENTRY_H + +#include <linux/sizes.h> +#include <pbl/entry.h> +#include <asm/barebox-riscv.h> + +#define SIFIVE_DRAM_BASE 0x80000000 + +/* +* OpenSBI starts the image in supervisor mode in DRAM. Every board has +* at least 128M, boards with more can raise mem_minsz. OpenSBI leaves +* the UART set up, so there's no console and the PBL uses DEBUG_LL. +*/ +#define PBL_SOC_SIFIVE_DEFAULTS (soc) \ + .soc_type = SOC_TYPE_##soc, \ + .second_stage = { \ + .stacktop = SIFIVE_DRAM_BASE + SZ_128M, \ + .mem_start = SIFIVE_DRAM_BASE, \ + .mem_minsz = SZ_128M, \ + .soc_chainload = pbl_stage_barebox_entry, \ + } + +#define PBL_SOC_FU540_HEAD __barebox_pbl_head_v1_supervisor +#define PBL_SOC_FU540_DEFAULTS PBL_SOC_SIFIVE_DEFAULTS(FU540) + +#define PBL_SOC_FU740_HEAD __barebox_pbl_head_v1_supervisor +#define PBL_SOC_FU740_DEFAULTS PBL_SOC_SIFIVE_DEFAULTS(FU740) + +#endif
Signature · 1 line
2.47.3
RFC PATCH 11/17 RISC-V: allow relocating the PBL more than once
In reply to Ahmad Fatoumrelocate_image() doesn't update the relocation entries, so a second relocation applies an offset relative to the first runtime address to the link-time addresses and writes wrong values to wrong places.
This happens when the PBL is first relocated in place and barebox_pbl_start() then copies it into the memory it was passed. ARM64 has handled this since fab26e665163 ("ARM: aarch64: Fixup relocation table for the second relocation"), so let's rebase each entry after applying it the same way.
Like on ARM, relocate_to_adr() now also copies the BSS, as it's already in use and setup_c() won't clear it again at the destination.
Tested on QEMU virt with an image whose stage memory starts above the address OpenSBI starts it at, which used to fault right after relocation.
--- arch/riscv/lib/reloc.c | 6 +++++- arch/riscv/lib/setupc.S | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-)
arch/riscv/lib/reloc.c +5 -1
@@ -46,7 +46,7 @@ static void relocate_image(unsigned long offset,switch (RISC_R_TYPE (rela->r_info)) {case R_RISCV_NONE : -break ; +continue ;case R_RISCV_RELATIVE : *fixup = rela->r_addend + offset;break ;
@@ -63,6 +63,10 @@ static void relocate_image(unsigned long offset,putc_ll ('\n' );__hang (); } + +/* Rebase the entry, so the image can be relocated again */ + rela->r_addend += offset; + rela->r_offset += offset; } }
arch/riscv/lib/setupc.S +1 -1
@@ -53,7 +53,7 @@ ENTRY(relocate_to_adr_full) ENDPROC(relocate_to_adr_full) ENTRY(relocate_to_adr) - lla a3, __bss_start /* skip piggy data */ + lla a3, __bss_stop /* skip piggy data */ 1: addi sp, sp, -SZREG * 2 lla a1, _text /* a1: source address */
Signature · 1 line
2.47.3
RFC PATCH 12/17 RISC-V: nmon: preserve s1-s3 for the caller
In reply to Ahmad Fatoumnmon clobbers s1-s3, but barebox_nmon_entry() is called from C, where these registers are callee-saved. With NMON enabled, BeagleV thus starts barebox proper with the 'q' that left nmon as hart ID. The upcoming stage dispatch also keeps its state in these registers.
There may be no stack yet, so let's stash them in a4-a6, which neither nmon nor the DEBUG_LL macros touch.
--- arch/riscv/boot/nmon_entry.S | 7 +++++++ 1 file changed, 7 insertions(+)
arch/riscv/boot/nmon_entry.S +7 -0
@@ -8,6 +8,13 @@ .section .text_bare_init ENTRY(__barebox_nmon_entry) + /* nmon uses s1-s3, keep them in registers it leaves alone */ + mv a4, s1 + mv a5, s2 + mv a6, s3 riscv_nmon + mv s1, a4 + mv s2, a5 + mv s3, a6 ret ENDPROC(__barebox_nmon_entry)
Signature · 1 line
2.47.3
RFC PATCH 13/17 RISC-V: beaglev: migrate to board info entry point
In reply to Ahmad FatoumOpenSBI starts the BeagleV image below the 512M at 0x84000000 that barebox gets. Instead of relocating in place and having barebox_pbl_start() copy the PBL again, the stage names the region as relocaddr, so the PBL is relocated once, directly into it.
DEBUG_LL init and nmon go into the CPU init callback, which runs before relocation like the old entry point did. The '>' is now printed by the stage dispatch.
--- arch/riscv/boards/beaglev/lowlevel.c | 30 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
arch/riscv/boards/beaglev/lowlevel.c +21 -9
@@ -2,19 +2,31 @@#include <common.h> #include <debug_ll.h> +#include <pbl/entry.h> #include <asm/barebox-riscv.h> #include <asm/riscv_nmon.h> -ENTRY_FUNCTION(start_beaglev_starlight, a0, a1, a2) -{ -extern char __dtb_z_jh7100_beaglev_starlight_start[]; -void *fdt; +#define BEAGLEV_MEM_BASE 0x84000000 +extern char __dtb_z_jh7100_beaglev_starlight_start[]; + +static void __prereloc beaglev_cpu_init(void) +{debug_ll_init ();barebox_nmon_entry (); -putc_ll ('>' ); - - fdt = __dtb_z_jh7100_beaglev_starlight_start +get_runtime_offset (); - -barebox_riscv_supervisor_entry (0x84000000 ,SZ_512M , a0, fdt); } + +/* +* OpenSBI starts its payload below the memory handed to barebox, so the +* PBL relocates there first. +*/ +ENTRY_POINT_HEAD (start_beaglev_starlight, __barebox_pbl_head_v1_supervisor) + .soc_type = SOC_TYPE_JH7100, + .second_stage.stacktop = BEAGLEV_MEM_BASE + SZ_512M, + .second_stage.relocaddr = BEAGLEV_MEM_BASE, + .second_stage.mem_start = BEAGLEV_MEM_BASE, + .second_stage.mem_minsz = SZ_512M, + .second_stage.board_data = __dtb_z_jh7100_beaglev_starlight_start, + .second_stage.prereloc_cpu_init = beaglev_cpu_init, + .second_stage.soc_chainload = pbl_stage_barebox_entry, +ENTRY_POINT_END
Signature · 1 line
2.47.3
RFC PATCH 14/17 RISC-V: allwinner-d1: migrate to board info entry point
In reply to Ahmad FatoumOpenSBI enters the D1 image like a kernel, so it uses the supervisor mode head, which stashes the hart ID in tp and records the device tree in a1 as entry data. The board init callback makes that the stage's board_data for pbl_stage_barebox_entry() to pass on. Memory region and stack top are unchanged.
--- arch/riscv/boards/allwinner-d1/lowlevel.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
arch/riscv/boards/allwinner-d1/lowlevel.c +14 -4
@@ -1,12 +1,22 @@// SPDX-License-Identifier: GPL-2.0-only #include <common.h> -#include <debug_ll.h> +#include <pbl/entry.h> #include <asm/barebox-riscv.h> -#define DRAM_BASE 0x40000000 +#define DRAM_BASE 0x40000000UL -ENTRY_FUNCTION(start_allwinner_d1, a0, a1, a2) +/* OpenSBI passes on the device tree the SPL loaded */ +static void allwinner_d1_bdinit (unsigned long entry_data) { -barebox_riscv_supervisor_entry (DRAM_BASE ,SZ_1G , a0, (void *)a1); +pbl_get_stage ()->board_data = (void *)entry_data; } + +ENTRY_POINT_HEAD (start_allwinner_d1, __barebox_pbl_head_v1_supervisor) + .soc_type = SOC_TYPE_SUN20I_D1, + .second_stage.stacktop = DRAM_BASE + SZ_1G, + .second_stage.mem_start = DRAM_BASE, + .second_stage.mem_minsz = SZ_1G, + .second_stage.board_init = allwinner_d1_bdinit, + .second_stage.soc_chainload = pbl_stage_barebox_entry, +ENTRY_POINT_END
Signature · 1 line
2.47.3
RFC PATCH 15/17 RISC-V: erizo: migrate to board info entry point
In reply to Ahmad FatoumErizo starts barebox from read-only memory on POR, which can be the boot ROM, the SPI flash or QEMU's BIOS ROM, so the old code copied the PBL to the start of SDRAM when it wasn't running from there. The stage names that as relocaddr instead, which is skipped when the image was loaded there.
DEBUG_LL initialization and nmon go into the CPU init callback, which runs before relocation, so nmon still runs from where the image was started and can load a new one into SDRAM. The '>' now comes from the stage dispatch.
--- arch/riscv/boards/erizo/lowlevel.c | 32 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-)
arch/riscv/boards/erizo/lowlevel.c +21 -11
@@ -1,22 +1,32 @@// SPDX-License-Identifier: GPL-2.0-only #include <common.h> +#include <pbl/entry.h> #include <asm/barebox-riscv.h> #include <debug_ll.h> #include <asm/riscv_nmon.h> -ENTRY_FUNCTION(start_erizo_generic, a0, a1, a2) -{ -extern char __dtb_z_erizo_generic_start[]; -void *fdt; +#define ERIZO_SDRAM_BASE 0x80000000 +extern char __dtb_z_erizo_generic_start[]; + +static void __prereloc erizo_cpu_init(void) +{debug_ll_init ();barebox_nmon_entry (); -putc_ll ('>' ); - -/* On POR, we are running from read-only memory here. */ - - fdt = __dtb_z_erizo_generic_start +get_runtime_offset (); - -barebox_riscv_machine_entry (0x80000000 ,SZ_8M , fdt); } + +/* +* On POR, we are running from read-only memory, so the PBL relocates to +* the start of SDRAM. It stays in place if it was loaded there. +*/ +ENTRY_POINT_HEAD (start_erizo_generic, __barebox_pbl_head_v1_machine) + .soc_type = SOC_TYPE_ERIZO, + .second_stage.stacktop = ERIZO_SDRAM_BASE + SZ_8M, + .second_stage.relocaddr = ERIZO_SDRAM_BASE, + .second_stage.mem_start = ERIZO_SDRAM_BASE, + .second_stage.mem_minsz = SZ_8M, + .second_stage.board_data = __dtb_z_erizo_generic_start, + .second_stage.prereloc_cpu_init = erizo_cpu_init, + .second_stage.soc_chainload = pbl_stage_barebox_entry, +ENTRY_POINT_END
Signature · 1 line
2.47.3
RFC PATCH 16/17 RISC-V: litex-linux: migrate to board info entry point
In reply to Ahmad FatoumThe LiteX BIOS loads barebox into RAM and starts it in machine mode, so the image uses the machine mode head and the stage describes the RAM the old entry point passed to barebox_riscv_machine_entry().
nmon goes into the CPU init callback, which runs before relocation like the old entry point did. The '>' now comes from the stage dispatch.
--- arch/riscv/boards/litex-linux/lowlevel.c | 30 ++++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-)
arch/riscv/boards/litex-linux/lowlevel.c +17 -13
@@ -1,22 +1,26 @@// SPDX-License-Identifier: GPL-2.0-only #include <common.h> +#include <pbl/entry.h> #include <asm/barebox-riscv.h> -#include <debug_ll.h> #include <asm/riscv_nmon.h> -ENTRY_FUNCTION(start_litex_linux, a0, a1, a2) +#define LITEX_RAM_BASE 0x40000000 + +extern char __dtb_z_litex_linux_start[]; + +static void __prereloc litex_cpu_init(void) { -extern char __dtb_z_litex_linux_start[]; -void *fdt; - -/* BIOS will have loaded us into RAM */ -barebox_nmon_entry (); - -putc_ll ('>' ); - - fdt = __dtb_z_litex_linux_start +get_runtime_offset (); - -barebox_riscv_machine_entry (0x40000000 ,SZ_256M , fdt); } + +/* BIOS will have loaded us into RAM */ +ENTRY_POINT_HEAD (start_litex_linux, __barebox_pbl_head_v1_machine) + .soc_type = SOC_TYPE_LITEX, + .second_stage.stacktop = LITEX_RAM_BASE + SZ_256M, + .second_stage.mem_start = LITEX_RAM_BASE, + .second_stage.mem_minsz = SZ_256M, + .second_stage.board_data = __dtb_z_litex_linux_start, + .second_stage.prereloc_cpu_init = litex_cpu_init, + .second_stage.soc_chainload = pbl_stage_barebox_entry, +ENTRY_POINT_END
Signature · 1 line
2.47.3
RFC PATCH 17/17 Documentation: porting: describe the board info entry point
In reply to Ahmad FatoumDescribe ENTRY_POINT() and the stage dispatch as the way to write a board's PBL entry point. The description of the entry point written by hand moves into a section of its own below it and the section on image heads gains the stage dispatch a head branches to.
--- Documentation/devel/porting.rst | 124 ++++++++++++++++++++++++++++++-- 1 file changed, 119 insertions(+), 5 deletions(-)
Documentation/devel/porting.rst +119 -5
@@ -40,9 +40,91 @@ Entry point =========== The PBL's entry point is the first of your code that's run. What happens -there depends on the previously running code. If a previous stage has already -initialized the DRAM, the only thing you need to do is to set up a stack and -call the common PBL code with a memory region and your device tree blob:: +there depends on the previously running code. Boards describe it with +``ENTRY_POINT()`` and leave setting up stack, relocation and BSS to common +code. If a previous stage has already initialized the DRAM, the only thing +you need to do is to describe the memory region, your console and your +device tree blob:: + + #include <pbl/entry.h> + #include <mach/mysoc/entry.h> + + extern char __dtb_z_my_board_start[]; + + ENTRY_POINT(MYSOC, start_my_board) + .console.arg = IOMEM(MY_UART_BASE), + .second_stage.mem_start = 0x80000000, + .second_stage.mem_minsz = SZ_256M, + .second_stage.board_data = __dtb_z_my_board_start, + ENTRY_POINT_END + +Let's look at this line by line: + +``ENTRY_POINT(MYSOC, start_my_board)`` + Defines the entry point ``start_my_board`` and a board info, which + starts out with the defaults from ``<mach/mysoc/entry.h>``, see + :ref:`entry_point_defaults`. Fields set by the board override them. + For a SoC without defaults, use ``ENTRY_POINT_HEAD(name, head)`` and + set ``.soc_type`` yourself. + +``.console.arg = IOMEM(MY_UART_BASE)`` + The SoC defaults usually provide the console driver and, if the UART + isn't usable on entry, a setup callback, so the board only needs to + name its UART. Without a console, the PBL prints through ``DEBUG_LL``. + +``.second_stage`` + A board info has up to three stages: ``rom_stage`` for executing in + place from ROM, ``first_stage`` for SRAM and ``second_stage`` for DRAM. + On entry, the stage whose memory contains the program counter is + selected, or the earliest stage with callbacks if there is none. + +``.second_stage.mem_start = 0x80000000`` + ``mem_start`` and ``mem_minsz`` describe the memory the stage runs from. + barebox proper starts out with this memory and may add more later on. + If the memory can be larger than ``mem_minsz``, set ``mem_maxsz``, so + the stage is still found by program counter. + +``.second_stage.board_data = __dtb_z_my_board_start`` + The device tree passed to barebox proper, see ``__dtb_z_*`` below. + Boards that select their device tree at runtime call + ``handoff_data_add_dt()`` from their board init callback instead. + +A stage can also have a stack top (``stacktop``), an address to relocate +to (``relocaddr``) and callbacks, which run in this order: + +``prereloc_cpu_init`` + Runs before relocation and before BSS is cleared, so it must be + ``__prereloc`` and may not access global variables. + +``soc_init`` + SoC lowlevel initialization. + +``board_init`` + Board setup like DRAM initialization. Its ``entry_data`` argument is + a register the image head saved, e.g. the device tree on RISC-V. + +``soc_chainload`` + Doesn't return. Usually enters barebox proper, e.g. with + ``pbl_stage_barebox_entry()``, which passes it the stage's memory and + ``board_data``. + +The SoC defaults usually provide the SoC callbacks, so a board typically +only sets ``board_init``. If the PBL runs from SRAM first, because the +whole image doesn't fit, ``first_stage.board_init`` sets up DRAM and +``first_stage.soc_chainload`` loads the full image, which is then entered +through ``second_stage``. + +The console is brought up before ``soc_init`` if it needs no setup, else +after it and failing that after ``board_init``. Then the number of the +stage is printed followed by ``>``, e.g. ``2>``, see :ref:`troubleshooting`. + +Unrelocated ENTRY_FUNCTION +-------------------------- + +Boards not using ``ENTRY_POINT()`` write the entry point as a C function +that does the above by hand. If a previous stage has already initialized +the DRAM, the only thing you need to do is to set up a stack and call the +common PBL code with a memory region and your device tree blob:: #include <asm/barebox-arm.h> #include <console.h>
@@ -127,7 +209,8 @@ Looking at other boards you might see some different patterns: stack beforehand. If either a barebox assembly entry point, ``ENTRY_FUNCTION_WITHSTACK`` or earlier firmware has set up the stack, there is no reason to use ``__naked``, just use ``ENTRY_FUNCTION_WITHSTACK`` with a zero - stack top. + stack top. Functions that run before the stack is set up are additionally + marked ``__unsafe_prestack`` for documentation. ``noinline`` Compiler code inlining is oblivious to stack manipulation in
@@ -337,17 +420,46 @@ and the entry point should be written in a dedicated assembly file. This is the case with ARM64, see for example ``__barebox_arm64_head`` and the ``ENTRY_PROC`` macro. +For ``ENTRY_POINT()``, the image head does what the boot protocol +requires, like recording the privilege mode on RISC-V, and branches to +the stage dispatch ``pbl_head_start_v1()`` with a boot argument register +as ``entry_data``, see ``arch/riscv/boot/head.S``. ``pbl_head_start_v1()`` +runs before there is a stack, so architectures should implement it in +assembly and select ``ARCH_HAS_PBL_HEAD_START_ASM``. The C version in +``pbl/entry.c`` is only a fallback, because even in a naked function, +the compiler may spill to the stack. + Another way, which is often used for non-executable headers with extra meta-information like a checksum, is adding a new tool to ``scripts/`` and have it run as part of the image build process. ``images/`` contains various examples. +.. _entry_point_defaults: + +Entry point defaults +==================== + +Boards of the same SoC share its boot flow, so it's described once in +``include/mach/$platform/entry.h``, e.g. ``include/mach/sifive/entry.h``: + +``PBL_SOC_<SOC>_HEAD`` + The image head. Usually one the architecture provides for the boot + protocol, unless the BootROM requires a special header. + +``PBL_SOC_<SOC>_DEFAULTS`` + The board info fields ``ENTRY_POINT(<SOC>, ...)`` starts out with, + at least ``.soc_type``, the SoC's entry in ``include/pbl/soc-type.h``. + barebox proper can query it with ``barebox_soc_type()``. + +Functions written for the defaults carry the ``PBL_SOC_<SOC>_`` prefix, +so they aren't mistaken for the lowlevel API older boards call. + Memory controller setup ======================= If you've an external DRAM controller, you will need to configure it. This may involve enabling clocks and PLLs. This should all happen -in the PBL entry point. +in the PBL entry point, i.e. the first stage's board init callback. Chainloading ============
@@ -360,6 +472,8 @@ One good way to go about it is to check whether the program counter is in DRAM or SRAM. If in DRAM, we can assume that the image was loaded in full and we can just go into the common PBL entry and extract barebox proper. If in SRAM, we'll need to load the remainder from the boot medium. +This is the distinction between the first and second stage of a board +info entry point. This loading requires the PBL to have a driver for the boot medium as well as its prerequisites like clocks, resets or pin multiplexers.
Signature · 1 line
2.47.3