Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 2 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/proc_fs.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/pci.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/mm.h> |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 30 | #include <linux/seq_file.h> |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 31 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/dma-mapping.h> |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 33 | #include <linux/genalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/moduleparam.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 35 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <sound/memalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 39 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | MODULE_DESCRIPTION("Memory allocator for ALSA system."); |
| 41 | MODULE_LICENSE("GPL"); |
| 42 | |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | */ |
| 46 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 47 | static DEFINE_MUTEX(list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static LIST_HEAD(mem_list_head); |
| 49 | |
| 50 | /* buffer preservation list */ |
| 51 | struct snd_mem_list { |
| 52 | struct snd_dma_buffer buffer; |
| 53 | unsigned int id; |
| 54 | struct list_head list; |
| 55 | }; |
| 56 | |
| 57 | /* id for pre-allocated buffers */ |
| 58 | #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1 |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * |
| 62 | * Generic memory allocators |
| 63 | * |
| 64 | */ |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /** |
| 67 | * snd_malloc_pages - allocate pages with the given size |
| 68 | * @size: the size to allocate in bytes |
| 69 | * @gfp_flags: the allocation conditions, GFP_XXX |
| 70 | * |
| 71 | * Allocates the physically contiguous pages with the given size. |
| 72 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 73 | * Return: The pointer of the buffer, or %NULL if no enough memory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
Al Viro | 1ef64e6 | 2005-10-21 03:22:18 -0400 | [diff] [blame] | 75 | void *snd_malloc_pages(size_t size, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | int pg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 79 | if (WARN_ON(!size)) |
| 80 | return NULL; |
| 81 | if (WARN_ON(!gfp_flags)) |
| 82 | return NULL; |
Hugh Dickins | f3d48f0 | 2005-11-21 21:32:22 -0800 | [diff] [blame] | 83 | gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | pg = get_order(size); |
Takashi Iwai | d7b1354 | 2014-01-08 16:09:31 +0100 | [diff] [blame^] | 85 | return (void *) __get_free_pages(gfp_flags, pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * snd_free_pages - release the pages |
| 90 | * @ptr: the buffer pointer to release |
| 91 | * @size: the allocated buffer size |
| 92 | * |
| 93 | * Releases the buffer allocated via snd_malloc_pages(). |
| 94 | */ |
| 95 | void snd_free_pages(void *ptr, size_t size) |
| 96 | { |
| 97 | int pg; |
| 98 | |
| 99 | if (ptr == NULL) |
| 100 | return; |
| 101 | pg = get_order(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | free_pages((unsigned long) ptr, pg); |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * |
| 107 | * Bus-specific memory allocators |
| 108 | * |
| 109 | */ |
| 110 | |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 111 | #ifdef CONFIG_HAS_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* allocate the coherent DMA pages */ |
| 113 | static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma) |
| 114 | { |
| 115 | int pg; |
Al Viro | 1ef64e6 | 2005-10-21 03:22:18 -0400 | [diff] [blame] | 116 | gfp_t gfp_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 118 | if (WARN_ON(!dma)) |
| 119 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | pg = get_order(size); |
| 121 | gfp_flags = GFP_KERNEL |
Hugh Dickins | f3d48f0 | 2005-11-21 21:32:22 -0800 | [diff] [blame] | 122 | | __GFP_COMP /* compound page lets parts be mapped */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | | __GFP_NORETRY /* don't trigger OOM-killer */ |
| 124 | | __GFP_NOWARN; /* no stack trace print - this call is non-critical */ |
Takashi Iwai | d7b1354 | 2014-01-08 16:09:31 +0100 | [diff] [blame^] | 125 | return dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* free the coherent DMA pages */ |
| 129 | static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr, |
| 130 | dma_addr_t dma) |
| 131 | { |
| 132 | int pg; |
| 133 | |
| 134 | if (ptr == NULL) |
| 135 | return; |
| 136 | pg = get_order(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma); |
| 138 | } |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 139 | |
Takashi Iwai | 6343731 | 2013-10-28 16:08:27 +0100 | [diff] [blame] | 140 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 141 | /** |
| 142 | * snd_malloc_dev_iram - allocate memory from on-chip internal ram |
| 143 | * @dmab: buffer allocation record to store the allocated data |
| 144 | * @size: number of bytes to allocate from the iram |
| 145 | * |
| 146 | * This function requires iram phandle provided via of_node |
| 147 | */ |
Takashi Iwai | 9f694bc | 2013-10-29 11:56:21 +0100 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | struct device *dev = dmab->dev.dev; |
| 151 | struct gen_pool *pool = NULL; |
| 152 | |
Takashi Iwai | a40a393 | 2013-10-29 11:59:31 +0100 | [diff] [blame] | 153 | dmab->area = NULL; |
| 154 | dmab->addr = 0; |
| 155 | |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 156 | if (dev->of_node) |
| 157 | pool = of_get_named_gen_pool(dev->of_node, "iram", 0); |
| 158 | |
| 159 | if (!pool) |
| 160 | return; |
| 161 | |
| 162 | /* Assign the pool into private_data field */ |
| 163 | dmab->private_data = pool; |
| 164 | |
Nicolin Chen | 07968fe | 2013-11-14 14:32:15 -0800 | [diff] [blame] | 165 | dmab->area = gen_pool_dma_alloc(pool, size, &dmab->addr); |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * snd_free_dev_iram - free allocated specific memory from on-chip internal ram |
| 170 | * @dmab: buffer allocation record to store the allocated data |
| 171 | */ |
Takashi Iwai | 9f694bc | 2013-10-29 11:56:21 +0100 | [diff] [blame] | 172 | static void snd_free_dev_iram(struct snd_dma_buffer *dmab) |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 173 | { |
| 174 | struct gen_pool *pool = dmab->private_data; |
| 175 | |
| 176 | if (pool && dmab->area) |
| 177 | gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes); |
| 178 | } |
Takashi Iwai | 6343731 | 2013-10-28 16:08:27 +0100 | [diff] [blame] | 179 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 180 | #endif /* CONFIG_HAS_DMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* |
| 183 | * |
| 184 | * ALSA generic memory management |
| 185 | * |
| 186 | */ |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | * snd_dma_alloc_pages - allocate the buffer area according to the given type |
| 191 | * @type: the DMA buffer type |
| 192 | * @device: the device pointer |
| 193 | * @size: the buffer size to allocate |
| 194 | * @dmab: buffer allocation record to store the allocated data |
| 195 | * |
| 196 | * Calls the memory-allocator function for the corresponding |
| 197 | * buffer type. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 198 | * |
| 199 | * Return: Zero if the buffer with the given size is allocated successfully, |
| 200 | * otherwise a negative value on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | */ |
| 202 | int snd_dma_alloc_pages(int type, struct device *device, size_t size, |
| 203 | struct snd_dma_buffer *dmab) |
| 204 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 205 | if (WARN_ON(!size)) |
| 206 | return -ENXIO; |
| 207 | if (WARN_ON(!dmab)) |
| 208 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | dmab->dev.type = type; |
| 211 | dmab->dev.dev = device; |
| 212 | dmab->bytes = 0; |
| 213 | switch (type) { |
| 214 | case SNDRV_DMA_TYPE_CONTINUOUS: |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 215 | dmab->area = snd_malloc_pages(size, |
| 216 | (__force gfp_t)(unsigned long)device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | dmab->addr = 0; |
| 218 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 219 | #ifdef CONFIG_HAS_DMA |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 220 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 221 | case SNDRV_DMA_TYPE_DEV_IRAM: |
| 222 | snd_malloc_dev_iram(dmab, size); |
| 223 | if (dmab->area) |
| 224 | break; |
| 225 | /* Internal memory might have limited size and no enough space, |
| 226 | * so if we fail to malloc, try to fetch memory traditionally. |
| 227 | */ |
| 228 | dmab->dev.type = SNDRV_DMA_TYPE_DEV; |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 229 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | case SNDRV_DMA_TYPE_DEV: |
| 231 | dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr); |
| 232 | break; |
Takashi Iwai | cc6a8ac | 2008-06-17 16:39:06 +0200 | [diff] [blame] | 233 | #endif |
| 234 | #ifdef CONFIG_SND_DMA_SGBUF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | case SNDRV_DMA_TYPE_DEV_SG: |
| 236 | snd_malloc_sgbuf_pages(device, size, dmab, NULL); |
| 237 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 238 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | default: |
| 240 | printk(KERN_ERR "snd-malloc: invalid device type %d\n", type); |
| 241 | dmab->area = NULL; |
| 242 | dmab->addr = 0; |
| 243 | return -ENXIO; |
| 244 | } |
| 245 | if (! dmab->area) |
| 246 | return -ENOMEM; |
| 247 | dmab->bytes = size; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback |
| 253 | * @type: the DMA buffer type |
| 254 | * @device: the device pointer |
| 255 | * @size: the buffer size to allocate |
| 256 | * @dmab: buffer allocation record to store the allocated data |
| 257 | * |
| 258 | * Calls the memory-allocator function for the corresponding |
| 259 | * buffer type. When no space is left, this function reduces the size and |
| 260 | * tries to allocate again. The size actually allocated is stored in |
| 261 | * res_size argument. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 262 | * |
| 263 | * Return: Zero if the buffer with the given size is allocated successfully, |
| 264 | * otherwise a negative value on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | */ |
| 266 | int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size, |
| 267 | struct snd_dma_buffer *dmab) |
| 268 | { |
| 269 | int err; |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) { |
Takashi Iwai | 4e184f8 | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 272 | size_t aligned_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (err != -ENOMEM) |
| 274 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (size <= PAGE_SIZE) |
| 276 | return -ENOMEM; |
Takashi Iwai | 4e184f8 | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 277 | aligned_size = PAGE_SIZE << get_order(size); |
| 278 | if (size != aligned_size) |
| 279 | size = aligned_size; |
| 280 | else |
| 281 | size >>= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | if (! dmab->area) |
| 284 | return -ENOMEM; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * snd_dma_free_pages - release the allocated buffer |
| 291 | * @dmab: the buffer allocation record to release |
| 292 | * |
| 293 | * Releases the allocated buffer via snd_dma_alloc_pages(). |
| 294 | */ |
| 295 | void snd_dma_free_pages(struct snd_dma_buffer *dmab) |
| 296 | { |
| 297 | switch (dmab->dev.type) { |
| 298 | case SNDRV_DMA_TYPE_CONTINUOUS: |
| 299 | snd_free_pages(dmab->area, dmab->bytes); |
| 300 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 301 | #ifdef CONFIG_HAS_DMA |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 302 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 303 | case SNDRV_DMA_TYPE_DEV_IRAM: |
| 304 | snd_free_dev_iram(dmab); |
| 305 | break; |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 306 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | case SNDRV_DMA_TYPE_DEV: |
| 308 | snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr); |
| 309 | break; |
Takashi Iwai | cc6a8ac | 2008-06-17 16:39:06 +0200 | [diff] [blame] | 310 | #endif |
| 311 | #ifdef CONFIG_SND_DMA_SGBUF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | case SNDRV_DMA_TYPE_DEV_SG: |
| 313 | snd_free_sgbuf_pages(dmab); |
| 314 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 315 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | default: |
| 317 | printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * snd_dma_get_reserved - get the reserved buffer for the given device |
| 324 | * @dmab: the buffer allocation record to store |
| 325 | * @id: the buffer id |
| 326 | * |
| 327 | * Looks for the reserved-buffer list and re-uses if the same buffer |
| 328 | * is found in the list. When the buffer is found, it's removed from the free list. |
| 329 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 330 | * Return: The size of buffer if the buffer is found, or zero if not found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | */ |
| 332 | size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id) |
| 333 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | struct snd_mem_list *mem; |
| 335 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 336 | if (WARN_ON(!dmab)) |
| 337 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 339 | mutex_lock(&list_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 340 | list_for_each_entry(mem, &mem_list_head, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (mem->id == id && |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 342 | (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL || |
| 343 | ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) { |
| 344 | struct device *dev = dmab->dev.dev; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 345 | list_del(&mem->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | *dmab = mem->buffer; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 347 | if (dmab->dev.dev == NULL) |
| 348 | dmab->dev.dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | kfree(mem); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 350 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | return dmab->bytes; |
| 352 | } |
| 353 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 354 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * snd_dma_reserve_buf - reserve the buffer |
| 360 | * @dmab: the buffer to reserve |
| 361 | * @id: the buffer id |
| 362 | * |
| 363 | * Reserves the given buffer as a reserved buffer. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 364 | * |
| 365 | * Return: Zero if successful, or a negative code on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | */ |
| 367 | int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id) |
| 368 | { |
| 369 | struct snd_mem_list *mem; |
| 370 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 371 | if (WARN_ON(!dmab)) |
| 372 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | mem = kmalloc(sizeof(*mem), GFP_KERNEL); |
| 374 | if (! mem) |
| 375 | return -ENOMEM; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 376 | mutex_lock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | mem->buffer = *dmab; |
| 378 | mem->id = id; |
| 379 | list_add_tail(&mem->list, &mem_list_head); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 380 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * purge all reserved buffers |
| 386 | */ |
| 387 | static void free_all_reserved_pages(void) |
| 388 | { |
| 389 | struct list_head *p; |
| 390 | struct snd_mem_list *mem; |
| 391 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 392 | mutex_lock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | while (! list_empty(&mem_list_head)) { |
| 394 | p = mem_list_head.next; |
| 395 | mem = list_entry(p, struct snd_mem_list, list); |
| 396 | list_del(p); |
| 397 | snd_dma_free_pages(&mem->buffer); |
| 398 | kfree(mem); |
| 399 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 400 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | #ifdef CONFIG_PROC_FS |
| 405 | /* |
| 406 | * proc file interface |
| 407 | */ |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 408 | #define SND_MEM_PROC_FILE "driver/snd-page-alloc" |
Clemens Ladisch | a53fc18 | 2005-08-11 15:59:17 +0200 | [diff] [blame] | 409 | static struct proc_dir_entry *snd_mem_proc; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 410 | |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 411 | static int snd_mem_proc_read(struct seq_file *seq, void *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | struct snd_mem_list *mem; |
| 414 | int devno; |
David S. Miller | 759ee81 | 2008-08-27 00:33:26 -0700 | [diff] [blame] | 415 | static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 417 | mutex_lock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | devno = 0; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 419 | list_for_each_entry(mem, &mem_list_head, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | devno++; |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 421 | seq_printf(seq, "buffer %d : ID %08x : type %s\n", |
| 422 | devno, mem->id, types[mem->buffer.dev.type]); |
| 423 | seq_printf(seq, " addr = 0x%lx, size = %d bytes\n", |
| 424 | (unsigned long)mem->buffer.addr, |
| 425 | (int)mem->buffer.bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 427 | mutex_unlock(&list_mutex); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int snd_mem_proc_open(struct inode *inode, struct file *file) |
| 432 | { |
| 433 | return single_open(file, snd_mem_proc_read, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 435 | |
| 436 | /* FIXME: for pci only - other bus? */ |
| 437 | #ifdef CONFIG_PCI |
| 438 | #define gettoken(bufp) strsep(bufp, " \t\n") |
| 439 | |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 440 | static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer, |
| 441 | size_t count, loff_t * ppos) |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 442 | { |
| 443 | char buf[128]; |
| 444 | char *token, *p; |
| 445 | |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 446 | if (count > sizeof(buf) - 1) |
| 447 | return -EINVAL; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 448 | if (copy_from_user(buf, buffer, count)) |
| 449 | return -EFAULT; |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 450 | buf[count] = '\0'; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 451 | |
| 452 | p = buf; |
| 453 | token = gettoken(&p); |
| 454 | if (! token || *token == '#') |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 455 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 456 | if (strcmp(token, "add") == 0) { |
| 457 | char *endp; |
| 458 | int vendor, device, size, buffers; |
| 459 | long mask; |
| 460 | int i, alloced; |
| 461 | struct pci_dev *pci; |
| 462 | |
| 463 | if ((token = gettoken(&p)) == NULL || |
| 464 | (vendor = simple_strtol(token, NULL, 0)) <= 0 || |
| 465 | (token = gettoken(&p)) == NULL || |
| 466 | (device = simple_strtol(token, NULL, 0)) <= 0 || |
| 467 | (token = gettoken(&p)) == NULL || |
| 468 | (mask = simple_strtol(token, NULL, 0)) < 0 || |
| 469 | (token = gettoken(&p)) == NULL || |
| 470 | (size = memparse(token, &endp)) < 64*1024 || |
| 471 | size > 16*1024*1024 /* too big */ || |
| 472 | (token = gettoken(&p)) == NULL || |
| 473 | (buffers = simple_strtol(token, NULL, 0)) <= 0 || |
| 474 | buffers > 4) { |
| 475 | printk(KERN_ERR "snd-page-alloc: invalid proc write format\n"); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 476 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 477 | } |
| 478 | vendor &= 0xffff; |
| 479 | device &= 0xffff; |
| 480 | |
| 481 | alloced = 0; |
| 482 | pci = NULL; |
Jiri Slaby | 0dd119f | 2005-09-07 14:28:33 +0200 | [diff] [blame] | 483 | while ((pci = pci_get_device(vendor, device, pci)) != NULL) { |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 484 | if (mask > 0 && mask < 0xffffffff) { |
| 485 | if (pci_set_dma_mask(pci, mask) < 0 || |
| 486 | pci_set_consistent_dma_mask(pci, mask) < 0) { |
| 487 | printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device); |
Julia Lawall | df1deb6 | 2007-11-28 11:58:56 +0100 | [diff] [blame] | 488 | pci_dev_put(pci); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 489 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | for (i = 0; i < buffers; i++) { |
| 493 | struct snd_dma_buffer dmab; |
| 494 | memset(&dmab, 0, sizeof(dmab)); |
| 495 | if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), |
| 496 | size, &dmab) < 0) { |
| 497 | printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size); |
Jiri Slaby | 0dd119f | 2005-09-07 14:28:33 +0200 | [diff] [blame] | 498 | pci_dev_put(pci); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 499 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 500 | } |
| 501 | snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci)); |
| 502 | } |
| 503 | alloced++; |
| 504 | } |
| 505 | if (! alloced) { |
| 506 | for (i = 0; i < buffers; i++) { |
| 507 | struct snd_dma_buffer dmab; |
| 508 | memset(&dmab, 0, sizeof(dmab)); |
| 509 | /* FIXME: We can allocate only in ZONE_DMA |
| 510 | * without a device pointer! |
| 511 | */ |
| 512 | if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL, |
| 513 | size, &dmab) < 0) { |
| 514 | printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size); |
| 515 | break; |
| 516 | } |
| 517 | snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device)); |
| 518 | } |
| 519 | } |
| 520 | } else if (strcmp(token, "erase") == 0) |
| 521 | /* FIXME: need for releasing each buffer chunk? */ |
| 522 | free_all_reserved_pages(); |
| 523 | else |
| 524 | printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n"); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 525 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 526 | } |
| 527 | #endif /* CONFIG_PCI */ |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 528 | |
| 529 | static const struct file_operations snd_mem_proc_fops = { |
| 530 | .owner = THIS_MODULE, |
| 531 | .open = snd_mem_proc_open, |
| 532 | .read = seq_read, |
| 533 | #ifdef CONFIG_PCI |
| 534 | .write = snd_mem_proc_write, |
| 535 | #endif |
| 536 | .llseek = seq_lseek, |
| 537 | .release = single_release, |
| 538 | }; |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | #endif /* CONFIG_PROC_FS */ |
| 541 | |
| 542 | /* |
| 543 | * module entry |
| 544 | */ |
| 545 | |
| 546 | static int __init snd_mem_init(void) |
| 547 | { |
| 548 | #ifdef CONFIG_PROC_FS |
Denis V. Lunev | 7bf4e6d | 2008-04-29 01:02:13 -0700 | [diff] [blame] | 549 | snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL, |
| 550 | &snd_mem_proc_fops); |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 551 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static void __exit snd_mem_exit(void) |
| 556 | { |
Takashi Iwai | e0be4d3 | 2005-08-23 11:11:03 +0200 | [diff] [blame] | 557 | remove_proc_entry(SND_MEM_PROC_FILE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | free_all_reserved_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
| 562 | module_init(snd_mem_init) |
| 563 | module_exit(snd_mem_exit) |
| 564 | |
| 565 | |
| 566 | /* |
| 567 | * exports |
| 568 | */ |
| 569 | EXPORT_SYMBOL(snd_dma_alloc_pages); |
| 570 | EXPORT_SYMBOL(snd_dma_alloc_pages_fallback); |
| 571 | EXPORT_SYMBOL(snd_dma_free_pages); |
| 572 | |
| 573 | EXPORT_SYMBOL(snd_dma_get_reserved_buf); |
| 574 | EXPORT_SYMBOL(snd_dma_reserve_buf); |
| 575 | |
| 576 | EXPORT_SYMBOL(snd_malloc_pages); |
| 577 | EXPORT_SYMBOL(snd_free_pages); |