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>
18 #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
20 #ifdef CONFIG_NOT_COHERENT_CACHE
22 * DMA-consistent mapping functions for PowerPCs that don't support
23 * cache snooping. These allocate/free a region of uncached mapped
24 * memory space for use with DMA devices. Alternatively, you could
25 * allocate the space "normally" and use the cache management functions
26 * to ensure it is consistent.
28 extern void *__dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp);
29 extern void __dma_free_coherent(size_t size, void *vaddr);
30 extern void __dma_sync(void *vaddr, size_t size, int direction);
31 extern void __dma_sync_page(struct page *page, unsigned long offset,
32 size_t size, int direction);
34 #else /* ! CONFIG_NOT_COHERENT_CACHE */
36 * Cache coherent cores.
39 #define __dma_alloc_coherent(gfp, size, handle) NULL
40 #define __dma_free_coherent(size, addr) ((void)0)
41 #define __dma_sync(addr, size, rw) ((void)0)
42 #define __dma_sync_page(pg, off, sz, rw) ((void)0)
44 #endif /* ! CONFIG_NOT_COHERENT_CACHE */
48 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
50 struct dma_mapping_ops {
51 void * (*alloc_coherent)(struct device *dev, size_t size,
52 dma_addr_t *dma_handle, gfp_t flag);
53 void (*free_coherent)(struct device *dev, size_t size,
54 void *vaddr, dma_addr_t dma_handle);
55 dma_addr_t (*map_single)(struct device *dev, void *ptr,
56 size_t size, enum dma_data_direction direction);
57 void (*unmap_single)(struct device *dev, dma_addr_t dma_addr,
58 size_t size, enum dma_data_direction direction);
59 int (*map_sg)(struct device *dev, struct scatterlist *sg,
60 int nents, enum dma_data_direction direction);
61 void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
62 int nents, enum dma_data_direction direction);
63 int (*dma_supported)(struct device *dev, u64 mask);
64 int (*set_dma_mask)(struct device *dev, u64 dma_mask);
67 static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
69 /* We don't handle the NULL dev case for ISA for now. We could
70 * do it via an out of line call but it is not needed for now. The
71 * only ISA DMA device we support is the floppy and we have a hack
72 * in the floppy driver directly to get a device for us.
74 if (unlikely(dev == NULL || dev->archdata.dma_ops == NULL))
76 return dev->archdata.dma_ops;
79 static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
81 dev->archdata.dma_ops = ops;
84 static inline int dma_supported(struct device *dev, u64 mask)
86 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
88 if (unlikely(dma_ops == NULL))
90 if (dma_ops->dma_supported == NULL)
92 return dma_ops->dma_supported(dev, mask);
95 /* We have our own implementation of pci_set_dma_mask() */
96 #define HAVE_ARCH_PCI_SET_DMA_MASK
98 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
100 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
102 if (unlikely(dma_ops == NULL))
104 if (dma_ops->set_dma_mask != NULL)
105 return dma_ops->set_dma_mask(dev, dma_mask);
106 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
108 *dev->dma_mask = dma_mask;
112 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
113 dma_addr_t *dma_handle, gfp_t flag)
115 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
118 return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
121 static inline void dma_free_coherent(struct device *dev, size_t size,
122 void *cpu_addr, dma_addr_t dma_handle)
124 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
127 dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
130 static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
132 enum dma_data_direction direction)
134 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
137 return dma_ops->map_single(dev, cpu_addr, size, direction);
140 static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
142 enum dma_data_direction direction)
144 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
147 dma_ops->unmap_single(dev, dma_addr, size, direction);
150 static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
151 unsigned long offset, size_t size,
152 enum dma_data_direction direction)
154 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
157 return dma_ops->map_single(dev, page_address(page) + offset, size,
161 static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
163 enum dma_data_direction direction)
165 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
168 dma_ops->unmap_single(dev, dma_address, size, direction);
171 static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
172 int nents, enum dma_data_direction direction)
174 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
177 return dma_ops->map_sg(dev, sg, nents, direction);
180 static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
182 enum dma_data_direction direction)
184 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
187 dma_ops->unmap_sg(dev, sg, nhwentries, direction);
192 * Available generic sets of operations
194 extern struct dma_mapping_ops dma_iommu_ops;
195 extern struct dma_mapping_ops dma_direct_ops;
197 #else /* CONFIG_PPC64 */
199 #define dma_supported(dev, mask) (1)
201 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
203 if (!dev->dma_mask || !dma_supported(dev, mask))
206 *dev->dma_mask = dma_mask;
211 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
212 dma_addr_t * dma_handle,
215 #ifdef CONFIG_NOT_COHERENT_CACHE
216 return __dma_alloc_coherent(size, dma_handle, gfp);
219 /* ignore region specifiers */
220 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
222 if (dev == NULL || dev->coherent_dma_mask < 0xffffffff)
225 ret = (void *)__get_free_pages(gfp, get_order(size));
228 memset(ret, 0, size);
229 *dma_handle = virt_to_bus(ret);
237 dma_free_coherent(struct device *dev, size_t size, void *vaddr,
238 dma_addr_t dma_handle)
240 #ifdef CONFIG_NOT_COHERENT_CACHE
241 __dma_free_coherent(size, vaddr);
243 free_pages((unsigned long)vaddr, get_order(size));
247 static inline dma_addr_t
248 dma_map_single(struct device *dev, void *ptr, size_t size,
249 enum dma_data_direction direction)
251 BUG_ON(direction == DMA_NONE);
253 __dma_sync(ptr, size, direction);
255 return virt_to_bus(ptr);
258 static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
260 enum dma_data_direction direction)
265 static inline dma_addr_t
266 dma_map_page(struct device *dev, struct page *page,
267 unsigned long offset, size_t size,
268 enum dma_data_direction direction)
270 BUG_ON(direction == DMA_NONE);
272 __dma_sync_page(page, offset, size, direction);
274 return page_to_bus(page) + offset;
277 static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
279 enum dma_data_direction direction)
285 dma_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
286 enum dma_data_direction direction)
288 struct scatterlist *sg;
291 BUG_ON(direction == DMA_NONE);
293 for_each_sg(sgl, sg, nents, i) {
294 BUG_ON(!sg_page(sg));
295 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
296 sg->dma_address = page_to_bus(sg_page(sg)) + sg->offset;
302 static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
304 enum dma_data_direction direction)
306 /* We don't do anything here. */
309 #endif /* CONFIG_PPC64 */
311 static inline void dma_sync_single_for_cpu(struct device *dev,
312 dma_addr_t dma_handle, size_t size,
313 enum dma_data_direction direction)
315 BUG_ON(direction == DMA_NONE);
316 __dma_sync(bus_to_virt(dma_handle), size, direction);
319 static inline void dma_sync_single_for_device(struct device *dev,
320 dma_addr_t dma_handle, size_t size,
321 enum dma_data_direction direction)
323 BUG_ON(direction == DMA_NONE);
324 __dma_sync(bus_to_virt(dma_handle), size, direction);
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 scatterlist *sg;
334 BUG_ON(direction == DMA_NONE);
336 for_each_sg(sgl, sg, nents, i)
337 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
340 static inline void dma_sync_sg_for_device(struct device *dev,
341 struct scatterlist *sgl, int nents,
342 enum dma_data_direction direction)
344 struct scatterlist *sg;
347 BUG_ON(direction == DMA_NONE);
349 for_each_sg(sgl, sg, nents, i)
350 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
353 static inline int dma_mapping_error(dma_addr_t dma_addr)
356 return (dma_addr == DMA_ERROR_CODE);
362 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
363 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
364 #ifdef CONFIG_NOT_COHERENT_CACHE
365 #define dma_is_consistent(d, h) (0)
367 #define dma_is_consistent(d, h) (1)
370 static inline int dma_get_cache_alignment(void)
373 /* no easy way to get cache size on all processors, so return
374 * the maximum possible, to be safe */
375 return (1 << INTERNODE_CACHE_SHIFT);
378 * Each processor family will define its own L1_CACHE_SHIFT,
379 * L1_CACHE_BYTES wraps to this, so this is always safe.
381 return L1_CACHE_BYTES;
385 static inline void dma_sync_single_range_for_cpu(struct device *dev,
386 dma_addr_t dma_handle, unsigned long offset, size_t size,
387 enum dma_data_direction direction)
389 /* just sync everything for now */
390 dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
393 static inline void dma_sync_single_range_for_device(struct device *dev,
394 dma_addr_t dma_handle, unsigned long offset, size_t size,
395 enum dma_data_direction direction)
397 /* just sync everything for now */
398 dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
401 static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
402 enum dma_data_direction direction)
404 BUG_ON(direction == DMA_NONE);
405 __dma_sync(vaddr, size, (int)direction);
408 #endif /* __KERNEL__ */
409 #endif /* _ASM_DMA_MAPPING_H */