sram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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/genalloc.h>
  22. #include <linux/io.h>
  23. #include <linux/list_sort.h>
  24. #include <linux/of_address.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/sram.h>
  28. #include <asm/cacheflush.h>
  29. #define SRAM_GRANULARITY 32
  30. static DEFINE_MUTEX(exec_pool_list_mutex);
  31. static LIST_HEAD(exec_pool_list);
  32. struct sram_partition {
  33. void __iomem *base;
  34. struct gen_pool *pool;
  35. struct bin_attribute battr;
  36. struct mutex lock;
  37. struct list_head list;
  38. };
  39. struct sram_dev {
  40. struct device *dev;
  41. void __iomem *virt_base;
  42. struct gen_pool *pool;
  43. struct clk *clk;
  44. struct sram_partition *partition;
  45. u32 partitions;
  46. };
  47. struct sram_reserve {
  48. struct list_head list;
  49. u32 start;
  50. u32 size;
  51. bool export;
  52. bool pool;
  53. bool protect_exec;
  54. const char *label;
  55. };
  56. /**
  57. * sram_exec_copy - copy data to a protected executable region of sram
  58. *
  59. * @pool: struct gen_pool retrieved that is part of this sram
  60. * @dst: Destination address for the copy, that must be inside pool
  61. * @src: Source address for the data to copy
  62. * @size: Size of copy to perform, which starting from dst, must reside in pool
  63. *
  64. * This helper function allows sram driver to act as central control location
  65. * of 'protect-exec' pools which are normal sram pools but are always set
  66. * read-only and executable except when copying data to them, at which point
  67. * they are set to read-write non-executable, to make sure no memory is
  68. * writeable and executable at the same time. This region must be page-aligned
  69. * and is checked during probe, otherwise page attribute manipulation would
  70. * not be possible.
  71. */
  72. int sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
  73. size_t size)
  74. {
  75. struct sram_partition *part = NULL, *p;
  76. unsigned long base;
  77. int pages;
  78. mutex_lock(&exec_pool_list_mutex);
  79. list_for_each_entry(p, &exec_pool_list, list) {
  80. if (p->pool == pool)
  81. part = p;
  82. }
  83. mutex_unlock(&exec_pool_list_mutex);
  84. if (!part)
  85. return -EINVAL;
  86. if (!addr_in_gen_pool(pool, (unsigned long)dst, size))
  87. return -EINVAL;
  88. base = (unsigned long)part->base;
  89. pages = PAGE_ALIGN(size) / PAGE_SIZE;
  90. mutex_lock(&part->lock);
  91. set_memory_nx((unsigned long)base, pages);
  92. set_memory_rw((unsigned long)base, pages);
  93. memcpy(dst, src, size);
  94. set_memory_ro((unsigned long)base, pages);
  95. set_memory_x((unsigned long)base, pages);
  96. mutex_unlock(&part->lock);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL_GPL(sram_exec_copy);
  100. static ssize_t sram_read(struct file *filp, struct kobject *kobj,
  101. struct bin_attribute *attr,
  102. char *buf, loff_t pos, size_t count)
  103. {
  104. struct sram_partition *part;
  105. part = container_of(attr, struct sram_partition, battr);
  106. mutex_lock(&part->lock);
  107. memcpy_fromio(buf, part->base + pos, count);
  108. mutex_unlock(&part->lock);
  109. return count;
  110. }
  111. static ssize_t sram_write(struct file *filp, struct kobject *kobj,
  112. struct bin_attribute *attr,
  113. char *buf, loff_t pos, size_t count)
  114. {
  115. struct sram_partition *part;
  116. part = container_of(attr, struct sram_partition, battr);
  117. mutex_lock(&part->lock);
  118. memcpy_toio(part->base + pos, buf, count);
  119. mutex_unlock(&part->lock);
  120. return count;
  121. }
  122. static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
  123. phys_addr_t start, struct sram_partition *part)
  124. {
  125. int ret;
  126. part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  127. NUMA_NO_NODE, block->label);
  128. if (IS_ERR(part->pool))
  129. return PTR_ERR(part->pool);
  130. ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
  131. block->size, NUMA_NO_NODE);
  132. if (ret < 0) {
  133. dev_err(sram->dev, "failed to register subpool: %d\n", ret);
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
  139. phys_addr_t start, struct sram_partition *part)
  140. {
  141. sysfs_bin_attr_init(&part->battr);
  142. part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
  143. "%llx.sram",
  144. (unsigned long long)start);
  145. if (!part->battr.attr.name)
  146. return -ENOMEM;
  147. part->battr.attr.mode = S_IRUSR | S_IWUSR;
  148. part->battr.read = sram_read;
  149. part->battr.write = sram_write;
  150. part->battr.size = block->size;
  151. return device_create_bin_file(sram->dev, &part->battr);
  152. }
  153. static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
  154. phys_addr_t start)
  155. {
  156. int ret;
  157. struct sram_partition *part = &sram->partition[sram->partitions];
  158. mutex_init(&part->lock);
  159. part->base = sram->virt_base + block->start;
  160. if (block->protect_exec) {
  161. unsigned long base = (unsigned long)part->base;
  162. unsigned long end = base + block->size;
  163. if (!PAGE_ALIGNED(base) || !PAGE_ALIGNED(end)) {
  164. dev_err(sram->dev,
  165. "SRAM pool marked with 'protect-exec' is not page aligned and will not be created.\n");
  166. return -ENOMEM;
  167. }
  168. }
  169. if (block->pool) {
  170. ret = sram_add_pool(sram, block, start, part);
  171. if (ret)
  172. return ret;
  173. }
  174. if (block->export) {
  175. ret = sram_add_export(sram, block, start, part);
  176. if (ret)
  177. return ret;
  178. }
  179. if (block->protect_exec) {
  180. /* Add to the list after successful call to sram_add_pool */
  181. mutex_lock(&exec_pool_list_mutex);
  182. list_add_tail(&part->list, &exec_pool_list);
  183. mutex_unlock(&exec_pool_list_mutex);
  184. }
  185. sram->partitions++;
  186. return 0;
  187. }
  188. static void sram_free_partitions(struct sram_dev *sram)
  189. {
  190. struct sram_partition *part;
  191. if (!sram->partitions)
  192. return;
  193. part = &sram->partition[sram->partitions - 1];
  194. for (; sram->partitions; sram->partitions--, part--) {
  195. if (part->battr.size)
  196. device_remove_bin_file(sram->dev, &part->battr);
  197. if (part->pool &&
  198. gen_pool_avail(part->pool) < gen_pool_size(part->pool))
  199. dev_err(sram->dev, "removed pool while SRAM allocated\n");
  200. }
  201. }
  202. static int sram_reserve_cmp(void *priv, struct list_head *a,
  203. struct list_head *b)
  204. {
  205. struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
  206. struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
  207. return ra->start - rb->start;
  208. }
  209. static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
  210. {
  211. struct device_node *np = sram->dev->of_node, *child;
  212. unsigned long size, cur_start, cur_size;
  213. struct sram_reserve *rblocks, *block;
  214. struct list_head reserve_list;
  215. unsigned int nblocks, exports = 0;
  216. const char *label;
  217. int ret = 0;
  218. INIT_LIST_HEAD(&reserve_list);
  219. size = resource_size(res);
  220. /*
  221. * We need an additional block to mark the end of the memory region
  222. * after the reserved blocks from the dt are processed.
  223. */
  224. nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
  225. rblocks = kzalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
  226. if (!rblocks)
  227. return -ENOMEM;
  228. block = &rblocks[0];
  229. for_each_available_child_of_node(np, child) {
  230. struct resource child_res;
  231. ret = of_address_to_resource(child, 0, &child_res);
  232. if (ret < 0) {
  233. dev_err(sram->dev,
  234. "could not get address for node %s\n",
  235. child->full_name);
  236. goto err_chunks;
  237. }
  238. if (child_res.start < res->start || child_res.end > res->end) {
  239. dev_err(sram->dev,
  240. "reserved block %s outside the sram area\n",
  241. child->full_name);
  242. ret = -EINVAL;
  243. goto err_chunks;
  244. }
  245. block->start = child_res.start - res->start;
  246. block->size = resource_size(&child_res);
  247. list_add_tail(&block->list, &reserve_list);
  248. if (of_find_property(child, "export", NULL))
  249. block->export = true;
  250. if (of_find_property(child, "pool", NULL))
  251. block->pool = true;
  252. if (of_find_property(child, "protect-exec", NULL)) {
  253. block->protect_exec = true;
  254. block->pool = true;
  255. }
  256. if ((block->export || block->pool) && block->size) {
  257. exports++;
  258. label = NULL;
  259. ret = of_property_read_string(child, "label", &label);
  260. if (ret && ret != -EINVAL) {
  261. dev_err(sram->dev,
  262. "%s has invalid label name\n",
  263. child->full_name);
  264. goto err_chunks;
  265. }
  266. if (!label)
  267. label = child->name;
  268. block->label = devm_kstrdup(sram->dev,
  269. label, GFP_KERNEL);
  270. if (!block->label)
  271. goto err_chunks;
  272. dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
  273. block->export ? "exported " : "", block->label,
  274. block->start, block->start + block->size);
  275. } else {
  276. dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
  277. block->start, block->start + block->size);
  278. }
  279. block++;
  280. }
  281. child = NULL;
  282. /* the last chunk marks the end of the region */
  283. rblocks[nblocks - 1].start = size;
  284. rblocks[nblocks - 1].size = 0;
  285. list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
  286. list_sort(NULL, &reserve_list, sram_reserve_cmp);
  287. if (exports) {
  288. sram->partition = devm_kzalloc(sram->dev,
  289. exports * sizeof(*sram->partition),
  290. GFP_KERNEL);
  291. if (!sram->partition) {
  292. ret = -ENOMEM;
  293. goto err_chunks;
  294. }
  295. }
  296. cur_start = 0;
  297. list_for_each_entry(block, &reserve_list, list) {
  298. /* can only happen if sections overlap */
  299. if (block->start < cur_start) {
  300. dev_err(sram->dev,
  301. "block at 0x%x starts after current offset 0x%lx\n",
  302. block->start, cur_start);
  303. ret = -EINVAL;
  304. sram_free_partitions(sram);
  305. goto err_chunks;
  306. }
  307. if ((block->export || block->pool) && block->size) {
  308. ret = sram_add_partition(sram, block,
  309. res->start + block->start);
  310. if (ret) {
  311. sram_free_partitions(sram);
  312. goto err_chunks;
  313. }
  314. }
  315. /* current start is in a reserved block, so continue after it */
  316. if (block->start == cur_start) {
  317. cur_start = block->start + block->size;
  318. continue;
  319. }
  320. /*
  321. * allocate the space between the current starting
  322. * address and the following reserved block, or the
  323. * end of the region.
  324. */
  325. cur_size = block->start - cur_start;
  326. dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
  327. cur_start, cur_start + cur_size);
  328. ret = gen_pool_add_virt(sram->pool,
  329. (unsigned long)sram->virt_base + cur_start,
  330. res->start + cur_start, cur_size, -1);
  331. if (ret < 0) {
  332. sram_free_partitions(sram);
  333. goto err_chunks;
  334. }
  335. /* next allocation after this reserved block */
  336. cur_start = block->start + block->size;
  337. }
  338. err_chunks:
  339. if (child)
  340. of_node_put(child);
  341. kfree(rblocks);
  342. return ret;
  343. }
  344. static int sram_probe(struct platform_device *pdev)
  345. {
  346. struct sram_dev *sram;
  347. struct resource *res;
  348. size_t size;
  349. int ret;
  350. sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
  351. if (!sram)
  352. return -ENOMEM;
  353. sram->dev = &pdev->dev;
  354. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  355. if (!res) {
  356. dev_err(sram->dev, "found no memory resource\n");
  357. return -EINVAL;
  358. }
  359. size = resource_size(res);
  360. if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
  361. dev_err(sram->dev, "could not request region for resource\n");
  362. return -EBUSY;
  363. }
  364. if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc"))
  365. sram->virt_base = devm_ioremap(sram->dev, res->start, size);
  366. else
  367. sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
  368. if (!sram->virt_base)
  369. return -ENOMEM;
  370. sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  371. NUMA_NO_NODE, NULL);
  372. if (IS_ERR(sram->pool))
  373. return PTR_ERR(sram->pool);
  374. ret = sram_reserve_regions(sram, res);
  375. if (ret)
  376. return ret;
  377. sram->clk = devm_clk_get(sram->dev, NULL);
  378. if (IS_ERR(sram->clk))
  379. sram->clk = NULL;
  380. else
  381. clk_prepare_enable(sram->clk);
  382. platform_set_drvdata(pdev, sram);
  383. dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
  384. gen_pool_size(sram->pool) / 1024, sram->virt_base);
  385. return 0;
  386. }
  387. static int sram_remove(struct platform_device *pdev)
  388. {
  389. struct sram_dev *sram = platform_get_drvdata(pdev);
  390. sram_free_partitions(sram);
  391. if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
  392. dev_err(sram->dev, "removed while SRAM allocated\n");
  393. if (sram->clk)
  394. clk_disable_unprepare(sram->clk);
  395. return 0;
  396. }
  397. #ifdef CONFIG_OF
  398. static const struct of_device_id sram_dt_ids[] = {
  399. { .compatible = "mmio-sram" },
  400. {}
  401. };
  402. #endif
  403. static struct platform_driver sram_driver = {
  404. .driver = {
  405. .name = "sram",
  406. .of_match_table = of_match_ptr(sram_dt_ids),
  407. },
  408. .probe = sram_probe,
  409. .remove = sram_remove,
  410. };
  411. static int __init sram_init(void)
  412. {
  413. return platform_driver_register(&sram_driver);
  414. }
  415. postcore_initcall(sram_init);