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