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