ALSA: jack: Unregister input device at disconnection
[sfrench/cifs-2.6.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 #ifdef CONFIG_GENERIC_ALLOCATOR
163 /**
164  * snd_malloc_dev_iram - allocate memory from on-chip internal ram
165  * @dmab: buffer allocation record to store the allocated data
166  * @size: number of bytes to allocate from the iram
167  *
168  * This function requires iram phandle provided via of_node
169  */
170 static void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size)
171 {
172         struct device *dev = dmab->dev.dev;
173         struct gen_pool *pool = NULL;
174
175         dmab->area = NULL;
176         dmab->addr = 0;
177
178         if (dev->of_node)
179                 pool = of_get_named_gen_pool(dev->of_node, "iram", 0);
180
181         if (!pool)
182                 return;
183
184         /* Assign the pool into private_data field */
185         dmab->private_data = pool;
186
187         dmab->area = (void *)gen_pool_alloc(pool, size);
188         if (!dmab->area)
189                 return;
190
191         dmab->addr = gen_pool_virt_to_phys(pool, (unsigned long)dmab->area);
192 }
193
194 /**
195  * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
196  * @dmab: buffer allocation record to store the allocated data
197  */
198 static void snd_free_dev_iram(struct snd_dma_buffer *dmab)
199 {
200         struct gen_pool *pool = dmab->private_data;
201
202         if (pool && dmab->area)
203                 gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
204 }
205 #endif /* CONFIG_GENERIC_ALLOCATOR */
206 #endif /* CONFIG_HAS_DMA */
207
208 /*
209  *
210  *  ALSA generic memory management
211  *
212  */
213
214
215 /**
216  * snd_dma_alloc_pages - allocate the buffer area according to the given type
217  * @type: the DMA buffer type
218  * @device: the device pointer
219  * @size: the buffer size to allocate
220  * @dmab: buffer allocation record to store the allocated data
221  *
222  * Calls the memory-allocator function for the corresponding
223  * buffer type.
224  *
225  * Return: Zero if the buffer with the given size is allocated successfully,
226  * otherwise a negative value on error.
227  */
228 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
229                         struct snd_dma_buffer *dmab)
230 {
231         if (WARN_ON(!size))
232                 return -ENXIO;
233         if (WARN_ON(!dmab))
234                 return -ENXIO;
235
236         dmab->dev.type = type;
237         dmab->dev.dev = device;
238         dmab->bytes = 0;
239         switch (type) {
240         case SNDRV_DMA_TYPE_CONTINUOUS:
241                 dmab->area = snd_malloc_pages(size,
242                                         (__force gfp_t)(unsigned long)device);
243                 dmab->addr = 0;
244                 break;
245 #ifdef CONFIG_HAS_DMA
246 #ifdef CONFIG_GENERIC_ALLOCATOR
247         case SNDRV_DMA_TYPE_DEV_IRAM:
248                 snd_malloc_dev_iram(dmab, size);
249                 if (dmab->area)
250                         break;
251                 /* Internal memory might have limited size and no enough space,
252                  * so if we fail to malloc, try to fetch memory traditionally.
253                  */
254                 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
255 #endif /* CONFIG_GENERIC_ALLOCATOR */
256         case SNDRV_DMA_TYPE_DEV:
257                 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
258                 break;
259 #endif
260 #ifdef CONFIG_SND_DMA_SGBUF
261         case SNDRV_DMA_TYPE_DEV_SG:
262                 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
263                 break;
264 #endif
265         default:
266                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
267                 dmab->area = NULL;
268                 dmab->addr = 0;
269                 return -ENXIO;
270         }
271         if (! dmab->area)
272                 return -ENOMEM;
273         dmab->bytes = size;
274         return 0;
275 }
276
277 /**
278  * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
279  * @type: the DMA buffer type
280  * @device: the device pointer
281  * @size: the buffer size to allocate
282  * @dmab: buffer allocation record to store the allocated data
283  *
284  * Calls the memory-allocator function for the corresponding
285  * buffer type.  When no space is left, this function reduces the size and
286  * tries to allocate again.  The size actually allocated is stored in
287  * res_size argument.
288  *
289  * Return: Zero if the buffer with the given size is allocated successfully,
290  * otherwise a negative value on error.
291  */
292 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
293                                  struct snd_dma_buffer *dmab)
294 {
295         int err;
296
297         while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
298                 size_t aligned_size;
299                 if (err != -ENOMEM)
300                         return err;
301                 if (size <= PAGE_SIZE)
302                         return -ENOMEM;
303                 aligned_size = PAGE_SIZE << get_order(size);
304                 if (size != aligned_size)
305                         size = aligned_size;
306                 else
307                         size >>= 1;
308         }
309         if (! dmab->area)
310                 return -ENOMEM;
311         return 0;
312 }
313
314
315 /**
316  * snd_dma_free_pages - release the allocated buffer
317  * @dmab: the buffer allocation record to release
318  *
319  * Releases the allocated buffer via snd_dma_alloc_pages().
320  */
321 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
322 {
323         switch (dmab->dev.type) {
324         case SNDRV_DMA_TYPE_CONTINUOUS:
325                 snd_free_pages(dmab->area, dmab->bytes);
326                 break;
327 #ifdef CONFIG_HAS_DMA
328 #ifdef CONFIG_GENERIC_ALLOCATOR
329         case SNDRV_DMA_TYPE_DEV_IRAM:
330                 snd_free_dev_iram(dmab);
331                 break;
332 #endif /* CONFIG_GENERIC_ALLOCATOR */
333         case SNDRV_DMA_TYPE_DEV:
334                 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
335                 break;
336 #endif
337 #ifdef CONFIG_SND_DMA_SGBUF
338         case SNDRV_DMA_TYPE_DEV_SG:
339                 snd_free_sgbuf_pages(dmab);
340                 break;
341 #endif
342         default:
343                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
344         }
345 }
346
347
348 /**
349  * snd_dma_get_reserved - get the reserved buffer for the given device
350  * @dmab: the buffer allocation record to store
351  * @id: the buffer id
352  *
353  * Looks for the reserved-buffer list and re-uses if the same buffer
354  * is found in the list.  When the buffer is found, it's removed from the free list.
355  *
356  * Return: The size of buffer if the buffer is found, or zero if not found.
357  */
358 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
359 {
360         struct snd_mem_list *mem;
361
362         if (WARN_ON(!dmab))
363                 return 0;
364
365         mutex_lock(&list_mutex);
366         list_for_each_entry(mem, &mem_list_head, list) {
367                 if (mem->id == id &&
368                     (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
369                      ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
370                         struct device *dev = dmab->dev.dev;
371                         list_del(&mem->list);
372                         *dmab = mem->buffer;
373                         if (dmab->dev.dev == NULL)
374                                 dmab->dev.dev = dev;
375                         kfree(mem);
376                         mutex_unlock(&list_mutex);
377                         return dmab->bytes;
378                 }
379         }
380         mutex_unlock(&list_mutex);
381         return 0;
382 }
383
384 /**
385  * snd_dma_reserve_buf - reserve the buffer
386  * @dmab: the buffer to reserve
387  * @id: the buffer id
388  *
389  * Reserves the given buffer as a reserved buffer.
390  *
391  * Return: Zero if successful, or a negative code on error.
392  */
393 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
394 {
395         struct snd_mem_list *mem;
396
397         if (WARN_ON(!dmab))
398                 return -EINVAL;
399         mem = kmalloc(sizeof(*mem), GFP_KERNEL);
400         if (! mem)
401                 return -ENOMEM;
402         mutex_lock(&list_mutex);
403         mem->buffer = *dmab;
404         mem->id = id;
405         list_add_tail(&mem->list, &mem_list_head);
406         mutex_unlock(&list_mutex);
407         return 0;
408 }
409
410 /*
411  * purge all reserved buffers
412  */
413 static void free_all_reserved_pages(void)
414 {
415         struct list_head *p;
416         struct snd_mem_list *mem;
417
418         mutex_lock(&list_mutex);
419         while (! list_empty(&mem_list_head)) {
420                 p = mem_list_head.next;
421                 mem = list_entry(p, struct snd_mem_list, list);
422                 list_del(p);
423                 snd_dma_free_pages(&mem->buffer);
424                 kfree(mem);
425         }
426         mutex_unlock(&list_mutex);
427 }
428
429
430 #ifdef CONFIG_PROC_FS
431 /*
432  * proc file interface
433  */
434 #define SND_MEM_PROC_FILE       "driver/snd-page-alloc"
435 static struct proc_dir_entry *snd_mem_proc;
436
437 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
438 {
439         long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
440         struct snd_mem_list *mem;
441         int devno;
442         static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
443
444         mutex_lock(&list_mutex);
445         seq_printf(seq, "pages  : %li bytes (%li pages per %likB)\n",
446                    pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
447         devno = 0;
448         list_for_each_entry(mem, &mem_list_head, list) {
449                 devno++;
450                 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
451                            devno, mem->id, types[mem->buffer.dev.type]);
452                 seq_printf(seq, "  addr = 0x%lx, size = %d bytes\n",
453                            (unsigned long)mem->buffer.addr,
454                            (int)mem->buffer.bytes);
455         }
456         mutex_unlock(&list_mutex);
457         return 0;
458 }
459
460 static int snd_mem_proc_open(struct inode *inode, struct file *file)
461 {
462         return single_open(file, snd_mem_proc_read, NULL);
463 }
464
465 /* FIXME: for pci only - other bus? */
466 #ifdef CONFIG_PCI
467 #define gettoken(bufp) strsep(bufp, " \t\n")
468
469 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
470                                   size_t count, loff_t * ppos)
471 {
472         char buf[128];
473         char *token, *p;
474
475         if (count > sizeof(buf) - 1)
476                 return -EINVAL;
477         if (copy_from_user(buf, buffer, count))
478                 return -EFAULT;
479         buf[count] = '\0';
480
481         p = buf;
482         token = gettoken(&p);
483         if (! token || *token == '#')
484                 return count;
485         if (strcmp(token, "add") == 0) {
486                 char *endp;
487                 int vendor, device, size, buffers;
488                 long mask;
489                 int i, alloced;
490                 struct pci_dev *pci;
491
492                 if ((token = gettoken(&p)) == NULL ||
493                     (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
494                     (token = gettoken(&p)) == NULL ||
495                     (device = simple_strtol(token, NULL, 0)) <= 0 ||
496                     (token = gettoken(&p)) == NULL ||
497                     (mask = simple_strtol(token, NULL, 0)) < 0 ||
498                     (token = gettoken(&p)) == NULL ||
499                     (size = memparse(token, &endp)) < 64*1024 ||
500                     size > 16*1024*1024 /* too big */ ||
501                     (token = gettoken(&p)) == NULL ||
502                     (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
503                     buffers > 4) {
504                         printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
505                         return count;
506                 }
507                 vendor &= 0xffff;
508                 device &= 0xffff;
509
510                 alloced = 0;
511                 pci = NULL;
512                 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
513                         if (mask > 0 && mask < 0xffffffff) {
514                                 if (pci_set_dma_mask(pci, mask) < 0 ||
515                                     pci_set_consistent_dma_mask(pci, mask) < 0) {
516                                         printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
517                                         pci_dev_put(pci);
518                                         return count;
519                                 }
520                         }
521                         for (i = 0; i < buffers; i++) {
522                                 struct snd_dma_buffer dmab;
523                                 memset(&dmab, 0, sizeof(dmab));
524                                 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
525                                                         size, &dmab) < 0) {
526                                         printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
527                                         pci_dev_put(pci);
528                                         return count;
529                                 }
530                                 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
531                         }
532                         alloced++;
533                 }
534                 if (! alloced) {
535                         for (i = 0; i < buffers; i++) {
536                                 struct snd_dma_buffer dmab;
537                                 memset(&dmab, 0, sizeof(dmab));
538                                 /* FIXME: We can allocate only in ZONE_DMA
539                                  * without a device pointer!
540                                  */
541                                 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
542                                                         size, &dmab) < 0) {
543                                         printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
544                                         break;
545                                 }
546                                 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
547                         }
548                 }
549         } else if (strcmp(token, "erase") == 0)
550                 /* FIXME: need for releasing each buffer chunk? */
551                 free_all_reserved_pages();
552         else
553                 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
554         return count;
555 }
556 #endif /* CONFIG_PCI */
557
558 static const struct file_operations snd_mem_proc_fops = {
559         .owner          = THIS_MODULE,
560         .open           = snd_mem_proc_open,
561         .read           = seq_read,
562 #ifdef CONFIG_PCI
563         .write          = snd_mem_proc_write,
564 #endif
565         .llseek         = seq_lseek,
566         .release        = single_release,
567 };
568
569 #endif /* CONFIG_PROC_FS */
570
571 /*
572  * module entry
573  */
574
575 static int __init snd_mem_init(void)
576 {
577 #ifdef CONFIG_PROC_FS
578         snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
579                                    &snd_mem_proc_fops);
580 #endif
581         return 0;
582 }
583
584 static void __exit snd_mem_exit(void)
585 {
586         remove_proc_entry(SND_MEM_PROC_FILE, NULL);
587         free_all_reserved_pages();
588         if (snd_allocated_pages > 0)
589                 printk(KERN_ERR "snd-malloc: Memory leak?  pages not freed = %li\n", snd_allocated_pages);
590 }
591
592
593 module_init(snd_mem_init)
594 module_exit(snd_mem_exit)
595
596
597 /*
598  * exports
599  */
600 EXPORT_SYMBOL(snd_dma_alloc_pages);
601 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
602 EXPORT_SYMBOL(snd_dma_free_pages);
603
604 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
605 EXPORT_SYMBOL(snd_dma_reserve_buf);
606
607 EXPORT_SYMBOL(snd_malloc_pages);
608 EXPORT_SYMBOL(snd_free_pages);