dma-mapping.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. extern dma_addr_t bad_dma_address;
  11. extern int iommu_merge;
  12. extern struct device fallback_dev;
  13. extern int panic_on_overflow;
  14. extern int force_iommu;
  15. struct dma_mapping_ops {
  16. int (*mapping_error)(struct device *dev,
  17. dma_addr_t dma_addr);
  18. void* (*alloc_coherent)(struct device *dev, size_t size,
  19. dma_addr_t *dma_handle, gfp_t gfp);
  20. void (*free_coherent)(struct device *dev, size_t size,
  21. void *vaddr, dma_addr_t dma_handle);
  22. dma_addr_t (*map_single)(struct device *hwdev, phys_addr_t ptr,
  23. size_t size, int direction);
  24. /* like map_single, but doesn't check the device mask */
  25. dma_addr_t (*map_simple)(struct device *hwdev, phys_addr_t ptr,
  26. size_t size, int direction);
  27. void (*unmap_single)(struct device *dev, dma_addr_t addr,
  28. size_t size, int direction);
  29. void (*sync_single_for_cpu)(struct device *hwdev,
  30. dma_addr_t dma_handle, size_t size,
  31. int direction);
  32. void (*sync_single_for_device)(struct device *hwdev,
  33. dma_addr_t dma_handle, size_t size,
  34. int direction);
  35. void (*sync_single_range_for_cpu)(struct device *hwdev,
  36. dma_addr_t dma_handle, unsigned long offset,
  37. size_t size, int direction);
  38. void (*sync_single_range_for_device)(struct device *hwdev,
  39. dma_addr_t dma_handle, unsigned long offset,
  40. size_t size, int direction);
  41. void (*sync_sg_for_cpu)(struct device *hwdev,
  42. struct scatterlist *sg, int nelems,
  43. int direction);
  44. void (*sync_sg_for_device)(struct device *hwdev,
  45. struct scatterlist *sg, int nelems,
  46. int direction);
  47. int (*map_sg)(struct device *hwdev, struct scatterlist *sg,
  48. int nents, int direction);
  49. void (*unmap_sg)(struct device *hwdev,
  50. struct scatterlist *sg, int nents,
  51. int direction);
  52. int (*dma_supported)(struct device *hwdev, u64 mask);
  53. int is_phys;
  54. };
  55. extern struct dma_mapping_ops *dma_ops;
  56. static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
  57. {
  58. #ifdef CONFIG_X86_32
  59. return dma_ops;
  60. #else
  61. if (unlikely(!dev) || !dev->archdata.dma_ops)
  62. return dma_ops;
  63. else
  64. return dev->archdata.dma_ops;
  65. #endif
  66. }
  67. /* Make sure we keep the same behaviour */
  68. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  69. {
  70. #ifdef CONFIG_X86_32
  71. return 0;
  72. #else
  73. struct dma_mapping_ops *ops = get_dma_ops(dev);
  74. if (ops->mapping_error)
  75. return ops->mapping_error(dev, dma_addr);
  76. return (dma_addr == bad_dma_address);
  77. #endif
  78. }
  79. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  80. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  81. void *dma_alloc_coherent(struct device *dev, size_t size,
  82. dma_addr_t *dma_handle, gfp_t flag);
  83. void dma_free_coherent(struct device *dev, size_t size,
  84. void *vaddr, dma_addr_t dma_handle);
  85. extern int dma_supported(struct device *hwdev, u64 mask);
  86. extern int dma_set_mask(struct device *dev, u64 mask);
  87. static inline dma_addr_t
  88. dma_map_single(struct device *hwdev, void *ptr, size_t size,
  89. int direction)
  90. {
  91. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  92. BUG_ON(!valid_dma_direction(direction));
  93. return ops->map_single(hwdev, virt_to_phys(ptr), size, direction);
  94. }
  95. static inline void
  96. dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
  97. int direction)
  98. {
  99. struct dma_mapping_ops *ops = get_dma_ops(dev);
  100. BUG_ON(!valid_dma_direction(direction));
  101. if (ops->unmap_single)
  102. ops->unmap_single(dev, addr, size, direction);
  103. }
  104. static inline int
  105. dma_map_sg(struct device *hwdev, struct scatterlist *sg,
  106. int nents, int direction)
  107. {
  108. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  109. BUG_ON(!valid_dma_direction(direction));
  110. return ops->map_sg(hwdev, sg, nents, direction);
  111. }
  112. static inline void
  113. dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
  114. int direction)
  115. {
  116. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  117. BUG_ON(!valid_dma_direction(direction));
  118. if (ops->unmap_sg)
  119. ops->unmap_sg(hwdev, sg, nents, direction);
  120. }
  121. static inline void
  122. dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  123. size_t size, int direction)
  124. {
  125. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  126. BUG_ON(!valid_dma_direction(direction));
  127. if (ops->sync_single_for_cpu)
  128. ops->sync_single_for_cpu(hwdev, dma_handle, size, direction);
  129. flush_write_buffers();
  130. }
  131. static inline void
  132. dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
  133. size_t size, int direction)
  134. {
  135. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  136. BUG_ON(!valid_dma_direction(direction));
  137. if (ops->sync_single_for_device)
  138. ops->sync_single_for_device(hwdev, dma_handle, size, direction);
  139. flush_write_buffers();
  140. }
  141. static inline void
  142. dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  143. unsigned long offset, size_t size, int direction)
  144. {
  145. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  146. BUG_ON(!valid_dma_direction(direction));
  147. if (ops->sync_single_range_for_cpu)
  148. ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
  149. size, direction);
  150. flush_write_buffers();
  151. }
  152. static inline void
  153. dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
  154. unsigned long offset, size_t size,
  155. int direction)
  156. {
  157. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  158. BUG_ON(!valid_dma_direction(direction));
  159. if (ops->sync_single_range_for_device)
  160. ops->sync_single_range_for_device(hwdev, dma_handle,
  161. offset, size, direction);
  162. flush_write_buffers();
  163. }
  164. static inline void
  165. dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
  166. int nelems, int direction)
  167. {
  168. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  169. BUG_ON(!valid_dma_direction(direction));
  170. if (ops->sync_sg_for_cpu)
  171. ops->sync_sg_for_cpu(hwdev, sg, nelems, direction);
  172. flush_write_buffers();
  173. }
  174. static inline void
  175. dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
  176. int nelems, int direction)
  177. {
  178. struct dma_mapping_ops *ops = get_dma_ops(hwdev);
  179. BUG_ON(!valid_dma_direction(direction));
  180. if (ops->sync_sg_for_device)
  181. ops->sync_sg_for_device(hwdev, sg, nelems, direction);
  182. flush_write_buffers();
  183. }
  184. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  185. size_t offset, size_t size,
  186. int direction)
  187. {
  188. struct dma_mapping_ops *ops = get_dma_ops(dev);
  189. BUG_ON(!valid_dma_direction(direction));
  190. return ops->map_single(dev, page_to_phys(page) + offset,
  191. size, direction);
  192. }
  193. static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
  194. size_t size, int direction)
  195. {
  196. dma_unmap_single(dev, addr, size, direction);
  197. }
  198. static inline void
  199. dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  200. enum dma_data_direction dir)
  201. {
  202. flush_write_buffers();
  203. }
  204. static inline int dma_get_cache_alignment(void)
  205. {
  206. /* no easy way to get cache size on all x86, so return the
  207. * maximum possible, to be safe */
  208. return boot_cpu_data.x86_clflush_size;
  209. }
  210. #define dma_is_consistent(d, h) (1)
  211. #include <asm-generic/dma-coherent.h>
  212. #endif