1 #ifndef _LINUX_DMA_MAPPING_H
2 #define _LINUX_DMA_MAPPING_H
4 #include <linux/sizes.h>
5 #include <linux/string.h>
6 #include <linux/device.h>
8 #include <linux/dma-attrs.h>
9 #include <linux/dma-direction.h>
10 #include <linux/scatterlist.h>
13 * A dma_addr_t can hold any valid DMA or bus address for the platform.
14 * It can be given to a device to use as a DMA source or target. A CPU cannot
15 * reference a dma_addr_t directly because there may be translation between
16 * its physical address space and the bus address space.
19 void* (*alloc)(struct device *dev, size_t size,
20 dma_addr_t *dma_handle, gfp_t gfp,
21 struct dma_attrs *attrs);
22 void (*free)(struct device *dev, size_t size,
23 void *vaddr, dma_addr_t dma_handle,
24 struct dma_attrs *attrs);
25 int (*mmap)(struct device *, struct vm_area_struct *,
26 void *, dma_addr_t, size_t, struct dma_attrs *attrs);
28 int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
29 dma_addr_t, size_t, struct dma_attrs *attrs);
31 dma_addr_t (*map_page)(struct device *dev, struct page *page,
32 unsigned long offset, size_t size,
33 enum dma_data_direction dir,
34 struct dma_attrs *attrs);
35 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
36 size_t size, enum dma_data_direction dir,
37 struct dma_attrs *attrs);
39 * map_sg returns 0 on error and a value > 0 on success.
40 * It should never return a value < 0.
42 int (*map_sg)(struct device *dev, struct scatterlist *sg,
43 int nents, enum dma_data_direction dir,
44 struct dma_attrs *attrs);
45 void (*unmap_sg)(struct device *dev,
46 struct scatterlist *sg, int nents,
47 enum dma_data_direction dir,
48 struct dma_attrs *attrs);
49 void (*sync_single_for_cpu)(struct device *dev,
50 dma_addr_t dma_handle, size_t size,
51 enum dma_data_direction dir);
52 void (*sync_single_for_device)(struct device *dev,
53 dma_addr_t dma_handle, size_t size,
54 enum dma_data_direction dir);
55 void (*sync_sg_for_cpu)(struct device *dev,
56 struct scatterlist *sg, int nents,
57 enum dma_data_direction dir);
58 void (*sync_sg_for_device)(struct device *dev,
59 struct scatterlist *sg, int nents,
60 enum dma_data_direction dir);
61 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
62 int (*dma_supported)(struct device *dev, u64 mask);
63 int (*set_dma_mask)(struct device *dev, u64 mask);
64 void *(*remap)(struct device *dev, void *cpu_addr, dma_addr_t handle,
65 size_t size, struct dma_attrs *attrs);
66 void (*unremap)(struct device *dev, void *remapped_address,
68 #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
69 u64 (*get_required_mask)(struct device *dev);
74 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
76 #define DMA_MASK_NONE 0x0ULL
78 static inline int valid_dma_direction(int dma_direction)
80 return ((dma_direction == DMA_BIDIRECTIONAL) ||
81 (dma_direction == DMA_TO_DEVICE) ||
82 (dma_direction == DMA_FROM_DEVICE));
85 static inline int is_device_dma_capable(struct device *dev)
87 return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
91 #include <asm/dma-mapping.h>
93 #include <asm-generic/dma-mapping-broken.h>
97 static inline void *dma_remap(struct device *dev, void *cpu_addr,
98 dma_addr_t dma_handle, size_t size, struct dma_attrs *attrs)
100 const struct dma_map_ops *ops = get_dma_ops(dev);
104 WARN_ONCE(1, "Remap function not implemented for %pS\n",
109 return ops->remap(dev, cpu_addr, dma_handle, size, attrs);
113 static inline void dma_unremap(struct device *dev, void *remapped_addr,
116 const struct dma_map_ops *ops = get_dma_ops(dev);
120 WARN_ONCE(1, "unremap function not implemented for %pS\n",
125 return ops->unremap(dev, remapped_addr, size);
130 static inline u64 dma_get_mask(struct device *dev)
132 if (dev && dev->dma_mask && *dev->dma_mask)
133 return *dev->dma_mask;
134 return DMA_BIT_MASK(32);
137 #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
138 int dma_set_coherent_mask(struct device *dev, u64 mask);
140 static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
142 if (!dma_supported(dev, mask))
144 dev->coherent_dma_mask = mask;
150 * Set both the DMA mask and the coherent DMA mask to the same thing.
151 * Note that we don't check the return value from dma_set_coherent_mask()
152 * as the DMA API guarantees that the coherent DMA mask can be set to
153 * the same or smaller than the streaming DMA mask.
155 static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
157 int rc = dma_set_mask(dev, mask);
159 dma_set_coherent_mask(dev, mask);
164 * Similar to the above, except it deals with the case where the device
165 * does not have dev->dma_mask appropriately setup.
167 static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
169 dev->dma_mask = &dev->coherent_dma_mask;
170 return dma_set_mask_and_coherent(dev, mask);
173 extern u64 dma_get_required_mask(struct device *dev);
175 #ifndef arch_setup_dma_ops
176 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
177 u64 size, struct iommu_ops *iommu,
181 #ifndef arch_teardown_dma_ops
182 static inline void arch_teardown_dma_ops(struct device *dev) { }
185 static inline unsigned int dma_get_max_seg_size(struct device *dev)
187 if (dev->dma_parms && dev->dma_parms->max_segment_size)
188 return dev->dma_parms->max_segment_size;
192 static inline unsigned int dma_set_max_seg_size(struct device *dev,
195 if (dev->dma_parms) {
196 dev->dma_parms->max_segment_size = size;
202 static inline unsigned long dma_get_seg_boundary(struct device *dev)
204 if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
205 return dev->dma_parms->segment_boundary_mask;
206 return DMA_BIT_MASK(32);
209 static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
211 if (dev->dma_parms) {
212 dev->dma_parms->segment_boundary_mask = mask;
219 static inline unsigned long dma_max_pfn(struct device *dev)
221 return *dev->dma_mask >> PAGE_SHIFT;
225 static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
226 dma_addr_t *dma_handle, gfp_t flag)
228 void *ret = dma_alloc_coherent(dev, size, dma_handle,
233 #ifdef CONFIG_HAS_DMA
234 static inline int dma_get_cache_alignment(void)
236 #ifdef ARCH_DMA_MINALIGN
237 return ARCH_DMA_MINALIGN;
243 /* flags for the coherent memory api */
244 #define DMA_MEMORY_MAP 0x01
245 #define DMA_MEMORY_IO 0x02
246 #define DMA_MEMORY_INCLUDES_CHILDREN 0x04
247 #define DMA_MEMORY_EXCLUSIVE 0x08
249 #ifndef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
251 dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
252 dma_addr_t device_addr, size_t size, int flags)
258 dma_release_declared_memory(struct device *dev)
263 dma_mark_declared_memory_occupied(struct device *dev,
264 dma_addr_t device_addr, size_t size)
266 return ERR_PTR(-EBUSY);
273 extern void *dmam_alloc_coherent(struct device *dev, size_t size,
274 dma_addr_t *dma_handle, gfp_t gfp);
275 extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
276 dma_addr_t dma_handle);
277 extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
278 dma_addr_t *dma_handle, gfp_t gfp);
279 extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
280 dma_addr_t dma_handle);
281 #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
282 extern int dmam_declare_coherent_memory(struct device *dev,
283 phys_addr_t phys_addr,
284 dma_addr_t device_addr, size_t size,
286 extern void dmam_release_declared_memory(struct device *dev);
287 #else /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
288 static inline int dmam_declare_coherent_memory(struct device *dev,
289 phys_addr_t phys_addr, dma_addr_t device_addr,
290 size_t size, gfp_t gfp)
295 static inline void dmam_release_declared_memory(struct device *dev)
298 #endif /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
300 #ifndef CONFIG_HAVE_DMA_ATTRS
303 #define dma_map_single_attrs(dev, cpu_addr, size, dir, attrs) \
304 dma_map_single(dev, cpu_addr, size, dir)
306 #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
307 dma_unmap_single(dev, dma_addr, size, dir)
309 #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
310 dma_map_sg(dev, sgl, nents, dir)
312 #define dma_unmap_sg_attrs(dev, sgl, nents, dir, attrs) \
313 dma_unmap_sg(dev, sgl, nents, dir)
316 static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
317 dma_addr_t *dma_addr, gfp_t gfp)
319 DEFINE_DMA_ATTRS(attrs);
320 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
321 return dma_alloc_attrs(dev, size, dma_addr, gfp, &attrs);
324 static inline void dma_free_writecombine(struct device *dev, size_t size,
325 void *cpu_addr, dma_addr_t dma_addr)
327 DEFINE_DMA_ATTRS(attrs);
328 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
329 return dma_free_attrs(dev, size, cpu_addr, dma_addr, &attrs);
332 static inline int dma_mmap_writecombine(struct device *dev,
333 struct vm_area_struct *vma,
334 void *cpu_addr, dma_addr_t dma_addr,
337 DEFINE_DMA_ATTRS(attrs);
338 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
339 return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs);
341 #endif /* CONFIG_HAVE_DMA_ATTRS */
343 #ifdef CONFIG_NEED_DMA_MAP_STATE
344 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
345 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
346 #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
347 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
348 #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
349 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
351 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
352 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
353 #define dma_unmap_addr(PTR, ADDR_NAME) (0)
354 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
355 #define dma_unmap_len(PTR, LEN_NAME) (0)
356 #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)