devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] MTD physmap: Adding reserved RAM support and fixing reserved_mem limitations
@ 2025-11-17 17:00 Gregory CLEMENT
  2025-11-17 17:00 ` [PATCH 1/3] of: reserved_mem: Support multiple 'reg' entries for memory-region Gregory CLEMENT
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Gregory CLEMENT @ 2025-11-17 17:00 UTC (permalink / raw)
  To: Rob Herring, Saravana Kannan, Linus Walleij, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Thomas Petazzoni, Vladimir Kondratiev, Benoît Monin,
	Théo Lebrun, devicetree, linux-kernel, linux-arm-kernel,
	linux-mtd, Gregory CLEMENT

Hello,

While working on adding reserved RAM support to the MTD physmap
driver, I encountered a limitation in the kernel's reserved memory
support: although the Device Tree specification allows multiple reg
entries for a memory-region node, the kernel only processed the first
one. This limitation prevented proper support for reserved RAM regions
in MTD.

This series addresses both issues:

First, the reserved memory support is extended to fully support
multiple reg entries per memory-region node, ensuring compliance with
the Device Tree specification.

Then, with this foundation in place, the series updates the MTD
physmap driver to support reserved RAM regions. The DT bindings are
extended to allow memory-region phandles for RAM access, and the
physmap driver is modified to use these regions.

Gregory

Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
---
Gregory CLEMENT (3):
      of: reserved_mem: Support multiple 'reg' entries for memory-region
      dt-bindings: mtd: physmap: Allow using memory-region to access memory resources
      mtd: physmap: Add support for RAM reserved memory regions

 .../devicetree/bindings/mtd/mtd-physmap.yaml       |  59 ++++++---
 drivers/mtd/maps/physmap-core.c                    |  56 ++++++--
 drivers/of/of_reserved_mem.c                       | 141 +++++++++++++++++++--
 include/linux/of_reserved_mem.h                    |   4 +
 4 files changed, 220 insertions(+), 40 deletions(-)
---
base-commit: b55d426c390f2a4762b2a88b113cc44c37e08e46
change-id: 20251117-mtd-memregion-8830c1dd70ce

Best regards,
-- 
Grégory CLEMENT, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


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

* [PATCH 1/3] of: reserved_mem: Support multiple 'reg' entries for memory-region
  2025-11-17 17:00 [PATCH 0/3] MTD physmap: Adding reserved RAM support and fixing reserved_mem limitations Gregory CLEMENT
@ 2025-11-17 17:00 ` Gregory CLEMENT
  2025-11-17 18:04   ` Rob Herring
  2025-11-17 17:00 ` [PATCH 2/3] dt-bindings: mtd: physmap: Allow using memory-region to access memory resources Gregory CLEMENT
  2025-11-17 17:00 ` [PATCH 3/3] mtd: physmap: Add support for RAM reserved memory regions Gregory CLEMENT
  2 siblings, 1 reply; 7+ messages in thread
From: Gregory CLEMENT @ 2025-11-17 17:00 UTC (permalink / raw)
  To: Rob Herring, Saravana Kannan, Linus Walleij, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Thomas Petazzoni, Vladimir Kondratiev, Benoît Monin,
	Théo Lebrun, devicetree, linux-kernel, linux-arm-kernel,
	linux-mtd, Gregory CLEMENT

The Device Tree specification allows a "memory-region" node to have
multiple 'reg' entries, but the current kernel implementation only
processes the first entry. This can lead to drivers not being able to
access all the reserved memory regions specified in the Device Tree.

This patch extends the reserved memory handling to support multiple
'reg' entries for a single "memory-region" node. The existing exported
functions remain unchanged for backward compatibility, but new APIs
are introduced to allow drivers to access all reserved memory regions.

Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
---
 drivers/of/of_reserved_mem.c    | 141 ++++++++++++++++++++++++++++++++++++----
 include/linux/of_reserved_mem.h |   4 ++
 2 files changed, 133 insertions(+), 12 deletions(-)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 2e9ea751ed2df..2477933883903 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -159,6 +159,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 	int len;
 	const __be32 *prop;
 	bool nomap;
+	int count = 0;
 
 	prop = of_get_flat_dt_prop(node, "reg", &len);
 	if (!prop)
@@ -183,6 +184,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 				dma_contiguous_early_fixup(base, size);
 			pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
 				uname, &base, (unsigned long)(size / SZ_1M));
+			count++;
 		} else {
 			pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
 			       uname, &base, (unsigned long)(size / SZ_1M));
@@ -190,7 +192,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 
 		len -= t_len;
 	}
-	return 0;
+	return count;
 }
 
 /*
@@ -235,7 +237,7 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
 	phys_addr_t base, size;
 	const __be32 *prop;
 	int node, child;
-	int len;
+	int len, i;
 
 	if (!fdt)
 		return;
@@ -273,12 +275,12 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
 		if (len > t_len)
 			pr_warn("%s() ignores %d regions in node '%s'\n",
 				__func__, len / t_len - 1, uname);
-
-		base = dt_mem_next_cell(dt_root_addr_cells, &prop);
-		size = dt_mem_next_cell(dt_root_size_cells, &prop);
-
-		if (size)
-			fdt_reserved_mem_save_node(child, uname, base, size);
+		for (i = 0; i < len; i += t_len) {
+			base = dt_mem_next_cell(dt_root_addr_cells, &prop);
+			size = dt_mem_next_cell(dt_root_size_cells, &prop);
+			if (size)
+				fdt_reserved_mem_save_node(child, uname, base, size);
+		}
 	}
 
 	/* check for overlapping reserved regions */
@@ -308,16 +310,16 @@ int __init fdt_scan_reserved_mem(void)
 
 	fdt_for_each_subnode(child, fdt, node) {
 		const char *uname;
-		int err;
+		int err, ret;
 
 		if (!of_fdt_device_is_available(fdt, child))
 			continue;
 
 		uname = fdt_get_name(fdt, child, NULL);
 
-		err = __reserved_mem_reserve_reg(child, uname);
-		if (!err)
-			count++;
+		ret = __reserved_mem_reserve_reg(child, uname);
+		if (ret > 0)
+			count += ret;
 		/*
 		 * Save the nodes for the dynamically-placed regions
 		 * into an array which will be used for allocation right
@@ -750,6 +752,37 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
 }
 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
 
+/**
+ * of_reserved_mem_array_lookup() - acquire reserved_mem array from a device node
+ * @np:		node pointer of the desired reserved-memory region
+ * @rmrm:	pointer to the first elemennt of the reserved_mem struct of the memory region
+ *
+ * This function allows drivers to acquire a reference to the array of the
+ *  reserved_mem struct based on a device node handle.
+ *
+ * Returns the number reserved_mem elements
+ */
+int of_reserved_mem_array_lookup(struct device_node *np,
+				 struct reserved_mem **rmem)
+{
+	const char *name;
+	int i, count = 0;
+
+	if (!np->full_name)
+		return 0;
+
+	name = kbasename(np->full_name);
+	for (i = 0; i < reserved_mem_count; i++)
+		if (!strcmp(reserved_mem[i].name, name)) {
+			if (!count)
+				*rmem = &reserved_mem[i];
+			count++;
+		}
+
+	return count;
+}
+EXPORT_SYMBOL_GPL(of_reserved_mem_array_lookup);
+
 /**
  * of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource
  * @np:		node containing 'memory-region' property
@@ -785,6 +818,49 @@ int of_reserved_mem_region_to_resource(const struct device_node *np,
 }
 EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource);
 
+/**
+ * of_reserved_mem_region_to_resource_array() - Get a reserved memory region as a resources
+ * @dev:	device associated to the node
+ * @np:		node containing 'memory-region' property
+ * @idx:	index of 'memory-region' property to lookup
+ * @res:	Pointer to an array of struct resource pointers to fill in with reserved regions
+ *
+ * This function allows drivers to lookup a node's 'memory-region' property
+ * entries by index and fill an array of struct resource pointers for the entries.
+ *
+ * Returns the number of resources filled in @res on success.
+ * Returns -ENODEV if 'memory-region' is missing or unavailable,
+ * -EINVAL for any other error.
+ */
+int of_reserved_mem_region_to_resource_array(struct device *dev, const struct device_node *np,
+					     unsigned int idx, struct resource **res)
+{
+	struct reserved_mem *rmem;
+	int count, i;
+	struct resource *r;
+
+	if (!np)
+		return -EINVAL;
+
+	struct device_node __free(device_node) *target = of_parse_phandle(np, "memory-region", idx);
+	if (!target || !of_device_is_available(target))
+		return -ENODEV;
+
+	count = of_reserved_mem_array_lookup(target, &rmem);
+	if (count <= 0)
+		return -EINVAL;
+
+	*res = devm_kzalloc(dev, count * sizeof(struct resource), GFP_KERNEL);
+	r = res[0];
+	for (i = 0; i < count; i++) {
+		resource_set_range(&r[i], rmem[i].base, rmem[i].size);
+		r[i].flags = IORESOURCE_MEM;
+		r[i].name = rmem[i].name;
+	}
+	return count;
+}
+EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource_array);
+
 /**
  * of_reserved_mem_region_to_resource_byname() - Get a reserved memory region as a resource
  * @np:		node containing 'memory-region' property
@@ -829,3 +905,44 @@ int of_reserved_mem_region_count(const struct device_node *np)
 	return of_count_phandle_with_args(np, "memory-region", NULL);
 }
 EXPORT_SYMBOL_GPL(of_reserved_mem_region_count);
+
+/**
+ * of_reserved_mem_region_count() - Return the total number of reserved memory regions
+ * @np:		node containing 'memory-region' property
+ *
+ * This function counts the total number of reserved memory regions referenced
+ * by a node's 'memory-region' property. It iterates over each phandle and sums
+ * the number of regions found in each referenced reserved memory node.
+ *
+ * Returns the total number of reserved memory regions on success.
+ * This function allows drivers to retrieve the number of entries for a node's
+ * 'memory-region' property.
+ *
+ * Returns total number of reserved memory regions on success, or negative error
+ * code on a malformed property.
+ */
+int of_reserved_mem_region_total_count(const struct device_node *np)
+{
+	int nregion = of_count_phandle_with_args(np, "memory-region", NULL);
+	struct device_node *target;
+	int i, nregs = 0;
+
+	for (i = 0; i < nregion; i++) {
+		struct reserved_mem *rmem;
+
+		target = of_parse_phandle(np, "memory-region", i);
+		if (!target)
+			return -ENODEV;
+
+		if (!of_device_is_available(target)) {
+			of_node_put(target);
+			return 0;
+		}
+
+		nregs += of_reserved_mem_array_lookup(target, &rmem);
+
+		of_node_put(target);
+	};
+	return nregs;
+}
+EXPORT_SYMBOL_GPL(of_reserved_mem_region_total_count);
diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
index f573423359f48..1e0c6afddd812 100644
--- a/include/linux/of_reserved_mem.h
+++ b/include/linux/of_reserved_mem.h
@@ -40,11 +40,15 @@ int of_reserved_mem_device_init_by_name(struct device *dev,
 void of_reserved_mem_device_release(struct device *dev);
 
 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np);
+int of_reserved_mem_array_lookup(struct device_node *np, struct reserved_mem **rmem);
 int of_reserved_mem_region_to_resource(const struct device_node *np,
 				       unsigned int idx, struct resource *res);
+int of_reserved_mem_region_to_resource_array(struct device *dev, const struct device_node *np,
+					     unsigned int idx, struct resource **res);
 int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
 					      const char *name, struct resource *res);
 int of_reserved_mem_region_count(const struct device_node *np);
+int of_reserved_mem_region_total_count(const struct device_node *np);
 
 #else
 

-- 
2.51.0


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

* [PATCH 2/3] dt-bindings: mtd: physmap: Allow using memory-region to access memory resources
  2025-11-17 17:00 [PATCH 0/3] MTD physmap: Adding reserved RAM support and fixing reserved_mem limitations Gregory CLEMENT
  2025-11-17 17:00 ` [PATCH 1/3] of: reserved_mem: Support multiple 'reg' entries for memory-region Gregory CLEMENT
@ 2025-11-17 17:00 ` Gregory CLEMENT
  2025-11-17 17:59   ` Rob Herring
  2025-11-17 17:00 ` [PATCH 3/3] mtd: physmap: Add support for RAM reserved memory regions Gregory CLEMENT
  2 siblings, 1 reply; 7+ messages in thread
From: Gregory CLEMENT @ 2025-11-17 17:00 UTC (permalink / raw)
  To: Rob Herring, Saravana Kannan, Linus Walleij, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Thomas Petazzoni, Vladimir Kondratiev, Benoît Monin,
	Théo Lebrun, devicetree, linux-kernel, linux-arm-kernel,
	linux-mtd, Gregory CLEMENT

Enable access to memory resources not only via I/O address using reg,
but also through a portion of main memory using memory-region. To
achieve this, new compatible strings have been introduced: mtd-mem and
mtd-memro.

Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
---
 .../devicetree/bindings/mtd/mtd-physmap.yaml       | 59 +++++++++++++++-------
 1 file changed, 40 insertions(+), 19 deletions(-)

diff --git a/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml b/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
index 1b375dee83b0c..0f75a1204b263 100644
--- a/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
+++ b/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
@@ -13,10 +13,6 @@ description: |
   Flash chips (Memory Technology Devices) are often used for solid state
   file systems on embedded devices.
 
-allOf:
-  - $ref: mtd.yaml#
-  - $ref: /schemas/memory-controllers/mc-peripheral-props.yaml#
-
 properties:
   compatible:
     oneOf:
@@ -61,6 +57,8 @@ properties:
           - jedec-flash
           - mtd-ram
           - mtd-rom
+          - mtd-mem
+          - mtd-memro
 
   reg:
     description: |
@@ -116,6 +114,10 @@ properties:
     minItems: 1
     maxItems: 8
 
+  memory-region:
+    items:
+      - description: Memory regions to map into mtd
+
   '#address-cells':
     const: 1
 
@@ -129,21 +131,25 @@ properties:
 
 required:
   - compatible
-  - reg
-
-if:
-  properties:
-    compatible:
-      contains:
-        const: cortina,gemini-flash
-then:
-  properties:
-    syscon:
-      $ref: /schemas/types.yaml#/definitions/phandle
-      description:
-        Phandle to the syscon controller
-  required:
-    - syscon
+
+allOf:
+  - $ref: mtd.yaml#
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mtd-mem
+              - mtd-memro
+    then:
+      required:
+        - memory-region
+      properties:
+        addr-gpios: false
+    else:
+      $ref: /schemas/memory-controllers/mc-peripheral-props.yaml#
+      required:
+        - reg
 
 unevaluatedProperties: false
 
@@ -223,4 +229,19 @@ examples:
             reg = <0 0x04000000>;
         };
     };
+
+  - |
+    /* An example using mtd-mem */
+    mem_logs: mem_logs@10000800 {
+        reg = <0x1 0x0000800 0x0 0x000f800>;
+        no-map;
+    };
+
+    sram {
+        compatible = "mtd-mem";
+        memory-region = <&mem_log>;
+        bank-width = <4>;
+        device-width = <1>;
+    };
+
 ...

-- 
2.51.0


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

* [PATCH 3/3] mtd: physmap: Add support for RAM reserved memory regions
  2025-11-17 17:00 [PATCH 0/3] MTD physmap: Adding reserved RAM support and fixing reserved_mem limitations Gregory CLEMENT
  2025-11-17 17:00 ` [PATCH 1/3] of: reserved_mem: Support multiple 'reg' entries for memory-region Gregory CLEMENT
  2025-11-17 17:00 ` [PATCH 2/3] dt-bindings: mtd: physmap: Allow using memory-region to access memory resources Gregory CLEMENT
@ 2025-11-17 17:00 ` Gregory CLEMENT
  2 siblings, 0 replies; 7+ messages in thread
From: Gregory CLEMENT @ 2025-11-17 17:00 UTC (permalink / raw)
  To: Rob Herring, Saravana Kannan, Linus Walleij, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Thomas Petazzoni, Vladimir Kondratiev, Benoît Monin,
	Théo Lebrun, devicetree, linux-kernel, linux-arm-kernel,
	linux-mtd, Gregory CLEMENT

MTD fixed-partitions can now be exposed for reserved RAM regions, not
just ROM reg regions. This is achieved by using a memory-region
phandle to reference the reserved RAM region instead of the reg
property.

Based on the work of Muhammad Musa <muhammad.musa@intel.com>

Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
---
 drivers/mtd/maps/physmap-core.c | 56 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 47 insertions(+), 9 deletions(-)

diff --git a/drivers/mtd/maps/physmap-core.c b/drivers/mtd/maps/physmap-core.c
index 2bd7a1af898c9..f084f6f34d611 100644
--- a/drivers/mtd/maps/physmap-core.c
+++ b/drivers/mtd/maps/physmap-core.c
@@ -39,6 +39,7 @@
 #include <linux/mtd/cfi_endian.h>
 #include <linux/io.h>
 #include <linux/of.h>
+#include <linux/of_reserved_mem.h>
 #include <linux/pm_runtime.h>
 #include <linux/gpio/consumer.h>
 
@@ -263,6 +264,14 @@ static const struct of_device_id of_flash_match[] = {
 		.type = "rom",
 		.compatible = "direct-mapped"
 	},
+	{
+		.compatible = "mtd-mem",
+		.data = "map_ram",
+	},
+	{
+		.compatible = "mtd-memro",
+		.data = "map_rom",
+	},
 	{ /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, of_flash_match);
@@ -446,8 +455,9 @@ static int physmap_flash_pdata_init(struct platform_device *dev)
 static int physmap_flash_probe(struct platform_device *dev)
 {
 	struct physmap_flash_info *info;
-	int err = 0;
-	int i;
+	struct resource *res_array;
+	int err = 0, is_rsvd_mem = 0, nreg = 0;
+	int i, curr_reg;
 
 	if (!dev->dev.of_node && !dev_get_platdata(&dev->dev))
 		return -EINVAL;
@@ -459,9 +469,13 @@ static int physmap_flash_probe(struct platform_device *dev)
 	while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps))
 		info->nmaps++;
 
-	if (!info->nmaps)
-		return -ENODEV;
-
+	if (!info->nmaps) {
+		info->nmaps = of_reserved_mem_region_total_count(dev->dev.of_node);
+		if (info->nmaps > 0)
+			is_rsvd_mem = 1;
+		else
+			return -ENODEV;
+	}
 	info->maps = devm_kzalloc(&dev->dev,
 				  sizeof(*info->maps) * info->nmaps,
 				  GFP_KERNEL);
@@ -503,7 +517,23 @@ static int physmap_flash_probe(struct platform_device *dev)
 	for (i = 0; i < info->nmaps; i++) {
 		struct resource *res;
 
-		info->maps[i].virt = devm_platform_get_and_ioremap_resource(dev, i, &res);
+		if (is_rsvd_mem) {
+			if (nreg <= i) {
+				int cnt = of_reserved_mem_region_to_resource_array(&dev->dev,
+							   dev->dev.of_node, i, &res_array);
+				if (cnt < 0) {
+					err = cnt;
+					goto err_out;
+				}
+				nreg += cnt;
+				curr_reg = 0;
+			}
+			res = &res_array[curr_reg++];
+			info->maps[i].virt = devm_ioremap_resource(&dev->dev, res);
+		} else {
+			info->maps[i].virt = devm_platform_get_and_ioremap_resource(dev, i, &res);
+		}
+
 		if (IS_ERR(info->maps[i].virt)) {
 			err = PTR_ERR(info->maps[i].virt);
 			goto err_out;
@@ -519,9 +549,17 @@ static int physmap_flash_probe(struct platform_device *dev)
 			info->maps[i].phys = res->start;
 
 		info->win_order = fls64(resource_size(res)) - 1;
-		info->maps[i].size = BIT(info->win_order +
-					 (info->gpios ?
-					  info->gpios->ndescs : 0));
+		/* When using a memory region, the size is not necessarily a
+		 * power of 2, so win_order is not applicable. Since GPIOs are
+		 * unavailable in this context, directly using the region's size
+		 * is safe.
+		 */
+		if (is_rsvd_mem)
+			info->maps[i].size = resource_size(res);
+		else
+			info->maps[i].size = BIT(info->win_order +
+						 (info->gpios ?
+						  info->gpios->ndescs : 0));
 
 		info->maps[i].map_priv_1 = (unsigned long)dev;
 

-- 
2.51.0


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

* Re: [PATCH 2/3] dt-bindings: mtd: physmap: Allow using memory-region to access memory resources
  2025-11-17 17:00 ` [PATCH 2/3] dt-bindings: mtd: physmap: Allow using memory-region to access memory resources Gregory CLEMENT
@ 2025-11-17 17:59   ` Rob Herring
  2025-11-19  9:47     ` Gregory CLEMENT
  0 siblings, 1 reply; 7+ messages in thread
From: Rob Herring @ 2025-11-17 17:59 UTC (permalink / raw)
  To: Gregory CLEMENT
  Cc: Saravana Kannan, Linus Walleij, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Krzysztof Kozlowski, Conor Dooley,
	Thomas Petazzoni, Vladimir Kondratiev, Benoît Monin,
	Théo Lebrun, devicetree, linux-kernel, linux-arm-kernel,
	linux-mtd

On Mon, Nov 17, 2025 at 06:00:15PM +0100, Gregory CLEMENT wrote:
> Enable access to memory resources not only via I/O address using reg,
> but also through a portion of main memory using memory-region. To
> achieve this, new compatible strings have been introduced: mtd-mem and
> mtd-memro.
> 
> Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
> ---
>  .../devicetree/bindings/mtd/mtd-physmap.yaml       | 59 +++++++++++++++-------
>  1 file changed, 40 insertions(+), 19 deletions(-)
> 
> diff --git a/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml b/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
> index 1b375dee83b0c..0f75a1204b263 100644
> --- a/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
> +++ b/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
> @@ -13,10 +13,6 @@ description: |
>    Flash chips (Memory Technology Devices) are often used for solid state
>    file systems on embedded devices.
>  
> -allOf:
> -  - $ref: mtd.yaml#
> -  - $ref: /schemas/memory-controllers/mc-peripheral-props.yaml#
> -
>  properties:
>    compatible:
>      oneOf:
> @@ -61,6 +57,8 @@ properties:
>            - jedec-flash
>            - mtd-ram
>            - mtd-rom
> +          - mtd-mem
> +          - mtd-memro

I thought we had a flag for read only.

>  
>    reg:
>      description: |
> @@ -116,6 +114,10 @@ properties:
>      minItems: 1
>      maxItems: 8
>  
> +  memory-region:
> +    items:
> +      - description: Memory regions to map into mtd
> +
>    '#address-cells':
>      const: 1
>  
> @@ -129,21 +131,25 @@ properties:
>  
>  required:
>    - compatible
> -  - reg
> -
> -if:
> -  properties:
> -    compatible:
> -      contains:
> -        const: cortina,gemini-flash
> -then:
> -  properties:
> -    syscon:
> -      $ref: /schemas/types.yaml#/definitions/phandle
> -      description:
> -        Phandle to the syscon controller
> -  required:
> -    - syscon
> +
> +allOf:
> +  - $ref: mtd.yaml#
> +  - if:
> +      properties:
> +        compatible:
> +          contains:
> +            enum:
> +              - mtd-mem
> +              - mtd-memro
> +    then:
> +      required:
> +        - memory-region
> +      properties:
> +        addr-gpios: false
> +    else:
> +      $ref: /schemas/memory-controllers/mc-peripheral-props.yaml#
> +      required:
> +        - reg
>  
>  unevaluatedProperties: false
>  
> @@ -223,4 +229,19 @@ examples:
>              reg = <0 0x04000000>;
>          };
>      };
> +
> +  - |
> +    /* An example using mtd-mem */
> +    mem_logs: mem_logs@10000800 {
> +        reg = <0x1 0x0000800 0x0 0x000f800>;
> +        no-map;
> +    };
> +
> +    sram {

It's really an abuse of /reserved-memory to define regions outside of 
what's defined in /memory nodes (or whatever defines system memory). Is 
that the case here with the suspicious 'sram'?

If we do keep this, I'd rather just add the properties below into the 
/reserved-memory node itself. Devices are created for those nodes if 
they have 'compatible'.


> +        compatible = "mtd-mem";
> +        memory-region = <&mem_log>;
> +        bank-width = <4>;
> +        device-width = <1>;
> +    };
> +
>  ...
> 
> -- 
> 2.51.0
> 

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

* Re: [PATCH 1/3] of: reserved_mem: Support multiple 'reg' entries for memory-region
  2025-11-17 17:00 ` [PATCH 1/3] of: reserved_mem: Support multiple 'reg' entries for memory-region Gregory CLEMENT
@ 2025-11-17 18:04   ` Rob Herring
  0 siblings, 0 replies; 7+ messages in thread
From: Rob Herring @ 2025-11-17 18:04 UTC (permalink / raw)
  To: Gregory CLEMENT
  Cc: Saravana Kannan, Linus Walleij, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Krzysztof Kozlowski, Conor Dooley,
	Thomas Petazzoni, Vladimir Kondratiev, Benoît Monin,
	Théo Lebrun, devicetree, linux-kernel, linux-arm-kernel,
	linux-mtd

On Mon, Nov 17, 2025 at 06:00:14PM +0100, Gregory CLEMENT wrote:
> The Device Tree specification allows a "memory-region" node to have
> multiple 'reg' entries, but the current kernel implementation only
> processes the first entry. This can lead to drivers not being able to
> access all the reserved memory regions specified in the Device Tree.
> 
> This patch extends the reserved memory handling to support multiple
> 'reg' entries for a single "memory-region" node. The existing exported
> functions remain unchanged for backward compatibility, but new APIs
> are introduced to allow drivers to access all reserved memory regions.
> 
> Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
> ---
>  drivers/of/of_reserved_mem.c    | 141 ++++++++++++++++++++++++++++++++++++----
>  include/linux/of_reserved_mem.h |   4 ++
>  2 files changed, 133 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
> index 2e9ea751ed2df..2477933883903 100644
> --- a/drivers/of/of_reserved_mem.c
> +++ b/drivers/of/of_reserved_mem.c
> @@ -159,6 +159,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>  	int len;
>  	const __be32 *prop;
>  	bool nomap;
> +	int count = 0;
>  
>  	prop = of_get_flat_dt_prop(node, "reg", &len);
>  	if (!prop)
> @@ -183,6 +184,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>  				dma_contiguous_early_fixup(base, size);
>  			pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
>  				uname, &base, (unsigned long)(size / SZ_1M));
> +			count++;
>  		} else {
>  			pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
>  			       uname, &base, (unsigned long)(size / SZ_1M));
> @@ -190,7 +192,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>  
>  		len -= t_len;
>  	}
> -	return 0;
> +	return count;
>  }
>  
>  /*
> @@ -235,7 +237,7 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
>  	phys_addr_t base, size;
>  	const __be32 *prop;
>  	int node, child;
> -	int len;
> +	int len, i;
>  
>  	if (!fdt)
>  		return;
> @@ -273,12 +275,12 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
>  		if (len > t_len)
>  			pr_warn("%s() ignores %d regions in node '%s'\n",
>  				__func__, len / t_len - 1, uname);
> -
> -		base = dt_mem_next_cell(dt_root_addr_cells, &prop);
> -		size = dt_mem_next_cell(dt_root_size_cells, &prop);

FYI, there's some changes around this code in flight.

> -
> -		if (size)
> -			fdt_reserved_mem_save_node(child, uname, base, size);
> +		for (i = 0; i < len; i += t_len) {
> +			base = dt_mem_next_cell(dt_root_addr_cells, &prop);
> +			size = dt_mem_next_cell(dt_root_size_cells, &prop);
> +			if (size)
> +				fdt_reserved_mem_save_node(child, uname, base, size);
> +		}
>  	}
>  
>  	/* check for overlapping reserved regions */
> @@ -308,16 +310,16 @@ int __init fdt_scan_reserved_mem(void)
>  
>  	fdt_for_each_subnode(child, fdt, node) {
>  		const char *uname;
> -		int err;
> +		int err, ret;
>  
>  		if (!of_fdt_device_is_available(fdt, child))
>  			continue;
>  
>  		uname = fdt_get_name(fdt, child, NULL);
>  
> -		err = __reserved_mem_reserve_reg(child, uname);
> -		if (!err)
> -			count++;
> +		ret = __reserved_mem_reserve_reg(child, uname);
> +		if (ret > 0)
> +			count += ret;
>  		/*
>  		 * Save the nodes for the dynamically-placed regions
>  		 * into an array which will be used for allocation right
> @@ -750,6 +752,37 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
>  }
>  EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
>  
> +/**
> + * of_reserved_mem_array_lookup() - acquire reserved_mem array from a device node
> + * @np:		node pointer of the desired reserved-memory region
> + * @rmrm:	pointer to the first elemennt of the reserved_mem struct of the memory region
> + *
> + * This function allows drivers to acquire a reference to the array of the
> + *  reserved_mem struct based on a device node handle.
> + *
> + * Returns the number reserved_mem elements
> + */
> +int of_reserved_mem_array_lookup(struct device_node *np,
> +				 struct reserved_mem **rmem)
> +{
> +	const char *name;
> +	int i, count = 0;
> +
> +	if (!np->full_name)
> +		return 0;
> +
> +	name = kbasename(np->full_name);
> +	for (i = 0; i < reserved_mem_count; i++)
> +		if (!strcmp(reserved_mem[i].name, name)) {
> +			if (!count)
> +				*rmem = &reserved_mem[i];
> +			count++;
> +		}
> +
> +	return count;
> +}
> +EXPORT_SYMBOL_GPL(of_reserved_mem_array_lookup);

Do you need this function? I'm trying to do away with drivers using 
struct reserved_mem in favor of the resource functions.

> +
>  /**
>   * of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource
>   * @np:		node containing 'memory-region' property
> @@ -785,6 +818,49 @@ int of_reserved_mem_region_to_resource(const struct device_node *np,
>  }
>  EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource);
>  
> +/**
> + * of_reserved_mem_region_to_resource_array() - Get a reserved memory region as a resources
> + * @dev:	device associated to the node
> + * @np:		node containing 'memory-region' property
> + * @idx:	index of 'memory-region' property to lookup
> + * @res:	Pointer to an array of struct resource pointers to fill in with reserved regions
> + *
> + * This function allows drivers to lookup a node's 'memory-region' property
> + * entries by index and fill an array of struct resource pointers for the entries.
> + *
> + * Returns the number of resources filled in @res on success.
> + * Returns -ENODEV if 'memory-region' is missing or unavailable,
> + * -EINVAL for any other error.
> + */
> +int of_reserved_mem_region_to_resource_array(struct device *dev, const struct device_node *np,
> +					     unsigned int idx, struct resource **res)
> +{
> +	struct reserved_mem *rmem;
> +	int count, i;
> +	struct resource *r;
> +
> +	if (!np)
> +		return -EINVAL;
> +
> +	struct device_node __free(device_node) *target = of_parse_phandle(np, "memory-region", idx);
> +	if (!target || !of_device_is_available(target))
> +		return -ENODEV;
> +
> +	count = of_reserved_mem_array_lookup(target, &rmem);
> +	if (count <= 0)
> +		return -EINVAL;
> +
> +	*res = devm_kzalloc(dev, count * sizeof(struct resource), GFP_KERNEL);
> +	r = res[0];
> +	for (i = 0; i < count; i++) {
> +		resource_set_range(&r[i], rmem[i].base, rmem[i].size);
> +		r[i].flags = IORESOURCE_MEM;
> +		r[i].name = rmem[i].name;
> +	}
> +	return count;
> +}
> +EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource_array);
> +
>  /**
>   * of_reserved_mem_region_to_resource_byname() - Get a reserved memory region as a resource
>   * @np:		node containing 'memory-region' property
> @@ -829,3 +905,44 @@ int of_reserved_mem_region_count(const struct device_node *np)
>  	return of_count_phandle_with_args(np, "memory-region", NULL);
>  }
>  EXPORT_SYMBOL_GPL(of_reserved_mem_region_count);
> +
> +/**
> + * of_reserved_mem_region_count() - Return the total number of reserved memory regions
> + * @np:		node containing 'memory-region' property
> + *
> + * This function counts the total number of reserved memory regions referenced
> + * by a node's 'memory-region' property. It iterates over each phandle and sums
> + * the number of regions found in each referenced reserved memory node.
> + *
> + * Returns the total number of reserved memory regions on success.
> + * This function allows drivers to retrieve the number of entries for a node's
> + * 'memory-region' property.
> + *
> + * Returns total number of reserved memory regions on success, or negative error
> + * code on a malformed property.
> + */
> +int of_reserved_mem_region_total_count(const struct device_node *np)
> +{
> +	int nregion = of_count_phandle_with_args(np, "memory-region", NULL);
> +	struct device_node *target;
> +	int i, nregs = 0;
> +
> +	for (i = 0; i < nregion; i++) {
> +		struct reserved_mem *rmem;
> +
> +		target = of_parse_phandle(np, "memory-region", i);
> +		if (!target)
> +			return -ENODEV;
> +
> +		if (!of_device_is_available(target)) {
> +			of_node_put(target);
> +			return 0;
> +		}
> +
> +		nregs += of_reserved_mem_array_lookup(target, &rmem);
> +
> +		of_node_put(target);
> +	};
> +	return nregs;
> +}
> +EXPORT_SYMBOL_GPL(of_reserved_mem_region_total_count);

Do you have a user?

Rob

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

* Re: [PATCH 2/3] dt-bindings: mtd: physmap: Allow using memory-region to access memory resources
  2025-11-17 17:59   ` Rob Herring
@ 2025-11-19  9:47     ` Gregory CLEMENT
  0 siblings, 0 replies; 7+ messages in thread
From: Gregory CLEMENT @ 2025-11-19  9:47 UTC (permalink / raw)
  To: Rob Herring
  Cc: Saravana Kannan, Linus Walleij, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Krzysztof Kozlowski, Conor Dooley,
	Thomas Petazzoni, Vladimir Kondratiev, Benoît Monin,
	Théo Lebrun, devicetree, linux-kernel, linux-arm-kernel,
	linux-mtd

Rob Herring <robh@kernel.org> writes:

> On Mon, Nov 17, 2025 at 06:00:15PM +0100, Gregory CLEMENT wrote:
>> Enable access to memory resources not only via I/O address using reg,
>> but also through a portion of main memory using memory-region. To
>> achieve this, new compatible strings have been introduced: mtd-mem and
>> mtd-memro.
>> 
>> Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com>
>> ---
>>  .../devicetree/bindings/mtd/mtd-physmap.yaml       | 59 +++++++++++++++-------
>>  1 file changed, 40 insertions(+), 19 deletions(-)
>> 
>> diff --git a/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml b/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
>> index 1b375dee83b0c..0f75a1204b263 100644
>> --- a/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
>> +++ b/Documentation/devicetree/bindings/mtd/mtd-physmap.yaml
>> @@ -13,10 +13,6 @@ description: |
>>    Flash chips (Memory Technology Devices) are often used for solid state
>>    file systems on embedded devices.
>>  
>> -allOf:
>> -  - $ref: mtd.yaml#
>> -  - $ref: /schemas/memory-controllers/mc-peripheral-props.yaml#
>> -
>>  properties:
>>    compatible:
>>      oneOf:
>> @@ -61,6 +57,8 @@ properties:
>>            - jedec-flash
>>            - mtd-ram
>>            - mtd-rom
>> +          - mtd-mem
>> +          - mtd-memro
>
> I thought we had a flag for read only.

The read-only flag is set at the partition level, not the device level.

>
>>  
>>    reg:
>>      description: |
>> @@ -116,6 +114,10 @@ properties:
>>      minItems: 1
>>      maxItems: 8
>>  
>> +  memory-region:
>> +    items:
>> +      - description: Memory regions to map into mtd
>> +
>>    '#address-cells':
>>      const: 1
>>  
>> @@ -129,21 +131,25 @@ properties:
>>  
>>  required:
>>    - compatible
>> -  - reg
>> -
>> -if:
>> -  properties:
>> -    compatible:
>> -      contains:
>> -        const: cortina,gemini-flash
>> -then:
>> -  properties:
>> -    syscon:
>> -      $ref: /schemas/types.yaml#/definitions/phandle
>> -      description:
>> -        Phandle to the syscon controller
>> -  required:
>> -    - syscon
>> +
>> +allOf:
>> +  - $ref: mtd.yaml#
>> +  - if:
>> +      properties:
>> +        compatible:
>> +          contains:
>> +            enum:
>> +              - mtd-mem
>> +              - mtd-memro
>> +    then:
>> +      required:
>> +        - memory-region
>> +      properties:
>> +        addr-gpios: false
>> +    else:
>> +      $ref: /schemas/memory-controllers/mc-peripheral-props.yaml#
>> +      required:
>> +        - reg
>>  
>>  unevaluatedProperties: false
>>  
>> @@ -223,4 +229,19 @@ examples:
>>              reg = <0 0x04000000>;
>>          };
>>      };
>> +
>> +  - |
>> +    /* An example using mtd-mem */
>> +    mem_logs: mem_logs@10000800 {
>> +        reg = <0x1 0x0000800 0x0 0x000f800>;
>> +        no-map;
>> +    };
>> +
>> +    sram {
>
> It's really an abuse of /reserved-memory to define regions outside of 
> what's defined in /memory nodes (or whatever defines system memory). Is 
> that the case here with the suspicious 'sram'?
>
> If we do keep this, I'd rather just add the properties below into the 
> /reserved-memory node itself. Devices are created for those nodes if 
> they have 'compatible'.

The name is indeed misleading. I drew inspiration from the example
above, but the actual use case was to share data between the kernel and
bootloader, for instance. Therefore, it is truly part of the main
memory. I will modify the name accordingly.

Thanks,

Gregory

>
>
>> +        compatible = "mtd-mem";
>> +        memory-region = <&mem_log>;
>> +        bank-width = <4>;
>> +        device-width = <1>;
>> +    };
>> +
>>  ...
>> 
>> -- 
>> 2.51.0
>> 

-- 
Grégory CLEMENT, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

end of thread, other threads:[~2025-11-19  9:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-17 17:00 [PATCH 0/3] MTD physmap: Adding reserved RAM support and fixing reserved_mem limitations Gregory CLEMENT
2025-11-17 17:00 ` [PATCH 1/3] of: reserved_mem: Support multiple 'reg' entries for memory-region Gregory CLEMENT
2025-11-17 18:04   ` Rob Herring
2025-11-17 17:00 ` [PATCH 2/3] dt-bindings: mtd: physmap: Allow using memory-region to access memory resources Gregory CLEMENT
2025-11-17 17:59   ` Rob Herring
2025-11-19  9:47     ` Gregory CLEMENT
2025-11-17 17:00 ` [PATCH 3/3] mtd: physmap: Add support for RAM reserved memory regions Gregory CLEMENT

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).