1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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"
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.");
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.");
28 static const size_t snd_minimum_buffer = 16384;
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.");
34 static void __update_allocated_size(struct snd_card *card, ssize_t bytes)
36 card->total_pcm_alloc_bytes += bytes;
39 static void update_allocated_size(struct snd_card *card, ssize_t bytes)
41 mutex_lock(&card->memory_mutex);
42 __update_allocated_size(card, bytes);
43 mutex_unlock(&card->memory_mutex);
46 static void decrease_allocated_size(struct snd_card *card, size_t bytes)
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);
54 static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
55 size_t size, struct snd_dma_buffer *dmab)
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);
66 __update_allocated_size(card, size);
67 mutex_unlock(&card->memory_mutex);
69 err = snd_dma_alloc_pages(type, dev, size, dmab);
71 /* the actual allocation size might be bigger than requested,
72 * and we need to correct the account
74 if (dmab->bytes != size)
75 update_allocated_size(card, dmab->bytes - size);
77 /* take back on allocation failure */
78 decrease_allocated_size(card, size);
83 static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
87 decrease_allocated_size(card, dmab->bytes);
88 snd_dma_free_pages(dmab);
93 * try to allocate as the large pages as possible.
94 * stores the resultant memory size in *res_size.
96 * the minimum size is snd_minimum_buffer. it should be power of 2.
98 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
100 struct snd_dma_buffer *dmab = &substream->dma_buffer;
101 struct snd_card *card = substream->pcm->card;
102 size_t orig_size = size;
106 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
111 } while (size >= snd_minimum_buffer);
112 dmab->bytes = 0; /* tell error */
113 pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
114 substream->pcm->card->number, substream->pcm->device,
115 substream->stream ? 'c' : 'p', substream->number,
116 substream->pcm->name, orig_size);
121 * release the preallocated buffer if not yet done.
123 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
125 do_free_pages(substream->pcm->card, &substream->dma_buffer);
129 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
130 * @substream: the pcm substream instance
132 * Releases the pre-allocated buffer of the given substream.
134 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
136 snd_pcm_lib_preallocate_dma_free(substream);
140 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
141 * @pcm: the pcm instance
143 * Releases all the pre-allocated buffers on the given pcm.
145 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
147 struct snd_pcm_substream *substream;
150 for (stream = 0; stream < 2; stream++)
151 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
152 snd_pcm_lib_preallocate_free(substream);
154 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
156 #ifdef CONFIG_SND_VERBOSE_PROCFS
158 * read callback for prealloc proc file
160 * prints the current allocated size in kB.
162 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
163 struct snd_info_buffer *buffer)
165 struct snd_pcm_substream *substream = entry->private_data;
166 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
170 * read callback for prealloc_max proc file
172 * prints the maximum allowed size in kB.
174 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
175 struct snd_info_buffer *buffer)
177 struct snd_pcm_substream *substream = entry->private_data;
178 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
182 * write callback for prealloc proc file
184 * accepts the preallocation size in kB.
186 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
187 struct snd_info_buffer *buffer)
189 struct snd_pcm_substream *substream = entry->private_data;
190 struct snd_card *card = substream->pcm->card;
191 char line[64], str[64];
193 struct snd_dma_buffer new_dmab;
195 mutex_lock(&substream->pcm->open_mutex);
196 if (substream->runtime) {
197 buffer->error = -EBUSY;
200 if (!snd_info_get_line(buffer, line, sizeof(line))) {
201 snd_info_get_str(str, line, sizeof(str));
202 size = simple_strtoul(str, NULL, 10) * 1024;
203 if ((size != 0 && size < 8192) || size > substream->dma_max) {
204 buffer->error = -EINVAL;
207 if (substream->dma_buffer.bytes == size)
209 memset(&new_dmab, 0, sizeof(new_dmab));
210 new_dmab.dev = substream->dma_buffer.dev;
212 if (do_alloc_pages(card,
213 substream->dma_buffer.dev.type,
214 substream->dma_buffer.dev.dev,
215 size, &new_dmab) < 0) {
216 buffer->error = -ENOMEM;
219 substream->buffer_bytes_max = size;
221 substream->buffer_bytes_max = UINT_MAX;
223 if (substream->dma_buffer.area)
224 do_free_pages(card, &substream->dma_buffer);
225 substream->dma_buffer = new_dmab;
227 buffer->error = -EINVAL;
230 mutex_unlock(&substream->pcm->open_mutex);
233 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
235 struct snd_info_entry *entry;
237 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
238 substream->proc_root);
240 snd_info_set_text_ops(entry, substream,
241 snd_pcm_lib_preallocate_proc_read);
242 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
245 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
246 substream->proc_root);
248 snd_info_set_text_ops(entry, substream,
249 snd_pcm_lib_preallocate_max_proc_read);
252 #else /* !CONFIG_SND_VERBOSE_PROCFS */
253 #define preallocate_info_init(s)
254 #endif /* CONFIG_SND_VERBOSE_PROCFS */
257 * pre-allocate the buffer and create a proc file for the substream
259 static void preallocate_pages(struct snd_pcm_substream *substream,
260 int type, struct device *data,
261 size_t size, size_t max, bool managed)
263 if (snd_BUG_ON(substream->dma_buffer.dev.type))
266 substream->dma_buffer.dev.type = type;
267 substream->dma_buffer.dev.dev = data;
269 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
270 preallocate_pcm_pages(substream, size);
272 if (substream->dma_buffer.bytes > 0)
273 substream->buffer_bytes_max = substream->dma_buffer.bytes;
274 substream->dma_max = max;
276 preallocate_info_init(substream);
278 substream->managed_buffer_alloc = 1;
281 static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
282 void *data, size_t size, size_t max,
285 struct snd_pcm_substream *substream;
288 for (stream = 0; stream < 2; stream++)
289 for (substream = pcm->streams[stream].substream; substream;
290 substream = substream->next)
291 preallocate_pages(substream, type, data, size, max,
296 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
297 * @substream: the pcm substream instance
298 * @type: DMA type (SNDRV_DMA_TYPE_*)
299 * @data: DMA type dependent data
300 * @size: the requested pre-allocation size in bytes
301 * @max: the max. allowed pre-allocation size
303 * Do pre-allocation for the given DMA buffer type.
305 void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
306 int type, struct device *data,
307 size_t size, size_t max)
309 preallocate_pages(substream, type, data, size, max, false);
311 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
314 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
315 * @pcm: the pcm instance
316 * @type: DMA type (SNDRV_DMA_TYPE_*)
317 * @data: DMA type dependent data
318 * @size: the requested pre-allocation size in bytes
319 * @max: the max. allowed pre-allocation size
321 * Do pre-allocation to all substreams of the given pcm for the
322 * specified DMA type.
324 void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
325 int type, void *data,
326 size_t size, size_t max)
328 preallocate_pages_for_all(pcm, type, data, size, max, false);
330 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
333 * snd_pcm_set_managed_buffer - set up buffer management for a substream
334 * @substream: the pcm substream instance
335 * @type: DMA type (SNDRV_DMA_TYPE_*)
336 * @data: DMA type dependent data
337 * @size: the requested pre-allocation size in bytes
338 * @max: the max. allowed pre-allocation size
340 * Do pre-allocation for the given DMA buffer type, and set the managed
341 * buffer allocation mode to the given substream.
342 * In this mode, PCM core will allocate a buffer automatically before PCM
343 * hw_params ops call, and release the buffer after PCM hw_free ops call
344 * as well, so that the driver doesn't need to invoke the allocation and
345 * the release explicitly in its callback.
346 * When a buffer is actually allocated before the PCM hw_params call, it
347 * turns on the runtime buffer_changed flag for drivers changing their h/w
348 * parameters accordingly.
350 void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
351 struct device *data, size_t size, size_t max)
353 preallocate_pages(substream, type, data, size, max, true);
355 EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
358 * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
360 * @pcm: the pcm instance
361 * @type: DMA type (SNDRV_DMA_TYPE_*)
362 * @data: DMA type dependent data
363 * @size: the requested pre-allocation size in bytes
364 * @max: the max. allowed pre-allocation size
366 * Do pre-allocation to all substreams of the given pcm for the specified DMA
367 * type and size, and set the managed_buffer_alloc flag to each substream.
369 void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
371 size_t size, size_t max)
373 preallocate_pages_for_all(pcm, type, data, size, max, true);
375 EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
377 #ifdef CONFIG_SND_DMA_SGBUF
379 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
380 * @substream: the pcm substream instance
381 * @offset: the buffer offset
383 * Used as the page callback of PCM ops.
385 * Return: The page struct at the given buffer offset. %NULL on failure.
387 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
389 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
391 unsigned int idx = offset >> PAGE_SHIFT;
392 if (idx >= (unsigned int)sgbuf->pages)
394 return sgbuf->page_table[idx];
396 #endif /* CONFIG_SND_DMA_SGBUF */
399 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
400 * @substream: the substream to allocate the DMA buffer to
401 * @size: the requested buffer size in bytes
403 * Allocates the DMA buffer on the BUS type given earlier to
404 * snd_pcm_lib_preallocate_xxx_pages().
406 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
409 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
411 struct snd_card *card;
412 struct snd_pcm_runtime *runtime;
413 struct snd_dma_buffer *dmab = NULL;
415 if (PCM_RUNTIME_CHECK(substream))
417 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
418 SNDRV_DMA_TYPE_UNKNOWN))
420 runtime = substream->runtime;
421 card = substream->pcm->card;
423 if (runtime->dma_buffer_p) {
424 /* perphaps, we might free the large DMA memory region
425 to save some space here, but the actual solution
426 costs us less time */
427 if (runtime->dma_buffer_p->bytes >= size) {
428 runtime->dma_bytes = size;
429 return 0; /* ok, do not change */
431 snd_pcm_lib_free_pages(substream);
433 if (substream->dma_buffer.area != NULL &&
434 substream->dma_buffer.bytes >= size) {
435 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
437 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
440 dmab->dev = substream->dma_buffer.dev;
441 if (do_alloc_pages(card,
442 substream->dma_buffer.dev.type,
443 substream->dma_buffer.dev.dev,
449 snd_pcm_set_runtime_buffer(substream, dmab);
450 runtime->dma_bytes = size;
451 return 1; /* area was changed */
453 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
456 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
457 * @substream: the substream to release the DMA buffer
459 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
461 * Return: Zero if successful, or a negative error code on failure.
463 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
465 struct snd_pcm_runtime *runtime;
467 if (PCM_RUNTIME_CHECK(substream))
469 runtime = substream->runtime;
470 if (runtime->dma_area == NULL)
472 if (runtime->dma_buffer_p != &substream->dma_buffer) {
473 struct snd_card *card = substream->pcm->card;
475 /* it's a newly allocated buffer. release it now. */
476 do_free_pages(card, runtime->dma_buffer_p);
477 kfree(runtime->dma_buffer_p);
479 snd_pcm_set_runtime_buffer(substream, NULL);
482 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
484 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
485 size_t size, gfp_t gfp_flags)
487 struct snd_pcm_runtime *runtime;
489 if (PCM_RUNTIME_CHECK(substream))
491 runtime = substream->runtime;
492 if (runtime->dma_area) {
493 if (runtime->dma_bytes >= size)
494 return 0; /* already large enough */
495 vfree(runtime->dma_area);
497 runtime->dma_area = __vmalloc(size, gfp_flags);
498 if (!runtime->dma_area)
500 runtime->dma_bytes = size;
503 EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
506 * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
507 * @substream: the substream with a buffer allocated by
508 * snd_pcm_lib_alloc_vmalloc_buffer()
510 * Return: Zero if successful, or a negative error code on failure.
512 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
514 struct snd_pcm_runtime *runtime;
516 if (PCM_RUNTIME_CHECK(substream))
518 runtime = substream->runtime;
519 vfree(runtime->dma_area);
520 runtime->dma_area = NULL;
523 EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
526 * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
527 * @substream: the substream with a buffer allocated by
528 * snd_pcm_lib_alloc_vmalloc_buffer()
529 * @offset: offset in the buffer
531 * This function is to be used as the page callback in the PCM ops.
533 * Return: The page struct, or %NULL on failure.
535 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
536 unsigned long offset)
538 return vmalloc_to_page(substream->runtime->dma_area + offset);
540 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);