2 * Copyright (c) by Jaroslav Kysela <perex@perex.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/module.h>
25 #include <linux/proc_fs.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
30 #include <linux/seq_file.h>
31 #include <asm/uaccess.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/moduleparam.h>
34 #include <linux/mutex.h>
35 #include <sound/memalloc.h>
41 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
42 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
43 MODULE_LICENSE("GPL");
49 static DEFINE_MUTEX(list_mutex
);
50 static LIST_HEAD(mem_list_head
);
52 /* buffer preservation list */
54 struct snd_dma_buffer buffer
;
56 struct list_head list
;
59 /* id for pre-allocated buffers */
60 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
64 * Generic memory allocators
68 static long snd_allocated_pages
; /* holding the number of allocated pages */
70 static inline void inc_snd_pages(int order
)
72 snd_allocated_pages
+= 1 << order
;
75 static inline void dec_snd_pages(int order
)
77 snd_allocated_pages
-= 1 << order
;
81 * snd_malloc_pages - allocate pages with the given size
82 * @size: the size to allocate in bytes
83 * @gfp_flags: the allocation conditions, GFP_XXX
85 * Allocates the physically contiguous pages with the given size.
87 * Returns the pointer of the buffer, or NULL if no enoguh memory.
89 void *snd_malloc_pages(size_t size
, gfp_t gfp_flags
)
96 if (WARN_ON(!gfp_flags
))
98 gfp_flags
|= __GFP_COMP
; /* compound page lets parts be mapped */
100 if ((res
= (void *) __get_free_pages(gfp_flags
, pg
)) != NULL
)
106 * snd_free_pages - release the pages
107 * @ptr: the buffer pointer to release
108 * @size: the allocated buffer size
110 * Releases the buffer allocated via snd_malloc_pages().
112 void snd_free_pages(void *ptr
, size_t size
)
118 pg
= get_order(size
);
120 free_pages((unsigned long) ptr
, pg
);
125 * Bus-specific memory allocators
129 #ifdef CONFIG_HAS_DMA
130 /* allocate the coherent DMA pages */
131 static void *snd_malloc_dev_pages(struct device
*dev
, size_t size
, dma_addr_t
*dma
)
139 pg
= get_order(size
);
140 gfp_flags
= GFP_KERNEL
141 | __GFP_COMP
/* compound page lets parts be mapped */
142 | __GFP_NORETRY
/* don't trigger OOM-killer */
143 | __GFP_NOWARN
; /* no stack trace print - this call is non-critical */
144 res
= dma_alloc_coherent(dev
, PAGE_SIZE
<< pg
, dma
, gfp_flags
);
151 /* free the coherent DMA pages */
152 static void snd_free_dev_pages(struct device
*dev
, size_t size
, void *ptr
,
159 pg
= get_order(size
);
161 dma_free_coherent(dev
, PAGE_SIZE
<< pg
, ptr
, dma
);
163 #endif /* CONFIG_HAS_DMA */
167 static void *snd_malloc_sbus_pages(struct device
*dev
, size_t size
,
168 dma_addr_t
*dma_addr
)
170 struct sbus_dev
*sdev
= (struct sbus_dev
*)dev
;
174 if (WARN_ON(!dma_addr
))
176 pg
= get_order(size
);
177 res
= sbus_alloc_consistent(sdev
, PAGE_SIZE
* (1 << pg
), dma_addr
);
183 static void snd_free_sbus_pages(struct device
*dev
, size_t size
,
184 void *ptr
, dma_addr_t dma_addr
)
186 struct sbus_dev
*sdev
= (struct sbus_dev
*)dev
;
191 pg
= get_order(size
);
193 sbus_free_consistent(sdev
, PAGE_SIZE
* (1 << pg
), ptr
, dma_addr
);
196 #endif /* CONFIG_SBUS */
200 * ALSA generic memory management
206 * snd_dma_alloc_pages - allocate the buffer area according to the given type
207 * @type: the DMA buffer type
208 * @device: the device pointer
209 * @size: the buffer size to allocate
210 * @dmab: buffer allocation record to store the allocated data
212 * Calls the memory-allocator function for the corresponding
215 * Returns zero if the buffer with the given size is allocated successfuly,
216 * other a negative value at error.
218 int snd_dma_alloc_pages(int type
, struct device
*device
, size_t size
,
219 struct snd_dma_buffer
*dmab
)
226 dmab
->dev
.type
= type
;
227 dmab
->dev
.dev
= device
;
230 case SNDRV_DMA_TYPE_CONTINUOUS
:
231 dmab
->area
= snd_malloc_pages(size
, (unsigned long)device
);
235 case SNDRV_DMA_TYPE_SBUS
:
236 dmab
->area
= snd_malloc_sbus_pages(device
, size
, &dmab
->addr
);
239 #ifdef CONFIG_HAS_DMA
240 case SNDRV_DMA_TYPE_DEV
:
241 dmab
->area
= snd_malloc_dev_pages(device
, size
, &dmab
->addr
);
243 case SNDRV_DMA_TYPE_DEV_SG
:
244 snd_malloc_sgbuf_pages(device
, size
, dmab
, NULL
);
248 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", type
);
260 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
261 * @type: the DMA buffer type
262 * @device: the device pointer
263 * @size: the buffer size to allocate
264 * @dmab: buffer allocation record to store the allocated data
266 * Calls the memory-allocator function for the corresponding
267 * buffer type. When no space is left, this function reduces the size and
268 * tries to allocate again. The size actually allocated is stored in
271 * Returns zero if the buffer with the given size is allocated successfuly,
272 * other a negative value at error.
274 int snd_dma_alloc_pages_fallback(int type
, struct device
*device
, size_t size
,
275 struct snd_dma_buffer
*dmab
)
279 while ((err
= snd_dma_alloc_pages(type
, device
, size
, dmab
)) < 0) {
283 if (size
<= PAGE_SIZE
)
285 aligned_size
= PAGE_SIZE
<< get_order(size
);
286 if (size
!= aligned_size
)
298 * snd_dma_free_pages - release the allocated buffer
299 * @dmab: the buffer allocation record to release
301 * Releases the allocated buffer via snd_dma_alloc_pages().
303 void snd_dma_free_pages(struct snd_dma_buffer
*dmab
)
305 switch (dmab
->dev
.type
) {
306 case SNDRV_DMA_TYPE_CONTINUOUS
:
307 snd_free_pages(dmab
->area
, dmab
->bytes
);
310 case SNDRV_DMA_TYPE_SBUS
:
311 snd_free_sbus_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
314 #ifdef CONFIG_HAS_DMA
315 case SNDRV_DMA_TYPE_DEV
:
316 snd_free_dev_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
318 case SNDRV_DMA_TYPE_DEV_SG
:
319 snd_free_sgbuf_pages(dmab
);
323 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", dmab
->dev
.type
);
329 * snd_dma_get_reserved - get the reserved buffer for the given device
330 * @dmab: the buffer allocation record to store
333 * Looks for the reserved-buffer list and re-uses if the same buffer
334 * is found in the list. When the buffer is found, it's removed from the free list.
336 * Returns the size of buffer if the buffer is found, or zero if not found.
338 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
340 struct snd_mem_list
*mem
;
345 mutex_lock(&list_mutex
);
346 list_for_each_entry(mem
, &mem_list_head
, list
) {
348 (mem
->buffer
.dev
.dev
== NULL
|| dmab
->dev
.dev
== NULL
||
349 ! memcmp(&mem
->buffer
.dev
, &dmab
->dev
, sizeof(dmab
->dev
)))) {
350 struct device
*dev
= dmab
->dev
.dev
;
351 list_del(&mem
->list
);
353 if (dmab
->dev
.dev
== NULL
)
356 mutex_unlock(&list_mutex
);
360 mutex_unlock(&list_mutex
);
365 * snd_dma_reserve_buf - reserve the buffer
366 * @dmab: the buffer to reserve
369 * Reserves the given buffer as a reserved buffer.
371 * Returns zero if successful, or a negative code at error.
373 int snd_dma_reserve_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
375 struct snd_mem_list
*mem
;
379 mem
= kmalloc(sizeof(*mem
), GFP_KERNEL
);
382 mutex_lock(&list_mutex
);
385 list_add_tail(&mem
->list
, &mem_list_head
);
386 mutex_unlock(&list_mutex
);
391 * purge all reserved buffers
393 static void free_all_reserved_pages(void)
396 struct snd_mem_list
*mem
;
398 mutex_lock(&list_mutex
);
399 while (! list_empty(&mem_list_head
)) {
400 p
= mem_list_head
.next
;
401 mem
= list_entry(p
, struct snd_mem_list
, list
);
403 snd_dma_free_pages(&mem
->buffer
);
406 mutex_unlock(&list_mutex
);
410 #ifdef CONFIG_PROC_FS
412 * proc file interface
414 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
415 static struct proc_dir_entry
*snd_mem_proc
;
417 static int snd_mem_proc_read(struct seq_file
*seq
, void *offset
)
419 long pages
= snd_allocated_pages
>> (PAGE_SHIFT
-12);
420 struct snd_mem_list
*mem
;
422 static char *types
[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
424 mutex_lock(&list_mutex
);
425 seq_printf(seq
, "pages : %li bytes (%li pages per %likB)\n",
426 pages
* PAGE_SIZE
, pages
, PAGE_SIZE
/ 1024);
428 list_for_each_entry(mem
, &mem_list_head
, list
) {
430 seq_printf(seq
, "buffer %d : ID %08x : type %s\n",
431 devno
, mem
->id
, types
[mem
->buffer
.dev
.type
]);
432 seq_printf(seq
, " addr = 0x%lx, size = %d bytes\n",
433 (unsigned long)mem
->buffer
.addr
,
434 (int)mem
->buffer
.bytes
);
436 mutex_unlock(&list_mutex
);
440 static int snd_mem_proc_open(struct inode
*inode
, struct file
*file
)
442 return single_open(file
, snd_mem_proc_read
, NULL
);
445 /* FIXME: for pci only - other bus? */
447 #define gettoken(bufp) strsep(bufp, " \t\n")
449 static ssize_t
snd_mem_proc_write(struct file
*file
, const char __user
* buffer
,
450 size_t count
, loff_t
* ppos
)
455 if (count
> sizeof(buf
) - 1)
457 if (copy_from_user(buf
, buffer
, count
))
462 token
= gettoken(&p
);
463 if (! token
|| *token
== '#')
465 if (strcmp(token
, "add") == 0) {
467 int vendor
, device
, size
, buffers
;
472 if ((token
= gettoken(&p
)) == NULL
||
473 (vendor
= simple_strtol(token
, NULL
, 0)) <= 0 ||
474 (token
= gettoken(&p
)) == NULL
||
475 (device
= simple_strtol(token
, NULL
, 0)) <= 0 ||
476 (token
= gettoken(&p
)) == NULL
||
477 (mask
= simple_strtol(token
, NULL
, 0)) < 0 ||
478 (token
= gettoken(&p
)) == NULL
||
479 (size
= memparse(token
, &endp
)) < 64*1024 ||
480 size
> 16*1024*1024 /* too big */ ||
481 (token
= gettoken(&p
)) == NULL
||
482 (buffers
= simple_strtol(token
, NULL
, 0)) <= 0 ||
484 printk(KERN_ERR
"snd-page-alloc: invalid proc write format\n");
492 while ((pci
= pci_get_device(vendor
, device
, pci
)) != NULL
) {
493 if (mask
> 0 && mask
< 0xffffffff) {
494 if (pci_set_dma_mask(pci
, mask
) < 0 ||
495 pci_set_consistent_dma_mask(pci
, mask
) < 0) {
496 printk(KERN_ERR
"snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask
, vendor
, device
);
501 for (i
= 0; i
< buffers
; i
++) {
502 struct snd_dma_buffer dmab
;
503 memset(&dmab
, 0, sizeof(dmab
));
504 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(pci
),
506 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
510 snd_dma_reserve_buf(&dmab
, snd_dma_pci_buf_id(pci
));
515 for (i
= 0; i
< buffers
; i
++) {
516 struct snd_dma_buffer dmab
;
517 memset(&dmab
, 0, sizeof(dmab
));
518 /* FIXME: We can allocate only in ZONE_DMA
519 * without a device pointer!
521 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, NULL
,
523 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
526 snd_dma_reserve_buf(&dmab
, (unsigned int)((vendor
<< 16) | device
));
529 } else if (strcmp(token
, "erase") == 0)
530 /* FIXME: need for releasing each buffer chunk? */
531 free_all_reserved_pages();
533 printk(KERN_ERR
"snd-page-alloc: invalid proc cmd\n");
536 #endif /* CONFIG_PCI */
538 static const struct file_operations snd_mem_proc_fops
= {
539 .owner
= THIS_MODULE
,
540 .open
= snd_mem_proc_open
,
543 .write
= snd_mem_proc_write
,
546 .release
= single_release
,
549 #endif /* CONFIG_PROC_FS */
555 static int __init
snd_mem_init(void)
557 #ifdef CONFIG_PROC_FS
558 snd_mem_proc
= proc_create(SND_MEM_PROC_FILE
, 0644, NULL
,
564 static void __exit
snd_mem_exit(void)
566 remove_proc_entry(SND_MEM_PROC_FILE
, NULL
);
567 free_all_reserved_pages();
568 if (snd_allocated_pages
> 0)
569 printk(KERN_ERR
"snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages
);
573 module_init(snd_mem_init
)
574 module_exit(snd_mem_exit
)
580 EXPORT_SYMBOL(snd_dma_alloc_pages
);
581 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback
);
582 EXPORT_SYMBOL(snd_dma_free_pages
);
584 EXPORT_SYMBOL(snd_dma_get_reserved_buf
);
585 EXPORT_SYMBOL(snd_dma_reserve_buf
);
587 EXPORT_SYMBOL(snd_malloc_pages
);
588 EXPORT_SYMBOL(snd_free_pages
);