blob: f87a89d086544b863b9635c07334dfc8530dd3d4 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Tejun Heo9ac78492007-01-20 16:00:26 +09002/*
Christoph Hellwigcf65a0f2018-06-12 19:01:45 +02003 * arch-independent dma-mapping routines
Tejun Heo9ac78492007-01-20 16:00:26 +09004 *
5 * Copyright (c) 2006 SUSE Linux Products GmbH
6 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
Tejun Heo9ac78492007-01-20 16:00:26 +09007 */
Christoph Hellwig05887cb2018-12-06 12:25:54 -08008#include <linux/memblock.h> /* for max_pfn */
Sricharan R09515ef2017-04-10 16:51:01 +05309#include <linux/acpi.h>
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020010#include <linux/dma-map-ops.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040011#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Sricharan R09515ef2017-04-10 16:51:01 +053013#include <linux/of_device.h>
Laura Abbott513510d2014-10-09 15:26:40 -070014#include <linux/slab.h>
15#include <linux/vmalloc.h>
Christoph Hellwiga1fd09e2020-09-11 10:12:44 +020016#include "debug.h"
Christoph Hellwig19c65c32020-09-22 15:34:22 +020017#include "direct.h"
Tejun Heo9ac78492007-01-20 16:00:26 +090018
19/*
20 * Managed DMA API
21 */
22struct dma_devres {
23 size_t size;
24 void *vaddr;
25 dma_addr_t dma_handle;
Christoph Hellwig63d36c92017-06-12 19:15:04 +020026 unsigned long attrs;
Tejun Heo9ac78492007-01-20 16:00:26 +090027};
28
Christoph Hellwig63d36c92017-06-12 19:15:04 +020029static void dmam_release(struct device *dev, void *res)
Tejun Heo9ac78492007-01-20 16:00:26 +090030{
31 struct dma_devres *this = res;
32
Christoph Hellwig63d36c92017-06-12 19:15:04 +020033 dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
34 this->attrs);
Tejun Heo9ac78492007-01-20 16:00:26 +090035}
36
37static int dmam_match(struct device *dev, void *res, void *match_data)
38{
39 struct dma_devres *this = res, *match = match_data;
40
41 if (this->vaddr == match->vaddr) {
42 WARN_ON(this->size != match->size ||
43 this->dma_handle != match->dma_handle);
44 return 1;
45 }
46 return 0;
47}
48
49/**
Tejun Heo9ac78492007-01-20 16:00:26 +090050 * dmam_free_coherent - Managed dma_free_coherent()
51 * @dev: Device to free coherent memory for
52 * @size: Size of allocation
53 * @vaddr: Virtual address of the memory to free
54 * @dma_handle: DMA handle of the memory to free
55 *
56 * Managed dma_free_coherent().
57 */
58void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
59 dma_addr_t dma_handle)
60{
61 struct dma_devres match_data = { size, vaddr, dma_handle };
62
63 dma_free_coherent(dev, size, vaddr, dma_handle);
Christoph Hellwig63d36c92017-06-12 19:15:04 +020064 WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
Tejun Heo9ac78492007-01-20 16:00:26 +090065}
66EXPORT_SYMBOL(dmam_free_coherent);
67
68/**
Christoph Hellwig63d36c92017-06-12 19:15:04 +020069 * dmam_alloc_attrs - Managed dma_alloc_attrs()
Tejun Heo9ac78492007-01-20 16:00:26 +090070 * @dev: Device to allocate non_coherent memory for
71 * @size: Size of allocation
72 * @dma_handle: Out argument for allocated DMA handle
73 * @gfp: Allocation flags
Christoph Hellwig63d36c92017-06-12 19:15:04 +020074 * @attrs: Flags in the DMA_ATTR_* namespace.
Tejun Heo9ac78492007-01-20 16:00:26 +090075 *
Christoph Hellwig63d36c92017-06-12 19:15:04 +020076 * Managed dma_alloc_attrs(). Memory allocated using this function will be
77 * automatically released on driver detach.
Tejun Heo9ac78492007-01-20 16:00:26 +090078 *
79 * RETURNS:
80 * Pointer to allocated memory on success, NULL on failure.
81 */
Christoph Hellwig63d36c92017-06-12 19:15:04 +020082void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
83 gfp_t gfp, unsigned long attrs)
Tejun Heo9ac78492007-01-20 16:00:26 +090084{
85 struct dma_devres *dr;
86 void *vaddr;
87
Christoph Hellwig63d36c92017-06-12 19:15:04 +020088 dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
Tejun Heo9ac78492007-01-20 16:00:26 +090089 if (!dr)
90 return NULL;
91
Christoph Hellwig63d36c92017-06-12 19:15:04 +020092 vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
Tejun Heo9ac78492007-01-20 16:00:26 +090093 if (!vaddr) {
94 devres_free(dr);
95 return NULL;
96 }
97
98 dr->vaddr = vaddr;
99 dr->dma_handle = *dma_handle;
100 dr->size = size;
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200101 dr->attrs = attrs;
Tejun Heo9ac78492007-01-20 16:00:26 +0900102
103 devres_add(dev, dr);
104
105 return vaddr;
106}
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200107EXPORT_SYMBOL(dmam_alloc_attrs);
Tejun Heo9ac78492007-01-20 16:00:26 +0900108
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100109static bool dma_go_direct(struct device *dev, dma_addr_t mask,
110 const struct dma_map_ops *ops)
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200111{
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100112 if (likely(!ops))
113 return true;
114#ifdef CONFIG_DMA_OPS_BYPASS
115 if (dev->dma_ops_bypass)
116 return min_not_zero(mask, dev->bus_dma_limit) >=
117 dma_direct_get_required_mask(dev);
118#endif
119 return false;
120}
121
122
123/*
124 * Check if the devices uses a direct mapping for streaming DMA operations.
125 * This allows IOMMU drivers to set a bypass mode if the DMA mask is large
126 * enough.
127 */
128static inline bool dma_alloc_direct(struct device *dev,
129 const struct dma_map_ops *ops)
130{
131 return dma_go_direct(dev, dev->coherent_dma_mask, ops);
132}
133
134static inline bool dma_map_direct(struct device *dev,
135 const struct dma_map_ops *ops)
136{
137 return dma_go_direct(dev, *dev->dma_mask, ops);
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200138}
139
140dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
141 size_t offset, size_t size, enum dma_data_direction dir,
142 unsigned long attrs)
143{
144 const struct dma_map_ops *ops = get_dma_ops(dev);
145 dma_addr_t addr;
146
147 BUG_ON(!valid_dma_direction(dir));
Thomas Taif959dcd2020-09-17 18:43:03 +0200148
149 if (WARN_ON_ONCE(!dev->dma_mask))
150 return DMA_MAPPING_ERROR;
151
Alexey Kardashevskiy8d8d53c2020-10-29 12:52:40 +1100152 if (dma_map_direct(dev, ops) ||
153 arch_dma_map_page_direct(dev, page_to_phys(page) + offset + size))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200154 addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
155 else
156 addr = ops->map_page(dev, page, offset, size, dir, attrs);
157 debug_dma_map_page(dev, page, offset, size, dir, addr);
158
159 return addr;
160}
161EXPORT_SYMBOL(dma_map_page_attrs);
162
163void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
164 enum dma_data_direction dir, unsigned long attrs)
165{
166 const struct dma_map_ops *ops = get_dma_ops(dev);
167
168 BUG_ON(!valid_dma_direction(dir));
Alexey Kardashevskiy8d8d53c2020-10-29 12:52:40 +1100169 if (dma_map_direct(dev, ops) ||
170 arch_dma_unmap_page_direct(dev, addr + size))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200171 dma_direct_unmap_page(dev, addr, size, dir, attrs);
172 else if (ops->unmap_page)
173 ops->unmap_page(dev, addr, size, dir, attrs);
174 debug_dma_unmap_page(dev, addr, size, dir);
175}
176EXPORT_SYMBOL(dma_unmap_page_attrs);
177
178/*
179 * dma_maps_sg_attrs returns 0 on error and > 0 on success.
180 * It should never return a value < 0.
181 */
182int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
183 enum dma_data_direction dir, unsigned long attrs)
184{
185 const struct dma_map_ops *ops = get_dma_ops(dev);
186 int ents;
187
188 BUG_ON(!valid_dma_direction(dir));
Thomas Taif959dcd2020-09-17 18:43:03 +0200189
190 if (WARN_ON_ONCE(!dev->dma_mask))
191 return 0;
192
Alexey Kardashevskiy8d8d53c2020-10-29 12:52:40 +1100193 if (dma_map_direct(dev, ops) ||
194 arch_dma_map_sg_direct(dev, sg, nents))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200195 ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
196 else
197 ents = ops->map_sg(dev, sg, nents, dir, attrs);
198 BUG_ON(ents < 0);
199 debug_dma_map_sg(dev, sg, nents, ents, dir);
200
201 return ents;
202}
203EXPORT_SYMBOL(dma_map_sg_attrs);
204
205void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
206 int nents, enum dma_data_direction dir,
207 unsigned long attrs)
208{
209 const struct dma_map_ops *ops = get_dma_ops(dev);
210
211 BUG_ON(!valid_dma_direction(dir));
212 debug_dma_unmap_sg(dev, sg, nents, dir);
Alexey Kardashevskiy8d8d53c2020-10-29 12:52:40 +1100213 if (dma_map_direct(dev, ops) ||
214 arch_dma_unmap_sg_direct(dev, sg, nents))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200215 dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
216 else if (ops->unmap_sg)
217 ops->unmap_sg(dev, sg, nents, dir, attrs);
218}
219EXPORT_SYMBOL(dma_unmap_sg_attrs);
220
221dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
222 size_t size, enum dma_data_direction dir, unsigned long attrs)
223{
224 const struct dma_map_ops *ops = get_dma_ops(dev);
225 dma_addr_t addr = DMA_MAPPING_ERROR;
226
227 BUG_ON(!valid_dma_direction(dir));
228
Thomas Taif959dcd2020-09-17 18:43:03 +0200229 if (WARN_ON_ONCE(!dev->dma_mask))
230 return DMA_MAPPING_ERROR;
231
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200232 /* Don't allow RAM to be mapped */
233 if (WARN_ON_ONCE(pfn_valid(PHYS_PFN(phys_addr))))
234 return DMA_MAPPING_ERROR;
235
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100236 if (dma_map_direct(dev, ops))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200237 addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
238 else if (ops->map_resource)
239 addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
240
241 debug_dma_map_resource(dev, phys_addr, size, dir, addr);
242 return addr;
243}
244EXPORT_SYMBOL(dma_map_resource);
245
246void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
247 enum dma_data_direction dir, unsigned long attrs)
248{
249 const struct dma_map_ops *ops = get_dma_ops(dev);
250
251 BUG_ON(!valid_dma_direction(dir));
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100252 if (!dma_map_direct(dev, ops) && ops->unmap_resource)
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200253 ops->unmap_resource(dev, addr, size, dir, attrs);
254 debug_dma_unmap_resource(dev, addr, size, dir);
255}
256EXPORT_SYMBOL(dma_unmap_resource);
257
258void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
259 enum dma_data_direction dir)
260{
261 const struct dma_map_ops *ops = get_dma_ops(dev);
262
263 BUG_ON(!valid_dma_direction(dir));
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100264 if (dma_map_direct(dev, ops))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200265 dma_direct_sync_single_for_cpu(dev, addr, size, dir);
266 else if (ops->sync_single_for_cpu)
267 ops->sync_single_for_cpu(dev, addr, size, dir);
268 debug_dma_sync_single_for_cpu(dev, addr, size, dir);
269}
270EXPORT_SYMBOL(dma_sync_single_for_cpu);
271
272void dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
273 size_t size, enum dma_data_direction dir)
274{
275 const struct dma_map_ops *ops = get_dma_ops(dev);
276
277 BUG_ON(!valid_dma_direction(dir));
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100278 if (dma_map_direct(dev, ops))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200279 dma_direct_sync_single_for_device(dev, addr, size, dir);
280 else if (ops->sync_single_for_device)
281 ops->sync_single_for_device(dev, addr, size, dir);
282 debug_dma_sync_single_for_device(dev, addr, size, dir);
283}
284EXPORT_SYMBOL(dma_sync_single_for_device);
285
286void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
287 int nelems, enum dma_data_direction dir)
288{
289 const struct dma_map_ops *ops = get_dma_ops(dev);
290
291 BUG_ON(!valid_dma_direction(dir));
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100292 if (dma_map_direct(dev, ops))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200293 dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir);
294 else if (ops->sync_sg_for_cpu)
295 ops->sync_sg_for_cpu(dev, sg, nelems, dir);
296 debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
297}
298EXPORT_SYMBOL(dma_sync_sg_for_cpu);
299
300void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
301 int nelems, enum dma_data_direction dir)
302{
303 const struct dma_map_ops *ops = get_dma_ops(dev);
304
305 BUG_ON(!valid_dma_direction(dir));
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100306 if (dma_map_direct(dev, ops))
Christoph Hellwigd3fa60d2020-07-08 09:45:11 +0200307 dma_direct_sync_sg_for_device(dev, sg, nelems, dir);
308 else if (ops->sync_sg_for_device)
309 ops->sync_sg_for_device(dev, sg, nelems, dir);
310 debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
311}
312EXPORT_SYMBOL(dma_sync_sg_for_device);
313
Marek Szyprowskid2b74282012-06-13 10:05:52 +0200314/*
Christoph Hellwig14451462019-03-15 17:56:43 +0100315 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
316 * that the intention is to allow exporting memory allocated via the
317 * coherent DMA APIs through the dma_buf API, which only accepts a
318 * scattertable. This presents a couple of problems:
319 * 1. Not all memory allocated via the coherent DMA APIs is backed by
320 * a struct page
321 * 2. Passing coherent DMA memory into the streaming APIs is not allowed
322 * as we will try to flush the memory through a different alias to that
323 * actually being used (and the flushes are redundant.)
324 */
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800325int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
326 void *cpu_addr, dma_addr_t dma_addr, size_t size,
327 unsigned long attrs)
328{
329 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwig356da6d2018-12-06 13:39:32 -0800330
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100331 if (dma_alloc_direct(dev, ops))
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100332 return dma_direct_get_sgtable(dev, sgt, cpu_addr, dma_addr,
Christoph Hellwigf9f32322019-08-06 15:01:50 +0300333 size, attrs);
334 if (!ops->get_sgtable)
335 return -ENXIO;
336 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs);
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800337}
338EXPORT_SYMBOL(dma_get_sgtable_attrs);
Marek Szyprowskid2b74282012-06-13 10:05:52 +0200339
Christoph Hellwig33dcb372019-07-26 09:26:40 +0200340#ifdef CONFIG_MMU
341/*
342 * Return the page attributes used for mapping dma_alloc_* memory, either in
343 * kernel space if remapping is needed, or to userspace through dma_mmap_*.
344 */
345pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs)
346{
Thomas Hellstrom17c4a2a2020-03-04 12:45:27 +0100347 if (force_dma_unencrypted(dev))
348 prot = pgprot_decrypted(prot);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200349 if (dev_is_dma_coherent(dev))
Christoph Hellwig33dcb372019-07-26 09:26:40 +0200350 return prot;
Christoph Hellwig419e2f12019-08-26 09:03:44 +0200351#ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE
352 if (attrs & DMA_ATTR_WRITE_COMBINE)
353 return pgprot_writecombine(prot);
354#endif
355 return pgprot_dmacoherent(prot);
Christoph Hellwig33dcb372019-07-26 09:26:40 +0200356}
357#endif /* CONFIG_MMU */
358
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800359/**
Christoph Hellwige29ccc12019-08-03 13:31:25 +0300360 * dma_can_mmap - check if a given device supports dma_mmap_*
361 * @dev: device to check
362 *
363 * Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to
364 * map DMA allocations to userspace.
365 */
366bool dma_can_mmap(struct device *dev)
367{
368 const struct dma_map_ops *ops = get_dma_ops(dev);
369
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100370 if (dma_alloc_direct(dev, ops))
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100371 return dma_direct_can_mmap(dev);
Christoph Hellwige29ccc12019-08-03 13:31:25 +0300372 return ops->mmap != NULL;
373}
374EXPORT_SYMBOL_GPL(dma_can_mmap);
375
376/**
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800377 * dma_mmap_attrs - map a coherent DMA allocation into user space
378 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
379 * @vma: vm_area_struct describing requested user mapping
380 * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
381 * @dma_addr: device-view address returned from dma_alloc_attrs
382 * @size: size of memory originally requested in dma_alloc_attrs
383 * @attrs: attributes of mapping properties requested in dma_alloc_attrs
384 *
385 * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user
386 * space. The coherent DMA buffer must not be freed by the driver until the
387 * user space mapping has been released.
388 */
389int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
390 void *cpu_addr, dma_addr_t dma_addr, size_t size,
391 unsigned long attrs)
392{
393 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwig356da6d2018-12-06 13:39:32 -0800394
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100395 if (dma_alloc_direct(dev, ops))
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100396 return dma_direct_mmap(dev, vma, cpu_addr, dma_addr, size,
Christoph Hellwigf9f32322019-08-06 15:01:50 +0300397 attrs);
398 if (!ops->mmap)
399 return -ENXIO;
400 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800401}
402EXPORT_SYMBOL(dma_mmap_attrs);
Christoph Hellwig05887cb2018-12-06 12:25:54 -0800403
Christoph Hellwig05887cb2018-12-06 12:25:54 -0800404u64 dma_get_required_mask(struct device *dev)
405{
406 const struct dma_map_ops *ops = get_dma_ops(dev);
407
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100408 if (dma_alloc_direct(dev, ops))
Christoph Hellwig356da6d2018-12-06 13:39:32 -0800409 return dma_direct_get_required_mask(dev);
Christoph Hellwig05887cb2018-12-06 12:25:54 -0800410 if (ops->get_required_mask)
411 return ops->get_required_mask(dev);
Christoph Hellwig249baa52019-08-06 15:01:38 +0300412
413 /*
414 * We require every DMA ops implementation to at least support a 32-bit
415 * DMA mask (and use bounce buffering if that isn't supported in
416 * hardware). As the direct mapping code has its own routine to
417 * actually report an optimal mask we default to 32-bit here as that
418 * is the right thing for most IOMMUs, and at least not actively
419 * harmful in general.
420 */
421 return DMA_BIT_MASK(32);
Christoph Hellwig05887cb2018-12-06 12:25:54 -0800422}
423EXPORT_SYMBOL_GPL(dma_get_required_mask);
Christoph Hellwig05887cb2018-12-06 12:25:54 -0800424
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800425void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
426 gfp_t flag, unsigned long attrs)
427{
428 const struct dma_map_ops *ops = get_dma_ops(dev);
429 void *cpu_addr;
430
Dan Carpenter148a97d2019-04-24 17:24:37 +0300431 WARN_ON_ONCE(!dev->coherent_dma_mask);
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800432
433 if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
434 return cpu_addr;
435
436 /* let the implementation decide on the zone to allocate from: */
437 flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
438
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100439 if (dma_alloc_direct(dev, ops))
Christoph Hellwig356da6d2018-12-06 13:39:32 -0800440 cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
441 else if (ops->alloc)
442 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
443 else
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800444 return NULL;
445
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800446 debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
447 return cpu_addr;
448}
449EXPORT_SYMBOL(dma_alloc_attrs);
450
451void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
452 dma_addr_t dma_handle, unsigned long attrs)
453{
454 const struct dma_map_ops *ops = get_dma_ops(dev);
455
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800456 if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
457 return;
458 /*
459 * On non-coherent platforms which implement DMA-coherent buffers via
460 * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
461 * this far in IRQ context is a) at risk of a BUG_ON() or trying to
462 * sleep on some machines, and b) an indication that the driver is
463 * probably misusing the coherent API anyway.
464 */
465 WARN_ON(irqs_disabled());
466
Christoph Hellwig356da6d2018-12-06 13:39:32 -0800467 if (!cpu_addr)
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800468 return;
469
470 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100471 if (dma_alloc_direct(dev, ops))
Christoph Hellwig356da6d2018-12-06 13:39:32 -0800472 dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
473 else if (ops->free)
474 ops->free(dev, size, cpu_addr, dma_handle, attrs);
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800475}
476EXPORT_SYMBOL(dma_free_attrs);
477
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200478struct page *dma_alloc_pages(struct device *dev, size_t size,
479 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
480{
481 const struct dma_map_ops *ops = get_dma_ops(dev);
482 struct page *page;
483
484 if (WARN_ON_ONCE(!dev->coherent_dma_mask))
485 return NULL;
486 if (WARN_ON_ONCE(gfp & (__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM)))
487 return NULL;
488
489 size = PAGE_ALIGN(size);
490 if (dma_alloc_direct(dev, ops))
491 page = dma_direct_alloc_pages(dev, size, dma_handle, dir, gfp);
492 else if (ops->alloc_pages)
493 page = ops->alloc_pages(dev, size, dma_handle, dir, gfp);
494 else
495 return NULL;
496
497 debug_dma_map_page(dev, page, 0, size, dir, *dma_handle);
498
499 return page;
500}
501EXPORT_SYMBOL_GPL(dma_alloc_pages);
502
503void dma_free_pages(struct device *dev, size_t size, struct page *page,
504 dma_addr_t dma_handle, enum dma_data_direction dir)
505{
506 const struct dma_map_ops *ops = get_dma_ops(dev);
507
508 size = PAGE_ALIGN(size);
509 debug_dma_unmap_page(dev, dma_handle, size, dir);
510
511 if (dma_alloc_direct(dev, ops))
512 dma_direct_free_pages(dev, size, page, dma_handle, dir);
513 else if (ops->free_pages)
514 ops->free_pages(dev, size, page, dma_handle, dir);
515}
516EXPORT_SYMBOL_GPL(dma_free_pages);
517
518void *dma_alloc_noncoherent(struct device *dev, size_t size,
519 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
520{
Christoph Hellwigde7cf912020-09-01 13:59:45 +0200521 const struct dma_map_ops *ops = get_dma_ops(dev);
522 void *vaddr;
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200523
Christoph Hellwigde7cf912020-09-01 13:59:45 +0200524 if (!ops || !ops->alloc_noncoherent) {
525 struct page *page;
526
527 page = dma_alloc_pages(dev, size, dma_handle, dir, gfp);
528 if (!page)
529 return NULL;
530 return page_address(page);
531 }
532
533 size = PAGE_ALIGN(size);
534 vaddr = ops->alloc_noncoherent(dev, size, dma_handle, dir, gfp);
535 if (vaddr)
536 debug_dma_map_page(dev, virt_to_page(vaddr), 0, size, dir,
537 *dma_handle);
538 return vaddr;
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200539}
540EXPORT_SYMBOL_GPL(dma_alloc_noncoherent);
541
542void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
543 dma_addr_t dma_handle, enum dma_data_direction dir)
544{
Christoph Hellwigde7cf912020-09-01 13:59:45 +0200545 const struct dma_map_ops *ops = get_dma_ops(dev);
546
547 if (!ops || !ops->free_noncoherent) {
548 dma_free_pages(dev, size, virt_to_page(vaddr), dma_handle, dir);
549 return;
550 }
551
552 size = PAGE_ALIGN(size);
553 debug_dma_unmap_page(dev, dma_handle, size, dir);
554 ops->free_noncoherent(dev, size, vaddr, dma_handle, dir);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200555}
556EXPORT_SYMBOL_GPL(dma_free_noncoherent);
557
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800558int dma_supported(struct device *dev, u64 mask)
559{
560 const struct dma_map_ops *ops = get_dma_ops(dev);
561
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100562 /*
563 * ->dma_supported sets the bypass flag, so we must always call
564 * into the method here unless the device is truly direct mapped.
565 */
566 if (!ops)
Christoph Hellwig356da6d2018-12-06 13:39:32 -0800567 return dma_direct_supported(dev, mask);
Thierry Reding8b1cce92018-12-20 17:35:47 +0100568 if (!ops->dma_supported)
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800569 return 1;
570 return ops->dma_supported(dev, mask);
571}
572EXPORT_SYMBOL(dma_supported);
573
Christoph Hellwig11ddce12019-02-13 08:01:22 +0100574#ifdef CONFIG_ARCH_HAS_DMA_SET_MASK
575void arch_dma_set_mask(struct device *dev, u64 mask);
576#else
577#define arch_dma_set_mask(dev, mask) do { } while (0)
578#endif
579
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800580int dma_set_mask(struct device *dev, u64 mask)
581{
Christoph Hellwig4a54d162019-04-29 09:16:42 -0500582 /*
583 * Truncate the mask to the actually supported dma_addr_t width to
584 * avoid generating unsupportable addresses.
585 */
586 mask = (dma_addr_t)mask;
587
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800588 if (!dev->dma_mask || !dma_supported(dev, mask))
589 return -EIO;
590
Christoph Hellwig11ddce12019-02-13 08:01:22 +0100591 arch_dma_set_mask(dev, mask);
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800592 *dev->dma_mask = mask;
593 return 0;
594}
595EXPORT_SYMBOL(dma_set_mask);
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800596
597#ifndef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
598int dma_set_coherent_mask(struct device *dev, u64 mask)
599{
Christoph Hellwig4a54d162019-04-29 09:16:42 -0500600 /*
601 * Truncate the mask to the actually supported dma_addr_t width to
602 * avoid generating unsupportable addresses.
603 */
604 mask = (dma_addr_t)mask;
605
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800606 if (!dma_supported(dev, mask))
607 return -EIO;
608
Christoph Hellwig7249c1a2018-12-06 12:43:30 -0800609 dev->coherent_dma_mask = mask;
610 return 0;
611}
612EXPORT_SYMBOL(dma_set_coherent_mask);
613#endif
Christoph Hellwig8ddbe592018-12-06 12:47:50 -0800614
Joerg Roedel133d6242019-02-07 12:59:15 +0100615size_t dma_max_mapping_size(struct device *dev)
616{
617 const struct dma_map_ops *ops = get_dma_ops(dev);
618 size_t size = SIZE_MAX;
619
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100620 if (dma_map_direct(dev, ops))
Joerg Roedel133d6242019-02-07 12:59:15 +0100621 size = dma_direct_max_mapping_size(dev);
622 else if (ops && ops->max_mapping_size)
623 size = ops->max_mapping_size(dev);
624
625 return size;
626}
627EXPORT_SYMBOL_GPL(dma_max_mapping_size);
Yoshihiro Shimoda6ba99412019-08-28 21:35:40 +0900628
Christoph Hellwig3aa9162502020-06-29 15:03:56 +0200629bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
630{
631 const struct dma_map_ops *ops = get_dma_ops(dev);
632
Christoph Hellwigd35834c2020-03-23 18:19:30 +0100633 if (dma_map_direct(dev, ops))
Christoph Hellwig3aa9162502020-06-29 15:03:56 +0200634 return dma_direct_need_sync(dev, dma_addr);
635 return ops->sync_single_for_cpu || ops->sync_single_for_device;
636}
637EXPORT_SYMBOL_GPL(dma_need_sync);
638
Yoshihiro Shimoda6ba99412019-08-28 21:35:40 +0900639unsigned long dma_get_merge_boundary(struct device *dev)
640{
641 const struct dma_map_ops *ops = get_dma_ops(dev);
642
643 if (!ops || !ops->get_merge_boundary)
644 return 0; /* can't merge */
645
646 return ops->get_merge_boundary(dev);
647}
648EXPORT_SYMBOL_GPL(dma_get_merge_boundary);