sram.c 12 KB

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