GNU Linux-libre 4.14.328-gnu1
[releases.git] / include / linux / dma-mapping.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_DMA_MAPPING_H
3 #define _LINUX_DMA_MAPPING_H
4
5 #include <linux/sizes.h>
6 #include <linux/string.h>
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/dma-debug.h>
10 #include <linux/dma-direction.h>
11 #include <linux/scatterlist.h>
12 #include <linux/bug.h>
13 #include <linux/mem_encrypt.h>
14
15 /**
16  * List of possible attributes associated with a DMA mapping. The semantics
17  * of each attribute should be defined in Documentation/DMA-attributes.txt.
18  *
19  * DMA_ATTR_WRITE_BARRIER: DMA to a memory region with this attribute
20  * forces all pending DMA writes to complete.
21  */
22 #define DMA_ATTR_WRITE_BARRIER          (1UL << 0)
23 /*
24  * DMA_ATTR_WEAK_ORDERING: Specifies that reads and writes to the mapping
25  * may be weakly ordered, that is that reads and writes may pass each other.
26  */
27 #define DMA_ATTR_WEAK_ORDERING          (1UL << 1)
28 /*
29  * DMA_ATTR_WRITE_COMBINE: Specifies that writes to the mapping may be
30  * buffered to improve performance.
31  */
32 #define DMA_ATTR_WRITE_COMBINE          (1UL << 2)
33 /*
34  * DMA_ATTR_NON_CONSISTENT: Lets the platform to choose to return either
35  * consistent or non-consistent memory as it sees fit.
36  */
37 #define DMA_ATTR_NON_CONSISTENT         (1UL << 3)
38 /*
39  * DMA_ATTR_NO_KERNEL_MAPPING: Lets the platform to avoid creating a kernel
40  * virtual mapping for the allocated buffer.
41  */
42 #define DMA_ATTR_NO_KERNEL_MAPPING      (1UL << 4)
43 /*
44  * DMA_ATTR_SKIP_CPU_SYNC: Allows platform code to skip synchronization of
45  * the CPU cache for the given buffer assuming that it has been already
46  * transferred to 'device' domain.
47  */
48 #define DMA_ATTR_SKIP_CPU_SYNC          (1UL << 5)
49 /*
50  * DMA_ATTR_FORCE_CONTIGUOUS: Forces contiguous allocation of the buffer
51  * in physical memory.
52  */
53 #define DMA_ATTR_FORCE_CONTIGUOUS       (1UL << 6)
54 /*
55  * DMA_ATTR_ALLOC_SINGLE_PAGES: This is a hint to the DMA-mapping subsystem
56  * that it's probably not worth the time to try to allocate memory to in a way
57  * that gives better TLB efficiency.
58  */
59 #define DMA_ATTR_ALLOC_SINGLE_PAGES     (1UL << 7)
60 /*
61  * DMA_ATTR_NO_WARN: This tells the DMA-mapping subsystem to suppress
62  * allocation failure reports (similarly to __GFP_NOWARN).
63  */
64 #define DMA_ATTR_NO_WARN        (1UL << 8)
65
66 /*
67  * DMA_ATTR_PRIVILEGED: used to indicate that the buffer is fully
68  * accessible at an elevated privilege level (and ideally inaccessible or
69  * at least read-only at lesser-privileged levels).
70  */
71 #define DMA_ATTR_PRIVILEGED             (1UL << 9)
72
73 /*
74  * A dma_addr_t can hold any valid DMA or bus address for the platform.
75  * It can be given to a device to use as a DMA source or target.  A CPU cannot
76  * reference a dma_addr_t directly because there may be translation between
77  * its physical address space and the bus address space.
78  */
79 struct dma_map_ops {
80         void* (*alloc)(struct device *dev, size_t size,
81                                 dma_addr_t *dma_handle, gfp_t gfp,
82                                 unsigned long attrs);
83         void (*free)(struct device *dev, size_t size,
84                               void *vaddr, dma_addr_t dma_handle,
85                               unsigned long attrs);
86         int (*mmap)(struct device *, struct vm_area_struct *,
87                           void *, dma_addr_t, size_t,
88                           unsigned long attrs);
89
90         int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
91                            dma_addr_t, size_t, unsigned long attrs);
92
93         dma_addr_t (*map_page)(struct device *dev, struct page *page,
94                                unsigned long offset, size_t size,
95                                enum dma_data_direction dir,
96                                unsigned long attrs);
97         void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
98                            size_t size, enum dma_data_direction dir,
99                            unsigned long attrs);
100         /*
101          * map_sg returns 0 on error and a value > 0 on success.
102          * It should never return a value < 0.
103          */
104         int (*map_sg)(struct device *dev, struct scatterlist *sg,
105                       int nents, enum dma_data_direction dir,
106                       unsigned long attrs);
107         void (*unmap_sg)(struct device *dev,
108                          struct scatterlist *sg, int nents,
109                          enum dma_data_direction dir,
110                          unsigned long attrs);
111         dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
112                                size_t size, enum dma_data_direction dir,
113                                unsigned long attrs);
114         void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
115                            size_t size, enum dma_data_direction dir,
116                            unsigned long attrs);
117         void (*sync_single_for_cpu)(struct device *dev,
118                                     dma_addr_t dma_handle, size_t size,
119                                     enum dma_data_direction dir);
120         void (*sync_single_for_device)(struct device *dev,
121                                        dma_addr_t dma_handle, size_t size,
122                                        enum dma_data_direction dir);
123         void (*sync_sg_for_cpu)(struct device *dev,
124                                 struct scatterlist *sg, int nents,
125                                 enum dma_data_direction dir);
126         void (*sync_sg_for_device)(struct device *dev,
127                                    struct scatterlist *sg, int nents,
128                                    enum dma_data_direction dir);
129         int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
130         int (*dma_supported)(struct device *dev, u64 mask);
131 #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
132         u64 (*get_required_mask)(struct device *dev);
133 #endif
134         int is_phys;
135 };
136
137 extern const struct dma_map_ops dma_noop_ops;
138 extern const struct dma_map_ops dma_virt_ops;
139
140 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
141
142 #define DMA_MASK_NONE   0x0ULL
143
144 static inline int valid_dma_direction(int dma_direction)
145 {
146         return ((dma_direction == DMA_BIDIRECTIONAL) ||
147                 (dma_direction == DMA_TO_DEVICE) ||
148                 (dma_direction == DMA_FROM_DEVICE));
149 }
150
151 static inline int is_device_dma_capable(struct device *dev)
152 {
153         return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
154 }
155
156 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
157 /*
158  * These three functions are only for dma allocator.
159  * Don't use them in device drivers.
160  */
161 int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
162                                        dma_addr_t *dma_handle, void **ret);
163 int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
164
165 int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
166                             void *cpu_addr, size_t size, int *ret);
167
168 void *dma_alloc_from_global_coherent(ssize_t size, dma_addr_t *dma_handle);
169 int dma_release_from_global_coherent(int order, void *vaddr);
170 int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
171                                   size_t size, int *ret);
172
173 #else
174 #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0)
175 #define dma_release_from_dev_coherent(dev, order, vaddr) (0)
176 #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
177
178 static inline void *dma_alloc_from_global_coherent(ssize_t size,
179                                                    dma_addr_t *dma_handle)
180 {
181         return NULL;
182 }
183
184 static inline int dma_release_from_global_coherent(int order, void *vaddr)
185 {
186         return 0;
187 }
188
189 static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
190                                                 void *cpu_addr, size_t size,
191                                                 int *ret)
192 {
193         return 0;
194 }
195 #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
196
197 #ifdef CONFIG_HAS_DMA
198 #include <asm/dma-mapping.h>
199 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
200 {
201         if (dev && dev->dma_ops)
202                 return dev->dma_ops;
203         return get_arch_dma_ops(dev ? dev->bus : NULL);
204 }
205
206 static inline void set_dma_ops(struct device *dev,
207                                const struct dma_map_ops *dma_ops)
208 {
209         dev->dma_ops = dma_ops;
210 }
211 #else
212 /*
213  * Define the dma api to allow compilation but not linking of
214  * dma dependent code.  Code that depends on the dma-mapping
215  * API needs to set 'depends on HAS_DMA' in its Kconfig
216  */
217 extern const struct dma_map_ops bad_dma_ops;
218 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
219 {
220         return &bad_dma_ops;
221 }
222 #endif
223
224 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
225                                               size_t size,
226                                               enum dma_data_direction dir,
227                                               unsigned long attrs)
228 {
229         const struct dma_map_ops *ops = get_dma_ops(dev);
230         dma_addr_t addr;
231
232         BUG_ON(!valid_dma_direction(dir));
233         addr = ops->map_page(dev, virt_to_page(ptr),
234                              offset_in_page(ptr), size,
235                              dir, attrs);
236         debug_dma_map_page(dev, virt_to_page(ptr),
237                            offset_in_page(ptr), size,
238                            dir, addr, true);
239         return addr;
240 }
241
242 static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
243                                           size_t size,
244                                           enum dma_data_direction dir,
245                                           unsigned long attrs)
246 {
247         const struct dma_map_ops *ops = get_dma_ops(dev);
248
249         BUG_ON(!valid_dma_direction(dir));
250         if (ops->unmap_page)
251                 ops->unmap_page(dev, addr, size, dir, attrs);
252         debug_dma_unmap_page(dev, addr, size, dir, true);
253 }
254
255 /*
256  * dma_maps_sg_attrs returns 0 on error and > 0 on success.
257  * It should never return a value < 0.
258  */
259 static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
260                                    int nents, enum dma_data_direction dir,
261                                    unsigned long attrs)
262 {
263         const struct dma_map_ops *ops = get_dma_ops(dev);
264         int ents;
265
266         BUG_ON(!valid_dma_direction(dir));
267         ents = ops->map_sg(dev, sg, nents, dir, attrs);
268         BUG_ON(ents < 0);
269         debug_dma_map_sg(dev, sg, nents, ents, dir);
270
271         return ents;
272 }
273
274 static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
275                                       int nents, enum dma_data_direction dir,
276                                       unsigned long attrs)
277 {
278         const struct dma_map_ops *ops = get_dma_ops(dev);
279
280         BUG_ON(!valid_dma_direction(dir));
281         debug_dma_unmap_sg(dev, sg, nents, dir);
282         if (ops->unmap_sg)
283                 ops->unmap_sg(dev, sg, nents, dir, attrs);
284 }
285
286 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
287                                             struct page *page,
288                                             size_t offset, size_t size,
289                                             enum dma_data_direction dir,
290                                             unsigned long attrs)
291 {
292         const struct dma_map_ops *ops = get_dma_ops(dev);
293         dma_addr_t addr;
294
295         BUG_ON(!valid_dma_direction(dir));
296         addr = ops->map_page(dev, page, offset, size, dir, attrs);
297         debug_dma_map_page(dev, page, offset, size, dir, addr, false);
298
299         return addr;
300 }
301
302 static inline void dma_unmap_page_attrs(struct device *dev,
303                                         dma_addr_t addr, size_t size,
304                                         enum dma_data_direction dir,
305                                         unsigned long attrs)
306 {
307         const struct dma_map_ops *ops = get_dma_ops(dev);
308
309         BUG_ON(!valid_dma_direction(dir));
310         if (ops->unmap_page)
311                 ops->unmap_page(dev, addr, size, dir, attrs);
312         debug_dma_unmap_page(dev, addr, size, dir, false);
313 }
314
315 static inline dma_addr_t dma_map_resource(struct device *dev,
316                                           phys_addr_t phys_addr,
317                                           size_t size,
318                                           enum dma_data_direction dir,
319                                           unsigned long attrs)
320 {
321         const struct dma_map_ops *ops = get_dma_ops(dev);
322         dma_addr_t addr;
323
324         BUG_ON(!valid_dma_direction(dir));
325
326         /* Don't allow RAM to be mapped */
327         BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
328
329         addr = phys_addr;
330         if (ops->map_resource)
331                 addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
332
333         debug_dma_map_resource(dev, phys_addr, size, dir, addr);
334
335         return addr;
336 }
337
338 static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
339                                       size_t size, enum dma_data_direction dir,
340                                       unsigned long attrs)
341 {
342         const struct dma_map_ops *ops = get_dma_ops(dev);
343
344         BUG_ON(!valid_dma_direction(dir));
345         if (ops->unmap_resource)
346                 ops->unmap_resource(dev, addr, size, dir, attrs);
347         debug_dma_unmap_resource(dev, addr, size, dir);
348 }
349
350 static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
351                                            size_t size,
352                                            enum dma_data_direction dir)
353 {
354         const struct dma_map_ops *ops = get_dma_ops(dev);
355
356         BUG_ON(!valid_dma_direction(dir));
357         if (ops->sync_single_for_cpu)
358                 ops->sync_single_for_cpu(dev, addr, size, dir);
359         debug_dma_sync_single_for_cpu(dev, addr, size, dir);
360 }
361
362 static inline void dma_sync_single_for_device(struct device *dev,
363                                               dma_addr_t addr, size_t size,
364                                               enum dma_data_direction dir)
365 {
366         const struct dma_map_ops *ops = get_dma_ops(dev);
367
368         BUG_ON(!valid_dma_direction(dir));
369         if (ops->sync_single_for_device)
370                 ops->sync_single_for_device(dev, addr, size, dir);
371         debug_dma_sync_single_for_device(dev, addr, size, dir);
372 }
373
374 static inline void dma_sync_single_range_for_cpu(struct device *dev,
375                                                  dma_addr_t addr,
376                                                  unsigned long offset,
377                                                  size_t size,
378                                                  enum dma_data_direction dir)
379 {
380         const struct dma_map_ops *ops = get_dma_ops(dev);
381
382         BUG_ON(!valid_dma_direction(dir));
383         if (ops->sync_single_for_cpu)
384                 ops->sync_single_for_cpu(dev, addr + offset, size, dir);
385         debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
386 }
387
388 static inline void dma_sync_single_range_for_device(struct device *dev,
389                                                     dma_addr_t addr,
390                                                     unsigned long offset,
391                                                     size_t size,
392                                                     enum dma_data_direction dir)
393 {
394         const struct dma_map_ops *ops = get_dma_ops(dev);
395
396         BUG_ON(!valid_dma_direction(dir));
397         if (ops->sync_single_for_device)
398                 ops->sync_single_for_device(dev, addr + offset, size, dir);
399         debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
400 }
401
402 static inline void
403 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
404                     int nelems, enum dma_data_direction dir)
405 {
406         const struct dma_map_ops *ops = get_dma_ops(dev);
407
408         BUG_ON(!valid_dma_direction(dir));
409         if (ops->sync_sg_for_cpu)
410                 ops->sync_sg_for_cpu(dev, sg, nelems, dir);
411         debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
412 }
413
414 static inline void
415 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
416                        int nelems, enum dma_data_direction dir)
417 {
418         const struct dma_map_ops *ops = get_dma_ops(dev);
419
420         BUG_ON(!valid_dma_direction(dir));
421         if (ops->sync_sg_for_device)
422                 ops->sync_sg_for_device(dev, sg, nelems, dir);
423         debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
424
425 }
426
427 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
428 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
429 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
430 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
431 #define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
432 #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
433
434 extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
435                            void *cpu_addr, dma_addr_t dma_addr, size_t size);
436
437 void *dma_common_contiguous_remap(struct page *page, size_t size,
438                         unsigned long vm_flags,
439                         pgprot_t prot, const void *caller);
440
441 void *dma_common_pages_remap(struct page **pages, size_t size,
442                         unsigned long vm_flags, pgprot_t prot,
443                         const void *caller);
444 void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
445
446 /**
447  * dma_mmap_attrs - map a coherent DMA allocation into user space
448  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
449  * @vma: vm_area_struct describing requested user mapping
450  * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
451  * @handle: device-view address returned from dma_alloc_attrs
452  * @size: size of memory originally requested in dma_alloc_attrs
453  * @attrs: attributes of mapping properties requested in dma_alloc_attrs
454  *
455  * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
456  * into user space.  The coherent DMA buffer must not be freed by the
457  * driver until the user space mapping has been released.
458  */
459 static inline int
460 dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
461                dma_addr_t dma_addr, size_t size, unsigned long attrs)
462 {
463         const struct dma_map_ops *ops = get_dma_ops(dev);
464         BUG_ON(!ops);
465         if (ops->mmap)
466                 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
467         return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
468 }
469
470 #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
471
472 int
473 dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
474                        void *cpu_addr, dma_addr_t dma_addr, size_t size);
475
476 static inline int
477 dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
478                       dma_addr_t dma_addr, size_t size,
479                       unsigned long attrs)
480 {
481         const struct dma_map_ops *ops = get_dma_ops(dev);
482         BUG_ON(!ops);
483         if (ops->get_sgtable)
484                 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
485                                         attrs);
486         return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
487 }
488
489 #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
490
491 #ifndef arch_dma_alloc_attrs
492 #define arch_dma_alloc_attrs(dev, flag) (true)
493 #endif
494
495 static inline void *dma_alloc_attrs(struct device *dev, size_t size,
496                                        dma_addr_t *dma_handle, gfp_t flag,
497                                        unsigned long attrs)
498 {
499         const struct dma_map_ops *ops = get_dma_ops(dev);
500         void *cpu_addr;
501
502         BUG_ON(!ops);
503
504         if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
505                 return cpu_addr;
506
507         if (!arch_dma_alloc_attrs(&dev, &flag))
508                 return NULL;
509         if (!ops->alloc)
510                 return NULL;
511
512         cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
513         debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
514         return cpu_addr;
515 }
516
517 static inline void dma_free_attrs(struct device *dev, size_t size,
518                                      void *cpu_addr, dma_addr_t dma_handle,
519                                      unsigned long attrs)
520 {
521         const struct dma_map_ops *ops = get_dma_ops(dev);
522
523         BUG_ON(!ops);
524         WARN_ON(irqs_disabled());
525
526         if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
527                 return;
528
529         if (!ops->free || !cpu_addr)
530                 return;
531
532         debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
533         ops->free(dev, size, cpu_addr, dma_handle, attrs);
534 }
535
536 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
537                 dma_addr_t *dma_handle, gfp_t flag)
538 {
539         return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
540 }
541
542 static inline void dma_free_coherent(struct device *dev, size_t size,
543                 void *cpu_addr, dma_addr_t dma_handle)
544 {
545         return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
546 }
547
548 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
549 {
550         const struct dma_map_ops *ops = get_dma_ops(dev);
551
552         debug_dma_mapping_error(dev, dma_addr);
553         if (ops->mapping_error)
554                 return ops->mapping_error(dev, dma_addr);
555         return 0;
556 }
557
558 static inline void dma_check_mask(struct device *dev, u64 mask)
559 {
560         if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1)))
561                 dev_warn(dev, "SME is active, device will require DMA bounce buffers\n");
562 }
563
564 static inline int dma_supported(struct device *dev, u64 mask)
565 {
566         const struct dma_map_ops *ops = get_dma_ops(dev);
567
568         if (!ops)
569                 return 0;
570         if (!ops->dma_supported)
571                 return 1;
572         return ops->dma_supported(dev, mask);
573 }
574
575 #ifndef HAVE_ARCH_DMA_SET_MASK
576 static inline int dma_set_mask(struct device *dev, u64 mask)
577 {
578         if (!dev->dma_mask || !dma_supported(dev, mask))
579                 return -EIO;
580
581         dma_check_mask(dev, mask);
582
583         *dev->dma_mask = mask;
584         return 0;
585 }
586 #endif
587
588 static inline u64 dma_get_mask(struct device *dev)
589 {
590         if (dev && dev->dma_mask && *dev->dma_mask)
591                 return *dev->dma_mask;
592         return DMA_BIT_MASK(32);
593 }
594
595 #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
596 int dma_set_coherent_mask(struct device *dev, u64 mask);
597 #else
598 static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
599 {
600         if (!dma_supported(dev, mask))
601                 return -EIO;
602
603         dma_check_mask(dev, mask);
604
605         dev->coherent_dma_mask = mask;
606         return 0;
607 }
608 #endif
609
610 /*
611  * Set both the DMA mask and the coherent DMA mask to the same thing.
612  * Note that we don't check the return value from dma_set_coherent_mask()
613  * as the DMA API guarantees that the coherent DMA mask can be set to
614  * the same or smaller than the streaming DMA mask.
615  */
616 static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
617 {
618         int rc = dma_set_mask(dev, mask);
619         if (rc == 0)
620                 dma_set_coherent_mask(dev, mask);
621         return rc;
622 }
623
624 /*
625  * Similar to the above, except it deals with the case where the device
626  * does not have dev->dma_mask appropriately setup.
627  */
628 static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
629 {
630         dev->dma_mask = &dev->coherent_dma_mask;
631         return dma_set_mask_and_coherent(dev, mask);
632 }
633
634 extern u64 dma_get_required_mask(struct device *dev);
635
636 #ifndef arch_setup_dma_ops
637 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
638                                       u64 size, const struct iommu_ops *iommu,
639                                       bool coherent) { }
640 #endif
641
642 #ifndef arch_teardown_dma_ops
643 static inline void arch_teardown_dma_ops(struct device *dev) { }
644 #endif
645
646 static inline unsigned int dma_get_max_seg_size(struct device *dev)
647 {
648         if (dev->dma_parms && dev->dma_parms->max_segment_size)
649                 return dev->dma_parms->max_segment_size;
650         return SZ_64K;
651 }
652
653 static inline int dma_set_max_seg_size(struct device *dev, unsigned int size)
654 {
655         if (dev->dma_parms) {
656                 dev->dma_parms->max_segment_size = size;
657                 return 0;
658         }
659         return -EIO;
660 }
661
662 static inline unsigned long dma_get_seg_boundary(struct device *dev)
663 {
664         if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
665                 return dev->dma_parms->segment_boundary_mask;
666         return DMA_BIT_MASK(32);
667 }
668
669 static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
670 {
671         if (dev->dma_parms) {
672                 dev->dma_parms->segment_boundary_mask = mask;
673                 return 0;
674         }
675         return -EIO;
676 }
677
678 #ifndef dma_max_pfn
679 static inline unsigned long dma_max_pfn(struct device *dev)
680 {
681         return *dev->dma_mask >> PAGE_SHIFT;
682 }
683 #endif
684
685 static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
686                                         dma_addr_t *dma_handle, gfp_t flag)
687 {
688         void *ret = dma_alloc_coherent(dev, size, dma_handle,
689                                        flag | __GFP_ZERO);
690         return ret;
691 }
692
693 static inline int dma_get_cache_alignment(void)
694 {
695 #ifdef ARCH_DMA_MINALIGN
696         return ARCH_DMA_MINALIGN;
697 #endif
698         return 1;
699 }
700
701 /* flags for the coherent memory api */
702 #define DMA_MEMORY_EXCLUSIVE            0x01
703
704 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
705 int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
706                                 dma_addr_t device_addr, size_t size, int flags);
707 void dma_release_declared_memory(struct device *dev);
708 void *dma_mark_declared_memory_occupied(struct device *dev,
709                                         dma_addr_t device_addr, size_t size);
710 #else
711 static inline int
712 dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
713                             dma_addr_t device_addr, size_t size, int flags)
714 {
715         return -ENOSYS;
716 }
717
718 static inline void
719 dma_release_declared_memory(struct device *dev)
720 {
721 }
722
723 static inline void *
724 dma_mark_declared_memory_occupied(struct device *dev,
725                                   dma_addr_t device_addr, size_t size)
726 {
727         return ERR_PTR(-EBUSY);
728 }
729 #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
730
731 #ifdef CONFIG_HAS_DMA
732 int dma_configure(struct device *dev);
733 void dma_deconfigure(struct device *dev);
734 #else
735 static inline int dma_configure(struct device *dev)
736 {
737         return 0;
738 }
739
740 static inline void dma_deconfigure(struct device *dev) {}
741 #endif
742
743 /*
744  * Managed DMA API
745  */
746 extern void *dmam_alloc_coherent(struct device *dev, size_t size,
747                                  dma_addr_t *dma_handle, gfp_t gfp);
748 extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
749                                dma_addr_t dma_handle);
750 extern void *dmam_alloc_attrs(struct device *dev, size_t size,
751                               dma_addr_t *dma_handle, gfp_t gfp,
752                               unsigned long attrs);
753 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
754 extern int dmam_declare_coherent_memory(struct device *dev,
755                                         phys_addr_t phys_addr,
756                                         dma_addr_t device_addr, size_t size,
757                                         int flags);
758 extern void dmam_release_declared_memory(struct device *dev);
759 #else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
760 static inline int dmam_declare_coherent_memory(struct device *dev,
761                                 phys_addr_t phys_addr, dma_addr_t device_addr,
762                                 size_t size, gfp_t gfp)
763 {
764         return 0;
765 }
766
767 static inline void dmam_release_declared_memory(struct device *dev)
768 {
769 }
770 #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
771
772 static inline void *dma_alloc_wc(struct device *dev, size_t size,
773                                  dma_addr_t *dma_addr, gfp_t gfp)
774 {
775         return dma_alloc_attrs(dev, size, dma_addr, gfp,
776                                DMA_ATTR_WRITE_COMBINE);
777 }
778 #ifndef dma_alloc_writecombine
779 #define dma_alloc_writecombine dma_alloc_wc
780 #endif
781
782 static inline void dma_free_wc(struct device *dev, size_t size,
783                                void *cpu_addr, dma_addr_t dma_addr)
784 {
785         return dma_free_attrs(dev, size, cpu_addr, dma_addr,
786                               DMA_ATTR_WRITE_COMBINE);
787 }
788 #ifndef dma_free_writecombine
789 #define dma_free_writecombine dma_free_wc
790 #endif
791
792 static inline int dma_mmap_wc(struct device *dev,
793                               struct vm_area_struct *vma,
794                               void *cpu_addr, dma_addr_t dma_addr,
795                               size_t size)
796 {
797         return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
798                               DMA_ATTR_WRITE_COMBINE);
799 }
800 #ifndef dma_mmap_writecombine
801 #define dma_mmap_writecombine dma_mmap_wc
802 #endif
803
804 #if defined(CONFIG_NEED_DMA_MAP_STATE) || defined(CONFIG_DMA_API_DEBUG)
805 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)        dma_addr_t ADDR_NAME
806 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)          __u32 LEN_NAME
807 #define dma_unmap_addr(PTR, ADDR_NAME)           ((PTR)->ADDR_NAME)
808 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL)  (((PTR)->ADDR_NAME) = (VAL))
809 #define dma_unmap_len(PTR, LEN_NAME)             ((PTR)->LEN_NAME)
810 #define dma_unmap_len_set(PTR, LEN_NAME, VAL)    (((PTR)->LEN_NAME) = (VAL))
811 #else
812 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
813 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
814 #define dma_unmap_addr(PTR, ADDR_NAME)           (0)
815 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL)  do { } while (0)
816 #define dma_unmap_len(PTR, LEN_NAME)             (0)
817 #define dma_unmap_len_set(PTR, LEN_NAME, VAL)    do { } while (0)
818 #endif
819
820 #endif