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>
19 #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
21 #ifdef CONFIG_NOT_COHERENT_CACHE
23 * DMA-consistent mapping functions for PowerPCs that don't support
24 * cache snooping. These allocate/free a region of uncached mapped
25 * memory space for use with DMA devices. Alternatively, you could
26 * allocate the space "normally" and use the cache management functions
27 * to ensure it is consistent.
29 extern void *__dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp);
30 extern void __dma_free_coherent(size_t size, void *vaddr);
31 extern void __dma_sync(void *vaddr, size_t size, int direction);
32 extern void __dma_sync_page(struct page *page, unsigned long offset,
33 size_t size, int direction);
35 #else /* ! CONFIG_NOT_COHERENT_CACHE */
37 * Cache coherent cores.
40 #define __dma_alloc_coherent(gfp, size, handle) NULL
41 #define __dma_free_coherent(size, addr) ((void)0)
42 #define __dma_sync(addr, size, rw) ((void)0)
43 #define __dma_sync_page(pg, off, sz, rw) ((void)0)
45 #endif /* ! CONFIG_NOT_COHERENT_CACHE */
47 static inline unsigned long device_to_mask(struct device *dev)
49 if (dev->dma_mask && *dev->dma_mask)
50 return *dev->dma_mask;
51 /* Assume devices without mask can take 32 bit addresses */
56 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
58 struct dma_mapping_ops {
59 void * (*alloc_coherent)(struct device *dev, size_t size,
60 dma_addr_t *dma_handle, gfp_t flag);
61 void (*free_coherent)(struct device *dev, size_t size,
62 void *vaddr, dma_addr_t dma_handle);
63 int (*map_sg)(struct device *dev, struct scatterlist *sg,
64 int nents, enum dma_data_direction direction,
65 struct dma_attrs *attrs);
66 void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
67 int nents, enum dma_data_direction direction,
68 struct dma_attrs *attrs);
69 int (*dma_supported)(struct device *dev, u64 mask);
70 int (*set_dma_mask)(struct device *dev, u64 dma_mask);
71 dma_addr_t (*map_page)(struct device *dev, struct page *page,
72 unsigned long offset, size_t size,
73 enum dma_data_direction direction,
74 struct dma_attrs *attrs);
75 void (*unmap_page)(struct device *dev,
76 dma_addr_t dma_address, size_t size,
77 enum dma_data_direction direction,
78 struct dma_attrs *attrs);
82 * Available generic sets of operations
85 extern struct dma_mapping_ops dma_iommu_ops;
87 extern struct dma_mapping_ops dma_direct_ops;
89 static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
91 /* We don't handle the NULL dev case for ISA for now. We could
92 * do it via an out of line call but it is not needed for now. The
93 * only ISA DMA device we support is the floppy and we have a hack
94 * in the floppy driver directly to get a device for us.
97 if (unlikely(dev == NULL) || dev->archdata.dma_ops == NULL) {
101 /* Use default on 32-bit if dma_ops is not set up */
102 /* TODO: Long term, we should fix drivers so that dev and
103 * archdata dma_ops are set up for all buses.
105 return &dma_direct_ops;
109 return dev->archdata.dma_ops;
112 static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
114 dev->archdata.dma_ops = ops;
117 static inline int dma_supported(struct device *dev, u64 mask)
119 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
121 if (unlikely(dma_ops == NULL))
123 if (dma_ops->dma_supported == NULL)
125 return dma_ops->dma_supported(dev, mask);
128 /* We have our own implementation of pci_set_dma_mask() */
129 #define HAVE_ARCH_PCI_SET_DMA_MASK
131 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
133 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
135 if (unlikely(dma_ops == NULL))
137 if (dma_ops->set_dma_mask != NULL)
138 return dma_ops->set_dma_mask(dev, dma_mask);
139 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
141 *dev->dma_mask = dma_mask;
146 * map_/unmap_single actually call through to map/unmap_page now that all the
147 * dma_mapping_ops have been converted over. We just have to get the page and
148 * offset to pass through to map_page
150 static inline dma_addr_t dma_map_single_attrs(struct device *dev,
153 enum dma_data_direction direction,
154 struct dma_attrs *attrs)
156 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
160 return dma_ops->map_page(dev, virt_to_page(cpu_addr),
161 (unsigned long)cpu_addr % PAGE_SIZE, size,
165 static inline void dma_unmap_single_attrs(struct device *dev,
168 enum dma_data_direction direction,
169 struct dma_attrs *attrs)
171 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
175 dma_ops->unmap_page(dev, dma_addr, size, direction, attrs);
178 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
180 unsigned long offset, size_t size,
181 enum dma_data_direction direction,
182 struct dma_attrs *attrs)
184 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
188 return dma_ops->map_page(dev, page, offset, size, direction, attrs);
191 static inline void dma_unmap_page_attrs(struct device *dev,
192 dma_addr_t dma_address,
194 enum dma_data_direction direction,
195 struct dma_attrs *attrs)
197 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
201 dma_ops->unmap_page(dev, dma_address, size, direction, attrs);
204 static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
205 int nents, enum dma_data_direction direction,
206 struct dma_attrs *attrs)
208 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
211 return dma_ops->map_sg(dev, sg, nents, direction, attrs);
214 static inline void dma_unmap_sg_attrs(struct device *dev,
215 struct scatterlist *sg,
217 enum dma_data_direction direction,
218 struct dma_attrs *attrs)
220 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
223 dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs);
226 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
227 dma_addr_t *dma_handle, gfp_t flag)
229 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
232 return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
235 static inline void dma_free_coherent(struct device *dev, size_t size,
236 void *cpu_addr, dma_addr_t dma_handle)
238 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
241 dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
244 static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
246 enum dma_data_direction direction)
248 return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL);
251 static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
253 enum dma_data_direction direction)
255 dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL);
258 static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
259 unsigned long offset, size_t size,
260 enum dma_data_direction direction)
262 return dma_map_page_attrs(dev, page, offset, size, direction, NULL);
265 static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
267 enum dma_data_direction direction)
269 dma_unmap_page_attrs(dev, dma_address, size, direction, NULL);
272 static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
273 int nents, enum dma_data_direction direction)
275 return dma_map_sg_attrs(dev, sg, nents, direction, NULL);
278 static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
280 enum dma_data_direction direction)
282 dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL);
285 static inline void dma_sync_single_for_cpu(struct device *dev,
286 dma_addr_t dma_handle, size_t size,
287 enum dma_data_direction direction)
289 BUG_ON(direction == DMA_NONE);
290 __dma_sync(bus_to_virt(dma_handle), size, direction);
293 static inline void dma_sync_single_for_device(struct device *dev,
294 dma_addr_t dma_handle, size_t size,
295 enum dma_data_direction direction)
297 BUG_ON(direction == DMA_NONE);
298 __dma_sync(bus_to_virt(dma_handle), size, direction);
301 static inline void dma_sync_sg_for_cpu(struct device *dev,
302 struct scatterlist *sgl, int nents,
303 enum dma_data_direction direction)
305 struct scatterlist *sg;
308 BUG_ON(direction == DMA_NONE);
310 for_each_sg(sgl, sg, nents, i)
311 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
314 static inline void dma_sync_sg_for_device(struct device *dev,
315 struct scatterlist *sgl, int nents,
316 enum dma_data_direction direction)
318 struct scatterlist *sg;
321 BUG_ON(direction == DMA_NONE);
323 for_each_sg(sgl, sg, nents, i)
324 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
327 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
330 return (dma_addr == DMA_ERROR_CODE);
336 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
337 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
338 #ifdef CONFIG_NOT_COHERENT_CACHE
339 #define dma_is_consistent(d, h) (0)
341 #define dma_is_consistent(d, h) (1)
344 static inline int dma_get_cache_alignment(void)
347 /* no easy way to get cache size on all processors, so return
348 * the maximum possible, to be safe */
349 return (1 << INTERNODE_CACHE_SHIFT);
352 * Each processor family will define its own L1_CACHE_SHIFT,
353 * L1_CACHE_BYTES wraps to this, so this is always safe.
355 return L1_CACHE_BYTES;
359 static inline void dma_sync_single_range_for_cpu(struct device *dev,
360 dma_addr_t dma_handle, unsigned long offset, size_t size,
361 enum dma_data_direction direction)
363 /* just sync everything for now */
364 dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
367 static inline void dma_sync_single_range_for_device(struct device *dev,
368 dma_addr_t dma_handle, unsigned long offset, size_t size,
369 enum dma_data_direction direction)
371 /* just sync everything for now */
372 dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
375 static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
376 enum dma_data_direction direction)
378 BUG_ON(direction == DMA_NONE);
379 __dma_sync(vaddr, size, (int)direction);
382 #endif /* __KERNEL__ */
383 #endif /* _ASM_DMA_MAPPING_H */