mm: remove the pgprot argument to __vmalloc
[linux-2.6-block.git] / sound / core / pcm_memory.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Digital Audio (PCM) abstract layer
c1017a4c 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
5 */
6
6cbbfe1c 7#include <linux/io.h>
1da177e4
LT
8#include <linux/time.h>
9#include <linux/init.h>
5a0e3ad6 10#include <linux/slab.h>
1da177e4 11#include <linux/moduleparam.h>
ee7c343c 12#include <linux/vmalloc.h>
d81a6d71 13#include <linux/export.h>
1da177e4
LT
14#include <sound/core.h>
15#include <sound/pcm.h>
16#include <sound/info.h>
17#include <sound/initval.h>
0821fd77 18#include "pcm_local.h"
1da177e4
LT
19
20static int preallocate_dma = 1;
21module_param(preallocate_dma, int, 0444);
22MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
23
24static int maximum_substreams = 4;
25module_param(maximum_substreams, int, 0444);
26MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
27
28static const size_t snd_minimum_buffer = 16384;
29
d4cfb30f
TI
30static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
31module_param(max_alloc_per_card, ulong, 0644);
32MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
33
34static 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
51static 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}
1da177e4
LT
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 */
877211f5 69static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
1da177e4
LT
70{
71 struct snd_dma_buffer *dmab = &substream->dma_buffer;
d4cfb30f 72 struct snd_card *card = substream->pcm->card;
6ab08ced 73 size_t orig_size = size;
1da177e4
LT
74 int err;
75
1da177e4 76 do {
d4cfb30f
TI
77 err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
78 size, dmab);
79 if (err != -ENOMEM)
80 return err;
1da177e4
LT
81 size >>= 1;
82 } while (size >= snd_minimum_buffer);
83 dmab->bytes = 0; /* tell error */
6ab08ced
TI
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);
1da177e4
LT
88 return 0;
89}
90
91/*
92 * release the preallocated buffer if not yet done.
93 */
877211f5 94static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
1da177e4 95{
d4cfb30f 96 do_free_pages(substream->pcm->card, &substream->dma_buffer);
1da177e4
LT
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.
1da177e4 104 */
bb580602 105void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
1da177e4
LT
106{
107 snd_pcm_lib_preallocate_dma_free(substream);
1da177e4
LT
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.
1da177e4 115 */
bb580602 116void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
1da177e4 117{
877211f5 118 struct snd_pcm_substream *substream;
1da177e4
LT
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);
1da177e4 124}
e88e8ae6
TI
125EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
126
b7d90a35 127#ifdef CONFIG_SND_VERBOSE_PROCFS
1da177e4
LT
128/*
129 * read callback for prealloc proc file
130 *
131 * prints the current allocated size in kB.
132 */
877211f5
TI
133static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
134 struct snd_info_buffer *buffer)
1da177e4 135{
877211f5 136 struct snd_pcm_substream *substream = entry->private_data;
1da177e4
LT
137 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
138}
139
c7132aeb
JK
140/*
141 * read callback for prealloc_max proc file
142 *
143 * prints the maximum allowed size in kB.
144 */
145static 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
1da177e4
LT
152/*
153 * write callback for prealloc proc file
154 *
155 * accepts the preallocation size in kB.
156 */
877211f5
TI
157static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
158 struct snd_info_buffer *buffer)
1da177e4 159{
877211f5 160 struct snd_pcm_substream *substream = entry->private_data;
d4cfb30f 161 struct snd_card *card = substream->pcm->card;
1da177e4
LT
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) {
d4cfb30f
TI
182 if (do_alloc_pages(card,
183 substream->dma_buffer.dev.type,
184 substream->dma_buffer.dev.dev,
185 size, &new_dmab) < 0) {
1da177e4
LT
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)
d4cfb30f 194 do_free_pages(card, &substream->dma_buffer);
1da177e4
LT
195 substream->dma_buffer = new_dmab;
196 } else {
197 buffer->error = -EINVAL;
198 }
199}
200
e28563cc 201static inline void preallocate_info_init(struct snd_pcm_substream *substream)
1da177e4 202{
877211f5 203 struct snd_info_entry *entry;
1da177e4 204
a8d14981
TI
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);
1da177e4 210 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
6a73cf46 211 entry->mode |= 0200;
c7132aeb 212 }
a8d14981
TI
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);
e28563cc
TI
218}
219
b7d90a35 220#else /* !CONFIG_SND_VERBOSE_PROCFS */
e28563cc 221#define preallocate_info_init(s)
b7d90a35 222#endif /* CONFIG_SND_VERBOSE_PROCFS */
e28563cc
TI
223
224/*
225 * pre-allocate the buffer and create a proc file for the substream
226 */
0dba808e
TI
227static void preallocate_pages(struct snd_pcm_substream *substream,
228 int type, struct device *data,
229 size_t size, size_t max, bool managed)
e28563cc 230{
0dba808e
TI
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;
e28563cc
TI
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;
d3978991
TI
243 if (max > 0)
244 preallocate_info_init(substream);
0dba808e
TI
245 if (managed)
246 substream->managed_buffer_alloc = 1;
1da177e4
LT
247}
248
0dba808e
TI
249static 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}
1da177e4
LT
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_*)
25985edc 267 * @data: DMA type dependent data
1da177e4
LT
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.
1da177e4 272 */
bb580602 273void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
1da177e4
LT
274 int type, struct device *data,
275 size_t size, size_t max)
276{
0dba808e 277 preallocate_pages(substream, type, data, size, max, false);
1da177e4 278}
e88e8ae6
TI
279EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
280
1da177e4 281/**
25985edc 282 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
df8db936 283 * @pcm: the pcm instance
1da177e4 284 * @type: DMA type (SNDRV_DMA_TYPE_*)
25985edc 285 * @data: DMA type dependent data
1da177e4
LT
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.
1da177e4 291 */
bb580602 292void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
1da177e4
LT
293 int type, void *data,
294 size_t size, size_t max)
295{
0dba808e 296 preallocate_pages_for_all(pcm, type, data, size, max, false);
1da177e4 297}
e88e8ae6
TI
298EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
299
0dba808e
TI
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 */
318void 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}
323EXPORT_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 */
337void 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}
343EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
344
cc6a8acd 345#ifdef CONFIG_SND_DMA_SGBUF
fc7af6bc 346/*
1da177e4
LT
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 *
1da177e4 351 * Used as the page callback of PCM ops.
eb7c06e8
YB
352 *
353 * Return: The page struct at the given buffer offset. %NULL on failure.
1da177e4 354 */
877211f5 355struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
1da177e4
LT
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}
cc6a8acd 364#endif /* CONFIG_SND_DMA_SGBUF */
51e9f2e6 365
1da177e4
LT
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 *
eb7c06e8 374 * Return: 1 if the buffer is changed, 0 if not changed, or a negative
1da177e4
LT
375 * code on failure.
376 */
877211f5 377int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
1da177e4 378{
d4cfb30f 379 struct snd_card *card = substream->pcm->card;
877211f5 380 struct snd_pcm_runtime *runtime;
1da177e4
LT
381 struct snd_dma_buffer *dmab = NULL;
382
7eaa943c
TI
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;
1da177e4 388 runtime = substream->runtime;
1da177e4
LT
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 }
877211f5
TI
400 if (substream->dma_buffer.area != NULL &&
401 substream->dma_buffer.bytes >= size) {
1da177e4
LT
402 dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
403 } else {
ca2c0966 404 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
1da177e4
LT
405 if (! dmab)
406 return -ENOMEM;
407 dmab->dev = substream->dma_buffer.dev;
d4cfb30f
TI
408 if (do_alloc_pages(card,
409 substream->dma_buffer.dev.type,
410 substream->dma_buffer.dev.dev,
411 size, dmab) < 0) {
1da177e4
LT
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}
e88e8ae6
TI
420EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
421
1da177e4
LT
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 *
eb7c06e8 428 * Return: Zero if successful, or a negative error code on failure.
1da177e4 429 */
877211f5 430int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
1da177e4 431{
d4cfb30f 432 struct snd_card *card = substream->pcm->card;
877211f5 433 struct snd_pcm_runtime *runtime;
1da177e4 434
7eaa943c
TI
435 if (PCM_RUNTIME_CHECK(substream))
436 return -EINVAL;
1da177e4 437 runtime = substream->runtime;
1da177e4
LT
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. */
d4cfb30f 442 do_free_pages(card, runtime->dma_buffer_p);
1da177e4
LT
443 kfree(runtime->dma_buffer_p);
444 }
445 snd_pcm_set_runtime_buffer(substream, NULL);
446 return 0;
447}
e88e8ae6 448EXPORT_SYMBOL(snd_pcm_lib_free_pages);
681b84e1
CL
449
450int _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 }
88dca4ca 463 runtime->dma_area = __vmalloc(size, gfp_flags);
681b84e1
CL
464 if (!runtime->dma_area)
465 return -ENOMEM;
466 runtime->dma_bytes = size;
467 return 1;
468}
469EXPORT_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()
eb7c06e8
YB
475 *
476 * Return: Zero if successful, or a negative error code on failure.
681b84e1
CL
477 */
478int 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}
489EXPORT_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.
eb7c06e8
YB
498 *
499 * Return: The page struct, or %NULL on failure.
681b84e1
CL
500 */
501struct 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}
506EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);