1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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
20 static int preallocate_dma = 1;
21 module_param(preallocate_dma, int, 0444);
22 MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
23
24 static int maximum_substreams = 4;
25 module_param(maximum_substreams, int, 0444);
26 MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
27
28 static const size_t snd_minimum_buffer = 16384;
29
30 static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
31 module_param(max_alloc_per_card, ulong, 0644);
32 MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
33
__update_allocated_size(struct snd_card * card,ssize_t bytes)34 static void __update_allocated_size(struct snd_card *card, ssize_t bytes)
35 {
36 card->total_pcm_alloc_bytes += bytes;
37 }
38
update_allocated_size(struct snd_card * card,ssize_t bytes)39 static void update_allocated_size(struct snd_card *card, ssize_t bytes)
40 {
41 mutex_lock(&card->memory_mutex);
42 __update_allocated_size(card, bytes);
43 mutex_unlock(&card->memory_mutex);
44 }
45
decrease_allocated_size(struct snd_card * card,size_t bytes)46 static void decrease_allocated_size(struct snd_card *card, size_t bytes)
47 {
48 mutex_lock(&card->memory_mutex);
49 WARN_ON(card->total_pcm_alloc_bytes < bytes);
50 __update_allocated_size(card, -(ssize_t)bytes);
51 mutex_unlock(&card->memory_mutex);
52 }
53
do_alloc_pages(struct snd_card * card,int type,struct device * dev,size_t size,struct snd_dma_buffer * dmab)54 static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
55 size_t size, struct snd_dma_buffer *dmab)
56 {
57 int err;
58
59 /* check and reserve the requested size */
60 mutex_lock(&card->memory_mutex);
61 if (max_alloc_per_card &&
62 card->total_pcm_alloc_bytes + size > max_alloc_per_card) {
63 mutex_unlock(&card->memory_mutex);
64 return -ENOMEM;
65 }
66 __update_allocated_size(card, size);
67 mutex_unlock(&card->memory_mutex);
68
69 err = snd_dma_alloc_pages(type, dev, size, dmab);
70 if (!err) {
71 /* the actual allocation size might be bigger than requested,
72 * and we need to correct the account
73 */
74 if (dmab->bytes != size)
75 update_allocated_size(card, dmab->bytes - size);
76 } else {
77 /* take back on allocation failure */
78 decrease_allocated_size(card, size);
79 }
80 return err;
81 }
82
do_free_pages(struct snd_card * card,struct snd_dma_buffer * dmab)83 static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
84 {
85 if (!dmab->area)
86 return;
87 decrease_allocated_size(card, dmab->bytes);
88 snd_dma_free_pages(dmab);
89 dmab->area = NULL;
90 }
91
92 /*
93 * try to allocate as the large pages as possible.
94 * stores the resultant memory size in *res_size.
95 *
96 * the minimum size is snd_minimum_buffer. it should be power of 2.
97 */
preallocate_pcm_pages(struct snd_pcm_substream * substream,size_t size,bool no_fallback)98 static int preallocate_pcm_pages(struct snd_pcm_substream *substream,
99 size_t size, bool no_fallback)
100 {
101 struct snd_dma_buffer *dmab = &substream->dma_buffer;
102 struct snd_card *card = substream->pcm->card;
103 size_t orig_size = size;
104 int err;
105
106 do {
107 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
108 size, dmab);
109 if (err != -ENOMEM)
110 return err;
111 if (no_fallback)
112 break;
113 size >>= 1;
114 } while (size >= snd_minimum_buffer);
115 dmab->bytes = 0; /* tell error */
116 pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
117 substream->pcm->card->number, substream->pcm->device,
118 substream->stream ? 'c' : 'p', substream->number,
119 substream->pcm->name, orig_size);
120 return -ENOMEM;
121 }
122
123 /**
124 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
125 * @substream: the pcm substream instance
126 *
127 * Releases the pre-allocated buffer of the given substream.
128 */
snd_pcm_lib_preallocate_free(struct snd_pcm_substream * substream)129 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
130 {
131 do_free_pages(substream->pcm->card, &substream->dma_buffer);
132 }
133
134 /**
135 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
136 * @pcm: the pcm instance
137 *
138 * Releases all the pre-allocated buffers on the given pcm.
139 */
snd_pcm_lib_preallocate_free_for_all(struct snd_pcm * pcm)140 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
141 {
142 struct snd_pcm_substream *substream;
143 int stream;
144
145 for_each_pcm_substream(pcm, stream, substream)
146 snd_pcm_lib_preallocate_free(substream);
147 }
148 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
149
150 #ifdef CONFIG_SND_VERBOSE_PROCFS
151 /*
152 * read callback for prealloc proc file
153 *
154 * prints the current allocated size in kB.
155 */
snd_pcm_lib_preallocate_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)156 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
157 struct snd_info_buffer *buffer)
158 {
159 struct snd_pcm_substream *substream = entry->private_data;
160 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
161 }
162
163 /*
164 * read callback for prealloc_max proc file
165 *
166 * prints the maximum allowed size in kB.
167 */
snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)168 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
169 struct snd_info_buffer *buffer)
170 {
171 struct snd_pcm_substream *substream = entry->private_data;
172 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
173 }
174
175 /*
176 * write callback for prealloc proc file
177 *
178 * accepts the preallocation size in kB.
179 */
snd_pcm_lib_preallocate_proc_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)180 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
181 struct snd_info_buffer *buffer)
182 {
183 struct snd_pcm_substream *substream = entry->private_data;
184 struct snd_card *card = substream->pcm->card;
185 char line[64], str[64];
186 size_t size;
187 struct snd_dma_buffer new_dmab;
188
189 mutex_lock(&substream->pcm->open_mutex);
190 if (substream->runtime) {
191 buffer->error = -EBUSY;
192 goto unlock;
193 }
194 if (!snd_info_get_line(buffer, line, sizeof(line))) {
195 snd_info_get_str(str, line, sizeof(str));
196 size = simple_strtoul(str, NULL, 10) * 1024;
197 if ((size != 0 && size < 8192) || size > substream->dma_max) {
198 buffer->error = -EINVAL;
199 goto unlock;
200 }
201 if (substream->dma_buffer.bytes == size)
202 goto unlock;
203 memset(&new_dmab, 0, sizeof(new_dmab));
204 new_dmab.dev = substream->dma_buffer.dev;
205 if (size > 0) {
206 if (do_alloc_pages(card,
207 substream->dma_buffer.dev.type,
208 substream->dma_buffer.dev.dev,
209 size, &new_dmab) < 0) {
210 buffer->error = -ENOMEM;
211 pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
212 substream->pcm->card->number, substream->pcm->device,
213 substream->stream ? 'c' : 'p', substream->number,
214 substream->pcm->name, size);
215 goto unlock;
216 }
217 substream->buffer_bytes_max = size;
218 } else {
219 substream->buffer_bytes_max = UINT_MAX;
220 }
221 if (substream->dma_buffer.area)
222 do_free_pages(card, &substream->dma_buffer);
223 substream->dma_buffer = new_dmab;
224 } else {
225 buffer->error = -EINVAL;
226 }
227 unlock:
228 mutex_unlock(&substream->pcm->open_mutex);
229 }
230
preallocate_info_init(struct snd_pcm_substream * substream)231 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
232 {
233 struct snd_info_entry *entry;
234
235 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
236 substream->proc_root);
237 if (entry) {
238 snd_info_set_text_ops(entry, substream,
239 snd_pcm_lib_preallocate_proc_read);
240 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
241 entry->mode |= 0200;
242 }
243 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
244 substream->proc_root);
245 if (entry)
246 snd_info_set_text_ops(entry, substream,
247 snd_pcm_lib_preallocate_max_proc_read);
248 }
249
250 #else /* !CONFIG_SND_VERBOSE_PROCFS */
preallocate_info_init(struct snd_pcm_substream * substream)251 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
252 {
253 }
254 #endif /* CONFIG_SND_VERBOSE_PROCFS */
255
256 /*
257 * pre-allocate the buffer and create a proc file for the substream
258 */
preallocate_pages(struct snd_pcm_substream * substream,int type,struct device * data,size_t size,size_t max,bool managed)259 static int preallocate_pages(struct snd_pcm_substream *substream,
260 int type, struct device *data,
261 size_t size, size_t max, bool managed)
262 {
263 int err;
264
265 if (snd_BUG_ON(substream->dma_buffer.dev.type))
266 return -EINVAL;
267
268 substream->dma_buffer.dev.type = type;
269 substream->dma_buffer.dev.dev = data;
270
271 if (size > 0) {
272 if (!max) {
273 /* no fallback, only also inform -ENOMEM */
274 err = preallocate_pcm_pages(substream, size, true);
275 if (err < 0)
276 return err;
277 } else if (preallocate_dma &&
278 substream->number < maximum_substreams) {
279 err = preallocate_pcm_pages(substream, size, false);
280 if (err < 0 && err != -ENOMEM)
281 return err;
282 }
283 }
284
285 if (substream->dma_buffer.bytes > 0)
286 substream->buffer_bytes_max = substream->dma_buffer.bytes;
287 substream->dma_max = max;
288 if (max > 0)
289 preallocate_info_init(substream);
290 if (managed)
291 substream->managed_buffer_alloc = 1;
292 return 0;
293 }
294
preallocate_pages_for_all(struct snd_pcm * pcm,int type,void * data,size_t size,size_t max,bool managed)295 static int preallocate_pages_for_all(struct snd_pcm *pcm, int type,
296 void *data, size_t size, size_t max,
297 bool managed)
298 {
299 struct snd_pcm_substream *substream;
300 int stream, err;
301
302 for_each_pcm_substream(pcm, stream, substream) {
303 err = preallocate_pages(substream, type, data, size, max, managed);
304 if (err < 0)
305 return err;
306 }
307 return 0;
308 }
309
310 /**
311 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
312 * @substream: the pcm substream instance
313 * @type: DMA type (SNDRV_DMA_TYPE_*)
314 * @data: DMA type dependent data
315 * @size: the requested pre-allocation size in bytes
316 * @max: the max. allowed pre-allocation size
317 *
318 * Do pre-allocation for the given DMA buffer type.
319 */
snd_pcm_lib_preallocate_pages(struct snd_pcm_substream * substream,int type,struct device * data,size_t size,size_t max)320 void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
321 int type, struct device *data,
322 size_t size, size_t max)
323 {
324 preallocate_pages(substream, type, data, size, max, false);
325 }
326 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
327
328 /**
329 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
330 * @pcm: the pcm instance
331 * @type: DMA type (SNDRV_DMA_TYPE_*)
332 * @data: DMA type dependent data
333 * @size: the requested pre-allocation size in bytes
334 * @max: the max. allowed pre-allocation size
335 *
336 * Do pre-allocation to all substreams of the given pcm for the
337 * specified DMA type.
338 */
snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm * pcm,int type,void * data,size_t size,size_t max)339 void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
340 int type, void *data,
341 size_t size, size_t max)
342 {
343 preallocate_pages_for_all(pcm, type, data, size, max, false);
344 }
345 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
346
347 /**
348 * snd_pcm_set_managed_buffer - set up buffer management for a substream
349 * @substream: the pcm substream instance
350 * @type: DMA type (SNDRV_DMA_TYPE_*)
351 * @data: DMA type dependent data
352 * @size: the requested pre-allocation size in bytes
353 * @max: the max. allowed pre-allocation size
354 *
355 * Do pre-allocation for the given DMA buffer type, and set the managed
356 * buffer allocation mode to the given substream.
357 * In this mode, PCM core will allocate a buffer automatically before PCM
358 * hw_params ops call, and release the buffer after PCM hw_free ops call
359 * as well, so that the driver doesn't need to invoke the allocation and
360 * the release explicitly in its callback.
361 * When a buffer is actually allocated before the PCM hw_params call, it
362 * turns on the runtime buffer_changed flag for drivers changing their h/w
363 * parameters accordingly.
364 *
365 * When @size is non-zero and @max is zero, this tries to allocate for only
366 * the exact buffer size without fallback, and may return -ENOMEM.
367 * Otherwise, the function tries to allocate smaller chunks if the allocation
368 * fails. This is the behavior of snd_pcm_set_fixed_buffer().
369 *
370 * When both @size and @max are zero, the function only sets up the buffer
371 * for later dynamic allocations. It's used typically for buffers with
372 * SNDRV_DMA_TYPE_VMALLOC type.
373 *
374 * Upon successful buffer allocation and setup, the function returns 0.
375 */
snd_pcm_set_managed_buffer(struct snd_pcm_substream * substream,int type,struct device * data,size_t size,size_t max)376 int snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
377 struct device *data, size_t size, size_t max)
378 {
379 return preallocate_pages(substream, type, data, size, max, true);
380 }
381 EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
382
383 /**
384 * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
385 * for all substreams
386 * @pcm: the pcm instance
387 * @type: DMA type (SNDRV_DMA_TYPE_*)
388 * @data: DMA type dependent data
389 * @size: the requested pre-allocation size in bytes
390 * @max: the max. allowed pre-allocation size
391 *
392 * Do pre-allocation to all substreams of the given pcm for the specified DMA
393 * type and size, and set the managed_buffer_alloc flag to each substream.
394 */
snd_pcm_set_managed_buffer_all(struct snd_pcm * pcm,int type,struct device * data,size_t size,size_t max)395 int snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
396 struct device *data,
397 size_t size, size_t max)
398 {
399 return preallocate_pages_for_all(pcm, type, data, size, max, true);
400 }
401 EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
402
403 /**
404 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
405 * @substream: the substream to allocate the DMA buffer to
406 * @size: the requested buffer size in bytes
407 *
408 * Allocates the DMA buffer on the BUS type given earlier to
409 * snd_pcm_lib_preallocate_xxx_pages().
410 *
411 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
412 * code on failure.
413 */
snd_pcm_lib_malloc_pages(struct snd_pcm_substream * substream,size_t size)414 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
415 {
416 struct snd_card *card;
417 struct snd_pcm_runtime *runtime;
418 struct snd_dma_buffer *dmab = NULL;
419
420 if (PCM_RUNTIME_CHECK(substream))
421 return -EINVAL;
422 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
423 SNDRV_DMA_TYPE_UNKNOWN))
424 return -EINVAL;
425 runtime = substream->runtime;
426 card = substream->pcm->card;
427
428 if (runtime->dma_buffer_p) {
429 /* perphaps, we might free the large DMA memory region
430 to save some space here, but the actual solution
431 costs us less time */
432 if (runtime->dma_buffer_p->bytes >= size) {
433 runtime->dma_bytes = size;
434 return 0; /* ok, do not change */
435 }
436 snd_pcm_lib_free_pages(substream);
437 }
438 if (substream->dma_buffer.area != NULL &&
439 substream->dma_buffer.bytes >= size) {
440 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
441 } else {
442 /* dma_max=0 means the fixed size preallocation */
443 if (substream->dma_buffer.area && !substream->dma_max)
444 return -ENOMEM;
445 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
446 if (! dmab)
447 return -ENOMEM;
448 dmab->dev = substream->dma_buffer.dev;
449 if (do_alloc_pages(card,
450 substream->dma_buffer.dev.type,
451 substream->dma_buffer.dev.dev,
452 size, dmab) < 0) {
453 kfree(dmab);
454 pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
455 substream->pcm->card->number, substream->pcm->device,
456 substream->stream ? 'c' : 'p', substream->number,
457 substream->pcm->name, size);
458 return -ENOMEM;
459 }
460 }
461 snd_pcm_set_runtime_buffer(substream, dmab);
462 runtime->dma_bytes = size;
463 return 1; /* area was changed */
464 }
465 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
466
467 /**
468 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
469 * @substream: the substream to release the DMA buffer
470 *
471 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
472 *
473 * Return: Zero if successful, or a negative error code on failure.
474 */
snd_pcm_lib_free_pages(struct snd_pcm_substream * substream)475 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
476 {
477 struct snd_pcm_runtime *runtime;
478
479 if (PCM_RUNTIME_CHECK(substream))
480 return -EINVAL;
481 runtime = substream->runtime;
482 if (runtime->dma_area == NULL)
483 return 0;
484 if (runtime->dma_buffer_p != &substream->dma_buffer) {
485 struct snd_card *card = substream->pcm->card;
486
487 /* it's a newly allocated buffer. release it now. */
488 do_free_pages(card, runtime->dma_buffer_p);
489 kfree(runtime->dma_buffer_p);
490 }
491 snd_pcm_set_runtime_buffer(substream, NULL);
492 return 0;
493 }
494 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
495
_snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream * substream,size_t size,gfp_t gfp_flags)496 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
497 size_t size, gfp_t gfp_flags)
498 {
499 struct snd_pcm_runtime *runtime;
500
501 if (PCM_RUNTIME_CHECK(substream))
502 return -EINVAL;
503 runtime = substream->runtime;
504 if (runtime->dma_area) {
505 if (runtime->dma_bytes >= size)
506 return 0; /* already large enough */
507 vfree(runtime->dma_area);
508 }
509 runtime->dma_area = __vmalloc(size, gfp_flags);
510 if (!runtime->dma_area)
511 return -ENOMEM;
512 runtime->dma_bytes = size;
513 return 1;
514 }
515 EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
516
517 /**
518 * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
519 * @substream: the substream with a buffer allocated by
520 * snd_pcm_lib_alloc_vmalloc_buffer()
521 *
522 * Return: Zero if successful, or a negative error code on failure.
523 */
snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream * substream)524 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
525 {
526 struct snd_pcm_runtime *runtime;
527
528 if (PCM_RUNTIME_CHECK(substream))
529 return -EINVAL;
530 runtime = substream->runtime;
531 vfree(runtime->dma_area);
532 runtime->dma_area = NULL;
533 return 0;
534 }
535 EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
536
537 /**
538 * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
539 * @substream: the substream with a buffer allocated by
540 * snd_pcm_lib_alloc_vmalloc_buffer()
541 * @offset: offset in the buffer
542 *
543 * This function is to be used as the page callback in the PCM ops.
544 *
545 * Return: The page struct, or %NULL on failure.
546 */
snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream * substream,unsigned long offset)547 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
548 unsigned long offset)
549 {
550 return vmalloc_to_page(substream->runtime->dma_area + offset);
551 }
552 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);
553