1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_DMA_MAPPING_H
3 #define _LINUX_DMA_MAPPING_H
4
5 #include <linux/cache.h>
6 #include <linux/sizes.h>
7 #include <linux/string.h>
8 #include <linux/device.h>
9 #include <linux/err.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/core-api/dma-attributes.rst.
18 */
19
20 /*
21 * DMA_ATTR_WEAK_ORDERING: Specifies that reads and writes to the mapping
22 * may be weakly ordered, that is that reads and writes may pass each other.
23 */
24 #define DMA_ATTR_WEAK_ORDERING (1UL << 1)
25 /*
26 * DMA_ATTR_WRITE_COMBINE: Specifies that writes to the mapping may be
27 * buffered to improve performance.
28 */
29 #define DMA_ATTR_WRITE_COMBINE (1UL << 2)
30 /*
31 * DMA_ATTR_NO_KERNEL_MAPPING: Lets the platform to avoid creating a kernel
32 * virtual mapping for the allocated buffer.
33 */
34 #define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4)
35 /*
36 * DMA_ATTR_SKIP_CPU_SYNC: Allows platform code to skip synchronization of
37 * the CPU cache for the given buffer assuming that it has been already
38 * transferred to 'device' domain.
39 */
40 #define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5)
41 /*
42 * DMA_ATTR_FORCE_CONTIGUOUS: Forces contiguous allocation of the buffer
43 * in physical memory.
44 */
45 #define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6)
46 /*
47 * DMA_ATTR_ALLOC_SINGLE_PAGES: This is a hint to the DMA-mapping subsystem
48 * that it's probably not worth the time to try to allocate memory to in a way
49 * that gives better TLB efficiency.
50 */
51 #define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7)
52 /*
53 * DMA_ATTR_NO_WARN: This tells the DMA-mapping subsystem to suppress
54 * allocation failure reports (similarly to __GFP_NOWARN).
55 */
56 #define DMA_ATTR_NO_WARN (1UL << 8)
57
58 /*
59 * DMA_ATTR_PRIVILEGED: used to indicate that the buffer is fully
60 * accessible at an elevated privilege level (and ideally inaccessible or
61 * at least read-only at lesser-privileged levels).
62 */
63 #define DMA_ATTR_PRIVILEGED (1UL << 9)
64
65 /*
66 * A dma_addr_t can hold any valid DMA or bus address for the platform. It can
67 * be given to a device to use as a DMA source or target. It is specific to a
68 * given device and there may be a translation between the CPU physical address
69 * space and the bus address space.
70 *
71 * DMA_MAPPING_ERROR is the magic error code if a mapping failed. It should not
72 * be used directly in drivers, but checked for using dma_mapping_error()
73 * instead.
74 */
75 #define DMA_MAPPING_ERROR (~(dma_addr_t)0)
76
77 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
78
79 struct dma_iova_state {
80 dma_addr_t addr;
81 u64 __size;
82 };
83
84 /*
85 * Use the high bit to mark if we used swiotlb for one or more ranges.
86 */
87 #define DMA_IOVA_USE_SWIOTLB (1ULL << 63)
88
dma_iova_size(struct dma_iova_state * state)89 static inline size_t dma_iova_size(struct dma_iova_state *state)
90 {
91 /* Casting is needed for 32-bits systems */
92 return (size_t)(state->__size & ~DMA_IOVA_USE_SWIOTLB);
93 }
94
95 #ifdef CONFIG_DMA_API_DEBUG
96 void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);
97 void debug_dma_map_single(struct device *dev, const void *addr,
98 unsigned long len);
99 #else
debug_dma_mapping_error(struct device * dev,dma_addr_t dma_addr)100 static inline void debug_dma_mapping_error(struct device *dev,
101 dma_addr_t dma_addr)
102 {
103 }
debug_dma_map_single(struct device * dev,const void * addr,unsigned long len)104 static inline void debug_dma_map_single(struct device *dev, const void *addr,
105 unsigned long len)
106 {
107 }
108 #endif /* CONFIG_DMA_API_DEBUG */
109
110 #ifdef CONFIG_HAS_DMA
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)111 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
112 {
113 debug_dma_mapping_error(dev, dma_addr);
114
115 if (unlikely(dma_addr == DMA_MAPPING_ERROR))
116 return -ENOMEM;
117 return 0;
118 }
119
120 dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
121 size_t offset, size_t size, enum dma_data_direction dir,
122 unsigned long attrs);
123 void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
124 enum dma_data_direction dir, unsigned long attrs);
125 unsigned int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
126 int nents, enum dma_data_direction dir, unsigned long attrs);
127 void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
128 int nents, enum dma_data_direction dir,
129 unsigned long attrs);
130 int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
131 enum dma_data_direction dir, unsigned long attrs);
132 dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
133 size_t size, enum dma_data_direction dir, unsigned long attrs);
134 void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
135 enum dma_data_direction dir, unsigned long attrs);
136 void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
137 gfp_t flag, unsigned long attrs);
138 void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
139 dma_addr_t dma_handle, unsigned long attrs);
140 void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
141 gfp_t gfp, unsigned long attrs);
142 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
143 dma_addr_t dma_handle);
144 int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
145 void *cpu_addr, dma_addr_t dma_addr, size_t size,
146 unsigned long attrs);
147 int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
148 void *cpu_addr, dma_addr_t dma_addr, size_t size,
149 unsigned long attrs);
150 bool dma_can_mmap(struct device *dev);
151 bool dma_pci_p2pdma_supported(struct device *dev);
152 int dma_set_mask(struct device *dev, u64 mask);
153 int dma_set_coherent_mask(struct device *dev, u64 mask);
154 u64 dma_get_required_mask(struct device *dev);
155 bool dma_addressing_limited(struct device *dev);
156 size_t dma_max_mapping_size(struct device *dev);
157 size_t dma_opt_mapping_size(struct device *dev);
158 unsigned long dma_get_merge_boundary(struct device *dev);
159 struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size,
160 enum dma_data_direction dir, gfp_t gfp, unsigned long attrs);
161 void dma_free_noncontiguous(struct device *dev, size_t size,
162 struct sg_table *sgt, enum dma_data_direction dir);
163 void *dma_vmap_noncontiguous(struct device *dev, size_t size,
164 struct sg_table *sgt);
165 void dma_vunmap_noncontiguous(struct device *dev, void *vaddr);
166 int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma,
167 size_t size, struct sg_table *sgt);
168 #else /* CONFIG_HAS_DMA */
dma_map_page_attrs(struct device * dev,struct page * page,size_t offset,size_t size,enum dma_data_direction dir,unsigned long attrs)169 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
170 struct page *page, size_t offset, size_t size,
171 enum dma_data_direction dir, unsigned long attrs)
172 {
173 return DMA_MAPPING_ERROR;
174 }
dma_unmap_page_attrs(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)175 static inline void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr,
176 size_t size, enum dma_data_direction dir, unsigned long attrs)
177 {
178 }
dma_map_sg_attrs(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)179 static inline unsigned int dma_map_sg_attrs(struct device *dev,
180 struct scatterlist *sg, int nents, enum dma_data_direction dir,
181 unsigned long attrs)
182 {
183 return 0;
184 }
dma_unmap_sg_attrs(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)185 static inline void dma_unmap_sg_attrs(struct device *dev,
186 struct scatterlist *sg, int nents, enum dma_data_direction dir,
187 unsigned long attrs)
188 {
189 }
dma_map_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)190 static inline int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
191 enum dma_data_direction dir, unsigned long attrs)
192 {
193 return -EOPNOTSUPP;
194 }
dma_map_resource(struct device * dev,phys_addr_t phys_addr,size_t size,enum dma_data_direction dir,unsigned long attrs)195 static inline dma_addr_t dma_map_resource(struct device *dev,
196 phys_addr_t phys_addr, size_t size, enum dma_data_direction dir,
197 unsigned long attrs)
198 {
199 return DMA_MAPPING_ERROR;
200 }
dma_unmap_resource(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)201 static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
202 size_t size, enum dma_data_direction dir, unsigned long attrs)
203 {
204 }
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)205 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
206 {
207 return -ENOMEM;
208 }
dma_alloc_attrs(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag,unsigned long attrs)209 static inline void *dma_alloc_attrs(struct device *dev, size_t size,
210 dma_addr_t *dma_handle, gfp_t flag, unsigned long attrs)
211 {
212 return NULL;
213 }
dma_free_attrs(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_handle,unsigned long attrs)214 static void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
215 dma_addr_t dma_handle, unsigned long attrs)
216 {
217 }
dmam_alloc_attrs(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp,unsigned long attrs)218 static inline void *dmam_alloc_attrs(struct device *dev, size_t size,
219 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
220 {
221 return NULL;
222 }
dmam_free_coherent(struct device * dev,size_t size,void * vaddr,dma_addr_t dma_handle)223 static inline void dmam_free_coherent(struct device *dev, size_t size,
224 void *vaddr, dma_addr_t dma_handle)
225 {
226 }
dma_get_sgtable_attrs(struct device * dev,struct sg_table * sgt,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)227 static inline int dma_get_sgtable_attrs(struct device *dev,
228 struct sg_table *sgt, void *cpu_addr, dma_addr_t dma_addr,
229 size_t size, unsigned long attrs)
230 {
231 return -ENXIO;
232 }
dma_mmap_attrs(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)233 static inline int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
234 void *cpu_addr, dma_addr_t dma_addr, size_t size,
235 unsigned long attrs)
236 {
237 return -ENXIO;
238 }
dma_can_mmap(struct device * dev)239 static inline bool dma_can_mmap(struct device *dev)
240 {
241 return false;
242 }
dma_pci_p2pdma_supported(struct device * dev)243 static inline bool dma_pci_p2pdma_supported(struct device *dev)
244 {
245 return false;
246 }
dma_set_mask(struct device * dev,u64 mask)247 static inline int dma_set_mask(struct device *dev, u64 mask)
248 {
249 return -EIO;
250 }
dma_set_coherent_mask(struct device * dev,u64 mask)251 static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
252 {
253 return -EIO;
254 }
dma_get_required_mask(struct device * dev)255 static inline u64 dma_get_required_mask(struct device *dev)
256 {
257 return 0;
258 }
dma_addressing_limited(struct device * dev)259 static inline bool dma_addressing_limited(struct device *dev)
260 {
261 return false;
262 }
dma_max_mapping_size(struct device * dev)263 static inline size_t dma_max_mapping_size(struct device *dev)
264 {
265 return 0;
266 }
dma_opt_mapping_size(struct device * dev)267 static inline size_t dma_opt_mapping_size(struct device *dev)
268 {
269 return 0;
270 }
dma_get_merge_boundary(struct device * dev)271 static inline unsigned long dma_get_merge_boundary(struct device *dev)
272 {
273 return 0;
274 }
dma_alloc_noncontiguous(struct device * dev,size_t size,enum dma_data_direction dir,gfp_t gfp,unsigned long attrs)275 static inline struct sg_table *dma_alloc_noncontiguous(struct device *dev,
276 size_t size, enum dma_data_direction dir, gfp_t gfp,
277 unsigned long attrs)
278 {
279 return NULL;
280 }
dma_free_noncontiguous(struct device * dev,size_t size,struct sg_table * sgt,enum dma_data_direction dir)281 static inline void dma_free_noncontiguous(struct device *dev, size_t size,
282 struct sg_table *sgt, enum dma_data_direction dir)
283 {
284 }
dma_vmap_noncontiguous(struct device * dev,size_t size,struct sg_table * sgt)285 static inline void *dma_vmap_noncontiguous(struct device *dev, size_t size,
286 struct sg_table *sgt)
287 {
288 return NULL;
289 }
dma_vunmap_noncontiguous(struct device * dev,void * vaddr)290 static inline void dma_vunmap_noncontiguous(struct device *dev, void *vaddr)
291 {
292 }
dma_mmap_noncontiguous(struct device * dev,struct vm_area_struct * vma,size_t size,struct sg_table * sgt)293 static inline int dma_mmap_noncontiguous(struct device *dev,
294 struct vm_area_struct *vma, size_t size, struct sg_table *sgt)
295 {
296 return -EINVAL;
297 }
298 #endif /* CONFIG_HAS_DMA */
299
300 #ifdef CONFIG_IOMMU_DMA
301 /**
302 * dma_use_iova - check if the IOVA API is used for this state
303 * @state: IOVA state
304 *
305 * Return %true if the DMA transfers uses the dma_iova_*() calls or %false if
306 * they can't be used.
307 */
dma_use_iova(struct dma_iova_state * state)308 static inline bool dma_use_iova(struct dma_iova_state *state)
309 {
310 return state->__size != 0;
311 }
312
313 bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state,
314 phys_addr_t phys, size_t size);
315 void dma_iova_free(struct device *dev, struct dma_iova_state *state);
316 void dma_iova_destroy(struct device *dev, struct dma_iova_state *state,
317 size_t mapped_len, enum dma_data_direction dir,
318 unsigned long attrs);
319 int dma_iova_sync(struct device *dev, struct dma_iova_state *state,
320 size_t offset, size_t size);
321 int dma_iova_link(struct device *dev, struct dma_iova_state *state,
322 phys_addr_t phys, size_t offset, size_t size,
323 enum dma_data_direction dir, unsigned long attrs);
324 void dma_iova_unlink(struct device *dev, struct dma_iova_state *state,
325 size_t offset, size_t size, enum dma_data_direction dir,
326 unsigned long attrs);
327 #else /* CONFIG_IOMMU_DMA */
dma_use_iova(struct dma_iova_state * state)328 static inline bool dma_use_iova(struct dma_iova_state *state)
329 {
330 return false;
331 }
dma_iova_try_alloc(struct device * dev,struct dma_iova_state * state,phys_addr_t phys,size_t size)332 static inline bool dma_iova_try_alloc(struct device *dev,
333 struct dma_iova_state *state, phys_addr_t phys, size_t size)
334 {
335 return false;
336 }
dma_iova_free(struct device * dev,struct dma_iova_state * state)337 static inline void dma_iova_free(struct device *dev,
338 struct dma_iova_state *state)
339 {
340 }
dma_iova_destroy(struct device * dev,struct dma_iova_state * state,size_t mapped_len,enum dma_data_direction dir,unsigned long attrs)341 static inline void dma_iova_destroy(struct device *dev,
342 struct dma_iova_state *state, size_t mapped_len,
343 enum dma_data_direction dir, unsigned long attrs)
344 {
345 }
dma_iova_sync(struct device * dev,struct dma_iova_state * state,size_t offset,size_t size)346 static inline int dma_iova_sync(struct device *dev,
347 struct dma_iova_state *state, size_t offset, size_t size)
348 {
349 return -EOPNOTSUPP;
350 }
dma_iova_link(struct device * dev,struct dma_iova_state * state,phys_addr_t phys,size_t offset,size_t size,enum dma_data_direction dir,unsigned long attrs)351 static inline int dma_iova_link(struct device *dev,
352 struct dma_iova_state *state, phys_addr_t phys, size_t offset,
353 size_t size, enum dma_data_direction dir, unsigned long attrs)
354 {
355 return -EOPNOTSUPP;
356 }
dma_iova_unlink(struct device * dev,struct dma_iova_state * state,size_t offset,size_t size,enum dma_data_direction dir,unsigned long attrs)357 static inline void dma_iova_unlink(struct device *dev,
358 struct dma_iova_state *state, size_t offset, size_t size,
359 enum dma_data_direction dir, unsigned long attrs)
360 {
361 }
362 #endif /* CONFIG_IOMMU_DMA */
363
364 #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC)
365 void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
366 enum dma_data_direction dir);
367 void __dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
368 size_t size, enum dma_data_direction dir);
369 void __dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
370 int nelems, enum dma_data_direction dir);
371 void __dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
372 int nelems, enum dma_data_direction dir);
373 bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr);
374
dma_dev_need_sync(const struct device * dev)375 static inline bool dma_dev_need_sync(const struct device *dev)
376 {
377 /* Always call DMA sync operations when debugging is enabled */
378 return !dev->dma_skip_sync || IS_ENABLED(CONFIG_DMA_API_DEBUG);
379 }
380
dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)381 static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
382 size_t size, enum dma_data_direction dir)
383 {
384 if (dma_dev_need_sync(dev))
385 __dma_sync_single_for_cpu(dev, addr, size, dir);
386 }
387
dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)388 static inline void dma_sync_single_for_device(struct device *dev,
389 dma_addr_t addr, size_t size, enum dma_data_direction dir)
390 {
391 if (dma_dev_need_sync(dev))
392 __dma_sync_single_for_device(dev, addr, size, dir);
393 }
394
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)395 static inline void dma_sync_sg_for_cpu(struct device *dev,
396 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
397 {
398 if (dma_dev_need_sync(dev))
399 __dma_sync_sg_for_cpu(dev, sg, nelems, dir);
400 }
401
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)402 static inline void dma_sync_sg_for_device(struct device *dev,
403 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
404 {
405 if (dma_dev_need_sync(dev))
406 __dma_sync_sg_for_device(dev, sg, nelems, dir);
407 }
408
dma_need_sync(struct device * dev,dma_addr_t dma_addr)409 static inline bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
410 {
411 return dma_dev_need_sync(dev) ? __dma_need_sync(dev, dma_addr) : false;
412 }
413 bool dma_need_unmap(struct device *dev);
414 #else /* !CONFIG_HAS_DMA || !CONFIG_DMA_NEED_SYNC */
dma_dev_need_sync(const struct device * dev)415 static inline bool dma_dev_need_sync(const struct device *dev)
416 {
417 return false;
418 }
dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)419 static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
420 size_t size, enum dma_data_direction dir)
421 {
422 }
dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)423 static inline void dma_sync_single_for_device(struct device *dev,
424 dma_addr_t addr, size_t size, enum dma_data_direction dir)
425 {
426 }
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)427 static inline void dma_sync_sg_for_cpu(struct device *dev,
428 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
429 {
430 }
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)431 static inline void dma_sync_sg_for_device(struct device *dev,
432 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
433 {
434 }
dma_need_sync(struct device * dev,dma_addr_t dma_addr)435 static inline bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
436 {
437 return false;
438 }
dma_need_unmap(struct device * dev)439 static inline bool dma_need_unmap(struct device *dev)
440 {
441 return false;
442 }
443 #endif /* !CONFIG_HAS_DMA || !CONFIG_DMA_NEED_SYNC */
444
445 struct page *dma_alloc_pages(struct device *dev, size_t size,
446 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
447 void dma_free_pages(struct device *dev, size_t size, struct page *page,
448 dma_addr_t dma_handle, enum dma_data_direction dir);
449 int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma,
450 size_t size, struct page *page);
451
dma_alloc_noncoherent(struct device * dev,size_t size,dma_addr_t * dma_handle,enum dma_data_direction dir,gfp_t gfp)452 static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
453 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
454 {
455 struct page *page = dma_alloc_pages(dev, size, dma_handle, dir, gfp);
456 return page ? page_address(page) : NULL;
457 }
458
dma_free_noncoherent(struct device * dev,size_t size,void * vaddr,dma_addr_t dma_handle,enum dma_data_direction dir)459 static inline void dma_free_noncoherent(struct device *dev, size_t size,
460 void *vaddr, dma_addr_t dma_handle, enum dma_data_direction dir)
461 {
462 dma_free_pages(dev, size, virt_to_page(vaddr), dma_handle, dir);
463 }
464
dma_map_single_attrs(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir,unsigned long attrs)465 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
466 size_t size, enum dma_data_direction dir, unsigned long attrs)
467 {
468 /* DMA must never operate on areas that might be remapped. */
469 if (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
470 "rejecting DMA map of vmalloc memory\n"))
471 return DMA_MAPPING_ERROR;
472 debug_dma_map_single(dev, ptr, size);
473 return dma_map_page_attrs(dev, virt_to_page(ptr), offset_in_page(ptr),
474 size, dir, attrs);
475 }
476
dma_unmap_single_attrs(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)477 static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
478 size_t size, enum dma_data_direction dir, unsigned long attrs)
479 {
480 return dma_unmap_page_attrs(dev, addr, size, dir, attrs);
481 }
482
dma_sync_single_range_for_cpu(struct device * dev,dma_addr_t addr,unsigned long offset,size_t size,enum dma_data_direction dir)483 static inline void dma_sync_single_range_for_cpu(struct device *dev,
484 dma_addr_t addr, unsigned long offset, size_t size,
485 enum dma_data_direction dir)
486 {
487 return dma_sync_single_for_cpu(dev, addr + offset, size, dir);
488 }
489
dma_sync_single_range_for_device(struct device * dev,dma_addr_t addr,unsigned long offset,size_t size,enum dma_data_direction dir)490 static inline void dma_sync_single_range_for_device(struct device *dev,
491 dma_addr_t addr, unsigned long offset, size_t size,
492 enum dma_data_direction dir)
493 {
494 return dma_sync_single_for_device(dev, addr + offset, size, dir);
495 }
496
497 /**
498 * dma_unmap_sgtable - Unmap the given buffer for DMA
499 * @dev: The device for which to perform the DMA operation
500 * @sgt: The sg_table object describing the buffer
501 * @dir: DMA direction
502 * @attrs: Optional DMA attributes for the unmap operation
503 *
504 * Unmaps a buffer described by a scatterlist stored in the given sg_table
505 * object for the @dir DMA operation by the @dev device. After this function
506 * the ownership of the buffer is transferred back to the CPU domain.
507 */
dma_unmap_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)508 static inline void dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
509 enum dma_data_direction dir, unsigned long attrs)
510 {
511 dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
512 }
513
514 /**
515 * dma_sync_sgtable_for_cpu - Synchronize the given buffer for CPU access
516 * @dev: The device for which to perform the DMA operation
517 * @sgt: The sg_table object describing the buffer
518 * @dir: DMA direction
519 *
520 * Performs the needed cache synchronization and moves the ownership of the
521 * buffer back to the CPU domain, so it is safe to perform any access to it
522 * by the CPU. Before doing any further DMA operations, one has to transfer
523 * the ownership of the buffer back to the DMA domain by calling the
524 * dma_sync_sgtable_for_device().
525 */
dma_sync_sgtable_for_cpu(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir)526 static inline void dma_sync_sgtable_for_cpu(struct device *dev,
527 struct sg_table *sgt, enum dma_data_direction dir)
528 {
529 dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir);
530 }
531
532 /**
533 * dma_sync_sgtable_for_device - Synchronize the given buffer for DMA
534 * @dev: The device for which to perform the DMA operation
535 * @sgt: The sg_table object describing the buffer
536 * @dir: DMA direction
537 *
538 * Performs the needed cache synchronization and moves the ownership of the
539 * buffer back to the DMA domain, so it is safe to perform the DMA operation.
540 * Once finished, one has to call dma_sync_sgtable_for_cpu() or
541 * dma_unmap_sgtable().
542 */
dma_sync_sgtable_for_device(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir)543 static inline void dma_sync_sgtable_for_device(struct device *dev,
544 struct sg_table *sgt, enum dma_data_direction dir)
545 {
546 dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir);
547 }
548
549 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
550 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
551 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
552 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
553 #define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
554 #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
555 #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
556 #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
557
558 bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size);
559
dma_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)560 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
561 dma_addr_t *dma_handle, gfp_t gfp)
562 {
563 return dma_alloc_attrs(dev, size, dma_handle, gfp,
564 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
565 }
566
dma_free_coherent(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_handle)567 static inline void dma_free_coherent(struct device *dev, size_t size,
568 void *cpu_addr, dma_addr_t dma_handle)
569 {
570 return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
571 }
572
573
dma_get_mask(struct device * dev)574 static inline u64 dma_get_mask(struct device *dev)
575 {
576 if (dev->dma_mask && *dev->dma_mask)
577 return *dev->dma_mask;
578 return DMA_BIT_MASK(32);
579 }
580
581 /*
582 * Set both the DMA mask and the coherent DMA mask to the same thing.
583 * Note that we don't check the return value from dma_set_coherent_mask()
584 * as the DMA API guarantees that the coherent DMA mask can be set to
585 * the same or smaller than the streaming DMA mask.
586 */
dma_set_mask_and_coherent(struct device * dev,u64 mask)587 static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
588 {
589 int rc = dma_set_mask(dev, mask);
590 if (rc == 0)
591 dma_set_coherent_mask(dev, mask);
592 return rc;
593 }
594
595 /*
596 * Similar to the above, except it deals with the case where the device
597 * does not have dev->dma_mask appropriately setup.
598 */
dma_coerce_mask_and_coherent(struct device * dev,u64 mask)599 static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
600 {
601 dev->dma_mask = &dev->coherent_dma_mask;
602 return dma_set_mask_and_coherent(dev, mask);
603 }
604
dma_get_max_seg_size(struct device * dev)605 static inline unsigned int dma_get_max_seg_size(struct device *dev)
606 {
607 if (dev->dma_parms && dev->dma_parms->max_segment_size)
608 return dev->dma_parms->max_segment_size;
609 return SZ_64K;
610 }
611
dma_set_max_seg_size(struct device * dev,unsigned int size)612 static inline void dma_set_max_seg_size(struct device *dev, unsigned int size)
613 {
614 if (WARN_ON_ONCE(!dev->dma_parms))
615 return;
616 dev->dma_parms->max_segment_size = size;
617 }
618
dma_get_seg_boundary(struct device * dev)619 static inline unsigned long dma_get_seg_boundary(struct device *dev)
620 {
621 if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
622 return dev->dma_parms->segment_boundary_mask;
623 return ULONG_MAX;
624 }
625
626 /**
627 * dma_get_seg_boundary_nr_pages - return the segment boundary in "page" units
628 * @dev: device to guery the boundary for
629 * @page_shift: ilog() of the IOMMU page size
630 *
631 * Return the segment boundary in IOMMU page units (which may be different from
632 * the CPU page size) for the passed in device.
633 *
634 * If @dev is NULL a boundary of U32_MAX is assumed, this case is just for
635 * non-DMA API callers.
636 */
dma_get_seg_boundary_nr_pages(struct device * dev,unsigned int page_shift)637 static inline unsigned long dma_get_seg_boundary_nr_pages(struct device *dev,
638 unsigned int page_shift)
639 {
640 if (!dev)
641 return (U32_MAX >> page_shift) + 1;
642 return (dma_get_seg_boundary(dev) >> page_shift) + 1;
643 }
644
dma_set_seg_boundary(struct device * dev,unsigned long mask)645 static inline void dma_set_seg_boundary(struct device *dev, unsigned long mask)
646 {
647 if (WARN_ON_ONCE(!dev->dma_parms))
648 return;
649 dev->dma_parms->segment_boundary_mask = mask;
650 }
651
dma_get_min_align_mask(struct device * dev)652 static inline unsigned int dma_get_min_align_mask(struct device *dev)
653 {
654 if (dev->dma_parms)
655 return dev->dma_parms->min_align_mask;
656 return 0;
657 }
658
dma_set_min_align_mask(struct device * dev,unsigned int min_align_mask)659 static inline void dma_set_min_align_mask(struct device *dev,
660 unsigned int min_align_mask)
661 {
662 if (WARN_ON_ONCE(!dev->dma_parms))
663 return;
664 dev->dma_parms->min_align_mask = min_align_mask;
665 }
666
667 #ifndef dma_get_cache_alignment
dma_get_cache_alignment(void)668 static inline int dma_get_cache_alignment(void)
669 {
670 #ifdef ARCH_HAS_DMA_MINALIGN
671 return ARCH_DMA_MINALIGN;
672 #endif
673 return 1;
674 }
675 #endif
676
dmam_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)677 static inline void *dmam_alloc_coherent(struct device *dev, size_t size,
678 dma_addr_t *dma_handle, gfp_t gfp)
679 {
680 return dmam_alloc_attrs(dev, size, dma_handle, gfp,
681 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
682 }
683
dma_alloc_wc(struct device * dev,size_t size,dma_addr_t * dma_addr,gfp_t gfp)684 static inline void *dma_alloc_wc(struct device *dev, size_t size,
685 dma_addr_t *dma_addr, gfp_t gfp)
686 {
687 unsigned long attrs = DMA_ATTR_WRITE_COMBINE;
688
689 if (gfp & __GFP_NOWARN)
690 attrs |= DMA_ATTR_NO_WARN;
691
692 return dma_alloc_attrs(dev, size, dma_addr, gfp, attrs);
693 }
694
dma_free_wc(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr)695 static inline void dma_free_wc(struct device *dev, size_t size,
696 void *cpu_addr, dma_addr_t dma_addr)
697 {
698 return dma_free_attrs(dev, size, cpu_addr, dma_addr,
699 DMA_ATTR_WRITE_COMBINE);
700 }
701
dma_mmap_wc(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size)702 static inline int dma_mmap_wc(struct device *dev,
703 struct vm_area_struct *vma,
704 void *cpu_addr, dma_addr_t dma_addr,
705 size_t size)
706 {
707 return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
708 DMA_ATTR_WRITE_COMBINE);
709 }
710
711 #ifdef CONFIG_NEED_DMA_MAP_STATE
712 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
713 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
714 #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
715 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
716 #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
717 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
718 #else
719 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
720 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
721 #define dma_unmap_addr(PTR, ADDR_NAME) \
722 ({ typeof(PTR) __p __maybe_unused = PTR; 0; })
723 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) \
724 do { typeof(PTR) __p __maybe_unused = PTR; } while (0)
725 #define dma_unmap_len(PTR, LEN_NAME) \
726 ({ typeof(PTR) __p __maybe_unused = PTR; 0; })
727 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) \
728 do { typeof(PTR) __p __maybe_unused = PTR; } while (0)
729 #endif
730
731 #endif /* _LINUX_DMA_MAPPING_H */
732