dma-mapping.h 8.4 KB

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