dma-mapping.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifndef _LINUX_DMA_MAPPING_H
  2. #define _LINUX_DMA_MAPPING_H
  3. #include <linux/device.h>
  4. #include <linux/err.h>
  5. #include <linux/dma-attrs.h>
  6. #include <linux/scatterlist.h>
  7. /* These definitions mirror those in pci.h, so they can be used
  8. * interchangeably with their PCI_ counterparts */
  9. enum dma_data_direction {
  10. DMA_BIDIRECTIONAL = 0,
  11. DMA_TO_DEVICE = 1,
  12. DMA_FROM_DEVICE = 2,
  13. DMA_NONE = 3,
  14. };
  15. struct dma_map_ops {
  16. void* (*alloc_coherent)(struct device *dev, size_t size,
  17. dma_addr_t *dma_handle, gfp_t gfp);
  18. void (*free_coherent)(struct device *dev, size_t size,
  19. void *vaddr, dma_addr_t dma_handle);
  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_single_range_for_cpu)(struct device *dev,
  41. dma_addr_t dma_handle,
  42. unsigned long offset,
  43. size_t size,
  44. enum dma_data_direction dir);
  45. void (*sync_single_range_for_device)(struct device *dev,
  46. dma_addr_t dma_handle,
  47. unsigned long offset,
  48. size_t size,
  49. enum dma_data_direction dir);
  50. void (*sync_sg_for_cpu)(struct device *dev,
  51. struct scatterlist *sg, int nents,
  52. enum dma_data_direction dir);
  53. void (*sync_sg_for_device)(struct device *dev,
  54. struct scatterlist *sg, int nents,
  55. enum dma_data_direction dir);
  56. int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
  57. int (*dma_supported)(struct device *dev, u64 mask);
  58. int (*set_dma_mask)(struct device *dev, u64 mask);
  59. int is_phys;
  60. };
  61. #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
  62. typedef u64 DMA_nnBIT_MASK __deprecated;
  63. /*
  64. * NOTE: do not use the below macros in new code and do not add new definitions
  65. * here.
  66. *
  67. * Instead, just open-code DMA_BIT_MASK(n) within your driver
  68. */
  69. #define DMA_64BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(64)
  70. #define DMA_48BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(48)
  71. #define DMA_47BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(47)
  72. #define DMA_40BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(40)
  73. #define DMA_39BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(39)
  74. #define DMA_35BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(35)
  75. #define DMA_32BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(32)
  76. #define DMA_31BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(31)
  77. #define DMA_30BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(30)
  78. #define DMA_29BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(29)
  79. #define DMA_28BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(28)
  80. #define DMA_24BIT_MASK (DMA_nnBIT_MASK)DMA_BIT_MASK(24)
  81. #define DMA_MASK_NONE 0x0ULL
  82. static inline int valid_dma_direction(int dma_direction)
  83. {
  84. return ((dma_direction == DMA_BIDIRECTIONAL) ||
  85. (dma_direction == DMA_TO_DEVICE) ||
  86. (dma_direction == DMA_FROM_DEVICE));
  87. }
  88. static inline int is_device_dma_capable(struct device *dev)
  89. {
  90. return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
  91. }
  92. #ifdef CONFIG_HAS_DMA
  93. #include <asm/dma-mapping.h>
  94. #else
  95. #include <asm-generic/dma-mapping-broken.h>
  96. #endif
  97. /* for backwards compatibility, removed soon */
  98. static inline void __deprecated dma_sync_single(struct device *dev,
  99. dma_addr_t addr, size_t size,
  100. enum dma_data_direction dir)
  101. {
  102. dma_sync_single_for_cpu(dev, addr, size, dir);
  103. }
  104. static inline void __deprecated dma_sync_sg(struct device *dev,
  105. struct scatterlist *sg, int nelems,
  106. enum dma_data_direction dir)
  107. {
  108. dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  109. }
  110. static inline u64 dma_get_mask(struct device *dev)
  111. {
  112. if (dev && dev->dma_mask && *dev->dma_mask)
  113. return *dev->dma_mask;
  114. return DMA_BIT_MASK(32);
  115. }
  116. extern u64 dma_get_required_mask(struct device *dev);
  117. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  118. {
  119. return dev->dma_parms ? dev->dma_parms->max_segment_size : 65536;
  120. }
  121. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  122. unsigned int size)
  123. {
  124. if (dev->dma_parms) {
  125. dev->dma_parms->max_segment_size = size;
  126. return 0;
  127. } else
  128. return -EIO;
  129. }
  130. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  131. {
  132. return dev->dma_parms ?
  133. dev->dma_parms->segment_boundary_mask : 0xffffffff;
  134. }
  135. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  136. {
  137. if (dev->dma_parms) {
  138. dev->dma_parms->segment_boundary_mask = mask;
  139. return 0;
  140. } else
  141. return -EIO;
  142. }
  143. /* flags for the coherent memory api */
  144. #define DMA_MEMORY_MAP 0x01
  145. #define DMA_MEMORY_IO 0x02
  146. #define DMA_MEMORY_INCLUDES_CHILDREN 0x04
  147. #define DMA_MEMORY_EXCLUSIVE 0x08
  148. #ifndef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  149. static inline int
  150. dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  151. dma_addr_t device_addr, size_t size, int flags)
  152. {
  153. return 0;
  154. }
  155. static inline void
  156. dma_release_declared_memory(struct device *dev)
  157. {
  158. }
  159. static inline void *
  160. dma_mark_declared_memory_occupied(struct device *dev,
  161. dma_addr_t device_addr, size_t size)
  162. {
  163. return ERR_PTR(-EBUSY);
  164. }
  165. #endif
  166. /*
  167. * Managed DMA API
  168. */
  169. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  170. dma_addr_t *dma_handle, gfp_t gfp);
  171. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  172. dma_addr_t dma_handle);
  173. extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  174. dma_addr_t *dma_handle, gfp_t gfp);
  175. extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  176. dma_addr_t dma_handle);
  177. #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  178. extern int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  179. dma_addr_t device_addr, size_t size,
  180. int flags);
  181. extern void dmam_release_declared_memory(struct device *dev);
  182. #else /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  183. static inline int dmam_declare_coherent_memory(struct device *dev,
  184. dma_addr_t bus_addr, dma_addr_t device_addr,
  185. size_t size, gfp_t gfp)
  186. {
  187. return 0;
  188. }
  189. static inline void dmam_release_declared_memory(struct device *dev)
  190. {
  191. }
  192. #endif /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
  193. #ifndef CONFIG_HAVE_DMA_ATTRS
  194. struct dma_attrs;
  195. #define dma_map_single_attrs(dev, cpu_addr, size, dir, attrs) \
  196. dma_map_single(dev, cpu_addr, size, dir)
  197. #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
  198. dma_unmap_single(dev, dma_addr, size, dir)
  199. #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
  200. dma_map_sg(dev, sgl, nents, dir)
  201. #define dma_unmap_sg_attrs(dev, sgl, nents, dir, attrs) \
  202. dma_unmap_sg(dev, sgl, nents, dir)
  203. #endif /* CONFIG_HAVE_DMA_ATTRS */
  204. #endif