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 << 14)
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 << 15)
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 (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 int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
126 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 dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
131 size_t size, enum dma_data_direction dir, unsigned long attrs);
132 void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
133 enum dma_data_direction dir, unsigned long attrs);
134 void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
135 enum dma_data_direction dir);
136 void dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
137 size_t size, enum dma_data_direction dir);
138 void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
139 int nelems, enum dma_data_direction dir);
140 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
141 int nelems, enum dma_data_direction dir);
142 void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
143 gfp_t flag, unsigned long attrs);
144 void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
145 dma_addr_t dma_handle, unsigned long attrs);
146 void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
147 gfp_t gfp, unsigned long attrs);
148 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
149 dma_addr_t dma_handle);
150 int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
151 void *cpu_addr, dma_addr_t dma_addr, size_t size,
152 unsigned long attrs);
153 int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
154 void *cpu_addr, dma_addr_t dma_addr, size_t size,
155 unsigned long attrs);
156 bool dma_can_mmap(struct device *dev);
157 int dma_supported(struct device *dev, u64 mask);
158 int dma_set_mask(struct device *dev, u64 mask);
159 int dma_set_coherent_mask(struct device *dev, u64 mask);
160 u64 dma_get_required_mask(struct device *dev);
161 size_t dma_max_mapping_size(struct device *dev);
162 bool dma_need_sync(struct device *dev, dma_addr_t dma_addr);
163 unsigned long dma_get_merge_boundary(struct device *dev);
164 #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)165 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
166 struct page *page, size_t offset, size_t size,
167 enum dma_data_direction dir, unsigned long attrs)
168 {
169 return DMA_MAPPING_ERROR;
170 }
dma_unmap_page_attrs(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)171 static inline void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr,
172 size_t size, enum dma_data_direction dir, unsigned long attrs)
173 {
174 }
dma_map_sg_attrs(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)175 static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
176 int nents, enum dma_data_direction dir, unsigned long attrs)
177 {
178 return 0;
179 }
dma_unmap_sg_attrs(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)180 static inline void dma_unmap_sg_attrs(struct device *dev,
181 struct scatterlist *sg, int nents, enum dma_data_direction dir,
182 unsigned long attrs)
183 {
184 }
dma_map_resource(struct device * dev,phys_addr_t phys_addr,size_t size,enum dma_data_direction dir,unsigned long attrs)185 static inline dma_addr_t dma_map_resource(struct device *dev,
186 phys_addr_t phys_addr, size_t size, enum dma_data_direction dir,
187 unsigned long attrs)
188 {
189 return DMA_MAPPING_ERROR;
190 }
dma_unmap_resource(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)191 static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
192 size_t size, enum dma_data_direction dir, unsigned long attrs)
193 {
194 }
dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)195 static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
196 size_t size, enum dma_data_direction dir)
197 {
198 }
dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)199 static inline void dma_sync_single_for_device(struct device *dev,
200 dma_addr_t addr, size_t size, enum dma_data_direction dir)
201 {
202 }
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)203 static inline void dma_sync_sg_for_cpu(struct device *dev,
204 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
205 {
206 }
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction dir)207 static inline void dma_sync_sg_for_device(struct device *dev,
208 struct scatterlist *sg, int nelems, enum dma_data_direction dir)
209 {
210 }
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)211 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
212 {
213 return -ENOMEM;
214 }
dma_alloc_attrs(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag,unsigned long attrs)215 static inline void *dma_alloc_attrs(struct device *dev, size_t size,
216 dma_addr_t *dma_handle, gfp_t flag, unsigned long attrs)
217 {
218 return NULL;
219 }
dma_free_attrs(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_handle,unsigned long attrs)220 static void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
221 dma_addr_t dma_handle, unsigned long attrs)
222 {
223 }
dmam_alloc_attrs(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp,unsigned long attrs)224 static inline void *dmam_alloc_attrs(struct device *dev, size_t size,
225 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
226 {
227 return NULL;
228 }
dmam_free_coherent(struct device * dev,size_t size,void * vaddr,dma_addr_t dma_handle)229 static inline void dmam_free_coherent(struct device *dev, size_t size,
230 void *vaddr, dma_addr_t dma_handle)
231 {
232 }
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)233 static inline int dma_get_sgtable_attrs(struct device *dev,
234 struct sg_table *sgt, void *cpu_addr, dma_addr_t dma_addr,
235 size_t size, unsigned long attrs)
236 {
237 return -ENXIO;
238 }
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)239 static inline int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
240 void *cpu_addr, dma_addr_t dma_addr, size_t size,
241 unsigned long attrs)
242 {
243 return -ENXIO;
244 }
dma_can_mmap(struct device * dev)245 static inline bool dma_can_mmap(struct device *dev)
246 {
247 return false;
248 }
dma_supported(struct device * dev,u64 mask)249 static inline int dma_supported(struct device *dev, u64 mask)
250 {
251 return 0;
252 }
dma_set_mask(struct device * dev,u64 mask)253 static inline int dma_set_mask(struct device *dev, u64 mask)
254 {
255 return -EIO;
256 }
dma_set_coherent_mask(struct device * dev,u64 mask)257 static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
258 {
259 return -EIO;
260 }
dma_get_required_mask(struct device * dev)261 static inline u64 dma_get_required_mask(struct device *dev)
262 {
263 return 0;
264 }
dma_max_mapping_size(struct device * dev)265 static inline size_t dma_max_mapping_size(struct device *dev)
266 {
267 return 0;
268 }
dma_need_sync(struct device * dev,dma_addr_t dma_addr)269 static inline bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
270 {
271 return false;
272 }
dma_get_merge_boundary(struct device * dev)273 static inline unsigned long dma_get_merge_boundary(struct device *dev)
274 {
275 return 0;
276 }
277 #endif /* CONFIG_HAS_DMA */
278
279 struct page *dma_alloc_pages(struct device *dev, size_t size,
280 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
281 void dma_free_pages(struct device *dev, size_t size, struct page *page,
282 dma_addr_t dma_handle, enum dma_data_direction dir);
283 void *dma_alloc_noncoherent(struct device *dev, size_t size,
284 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
285 void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
286 dma_addr_t dma_handle, enum dma_data_direction dir);
287
dma_map_single_attrs(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir,unsigned long attrs)288 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
289 size_t size, enum dma_data_direction dir, unsigned long attrs)
290 {
291 /* DMA must never operate on areas that might be remapped. */
292 if (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
293 "rejecting DMA map of vmalloc memory\n"))
294 return DMA_MAPPING_ERROR;
295 debug_dma_map_single(dev, ptr, size);
296 return dma_map_page_attrs(dev, virt_to_page(ptr), offset_in_page(ptr),
297 size, dir, attrs);
298 }
299
dma_unmap_single_attrs(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir,unsigned long attrs)300 static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
301 size_t size, enum dma_data_direction dir, unsigned long attrs)
302 {
303 return dma_unmap_page_attrs(dev, addr, size, dir, attrs);
304 }
305
dma_sync_single_range_for_cpu(struct device * dev,dma_addr_t addr,unsigned long offset,size_t size,enum dma_data_direction dir)306 static inline void dma_sync_single_range_for_cpu(struct device *dev,
307 dma_addr_t addr, unsigned long offset, size_t size,
308 enum dma_data_direction dir)
309 {
310 return dma_sync_single_for_cpu(dev, addr + offset, size, dir);
311 }
312
dma_sync_single_range_for_device(struct device * dev,dma_addr_t addr,unsigned long offset,size_t size,enum dma_data_direction dir)313 static inline void dma_sync_single_range_for_device(struct device *dev,
314 dma_addr_t addr, unsigned long offset, size_t size,
315 enum dma_data_direction dir)
316 {
317 return dma_sync_single_for_device(dev, addr + offset, size, dir);
318 }
319
320 /**
321 * dma_map_sgtable - Map the given buffer for DMA
322 * @dev: The device for which to perform the DMA operation
323 * @sgt: The sg_table object describing the buffer
324 * @dir: DMA direction
325 * @attrs: Optional DMA attributes for the map operation
326 *
327 * Maps a buffer described by a scatterlist stored in the given sg_table
328 * object for the @dir DMA operation by the @dev device. After success the
329 * ownership for the buffer is transferred to the DMA domain. One has to
330 * call dma_sync_sgtable_for_cpu() or dma_unmap_sgtable() to move the
331 * ownership of the buffer back to the CPU domain before touching the
332 * buffer by the CPU.
333 *
334 * Returns 0 on success or -EINVAL on error during mapping the buffer.
335 */
dma_map_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)336 static inline int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
337 enum dma_data_direction dir, unsigned long attrs)
338 {
339 int nents;
340
341 nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
342 if (nents <= 0)
343 return -EINVAL;
344 sgt->nents = nents;
345 return 0;
346 }
347
348 /**
349 * dma_unmap_sgtable - Unmap the given buffer for DMA
350 * @dev: The device for which to perform the DMA operation
351 * @sgt: The sg_table object describing the buffer
352 * @dir: DMA direction
353 * @attrs: Optional DMA attributes for the unmap operation
354 *
355 * Unmaps a buffer described by a scatterlist stored in the given sg_table
356 * object for the @dir DMA operation by the @dev device. After this function
357 * the ownership of the buffer is transferred back to the CPU domain.
358 */
dma_unmap_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)359 static inline void dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
360 enum dma_data_direction dir, unsigned long attrs)
361 {
362 dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
363 }
364
365 /**
366 * dma_sync_sgtable_for_cpu - Synchronize the given buffer for CPU access
367 * @dev: The device for which to perform the DMA operation
368 * @sgt: The sg_table object describing the buffer
369 * @dir: DMA direction
370 *
371 * Performs the needed cache synchronization and moves the ownership of the
372 * buffer back to the CPU domain, so it is safe to perform any access to it
373 * by the CPU. Before doing any further DMA operations, one has to transfer
374 * the ownership of the buffer back to the DMA domain by calling the
375 * dma_sync_sgtable_for_device().
376 */
dma_sync_sgtable_for_cpu(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir)377 static inline void dma_sync_sgtable_for_cpu(struct device *dev,
378 struct sg_table *sgt, enum dma_data_direction dir)
379 {
380 dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir);
381 }
382
383 /**
384 * dma_sync_sgtable_for_device - Synchronize the given buffer for DMA
385 * @dev: The device for which to perform the DMA operation
386 * @sgt: The sg_table object describing the buffer
387 * @dir: DMA direction
388 *
389 * Performs the needed cache synchronization and moves the ownership of the
390 * buffer back to the DMA domain, so it is safe to perform the DMA operation.
391 * Once finished, one has to call dma_sync_sgtable_for_cpu() or
392 * dma_unmap_sgtable().
393 */
dma_sync_sgtable_for_device(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir)394 static inline void dma_sync_sgtable_for_device(struct device *dev,
395 struct sg_table *sgt, enum dma_data_direction dir)
396 {
397 dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir);
398 }
399
400 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
401 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
402 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
403 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
404 #define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
405 #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
406 #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
407 #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
408
dma_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)409 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
410 dma_addr_t *dma_handle, gfp_t gfp)
411 {
412
413 return dma_alloc_attrs(dev, size, dma_handle, gfp,
414 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
415 }
416
dma_free_coherent(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_handle)417 static inline void dma_free_coherent(struct device *dev, size_t size,
418 void *cpu_addr, dma_addr_t dma_handle)
419 {
420 return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
421 }
422
423
dma_get_mask(struct device * dev)424 static inline u64 dma_get_mask(struct device *dev)
425 {
426 if (dev->dma_mask && *dev->dma_mask)
427 return *dev->dma_mask;
428 return DMA_BIT_MASK(32);
429 }
430
431 /*
432 * Set both the DMA mask and the coherent DMA mask to the same thing.
433 * Note that we don't check the return value from dma_set_coherent_mask()
434 * as the DMA API guarantees that the coherent DMA mask can be set to
435 * the same or smaller than the streaming DMA mask.
436 */
dma_set_mask_and_coherent(struct device * dev,u64 mask)437 static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
438 {
439 int rc = dma_set_mask(dev, mask);
440 if (rc == 0)
441 dma_set_coherent_mask(dev, mask);
442 return rc;
443 }
444
445 /*
446 * Similar to the above, except it deals with the case where the device
447 * does not have dev->dma_mask appropriately setup.
448 */
dma_coerce_mask_and_coherent(struct device * dev,u64 mask)449 static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
450 {
451 dev->dma_mask = &dev->coherent_dma_mask;
452 return dma_set_mask_and_coherent(dev, mask);
453 }
454
455 /**
456 * dma_addressing_limited - return if the device is addressing limited
457 * @dev: device to check
458 *
459 * Return %true if the devices DMA mask is too small to address all memory in
460 * the system, else %false. Lack of addressing bits is the prime reason for
461 * bounce buffering, but might not be the only one.
462 */
dma_addressing_limited(struct device * dev)463 static inline bool dma_addressing_limited(struct device *dev)
464 {
465 return min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) <
466 dma_get_required_mask(dev);
467 }
468
dma_get_max_seg_size(struct device * dev)469 static inline unsigned int dma_get_max_seg_size(struct device *dev)
470 {
471 if (dev->dma_parms && dev->dma_parms->max_segment_size)
472 return dev->dma_parms->max_segment_size;
473 return SZ_64K;
474 }
475
dma_set_max_seg_size(struct device * dev,unsigned int size)476 static inline int dma_set_max_seg_size(struct device *dev, unsigned int size)
477 {
478 if (dev->dma_parms) {
479 dev->dma_parms->max_segment_size = size;
480 return 0;
481 }
482 return -EIO;
483 }
484
dma_get_seg_boundary(struct device * dev)485 static inline unsigned long dma_get_seg_boundary(struct device *dev)
486 {
487 if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
488 return dev->dma_parms->segment_boundary_mask;
489 return ULONG_MAX;
490 }
491
492 /**
493 * dma_get_seg_boundary_nr_pages - return the segment boundary in "page" units
494 * @dev: device to guery the boundary for
495 * @page_shift: ilog() of the IOMMU page size
496 *
497 * Return the segment boundary in IOMMU page units (which may be different from
498 * the CPU page size) for the passed in device.
499 *
500 * If @dev is NULL a boundary of U32_MAX is assumed, this case is just for
501 * non-DMA API callers.
502 */
dma_get_seg_boundary_nr_pages(struct device * dev,unsigned int page_shift)503 static inline unsigned long dma_get_seg_boundary_nr_pages(struct device *dev,
504 unsigned int page_shift)
505 {
506 if (!dev)
507 return (U32_MAX >> page_shift) + 1;
508 return (dma_get_seg_boundary(dev) >> page_shift) + 1;
509 }
510
dma_set_seg_boundary(struct device * dev,unsigned long mask)511 static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
512 {
513 if (dev->dma_parms) {
514 dev->dma_parms->segment_boundary_mask = mask;
515 return 0;
516 }
517 return -EIO;
518 }
519
dma_get_min_align_mask(struct device * dev)520 static inline unsigned int dma_get_min_align_mask(struct device *dev)
521 {
522 if (dev->dma_parms)
523 return dev->dma_parms->min_align_mask;
524 return 0;
525 }
526
dma_set_min_align_mask(struct device * dev,unsigned int min_align_mask)527 static inline int dma_set_min_align_mask(struct device *dev,
528 unsigned int min_align_mask)
529 {
530 if (WARN_ON_ONCE(!dev->dma_parms))
531 return -EIO;
532 dev->dma_parms->min_align_mask = min_align_mask;
533 return 0;
534 }
535
dma_get_cache_alignment(void)536 static inline int dma_get_cache_alignment(void)
537 {
538 #ifdef ARCH_DMA_MINALIGN
539 return ARCH_DMA_MINALIGN;
540 #endif
541 return 1;
542 }
543
dmam_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)544 static inline void *dmam_alloc_coherent(struct device *dev, size_t size,
545 dma_addr_t *dma_handle, gfp_t gfp)
546 {
547 return dmam_alloc_attrs(dev, size, dma_handle, gfp,
548 (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
549 }
550
dma_alloc_wc(struct device * dev,size_t size,dma_addr_t * dma_addr,gfp_t gfp)551 static inline void *dma_alloc_wc(struct device *dev, size_t size,
552 dma_addr_t *dma_addr, gfp_t gfp)
553 {
554 unsigned long attrs = DMA_ATTR_WRITE_COMBINE;
555
556 if (gfp & __GFP_NOWARN)
557 attrs |= DMA_ATTR_NO_WARN;
558
559 return dma_alloc_attrs(dev, size, dma_addr, gfp, attrs);
560 }
561
dma_free_wc(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr)562 static inline void dma_free_wc(struct device *dev, size_t size,
563 void *cpu_addr, dma_addr_t dma_addr)
564 {
565 return dma_free_attrs(dev, size, cpu_addr, dma_addr,
566 DMA_ATTR_WRITE_COMBINE);
567 }
568
dma_mmap_wc(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size)569 static inline int dma_mmap_wc(struct device *dev,
570 struct vm_area_struct *vma,
571 void *cpu_addr, dma_addr_t dma_addr,
572 size_t size)
573 {
574 return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
575 DMA_ATTR_WRITE_COMBINE);
576 }
577
578 #ifdef CONFIG_NEED_DMA_MAP_STATE
579 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
580 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
581 #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
582 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
583 #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
584 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
585 #else
586 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
587 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
588 #define dma_unmap_addr(PTR, ADDR_NAME) (0)
589 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
590 #define dma_unmap_len(PTR, LEN_NAME) (0)
591 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
592 #endif
593
594 /*
595 * Legacy interface to set up the dma offset map. Drivers really should not
596 * actually use it, but we have a few legacy cases left.
597 */
598 int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
599 dma_addr_t dma_start, u64 size);
600
601 extern const struct dma_map_ops dma_virt_ops;
602
603 #endif /* _LINUX_DMA_MAPPING_H */
604