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