blob: 6850d13aa98c5ae4f1ef318618757a3c53fef84f [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Takashi Iwai <tiwai@suse.de>
5 *
6 * Generic memory allocators
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
10#include <linux/mm.h>
11#include <linux/dma-mapping.h>
Nicolin Chen05503212013-10-23 11:47:43 +080012#include <linux/genalloc.h>
Takashi Iwai42e748a2018-08-08 17:01:00 +020013#ifdef CONFIG_X86
14#include <asm/set_memory.h>
15#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <sound/memalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Bus-specific memory allocators
21 *
22 */
23
Takashi Iwai8f115512007-07-26 18:59:36 +020024#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* allocate the coherent DMA pages */
Takashi Iwai28f3f4f2018-08-10 14:43:37 +020026static void snd_malloc_dev_pages(struct snd_dma_buffer *dmab, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Al Viro1ef64e62005-10-21 03:22:18 -040028 gfp_t gfp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 gfp_flags = GFP_KERNEL
Hugh Dickinsf3d48f02005-11-21 21:32:22 -080031 | __GFP_COMP /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 | __GFP_NORETRY /* don't trigger OOM-killer */
33 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
Takashi Iwai28f3f4f2018-08-10 14:43:37 +020034 dmab->area = dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr,
35 gfp_flags);
Takashi Iwai42e748a2018-08-08 17:01:00 +020036#ifdef CONFIG_X86
37 if (dmab->area && dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC)
38 set_memory_wc((unsigned long)dmab->area,
39 PAGE_ALIGN(size) >> PAGE_SHIFT);
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43/* free the coherent DMA pages */
Takashi Iwai28f3f4f2018-08-10 14:43:37 +020044static void snd_free_dev_pages(struct snd_dma_buffer *dmab)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Takashi Iwai42e748a2018-08-08 17:01:00 +020046#ifdef CONFIG_X86
47 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC)
48 set_memory_wb((unsigned long)dmab->area,
49 PAGE_ALIGN(dmab->bytes) >> PAGE_SHIFT);
50#endif
Takashi Iwai28f3f4f2018-08-10 14:43:37 +020051 dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Nicolin Chen05503212013-10-23 11:47:43 +080053
Takashi Iwai63437312013-10-28 16:08:27 +010054#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +080055/**
56 * snd_malloc_dev_iram - allocate memory from on-chip internal ram
57 * @dmab: buffer allocation record to store the allocated data
58 * @size: number of bytes to allocate from the iram
59 *
60 * This function requires iram phandle provided via of_node
61 */
Takashi Iwai9f694bc2013-10-29 11:56:21 +010062static void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size)
Nicolin Chen05503212013-10-23 11:47:43 +080063{
64 struct device *dev = dmab->dev.dev;
65 struct gen_pool *pool = NULL;
66
Takashi Iwaia40a3932013-10-29 11:59:31 +010067 dmab->area = NULL;
68 dmab->addr = 0;
69
Nicolin Chen05503212013-10-23 11:47:43 +080070 if (dev->of_node)
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -070071 pool = of_gen_pool_get(dev->of_node, "iram", 0);
Nicolin Chen05503212013-10-23 11:47:43 +080072
73 if (!pool)
74 return;
75
76 /* Assign the pool into private_data field */
77 dmab->private_data = pool;
78
Nicolin Chen07968fe2013-11-14 14:32:15 -080079 dmab->area = gen_pool_dma_alloc(pool, size, &dmab->addr);
Nicolin Chen05503212013-10-23 11:47:43 +080080}
81
82/**
83 * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
84 * @dmab: buffer allocation record to store the allocated data
85 */
Takashi Iwai9f694bc2013-10-29 11:56:21 +010086static void snd_free_dev_iram(struct snd_dma_buffer *dmab)
Nicolin Chen05503212013-10-23 11:47:43 +080087{
88 struct gen_pool *pool = dmab->private_data;
89
90 if (pool && dmab->area)
91 gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
92}
Takashi Iwai63437312013-10-28 16:08:27 +010093#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai8f115512007-07-26 18:59:36 +020094#endif /* CONFIG_HAS_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 *
98 * ALSA generic memory management
99 *
100 */
101
102
103/**
104 * snd_dma_alloc_pages - allocate the buffer area according to the given type
105 * @type: the DMA buffer type
106 * @device: the device pointer
107 * @size: the buffer size to allocate
108 * @dmab: buffer allocation record to store the allocated data
109 *
110 * Calls the memory-allocator function for the corresponding
111 * buffer type.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100112 *
113 * Return: Zero if the buffer with the given size is allocated successfully,
114 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116int snd_dma_alloc_pages(int type, struct device *device, size_t size,
117 struct snd_dma_buffer *dmab)
118{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200119 if (WARN_ON(!size))
120 return -ENXIO;
121 if (WARN_ON(!dmab))
122 return -ENXIO;
Takashi Iwai6ce1d632019-02-04 14:34:00 +0100123 if (WARN_ON(!device))
124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 dmab->dev.type = type;
127 dmab->dev.dev = device;
128 dmab->bytes = 0;
129 switch (type) {
130 case SNDRV_DMA_TYPE_CONTINUOUS:
Takashi Iwai734b5a02018-11-23 19:38:13 +0100131 dmab->area = alloc_pages_exact(size,
132 (__force gfp_t)(unsigned long)device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 dmab->addr = 0;
134 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200135#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200136#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800137 case SNDRV_DMA_TYPE_DEV_IRAM:
138 snd_malloc_dev_iram(dmab, size);
139 if (dmab->area)
140 break;
141 /* Internal memory might have limited size and no enough space,
142 * so if we fail to malloc, try to fetch memory traditionally.
143 */
144 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200145#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai3c4cfa72018-10-04 19:54:51 +0200146 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case SNDRV_DMA_TYPE_DEV:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200148 case SNDRV_DMA_TYPE_DEV_UC:
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200149 snd_malloc_dev_pages(dmab, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200151#endif
152#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 case SNDRV_DMA_TYPE_DEV_SG:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200154 case SNDRV_DMA_TYPE_DEV_UC_SG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
156 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 default:
Takashi Iwaif2f93072014-02-04 18:21:03 +0100159 pr_err("snd-malloc: invalid device type %d\n", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 dmab->area = NULL;
161 dmab->addr = 0;
162 return -ENXIO;
163 }
164 if (! dmab->area)
165 return -ENOMEM;
166 dmab->bytes = size;
167 return 0;
168}
Takashi Iwai35f80012017-06-16 16:16:33 +0200169EXPORT_SYMBOL(snd_dma_alloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/**
172 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
173 * @type: the DMA buffer type
174 * @device: the device pointer
175 * @size: the buffer size to allocate
176 * @dmab: buffer allocation record to store the allocated data
177 *
178 * Calls the memory-allocator function for the corresponding
179 * buffer type. When no space is left, this function reduces the size and
180 * tries to allocate again. The size actually allocated is stored in
181 * res_size argument.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100182 *
183 * Return: Zero if the buffer with the given size is allocated successfully,
184 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
186int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
187 struct snd_dma_buffer *dmab)
188{
189 int err;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
192 if (err != -ENOMEM)
193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (size <= PAGE_SIZE)
195 return -ENOMEM;
Takashi Iwaidfef01e2018-07-19 11:01:04 +0200196 size >>= 1;
197 size = PAGE_SIZE << get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 if (! dmab->area)
200 return -ENOMEM;
201 return 0;
202}
Takashi Iwai35f80012017-06-16 16:16:33 +0200203EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205
206/**
207 * snd_dma_free_pages - release the allocated buffer
208 * @dmab: the buffer allocation record to release
209 *
210 * Releases the allocated buffer via snd_dma_alloc_pages().
211 */
212void snd_dma_free_pages(struct snd_dma_buffer *dmab)
213{
214 switch (dmab->dev.type) {
215 case SNDRV_DMA_TYPE_CONTINUOUS:
Takashi Iwai734b5a02018-11-23 19:38:13 +0100216 free_pages_exact(dmab->area, dmab->bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200218#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200219#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800220 case SNDRV_DMA_TYPE_DEV_IRAM:
221 snd_free_dev_iram(dmab);
222 break;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200223#endif /* CONFIG_GENERIC_ALLOCATOR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 case SNDRV_DMA_TYPE_DEV:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200225 case SNDRV_DMA_TYPE_DEV_UC:
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200226 snd_free_dev_pages(dmab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200228#endif
229#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 case SNDRV_DMA_TYPE_DEV_SG:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200231 case SNDRV_DMA_TYPE_DEV_UC_SG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 snd_free_sgbuf_pages(dmab);
233 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 default:
Takashi Iwaif2f93072014-02-04 18:21:03 +0100236 pr_err("snd-malloc: invalid device type %d\n", dmab->dev.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239EXPORT_SYMBOL(snd_dma_free_pages);