dma-mapping.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * File: arch/blackfin/kernel/dma-mapping.c
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description: Dynamic DMA mapping support.
  8. *
  9. * Modified:
  10. * Copyright 2004-2006 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <linux/types.h>
  30. #include <linux/mm.h>
  31. #include <linux/string.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/device.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/io.h>
  37. #include <asm/cacheflush.h>
  38. #include <asm/bfin-global.h>
  39. static spinlock_t dma_page_lock;
  40. static unsigned int *dma_page;
  41. static unsigned int dma_pages;
  42. static unsigned long dma_base;
  43. static unsigned long dma_size;
  44. static unsigned int dma_initialized;
  45. void dma_alloc_init(unsigned long start, unsigned long end)
  46. {
  47. spin_lock_init(&dma_page_lock);
  48. dma_initialized = 0;
  49. dma_page = (unsigned int *)__get_free_page(GFP_KERNEL);
  50. memset(dma_page, 0, PAGE_SIZE);
  51. dma_base = PAGE_ALIGN(start);
  52. dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start);
  53. dma_pages = dma_size >> PAGE_SHIFT;
  54. memset((void *)dma_base, 0, DMA_UNCACHED_REGION);
  55. dma_initialized = 1;
  56. printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __FUNCTION__,
  57. dma_page, dma_pages, dma_base);
  58. }
  59. static inline unsigned int get_pages(size_t size)
  60. {
  61. return ((size - 1) >> PAGE_SHIFT) + 1;
  62. }
  63. static unsigned long __alloc_dma_pages(unsigned int pages)
  64. {
  65. unsigned long ret = 0, flags;
  66. int i, count = 0;
  67. if (dma_initialized == 0)
  68. dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend);
  69. spin_lock_irqsave(&dma_page_lock, flags);
  70. for (i = 0; i < dma_pages;) {
  71. if (dma_page[i++] == 0) {
  72. if (++count == pages) {
  73. while (count--)
  74. dma_page[--i] = 1;
  75. ret = dma_base + (i << PAGE_SHIFT);
  76. break;
  77. }
  78. } else
  79. count = 0;
  80. }
  81. spin_unlock_irqrestore(&dma_page_lock, flags);
  82. return ret;
  83. }
  84. static void __free_dma_pages(unsigned long addr, unsigned int pages)
  85. {
  86. unsigned long page = (addr - dma_base) >> PAGE_SHIFT;
  87. unsigned long flags;
  88. int i;
  89. if ((page + pages) > dma_pages) {
  90. printk(KERN_ERR "%s: freeing outside range.\n", __FUNCTION__);
  91. BUG();
  92. }
  93. spin_lock_irqsave(&dma_page_lock, flags);
  94. for (i = page; i < page + pages; i++) {
  95. dma_page[i] = 0;
  96. }
  97. spin_unlock_irqrestore(&dma_page_lock, flags);
  98. }
  99. void *dma_alloc_coherent(struct device *dev, size_t size,
  100. dma_addr_t * dma_handle, gfp_t gfp)
  101. {
  102. void *ret;
  103. ret = (void *)__alloc_dma_pages(get_pages(size));
  104. if (ret) {
  105. memset(ret, 0, size);
  106. *dma_handle = virt_to_phys(ret);
  107. }
  108. return ret;
  109. }
  110. EXPORT_SYMBOL(dma_alloc_coherent);
  111. void
  112. dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  113. dma_addr_t dma_handle)
  114. {
  115. __free_dma_pages((unsigned long)vaddr, get_pages(size));
  116. }
  117. EXPORT_SYMBOL(dma_free_coherent);
  118. /*
  119. * Dummy functions defined for some existing drivers
  120. */
  121. dma_addr_t
  122. dma_map_single(struct device *dev, void *ptr, size_t size,
  123. enum dma_data_direction direction)
  124. {
  125. BUG_ON(direction == DMA_NONE);
  126. invalidate_dcache_range((unsigned long)ptr,
  127. (unsigned long)ptr + size);
  128. return (dma_addr_t) ptr;
  129. }
  130. EXPORT_SYMBOL(dma_map_single);
  131. int
  132. dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  133. enum dma_data_direction direction)
  134. {
  135. int i;
  136. BUG_ON(direction == DMA_NONE);
  137. for (i = 0; i < nents; i++, sg++) {
  138. sg->dma_address = (dma_addr_t)(page_address(sg->page) +
  139. sg->offset);
  140. invalidate_dcache_range(sg_dma_address(sg),
  141. sg_dma_address(sg) +
  142. sg_dma_len(sg));
  143. }
  144. return nents;
  145. }
  146. EXPORT_SYMBOL(dma_map_sg);
  147. void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  148. enum dma_data_direction direction)
  149. {
  150. BUG_ON(direction == DMA_NONE);
  151. }
  152. EXPORT_SYMBOL(dma_unmap_single);
  153. void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  154. int nhwentries, enum dma_data_direction direction)
  155. {
  156. BUG_ON(direction == DMA_NONE);
  157. }
  158. EXPORT_SYMBOL(dma_unmap_sg);