ALSA: Add ifdef CONFIG_GENERIC_ALLOCATOR for SNDRV_DMA_TYPE_IRAM code
[pandora-kernel.git] / sound / core / memalloc.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3  *                   Takashi Iwai <tiwai@suse.de>
4  * 
5  *  Generic memory allocators
6  *
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/proc_fs.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/mm.h>
30 #include <linux/seq_file.h>
31 #include <asm/uaccess.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/genalloc.h>
34 #include <linux/moduleparam.h>
35 #include <linux/mutex.h>
36 #include <sound/memalloc.h>
37
38
39 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
41 MODULE_LICENSE("GPL");
42
43
44 /*
45  */
46
47 static DEFINE_MUTEX(list_mutex);
48 static LIST_HEAD(mem_list_head);
49
50 /* buffer preservation list */
51 struct snd_mem_list {
52         struct snd_dma_buffer buffer;
53         unsigned int id;
54         struct list_head list;
55 };
56
57 /* id for pre-allocated buffers */
58 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
59
60 /*
61  *
62  *  Generic memory allocators
63  *
64  */
65
66 static long snd_allocated_pages; /* holding the number of allocated pages */
67
68 static inline void inc_snd_pages(int order)
69 {
70         snd_allocated_pages += 1 << order;
71 }
72
73 static inline void dec_snd_pages(int order)
74 {
75         snd_allocated_pages -= 1 << order;
76 }
77
78 /**
79  * snd_malloc_pages - allocate pages with the given size
80  * @size: the size to allocate in bytes
81  * @gfp_flags: the allocation conditions, GFP_XXX
82  *
83  * Allocates the physically contiguous pages with the given size.
84  *
85  * Return: The pointer of the buffer, or %NULL if no enough memory.
86  */
87 void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
88 {
89         int pg;
90         void *res;
91
92         if (WARN_ON(!size))
93                 return NULL;
94         if (WARN_ON(!gfp_flags))
95                 return NULL;
96         gfp_flags |= __GFP_COMP;        /* compound page lets parts be mapped */
97         pg = get_order(size);
98         if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
99                 inc_snd_pages(pg);
100         return res;
101 }
102
103 /**
104  * snd_free_pages - release the pages
105  * @ptr: the buffer pointer to release
106  * @size: the allocated buffer size
107  *
108  * Releases the buffer allocated via snd_malloc_pages().
109  */
110 void snd_free_pages(void *ptr, size_t size)
111 {
112         int pg;
113
114         if (ptr == NULL)
115                 return;
116         pg = get_order(size);
117         dec_snd_pages(pg);
118         free_pages((unsigned long) ptr, pg);
119 }
120
121 /*
122  *
123  *  Bus-specific memory allocators
124  *
125  */
126
127 #ifdef CONFIG_HAS_DMA
128 /* allocate the coherent DMA pages */
129 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
130 {
131         int pg;
132         void *res;
133         gfp_t gfp_flags;
134
135         if (WARN_ON(!dma))
136                 return NULL;
137         pg = get_order(size);
138         gfp_flags = GFP_KERNEL
139                 | __GFP_COMP    /* compound page lets parts be mapped */
140                 | __GFP_NORETRY /* don't trigger OOM-killer */
141                 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
142         res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
143         if (res != NULL)
144                 inc_snd_pages(pg);
145
146         return res;
147 }
148
149 /* free the coherent DMA pages */
150 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
151                                dma_addr_t dma)
152 {
153         int pg;
154
155         if (ptr == NULL)
156                 return;
157         pg = get_order(size);
158         dec_snd_pages(pg);
159         dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
160 }
161
162 /**
163  * snd_malloc_dev_iram - allocate memory from on-chip internal ram
164  * @dmab: buffer allocation record to store the allocated data
165  * @size: number of bytes to allocate from the iram
166  *
167  * This function requires iram phandle provided via of_node
168  */
169 void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size)
170 {
171         struct device *dev = dmab->dev.dev;
172         struct gen_pool *pool = NULL;
173
174         if (dev->of_node)
175                 pool = of_get_named_gen_pool(dev->of_node, "iram", 0);
176
177         if (!pool)
178                 return;
179
180         /* Assign the pool into private_data field */
181         dmab->private_data = pool;
182
183         dmab->area = (void *)gen_pool_alloc(pool, size);
184         if (!dmab->area)
185                 return;
186
187         dmab->addr = gen_pool_virt_to_phys(pool, (unsigned long)dmab->area);
188 }
189
190 /**
191  * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
192  * @dmab: buffer allocation record to store the allocated data
193  */
194 void snd_free_dev_iram(struct snd_dma_buffer *dmab)
195 {
196         struct gen_pool *pool = dmab->private_data;
197
198         if (pool && dmab->area)
199                 gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
200 }
201 #endif /* CONFIG_HAS_DMA */
202
203 /*
204  *
205  *  ALSA generic memory management
206  *
207  */
208
209
210 /**
211  * snd_dma_alloc_pages - allocate the buffer area according to the given type
212  * @type: the DMA buffer type
213  * @device: the device pointer
214  * @size: the buffer size to allocate
215  * @dmab: buffer allocation record to store the allocated data
216  *
217  * Calls the memory-allocator function for the corresponding
218  * buffer type.
219  *
220  * Return: Zero if the buffer with the given size is allocated successfully,
221  * otherwise a negative value on error.
222  */
223 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
224                         struct snd_dma_buffer *dmab)
225 {
226         if (WARN_ON(!size))
227                 return -ENXIO;
228         if (WARN_ON(!dmab))
229                 return -ENXIO;
230
231         dmab->dev.type = type;
232         dmab->dev.dev = device;
233         dmab->bytes = 0;
234         switch (type) {
235         case SNDRV_DMA_TYPE_CONTINUOUS:
236                 dmab->area = snd_malloc_pages(size,
237                                         (__force gfp_t)(unsigned long)device);
238                 dmab->addr = 0;
239                 break;
240 #ifdef CONFIG_HAS_DMA
241 #ifdef CONFIG_GENERIC_ALLOCATOR
242         case SNDRV_DMA_TYPE_DEV_IRAM:
243                 snd_malloc_dev_iram(dmab, size);
244                 if (dmab->area)
245                         break;
246                 /* Internal memory might have limited size and no enough space,
247                  * so if we fail to malloc, try to fetch memory traditionally.
248                  */
249                 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
250 #endif /* CONFIG_GENERIC_ALLOCATOR */
251         case SNDRV_DMA_TYPE_DEV:
252                 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
253                 break;
254 #endif
255 #ifdef CONFIG_SND_DMA_SGBUF
256         case SNDRV_DMA_TYPE_DEV_SG:
257                 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
258                 break;
259 #endif
260         default:
261                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
262                 dmab->area = NULL;
263                 dmab->addr = 0;
264                 return -ENXIO;
265         }
266         if (! dmab->area)
267                 return -ENOMEM;
268         dmab->bytes = size;
269         return 0;
270 }
271
272 /**
273  * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
274  * @type: the DMA buffer type
275  * @device: the device pointer
276  * @size: the buffer size to allocate
277  * @dmab: buffer allocation record to store the allocated data
278  *
279  * Calls the memory-allocator function for the corresponding
280  * buffer type.  When no space is left, this function reduces the size and
281  * tries to allocate again.  The size actually allocated is stored in
282  * res_size argument.
283  *
284  * Return: Zero if the buffer with the given size is allocated successfully,
285  * otherwise a negative value on error.
286  */
287 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
288                                  struct snd_dma_buffer *dmab)
289 {
290         int err;
291
292         while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
293                 size_t aligned_size;
294                 if (err != -ENOMEM)
295                         return err;
296                 if (size <= PAGE_SIZE)
297                         return -ENOMEM;
298                 aligned_size = PAGE_SIZE << get_order(size);
299                 if (size != aligned_size)
300                         size = aligned_size;
301                 else
302                         size >>= 1;
303         }
304         if (! dmab->area)
305                 return -ENOMEM;
306         return 0;
307 }
308
309
310 /**
311  * snd_dma_free_pages - release the allocated buffer
312  * @dmab: the buffer allocation record to release
313  *
314  * Releases the allocated buffer via snd_dma_alloc_pages().
315  */
316 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
317 {
318         switch (dmab->dev.type) {
319         case SNDRV_DMA_TYPE_CONTINUOUS:
320                 snd_free_pages(dmab->area, dmab->bytes);
321                 break;
322 #ifdef CONFIG_HAS_DMA
323 #ifdef CONFIG_GENERIC_ALLOCATOR
324         case SNDRV_DMA_TYPE_DEV_IRAM:
325                 snd_free_dev_iram(dmab);
326                 break;
327 #endif /* CONFIG_GENERIC_ALLOCATOR */
328         case SNDRV_DMA_TYPE_DEV:
329                 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
330                 break;
331 #endif
332 #ifdef CONFIG_SND_DMA_SGBUF
333         case SNDRV_DMA_TYPE_DEV_SG:
334                 snd_free_sgbuf_pages(dmab);
335                 break;
336 #endif
337         default:
338                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
339         }
340 }
341
342
343 /**
344  * snd_dma_get_reserved - get the reserved buffer for the given device
345  * @dmab: the buffer allocation record to store
346  * @id: the buffer id
347  *
348  * Looks for the reserved-buffer list and re-uses if the same buffer
349  * is found in the list.  When the buffer is found, it's removed from the free list.
350  *
351  * Return: The size of buffer if the buffer is found, or zero if not found.
352  */
353 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
354 {
355         struct snd_mem_list *mem;
356
357         if (WARN_ON(!dmab))
358                 return 0;
359
360         mutex_lock(&list_mutex);
361         list_for_each_entry(mem, &mem_list_head, list) {
362                 if (mem->id == id &&
363                     (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
364                      ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
365                         struct device *dev = dmab->dev.dev;
366                         list_del(&mem->list);
367                         *dmab = mem->buffer;
368                         if (dmab->dev.dev == NULL)
369                                 dmab->dev.dev = dev;
370                         kfree(mem);
371                         mutex_unlock(&list_mutex);
372                         return dmab->bytes;
373                 }
374         }
375         mutex_unlock(&list_mutex);
376         return 0;
377 }
378
379 /**
380  * snd_dma_reserve_buf - reserve the buffer
381  * @dmab: the buffer to reserve
382  * @id: the buffer id
383  *
384  * Reserves the given buffer as a reserved buffer.
385  *
386  * Return: Zero if successful, or a negative code on error.
387  */
388 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
389 {
390         struct snd_mem_list *mem;
391
392         if (WARN_ON(!dmab))
393                 return -EINVAL;
394         mem = kmalloc(sizeof(*mem), GFP_KERNEL);
395         if (! mem)
396                 return -ENOMEM;
397         mutex_lock(&list_mutex);
398         mem->buffer = *dmab;
399         mem->id = id;
400         list_add_tail(&mem->list, &mem_list_head);
401         mutex_unlock(&list_mutex);
402         return 0;
403 }
404
405 /*
406  * purge all reserved buffers
407  */
408 static void free_all_reserved_pages(void)
409 {
410         struct list_head *p;
411         struct snd_mem_list *mem;
412
413         mutex_lock(&list_mutex);
414         while (! list_empty(&mem_list_head)) {
415                 p = mem_list_head.next;
416                 mem = list_entry(p, struct snd_mem_list, list);
417                 list_del(p);
418                 snd_dma_free_pages(&mem->buffer);
419                 kfree(mem);
420         }
421         mutex_unlock(&list_mutex);
422 }
423
424
425 #ifdef CONFIG_PROC_FS
426 /*
427  * proc file interface
428  */
429 #define SND_MEM_PROC_FILE       "driver/snd-page-alloc"
430 static struct proc_dir_entry *snd_mem_proc;
431
432 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
433 {
434         long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
435         struct snd_mem_list *mem;
436         int devno;
437         static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
438
439         mutex_lock(&list_mutex);
440         seq_printf(seq, "pages  : %li bytes (%li pages per %likB)\n",
441                    pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
442         devno = 0;
443         list_for_each_entry(mem, &mem_list_head, list) {
444                 devno++;
445                 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
446                            devno, mem->id, types[mem->buffer.dev.type]);
447                 seq_printf(seq, "  addr = 0x%lx, size = %d bytes\n",
448                            (unsigned long)mem->buffer.addr,
449                            (int)mem->buffer.bytes);
450         }
451         mutex_unlock(&list_mutex);
452         return 0;
453 }
454
455 static int snd_mem_proc_open(struct inode *inode, struct file *file)
456 {
457         return single_open(file, snd_mem_proc_read, NULL);
458 }
459
460 /* FIXME: for pci only - other bus? */
461 #ifdef CONFIG_PCI
462 #define gettoken(bufp) strsep(bufp, " \t\n")
463
464 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
465                                   size_t count, loff_t * ppos)
466 {
467         char buf[128];
468         char *token, *p;
469
470         if (count > sizeof(buf) - 1)
471                 return -EINVAL;
472         if (copy_from_user(buf, buffer, count))
473                 return -EFAULT;
474         buf[count] = '\0';
475
476         p = buf;
477         token = gettoken(&p);
478         if (! token || *token == '#')
479                 return count;
480         if (strcmp(token, "add") == 0) {
481                 char *endp;
482                 int vendor, device, size, buffers;
483                 long mask;
484                 int i, alloced;
485                 struct pci_dev *pci;
486
487                 if ((token = gettoken(&p)) == NULL ||
488                     (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
489                     (token = gettoken(&p)) == NULL ||
490                     (device = simple_strtol(token, NULL, 0)) <= 0 ||
491                     (token = gettoken(&p)) == NULL ||
492                     (mask = simple_strtol(token, NULL, 0)) < 0 ||
493                     (token = gettoken(&p)) == NULL ||
494                     (size = memparse(token, &endp)) < 64*1024 ||
495                     size > 16*1024*1024 /* too big */ ||
496                     (token = gettoken(&p)) == NULL ||
497                     (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
498                     buffers > 4) {
499                         printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
500                         return count;
501                 }
502                 vendor &= 0xffff;
503                 device &= 0xffff;
504
505                 alloced = 0;
506                 pci = NULL;
507                 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
508                         if (mask > 0 && mask < 0xffffffff) {
509                                 if (pci_set_dma_mask(pci, mask) < 0 ||
510                                     pci_set_consistent_dma_mask(pci, mask) < 0) {
511                                         printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
512                                         pci_dev_put(pci);
513                                         return count;
514                                 }
515                         }
516                         for (i = 0; i < buffers; i++) {
517                                 struct snd_dma_buffer dmab;
518                                 memset(&dmab, 0, sizeof(dmab));
519                                 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
520                                                         size, &dmab) < 0) {
521                                         printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
522                                         pci_dev_put(pci);
523                                         return count;
524                                 }
525                                 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
526                         }
527                         alloced++;
528                 }
529                 if (! alloced) {
530                         for (i = 0; i < buffers; i++) {
531                                 struct snd_dma_buffer dmab;
532                                 memset(&dmab, 0, sizeof(dmab));
533                                 /* FIXME: We can allocate only in ZONE_DMA
534                                  * without a device pointer!
535                                  */
536                                 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
537                                                         size, &dmab) < 0) {
538                                         printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
539                                         break;
540                                 }
541                                 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
542                         }
543                 }
544         } else if (strcmp(token, "erase") == 0)
545                 /* FIXME: need for releasing each buffer chunk? */
546                 free_all_reserved_pages();
547         else
548                 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
549         return count;
550 }
551 #endif /* CONFIG_PCI */
552
553 static const struct file_operations snd_mem_proc_fops = {
554         .owner          = THIS_MODULE,
555         .open           = snd_mem_proc_open,
556         .read           = seq_read,
557 #ifdef CONFIG_PCI
558         .write          = snd_mem_proc_write,
559 #endif
560         .llseek         = seq_lseek,
561         .release        = single_release,
562 };
563
564 #endif /* CONFIG_PROC_FS */
565
566 /*
567  * module entry
568  */
569
570 static int __init snd_mem_init(void)
571 {
572 #ifdef CONFIG_PROC_FS
573         snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
574                                    &snd_mem_proc_fops);
575 #endif
576         return 0;
577 }
578
579 static void __exit snd_mem_exit(void)
580 {
581         remove_proc_entry(SND_MEM_PROC_FILE, NULL);
582         free_all_reserved_pages();
583         if (snd_allocated_pages > 0)
584                 printk(KERN_ERR "snd-malloc: Memory leak?  pages not freed = %li\n", snd_allocated_pages);
585 }
586
587
588 module_init(snd_mem_init)
589 module_exit(snd_mem_exit)
590
591
592 /*
593  * exports
594  */
595 EXPORT_SYMBOL(snd_dma_alloc_pages);
596 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
597 EXPORT_SYMBOL(snd_dma_free_pages);
598
599 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
600 EXPORT_SYMBOL(snd_dma_reserve_buf);
601
602 EXPORT_SYMBOL(snd_malloc_pages);
603 EXPORT_SYMBOL(snd_free_pages);