sram.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/genalloc.h>
  10. #include <linux/io.h>
  11. #include <linux/list_sort.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #include <linux/slab.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <soc/at91/atmel-secumod.h>
  19. #include "sram.h"
  20. #define SRAM_GRANULARITY 32
  21. static ssize_t sram_read(struct file *filp, struct kobject *kobj,
  22. struct bin_attribute *attr,
  23. char *buf, loff_t pos, size_t count)
  24. {
  25. struct sram_partition *part;
  26. part = container_of(attr, struct sram_partition, battr);
  27. mutex_lock(&part->lock);
  28. memcpy_fromio(buf, part->base + pos, count);
  29. mutex_unlock(&part->lock);
  30. return count;
  31. }
  32. static ssize_t sram_write(struct file *filp, struct kobject *kobj,
  33. struct bin_attribute *attr,
  34. char *buf, loff_t pos, size_t count)
  35. {
  36. struct sram_partition *part;
  37. part = container_of(attr, struct sram_partition, battr);
  38. mutex_lock(&part->lock);
  39. memcpy_toio(part->base + pos, buf, count);
  40. mutex_unlock(&part->lock);
  41. return count;
  42. }
  43. static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
  44. phys_addr_t start, struct sram_partition *part)
  45. {
  46. int ret;
  47. part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  48. NUMA_NO_NODE, block->label);
  49. if (IS_ERR(part->pool))
  50. return PTR_ERR(part->pool);
  51. ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
  52. block->size, NUMA_NO_NODE);
  53. if (ret < 0) {
  54. dev_err(sram->dev, "failed to register subpool: %d\n", ret);
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
  60. phys_addr_t start, struct sram_partition *part)
  61. {
  62. sysfs_bin_attr_init(&part->battr);
  63. part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
  64. "%llx.sram",
  65. (unsigned long long)start);
  66. if (!part->battr.attr.name)
  67. return -ENOMEM;
  68. part->battr.attr.mode = S_IRUSR | S_IWUSR;
  69. part->battr.read = sram_read;
  70. part->battr.write = sram_write;
  71. part->battr.size = block->size;
  72. return device_create_bin_file(sram->dev, &part->battr);
  73. }
  74. static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
  75. phys_addr_t start)
  76. {
  77. int ret;
  78. struct sram_partition *part = &sram->partition[sram->partitions];
  79. mutex_init(&part->lock);
  80. part->base = sram->virt_base + block->start;
  81. if (block->pool) {
  82. ret = sram_add_pool(sram, block, start, part);
  83. if (ret)
  84. return ret;
  85. }
  86. if (block->export) {
  87. ret = sram_add_export(sram, block, start, part);
  88. if (ret)
  89. return ret;
  90. }
  91. if (block->protect_exec) {
  92. ret = sram_check_protect_exec(sram, block, part);
  93. if (ret)
  94. return ret;
  95. ret = sram_add_pool(sram, block, start, part);
  96. if (ret)
  97. return ret;
  98. sram_add_protect_exec(part);
  99. }
  100. sram->partitions++;
  101. return 0;
  102. }
  103. static void sram_free_partitions(struct sram_dev *sram)
  104. {
  105. struct sram_partition *part;
  106. if (!sram->partitions)
  107. return;
  108. part = &sram->partition[sram->partitions - 1];
  109. for (; sram->partitions; sram->partitions--, part--) {
  110. if (part->battr.size)
  111. device_remove_bin_file(sram->dev, &part->battr);
  112. if (part->pool &&
  113. gen_pool_avail(part->pool) < gen_pool_size(part->pool))
  114. dev_err(sram->dev, "removed pool while SRAM allocated\n");
  115. }
  116. }
  117. static int sram_reserve_cmp(void *priv, struct list_head *a,
  118. struct list_head *b)
  119. {
  120. struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
  121. struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
  122. return ra->start - rb->start;
  123. }
  124. static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
  125. {
  126. struct device_node *np = sram->dev->of_node, *child;
  127. unsigned long size, cur_start, cur_size;
  128. struct sram_reserve *rblocks, *block;
  129. struct list_head reserve_list;
  130. unsigned int nblocks, exports = 0;
  131. const char *label;
  132. int ret = 0;
  133. INIT_LIST_HEAD(&reserve_list);
  134. size = resource_size(res);
  135. /*
  136. * We need an additional block to mark the end of the memory region
  137. * after the reserved blocks from the dt are processed.
  138. */
  139. nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
  140. rblocks = kcalloc(nblocks, sizeof(*rblocks), GFP_KERNEL);
  141. if (!rblocks)
  142. return -ENOMEM;
  143. block = &rblocks[0];
  144. for_each_available_child_of_node(np, child) {
  145. struct resource child_res;
  146. ret = of_address_to_resource(child, 0, &child_res);
  147. if (ret < 0) {
  148. dev_err(sram->dev,
  149. "could not get address for node %pOF\n",
  150. child);
  151. goto err_chunks;
  152. }
  153. if (child_res.start < res->start || child_res.end > res->end) {
  154. dev_err(sram->dev,
  155. "reserved block %pOF outside the sram area\n",
  156. child);
  157. ret = -EINVAL;
  158. goto err_chunks;
  159. }
  160. block->start = child_res.start - res->start;
  161. block->size = resource_size(&child_res);
  162. list_add_tail(&block->list, &reserve_list);
  163. if (of_find_property(child, "export", NULL))
  164. block->export = true;
  165. if (of_find_property(child, "pool", NULL))
  166. block->pool = true;
  167. if (of_find_property(child, "protect-exec", NULL))
  168. block->protect_exec = true;
  169. if ((block->export || block->pool || block->protect_exec) &&
  170. block->size) {
  171. exports++;
  172. label = NULL;
  173. ret = of_property_read_string(child, "label", &label);
  174. if (ret && ret != -EINVAL) {
  175. dev_err(sram->dev,
  176. "%pOF has invalid label name\n",
  177. child);
  178. goto err_chunks;
  179. }
  180. if (!label)
  181. label = child->name;
  182. block->label = devm_kstrdup(sram->dev,
  183. label, GFP_KERNEL);
  184. if (!block->label) {
  185. ret = -ENOMEM;
  186. goto err_chunks;
  187. }
  188. dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
  189. block->export ? "exported " : "", block->label,
  190. block->start, block->start + block->size);
  191. } else {
  192. dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
  193. block->start, block->start + block->size);
  194. }
  195. block++;
  196. }
  197. child = NULL;
  198. /* the last chunk marks the end of the region */
  199. rblocks[nblocks - 1].start = size;
  200. rblocks[nblocks - 1].size = 0;
  201. list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
  202. list_sort(NULL, &reserve_list, sram_reserve_cmp);
  203. if (exports) {
  204. sram->partition = devm_kcalloc(sram->dev,
  205. exports, sizeof(*sram->partition),
  206. GFP_KERNEL);
  207. if (!sram->partition) {
  208. ret = -ENOMEM;
  209. goto err_chunks;
  210. }
  211. }
  212. cur_start = 0;
  213. list_for_each_entry(block, &reserve_list, list) {
  214. /* can only happen if sections overlap */
  215. if (block->start < cur_start) {
  216. dev_err(sram->dev,
  217. "block at 0x%x starts after current offset 0x%lx\n",
  218. block->start, cur_start);
  219. ret = -EINVAL;
  220. sram_free_partitions(sram);
  221. goto err_chunks;
  222. }
  223. if ((block->export || block->pool || block->protect_exec) &&
  224. block->size) {
  225. ret = sram_add_partition(sram, block,
  226. res->start + block->start);
  227. if (ret) {
  228. sram_free_partitions(sram);
  229. goto err_chunks;
  230. }
  231. }
  232. /* current start is in a reserved block, so continue after it */
  233. if (block->start == cur_start) {
  234. cur_start = block->start + block->size;
  235. continue;
  236. }
  237. /*
  238. * allocate the space between the current starting
  239. * address and the following reserved block, or the
  240. * end of the region.
  241. */
  242. cur_size = block->start - cur_start;
  243. dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
  244. cur_start, cur_start + cur_size);
  245. ret = gen_pool_add_virt(sram->pool,
  246. (unsigned long)sram->virt_base + cur_start,
  247. res->start + cur_start, cur_size, -1);
  248. if (ret < 0) {
  249. sram_free_partitions(sram);
  250. goto err_chunks;
  251. }
  252. /* next allocation after this reserved block */
  253. cur_start = block->start + block->size;
  254. }
  255. err_chunks:
  256. of_node_put(child);
  257. kfree(rblocks);
  258. return ret;
  259. }
  260. static int atmel_securam_wait(void)
  261. {
  262. struct regmap *regmap;
  263. u32 val;
  264. regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
  265. if (IS_ERR(regmap))
  266. return -ENODEV;
  267. return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
  268. val & AT91_SECUMOD_RAMRDY_READY,
  269. 10000, 500000);
  270. }
  271. static const struct of_device_id sram_dt_ids[] = {
  272. { .compatible = "mmio-sram" },
  273. { .compatible = "atmel,sama5d2-securam", .data = atmel_securam_wait },
  274. {}
  275. };
  276. static int sram_probe(struct platform_device *pdev)
  277. {
  278. struct sram_dev *sram;
  279. int ret;
  280. int (*init_func)(void);
  281. sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
  282. if (!sram)
  283. return -ENOMEM;
  284. sram->dev = &pdev->dev;
  285. if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc"))
  286. sram->virt_base = devm_platform_ioremap_resource(pdev, 0);
  287. else
  288. sram->virt_base = devm_platform_ioremap_resource_wc(pdev, 0);
  289. if (IS_ERR(sram->virt_base)) {
  290. dev_err(&pdev->dev, "could not map SRAM registers\n");
  291. return PTR_ERR(sram->virt_base);
  292. }
  293. sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  294. NUMA_NO_NODE, NULL);
  295. if (IS_ERR(sram->pool))
  296. return PTR_ERR(sram->pool);
  297. sram->clk = devm_clk_get(sram->dev, NULL);
  298. if (IS_ERR(sram->clk))
  299. sram->clk = NULL;
  300. else
  301. clk_prepare_enable(sram->clk);
  302. ret = sram_reserve_regions(sram,
  303. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  304. if (ret)
  305. goto err_disable_clk;
  306. platform_set_drvdata(pdev, sram);
  307. init_func = of_device_get_match_data(&pdev->dev);
  308. if (init_func) {
  309. ret = init_func();
  310. if (ret)
  311. goto err_free_partitions;
  312. }
  313. dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
  314. gen_pool_size(sram->pool) / 1024, sram->virt_base);
  315. return 0;
  316. err_free_partitions:
  317. sram_free_partitions(sram);
  318. err_disable_clk:
  319. if (sram->clk)
  320. clk_disable_unprepare(sram->clk);
  321. return ret;
  322. }
  323. static int sram_remove(struct platform_device *pdev)
  324. {
  325. struct sram_dev *sram = platform_get_drvdata(pdev);
  326. sram_free_partitions(sram);
  327. if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
  328. dev_err(sram->dev, "removed while SRAM allocated\n");
  329. if (sram->clk)
  330. clk_disable_unprepare(sram->clk);
  331. return 0;
  332. }
  333. static struct platform_driver sram_driver = {
  334. .driver = {
  335. .name = "sram",
  336. .of_match_table = sram_dt_ids,
  337. },
  338. .probe = sram_probe,
  339. .remove = sram_remove,
  340. };
  341. static int __init sram_init(void)
  342. {
  343. return platform_driver_register(&sram_driver);
  344. }
  345. postcore_initcall(sram_init);