dma-mapping.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef _ASM_DMA_MAPPING_H_
  2. #define _ASM_DMA_MAPPING_H_
  3. /*
  4. * IOMMU interface. See Documentation/DMA-mapping.txt and DMA-API.txt for
  5. * documentation.
  6. */
  7. #include <linux/scatterlist.h>
  8. #include <asm/io.h>
  9. #include <asm/swiotlb.h>
  10. struct dma_mapping_ops {
  11. int (*mapping_error)(dma_addr_t dma_addr);
  12. void* (*alloc_coherent)(struct device *dev, size_t size,
  13. dma_addr_t *dma_handle, gfp_t gfp);
  14. void (*free_coherent)(struct device *dev, size_t size,
  15. void *vaddr, dma_addr_t dma_handle);
  16. dma_addr_t (*map_single)(struct device *hwdev, phys_addr_t ptr,
  17. size_t size, int direction);
  18. /* like map_single, but doesn't check the device mask */
  19. dma_addr_t (*map_simple)(struct device *hwdev, phys_addr_t ptr,
  20. size_t size, int direction);
  21. void (*unmap_single)(struct device *dev, dma_addr_t addr,
  22. size_t size, int direction);
  23. void (*sync_single_for_cpu)(struct device *hwdev,
  24. dma_addr_t dma_handle, size_t size,
  25. int direction);
  26. void (*sync_single_for_device)(struct device *hwdev,
  27. dma_addr_t dma_handle, size_t size,
  28. int direction);
  29. void (*sync_single_range_for_cpu)(struct device *hwdev,
  30. dma_addr_t dma_handle, unsigned long offset,
  31. size_t size, int direction);
  32. void (*sync_single_range_for_device)(struct device *hwdev,
  33. dma_addr_t dma_handle, unsigned long offset,
  34. size_t size, int direction);
  35. void (*sync_sg_for_cpu)(struct device *hwdev,
  36. struct scatterlist *sg, int nelems,
  37. int direction);
  38. void (*sync_sg_for_device)(struct device *hwdev,
  39. struct scatterlist *sg, int nelems,
  40. int direction);
  41. int (*map_sg)(struct device *hwdev, struct scatterlist *sg,
  42. int nents, int direction);
  43. void (*unmap_sg)(struct device *hwdev,
  44. struct scatterlist *sg, int nents,
  45. int direction);
  46. int (*dma_supported)(struct device *hwdev, u64 mask);
  47. int is_phys;
  48. };
  49. extern const struct dma_mapping_ops *dma_ops;
  50. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  51. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  52. void *dma_alloc_coherent(struct device *dev, size_t size,
  53. dma_addr_t *dma_handle, gfp_t flag);
  54. void dma_free_coherent(struct device *dev, size_t size,
  55. void *vaddr, dma_addr_t dma_handle);
  56. extern int dma_supported(struct device *hwdev, u64 mask);
  57. extern int dma_set_mask(struct device *dev, u64 mask);
  58. #ifdef CONFIG_X86_32
  59. # include "dma-mapping_32.h"
  60. #else
  61. # include "dma-mapping_64.h"
  62. #endif
  63. static inline dma_addr_t
  64. dma_map_single(struct device *hwdev, void *ptr, size_t size,
  65. int direction)
  66. {
  67. BUG_ON(!valid_dma_direction(direction));
  68. return dma_ops->map_single(hwdev, virt_to_phys(ptr), size, direction);
  69. }
  70. static inline void
  71. dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
  72. int direction)
  73. {
  74. BUG_ON(!valid_dma_direction(direction));
  75. if (dma_ops->unmap_single)
  76. dma_ops->unmap_single(dev, addr, size, direction);
  77. }
  78. static inline int
  79. dma_map_sg(struct device *hwdev, struct scatterlist *sg,
  80. int nents, int direction)
  81. {
  82. BUG_ON(!valid_dma_direction(direction));
  83. return dma_ops->map_sg(hwdev, sg, nents, direction);
  84. }
  85. static inline void
  86. dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
  87. int direction)
  88. {
  89. BUG_ON(!valid_dma_direction(direction));
  90. if (dma_ops->unmap_sg)
  91. dma_ops->unmap_sg(hwdev, sg, nents, direction);
  92. }
  93. static inline void
  94. dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  95. size_t size, int direction)
  96. {
  97. BUG_ON(!valid_dma_direction(direction));
  98. if (dma_ops->sync_single_for_cpu)
  99. dma_ops->sync_single_for_cpu(hwdev, dma_handle, size,
  100. direction);
  101. flush_write_buffers();
  102. }
  103. static inline void
  104. dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
  105. size_t size, int direction)
  106. {
  107. BUG_ON(!valid_dma_direction(direction));
  108. if (dma_ops->sync_single_for_device)
  109. dma_ops->sync_single_for_device(hwdev, dma_handle, size,
  110. direction);
  111. flush_write_buffers();
  112. }
  113. static inline void
  114. dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  115. unsigned long offset, size_t size, int direction)
  116. {
  117. BUG_ON(!valid_dma_direction(direction));
  118. if (dma_ops->sync_single_range_for_cpu)
  119. dma_ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
  120. size, direction);
  121. flush_write_buffers();
  122. }
  123. static inline void
  124. dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
  125. unsigned long offset, size_t size,
  126. int direction)
  127. {
  128. BUG_ON(!valid_dma_direction(direction));
  129. if (dma_ops->sync_single_range_for_device)
  130. dma_ops->sync_single_range_for_device(hwdev, dma_handle,
  131. offset, size, direction);
  132. flush_write_buffers();
  133. }
  134. static inline void
  135. dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
  136. int nelems, int direction)
  137. {
  138. BUG_ON(!valid_dma_direction(direction));
  139. if (dma_ops->sync_sg_for_cpu)
  140. dma_ops->sync_sg_for_cpu(hwdev, sg, nelems, direction);
  141. flush_write_buffers();
  142. }
  143. static inline void
  144. dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
  145. int nelems, int direction)
  146. {
  147. BUG_ON(!valid_dma_direction(direction));
  148. if (dma_ops->sync_sg_for_device)
  149. dma_ops->sync_sg_for_device(hwdev, sg, nelems, direction);
  150. flush_write_buffers();
  151. }
  152. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  153. size_t offset, size_t size,
  154. int direction)
  155. {
  156. BUG_ON(!valid_dma_direction(direction));
  157. return dma_ops->map_single(dev, page_to_phys(page)+offset,
  158. size, direction);
  159. }
  160. static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
  161. size_t size, int direction)
  162. {
  163. dma_unmap_single(dev, addr, size, direction);
  164. }
  165. static inline void
  166. dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  167. enum dma_data_direction dir)
  168. {
  169. flush_write_buffers();
  170. }
  171. #endif