* [PATCH 2/3] add handoff-data support
2024-04-30 10:53 [PATCH 0/3] add PBL handoff-data support Sascha Hauer
2024-04-30 10:53 ` [PATCH 1/3] ARM: move blob_is_arm_boarddata() to include Sascha Hauer
@ 2024-04-30 10:53 ` Sascha Hauer
2024-04-30 10:53 ` [PATCH 3/3] ARM: pass handoff data from PBL to proper Sascha Hauer
2024-05-07 7:35 ` [PATCH 0/3] add PBL handoff-data support Sascha Hauer
3 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2024-04-30 10:53 UTC (permalink / raw)
To: Barebox List
We need to pass data from the PBL to barebox proper. Right now we do
this with passing the data in registers which is quite limited. As the
amount of information that has to be passed increases it's time to
overcome this limitation.
With this patch we introduce handoff-data which is a linked list of
memory blobs that can be passed from PBL to barebox proper.
The data format is done in a way that enables us to compile the list
entries and the data into the binary, so that no memory allocations
are needed in PBL.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/handoff-data.h | 48 +++++++++++
pbl/Makefile | 1 +
pbl/handoff-data.c | 192 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 241 insertions(+)
create mode 100644 include/handoff-data.h
create mode 100644 pbl/handoff-data.c
diff --git a/include/handoff-data.h b/include/handoff-data.h
new file mode 100644
index 0000000000..f5586e22fe
--- /dev/null
+++ b/include/handoff-data.h
@@ -0,0 +1,48 @@
+#ifndef __HANDOFF_DATA_H
+#define __HANDOFF_DATA_H
+
+#include <linux/list.h>
+
+struct handoff_data {
+ struct list_head entries;
+};
+
+#define HANDOFF_DATA_BAREBOX(n) (0x28061971 + (n))
+#define HANDOFF_DATA_INTERNAL_DT HANDOFF_DATA_BAREBOX(0)
+#define HANDOFF_DATA_INTERNAL_DT_Z HANDOFF_DATA_BAREBOX(1)
+#define HANDOFF_DATA_EXTERNAL_DT HANDOFF_DATA_BAREBOX(2)
+#define HANDOFF_DATA_BOARDDATA HANDOFF_DATA_BAREBOX(3)
+
+#define HANDOFF_DATA_BOARD(n) (0x951726fb + (n))
+
+struct handoff_data_entry {
+ struct list_head list;
+ void *data;
+ size_t size;
+ unsigned int cookie;
+#define HANDOFF_DATA_FLAG_NO_COPY BIT(0)
+ unsigned int flags;
+};
+
+#define handoff_data_add_flags(_cookie, _data, _size, _flags) \
+ do { \
+ static struct handoff_data_entry hde; \
+ hde.cookie = _cookie; \
+ hde.data = _data; \
+ hde.size = _size; \
+ hde.flags = _flags; \
+ \
+ handoff_data_add_entry(&hde); \
+ } while (0);
+
+#define handoff_data_add(_cookie, _data, _size) \
+ handoff_data_add_flags((_cookie), (_data), (_size), 0)
+
+void handoff_data_add_entry(struct handoff_data_entry *entry);
+size_t handoff_data_size(void);
+void handoff_data_move(void *dest);
+void handoff_data_set(struct handoff_data *handoff);
+void *handoff_data_get_entry(unsigned int cookie, size_t *size);
+int handoff_data_show(void);
+
+#endif /* __HANDOFF_DATA_H */
diff --git a/pbl/Makefile b/pbl/Makefile
index f6e98e78be..79837c5611 100644
--- a/pbl/Makefile
+++ b/pbl/Makefile
@@ -8,3 +8,4 @@ pbl-y += string.o
pbl-y += decomp.o
pbl-$(CONFIG_LIBFDT) += fdt.o
pbl-$(CONFIG_PBL_CONSOLE) += console.o
+obj-pbl-y += handoff-data.o
diff --git a/pbl/handoff-data.c b/pbl/handoff-data.c
new file mode 100644
index 0000000000..fcb27234a6
--- /dev/null
+++ b/pbl/handoff-data.c
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <common.h>
+#include <handoff-data.h>
+#include <init.h>
+#include <linux/list.h>
+#include <memory.h>
+
+static struct handoff_data *handoff_data = (void *)-1;
+
+static struct handoff_data *handoff_data_get(void)
+{
+ static struct handoff_data __handoff_data;
+
+ /*
+ * Sometimes the PBL copies itself to some other location and is
+ * re-entered at that location. For example on some i.MX SoCs we have
+ * to move the PBL out of the SRAM (which will be occupied by the TF-A
+ * later). We force the handoff_data variable into the data segment.
+ * When moving the PBL somewhere else with handoff_data set we move the
+ * content of the variable with it and thus find it to have the correct
+ * value in the new PBL.
+ */
+ if (handoff_data == (void *)-1) {
+ handoff_data = &__handoff_data;
+ INIT_LIST_HEAD(&handoff_data->entries);
+ }
+
+ return handoff_data;
+}
+
+/**
+ * handoff_data_set - set the handoff data to be at a specified pointer
+ * @handoff: the place where the handoff data is
+ *
+ * This sets the handoff data to @handoff. To be used by barebox proper
+ * to pass the place where the handoff data has been placed by the PBL.
+ */
+void handoff_data_set(struct handoff_data *handoff)
+{
+ handoff_data = handoff;
+}
+
+/**
+ * handoff_data_add_entry - add a new handoff data entry
+ * @hde: the new entry
+ *
+ * This adds a new handoff data entry.
+ */
+void handoff_data_add_entry(struct handoff_data_entry *hde)
+{
+ struct handoff_data *hd = handoff_data_get();
+
+ list_add_tail(&hde->list, &hd->entries);
+}
+
+/**
+ * handoff_data_size - calculate the handoff data size
+ *
+ * This calculates the size needed for the current handoff data
+ * when put to a contiguous memory regions. Can be used to get the
+ * size needed in preparation for a handoff_data_move()
+ */
+size_t handoff_data_size(void)
+{
+ struct handoff_data *hd = handoff_data_get();
+ struct handoff_data_entry *hde;
+ size_t size = 0;
+ size_t dsize = 0;
+
+ dsize += sizeof(*hd);
+
+ list_for_each_entry(hde, &hd->entries, list) {
+ dsize += sizeof(*hde);
+ size += ALIGN(hde->size, 8);
+ }
+
+ return dsize + size;
+}
+
+/**
+ * handoff_data_move - move handoff data to specified destination
+ * @dest: The place where to move the handoff data to
+ *
+ * This moves the handoff data to @dest and also sets the new location
+ * to @dest. This can be used to move the handoff data to a contiguous
+ * region outside the binary. Note once moved no data should be added,
+ * as that would make the handoff_data discontigoous again.
+ */
+void handoff_data_move(void *dest)
+{
+ struct handoff_data *hd = handoff_data_get();
+ struct handoff_data *hdnew = dest;
+ struct handoff_data_entry *hde;
+
+ INIT_LIST_HEAD(&hdnew->entries);
+
+ dest = hdnew + 1;
+
+ list_for_each_entry(hde, &hd->entries, list) {
+ struct handoff_data_entry *newde = dest;
+
+ dest = newde + 1;
+
+ if (hde->flags & HANDOFF_DATA_FLAG_NO_COPY) {
+ newde->data = hde->data;
+ } else {
+ memcpy(dest, hde->data, hde->size);
+ newde->data = dest;
+ dest += ALIGN(hde->size, 8);
+ }
+
+ newde->size = hde->size;
+ newde->cookie = hde->cookie;
+ list_add_tail(&newde->list, &hdnew->entries);
+ }
+
+ handoff_data_set(hdnew);
+}
+
+/**
+ * handoff_data_get_entry - get the memory associated to a cookie
+ * @cookie: the cookie the data is identified with
+ * @size: size of the memory returned
+ *
+ * This returns the memory associated with @cookie.
+ */
+void *handoff_data_get_entry(unsigned int cookie, size_t *size)
+{
+ struct handoff_data *hd = handoff_data_get();
+ struct handoff_data_entry *hde;
+
+ list_for_each_entry(hde, &hd->entries, list) {
+ if (hde->cookie == cookie) {
+ *size = hde->size;
+ return hde->data;
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * handoff_data_show - show current handoff data entries
+ *
+ * This prints the current handoff data entries to the console for debugging
+ * purposes.
+ */
+int handoff_data_show(void)
+{
+ struct handoff_data *hd = handoff_data_get();
+ struct handoff_data_entry *hde;
+
+ list_for_each_entry(hde, &hd->entries, list) {
+ printf("handoff 0x%08x at 0x%p (size %zu)\n",
+ hde->cookie, hde->data, hde->size);
+ }
+
+ return 0;
+}
+
+static const char *handoff_data_entry_name(struct handoff_data_entry *hde)
+{
+ static char name[sizeof("handoff 12345678")];
+
+ switch (hde->cookie) {
+ case HANDOFF_DATA_INTERNAL_DT:
+ return "handoff FDT (internal)";
+ case HANDOFF_DATA_INTERNAL_DT_Z:
+ return "handoff FDT (internal, compressed)";
+ case HANDOFF_DATA_EXTERNAL_DT:
+ return "handoff FDT (external)";
+ case HANDOFF_DATA_BOARDDATA:
+ return "handoff boarddata";
+ default:
+ sprintf(name, "handoff %08x", hde->cookie);
+ return name;
+ }
+}
+
+static int handoff_data_reserve(void)
+{
+ struct handoff_data *hd = handoff_data_get();
+ struct handoff_data_entry *hde;
+
+ list_for_each_entry(hde, &hd->entries, list) {
+ const char *name = handoff_data_entry_name(hde);
+ request_sdram_region(name, (resource_size_t)hde->data, hde->size);
+ }
+
+ return 0;
+}
+late_initcall(handoff_data_reserve);
--
2.39.2
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 3/3] ARM: pass handoff data from PBL to proper
2024-04-30 10:53 [PATCH 0/3] add PBL handoff-data support Sascha Hauer
2024-04-30 10:53 ` [PATCH 1/3] ARM: move blob_is_arm_boarddata() to include Sascha Hauer
2024-04-30 10:53 ` [PATCH 2/3] add handoff-data support Sascha Hauer
@ 2024-04-30 10:53 ` Sascha Hauer
2024-05-07 7:35 ` [PATCH 0/3] add PBL handoff-data support Sascha Hauer
3 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2024-04-30 10:53 UTC (permalink / raw)
To: Barebox List
Use newly introduced handoff data to pass data from PBL to barebox
proper. This will allow us later to pass more SoC and/or board specific
data from PBL to barebox proper.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
arch/arm/cpu/start.c | 53 ++++++++++++---------------------------
arch/arm/cpu/uncompress.c | 33 +++++++++++++++++++++---
2 files changed, 45 insertions(+), 41 deletions(-)
diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c
index c13e93c243..b9dbe1f2fb 100644
--- a/arch/arm/cpu/start.c
+++ b/arch/arm/cpu/start.c
@@ -21,6 +21,7 @@
#include <asm/mmu.h>
#include <linux/kasan.h>
#include <memory.h>
+#include <handoff-data.h>
#include <uncompress.h>
#include <compressed-dtb.h>
#include <malloc.h>
@@ -38,10 +39,9 @@ static unsigned long barebox_boarddata_size;
const struct barebox_boarddata *barebox_get_boarddata(void)
{
- if (!barebox_boarddata || !blob_is_arm_boarddata(barebox_boarddata))
- return NULL;
+ size_t size;
- return barebox_boarddata;
+ return handoff_data_get_entry(HANDOFF_DATA_BOARDDATA, &size);
}
u32 barebox_arm_machine(void)
@@ -56,19 +56,24 @@ void *barebox_arm_boot_dtb(void)
int ret = 0;
struct barebox_boarddata_compressed_dtb *compressed_dtb;
static void *boot_dtb;
+ void *blob;
+ size_t size;
if (boot_dtb)
return boot_dtb;
- if (barebox_boarddata && blob_is_fdt(barebox_boarddata)) {
- pr_debug("%s: using barebox_boarddata\n", __func__);
- return barebox_boarddata;
- }
+ blob = handoff_data_get_entry(HANDOFF_DATA_INTERNAL_DT, &size);
+ if (blob)
+ return blob;
+
+ blob = handoff_data_get_entry(HANDOFF_DATA_INTERNAL_DT_Z, &size);
+ if (!blob)
+ return NULL;
- if (!fdt_blob_can_be_decompressed(barebox_boarddata))
+ if (!fdt_blob_can_be_decompressed(blob))
return NULL;
- compressed_dtb = barebox_boarddata;
+ compressed_dtb = blob;
pr_debug("%s: using compressed_dtb\n", __func__);
@@ -167,34 +172,6 @@ __noreturn __prereloc void barebox_non_pbl_start(unsigned long membase,
arm_barebox_size = barebox_size;
malloc_end = barebox_base;
- if (boarddata) {
- uint32_t totalsize = 0;
- const char *name;
-
- if (blob_is_fdt(boarddata)) {
- totalsize = get_unaligned_be32(boarddata + 4);
- name = "DTB";
- } else if (blob_is_compressed_fdt(boarddata)) {
- struct barebox_boarddata_compressed_dtb *bd = boarddata;
- totalsize = bd->datalen + sizeof(*bd);
- name = "Compressed DTB";
- } else if (blob_is_arm_boarddata(boarddata)) {
- totalsize = sizeof(struct barebox_arm_boarddata);
- name = "machine type";
- }
-
- if (totalsize) {
- unsigned long mem = arm_mem_boarddata(membase, endmem,
- totalsize);
- pr_debug("found %s in boarddata, copying to 0x%08lx\n",
- name, mem);
- barebox_boarddata = memcpy((void *)mem, boarddata,
- totalsize);
- barebox_boarddata_size = totalsize;
- malloc_end = mem;
- }
- }
-
/*
* Maximum malloc space is the Kconfig value if given
* or 1GB.
@@ -216,6 +193,8 @@ __noreturn __prereloc void barebox_non_pbl_start(unsigned long membase,
mem_malloc_init((void *)malloc_start, (void *)malloc_end - 1);
+ handoff_data_set(boarddata);
+
if (IS_ENABLED(CONFIG_BOOTM_OPTEE))
of_add_reserve_entry(endmem - OPTEE_SIZE, endmem - 1);
diff --git a/arch/arm/cpu/uncompress.c b/arch/arm/cpu/uncompress.c
index aa1a49bfc9..a29703e760 100644
--- a/arch/arm/cpu/uncompress.c
+++ b/arch/arm/cpu/uncompress.c
@@ -10,6 +10,7 @@
#include <init.h>
#include <linux/sizes.h>
#include <pbl.h>
+#include <handoff-data.h>
#include <asm/barebox-arm.h>
#include <asm/barebox-arm-head.h>
#include <asm-generic/memory_layout.h>
@@ -18,6 +19,7 @@
#include <asm/cache.h>
#include <asm/mmu.h>
#include <asm/unaligned.h>
+#include <compressed-dtb.h>
#include <debug_ll.h>
@@ -29,6 +31,22 @@ unsigned long free_mem_end_ptr;
extern unsigned char input_data[];
extern unsigned char input_data_end[];
+static void add_handoff_data(void *boarddata)
+{
+ if (blob_is_fdt(boarddata)) {
+ handoff_data_add(HANDOFF_DATA_INTERNAL_DT, boarddata,
+ get_unaligned_be32(boarddata + 4));
+ } else if (blob_is_compressed_fdt(boarddata)) {
+ struct barebox_boarddata_compressed_dtb *bd = boarddata;
+
+ handoff_data_add(HANDOFF_DATA_INTERNAL_DT_Z, boarddata,
+ bd->datalen + sizeof(*bd));
+ } else if (blob_is_arm_boarddata(boarddata)) {
+ handoff_data_add(HANDOFF_DATA_BOARDDATA, boarddata,
+ sizeof(struct barebox_arm_boarddata));
+ }
+}
+
void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
void *boarddata)
{
@@ -38,6 +56,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
unsigned long barebox_base;
void *pg_start, *pg_end;
unsigned long pc = get_pc();
+ void *handoff_data;
/* piggy data is not relocated, so determine the bounds now */
pg_start = runtime_address(input_data);
@@ -56,9 +75,6 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
pg_len = pg_end - pg_start;
uncompressed_len = get_unaligned((const u32 *)(pg_start + pg_len - 4));
- barebox_base = arm_mem_barebox_image(membase, endmem,
- uncompressed_len + MAX_BSS_SIZE);
-
setup_c();
pr_debug("memory at 0x%08lx, size 0x%08lx\n", membase, memsize);
@@ -69,11 +85,20 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
free_mem_ptr = arm_mem_early_malloc(endmem);
free_mem_end_ptr = arm_mem_early_malloc_end(endmem);
+ barebox_base = arm_mem_barebox_image(membase, endmem,
+ uncompressed_len + MAX_BSS_SIZE + handoff_data_size());
+
+ handoff_data = (void *)barebox_base + uncompressed_len + MAX_BSS_SIZE;
+
pr_debug("uncompressing barebox binary at 0x%p (size 0x%08x) to 0x%08lx (uncompressed size: 0x%08x)\n",
pg_start, pg_len, barebox_base, uncompressed_len);
pbl_barebox_uncompress((void*)barebox_base, pg_start, pg_len);
+ add_handoff_data(boarddata);
+
+ handoff_data_move(handoff_data);
+
sync_caches_for_execution();
if (IS_ENABLED(CONFIG_THUMB2_BAREBOX))
@@ -86,5 +111,5 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
if (IS_ENABLED(CONFIG_CPU_V7) && boot_cpu_mode() == HYP_MODE)
armv7_switch_to_hyp();
- barebox(membase, memsize, boarddata);
+ barebox(membase, memsize, handoff_data);
}
--
2.39.2
^ permalink raw reply [flat|nested] 6+ messages in thread