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 = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
201 static void snd_free_sbus_pages(struct device *dev, size_t size,
202 void *ptr, dma_addr_t dma_addr)
204 struct sbus_dev *sdev = (struct sbus_dev *)dev;
209 pg = get_order(size);
211 sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
214 #endif /* CONFIG_SBUS */
218 * ALSA generic memory management
224 * snd_dma_alloc_pages - allocate the buffer area according to the given type
225 * @type: the DMA buffer type
226 * @device: the device pointer
227 * @size: the buffer size to allocate
228 * @dmab: buffer allocation record to store the allocated data
230 * Calls the memory-allocator function for the corresponding
233 * Returns zero if the buffer with the given size is allocated successfuly,
234 * other a negative value at error.
236 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
237 struct snd_dma_buffer *dmab)
239 snd_assert(size > 0, return -ENXIO);
240 snd_assert(dmab != NULL, return -ENXIO);
242 dmab->dev.type = type;
243 dmab->dev.dev = device;
246 case SNDRV_DMA_TYPE_CONTINUOUS:
247 dmab->area = snd_malloc_pages(size, (unsigned long)device);
251 case SNDRV_DMA_TYPE_SBUS:
252 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
255 #ifdef CONFIG_HAS_DMA
256 case SNDRV_DMA_TYPE_DEV:
257 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
259 case SNDRV_DMA_TYPE_DEV_SG:
260 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
264 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
276 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
277 * @type: the DMA buffer type
278 * @device: the device pointer
279 * @size: the buffer size to allocate
280 * @dmab: buffer allocation record to store the allocated data
282 * Calls the memory-allocator function for the corresponding
283 * buffer type. When no space is left, this function reduces the size and
284 * tries to allocate again. The size actually allocated is stored in
287 * Returns zero if the buffer with the given size is allocated successfuly,
288 * other a negative value at error.
290 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
291 struct snd_dma_buffer *dmab)
295 snd_assert(size > 0, return -ENXIO);
296 snd_assert(dmab != NULL, return -ENXIO);
298 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
302 if (size <= PAGE_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);
324 case SNDRV_DMA_TYPE_SBUS:
325 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
328 #ifdef CONFIG_HAS_DMA
329 case SNDRV_DMA_TYPE_DEV:
330 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
332 case SNDRV_DMA_TYPE_DEV_SG:
333 snd_free_sgbuf_pages(dmab);
337 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
343 * snd_dma_get_reserved - get the reserved buffer for the given device
344 * @dmab: the buffer allocation record to store
347 * Looks for the reserved-buffer list and re-uses if the same buffer
348 * is found in the list. When the buffer is found, it's removed from the free list.
350 * Returns the size of buffer if the buffer is found, or zero if not found.
352 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
354 struct snd_mem_list *mem;
356 snd_assert(dmab, return 0);
358 mutex_lock(&list_mutex);
359 list_for_each_entry(mem, &mem_list_head, list) {
361 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
362 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
363 struct device *dev = dmab->dev.dev;
364 list_del(&mem->list);
366 if (dmab->dev.dev == NULL)
369 mutex_unlock(&list_mutex);
373 mutex_unlock(&list_mutex);
378 * snd_dma_reserve_buf - reserve the buffer
379 * @dmab: the buffer to reserve
382 * Reserves the given buffer as a reserved buffer.
384 * Returns zero if successful, or a negative code at error.
386 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
388 struct snd_mem_list *mem;
390 snd_assert(dmab, return -EINVAL);
391 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
394 mutex_lock(&list_mutex);
397 list_add_tail(&mem->list, &mem_list_head);
398 mutex_unlock(&list_mutex);
403 * purge all reserved buffers
405 static void free_all_reserved_pages(void)
408 struct snd_mem_list *mem;
410 mutex_lock(&list_mutex);
411 while (! list_empty(&mem_list_head)) {
412 p = mem_list_head.next;
413 mem = list_entry(p, struct snd_mem_list, list);
415 snd_dma_free_pages(&mem->buffer);
418 mutex_unlock(&list_mutex);
422 #ifdef CONFIG_PROC_FS
424 * proc file interface
426 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
427 static struct proc_dir_entry *snd_mem_proc;
429 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
431 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
432 struct snd_mem_list *mem;
434 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
436 mutex_lock(&list_mutex);
437 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
438 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
440 list_for_each_entry(mem, &mem_list_head, list) {
442 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
443 devno, mem->id, types[mem->buffer.dev.type]);
444 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
445 (unsigned long)mem->buffer.addr,
446 (int)mem->buffer.bytes);
448 mutex_unlock(&list_mutex);
452 static int snd_mem_proc_open(struct inode *inode, struct file *file)
454 return single_open(file, snd_mem_proc_read, NULL);
457 /* FIXME: for pci only - other bus? */
459 #define gettoken(bufp) strsep(bufp, " \t\n")
461 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
462 size_t count, loff_t * ppos)
467 if (count > sizeof(buf) - 1)
469 if (copy_from_user(buf, buffer, count))
474 token = gettoken(&p);
475 if (! token || *token == '#')
477 if (strcmp(token, "add") == 0) {
479 int vendor, device, size, buffers;
484 if ((token = gettoken(&p)) == NULL ||
485 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
486 (token = gettoken(&p)) == NULL ||
487 (device = simple_strtol(token, NULL, 0)) <= 0 ||
488 (token = gettoken(&p)) == NULL ||
489 (mask = simple_strtol(token, NULL, 0)) < 0 ||
490 (token = gettoken(&p)) == NULL ||
491 (size = memparse(token, &endp)) < 64*1024 ||
492 size > 16*1024*1024 /* too big */ ||
493 (token = gettoken(&p)) == NULL ||
494 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
496 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
504 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
505 if (mask > 0 && mask < 0xffffffff) {
506 if (pci_set_dma_mask(pci, mask) < 0 ||
507 pci_set_consistent_dma_mask(pci, mask) < 0) {
508 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
513 for (i = 0; i < buffers; i++) {
514 struct snd_dma_buffer dmab;
515 memset(&dmab, 0, sizeof(dmab));
516 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
518 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
522 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
527 for (i = 0; i < buffers; i++) {
528 struct snd_dma_buffer dmab;
529 memset(&dmab, 0, sizeof(dmab));
530 /* FIXME: We can allocate only in ZONE_DMA
531 * without a device pointer!
533 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
535 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
538 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
541 } else if (strcmp(token, "erase") == 0)
542 /* FIXME: need for releasing each buffer chunk? */
543 free_all_reserved_pages();
545 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
548 #endif /* CONFIG_PCI */
550 static const struct file_operations snd_mem_proc_fops = {
551 .owner = THIS_MODULE,
552 .open = snd_mem_proc_open,
555 .write = snd_mem_proc_write,
558 .release = single_release,
561 #endif /* CONFIG_PROC_FS */
567 static int __init snd_mem_init(void)
569 #ifdef CONFIG_PROC_FS
570 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
576 static void __exit snd_mem_exit(void)
578 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
579 free_all_reserved_pages();
580 if (snd_allocated_pages > 0)
581 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
585 module_init(snd_mem_init)
586 module_exit(snd_mem_exit)
592 EXPORT_SYMBOL(snd_dma_alloc_pages);
593 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
594 EXPORT_SYMBOL(snd_dma_free_pages);
596 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
597 EXPORT_SYMBOL(snd_dma_reserve_buf);
599 EXPORT_SYMBOL(snd_malloc_pages);
600 EXPORT_SYMBOL(snd_free_pages);