dma-mapping.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2004-2009 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later.
  5. */
  6. #ifndef _BLACKFIN_DMA_MAPPING_H
  7. #define _BLACKFIN_DMA_MAPPING_H
  8. #include <asm/cacheflush.h>
  9. struct scatterlist;
  10. void *dma_alloc_coherent(struct device *dev, size_t size,
  11. dma_addr_t *dma_handle, gfp_t gfp);
  12. void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  13. dma_addr_t dma_handle);
  14. /*
  15. * Now for the API extensions over the pci_ one
  16. */
  17. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  18. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  19. #define dma_supported(d, m) (1)
  20. #define dma_get_cache_alignment() (32)
  21. #define dma_is_consistent(d, h) (1)
  22. static inline int
  23. dma_set_mask(struct device *dev, u64 dma_mask)
  24. {
  25. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  26. return -EIO;
  27. *dev->dma_mask = dma_mask;
  28. return 0;
  29. }
  30. static inline int
  31. dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  32. {
  33. return 0;
  34. }
  35. extern void
  36. __dma_sync(dma_addr_t addr, size_t size, enum dma_data_direction dir);
  37. static inline void
  38. _dma_sync(dma_addr_t addr, size_t size, enum dma_data_direction dir)
  39. {
  40. if (!__builtin_constant_p(dir)) {
  41. __dma_sync(addr, size, dir);
  42. return;
  43. }
  44. switch (dir) {
  45. case DMA_NONE:
  46. BUG();
  47. case DMA_TO_DEVICE: /* writeback only */
  48. flush_dcache_range(addr, addr + size);
  49. break;
  50. case DMA_FROM_DEVICE: /* invalidate only */
  51. case DMA_BIDIRECTIONAL: /* flush and invalidate */
  52. /* Blackfin has no dedicated invalidate (it includes a flush) */
  53. invalidate_dcache_range(addr, addr + size);
  54. break;
  55. }
  56. }
  57. /*
  58. * Map a single buffer of the indicated size for DMA in streaming mode.
  59. * The 32-bit bus address to use is returned.
  60. *
  61. * Once the device is given the dma address, the device owns this memory
  62. * until either pci_unmap_single or pci_dma_sync_single is performed.
  63. */
  64. static inline dma_addr_t
  65. dma_map_single(struct device *dev, void *ptr, size_t size,
  66. enum dma_data_direction dir)
  67. {
  68. _dma_sync((dma_addr_t)ptr, size, dir);
  69. return (dma_addr_t) ptr;
  70. }
  71. static inline dma_addr_t
  72. dma_map_page(struct device *dev, struct page *page,
  73. unsigned long offset, size_t size,
  74. enum dma_data_direction dir)
  75. {
  76. return dma_map_single(dev, page_address(page) + offset, size, dir);
  77. }
  78. /*
  79. * Unmap a single streaming mode DMA translation. The dma_addr and size
  80. * must match what was provided for in a previous pci_map_single call. All
  81. * other usages are undefined.
  82. *
  83. * After this call, reads by the cpu to the buffer are guarenteed to see
  84. * whatever the device wrote there.
  85. */
  86. static inline void
  87. dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  88. enum dma_data_direction dir)
  89. {
  90. BUG_ON(!valid_dma_direction(dir));
  91. }
  92. static inline void
  93. dma_unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  94. enum dma_data_direction dir)
  95. {
  96. dma_unmap_single(dev, dma_addr, size, dir);
  97. }
  98. /*
  99. * Map a set of buffers described by scatterlist in streaming
  100. * mode for DMA. This is the scather-gather version of the
  101. * above pci_map_single interface. Here the scatter gather list
  102. * elements are each tagged with the appropriate dma address
  103. * and length. They are obtained via sg_dma_{address,length}(SG).
  104. *
  105. * NOTE: An implementation may be able to use a smaller number of
  106. * DMA address/length pairs than there are SG table elements.
  107. * (for example via virtual mapping capabilities)
  108. * The routine returns the number of addr/length pairs actually
  109. * used, at most nents.
  110. *
  111. * Device ownership issues as mentioned above for pci_map_single are
  112. * the same here.
  113. */
  114. extern int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  115. enum dma_data_direction dir);
  116. /*
  117. * Unmap a set of streaming mode DMA translations.
  118. * Again, cpu read rules concerning calls here are the same as for
  119. * pci_unmap_single() above.
  120. */
  121. static inline void
  122. dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  123. int nhwentries, enum dma_data_direction dir)
  124. {
  125. BUG_ON(!valid_dma_direction(dir));
  126. }
  127. static inline void
  128. dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t handle,
  129. unsigned long offset, size_t size,
  130. enum dma_data_direction dir)
  131. {
  132. BUG_ON(!valid_dma_direction(dir));
  133. }
  134. static inline void
  135. dma_sync_single_range_for_device(struct device *dev, dma_addr_t handle,
  136. unsigned long offset, size_t size,
  137. enum dma_data_direction dir)
  138. {
  139. _dma_sync(handle + offset, size, dir);
  140. }
  141. static inline void
  142. dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle, size_t size,
  143. enum dma_data_direction dir)
  144. {
  145. dma_sync_single_range_for_cpu(dev, handle, 0, size, dir);
  146. }
  147. static inline void
  148. dma_sync_single_for_device(struct device *dev, dma_addr_t handle, size_t size,
  149. enum dma_data_direction dir)
  150. {
  151. dma_sync_single_range_for_device(dev, handle, 0, size, dir);
  152. }
  153. static inline void
  154. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
  155. enum dma_data_direction dir)
  156. {
  157. BUG_ON(!valid_dma_direction(dir));
  158. }
  159. extern void
  160. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  161. int nents, enum dma_data_direction dir);
  162. static inline void
  163. dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  164. enum dma_data_direction dir)
  165. {
  166. _dma_sync((dma_addr_t)vaddr, size, dir);
  167. }
  168. #endif /* _BLACKFIN_DMA_MAPPING_H */