1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
44
45
46
47
48static 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
54 if (snd_dma_get_reserved_buf(dmab, substream->dma_buf_id) > 0) {
55 if (dmab->bytes >= size)
56 return 0;
57
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;
67 } else
68 return 0;
69 size >>= 1;
70 } while (size >= snd_minimum_buffer);
71 dmab->bytes = 0;
72 return 0;
73}
74
75
76
77
78static 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
91
92
93
94
95
96
97int 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
111
112
113
114
115
116
117int 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
128EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
129
130#ifdef CONFIG_SND_VERBOSE_PROCFS
131
132
133
134
135
136static 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
145
146
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
156
157
158
159
160static 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
202static 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
229#define preallocate_info_init(s)
230#endif
231
232
233
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);
246 return 0;
247}
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267int 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
276EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291int 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
305EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
306
307#ifdef CONFIG_SND_DMA_SGBUF
308
309
310
311
312
313
314
315
316struct 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
326EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
327
328
329
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
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
350 return size;
351}
352EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size);
353#endif
354
355
356
357
358
359
360
361
362
363
364
365
366int 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
380
381
382 if (runtime->dma_buffer_p->bytes >= size) {
383 runtime->dma_bytes = size;
384 return 0;
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;
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;
406}
407
408EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
409
410
411
412
413
414
415
416
417
418int 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
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
436EXPORT_SYMBOL(snd_pcm_lib_free_pages);
437