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