dma-mapping.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef ASMARM_DMA_MAPPING_H
  2. #define ASMARM_DMA_MAPPING_H
  3. #ifdef __KERNEL__
  4. #include <linux/mm_types.h>
  5. #include <linux/scatterlist.h>
  6. #include <linux/dma-attrs.h>
  7. #include <linux/dma-debug.h>
  8. #include <asm-generic/dma-coherent.h>
  9. #include <asm/memory.h>
  10. #define DMA_ERROR_CODE (~0)
  11. extern struct dma_map_ops arm_dma_ops;
  12. extern struct dma_map_ops arm_coherent_dma_ops;
  13. static inline struct dma_map_ops *get_dma_ops(struct device *dev)
  14. {
  15. if (dev && dev->archdata.dma_ops)
  16. return dev->archdata.dma_ops;
  17. return &arm_dma_ops;
  18. }
  19. static inline void set_dma_ops(struct device *dev, struct dma_map_ops *ops)
  20. {
  21. BUG_ON(!dev);
  22. dev->archdata.dma_ops = ops;
  23. }
  24. #include <asm-generic/dma-mapping-common.h>
  25. static inline int dma_set_mask(struct device *dev, u64 mask)
  26. {
  27. return get_dma_ops(dev)->set_dma_mask(dev, mask);
  28. }
  29. #ifdef __arch_page_to_dma
  30. #error Please update to __arch_pfn_to_dma
  31. #endif
  32. /*
  33. * dma_to_pfn/pfn_to_dma/dma_to_virt/virt_to_dma are architecture private
  34. * functions used internally by the DMA-mapping API to provide DMA
  35. * addresses. They must not be used by drivers.
  36. */
  37. #ifndef __arch_pfn_to_dma
  38. static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
  39. {
  40. return (dma_addr_t)__pfn_to_bus(pfn);
  41. }
  42. static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
  43. {
  44. return __bus_to_pfn(addr);
  45. }
  46. static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
  47. {
  48. return (void *)__bus_to_virt((unsigned long)addr);
  49. }
  50. static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
  51. {
  52. return (dma_addr_t)__virt_to_bus((unsigned long)(addr));
  53. }
  54. #else
  55. static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
  56. {
  57. return __arch_pfn_to_dma(dev, pfn);
  58. }
  59. static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
  60. {
  61. return __arch_dma_to_pfn(dev, addr);
  62. }
  63. static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
  64. {
  65. return __arch_dma_to_virt(dev, addr);
  66. }
  67. static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
  68. {
  69. return __arch_virt_to_dma(dev, addr);
  70. }
  71. #endif
  72. /*
  73. * DMA errors are defined by all-bits-set in the DMA address.
  74. */
  75. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  76. {
  77. return dma_addr == DMA_ERROR_CODE;
  78. }
  79. /*
  80. * Dummy noncoherent implementation. We don't provide a dma_cache_sync
  81. * function so drivers using this API are highlighted with build warnings.
  82. */
  83. static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
  84. dma_addr_t *handle, gfp_t gfp)
  85. {
  86. return NULL;
  87. }
  88. static inline void dma_free_noncoherent(struct device *dev, size_t size,
  89. void *cpu_addr, dma_addr_t handle)
  90. {
  91. }
  92. extern int dma_supported(struct device *dev, u64 mask);
  93. /**
  94. * arm_dma_alloc - allocate consistent memory for DMA
  95. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  96. * @size: required memory size
  97. * @handle: bus-specific DMA address
  98. * @attrs: optinal attributes that specific mapping properties
  99. *
  100. * Allocate some memory for a device for performing DMA. This function
  101. * allocates pages, and will return the CPU-viewed address, and sets @handle
  102. * to be the device-viewed address.
  103. */
  104. extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  105. gfp_t gfp, struct dma_attrs *attrs);
  106. #define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
  107. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  108. dma_addr_t *dma_handle, gfp_t flag,
  109. struct dma_attrs *attrs)
  110. {
  111. struct dma_map_ops *ops = get_dma_ops(dev);
  112. void *cpu_addr;
  113. BUG_ON(!ops);
  114. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  115. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  116. return cpu_addr;
  117. }
  118. /**
  119. * arm_dma_free - free memory allocated by arm_dma_alloc
  120. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  121. * @size: size of memory originally requested in dma_alloc_coherent
  122. * @cpu_addr: CPU-view address returned from dma_alloc_coherent
  123. * @handle: device-view address returned from dma_alloc_coherent
  124. * @attrs: optinal attributes that specific mapping properties
  125. *
  126. * Free (and unmap) a DMA buffer previously allocated by
  127. * arm_dma_alloc().
  128. *
  129. * References to memory and mappings associated with cpu_addr/handle
  130. * during and after this call executing are illegal.
  131. */
  132. extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  133. dma_addr_t handle, struct dma_attrs *attrs);
  134. #define dma_free_coherent(d, s, c, h) dma_free_attrs(d, s, c, h, NULL)
  135. static inline void dma_free_attrs(struct device *dev, size_t size,
  136. void *cpu_addr, dma_addr_t dma_handle,
  137. struct dma_attrs *attrs)
  138. {
  139. struct dma_map_ops *ops = get_dma_ops(dev);
  140. BUG_ON(!ops);
  141. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  142. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  143. }
  144. /**
  145. * arm_dma_mmap - map a coherent DMA allocation into user space
  146. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  147. * @vma: vm_area_struct describing requested user mapping
  148. * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
  149. * @handle: device-view address returned from dma_alloc_coherent
  150. * @size: size of memory originally requested in dma_alloc_coherent
  151. * @attrs: optinal attributes that specific mapping properties
  152. *
  153. * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
  154. * into user space. The coherent DMA buffer must not be freed by the
  155. * driver until the user space mapping has been released.
  156. */
  157. extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  158. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  159. struct dma_attrs *attrs);
  160. static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
  161. dma_addr_t *dma_handle, gfp_t flag)
  162. {
  163. DEFINE_DMA_ATTRS(attrs);
  164. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  165. return dma_alloc_attrs(dev, size, dma_handle, flag, &attrs);
  166. }
  167. static inline void dma_free_writecombine(struct device *dev, size_t size,
  168. void *cpu_addr, dma_addr_t dma_handle)
  169. {
  170. DEFINE_DMA_ATTRS(attrs);
  171. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  172. return dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs);
  173. }
  174. /*
  175. * This can be called during early boot to increase the size of the atomic
  176. * coherent DMA pool above the default value of 256KiB. It must be called
  177. * before postcore_initcall.
  178. */
  179. extern void __init init_dma_coherent_pool_size(unsigned long size);
  180. /*
  181. * This can be called during boot to increase the size of the consistent
  182. * DMA region above it's default value of 2MB. It must be called before the
  183. * memory allocator is initialised, i.e. before any core_initcall.
  184. */
  185. static inline void init_consistent_dma_size(unsigned long size) { }
  186. /*
  187. * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
  188. * and utilize bounce buffers as needed to work around limited DMA windows.
  189. *
  190. * On the SA-1111, a bug limits DMA to only certain regions of RAM.
  191. * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
  192. * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
  193. *
  194. * The following are helper functions used by the dmabounce subystem
  195. *
  196. */
  197. /**
  198. * dmabounce_register_dev
  199. *
  200. * @dev: valid struct device pointer
  201. * @small_buf_size: size of buffers to use with small buffer pool
  202. * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
  203. * @needs_bounce_fn: called to determine whether buffer needs bouncing
  204. *
  205. * This function should be called by low-level platform code to register
  206. * a device as requireing DMA buffer bouncing. The function will allocate
  207. * appropriate DMA pools for the device.
  208. */
  209. extern int dmabounce_register_dev(struct device *, unsigned long,
  210. unsigned long, int (*)(struct device *, dma_addr_t, size_t));
  211. /**
  212. * dmabounce_unregister_dev
  213. *
  214. * @dev: valid struct device pointer
  215. *
  216. * This function should be called by low-level platform code when device
  217. * that was previously registered with dmabounce_register_dev is removed
  218. * from the system.
  219. *
  220. */
  221. extern void dmabounce_unregister_dev(struct device *);
  222. /*
  223. * The scatter list versions of the above methods.
  224. */
  225. extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
  226. enum dma_data_direction, struct dma_attrs *attrs);
  227. extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
  228. enum dma_data_direction, struct dma_attrs *attrs);
  229. extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
  230. enum dma_data_direction);
  231. extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
  232. enum dma_data_direction);
  233. extern int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  234. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  235. struct dma_attrs *attrs);
  236. #endif /* __KERNEL__ */
  237. #endif