blob: 1b1c7620cbdac292ecada6ad22085acbe81aa784 [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
Takashi Iwai08422d22019-11-05 09:01:35 +0100102static inline gfp_t snd_mem_get_gfp_flags(const struct device *dev)
103{
104 if (!dev)
105 return GFP_KERNEL;
106 else
107 return (__force gfp_t)(unsigned long)dev;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/**
111 * snd_dma_alloc_pages - allocate the buffer area according to the given type
112 * @type: the DMA buffer type
113 * @device: the device pointer
114 * @size: the buffer size to allocate
115 * @dmab: buffer allocation record to store the allocated data
116 *
117 * Calls the memory-allocator function for the corresponding
118 * buffer type.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100119 *
120 * Return: Zero if the buffer with the given size is allocated successfully,
121 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123int snd_dma_alloc_pages(int type, struct device *device, size_t size,
124 struct snd_dma_buffer *dmab)
125{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200126 if (WARN_ON(!size))
127 return -ENXIO;
128 if (WARN_ON(!dmab))
129 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 dmab->dev.type = type;
132 dmab->dev.dev = device;
133 dmab->bytes = 0;
134 switch (type) {
135 case SNDRV_DMA_TYPE_CONTINUOUS:
Takashi Iwai734b5a02018-11-23 19:38:13 +0100136 dmab->area = alloc_pages_exact(size,
Takashi Iwai08422d22019-11-05 09:01:35 +0100137 snd_mem_get_gfp_flags(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 dmab->addr = 0;
139 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200140#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200141#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800142 case SNDRV_DMA_TYPE_DEV_IRAM:
143 snd_malloc_dev_iram(dmab, size);
144 if (dmab->area)
145 break;
146 /* Internal memory might have limited size and no enough space,
147 * so if we fail to malloc, try to fetch memory traditionally.
148 */
149 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200150#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai3c4cfa72018-10-04 19:54:51 +0200151 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 case SNDRV_DMA_TYPE_DEV:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200153 case SNDRV_DMA_TYPE_DEV_UC:
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200154 snd_malloc_dev_pages(dmab, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200156#endif
157#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case SNDRV_DMA_TYPE_DEV_SG:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200159 case SNDRV_DMA_TYPE_DEV_UC_SG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
161 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 default:
Takashi Iwaif2f93072014-02-04 18:21:03 +0100164 pr_err("snd-malloc: invalid device type %d\n", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dmab->area = NULL;
166 dmab->addr = 0;
167 return -ENXIO;
168 }
169 if (! dmab->area)
170 return -ENOMEM;
171 dmab->bytes = size;
172 return 0;
173}
Takashi Iwai35f80012017-06-16 16:16:33 +0200174EXPORT_SYMBOL(snd_dma_alloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/**
177 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
178 * @type: the DMA buffer type
179 * @device: the device pointer
180 * @size: the buffer size to allocate
181 * @dmab: buffer allocation record to store the allocated data
182 *
183 * Calls the memory-allocator function for the corresponding
184 * buffer type. When no space is left, this function reduces the size and
185 * tries to allocate again. The size actually allocated is stored in
186 * res_size argument.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100187 *
188 * Return: Zero if the buffer with the given size is allocated successfully,
189 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
192 struct snd_dma_buffer *dmab)
193{
194 int err;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
197 if (err != -ENOMEM)
198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (size <= PAGE_SIZE)
200 return -ENOMEM;
Takashi Iwaidfef01e2018-07-19 11:01:04 +0200201 size >>= 1;
202 size = PAGE_SIZE << get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 if (! dmab->area)
205 return -ENOMEM;
206 return 0;
207}
Takashi Iwai35f80012017-06-16 16:16:33 +0200208EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210
211/**
212 * snd_dma_free_pages - release the allocated buffer
213 * @dmab: the buffer allocation record to release
214 *
215 * Releases the allocated buffer via snd_dma_alloc_pages().
216 */
217void snd_dma_free_pages(struct snd_dma_buffer *dmab)
218{
219 switch (dmab->dev.type) {
220 case SNDRV_DMA_TYPE_CONTINUOUS:
Takashi Iwai734b5a02018-11-23 19:38:13 +0100221 free_pages_exact(dmab->area, dmab->bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200223#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200224#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800225 case SNDRV_DMA_TYPE_DEV_IRAM:
226 snd_free_dev_iram(dmab);
227 break;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200228#endif /* CONFIG_GENERIC_ALLOCATOR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 case SNDRV_DMA_TYPE_DEV:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200230 case SNDRV_DMA_TYPE_DEV_UC:
Takashi Iwai28f3f4f2018-08-10 14:43:37 +0200231 snd_free_dev_pages(dmab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200233#endif
234#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 case SNDRV_DMA_TYPE_DEV_SG:
Takashi Iwai42e748a2018-08-08 17:01:00 +0200236 case SNDRV_DMA_TYPE_DEV_UC_SG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 snd_free_sgbuf_pages(dmab);
238 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200239#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 default:
Takashi Iwaif2f93072014-02-04 18:21:03 +0100241 pr_err("snd-malloc: invalid device type %d\n", dmab->dev.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244EXPORT_SYMBOL(snd_dma_free_pages);