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