xref: /openbmc/linux/kernel/dma/mapping.c (revision f959dcd6ddfd29235030e8026471ac1b022ad2b0)
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>
10356da6d0SChristoph Hellwig #include <linux/dma-direct.h>
11545d2927SChristoph Hellwig #include <linux/dma-mapping.h>
12cf65a0f6SChristoph Hellwig #include <linux/export.h>
13cf65a0f6SChristoph Hellwig #include <linux/gfp.h>
14cf65a0f6SChristoph Hellwig #include <linux/of_device.h>
15cf65a0f6SChristoph Hellwig #include <linux/slab.h>
16cf65a0f6SChristoph Hellwig #include <linux/vmalloc.h>
17cf65a0f6SChristoph Hellwig 
18cf65a0f6SChristoph Hellwig /*
19cf65a0f6SChristoph Hellwig  * Managed DMA API
20cf65a0f6SChristoph Hellwig  */
21cf65a0f6SChristoph Hellwig struct dma_devres {
22cf65a0f6SChristoph Hellwig 	size_t		size;
23cf65a0f6SChristoph Hellwig 	void		*vaddr;
24cf65a0f6SChristoph Hellwig 	dma_addr_t	dma_handle;
25cf65a0f6SChristoph Hellwig 	unsigned long	attrs;
26cf65a0f6SChristoph Hellwig };
27cf65a0f6SChristoph Hellwig 
28cf65a0f6SChristoph Hellwig static void dmam_release(struct device *dev, void *res)
29cf65a0f6SChristoph Hellwig {
30cf65a0f6SChristoph Hellwig 	struct dma_devres *this = res;
31cf65a0f6SChristoph Hellwig 
32cf65a0f6SChristoph Hellwig 	dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
33cf65a0f6SChristoph Hellwig 			this->attrs);
34cf65a0f6SChristoph Hellwig }
35cf65a0f6SChristoph Hellwig 
36cf65a0f6SChristoph Hellwig static int dmam_match(struct device *dev, void *res, void *match_data)
37cf65a0f6SChristoph Hellwig {
38cf65a0f6SChristoph Hellwig 	struct dma_devres *this = res, *match = match_data;
39cf65a0f6SChristoph Hellwig 
40cf65a0f6SChristoph Hellwig 	if (this->vaddr == match->vaddr) {
41cf65a0f6SChristoph Hellwig 		WARN_ON(this->size != match->size ||
42cf65a0f6SChristoph Hellwig 			this->dma_handle != match->dma_handle);
43cf65a0f6SChristoph Hellwig 		return 1;
44cf65a0f6SChristoph Hellwig 	}
45cf65a0f6SChristoph Hellwig 	return 0;
46cf65a0f6SChristoph Hellwig }
47cf65a0f6SChristoph Hellwig 
48cf65a0f6SChristoph Hellwig /**
49cf65a0f6SChristoph Hellwig  * dmam_free_coherent - Managed dma_free_coherent()
50cf65a0f6SChristoph Hellwig  * @dev: Device to free coherent memory for
51cf65a0f6SChristoph Hellwig  * @size: Size of allocation
52cf65a0f6SChristoph Hellwig  * @vaddr: Virtual address of the memory to free
53cf65a0f6SChristoph Hellwig  * @dma_handle: DMA handle of the memory to free
54cf65a0f6SChristoph Hellwig  *
55cf65a0f6SChristoph Hellwig  * Managed dma_free_coherent().
56cf65a0f6SChristoph Hellwig  */
57cf65a0f6SChristoph Hellwig void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
58cf65a0f6SChristoph Hellwig 			dma_addr_t dma_handle)
59cf65a0f6SChristoph Hellwig {
60cf65a0f6SChristoph Hellwig 	struct dma_devres match_data = { size, vaddr, dma_handle };
61cf65a0f6SChristoph Hellwig 
62cf65a0f6SChristoph Hellwig 	dma_free_coherent(dev, size, vaddr, dma_handle);
63cf65a0f6SChristoph Hellwig 	WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
64cf65a0f6SChristoph Hellwig }
65cf65a0f6SChristoph Hellwig EXPORT_SYMBOL(dmam_free_coherent);
66cf65a0f6SChristoph Hellwig 
67cf65a0f6SChristoph Hellwig /**
68cf65a0f6SChristoph Hellwig  * dmam_alloc_attrs - Managed dma_alloc_attrs()
69cf65a0f6SChristoph Hellwig  * @dev: Device to allocate non_coherent memory for
70cf65a0f6SChristoph Hellwig  * @size: Size of allocation
71cf65a0f6SChristoph Hellwig  * @dma_handle: Out argument for allocated DMA handle
72cf65a0f6SChristoph Hellwig  * @gfp: Allocation flags
73cf65a0f6SChristoph Hellwig  * @attrs: Flags in the DMA_ATTR_* namespace.
74cf65a0f6SChristoph Hellwig  *
75cf65a0f6SChristoph Hellwig  * Managed dma_alloc_attrs().  Memory allocated using this function will be
76cf65a0f6SChristoph Hellwig  * automatically released on driver detach.
77cf65a0f6SChristoph Hellwig  *
78cf65a0f6SChristoph Hellwig  * RETURNS:
79cf65a0f6SChristoph Hellwig  * Pointer to allocated memory on success, NULL on failure.
80cf65a0f6SChristoph Hellwig  */
81cf65a0f6SChristoph Hellwig void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
82cf65a0f6SChristoph Hellwig 		gfp_t gfp, unsigned long attrs)
83cf65a0f6SChristoph Hellwig {
84cf65a0f6SChristoph Hellwig 	struct dma_devres *dr;
85cf65a0f6SChristoph Hellwig 	void *vaddr;
86cf65a0f6SChristoph Hellwig 
87cf65a0f6SChristoph Hellwig 	dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
88cf65a0f6SChristoph Hellwig 	if (!dr)
89cf65a0f6SChristoph Hellwig 		return NULL;
90cf65a0f6SChristoph Hellwig 
91cf65a0f6SChristoph Hellwig 	vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
92cf65a0f6SChristoph Hellwig 	if (!vaddr) {
93cf65a0f6SChristoph Hellwig 		devres_free(dr);
94cf65a0f6SChristoph Hellwig 		return NULL;
95cf65a0f6SChristoph Hellwig 	}
96cf65a0f6SChristoph Hellwig 
97cf65a0f6SChristoph Hellwig 	dr->vaddr = vaddr;
98cf65a0f6SChristoph Hellwig 	dr->dma_handle = *dma_handle;
99cf65a0f6SChristoph Hellwig 	dr->size = size;
100cf65a0f6SChristoph Hellwig 	dr->attrs = attrs;
101cf65a0f6SChristoph Hellwig 
102cf65a0f6SChristoph Hellwig 	devres_add(dev, dr);
103cf65a0f6SChristoph Hellwig 
104cf65a0f6SChristoph Hellwig 	return vaddr;
105cf65a0f6SChristoph Hellwig }
106cf65a0f6SChristoph Hellwig EXPORT_SYMBOL(dmam_alloc_attrs);
107cf65a0f6SChristoph Hellwig 
108d35834c6SChristoph Hellwig static bool dma_go_direct(struct device *dev, dma_addr_t mask,
109d35834c6SChristoph Hellwig 		const struct dma_map_ops *ops)
110d3fa60d7SChristoph Hellwig {
111d35834c6SChristoph Hellwig 	if (likely(!ops))
112d35834c6SChristoph Hellwig 		return true;
113d35834c6SChristoph Hellwig #ifdef CONFIG_DMA_OPS_BYPASS
114d35834c6SChristoph Hellwig 	if (dev->dma_ops_bypass)
115d35834c6SChristoph Hellwig 		return min_not_zero(mask, dev->bus_dma_limit) >=
116d35834c6SChristoph Hellwig 			    dma_direct_get_required_mask(dev);
117d35834c6SChristoph Hellwig #endif
118d35834c6SChristoph Hellwig 	return false;
119d35834c6SChristoph Hellwig }
120d35834c6SChristoph Hellwig 
121d35834c6SChristoph Hellwig 
122d35834c6SChristoph Hellwig /*
123d35834c6SChristoph Hellwig  * Check if the devices uses a direct mapping for streaming DMA operations.
124d35834c6SChristoph Hellwig  * This allows IOMMU drivers to set a bypass mode if the DMA mask is large
125d35834c6SChristoph Hellwig  * enough.
126d35834c6SChristoph Hellwig  */
127d35834c6SChristoph Hellwig static inline bool dma_alloc_direct(struct device *dev,
128d35834c6SChristoph Hellwig 		const struct dma_map_ops *ops)
129d35834c6SChristoph Hellwig {
130d35834c6SChristoph Hellwig 	return dma_go_direct(dev, dev->coherent_dma_mask, ops);
131d35834c6SChristoph Hellwig }
132d35834c6SChristoph Hellwig 
133d35834c6SChristoph Hellwig static inline bool dma_map_direct(struct device *dev,
134d35834c6SChristoph Hellwig 		const struct dma_map_ops *ops)
135d35834c6SChristoph Hellwig {
136d35834c6SChristoph Hellwig 	return dma_go_direct(dev, *dev->dma_mask, ops);
137d3fa60d7SChristoph Hellwig }
138d3fa60d7SChristoph Hellwig 
139d3fa60d7SChristoph Hellwig dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
140d3fa60d7SChristoph Hellwig 		size_t offset, size_t size, enum dma_data_direction dir,
141d3fa60d7SChristoph Hellwig 		unsigned long attrs)
142d3fa60d7SChristoph Hellwig {
143d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
144d3fa60d7SChristoph Hellwig 	dma_addr_t addr;
145d3fa60d7SChristoph Hellwig 
146d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
147*f959dcd6SThomas Tai 
148*f959dcd6SThomas Tai 	if (WARN_ON_ONCE(!dev->dma_mask))
149*f959dcd6SThomas Tai 		return DMA_MAPPING_ERROR;
150*f959dcd6SThomas Tai 
151d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
152d3fa60d7SChristoph Hellwig 		addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
153d3fa60d7SChristoph Hellwig 	else
154d3fa60d7SChristoph Hellwig 		addr = ops->map_page(dev, page, offset, size, dir, attrs);
155d3fa60d7SChristoph Hellwig 	debug_dma_map_page(dev, page, offset, size, dir, addr);
156d3fa60d7SChristoph Hellwig 
157d3fa60d7SChristoph Hellwig 	return addr;
158d3fa60d7SChristoph Hellwig }
159d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_map_page_attrs);
160d3fa60d7SChristoph Hellwig 
161d3fa60d7SChristoph Hellwig void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
162d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
163d3fa60d7SChristoph Hellwig {
164d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
165d3fa60d7SChristoph Hellwig 
166d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
167d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
168d3fa60d7SChristoph Hellwig 		dma_direct_unmap_page(dev, addr, size, dir, attrs);
169d3fa60d7SChristoph Hellwig 	else if (ops->unmap_page)
170d3fa60d7SChristoph Hellwig 		ops->unmap_page(dev, addr, size, dir, attrs);
171d3fa60d7SChristoph Hellwig 	debug_dma_unmap_page(dev, addr, size, dir);
172d3fa60d7SChristoph Hellwig }
173d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_unmap_page_attrs);
174d3fa60d7SChristoph Hellwig 
175d3fa60d7SChristoph Hellwig /*
176d3fa60d7SChristoph Hellwig  * dma_maps_sg_attrs returns 0 on error and > 0 on success.
177d3fa60d7SChristoph Hellwig  * It should never return a value < 0.
178d3fa60d7SChristoph Hellwig  */
179d3fa60d7SChristoph Hellwig int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
180d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
181d3fa60d7SChristoph Hellwig {
182d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
183d3fa60d7SChristoph Hellwig 	int ents;
184d3fa60d7SChristoph Hellwig 
185d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
186*f959dcd6SThomas Tai 
187*f959dcd6SThomas Tai 	if (WARN_ON_ONCE(!dev->dma_mask))
188*f959dcd6SThomas Tai 		return 0;
189*f959dcd6SThomas Tai 
190d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
191d3fa60d7SChristoph Hellwig 		ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
192d3fa60d7SChristoph Hellwig 	else
193d3fa60d7SChristoph Hellwig 		ents = ops->map_sg(dev, sg, nents, dir, attrs);
194d3fa60d7SChristoph Hellwig 	BUG_ON(ents < 0);
195d3fa60d7SChristoph Hellwig 	debug_dma_map_sg(dev, sg, nents, ents, dir);
196d3fa60d7SChristoph Hellwig 
197d3fa60d7SChristoph Hellwig 	return ents;
198d3fa60d7SChristoph Hellwig }
199d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_map_sg_attrs);
200d3fa60d7SChristoph Hellwig 
201d3fa60d7SChristoph Hellwig void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
202d3fa60d7SChristoph Hellwig 				      int nents, enum dma_data_direction dir,
203d3fa60d7SChristoph Hellwig 				      unsigned long attrs)
204d3fa60d7SChristoph Hellwig {
205d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
206d3fa60d7SChristoph Hellwig 
207d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
208d3fa60d7SChristoph Hellwig 	debug_dma_unmap_sg(dev, sg, nents, dir);
209d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
210d3fa60d7SChristoph Hellwig 		dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
211d3fa60d7SChristoph Hellwig 	else if (ops->unmap_sg)
212d3fa60d7SChristoph Hellwig 		ops->unmap_sg(dev, sg, nents, dir, attrs);
213d3fa60d7SChristoph Hellwig }
214d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_unmap_sg_attrs);
215d3fa60d7SChristoph Hellwig 
216d3fa60d7SChristoph Hellwig dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
217d3fa60d7SChristoph Hellwig 		size_t size, enum dma_data_direction dir, unsigned long attrs)
218d3fa60d7SChristoph Hellwig {
219d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
220d3fa60d7SChristoph Hellwig 	dma_addr_t addr = DMA_MAPPING_ERROR;
221d3fa60d7SChristoph Hellwig 
222d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
223d3fa60d7SChristoph Hellwig 
224*f959dcd6SThomas Tai 	if (WARN_ON_ONCE(!dev->dma_mask))
225*f959dcd6SThomas Tai 		return DMA_MAPPING_ERROR;
226*f959dcd6SThomas Tai 
227d3fa60d7SChristoph Hellwig 	/* Don't allow RAM to be mapped */
228d3fa60d7SChristoph Hellwig 	if (WARN_ON_ONCE(pfn_valid(PHYS_PFN(phys_addr))))
229d3fa60d7SChristoph Hellwig 		return DMA_MAPPING_ERROR;
230d3fa60d7SChristoph Hellwig 
231d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
232d3fa60d7SChristoph Hellwig 		addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
233d3fa60d7SChristoph Hellwig 	else if (ops->map_resource)
234d3fa60d7SChristoph Hellwig 		addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
235d3fa60d7SChristoph Hellwig 
236d3fa60d7SChristoph Hellwig 	debug_dma_map_resource(dev, phys_addr, size, dir, addr);
237d3fa60d7SChristoph Hellwig 	return addr;
238d3fa60d7SChristoph Hellwig }
239d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_map_resource);
240d3fa60d7SChristoph Hellwig 
241d3fa60d7SChristoph Hellwig void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
242d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
243d3fa60d7SChristoph Hellwig {
244d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
245d3fa60d7SChristoph Hellwig 
246d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
247d35834c6SChristoph Hellwig 	if (!dma_map_direct(dev, ops) && ops->unmap_resource)
248d3fa60d7SChristoph Hellwig 		ops->unmap_resource(dev, addr, size, dir, attrs);
249d3fa60d7SChristoph Hellwig 	debug_dma_unmap_resource(dev, addr, size, dir);
250d3fa60d7SChristoph Hellwig }
251d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_unmap_resource);
252d3fa60d7SChristoph Hellwig 
253d3fa60d7SChristoph Hellwig void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
254d3fa60d7SChristoph Hellwig 		enum dma_data_direction dir)
255d3fa60d7SChristoph Hellwig {
256d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
257d3fa60d7SChristoph Hellwig 
258d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
259d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
260d3fa60d7SChristoph Hellwig 		dma_direct_sync_single_for_cpu(dev, addr, size, dir);
261d3fa60d7SChristoph Hellwig 	else if (ops->sync_single_for_cpu)
262d3fa60d7SChristoph Hellwig 		ops->sync_single_for_cpu(dev, addr, size, dir);
263d3fa60d7SChristoph Hellwig 	debug_dma_sync_single_for_cpu(dev, addr, size, dir);
264d3fa60d7SChristoph Hellwig }
265d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_single_for_cpu);
266d3fa60d7SChristoph Hellwig 
267d3fa60d7SChristoph Hellwig void dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
268d3fa60d7SChristoph Hellwig 		size_t size, enum dma_data_direction dir)
269d3fa60d7SChristoph Hellwig {
270d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
271d3fa60d7SChristoph Hellwig 
272d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
273d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
274d3fa60d7SChristoph Hellwig 		dma_direct_sync_single_for_device(dev, addr, size, dir);
275d3fa60d7SChristoph Hellwig 	else if (ops->sync_single_for_device)
276d3fa60d7SChristoph Hellwig 		ops->sync_single_for_device(dev, addr, size, dir);
277d3fa60d7SChristoph Hellwig 	debug_dma_sync_single_for_device(dev, addr, size, dir);
278d3fa60d7SChristoph Hellwig }
279d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_single_for_device);
280d3fa60d7SChristoph Hellwig 
281d3fa60d7SChristoph Hellwig void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
282d3fa60d7SChristoph Hellwig 		    int nelems, enum dma_data_direction dir)
283d3fa60d7SChristoph Hellwig {
284d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
285d3fa60d7SChristoph Hellwig 
286d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
287d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
288d3fa60d7SChristoph Hellwig 		dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir);
289d3fa60d7SChristoph Hellwig 	else if (ops->sync_sg_for_cpu)
290d3fa60d7SChristoph Hellwig 		ops->sync_sg_for_cpu(dev, sg, nelems, dir);
291d3fa60d7SChristoph Hellwig 	debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
292d3fa60d7SChristoph Hellwig }
293d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_sg_for_cpu);
294d3fa60d7SChristoph Hellwig 
295d3fa60d7SChristoph Hellwig void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
296d3fa60d7SChristoph Hellwig 		       int nelems, enum dma_data_direction dir)
297d3fa60d7SChristoph Hellwig {
298d3fa60d7SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
299d3fa60d7SChristoph Hellwig 
300d3fa60d7SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
301d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
302d3fa60d7SChristoph Hellwig 		dma_direct_sync_sg_for_device(dev, sg, nelems, dir);
303d3fa60d7SChristoph Hellwig 	else if (ops->sync_sg_for_device)
304d3fa60d7SChristoph Hellwig 		ops->sync_sg_for_device(dev, sg, nelems, dir);
305d3fa60d7SChristoph Hellwig 	debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
306d3fa60d7SChristoph Hellwig }
307d3fa60d7SChristoph Hellwig EXPORT_SYMBOL(dma_sync_sg_for_device);
308d3fa60d7SChristoph Hellwig 
309cf65a0f6SChristoph Hellwig /*
31014451467SChristoph Hellwig  * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
31114451467SChristoph Hellwig  * that the intention is to allow exporting memory allocated via the
31214451467SChristoph Hellwig  * coherent DMA APIs through the dma_buf API, which only accepts a
31314451467SChristoph Hellwig  * scattertable.  This presents a couple of problems:
31414451467SChristoph Hellwig  * 1. Not all memory allocated via the coherent DMA APIs is backed by
31514451467SChristoph Hellwig  *    a struct page
31614451467SChristoph Hellwig  * 2. Passing coherent DMA memory into the streaming APIs is not allowed
31714451467SChristoph Hellwig  *    as we will try to flush the memory through a different alias to that
31814451467SChristoph Hellwig  *    actually being used (and the flushes are redundant.)
31914451467SChristoph Hellwig  */
3207249c1a5SChristoph Hellwig int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
3217249c1a5SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
3227249c1a5SChristoph Hellwig 		unsigned long attrs)
3237249c1a5SChristoph Hellwig {
3247249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
325356da6d0SChristoph Hellwig 
326d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
32734dc0ea6SChristoph Hellwig 		return dma_direct_get_sgtable(dev, sgt, cpu_addr, dma_addr,
328f9f3232aSChristoph Hellwig 				size, attrs);
329f9f3232aSChristoph Hellwig 	if (!ops->get_sgtable)
330f9f3232aSChristoph Hellwig 		return -ENXIO;
331f9f3232aSChristoph Hellwig 	return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs);
3327249c1a5SChristoph Hellwig }
3337249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_get_sgtable_attrs);
334cf65a0f6SChristoph Hellwig 
33533dcb37cSChristoph Hellwig #ifdef CONFIG_MMU
33633dcb37cSChristoph Hellwig /*
33733dcb37cSChristoph Hellwig  * Return the page attributes used for mapping dma_alloc_* memory, either in
33833dcb37cSChristoph Hellwig  * kernel space if remapping is needed, or to userspace through dma_mmap_*.
33933dcb37cSChristoph Hellwig  */
34033dcb37cSChristoph Hellwig pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs)
34133dcb37cSChristoph Hellwig {
34217c4a2aeSThomas Hellstrom 	if (force_dma_unencrypted(dev))
34317c4a2aeSThomas Hellstrom 		prot = pgprot_decrypted(prot);
34433dcb37cSChristoph Hellwig 	if (dev_is_dma_coherent(dev) ||
34533dcb37cSChristoph Hellwig 	    (IS_ENABLED(CONFIG_DMA_NONCOHERENT_CACHE_SYNC) &&
34633dcb37cSChristoph Hellwig              (attrs & DMA_ATTR_NON_CONSISTENT)))
34733dcb37cSChristoph Hellwig 		return prot;
348419e2f18SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE
349419e2f18SChristoph Hellwig 	if (attrs & DMA_ATTR_WRITE_COMBINE)
350419e2f18SChristoph Hellwig 		return pgprot_writecombine(prot);
351419e2f18SChristoph Hellwig #endif
352419e2f18SChristoph Hellwig 	return pgprot_dmacoherent(prot);
35333dcb37cSChristoph Hellwig }
35433dcb37cSChristoph Hellwig #endif /* CONFIG_MMU */
35533dcb37cSChristoph Hellwig 
3567249c1a5SChristoph Hellwig /**
357e29ccc18SChristoph Hellwig  * dma_can_mmap - check if a given device supports dma_mmap_*
358e29ccc18SChristoph Hellwig  * @dev: device to check
359e29ccc18SChristoph Hellwig  *
360e29ccc18SChristoph Hellwig  * Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to
361e29ccc18SChristoph Hellwig  * map DMA allocations to userspace.
362e29ccc18SChristoph Hellwig  */
363e29ccc18SChristoph Hellwig bool dma_can_mmap(struct device *dev)
364e29ccc18SChristoph Hellwig {
365e29ccc18SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
366e29ccc18SChristoph Hellwig 
367d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
36834dc0ea6SChristoph Hellwig 		return dma_direct_can_mmap(dev);
369e29ccc18SChristoph Hellwig 	return ops->mmap != NULL;
370e29ccc18SChristoph Hellwig }
371e29ccc18SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_can_mmap);
372e29ccc18SChristoph Hellwig 
373e29ccc18SChristoph Hellwig /**
3747249c1a5SChristoph Hellwig  * dma_mmap_attrs - map a coherent DMA allocation into user space
3757249c1a5SChristoph Hellwig  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
3767249c1a5SChristoph Hellwig  * @vma: vm_area_struct describing requested user mapping
3777249c1a5SChristoph Hellwig  * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
3787249c1a5SChristoph Hellwig  * @dma_addr: device-view address returned from dma_alloc_attrs
3797249c1a5SChristoph Hellwig  * @size: size of memory originally requested in dma_alloc_attrs
3807249c1a5SChristoph Hellwig  * @attrs: attributes of mapping properties requested in dma_alloc_attrs
3817249c1a5SChristoph Hellwig  *
3827249c1a5SChristoph Hellwig  * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user
3837249c1a5SChristoph Hellwig  * space.  The coherent DMA buffer must not be freed by the driver until the
3847249c1a5SChristoph Hellwig  * user space mapping has been released.
3857249c1a5SChristoph Hellwig  */
3867249c1a5SChristoph Hellwig int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
3877249c1a5SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
3887249c1a5SChristoph Hellwig 		unsigned long attrs)
3897249c1a5SChristoph Hellwig {
3907249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
391356da6d0SChristoph Hellwig 
392d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
39334dc0ea6SChristoph Hellwig 		return dma_direct_mmap(dev, vma, cpu_addr, dma_addr, size,
394f9f3232aSChristoph Hellwig 				attrs);
395f9f3232aSChristoph Hellwig 	if (!ops->mmap)
396f9f3232aSChristoph Hellwig 		return -ENXIO;
3977249c1a5SChristoph Hellwig 	return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
3987249c1a5SChristoph Hellwig }
3997249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_mmap_attrs);
40005887cb6SChristoph Hellwig 
40105887cb6SChristoph Hellwig u64 dma_get_required_mask(struct device *dev)
40205887cb6SChristoph Hellwig {
40305887cb6SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
40405887cb6SChristoph Hellwig 
405d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
406356da6d0SChristoph Hellwig 		return dma_direct_get_required_mask(dev);
40705887cb6SChristoph Hellwig 	if (ops->get_required_mask)
40805887cb6SChristoph Hellwig 		return ops->get_required_mask(dev);
409249baa54SChristoph Hellwig 
410249baa54SChristoph Hellwig 	/*
411249baa54SChristoph Hellwig 	 * We require every DMA ops implementation to at least support a 32-bit
412249baa54SChristoph Hellwig 	 * DMA mask (and use bounce buffering if that isn't supported in
413249baa54SChristoph Hellwig 	 * hardware).  As the direct mapping code has its own routine to
414249baa54SChristoph Hellwig 	 * actually report an optimal mask we default to 32-bit here as that
415249baa54SChristoph Hellwig 	 * is the right thing for most IOMMUs, and at least not actively
416249baa54SChristoph Hellwig 	 * harmful in general.
417249baa54SChristoph Hellwig 	 */
418249baa54SChristoph Hellwig 	return DMA_BIT_MASK(32);
41905887cb6SChristoph Hellwig }
42005887cb6SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_get_required_mask);
42105887cb6SChristoph Hellwig 
4227249c1a5SChristoph Hellwig void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
4237249c1a5SChristoph Hellwig 		gfp_t flag, unsigned long attrs)
4247249c1a5SChristoph Hellwig {
4257249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
4267249c1a5SChristoph Hellwig 	void *cpu_addr;
4277249c1a5SChristoph Hellwig 
428148a97d5SDan Carpenter 	WARN_ON_ONCE(!dev->coherent_dma_mask);
4297249c1a5SChristoph Hellwig 
4307249c1a5SChristoph Hellwig 	if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
4317249c1a5SChristoph Hellwig 		return cpu_addr;
4327249c1a5SChristoph Hellwig 
4337249c1a5SChristoph Hellwig 	/* let the implementation decide on the zone to allocate from: */
4347249c1a5SChristoph Hellwig 	flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
4357249c1a5SChristoph Hellwig 
436d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
437356da6d0SChristoph Hellwig 		cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
438356da6d0SChristoph Hellwig 	else if (ops->alloc)
439356da6d0SChristoph Hellwig 		cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
440356da6d0SChristoph Hellwig 	else
4417249c1a5SChristoph Hellwig 		return NULL;
4427249c1a5SChristoph Hellwig 
4437249c1a5SChristoph Hellwig 	debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
4447249c1a5SChristoph Hellwig 	return cpu_addr;
4457249c1a5SChristoph Hellwig }
4467249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_alloc_attrs);
4477249c1a5SChristoph Hellwig 
4487249c1a5SChristoph Hellwig void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
4497249c1a5SChristoph Hellwig 		dma_addr_t dma_handle, unsigned long attrs)
4507249c1a5SChristoph Hellwig {
4517249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
4527249c1a5SChristoph Hellwig 
4537249c1a5SChristoph Hellwig 	if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
4547249c1a5SChristoph Hellwig 		return;
4557249c1a5SChristoph Hellwig 	/*
4567249c1a5SChristoph Hellwig 	 * On non-coherent platforms which implement DMA-coherent buffers via
4577249c1a5SChristoph Hellwig 	 * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
4587249c1a5SChristoph Hellwig 	 * this far in IRQ context is a) at risk of a BUG_ON() or trying to
4597249c1a5SChristoph Hellwig 	 * sleep on some machines, and b) an indication that the driver is
4607249c1a5SChristoph Hellwig 	 * probably misusing the coherent API anyway.
4617249c1a5SChristoph Hellwig 	 */
4627249c1a5SChristoph Hellwig 	WARN_ON(irqs_disabled());
4637249c1a5SChristoph Hellwig 
464356da6d0SChristoph Hellwig 	if (!cpu_addr)
4657249c1a5SChristoph Hellwig 		return;
4667249c1a5SChristoph Hellwig 
4677249c1a5SChristoph Hellwig 	debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
468d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
469356da6d0SChristoph Hellwig 		dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
470356da6d0SChristoph Hellwig 	else if (ops->free)
4717249c1a5SChristoph Hellwig 		ops->free(dev, size, cpu_addr, dma_handle, attrs);
4727249c1a5SChristoph Hellwig }
4737249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_free_attrs);
4747249c1a5SChristoph Hellwig 
4757249c1a5SChristoph Hellwig int dma_supported(struct device *dev, u64 mask)
4767249c1a5SChristoph Hellwig {
4777249c1a5SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
4787249c1a5SChristoph Hellwig 
479d35834c6SChristoph Hellwig 	/*
480d35834c6SChristoph Hellwig 	 * ->dma_supported sets the bypass flag, so we must always call
481d35834c6SChristoph Hellwig 	 * into the method here unless the device is truly direct mapped.
482d35834c6SChristoph Hellwig 	 */
483d35834c6SChristoph Hellwig 	if (!ops)
484356da6d0SChristoph Hellwig 		return dma_direct_supported(dev, mask);
4858b1cce9fSThierry Reding 	if (!ops->dma_supported)
4867249c1a5SChristoph Hellwig 		return 1;
4877249c1a5SChristoph Hellwig 	return ops->dma_supported(dev, mask);
4887249c1a5SChristoph Hellwig }
4897249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_supported);
4907249c1a5SChristoph Hellwig 
49111ddce15SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_DMA_SET_MASK
49211ddce15SChristoph Hellwig void arch_dma_set_mask(struct device *dev, u64 mask);
49311ddce15SChristoph Hellwig #else
49411ddce15SChristoph Hellwig #define arch_dma_set_mask(dev, mask)	do { } while (0)
49511ddce15SChristoph Hellwig #endif
49611ddce15SChristoph Hellwig 
4977249c1a5SChristoph Hellwig int dma_set_mask(struct device *dev, u64 mask)
4987249c1a5SChristoph Hellwig {
4994a54d16fSChristoph Hellwig 	/*
5004a54d16fSChristoph Hellwig 	 * Truncate the mask to the actually supported dma_addr_t width to
5014a54d16fSChristoph Hellwig 	 * avoid generating unsupportable addresses.
5024a54d16fSChristoph Hellwig 	 */
5034a54d16fSChristoph Hellwig 	mask = (dma_addr_t)mask;
5044a54d16fSChristoph Hellwig 
5057249c1a5SChristoph Hellwig 	if (!dev->dma_mask || !dma_supported(dev, mask))
5067249c1a5SChristoph Hellwig 		return -EIO;
5077249c1a5SChristoph Hellwig 
50811ddce15SChristoph Hellwig 	arch_dma_set_mask(dev, mask);
5097249c1a5SChristoph Hellwig 	*dev->dma_mask = mask;
5107249c1a5SChristoph Hellwig 	return 0;
5117249c1a5SChristoph Hellwig }
5127249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_set_mask);
5137249c1a5SChristoph Hellwig 
5147249c1a5SChristoph Hellwig #ifndef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
5157249c1a5SChristoph Hellwig int dma_set_coherent_mask(struct device *dev, u64 mask)
5167249c1a5SChristoph Hellwig {
5174a54d16fSChristoph Hellwig 	/*
5184a54d16fSChristoph Hellwig 	 * Truncate the mask to the actually supported dma_addr_t width to
5194a54d16fSChristoph Hellwig 	 * avoid generating unsupportable addresses.
5204a54d16fSChristoph Hellwig 	 */
5214a54d16fSChristoph Hellwig 	mask = (dma_addr_t)mask;
5224a54d16fSChristoph Hellwig 
5237249c1a5SChristoph Hellwig 	if (!dma_supported(dev, mask))
5247249c1a5SChristoph Hellwig 		return -EIO;
5257249c1a5SChristoph Hellwig 
5267249c1a5SChristoph Hellwig 	dev->coherent_dma_mask = mask;
5277249c1a5SChristoph Hellwig 	return 0;
5287249c1a5SChristoph Hellwig }
5297249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_set_coherent_mask);
5307249c1a5SChristoph Hellwig #endif
5318ddbe594SChristoph Hellwig 
5328ddbe594SChristoph Hellwig void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
5338ddbe594SChristoph Hellwig 		enum dma_data_direction dir)
5348ddbe594SChristoph Hellwig {
5358ddbe594SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
5368ddbe594SChristoph Hellwig 
5378ddbe594SChristoph Hellwig 	BUG_ON(!valid_dma_direction(dir));
538356da6d0SChristoph Hellwig 
539d35834c6SChristoph Hellwig 	if (dma_alloc_direct(dev, ops))
540356da6d0SChristoph Hellwig 		arch_dma_cache_sync(dev, vaddr, size, dir);
541356da6d0SChristoph Hellwig 	else if (ops->cache_sync)
5428ddbe594SChristoph Hellwig 		ops->cache_sync(dev, vaddr, size, dir);
5438ddbe594SChristoph Hellwig }
5448ddbe594SChristoph Hellwig EXPORT_SYMBOL(dma_cache_sync);
545133d624bSJoerg Roedel 
546133d624bSJoerg Roedel size_t dma_max_mapping_size(struct device *dev)
547133d624bSJoerg Roedel {
548133d624bSJoerg Roedel 	const struct dma_map_ops *ops = get_dma_ops(dev);
549133d624bSJoerg Roedel 	size_t size = SIZE_MAX;
550133d624bSJoerg Roedel 
551d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
552133d624bSJoerg Roedel 		size = dma_direct_max_mapping_size(dev);
553133d624bSJoerg Roedel 	else if (ops && ops->max_mapping_size)
554133d624bSJoerg Roedel 		size = ops->max_mapping_size(dev);
555133d624bSJoerg Roedel 
556133d624bSJoerg Roedel 	return size;
557133d624bSJoerg Roedel }
558133d624bSJoerg Roedel EXPORT_SYMBOL_GPL(dma_max_mapping_size);
5596ba99411SYoshihiro Shimoda 
5603aa91625SChristoph Hellwig bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
5613aa91625SChristoph Hellwig {
5623aa91625SChristoph Hellwig 	const struct dma_map_ops *ops = get_dma_ops(dev);
5633aa91625SChristoph Hellwig 
564d35834c6SChristoph Hellwig 	if (dma_map_direct(dev, ops))
5653aa91625SChristoph Hellwig 		return dma_direct_need_sync(dev, dma_addr);
5663aa91625SChristoph Hellwig 	return ops->sync_single_for_cpu || ops->sync_single_for_device;
5673aa91625SChristoph Hellwig }
5683aa91625SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_need_sync);
5693aa91625SChristoph Hellwig 
5706ba99411SYoshihiro Shimoda unsigned long dma_get_merge_boundary(struct device *dev)
5716ba99411SYoshihiro Shimoda {
5726ba99411SYoshihiro Shimoda 	const struct dma_map_ops *ops = get_dma_ops(dev);
5736ba99411SYoshihiro Shimoda 
5746ba99411SYoshihiro Shimoda 	if (!ops || !ops->get_merge_boundary)
5756ba99411SYoshihiro Shimoda 		return 0;	/* can't merge */
5766ba99411SYoshihiro Shimoda 
5776ba99411SYoshihiro Shimoda 	return ops->get_merge_boundary(dev);
5786ba99411SYoshihiro Shimoda }
5796ba99411SYoshihiro Shimoda EXPORT_SYMBOL_GPL(dma_get_merge_boundary);
580