2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Takashi Iwai <tiwai@suse.de>
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>
31 #include <linux/dma-mapping.h>
32 #include <linux/moduleparam.h>
33 #include <asm/semaphore.h>
34 #include <sound/memalloc.h>
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");
49 /* FIXME: so far only some PCI devices have the preallocation table */
51 static int enable[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
53 module_param_array(enable, bool, boot_devs, 0444);
54 MODULE_PARM_DESC(enable, "Enable cards to allocate buffers.");
60 void *snd_malloc_sgbuf_pages(struct device *device,
61 size_t size, struct snd_dma_buffer *dmab,
63 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
68 static DECLARE_MUTEX(list_mutex);
69 static LIST_HEAD(mem_list_head);
71 /* buffer preservation list */
73 struct snd_dma_buffer buffer;
75 struct list_head list;
78 /* id for pre-allocated buffers */
79 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
81 #ifdef CONFIG_SND_DEBUG
82 #define __ASTRING__(x) #x
83 #define snd_assert(expr, args...) do {\
85 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
90 #define snd_assert(expr, args...) /**/
97 #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
99 * A hack to allocate large buffers via dma_alloc_coherent()
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.
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
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.
114 static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
115 dma_addr_t *dma_handle, int flags)
118 u64 dma_mask, coherent_dma_mask;
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 */
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);
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;
142 ret = dma_alloc_coherent(dev, size, dma_handle, flags);
148 /* redefine dma_alloc_coherent for some architectures */
149 #undef dma_alloc_coherent
150 #define dma_alloc_coherent snd_dma_hack_alloc_coherent
154 #if ! defined(__arm__)
155 #define NEED_RESERVE_PAGES
160 * Generic memory allocators
164 static long snd_allocated_pages; /* holding the number of allocated pages */
166 static inline void inc_snd_pages(int order)
168 snd_allocated_pages += 1 << order;
171 static inline void dec_snd_pages(int order)
173 snd_allocated_pages -= 1 << order;
176 static void mark_pages(struct page *page, int order)
178 struct page *last_page = page + (1 << order);
179 while (page < last_page)
180 SetPageReserved(page++);
183 static void unmark_pages(struct page *page, int order)
185 struct page *last_page = page + (1 << order);
186 while (page < last_page)
187 ClearPageReserved(page++);
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
195 * Allocates the physically contiguous pages with the given size.
197 * Returns the pointer of the buffer, or NULL if no enoguh memory.
199 void *snd_malloc_pages(size_t size, unsigned int gfp_flags)
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);
215 * snd_free_pages - release the pages
216 * @ptr: the buffer pointer to release
217 * @size: the allocated buffer size
219 * Releases the buffer allocated via snd_malloc_pages().
221 void snd_free_pages(void *ptr, size_t size)
227 pg = get_order(size);
229 unmark_pages(virt_to_page(ptr), pg);
230 free_pages((unsigned long) ptr, pg);
235 * Bus-specific memory allocators
239 /* allocate the coherent DMA pages */
240 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
244 unsigned int gfp_flags;
246 snd_assert(size > 0, return NULL);
247 snd_assert(dma != NULL, return NULL);
248 pg = get_order(size);
249 gfp_flags = GFP_KERNEL;
251 gfp_flags |= __GFP_NOWARN;
252 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
254 #ifdef NEED_RESERVE_PAGES
255 mark_pages(virt_to_page(res), pg); /* should be dma_to_page() */
263 /* free the coherent DMA pages */
264 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
271 pg = get_order(size);
273 #ifdef NEED_RESERVE_PAGES
274 unmark_pages(virt_to_page(ptr), pg); /* should be dma_to_page() */
276 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
281 static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
282 dma_addr_t *dma_addr)
284 struct sbus_dev *sdev = (struct sbus_dev *)dev;
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);
297 static void snd_free_sbus_pages(struct device *dev, size_t size,
298 void *ptr, dma_addr_t dma_addr)
300 struct sbus_dev *sdev = (struct sbus_dev *)dev;
305 pg = get_order(size);
307 sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
310 #endif /* CONFIG_SBUS */
314 * ALSA generic memory management
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
326 * Calls the memory-allocator function for the corresponding
329 * Returns zero if the buffer with the given size is allocated successfuly,
330 * other a negative value at error.
332 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
333 struct snd_dma_buffer *dmab)
335 snd_assert(size > 0, return -ENXIO);
336 snd_assert(dmab != NULL, return -ENXIO);
338 dmab->dev.type = type;
339 dmab->dev.dev = device;
342 case SNDRV_DMA_TYPE_CONTINUOUS:
343 dmab->area = snd_malloc_pages(size, (unsigned long)device);
347 case SNDRV_DMA_TYPE_SBUS:
348 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
351 case SNDRV_DMA_TYPE_DEV:
352 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
354 case SNDRV_DMA_TYPE_DEV_SG:
355 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
358 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
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
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
381 * Returns zero if the buffer with the given size is allocated successfuly,
382 * other a negative value at error.
384 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
385 struct snd_dma_buffer *dmab)
389 snd_assert(size > 0, return -ENXIO);
390 snd_assert(dmab != NULL, return -ENXIO);
392 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
396 if (size <= PAGE_SIZE)
406 * snd_dma_free_pages - release the allocated buffer
407 * @dmab: the buffer allocation record to release
409 * Releases the allocated buffer via snd_dma_alloc_pages().
411 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
413 switch (dmab->dev.type) {
414 case SNDRV_DMA_TYPE_CONTINUOUS:
415 snd_free_pages(dmab->area, dmab->bytes);
418 case SNDRV_DMA_TYPE_SBUS:
419 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
422 case SNDRV_DMA_TYPE_DEV:
423 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
425 case SNDRV_DMA_TYPE_DEV_SG:
426 snd_free_sgbuf_pages(dmab);
429 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
435 * snd_dma_get_reserved - get the reserved buffer for the given device
436 * @dmab: the buffer allocation record to store
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.
442 * Returns the size of buffer if the buffer is found, or zero if not found.
444 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
447 struct snd_mem_list *mem;
449 snd_assert(dmab, return 0);
452 list_for_each(p, &mem_list_head) {
453 mem = list_entry(p, struct snd_mem_list, list);
455 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev))) {
468 * snd_dma_reserve_buf - reserve the buffer
469 * @dmab: the buffer to reserve
472 * Reserves the given buffer as a reserved buffer.
474 * Returns zero if successful, or a negative code at error.
476 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
478 struct snd_mem_list *mem;
480 snd_assert(dmab, return -EINVAL);
481 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
487 list_add_tail(&mem->list, &mem_list_head);
493 * purge all reserved buffers
495 static void free_all_reserved_pages(void)
498 struct snd_mem_list *mem;
501 while (! list_empty(&mem_list_head)) {
502 p = mem_list_head.next;
503 mem = list_entry(p, struct snd_mem_list, list);
505 snd_dma_free_pages(&mem->buffer);
514 * allocation of buffers for pre-defined devices
518 /* FIXME: for pci only - other bus? */
519 struct prealloc_dev {
520 unsigned short vendor;
521 unsigned short device;
522 unsigned long dma_mask;
524 unsigned int buffers;
527 #define HAMMERFALL_BUFFER_SIZE (16*1024*4*(26+1)+0x10000)
529 static struct prealloc_dev prealloc_devices[] __initdata = {
534 .dma_mask = 0xffffffff,
535 .size = HAMMERFALL_BUFFER_SIZE,
542 .dma_mask = 0xffffffff,
543 .size = HAMMERFALL_BUFFER_SIZE,
546 { }, /* terminator */
549 static void __init preallocate_cards(void)
551 struct pci_dev *pci = NULL;
556 while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
557 struct prealloc_dev *dev;
559 if (card >= SNDRV_CARDS)
561 for (dev = prealloc_devices; dev->vendor; dev++) {
562 if (dev->vendor == pci->vendor && dev->device == pci->device)
567 if (! enable[card++]) {
568 printk(KERN_DEBUG "snd-page-alloc: skipping card %d, device %04x:%04x\n", card, pci->vendor, pci->device);
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);
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);
584 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
589 #define preallocate_cards() /* NOP */
593 #ifdef CONFIG_PROC_FS
595 * proc file interface
597 static int snd_mem_proc_read(char *page, char **start, off_t off,
598 int count, int *eof, void *data)
601 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
603 struct snd_mem_list *mem;
605 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
608 len += snprintf(page + len, count - len,
609 "pages : %li bytes (%li pages per %likB)\n",
610 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
612 list_for_each(p, &mem_list_head) {
613 mem = list_entry(p, struct snd_mem_list, list);
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);
625 #endif /* CONFIG_PROC_FS */
631 static int __init snd_mem_init(void)
633 #ifdef CONFIG_PROC_FS
634 create_proc_read_entry("driver/snd-page-alloc", 0, NULL, snd_mem_proc_read, NULL);
640 static void __exit snd_mem_exit(void)
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);
649 module_init(snd_mem_init)
650 module_exit(snd_mem_exit)
656 EXPORT_SYMBOL(snd_dma_alloc_pages);
657 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
658 EXPORT_SYMBOL(snd_dma_free_pages);
660 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
661 EXPORT_SYMBOL(snd_dma_reserve_buf);
663 EXPORT_SYMBOL(snd_malloc_pages);
664 EXPORT_SYMBOL(snd_free_pages);