dma-mapping.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. };
  131. extern const struct dma_map_ops dma_direct_ops;
  132. extern const struct dma_map_ops dma_noncoherent_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. bool zero);
  154. int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
  155. int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
  156. void *cpu_addr, size_t size, int *ret);
  157. void *dma_alloc_from_global_coherent(ssize_t size, dma_addr_t *dma_handle);
  158. int dma_release_from_global_coherent(int order, void *vaddr);
  159. int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
  160. size_t size, int *ret);
  161. #else
  162. #define dma_alloc_from_dev_coherent(dev, size, handle, ret, zero) (0)
  163. #define dma_release_from_dev_coherent(dev, order, vaddr) (0)
  164. #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
  165. static inline void *dma_alloc_from_global_coherent(ssize_t size,
  166. dma_addr_t *dma_handle)
  167. {
  168. return NULL;
  169. }
  170. static inline int dma_release_from_global_coherent(int order, void *vaddr)
  171. {
  172. return 0;
  173. }
  174. static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
  175. void *cpu_addr, size_t size,
  176. int *ret)
  177. {
  178. return 0;
  179. }
  180. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  181. #ifdef CONFIG_HAS_DMA
  182. #include <asm/dma-mapping.h>
  183. static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
  184. {
  185. if (dev && dev->dma_ops)
  186. return dev->dma_ops;
  187. return get_arch_dma_ops(dev ? dev->bus : NULL);
  188. }
  189. static inline void set_dma_ops(struct device *dev,
  190. const struct dma_map_ops *dma_ops)
  191. {
  192. dev->dma_ops = dma_ops;
  193. }
  194. #else
  195. /*
  196. * Define the dma api to allow compilation of dma dependent code.
  197. * Code that depends on the dma-mapping API needs to set 'depends on HAS_DMA'
  198. * in its Kconfig, unless it already depends on <something> || COMPILE_TEST,
  199. * where <something> guarantuees the availability of the dma-mapping API.
  200. */
  201. static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
  202. {
  203. return NULL;
  204. }
  205. #endif
  206. static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
  207. size_t size,
  208. enum dma_data_direction dir,
  209. unsigned long attrs)
  210. {
  211. const struct dma_map_ops *ops = get_dma_ops(dev);
  212. dma_addr_t addr;
  213. BUG_ON(!valid_dma_direction(dir));
  214. addr = ops->map_page(dev, virt_to_page(ptr),
  215. offset_in_page(ptr), size,
  216. dir, attrs);
  217. debug_dma_map_page(dev, virt_to_page(ptr),
  218. offset_in_page(ptr), size,
  219. dir, addr, true);
  220. return addr;
  221. }
  222. static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
  223. size_t size,
  224. enum dma_data_direction dir,
  225. unsigned long attrs)
  226. {
  227. const struct dma_map_ops *ops = get_dma_ops(dev);
  228. BUG_ON(!valid_dma_direction(dir));
  229. if (ops->unmap_page)
  230. ops->unmap_page(dev, addr, size, dir, attrs);
  231. debug_dma_unmap_page(dev, addr, size, dir, true);
  232. }
  233. /*
  234. * dma_maps_sg_attrs returns 0 on error and > 0 on success.
  235. * It should never return a value < 0.
  236. */
  237. static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  238. int nents, enum dma_data_direction dir,
  239. unsigned long attrs)
  240. {
  241. const struct dma_map_ops *ops = get_dma_ops(dev);
  242. int ents;
  243. BUG_ON(!valid_dma_direction(dir));
  244. ents = ops->map_sg(dev, sg, nents, dir, attrs);
  245. BUG_ON(ents < 0);
  246. debug_dma_map_sg(dev, sg, nents, ents, dir);
  247. return ents;
  248. }
  249. static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  250. int nents, enum dma_data_direction dir,
  251. unsigned long attrs)
  252. {
  253. const struct dma_map_ops *ops = get_dma_ops(dev);
  254. BUG_ON(!valid_dma_direction(dir));
  255. debug_dma_unmap_sg(dev, sg, nents, dir);
  256. if (ops->unmap_sg)
  257. ops->unmap_sg(dev, sg, nents, dir, attrs);
  258. }
  259. static inline dma_addr_t dma_map_page_attrs(struct device *dev,
  260. struct page *page,
  261. size_t offset, size_t size,
  262. enum dma_data_direction dir,
  263. unsigned long attrs)
  264. {
  265. const struct dma_map_ops *ops = get_dma_ops(dev);
  266. dma_addr_t addr;
  267. BUG_ON(!valid_dma_direction(dir));
  268. addr = ops->map_page(dev, page, offset, size, dir, attrs);
  269. debug_dma_map_page(dev, page, offset, size, dir, addr, false);
  270. return addr;
  271. }
  272. static inline void dma_unmap_page_attrs(struct device *dev,
  273. dma_addr_t addr, size_t size,
  274. enum dma_data_direction dir,
  275. unsigned long attrs)
  276. {
  277. const struct dma_map_ops *ops = get_dma_ops(dev);
  278. BUG_ON(!valid_dma_direction(dir));
  279. if (ops->unmap_page)
  280. ops->unmap_page(dev, addr, size, dir, attrs);
  281. debug_dma_unmap_page(dev, addr, size, dir, false);
  282. }
  283. static inline dma_addr_t dma_map_resource(struct device *dev,
  284. phys_addr_t phys_addr,
  285. size_t size,
  286. enum dma_data_direction dir,
  287. unsigned long attrs)
  288. {
  289. const struct dma_map_ops *ops = get_dma_ops(dev);
  290. dma_addr_t addr;
  291. BUG_ON(!valid_dma_direction(dir));
  292. /* Don't allow RAM to be mapped */
  293. BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
  294. addr = phys_addr;
  295. if (ops->map_resource)
  296. addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
  297. debug_dma_map_resource(dev, phys_addr, size, dir, addr);
  298. return addr;
  299. }
  300. static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
  301. size_t size, enum dma_data_direction dir,
  302. unsigned long attrs)
  303. {
  304. const struct dma_map_ops *ops = get_dma_ops(dev);
  305. BUG_ON(!valid_dma_direction(dir));
  306. if (ops->unmap_resource)
  307. ops->unmap_resource(dev, addr, size, dir, attrs);
  308. debug_dma_unmap_resource(dev, addr, size, dir);
  309. }
  310. static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  311. size_t size,
  312. enum dma_data_direction dir)
  313. {
  314. const struct dma_map_ops *ops = get_dma_ops(dev);
  315. BUG_ON(!valid_dma_direction(dir));
  316. if (ops->sync_single_for_cpu)
  317. ops->sync_single_for_cpu(dev, addr, size, dir);
  318. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  319. }
  320. static inline void dma_sync_single_for_device(struct device *dev,
  321. dma_addr_t addr, size_t size,
  322. enum dma_data_direction dir)
  323. {
  324. const struct dma_map_ops *ops = get_dma_ops(dev);
  325. BUG_ON(!valid_dma_direction(dir));
  326. if (ops->sync_single_for_device)
  327. ops->sync_single_for_device(dev, addr, size, dir);
  328. debug_dma_sync_single_for_device(dev, addr, size, dir);
  329. }
  330. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  331. dma_addr_t addr,
  332. unsigned long offset,
  333. size_t size,
  334. enum dma_data_direction dir)
  335. {
  336. const struct dma_map_ops *ops = get_dma_ops(dev);
  337. BUG_ON(!valid_dma_direction(dir));
  338. if (ops->sync_single_for_cpu)
  339. ops->sync_single_for_cpu(dev, addr + offset, size, dir);
  340. debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
  341. }
  342. static inline void dma_sync_single_range_for_device(struct device *dev,
  343. dma_addr_t addr,
  344. unsigned long offset,
  345. size_t size,
  346. enum dma_data_direction dir)
  347. {
  348. const struct dma_map_ops *ops = get_dma_ops(dev);
  349. BUG_ON(!valid_dma_direction(dir));
  350. if (ops->sync_single_for_device)
  351. ops->sync_single_for_device(dev, addr + offset, size, dir);
  352. debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
  353. }
  354. static inline void
  355. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  356. int nelems, enum dma_data_direction dir)
  357. {
  358. const struct dma_map_ops *ops = get_dma_ops(dev);
  359. BUG_ON(!valid_dma_direction(dir));
  360. if (ops->sync_sg_for_cpu)
  361. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  362. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  363. }
  364. static inline void
  365. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  366. int nelems, enum dma_data_direction dir)
  367. {
  368. const struct dma_map_ops *ops = get_dma_ops(dev);
  369. BUG_ON(!valid_dma_direction(dir));
  370. if (ops->sync_sg_for_device)
  371. ops->sync_sg_for_device(dev, sg, nelems, dir);
  372. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  373. }
  374. #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
  375. #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
  376. #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
  377. #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
  378. #define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
  379. #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
  380. static inline void
  381. dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  382. enum dma_data_direction dir)
  383. {
  384. const struct dma_map_ops *ops = get_dma_ops(dev);
  385. BUG_ON(!valid_dma_direction(dir));
  386. if (ops->cache_sync)
  387. ops->cache_sync(dev, vaddr, size, dir);
  388. }
  389. extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  390. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  391. void *dma_common_contiguous_remap(struct page *page, size_t size,
  392. unsigned long vm_flags,
  393. pgprot_t prot, const void *caller);
  394. void *dma_common_pages_remap(struct page **pages, size_t size,
  395. unsigned long vm_flags, pgprot_t prot,
  396. const void *caller);
  397. void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
  398. /**
  399. * dma_mmap_attrs - map a coherent DMA allocation into user space
  400. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  401. * @vma: vm_area_struct describing requested user mapping
  402. * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
  403. * @handle: device-view address returned from dma_alloc_attrs
  404. * @size: size of memory originally requested in dma_alloc_attrs
  405. * @attrs: attributes of mapping properties requested in dma_alloc_attrs
  406. *
  407. * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
  408. * into user space. The coherent DMA buffer must not be freed by the
  409. * driver until the user space mapping has been released.
  410. */
  411. static inline int
  412. dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
  413. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  414. {
  415. const struct dma_map_ops *ops = get_dma_ops(dev);
  416. BUG_ON(!ops);
  417. if (ops->mmap)
  418. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  419. return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
  420. }
  421. #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
  422. int
  423. dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  424. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  425. static inline int
  426. dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
  427. dma_addr_t dma_addr, size_t size,
  428. unsigned long attrs)
  429. {
  430. const struct dma_map_ops *ops = get_dma_ops(dev);
  431. BUG_ON(!ops);
  432. if (ops->get_sgtable)
  433. return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
  434. attrs);
  435. return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  436. }
  437. #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
  438. #ifndef arch_dma_alloc_attrs
  439. #define arch_dma_alloc_attrs(dev) (true)
  440. #endif
  441. static inline void *dma_malloc_attrs(struct device *dev, size_t size,
  442. dma_addr_t *dma_handle, gfp_t flag,
  443. unsigned long attrs, bool zero)
  444. {
  445. const struct dma_map_ops *ops = get_dma_ops(dev);
  446. void *cpu_addr;
  447. BUG_ON(!ops);
  448. WARN_ON_ONCE(dev && !dev->coherent_dma_mask);
  449. if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr, zero))
  450. return cpu_addr;
  451. /* let the implementation decide on the zone to allocate from: */
  452. flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  453. if (!arch_dma_alloc_attrs(&dev))
  454. return NULL;
  455. if (!ops->alloc)
  456. return NULL;
  457. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  458. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  459. return cpu_addr;
  460. }
  461. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  462. dma_addr_t *dma_handle, gfp_t flag,
  463. unsigned long attrs)
  464. {
  465. return dma_malloc_attrs(dev, size, dma_handle, flag, attrs, true);
  466. }
  467. static inline void dma_free_attrs(struct device *dev, size_t size,
  468. void *cpu_addr, dma_addr_t dma_handle,
  469. unsigned long attrs)
  470. {
  471. const struct dma_map_ops *ops = get_dma_ops(dev);
  472. BUG_ON(!ops);
  473. if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
  474. return;
  475. /*
  476. * On non-coherent platforms which implement DMA-coherent buffers via
  477. * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
  478. * this far in IRQ context is a) at risk of a BUG_ON() or trying to
  479. * sleep on some machines, and b) an indication that the driver is
  480. * probably misusing the coherent API anyway.
  481. */
  482. WARN_ON(irqs_disabled());
  483. if (!ops->free || !cpu_addr)
  484. return;
  485. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  486. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  487. }
  488. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  489. dma_addr_t *dma_handle, gfp_t flag)
  490. {
  491. return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
  492. }
  493. static inline void *dma_malloc_coherent(struct device *dev, size_t size,
  494. dma_addr_t *dma_handle, gfp_t flag)
  495. {
  496. return dma_malloc_attrs(dev, size, dma_handle, flag, 0, false);
  497. }
  498. static inline void dma_free_coherent(struct device *dev, size_t size,
  499. void *cpu_addr, dma_addr_t dma_handle)
  500. {
  501. return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
  502. }
  503. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  504. {
  505. const struct dma_map_ops *ops = get_dma_ops(dev);
  506. debug_dma_mapping_error(dev, dma_addr);
  507. if (ops->mapping_error)
  508. return ops->mapping_error(dev, dma_addr);
  509. return 0;
  510. }
  511. static inline void dma_check_mask(struct device *dev, u64 mask)
  512. {
  513. if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1)))
  514. dev_warn(dev, "SME is active, device will require DMA bounce buffers\n");
  515. }
  516. static inline int dma_supported(struct device *dev, u64 mask)
  517. {
  518. const struct dma_map_ops *ops = get_dma_ops(dev);
  519. if (!ops)
  520. return 0;
  521. if (!ops->dma_supported)
  522. return 1;
  523. return ops->dma_supported(dev, mask);
  524. }
  525. #ifndef HAVE_ARCH_DMA_SET_MASK
  526. static inline int dma_set_mask(struct device *dev, u64 mask)
  527. {
  528. if (!dev->dma_mask || !dma_supported(dev, mask))
  529. return -EIO;
  530. dma_check_mask(dev, mask);
  531. *dev->dma_mask = mask;
  532. return 0;
  533. }
  534. #endif
  535. static inline u64 dma_get_mask(struct device *dev)
  536. {
  537. if (dev && dev->dma_mask && *dev->dma_mask)
  538. return *dev->dma_mask;
  539. return DMA_BIT_MASK(32);
  540. }
  541. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  542. int dma_set_coherent_mask(struct device *dev, u64 mask);
  543. #else
  544. static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
  545. {
  546. if (!dma_supported(dev, mask))
  547. return -EIO;
  548. dma_check_mask(dev, mask);
  549. dev->coherent_dma_mask = mask;
  550. return 0;
  551. }
  552. #endif
  553. /*
  554. * Set both the DMA mask and the coherent DMA mask to the same thing.
  555. * Note that we don't check the return value from dma_set_coherent_mask()
  556. * as the DMA API guarantees that the coherent DMA mask can be set to
  557. * the same or smaller than the streaming DMA mask.
  558. */
  559. static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
  560. {
  561. int rc = dma_set_mask(dev, mask);
  562. if (rc == 0)
  563. dma_set_coherent_mask(dev, mask);
  564. return rc;
  565. }
  566. /*
  567. * Similar to the above, except it deals with the case where the device
  568. * does not have dev->dma_mask appropriately setup.
  569. */
  570. static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
  571. {
  572. dev->dma_mask = &dev->coherent_dma_mask;
  573. return dma_set_mask_and_coherent(dev, mask);
  574. }
  575. extern u64 dma_get_required_mask(struct device *dev);
  576. #ifndef arch_setup_dma_ops
  577. static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
  578. u64 size, const struct iommu_ops *iommu,
  579. bool coherent) { }
  580. #endif
  581. #ifndef arch_teardown_dma_ops
  582. static inline void arch_teardown_dma_ops(struct device *dev) { }
  583. #endif
  584. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  585. {
  586. if (dev->dma_parms && dev->dma_parms->max_segment_size)
  587. return dev->dma_parms->max_segment_size;
  588. return SZ_64K;
  589. }
  590. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  591. unsigned int size)
  592. {
  593. if (dev->dma_parms) {
  594. dev->dma_parms->max_segment_size = size;
  595. return 0;
  596. }
  597. return -EIO;
  598. }
  599. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  600. {
  601. if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
  602. return dev->dma_parms->segment_boundary_mask;
  603. return DMA_BIT_MASK(32);
  604. }
  605. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  606. {
  607. if (dev->dma_parms) {
  608. dev->dma_parms->segment_boundary_mask = mask;
  609. return 0;
  610. }
  611. return -EIO;
  612. }
  613. #ifndef dma_max_pfn
  614. static inline unsigned long dma_max_pfn(struct device *dev)
  615. {
  616. return (*dev->dma_mask >> PAGE_SHIFT) + dev->dma_pfn_offset;
  617. }
  618. #endif
  619. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  620. dma_addr_t *dma_handle, gfp_t flag)
  621. {
  622. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  623. flag | __GFP_ZERO);
  624. return ret;
  625. }
  626. static inline int dma_get_cache_alignment(void)
  627. {
  628. #ifdef ARCH_DMA_MINALIGN
  629. return ARCH_DMA_MINALIGN;
  630. #endif
  631. return 1;
  632. }
  633. /* flags for the coherent memory api */
  634. #define DMA_MEMORY_EXCLUSIVE 0x01
  635. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  636. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  637. dma_addr_t device_addr, size_t size, int flags);
  638. void dma_release_declared_memory(struct device *dev);
  639. void *dma_mark_declared_memory_occupied(struct device *dev,
  640. dma_addr_t device_addr, size_t size);
  641. #else
  642. static inline int
  643. dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  644. dma_addr_t device_addr, size_t size, int flags)
  645. {
  646. return -ENOSYS;
  647. }
  648. static inline void
  649. dma_release_declared_memory(struct device *dev)
  650. {
  651. }
  652. static inline void *
  653. dma_mark_declared_memory_occupied(struct device *dev,
  654. dma_addr_t device_addr, size_t size)
  655. {
  656. return ERR_PTR(-EBUSY);
  657. }
  658. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  659. #ifdef CONFIG_HAS_DMA
  660. int dma_configure(struct device *dev);
  661. void dma_deconfigure(struct device *dev);
  662. #else
  663. static inline int dma_configure(struct device *dev)
  664. {
  665. return 0;
  666. }
  667. static inline void dma_deconfigure(struct device *dev) {}
  668. #endif
  669. /*
  670. * Managed DMA API
  671. */
  672. #ifdef CONFIG_HAS_DMA
  673. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  674. dma_addr_t *dma_handle, gfp_t gfp);
  675. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  676. dma_addr_t dma_handle);
  677. #else /* !CONFIG_HAS_DMA */
  678. static inline void *dmam_alloc_coherent(struct device *dev, size_t size,
  679. dma_addr_t *dma_handle, gfp_t gfp)
  680. { return NULL; }
  681. static inline void dmam_free_coherent(struct device *dev, size_t size,
  682. void *vaddr, dma_addr_t dma_handle) { }
  683. #endif /* !CONFIG_HAS_DMA */
  684. extern void *dmam_alloc_attrs(struct device *dev, size_t size,
  685. dma_addr_t *dma_handle, gfp_t gfp,
  686. unsigned long attrs);
  687. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  688. extern int dmam_declare_coherent_memory(struct device *dev,
  689. phys_addr_t phys_addr,
  690. dma_addr_t device_addr, size_t size,
  691. int flags);
  692. extern void dmam_release_declared_memory(struct device *dev);
  693. #else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  694. static inline int dmam_declare_coherent_memory(struct device *dev,
  695. phys_addr_t phys_addr, dma_addr_t device_addr,
  696. size_t size, gfp_t gfp)
  697. {
  698. return 0;
  699. }
  700. static inline void dmam_release_declared_memory(struct device *dev)
  701. {
  702. }
  703. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  704. static inline void *dma_alloc_wc(struct device *dev, size_t size,
  705. dma_addr_t *dma_addr, gfp_t gfp)
  706. {
  707. return dma_alloc_attrs(dev, size, dma_addr, gfp,
  708. DMA_ATTR_WRITE_COMBINE);
  709. }
  710. #ifndef dma_alloc_writecombine
  711. #define dma_alloc_writecombine dma_alloc_wc
  712. #endif
  713. static inline void dma_free_wc(struct device *dev, size_t size,
  714. void *cpu_addr, dma_addr_t dma_addr)
  715. {
  716. return dma_free_attrs(dev, size, cpu_addr, dma_addr,
  717. DMA_ATTR_WRITE_COMBINE);
  718. }
  719. #ifndef dma_free_writecombine
  720. #define dma_free_writecombine dma_free_wc
  721. #endif
  722. static inline int dma_mmap_wc(struct device *dev,
  723. struct vm_area_struct *vma,
  724. void *cpu_addr, dma_addr_t dma_addr,
  725. size_t size)
  726. {
  727. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
  728. DMA_ATTR_WRITE_COMBINE);
  729. }
  730. #ifndef dma_mmap_writecombine
  731. #define dma_mmap_writecombine dma_mmap_wc
  732. #endif
  733. #ifdef CONFIG_NEED_DMA_MAP_STATE
  734. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  735. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  736. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  737. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  738. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  739. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  740. #else
  741. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  742. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  743. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  744. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  745. #define dma_unmap_len(PTR, LEN_NAME) (0)
  746. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  747. #endif
  748. #endif