dma-mapping.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Dynamic DMA mapping support
  3. *
  4. * Copyright 2005-2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later
  7. */
  8. #include <linux/types.h>
  9. #include <linux/gfp.h>
  10. #include <linux/string.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/export.h>
  15. #include <linux/bitmap.h>
  16. static spinlock_t dma_page_lock;
  17. static unsigned long *dma_page;
  18. static unsigned int dma_pages;
  19. static unsigned long dma_base;
  20. static unsigned long dma_size;
  21. static unsigned int dma_initialized;
  22. static void dma_alloc_init(unsigned long start, unsigned long end)
  23. {
  24. spin_lock_init(&dma_page_lock);
  25. dma_initialized = 0;
  26. dma_page = (unsigned long *)__get_free_page(GFP_KERNEL);
  27. memset(dma_page, 0, PAGE_SIZE);
  28. dma_base = PAGE_ALIGN(start);
  29. dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start);
  30. dma_pages = dma_size >> PAGE_SHIFT;
  31. memset((void *)dma_base, 0, DMA_UNCACHED_REGION);
  32. dma_initialized = 1;
  33. printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__,
  34. dma_page, dma_pages, dma_base);
  35. }
  36. static inline unsigned int get_pages(size_t size)
  37. {
  38. return ((size - 1) >> PAGE_SHIFT) + 1;
  39. }
  40. static unsigned long __alloc_dma_pages(unsigned int pages)
  41. {
  42. unsigned long ret = 0, flags;
  43. unsigned long start;
  44. if (dma_initialized == 0)
  45. dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend);
  46. spin_lock_irqsave(&dma_page_lock, flags);
  47. start = bitmap_find_next_zero_area(dma_page, dma_pages, 0, pages, 0);
  48. if (start < dma_pages) {
  49. ret = dma_base + (start << PAGE_SHIFT);
  50. bitmap_set(dma_page, start, pages);
  51. }
  52. spin_unlock_irqrestore(&dma_page_lock, flags);
  53. return ret;
  54. }
  55. static void __free_dma_pages(unsigned long addr, unsigned int pages)
  56. {
  57. unsigned long page = (addr - dma_base) >> PAGE_SHIFT;
  58. unsigned long flags;
  59. if ((page + pages) > dma_pages) {
  60. printk(KERN_ERR "%s: freeing outside range.\n", __func__);
  61. BUG();
  62. }
  63. spin_lock_irqsave(&dma_page_lock, flags);
  64. bitmap_clear(dma_page, page, pages);
  65. spin_unlock_irqrestore(&dma_page_lock, flags);
  66. }
  67. static void *bfin_dma_alloc(struct device *dev, size_t size,
  68. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  69. {
  70. void *ret;
  71. ret = (void *)__alloc_dma_pages(get_pages(size));
  72. if (ret) {
  73. memset(ret, 0, size);
  74. *dma_handle = virt_to_phys(ret);
  75. }
  76. return ret;
  77. }
  78. static void bfin_dma_free(struct device *dev, size_t size, void *vaddr,
  79. dma_addr_t dma_handle, unsigned long attrs)
  80. {
  81. __free_dma_pages((unsigned long)vaddr, get_pages(size));
  82. }
  83. /*
  84. * Streaming DMA mappings
  85. */
  86. void __dma_sync(dma_addr_t addr, size_t size,
  87. enum dma_data_direction dir)
  88. {
  89. __dma_sync_inline(addr, size, dir);
  90. }
  91. EXPORT_SYMBOL(__dma_sync);
  92. static int bfin_dma_map_sg(struct device *dev, struct scatterlist *sg_list,
  93. int nents, enum dma_data_direction direction,
  94. unsigned long attrs)
  95. {
  96. struct scatterlist *sg;
  97. int i;
  98. for_each_sg(sg_list, sg, nents, i) {
  99. sg->dma_address = (dma_addr_t) sg_virt(sg);
  100. __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
  101. }
  102. return nents;
  103. }
  104. static void bfin_dma_sync_sg_for_device(struct device *dev,
  105. struct scatterlist *sg_list, int nelems,
  106. enum dma_data_direction direction)
  107. {
  108. struct scatterlist *sg;
  109. int i;
  110. for_each_sg(sg_list, sg, nelems, i) {
  111. sg->dma_address = (dma_addr_t) sg_virt(sg);
  112. __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
  113. }
  114. }
  115. static dma_addr_t bfin_dma_map_page(struct device *dev, struct page *page,
  116. unsigned long offset, size_t size, enum dma_data_direction dir,
  117. unsigned long attrs)
  118. {
  119. dma_addr_t handle = (dma_addr_t)(page_address(page) + offset);
  120. _dma_sync(handle, size, dir);
  121. return handle;
  122. }
  123. static inline void bfin_dma_sync_single_for_device(struct device *dev,
  124. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  125. {
  126. _dma_sync(handle, size, dir);
  127. }
  128. struct dma_map_ops bfin_dma_ops = {
  129. .alloc = bfin_dma_alloc,
  130. .free = bfin_dma_free,
  131. .map_page = bfin_dma_map_page,
  132. .map_sg = bfin_dma_map_sg,
  133. .sync_single_for_device = bfin_dma_sync_single_for_device,
  134. .sync_sg_for_device = bfin_dma_sync_sg_for_device,
  135. };
  136. EXPORT_SYMBOL(bfin_dma_ops);