mapping.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch-independent dma-mapping routines
  4. *
  5. * Copyright (c) 2006 SUSE Linux Products GmbH
  6. * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
  7. */
  8. #include <linux/memblock.h> /* for max_pfn */
  9. #include <linux/acpi.h>
  10. #include <linux/dma-map-ops.h>
  11. #include <linux/export.h>
  12. #include <linux/gfp.h>
  13. #include <linux/iommu-dma.h>
  14. #include <linux/kmsan.h>
  15. #include <linux/of_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include "debug.h"
  19. #include "direct.h"
  20. #define CREATE_TRACE_POINTS
  21. #include <trace/events/dma.h>
  22. #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
  23. defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
  24. defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
  25. bool dma_default_coherent = IS_ENABLED(CONFIG_ARCH_DMA_DEFAULT_COHERENT);
  26. #endif
  27. /*
  28. * Managed DMA API
  29. */
  30. struct dma_devres {
  31. size_t size;
  32. void *vaddr;
  33. dma_addr_t dma_handle;
  34. unsigned long attrs;
  35. };
  36. static void dmam_release(struct device *dev, void *res)
  37. {
  38. struct dma_devres *this = res;
  39. dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
  40. this->attrs);
  41. }
  42. static int dmam_match(struct device *dev, void *res, void *match_data)
  43. {
  44. struct dma_devres *this = res, *match = match_data;
  45. if (this->vaddr == match->vaddr) {
  46. WARN_ON(this->size != match->size ||
  47. this->dma_handle != match->dma_handle);
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. /**
  53. * dmam_free_coherent - Managed dma_free_coherent()
  54. * @dev: Device to free coherent memory for
  55. * @size: Size of allocation
  56. * @vaddr: Virtual address of the memory to free
  57. * @dma_handle: DMA handle of the memory to free
  58. *
  59. * Managed dma_free_coherent().
  60. */
  61. void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  62. dma_addr_t dma_handle)
  63. {
  64. struct dma_devres match_data = { size, vaddr, dma_handle };
  65. WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
  66. dma_free_coherent(dev, size, vaddr, dma_handle);
  67. }
  68. EXPORT_SYMBOL(dmam_free_coherent);
  69. /**
  70. * dmam_alloc_attrs - Managed dma_alloc_attrs()
  71. * @dev: Device to allocate non_coherent memory for
  72. * @size: Size of allocation
  73. * @dma_handle: Out argument for allocated DMA handle
  74. * @gfp: Allocation flags
  75. * @attrs: Flags in the DMA_ATTR_* namespace.
  76. *
  77. * Managed dma_alloc_attrs(). Memory allocated using this function will be
  78. * automatically released on driver detach.
  79. *
  80. * RETURNS:
  81. * Pointer to allocated memory on success, NULL on failure.
  82. */
  83. void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
  84. gfp_t gfp, unsigned long attrs)
  85. {
  86. struct dma_devres *dr;
  87. void *vaddr;
  88. dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
  89. if (!dr)
  90. return NULL;
  91. vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
  92. if (!vaddr) {
  93. devres_free(dr);
  94. return NULL;
  95. }
  96. dr->vaddr = vaddr;
  97. dr->dma_handle = *dma_handle;
  98. dr->size = size;
  99. dr->attrs = attrs;
  100. devres_add(dev, dr);
  101. return vaddr;
  102. }
  103. EXPORT_SYMBOL(dmam_alloc_attrs);
  104. static bool dma_go_direct(struct device *dev, dma_addr_t mask,
  105. const struct dma_map_ops *ops)
  106. {
  107. if (use_dma_iommu(dev))
  108. return false;
  109. if (likely(!ops))
  110. return true;
  111. #ifdef CONFIG_DMA_OPS_BYPASS
  112. if (dev->dma_ops_bypass)
  113. return min_not_zero(mask, dev->bus_dma_limit) >=
  114. dma_direct_get_required_mask(dev);
  115. #endif
  116. return false;
  117. }
  118. /*
  119. * Check if the devices uses a direct mapping for streaming DMA operations.
  120. * This allows IOMMU drivers to set a bypass mode if the DMA mask is large
  121. * enough.
  122. */
  123. static inline bool dma_alloc_direct(struct device *dev,
  124. const struct dma_map_ops *ops)
  125. {
  126. return dma_go_direct(dev, dev->coherent_dma_mask, ops);
  127. }
  128. static inline bool dma_map_direct(struct device *dev,
  129. const struct dma_map_ops *ops)
  130. {
  131. return dma_go_direct(dev, *dev->dma_mask, ops);
  132. }
  133. dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
  134. size_t offset, size_t size, enum dma_data_direction dir,
  135. unsigned long attrs)
  136. {
  137. const struct dma_map_ops *ops = get_dma_ops(dev);
  138. dma_addr_t addr;
  139. BUG_ON(!valid_dma_direction(dir));
  140. if (WARN_ON_ONCE(!dev->dma_mask))
  141. return DMA_MAPPING_ERROR;
  142. if (dma_map_direct(dev, ops) ||
  143. arch_dma_map_page_direct(dev, page_to_phys(page) + offset + size))
  144. addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
  145. else if (use_dma_iommu(dev))
  146. addr = iommu_dma_map_page(dev, page, offset, size, dir, attrs);
  147. else
  148. addr = ops->map_page(dev, page, offset, size, dir, attrs);
  149. kmsan_handle_dma(page, offset, size, dir);
  150. trace_dma_map_page(dev, page_to_phys(page) + offset, addr, size, dir,
  151. attrs);
  152. debug_dma_map_page(dev, page, offset, size, dir, addr, attrs);
  153. return addr;
  154. }
  155. EXPORT_SYMBOL(dma_map_page_attrs);
  156. void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
  157. enum dma_data_direction dir, unsigned long attrs)
  158. {
  159. const struct dma_map_ops *ops = get_dma_ops(dev);
  160. BUG_ON(!valid_dma_direction(dir));
  161. if (dma_map_direct(dev, ops) ||
  162. arch_dma_unmap_page_direct(dev, addr + size))
  163. dma_direct_unmap_page(dev, addr, size, dir, attrs);
  164. else if (use_dma_iommu(dev))
  165. iommu_dma_unmap_page(dev, addr, size, dir, attrs);
  166. else
  167. ops->unmap_page(dev, addr, size, dir, attrs);
  168. trace_dma_unmap_page(dev, addr, size, dir, attrs);
  169. debug_dma_unmap_page(dev, addr, size, dir);
  170. }
  171. EXPORT_SYMBOL(dma_unmap_page_attrs);
  172. static int __dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  173. int nents, enum dma_data_direction dir, unsigned long attrs)
  174. {
  175. const struct dma_map_ops *ops = get_dma_ops(dev);
  176. int ents;
  177. BUG_ON(!valid_dma_direction(dir));
  178. if (WARN_ON_ONCE(!dev->dma_mask))
  179. return 0;
  180. if (dma_map_direct(dev, ops) ||
  181. arch_dma_map_sg_direct(dev, sg, nents))
  182. ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
  183. else if (use_dma_iommu(dev))
  184. ents = iommu_dma_map_sg(dev, sg, nents, dir, attrs);
  185. else
  186. ents = ops->map_sg(dev, sg, nents, dir, attrs);
  187. if (ents > 0) {
  188. kmsan_handle_dma_sg(sg, nents, dir);
  189. trace_dma_map_sg(dev, sg, nents, ents, dir, attrs);
  190. debug_dma_map_sg(dev, sg, nents, ents, dir, attrs);
  191. } else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM &&
  192. ents != -EIO && ents != -EREMOTEIO)) {
  193. return -EIO;
  194. }
  195. return ents;
  196. }
  197. /**
  198. * dma_map_sg_attrs - Map the given buffer for DMA
  199. * @dev: The device for which to perform the DMA operation
  200. * @sg: The sg_table object describing the buffer
  201. * @nents: Number of entries to map
  202. * @dir: DMA direction
  203. * @attrs: Optional DMA attributes for the map operation
  204. *
  205. * Maps a buffer described by a scatterlist passed in the sg argument with
  206. * nents segments for the @dir DMA operation by the @dev device.
  207. *
  208. * Returns the number of mapped entries (which can be less than nents)
  209. * on success. Zero is returned for any error.
  210. *
  211. * dma_unmap_sg_attrs() should be used to unmap the buffer with the
  212. * original sg and original nents (not the value returned by this funciton).
  213. */
  214. unsigned int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  215. int nents, enum dma_data_direction dir, unsigned long attrs)
  216. {
  217. int ret;
  218. ret = __dma_map_sg_attrs(dev, sg, nents, dir, attrs);
  219. if (ret < 0)
  220. return 0;
  221. return ret;
  222. }
  223. EXPORT_SYMBOL(dma_map_sg_attrs);
  224. /**
  225. * dma_map_sgtable - Map the given buffer for DMA
  226. * @dev: The device for which to perform the DMA operation
  227. * @sgt: The sg_table object describing the buffer
  228. * @dir: DMA direction
  229. * @attrs: Optional DMA attributes for the map operation
  230. *
  231. * Maps a buffer described by a scatterlist stored in the given sg_table
  232. * object for the @dir DMA operation by the @dev device. After success, the
  233. * ownership for the buffer is transferred to the DMA domain. One has to
  234. * call dma_sync_sgtable_for_cpu() or dma_unmap_sgtable() to move the
  235. * ownership of the buffer back to the CPU domain before touching the
  236. * buffer by the CPU.
  237. *
  238. * Returns 0 on success or a negative error code on error. The following
  239. * error codes are supported with the given meaning:
  240. *
  241. * -EINVAL An invalid argument, unaligned access or other error
  242. * in usage. Will not succeed if retried.
  243. * -ENOMEM Insufficient resources (like memory or IOVA space) to
  244. * complete the mapping. Should succeed if retried later.
  245. * -EIO Legacy error code with an unknown meaning. eg. this is
  246. * returned if a lower level call returned
  247. * DMA_MAPPING_ERROR.
  248. * -EREMOTEIO The DMA device cannot access P2PDMA memory specified
  249. * in the sg_table. This will not succeed if retried.
  250. */
  251. int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
  252. enum dma_data_direction dir, unsigned long attrs)
  253. {
  254. int nents;
  255. nents = __dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
  256. if (nents < 0)
  257. return nents;
  258. sgt->nents = nents;
  259. return 0;
  260. }
  261. EXPORT_SYMBOL_GPL(dma_map_sgtable);
  262. void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  263. int nents, enum dma_data_direction dir,
  264. unsigned long attrs)
  265. {
  266. const struct dma_map_ops *ops = get_dma_ops(dev);
  267. BUG_ON(!valid_dma_direction(dir));
  268. trace_dma_unmap_sg(dev, sg, nents, dir, attrs);
  269. debug_dma_unmap_sg(dev, sg, nents, dir);
  270. if (dma_map_direct(dev, ops) ||
  271. arch_dma_unmap_sg_direct(dev, sg, nents))
  272. dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
  273. else if (use_dma_iommu(dev))
  274. iommu_dma_unmap_sg(dev, sg, nents, dir, attrs);
  275. else if (ops->unmap_sg)
  276. ops->unmap_sg(dev, sg, nents, dir, attrs);
  277. }
  278. EXPORT_SYMBOL(dma_unmap_sg_attrs);
  279. dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
  280. size_t size, enum dma_data_direction dir, unsigned long attrs)
  281. {
  282. const struct dma_map_ops *ops = get_dma_ops(dev);
  283. dma_addr_t addr = DMA_MAPPING_ERROR;
  284. BUG_ON(!valid_dma_direction(dir));
  285. if (WARN_ON_ONCE(!dev->dma_mask))
  286. return DMA_MAPPING_ERROR;
  287. if (dma_map_direct(dev, ops))
  288. addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
  289. else if (use_dma_iommu(dev))
  290. addr = iommu_dma_map_resource(dev, phys_addr, size, dir, attrs);
  291. else if (ops->map_resource)
  292. addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
  293. trace_dma_map_resource(dev, phys_addr, addr, size, dir, attrs);
  294. debug_dma_map_resource(dev, phys_addr, size, dir, addr, attrs);
  295. return addr;
  296. }
  297. EXPORT_SYMBOL(dma_map_resource);
  298. void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
  299. enum dma_data_direction dir, unsigned long attrs)
  300. {
  301. const struct dma_map_ops *ops = get_dma_ops(dev);
  302. BUG_ON(!valid_dma_direction(dir));
  303. if (dma_map_direct(dev, ops))
  304. ; /* nothing to do: uncached and no swiotlb */
  305. else if (use_dma_iommu(dev))
  306. iommu_dma_unmap_resource(dev, addr, size, dir, attrs);
  307. else if (ops->unmap_resource)
  308. ops->unmap_resource(dev, addr, size, dir, attrs);
  309. trace_dma_unmap_resource(dev, addr, size, dir, attrs);
  310. debug_dma_unmap_resource(dev, addr, size, dir);
  311. }
  312. EXPORT_SYMBOL(dma_unmap_resource);
  313. #ifdef CONFIG_DMA_NEED_SYNC
  314. void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
  315. enum dma_data_direction dir)
  316. {
  317. const struct dma_map_ops *ops = get_dma_ops(dev);
  318. BUG_ON(!valid_dma_direction(dir));
  319. if (dma_map_direct(dev, ops))
  320. dma_direct_sync_single_for_cpu(dev, addr, size, dir);
  321. else if (use_dma_iommu(dev))
  322. iommu_dma_sync_single_for_cpu(dev, addr, size, dir);
  323. else if (ops->sync_single_for_cpu)
  324. ops->sync_single_for_cpu(dev, addr, size, dir);
  325. trace_dma_sync_single_for_cpu(dev, addr, size, dir);
  326. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  327. }
  328. EXPORT_SYMBOL(__dma_sync_single_for_cpu);
  329. void __dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
  330. size_t size, enum dma_data_direction dir)
  331. {
  332. const struct dma_map_ops *ops = get_dma_ops(dev);
  333. BUG_ON(!valid_dma_direction(dir));
  334. if (dma_map_direct(dev, ops))
  335. dma_direct_sync_single_for_device(dev, addr, size, dir);
  336. else if (use_dma_iommu(dev))
  337. iommu_dma_sync_single_for_device(dev, addr, size, dir);
  338. else if (ops->sync_single_for_device)
  339. ops->sync_single_for_device(dev, addr, size, dir);
  340. trace_dma_sync_single_for_device(dev, addr, size, dir);
  341. debug_dma_sync_single_for_device(dev, addr, size, dir);
  342. }
  343. EXPORT_SYMBOL(__dma_sync_single_for_device);
  344. void __dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  345. int nelems, 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 (dma_map_direct(dev, ops))
  350. dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir);
  351. else if (use_dma_iommu(dev))
  352. iommu_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  353. else if (ops->sync_sg_for_cpu)
  354. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  355. trace_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  356. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  357. }
  358. EXPORT_SYMBOL(__dma_sync_sg_for_cpu);
  359. void __dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  360. int nelems, enum dma_data_direction dir)
  361. {
  362. const struct dma_map_ops *ops = get_dma_ops(dev);
  363. BUG_ON(!valid_dma_direction(dir));
  364. if (dma_map_direct(dev, ops))
  365. dma_direct_sync_sg_for_device(dev, sg, nelems, dir);
  366. else if (use_dma_iommu(dev))
  367. iommu_dma_sync_sg_for_device(dev, sg, nelems, dir);
  368. else if (ops->sync_sg_for_device)
  369. ops->sync_sg_for_device(dev, sg, nelems, dir);
  370. trace_dma_sync_sg_for_device(dev, sg, nelems, dir);
  371. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  372. }
  373. EXPORT_SYMBOL(__dma_sync_sg_for_device);
  374. bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr)
  375. {
  376. const struct dma_map_ops *ops = get_dma_ops(dev);
  377. if (dma_map_direct(dev, ops))
  378. /*
  379. * dma_skip_sync could've been reset on first SWIOTLB buffer
  380. * mapping, but @dma_addr is not necessary an SWIOTLB buffer.
  381. * In this case, fall back to more granular check.
  382. */
  383. return dma_direct_need_sync(dev, dma_addr);
  384. return true;
  385. }
  386. EXPORT_SYMBOL_GPL(__dma_need_sync);
  387. static void dma_setup_need_sync(struct device *dev)
  388. {
  389. const struct dma_map_ops *ops = get_dma_ops(dev);
  390. if (dma_map_direct(dev, ops) || use_dma_iommu(dev))
  391. /*
  392. * dma_skip_sync will be reset to %false on first SWIOTLB buffer
  393. * mapping, if any. During the device initialization, it's
  394. * enough to check only for the DMA coherence.
  395. */
  396. dev->dma_skip_sync = dev_is_dma_coherent(dev);
  397. else if (!ops->sync_single_for_device && !ops->sync_single_for_cpu &&
  398. !ops->sync_sg_for_device && !ops->sync_sg_for_cpu)
  399. /*
  400. * Synchronization is not possible when none of DMA sync ops
  401. * is set.
  402. */
  403. dev->dma_skip_sync = true;
  404. else
  405. dev->dma_skip_sync = false;
  406. }
  407. #else /* !CONFIG_DMA_NEED_SYNC */
  408. static inline void dma_setup_need_sync(struct device *dev) { }
  409. #endif /* !CONFIG_DMA_NEED_SYNC */
  410. /*
  411. * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
  412. * that the intention is to allow exporting memory allocated via the
  413. * coherent DMA APIs through the dma_buf API, which only accepts a
  414. * scattertable. This presents a couple of problems:
  415. * 1. Not all memory allocated via the coherent DMA APIs is backed by
  416. * a struct page
  417. * 2. Passing coherent DMA memory into the streaming APIs is not allowed
  418. * as we will try to flush the memory through a different alias to that
  419. * actually being used (and the flushes are redundant.)
  420. */
  421. int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
  422. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  423. unsigned long attrs)
  424. {
  425. const struct dma_map_ops *ops = get_dma_ops(dev);
  426. if (dma_alloc_direct(dev, ops))
  427. return dma_direct_get_sgtable(dev, sgt, cpu_addr, dma_addr,
  428. size, attrs);
  429. if (use_dma_iommu(dev))
  430. return iommu_dma_get_sgtable(dev, sgt, cpu_addr, dma_addr,
  431. size, attrs);
  432. if (!ops->get_sgtable)
  433. return -ENXIO;
  434. return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs);
  435. }
  436. EXPORT_SYMBOL(dma_get_sgtable_attrs);
  437. #ifdef CONFIG_MMU
  438. /*
  439. * Return the page attributes used for mapping dma_alloc_* memory, either in
  440. * kernel space if remapping is needed, or to userspace through dma_mmap_*.
  441. */
  442. pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs)
  443. {
  444. if (dev_is_dma_coherent(dev))
  445. return prot;
  446. #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE
  447. if (attrs & DMA_ATTR_WRITE_COMBINE)
  448. return pgprot_writecombine(prot);
  449. #endif
  450. return pgprot_dmacoherent(prot);
  451. }
  452. #endif /* CONFIG_MMU */
  453. /**
  454. * dma_can_mmap - check if a given device supports dma_mmap_*
  455. * @dev: device to check
  456. *
  457. * Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to
  458. * map DMA allocations to userspace.
  459. */
  460. bool dma_can_mmap(struct device *dev)
  461. {
  462. const struct dma_map_ops *ops = get_dma_ops(dev);
  463. if (dma_alloc_direct(dev, ops))
  464. return dma_direct_can_mmap(dev);
  465. if (use_dma_iommu(dev))
  466. return true;
  467. return ops->mmap != NULL;
  468. }
  469. EXPORT_SYMBOL_GPL(dma_can_mmap);
  470. /**
  471. * dma_mmap_attrs - map a coherent DMA allocation into user space
  472. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  473. * @vma: vm_area_struct describing requested user mapping
  474. * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
  475. * @dma_addr: device-view address returned from dma_alloc_attrs
  476. * @size: size of memory originally requested in dma_alloc_attrs
  477. * @attrs: attributes of mapping properties requested in dma_alloc_attrs
  478. *
  479. * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user
  480. * space. The coherent DMA buffer must not be freed by the driver until the
  481. * user space mapping has been released.
  482. */
  483. int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  484. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  485. unsigned long attrs)
  486. {
  487. const struct dma_map_ops *ops = get_dma_ops(dev);
  488. if (dma_alloc_direct(dev, ops))
  489. return dma_direct_mmap(dev, vma, cpu_addr, dma_addr, size,
  490. attrs);
  491. if (use_dma_iommu(dev))
  492. return iommu_dma_mmap(dev, vma, cpu_addr, dma_addr, size,
  493. attrs);
  494. if (!ops->mmap)
  495. return -ENXIO;
  496. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  497. }
  498. EXPORT_SYMBOL(dma_mmap_attrs);
  499. u64 dma_get_required_mask(struct device *dev)
  500. {
  501. const struct dma_map_ops *ops = get_dma_ops(dev);
  502. if (dma_alloc_direct(dev, ops))
  503. return dma_direct_get_required_mask(dev);
  504. if (use_dma_iommu(dev))
  505. return DMA_BIT_MASK(32);
  506. if (ops->get_required_mask)
  507. return ops->get_required_mask(dev);
  508. /*
  509. * We require every DMA ops implementation to at least support a 32-bit
  510. * DMA mask (and use bounce buffering if that isn't supported in
  511. * hardware). As the direct mapping code has its own routine to
  512. * actually report an optimal mask we default to 32-bit here as that
  513. * is the right thing for most IOMMUs, and at least not actively
  514. * harmful in general.
  515. */
  516. return DMA_BIT_MASK(32);
  517. }
  518. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  519. void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
  520. gfp_t flag, unsigned long attrs)
  521. {
  522. const struct dma_map_ops *ops = get_dma_ops(dev);
  523. void *cpu_addr;
  524. WARN_ON_ONCE(!dev->coherent_dma_mask);
  525. /*
  526. * DMA allocations can never be turned back into a page pointer, so
  527. * requesting compound pages doesn't make sense (and can't even be
  528. * supported at all by various backends).
  529. */
  530. if (WARN_ON_ONCE(flag & __GFP_COMP))
  531. return NULL;
  532. if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
  533. return cpu_addr;
  534. /* let the implementation decide on the zone to allocate from: */
  535. flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  536. if (dma_alloc_direct(dev, ops))
  537. cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
  538. else if (use_dma_iommu(dev))
  539. cpu_addr = iommu_dma_alloc(dev, size, dma_handle, flag, attrs);
  540. else if (ops->alloc)
  541. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  542. else
  543. return NULL;
  544. trace_dma_alloc(dev, cpu_addr, *dma_handle, size, flag, attrs);
  545. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr, attrs);
  546. return cpu_addr;
  547. }
  548. EXPORT_SYMBOL(dma_alloc_attrs);
  549. void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  550. dma_addr_t dma_handle, unsigned long attrs)
  551. {
  552. const struct dma_map_ops *ops = get_dma_ops(dev);
  553. if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
  554. return;
  555. /*
  556. * On non-coherent platforms which implement DMA-coherent buffers via
  557. * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
  558. * this far in IRQ context is a) at risk of a BUG_ON() or trying to
  559. * sleep on some machines, and b) an indication that the driver is
  560. * probably misusing the coherent API anyway.
  561. */
  562. WARN_ON(irqs_disabled());
  563. if (!cpu_addr)
  564. return;
  565. trace_dma_free(dev, cpu_addr, dma_handle, size, attrs);
  566. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  567. if (dma_alloc_direct(dev, ops))
  568. dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
  569. else if (use_dma_iommu(dev))
  570. iommu_dma_free(dev, size, cpu_addr, dma_handle, attrs);
  571. else if (ops->free)
  572. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  573. }
  574. EXPORT_SYMBOL(dma_free_attrs);
  575. static struct page *__dma_alloc_pages(struct device *dev, size_t size,
  576. dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
  577. {
  578. const struct dma_map_ops *ops = get_dma_ops(dev);
  579. if (WARN_ON_ONCE(!dev->coherent_dma_mask))
  580. return NULL;
  581. if (WARN_ON_ONCE(gfp & (__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM)))
  582. return NULL;
  583. if (WARN_ON_ONCE(gfp & __GFP_COMP))
  584. return NULL;
  585. size = PAGE_ALIGN(size);
  586. if (dma_alloc_direct(dev, ops))
  587. return dma_direct_alloc_pages(dev, size, dma_handle, dir, gfp);
  588. if (use_dma_iommu(dev))
  589. return dma_common_alloc_pages(dev, size, dma_handle, dir, gfp);
  590. if (!ops->alloc_pages_op)
  591. return NULL;
  592. return ops->alloc_pages_op(dev, size, dma_handle, dir, gfp);
  593. }
  594. struct page *dma_alloc_pages(struct device *dev, size_t size,
  595. dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
  596. {
  597. struct page *page = __dma_alloc_pages(dev, size, dma_handle, dir, gfp);
  598. if (page) {
  599. trace_dma_map_page(dev, page_to_phys(page), *dma_handle, size,
  600. dir, 0);
  601. debug_dma_map_page(dev, page, 0, size, dir, *dma_handle, 0);
  602. }
  603. return page;
  604. }
  605. EXPORT_SYMBOL_GPL(dma_alloc_pages);
  606. static void __dma_free_pages(struct device *dev, size_t size, struct page *page,
  607. dma_addr_t dma_handle, enum dma_data_direction dir)
  608. {
  609. const struct dma_map_ops *ops = get_dma_ops(dev);
  610. size = PAGE_ALIGN(size);
  611. if (dma_alloc_direct(dev, ops))
  612. dma_direct_free_pages(dev, size, page, dma_handle, dir);
  613. else if (use_dma_iommu(dev))
  614. dma_common_free_pages(dev, size, page, dma_handle, dir);
  615. else if (ops->free_pages)
  616. ops->free_pages(dev, size, page, dma_handle, dir);
  617. }
  618. void dma_free_pages(struct device *dev, size_t size, struct page *page,
  619. dma_addr_t dma_handle, enum dma_data_direction dir)
  620. {
  621. trace_dma_unmap_page(dev, dma_handle, size, dir, 0);
  622. debug_dma_unmap_page(dev, dma_handle, size, dir);
  623. __dma_free_pages(dev, size, page, dma_handle, dir);
  624. }
  625. EXPORT_SYMBOL_GPL(dma_free_pages);
  626. int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma,
  627. size_t size, struct page *page)
  628. {
  629. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  630. if (vma->vm_pgoff >= count || vma_pages(vma) > count - vma->vm_pgoff)
  631. return -ENXIO;
  632. return remap_pfn_range(vma, vma->vm_start,
  633. page_to_pfn(page) + vma->vm_pgoff,
  634. vma_pages(vma) << PAGE_SHIFT, vma->vm_page_prot);
  635. }
  636. EXPORT_SYMBOL_GPL(dma_mmap_pages);
  637. static struct sg_table *alloc_single_sgt(struct device *dev, size_t size,
  638. enum dma_data_direction dir, gfp_t gfp)
  639. {
  640. struct sg_table *sgt;
  641. struct page *page;
  642. sgt = kmalloc(sizeof(*sgt), gfp);
  643. if (!sgt)
  644. return NULL;
  645. if (sg_alloc_table(sgt, 1, gfp))
  646. goto out_free_sgt;
  647. page = __dma_alloc_pages(dev, size, &sgt->sgl->dma_address, dir, gfp);
  648. if (!page)
  649. goto out_free_table;
  650. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  651. sg_dma_len(sgt->sgl) = sgt->sgl->length;
  652. return sgt;
  653. out_free_table:
  654. sg_free_table(sgt);
  655. out_free_sgt:
  656. kfree(sgt);
  657. return NULL;
  658. }
  659. struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size,
  660. enum dma_data_direction dir, gfp_t gfp, unsigned long attrs)
  661. {
  662. struct sg_table *sgt;
  663. if (WARN_ON_ONCE(attrs & ~DMA_ATTR_ALLOC_SINGLE_PAGES))
  664. return NULL;
  665. if (WARN_ON_ONCE(gfp & __GFP_COMP))
  666. return NULL;
  667. if (use_dma_iommu(dev))
  668. sgt = iommu_dma_alloc_noncontiguous(dev, size, dir, gfp, attrs);
  669. else
  670. sgt = alloc_single_sgt(dev, size, dir, gfp);
  671. if (sgt) {
  672. sgt->nents = 1;
  673. trace_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs);
  674. debug_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs);
  675. }
  676. return sgt;
  677. }
  678. EXPORT_SYMBOL_GPL(dma_alloc_noncontiguous);
  679. static void free_single_sgt(struct device *dev, size_t size,
  680. struct sg_table *sgt, enum dma_data_direction dir)
  681. {
  682. __dma_free_pages(dev, size, sg_page(sgt->sgl), sgt->sgl->dma_address,
  683. dir);
  684. sg_free_table(sgt);
  685. kfree(sgt);
  686. }
  687. void dma_free_noncontiguous(struct device *dev, size_t size,
  688. struct sg_table *sgt, enum dma_data_direction dir)
  689. {
  690. trace_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir, 0);
  691. debug_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
  692. if (use_dma_iommu(dev))
  693. iommu_dma_free_noncontiguous(dev, size, sgt, dir);
  694. else
  695. free_single_sgt(dev, size, sgt, dir);
  696. }
  697. EXPORT_SYMBOL_GPL(dma_free_noncontiguous);
  698. void *dma_vmap_noncontiguous(struct device *dev, size_t size,
  699. struct sg_table *sgt)
  700. {
  701. if (use_dma_iommu(dev))
  702. return iommu_dma_vmap_noncontiguous(dev, size, sgt);
  703. return page_address(sg_page(sgt->sgl));
  704. }
  705. EXPORT_SYMBOL_GPL(dma_vmap_noncontiguous);
  706. void dma_vunmap_noncontiguous(struct device *dev, void *vaddr)
  707. {
  708. if (use_dma_iommu(dev))
  709. iommu_dma_vunmap_noncontiguous(dev, vaddr);
  710. }
  711. EXPORT_SYMBOL_GPL(dma_vunmap_noncontiguous);
  712. int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma,
  713. size_t size, struct sg_table *sgt)
  714. {
  715. if (use_dma_iommu(dev))
  716. return iommu_dma_mmap_noncontiguous(dev, vma, size, sgt);
  717. return dma_mmap_pages(dev, vma, size, sg_page(sgt->sgl));
  718. }
  719. EXPORT_SYMBOL_GPL(dma_mmap_noncontiguous);
  720. static int dma_supported(struct device *dev, u64 mask)
  721. {
  722. const struct dma_map_ops *ops = get_dma_ops(dev);
  723. if (use_dma_iommu(dev)) {
  724. if (WARN_ON(ops))
  725. return false;
  726. return true;
  727. }
  728. /*
  729. * ->dma_supported sets and clears the bypass flag, so ignore it here
  730. * and always call into the method if there is one.
  731. */
  732. if (ops) {
  733. if (!ops->dma_supported)
  734. return true;
  735. return ops->dma_supported(dev, mask);
  736. }
  737. return dma_direct_supported(dev, mask);
  738. }
  739. bool dma_pci_p2pdma_supported(struct device *dev)
  740. {
  741. const struct dma_map_ops *ops = get_dma_ops(dev);
  742. /*
  743. * Note: dma_ops_bypass is not checked here because P2PDMA should
  744. * not be used with dma mapping ops that do not have support even
  745. * if the specific device is bypassing them.
  746. */
  747. /* if ops is not set, dma direct and default IOMMU support P2PDMA */
  748. return !ops;
  749. }
  750. EXPORT_SYMBOL_GPL(dma_pci_p2pdma_supported);
  751. int dma_set_mask(struct device *dev, u64 mask)
  752. {
  753. /*
  754. * Truncate the mask to the actually supported dma_addr_t width to
  755. * avoid generating unsupportable addresses.
  756. */
  757. mask = (dma_addr_t)mask;
  758. if (!dev->dma_mask || !dma_supported(dev, mask))
  759. return -EIO;
  760. arch_dma_set_mask(dev, mask);
  761. *dev->dma_mask = mask;
  762. dma_setup_need_sync(dev);
  763. return 0;
  764. }
  765. EXPORT_SYMBOL(dma_set_mask);
  766. int dma_set_coherent_mask(struct device *dev, u64 mask)
  767. {
  768. /*
  769. * Truncate the mask to the actually supported dma_addr_t width to
  770. * avoid generating unsupportable addresses.
  771. */
  772. mask = (dma_addr_t)mask;
  773. if (!dma_supported(dev, mask))
  774. return -EIO;
  775. dev->coherent_dma_mask = mask;
  776. return 0;
  777. }
  778. EXPORT_SYMBOL(dma_set_coherent_mask);
  779. /**
  780. * dma_addressing_limited - return if the device is addressing limited
  781. * @dev: device to check
  782. *
  783. * Return %true if the devices DMA mask is too small to address all memory in
  784. * the system, else %false. Lack of addressing bits is the prime reason for
  785. * bounce buffering, but might not be the only one.
  786. */
  787. bool dma_addressing_limited(struct device *dev)
  788. {
  789. const struct dma_map_ops *ops = get_dma_ops(dev);
  790. if (min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) <
  791. dma_get_required_mask(dev))
  792. return true;
  793. if (unlikely(ops) || use_dma_iommu(dev))
  794. return false;
  795. return !dma_direct_all_ram_mapped(dev);
  796. }
  797. EXPORT_SYMBOL_GPL(dma_addressing_limited);
  798. size_t dma_max_mapping_size(struct device *dev)
  799. {
  800. const struct dma_map_ops *ops = get_dma_ops(dev);
  801. size_t size = SIZE_MAX;
  802. if (dma_map_direct(dev, ops))
  803. size = dma_direct_max_mapping_size(dev);
  804. else if (use_dma_iommu(dev))
  805. size = iommu_dma_max_mapping_size(dev);
  806. else if (ops && ops->max_mapping_size)
  807. size = ops->max_mapping_size(dev);
  808. return size;
  809. }
  810. EXPORT_SYMBOL_GPL(dma_max_mapping_size);
  811. size_t dma_opt_mapping_size(struct device *dev)
  812. {
  813. const struct dma_map_ops *ops = get_dma_ops(dev);
  814. size_t size = SIZE_MAX;
  815. if (use_dma_iommu(dev))
  816. size = iommu_dma_opt_mapping_size();
  817. else if (ops && ops->opt_mapping_size)
  818. size = ops->opt_mapping_size();
  819. return min(dma_max_mapping_size(dev), size);
  820. }
  821. EXPORT_SYMBOL_GPL(dma_opt_mapping_size);
  822. unsigned long dma_get_merge_boundary(struct device *dev)
  823. {
  824. const struct dma_map_ops *ops = get_dma_ops(dev);
  825. if (use_dma_iommu(dev))
  826. return iommu_dma_get_merge_boundary(dev);
  827. if (!ops || !ops->get_merge_boundary)
  828. return 0; /* can't merge */
  829. return ops->get_merge_boundary(dev);
  830. }
  831. EXPORT_SYMBOL_GPL(dma_get_merge_boundary);