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