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