dma-mapping.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 arch_setup_dma_ops
  118. static inline void arch_setup_dma_ops(struct device *dev, bool coherent) { }
  119. #endif
  120. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  121. {
  122. return dev->dma_parms ? dev->dma_parms->max_segment_size : 65536;
  123. }
  124. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  125. unsigned int size)
  126. {
  127. if (dev->dma_parms) {
  128. dev->dma_parms->max_segment_size = size;
  129. return 0;
  130. } else
  131. return -EIO;
  132. }
  133. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  134. {
  135. return dev->dma_parms ?
  136. dev->dma_parms->segment_boundary_mask : 0xffffffff;
  137. }
  138. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  139. {
  140. if (dev->dma_parms) {
  141. dev->dma_parms->segment_boundary_mask = mask;
  142. return 0;
  143. } else
  144. return -EIO;
  145. }
  146. #ifndef dma_max_pfn
  147. static inline unsigned long dma_max_pfn(struct device *dev)
  148. {
  149. return *dev->dma_mask >> PAGE_SHIFT;
  150. }
  151. #endif
  152. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  153. dma_addr_t *dma_handle, gfp_t flag)
  154. {
  155. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  156. flag | __GFP_ZERO);
  157. return ret;
  158. }
  159. #ifdef CONFIG_HAS_DMA
  160. static inline int dma_get_cache_alignment(void)
  161. {
  162. #ifdef ARCH_DMA_MINALIGN
  163. return ARCH_DMA_MINALIGN;
  164. #endif
  165. return 1;
  166. }
  167. #endif
  168. /* flags for the coherent memory api */
  169. #define DMA_MEMORY_MAP 0x01
  170. #define DMA_MEMORY_IO 0x02
  171. #define DMA_MEMORY_INCLUDES_CHILDREN 0x04
  172. #define DMA_MEMORY_EXCLUSIVE 0x08
  173. #ifndef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  174. static inline int
  175. dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  176. dma_addr_t device_addr, size_t size, int flags)
  177. {
  178. return 0;
  179. }
  180. static inline void
  181. dma_release_declared_memory(struct device *dev)
  182. {
  183. }
  184. static inline void *
  185. dma_mark_declared_memory_occupied(struct device *dev,
  186. dma_addr_t device_addr, size_t size)
  187. {
  188. return ERR_PTR(-EBUSY);
  189. }
  190. #endif
  191. /*
  192. * Managed DMA API
  193. */
  194. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  195. dma_addr_t *dma_handle, gfp_t gfp);
  196. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  197. dma_addr_t dma_handle);
  198. extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  199. dma_addr_t *dma_handle, gfp_t gfp);
  200. extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  201. dma_addr_t dma_handle);
  202. #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  203. extern int dmam_declare_coherent_memory(struct device *dev,
  204. phys_addr_t phys_addr,
  205. dma_addr_t device_addr, size_t size,
  206. int flags);
  207. extern void dmam_release_declared_memory(struct device *dev);
  208. #else /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  209. static inline int dmam_declare_coherent_memory(struct device *dev,
  210. phys_addr_t phys_addr, dma_addr_t device_addr,
  211. size_t size, gfp_t gfp)
  212. {
  213. return 0;
  214. }
  215. static inline void dmam_release_declared_memory(struct device *dev)
  216. {
  217. }
  218. #endif /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  219. #ifndef CONFIG_HAVE_DMA_ATTRS
  220. struct dma_attrs;
  221. #define dma_map_single_attrs(dev, cpu_addr, size, dir, attrs) \
  222. dma_map_single(dev, cpu_addr, size, dir)
  223. #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
  224. dma_unmap_single(dev, dma_addr, size, dir)
  225. #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
  226. dma_map_sg(dev, sgl, nents, dir)
  227. #define dma_unmap_sg_attrs(dev, sgl, nents, dir, attrs) \
  228. dma_unmap_sg(dev, sgl, nents, dir)
  229. #else
  230. static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
  231. dma_addr_t *dma_addr, gfp_t gfp)
  232. {
  233. DEFINE_DMA_ATTRS(attrs);
  234. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  235. return dma_alloc_attrs(dev, size, dma_addr, gfp, &attrs);
  236. }
  237. static inline void dma_free_writecombine(struct device *dev, size_t size,
  238. void *cpu_addr, dma_addr_t dma_addr)
  239. {
  240. DEFINE_DMA_ATTRS(attrs);
  241. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  242. return dma_free_attrs(dev, size, cpu_addr, dma_addr, &attrs);
  243. }
  244. static inline int dma_mmap_writecombine(struct device *dev,
  245. struct vm_area_struct *vma,
  246. void *cpu_addr, dma_addr_t dma_addr,
  247. size_t size)
  248. {
  249. DEFINE_DMA_ATTRS(attrs);
  250. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  251. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs);
  252. }
  253. #endif /* CONFIG_HAVE_DMA_ATTRS */
  254. #ifdef CONFIG_NEED_DMA_MAP_STATE
  255. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  256. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  257. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  258. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  259. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  260. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  261. #else
  262. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  263. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  264. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  265. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  266. #define dma_unmap_len(PTR, LEN_NAME) (0)
  267. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  268. #endif
  269. #endif