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