blob: 59a4adc286ed77bb647788706a6ebd9323a1b640 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Takashi Iwai <tiwai@suse.de>
4 *
5 * Generic memory allocators
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/mm.h>
26#include <linux/dma-mapping.h>
Nicolin Chen05503212013-10-23 11:47:43 +080027#include <linux/genalloc.h>
Takashi Iwai42e748a2018-08-08 17:01:00 +020028#ifdef CONFIG_X86
29#include <asm/set_memory.h>
30#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/memalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 * Generic memory allocators
36 *
37 */
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/**
40 * snd_malloc_pages - allocate pages with the given size
41 * @size: the size to allocate in bytes
42 * @gfp_flags: the allocation conditions, GFP_XXX
43 *
44 * Allocates the physically contiguous pages with the given size.
45 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +010046 * Return: The pointer of the buffer, or %NULL if no enough memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Al Viro1ef64e62005-10-21 03:22:18 -040048void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 int pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai7eaa9432008-08-08 17:09:09 +020052 if (WARN_ON(!size))
53 return NULL;
54 if (WARN_ON(!gfp_flags))
55 return NULL;
Hugh Dickinsf3d48f02005-11-21 21:32:22 -080056 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 pg = get_order(size);
Takashi Iwaid7b13542014-01-08 16:09:31 +010058 return (void *) __get_free_pages(gfp_flags, pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
Takashi Iwai35f80012017-06-16 16:16:33 +020060EXPORT_SYMBOL(snd_malloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/**
63 * snd_free_pages - release the pages
64 * @ptr: the buffer pointer to release
65 * @size: the allocated buffer size
66 *
67 * Releases the buffer allocated via snd_malloc_pages().
68 */
69void snd_free_pages(void *ptr, size_t size)
70{
71 int pg;
72
73 if (ptr == NULL)
74 return;
75 pg = get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 free_pages((unsigned long) ptr, pg);
77}
Takashi Iwai35f80012017-06-16 16:16:33 +020078EXPORT_SYMBOL(snd_free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 *
82 * Bus-specific memory allocators
83 *
84 */
85
Takashi Iwai8f115512007-07-26 18:59:36 +020086#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* allocate the coherent DMA pages */
Takashi Iwai28f3f4f2018-08-10 14:43:37 +020088static void snd_malloc_dev_pages(struct snd_dma_buffer *dmab, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Al Viro1ef64e62005-10-21 03:22:18 -040090 gfp_t gfp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 gfp_flags = GFP_KERNEL
Hugh Dickinsf3d48f02005-11-21 21:32:22 -080093 | __GFP_COMP /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 | __GFP_NORETRY /* don't trigger OOM-killer */
95 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
Takashi Iwai28f3f4f2018-08-10 14:43:37 +020096 dmab->area = dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr,
97 gfp_flags);
Takashi Iwai42e748a2018-08-08 17:01:00 +020098#ifdef CONFIG_X86
99 if (dmab->area && dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC)
100 set_memory_wc((unsigned long)dmab->area,
101 PAGE_ALIGN(size) >> PAGE_SHIFT);
102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/* free the coherent DMA pages */
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200106static void snd_free_dev_pages(struct snd_dma_buffer *dmab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Takashi Iwai42e748a2018-08-08 17:01:00 +0200108#ifdef CONFIG_X86
109 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC)
110 set_memory_wb((unsigned long)dmab->area,
111 PAGE_ALIGN(dmab->bytes) >> PAGE_SHIFT);
112#endif
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200113 dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
Nicolin Chen05503212013-10-23 11:47:43 +0800115
Takashi Iwai63437312013-10-28 16:08:27 +0100116#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800117/**
118 * snd_malloc_dev_iram - allocate memory from on-chip internal ram
119 * @dmab: buffer allocation record to store the allocated data
120 * @size: number of bytes to allocate from the iram
121 *
122 * This function requires iram phandle provided via of_node
123 */
Takashi Iwai9f694bc2013-10-29 11:56:21 +0100124static void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size)
Nicolin Chen05503212013-10-23 11:47:43 +0800125{
126 struct device *dev = dmab->dev.dev;
127 struct gen_pool *pool = NULL;
128
Takashi Iwaia40a3932013-10-29 11:59:31 +0100129 dmab->area = NULL;
130 dmab->addr = 0;
131
Nicolin Chen05503212013-10-23 11:47:43 +0800132 if (dev->of_node)
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -0700133 pool = of_gen_pool_get(dev->of_node, "iram", 0);
Nicolin Chen05503212013-10-23 11:47:43 +0800134
135 if (!pool)
136 return;
137
138 /* Assign the pool into private_data field */
139 dmab->private_data = pool;
140
Nicolin Chen07968fe2013-11-14 14:32:15 -0800141 dmab->area = gen_pool_dma_alloc(pool, size, &dmab->addr);
Nicolin Chen05503212013-10-23 11:47:43 +0800142}
143
144/**
145 * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
146 * @dmab: buffer allocation record to store the allocated data
147 */
Takashi Iwai9f694bc2013-10-29 11:56:21 +0100148static void snd_free_dev_iram(struct snd_dma_buffer *dmab)
Nicolin Chen05503212013-10-23 11:47:43 +0800149{
150 struct gen_pool *pool = dmab->private_data;
151
152 if (pool && dmab->area)
153 gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
154}
Takashi Iwai63437312013-10-28 16:08:27 +0100155#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai8f115512007-07-26 18:59:36 +0200156#endif /* CONFIG_HAS_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 *
160 * ALSA generic memory management
161 *
162 */
163
164
165/**
166 * snd_dma_alloc_pages - allocate the buffer area according to the given type
167 * @type: the DMA buffer type
168 * @device: the device pointer
169 * @size: the buffer size to allocate
170 * @dmab: buffer allocation record to store the allocated data
171 *
172 * Calls the memory-allocator function for the corresponding
173 * buffer type.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100174 *
175 * Return: Zero if the buffer with the given size is allocated successfully,
176 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178int snd_dma_alloc_pages(int type, struct device *device, size_t size,
179 struct snd_dma_buffer *dmab)
180{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200181 if (WARN_ON(!size))
182 return -ENXIO;
183 if (WARN_ON(!dmab))
184 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 dmab->dev.type = type;
187 dmab->dev.dev = device;
188 dmab->bytes = 0;
189 switch (type) {
190 case SNDRV_DMA_TYPE_CONTINUOUS:
Clemens Ladischfea952e2011-02-14 11:00:47 +0100191 dmab->area = snd_malloc_pages(size,
192 (__force gfp_t)(unsigned long)device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 dmab->addr = 0;
194 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200195#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200196#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800197 case SNDRV_DMA_TYPE_DEV_IRAM:
198 snd_malloc_dev_iram(dmab, size);
199 if (dmab->area)
200 break;
201 /* Internal memory might have limited size and no enough space,
202 * so if we fail to malloc, try to fetch memory traditionally.
203 */
204 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200205#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai3c4cfa72018-10-04 19:54:51 +0200206 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 case SNDRV_DMA_TYPE_DEV:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200208 case SNDRV_DMA_TYPE_DEV_UC:
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200209 snd_malloc_dev_pages(dmab, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200211#endif
212#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 case SNDRV_DMA_TYPE_DEV_SG:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200214 case SNDRV_DMA_TYPE_DEV_UC_SG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
216 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 default:
Takashi Iwaif2f93072014-02-04 18:21:03 +0100219 pr_err("snd-malloc: invalid device type %d\n", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dmab->area = NULL;
221 dmab->addr = 0;
222 return -ENXIO;
223 }
224 if (! dmab->area)
225 return -ENOMEM;
226 dmab->bytes = size;
227 return 0;
228}
Takashi Iwai35f80012017-06-16 16:16:33 +0200229EXPORT_SYMBOL(snd_dma_alloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/**
232 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
233 * @type: the DMA buffer type
234 * @device: the device pointer
235 * @size: the buffer size to allocate
236 * @dmab: buffer allocation record to store the allocated data
237 *
238 * Calls the memory-allocator function for the corresponding
239 * buffer type. When no space is left, this function reduces the size and
240 * tries to allocate again. The size actually allocated is stored in
241 * res_size argument.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100242 *
243 * Return: Zero if the buffer with the given size is allocated successfully,
244 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
247 struct snd_dma_buffer *dmab)
248{
249 int err;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
252 if (err != -ENOMEM)
253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (size <= PAGE_SIZE)
255 return -ENOMEM;
Takashi Iwaidfef01e2018-07-19 11:01:04 +0200256 size >>= 1;
257 size = PAGE_SIZE << get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 if (! dmab->area)
260 return -ENOMEM;
261 return 0;
262}
Takashi Iwai35f80012017-06-16 16:16:33 +0200263EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265
266/**
267 * snd_dma_free_pages - release the allocated buffer
268 * @dmab: the buffer allocation record to release
269 *
270 * Releases the allocated buffer via snd_dma_alloc_pages().
271 */
272void snd_dma_free_pages(struct snd_dma_buffer *dmab)
273{
274 switch (dmab->dev.type) {
275 case SNDRV_DMA_TYPE_CONTINUOUS:
276 snd_free_pages(dmab->area, dmab->bytes);
277 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200278#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200279#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800280 case SNDRV_DMA_TYPE_DEV_IRAM:
281 snd_free_dev_iram(dmab);
282 break;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200283#endif /* CONFIG_GENERIC_ALLOCATOR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 case SNDRV_DMA_TYPE_DEV:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200285 case SNDRV_DMA_TYPE_DEV_UC:
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200286 snd_free_dev_pages(dmab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200288#endif
289#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 case SNDRV_DMA_TYPE_DEV_SG:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200291 case SNDRV_DMA_TYPE_DEV_UC_SG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 snd_free_sgbuf_pages(dmab);
293 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 default:
Takashi Iwaif2f93072014-02-04 18:21:03 +0100296 pr_err("snd-malloc: invalid device type %d\n", dmab->dev.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299EXPORT_SYMBOL(snd_dma_free_pages);