commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / sound / core / memalloc.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.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/config.h>
25 #include <linux/version.h>
26 #include <linux/module.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31 #include <asm/semaphore.h>
32 #include <sound/memalloc.h>
33
34
35 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
36 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
37 MODULE_LICENSE("GPL");
38
39
40 #ifndef SNDRV_CARDS
41 #define SNDRV_CARDS     8
42 #endif
43 static int enable[8] = {[0 ... (SNDRV_CARDS-1)] = 1};
44 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
45 MODULE_PARM_DESC(enable, "Enable cards to allocate buffers.");
46
47
48 /*
49  */
50
51 static DECLARE_MUTEX(list_mutex);
52 static LIST_HEAD(mem_list_head);
53
54 /* buffer preservation list */
55 struct snd_mem_list {
56         struct snd_dma_device dev;
57         struct snd_dma_buffer buffer;
58         int used;
59         struct list_head list;
60 };
61
62 /* id for pre-allocated buffers */
63 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
64
65 #ifdef CONFIG_SND_DEBUG
66 #define __ASTRING__(x) #x
67 #define snd_assert(expr, args...) do {\
68         if (!(expr)) {\
69                 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
70                 args;\
71         }\
72 } while (0)
73 #else
74 #define snd_assert(expr, args...) /**/
75 #endif
76
77 #ifdef CONFIG_PCI
78 #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
79 #define HACK_PCI_ALLOC_CONSISTENT
80
81 /*
82  * A hack to allocate large buffers via pci_alloc_consistent()
83  *
84  * since pci_alloc_consistent always tries GFP_DMA when the requested
85  * pci memory region is below 32bit, it happens quite often that even
86  * 2 order of pages cannot be allocated.
87  *
88  * so in the following, we allocate at first without dma_mask, so that
89  * allocation will be done without GFP_DMA.  if the area doesn't match
90  * with the requested region, then realloate with the original dma_mask
91  * again.
92  */
93
94 static void *snd_pci_hack_alloc_consistent(struct pci_dev *hwdev, size_t size,
95                                     dma_addr_t *dma_handle)
96 {
97         void *ret;
98         u64 dma_mask;
99         unsigned long rmask;
100
101         if (hwdev == NULL)
102                 return pci_alloc_consistent(hwdev, size, dma_handle);
103         dma_mask = hwdev->dma_mask;
104         rmask = ~((unsigned long)dma_mask);
105         hwdev->dma_mask = 0xffffffff; /* do without masking */
106         ret = pci_alloc_consistent(hwdev, size, dma_handle);
107         hwdev->dma_mask = dma_mask; /* restore */
108         if (ret) {
109                 /* obtained address is out of range? */
110                 if (((unsigned long)*dma_handle + size - 1) & rmask) {
111                         /* reallocate with the proper mask */
112                         pci_free_consistent(hwdev, size, ret, *dma_handle);
113                         ret = pci_alloc_consistent(hwdev, size, dma_handle);
114                 }
115         } else {
116                 /* wish to success now with the proper mask... */
117                 if (dma_mask != 0xffffffff)
118                         ret = pci_alloc_consistent(hwdev, size, dma_handle);
119         }
120         return ret;
121 }
122
123 /* redefine pci_alloc_consistent for some architectures */
124 #undef pci_alloc_consistent
125 #define pci_alloc_consistent snd_pci_hack_alloc_consistent
126
127 #endif /* arch */
128 #endif /* CONFIG_PCI */
129
130
131 /*
132  * compare the two devices
133  * returns non-zero if matched.
134  */
135 static int compare_device(const struct snd_dma_device *a, const struct snd_dma_device *b, int allow_unused)
136 {
137         if (a->type != b->type)
138                 return 0;
139         if (a->id != b->id) {
140                 if (! allow_unused || (a->id != SNDRV_DMA_DEVICE_UNUSED && b->id != SNDRV_DMA_DEVICE_UNUSED))
141                         return 0;
142         }
143         switch (a->type) {
144         case SNDRV_DMA_TYPE_CONTINUOUS:
145 #ifdef CONFIG_ISA
146         case SNDRV_DMA_TYPE_ISA:
147 #endif
148                 return a->dev.flags == b->dev.flags;
149 #ifdef CONFIG_PCI
150         case SNDRV_DMA_TYPE_PCI:
151         case SNDRV_DMA_TYPE_PCI_SG:
152                 return a->dev.pci == b->dev.pci;
153 #endif
154 #ifdef CONFIG_SBUS
155         case SNDRV_DMA_TYPE_SBUS:
156                 return a->dev.sbus == b->dev.sbus;
157 #endif
158         }
159         return 0;
160 }
161
162 /**
163  * snd_dma_alloc_pages - allocate the buffer area according to the given type
164  * @dev: the buffer device info
165  * @size: the buffer size to allocate
166  * @dmab: buffer allocation record to store the allocated data
167  *
168  * Calls the memory-allocator function for the corresponding
169  * buffer type.
170  * 
171  * Returns zero if the buffer with the given size is allocated successfuly,
172  * other a negative value at error.
173  */
174 int snd_dma_alloc_pages(const struct snd_dma_device *dev, size_t size,
175                         struct snd_dma_buffer *dmab)
176 {
177         snd_assert(dev != NULL, return -ENXIO);
178         snd_assert(size > 0, return -ENXIO);
179         snd_assert(dmab != NULL, return -ENXIO);
180
181         dmab->bytes = 0;
182         switch (dev->type) {
183         case SNDRV_DMA_TYPE_CONTINUOUS:
184                 dmab->area = snd_malloc_pages(size, dev->dev.flags);
185                 dmab->addr = 0;
186                 break;
187 #ifdef CONFIG_ISA
188         case SNDRV_DMA_TYPE_ISA:
189                 dmab->area = snd_malloc_isa_pages(size, &dmab->addr);
190                 break;
191 #endif
192 #ifdef CONFIG_PCI
193         case SNDRV_DMA_TYPE_PCI:
194                 dmab->area = snd_malloc_pci_pages(dev->dev.pci, size, &dmab->addr);
195                 break;
196         case SNDRV_DMA_TYPE_PCI_SG:
197                 snd_malloc_sgbuf_pages(dev->dev.pci, size, dmab);
198                 break;
199 #endif
200 #ifdef CONFIG_SBUS
201         case SNDRV_DMA_TYPE_SBUS:
202                 dmab->area = snd_malloc_sbus_pages(dev->dev.sbus, size, &dmab->addr);
203                 break;
204 #endif
205         default:
206                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
207                 dmab->area = NULL;
208                 dmab->addr = 0;
209                 return -ENXIO;
210         }
211         if (dmab->area)
212                 dmab->bytes = size;
213         return 0;
214 }
215
216
217 /**
218  * snd_dma_free_pages - release the allocated buffer
219  * @dev: the buffer device info
220  * @dmbab: the buffer allocation record to release
221  *
222  * Releases the allocated buffer via snd_dma_alloc_pages().
223  */
224 void snd_dma_free_pages(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
225 {
226         switch (dev->type) {
227         case SNDRV_DMA_TYPE_CONTINUOUS:
228                 snd_free_pages(dmab->area, dmab->bytes);
229                 break;
230 #ifdef CONFIG_ISA
231         case SNDRV_DMA_TYPE_ISA:
232                 snd_free_isa_pages(dmab->bytes, dmab->area, dmab->addr);
233                 break;
234 #endif
235 #ifdef CONFIG_PCI
236         case SNDRV_DMA_TYPE_PCI:
237                 snd_free_pci_pages(dev->dev.pci, dmab->bytes, dmab->area, dmab->addr);
238                 break;
239         case SNDRV_DMA_TYPE_PCI_SG:
240                 snd_free_sgbuf_pages(dmab);
241                 break;
242 #endif
243 #ifdef CONFIG_SBUS
244         case SNDRV_DMA_TYPE_SBUS:
245                 snd_free_sbus_pages(dev->dev.sbus, dmab->bytes, dmab->area, dmab->addr);
246                 break;
247 #endif
248         default:
249                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
250         }
251 }
252
253
254 /*
255  * search for the device
256  */
257 static struct snd_mem_list *mem_list_find(const struct snd_dma_device *dev, int search_empty)
258 {
259         struct list_head *p;
260         struct snd_mem_list *mem;
261
262         list_for_each(p, &mem_list_head) {
263                 mem = list_entry(p, struct snd_mem_list, list);
264                 if (mem->used && search_empty)
265                         continue;
266                 if (compare_device(&mem->dev, dev, search_empty))
267                         return mem;
268         }
269         return NULL;
270 }
271
272 /**
273  * snd_dma_get_reserved - get the reserved buffer for the given device
274  * @dev: the buffer device info
275  * @dmab: the buffer allocation record to store
276  *
277  * Looks for the reserved-buffer list and re-uses if the same buffer
278  * is found in the list.  When the buffer is found, it's marked as used.
279  * For unmarking the buffer, call snd_dma_free_reserved().
280  *
281  * Returns the size of buffer if the buffer is found, or zero if not found.
282  */
283 size_t snd_dma_get_reserved(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
284 {
285         struct snd_mem_list *mem;
286
287         snd_assert(dev && dmab, return 0);
288
289         down(&list_mutex);
290         mem = mem_list_find(dev, 1);
291         if (mem) {
292                 mem->used = 1;
293                 mem->dev = *dev;
294                 *dmab = mem->buffer;
295                 up(&list_mutex);
296                 return dmab->bytes;
297         }
298         up(&list_mutex);
299         return 0;
300 }
301
302 /**
303  * snd_dma_free_reserved - unmark the reserved buffer
304  * @dev: the buffer device info
305  *
306  * Looks for the matching reserved buffer and erases the mark on it
307  * if found.
308  *
309  * Returns zero.
310  */
311 int snd_dma_free_reserved(const struct snd_dma_device *dev)
312 {
313         struct snd_mem_list *mem;
314
315         snd_assert(dev, return -EINVAL);
316         down(&list_mutex);
317         mem = mem_list_find(dev, 0);
318         if (mem)
319                 mem->used = 0;
320         up(&list_mutex);
321         return 0;
322 }
323
324 /**
325  * snd_dma_set_reserved - reserve the buffer
326  * @dev: the buffer device info
327  * @dmab: the buffer to reserve
328  *
329  * Reserves the given buffer as a reserved buffer.
330  * When an old reserved buffer already exists, the old one is released
331  * and replaced with the new one.
332  *
333  * When NULL buffer pointer or zero buffer size is given, the existing
334  * buffer is released and the entry is removed.
335  * 
336  * Returns zero if successful, or a negative code at error.
337  */
338 int snd_dma_set_reserved(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
339 {
340         struct snd_mem_list *mem;
341
342         snd_assert(dev, return -EINVAL);
343         down(&list_mutex);
344         mem = mem_list_find(dev, 0);
345         if (mem) {
346                 snd_dma_free_pages(dev, &mem->buffer);
347                 if (! dmab || ! dmab->bytes) {
348                         /* remove the entry */
349                         list_del(&mem->list);
350                         kfree(mem);
351                         up(&list_mutex);
352                         return 0;
353                 }
354         } else {
355                 if (! dmab || ! dmab->bytes) {
356                         up(&list_mutex);
357                         return 0;
358                 }
359                 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
360                 if (! mem) {
361                         up(&list_mutex);
362                         return -ENOMEM;
363                 }
364                 mem->dev = *dev;
365                 list_add(&mem->list, &mem_list_head);
366         }
367         /* store the entry */
368         mem->used = 1;
369         mem->buffer = *dmab;
370         up(&list_mutex);
371         return 0;
372 }
373
374 /*
375  * purge all reserved buffers
376  */
377 static void free_all_reserved_pages(void)
378 {
379         struct list_head *p;
380         struct snd_mem_list *mem;
381
382         down(&list_mutex);
383         while (! list_empty(&mem_list_head)) {
384                 p = mem_list_head.next;
385                 mem = list_entry(p, struct snd_mem_list, list);
386                 list_del(p);
387                 snd_dma_free_pages(&mem->dev, &mem->buffer);
388                 kfree(mem);
389         }
390         up(&list_mutex);
391 }
392
393
394 /*
395  *
396  *  Generic memory allocators
397  *
398  */
399
400 static long snd_allocated_pages; /* holding the number of allocated pages */
401
402 static void mark_pages(void *res, int order)
403 {
404         struct page *page = virt_to_page(res);
405         struct page *last_page = page + (1 << order);
406         while (page < last_page)
407                 SetPageReserved(page++);
408         snd_allocated_pages += 1 << order;
409 }
410
411 static void unmark_pages(void *res, int order)
412 {
413         struct page *page = virt_to_page(res);
414         struct page *last_page = page + (1 << order);
415         while (page < last_page)
416                 ClearPageReserved(page++);
417         snd_allocated_pages -= 1 << order;
418 }
419
420 /**
421  * snd_malloc_pages - allocate pages with the given size
422  * @size: the size to allocate in bytes
423  * @gfp_flags: the allocation conditions, GFP_XXX
424  *
425  * Allocates the physically contiguous pages with the given size.
426  *
427  * Returns the pointer of the buffer, or NULL if no enoguh memory.
428  */
429 void *snd_malloc_pages(size_t size, unsigned int gfp_flags)
430 {
431         int pg;
432         void *res;
433
434         snd_assert(size > 0, return NULL);
435         snd_assert(gfp_flags != 0, return NULL);
436         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
437         if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) {
438                 mark_pages(res, pg);
439         }
440         return res;
441 }
442
443 /**
444  * snd_malloc_pages_fallback - allocate pages with the given size with fallback
445  * @size: the requested size to allocate in bytes
446  * @gfp_flags: the allocation conditions, GFP_XXX
447  * @res_size: the pointer to store the size of buffer actually allocated
448  *
449  * Allocates the physically contiguous pages with the given request
450  * size.  When no space is left, this function reduces the size and
451  * tries to allocate again.  The size actually allocated is stored in
452  * res_size argument.
453  *
454  * Returns the pointer of the buffer, or NULL if no enoguh memory.
455  */
456 void *snd_malloc_pages_fallback(size_t size, unsigned int gfp_flags, size_t *res_size)
457 {
458         void *res;
459
460         snd_assert(size > 0, return NULL);
461         snd_assert(res_size != NULL, return NULL);
462         do {
463                 if ((res = snd_malloc_pages(size, gfp_flags)) != NULL) {
464                         *res_size = size;
465                         return res;
466                 }
467                 size >>= 1;
468         } while (size >= PAGE_SIZE);
469         return NULL;
470 }
471
472 /**
473  * snd_free_pages - release the pages
474  * @ptr: the buffer pointer to release
475  * @size: the allocated buffer size
476  *
477  * Releases the buffer allocated via snd_malloc_pages().
478  */
479 void snd_free_pages(void *ptr, size_t size)
480 {
481         int pg;
482
483         if (ptr == NULL)
484                 return;
485         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
486         unmark_pages(ptr, pg);
487         free_pages((unsigned long) ptr, pg);
488 }
489
490 #if defined(CONFIG_ISA) && ! defined(CONFIG_PCI)
491
492 /**
493  * snd_malloc_isa_pages - allocate pages for ISA bus with the given size
494  * @size: the size to allocate in bytes
495  * @dma_addr: the pointer to store the physical address of the buffer
496  *
497  * Allocates the physically contiguous pages with the given size for
498  * ISA bus.
499  *
500  * Returns the pointer of the buffer, or NULL if no enoguh memory.
501  */
502 void *snd_malloc_isa_pages(size_t size, dma_addr_t *dma_addr)
503 {
504         void *dma_area;
505         dma_area = snd_malloc_pages(size, GFP_ATOMIC|GFP_DMA);
506         *dma_addr = dma_area ? isa_virt_to_bus(dma_area) : 0UL;
507         return dma_area;
508 }
509
510 /**
511  * snd_malloc_isa_pages_fallback - allocate pages with the given size with fallback for ISA bus
512  * @size: the requested size to allocate in bytes
513  * @dma_addr: the pointer to store the physical address of the buffer
514  * @res_size: the pointer to store the size of buffer actually allocated
515  *
516  * Allocates the physically contiguous pages with the given request
517  * size for PCI bus.  When no space is left, this function reduces the size and
518  * tries to allocate again.  The size actually allocated is stored in
519  * res_size argument.
520  *
521  * Returns the pointer of the buffer, or NULL if no enoguh memory.
522  */
523 void *snd_malloc_isa_pages_fallback(size_t size,
524                                     dma_addr_t *dma_addr,
525                                     size_t *res_size)
526 {
527         void *dma_area;
528         dma_area = snd_malloc_pages_fallback(size, GFP_ATOMIC|GFP_DMA, res_size);
529         *dma_addr = dma_area ? isa_virt_to_bus(dma_area) : 0UL;
530         return dma_area;
531 }
532
533 #endif /* CONFIG_ISA && !CONFIG_PCI */
534
535 #ifdef CONFIG_PCI
536
537 /**
538  * snd_malloc_pci_pages - allocate pages for PCI bus with the given size
539  * @pci: the pci device pointer
540  * @size: the size to allocate in bytes
541  * @dma_addr: the pointer to store the physical address of the buffer
542  *
543  * Allocates the physically contiguous pages with the given size for
544  * PCI bus.
545  *
546  * Returns the pointer of the buffer, or NULL if no enoguh memory.
547  */
548 void *snd_malloc_pci_pages(struct pci_dev *pci,
549                            size_t size,
550                            dma_addr_t *dma_addr)
551 {
552         int pg;
553         void *res;
554
555         snd_assert(size > 0, return NULL);
556         snd_assert(dma_addr != NULL, return NULL);
557         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
558         res = pci_alloc_consistent(pci, PAGE_SIZE * (1 << pg), dma_addr);
559         if (res != NULL) {
560                 mark_pages(res, pg);
561         }
562         return res;
563 }
564
565 /**
566  * snd_malloc_pci_pages_fallback - allocate pages with the given size with fallback for PCI bus
567  * @pci: pci device pointer
568  * @size: the requested size to allocate in bytes
569  * @dma_addr: the pointer to store the physical address of the buffer
570  * @res_size: the pointer to store the size of buffer actually allocated
571  *
572  * Allocates the physically contiguous pages with the given request
573  * size for PCI bus.  When no space is left, this function reduces the size and
574  * tries to allocate again.  The size actually allocated is stored in
575  * res_size argument.
576  *
577  * Returns the pointer of the buffer, or NULL if no enoguh memory.
578  */
579 void *snd_malloc_pci_pages_fallback(struct pci_dev *pci,
580                                     size_t size,
581                                     dma_addr_t *dma_addr,
582                                     size_t *res_size)
583 {
584         void *res;
585
586         snd_assert(res_size != NULL, return NULL);
587         do {
588                 if ((res = snd_malloc_pci_pages(pci, size, dma_addr)) != NULL) {
589                         *res_size = size;
590                         return res;
591                 }
592                 size >>= 1;
593         } while (size >= PAGE_SIZE);
594         return NULL;
595 }
596
597 /**
598  * snd_free_pci_pages - release the pages
599  * @pci: pci device pointer
600  * @size: the allocated buffer size
601  * @ptr: the buffer pointer to release
602  * @dma_addr: the physical address of the buffer
603  *
604  * Releases the buffer allocated via snd_malloc_pci_pages().
605  */
606 void snd_free_pci_pages(struct pci_dev *pci,
607                         size_t size,
608                         void *ptr,
609                         dma_addr_t dma_addr)
610 {
611         int pg;
612
613         if (ptr == NULL)
614                 return;
615         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
616         unmark_pages(ptr, pg);
617         pci_free_consistent(pci, PAGE_SIZE * (1 << pg), ptr, dma_addr);
618 }
619
620
621 #if defined(__i386__)
622 /*
623  * on ix86, we allocate a page with GFP_KERNEL to assure the
624  * allocation.  the code is almost same with kernel/i386/pci-dma.c but
625  * it allocates only a single page and checks the validity of the
626  * page address with the given pci dma mask.
627  */
628
629 /**
630  * snd_malloc_pci_page - allocate a page in the valid pci dma mask
631  * @pci: pci device pointer
632  * @addrp: the pointer to store the physical address of the buffer
633  *
634  * Allocates a single page for the given PCI device and returns
635  * the virtual address and stores the physical address on addrp.
636  * 
637  * This function cannot be called from interrupt handlers or
638  * within spinlocks.
639  */
640 void *snd_malloc_pci_page(struct pci_dev *pci, dma_addr_t *addrp)
641 {
642         void *ptr;
643         dma_addr_t addr;
644         unsigned long rmask;
645
646         rmask = ~(unsigned long)(pci ? pci->dma_mask : 0x00ffffff);
647         ptr = (void *)__get_free_page(GFP_KERNEL);
648         if (ptr) {
649                 addr = virt_to_phys(ptr);
650                 if (((unsigned long)addr + PAGE_SIZE - 1) & rmask) {
651                         /* try to reallocate with the GFP_DMA */
652                         free_page((unsigned long)ptr);
653                         /* use GFP_ATOMIC for the DMA zone to avoid stall */
654                         ptr = (void *)__get_free_page(GFP_ATOMIC | GFP_DMA);
655                         if (ptr) /* ok, the address must be within lower 16MB... */
656                                 addr = virt_to_phys(ptr);
657                         else
658                                 addr = 0;
659                 }
660         } else
661                 addr = 0;
662         if (ptr) {
663                 memset(ptr, 0, PAGE_SIZE);
664                 mark_pages(ptr, 0);
665         }
666         *addrp = addr;
667         return ptr;
668 }
669 #else
670
671 /* on other architectures, call snd_malloc_pci_pages() helper function
672  * which uses pci_alloc_consistent().
673  */
674 void *snd_malloc_pci_page(struct pci_dev *pci, dma_addr_t *addrp)
675 {
676         return snd_malloc_pci_pages(pci, PAGE_SIZE, addrp);
677 }
678
679 #endif
680
681 #if 0 /* for kernel-doc */
682 /**
683  * snd_free_pci_page - release a page
684  * @pci: pci device pointer
685  * @ptr: the buffer pointer to release
686  * @dma_addr: the physical address of the buffer
687  *
688  * Releases the buffer allocated via snd_malloc_pci_page().
689  */
690 void snd_free_pci_page(struct pci_dev *pci, void *ptr, dma_addr_t dma_addr);
691 #endif /* for kernel-doc */
692
693 #endif /* CONFIG_PCI */
694
695 #ifdef CONFIG_SBUS
696
697 /**
698  * snd_malloc_sbus_pages - allocate pages for SBUS with the given size
699  * @sdev: sbus device pointer
700  * @size: the size to allocate in bytes
701  * @dma_addr: the pointer to store the physical address of the buffer
702  *
703  * Allocates the physically contiguous pages with the given size for
704  * SBUS.
705  *
706  * Returns the pointer of the buffer, or NULL if no enoguh memory.
707  */
708 void *snd_malloc_sbus_pages(struct sbus_dev *sdev,
709                             size_t size,
710                             dma_addr_t *dma_addr)
711 {
712         int pg;
713         void *res;
714
715         snd_assert(size > 0, return NULL);
716         snd_assert(dma_addr != NULL, return NULL);
717         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
718         res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
719         if (res != NULL) {
720                 mark_pages(res, pg);
721         }
722         return res;
723 }
724
725 /**
726  * snd_malloc_pci_pages_fallback - allocate pages with the given size with fallback for SBUS
727  * @sdev: sbus device pointer
728  * @size: the requested size to allocate in bytes
729  * @dma_addr: the pointer to store the physical address of the buffer
730  * @res_size: the pointer to store the size of buffer actually allocated
731  *
732  * Allocates the physically contiguous pages with the given request
733  * size for SBUS.  When no space is left, this function reduces the size and
734  * tries to allocate again.  The size actually allocated is stored in
735  * res_size argument.
736  *
737  * Returns the pointer of the buffer, or NULL if no enoguh memory.
738  */
739 void *snd_malloc_sbus_pages_fallback(struct sbus_dev *sdev,
740                                      size_t size,
741                                      dma_addr_t *dma_addr,
742                                      size_t *res_size)
743 {
744         void *res;
745
746         snd_assert(res_size != NULL, return NULL);
747         do {
748                 if ((res = snd_malloc_sbus_pages(sdev, size, dma_addr)) != NULL) {
749                         *res_size = size;
750                         return res;
751                 }
752                 size >>= 1;
753         } while (size >= PAGE_SIZE);
754         return NULL;
755 }
756
757 /**
758  * snd_free_sbus_pages - release the pages
759  * @sdev: sbus device pointer
760  * @size: the allocated buffer size
761  * @ptr: the buffer pointer to release
762  * @dma_addr: the physical address of the buffer
763  *
764  * Releases the buffer allocated via snd_malloc_pci_pages().
765  */
766 void snd_free_sbus_pages(struct sbus_dev *sdev,
767                          size_t size,
768                          void *ptr,
769                          dma_addr_t dma_addr)
770 {
771         int pg;
772
773         if (ptr == NULL)
774                 return;
775         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
776         unmark_pages(ptr, pg);
777         sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
778 }
779
780 #endif /* CONFIG_SBUS */
781
782
783 /*
784  * allocation of buffers for pre-defined devices
785  */
786
787 /* FIXME: for pci only - other bus? */
788 struct prealloc_dev {
789         unsigned short vendor;
790         unsigned short device;
791         unsigned long dma_mask;
792         unsigned int size;
793         unsigned int buffers;
794 };
795
796 #define HAMMERFALL_BUFFER_SIZE    (16*1024*4*(26+1))
797
798 static struct prealloc_dev prealloc_devices[] __initdata = {
799         {
800                 /* hammerfall */
801                 .vendor = 0x10ee,
802                 .device = 0x3fc4,
803                 .dma_mask = 0xffffffff,
804                 .size = HAMMERFALL_BUFFER_SIZE,
805                 .buffers = 2
806         },
807         {
808                 /* HDSP */
809                 .vendor = 0x10ee,
810                 .device = 0x3fc5,
811                 .dma_mask = 0xffffffff,
812                 .size = HAMMERFALL_BUFFER_SIZE,
813                 .buffers = 2
814         },
815         { }, /* terminator */
816 };
817
818 static void __init preallocate_cards(void)
819 {
820         struct pci_dev *pci = NULL;
821         int card;
822
823         card = 0;
824
825         while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
826                 struct prealloc_dev *dev;
827                 if (card >= SNDRV_CARDS)
828                         break;
829                 for (dev = prealloc_devices; dev->vendor; dev++) {
830                         unsigned int i;
831                         if (dev->vendor != pci->vendor || dev->device != pci->device)
832                                 continue;
833                         if (! enable[card++])
834                                 continue;
835                         
836                         if (pci_set_dma_mask(pci, dev->dma_mask) < 0) {
837                                 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", dev->dma_mask, dev->vendor, dev->device);
838                                 continue;
839                         }
840
841                         for (i = 0; i < dev->buffers; i++) {
842                                 struct snd_dma_device dma;
843                                 struct snd_dma_buffer buf;
844                                 snd_dma_device_pci(&dma, pci, SNDRV_DMA_DEVICE_UNUSED);
845                                 memset(&buf, 0, sizeof(buf));
846                                 snd_dma_alloc_pages(&dma, dev->size, &buf);
847                                 if (buf.bytes) {
848                                         if (snd_dma_set_reserved(&dma, &buf) < 0) {
849                                                 printk(KERN_WARNING "snd-page-alloc: cannot reserve buffer\n");
850                                                 snd_dma_free_pages(&dma, &buf);
851                                         }
852                                 } else
853                                         printk(KERN_WARNING "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", dev->size);
854                         }
855                 }
856         }
857 }
858
859
860 #ifdef CONFIG_PROC_FS
861 /*
862  * proc file interface
863  */
864 static int snd_mem_proc_read(char *page, char **start, off_t off,
865                              int count, int *eof, void *data)
866 {
867         int len = 0;
868         long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
869         struct list_head *p;
870         struct snd_mem_list *mem;
871         int devno;
872
873         down(&list_mutex);
874         len += sprintf(page + len, "pages  : %li bytes (%li pages per %likB)\n",
875                        pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
876         devno = 0;
877         list_for_each(p, &mem_list_head) {
878                 mem = list_entry(p, struct snd_mem_list, list);
879                 devno++;
880                 len += sprintf(page + len, "buffer %d : ", devno);
881                 if (mem->dev.id == SNDRV_DMA_DEVICE_UNUSED)
882                         len += sprintf(page + len, "UNUSED");
883                 else
884                         len += sprintf(page + len, "ID %08x", mem->dev.id);
885                 len += sprintf(page + len, " : type ");
886                 switch (mem->dev.type) {
887                 case SNDRV_DMA_TYPE_CONTINUOUS:
888                         len += sprintf(page + len, "CONT [%x]", mem->dev.dev.flags);
889                         break;
890 #ifdef CONFIG_PCI
891                 case SNDRV_DMA_TYPE_PCI:
892                 case SNDRV_DMA_TYPE_PCI_SG:
893                         if (mem->dev.dev.pci) {
894                                 len += sprintf(page + len, "PCI [%04x:%04x]",
895                                                mem->dev.dev.pci->vendor,
896                                                mem->dev.dev.pci->device);
897                         }
898                         break;
899 #endif
900 #ifdef CONFIG_ISA
901                 case SNDRV_DMA_TYPE_ISA:
902                         len += sprintf(page + len, "ISA [%x]", mem->dev.dev.flags);
903                         break;
904 #endif
905 #ifdef CONFIG_SBUS
906                 case SNDRV_DMA_TYPE_SBUS:
907                         len += sprintf(page + len, "SBUS [%x]", mem->dev.dev.sbus->slot);
908                         break;
909 #endif
910                 default:
911                         len += sprintf(page + len, "UNKNOWN");
912                         break;
913                 }
914                 len += sprintf(page + len, "\n  addr = 0x%lx, size = %d bytes, used = %s\n",
915                                (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes,
916                                mem->used ? "yes" : "no");
917         }
918         up(&list_mutex);
919         return len;
920 }
921 #endif /* CONFIG_PROC_FS */
922
923 /*
924  * module entry
925  */
926
927 static int __init snd_mem_init(void)
928 {
929 #ifdef CONFIG_PROC_FS
930         create_proc_read_entry("driver/snd-page-alloc", 0, 0, snd_mem_proc_read, NULL);
931 #endif
932         preallocate_cards();
933         return 0;
934 }
935
936 static void __exit snd_mem_exit(void)
937 {
938         remove_proc_entry("driver/snd-page-alloc", NULL);
939         free_all_reserved_pages();
940         if (snd_allocated_pages > 0)
941                 printk(KERN_ERR "snd-malloc: Memory leak?  pages not freed = %li\n", snd_allocated_pages);
942 }
943
944
945 module_init(snd_mem_init)
946 module_exit(snd_mem_exit)
947
948
949 #ifndef MODULE
950
951 /* format is: snd-page-alloc=enable */
952
953 static int __init snd_mem_setup(char *str)
954 {
955         static unsigned __initdata nr_dev = 0;
956
957         if (nr_dev >= SNDRV_CARDS)
958                 return 0;
959         (void)(get_option(&str,&enable[nr_dev]) == 2);
960         nr_dev++;
961         return 1;
962 }
963
964 __setup("snd-page-alloc=", snd_mem_setup);
965
966 #endif
967
968 /*
969  * exports
970  */
971 EXPORT_SYMBOL(snd_dma_alloc_pages);
972 EXPORT_SYMBOL(snd_dma_free_pages);
973 EXPORT_SYMBOL(snd_dma_get_reserved);
974 EXPORT_SYMBOL(snd_dma_free_reserved);
975 EXPORT_SYMBOL(snd_dma_set_reserved);
976
977 EXPORT_SYMBOL(snd_malloc_pages);
978 EXPORT_SYMBOL(snd_malloc_pages_fallback);
979 EXPORT_SYMBOL(snd_free_pages);
980 #if defined(CONFIG_ISA) && ! defined(CONFIG_PCI)
981 EXPORT_SYMBOL(snd_malloc_isa_pages);
982 EXPORT_SYMBOL(snd_malloc_isa_pages_fallback);
983 #endif
984 #ifdef CONFIG_PCI
985 EXPORT_SYMBOL(snd_malloc_pci_pages);
986 EXPORT_SYMBOL(snd_malloc_pci_pages_fallback);
987 EXPORT_SYMBOL(snd_malloc_pci_page);
988 EXPORT_SYMBOL(snd_free_pci_pages);
989 EXPORT_SYMBOL(snd_malloc_sgbuf_pages);
990 EXPORT_SYMBOL(snd_free_sgbuf_pages);
991 #endif
992 #ifdef CONFIG_SBUS
993 EXPORT_SYMBOL(snd_malloc_sbus_pages);
994 EXPORT_SYMBOL(snd_malloc_sbus_pages_fallback);
995 EXPORT_SYMBOL(snd_free_sbus_pages);
996 #endif