dma-mapping.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
  3. * Copyright (C) 2009 Wind River Systems Inc
  4. * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
  5. *
  6. * Based on DMA code from MIPS.
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/export.h>
  15. #include <linux/string.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/io.h>
  19. #include <linux/cache.h>
  20. #include <asm/cacheflush.h>
  21. void *dma_alloc_coherent(struct device *dev, size_t size,
  22. dma_addr_t *dma_handle, gfp_t gfp)
  23. {
  24. void *ret;
  25. /* ignore region specifiers */
  26. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  27. /* optimized page clearing */
  28. gfp |= __GFP_ZERO;
  29. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  30. gfp |= GFP_DMA;
  31. ret = (void *) __get_free_pages(gfp, get_order(size));
  32. if (ret != NULL) {
  33. *dma_handle = virt_to_phys(ret);
  34. flush_dcache_range((unsigned long) ret,
  35. (unsigned long) ret + size);
  36. ret = UNCAC_ADDR(ret);
  37. }
  38. return ret;
  39. }
  40. EXPORT_SYMBOL(dma_alloc_coherent);
  41. void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  42. dma_addr_t dma_handle)
  43. {
  44. unsigned long addr = (unsigned long) CAC_ADDR((unsigned long) vaddr);
  45. free_pages(addr, get_order(size));
  46. }
  47. EXPORT_SYMBOL(dma_free_coherent);
  48. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  49. enum dma_data_direction direction)
  50. {
  51. int i;
  52. BUG_ON(!valid_dma_direction(direction));
  53. for_each_sg(sg, sg, nents, i) {
  54. void *addr;
  55. addr = sg_virt(sg);
  56. if (addr) {
  57. __dma_sync_for_device(addr, sg->length, direction);
  58. sg->dma_address = sg_phys(sg);
  59. }
  60. }
  61. return nents;
  62. }
  63. EXPORT_SYMBOL(dma_map_sg);
  64. dma_addr_t dma_map_page(struct device *dev, struct page *page,
  65. unsigned long offset, size_t size,
  66. enum dma_data_direction direction)
  67. {
  68. void *addr;
  69. BUG_ON(!valid_dma_direction(direction));
  70. addr = page_address(page) + offset;
  71. __dma_sync_for_device(addr, size, direction);
  72. return page_to_phys(page) + offset;
  73. }
  74. EXPORT_SYMBOL(dma_map_page);
  75. void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  76. enum dma_data_direction direction)
  77. {
  78. BUG_ON(!valid_dma_direction(direction));
  79. __dma_sync_for_cpu(phys_to_virt(dma_address), size, direction);
  80. }
  81. EXPORT_SYMBOL(dma_unmap_page);
  82. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  83. enum dma_data_direction direction)
  84. {
  85. void *addr;
  86. int i;
  87. BUG_ON(!valid_dma_direction(direction));
  88. if (direction == DMA_TO_DEVICE)
  89. return;
  90. for_each_sg(sg, sg, nhwentries, i) {
  91. addr = sg_virt(sg);
  92. if (addr)
  93. __dma_sync_for_cpu(addr, sg->length, direction);
  94. }
  95. }
  96. EXPORT_SYMBOL(dma_unmap_sg);
  97. void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  98. size_t size, enum dma_data_direction direction)
  99. {
  100. BUG_ON(!valid_dma_direction(direction));
  101. __dma_sync_for_cpu(phys_to_virt(dma_handle), size, direction);
  102. }
  103. EXPORT_SYMBOL(dma_sync_single_for_cpu);
  104. void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  105. size_t size, enum dma_data_direction direction)
  106. {
  107. BUG_ON(!valid_dma_direction(direction));
  108. __dma_sync_for_device(phys_to_virt(dma_handle), size, direction);
  109. }
  110. EXPORT_SYMBOL(dma_sync_single_for_device);
  111. void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  112. unsigned long offset, size_t size,
  113. enum dma_data_direction direction)
  114. {
  115. BUG_ON(!valid_dma_direction(direction));
  116. __dma_sync_for_cpu(phys_to_virt(dma_handle), size, direction);
  117. }
  118. EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
  119. void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  120. unsigned long offset, size_t size,
  121. enum dma_data_direction direction)
  122. {
  123. BUG_ON(!valid_dma_direction(direction));
  124. __dma_sync_for_device(phys_to_virt(dma_handle), size, direction);
  125. }
  126. EXPORT_SYMBOL(dma_sync_single_range_for_device);
  127. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
  128. enum dma_data_direction direction)
  129. {
  130. int i;
  131. BUG_ON(!valid_dma_direction(direction));
  132. /* Make sure that gcc doesn't leave the empty loop body. */
  133. for_each_sg(sg, sg, nelems, i)
  134. __dma_sync_for_cpu(sg_virt(sg), sg->length, direction);
  135. }
  136. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  137. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  138. int nelems, enum dma_data_direction direction)
  139. {
  140. int i;
  141. BUG_ON(!valid_dma_direction(direction));
  142. /* Make sure that gcc doesn't leave the empty loop body. */
  143. for_each_sg(sg, sg, nelems, i)
  144. __dma_sync_for_device(sg_virt(sg), sg->length, direction);
  145. }
  146. EXPORT_SYMBOL(dma_sync_sg_for_device);