xref: /openbmc/linux/kernel/dma/mapping.c (revision 198c50e2ccff5c78ddbe0cb01593ac32458deb69)
1cf65a0f6SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2cf65a0f6SChristoph Hellwig /*
3cf65a0f6SChristoph Hellwig  * arch-independent dma-mapping routines
4cf65a0f6SChristoph Hellwig  *
5cf65a0f6SChristoph Hellwig  * Copyright (c) 2006  SUSE Linux Products GmbH
6cf65a0f6SChristoph Hellwig  * Copyright (c) 2006  Tejun Heo <teheo@suse.de>
7cf65a0f6SChristoph Hellwig  */
805887cb6SChristoph Hellwig #include <linux/memblock.h> /* for max_pfn */
9cf65a0f6SChristoph Hellwig #include <linux/acpi.h>
100a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
11cf65a0f6SChristoph Hellwig #include <linux/export.h>
12cf65a0f6SChristoph Hellwig #include <linux/gfp.h>
13cf65a0f6SChristoph Hellwig #include <linux/of_device.h>
14cf65a0f6SChristoph Hellwig #include <linux/slab.h>
15cf65a0f6SChristoph Hellwig #include <linux/vmalloc.h>
16a1fd09e8SChristoph Hellwig #include "debug.h"
1719c65c3dSChristoph Hellwig #include "direct.h"
18cf65a0f6SChristoph Hellwig 
196d4e9a8eSChristoph Hellwig bool dma_default_coherent;
206d4e9a8eSChristoph Hellwig 
21cf65a0f6SChristoph Hellwig /*
22cf65a0f6SChristoph Hellwig  * Managed DMA API
23cf65a0f6SChristoph Hellwig  */
24cf65a0f6SChristoph Hellwig struct dma_devres {
25cf65a0f6SChristoph Hellwig 	size_t		size;
26cf65a0f6SChristoph Hellwig 	void		*vaddr;
27cf65a0f6SChristoph Hellwig 	dma_addr_t	dma_handle;
28cf65a0f6SChristoph Hellwig 	unsigned long	attrs;
29cf65a0f6SChristoph Hellwig };
30cf65a0f6SChristoph Hellwig 
31cf65a0f6SChristoph Hellwig static void dmam_release(struct device *dev, void *res)
32cf65a0f6SChristoph Hellwig {
33cf65a0f6SChristoph Hellwig 	struct dma_devres *this = res;
34cf65a0f6SChristoph Hellwig 
35cf65a0f6SChristoph Hellwig 	dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
36cf65a0f6SChristoph Hellwig 			this->attrs);
37cf65a0f6SChristoph Hellwig }
38cf65a0f6SChristoph Hellwig 
39cf65a0f6SChristoph Hellwig static int dmam_match(struct device *dev, void *res, void *match_data)
40cf65a0f6SChristoph Hellwig {
41cf65a0f6SChristoph Hellwig 	struct dma_devres *this = res, *match = match_data;
42cf65a0f6SChristoph Hellwig 
43cf65a0f6SChristoph Hellwig 	if (this->vaddr == match->vaddr) {
44cf65a0f6SChristoph Hellwig 		WARN_ON(this->size != match->size ||
45cf65a0f6SChristoph Hellwig 			this->dma_handle != match->dma_handle);
46cf65a0f6SChristoph Hellwig 		return 1;
47cf65a0f6SChristoph Hellwig 	}
48cf65a0f6SChristoph Hellwig 	return 0;
49cf65a0f6SChristoph Hellwig }
50cf65a0f6SChristoph Hellwig 
51cf65a0f6SChristoph Hellwig /**
52cf65a0f6SChristoph Hellwig  * dmam_free_coherent - Managed dma_free_coherent()
53cf65a0f6SChristoph Hellwig  * @dev: Device to free coherent memory for
54cf65a0f6SChristoph Hellwig  * @size: Size of allocation
55cf65a0f6SChristoph Hellwig  * @vaddr: Virtual address of the memory to free
56cf65a0f6SChristoph Hellwig  * @dma_handle: DMA handle of the memory to free
57cf65a0f6SChristoph Hellwig  *
58cf65a0f6SChristoph Hellwig  * Managed dma_free_coherent().
59cf65a0f6SChristoph Hellwig  */
60cf65a0f6SChristoph Hellwig void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
61cf65a0f6SChristoph Hellwig 			dma_addr_t dma_handle)
62cf65a0f6SChristoph Hellwig {
63cf65a0f6SChristoph Hellwig 	struct dma_devres match_data = { size, vaddr, dma_handle };
64cf65a0f6SChristoph Hellwig 
65cf65a0f6SChristoph Hellwig 	dma_free_coherent(dev, size, vaddr, dma_handle);
66cf65a0f6SChristoph Hellwig 	WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
67cf65a0f6SChristoph Hellwig }
68cf65a0f6SChristoph Hellwig EXPORT_SYMBOL(dmam_free_coherent);
69cf65a0f6SChristoph Hellwig 
70cf65a0f6SChristoph Hellwig /**
71cf65a0f6SChristoph Hellwig  * dmam_alloc_attrs - Managed dma_alloc_attrs()
72cf65a0f6SChristoph Hellwig  * @dev: Device to allocate non_coherent memory for
73cf65a0f6SChristoph Hellwig  * @size: Size of allocation
74cf65a0f6SChristoph Hellwig  * @dma_handle: Out argument for allocated DMA handle
75cf65a0f6SChristoph Hellwig  * @gfp: Allocation flags
76cf65a0f6SChristoph Hellwig  * @attrs: Flags in the DMA_ATTR_* namespace.
77cf65a0f6SChristoph Hellwig  *
78cf65a0f6SChristoph Hellwig  * Managed dma_alloc_attrs().  Memory allocated using this function will be
79cf65a0f6SChristoph Hellwig  * automatically released on driver detach.
80cf65a0f6SChristoph Hellwig  *
81cf65a0f6SChristoph Hellwig  * RETURNS:
82cf65a0f6SChristoph Hellwig  * Pointer to allocated memory on success, NULL on failure.
83cf65a0f6SChristoph Hellwig  */
84cf65a0f6SChristoph Hellwig void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
85cf65a0f6SChristoph Hellwig 		gfp_t gfp, unsigned long attrs)
86cf65a0f6SChristoph Hellwig {
87cf65a0f6SChristoph Hellwig 	struct dma_devres *dr;
88cf65a0f6SChristoph Hellwig 	void *vaddr;
89cf65a0f6SChristoph Hellwig 
90cf65a0f6SChristoph Hellwig 	dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
91cf65a0f6SChristoph Hellwig 	if (!dr)
92cf65a0f6SChristoph Hellwig 		return NULL;
93cf65a0f6SChristoph Hellwig 
94cf65a0f6SChristoph Hellwig 	vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
95cf65a0f6SChristoph Hellwig 	if (!vaddr) {
96cf65a0f6SChristoph Hellwig 		devres_free(dr);
97cf65a0f6SChristoph Hellwig 		return NULL;
98cf65a0f6SChristoph Hellwig 	}
99cf65a0f6SChristoph Hellwig 
100cf65a0f6SChristoph Hellwig 	dr->vaddr = vaddr;
101cf65a0f6SChristoph Hellwig 	dr->dma_handle = *dma_handle;
102cf65a0f6SChristoph Hellwig 	dr->size = size;
103cf65a0f6SChristoph Hellwig 	dr->attrs = attrs;
104cf65a0f6SChristoph Hellwig 
105cf65a0f6SChristoph Hellwig 	devres_add(dev, dr);
106cf65a0f6SChristoph Hellwig 
107cf65a0f6SChristoph Hellwig 	return vaddr;
108cf65a0f6SChristoph Hellwig }
109cf65a0f6SChristoph Hellwig EXPORT_SYMBOL(dmam_alloc_attrs);
110cf65a0f6SChristoph Hellwig 
111d35834c6SChristoph Hellwig static bool dma_go_direct(struct device *dev, dma_addr_t mask,
112d35834c6SChristoph Hellwig 		const struct dma_map_ops *ops)
113d3fa60d7SChristoph Hellwig {
114d35834c6SChristoph Hellwig 	if (likely(!ops))
115d35834c6SChristoph Hellwig 		return true;
116d35834c6SChristoph Hellwig #ifdef CONFIG_DMA_OPS_BYPASS
117d35834c6SChristoph Hellwig 	if (dev->dma_ops_bypass)
118d35834c6SChristoph Hellwig 		return min_not_zero(mask, dev->bus_dma_limit) >=
119d35834c6SChristoph Hellwig 			    dma_direct_get_required_mask(dev);
120d35834c6SChristoph Hellwig #endif
121d35834c6SChristoph Hellwig 	return false;
122d35834c6SChristoph Hellwig }
123d35834c6SChristoph Hellwig 
124d35834c6SChristoph Hellwig 
125d35834c6SChristoph Hellwig /*
126d35834c6SChristoph Hellwig  * Check if the devices uses a direct mapping for streaming DMA operations.
127d35834c6SChristoph Hellwig  * This allows IOMMU drivers to set a bypass mode if the DMA mask is large
128d35834c6SChristoph Hellwig  * enough.
129d35834c6SChristoph Hellwig  */
130d35834c6SChristoph Hellwig static inline bool dma_alloc_direct(struct device *dev,
131d35834c6SChristoph Hellwig 		const struct dma_map_ops *ops)
132d35834c6SChristoph Hellwig {
133d35834c6SChristoph Hellwig 	return dma_go_direct(dev, dev->coherent_dma_mask, ops);
134d35834c6SChristoph Hellwig }
135d35834c6SChristoph Hellwig 
136d35834c6SChristoph Hellwig static inline bool dma_map_direct(struct device *dev,
137d35834c6SChristoph Hellwig 		const struct dma_map_ops *ops)
138d35834c6SChristoph Hellwig {
139d35834c6SChristoph Hellwig 	return dma_go_direct(dev, *dev->dma_mask, ops);
140d3fa60d7SChristoph Hellwig }
141d3fa60d7SChristoph Hellwig 
142d3fa60d7SChristoph Hellwig dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
143d3fa60d7SChristoph Hellwig 		size_t offset, size_t size, enum dma_data_direction dir,
144d3fa60d7SChristoph Hellwig 		unsigned long attrs)
145d3fa60d7SChristoph Hellwig {
146d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
147d3fa60d7SChristoph Hellwig 	dma_addr_t addr;
148d3fa60d7SChristoph Hellwig 
149d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
150f959dcd6SThomas Tai 
151f959dcd6SThomas Tai 	if (WARN_ON_ONCE(!dev->dma_mask))
152f959dcd6SThomas Tai 		return DMA_MAPPING_ERROR;
153f959dcd6SThomas Tai 
1548d8d53cfSAlexey Kardashevskiy 	if (dma_map_direct(dev, ops) ||
1558d8d53cfSAlexey Kardashevskiy 	    arch_dma_map_page_direct(dev, page_to_phys(page) + offset + size))
156d3fa60d7SChristoph Hellwig 		addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
157d3fa60d7SChristoph Hellwig 	else
158d3fa60d7SChristoph Hellwig 		addr = ops->map_page(dev, page, offset, size, dir, attrs);
159d3fa60d7SChristoph Hellwig 	debug_dma_map_page(dev, page, offset, size, dir, addr);
160d3fa60d7SChristoph Hellwig 
161d3fa60d7SChristoph Hellwig 	return addr;
162d3fa60d7SChristoph Hellwig }
163d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_map_page_attrs);
164d3fa60d7SChristoph Hellwig 
165d3fa60d7SChristoph Hellwig void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
166d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
167d3fa60d7SChristoph Hellwig {
168d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
169d3fa60d7SChristoph Hellwig 
170d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
1718d8d53cfSAlexey Kardashevskiy 	if (dma_map_direct(dev, ops) ||
1728d8d53cfSAlexey Kardashevskiy 	    arch_dma_unmap_page_direct(dev, addr + size))
173d3fa60d7SChristoph Hellwig 		dma_direct_unmap_page(dev, addr, size, dir, attrs);
174d3fa60d7SChristoph Hellwig 	else if (ops->unmap_page)
175d3fa60d7SChristoph Hellwig 		ops->unmap_page(dev, addr, size, dir, attrs);
176d3fa60d7SChristoph Hellwig 	debug_dma_unmap_page(dev, addr, size, dir);
177d3fa60d7SChristoph Hellwig }
178d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_unmap_page_attrs);
179d3fa60d7SChristoph Hellwig 
180d3fa60d7SChristoph Hellwig /*
181d3fa60d7SChristoph Hellwig  * dma_maps_sg_attrs returns 0 on error and > 0 on success.
182d3fa60d7SChristoph Hellwig  * It should never return a value < 0.
183d3fa60d7SChristoph Hellwig  */
184d3fa60d7SChristoph Hellwig int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
185d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
186d3fa60d7SChristoph Hellwig {
187d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
188d3fa60d7SChristoph Hellwig 	int ents;
189d3fa60d7SChristoph Hellwig 
190d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
191f959dcd6SThomas Tai 
192f959dcd6SThomas Tai 	if (WARN_ON_ONCE(!dev->dma_mask))
193f959dcd6SThomas Tai 		return 0;
194f959dcd6SThomas Tai 
1958d8d53cfSAlexey Kardashevskiy 	if (dma_map_direct(dev, ops) ||
1968d8d53cfSAlexey Kardashevskiy 	    arch_dma_map_sg_direct(dev, sg, nents))
197d3fa60d7SChristoph Hellwig 		ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
198d3fa60d7SChristoph Hellwig 	else
199d3fa60d7SChristoph Hellwig 		ents = ops->map_sg(dev, sg, nents, dir, attrs);
200d3fa60d7SChristoph Hellwig 	BUG_ON(ents < 0);
201d3fa60d7SChristoph Hellwig 	debug_dma_map_sg(dev, sg, nents, ents, dir);
202d3fa60d7SChristoph Hellwig 
203d3fa60d7SChristoph Hellwig 	return ents;
204d3fa60d7SChristoph Hellwig }
205d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_map_sg_attrs);
206d3fa60d7SChristoph Hellwig 
207d3fa60d7SChristoph Hellwig void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
208d3fa60d7SChristoph Hellwig 				      int nents, enum dma_data_direction dir,
209d3fa60d7SChristoph Hellwig 				      unsigned long attrs)
210d3fa60d7SChristoph Hellwig {
211d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
212d3fa60d7SChristoph Hellwig 
213d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
214d3fa60d7SChristoph Hellwig 	debug_dma_unmap_sg(dev, sg, nents, dir);
2158d8d53cfSAlexey Kardashevskiy 	if (dma_map_direct(dev, ops) ||
2168d8d53cfSAlexey Kardashevskiy 	    arch_dma_unmap_sg_direct(dev, sg, nents))
217d3fa60d7SChristoph Hellwig 		dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
218d3fa60d7SChristoph Hellwig 	else if (ops->unmap_sg)
219d3fa60d7SChristoph Hellwig 		ops->unmap_sg(dev, sg, nents, dir, attrs);
220d3fa60d7SChristoph Hellwig }
221d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_unmap_sg_attrs);
222d3fa60d7SChristoph Hellwig 
223d3fa60d7SChristoph Hellwig dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
224d3fa60d7SChristoph Hellwig 		size_t size, enum dma_data_direction dir, unsigned long attrs)
225d3fa60d7SChristoph Hellwig {
226d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
227d3fa60d7SChristoph Hellwig 	dma_addr_t addr = DMA_MAPPING_ERROR;
228d3fa60d7SChristoph Hellwig 
229d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
230d3fa60d7SChristoph Hellwig 
231f959dcd6SThomas Tai 	if (WARN_ON_ONCE(!dev->dma_mask))
232f959dcd6SThomas Tai 		return DMA_MAPPING_ERROR;
233f959dcd6SThomas Tai 
234d3fa60d7SChristoph Hellwig 	/* Don't allow RAM to be mapped */
235d3fa60d7SChristoph Hellwig 	if (WARN_ON_ONCE(pfn_valid(PHYS_PFN(phys_addr))))
236d3fa60d7SChristoph Hellwig 		return DMA_MAPPING_ERROR;
237d3fa60d7SChristoph Hellwig 
238d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
239d3fa60d7SChristoph Hellwig 		addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
240d3fa60d7SChristoph Hellwig 	else if (ops->map_resource)
241d3fa60d7SChristoph Hellwig 		addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
242d3fa60d7SChristoph Hellwig 
243d3fa60d7SChristoph Hellwig 	debug_dma_map_resource(dev, phys_addr, size, dir, addr);
244d3fa60d7SChristoph Hellwig 	return addr;
245d3fa60d7SChristoph Hellwig }
246d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_map_resource);
247d3fa60d7SChristoph Hellwig 
248d3fa60d7SChristoph Hellwig void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
249d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
250d3fa60d7SChristoph Hellwig {
251d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
252d3fa60d7SChristoph Hellwig 
253d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
254d35834c6SChristoph Hellwig 	if (!dma_map_direct(dev, ops) && ops->unmap_resource)
255d3fa60d7SChristoph Hellwig 		ops->unmap_resource(dev, addr, size, dir, attrs);
256d3fa60d7SChristoph Hellwig 	debug_dma_unmap_resource(dev, addr, size, dir);
257d3fa60d7SChristoph Hellwig }
258d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_unmap_resource);
259d3fa60d7SChristoph Hellwig 
260d3fa60d7SChristoph Hellwig void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
261d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir)
262d3fa60d7SChristoph Hellwig {
263d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
264d3fa60d7SChristoph Hellwig 
265d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
266d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
267d3fa60d7SChristoph Hellwig 		dma_direct_sync_single_for_cpu(dev, addr, size, dir);
268d3fa60d7SChristoph Hellwig 	else if (ops->sync_single_for_cpu)
269d3fa60d7SChristoph Hellwig 		ops->sync_single_for_cpu(dev, addr, size, dir);
270d3fa60d7SChristoph Hellwig 	debug_dma_sync_single_for_cpu(dev, addr, size, dir);
271d3fa60d7SChristoph Hellwig }
272d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_single_for_cpu);
273d3fa60d7SChristoph Hellwig 
274d3fa60d7SChristoph Hellwig void dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
275d3fa60d7SChristoph Hellwig 		size_t size, enum dma_data_direction dir)
276d3fa60d7SChristoph Hellwig {
277d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
278d3fa60d7SChristoph Hellwig 
279d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
280d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
281d3fa60d7SChristoph Hellwig 		dma_direct_sync_single_for_device(dev, addr, size, dir);
282d3fa60d7SChristoph Hellwig 	else if (ops->sync_single_for_device)
283d3fa60d7SChristoph Hellwig 		ops->sync_single_for_device(dev, addr, size, dir);
284d3fa60d7SChristoph Hellwig 	debug_dma_sync_single_for_device(dev, addr, size, dir);
285d3fa60d7SChristoph Hellwig }
286d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_single_for_device);
287d3fa60d7SChristoph Hellwig 
288d3fa60d7SChristoph Hellwig void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
289d3fa60d7SChristoph Hellwig 		    int nelems, enum dma_data_direction dir)
290d3fa60d7SChristoph Hellwig {
291d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
292d3fa60d7SChristoph Hellwig 
293d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
294d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
295d3fa60d7SChristoph Hellwig 		dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir);
296d3fa60d7SChristoph Hellwig 	else if (ops->sync_sg_for_cpu)
297d3fa60d7SChristoph Hellwig 		ops->sync_sg_for_cpu(dev, sg, nelems, dir);
298d3fa60d7SChristoph Hellwig 	debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
299d3fa60d7SChristoph Hellwig }
300d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_sg_for_cpu);
301d3fa60d7SChristoph Hellwig 
302d3fa60d7SChristoph Hellwig void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
303d3fa60d7SChristoph Hellwig 		       int nelems, enum dma_data_direction dir)
304d3fa60d7SChristoph Hellwig {
305d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
306d3fa60d7SChristoph Hellwig 
307d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
308d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
309d3fa60d7SChristoph Hellwig 		dma_direct_sync_sg_for_device(dev, sg, nelems, dir);
310d3fa60d7SChristoph Hellwig 	else if (ops->sync_sg_for_device)
311d3fa60d7SChristoph Hellwig 		ops->sync_sg_for_device(dev, sg, nelems, dir);
312d3fa60d7SChristoph Hellwig 	debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
313d3fa60d7SChristoph Hellwig }
314d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_sg_for_device);
315d3fa60d7SChristoph Hellwig 
316cf65a0f6SChristoph Hellwig /*
31714451467SChristoph Hellwig  * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
31814451467SChristoph Hellwig  * that the intention is to allow exporting memory allocated via the
31914451467SChristoph Hellwig  * coherent DMA APIs through the dma_buf API, which only accepts a
32014451467SChristoph Hellwig  * scattertable.  This presents a couple of problems:
32114451467SChristoph Hellwig  * 1. Not all memory allocated via the coherent DMA APIs is backed by
32214451467SChristoph Hellwig  *    a struct page
32314451467SChristoph Hellwig  * 2. Passing coherent DMA memory into the streaming APIs is not allowed
32414451467SChristoph Hellwig  *    as we will try to flush the memory through a different alias to that
32514451467SChristoph Hellwig  *    actually being used (and the flushes are redundant.)
32614451467SChristoph Hellwig  */
3277249c1a5SChristoph Hellwig int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
3287249c1a5SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
3297249c1a5SChristoph Hellwig 		unsigned long attrs)
3307249c1a5SChristoph Hellwig {
3317249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
332356da6d0SChristoph Hellwig 
333d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
33434dc0ea6SChristoph Hellwig 		return dma_direct_get_sgtable(dev, sgt, cpu_addr, dma_addr,
335f9f3232aSChristoph Hellwig 				size, attrs);
336f9f3232aSChristoph Hellwig 	if (!ops->get_sgtable)
337f9f3232aSChristoph Hellwig 		return -ENXIO;
338f9f3232aSChristoph Hellwig 	return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs);
3397249c1a5SChristoph Hellwig }
3407249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_get_sgtable_attrs);
341cf65a0f6SChristoph Hellwig 
34233dcb37cSChristoph Hellwig #ifdef CONFIG_MMU
34333dcb37cSChristoph Hellwig /*
34433dcb37cSChristoph Hellwig  * Return the page attributes used for mapping dma_alloc_* memory, either in
34533dcb37cSChristoph Hellwig  * kernel space if remapping is needed, or to userspace through dma_mmap_*.
34633dcb37cSChristoph Hellwig  */
34733dcb37cSChristoph Hellwig pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs)
34833dcb37cSChristoph Hellwig {
34917c4a2aeSThomas Hellstrom 	if (force_dma_unencrypted(dev))
35017c4a2aeSThomas Hellstrom 		prot = pgprot_decrypted(prot);
351efa70f2fSChristoph Hellwig 	if (dev_is_dma_coherent(dev))
35233dcb37cSChristoph Hellwig 		return prot;
353419e2f18SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE
354419e2f18SChristoph Hellwig 	if (attrs & DMA_ATTR_WRITE_COMBINE)
355419e2f18SChristoph Hellwig 		return pgprot_writecombine(prot);
356419e2f18SChristoph Hellwig #endif
357419e2f18SChristoph Hellwig 	return pgprot_dmacoherent(prot);
35833dcb37cSChristoph Hellwig }
35933dcb37cSChristoph Hellwig #endif /* CONFIG_MMU */
36033dcb37cSChristoph Hellwig 
3617249c1a5SChristoph Hellwig /**
362e29ccc18SChristoph Hellwig  * dma_can_mmap - check if a given device supports dma_mmap_*
363e29ccc18SChristoph Hellwig  * @dev: device to check
364e29ccc18SChristoph Hellwig  *
365e29ccc18SChristoph Hellwig  * Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to
366e29ccc18SChristoph Hellwig  * map DMA allocations to userspace.
367e29ccc18SChristoph Hellwig  */
368e29ccc18SChristoph Hellwig bool dma_can_mmap(struct device *dev)
369e29ccc18SChristoph Hellwig {
370e29ccc18SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
371e29ccc18SChristoph Hellwig 
372d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
37334dc0ea6SChristoph Hellwig 		return dma_direct_can_mmap(dev);
374e29ccc18SChristoph Hellwig 	return ops->mmap != NULL;
375e29ccc18SChristoph Hellwig }
376e29ccc18SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_can_mmap);
377e29ccc18SChristoph Hellwig 
378e29ccc18SChristoph Hellwig /**
3797249c1a5SChristoph Hellwig  * dma_mmap_attrs - map a coherent DMA allocation into user space
3807249c1a5SChristoph Hellwig  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
3817249c1a5SChristoph Hellwig  * @vma: vm_area_struct describing requested user mapping
3827249c1a5SChristoph Hellwig  * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
3837249c1a5SChristoph Hellwig  * @dma_addr: device-view address returned from dma_alloc_attrs
3847249c1a5SChristoph Hellwig  * @size: size of memory originally requested in dma_alloc_attrs
3857249c1a5SChristoph Hellwig  * @attrs: attributes of mapping properties requested in dma_alloc_attrs
3867249c1a5SChristoph Hellwig  *
3877249c1a5SChristoph Hellwig  * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user
3887249c1a5SChristoph Hellwig  * space.  The coherent DMA buffer must not be freed by the driver until the
3897249c1a5SChristoph Hellwig  * user space mapping has been released.
3907249c1a5SChristoph Hellwig  */
3917249c1a5SChristoph Hellwig int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
3927249c1a5SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
3937249c1a5SChristoph Hellwig 		unsigned long attrs)
3947249c1a5SChristoph Hellwig {
3957249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
396356da6d0SChristoph Hellwig 
397d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
39834dc0ea6SChristoph Hellwig 		return dma_direct_mmap(dev, vma, cpu_addr, dma_addr, size,
399f9f3232aSChristoph Hellwig 				attrs);
400f9f3232aSChristoph Hellwig 	if (!ops->mmap)
401f9f3232aSChristoph Hellwig 		return -ENXIO;
4027249c1a5SChristoph Hellwig 	return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
4037249c1a5SChristoph Hellwig }
4047249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_mmap_attrs);
40505887cb6SChristoph Hellwig 
40605887cb6SChristoph Hellwig u64 dma_get_required_mask(struct device *dev)
40705887cb6SChristoph Hellwig {
40805887cb6SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
40905887cb6SChristoph Hellwig 
410d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
411356da6d0SChristoph Hellwig 		return dma_direct_get_required_mask(dev);
41205887cb6SChristoph Hellwig 	if (ops->get_required_mask)
41305887cb6SChristoph Hellwig 		return ops->get_required_mask(dev);
414249baa54SChristoph Hellwig 
415249baa54SChristoph Hellwig 	/*
416249baa54SChristoph Hellwig 	 * We require every DMA ops implementation to at least support a 32-bit
417249baa54SChristoph Hellwig 	 * DMA mask (and use bounce buffering if that isn't supported in
418249baa54SChristoph Hellwig 	 * hardware).  As the direct mapping code has its own routine to
419249baa54SChristoph Hellwig 	 * actually report an optimal mask we default to 32-bit here as that
420249baa54SChristoph Hellwig 	 * is the right thing for most IOMMUs, and at least not actively
421249baa54SChristoph Hellwig 	 * harmful in general.
422249baa54SChristoph Hellwig 	 */
423249baa54SChristoph Hellwig 	return DMA_BIT_MASK(32);
42405887cb6SChristoph Hellwig }
42505887cb6SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_get_required_mask);
42605887cb6SChristoph Hellwig 
4277249c1a5SChristoph Hellwig void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
4287249c1a5SChristoph Hellwig 		gfp_t flag, unsigned long attrs)
4297249c1a5SChristoph Hellwig {
4307249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
4317249c1a5SChristoph Hellwig 	void *cpu_addr;
4327249c1a5SChristoph Hellwig 
433148a97d5SDan Carpenter 	WARN_ON_ONCE(!dev->coherent_dma_mask);
4347249c1a5SChristoph Hellwig 
4357249c1a5SChristoph Hellwig 	if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
4367249c1a5SChristoph Hellwig 		return cpu_addr;
4377249c1a5SChristoph Hellwig 
4387249c1a5SChristoph Hellwig 	/* let the implementation decide on the zone to allocate from: */
4397249c1a5SChristoph Hellwig 	flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
4407249c1a5SChristoph Hellwig 
441d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
442356da6d0SChristoph Hellwig 		cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
443356da6d0SChristoph Hellwig 	else if (ops->alloc)
444356da6d0SChristoph Hellwig 		cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
445356da6d0SChristoph Hellwig 	else
4467249c1a5SChristoph Hellwig 		return NULL;
4477249c1a5SChristoph Hellwig 
4487249c1a5SChristoph Hellwig 	debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
4497249c1a5SChristoph Hellwig 	return cpu_addr;
4507249c1a5SChristoph Hellwig }
4517249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_alloc_attrs);
4527249c1a5SChristoph Hellwig 
4537249c1a5SChristoph Hellwig void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
4547249c1a5SChristoph Hellwig 		dma_addr_t dma_handle, unsigned long attrs)
4557249c1a5SChristoph Hellwig {
4567249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
4577249c1a5SChristoph Hellwig 
4587249c1a5SChristoph Hellwig 	if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
4597249c1a5SChristoph Hellwig 		return;
4607249c1a5SChristoph Hellwig 	/*
4617249c1a5SChristoph Hellwig 	 * On non-coherent platforms which implement DMA-coherent buffers via
4627249c1a5SChristoph Hellwig 	 * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
4637249c1a5SChristoph Hellwig 	 * this far in IRQ context is a) at risk of a BUG_ON() or trying to
4647249c1a5SChristoph Hellwig 	 * sleep on some machines, and b) an indication that the driver is
4657249c1a5SChristoph Hellwig 	 * probably misusing the coherent API anyway.
4667249c1a5SChristoph Hellwig 	 */
4677249c1a5SChristoph Hellwig 	WARN_ON(irqs_disabled());
4687249c1a5SChristoph Hellwig 
469356da6d0SChristoph Hellwig 	if (!cpu_addr)
4707249c1a5SChristoph Hellwig 		return;
4717249c1a5SChristoph Hellwig 
4727249c1a5SChristoph Hellwig 	debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
473d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
474356da6d0SChristoph Hellwig 		dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
475356da6d0SChristoph Hellwig 	else if (ops->free)
4767249c1a5SChristoph Hellwig 		ops->free(dev, size, cpu_addr, dma_handle, attrs);
4777249c1a5SChristoph Hellwig }
4787249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_free_attrs);
4797249c1a5SChristoph Hellwig 
480*198c50e2SChristoph Hellwig static struct page *__dma_alloc_pages(struct device *dev, size_t size,
481efa70f2fSChristoph Hellwig 		dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
482efa70f2fSChristoph Hellwig {
483efa70f2fSChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
484efa70f2fSChristoph Hellwig 
485efa70f2fSChristoph Hellwig 	if (WARN_ON_ONCE(!dev->coherent_dma_mask))
486efa70f2fSChristoph Hellwig 		return NULL;
487efa70f2fSChristoph Hellwig 	if (WARN_ON_ONCE(gfp & (__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM)))
488efa70f2fSChristoph Hellwig 		return NULL;
489efa70f2fSChristoph Hellwig 
490efa70f2fSChristoph Hellwig 	size = PAGE_ALIGN(size);
491efa70f2fSChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
492*198c50e2SChristoph Hellwig 		return dma_direct_alloc_pages(dev, size, dma_handle, dir, gfp);
493*198c50e2SChristoph Hellwig 	if (!ops->alloc_pages)
494efa70f2fSChristoph Hellwig 		return NULL;
495*198c50e2SChristoph Hellwig 	return ops->alloc_pages(dev, size, dma_handle, dir, gfp);
496*198c50e2SChristoph Hellwig }
497efa70f2fSChristoph Hellwig 
498*198c50e2SChristoph Hellwig struct page *dma_alloc_pages(struct device *dev, size_t size,
499*198c50e2SChristoph Hellwig 		dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
500*198c50e2SChristoph Hellwig {
501*198c50e2SChristoph Hellwig 	struct page *page = __dma_alloc_pages(dev, size, dma_handle, dir, gfp);
502*198c50e2SChristoph Hellwig 
503*198c50e2SChristoph Hellwig 	if (page)
504efa70f2fSChristoph Hellwig 		debug_dma_map_page(dev, page, 0, size, dir, *dma_handle);
505efa70f2fSChristoph Hellwig 	return page;
506efa70f2fSChristoph Hellwig }
507efa70f2fSChristoph Hellwig EXPORT_SYMBOL_GPL(dma_alloc_pages);
508efa70f2fSChristoph Hellwig 
509*198c50e2SChristoph Hellwig static void __dma_free_pages(struct device *dev, size_t size, struct page *page,
510efa70f2fSChristoph Hellwig 		dma_addr_t dma_handle, enum dma_data_direction dir)
511efa70f2fSChristoph Hellwig {
512efa70f2fSChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
513efa70f2fSChristoph Hellwig 
514efa70f2fSChristoph Hellwig 	size = PAGE_ALIGN(size);
515efa70f2fSChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
516efa70f2fSChristoph Hellwig 		dma_direct_free_pages(dev, size, page, dma_handle, dir);
517efa70f2fSChristoph Hellwig 	else if (ops->free_pages)
518efa70f2fSChristoph Hellwig 		ops->free_pages(dev, size, page, dma_handle, dir);
519efa70f2fSChristoph Hellwig }
520*198c50e2SChristoph Hellwig 
521*198c50e2SChristoph Hellwig void dma_free_pages(struct device *dev, size_t size, struct page *page,
522*198c50e2SChristoph Hellwig 		dma_addr_t dma_handle, enum dma_data_direction dir)
523*198c50e2SChristoph Hellwig {
524*198c50e2SChristoph Hellwig 	debug_dma_unmap_page(dev, dma_handle, size, dir);
525*198c50e2SChristoph Hellwig 	__dma_free_pages(dev, size, page, dma_handle, dir);
526*198c50e2SChristoph Hellwig }
527efa70f2fSChristoph Hellwig EXPORT_SYMBOL_GPL(dma_free_pages);
528efa70f2fSChristoph Hellwig 
529eedb0b12SChristoph Hellwig int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma,
530eedb0b12SChristoph Hellwig 		size_t size, struct page *page)
531eedb0b12SChristoph Hellwig {
532eedb0b12SChristoph Hellwig 	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
533eedb0b12SChristoph Hellwig 
534eedb0b12SChristoph Hellwig 	if (vma->vm_pgoff >= count || vma_pages(vma) > count - vma->vm_pgoff)
535eedb0b12SChristoph Hellwig 		return -ENXIO;
536eedb0b12SChristoph Hellwig 	return remap_pfn_range(vma, vma->vm_start,
537eedb0b12SChristoph Hellwig 			       page_to_pfn(page) + vma->vm_pgoff,
538eedb0b12SChristoph Hellwig 			       vma_pages(vma) << PAGE_SHIFT, vma->vm_page_prot);
539eedb0b12SChristoph Hellwig }
540eedb0b12SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_mmap_pages);
541eedb0b12SChristoph Hellwig 
5427249c1a5SChristoph Hellwig int dma_supported(struct device *dev, u64 mask)
5437249c1a5SChristoph Hellwig {
5447249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
5457249c1a5SChristoph Hellwig 
546d35834c6SChristoph Hellwig 	/*
547d35834c6SChristoph Hellwig 	 * ->dma_supported sets the bypass flag, so we must always call
548d35834c6SChristoph Hellwig 	 * into the method here unless the device is truly direct mapped.
549d35834c6SChristoph Hellwig 	 */
550d35834c6SChristoph Hellwig 	if (!ops)
551356da6d0SChristoph Hellwig 		return dma_direct_supported(dev, mask);
5528b1cce9fSThierry Reding 	if (!ops->dma_supported)
5537249c1a5SChristoph Hellwig 		return 1;
5547249c1a5SChristoph Hellwig 	return ops->dma_supported(dev, mask);
5557249c1a5SChristoph Hellwig }
5567249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_supported);
5577249c1a5SChristoph Hellwig 
55811ddce15SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_DMA_SET_MASK
55911ddce15SChristoph Hellwig void arch_dma_set_mask(struct device *dev, u64 mask);
56011ddce15SChristoph Hellwig #else
56111ddce15SChristoph Hellwig #define arch_dma_set_mask(dev, mask)	do { } while (0)
56211ddce15SChristoph Hellwig #endif
56311ddce15SChristoph Hellwig 
5647249c1a5SChristoph Hellwig int dma_set_mask(struct device *dev, u64 mask)
5657249c1a5SChristoph Hellwig {
5664a54d16fSChristoph Hellwig 	/*
5674a54d16fSChristoph Hellwig 	 * Truncate the mask to the actually supported dma_addr_t width to
5684a54d16fSChristoph Hellwig 	 * avoid generating unsupportable addresses.
5694a54d16fSChristoph Hellwig 	 */
5704a54d16fSChristoph Hellwig 	mask = (dma_addr_t)mask;
5714a54d16fSChristoph Hellwig 
5727249c1a5SChristoph Hellwig 	if (!dev->dma_mask || !dma_supported(dev, mask))
5737249c1a5SChristoph Hellwig 		return -EIO;
5747249c1a5SChristoph Hellwig 
57511ddce15SChristoph Hellwig 	arch_dma_set_mask(dev, mask);
5767249c1a5SChristoph Hellwig 	*dev->dma_mask = mask;
5777249c1a5SChristoph Hellwig 	return 0;
5787249c1a5SChristoph Hellwig }
5797249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_set_mask);
5807249c1a5SChristoph Hellwig 
5817249c1a5SChristoph Hellwig #ifndef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
5827249c1a5SChristoph Hellwig int dma_set_coherent_mask(struct device *dev, u64 mask)
5837249c1a5SChristoph Hellwig {
5844a54d16fSChristoph Hellwig 	/*
5854a54d16fSChristoph Hellwig 	 * Truncate the mask to the actually supported dma_addr_t width to
5864a54d16fSChristoph Hellwig 	 * avoid generating unsupportable addresses.
5874a54d16fSChristoph Hellwig 	 */
5884a54d16fSChristoph Hellwig 	mask = (dma_addr_t)mask;
5894a54d16fSChristoph Hellwig 
5907249c1a5SChristoph Hellwig 	if (!dma_supported(dev, mask))
5917249c1a5SChristoph Hellwig 		return -EIO;
5927249c1a5SChristoph Hellwig 
5937249c1a5SChristoph Hellwig 	dev->coherent_dma_mask = mask;
5947249c1a5SChristoph Hellwig 	return 0;
5957249c1a5SChristoph Hellwig }
5967249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_set_coherent_mask);
5977249c1a5SChristoph Hellwig #endif
5988ddbe594SChristoph Hellwig 
599133d624bSJoerg Roedel size_t dma_max_mapping_size(struct device *dev)
600133d624bSJoerg Roedel {
601133d624bSJoerg Roedel 	const struct dma_map_ops *ops = get_dma_ops(dev);
602133d624bSJoerg Roedel 	size_t size = SIZE_MAX;
603133d624bSJoerg Roedel 
604d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
605133d624bSJoerg Roedel 		size = dma_direct_max_mapping_size(dev);
606133d624bSJoerg Roedel 	else if (ops && ops->max_mapping_size)
607133d624bSJoerg Roedel 		size = ops->max_mapping_size(dev);
608133d624bSJoerg Roedel 
609133d624bSJoerg Roedel 	return size;
610133d624bSJoerg Roedel }
611133d624bSJoerg Roedel EXPORT_SYMBOL_GPL(dma_max_mapping_size);
6126ba99411SYoshihiro Shimoda 
6133aa91625SChristoph Hellwig bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
6143aa91625SChristoph Hellwig {
6153aa91625SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
6163aa91625SChristoph Hellwig 
617d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
6183aa91625SChristoph Hellwig 		return dma_direct_need_sync(dev, dma_addr);
6193aa91625SChristoph Hellwig 	return ops->sync_single_for_cpu || ops->sync_single_for_device;
6203aa91625SChristoph Hellwig }
6213aa91625SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_need_sync);
6223aa91625SChristoph Hellwig 
6236ba99411SYoshihiro Shimoda unsigned long dma_get_merge_boundary(struct device *dev)
6246ba99411SYoshihiro Shimoda {
6256ba99411SYoshihiro Shimoda 	const struct dma_map_ops *ops = get_dma_ops(dev);
6266ba99411SYoshihiro Shimoda 
6276ba99411SYoshihiro Shimoda 	if (!ops || !ops->get_merge_boundary)
6286ba99411SYoshihiro Shimoda 		return 0;	/* can't merge */
6296ba99411SYoshihiro Shimoda 
6306ba99411SYoshihiro Shimoda 	return ops->get_merge_boundary(dev);
6316ba99411SYoshihiro Shimoda }
6326ba99411SYoshihiro Shimoda EXPORT_SYMBOL_GPL(dma_get_merge_boundary);
633