1 #ifndef _ASM_X86_DMA_MAPPING_H
2 #define _ASM_X86_DMA_MAPPING_H
5 * IOMMU interface. See Documentation/PCI/PCI-DMA-mapping.txt and
6 * Documentation/DMA-API.txt for documentation.
9 #include <linux/scatterlist.h>
11 #include <asm/swiotlb.h>
12 #include <asm-generic/dma-coherent.h>
14 extern dma_addr_t bad_dma_address;
15 extern int iommu_merge;
16 extern struct device x86_dma_fallback_dev;
17 extern int panic_on_overflow;
19 struct dma_mapping_ops {
20 int (*mapping_error)(struct device *dev,
22 void* (*alloc_coherent)(struct device *dev, size_t size,
23 dma_addr_t *dma_handle, gfp_t gfp);
24 void (*free_coherent)(struct device *dev, size_t size,
25 void *vaddr, dma_addr_t dma_handle);
26 dma_addr_t (*map_single)(struct device *hwdev, phys_addr_t ptr,
27 size_t size, int direction);
28 void (*unmap_single)(struct device *dev, dma_addr_t addr,
29 size_t size, int direction);
30 void (*sync_single_for_cpu)(struct device *hwdev,
31 dma_addr_t dma_handle, size_t size,
33 void (*sync_single_for_device)(struct device *hwdev,
34 dma_addr_t dma_handle, size_t size,
36 void (*sync_single_range_for_cpu)(struct device *hwdev,
37 dma_addr_t dma_handle, unsigned long offset,
38 size_t size, int direction);
39 void (*sync_single_range_for_device)(struct device *hwdev,
40 dma_addr_t dma_handle, unsigned long offset,
41 size_t size, int direction);
42 void (*sync_sg_for_cpu)(struct device *hwdev,
43 struct scatterlist *sg, int nelems,
45 void (*sync_sg_for_device)(struct device *hwdev,
46 struct scatterlist *sg, int nelems,
48 int (*map_sg)(struct device *hwdev, struct scatterlist *sg,
49 int nents, int direction);
50 void (*unmap_sg)(struct device *hwdev,
51 struct scatterlist *sg, int nents,
53 int (*dma_supported)(struct device *hwdev, u64 mask);
57 extern struct dma_mapping_ops *dma_ops;
59 static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
64 if (unlikely(!dev) || !dev->archdata.dma_ops)
67 return dev->archdata.dma_ops;
71 /* Make sure we keep the same behaviour */
72 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
74 struct dma_mapping_ops *ops = get_dma_ops(dev);
75 if (ops->mapping_error)
76 return ops->mapping_error(dev, dma_addr);
78 return (dma_addr == bad_dma_address);
81 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
82 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
83 #define dma_is_consistent(d, h) (1)
85 extern int dma_supported(struct device *hwdev, u64 mask);
86 extern int dma_set_mask(struct device *dev, u64 mask);
88 extern void *dma_generic_alloc_coherent(struct device *dev, size_t size,
89 dma_addr_t *dma_addr, gfp_t flag);
91 static inline dma_addr_t
92 dma_map_single(struct device *hwdev, void *ptr, size_t size,
95 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
97 BUG_ON(!valid_dma_direction(direction));
98 return ops->map_single(hwdev, virt_to_phys(ptr), size, direction);
102 dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
105 struct dma_mapping_ops *ops = get_dma_ops(dev);
107 BUG_ON(!valid_dma_direction(direction));
108 if (ops->unmap_single)
109 ops->unmap_single(dev, addr, size, direction);
113 dma_map_sg(struct device *hwdev, struct scatterlist *sg,
114 int nents, int direction)
116 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
118 BUG_ON(!valid_dma_direction(direction));
119 return ops->map_sg(hwdev, sg, nents, direction);
123 dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
126 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
128 BUG_ON(!valid_dma_direction(direction));
130 ops->unmap_sg(hwdev, sg, nents, direction);
134 dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
135 size_t size, int direction)
137 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
139 BUG_ON(!valid_dma_direction(direction));
140 if (ops->sync_single_for_cpu)
141 ops->sync_single_for_cpu(hwdev, dma_handle, size, direction);
142 flush_write_buffers();
146 dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
147 size_t size, int direction)
149 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
151 BUG_ON(!valid_dma_direction(direction));
152 if (ops->sync_single_for_device)
153 ops->sync_single_for_device(hwdev, dma_handle, size, direction);
154 flush_write_buffers();
158 dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
159 unsigned long offset, size_t size, int direction)
161 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
163 BUG_ON(!valid_dma_direction(direction));
164 if (ops->sync_single_range_for_cpu)
165 ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
167 flush_write_buffers();
171 dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
172 unsigned long offset, size_t size,
175 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
177 BUG_ON(!valid_dma_direction(direction));
178 if (ops->sync_single_range_for_device)
179 ops->sync_single_range_for_device(hwdev, dma_handle,
180 offset, size, direction);
181 flush_write_buffers();
185 dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
186 int nelems, int direction)
188 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
190 BUG_ON(!valid_dma_direction(direction));
191 if (ops->sync_sg_for_cpu)
192 ops->sync_sg_for_cpu(hwdev, sg, nelems, direction);
193 flush_write_buffers();
197 dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
198 int nelems, int direction)
200 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
202 BUG_ON(!valid_dma_direction(direction));
203 if (ops->sync_sg_for_device)
204 ops->sync_sg_for_device(hwdev, sg, nelems, direction);
206 flush_write_buffers();
209 static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
210 size_t offset, size_t size,
213 struct dma_mapping_ops *ops = get_dma_ops(dev);
215 BUG_ON(!valid_dma_direction(direction));
216 return ops->map_single(dev, page_to_phys(page) + offset,
220 static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
221 size_t size, int direction)
223 dma_unmap_single(dev, addr, size, direction);
227 dma_cache_sync(struct device *dev, void *vaddr, size_t size,
228 enum dma_data_direction dir)
230 flush_write_buffers();
233 static inline int dma_get_cache_alignment(void)
235 /* no easy way to get cache size on all x86, so return the
236 * maximum possible, to be safe */
237 return boot_cpu_data.x86_clflush_size;
240 static inline unsigned long dma_alloc_coherent_mask(struct device *dev,
243 unsigned long dma_mask = 0;
245 dma_mask = dev->coherent_dma_mask;
247 dma_mask = (gfp & GFP_DMA) ? DMA_24BIT_MASK : DMA_32BIT_MASK;
252 static inline gfp_t dma_alloc_coherent_gfp_flags(struct device *dev, gfp_t gfp)
254 unsigned long dma_mask = dma_alloc_coherent_mask(dev, gfp);
256 if (dma_mask <= DMA_24BIT_MASK)
259 if (dma_mask <= DMA_32BIT_MASK && !(gfp & GFP_DMA))
266 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
269 struct dma_mapping_ops *ops = get_dma_ops(dev);
272 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
274 if (dma_alloc_from_coherent(dev, size, dma_handle, &memory))
278 dev = &x86_dma_fallback_dev;
282 if (!is_device_dma_capable(dev))
285 if (!ops->alloc_coherent)
288 return ops->alloc_coherent(dev, size, dma_handle,
289 dma_alloc_coherent_gfp_flags(dev, gfp));
292 static inline void dma_free_coherent(struct device *dev, size_t size,
293 void *vaddr, dma_addr_t bus)
295 struct dma_mapping_ops *ops = get_dma_ops(dev);
297 WARN_ON(irqs_disabled()); /* for portability */
299 if (dma_release_from_coherent(dev, get_order(size), vaddr))
302 if (ops->free_coherent)
303 ops->free_coherent(dev, size, vaddr, bus);