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 <asm/semaphore.h>
33 #include <sound/memalloc.h>
39 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
40 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
41 MODULE_LICENSE("GPL");
47 static int enable[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
48 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
49 MODULE_PARM_DESC(enable, "Enable cards to allocate buffers.");
54 void *snd_malloc_sgbuf_pages(const struct snd_dma_device *dev,
55 size_t size, struct snd_dma_buffer *dmab,
57 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
62 static DECLARE_MUTEX(list_mutex);
63 static LIST_HEAD(mem_list_head);
65 /* buffer preservation list */
67 struct snd_dma_device dev;
68 struct snd_dma_buffer buffer;
70 struct list_head list;
73 /* id for pre-allocated buffers */
74 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
76 #ifdef CONFIG_SND_DEBUG
77 #define __ASTRING__(x) #x
78 #define snd_assert(expr, args...) do {\
80 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
85 #define snd_assert(expr, args...) /**/
92 static void *snd_dma_alloc_coherent1(struct device *dev, size_t size,
93 dma_addr_t *dma_handle, int flags)
96 return dma_alloc_coherent(dev, size, dma_handle, flags);
97 else /* FIXME: dma_alloc_coherent does't always accept dev=NULL */
98 return pci_alloc_consistent(NULL, size, dma_handle);
101 static void snd_dma_free_coherent1(struct device *dev, size_t size, void *dma_addr,
102 dma_addr_t dma_handle)
105 return dma_free_coherent(dev, size, dma_addr, dma_handle);
107 return pci_free_consistent(NULL, size, dma_addr, dma_handle);
110 #undef dma_alloc_coherent
111 #define dma_alloc_coherent snd_dma_alloc_coherent1
112 #undef dma_free_coherent
113 #define dma_free_coherent snd_dma_free_coherent1
116 #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
119 * A hack to allocate large buffers via dma_alloc_coherent()
121 * since dma_alloc_coherent always tries GFP_DMA when the requested
122 * pci memory region is below 32bit, it happens quite often that even
123 * 2 order of pages cannot be allocated.
125 * so in the following, we allocate at first without dma_mask, so that
126 * allocation will be done without GFP_DMA. if the area doesn't match
127 * with the requested region, then realloate with the original dma_mask
130 * Really, we want to move this type of thing into dma_alloc_coherent()
131 * so dma_mask doesn't have to be messed with.
134 static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
135 dma_addr_t *dma_handle, int flags)
140 if (dev == NULL || !dev->dma_mask)
141 return dma_alloc_coherent(dev, size, dma_handle, flags);
142 dma_mask = *dev->dma_mask;
143 *dev->dma_mask = 0xffffffff; /* do without masking */
144 ret = dma_alloc_coherent(dev, size, dma_handle, flags);
145 *dev->dma_mask = dma_mask; /* restore */
147 /* obtained address is out of range? */
148 if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) {
149 /* reallocate with the proper mask */
150 dma_free_coherent(dev, size, ret, *dma_handle);
151 ret = dma_alloc_coherent(dev, size, dma_handle, flags);
154 /* wish to success now with the proper mask... */
155 if (dma_mask != 0xffffffffUL)
156 ret = dma_alloc_coherent(dev, size, dma_handle, flags);
161 /* redefine dma_alloc_coherent for some architectures */
162 #undef dma_alloc_coherent
163 #define dma_alloc_coherent snd_dma_hack_alloc_coherent
169 * Generic memory allocators
173 static long snd_allocated_pages; /* holding the number of allocated pages */
175 static void mark_pages(void *res, int order)
177 struct page *page = virt_to_page(res);
178 struct page *last_page = page + (1 << order);
179 while (page < last_page)
180 SetPageReserved(page++);
181 snd_allocated_pages += 1 << order;
184 static void unmark_pages(void *res, int order)
186 struct page *page = virt_to_page(res);
187 struct page *last_page = page + (1 << order);
188 while (page < last_page)
189 ClearPageReserved(page++);
190 snd_allocated_pages -= 1 << order;
194 * snd_malloc_pages - allocate pages with the given size
195 * @size: the size to allocate in bytes
196 * @gfp_flags: the allocation conditions, GFP_XXX
198 * Allocates the physically contiguous pages with the given size.
200 * Returns the pointer of the buffer, or NULL if no enoguh memory.
202 void *snd_malloc_pages(size_t size, unsigned int gfp_flags)
207 snd_assert(size > 0, return NULL);
208 snd_assert(gfp_flags != 0, return NULL);
209 for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
210 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) {
217 * snd_malloc_pages_fallback - allocate pages with the given size with fallback
218 * @size: the requested size to allocate in bytes
219 * @gfp_flags: the allocation conditions, GFP_XXX
220 * @res_size: the pointer to store the size of buffer actually allocated
222 * Allocates the physically contiguous pages with the given request
223 * size. When no space is left, this function reduces the size and
224 * tries to allocate again. The size actually allocated is stored in
227 * Returns the pointer of the buffer, or NULL if no enoguh memory.
229 void *snd_malloc_pages_fallback(size_t size, unsigned int gfp_flags, size_t *res_size)
233 snd_assert(size > 0, return NULL);
234 snd_assert(res_size != NULL, return NULL);
236 if ((res = snd_malloc_pages(size, gfp_flags)) != NULL) {
241 } while (size >= PAGE_SIZE);
246 * snd_free_pages - release the pages
247 * @ptr: the buffer pointer to release
248 * @size: the allocated buffer size
250 * Releases the buffer allocated via snd_malloc_pages().
252 void snd_free_pages(void *ptr, size_t size)
258 for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
259 unmark_pages(ptr, pg);
260 free_pages((unsigned long) ptr, pg);
265 * Bus-specific memory allocators
269 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
273 unsigned int gfp_flags;
275 snd_assert(size > 0, return NULL);
276 snd_assert(dma != NULL, return NULL);
277 pg = get_order(size);
278 gfp_flags = GFP_KERNEL;
280 gfp_flags |= __GFP_NOWARN;
281 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
288 static void *snd_malloc_dev_pages_fallback(struct device *dev, size_t size,
289 dma_addr_t *dma, size_t *res_size)
293 snd_assert(res_size != NULL, return NULL);
295 if ((res = snd_malloc_dev_pages(dev, size, dma)) != NULL) {
300 } while (size >= PAGE_SIZE);
304 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
311 pg = get_order(size);
312 unmark_pages(ptr, pg);
313 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
318 static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
319 dma_addr_t *dma_addr)
321 struct sbus_dev *sdev = (struct sbus_dev *)dev;
325 snd_assert(size > 0, return NULL);
326 snd_assert(dma_addr != NULL, return NULL);
327 for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
328 res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
335 static void *snd_malloc_sbus_pages_fallback(struct device *dev, size_t size,
336 dma_addr_t *dma_addr, size_t *res_size)
340 snd_assert(res_size != NULL, return NULL);
342 if ((res = snd_malloc_sbus_pages(dev, size, dma_addr)) != NULL) {
347 } while (size >= PAGE_SIZE);
351 static void snd_free_sbus_pages(struct device *dev, size_t size,
352 void *ptr, dma_addr_t dma_addr)
354 struct sbus_dev *sdev = (struct sbus_dev *)dev;
359 for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
360 unmark_pages(ptr, pg);
361 sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
364 #endif /* CONFIG_SBUS */
368 * ALSA generic memory management
374 * compare the two devices
375 * returns non-zero if matched.
377 static int compare_device(const struct snd_dma_device *a, const struct snd_dma_device *b, int allow_unused)
379 if (a->type != b->type)
381 if (a->id != b->id) {
382 if (! allow_unused || (a->id != SNDRV_DMA_DEVICE_UNUSED && b->id != SNDRV_DMA_DEVICE_UNUSED))
385 return a->dev == b->dev;
389 * snd_dma_alloc_pages - allocate the buffer area according to the given type
390 * @dev: the buffer device info
391 * @size: the buffer size to allocate
392 * @dmab: buffer allocation record to store the allocated data
394 * Calls the memory-allocator function for the corresponding
397 * Returns zero if the buffer with the given size is allocated successfuly,
398 * other a negative value at error.
400 int snd_dma_alloc_pages(const struct snd_dma_device *dev, size_t size,
401 struct snd_dma_buffer *dmab)
403 snd_assert(dev != NULL, return -ENXIO);
404 snd_assert(size > 0, return -ENXIO);
405 snd_assert(dmab != NULL, return -ENXIO);
409 case SNDRV_DMA_TYPE_CONTINUOUS:
410 dmab->area = snd_malloc_pages(size, (unsigned long)dev->dev);
414 case SNDRV_DMA_TYPE_SBUS:
415 dmab->area = snd_malloc_sbus_pages(dev->dev, size, &dmab->addr);
418 case SNDRV_DMA_TYPE_DEV:
419 dmab->area = snd_malloc_dev_pages(dev->dev, size, &dmab->addr);
421 case SNDRV_DMA_TYPE_DEV_SG:
422 snd_malloc_sgbuf_pages(dev, size, dmab, NULL);
425 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
437 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
438 * @dev: the buffer device info
439 * @size: the buffer size to allocate
440 * @dmab: buffer allocation record to store the allocated data
442 * Calls the memory-allocator function for the corresponding
443 * buffer type. When no space is left, this function reduces the size and
444 * tries to allocate again. The size actually allocated is stored in
447 * Returns zero if the buffer with the given size is allocated successfuly,
448 * other a negative value at error.
450 int snd_dma_alloc_pages_fallback(const struct snd_dma_device *dev, size_t size,
451 struct snd_dma_buffer *dmab)
453 snd_assert(dev != NULL, return -ENXIO);
454 snd_assert(size > 0, return -ENXIO);
455 snd_assert(dmab != NULL, return -ENXIO);
459 case SNDRV_DMA_TYPE_CONTINUOUS:
460 dmab->area = snd_malloc_pages_fallback(size, (unsigned long)dev->dev, &dmab->bytes);
464 case SNDRV_DMA_TYPE_SBUS:
465 dmab->area = snd_malloc_sbus_pages_fallback(dev->dev, size, &dmab->addr, &dmab->bytes);
468 case SNDRV_DMA_TYPE_DEV:
469 dmab->area = snd_malloc_dev_pages_fallback(dev->dev, size, &dmab->addr, &dmab->bytes);
471 case SNDRV_DMA_TYPE_DEV_SG:
472 snd_malloc_sgbuf_pages(dev, size, dmab, &dmab->bytes);
475 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
487 * snd_dma_free_pages - release the allocated buffer
488 * @dev: the buffer device info
489 * @dmbab: the buffer allocation record to release
491 * Releases the allocated buffer via snd_dma_alloc_pages().
493 void snd_dma_free_pages(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
496 case SNDRV_DMA_TYPE_CONTINUOUS:
497 snd_free_pages(dmab->area, dmab->bytes);
500 case SNDRV_DMA_TYPE_SBUS:
501 snd_free_sbus_pages(dev->dev, dmab->bytes, dmab->area, dmab->addr);
504 case SNDRV_DMA_TYPE_DEV:
505 snd_free_dev_pages(dev->dev, dmab->bytes, dmab->area, dmab->addr);
507 case SNDRV_DMA_TYPE_DEV_SG:
508 snd_free_sgbuf_pages(dmab);
511 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
517 * search for the device
519 static struct snd_mem_list *mem_list_find(const struct snd_dma_device *dev, int search_empty)
522 struct snd_mem_list *mem;
524 list_for_each(p, &mem_list_head) {
525 mem = list_entry(p, struct snd_mem_list, list);
526 if (mem->used && search_empty)
528 if (compare_device(&mem->dev, dev, search_empty))
535 * snd_dma_get_reserved - get the reserved buffer for the given device
536 * @dev: the buffer device info
537 * @dmab: the buffer allocation record to store
539 * Looks for the reserved-buffer list and re-uses if the same buffer
540 * is found in the list. When the buffer is found, it's marked as used.
541 * For unmarking the buffer, call snd_dma_free_reserved().
543 * Returns the size of buffer if the buffer is found, or zero if not found.
545 size_t snd_dma_get_reserved(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
547 struct snd_mem_list *mem;
549 snd_assert(dev && dmab, return 0);
552 mem = mem_list_find(dev, 1);
565 * snd_dma_free_reserved - unmark the reserved buffer
566 * @dev: the buffer device info
568 * Looks for the matching reserved buffer and erases the mark on it
573 int snd_dma_free_reserved(const struct snd_dma_device *dev)
575 struct snd_mem_list *mem;
577 snd_assert(dev, return -EINVAL);
579 mem = mem_list_find(dev, 0);
587 * snd_dma_set_reserved - reserve the buffer
588 * @dev: the buffer device info
589 * @dmab: the buffer to reserve
591 * Reserves the given buffer as a reserved buffer.
592 * When an old reserved buffer already exists, the old one is released
593 * and replaced with the new one.
595 * When NULL buffer pointer or zero buffer size is given, the existing
596 * buffer is released and the entry is removed.
598 * Returns zero if successful, or a negative code at error.
600 int snd_dma_set_reserved(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
602 struct snd_mem_list *mem;
604 snd_assert(dev, return -EINVAL);
606 mem = mem_list_find(dev, 0);
609 printk(KERN_WARNING "snd-page-alloc: releasing the used block (type=%d, id=0x%x\n", mem->dev.type, mem->dev.id);
610 snd_dma_free_pages(dev, &mem->buffer);
611 if (! dmab || ! dmab->bytes) {
612 /* remove the entry */
613 list_del(&mem->list);
619 if (! dmab || ! dmab->bytes) {
623 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
629 list_add_tail(&mem->list, &mem_list_head);
631 /* store the entry */
639 * purge all reserved buffers
641 static void free_all_reserved_pages(void)
644 struct snd_mem_list *mem;
647 while (! list_empty(&mem_list_head)) {
648 p = mem_list_head.next;
649 mem = list_entry(p, struct snd_mem_list, list);
651 snd_dma_free_pages(&mem->dev, &mem->buffer);
660 * allocation of buffers for pre-defined devices
664 /* FIXME: for pci only - other bus? */
665 struct prealloc_dev {
666 unsigned short vendor;
667 unsigned short device;
668 unsigned long dma_mask;
670 unsigned int buffers;
673 #define HAMMERFALL_BUFFER_SIZE (16*1024*4*(26+1))
675 static struct prealloc_dev prealloc_devices[] __initdata = {
680 .dma_mask = 0xffffffff,
681 .size = HAMMERFALL_BUFFER_SIZE,
688 .dma_mask = 0xffffffff,
689 .size = HAMMERFALL_BUFFER_SIZE,
692 { }, /* terminator */
696 * compose a snd_dma_device struct for the PCI device
698 static inline void snd_dma_device_pci(struct snd_dma_device *dev, struct pci_dev *pci, unsigned int id)
700 memset(dev, 0, sizeof(*dev));
701 dev->type = SNDRV_DMA_TYPE_DEV;
702 dev->dev = snd_dma_pci_data(pci);
706 static void __init preallocate_cards(void)
708 struct pci_dev *pci = NULL;
713 while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
714 struct prealloc_dev *dev;
716 if (card >= SNDRV_CARDS)
718 for (dev = prealloc_devices; dev->vendor; dev++) {
719 if (dev->vendor == pci->vendor && dev->device == pci->device)
724 if (! enable[card++]) {
725 printk(KERN_DEBUG "snd-page-alloc: skipping card %d, device %04x:%04x\n", card, pci->vendor, pci->device);
729 if (pci_set_dma_mask(pci, dev->dma_mask) < 0 ||
730 pci_set_consistent_dma_mask(pci, dev->dma_mask) < 0) {
731 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", dev->dma_mask, dev->vendor, dev->device);
734 for (i = 0; i < dev->buffers; i++) {
735 struct snd_mem_list *mem;
736 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
738 printk(KERN_WARNING "snd-page-alloc: can't malloc memlist\n");
741 memset(mem, 0, sizeof(*mem));
742 snd_dma_device_pci(&mem->dev, pci, SNDRV_DMA_DEVICE_UNUSED);
743 if (snd_dma_alloc_pages(&mem->dev, dev->size, &mem->buffer) < 0) {
744 printk(KERN_WARNING "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", dev->size);
748 list_add_tail(&mem->list, &mem_list_head);
755 #define preallocate_cards() /* NOP */
759 #ifdef CONFIG_PROC_FS
761 * proc file interface
763 static int snd_mem_proc_read(char *page, char **start, off_t off,
764 int count, int *eof, void *data)
767 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
769 struct snd_mem_list *mem;
773 len += sprintf(page + len, "pages : %li bytes (%li pages per %likB)\n",
774 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
776 list_for_each(p, &mem_list_head) {
777 mem = list_entry(p, struct snd_mem_list, list);
779 len += sprintf(page + len, "buffer %d : ", devno);
780 if (mem->dev.id == SNDRV_DMA_DEVICE_UNUSED)
781 len += sprintf(page + len, "UNUSED");
783 len += sprintf(page + len, "ID %08x", mem->dev.id);
784 len += sprintf(page + len, " : type ");
785 switch (mem->dev.type) {
786 case SNDRV_DMA_TYPE_CONTINUOUS:
787 len += sprintf(page + len, "CONT [%p]", mem->dev.dev);
790 case SNDRV_DMA_TYPE_SBUS:
792 struct sbus_dev *sdev = (struct sbus_dev *)(mem->dev.dev);
793 len += sprintf(page + len, "SBUS [%x]", sdev->slot);
797 case SNDRV_DMA_TYPE_DEV:
798 case SNDRV_DMA_TYPE_DEV_SG:
800 len += sprintf(page + len, "%s [%s]",
801 mem->dev.type == SNDRV_DMA_TYPE_DEV_SG ? "DEV-SG" : "DEV",
802 mem->dev.dev->bus_id);
804 len += sprintf(page + len, "ISA");
807 len += sprintf(page + len, "UNKNOWN");
810 len += sprintf(page + len, "\n addr = 0x%lx, size = %d bytes, used = %s\n",
811 (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes,
812 mem->used ? "yes" : "no");
817 #endif /* CONFIG_PROC_FS */
823 static int __init snd_mem_init(void)
825 #ifdef CONFIG_PROC_FS
826 create_proc_read_entry("driver/snd-page-alloc", 0, 0, snd_mem_proc_read, NULL);
832 static void __exit snd_mem_exit(void)
834 remove_proc_entry("driver/snd-page-alloc", NULL);
835 free_all_reserved_pages();
836 if (snd_allocated_pages > 0)
837 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
841 module_init(snd_mem_init)
842 module_exit(snd_mem_exit)
847 /* format is: snd-page-alloc=enable */
849 static int __init snd_mem_setup(char *str)
851 static unsigned __initdata nr_dev = 0;
853 if (nr_dev >= SNDRV_CARDS)
855 (void)(get_option(&str,&enable[nr_dev]) == 2);
860 __setup("snd-page-alloc=", snd_mem_setup);
867 EXPORT_SYMBOL(snd_dma_alloc_pages);
868 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
869 EXPORT_SYMBOL(snd_dma_free_pages);
871 EXPORT_SYMBOL(snd_dma_get_reserved);
872 EXPORT_SYMBOL(snd_dma_free_reserved);
873 EXPORT_SYMBOL(snd_dma_set_reserved);
875 EXPORT_SYMBOL(snd_malloc_pages);
876 EXPORT_SYMBOL(snd_malloc_pages_fallback);
877 EXPORT_SYMBOL(snd_free_pages);