dma-mapping.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef ASMARM_DMA_MAPPING_H
  3. #define ASMARM_DMA_MAPPING_H
  4. #ifdef __KERNEL__
  5. #include <linux/mm_types.h>
  6. #include <linux/scatterlist.h>
  7. #include <linux/dma-debug.h>
  8. #include <asm/memory.h>
  9. #include <xen/xen.h>
  10. #include <asm/xen/hypervisor.h>
  11. extern const struct dma_map_ops arm_dma_ops;
  12. extern const struct dma_map_ops arm_coherent_dma_ops;
  13. static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
  14. {
  15. return IS_ENABLED(CONFIG_MMU) ? &arm_dma_ops : &dma_direct_ops;
  16. }
  17. #ifdef __arch_page_to_dma
  18. #error Please update to __arch_pfn_to_dma
  19. #endif
  20. /*
  21. * dma_to_pfn/pfn_to_dma/dma_to_virt/virt_to_dma are architecture private
  22. * functions used internally by the DMA-mapping API to provide DMA
  23. * addresses. They must not be used by drivers.
  24. */
  25. #ifndef __arch_pfn_to_dma
  26. static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
  27. {
  28. if (dev)
  29. pfn -= dev->dma_pfn_offset;
  30. return (dma_addr_t)__pfn_to_bus(pfn);
  31. }
  32. static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
  33. {
  34. unsigned long pfn = __bus_to_pfn(addr);
  35. if (dev)
  36. pfn += dev->dma_pfn_offset;
  37. return pfn;
  38. }
  39. static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
  40. {
  41. if (dev) {
  42. unsigned long pfn = dma_to_pfn(dev, addr);
  43. return phys_to_virt(__pfn_to_phys(pfn));
  44. }
  45. return (void *)__bus_to_virt((unsigned long)addr);
  46. }
  47. static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
  48. {
  49. if (dev)
  50. return pfn_to_dma(dev, virt_to_pfn(addr));
  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. /* The ARM override for dma_max_pfn() */
  72. static inline unsigned long dma_max_pfn(struct device *dev)
  73. {
  74. return dma_to_pfn(dev, *dev->dma_mask);
  75. }
  76. #define dma_max_pfn(dev) dma_max_pfn(dev)
  77. #define arch_setup_dma_ops arch_setup_dma_ops
  78. extern void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  79. const struct iommu_ops *iommu, bool coherent);
  80. #define arch_teardown_dma_ops arch_teardown_dma_ops
  81. extern void arch_teardown_dma_ops(struct device *dev);
  82. /* do not use this function in a driver */
  83. static inline bool is_device_dma_coherent(struct device *dev)
  84. {
  85. return dev->archdata.dma_coherent;
  86. }
  87. /**
  88. * arm_dma_alloc - allocate consistent memory for DMA
  89. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  90. * @size: required memory size
  91. * @handle: bus-specific DMA address
  92. * @attrs: optinal attributes that specific mapping properties
  93. *
  94. * Allocate some memory for a device for performing DMA. This function
  95. * allocates pages, and will return the CPU-viewed address, and sets @handle
  96. * to be the device-viewed address.
  97. */
  98. extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  99. gfp_t gfp, unsigned long attrs);
  100. /**
  101. * arm_dma_free - free memory allocated by arm_dma_alloc
  102. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  103. * @size: size of memory originally requested in dma_alloc_coherent
  104. * @cpu_addr: CPU-view address returned from dma_alloc_coherent
  105. * @handle: device-view address returned from dma_alloc_coherent
  106. * @attrs: optinal attributes that specific mapping properties
  107. *
  108. * Free (and unmap) a DMA buffer previously allocated by
  109. * arm_dma_alloc().
  110. *
  111. * References to memory and mappings associated with cpu_addr/handle
  112. * during and after this call executing are illegal.
  113. */
  114. extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
  115. dma_addr_t handle, unsigned long attrs);
  116. /**
  117. * arm_dma_mmap - map a coherent DMA allocation into user space
  118. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  119. * @vma: vm_area_struct describing requested user mapping
  120. * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
  121. * @handle: device-view address returned from dma_alloc_coherent
  122. * @size: size of memory originally requested in dma_alloc_coherent
  123. * @attrs: optinal attributes that specific mapping properties
  124. *
  125. * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
  126. * into user space. The coherent DMA buffer must not be freed by the
  127. * driver until the user space mapping has been released.
  128. */
  129. extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  130. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  131. unsigned long attrs);
  132. /*
  133. * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
  134. * and utilize bounce buffers as needed to work around limited DMA windows.
  135. *
  136. * On the SA-1111, a bug limits DMA to only certain regions of RAM.
  137. * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
  138. * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
  139. *
  140. * The following are helper functions used by the dmabounce subystem
  141. *
  142. */
  143. /**
  144. * dmabounce_register_dev
  145. *
  146. * @dev: valid struct device pointer
  147. * @small_buf_size: size of buffers to use with small buffer pool
  148. * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
  149. * @needs_bounce_fn: called to determine whether buffer needs bouncing
  150. *
  151. * This function should be called by low-level platform code to register
  152. * a device as requireing DMA buffer bouncing. The function will allocate
  153. * appropriate DMA pools for the device.
  154. */
  155. extern int dmabounce_register_dev(struct device *, unsigned long,
  156. unsigned long, int (*)(struct device *, dma_addr_t, size_t));
  157. /**
  158. * dmabounce_unregister_dev
  159. *
  160. * @dev: valid struct device pointer
  161. *
  162. * This function should be called by low-level platform code when device
  163. * that was previously registered with dmabounce_register_dev is removed
  164. * from the system.
  165. *
  166. */
  167. extern void dmabounce_unregister_dev(struct device *);
  168. /*
  169. * The scatter list versions of the above methods.
  170. */
  171. extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
  172. enum dma_data_direction, unsigned long attrs);
  173. extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
  174. enum dma_data_direction, unsigned long attrs);
  175. extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
  176. enum dma_data_direction);
  177. extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
  178. enum dma_data_direction);
  179. extern int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  180. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  181. unsigned long attrs);
  182. #endif /* __KERNEL__ */
  183. #endif