ALSA: pcm: Use SG-buffer only when direct DMA is available
[linux-2.6-block.git] / sound / core / pcm_memory.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Digital Audio (PCM) abstract layer
c1017a4c 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
5 */
6
6cbbfe1c 7#include <linux/io.h>
1da177e4
LT
8#include <linux/time.h>
9#include <linux/init.h>
5a0e3ad6 10#include <linux/slab.h>
1da177e4 11#include <linux/moduleparam.h>
ee7c343c 12#include <linux/vmalloc.h>
d81a6d71 13#include <linux/export.h>
3ad796cb 14#include <linux/dma-mapping.h>
1da177e4
LT
15#include <sound/core.h>
16#include <sound/pcm.h>
17#include <sound/info.h>
18#include <sound/initval.h>
0821fd77 19#include "pcm_local.h"
1da177e4
LT
20
21static int preallocate_dma = 1;
22module_param(preallocate_dma, int, 0444);
23MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
24
25static int maximum_substreams = 4;
26module_param(maximum_substreams, int, 0444);
27MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
28
29static const size_t snd_minimum_buffer = 16384;
30
d4cfb30f
TI
31static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
32module_param(max_alloc_per_card, ulong, 0644);
33MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
34
35static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
36 size_t size, struct snd_dma_buffer *dmab)
37{
38 int err;
39
40 if (max_alloc_per_card &&
41 card->total_pcm_alloc_bytes + size > max_alloc_per_card)
42 return -ENOMEM;
3ad796cb
TI
43
44 if (IS_ENABLED(CONFIG_SND_DMA_SGBUF) &&
45 (type == SNDRV_DMA_TYPE_DEV_SG || type == SNDRV_DMA_TYPE_DEV_UC_SG) &&
46 !dma_is_direct(get_dma_ops(dev))) {
47 /* mutate to continuous page allocation */
48 dev_dbg(dev, "Use continuous page allocator\n");
49 if (type == SNDRV_DMA_TYPE_DEV_SG)
50 type = SNDRV_DMA_TYPE_DEV;
51 else
52 type = SNDRV_DMA_TYPE_DEV_UC;
53 }
54
d4cfb30f
TI
55 err = snd_dma_alloc_pages(type, dev, size, dmab);
56 if (!err) {
57 mutex_lock(&card->memory_mutex);
58 card->total_pcm_alloc_bytes += dmab->bytes;
59 mutex_unlock(&card->memory_mutex);
60 }
61 return err;
62}
63
64static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
65{
66 if (!dmab->area)
67 return;
68 mutex_lock(&card->memory_mutex);
69 WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes);
70 card->total_pcm_alloc_bytes -= dmab->bytes;
71 mutex_unlock(&card->memory_mutex);
72 snd_dma_free_pages(dmab);
73 dmab->area = NULL;
74}
1da177e4
LT
75
76/*
77 * try to allocate as the large pages as possible.
78 * stores the resultant memory size in *res_size.
79 *
80 * the minimum size is snd_minimum_buffer. it should be power of 2.
81 */
877211f5 82static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
1da177e4
LT
83{
84 struct snd_dma_buffer *dmab = &substream->dma_buffer;
d4cfb30f 85 struct snd_card *card = substream->pcm->card;
6ab08ced 86 size_t orig_size = size;
1da177e4
LT
87 int err;
88
1da177e4 89 do {
d4cfb30f
TI
90 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
91 size, dmab);
92 if (err != -ENOMEM)
93 return err;
1da177e4
LT
94 size >>= 1;
95 } while (size >= snd_minimum_buffer);
96 dmab->bytes = 0; /* tell error */
6ab08ced
TI
97 pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
98 substream->pcm->card->number, substream->pcm->device,
99 substream->stream ? 'c' : 'p', substream->number,
100 substream->pcm->name, orig_size);
1da177e4
LT
101 return 0;
102}
103
104/*
105 * release the preallocated buffer if not yet done.
106 */
877211f5 107static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
1da177e4 108{
d4cfb30f 109 do_free_pages(substream->pcm->card, &substream->dma_buffer);
1da177e4
LT
110}
111
112/**
113 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
114 * @substream: the pcm substream instance
115 *
116 * Releases the pre-allocated buffer of the given substream.
1da177e4 117 */
bb580602 118void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
1da177e4
LT
119{
120 snd_pcm_lib_preallocate_dma_free(substream);
1da177e4
LT
121}
122
123/**
124 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
125 * @pcm: the pcm instance
126 *
127 * Releases all the pre-allocated buffers on the given pcm.
1da177e4 128 */
bb580602 129void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
1da177e4 130{
877211f5 131 struct snd_pcm_substream *substream;
1da177e4
LT
132 int stream;
133
134 for (stream = 0; stream < 2; stream++)
135 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
136 snd_pcm_lib_preallocate_free(substream);
1da177e4 137}
e88e8ae6
TI
138EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
139
b7d90a35 140#ifdef CONFIG_SND_VERBOSE_PROCFS
1da177e4
LT
141/*
142 * read callback for prealloc proc file
143 *
144 * prints the current allocated size in kB.
145 */
877211f5
TI
146static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
147 struct snd_info_buffer *buffer)
1da177e4 148{
877211f5 149 struct snd_pcm_substream *substream = entry->private_data;
1da177e4
LT
150 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
151}
152
c7132aeb
JK
153/*
154 * read callback for prealloc_max proc file
155 *
156 * prints the maximum allowed size in kB.
157 */
158static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
159 struct snd_info_buffer *buffer)
160{
161 struct snd_pcm_substream *substream = entry->private_data;
162 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
163}
164
1da177e4
LT
165/*
166 * write callback for prealloc proc file
167 *
168 * accepts the preallocation size in kB.
169 */
877211f5
TI
170static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
171 struct snd_info_buffer *buffer)
1da177e4 172{
877211f5 173 struct snd_pcm_substream *substream = entry->private_data;
d4cfb30f 174 struct snd_card *card = substream->pcm->card;
1da177e4
LT
175 char line[64], str[64];
176 size_t size;
177 struct snd_dma_buffer new_dmab;
178
179 if (substream->runtime) {
180 buffer->error = -EBUSY;
181 return;
182 }
183 if (!snd_info_get_line(buffer, line, sizeof(line))) {
184 snd_info_get_str(str, line, sizeof(str));
185 size = simple_strtoul(str, NULL, 10) * 1024;
186 if ((size != 0 && size < 8192) || size > substream->dma_max) {
187 buffer->error = -EINVAL;
188 return;
189 }
190 if (substream->dma_buffer.bytes == size)
191 return;
192 memset(&new_dmab, 0, sizeof(new_dmab));
193 new_dmab.dev = substream->dma_buffer.dev;
194 if (size > 0) {
d4cfb30f
TI
195 if (do_alloc_pages(card,
196 substream->dma_buffer.dev.type,
197 substream->dma_buffer.dev.dev,
198 size, &new_dmab) < 0) {
1da177e4
LT
199 buffer->error = -ENOMEM;
200 return;
201 }
202 substream->buffer_bytes_max = size;
203 } else {
204 substream->buffer_bytes_max = UINT_MAX;
205 }
206 if (substream->dma_buffer.area)
d4cfb30f 207 do_free_pages(card, &substream->dma_buffer);
1da177e4
LT
208 substream->dma_buffer = new_dmab;
209 } else {
210 buffer->error = -EINVAL;
211 }
212}
213
e28563cc 214static inline void preallocate_info_init(struct snd_pcm_substream *substream)
1da177e4 215{
877211f5 216 struct snd_info_entry *entry;
1da177e4 217
a8d14981
TI
218 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
219 substream->proc_root);
220 if (entry) {
221 snd_info_set_text_ops(entry, substream,
222 snd_pcm_lib_preallocate_proc_read);
1da177e4 223 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
6a73cf46 224 entry->mode |= 0200;
c7132aeb 225 }
a8d14981
TI
226 entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
227 substream->proc_root);
228 if (entry)
229 snd_info_set_text_ops(entry, substream,
230 snd_pcm_lib_preallocate_max_proc_read);
e28563cc
TI
231}
232
b7d90a35 233#else /* !CONFIG_SND_VERBOSE_PROCFS */
e28563cc 234#define preallocate_info_init(s)
b7d90a35 235#endif /* CONFIG_SND_VERBOSE_PROCFS */
e28563cc
TI
236
237/*
238 * pre-allocate the buffer and create a proc file for the substream
239 */
0dba808e
TI
240static void preallocate_pages(struct snd_pcm_substream *substream,
241 int type, struct device *data,
242 size_t size, size_t max, bool managed)
e28563cc 243{
0dba808e
TI
244 if (snd_BUG_ON(substream->dma_buffer.dev.type))
245 return;
246
247 substream->dma_buffer.dev.type = type;
248 substream->dma_buffer.dev.dev = data;
e28563cc
TI
249
250 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
251 preallocate_pcm_pages(substream, size);
252
253 if (substream->dma_buffer.bytes > 0)
254 substream->buffer_bytes_max = substream->dma_buffer.bytes;
255 substream->dma_max = max;
d3978991
TI
256 if (max > 0)
257 preallocate_info_init(substream);
0dba808e
TI
258 if (managed)
259 substream->managed_buffer_alloc = 1;
1da177e4
LT
260}
261
0dba808e
TI
262static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
263 void *data, size_t size, size_t max,
264 bool managed)
265{
266 struct snd_pcm_substream *substream;
267 int stream;
268
269 for (stream = 0; stream < 2; stream++)
270 for (substream = pcm->streams[stream].substream; substream;
271 substream = substream->next)
272 preallocate_pages(substream, type, data, size, max,
273 managed);
274}
1da177e4
LT
275
276/**
277 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
278 * @substream: the pcm substream instance
279 * @type: DMA type (SNDRV_DMA_TYPE_*)
25985edc 280 * @data: DMA type dependent data
1da177e4
LT
281 * @size: the requested pre-allocation size in bytes
282 * @max: the max. allowed pre-allocation size
283 *
284 * Do pre-allocation for the given DMA buffer type.
1da177e4 285 */
bb580602 286void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
1da177e4
LT
287 int type, struct device *data,
288 size_t size, size_t max)
289{
0dba808e 290 preallocate_pages(substream, type, data, size, max, false);
1da177e4 291}
e88e8ae6
TI
292EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
293
1da177e4 294/**
25985edc 295 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
df8db936 296 * @pcm: the pcm instance
1da177e4 297 * @type: DMA type (SNDRV_DMA_TYPE_*)
25985edc 298 * @data: DMA type dependent data
1da177e4
LT
299 * @size: the requested pre-allocation size in bytes
300 * @max: the max. allowed pre-allocation size
301 *
302 * Do pre-allocation to all substreams of the given pcm for the
303 * specified DMA type.
1da177e4 304 */
bb580602 305void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
1da177e4
LT
306 int type, void *data,
307 size_t size, size_t max)
308{
0dba808e 309 preallocate_pages_for_all(pcm, type, data, size, max, false);
1da177e4 310}
e88e8ae6
TI
311EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
312
0dba808e
TI
313/**
314 * snd_pcm_set_managed_buffer - set up buffer management for a substream
315 * @substream: the pcm substream 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
320 *
321 * Do pre-allocation for the given DMA buffer type, and set the managed
322 * buffer allocation mode to the given substream.
323 * In this mode, PCM core will allocate a buffer automatically before PCM
324 * hw_params ops call, and release the buffer after PCM hw_free ops call
325 * as well, so that the driver doesn't need to invoke the allocation and
326 * the release explicitly in its callback.
327 * When a buffer is actually allocated before the PCM hw_params call, it
328 * turns on the runtime buffer_changed flag for drivers changing their h/w
329 * parameters accordingly.
330 */
331void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
332 struct device *data, size_t size, size_t max)
333{
334 preallocate_pages(substream, type, data, size, max, true);
335}
336EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
337
338/**
339 * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
340 * for all substreams
341 * @pcm: the pcm instance
342 * @type: DMA type (SNDRV_DMA_TYPE_*)
343 * @data: DMA type dependent data
344 * @size: the requested pre-allocation size in bytes
345 * @max: the max. allowed pre-allocation size
346 *
347 * Do pre-allocation to all substreams of the given pcm for the specified DMA
348 * type and size, and set the managed_buffer_alloc flag to each substream.
349 */
350void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
351 struct device *data,
352 size_t size, size_t max)
353{
354 preallocate_pages_for_all(pcm, type, data, size, max, true);
355}
356EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
357
cc6a8acd 358#ifdef CONFIG_SND_DMA_SGBUF
fc7af6bc 359/*
1da177e4
LT
360 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
361 * @substream: the pcm substream instance
362 * @offset: the buffer offset
363 *
1da177e4 364 * Used as the page callback of PCM ops.
eb7c06e8
YB
365 *
366 * Return: The page struct at the given buffer offset. %NULL on failure.
1da177e4 367 */
877211f5 368struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
1da177e4
LT
369{
370 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
371
372 unsigned int idx = offset >> PAGE_SHIFT;
373 if (idx >= (unsigned int)sgbuf->pages)
374 return NULL;
375 return sgbuf->page_table[idx];
376}
cc6a8acd 377#endif /* CONFIG_SND_DMA_SGBUF */
51e9f2e6 378
1da177e4
LT
379/**
380 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
381 * @substream: the substream to allocate the DMA buffer to
382 * @size: the requested buffer size in bytes
383 *
384 * Allocates the DMA buffer on the BUS type given earlier to
385 * snd_pcm_lib_preallocate_xxx_pages().
386 *
eb7c06e8 387 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
1da177e4
LT
388 * code on failure.
389 */
877211f5 390int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
1da177e4 391{
d4cfb30f 392 struct snd_card *card = substream->pcm->card;
877211f5 393 struct snd_pcm_runtime *runtime;
1da177e4
LT
394 struct snd_dma_buffer *dmab = NULL;
395
7eaa943c
TI
396 if (PCM_RUNTIME_CHECK(substream))
397 return -EINVAL;
398 if (snd_BUG_ON(substream->dma_buffer.dev.type ==
399 SNDRV_DMA_TYPE_UNKNOWN))
400 return -EINVAL;
1da177e4 401 runtime = substream->runtime;
1da177e4
LT
402
403 if (runtime->dma_buffer_p) {
404 /* perphaps, we might free the large DMA memory region
405 to save some space here, but the actual solution
406 costs us less time */
407 if (runtime->dma_buffer_p->bytes >= size) {
408 runtime->dma_bytes = size;
409 return 0; /* ok, do not change */
410 }
411 snd_pcm_lib_free_pages(substream);
412 }
877211f5
TI
413 if (substream->dma_buffer.area != NULL &&
414 substream->dma_buffer.bytes >= size) {
1da177e4
LT
415 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
416 } else {
ca2c0966 417 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
1da177e4
LT
418 if (! dmab)
419 return -ENOMEM;
420 dmab->dev = substream->dma_buffer.dev;
d4cfb30f
TI
421 if (do_alloc_pages(card,
422 substream->dma_buffer.dev.type,
423 substream->dma_buffer.dev.dev,
424 size, dmab) < 0) {
1da177e4
LT
425 kfree(dmab);
426 return -ENOMEM;
427 }
428 }
429 snd_pcm_set_runtime_buffer(substream, dmab);
430 runtime->dma_bytes = size;
431 return 1; /* area was changed */
432}
e88e8ae6
TI
433EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
434
1da177e4
LT
435/**
436 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
437 * @substream: the substream to release the DMA buffer
438 *
439 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
440 *
eb7c06e8 441 * Return: Zero if successful, or a negative error code on failure.
1da177e4 442 */
877211f5 443int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
1da177e4 444{
d4cfb30f 445 struct snd_card *card = substream->pcm->card;
877211f5 446 struct snd_pcm_runtime *runtime;
1da177e4 447
7eaa943c
TI
448 if (PCM_RUNTIME_CHECK(substream))
449 return -EINVAL;
1da177e4 450 runtime = substream->runtime;
1da177e4
LT
451 if (runtime->dma_area == NULL)
452 return 0;
453 if (runtime->dma_buffer_p != &substream->dma_buffer) {
454 /* it's a newly allocated buffer. release it now. */
d4cfb30f 455 do_free_pages(card, runtime->dma_buffer_p);
1da177e4
LT
456 kfree(runtime->dma_buffer_p);
457 }
458 snd_pcm_set_runtime_buffer(substream, NULL);
459 return 0;
460}
e88e8ae6 461EXPORT_SYMBOL(snd_pcm_lib_free_pages);
681b84e1
CL
462
463int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
464 size_t size, gfp_t gfp_flags)
465{
466 struct snd_pcm_runtime *runtime;
467
468 if (PCM_RUNTIME_CHECK(substream))
469 return -EINVAL;
470 runtime = substream->runtime;
471 if (runtime->dma_area) {
472 if (runtime->dma_bytes >= size)
473 return 0; /* already large enough */
474 vfree(runtime->dma_area);
475 }
88dca4ca 476 runtime->dma_area = __vmalloc(size, gfp_flags);
681b84e1
CL
477 if (!runtime->dma_area)
478 return -ENOMEM;
479 runtime->dma_bytes = size;
480 return 1;
481}
482EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
483
484/**
485 * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
486 * @substream: the substream with a buffer allocated by
487 * snd_pcm_lib_alloc_vmalloc_buffer()
eb7c06e8
YB
488 *
489 * Return: Zero if successful, or a negative error code on failure.
681b84e1
CL
490 */
491int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
492{
493 struct snd_pcm_runtime *runtime;
494
495 if (PCM_RUNTIME_CHECK(substream))
496 return -EINVAL;
497 runtime = substream->runtime;
498 vfree(runtime->dma_area);
499 runtime->dma_area = NULL;
500 return 0;
501}
502EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
503
504/**
505 * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
506 * @substream: the substream with a buffer allocated by
507 * snd_pcm_lib_alloc_vmalloc_buffer()
508 * @offset: offset in the buffer
509 *
510 * This function is to be used as the page callback in the PCM ops.
eb7c06e8
YB
511 *
512 * Return: The page struct, or %NULL on failure.
681b84e1
CL
513 */
514struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
515 unsigned long offset)
516{
517 return vmalloc_to_page(substream->runtime->dma_area + offset);
518}
519EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);