From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: mfe@pengutronix.de, mtr@pengutronix.de,
Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [RFC PATCH 09/17] RISC-V: board-dt-2nd: migrate to board info entry point
Date: Tue, 27 Oct 2026 08:24:42 +0100 [thread overview]
Message-ID: <20261027074014.3280396-10-a.fatoum@barebox.org> (raw)
In-Reply-To: <20261027074014.3280396-1-a.fatoum@barebox.org>
The 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.
Assisted-by: Claude:fable-5.1
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
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
diff --git a/arch/riscv/boot/Makefile b/arch/riscv/boot/Makefile
index d98adf7f724b..7878d85b3e64 100644
--- a/arch/riscv/boot/Makefile
+++ b/arch/riscv/boot/Makefile
@@ -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
diff --git a/arch/riscv/boot/board-dt-2nd-head.S b/arch/riscv/boot/board-dt-2nd-head.S
new file mode 100644
index 000000000000..32f84412dcfe
--- /dev/null
+++ b/arch/riscv/boot/board-dt-2nd-head.S
@@ -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)
diff --git a/arch/riscv/boot/board-dt-2nd.c b/arch/riscv/boot/board-dt-2nd.c
index a5e82257e269..36c0d5faa984 100644
--- a/arch/riscv/boot/board-dt-2nd.c
+++ b/arch/riscv/boot/board-dt-2nd.c
@@ -4,70 +4,57 @@
#include <asm/sections.h>
#include <linux/sizes.h>
#include <asm/ns16550.h>
-#include <pbl.h>
-#include <fdt.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
-
-/* because we can depend on being loaded at an offset, we can just use
- * our load address as stack top
- */
-#define __barebox_riscv_head() \
- __barebox_riscv_header("auipc sp, 0", IMAGE_LOAD_OFFSET, \
- RISCV_HEADER_VERSION, "RISCV", RISCV_IMAGE_MAGIC2)
-
#include <asm/barebox-riscv.h>
+#include <pbl.h>
+#include <pbl/entry.h>
+#include <fdt.h>
static void virt_ns16550_putc(void *base, int ch)
{
- early_ns16550_putc(ch, base, 0, readb, writeb);
+ early_ns16550_putc(ch, IOMEM(base), 0, readb, writeb);
}
-static void virt_ns16550_init(void)
+static void virt_ns16550_setup(void *base)
{
- void __iomem *base = IOMEM(0x10000000);
-
- early_ns16550_init(base, 3686400 / CONFIG_BAUDRATE, 0, writeb);
- pbl_set_putc(virt_ns16550_putc, base);
+ early_ns16550_init(IOMEM(base), 3686400 / CONFIG_BAUDRATE, 0, writeb);
}
+static struct pbl_console riscv_virtio_console = {
+ .setup = virt_ns16550_setup,
+ .arg = (void *)0x10000000,
+ .driver = &(struct pbl_console_driver) { virt_ns16550_putc }
+};
+
static const struct fdt_device_id console_ids[] = {
- { .compatible = "riscv-virtio", .data = virt_ns16550_init },
+ { .compatible = "riscv-virtio", .data = &riscv_virtio_console },
{ /* sentinel */ }
};
-static void noinline __noreturn start_dt_2nd_nonnaked(unsigned long hartid,
- unsigned long _fdt)
+/* entry_data is the device tree the image was started with, if any */
+static void dt_2nd_bdinit(unsigned long entry_data)
{
- unsigned long membase, memsize, endmem, endfdt, barebox_size;
- struct fdt_header *fdt = (void *)_fdt;
- void (*pbl_uart_init)(void);
+ struct fdt_header *fdt = (void *)entry_data;
+ const struct pbl_console *console;
if (!fdt)
hang();
- /*
- * We need to call this here, as a multiplatform build
- * depends on querying mode for riscv_vendor_id()
- */
- riscv_set_flags(RISCV_S_MODE);
+ console = fdt_device_get_match_data(fdt, "/", console_ids);
+ if (console)
+ __pbl_board_info.console = *console;
- relocate_to_current_adr();
- setup_c();
+ pbl_get_stage()->board_data = fdt;
+}
- pbl_uart_init = fdt_device_get_match_data(fdt, "/", console_ids);
- if (pbl_uart_init) {
- pbl_uart_init();
- putchar('>');
- }
+static void __noreturn dt_2nd_chainload(void *boarddata)
+{
+ unsigned long membase, memsize, endmem, endfdt, barebox_size;
+ unsigned long boarddata_ul = (unsigned long)boarddata;
+ struct fdt_header *fdt = boarddata;
fdt_find_mem(fdt, &membase, &memsize);
endmem = membase + memsize;
- endfdt = _fdt + be32_to_cpu(fdt->totalsize);
+ endfdt = boarddata_ul + be32_to_cpu(fdt->totalsize);
/*
* QEMU likes to place the FDT at the end of RAM, where barebox
@@ -80,13 +67,19 @@ static void noinline __noreturn start_dt_2nd_nonnaked(unsigned long hartid,
handoff_data_add_dt(fdt);
if (riscv_mem_barebox_image(membase, endmem, barebox_size, NULL) < endfdt &&
- _fdt < riscv_mem_stack_top(membase, endmem))
- memsize = ALIGN_DOWN(_fdt - membase, SZ_1M);
+ boarddata_ul < riscv_mem_stack_top(membase, endmem))
+ memsize = ALIGN_DOWN(boarddata_ul - membase, SZ_1M);
- barebox_riscv_supervisor_entry(membase, memsize, hartid, NULL);
+ /* The head has set the mode and the hart ID in tp already */
+ barebox_riscv_entry(membase, memsize, NULL, RISCV_S_MODE);
}
-ENTRY_FUNCTION(start_dt_2nd, hartid, _fdt, a2)
-{
- start_dt_2nd_nonnaked(hartid, _fdt);
-}
+/* The console depends on the machine, so dt_2nd_bdinit() looks it up */
+extern char __dt_2nd_head[];
+
+ENTRY_POINT_HEAD(start_dt_2nd, __dt_2nd_head)
+ .soc_type = SOC_TYPE_GENERIC_DT,
+ .second_stage.stacktop = PBL_STACKTOP_BEFORE_BAREBOX,
+ .second_stage.board_init = dt_2nd_bdinit,
+ .second_stage.soc_chainload = dt_2nd_chainload,
+ENTRY_POINT_END
--
2.47.3
next prev parent reply other threads:[~2026-10-27 7:45 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 7:24 [RFC PATCH 00/17] Switch RISC-V to declarative entry points Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 01/17] asm-generic: add registers.h with a pc_inside() helper Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 02/17] RISC-V: add call_with_stack() helper Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 03/17] RISC-V: implement relocate_to_adr_full() Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 04/17] PBL: add SoC type declaration and handoff support Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 05/17] PBL: introduce board info based entry point framework Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 06/17] PBL: describe the console in the board info and report the stage Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 07/17] RISC-V: make barebox_riscv_entry() non-naked Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 08/17] RISC-V: add PBL heads for the board info stage dispatch Ahmad Fatoum
2026-10-27 7:24 ` Ahmad Fatoum [this message]
2026-10-27 7:24 ` [RFC PATCH 10/17] RISC-V: hifive: migrate to board info entry point Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 11/17] RISC-V: allow relocating the PBL more than once Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 12/17] RISC-V: nmon: preserve s1-s3 for the caller Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 13/17] RISC-V: beaglev: migrate to board info entry point Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 14/17] RISC-V: allwinner-d1: " Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 15/17] RISC-V: erizo: " Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 16/17] RISC-V: litex-linux: " Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 17/17] Documentation: porting: describe the " Ahmad Fatoum
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261027074014.3280396-10-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
--cc=mfe@pengutronix.de \
--cc=mtr@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox