xref: /linux/sound/core/pcm_memory.c (revision 9a6b55ac)
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 
31 /*
32  * try to allocate as the large pages as possible.
33  * stores the resultant memory size in *res_size.
34  *
35  * the minimum size is snd_minimum_buffer.  it should be power of 2.
36  */
37 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
38 {
39 	struct snd_dma_buffer *dmab = &substream->dma_buffer;
40 	size_t orig_size = size;
41 	int err;
42 
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;
58 }
59 
60 /*
61  * release the preallocated buffer if not yet done.
62  */
63 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
64 {
65 	if (substream->dma_buffer.area == NULL)
66 		return;
67 	snd_dma_free_pages(&substream->dma_buffer);
68 	substream->dma_buffer.area = NULL;
69 }
70 
71 /**
72  * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
73  * @substream: the pcm substream instance
74  *
75  * Releases the pre-allocated buffer of the given substream.
76  */
77 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
78 {
79 	snd_pcm_lib_preallocate_dma_free(substream);
80 }
81 
82 /**
83  * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
84  * @pcm: the pcm instance
85  *
86  * Releases all the pre-allocated buffers on the given pcm.
87  */
88 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
89 {
90 	struct snd_pcm_substream *substream;
91 	int stream;
92 
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);
96 }
97 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
98 
99 #ifdef CONFIG_SND_VERBOSE_PROCFS
100 /*
101  * read callback for prealloc proc file
102  *
103  * prints the current allocated size in kB.
104  */
105 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
106 					      struct snd_info_buffer *buffer)
107 {
108 	struct snd_pcm_substream *substream = entry->private_data;
109 	snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
110 }
111 
112 /*
113  * read callback for prealloc_max proc file
114  *
115  * prints the maximum allowed size in kB.
116  */
117 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
118 						  struct snd_info_buffer *buffer)
119 {
120 	struct snd_pcm_substream *substream = entry->private_data;
121 	snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
122 }
123 
124 /*
125  * write callback for prealloc proc file
126  *
127  * accepts the preallocation size in kB.
128  */
129 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
130 					       struct snd_info_buffer *buffer)
131 {
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;
136 
137 	if (substream->runtime) {
138 		buffer->error = -EBUSY;
139 		return;
140 	}
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;
147 		}
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;
158 			}
159 			substream->buffer_bytes_max = size;
160 		} else {
161 			substream->buffer_bytes_max = UINT_MAX;
162 		}
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;
168 	}
169 }
170 
171 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
172 {
173 	struct snd_info_entry *entry;
174 
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;
182 	}
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);
188 }
189 
190 #else /* !CONFIG_SND_VERBOSE_PROCFS */
191 #define preallocate_info_init(s)
192 #endif /* CONFIG_SND_VERBOSE_PROCFS */
193 
194 /*
195  * pre-allocate the buffer and create a proc file for the substream
196  */
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)
200 {
201 	if (snd_BUG_ON(substream->dma_buffer.dev.type))
202 		return;
203 
204 	substream->dma_buffer.dev.type = type;
205 	substream->dma_buffer.dev.dev = data;
206 
207 	if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
208 		preallocate_pcm_pages(substream, size);
209 
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;
217 }
218 
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)
222 {
223 	struct snd_pcm_substream *substream;
224 	int stream;
225 
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);
231 }
232 
233 /**
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
240  *
241  * Do pre-allocation for the given DMA buffer type.
242  */
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)
246 {
247 	preallocate_pages(substream, type, data, size, max, false);
248 }
249 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
250 
251 /**
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
258  *
259  * Do pre-allocation to all substreams of the given pcm for the
260  * specified DMA type.
261  */
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)
265 {
266 	preallocate_pages_for_all(pcm, type, data, size, max, false);
267 }
268 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
269 
270 /**
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
277  *
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.
287  */
288 void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
289 				struct device *data, size_t size, size_t max)
290 {
291 	preallocate_pages(substream, type, data, size, max, true);
292 }
293 EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
294 
295 /**
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
303  *
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.
306  */
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)
310 {
311 	preallocate_pages_for_all(pcm, type, data, size, max, true);
312 }
313 EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
314 
315 #ifdef CONFIG_SND_DMA_SGBUF
316 /*
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
320  *
321  * Used as the page callback of PCM ops.
322  *
323  * Return: The page struct at the given buffer offset. %NULL on failure.
324  */
325 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
326 {
327 	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
328 
329 	unsigned int idx = offset >> PAGE_SHIFT;
330 	if (idx >= (unsigned int)sgbuf->pages)
331 		return NULL;
332 	return sgbuf->page_table[idx];
333 }
334 #endif /* CONFIG_SND_DMA_SGBUF */
335 
336 /**
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
340  *
341  * Allocates the DMA buffer on the BUS type given earlier to
342  * snd_pcm_lib_preallocate_xxx_pages().
343  *
344  * Return: 1 if the buffer is changed, 0 if not changed, or a negative
345  * code on failure.
346  */
347 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
348 {
349 	struct snd_pcm_runtime *runtime;
350 	struct snd_dma_buffer *dmab = NULL;
351 
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;
358 
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 */
366 		}
367 		snd_pcm_lib_free_pages(substream);
368 	}
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;
382 		}
383 	}
384 	snd_pcm_set_runtime_buffer(substream, dmab);
385 	runtime->dma_bytes = size;
386 	return 1;			/* area was changed */
387 }
388 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
389 
390 /**
391  * snd_pcm_lib_free_pages - release the allocated DMA buffer.
392  * @substream: the substream to release the DMA buffer
393  *
394  * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
395  *
396  * Return: Zero if successful, or a negative error code on failure.
397  */
398 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
399 {
400 	struct snd_pcm_runtime *runtime;
401 
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);
411 	}
412 	snd_pcm_set_runtime_buffer(substream, NULL);
413 	return 0;
414 }
415 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
416 
417 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
418 				      size_t size, gfp_t gfp_flags)
419 {
420 	struct snd_pcm_runtime *runtime;
421 
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);
429 	}
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;
435 }
436 EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
437 
438 /**
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()
442  *
443  * Return: Zero if successful, or a negative error code on failure.
444  */
445 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
446 {
447 	struct snd_pcm_runtime *runtime;
448 
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;
455 }
456 EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
457 
458 /**
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
463  *
464  * This function is to be used as the page callback in the PCM ops.
465  *
466  * Return: The page struct, or %NULL on failure.
467  */
468 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
469 					  unsigned long offset)
470 {
471 	return vmalloc_to_page(substream->runtime->dma_area + offset);
472 }
473 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);
474