vserver 1.9.3
[linux-2.6.git] / sound / core / memalloc.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.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/config.h>
25 #include <linux/module.h>
26 #include <linux/proc_fs.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/moduleparam.h>
33 #include <asm/semaphore.h>
34 #include <sound/memalloc.h>
35 #ifdef CONFIG_SBUS
36 #include <asm/sbus.h>
37 #endif
38
39
40 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
41 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
42 MODULE_LICENSE("GPL");
43
44
45 #ifndef SNDRV_CARDS
46 #define SNDRV_CARDS     8
47 #endif
48
49 /* FIXME: so far only some PCI devices have the preallocation table */
50 #ifdef CONFIG_PCI
51 static int enable[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
52 static int boot_devs;
53 module_param_array(enable, bool, boot_devs, 0444);
54 MODULE_PARM_DESC(enable, "Enable cards to allocate buffers.");
55 #endif
56
57 /*
58  */
59
60 void *snd_malloc_sgbuf_pages(struct device *device,
61                              size_t size, struct snd_dma_buffer *dmab,
62                              size_t *res_size);
63 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
64
65 /*
66  */
67
68 static DECLARE_MUTEX(list_mutex);
69 static LIST_HEAD(mem_list_head);
70
71 /* buffer preservation list */
72 struct snd_mem_list {
73         struct snd_dma_buffer buffer;
74         unsigned int id;
75         struct list_head list;
76 };
77
78 /* id for pre-allocated buffers */
79 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
80
81 #ifdef CONFIG_SND_DEBUG
82 #define __ASTRING__(x) #x
83 #define snd_assert(expr, args...) do {\
84         if (!(expr)) {\
85                 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
86                 args;\
87         }\
88 } while (0)
89 #else
90 #define snd_assert(expr, args...) /**/
91 #endif
92
93 /*
94  *  Hacks
95  */
96
97 #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
98 /*
99  * A hack to allocate large buffers via dma_alloc_coherent()
100  *
101  * since dma_alloc_coherent always tries GFP_DMA when the requested
102  * pci memory region is below 32bit, it happens quite often that even
103  * 2 order of pages cannot be allocated.
104  *
105  * so in the following, we allocate at first without dma_mask, so that
106  * allocation will be done without GFP_DMA.  if the area doesn't match
107  * with the requested region, then realloate with the original dma_mask
108  * again.
109  *
110  * Really, we want to move this type of thing into dma_alloc_coherent()
111  * so dma_mask doesn't have to be messed with.
112  */
113
114 static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
115                                          dma_addr_t *dma_handle, int flags)
116 {
117         void *ret;
118         u64 dma_mask, coherent_dma_mask;
119
120         if (dev == NULL || !dev->dma_mask)
121                 return dma_alloc_coherent(dev, size, dma_handle, flags);
122         dma_mask = *dev->dma_mask;
123         coherent_dma_mask = dev->coherent_dma_mask;
124         *dev->dma_mask = 0xffffffff;    /* do without masking */
125         dev->coherent_dma_mask = 0xffffffff;    /* do without masking */
126         ret = dma_alloc_coherent(dev, size, dma_handle, flags);
127         *dev->dma_mask = dma_mask;      /* restore */
128         dev->coherent_dma_mask = coherent_dma_mask;     /* restore */
129         if (ret) {
130                 /* obtained address is out of range? */
131                 if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) {
132                         /* reallocate with the proper mask */
133                         dma_free_coherent(dev, size, ret, *dma_handle);
134                         ret = dma_alloc_coherent(dev, size, dma_handle, flags);
135                 }
136         } else {
137                 /* wish to success now with the proper mask... */
138                 if (dma_mask != 0xffffffffUL) {
139                         /* allocation with GFP_ATOMIC to avoid the long stall */
140                         flags &= ~GFP_KERNEL;
141                         flags |= GFP_ATOMIC;
142                         ret = dma_alloc_coherent(dev, size, dma_handle, flags);
143                 }
144         }
145         return ret;
146 }
147
148 /* redefine dma_alloc_coherent for some architectures */
149 #undef dma_alloc_coherent
150 #define dma_alloc_coherent snd_dma_hack_alloc_coherent
151
152 #endif /* arch */
153
154 #if ! defined(__arm__)
155 #define NEED_RESERVE_PAGES
156 #endif
157
158 /*
159  *
160  *  Generic memory allocators
161  *
162  */
163
164 static long snd_allocated_pages; /* holding the number of allocated pages */
165
166 static inline void inc_snd_pages(int order)
167 {
168         snd_allocated_pages += 1 << order;
169 }
170
171 static inline void dec_snd_pages(int order)
172 {
173         snd_allocated_pages -= 1 << order;
174 }
175
176 static void mark_pages(struct page *page, int order)
177 {
178         struct page *last_page = page + (1 << order);
179         while (page < last_page)
180                 SetPageReserved(page++);
181 }
182
183 static void unmark_pages(struct page *page, int order)
184 {
185         struct page *last_page = page + (1 << order);
186         while (page < last_page)
187                 ClearPageReserved(page++);
188 }
189
190 /**
191  * snd_malloc_pages - allocate pages with the given size
192  * @size: the size to allocate in bytes
193  * @gfp_flags: the allocation conditions, GFP_XXX
194  *
195  * Allocates the physically contiguous pages with the given size.
196  *
197  * Returns the pointer of the buffer, or NULL if no enoguh memory.
198  */
199 void *snd_malloc_pages(size_t size, unsigned int gfp_flags)
200 {
201         int pg;
202         void *res;
203
204         snd_assert(size > 0, return NULL);
205         snd_assert(gfp_flags != 0, return NULL);
206         pg = get_order(size);
207         if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) {
208                 mark_pages(virt_to_page(res), pg);
209                 inc_snd_pages(pg);
210         }
211         return res;
212 }
213
214 /**
215  * snd_free_pages - release the pages
216  * @ptr: the buffer pointer to release
217  * @size: the allocated buffer size
218  *
219  * Releases the buffer allocated via snd_malloc_pages().
220  */
221 void snd_free_pages(void *ptr, size_t size)
222 {
223         int pg;
224
225         if (ptr == NULL)
226                 return;
227         pg = get_order(size);
228         dec_snd_pages(pg);
229         unmark_pages(virt_to_page(ptr), pg);
230         free_pages((unsigned long) ptr, pg);
231 }
232
233 /*
234  *
235  *  Bus-specific memory allocators
236  *
237  */
238
239 /* allocate the coherent DMA pages */
240 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
241 {
242         int pg;
243         void *res;
244         unsigned int gfp_flags;
245
246         snd_assert(size > 0, return NULL);
247         snd_assert(dma != NULL, return NULL);
248         pg = get_order(size);
249         gfp_flags = GFP_KERNEL;
250         if (pg > 0)
251                 gfp_flags |= __GFP_NOWARN;
252         res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
253         if (res != NULL) {
254 #ifdef NEED_RESERVE_PAGES
255                 mark_pages(virt_to_page(res), pg); /* should be dma_to_page() */
256 #endif
257                 inc_snd_pages(pg);
258         }
259
260         return res;
261 }
262
263 /* free the coherent DMA pages */
264 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
265                                dma_addr_t dma)
266 {
267         int pg;
268
269         if (ptr == NULL)
270                 return;
271         pg = get_order(size);
272         dec_snd_pages(pg);
273 #ifdef NEED_RESERVE_PAGES
274         unmark_pages(virt_to_page(ptr), pg); /* should be dma_to_page() */
275 #endif
276         dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
277 }
278
279 #ifdef CONFIG_SBUS
280
281 static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
282                                    dma_addr_t *dma_addr)
283 {
284         struct sbus_dev *sdev = (struct sbus_dev *)dev;
285         int pg;
286         void *res;
287
288         snd_assert(size > 0, return NULL);
289         snd_assert(dma_addr != NULL, return NULL);
290         pg = get_order(size);
291         res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
292         if (res != NULL)
293                 inc_snd_pages(pg);
294         return res;
295 }
296
297 static void snd_free_sbus_pages(struct device *dev, size_t size,
298                                 void *ptr, dma_addr_t dma_addr)
299 {
300         struct sbus_dev *sdev = (struct sbus_dev *)dev;
301         int pg;
302
303         if (ptr == NULL)
304                 return;
305         pg = get_order(size);
306         dec_snd_pages(pg);
307         sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
308 }
309
310 #endif /* CONFIG_SBUS */
311
312 /*
313  *
314  *  ALSA generic memory management
315  *
316  */
317
318
319 /**
320  * snd_dma_alloc_pages - allocate the buffer area according to the given type
321  * @type: the DMA buffer type
322  * @device: the device pointer
323  * @size: the buffer size to allocate
324  * @dmab: buffer allocation record to store the allocated data
325  *
326  * Calls the memory-allocator function for the corresponding
327  * buffer type.
328  * 
329  * Returns zero if the buffer with the given size is allocated successfuly,
330  * other a negative value at error.
331  */
332 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
333                         struct snd_dma_buffer *dmab)
334 {
335         snd_assert(size > 0, return -ENXIO);
336         snd_assert(dmab != NULL, return -ENXIO);
337
338         dmab->dev.type = type;
339         dmab->dev.dev = device;
340         dmab->bytes = 0;
341         switch (type) {
342         case SNDRV_DMA_TYPE_CONTINUOUS:
343                 dmab->area = snd_malloc_pages(size, (unsigned long)device);
344                 dmab->addr = 0;
345                 break;
346 #ifdef CONFIG_SBUS
347         case SNDRV_DMA_TYPE_SBUS:
348                 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
349                 break;
350 #endif
351         case SNDRV_DMA_TYPE_DEV:
352                 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
353                 break;
354         case SNDRV_DMA_TYPE_DEV_SG:
355                 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
356                 break;
357         default:
358                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
359                 dmab->area = NULL;
360                 dmab->addr = 0;
361                 return -ENXIO;
362         }
363         if (! dmab->area)
364                 return -ENOMEM;
365         dmab->bytes = size;
366         return 0;
367 }
368
369 /**
370  * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
371  * @type: the DMA buffer type
372  * @device: the device pointer
373  * @size: the buffer size to allocate
374  * @dmab: buffer allocation record to store the allocated data
375  *
376  * Calls the memory-allocator function for the corresponding
377  * buffer type.  When no space is left, this function reduces the size and
378  * tries to allocate again.  The size actually allocated is stored in
379  * res_size argument.
380  * 
381  * Returns zero if the buffer with the given size is allocated successfuly,
382  * other a negative value at error.
383  */
384 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
385                                  struct snd_dma_buffer *dmab)
386 {
387         int err;
388
389         snd_assert(size > 0, return -ENXIO);
390         snd_assert(dmab != NULL, return -ENXIO);
391
392         while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
393                 if (err != -ENOMEM)
394                         return err;
395                 size >>= 1;
396                 if (size <= PAGE_SIZE)
397                         return -ENOMEM;
398         }
399         if (! dmab->area)
400                 return -ENOMEM;
401         return 0;
402 }
403
404
405 /**
406  * snd_dma_free_pages - release the allocated buffer
407  * @dmab: the buffer allocation record to release
408  *
409  * Releases the allocated buffer via snd_dma_alloc_pages().
410  */
411 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
412 {
413         switch (dmab->dev.type) {
414         case SNDRV_DMA_TYPE_CONTINUOUS:
415                 snd_free_pages(dmab->area, dmab->bytes);
416                 break;
417 #ifdef CONFIG_SBUS
418         case SNDRV_DMA_TYPE_SBUS:
419                 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
420                 break;
421 #endif
422         case SNDRV_DMA_TYPE_DEV:
423                 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
424                 break;
425         case SNDRV_DMA_TYPE_DEV_SG:
426                 snd_free_sgbuf_pages(dmab);
427                 break;
428         default:
429                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
430         }
431 }
432
433
434 /**
435  * snd_dma_get_reserved - get the reserved buffer for the given device
436  * @dmab: the buffer allocation record to store
437  * @id: the buffer id
438  *
439  * Looks for the reserved-buffer list and re-uses if the same buffer
440  * is found in the list.  When the buffer is found, it's removed from the free list.
441  *
442  * Returns the size of buffer if the buffer is found, or zero if not found.
443  */
444 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
445 {
446         struct list_head *p;
447         struct snd_mem_list *mem;
448
449         snd_assert(dmab, return 0);
450
451         down(&list_mutex);
452         list_for_each(p, &mem_list_head) {
453                 mem = list_entry(p, struct snd_mem_list, list);
454                 if (mem->id == id &&
455                     ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev))) {
456                         list_del(p);
457                         *dmab = mem->buffer;
458                         kfree(mem);
459                         up(&list_mutex);
460                         return dmab->bytes;
461                 }
462         }
463         up(&list_mutex);
464         return 0;
465 }
466
467 /**
468  * snd_dma_reserve_buf - reserve the buffer
469  * @dmab: the buffer to reserve
470  * @id: the buffer id
471  *
472  * Reserves the given buffer as a reserved buffer.
473  * 
474  * Returns zero if successful, or a negative code at error.
475  */
476 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
477 {
478         struct snd_mem_list *mem;
479
480         snd_assert(dmab, return -EINVAL);
481         mem = kmalloc(sizeof(*mem), GFP_KERNEL);
482         if (! mem)
483                 return -ENOMEM;
484         down(&list_mutex);
485         mem->buffer = *dmab;
486         mem->id = id;
487         list_add_tail(&mem->list, &mem_list_head);
488         up(&list_mutex);
489         return 0;
490 }
491
492 /*
493  * purge all reserved buffers
494  */
495 static void free_all_reserved_pages(void)
496 {
497         struct list_head *p;
498         struct snd_mem_list *mem;
499
500         down(&list_mutex);
501         while (! list_empty(&mem_list_head)) {
502                 p = mem_list_head.next;
503                 mem = list_entry(p, struct snd_mem_list, list);
504                 list_del(p);
505                 snd_dma_free_pages(&mem->buffer);
506                 kfree(mem);
507         }
508         up(&list_mutex);
509 }
510
511
512
513 /*
514  * allocation of buffers for pre-defined devices
515  */
516
517 #ifdef CONFIG_PCI
518 /* FIXME: for pci only - other bus? */
519 struct prealloc_dev {
520         unsigned short vendor;
521         unsigned short device;
522         unsigned long dma_mask;
523         unsigned int size;
524         unsigned int buffers;
525 };
526
527 #define HAMMERFALL_BUFFER_SIZE    (16*1024*4*(26+1)+0x10000)
528
529 static struct prealloc_dev prealloc_devices[] __initdata = {
530         {
531                 /* hammerfall */
532                 .vendor = 0x10ee,
533                 .device = 0x3fc4,
534                 .dma_mask = 0xffffffff,
535                 .size = HAMMERFALL_BUFFER_SIZE,
536                 .buffers = 2
537         },
538         {
539                 /* HDSP */
540                 .vendor = 0x10ee,
541                 .device = 0x3fc5,
542                 .dma_mask = 0xffffffff,
543                 .size = HAMMERFALL_BUFFER_SIZE,
544                 .buffers = 2
545         },
546         { }, /* terminator */
547 };
548
549 static void __init preallocate_cards(void)
550 {
551         struct pci_dev *pci = NULL;
552         int card;
553
554         card = 0;
555
556         while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
557                 struct prealloc_dev *dev;
558                 unsigned int i;
559                 if (card >= SNDRV_CARDS)
560                         break;
561                 for (dev = prealloc_devices; dev->vendor; dev++) {
562                         if (dev->vendor == pci->vendor && dev->device == pci->device)
563                                 break;
564                 }
565                 if (! dev->vendor)
566                         continue;
567                 if (! enable[card++]) {
568                         printk(KERN_DEBUG "snd-page-alloc: skipping card %d, device %04x:%04x\n", card, pci->vendor, pci->device);
569                         continue;
570                 }
571                         
572                 if (pci_set_dma_mask(pci, dev->dma_mask) < 0 ||
573                     pci_set_consistent_dma_mask(pci, dev->dma_mask) < 0) {
574                         printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", dev->dma_mask, dev->vendor, dev->device);
575                         continue;
576                 }
577                 for (i = 0; i < dev->buffers; i++) {
578                         struct snd_dma_buffer dmab;
579                         memset(&dmab, 0, sizeof(dmab));
580                         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
581                                                 dev->size, &dmab) < 0)
582                                 printk(KERN_WARNING "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", dev->size);
583                         else
584                                 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
585                 }
586         }
587 }
588 #else
589 #define preallocate_cards()     /* NOP */
590 #endif
591
592
593 #ifdef CONFIG_PROC_FS
594 /*
595  * proc file interface
596  */
597 static int snd_mem_proc_read(char *page, char **start, off_t off,
598                              int count, int *eof, void *data)
599 {
600         int len = 0;
601         long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
602         struct list_head *p;
603         struct snd_mem_list *mem;
604         int devno;
605         static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
606
607         down(&list_mutex);
608         len += snprintf(page + len, count - len,
609                         "pages  : %li bytes (%li pages per %likB)\n",
610                         pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
611         devno = 0;
612         list_for_each(p, &mem_list_head) {
613                 mem = list_entry(p, struct snd_mem_list, list);
614                 devno++;
615                 len += snprintf(page + len, count - len,
616                                 "buffer %d : ID %08x : type %s\n",
617                                 devno, mem->id, types[mem->buffer.dev.type]);
618                 len += snprintf(page + len, count - len,
619                                 "  addr = 0x%lx, size = %d bytes\n",
620                                 (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes);
621         }
622         up(&list_mutex);
623         return len;
624 }
625 #endif /* CONFIG_PROC_FS */
626
627 /*
628  * module entry
629  */
630
631 static int __init snd_mem_init(void)
632 {
633 #ifdef CONFIG_PROC_FS
634         create_proc_read_entry("driver/snd-page-alloc", 0, NULL, snd_mem_proc_read, NULL);
635 #endif
636         preallocate_cards();
637         return 0;
638 }
639
640 static void __exit snd_mem_exit(void)
641 {
642         remove_proc_entry("driver/snd-page-alloc", NULL);
643         free_all_reserved_pages();
644         if (snd_allocated_pages > 0)
645                 printk(KERN_ERR "snd-malloc: Memory leak?  pages not freed = %li\n", snd_allocated_pages);
646 }
647
648
649 module_init(snd_mem_init)
650 module_exit(snd_mem_exit)
651
652
653 /*
654  * exports
655  */
656 EXPORT_SYMBOL(snd_dma_alloc_pages);
657 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
658 EXPORT_SYMBOL(snd_dma_free_pages);
659
660 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
661 EXPORT_SYMBOL(snd_dma_reserve_buf);
662
663 EXPORT_SYMBOL(snd_malloc_pages);
664 EXPORT_SYMBOL(snd_free_pages);