Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / memalloc.c
blob344a83fd7c2e6b884cc318cad9518dfa733def8e
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Takashi Iwai <tiwai@suse.de>
4 *
5 * Generic memory allocators
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.
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.
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
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
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");
45 #ifndef SNDRV_CARDS
46 #define SNDRV_CARDS 8
47 #endif
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 module_param_array(enable, bool, NULL, 0444);
53 MODULE_PARM_DESC(enable, "Enable cards to allocate buffers.");
54 #endif
59 void *snd_malloc_sgbuf_pages(struct device *device,
60 size_t size, struct snd_dma_buffer *dmab,
61 size_t *res_size);
62 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
67 static DECLARE_MUTEX(list_mutex);
68 static LIST_HEAD(mem_list_head);
70 /* buffer preservation list */
71 struct snd_mem_list {
72 struct snd_dma_buffer buffer;
73 unsigned int id;
74 struct list_head list;
77 /* id for pre-allocated buffers */
78 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
80 #ifdef CONFIG_SND_DEBUG
81 #define __ASTRING__(x) #x
82 #define snd_assert(expr, args...) do {\
83 if (!(expr)) {\
84 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
85 args;\
87 } while (0)
88 #else
89 #define snd_assert(expr, args...) /**/
90 #endif
93 * Hacks
96 #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
98 * A hack to allocate large buffers via dma_alloc_coherent()
100 * since dma_alloc_coherent always tries GFP_DMA when the requested
101 * pci memory region is below 32bit, it happens quite often that even
102 * 2 order of pages cannot be allocated.
104 * so in the following, we allocate at first without dma_mask, so that
105 * allocation will be done without GFP_DMA. if the area doesn't match
106 * with the requested region, then realloate with the original dma_mask
107 * again.
109 * Really, we want to move this type of thing into dma_alloc_coherent()
110 * so dma_mask doesn't have to be messed with.
113 static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
114 dma_addr_t *dma_handle, int flags)
116 void *ret;
117 u64 dma_mask, coherent_dma_mask;
119 if (dev == NULL || !dev->dma_mask)
120 return dma_alloc_coherent(dev, size, dma_handle, flags);
121 dma_mask = *dev->dma_mask;
122 coherent_dma_mask = dev->coherent_dma_mask;
123 *dev->dma_mask = 0xffffffff; /* do without masking */
124 dev->coherent_dma_mask = 0xffffffff; /* do without masking */
125 ret = dma_alloc_coherent(dev, size, dma_handle, flags);
126 *dev->dma_mask = dma_mask; /* restore */
127 dev->coherent_dma_mask = coherent_dma_mask; /* restore */
128 if (ret) {
129 /* obtained address is out of range? */
130 if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) {
131 /* reallocate with the proper mask */
132 dma_free_coherent(dev, size, ret, *dma_handle);
133 ret = dma_alloc_coherent(dev, size, dma_handle, flags);
135 } else {
136 /* wish to success now with the proper mask... */
137 if (dma_mask != 0xffffffffUL) {
138 /* allocation with GFP_ATOMIC to avoid the long stall */
139 flags &= ~GFP_KERNEL;
140 flags |= GFP_ATOMIC;
141 ret = dma_alloc_coherent(dev, size, dma_handle, flags);
144 return ret;
147 /* redefine dma_alloc_coherent for some architectures */
148 #undef dma_alloc_coherent
149 #define dma_alloc_coherent snd_dma_hack_alloc_coherent
151 #endif /* arch */
153 #if ! defined(__arm__)
154 #define NEED_RESERVE_PAGES
155 #endif
159 * Generic memory allocators
163 static long snd_allocated_pages; /* holding the number of allocated pages */
165 static inline void inc_snd_pages(int order)
167 snd_allocated_pages += 1 << order;
170 static inline void dec_snd_pages(int order)
172 snd_allocated_pages -= 1 << order;
175 static void mark_pages(struct page *page, int order)
177 struct page *last_page = page + (1 << order);
178 while (page < last_page)
179 SetPageReserved(page++);
182 static void unmark_pages(struct page *page, int order)
184 struct page *last_page = page + (1 << order);
185 while (page < last_page)
186 ClearPageReserved(page++);
190 * snd_malloc_pages - allocate pages with the given size
191 * @size: the size to allocate in bytes
192 * @gfp_flags: the allocation conditions, GFP_XXX
194 * Allocates the physically contiguous pages with the given size.
196 * Returns the pointer of the buffer, or NULL if no enoguh memory.
198 void *snd_malloc_pages(size_t size, unsigned int gfp_flags)
200 int pg;
201 void *res;
203 snd_assert(size > 0, return NULL);
204 snd_assert(gfp_flags != 0, return NULL);
205 pg = get_order(size);
206 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) {
207 mark_pages(virt_to_page(res), pg);
208 inc_snd_pages(pg);
210 return res;
214 * snd_free_pages - release the pages
215 * @ptr: the buffer pointer to release
216 * @size: the allocated buffer size
218 * Releases the buffer allocated via snd_malloc_pages().
220 void snd_free_pages(void *ptr, size_t size)
222 int pg;
224 if (ptr == NULL)
225 return;
226 pg = get_order(size);
227 dec_snd_pages(pg);
228 unmark_pages(virt_to_page(ptr), pg);
229 free_pages((unsigned long) ptr, pg);
234 * Bus-specific memory allocators
238 /* allocate the coherent DMA pages */
239 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
241 int pg;
242 void *res;
243 unsigned int gfp_flags;
245 snd_assert(size > 0, return NULL);
246 snd_assert(dma != NULL, return NULL);
247 pg = get_order(size);
248 gfp_flags = GFP_KERNEL
249 | __GFP_NORETRY /* don't trigger OOM-killer */
250 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
251 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
252 if (res != NULL) {
253 #ifdef NEED_RESERVE_PAGES
254 mark_pages(virt_to_page(res), pg); /* should be dma_to_page() */
255 #endif
256 inc_snd_pages(pg);
259 return res;
262 /* free the coherent DMA pages */
263 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
264 dma_addr_t dma)
266 int pg;
268 if (ptr == NULL)
269 return;
270 pg = get_order(size);
271 dec_snd_pages(pg);
272 #ifdef NEED_RESERVE_PAGES
273 unmark_pages(virt_to_page(ptr), pg); /* should be dma_to_page() */
274 #endif
275 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
278 #ifdef CONFIG_SBUS
280 static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
281 dma_addr_t *dma_addr)
283 struct sbus_dev *sdev = (struct sbus_dev *)dev;
284 int pg;
285 void *res;
287 snd_assert(size > 0, return NULL);
288 snd_assert(dma_addr != NULL, return NULL);
289 pg = get_order(size);
290 res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
291 if (res != NULL)
292 inc_snd_pages(pg);
293 return res;
296 static void snd_free_sbus_pages(struct device *dev, size_t size,
297 void *ptr, dma_addr_t dma_addr)
299 struct sbus_dev *sdev = (struct sbus_dev *)dev;
300 int pg;
302 if (ptr == NULL)
303 return;
304 pg = get_order(size);
305 dec_snd_pages(pg);
306 sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
309 #endif /* CONFIG_SBUS */
313 * ALSA generic memory management
319 * snd_dma_alloc_pages - allocate the buffer area according to the given type
320 * @type: the DMA buffer type
321 * @device: the device pointer
322 * @size: the buffer size to allocate
323 * @dmab: buffer allocation record to store the allocated data
325 * Calls the memory-allocator function for the corresponding
326 * buffer type.
328 * Returns zero if the buffer with the given size is allocated successfuly,
329 * other a negative value at error.
331 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
332 struct snd_dma_buffer *dmab)
334 snd_assert(size > 0, return -ENXIO);
335 snd_assert(dmab != NULL, return -ENXIO);
337 dmab->dev.type = type;
338 dmab->dev.dev = device;
339 dmab->bytes = 0;
340 switch (type) {
341 case SNDRV_DMA_TYPE_CONTINUOUS:
342 dmab->area = snd_malloc_pages(size, (unsigned long)device);
343 dmab->addr = 0;
344 break;
345 #ifdef CONFIG_SBUS
346 case SNDRV_DMA_TYPE_SBUS:
347 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
348 break;
349 #endif
350 case SNDRV_DMA_TYPE_DEV:
351 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
352 break;
353 case SNDRV_DMA_TYPE_DEV_SG:
354 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
355 break;
356 default:
357 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
358 dmab->area = NULL;
359 dmab->addr = 0;
360 return -ENXIO;
362 if (! dmab->area)
363 return -ENOMEM;
364 dmab->bytes = size;
365 return 0;
369 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
370 * @type: the DMA buffer type
371 * @device: the device pointer
372 * @size: the buffer size to allocate
373 * @dmab: buffer allocation record to store the allocated data
375 * Calls the memory-allocator function for the corresponding
376 * buffer type. When no space is left, this function reduces the size and
377 * tries to allocate again. The size actually allocated is stored in
378 * res_size argument.
380 * Returns zero if the buffer with the given size is allocated successfuly,
381 * other a negative value at error.
383 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
384 struct snd_dma_buffer *dmab)
386 int err;
388 snd_assert(size > 0, return -ENXIO);
389 snd_assert(dmab != NULL, return -ENXIO);
391 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
392 if (err != -ENOMEM)
393 return err;
394 size >>= 1;
395 if (size <= PAGE_SIZE)
396 return -ENOMEM;
398 if (! dmab->area)
399 return -ENOMEM;
400 return 0;
405 * snd_dma_free_pages - release the allocated buffer
406 * @dmab: the buffer allocation record to release
408 * Releases the allocated buffer via snd_dma_alloc_pages().
410 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
412 switch (dmab->dev.type) {
413 case SNDRV_DMA_TYPE_CONTINUOUS:
414 snd_free_pages(dmab->area, dmab->bytes);
415 break;
416 #ifdef CONFIG_SBUS
417 case SNDRV_DMA_TYPE_SBUS:
418 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
419 break;
420 #endif
421 case SNDRV_DMA_TYPE_DEV:
422 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
423 break;
424 case SNDRV_DMA_TYPE_DEV_SG:
425 snd_free_sgbuf_pages(dmab);
426 break;
427 default:
428 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
434 * snd_dma_get_reserved - get the reserved buffer for the given device
435 * @dmab: the buffer allocation record to store
436 * @id: the buffer id
438 * Looks for the reserved-buffer list and re-uses if the same buffer
439 * is found in the list. When the buffer is found, it's removed from the free list.
441 * Returns the size of buffer if the buffer is found, or zero if not found.
443 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
445 struct list_head *p;
446 struct snd_mem_list *mem;
448 snd_assert(dmab, return 0);
450 down(&list_mutex);
451 list_for_each(p, &mem_list_head) {
452 mem = list_entry(p, struct snd_mem_list, list);
453 if (mem->id == id &&
454 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev))) {
455 list_del(p);
456 *dmab = mem->buffer;
457 kfree(mem);
458 up(&list_mutex);
459 return dmab->bytes;
462 up(&list_mutex);
463 return 0;
467 * snd_dma_reserve_buf - reserve the buffer
468 * @dmab: the buffer to reserve
469 * @id: the buffer id
471 * Reserves the given buffer as a reserved buffer.
473 * Returns zero if successful, or a negative code at error.
475 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
477 struct snd_mem_list *mem;
479 snd_assert(dmab, return -EINVAL);
480 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
481 if (! mem)
482 return -ENOMEM;
483 down(&list_mutex);
484 mem->buffer = *dmab;
485 mem->id = id;
486 list_add_tail(&mem->list, &mem_list_head);
487 up(&list_mutex);
488 return 0;
492 * purge all reserved buffers
494 static void free_all_reserved_pages(void)
496 struct list_head *p;
497 struct snd_mem_list *mem;
499 down(&list_mutex);
500 while (! list_empty(&mem_list_head)) {
501 p = mem_list_head.next;
502 mem = list_entry(p, struct snd_mem_list, list);
503 list_del(p);
504 snd_dma_free_pages(&mem->buffer);
505 kfree(mem);
507 up(&list_mutex);
513 * allocation of buffers for pre-defined devices
516 #ifdef CONFIG_PCI
517 /* FIXME: for pci only - other bus? */
518 struct prealloc_dev {
519 unsigned short vendor;
520 unsigned short device;
521 unsigned long dma_mask;
522 unsigned int size;
523 unsigned int buffers;
526 #define HAMMERFALL_BUFFER_SIZE (16*1024*4*(26+1)+0x10000)
528 static struct prealloc_dev prealloc_devices[] __initdata = {
530 /* hammerfall */
531 .vendor = 0x10ee,
532 .device = 0x3fc4,
533 .dma_mask = 0xffffffff,
534 .size = HAMMERFALL_BUFFER_SIZE,
535 .buffers = 2
538 /* HDSP */
539 .vendor = 0x10ee,
540 .device = 0x3fc5,
541 .dma_mask = 0xffffffff,
542 .size = HAMMERFALL_BUFFER_SIZE,
543 .buffers = 2
545 { }, /* terminator */
548 static void __init preallocate_cards(void)
550 struct pci_dev *pci = NULL;
551 int card;
553 card = 0;
555 while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
556 struct prealloc_dev *dev;
557 unsigned int i;
558 if (card >= SNDRV_CARDS)
559 break;
560 for (dev = prealloc_devices; dev->vendor; dev++) {
561 if (dev->vendor == pci->vendor && dev->device == pci->device)
562 break;
564 if (! dev->vendor)
565 continue;
566 if (! enable[card++]) {
567 printk(KERN_DEBUG "snd-page-alloc: skipping card %d, device %04x:%04x\n", card, pci->vendor, pci->device);
568 continue;
571 if (pci_set_dma_mask(pci, dev->dma_mask) < 0 ||
572 pci_set_consistent_dma_mask(pci, dev->dma_mask) < 0) {
573 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", dev->dma_mask, dev->vendor, dev->device);
574 continue;
576 for (i = 0; i < dev->buffers; i++) {
577 struct snd_dma_buffer dmab;
578 memset(&dmab, 0, sizeof(dmab));
579 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
580 dev->size, &dmab) < 0)
581 printk(KERN_WARNING "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", dev->size);
582 else
583 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
587 #else
588 #define preallocate_cards() /* NOP */
589 #endif
592 #ifdef CONFIG_PROC_FS
594 * proc file interface
596 static int snd_mem_proc_read(char *page, char **start, off_t off,
597 int count, int *eof, void *data)
599 int len = 0;
600 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
601 struct list_head *p;
602 struct snd_mem_list *mem;
603 int devno;
604 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
606 down(&list_mutex);
607 len += snprintf(page + len, count - len,
608 "pages : %li bytes (%li pages per %likB)\n",
609 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
610 devno = 0;
611 list_for_each(p, &mem_list_head) {
612 mem = list_entry(p, struct snd_mem_list, list);
613 devno++;
614 len += snprintf(page + len, count - len,
615 "buffer %d : ID %08x : type %s\n",
616 devno, mem->id, types[mem->buffer.dev.type]);
617 len += snprintf(page + len, count - len,
618 " addr = 0x%lx, size = %d bytes\n",
619 (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes);
621 up(&list_mutex);
622 return len;
624 #endif /* CONFIG_PROC_FS */
627 * module entry
630 static int __init snd_mem_init(void)
632 #ifdef CONFIG_PROC_FS
633 create_proc_read_entry("driver/snd-page-alloc", 0, NULL, snd_mem_proc_read, NULL);
634 #endif
635 preallocate_cards();
636 return 0;
639 static void __exit snd_mem_exit(void)
641 remove_proc_entry("driver/snd-page-alloc", NULL);
642 free_all_reserved_pages();
643 if (snd_allocated_pages > 0)
644 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
648 module_init(snd_mem_init)
649 module_exit(snd_mem_exit)
653 * exports
655 EXPORT_SYMBOL(snd_dma_alloc_pages);
656 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
657 EXPORT_SYMBOL(snd_dma_free_pages);
659 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
660 EXPORT_SYMBOL(snd_dma_reserve_buf);
662 EXPORT_SYMBOL(snd_malloc_pages);
663 EXPORT_SYMBOL(snd_free_pages);