79fc84f5ace9f3009e53dc80df799f12b9595868
[openwrt/staging/linusw.git] /
1 From 639949895123bb937b374ccacae0675ff4ab6e55 Mon Sep 17 00:00:00 2001
2 From: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
3 Date: Thu, 10 Dec 2020 12:13:49 +0100
4 Subject: [PATCH] nvmem: Add driver to expose reserved memory as nvmem
5
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.
10
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.
13
14 Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
15
16 ---
17
18 Changes since v1:
19 - Remove reserved memory phandle indirection by directly creating a
20 platform device from the reserved memory DT node
21 - Only map memory upon reading it to avoid corruption
22 - Small cosmetic cleanups
23 ---
24 drivers/nvmem/Kconfig | 8 ++++
25 drivers/nvmem/Makefile | 2 +
26 drivers/nvmem/rmem.c | 97 ++++++++++++++++++++++++++++++++++++++++++
27 drivers/of/platform.c | 1 +
28 4 files changed, 108 insertions(+)
29 create mode 100644 drivers/nvmem/rmem.c
30
31 --- a/drivers/nvmem/Kconfig
32 +++ b/drivers/nvmem/Kconfig
33 @@ -270,4 +270,12 @@ config SPRD_EFUSE
34 This driver can also be built as a module. If so, the module
35 will be called nvmem-sprd-efuse.
36
37 +config NVMEM_RMEM
38 + tristate "Reserved Memory Based Driver Support"
39 + help
40 + This drivers maps reserved memory into an nvmem device. It might be
41 + useful to expose information left by firmware in memory.
42 +
43 + This driver can also be built as a module. If so, the module
44 + will be called nvmem-rmem.
45 endif
46 --- a/drivers/nvmem/Makefile
47 +++ b/drivers/nvmem/Makefile
48 @@ -55,3 +55,5 @@ obj-$(CONFIG_NVMEM_ZYNQMP) += nvmem_zynq
49 nvmem_zynqmp_nvmem-y := zynqmp_nvmem.o
50 obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_efuse.o
51 nvmem_sprd_efuse-y := sprd-efuse.o
52 +obj-$(CONFIG_NVMEM_RMEM) += nvmem-rmem.o
53 +nvmem-rmem-y := rmem.o
54 --- /dev/null
55 +++ b/drivers/nvmem/rmem.c
56 @@ -0,0 +1,97 @@
57 +// SPDX-License-Identifier: GPL-2.0+
58 +/*
59 + * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
60 + */
61 +
62 +#include <linux/io.h>
63 +#include <linux/module.h>
64 +#include <linux/nvmem-provider.h>
65 +#include <linux/of_reserved_mem.h>
66 +#include <linux/platform_device.h>
67 +
68 +struct rmem {
69 + struct device *dev;
70 + struct nvmem_device *nvmem;
71 + struct reserved_mem *mem;
72 +
73 + phys_addr_t size;
74 +};
75 +
76 +static int rmem_read(void *context, unsigned int offset,
77 + void *val, size_t bytes)
78 +{
79 + struct rmem *priv = context;
80 + size_t available = priv->mem->size;
81 + loff_t off = offset;
82 + void *addr;
83 + int count;
84 +
85 + /*
86 + * Only map the reserved memory at this point to avoid potential rogue
87 + * kernel threads inadvertently modifying it. Based on the current
88 + * uses-cases for this driver, the performance hit isn't a concern.
89 + * Nor is likely to be, given the nature of the subsystem. Most nvmem
90 + * devices operate over slow buses to begin with.
91 + *
92 + * An alternative would be setting the memory as RO, set_memory_ro(),
93 + * but as of Dec 2020 this isn't possible on arm64.
94 + */
95 + addr = memremap(priv->mem->base, available, MEMREMAP_WB);
96 + if (IS_ERR(addr)) {
97 + dev_err(priv->dev, "Failed to remap memory region\n");
98 + return PTR_ERR(addr);
99 + }
100 +
101 + count = memory_read_from_buffer(val, bytes, &off, addr, available);
102 +
103 + memunmap(addr);
104 +
105 + return count;
106 +}
107 +
108 +static int rmem_probe(struct platform_device *pdev)
109 +{
110 + struct nvmem_config config = { };
111 + struct device *dev = &pdev->dev;
112 + struct reserved_mem *mem;
113 + struct rmem *priv;
114 +
115 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
116 + if (!priv)
117 + return -ENOMEM;
118 + priv->dev = dev;
119 +
120 + mem = of_reserved_mem_lookup(dev->of_node);
121 + if (!mem) {
122 + dev_err(dev, "Failed to lookup reserved memory\n");
123 + return -EINVAL;
124 + }
125 + priv->mem = mem;
126 +
127 + config.dev = dev;
128 + config.priv = priv;
129 + config.name = "rmem";
130 + config.size = mem->size;
131 + config.reg_read = rmem_read;
132 +
133 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
134 +}
135 +
136 +static const struct of_device_id rmem_match[] = {
137 + { .compatible = "nvmem-rmem", },
138 + { /* sentinel */ },
139 +};
140 +MODULE_DEVICE_TABLE(of, rmem_match);
141 +
142 +static struct platform_driver rmem_driver = {
143 + .probe = rmem_probe,
144 + .driver = {
145 + .name = "rmem",
146 + .of_match_table = rmem_match,
147 + },
148 +};
149 +module_platform_driver(rmem_driver);
150 +
151 +MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
152 +MODULE_DESCRIPTION("Reserved Memory Based nvmem Driver");
153 +MODULE_LICENSE("GPL");
154 --- a/drivers/of/platform.c
155 +++ b/drivers/of/platform.c
156 @@ -511,6 +511,7 @@ static const struct of_device_id reserve
157 { .compatible = "qcom,rmtfs-mem" },
158 { .compatible = "qcom,cmd-db" },
159 { .compatible = "ramoops" },
160 + { .compatible = "nvmem-rmem" },
161 {}
162 };
163