Copy the no-map reserved-memory regions of the control devicetree into
the devicetree passed to the OS when MTK_DRAM_DESC_HANDOFF is enabled.

The control devicetree describes the memory reserved for firmware such
as BL31 and OP-TEE, and the boot stage that initializes the DRAM adds
regions whose location depends on the DRAM fitted to the board. An OS
devicetree loaded from elsewhere does not necessarily describe these
regions, but the OS must not use them.

Signed-off-by: David Lechner <[email protected]>
---
 arch/arm/mach-mediatek/Kconfig     |  6 ++++-
 arch/arm/mach-mediatek/Makefile    |  3 +++
 arch/arm/mach-mediatek/dram_desc.c | 52 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/arch/arm/mach-mediatek/Kconfig b/arch/arm/mach-mediatek/Kconfig
index 42868c9113d..98e95a11bf4 100644
--- a/arch/arm/mach-mediatek/Kconfig
+++ b/arch/arm/mach-mediatek/Kconfig
@@ -282,6 +282,8 @@ config MTK_DRAM_DESC_HANDOFF
        bool "Require the DRAM description in a transfer list"
        depends on BLOBLIST
        depends on SPL_BLOBLIST || !SPL
+       depends on !MTK_TZ_MOVABLE
+       select OF_SYSTEM_SETUP
        help
          The DRAM size and the memory reserved for the DRAM controller depend
          on the DRAM fitted to the board, so the devicetree built into U-Boot
@@ -289,7 +291,9 @@ config MTK_DRAM_DESC_HANDOFF
          describes them instead, with a devicetree overlay in a Firmware
          Handoff transfer list.
 
-         Make SPL stop if it does not receive such an overlay.
+         Make SPL stop if it does not receive such an overlay, and pass the
+         no-map reserved memory regions of the control devicetree on to the
+         OS.
 
 config MTK_TZ_MOVABLE
        select ARCH_MISC_INIT
diff --git a/arch/arm/mach-mediatek/Makefile b/arch/arm/mach-mediatek/Makefile
index 661fc0b88d9..665909cf585 100644
--- a/arch/arm/mach-mediatek/Makefile
+++ b/arch/arm/mach-mediatek/Makefile
@@ -4,6 +4,9 @@ obj-$(CONFIG_ARM64) += armv8-mem-map.o
 obj-y  += cpu.o
 obj-$(CONFIG_MTK_TZ_MOVABLE)   += tzcfg.o
 obj-$(CONFIG_XPL_BUILD)        += spl.o
+ifndef CONFIG_XPL_BUILD
+obj-$(CONFIG_MTK_DRAM_DESC_HANDOFF) += dram_desc.o
+endif
 obj-$(CONFIG_$(PHASE_)MTK_FIRMWARE_AB) += spl_ab.o mtk_ab.o
 
 obj-$(CONFIG_TARGET_MT7622) += mt7622/
diff --git a/arch/arm/mach-mediatek/dram_desc.c 
b/arch/arm/mach-mediatek/dram_desc.c
new file mode 100644
index 00000000000..1e66ea2a5a6
--- /dev/null
+++ b/arch/arm/mach-mediatek/dram_desc.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 BayLibre SAS
+ */
+
+#include <fdt_support.h>
+#include <fdtdec.h>
+#include <string.h>
+#include <asm/u-boot.h>
+#include <dm/ofnode.h>
+#include <linux/ioport.h>
+
+/*
+ * Pass the no-map reserved memory regions of the control devicetree on to the
+ * OS. Besides the static firmware regions, these can include regions that an
+ * earlier boot stage added, at addresses that depend on the DRAM fitted to the
+ * board.
+ */
+int ft_system_setup(void *blob, struct bd_info *bd)
+{
+       ofnode node, subnode;
+       int ret;
+
+       node = ofnode_path("/reserved-memory");
+       if (!ofnode_valid(node))
+               return 0;
+
+       ofnode_for_each_subnode(subnode, node) {
+               struct fdt_memory carveout;
+               struct resource res;
+               char name[64], *at;
+
+               if (!ofnode_read_bool(subnode, "no-map") ||
+                   ofnode_read_resource(subnode, 0, &res))
+                       continue;
+
+               strlcpy(name, ofnode_get_name(subnode), sizeof(name));
+               at = strchr(name, '@');
+               if (at)
+                       *at = '\0';
+
+               carveout.start = res.start;
+               carveout.end = res.end;
+
+               ret = fdtdec_add_reserved_memory(blob, name, &carveout, NULL, 0,
+                                                NULL, 
FDTDEC_RESERVED_MEMORY_NO_MAP);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}

-- 
2.53.0

Reply via email to