dma-mapping.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. /*
  92. * dma_coherent_pre_ops - barrier functions for coherent memory before DMA.
  93. * A barrier is required to ensure memory operations are complete before the
  94. * initiation of a DMA xfer.
  95. * If the coherent memory is Strongly Ordered
  96. * - pre ARMv7 and 8x50 guarantees ordering wrt other mem accesses
  97. * - ARMv7 guarantees ordering only within a 1KB block, so we need a barrier
  98. * If coherent memory is normal then we need a barrier to prevent
  99. * reordering
  100. */
  101. static inline void dma_coherent_pre_ops(void)
  102. {
  103. #if COHERENT_IS_NORMAL == 1
  104. dmb();
  105. #else
  106. if (arch_is_coherent())
  107. dmb();
  108. else
  109. barrier();
  110. #endif
  111. }
  112. /*
  113. * dma_post_coherent_ops - barrier functions for coherent memory after DMA.
  114. * If the coherent memory is Strongly Ordered we dont need a barrier since
  115. * there are no speculative fetches to Strongly Ordered memory.
  116. * If coherent memory is normal then we need a barrier to prevent reordering
  117. */
  118. static inline void dma_coherent_post_ops(void)
  119. {
  120. #if COHERENT_IS_NORMAL == 1
  121. dmb();
  122. #else
  123. if (arch_is_coherent())
  124. dmb();
  125. else
  126. barrier();
  127. #endif
  128. }
  129. extern int dma_supported(struct device *dev, u64 mask);
  130. /**
  131. * arm_dma_alloc - allocate consistent memory for DMA
  132. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  133. * @size: required memory size
  134. * @handle: bus-specific DMA address
  135. * @attrs: optinal attributes that specific mapping properties
  136. *
  137. * Allocate some memory for a device for performing DMA. This function
  138. * allocates pages, and will return the CPU-viewed address, and sets @handle
  139. * to be the device-viewed address.
  140. */
  141. extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  142. gfp_t gfp, struct dma_attrs *attrs);
  143. #define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
  144. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  145. dma_addr_t *dma_handle, gfp_t flag,
  146. struct dma_attrs *attrs)
  147. {
  148. struct dma_map_ops *ops = get_dma_ops(dev);
  149. void *cpu_addr;
  150. BUG_ON(!ops);
  151. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  152. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  153. return cpu_addr;
  154. }
  155. /**
  156. * arm_dma_free - free memory allocated by arm_dma_alloc
  157. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  158. * @size: size of memory originally requested in dma_alloc_coherent
  159. * @cpu_addr: CPU-view address returned from dma_alloc_coherent
  160. * @handle: device-view address returned from dma_alloc_coherent
  161. * @attrs: optinal attributes that specific mapping properties
  162. *
  163. * Free (and unmap) a DMA buffer previously allocated by
  164. * arm_dma_alloc().
  165. *
  166. * References to memory and mappings associated with cpu_addr/handle
  167. * during and after this call executing are illegal.
  168. */
  169. extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  170. dma_addr_t handle, struct dma_attrs *attrs);
  171. #define dma_free_coherent(d, s, c, h) dma_free_attrs(d, s, c, h, NULL)
  172. static inline void dma_free_attrs(struct device *dev, size_t size,
  173. void *cpu_addr, dma_addr_t dma_handle,
  174. struct dma_attrs *attrs)
  175. {
  176. struct dma_map_ops *ops = get_dma_ops(dev);
  177. BUG_ON(!ops);
  178. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  179. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  180. }
  181. /**
  182. * arm_dma_mmap - map a coherent DMA allocation into user space
  183. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  184. * @vma: vm_area_struct describing requested user mapping
  185. * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
  186. * @handle: device-view address returned from dma_alloc_coherent
  187. * @size: size of memory originally requested in dma_alloc_coherent
  188. * @attrs: optinal attributes that specific mapping properties
  189. *
  190. * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
  191. * into user space. The coherent DMA buffer must not be freed by the
  192. * driver until the user space mapping has been released.
  193. */
  194. extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  195. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  196. struct dma_attrs *attrs);
  197. #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, NULL)
  198. static inline int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  199. void *cpu_addr, dma_addr_t dma_addr,
  200. size_t size, struct dma_attrs *attrs)
  201. {
  202. struct dma_map_ops *ops = get_dma_ops(dev);
  203. BUG_ON(!ops);
  204. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  205. }
  206. static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
  207. dma_addr_t *dma_handle, gfp_t flag)
  208. {
  209. DEFINE_DMA_ATTRS(attrs);
  210. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  211. return dma_alloc_attrs(dev, size, dma_handle, flag, &attrs);
  212. }
  213. static inline void dma_free_writecombine(struct device *dev, size_t size,
  214. void *cpu_addr, dma_addr_t dma_handle)
  215. {
  216. DEFINE_DMA_ATTRS(attrs);
  217. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  218. return dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs);
  219. }
  220. static inline int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  221. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  222. {
  223. DEFINE_DMA_ATTRS(attrs);
  224. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  225. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs);
  226. }
  227. static inline void *dma_alloc_stronglyordered(struct device *dev, size_t size,
  228. dma_addr_t *dma_handle, gfp_t flag)
  229. {
  230. DEFINE_DMA_ATTRS(attrs);
  231. dma_set_attr(DMA_ATTR_STRONGLY_ORDERED, &attrs);
  232. return dma_alloc_attrs(dev, size, dma_handle, flag, &attrs);
  233. }
  234. static inline void dma_free_stronglyordered(struct device *dev, size_t size,
  235. void *cpu_addr, dma_addr_t dma_handle)
  236. {
  237. DEFINE_DMA_ATTRS(attrs);
  238. dma_set_attr(DMA_ATTR_STRONGLY_ORDERED, &attrs);
  239. return dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs);
  240. }
  241. static inline int dma_mmap_stronglyordered(struct device *dev,
  242. struct vm_area_struct *vma, void *cpu_addr,
  243. dma_addr_t dma_addr, size_t size)
  244. {
  245. DEFINE_DMA_ATTRS(attrs);
  246. dma_set_attr(DMA_ATTR_STRONGLY_ORDERED, &attrs);
  247. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs);
  248. }
  249. static inline void *dma_alloc_nonconsistent(struct device *dev, size_t size,
  250. dma_addr_t *dma_handle, gfp_t flag)
  251. {
  252. DEFINE_DMA_ATTRS(attrs);
  253. dma_set_attr(DMA_ATTR_NON_CONSISTENT, &attrs);
  254. return dma_alloc_attrs(dev, size, dma_handle, flag, &attrs);
  255. }
  256. static inline void dma_free_nonconsistent(struct device *dev, size_t size,
  257. void *cpu_addr, dma_addr_t dma_handle)
  258. {
  259. DEFINE_DMA_ATTRS(attrs);
  260. dma_set_attr(DMA_ATTR_NON_CONSISTENT, &attrs);
  261. return dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs);
  262. }
  263. static inline int dma_mmap_nonconsistent(struct device *dev,
  264. struct vm_area_struct *vma, void *cpu_addr,
  265. dma_addr_t dma_addr, size_t size)
  266. {
  267. DEFINE_DMA_ATTRS(attrs);
  268. dma_set_attr(DMA_ATTR_NON_CONSISTENT, &attrs);
  269. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs);
  270. }
  271. /*
  272. * This can be called during boot to increase the size of the consistent
  273. * DMA region above it's default value of 2MB. It must be called before the
  274. * memory allocator is initialised, i.e. before any core_initcall.
  275. */
  276. static inline void init_consistent_dma_size(unsigned long size) { }
  277. /*
  278. * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
  279. * and utilize bounce buffers as needed to work around limited DMA windows.
  280. *
  281. * On the SA-1111, a bug limits DMA to only certain regions of RAM.
  282. * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
  283. * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
  284. *
  285. * The following are helper functions used by the dmabounce subystem
  286. *
  287. */
  288. /**
  289. * dmabounce_register_dev
  290. *
  291. * @dev: valid struct device pointer
  292. * @small_buf_size: size of buffers to use with small buffer pool
  293. * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
  294. * @needs_bounce_fn: called to determine whether buffer needs bouncing
  295. *
  296. * This function should be called by low-level platform code to register
  297. * a device as requireing DMA buffer bouncing. The function will allocate
  298. * appropriate DMA pools for the device.
  299. */
  300. extern int dmabounce_register_dev(struct device *, unsigned long,
  301. unsigned long, int (*)(struct device *, dma_addr_t, size_t));
  302. /**
  303. * dmabounce_unregister_dev
  304. *
  305. * @dev: valid struct device pointer
  306. *
  307. * This function should be called by low-level platform code when device
  308. * that was previously registered with dmabounce_register_dev is removed
  309. * from the system.
  310. *
  311. */
  312. extern void dmabounce_unregister_dev(struct device *);
  313. /**
  314. * dma_cache_pre_ops - clean or invalidate cache before dma transfer is
  315. * initiated and perform a barrier operation.
  316. * @virtual_addr: A kernel logical or kernel virtual address
  317. * @size: size of buffer to map
  318. * @dir: DMA transfer direction
  319. *
  320. * Ensure that any data held in the cache is appropriately discarded
  321. * or written back.
  322. *
  323. */
  324. static inline void dma_cache_pre_ops(void *virtual_addr,
  325. size_t size, enum dma_data_direction dir)
  326. {
  327. extern void ___dma_single_cpu_to_dev(const void *, size_t,
  328. enum dma_data_direction);
  329. BUG_ON(!valid_dma_direction(dir));
  330. if (!arch_is_coherent())
  331. ___dma_single_cpu_to_dev(virtual_addr, size, dir);
  332. }
  333. /**
  334. * dma_cache_post_ops - clean or invalidate cache after dma transfer is
  335. * initiated and perform a barrier operation.
  336. * @virtual_addr: A kernel logical or kernel virtual address
  337. * @size: size of buffer to map
  338. * @dir: DMA transfer direction
  339. *
  340. * Ensure that any data held in the cache is appropriately discarded
  341. * or written back.
  342. *
  343. */
  344. static inline void dma_cache_post_ops(void *virtual_addr,
  345. size_t size, enum dma_data_direction dir)
  346. {
  347. extern void ___dma_single_cpu_to_dev(const void *, size_t,
  348. enum dma_data_direction);
  349. BUG_ON(!valid_dma_direction(dir));
  350. if (arch_has_speculative_dfetch() && !arch_is_coherent()
  351. && dir != DMA_TO_DEVICE)
  352. /*
  353. * Treat DMA_BIDIRECTIONAL and DMA_FROM_DEVICE
  354. * identically: invalidate
  355. */
  356. ___dma_single_cpu_to_dev(virtual_addr,
  357. size, DMA_FROM_DEVICE);
  358. }
  359. /*
  360. * The scatter list versions of the above methods.
  361. */
  362. extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
  363. enum dma_data_direction, struct dma_attrs *attrs);
  364. extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
  365. enum dma_data_direction, struct dma_attrs *attrs);
  366. extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
  367. enum dma_data_direction);
  368. extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
  369. enum dma_data_direction);
  370. #endif /* __KERNEL__ */
  371. #endif