dma-coherent.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Port on Texas Instruments TMS320C6x architecture
  3. *
  4. * Copyright (C) 2004, 2009, 2010, 2011 Texas Instruments Incorporated
  5. * Author: Aurelien Jacquiot <aurelien.jacquiot@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * DMA uncached mapping support.
  12. *
  13. * Using code pulled from ARM
  14. * Copyright (C) 2000-2004 Russell King
  15. *
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/bitmap.h>
  19. #include <linux/bitops.h>
  20. #include <linux/module.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/memblock.h>
  24. #include <asm/page.h>
  25. /*
  26. * DMA coherent memory management, can be redefined using the memdma=
  27. * kernel command line
  28. */
  29. /* none by default */
  30. static phys_addr_t dma_base;
  31. static u32 dma_size;
  32. static u32 dma_pages;
  33. static unsigned long *dma_bitmap;
  34. /* bitmap lock */
  35. static DEFINE_SPINLOCK(dma_lock);
  36. /*
  37. * Return a DMA coherent and contiguous memory chunk from the DMA memory
  38. */
  39. static inline u32 __alloc_dma_pages(int order)
  40. {
  41. unsigned long flags;
  42. u32 pos;
  43. spin_lock_irqsave(&dma_lock, flags);
  44. pos = bitmap_find_free_region(dma_bitmap, dma_pages, order);
  45. spin_unlock_irqrestore(&dma_lock, flags);
  46. return dma_base + (pos << PAGE_SHIFT);
  47. }
  48. static void __free_dma_pages(u32 addr, int order)
  49. {
  50. unsigned long flags;
  51. u32 pos = (addr - dma_base) >> PAGE_SHIFT;
  52. if (addr < dma_base || (pos + (1 << order)) >= dma_pages) {
  53. printk(KERN_ERR "%s: freeing outside range.\n", __func__);
  54. BUG();
  55. }
  56. spin_lock_irqsave(&dma_lock, flags);
  57. bitmap_release_region(dma_bitmap, pos, order);
  58. spin_unlock_irqrestore(&dma_lock, flags);
  59. }
  60. /*
  61. * Allocate DMA coherent memory space and return both the kernel
  62. * virtual and DMA address for that space.
  63. */
  64. void *c6x_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  65. gfp_t gfp, unsigned long attrs)
  66. {
  67. u32 paddr;
  68. int order;
  69. if (!dma_size || !size)
  70. return NULL;
  71. order = get_count_order(((size - 1) >> PAGE_SHIFT) + 1);
  72. paddr = __alloc_dma_pages(order);
  73. if (handle)
  74. *handle = paddr;
  75. if (!paddr)
  76. return NULL;
  77. return phys_to_virt(paddr);
  78. }
  79. /*
  80. * Free DMA coherent memory as defined by the above mapping.
  81. */
  82. void c6x_dma_free(struct device *dev, size_t size, void *vaddr,
  83. dma_addr_t dma_handle, unsigned long attrs)
  84. {
  85. int order;
  86. if (!dma_size || !size)
  87. return;
  88. order = get_count_order(((size - 1) >> PAGE_SHIFT) + 1);
  89. __free_dma_pages(virt_to_phys(vaddr), order);
  90. }
  91. /*
  92. * Initialise the coherent DMA memory allocator using the given uncached region.
  93. */
  94. void __init coherent_mem_init(phys_addr_t start, u32 size)
  95. {
  96. phys_addr_t bitmap_phys;
  97. if (!size)
  98. return;
  99. printk(KERN_INFO
  100. "Coherent memory (DMA) region start=0x%x size=0x%x\n",
  101. start, size);
  102. dma_base = start;
  103. dma_size = size;
  104. /* allocate bitmap */
  105. dma_pages = dma_size >> PAGE_SHIFT;
  106. if (dma_size & (PAGE_SIZE - 1))
  107. ++dma_pages;
  108. bitmap_phys = memblock_alloc(BITS_TO_LONGS(dma_pages) * sizeof(long),
  109. sizeof(long));
  110. dma_bitmap = phys_to_virt(bitmap_phys);
  111. memset(dma_bitmap, 0, dma_pages * PAGE_SIZE);
  112. }