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/genalloc.h>
34 #include <linux/moduleparam.h>
35 #include <linux/mutex.h>
36 #include <sound/memalloc.h>
39 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
41 MODULE_LICENSE("GPL");
47 static DEFINE_MUTEX(list_mutex
);
48 static LIST_HEAD(mem_list_head
);
50 /* buffer preservation list */
52 struct snd_dma_buffer buffer
;
54 struct list_head list
;
57 /* id for pre-allocated buffers */
58 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
62 * Generic memory allocators
66 static long snd_allocated_pages
; /* holding the number of allocated pages */
68 static inline void inc_snd_pages(int order
)
70 snd_allocated_pages
+= 1 << order
;
73 static inline void dec_snd_pages(int order
)
75 snd_allocated_pages
-= 1 << order
;
79 * snd_malloc_pages - allocate pages with the given size
80 * @size: the size to allocate in bytes
81 * @gfp_flags: the allocation conditions, GFP_XXX
83 * Allocates the physically contiguous pages with the given size.
85 * Return: The pointer of the buffer, or %NULL if no enough memory.
87 void *snd_malloc_pages(size_t size
, gfp_t gfp_flags
)
94 if (WARN_ON(!gfp_flags
))
96 gfp_flags
|= __GFP_COMP
; /* compound page lets parts be mapped */
98 if ((res
= (void *) __get_free_pages(gfp_flags
, pg
)) != NULL
)
104 * snd_free_pages - release the pages
105 * @ptr: the buffer pointer to release
106 * @size: the allocated buffer size
108 * Releases the buffer allocated via snd_malloc_pages().
110 void snd_free_pages(void *ptr
, size_t size
)
116 pg
= get_order(size
);
118 free_pages((unsigned long) ptr
, pg
);
123 * Bus-specific memory allocators
127 #ifdef CONFIG_HAS_DMA
128 /* allocate the coherent DMA pages */
129 static void *snd_malloc_dev_pages(struct device
*dev
, size_t size
, dma_addr_t
*dma
)
137 pg
= get_order(size
);
138 gfp_flags
= GFP_KERNEL
139 | __GFP_COMP
/* compound page lets parts be mapped */
140 | __GFP_NORETRY
/* don't trigger OOM-killer */
141 | __GFP_NOWARN
; /* no stack trace print - this call is non-critical */
142 res
= dma_alloc_coherent(dev
, PAGE_SIZE
<< pg
, dma
, gfp_flags
);
149 /* free the coherent DMA pages */
150 static void snd_free_dev_pages(struct device
*dev
, size_t size
, void *ptr
,
157 pg
= get_order(size
);
159 dma_free_coherent(dev
, PAGE_SIZE
<< pg
, ptr
, dma
);
162 #ifdef CONFIG_GENERIC_ALLOCATOR
164 * snd_malloc_dev_iram - allocate memory from on-chip internal ram
165 * @dmab: buffer allocation record to store the allocated data
166 * @size: number of bytes to allocate from the iram
168 * This function requires iram phandle provided via of_node
170 static void snd_malloc_dev_iram(struct snd_dma_buffer
*dmab
, size_t size
)
172 struct device
*dev
= dmab
->dev
.dev
;
173 struct gen_pool
*pool
= NULL
;
179 pool
= of_get_named_gen_pool(dev
->of_node
, "iram", 0);
184 /* Assign the pool into private_data field */
185 dmab
->private_data
= pool
;
187 dmab
->area
= gen_pool_dma_alloc(pool
, size
, &dmab
->addr
);
191 * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
192 * @dmab: buffer allocation record to store the allocated data
194 static void snd_free_dev_iram(struct snd_dma_buffer
*dmab
)
196 struct gen_pool
*pool
= dmab
->private_data
;
198 if (pool
&& dmab
->area
)
199 gen_pool_free(pool
, (unsigned long)dmab
->area
, dmab
->bytes
);
201 #endif /* CONFIG_GENERIC_ALLOCATOR */
202 #endif /* CONFIG_HAS_DMA */
206 * ALSA generic memory management
212 * snd_dma_alloc_pages - allocate the buffer area according to the given type
213 * @type: the DMA buffer type
214 * @device: the device pointer
215 * @size: the buffer size to allocate
216 * @dmab: buffer allocation record to store the allocated data
218 * Calls the memory-allocator function for the corresponding
221 * Return: Zero if the buffer with the given size is allocated successfully,
222 * otherwise a negative value on error.
224 int snd_dma_alloc_pages(int type
, struct device
*device
, size_t size
,
225 struct snd_dma_buffer
*dmab
)
232 dmab
->dev
.type
= type
;
233 dmab
->dev
.dev
= device
;
236 case SNDRV_DMA_TYPE_CONTINUOUS
:
237 dmab
->area
= snd_malloc_pages(size
,
238 (__force gfp_t
)(unsigned long)device
);
241 #ifdef CONFIG_HAS_DMA
242 #ifdef CONFIG_GENERIC_ALLOCATOR
243 case SNDRV_DMA_TYPE_DEV_IRAM
:
244 snd_malloc_dev_iram(dmab
, size
);
247 /* Internal memory might have limited size and no enough space,
248 * so if we fail to malloc, try to fetch memory traditionally.
250 dmab
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
251 #endif /* CONFIG_GENERIC_ALLOCATOR */
252 case SNDRV_DMA_TYPE_DEV
:
253 dmab
->area
= snd_malloc_dev_pages(device
, size
, &dmab
->addr
);
256 #ifdef CONFIG_SND_DMA_SGBUF
257 case SNDRV_DMA_TYPE_DEV_SG
:
258 snd_malloc_sgbuf_pages(device
, size
, dmab
, NULL
);
262 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", type
);
274 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
275 * @type: the DMA buffer type
276 * @device: the device pointer
277 * @size: the buffer size to allocate
278 * @dmab: buffer allocation record to store the allocated data
280 * Calls the memory-allocator function for the corresponding
281 * buffer type. When no space is left, this function reduces the size and
282 * tries to allocate again. The size actually allocated is stored in
285 * Return: Zero if the buffer with the given size is allocated successfully,
286 * otherwise a negative value on error.
288 int snd_dma_alloc_pages_fallback(int type
, struct device
*device
, size_t size
,
289 struct snd_dma_buffer
*dmab
)
293 while ((err
= snd_dma_alloc_pages(type
, device
, size
, dmab
)) < 0) {
297 if (size
<= PAGE_SIZE
)
299 aligned_size
= PAGE_SIZE
<< get_order(size
);
300 if (size
!= aligned_size
)
312 * snd_dma_free_pages - release the allocated buffer
313 * @dmab: the buffer allocation record to release
315 * Releases the allocated buffer via snd_dma_alloc_pages().
317 void snd_dma_free_pages(struct snd_dma_buffer
*dmab
)
319 switch (dmab
->dev
.type
) {
320 case SNDRV_DMA_TYPE_CONTINUOUS
:
321 snd_free_pages(dmab
->area
, dmab
->bytes
);
323 #ifdef CONFIG_HAS_DMA
324 #ifdef CONFIG_GENERIC_ALLOCATOR
325 case SNDRV_DMA_TYPE_DEV_IRAM
:
326 snd_free_dev_iram(dmab
);
328 #endif /* CONFIG_GENERIC_ALLOCATOR */
329 case SNDRV_DMA_TYPE_DEV
:
330 snd_free_dev_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
333 #ifdef CONFIG_SND_DMA_SGBUF
334 case SNDRV_DMA_TYPE_DEV_SG
:
335 snd_free_sgbuf_pages(dmab
);
339 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", dmab
->dev
.type
);
345 * snd_dma_get_reserved - get the reserved buffer for the given device
346 * @dmab: the buffer allocation record to store
349 * Looks for the reserved-buffer list and re-uses if the same buffer
350 * is found in the list. When the buffer is found, it's removed from the free list.
352 * Return: The size of buffer if the buffer is found, or zero if not found.
354 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
356 struct snd_mem_list
*mem
;
361 mutex_lock(&list_mutex
);
362 list_for_each_entry(mem
, &mem_list_head
, list
) {
364 (mem
->buffer
.dev
.dev
== NULL
|| dmab
->dev
.dev
== NULL
||
365 ! memcmp(&mem
->buffer
.dev
, &dmab
->dev
, sizeof(dmab
->dev
)))) {
366 struct device
*dev
= dmab
->dev
.dev
;
367 list_del(&mem
->list
);
369 if (dmab
->dev
.dev
== NULL
)
372 mutex_unlock(&list_mutex
);
376 mutex_unlock(&list_mutex
);
381 * snd_dma_reserve_buf - reserve the buffer
382 * @dmab: the buffer to reserve
385 * Reserves the given buffer as a reserved buffer.
387 * Return: Zero if successful, or a negative code on error.
389 int snd_dma_reserve_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
391 struct snd_mem_list
*mem
;
395 mem
= kmalloc(sizeof(*mem
), GFP_KERNEL
);
398 mutex_lock(&list_mutex
);
401 list_add_tail(&mem
->list
, &mem_list_head
);
402 mutex_unlock(&list_mutex
);
407 * purge all reserved buffers
409 static void free_all_reserved_pages(void)
412 struct snd_mem_list
*mem
;
414 mutex_lock(&list_mutex
);
415 while (! list_empty(&mem_list_head
)) {
416 p
= mem_list_head
.next
;
417 mem
= list_entry(p
, struct snd_mem_list
, list
);
419 snd_dma_free_pages(&mem
->buffer
);
422 mutex_unlock(&list_mutex
);
426 #ifdef CONFIG_PROC_FS
428 * proc file interface
430 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
431 static struct proc_dir_entry
*snd_mem_proc
;
433 static int snd_mem_proc_read(struct seq_file
*seq
, void *offset
)
435 long pages
= snd_allocated_pages
>> (PAGE_SHIFT
-12);
436 struct snd_mem_list
*mem
;
438 static char *types
[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
440 mutex_lock(&list_mutex
);
441 seq_printf(seq
, "pages : %li bytes (%li pages per %likB)\n",
442 pages
* PAGE_SIZE
, pages
, PAGE_SIZE
/ 1024);
444 list_for_each_entry(mem
, &mem_list_head
, list
) {
446 seq_printf(seq
, "buffer %d : ID %08x : type %s\n",
447 devno
, mem
->id
, types
[mem
->buffer
.dev
.type
]);
448 seq_printf(seq
, " addr = 0x%lx, size = %d bytes\n",
449 (unsigned long)mem
->buffer
.addr
,
450 (int)mem
->buffer
.bytes
);
452 mutex_unlock(&list_mutex
);
456 static int snd_mem_proc_open(struct inode
*inode
, struct file
*file
)
458 return single_open(file
, snd_mem_proc_read
, NULL
);
461 /* FIXME: for pci only - other bus? */
463 #define gettoken(bufp) strsep(bufp, " \t\n")
465 static ssize_t
snd_mem_proc_write(struct file
*file
, const char __user
* buffer
,
466 size_t count
, loff_t
* ppos
)
471 if (count
> sizeof(buf
) - 1)
473 if (copy_from_user(buf
, buffer
, count
))
478 token
= gettoken(&p
);
479 if (! token
|| *token
== '#')
481 if (strcmp(token
, "add") == 0) {
483 int vendor
, device
, size
, buffers
;
488 if ((token
= gettoken(&p
)) == NULL
||
489 (vendor
= simple_strtol(token
, NULL
, 0)) <= 0 ||
490 (token
= gettoken(&p
)) == NULL
||
491 (device
= simple_strtol(token
, NULL
, 0)) <= 0 ||
492 (token
= gettoken(&p
)) == NULL
||
493 (mask
= simple_strtol(token
, NULL
, 0)) < 0 ||
494 (token
= gettoken(&p
)) == NULL
||
495 (size
= memparse(token
, &endp
)) < 64*1024 ||
496 size
> 16*1024*1024 /* too big */ ||
497 (token
= gettoken(&p
)) == NULL
||
498 (buffers
= simple_strtol(token
, NULL
, 0)) <= 0 ||
500 printk(KERN_ERR
"snd-page-alloc: invalid proc write format\n");
508 while ((pci
= pci_get_device(vendor
, device
, pci
)) != NULL
) {
509 if (mask
> 0 && mask
< 0xffffffff) {
510 if (pci_set_dma_mask(pci
, mask
) < 0 ||
511 pci_set_consistent_dma_mask(pci
, mask
) < 0) {
512 printk(KERN_ERR
"snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask
, vendor
, device
);
517 for (i
= 0; i
< buffers
; i
++) {
518 struct snd_dma_buffer dmab
;
519 memset(&dmab
, 0, sizeof(dmab
));
520 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(pci
),
522 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
526 snd_dma_reserve_buf(&dmab
, snd_dma_pci_buf_id(pci
));
531 for (i
= 0; i
< buffers
; i
++) {
532 struct snd_dma_buffer dmab
;
533 memset(&dmab
, 0, sizeof(dmab
));
534 /* FIXME: We can allocate only in ZONE_DMA
535 * without a device pointer!
537 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, NULL
,
539 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
542 snd_dma_reserve_buf(&dmab
, (unsigned int)((vendor
<< 16) | device
));
545 } else if (strcmp(token
, "erase") == 0)
546 /* FIXME: need for releasing each buffer chunk? */
547 free_all_reserved_pages();
549 printk(KERN_ERR
"snd-page-alloc: invalid proc cmd\n");
552 #endif /* CONFIG_PCI */
554 static const struct file_operations snd_mem_proc_fops
= {
555 .owner
= THIS_MODULE
,
556 .open
= snd_mem_proc_open
,
559 .write
= snd_mem_proc_write
,
562 .release
= single_release
,
565 #endif /* CONFIG_PROC_FS */
571 static int __init
snd_mem_init(void)
573 #ifdef CONFIG_PROC_FS
574 snd_mem_proc
= proc_create(SND_MEM_PROC_FILE
, 0644, NULL
,
580 static void __exit
snd_mem_exit(void)
582 remove_proc_entry(SND_MEM_PROC_FILE
, NULL
);
583 free_all_reserved_pages();
584 if (snd_allocated_pages
> 0)
585 printk(KERN_ERR
"snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages
);
589 module_init(snd_mem_init
)
590 module_exit(snd_mem_exit
)
596 EXPORT_SYMBOL(snd_dma_alloc_pages
);
597 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback
);
598 EXPORT_SYMBOL(snd_dma_free_pages
);
600 EXPORT_SYMBOL(snd_dma_get_reserved_buf
);
601 EXPORT_SYMBOL(snd_dma_reserve_buf
);
603 EXPORT_SYMBOL(snd_malloc_pages
);
604 EXPORT_SYMBOL(snd_free_pages
);