1
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-direction.h>
10#include <linux/scatterlist.h>
11#include <linux/bug.h>
12#include <linux/mem_encrypt.h>
13
14
15
16
17
18
19
20
21
22
23#define DMA_ATTR_WEAK_ORDERING (1UL << 1)
24
25
26
27
28#define DMA_ATTR_WRITE_COMBINE (1UL << 2)
29
30
31
32
33#define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4)
34
35
36
37
38
39#define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5)
40
41
42
43
44#define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6)
45
46
47
48
49
50#define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7)
51
52
53
54
55#define DMA_ATTR_NO_WARN (1UL << 8)
56
57
58
59
60
61
62#define DMA_ATTR_PRIVILEGED (1UL << 9)
63
64
65
66
67
68
69
70
71
72
73
74#define DMA_MAPPING_ERROR (~(dma_addr_t)0)
75
76#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
77
78#ifdef CONFIG_DMA_API_DEBUG
79void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);
80void debug_dma_map_single(struct device *dev, const void *addr,
81 unsigned long len);
82#else
83static inline void debug_dma_mapping_error(struct device *dev,
84 dma_addr_t dma_addr)
85{
86}
87static inline void debug_dma_map_single(struct device *dev, const void *addr,
88 unsigned long len)
89{
90}
91#endif
92
93#ifdef CONFIG_HAS_DMA
94static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
95{
96 debug_dma_mapping_error(dev, dma_addr);
97
98 if (unlikely(dma_addr == DMA_MAPPING_ERROR))
99 return -ENOMEM;
100 return 0;
101}
102
103dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
104 size_t offset, size_t size, enum dma_data_direction dir,
105 unsigned long attrs);
106void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
107 enum dma_data_direction dir, unsigned long attrs);
108int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
109 enum dma_data_direction dir, unsigned long attrs);
110void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
111 int nents, enum dma_data_direction dir,
112 unsigned long attrs);
113dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
114 size_t size, enum dma_data_direction dir, unsigned long attrs);
115void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
116 enum dma_data_direction dir, unsigned long attrs);
117void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
118 enum dma_data_direction dir);
119void dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
120 size_t size, enum dma_data_direction dir);
121void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
122 int nelems, enum dma_data_direction dir);
123void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
124 int nelems, enum dma_data_direction dir);
125void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
126 gfp_t flag, unsigned long attrs);
127void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
128 dma_addr_t dma_handle, unsigned long attrs);
129void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
130 gfp_t gfp, unsigned long attrs);
131void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
132 dma_addr_t dma_handle);
133int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
134 void *cpu_addr, dma_addr_t dma_addr, size_t size,
135 unsigned long attrs);
136int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
137 void *cpu_addr, dma_addr_t dma_addr, size_t size,
138 unsigned long attrs);
139bool dma_can_mmap(struct device *dev);
140int dma_supported(struct device *dev, u64 mask);
141int dma_set_mask(struct device *dev, u64 mask);
142int dma_set_coherent_mask(struct device *dev, u64 mask);
143u64 dma_get_required_mask(struct device *dev);
144size_t dma_max_mapping_size(struct device *dev);
145bool dma_need_sync(struct device *dev, dma_addr_t dma_addr);
146unsigned long dma_get_merge_boundary(struct device *dev);
147struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size,
148 enum dma_data_direction dir, gfp_t gfp, unsigned long attrs);
149void dma_free_noncontiguous(struct device *dev, size_t size,
150 struct sg_table *sgt, enum dma_data_direction dir);
151void *dma_vmap_noncontiguous(struct device *dev, size_t size,
152 struct sg_table *sgt);
153void dma_vunmap_noncontiguous(struct device *dev, void *vaddr);
154int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma,
155 size_t size, struct sg_table *sgt);
156#else
157static inline dma_addr_t dma_map_page_attrs(struct device *dev,
158 struct page *page, size_t offset, size_t size,
159 enum dma_data_direction dir, unsigned long attrs)
160{
161 return DMA_MAPPING_ERROR;
162}
163static inline void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr,
164 size_t size, enum dma_data_direction dir, unsigned long attrs)
165{
166}
167static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
168 int nents, enum dma_data_direction dir, unsigned long attrs)
169{
170 return 0;
171}
172static inline void dma_unmap_sg_attrs(struct device *dev,
173 struct scatterlist *sg, int nents, enum dma_data_direction dir,
174 unsigned long attrs)
175{
176}
177static inline dma_addr_t dma_map_resource(struct device *dev,
178 phys_addr_t phys_addr, size_t size, enum dma_data_direction dir,
179 unsigned long attrs)
180{
181 return DMA_MAPPING_ERROR;
182}
183static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
184 size_t size, enum dma_data_direction dir, unsigned long attrs)
185{
186}
187static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
188 size_t size, enum dma_data_direction dir)
189{
190}
191static inline void dma_sync_single_for_device(struct device *dev,
192 dma_addr_t addr, size_t size, enum dma_data_direction dir)
193{
194}
195static inline void dma_sync_sg_for_cpu(struct device *dev,
196 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
197{
198}
199static inline void dma_sync_sg_for_device(struct device *dev,
200 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
201{
202}
203static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
204{
205 return -ENOMEM;
206}
207static inline void *dma_alloc_attrs(struct device *dev, size_t size,
208 dma_addr_t *dma_handle, gfp_t flag, unsigned long attrs)
209{
210 return NULL;
211}
212static void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
213 dma_addr_t dma_handle, unsigned long attrs)
214{
215}
216static inline void *dmam_alloc_attrs(struct device *dev, size_t size,
217 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
218{
219 return NULL;
220}
221static inline void dmam_free_coherent(struct device *dev, size_t size,
222 void *vaddr, dma_addr_t dma_handle)
223{
224}
225static inline int dma_get_sgtable_attrs(struct device *dev,
226 struct sg_table *sgt, void *cpu_addr, dma_addr_t dma_addr,
227 size_t size, unsigned long attrs)
228{
229 return -ENXIO;
230}
231static inline int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
232 void *cpu_addr, dma_addr_t dma_addr, size_t size,
233 unsigned long attrs)
234{
235 return -ENXIO;
236}
237static inline bool dma_can_mmap(struct device *dev)
238{
239 return false;
240}
241static inline int dma_supported(struct device *dev, u64 mask)
242{
243 return 0;
244}
245static inline int dma_set_mask(struct device *dev, u64 mask)
246{
247 return -EIO;
248}
249static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
250{
251 return -EIO;
252}
253static inline u64 dma_get_required_mask(struct device *dev)
254{
255 return 0;
256}
257static inline size_t dma_max_mapping_size(struct device *dev)
258{
259 return 0;
260}
261static inline bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
262{
263 return false;
264}
265static inline unsigned long dma_get_merge_boundary(struct device *dev)
266{
267 return 0;
268}
269static inline struct sg_table *dma_alloc_noncontiguous(struct device *dev,
270 size_t size, enum dma_data_direction dir, gfp_t gfp,
271 unsigned long attrs)
272{
273 return NULL;
274}
275static inline void dma_free_noncontiguous(struct device *dev, size_t size,
276 struct sg_table *sgt, enum dma_data_direction dir)
277{
278}
279static inline void *dma_vmap_noncontiguous(struct device *dev, size_t size,
280 struct sg_table *sgt)
281{
282 return NULL;
283}
284static inline void dma_vunmap_noncontiguous(struct device *dev, void *vaddr)
285{
286}
287static inline int dma_mmap_noncontiguous(struct device *dev,
288 struct vm_area_struct *vma, size_t size, struct sg_table *sgt)
289{
290 return -EINVAL;
291}
292#endif
293
294struct page *dma_alloc_pages(struct device *dev, size_t size,
295 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
296void dma_free_pages(struct device *dev, size_t size, struct page *page,
297 dma_addr_t dma_handle, enum dma_data_direction dir);
298int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma,
299 size_t size, struct page *page);
300
301static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
302 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
303{
304 struct page *page = dma_alloc_pages(dev, size, dma_handle, dir, gfp);
305 return page ? page_address(page) : NULL;
306}
307
308static inline void dma_free_noncoherent(struct device *dev, size_t size,
309 void *vaddr, dma_addr_t dma_handle, enum dma_data_direction dir)
310{
311 dma_free_pages(dev, size, virt_to_page(vaddr), dma_handle, dir);
312}
313
314static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
315 size_t size, enum dma_data_direction dir, unsigned long attrs)
316{
317
318 if (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
319 "rejecting DMA map of vmalloc memory\n"))
320 return DMA_MAPPING_ERROR;
321 debug_dma_map_single(dev, ptr, size);
322 return dma_map_page_attrs(dev, virt_to_page(ptr), offset_in_page(ptr),
323 size, dir, attrs);
324}
325
326static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
327 size_t size, enum dma_data_direction dir, unsigned long attrs)
328{
329 return dma_unmap_page_attrs(dev, addr, size, dir, attrs);
330}
331
332static inline void dma_sync_single_range_for_cpu(struct device *dev,
333 dma_addr_t addr, unsigned long offset, size_t size,
334 enum dma_data_direction dir)
335{
336 return dma_sync_single_for_cpu(dev, addr + offset, size, dir);
337}
338
339static inline void dma_sync_single_range_for_device(struct device *dev,
340 dma_addr_t addr, unsigned long offset, size_t size,
341 enum dma_data_direction dir)
342{
343 return dma_sync_single_for_device(dev, addr + offset, size, dir);
344}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362static inline int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
363 enum dma_data_direction dir, unsigned long attrs)
364{
365 int nents;
366
367 nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
368 if (nents <= 0)
369 return -EINVAL;
370 sgt->nents = nents;
371 return 0;
372}
373
374
375
376
377
378
379
380
381
382
383
384
385static inline void dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
386 enum dma_data_direction dir, unsigned long attrs)
387{
388 dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
389}
390
391
392
393
394
395
396
397
398
399
400
401
402
403static inline void dma_sync_sgtable_for_cpu(struct device *dev,
404 struct sg_table *sgt, enum dma_data_direction dir)
405{
406 dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir);
407}
408
409
410
411
412
413
414
415
416
417
418
419
420static inline void dma_sync_sgtable_for_device(struct device *dev,
421 struct sg_table *sgt, enum dma_data_direction dir)
422{
423 dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir);
424}
425
426#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
427#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
428#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
429#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
430#define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
431#define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
432#define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
433#define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
434
435static inline void *dma_alloc_coherent(struct device *dev, size_t size,
436 dma_addr_t *dma_handle, gfp_t gfp)
437{
438 return dma_alloc_attrs(dev, size, dma_handle, gfp,
439 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
440}
441
442static inline void dma_free_coherent(struct device *dev, size_t size,
443 void *cpu_addr, dma_addr_t dma_handle)
444{
445 return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
446}
447
448
449static inline u64 dma_get_mask(struct device *dev)
450{
451 if (dev->dma_mask && *dev->dma_mask)
452 return *dev->dma_mask;
453 return DMA_BIT_MASK(32);
454}
455
456
457
458
459
460
461
462static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
463{
464 int rc = dma_set_mask(dev, mask);
465 if (rc == 0)
466 dma_set_coherent_mask(dev, mask);
467 return rc;
468}
469
470
471
472
473
474static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
475{
476 dev->dma_mask = &dev->coherent_dma_mask;
477 return dma_set_mask_and_coherent(dev, mask);
478}
479
480
481
482
483
484
485
486
487
488static inline bool dma_addressing_limited(struct device *dev)
489{
490 return min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) <
491 dma_get_required_mask(dev);
492}
493
494static inline unsigned int dma_get_max_seg_size(struct device *dev)
495{
496 if (dev->dma_parms && dev->dma_parms->max_segment_size)
497 return dev->dma_parms->max_segment_size;
498 return SZ_64K;
499}
500
501static inline int dma_set_max_seg_size(struct device *dev, unsigned int size)
502{
503 if (dev->dma_parms) {
504 dev->dma_parms->max_segment_size = size;
505 return 0;
506 }
507 return -EIO;
508}
509
510static inline unsigned long dma_get_seg_boundary(struct device *dev)
511{
512 if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
513 return dev->dma_parms->segment_boundary_mask;
514 return ULONG_MAX;
515}
516
517
518
519
520
521
522
523
524
525
526
527
528static inline unsigned long dma_get_seg_boundary_nr_pages(struct device *dev,
529 unsigned int page_shift)
530{
531 if (!dev)
532 return (U32_MAX >> page_shift) + 1;
533 return (dma_get_seg_boundary(dev) >> page_shift) + 1;
534}
535
536static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
537{
538 if (dev->dma_parms) {
539 dev->dma_parms->segment_boundary_mask = mask;
540 return 0;
541 }
542 return -EIO;
543}
544
545static inline unsigned int dma_get_min_align_mask(struct device *dev)
546{
547 if (dev->dma_parms)
548 return dev->dma_parms->min_align_mask;
549 return 0;
550}
551
552static inline int dma_set_min_align_mask(struct device *dev,
553 unsigned int min_align_mask)
554{
555 if (WARN_ON_ONCE(!dev->dma_parms))
556 return -EIO;
557 dev->dma_parms->min_align_mask = min_align_mask;
558 return 0;
559}
560
561static inline int dma_get_cache_alignment(void)
562{
563#ifdef ARCH_DMA_MINALIGN
564 return ARCH_DMA_MINALIGN;
565#endif
566 return 1;
567}
568
569static inline void *dmam_alloc_coherent(struct device *dev, size_t size,
570 dma_addr_t *dma_handle, gfp_t gfp)
571{
572 return dmam_alloc_attrs(dev, size, dma_handle, gfp,
573 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
574}
575
576static inline void *dma_alloc_wc(struct device *dev, size_t size,
577 dma_addr_t *dma_addr, gfp_t gfp)
578{
579 unsigned long attrs = DMA_ATTR_WRITE_COMBINE;
580
581 if (gfp & __GFP_NOWARN)
582 attrs |= DMA_ATTR_NO_WARN;
583
584 return dma_alloc_attrs(dev, size, dma_addr, gfp, attrs);
585}
586
587static inline void dma_free_wc(struct device *dev, size_t size,
588 void *cpu_addr, dma_addr_t dma_addr)
589{
590 return dma_free_attrs(dev, size, cpu_addr, dma_addr,
591 DMA_ATTR_WRITE_COMBINE);
592}
593
594static inline int dma_mmap_wc(struct device *dev,
595 struct vm_area_struct *vma,
596 void *cpu_addr, dma_addr_t dma_addr,
597 size_t size)
598{
599 return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
600 DMA_ATTR_WRITE_COMBINE);
601}
602
603#ifdef CONFIG_NEED_DMA_MAP_STATE
604#define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
605#define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
606#define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
607#define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
608#define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
609#define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
610#else
611#define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
612#define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
613#define dma_unmap_addr(PTR, ADDR_NAME) (0)
614#define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
615#define dma_unmap_len(PTR, LEN_NAME) (0)
616#define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
617#endif
618
619#endif
620