blob: caa7796bc2f513e2bb2a73c1c26a8c866856894b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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
31static int preallocate_dma = 1;
32module_param(preallocate_dma, int, 0444);
33MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
34
35static int maximum_substreams = 4;
36module_param(maximum_substreams, int, 0444);
37MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
38
39static 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 */
Takashi Iwai877211f2005-11-17 13:59:38 +010048static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct snd_dma_buffer *dmab = &substream->dma_buffer;
51 int err;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* 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 */
Takashi Iwai877211f2005-11-17 13:59:38 +010078static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
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 */
Takashi Iwai877211f2005-11-17 13:59:38 +010097int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 snd_pcm_lib_preallocate_dma_free(substream);
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200100#ifdef CONFIG_SND_VERBOSE_PROCFS
Jaroslav Kyselac7132ae2006-10-06 15:12:29 +0200101 snd_info_free_entry(substream->proc_prealloc_max_entry);
102 substream->proc_prealloc_max_entry = NULL;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200103 snd_info_free_entry(substream->proc_prealloc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +0100104 substream->proc_prealloc_entry = NULL;
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 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 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100117int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Takashi Iwai877211f2005-11-17 13:59:38 +0100119 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 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
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200128EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
129
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200130#ifdef CONFIG_SND_VERBOSE_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
132 * read callback for prealloc proc file
133 *
134 * prints the current allocated size in kB.
135 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100136static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
137 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Takashi Iwai877211f2005-11-17 13:59:38 +0100139 struct snd_pcm_substream *substream = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
141}
142
143/*
Jaroslav Kyselac7132ae2006-10-06 15:12:29 +0200144 * read callback for prealloc_max proc file
145 *
146 * prints the maximum allowed size in kB.
147 */
148static 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * write callback for prealloc proc file
157 *
158 * accepts the preallocation size in kB.
159 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100160static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
161 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Takashi Iwai877211f2005-11-17 13:59:38 +0100163 struct snd_pcm_substream *substream = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 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
Takashi Iwaie28563c2005-12-01 10:42:42 +0100202static inline void preallocate_info_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Takashi Iwai877211f2005-11-17 13:59:38 +0100204 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200209 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 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;
Jaroslav Kyselac7132ae2006-10-06 15:12:29 +0200217 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;
Takashi Iwaie28563c2005-12-01 10:42:42 +0100226}
227
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200228#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100229#define preallocate_info_init(s)
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200230#endif /* CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100231
232/*
233 * pre-allocate the buffer and create a proc file for the substream
234 */
235static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100267int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 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
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200276EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/**
279 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams)
Takashi Iwaidf8db932005-09-07 13:38:19 +0200280 * @pcm: the pcm instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * @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 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100291int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int type, void *data,
293 size_t size, size_t max)
294{
Takashi Iwai877211f2005-11-17 13:59:38 +0100295 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 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
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200305EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
306
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200307#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/**
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 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100316struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
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
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200326EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
327
Takashi Iwai51e9f2e2008-07-30 15:13:33 +0200328/*
329 * compute the max chunk size with continuous pages on sg-buffer
330 */
331unsigned 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}
352EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size);
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200353#endif /* CONFIG_SND_DMA_SGBUF */
Takashi Iwai51e9f2e2008-07-30 15:13:33 +0200354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/**
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 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100366int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Takashi Iwai877211f2005-11-17 13:59:38 +0100368 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct snd_dma_buffer *dmab = NULL;
370
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200371 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
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 }
Takashi Iwai877211f2005-11-17 13:59:38 +0100388 if (substream->dma_buffer.area != NULL &&
389 substream->dma_buffer.bytes >= size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
391 } else {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200392 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 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
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200408EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
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 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100418int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Takashi Iwai877211f2005-11-17 13:59:38 +0100420 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200422 if (PCM_RUNTIME_CHECK(substream))
423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 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}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200435
436EXPORT_SYMBOL(snd_pcm_lib_free_pages);