dma-mapping.h 9.8 KB

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