powerpc: Add support for swiotlb on 32-bit
[deliverable/linux.git] / arch / powerpc / include / asm / dma-mapping.h
1 /*
2 * Copyright (C) 2004 IBM
3 *
4 * Implements the generic device dma API for powerpc.
5 * the pci and vio busses
6 */
7 #ifndef _ASM_DMA_MAPPING_H
8 #define _ASM_DMA_MAPPING_H
9 #ifdef __KERNEL__
10
11 #include <linux/types.h>
12 #include <linux/cache.h>
13 /* need struct page definitions */
14 #include <linux/mm.h>
15 #include <linux/scatterlist.h>
16 #include <linux/dma-attrs.h>
17 #include <asm/io.h>
18 #include <asm/swiotlb.h>
19
20 #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
21
22 /* Some dma direct funcs must be visible for use in other dma_ops */
23 extern void *dma_direct_alloc_coherent(struct device *dev, size_t size,
24 dma_addr_t *dma_handle, gfp_t flag);
25 extern void dma_direct_free_coherent(struct device *dev, size_t size,
26 void *vaddr, dma_addr_t dma_handle);
27
28 extern unsigned long get_dma_direct_offset(struct device *dev);
29
30 #ifdef CONFIG_NOT_COHERENT_CACHE
31 /*
32 * DMA-consistent mapping functions for PowerPCs that don't support
33 * cache snooping. These allocate/free a region of uncached mapped
34 * memory space for use with DMA devices. Alternatively, you could
35 * allocate the space "normally" and use the cache management functions
36 * to ensure it is consistent.
37 */
38 struct device;
39 extern void *__dma_alloc_coherent(struct device *dev, size_t size,
40 dma_addr_t *handle, gfp_t gfp);
41 extern void __dma_free_coherent(size_t size, void *vaddr);
42 extern void __dma_sync(void *vaddr, size_t size, int direction);
43 extern void __dma_sync_page(struct page *page, unsigned long offset,
44 size_t size, int direction);
45
46 #else /* ! CONFIG_NOT_COHERENT_CACHE */
47 /*
48 * Cache coherent cores.
49 */
50
51 #define __dma_alloc_coherent(dev, gfp, size, handle) NULL
52 #define __dma_free_coherent(size, addr) ((void)0)
53 #define __dma_sync(addr, size, rw) ((void)0)
54 #define __dma_sync_page(pg, off, sz, rw) ((void)0)
55
56 #endif /* ! CONFIG_NOT_COHERENT_CACHE */
57
58 static inline unsigned long device_to_mask(struct device *dev)
59 {
60 if (dev->dma_mask && *dev->dma_mask)
61 return *dev->dma_mask;
62 /* Assume devices without mask can take 32 bit addresses */
63 return 0xfffffffful;
64 }
65
66 /*
67 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
68 */
69 struct dma_mapping_ops {
70 void * (*alloc_coherent)(struct device *dev, size_t size,
71 dma_addr_t *dma_handle, gfp_t flag);
72 void (*free_coherent)(struct device *dev, size_t size,
73 void *vaddr, dma_addr_t dma_handle);
74 int (*map_sg)(struct device *dev, struct scatterlist *sg,
75 int nents, enum dma_data_direction direction,
76 struct dma_attrs *attrs);
77 void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
78 int nents, enum dma_data_direction direction,
79 struct dma_attrs *attrs);
80 int (*dma_supported)(struct device *dev, u64 mask);
81 int (*set_dma_mask)(struct device *dev, u64 dma_mask);
82 dma_addr_t (*map_page)(struct device *dev, struct page *page,
83 unsigned long offset, size_t size,
84 enum dma_data_direction direction,
85 struct dma_attrs *attrs);
86 void (*unmap_page)(struct device *dev,
87 dma_addr_t dma_address, size_t size,
88 enum dma_data_direction direction,
89 struct dma_attrs *attrs);
90 int (*addr_needs_map)(struct device *dev, dma_addr_t addr,
91 size_t size);
92 #ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS
93 void (*sync_single_range_for_cpu)(struct device *hwdev,
94 dma_addr_t dma_handle, unsigned long offset,
95 size_t size,
96 enum dma_data_direction direction);
97 void (*sync_single_range_for_device)(struct device *hwdev,
98 dma_addr_t dma_handle, unsigned long offset,
99 size_t size,
100 enum dma_data_direction direction);
101 void (*sync_sg_for_cpu)(struct device *hwdev,
102 struct scatterlist *sg, int nelems,
103 enum dma_data_direction direction);
104 void (*sync_sg_for_device)(struct device *hwdev,
105 struct scatterlist *sg, int nelems,
106 enum dma_data_direction direction);
107 #endif
108 };
109
110 /*
111 * Available generic sets of operations
112 */
113 #ifdef CONFIG_PPC64
114 extern struct dma_mapping_ops dma_iommu_ops;
115 #endif
116 extern struct dma_mapping_ops dma_direct_ops;
117
118 static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
119 {
120 /* We don't handle the NULL dev case for ISA for now. We could
121 * do it via an out of line call but it is not needed for now. The
122 * only ISA DMA device we support is the floppy and we have a hack
123 * in the floppy driver directly to get a device for us.
124 */
125 if (unlikely(dev == NULL))
126 return NULL;
127
128 return dev->archdata.dma_ops;
129 }
130
131 static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
132 {
133 dev->archdata.dma_ops = ops;
134 }
135
136 static inline int dma_supported(struct device *dev, u64 mask)
137 {
138 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
139
140 if (unlikely(dma_ops == NULL))
141 return 0;
142 if (dma_ops->dma_supported == NULL)
143 return 1;
144 return dma_ops->dma_supported(dev, mask);
145 }
146
147 /* We have our own implementation of pci_set_dma_mask() */
148 #define HAVE_ARCH_PCI_SET_DMA_MASK
149
150 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
151 {
152 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
153
154 if (unlikely(dma_ops == NULL))
155 return -EIO;
156 if (dma_ops->set_dma_mask != NULL)
157 return dma_ops->set_dma_mask(dev, dma_mask);
158 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
159 return -EIO;
160 *dev->dma_mask = dma_mask;
161 return 0;
162 }
163
164 /*
165 * map_/unmap_single actually call through to map/unmap_page now that all the
166 * dma_mapping_ops have been converted over. We just have to get the page and
167 * offset to pass through to map_page
168 */
169 static inline dma_addr_t dma_map_single_attrs(struct device *dev,
170 void *cpu_addr,
171 size_t size,
172 enum dma_data_direction direction,
173 struct dma_attrs *attrs)
174 {
175 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
176
177 BUG_ON(!dma_ops);
178
179 return dma_ops->map_page(dev, virt_to_page(cpu_addr),
180 (unsigned long)cpu_addr % PAGE_SIZE, size,
181 direction, attrs);
182 }
183
184 static inline void dma_unmap_single_attrs(struct device *dev,
185 dma_addr_t dma_addr,
186 size_t size,
187 enum dma_data_direction direction,
188 struct dma_attrs *attrs)
189 {
190 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
191
192 BUG_ON(!dma_ops);
193
194 dma_ops->unmap_page(dev, dma_addr, size, direction, attrs);
195 }
196
197 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
198 struct page *page,
199 unsigned long offset, size_t size,
200 enum dma_data_direction direction,
201 struct dma_attrs *attrs)
202 {
203 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
204
205 BUG_ON(!dma_ops);
206
207 return dma_ops->map_page(dev, page, offset, size, direction, attrs);
208 }
209
210 static inline void dma_unmap_page_attrs(struct device *dev,
211 dma_addr_t dma_address,
212 size_t size,
213 enum dma_data_direction direction,
214 struct dma_attrs *attrs)
215 {
216 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
217
218 BUG_ON(!dma_ops);
219
220 dma_ops->unmap_page(dev, dma_address, size, direction, attrs);
221 }
222
223 static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
224 int nents, enum dma_data_direction direction,
225 struct dma_attrs *attrs)
226 {
227 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
228
229 BUG_ON(!dma_ops);
230 return dma_ops->map_sg(dev, sg, nents, direction, attrs);
231 }
232
233 static inline void dma_unmap_sg_attrs(struct device *dev,
234 struct scatterlist *sg,
235 int nhwentries,
236 enum dma_data_direction direction,
237 struct dma_attrs *attrs)
238 {
239 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
240
241 BUG_ON(!dma_ops);
242 dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs);
243 }
244
245 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
246 dma_addr_t *dma_handle, gfp_t flag)
247 {
248 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
249
250 BUG_ON(!dma_ops);
251 return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
252 }
253
254 static inline void dma_free_coherent(struct device *dev, size_t size,
255 void *cpu_addr, dma_addr_t dma_handle)
256 {
257 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
258
259 BUG_ON(!dma_ops);
260 dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
261 }
262
263 static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
264 size_t size,
265 enum dma_data_direction direction)
266 {
267 return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL);
268 }
269
270 static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
271 size_t size,
272 enum dma_data_direction direction)
273 {
274 dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL);
275 }
276
277 static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
278 unsigned long offset, size_t size,
279 enum dma_data_direction direction)
280 {
281 return dma_map_page_attrs(dev, page, offset, size, direction, NULL);
282 }
283
284 static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
285 size_t size,
286 enum dma_data_direction direction)
287 {
288 dma_unmap_page_attrs(dev, dma_address, size, direction, NULL);
289 }
290
291 static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
292 int nents, enum dma_data_direction direction)
293 {
294 return dma_map_sg_attrs(dev, sg, nents, direction, NULL);
295 }
296
297 static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
298 int nhwentries,
299 enum dma_data_direction direction)
300 {
301 dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL);
302 }
303
304 #ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS
305 static inline void dma_sync_single_for_cpu(struct device *dev,
306 dma_addr_t dma_handle, size_t size,
307 enum dma_data_direction direction)
308 {
309 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
310
311 BUG_ON(!dma_ops);
312 dma_ops->sync_single_range_for_cpu(dev, dma_handle, 0,
313 size, direction);
314 }
315
316 static inline void dma_sync_single_for_device(struct device *dev,
317 dma_addr_t dma_handle, size_t size,
318 enum dma_data_direction direction)
319 {
320 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
321
322 BUG_ON(!dma_ops);
323 dma_ops->sync_single_range_for_device(dev, dma_handle,
324 0, size, direction);
325 }
326
327 static inline void dma_sync_sg_for_cpu(struct device *dev,
328 struct scatterlist *sgl, int nents,
329 enum dma_data_direction direction)
330 {
331 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
332
333 BUG_ON(!dma_ops);
334 dma_ops->sync_sg_for_cpu(dev, sgl, nents, direction);
335 }
336
337 static inline void dma_sync_sg_for_device(struct device *dev,
338 struct scatterlist *sgl, int nents,
339 enum dma_data_direction direction)
340 {
341 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
342
343 BUG_ON(!dma_ops);
344 dma_ops->sync_sg_for_device(dev, sgl, nents, direction);
345 }
346
347 static inline void dma_sync_single_range_for_cpu(struct device *dev,
348 dma_addr_t dma_handle, unsigned long offset, size_t size,
349 enum dma_data_direction direction)
350 {
351 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
352
353 BUG_ON(!dma_ops);
354 dma_ops->sync_single_range_for_cpu(dev, dma_handle,
355 offset, size, direction);
356 }
357
358 static inline void dma_sync_single_range_for_device(struct device *dev,
359 dma_addr_t dma_handle, unsigned long offset, size_t size,
360 enum dma_data_direction direction)
361 {
362 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
363
364 BUG_ON(!dma_ops);
365 dma_ops->sync_single_range_for_device(dev, dma_handle, offset,
366 size, direction);
367 }
368 #else /* CONFIG_PPC_NEED_DMA_SYNC_OPS */
369 static inline void dma_sync_single_for_cpu(struct device *dev,
370 dma_addr_t dma_handle, size_t size,
371 enum dma_data_direction direction)
372 {
373 }
374
375 static inline void dma_sync_single_for_device(struct device *dev,
376 dma_addr_t dma_handle, size_t size,
377 enum dma_data_direction direction)
378 {
379 }
380
381 static inline void dma_sync_sg_for_cpu(struct device *dev,
382 struct scatterlist *sgl, int nents,
383 enum dma_data_direction direction)
384 {
385 }
386
387 static inline void dma_sync_sg_for_device(struct device *dev,
388 struct scatterlist *sgl, int nents,
389 enum dma_data_direction direction)
390 {
391 }
392
393 static inline void dma_sync_single_range_for_cpu(struct device *dev,
394 dma_addr_t dma_handle, unsigned long offset, size_t size,
395 enum dma_data_direction direction)
396 {
397 }
398
399 static inline void dma_sync_single_range_for_device(struct device *dev,
400 dma_addr_t dma_handle, unsigned long offset, size_t size,
401 enum dma_data_direction direction)
402 {
403 }
404 #endif
405
406 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
407 {
408 #ifdef CONFIG_PPC64
409 return (dma_addr == DMA_ERROR_CODE);
410 #else
411 return 0;
412 #endif
413 }
414
415 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
416 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
417 #ifdef CONFIG_NOT_COHERENT_CACHE
418 #define dma_is_consistent(d, h) (0)
419 #else
420 #define dma_is_consistent(d, h) (1)
421 #endif
422
423 static inline int dma_get_cache_alignment(void)
424 {
425 #ifdef CONFIG_PPC64
426 /* no easy way to get cache size on all processors, so return
427 * the maximum possible, to be safe */
428 return (1 << INTERNODE_CACHE_SHIFT);
429 #else
430 /*
431 * Each processor family will define its own L1_CACHE_SHIFT,
432 * L1_CACHE_BYTES wraps to this, so this is always safe.
433 */
434 return L1_CACHE_BYTES;
435 #endif
436 }
437
438 static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
439 enum dma_data_direction direction)
440 {
441 BUG_ON(direction == DMA_NONE);
442 __dma_sync(vaddr, size, (int)direction);
443 }
444
445 #endif /* __KERNEL__ */
446 #endif /* _ASM_DMA_MAPPING_H */
This page took 0.041631 seconds and 6 git commands to generate.