Commit 723c1252 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: memalloc: Minor refactoring

Return the pointer directly from alloc ops instead of setting
dmab->area at each place.  It simplifies the code a bit.
Acked-by: default avatarMark Brown <broonie@kernel.org>
Link: https://lore.kernel.org/r/20210802072815.13551-2-tiwai@suse.deSigned-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent d1254593
...@@ -29,12 +29,12 @@ static inline gfp_t snd_mem_get_gfp_flags(const struct snd_dma_buffer *dmab, ...@@ -29,12 +29,12 @@ static inline gfp_t snd_mem_get_gfp_flags(const struct snd_dma_buffer *dmab,
return (__force gfp_t)(unsigned long)dmab->dev.dev; return (__force gfp_t)(unsigned long)dmab->dev.dev;
} }
static int __snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size) static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
{ {
const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab); const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
if (WARN_ON_ONCE(!ops || !ops->alloc)) if (WARN_ON_ONCE(!ops || !ops->alloc))
return -EINVAL; return NULL;
return ops->alloc(dmab, size); return ops->alloc(dmab, size);
} }
...@@ -54,8 +54,6 @@ static int __snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size) ...@@ -54,8 +54,6 @@ static int __snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
int snd_dma_alloc_pages(int type, struct device *device, size_t size, int snd_dma_alloc_pages(int type, struct device *device, size_t size,
struct snd_dma_buffer *dmab) struct snd_dma_buffer *dmab)
{ {
int err;
if (WARN_ON(!size)) if (WARN_ON(!size))
return -ENXIO; return -ENXIO;
if (WARN_ON(!dmab)) if (WARN_ON(!dmab))
...@@ -65,12 +63,9 @@ int snd_dma_alloc_pages(int type, struct device *device, size_t size, ...@@ -65,12 +63,9 @@ int snd_dma_alloc_pages(int type, struct device *device, size_t size,
dmab->dev.type = type; dmab->dev.type = type;
dmab->dev.dev = device; dmab->dev.dev = device;
dmab->bytes = 0; dmab->bytes = 0;
dmab->area = NULL;
dmab->addr = 0; dmab->addr = 0;
dmab->private_data = NULL; dmab->private_data = NULL;
err = __snd_dma_alloc_pages(dmab, size); dmab->area = __snd_dma_alloc_pages(dmab, size);
if (err < 0)
return err;
if (!dmab->area) if (!dmab->area)
return -ENOMEM; return -ENOMEM;
dmab->bytes = size; dmab->bytes = size;
...@@ -244,12 +239,11 @@ EXPORT_SYMBOL(snd_sgbuf_get_chunk_size); ...@@ -244,12 +239,11 @@ EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
/* /*
* Continuous pages allocator * Continuous pages allocator
*/ */
static int snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size) static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
{ {
gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL); gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL);
dmab->area = alloc_pages_exact(size, gfp); return alloc_pages_exact(size, gfp);
return 0;
} }
static void snd_dma_continuous_free(struct snd_dma_buffer *dmab) static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
...@@ -275,12 +269,11 @@ static const struct snd_malloc_ops snd_dma_continuous_ops = { ...@@ -275,12 +269,11 @@ static const struct snd_malloc_ops snd_dma_continuous_ops = {
/* /*
* VMALLOC allocator * VMALLOC allocator
*/ */
static int snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size) static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
{ {
gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL | __GFP_HIGHMEM); gfp_t gfp = snd_mem_get_gfp_flags(dmab, GFP_KERNEL | __GFP_HIGHMEM);
dmab->area = __vmalloc(size, gfp); return __vmalloc(size, gfp);
return 0;
} }
static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab) static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
...@@ -332,20 +325,20 @@ static const struct snd_malloc_ops snd_dma_vmalloc_ops = { ...@@ -332,20 +325,20 @@ static const struct snd_malloc_ops snd_dma_vmalloc_ops = {
* IRAM allocator * IRAM allocator
*/ */
#ifdef CONFIG_GENERIC_ALLOCATOR #ifdef CONFIG_GENERIC_ALLOCATOR
static int snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size) static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
{ {
struct device *dev = dmab->dev.dev; struct device *dev = dmab->dev.dev;
struct gen_pool *pool; struct gen_pool *pool;
void *p;
if (dev->of_node) { if (dev->of_node) {
pool = of_gen_pool_get(dev->of_node, "iram", 0); pool = of_gen_pool_get(dev->of_node, "iram", 0);
/* Assign the pool into private_data field */ /* Assign the pool into private_data field */
dmab->private_data = pool; dmab->private_data = pool;
dmab->area = gen_pool_dma_alloc_align(pool, size, &dmab->addr, p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
PAGE_SIZE); if (p)
if (dmab->area) return p;
return 0;
} }
/* Internal memory might have limited size and no enough space, /* Internal memory might have limited size and no enough space,
...@@ -383,22 +376,21 @@ static const struct snd_malloc_ops snd_dma_iram_ops = { ...@@ -383,22 +376,21 @@ static const struct snd_malloc_ops snd_dma_iram_ops = {
/* /*
* Coherent device pages allocator * Coherent device pages allocator
*/ */
static int snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size) static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
{ {
gfp_t gfp_flags; gfp_t gfp_flags;
void *p;
gfp_flags = GFP_KERNEL gfp_flags = GFP_KERNEL
| __GFP_COMP /* compound page lets parts be mapped */ | __GFP_COMP /* compound page lets parts be mapped */
| __GFP_NORETRY /* don't trigger OOM-killer */ | __GFP_NORETRY /* don't trigger OOM-killer */
| __GFP_NOWARN; /* no stack trace print - this call is non-critical */ | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
dmab->area = dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, p = dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, gfp_flags);
gfp_flags);
#ifdef CONFIG_X86 #ifdef CONFIG_X86
if (dmab->area && dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC) if (p && dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC)
set_memory_wc((unsigned long)dmab->area, set_memory_wc((unsigned long)p, PAGE_ALIGN(size) >> PAGE_SHIFT);
PAGE_ALIGN(size) >> PAGE_SHIFT);
#endif #endif
return 0; return p;
} }
static void snd_dma_dev_free(struct snd_dma_buffer *dmab) static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#define __MEMALLOC_LOCAL_H #define __MEMALLOC_LOCAL_H
struct snd_malloc_ops { struct snd_malloc_ops {
int (*alloc)(struct snd_dma_buffer *dmab, size_t size); void *(*alloc)(struct snd_dma_buffer *dmab, size_t size);
void (*free)(struct snd_dma_buffer *dmab); void (*free)(struct snd_dma_buffer *dmab);
dma_addr_t (*get_addr)(struct snd_dma_buffer *dmab, size_t offset); dma_addr_t (*get_addr)(struct snd_dma_buffer *dmab, size_t offset);
struct page *(*get_page)(struct snd_dma_buffer *dmab, size_t offset); struct page *(*get_page)(struct snd_dma_buffer *dmab, size_t offset);
......
...@@ -63,7 +63,7 @@ static void snd_dma_sg_free(struct snd_dma_buffer *dmab) ...@@ -63,7 +63,7 @@ static void snd_dma_sg_free(struct snd_dma_buffer *dmab)
#define MAX_ALLOC_PAGES 32 #define MAX_ALLOC_PAGES 32
static int snd_dma_sg_alloc(struct snd_dma_buffer *dmab, size_t size) static void *snd_dma_sg_alloc(struct snd_dma_buffer *dmab, size_t size)
{ {
struct snd_sg_buf *sgbuf; struct snd_sg_buf *sgbuf;
unsigned int i, pages, chunk, maxpages; unsigned int i, pages, chunk, maxpages;
...@@ -72,10 +72,11 @@ static int snd_dma_sg_alloc(struct snd_dma_buffer *dmab, size_t size) ...@@ -72,10 +72,11 @@ static int snd_dma_sg_alloc(struct snd_dma_buffer *dmab, size_t size)
struct page **pgtable; struct page **pgtable;
int type = SNDRV_DMA_TYPE_DEV; int type = SNDRV_DMA_TYPE_DEV;
pgprot_t prot = PAGE_KERNEL; pgprot_t prot = PAGE_KERNEL;
void *area;
dmab->private_data = sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL); dmab->private_data = sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
if (!sgbuf) if (!sgbuf)
return -ENOMEM; return NULL;
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC_SG) { if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC_SG) {
type = SNDRV_DMA_TYPE_DEV_UC; type = SNDRV_DMA_TYPE_DEV_UC;
#ifdef pgprot_noncached #ifdef pgprot_noncached
...@@ -127,14 +128,14 @@ static int snd_dma_sg_alloc(struct snd_dma_buffer *dmab, size_t size) ...@@ -127,14 +128,14 @@ static int snd_dma_sg_alloc(struct snd_dma_buffer *dmab, size_t size)
} }
sgbuf->size = size; sgbuf->size = size;
dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, prot); area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, prot);
if (! dmab->area) if (!area)
goto _failed; goto _failed;
return 0; return area;
_failed: _failed:
snd_dma_sg_free(dmab); /* free the table */ snd_dma_sg_free(dmab); /* free the table */
return -ENOMEM; return NULL;
} }
static dma_addr_t snd_dma_sg_get_addr(struct snd_dma_buffer *dmab, static dma_addr_t snd_dma_sg_get_addr(struct snd_dma_buffer *dmab,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment