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>
10 #include <linux/dma-debug.h>
11 #include <linux/dma-attrs.h>
13 #include <asm/swiotlb.h>
14 #include <asm-generic/dma-coherent.h>
16 extern dma_addr_t bad_dma_address
;
17 extern int iommu_merge
;
18 extern struct device x86_dma_fallback_dev
;
19 extern int panic_on_overflow
;
21 extern struct dma_map_ops
*dma_ops
;
23 static inline struct dma_map_ops
*get_dma_ops(struct device
*dev
)
28 if (unlikely(!dev
) || !dev
->archdata
.dma_ops
)
31 return dev
->archdata
.dma_ops
;
35 /* Make sure we keep the same behaviour */
36 static inline int dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
38 struct dma_map_ops
*ops
= get_dma_ops(dev
);
39 if (ops
->mapping_error
)
40 return ops
->mapping_error(dev
, dma_addr
);
42 return (dma_addr
== bad_dma_address
);
45 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
46 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
47 #define dma_is_consistent(d, h) (1)
49 extern int dma_supported(struct device
*hwdev
, u64 mask
);
50 extern int dma_set_mask(struct device
*dev
, u64 mask
);
52 extern void *dma_generic_alloc_coherent(struct device
*dev
, size_t size
,
53 dma_addr_t
*dma_addr
, gfp_t flag
);
55 static inline dma_addr_t
56 dma_map_single(struct device
*hwdev
, void *ptr
, size_t size
,
57 enum dma_data_direction dir
)
59 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
62 BUG_ON(!valid_dma_direction(dir
));
63 addr
= ops
->map_page(hwdev
, virt_to_page(ptr
),
64 (unsigned long)ptr
& ~PAGE_MASK
, size
,
66 debug_dma_map_page(hwdev
, virt_to_page(ptr
),
67 (unsigned long)ptr
& ~PAGE_MASK
, size
,
73 dma_unmap_single(struct device
*dev
, dma_addr_t addr
, size_t size
,
74 enum dma_data_direction dir
)
76 struct dma_map_ops
*ops
= get_dma_ops(dev
);
78 BUG_ON(!valid_dma_direction(dir
));
80 ops
->unmap_page(dev
, addr
, size
, dir
, NULL
);
81 debug_dma_unmap_page(dev
, addr
, size
, dir
, true);
85 dma_map_sg(struct device
*hwdev
, struct scatterlist
*sg
,
86 int nents
, enum dma_data_direction dir
)
88 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
91 BUG_ON(!valid_dma_direction(dir
));
92 ents
= ops
->map_sg(hwdev
, sg
, nents
, dir
, NULL
);
93 debug_dma_map_sg(hwdev
, sg
, nents
, ents
, dir
);
99 dma_unmap_sg(struct device
*hwdev
, struct scatterlist
*sg
, int nents
,
100 enum dma_data_direction dir
)
102 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
104 BUG_ON(!valid_dma_direction(dir
));
105 debug_dma_unmap_sg(hwdev
, sg
, nents
, dir
);
107 ops
->unmap_sg(hwdev
, sg
, nents
, dir
, NULL
);
111 dma_sync_single_for_cpu(struct device
*hwdev
, dma_addr_t dma_handle
,
112 size_t size
, enum dma_data_direction dir
)
114 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
116 BUG_ON(!valid_dma_direction(dir
));
117 if (ops
->sync_single_for_cpu
)
118 ops
->sync_single_for_cpu(hwdev
, dma_handle
, size
, dir
);
119 debug_dma_sync_single_for_cpu(hwdev
, dma_handle
, size
, dir
);
120 flush_write_buffers();
124 dma_sync_single_for_device(struct device
*hwdev
, dma_addr_t dma_handle
,
125 size_t size
, enum dma_data_direction dir
)
127 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
129 BUG_ON(!valid_dma_direction(dir
));
130 if (ops
->sync_single_for_device
)
131 ops
->sync_single_for_device(hwdev
, dma_handle
, size
, dir
);
132 debug_dma_sync_single_for_device(hwdev
, dma_handle
, size
, dir
);
133 flush_write_buffers();
137 dma_sync_single_range_for_cpu(struct device
*hwdev
, dma_addr_t dma_handle
,
138 unsigned long offset
, size_t size
,
139 enum dma_data_direction dir
)
141 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
143 BUG_ON(!valid_dma_direction(dir
));
144 if (ops
->sync_single_range_for_cpu
)
145 ops
->sync_single_range_for_cpu(hwdev
, dma_handle
, offset
,
147 debug_dma_sync_single_range_for_cpu(hwdev
, dma_handle
,
149 flush_write_buffers();
153 dma_sync_single_range_for_device(struct device
*hwdev
, dma_addr_t dma_handle
,
154 unsigned long offset
, size_t size
,
155 enum dma_data_direction dir
)
157 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
159 BUG_ON(!valid_dma_direction(dir
));
160 if (ops
->sync_single_range_for_device
)
161 ops
->sync_single_range_for_device(hwdev
, dma_handle
,
163 debug_dma_sync_single_range_for_device(hwdev
, dma_handle
,
165 flush_write_buffers();
169 dma_sync_sg_for_cpu(struct device
*hwdev
, struct scatterlist
*sg
,
170 int nelems
, enum dma_data_direction dir
)
172 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
174 BUG_ON(!valid_dma_direction(dir
));
175 if (ops
->sync_sg_for_cpu
)
176 ops
->sync_sg_for_cpu(hwdev
, sg
, nelems
, dir
);
177 debug_dma_sync_sg_for_cpu(hwdev
, sg
, nelems
, dir
);
178 flush_write_buffers();
182 dma_sync_sg_for_device(struct device
*hwdev
, struct scatterlist
*sg
,
183 int nelems
, enum dma_data_direction dir
)
185 struct dma_map_ops
*ops
= get_dma_ops(hwdev
);
187 BUG_ON(!valid_dma_direction(dir
));
188 if (ops
->sync_sg_for_device
)
189 ops
->sync_sg_for_device(hwdev
, sg
, nelems
, dir
);
190 debug_dma_sync_sg_for_device(hwdev
, sg
, nelems
, dir
);
192 flush_write_buffers();
195 static inline dma_addr_t
dma_map_page(struct device
*dev
, struct page
*page
,
196 size_t offset
, size_t size
,
197 enum dma_data_direction dir
)
199 struct dma_map_ops
*ops
= get_dma_ops(dev
);
202 BUG_ON(!valid_dma_direction(dir
));
203 addr
= ops
->map_page(dev
, page
, offset
, size
, dir
, NULL
);
204 debug_dma_map_page(dev
, page
, offset
, size
, dir
, addr
, false);
209 static inline void dma_unmap_page(struct device
*dev
, dma_addr_t addr
,
210 size_t size
, enum dma_data_direction dir
)
212 struct dma_map_ops
*ops
= get_dma_ops(dev
);
214 BUG_ON(!valid_dma_direction(dir
));
216 ops
->unmap_page(dev
, addr
, size
, dir
, NULL
);
217 debug_dma_unmap_page(dev
, addr
, size
, dir
, false);
221 dma_cache_sync(struct device
*dev
, void *vaddr
, size_t size
,
222 enum dma_data_direction dir
)
224 flush_write_buffers();
227 static inline int dma_get_cache_alignment(void)
229 /* no easy way to get cache size on all x86, so return the
230 * maximum possible, to be safe */
231 return boot_cpu_data
.x86_clflush_size
;
234 static inline unsigned long dma_alloc_coherent_mask(struct device
*dev
,
237 unsigned long dma_mask
= 0;
239 dma_mask
= dev
->coherent_dma_mask
;
241 dma_mask
= (gfp
& GFP_DMA
) ? DMA_BIT_MASK(24) : DMA_BIT_MASK(32);
246 static inline gfp_t
dma_alloc_coherent_gfp_flags(struct device
*dev
, gfp_t gfp
)
248 unsigned long dma_mask
= dma_alloc_coherent_mask(dev
, gfp
);
250 if (dma_mask
<= DMA_BIT_MASK(24))
253 if (dma_mask
<= DMA_BIT_MASK(32) && !(gfp
& GFP_DMA
))
260 dma_alloc_coherent(struct device
*dev
, size_t size
, dma_addr_t
*dma_handle
,
263 struct dma_map_ops
*ops
= get_dma_ops(dev
);
266 gfp
&= ~(__GFP_DMA
| __GFP_HIGHMEM
| __GFP_DMA32
);
268 if (dma_alloc_from_coherent(dev
, size
, dma_handle
, &memory
))
272 dev
= &x86_dma_fallback_dev
;
276 if (!is_device_dma_capable(dev
))
279 if (!ops
->alloc_coherent
)
282 memory
= ops
->alloc_coherent(dev
, size
, dma_handle
,
283 dma_alloc_coherent_gfp_flags(dev
, gfp
));
284 debug_dma_alloc_coherent(dev
, size
, *dma_handle
, memory
);
289 static inline void dma_free_coherent(struct device
*dev
, size_t size
,
290 void *vaddr
, dma_addr_t bus
)
292 struct dma_map_ops
*ops
= get_dma_ops(dev
);
294 WARN_ON(irqs_disabled()); /* for portability */
296 if (dma_release_from_coherent(dev
, get_order(size
), vaddr
))
299 debug_dma_free_coherent(dev
, size
, vaddr
, bus
);
300 if (ops
->free_coherent
)
301 ops
->free_coherent(dev
, size
, vaddr
, bus
);