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