blob: d046ba16041b0d0102bf34008de2bbee3d79ddc2 [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)
Laura Abbottcc36e642012-10-29 13:08:03 -070059/*
60 * DMA_ATTR_STRONGLY_ORDERED: Specifies that accesses to the mapping must
61 * not be buffered, reordered, merged with other accesses, or unaligned.
62 * No speculative access may occur in this mapping.
63 */
64#define DMA_ATTR_STRONGLY_ORDERED (1UL << 8)
Laura Abbott91e4dc42014-08-05 19:39:38 -070065/*
66 * DMA_ATTR_SKIP_ZEROING: Do not zero mapping.
67 */
68#define DMA_ATTR_SKIP_ZEROING (1UL << 9)
Rohit Vaswani679ede32015-07-06 16:22:29 -070069/*
70 * DMA_ATTR_NO_DELAYED_UNMAP: Used by msm specific lazy mapping to indicate
71 * that the mapping can be freed on unmap, rather than when the ion_buffer
72 * is freed.
73 */
74#define DMA_ATTR_NO_DELAYED_UNMAP (1UL << 10)
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070075
Bjorn Helgaas77f2ea22014-04-30 11:20:53 -060076/*
77 * A dma_addr_t can hold any valid DMA or bus address for the platform.
78 * It can be given to a device to use as a DMA source or target. A CPU cannot
79 * reference a dma_addr_t directly because there may be translation between
80 * its physical address space and the bus address space.
81 */
FUJITA Tomonorif0402a22009-01-05 23:59:01 +090082struct dma_map_ops {
Marek Szyprowski613c4572012-03-28 16:36:27 +020083 void* (*alloc)(struct device *dev, size_t size,
84 dma_addr_t *dma_handle, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070085 unsigned long attrs);
Marek Szyprowski613c4572012-03-28 16:36:27 +020086 void (*free)(struct device *dev, size_t size,
87 void *vaddr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070088 unsigned long attrs);
Marek Szyprowski9adc5372011-12-21 16:55:33 +010089 int (*mmap)(struct device *, struct vm_area_struct *,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070090 void *, dma_addr_t, size_t,
91 unsigned long attrs);
Marek Szyprowski9adc5372011-12-21 16:55:33 +010092
Marek Szyprowskid2b74282012-06-13 10:05:52 +020093 int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070094 dma_addr_t, size_t, unsigned long attrs);
Marek Szyprowskid2b74282012-06-13 10:05:52 +020095
FUJITA Tomonorif0402a22009-01-05 23:59:01 +090096 dma_addr_t (*map_page)(struct device *dev, struct page *page,
97 unsigned long offset, size_t size,
98 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070099 unsigned long attrs);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900100 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
101 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700102 unsigned long attrs);
Ricardo Ribalda Delgado04abab62015-02-11 13:53:15 +0100103 /*
104 * map_sg returns 0 on error and a value > 0 on success.
105 * It should never return a value < 0.
106 */
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900107 int (*map_sg)(struct device *dev, struct scatterlist *sg,
108 int nents, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700109 unsigned long attrs);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900110 void (*unmap_sg)(struct device *dev,
111 struct scatterlist *sg, int nents,
112 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700113 unsigned long attrs);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900114 void (*sync_single_for_cpu)(struct device *dev,
115 dma_addr_t dma_handle, size_t size,
116 enum dma_data_direction dir);
117 void (*sync_single_for_device)(struct device *dev,
118 dma_addr_t dma_handle, size_t size,
119 enum dma_data_direction dir);
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900120 void (*sync_sg_for_cpu)(struct device *dev,
121 struct scatterlist *sg, int nents,
122 enum dma_data_direction dir);
123 void (*sync_sg_for_device)(struct device *dev,
124 struct scatterlist *sg, int nents,
125 enum dma_data_direction dir);
126 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
127 int (*dma_supported)(struct device *dev, u64 mask);
FUJITA Tomonorif726f30e2009-08-04 19:08:24 +0000128 int (*set_dma_mask)(struct device *dev, u64 mask);
Laura Abbott060e2df2014-08-05 19:16:28 -0700129 void *(*remap)(struct device *dev, void *cpu_addr, dma_addr_t handle,
130 size_t size, unsigned long attrs);
131 void (*unremap)(struct device *dev, void *remapped_address,
132 size_t size);
Milton Miller3a8f7552011-06-24 09:05:23 +0000133#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
134 u64 (*get_required_mask)(struct device *dev);
135#endif
FUJITA Tomonorif0402a22009-01-05 23:59:01 +0900136 int is_phys;
137};
138
Christian Borntraegera8463d42016-02-02 21:46:32 -0800139extern struct dma_map_ops dma_noop_ops;
140
Andrew Morton8f286c32007-10-18 03:05:07 -0700141#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
Borislav Petkov34c65382007-10-18 03:05:06 -0700142
James Bottomley32e8f702007-10-16 01:23:55 -0700143#define DMA_MASK_NONE 0x0ULL
144
Rolf Eike Beerd6bd3a32006-09-29 01:59:48 -0700145static inline int valid_dma_direction(int dma_direction)
146{
147 return ((dma_direction == DMA_BIDIRECTIONAL) ||
148 (dma_direction == DMA_TO_DEVICE) ||
149 (dma_direction == DMA_FROM_DEVICE));
150}
151
James Bottomley32e8f702007-10-16 01:23:55 -0700152static inline int is_device_dma_capable(struct device *dev)
153{
154 return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
155}
156
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800157#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
158/*
159 * These three functions are only for dma allocator.
160 * Don't use them in device drivers.
161 */
162int dma_alloc_from_coherent(struct device *dev, ssize_t size,
163 dma_addr_t *dma_handle, void **ret);
164int dma_release_from_coherent(struct device *dev, int order, void *vaddr);
165
166int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
167 void *cpu_addr, size_t size, int *ret);
168#else
169#define dma_alloc_from_coherent(dev, size, handle, ret) (0)
170#define dma_release_from_coherent(dev, order, vaddr) (0)
171#define dma_mmap_from_coherent(dev, vma, vaddr, order, ret) (0)
172#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
173
Dan Williams1b0fac42007-07-15 23:40:26 -0700174#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#include <asm/dma-mapping.h>
Dan Williams1b0fac42007-07-15 23:40:26 -0700176#else
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800177/*
178 * Define the dma api to allow compilation but not linking of
179 * dma dependent code. Code that depends on the dma-mapping
180 * API needs to set 'depends on HAS_DMA' in its Kconfig
181 */
182extern struct dma_map_ops bad_dma_ops;
183static inline struct dma_map_ops *get_dma_ops(struct device *dev)
184{
185 return &bad_dma_ops;
186}
187#endif
188
189static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
190 size_t size,
191 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700192 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800193{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700194 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800195 dma_addr_t addr;
196
197 kmemcheck_mark_initialized(ptr, size);
198 BUG_ON(!valid_dma_direction(dir));
199 addr = ops->map_page(dev, virt_to_page(ptr),
Geliang Tang8e994692016-01-20 15:02:12 -0800200 offset_in_page(ptr), size,
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800201 dir, attrs);
202 debug_dma_map_page(dev, virt_to_page(ptr),
Geliang Tang8e994692016-01-20 15:02:12 -0800203 offset_in_page(ptr), size,
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800204 dir, addr, true);
205 return addr;
206}
207
208static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
209 size_t size,
210 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700211 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800212{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700213 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800214
215 BUG_ON(!valid_dma_direction(dir));
216 if (ops->unmap_page)
217 ops->unmap_page(dev, addr, size, dir, attrs);
218 debug_dma_unmap_page(dev, addr, size, dir, true);
219}
220
221/*
222 * dma_maps_sg_attrs returns 0 on error and > 0 on success.
223 * It should never return a value < 0.
224 */
225static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
226 int nents, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700227 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800228{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700229 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800230 int i, ents;
231 struct scatterlist *s;
232
233 for_each_sg(sg, s, nents, i)
234 kmemcheck_mark_initialized(sg_virt(s), s->length);
235 BUG_ON(!valid_dma_direction(dir));
236 ents = ops->map_sg(dev, sg, nents, dir, attrs);
237 BUG_ON(ents < 0);
238 debug_dma_map_sg(dev, sg, nents, ents, dir);
239
240 return ents;
241}
242
243static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
244 int nents, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700245 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800246{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700247 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800248
249 BUG_ON(!valid_dma_direction(dir));
250 debug_dma_unmap_sg(dev, sg, nents, dir);
251 if (ops->unmap_sg)
252 ops->unmap_sg(dev, sg, nents, dir, attrs);
253}
254
255static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
256 size_t offset, size_t size,
257 enum dma_data_direction dir)
258{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700259 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800260 dma_addr_t addr;
261
262 kmemcheck_mark_initialized(page_address(page) + offset, size);
263 BUG_ON(!valid_dma_direction(dir));
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700264 addr = ops->map_page(dev, page, offset, size, dir, 0);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800265 debug_dma_map_page(dev, page, offset, size, dir, addr, false);
266
267 return addr;
268}
269
270static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
271 size_t size, enum dma_data_direction dir)
272{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700273 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800274
275 BUG_ON(!valid_dma_direction(dir));
276 if (ops->unmap_page)
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700277 ops->unmap_page(dev, addr, size, dir, 0);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800278 debug_dma_unmap_page(dev, addr, size, dir, false);
279}
280
281static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
282 size_t size,
283 enum dma_data_direction dir)
284{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700285 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800286
287 BUG_ON(!valid_dma_direction(dir));
288 if (ops->sync_single_for_cpu)
289 ops->sync_single_for_cpu(dev, addr, size, dir);
290 debug_dma_sync_single_for_cpu(dev, addr, size, dir);
291}
292
293static inline void dma_sync_single_for_device(struct device *dev,
294 dma_addr_t addr, size_t size,
295 enum dma_data_direction dir)
296{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700297 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800298
299 BUG_ON(!valid_dma_direction(dir));
300 if (ops->sync_single_for_device)
301 ops->sync_single_for_device(dev, addr, size, dir);
302 debug_dma_sync_single_for_device(dev, addr, size, dir);
303}
304
305static inline void dma_sync_single_range_for_cpu(struct device *dev,
306 dma_addr_t addr,
307 unsigned long offset,
308 size_t size,
309 enum dma_data_direction dir)
310{
311 const struct dma_map_ops *ops = get_dma_ops(dev);
312
313 BUG_ON(!valid_dma_direction(dir));
314 if (ops->sync_single_for_cpu)
315 ops->sync_single_for_cpu(dev, addr + offset, size, dir);
316 debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
317}
318
319static inline void dma_sync_single_range_for_device(struct device *dev,
320 dma_addr_t addr,
321 unsigned long offset,
322 size_t size,
323 enum dma_data_direction dir)
324{
325 const struct dma_map_ops *ops = get_dma_ops(dev);
326
327 BUG_ON(!valid_dma_direction(dir));
328 if (ops->sync_single_for_device)
329 ops->sync_single_for_device(dev, addr + offset, size, dir);
330 debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
331}
332
333static inline void
334dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
335 int nelems, enum dma_data_direction dir)
336{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700337 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800338
339 BUG_ON(!valid_dma_direction(dir));
340 if (ops->sync_sg_for_cpu)
341 ops->sync_sg_for_cpu(dev, sg, nelems, dir);
342 debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
343}
344
345static inline void
346dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
347 int nelems, enum dma_data_direction dir)
348{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700349 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800350
351 BUG_ON(!valid_dma_direction(dir));
352 if (ops->sync_sg_for_device)
353 ops->sync_sg_for_device(dev, sg, nelems, dir);
354 debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
355
356}
357
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700358#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
359#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
360#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
361#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800362
363extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
364 void *cpu_addr, dma_addr_t dma_addr, size_t size);
365
366void *dma_common_contiguous_remap(struct page *page, size_t size,
367 unsigned long vm_flags,
368 pgprot_t prot, const void *caller);
369
370void *dma_common_pages_remap(struct page **pages, size_t size,
371 unsigned long vm_flags, pgprot_t prot,
372 const void *caller);
Laura Abbott060e2df2014-08-05 19:16:28 -0700373void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags,
374 bool nowarn);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800375
376/**
377 * 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 * @handle: 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
386 * into user space. The coherent DMA buffer must not be freed by the
387 * driver until the user space mapping has been released.
388 */
389static inline int
390dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700391 dma_addr_t dma_addr, size_t size, unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800392{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700393 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800394 BUG_ON(!ops);
395 if (ops->mmap)
396 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
397 return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
398}
399
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700400#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 -0800401
402int
403dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
404 void *cpu_addr, dma_addr_t dma_addr, size_t size);
405
406static inline int
407dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700408 dma_addr_t dma_addr, size_t size,
409 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800410{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700411 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800412 BUG_ON(!ops);
413 if (ops->get_sgtable)
414 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
415 attrs);
416 return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
417}
418
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700419#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 -0800420
421#ifndef arch_dma_alloc_attrs
422#define arch_dma_alloc_attrs(dev, flag) (true)
423#endif
424
425static inline void *dma_alloc_attrs(struct device *dev, size_t size,
426 dma_addr_t *dma_handle, gfp_t flag,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700427 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800428{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700429 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800430 void *cpu_addr;
431
432 BUG_ON(!ops);
433
434 if (dma_alloc_from_coherent(dev, size, dma_handle, &cpu_addr))
435 return cpu_addr;
436
437 if (!arch_dma_alloc_attrs(&dev, &flag))
438 return NULL;
439 if (!ops->alloc)
440 return NULL;
441
442 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
443 debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
444 return cpu_addr;
445}
446
447static inline void dma_free_attrs(struct device *dev, size_t size,
448 void *cpu_addr, dma_addr_t dma_handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700449 unsigned long attrs)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800450{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700451 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800452
453 BUG_ON(!ops);
454 WARN_ON(irqs_disabled());
455
456 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
457 return;
458
Zhen Leid6b7eae2016-03-09 14:08:38 -0800459 if (!ops->free || !cpu_addr)
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800460 return;
461
462 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
463 ops->free(dev, size, cpu_addr, dma_handle, attrs);
464}
465
466static inline void *dma_alloc_coherent(struct device *dev, size_t size,
467 dma_addr_t *dma_handle, gfp_t flag)
468{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700469 return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800470}
471
472static inline void dma_free_coherent(struct device *dev, size_t size,
473 void *cpu_addr, dma_addr_t dma_handle)
474{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700475 return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800476}
477
478static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
479 dma_addr_t *dma_handle, gfp_t gfp)
480{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700481 return dma_alloc_attrs(dev, size, dma_handle, gfp,
482 DMA_ATTR_NON_CONSISTENT);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800483}
484
485static inline void dma_free_noncoherent(struct device *dev, size_t size,
486 void *cpu_addr, dma_addr_t dma_handle)
487{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700488 dma_free_attrs(dev, size, cpu_addr, dma_handle,
489 DMA_ATTR_NON_CONSISTENT);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800490}
491
492static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
493{
494 debug_dma_mapping_error(dev, dma_addr);
495
496 if (get_dma_ops(dev)->mapping_error)
497 return get_dma_ops(dev)->mapping_error(dev, dma_addr);
498
499#ifdef DMA_ERROR_CODE
500 return dma_addr == DMA_ERROR_CODE;
501#else
502 return 0;
503#endif
504}
505
506#ifndef HAVE_ARCH_DMA_SUPPORTED
507static inline int dma_supported(struct device *dev, u64 mask)
508{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700509 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800510
511 if (!ops)
512 return 0;
513 if (!ops->dma_supported)
514 return 1;
515 return ops->dma_supported(dev, mask);
516}
517#endif
518
519#ifndef HAVE_ARCH_DMA_SET_MASK
520static inline int dma_set_mask(struct device *dev, u64 mask)
521{
Mitchel Humpherys56a826c2014-09-09 16:02:15 -0700522 const struct dma_map_ops *ops = get_dma_ops(dev);
Christoph Hellwige1c7e322016-01-20 15:02:05 -0800523
524 if (ops->set_dma_mask)
525 return ops->set_dma_mask(dev, mask);
526
527 if (!dev->dma_mask || !dma_supported(dev, mask))
528 return -EIO;
529 *dev->dma_mask = mask;
530 return 0;
531}
Dan Williams1b0fac42007-07-15 23:40:26 -0700532#endif
Laura Abbott060e2df2014-08-05 19:16:28 -0700533static inline void *dma_remap(struct device *dev, void *cpu_addr,
534 dma_addr_t dma_handle, size_t size, unsigned long attrs)
535{
536 const struct dma_map_ops *ops = get_dma_ops(dev);
537
538 if (!ops->remap) {
539 WARN_ONCE(1, "Remap function not implemented for %pS\n",
540 ops->remap);
541 return NULL;
542 }
543
544 return ops->remap(dev, cpu_addr, dma_handle, size, attrs);
545}
546
547
548static inline void dma_unremap(struct device *dev, void *remapped_addr,
549 size_t size)
550{
551 const struct dma_map_ops *ops = get_dma_ops(dev);
552
553 if (!ops->unremap) {
554 WARN_ONCE(1, "unremap function not implemented for %pS\n",
555 ops->unremap);
556 return;
557 }
558
559 return ops->unremap(dev, remapped_addr, size);
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
FUJITA Tomonori589fc9a2008-09-12 19:42:34 +0900563static inline u64 dma_get_mask(struct device *dev)
564{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900565 if (dev && dev->dma_mask && *dev->dma_mask)
FUJITA Tomonori589fc9a2008-09-12 19:42:34 +0900566 return *dev->dma_mask;
Yang Hongyang284901a2009-04-06 19:01:15 -0700567 return DMA_BIT_MASK(32);
FUJITA Tomonori589fc9a2008-09-12 19:42:34 +0900568}
569
Rob Herring58af4a22012-03-20 14:33:01 -0500570#ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
FUJITA Tomonori710224f2010-09-22 13:04:55 -0700571int dma_set_coherent_mask(struct device *dev, u64 mask);
572#else
FUJITA Tomonori6a1961f2010-03-10 15:23:39 -0800573static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
574{
575 if (!dma_supported(dev, mask))
576 return -EIO;
577 dev->coherent_dma_mask = mask;
578 return 0;
579}
FUJITA Tomonori710224f2010-09-22 13:04:55 -0700580#endif
FUJITA Tomonori6a1961f2010-03-10 15:23:39 -0800581
Russell King4aa806b2013-06-26 13:49:44 +0100582/*
583 * Set both the DMA mask and the coherent DMA mask to the same thing.
584 * Note that we don't check the return value from dma_set_coherent_mask()
585 * as the DMA API guarantees that the coherent DMA mask can be set to
586 * the same or smaller than the streaming DMA mask.
587 */
588static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
589{
590 int rc = dma_set_mask(dev, mask);
591 if (rc == 0)
592 dma_set_coherent_mask(dev, mask);
593 return rc;
594}
595
Russell Kingfa6a8d62013-06-27 12:21:45 +0100596/*
597 * Similar to the above, except it deals with the case where the device
598 * does not have dev->dma_mask appropriately setup.
599 */
600static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
601{
602 dev->dma_mask = &dev->coherent_dma_mask;
603 return dma_set_mask_and_coherent(dev, mask);
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606extern u64 dma_get_required_mask(struct device *dev);
607
Will Deacona3a60f82014-08-27 15:49:10 +0100608#ifndef arch_setup_dma_ops
Will Deacon97890ba2014-08-27 16:24:20 +0100609static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
Robin Murphy53c92d72016-04-07 18:42:05 +0100610 u64 size, const struct iommu_ops *iommu,
Will Deacon97890ba2014-08-27 16:24:20 +0100611 bool coherent) { }
612#endif
613
614#ifndef arch_teardown_dma_ops
615static inline void arch_teardown_dma_ops(struct device *dev) { }
Santosh Shilimkar591c1ee2014-04-24 11:30:04 -0400616#endif
617
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800618static inline unsigned int dma_get_max_seg_size(struct device *dev)
619{
Robin Murphy002edb62015-11-06 16:32:51 -0800620 if (dev->dma_parms && dev->dma_parms->max_segment_size)
621 return dev->dma_parms->max_segment_size;
622 return SZ_64K;
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800623}
624
625static inline unsigned int dma_set_max_seg_size(struct device *dev,
626 unsigned int size)
627{
628 if (dev->dma_parms) {
629 dev->dma_parms->max_segment_size = size;
630 return 0;
Robin Murphy002edb62015-11-06 16:32:51 -0800631 }
632 return -EIO;
FUJITA Tomonori6b7b6512008-02-04 22:27:55 -0800633}
634
FUJITA Tomonorid22a6962008-02-04 22:28:13 -0800635static inline unsigned long dma_get_seg_boundary(struct device *dev)
636{
Robin Murphy002edb62015-11-06 16:32:51 -0800637 if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
638 return dev->dma_parms->segment_boundary_mask;
639 return DMA_BIT_MASK(32);
FUJITA Tomonorid22a6962008-02-04 22:28:13 -0800640}
641
642static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
643{
644 if (dev->dma_parms) {
645 dev->dma_parms->segment_boundary_mask = mask;
646 return 0;
Robin Murphy002edb62015-11-06 16:32:51 -0800647 }
648 return -EIO;
FUJITA Tomonorid22a6962008-02-04 22:28:13 -0800649}
650
Santosh Shilimkar00c8f162013-07-29 14:18:48 +0100651#ifndef dma_max_pfn
652static inline unsigned long dma_max_pfn(struct device *dev)
653{
654 return *dev->dma_mask >> PAGE_SHIFT;
655}
656#endif
657
Andrew Morton842fa692011-11-02 13:39:33 -0700658static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
659 dma_addr_t *dma_handle, gfp_t flag)
660{
Joe Perchesede23fa2013-08-26 22:45:23 -0700661 void *ret = dma_alloc_coherent(dev, size, dma_handle,
662 flag | __GFP_ZERO);
Andrew Morton842fa692011-11-02 13:39:33 -0700663 return ret;
664}
665
Heiko Carstense259f192010-08-13 09:39:18 +0200666#ifdef CONFIG_HAS_DMA
FUJITA Tomonori4565f012010-08-10 18:03:22 -0700667static inline int dma_get_cache_alignment(void)
668{
669#ifdef ARCH_DMA_MINALIGN
670 return ARCH_DMA_MINALIGN;
671#endif
672 return 1;
673}
Heiko Carstense259f192010-08-13 09:39:18 +0200674#endif
FUJITA Tomonori4565f012010-08-10 18:03:22 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/* flags for the coherent memory api */
677#define DMA_MEMORY_MAP 0x01
678#define DMA_MEMORY_IO 0x02
679#define DMA_MEMORY_INCLUDES_CHILDREN 0x04
680#define DMA_MEMORY_EXCLUSIVE 0x08
681
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800682#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
683int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
684 dma_addr_t device_addr, size_t size, int flags);
685void dma_release_declared_memory(struct device *dev);
686void *dma_mark_declared_memory_occupied(struct device *dev,
687 dma_addr_t device_addr, size_t size);
688#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static inline int
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600690dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 dma_addr_t device_addr, size_t size, int flags)
692{
693 return 0;
694}
695
696static inline void
697dma_release_declared_memory(struct device *dev)
698{
699}
700
701static inline void *
702dma_mark_declared_memory_occupied(struct device *dev,
703 dma_addr_t device_addr, size_t size)
704{
705 return ERR_PTR(-EBUSY);
706}
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800707#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Tejun Heo9ac78492007-01-20 16:00:26 +0900709/*
710 * Managed DMA API
711 */
712extern void *dmam_alloc_coherent(struct device *dev, size_t size,
713 dma_addr_t *dma_handle, gfp_t gfp);
714extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
715 dma_addr_t dma_handle);
716extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
717 dma_addr_t *dma_handle, gfp_t gfp);
718extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
719 dma_addr_t dma_handle);
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800720#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600721extern int dmam_declare_coherent_memory(struct device *dev,
722 phys_addr_t phys_addr,
Tejun Heo9ac78492007-01-20 16:00:26 +0900723 dma_addr_t device_addr, size_t size,
724 int flags);
725extern void dmam_release_declared_memory(struct device *dev);
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800726#else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
Tejun Heo9ac78492007-01-20 16:00:26 +0900727static inline int dmam_declare_coherent_memory(struct device *dev,
Bjorn Helgaas88a984b2014-05-20 16:54:22 -0600728 phys_addr_t phys_addr, dma_addr_t device_addr,
Tejun Heo9ac78492007-01-20 16:00:26 +0900729 size_t size, gfp_t gfp)
730{
731 return 0;
732}
733
734static inline void dmam_release_declared_memory(struct device *dev)
735{
736}
Christoph Hellwig20d666e2016-01-20 15:02:09 -0800737#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
Tejun Heo9ac78492007-01-20 16:00:26 +0900738
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800739static inline void *dma_alloc_wc(struct device *dev, size_t size,
740 dma_addr_t *dma_addr, gfp_t gfp)
Thierry Redingb4bbb102014-06-27 11:56:58 +0200741{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700742 return dma_alloc_attrs(dev, size, dma_addr, gfp,
743 DMA_ATTR_WRITE_COMBINE);
Thierry Redingb4bbb102014-06-27 11:56:58 +0200744}
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800745#ifndef dma_alloc_writecombine
746#define dma_alloc_writecombine dma_alloc_wc
747#endif
Thierry Redingb4bbb102014-06-27 11:56:58 +0200748
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800749static inline void dma_free_wc(struct device *dev, size_t size,
750 void *cpu_addr, dma_addr_t dma_addr)
Thierry Redingb4bbb102014-06-27 11:56:58 +0200751{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700752 return dma_free_attrs(dev, size, cpu_addr, dma_addr,
753 DMA_ATTR_WRITE_COMBINE);
Thierry Redingb4bbb102014-06-27 11:56:58 +0200754}
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800755#ifndef dma_free_writecombine
756#define dma_free_writecombine dma_free_wc
757#endif
Thierry Redingb4bbb102014-06-27 11:56:58 +0200758
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800759static inline int dma_mmap_wc(struct device *dev,
760 struct vm_area_struct *vma,
761 void *cpu_addr, dma_addr_t dma_addr,
762 size_t size)
Thierry Redingb4bbb102014-06-27 11:56:58 +0200763{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700764 return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
765 DMA_ATTR_WRITE_COMBINE);
Thierry Redingb4bbb102014-06-27 11:56:58 +0200766}
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800767#ifndef dma_mmap_writecombine
768#define dma_mmap_writecombine dma_mmap_wc
769#endif
Arthur Kepner74bc7ce2008-04-29 01:00:30 -0700770
FUJITA Tomonori0acedc12010-03-10 15:23:31 -0800771#ifdef CONFIG_NEED_DMA_MAP_STATE
772#define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
773#define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
774#define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
775#define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
776#define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
777#define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
778#else
779#define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
780#define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
781#define dma_unmap_addr(PTR, ADDR_NAME) (0)
782#define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
783#define dma_unmap_len(PTR, LEN_NAME) (0)
784#define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
785#endif
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#endif