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