2 * Copyright (C) 2004 IBM
4 * Implements the generic device dma API for powerpc.
5 * the pci and vio busses
7 #ifndef _ASM_DMA_MAPPING_H
8 #define _ASM_DMA_MAPPING_H
11 #include <linux/types.h>
12 #include <linux/cache.h>
13 /* need struct page definitions */
15 #include <linux/scatterlist.h>
16 #include <linux/dma-attrs.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.
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 */
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,
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,
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,
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);
111 * Available generic sets of operations
114 extern struct dma_mapping_ops dma_iommu_ops;
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))
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))
142 if (dma_ops->dma_supported == NULL)
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))
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))
160 *dev->dma_mask = dma_mask;
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,
172 enum dma_data_direction direction,
173 struct dma_attrs *attrs)
175 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
179 return dma_ops->map_page(dev, virt_to_page(cpu_addr),
180 (unsigned long)cpu_addr % PAGE_SIZE, size,
184 static inline void dma_unmap_single_attrs(struct device *dev,
187 enum dma_data_direction direction,
188 struct dma_attrs *attrs)
190 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
194 dma_ops->unmap_page(dev, dma_addr, size, direction, attrs);
197 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
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);
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,
213 enum dma_data_direction direction,
214 struct dma_attrs *attrs)
216 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
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);
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,
236 enum dma_data_direction direction,
237 struct dma_attrs *attrs)
239 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
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);
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);
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,
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,
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,
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,
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);
312 dma_ops->sync_single_range_for_cpu(dev, dma_handle, 0,
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)
320 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
323 dma_ops->sync_single_range_for_device(dev, dma_handle,
327 static inline void dma_sync_sg_for_cpu(struct device *dev,
328 struct scatterlist *sgl, int nents,
329 enum dma_data_direction direction)
331 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
334 dma_ops->sync_sg_for_cpu(dev, sgl, nents, direction);
337 static inline void dma_sync_sg_for_device(struct device *dev,
338 struct scatterlist *sgl, int nents,
339 enum dma_data_direction direction)
341 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
344 dma_ops->sync_sg_for_device(dev, sgl, nents, direction);
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)
351 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
354 dma_ops->sync_single_range_for_cpu(dev, dma_handle,
355 offset, size, direction);
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)
362 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
365 dma_ops->sync_single_range_for_device(dev, dma_handle, offset,
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)
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)
381 static inline void dma_sync_sg_for_cpu(struct device *dev,
382 struct scatterlist *sgl, int nents,
383 enum dma_data_direction direction)
387 static inline void dma_sync_sg_for_device(struct device *dev,
388 struct scatterlist *sgl, int nents,
389 enum dma_data_direction direction)
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)
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)
406 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
409 return (dma_addr == DMA_ERROR_CODE);
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)
420 #define dma_is_consistent(d, h) (1)
423 static inline int dma_get_cache_alignment(void)
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);
431 * Each processor family will define its own L1_CACHE_SHIFT,
432 * L1_CACHE_BYTES wraps to this, so this is always safe.
434 return L1_CACHE_BYTES;
438 static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
439 enum dma_data_direction direction)
441 BUG_ON(direction == DMA_NONE);
442 __dma_sync(vaddr, size, (int)direction);
445 #endif /* __KERNEL__ */
446 #endif /* _ASM_DMA_MAPPING_H */