dma-mapping.h 8.1 KB

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