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