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};
52 module_param_array(enable
, bool, NULL
, 0444);
53 MODULE_PARM_DESC(enable
, "Enable cards to allocate buffers.");
59 void *snd_malloc_sgbuf_pages(struct device
*device
,
60 size_t size
, struct snd_dma_buffer
*dmab
,
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 */
72 struct snd_dma_buffer buffer
;
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 {\
84 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
89 #define snd_assert(expr, args...) /**/
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
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
)
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 */
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
);
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
;
141 ret
= dma_alloc_coherent(dev
, size
, dma_handle
, flags
);
147 /* redefine dma_alloc_coherent for some architectures */
148 #undef dma_alloc_coherent
149 #define dma_alloc_coherent snd_dma_hack_alloc_coherent
153 #if ! defined(__arm__)
154 #define NEED_RESERVE_PAGES
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
)
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
);
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
)
226 pg
= get_order(size
);
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
)
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
);
253 #ifdef NEED_RESERVE_PAGES
254 mark_pages(virt_to_page(res
), pg
); /* should be dma_to_page() */
262 /* free the coherent DMA pages */
263 static void snd_free_dev_pages(struct device
*dev
, size_t size
, void *ptr
,
270 pg
= get_order(size
);
272 #ifdef NEED_RESERVE_PAGES
273 unmark_pages(virt_to_page(ptr
), pg
); /* should be dma_to_page() */
275 dma_free_coherent(dev
, PAGE_SIZE
<< pg
, ptr
, dma
);
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
;
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
);
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
;
304 pg
= get_order(size
);
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
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
;
341 case SNDRV_DMA_TYPE_CONTINUOUS
:
342 dmab
->area
= snd_malloc_pages(size
, (unsigned long)device
);
346 case SNDRV_DMA_TYPE_SBUS
:
347 dmab
->area
= snd_malloc_sbus_pages(device
, size
, &dmab
->addr
);
350 case SNDRV_DMA_TYPE_DEV
:
351 dmab
->area
= snd_malloc_dev_pages(device
, size
, &dmab
->addr
);
353 case SNDRV_DMA_TYPE_DEV_SG
:
354 snd_malloc_sgbuf_pages(device
, size
, dmab
, NULL
);
357 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", type
);
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
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
)
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) {
395 if (size
<= PAGE_SIZE
)
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
);
417 case SNDRV_DMA_TYPE_SBUS
:
418 snd_free_sbus_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
421 case SNDRV_DMA_TYPE_DEV
:
422 snd_free_dev_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
424 case SNDRV_DMA_TYPE_DEV_SG
:
425 snd_free_sgbuf_pages(dmab
);
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
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
)
446 struct snd_mem_list
*mem
;
448 snd_assert(dmab
, return 0);
451 list_for_each(p
, &mem_list_head
) {
452 mem
= list_entry(p
, struct snd_mem_list
, list
);
454 ! memcmp(&mem
->buffer
.dev
, &dmab
->dev
, sizeof(dmab
->dev
))) {
467 * snd_dma_reserve_buf - reserve the buffer
468 * @dmab: the buffer to reserve
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
);
486 list_add_tail(&mem
->list
, &mem_list_head
);
492 * purge all reserved buffers
494 static void free_all_reserved_pages(void)
497 struct snd_mem_list
*mem
;
500 while (! list_empty(&mem_list_head
)) {
501 p
= mem_list_head
.next
;
502 mem
= list_entry(p
, struct snd_mem_list
, list
);
504 snd_dma_free_pages(&mem
->buffer
);
513 * allocation of buffers for pre-defined devices
517 /* FIXME: for pci only - other bus? */
518 struct prealloc_dev
{
519 unsigned short vendor
;
520 unsigned short device
;
521 unsigned long dma_mask
;
523 unsigned int buffers
;
526 #define HAMMERFALL_BUFFER_SIZE (16*1024*4*(26+1)+0x10000)
528 static struct prealloc_dev prealloc_devices
[] __initdata
= {
533 .dma_mask
= 0xffffffff,
534 .size
= HAMMERFALL_BUFFER_SIZE
,
541 .dma_mask
= 0xffffffff,
542 .size
= HAMMERFALL_BUFFER_SIZE
,
545 { }, /* terminator */
548 static void __init
preallocate_cards(void)
550 struct pci_dev
*pci
= NULL
;
555 while ((pci
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pci
)) != NULL
) {
556 struct prealloc_dev
*dev
;
558 if (card
>= SNDRV_CARDS
)
560 for (dev
= prealloc_devices
; dev
->vendor
; dev
++) {
561 if (dev
->vendor
== pci
->vendor
&& dev
->device
== pci
->device
)
566 if (! enable
[card
++]) {
567 printk(KERN_DEBUG
"snd-page-alloc: skipping card %d, device %04x:%04x\n", card
, pci
->vendor
, pci
->device
);
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
);
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
);
583 snd_dma_reserve_buf(&dmab
, snd_dma_pci_buf_id(pci
));
588 #define preallocate_cards() /* NOP */
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
)
600 long pages
= snd_allocated_pages
>> (PAGE_SHIFT
-12);
602 struct snd_mem_list
*mem
;
604 static char *types
[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
607 len
+= snprintf(page
+ len
, count
- len
,
608 "pages : %li bytes (%li pages per %likB)\n",
609 pages
* PAGE_SIZE
, pages
, PAGE_SIZE
/ 1024);
611 list_for_each(p
, &mem_list_head
) {
612 mem
= list_entry(p
, struct snd_mem_list
, list
);
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
);
624 #endif /* CONFIG_PROC_FS */
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
);
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
)
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
);