dma-mapping.h 8.6 KB

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