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