dma-mapping.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #ifndef _LINUX_DMA_MAPPING_H
  2. #define _LINUX_DMA_MAPPING_H
  3. #include <linux/string.h>
  4. #include <linux/device.h>
  5. #include <linux/err.h>
  6. #include <linux/dma-attrs.h>
  7. #include <linux/dma-direction.h>
  8. #include <linux/scatterlist.h>
  9. /*
  10. * A dma_addr_t can hold any valid DMA or bus address for the platform.
  11. * It can be given to a device to use as a DMA source or target. A CPU cannot
  12. * reference a dma_addr_t directly because there may be translation between
  13. * its physical address space and the bus address space.
  14. */
  15. struct dma_map_ops {
  16. void* (*alloc)(struct device *dev, size_t size,
  17. dma_addr_t *dma_handle, gfp_t gfp,
  18. struct dma_attrs *attrs);
  19. void (*free)(struct device *dev, size_t size,
  20. void *vaddr, dma_addr_t dma_handle,
  21. struct dma_attrs *attrs);
  22. int (*mmap)(struct device *, struct vm_area_struct *,
  23. void *, dma_addr_t, size_t, struct dma_attrs *attrs);
  24. int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
  25. dma_addr_t, size_t, struct dma_attrs *attrs);
  26. dma_addr_t (*map_page)(struct device *dev, struct page *page,
  27. unsigned long offset, size_t size,
  28. enum dma_data_direction dir,
  29. struct dma_attrs *attrs);
  30. void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
  31. size_t size, enum dma_data_direction dir,
  32. struct dma_attrs *attrs);
  33. int (*map_sg)(struct device *dev, struct scatterlist *sg,
  34. int nents, enum dma_data_direction dir,
  35. struct dma_attrs *attrs);
  36. void (*unmap_sg)(struct device *dev,
  37. struct scatterlist *sg, int nents,
  38. enum dma_data_direction dir,
  39. struct dma_attrs *attrs);
  40. void (*sync_single_for_cpu)(struct device *dev,
  41. dma_addr_t dma_handle, size_t size,
  42. enum dma_data_direction dir);
  43. void (*sync_single_for_device)(struct device *dev,
  44. dma_addr_t dma_handle, size_t size,
  45. enum dma_data_direction dir);
  46. void (*sync_sg_for_cpu)(struct device *dev,
  47. struct scatterlist *sg, int nents,
  48. enum dma_data_direction dir);
  49. void (*sync_sg_for_device)(struct device *dev,
  50. struct scatterlist *sg, int nents,
  51. enum dma_data_direction dir);
  52. int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
  53. int (*dma_supported)(struct device *dev, u64 mask);
  54. int (*set_dma_mask)(struct device *dev, u64 mask);
  55. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  56. u64 (*get_required_mask)(struct device *dev);
  57. #endif
  58. int is_phys;
  59. };
  60. #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
  61. #define DMA_MASK_NONE 0x0ULL
  62. static inline int valid_dma_direction(int dma_direction)
  63. {
  64. return ((dma_direction == DMA_BIDIRECTIONAL) ||
  65. (dma_direction == DMA_TO_DEVICE) ||
  66. (dma_direction == DMA_FROM_DEVICE));
  67. }
  68. static inline int is_device_dma_capable(struct device *dev)
  69. {
  70. return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
  71. }
  72. #ifdef CONFIG_HAS_DMA
  73. #include <asm/dma-mapping.h>
  74. #else
  75. #include <asm-generic/dma-mapping-broken.h>
  76. #endif
  77. static inline u64 dma_get_mask(struct device *dev)
  78. {
  79. if (dev && dev->dma_mask && *dev->dma_mask)
  80. return *dev->dma_mask;
  81. return DMA_BIT_MASK(32);
  82. }
  83. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  84. int dma_set_coherent_mask(struct device *dev, u64 mask);
  85. #else
  86. static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
  87. {
  88. if (!dma_supported(dev, mask))
  89. return -EIO;
  90. dev->coherent_dma_mask = mask;
  91. return 0;
  92. }
  93. #endif
  94. /*
  95. * Set both the DMA mask and the coherent DMA mask to the same thing.
  96. * Note that we don't check the return value from dma_set_coherent_mask()
  97. * as the DMA API guarantees that the coherent DMA mask can be set to
  98. * the same or smaller than the streaming DMA mask.
  99. */
  100. static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
  101. {
  102. int rc = dma_set_mask(dev, mask);
  103. if (rc == 0)
  104. dma_set_coherent_mask(dev, mask);
  105. return rc;
  106. }
  107. /*
  108. * Similar to the above, except it deals with the case where the device
  109. * does not have dev->dma_mask appropriately setup.
  110. */
  111. static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
  112. {
  113. dev->dma_mask = &dev->coherent_dma_mask;
  114. return dma_set_mask_and_coherent(dev, mask);
  115. }
  116. extern u64 dma_get_required_mask(struct device *dev);
  117. #ifndef set_arch_dma_coherent_ops
  118. static inline int set_arch_dma_coherent_ops(struct device *dev)
  119. {
  120. return 0;
  121. }
  122. #endif
  123. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  124. {
  125. return dev->dma_parms ? dev->dma_parms->max_segment_size : 65536;
  126. }
  127. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  128. unsigned int size)
  129. {
  130. if (dev->dma_parms) {
  131. dev->dma_parms->max_segment_size = size;
  132. return 0;
  133. } else
  134. return -EIO;
  135. }
  136. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  137. {
  138. return dev->dma_parms ?
  139. dev->dma_parms->segment_boundary_mask : 0xffffffff;
  140. }
  141. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  142. {
  143. if (dev->dma_parms) {
  144. dev->dma_parms->segment_boundary_mask = mask;
  145. return 0;
  146. } else
  147. return -EIO;
  148. }
  149. #ifndef dma_max_pfn
  150. static inline unsigned long dma_max_pfn(struct device *dev)
  151. {
  152. return *dev->dma_mask >> PAGE_SHIFT;
  153. }
  154. #endif
  155. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  156. dma_addr_t *dma_handle, gfp_t flag)
  157. {
  158. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  159. flag | __GFP_ZERO);
  160. return ret;
  161. }
  162. #ifdef CONFIG_HAS_DMA
  163. static inline int dma_get_cache_alignment(void)
  164. {
  165. #ifdef ARCH_DMA_MINALIGN
  166. return ARCH_DMA_MINALIGN;
  167. #endif
  168. return 1;
  169. }
  170. #endif
  171. /* flags for the coherent memory api */
  172. #define DMA_MEMORY_MAP 0x01
  173. #define DMA_MEMORY_IO 0x02
  174. #define DMA_MEMORY_INCLUDES_CHILDREN 0x04
  175. #define DMA_MEMORY_EXCLUSIVE 0x08
  176. #ifndef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  177. static inline int
  178. dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  179. dma_addr_t device_addr, size_t size, int flags)
  180. {
  181. return 0;
  182. }
  183. static inline void
  184. dma_release_declared_memory(struct device *dev)
  185. {
  186. }
  187. static inline void *
  188. dma_mark_declared_memory_occupied(struct device *dev,
  189. dma_addr_t device_addr, size_t size)
  190. {
  191. return ERR_PTR(-EBUSY);
  192. }
  193. #endif
  194. /*
  195. * Managed DMA API
  196. */
  197. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  198. dma_addr_t *dma_handle, gfp_t gfp);
  199. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  200. dma_addr_t dma_handle);
  201. extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  202. dma_addr_t *dma_handle, gfp_t gfp);
  203. extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  204. dma_addr_t dma_handle);
  205. #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  206. extern int dmam_declare_coherent_memory(struct device *dev,
  207. phys_addr_t phys_addr,
  208. dma_addr_t device_addr, size_t size,
  209. int flags);
  210. extern void dmam_release_declared_memory(struct device *dev);
  211. #else /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  212. static inline int dmam_declare_coherent_memory(struct device *dev,
  213. phys_addr_t phys_addr, dma_addr_t device_addr,
  214. size_t size, gfp_t gfp)
  215. {
  216. return 0;
  217. }
  218. static inline void dmam_release_declared_memory(struct device *dev)
  219. {
  220. }
  221. #endif /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  222. #ifndef CONFIG_HAVE_DMA_ATTRS
  223. struct dma_attrs;
  224. #define dma_map_single_attrs(dev, cpu_addr, size, dir, attrs) \
  225. dma_map_single(dev, cpu_addr, size, dir)
  226. #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
  227. dma_unmap_single(dev, dma_addr, size, dir)
  228. #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
  229. dma_map_sg(dev, sgl, nents, dir)
  230. #define dma_unmap_sg_attrs(dev, sgl, nents, dir, attrs) \
  231. dma_unmap_sg(dev, sgl, nents, dir)
  232. #else
  233. static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
  234. dma_addr_t *dma_addr, gfp_t gfp)
  235. {
  236. DEFINE_DMA_ATTRS(attrs);
  237. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  238. return dma_alloc_attrs(dev, size, dma_addr, gfp, &attrs);
  239. }
  240. static inline void dma_free_writecombine(struct device *dev, size_t size,
  241. void *cpu_addr, dma_addr_t dma_addr)
  242. {
  243. DEFINE_DMA_ATTRS(attrs);
  244. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  245. return dma_free_attrs(dev, size, cpu_addr, dma_addr, &attrs);
  246. }
  247. static inline int dma_mmap_writecombine(struct device *dev,
  248. struct vm_area_struct *vma,
  249. void *cpu_addr, dma_addr_t dma_addr,
  250. size_t size)
  251. {
  252. DEFINE_DMA_ATTRS(attrs);
  253. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  254. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs);
  255. }
  256. #endif /* CONFIG_HAVE_DMA_ATTRS */
  257. #ifdef CONFIG_NEED_DMA_MAP_STATE
  258. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  259. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  260. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  261. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  262. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  263. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  264. #else
  265. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  266. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  267. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  268. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  269. #define dma_unmap_len(PTR, LEN_NAME) (0)
  270. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  271. #endif
  272. #endif