ALSA: core: Add device-managed page allocator helper
authorTakashi Iwai <tiwai@suse.de>
Thu, 15 Jul 2021 07:58:23 +0000 (09:58 +0200)
committerTakashi Iwai <tiwai@suse.de>
Mon, 19 Jul 2021 14:16:32 +0000 (16:16 +0200)
This is a preparation for allowing devres usages more widely in
various sound drivers.  As a first step, this patch adds a new
allocator function, snd_devm_alloc_pages(), to manage the allocated
pages via devres, so that the pages will be automagically released as
device unbinding.

Unlike the old snd_dma_alloc_pages(), the new function returns
directly the snd_dma_buffer pointer.  The caller needs NULL-check for
the allocation error appropriately.

Also, since a real device pointer is mandatory for devres,
SNDRV_DMA_TYPE_CONTINUOUS or SNDRV_DMA_TYPE_VMALLOC type can't be used
for this function.

Link: https://lore.kernel.org/r/20210715075941.23332-2-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
include/sound/memalloc.h
sound/core/memalloc.c

index 44d87775b3527483554780aa260c92451744f31a..d22c9387b2babcd8636ea4b69a5613e0b65ebbe7 100644 (file)
@@ -79,5 +79,9 @@ struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset);
 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
                                      unsigned int ofs, unsigned int size);
 
+/* device-managed memory allocator */
+struct snd_dma_buffer *snd_devm_alloc_pages(struct device *dev, int type,
+                                           size_t size);
+
 #endif /* __SOUND_MEMALLOC_H */
 
index 83b79edfa52d81cec1a095f6a62eef06565e794f..3a78fdad1ab4132f785a1f90a4b5f54cc2a0f1a2 100644 (file)
@@ -127,6 +127,52 @@ void snd_dma_free_pages(struct snd_dma_buffer *dmab)
 }
 EXPORT_SYMBOL(snd_dma_free_pages);
 
+/* called by devres */
+static void __snd_release_pages(struct device *dev, void *res)
+{
+       snd_dma_free_pages(res);
+}
+
+/**
+ * snd_devm_alloc_pages - allocate the buffer and manage with devres
+ * @dev: the device pointer
+ * @type: the DMA buffer type
+ * @size: the buffer size to allocate
+ *
+ * Allocate buffer pages depending on the given type and manage using devres.
+ * The pages will be released automatically at the device removal.
+ *
+ * Unlike snd_dma_alloc_pages(), this function requires the real device pointer,
+ * hence it can't work with SNDRV_DMA_TYPE_CONTINUOUS or
+ * SNDRV_DMA_TYPE_VMALLOC type.
+ *
+ * The function returns the snd_dma_buffer object at success, or NULL if failed.
+ */
+struct snd_dma_buffer *
+snd_devm_alloc_pages(struct device *dev, int type, size_t size)
+{
+       struct snd_dma_buffer *dmab;
+       int err;
+
+       if (WARN_ON(type == SNDRV_DMA_TYPE_CONTINUOUS ||
+                   type == SNDRV_DMA_TYPE_VMALLOC))
+               return NULL;
+
+       dmab = devres_alloc(__snd_release_pages, sizeof(*dmab), GFP_KERNEL);
+       if (!dmab)
+               return NULL;
+
+       err = snd_dma_alloc_pages(type, dev, size, dmab);
+       if (err < 0) {
+               devres_free(dmab);
+               return NULL;
+       }
+
+       devres_add(dev, dmab);
+       return dmab;
+}
+EXPORT_SYMBOL_GPL(snd_devm_alloc_pages);
+
 /**
  * snd_dma_buffer_mmap - perform mmap of the given DMA buffer
  * @dmab: buffer allocation information