| 123 | | #endif /* CONFIG_PCI */ |
| | 127 | /* |
| | 128 | * |
| | 129 | * Generic memory allocators |
| | 130 | * |
| | 131 | */ |
| | 132 | |
| | 133 | static long snd_allocated_pages; /* holding the number of allocated pages */ |
| | 134 | |
| | 135 | static inline void inc_snd_pages(int order) |
| | 136 | { |
| | 137 | snd_allocated_pages += 1 << order; |
| | 138 | } |
| | 139 | |
| | 140 | static inline void dec_snd_pages(int order) |
| | 141 | { |
| | 142 | snd_allocated_pages -= 1 << order; |
| | 143 | } |
| | 144 | |
| | 145 | /** |
| | 146 | * snd_malloc_pages - allocate pages with the given size |
| | 147 | * @size: the size to allocate in bytes |
| | 148 | * @gfp_flags: the allocation conditions, GFP_XXX |
| | 149 | * |
| | 150 | * Allocates the physically contiguous pages with the given size. |
| | 151 | * |
| | 152 | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| | 153 | */ |
| | 154 | void *snd_malloc_pages(size_t size, unsigned int dma_flags) |
| | 155 | { |
| | 156 | int pg; |
| | 157 | void *res; |
| | 158 | |
| | 159 | snd_assert(size > 0, return NULL); |
| | 160 | snd_assert(dma_flags != 0, return NULL); |
| | 161 | pg = get_order(size); |
| | 162 | if ((res = (void *) __get_free_pages(dma_flags, pg)) != NULL) { |
| | 163 | inc_snd_pages(pg); |
| | 164 | } |
| | 165 | return res; |
| | 166 | } |
| | 167 | |
| | 168 | /** |
| | 169 | * snd_malloc_pages_fallback - allocate pages with the given size with fallback |
| | 170 | * @size: the requested size to allocate in bytes |
| | 171 | * @gfp_flags: the allocation conditions, GFP_XXX |
| | 172 | * @res_size: the pointer to store the size of buffer actually allocated |
| | 173 | * |
| | 174 | * Allocates the physically contiguous pages with the given request |
| | 175 | * size. When no space is left, this function reduces the size and |
| | 176 | * tries to allocate again. The size actually allocated is stored in |
| | 177 | * res_size argument. |
| | 178 | * |
| | 179 | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| | 180 | */ |
| | 181 | void *snd_malloc_pages_fallback(size_t size, unsigned int gfp_flags, size_t *res_size) |
| | 182 | { |
| | 183 | void *res; |
| | 184 | |
| | 185 | snd_assert(size > 0, return NULL); |
| | 186 | snd_assert(res_size != NULL, return NULL); |
| | 187 | do { |
| | 188 | if ((res = snd_malloc_pages(size, gfp_flags)) != NULL) { |
| | 189 | *res_size = size; |
| | 190 | return res; |
| | 191 | } |
| | 192 | size >>= 1; |
| | 193 | } while (size >= PAGE_SIZE); |
| | 194 | return NULL; |
| | 195 | } |
| | 196 | |
| | 197 | /** |
| | 198 | * snd_free_pages - release the pages |
| | 199 | * @ptr: the buffer pointer to release |
| | 200 | * @size: the allocated buffer size |
| | 201 | * |
| | 202 | * Releases the buffer allocated via snd_malloc_pages(). |
| | 203 | */ |
| | 204 | void snd_free_pages(void *ptr, size_t size) |
| | 205 | { |
| | 206 | int pg; |
| | 207 | struct page *page, *last_page; |
| | 208 | |
| | 209 | if (ptr == NULL) |
| | 210 | return; |
| | 211 | pg = get_order(size); |
| | 212 | dec_snd_pages(pg); |
| | 213 | free_pages((unsigned long) ptr, pg); |
| | 214 | } |
| | 215 | |
| | 216 | /* |
| | 217 | * |
| | 218 | * Bus-specific memory allocators |
| | 219 | * |
| | 220 | */ |
| 375 | | static long snd_allocated_pages; /* holding the number of allocated pages */ |
| 376 | | |
| 377 | | static inline void inc_snd_pages(int order) |
| 378 | | { |
| 379 | | snd_allocated_pages += 1 << order; |
| 380 | | } |
| 381 | | |
| 382 | | static inline void dec_snd_pages(int order) |
| 383 | | { |
| 384 | | snd_allocated_pages -= 1 << order; |
| 385 | | } |
| 386 | | |
| 387 | | |
| 388 | | /** |
| 389 | | * snd_malloc_pages - allocate pages with the given size |
| 390 | | * @size: the size to allocate in bytes |
| 391 | | * @gfp_flags: the allocation conditions, GFP_XXX |
| 392 | | * |
| 393 | | * Allocates the physically contiguous pages with the given size. |
| 394 | | * |
| 395 | | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 396 | | */ |
| 397 | | void *snd_malloc_pages(size_t size, unsigned int dma_flags) |
| 398 | | { |
| 399 | | int pg; |
| 400 | | void *res; |
| 401 | | |
| 402 | | snd_assert(size > 0, return NULL); |
| 403 | | snd_assert(dma_flags != 0, return NULL); |
| 404 | | pg = get_order(size); |
| 405 | | if ((res = (void *) __get_free_pages(dma_flags, pg)) != NULL) { |
| 406 | | inc_snd_pages(pg); |
| 407 | | } |
| 408 | | return res; |
| 409 | | } |
| 410 | | |
| 411 | | /** |
| 412 | | * snd_free_pages - release the pages |
| 413 | | * @ptr: the buffer pointer to release |
| 414 | | * @size: the allocated buffer size |
| 415 | | * |
| 416 | | * Releases the buffer allocated via snd_malloc_pages(). |
| 417 | | */ |
| 418 | | void snd_free_pages(void *ptr, size_t size) |
| 419 | | { |
| 420 | | int pg; |
| 421 | | struct page *page, *last_page; |
| 422 | | |
| 423 | | if (ptr == NULL) |
| 424 | | return; |
| 425 | | pg = get_order(size); |
| 426 | | dec_snd_pages(pg); |
| 427 | | free_pages((unsigned long) ptr, pg); |
| 428 | | } |
| 429 | | |
| 430 | | #if defined(CONFIG_ISA) && ! defined(CONFIG_PCI) |
| 431 | | |
| 432 | | /** |
| 433 | | * snd_malloc_isa_pages - allocate pages for ISA bus with the given size |
| 434 | | * @size: the size to allocate in bytes |
| 435 | | * @dma_addr: the pointer to store the physical address of the buffer |
| 436 | | * |
| 437 | | * Allocates the physically contiguous pages with the given size for |
| 438 | | * ISA bus. |
| 439 | | * |
| 440 | | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 441 | | */ |
| 442 | | void *snd_malloc_isa_pages(size_t size, dma_addr_t *dma_addr) |
| 443 | | { |
| 444 | | void *dma_area; |
| 445 | | dma_area = snd_malloc_pages(size, GFP_ATOMIC|GFP_DMA); |
| 446 | | *dma_addr = dma_area ? isa_virt_to_bus(dma_area) : 0UL; |
| 447 | | return dma_area; |
| 448 | | } |
| 449 | | |
| 450 | | /** |
| 451 | | * snd_malloc_isa_pages_fallback - allocate pages with the given size with fallback for ISA bus |
| 452 | | * @size: the requested size to allocate in bytes |
| 453 | | * @dma_addr: the pointer to store the physical address of the buffer |
| 454 | | * @res_size: the pointer to store the size of buffer actually allocated |
| 455 | | * |
| 456 | | * Allocates the physically contiguous pages with the given request |
| 457 | | * size for PCI bus. When no space is left, this function reduces the size and |
| 458 | | * tries to allocate again. The size actually allocated is stored in |
| 459 | | * res_size argument. |
| 460 | | * |
| 461 | | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 462 | | */ |
| 463 | | void *snd_malloc_isa_pages_fallback(size_t size, |
| 464 | | dma_addr_t *dma_addr, |
| 465 | | size_t *res_size) |
| 466 | | { |
| 467 | | void *dma_area; |
| 468 | | dma_area = snd_malloc_pages_fallback(size, GFP_ATOMIC|GFP_DMA, res_size); |
| 469 | | *dma_addr = dma_area ? isa_virt_to_bus(dma_area) : 0UL; |
| 470 | | return dma_area; |
| 471 | | } |
| 472 | | |
| 473 | | #endif /* CONFIG_ISA && !CONFIG_PCI */ |
| 474 | | |
| 475 | | #ifdef CONFIG_PCI |
| 476 | | |
| 477 | | /** |
| 478 | | * snd_malloc_pci_pages - allocate pages for PCI bus with the given size |
| 479 | | * @pci: the pci device pointer |
| 480 | | * @size: the size to allocate in bytes |
| 481 | | * @dma_addr: the pointer to store the physical address of the buffer |
| 482 | | * |
| 483 | | * Allocates the physically contiguous pages with the given size for |
| 484 | | * PCI bus. |
| 485 | | * |
| 486 | | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 487 | | */ |
| 488 | | void *snd_malloc_pci_pages(struct pci_dev *pci, |
| 489 | | size_t size, |
| 490 | | dma_addr_t *dmaaddr) |
| 491 | | { |
| 492 | | int pg; |
| 493 | | void *res; |
| 494 | | #ifdef DEBUG |
| 495 | | dprintf(("snd_malloc_pci_pages. size = %d",size)); |
| 496 | | #endif |
| 497 | | snd_assert(size > 0, return NULL); |
| 498 | | snd_assert(dmaaddr != NULL, return NULL); |
| 499 | | for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++); |
| 500 | | res = pci_alloc_consistent(pci, PAGE_SIZE * (1 << pg), dmaaddr); |
| 501 | | if (res != NULL) { |
| 502 | | inc_snd_pages(pg); |
| 503 | | } |
| 504 | | return res; |
| 505 | | } |
| 506 | | |
| 507 | | /** |
| 508 | | * snd_malloc_pci_pages_fallback - allocate pages with the given size with fallback for PCI bus |
| 509 | | * @pci: pci device pointer |
| 510 | | * @size: the requested size to allocate in bytes |
| 511 | | * @dma_addr: the pointer to store the physical address of the buffer |
| 512 | | * @res_size: the pointer to store the size of buffer actually allocated |
| 513 | | * |
| 514 | | * Allocates the physically contiguous pages with the given request |
| 515 | | * size for PCI bus. When no space is left, this function reduces the size and |
| 516 | | * tries to allocate again. The size actually allocated is stored in |
| 517 | | * res_size argument. |
| 518 | | * |
| 519 | | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 520 | | */ |
| 521 | | void *snd_malloc_pci_pages_fallback(struct pci_dev *pci, size_t size, |
| 522 | | dma_addr_t *dmaaddr, |
| 523 | | size_t *res_size) |
| 524 | | { |
| 525 | | void *res; |
| 526 | | |
| 527 | | snd_assert(res_size != NULL, return NULL); |
| 528 | | do { |
| 529 | | if ((res = snd_malloc_pci_pages(pci, size, dmaaddr)) != NULL) { |
| 530 | | *res_size = size; |
| 531 | | return res; |
| 532 | | } |
| 533 | | size >>= 1; |
| 534 | | } while (size >= PAGE_SIZE); |
| 535 | | return NULL; |
| 536 | | } |
| 537 | | |
| 538 | | /** |
| 539 | | * snd_free_pci_pages - release the pages |
| 540 | | * @pci: pci device pointer |
| 541 | | * @size: the allocated buffer size |
| 542 | | * @ptr: the buffer pointer to release |
| 543 | | * @dma_addr: the physical address of the buffer |
| 544 | | * |
| 545 | | * Releases the buffer allocated via snd_malloc_pci_pages(). |
| 546 | | */ |
| 547 | | void snd_free_pci_pages(struct pci_dev *pci, size_t size, void *ptr, dma_addr_t dmaaddr) |
| 548 | | { |
| 549 | | int pg; |
| 550 | | mem_map_t *page, *last_page; |
| 551 | | |
| 552 | | if (ptr == NULL) |
| 553 | | return; |
| 554 | | for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++); |
| 555 | | dec_snd_pages(pg); |
| 556 | | pci_free_consistent(pci, PAGE_SIZE * (1 << pg), ptr, dmaaddr); |
| 557 | | } |
| 558 | | |
| 559 | | #if defined(__i386__) |
| 560 | | /* |
| 561 | | * on ix86, we allocate a page with GFP_KERNEL to assure the |
| 562 | | * allocation. the code is almost same with kernel/i386/pci-dma.c but |
| 563 | | * it allocates only a single page and checks the validity of the |
| 564 | | * page address with the given pci dma mask. |
| 565 | | */ |
| 566 | | |
| 567 | | /** |
| 568 | | * snd_malloc_pci_page - allocate a page in the valid pci dma mask |
| 569 | | * @pci: pci device pointer |
| 570 | | * @addrp: the pointer to store the physical address of the buffer |
| 571 | | * |
| 572 | | * Allocates a single page for the given PCI device and returns |
| 573 | | * the virtual address and stores the physical address on addrp. |
| 574 | | * |
| 575 | | * This function cannot be called from interrupt handlers or |
| 576 | | * within spinlocks. |
| 577 | | */ |
| 578 | | void *snd_malloc_pci_page(struct pci_dev *pci, dma_addr_t *addrp) |
| 579 | | { |
| 580 | | void *ptr; |
| 581 | | dma_addr_t addr; |
| 582 | | unsigned long mask; |
| 583 | | |
| 584 | | mask = pci ? (unsigned long)pci->consistent_dma_mask : 0x00ffffffUL; |
| 585 | | ptr = (void *)__get_free_page(GFP_KERNEL); |
| 586 | | if (ptr) { |
| 587 | | addr = virt_to_phys(ptr); |
| 588 | | if (((unsigned long)addr + PAGE_SIZE - 1) & ~mask) { |
| 589 | | /* try to reallocate with the GFP_DMA */ |
| 590 | | free_page((unsigned long)ptr); |
| 591 | | /* use GFP_ATOMIC for the DMA zone to avoid stall */ |
| 592 | | ptr = (void *)__get_free_page(GFP_ATOMIC | GFP_DMA); |
| 593 | | if (ptr) /* ok, the address must be within lower 16MB... */ |
| 594 | | addr = virt_to_phys(ptr); |
| 595 | | else |
| 596 | | addr = 0; |
| 597 | | } |
| 598 | | } else |
| 599 | | addr = 0; |
| 600 | | if (ptr) { |
| 601 | | memset(ptr, 0, PAGE_SIZE); |
| 602 | | } |
| 603 | | *addrp = addr; |
| 604 | | return ptr; |
| 605 | | } |
| 606 | | #else |
| 607 | | |
| 608 | | /* on other architectures, call snd_malloc_pci_pages() helper function |
| 609 | | * which uses pci_alloc_consistent(). |
| 610 | | */ |
| 611 | | void *snd_malloc_pci_page(struct pci_dev *pci, dma_addr_t *addrp) |
| 612 | | { |
| 613 | | return snd_malloc_pci_pages(pci, PAGE_SIZE, addrp); |
| 614 | | } |
| 615 | | |
| 616 | | #endif |
| 617 | | |
| 618 | | #endif /* CONFIG_PCI */ |
| 619 | | |
| 620 | | #ifdef CONFIG_SBUS |
| 621 | | |
| 622 | | /** |
| 623 | | * snd_malloc_sbus_pages - allocate pages for SBUS with the given size |
| 624 | | * @sdev: sbus device pointer |
| 625 | | * @size: the size to allocate in bytes |
| 626 | | * @dma_addr: the pointer to store the physical address of the buffer |
| 627 | | * |
| 628 | | * Allocates the physically contiguous pages with the given size for |
| 629 | | * SBUS. |
| 630 | | * |
| 631 | | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 632 | | */ |
| 633 | | void *snd_malloc_sbus_pages(struct sbus_dev *sdev, |
| 634 | | size_t size, |
| 635 | | dma_addr_t *dma_addr) |
| 636 | | { |
| 637 | | int pg; |
| 638 | | void *res; |
| 639 | | |
| 640 | | snd_assert(size > 0, return NULL); |
| 641 | | snd_assert(dma_addr != NULL, return NULL); |
| 642 | | pg = get_order(size); |
| 643 | | res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr); |
| 644 | | if (res != NULL) |
| 645 | | inc_snd_pages(pg); |
| 646 | | return res; |
| 647 | | } |
| 648 | | |
| 649 | | /** |
| 650 | | * snd_malloc_pci_pages_fallback - allocate pages with the given size with fallback for SBUS |
| 651 | | * @sdev: sbus device pointer |
| 652 | | * @size: the requested size to allocate in bytes |
| 653 | | * @dma_addr: the pointer to store the physical address of the buffer |
| 654 | | * @res_size: the pointer to store the size of buffer actually allocated |
| 655 | | * |
| 656 | | * Allocates the physically contiguous pages with the given request |
| 657 | | * size for SBUS. When no space is left, this function reduces the size and |
| 658 | | * tries to allocate again. The size actually allocated is stored in |
| 659 | | * res_size argument. |
| 660 | | * |
| 661 | | * Returns the pointer of the buffer, or NULL if no enoguh memory. |
| 662 | | */ |
| 663 | | /** |
| 664 | | * snd_free_sbus_pages - release the pages |
| 665 | | * @sdev: sbus device pointer |
| 666 | | * @size: the allocated buffer size |
| 667 | | * @ptr: the buffer pointer to release |
| 668 | | * @dma_addr: the physical address of the buffer |
| 669 | | * |
| 670 | | * Releases the buffer allocated via snd_malloc_pci_pages(). |
| 671 | | */ |
| 672 | | void snd_free_sbus_pages(struct sbus_dev *sdev, |
| 673 | | size_t size, |
| 674 | | void *ptr, |
| 675 | | dma_addr_t dma_addr) |
| 676 | | { |
| 677 | | int pg; |
| 678 | | |
| 679 | | if (ptr == NULL) |
| 680 | | return; |
| 681 | | pg = get_order(size); |
| 682 | | dec_snd_pages(pg); |
| 683 | | sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr); |
| 684 | | } |
| 685 | | |
| 686 | | #endif /* CONFIG_SBUS */ |
| | 521 | |