Merge branch 'topic/pcm-drain-nonblock' into for-linus
[pandora-kernel.git] / sound / core / pcm_memory.c
1 /*
2  *  Digital Audio (PCM) abstract layer
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <asm/io.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/moduleparam.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/info.h>
29 #include <sound/initval.h>
30
31 static int preallocate_dma = 1;
32 module_param(preallocate_dma, int, 0444);
33 MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
34
35 static int maximum_substreams = 4;
36 module_param(maximum_substreams, int, 0444);
37 MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
38
39 static const size_t snd_minimum_buffer = 16384;
40
41
42 /*
43  * try to allocate as the large pages as possible.
44  * stores the resultant memory size in *res_size.
45  *
46  * the minimum size is snd_minimum_buffer.  it should be power of 2.
47  */
48 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
49 {
50         struct snd_dma_buffer *dmab = &substream->dma_buffer;
51         int err;
52
53         /* already reserved? */
54         if (snd_dma_get_reserved_buf(dmab, substream->dma_buf_id) > 0) {
55                 if (dmab->bytes >= size)
56                         return 0; /* yes */
57                 /* no, free the reserved block */
58                 snd_dma_free_pages(dmab);
59                 dmab->bytes = 0;
60         }
61
62         do {
63                 if ((err = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev,
64                                                size, dmab)) < 0) {
65                         if (err != -ENOMEM)
66                                 return err; /* fatal error */
67                 } else
68                         return 0;
69                 size >>= 1;
70         } while (size >= snd_minimum_buffer);
71         dmab->bytes = 0; /* tell error */
72         return 0;
73 }
74
75 /*
76  * release the preallocated buffer if not yet done.
77  */
78 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
79 {
80         if (substream->dma_buffer.area == NULL)
81                 return;
82         if (substream->dma_buf_id)
83                 snd_dma_reserve_buf(&substream->dma_buffer, substream->dma_buf_id);
84         else
85                 snd_dma_free_pages(&substream->dma_buffer);
86         substream->dma_buffer.area = NULL;
87 }
88
89 /**
90  * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
91  * @substream: the pcm substream instance
92  *
93  * Releases the pre-allocated buffer of the given substream.
94  *
95  * Returns zero if successful, or a negative error code on failure.
96  */
97 int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
98 {
99         snd_pcm_lib_preallocate_dma_free(substream);
100 #ifdef CONFIG_SND_VERBOSE_PROCFS
101         snd_info_free_entry(substream->proc_prealloc_max_entry);
102         substream->proc_prealloc_max_entry = NULL;
103         snd_info_free_entry(substream->proc_prealloc_entry);
104         substream->proc_prealloc_entry = NULL;
105 #endif
106         return 0;
107 }
108
109 /**
110  * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
111  * @pcm: the pcm instance
112  *
113  * Releases all the pre-allocated buffers on the given pcm.
114  *
115  * Returns zero if successful, or a negative error code on failure.
116  */
117 int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
118 {
119         struct snd_pcm_substream *substream;
120         int stream;
121
122         for (stream = 0; stream < 2; stream++)
123                 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
124                         snd_pcm_lib_preallocate_free(substream);
125         return 0;
126 }
127
128 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
129
130 #ifdef CONFIG_SND_VERBOSE_PROCFS
131 /*
132  * read callback for prealloc proc file
133  *
134  * prints the current allocated size in kB.
135  */
136 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
137                                               struct snd_info_buffer *buffer)
138 {
139         struct snd_pcm_substream *substream = entry->private_data;
140         snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
141 }
142
143 /*
144  * read callback for prealloc_max proc file
145  *
146  * prints the maximum allowed size in kB.
147  */
148 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
149                                                   struct snd_info_buffer *buffer)
150 {
151         struct snd_pcm_substream *substream = entry->private_data;
152         snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
153 }
154
155 /*
156  * write callback for prealloc proc file
157  *
158  * accepts the preallocation size in kB.
159  */
160 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
161                                                struct snd_info_buffer *buffer)
162 {
163         struct snd_pcm_substream *substream = entry->private_data;
164         char line[64], str[64];
165         size_t size;
166         struct snd_dma_buffer new_dmab;
167
168         if (substream->runtime) {
169                 buffer->error = -EBUSY;
170                 return;
171         }
172         if (!snd_info_get_line(buffer, line, sizeof(line))) {
173                 snd_info_get_str(str, line, sizeof(str));
174                 size = simple_strtoul(str, NULL, 10) * 1024;
175                 if ((size != 0 && size < 8192) || size > substream->dma_max) {
176                         buffer->error = -EINVAL;
177                         return;
178                 }
179                 if (substream->dma_buffer.bytes == size)
180                         return;
181                 memset(&new_dmab, 0, sizeof(new_dmab));
182                 new_dmab.dev = substream->dma_buffer.dev;
183                 if (size > 0) {
184                         if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
185                                                 substream->dma_buffer.dev.dev,
186                                                 size, &new_dmab) < 0) {
187                                 buffer->error = -ENOMEM;
188                                 return;
189                         }
190                         substream->buffer_bytes_max = size;
191                 } else {
192                         substream->buffer_bytes_max = UINT_MAX;
193                 }
194                 if (substream->dma_buffer.area)
195                         snd_dma_free_pages(&substream->dma_buffer);
196                 substream->dma_buffer = new_dmab;
197         } else {
198                 buffer->error = -EINVAL;
199         }
200 }
201
202 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
203 {
204         struct snd_info_entry *entry;
205
206         if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
207                 entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
208                 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
209                 entry->mode |= S_IWUSR;
210                 entry->private_data = substream;
211                 if (snd_info_register(entry) < 0) {
212                         snd_info_free_entry(entry);
213                         entry = NULL;
214                 }
215         }
216         substream->proc_prealloc_entry = entry;
217         if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max", substream->proc_root)) != NULL) {
218                 entry->c.text.read = snd_pcm_lib_preallocate_max_proc_read;
219                 entry->private_data = substream;
220                 if (snd_info_register(entry) < 0) {
221                         snd_info_free_entry(entry);
222                         entry = NULL;
223                 }
224         }
225         substream->proc_prealloc_max_entry = entry;
226 }
227
228 #else /* !CONFIG_SND_VERBOSE_PROCFS */
229 #define preallocate_info_init(s)
230 #endif /* CONFIG_SND_VERBOSE_PROCFS */
231
232 /*
233  * pre-allocate the buffer and create a proc file for the substream
234  */
235 static int snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
236                                           size_t size, size_t max)
237 {
238
239         if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
240                 preallocate_pcm_pages(substream, size);
241
242         if (substream->dma_buffer.bytes > 0)
243                 substream->buffer_bytes_max = substream->dma_buffer.bytes;
244         substream->dma_max = max;
245         preallocate_info_init(substream);
246         return 0;
247 }
248
249
250 /**
251  * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
252  * @substream: the pcm substream instance
253  * @type: DMA type (SNDRV_DMA_TYPE_*)
254  * @data: DMA type dependant data
255  * @size: the requested pre-allocation size in bytes
256  * @max: the max. allowed pre-allocation size
257  *
258  * Do pre-allocation for the given DMA buffer type.
259  *
260  * When substream->dma_buf_id is set, the function tries to look for
261  * the reserved buffer, and the buffer is not freed but reserved at
262  * destruction time.  The dma_buf_id must be unique for all systems
263  * (in the same DMA buffer type) e.g. using snd_dma_pci_buf_id().
264  *
265  * Returns zero if successful, or a negative error code on failure.
266  */
267 int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
268                                   int type, struct device *data,
269                                   size_t size, size_t max)
270 {
271         substream->dma_buffer.dev.type = type;
272         substream->dma_buffer.dev.dev = data;
273         return snd_pcm_lib_preallocate_pages1(substream, size, max);
274 }
275
276 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
277
278 /**
279  * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams)
280  * @pcm: the pcm instance
281  * @type: DMA type (SNDRV_DMA_TYPE_*)
282  * @data: DMA type dependant data
283  * @size: the requested pre-allocation size in bytes
284  * @max: the max. allowed pre-allocation size
285  *
286  * Do pre-allocation to all substreams of the given pcm for the
287  * specified DMA type.
288  *
289  * Returns zero if successful, or a negative error code on failure.
290  */
291 int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
292                                           int type, void *data,
293                                           size_t size, size_t max)
294 {
295         struct snd_pcm_substream *substream;
296         int stream, err;
297
298         for (stream = 0; stream < 2; stream++)
299                 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
300                         if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0)
301                                 return err;
302         return 0;
303 }
304
305 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
306
307 #ifdef CONFIG_SND_DMA_SGBUF
308 /**
309  * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
310  * @substream: the pcm substream instance
311  * @offset: the buffer offset
312  *
313  * Returns the page struct at the given buffer offset.
314  * Used as the page callback of PCM ops.
315  */
316 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
317 {
318         struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
319
320         unsigned int idx = offset >> PAGE_SHIFT;
321         if (idx >= (unsigned int)sgbuf->pages)
322                 return NULL;
323         return sgbuf->page_table[idx];
324 }
325
326 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
327
328 /*
329  * compute the max chunk size with continuous pages on sg-buffer
330  */
331 unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
332                                           unsigned int ofs, unsigned int size)
333 {
334         struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
335         unsigned int start, end, pg;
336
337         start = ofs >> PAGE_SHIFT;
338         end = (ofs + size - 1) >> PAGE_SHIFT;
339         /* check page continuity */
340         pg = sg->table[start].addr >> PAGE_SHIFT;
341         for (;;) {
342                 start++;
343                 if (start > end)
344                         break;
345                 pg++;
346                 if ((sg->table[start].addr >> PAGE_SHIFT) != pg)
347                         return (start << PAGE_SHIFT) - ofs;
348         }
349         /* ok, all on continuous pages */
350         return size;
351 }
352 EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size);
353 #endif /* CONFIG_SND_DMA_SGBUF */
354
355 /**
356  * snd_pcm_lib_malloc_pages - allocate the DMA buffer
357  * @substream: the substream to allocate the DMA buffer to
358  * @size: the requested buffer size in bytes
359  *
360  * Allocates the DMA buffer on the BUS type given earlier to
361  * snd_pcm_lib_preallocate_xxx_pages().
362  *
363  * Returns 1 if the buffer is changed, 0 if not changed, or a negative
364  * code on failure.
365  */
366 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
367 {
368         struct snd_pcm_runtime *runtime;
369         struct snd_dma_buffer *dmab = NULL;
370
371         if (PCM_RUNTIME_CHECK(substream))
372                 return -EINVAL;
373         if (snd_BUG_ON(substream->dma_buffer.dev.type ==
374                        SNDRV_DMA_TYPE_UNKNOWN))
375                 return -EINVAL;
376         runtime = substream->runtime;
377
378         if (runtime->dma_buffer_p) {
379                 /* perphaps, we might free the large DMA memory region
380                    to save some space here, but the actual solution
381                    costs us less time */
382                 if (runtime->dma_buffer_p->bytes >= size) {
383                         runtime->dma_bytes = size;
384                         return 0;       /* ok, do not change */
385                 }
386                 snd_pcm_lib_free_pages(substream);
387         }
388         if (substream->dma_buffer.area != NULL &&
389             substream->dma_buffer.bytes >= size) {
390                 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
391         } else {
392                 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
393                 if (! dmab)
394                         return -ENOMEM;
395                 dmab->dev = substream->dma_buffer.dev;
396                 if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
397                                         substream->dma_buffer.dev.dev,
398                                         size, dmab) < 0) {
399                         kfree(dmab);
400                         return -ENOMEM;
401                 }
402         }
403         snd_pcm_set_runtime_buffer(substream, dmab);
404         runtime->dma_bytes = size;
405         return 1;                       /* area was changed */
406 }
407
408 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
409
410 /**
411  * snd_pcm_lib_free_pages - release the allocated DMA buffer.
412  * @substream: the substream to release the DMA buffer
413  *
414  * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
415  *
416  * Returns zero if successful, or a negative error code on failure.
417  */
418 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
419 {
420         struct snd_pcm_runtime *runtime;
421
422         if (PCM_RUNTIME_CHECK(substream))
423                 return -EINVAL;
424         runtime = substream->runtime;
425         if (runtime->dma_area == NULL)
426                 return 0;
427         if (runtime->dma_buffer_p != &substream->dma_buffer) {
428                 /* it's a newly allocated buffer.  release it now. */
429                 snd_dma_free_pages(runtime->dma_buffer_p);
430                 kfree(runtime->dma_buffer_p);
431         }
432         snd_pcm_set_runtime_buffer(substream, NULL);
433         return 0;
434 }
435
436 EXPORT_SYMBOL(snd_pcm_lib_free_pages);