dma-mapping.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. debug_dma_mapping_error(dev, dma_addr);
  78. return dma_addr == DMA_ERROR_CODE;
  79. }
  80. /*
  81. * Dummy noncoherent implementation. We don't provide a dma_cache_sync
  82. * function so drivers using this API are highlighted with build warnings.
  83. */
  84. static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
  85. dma_addr_t *handle, gfp_t gfp)
  86. {
  87. return NULL;
  88. }
  89. static inline void dma_free_noncoherent(struct device *dev, size_t size,
  90. void *cpu_addr, dma_addr_t handle)
  91. {
  92. }
  93. extern int dma_supported(struct device *dev, u64 mask);
  94. extern int arm_dma_set_mask(struct device *dev, u64 dma_mask);
  95. /**
  96. * arm_dma_alloc - allocate consistent memory for DMA
  97. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  98. * @size: required memory size
  99. * @handle: bus-specific DMA address
  100. * @attrs: optinal attributes that specific mapping properties
  101. *
  102. * Allocate some memory for a device for performing DMA. This function
  103. * allocates pages, and will return the CPU-viewed address, and sets @handle
  104. * to be the device-viewed address.
  105. */
  106. extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  107. gfp_t gfp, struct dma_attrs *attrs);
  108. #define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
  109. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  110. dma_addr_t *dma_handle, gfp_t flag,
  111. struct dma_attrs *attrs)
  112. {
  113. struct dma_map_ops *ops = get_dma_ops(dev);
  114. void *cpu_addr;
  115. BUG_ON(!ops);
  116. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  117. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  118. return cpu_addr;
  119. }
  120. /**
  121. * arm_dma_free - free memory allocated by arm_dma_alloc
  122. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  123. * @size: size of memory originally requested in dma_alloc_coherent
  124. * @cpu_addr: CPU-view address returned from dma_alloc_coherent
  125. * @handle: device-view address returned from dma_alloc_coherent
  126. * @attrs: optinal attributes that specific mapping properties
  127. *
  128. * Free (and unmap) a DMA buffer previously allocated by
  129. * arm_dma_alloc().
  130. *
  131. * References to memory and mappings associated with cpu_addr/handle
  132. * during and after this call executing are illegal.
  133. */
  134. extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  135. dma_addr_t handle, struct dma_attrs *attrs);
  136. #define dma_free_coherent(d, s, c, h) dma_free_attrs(d, s, c, h, NULL)
  137. static inline void dma_free_attrs(struct device *dev, size_t size,
  138. void *cpu_addr, dma_addr_t dma_handle,
  139. struct dma_attrs *attrs)
  140. {
  141. struct dma_map_ops *ops = get_dma_ops(dev);
  142. BUG_ON(!ops);
  143. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  144. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  145. }
  146. /**
  147. * arm_dma_mmap - map a coherent DMA allocation into user space
  148. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  149. * @vma: vm_area_struct describing requested user mapping
  150. * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
  151. * @handle: device-view address returned from dma_alloc_coherent
  152. * @size: size of memory originally requested in dma_alloc_coherent
  153. * @attrs: optinal attributes that specific mapping properties
  154. *
  155. * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
  156. * into user space. The coherent DMA buffer must not be freed by the
  157. * driver until the user space mapping has been released.
  158. */
  159. extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  160. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  161. struct dma_attrs *attrs);
  162. static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
  163. dma_addr_t *dma_handle, gfp_t flag)
  164. {
  165. DEFINE_DMA_ATTRS(attrs);
  166. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  167. return dma_alloc_attrs(dev, size, dma_handle, flag, &attrs);
  168. }
  169. static inline void dma_free_writecombine(struct device *dev, size_t size,
  170. void *cpu_addr, dma_addr_t dma_handle)
  171. {
  172. DEFINE_DMA_ATTRS(attrs);
  173. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  174. return dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs);
  175. }
  176. /*
  177. * This can be called during early boot to increase the size of the atomic
  178. * coherent DMA pool above the default value of 256KiB. It must be called
  179. * before postcore_initcall.
  180. */
  181. extern void __init init_dma_coherent_pool_size(unsigned long size);
  182. /*
  183. * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
  184. * and utilize bounce buffers as needed to work around limited DMA windows.
  185. *
  186. * On the SA-1111, a bug limits DMA to only certain regions of RAM.
  187. * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
  188. * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
  189. *
  190. * The following are helper functions used by the dmabounce subystem
  191. *
  192. */
  193. /**
  194. * dmabounce_register_dev
  195. *
  196. * @dev: valid struct device pointer
  197. * @small_buf_size: size of buffers to use with small buffer pool
  198. * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
  199. * @needs_bounce_fn: called to determine whether buffer needs bouncing
  200. *
  201. * This function should be called by low-level platform code to register
  202. * a device as requireing DMA buffer bouncing. The function will allocate
  203. * appropriate DMA pools for the device.
  204. */
  205. extern int dmabounce_register_dev(struct device *, unsigned long,
  206. unsigned long, int (*)(struct device *, dma_addr_t, size_t));
  207. /**
  208. * dmabounce_unregister_dev
  209. *
  210. * @dev: valid struct device pointer
  211. *
  212. * This function should be called by low-level platform code when device
  213. * that was previously registered with dmabounce_register_dev is removed
  214. * from the system.
  215. *
  216. */
  217. extern void dmabounce_unregister_dev(struct device *);
  218. /*
  219. * The scatter list versions of the above methods.
  220. */
  221. extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
  222. enum dma_data_direction, struct dma_attrs *attrs);
  223. extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
  224. enum dma_data_direction, struct dma_attrs *attrs);
  225. extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
  226. enum dma_data_direction);
  227. extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
  228. enum dma_data_direction);
  229. extern int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  230. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  231. struct dma_attrs *attrs);
  232. #endif /* __KERNEL__ */
  233. #endif