mapping.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. trace_dma_map_sg_err(dev, sg, nents, ents, dir, attrs);
  194. return -EIO;
  195. }
  196. return ents;
  197. }
  198. /**
  199. * dma_map_sg_attrs - Map the given buffer for DMA
  200. * @dev: The device for which to perform the DMA operation
  201. * @sg: The sg_table object describing the buffer
  202. * @nents: Number of entries to map
  203. * @dir: DMA direction
  204. * @attrs: Optional DMA attributes for the map operation
  205. *
  206. * Maps a buffer described by a scatterlist passed in the sg argument with
  207. * nents segments for the @dir DMA operation by the @dev device.
  208. *
  209. * Returns the number of mapped entries (which can be less than nents)
  210. * on success. Zero is returned for any error.
  211. *
  212. * dma_unmap_sg_attrs() should be used to unmap the buffer with the
  213. * original sg and original nents (not the value returned by this funciton).
  214. */
  215. unsigned int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  216. int nents, enum dma_data_direction dir, unsigned long attrs)
  217. {
  218. int ret;
  219. ret = __dma_map_sg_attrs(dev, sg, nents, dir, attrs);
  220. if (ret < 0)
  221. return 0;
  222. return ret;
  223. }
  224. EXPORT_SYMBOL(dma_map_sg_attrs);
  225. /**
  226. * dma_map_sgtable - Map the given buffer for DMA
  227. * @dev: The device for which to perform the DMA operation
  228. * @sgt: The sg_table object describing the buffer
  229. * @dir: DMA direction
  230. * @attrs: Optional DMA attributes for the map operation
  231. *
  232. * Maps a buffer described by a scatterlist stored in the given sg_table
  233. * object for the @dir DMA operation by the @dev device. After success, the
  234. * ownership for the buffer is transferred to the DMA domain. One has to
  235. * call dma_sync_sgtable_for_cpu() or dma_unmap_sgtable() to move the
  236. * ownership of the buffer back to the CPU domain before touching the
  237. * buffer by the CPU.
  238. *
  239. * Returns 0 on success or a negative error code on error. The following
  240. * error codes are supported with the given meaning:
  241. *
  242. * -EINVAL An invalid argument, unaligned access or other error
  243. * in usage. Will not succeed if retried.
  244. * -ENOMEM Insufficient resources (like memory or IOVA space) to
  245. * complete the mapping. Should succeed if retried later.
  246. * -EIO Legacy error code with an unknown meaning. eg. this is
  247. * returned if a lower level call returned
  248. * DMA_MAPPING_ERROR.
  249. * -EREMOTEIO The DMA device cannot access P2PDMA memory specified
  250. * in the sg_table. This will not succeed if retried.
  251. */
  252. int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
  253. enum dma_data_direction dir, unsigned long attrs)
  254. {
  255. int nents;
  256. nents = __dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
  257. if (nents < 0)
  258. return nents;
  259. sgt->nents = nents;
  260. return 0;
  261. }
  262. EXPORT_SYMBOL_GPL(dma_map_sgtable);
  263. void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  264. int nents, enum dma_data_direction dir,
  265. unsigned long attrs)
  266. {
  267. const struct dma_map_ops *ops = get_dma_ops(dev);
  268. BUG_ON(!valid_dma_direction(dir));
  269. trace_dma_unmap_sg(dev, sg, nents, dir, attrs);
  270. debug_dma_unmap_sg(dev, sg, nents, dir);
  271. if (dma_map_direct(dev, ops) ||
  272. arch_dma_unmap_sg_direct(dev, sg, nents))
  273. dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
  274. else if (use_dma_iommu(dev))
  275. iommu_dma_unmap_sg(dev, sg, nents, dir, attrs);
  276. else if (ops->unmap_sg)
  277. ops->unmap_sg(dev, sg, nents, dir, attrs);
  278. }
  279. EXPORT_SYMBOL(dma_unmap_sg_attrs);
  280. dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
  281. size_t size, enum dma_data_direction dir, unsigned long attrs)
  282. {
  283. const struct dma_map_ops *ops = get_dma_ops(dev);
  284. dma_addr_t addr = DMA_MAPPING_ERROR;
  285. BUG_ON(!valid_dma_direction(dir));
  286. if (WARN_ON_ONCE(!dev->dma_mask))
  287. return DMA_MAPPING_ERROR;
  288. if (dma_map_direct(dev, ops))
  289. addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
  290. else if (use_dma_iommu(dev))
  291. addr = iommu_dma_map_resource(dev, phys_addr, size, dir, attrs);
  292. else if (ops->map_resource)
  293. addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
  294. trace_dma_map_resource(dev, phys_addr, addr, size, dir, attrs);
  295. debug_dma_map_resource(dev, phys_addr, size, dir, addr, attrs);
  296. return addr;
  297. }
  298. EXPORT_SYMBOL(dma_map_resource);
  299. void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
  300. enum dma_data_direction dir, unsigned long attrs)
  301. {
  302. const struct dma_map_ops *ops = get_dma_ops(dev);
  303. BUG_ON(!valid_dma_direction(dir));
  304. if (dma_map_direct(dev, ops))
  305. ; /* nothing to do: uncached and no swiotlb */
  306. else if (use_dma_iommu(dev))
  307. iommu_dma_unmap_resource(dev, addr, size, dir, attrs);
  308. else if (ops->unmap_resource)
  309. ops->unmap_resource(dev, addr, size, dir, attrs);
  310. trace_dma_unmap_resource(dev, addr, size, dir, attrs);
  311. debug_dma_unmap_resource(dev, addr, size, dir);
  312. }
  313. EXPORT_SYMBOL(dma_unmap_resource);
  314. #ifdef CONFIG_DMA_NEED_SYNC
  315. void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
  316. enum dma_data_direction dir)
  317. {
  318. const struct dma_map_ops *ops = get_dma_ops(dev);
  319. BUG_ON(!valid_dma_direction(dir));
  320. if (dma_map_direct(dev, ops))
  321. dma_direct_sync_single_for_cpu(dev, addr, size, dir);
  322. else if (use_dma_iommu(dev))
  323. iommu_dma_sync_single_for_cpu(dev, addr, size, dir);
  324. else if (ops->sync_single_for_cpu)
  325. ops->sync_single_for_cpu(dev, addr, size, dir);
  326. trace_dma_sync_single_for_cpu(dev, addr, size, dir);
  327. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  328. }
  329. EXPORT_SYMBOL(__dma_sync_single_for_cpu);
  330. void __dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
  331. size_t size, enum dma_data_direction dir)
  332. {
  333. const struct dma_map_ops *ops = get_dma_ops(dev);
  334. BUG_ON(!valid_dma_direction(dir));
  335. if (dma_map_direct(dev, ops))
  336. dma_direct_sync_single_for_device(dev, addr, size, dir);
  337. else if (use_dma_iommu(dev))
  338. iommu_dma_sync_single_for_device(dev, addr, size, dir);
  339. else if (ops->sync_single_for_device)
  340. ops->sync_single_for_device(dev, addr, size, dir);
  341. trace_dma_sync_single_for_device(dev, addr, size, dir);
  342. debug_dma_sync_single_for_device(dev, addr, size, dir);
  343. }
  344. EXPORT_SYMBOL(__dma_sync_single_for_device);
  345. void __dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  346. int nelems, 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 (dma_map_direct(dev, ops))
  351. dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir);
  352. else if (use_dma_iommu(dev))
  353. iommu_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  354. else if (ops->sync_sg_for_cpu)
  355. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  356. trace_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  357. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  358. }
  359. EXPORT_SYMBOL(__dma_sync_sg_for_cpu);
  360. void __dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  361. int nelems, enum dma_data_direction dir)
  362. {
  363. const struct dma_map_ops *ops = get_dma_ops(dev);
  364. BUG_ON(!valid_dma_direction(dir));
  365. if (dma_map_direct(dev, ops))
  366. dma_direct_sync_sg_for_device(dev, sg, nelems, dir);
  367. else if (use_dma_iommu(dev))
  368. iommu_dma_sync_sg_for_device(dev, sg, nelems, dir);
  369. else if (ops->sync_sg_for_device)
  370. ops->sync_sg_for_device(dev, sg, nelems, dir);
  371. trace_dma_sync_sg_for_device(dev, sg, nelems, dir);
  372. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  373. }
  374. EXPORT_SYMBOL(__dma_sync_sg_for_device);
  375. bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr)
  376. {
  377. const struct dma_map_ops *ops = get_dma_ops(dev);
  378. if (dma_map_direct(dev, ops))
  379. /*
  380. * dma_skip_sync could've been reset on first SWIOTLB buffer
  381. * mapping, but @dma_addr is not necessary an SWIOTLB buffer.
  382. * In this case, fall back to more granular check.
  383. */
  384. return dma_direct_need_sync(dev, dma_addr);
  385. return true;
  386. }
  387. EXPORT_SYMBOL_GPL(__dma_need_sync);
  388. static void dma_setup_need_sync(struct device *dev)
  389. {
  390. const struct dma_map_ops *ops = get_dma_ops(dev);
  391. if (dma_map_direct(dev, ops) || use_dma_iommu(dev))
  392. /*
  393. * dma_skip_sync will be reset to %false on first SWIOTLB buffer
  394. * mapping, if any. During the device initialization, it's
  395. * enough to check only for the DMA coherence.
  396. */
  397. dev->dma_skip_sync = dev_is_dma_coherent(dev);
  398. else if (!ops->sync_single_for_device && !ops->sync_single_for_cpu &&
  399. !ops->sync_sg_for_device && !ops->sync_sg_for_cpu)
  400. /*
  401. * Synchronization is not possible when none of DMA sync ops
  402. * is set.
  403. */
  404. dev->dma_skip_sync = true;
  405. else
  406. dev->dma_skip_sync = false;
  407. }
  408. #else /* !CONFIG_DMA_NEED_SYNC */
  409. static inline void dma_setup_need_sync(struct device *dev) { }
  410. #endif /* !CONFIG_DMA_NEED_SYNC */
  411. /*
  412. * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
  413. * that the intention is to allow exporting memory allocated via the
  414. * coherent DMA APIs through the dma_buf API, which only accepts a
  415. * scattertable. This presents a couple of problems:
  416. * 1. Not all memory allocated via the coherent DMA APIs is backed by
  417. * a struct page
  418. * 2. Passing coherent DMA memory into the streaming APIs is not allowed
  419. * as we will try to flush the memory through a different alias to that
  420. * actually being used (and the flushes are redundant.)
  421. */
  422. int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
  423. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  424. unsigned long attrs)
  425. {
  426. const struct dma_map_ops *ops = get_dma_ops(dev);
  427. if (dma_alloc_direct(dev, ops))
  428. return dma_direct_get_sgtable(dev, sgt, cpu_addr, dma_addr,
  429. size, attrs);
  430. if (use_dma_iommu(dev))
  431. return iommu_dma_get_sgtable(dev, sgt, cpu_addr, dma_addr,
  432. size, attrs);
  433. if (!ops->get_sgtable)
  434. return -ENXIO;
  435. return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs);
  436. }
  437. EXPORT_SYMBOL(dma_get_sgtable_attrs);
  438. #ifdef CONFIG_MMU
  439. /*
  440. * Return the page attributes used for mapping dma_alloc_* memory, either in
  441. * kernel space if remapping is needed, or to userspace through dma_mmap_*.
  442. */
  443. pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs)
  444. {
  445. if (dev_is_dma_coherent(dev))
  446. return prot;
  447. #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE
  448. if (attrs & DMA_ATTR_WRITE_COMBINE)
  449. return pgprot_writecombine(prot);
  450. #endif
  451. return pgprot_dmacoherent(prot);
  452. }
  453. #endif /* CONFIG_MMU */
  454. /**
  455. * dma_can_mmap - check if a given device supports dma_mmap_*
  456. * @dev: device to check
  457. *
  458. * Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to
  459. * map DMA allocations to userspace.
  460. */
  461. bool dma_can_mmap(struct device *dev)
  462. {
  463. const struct dma_map_ops *ops = get_dma_ops(dev);
  464. if (dma_alloc_direct(dev, ops))
  465. return dma_direct_can_mmap(dev);
  466. if (use_dma_iommu(dev))
  467. return true;
  468. return ops->mmap != NULL;
  469. }
  470. EXPORT_SYMBOL_GPL(dma_can_mmap);
  471. /**
  472. * dma_mmap_attrs - map a coherent DMA allocation into user space
  473. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  474. * @vma: vm_area_struct describing requested user mapping
  475. * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
  476. * @dma_addr: device-view address returned from dma_alloc_attrs
  477. * @size: size of memory originally requested in dma_alloc_attrs
  478. * @attrs: attributes of mapping properties requested in dma_alloc_attrs
  479. *
  480. * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user
  481. * space. The coherent DMA buffer must not be freed by the driver until the
  482. * user space mapping has been released.
  483. */
  484. int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
  485. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  486. unsigned long attrs)
  487. {
  488. const struct dma_map_ops *ops = get_dma_ops(dev);
  489. if (dma_alloc_direct(dev, ops))
  490. return dma_direct_mmap(dev, vma, cpu_addr, dma_addr, size,
  491. attrs);
  492. if (use_dma_iommu(dev))
  493. return iommu_dma_mmap(dev, vma, cpu_addr, dma_addr, size,
  494. attrs);
  495. if (!ops->mmap)
  496. return -ENXIO;
  497. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  498. }
  499. EXPORT_SYMBOL(dma_mmap_attrs);
  500. u64 dma_get_required_mask(struct device *dev)
  501. {
  502. const struct dma_map_ops *ops = get_dma_ops(dev);
  503. if (dma_alloc_direct(dev, ops))
  504. return dma_direct_get_required_mask(dev);
  505. if (use_dma_iommu(dev))
  506. return DMA_BIT_MASK(32);
  507. if (ops->get_required_mask)
  508. return ops->get_required_mask(dev);
  509. /*
  510. * We require every DMA ops implementation to at least support a 32-bit
  511. * DMA mask (and use bounce buffering if that isn't supported in
  512. * hardware). As the direct mapping code has its own routine to
  513. * actually report an optimal mask we default to 32-bit here as that
  514. * is the right thing for most IOMMUs, and at least not actively
  515. * harmful in general.
  516. */
  517. return DMA_BIT_MASK(32);
  518. }
  519. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  520. void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
  521. gfp_t flag, unsigned long attrs)
  522. {
  523. const struct dma_map_ops *ops = get_dma_ops(dev);
  524. void *cpu_addr;
  525. WARN_ON_ONCE(!dev->coherent_dma_mask);
  526. /*
  527. * DMA allocations can never be turned back into a page pointer, so
  528. * requesting compound pages doesn't make sense (and can't even be
  529. * supported at all by various backends).
  530. */
  531. if (WARN_ON_ONCE(flag & __GFP_COMP))
  532. return NULL;
  533. if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr)) {
  534. trace_dma_alloc(dev, cpu_addr, *dma_handle, size,
  535. DMA_BIDIRECTIONAL, flag, attrs);
  536. return cpu_addr;
  537. }
  538. /* let the implementation decide on the zone to allocate from: */
  539. flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  540. if (dma_alloc_direct(dev, ops)) {
  541. cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
  542. } else if (use_dma_iommu(dev)) {
  543. cpu_addr = iommu_dma_alloc(dev, size, dma_handle, flag, attrs);
  544. } else if (ops->alloc) {
  545. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  546. } else {
  547. trace_dma_alloc(dev, NULL, 0, size, DMA_BIDIRECTIONAL, flag,
  548. attrs);
  549. return NULL;
  550. }
  551. trace_dma_alloc(dev, cpu_addr, *dma_handle, size, DMA_BIDIRECTIONAL,
  552. flag, attrs);
  553. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr, attrs);
  554. return cpu_addr;
  555. }
  556. EXPORT_SYMBOL(dma_alloc_attrs);
  557. void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  558. dma_addr_t dma_handle, unsigned long attrs)
  559. {
  560. const struct dma_map_ops *ops = get_dma_ops(dev);
  561. if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
  562. return;
  563. /*
  564. * On non-coherent platforms which implement DMA-coherent buffers via
  565. * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
  566. * this far in IRQ context is a) at risk of a BUG_ON() or trying to
  567. * sleep on some machines, and b) an indication that the driver is
  568. * probably misusing the coherent API anyway.
  569. */
  570. WARN_ON(irqs_disabled());
  571. trace_dma_free(dev, cpu_addr, dma_handle, size, DMA_BIDIRECTIONAL,
  572. attrs);
  573. if (!cpu_addr)
  574. return;
  575. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  576. if (dma_alloc_direct(dev, ops))
  577. dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
  578. else if (use_dma_iommu(dev))
  579. iommu_dma_free(dev, size, cpu_addr, dma_handle, attrs);
  580. else if (ops->free)
  581. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  582. }
  583. EXPORT_SYMBOL(dma_free_attrs);
  584. static struct page *__dma_alloc_pages(struct device *dev, size_t size,
  585. dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
  586. {
  587. const struct dma_map_ops *ops = get_dma_ops(dev);
  588. if (WARN_ON_ONCE(!dev->coherent_dma_mask))
  589. return NULL;
  590. if (WARN_ON_ONCE(gfp & (__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM)))
  591. return NULL;
  592. if (WARN_ON_ONCE(gfp & __GFP_COMP))
  593. return NULL;
  594. size = PAGE_ALIGN(size);
  595. if (dma_alloc_direct(dev, ops))
  596. return dma_direct_alloc_pages(dev, size, dma_handle, dir, gfp);
  597. if (use_dma_iommu(dev))
  598. return dma_common_alloc_pages(dev, size, dma_handle, dir, gfp);
  599. if (!ops->alloc_pages_op)
  600. return NULL;
  601. return ops->alloc_pages_op(dev, size, dma_handle, dir, gfp);
  602. }
  603. struct page *dma_alloc_pages(struct device *dev, size_t size,
  604. dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
  605. {
  606. struct page *page = __dma_alloc_pages(dev, size, dma_handle, dir, gfp);
  607. if (page) {
  608. trace_dma_alloc_pages(dev, page_to_virt(page), *dma_handle,
  609. size, dir, gfp, 0);
  610. debug_dma_alloc_pages(dev, page, size, dir, *dma_handle, 0);
  611. } else {
  612. trace_dma_alloc_pages(dev, NULL, 0, size, dir, gfp, 0);
  613. }
  614. return page;
  615. }
  616. EXPORT_SYMBOL_GPL(dma_alloc_pages);
  617. static void __dma_free_pages(struct device *dev, size_t size, struct page *page,
  618. dma_addr_t dma_handle, enum dma_data_direction dir)
  619. {
  620. const struct dma_map_ops *ops = get_dma_ops(dev);
  621. size = PAGE_ALIGN(size);
  622. if (dma_alloc_direct(dev, ops))
  623. dma_direct_free_pages(dev, size, page, dma_handle, dir);
  624. else if (use_dma_iommu(dev))
  625. dma_common_free_pages(dev, size, page, dma_handle, dir);
  626. else if (ops->free_pages)
  627. ops->free_pages(dev, size, page, dma_handle, dir);
  628. }
  629. void dma_free_pages(struct device *dev, size_t size, struct page *page,
  630. dma_addr_t dma_handle, enum dma_data_direction dir)
  631. {
  632. trace_dma_free_pages(dev, page_to_virt(page), dma_handle, size, dir, 0);
  633. debug_dma_free_pages(dev, page, size, dir, dma_handle);
  634. __dma_free_pages(dev, size, page, dma_handle, dir);
  635. }
  636. EXPORT_SYMBOL_GPL(dma_free_pages);
  637. int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma,
  638. size_t size, struct page *page)
  639. {
  640. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  641. if (vma->vm_pgoff >= count || vma_pages(vma) > count - vma->vm_pgoff)
  642. return -ENXIO;
  643. return remap_pfn_range(vma, vma->vm_start,
  644. page_to_pfn(page) + vma->vm_pgoff,
  645. vma_pages(vma) << PAGE_SHIFT, vma->vm_page_prot);
  646. }
  647. EXPORT_SYMBOL_GPL(dma_mmap_pages);
  648. static struct sg_table *alloc_single_sgt(struct device *dev, size_t size,
  649. enum dma_data_direction dir, gfp_t gfp)
  650. {
  651. struct sg_table *sgt;
  652. struct page *page;
  653. sgt = kmalloc(sizeof(*sgt), gfp);
  654. if (!sgt)
  655. return NULL;
  656. if (sg_alloc_table(sgt, 1, gfp))
  657. goto out_free_sgt;
  658. page = __dma_alloc_pages(dev, size, &sgt->sgl->dma_address, dir, gfp);
  659. if (!page)
  660. goto out_free_table;
  661. sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
  662. sg_dma_len(sgt->sgl) = sgt->sgl->length;
  663. return sgt;
  664. out_free_table:
  665. sg_free_table(sgt);
  666. out_free_sgt:
  667. kfree(sgt);
  668. return NULL;
  669. }
  670. struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size,
  671. enum dma_data_direction dir, gfp_t gfp, unsigned long attrs)
  672. {
  673. struct sg_table *sgt;
  674. if (WARN_ON_ONCE(attrs & ~DMA_ATTR_ALLOC_SINGLE_PAGES))
  675. return NULL;
  676. if (WARN_ON_ONCE(gfp & __GFP_COMP))
  677. return NULL;
  678. if (use_dma_iommu(dev))
  679. sgt = iommu_dma_alloc_noncontiguous(dev, size, dir, gfp, attrs);
  680. else
  681. sgt = alloc_single_sgt(dev, size, dir, gfp);
  682. if (sgt) {
  683. sgt->nents = 1;
  684. trace_dma_alloc_sgt(dev, sgt, size, dir, gfp, attrs);
  685. debug_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs);
  686. } else {
  687. trace_dma_alloc_sgt_err(dev, NULL, 0, size, dir, gfp, attrs);
  688. }
  689. return sgt;
  690. }
  691. EXPORT_SYMBOL_GPL(dma_alloc_noncontiguous);
  692. static void free_single_sgt(struct device *dev, size_t size,
  693. struct sg_table *sgt, enum dma_data_direction dir)
  694. {
  695. __dma_free_pages(dev, size, sg_page(sgt->sgl), sgt->sgl->dma_address,
  696. dir);
  697. sg_free_table(sgt);
  698. kfree(sgt);
  699. }
  700. void dma_free_noncontiguous(struct device *dev, size_t size,
  701. struct sg_table *sgt, enum dma_data_direction dir)
  702. {
  703. trace_dma_free_sgt(dev, sgt, size, dir);
  704. debug_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
  705. if (use_dma_iommu(dev))
  706. iommu_dma_free_noncontiguous(dev, size, sgt, dir);
  707. else
  708. free_single_sgt(dev, size, sgt, dir);
  709. }
  710. EXPORT_SYMBOL_GPL(dma_free_noncontiguous);
  711. void *dma_vmap_noncontiguous(struct device *dev, size_t size,
  712. struct sg_table *sgt)
  713. {
  714. if (use_dma_iommu(dev))
  715. return iommu_dma_vmap_noncontiguous(dev, size, sgt);
  716. return page_address(sg_page(sgt->sgl));
  717. }
  718. EXPORT_SYMBOL_GPL(dma_vmap_noncontiguous);
  719. void dma_vunmap_noncontiguous(struct device *dev, void *vaddr)
  720. {
  721. if (use_dma_iommu(dev))
  722. iommu_dma_vunmap_noncontiguous(dev, vaddr);
  723. }
  724. EXPORT_SYMBOL_GPL(dma_vunmap_noncontiguous);
  725. int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma,
  726. size_t size, struct sg_table *sgt)
  727. {
  728. if (use_dma_iommu(dev))
  729. return iommu_dma_mmap_noncontiguous(dev, vma, size, sgt);
  730. return dma_mmap_pages(dev, vma, size, sg_page(sgt->sgl));
  731. }
  732. EXPORT_SYMBOL_GPL(dma_mmap_noncontiguous);
  733. static int dma_supported(struct device *dev, u64 mask)
  734. {
  735. const struct dma_map_ops *ops = get_dma_ops(dev);
  736. if (use_dma_iommu(dev)) {
  737. if (WARN_ON(ops))
  738. return false;
  739. return true;
  740. }
  741. /*
  742. * ->dma_supported sets and clears the bypass flag, so ignore it here
  743. * and always call into the method if there is one.
  744. */
  745. if (ops) {
  746. if (!ops->dma_supported)
  747. return true;
  748. return ops->dma_supported(dev, mask);
  749. }
  750. return dma_direct_supported(dev, mask);
  751. }
  752. bool dma_pci_p2pdma_supported(struct device *dev)
  753. {
  754. const struct dma_map_ops *ops = get_dma_ops(dev);
  755. /*
  756. * Note: dma_ops_bypass is not checked here because P2PDMA should
  757. * not be used with dma mapping ops that do not have support even
  758. * if the specific device is bypassing them.
  759. */
  760. /* if ops is not set, dma direct and default IOMMU support P2PDMA */
  761. return !ops;
  762. }
  763. EXPORT_SYMBOL_GPL(dma_pci_p2pdma_supported);
  764. int dma_set_mask(struct device *dev, u64 mask)
  765. {
  766. /*
  767. * Truncate the mask to the actually supported dma_addr_t width to
  768. * avoid generating unsupportable addresses.
  769. */
  770. mask = (dma_addr_t)mask;
  771. if (!dev->dma_mask || !dma_supported(dev, mask))
  772. return -EIO;
  773. arch_dma_set_mask(dev, mask);
  774. *dev->dma_mask = mask;
  775. dma_setup_need_sync(dev);
  776. return 0;
  777. }
  778. EXPORT_SYMBOL(dma_set_mask);
  779. int dma_set_coherent_mask(struct device *dev, u64 mask)
  780. {
  781. /*
  782. * Truncate the mask to the actually supported dma_addr_t width to
  783. * avoid generating unsupportable addresses.
  784. */
  785. mask = (dma_addr_t)mask;
  786. if (!dma_supported(dev, mask))
  787. return -EIO;
  788. dev->coherent_dma_mask = mask;
  789. return 0;
  790. }
  791. EXPORT_SYMBOL(dma_set_coherent_mask);
  792. static bool __dma_addressing_limited(struct device *dev)
  793. {
  794. const struct dma_map_ops *ops = get_dma_ops(dev);
  795. if (min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) <
  796. dma_get_required_mask(dev))
  797. return true;
  798. if (unlikely(ops) || use_dma_iommu(dev))
  799. return false;
  800. return !dma_direct_all_ram_mapped(dev);
  801. }
  802. /**
  803. * dma_addressing_limited - return if the device is addressing limited
  804. * @dev: device to check
  805. *
  806. * Return %true if the devices DMA mask is too small to address all memory in
  807. * the system, else %false. Lack of addressing bits is the prime reason for
  808. * bounce buffering, but might not be the only one.
  809. */
  810. bool dma_addressing_limited(struct device *dev)
  811. {
  812. if (!__dma_addressing_limited(dev))
  813. return false;
  814. dev_dbg(dev, "device is DMA addressing limited\n");
  815. return true;
  816. }
  817. EXPORT_SYMBOL_GPL(dma_addressing_limited);
  818. size_t dma_max_mapping_size(struct device *dev)
  819. {
  820. const struct dma_map_ops *ops = get_dma_ops(dev);
  821. size_t size = SIZE_MAX;
  822. if (dma_map_direct(dev, ops))
  823. size = dma_direct_max_mapping_size(dev);
  824. else if (use_dma_iommu(dev))
  825. size = iommu_dma_max_mapping_size(dev);
  826. else if (ops && ops->max_mapping_size)
  827. size = ops->max_mapping_size(dev);
  828. return size;
  829. }
  830. EXPORT_SYMBOL_GPL(dma_max_mapping_size);
  831. size_t dma_opt_mapping_size(struct device *dev)
  832. {
  833. const struct dma_map_ops *ops = get_dma_ops(dev);
  834. size_t size = SIZE_MAX;
  835. if (use_dma_iommu(dev))
  836. size = iommu_dma_opt_mapping_size();
  837. else if (ops && ops->opt_mapping_size)
  838. size = ops->opt_mapping_size();
  839. return min(dma_max_mapping_size(dev), size);
  840. }
  841. EXPORT_SYMBOL_GPL(dma_opt_mapping_size);
  842. unsigned long dma_get_merge_boundary(struct device *dev)
  843. {
  844. const struct dma_map_ops *ops = get_dma_ops(dev);
  845. if (use_dma_iommu(dev))
  846. return iommu_dma_get_merge_boundary(dev);
  847. if (!ops || !ops->get_merge_boundary)
  848. return 0; /* can't merge */
  849. return ops->get_merge_boundary(dev);
  850. }
  851. EXPORT_SYMBOL_GPL(dma_get_merge_boundary);