ALSA: Clean up SG-buffer helper functions and macros
[deliverable/linux.git] / sound / core / memalloc.c
CommitLineData
1da177e4 1/*
c1017a4c 2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
3 * Takashi Iwai <tiwai@suse.de>
4 *
5 * Generic memory allocators
6 *
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
1da177e4
LT
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>
ccec6e2c 30#include <linux/seq_file.h>
b6a96915 31#include <asm/uaccess.h>
1da177e4
LT
32#include <linux/dma-mapping.h>
33#include <linux/moduleparam.h>
1a60d4c5 34#include <linux/mutex.h>
1da177e4
LT
35#include <sound/memalloc.h>
36#ifdef CONFIG_SBUS
37#include <asm/sbus.h>
38#endif
39
40
c1017a4c 41MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
42MODULE_DESCRIPTION("Memory allocator for ALSA system.");
43MODULE_LICENSE("GPL");
44
45
1da177e4
LT
46/*
47 */
48
1a60d4c5 49static DEFINE_MUTEX(list_mutex);
1da177e4
LT
50static LIST_HEAD(mem_list_head);
51
52/* buffer preservation list */
53struct snd_mem_list {
54 struct snd_dma_buffer buffer;
55 unsigned int id;
56 struct list_head list;
57};
58
59/* id for pre-allocated buffers */
60#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
61
1da177e4
LT
62/*
63 *
64 * Generic memory allocators
65 *
66 */
67
68static long snd_allocated_pages; /* holding the number of allocated pages */
69
70static inline void inc_snd_pages(int order)
71{
72 snd_allocated_pages += 1 << order;
73}
74
75static inline void dec_snd_pages(int order)
76{
77 snd_allocated_pages -= 1 << order;
78}
79
1da177e4
LT
80/**
81 * snd_malloc_pages - allocate pages with the given size
82 * @size: the size to allocate in bytes
83 * @gfp_flags: the allocation conditions, GFP_XXX
84 *
85 * Allocates the physically contiguous pages with the given size.
86 *
87 * Returns the pointer of the buffer, or NULL if no enoguh memory.
88 */
1ef64e67 89void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
1da177e4
LT
90{
91 int pg;
92 void *res;
93
7eaa943c
TI
94 if (WARN_ON(!size))
95 return NULL;
96 if (WARN_ON(!gfp_flags))
97 return NULL;
f3d48f03 98 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
1da177e4 99 pg = get_order(size);
2ba8c15c 100 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
1da177e4 101 inc_snd_pages(pg);
1da177e4
LT
102 return res;
103}
104
105/**
106 * snd_free_pages - release the pages
107 * @ptr: the buffer pointer to release
108 * @size: the allocated buffer size
109 *
110 * Releases the buffer allocated via snd_malloc_pages().
111 */
112void snd_free_pages(void *ptr, size_t size)
113{
114 int pg;
115
116 if (ptr == NULL)
117 return;
118 pg = get_order(size);
119 dec_snd_pages(pg);
1da177e4
LT
120 free_pages((unsigned long) ptr, pg);
121}
122
123/*
124 *
125 * Bus-specific memory allocators
126 *
127 */
128
8f11551b 129#ifdef CONFIG_HAS_DMA
1da177e4
LT
130/* allocate the coherent DMA pages */
131static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
132{
133 int pg;
134 void *res;
1ef64e67 135 gfp_t gfp_flags;
1da177e4 136
7eaa943c
TI
137 if (WARN_ON(!dma))
138 return NULL;
1da177e4
LT
139 pg = get_order(size);
140 gfp_flags = GFP_KERNEL
f3d48f03 141 | __GFP_COMP /* compound page lets parts be mapped */
1da177e4
LT
142 | __GFP_NORETRY /* don't trigger OOM-killer */
143 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
144 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
2ba8c15c 145 if (res != NULL)
1da177e4 146 inc_snd_pages(pg);
1da177e4
LT
147
148 return res;
149}
150
151/* free the coherent DMA pages */
152static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
153 dma_addr_t dma)
154{
155 int pg;
156
157 if (ptr == NULL)
158 return;
159 pg = get_order(size);
160 dec_snd_pages(pg);
1da177e4
LT
161 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
162}
8f11551b 163#endif /* CONFIG_HAS_DMA */
1da177e4
LT
164
165#ifdef CONFIG_SBUS
166
167static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
168 dma_addr_t *dma_addr)
169{
170 struct sbus_dev *sdev = (struct sbus_dev *)dev;
171 int pg;
172 void *res;
173
7eaa943c
TI
174 if (WARN_ON(!dma_addr))
175 return NULL;
1da177e4
LT
176 pg = get_order(size);
177 res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
178 if (res != NULL)
179 inc_snd_pages(pg);
180 return res;
181}
182
183static void snd_free_sbus_pages(struct device *dev, size_t size,
184 void *ptr, dma_addr_t dma_addr)
185{
186 struct sbus_dev *sdev = (struct sbus_dev *)dev;
187 int pg;
188
189 if (ptr == NULL)
190 return;
191 pg = get_order(size);
192 dec_snd_pages(pg);
193 sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
194}
195
196#endif /* CONFIG_SBUS */
197
198/*
199 *
200 * ALSA generic memory management
201 *
202 */
203
204
205/**
206 * snd_dma_alloc_pages - allocate the buffer area according to the given type
207 * @type: the DMA buffer type
208 * @device: the device pointer
209 * @size: the buffer size to allocate
210 * @dmab: buffer allocation record to store the allocated data
211 *
212 * Calls the memory-allocator function for the corresponding
213 * buffer type.
214 *
215 * Returns zero if the buffer with the given size is allocated successfuly,
216 * other a negative value at error.
217 */
218int snd_dma_alloc_pages(int type, struct device *device, size_t size,
219 struct snd_dma_buffer *dmab)
220{
7eaa943c
TI
221 if (WARN_ON(!size))
222 return -ENXIO;
223 if (WARN_ON(!dmab))
224 return -ENXIO;
1da177e4
LT
225
226 dmab->dev.type = type;
227 dmab->dev.dev = device;
228 dmab->bytes = 0;
229 switch (type) {
230 case SNDRV_DMA_TYPE_CONTINUOUS:
231 dmab->area = snd_malloc_pages(size, (unsigned long)device);
232 dmab->addr = 0;
233 break;
234#ifdef CONFIG_SBUS
235 case SNDRV_DMA_TYPE_SBUS:
236 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
237 break;
238#endif
8f11551b 239#ifdef CONFIG_HAS_DMA
1da177e4
LT
240 case SNDRV_DMA_TYPE_DEV:
241 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
242 break;
243 case SNDRV_DMA_TYPE_DEV_SG:
244 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
245 break;
8f11551b 246#endif
1da177e4
LT
247 default:
248 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
249 dmab->area = NULL;
250 dmab->addr = 0;
251 return -ENXIO;
252 }
253 if (! dmab->area)
254 return -ENOMEM;
255 dmab->bytes = size;
256 return 0;
257}
258
259/**
260 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
261 * @type: the DMA buffer type
262 * @device: the device pointer
263 * @size: the buffer size to allocate
264 * @dmab: buffer allocation record to store the allocated data
265 *
266 * Calls the memory-allocator function for the corresponding
267 * buffer type. When no space is left, this function reduces the size and
268 * tries to allocate again. The size actually allocated is stored in
269 * res_size argument.
270 *
271 * Returns zero if the buffer with the given size is allocated successfuly,
272 * other a negative value at error.
273 */
274int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
275 struct snd_dma_buffer *dmab)
276{
277 int err;
278
1da177e4
LT
279 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
280 if (err != -ENOMEM)
281 return err;
282 size >>= 1;
283 if (size <= PAGE_SIZE)
284 return -ENOMEM;
285 }
286 if (! dmab->area)
287 return -ENOMEM;
288 return 0;
289}
290
291
292/**
293 * snd_dma_free_pages - release the allocated buffer
294 * @dmab: the buffer allocation record to release
295 *
296 * Releases the allocated buffer via snd_dma_alloc_pages().
297 */
298void snd_dma_free_pages(struct snd_dma_buffer *dmab)
299{
300 switch (dmab->dev.type) {
301 case SNDRV_DMA_TYPE_CONTINUOUS:
302 snd_free_pages(dmab->area, dmab->bytes);
303 break;
304#ifdef CONFIG_SBUS
305 case SNDRV_DMA_TYPE_SBUS:
306 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
307 break;
308#endif
8f11551b 309#ifdef CONFIG_HAS_DMA
1da177e4
LT
310 case SNDRV_DMA_TYPE_DEV:
311 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
312 break;
313 case SNDRV_DMA_TYPE_DEV_SG:
314 snd_free_sgbuf_pages(dmab);
315 break;
8f11551b 316#endif
1da177e4
LT
317 default:
318 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
319 }
320}
321
322
323/**
324 * snd_dma_get_reserved - get the reserved buffer for the given device
325 * @dmab: the buffer allocation record to store
326 * @id: the buffer id
327 *
328 * Looks for the reserved-buffer list and re-uses if the same buffer
329 * is found in the list. When the buffer is found, it's removed from the free list.
330 *
331 * Returns the size of buffer if the buffer is found, or zero if not found.
332 */
333size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
334{
1da177e4
LT
335 struct snd_mem_list *mem;
336
7eaa943c
TI
337 if (WARN_ON(!dmab))
338 return 0;
1da177e4 339
1a60d4c5 340 mutex_lock(&list_mutex);
9244b2c3 341 list_for_each_entry(mem, &mem_list_head, list) {
1da177e4 342 if (mem->id == id &&
b6a96915
TI
343 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
344 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
345 struct device *dev = dmab->dev.dev;
9244b2c3 346 list_del(&mem->list);
1da177e4 347 *dmab = mem->buffer;
b6a96915
TI
348 if (dmab->dev.dev == NULL)
349 dmab->dev.dev = dev;
1da177e4 350 kfree(mem);
1a60d4c5 351 mutex_unlock(&list_mutex);
1da177e4
LT
352 return dmab->bytes;
353 }
354 }
1a60d4c5 355 mutex_unlock(&list_mutex);
1da177e4
LT
356 return 0;
357}
358
359/**
360 * snd_dma_reserve_buf - reserve the buffer
361 * @dmab: the buffer to reserve
362 * @id: the buffer id
363 *
364 * Reserves the given buffer as a reserved buffer.
365 *
366 * Returns zero if successful, or a negative code at error.
367 */
368int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
369{
370 struct snd_mem_list *mem;
371
7eaa943c
TI
372 if (WARN_ON(!dmab))
373 return -EINVAL;
1da177e4
LT
374 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
375 if (! mem)
376 return -ENOMEM;
1a60d4c5 377 mutex_lock(&list_mutex);
1da177e4
LT
378 mem->buffer = *dmab;
379 mem->id = id;
380 list_add_tail(&mem->list, &mem_list_head);
1a60d4c5 381 mutex_unlock(&list_mutex);
1da177e4
LT
382 return 0;
383}
384
385/*
386 * purge all reserved buffers
387 */
388static void free_all_reserved_pages(void)
389{
390 struct list_head *p;
391 struct snd_mem_list *mem;
392
1a60d4c5 393 mutex_lock(&list_mutex);
1da177e4
LT
394 while (! list_empty(&mem_list_head)) {
395 p = mem_list_head.next;
396 mem = list_entry(p, struct snd_mem_list, list);
397 list_del(p);
398 snd_dma_free_pages(&mem->buffer);
399 kfree(mem);
400 }
1a60d4c5 401 mutex_unlock(&list_mutex);
1da177e4
LT
402}
403
404
1da177e4
LT
405#ifdef CONFIG_PROC_FS
406/*
407 * proc file interface
408 */
b6a96915 409#define SND_MEM_PROC_FILE "driver/snd-page-alloc"
a53fc188 410static struct proc_dir_entry *snd_mem_proc;
b6a96915 411
ccec6e2c 412static int snd_mem_proc_read(struct seq_file *seq, void *offset)
1da177e4 413{
1da177e4 414 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
1da177e4
LT
415 struct snd_mem_list *mem;
416 int devno;
417 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
418
1a60d4c5 419 mutex_lock(&list_mutex);
ccec6e2c
TI
420 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
421 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
1da177e4 422 devno = 0;
9244b2c3 423 list_for_each_entry(mem, &mem_list_head, list) {
1da177e4 424 devno++;
ccec6e2c
TI
425 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
426 devno, mem->id, types[mem->buffer.dev.type]);
427 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
428 (unsigned long)mem->buffer.addr,
429 (int)mem->buffer.bytes);
1da177e4 430 }
1a60d4c5 431 mutex_unlock(&list_mutex);
ccec6e2c
TI
432 return 0;
433}
434
435static int snd_mem_proc_open(struct inode *inode, struct file *file)
436{
437 return single_open(file, snd_mem_proc_read, NULL);
1da177e4 438}
b6a96915
TI
439
440/* FIXME: for pci only - other bus? */
441#ifdef CONFIG_PCI
442#define gettoken(bufp) strsep(bufp, " \t\n")
443
ccec6e2c
TI
444static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
445 size_t count, loff_t * ppos)
b6a96915
TI
446{
447 char buf[128];
448 char *token, *p;
449
ccec6e2c
TI
450 if (count > sizeof(buf) - 1)
451 return -EINVAL;
b6a96915
TI
452 if (copy_from_user(buf, buffer, count))
453 return -EFAULT;
ccec6e2c 454 buf[count] = '\0';
b6a96915
TI
455
456 p = buf;
457 token = gettoken(&p);
458 if (! token || *token == '#')
ccec6e2c 459 return count;
b6a96915
TI
460 if (strcmp(token, "add") == 0) {
461 char *endp;
462 int vendor, device, size, buffers;
463 long mask;
464 int i, alloced;
465 struct pci_dev *pci;
466
467 if ((token = gettoken(&p)) == NULL ||
468 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
469 (token = gettoken(&p)) == NULL ||
470 (device = simple_strtol(token, NULL, 0)) <= 0 ||
471 (token = gettoken(&p)) == NULL ||
472 (mask = simple_strtol(token, NULL, 0)) < 0 ||
473 (token = gettoken(&p)) == NULL ||
474 (size = memparse(token, &endp)) < 64*1024 ||
475 size > 16*1024*1024 /* too big */ ||
476 (token = gettoken(&p)) == NULL ||
477 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
478 buffers > 4) {
479 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
ccec6e2c 480 return count;
b6a96915
TI
481 }
482 vendor &= 0xffff;
483 device &= 0xffff;
484
485 alloced = 0;
486 pci = NULL;
0dd119f7 487 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
b6a96915
TI
488 if (mask > 0 && mask < 0xffffffff) {
489 if (pci_set_dma_mask(pci, mask) < 0 ||
490 pci_set_consistent_dma_mask(pci, mask) < 0) {
491 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
df1deb67 492 pci_dev_put(pci);
ccec6e2c 493 return count;
b6a96915
TI
494 }
495 }
496 for (i = 0; i < buffers; i++) {
497 struct snd_dma_buffer dmab;
498 memset(&dmab, 0, sizeof(dmab));
499 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
500 size, &dmab) < 0) {
501 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
0dd119f7 502 pci_dev_put(pci);
ccec6e2c 503 return count;
b6a96915
TI
504 }
505 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
506 }
507 alloced++;
508 }
509 if (! alloced) {
510 for (i = 0; i < buffers; i++) {
511 struct snd_dma_buffer dmab;
512 memset(&dmab, 0, sizeof(dmab));
513 /* FIXME: We can allocate only in ZONE_DMA
514 * without a device pointer!
515 */
516 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
517 size, &dmab) < 0) {
518 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
519 break;
520 }
521 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
522 }
523 }
524 } else if (strcmp(token, "erase") == 0)
525 /* FIXME: need for releasing each buffer chunk? */
526 free_all_reserved_pages();
527 else
528 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
ccec6e2c 529 return count;
b6a96915
TI
530}
531#endif /* CONFIG_PCI */
ccec6e2c
TI
532
533static const struct file_operations snd_mem_proc_fops = {
534 .owner = THIS_MODULE,
535 .open = snd_mem_proc_open,
536 .read = seq_read,
537#ifdef CONFIG_PCI
538 .write = snd_mem_proc_write,
539#endif
540 .llseek = seq_lseek,
541 .release = single_release,
542};
543
1da177e4
LT
544#endif /* CONFIG_PROC_FS */
545
546/*
547 * module entry
548 */
549
550static int __init snd_mem_init(void)
551{
552#ifdef CONFIG_PROC_FS
7bf4e6d3
DL
553 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
554 &snd_mem_proc_fops);
1da177e4 555#endif
1da177e4
LT
556 return 0;
557}
558
559static void __exit snd_mem_exit(void)
560{
e0be4d32 561 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
1da177e4
LT
562 free_all_reserved_pages();
563 if (snd_allocated_pages > 0)
564 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
565}
566
567
568module_init(snd_mem_init)
569module_exit(snd_mem_exit)
570
571
572/*
573 * exports
574 */
575EXPORT_SYMBOL(snd_dma_alloc_pages);
576EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
577EXPORT_SYMBOL(snd_dma_free_pages);
578
579EXPORT_SYMBOL(snd_dma_get_reserved_buf);
580EXPORT_SYMBOL(snd_dma_reserve_buf);
581
582EXPORT_SYMBOL(snd_malloc_pages);
583EXPORT_SYMBOL(snd_free_pages);
This page took 0.397028 seconds and 5 git commands to generate.