mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] of: add new of_fixup_reserved_memory() helper
@ 2022-08-15 11:42 Ahmad Fatoum
  2022-08-15 11:42 ` [PATCH 2/2] pstore: ram: use " Ahmad Fatoum
  2022-08-16  8:18 ` [PATCH 1/2] of: add " Sascha Hauer
  0 siblings, 2 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2022-08-15 11:42 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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@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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] pstore: ram: use new of_fixup_reserved_memory() helper
  2022-08-15 11:42 [PATCH 1/2] of: add new of_fixup_reserved_memory() helper Ahmad Fatoum
@ 2022-08-15 11:42 ` Ahmad Fatoum
  2022-08-15 14:17   ` [PATCH] fixup! " Ahmad Fatoum
  2022-08-16  8:18 ` [PATCH 1/2] of: add " Sascha Hauer
  1 sibling, 1 reply; 4+ messages in thread
From: Ahmad Fatoum @ 2022-08-15 11:42 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The new of_fixup_reserved_memory() can handle everything that the
opencoded version in pstore can with the added benefit that it correctly
handles #address-cells/#size-cells != 1, which previously were just
assumed to be 1.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/pstore/ram.c | 31 +++++++++----------------------
 1 file changed, 9 insertions(+), 22 deletions(-)

diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
index 0d8bb8f418f4..1f105c2a384e 100644
--- a/fs/pstore/ram.c
+++ b/fs/pstore/ram.c
@@ -474,35 +474,22 @@ static int ramoops_of_fixup(struct device_node *root, void *data)
 {
 	struct ramoops_platform_data *pdata = data;
 	struct device_node *node;
-	u32 reg[2];
+	struct resource res = {};
 	int ret;
 
-	node = of_get_child_by_name(root, "reserved-memory");
-	if (!node) {
-		pr_info("Adding reserved-memory node\n");
-		node = of_create_node(root, "/reserved-memory");
-		if (!node)
-			return -ENOMEM;
+	res.start = pdata->mem_address;
+	res.end = res.start + pdata->mem_size;
+	res.name = "ramoops";
 
-		of_property_write_u32(node, "#address-cells", 1);
-		of_property_write_u32(node, "#size-cells", 1);
-		of_new_property(node, "ranges", NULL, 0);
-	}
+	ret = of_fixup_reserved_memory(root, &res);
+	if (ret)
+		return ret;
 
 	node = of_get_child_by_name(node, "ramoops");
-	if (!node) {
-		pr_info("Adding ramoops node\n");
-		node = of_create_node(root, "/reserved-memory/ramoops");
-		if (!node)
-			return -ENOMEM;
-	}
+	if (!node)
+		return -ENOMEM;
 
 	ret = of_property_write_string(node, "compatible", "ramoops");
-	if (ret)
-		return ret;
-	reg[0] = pdata->mem_address;
-	reg[1] = pdata->mem_size;
-	ret = of_property_write_u32_array(node, "reg", reg, 2);
 	if (ret)
 		return ret;
 
-- 
2.30.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] fixup! pstore: ram: use new of_fixup_reserved_memory() helper
  2022-08-15 11:42 ` [PATCH 2/2] pstore: ram: use " Ahmad Fatoum
@ 2022-08-15 14:17   ` Ahmad Fatoum
  0 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2022-08-15 14:17 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Sent out series too soon. Now tested on arm32 system with
ramoops enabled and fixup was functional.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/pstore/ram.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
index e8830dcc31f0..29585bff5950 100644
--- a/fs/pstore/ram.c
+++ b/fs/pstore/ram.c
@@ -485,7 +485,7 @@ static int ramoops_of_fixup(struct device_node *root, void *data)
 	if (ret)
 		return ret;
 
-	node = of_get_child_by_name(node, "ramoops");
+	node = of_find_node_by_path_from(root, "/reserved-memory/ramoops");
 	if (!node)
 		return -ENOMEM;
 
-- 
2.30.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] of: add new of_fixup_reserved_memory() helper
  2022-08-15 11:42 [PATCH 1/2] of: add new of_fixup_reserved_memory() helper Ahmad Fatoum
  2022-08-15 11:42 ` [PATCH 2/2] pstore: ram: use " Ahmad Fatoum
@ 2022-08-16  8:18 ` Sascha Hauer
  1 sibling, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2022-08-16  8:18 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Aug 15, 2022 at 01:42:45PM +0200, Ahmad Fatoum wrote:
> 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@pengutronix.de>
> ---
>  common/oftree.c | 41 +++++++++++++++++++++++++++++++++++++++++
>  include/of.h    |  2 ++
>  2 files changed, 43 insertions(+)

Applied, thanks

Sascha

> 
> 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
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-08-16  8:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-15 11:42 [PATCH 1/2] of: add new of_fixup_reserved_memory() helper Ahmad Fatoum
2022-08-15 11:42 ` [PATCH 2/2] pstore: ram: use " Ahmad Fatoum
2022-08-15 14:17   ` [PATCH] fixup! " Ahmad Fatoum
2022-08-16  8:18 ` [PATCH 1/2] of: add " Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox