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 void *snd_malloc_sgbuf_pages(struct device *device,
47 size_t size, struct snd_dma_buffer *dmab,
49 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
54 static DEFINE_MUTEX(list_mutex);
55 static LIST_HEAD(mem_list_head);
57 /* buffer preservation list */
59 struct snd_dma_buffer buffer;
61 struct list_head list;
64 /* id for pre-allocated buffers */
65 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
67 #ifdef CONFIG_SND_DEBUG
68 #define __ASTRING__(x) #x
69 #define snd_assert(expr, args...) do {\
71 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
76 #define snd_assert(expr, args...) /**/
81 * Generic memory allocators
85 static long snd_allocated_pages; /* holding the number of allocated pages */
87 static inline void inc_snd_pages(int order)
89 snd_allocated_pages += 1 << order;
92 static inline void dec_snd_pages(int order)
94 snd_allocated_pages -= 1 << order;
98 * snd_malloc_pages - allocate pages with the given size
99 * @size: the size to allocate in bytes
100 * @gfp_flags: the allocation conditions, GFP_XXX
102 * Allocates the physically contiguous pages with the given size.
104 * Returns the pointer of the buffer, or NULL if no enoguh memory.
106 void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
111 snd_assert(size > 0, return NULL);
112 snd_assert(gfp_flags != 0, return NULL);
113 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
114 pg = get_order(size);
115 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
121 * snd_free_pages - release the pages
122 * @ptr: the buffer pointer to release
123 * @size: the allocated buffer size
125 * Releases the buffer allocated via snd_malloc_pages().
127 void snd_free_pages(void *ptr, size_t size)
133 pg = get_order(size);
135 free_pages((unsigned long) ptr, pg);
140 * Bus-specific memory allocators
144 #ifdef CONFIG_HAS_DMA
145 /* allocate the coherent DMA pages */
146 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
152 snd_assert(size > 0, return NULL);
153 snd_assert(dma != NULL, return NULL);
154 pg = get_order(size);
155 gfp_flags = GFP_KERNEL
156 | __GFP_COMP /* compound page lets parts be mapped */
157 | __GFP_NORETRY /* don't trigger OOM-killer */
158 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
159 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
166 /* free the coherent DMA pages */
167 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
174 pg = get_order(size);
176 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
178 #endif /* CONFIG_HAS_DMA */
182 * ALSA generic memory management
188 * snd_dma_alloc_pages - allocate the buffer area according to the given type
189 * @type: the DMA buffer type
190 * @device: the device pointer
191 * @size: the buffer size to allocate
192 * @dmab: buffer allocation record to store the allocated data
194 * Calls the memory-allocator function for the corresponding
197 * Returns zero if the buffer with the given size is allocated successfuly,
198 * other a negative value at error.
200 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
201 struct snd_dma_buffer *dmab)
203 snd_assert(size > 0, return -ENXIO);
204 snd_assert(dmab != NULL, return -ENXIO);
206 dmab->dev.type = type;
207 dmab->dev.dev = device;
210 case SNDRV_DMA_TYPE_CONTINUOUS:
211 dmab->area = snd_malloc_pages(size, (unsigned long)device);
214 #ifdef CONFIG_HAS_DMA
215 case SNDRV_DMA_TYPE_DEV:
216 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
218 case SNDRV_DMA_TYPE_DEV_SG:
219 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
223 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
235 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
236 * @type: the DMA buffer type
237 * @device: the device pointer
238 * @size: the buffer size to allocate
239 * @dmab: buffer allocation record to store the allocated data
241 * Calls the memory-allocator function for the corresponding
242 * buffer type. When no space is left, this function reduces the size and
243 * tries to allocate again. The size actually allocated is stored in
246 * Returns zero if the buffer with the given size is allocated successfuly,
247 * other a negative value at error.
249 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
250 struct snd_dma_buffer *dmab)
254 snd_assert(size > 0, return -ENXIO);
255 snd_assert(dmab != NULL, return -ENXIO);
257 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
261 if (size <= PAGE_SIZE)
271 * snd_dma_free_pages - release the allocated buffer
272 * @dmab: the buffer allocation record to release
274 * Releases the allocated buffer via snd_dma_alloc_pages().
276 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
278 switch (dmab->dev.type) {
279 case SNDRV_DMA_TYPE_CONTINUOUS:
280 snd_free_pages(dmab->area, dmab->bytes);
282 #ifdef CONFIG_HAS_DMA
283 case SNDRV_DMA_TYPE_DEV:
284 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
286 case SNDRV_DMA_TYPE_DEV_SG:
287 snd_free_sgbuf_pages(dmab);
291 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
297 * snd_dma_get_reserved - get the reserved buffer for the given device
298 * @dmab: the buffer allocation record to store
301 * Looks for the reserved-buffer list and re-uses if the same buffer
302 * is found in the list. When the buffer is found, it's removed from the free list.
304 * Returns the size of buffer if the buffer is found, or zero if not found.
306 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
308 struct snd_mem_list *mem;
310 snd_assert(dmab, return 0);
312 mutex_lock(&list_mutex);
313 list_for_each_entry(mem, &mem_list_head, list) {
315 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
316 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
317 struct device *dev = dmab->dev.dev;
318 list_del(&mem->list);
320 if (dmab->dev.dev == NULL)
323 mutex_unlock(&list_mutex);
327 mutex_unlock(&list_mutex);
332 * snd_dma_reserve_buf - reserve the buffer
333 * @dmab: the buffer to reserve
336 * Reserves the given buffer as a reserved buffer.
338 * Returns zero if successful, or a negative code at error.
340 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
342 struct snd_mem_list *mem;
344 snd_assert(dmab, return -EINVAL);
345 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
348 mutex_lock(&list_mutex);
351 list_add_tail(&mem->list, &mem_list_head);
352 mutex_unlock(&list_mutex);
357 * purge all reserved buffers
359 static void free_all_reserved_pages(void)
362 struct snd_mem_list *mem;
364 mutex_lock(&list_mutex);
365 while (! list_empty(&mem_list_head)) {
366 p = mem_list_head.next;
367 mem = list_entry(p, struct snd_mem_list, list);
369 snd_dma_free_pages(&mem->buffer);
372 mutex_unlock(&list_mutex);
376 #ifdef CONFIG_PROC_FS
378 * proc file interface
380 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
381 static struct proc_dir_entry *snd_mem_proc;
383 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
385 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
386 struct snd_mem_list *mem;
388 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
390 mutex_lock(&list_mutex);
391 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
392 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
394 list_for_each_entry(mem, &mem_list_head, list) {
396 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
397 devno, mem->id, types[mem->buffer.dev.type]);
398 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
399 (unsigned long)mem->buffer.addr,
400 (int)mem->buffer.bytes);
402 mutex_unlock(&list_mutex);
406 static int snd_mem_proc_open(struct inode *inode, struct file *file)
408 return single_open(file, snd_mem_proc_read, NULL);
411 /* FIXME: for pci only - other bus? */
413 #define gettoken(bufp) strsep(bufp, " \t\n")
415 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
416 size_t count, loff_t * ppos)
421 if (count > sizeof(buf) - 1)
423 if (copy_from_user(buf, buffer, count))
428 token = gettoken(&p);
429 if (! token || *token == '#')
431 if (strcmp(token, "add") == 0) {
433 int vendor, device, size, buffers;
438 if ((token = gettoken(&p)) == NULL ||
439 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
440 (token = gettoken(&p)) == NULL ||
441 (device = simple_strtol(token, NULL, 0)) <= 0 ||
442 (token = gettoken(&p)) == NULL ||
443 (mask = simple_strtol(token, NULL, 0)) < 0 ||
444 (token = gettoken(&p)) == NULL ||
445 (size = memparse(token, &endp)) < 64*1024 ||
446 size > 16*1024*1024 /* too big */ ||
447 (token = gettoken(&p)) == NULL ||
448 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
450 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
458 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
459 if (mask > 0 && mask < 0xffffffff) {
460 if (pci_set_dma_mask(pci, mask) < 0 ||
461 pci_set_consistent_dma_mask(pci, mask) < 0) {
462 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
467 for (i = 0; i < buffers; i++) {
468 struct snd_dma_buffer dmab;
469 memset(&dmab, 0, sizeof(dmab));
470 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
472 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
476 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
481 for (i = 0; i < buffers; i++) {
482 struct snd_dma_buffer dmab;
483 memset(&dmab, 0, sizeof(dmab));
484 /* FIXME: We can allocate only in ZONE_DMA
485 * without a device pointer!
487 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
489 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
492 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
495 } else if (strcmp(token, "erase") == 0)
496 /* FIXME: need for releasing each buffer chunk? */
497 free_all_reserved_pages();
499 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
502 #endif /* CONFIG_PCI */
504 static const struct file_operations snd_mem_proc_fops = {
505 .owner = THIS_MODULE,
506 .open = snd_mem_proc_open,
509 .write = snd_mem_proc_write,
512 .release = single_release,
515 #endif /* CONFIG_PROC_FS */
521 static int __init snd_mem_init(void)
523 #ifdef CONFIG_PROC_FS
524 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
530 static void __exit snd_mem_exit(void)
532 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
533 free_all_reserved_pages();
534 if (snd_allocated_pages > 0)
535 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
539 module_init(snd_mem_init)
540 module_exit(snd_mem_exit)
546 EXPORT_SYMBOL(snd_dma_alloc_pages);
547 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
548 EXPORT_SYMBOL(snd_dma_free_pages);
550 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
551 EXPORT_SYMBOL(snd_dma_reserve_buf);
553 EXPORT_SYMBOL(snd_malloc_pages);
554 EXPORT_SYMBOL(snd_free_pages);