dma-mapping.h 7.7 KB

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