dma-mapping.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef _ASM_DMA_MAPPING_H_
  2. #define _ASM_DMA_MAPPING_H_
  3. /*
  4. * IOMMU interface. See Documentation/DMA-mapping.txt and DMA-API.txt for
  5. * documentation.
  6. */
  7. #include <linux/scatterlist.h>
  8. #include <asm/io.h>
  9. #include <asm/swiotlb.h>
  10. extern dma_addr_t bad_dma_address;
  11. extern int iommu_merge;
  12. extern struct device fallback_dev;
  13. extern int panic_on_overflow;
  14. extern int forbid_dac;
  15. extern int force_iommu;
  16. struct dma_mapping_ops {
  17. int (*mapping_error)(dma_addr_t dma_addr);
  18. void* (*alloc_coherent)(struct device *dev, size_t size,
  19. dma_addr_t *dma_handle, gfp_t gfp);
  20. void (*free_coherent)(struct device *dev, size_t size,
  21. void *vaddr, dma_addr_t dma_handle);
  22. dma_addr_t (*map_single)(struct device *hwdev, phys_addr_t ptr,
  23. size_t size, int direction);
  24. /* like map_single, but doesn't check the device mask */
  25. dma_addr_t (*map_simple)(struct device *hwdev, phys_addr_t ptr,
  26. size_t size, int direction);
  27. void (*unmap_single)(struct device *dev, dma_addr_t addr,
  28. size_t size, int direction);
  29. void (*sync_single_for_cpu)(struct device *hwdev,
  30. dma_addr_t dma_handle, size_t size,
  31. int direction);
  32. void (*sync_single_for_device)(struct device *hwdev,
  33. dma_addr_t dma_handle, size_t size,
  34. int direction);
  35. void (*sync_single_range_for_cpu)(struct device *hwdev,
  36. dma_addr_t dma_handle, unsigned long offset,
  37. size_t size, int direction);
  38. void (*sync_single_range_for_device)(struct device *hwdev,
  39. dma_addr_t dma_handle, unsigned long offset,
  40. size_t size, int direction);
  41. void (*sync_sg_for_cpu)(struct device *hwdev,
  42. struct scatterlist *sg, int nelems,
  43. int direction);
  44. void (*sync_sg_for_device)(struct device *hwdev,
  45. struct scatterlist *sg, int nelems,
  46. int direction);
  47. int (*map_sg)(struct device *hwdev, struct scatterlist *sg,
  48. int nents, int direction);
  49. void (*unmap_sg)(struct device *hwdev,
  50. struct scatterlist *sg, int nents,
  51. int direction);
  52. int (*dma_supported)(struct device *hwdev, u64 mask);
  53. int is_phys;
  54. };
  55. extern const struct dma_mapping_ops *dma_ops;
  56. static inline int dma_mapping_error(dma_addr_t dma_addr)
  57. {
  58. if (dma_ops->mapping_error)
  59. return dma_ops->mapping_error(dma_addr);
  60. return (dma_addr == bad_dma_address);
  61. }
  62. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  63. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  64. void *dma_alloc_coherent(struct device *dev, size_t size,
  65. dma_addr_t *dma_handle, gfp_t flag);
  66. void dma_free_coherent(struct device *dev, size_t size,
  67. void *vaddr, dma_addr_t dma_handle);
  68. extern int dma_supported(struct device *hwdev, u64 mask);
  69. extern int dma_set_mask(struct device *dev, u64 mask);
  70. static inline dma_addr_t
  71. dma_map_single(struct device *hwdev, void *ptr, size_t size,
  72. int direction)
  73. {
  74. BUG_ON(!valid_dma_direction(direction));
  75. return dma_ops->map_single(hwdev, virt_to_phys(ptr), size, direction);
  76. }
  77. static inline void
  78. dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
  79. int direction)
  80. {
  81. BUG_ON(!valid_dma_direction(direction));
  82. if (dma_ops->unmap_single)
  83. dma_ops->unmap_single(dev, addr, size, direction);
  84. }
  85. static inline int
  86. dma_map_sg(struct device *hwdev, struct scatterlist *sg,
  87. int nents, int direction)
  88. {
  89. BUG_ON(!valid_dma_direction(direction));
  90. return dma_ops->map_sg(hwdev, sg, nents, direction);
  91. }
  92. static inline void
  93. dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
  94. int direction)
  95. {
  96. BUG_ON(!valid_dma_direction(direction));
  97. if (dma_ops->unmap_sg)
  98. dma_ops->unmap_sg(hwdev, sg, nents, direction);
  99. }
  100. static inline void
  101. dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  102. size_t size, int direction)
  103. {
  104. BUG_ON(!valid_dma_direction(direction));
  105. if (dma_ops->sync_single_for_cpu)
  106. dma_ops->sync_single_for_cpu(hwdev, dma_handle, size,
  107. direction);
  108. flush_write_buffers();
  109. }
  110. static inline void
  111. dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
  112. size_t size, int direction)
  113. {
  114. BUG_ON(!valid_dma_direction(direction));
  115. if (dma_ops->sync_single_for_device)
  116. dma_ops->sync_single_for_device(hwdev, dma_handle, size,
  117. direction);
  118. flush_write_buffers();
  119. }
  120. static inline void
  121. dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  122. unsigned long offset, size_t size, int direction)
  123. {
  124. BUG_ON(!valid_dma_direction(direction));
  125. if (dma_ops->sync_single_range_for_cpu)
  126. dma_ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
  127. size, direction);
  128. flush_write_buffers();
  129. }
  130. static inline void
  131. dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
  132. unsigned long offset, size_t size,
  133. int direction)
  134. {
  135. BUG_ON(!valid_dma_direction(direction));
  136. if (dma_ops->sync_single_range_for_device)
  137. dma_ops->sync_single_range_for_device(hwdev, dma_handle,
  138. offset, size, direction);
  139. flush_write_buffers();
  140. }
  141. static inline void
  142. dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
  143. int nelems, int direction)
  144. {
  145. BUG_ON(!valid_dma_direction(direction));
  146. if (dma_ops->sync_sg_for_cpu)
  147. dma_ops->sync_sg_for_cpu(hwdev, sg, nelems, direction);
  148. flush_write_buffers();
  149. }
  150. static inline void
  151. dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
  152. int nelems, int direction)
  153. {
  154. BUG_ON(!valid_dma_direction(direction));
  155. if (dma_ops->sync_sg_for_device)
  156. dma_ops->sync_sg_for_device(hwdev, sg, nelems, direction);
  157. flush_write_buffers();
  158. }
  159. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  160. size_t offset, size_t size,
  161. int direction)
  162. {
  163. BUG_ON(!valid_dma_direction(direction));
  164. return dma_ops->map_single(dev, page_to_phys(page)+offset,
  165. size, direction);
  166. }
  167. static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
  168. size_t size, int direction)
  169. {
  170. dma_unmap_single(dev, addr, size, direction);
  171. }
  172. static inline void
  173. dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  174. enum dma_data_direction dir)
  175. {
  176. flush_write_buffers();
  177. }
  178. static inline int dma_get_cache_alignment(void)
  179. {
  180. /* no easy way to get cache size on all x86, so return the
  181. * maximum possible, to be safe */
  182. return boot_cpu_data.x86_clflush_size;
  183. }
  184. #define dma_is_consistent(d, h) (1)
  185. #ifdef CONFIG_X86_32
  186. # define ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
  187. struct dma_coherent_mem {
  188. void *virt_base;
  189. u32 device_base;
  190. int size;
  191. int flags;
  192. unsigned long *bitmap;
  193. };
  194. extern int
  195. dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
  196. dma_addr_t device_addr, size_t size, int flags);
  197. extern void
  198. dma_release_declared_memory(struct device *dev);
  199. extern void *
  200. dma_mark_declared_memory_occupied(struct device *dev,
  201. dma_addr_t device_addr, size_t size);
  202. #endif /* CONFIG_X86_32 */
  203. #endif