dma-map-ops.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This header is for implementations of dma_map_ops and related code.
  4. * It should not be included in drivers just using the DMA API.
  5. */
  6. #ifndef _LINUX_DMA_MAP_OPS_H
  7. #define _LINUX_DMA_MAP_OPS_H
  8. #include <linux/dma-mapping.h>
  9. #include <linux/pgtable.h>
  10. #include <linux/android_kabi.h>
  11. struct cma;
  12. /*
  13. * Values for struct dma_map_ops.flags:
  14. *
  15. * DMA_F_PCI_P2PDMA_SUPPORTED: Indicates the dma_map_ops implementation can
  16. * handle PCI P2PDMA pages in the map_sg/unmap_sg operation.
  17. */
  18. #define DMA_F_PCI_P2PDMA_SUPPORTED (1 << 0)
  19. struct dma_map_ops {
  20. unsigned int flags;
  21. void *(*alloc)(struct device *dev, size_t size,
  22. dma_addr_t *dma_handle, gfp_t gfp,
  23. unsigned long attrs);
  24. void (*free)(struct device *dev, size_t size, void *vaddr,
  25. dma_addr_t dma_handle, unsigned long attrs);
  26. struct page *(*alloc_pages)(struct device *dev, size_t size,
  27. dma_addr_t *dma_handle, enum dma_data_direction dir,
  28. gfp_t gfp);
  29. void (*free_pages)(struct device *dev, size_t size, struct page *vaddr,
  30. dma_addr_t dma_handle, enum dma_data_direction dir);
  31. struct sg_table *(*alloc_noncontiguous)(struct device *dev, size_t size,
  32. enum dma_data_direction dir, gfp_t gfp,
  33. unsigned long attrs);
  34. void (*free_noncontiguous)(struct device *dev, size_t size,
  35. struct sg_table *sgt, enum dma_data_direction dir);
  36. int (*mmap)(struct device *, struct vm_area_struct *,
  37. void *, dma_addr_t, size_t, unsigned long attrs);
  38. int (*get_sgtable)(struct device *dev, struct sg_table *sgt,
  39. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  40. unsigned long attrs);
  41. dma_addr_t (*map_page)(struct device *dev, struct page *page,
  42. unsigned long offset, size_t size,
  43. enum dma_data_direction dir, unsigned long attrs);
  44. void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
  45. size_t size, enum dma_data_direction dir,
  46. unsigned long attrs);
  47. /*
  48. * map_sg should return a negative error code on error. See
  49. * dma_map_sgtable() for a list of appropriate error codes
  50. * and their meanings.
  51. */
  52. int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents,
  53. enum dma_data_direction dir, unsigned long attrs);
  54. void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
  55. enum dma_data_direction dir, unsigned long attrs);
  56. dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
  57. size_t size, enum dma_data_direction dir,
  58. unsigned long attrs);
  59. void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
  60. size_t size, enum dma_data_direction dir,
  61. unsigned long attrs);
  62. void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
  63. size_t size, enum dma_data_direction dir);
  64. void (*sync_single_for_device)(struct device *dev,
  65. dma_addr_t dma_handle, size_t size,
  66. enum dma_data_direction dir);
  67. void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
  68. int nents, enum dma_data_direction dir);
  69. void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
  70. int nents, enum dma_data_direction dir);
  71. void (*cache_sync)(struct device *dev, void *vaddr, size_t size,
  72. enum dma_data_direction direction);
  73. int (*dma_supported)(struct device *dev, u64 mask);
  74. u64 (*get_required_mask)(struct device *dev);
  75. size_t (*max_mapping_size)(struct device *dev);
  76. size_t (*opt_mapping_size)(void);
  77. unsigned long (*get_merge_boundary)(struct device *dev);
  78. ANDROID_KABI_RESERVE(1);
  79. ANDROID_KABI_RESERVE(2);
  80. ANDROID_KABI_RESERVE(3);
  81. ANDROID_KABI_RESERVE(4);
  82. };
  83. #ifdef CONFIG_DMA_OPS
  84. #include <asm/dma-mapping.h>
  85. static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
  86. {
  87. if (dev->dma_ops)
  88. return dev->dma_ops;
  89. return get_arch_dma_ops(dev->bus);
  90. }
  91. static inline void set_dma_ops(struct device *dev,
  92. const struct dma_map_ops *dma_ops)
  93. {
  94. dev->dma_ops = dma_ops;
  95. }
  96. #else /* CONFIG_DMA_OPS */
  97. static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
  98. {
  99. return NULL;
  100. }
  101. static inline void set_dma_ops(struct device *dev,
  102. const struct dma_map_ops *dma_ops)
  103. {
  104. }
  105. #endif /* CONFIG_DMA_OPS */
  106. #ifdef CONFIG_DMA_CMA
  107. extern struct cma *dma_contiguous_default_area;
  108. static inline struct cma *dev_get_cma_area(struct device *dev)
  109. {
  110. if (dev && dev->cma_area)
  111. return dev->cma_area;
  112. return dma_contiguous_default_area;
  113. }
  114. void dma_contiguous_reserve(phys_addr_t addr_limit);
  115. int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
  116. phys_addr_t limit, struct cma **res_cma, bool fixed);
  117. struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
  118. unsigned int order, bool no_warn);
  119. bool dma_release_from_contiguous(struct device *dev, struct page *pages,
  120. int count);
  121. struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
  122. void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
  123. void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size);
  124. #else /* CONFIG_DMA_CMA */
  125. static inline struct cma *dev_get_cma_area(struct device *dev)
  126. {
  127. return NULL;
  128. }
  129. static inline void dma_contiguous_reserve(phys_addr_t limit)
  130. {
  131. }
  132. static inline int dma_contiguous_reserve_area(phys_addr_t size,
  133. phys_addr_t base, phys_addr_t limit, struct cma **res_cma,
  134. bool fixed)
  135. {
  136. return -ENOSYS;
  137. }
  138. static inline struct page *dma_alloc_from_contiguous(struct device *dev,
  139. size_t count, unsigned int order, bool no_warn)
  140. {
  141. return NULL;
  142. }
  143. static inline bool dma_release_from_contiguous(struct device *dev,
  144. struct page *pages, int count)
  145. {
  146. return false;
  147. }
  148. /* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */
  149. static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
  150. gfp_t gfp)
  151. {
  152. return NULL;
  153. }
  154. static inline void dma_free_contiguous(struct device *dev, struct page *page,
  155. size_t size)
  156. {
  157. __free_pages(page, get_order(size));
  158. }
  159. #endif /* CONFIG_DMA_CMA*/
  160. #ifdef CONFIG_DMA_PERNUMA_CMA
  161. void dma_pernuma_cma_reserve(void);
  162. #else
  163. static inline void dma_pernuma_cma_reserve(void) { }
  164. #endif /* CONFIG_DMA_PERNUMA_CMA */
  165. #ifdef CONFIG_DMA_DECLARE_COHERENT
  166. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  167. dma_addr_t device_addr, size_t size);
  168. void dma_release_coherent_memory(struct device *dev);
  169. int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
  170. dma_addr_t *dma_handle, void **ret);
  171. int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
  172. int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
  173. void *cpu_addr, size_t size, int *ret);
  174. #else
  175. static inline int dma_declare_coherent_memory(struct device *dev,
  176. phys_addr_t phys_addr, dma_addr_t device_addr, size_t size)
  177. {
  178. return -ENOSYS;
  179. }
  180. #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0)
  181. #define dma_release_from_dev_coherent(dev, order, vaddr) (0)
  182. #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
  183. static inline void dma_release_coherent_memory(struct device *dev) { }
  184. #endif /* CONFIG_DMA_DECLARE_COHERENT */
  185. #ifdef CONFIG_DMA_GLOBAL_POOL
  186. void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
  187. dma_addr_t *dma_handle);
  188. int dma_release_from_global_coherent(int order, void *vaddr);
  189. int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
  190. size_t size, int *ret);
  191. int dma_init_global_coherent(phys_addr_t phys_addr, size_t size);
  192. #else
  193. static inline void *dma_alloc_from_global_coherent(struct device *dev,
  194. ssize_t size, dma_addr_t *dma_handle)
  195. {
  196. return NULL;
  197. }
  198. static inline int dma_release_from_global_coherent(int order, void *vaddr)
  199. {
  200. return 0;
  201. }
  202. static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
  203. void *cpu_addr, size_t size, int *ret)
  204. {
  205. return 0;
  206. }
  207. #endif /* CONFIG_DMA_GLOBAL_POOL */
  208. /*
  209. * This is the actual return value from the ->alloc_noncontiguous method.
  210. * The users of the DMA API should only care about the sg_table, but to make
  211. * the DMA-API internal vmaping and freeing easier we stash away the page
  212. * array as well (except for the fallback case). This can go away any time,
  213. * e.g. when a vmap-variant that takes a scatterlist comes along.
  214. */
  215. struct dma_sgt_handle {
  216. struct sg_table sgt;
  217. struct page **pages;
  218. };
  219. #define sgt_handle(sgt) \
  220. container_of((sgt), struct dma_sgt_handle, sgt)
  221. int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  222. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  223. unsigned long attrs);
  224. int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  225. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  226. unsigned long attrs);
  227. struct page *dma_common_alloc_pages(struct device *dev, size_t size,
  228. dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
  229. void dma_common_free_pages(struct device *dev, size_t size, struct page *vaddr,
  230. dma_addr_t dma_handle, enum dma_data_direction dir);
  231. struct page **dma_common_find_pages(void *cpu_addr);
  232. void *dma_common_contiguous_remap(struct page *page, size_t size, pgprot_t prot,
  233. const void *caller);
  234. void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot,
  235. const void *caller);
  236. void dma_common_free_remap(void *cpu_addr, size_t size);
  237. struct page *dma_alloc_from_pool(struct device *dev, size_t size,
  238. void **cpu_addr, gfp_t flags,
  239. bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t));
  240. bool dma_free_from_pool(struct device *dev, void *start, size_t size);
  241. int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
  242. dma_addr_t dma_start, u64 size);
  243. #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
  244. defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
  245. defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
  246. extern bool dma_default_coherent;
  247. static inline bool dev_is_dma_coherent(struct device *dev)
  248. {
  249. return dev->dma_coherent;
  250. }
  251. #else
  252. static inline bool dev_is_dma_coherent(struct device *dev)
  253. {
  254. return true;
  255. }
  256. #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
  257. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
  258. gfp_t gfp, unsigned long attrs);
  259. void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
  260. dma_addr_t dma_addr, unsigned long attrs);
  261. #ifdef CONFIG_MMU
  262. /*
  263. * Page protection so that devices that can't snoop CPU caches can use the
  264. * memory coherently. We default to pgprot_noncached which is usually used
  265. * for ioremap as a safe bet, but architectures can override this with less
  266. * strict semantics if possible.
  267. */
  268. #ifndef pgprot_dmacoherent
  269. #define pgprot_dmacoherent(prot) pgprot_noncached(prot)
  270. #endif
  271. /*
  272. * If there is no system cache pgprot, then fallback to dmacoherent
  273. * pgprot, as the expectation is that the device is not coherent.
  274. */
  275. #ifndef pgprot_syscached
  276. #define pgprot_syscached(prot) pgprot_dmacoherent(prot)
  277. #endif
  278. pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs);
  279. #else
  280. static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot,
  281. unsigned long attrs)
  282. {
  283. return prot; /* no protection bits supported without page tables */
  284. }
  285. #endif /* CONFIG_MMU */
  286. #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
  287. void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
  288. enum dma_data_direction dir);
  289. #else
  290. static inline void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
  291. enum dma_data_direction dir)
  292. {
  293. }
  294. #endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */
  295. #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
  296. void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
  297. enum dma_data_direction dir);
  298. #else
  299. static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
  300. enum dma_data_direction dir)
  301. {
  302. }
  303. #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
  304. #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
  305. void arch_sync_dma_for_cpu_all(void);
  306. #else
  307. static inline void arch_sync_dma_for_cpu_all(void)
  308. {
  309. }
  310. #endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
  311. #ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT
  312. void arch_dma_prep_coherent(struct page *page, size_t size);
  313. #else
  314. static inline void arch_dma_prep_coherent(struct page *page, size_t size)
  315. {
  316. }
  317. #endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */
  318. #ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN
  319. void arch_dma_mark_clean(phys_addr_t paddr, size_t size);
  320. #else
  321. static inline void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
  322. {
  323. }
  324. #endif /* ARCH_HAS_DMA_MARK_CLEAN */
  325. void *arch_dma_set_uncached(void *addr, size_t size);
  326. void arch_dma_clear_uncached(void *addr, size_t size);
  327. #ifdef CONFIG_ARCH_HAS_DMA_MAP_DIRECT
  328. bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr);
  329. bool arch_dma_unmap_page_direct(struct device *dev, dma_addr_t dma_handle);
  330. bool arch_dma_map_sg_direct(struct device *dev, struct scatterlist *sg,
  331. int nents);
  332. bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg,
  333. int nents);
  334. #else
  335. #define arch_dma_map_page_direct(d, a) (false)
  336. #define arch_dma_unmap_page_direct(d, a) (false)
  337. #define arch_dma_map_sg_direct(d, s, n) (false)
  338. #define arch_dma_unmap_sg_direct(d, s, n) (false)
  339. #endif
  340. #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
  341. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  342. const struct iommu_ops *iommu, bool coherent);
  343. #else
  344. static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
  345. u64 size, const struct iommu_ops *iommu, bool coherent)
  346. {
  347. }
  348. #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
  349. #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
  350. void arch_teardown_dma_ops(struct device *dev);
  351. #else
  352. static inline void arch_teardown_dma_ops(struct device *dev)
  353. {
  354. }
  355. #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
  356. #ifdef CONFIG_DMA_API_DEBUG
  357. void dma_debug_add_bus(struct bus_type *bus);
  358. void debug_dma_dump_mappings(struct device *dev);
  359. #else
  360. static inline void dma_debug_add_bus(struct bus_type *bus)
  361. {
  362. }
  363. static inline void debug_dma_dump_mappings(struct device *dev)
  364. {
  365. }
  366. #endif /* CONFIG_DMA_API_DEBUG */
  367. extern const struct dma_map_ops dma_dummy_ops;
  368. enum pci_p2pdma_map_type {
  369. /*
  370. * PCI_P2PDMA_MAP_UNKNOWN: Used internally for indicating the mapping
  371. * type hasn't been calculated yet. Functions that return this enum
  372. * never return this value.
  373. */
  374. PCI_P2PDMA_MAP_UNKNOWN = 0,
  375. /*
  376. * PCI_P2PDMA_MAP_NOT_SUPPORTED: Indicates the transaction will
  377. * traverse the host bridge and the host bridge is not in the
  378. * allowlist. DMA Mapping routines should return an error when
  379. * this is returned.
  380. */
  381. PCI_P2PDMA_MAP_NOT_SUPPORTED,
  382. /*
  383. * PCI_P2PDMA_BUS_ADDR: Indicates that two devices can talk to
  384. * each other directly through a PCI switch and the transaction will
  385. * not traverse the host bridge. Such a mapping should program
  386. * the DMA engine with PCI bus addresses.
  387. */
  388. PCI_P2PDMA_MAP_BUS_ADDR,
  389. /*
  390. * PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: Indicates two devices can talk
  391. * to each other, but the transaction traverses a host bridge on the
  392. * allowlist. In this case, a normal mapping either with CPU physical
  393. * addresses (in the case of dma-direct) or IOVA addresses (in the
  394. * case of IOMMUs) should be used to program the DMA engine.
  395. */
  396. PCI_P2PDMA_MAP_THRU_HOST_BRIDGE,
  397. };
  398. struct pci_p2pdma_map_state {
  399. struct dev_pagemap *pgmap;
  400. int map;
  401. u64 bus_off;
  402. };
  403. #ifdef CONFIG_PCI_P2PDMA
  404. enum pci_p2pdma_map_type
  405. pci_p2pdma_map_segment(struct pci_p2pdma_map_state *state, struct device *dev,
  406. struct scatterlist *sg);
  407. #else /* CONFIG_PCI_P2PDMA */
  408. static inline enum pci_p2pdma_map_type
  409. pci_p2pdma_map_segment(struct pci_p2pdma_map_state *state, struct device *dev,
  410. struct scatterlist *sg)
  411. {
  412. return PCI_P2PDMA_MAP_NOT_SUPPORTED;
  413. }
  414. #endif /* CONFIG_PCI_P2PDMA */
  415. #endif /* _LINUX_DMA_MAP_OPS_H */