blob: 3b43a65894c4ac44c5c96e6a736d12ec1ceeb664 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_DMA_MAPPING_H
2#define _ASM_X86_DMA_MAPPING_H
Glauber Costa6f536632008-03-25 18:36:20 -03003
4/*
5 * IOMMU interface. See Documentation/DMA-mapping.txt and DMA-API.txt for
6 * documentation.
7 */
8
9#include <linux/scatterlist.h>
10#include <asm/io.h>
11#include <asm/swiotlb.h>
Joerg Roedel6c505ce2008-08-19 16:32:45 +020012#include <asm-generic/dma-coherent.h>
Glauber Costa6f536632008-03-25 18:36:20 -030013
Glauber Costa7c183412008-03-25 18:36:36 -030014extern dma_addr_t bad_dma_address;
Glauber Costab7107a32008-03-25 18:36:39 -030015extern int iommu_merge;
Joerg Roedel6c505ce2008-08-19 16:32:45 +020016extern struct device x86_dma_fallback_dev;
Glauber Costab7107a32008-03-25 18:36:39 -030017extern int panic_on_overflow;
Glauber Costa7c183412008-03-25 18:36:36 -030018
Glauber Costa6f536632008-03-25 18:36:20 -030019struct dma_mapping_ops {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070020 int (*mapping_error)(struct device *dev,
21 dma_addr_t dma_addr);
Glauber Costa6f536632008-03-25 18:36:20 -030022 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);
Ingo Molnar2be62142008-04-19 19:19:56 +020026 dma_addr_t (*map_single)(struct device *hwdev, phys_addr_t ptr,
Glauber Costa6f536632008-03-25 18:36:20 -030027 size_t size, int direction);
Glauber Costa6f536632008-03-25 18:36:20 -030028 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,
32 int direction);
33 void (*sync_single_for_device)(struct device *hwdev,
34 dma_addr_t dma_handle, size_t size,
35 int direction);
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,
44 int direction);
45 void (*sync_sg_for_device)(struct device *hwdev,
46 struct scatterlist *sg, int nelems,
47 int direction);
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,
52 int direction);
53 int (*dma_supported)(struct device *hwdev, u64 mask);
54 int is_phys;
55};
56
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070057extern struct dma_mapping_ops *dma_ops;
Glauber Costa22456b92008-03-25 18:36:21 -030058
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070059static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
Glauber Costac786df02008-03-25 18:36:37 -030060{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070061#ifdef CONFIG_X86_32
62 return dma_ops;
63#else
64 if (unlikely(!dev) || !dev->archdata.dma_ops)
65 return dma_ops;
66 else
67 return dev->archdata.dma_ops;
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080068#endif
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070069}
70
71/* Make sure we keep the same behaviour */
72static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
73{
Thomas Bogendoerfer7b1dedc2008-11-29 13:46:27 +010074#ifdef CONFIG_X86_64
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070075 struct dma_mapping_ops *ops = get_dma_ops(dev);
76 if (ops->mapping_error)
77 return ops->mapping_error(dev, dma_addr);
Glauber Costac786df02008-03-25 18:36:37 -030078
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070079#endif
Thomas Bogendoerfer7b1dedc2008-11-29 13:46:27 +010080 return (dma_addr == bad_dma_address);
Glauber Costac786df02008-03-25 18:36:37 -030081}
82
Glauber Costa8d396de2008-03-25 18:36:31 -030083#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
84#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
Joerg Roedel6c505ce2008-08-19 16:32:45 +020085#define dma_is_consistent(d, h) (1)
Glauber Costa8d396de2008-03-25 18:36:31 -030086
Glauber Costa802c1f62008-03-25 18:36:34 -030087extern int dma_supported(struct device *hwdev, u64 mask);
88extern int dma_set_mask(struct device *dev, u64 mask);
89
FUJITA Tomonori9f6ac572008-09-24 20:48:35 +090090extern void *dma_generic_alloc_coherent(struct device *dev, size_t size,
91 dma_addr_t *dma_addr, gfp_t flag);
92
Glauber Costa22456b92008-03-25 18:36:21 -030093static inline dma_addr_t
94dma_map_single(struct device *hwdev, void *ptr, size_t size,
95 int direction)
96{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070097 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
98
Glauber Costa22456b92008-03-25 18:36:21 -030099 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700100 return ops->map_single(hwdev, virt_to_phys(ptr), size, direction);
Glauber Costa22456b92008-03-25 18:36:21 -0300101}
102
Glauber Costa0cb0ae62008-03-25 18:36:22 -0300103static inline void
104dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
105 int direction)
106{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700107 struct dma_mapping_ops *ops = get_dma_ops(dev);
108
Glauber Costa0cb0ae62008-03-25 18:36:22 -0300109 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700110 if (ops->unmap_single)
111 ops->unmap_single(dev, addr, size, direction);
Glauber Costa0cb0ae62008-03-25 18:36:22 -0300112}
113
Glauber Costa16a3ce92008-03-25 18:36:23 -0300114static inline int
115dma_map_sg(struct device *hwdev, struct scatterlist *sg,
116 int nents, int direction)
117{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700118 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
119
Glauber Costa16a3ce92008-03-25 18:36:23 -0300120 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700121 return ops->map_sg(hwdev, sg, nents, direction);
Glauber Costa16a3ce92008-03-25 18:36:23 -0300122}
Glauber Costa72c784f2008-03-25 18:36:24 -0300123
124static inline void
125dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
126 int direction)
127{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700128 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
129
Glauber Costa72c784f2008-03-25 18:36:24 -0300130 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700131 if (ops->unmap_sg)
132 ops->unmap_sg(hwdev, sg, nents, direction);
Glauber Costa72c784f2008-03-25 18:36:24 -0300133}
Glauber Costac01dd8c2008-03-25 18:36:25 -0300134
135static inline void
136dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
137 size_t size, int direction)
138{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700139 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
140
Glauber Costac01dd8c2008-03-25 18:36:25 -0300141 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700142 if (ops->sync_single_for_cpu)
143 ops->sync_single_for_cpu(hwdev, dma_handle, size, direction);
Glauber Costac01dd8c2008-03-25 18:36:25 -0300144 flush_write_buffers();
145}
146
Glauber Costa9231b262008-03-25 18:36:26 -0300147static inline void
148dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
149 size_t size, int direction)
150{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700151 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
152
Glauber Costa9231b262008-03-25 18:36:26 -0300153 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700154 if (ops->sync_single_for_device)
155 ops->sync_single_for_device(hwdev, dma_handle, size, direction);
Glauber Costa9231b262008-03-25 18:36:26 -0300156 flush_write_buffers();
157}
158
Glauber Costa627610f2008-03-25 18:36:27 -0300159static inline void
160dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
161 unsigned long offset, size_t size, int direction)
162{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700163 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
Glauber Costa627610f2008-03-25 18:36:27 -0300164
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700165 BUG_ON(!valid_dma_direction(direction));
166 if (ops->sync_single_range_for_cpu)
167 ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
168 size, direction);
Glauber Costa627610f2008-03-25 18:36:27 -0300169 flush_write_buffers();
170}
Glauber Costa71362332008-03-25 18:36:28 -0300171
172static inline void
173dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
174 unsigned long offset, size_t size,
175 int direction)
176{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700177 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
Glauber Costa71362332008-03-25 18:36:28 -0300178
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700179 BUG_ON(!valid_dma_direction(direction));
180 if (ops->sync_single_range_for_device)
181 ops->sync_single_range_for_device(hwdev, dma_handle,
182 offset, size, direction);
Glauber Costa71362332008-03-25 18:36:28 -0300183 flush_write_buffers();
184}
185
Glauber Costaed435de2008-03-25 18:36:29 -0300186static inline void
187dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
188 int nelems, int direction)
189{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700190 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
191
Glauber Costaed435de2008-03-25 18:36:29 -0300192 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700193 if (ops->sync_sg_for_cpu)
194 ops->sync_sg_for_cpu(hwdev, sg, nelems, direction);
Glauber Costaed435de2008-03-25 18:36:29 -0300195 flush_write_buffers();
196}
Glauber Costae7f3a912008-03-25 18:36:30 -0300197
198static inline void
199dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
200 int nelems, int direction)
201{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700202 struct dma_mapping_ops *ops = get_dma_ops(hwdev);
203
Glauber Costae7f3a912008-03-25 18:36:30 -0300204 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700205 if (ops->sync_sg_for_device)
206 ops->sync_sg_for_device(hwdev, sg, nelems, direction);
Glauber Costae7f3a912008-03-25 18:36:30 -0300207
208 flush_write_buffers();
209}
Glauber Costa4d92fbf2008-03-25 18:36:32 -0300210
211static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
212 size_t offset, size_t size,
213 int direction)
214{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700215 struct dma_mapping_ops *ops = get_dma_ops(dev);
216
Ingo Molnar2be62142008-04-19 19:19:56 +0200217 BUG_ON(!valid_dma_direction(direction));
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700218 return ops->map_single(dev, page_to_phys(page) + offset,
219 size, direction);
Glauber Costa4d92fbf2008-03-25 18:36:32 -0300220}
221
222static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
223 size_t size, int direction)
224{
225 dma_unmap_single(dev, addr, size, direction);
226}
227
Glauber Costa3cb6a912008-03-25 18:36:33 -0300228static inline void
229dma_cache_sync(struct device *dev, void *vaddr, size_t size,
230 enum dma_data_direction dir)
231{
232 flush_write_buffers();
233}
Glauber Costaae17a63b2008-03-25 18:36:38 -0300234
Glauber Costab7107a32008-03-25 18:36:39 -0300235static inline int dma_get_cache_alignment(void)
236{
237 /* no easy way to get cache size on all x86, so return the
238 * maximum possible, to be safe */
239 return boot_cpu_data.x86_clflush_size;
240}
241
FUJITA Tomonori823e7e82008-09-08 18:10:13 +0900242static inline unsigned long dma_alloc_coherent_mask(struct device *dev,
243 gfp_t gfp)
244{
245 unsigned long dma_mask = 0;
Glauber Costab7107a32008-03-25 18:36:39 -0300246
FUJITA Tomonori823e7e82008-09-08 18:10:13 +0900247 dma_mask = dev->coherent_dma_mask;
248 if (!dma_mask)
249 dma_mask = (gfp & GFP_DMA) ? DMA_24BIT_MASK : DMA_32BIT_MASK;
250
251 return dma_mask;
252}
253
254static inline gfp_t dma_alloc_coherent_gfp_flags(struct device *dev, gfp_t gfp)
255{
256 unsigned long dma_mask = dma_alloc_coherent_mask(dev, gfp);
257
FUJITA Tomonori75bebb72008-10-23 20:46:55 +0900258 if (dma_mask <= DMA_24BIT_MASK)
259 gfp |= GFP_DMA;
260#ifdef CONFIG_X86_64
FUJITA Tomonori823e7e82008-09-08 18:10:13 +0900261 if (dma_mask <= DMA_32BIT_MASK && !(gfp & GFP_DMA))
262 gfp |= GFP_DMA32;
263#endif
264 return gfp;
265}
266
Joerg Roedel6c505ce2008-08-19 16:32:45 +0200267static inline void *
268dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
269 gfp_t gfp)
270{
271 struct dma_mapping_ops *ops = get_dma_ops(dev);
272 void *memory;
Glauber Costaae17a63b2008-03-25 18:36:38 -0300273
FUJITA Tomonori8a53ad62008-09-08 18:10:12 +0900274 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
275
Joerg Roedel6c505ce2008-08-19 16:32:45 +0200276 if (dma_alloc_from_coherent(dev, size, dma_handle, &memory))
277 return memory;
278
279 if (!dev) {
280 dev = &x86_dma_fallback_dev;
281 gfp |= GFP_DMA;
282 }
283
FUJITA Tomonori98216262008-09-10 00:49:48 +0900284 if (!is_device_dma_capable(dev))
FUJITA Tomonoride9f5212008-09-08 18:10:11 +0900285 return NULL;
286
FUJITA Tomonori823e7e82008-09-08 18:10:13 +0900287 if (!ops->alloc_coherent)
288 return NULL;
289
290 return ops->alloc_coherent(dev, size, dma_handle,
291 dma_alloc_coherent_gfp_flags(dev, gfp));
Joerg Roedel6c505ce2008-08-19 16:32:45 +0200292}
293
294static inline void dma_free_coherent(struct device *dev, size_t size,
295 void *vaddr, dma_addr_t bus)
296{
297 struct dma_mapping_ops *ops = get_dma_ops(dev);
298
299 WARN_ON(irqs_disabled()); /* for portability */
300
301 if (dma_release_from_coherent(dev, get_order(size), vaddr))
302 return;
303
304 if (ops->free_coherent)
305 ops->free_coherent(dev, size, vaddr, bus);
306}
307
Glauber Costa6f536632008-03-25 18:36:20 -0300308#endif