dma-mapping.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef dma_max_pfn
  138. static inline unsigned long dma_max_pfn(struct device *dev)
  139. {
  140. return *dev->dma_mask >> PAGE_SHIFT;
  141. }
  142. #endif
  143. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  144. dma_addr_t *dma_handle, gfp_t flag)
  145. {
  146. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  147. flag | __GFP_ZERO);
  148. return ret;
  149. }
  150. #ifdef CONFIG_HAS_DMA
  151. static inline int dma_get_cache_alignment(void)
  152. {
  153. #ifdef ARCH_DMA_MINALIGN
  154. return ARCH_DMA_MINALIGN;
  155. #endif
  156. return 1;
  157. }
  158. #endif
  159. /* flags for the coherent memory api */
  160. #define DMA_MEMORY_MAP 0x01
  161. #define DMA_MEMORY_IO 0x02
  162. #define DMA_MEMORY_INCLUDES_CHILDREN 0x04
  163. #define DMA_MEMORY_EXCLUSIVE 0x08
  164. #ifndef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  165. static inline int
  166. dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  167. dma_addr_t device_addr, size_t size, int flags)
  168. {
  169. return 0;
  170. }
  171. static inline void
  172. dma_release_declared_memory(struct device *dev)
  173. {
  174. }
  175. static inline void *
  176. dma_mark_declared_memory_occupied(struct device *dev,
  177. dma_addr_t device_addr, size_t size)
  178. {
  179. return ERR_PTR(-EBUSY);
  180. }
  181. #endif
  182. /*
  183. * Managed DMA API
  184. */
  185. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  186. dma_addr_t *dma_handle, gfp_t gfp);
  187. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  188. dma_addr_t dma_handle);
  189. extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  190. dma_addr_t *dma_handle, gfp_t gfp);
  191. extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  192. dma_addr_t dma_handle);
  193. #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  194. extern int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  195. dma_addr_t device_addr, size_t size,
  196. int flags);
  197. extern void dmam_release_declared_memory(struct device *dev);
  198. #else /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  199. static inline int dmam_declare_coherent_memory(struct device *dev,
  200. dma_addr_t bus_addr, dma_addr_t device_addr,
  201. size_t size, gfp_t gfp)
  202. {
  203. return 0;
  204. }
  205. static inline void dmam_release_declared_memory(struct device *dev)
  206. {
  207. }
  208. #endif /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  209. #ifndef CONFIG_HAVE_DMA_ATTRS
  210. struct dma_attrs;
  211. #define dma_map_single_attrs(dev, cpu_addr, size, dir, attrs) \
  212. dma_map_single(dev, cpu_addr, size, dir)
  213. #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
  214. dma_unmap_single(dev, dma_addr, size, dir)
  215. #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
  216. dma_map_sg(dev, sgl, nents, dir)
  217. #define dma_unmap_sg_attrs(dev, sgl, nents, dir, attrs) \
  218. dma_unmap_sg(dev, sgl, nents, dir)
  219. #endif /* CONFIG_HAVE_DMA_ATTRS */
  220. #ifdef CONFIG_NEED_DMA_MAP_STATE
  221. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  222. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  223. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  224. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  225. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  226. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  227. #else
  228. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  229. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  230. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  231. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  232. #define dma_unmap_len(PTR, LEN_NAME) (0)
  233. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  234. #endif
  235. #endif