sparc: Make SBUS DMA interfaces take struct device.
[safe/jmp/linux-2.6] / sound / core / memalloc.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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
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>
36 #ifdef CONFIG_SBUS
37 #include <asm/sbus.h>
38 #endif
39
40
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");
44
45
46 /*
47  */
48
49 void *snd_malloc_sgbuf_pages(struct device *device,
50                              size_t size, struct snd_dma_buffer *dmab,
51                              size_t *res_size);
52 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
53
54 /*
55  */
56
57 static DEFINE_MUTEX(list_mutex);
58 static LIST_HEAD(mem_list_head);
59
60 /* buffer preservation list */
61 struct 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
82 /*
83  *
84  *  Generic memory allocators
85  *
86  */
87
88 static long snd_allocated_pages; /* holding the number of allocated pages */
89
90 static inline void inc_snd_pages(int order)
91 {
92         snd_allocated_pages += 1 << order;
93 }
94
95 static inline void dec_snd_pages(int order)
96 {
97         snd_allocated_pages -= 1 << order;
98 }
99
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  */
109 void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
110 {
111         int pg;
112         void *res;
113
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)
119                 inc_snd_pages(pg);
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  */
130 void 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);
138         free_pages((unsigned long) ptr, pg);
139 }
140
141 /*
142  *
143  *  Bus-specific memory allocators
144  *
145  */
146
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)
150 {
151         int pg;
152         void *res;
153         gfp_t gfp_flags;
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
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);
163         if (res != NULL)
164                 inc_snd_pages(pg);
165
166         return res;
167 }
168
169 /* free the coherent DMA pages */
170 static 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);
179         dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
180 }
181 #endif /* CONFIG_HAS_DMA */
182
183 #ifdef CONFIG_SBUS
184
185 static 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);
195         res = sbus_alloc_consistent(&sdev->ofdev.dev, PAGE_SIZE * (1 << pg),
196                                     dma_addr);
197         if (res != NULL)
198                 inc_snd_pages(pg);
199         return res;
200 }
201
202 static 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);
212         sbus_free_consistent(&sdev->ofdev.dev, PAGE_SIZE * (1 << pg),
213                              ptr, dma_addr);
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  */
238 int 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
257 #ifdef CONFIG_HAS_DMA
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;
264 #endif
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  */
292 int 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  */
319 void 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
330 #ifdef CONFIG_HAS_DMA
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;
337 #endif
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  */
354 size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
355 {
356         struct snd_mem_list *mem;
357
358         snd_assert(dmab, return 0);
359
360         mutex_lock(&list_mutex);
361         list_for_each_entry(mem, &mem_list_head, list) {
362                 if (mem->id == id &&
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;
366                         list_del(&mem->list);
367                         *dmab = mem->buffer;
368                         if (dmab->dev.dev == NULL)
369                                 dmab->dev.dev = dev;
370                         kfree(mem);
371                         mutex_unlock(&list_mutex);
372                         return dmab->bytes;
373                 }
374         }
375         mutex_unlock(&list_mutex);
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  */
388 int 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;
396         mutex_lock(&list_mutex);
397         mem->buffer = *dmab;
398         mem->id = id;
399         list_add_tail(&mem->list, &mem_list_head);
400         mutex_unlock(&list_mutex);
401         return 0;
402 }
403
404 /*
405  * purge all reserved buffers
406  */
407 static void free_all_reserved_pages(void)
408 {
409         struct list_head *p;
410         struct snd_mem_list *mem;
411
412         mutex_lock(&list_mutex);
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         }
420         mutex_unlock(&list_mutex);
421 }
422
423
424 #ifdef CONFIG_PROC_FS
425 /*
426  * proc file interface
427  */
428 #define SND_MEM_PROC_FILE       "driver/snd-page-alloc"
429 static struct proc_dir_entry *snd_mem_proc;
430
431 static int snd_mem_proc_read(struct seq_file *seq, void *offset)
432 {
433         long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
434         struct snd_mem_list *mem;
435         int devno;
436         static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
437
438         mutex_lock(&list_mutex);
439         seq_printf(seq, "pages  : %li bytes (%li pages per %likB)\n",
440                    pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
441         devno = 0;
442         list_for_each_entry(mem, &mem_list_head, list) {
443                 devno++;
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);
449         }
450         mutex_unlock(&list_mutex);
451         return 0;
452 }
453
454 static int snd_mem_proc_open(struct inode *inode, struct file *file)
455 {
456         return single_open(file, snd_mem_proc_read, NULL);
457 }
458
459 /* FIXME: for pci only - other bus? */
460 #ifdef CONFIG_PCI
461 #define gettoken(bufp) strsep(bufp, " \t\n")
462
463 static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
464                                   size_t count, loff_t * ppos)
465 {
466         char buf[128];
467         char *token, *p;
468
469         if (count > sizeof(buf) - 1)
470                 return -EINVAL;
471         if (copy_from_user(buf, buffer, count))
472                 return -EFAULT;
473         buf[count] = '\0';
474
475         p = buf;
476         token = gettoken(&p);
477         if (! token || *token == '#')
478                 return count;
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");
499                         return count;
500                 }
501                 vendor &= 0xffff;
502                 device &= 0xffff;
503
504                 alloced = 0;
505                 pci = NULL;
506                 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
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);
511                                         pci_dev_put(pci);
512                                         return count;
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);
521                                         pci_dev_put(pci);
522                                         return count;
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");
548         return count;
549 }
550 #endif /* CONFIG_PCI */
551
552 static 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
563 #endif /* CONFIG_PROC_FS */
564
565 /*
566  * module entry
567  */
568
569 static int __init snd_mem_init(void)
570 {
571 #ifdef CONFIG_PROC_FS
572         snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
573                                    &snd_mem_proc_fops);
574 #endif
575         return 0;
576 }
577
578 static void __exit snd_mem_exit(void)
579 {
580         remove_proc_entry(SND_MEM_PROC_FILE, NULL);
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
587 module_init(snd_mem_init)
588 module_exit(snd_mem_exit)
589
590
591 /*
592  * exports
593  */
594 EXPORT_SYMBOL(snd_dma_alloc_pages);
595 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
596 EXPORT_SYMBOL(snd_dma_free_pages);
597
598 EXPORT_SYMBOL(snd_dma_get_reserved_buf);
599 EXPORT_SYMBOL(snd_dma_reserve_buf);
600
601 EXPORT_SYMBOL(snd_malloc_pages);
602 EXPORT_SYMBOL(snd_free_pages);