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