powerpc: Swiotlb breaks pseries
[linux-2.6-xlnx.git] / arch / powerpc / include / asm / dma-mapping.h
blobb44aaabdd1a685f2c52c98fe3cf7ae798c779e31
1 /*
2 * Copyright (C) 2004 IBM
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__
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>
20 #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
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);
28 extern unsigned long get_dma_direct_offset(struct device *dev);
30 #ifdef CONFIG_NOT_COHERENT_CACHE
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.
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);
46 #else /* ! CONFIG_NOT_COHERENT_CACHE */
48 * Cache coherent cores.
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)
56 #endif /* ! CONFIG_NOT_COHERENT_CACHE */
58 static inline unsigned long device_to_mask(struct device *dev)
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;
67 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
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
111 * Available generic sets of operations
113 #ifdef CONFIG_PPC64
114 extern struct dma_mapping_ops dma_iommu_ops;
115 #endif
116 extern struct dma_mapping_ops dma_direct_ops;
118 static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
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.
125 if (unlikely(dev == NULL))
126 return NULL;
128 return dev->archdata.dma_ops;
131 static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
133 dev->archdata.dma_ops = ops;
136 static inline int dma_supported(struct device *dev, u64 mask)
138 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
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);
147 /* We have our own implementation of pci_set_dma_mask() */
148 #define HAVE_ARCH_PCI_SET_DMA_MASK
150 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
152 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
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;
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
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)
175 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
177 BUG_ON(!dma_ops);
179 return dma_ops->map_page(dev, virt_to_page(cpu_addr),
180 (unsigned long)cpu_addr % PAGE_SIZE, size,
181 direction, attrs);
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)
190 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
192 BUG_ON(!dma_ops);
194 dma_ops->unmap_page(dev, dma_addr, size, direction, attrs);
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)
203 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
205 BUG_ON(!dma_ops);
207 return dma_ops->map_page(dev, page, offset, size, direction, attrs);
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)
216 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
218 BUG_ON(!dma_ops);
220 dma_ops->unmap_page(dev, dma_address, size, direction, attrs);
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)
227 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
229 BUG_ON(!dma_ops);
230 return dma_ops->map_sg(dev, sg, nents, direction, attrs);
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)
239 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
241 BUG_ON(!dma_ops);
242 dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs);
245 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
246 dma_addr_t *dma_handle, gfp_t flag)
248 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
250 BUG_ON(!dma_ops);
251 return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
254 static inline void dma_free_coherent(struct device *dev, size_t size,
255 void *cpu_addr, dma_addr_t dma_handle)
257 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
259 BUG_ON(!dma_ops);
260 dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
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)
267 return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL);
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)
274 dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL);
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)
281 return dma_map_page_attrs(dev, page, offset, size, direction, NULL);
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)
288 dma_unmap_page_attrs(dev, dma_address, size, direction, NULL);
291 static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
292 int nents, enum dma_data_direction direction)
294 return dma_map_sg_attrs(dev, sg, nents, direction, NULL);
297 static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
298 int nhwentries,
299 enum dma_data_direction direction)
301 dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL);
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)
309 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
311 BUG_ON(!dma_ops);
313 if (dma_ops->sync_single_range_for_cpu)
314 dma_ops->sync_single_range_for_cpu(dev, dma_handle, 0,
315 size, direction);
318 static inline void dma_sync_single_for_device(struct device *dev,
319 dma_addr_t dma_handle, size_t size,
320 enum dma_data_direction direction)
322 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
324 BUG_ON(!dma_ops);
326 if (dma_ops->sync_single_range_for_device)
327 dma_ops->sync_single_range_for_device(dev, dma_handle,
328 0, size, direction);
331 static inline void dma_sync_sg_for_cpu(struct device *dev,
332 struct scatterlist *sgl, int nents,
333 enum dma_data_direction direction)
335 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
337 BUG_ON(!dma_ops);
339 if (dma_ops->sync_sg_for_cpu)
340 dma_ops->sync_sg_for_cpu(dev, sgl, nents, direction);
343 static inline void dma_sync_sg_for_device(struct device *dev,
344 struct scatterlist *sgl, int nents,
345 enum dma_data_direction direction)
347 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
349 BUG_ON(!dma_ops);
351 if (dma_ops->sync_sg_for_device)
352 dma_ops->sync_sg_for_device(dev, sgl, nents, direction);
355 static inline void dma_sync_single_range_for_cpu(struct device *dev,
356 dma_addr_t dma_handle, unsigned long offset, size_t size,
357 enum dma_data_direction direction)
359 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
361 BUG_ON(!dma_ops);
363 if (dma_ops->sync_single_range_for_cpu)
364 dma_ops->sync_single_range_for_cpu(dev, dma_handle,
365 offset, size, direction);
368 static inline void dma_sync_single_range_for_device(struct device *dev,
369 dma_addr_t dma_handle, unsigned long offset, size_t size,
370 enum dma_data_direction direction)
372 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
374 BUG_ON(!dma_ops);
376 if (dma_ops->sync_single_range_for_device)
377 dma_ops->sync_single_range_for_device(dev, dma_handle, offset,
378 size, direction);
380 #else /* CONFIG_PPC_NEED_DMA_SYNC_OPS */
381 static inline void dma_sync_single_for_cpu(struct device *dev,
382 dma_addr_t dma_handle, size_t size,
383 enum dma_data_direction direction)
387 static inline void dma_sync_single_for_device(struct device *dev,
388 dma_addr_t dma_handle, size_t size,
389 enum dma_data_direction direction)
393 static inline void dma_sync_sg_for_cpu(struct device *dev,
394 struct scatterlist *sgl, int nents,
395 enum dma_data_direction direction)
399 static inline void dma_sync_sg_for_device(struct device *dev,
400 struct scatterlist *sgl, int nents,
401 enum dma_data_direction direction)
405 static inline void dma_sync_single_range_for_cpu(struct device *dev,
406 dma_addr_t dma_handle, unsigned long offset, size_t size,
407 enum dma_data_direction direction)
411 static inline void dma_sync_single_range_for_device(struct device *dev,
412 dma_addr_t dma_handle, unsigned long offset, size_t size,
413 enum dma_data_direction direction)
416 #endif
418 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
420 #ifdef CONFIG_PPC64
421 return (dma_addr == DMA_ERROR_CODE);
422 #else
423 return 0;
424 #endif
427 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
428 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
429 #ifdef CONFIG_NOT_COHERENT_CACHE
430 #define dma_is_consistent(d, h) (0)
431 #else
432 #define dma_is_consistent(d, h) (1)
433 #endif
435 static inline int dma_get_cache_alignment(void)
437 #ifdef CONFIG_PPC64
438 /* no easy way to get cache size on all processors, so return
439 * the maximum possible, to be safe */
440 return (1 << INTERNODE_CACHE_SHIFT);
441 #else
443 * Each processor family will define its own L1_CACHE_SHIFT,
444 * L1_CACHE_BYTES wraps to this, so this is always safe.
446 return L1_CACHE_BYTES;
447 #endif
450 static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
451 enum dma_data_direction direction)
453 BUG_ON(direction == DMA_NONE);
454 __dma_sync(vaddr, size, (int)direction);
457 #endif /* __KERNEL__ */
458 #endif /* _ASM_DMA_MAPPING_H */