ppc4xx_ocm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * PowerPC 4xx OCM memory allocation support
  3. *
  4. * (C) Copyright 2009, Applied Micro Circuits Corporation
  5. * Victor Gallardo (vgallardo@amcc.com)
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/of.h>
  28. #include <asm/rheap.h>
  29. #include <asm/ppc4xx_ocm.h>
  30. #include <linux/slab.h>
  31. #include <linux/debugfs.h>
  32. #define OCM_DISABLED 0
  33. #define OCM_ENABLED 1
  34. struct ocm_block {
  35. struct list_head list;
  36. void __iomem *addr;
  37. int size;
  38. const char *owner;
  39. };
  40. /* non-cached or cached region */
  41. struct ocm_region {
  42. phys_addr_t phys;
  43. void __iomem *virt;
  44. int memtotal;
  45. int memfree;
  46. rh_info_t *rh;
  47. struct list_head list;
  48. };
  49. struct ocm_info {
  50. int index;
  51. int status;
  52. int ready;
  53. phys_addr_t phys;
  54. int alignment;
  55. int memtotal;
  56. int cache_size;
  57. struct ocm_region nc; /* non-cached region */
  58. struct ocm_region c; /* cached region */
  59. };
  60. static struct ocm_info *ocm_nodes;
  61. static int ocm_count;
  62. static struct ocm_info *ocm_get_node(unsigned int index)
  63. {
  64. if (index >= ocm_count) {
  65. printk(KERN_ERR "PPC4XX OCM: invalid index");
  66. return NULL;
  67. }
  68. return &ocm_nodes[index];
  69. }
  70. static int ocm_free_region(struct ocm_region *ocm_reg, const void *addr)
  71. {
  72. struct ocm_block *blk, *tmp;
  73. unsigned long offset;
  74. if (!ocm_reg->virt)
  75. return 0;
  76. list_for_each_entry_safe(blk, tmp, &ocm_reg->list, list) {
  77. if (blk->addr == addr) {
  78. offset = addr - ocm_reg->virt;
  79. ocm_reg->memfree += blk->size;
  80. rh_free(ocm_reg->rh, offset);
  81. list_del(&blk->list);
  82. kfree(blk);
  83. return 1;
  84. }
  85. }
  86. return 0;
  87. }
  88. static void __init ocm_init_node(int count, struct device_node *node)
  89. {
  90. struct ocm_info *ocm;
  91. const unsigned int *cell_index;
  92. const unsigned int *cache_size;
  93. int len;
  94. struct resource rsrc;
  95. int ioflags;
  96. ocm = ocm_get_node(count);
  97. cell_index = of_get_property(node, "cell-index", &len);
  98. if (!cell_index) {
  99. printk(KERN_ERR "PPC4XX OCM: missing cell-index property");
  100. return;
  101. }
  102. ocm->index = *cell_index;
  103. if (of_device_is_available(node))
  104. ocm->status = OCM_ENABLED;
  105. cache_size = of_get_property(node, "cached-region-size", &len);
  106. if (cache_size)
  107. ocm->cache_size = *cache_size;
  108. if (of_address_to_resource(node, 0, &rsrc)) {
  109. printk(KERN_ERR "PPC4XX OCM%d: could not get resource address\n",
  110. ocm->index);
  111. return;
  112. }
  113. ocm->phys = rsrc.start;
  114. ocm->memtotal = (rsrc.end - rsrc.start + 1);
  115. printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (%s)\n",
  116. ocm->index, ocm->memtotal,
  117. (ocm->status == OCM_DISABLED) ? "disabled" : "enabled");
  118. if (ocm->status == OCM_DISABLED)
  119. return;
  120. /* request region */
  121. if (!request_mem_region(ocm->phys, ocm->memtotal, "ppc4xx_ocm")) {
  122. printk(KERN_ERR "PPC4XX OCM%d: could not request region\n",
  123. ocm->index);
  124. return;
  125. }
  126. /* Configure non-cached and cached regions */
  127. ocm->nc.phys = ocm->phys;
  128. ocm->nc.memtotal = ocm->memtotal - ocm->cache_size;
  129. ocm->nc.memfree = ocm->nc.memtotal;
  130. ocm->c.phys = ocm->phys + ocm->nc.memtotal;
  131. ocm->c.memtotal = ocm->cache_size;
  132. ocm->c.memfree = ocm->c.memtotal;
  133. if (ocm->nc.memtotal == 0)
  134. ocm->nc.phys = 0;
  135. if (ocm->c.memtotal == 0)
  136. ocm->c.phys = 0;
  137. printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (non-cached)\n",
  138. ocm->index, ocm->nc.memtotal);
  139. printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (cached)\n",
  140. ocm->index, ocm->c.memtotal);
  141. /* ioremap the non-cached region */
  142. if (ocm->nc.memtotal) {
  143. ioflags = _PAGE_NO_CACHE | _PAGE_GUARDED | _PAGE_EXEC;
  144. ocm->nc.virt = __ioremap(ocm->nc.phys, ocm->nc.memtotal,
  145. ioflags);
  146. if (!ocm->nc.virt) {
  147. printk(KERN_ERR
  148. "PPC4XX OCM%d: failed to ioremap non-cached memory\n",
  149. ocm->index);
  150. ocm->nc.memfree = 0;
  151. return;
  152. }
  153. }
  154. /* ioremap the cached region */
  155. if (ocm->c.memtotal) {
  156. ioflags = _PAGE_EXEC;
  157. ocm->c.virt = __ioremap(ocm->c.phys, ocm->c.memtotal,
  158. ioflags);
  159. if (!ocm->c.virt) {
  160. printk(KERN_ERR
  161. "PPC4XX OCM%d: failed to ioremap cached memory\n",
  162. ocm->index);
  163. ocm->c.memfree = 0;
  164. return;
  165. }
  166. }
  167. /* Create Remote Heaps */
  168. ocm->alignment = 4; /* default 4 byte alignment */
  169. if (ocm->nc.virt) {
  170. ocm->nc.rh = rh_create(ocm->alignment);
  171. rh_attach_region(ocm->nc.rh, 0, ocm->nc.memtotal);
  172. }
  173. if (ocm->c.virt) {
  174. ocm->c.rh = rh_create(ocm->alignment);
  175. rh_attach_region(ocm->c.rh, 0, ocm->c.memtotal);
  176. }
  177. INIT_LIST_HEAD(&ocm->nc.list);
  178. INIT_LIST_HEAD(&ocm->c.list);
  179. ocm->ready = 1;
  180. return;
  181. }
  182. static int ocm_debugfs_show(struct seq_file *m, void *v)
  183. {
  184. struct ocm_block *blk, *tmp;
  185. unsigned int i;
  186. for (i = 0; i < ocm_count; i++) {
  187. struct ocm_info *ocm = ocm_get_node(i);
  188. if (!ocm || !ocm->ready)
  189. continue;
  190. seq_printf(m, "PPC4XX OCM : %d\n", ocm->index);
  191. seq_printf(m, "PhysAddr : 0x%llx\n", ocm->phys);
  192. seq_printf(m, "MemTotal : %d Bytes\n", ocm->memtotal);
  193. seq_printf(m, "MemTotal(NC) : %d Bytes\n", ocm->nc.memtotal);
  194. seq_printf(m, "MemTotal(C) : %d Bytes\n", ocm->c.memtotal);
  195. seq_printf(m, "\n");
  196. seq_printf(m, "NC.PhysAddr : 0x%llx\n", ocm->nc.phys);
  197. seq_printf(m, "NC.VirtAddr : 0x%p\n", ocm->nc.virt);
  198. seq_printf(m, "NC.MemTotal : %d Bytes\n", ocm->nc.memtotal);
  199. seq_printf(m, "NC.MemFree : %d Bytes\n", ocm->nc.memfree);
  200. list_for_each_entry_safe(blk, tmp, &ocm->nc.list, list) {
  201. seq_printf(m, "NC.MemUsed : %d Bytes (%s)\n",
  202. blk->size, blk->owner);
  203. }
  204. seq_printf(m, "\n");
  205. seq_printf(m, "C.PhysAddr : 0x%llx\n", ocm->c.phys);
  206. seq_printf(m, "C.VirtAddr : 0x%p\n", ocm->c.virt);
  207. seq_printf(m, "C.MemTotal : %d Bytes\n", ocm->c.memtotal);
  208. seq_printf(m, "C.MemFree : %d Bytes\n", ocm->c.memfree);
  209. list_for_each_entry_safe(blk, tmp, &ocm->c.list, list) {
  210. seq_printf(m, "C.MemUsed : %d Bytes (%s)\n",
  211. blk->size, blk->owner);
  212. }
  213. seq_printf(m, "\n");
  214. }
  215. return 0;
  216. }
  217. static int ocm_debugfs_open(struct inode *inode, struct file *file)
  218. {
  219. return single_open(file, ocm_debugfs_show, NULL);
  220. }
  221. static const struct file_operations ocm_debugfs_fops = {
  222. .open = ocm_debugfs_open,
  223. .read = seq_read,
  224. .llseek = seq_lseek,
  225. .release = single_release,
  226. };
  227. static int ocm_debugfs_init(void)
  228. {
  229. struct dentry *junk;
  230. junk = debugfs_create_dir("ppc4xx_ocm", 0);
  231. if (!junk) {
  232. printk(KERN_ALERT "debugfs ppc4xx ocm: failed to create dir\n");
  233. return -1;
  234. }
  235. if (debugfs_create_file("info", 0644, junk, NULL, &ocm_debugfs_fops)) {
  236. printk(KERN_ALERT "debugfs ppc4xx ocm: failed to create file\n");
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. void *ppc4xx_ocm_alloc(phys_addr_t *phys, int size, int align,
  242. int flags, const char *owner)
  243. {
  244. void __iomem *addr = NULL;
  245. unsigned long offset;
  246. struct ocm_info *ocm;
  247. struct ocm_region *ocm_reg;
  248. struct ocm_block *ocm_blk;
  249. int i;
  250. for (i = 0; i < ocm_count; i++) {
  251. ocm = ocm_get_node(i);
  252. if (!ocm || !ocm->ready)
  253. continue;
  254. if (flags == PPC4XX_OCM_NON_CACHED)
  255. ocm_reg = &ocm->nc;
  256. else
  257. ocm_reg = &ocm->c;
  258. if (!ocm_reg->virt)
  259. continue;
  260. if (align < ocm->alignment)
  261. align = ocm->alignment;
  262. offset = rh_alloc_align(ocm_reg->rh, size, align, NULL);
  263. if (IS_ERR_VALUE(offset))
  264. continue;
  265. ocm_blk = kzalloc(sizeof(struct ocm_block *), GFP_KERNEL);
  266. if (!ocm_blk) {
  267. printk(KERN_ERR "PPC4XX OCM: could not allocate ocm block");
  268. rh_free(ocm_reg->rh, offset);
  269. break;
  270. }
  271. *phys = ocm_reg->phys + offset;
  272. addr = ocm_reg->virt + offset;
  273. size = ALIGN(size, align);
  274. ocm_blk->addr = addr;
  275. ocm_blk->size = size;
  276. ocm_blk->owner = owner;
  277. list_add_tail(&ocm_blk->list, &ocm_reg->list);
  278. ocm_reg->memfree -= size;
  279. break;
  280. }
  281. return addr;
  282. }
  283. void ppc4xx_ocm_free(const void *addr)
  284. {
  285. int i;
  286. if (!addr)
  287. return;
  288. for (i = 0; i < ocm_count; i++) {
  289. struct ocm_info *ocm = ocm_get_node(i);
  290. if (!ocm || !ocm->ready)
  291. continue;
  292. if (ocm_free_region(&ocm->nc, addr) ||
  293. ocm_free_region(&ocm->c, addr))
  294. return;
  295. }
  296. }
  297. static int __init ppc4xx_ocm_init(void)
  298. {
  299. struct device_node *np;
  300. int count;
  301. count = 0;
  302. for_each_compatible_node(np, NULL, "ibm,ocm")
  303. count++;
  304. if (!count)
  305. return 0;
  306. ocm_nodes = kzalloc((count * sizeof(struct ocm_info)), GFP_KERNEL);
  307. if (!ocm_nodes) {
  308. printk(KERN_ERR "PPC4XX OCM: failed to allocate OCM nodes!\n");
  309. return -ENOMEM;
  310. }
  311. ocm_count = count;
  312. count = 0;
  313. for_each_compatible_node(np, NULL, "ibm,ocm") {
  314. ocm_init_node(count, np);
  315. count++;
  316. }
  317. ocm_debugfs_init();
  318. return 0;
  319. }
  320. arch_initcall(ppc4xx_ocm_init);