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