2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
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>
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.");
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.");
39 static const size_t snd_minimum_buffer
= 16384;
43 * try to allocate as the large pages as possible.
44 * stores the resultant memory size in *res_size.
46 * the minimum size is snd_minimum_buffer. it should be power of 2.
48 static int preallocate_pcm_pages(struct snd_pcm_substream
*substream
, size_t size
)
50 struct snd_dma_buffer
*dmab
= &substream
->dma_buffer
;
53 /* already reserved? */
54 if (snd_dma_get_reserved_buf(dmab
, substream
->dma_buf_id
) > 0) {
55 if (dmab
->bytes
>= size
)
57 /* no, free the reserved block */
58 snd_dma_free_pages(dmab
);
63 if ((err
= snd_dma_alloc_pages(dmab
->dev
.type
, dmab
->dev
.dev
,
66 return err
; /* fatal error */
70 } while (size
>= snd_minimum_buffer
);
71 dmab
->bytes
= 0; /* tell error */
76 * release the preallocated buffer if not yet done.
78 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream
*substream
)
80 if (substream
->dma_buffer
.area
== NULL
)
82 if (substream
->dma_buf_id
)
83 snd_dma_reserve_buf(&substream
->dma_buffer
, substream
->dma_buf_id
);
85 snd_dma_free_pages(&substream
->dma_buffer
);
86 substream
->dma_buffer
.area
= NULL
;
90 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
91 * @substream: the pcm substream instance
93 * Releases the pre-allocated buffer of the given substream.
95 * Returns zero if successful, or a negative error code on failure.
97 int snd_pcm_lib_preallocate_free(struct snd_pcm_substream
*substream
)
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
;
110 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
111 * @pcm: the pcm instance
113 * Releases all the pre-allocated buffers on the given pcm.
115 * Returns zero if successful, or a negative error code on failure.
117 int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm
*pcm
)
119 struct snd_pcm_substream
*substream
;
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
);
128 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all
);
130 #ifdef CONFIG_SND_VERBOSE_PROCFS
132 * read callback for prealloc proc file
134 * prints the current allocated size in kB.
136 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry
*entry
,
137 struct snd_info_buffer
*buffer
)
139 struct snd_pcm_substream
*substream
= entry
->private_data
;
140 snd_iprintf(buffer
, "%lu\n", (unsigned long) substream
->dma_buffer
.bytes
/ 1024);
144 * read callback for prealloc_max proc file
146 * prints the maximum allowed size in kB.
148 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry
*entry
,
149 struct snd_info_buffer
*buffer
)
151 struct snd_pcm_substream
*substream
= entry
->private_data
;
152 snd_iprintf(buffer
, "%lu\n", (unsigned long) substream
->dma_max
/ 1024);
156 * write callback for prealloc proc file
158 * accepts the preallocation size in kB.
160 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry
*entry
,
161 struct snd_info_buffer
*buffer
)
163 struct snd_pcm_substream
*substream
= entry
->private_data
;
164 char line
[64], str
[64];
166 struct snd_dma_buffer new_dmab
;
168 if (substream
->runtime
) {
169 buffer
->error
= -EBUSY
;
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
;
179 if (substream
->dma_buffer
.bytes
== size
)
181 memset(&new_dmab
, 0, sizeof(new_dmab
));
182 new_dmab
.dev
= substream
->dma_buffer
.dev
;
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
;
190 substream
->buffer_bytes_max
= size
;
192 substream
->buffer_bytes_max
= UINT_MAX
;
194 if (substream
->dma_buffer
.area
)
195 snd_dma_free_pages(&substream
->dma_buffer
);
196 substream
->dma_buffer
= new_dmab
;
198 buffer
->error
= -EINVAL
;
202 static inline void preallocate_info_init(struct snd_pcm_substream
*substream
)
204 struct snd_info_entry
*entry
;
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
);
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
);
225 substream
->proc_prealloc_max_entry
= entry
;
228 #else /* !CONFIG_SND_VERBOSE_PROCFS */
229 #define preallocate_info_init(s)
230 #endif /* CONFIG_SND_VERBOSE_PROCFS */
233 * pre-allocate the buffer and create a proc file for the substream
235 static int snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream
*substream
,
236 size_t size
, size_t max
)
239 if (size
> 0 && preallocate_dma
&& substream
->number
< maximum_substreams
)
240 preallocate_pcm_pages(substream
, size
);
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
);
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
258 * Do pre-allocation for the given DMA buffer type.
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().
265 * Returns zero if successful, or a negative error code on failure.
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
)
271 substream
->dma_buffer
.dev
.type
= type
;
272 substream
->dma_buffer
.dev
.dev
= data
;
273 return snd_pcm_lib_preallocate_pages1(substream
, size
, max
);
276 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages
);
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
286 * Do pre-allocation to all substreams of the given pcm for the
287 * specified DMA type.
289 * Returns zero if successful, or a negative error code on failure.
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
)
295 struct snd_pcm_substream
*substream
;
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)
305 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all
);
308 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
309 * @substream: the pcm substream instance
310 * @offset: the buffer offset
312 * Returns the page struct at the given buffer offset.
313 * Used as the page callback of PCM ops.
315 struct page
*snd_pcm_sgbuf_ops_page(struct snd_pcm_substream
*substream
, unsigned long offset
)
317 struct snd_sg_buf
*sgbuf
= snd_pcm_substream_sgbuf(substream
);
319 unsigned int idx
= offset
>> PAGE_SHIFT
;
320 if (idx
>= (unsigned int)sgbuf
->pages
)
322 return sgbuf
->page_table
[idx
];
325 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page
);
328 * compute the max chunk size with continuous pages on sg-buffer
330 unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream
*substream
,
331 unsigned int ofs
, unsigned int size
)
333 struct snd_sg_buf
*sg
= snd_pcm_substream_sgbuf(substream
);
334 unsigned int start
, end
, pg
;
336 start
= ofs
>> PAGE_SHIFT
;
337 end
= (ofs
+ size
- 1) >> PAGE_SHIFT
;
338 /* check page continuity */
339 pg
= sg
->table
[start
].addr
>> PAGE_SHIFT
;
345 if ((sg
->table
[start
].addr
>> PAGE_SHIFT
) != pg
)
346 return (start
<< PAGE_SHIFT
) - ofs
;
348 /* ok, all on continuous pages */
351 EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size
);
354 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
355 * @substream: the substream to allocate the DMA buffer to
356 * @size: the requested buffer size in bytes
358 * Allocates the DMA buffer on the BUS type given earlier to
359 * snd_pcm_lib_preallocate_xxx_pages().
361 * Returns 1 if the buffer is changed, 0 if not changed, or a negative
364 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream
*substream
, size_t size
)
366 struct snd_pcm_runtime
*runtime
;
367 struct snd_dma_buffer
*dmab
= NULL
;
369 if (PCM_RUNTIME_CHECK(substream
))
371 if (snd_BUG_ON(substream
->dma_buffer
.dev
.type
==
372 SNDRV_DMA_TYPE_UNKNOWN
))
374 runtime
= substream
->runtime
;
376 if (runtime
->dma_buffer_p
) {
377 /* perphaps, we might free the large DMA memory region
378 to save some space here, but the actual solution
379 costs us less time */
380 if (runtime
->dma_buffer_p
->bytes
>= size
) {
381 runtime
->dma_bytes
= size
;
382 return 0; /* ok, do not change */
384 snd_pcm_lib_free_pages(substream
);
386 if (substream
->dma_buffer
.area
!= NULL
&&
387 substream
->dma_buffer
.bytes
>= size
) {
388 dmab
= &substream
->dma_buffer
; /* use the pre-allocated buffer */
390 dmab
= kzalloc(sizeof(*dmab
), GFP_KERNEL
);
393 dmab
->dev
= substream
->dma_buffer
.dev
;
394 if (snd_dma_alloc_pages(substream
->dma_buffer
.dev
.type
,
395 substream
->dma_buffer
.dev
.dev
,
401 snd_pcm_set_runtime_buffer(substream
, dmab
);
402 runtime
->dma_bytes
= size
;
403 return 1; /* area was changed */
406 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages
);
409 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
410 * @substream: the substream to release the DMA buffer
412 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
414 * Returns zero if successful, or a negative error code on failure.
416 int snd_pcm_lib_free_pages(struct snd_pcm_substream
*substream
)
418 struct snd_pcm_runtime
*runtime
;
420 if (PCM_RUNTIME_CHECK(substream
))
422 runtime
= substream
->runtime
;
423 if (runtime
->dma_area
== NULL
)
425 if (runtime
->dma_buffer_p
!= &substream
->dma_buffer
) {
426 /* it's a newly allocated buffer. release it now. */
427 snd_dma_free_pages(runtime
->dma_buffer_p
);
428 kfree(runtime
->dma_buffer_p
);
430 snd_pcm_set_runtime_buffer(substream
, NULL
);
434 EXPORT_SYMBOL(snd_pcm_lib_free_pages
);