sram.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #define SRAM_GRANULARITY 32
  28. struct sram_partition {
  29. void __iomem *base;
  30. struct gen_pool *pool;
  31. struct bin_attribute battr;
  32. struct mutex lock;
  33. };
  34. struct sram_dev {
  35. struct device *dev;
  36. void __iomem *virt_base;
  37. struct gen_pool *pool;
  38. struct clk *clk;
  39. struct sram_partition *partition;
  40. u32 partitions;
  41. };
  42. struct sram_reserve {
  43. struct list_head list;
  44. u32 start;
  45. u32 size;
  46. bool export;
  47. bool pool;
  48. const char *label;
  49. };
  50. static ssize_t sram_read(struct file *filp, struct kobject *kobj,
  51. struct bin_attribute *attr,
  52. char *buf, loff_t pos, size_t count)
  53. {
  54. struct sram_partition *part;
  55. part = container_of(attr, struct sram_partition, battr);
  56. mutex_lock(&part->lock);
  57. memcpy_fromio(buf, part->base + pos, count);
  58. mutex_unlock(&part->lock);
  59. return count;
  60. }
  61. static ssize_t sram_write(struct file *filp, struct kobject *kobj,
  62. struct bin_attribute *attr,
  63. char *buf, loff_t pos, size_t count)
  64. {
  65. struct sram_partition *part;
  66. part = container_of(attr, struct sram_partition, battr);
  67. mutex_lock(&part->lock);
  68. memcpy_toio(part->base + pos, buf, count);
  69. mutex_unlock(&part->lock);
  70. return count;
  71. }
  72. static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
  73. phys_addr_t start, struct sram_partition *part)
  74. {
  75. int ret;
  76. part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  77. NUMA_NO_NODE, block->label);
  78. if (IS_ERR(part->pool))
  79. return PTR_ERR(part->pool);
  80. ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
  81. block->size, NUMA_NO_NODE);
  82. if (ret < 0) {
  83. dev_err(sram->dev, "failed to register subpool: %d\n", ret);
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
  89. phys_addr_t start, struct sram_partition *part)
  90. {
  91. sysfs_bin_attr_init(&part->battr);
  92. part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
  93. "%llx.sram",
  94. (unsigned long long)start);
  95. if (!part->battr.attr.name)
  96. return -ENOMEM;
  97. part->battr.attr.mode = S_IRUSR | S_IWUSR;
  98. part->battr.read = sram_read;
  99. part->battr.write = sram_write;
  100. part->battr.size = block->size;
  101. return device_create_bin_file(sram->dev, &part->battr);
  102. }
  103. static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
  104. phys_addr_t start)
  105. {
  106. int ret;
  107. struct sram_partition *part = &sram->partition[sram->partitions];
  108. mutex_init(&part->lock);
  109. part->base = sram->virt_base + block->start;
  110. if (block->pool) {
  111. ret = sram_add_pool(sram, block, start, part);
  112. if (ret)
  113. return ret;
  114. }
  115. if (block->export) {
  116. ret = sram_add_export(sram, block, start, part);
  117. if (ret)
  118. return ret;
  119. }
  120. sram->partitions++;
  121. return 0;
  122. }
  123. static void sram_free_partitions(struct sram_dev *sram)
  124. {
  125. struct sram_partition *part;
  126. if (!sram->partitions)
  127. return;
  128. part = &sram->partition[sram->partitions - 1];
  129. for (; sram->partitions; sram->partitions--, part--) {
  130. if (part->battr.size)
  131. device_remove_bin_file(sram->dev, &part->battr);
  132. if (part->pool &&
  133. gen_pool_avail(part->pool) < gen_pool_size(part->pool))
  134. dev_err(sram->dev, "removed pool while SRAM allocated\n");
  135. }
  136. }
  137. static int sram_reserve_cmp(void *priv, struct list_head *a,
  138. struct list_head *b)
  139. {
  140. struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
  141. struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
  142. return ra->start - rb->start;
  143. }
  144. static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
  145. {
  146. struct device_node *np = sram->dev->of_node, *child;
  147. unsigned long size, cur_start, cur_size;
  148. struct sram_reserve *rblocks, *block;
  149. struct list_head reserve_list;
  150. unsigned int nblocks, exports = 0;
  151. const char *label;
  152. int ret = 0;
  153. INIT_LIST_HEAD(&reserve_list);
  154. size = resource_size(res);
  155. /*
  156. * We need an additional block to mark the end of the memory region
  157. * after the reserved blocks from the dt are processed.
  158. */
  159. nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
  160. rblocks = kzalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
  161. if (!rblocks)
  162. return -ENOMEM;
  163. block = &rblocks[0];
  164. for_each_available_child_of_node(np, child) {
  165. struct resource child_res;
  166. ret = of_address_to_resource(child, 0, &child_res);
  167. if (ret < 0) {
  168. dev_err(sram->dev,
  169. "could not get address for node %s\n",
  170. child->full_name);
  171. goto err_chunks;
  172. }
  173. if (child_res.start < res->start || child_res.end > res->end) {
  174. dev_err(sram->dev,
  175. "reserved block %s outside the sram area\n",
  176. child->full_name);
  177. ret = -EINVAL;
  178. goto err_chunks;
  179. }
  180. block->start = child_res.start - res->start;
  181. block->size = resource_size(&child_res);
  182. list_add_tail(&block->list, &reserve_list);
  183. if (of_find_property(child, "export", NULL))
  184. block->export = true;
  185. if (of_find_property(child, "pool", NULL))
  186. block->pool = true;
  187. if ((block->export || block->pool) && block->size) {
  188. exports++;
  189. label = NULL;
  190. ret = of_property_read_string(child, "label", &label);
  191. if (ret && ret != -EINVAL) {
  192. dev_err(sram->dev,
  193. "%s has invalid label name\n",
  194. child->full_name);
  195. goto err_chunks;
  196. }
  197. if (!label)
  198. label = child->name;
  199. block->label = devm_kstrdup(sram->dev,
  200. label, GFP_KERNEL);
  201. if (!block->label)
  202. goto err_chunks;
  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_kzalloc(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->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. dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
  258. cur_start, cur_start + cur_size);
  259. ret = gen_pool_add_virt(sram->pool,
  260. (unsigned long)sram->virt_base + cur_start,
  261. res->start + cur_start, cur_size, -1);
  262. if (ret < 0) {
  263. sram_free_partitions(sram);
  264. goto err_chunks;
  265. }
  266. /* next allocation after this reserved block */
  267. cur_start = block->start + block->size;
  268. }
  269. err_chunks:
  270. if (child)
  271. of_node_put(child);
  272. kfree(rblocks);
  273. return ret;
  274. }
  275. static int sram_probe(struct platform_device *pdev)
  276. {
  277. struct sram_dev *sram;
  278. struct resource *res;
  279. size_t size;
  280. int ret;
  281. sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
  282. if (!sram)
  283. return -ENOMEM;
  284. sram->dev = &pdev->dev;
  285. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  286. if (!res) {
  287. dev_err(sram->dev, "found no memory resource\n");
  288. return -EINVAL;
  289. }
  290. size = resource_size(res);
  291. if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
  292. dev_err(sram->dev, "could not request region for resource\n");
  293. return -EBUSY;
  294. }
  295. sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
  296. if (IS_ERR(sram->virt_base))
  297. return PTR_ERR(sram->virt_base);
  298. sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  299. NUMA_NO_NODE, NULL);
  300. if (IS_ERR(sram->pool))
  301. return PTR_ERR(sram->pool);
  302. ret = sram_reserve_regions(sram, res);
  303. if (ret)
  304. return ret;
  305. sram->clk = devm_clk_get(sram->dev, NULL);
  306. if (IS_ERR(sram->clk))
  307. sram->clk = NULL;
  308. else
  309. clk_prepare_enable(sram->clk);
  310. platform_set_drvdata(pdev, sram);
  311. dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
  312. gen_pool_size(sram->pool) / 1024, sram->virt_base);
  313. return 0;
  314. }
  315. static int sram_remove(struct platform_device *pdev)
  316. {
  317. struct sram_dev *sram = platform_get_drvdata(pdev);
  318. sram_free_partitions(sram);
  319. if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
  320. dev_err(sram->dev, "removed while SRAM allocated\n");
  321. if (sram->clk)
  322. clk_disable_unprepare(sram->clk);
  323. return 0;
  324. }
  325. #ifdef CONFIG_OF
  326. static const struct of_device_id sram_dt_ids[] = {
  327. { .compatible = "mmio-sram" },
  328. {}
  329. };
  330. #endif
  331. static struct platform_driver sram_driver = {
  332. .driver = {
  333. .name = "sram",
  334. .of_match_table = of_match_ptr(sram_dt_ids),
  335. },
  336. .probe = sram_probe,
  337. .remove = sram_remove,
  338. };
  339. static int __init sram_init(void)
  340. {
  341. return platform_driver_register(&sram_driver);
  342. }
  343. postcore_initcall(sram_init);