dma-mapping.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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_noop_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. if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
  448. return cpu_addr;
  449. if (!arch_dma_alloc_attrs(&dev, &flag))
  450. return NULL;
  451. if (!ops->alloc)
  452. return NULL;
  453. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  454. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  455. return cpu_addr;
  456. }
  457. static inline void dma_free_attrs(struct device *dev, size_t size,
  458. void *cpu_addr, dma_addr_t dma_handle,
  459. unsigned long attrs)
  460. {
  461. const struct dma_map_ops *ops = get_dma_ops(dev);
  462. BUG_ON(!ops);
  463. WARN_ON(irqs_disabled());
  464. if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
  465. return;
  466. if (!ops->free || !cpu_addr)
  467. return;
  468. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  469. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  470. }
  471. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  472. dma_addr_t *dma_handle, gfp_t flag)
  473. {
  474. return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
  475. }
  476. static inline void dma_free_coherent(struct device *dev, size_t size,
  477. void *cpu_addr, dma_addr_t dma_handle)
  478. {
  479. return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
  480. }
  481. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  482. {
  483. const struct dma_map_ops *ops = get_dma_ops(dev);
  484. debug_dma_mapping_error(dev, dma_addr);
  485. if (ops->mapping_error)
  486. return ops->mapping_error(dev, dma_addr);
  487. return 0;
  488. }
  489. static inline void dma_check_mask(struct device *dev, u64 mask)
  490. {
  491. if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1)))
  492. dev_warn(dev, "SME is active, device will require DMA bounce buffers\n");
  493. }
  494. static inline int dma_supported(struct device *dev, u64 mask)
  495. {
  496. const struct dma_map_ops *ops = get_dma_ops(dev);
  497. if (!ops)
  498. return 0;
  499. if (!ops->dma_supported)
  500. return 1;
  501. return ops->dma_supported(dev, mask);
  502. }
  503. #ifndef HAVE_ARCH_DMA_SET_MASK
  504. static inline int dma_set_mask(struct device *dev, u64 mask)
  505. {
  506. if (!dev->dma_mask || !dma_supported(dev, mask))
  507. return -EIO;
  508. dma_check_mask(dev, mask);
  509. *dev->dma_mask = mask;
  510. return 0;
  511. }
  512. #endif
  513. static inline u64 dma_get_mask(struct device *dev)
  514. {
  515. if (dev && dev->dma_mask && *dev->dma_mask)
  516. return *dev->dma_mask;
  517. return DMA_BIT_MASK(32);
  518. }
  519. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  520. int dma_set_coherent_mask(struct device *dev, u64 mask);
  521. #else
  522. static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
  523. {
  524. if (!dma_supported(dev, mask))
  525. return -EIO;
  526. dma_check_mask(dev, mask);
  527. dev->coherent_dma_mask = mask;
  528. return 0;
  529. }
  530. #endif
  531. /*
  532. * Set both the DMA mask and the coherent DMA mask to the same thing.
  533. * Note that we don't check the return value from dma_set_coherent_mask()
  534. * as the DMA API guarantees that the coherent DMA mask can be set to
  535. * the same or smaller than the streaming DMA mask.
  536. */
  537. static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
  538. {
  539. int rc = dma_set_mask(dev, mask);
  540. if (rc == 0)
  541. dma_set_coherent_mask(dev, mask);
  542. return rc;
  543. }
  544. /*
  545. * Similar to the above, except it deals with the case where the device
  546. * does not have dev->dma_mask appropriately setup.
  547. */
  548. static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
  549. {
  550. dev->dma_mask = &dev->coherent_dma_mask;
  551. return dma_set_mask_and_coherent(dev, mask);
  552. }
  553. extern u64 dma_get_required_mask(struct device *dev);
  554. #ifndef arch_setup_dma_ops
  555. static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
  556. u64 size, const struct iommu_ops *iommu,
  557. bool coherent) { }
  558. #endif
  559. #ifndef arch_teardown_dma_ops
  560. static inline void arch_teardown_dma_ops(struct device *dev) { }
  561. #endif
  562. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  563. {
  564. if (dev->dma_parms && dev->dma_parms->max_segment_size)
  565. return dev->dma_parms->max_segment_size;
  566. return SZ_64K;
  567. }
  568. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  569. unsigned int size)
  570. {
  571. if (dev->dma_parms) {
  572. dev->dma_parms->max_segment_size = size;
  573. return 0;
  574. }
  575. return -EIO;
  576. }
  577. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  578. {
  579. if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
  580. return dev->dma_parms->segment_boundary_mask;
  581. return DMA_BIT_MASK(32);
  582. }
  583. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  584. {
  585. if (dev->dma_parms) {
  586. dev->dma_parms->segment_boundary_mask = mask;
  587. return 0;
  588. }
  589. return -EIO;
  590. }
  591. #ifndef dma_max_pfn
  592. static inline unsigned long dma_max_pfn(struct device *dev)
  593. {
  594. return *dev->dma_mask >> PAGE_SHIFT;
  595. }
  596. #endif
  597. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  598. dma_addr_t *dma_handle, gfp_t flag)
  599. {
  600. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  601. flag | __GFP_ZERO);
  602. return ret;
  603. }
  604. #ifdef CONFIG_HAS_DMA
  605. static inline int dma_get_cache_alignment(void)
  606. {
  607. #ifdef ARCH_DMA_MINALIGN
  608. return ARCH_DMA_MINALIGN;
  609. #endif
  610. return 1;
  611. }
  612. #endif
  613. /* flags for the coherent memory api */
  614. #define DMA_MEMORY_EXCLUSIVE 0x01
  615. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  616. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  617. dma_addr_t device_addr, size_t size, int flags);
  618. void dma_release_declared_memory(struct device *dev);
  619. void *dma_mark_declared_memory_occupied(struct device *dev,
  620. dma_addr_t device_addr, size_t size);
  621. #else
  622. static inline int
  623. dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  624. dma_addr_t device_addr, size_t size, int flags)
  625. {
  626. return -ENOSYS;
  627. }
  628. static inline void
  629. dma_release_declared_memory(struct device *dev)
  630. {
  631. }
  632. static inline void *
  633. dma_mark_declared_memory_occupied(struct device *dev,
  634. dma_addr_t device_addr, size_t size)
  635. {
  636. return ERR_PTR(-EBUSY);
  637. }
  638. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  639. #ifdef CONFIG_HAS_DMA
  640. int dma_configure(struct device *dev);
  641. void dma_deconfigure(struct device *dev);
  642. #else
  643. static inline int dma_configure(struct device *dev)
  644. {
  645. return 0;
  646. }
  647. static inline void dma_deconfigure(struct device *dev) {}
  648. #endif
  649. /*
  650. * Managed DMA API
  651. */
  652. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  653. dma_addr_t *dma_handle, gfp_t gfp);
  654. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  655. dma_addr_t dma_handle);
  656. extern void *dmam_alloc_attrs(struct device *dev, size_t size,
  657. dma_addr_t *dma_handle, gfp_t gfp,
  658. unsigned long attrs);
  659. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  660. extern int dmam_declare_coherent_memory(struct device *dev,
  661. phys_addr_t phys_addr,
  662. dma_addr_t device_addr, size_t size,
  663. int flags);
  664. extern void dmam_release_declared_memory(struct device *dev);
  665. #else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  666. static inline int dmam_declare_coherent_memory(struct device *dev,
  667. phys_addr_t phys_addr, dma_addr_t device_addr,
  668. size_t size, gfp_t gfp)
  669. {
  670. return 0;
  671. }
  672. static inline void dmam_release_declared_memory(struct device *dev)
  673. {
  674. }
  675. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  676. static inline void *dma_alloc_wc(struct device *dev, size_t size,
  677. dma_addr_t *dma_addr, gfp_t gfp)
  678. {
  679. return dma_alloc_attrs(dev, size, dma_addr, gfp,
  680. DMA_ATTR_WRITE_COMBINE);
  681. }
  682. #ifndef dma_alloc_writecombine
  683. #define dma_alloc_writecombine dma_alloc_wc
  684. #endif
  685. static inline void dma_free_wc(struct device *dev, size_t size,
  686. void *cpu_addr, dma_addr_t dma_addr)
  687. {
  688. return dma_free_attrs(dev, size, cpu_addr, dma_addr,
  689. DMA_ATTR_WRITE_COMBINE);
  690. }
  691. #ifndef dma_free_writecombine
  692. #define dma_free_writecombine dma_free_wc
  693. #endif
  694. static inline int dma_mmap_wc(struct device *dev,
  695. struct vm_area_struct *vma,
  696. void *cpu_addr, dma_addr_t dma_addr,
  697. size_t size)
  698. {
  699. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
  700. DMA_ATTR_WRITE_COMBINE);
  701. }
  702. #ifndef dma_mmap_writecombine
  703. #define dma_mmap_writecombine dma_mmap_wc
  704. #endif
  705. #if defined(CONFIG_NEED_DMA_MAP_STATE) || defined(CONFIG_DMA_API_DEBUG)
  706. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  707. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  708. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  709. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  710. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  711. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  712. #else
  713. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  714. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  715. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  716. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  717. #define dma_unmap_len(PTR, LEN_NAME) (0)
  718. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  719. #endif
  720. #endif