Lines Matching +full:- +full:sram

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic on-chip SRAM allocation driver
19 #include <soc/at91/atmel-secumod.h>
21 #include "sram.h"
33 mutex_lock(&part->lock); in sram_read()
34 memcpy_fromio(buf, part->base + pos, count); in sram_read()
35 mutex_unlock(&part->lock); in sram_read()
48 mutex_lock(&part->lock); in sram_write()
49 memcpy_toio(part->base + pos, buf, count); in sram_write()
50 mutex_unlock(&part->lock); in sram_write()
55 static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block, in sram_add_pool() argument
60 part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY), in sram_add_pool()
61 NUMA_NO_NODE, block->label); in sram_add_pool()
62 if (IS_ERR(part->pool)) in sram_add_pool()
63 return PTR_ERR(part->pool); in sram_add_pool()
65 ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start, in sram_add_pool()
66 block->size, NUMA_NO_NODE); in sram_add_pool()
68 dev_err(sram->dev, "failed to register subpool: %d\n", ret); in sram_add_pool()
75 static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block, in sram_add_export() argument
78 sysfs_bin_attr_init(&part->battr); in sram_add_export()
79 part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL, in sram_add_export()
80 "%llx.sram", in sram_add_export()
82 if (!part->battr.attr.name) in sram_add_export()
83 return -ENOMEM; in sram_add_export()
85 part->battr.attr.mode = S_IRUSR | S_IWUSR; in sram_add_export()
86 part->battr.read = sram_read; in sram_add_export()
87 part->battr.write = sram_write; in sram_add_export()
88 part->battr.size = block->size; in sram_add_export()
90 return device_create_bin_file(sram->dev, &part->battr); in sram_add_export()
93 static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block, in sram_add_partition() argument
97 struct sram_partition *part = &sram->partition[sram->partitions]; in sram_add_partition()
99 mutex_init(&part->lock); in sram_add_partition()
100 part->base = sram->virt_base + block->start; in sram_add_partition()
102 if (block->pool) { in sram_add_partition()
103 ret = sram_add_pool(sram, block, start, part); in sram_add_partition()
107 if (block->export) { in sram_add_partition()
108 ret = sram_add_export(sram, block, start, part); in sram_add_partition()
112 if (block->protect_exec) { in sram_add_partition()
113 ret = sram_check_protect_exec(sram, block, part); in sram_add_partition()
117 ret = sram_add_pool(sram, block, start, part); in sram_add_partition()
124 sram->partitions++; in sram_add_partition()
129 static void sram_free_partitions(struct sram_dev *sram) in sram_free_partitions() argument
133 if (!sram->partitions) in sram_free_partitions()
136 part = &sram->partition[sram->partitions - 1]; in sram_free_partitions()
137 for (; sram->partitions; sram->partitions--, part--) { in sram_free_partitions()
138 if (part->battr.size) in sram_free_partitions()
139 device_remove_bin_file(sram->dev, &part->battr); in sram_free_partitions()
141 if (part->pool && in sram_free_partitions()
142 gen_pool_avail(part->pool) < gen_pool_size(part->pool)) in sram_free_partitions()
143 dev_err(sram->dev, "removed pool while SRAM allocated\n"); in sram_free_partitions()
153 return ra->start - rb->start; in sram_reserve_cmp()
156 static int sram_reserve_regions(struct sram_dev *sram, struct resource *res) in sram_reserve_regions() argument
158 struct device_node *np = sram->dev->of_node, *child; in sram_reserve_regions()
177 return -ENOMEM; in sram_reserve_regions()
185 dev_err(sram->dev, in sram_reserve_regions()
191 if (child_res.start < res->start || child_res.end > res->end) { in sram_reserve_regions()
192 dev_err(sram->dev, in sram_reserve_regions()
193 "reserved block %pOF outside the sram area\n", in sram_reserve_regions()
195 ret = -EINVAL; in sram_reserve_regions()
199 block->start = child_res.start - res->start; in sram_reserve_regions()
200 block->size = resource_size(&child_res); in sram_reserve_regions()
201 list_add_tail(&block->list, &reserve_list); in sram_reserve_regions()
204 block->export = true; in sram_reserve_regions()
207 block->pool = true; in sram_reserve_regions()
209 if (of_find_property(child, "protect-exec", NULL)) in sram_reserve_regions()
210 block->protect_exec = true; in sram_reserve_regions()
212 if ((block->export || block->pool || block->protect_exec) && in sram_reserve_regions()
213 block->size) { in sram_reserve_regions()
218 if (ret && ret != -EINVAL) { in sram_reserve_regions()
219 dev_err(sram->dev, in sram_reserve_regions()
225 label = child->name; in sram_reserve_regions()
227 block->label = devm_kstrdup(sram->dev, in sram_reserve_regions()
229 if (!block->label) { in sram_reserve_regions()
230 ret = -ENOMEM; in sram_reserve_regions()
234 dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n", in sram_reserve_regions()
235 block->export ? "exported " : "", block->label, in sram_reserve_regions()
236 block->start, block->start + block->size); in sram_reserve_regions()
238 dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n", in sram_reserve_regions()
239 block->start, block->start + block->size); in sram_reserve_regions()
247 rblocks[nblocks - 1].start = size; in sram_reserve_regions()
248 rblocks[nblocks - 1].size = 0; in sram_reserve_regions()
249 list_add_tail(&rblocks[nblocks - 1].list, &reserve_list); in sram_reserve_regions()
254 sram->partition = devm_kcalloc(sram->dev, in sram_reserve_regions()
255 exports, sizeof(*sram->partition), in sram_reserve_regions()
257 if (!sram->partition) { in sram_reserve_regions()
258 ret = -ENOMEM; in sram_reserve_regions()
266 if (block->start < cur_start) { in sram_reserve_regions()
267 dev_err(sram->dev, in sram_reserve_regions()
269 block->start, cur_start); in sram_reserve_regions()
270 ret = -EINVAL; in sram_reserve_regions()
271 sram_free_partitions(sram); in sram_reserve_regions()
275 if ((block->export || block->pool || block->protect_exec) && in sram_reserve_regions()
276 block->size) { in sram_reserve_regions()
277 ret = sram_add_partition(sram, block, in sram_reserve_regions()
278 res->start + block->start); in sram_reserve_regions()
280 sram_free_partitions(sram); in sram_reserve_regions()
286 if (block->start == cur_start) { in sram_reserve_regions()
287 cur_start = block->start + block->size; in sram_reserve_regions()
296 cur_size = block->start - cur_start; in sram_reserve_regions()
298 dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n", in sram_reserve_regions()
301 ret = gen_pool_add_virt(sram->pool, in sram_reserve_regions()
302 (unsigned long)sram->virt_base + cur_start, in sram_reserve_regions()
303 res->start + cur_start, cur_size, -1); in sram_reserve_regions()
305 sram_free_partitions(sram); in sram_reserve_regions()
310 cur_start = block->start + block->size; in sram_reserve_regions()
325 regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod"); in atmel_securam_wait()
327 return -ENODEV; in atmel_securam_wait()
335 { .compatible = "mmio-sram" },
336 { .compatible = "atmel,sama5d2-securam", .data = atmel_securam_wait },
342 struct sram_dev *sram; in sram_probe() local
346 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL); in sram_probe()
347 if (!sram) in sram_probe()
348 return -ENOMEM; in sram_probe()
350 sram->dev = &pdev->dev; in sram_probe()
352 if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc")) in sram_probe()
353 sram->virt_base = devm_platform_ioremap_resource(pdev, 0); in sram_probe()
355 sram->virt_base = devm_platform_ioremap_resource_wc(pdev, 0); in sram_probe()
356 if (IS_ERR(sram->virt_base)) { in sram_probe()
357 dev_err(&pdev->dev, "could not map SRAM registers\n"); in sram_probe()
358 return PTR_ERR(sram->virt_base); in sram_probe()
361 sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY), in sram_probe()
363 if (IS_ERR(sram->pool)) in sram_probe()
364 return PTR_ERR(sram->pool); in sram_probe()
366 sram->clk = devm_clk_get(sram->dev, NULL); in sram_probe()
367 if (IS_ERR(sram->clk)) in sram_probe()
368 sram->clk = NULL; in sram_probe()
370 clk_prepare_enable(sram->clk); in sram_probe()
372 ret = sram_reserve_regions(sram, in sram_probe()
377 platform_set_drvdata(pdev, sram); in sram_probe()
379 init_func = of_device_get_match_data(&pdev->dev); in sram_probe()
386 dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n", in sram_probe()
387 gen_pool_size(sram->pool) / 1024, sram->virt_base); in sram_probe()
392 sram_free_partitions(sram); in sram_probe()
394 if (sram->clk) in sram_probe()
395 clk_disable_unprepare(sram->clk); in sram_probe()
402 struct sram_dev *sram = platform_get_drvdata(pdev); in sram_remove() local
404 sram_free_partitions(sram); in sram_remove()
406 if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool)) in sram_remove()
407 dev_err(sram->dev, "removed while SRAM allocated\n"); in sram_remove()
409 if (sram->clk) in sram_remove()
410 clk_disable_unprepare(sram->clk); in sram_remove()
417 .name = "sram",