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>
38 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
40 MODULE_LICENSE("GPL");
46 static DEFINE_MUTEX(list_mutex);
47 static LIST_HEAD(mem_list_head);
49 /* buffer preservation list */
51 struct snd_dma_buffer buffer;
53 struct list_head list;
56 /* id for pre-allocated buffers */
57 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
61 * Generic memory allocators
65 static long snd_allocated_pages; /* holding the number of allocated pages */
67 static inline void inc_snd_pages(int order)
69 snd_allocated_pages += 1 << order;
72 static inline void dec_snd_pages(int order)
74 snd_allocated_pages -= 1 << order;
78 * snd_malloc_pages - allocate pages with the given size
79 * @size: the size to allocate in bytes
80 * @gfp_flags: the allocation conditions, GFP_XXX
82 * Allocates the physically contiguous pages with the given size.
84 * Return: The pointer of the buffer, or %NULL if no enough memory.
86 void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
93 if (WARN_ON(!gfp_flags))
95 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
97 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
103 * snd_free_pages - release the pages
104 * @ptr: the buffer pointer to release
105 * @size: the allocated buffer size
107 * Releases the buffer allocated via snd_malloc_pages().
109 void snd_free_pages(void *ptr, size_t size)
115 pg = get_order(size);
117 free_pages((unsigned long) ptr, pg);
122 * Bus-specific memory allocators
126 #ifdef CONFIG_HAS_DMA
127 /* allocate the coherent DMA pages */
128 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
136 pg = get_order(size);
137 gfp_flags = GFP_KERNEL
138 | __GFP_COMP /* compound page lets parts be mapped */
139 | __GFP_NORETRY /* don't trigger OOM-killer */
140 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
141 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
148 /* free the coherent DMA pages */
149 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
156 pg = get_order(size);
158 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
160 #endif /* CONFIG_HAS_DMA */
164 * ALSA generic memory management
170 * snd_dma_alloc_pages - allocate the buffer area according to the given type
171 * @type: the DMA buffer type
172 * @device: the device pointer
173 * @size: the buffer size to allocate
174 * @dmab: buffer allocation record to store the allocated data
176 * Calls the memory-allocator function for the corresponding
179 * Return: Zero if the buffer with the given size is allocated successfully,
180 * otherwise a negative value on error.
182 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
183 struct snd_dma_buffer *dmab)
190 dmab->dev.type = type;
191 dmab->dev.dev = device;
194 case SNDRV_DMA_TYPE_CONTINUOUS:
195 dmab->area = snd_malloc_pages(size,
196 (__force gfp_t)(unsigned long)device);
199 #ifdef CONFIG_HAS_DMA
200 case SNDRV_DMA_TYPE_DEV:
201 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
204 #ifdef CONFIG_SND_DMA_SGBUF
205 case SNDRV_DMA_TYPE_DEV_SG:
206 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
210 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
222 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
223 * @type: the DMA buffer type
224 * @device: the device pointer
225 * @size: the buffer size to allocate
226 * @dmab: buffer allocation record to store the allocated data
228 * Calls the memory-allocator function for the corresponding
229 * buffer type. When no space is left, this function reduces the size and
230 * tries to allocate again. The size actually allocated is stored in
233 * Return: Zero if the buffer with the given size is allocated successfully,
234 * otherwise a negative value on error.
236 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
237 struct snd_dma_buffer *dmab)
241 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
245 if (size <= PAGE_SIZE)
247 aligned_size = PAGE_SIZE << get_order(size);
248 if (size != aligned_size)
260 * snd_dma_free_pages - release the allocated buffer
261 * @dmab: the buffer allocation record to release
263 * Releases the allocated buffer via snd_dma_alloc_pages().
265 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
267 switch (dmab->dev.type) {
268 case SNDRV_DMA_TYPE_CONTINUOUS:
269 snd_free_pages(dmab->area, dmab->bytes);
271 #ifdef CONFIG_HAS_DMA
272 case SNDRV_DMA_TYPE_DEV:
273 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
276 #ifdef CONFIG_SND_DMA_SGBUF
277 case SNDRV_DMA_TYPE_DEV_SG:
278 snd_free_sgbuf_pages(dmab);
282 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
288 * snd_dma_get_reserved - get the reserved buffer for the given device
289 * @dmab: the buffer allocation record to store
292 * Looks for the reserved-buffer list and re-uses if the same buffer
293 * is found in the list. When the buffer is found, it's removed from the free list.
295 * Return: The size of buffer if the buffer is found, or zero if not found.
297 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
299 struct snd_mem_list *mem;
304 mutex_lock(&list_mutex);
305 list_for_each_entry(mem, &mem_list_head, list) {
307 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
308 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
309 struct device *dev = dmab->dev.dev;
310 list_del(&mem->list);
312 if (dmab->dev.dev == NULL)
315 mutex_unlock(&list_mutex);
319 mutex_unlock(&list_mutex);
324 * snd_dma_reserve_buf - reserve the buffer
325 * @dmab: the buffer to reserve
328 * Reserves the given buffer as a reserved buffer.
330 * Return: Zero if successful, or a negative code on error.
332 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
334 struct snd_mem_list *mem;
338 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
341 mutex_lock(&list_mutex);
344 list_add_tail(&mem->list, &mem_list_head);
345 mutex_unlock(&list_mutex);
350 * purge all reserved buffers
352 static void free_all_reserved_pages(void)
355 struct snd_mem_list *mem;
357 mutex_lock(&list_mutex);
358 while (! list_empty(&mem_list_head)) {
359 p = mem_list_head.next;
360 mem = list_entry(p, struct snd_mem_list, list);
362 snd_dma_free_pages(&mem->buffer);
365 mutex_unlock(&list_mutex);
369 #ifdef CONFIG_PROC_FS
371 * proc file interface
373 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
374 static struct proc_dir_entry *snd_mem_proc;
376 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
378 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
379 struct snd_mem_list *mem;
381 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
383 mutex_lock(&list_mutex);
384 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
385 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
387 list_for_each_entry(mem, &mem_list_head, list) {
389 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
390 devno, mem->id, types[mem->buffer.dev.type]);
391 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
392 (unsigned long)mem->buffer.addr,
393 (int)mem->buffer.bytes);
395 mutex_unlock(&list_mutex);
399 static int snd_mem_proc_open(struct inode *inode, struct file *file)
401 return single_open(file, snd_mem_proc_read, NULL);
404 /* FIXME: for pci only - other bus? */
406 #define gettoken(bufp) strsep(bufp, " \t\n")
408 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
409 size_t count, loff_t * ppos)
414 if (count > sizeof(buf) - 1)
416 if (copy_from_user(buf, buffer, count))
421 token = gettoken(&p);
422 if (! token || *token == '#')
424 if (strcmp(token, "add") == 0) {
426 int vendor, device, size, buffers;
431 if ((token = gettoken(&p)) == NULL ||
432 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
433 (token = gettoken(&p)) == NULL ||
434 (device = simple_strtol(token, NULL, 0)) <= 0 ||
435 (token = gettoken(&p)) == NULL ||
436 (mask = simple_strtol(token, NULL, 0)) < 0 ||
437 (token = gettoken(&p)) == NULL ||
438 (size = memparse(token, &endp)) < 64*1024 ||
439 size > 16*1024*1024 /* too big */ ||
440 (token = gettoken(&p)) == NULL ||
441 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
443 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
451 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
452 if (mask > 0 && mask < 0xffffffff) {
453 if (pci_set_dma_mask(pci, mask) < 0 ||
454 pci_set_consistent_dma_mask(pci, mask) < 0) {
455 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
460 for (i = 0; i < buffers; i++) {
461 struct snd_dma_buffer dmab;
462 memset(&dmab, 0, sizeof(dmab));
463 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
465 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
469 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
474 for (i = 0; i < buffers; i++) {
475 struct snd_dma_buffer dmab;
476 memset(&dmab, 0, sizeof(dmab));
477 /* FIXME: We can allocate only in ZONE_DMA
478 * without a device pointer!
480 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
482 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
485 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
488 } else if (strcmp(token, "erase") == 0)
489 /* FIXME: need for releasing each buffer chunk? */
490 free_all_reserved_pages();
492 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
495 #endif /* CONFIG_PCI */
497 static const struct file_operations snd_mem_proc_fops = {
498 .owner = THIS_MODULE,
499 .open = snd_mem_proc_open,
502 .write = snd_mem_proc_write,
505 .release = single_release,
508 #endif /* CONFIG_PROC_FS */
514 static int __init snd_mem_init(void)
516 #ifdef CONFIG_PROC_FS
517 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
523 static void __exit snd_mem_exit(void)
525 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
526 free_all_reserved_pages();
527 if (snd_allocated_pages > 0)
528 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
532 module_init(snd_mem_init)
533 module_exit(snd_mem_exit)
539 EXPORT_SYMBOL(snd_dma_alloc_pages);
540 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
541 EXPORT_SYMBOL(snd_dma_free_pages);
543 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
544 EXPORT_SYMBOL(snd_dma_reserve_buf);
546 EXPORT_SYMBOL(snd_malloc_pages);
547 EXPORT_SYMBOL(snd_free_pages);