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> |
| 33 | #include <linux/moduleparam.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <sound/memalloc.h> |
| 36 | #ifdef CONFIG_SBUS |
| 37 | #include <asm/sbus.h> |
| 38 | #endif |
| 39 | |
| 40 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 41 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | MODULE_DESCRIPTION("Memory allocator for ALSA system."); |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
| 47 | */ |
| 48 | |
| 49 | void *snd_malloc_sgbuf_pages(struct device *device, |
| 50 | size_t size, struct snd_dma_buffer *dmab, |
| 51 | size_t *res_size); |
| 52 | int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab); |
| 53 | |
| 54 | /* |
| 55 | */ |
| 56 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 57 | static DEFINE_MUTEX(list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static LIST_HEAD(mem_list_head); |
| 59 | |
| 60 | /* buffer preservation list */ |
| 61 | struct snd_mem_list { |
| 62 | struct snd_dma_buffer buffer; |
| 63 | unsigned int id; |
| 64 | struct list_head list; |
| 65 | }; |
| 66 | |
| 67 | /* id for pre-allocated buffers */ |
| 68 | #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1 |
| 69 | |
| 70 | #ifdef CONFIG_SND_DEBUG |
| 71 | #define __ASTRING__(x) #x |
| 72 | #define snd_assert(expr, args...) do {\ |
| 73 | if (!(expr)) {\ |
| 74 | printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\ |
| 75 | args;\ |
| 76 | }\ |
| 77 | } while (0) |
| 78 | #else |
| 79 | #define snd_assert(expr, args...) /**/ |
| 80 | #endif |
| 81 | |
| 82 | /* |
| 83 | * Hacks |
| 84 | */ |
| 85 | |
Takashi Iwai | ea50888 | 2006-01-23 15:49:18 +0100 | [diff] [blame] | 86 | #if defined(__i386__) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
| 88 | * A hack to allocate large buffers via dma_alloc_coherent() |
| 89 | * |
| 90 | * since dma_alloc_coherent always tries GFP_DMA when the requested |
| 91 | * pci memory region is below 32bit, it happens quite often that even |
| 92 | * 2 order of pages cannot be allocated. |
| 93 | * |
| 94 | * so in the following, we allocate at first without dma_mask, so that |
| 95 | * allocation will be done without GFP_DMA. if the area doesn't match |
| 96 | * with the requested region, then realloate with the original dma_mask |
| 97 | * again. |
| 98 | * |
| 99 | * Really, we want to move this type of thing into dma_alloc_coherent() |
| 100 | * so dma_mask doesn't have to be messed with. |
| 101 | */ |
| 102 | |
| 103 | static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size, |
Victor Fusco | 5a0f217 | 2005-07-26 13:42:31 +0200 | [diff] [blame] | 104 | dma_addr_t *dma_handle, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 105 | gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | void *ret; |
| 108 | u64 dma_mask, coherent_dma_mask; |
| 109 | |
| 110 | if (dev == NULL || !dev->dma_mask) |
| 111 | return dma_alloc_coherent(dev, size, dma_handle, flags); |
| 112 | dma_mask = *dev->dma_mask; |
| 113 | coherent_dma_mask = dev->coherent_dma_mask; |
| 114 | *dev->dma_mask = 0xffffffff; /* do without masking */ |
| 115 | dev->coherent_dma_mask = 0xffffffff; /* do without masking */ |
| 116 | ret = dma_alloc_coherent(dev, size, dma_handle, flags); |
| 117 | *dev->dma_mask = dma_mask; /* restore */ |
| 118 | dev->coherent_dma_mask = coherent_dma_mask; /* restore */ |
| 119 | if (ret) { |
| 120 | /* obtained address is out of range? */ |
| 121 | if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) { |
| 122 | /* reallocate with the proper mask */ |
| 123 | dma_free_coherent(dev, size, ret, *dma_handle); |
| 124 | ret = dma_alloc_coherent(dev, size, dma_handle, flags); |
| 125 | } |
| 126 | } else { |
| 127 | /* wish to success now with the proper mask... */ |
| 128 | if (dma_mask != 0xffffffffUL) { |
| 129 | /* allocation with GFP_ATOMIC to avoid the long stall */ |
| 130 | flags &= ~GFP_KERNEL; |
| 131 | flags |= GFP_ATOMIC; |
| 132 | ret = dma_alloc_coherent(dev, size, dma_handle, flags); |
| 133 | } |
| 134 | } |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* redefine dma_alloc_coherent for some architectures */ |
| 139 | #undef dma_alloc_coherent |
| 140 | #define dma_alloc_coherent snd_dma_hack_alloc_coherent |
| 141 | |
| 142 | #endif /* arch */ |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | /* |
| 145 | * |
| 146 | * Generic memory allocators |
| 147 | * |
| 148 | */ |
| 149 | |
| 150 | static long snd_allocated_pages; /* holding the number of allocated pages */ |
| 151 | |
| 152 | static inline void inc_snd_pages(int order) |
| 153 | { |
| 154 | snd_allocated_pages += 1 << order; |
| 155 | } |
| 156 | |
| 157 | static inline void dec_snd_pages(int order) |
| 158 | { |
| 159 | snd_allocated_pages -= 1 << order; |
| 160 | } |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /** |
| 163 | * snd_malloc_pages - allocate pages with the given size |
| 164 | * @size: the size to allocate in bytes |
| 165 | * @gfp_flags: the allocation conditions, GFP_XXX |
| 166 | * |
| 167 | * Allocates the physically contiguous pages with the given size. |
| 168 | * |
| 169 | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 170 | */ |
Al Viro | 1ef64e6 | 2005-10-21 03:22:18 -0400 | [diff] [blame] | 171 | void *snd_malloc_pages(size_t size, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | int pg; |
| 174 | void *res; |
| 175 | |
| 176 | snd_assert(size > 0, return NULL); |
| 177 | snd_assert(gfp_flags != 0, return NULL); |
Hugh Dickins | f3d48f0 | 2005-11-21 21:32:22 -0800 | [diff] [blame] | 178 | gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | pg = get_order(size); |
Takashi Iwai | 2ba8c15 | 2006-01-31 14:44:28 +0100 | [diff] [blame] | 180 | if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | inc_snd_pages(pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return res; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * snd_free_pages - release the pages |
| 187 | * @ptr: the buffer pointer to release |
| 188 | * @size: the allocated buffer size |
| 189 | * |
| 190 | * Releases the buffer allocated via snd_malloc_pages(). |
| 191 | */ |
| 192 | void snd_free_pages(void *ptr, size_t size) |
| 193 | { |
| 194 | int pg; |
| 195 | |
| 196 | if (ptr == NULL) |
| 197 | return; |
| 198 | pg = get_order(size); |
| 199 | dec_snd_pages(pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | free_pages((unsigned long) ptr, pg); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * |
| 205 | * Bus-specific memory allocators |
| 206 | * |
| 207 | */ |
| 208 | |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 209 | #ifdef CONFIG_HAS_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* allocate the coherent DMA pages */ |
| 211 | static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma) |
| 212 | { |
| 213 | int pg; |
| 214 | void *res; |
Al Viro | 1ef64e6 | 2005-10-21 03:22:18 -0400 | [diff] [blame] | 215 | gfp_t gfp_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | snd_assert(size > 0, return NULL); |
| 218 | snd_assert(dma != NULL, return NULL); |
| 219 | pg = get_order(size); |
| 220 | gfp_flags = GFP_KERNEL |
Hugh Dickins | f3d48f0 | 2005-11-21 21:32:22 -0800 | [diff] [blame] | 221 | | __GFP_COMP /* compound page lets parts be mapped */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | | __GFP_NORETRY /* don't trigger OOM-killer */ |
| 223 | | __GFP_NOWARN; /* no stack trace print - this call is non-critical */ |
| 224 | res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags); |
Takashi Iwai | 2ba8c15 | 2006-01-31 14:44:28 +0100 | [diff] [blame] | 225 | if (res != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | inc_snd_pages(pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | return res; |
| 229 | } |
| 230 | |
| 231 | /* free the coherent DMA pages */ |
| 232 | static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr, |
| 233 | dma_addr_t dma) |
| 234 | { |
| 235 | int pg; |
| 236 | |
| 237 | if (ptr == NULL) |
| 238 | return; |
| 239 | pg = get_order(size); |
| 240 | dec_snd_pages(pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma); |
| 242 | } |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 243 | #endif /* CONFIG_HAS_DMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | #ifdef CONFIG_SBUS |
| 246 | |
| 247 | static void *snd_malloc_sbus_pages(struct device *dev, size_t size, |
| 248 | dma_addr_t *dma_addr) |
| 249 | { |
| 250 | struct sbus_dev *sdev = (struct sbus_dev *)dev; |
| 251 | int pg; |
| 252 | void *res; |
| 253 | |
| 254 | snd_assert(size > 0, return NULL); |
| 255 | snd_assert(dma_addr != NULL, return NULL); |
| 256 | pg = get_order(size); |
| 257 | res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr); |
| 258 | if (res != NULL) |
| 259 | inc_snd_pages(pg); |
| 260 | return res; |
| 261 | } |
| 262 | |
| 263 | static void snd_free_sbus_pages(struct device *dev, size_t size, |
| 264 | void *ptr, dma_addr_t dma_addr) |
| 265 | { |
| 266 | struct sbus_dev *sdev = (struct sbus_dev *)dev; |
| 267 | int pg; |
| 268 | |
| 269 | if (ptr == NULL) |
| 270 | return; |
| 271 | pg = get_order(size); |
| 272 | dec_snd_pages(pg); |
| 273 | sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr); |
| 274 | } |
| 275 | |
| 276 | #endif /* CONFIG_SBUS */ |
| 277 | |
| 278 | /* |
| 279 | * |
| 280 | * ALSA generic memory management |
| 281 | * |
| 282 | */ |
| 283 | |
| 284 | |
| 285 | /** |
| 286 | * snd_dma_alloc_pages - allocate the buffer area according to the given type |
| 287 | * @type: the DMA buffer type |
| 288 | * @device: the device pointer |
| 289 | * @size: the buffer size to allocate |
| 290 | * @dmab: buffer allocation record to store the allocated data |
| 291 | * |
| 292 | * Calls the memory-allocator function for the corresponding |
| 293 | * buffer type. |
| 294 | * |
| 295 | * Returns zero if the buffer with the given size is allocated successfuly, |
| 296 | * other a negative value at error. |
| 297 | */ |
| 298 | int snd_dma_alloc_pages(int type, struct device *device, size_t size, |
| 299 | struct snd_dma_buffer *dmab) |
| 300 | { |
| 301 | snd_assert(size > 0, return -ENXIO); |
| 302 | snd_assert(dmab != NULL, return -ENXIO); |
| 303 | |
| 304 | dmab->dev.type = type; |
| 305 | dmab->dev.dev = device; |
| 306 | dmab->bytes = 0; |
| 307 | switch (type) { |
| 308 | case SNDRV_DMA_TYPE_CONTINUOUS: |
| 309 | dmab->area = snd_malloc_pages(size, (unsigned long)device); |
| 310 | dmab->addr = 0; |
| 311 | break; |
| 312 | #ifdef CONFIG_SBUS |
| 313 | case SNDRV_DMA_TYPE_SBUS: |
| 314 | dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr); |
| 315 | break; |
| 316 | #endif |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 317 | #ifdef CONFIG_HAS_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | case SNDRV_DMA_TYPE_DEV: |
| 319 | dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr); |
| 320 | break; |
| 321 | case SNDRV_DMA_TYPE_DEV_SG: |
| 322 | snd_malloc_sgbuf_pages(device, size, dmab, NULL); |
| 323 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 324 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | default: |
| 326 | printk(KERN_ERR "snd-malloc: invalid device type %d\n", type); |
| 327 | dmab->area = NULL; |
| 328 | dmab->addr = 0; |
| 329 | return -ENXIO; |
| 330 | } |
| 331 | if (! dmab->area) |
| 332 | return -ENOMEM; |
| 333 | dmab->bytes = size; |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback |
| 339 | * @type: the DMA buffer type |
| 340 | * @device: the device pointer |
| 341 | * @size: the buffer size to allocate |
| 342 | * @dmab: buffer allocation record to store the allocated data |
| 343 | * |
| 344 | * Calls the memory-allocator function for the corresponding |
| 345 | * buffer type. When no space is left, this function reduces the size and |
| 346 | * tries to allocate again. The size actually allocated is stored in |
| 347 | * res_size argument. |
| 348 | * |
| 349 | * Returns zero if the buffer with the given size is allocated successfuly, |
| 350 | * other a negative value at error. |
| 351 | */ |
| 352 | int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size, |
| 353 | struct snd_dma_buffer *dmab) |
| 354 | { |
| 355 | int err; |
| 356 | |
| 357 | snd_assert(size > 0, return -ENXIO); |
| 358 | snd_assert(dmab != NULL, return -ENXIO); |
| 359 | |
| 360 | while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) { |
| 361 | if (err != -ENOMEM) |
| 362 | return err; |
| 363 | size >>= 1; |
| 364 | if (size <= PAGE_SIZE) |
| 365 | return -ENOMEM; |
| 366 | } |
| 367 | if (! dmab->area) |
| 368 | return -ENOMEM; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * snd_dma_free_pages - release the allocated buffer |
| 375 | * @dmab: the buffer allocation record to release |
| 376 | * |
| 377 | * Releases the allocated buffer via snd_dma_alloc_pages(). |
| 378 | */ |
| 379 | void snd_dma_free_pages(struct snd_dma_buffer *dmab) |
| 380 | { |
| 381 | switch (dmab->dev.type) { |
| 382 | case SNDRV_DMA_TYPE_CONTINUOUS: |
| 383 | snd_free_pages(dmab->area, dmab->bytes); |
| 384 | break; |
| 385 | #ifdef CONFIG_SBUS |
| 386 | case SNDRV_DMA_TYPE_SBUS: |
| 387 | snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr); |
| 388 | break; |
| 389 | #endif |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 390 | #ifdef CONFIG_HAS_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | case SNDRV_DMA_TYPE_DEV: |
| 392 | snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr); |
| 393 | break; |
| 394 | case SNDRV_DMA_TYPE_DEV_SG: |
| 395 | snd_free_sgbuf_pages(dmab); |
| 396 | break; |
Takashi Iwai | 8f11551 | 2007-07-26 18:59:36 +0200 | [diff] [blame] | 397 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | default: |
| 399 | printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /** |
| 405 | * snd_dma_get_reserved - get the reserved buffer for the given device |
| 406 | * @dmab: the buffer allocation record to store |
| 407 | * @id: the buffer id |
| 408 | * |
| 409 | * Looks for the reserved-buffer list and re-uses if the same buffer |
| 410 | * is found in the list. When the buffer is found, it's removed from the free list. |
| 411 | * |
| 412 | * Returns the size of buffer if the buffer is found, or zero if not found. |
| 413 | */ |
| 414 | size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id) |
| 415 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | struct snd_mem_list *mem; |
| 417 | |
| 418 | snd_assert(dmab, return 0); |
| 419 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 420 | mutex_lock(&list_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 421 | list_for_each_entry(mem, &mem_list_head, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (mem->id == id && |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 423 | (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL || |
| 424 | ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) { |
| 425 | struct device *dev = dmab->dev.dev; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 426 | list_del(&mem->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | *dmab = mem->buffer; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 428 | if (dmab->dev.dev == NULL) |
| 429 | dmab->dev.dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | kfree(mem); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 431 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return dmab->bytes; |
| 433 | } |
| 434 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 435 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * snd_dma_reserve_buf - reserve the buffer |
| 441 | * @dmab: the buffer to reserve |
| 442 | * @id: the buffer id |
| 443 | * |
| 444 | * Reserves the given buffer as a reserved buffer. |
| 445 | * |
| 446 | * Returns zero if successful, or a negative code at error. |
| 447 | */ |
| 448 | int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id) |
| 449 | { |
| 450 | struct snd_mem_list *mem; |
| 451 | |
| 452 | snd_assert(dmab, return -EINVAL); |
| 453 | mem = kmalloc(sizeof(*mem), GFP_KERNEL); |
| 454 | if (! mem) |
| 455 | return -ENOMEM; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 456 | mutex_lock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | mem->buffer = *dmab; |
| 458 | mem->id = id; |
| 459 | list_add_tail(&mem->list, &mem_list_head); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 460 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * purge all reserved buffers |
| 466 | */ |
| 467 | static void free_all_reserved_pages(void) |
| 468 | { |
| 469 | struct list_head *p; |
| 470 | struct snd_mem_list *mem; |
| 471 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 472 | mutex_lock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | while (! list_empty(&mem_list_head)) { |
| 474 | p = mem_list_head.next; |
| 475 | mem = list_entry(p, struct snd_mem_list, list); |
| 476 | list_del(p); |
| 477 | snd_dma_free_pages(&mem->buffer); |
| 478 | kfree(mem); |
| 479 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 480 | mutex_unlock(&list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | #ifdef CONFIG_PROC_FS |
| 485 | /* |
| 486 | * proc file interface |
| 487 | */ |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 488 | #define SND_MEM_PROC_FILE "driver/snd-page-alloc" |
Clemens Ladisch | a53fc18 | 2005-08-11 15:59:17 +0200 | [diff] [blame] | 489 | static struct proc_dir_entry *snd_mem_proc; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 490 | |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 491 | static int snd_mem_proc_read(struct seq_file *seq, void *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | long pages = snd_allocated_pages >> (PAGE_SHIFT-12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | struct snd_mem_list *mem; |
| 495 | int devno; |
| 496 | static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" }; |
| 497 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 498 | mutex_lock(&list_mutex); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 499 | seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n", |
| 500 | pages * PAGE_SIZE, pages, PAGE_SIZE / 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | devno = 0; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 502 | list_for_each_entry(mem, &mem_list_head, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | devno++; |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 504 | seq_printf(seq, "buffer %d : ID %08x : type %s\n", |
| 505 | devno, mem->id, types[mem->buffer.dev.type]); |
| 506 | seq_printf(seq, " addr = 0x%lx, size = %d bytes\n", |
| 507 | (unsigned long)mem->buffer.addr, |
| 508 | (int)mem->buffer.bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 510 | mutex_unlock(&list_mutex); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static int snd_mem_proc_open(struct inode *inode, struct file *file) |
| 515 | { |
| 516 | return single_open(file, snd_mem_proc_read, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 518 | |
| 519 | /* FIXME: for pci only - other bus? */ |
| 520 | #ifdef CONFIG_PCI |
| 521 | #define gettoken(bufp) strsep(bufp, " \t\n") |
| 522 | |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 523 | static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer, |
| 524 | size_t count, loff_t * ppos) |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 525 | { |
| 526 | char buf[128]; |
| 527 | char *token, *p; |
| 528 | |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 529 | if (count > sizeof(buf) - 1) |
| 530 | return -EINVAL; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 531 | if (copy_from_user(buf, buffer, count)) |
| 532 | return -EFAULT; |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 533 | buf[count] = '\0'; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 534 | |
| 535 | p = buf; |
| 536 | token = gettoken(&p); |
| 537 | if (! token || *token == '#') |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 538 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 539 | if (strcmp(token, "add") == 0) { |
| 540 | char *endp; |
| 541 | int vendor, device, size, buffers; |
| 542 | long mask; |
| 543 | int i, alloced; |
| 544 | struct pci_dev *pci; |
| 545 | |
| 546 | if ((token = gettoken(&p)) == NULL || |
| 547 | (vendor = simple_strtol(token, NULL, 0)) <= 0 || |
| 548 | (token = gettoken(&p)) == NULL || |
| 549 | (device = simple_strtol(token, NULL, 0)) <= 0 || |
| 550 | (token = gettoken(&p)) == NULL || |
| 551 | (mask = simple_strtol(token, NULL, 0)) < 0 || |
| 552 | (token = gettoken(&p)) == NULL || |
| 553 | (size = memparse(token, &endp)) < 64*1024 || |
| 554 | size > 16*1024*1024 /* too big */ || |
| 555 | (token = gettoken(&p)) == NULL || |
| 556 | (buffers = simple_strtol(token, NULL, 0)) <= 0 || |
| 557 | buffers > 4) { |
| 558 | printk(KERN_ERR "snd-page-alloc: invalid proc write format\n"); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 559 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 560 | } |
| 561 | vendor &= 0xffff; |
| 562 | device &= 0xffff; |
| 563 | |
| 564 | alloced = 0; |
| 565 | pci = NULL; |
Jiri Slaby | 0dd119f | 2005-09-07 14:28:33 +0200 | [diff] [blame] | 566 | while ((pci = pci_get_device(vendor, device, pci)) != NULL) { |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 567 | if (mask > 0 && mask < 0xffffffff) { |
| 568 | if (pci_set_dma_mask(pci, mask) < 0 || |
| 569 | pci_set_consistent_dma_mask(pci, mask) < 0) { |
| 570 | printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 571 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | for (i = 0; i < buffers; i++) { |
| 575 | struct snd_dma_buffer dmab; |
| 576 | memset(&dmab, 0, sizeof(dmab)); |
| 577 | if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), |
| 578 | size, &dmab) < 0) { |
| 579 | 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] | 580 | pci_dev_put(pci); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 581 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 582 | } |
| 583 | snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci)); |
| 584 | } |
| 585 | alloced++; |
| 586 | } |
| 587 | if (! alloced) { |
| 588 | for (i = 0; i < buffers; i++) { |
| 589 | struct snd_dma_buffer dmab; |
| 590 | memset(&dmab, 0, sizeof(dmab)); |
| 591 | /* FIXME: We can allocate only in ZONE_DMA |
| 592 | * without a device pointer! |
| 593 | */ |
| 594 | if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL, |
| 595 | size, &dmab) < 0) { |
| 596 | printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size); |
| 597 | break; |
| 598 | } |
| 599 | snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device)); |
| 600 | } |
| 601 | } |
| 602 | } else if (strcmp(token, "erase") == 0) |
| 603 | /* FIXME: need for releasing each buffer chunk? */ |
| 604 | free_all_reserved_pages(); |
| 605 | else |
| 606 | printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n"); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 607 | return count; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 608 | } |
| 609 | #endif /* CONFIG_PCI */ |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 610 | |
| 611 | static const struct file_operations snd_mem_proc_fops = { |
| 612 | .owner = THIS_MODULE, |
| 613 | .open = snd_mem_proc_open, |
| 614 | .read = seq_read, |
| 615 | #ifdef CONFIG_PCI |
| 616 | .write = snd_mem_proc_write, |
| 617 | #endif |
| 618 | .llseek = seq_lseek, |
| 619 | .release = single_release, |
| 620 | }; |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | #endif /* CONFIG_PROC_FS */ |
| 623 | |
| 624 | /* |
| 625 | * module entry |
| 626 | */ |
| 627 | |
| 628 | static int __init snd_mem_init(void) |
| 629 | { |
| 630 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 631 | snd_mem_proc = create_proc_entry(SND_MEM_PROC_FILE, 0644, NULL); |
Takashi Iwai | ccec6e2 | 2007-09-17 21:55:10 +0200 | [diff] [blame] | 632 | if (snd_mem_proc) |
| 633 | snd_mem_proc->proc_fops = &snd_mem_proc_fops; |
Takashi Iwai | b6a9691 | 2005-05-30 18:27:03 +0200 | [diff] [blame] | 634 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static void __exit snd_mem_exit(void) |
| 639 | { |
Takashi Iwai | e0be4d3 | 2005-08-23 11:11:03 +0200 | [diff] [blame] | 640 | remove_proc_entry(SND_MEM_PROC_FILE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | free_all_reserved_pages(); |
| 642 | if (snd_allocated_pages > 0) |
| 643 | printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages); |
| 644 | } |
| 645 | |
| 646 | |
| 647 | module_init(snd_mem_init) |
| 648 | module_exit(snd_mem_exit) |
| 649 | |
| 650 | |
| 651 | /* |
| 652 | * exports |
| 653 | */ |
| 654 | EXPORT_SYMBOL(snd_dma_alloc_pages); |
| 655 | EXPORT_SYMBOL(snd_dma_alloc_pages_fallback); |
| 656 | EXPORT_SYMBOL(snd_dma_free_pages); |
| 657 | |
| 658 | EXPORT_SYMBOL(snd_dma_get_reserved_buf); |
| 659 | EXPORT_SYMBOL(snd_dma_reserve_buf); |
| 660 | |
| 661 | EXPORT_SYMBOL(snd_malloc_pages); |
| 662 | EXPORT_SYMBOL(snd_free_pages); |