[PATCH 1/2] of: add new of_fixup_reserved_memory() helper

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Aug 15 04:42:45 PDT 2022


We are opencoding the reserved memory fixup in fs/pstore/ram.c and the
sequence is generic enough that we could use it for other fixups as
well, e.g. rmem, barebox as secure monitor or OP-TEE which is not
configured to generate an overlay or fix up the device tree itself.

Add a helper that can be directly registered and reads all the necessary
information out of a struct resource.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/oftree.c | 41 +++++++++++++++++++++++++++++++++++++++++
 include/of.h    |  2 ++
 2 files changed, 43 insertions(+)

diff --git a/common/oftree.c b/common/oftree.c
index 91b3fcc9fad6..e459f84601a3 100644
--- a/common/oftree.c
+++ b/common/oftree.c
@@ -271,6 +271,47 @@ static int of_register_bootargs_fixup(void)
 }
 late_initcall(of_register_bootargs_fixup);
 
+int of_fixup_reserved_memory(struct device_node *root, void *_res)
+{
+	struct resource *res = _res;
+	struct device_node *node, *child;
+	struct property *pp;
+	unsigned addr_n_cells = sizeof(void *) / sizeof(__be32),
+		 size_n_cells = sizeof(void *) / sizeof(__be32);
+	unsigned rangelen = 0;
+	__be32 reg[4];
+	int ret;
+
+	node = of_get_child_by_name(root, "reserved-memory") ?: of_new_node(root, "reserved-memory");
+
+	ret = of_property_read_u32(node, "#address-cells", &addr_n_cells);
+	if (ret)
+		of_property_write_u32(node, "#address-cells", addr_n_cells);
+
+	ret = of_property_read_u32(node, "#size-cells", &size_n_cells);
+	if (ret)
+		of_property_write_u32(node, "#size-cells", size_n_cells);
+
+	pp = of_find_property(node, "ranges", &rangelen) ?: of_new_property(node, "ranges", NULL, 0);
+	if (rangelen) {
+		pr_warn("reserved-memory ranges not 1:1 mapped. Aborting fixup\n");
+		return -EINVAL;
+	}
+
+	child = of_get_child_by_name(node, res->name) ?: of_new_node(node, res->name);
+
+	if (res->flags & IORESOURCE_BUSY)
+		of_property_write_bool(child, "no-map", true);
+
+	of_write_number(reg, res->start, addr_n_cells);
+	of_write_number(reg + addr_n_cells, resource_size(res), size_n_cells);
+
+	of_new_property(child, "reg", reg,
+			(addr_n_cells + size_n_cells) * sizeof(*reg));
+
+	return 0;
+}
+
 struct of_fixup_status_data {
 	const char *path;
 	bool status;
diff --git a/include/of.h b/include/of.h
index 9130a36d372d..223c634bc88a 100644
--- a/include/of.h
+++ b/include/of.h
@@ -113,6 +113,8 @@ int of_parse_dtb(struct fdt_header *fdt);
 struct device_node *of_unflatten_dtb(const void *fdt, int size);
 struct device_node *of_unflatten_dtb_const(const void *infdt, int size);
 
+int of_fixup_reserved_memory(struct device_node *node, void *data);
+
 struct cdev;
 
 #ifdef CONFIG_OFTREE
-- 
2.30.2




More information about the barebox mailing list