[PATCH] common: fdt: introduce fdt_fixup_reserved_memory

Tommaso Merciai tommaso.merciai at amarulasolutions.com
Mon Apr 18 21:50:26 CEST 2022


The intent of fdt_fixup_reserved_memory is to reserve memory and prevent it from
being included in the kernel's linear map from U-Boot. This is done by creating
a no-map zone into reserved-memory region

no-map (optional) - empty property

Indicates the operating system must not create a virtual mapping of the region
as part of its standard mapping of system memory, nor permit speculative access
to it under any circumstances other than under the control of the device driver
using the region.

References:
 - Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt

Signed-off-by: Tommaso Merciai <tommaso.merciai at amarulasolutions.com>
Developed-by: Michael Trimarchi <michael at amarulasolutions.com>
Tested-by: Tommaso Merciai <tommaso.merciai at amarulasolutions.com>
---
 common/fdt_support.c  | 53 +++++++++++++++++++++++++++++++++++++++++++
 include/fdt_support.h | 11 +++++++++
 2 files changed, 64 insertions(+)

diff --git a/common/fdt_support.c b/common/fdt_support.c
index ea18ea3f04..7cfaf4310c 100644
--- a/common/fdt_support.c
+++ b/common/fdt_support.c
@@ -412,6 +412,59 @@ static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
 	return p - (char *)buf;
 }
 
+int fdt_fixup_reserved_memory(void *blob, const char *name, u64 start[], u64 size[])
+{
+	int offs, len, err;
+	const char *subpath;
+	const char *path = "/reserved-memory";
+	fdt32_t address_cells = cpu_to_fdt32(fdt_address_cells(blob, 0));
+	fdt32_t size_cells = cpu_to_fdt32(fdt_size_cells(blob, 0));
+	u8 temp[16]; /* Up to 64-bit address + 64-bit size */
+
+	offs = fdt_path_offset(blob, path);
+	if (offs < 0) {
+		debug("Node %s not found\n", path);
+		path = "/";
+		subpath = "reserved-memory";
+		offs = fdt_path_offset(blob, path);
+		offs = fdt_add_subnode(blob, offs, subpath);
+		if (offs < 0) {
+			printf("Could not create %s%s node.\n", path, subpath);
+			return -1;
+		}
+		path = "/reserved-memory";
+		offs = fdt_path_offset(blob, path);
+
+		fdt_setprop(blob, offs, "#address-cells", &address_cells, sizeof(address_cells));
+		fdt_setprop(blob, offs, "#size-cells", &size_cells, sizeof(size_cells));
+		fdt_setprop(blob, offs, "ranges", NULL, 0);
+	}
+
+	offs = fdt_add_subnode(blob, offs, name ? : "private");
+	if (offs < 0) {
+		printf("Could not create %s%s node.\n", path, subpath);
+		return -1;
+	}
+
+	err = fdt_setprop(blob, offs, "no-map", NULL, 0);
+	if (err < 0) {
+		printf("WARNING: could not set %s %s.\n",
+				"no-map", fdt_strerror(err));
+		return err;
+	}
+
+	len = fdt_pack_reg(blob, temp, start, size, 1);
+
+	err = fdt_setprop(blob, offs, "reg", temp, len);
+	if (err < 0) {
+		printf("WARNING: could not set %s %s.\n",
+				"reg", fdt_strerror(err));
+		return err;
+	}
+
+	return 0;
+}
+
 #if CONFIG_NR_DRAM_BANKS > 4
 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
 #else
diff --git a/include/fdt_support.h b/include/fdt_support.h
index ac76939e81..0ade91a08f 100644
--- a/include/fdt_support.h
+++ b/include/fdt_support.h
@@ -93,6 +93,17 @@ void do_fixup_by_compat_u32(void *fdt, const char *compat,
  */
 int fdt_fixup_memory(void *blob, u64 start, u64 size);
 
+/**
+ * Setup the memory reserved node in the DT. Creates one if none was existing before.
+ *
+ * @param blob		FDT blob to update
+ * @param name		Reserved area name
+ * @param start		Begin of DRAM mapping in physical memory
+ * @param size		Size of the single memory bank
+ * @return 0 if ok, or -1 or -FDT_ERR_... on error
+ */
+int fdt_fixup_reserved_memory(void *blob, const char *name, u64 start[], u64 size[]);
+
 /**
  * Fill the DT memory node with multiple memory banks.
  * Creates the node if none was existing before.
-- 
2.25.1



More information about the U-Boot mailing list