GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / core / memalloc.c
blobc0ae89976875b84c4b392cc8c13cd15b9c5aec3b
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Takashi Iwai <tiwai@suse.de>
4 *
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>
29 #include <linux/mm.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 */
50 struct snd_mem_list {
51 struct snd_dma_buffer buffer;
52 unsigned int id;
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;
77 /**
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 * Returns the pointer of the buffer, or NULL if no enoguh memory.
86 void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
88 int pg;
89 void *res;
91 if (WARN_ON(!size))
92 return NULL;
93 if (WARN_ON(!gfp_flags))
94 return NULL;
95 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
96 pg = get_order(size);
97 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
98 inc_snd_pages(pg);
99 return res;
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)
111 int pg;
113 if (ptr == NULL)
114 return;
115 pg = get_order(size);
116 dec_snd_pages(pg);
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)
130 int pg;
131 void *res;
132 gfp_t gfp_flags;
134 if (WARN_ON(!dma))
135 return NULL;
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);
142 if (res != NULL)
143 inc_snd_pages(pg);
145 return res;
148 /* free the coherent DMA pages */
149 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
150 dma_addr_t dma)
152 int pg;
154 if (ptr == NULL)
155 return;
156 pg = get_order(size);
157 dec_snd_pages(pg);
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
177 * buffer type.
179 * Returns zero if the buffer with the given size is allocated successfuly,
180 * other a negative value at error.
182 int snd_dma_alloc_pages(int type, struct device *device, size_t size,
183 struct snd_dma_buffer *dmab)
185 if (WARN_ON(!size))
186 return -ENXIO;
187 if (WARN_ON(!dmab))
188 return -ENXIO;
190 dmab->dev.type = type;
191 dmab->dev.dev = device;
192 dmab->bytes = 0;
193 switch (type) {
194 case SNDRV_DMA_TYPE_CONTINUOUS:
195 dmab->area = snd_malloc_pages(size, (unsigned long)device);
196 dmab->addr = 0;
197 break;
198 #ifdef CONFIG_HAS_DMA
199 case SNDRV_DMA_TYPE_DEV:
200 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
201 break;
202 #endif
203 #ifdef CONFIG_SND_DMA_SGBUF
204 case SNDRV_DMA_TYPE_DEV_SG:
205 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
206 break;
207 #endif
208 default:
209 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
210 dmab->area = NULL;
211 dmab->addr = 0;
212 return -ENXIO;
214 if (! dmab->area)
215 return -ENOMEM;
216 dmab->bytes = size;
217 return 0;
221 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
222 * @type: the DMA buffer type
223 * @device: the device pointer
224 * @size: the buffer size to allocate
225 * @dmab: buffer allocation record to store the allocated data
227 * Calls the memory-allocator function for the corresponding
228 * buffer type. When no space is left, this function reduces the size and
229 * tries to allocate again. The size actually allocated is stored in
230 * res_size argument.
232 * Returns zero if the buffer with the given size is allocated successfuly,
233 * other a negative value at error.
235 int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
236 struct snd_dma_buffer *dmab)
238 int err;
240 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
241 size_t aligned_size;
242 if (err != -ENOMEM)
243 return err;
244 if (size <= PAGE_SIZE)
245 return -ENOMEM;
246 aligned_size = PAGE_SIZE << get_order(size);
247 if (size != aligned_size)
248 size = aligned_size;
249 else
250 size >>= 1;
252 if (! dmab->area)
253 return -ENOMEM;
254 return 0;
259 * snd_dma_free_pages - release the allocated buffer
260 * @dmab: the buffer allocation record to release
262 * Releases the allocated buffer via snd_dma_alloc_pages().
264 void snd_dma_free_pages(struct snd_dma_buffer *dmab)
266 switch (dmab->dev.type) {
267 case SNDRV_DMA_TYPE_CONTINUOUS:
268 snd_free_pages(dmab->area, dmab->bytes);
269 break;
270 #ifdef CONFIG_HAS_DMA
271 case SNDRV_DMA_TYPE_DEV:
272 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
273 break;
274 #endif
275 #ifdef CONFIG_SND_DMA_SGBUF
276 case SNDRV_DMA_TYPE_DEV_SG:
277 snd_free_sgbuf_pages(dmab);
278 break;
279 #endif
280 default:
281 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
287 * snd_dma_get_reserved - get the reserved buffer for the given device
288 * @dmab: the buffer allocation record to store
289 * @id: the buffer id
291 * Looks for the reserved-buffer list and re-uses if the same buffer
292 * is found in the list. When the buffer is found, it's removed from the free list.
294 * Returns the size of buffer if the buffer is found, or zero if not found.
296 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
298 struct snd_mem_list *mem;
300 if (WARN_ON(!dmab))
301 return 0;
303 mutex_lock(&list_mutex);
304 list_for_each_entry(mem, &mem_list_head, list) {
305 if (mem->id == id &&
306 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
307 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
308 struct device *dev = dmab->dev.dev;
309 list_del(&mem->list);
310 *dmab = mem->buffer;
311 if (dmab->dev.dev == NULL)
312 dmab->dev.dev = dev;
313 kfree(mem);
314 mutex_unlock(&list_mutex);
315 return dmab->bytes;
318 mutex_unlock(&list_mutex);
319 return 0;
323 * snd_dma_reserve_buf - reserve the buffer
324 * @dmab: the buffer to reserve
325 * @id: the buffer id
327 * Reserves the given buffer as a reserved buffer.
329 * Returns zero if successful, or a negative code at error.
331 int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
333 struct snd_mem_list *mem;
335 if (WARN_ON(!dmab))
336 return -EINVAL;
337 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
338 if (! mem)
339 return -ENOMEM;
340 mutex_lock(&list_mutex);
341 mem->buffer = *dmab;
342 mem->id = id;
343 list_add_tail(&mem->list, &mem_list_head);
344 mutex_unlock(&list_mutex);
345 return 0;
349 * purge all reserved buffers
351 static void free_all_reserved_pages(void)
353 struct list_head *p;
354 struct snd_mem_list *mem;
356 mutex_lock(&list_mutex);
357 while (! list_empty(&mem_list_head)) {
358 p = mem_list_head.next;
359 mem = list_entry(p, struct snd_mem_list, list);
360 list_del(p);
361 snd_dma_free_pages(&mem->buffer);
362 kfree(mem);
364 mutex_unlock(&list_mutex);
368 #ifdef CONFIG_PROC_FS
370 * proc file interface
372 #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
373 static struct proc_dir_entry *snd_mem_proc;
375 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
377 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
378 struct snd_mem_list *mem;
379 int devno;
380 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
382 mutex_lock(&list_mutex);
383 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
384 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
385 devno = 0;
386 list_for_each_entry(mem, &mem_list_head, list) {
387 devno++;
388 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
389 devno, mem->id, types[mem->buffer.dev.type]);
390 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
391 (unsigned long)mem->buffer.addr,
392 (int)mem->buffer.bytes);
394 mutex_unlock(&list_mutex);
395 return 0;
398 static int snd_mem_proc_open(struct inode *inode, struct file *file)
400 return single_open(file, snd_mem_proc_read, NULL);
403 #ifdef CONFIG_PCI
404 #define gettoken(bufp) strsep(bufp, " \t\n")
406 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
407 size_t count, loff_t * ppos)
409 char buf[128];
410 char *token, *p;
412 if (count > sizeof(buf) - 1)
413 return -EINVAL;
414 if (copy_from_user(buf, buffer, count))
415 return -EFAULT;
416 buf[count] = '\0';
418 p = buf;
419 token = gettoken(&p);
420 if (! token || *token == '#')
421 return count;
422 if (strcmp(token, "add") == 0) {
423 char *endp;
424 int vendor, device, size, buffers;
425 long mask;
426 int i, alloced;
427 struct pci_dev *pci;
429 if ((token = gettoken(&p)) == NULL ||
430 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
431 (token = gettoken(&p)) == NULL ||
432 (device = simple_strtol(token, NULL, 0)) <= 0 ||
433 (token = gettoken(&p)) == NULL ||
434 (mask = simple_strtol(token, NULL, 0)) < 0 ||
435 (token = gettoken(&p)) == NULL ||
436 (size = memparse(token, &endp)) < 64*1024 ||
437 size > 16*1024*1024 /* too big */ ||
438 (token = gettoken(&p)) == NULL ||
439 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
440 buffers > 4) {
441 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
442 return count;
444 vendor &= 0xffff;
445 device &= 0xffff;
447 alloced = 0;
448 pci = NULL;
449 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
450 if (mask > 0 && mask < 0xffffffff) {
451 if (pci_set_dma_mask(pci, mask) < 0 ||
452 pci_set_consistent_dma_mask(pci, mask) < 0) {
453 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
454 pci_dev_put(pci);
455 return count;
458 for (i = 0; i < buffers; i++) {
459 struct snd_dma_buffer dmab;
460 memset(&dmab, 0, sizeof(dmab));
461 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
462 size, &dmab) < 0) {
463 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
464 pci_dev_put(pci);
465 return count;
467 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
469 alloced++;
471 if (! alloced) {
472 for (i = 0; i < buffers; i++) {
473 struct snd_dma_buffer dmab;
474 memset(&dmab, 0, sizeof(dmab));
475 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
476 size, &dmab) < 0) {
477 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
478 break;
480 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
483 } else if (strcmp(token, "erase") == 0)
484 free_all_reserved_pages();
485 else
486 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
487 return count;
489 #endif /* CONFIG_PCI */
491 static const struct file_operations snd_mem_proc_fops = {
492 .owner = THIS_MODULE,
493 .open = snd_mem_proc_open,
494 .read = seq_read,
495 #ifdef CONFIG_PCI
496 .write = snd_mem_proc_write,
497 #endif
498 .llseek = seq_lseek,
499 .release = single_release,
502 #endif /* CONFIG_PROC_FS */
505 * module entry
508 static int __init snd_mem_init(void)
510 #ifdef CONFIG_PROC_FS
511 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
512 &snd_mem_proc_fops);
513 #endif
514 return 0;
517 static void __exit snd_mem_exit(void)
519 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
520 free_all_reserved_pages();
521 if (snd_allocated_pages > 0)
522 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
526 module_init(snd_mem_init)
527 module_exit(snd_mem_exit)
531 * exports
533 EXPORT_SYMBOL(snd_dma_alloc_pages);
534 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
535 EXPORT_SYMBOL(snd_dma_free_pages);
537 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
538 EXPORT_SYMBOL(snd_dma_reserve_buf);
540 EXPORT_SYMBOL(snd_malloc_pages);
541 EXPORT_SYMBOL(snd_free_pages);