dma-mapping.h 8.2 KB

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