dma-mapping.h 8.4 KB

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