dma-mapping.h 8.4 KB

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