Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Digital Audio (PCM) abstract layer | |
c1017a4c | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
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 | ||
1da177e4 LT |
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 | */ | |
877211f5 | 48 | static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size) |
1da177e4 LT |
49 | { |
50 | struct snd_dma_buffer *dmab = &substream->dma_buffer; | |
51 | int err; | |
52 | ||
1da177e4 LT |
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 | */ | |
877211f5 | 78 | static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream) |
1da177e4 LT |
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 | */ | |
877211f5 | 97 | int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream) |
1da177e4 LT |
98 | { |
99 | snd_pcm_lib_preallocate_dma_free(substream); | |
b7d90a35 | 100 | #ifdef CONFIG_SND_VERBOSE_PROCFS |
c7132aeb JK |
101 | snd_info_free_entry(substream->proc_prealloc_max_entry); |
102 | substream->proc_prealloc_max_entry = NULL; | |
746d4a02 | 103 | snd_info_free_entry(substream->proc_prealloc_entry); |
e28563cc | 104 | substream->proc_prealloc_entry = NULL; |
b7d90a35 | 105 | #endif |
1da177e4 LT |
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 | */ | |
877211f5 | 117 | int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm) |
1da177e4 | 118 | { |
877211f5 | 119 | struct snd_pcm_substream *substream; |
1da177e4 LT |
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 | ||
e88e8ae6 TI |
128 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all); |
129 | ||
b7d90a35 | 130 | #ifdef CONFIG_SND_VERBOSE_PROCFS |
1da177e4 LT |
131 | /* |
132 | * read callback for prealloc proc file | |
133 | * | |
134 | * prints the current allocated size in kB. | |
135 | */ | |
877211f5 TI |
136 | static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry, |
137 | struct snd_info_buffer *buffer) | |
1da177e4 | 138 | { |
877211f5 | 139 | struct snd_pcm_substream *substream = entry->private_data; |
1da177e4 LT |
140 | snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024); |
141 | } | |
142 | ||
c7132aeb JK |
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 | ||
1da177e4 LT |
155 | /* |
156 | * write callback for prealloc proc file | |
157 | * | |
158 | * accepts the preallocation size in kB. | |
159 | */ | |
877211f5 TI |
160 | static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry, |
161 | struct snd_info_buffer *buffer) | |
1da177e4 | 162 | { |
877211f5 | 163 | struct snd_pcm_substream *substream = entry->private_data; |
1da177e4 LT |
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 | ||
e28563cc | 202 | static inline void preallocate_info_init(struct snd_pcm_substream *substream) |
1da177e4 | 203 | { |
877211f5 | 204 | struct snd_info_entry *entry; |
1da177e4 | 205 | |
1da177e4 | 206 | if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) { |
1da177e4 | 207 | entry->c.text.read = snd_pcm_lib_preallocate_proc_read; |
1da177e4 | 208 | entry->c.text.write = snd_pcm_lib_preallocate_proc_write; |
bd7bf042 | 209 | entry->mode |= S_IWUSR; |
1da177e4 LT |
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; | |
c7132aeb JK |
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; | |
e28563cc TI |
226 | } |
227 | ||
b7d90a35 | 228 | #else /* !CONFIG_SND_VERBOSE_PROCFS */ |
e28563cc | 229 | #define preallocate_info_init(s) |
b7d90a35 | 230 | #endif /* CONFIG_SND_VERBOSE_PROCFS */ |
e28563cc TI |
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); | |
1da177e4 LT |
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 | */ | |
877211f5 | 267 | int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream, |
1da177e4 LT |
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 | ||
e88e8ae6 TI |
276 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages); |
277 | ||
1da177e4 LT |
278 | /** |
279 | * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams) | |
df8db936 | 280 | * @pcm: the pcm instance |
1da177e4 LT |
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 | */ | |
877211f5 | 291 | int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm, |
1da177e4 LT |
292 | int type, void *data, |
293 | size_t size, size_t max) | |
294 | { | |
877211f5 | 295 | struct snd_pcm_substream *substream; |
1da177e4 LT |
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 | ||
e88e8ae6 TI |
305 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all); |
306 | ||
1da177e4 LT |
307 | /** |
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 | |
311 | * | |
312 | * Returns the page struct at the given buffer offset. | |
313 | * Used as the page callback of PCM ops. | |
314 | */ | |
877211f5 | 315 | struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset) |
1da177e4 LT |
316 | { |
317 | struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream); | |
318 | ||
319 | unsigned int idx = offset >> PAGE_SHIFT; | |
320 | if (idx >= (unsigned int)sgbuf->pages) | |
321 | return NULL; | |
322 | return sgbuf->page_table[idx]; | |
323 | } | |
324 | ||
e88e8ae6 TI |
325 | EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page); |
326 | ||
51e9f2e6 TI |
327 | /* |
328 | * compute the max chunk size with continuous pages on sg-buffer | |
329 | */ | |
330 | unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream, | |
331 | unsigned int ofs, unsigned int size) | |
332 | { | |
333 | struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream); | |
334 | unsigned int start, end, pg; | |
335 | ||
336 | start = ofs >> PAGE_SHIFT; | |
337 | end = (ofs + size - 1) >> PAGE_SHIFT; | |
338 | /* check page continuity */ | |
339 | pg = sg->table[start].addr >> PAGE_SHIFT; | |
340 | for (;;) { | |
341 | start++; | |
342 | if (start > end) | |
343 | break; | |
344 | pg++; | |
345 | if ((sg->table[start].addr >> PAGE_SHIFT) != pg) | |
346 | return (start << PAGE_SHIFT) - ofs; | |
347 | } | |
348 | /* ok, all on continuous pages */ | |
349 | return size; | |
350 | } | |
351 | EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size); | |
352 | ||
1da177e4 LT |
353 | /** |
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 | |
357 | * | |
358 | * Allocates the DMA buffer on the BUS type given earlier to | |
359 | * snd_pcm_lib_preallocate_xxx_pages(). | |
360 | * | |
361 | * Returns 1 if the buffer is changed, 0 if not changed, or a negative | |
362 | * code on failure. | |
363 | */ | |
877211f5 | 364 | int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size) |
1da177e4 | 365 | { |
877211f5 | 366 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
367 | struct snd_dma_buffer *dmab = NULL; |
368 | ||
7eaa943c TI |
369 | if (PCM_RUNTIME_CHECK(substream)) |
370 | return -EINVAL; | |
371 | if (snd_BUG_ON(substream->dma_buffer.dev.type == | |
372 | SNDRV_DMA_TYPE_UNKNOWN)) | |
373 | return -EINVAL; | |
1da177e4 | 374 | runtime = substream->runtime; |
1da177e4 LT |
375 | |
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 */ | |
383 | } | |
384 | snd_pcm_lib_free_pages(substream); | |
385 | } | |
877211f5 TI |
386 | if (substream->dma_buffer.area != NULL && |
387 | substream->dma_buffer.bytes >= size) { | |
1da177e4 LT |
388 | dmab = &substream->dma_buffer; /* use the pre-allocated buffer */ |
389 | } else { | |
ca2c0966 | 390 | dmab = kzalloc(sizeof(*dmab), GFP_KERNEL); |
1da177e4 LT |
391 | if (! dmab) |
392 | return -ENOMEM; | |
393 | dmab->dev = substream->dma_buffer.dev; | |
394 | if (snd_dma_alloc_pages(substream->dma_buffer.dev.type, | |
395 | substream->dma_buffer.dev.dev, | |
396 | size, dmab) < 0) { | |
397 | kfree(dmab); | |
398 | return -ENOMEM; | |
399 | } | |
400 | } | |
401 | snd_pcm_set_runtime_buffer(substream, dmab); | |
402 | runtime->dma_bytes = size; | |
403 | return 1; /* area was changed */ | |
404 | } | |
405 | ||
e88e8ae6 TI |
406 | EXPORT_SYMBOL(snd_pcm_lib_malloc_pages); |
407 | ||
1da177e4 LT |
408 | /** |
409 | * snd_pcm_lib_free_pages - release the allocated DMA buffer. | |
410 | * @substream: the substream to release the DMA buffer | |
411 | * | |
412 | * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages(). | |
413 | * | |
414 | * Returns zero if successful, or a negative error code on failure. | |
415 | */ | |
877211f5 | 416 | int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream) |
1da177e4 | 417 | { |
877211f5 | 418 | struct snd_pcm_runtime *runtime; |
1da177e4 | 419 | |
7eaa943c TI |
420 | if (PCM_RUNTIME_CHECK(substream)) |
421 | return -EINVAL; | |
1da177e4 | 422 | runtime = substream->runtime; |
1da177e4 LT |
423 | if (runtime->dma_area == NULL) |
424 | return 0; | |
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); | |
429 | } | |
430 | snd_pcm_set_runtime_buffer(substream, NULL); | |
431 | return 0; | |
432 | } | |
e88e8ae6 TI |
433 | |
434 | EXPORT_SYMBOL(snd_pcm_lib_free_pages); |