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