cs4231: Use SNDRV_DMA_TYPE_DEV instead of SNDRV_DMA_TYPE_SBUS.
[linux-2.6-block.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
49void *snd_malloc_sgbuf_pages(struct device *device,
50 size_t size, struct snd_dma_buffer *dmab,
51 size_t *res_size);
52int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
53
54/*
55 */
56
1a60d4c5 57static DEFINE_MUTEX(list_mutex);
1da177e4
LT
58static LIST_HEAD(mem_list_head);
59
60/* buffer preservation list */
61struct snd_mem_list {
62 struct snd_dma_buffer buffer;
63 unsigned int id;
64 struct list_head list;
65};
66
67/* id for pre-allocated buffers */
68#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
69
70#ifdef CONFIG_SND_DEBUG
71#define __ASTRING__(x) #x
72#define snd_assert(expr, args...) do {\
73 if (!(expr)) {\
74 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
75 args;\
76 }\
77} while (0)
78#else
79#define snd_assert(expr, args...) /**/
80#endif
81
1da177e4
LT
82/*
83 *
84 * Generic memory allocators
85 *
86 */
87
88static long snd_allocated_pages; /* holding the number of allocated pages */
89
90static inline void inc_snd_pages(int order)
91{
92 snd_allocated_pages += 1 << order;
93}
94
95static inline void dec_snd_pages(int order)
96{
97 snd_allocated_pages -= 1 << order;
98}
99
1da177e4
LT
100/**
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
104 *
105 * Allocates the physically contiguous pages with the given size.
106 *
107 * Returns the pointer of the buffer, or NULL if no enoguh memory.
108 */
1ef64e67 109void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
1da177e4
LT
110{
111 int pg;
112 void *res;
113
114 snd_assert(size > 0, return NULL);
115 snd_assert(gfp_flags != 0, return NULL);
f3d48f03 116 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
1da177e4 117 pg = get_order(size);
2ba8c15c 118 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
1da177e4 119 inc_snd_pages(pg);
1da177e4
LT
120 return res;
121}
122
123/**
124 * snd_free_pages - release the pages
125 * @ptr: the buffer pointer to release
126 * @size: the allocated buffer size
127 *
128 * Releases the buffer allocated via snd_malloc_pages().
129 */
130void snd_free_pages(void *ptr, size_t size)
131{
132 int pg;
133
134 if (ptr == NULL)
135 return;
136 pg = get_order(size);
137 dec_snd_pages(pg);
1da177e4
LT
138 free_pages((unsigned long) ptr, pg);
139}
140
141/*
142 *
143 * Bus-specific memory allocators
144 *
145 */
146
8f11551b 147#ifdef CONFIG_HAS_DMA
1da177e4
LT
148/* allocate the coherent DMA pages */
149static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
150{
151 int pg;
152 void *res;
1ef64e67 153 gfp_t gfp_flags;
1da177e4
LT
154
155 snd_assert(size > 0, return NULL);
156 snd_assert(dma != NULL, return NULL);
157 pg = get_order(size);
158 gfp_flags = GFP_KERNEL
f3d48f03 159 | __GFP_COMP /* compound page lets parts be mapped */
1da177e4
LT
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);
2ba8c15c 163 if (res != NULL)
1da177e4 164 inc_snd_pages(pg);
1da177e4
LT
165
166 return res;
167}
168
169/* free the coherent DMA pages */
170static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
171 dma_addr_t dma)
172{
173 int pg;
174
175 if (ptr == NULL)
176 return;
177 pg = get_order(size);
178 dec_snd_pages(pg);
1da177e4
LT
179 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
180}
8f11551b 181#endif /* CONFIG_HAS_DMA */
1da177e4
LT
182
183#ifdef CONFIG_SBUS
184
185static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
186 dma_addr_t *dma_addr)
187{
188 struct sbus_dev *sdev = (struct sbus_dev *)dev;
189 int pg;
190 void *res;
191
192 snd_assert(size > 0, return NULL);
193 snd_assert(dma_addr != NULL, return NULL);
194 pg = get_order(size);
738f2b7b
DM
195 res = dma_alloc_coherent(&sdev->ofdev.dev, PAGE_SIZE * (1 << pg),
196 dma_addr, GFP_ATOMIC);
1da177e4
LT
197 if (res != NULL)
198 inc_snd_pages(pg);
199 return res;
200}
201
202static void snd_free_sbus_pages(struct device *dev, size_t size,
203 void *ptr, dma_addr_t dma_addr)
204{
205 struct sbus_dev *sdev = (struct sbus_dev *)dev;
206 int pg;
207
208 if (ptr == NULL)
209 return;
210 pg = get_order(size);
211 dec_snd_pages(pg);
738f2b7b
DM
212 dma_free_coherent(&sdev->ofdev.dev, PAGE_SIZE * (1 << pg),
213 ptr, dma_addr);
1da177e4
LT
214}
215
216#endif /* CONFIG_SBUS */
217
218/*
219 *
220 * ALSA generic memory management
221 *
222 */
223
224
225/**
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
231 *
232 * Calls the memory-allocator function for the corresponding
233 * buffer type.
234 *
235 * Returns zero if the buffer with the given size is allocated successfuly,
236 * other a negative value at error.
237 */
238int snd_dma_alloc_pages(int type, struct device *device, size_t size,
239 struct snd_dma_buffer *dmab)
240{
241 snd_assert(size > 0, return -ENXIO);
242 snd_assert(dmab != NULL, return -ENXIO);
243
244 dmab->dev.type = type;
245 dmab->dev.dev = device;
246 dmab->bytes = 0;
247 switch (type) {
248 case SNDRV_DMA_TYPE_CONTINUOUS:
249 dmab->area = snd_malloc_pages(size, (unsigned long)device);
250 dmab->addr = 0;
251 break;
252#ifdef CONFIG_SBUS
253 case SNDRV_DMA_TYPE_SBUS:
254 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
255 break;
256#endif
8f11551b 257#ifdef CONFIG_HAS_DMA
1da177e4
LT
258 case SNDRV_DMA_TYPE_DEV:
259 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
260 break;
261 case SNDRV_DMA_TYPE_DEV_SG:
262 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
263 break;
8f11551b 264#endif
1da177e4
LT
265 default:
266 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
267 dmab->area = NULL;
268 dmab->addr = 0;
269 return -ENXIO;
270 }
271 if (! dmab->area)
272 return -ENOMEM;
273 dmab->bytes = size;
274 return 0;
275}
276
277/**
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
283 *
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
287 * res_size argument.
288 *
289 * Returns zero if the buffer with the given size is allocated successfuly,
290 * other a negative value at error.
291 */
292int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
293 struct snd_dma_buffer *dmab)
294{
295 int err;
296
297 snd_assert(size > 0, return -ENXIO);
298 snd_assert(dmab != NULL, return -ENXIO);
299
300 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
301 if (err != -ENOMEM)
302 return err;
303 size >>= 1;
304 if (size <= PAGE_SIZE)
305 return -ENOMEM;
306 }
307 if (! dmab->area)
308 return -ENOMEM;
309 return 0;
310}
311
312
313/**
314 * snd_dma_free_pages - release the allocated buffer
315 * @dmab: the buffer allocation record to release
316 *
317 * Releases the allocated buffer via snd_dma_alloc_pages().
318 */
319void snd_dma_free_pages(struct snd_dma_buffer *dmab)
320{
321 switch (dmab->dev.type) {
322 case SNDRV_DMA_TYPE_CONTINUOUS:
323 snd_free_pages(dmab->area, dmab->bytes);
324 break;
325#ifdef CONFIG_SBUS
326 case SNDRV_DMA_TYPE_SBUS:
327 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
328 break;
329#endif
8f11551b 330#ifdef CONFIG_HAS_DMA
1da177e4
LT
331 case SNDRV_DMA_TYPE_DEV:
332 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
333 break;
334 case SNDRV_DMA_TYPE_DEV_SG:
335 snd_free_sgbuf_pages(dmab);
336 break;
8f11551b 337#endif
1da177e4
LT
338 default:
339 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
340 }
341}
342
343
344/**
345 * snd_dma_get_reserved - get the reserved buffer for the given device
346 * @dmab: the buffer allocation record to store
347 * @id: the buffer id
348 *
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.
351 *
352 * Returns the size of buffer if the buffer is found, or zero if not found.
353 */
354size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
355{
1da177e4
LT
356 struct snd_mem_list *mem;
357
358 snd_assert(dmab, return 0);
359
1a60d4c5 360 mutex_lock(&list_mutex);
9244b2c3 361 list_for_each_entry(mem, &mem_list_head, list) {
1da177e4 362 if (mem->id == id &&
b6a96915
TI
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;
9244b2c3 366 list_del(&mem->list);
1da177e4 367 *dmab = mem->buffer;
b6a96915
TI
368 if (dmab->dev.dev == NULL)
369 dmab->dev.dev = dev;
1da177e4 370 kfree(mem);
1a60d4c5 371 mutex_unlock(&list_mutex);
1da177e4
LT
372 return dmab->bytes;
373 }
374 }
1a60d4c5 375 mutex_unlock(&list_mutex);
1da177e4
LT
376 return 0;
377}
378
379/**
380 * snd_dma_reserve_buf - reserve the buffer
381 * @dmab: the buffer to reserve
382 * @id: the buffer id
383 *
384 * Reserves the given buffer as a reserved buffer.
385 *
386 * Returns zero if successful, or a negative code at error.
387 */
388int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
389{
390 struct snd_mem_list *mem;
391
392 snd_assert(dmab, return -EINVAL);
393 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
394 if (! mem)
395 return -ENOMEM;
1a60d4c5 396 mutex_lock(&list_mutex);
1da177e4
LT
397 mem->buffer = *dmab;
398 mem->id = id;
399 list_add_tail(&mem->list, &mem_list_head);
1a60d4c5 400 mutex_unlock(&list_mutex);
1da177e4
LT
401 return 0;
402}
403
404/*
405 * purge all reserved buffers
406 */
407static void free_all_reserved_pages(void)
408{
409 struct list_head *p;
410 struct snd_mem_list *mem;
411
1a60d4c5 412 mutex_lock(&list_mutex);
1da177e4
LT
413 while (! list_empty(&mem_list_head)) {
414 p = mem_list_head.next;
415 mem = list_entry(p, struct snd_mem_list, list);
416 list_del(p);
417 snd_dma_free_pages(&mem->buffer);
418 kfree(mem);
419 }
1a60d4c5 420 mutex_unlock(&list_mutex);
1da177e4
LT
421}
422
423
1da177e4
LT
424#ifdef CONFIG_PROC_FS
425/*
426 * proc file interface
427 */
b6a96915 428#define SND_MEM_PROC_FILE "driver/snd-page-alloc"
a53fc188 429static struct proc_dir_entry *snd_mem_proc;
b6a96915 430
ccec6e2c 431static int snd_mem_proc_read(struct seq_file *seq, void *offset)
1da177e4 432{
1da177e4 433 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
1da177e4
LT
434 struct snd_mem_list *mem;
435 int devno;
436 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
437
1a60d4c5 438 mutex_lock(&list_mutex);
ccec6e2c
TI
439 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
440 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
1da177e4 441 devno = 0;
9244b2c3 442 list_for_each_entry(mem, &mem_list_head, list) {
1da177e4 443 devno++;
ccec6e2c
TI
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);
1da177e4 449 }
1a60d4c5 450 mutex_unlock(&list_mutex);
ccec6e2c
TI
451 return 0;
452}
453
454static int snd_mem_proc_open(struct inode *inode, struct file *file)
455{
456 return single_open(file, snd_mem_proc_read, NULL);
1da177e4 457}
b6a96915
TI
458
459/* FIXME: for pci only - other bus? */
460#ifdef CONFIG_PCI
461#define gettoken(bufp) strsep(bufp, " \t\n")
462
ccec6e2c
TI
463static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
464 size_t count, loff_t * ppos)
b6a96915
TI
465{
466 char buf[128];
467 char *token, *p;
468
ccec6e2c
TI
469 if (count > sizeof(buf) - 1)
470 return -EINVAL;
b6a96915
TI
471 if (copy_from_user(buf, buffer, count))
472 return -EFAULT;
ccec6e2c 473 buf[count] = '\0';
b6a96915
TI
474
475 p = buf;
476 token = gettoken(&p);
477 if (! token || *token == '#')
ccec6e2c 478 return count;
b6a96915
TI
479 if (strcmp(token, "add") == 0) {
480 char *endp;
481 int vendor, device, size, buffers;
482 long mask;
483 int i, alloced;
484 struct pci_dev *pci;
485
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 ||
497 buffers > 4) {
498 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
ccec6e2c 499 return count;
b6a96915
TI
500 }
501 vendor &= 0xffff;
502 device &= 0xffff;
503
504 alloced = 0;
505 pci = NULL;
0dd119f7 506 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
b6a96915
TI
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);
df1deb67 511 pci_dev_put(pci);
ccec6e2c 512 return count;
b6a96915
TI
513 }
514 }
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),
519 size, &dmab) < 0) {
520 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
0dd119f7 521 pci_dev_put(pci);
ccec6e2c 522 return count;
b6a96915
TI
523 }
524 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
525 }
526 alloced++;
527 }
528 if (! alloced) {
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!
534 */
535 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
536 size, &dmab) < 0) {
537 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
538 break;
539 }
540 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
541 }
542 }
543 } else if (strcmp(token, "erase") == 0)
544 /* FIXME: need for releasing each buffer chunk? */
545 free_all_reserved_pages();
546 else
547 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
ccec6e2c 548 return count;
b6a96915
TI
549}
550#endif /* CONFIG_PCI */
ccec6e2c
TI
551
552static const struct file_operations snd_mem_proc_fops = {
553 .owner = THIS_MODULE,
554 .open = snd_mem_proc_open,
555 .read = seq_read,
556#ifdef CONFIG_PCI
557 .write = snd_mem_proc_write,
558#endif
559 .llseek = seq_lseek,
560 .release = single_release,
561};
562
1da177e4
LT
563#endif /* CONFIG_PROC_FS */
564
565/*
566 * module entry
567 */
568
569static int __init snd_mem_init(void)
570{
571#ifdef CONFIG_PROC_FS
7bf4e6d3
DL
572 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
573 &snd_mem_proc_fops);
1da177e4 574#endif
1da177e4
LT
575 return 0;
576}
577
578static void __exit snd_mem_exit(void)
579{
e0be4d32 580 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
1da177e4
LT
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);
584}
585
586
587module_init(snd_mem_init)
588module_exit(snd_mem_exit)
589
590
591/*
592 * exports
593 */
594EXPORT_SYMBOL(snd_dma_alloc_pages);
595EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
596EXPORT_SYMBOL(snd_dma_free_pages);
597
598EXPORT_SYMBOL(snd_dma_get_reserved_buf);
599EXPORT_SYMBOL(snd_dma_reserve_buf);
600
601EXPORT_SYMBOL(snd_malloc_pages);
602EXPORT_SYMBOL(snd_free_pages);