1
2
3
4
5
6
7#include <linux/io.h>
8#include <linux/time.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/moduleparam.h>
12#include <linux/vmalloc.h>
13#include <linux/export.h>
14#include <sound/core.h>
15#include <sound/pcm.h>
16#include <sound/info.h>
17#include <sound/initval.h>
18#include "pcm_local.h"
19
20static int preallocate_dma = 1;
21module_param(preallocate_dma, int, 0444);
22MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
23
24static int maximum_substreams = 4;
25module_param(maximum_substreams, int, 0444);
26MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
27
28static const size_t snd_minimum_buffer = 16384;
29
30static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
31module_param(max_alloc_per_card, ulong, 0644);
32MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
33
34static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
35 size_t size, struct snd_dma_buffer *dmab)
36{
37 int err;
38
39 if (max_alloc_per_card &&
40 card->total_pcm_alloc_bytes + size > max_alloc_per_card)
41 return -ENOMEM;
42
43 err = snd_dma_alloc_pages(type, dev, size, dmab);
44 if (!err) {
45 mutex_lock(&card->memory_mutex);
46 card->total_pcm_alloc_bytes += dmab->bytes;
47 mutex_unlock(&card->memory_mutex);
48 }
49 return err;
50}
51
52static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
53{
54 if (!dmab->area)
55 return;
56 mutex_lock(&card->memory_mutex);
57 WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes);
58 card->total_pcm_alloc_bytes -= dmab->bytes;
59 mutex_unlock(&card->memory_mutex);
60 snd_dma_free_pages(dmab);
61 dmab->area = NULL;
62}
63
64
65
66
67
68
69
70static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
71{
72 struct snd_dma_buffer *dmab = &substream->dma_buffer;
73 struct snd_card *card = substream->pcm->card;
74 size_t orig_size = size;
75 int err;
76
77 do {
78 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
79 size, dmab);
80 if (err != -ENOMEM)
81 return err;
82 size >>= 1;
83 } while (size >= snd_minimum_buffer);
84 dmab->bytes = 0;
85 pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
86 substream->pcm->card->number, substream->pcm->device,
87 substream->stream ? 'c' : 'p', substream->number,
88 substream->pcm->name, orig_size);
89 return 0;
90}
91
92
93
94
95static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
96{
97 do_free_pages(substream->pcm->card, &substream->dma_buffer);
98}
99
100
101
102
103
104
105
106void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
107{
108 snd_pcm_lib_preallocate_dma_free(substream);
109}
110
111
112
113
114
115
116
117void 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}
126EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
127
128#ifdef CONFIG_SND_VERBOSE_PROCFS
129
130
131
132
133
134static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
135 struct snd_info_buffer *buffer)
136{
137 struct snd_pcm_substream *substream = entry->private_data;
138 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
139}
140
141
142
143
144
145
146static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
147 struct snd_info_buffer *buffer)
148{
149 struct snd_pcm_substream *substream = entry->private_data;
150 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
151}
152
153
154
155
156
157
158static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
159 struct snd_info_buffer *buffer)
160{
161 struct snd_pcm_substream *substream = entry->private_data;
162 struct snd_card *card = substream->pcm->card;
163 char line[64], str[64];
164 size_t size;
165 struct snd_dma_buffer new_dmab;
166
167 if (substream->runtime) {
168 buffer->error = -EBUSY;
169 return;
170 }
171 if (!snd_info_get_line(buffer, line, sizeof(line))) {
172 snd_info_get_str(str, line, sizeof(str));
173 size = simple_strtoul(str, NULL, 10) * 1024;
174 if ((size != 0 && size < 8192) || size > substream->dma_max) {
175 buffer->error = -EINVAL;
176 return;
177 }
178 if (substream->dma_buffer.bytes == size)
179 return;
180 memset(&new_dmab, 0, sizeof(new_dmab));
181 new_dmab.dev = substream->dma_buffer.dev;
182 if (size > 0) {
183 if (do_alloc_pages(card,
184 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 do_free_pages(card, &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 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
207 substream->proc_root);
208 if (entry) {
209 snd_info_set_text_ops(entry, substream,
210 snd_pcm_lib_preallocate_proc_read);
211 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
212 entry->mode |= 0200;
213 }
214 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
215 substream->proc_root);
216 if (entry)
217 snd_info_set_text_ops(entry, substream,
218 snd_pcm_lib_preallocate_max_proc_read);
219}
220
221#else
222#define preallocate_info_init(s)
223#endif
224
225
226
227
228static void preallocate_pages(struct snd_pcm_substream *substream,
229 int type, struct device *data,
230 size_t size, size_t max, bool managed)
231{
232 if (snd_BUG_ON(substream->dma_buffer.dev.type))
233 return;
234
235 substream->dma_buffer.dev.type = type;
236 substream->dma_buffer.dev.dev = data;
237
238 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
239 preallocate_pcm_pages(substream, size);
240
241 if (substream->dma_buffer.bytes > 0)
242 substream->buffer_bytes_max = substream->dma_buffer.bytes;
243 substream->dma_max = max;
244 if (max > 0)
245 preallocate_info_init(substream);
246 if (managed)
247 substream->managed_buffer_alloc = 1;
248}
249
250static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
251 void *data, size_t size, size_t max,
252 bool managed)
253{
254 struct snd_pcm_substream *substream;
255 int stream;
256
257 for (stream = 0; stream < 2; stream++)
258 for (substream = pcm->streams[stream].substream; substream;
259 substream = substream->next)
260 preallocate_pages(substream, type, data, size, max,
261 managed);
262}
263
264
265
266
267
268
269
270
271
272
273
274void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
275 int type, struct device *data,
276 size_t size, size_t max)
277{
278 preallocate_pages(substream, type, data, size, max, false);
279}
280EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
281
282
283
284
285
286
287
288
289
290
291
292
293void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
294 int type, void *data,
295 size_t size, size_t max)
296{
297 preallocate_pages_for_all(pcm, type, data, size, max, false);
298}
299EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
320 struct device *data, size_t size, size_t max)
321{
322 preallocate_pages(substream, type, data, size, max, true);
323}
324EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
325
326
327
328
329
330
331
332
333
334
335
336
337
338void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
339 struct device *data,
340 size_t size, size_t max)
341{
342 preallocate_pages_for_all(pcm, type, data, size, max, true);
343}
344EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
345
346#ifdef CONFIG_SND_DMA_SGBUF
347
348
349
350
351
352
353
354
355
356struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
357{
358 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
359
360 unsigned int idx = offset >> PAGE_SHIFT;
361 if (idx >= (unsigned int)sgbuf->pages)
362 return NULL;
363 return sgbuf->page_table[idx];
364}
365#endif
366
367
368
369
370
371
372
373
374
375
376
377
378int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
379{
380 struct snd_card *card;
381 struct snd_pcm_runtime *runtime;
382 struct snd_dma_buffer *dmab = NULL;
383
384 if (PCM_RUNTIME_CHECK(substream))
385 return -EINVAL;
386 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
387 SNDRV_DMA_TYPE_UNKNOWN))
388 return -EINVAL;
389 runtime = substream->runtime;
390 card = substream->pcm->card;
391
392 if (runtime->dma_buffer_p) {
393
394
395
396 if (runtime->dma_buffer_p->bytes >= size) {
397 runtime->dma_bytes = size;
398 return 0;
399 }
400 snd_pcm_lib_free_pages(substream);
401 }
402 if (substream->dma_buffer.area != NULL &&
403 substream->dma_buffer.bytes >= size) {
404 dmab = &substream->dma_buffer;
405 } else {
406 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
407 if (! dmab)
408 return -ENOMEM;
409 dmab->dev = substream->dma_buffer.dev;
410 if (do_alloc_pages(card,
411 substream->dma_buffer.dev.type,
412 substream->dma_buffer.dev.dev,
413 size, dmab) < 0) {
414 kfree(dmab);
415 return -ENOMEM;
416 }
417 }
418 snd_pcm_set_runtime_buffer(substream, dmab);
419 runtime->dma_bytes = size;
420 return 1;
421}
422EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
423
424
425
426
427
428
429
430
431
432int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
433{
434 struct snd_card *card = substream->pcm->card;
435 struct snd_pcm_runtime *runtime;
436
437 if (PCM_RUNTIME_CHECK(substream))
438 return -EINVAL;
439 runtime = substream->runtime;
440 if (runtime->dma_area == NULL)
441 return 0;
442 if (runtime->dma_buffer_p != &substream->dma_buffer) {
443
444 do_free_pages(card, runtime->dma_buffer_p);
445 kfree(runtime->dma_buffer_p);
446 }
447 snd_pcm_set_runtime_buffer(substream, NULL);
448 return 0;
449}
450EXPORT_SYMBOL(snd_pcm_lib_free_pages);
451
452int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
453 size_t size, gfp_t gfp_flags)
454{
455 struct snd_pcm_runtime *runtime;
456
457 if (PCM_RUNTIME_CHECK(substream))
458 return -EINVAL;
459 runtime = substream->runtime;
460 if (runtime->dma_area) {
461 if (runtime->dma_bytes >= size)
462 return 0;
463 vfree(runtime->dma_area);
464 }
465 runtime->dma_area = __vmalloc(size, gfp_flags);
466 if (!runtime->dma_area)
467 return -ENOMEM;
468 runtime->dma_bytes = size;
469 return 1;
470}
471EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
472
473
474
475
476
477
478
479
480int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
481{
482 struct snd_pcm_runtime *runtime;
483
484 if (PCM_RUNTIME_CHECK(substream))
485 return -EINVAL;
486 runtime = substream->runtime;
487 vfree(runtime->dma_area);
488 runtime->dma_area = NULL;
489 return 0;
490}
491EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
492
493
494
495
496
497
498
499
500
501
502
503struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
504 unsigned long offset)
505{
506 return vmalloc_to_page(substream->runtime->dma_area + offset);
507}
508EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);
509