1 From 5a3fa75a4d9cb6bcfc9081ef224a4cdcd4b3eafe Mon Sep 17 00:00:00 2001
2 From: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
3 Date: Fri, 29 Jan 2021 17:14:29 +0000
4 Subject: [PATCH] nvmem: Add driver to expose reserved memory as nvmem
6 Firmware/co-processors might use reserved memory areas in order to pass
7 data stemming from an nvmem device otherwise non accessible to Linux.
8 For example an EEPROM memory only physically accessible to firmware, or
9 data only accessible early at boot time.
11 In order to expose this data to other drivers and user-space, the driver
12 models the reserved memory area as an nvmem device.
14 Tested-by: Tim Gover <tim.gover@raspberrypi.com>
15 Reviewed-by: Rob Herring <robh@kernel.org>
16 Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
17 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
18 Link: https://lore.kernel.org/r/20210129171430.11328-5-srinivas.kandagatla@linaro.org
19 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
21 drivers/nvmem/Kconfig | 8 ++++
22 drivers/nvmem/Makefile | 2 +
23 drivers/nvmem/rmem.c | 97 ++++++++++++++++++++++++++++++++++++++++++
24 drivers/of/platform.c | 1 +
25 4 files changed, 108 insertions(+)
26 create mode 100644 drivers/nvmem/rmem.c
28 --- a/drivers/nvmem/Kconfig
29 +++ b/drivers/nvmem/Kconfig
30 @@ -270,4 +270,12 @@ config SPRD_EFUSE
31 This driver can also be built as a module. If so, the module
32 will be called nvmem-sprd-efuse.
35 + tristate "Reserved Memory Based Driver Support"
37 + This drivers maps reserved memory into an nvmem device. It might be
38 + useful to expose information left by firmware in memory.
40 + This driver can also be built as a module. If so, the module
41 + will be called nvmem-rmem.
43 --- a/drivers/nvmem/Makefile
44 +++ b/drivers/nvmem/Makefile
45 @@ -55,3 +55,5 @@ obj-$(CONFIG_NVMEM_ZYNQMP) += nvmem_zynq
46 nvmem_zynqmp_nvmem-y := zynqmp_nvmem.o
47 obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_efuse.o
48 nvmem_sprd_efuse-y := sprd-efuse.o
49 +obj-$(CONFIG_NVMEM_RMEM) += nvmem-rmem.o
50 +nvmem-rmem-y := rmem.o
52 +++ b/drivers/nvmem/rmem.c
54 +// SPDX-License-Identifier: GPL-2.0+
56 + * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
59 +#include <linux/io.h>
60 +#include <linux/module.h>
61 +#include <linux/nvmem-provider.h>
62 +#include <linux/of_reserved_mem.h>
63 +#include <linux/platform_device.h>
67 + struct nvmem_device *nvmem;
68 + struct reserved_mem *mem;
73 +static int rmem_read(void *context, unsigned int offset,
74 + void *val, size_t bytes)
76 + struct rmem *priv = context;
77 + size_t available = priv->mem->size;
78 + loff_t off = offset;
83 + * Only map the reserved memory at this point to avoid potential rogue
84 + * kernel threads inadvertently modifying it. Based on the current
85 + * uses-cases for this driver, the performance hit isn't a concern.
86 + * Nor is likely to be, given the nature of the subsystem. Most nvmem
87 + * devices operate over slow buses to begin with.
89 + * An alternative would be setting the memory as RO, set_memory_ro(),
90 + * but as of Dec 2020 this isn't possible on arm64.
92 + addr = memremap(priv->mem->base, available, MEMREMAP_WB);
94 + dev_err(priv->dev, "Failed to remap memory region\n");
95 + return PTR_ERR(addr);
98 + count = memory_read_from_buffer(val, bytes, &off, addr, available);
105 +static int rmem_probe(struct platform_device *pdev)
107 + struct nvmem_config config = { };
108 + struct device *dev = &pdev->dev;
109 + struct reserved_mem *mem;
112 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
117 + mem = of_reserved_mem_lookup(dev->of_node);
119 + dev_err(dev, "Failed to lookup reserved memory\n");
125 + config.priv = priv;
126 + config.name = "rmem";
127 + config.size = mem->size;
128 + config.reg_read = rmem_read;
130 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
133 +static const struct of_device_id rmem_match[] = {
134 + { .compatible = "nvmem-rmem", },
135 + { /* sentinel */ },
137 +MODULE_DEVICE_TABLE(of, rmem_match);
139 +static struct platform_driver rmem_driver = {
140 + .probe = rmem_probe,
143 + .of_match_table = rmem_match,
146 +module_platform_driver(rmem_driver);
148 +MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
149 +MODULE_DESCRIPTION("Reserved Memory Based nvmem Driver");
150 +MODULE_LICENSE("GPL");
151 --- a/drivers/of/platform.c
152 +++ b/drivers/of/platform.c
153 @@ -511,6 +511,7 @@ static const struct of_device_id reserve
154 { .compatible = "qcom,rmtfs-mem" },
155 { .compatible = "qcom,cmd-db" },
156 { .compatible = "ramoops" },
157 + { .compatible = "nvmem-rmem" },