xref: /openbmc/linux/sound/core/pcm_memory.c (revision 6c33a6f4)
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 	err = snd_dma_alloc_pages(type, dev, size, dmab);
43 	if (!err) {
44 		mutex_lock(&card->memory_mutex);
45 		card->total_pcm_alloc_bytes += dmab->bytes;
46 		mutex_unlock(&card->memory_mutex);
47 	}
48 	return err;
49 }
50 
51 static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
52 {
53 	if (!dmab->area)
54 		return;
55 	mutex_lock(&card->memory_mutex);
56 	WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes);
57 	card->total_pcm_alloc_bytes -= dmab->bytes;
58 	mutex_unlock(&card->memory_mutex);
59 	snd_dma_free_pages(dmab);
60 	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 static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
70 {
71 	struct snd_dma_buffer *dmab = &substream->dma_buffer;
72 	struct snd_card *card = substream->pcm->card;
73 	size_t orig_size = size;
74 	int err;
75 
76 	do {
77 		err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
78 				     size, dmab);
79 		if (err != -ENOMEM)
80 			return err;
81 		size >>= 1;
82 	} while (size >= snd_minimum_buffer);
83 	dmab->bytes = 0; /* tell error */
84 	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 	return 0;
89 }
90 
91 /*
92  * release the preallocated buffer if not yet done.
93  */
94 static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
95 {
96 	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 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
106 {
107 	snd_pcm_lib_preallocate_dma_free(substream);
108 }
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 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
117 {
118 	struct snd_pcm_substream *substream;
119 	int stream;
120 
121 	for (stream = 0; stream < 2; stream++)
122 		for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
123 			snd_pcm_lib_preallocate_free(substream);
124 }
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 static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
134 					      struct snd_info_buffer *buffer)
135 {
136 	struct snd_pcm_substream *substream = entry->private_data;
137 	snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
138 }
139 
140 /*
141  * read callback for prealloc_max proc file
142  *
143  * prints the maximum allowed size in kB.
144  */
145 static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
146 						  struct snd_info_buffer *buffer)
147 {
148 	struct snd_pcm_substream *substream = entry->private_data;
149 	snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
150 }
151 
152 /*
153  * write callback for prealloc proc file
154  *
155  * accepts the preallocation size in kB.
156  */
157 static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
158 					       struct snd_info_buffer *buffer)
159 {
160 	struct snd_pcm_substream *substream = entry->private_data;
161 	struct snd_card *card = substream->pcm->card;
162 	char line[64], str[64];
163 	size_t size;
164 	struct snd_dma_buffer new_dmab;
165 
166 	if (substream->runtime) {
167 		buffer->error = -EBUSY;
168 		return;
169 	}
170 	if (!snd_info_get_line(buffer, line, sizeof(line))) {
171 		snd_info_get_str(str, line, sizeof(str));
172 		size = simple_strtoul(str, NULL, 10) * 1024;
173 		if ((size != 0 && size < 8192) || size > substream->dma_max) {
174 			buffer->error = -EINVAL;
175 			return;
176 		}
177 		if (substream->dma_buffer.bytes == size)
178 			return;
179 		memset(&new_dmab, 0, sizeof(new_dmab));
180 		new_dmab.dev = substream->dma_buffer.dev;
181 		if (size > 0) {
182 			if (do_alloc_pages(card,
183 					   substream->dma_buffer.dev.type,
184 					   substream->dma_buffer.dev.dev,
185 					   size, &new_dmab) < 0) {
186 				buffer->error = -ENOMEM;
187 				return;
188 			}
189 			substream->buffer_bytes_max = size;
190 		} else {
191 			substream->buffer_bytes_max = UINT_MAX;
192 		}
193 		if (substream->dma_buffer.area)
194 			do_free_pages(card, &substream->dma_buffer);
195 		substream->dma_buffer = new_dmab;
196 	} else {
197 		buffer->error = -EINVAL;
198 	}
199 }
200 
201 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
202 {
203 	struct snd_info_entry *entry;
204 
205 	entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
206 					   substream->proc_root);
207 	if (entry) {
208 		snd_info_set_text_ops(entry, substream,
209 				      snd_pcm_lib_preallocate_proc_read);
210 		entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
211 		entry->mode |= 0200;
212 	}
213 	entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
214 					   substream->proc_root);
215 	if (entry)
216 		snd_info_set_text_ops(entry, substream,
217 				      snd_pcm_lib_preallocate_max_proc_read);
218 }
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 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 	if (snd_BUG_ON(substream->dma_buffer.dev.type))
232 		return;
233 
234 	substream->dma_buffer.dev.type = type;
235 	substream->dma_buffer.dev.dev = data;
236 
237 	if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
238 		preallocate_pcm_pages(substream, size);
239 
240 	if (substream->dma_buffer.bytes > 0)
241 		substream->buffer_bytes_max = substream->dma_buffer.bytes;
242 	substream->dma_max = max;
243 	if (max > 0)
244 		preallocate_info_init(substream);
245 	if (managed)
246 		substream->managed_buffer_alloc = 1;
247 }
248 
249 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 	struct snd_pcm_substream *substream;
254 	int stream;
255 
256 	for (stream = 0; stream < 2; stream++)
257 		for (substream = pcm->streams[stream].substream; substream;
258 		     substream = substream->next)
259 			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 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 	preallocate_pages(substream, type, data, size, max, false);
278 }
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 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 	preallocate_pages_for_all(pcm, type, data, size, max, false);
297 }
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 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 	preallocate_pages(substream, type, data, size, max, true);
322 }
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 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 	preallocate_pages_for_all(pcm, type, data, size, max, true);
342 }
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 struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
356 {
357 	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
358 
359 	unsigned int idx = offset >> PAGE_SHIFT;
360 	if (idx >= (unsigned int)sgbuf->pages)
361 		return NULL;
362 	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 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
378 {
379 	struct snd_card *card = substream->pcm->card;
380 	struct snd_pcm_runtime *runtime;
381 	struct snd_dma_buffer *dmab = NULL;
382 
383 	if (PCM_RUNTIME_CHECK(substream))
384 		return -EINVAL;
385 	if (snd_BUG_ON(substream->dma_buffer.dev.type ==
386 		       SNDRV_DMA_TYPE_UNKNOWN))
387 		return -EINVAL;
388 	runtime = substream->runtime;
389 
390 	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 		if (runtime->dma_buffer_p->bytes >= size) {
395 			runtime->dma_bytes = size;
396 			return 0;	/* ok, do not change */
397 		}
398 		snd_pcm_lib_free_pages(substream);
399 	}
400 	if (substream->dma_buffer.area != NULL &&
401 	    substream->dma_buffer.bytes >= size) {
402 		dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
403 	} else {
404 		dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
405 		if (! dmab)
406 			return -ENOMEM;
407 		dmab->dev = substream->dma_buffer.dev;
408 		if (do_alloc_pages(card,
409 				   substream->dma_buffer.dev.type,
410 				   substream->dma_buffer.dev.dev,
411 				   size, dmab) < 0) {
412 			kfree(dmab);
413 			return -ENOMEM;
414 		}
415 	}
416 	snd_pcm_set_runtime_buffer(substream, dmab);
417 	runtime->dma_bytes = size;
418 	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 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
431 {
432 	struct snd_card *card = substream->pcm->card;
433 	struct snd_pcm_runtime *runtime;
434 
435 	if (PCM_RUNTIME_CHECK(substream))
436 		return -EINVAL;
437 	runtime = substream->runtime;
438 	if (runtime->dma_area == NULL)
439 		return 0;
440 	if (runtime->dma_buffer_p != &substream->dma_buffer) {
441 		/* it's a newly allocated buffer.  release it now. */
442 		do_free_pages(card, runtime->dma_buffer_p);
443 		kfree(runtime->dma_buffer_p);
444 	}
445 	snd_pcm_set_runtime_buffer(substream, NULL);
446 	return 0;
447 }
448 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
449 
450 int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
451 				      size_t size, gfp_t gfp_flags)
452 {
453 	struct snd_pcm_runtime *runtime;
454 
455 	if (PCM_RUNTIME_CHECK(substream))
456 		return -EINVAL;
457 	runtime = substream->runtime;
458 	if (runtime->dma_area) {
459 		if (runtime->dma_bytes >= size)
460 			return 0; /* already large enough */
461 		vfree(runtime->dma_area);
462 	}
463 	runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL);
464 	if (!runtime->dma_area)
465 		return -ENOMEM;
466 	runtime->dma_bytes = size;
467 	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 int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
479 {
480 	struct snd_pcm_runtime *runtime;
481 
482 	if (PCM_RUNTIME_CHECK(substream))
483 		return -EINVAL;
484 	runtime = substream->runtime;
485 	vfree(runtime->dma_area);
486 	runtime->dma_area = NULL;
487 	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 struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
502 					  unsigned long offset)
503 {
504 	return vmalloc_to_page(substream->runtime->dma_area + offset);
505 }
506 EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);
507