blob: 843ab866e0f487c2000e0974592ee98544293758 [file] [log] [blame]
Robert P. J. Day96532ba2008-02-03 15:06:26 +02001#ifndef _LINUX_DMA_MAPPING_H
2#define _LINUX_DMA_MAPPING_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
Robin Murphy002edb62015-11-06 16:32:51 -08004#include <linux/sizes.h>
Andrew Morton842fa692011-11-02 13:39:33 -07005#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/device.h>
7#include <linux/err.h>
Christoph Hellwige1c7e322016-01-20 15:02:05 -08008#include <linux/dma-debug.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +00009#include <linux/dma-direction.h>
FUJITA Tomonorif0402a22009-01-05 23:59:01 +090010#include <linux/scatterlist.h>
Christoph Hellwige1c7e322016-01-20 15:02:05 -080011#include <linux/kmemcheck.h>
12#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070014/**
15 * List of possible attributes associated with a DMA mapping. The semantics
16 * of each attribute should be defined in Documentation/DMA-attributes.txt.
17 *
18 * DMA_ATTR_WRITE_BARRIER: DMA to a memory region with this attribute
19 * forces all pending DMA writes to complete.
20 */
21#define DMA_ATTR_WRITE_BARRIER (1UL << 0)
22/*
23 * DMA_ATTR_WEAK_ORDERING: Specifies that reads and writes to the mapping
24 * may be weakly ordered, that is that reads and writes may pass each other.
25 */
26#define DMA_ATTR_WEAK_ORDERING (1UL << 1)
27/*
28 * DMA_ATTR_WRITE_COMBINE: Specifies that writes to the mapping may be
29 * buffered to improve performance.
30 */
31#define DMA_ATTR_WRITE_COMBINE (1UL << 2)
32/*
33 * DMA_ATTR_NON_CONSISTENT: Lets the platform to choose to return either
34 * consistent or non-consistent memory as it sees fit.
35 */
36#define DMA_ATTR_NON_CONSISTENT (1UL << 3)
37/*
38 * DMA_ATTR_NO_KERNEL_MAPPING: Lets the platform to avoid creating a kernel
39 * virtual mapping for the allocated buffer.
40 */
41#define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4)
42/*
43 * DMA_ATTR_SKIP_CPU_SYNC: Allows platform code to skip synchronization of
44 * the CPU cache for the given buffer assuming that it has been already
45 * transferred to 'device' domain.
46 */
47#define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5)
48/*
49 * DMA_ATTR_FORCE_CONTIGUOUS: Forces contiguous allocation of the buffer
50 * in physical memory.
51 */
52#define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6)
53/*
54 * DMA_ATTR_ALLOC_SINGLE_PAGES: This is a hint to the DMA-mapping subsystem
55 * that it's probably not worth the time to try to allocate memory to in a way
56 * that gives better TLB efficiency.
57 */
58#define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7)
Mauricio Faria de Oliveiraa9a62c92016-10-11 13:54:14 -070059/*
60 * DMA_ATTR_NO_WARN: This tells the DMA-mapping subsystem to suppress
61 * allocation failure reports (similarly to __GFP_NOWARN).
62 */
63#define DMA_ATTR_NO_WARN (1UL << 8)
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070064
Bjorn Helgaas77f2ea22014-04-30 11:20:53 -060065/*
Mitchel Humpherysb2fb3662017-01-06 18:58:11 +053066 * DMA_ATTR_PRIVILEGED: used to indicate that the buffer is fully
67 * accessible at an elevated privilege level (and ideally inaccessible or
68 * at least read-only at lesser-privileged levels).
69 */
70#define DMA_ATTR_PRIVILEGED (1UL << 9)
71
72/*
Bjorn Helgaas77f2ea22014-04-30 11:20:53 -060073 * A dma_addr_t can hold any valid DMA or bus address for the platform.
74 * It can be given to a device to use as a DMA source or target. A CPU cannot
75 * reference a dma_addr_t directly because there may be translation between
76 * its physical address space and the bus address space.
77 */
FUJITA Tomonorif0402a22009-01-05 23:59:01 +090078struct dma_map_ops {
Marek Szyprowski613c4572012-03-28 16:36:27 +020079 void* (*alloc)(struct device *dev, size_t size,
80 dma_addr_t *dma_handle, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070081 unsigned long attrs);
Marek Szyprowski613c4572012-03-28 16:36:27 +020082 void (*free)(struct device *dev, size_t size,
83 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070084 unsigned long attrs);
Marek Szyprowski9adc5372011-12-21 16:55:33 +010085 int (*mmap)(struct device *, struct vm_area_struct *,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070086 void *, dma_addr_t, size_t,
87 unsigned long attrs);
Marek Szyprowski9adc5372011-12-21 16:55:33 +010088
Marek Szyprowskid2b74282012-06-13 10:05:52 +020089 int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070090 dma_addr_t, size_t, unsigned long attrs);
Marek Szyprowskid2b74282012-06-13 10:05:52 +020091
FUJITA Tomonorif0402a22009-01-05 23:59:01 +090092 dma_addr_t (*map_page)(struct device *dev, struct page *page,
93 unsigned long offset, size_t size,
94 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070095 unsigned long attrs);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +090096 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
97 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070098 unsigned long attrs);
Ricardo Ribalda Delgado04abab62015-02-11 13:53:15 +010099 /*
100 * map_sg returns 0 on error and a value > 0 on success.
101 * It should never return a value < 0.
102 */
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900103 int (*map_sg)(struct device *dev, struct scatterlist *sg,
104 int nents, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700105 unsigned long attrs);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900106 void (*unmap_sg)(struct device *dev,
107 struct scatterlist *sg, int nents,
108 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700109 unsigned long attrs);
Niklas Söderlundba409b32016-08-10 13:22:14 +0200110 dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
111 size_t size, enum dma_data_direction dir,
112 unsigned long attrs);
113 void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
114 size_t size, enum dma_data_direction dir,
115 unsigned long attrs);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900116 void (*sync_single_for_cpu)(struct device *dev,
117 dma_addr_t dma_handle, size_t size,
118 enum dma_data_direction dir);
119 void (*sync_single_for_device)(struct device *dev,
120 dma_addr_t dma_handle, size_t size,
121 enum dma_data_direction dir);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900122 void (*sync_sg_for_cpu)(struct device *dev,
123 struct scatterlist *sg, int nents,
124 enum dma_data_direction dir);
125 void (*sync_sg_for_device)(struct device *dev,
126 struct scatterlist *sg, int nents,
127 enum dma_data_direction dir);
128 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
129 int (*dma_supported)(struct device *dev, u64 mask);
Milton Miller3a8f7552011-06-24 09:05:23 +0000130#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
131 u64 (*get_required_mask)(struct device *dev);
132#endif
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900133 int is_phys;
134};
135
Bart Van Assche52997092017-01-20 13:04:01 -0800136extern const struct dma_map_ops dma_noop_ops;
Bart Van Assche551199a2017-01-20 13:04:07 -0800137extern const struct dma_map_ops dma_virt_ops;
Christian Borntraegera8463d42016-02-02 21:46:32 -0800138
Andrew Morton8f286c32007-10-18 03:05:07 -0700139#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
Borislav Petkov34c65382007-10-18 03:05:06 -0700140
James Bottomley32e8f702007-10-16 01:23:55 -0700141#define DMA_MASK_NONE 0x0ULL
142
Rolf Eike Beerd6bd3a32006-09-29 01:59:48 -0700143static inline int valid_dma_direction(int dma_direction)
144{
145 return ((dma_direction == DMA_BIDIRECTIONAL) ||
146 (dma_direction == DMA_TO_DEVICE) ||
147 (dma_direction == DMA_FROM_DEVICE));
148}
149
James Bottomley32e8f702007-10-16 01:23:55 -0700150static inline int is_device_dma_capable(struct device *dev)
151{
152 return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
153}
154
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800155#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
156/*
157 * These three functions are only for dma allocator.
158 * Don't use them in device drivers.
159 */
160int dma_alloc_from_coherent(struct device *dev, ssize_t size,
161 dma_addr_t *dma_handle, void **ret);
162int dma_release_from_coherent(struct device *dev, int order, void *vaddr);
163
164int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
165 void *cpu_addr, size_t size, int *ret);
166#else
167#define dma_alloc_from_coherent(dev, size, handle, ret) (0)
168#define dma_release_from_coherent(dev, order, vaddr) (0)
169#define dma_mmap_from_coherent(dev, vma, vaddr, order, ret) (0)
170#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
171
Dan Williams1b0fac42007-07-15 23:40:26 -0700172#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#include <asm/dma-mapping.h>
Bart Van Assche815dd182017-01-20 13:04:04 -0800174static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
175{
176 if (dev && dev->dma_ops)
177 return dev->dma_ops;
178 return get_arch_dma_ops(dev ? dev->bus : NULL);
179}
180
Bart Van Asscheca6e8e12017-01-20 13:04:03 -0800181static inline void set_dma_ops(struct device *dev,
182 const struct dma_map_ops *dma_ops)
183{
184 dev->dma_ops = dma_ops;
185}
Dan Williams1b0fac42007-07-15 23:40:26 -0700186#else
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800187/*
188 * Define the dma api to allow compilation but not linking of
189 * dma dependent code. Code that depends on the dma-mapping
190 * API needs to set 'depends on HAS_DMA' in its Kconfig
191 */
Bart Van Assche52997092017-01-20 13:04:01 -0800192extern const struct dma_map_ops bad_dma_ops;
193static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800194{
195 return &bad_dma_ops;
196}
197#endif
198
199static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
200 size_t size,
201 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700202 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800203{
Bart Van Assche52997092017-01-20 13:04:01 -0800204 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800205 dma_addr_t addr;
206
207 kmemcheck_mark_initialized(ptr, size);
208 BUG_ON(!valid_dma_direction(dir));
209 addr = ops->map_page(dev, virt_to_page(ptr),
Geliang Tang8e994692016-01-20 15:02:12 -0800210 offset_in_page(ptr), size,
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800211 dir, attrs);
212 debug_dma_map_page(dev, virt_to_page(ptr),
Geliang Tang8e994692016-01-20 15:02:12 -0800213 offset_in_page(ptr), size,
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800214 dir, addr, true);
215 return addr;
216}
217
218static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
219 size_t size,
220 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700221 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800222{
Bart Van Assche52997092017-01-20 13:04:01 -0800223 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800224
225 BUG_ON(!valid_dma_direction(dir));
226 if (ops->unmap_page)
227 ops->unmap_page(dev, addr, size, dir, attrs);
228 debug_dma_unmap_page(dev, addr, size, dir, true);
229}
230
231/*
232 * dma_maps_sg_attrs returns 0 on error and > 0 on success.
233 * It should never return a value < 0.
234 */
235static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
236 int nents, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700237 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800238{
Bart Van Assche52997092017-01-20 13:04:01 -0800239 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800240 int i, ents;
241 struct scatterlist *s;
242
243 for_each_sg(sg, s, nents, i)
244 kmemcheck_mark_initialized(sg_virt(s), s->length);
245 BUG_ON(!valid_dma_direction(dir));
246 ents = ops->map_sg(dev, sg, nents, dir, attrs);
247 BUG_ON(ents < 0);
248 debug_dma_map_sg(dev, sg, nents, ents, dir);
249
250 return ents;
251}
252
253static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
254 int nents, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700255 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800256{
Bart Van Assche52997092017-01-20 13:04:01 -0800257 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800258
259 BUG_ON(!valid_dma_direction(dir));
260 debug_dma_unmap_sg(dev, sg, nents, dir);
261 if (ops->unmap_sg)
262 ops->unmap_sg(dev, sg, nents, dir, attrs);
263}
264
Alexander Duyck0495c3d2016-12-14 15:05:23 -0800265static inline dma_addr_t dma_map_page_attrs(struct device *dev,
266 struct page *page,
267 size_t offset, size_t size,
268 enum dma_data_direction dir,
269 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800270{
Bart Van Assche52997092017-01-20 13:04:01 -0800271 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800272 dma_addr_t addr;
273
274 kmemcheck_mark_initialized(page_address(page) + offset, size);
275 BUG_ON(!valid_dma_direction(dir));
Alexander Duyck0495c3d2016-12-14 15:05:23 -0800276 addr = ops->map_page(dev, page, offset, size, dir, attrs);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800277 debug_dma_map_page(dev, page, offset, size, dir, addr, false);
278
279 return addr;
280}
281
Alexander Duyck0495c3d2016-12-14 15:05:23 -0800282static inline void dma_unmap_page_attrs(struct device *dev,
283 dma_addr_t addr, size_t size,
284 enum dma_data_direction dir,
285 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800286{
Bart Van Assche52997092017-01-20 13:04:01 -0800287 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800288
289 BUG_ON(!valid_dma_direction(dir));
290 if (ops->unmap_page)
Alexander Duyck0495c3d2016-12-14 15:05:23 -0800291 ops->unmap_page(dev, addr, size, dir, attrs);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800292 debug_dma_unmap_page(dev, addr, size, dir, false);
293}
294
Niklas Söderlund6f3d8792016-08-10 13:22:16 +0200295static inline dma_addr_t dma_map_resource(struct device *dev,
296 phys_addr_t phys_addr,
297 size_t size,
298 enum dma_data_direction dir,
299 unsigned long attrs)
300{
Bart Van Assche52997092017-01-20 13:04:01 -0800301 const struct dma_map_ops *ops = get_dma_ops(dev);
Niklas Söderlund6f3d8792016-08-10 13:22:16 +0200302 dma_addr_t addr;
303
304 BUG_ON(!valid_dma_direction(dir));
305
306 /* Don't allow RAM to be mapped */
Niklas Söderlund3757dc42016-09-29 12:02:40 +0200307 BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
Niklas Söderlund6f3d8792016-08-10 13:22:16 +0200308
309 addr = phys_addr;
310 if (ops->map_resource)
311 addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
312
313 debug_dma_map_resource(dev, phys_addr, size, dir, addr);
314
315 return addr;
316}
317
318static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
319 size_t size, enum dma_data_direction dir,
320 unsigned long attrs)
321{
Bart Van Assche52997092017-01-20 13:04:01 -0800322 const struct dma_map_ops *ops = get_dma_ops(dev);
Niklas Söderlund6f3d8792016-08-10 13:22:16 +0200323
324 BUG_ON(!valid_dma_direction(dir));
325 if (ops->unmap_resource)
326 ops->unmap_resource(dev, addr, size, dir, attrs);
327 debug_dma_unmap_resource(dev, addr, size, dir);
328}
329
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800330static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
331 size_t size,
332 enum dma_data_direction dir)
333{
Bart Van Assche52997092017-01-20 13:04:01 -0800334 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800335
336 BUG_ON(!valid_dma_direction(dir));
337 if (ops->sync_single_for_cpu)
338 ops->sync_single_for_cpu(dev, addr, size, dir);
339 debug_dma_sync_single_for_cpu(dev, addr, size, dir);
340}
341
342static inline void dma_sync_single_for_device(struct device *dev,
343 dma_addr_t addr, size_t size,
344 enum dma_data_direction dir)
345{
Bart Van Assche52997092017-01-20 13:04:01 -0800346 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800347
348 BUG_ON(!valid_dma_direction(dir));
349 if (ops->sync_single_for_device)
350 ops->sync_single_for_device(dev, addr, size, dir);
351 debug_dma_sync_single_for_device(dev, addr, size, dir);
352}
353
354static inline void dma_sync_single_range_for_cpu(struct device *dev,
355 dma_addr_t addr,
356 unsigned long offset,
357 size_t size,
358 enum dma_data_direction dir)
359{
360 const struct dma_map_ops *ops = get_dma_ops(dev);
361
362 BUG_ON(!valid_dma_direction(dir));
363 if (ops->sync_single_for_cpu)
364 ops->sync_single_for_cpu(dev, addr + offset, size, dir);
365 debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
366}
367
368static inline void dma_sync_single_range_for_device(struct device *dev,
369 dma_addr_t addr,
370 unsigned long offset,
371 size_t size,
372 enum dma_data_direction dir)
373{
374 const struct dma_map_ops *ops = get_dma_ops(dev);
375
376 BUG_ON(!valid_dma_direction(dir));
377 if (ops->sync_single_for_device)
378 ops->sync_single_for_device(dev, addr + offset, size, dir);
379 debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
380}
381
382static inline void
383dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
384 int nelems, enum dma_data_direction dir)
385{
Bart Van Assche52997092017-01-20 13:04:01 -0800386 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800387
388 BUG_ON(!valid_dma_direction(dir));
389 if (ops->sync_sg_for_cpu)
390 ops->sync_sg_for_cpu(dev, sg, nelems, dir);
391 debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
392}
393
394static inline void
395dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
396 int nelems, enum dma_data_direction dir)
397{
Bart Van Assche52997092017-01-20 13:04:01 -0800398 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800399
400 BUG_ON(!valid_dma_direction(dir));
401 if (ops->sync_sg_for_device)
402 ops->sync_sg_for_device(dev, sg, nelems, dir);
403 debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
404
405}
406
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700407#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
408#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
409#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
410#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
Alexander Duyck0495c3d2016-12-14 15:05:23 -0800411#define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
412#define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800413
414extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
415 void *cpu_addr, dma_addr_t dma_addr, size_t size);
416
417void *dma_common_contiguous_remap(struct page *page, size_t size,
418 unsigned long vm_flags,
419 pgprot_t prot, const void *caller);
420
421void *dma_common_pages_remap(struct page **pages, size_t size,
422 unsigned long vm_flags, pgprot_t prot,
423 const void *caller);
424void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
425
426/**
427 * dma_mmap_attrs - map a coherent DMA allocation into user space
428 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
429 * @vma: vm_area_struct describing requested user mapping
430 * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
431 * @handle: device-view address returned from dma_alloc_attrs
432 * @size: size of memory originally requested in dma_alloc_attrs
433 * @attrs: attributes of mapping properties requested in dma_alloc_attrs
434 *
435 * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
436 * into user space. The coherent DMA buffer must not be freed by the
437 * driver until the user space mapping has been released.
438 */
439static inline int
440dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700441 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800442{
Bart Van Assche52997092017-01-20 13:04:01 -0800443 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800444 BUG_ON(!ops);
445 if (ops->mmap)
446 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
447 return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
448}
449
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700450#define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800451
452int
453dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
454 void *cpu_addr, dma_addr_t dma_addr, size_t size);
455
456static inline int
457dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700458 dma_addr_t dma_addr, size_t size,
459 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800460{
Bart Van Assche52997092017-01-20 13:04:01 -0800461 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800462 BUG_ON(!ops);
463 if (ops->get_sgtable)
464 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
465 attrs);
466 return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
467}
468
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700469#define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800470
471#ifndef arch_dma_alloc_attrs
472#define arch_dma_alloc_attrs(dev, flag) (true)
473#endif
474
475static inline void *dma_alloc_attrs(struct device *dev, size_t size,
476 dma_addr_t *dma_handle, gfp_t flag,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700477 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800478{
Bart Van Assche52997092017-01-20 13:04:01 -0800479 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800480 void *cpu_addr;
481
482 BUG_ON(!ops);
483
484 if (dma_alloc_from_coherent(dev, size, dma_handle, &cpu_addr))
485 return cpu_addr;
486
487 if (!arch_dma_alloc_attrs(&dev, &flag))
488 return NULL;
489 if (!ops->alloc)
490 return NULL;
491
492 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
493 debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
494 return cpu_addr;
495}
496
497static inline void dma_free_attrs(struct device *dev, size_t size,
498 void *cpu_addr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700499 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800500{
Bart Van Assche52997092017-01-20 13:04:01 -0800501 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800502
503 BUG_ON(!ops);
504 WARN_ON(irqs_disabled());
505
506 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
507 return;
508
Zhen Leid6b7eae2016-03-09 14:08:38 -0800509 if (!ops->free || !cpu_addr)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800510 return;
511
512 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
513 ops->free(dev, size, cpu_addr, dma_handle, attrs);
514}
515
516static inline void *dma_alloc_coherent(struct device *dev, size_t size,
517 dma_addr_t *dma_handle, gfp_t flag)
518{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700519 return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800520}
521
522static inline void dma_free_coherent(struct device *dev, size_t size,
523 void *cpu_addr, dma_addr_t dma_handle)
524{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700525 return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800526}
527
528static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
529 dma_addr_t *dma_handle, gfp_t gfp)
530{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700531 return dma_alloc_attrs(dev, size, dma_handle, gfp,
532 DMA_ATTR_NON_CONSISTENT);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800533}
534
535static inline void dma_free_noncoherent(struct device *dev, size_t size,
536 void *cpu_addr, dma_addr_t dma_handle)
537{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700538 dma_free_attrs(dev, size, cpu_addr, dma_handle,
539 DMA_ATTR_NON_CONSISTENT);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800540}
541
542static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
543{
544 debug_dma_mapping_error(dev, dma_addr);
545
546 if (get_dma_ops(dev)->mapping_error)
547 return get_dma_ops(dev)->mapping_error(dev, dma_addr);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800548 return 0;
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800549}
550
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800551static inline int dma_supported(struct device *dev, u64 mask)
552{
Bart Van Assche52997092017-01-20 13:04:01 -0800553 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800554
555 if (!ops)
556 return 0;
557 if (!ops->dma_supported)
558 return 1;
559 return ops->dma_supported(dev, mask);
560}
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800561
562#ifndef HAVE_ARCH_DMA_SET_MASK
563static inline int dma_set_mask(struct device *dev, u64 mask)
564{
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800565 if (!dev->dma_mask || !dma_supported(dev, mask))
566 return -EIO;
567 *dev->dma_mask = mask;
568 return 0;
569}
Dan Williams1b0fac42007-07-15 23:40:26 -0700570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
FUJITA Tomonori589fc9a2008-09-12 19:42:34 +0900572static inline u64 dma_get_mask(struct device *dev)
573{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900574 if (dev && dev->dma_mask && *dev->dma_mask)
FUJITA Tomonori589fc9a2008-09-12 19:42:34 +0900575 return *dev->dma_mask;
Yang Hongyang284901a2009-04-06 19:01:15 -0700576 return DMA_BIT_MASK(32);
FUJITA Tomonori589fc9a2008-09-12 19:42:34 +0900577}
578
Rob Herring58af4a22012-03-20 14:33:01 -0500579#ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
FUJITA Tomonori710224f2010-09-22 13:04:55 -0700580int dma_set_coherent_mask(struct device *dev, u64 mask);
581#else
FUJITA Tomonori6a1961f2010-03-10 15:23:39 -0800582static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
583{
584 if (!dma_supported(dev, mask))
585 return -EIO;
586 dev->coherent_dma_mask = mask;
587 return 0;
588}
FUJITA Tomonori710224f2010-09-22 13:04:55 -0700589#endif
FUJITA Tomonori6a1961f2010-03-10 15:23:39 -0800590
Russell King4aa806b2013-06-26 13:49:44 +0100591/*
592 * Set both the DMA mask and the coherent DMA mask to the same thing.
593 * Note that we don't check the return value from dma_set_coherent_mask()
594 * as the DMA API guarantees that the coherent DMA mask can be set to
595 * the same or smaller than the streaming DMA mask.
596 */
597static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
598{
599 int rc = dma_set_mask(dev, mask);
600 if (rc == 0)
601 dma_set_coherent_mask(dev, mask);
602 return rc;
603}
604
Russell Kingfa6a8d62013-06-27 12:21:45 +0100605/*
606 * Similar to the above, except it deals with the case where the device
607 * does not have dev->dma_mask appropriately setup.
608 */
609static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
610{
611 dev->dma_mask = &dev->coherent_dma_mask;
612 return dma_set_mask_and_coherent(dev, mask);
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615extern u64 dma_get_required_mask(struct device *dev);
616
Will Deacona3a60f82014-08-27 15:49:10 +0100617#ifndef arch_setup_dma_ops
Will Deacon97890ba2014-08-27 16:24:20 +0100618static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
Robin Murphy53c92d72016-04-07 18:42:05 +0100619 u64 size, const struct iommu_ops *iommu,
Will Deacon97890ba2014-08-27 16:24:20 +0100620 bool coherent) { }
621#endif
622
623#ifndef arch_teardown_dma_ops
624static inline void arch_teardown_dma_ops(struct device *dev) { }
Santosh Shilimkar591c1ee2014-04-24 11:30:04 -0400625#endif
626
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800627static inline unsigned int dma_get_max_seg_size(struct device *dev)
628{
Robin Murphy002edb62015-11-06 16:32:51 -0800629 if (dev->dma_parms && dev->dma_parms->max_segment_size)
630 return dev->dma_parms->max_segment_size;
631 return SZ_64K;
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800632}
633
634static inline unsigned int dma_set_max_seg_size(struct device *dev,
635 unsigned int size)
636{
637 if (dev->dma_parms) {
638 dev->dma_parms->max_segment_size = size;
639 return 0;
Robin Murphy002edb62015-11-06 16:32:51 -0800640 }
641 return -EIO;
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800642}
643
FUJITA Tomonorid22a6962008-02-04 22:28:13 -0800644static inline unsigned long dma_get_seg_boundary(struct device *dev)
645{
Robin Murphy002edb62015-11-06 16:32:51 -0800646 if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
647 return dev->dma_parms->segment_boundary_mask;
648 return DMA_BIT_MASK(32);
FUJITA Tomonorid22a6962008-02-04 22:28:13 -0800649}
650
651static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
652{
653 if (dev->dma_parms) {
654 dev->dma_parms->segment_boundary_mask = mask;
655 return 0;
Robin Murphy002edb62015-11-06 16:32:51 -0800656 }
657 return -EIO;
FUJITA Tomonorid22a6962008-02-04 22:28:13 -0800658}
659
Santosh Shilimkar00c8f162013-07-29 14:18:48 +0100660#ifndef dma_max_pfn
661static inline unsigned long dma_max_pfn(struct device *dev)
662{
663 return *dev->dma_mask >> PAGE_SHIFT;
664}
665#endif
666
Andrew Morton842fa692011-11-02 13:39:33 -0700667static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
668 dma_addr_t *dma_handle, gfp_t flag)
669{
Joe Perchesede23fa82013-08-26 22:45:23 -0700670 void *ret = dma_alloc_coherent(dev, size, dma_handle,
671 flag | __GFP_ZERO);
Andrew Morton842fa692011-11-02 13:39:33 -0700672 return ret;
673}
674
Heiko Carstense259f192010-08-13 09:39:18 +0200675#ifdef CONFIG_HAS_DMA
FUJITA Tomonori4565f012010-08-10 18:03:22 -0700676static inline int dma_get_cache_alignment(void)
677{
678#ifdef ARCH_DMA_MINALIGN
679 return ARCH_DMA_MINALIGN;
680#endif
681 return 1;
682}
Heiko Carstense259f192010-08-13 09:39:18 +0200683#endif
FUJITA Tomonori4565f012010-08-10 18:03:22 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/* flags for the coherent memory api */
686#define DMA_MEMORY_MAP 0x01
687#define DMA_MEMORY_IO 0x02
688#define DMA_MEMORY_INCLUDES_CHILDREN 0x04
689#define DMA_MEMORY_EXCLUSIVE 0x08
690
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800691#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
692int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
693 dma_addr_t device_addr, size_t size, int flags);
694void dma_release_declared_memory(struct device *dev);
695void *dma_mark_declared_memory_occupied(struct device *dev,
696 dma_addr_t device_addr, size_t size);
697#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static inline int
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600699dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 dma_addr_t device_addr, size_t size, int flags)
701{
702 return 0;
703}
704
705static inline void
706dma_release_declared_memory(struct device *dev)
707{
708}
709
710static inline void *
711dma_mark_declared_memory_occupied(struct device *dev,
712 dma_addr_t device_addr, size_t size)
713{
714 return ERR_PTR(-EBUSY);
715}
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800716#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Sricharan R09515ef2017-04-10 16:51:01 +0530718#ifdef CONFIG_HAS_DMA
719int dma_configure(struct device *dev);
720void dma_deconfigure(struct device *dev);
721#else
722static inline int dma_configure(struct device *dev)
723{
724 return 0;
725}
726
727static inline void dma_deconfigure(struct device *dev) {}
728#endif
729
Tejun Heo9ac78492007-01-20 16:00:26 +0900730/*
731 * Managed DMA API
732 */
733extern void *dmam_alloc_coherent(struct device *dev, size_t size,
734 dma_addr_t *dma_handle, gfp_t gfp);
735extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
736 dma_addr_t dma_handle);
Christoph Hellwig63d36c92017-06-12 19:15:04 +0200737extern void *dmam_alloc_attrs(struct device *dev, size_t size,
738 dma_addr_t *dma_handle, gfp_t gfp,
739 unsigned long attrs);
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800740#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600741extern int dmam_declare_coherent_memory(struct device *dev,
742 phys_addr_t phys_addr,
Tejun Heo9ac78492007-01-20 16:00:26 +0900743 dma_addr_t device_addr, size_t size,
744 int flags);
745extern void dmam_release_declared_memory(struct device *dev);
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800746#else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
Tejun Heo9ac78492007-01-20 16:00:26 +0900747static inline int dmam_declare_coherent_memory(struct device *dev,
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600748 phys_addr_t phys_addr, dma_addr_t device_addr,
Tejun Heo9ac78492007-01-20 16:00:26 +0900749 size_t size, gfp_t gfp)
750{
751 return 0;
752}
753
754static inline void dmam_release_declared_memory(struct device *dev)
755{
756}
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800757#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
Tejun Heo9ac78492007-01-20 16:00:26 +0900758
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800759static inline void *dma_alloc_wc(struct device *dev, size_t size,
760 dma_addr_t *dma_addr, gfp_t gfp)
Thierry Redingb4bbb102014-06-27 11:56:58 +0200761{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700762 return dma_alloc_attrs(dev, size, dma_addr, gfp,
763 DMA_ATTR_WRITE_COMBINE);
Thierry Redingb4bbb102014-06-27 11:56:58 +0200764}
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800765#ifndef dma_alloc_writecombine
766#define dma_alloc_writecombine dma_alloc_wc
767#endif
Thierry Redingb4bbb102014-06-27 11:56:58 +0200768
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800769static inline void dma_free_wc(struct device *dev, size_t size,
770 void *cpu_addr, dma_addr_t dma_addr)
Thierry Redingb4bbb102014-06-27 11:56:58 +0200771{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700772 return dma_free_attrs(dev, size, cpu_addr, dma_addr,
773 DMA_ATTR_WRITE_COMBINE);
Thierry Redingb4bbb102014-06-27 11:56:58 +0200774}
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800775#ifndef dma_free_writecombine
776#define dma_free_writecombine dma_free_wc
777#endif
Thierry Redingb4bbb102014-06-27 11:56:58 +0200778
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800779static inline int dma_mmap_wc(struct device *dev,
780 struct vm_area_struct *vma,
781 void *cpu_addr, dma_addr_t dma_addr,
782 size_t size)
Thierry Redingb4bbb102014-06-27 11:56:58 +0200783{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700784 return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
785 DMA_ATTR_WRITE_COMBINE);
Thierry Redingb4bbb102014-06-27 11:56:58 +0200786}
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800787#ifndef dma_mmap_writecombine
788#define dma_mmap_writecombine dma_mmap_wc
789#endif
Arthur Kepner74bc7ce2008-04-29 01:00:30 -0700790
Andrey Smirnov24813662016-09-28 15:22:33 -0700791#if defined(CONFIG_NEED_DMA_MAP_STATE) || defined(CONFIG_DMA_API_DEBUG)
FUJITA Tomonori0acedc12010-03-10 15:23:31 -0800792#define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
793#define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
794#define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
795#define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
796#define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
797#define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
798#else
799#define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
800#define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
801#define dma_unmap_addr(PTR, ADDR_NAME) (0)
802#define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
803#define dma_unmap_len(PTR, LEN_NAME) (0)
804#define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
805#endif
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807#endif