ppc4xx_ocm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 <linux/of_address.h>
  29. #include <asm/rheap.h>
  30. #include <asm/ppc4xx_ocm.h>
  31. #include <linux/slab.h>
  32. #include <linux/debugfs.h>
  33. #define OCM_DISABLED 0
  34. #define OCM_ENABLED 1
  35. struct ocm_block {
  36. struct list_head list;
  37. void __iomem *addr;
  38. int size;
  39. const char *owner;
  40. };
  41. /* non-cached or cached region */
  42. struct ocm_region {
  43. phys_addr_t phys;
  44. void __iomem *virt;
  45. int memtotal;
  46. int memfree;
  47. rh_info_t *rh;
  48. struct list_head list;
  49. };
  50. struct ocm_info {
  51. int index;
  52. int status;
  53. int ready;
  54. phys_addr_t phys;
  55. int alignment;
  56. int memtotal;
  57. int cache_size;
  58. struct ocm_region nc; /* non-cached region */
  59. struct ocm_region c; /* cached region */
  60. };
  61. static struct ocm_info *ocm_nodes;
  62. static int ocm_count;
  63. static struct ocm_info *ocm_get_node(unsigned int index)
  64. {
  65. if (index >= ocm_count) {
  66. printk(KERN_ERR "PPC4XX OCM: invalid index");
  67. return NULL;
  68. }
  69. return &ocm_nodes[index];
  70. }
  71. static int ocm_free_region(struct ocm_region *ocm_reg, const void *addr)
  72. {
  73. struct ocm_block *blk, *tmp;
  74. unsigned long offset;
  75. if (!ocm_reg->virt)
  76. return 0;
  77. list_for_each_entry_safe(blk, tmp, &ocm_reg->list, list) {
  78. if (blk->addr == addr) {
  79. offset = addr - ocm_reg->virt;
  80. ocm_reg->memfree += blk->size;
  81. rh_free(ocm_reg->rh, offset);
  82. list_del(&blk->list);
  83. kfree(blk);
  84. return 1;
  85. }
  86. }
  87. return 0;
  88. }
  89. static void __init ocm_init_node(int count, struct device_node *node)
  90. {
  91. struct ocm_info *ocm;
  92. const unsigned int *cell_index;
  93. const unsigned int *cache_size;
  94. int len;
  95. struct resource rsrc;
  96. int ioflags;
  97. ocm = ocm_get_node(count);
  98. cell_index = of_get_property(node, "cell-index", &len);
  99. if (!cell_index) {
  100. printk(KERN_ERR "PPC4XX OCM: missing cell-index property");
  101. return;
  102. }
  103. ocm->index = *cell_index;
  104. if (of_device_is_available(node))
  105. ocm->status = OCM_ENABLED;
  106. cache_size = of_get_property(node, "cached-region-size", &len);
  107. if (cache_size)
  108. ocm->cache_size = *cache_size;
  109. if (of_address_to_resource(node, 0, &rsrc)) {
  110. printk(KERN_ERR "PPC4XX OCM%d: could not get resource address\n",
  111. ocm->index);
  112. return;
  113. }
  114. ocm->phys = rsrc.start;
  115. ocm->memtotal = (rsrc.end - rsrc.start + 1);
  116. printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (%s)\n",
  117. ocm->index, ocm->memtotal,
  118. (ocm->status == OCM_DISABLED) ? "disabled" : "enabled");
  119. if (ocm->status == OCM_DISABLED)
  120. return;
  121. /* request region */
  122. if (!request_mem_region(ocm->phys, ocm->memtotal, "ppc4xx_ocm")) {
  123. printk(KERN_ERR "PPC4XX OCM%d: could not request region\n",
  124. ocm->index);
  125. return;
  126. }
  127. /* Configure non-cached and cached regions */
  128. ocm->nc.phys = ocm->phys;
  129. ocm->nc.memtotal = ocm->memtotal - ocm->cache_size;
  130. ocm->nc.memfree = ocm->nc.memtotal;
  131. ocm->c.phys = ocm->phys + ocm->nc.memtotal;
  132. ocm->c.memtotal = ocm->cache_size;
  133. ocm->c.memfree = ocm->c.memtotal;
  134. if (ocm->nc.memtotal == 0)
  135. ocm->nc.phys = 0;
  136. if (ocm->c.memtotal == 0)
  137. ocm->c.phys = 0;
  138. printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (non-cached)\n",
  139. ocm->index, ocm->nc.memtotal);
  140. printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (cached)\n",
  141. ocm->index, ocm->c.memtotal);
  142. /* ioremap the non-cached region */
  143. if (ocm->nc.memtotal) {
  144. ioflags = _PAGE_NO_CACHE | _PAGE_GUARDED | _PAGE_EXEC;
  145. ocm->nc.virt = __ioremap(ocm->nc.phys, ocm->nc.memtotal,
  146. ioflags);
  147. if (!ocm->nc.virt) {
  148. printk(KERN_ERR
  149. "PPC4XX OCM%d: failed to ioremap non-cached memory\n",
  150. ocm->index);
  151. ocm->nc.memfree = 0;
  152. return;
  153. }
  154. }
  155. /* ioremap the cached region */
  156. if (ocm->c.memtotal) {
  157. ioflags = _PAGE_EXEC;
  158. ocm->c.virt = __ioremap(ocm->c.phys, ocm->c.memtotal,
  159. ioflags);
  160. if (!ocm->c.virt) {
  161. printk(KERN_ERR
  162. "PPC4XX OCM%d: failed to ioremap cached memory\n",
  163. ocm->index);
  164. ocm->c.memfree = 0;
  165. return;
  166. }
  167. }
  168. /* Create Remote Heaps */
  169. ocm->alignment = 4; /* default 4 byte alignment */
  170. if (ocm->nc.virt) {
  171. ocm->nc.rh = rh_create(ocm->alignment);
  172. rh_attach_region(ocm->nc.rh, 0, ocm->nc.memtotal);
  173. }
  174. if (ocm->c.virt) {
  175. ocm->c.rh = rh_create(ocm->alignment);
  176. rh_attach_region(ocm->c.rh, 0, ocm->c.memtotal);
  177. }
  178. INIT_LIST_HEAD(&ocm->nc.list);
  179. INIT_LIST_HEAD(&ocm->c.list);
  180. ocm->ready = 1;
  181. return;
  182. }
  183. static int ocm_debugfs_show(struct seq_file *m, void *v)
  184. {
  185. struct ocm_block *blk, *tmp;
  186. unsigned int i;
  187. for (i = 0; i < ocm_count; i++) {
  188. struct ocm_info *ocm = ocm_get_node(i);
  189. if (!ocm || !ocm->ready)
  190. continue;
  191. seq_printf(m, "PPC4XX OCM : %d\n", ocm->index);
  192. seq_printf(m, "PhysAddr : 0x%llx\n", ocm->phys);
  193. seq_printf(m, "MemTotal : %d Bytes\n", ocm->memtotal);
  194. seq_printf(m, "MemTotal(NC) : %d Bytes\n", ocm->nc.memtotal);
  195. seq_printf(m, "MemTotal(C) : %d Bytes\n", ocm->c.memtotal);
  196. seq_printf(m, "\n");
  197. seq_printf(m, "NC.PhysAddr : 0x%llx\n", ocm->nc.phys);
  198. seq_printf(m, "NC.VirtAddr : 0x%p\n", ocm->nc.virt);
  199. seq_printf(m, "NC.MemTotal : %d Bytes\n", ocm->nc.memtotal);
  200. seq_printf(m, "NC.MemFree : %d Bytes\n", ocm->nc.memfree);
  201. list_for_each_entry_safe(blk, tmp, &ocm->nc.list, list) {
  202. seq_printf(m, "NC.MemUsed : %d Bytes (%s)\n",
  203. blk->size, blk->owner);
  204. }
  205. seq_printf(m, "\n");
  206. seq_printf(m, "C.PhysAddr : 0x%llx\n", ocm->c.phys);
  207. seq_printf(m, "C.VirtAddr : 0x%p\n", ocm->c.virt);
  208. seq_printf(m, "C.MemTotal : %d Bytes\n", ocm->c.memtotal);
  209. seq_printf(m, "C.MemFree : %d Bytes\n", ocm->c.memfree);
  210. list_for_each_entry_safe(blk, tmp, &ocm->c.list, list) {
  211. seq_printf(m, "C.MemUsed : %d Bytes (%s)\n",
  212. blk->size, blk->owner);
  213. }
  214. seq_printf(m, "\n");
  215. }
  216. return 0;
  217. }
  218. static int ocm_debugfs_open(struct inode *inode, struct file *file)
  219. {
  220. return single_open(file, ocm_debugfs_show, NULL);
  221. }
  222. static const struct file_operations ocm_debugfs_fops = {
  223. .open = ocm_debugfs_open,
  224. .read = seq_read,
  225. .llseek = seq_lseek,
  226. .release = single_release,
  227. };
  228. static int ocm_debugfs_init(void)
  229. {
  230. struct dentry *junk;
  231. junk = debugfs_create_dir("ppc4xx_ocm", 0);
  232. if (!junk) {
  233. printk(KERN_ALERT "debugfs ppc4xx ocm: failed to create dir\n");
  234. return -1;
  235. }
  236. if (debugfs_create_file("info", 0644, junk, NULL, &ocm_debugfs_fops)) {
  237. printk(KERN_ALERT "debugfs ppc4xx ocm: failed to create file\n");
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. void *ppc4xx_ocm_alloc(phys_addr_t *phys, int size, int align,
  243. int flags, const char *owner)
  244. {
  245. void __iomem *addr = NULL;
  246. unsigned long offset;
  247. struct ocm_info *ocm;
  248. struct ocm_region *ocm_reg;
  249. struct ocm_block *ocm_blk;
  250. int i;
  251. for (i = 0; i < ocm_count; i++) {
  252. ocm = ocm_get_node(i);
  253. if (!ocm || !ocm->ready)
  254. continue;
  255. if (flags == PPC4XX_OCM_NON_CACHED)
  256. ocm_reg = &ocm->nc;
  257. else
  258. ocm_reg = &ocm->c;
  259. if (!ocm_reg->virt)
  260. continue;
  261. if (align < ocm->alignment)
  262. align = ocm->alignment;
  263. offset = rh_alloc_align(ocm_reg->rh, size, align, NULL);
  264. if (IS_ERR_VALUE(offset))
  265. continue;
  266. ocm_blk = kzalloc(sizeof(struct ocm_block), GFP_KERNEL);
  267. if (!ocm_blk) {
  268. printk(KERN_ERR "PPC4XX OCM: could not allocate ocm block");
  269. rh_free(ocm_reg->rh, offset);
  270. break;
  271. }
  272. *phys = ocm_reg->phys + offset;
  273. addr = ocm_reg->virt + offset;
  274. size = ALIGN(size, align);
  275. ocm_blk->addr = addr;
  276. ocm_blk->size = size;
  277. ocm_blk->owner = owner;
  278. list_add_tail(&ocm_blk->list, &ocm_reg->list);
  279. ocm_reg->memfree -= size;
  280. break;
  281. }
  282. return addr;
  283. }
  284. void ppc4xx_ocm_free(const void *addr)
  285. {
  286. int i;
  287. if (!addr)
  288. return;
  289. for (i = 0; i < ocm_count; i++) {
  290. struct ocm_info *ocm = ocm_get_node(i);
  291. if (!ocm || !ocm->ready)
  292. continue;
  293. if (ocm_free_region(&ocm->nc, addr) ||
  294. ocm_free_region(&ocm->c, addr))
  295. return;
  296. }
  297. }
  298. static int __init ppc4xx_ocm_init(void)
  299. {
  300. struct device_node *np;
  301. int count;
  302. count = 0;
  303. for_each_compatible_node(np, NULL, "ibm,ocm")
  304. count++;
  305. if (!count)
  306. return 0;
  307. ocm_nodes = kzalloc((count * sizeof(struct ocm_info)), GFP_KERNEL);
  308. if (!ocm_nodes) {
  309. printk(KERN_ERR "PPC4XX OCM: failed to allocate OCM nodes!\n");
  310. return -ENOMEM;
  311. }
  312. ocm_count = count;
  313. count = 0;
  314. for_each_compatible_node(np, NULL, "ibm,ocm") {
  315. ocm_init_node(count, np);
  316. count++;
  317. }
  318. ocm_debugfs_init();
  319. return 0;
  320. }
  321. arch_initcall(ppc4xx_ocm_init);