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
);
163 * snd_malloc_dev_iram - allocate memory from on-chip internal ram
164 * @dmab: buffer allocation record to store the allocated data
165 * @size: number of bytes to allocate from the iram
167 * This function requires iram phandle provided via of_node
169 void snd_malloc_dev_iram(struct snd_dma_buffer
*dmab
, size_t size
)
171 struct device
*dev
= dmab
->dev
.dev
;
172 struct gen_pool
*pool
= NULL
;
175 pool
= of_get_named_gen_pool(dev
->of_node
, "iram", 0);
180 /* Assign the pool into private_data field */
181 dmab
->private_data
= pool
;
183 dmab
->area
= (void *)gen_pool_alloc(pool
, size
);
187 dmab
->addr
= gen_pool_virt_to_phys(pool
, (unsigned long)dmab
->area
);
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 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_HAS_DMA */
205 * ALSA generic memory management
211 * snd_dma_alloc_pages - allocate the buffer area according to the given type
212 * @type: the DMA buffer type
213 * @device: the device pointer
214 * @size: the buffer size to allocate
215 * @dmab: buffer allocation record to store the allocated data
217 * Calls the memory-allocator function for the corresponding
220 * Return: Zero if the buffer with the given size is allocated successfully,
221 * otherwise a negative value on error.
223 int snd_dma_alloc_pages(int type
, struct device
*device
, size_t size
,
224 struct snd_dma_buffer
*dmab
)
231 dmab
->dev
.type
= type
;
232 dmab
->dev
.dev
= device
;
235 case SNDRV_DMA_TYPE_CONTINUOUS
:
236 dmab
->area
= snd_malloc_pages(size
,
237 (__force gfp_t
)(unsigned long)device
);
240 #ifdef CONFIG_HAS_DMA
241 case SNDRV_DMA_TYPE_DEV_IRAM
:
242 snd_malloc_dev_iram(dmab
, size
);
245 /* Internal memory might have limited size and no enough space,
246 * so if we fail to malloc, try to fetch memory traditionally.
248 dmab
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
249 case SNDRV_DMA_TYPE_DEV
:
250 dmab
->area
= snd_malloc_dev_pages(device
, size
, &dmab
->addr
);
253 #ifdef CONFIG_SND_DMA_SGBUF
254 case SNDRV_DMA_TYPE_DEV_SG
:
255 snd_malloc_sgbuf_pages(device
, size
, dmab
, NULL
);
259 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", type
);
271 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
272 * @type: the DMA buffer type
273 * @device: the device pointer
274 * @size: the buffer size to allocate
275 * @dmab: buffer allocation record to store the allocated data
277 * Calls the memory-allocator function for the corresponding
278 * buffer type. When no space is left, this function reduces the size and
279 * tries to allocate again. The size actually allocated is stored in
282 * Return: Zero if the buffer with the given size is allocated successfully,
283 * otherwise a negative value on error.
285 int snd_dma_alloc_pages_fallback(int type
, struct device
*device
, size_t size
,
286 struct snd_dma_buffer
*dmab
)
290 while ((err
= snd_dma_alloc_pages(type
, device
, size
, dmab
)) < 0) {
294 if (size
<= PAGE_SIZE
)
296 aligned_size
= PAGE_SIZE
<< get_order(size
);
297 if (size
!= aligned_size
)
309 * snd_dma_free_pages - release the allocated buffer
310 * @dmab: the buffer allocation record to release
312 * Releases the allocated buffer via snd_dma_alloc_pages().
314 void snd_dma_free_pages(struct snd_dma_buffer
*dmab
)
316 switch (dmab
->dev
.type
) {
317 case SNDRV_DMA_TYPE_CONTINUOUS
:
318 snd_free_pages(dmab
->area
, dmab
->bytes
);
320 #ifdef CONFIG_HAS_DMA
321 case SNDRV_DMA_TYPE_DEV_IRAM
:
322 snd_free_dev_iram(dmab
);
324 case SNDRV_DMA_TYPE_DEV
:
325 snd_free_dev_pages(dmab
->dev
.dev
, dmab
->bytes
, dmab
->area
, dmab
->addr
);
328 #ifdef CONFIG_SND_DMA_SGBUF
329 case SNDRV_DMA_TYPE_DEV_SG
:
330 snd_free_sgbuf_pages(dmab
);
334 printk(KERN_ERR
"snd-malloc: invalid device type %d\n", dmab
->dev
.type
);
340 * snd_dma_get_reserved - get the reserved buffer for the given device
341 * @dmab: the buffer allocation record to store
344 * Looks for the reserved-buffer list and re-uses if the same buffer
345 * is found in the list. When the buffer is found, it's removed from the free list.
347 * Return: The size of buffer if the buffer is found, or zero if not found.
349 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
351 struct snd_mem_list
*mem
;
356 mutex_lock(&list_mutex
);
357 list_for_each_entry(mem
, &mem_list_head
, list
) {
359 (mem
->buffer
.dev
.dev
== NULL
|| dmab
->dev
.dev
== NULL
||
360 ! memcmp(&mem
->buffer
.dev
, &dmab
->dev
, sizeof(dmab
->dev
)))) {
361 struct device
*dev
= dmab
->dev
.dev
;
362 list_del(&mem
->list
);
364 if (dmab
->dev
.dev
== NULL
)
367 mutex_unlock(&list_mutex
);
371 mutex_unlock(&list_mutex
);
376 * snd_dma_reserve_buf - reserve the buffer
377 * @dmab: the buffer to reserve
380 * Reserves the given buffer as a reserved buffer.
382 * Return: Zero if successful, or a negative code on error.
384 int snd_dma_reserve_buf(struct snd_dma_buffer
*dmab
, unsigned int id
)
386 struct snd_mem_list
*mem
;
390 mem
= kmalloc(sizeof(*mem
), GFP_KERNEL
);
393 mutex_lock(&list_mutex
);
396 list_add_tail(&mem
->list
, &mem_list_head
);
397 mutex_unlock(&list_mutex
);
402 * purge all reserved buffers
404 static void free_all_reserved_pages(void)
407 struct snd_mem_list
*mem
;
409 mutex_lock(&list_mutex
);
410 while (! list_empty(&mem_list_head
)) {
411 p
= mem_list_head
.next
;
412 mem
= list_entry(p
, struct snd_mem_list
, list
);
414 snd_dma_free_pages(&mem
->buffer
);
417 mutex_unlock(&list_mutex
);
421 #ifdef CONFIG_PROC_FS
423 * proc file interface
425 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
426 static struct proc_dir_entry
*snd_mem_proc
;
428 static int snd_mem_proc_read(struct seq_file
*seq
, void *offset
)
430 long pages
= snd_allocated_pages
>> (PAGE_SHIFT
-12);
431 struct snd_mem_list
*mem
;
433 static char *types
[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
435 mutex_lock(&list_mutex
);
436 seq_printf(seq
, "pages : %li bytes (%li pages per %likB)\n",
437 pages
* PAGE_SIZE
, pages
, PAGE_SIZE
/ 1024);
439 list_for_each_entry(mem
, &mem_list_head
, list
) {
441 seq_printf(seq
, "buffer %d : ID %08x : type %s\n",
442 devno
, mem
->id
, types
[mem
->buffer
.dev
.type
]);
443 seq_printf(seq
, " addr = 0x%lx, size = %d bytes\n",
444 (unsigned long)mem
->buffer
.addr
,
445 (int)mem
->buffer
.bytes
);
447 mutex_unlock(&list_mutex
);
451 static int snd_mem_proc_open(struct inode
*inode
, struct file
*file
)
453 return single_open(file
, snd_mem_proc_read
, NULL
);
456 /* FIXME: for pci only - other bus? */
458 #define gettoken(bufp) strsep(bufp, " \t\n")
460 static ssize_t
snd_mem_proc_write(struct file
*file
, const char __user
* buffer
,
461 size_t count
, loff_t
* ppos
)
466 if (count
> sizeof(buf
) - 1)
468 if (copy_from_user(buf
, buffer
, count
))
473 token
= gettoken(&p
);
474 if (! token
|| *token
== '#')
476 if (strcmp(token
, "add") == 0) {
478 int vendor
, device
, size
, buffers
;
483 if ((token
= gettoken(&p
)) == NULL
||
484 (vendor
= simple_strtol(token
, NULL
, 0)) <= 0 ||
485 (token
= gettoken(&p
)) == NULL
||
486 (device
= simple_strtol(token
, NULL
, 0)) <= 0 ||
487 (token
= gettoken(&p
)) == NULL
||
488 (mask
= simple_strtol(token
, NULL
, 0)) < 0 ||
489 (token
= gettoken(&p
)) == NULL
||
490 (size
= memparse(token
, &endp
)) < 64*1024 ||
491 size
> 16*1024*1024 /* too big */ ||
492 (token
= gettoken(&p
)) == NULL
||
493 (buffers
= simple_strtol(token
, NULL
, 0)) <= 0 ||
495 printk(KERN_ERR
"snd-page-alloc: invalid proc write format\n");
503 while ((pci
= pci_get_device(vendor
, device
, pci
)) != NULL
) {
504 if (mask
> 0 && mask
< 0xffffffff) {
505 if (pci_set_dma_mask(pci
, mask
) < 0 ||
506 pci_set_consistent_dma_mask(pci
, mask
) < 0) {
507 printk(KERN_ERR
"snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask
, vendor
, device
);
512 for (i
= 0; i
< buffers
; i
++) {
513 struct snd_dma_buffer dmab
;
514 memset(&dmab
, 0, sizeof(dmab
));
515 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(pci
),
517 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
521 snd_dma_reserve_buf(&dmab
, snd_dma_pci_buf_id(pci
));
526 for (i
= 0; i
< buffers
; i
++) {
527 struct snd_dma_buffer dmab
;
528 memset(&dmab
, 0, sizeof(dmab
));
529 /* FIXME: We can allocate only in ZONE_DMA
530 * without a device pointer!
532 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, NULL
,
534 printk(KERN_ERR
"snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size
);
537 snd_dma_reserve_buf(&dmab
, (unsigned int)((vendor
<< 16) | device
));
540 } else if (strcmp(token
, "erase") == 0)
541 /* FIXME: need for releasing each buffer chunk? */
542 free_all_reserved_pages();
544 printk(KERN_ERR
"snd-page-alloc: invalid proc cmd\n");
547 #endif /* CONFIG_PCI */
549 static const struct file_operations snd_mem_proc_fops
= {
550 .owner
= THIS_MODULE
,
551 .open
= snd_mem_proc_open
,
554 .write
= snd_mem_proc_write
,
557 .release
= single_release
,
560 #endif /* CONFIG_PROC_FS */
566 static int __init
snd_mem_init(void)
568 #ifdef CONFIG_PROC_FS
569 snd_mem_proc
= proc_create(SND_MEM_PROC_FILE
, 0644, NULL
,
575 static void __exit
snd_mem_exit(void)
577 remove_proc_entry(SND_MEM_PROC_FILE
, NULL
);
578 free_all_reserved_pages();
579 if (snd_allocated_pages
> 0)
580 printk(KERN_ERR
"snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages
);
584 module_init(snd_mem_init
)
585 module_exit(snd_mem_exit
)
591 EXPORT_SYMBOL(snd_dma_alloc_pages
);
592 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback
);
593 EXPORT_SYMBOL(snd_dma_free_pages
);
595 EXPORT_SYMBOL(snd_dma_get_reserved_buf
);
596 EXPORT_SYMBOL(snd_dma_reserve_buf
);
598 EXPORT_SYMBOL(snd_malloc_pages
);
599 EXPORT_SYMBOL(snd_free_pages
);