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>
19 static int preallocate_dma = 1;
20 module_param(preallocate_dma, int, 0444);
21 MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
23 static int maximum_substreams = 4;
24 module_param(maximum_substreams, int, 0444);
25 MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
27 static const size_t snd_minimum_buffer = 16384;
29 static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
30 module_param(max_alloc_per_card, ulong, 0644);
31 MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
33 static void __update_allocated_size(struct snd_card *card, ssize_t bytes)
35 card->total_pcm_alloc_bytes += bytes;
38 static void update_allocated_size(struct snd_card *card, ssize_t bytes)
40 mutex_lock(&card->memory_mutex);
41 __update_allocated_size(card, bytes);
42 mutex_unlock(&card->memory_mutex);
45 static void decrease_allocated_size(struct snd_card *card, size_t bytes)
47 mutex_lock(&card->memory_mutex);
48 WARN_ON(card->total_pcm_alloc_bytes < bytes);
49 __update_allocated_size(card, -(ssize_t)bytes);
50 mutex_unlock(&card->memory_mutex);
53 static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
54 size_t size, struct snd_dma_buffer *dmab)
58 /* check and reserve the requested size */
59 mutex_lock(&card->memory_mutex);
60 if (max_alloc_per_card &&
61 card->total_pcm_alloc_bytes + size > max_alloc_per_card) {
62 mutex_unlock(&card->memory_mutex);
65 __update_allocated_size(card, size);
66 mutex_unlock(&card->memory_mutex);
68 err = snd_dma_alloc_pages(type, dev, size, dmab);
70 /* the actual allocation size might be bigger than requested,
71 * and we need to correct the account
73 if (dmab->bytes != size)
74 update_allocated_size(card, dmab->bytes - size);
76 /* take back on allocation failure */
77 decrease_allocated_size(card, size);
82 static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
86 decrease_allocated_size(card, dmab->bytes);
87 snd_dma_free_pages(dmab);
92 * try to allocate as the large pages as possible.
93 * stores the resultant memory size in *res_size.
95 * the minimum size is snd_minimum_buffer. it should be power of 2.
97 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
99 struct snd_dma_buffer *dmab = &substream->dma_buffer;
100 struct snd_card *card = substream->pcm->card;
101 size_t orig_size = size;
105 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
110 } while (size >= snd_minimum_buffer);
111 dmab->bytes = 0; /* tell error */
112 pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
113 substream->pcm->card->number, substream->pcm->device,
114 substream->stream ? 'c' : 'p', substream->number,
115 substream->pcm->name, orig_size);
120 * release the preallocated buffer if not yet done.
122 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
124 do_free_pages(substream->pcm->card, &substream->dma_buffer);
128 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
129 * @substream: the pcm substream instance
131 * Releases the pre-allocated buffer of the given substream.
133 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
135 snd_pcm_lib_preallocate_dma_free(substream);
139 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
140 * @pcm: the pcm instance
142 * Releases all the pre-allocated buffers on the given pcm.
144 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
146 struct snd_pcm_substream *substream;
149 for (stream = 0; stream < 2; stream++)
150 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
151 snd_pcm_lib_preallocate_free(substream);
153 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
155 #ifdef CONFIG_SND_VERBOSE_PROCFS
157 * read callback for prealloc proc file
159 * prints the current allocated size in kB.
161 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
162 struct snd_info_buffer *buffer)
164 struct snd_pcm_substream *substream = entry->private_data;
165 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
169 * read callback for prealloc_max proc file
171 * prints the maximum allowed size in kB.
173 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
174 struct snd_info_buffer *buffer)
176 struct snd_pcm_substream *substream = entry->private_data;
177 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
181 * write callback for prealloc proc file
183 * accepts the preallocation size in kB.
185 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
186 struct snd_info_buffer *buffer)
188 struct snd_pcm_substream *substream = entry->private_data;
189 struct snd_card *card = substream->pcm->card;
190 char line[64], str[64];
192 struct snd_dma_buffer new_dmab;
194 mutex_lock(&substream->pcm->open_mutex);
195 if (substream->runtime) {
196 buffer->error = -EBUSY;
199 if (!snd_info_get_line(buffer, line, sizeof(line))) {
200 snd_info_get_str(str, line, sizeof(str));
201 size = simple_strtoul(str, NULL, 10) * 1024;
202 if ((size != 0 && size < 8192) || size > substream->dma_max) {
203 buffer->error = -EINVAL;
206 if (substream->dma_buffer.bytes == size)
208 memset(&new_dmab, 0, sizeof(new_dmab));
209 new_dmab.dev = substream->dma_buffer.dev;
211 if (do_alloc_pages(card,
212 substream->dma_buffer.dev.type,
213 substream->dma_buffer.dev.dev,
214 size, &new_dmab) < 0) {
215 buffer->error = -ENOMEM;
218 substream->buffer_bytes_max = size;
220 substream->buffer_bytes_max = UINT_MAX;
222 if (substream->dma_buffer.area)
223 do_free_pages(card, &substream->dma_buffer);
224 substream->dma_buffer = new_dmab;
226 buffer->error = -EINVAL;
229 mutex_unlock(&substream->pcm->open_mutex);
232 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
234 struct snd_info_entry *entry;
236 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
237 substream->proc_root);
239 snd_info_set_text_ops(entry, substream,
240 snd_pcm_lib_preallocate_proc_read);
241 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
244 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
245 substream->proc_root);
247 snd_info_set_text_ops(entry, substream,
248 snd_pcm_lib_preallocate_max_proc_read);
251 #else /* !CONFIG_SND_VERBOSE_PROCFS */
252 #define preallocate_info_init(s)
253 #endif /* CONFIG_SND_VERBOSE_PROCFS */
256 * pre-allocate the buffer and create a proc file for the substream
258 static void snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream,
259 size_t size, size_t max)
262 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
263 preallocate_pcm_pages(substream, size);
265 if (substream->dma_buffer.bytes > 0)
266 substream->buffer_bytes_max = substream->dma_buffer.bytes;
267 substream->dma_max = max;
268 preallocate_info_init(substream);
273 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
274 * @substream: the pcm substream instance
275 * @type: DMA type (SNDRV_DMA_TYPE_*)
276 * @data: DMA type dependent data
277 * @size: the requested pre-allocation size in bytes
278 * @max: the max. allowed pre-allocation size
280 * Do pre-allocation for the given DMA buffer type.
282 void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
283 int type, struct device *data,
284 size_t size, size_t max)
286 substream->dma_buffer.dev.type = type;
287 substream->dma_buffer.dev.dev = data;
288 snd_pcm_lib_preallocate_pages1(substream, size, max);
290 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
293 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
294 * @pcm: the pcm instance
295 * @type: DMA type (SNDRV_DMA_TYPE_*)
296 * @data: DMA type dependent data
297 * @size: the requested pre-allocation size in bytes
298 * @max: the max. allowed pre-allocation size
300 * Do pre-allocation to all substreams of the given pcm for the
301 * specified DMA type.
303 void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
304 int type, void *data,
305 size_t size, size_t max)
307 struct snd_pcm_substream *substream;
310 for (stream = 0; stream < 2; stream++)
311 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
312 snd_pcm_lib_preallocate_pages(substream, type, data, size, max);
314 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
316 #ifdef CONFIG_SND_DMA_SGBUF
318 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
319 * @substream: the pcm substream instance
320 * @offset: the buffer offset
322 * Used as the page callback of PCM ops.
324 * Return: The page struct at the given buffer offset. %NULL on failure.
326 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
328 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
330 unsigned int idx = offset >> PAGE_SHIFT;
331 if (idx >= (unsigned int)sgbuf->pages)
333 return sgbuf->page_table[idx];
335 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
336 #endif /* CONFIG_SND_DMA_SGBUF */
339 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
340 * @substream: the substream to allocate the DMA buffer to
341 * @size: the requested buffer size in bytes
343 * Allocates the DMA buffer on the BUS type given earlier to
344 * snd_pcm_lib_preallocate_xxx_pages().
346 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
349 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
351 struct snd_card *card = substream->pcm->card;
352 struct snd_pcm_runtime *runtime;
353 struct snd_dma_buffer *dmab = NULL;
355 if (PCM_RUNTIME_CHECK(substream))
357 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
358 SNDRV_DMA_TYPE_UNKNOWN))
360 runtime = substream->runtime;
362 if (runtime->dma_buffer_p) {
363 /* perphaps, we might free the large DMA memory region
364 to save some space here, but the actual solution
365 costs us less time */
366 if (runtime->dma_buffer_p->bytes >= size) {
367 runtime->dma_bytes = size;
368 return 0; /* ok, do not change */
370 snd_pcm_lib_free_pages(substream);
372 if (substream->dma_buffer.area != NULL &&
373 substream->dma_buffer.bytes >= size) {
374 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
376 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
379 dmab->dev = substream->dma_buffer.dev;
380 if (do_alloc_pages(card,
381 substream->dma_buffer.dev.type,
382 substream->dma_buffer.dev.dev,
388 snd_pcm_set_runtime_buffer(substream, dmab);
389 runtime->dma_bytes = size;
390 return 1; /* area was changed */
392 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
395 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
396 * @substream: the substream to release the DMA buffer
398 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
400 * Return: Zero if successful, or a negative error code on failure.
402 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
404 struct snd_pcm_runtime *runtime;
406 if (PCM_RUNTIME_CHECK(substream))
408 runtime = substream->runtime;
409 if (runtime->dma_area == NULL)
411 if (runtime->dma_buffer_p != &substream->dma_buffer) {
412 struct snd_card *card = substream->pcm->card;
414 /* it's a newly allocated buffer. release it now. */
415 do_free_pages(card, runtime->dma_buffer_p);
416 kfree(runtime->dma_buffer_p);
418 snd_pcm_set_runtime_buffer(substream, NULL);
421 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
423 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
424 size_t size, gfp_t gfp_flags)
426 struct snd_pcm_runtime *runtime;
428 if (PCM_RUNTIME_CHECK(substream))
430 runtime = substream->runtime;
431 if (runtime->dma_area) {
432 if (runtime->dma_bytes >= size)
433 return 0; /* already large enough */
434 vfree(runtime->dma_area);
436 runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL);
437 if (!runtime->dma_area)
439 runtime->dma_bytes = size;
442 EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
445 * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
446 * @substream: the substream with a buffer allocated by
447 * snd_pcm_lib_alloc_vmalloc_buffer()
449 * Return: Zero if successful, or a negative error code on failure.
451 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
453 struct snd_pcm_runtime *runtime;
455 if (PCM_RUNTIME_CHECK(substream))
457 runtime = substream->runtime;
458 vfree(runtime->dma_area);
459 runtime->dma_area = NULL;
462 EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
465 * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
466 * @substream: the substream with a buffer allocated by
467 * snd_pcm_lib_alloc_vmalloc_buffer()
468 * @offset: offset in the buffer
470 * This function is to be used as the page callback in the PCM ops.
472 * Return: The page struct, or %NULL on failure.
474 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
475 unsigned long offset)
477 return vmalloc_to_page(substream->runtime->dma_area + offset);
479 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);