123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- // SPDX-License-Identifier: GPL-2.0-or-later
- /*
- * Digital Audio (PCM) abstract layer
- * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
- */
- #include <linux/io.h>
- #include <linux/time.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/moduleparam.h>
- #include <linux/vmalloc.h>
- #include <linux/export.h>
- #include <sound/core.h>
- #include <sound/pcm.h>
- #include <sound/info.h>
- #include <sound/initval.h>
- #include "pcm_local.h"
- static int preallocate_dma = 1;
- module_param(preallocate_dma, int, 0444);
- MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
- static int maximum_substreams = 4;
- module_param(maximum_substreams, int, 0444);
- MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
- static const size_t snd_minimum_buffer = 16384;
- static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL;
- module_param(max_alloc_per_card, ulong, 0644);
- MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
- static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
- size_t size, struct snd_dma_buffer *dmab)
- {
- int err;
- if (max_alloc_per_card &&
- card->total_pcm_alloc_bytes + size > max_alloc_per_card)
- return -ENOMEM;
- err = snd_dma_alloc_pages(type, dev, size, dmab);
- if (!err) {
- mutex_lock(&card->memory_mutex);
- card->total_pcm_alloc_bytes += dmab->bytes;
- mutex_unlock(&card->memory_mutex);
- }
- return err;
- }
- static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab)
- {
- if (!dmab->area)
- return;
- mutex_lock(&card->memory_mutex);
- WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes);
- card->total_pcm_alloc_bytes -= dmab->bytes;
- mutex_unlock(&card->memory_mutex);
- snd_dma_free_pages(dmab);
- dmab->area = NULL;
- }
- /*
- * try to allocate as the large pages as possible.
- * stores the resultant memory size in *res_size.
- *
- * the minimum size is snd_minimum_buffer. it should be power of 2.
- */
- static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size)
- {
- struct snd_dma_buffer *dmab = &substream->dma_buffer;
- struct snd_card *card = substream->pcm->card;
- size_t orig_size = size;
- int err;
- do {
- err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev,
- size, dmab);
- if (err != -ENOMEM)
- return err;
- size >>= 1;
- } while (size >= snd_minimum_buffer);
- dmab->bytes = 0; /* tell error */
- pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n",
- substream->pcm->card->number, substream->pcm->device,
- substream->stream ? 'c' : 'p', substream->number,
- substream->pcm->name, orig_size);
- return 0;
- }
- /*
- * release the preallocated buffer if not yet done.
- */
- static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream)
- {
- do_free_pages(substream->pcm->card, &substream->dma_buffer);
- }
- /**
- * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
- * @substream: the pcm substream instance
- *
- * Releases the pre-allocated buffer of the given substream.
- */
- void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream)
- {
- snd_pcm_lib_preallocate_dma_free(substream);
- }
- /**
- * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
- * @pcm: the pcm instance
- *
- * Releases all the pre-allocated buffers on the given pcm.
- */
- void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
- {
- struct snd_pcm_substream *substream;
- int stream;
- for (stream = 0; stream < 2; stream++)
- for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
- snd_pcm_lib_preallocate_free(substream);
- }
- EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
- #ifdef CONFIG_SND_VERBOSE_PROCFS
- /*
- * read callback for prealloc proc file
- *
- * prints the current allocated size in kB.
- */
- static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry,
- struct snd_info_buffer *buffer)
- {
- struct snd_pcm_substream *substream = entry->private_data;
- snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
- }
- /*
- * read callback for prealloc_max proc file
- *
- * prints the maximum allowed size in kB.
- */
- static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry,
- struct snd_info_buffer *buffer)
- {
- struct snd_pcm_substream *substream = entry->private_data;
- snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024);
- }
- /*
- * write callback for prealloc proc file
- *
- * accepts the preallocation size in kB.
- */
- static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
- struct snd_info_buffer *buffer)
- {
- struct snd_pcm_substream *substream = entry->private_data;
- struct snd_card *card = substream->pcm->card;
- char line[64], str[64];
- size_t size;
- struct snd_dma_buffer new_dmab;
- mutex_lock(&substream->pcm->open_mutex);
- if (substream->runtime) {
- buffer->error = -EBUSY;
- goto unlock;
- }
- if (!snd_info_get_line(buffer, line, sizeof(line))) {
- snd_info_get_str(str, line, sizeof(str));
- size = simple_strtoul(str, NULL, 10) * 1024;
- if ((size != 0 && size < 8192) || size > substream->dma_max) {
- buffer->error = -EINVAL;
- goto unlock;
- }
- if (substream->dma_buffer.bytes == size)
- goto unlock;
- memset(&new_dmab, 0, sizeof(new_dmab));
- new_dmab.dev = substream->dma_buffer.dev;
- if (size > 0) {
- if (do_alloc_pages(card,
- substream->dma_buffer.dev.type,
- substream->dma_buffer.dev.dev,
- size, &new_dmab) < 0) {
- buffer->error = -ENOMEM;
- goto unlock;
- }
- substream->buffer_bytes_max = size;
- } else {
- substream->buffer_bytes_max = UINT_MAX;
- }
- if (substream->dma_buffer.area)
- do_free_pages(card, &substream->dma_buffer);
- substream->dma_buffer = new_dmab;
- } else {
- buffer->error = -EINVAL;
- }
- unlock:
- mutex_unlock(&substream->pcm->open_mutex);
- }
- static inline void preallocate_info_init(struct snd_pcm_substream *substream)
- {
- struct snd_info_entry *entry;
- entry = snd_info_create_card_entry(substream->pcm->card, "prealloc",
- substream->proc_root);
- if (entry) {
- snd_info_set_text_ops(entry, substream,
- snd_pcm_lib_preallocate_proc_read);
- entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
- entry->mode |= 0200;
- }
- entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max",
- substream->proc_root);
- if (entry)
- snd_info_set_text_ops(entry, substream,
- snd_pcm_lib_preallocate_max_proc_read);
- }
- #else /* !CONFIG_SND_VERBOSE_PROCFS */
- #define preallocate_info_init(s)
- #endif /* CONFIG_SND_VERBOSE_PROCFS */
- /*
- * pre-allocate the buffer and create a proc file for the substream
- */
- static void preallocate_pages(struct snd_pcm_substream *substream,
- int type, struct device *data,
- size_t size, size_t max, bool managed)
- {
- if (snd_BUG_ON(substream->dma_buffer.dev.type))
- return;
- substream->dma_buffer.dev.type = type;
- substream->dma_buffer.dev.dev = data;
- if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
- preallocate_pcm_pages(substream, size);
- if (substream->dma_buffer.bytes > 0)
- substream->buffer_bytes_max = substream->dma_buffer.bytes;
- substream->dma_max = max;
- if (max > 0)
- preallocate_info_init(substream);
- if (managed)
- substream->managed_buffer_alloc = 1;
- }
- static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
- void *data, size_t size, size_t max,
- bool managed)
- {
- struct snd_pcm_substream *substream;
- int stream;
- for (stream = 0; stream < 2; stream++)
- for (substream = pcm->streams[stream].substream; substream;
- substream = substream->next)
- preallocate_pages(substream, type, data, size, max,
- managed);
- }
- /**
- * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
- * @substream: the pcm substream instance
- * @type: DMA type (SNDRV_DMA_TYPE_*)
- * @data: DMA type dependent data
- * @size: the requested pre-allocation size in bytes
- * @max: the max. allowed pre-allocation size
- *
- * Do pre-allocation for the given DMA buffer type.
- */
- void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
- int type, struct device *data,
- size_t size, size_t max)
- {
- preallocate_pages(substream, type, data, size, max, false);
- }
- EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
- /**
- * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams)
- * @pcm: the pcm instance
- * @type: DMA type (SNDRV_DMA_TYPE_*)
- * @data: DMA type dependent data
- * @size: the requested pre-allocation size in bytes
- * @max: the max. allowed pre-allocation size
- *
- * Do pre-allocation to all substreams of the given pcm for the
- * specified DMA type.
- */
- void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
- int type, void *data,
- size_t size, size_t max)
- {
- preallocate_pages_for_all(pcm, type, data, size, max, false);
- }
- EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
- /**
- * snd_pcm_set_managed_buffer - set up buffer management for a substream
- * @substream: the pcm substream instance
- * @type: DMA type (SNDRV_DMA_TYPE_*)
- * @data: DMA type dependent data
- * @size: the requested pre-allocation size in bytes
- * @max: the max. allowed pre-allocation size
- *
- * Do pre-allocation for the given DMA buffer type, and set the managed
- * buffer allocation mode to the given substream.
- * In this mode, PCM core will allocate a buffer automatically before PCM
- * hw_params ops call, and release the buffer after PCM hw_free ops call
- * as well, so that the driver doesn't need to invoke the allocation and
- * the release explicitly in its callback.
- * When a buffer is actually allocated before the PCM hw_params call, it
- * turns on the runtime buffer_changed flag for drivers changing their h/w
- * parameters accordingly.
- */
- void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
- struct device *data, size_t size, size_t max)
- {
- preallocate_pages(substream, type, data, size, max, true);
- }
- EXPORT_SYMBOL(snd_pcm_set_managed_buffer);
- /**
- * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams
- * for all substreams
- * @pcm: the pcm instance
- * @type: DMA type (SNDRV_DMA_TYPE_*)
- * @data: DMA type dependent data
- * @size: the requested pre-allocation size in bytes
- * @max: the max. allowed pre-allocation size
- *
- * Do pre-allocation to all substreams of the given pcm for the specified DMA
- * type and size, and set the managed_buffer_alloc flag to each substream.
- */
- void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
- struct device *data,
- size_t size, size_t max)
- {
- preallocate_pages_for_all(pcm, type, data, size, max, true);
- }
- EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all);
- #ifdef CONFIG_SND_DMA_SGBUF
- /*
- * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
- * @substream: the pcm substream instance
- * @offset: the buffer offset
- *
- * Used as the page callback of PCM ops.
- *
- * Return: The page struct at the given buffer offset. %NULL on failure.
- */
- struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset)
- {
- struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
- unsigned int idx = offset >> PAGE_SHIFT;
- if (idx >= (unsigned int)sgbuf->pages)
- return NULL;
- return sgbuf->page_table[idx];
- }
- #endif /* CONFIG_SND_DMA_SGBUF */
- /**
- * snd_pcm_lib_malloc_pages - allocate the DMA buffer
- * @substream: the substream to allocate the DMA buffer to
- * @size: the requested buffer size in bytes
- *
- * Allocates the DMA buffer on the BUS type given earlier to
- * snd_pcm_lib_preallocate_xxx_pages().
- *
- * Return: 1 if the buffer is changed, 0 if not changed, or a negative
- * code on failure.
- */
- int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
- {
- struct snd_card *card;
- struct snd_pcm_runtime *runtime;
- struct snd_dma_buffer *dmab = NULL;
- if (PCM_RUNTIME_CHECK(substream))
- return -EINVAL;
- if (snd_BUG_ON(substream->dma_buffer.dev.type ==
- SNDRV_DMA_TYPE_UNKNOWN))
- return -EINVAL;
- runtime = substream->runtime;
- card = substream->pcm->card;
- if (runtime->dma_buffer_p) {
- /* perphaps, we might free the large DMA memory region
- to save some space here, but the actual solution
- costs us less time */
- if (runtime->dma_buffer_p->bytes >= size) {
- runtime->dma_bytes = size;
- return 0; /* ok, do not change */
- }
- snd_pcm_lib_free_pages(substream);
- }
- if (substream->dma_buffer.area != NULL &&
- substream->dma_buffer.bytes >= size) {
- dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
- } else {
- dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
- if (! dmab)
- return -ENOMEM;
- dmab->dev = substream->dma_buffer.dev;
- if (do_alloc_pages(card,
- substream->dma_buffer.dev.type,
- substream->dma_buffer.dev.dev,
- size, dmab) < 0) {
- kfree(dmab);
- return -ENOMEM;
- }
- }
- snd_pcm_set_runtime_buffer(substream, dmab);
- runtime->dma_bytes = size;
- return 1; /* area was changed */
- }
- EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
- /**
- * snd_pcm_lib_free_pages - release the allocated DMA buffer.
- * @substream: the substream to release the DMA buffer
- *
- * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
- *
- * Return: Zero if successful, or a negative error code on failure.
- */
- int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
- {
- struct snd_card *card = substream->pcm->card;
- struct snd_pcm_runtime *runtime;
- if (PCM_RUNTIME_CHECK(substream))
- return -EINVAL;
- runtime = substream->runtime;
- if (runtime->dma_area == NULL)
- return 0;
- if (runtime->dma_buffer_p != &substream->dma_buffer) {
- /* it's a newly allocated buffer. release it now. */
- do_free_pages(card, runtime->dma_buffer_p);
- kfree(runtime->dma_buffer_p);
- }
- snd_pcm_set_runtime_buffer(substream, NULL);
- return 0;
- }
- EXPORT_SYMBOL(snd_pcm_lib_free_pages);
- int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
- size_t size, gfp_t gfp_flags)
- {
- struct snd_pcm_runtime *runtime;
- if (PCM_RUNTIME_CHECK(substream))
- return -EINVAL;
- runtime = substream->runtime;
- if (runtime->dma_area) {
- if (runtime->dma_bytes >= size)
- return 0; /* already large enough */
- vfree(runtime->dma_area);
- }
- runtime->dma_area = __vmalloc(size, gfp_flags);
- if (!runtime->dma_area)
- return -ENOMEM;
- runtime->dma_bytes = size;
- return 1;
- }
- EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer);
- /**
- * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer
- * @substream: the substream with a buffer allocated by
- * snd_pcm_lib_alloc_vmalloc_buffer()
- *
- * Return: Zero if successful, or a negative error code on failure.
- */
- int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime;
- if (PCM_RUNTIME_CHECK(substream))
- return -EINVAL;
- runtime = substream->runtime;
- vfree(runtime->dma_area);
- runtime->dma_area = NULL;
- return 0;
- }
- EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer);
- /**
- * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct
- * @substream: the substream with a buffer allocated by
- * snd_pcm_lib_alloc_vmalloc_buffer()
- * @offset: offset in the buffer
- *
- * This function is to be used as the page callback in the PCM ops.
- *
- * Return: The page struct, or %NULL on failure.
- */
- struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
- unsigned long offset)
- {
- return vmalloc_to_page(substream->runtime->dma_area + offset);
- }
- EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);
|