dma-mapping.h 9.5 KB

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