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 void *snd_malloc_sgbuf_pages(struct device *device,
50 size_t size, struct snd_dma_buffer *dmab,
52 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
57 static DEFINE_MUTEX(list_mutex);
58 static LIST_HEAD(mem_list_head);
60 /* buffer preservation list */
62 struct snd_dma_buffer buffer;
64 struct list_head list;
67 /* id for pre-allocated buffers */
68 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
70 #ifdef CONFIG_SND_DEBUG
71 #define __ASTRING__(x) #x
72 #define snd_assert(expr, args...) do {\
74 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
79 #define snd_assert(expr, args...) /**/
84 * Generic memory allocators
88 static long snd_allocated_pages; /* holding the number of allocated pages */
90 static inline void inc_snd_pages(int order)
92 snd_allocated_pages += 1 << order;
95 static inline void dec_snd_pages(int order)
97 snd_allocated_pages -= 1 << order;
101 * snd_malloc_pages - allocate pages with the given size
102 * @size: the size to allocate in bytes
103 * @gfp_flags: the allocation conditions, GFP_XXX
105 * Allocates the physically contiguous pages with the given size.
107 * Returns the pointer of the buffer, or NULL if no enoguh memory.
109 void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
114 snd_assert(size > 0, return NULL);
115 snd_assert(gfp_flags != 0, return NULL);
116 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
117 pg = get_order(size);
118 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
124 * snd_free_pages - release the pages
125 * @ptr: the buffer pointer to release
126 * @size: the allocated buffer size
128 * Releases the buffer allocated via snd_malloc_pages().
130 void snd_free_pages(void *ptr, size_t size)
136 pg = get_order(size);
138 free_pages((unsigned long) ptr, pg);
143 * Bus-specific memory allocators
147 #ifdef CONFIG_HAS_DMA
148 /* allocate the coherent DMA pages */
149 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
155 snd_assert(size > 0, return NULL);
156 snd_assert(dma != NULL, return NULL);
157 pg = get_order(size);
158 gfp_flags = GFP_KERNEL
159 | __GFP_COMP /* compound page lets parts be mapped */
160 | __GFP_NORETRY /* don't trigger OOM-killer */
161 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
162 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
169 /* free the coherent DMA pages */
170 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
177 pg = get_order(size);
179 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
181 #endif /* CONFIG_HAS_DMA */
185 static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
186 dma_addr_t *dma_addr)
188 struct sbus_dev *sdev = (struct sbus_dev *)dev;
192 snd_assert(size > 0, return NULL);
193 snd_assert(dma_addr != NULL, return NULL);
194 pg = get_order(size);
195 res = dma_alloc_coherent(&sdev->ofdev.dev, PAGE_SIZE * (1 << pg),
196 dma_addr, GFP_ATOMIC);
202 static void snd_free_sbus_pages(struct device *dev, size_t size,
203 void *ptr, dma_addr_t dma_addr)
205 struct sbus_dev *sdev = (struct sbus_dev *)dev;
210 pg = get_order(size);
212 dma_free_coherent(&sdev->ofdev.dev, PAGE_SIZE * (1 << pg),
216 #endif /* CONFIG_SBUS */
220 * ALSA generic memory management
226 * snd_dma_alloc_pages - allocate the buffer area according to the given type
227 * @type: the DMA buffer type
228 * @device: the device pointer
229 * @size: the buffer size to allocate
230 * @dmab: buffer allocation record to store the allocated data
232 * Calls the memory-allocator function for the corresponding
235 * Returns zero if the buffer with the given size is allocated successfuly,
236 * other a negative value at error.
238 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
239 struct snd_dma_buffer *dmab)
241 snd_assert(size > 0, return -ENXIO);
242 snd_assert(dmab != NULL, return -ENXIO);
244 dmab->dev.type = type;
245 dmab->dev.dev = device;
248 case SNDRV_DMA_TYPE_CONTINUOUS:
249 dmab->area = snd_malloc_pages(size, (unsigned long)device);
253 case SNDRV_DMA_TYPE_SBUS:
254 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
257 #ifdef CONFIG_HAS_DMA
258 case SNDRV_DMA_TYPE_DEV:
259 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
261 case SNDRV_DMA_TYPE_DEV_SG:
262 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
266 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
278 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
279 * @type: the DMA buffer type
280 * @device: the device pointer
281 * @size: the buffer size to allocate
282 * @dmab: buffer allocation record to store the allocated data
284 * Calls the memory-allocator function for the corresponding
285 * buffer type. When no space is left, this function reduces the size and
286 * tries to allocate again. The size actually allocated is stored in
289 * Returns zero if the buffer with the given size is allocated successfuly,
290 * other a negative value at error.
292 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
293 struct snd_dma_buffer *dmab)
297 snd_assert(size > 0, return -ENXIO);
298 snd_assert(dmab != NULL, return -ENXIO);
300 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
304 if (size <= PAGE_SIZE)
314 * snd_dma_free_pages - release the allocated buffer
315 * @dmab: the buffer allocation record to release
317 * Releases the allocated buffer via snd_dma_alloc_pages().
319 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
321 switch (dmab->dev.type) {
322 case SNDRV_DMA_TYPE_CONTINUOUS:
323 snd_free_pages(dmab->area, dmab->bytes);
326 case SNDRV_DMA_TYPE_SBUS:
327 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
330 #ifdef CONFIG_HAS_DMA
331 case SNDRV_DMA_TYPE_DEV:
332 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
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 * Returns 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;
358 snd_assert(dmab, return 0);
360 mutex_lock(&list_mutex);
361 list_for_each_entry(mem, &mem_list_head, list) {
363 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
364 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
365 struct device *dev = dmab->dev.dev;
366 list_del(&mem->list);
368 if (dmab->dev.dev == NULL)
371 mutex_unlock(&list_mutex);
375 mutex_unlock(&list_mutex);
380 * snd_dma_reserve_buf - reserve the buffer
381 * @dmab: the buffer to reserve
384 * Reserves the given buffer as a reserved buffer.
386 * Returns zero if successful, or a negative code at error.
388 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
390 struct snd_mem_list *mem;
392 snd_assert(dmab, return -EINVAL);
393 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
396 mutex_lock(&list_mutex);
399 list_add_tail(&mem->list, &mem_list_head);
400 mutex_unlock(&list_mutex);
405 * purge all reserved buffers
407 static void free_all_reserved_pages(void)
410 struct snd_mem_list *mem;
412 mutex_lock(&list_mutex);
413 while (! list_empty(&mem_list_head)) {
414 p = mem_list_head.next;
415 mem = list_entry(p, struct snd_mem_list, list);
417 snd_dma_free_pages(&mem->buffer);
420 mutex_unlock(&list_mutex);
424 #ifdef CONFIG_PROC_FS
426 * proc file interface
428 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
429 static struct proc_dir_entry *snd_mem_proc;
431 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
433 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
434 struct snd_mem_list *mem;
436 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
438 mutex_lock(&list_mutex);
439 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
440 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
442 list_for_each_entry(mem, &mem_list_head, list) {
444 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
445 devno, mem->id, types[mem->buffer.dev.type]);
446 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
447 (unsigned long)mem->buffer.addr,
448 (int)mem->buffer.bytes);
450 mutex_unlock(&list_mutex);
454 static int snd_mem_proc_open(struct inode *inode, struct file *file)
456 return single_open(file, snd_mem_proc_read, NULL);
459 /* FIXME: for pci only - other bus? */
461 #define gettoken(bufp) strsep(bufp, " \t\n")
463 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
464 size_t count, loff_t * ppos)
469 if (count > sizeof(buf) - 1)
471 if (copy_from_user(buf, buffer, count))
476 token = gettoken(&p);
477 if (! token || *token == '#')
479 if (strcmp(token, "add") == 0) {
481 int vendor, device, size, buffers;
486 if ((token = gettoken(&p)) == NULL ||
487 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
488 (token = gettoken(&p)) == NULL ||
489 (device = simple_strtol(token, NULL, 0)) <= 0 ||
490 (token = gettoken(&p)) == NULL ||
491 (mask = simple_strtol(token, NULL, 0)) < 0 ||
492 (token = gettoken(&p)) == NULL ||
493 (size = memparse(token, &endp)) < 64*1024 ||
494 size > 16*1024*1024 /* too big */ ||
495 (token = gettoken(&p)) == NULL ||
496 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
498 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
506 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
507 if (mask > 0 && mask < 0xffffffff) {
508 if (pci_set_dma_mask(pci, mask) < 0 ||
509 pci_set_consistent_dma_mask(pci, mask) < 0) {
510 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
515 for (i = 0; i < buffers; i++) {
516 struct snd_dma_buffer dmab;
517 memset(&dmab, 0, sizeof(dmab));
518 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
520 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
524 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
529 for (i = 0; i < buffers; i++) {
530 struct snd_dma_buffer dmab;
531 memset(&dmab, 0, sizeof(dmab));
532 /* FIXME: We can allocate only in ZONE_DMA
533 * without a device pointer!
535 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
537 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
540 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
543 } else if (strcmp(token, "erase") == 0)
544 /* FIXME: need for releasing each buffer chunk? */
545 free_all_reserved_pages();
547 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
550 #endif /* CONFIG_PCI */
552 static const struct file_operations snd_mem_proc_fops = {
553 .owner = THIS_MODULE,
554 .open = snd_mem_proc_open,
557 .write = snd_mem_proc_write,
560 .release = single_release,
563 #endif /* CONFIG_PROC_FS */
569 static int __init snd_mem_init(void)
571 #ifdef CONFIG_PROC_FS
572 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
578 static void __exit snd_mem_exit(void)
580 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
581 free_all_reserved_pages();
582 if (snd_allocated_pages > 0)
583 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
587 module_init(snd_mem_init)
588 module_exit(snd_mem_exit)
594 EXPORT_SYMBOL(snd_dma_alloc_pages);
595 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
596 EXPORT_SYMBOL(snd_dma_free_pages);
598 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
599 EXPORT_SYMBOL(snd_dma_reserve_buf);
601 EXPORT_SYMBOL(snd_malloc_pages);
602 EXPORT_SYMBOL(snd_free_pages);