dma-mapping.h 7.6 KB

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