* [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE
@ 2025-05-28 14:11 Michael Tretter
2025-05-28 14:11 ` [PATCH v2 1/9] ARM: rockchip: fix formatting Michael Tretter
` (8 more replies)
0 siblings, 9 replies; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
On rk3588, OP-TEE uses the device tree to detect the available SDRAM
that should be used for dynamic shared memory. Since the amount of SDRAM
is detected during boot, the rk3588 device tree doesn't contain memory
nodes. Therefore, barebox has to detect the memory and add memory nodes
the device tree in the PBL before passing the device tree to the TF-A,
which passes it on to OP-TEE.
Adding nodes to the fdt increases the size of the fdt. As OP-TEE also
modifies and extends the passed device tree, the fdt grows even further
in OP-TEE. Therefore, barebox must add some extra space for fdt
modification. OP-TEE has CFG_DTB_MAX_SIZE as limit, which is the minimum
amount of space that must be reserved by barebox. The
ARCH_ROCKCHIP_ATF_FDT_SIZE option allows to configure this size as
minimum Rockchip machines.
If the device tree fixup function finds memory nodes with the detected
addresses, it ensures that the nodes have the correct size and are
enabled. If a matching memory node is missing from the device tree, it
adds a new node.
The downstream TF-A may crash if barebox passes a device tree to it.
barebox cannot detect, if the loaded TF-A is downstream or upstream.
Therefore, add a config item to enable passing the device tree.
Patches 1 and 2 are some cleanup as preparation for actual changes.
Patches 3 to 5 extend the libfdt integration with helpers to modify and
add memory dt nodes.
Patch 6 introduces CONFIG_SCRATCH_SIZE to configure the size of the
arm_mem_scratch area.
Patches 7 to 9 add the rockchip specific code to read the memory size
from the DRAM controller, add memory dt nodes to the fdt, and pass the
fdt to the TF-A and OP-TEE.
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- pass a copy of fdt in scratch space instead of original fdt to TF-A
- drop config option to reserve space in dtb
- change fdt_fixup_mem to possibly update existing memory nodes
- Link to v1: https://lore.kernel.org/r/20250526-rk3588-optee-v1-0-5004995cbd03@pengutronix.de
---
Michael Tretter (9):
ARM: rockchip: fix formatting
ARM: rockchip: dmc: use RK3588_INT_REG_START for rk3588
lib: fdt: add fdt_addresses
PBL: fdt: refactor helper for reading nr of cells
PBL: fdt: add fdt_fixup_mem to fixup memory nodes
ARM: add CONFIG_SCRATCH_SIZE
ARM: rockchip: dmc: add rk3588_ram_sizes to get full ram size
ARM: rockchip: pass device tree to TF-A
ARM: rockchip: fixup memory in device tree for TF-A
Documentation/boards/rockchip.rst | 4 ++
arch/arm/include/asm/barebox-arm.h | 4 +-
arch/arm/mach-rockchip/Kconfig | 23 +++++++++
arch/arm/mach-rockchip/atf.c | 69 +++++++++++++++++----------
arch/arm/mach-rockchip/dmc.c | 37 +++++++++++++--
common/Kconfig | 5 ++
include/asm-generic/memory_layout.h | 1 +
include/mach/rockchip/bootrom.h | 3 ++
include/mach/rockchip/dmc.h | 2 +
include/pbl.h | 1 +
lib/Makefile | 2 +-
lib/fdt_addresses.c | 4 ++
pbl/fdt.c | 93 +++++++++++++++++++++++++++++++++----
13 files changed, 209 insertions(+), 39 deletions(-)
---
base-commit: fb35d7a2fe800260ca1ce2e189731e48ccefcf7b
change-id: 20250515-rk3588-optee-08cc33320963
Best regards,
--
Michael Tretter <m.tretter@pengutronix.de>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 1/9] ARM: rockchip: fix formatting
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 14:11 ` [PATCH v2 2/9] ARM: rockchip: dmc: use RK3588_INT_REG_START for rk3588 Michael Tretter
` (7 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
Replace spaces with tabs and remove a double newline. No semantic
changes.
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- none
---
arch/arm/mach-rockchip/atf.c | 47 ++++++++++++++++++++++----------------------
1 file changed, 23 insertions(+), 24 deletions(-)
diff --git a/arch/arm/mach-rockchip/atf.c b/arch/arm/mach-rockchip/atf.c
index d3a40e3c1b19db51b9bf620648176c54493c3afb..cfa6df043b34c0f36919048237c7ecf33dfe0724 100644
--- a/arch/arm/mach-rockchip/atf.c
+++ b/arch/arm/mach-rockchip/atf.c
@@ -101,7 +101,6 @@ static uintptr_t rk_load_optee(uintptr_t bl32, const void *bl32_image,
rk_scratch_save_optee_hdr(hdr);
-
memcpy((void *)bl32, bl32_image, bl32_size);
return bl32;
@@ -176,32 +175,32 @@ void rk3588_atf_load_bl31(void *fdt)
void __noreturn rk3588_barebox_entry(void *fdt)
{
- unsigned long membase, endmem;
+ unsigned long membase, endmem;
- membase = RK3588_DRAM_BOTTOM;
- endmem = rk3588_ram0_size();
+ membase = RK3588_DRAM_BOTTOM;
+ endmem = rk3588_ram0_size();
- rk_scratch = (void *)arm_mem_scratch(endmem);
+ rk_scratch = (void *)arm_mem_scratch(endmem);
- if (current_el() == 3) {
- rk3588_lowlevel_init();
- rockchip_store_bootrom_iram(IOMEM(RK3588_IRAM_BASE));
+ if (current_el() == 3) {
+ rk3588_lowlevel_init();
+ rockchip_store_bootrom_iram(IOMEM(RK3588_IRAM_BASE));
- /*
- * The downstream TF-A doesn't cope with our device tree when
- * CONFIG_OF_OVERLAY_LIVE is enabled, supposedly because it is
- * too big for some reason. Otherwise it doesn't have any visible
- * effect if we pass a device tree or not, except that the TF-A
- * fills in the ethernet MAC address into the device tree.
- * The upstream TF-A doesn't use the device tree at all.
- *
- * Pass NULL for now until we have a good reason to pass a real
- * device tree.
- */
- rk3588_atf_load_bl31(NULL);
- /* not reached when CONFIG_ARCH_ROCKCHIP_ATF */
- }
+ /*
+ * The downstream TF-A doesn't cope with our device tree when
+ * CONFIG_OF_OVERLAY_LIVE is enabled, supposedly because it is
+ * too big for some reason. Otherwise it doesn't have any visible
+ * effect if we pass a device tree or not, except that the TF-A
+ * fills in the ethernet MAC address into the device tree.
+ * The upstream TF-A doesn't use the device tree at all.
+ *
+ * Pass NULL for now until we have a good reason to pass a real
+ * device tree.
+ */
+ rk3588_atf_load_bl31(NULL);
+ /* not reached when CONFIG_ARCH_ROCKCHIP_ATF */
+ }
- optee_set_membase(rk_scratch_get_optee_hdr());
- barebox_arm_entry(membase, endmem - membase, fdt);
+ optee_set_membase(rk_scratch_get_optee_hdr());
+ barebox_arm_entry(membase, endmem - membase, fdt);
}
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 2/9] ARM: rockchip: dmc: use RK3588_INT_REG_START for rk3588
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
2025-05-28 14:11 ` [PATCH v2 1/9] ARM: rockchip: fix formatting Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 14:11 ` [PATCH v2 3/9] lib: fdt: add fdt_addresses Michael Tretter
` (6 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
While RK3588_INT_REG_START and RK3568_INT_REG_START are the same value,
the implementation for rk3588 should use RK3588_INT_REG_START to avoid
confusion.
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- none
---
arch/arm/mach-rockchip/dmc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm/mach-rockchip/dmc.c b/arch/arm/mach-rockchip/dmc.c
index cf63fdb5a8bb6ea5a7474fcaa1590d4a7428ceab..62a7ef8f1e38e989e84f08f6f4a6586be3e85532 100644
--- a/arch/arm/mach-rockchip/dmc.c
+++ b/arch/arm/mach-rockchip/dmc.c
@@ -187,7 +187,7 @@ resource_size_t rk3588_ram0_size(void)
pr_info("%s() size1 = 0x%08llx, size2 = 0x%08llx\n", __func__, (u64)size1, (u64)size2);
- size = min_t(resource_size_t, RK3568_INT_REG_START, size1 + size2);
+ size = min_t(resource_size_t, RK3588_INT_REG_START, size1 + size2);
return size;
}
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 3/9] lib: fdt: add fdt_addresses
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
2025-05-28 14:11 ` [PATCH v2 1/9] ARM: rockchip: fix formatting Michael Tretter
2025-05-28 14:11 ` [PATCH v2 2/9] ARM: rockchip: dmc: use RK3588_INT_REG_START for rk3588 Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 14:11 ` [PATCH v2 4/9] PBL: fdt: refactor helper for reading nr of cells Michael Tretter
` (5 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
fdt_addresses contains some helpers for handling addresses and sizes.
Add it to the local libfdt.
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- none
---
lib/Makefile | 2 +-
lib/fdt_addresses.c | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/lib/Makefile b/lib/Makefile
index 4e75f2467ac1c05edb1489a997e0767f9e9972ba..0d1d22c0845b61962a36bc7549722570f7475685 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -100,7 +100,7 @@ KASAN_SANITIZE_ubsan.o := n
CFLAGS_ubsan.o := -fno-stack-protector
libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \
- fdt_empty_tree.o
+ fdt_empty_tree.o fdt_addresses.o
$(foreach file, $(libfdt_files), \
$(eval CFLAGS_$(file) = -I $(srctree)/scripts/dtc/libfdt) \
$(eval CFLAGS_$(file:%.o=%.pbl.o) = -I $(srctree)/scripts/dtc/libfdt))
diff --git a/lib/fdt_addresses.c b/lib/fdt_addresses.c
new file mode 100644
index 0000000000000000000000000000000000000000..4bde1bb186559fea9aa6b842de8b96e9bc9bcc4a
--- /dev/null
+++ b/lib/fdt_addresses.c
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <linux/libfdt_env.h>
+#include "../scripts/dtc/libfdt/fdt_addresses.c"
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 4/9] PBL: fdt: refactor helper for reading nr of cells
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
` (2 preceding siblings ...)
2025-05-28 14:11 ` [PATCH v2 3/9] lib: fdt: add fdt_addresses Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 14:11 ` [PATCH v2 5/9] PBL: fdt: add fdt_fixup_mem to fixup memory nodes Michael Tretter
` (4 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
Since libfdt now contains fdt_addresses, the fdt_find_mem function can
use the already existing helpers to read #size-cells and #address-cells.
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- none
---
pbl/fdt.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/pbl/fdt.c b/pbl/fdt.c
index 8e4d1295074a6b8dc4ad07ad2ada5882148c9ce6..3b1783152a8cb81f3eda1187b2f7bf998c4addf4 100644
--- a/pbl/fdt.c
+++ b/pbl/fdt.c
@@ -17,8 +17,8 @@ static const __be32 *fdt_parse_reg(const __be32 *reg, uint32_t n,
void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize)
{
- const __be32 *nap, *nsp, *reg;
- uint32_t na, ns;
+ const __be32 *reg;
+ int na, ns;
uint64_t memsize64, membase64;
int node, size;
@@ -28,26 +28,23 @@ void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsiz
goto err;
}
- /* Find the #address-cells and #size-cells properties */
node = fdt_path_offset(fdt, "/");
if (node < 0) {
pr_err("Cannot find root node\n");
goto err;
}
- nap = fdt_getprop(fdt, node, "#address-cells", &size);
- if (!nap || (size != 4)) {
+ na = fdt_address_cells(fdt, node);
+ if (na < 0) {
pr_err("Cannot find #address-cells property");
goto err;
}
- na = fdt32_to_cpu(*nap);
- nsp = fdt_getprop(fdt, node, "#size-cells", &size);
- if (!nsp || (size != 4)) {
+ ns = fdt_size_cells(fdt, node);
+ if (ns < 0) {
pr_err("Cannot find #size-cells property");
goto err;
}
- ns = fdt32_to_cpu(*nsp);
/* Find the memory range */
node = fdt_node_offset_by_prop_value(fdt, -1, "device_type",
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 5/9] PBL: fdt: add fdt_fixup_mem to fixup memory nodes
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
` (3 preceding siblings ...)
2025-05-28 14:11 ` [PATCH v2 4/9] PBL: fdt: refactor helper for reading nr of cells Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 14:11 ` [PATCH v2 6/9] ARM: add CONFIG_SCRATCH_SIZE Michael Tretter
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
Board code in the PBL may use fdt_fixup_mem() to write the base
addresses and sizes of detected SDRAM to the fdt before passing the fdt
to other software like the TF-A and OP-TEE.
This is implemented on the fdt to avoid that the PBL needs to unpack the
device tree, use the of function for manipulation and repack the device
tree.
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- drop unused na and ns
- fixup existing memory nodes if possible
- remove existing memory nodes without @address postfix
- print error reason for invalid device tree blob
---
include/pbl.h | 1 +
pbl/fdt.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+)
diff --git a/include/pbl.h b/include/pbl.h
index abac3458593af2cec972a54ce9fb69e344179670..b330010562c4aba5fccbb4c421bb95291fa0bea1 100644
--- a/include/pbl.h
+++ b/include/pbl.h
@@ -17,6 +17,7 @@ extern unsigned long free_mem_end_ptr;
void pbl_barebox_uncompress(void *dest, void *compressed_start, unsigned int len);
void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize);
+int fdt_fixup_mem(void *fdt, unsigned long membase[], unsigned long memsize[], size_t num);
struct fdt_device_id {
const char *compatible;
diff --git a/pbl/fdt.c b/pbl/fdt.c
index 3b1783152a8cb81f3eda1187b2f7bf998c4addf4..c9820112c6442b65d4f462c93b6847442097c5ab 100644
--- a/pbl/fdt.c
+++ b/pbl/fdt.c
@@ -2,6 +2,7 @@
#include <linux/libfdt.h>
#include <pbl.h>
#include <linux/printk.h>
+#include <stdio.h>
static const __be32 *fdt_parse_reg(const __be32 *reg, uint32_t n,
uint64_t *val)
@@ -73,6 +74,83 @@ void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsiz
while (1);
}
+static int fdt_find_or_add_memory(void *fdt, int parentoffset, const char *name)
+{
+ int err;
+ int node;
+
+ node = fdt_subnode_offset(fdt, parentoffset, name);
+ if (node != -FDT_ERR_NOTFOUND)
+ return node;
+
+ /* Create new memory node */
+ node = fdt_add_subnode(fdt, parentoffset, name);
+ if (node < 0)
+ return node;
+ err = fdt_setprop(fdt, node, "device_type", "memory", sizeof("memory"));
+ if (err < 0)
+ return err;
+
+ return node;
+}
+
+int fdt_fixup_mem(void *fdt, unsigned long membase[], unsigned long memsize[],
+ size_t num)
+{
+ int node, root;
+ int err;
+ int i;
+
+ err = fdt_check_header(fdt);
+ if (err != 0) {
+ pr_err("Invalid device tree blob: %s\n", fdt_strerror(err));
+ return err;
+ }
+
+ root = fdt_path_offset(fdt, "/");
+ if (root < 0) {
+ pr_err("Cannot find root node: %s\n", fdt_strerror(root));
+ return root;
+ }
+
+ /* Delete memory node without @address postfix */
+ node = fdt_subnode_offset(fdt, root, "memory");
+ if (node >= 0)
+ fdt_del_node(fdt, node);
+
+ for (i = 0; i < num; i++) {
+ unsigned long base = membase[i];
+ unsigned long size = memsize[i];
+ char name[32];
+
+ if (size == 0)
+ continue;
+
+ snprintf(name, sizeof(name), "memory@%lx", base);
+ node = fdt_find_or_add_memory(fdt, root, name);
+ if (!node) {
+ pr_warn("%s: Failed to get node: %s\n",
+ name, fdt_strerror(err));
+ continue;
+ }
+
+ /* Add or rewrite the reg property */
+ fdt_delprop(fdt, node, "reg");
+ err = fdt_appendprop_addrrange(fdt, root, node, "reg",
+ base, size);
+ if (err < 0) {
+ pr_warn("%s: Failed to set reg property %lx %lx: %s\n",
+ name, base, size, fdt_strerror(err));
+ continue;
+ }
+
+ /* Remove status property to ensure the node is enabled */
+ fdt_delprop(fdt, node, "status");
+ }
+
+ return err;
+}
+
const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
const struct fdt_device_id ids[])
{
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 6/9] ARM: add CONFIG_SCRATCH_SIZE
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
` (4 preceding siblings ...)
2025-05-28 14:11 ` [PATCH v2 5/9] PBL: fdt: add fdt_fixup_mem to fixup memory nodes Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 15:34 ` Marco Felsch
2025-05-28 14:11 ` [PATCH v2 7/9] ARM: rockchip: dmc: add rk3588_ram_sizes to get full ram size Michael Tretter
` (2 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
CONFIG_SCRATCH_SIZE allows to configure the size of the scratch area for
passing data from the PBL to barebox proper.
It may be also used to pass data from the PBL to other firmware or use
it for passing data back to barebox, the size should be configurable.
Since this may be more than 32K, the size should be configurable.
Since other sizes in the memory layout are configurable as well, the
memory layout isn't static.
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- new patch
---
arch/arm/include/asm/barebox-arm.h | 4 ++--
common/Kconfig | 5 +++++
include/asm-generic/memory_layout.h | 1 +
3 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/arch/arm/include/asm/barebox-arm.h b/arch/arm/include/asm/barebox-arm.h
index 7d35e88c812393d45e331f238baecfa91cbbe299..1ad863681a04b3172be5ecd3f7fbc5ca11f3c3d7 100644
--- a/arch/arm/include/asm/barebox-arm.h
+++ b/arch/arm/include/asm/barebox-arm.h
@@ -61,7 +61,7 @@ void *barebox_arm_boot_dtb(void);
* ↓
* ---------- arm_mem_optee() / arm_mem_barebox_image_end() ----------
* ↑
- * SZ_32K
+ * SCRATCH_SIZE
* ↓
* ------------------------ arm_mem_scratch() ------------------------
* ↑
@@ -105,7 +105,7 @@ static inline unsigned long arm_mem_barebox_image_end(unsigned long endmem)
static inline unsigned long arm_mem_scratch(unsigned long endmem)
{
- return arm_mem_optee(endmem) - SZ_32K;
+ return arm_mem_optee(endmem) - SCRATCH_SIZE;
}
static inline unsigned long arm_mem_stack(unsigned long endmem)
diff --git a/common/Kconfig b/common/Kconfig
index fe50da8f3084a9d4f3b659b8a5db049b3324ce0a..4186c499b3a9012ce2ba8c7a0909c7b2bffa42bd 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -300,6 +300,11 @@ config MALLOC_SIZE
default 0x400000
prompt "malloc area size"
+config SCRATCH_SIZE
+ hex
+ default 0x8000
+ prompt "Scratch size"
+
config MALLOC_ALIGNMENT
hex
default 8
diff --git a/include/asm-generic/memory_layout.h b/include/asm-generic/memory_layout.h
index 6af1db8113f2226bc30710cee35e2aecf290b15c..39af73849796fd1954521f0b53bf38fcb0984cef 100644
--- a/include/asm-generic/memory_layout.h
+++ b/include/asm-generic/memory_layout.h
@@ -28,6 +28,7 @@
#define HEAD_TEXT_BASE MALLOC_BASE
#define MALLOC_SIZE CONFIG_MALLOC_SIZE
#define STACK_SIZE CONFIG_STACK_SIZE
+#define SCRATCH_SIZE CONFIG_SCRATCH_SIZE
/*
* This generates a useless load from the specified symbol
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 7/9] ARM: rockchip: dmc: add rk3588_ram_sizes to get full ram size
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
` (5 preceding siblings ...)
2025-05-28 14:11 ` [PATCH v2 6/9] ARM: add CONFIG_SCRATCH_SIZE Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 14:11 ` [PATCH v2 8/9] ARM: rockchip: pass device tree to TF-A Michael Tretter
2025-05-28 14:11 ` [PATCH v2 9/9] ARM: rockchip: fixup memory in device tree for TF-A Michael Tretter
8 siblings, 0 replies; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
The PBL has to pass a full description of the SDRAM to OP-TEE to allow
OP-TEE to handle dynamic shared memory in the entire SDRAM. Thus, the
PBL needs to read the full memory configuration.
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- none
---
arch/arm/mach-rockchip/dmc.c | 37 ++++++++++++++++++++++++++++++++++---
include/mach/rockchip/dmc.h | 2 ++
2 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/arch/arm/mach-rockchip/dmc.c b/arch/arm/mach-rockchip/dmc.c
index 62a7ef8f1e38e989e84f08f6f4a6586be3e85532..260b8be9c7da9e8f83323822eab92a439ef4e2ef 100644
--- a/arch/arm/mach-rockchip/dmc.c
+++ b/arch/arm/mach-rockchip/dmc.c
@@ -171,11 +171,12 @@ resource_size_t rk3568_ram0_size(void)
#define RK3588_PMUGRF_OS_REG4 0x210
#define RK3588_PMUGRF_OS_REG5 0x214
-resource_size_t rk3588_ram0_size(void)
+size_t rk3588_ram_sizes(phys_addr_t *base, resource_size_t *size, size_t n)
{
void __iomem *pmugrf = IOMEM(RK3588_PMUGRF_BASE);
u32 sys_reg2, sys_reg3, sys_reg4, sys_reg5;
- resource_size_t size, size1, size2;
+ resource_size_t memsize, size1, size2;
+ size_t i = 0;
sys_reg2 = readl(pmugrf + RK3588_PMUGRF_OS_REG2);
sys_reg3 = readl(pmugrf + RK3588_PMUGRF_OS_REG3);
@@ -187,7 +188,37 @@ resource_size_t rk3588_ram0_size(void)
pr_info("%s() size1 = 0x%08llx, size2 = 0x%08llx\n", __func__, (u64)size1, (u64)size2);
- size = min_t(resource_size_t, RK3588_INT_REG_START, size1 + size2);
+ memsize = size1 + size2;
+
+ base[i] = 0xa00000;
+ size[i] = min_t(resource_size_t, RK3588_INT_REG_START, memsize) - 0xa00000;
+ i++;
+
+ if (i < n && memsize > SZ_4G) {
+ base[i] = SZ_4G;
+ size[i] = min_t(unsigned long, DRAM_GAP1_START, memsize) - SZ_4G;
+ i++;
+ }
+ if (i < n && memsize > DRAM_GAP1_END) {
+ base[i] = DRAM_GAP1_END;
+ size[i] = min_t(unsigned long, DRAM_GAP2_START, memsize) - DRAM_GAP1_END;
+ i++;
+ }
+ if (i < n && memsize > DRAM_GAP2_END) {
+ base[i] = DRAM_GAP2_END;
+ size[i] = memsize - DRAM_GAP2_END;
+ i++;
+ }
+
+ return i;
+}
+
+resource_size_t rk3588_ram0_size(void)
+{
+ phys_addr_t base;
+ resource_size_t size;
+
+ rk3588_ram_sizes(&base, &size, 1);
return size;
}
diff --git a/include/mach/rockchip/dmc.h b/include/mach/rockchip/dmc.h
index 3df9aa5e9c87857e9ac3994e0e40c60d8c91b0a4..bb5a5afd02417920446c729b96eeb90f1fa0ead0 100644
--- a/include/mach/rockchip/dmc.h
+++ b/include/mach/rockchip/dmc.h
@@ -87,4 +87,6 @@ resource_size_t rk3399_ram0_size(void);
resource_size_t rk3568_ram0_size(void);
resource_size_t rk3588_ram0_size(void);
+size_t rk3588_ram_sizes(phys_addr_t *base, resource_size_t *size, size_t n);
+
#endif
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 8/9] ARM: rockchip: pass device tree to TF-A
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
` (6 preceding siblings ...)
2025-05-28 14:11 ` [PATCH v2 7/9] ARM: rockchip: dmc: add rk3588_ram_sizes to get full ram size Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 15:39 ` Marco Felsch
2025-05-28 14:11 ` [PATCH v2 9/9] ARM: rockchip: fixup memory in device tree for TF-A Michael Tretter
8 siblings, 1 reply; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
The upstream OP-TEE expects a device tree to be able to initialize the
dynamic shared memory. Therefore, barebox should pass a device tree that
contains memory nodes through the TF-A to OP-TEE.
OP-TEE may modify the passed fdt. Thus, barebox copies the fdt from the
fixed size rodata area to the rk_scratch area and allocates a
configurable memory area for the fdt to allow fdt modification.
OP-TEE has the CFG_DTB_MAX_SIZE config option for the maximum size of
the fdt. barebox must reserve at least that much memory for the fdt to
avoid out of bounds accesses from OP-TEE.
Unfortunately, the downstream TF-A fails to start if barebox passes its
device tree and it is not possible to detect if the loaded TF-A is able
to handle the device tree. Add a config option to pass the device tree
if it is possible.
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- pass copy of fdt in scratch space to TF-A
- add config option for fdt size
- add documentation for CONFIG_ARCH_ROCKCHIP_ATF_PASS_FDT
---
Documentation/boards/rockchip.rst | 4 ++++
arch/arm/mach-rockchip/Kconfig | 23 +++++++++++++++++++++++
arch/arm/mach-rockchip/atf.c | 24 ++++++++++++------------
include/mach/rockchip/bootrom.h | 3 +++
4 files changed, 42 insertions(+), 12 deletions(-)
diff --git a/Documentation/boards/rockchip.rst b/Documentation/boards/rockchip.rst
index b2b04abb03cd1926bb59799af5f6a8c11d410cc2..8bce92865e3eda193412180c7295b5a35b3531e8 100644
--- a/Documentation/boards/rockchip.rst
+++ b/Documentation/boards/rockchip.rst
@@ -94,6 +94,10 @@ With these barebox can be compiled as:
.. note:: The RK3566 and RK3568 seem to share the bl31 and bl32 firmware files,
whereas the memory initialization blob is different.
+.. note:: The bl31 from the rkbin repository seems to be unable to handle
+ device trees of a larger size (for example, if CONFIG_OF_OVERLAY_LIVE is
+ enabled). Disable CONFIG_ARCH_ROCKCHIP_ATF_PASS_FDT in this case.
+
Creating a bootable SD card
---------------------------
diff --git a/arch/arm/mach-rockchip/Kconfig b/arch/arm/mach-rockchip/Kconfig
index 98dfd11c182b9fee6e3c958653ad4fa8a7d98d84..1b72dc4b8a16ce9dca742ff1173fc2208a0e619d 100644
--- a/arch/arm/mach-rockchip/Kconfig
+++ b/arch/arm/mach-rockchip/Kconfig
@@ -141,6 +141,29 @@ config ARCH_ROCKCHIP_ATF
useful for debugging early startup, but for all other cases,
say y here.
+config ARCH_ROCKCHIP_ATF_PASS_FDT
+ bool "Pass device tree to TF-A"
+ depends on ARCH_ROCKCHIP_ATF
+ help
+ Enable this option if you are using an upstream OP-TEE that uses the
+ device tree to initialize dynamic shared memory, which is passed
+ through the upstream TF-A.
+
+ Disable the option if you are using a downstream TF-A since it
+ doesn't always cope with device trees. Supposedly this happens if the
+ device tree is too large, for example if CONFIG_OF_OVERLAY_LIVE is
+ enabled.
+
+config ARCH_ROCKCHIP_ATF_FDT_SIZE
+ hex
+ default 0x0
+ default 0x60000 if ARCH_ROCKCHIP_ATF_PASS_FDT
+ prompt "Reserved size for the FDT blob passed to the TF-A"
+ help
+ Set this size to CFG_DTB_MAX_SIZE in the OP-TEE configuration. OP-TEE
+ may modify the passed device tree and increase it's size. This
+ ensures that barebox reserves enough memory for the modifications.
+
config ARCH_ROCKCHIP_OPTEE
bool "Build rockchip OP-TEE binary into barebox"
depends on ARCH_ROCKCHIP_ATF
diff --git a/arch/arm/mach-rockchip/atf.c b/arch/arm/mach-rockchip/atf.c
index cfa6df043b34c0f36919048237c7ecf33dfe0724..f4a71ef2dc8dffc6ceb4f97ee542f5b83858120b 100644
--- a/arch/arm/mach-rockchip/atf.c
+++ b/arch/arm/mach-rockchip/atf.c
@@ -183,21 +183,21 @@ void __noreturn rk3588_barebox_entry(void *fdt)
rk_scratch = (void *)arm_mem_scratch(endmem);
if (current_el() == 3) {
+ void *fdt_scratch = NULL;
+
rk3588_lowlevel_init();
rockchip_store_bootrom_iram(IOMEM(RK3588_IRAM_BASE));
- /*
- * The downstream TF-A doesn't cope with our device tree when
- * CONFIG_OF_OVERLAY_LIVE is enabled, supposedly because it is
- * too big for some reason. Otherwise it doesn't have any visible
- * effect if we pass a device tree or not, except that the TF-A
- * fills in the ethernet MAC address into the device tree.
- * The upstream TF-A doesn't use the device tree at all.
- *
- * Pass NULL for now until we have a good reason to pass a real
- * device tree.
- */
- rk3588_atf_load_bl31(NULL);
+#ifdef CONFIG_ARCH_ROCKCHIP_ATF_PASS_FDT
+ pr_debug("Copy fdt to scratch area 0x%p (%zu bytes)\n",
+ rk_scratch->fdt, sizeof(rk_scratch->fdt));
+ if (fdt_open_into(fdt, rk_scratch->fdt, sizeof(rk_scratch->fdt)) == 0)
+ fdt_scratch = rk_scratch->fdt;
+ else
+ pr_warn("Failed to copy fdt to scratch: Continue without fdt\n");
+#endif
+
+ rk3588_atf_load_bl31(fdt_scratch);
/* not reached when CONFIG_ARCH_ROCKCHIP_ATF */
}
diff --git a/include/mach/rockchip/bootrom.h b/include/mach/rockchip/bootrom.h
index 6776ac5ef9813de903f936e340afd72ecd417c82..586008a78505943c968301a9ce90ed5e4aa9b18b 100644
--- a/include/mach/rockchip/bootrom.h
+++ b/include/mach/rockchip/bootrom.h
@@ -13,7 +13,10 @@
struct rockchip_scratch_space {
u32 irom[16];
struct optee_header optee_hdr;
+ /* FDT needs an 8 byte alignment */
+ u8 fdt[CONFIG_ARCH_ROCKCHIP_ATF_FDT_SIZE] __aligned(8);
};
+static_assert(sizeof(struct rockchip_scratch_space) <= CONFIG_SCRATCH_SIZE);
extern struct rockchip_scratch_space *rk_scratch;
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 9/9] ARM: rockchip: fixup memory in device tree for TF-A
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
` (7 preceding siblings ...)
2025-05-28 14:11 ` [PATCH v2 8/9] ARM: rockchip: pass device tree to TF-A Michael Tretter
@ 2025-05-28 14:11 ` Michael Tretter
2025-05-28 15:40 ` Marco Felsch
8 siblings, 1 reply; 13+ messages in thread
From: Michael Tretter @ 2025-05-28 14:11 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer, Marco Felsch; +Cc: Michael Tretter
Add the memory nodes for the detected SDRAM configuration to the fdt
before passing it to the TF-A.
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- none
---
arch/arm/mach-rockchip/atf.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/arch/arm/mach-rockchip/atf.c b/arch/arm/mach-rockchip/atf.c
index f4a71ef2dc8dffc6ceb4f97ee542f5b83858120b..c412e7adb9c93c63d0dfcc312d6a7374770ce2da 100644
--- a/arch/arm/mach-rockchip/atf.c
+++ b/arch/arm/mach-rockchip/atf.c
@@ -173,6 +173,26 @@ void rk3588_atf_load_bl31(void *fdt)
rockchip_atf_load_bl31(RK3588, rk3588_bl31_bin, rk3588_bl32_bin, fdt);
}
+#ifdef CONFIG_ARCH_ROCKCHIP_ATF_PASS_FDT
+static int rk3588_fixup_mem(void *fdt)
+{
+ /* Use 4 blocks since rk3588 has 3 gaps in the address space */
+ unsigned long base[4];
+ unsigned long size[ARRAY_SIZE(base)];
+ phys_addr_t base_tmp[ARRAY_SIZE(base)];
+ resource_size_t size_tmp[ARRAY_SIZE(base_tmp)];
+ int i, n;
+
+ n = rk3588_ram_sizes(base_tmp, size_tmp, ARRAY_SIZE(base_tmp));
+ for (i = 0; i < n; i++) {
+ base[i] = base_tmp[i];
+ size[i] = size_tmp[i];
+ }
+
+ return fdt_fixup_mem(fdt, base, size, i);
+}
+#endif
+
void __noreturn rk3588_barebox_entry(void *fdt)
{
unsigned long membase, endmem;
@@ -195,6 +215,8 @@ void __noreturn rk3588_barebox_entry(void *fdt)
fdt_scratch = rk_scratch->fdt;
else
pr_warn("Failed to copy fdt to scratch: Continue without fdt\n");
+ if (fdt_scratch && rk3588_fixup_mem(fdt_scratch) != 0)
+ pr_warn("Failed to fixup memory nodes\n");
#endif
rk3588_atf_load_bl31(fdt_scratch);
--
2.39.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 6/9] ARM: add CONFIG_SCRATCH_SIZE
2025-05-28 14:11 ` [PATCH v2 6/9] ARM: add CONFIG_SCRATCH_SIZE Michael Tretter
@ 2025-05-28 15:34 ` Marco Felsch
0 siblings, 0 replies; 13+ messages in thread
From: Marco Felsch @ 2025-05-28 15:34 UTC (permalink / raw)
To: Michael Tretter; +Cc: BAREBOX
On 25-05-28, Michael Tretter wrote:
> CONFIG_SCRATCH_SIZE allows to configure the size of the scratch area for
> passing data from the PBL to barebox proper.
>
> It may be also used to pass data from the PBL to other firmware or use
> it for passing data back to barebox, the size should be configurable.
> Since this may be more than 32K, the size should be configurable.
^
Nit: repetition of "the size should be configurable"
>
> Since other sizes in the memory layout are configurable as well, the
> memory layout isn't static.
>
> Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 8/9] ARM: rockchip: pass device tree to TF-A
2025-05-28 14:11 ` [PATCH v2 8/9] ARM: rockchip: pass device tree to TF-A Michael Tretter
@ 2025-05-28 15:39 ` Marco Felsch
0 siblings, 0 replies; 13+ messages in thread
From: Marco Felsch @ 2025-05-28 15:39 UTC (permalink / raw)
To: Michael Tretter; +Cc: BAREBOX
On 25-05-28, Michael Tretter wrote:
> The upstream OP-TEE expects a device tree to be able to initialize the
> dynamic shared memory. Therefore, barebox should pass a device tree that
> contains memory nodes through the TF-A to OP-TEE.
>
> OP-TEE may modify the passed fdt. Thus, barebox copies the fdt from the
> fixed size rodata area to the rk_scratch area and allocates a
> configurable memory area for the fdt to allow fdt modification.
>
> OP-TEE has the CFG_DTB_MAX_SIZE config option for the maximum size of
> the fdt. barebox must reserve at least that much memory for the fdt to
> avoid out of bounds accesses from OP-TEE.
>
> Unfortunately, the downstream TF-A fails to start if barebox passes its
> device tree and it is not possible to detect if the loaded TF-A is able
> to handle the device tree. Add a config option to pass the device tree
> if it is possible.
>
> Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 9/9] ARM: rockchip: fixup memory in device tree for TF-A
2025-05-28 14:11 ` [PATCH v2 9/9] ARM: rockchip: fixup memory in device tree for TF-A Michael Tretter
@ 2025-05-28 15:40 ` Marco Felsch
0 siblings, 0 replies; 13+ messages in thread
From: Marco Felsch @ 2025-05-28 15:40 UTC (permalink / raw)
To: Michael Tretter; +Cc: BAREBOX
On 25-05-28, Michael Tretter wrote:
> Add the memory nodes for the detected SDRAM configuration to the fdt
> before passing it to the TF-A.
>
> Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2025-05-28 16:28 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-28 14:11 [PATCH v2 0/9] ARM: rockchip: fix dynamic shared memory in OP-TEE Michael Tretter
2025-05-28 14:11 ` [PATCH v2 1/9] ARM: rockchip: fix formatting Michael Tretter
2025-05-28 14:11 ` [PATCH v2 2/9] ARM: rockchip: dmc: use RK3588_INT_REG_START for rk3588 Michael Tretter
2025-05-28 14:11 ` [PATCH v2 3/9] lib: fdt: add fdt_addresses Michael Tretter
2025-05-28 14:11 ` [PATCH v2 4/9] PBL: fdt: refactor helper for reading nr of cells Michael Tretter
2025-05-28 14:11 ` [PATCH v2 5/9] PBL: fdt: add fdt_fixup_mem to fixup memory nodes Michael Tretter
2025-05-28 14:11 ` [PATCH v2 6/9] ARM: add CONFIG_SCRATCH_SIZE Michael Tretter
2025-05-28 15:34 ` Marco Felsch
2025-05-28 14:11 ` [PATCH v2 7/9] ARM: rockchip: dmc: add rk3588_ram_sizes to get full ram size Michael Tretter
2025-05-28 14:11 ` [PATCH v2 8/9] ARM: rockchip: pass device tree to TF-A Michael Tretter
2025-05-28 15:39 ` Marco Felsch
2025-05-28 14:11 ` [PATCH v2 9/9] ARM: rockchip: fixup memory in device tree for TF-A Michael Tretter
2025-05-28 15:40 ` Marco Felsch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox