Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * Generic memory allocators |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/slab.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/dma-mapping.h> |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 12 | #include <linux/genalloc.h> |
Takashi Iwai | 42e748a | 2018-08-08 17:01:00 +0200 | [diff] [blame] | 13 | #ifdef CONFIG_X86 |
| 14 | #include <asm/set_memory.h> |
| 15 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <sound/memalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * Bus-specific memory allocators |
| 21 | * |
| 22 | */ |
| 23 | |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 24 | #ifdef CONFIG_HAS_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | /* allocate the coherent DMA pages */ |
Takashi Iwai | 28f3f4f | 2018-08-10 14:43:37 +0200 | [diff] [blame] | 26 | static void snd_malloc_dev_pages(struct snd_dma_buffer *dmab, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Al Viro | 1ef64e6 | 2005-10-21 03:22:18 -0400 | [diff] [blame] | 28 | gfp_t gfp_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | gfp_flags = GFP_KERNEL |
Hugh Dickins | f3d48f0 | 2005-11-21 21:32:22 -0800 | [diff] [blame] | 31 | | __GFP_COMP /* compound page lets parts be mapped */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | | __GFP_NORETRY /* don't trigger OOM-killer */ |
| 33 | | __GFP_NOWARN; /* no stack trace print - this call is non-critical */ |
Takashi Iwai | 28f3f4f | 2018-08-10 14:43:37 +0200 | [diff] [blame] | 34 | dmab->area = dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, |
| 35 | gfp_flags); |
Takashi Iwai | 42e748a | 2018-08-08 17:01:00 +0200 | [diff] [blame] | 36 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* free the coherent DMA pages */ |
Takashi Iwai | 28f3f4f | 2018-08-10 14:43:37 +0200 | [diff] [blame] | 44 | static void snd_free_dev_pages(struct snd_dma_buffer *dmab) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Takashi Iwai | 42e748a | 2018-08-08 17:01:00 +0200 | [diff] [blame] | 46 | #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 Iwai | 28f3f4f | 2018-08-10 14:43:37 +0200 | [diff] [blame] | 51 | dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 53 | |
Takashi Iwai | 6343731 | 2013-10-28 16:08:27 +0100 | [diff] [blame] | 54 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 55 | /** |
| 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 Iwai | 9f694bc | 2013-10-29 11:56:21 +0100 | [diff] [blame] | 62 | static void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size) |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 63 | { |
| 64 | struct device *dev = dmab->dev.dev; |
| 65 | struct gen_pool *pool = NULL; |
| 66 | |
Takashi Iwai | a40a393 | 2013-10-29 11:59:31 +0100 | [diff] [blame] | 67 | dmab->area = NULL; |
| 68 | dmab->addr = 0; |
| 69 | |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 70 | if (dev->of_node) |
Vladimir Zapolskiy | abdd4a7 | 2015-06-30 15:00:07 -0700 | [diff] [blame] | 71 | pool = of_gen_pool_get(dev->of_node, "iram", 0); |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 72 | |
| 73 | if (!pool) |
| 74 | return; |
| 75 | |
| 76 | /* Assign the pool into private_data field */ |
| 77 | dmab->private_data = pool; |
| 78 | |
Nicolin Chen | 07968fe | 2013-11-14 14:32:15 -0800 | [diff] [blame] | 79 | dmab->area = gen_pool_dma_alloc(pool, size, &dmab->addr); |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 80 | } |
| 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 Iwai | 9f694bc | 2013-10-29 11:56:21 +0100 | [diff] [blame] | 86 | static void snd_free_dev_iram(struct snd_dma_buffer *dmab) |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 87 | { |
| 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 Iwai | 6343731 | 2013-10-28 16:08:27 +0100 | [diff] [blame] | 93 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 94 | #endif /* CONFIG_HAS_DMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* |
| 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 Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 112 | * |
| 113 | * Return: Zero if the buffer with the given size is allocated successfully, |
| 114 | * otherwise a negative value on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | */ |
| 116 | int snd_dma_alloc_pages(int type, struct device *device, size_t size, |
| 117 | struct snd_dma_buffer *dmab) |
| 118 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 119 | if (WARN_ON(!size)) |
| 120 | return -ENXIO; |
| 121 | if (WARN_ON(!dmab)) |
| 122 | return -ENXIO; |
Takashi Iwai | 6ce1d63 | 2019-02-04 14:34:00 +0100 | [diff] [blame] | 123 | if (WARN_ON(!device)) |
| 124 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | dmab->dev.type = type; |
| 127 | dmab->dev.dev = device; |
| 128 | dmab->bytes = 0; |
| 129 | switch (type) { |
| 130 | case SNDRV_DMA_TYPE_CONTINUOUS: |
Takashi Iwai | 734b5a0 | 2018-11-23 19:38:13 +0100 | [diff] [blame] | 131 | dmab->area = alloc_pages_exact(size, |
| 132 | (__force gfp_t)(unsigned long)device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | dmab->addr = 0; |
| 134 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 135 | #ifdef CONFIG_HAS_DMA |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 136 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 137 | 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 Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 145 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
Takashi Iwai | 3c4cfa7 | 2018-10-04 19:54:51 +0200 | [diff] [blame] | 146 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | case SNDRV_DMA_TYPE_DEV: |
Takashi Iwai | 42e748a | 2018-08-08 17:01:00 +0200 | [diff] [blame] | 148 | case SNDRV_DMA_TYPE_DEV_UC: |
Takashi Iwai | 28f3f4f | 2018-08-10 14:43:37 +0200 | [diff] [blame] | 149 | snd_malloc_dev_pages(dmab, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | break; |
Takashi Iwai | cc6a8ac | 2008-06-17 16:39:06 +0200 | [diff] [blame] | 151 | #endif |
| 152 | #ifdef CONFIG_SND_DMA_SGBUF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | case SNDRV_DMA_TYPE_DEV_SG: |
Takashi Iwai | 42e748a | 2018-08-08 17:01:00 +0200 | [diff] [blame] | 154 | case SNDRV_DMA_TYPE_DEV_UC_SG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | snd_malloc_sgbuf_pages(device, size, dmab, NULL); |
| 156 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 157 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | default: |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 159 | pr_err("snd-malloc: invalid device type %d\n", type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | 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 Iwai | 35f8001 | 2017-06-16 16:16:33 +0200 | [diff] [blame] | 169 | EXPORT_SYMBOL(snd_dma_alloc_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 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 Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 182 | * |
| 183 | * Return: Zero if the buffer with the given size is allocated successfully, |
| 184 | * otherwise a negative value on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | */ |
| 186 | int 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) { |
| 192 | if (err != -ENOMEM) |
| 193 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (size <= PAGE_SIZE) |
| 195 | return -ENOMEM; |
Takashi Iwai | dfef01e | 2018-07-19 11:01:04 +0200 | [diff] [blame] | 196 | size >>= 1; |
| 197 | size = PAGE_SIZE << get_order(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | if (! dmab->area) |
| 200 | return -ENOMEM; |
| 201 | return 0; |
| 202 | } |
Takashi Iwai | 35f8001 | 2017-06-16 16:16:33 +0200 | [diff] [blame] | 203 | EXPORT_SYMBOL(snd_dma_alloc_pages_fallback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 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 | */ |
| 212 | void snd_dma_free_pages(struct snd_dma_buffer *dmab) |
| 213 | { |
| 214 | switch (dmab->dev.type) { |
| 215 | case SNDRV_DMA_TYPE_CONTINUOUS: |
Takashi Iwai | 734b5a0 | 2018-11-23 19:38:13 +0100 | [diff] [blame] | 216 | free_pages_exact(dmab->area, dmab->bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 218 | #ifdef CONFIG_HAS_DMA |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 219 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 220 | case SNDRV_DMA_TYPE_DEV_IRAM: |
| 221 | snd_free_dev_iram(dmab); |
| 222 | break; |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 223 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | case SNDRV_DMA_TYPE_DEV: |
Takashi Iwai | 42e748a | 2018-08-08 17:01:00 +0200 | [diff] [blame] | 225 | case SNDRV_DMA_TYPE_DEV_UC: |
Takashi Iwai | 28f3f4f | 2018-08-10 14:43:37 +0200 | [diff] [blame] | 226 | snd_free_dev_pages(dmab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | break; |
Takashi Iwai | cc6a8ac | 2008-06-17 16:39:06 +0200 | [diff] [blame] | 228 | #endif |
| 229 | #ifdef CONFIG_SND_DMA_SGBUF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | case SNDRV_DMA_TYPE_DEV_SG: |
Takashi Iwai | 42e748a | 2018-08-08 17:01:00 +0200 | [diff] [blame] | 231 | case SNDRV_DMA_TYPE_DEV_UC_SG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | snd_free_sgbuf_pages(dmab); |
| 233 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 234 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | default: |
Takashi Iwai | f2f9307 | 2014-02-04 18:21:03 +0100 | [diff] [blame] | 236 | pr_err("snd-malloc: invalid device type %d\n", dmab->dev.type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | EXPORT_SYMBOL(snd_dma_free_pages); |