do not shrink nbcongwin below mss
[cor.git] / sound / core / pcm_memory.c
blobd4702cc1d3766080ad635132bfe18fc01c1e3444
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 */
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"
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;
32 * try to allocate as the large pages as possible.
33 * stores the resultant memory size in *res_size.
35 * the minimum size is snd_minimum_buffer. it should be power of 2.
37 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
39 struct snd_dma_buffer *dmab = &substream->dma_buffer;
40 size_t orig_size = size;
41 int err;
43 do {
44 if ((err = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev,
45 size, dmab)) < 0) {
46 if (err != -ENOMEM)
47 return err; /* fatal error */
48 } else
49 return 0;
50 size >>= 1;
51 } while (size >= snd_minimum_buffer);
52 dmab->bytes = 0; /* tell error */
53 pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
54 substream->pcm->card->number, substream->pcm->device,
55 substream->stream ? 'c' : 'p', substream->number,
56 substream->pcm->name, orig_size);
57 return 0;
61 * release the preallocated buffer if not yet done.
63 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
65 if (substream->dma_buffer.area == NULL)
66 return;
67 snd_dma_free_pages(&substream->dma_buffer);
68 substream->dma_buffer.area = NULL;
71 /**
72 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
73 * @substream: the pcm substream instance
75 * Releases the pre-allocated buffer of the given substream.
77 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
79 snd_pcm_lib_preallocate_dma_free(substream);
82 /**
83 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
84 * @pcm: the pcm instance
86 * Releases all the pre-allocated buffers on the given pcm.
88 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
90 struct snd_pcm_substream *substream;
91 int stream;
93 for (stream = 0; stream < 2; stream++)
94 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
95 snd_pcm_lib_preallocate_free(substream);
97 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
99 #ifdef CONFIG_SND_VERBOSE_PROCFS
101 * read callback for prealloc proc file
103 * prints the current allocated size in kB.
105 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
106 struct snd_info_buffer *buffer)
108 struct snd_pcm_substream *substream = entry->private_data;
109 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
113 * read callback for prealloc_max proc file
115 * prints the maximum allowed size in kB.
117 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
118 struct snd_info_buffer *buffer)
120 struct snd_pcm_substream *substream = entry->private_data;
121 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
125 * write callback for prealloc proc file
127 * accepts the preallocation size in kB.
129 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
130 struct snd_info_buffer *buffer)
132 struct snd_pcm_substream *substream = entry->private_data;
133 char line[64], str[64];
134 size_t size;
135 struct snd_dma_buffer new_dmab;
137 if (substream->runtime) {
138 buffer->error = -EBUSY;
139 return;
141 if (!snd_info_get_line(buffer, line, sizeof(line))) {
142 snd_info_get_str(str, line, sizeof(str));
143 size = simple_strtoul(str, NULL, 10) * 1024;
144 if ((size != 0 && size < 8192) || size > substream->dma_max) {
145 buffer->error = -EINVAL;
146 return;
148 if (substream->dma_buffer.bytes == size)
149 return;
150 memset(&new_dmab, 0, sizeof(new_dmab));
151 new_dmab.dev = substream->dma_buffer.dev;
152 if (size > 0) {
153 if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
154 substream->dma_buffer.dev.dev,
155 size, &new_dmab) < 0) {
156 buffer->error = -ENOMEM;
157 return;
159 substream->buffer_bytes_max = size;
160 } else {
161 substream->buffer_bytes_max = UINT_MAX;
163 if (substream->dma_buffer.area)
164 snd_dma_free_pages(&substream->dma_buffer);
165 substream->dma_buffer = new_dmab;
166 } else {
167 buffer->error = -EINVAL;
171 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
173 struct snd_info_entry *entry;
175 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
176 substream->proc_root);
177 if (entry) {
178 snd_info_set_text_ops(entry, substream,
179 snd_pcm_lib_preallocate_proc_read);
180 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
181 entry->mode |= 0200;
183 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
184 substream->proc_root);
185 if (entry)
186 snd_info_set_text_ops(entry, substream,
187 snd_pcm_lib_preallocate_max_proc_read);
190 #else /* !CONFIG_SND_VERBOSE_PROCFS */
191 #define preallocate_info_init(s)
192 #endif /* CONFIG_SND_VERBOSE_PROCFS */
195 * pre-allocate the buffer and create a proc file for the substream
197 static void preallocate_pages(struct snd_pcm_substream *substream,
198 int type, struct device *data,
199 size_t size, size_t max, bool managed)
201 if (snd_BUG_ON(substream->dma_buffer.dev.type))
202 return;
204 substream->dma_buffer.dev.type = type;
205 substream->dma_buffer.dev.dev = data;
207 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
208 preallocate_pcm_pages(substream, size);
210 if (substream->dma_buffer.bytes > 0)
211 substream->buffer_bytes_max = substream->dma_buffer.bytes;
212 substream->dma_max = max;
213 if (max > 0)
214 preallocate_info_init(substream);
215 if (managed)
216 substream->managed_buffer_alloc = 1;
219 static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
220 void *data, size_t size, size_t max,
221 bool managed)
223 struct snd_pcm_substream *substream;
224 int stream;
226 for (stream = 0; stream < 2; stream++)
227 for (substream = pcm->streams[stream].substream; substream;
228 substream = substream->next)
229 preallocate_pages(substream, type, data, size, max,
230 managed);
234 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
235 * @substream: the pcm substream instance
236 * @type: DMA type (SNDRV_DMA_TYPE_*)
237 * @data: DMA type dependent data
238 * @size: the requested pre-allocation size in bytes
239 * @max: the max. allowed pre-allocation size
241 * Do pre-allocation for the given DMA buffer type.
243 void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
244 int type, struct device *data,
245 size_t size, size_t max)
247 preallocate_pages(substream, type, data, size, max, false);
249 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
252 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
253 * @pcm: the pcm instance
254 * @type: DMA type (SNDRV_DMA_TYPE_*)
255 * @data: DMA type dependent data
256 * @size: the requested pre-allocation size in bytes
257 * @max: the max. allowed pre-allocation size
259 * Do pre-allocation to all substreams of the given pcm for the
260 * specified DMA type.
262 void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
263 int type, void *data,
264 size_t size, size_t max)
266 preallocate_pages_for_all(pcm, type, data, size, max, false);
268 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
271 * snd_pcm_set_managed_buffer - set up buffer management for a substream
272 * @substream: the pcm substream instance
273 * @type: DMA type (SNDRV_DMA_TYPE_*)
274 * @data: DMA type dependent data
275 * @size: the requested pre-allocation size in bytes
276 * @max: the max. allowed pre-allocation size
278 * Do pre-allocation for the given DMA buffer type, and set the managed
279 * buffer allocation mode to the given substream.
280 * In this mode, PCM core will allocate a buffer automatically before PCM
281 * hw_params ops call, and release the buffer after PCM hw_free ops call
282 * as well, so that the driver doesn't need to invoke the allocation and
283 * the release explicitly in its callback.
284 * When a buffer is actually allocated before the PCM hw_params call, it
285 * turns on the runtime buffer_changed flag for drivers changing their h/w
286 * parameters accordingly.
288 void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
289 struct device *data, size_t size, size_t max)
291 preallocate_pages(substream, type, data, size, max, true);
293 EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
296 * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
297 * for all substreams
298 * @pcm: the pcm instance
299 * @type: DMA type (SNDRV_DMA_TYPE_*)
300 * @data: DMA type dependent data
301 * @size: the requested pre-allocation size in bytes
302 * @max: the max. allowed pre-allocation size
304 * Do pre-allocation to all substreams of the given pcm for the specified DMA
305 * type and size, and set the managed_buffer_alloc flag to each substream.
307 void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
308 struct device *data,
309 size_t size, size_t max)
311 preallocate_pages_for_all(pcm, type, data, size, max, true);
313 EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
315 #ifdef CONFIG_SND_DMA_SGBUF
317 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
318 * @substream: the pcm substream instance
319 * @offset: the buffer offset
321 * Used as the page callback of PCM ops.
323 * Return: The page struct at the given buffer offset. %NULL on failure.
325 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
327 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
329 unsigned int idx = offset >> PAGE_SHIFT;
330 if (idx >= (unsigned int)sgbuf->pages)
331 return NULL;
332 return sgbuf->page_table[idx];
334 #endif /* CONFIG_SND_DMA_SGBUF */
337 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
338 * @substream: the substream to allocate the DMA buffer to
339 * @size: the requested buffer size in bytes
341 * Allocates the DMA buffer on the BUS type given earlier to
342 * snd_pcm_lib_preallocate_xxx_pages().
344 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
345 * code on failure.
347 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
349 struct snd_pcm_runtime *runtime;
350 struct snd_dma_buffer *dmab = NULL;
352 if (PCM_RUNTIME_CHECK(substream))
353 return -EINVAL;
354 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
355 SNDRV_DMA_TYPE_UNKNOWN))
356 return -EINVAL;
357 runtime = substream->runtime;
359 if (runtime->dma_buffer_p) {
360 /* perphaps, we might free the large DMA memory region
361 to save some space here, but the actual solution
362 costs us less time */
363 if (runtime->dma_buffer_p->bytes >= size) {
364 runtime->dma_bytes = size;
365 return 0; /* ok, do not change */
367 snd_pcm_lib_free_pages(substream);
369 if (substream->dma_buffer.area != NULL &&
370 substream->dma_buffer.bytes >= size) {
371 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
372 } else {
373 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
374 if (! dmab)
375 return -ENOMEM;
376 dmab->dev = substream->dma_buffer.dev;
377 if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
378 substream->dma_buffer.dev.dev,
379 size, dmab) < 0) {
380 kfree(dmab);
381 return -ENOMEM;
384 snd_pcm_set_runtime_buffer(substream, dmab);
385 runtime->dma_bytes = size;
386 return 1; /* area was changed */
388 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
391 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
392 * @substream: the substream to release the DMA buffer
394 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
396 * Return: Zero if successful, or a negative error code on failure.
398 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
400 struct snd_pcm_runtime *runtime;
402 if (PCM_RUNTIME_CHECK(substream))
403 return -EINVAL;
404 runtime = substream->runtime;
405 if (runtime->dma_area == NULL)
406 return 0;
407 if (runtime->dma_buffer_p != &substream->dma_buffer) {
408 /* it's a newly allocated buffer. release it now. */
409 snd_dma_free_pages(runtime->dma_buffer_p);
410 kfree(runtime->dma_buffer_p);
412 snd_pcm_set_runtime_buffer(substream, NULL);
413 return 0;
415 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
417 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
418 size_t size, gfp_t gfp_flags)
420 struct snd_pcm_runtime *runtime;
422 if (PCM_RUNTIME_CHECK(substream))
423 return -EINVAL;
424 runtime = substream->runtime;
425 if (runtime->dma_area) {
426 if (runtime->dma_bytes >= size)
427 return 0; /* already large enough */
428 vfree(runtime->dma_area);
430 runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL);
431 if (!runtime->dma_area)
432 return -ENOMEM;
433 runtime->dma_bytes = size;
434 return 1;
436 EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
439 * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
440 * @substream: the substream with a buffer allocated by
441 * snd_pcm_lib_alloc_vmalloc_buffer()
443 * Return: Zero if successful, or a negative error code on failure.
445 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
447 struct snd_pcm_runtime *runtime;
449 if (PCM_RUNTIME_CHECK(substream))
450 return -EINVAL;
451 runtime = substream->runtime;
452 vfree(runtime->dma_area);
453 runtime->dma_area = NULL;
454 return 0;
456 EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
459 * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
460 * @substream: the substream with a buffer allocated by
461 * snd_pcm_lib_alloc_vmalloc_buffer()
462 * @offset: offset in the buffer
464 * This function is to be used as the page callback in the PCM ops.
466 * Return: The page struct, or %NULL on failure.
468 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
469 unsigned long offset)
471 return vmalloc_to_page(substream->runtime->dma_area + offset);
473 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);