linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] nvmem: patches (set 1) for 5.12
@ 2021-01-29 17:14 Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 1/5] nvmem: core: Fix a resource leak on error in nvmem_add_cells_from_of() Srinivas Kandagatla
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Srinivas Kandagatla @ 2021-01-29 17:14 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel, Srinivas Kandagatla

Hi Greg,

Here are some nvmem patches for 5.12 which includes
- adding support to new rmem nvmem provider
- a improvement in core to skip invalid node and a fix a leak
- patch in imx driver to use of_device_get_match_data

Can you please queue them up for 5.12.

thanks for you help,
srini

Ahmad Fatoum (1):
  nvmem: core: skip child nodes not matching binding

Dan Carpenter (1):
  nvmem: core: Fix a resource leak on error in nvmem_add_cells_from_of()

Fabio Estevam (1):
  nvmem: imx-iim: Use of_device_get_match_data()

Nicolas Saenz Julienne (2):
  dt-bindings: nvmem: Add bindings for rmem driver
  nvmem: Add driver to expose reserved memory as nvmem

 .../devicetree/bindings/nvmem/rmem.yaml       | 49 ++++++++++
 drivers/nvmem/Kconfig                         |  8 ++
 drivers/nvmem/Makefile                        |  2 +
 drivers/nvmem/core.c                          |  5 +-
 drivers/nvmem/imx-iim.c                       |  7 +-
 drivers/nvmem/rmem.c                          | 97 +++++++++++++++++++
 drivers/of/platform.c                         |  1 +
 7 files changed, 162 insertions(+), 7 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/nvmem/rmem.yaml
 create mode 100644 drivers/nvmem/rmem.c

-- 
2.21.0


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

* [PATCH 1/5] nvmem: core: Fix a resource leak on error in nvmem_add_cells_from_of()
  2021-01-29 17:14 [PATCH 0/5] nvmem: patches (set 1) for 5.12 Srinivas Kandagatla
@ 2021-01-29 17:14 ` Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 2/5] nvmem: imx-iim: Use of_device_get_match_data() Srinivas Kandagatla
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Srinivas Kandagatla @ 2021-01-29 17:14 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel, Dan Carpenter, Srinivas Kandagatla

From: Dan Carpenter <dan.carpenter@oracle.com>

This doesn't call of_node_put() on the error path so it leads to a
memory leak.

Fixes: 0749aa25af82 ("nvmem: core: fix regression in of_nvmem_cell_get()")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 177f5bf27c6d..68ae6f24b57f 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -713,6 +713,7 @@ static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
 				cell->name, nvmem->stride);
 			/* Cells already added will be freed later. */
 			kfree_const(cell->name);
+			of_node_put(cell->np);
 			kfree(cell);
 			return -EINVAL;
 		}
-- 
2.21.0


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

* [PATCH 2/5] nvmem: imx-iim: Use of_device_get_match_data()
  2021-01-29 17:14 [PATCH 0/5] nvmem: patches (set 1) for 5.12 Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 1/5] nvmem: core: Fix a resource leak on error in nvmem_add_cells_from_of() Srinivas Kandagatla
@ 2021-01-29 17:14 ` Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 3/5] dt-bindings: nvmem: Add bindings for rmem driver Srinivas Kandagatla
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Srinivas Kandagatla @ 2021-01-29 17:14 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel, Fabio Estevam, Srinivas Kandagatla

From: Fabio Estevam <festevam@gmail.com>

The retrieval of driver data via of_device_get_match_data() can make
the code simpler.

Use of_device_get_match_data() to simplify the code.

Signed-off-by: Fabio Estevam <festevam@gmail.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/imx-iim.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/drivers/nvmem/imx-iim.c b/drivers/nvmem/imx-iim.c
index 701704b87dc9..c86339a7f583 100644
--- a/drivers/nvmem/imx-iim.c
+++ b/drivers/nvmem/imx-iim.c
@@ -96,7 +96,6 @@ MODULE_DEVICE_TABLE(of, imx_iim_dt_ids);
 
 static int imx_iim_probe(struct platform_device *pdev)
 {
-	const struct of_device_id *of_id;
 	struct device *dev = &pdev->dev;
 	struct iim_priv *iim;
 	struct nvmem_device *nvmem;
@@ -111,11 +110,7 @@ static int imx_iim_probe(struct platform_device *pdev)
 	if (IS_ERR(iim->base))
 		return PTR_ERR(iim->base);
 
-	of_id = of_match_device(imx_iim_dt_ids, dev);
-	if (!of_id)
-		return -ENODEV;
-
-	drvdata = of_id->data;
+	drvdata = of_device_get_match_data(&pdev->dev);
 
 	iim->clk = devm_clk_get(dev, NULL);
 	if (IS_ERR(iim->clk))
-- 
2.21.0


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

* [PATCH 3/5] dt-bindings: nvmem: Add bindings for rmem driver
  2021-01-29 17:14 [PATCH 0/5] nvmem: patches (set 1) for 5.12 Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 1/5] nvmem: core: Fix a resource leak on error in nvmem_add_cells_from_of() Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 2/5] nvmem: imx-iim: Use of_device_get_match_data() Srinivas Kandagatla
@ 2021-01-29 17:14 ` Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 4/5] nvmem: Add driver to expose reserved memory as nvmem Srinivas Kandagatla
  2021-01-29 17:14 ` [PATCH 5/5] nvmem: core: skip child nodes not matching binding Srinivas Kandagatla
  4 siblings, 0 replies; 7+ messages in thread
From: Srinivas Kandagatla @ 2021-01-29 17:14 UTC (permalink / raw)
  To: gregkh
  Cc: linux-kernel, Nicolas Saenz Julienne, Rob Herring, Srinivas Kandagatla

From: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>

Firmware/co-processors might use reserved memory areas in order to pass
data stemming from an nvmem device otherwise non accessible to Linux.
For example an EEPROM memory only physically accessible to firmware, or
data only accessible early at boot time.

Introduce the dt-bindings to nvmem's rmem.

Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 .../devicetree/bindings/nvmem/rmem.yaml       | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/rmem.yaml

diff --git a/Documentation/devicetree/bindings/nvmem/rmem.yaml b/Documentation/devicetree/bindings/nvmem/rmem.yaml
new file mode 100644
index 000000000000..1d85a0a30846
--- /dev/null
+++ b/Documentation/devicetree/bindings/nvmem/rmem.yaml
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/nvmem/rmem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Reserved Memory Based nvmem Device
+
+maintainers:
+  - Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
+
+allOf:
+  - $ref: "nvmem.yaml#"
+
+properties:
+  compatible:
+    items:
+      - enum:
+          - raspberrypi,bootloader-config
+      - const: nvmem-rmem
+
+  no-map:
+    $ref: /schemas/types.yaml#/definitions/flag
+    description:
+      Avoid creating a virtual mapping of the region as part of the OS'
+      standard mapping of system memory.
+
+required:
+  - compatible
+  - no-map
+
+unevaluatedProperties: false
+
+examples:
+  - |
+        reserved-memory {
+                #address-cells = <1>;
+                #size-cells = <1>;
+
+                blconfig: nvram@10000000 {
+                        compatible = "raspberrypi,bootloader-config", "nvmem-rmem";
+                        #address-cells = <1>;
+                        #size-cells = <1>;
+                        reg = <0x10000000 0x1000>;
+                        no-map;
+                };
+        };
+
+...
-- 
2.21.0


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

* [PATCH 4/5] nvmem: Add driver to expose reserved memory as nvmem
  2021-01-29 17:14 [PATCH 0/5] nvmem: patches (set 1) for 5.12 Srinivas Kandagatla
                   ` (2 preceding siblings ...)
  2021-01-29 17:14 ` [PATCH 3/5] dt-bindings: nvmem: Add bindings for rmem driver Srinivas Kandagatla
@ 2021-01-29 17:14 ` Srinivas Kandagatla
  2021-01-29 17:51   ` Randy Dunlap
  2021-01-29 17:14 ` [PATCH 5/5] nvmem: core: skip child nodes not matching binding Srinivas Kandagatla
  4 siblings, 1 reply; 7+ messages in thread
From: Srinivas Kandagatla @ 2021-01-29 17:14 UTC (permalink / raw)
  To: gregkh
  Cc: linux-kernel, Nicolas Saenz Julienne, Rob Herring, Tim Gover,
	Srinivas Kandagatla

From: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>

Firmware/co-processors might use reserved memory areas in order to pass
data stemming from an nvmem device otherwise non accessible to Linux.
For example an EEPROM memory only physically accessible to firmware, or
data only accessible early at boot time.

In order to expose this data to other drivers and user-space, the driver
models the reserved memory area as an nvmem device.

Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
Reviewed-by: Rob Herring <robh@kernel.org>
Tested-by: Tim Gover <tim.gover@raspberrypi.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/Kconfig  |  8 ++++
 drivers/nvmem/Makefile |  2 +
 drivers/nvmem/rmem.c   | 97 ++++++++++++++++++++++++++++++++++++++++++
 drivers/of/platform.c  |  1 +
 4 files changed, 108 insertions(+)
 create mode 100644 drivers/nvmem/rmem.c

diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index 954d3b4a52ab..fecc19b884bf 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -270,4 +270,12 @@ config SPRD_EFUSE
 	  This driver can also be built as a module. If so, the module
 	  will be called nvmem-sprd-efuse.
 
+config NVMEM_RMEM
+	tristate "Reserved Memory Based Driver Support"
+	help
+	  This drivers maps reserved memory into an nvmem device. It might be
+	  useful to expose information left by firmware in memory.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called nvmem-rmem.
 endif
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index a7c377218341..5376b8e0dae5 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -55,3 +55,5 @@ obj-$(CONFIG_NVMEM_ZYNQMP)	+= nvmem_zynqmp_nvmem.o
 nvmem_zynqmp_nvmem-y		:= zynqmp_nvmem.o
 obj-$(CONFIG_SPRD_EFUSE)	+= nvmem_sprd_efuse.o
 nvmem_sprd_efuse-y		:= sprd-efuse.o
+obj-$(CONFIG_NVMEM_RMEM) 	+= nvmem-rmem.o
+nvmem-rmem-y			:= rmem.o
diff --git a/drivers/nvmem/rmem.c b/drivers/nvmem/rmem.c
new file mode 100644
index 000000000000..b11c3c974b3d
--- /dev/null
+++ b/drivers/nvmem/rmem.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
+ */
+
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/nvmem-provider.h>
+#include <linux/of_reserved_mem.h>
+#include <linux/platform_device.h>
+
+struct rmem {
+	struct device *dev;
+	struct nvmem_device *nvmem;
+	struct reserved_mem *mem;
+
+	phys_addr_t size;
+};
+
+static int rmem_read(void *context, unsigned int offset,
+		     void *val, size_t bytes)
+{
+	struct rmem *priv = context;
+	size_t available = priv->mem->size;
+	loff_t off = offset;
+	void *addr;
+	int count;
+
+	/*
+	 * Only map the reserved memory at this point to avoid potential rogue
+	 * kernel threads inadvertently modifying it. Based on the current
+	 * uses-cases for this driver, the performance hit isn't a concern.
+	 * Nor is likely to be, given the nature of the subsystem. Most nvmem
+	 * devices operate over slow buses to begin with.
+	 *
+	 * An alternative would be setting the memory as RO, set_memory_ro(),
+	 * but as of Dec 2020 this isn't possible on arm64.
+	 */
+	addr = memremap(priv->mem->base, available, MEMREMAP_WB);
+	if (IS_ERR(addr)) {
+		dev_err(priv->dev, "Failed to remap memory region\n");
+		return PTR_ERR(addr);
+	}
+
+	count = memory_read_from_buffer(val, bytes, &off, addr, available);
+
+	memunmap(addr);
+
+	return count;
+}
+
+static int rmem_probe(struct platform_device *pdev)
+{
+	struct nvmem_config config = { };
+	struct device *dev = &pdev->dev;
+	struct reserved_mem *mem;
+	struct rmem *priv;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+	priv->dev = dev;
+
+	mem = of_reserved_mem_lookup(dev->of_node);
+	if (!mem) {
+		dev_err(dev, "Failed to lookup reserved memory\n");
+		return -EINVAL;
+	}
+	priv->mem = mem;
+
+	config.dev = dev;
+	config.priv = priv;
+	config.name = "rmem";
+	config.size = mem->size;
+	config.reg_read = rmem_read;
+
+	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
+}
+
+static const struct of_device_id rmem_match[] = {
+	{ .compatible = "nvmem-rmem", },
+	{ /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, rmem_match);
+
+static struct platform_driver rmem_driver = {
+	.probe = rmem_probe,
+	.driver = {
+		.name = "rmem",
+		.of_match_table = rmem_match,
+	},
+};
+module_platform_driver(rmem_driver);
+
+MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
+MODULE_DESCRIPTION("Reserved Memory Based nvmem Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
index 79bd5f5a1bf1..6699cdbe58b6 100644
--- a/drivers/of/platform.c
+++ b/drivers/of/platform.c
@@ -511,6 +511,7 @@ static const struct of_device_id reserved_mem_matches[] = {
 	{ .compatible = "qcom,rmtfs-mem" },
 	{ .compatible = "qcom,cmd-db" },
 	{ .compatible = "ramoops" },
+	{ .compatible = "nvmem-rmem" },
 	{}
 };
 
-- 
2.21.0


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

* [PATCH 5/5] nvmem: core: skip child nodes not matching binding
  2021-01-29 17:14 [PATCH 0/5] nvmem: patches (set 1) for 5.12 Srinivas Kandagatla
                   ` (3 preceding siblings ...)
  2021-01-29 17:14 ` [PATCH 4/5] nvmem: Add driver to expose reserved memory as nvmem Srinivas Kandagatla
@ 2021-01-29 17:14 ` Srinivas Kandagatla
  4 siblings, 0 replies; 7+ messages in thread
From: Srinivas Kandagatla @ 2021-01-29 17:14 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel, Ahmad Fatoum, Srinivas Kandagatla

From: Ahmad Fatoum <a.fatoum@pengutronix.de>

The nvmem cell binding applies to all eeprom child nodes matching
"^.*@[0-9a-f]+$" without taking a compatible into account.

Linux drivers, like at24, are even more extensive and assume
_all_ at24 eeprom child nodes to be nvmem cells since e888d445ac33
("nvmem: resolve cells from DT at registration time").

Since df5f3b6f5357 ("dt-bindings: nvmem: stm32: new property for
data access"), the additionalProperties: True means it's Ok to have
other properties as long as they don't match "^.*@[0-9a-f]+$".

The barebox bootloader extends the MTD partitions binding to
EEPROM and can fix up following device tree node:

  &eeprom {
    partitions {
      compatible = "fixed-partitions";
    };
  };

This is allowed binding-wise, but drivers using nvmem_register()
like at24 will fail to parse because the function expects all child
nodes to have a reg property present. This results in the whole
EEPROM driver probe failing despite the device tree being correct.

Fix this by skipping nodes lacking a reg property instead of
returning an error. This effectively makes the drivers adhere
to the binding because all nodes with a unit address must have
a reg property and vice versa.

Fixes: e888d445ac33 ("nvmem: resolve cells from DT at registration time").
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 68ae6f24b57f..a5ab1e0c74cf 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -682,7 +682,9 @@ static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
 
 	for_each_child_of_node(parent, child) {
 		addr = of_get_property(child, "reg", &len);
-		if (!addr || (len < 2 * sizeof(u32))) {
+		if (!addr)
+			continue;
+		if (len < 2 * sizeof(u32)) {
 			dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
 			return -EINVAL;
 		}
-- 
2.21.0


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

* Re: [PATCH 4/5] nvmem: Add driver to expose reserved memory as nvmem
  2021-01-29 17:14 ` [PATCH 4/5] nvmem: Add driver to expose reserved memory as nvmem Srinivas Kandagatla
@ 2021-01-29 17:51   ` Randy Dunlap
  0 siblings, 0 replies; 7+ messages in thread
From: Randy Dunlap @ 2021-01-29 17:51 UTC (permalink / raw)
  To: Srinivas Kandagatla, gregkh
  Cc: linux-kernel, Nicolas Saenz Julienne, Rob Herring, Tim Gover

Hi,

On 1/29/21 9:14 AM, Srinivas Kandagatla wrote:
> diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
> index 954d3b4a52ab..fecc19b884bf 100644
> --- a/drivers/nvmem/Kconfig
> +++ b/drivers/nvmem/Kconfig
> @@ -270,4 +270,12 @@ config SPRD_EFUSE
>   	  This driver can also be built as a module. If so, the module
>   	  will be called nvmem-sprd-efuse.
>   
> +config NVMEM_RMEM
> +	tristate "Reserved Memory Based Driver Support"
> +	help
> +	  This drivers maps reserved memory into an nvmem device. It might be

	  This driver

> +	  useful to expose information left by firmware in memory.
> +
> +	  This driver can also be built as a module. If so, the module
> +	  will be called nvmem-rmem.
>   endif


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

end of thread, other threads:[~2021-01-29 17:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-29 17:14 [PATCH 0/5] nvmem: patches (set 1) for 5.12 Srinivas Kandagatla
2021-01-29 17:14 ` [PATCH 1/5] nvmem: core: Fix a resource leak on error in nvmem_add_cells_from_of() Srinivas Kandagatla
2021-01-29 17:14 ` [PATCH 2/5] nvmem: imx-iim: Use of_device_get_match_data() Srinivas Kandagatla
2021-01-29 17:14 ` [PATCH 3/5] dt-bindings: nvmem: Add bindings for rmem driver Srinivas Kandagatla
2021-01-29 17:14 ` [PATCH 4/5] nvmem: Add driver to expose reserved memory as nvmem Srinivas Kandagatla
2021-01-29 17:51   ` Randy Dunlap
2021-01-29 17:14 ` [PATCH 5/5] nvmem: core: skip child nodes not matching binding Srinivas Kandagatla

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