[MTD] Fix printk format warning in physmap. (resources again)
[linux-2.6/kvm.git] / include / asm-powerpc / dma-mapping.h
blob2ab9baf78bb4e40514bf317296903097ba110d47
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 <asm/scatterlist.h>
16 #include <asm/io.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) do { } while (0)
41 #define __dma_sync(addr, size, rw) do { } while (0)
42 #define __dma_sync_page(pg, off, sz, rw) do { } while (0)
44 #endif /* ! CONFIG_NOT_COHERENT_CACHE */
46 #ifdef CONFIG_PPC64
48 extern int dma_supported(struct device *dev, u64 mask);
49 extern int dma_set_mask(struct device *dev, u64 dma_mask);
50 extern void *dma_alloc_coherent(struct device *dev, size_t size,
51 dma_addr_t *dma_handle, gfp_t flag);
52 extern void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
53 dma_addr_t dma_handle);
54 extern dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
55 size_t size, enum dma_data_direction direction);
56 extern void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
57 size_t size, enum dma_data_direction direction);
58 extern dma_addr_t dma_map_page(struct device *dev, struct page *page,
59 unsigned long offset, size_t size,
60 enum dma_data_direction direction);
61 extern void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
62 size_t size, enum dma_data_direction direction);
63 extern int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
64 enum dma_data_direction direction);
65 extern void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
66 int nhwentries, enum dma_data_direction direction);
68 #else /* CONFIG_PPC64 */
70 #define dma_supported(dev, mask) (1)
72 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
74 if (!dev->dma_mask || !dma_supported(dev, mask))
75 return -EIO;
77 *dev->dma_mask = dma_mask;
79 return 0;
82 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
83 dma_addr_t * dma_handle,
84 gfp_t gfp)
86 #ifdef CONFIG_NOT_COHERENT_CACHE
87 return __dma_alloc_coherent(size, dma_handle, gfp);
88 #else
89 void *ret;
90 /* ignore region specifiers */
91 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
93 if (dev == NULL || dev->coherent_dma_mask < 0xffffffff)
94 gfp |= GFP_DMA;
96 ret = (void *)__get_free_pages(gfp, get_order(size));
98 if (ret != NULL) {
99 memset(ret, 0, size);
100 *dma_handle = virt_to_bus(ret);
103 return ret;
104 #endif
107 static inline void
108 dma_free_coherent(struct device *dev, size_t size, void *vaddr,
109 dma_addr_t dma_handle)
111 #ifdef CONFIG_NOT_COHERENT_CACHE
112 __dma_free_coherent(size, vaddr);
113 #else
114 free_pages((unsigned long)vaddr, get_order(size));
115 #endif
118 static inline dma_addr_t
119 dma_map_single(struct device *dev, void *ptr, size_t size,
120 enum dma_data_direction direction)
122 BUG_ON(direction == DMA_NONE);
124 __dma_sync(ptr, size, direction);
126 return virt_to_bus(ptr);
129 /* We do nothing. */
130 #define dma_unmap_single(dev, addr, size, dir) do { } while (0)
132 static inline dma_addr_t
133 dma_map_page(struct device *dev, struct page *page,
134 unsigned long offset, size_t size,
135 enum dma_data_direction direction)
137 BUG_ON(direction == DMA_NONE);
139 __dma_sync_page(page, offset, size, direction);
141 return page_to_bus(page) + offset;
144 /* We do nothing. */
145 #define dma_unmap_page(dev, handle, size, dir) do { } while (0)
147 static inline int
148 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
149 enum dma_data_direction direction)
151 int i;
153 BUG_ON(direction == DMA_NONE);
155 for (i = 0; i < nents; i++, sg++) {
156 BUG_ON(!sg->page);
157 __dma_sync_page(sg->page, sg->offset, sg->length, direction);
158 sg->dma_address = page_to_bus(sg->page) + sg->offset;
161 return nents;
164 /* We don't do anything here. */
165 #define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
167 #endif /* CONFIG_PPC64 */
169 static inline void dma_sync_single_for_cpu(struct device *dev,
170 dma_addr_t dma_handle, size_t size,
171 enum dma_data_direction direction)
173 BUG_ON(direction == DMA_NONE);
174 __dma_sync(bus_to_virt(dma_handle), size, direction);
177 static inline void dma_sync_single_for_device(struct device *dev,
178 dma_addr_t dma_handle, size_t size,
179 enum dma_data_direction direction)
181 BUG_ON(direction == DMA_NONE);
182 __dma_sync(bus_to_virt(dma_handle), size, direction);
185 static inline void dma_sync_sg_for_cpu(struct device *dev,
186 struct scatterlist *sg, int nents,
187 enum dma_data_direction direction)
189 int i;
191 BUG_ON(direction == DMA_NONE);
193 for (i = 0; i < nents; i++, sg++)
194 __dma_sync_page(sg->page, sg->offset, sg->length, direction);
197 static inline void dma_sync_sg_for_device(struct device *dev,
198 struct scatterlist *sg, int nents,
199 enum dma_data_direction direction)
201 int i;
203 BUG_ON(direction == DMA_NONE);
205 for (i = 0; i < nents; i++, sg++)
206 __dma_sync_page(sg->page, sg->offset, sg->length, direction);
209 static inline int dma_mapping_error(dma_addr_t dma_addr)
211 #ifdef CONFIG_PPC64
212 return (dma_addr == DMA_ERROR_CODE);
213 #else
214 return 0;
215 #endif
218 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
219 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
220 #ifdef CONFIG_NOT_COHERENT_CACHE
221 #define dma_is_consistent(d) (0)
222 #else
223 #define dma_is_consistent(d) (1)
224 #endif
226 static inline int dma_get_cache_alignment(void)
228 #ifdef CONFIG_PPC64
229 /* no easy way to get cache size on all processors, so return
230 * the maximum possible, to be safe */
231 return (1 << INTERNODE_CACHE_SHIFT);
232 #else
234 * Each processor family will define its own L1_CACHE_SHIFT,
235 * L1_CACHE_BYTES wraps to this, so this is always safe.
237 return L1_CACHE_BYTES;
238 #endif
241 static inline void dma_sync_single_range_for_cpu(struct device *dev,
242 dma_addr_t dma_handle, unsigned long offset, size_t size,
243 enum dma_data_direction direction)
245 /* just sync everything for now */
246 dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
249 static inline void dma_sync_single_range_for_device(struct device *dev,
250 dma_addr_t dma_handle, unsigned long offset, size_t size,
251 enum dma_data_direction direction)
253 /* just sync everything for now */
254 dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
257 static inline void dma_cache_sync(void *vaddr, size_t size,
258 enum dma_data_direction direction)
260 BUG_ON(direction == DMA_NONE);
261 __dma_sync(vaddr, size, (int)direction);
265 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
267 struct dma_mapping_ops {
268 void * (*alloc_coherent)(struct device *dev, size_t size,
269 dma_addr_t *dma_handle, gfp_t flag);
270 void (*free_coherent)(struct device *dev, size_t size,
271 void *vaddr, dma_addr_t dma_handle);
272 dma_addr_t (*map_single)(struct device *dev, void *ptr,
273 size_t size, enum dma_data_direction direction);
274 void (*unmap_single)(struct device *dev, dma_addr_t dma_addr,
275 size_t size, enum dma_data_direction direction);
276 int (*map_sg)(struct device *dev, struct scatterlist *sg,
277 int nents, enum dma_data_direction direction);
278 void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
279 int nents, enum dma_data_direction direction);
280 int (*dma_supported)(struct device *dev, u64 mask);
281 int (*dac_dma_supported)(struct device *dev, u64 mask);
284 #endif /* __KERNEL__ */
285 #endif /* _ASM_DMA_MAPPING_H */