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/kmemcheck.h>
10 #include <linux/scatterlist.h>
11 #include <linux/dma-debug.h>
12 #include <linux/dma-attrs.h>
14 #include <asm/swiotlb.h>
15 #include <asm-generic/dma-coherent.h>
17 extern dma_addr_t bad_dma_address;
18 extern int iommu_merge;
19 extern struct device x86_dma_fallback_dev;
20 extern int panic_on_overflow;
22 extern struct dma_map_ops *dma_ops;
24 static inline struct dma_map_ops *get_dma_ops(struct device *dev)
29 if (unlikely(!dev) || !dev->archdata.dma_ops)
32 return dev->archdata.dma_ops;
36 /* Make sure we keep the same behaviour */
37 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
39 struct dma_map_ops *ops = get_dma_ops(dev);
40 if (ops->mapping_error)
41 return ops->mapping_error(dev, dma_addr);
43 return (dma_addr == bad_dma_address);
46 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
47 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
48 #define dma_is_consistent(d, h) (1)
50 extern int dma_supported(struct device *hwdev, u64 mask);
51 extern int dma_set_mask(struct device *dev, u64 mask);
53 extern void *dma_generic_alloc_coherent(struct device *dev, size_t size,
54 dma_addr_t *dma_addr, gfp_t flag);
56 static inline dma_addr_t
57 dma_map_single(struct device *hwdev, void *ptr, size_t size,
58 enum dma_data_direction dir)
60 struct dma_map_ops *ops = get_dma_ops(hwdev);
63 BUG_ON(!valid_dma_direction(dir));
64 kmemcheck_mark_initialized(ptr, size);
65 addr = ops->map_page(hwdev, virt_to_page(ptr),
66 (unsigned long)ptr & ~PAGE_MASK, size,
68 debug_dma_map_page(hwdev, virt_to_page(ptr),
69 (unsigned long)ptr & ~PAGE_MASK, size,
75 dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
76 enum dma_data_direction dir)
78 struct dma_map_ops *ops = get_dma_ops(dev);
80 BUG_ON(!valid_dma_direction(dir));
82 ops->unmap_page(dev, addr, size, dir, NULL);
83 debug_dma_unmap_page(dev, addr, size, dir, true);
87 dma_map_sg(struct device *hwdev, struct scatterlist *sg,
88 int nents, enum dma_data_direction dir)
90 struct dma_map_ops *ops = get_dma_ops(hwdev);
92 struct scatterlist *s;
95 BUG_ON(!valid_dma_direction(dir));
96 for_each_sg(sg, s, nents, i)
97 kmemcheck_mark_initialized(sg_virt(s), s->length);
98 ents = ops->map_sg(hwdev, sg, nents, dir, NULL);
99 debug_dma_map_sg(hwdev, sg, nents, ents, dir);
105 dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
106 enum dma_data_direction dir)
108 struct dma_map_ops *ops = get_dma_ops(hwdev);
110 BUG_ON(!valid_dma_direction(dir));
111 debug_dma_unmap_sg(hwdev, sg, nents, dir);
113 ops->unmap_sg(hwdev, sg, nents, dir, NULL);
117 dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
118 size_t size, enum dma_data_direction dir)
120 struct dma_map_ops *ops = get_dma_ops(hwdev);
122 BUG_ON(!valid_dma_direction(dir));
123 if (ops->sync_single_for_cpu)
124 ops->sync_single_for_cpu(hwdev, dma_handle, size, dir);
125 debug_dma_sync_single_for_cpu(hwdev, dma_handle, size, dir);
126 flush_write_buffers();
130 dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
131 size_t size, enum dma_data_direction dir)
133 struct dma_map_ops *ops = get_dma_ops(hwdev);
135 BUG_ON(!valid_dma_direction(dir));
136 if (ops->sync_single_for_device)
137 ops->sync_single_for_device(hwdev, dma_handle, size, dir);
138 debug_dma_sync_single_for_device(hwdev, dma_handle, size, dir);
139 flush_write_buffers();
143 dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
144 unsigned long offset, size_t size,
145 enum dma_data_direction dir)
147 struct dma_map_ops *ops = get_dma_ops(hwdev);
149 BUG_ON(!valid_dma_direction(dir));
150 if (ops->sync_single_range_for_cpu)
151 ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
153 debug_dma_sync_single_range_for_cpu(hwdev, dma_handle,
155 flush_write_buffers();
159 dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
160 unsigned long offset, size_t size,
161 enum dma_data_direction dir)
163 struct dma_map_ops *ops = get_dma_ops(hwdev);
165 BUG_ON(!valid_dma_direction(dir));
166 if (ops->sync_single_range_for_device)
167 ops->sync_single_range_for_device(hwdev, dma_handle,
169 debug_dma_sync_single_range_for_device(hwdev, dma_handle,
171 flush_write_buffers();
175 dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
176 int nelems, enum dma_data_direction dir)
178 struct dma_map_ops *ops = get_dma_ops(hwdev);
180 BUG_ON(!valid_dma_direction(dir));
181 if (ops->sync_sg_for_cpu)
182 ops->sync_sg_for_cpu(hwdev, sg, nelems, dir);
183 debug_dma_sync_sg_for_cpu(hwdev, sg, nelems, dir);
184 flush_write_buffers();
188 dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
189 int nelems, enum dma_data_direction dir)
191 struct dma_map_ops *ops = get_dma_ops(hwdev);
193 BUG_ON(!valid_dma_direction(dir));
194 if (ops->sync_sg_for_device)
195 ops->sync_sg_for_device(hwdev, sg, nelems, dir);
196 debug_dma_sync_sg_for_device(hwdev, sg, nelems, dir);
198 flush_write_buffers();
201 static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
202 size_t offset, size_t size,
203 enum dma_data_direction dir)
205 struct dma_map_ops *ops = get_dma_ops(dev);
208 BUG_ON(!valid_dma_direction(dir));
209 kmemcheck_mark_initialized(page_address(page) + offset, size);
210 addr = ops->map_page(dev, page, offset, size, dir, NULL);
211 debug_dma_map_page(dev, page, offset, size, dir, addr, false);
216 static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
217 size_t size, enum dma_data_direction dir)
219 struct dma_map_ops *ops = get_dma_ops(dev);
221 BUG_ON(!valid_dma_direction(dir));
223 ops->unmap_page(dev, addr, size, dir, NULL);
224 debug_dma_unmap_page(dev, addr, size, dir, false);
228 dma_cache_sync(struct device *dev, void *vaddr, size_t size,
229 enum dma_data_direction dir)
231 flush_write_buffers();
234 static inline int dma_get_cache_alignment(void)
236 /* no easy way to get cache size on all x86, so return the
237 * maximum possible, to be safe */
238 return boot_cpu_data.x86_clflush_size;
241 static inline unsigned long dma_alloc_coherent_mask(struct device *dev,
244 unsigned long dma_mask = 0;
246 dma_mask = dev->coherent_dma_mask;
248 dma_mask = (gfp & GFP_DMA) ? DMA_BIT_MASK(24) : DMA_BIT_MASK(32);
253 static inline gfp_t dma_alloc_coherent_gfp_flags(struct device *dev, gfp_t gfp)
255 unsigned long dma_mask = dma_alloc_coherent_mask(dev, gfp);
257 if (dma_mask <= DMA_BIT_MASK(24))
260 if (dma_mask <= DMA_BIT_MASK(32) && !(gfp & GFP_DMA))
267 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
270 struct dma_map_ops *ops = get_dma_ops(dev);
273 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
275 if (dma_alloc_from_coherent(dev, size, dma_handle, &memory))
279 dev = &x86_dma_fallback_dev;
283 if (!is_device_dma_capable(dev))
286 if (!ops->alloc_coherent)
289 memory = ops->alloc_coherent(dev, size, dma_handle,
290 dma_alloc_coherent_gfp_flags(dev, gfp));
291 debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
296 static inline void dma_free_coherent(struct device *dev, size_t size,
297 void *vaddr, dma_addr_t bus)
299 struct dma_map_ops *ops = get_dma_ops(dev);
301 WARN_ON(irqs_disabled()); /* for portability */
303 if (dma_release_from_coherent(dev, get_order(size), vaddr))
306 debug_dma_free_coherent(dev, size, vaddr, bus);
307 if (ops->free_coherent)
308 ops->free_coherent(dev, size, vaddr, bus);