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 int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
35 size_t size, struct snd_dma_buffer *dmab)
39 if (max_alloc_per_card &&
40 card->total_pcm_alloc_bytes + size > max_alloc_per_card)
42 err = snd_dma_alloc_pages(type, dev, size, dmab);
44 mutex_lock(&card->memory_mutex);
45 card->total_pcm_alloc_bytes += dmab->bytes;
46 mutex_unlock(&card->memory_mutex);
51 static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
55 mutex_lock(&card->memory_mutex);
56 WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes);
57 card->total_pcm_alloc_bytes -= dmab->bytes;
58 mutex_unlock(&card->memory_mutex);
59 snd_dma_free_pages(dmab);
64 * try to allocate as the large pages as possible.
65 * stores the resultant memory size in *res_size.
67 * the minimum size is snd_minimum_buffer. it should be power of 2.
69 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
71 struct snd_dma_buffer *dmab = &substream->dma_buffer;
72 struct snd_card *card = substream->pcm->card;
73 size_t orig_size = size;
77 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
82 } while (size >= snd_minimum_buffer);
83 dmab->bytes = 0; /* tell error */
84 pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
85 substream->pcm->card->number, substream->pcm->device,
86 substream->stream ? 'c' : 'p', substream->number,
87 substream->pcm->name, orig_size);
92 * release the preallocated buffer if not yet done.
94 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
96 do_free_pages(substream->pcm->card, &substream->dma_buffer);
100 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
101 * @substream: the pcm substream instance
103 * Releases the pre-allocated buffer of the given substream.
105 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
107 snd_pcm_lib_preallocate_dma_free(substream);
111 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
112 * @pcm: the pcm instance
114 * Releases all the pre-allocated buffers on the given pcm.
116 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
118 struct snd_pcm_substream *substream;
121 for (stream = 0; stream < 2; stream++)
122 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
123 snd_pcm_lib_preallocate_free(substream);
125 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
127 #ifdef CONFIG_SND_VERBOSE_PROCFS
129 * read callback for prealloc proc file
131 * prints the current allocated size in kB.
133 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
134 struct snd_info_buffer *buffer)
136 struct snd_pcm_substream *substream = entry->private_data;
137 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
141 * read callback for prealloc_max proc file
143 * prints the maximum allowed size in kB.
145 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
146 struct snd_info_buffer *buffer)
148 struct snd_pcm_substream *substream = entry->private_data;
149 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
153 * write callback for prealloc proc file
155 * accepts the preallocation size in kB.
157 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
158 struct snd_info_buffer *buffer)
160 struct snd_pcm_substream *substream = entry->private_data;
161 struct snd_card *card = substream->pcm->card;
162 char line[64], str[64];
164 struct snd_dma_buffer new_dmab;
166 if (substream->runtime) {
167 buffer->error = -EBUSY;
170 if (!snd_info_get_line(buffer, line, sizeof(line))) {
171 snd_info_get_str(str, line, sizeof(str));
172 size = simple_strtoul(str, NULL, 10) * 1024;
173 if ((size != 0 && size < 8192) || size > substream->dma_max) {
174 buffer->error = -EINVAL;
177 if (substream->dma_buffer.bytes == size)
179 memset(&new_dmab, 0, sizeof(new_dmab));
180 new_dmab.dev = substream->dma_buffer.dev;
182 if (do_alloc_pages(card,
183 substream->dma_buffer.dev.type,
184 substream->dma_buffer.dev.dev,
185 size, &new_dmab) < 0) {
186 buffer->error = -ENOMEM;
189 substream->buffer_bytes_max = size;
191 substream->buffer_bytes_max = UINT_MAX;
193 if (substream->dma_buffer.area)
194 do_free_pages(card, &substream->dma_buffer);
195 substream->dma_buffer = new_dmab;
197 buffer->error = -EINVAL;
201 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
203 struct snd_info_entry *entry;
205 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
206 substream->proc_root);
208 snd_info_set_text_ops(entry, substream,
209 snd_pcm_lib_preallocate_proc_read);
210 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
213 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
214 substream->proc_root);
216 snd_info_set_text_ops(entry, substream,
217 snd_pcm_lib_preallocate_max_proc_read);
220 #else /* !CONFIG_SND_VERBOSE_PROCFS */
221 #define preallocate_info_init(s)
222 #endif /* CONFIG_SND_VERBOSE_PROCFS */
225 * pre-allocate the buffer and create a proc file for the substream
227 static void preallocate_pages(struct snd_pcm_substream *substream,
228 int type, struct device *data,
229 size_t size, size_t max, bool managed)
231 if (snd_BUG_ON(substream->dma_buffer.dev.type))
234 substream->dma_buffer.dev.type = type;
235 substream->dma_buffer.dev.dev = data;
237 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
238 preallocate_pcm_pages(substream, size);
240 if (substream->dma_buffer.bytes > 0)
241 substream->buffer_bytes_max = substream->dma_buffer.bytes;
242 substream->dma_max = max;
244 preallocate_info_init(substream);
246 substream->managed_buffer_alloc = 1;
249 static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
250 void *data, size_t size, size_t max,
253 struct snd_pcm_substream *substream;
256 for (stream = 0; stream < 2; stream++)
257 for (substream = pcm->streams[stream].substream; substream;
258 substream = substream->next)
259 preallocate_pages(substream, type, data, size, max,
264 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
265 * @substream: the pcm substream instance
266 * @type: DMA type (SNDRV_DMA_TYPE_*)
267 * @data: DMA type dependent data
268 * @size: the requested pre-allocation size in bytes
269 * @max: the max. allowed pre-allocation size
271 * Do pre-allocation for the given DMA buffer type.
273 void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
274 int type, struct device *data,
275 size_t size, size_t max)
277 preallocate_pages(substream, type, data, size, max, false);
279 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
282 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
283 * @pcm: the pcm instance
284 * @type: DMA type (SNDRV_DMA_TYPE_*)
285 * @data: DMA type dependent data
286 * @size: the requested pre-allocation size in bytes
287 * @max: the max. allowed pre-allocation size
289 * Do pre-allocation to all substreams of the given pcm for the
290 * specified DMA type.
292 void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
293 int type, void *data,
294 size_t size, size_t max)
296 preallocate_pages_for_all(pcm, type, data, size, max, false);
298 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
301 * snd_pcm_set_managed_buffer - set up buffer management for a substream
302 * @substream: the pcm substream instance
303 * @type: DMA type (SNDRV_DMA_TYPE_*)
304 * @data: DMA type dependent data
305 * @size: the requested pre-allocation size in bytes
306 * @max: the max. allowed pre-allocation size
308 * Do pre-allocation for the given DMA buffer type, and set the managed
309 * buffer allocation mode to the given substream.
310 * In this mode, PCM core will allocate a buffer automatically before PCM
311 * hw_params ops call, and release the buffer after PCM hw_free ops call
312 * as well, so that the driver doesn't need to invoke the allocation and
313 * the release explicitly in its callback.
314 * When a buffer is actually allocated before the PCM hw_params call, it
315 * turns on the runtime buffer_changed flag for drivers changing their h/w
316 * parameters accordingly.
318 void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
319 struct device *data, size_t size, size_t max)
321 preallocate_pages(substream, type, data, size, max, true);
323 EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
326 * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
328 * @pcm: the pcm instance
329 * @type: DMA type (SNDRV_DMA_TYPE_*)
330 * @data: DMA type dependent data
331 * @size: the requested pre-allocation size in bytes
332 * @max: the max. allowed pre-allocation size
334 * Do pre-allocation to all substreams of the given pcm for the specified DMA
335 * type and size, and set the managed_buffer_alloc flag to each substream.
337 void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
339 size_t size, size_t max)
341 preallocate_pages_for_all(pcm, type, data, size, max, true);
343 EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
345 #ifdef CONFIG_SND_DMA_SGBUF
347 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
348 * @substream: the pcm substream instance
349 * @offset: the buffer offset
351 * Used as the page callback of PCM ops.
353 * Return: The page struct at the given buffer offset. %NULL on failure.
355 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
357 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
359 unsigned int idx = offset >> PAGE_SHIFT;
360 if (idx >= (unsigned int)sgbuf->pages)
362 return sgbuf->page_table[idx];
364 #endif /* CONFIG_SND_DMA_SGBUF */
367 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
368 * @substream: the substream to allocate the DMA buffer to
369 * @size: the requested buffer size in bytes
371 * Allocates the DMA buffer on the BUS type given earlier to
372 * snd_pcm_lib_preallocate_xxx_pages().
374 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
377 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
379 struct snd_card *card = substream->pcm->card;
380 struct snd_pcm_runtime *runtime;
381 struct snd_dma_buffer *dmab = NULL;
383 if (PCM_RUNTIME_CHECK(substream))
385 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
386 SNDRV_DMA_TYPE_UNKNOWN))
388 runtime = substream->runtime;
390 if (runtime->dma_buffer_p) {
391 /* perphaps, we might free the large DMA memory region
392 to save some space here, but the actual solution
393 costs us less time */
394 if (runtime->dma_buffer_p->bytes >= size) {
395 runtime->dma_bytes = size;
396 return 0; /* ok, do not change */
398 snd_pcm_lib_free_pages(substream);
400 if (substream->dma_buffer.area != NULL &&
401 substream->dma_buffer.bytes >= size) {
402 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
404 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
407 dmab->dev = substream->dma_buffer.dev;
408 if (do_alloc_pages(card,
409 substream->dma_buffer.dev.type,
410 substream->dma_buffer.dev.dev,
416 snd_pcm_set_runtime_buffer(substream, dmab);
417 runtime->dma_bytes = size;
418 return 1; /* area was changed */
420 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
423 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
424 * @substream: the substream to release the DMA buffer
426 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
428 * Return: Zero if successful, or a negative error code on failure.
430 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
432 struct snd_card *card = substream->pcm->card;
433 struct snd_pcm_runtime *runtime;
435 if (PCM_RUNTIME_CHECK(substream))
437 runtime = substream->runtime;
438 if (runtime->dma_area == NULL)
440 if (runtime->dma_buffer_p != &substream->dma_buffer) {
441 /* it's a newly allocated buffer. release it now. */
442 do_free_pages(card, runtime->dma_buffer_p);
443 kfree(runtime->dma_buffer_p);
445 snd_pcm_set_runtime_buffer(substream, NULL);
448 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
450 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
451 size_t size, gfp_t gfp_flags)
453 struct snd_pcm_runtime *runtime;
455 if (PCM_RUNTIME_CHECK(substream))
457 runtime = substream->runtime;
458 if (runtime->dma_area) {
459 if (runtime->dma_bytes >= size)
460 return 0; /* already large enough */
461 vfree(runtime->dma_area);
463 runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL);
464 if (!runtime->dma_area)
466 runtime->dma_bytes = size;
469 EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
472 * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
473 * @substream: the substream with a buffer allocated by
474 * snd_pcm_lib_alloc_vmalloc_buffer()
476 * Return: Zero if successful, or a negative error code on failure.
478 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
480 struct snd_pcm_runtime *runtime;
482 if (PCM_RUNTIME_CHECK(substream))
484 runtime = substream->runtime;
485 vfree(runtime->dma_area);
486 runtime->dma_area = NULL;
489 EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
492 * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
493 * @substream: the substream with a buffer allocated by
494 * snd_pcm_lib_alloc_vmalloc_buffer()
495 * @offset: offset in the buffer
497 * This function is to be used as the page callback in the PCM ops.
499 * Return: The page struct, or %NULL on failure.
501 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
502 unsigned long offset)
504 return vmalloc_to_page(substream->runtime->dma_area + offset);
506 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);