~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/powerpc/platforms/4xx/ocm.c

Version: ~ [ linux-5.6-rc1 ] ~ [ linux-5.5.2 ] ~ [ linux-5.4.17 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.102 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.170 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.213 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.213 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp