dma-mapping-common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #ifndef _ASM_GENERIC_DMA_MAPPING_H
  2. #define _ASM_GENERIC_DMA_MAPPING_H
  3. #include <linux/kmemcheck.h>
  4. #include <linux/bug.h>
  5. #include <linux/scatterlist.h>
  6. #include <linux/dma-debug.h>
  7. #include <linux/dma-attrs.h>
  8. #include <asm-generic/dma-coherent.h>
  9. static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
  10. size_t size,
  11. enum dma_data_direction dir,
  12. struct dma_attrs *attrs)
  13. {
  14. struct dma_map_ops *ops = get_dma_ops(dev);
  15. dma_addr_t addr;
  16. kmemcheck_mark_initialized(ptr, size);
  17. BUG_ON(!valid_dma_direction(dir));
  18. addr = ops->map_page(dev, virt_to_page(ptr),
  19. (unsigned long)ptr & ~PAGE_MASK, size,
  20. dir, attrs);
  21. debug_dma_map_page(dev, virt_to_page(ptr),
  22. (unsigned long)ptr & ~PAGE_MASK, size,
  23. dir, addr, true);
  24. return addr;
  25. }
  26. static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
  27. size_t size,
  28. enum dma_data_direction dir,
  29. struct dma_attrs *attrs)
  30. {
  31. struct dma_map_ops *ops = get_dma_ops(dev);
  32. BUG_ON(!valid_dma_direction(dir));
  33. if (ops->unmap_page)
  34. ops->unmap_page(dev, addr, size, dir, attrs);
  35. debug_dma_unmap_page(dev, addr, size, dir, true);
  36. }
  37. /*
  38. * dma_maps_sg_attrs returns 0 on error and > 0 on success.
  39. * It should never return a value < 0.
  40. */
  41. static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  42. int nents, enum dma_data_direction dir,
  43. struct dma_attrs *attrs)
  44. {
  45. struct dma_map_ops *ops = get_dma_ops(dev);
  46. int i, ents;
  47. struct scatterlist *s;
  48. for_each_sg(sg, s, nents, i)
  49. kmemcheck_mark_initialized(sg_virt(s), s->length);
  50. BUG_ON(!valid_dma_direction(dir));
  51. ents = ops->map_sg(dev, sg, nents, dir, attrs);
  52. BUG_ON(ents < 0);
  53. debug_dma_map_sg(dev, sg, nents, ents, dir);
  54. return ents;
  55. }
  56. static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  57. int nents, enum dma_data_direction dir,
  58. struct dma_attrs *attrs)
  59. {
  60. struct dma_map_ops *ops = get_dma_ops(dev);
  61. BUG_ON(!valid_dma_direction(dir));
  62. debug_dma_unmap_sg(dev, sg, nents, dir);
  63. if (ops->unmap_sg)
  64. ops->unmap_sg(dev, sg, nents, dir, attrs);
  65. }
  66. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  67. size_t offset, size_t size,
  68. enum dma_data_direction dir)
  69. {
  70. struct dma_map_ops *ops = get_dma_ops(dev);
  71. dma_addr_t addr;
  72. kmemcheck_mark_initialized(page_address(page) + offset, size);
  73. BUG_ON(!valid_dma_direction(dir));
  74. addr = ops->map_page(dev, page, offset, size, dir, NULL);
  75. debug_dma_map_page(dev, page, offset, size, dir, addr, false);
  76. return addr;
  77. }
  78. static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
  79. size_t size, enum dma_data_direction dir)
  80. {
  81. struct dma_map_ops *ops = get_dma_ops(dev);
  82. BUG_ON(!valid_dma_direction(dir));
  83. if (ops->unmap_page)
  84. ops->unmap_page(dev, addr, size, dir, NULL);
  85. debug_dma_unmap_page(dev, addr, size, dir, false);
  86. }
  87. static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  88. size_t size,
  89. enum dma_data_direction dir)
  90. {
  91. struct dma_map_ops *ops = get_dma_ops(dev);
  92. BUG_ON(!valid_dma_direction(dir));
  93. if (ops->sync_single_for_cpu)
  94. ops->sync_single_for_cpu(dev, addr, size, dir);
  95. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  96. }
  97. static inline void dma_sync_single_for_device(struct device *dev,
  98. dma_addr_t addr, size_t size,
  99. enum dma_data_direction dir)
  100. {
  101. struct dma_map_ops *ops = get_dma_ops(dev);
  102. BUG_ON(!valid_dma_direction(dir));
  103. if (ops->sync_single_for_device)
  104. ops->sync_single_for_device(dev, addr, size, dir);
  105. debug_dma_sync_single_for_device(dev, addr, size, dir);
  106. }
  107. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  108. dma_addr_t addr,
  109. unsigned long offset,
  110. size_t size,
  111. enum dma_data_direction dir)
  112. {
  113. const struct dma_map_ops *ops = get_dma_ops(dev);
  114. BUG_ON(!valid_dma_direction(dir));
  115. if (ops->sync_single_for_cpu)
  116. ops->sync_single_for_cpu(dev, addr + offset, size, dir);
  117. debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
  118. }
  119. static inline void dma_sync_single_range_for_device(struct device *dev,
  120. dma_addr_t addr,
  121. unsigned long offset,
  122. size_t size,
  123. enum dma_data_direction dir)
  124. {
  125. const struct dma_map_ops *ops = get_dma_ops(dev);
  126. BUG_ON(!valid_dma_direction(dir));
  127. if (ops->sync_single_for_device)
  128. ops->sync_single_for_device(dev, addr + offset, size, dir);
  129. debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
  130. }
  131. static inline void
  132. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  133. int nelems, enum dma_data_direction dir)
  134. {
  135. struct dma_map_ops *ops = get_dma_ops(dev);
  136. BUG_ON(!valid_dma_direction(dir));
  137. if (ops->sync_sg_for_cpu)
  138. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  139. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  140. }
  141. static inline void
  142. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  143. int nelems, enum dma_data_direction dir)
  144. {
  145. struct dma_map_ops *ops = get_dma_ops(dev);
  146. BUG_ON(!valid_dma_direction(dir));
  147. if (ops->sync_sg_for_device)
  148. ops->sync_sg_for_device(dev, sg, nelems, dir);
  149. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  150. }
  151. #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
  152. #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL)
  153. #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL)
  154. #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL)
  155. extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  156. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  157. void *dma_common_contiguous_remap(struct page *page, size_t size,
  158. unsigned long vm_flags,
  159. pgprot_t prot, const void *caller);
  160. void *dma_common_pages_remap(struct page **pages, size_t size,
  161. unsigned long vm_flags, pgprot_t prot,
  162. const void *caller);
  163. void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
  164. /**
  165. * dma_mmap_attrs - map a coherent DMA allocation into user space
  166. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  167. * @vma: vm_area_struct describing requested user mapping
  168. * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
  169. * @handle: device-view address returned from dma_alloc_attrs
  170. * @size: size of memory originally requested in dma_alloc_attrs
  171. * @attrs: attributes of mapping properties requested in dma_alloc_attrs
  172. *
  173. * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
  174. * into user space. The coherent DMA buffer must not be freed by the
  175. * driver until the user space mapping has been released.
  176. */
  177. static inline int
  178. dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
  179. dma_addr_t dma_addr, size_t size, struct dma_attrs *attrs)
  180. {
  181. struct dma_map_ops *ops = get_dma_ops(dev);
  182. BUG_ON(!ops);
  183. if (ops->mmap)
  184. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  185. return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
  186. }
  187. #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, NULL)
  188. int
  189. dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  190. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  191. static inline int
  192. dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
  193. dma_addr_t dma_addr, size_t size, struct dma_attrs *attrs)
  194. {
  195. struct dma_map_ops *ops = get_dma_ops(dev);
  196. BUG_ON(!ops);
  197. if (ops->get_sgtable)
  198. return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
  199. attrs);
  200. return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  201. }
  202. #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, NULL)
  203. #ifndef arch_dma_alloc_attrs
  204. #define arch_dma_alloc_attrs(dev, flag) (true)
  205. #endif
  206. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  207. dma_addr_t *dma_handle, gfp_t flag,
  208. struct dma_attrs *attrs)
  209. {
  210. struct dma_map_ops *ops = get_dma_ops(dev);
  211. void *cpu_addr;
  212. BUG_ON(!ops);
  213. if (dma_alloc_from_coherent(dev, size, dma_handle, &cpu_addr))
  214. return cpu_addr;
  215. if (!arch_dma_alloc_attrs(&dev, &flag))
  216. return NULL;
  217. if (!ops->alloc)
  218. return NULL;
  219. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  220. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  221. return cpu_addr;
  222. }
  223. static inline void dma_free_attrs(struct device *dev, size_t size,
  224. void *cpu_addr, dma_addr_t dma_handle,
  225. struct dma_attrs *attrs)
  226. {
  227. struct dma_map_ops *ops = get_dma_ops(dev);
  228. BUG_ON(!ops);
  229. WARN_ON(irqs_disabled());
  230. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  231. return;
  232. if (!ops->free)
  233. return;
  234. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  235. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  236. }
  237. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  238. dma_addr_t *dma_handle, gfp_t flag)
  239. {
  240. return dma_alloc_attrs(dev, size, dma_handle, flag, NULL);
  241. }
  242. static inline void dma_free_coherent(struct device *dev, size_t size,
  243. void *cpu_addr, dma_addr_t dma_handle)
  244. {
  245. return dma_free_attrs(dev, size, cpu_addr, dma_handle, NULL);
  246. }
  247. static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
  248. dma_addr_t *dma_handle, gfp_t gfp)
  249. {
  250. DEFINE_DMA_ATTRS(attrs);
  251. dma_set_attr(DMA_ATTR_NON_CONSISTENT, &attrs);
  252. return dma_alloc_attrs(dev, size, dma_handle, gfp, &attrs);
  253. }
  254. static inline void dma_free_noncoherent(struct device *dev, size_t size,
  255. void *cpu_addr, dma_addr_t dma_handle)
  256. {
  257. DEFINE_DMA_ATTRS(attrs);
  258. dma_set_attr(DMA_ATTR_NON_CONSISTENT, &attrs);
  259. dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs);
  260. }
  261. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  262. {
  263. debug_dma_mapping_error(dev, dma_addr);
  264. if (get_dma_ops(dev)->mapping_error)
  265. return get_dma_ops(dev)->mapping_error(dev, dma_addr);
  266. #ifdef DMA_ERROR_CODE
  267. return dma_addr == DMA_ERROR_CODE;
  268. #else
  269. return 0;
  270. #endif
  271. }
  272. #ifndef HAVE_ARCH_DMA_SUPPORTED
  273. static inline int dma_supported(struct device *dev, u64 mask)
  274. {
  275. struct dma_map_ops *ops = get_dma_ops(dev);
  276. if (!ops)
  277. return 0;
  278. if (!ops->dma_supported)
  279. return 1;
  280. return ops->dma_supported(dev, mask);
  281. }
  282. #endif
  283. #ifndef HAVE_ARCH_DMA_SET_MASK
  284. static inline int dma_set_mask(struct device *dev, u64 mask)
  285. {
  286. struct dma_map_ops *ops = get_dma_ops(dev);
  287. if (ops->set_dma_mask)
  288. return ops->set_dma_mask(dev, mask);
  289. if (!dev->dma_mask || !dma_supported(dev, mask))
  290. return -EIO;
  291. *dev->dma_mask = mask;
  292. return 0;
  293. }
  294. #endif
  295. #endif