11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Digital Audio (PCM) abstract layer
4c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds
76cbbfe1cSTakashi Iwai #include <linux/io.h>
81da177e4SLinus Torvalds #include <linux/time.h>
91da177e4SLinus Torvalds #include <linux/init.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/moduleparam.h>
12ee7c343cSTakashi Iwai #include <linux/vmalloc.h>
13d81a6d71SPaul Gortmaker #include <linux/export.h>
141da177e4SLinus Torvalds #include <sound/core.h>
151da177e4SLinus Torvalds #include <sound/pcm.h>
161da177e4SLinus Torvalds #include <sound/info.h>
171da177e4SLinus Torvalds #include <sound/initval.h>
180821fd77STakashi Iwai #include "pcm_local.h"
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds static int preallocate_dma = 1;
211da177e4SLinus Torvalds module_param(preallocate_dma, int, 0444);
221da177e4SLinus Torvalds MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
231da177e4SLinus Torvalds
241da177e4SLinus Torvalds static int maximum_substreams = 4;
251da177e4SLinus Torvalds module_param(maximum_substreams, int, 0444);
261da177e4SLinus Torvalds MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds static const size_t snd_minimum_buffer = 16384;
291da177e4SLinus Torvalds
30d4cfb30fSTakashi Iwai static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
31d4cfb30fSTakashi Iwai module_param(max_alloc_per_card, ulong, 0644);
32d4cfb30fSTakashi Iwai MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
33d4cfb30fSTakashi Iwai
__update_allocated_size(struct snd_card * card,ssize_t bytes)34*bd55842eSTakashi Iwai static void __update_allocated_size(struct snd_card *card, ssize_t bytes)
35*bd55842eSTakashi Iwai {
36*bd55842eSTakashi Iwai card->total_pcm_alloc_bytes += bytes;
37*bd55842eSTakashi Iwai }
38*bd55842eSTakashi Iwai
update_allocated_size(struct snd_card * card,ssize_t bytes)39*bd55842eSTakashi Iwai static void update_allocated_size(struct snd_card *card, ssize_t bytes)
40*bd55842eSTakashi Iwai {
41*bd55842eSTakashi Iwai mutex_lock(&card->memory_mutex);
42*bd55842eSTakashi Iwai __update_allocated_size(card, bytes);
43*bd55842eSTakashi Iwai mutex_unlock(&card->memory_mutex);
44*bd55842eSTakashi Iwai }
45*bd55842eSTakashi Iwai
decrease_allocated_size(struct snd_card * card,size_t bytes)46*bd55842eSTakashi Iwai static void decrease_allocated_size(struct snd_card *card, size_t bytes)
47*bd55842eSTakashi Iwai {
48*bd55842eSTakashi Iwai mutex_lock(&card->memory_mutex);
49*bd55842eSTakashi Iwai WARN_ON(card->total_pcm_alloc_bytes < bytes);
50*bd55842eSTakashi Iwai __update_allocated_size(card, -(ssize_t)bytes);
51*bd55842eSTakashi Iwai mutex_unlock(&card->memory_mutex);
52*bd55842eSTakashi Iwai }
53*bd55842eSTakashi Iwai
do_alloc_pages(struct snd_card * card,int type,struct device * dev,int str,size_t size,struct snd_dma_buffer * dmab)54d4cfb30fSTakashi Iwai static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
55a25684a9STakashi Iwai int str, size_t size, struct snd_dma_buffer *dmab)
56d4cfb30fSTakashi Iwai {
57a25684a9STakashi Iwai enum dma_data_direction dir;
58d4cfb30fSTakashi Iwai int err;
59d4cfb30fSTakashi Iwai
60*bd55842eSTakashi Iwai /* check and reserve the requested size */
61*bd55842eSTakashi Iwai mutex_lock(&card->memory_mutex);
62d4cfb30fSTakashi Iwai if (max_alloc_per_card &&
63*bd55842eSTakashi Iwai card->total_pcm_alloc_bytes + size > max_alloc_per_card) {
64*bd55842eSTakashi Iwai mutex_unlock(&card->memory_mutex);
65d4cfb30fSTakashi Iwai return -ENOMEM;
66*bd55842eSTakashi Iwai }
67*bd55842eSTakashi Iwai __update_allocated_size(card, size);
68*bd55842eSTakashi Iwai mutex_unlock(&card->memory_mutex);
693ad796cbSTakashi Iwai
70a25684a9STakashi Iwai if (str == SNDRV_PCM_STREAM_PLAYBACK)
71a25684a9STakashi Iwai dir = DMA_TO_DEVICE;
72a25684a9STakashi Iwai else
73a25684a9STakashi Iwai dir = DMA_FROM_DEVICE;
74a25684a9STakashi Iwai err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
75d4cfb30fSTakashi Iwai if (!err) {
76*bd55842eSTakashi Iwai /* the actual allocation size might be bigger than requested,
77*bd55842eSTakashi Iwai * and we need to correct the account
78*bd55842eSTakashi Iwai */
79*bd55842eSTakashi Iwai if (dmab->bytes != size)
80*bd55842eSTakashi Iwai update_allocated_size(card, dmab->bytes - size);
81*bd55842eSTakashi Iwai } else {
82*bd55842eSTakashi Iwai /* take back on allocation failure */
83*bd55842eSTakashi Iwai decrease_allocated_size(card, size);
84d4cfb30fSTakashi Iwai }
85d4cfb30fSTakashi Iwai return err;
86d4cfb30fSTakashi Iwai }
87d4cfb30fSTakashi Iwai
do_free_pages(struct snd_card * card,struct snd_dma_buffer * dmab)88d4cfb30fSTakashi Iwai static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
89d4cfb30fSTakashi Iwai {
90d4cfb30fSTakashi Iwai if (!dmab->area)
91d4cfb30fSTakashi Iwai return;
92*bd55842eSTakashi Iwai decrease_allocated_size(card, dmab->bytes);
93d4cfb30fSTakashi Iwai snd_dma_free_pages(dmab);
94d4cfb30fSTakashi Iwai dmab->area = NULL;
95d4cfb30fSTakashi Iwai }
961da177e4SLinus Torvalds
971da177e4SLinus Torvalds /*
981da177e4SLinus Torvalds * try to allocate as the large pages as possible.
991da177e4SLinus Torvalds * stores the resultant memory size in *res_size.
1001da177e4SLinus Torvalds *
1011da177e4SLinus Torvalds * the minimum size is snd_minimum_buffer. it should be power of 2.
1021da177e4SLinus Torvalds */
preallocate_pcm_pages(struct snd_pcm_substream * substream,size_t size,bool no_fallback)103ac9245a5STakashi Iwai static int preallocate_pcm_pages(struct snd_pcm_substream *substream,
104ac9245a5STakashi Iwai size_t size, bool no_fallback)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds struct snd_dma_buffer *dmab = &substream->dma_buffer;
107d4cfb30fSTakashi Iwai struct snd_card *card = substream->pcm->card;
1086ab08cedSTakashi Iwai size_t orig_size = size;
1091da177e4SLinus Torvalds int err;
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds do {
112d4cfb30fSTakashi Iwai err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
113a25684a9STakashi Iwai substream->stream, size, dmab);
1141da177e4SLinus Torvalds if (err != -ENOMEM)
115d4cfb30fSTakashi Iwai return err;
116ac9245a5STakashi Iwai if (no_fallback)
117ac9245a5STakashi Iwai break;
1181da177e4SLinus Torvalds size >>= 1;
1191da177e4SLinus Torvalds } while (size >= snd_minimum_buffer);
1201da177e4SLinus Torvalds dmab->bytes = 0; /* tell error */
1216ab08cedSTakashi Iwai pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
1226ab08cedSTakashi Iwai substream->pcm->card->number, substream->pcm->device,
1236ab08cedSTakashi Iwai substream->stream ? 'c' : 'p', substream->number,
1246ab08cedSTakashi Iwai substream->pcm->name, orig_size);
125ac9245a5STakashi Iwai return -ENOMEM;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds
1281da177e4SLinus Torvalds /**
1291da177e4SLinus Torvalds * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
1301da177e4SLinus Torvalds * @substream: the pcm substream instance
1311da177e4SLinus Torvalds *
1321da177e4SLinus Torvalds * Releases the pre-allocated buffer of the given substream.
1331da177e4SLinus Torvalds */
snd_pcm_lib_preallocate_free(struct snd_pcm_substream * substream)134bb580602STakashi Iwai void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
1351da177e4SLinus Torvalds {
136f2283366SLars-Peter Clausen do_free_pages(substream->pcm->card, &substream->dma_buffer);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds
1391da177e4SLinus Torvalds /**
1401da177e4SLinus Torvalds * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
1411da177e4SLinus Torvalds * @pcm: the pcm instance
1421da177e4SLinus Torvalds *
1431da177e4SLinus Torvalds * Releases all the pre-allocated buffers on the given pcm.
1441da177e4SLinus Torvalds */
snd_pcm_lib_preallocate_free_for_all(struct snd_pcm * pcm)145bb580602STakashi Iwai void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
1461da177e4SLinus Torvalds {
147877211f5STakashi Iwai struct snd_pcm_substream *substream;
1481da177e4SLinus Torvalds int stream;
1491da177e4SLinus Torvalds
1508d19b4e0STakashi Iwai for_each_pcm_substream(pcm, stream, substream)
1511da177e4SLinus Torvalds snd_pcm_lib_preallocate_free(substream);
1521da177e4SLinus Torvalds }
153e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
154e88e8ae6STakashi Iwai
155b7d90a35STakashi Iwai #ifdef CONFIG_SND_VERBOSE_PROCFS
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds * read callback for prealloc proc file
1581da177e4SLinus Torvalds *
1591da177e4SLinus Torvalds * prints the current allocated size in kB.
1601da177e4SLinus Torvalds */
snd_pcm_lib_preallocate_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)161877211f5STakashi Iwai static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
162877211f5STakashi Iwai struct snd_info_buffer *buffer)
1631da177e4SLinus Torvalds {
164877211f5STakashi Iwai struct snd_pcm_substream *substream = entry->private_data;
1651da177e4SLinus Torvalds snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds
1681da177e4SLinus Torvalds /*
169c7132aebSJaroslav Kysela * read callback for prealloc_max proc file
170c7132aebSJaroslav Kysela *
171c7132aebSJaroslav Kysela * prints the maximum allowed size in kB.
172c7132aebSJaroslav Kysela */
snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)173c7132aebSJaroslav Kysela static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
174c7132aebSJaroslav Kysela struct snd_info_buffer *buffer)
175c7132aebSJaroslav Kysela {
176c7132aebSJaroslav Kysela struct snd_pcm_substream *substream = entry->private_data;
177c7132aebSJaroslav Kysela snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
178c7132aebSJaroslav Kysela }
179c7132aebSJaroslav Kysela
180c7132aebSJaroslav Kysela /*
1811da177e4SLinus Torvalds * write callback for prealloc proc file
1821da177e4SLinus Torvalds *
1831da177e4SLinus Torvalds * accepts the preallocation size in kB.
1841da177e4SLinus Torvalds */
snd_pcm_lib_preallocate_proc_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)185877211f5STakashi Iwai static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
186877211f5STakashi Iwai struct snd_info_buffer *buffer)
1871da177e4SLinus Torvalds {
188877211f5STakashi Iwai struct snd_pcm_substream *substream = entry->private_data;
189d4cfb30fSTakashi Iwai struct snd_card *card = substream->pcm->card;
1901da177e4SLinus Torvalds char line[64], str[64];
1911da177e4SLinus Torvalds size_t size;
1921da177e4SLinus Torvalds struct snd_dma_buffer new_dmab;
1931da177e4SLinus Torvalds
19469534c48STakashi Iwai mutex_lock(&substream->pcm->open_mutex);
1951da177e4SLinus Torvalds if (substream->runtime) {
1961da177e4SLinus Torvalds buffer->error = -EBUSY;
19769534c48STakashi Iwai goto unlock;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds if (!snd_info_get_line(buffer, line, sizeof(line))) {
2001da177e4SLinus Torvalds snd_info_get_str(str, line, sizeof(str));
2011da177e4SLinus Torvalds size = simple_strtoul(str, NULL, 10) * 1024;
2021da177e4SLinus Torvalds if ((size != 0 && size < 8192) || size > substream->dma_max) {
2031da177e4SLinus Torvalds buffer->error = -EINVAL;
20469534c48STakashi Iwai goto unlock;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds if (substream->dma_buffer.bytes == size)
20769534c48STakashi Iwai goto unlock;
2081da177e4SLinus Torvalds memset(&new_dmab, 0, sizeof(new_dmab));
2091da177e4SLinus Torvalds new_dmab.dev = substream->dma_buffer.dev;
2101da177e4SLinus Torvalds if (size > 0) {
211d4cfb30fSTakashi Iwai if (do_alloc_pages(card,
212d4cfb30fSTakashi Iwai substream->dma_buffer.dev.type,
2131da177e4SLinus Torvalds substream->dma_buffer.dev.dev,
214a25684a9STakashi Iwai substream->stream,
2151da177e4SLinus Torvalds size, &new_dmab) < 0) {
2161da177e4SLinus Torvalds buffer->error = -ENOMEM;
217dc85fc9dSAmadeusz Sławiński pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
218dc85fc9dSAmadeusz Sławiński substream->pcm->card->number, substream->pcm->device,
219dc85fc9dSAmadeusz Sławiński substream->stream ? 'c' : 'p', substream->number,
220dc85fc9dSAmadeusz Sławiński substream->pcm->name, size);
22169534c48STakashi Iwai goto unlock;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds substream->buffer_bytes_max = size;
2241da177e4SLinus Torvalds } else {
2251da177e4SLinus Torvalds substream->buffer_bytes_max = UINT_MAX;
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds if (substream->dma_buffer.area)
228d4cfb30fSTakashi Iwai do_free_pages(card, &substream->dma_buffer);
2291da177e4SLinus Torvalds substream->dma_buffer = new_dmab;
2301da177e4SLinus Torvalds } else {
2311da177e4SLinus Torvalds buffer->error = -EINVAL;
2321da177e4SLinus Torvalds }
23369534c48STakashi Iwai unlock:
23469534c48STakashi Iwai mutex_unlock(&substream->pcm->open_mutex);
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds
preallocate_info_init(struct snd_pcm_substream * substream)237e28563ccSTakashi Iwai static inline void preallocate_info_init(struct snd_pcm_substream *substream)
2381da177e4SLinus Torvalds {
239877211f5STakashi Iwai struct snd_info_entry *entry;
2401da177e4SLinus Torvalds
241a8d14981STakashi Iwai entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
242a8d14981STakashi Iwai substream->proc_root);
243a8d14981STakashi Iwai if (entry) {
244a8d14981STakashi Iwai snd_info_set_text_ops(entry, substream,
245a8d14981STakashi Iwai snd_pcm_lib_preallocate_proc_read);
2461da177e4SLinus Torvalds entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
2476a73cf46SJoe Perches entry->mode |= 0200;
2481da177e4SLinus Torvalds }
249a8d14981STakashi Iwai entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
250a8d14981STakashi Iwai substream->proc_root);
251a8d14981STakashi Iwai if (entry)
252a8d14981STakashi Iwai snd_info_set_text_ops(entry, substream,
253a8d14981STakashi Iwai snd_pcm_lib_preallocate_max_proc_read);
254c7132aebSJaroslav Kysela }
255e28563ccSTakashi Iwai
256b7d90a35STakashi Iwai #else /* !CONFIG_SND_VERBOSE_PROCFS */
preallocate_info_init(struct snd_pcm_substream * substream)257940ba1f5SArnd Bergmann static inline void preallocate_info_init(struct snd_pcm_substream *substream)
258940ba1f5SArnd Bergmann {
259940ba1f5SArnd Bergmann }
260b7d90a35STakashi Iwai #endif /* CONFIG_SND_VERBOSE_PROCFS */
261e28563ccSTakashi Iwai
262e28563ccSTakashi Iwai /*
263e28563ccSTakashi Iwai * pre-allocate the buffer and create a proc file for the substream
264e28563ccSTakashi Iwai */
preallocate_pages(struct snd_pcm_substream * substream,int type,struct device * data,size_t size,size_t max,bool managed)265ac9245a5STakashi Iwai static int preallocate_pages(struct snd_pcm_substream *substream,
2660dba808eSTakashi Iwai int type, struct device *data,
2670dba808eSTakashi Iwai size_t size, size_t max, bool managed)
268e28563ccSTakashi Iwai {
269ac9245a5STakashi Iwai int err;
270ac9245a5STakashi Iwai
2710dba808eSTakashi Iwai if (snd_BUG_ON(substream->dma_buffer.dev.type))
272ac9245a5STakashi Iwai return -EINVAL;
2730dba808eSTakashi Iwai
2740dba808eSTakashi Iwai substream->dma_buffer.dev.type = type;
2750dba808eSTakashi Iwai substream->dma_buffer.dev.dev = data;
276e28563ccSTakashi Iwai
277ac9245a5STakashi Iwai if (size > 0) {
278ac9245a5STakashi Iwai if (!max) {
279ac9245a5STakashi Iwai /* no fallback, only also inform -ENOMEM */
280ac9245a5STakashi Iwai err = preallocate_pcm_pages(substream, size, true);
281ac9245a5STakashi Iwai if (err < 0)
282ac9245a5STakashi Iwai return err;
283ac9245a5STakashi Iwai } else if (preallocate_dma &&
284ac9245a5STakashi Iwai substream->number < maximum_substreams) {
285ac9245a5STakashi Iwai err = preallocate_pcm_pages(substream, size, false);
286ac9245a5STakashi Iwai if (err < 0 && err != -ENOMEM)
287ac9245a5STakashi Iwai return err;
288ac9245a5STakashi Iwai }
289ac9245a5STakashi Iwai }
290e28563ccSTakashi Iwai
291e28563ccSTakashi Iwai if (substream->dma_buffer.bytes > 0)
292e28563ccSTakashi Iwai substream->buffer_bytes_max = substream->dma_buffer.bytes;
293e28563ccSTakashi Iwai substream->dma_max = max;
294d3978991STakashi Iwai if (max > 0)
295e28563ccSTakashi Iwai preallocate_info_init(substream);
2960dba808eSTakashi Iwai if (managed)
2970dba808eSTakashi Iwai substream->managed_buffer_alloc = 1;
298ac9245a5STakashi Iwai return 0;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds
preallocate_pages_for_all(struct snd_pcm * pcm,int type,void * data,size_t size,size_t max,bool managed)301ac9245a5STakashi Iwai static int preallocate_pages_for_all(struct snd_pcm *pcm, int type,
3020dba808eSTakashi Iwai void *data, size_t size, size_t max,
3030dba808eSTakashi Iwai bool managed)
3040dba808eSTakashi Iwai {
3050dba808eSTakashi Iwai struct snd_pcm_substream *substream;
306ac9245a5STakashi Iwai int stream, err;
3070dba808eSTakashi Iwai
308ac9245a5STakashi Iwai for_each_pcm_substream(pcm, stream, substream) {
309ac9245a5STakashi Iwai err = preallocate_pages(substream, type, data, size, max, managed);
310ac9245a5STakashi Iwai if (err < 0)
311ac9245a5STakashi Iwai return err;
312ac9245a5STakashi Iwai }
313ac9245a5STakashi Iwai return 0;
3140dba808eSTakashi Iwai }
3151da177e4SLinus Torvalds
3161da177e4SLinus Torvalds /**
3171da177e4SLinus Torvalds * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
3181da177e4SLinus Torvalds * @substream: the pcm substream instance
3191da177e4SLinus Torvalds * @type: DMA type (SNDRV_DMA_TYPE_*)
32025985edcSLucas De Marchi * @data: DMA type dependent data
3211da177e4SLinus Torvalds * @size: the requested pre-allocation size in bytes
3221da177e4SLinus Torvalds * @max: the max. allowed pre-allocation size
3231da177e4SLinus Torvalds *
3241da177e4SLinus Torvalds * Do pre-allocation for the given DMA buffer type.
3251da177e4SLinus Torvalds */
snd_pcm_lib_preallocate_pages(struct snd_pcm_substream * substream,int type,struct device * data,size_t size,size_t max)326bb580602STakashi Iwai void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
3271da177e4SLinus Torvalds int type, struct device *data,
3281da177e4SLinus Torvalds size_t size, size_t max)
3291da177e4SLinus Torvalds {
3300dba808eSTakashi Iwai preallocate_pages(substream, type, data, size, max, false);
3311da177e4SLinus Torvalds }
332e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
333e88e8ae6STakashi Iwai
3341da177e4SLinus Torvalds /**
33525985edcSLucas De Marchi * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
336df8db936STakashi Iwai * @pcm: the pcm instance
3371da177e4SLinus Torvalds * @type: DMA type (SNDRV_DMA_TYPE_*)
33825985edcSLucas De Marchi * @data: DMA type dependent data
3391da177e4SLinus Torvalds * @size: the requested pre-allocation size in bytes
3401da177e4SLinus Torvalds * @max: the max. allowed pre-allocation size
3411da177e4SLinus Torvalds *
3421da177e4SLinus Torvalds * Do pre-allocation to all substreams of the given pcm for the
3431da177e4SLinus Torvalds * specified DMA type.
3441da177e4SLinus Torvalds */
snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm * pcm,int type,void * data,size_t size,size_t max)345bb580602STakashi Iwai void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
3461da177e4SLinus Torvalds int type, void *data,
3471da177e4SLinus Torvalds size_t size, size_t max)
3481da177e4SLinus Torvalds {
3490dba808eSTakashi Iwai preallocate_pages_for_all(pcm, type, data, size, max, false);
3501da177e4SLinus Torvalds }
351e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
352e88e8ae6STakashi Iwai
3530dba808eSTakashi Iwai /**
3540dba808eSTakashi Iwai * snd_pcm_set_managed_buffer - set up buffer management for a substream
3550dba808eSTakashi Iwai * @substream: the pcm substream instance
3560dba808eSTakashi Iwai * @type: DMA type (SNDRV_DMA_TYPE_*)
3570dba808eSTakashi Iwai * @data: DMA type dependent data
3580dba808eSTakashi Iwai * @size: the requested pre-allocation size in bytes
3590dba808eSTakashi Iwai * @max: the max. allowed pre-allocation size
3600dba808eSTakashi Iwai *
3610dba808eSTakashi Iwai * Do pre-allocation for the given DMA buffer type, and set the managed
3620dba808eSTakashi Iwai * buffer allocation mode to the given substream.
3630dba808eSTakashi Iwai * In this mode, PCM core will allocate a buffer automatically before PCM
3640dba808eSTakashi Iwai * hw_params ops call, and release the buffer after PCM hw_free ops call
3650dba808eSTakashi Iwai * as well, so that the driver doesn't need to invoke the allocation and
3660dba808eSTakashi Iwai * the release explicitly in its callback.
3670dba808eSTakashi Iwai * When a buffer is actually allocated before the PCM hw_params call, it
3680dba808eSTakashi Iwai * turns on the runtime buffer_changed flag for drivers changing their h/w
3690dba808eSTakashi Iwai * parameters accordingly.
370ac9245a5STakashi Iwai *
371ac9245a5STakashi Iwai * When @size is non-zero and @max is zero, this tries to allocate for only
372ac9245a5STakashi Iwai * the exact buffer size without fallback, and may return -ENOMEM.
373ac9245a5STakashi Iwai * Otherwise, the function tries to allocate smaller chunks if the allocation
374ac9245a5STakashi Iwai * fails. This is the behavior of snd_pcm_set_fixed_buffer().
375ac9245a5STakashi Iwai *
376ac9245a5STakashi Iwai * When both @size and @max are zero, the function only sets up the buffer
377ac9245a5STakashi Iwai * for later dynamic allocations. It's used typically for buffers with
378ac9245a5STakashi Iwai * SNDRV_DMA_TYPE_VMALLOC type.
379ac9245a5STakashi Iwai *
380ac9245a5STakashi Iwai * Upon successful buffer allocation and setup, the function returns 0.
3814e2b7067STakashi Iwai *
3824e2b7067STakashi Iwai * Return: zero if successful, or a negative error code
3830dba808eSTakashi Iwai */
snd_pcm_set_managed_buffer(struct snd_pcm_substream * substream,int type,struct device * data,size_t size,size_t max)384ac9245a5STakashi Iwai int snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
3850dba808eSTakashi Iwai struct device *data, size_t size, size_t max)
3860dba808eSTakashi Iwai {
387ac9245a5STakashi Iwai return preallocate_pages(substream, type, data, size, max, true);
3880dba808eSTakashi Iwai }
3890dba808eSTakashi Iwai EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
3900dba808eSTakashi Iwai
3910dba808eSTakashi Iwai /**
3920dba808eSTakashi Iwai * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
3930dba808eSTakashi Iwai * for all substreams
3940dba808eSTakashi Iwai * @pcm: the pcm instance
3950dba808eSTakashi Iwai * @type: DMA type (SNDRV_DMA_TYPE_*)
3960dba808eSTakashi Iwai * @data: DMA type dependent data
3970dba808eSTakashi Iwai * @size: the requested pre-allocation size in bytes
3980dba808eSTakashi Iwai * @max: the max. allowed pre-allocation size
3990dba808eSTakashi Iwai *
4000dba808eSTakashi Iwai * Do pre-allocation to all substreams of the given pcm for the specified DMA
4010dba808eSTakashi Iwai * type and size, and set the managed_buffer_alloc flag to each substream.
4024e2b7067STakashi Iwai *
4034e2b7067STakashi Iwai * Return: zero if successful, or a negative error code
4040dba808eSTakashi Iwai */
snd_pcm_set_managed_buffer_all(struct snd_pcm * pcm,int type,struct device * data,size_t size,size_t max)405ac9245a5STakashi Iwai int snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
4060dba808eSTakashi Iwai struct device *data,
4070dba808eSTakashi Iwai size_t size, size_t max)
4080dba808eSTakashi Iwai {
409ac9245a5STakashi Iwai return preallocate_pages_for_all(pcm, type, data, size, max, true);
4100dba808eSTakashi Iwai }
4110dba808eSTakashi Iwai EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
4120dba808eSTakashi Iwai
4131da177e4SLinus Torvalds /**
4141da177e4SLinus Torvalds * snd_pcm_lib_malloc_pages - allocate the DMA buffer
4151da177e4SLinus Torvalds * @substream: the substream to allocate the DMA buffer to
4161da177e4SLinus Torvalds * @size: the requested buffer size in bytes
4171da177e4SLinus Torvalds *
4181da177e4SLinus Torvalds * Allocates the DMA buffer on the BUS type given earlier to
4191da177e4SLinus Torvalds * snd_pcm_lib_preallocate_xxx_pages().
4201da177e4SLinus Torvalds *
421eb7c06e8SYacine Belkadi * Return: 1 if the buffer is changed, 0 if not changed, or a negative
4221da177e4SLinus Torvalds * code on failure.
4231da177e4SLinus Torvalds */
snd_pcm_lib_malloc_pages(struct snd_pcm_substream * substream,size_t size)424877211f5STakashi Iwai int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
4251da177e4SLinus Torvalds {
426b658cbabSPierre-Louis Bossart struct snd_card *card;
427877211f5STakashi Iwai struct snd_pcm_runtime *runtime;
4281da177e4SLinus Torvalds struct snd_dma_buffer *dmab = NULL;
4291da177e4SLinus Torvalds
4307eaa943cSTakashi Iwai if (PCM_RUNTIME_CHECK(substream))
4317eaa943cSTakashi Iwai return -EINVAL;
4327eaa943cSTakashi Iwai if (snd_BUG_ON(substream->dma_buffer.dev.type ==
4337eaa943cSTakashi Iwai SNDRV_DMA_TYPE_UNKNOWN))
4347eaa943cSTakashi Iwai return -EINVAL;
4351da177e4SLinus Torvalds runtime = substream->runtime;
436b658cbabSPierre-Louis Bossart card = substream->pcm->card;
4371da177e4SLinus Torvalds
4381da177e4SLinus Torvalds if (runtime->dma_buffer_p) {
4391da177e4SLinus Torvalds /* perphaps, we might free the large DMA memory region
4401da177e4SLinus Torvalds to save some space here, but the actual solution
4411da177e4SLinus Torvalds costs us less time */
4421da177e4SLinus Torvalds if (runtime->dma_buffer_p->bytes >= size) {
4431da177e4SLinus Torvalds runtime->dma_bytes = size;
4441da177e4SLinus Torvalds return 0; /* ok, do not change */
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds snd_pcm_lib_free_pages(substream);
4471da177e4SLinus Torvalds }
448877211f5STakashi Iwai if (substream->dma_buffer.area != NULL &&
449877211f5STakashi Iwai substream->dma_buffer.bytes >= size) {
4501da177e4SLinus Torvalds dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
4511da177e4SLinus Torvalds } else {
452ac9245a5STakashi Iwai /* dma_max=0 means the fixed size preallocation */
453ac9245a5STakashi Iwai if (substream->dma_buffer.area && !substream->dma_max)
454ac9245a5STakashi Iwai return -ENOMEM;
455ca2c0966STakashi Iwai dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
4561da177e4SLinus Torvalds if (! dmab)
4571da177e4SLinus Torvalds return -ENOMEM;
4581da177e4SLinus Torvalds dmab->dev = substream->dma_buffer.dev;
459d4cfb30fSTakashi Iwai if (do_alloc_pages(card,
460d4cfb30fSTakashi Iwai substream->dma_buffer.dev.type,
4611da177e4SLinus Torvalds substream->dma_buffer.dev.dev,
462a25684a9STakashi Iwai substream->stream,
4631da177e4SLinus Torvalds size, dmab) < 0) {
4641da177e4SLinus Torvalds kfree(dmab);
465dc85fc9dSAmadeusz Sławiński pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
466dc85fc9dSAmadeusz Sławiński substream->pcm->card->number, substream->pcm->device,
467dc85fc9dSAmadeusz Sławiński substream->stream ? 'c' : 'p', substream->number,
468dc85fc9dSAmadeusz Sławiński substream->pcm->name, size);
4691da177e4SLinus Torvalds return -ENOMEM;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds snd_pcm_set_runtime_buffer(substream, dmab);
4731da177e4SLinus Torvalds runtime->dma_bytes = size;
4741da177e4SLinus Torvalds return 1; /* area was changed */
4751da177e4SLinus Torvalds }
476e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
477e88e8ae6STakashi Iwai
4781da177e4SLinus Torvalds /**
4791da177e4SLinus Torvalds * snd_pcm_lib_free_pages - release the allocated DMA buffer.
4801da177e4SLinus Torvalds * @substream: the substream to release the DMA buffer
4811da177e4SLinus Torvalds *
4821da177e4SLinus Torvalds * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
4831da177e4SLinus Torvalds *
484eb7c06e8SYacine Belkadi * Return: Zero if successful, or a negative error code on failure.
4851da177e4SLinus Torvalds */
snd_pcm_lib_free_pages(struct snd_pcm_substream * substream)486877211f5STakashi Iwai int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
4871da177e4SLinus Torvalds {
488877211f5STakashi Iwai struct snd_pcm_runtime *runtime;
4891da177e4SLinus Torvalds
4907eaa943cSTakashi Iwai if (PCM_RUNTIME_CHECK(substream))
4917eaa943cSTakashi Iwai return -EINVAL;
4921da177e4SLinus Torvalds runtime = substream->runtime;
4931da177e4SLinus Torvalds if (runtime->dma_area == NULL)
4941da177e4SLinus Torvalds return 0;
4951da177e4SLinus Torvalds if (runtime->dma_buffer_p != &substream->dma_buffer) {
496011b559bSColin Ian King struct snd_card *card = substream->pcm->card;
497011b559bSColin Ian King
4981da177e4SLinus Torvalds /* it's a newly allocated buffer. release it now. */
499d4cfb30fSTakashi Iwai do_free_pages(card, runtime->dma_buffer_p);
5001da177e4SLinus Torvalds kfree(runtime->dma_buffer_p);
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds snd_pcm_set_runtime_buffer(substream, NULL);
5031da177e4SLinus Torvalds return 0;
5041da177e4SLinus Torvalds }
505e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_lib_free_pages);
506681b84e1SClemens Ladisch
_snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream * substream,size_t size,gfp_t gfp_flags)507681b84e1SClemens Ladisch int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
508681b84e1SClemens Ladisch size_t size, gfp_t gfp_flags)
509681b84e1SClemens Ladisch {
510681b84e1SClemens Ladisch struct snd_pcm_runtime *runtime;
511681b84e1SClemens Ladisch
512681b84e1SClemens Ladisch if (PCM_RUNTIME_CHECK(substream))
513681b84e1SClemens Ladisch return -EINVAL;
514681b84e1SClemens Ladisch runtime = substream->runtime;
515681b84e1SClemens Ladisch if (runtime->dma_area) {
516681b84e1SClemens Ladisch if (runtime->dma_bytes >= size)
517681b84e1SClemens Ladisch return 0; /* already large enough */
518681b84e1SClemens Ladisch vfree(runtime->dma_area);
519681b84e1SClemens Ladisch }
52088dca4caSChristoph Hellwig runtime->dma_area = __vmalloc(size, gfp_flags);
521681b84e1SClemens Ladisch if (!runtime->dma_area)
522681b84e1SClemens Ladisch return -ENOMEM;
523681b84e1SClemens Ladisch runtime->dma_bytes = size;
524681b84e1SClemens Ladisch return 1;
525681b84e1SClemens Ladisch }
526681b84e1SClemens Ladisch EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
527681b84e1SClemens Ladisch
528681b84e1SClemens Ladisch /**
529681b84e1SClemens Ladisch * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
530681b84e1SClemens Ladisch * @substream: the substream with a buffer allocated by
531681b84e1SClemens Ladisch * snd_pcm_lib_alloc_vmalloc_buffer()
532eb7c06e8SYacine Belkadi *
533eb7c06e8SYacine Belkadi * Return: Zero if successful, or a negative error code on failure.
534681b84e1SClemens Ladisch */
snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream * substream)535681b84e1SClemens Ladisch int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
536681b84e1SClemens Ladisch {
537681b84e1SClemens Ladisch struct snd_pcm_runtime *runtime;
538681b84e1SClemens Ladisch
539681b84e1SClemens Ladisch if (PCM_RUNTIME_CHECK(substream))
540681b84e1SClemens Ladisch return -EINVAL;
541681b84e1SClemens Ladisch runtime = substream->runtime;
542681b84e1SClemens Ladisch vfree(runtime->dma_area);
543681b84e1SClemens Ladisch runtime->dma_area = NULL;
544681b84e1SClemens Ladisch return 0;
545681b84e1SClemens Ladisch }
546681b84e1SClemens Ladisch EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
547681b84e1SClemens Ladisch
548681b84e1SClemens Ladisch /**
549681b84e1SClemens Ladisch * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
550681b84e1SClemens Ladisch * @substream: the substream with a buffer allocated by
551681b84e1SClemens Ladisch * snd_pcm_lib_alloc_vmalloc_buffer()
552681b84e1SClemens Ladisch * @offset: offset in the buffer
553681b84e1SClemens Ladisch *
554681b84e1SClemens Ladisch * This function is to be used as the page callback in the PCM ops.
555eb7c06e8SYacine Belkadi *
556eb7c06e8SYacine Belkadi * Return: The page struct, or %NULL on failure.
557681b84e1SClemens Ladisch */
snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream * substream,unsigned long offset)558681b84e1SClemens Ladisch struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
559681b84e1SClemens Ladisch unsigned long offset)
560681b84e1SClemens Ladisch {
561681b84e1SClemens Ladisch return vmalloc_to_page(substream->runtime->dma_area + offset);
562681b84e1SClemens Ladisch }
563681b84e1SClemens Ladisch EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);
564