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