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