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 <asm/uaccess.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/moduleparam.h>
34 #include <asm/semaphore.h>
35 #include <sound/memalloc.h>
41 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
42 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
43 MODULE_LICENSE("GPL");
53 void *snd_malloc_sgbuf_pages(struct device
*device
,
54 size_t size
, struct snd_dma_buffer
*dmab
,
56 int snd_free_sgbuf_pages(struct snd_dma_buffer
*dmab
);
61 static DECLARE_MUTEX(list_mutex
);
62 static LIST_HEAD(mem_list_head
);
64 /* buffer preservation list */
66 struct snd_dma_buffer buffer
;
68 struct list_head list
;
71 /* id for pre-allocated buffers */
72 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
74 #ifdef CONFIG_SND_DEBUG
75 #define __ASTRING__(x) #x
76 #define snd_assert(expr, args...) do {\
78 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
83 #define snd_assert(expr, args...) /**/
90 #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
92 * A hack to allocate large buffers via dma_alloc_coherent()
94 * since dma_alloc_coherent always tries GFP_DMA when the requested
95 * pci memory region is below 32bit, it happens quite often that even
96 * 2 order of pages cannot be allocated.
98 * so in the following, we allocate at first without dma_mask, so that
99 * allocation will be done without GFP_DMA. if the area doesn't match
100 * with the requested region, then realloate with the original dma_mask
103 * Really, we want to move this type of thing into dma_alloc_coherent()
104 * so dma_mask doesn't have to be messed with.
107 static void *snd_dma_hack_alloc_coherent(struct device
*dev
, size_t size
,
108 dma_addr_t
*dma_handle
, int flags
)
111 u64 dma_mask
, coherent_dma_mask
;
113 if (dev
== NULL
|| !dev
->dma_mask
)
114 return dma_alloc_coherent(dev
, size
, dma_handle
, flags
);
115 dma_mask
= *dev
->dma_mask
;
116 coherent_dma_mask
= dev
->coherent_dma_mask
;
117 *dev
->dma_mask
= 0xffffffff; /* do without masking */
118 dev
->coherent_dma_mask
= 0xffffffff; /* do without masking */
119 ret
= dma_alloc_coherent(dev
, size
, dma_handle
, flags
);
120 *dev
->dma_mask
= dma_mask
; /* restore */
121 dev
->coherent_dma_mask
= coherent_dma_mask
; /* restore */
123 /* obtained address is out of range? */
124 if (((unsigned long)*dma_handle
+ size
- 1) & ~dma_mask
) {
125 /* reallocate with the proper mask */
126 dma_free_coherent(dev
, size
, ret
, *dma_handle
);
127 ret
= dma_alloc_coherent(dev
, size
, dma_handle
, flags
);
130 /* wish to success now with the proper mask... */
131 if (dma_mask
!= 0xffffffffUL
) {
132 /* allocation with GFP_ATOMIC to avoid the long stall */
133 flags
&= ~GFP_KERNEL
;
135 ret
= dma_alloc_coherent(dev
, size
, dma_handle
, flags
);
141 /* redefine dma_alloc_coherent for some architectures */
142 #undef dma_alloc_coherent
143 #define dma_alloc_coherent snd_dma_hack_alloc_coherent
147 #if ! defined(__arm__)
148 #define NEED_RESERVE_PAGES
153 * Generic memory allocators
157 static long snd_allocated_pages
; /* holding the number of allocated pages */
159 static inline void inc_snd_pages(int order
)
161 snd_allocated_pages
+= 1 << order
;
164 static inline void dec_snd_pages(int order
)
166 snd_allocated_pages
-= 1 << order
;
169 static void mark_pages(struct page
*page
, int order
)
171 struct page
*last_page
= page
+ (1 << order
);
172 while (page
< last_page
)
173 SetPageReserved(page
++);
176 static void unmark_pages(struct page
*page
, int order
)
178 struct page
*last_page
= page
+ (1 << order
);
179 while (page
< last_page
)
180 ClearPageReserved(page
++);
184 * snd_malloc_pages - allocate pages with the given size
185 * @size: the size to allocate in bytes
186 * @gfp_flags: the allocation conditions, GFP_XXX
188 * Allocates the physically contiguous pages with the given size.
190 * Returns the pointer of the buffer, or NULL if no enoguh memory.
192 void *snd_malloc_pages(size_t size
, unsigned int gfp_flags
)
197 snd_assert(size
> 0, return NULL
);
198 snd_assert(gfp_flags
!= 0, return NULL
);
199 pg
= get_order(size
);
200 if ((res
= (void *) __get_free_pages(gfp_flags
, pg
)) != NULL
) {
201 mark_pages(virt_to_page(res
), pg
);
208 * snd_free_pages - release the pages
209 * @ptr: the buffer pointer to release
210 * @size: the allocated buffer size
212 * Releases the buffer allocated via snd_malloc_pages().
214 void snd_free_pages(void *ptr
, size_t size
)
220 pg
= get_order(size
);
222 unmark_pages(virt_to_page(ptr
), pg
);
223 free_pages((unsigned long) ptr
, pg
);
228 * Bus-specific memory allocators
232 /* allocate the coherent DMA pages */
233 static void *snd_malloc_dev_pages(struct device
*dev
, size_t size
, dma_addr_t
*dma
)
237 unsigned int gfp_flags
;
239 snd_assert(size
> 0, return NULL
);
240 snd_assert(dma
!= NULL
, return NULL
);
241 pg
= get_order(size
);
242 gfp_flags
= GFP_KERNEL
243 | __GFP_NORETRY
/* don't trigger OOM-killer */
244 | __GFP_NOWARN
; /* no stack trace print - this call is non-critical */
245 res
= dma_alloc_coherent(dev
, PAGE_SIZE
<< pg
, dma
, gfp_flags
);
247 #ifdef NEED_RESERVE_PAGES
248 mark_pages(virt_to_page(res
), pg
); /* should be dma_to_page() */
256 /* free the coherent DMA pages */
257 static void snd_free_dev_pages(struct device
*dev
, size_t size
, void *ptr
,
264 pg
= get_order(size
);
266 #ifdef NEED_RESERVE_PAGES
267 unmark_pages(virt_to_page(ptr
), pg
); /* should be dma_to_page() */
269 dma_free_coherent(dev
, PAGE_SIZE
<< pg
, ptr
, dma
);
274 static void *snd_malloc_sbus_pages(struct device
*dev
, size_t size
,
275 dma_addr_t
*dma_addr
)
277 struct sbus_dev
*sdev
= (struct sbus_dev
*)dev
;
281 snd_assert(size
> 0, return NULL
);
282 snd_assert(dma_addr
!= NULL
, return NULL
);
283 pg
= get_order(size
);
284 res
= sbus_alloc_consistent(sdev
, PAGE_SIZE
* (1 << pg
), dma_addr
);
290 static void snd_free_sbus_pages(struct device
*dev
, size_t size
,
291 void *ptr
, dma_addr_t dma_addr
)
293 struct sbus_dev
*sdev
= (struct sbus_dev
*)dev
;
298 pg
= get_order(size
);
300 sbus_free_consistent(sdev
, PAGE_SIZE
* (1 << pg
), ptr
, dma_addr
);
303 #endif /* CONFIG_SBUS */
307 * ALSA generic memory management
313 * snd_dma_alloc_pages - allocate the buffer area according to the given type
314 * @type: the DMA buffer type
315 * @device: the device pointer
316 * @size: the buffer size to allocate
317 * @dmab: buffer allocation record to store the allocated data
319 * Calls the memory-allocator function for the corresponding
322 * Returns zero if the buffer with the given size is allocated successfuly,
323 * other a negative value at error.
325 int snd_dma_alloc_pages(int type
, struct device
*device
, size_t size
,
326 struct snd_dma_buffer
*dmab
)
328 snd_assert(size
> 0, return -ENXIO
);
329 snd_assert(dmab
!= NULL
, return -ENXIO
);
331 dmab
->dev
.type
= type
;
332 dmab
->dev
.dev
= device
;
335 case SNDRV_DMA_TYPE_CONTINUOUS
:
336 dmab
->area
= snd_malloc_pages(size
, (unsigned long)device
);
340 case SNDRV_DMA_TYPE_SBUS
:
341 dmab
->area
= snd_malloc_sbus_pages(device
, size
, &dmab
->addr
);
344 case SNDRV_DMA_TYPE_DEV
:
345 dmab
->area
= snd_malloc_dev_pages(device
, size
, &dmab
->addr
);
347 case SNDRV_DMA_TYPE_DEV_SG
:
348 snd_malloc_sgbuf_pages(device
, size
, dmab
, NULL
);
351 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", type
);
363 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
364 * @type: the DMA buffer type
365 * @device: the device pointer
366 * @size: the buffer size to allocate
367 * @dmab: buffer allocation record to store the allocated data
369 * Calls the memory-allocator function for the corresponding
370 * buffer type. When no space is left, this function reduces the size and
371 * tries to allocate again. The size actually allocated is stored in
374 * Returns zero if the buffer with the given size is allocated successfuly,
375 * other a negative value at error.
377 int snd_dma_alloc_pages_fallback(int type
, struct device
*device
, size_t size
,
378 struct snd_dma_buffer
*dmab
)
382 snd_assert(size
> 0, return -ENXIO
);
383 snd_assert(dmab
!= NULL
, return -ENXIO
);
385 while ((err
= snd_dma_alloc_pages(type
, device
, size
, dmab
)) < 0) {
389 if (size
<= PAGE_SIZE
)
399 * snd_dma_free_pages - release the allocated buffer
400 * @dmab: the buffer allocation record to release
402 * Releases the allocated buffer via snd_dma_alloc_pages().
404 void snd_dma_free_pages(struct snd_dma_buffer
*dmab
)
406 switch (dmab
->dev
.type
) {
407 case SNDRV_DMA_TYPE_CONTINUOUS
:
408 snd_free_pages(dmab
->area
, dmab
->bytes
);
411 case SNDRV_DMA_TYPE_SBUS
:
412 snd_free_sbus_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
415 case SNDRV_DMA_TYPE_DEV
:
416 snd_free_dev_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
418 case SNDRV_DMA_TYPE_DEV_SG
:
419 snd_free_sgbuf_pages(dmab
);
422 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", dmab
->dev
.type
);
428 * snd_dma_get_reserved - get the reserved buffer for the given device
429 * @dmab: the buffer allocation record to store
432 * Looks for the reserved-buffer list and re-uses if the same buffer
433 * is found in the list. When the buffer is found, it's removed from the free list.
435 * Returns the size of buffer if the buffer is found, or zero if not found.
437 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
440 struct snd_mem_list
*mem
;
442 snd_assert(dmab
, return 0);
445 list_for_each(p
, &mem_list_head
) {
446 mem
= list_entry(p
, struct snd_mem_list
, list
);
448 (mem
->buffer
.dev
.dev
== NULL
|| dmab
->dev
.dev
== NULL
||
449 ! memcmp(&mem
->buffer
.dev
, &dmab
->dev
, sizeof(dmab
->dev
)))) {
450 struct device
*dev
= dmab
->dev
.dev
;
453 if (dmab
->dev
.dev
== NULL
)
465 * snd_dma_reserve_buf - reserve the buffer
466 * @dmab: the buffer to reserve
469 * Reserves the given buffer as a reserved buffer.
471 * Returns zero if successful, or a negative code at error.
473 int snd_dma_reserve_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
475 struct snd_mem_list
*mem
;
477 snd_assert(dmab
, return -EINVAL
);
478 mem
= kmalloc(sizeof(*mem
), GFP_KERNEL
);
484 list_add_tail(&mem
->list
, &mem_list_head
);
490 * purge all reserved buffers
492 static void free_all_reserved_pages(void)
495 struct snd_mem_list
*mem
;
498 while (! list_empty(&mem_list_head
)) {
499 p
= mem_list_head
.next
;
500 mem
= list_entry(p
, struct snd_mem_list
, list
);
502 snd_dma_free_pages(&mem
->buffer
);
509 #ifdef CONFIG_PROC_FS
511 * proc file interface
513 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
514 struct proc_dir_entry
*snd_mem_proc
;
516 static int snd_mem_proc_read(char *page
, char **start
, off_t off
,
517 int count
, int *eof
, void *data
)
520 long pages
= snd_allocated_pages
>> (PAGE_SHIFT
-12);
522 struct snd_mem_list
*mem
;
524 static char *types
[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
527 len
+= snprintf(page
+ len
, count
- len
,
528 "pages : %li bytes (%li pages per %likB)\n",
529 pages
* PAGE_SIZE
, pages
, PAGE_SIZE
/ 1024);
531 list_for_each(p
, &mem_list_head
) {
532 mem
= list_entry(p
, struct snd_mem_list
, list
);
534 len
+= snprintf(page
+ len
, count
- len
,
535 "buffer %d : ID %08x : type %s\n",
536 devno
, mem
->id
, types
[mem
->buffer
.dev
.type
]);
537 len
+= snprintf(page
+ len
, count
- len
,
538 " addr = 0x%lx, size = %d bytes\n",
539 (unsigned long)mem
->buffer
.addr
, (int)mem
->buffer
.bytes
);
545 /* FIXME: for pci only - other bus? */
547 #define gettoken(bufp) strsep(bufp, " \t\n")
549 static int snd_mem_proc_write(struct file
*file
, const char __user
*buffer
,
550 unsigned long count
, void *data
)
555 if (count
> ARRAY_SIZE(buf
) - 1)
556 count
= ARRAY_SIZE(buf
) - 1;
557 if (copy_from_user(buf
, buffer
, count
))
559 buf
[ARRAY_SIZE(buf
) - 1] = '\0';
562 token
= gettoken(&p
);
563 if (! token
|| *token
== '#')
565 if (strcmp(token
, "add") == 0) {
567 int vendor
, device
, size
, buffers
;
572 if ((token
= gettoken(&p
)) == NULL
||
573 (vendor
= simple_strtol(token
, NULL
, 0)) <= 0 ||
574 (token
= gettoken(&p
)) == NULL
||
575 (device
= simple_strtol(token
, NULL
, 0)) <= 0 ||
576 (token
= gettoken(&p
)) == NULL
||
577 (mask
= simple_strtol(token
, NULL
, 0)) < 0 ||
578 (token
= gettoken(&p
)) == NULL
||
579 (size
= memparse(token
, &endp
)) < 64*1024 ||
580 size
> 16*1024*1024 /* too big */ ||
581 (token
= gettoken(&p
)) == NULL
||
582 (buffers
= simple_strtol(token
, NULL
, 0)) <= 0 ||
584 printk(KERN_ERR
"snd-page-alloc: invalid proc write format\n");
592 while ((pci
= pci_find_device(vendor
, device
, pci
)) != NULL
) {
593 if (mask
> 0 && mask
< 0xffffffff) {
594 if (pci_set_dma_mask(pci
, mask
) < 0 ||
595 pci_set_consistent_dma_mask(pci
, mask
) < 0) {
596 printk(KERN_ERR
"snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask
, vendor
, device
);
600 for (i
= 0; i
< buffers
; i
++) {
601 struct snd_dma_buffer dmab
;
602 memset(&dmab
, 0, sizeof(dmab
));
603 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(pci
),
605 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
608 snd_dma_reserve_buf(&dmab
, snd_dma_pci_buf_id(pci
));
613 for (i
= 0; i
< buffers
; i
++) {
614 struct snd_dma_buffer dmab
;
615 memset(&dmab
, 0, sizeof(dmab
));
616 /* FIXME: We can allocate only in ZONE_DMA
617 * without a device pointer!
619 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, NULL
,
621 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
624 snd_dma_reserve_buf(&dmab
, (unsigned int)((vendor
<< 16) | device
));
627 } else if (strcmp(token
, "erase") == 0)
628 /* FIXME: need for releasing each buffer chunk? */
629 free_all_reserved_pages();
631 printk(KERN_ERR
"snd-page-alloc: invalid proc cmd\n");
634 #endif /* CONFIG_PCI */
635 #endif /* CONFIG_PROC_FS */
641 static int __init
snd_mem_init(void)
643 #ifdef CONFIG_PROC_FS
644 snd_mem_proc
= create_proc_entry(SND_MEM_PROC_FILE
, 0644, NULL
);
646 snd_mem_proc
->read_proc
= snd_mem_proc_read
;
648 snd_mem_proc
->write_proc
= snd_mem_proc_write
;
655 static void __exit
snd_mem_exit(void)
658 remove_proc_entry(SND_MEM_PROC_FILE
, NULL
);
659 free_all_reserved_pages();
660 if (snd_allocated_pages
> 0)
661 printk(KERN_ERR
"snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages
);
665 module_init(snd_mem_init
)
666 module_exit(snd_mem_exit
)
672 EXPORT_SYMBOL(snd_dma_alloc_pages
);
673 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback
);
674 EXPORT_SYMBOL(snd_dma_free_pages
);
676 EXPORT_SYMBOL(snd_dma_get_reserved_buf
);
677 EXPORT_SYMBOL(snd_dma_reserve_buf
);
679 EXPORT_SYMBOL(snd_malloc_pages
);
680 EXPORT_SYMBOL(snd_free_pages
);