[PATCH 4/4] of: request reserved memory regions so other code can't

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jun 8 22:43:42 PDT 2022


From: Rouven Czerwinski <r.czerwinski at pengutronix.de>

Add a reserved_mem_read initcall which parses the reserved-memory
entries and adds barebox of reserve entries. This avoids e.g. bootm
trying to place the kernel into a reserved region.

Signed-off-by: Rouven Czerwinski <r.czerwinski at pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
Compared with Rouven's v2, rename OF_RESERVE_ENTRY_FLAG_NO_RESERVE
to NO_FIXUP and read both /reserved-memory and /memreserve
to request memory regions.
---
 common/memory.c     | 21 +++++++++++++++++++--
 drivers/of/Makefile |  1 +
 drivers/of/fdt.c    | 12 ++++++++----
 include/of.h        |  2 ++
 4 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/common/memory.c b/common/memory.c
index 95995bb6e310..b40c74bfe97f 100644
--- a/common/memory.c
+++ b/common/memory.c
@@ -12,6 +12,7 @@
 #include <asm-generic/memory_layout.h>
 #include <asm/sections.h>
 #include <malloc.h>
+#include <of.h>
 
 /*
  * Begin and End of memory area for malloc(), and current "brk"
@@ -53,9 +54,12 @@ void mem_malloc_init(void *start, void *end)
 	mem_malloc_initialized = 1;
 }
 
-#if !defined __SANDBOX__
 static int mem_malloc_resource(void)
 {
+	struct of_reserve_map *map;
+	int i;
+
+#if !defined __SANDBOX__
 	/*
 	 * Normally it's a bug when one of these fails,
 	 * but we have some setups where some of these
@@ -80,10 +84,23 @@ static int mem_malloc_resource(void)
 #ifdef STACK_BASE
 	request_sdram_region("stack", STACK_BASE, STACK_SIZE);
 #endif
+#endif
+
+	map = of_get_reserve_map();
+	if (!map)
+		return 0;
+
+	for (i = 0; i < map->num_entries; i++) {
+		const char *name;
+
+		name = map->runtime_fw & BIT(i) ? "protected code" : "protected data";
+		request_sdram_region(name, map->start[i],
+				     map->end[i] - map->start[i] + 1);
+	}
+
 	return 0;
 }
 coredevice_initcall(mem_malloc_resource);
-#endif
 
 static void *sbrk_no_zero(ptrdiff_t increment)
 {
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index ca8da71cb4f0..99b610cba85e 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_OF_GPIO) += of_gpio.o
 obj-$(CONFIG_OF_PCI) += of_pci.o
 obj-y += partition.o
 obj-y += of_net.o
+obj-$(CONFIG_OFDEVICE) += reserved-mem.o
 obj-$(CONFIG_MTD) += of_mtd.o
 obj-$(CONFIG_OF_BAREBOX_DRIVERS) += barebox.o
 obj-$(CONFIG_OF_OVERLAY) += overlay.o resolver.o of_firmware.o
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 4a4eeba24bc3..ee875f5787b1 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -551,6 +551,9 @@ int of_add_reserve_entry(resource_size_t start, resource_size_t end,
 	if (flags & OF_RESERVE_ENTRY_FLAG_RUNTIME_FW)
 		of_reserve_map.runtime_fw |= BIT(e);
 
+	if (flags & OF_RESERVE_ENTRY_FLAG_NO_FIXUP)
+		of_reserve_map.no_fixup |= BIT(e);
+
 	return 0;
 }
 
@@ -592,11 +595,12 @@ void fdt_add_reserve_map(void *__fdt)
 
 	fdt_res += n;
 
-	for (i = 0; i < res->num_entries; i++) {
+	for (i = 0; i < res->num_entries; i++, fdt_res++) {
+		if (BIT(i) & res->no_fixup)
+			continue;
+
 		of_write_number(&fdt_res->address, res->start[i], 2);
-		of_write_number(&fdt_res->size, res->end[i] - res->start[i] + 1,
-				2);
-		fdt_res++;
+		of_write_number(&fdt_res->size, res->end[i] - res->start[i] + 1, 2);
 	}
 
 	of_write_number(&fdt_res->address, (unsigned long)__fdt, 2);
diff --git a/include/of.h b/include/of.h
index d55016f1e372..c2a0cdae8dc2 100644
--- a/include/of.h
+++ b/include/of.h
@@ -56,9 +56,11 @@ struct of_reserve_map {
 	uint64_t end[OF_MAX_RESERVE_MAP];
 	int num_entries;
 	u16 runtime_fw : OF_MAX_RESERVE_MAP;
+	u16 no_fixup : OF_MAX_RESERVE_MAP;
 };
 
 #define OF_RESERVE_ENTRY_FLAG_RUNTIME_FW	BIT(0)
+#define OF_RESERVE_ENTRY_FLAG_NO_FIXUP		BIT(1)
 
 int of_add_reserve_entry(resource_size_t start, resource_size_t end,
 		 int flags);
-- 
2.30.2




More information about the barebox mailing list