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)
43 err = snd_dma_alloc_pages(type, dev, size, dmab);
45 mutex_lock(&card->memory_mutex);
46 card->total_pcm_alloc_bytes += dmab->bytes;
47 mutex_unlock(&card->memory_mutex);
52 static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
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);
65 * try to allocate as the large pages as possible.
66 * stores the resultant memory size in *res_size.
68 * the minimum size is snd_minimum_buffer. it should be power of 2.
70 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
72 struct snd_dma_buffer *dmab = &substream->dma_buffer;
73 struct snd_card *card = substream->pcm->card;
74 size_t orig_size = size;
78 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
83 } while (size >= snd_minimum_buffer);
84 dmab->bytes = 0; /* tell error */
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);
93 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
94 * @substream: the pcm substream instance
96 * Releases the pre-allocated buffer of the given substream.
98 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
100 do_free_pages(substream->pcm->card, &substream->dma_buffer);
104 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
105 * @pcm: the pcm instance
107 * Releases all the pre-allocated buffers on the given pcm.
109 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
111 struct snd_pcm_substream *substream;
114 for (stream = 0; stream < 2; stream++)
115 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
116 snd_pcm_lib_preallocate_free(substream);
118 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
120 #ifdef CONFIG_SND_VERBOSE_PROCFS
122 * read callback for prealloc proc file
124 * prints the current allocated size in kB.
126 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
127 struct snd_info_buffer *buffer)
129 struct snd_pcm_substream *substream = entry->private_data;
130 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
134 * read callback for prealloc_max proc file
136 * prints the maximum allowed size in kB.
138 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
139 struct snd_info_buffer *buffer)
141 struct snd_pcm_substream *substream = entry->private_data;
142 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
146 * write callback for prealloc proc file
148 * accepts the preallocation size in kB.
150 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
151 struct snd_info_buffer *buffer)
153 struct snd_pcm_substream *substream = entry->private_data;
154 struct snd_card *card = substream->pcm->card;
155 char line[64], str[64];
157 struct snd_dma_buffer new_dmab;
159 if (substream->runtime) {
160 buffer->error = -EBUSY;
163 if (!snd_info_get_line(buffer, line, sizeof(line))) {
164 snd_info_get_str(str, line, sizeof(str));
165 size = simple_strtoul(str, NULL, 10) * 1024;
166 if ((size != 0 && size < 8192) || size > substream->dma_max) {
167 buffer->error = -EINVAL;
170 if (substream->dma_buffer.bytes == size)
172 memset(&new_dmab, 0, sizeof(new_dmab));
173 new_dmab.dev = substream->dma_buffer.dev;
175 if (do_alloc_pages(card,
176 substream->dma_buffer.dev.type,
177 substream->dma_buffer.dev.dev,
178 size, &new_dmab) < 0) {
179 buffer->error = -ENOMEM;
182 substream->buffer_bytes_max = size;
184 substream->buffer_bytes_max = UINT_MAX;
186 if (substream->dma_buffer.area)
187 do_free_pages(card, &substream->dma_buffer);
188 substream->dma_buffer = new_dmab;
190 buffer->error = -EINVAL;
194 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
196 struct snd_info_entry *entry;
198 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
199 substream->proc_root);
201 snd_info_set_text_ops(entry, substream,
202 snd_pcm_lib_preallocate_proc_read);
203 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
206 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
207 substream->proc_root);
209 snd_info_set_text_ops(entry, substream,
210 snd_pcm_lib_preallocate_max_proc_read);
213 #else /* !CONFIG_SND_VERBOSE_PROCFS */
214 #define preallocate_info_init(s)
215 #endif /* CONFIG_SND_VERBOSE_PROCFS */
218 * pre-allocate the buffer and create a proc file for the substream
220 static void preallocate_pages(struct snd_pcm_substream *substream,
221 int type, struct device *data,
222 size_t size, size_t max, bool managed)
224 if (snd_BUG_ON(substream->dma_buffer.dev.type))
227 substream->dma_buffer.dev.type = type;
228 substream->dma_buffer.dev.dev = data;
230 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
231 preallocate_pcm_pages(substream, size);
233 if (substream->dma_buffer.bytes > 0)
234 substream->buffer_bytes_max = substream->dma_buffer.bytes;
235 substream->dma_max = max;
237 preallocate_info_init(substream);
239 substream->managed_buffer_alloc = 1;
242 static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
243 void *data, size_t size, size_t max,
246 struct snd_pcm_substream *substream;
249 for (stream = 0; stream < 2; stream++)
250 for (substream = pcm->streams[stream].substream; substream;
251 substream = substream->next)
252 preallocate_pages(substream, type, data, size, max,
257 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
258 * @substream: the pcm substream instance
259 * @type: DMA type (SNDRV_DMA_TYPE_*)
260 * @data: DMA type dependent data
261 * @size: the requested pre-allocation size in bytes
262 * @max: the max. allowed pre-allocation size
264 * Do pre-allocation for the given DMA buffer type.
266 void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
267 int type, struct device *data,
268 size_t size, size_t max)
270 preallocate_pages(substream, type, data, size, max, false);
272 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
275 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
276 * @pcm: the pcm instance
277 * @type: DMA type (SNDRV_DMA_TYPE_*)
278 * @data: DMA type dependent data
279 * @size: the requested pre-allocation size in bytes
280 * @max: the max. allowed pre-allocation size
282 * Do pre-allocation to all substreams of the given pcm for the
283 * specified DMA type.
285 void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
286 int type, void *data,
287 size_t size, size_t max)
289 preallocate_pages_for_all(pcm, type, data, size, max, false);
291 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
294 * snd_pcm_set_managed_buffer - set up buffer management for a substream
295 * @substream: the pcm substream instance
296 * @type: DMA type (SNDRV_DMA_TYPE_*)
297 * @data: DMA type dependent data
298 * @size: the requested pre-allocation size in bytes
299 * @max: the max. allowed pre-allocation size
301 * Do pre-allocation for the given DMA buffer type, and set the managed
302 * buffer allocation mode to the given substream.
303 * In this mode, PCM core will allocate a buffer automatically before PCM
304 * hw_params ops call, and release the buffer after PCM hw_free ops call
305 * as well, so that the driver doesn't need to invoke the allocation and
306 * the release explicitly in its callback.
307 * When a buffer is actually allocated before the PCM hw_params call, it
308 * turns on the runtime buffer_changed flag for drivers changing their h/w
309 * parameters accordingly.
311 void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
312 struct device *data, size_t size, size_t max)
314 preallocate_pages(substream, type, data, size, max, true);
316 EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
319 * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
321 * @pcm: the pcm instance
322 * @type: DMA type (SNDRV_DMA_TYPE_*)
323 * @data: DMA type dependent data
324 * @size: the requested pre-allocation size in bytes
325 * @max: the max. allowed pre-allocation size
327 * Do pre-allocation to all substreams of the given pcm for the specified DMA
328 * type and size, and set the managed_buffer_alloc flag to each substream.
330 void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
332 size_t size, size_t max)
334 preallocate_pages_for_all(pcm, type, data, size, max, true);
336 EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
338 #ifdef CONFIG_SND_DMA_SGBUF
340 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
341 * @substream: the pcm substream instance
342 * @offset: the buffer offset
344 * Used as the page callback of PCM ops.
346 * Return: The page struct at the given buffer offset. %NULL on failure.
348 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
350 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
352 unsigned int idx = offset >> PAGE_SHIFT;
353 if (idx >= (unsigned int)sgbuf->pages)
355 return sgbuf->page_table[idx];
357 #endif /* CONFIG_SND_DMA_SGBUF */
360 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
361 * @substream: the substream to allocate the DMA buffer to
362 * @size: the requested buffer size in bytes
364 * Allocates the DMA buffer on the BUS type given earlier to
365 * snd_pcm_lib_preallocate_xxx_pages().
367 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
370 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
372 struct snd_card *card;
373 struct snd_pcm_runtime *runtime;
374 struct snd_dma_buffer *dmab = NULL;
376 if (PCM_RUNTIME_CHECK(substream))
378 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
379 SNDRV_DMA_TYPE_UNKNOWN))
381 runtime = substream->runtime;
382 card = substream->pcm->card;
384 if (runtime->dma_buffer_p) {
385 /* perphaps, we might free the large DMA memory region
386 to save some space here, but the actual solution
387 costs us less time */
388 if (runtime->dma_buffer_p->bytes >= size) {
389 runtime->dma_bytes = size;
390 return 0; /* ok, do not change */
392 snd_pcm_lib_free_pages(substream);
394 if (substream->dma_buffer.area != NULL &&
395 substream->dma_buffer.bytes >= size) {
396 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
398 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
401 dmab->dev = substream->dma_buffer.dev;
402 if (do_alloc_pages(card,
403 substream->dma_buffer.dev.type,
404 substream->dma_buffer.dev.dev,
410 snd_pcm_set_runtime_buffer(substream, dmab);
411 runtime->dma_bytes = size;
412 return 1; /* area was changed */
414 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
417 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
418 * @substream: the substream to release the DMA buffer
420 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
422 * Return: Zero if successful, or a negative error code on failure.
424 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
426 struct snd_card *card = substream->pcm->card;
427 struct snd_pcm_runtime *runtime;
429 if (PCM_RUNTIME_CHECK(substream))
431 runtime = substream->runtime;
432 if (runtime->dma_area == NULL)
434 if (runtime->dma_buffer_p != &substream->dma_buffer) {
435 /* it's a newly allocated buffer. release it now. */
436 do_free_pages(card, runtime->dma_buffer_p);
437 kfree(runtime->dma_buffer_p);
439 snd_pcm_set_runtime_buffer(substream, NULL);
442 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
444 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
445 size_t size, gfp_t gfp_flags)
447 struct snd_pcm_runtime *runtime;
449 if (PCM_RUNTIME_CHECK(substream))
451 runtime = substream->runtime;
452 if (runtime->dma_area) {
453 if (runtime->dma_bytes >= size)
454 return 0; /* already large enough */
455 vfree(runtime->dma_area);
457 runtime->dma_area = __vmalloc(size, gfp_flags);
458 if (!runtime->dma_area)
460 runtime->dma_bytes = size;
463 EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
466 * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
467 * @substream: the substream with a buffer allocated by
468 * snd_pcm_lib_alloc_vmalloc_buffer()
470 * Return: Zero if successful, or a negative error code on failure.
472 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
474 struct snd_pcm_runtime *runtime;
476 if (PCM_RUNTIME_CHECK(substream))
478 runtime = substream->runtime;
479 vfree(runtime->dma_area);
480 runtime->dma_area = NULL;
483 EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
486 * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
487 * @substream: the substream with a buffer allocated by
488 * snd_pcm_lib_alloc_vmalloc_buffer()
489 * @offset: offset in the buffer
491 * This function is to be used as the page callback in the PCM ops.
493 * Return: The page struct, or %NULL on failure.
495 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
496 unsigned long offset)
498 return vmalloc_to_page(substream->runtime->dma_area + offset);
500 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);