sram.c 11 KB

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