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-direction.h>
10 #include <linux/scatterlist.h>
11 #include <linux/bug.h>
12 #include <linux/mem_encrypt.h>
13
14 /**
15 * List of possible attributes associated with a DMA mapping. The semantics
16 * of each attribute should be defined in Documentation/core-api/dma-attributes.rst.
17 */
18
19 /*
20 * DMA_ATTR_WEAK_ORDERING: Specifies that reads and writes to the mapping
21 * may be weakly ordered, that is that reads and writes may pass each other.
22 */
23 #define DMA_ATTR_WEAK_ORDERING (1UL << 1)
24 /*
25 * DMA_ATTR_WRITE_COMBINE: Specifies that writes to the mapping may be
26 * buffered to improve performance.
27 */
28 #define DMA_ATTR_WRITE_COMBINE (1UL << 2)
29 /*
30 * DMA_ATTR_NO_KERNEL_MAPPING: Lets the platform to avoid creating a kernel
31 * virtual mapping for the allocated buffer.
32 */
33 #define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4)
34 /*
35 * DMA_ATTR_SKIP_CPU_SYNC: Allows platform code to skip synchronization of
36 * the CPU cache for the given buffer assuming that it has been already
37 * transferred to 'device' domain.
38 */
39 #define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5)
40 /*
41 * DMA_ATTR_FORCE_CONTIGUOUS: Forces contiguous allocation of the buffer
42 * in physical memory.
43 */
44 #define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6)
45 /*
46 * DMA_ATTR_ALLOC_SINGLE_PAGES: This is a hint to the DMA-mapping subsystem
47 * that it's probably not worth the time to try to allocate memory to in a way
48 * that gives better TLB efficiency.
49 */
50 #define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7)
51 /*
52 * DMA_ATTR_NO_WARN: This tells the DMA-mapping subsystem to suppress
53 * allocation failure reports (similarly to __GFP_NOWARN).
54 */
55 #define DMA_ATTR_NO_WARN (1UL << 8)
56
57 /*
58 * DMA_ATTR_PRIVILEGED: used to indicate that the buffer is fully
59 * accessible at an elevated privilege level (and ideally inaccessible or
60 * at least read-only at lesser-privileged levels).
61 */
62 #define DMA_ATTR_PRIVILEGED (1UL << 9)
63
64 /*
65 * DMA_ATTR_SYS_CACHE_ONLY: used to indicate that the buffer should be mapped
66 * with the correct memory attributes so that it can be cached in the system
67 * or last level cache. This is useful for buffers that are being mapped for
68 * devices that are non-coherent, but can use the system cache.
69 */
70 #define DMA_ATTR_SYS_CACHE_ONLY (1UL << 10)
71
72 /*
73 * DMA_ATTR_SYS_CACHE_ONLY_NWA: used to indicate that the buffer should be
74 * mapped with the correct memory attributes so that it can be cached in the
75 * system or last level cache, with a no write allocate cache policy. This is
76 * useful for buffers that are being mapped for devices that are non-coherent,
77 * but can use the system cache.
78 */
79 #define DMA_ATTR_SYS_CACHE_ONLY_NWA (1UL << 11)
80
81 /*
82 * A dma_addr_t can hold any valid DMA or bus address for the platform. It can
83 * be given to a device to use as a DMA source or target. It is specific to a
84 * given device and there may be a translation between the CPU physical address
85 * space and the bus address space.
86 *
87 * DMA_MAPPING_ERROR is the magic error code if a mapping failed. It should not
88 * be used directly in drivers, but checked for using dma_mapping_error()
89 * instead.
90 */
91 #define DMA_MAPPING_ERROR (~(dma_addr_t)0)
92
93 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
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_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
137 enum dma_data_direction dir);
138 void dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
139 size_t size, enum dma_data_direction dir);
140 void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
141 int nelems, enum dma_data_direction dir);
142 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
143 int nelems, enum dma_data_direction dir);
144 void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
145 gfp_t flag, unsigned long attrs);
146 void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
147 dma_addr_t dma_handle, unsigned long attrs);
148 void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
149 gfp_t gfp, unsigned long attrs);
150 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
151 dma_addr_t dma_handle);
152 int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
153 void *cpu_addr, dma_addr_t dma_addr, size_t size,
154 unsigned long attrs);
155 int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
156 void *cpu_addr, dma_addr_t dma_addr, size_t size,
157 unsigned long attrs);
158 bool dma_can_mmap(struct device *dev);
159 int dma_supported(struct device *dev, u64 mask);
160 int dma_set_mask(struct device *dev, u64 mask);
161 int dma_set_coherent_mask(struct device *dev, u64 mask);
162 u64 dma_get_required_mask(struct device *dev);
163 size_t dma_max_mapping_size(struct device *dev);
164 bool dma_need_sync(struct device *dev, dma_addr_t dma_addr);
165 unsigned long dma_get_merge_boundary(struct device *dev);
166 struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size,
167 enum dma_data_direction dir, gfp_t gfp, unsigned long attrs);
168 void dma_free_noncontiguous(struct device *dev, size_t size,
169 struct sg_table *sgt, enum dma_data_direction dir);
170 void *dma_vmap_noncontiguous(struct device *dev, size_t size,
171 struct sg_table *sgt);
172 void dma_vunmap_noncontiguous(struct device *dev, void *vaddr);
173 int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma,
174 size_t size, struct sg_table *sgt);
175 #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)176 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
177 struct page *page, size_t offset, size_t size,
178 enum dma_data_direction dir, unsigned long attrs)
179 {
180 return DMA_MAPPING_ERROR;
181 }
dma_unmap_page_attrs(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)182 static inline void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr,
183 size_t size, enum dma_data_direction dir, unsigned long attrs)
184 {
185 }
dma_map_sg_attrs(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)186 static inline unsigned int dma_map_sg_attrs(struct device *dev,
187 struct scatterlist *sg, int nents, enum dma_data_direction dir,
188 unsigned long attrs)
189 {
190 return 0;
191 }
dma_unmap_sg_attrs(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)192 static inline void dma_unmap_sg_attrs(struct device *dev,
193 struct scatterlist *sg, int nents, enum dma_data_direction dir,
194 unsigned long attrs)
195 {
196 }
dma_map_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)197 static inline int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
198 enum dma_data_direction dir, unsigned long attrs)
199 {
200 return -EOPNOTSUPP;
201 }
dma_map_resource(struct device * dev,phys_addr_t phys_addr,size_t size,enum dma_data_direction dir,unsigned long attrs)202 static inline dma_addr_t dma_map_resource(struct device *dev,
203 phys_addr_t phys_addr, size_t size, enum dma_data_direction dir,
204 unsigned long attrs)
205 {
206 return DMA_MAPPING_ERROR;
207 }
dma_unmap_resource(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)208 static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
209 size_t size, enum dma_data_direction dir, unsigned long attrs)
210 {
211 }
dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)212 static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
213 size_t size, enum dma_data_direction dir)
214 {
215 }
dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)216 static inline void dma_sync_single_for_device(struct device *dev,
217 dma_addr_t addr, size_t size, enum dma_data_direction dir)
218 {
219 }
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)220 static inline void dma_sync_sg_for_cpu(struct device *dev,
221 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
222 {
223 }
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)224 static inline void dma_sync_sg_for_device(struct device *dev,
225 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
226 {
227 }
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)228 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
229 {
230 return -ENOMEM;
231 }
dma_alloc_attrs(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag,unsigned long attrs)232 static inline void *dma_alloc_attrs(struct device *dev, size_t size,
233 dma_addr_t *dma_handle, gfp_t flag, unsigned long attrs)
234 {
235 return NULL;
236 }
dma_free_attrs(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_handle,unsigned long attrs)237 static void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
238 dma_addr_t dma_handle, unsigned long attrs)
239 {
240 }
dmam_alloc_attrs(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp,unsigned long attrs)241 static inline void *dmam_alloc_attrs(struct device *dev, size_t size,
242 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
243 {
244 return NULL;
245 }
dmam_free_coherent(struct device * dev,size_t size,void * vaddr,dma_addr_t dma_handle)246 static inline void dmam_free_coherent(struct device *dev, size_t size,
247 void *vaddr, dma_addr_t dma_handle)
248 {
249 }
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)250 static inline int dma_get_sgtable_attrs(struct device *dev,
251 struct sg_table *sgt, void *cpu_addr, dma_addr_t dma_addr,
252 size_t size, unsigned long attrs)
253 {
254 return -ENXIO;
255 }
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)256 static inline int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
257 void *cpu_addr, dma_addr_t dma_addr, size_t size,
258 unsigned long attrs)
259 {
260 return -ENXIO;
261 }
dma_can_mmap(struct device * dev)262 static inline bool dma_can_mmap(struct device *dev)
263 {
264 return false;
265 }
dma_supported(struct device * dev,u64 mask)266 static inline int dma_supported(struct device *dev, u64 mask)
267 {
268 return 0;
269 }
dma_set_mask(struct device * dev,u64 mask)270 static inline int dma_set_mask(struct device *dev, u64 mask)
271 {
272 return -EIO;
273 }
dma_set_coherent_mask(struct device * dev,u64 mask)274 static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
275 {
276 return -EIO;
277 }
dma_get_required_mask(struct device * dev)278 static inline u64 dma_get_required_mask(struct device *dev)
279 {
280 return 0;
281 }
dma_max_mapping_size(struct device * dev)282 static inline size_t dma_max_mapping_size(struct device *dev)
283 {
284 return 0;
285 }
dma_need_sync(struct device * dev,dma_addr_t dma_addr)286 static inline bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
287 {
288 return false;
289 }
dma_get_merge_boundary(struct device * dev)290 static inline unsigned long dma_get_merge_boundary(struct device *dev)
291 {
292 return 0;
293 }
dma_alloc_noncontiguous(struct device * dev,size_t size,enum dma_data_direction dir,gfp_t gfp,unsigned long attrs)294 static inline struct sg_table *dma_alloc_noncontiguous(struct device *dev,
295 size_t size, enum dma_data_direction dir, gfp_t gfp,
296 unsigned long attrs)
297 {
298 return NULL;
299 }
dma_free_noncontiguous(struct device * dev,size_t size,struct sg_table * sgt,enum dma_data_direction dir)300 static inline void dma_free_noncontiguous(struct device *dev, size_t size,
301 struct sg_table *sgt, enum dma_data_direction dir)
302 {
303 }
dma_vmap_noncontiguous(struct device * dev,size_t size,struct sg_table * sgt)304 static inline void *dma_vmap_noncontiguous(struct device *dev, size_t size,
305 struct sg_table *sgt)
306 {
307 return NULL;
308 }
dma_vunmap_noncontiguous(struct device * dev,void * vaddr)309 static inline void dma_vunmap_noncontiguous(struct device *dev, void *vaddr)
310 {
311 }
dma_mmap_noncontiguous(struct device * dev,struct vm_area_struct * vma,size_t size,struct sg_table * sgt)312 static inline int dma_mmap_noncontiguous(struct device *dev,
313 struct vm_area_struct *vma, size_t size, struct sg_table *sgt)
314 {
315 return -EINVAL;
316 }
317 #endif /* CONFIG_HAS_DMA */
318
319 struct page *dma_alloc_pages(struct device *dev, size_t size,
320 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
321 void dma_free_pages(struct device *dev, size_t size, struct page *page,
322 dma_addr_t dma_handle, enum dma_data_direction dir);
323 int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma,
324 size_t size, struct page *page);
325
dma_alloc_noncoherent(struct device * dev,size_t size,dma_addr_t * dma_handle,enum dma_data_direction dir,gfp_t gfp)326 static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
327 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
328 {
329 struct page *page = dma_alloc_pages(dev, size, dma_handle, dir, gfp);
330 return page ? page_address(page) : NULL;
331 }
332
dma_free_noncoherent(struct device * dev,size_t size,void * vaddr,dma_addr_t dma_handle,enum dma_data_direction dir)333 static inline void dma_free_noncoherent(struct device *dev, size_t size,
334 void *vaddr, dma_addr_t dma_handle, enum dma_data_direction dir)
335 {
336 dma_free_pages(dev, size, virt_to_page(vaddr), dma_handle, dir);
337 }
338
dma_map_single_attrs(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir,unsigned long attrs)339 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
340 size_t size, enum dma_data_direction dir, unsigned long attrs)
341 {
342 /* DMA must never operate on areas that might be remapped. */
343 if (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
344 "rejecting DMA map of vmalloc memory\n"))
345 return DMA_MAPPING_ERROR;
346 debug_dma_map_single(dev, ptr, size);
347 return dma_map_page_attrs(dev, virt_to_page(ptr), offset_in_page(ptr),
348 size, dir, attrs);
349 }
350
dma_unmap_single_attrs(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)351 static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
352 size_t size, enum dma_data_direction dir, unsigned long attrs)
353 {
354 return dma_unmap_page_attrs(dev, addr, size, dir, attrs);
355 }
356
dma_sync_single_range_for_cpu(struct device * dev,dma_addr_t addr,unsigned long offset,size_t size,enum dma_data_direction dir)357 static inline void dma_sync_single_range_for_cpu(struct device *dev,
358 dma_addr_t addr, unsigned long offset, size_t size,
359 enum dma_data_direction dir)
360 {
361 return dma_sync_single_for_cpu(dev, addr + offset, size, dir);
362 }
363
dma_sync_single_range_for_device(struct device * dev,dma_addr_t addr,unsigned long offset,size_t size,enum dma_data_direction dir)364 static inline void dma_sync_single_range_for_device(struct device *dev,
365 dma_addr_t addr, unsigned long offset, size_t size,
366 enum dma_data_direction dir)
367 {
368 return dma_sync_single_for_device(dev, addr + offset, size, dir);
369 }
370
371 /**
372 * dma_unmap_sgtable - Unmap the given buffer for DMA
373 * @dev: The device for which to perform the DMA operation
374 * @sgt: The sg_table object describing the buffer
375 * @dir: DMA direction
376 * @attrs: Optional DMA attributes for the unmap operation
377 *
378 * Unmaps a buffer described by a scatterlist stored in the given sg_table
379 * object for the @dir DMA operation by the @dev device. After this function
380 * the ownership of the buffer is transferred back to the CPU domain.
381 */
dma_unmap_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)382 static inline void dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
383 enum dma_data_direction dir, unsigned long attrs)
384 {
385 dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
386 }
387
388 /**
389 * dma_sync_sgtable_for_cpu - Synchronize the given buffer for CPU access
390 * @dev: The device for which to perform the DMA operation
391 * @sgt: The sg_table object describing the buffer
392 * @dir: DMA direction
393 *
394 * Performs the needed cache synchronization and moves the ownership of the
395 * buffer back to the CPU domain, so it is safe to perform any access to it
396 * by the CPU. Before doing any further DMA operations, one has to transfer
397 * the ownership of the buffer back to the DMA domain by calling the
398 * dma_sync_sgtable_for_device().
399 */
dma_sync_sgtable_for_cpu(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir)400 static inline void dma_sync_sgtable_for_cpu(struct device *dev,
401 struct sg_table *sgt, enum dma_data_direction dir)
402 {
403 dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir);
404 }
405
406 /**
407 * dma_sync_sgtable_for_device - Synchronize the given buffer for DMA
408 * @dev: The device for which to perform the DMA operation
409 * @sgt: The sg_table object describing the buffer
410 * @dir: DMA direction
411 *
412 * Performs the needed cache synchronization and moves the ownership of the
413 * buffer back to the DMA domain, so it is safe to perform the DMA operation.
414 * Once finished, one has to call dma_sync_sgtable_for_cpu() or
415 * dma_unmap_sgtable().
416 */
dma_sync_sgtable_for_device(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir)417 static inline void dma_sync_sgtable_for_device(struct device *dev,
418 struct sg_table *sgt, enum dma_data_direction dir)
419 {
420 dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir);
421 }
422
423 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
424 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
425 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
426 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
427 #define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
428 #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
429 #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
430 #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
431
dma_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)432 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
433 dma_addr_t *dma_handle, gfp_t gfp)
434 {
435 return dma_alloc_attrs(dev, size, dma_handle, gfp,
436 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
437 }
438
dma_free_coherent(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_handle)439 static inline void dma_free_coherent(struct device *dev, size_t size,
440 void *cpu_addr, dma_addr_t dma_handle)
441 {
442 return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
443 }
444
445
dma_get_mask(struct device * dev)446 static inline u64 dma_get_mask(struct device *dev)
447 {
448 if (dev->dma_mask && *dev->dma_mask)
449 return *dev->dma_mask;
450 return DMA_BIT_MASK(32);
451 }
452
453 /*
454 * Set both the DMA mask and the coherent DMA mask to the same thing.
455 * Note that we don't check the return value from dma_set_coherent_mask()
456 * as the DMA API guarantees that the coherent DMA mask can be set to
457 * the same or smaller than the streaming DMA mask.
458 */
dma_set_mask_and_coherent(struct device * dev,u64 mask)459 static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
460 {
461 int rc = dma_set_mask(dev, mask);
462 if (rc == 0)
463 dma_set_coherent_mask(dev, mask);
464 return rc;
465 }
466
467 /*
468 * Similar to the above, except it deals with the case where the device
469 * does not have dev->dma_mask appropriately setup.
470 */
dma_coerce_mask_and_coherent(struct device * dev,u64 mask)471 static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
472 {
473 dev->dma_mask = &dev->coherent_dma_mask;
474 return dma_set_mask_and_coherent(dev, mask);
475 }
476
477 /**
478 * dma_addressing_limited - return if the device is addressing limited
479 * @dev: device to check
480 *
481 * Return %true if the devices DMA mask is too small to address all memory in
482 * the system, else %false. Lack of addressing bits is the prime reason for
483 * bounce buffering, but might not be the only one.
484 */
dma_addressing_limited(struct device * dev)485 static inline bool dma_addressing_limited(struct device *dev)
486 {
487 return min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) <
488 dma_get_required_mask(dev);
489 }
490
dma_get_max_seg_size(struct device * dev)491 static inline unsigned int dma_get_max_seg_size(struct device *dev)
492 {
493 if (dev->dma_parms && dev->dma_parms->max_segment_size)
494 return dev->dma_parms->max_segment_size;
495 return SZ_64K;
496 }
497
dma_set_max_seg_size(struct device * dev,unsigned int size)498 static inline int dma_set_max_seg_size(struct device *dev, unsigned int size)
499 {
500 if (dev->dma_parms) {
501 dev->dma_parms->max_segment_size = size;
502 return 0;
503 }
504 return -EIO;
505 }
506
dma_get_seg_boundary(struct device * dev)507 static inline unsigned long dma_get_seg_boundary(struct device *dev)
508 {
509 if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
510 return dev->dma_parms->segment_boundary_mask;
511 return ULONG_MAX;
512 }
513
514 /**
515 * dma_get_seg_boundary_nr_pages - return the segment boundary in "page" units
516 * @dev: device to guery the boundary for
517 * @page_shift: ilog() of the IOMMU page size
518 *
519 * Return the segment boundary in IOMMU page units (which may be different from
520 * the CPU page size) for the passed in device.
521 *
522 * If @dev is NULL a boundary of U32_MAX is assumed, this case is just for
523 * non-DMA API callers.
524 */
dma_get_seg_boundary_nr_pages(struct device * dev,unsigned int page_shift)525 static inline unsigned long dma_get_seg_boundary_nr_pages(struct device *dev,
526 unsigned int page_shift)
527 {
528 if (!dev)
529 return (U32_MAX >> page_shift) + 1;
530 return (dma_get_seg_boundary(dev) >> page_shift) + 1;
531 }
532
dma_set_seg_boundary(struct device * dev,unsigned long mask)533 static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
534 {
535 if (dev->dma_parms) {
536 dev->dma_parms->segment_boundary_mask = mask;
537 return 0;
538 }
539 return -EIO;
540 }
541
dma_get_min_align_mask(struct device * dev)542 static inline unsigned int dma_get_min_align_mask(struct device *dev)
543 {
544 if (dev->dma_parms)
545 return dev->dma_parms->min_align_mask;
546 return 0;
547 }
548
dma_set_min_align_mask(struct device * dev,unsigned int min_align_mask)549 static inline int dma_set_min_align_mask(struct device *dev,
550 unsigned int min_align_mask)
551 {
552 if (WARN_ON_ONCE(!dev->dma_parms))
553 return -EIO;
554 dev->dma_parms->min_align_mask = min_align_mask;
555 return 0;
556 }
557
dma_get_cache_alignment(void)558 static inline int dma_get_cache_alignment(void)
559 {
560 #ifdef ARCH_DMA_MINALIGN
561 return ARCH_DMA_MINALIGN;
562 #endif
563 return 1;
564 }
565
dmam_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)566 static inline void *dmam_alloc_coherent(struct device *dev, size_t size,
567 dma_addr_t *dma_handle, gfp_t gfp)
568 {
569 return dmam_alloc_attrs(dev, size, dma_handle, gfp,
570 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
571 }
572
dma_alloc_wc(struct device * dev,size_t size,dma_addr_t * dma_addr,gfp_t gfp)573 static inline void *dma_alloc_wc(struct device *dev, size_t size,
574 dma_addr_t *dma_addr, gfp_t gfp)
575 {
576 unsigned long attrs = DMA_ATTR_WRITE_COMBINE;
577
578 if (gfp & __GFP_NOWARN)
579 attrs |= DMA_ATTR_NO_WARN;
580
581 return dma_alloc_attrs(dev, size, dma_addr, gfp, attrs);
582 }
583
dma_free_wc(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr)584 static inline void dma_free_wc(struct device *dev, size_t size,
585 void *cpu_addr, dma_addr_t dma_addr)
586 {
587 return dma_free_attrs(dev, size, cpu_addr, dma_addr,
588 DMA_ATTR_WRITE_COMBINE);
589 }
590
dma_mmap_wc(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size)591 static inline int dma_mmap_wc(struct device *dev,
592 struct vm_area_struct *vma,
593 void *cpu_addr, dma_addr_t dma_addr,
594 size_t size)
595 {
596 return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
597 DMA_ATTR_WRITE_COMBINE);
598 }
599
600 #ifdef CONFIG_NEED_DMA_MAP_STATE
601 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
602 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
603 #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
604 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
605 #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
606 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
607 #else
608 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
609 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
610 #define dma_unmap_addr(PTR, ADDR_NAME) (0)
611 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
612 #define dma_unmap_len(PTR, LEN_NAME) (0)
613 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
614 #endif
615
616 #endif /* _LINUX_DMA_MAPPING_H */
617