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 */
49 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
51 struct dma_mapping_ops {
52 void * (*alloc_coherent)(struct device *dev, size_t size,
53 dma_addr_t *dma_handle, gfp_t flag);
54 void (*free_coherent)(struct device *dev, size_t size,
55 void *vaddr, dma_addr_t dma_handle);
56 dma_addr_t (*map_single)(struct device *dev, void *ptr,
57 size_t size, enum dma_data_direction direction,
58 struct dma_attrs *attrs);
59 void (*unmap_single)(struct device *dev, dma_addr_t dma_addr,
60 size_t size, enum dma_data_direction direction,
61 struct dma_attrs *attrs);
62 int (*map_sg)(struct device *dev, struct scatterlist *sg,
63 int nents, enum dma_data_direction direction,
64 struct dma_attrs *attrs);
65 void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
66 int nents, enum dma_data_direction direction,
67 struct dma_attrs *attrs);
68 int (*dma_supported)(struct device *dev, u64 mask);
69 int (*set_dma_mask)(struct device *dev, u64 dma_mask);
72 static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
74 /* We don't handle the NULL dev case for ISA for now. We could
75 * do it via an out of line call but it is not needed for now. The
76 * only ISA DMA device we support is the floppy and we have a hack
77 * in the floppy driver directly to get a device for us.
79 if (unlikely(dev == NULL || dev->archdata.dma_ops == NULL))
81 return dev->archdata.dma_ops;
84 static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
86 dev->archdata.dma_ops = ops;
89 static inline int dma_supported(struct device *dev, u64 mask)
91 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
93 if (unlikely(dma_ops == NULL))
95 if (dma_ops->dma_supported == NULL)
97 return dma_ops->dma_supported(dev, mask);
100 /* We have our own implementation of pci_set_dma_mask() */
101 #define HAVE_ARCH_PCI_SET_DMA_MASK
103 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
105 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
107 if (unlikely(dma_ops == NULL))
109 if (dma_ops->set_dma_mask != NULL)
110 return dma_ops->set_dma_mask(dev, dma_mask);
111 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
113 *dev->dma_mask = dma_mask;
117 static inline dma_addr_t dma_map_single_attrs(struct device *dev,
120 enum dma_data_direction direction,
121 struct dma_attrs *attrs)
123 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
126 return dma_ops->map_single(dev, cpu_addr, size, direction, attrs);
129 static inline void dma_unmap_single_attrs(struct device *dev,
132 enum dma_data_direction direction,
133 struct dma_attrs *attrs)
135 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
138 dma_ops->unmap_single(dev, dma_addr, size, direction, attrs);
141 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
143 unsigned long offset, size_t size,
144 enum dma_data_direction direction,
145 struct dma_attrs *attrs)
147 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
150 return dma_ops->map_single(dev, page_address(page) + offset, size,
154 static inline void dma_unmap_page_attrs(struct device *dev,
155 dma_addr_t dma_address,
157 enum dma_data_direction direction,
158 struct dma_attrs *attrs)
160 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
163 dma_ops->unmap_single(dev, dma_address, size, direction, attrs);
166 static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
167 int nents, enum dma_data_direction direction,
168 struct dma_attrs *attrs)
170 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
173 return dma_ops->map_sg(dev, sg, nents, direction, attrs);
176 static inline void dma_unmap_sg_attrs(struct device *dev,
177 struct scatterlist *sg,
179 enum dma_data_direction direction,
180 struct dma_attrs *attrs)
182 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
185 dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs);
188 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
189 dma_addr_t *dma_handle, gfp_t flag)
191 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
194 return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
197 static inline void dma_free_coherent(struct device *dev, size_t size,
198 void *cpu_addr, dma_addr_t dma_handle)
200 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
203 dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
206 static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
208 enum dma_data_direction direction)
210 return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL);
213 static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
215 enum dma_data_direction direction)
217 dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL);
220 static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
221 unsigned long offset, size_t size,
222 enum dma_data_direction direction)
224 return dma_map_page_attrs(dev, page, offset, size, direction, NULL);
227 static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
229 enum dma_data_direction direction)
231 dma_unmap_page_attrs(dev, dma_address, size, direction, NULL);
234 static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
235 int nents, enum dma_data_direction direction)
237 return dma_map_sg_attrs(dev, sg, nents, direction, NULL);
240 static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
242 enum dma_data_direction direction)
244 dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL);
248 * Available generic sets of operations
250 extern struct dma_mapping_ops dma_iommu_ops;
251 extern struct dma_mapping_ops dma_direct_ops;
253 #else /* CONFIG_PPC64 */
255 #define dma_supported(dev, mask) (1)
257 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
259 if (!dev->dma_mask || !dma_supported(dev, mask))
262 *dev->dma_mask = dma_mask;
267 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
268 dma_addr_t * dma_handle,
271 #ifdef CONFIG_NOT_COHERENT_CACHE
272 return __dma_alloc_coherent(size, dma_handle, gfp);
275 /* ignore region specifiers */
276 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
278 if (dev == NULL || dev->coherent_dma_mask < 0xffffffff)
281 ret = (void *)__get_free_pages(gfp, get_order(size));
284 memset(ret, 0, size);
285 *dma_handle = virt_to_bus(ret);
293 dma_free_coherent(struct device *dev, size_t size, void *vaddr,
294 dma_addr_t dma_handle)
296 #ifdef CONFIG_NOT_COHERENT_CACHE
297 __dma_free_coherent(size, vaddr);
299 free_pages((unsigned long)vaddr, get_order(size));
303 static inline dma_addr_t
304 dma_map_single(struct device *dev, void *ptr, size_t size,
305 enum dma_data_direction direction)
307 BUG_ON(direction == DMA_NONE);
309 __dma_sync(ptr, size, direction);
311 return virt_to_bus(ptr);
314 static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
316 enum dma_data_direction direction)
321 static inline dma_addr_t
322 dma_map_page(struct device *dev, struct page *page,
323 unsigned long offset, size_t size,
324 enum dma_data_direction direction)
326 BUG_ON(direction == DMA_NONE);
328 __dma_sync_page(page, offset, size, direction);
330 return page_to_bus(page) + offset;
333 static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
335 enum dma_data_direction direction)
341 dma_map_sg(struct device *dev, 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 BUG_ON(!sg_page(sg));
351 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
352 sg->dma_address = page_to_bus(sg_page(sg)) + sg->offset;
358 static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
360 enum dma_data_direction direction)
362 /* We don't do anything here. */
365 #endif /* CONFIG_PPC64 */
367 static inline void dma_sync_single_for_cpu(struct device *dev,
368 dma_addr_t dma_handle, size_t size,
369 enum dma_data_direction direction)
371 BUG_ON(direction == DMA_NONE);
372 __dma_sync(bus_to_virt(dma_handle), size, 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)
379 BUG_ON(direction == DMA_NONE);
380 __dma_sync(bus_to_virt(dma_handle), size, direction);
383 static inline void dma_sync_sg_for_cpu(struct device *dev,
384 struct scatterlist *sgl, int nents,
385 enum dma_data_direction direction)
387 struct scatterlist *sg;
390 BUG_ON(direction == DMA_NONE);
392 for_each_sg(sgl, sg, nents, i)
393 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
396 static inline void dma_sync_sg_for_device(struct device *dev,
397 struct scatterlist *sgl, int nents,
398 enum dma_data_direction direction)
400 struct scatterlist *sg;
403 BUG_ON(direction == DMA_NONE);
405 for_each_sg(sgl, sg, nents, i)
406 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
409 static inline int dma_mapping_error(dma_addr_t dma_addr)
412 return (dma_addr == DMA_ERROR_CODE);
418 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
419 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
420 #ifdef CONFIG_NOT_COHERENT_CACHE
421 #define dma_is_consistent(d, h) (0)
423 #define dma_is_consistent(d, h) (1)
426 static inline int dma_get_cache_alignment(void)
429 /* no easy way to get cache size on all processors, so return
430 * the maximum possible, to be safe */
431 return (1 << INTERNODE_CACHE_SHIFT);
434 * Each processor family will define its own L1_CACHE_SHIFT,
435 * L1_CACHE_BYTES wraps to this, so this is always safe.
437 return L1_CACHE_BYTES;
441 static inline void dma_sync_single_range_for_cpu(struct device *dev,
442 dma_addr_t dma_handle, unsigned long offset, size_t size,
443 enum dma_data_direction direction)
445 /* just sync everything for now */
446 dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
449 static inline void dma_sync_single_range_for_device(struct device *dev,
450 dma_addr_t dma_handle, unsigned long offset, size_t size,
451 enum dma_data_direction direction)
453 /* just sync everything for now */
454 dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
457 static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
458 enum dma_data_direction direction)
460 BUG_ON(direction == DMA_NONE);
461 __dma_sync(vaddr, size, (int)direction);
464 #endif /* __KERNEL__ */
465 #endif /* _ASM_DMA_MAPPING_H */