sram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. if (sram->config && sram->config->map_only_reserved) {
  81. void __iomem *virt_base;
  82. if (sram->no_memory_wc)
  83. virt_base = devm_ioremap_resource(sram->dev, &block->res);
  84. else
  85. virt_base = devm_ioremap_resource_wc(sram->dev, &block->res);
  86. if (IS_ERR(virt_base)) {
  87. dev_err(sram->dev, "could not map SRAM at %pr\n", &block->res);
  88. return PTR_ERR(virt_base);
  89. }
  90. part->base = virt_base;
  91. } else {
  92. part->base = sram->virt_base + block->start;
  93. }
  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, const struct list_head *a,
  131. const 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. block->res = child_res;
  176. list_add_tail(&block->list, &reserve_list);
  177. if (of_find_property(child, "export", NULL))
  178. block->export = true;
  179. if (of_find_property(child, "pool", NULL))
  180. block->pool = true;
  181. if (of_find_property(child, "protect-exec", NULL))
  182. block->protect_exec = true;
  183. if ((block->export || block->pool || block->protect_exec) &&
  184. block->size) {
  185. exports++;
  186. label = NULL;
  187. ret = of_property_read_string(child, "label", &label);
  188. if (ret && ret != -EINVAL) {
  189. dev_err(sram->dev,
  190. "%pOF has invalid label name\n",
  191. child);
  192. goto err_chunks;
  193. }
  194. if (!label)
  195. label = child->name;
  196. block->label = devm_kstrdup(sram->dev,
  197. label, GFP_KERNEL);
  198. if (!block->label) {
  199. ret = -ENOMEM;
  200. goto err_chunks;
  201. }
  202. dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
  203. block->export ? "exported " : "", block->label,
  204. block->start, block->start + block->size);
  205. } else {
  206. dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
  207. block->start, block->start + block->size);
  208. }
  209. block++;
  210. }
  211. child = NULL;
  212. /* the last chunk marks the end of the region */
  213. rblocks[nblocks - 1].start = size;
  214. rblocks[nblocks - 1].size = 0;
  215. list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
  216. list_sort(NULL, &reserve_list, sram_reserve_cmp);
  217. if (exports) {
  218. sram->partition = devm_kcalloc(sram->dev,
  219. exports, sizeof(*sram->partition),
  220. GFP_KERNEL);
  221. if (!sram->partition) {
  222. ret = -ENOMEM;
  223. goto err_chunks;
  224. }
  225. }
  226. cur_start = 0;
  227. list_for_each_entry(block, &reserve_list, list) {
  228. /* can only happen if sections overlap */
  229. if (block->start < cur_start) {
  230. dev_err(sram->dev,
  231. "block at 0x%x starts after current offset 0x%lx\n",
  232. block->start, cur_start);
  233. ret = -EINVAL;
  234. sram_free_partitions(sram);
  235. goto err_chunks;
  236. }
  237. if ((block->export || block->pool || block->protect_exec) &&
  238. block->size) {
  239. ret = sram_add_partition(sram, block,
  240. res->start + block->start);
  241. if (ret) {
  242. sram_free_partitions(sram);
  243. goto err_chunks;
  244. }
  245. }
  246. /* current start is in a reserved block, so continue after it */
  247. if (block->start == cur_start) {
  248. cur_start = block->start + block->size;
  249. continue;
  250. }
  251. /*
  252. * allocate the space between the current starting
  253. * address and the following reserved block, or the
  254. * end of the region.
  255. */
  256. cur_size = block->start - cur_start;
  257. if (sram->pool) {
  258. dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
  259. cur_start, cur_start + cur_size);
  260. ret = gen_pool_add_virt(sram->pool,
  261. (unsigned long)sram->virt_base + cur_start,
  262. res->start + cur_start, cur_size, -1);
  263. if (ret < 0) {
  264. sram_free_partitions(sram);
  265. goto err_chunks;
  266. }
  267. }
  268. /* next allocation after this reserved block */
  269. cur_start = block->start + block->size;
  270. }
  271. err_chunks:
  272. of_node_put(child);
  273. kfree(rblocks);
  274. return ret;
  275. }
  276. static int atmel_securam_wait(void)
  277. {
  278. struct regmap *regmap;
  279. u32 val;
  280. regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
  281. if (IS_ERR(regmap))
  282. return -ENODEV;
  283. return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
  284. val & AT91_SECUMOD_RAMRDY_READY,
  285. 10000, 500000);
  286. }
  287. static const struct sram_config atmel_securam_config = {
  288. .init = atmel_securam_wait,
  289. };
  290. /*
  291. * SYSRAM contains areas that are not accessible by the
  292. * kernel, such as the first 256K that is reserved for TZ.
  293. * Accesses to those areas (including speculative accesses)
  294. * trigger SErrors. As such we must map only the areas of
  295. * SYSRAM specified in the device tree.
  296. */
  297. static const struct sram_config tegra_sysram_config = {
  298. .map_only_reserved = true,
  299. };
  300. static const struct of_device_id sram_dt_ids[] = {
  301. { .compatible = "mmio-sram" },
  302. { .compatible = "atmel,sama5d2-securam", .data = &atmel_securam_config },
  303. { .compatible = "nvidia,tegra186-sysram", .data = &tegra_sysram_config },
  304. { .compatible = "nvidia,tegra194-sysram", .data = &tegra_sysram_config },
  305. { .compatible = "nvidia,tegra234-sysram", .data = &tegra_sysram_config },
  306. {}
  307. };
  308. static int sram_probe(struct platform_device *pdev)
  309. {
  310. const struct sram_config *config;
  311. struct sram_dev *sram;
  312. int ret;
  313. struct resource *res;
  314. config = of_device_get_match_data(&pdev->dev);
  315. sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
  316. if (!sram)
  317. return -ENOMEM;
  318. sram->dev = &pdev->dev;
  319. sram->no_memory_wc = of_property_read_bool(pdev->dev.of_node, "no-memory-wc");
  320. sram->config = config;
  321. if (!config || !config->map_only_reserved) {
  322. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  323. if (sram->no_memory_wc)
  324. sram->virt_base = devm_ioremap_resource(&pdev->dev, res);
  325. else
  326. sram->virt_base = devm_ioremap_resource_wc(&pdev->dev, res);
  327. if (IS_ERR(sram->virt_base)) {
  328. dev_err(&pdev->dev, "could not map SRAM registers\n");
  329. return PTR_ERR(sram->virt_base);
  330. }
  331. sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  332. NUMA_NO_NODE, NULL);
  333. if (IS_ERR(sram->pool))
  334. return PTR_ERR(sram->pool);
  335. }
  336. sram->clk = devm_clk_get(sram->dev, NULL);
  337. if (IS_ERR(sram->clk))
  338. sram->clk = NULL;
  339. else
  340. clk_prepare_enable(sram->clk);
  341. ret = sram_reserve_regions(sram,
  342. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  343. if (ret)
  344. goto err_disable_clk;
  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. err_disable_clk:
  358. if (sram->clk)
  359. clk_disable_unprepare(sram->clk);
  360. return ret;
  361. }
  362. static int sram_remove(struct platform_device *pdev)
  363. {
  364. struct sram_dev *sram = platform_get_drvdata(pdev);
  365. sram_free_partitions(sram);
  366. if (sram->pool && gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
  367. dev_err(sram->dev, "removed while SRAM allocated\n");
  368. if (sram->clk)
  369. clk_disable_unprepare(sram->clk);
  370. return 0;
  371. }
  372. static struct platform_driver sram_driver = {
  373. .driver = {
  374. .name = "sram",
  375. .of_match_table = sram_dt_ids,
  376. },
  377. .probe = sram_probe,
  378. .remove = sram_remove,
  379. };
  380. static int __init sram_init(void)
  381. {
  382. return platform_driver_register(&sram_driver);
  383. }
  384. postcore_initcall(sram_init);