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