dma-mapping.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_DMA_MAPPING_H
  3. #define _LINUX_DMA_MAPPING_H
  4. #include <linux/sizes.h>
  5. #include <linux/string.h>
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/dma-debug.h>
  9. #include <linux/dma-direction.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/bug.h>
  12. #include <linux/mem_encrypt.h>
  13. /**
  14. * List of possible attributes associated with a DMA mapping. The semantics
  15. * of each attribute should be defined in Documentation/DMA-attributes.txt.
  16. *
  17. * DMA_ATTR_WRITE_BARRIER: DMA to a memory region with this attribute
  18. * forces all pending DMA writes to complete.
  19. */
  20. #define DMA_ATTR_WRITE_BARRIER (1UL << 0)
  21. /*
  22. * DMA_ATTR_WEAK_ORDERING: Specifies that reads and writes to the mapping
  23. * may be weakly ordered, that is that reads and writes may pass each other.
  24. */
  25. #define DMA_ATTR_WEAK_ORDERING (1UL << 1)
  26. /*
  27. * DMA_ATTR_WRITE_COMBINE: Specifies that writes to the mapping may be
  28. * buffered to improve performance.
  29. */
  30. #define DMA_ATTR_WRITE_COMBINE (1UL << 2)
  31. /*
  32. * DMA_ATTR_NON_CONSISTENT: Lets the platform to choose to return either
  33. * consistent or non-consistent memory as it sees fit.
  34. */
  35. #define DMA_ATTR_NON_CONSISTENT (1UL << 3)
  36. /*
  37. * DMA_ATTR_NO_KERNEL_MAPPING: Lets the platform to avoid creating a kernel
  38. * virtual mapping for the allocated buffer.
  39. */
  40. #define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4)
  41. /*
  42. * DMA_ATTR_SKIP_CPU_SYNC: Allows platform code to skip synchronization of
  43. * the CPU cache for the given buffer assuming that it has been already
  44. * transferred to 'device' domain.
  45. */
  46. #define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5)
  47. /*
  48. * DMA_ATTR_FORCE_CONTIGUOUS: Forces contiguous allocation of the buffer
  49. * in physical memory.
  50. */
  51. #define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6)
  52. /*
  53. * DMA_ATTR_ALLOC_SINGLE_PAGES: This is a hint to the DMA-mapping subsystem
  54. * that it's probably not worth the time to try to allocate memory to in a way
  55. * that gives better TLB efficiency.
  56. */
  57. #define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7)
  58. /*
  59. * DMA_ATTR_NO_WARN: This tells the DMA-mapping subsystem to suppress
  60. * allocation failure reports (similarly to __GFP_NOWARN).
  61. */
  62. #define DMA_ATTR_NO_WARN (1UL << 8)
  63. /*
  64. * DMA_ATTR_PRIVILEGED: used to indicate that the buffer is fully
  65. * accessible at an elevated privilege level (and ideally inaccessible or
  66. * at least read-only at lesser-privileged levels).
  67. */
  68. #define DMA_ATTR_PRIVILEGED (1UL << 9)
  69. /*
  70. * A dma_addr_t can hold any valid DMA or bus address for the platform.
  71. * It can be given to a device to use as a DMA source or target. A CPU cannot
  72. * reference a dma_addr_t directly because there may be translation between
  73. * its physical address space and the bus address space.
  74. */
  75. struct dma_map_ops {
  76. void* (*alloc)(struct device *dev, size_t size,
  77. dma_addr_t *dma_handle, gfp_t gfp,
  78. unsigned long attrs);
  79. void (*free)(struct device *dev, size_t size,
  80. void *vaddr, dma_addr_t dma_handle,
  81. unsigned long attrs);
  82. int (*mmap)(struct device *, struct vm_area_struct *,
  83. void *, dma_addr_t, size_t,
  84. unsigned long attrs);
  85. int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
  86. dma_addr_t, size_t, unsigned long attrs);
  87. dma_addr_t (*map_page)(struct device *dev, struct page *page,
  88. unsigned long offset, size_t size,
  89. enum dma_data_direction dir,
  90. unsigned long attrs);
  91. void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
  92. size_t size, enum dma_data_direction dir,
  93. unsigned long attrs);
  94. /*
  95. * map_sg returns 0 on error and a value > 0 on success.
  96. * It should never return a value < 0.
  97. */
  98. int (*map_sg)(struct device *dev, struct scatterlist *sg,
  99. int nents, enum dma_data_direction dir,
  100. unsigned long attrs);
  101. void (*unmap_sg)(struct device *dev,
  102. struct scatterlist *sg, int nents,
  103. enum dma_data_direction dir,
  104. unsigned long attrs);
  105. dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
  106. size_t size, enum dma_data_direction dir,
  107. unsigned long attrs);
  108. void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
  109. size_t size, enum dma_data_direction dir,
  110. unsigned long attrs);
  111. void (*sync_single_for_cpu)(struct device *dev,
  112. dma_addr_t dma_handle, size_t size,
  113. enum dma_data_direction dir);
  114. void (*sync_single_for_device)(struct device *dev,
  115. dma_addr_t dma_handle, size_t size,
  116. enum dma_data_direction dir);
  117. void (*sync_sg_for_cpu)(struct device *dev,
  118. struct scatterlist *sg, int nents,
  119. enum dma_data_direction dir);
  120. void (*sync_sg_for_device)(struct device *dev,
  121. struct scatterlist *sg, int nents,
  122. enum dma_data_direction dir);
  123. void (*cache_sync)(struct device *dev, void *vaddr, size_t size,
  124. enum dma_data_direction direction);
  125. int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
  126. int (*dma_supported)(struct device *dev, u64 mask);
  127. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  128. u64 (*get_required_mask)(struct device *dev);
  129. #endif
  130. int is_phys;
  131. };
  132. extern const struct dma_map_ops dma_direct_ops;
  133. extern const struct dma_map_ops dma_virt_ops;
  134. #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
  135. #define DMA_MASK_NONE 0x0ULL
  136. static inline int valid_dma_direction(int dma_direction)
  137. {
  138. return ((dma_direction == DMA_BIDIRECTIONAL) ||
  139. (dma_direction == DMA_TO_DEVICE) ||
  140. (dma_direction == DMA_FROM_DEVICE));
  141. }
  142. static inline int is_device_dma_capable(struct device *dev)
  143. {
  144. return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
  145. }
  146. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  147. /*
  148. * These three functions are only for dma allocator.
  149. * Don't use them in device drivers.
  150. */
  151. int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
  152. dma_addr_t *dma_handle, void **ret);
  153. int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
  154. int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
  155. void *cpu_addr, size_t size, int *ret);
  156. void *dma_alloc_from_global_coherent(ssize_t size, dma_addr_t *dma_handle);
  157. int dma_release_from_global_coherent(int order, void *vaddr);
  158. int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
  159. size_t size, int *ret);
  160. #else
  161. #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0)
  162. #define dma_release_from_dev_coherent(dev, order, vaddr) (0)
  163. #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
  164. static inline void *dma_alloc_from_global_coherent(ssize_t size,
  165. dma_addr_t *dma_handle)
  166. {
  167. return NULL;
  168. }
  169. static inline int dma_release_from_global_coherent(int order, void *vaddr)
  170. {
  171. return 0;
  172. }
  173. static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
  174. void *cpu_addr, size_t size,
  175. int *ret)
  176. {
  177. return 0;
  178. }
  179. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  180. #ifdef CONFIG_HAS_DMA
  181. #include <asm/dma-mapping.h>
  182. static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
  183. {
  184. if (dev && dev->dma_ops)
  185. return dev->dma_ops;
  186. return get_arch_dma_ops(dev ? dev->bus : NULL);
  187. }
  188. static inline void set_dma_ops(struct device *dev,
  189. const struct dma_map_ops *dma_ops)
  190. {
  191. dev->dma_ops = dma_ops;
  192. }
  193. #else
  194. /*
  195. * Define the dma api to allow compilation but not linking of
  196. * dma dependent code. Code that depends on the dma-mapping
  197. * API needs to set 'depends on HAS_DMA' in its Kconfig
  198. */
  199. extern const struct dma_map_ops bad_dma_ops;
  200. static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
  201. {
  202. return &bad_dma_ops;
  203. }
  204. #endif
  205. static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
  206. size_t size,
  207. enum dma_data_direction dir,
  208. unsigned long attrs)
  209. {
  210. const struct dma_map_ops *ops = get_dma_ops(dev);
  211. dma_addr_t addr;
  212. BUG_ON(!valid_dma_direction(dir));
  213. addr = ops->map_page(dev, virt_to_page(ptr),
  214. offset_in_page(ptr), size,
  215. dir, attrs);
  216. debug_dma_map_page(dev, virt_to_page(ptr),
  217. offset_in_page(ptr), size,
  218. dir, addr, true);
  219. return addr;
  220. }
  221. static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
  222. size_t size,
  223. enum dma_data_direction dir,
  224. unsigned long attrs)
  225. {
  226. const struct dma_map_ops *ops = get_dma_ops(dev);
  227. BUG_ON(!valid_dma_direction(dir));
  228. if (ops->unmap_page)
  229. ops->unmap_page(dev, addr, size, dir, attrs);
  230. debug_dma_unmap_page(dev, addr, size, dir, true);
  231. }
  232. /*
  233. * dma_maps_sg_attrs returns 0 on error and > 0 on success.
  234. * It should never return a value < 0.
  235. */
  236. static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  237. int nents, enum dma_data_direction dir,
  238. unsigned long attrs)
  239. {
  240. const struct dma_map_ops *ops = get_dma_ops(dev);
  241. int ents;
  242. BUG_ON(!valid_dma_direction(dir));
  243. ents = ops->map_sg(dev, sg, nents, dir, attrs);
  244. BUG_ON(ents < 0);
  245. debug_dma_map_sg(dev, sg, nents, ents, dir);
  246. return ents;
  247. }
  248. static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  249. int nents, enum dma_data_direction dir,
  250. unsigned long attrs)
  251. {
  252. const struct dma_map_ops *ops = get_dma_ops(dev);
  253. BUG_ON(!valid_dma_direction(dir));
  254. debug_dma_unmap_sg(dev, sg, nents, dir);
  255. if (ops->unmap_sg)
  256. ops->unmap_sg(dev, sg, nents, dir, attrs);
  257. }
  258. static inline dma_addr_t dma_map_page_attrs(struct device *dev,
  259. struct page *page,
  260. size_t offset, size_t size,
  261. enum dma_data_direction dir,
  262. unsigned long attrs)
  263. {
  264. const struct dma_map_ops *ops = get_dma_ops(dev);
  265. dma_addr_t addr;
  266. BUG_ON(!valid_dma_direction(dir));
  267. addr = ops->map_page(dev, page, offset, size, dir, attrs);
  268. debug_dma_map_page(dev, page, offset, size, dir, addr, false);
  269. return addr;
  270. }
  271. static inline void dma_unmap_page_attrs(struct device *dev,
  272. dma_addr_t addr, size_t size,
  273. enum dma_data_direction dir,
  274. unsigned long attrs)
  275. {
  276. const struct dma_map_ops *ops = get_dma_ops(dev);
  277. BUG_ON(!valid_dma_direction(dir));
  278. if (ops->unmap_page)
  279. ops->unmap_page(dev, addr, size, dir, attrs);
  280. debug_dma_unmap_page(dev, addr, size, dir, false);
  281. }
  282. static inline dma_addr_t dma_map_resource(struct device *dev,
  283. phys_addr_t phys_addr,
  284. size_t size,
  285. enum dma_data_direction dir,
  286. unsigned long attrs)
  287. {
  288. const struct dma_map_ops *ops = get_dma_ops(dev);
  289. dma_addr_t addr;
  290. BUG_ON(!valid_dma_direction(dir));
  291. /* Don't allow RAM to be mapped */
  292. BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
  293. addr = phys_addr;
  294. if (ops->map_resource)
  295. addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
  296. debug_dma_map_resource(dev, phys_addr, size, dir, addr);
  297. return addr;
  298. }
  299. static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
  300. size_t size, enum dma_data_direction dir,
  301. unsigned long attrs)
  302. {
  303. const struct dma_map_ops *ops = get_dma_ops(dev);
  304. BUG_ON(!valid_dma_direction(dir));
  305. if (ops->unmap_resource)
  306. ops->unmap_resource(dev, addr, size, dir, attrs);
  307. debug_dma_unmap_resource(dev, addr, size, dir);
  308. }
  309. static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  310. size_t size,
  311. enum dma_data_direction dir)
  312. {
  313. const struct dma_map_ops *ops = get_dma_ops(dev);
  314. BUG_ON(!valid_dma_direction(dir));
  315. if (ops->sync_single_for_cpu)
  316. ops->sync_single_for_cpu(dev, addr, size, dir);
  317. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  318. }
  319. static inline void dma_sync_single_for_device(struct device *dev,
  320. dma_addr_t addr, size_t size,
  321. enum dma_data_direction dir)
  322. {
  323. const struct dma_map_ops *ops = get_dma_ops(dev);
  324. BUG_ON(!valid_dma_direction(dir));
  325. if (ops->sync_single_for_device)
  326. ops->sync_single_for_device(dev, addr, size, dir);
  327. debug_dma_sync_single_for_device(dev, addr, size, dir);
  328. }
  329. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  330. dma_addr_t addr,
  331. unsigned long offset,
  332. size_t size,
  333. enum dma_data_direction dir)
  334. {
  335. const struct dma_map_ops *ops = get_dma_ops(dev);
  336. BUG_ON(!valid_dma_direction(dir));
  337. if (ops->sync_single_for_cpu)
  338. ops->sync_single_for_cpu(dev, addr + offset, size, dir);
  339. debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
  340. }
  341. static inline void dma_sync_single_range_for_device(struct device *dev,
  342. dma_addr_t addr,
  343. unsigned long offset,
  344. size_t size,
  345. enum dma_data_direction dir)
  346. {
  347. const struct dma_map_ops *ops = get_dma_ops(dev);
  348. BUG_ON(!valid_dma_direction(dir));
  349. if (ops->sync_single_for_device)
  350. ops->sync_single_for_device(dev, addr + offset, size, dir);
  351. debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
  352. }
  353. static inline void
  354. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  355. int nelems, enum dma_data_direction dir)
  356. {
  357. const struct dma_map_ops *ops = get_dma_ops(dev);
  358. BUG_ON(!valid_dma_direction(dir));
  359. if (ops->sync_sg_for_cpu)
  360. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  361. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  362. }
  363. static inline void
  364. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  365. int nelems, enum dma_data_direction dir)
  366. {
  367. const struct dma_map_ops *ops = get_dma_ops(dev);
  368. BUG_ON(!valid_dma_direction(dir));
  369. if (ops->sync_sg_for_device)
  370. ops->sync_sg_for_device(dev, sg, nelems, dir);
  371. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  372. }
  373. #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
  374. #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
  375. #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
  376. #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
  377. #define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
  378. #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
  379. static inline void
  380. dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  381. enum dma_data_direction dir)
  382. {
  383. const struct dma_map_ops *ops = get_dma_ops(dev);
  384. BUG_ON(!valid_dma_direction(dir));
  385. if (ops->cache_sync)
  386. ops->cache_sync(dev, vaddr, size, dir);
  387. }
  388. extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  389. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  390. void *dma_common_contiguous_remap(struct page *page, size_t size,
  391. unsigned long vm_flags,
  392. pgprot_t prot, const void *caller);
  393. void *dma_common_pages_remap(struct page **pages, size_t size,
  394. unsigned long vm_flags, pgprot_t prot,
  395. const void *caller);
  396. void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
  397. /**
  398. * dma_mmap_attrs - map a coherent DMA allocation into user space
  399. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  400. * @vma: vm_area_struct describing requested user mapping
  401. * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
  402. * @handle: device-view address returned from dma_alloc_attrs
  403. * @size: size of memory originally requested in dma_alloc_attrs
  404. * @attrs: attributes of mapping properties requested in dma_alloc_attrs
  405. *
  406. * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
  407. * into user space. The coherent DMA buffer must not be freed by the
  408. * driver until the user space mapping has been released.
  409. */
  410. static inline int
  411. dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
  412. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  413. {
  414. const struct dma_map_ops *ops = get_dma_ops(dev);
  415. BUG_ON(!ops);
  416. if (ops->mmap)
  417. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  418. return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
  419. }
  420. #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
  421. int
  422. dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  423. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  424. static inline int
  425. dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
  426. dma_addr_t dma_addr, size_t size,
  427. unsigned long attrs)
  428. {
  429. const struct dma_map_ops *ops = get_dma_ops(dev);
  430. BUG_ON(!ops);
  431. if (ops->get_sgtable)
  432. return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
  433. attrs);
  434. return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  435. }
  436. #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
  437. #ifndef arch_dma_alloc_attrs
  438. #define arch_dma_alloc_attrs(dev, flag) (true)
  439. #endif
  440. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  441. dma_addr_t *dma_handle, gfp_t flag,
  442. unsigned long attrs)
  443. {
  444. const struct dma_map_ops *ops = get_dma_ops(dev);
  445. void *cpu_addr;
  446. BUG_ON(!ops);
  447. WARN_ON_ONCE(dev && !dev->coherent_dma_mask);
  448. if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
  449. return cpu_addr;
  450. /*
  451. * Let the implementation decide on the zone to allocate from, and
  452. * decide on the way of zeroing the memory given that the memory
  453. * returned should always be zeroed.
  454. */
  455. flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM | __GFP_ZERO);
  456. if (!arch_dma_alloc_attrs(&dev, &flag))
  457. return NULL;
  458. if (!ops->alloc)
  459. return NULL;
  460. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  461. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  462. return cpu_addr;
  463. }
  464. static inline void dma_free_attrs(struct device *dev, size_t size,
  465. void *cpu_addr, dma_addr_t dma_handle,
  466. unsigned long attrs)
  467. {
  468. const struct dma_map_ops *ops = get_dma_ops(dev);
  469. BUG_ON(!ops);
  470. WARN_ON(irqs_disabled());
  471. if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
  472. return;
  473. if (!ops->free || !cpu_addr)
  474. return;
  475. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  476. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  477. }
  478. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  479. dma_addr_t *dma_handle, gfp_t flag)
  480. {
  481. return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
  482. }
  483. static inline void dma_free_coherent(struct device *dev, size_t size,
  484. void *cpu_addr, dma_addr_t dma_handle)
  485. {
  486. return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
  487. }
  488. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  489. {
  490. const struct dma_map_ops *ops = get_dma_ops(dev);
  491. debug_dma_mapping_error(dev, dma_addr);
  492. if (ops->mapping_error)
  493. return ops->mapping_error(dev, dma_addr);
  494. return 0;
  495. }
  496. /*
  497. * This is a hack for the legacy x86 forbid_dac and iommu_sac_force. Please
  498. * don't use this is new code.
  499. */
  500. #ifndef arch_dma_supported
  501. #define arch_dma_supported(dev, mask) (1)
  502. #endif
  503. static inline void dma_check_mask(struct device *dev, u64 mask)
  504. {
  505. if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1)))
  506. dev_warn(dev, "SME is active, device will require DMA bounce buffers\n");
  507. }
  508. static inline int dma_supported(struct device *dev, u64 mask)
  509. {
  510. const struct dma_map_ops *ops = get_dma_ops(dev);
  511. if (!ops)
  512. return 0;
  513. if (!arch_dma_supported(dev, mask))
  514. return 0;
  515. if (!ops->dma_supported)
  516. return 1;
  517. return ops->dma_supported(dev, mask);
  518. }
  519. #ifndef HAVE_ARCH_DMA_SET_MASK
  520. static inline int dma_set_mask(struct device *dev, u64 mask)
  521. {
  522. if (!dev->dma_mask || !dma_supported(dev, mask))
  523. return -EIO;
  524. dma_check_mask(dev, mask);
  525. *dev->dma_mask = mask;
  526. return 0;
  527. }
  528. #endif
  529. static inline u64 dma_get_mask(struct device *dev)
  530. {
  531. if (dev && dev->dma_mask && *dev->dma_mask)
  532. return *dev->dma_mask;
  533. return DMA_BIT_MASK(32);
  534. }
  535. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  536. int dma_set_coherent_mask(struct device *dev, u64 mask);
  537. #else
  538. static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
  539. {
  540. if (!dma_supported(dev, mask))
  541. return -EIO;
  542. dma_check_mask(dev, mask);
  543. dev->coherent_dma_mask = mask;
  544. return 0;
  545. }
  546. #endif
  547. /*
  548. * Set both the DMA mask and the coherent DMA mask to the same thing.
  549. * Note that we don't check the return value from dma_set_coherent_mask()
  550. * as the DMA API guarantees that the coherent DMA mask can be set to
  551. * the same or smaller than the streaming DMA mask.
  552. */
  553. static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
  554. {
  555. int rc = dma_set_mask(dev, mask);
  556. if (rc == 0)
  557. dma_set_coherent_mask(dev, mask);
  558. return rc;
  559. }
  560. /*
  561. * Similar to the above, except it deals with the case where the device
  562. * does not have dev->dma_mask appropriately setup.
  563. */
  564. static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
  565. {
  566. dev->dma_mask = &dev->coherent_dma_mask;
  567. return dma_set_mask_and_coherent(dev, mask);
  568. }
  569. extern u64 dma_get_required_mask(struct device *dev);
  570. #ifndef arch_setup_dma_ops
  571. static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
  572. u64 size, const struct iommu_ops *iommu,
  573. bool coherent) { }
  574. #endif
  575. #ifndef arch_teardown_dma_ops
  576. static inline void arch_teardown_dma_ops(struct device *dev) { }
  577. #endif
  578. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  579. {
  580. if (dev->dma_parms && dev->dma_parms->max_segment_size)
  581. return dev->dma_parms->max_segment_size;
  582. return SZ_64K;
  583. }
  584. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  585. unsigned int size)
  586. {
  587. if (dev->dma_parms) {
  588. dev->dma_parms->max_segment_size = size;
  589. return 0;
  590. }
  591. return -EIO;
  592. }
  593. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  594. {
  595. if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
  596. return dev->dma_parms->segment_boundary_mask;
  597. return DMA_BIT_MASK(32);
  598. }
  599. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  600. {
  601. if (dev->dma_parms) {
  602. dev->dma_parms->segment_boundary_mask = mask;
  603. return 0;
  604. }
  605. return -EIO;
  606. }
  607. #ifndef dma_max_pfn
  608. static inline unsigned long dma_max_pfn(struct device *dev)
  609. {
  610. return (*dev->dma_mask >> PAGE_SHIFT) + dev->dma_pfn_offset;
  611. }
  612. #endif
  613. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  614. dma_addr_t *dma_handle, gfp_t flag)
  615. {
  616. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  617. flag | __GFP_ZERO);
  618. return ret;
  619. }
  620. static inline int dma_get_cache_alignment(void)
  621. {
  622. #ifdef ARCH_DMA_MINALIGN
  623. return ARCH_DMA_MINALIGN;
  624. #endif
  625. return 1;
  626. }
  627. /* flags for the coherent memory api */
  628. #define DMA_MEMORY_EXCLUSIVE 0x01
  629. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  630. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  631. dma_addr_t device_addr, size_t size, int flags);
  632. void dma_release_declared_memory(struct device *dev);
  633. void *dma_mark_declared_memory_occupied(struct device *dev,
  634. dma_addr_t device_addr, size_t size);
  635. #else
  636. static inline int
  637. dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  638. dma_addr_t device_addr, size_t size, int flags)
  639. {
  640. return -ENOSYS;
  641. }
  642. static inline void
  643. dma_release_declared_memory(struct device *dev)
  644. {
  645. }
  646. static inline void *
  647. dma_mark_declared_memory_occupied(struct device *dev,
  648. dma_addr_t device_addr, size_t size)
  649. {
  650. return ERR_PTR(-EBUSY);
  651. }
  652. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  653. #ifdef CONFIG_HAS_DMA
  654. int dma_configure(struct device *dev);
  655. void dma_deconfigure(struct device *dev);
  656. #else
  657. static inline int dma_configure(struct device *dev)
  658. {
  659. return 0;
  660. }
  661. static inline void dma_deconfigure(struct device *dev) {}
  662. #endif
  663. /*
  664. * Managed DMA API
  665. */
  666. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  667. dma_addr_t *dma_handle, gfp_t gfp);
  668. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  669. dma_addr_t dma_handle);
  670. extern void *dmam_alloc_attrs(struct device *dev, size_t size,
  671. dma_addr_t *dma_handle, gfp_t gfp,
  672. unsigned long attrs);
  673. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  674. extern int dmam_declare_coherent_memory(struct device *dev,
  675. phys_addr_t phys_addr,
  676. dma_addr_t device_addr, size_t size,
  677. int flags);
  678. extern void dmam_release_declared_memory(struct device *dev);
  679. #else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  680. static inline int dmam_declare_coherent_memory(struct device *dev,
  681. phys_addr_t phys_addr, dma_addr_t device_addr,
  682. size_t size, gfp_t gfp)
  683. {
  684. return 0;
  685. }
  686. static inline void dmam_release_declared_memory(struct device *dev)
  687. {
  688. }
  689. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  690. static inline void *dma_alloc_wc(struct device *dev, size_t size,
  691. dma_addr_t *dma_addr, gfp_t gfp)
  692. {
  693. return dma_alloc_attrs(dev, size, dma_addr, gfp,
  694. DMA_ATTR_WRITE_COMBINE);
  695. }
  696. #ifndef dma_alloc_writecombine
  697. #define dma_alloc_writecombine dma_alloc_wc
  698. #endif
  699. static inline void dma_free_wc(struct device *dev, size_t size,
  700. void *cpu_addr, dma_addr_t dma_addr)
  701. {
  702. return dma_free_attrs(dev, size, cpu_addr, dma_addr,
  703. DMA_ATTR_WRITE_COMBINE);
  704. }
  705. #ifndef dma_free_writecombine
  706. #define dma_free_writecombine dma_free_wc
  707. #endif
  708. static inline int dma_mmap_wc(struct device *dev,
  709. struct vm_area_struct *vma,
  710. void *cpu_addr, dma_addr_t dma_addr,
  711. size_t size)
  712. {
  713. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
  714. DMA_ATTR_WRITE_COMBINE);
  715. }
  716. #ifndef dma_mmap_writecombine
  717. #define dma_mmap_writecombine dma_mmap_wc
  718. #endif
  719. #if defined(CONFIG_NEED_DMA_MAP_STATE) || defined(CONFIG_DMA_API_DEBUG)
  720. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  721. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  722. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  723. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  724. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  725. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  726. #else
  727. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  728. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  729. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  730. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  731. #define dma_unmap_len(PTR, LEN_NAME) (0)
  732. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  733. #endif
  734. #endif