xref: /linux/drivers/misc/sram.c (revision 52990390f91c1c39ca742fc8f390b29891d95127)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Generic on-chip SRAM allocation driver
4  *
5  * Copyright (C) 2012 Philipp Zabel, Pengutronix
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/genalloc.h>
11 #include <linux/io.h>
12 #include <linux/list_sort.h>
13 #include <linux/of_address.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
18 #include <linux/mfd/syscon.h>
19 #include <soc/at91/atmel-secumod.h>
20 
21 #include "sram.h"
22 
23 #define SRAM_GRANULARITY	32
24 
25 static ssize_t sram_read(struct file *filp, struct kobject *kobj,
26 			 struct bin_attribute *attr,
27 			 char *buf, loff_t pos, size_t count)
28 {
29 	struct sram_partition *part;
30 
31 	part = container_of(attr, struct sram_partition, battr);
32 
33 	mutex_lock(&part->lock);
34 	memcpy_fromio(buf, part->base + pos, count);
35 	mutex_unlock(&part->lock);
36 
37 	return count;
38 }
39 
40 static ssize_t sram_write(struct file *filp, struct kobject *kobj,
41 			  struct bin_attribute *attr,
42 			  char *buf, loff_t pos, size_t count)
43 {
44 	struct sram_partition *part;
45 
46 	part = container_of(attr, struct sram_partition, battr);
47 
48 	mutex_lock(&part->lock);
49 	memcpy_toio(part->base + pos, buf, count);
50 	mutex_unlock(&part->lock);
51 
52 	return count;
53 }
54 
55 static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
56 			 phys_addr_t start, struct sram_partition *part)
57 {
58 	int ret;
59 
60 	part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
61 					  NUMA_NO_NODE, block->label);
62 	if (IS_ERR(part->pool))
63 		return PTR_ERR(part->pool);
64 
65 	ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
66 				block->size, NUMA_NO_NODE);
67 	if (ret < 0) {
68 		dev_err(sram->dev, "failed to register subpool: %d\n", ret);
69 		return ret;
70 	}
71 
72 	return 0;
73 }
74 
75 static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
76 			   phys_addr_t start, struct sram_partition *part)
77 {
78 	sysfs_bin_attr_init(&part->battr);
79 	part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
80 					       "%llx.sram",
81 					       (unsigned long long)start);
82 	if (!part->battr.attr.name)
83 		return -ENOMEM;
84 
85 	part->battr.attr.mode = S_IRUSR | S_IWUSR;
86 	part->battr.read = sram_read;
87 	part->battr.write = sram_write;
88 	part->battr.size = block->size;
89 
90 	return device_create_bin_file(sram->dev, &part->battr);
91 }
92 
93 static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
94 			      phys_addr_t start)
95 {
96 	int ret;
97 	struct sram_partition *part = &sram->partition[sram->partitions];
98 
99 	mutex_init(&part->lock);
100 
101 	if (sram->config && sram->config->map_only_reserved) {
102 		void __iomem *virt_base;
103 
104 		if (sram->no_memory_wc)
105 			virt_base = devm_ioremap_resource(sram->dev, &block->res);
106 		else
107 			virt_base = devm_ioremap_resource_wc(sram->dev, &block->res);
108 
109 		if (IS_ERR(virt_base)) {
110 			dev_err(sram->dev, "could not map SRAM at %pr\n", &block->res);
111 			return PTR_ERR(virt_base);
112 		}
113 
114 		part->base = virt_base;
115 	} else {
116 		part->base = sram->virt_base + block->start;
117 	}
118 
119 	if (block->pool) {
120 		ret = sram_add_pool(sram, block, start, part);
121 		if (ret)
122 			return ret;
123 	}
124 	if (block->export) {
125 		ret = sram_add_export(sram, block, start, part);
126 		if (ret)
127 			return ret;
128 	}
129 	if (block->protect_exec) {
130 		ret = sram_check_protect_exec(sram, block, part);
131 		if (ret)
132 			return ret;
133 
134 		ret = sram_add_pool(sram, block, start, part);
135 		if (ret)
136 			return ret;
137 
138 		sram_add_protect_exec(part);
139 	}
140 
141 	sram->partitions++;
142 
143 	return 0;
144 }
145 
146 static void sram_free_partitions(struct sram_dev *sram)
147 {
148 	struct sram_partition *part;
149 
150 	if (!sram->partitions)
151 		return;
152 
153 	part = &sram->partition[sram->partitions - 1];
154 	for (; sram->partitions; sram->partitions--, part--) {
155 		if (part->battr.size)
156 			device_remove_bin_file(sram->dev, &part->battr);
157 
158 		if (part->pool &&
159 		    gen_pool_avail(part->pool) < gen_pool_size(part->pool))
160 			dev_err(sram->dev, "removed pool while SRAM allocated\n");
161 	}
162 }
163 
164 static int sram_reserve_cmp(void *priv, const struct list_head *a,
165 					const struct list_head *b)
166 {
167 	struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
168 	struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
169 
170 	return ra->start - rb->start;
171 }
172 
173 static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
174 {
175 	struct device_node *np = sram->dev->of_node, *child;
176 	unsigned long size, cur_start, cur_size;
177 	struct sram_reserve *rblocks, *block;
178 	struct list_head reserve_list;
179 	unsigned int nblocks, exports = 0;
180 	const char *label;
181 	int ret = 0;
182 
183 	INIT_LIST_HEAD(&reserve_list);
184 
185 	size = resource_size(res);
186 
187 	/*
188 	 * We need an additional block to mark the end of the memory region
189 	 * after the reserved blocks from the dt are processed.
190 	 */
191 	nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
192 	rblocks = kcalloc(nblocks, sizeof(*rblocks), GFP_KERNEL);
193 	if (!rblocks)
194 		return -ENOMEM;
195 
196 	block = &rblocks[0];
197 	for_each_available_child_of_node(np, child) {
198 		struct resource child_res;
199 
200 		ret = of_address_to_resource(child, 0, &child_res);
201 		if (ret < 0) {
202 			dev_err(sram->dev,
203 				"could not get address for node %pOF\n",
204 				child);
205 			goto err_chunks;
206 		}
207 
208 		if (child_res.start < res->start || child_res.end > res->end) {
209 			dev_err(sram->dev,
210 				"reserved block %pOF outside the sram area\n",
211 				child);
212 			ret = -EINVAL;
213 			goto err_chunks;
214 		}
215 
216 		block->start = child_res.start - res->start;
217 		block->size = resource_size(&child_res);
218 		block->res = child_res;
219 		list_add_tail(&block->list, &reserve_list);
220 
221 		block->export = of_property_read_bool(child, "export");
222 		block->pool = of_property_read_bool(child, "pool");
223 		block->protect_exec = of_property_read_bool(child, "protect-exec");
224 
225 		if ((block->export || block->pool || block->protect_exec) &&
226 		    block->size) {
227 			exports++;
228 
229 			label = NULL;
230 			ret = of_property_read_string(child, "label", &label);
231 			if (ret && ret != -EINVAL) {
232 				dev_err(sram->dev,
233 					"%pOF has invalid label name\n",
234 					child);
235 				goto err_chunks;
236 			}
237 			if (!label)
238 				label = child->name;
239 
240 			block->label = devm_kstrdup(sram->dev,
241 						    label, GFP_KERNEL);
242 			if (!block->label) {
243 				ret = -ENOMEM;
244 				goto err_chunks;
245 			}
246 
247 			dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
248 				block->export ? "exported " : "", block->label,
249 				block->start, block->start + block->size);
250 		} else {
251 			dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
252 				block->start, block->start + block->size);
253 		}
254 
255 		block++;
256 	}
257 	child = NULL;
258 
259 	/* the last chunk marks the end of the region */
260 	rblocks[nblocks - 1].start = size;
261 	rblocks[nblocks - 1].size = 0;
262 	list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
263 
264 	list_sort(NULL, &reserve_list, sram_reserve_cmp);
265 
266 	if (exports) {
267 		sram->partition = devm_kcalloc(sram->dev,
268 				       exports, sizeof(*sram->partition),
269 				       GFP_KERNEL);
270 		if (!sram->partition) {
271 			ret = -ENOMEM;
272 			goto err_chunks;
273 		}
274 	}
275 
276 	cur_start = 0;
277 	list_for_each_entry(block, &reserve_list, list) {
278 		/* can only happen if sections overlap */
279 		if (block->start < cur_start) {
280 			dev_err(sram->dev,
281 				"block at 0x%x starts after current offset 0x%lx\n",
282 				block->start, cur_start);
283 			ret = -EINVAL;
284 			sram_free_partitions(sram);
285 			goto err_chunks;
286 		}
287 
288 		if ((block->export || block->pool || block->protect_exec) &&
289 		    block->size) {
290 			ret = sram_add_partition(sram, block,
291 						 res->start + block->start);
292 			if (ret) {
293 				sram_free_partitions(sram);
294 				goto err_chunks;
295 			}
296 		}
297 
298 		/* current start is in a reserved block, so continue after it */
299 		if (block->start == cur_start) {
300 			cur_start = block->start + block->size;
301 			continue;
302 		}
303 
304 		/*
305 		 * allocate the space between the current starting
306 		 * address and the following reserved block, or the
307 		 * end of the region.
308 		 */
309 		cur_size = block->start - cur_start;
310 
311 		if (sram->pool) {
312 			dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
313 				cur_start, cur_start + cur_size);
314 
315 			ret = gen_pool_add_virt(sram->pool,
316 					(unsigned long)sram->virt_base + cur_start,
317 					res->start + cur_start, cur_size, -1);
318 			if (ret < 0) {
319 				sram_free_partitions(sram);
320 				goto err_chunks;
321 			}
322 		}
323 
324 		/* next allocation after this reserved block */
325 		cur_start = block->start + block->size;
326 	}
327 
328 err_chunks:
329 	of_node_put(child);
330 	kfree(rblocks);
331 
332 	return ret;
333 }
334 
335 static int atmel_securam_wait(void)
336 {
337 	struct regmap *regmap;
338 	u32 val;
339 
340 	regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
341 	if (IS_ERR(regmap))
342 		return -ENODEV;
343 
344 	return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
345 					val & AT91_SECUMOD_RAMRDY_READY,
346 					10000, 500000);
347 }
348 
349 static const struct sram_config atmel_securam_config = {
350 	.init = atmel_securam_wait,
351 };
352 
353 /*
354  * SYSRAM contains areas that are not accessible by the
355  * kernel, such as the first 256K that is reserved for TZ.
356  * Accesses to those areas (including speculative accesses)
357  * trigger SErrors. As such we must map only the areas of
358  * SYSRAM specified in the device tree.
359  */
360 static const struct sram_config tegra_sysram_config = {
361 	.map_only_reserved = true,
362 };
363 
364 static const struct of_device_id sram_dt_ids[] = {
365 	{ .compatible = "mmio-sram" },
366 	{ .compatible = "atmel,sama5d2-securam", .data = &atmel_securam_config },
367 	{ .compatible = "nvidia,tegra186-sysram", .data = &tegra_sysram_config },
368 	{ .compatible = "nvidia,tegra194-sysram", .data = &tegra_sysram_config },
369 	{ .compatible = "nvidia,tegra234-sysram", .data = &tegra_sysram_config },
370 	{}
371 };
372 
373 static int sram_probe(struct platform_device *pdev)
374 {
375 	const struct sram_config *config;
376 	struct sram_dev *sram;
377 	int ret;
378 	struct resource *res;
379 	struct clk *clk;
380 
381 	config = of_device_get_match_data(&pdev->dev);
382 
383 	sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
384 	if (!sram)
385 		return -ENOMEM;
386 
387 	sram->dev = &pdev->dev;
388 	sram->no_memory_wc = of_property_read_bool(pdev->dev.of_node, "no-memory-wc");
389 	sram->config = config;
390 
391 	if (!config || !config->map_only_reserved) {
392 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
393 		if (sram->no_memory_wc)
394 			sram->virt_base = devm_ioremap_resource(&pdev->dev, res);
395 		else
396 			sram->virt_base = devm_ioremap_resource_wc(&pdev->dev, res);
397 		if (IS_ERR(sram->virt_base)) {
398 			dev_err(&pdev->dev, "could not map SRAM registers\n");
399 			return PTR_ERR(sram->virt_base);
400 		}
401 
402 		sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
403 						  NUMA_NO_NODE, NULL);
404 		if (IS_ERR(sram->pool))
405 			return PTR_ERR(sram->pool);
406 	}
407 
408 	clk = devm_clk_get_optional_enabled(sram->dev, NULL);
409 	if (IS_ERR(clk))
410 		return PTR_ERR(clk);
411 
412 	ret = sram_reserve_regions(sram,
413 			platform_get_resource(pdev, IORESOURCE_MEM, 0));
414 	if (ret)
415 		return ret;
416 
417 	platform_set_drvdata(pdev, sram);
418 
419 	if (config && config->init) {
420 		ret = config->init();
421 		if (ret)
422 			goto err_free_partitions;
423 	}
424 
425 	if (sram->pool)
426 		dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
427 			gen_pool_size(sram->pool) / 1024, sram->virt_base);
428 
429 	return 0;
430 
431 err_free_partitions:
432 	sram_free_partitions(sram);
433 
434 	return ret;
435 }
436 
437 static int sram_remove(struct platform_device *pdev)
438 {
439 	struct sram_dev *sram = platform_get_drvdata(pdev);
440 
441 	sram_free_partitions(sram);
442 
443 	if (sram->pool && gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
444 		dev_err(sram->dev, "removed while SRAM allocated\n");
445 
446 	return 0;
447 }
448 
449 static struct platform_driver sram_driver = {
450 	.driver = {
451 		.name = "sram",
452 		.of_match_table = sram_dt_ids,
453 	},
454 	.probe = sram_probe,
455 	.remove = sram_remove,
456 };
457 
458 static int __init sram_init(void)
459 {
460 	return platform_driver_register(&sram_driver);
461 }
462 
463 postcore_initcall(sram_init);
464