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