ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-arm / dma-mapping.h
1 #ifndef ASMARM_DMA_MAPPING_H
2 #define ASMARM_DMA_MAPPING_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/config.h>
7 #include <linux/mm.h> /* need struct page */
8
9 #include <asm/scatterlist.h>
10
11 /*
12  * DMA-consistent mapping functions.  These allocate/free a region of
13  * uncached, unwrite-buffered mapped memory space for use with DMA
14  * devices.  This is the "generic" version.  The PCI specific version
15  * is in pci.h
16  */
17 extern void consistent_sync(void *kaddr, size_t size, int rw);
18
19 /*
20  * Return whether the given device DMA address mask can be supported
21  * properly.  For example, if your device can only drive the low 24-bits
22  * during bus mastering, then you would pass 0x00ffffff as the mask
23  * to this function.
24  */
25 static inline int dma_supported(struct device *dev, u64 mask)
26 {
27         return dev->dma_mask && *dev->dma_mask != 0;
28 }
29
30 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
31 {
32         if (!dev->dma_mask || !dma_supported(dev, dma_mask))
33                 return -EIO;
34
35         *dev->dma_mask = dma_mask;
36
37         return 0;
38 }
39
40 static inline int dma_get_cache_alignment(void)
41 {
42         return 32;
43 }
44
45 static inline int dma_is_consistent(dma_addr_t handle)
46 {
47         return 0;
48 }
49
50 /*
51  * DMA errors are defined by all-bits-set in the DMA address.
52  */
53 static inline int dma_mapping_error(dma_addr_t dma_addr)
54 {
55         return dma_addr == ~0;
56 }
57
58 /**
59  * dma_alloc_coherent - allocate consistent memory for DMA
60  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
61  * @size: required memory size
62  * @handle: bus-specific DMA address
63  *
64  * Allocate some uncached, unbuffered memory for a device for
65  * performing DMA.  This function allocates pages, and will
66  * return the CPU-viewed address, and sets @handle to be the
67  * device-viewed address.
68  */
69 extern void *
70 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, int gfp);
71
72 /**
73  * dma_free_coherent - free memory allocated by dma_alloc_coherent
74  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
75  * @size: size of memory originally requested in dma_alloc_coherent
76  * @cpu_addr: CPU-view address returned from dma_alloc_coherent
77  * @handle: device-view address returned from dma_alloc_coherent
78  *
79  * Free (and unmap) a DMA buffer previously allocated by
80  * dma_alloc_coherent().
81  *
82  * References to memory and mappings associated with cpu_addr/handle
83  * during and after this call executing are illegal.
84  */
85 extern void
86 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
87                   dma_addr_t handle);
88
89 /**
90  * dma_alloc_writecombine - allocate writecombining memory for DMA
91  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
92  * @size: required memory size
93  * @handle: bus-specific DMA address
94  *
95  * Allocate some uncached, buffered memory for a device for
96  * performing DMA.  This function allocates pages, and will
97  * return the CPU-viewed address, and sets @handle to be the
98  * device-viewed address.
99  */
100 extern void *
101 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, int gfp);
102
103 #define dma_free_writecombine(dev,size,cpu_addr,handle) \
104         dma_free_coherent(dev,size,cpu_addr,handle)
105
106
107 /**
108  * dma_map_single - map a single buffer for streaming DMA
109  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
110  * @cpu_addr: CPU direct mapped address of buffer
111  * @size: size of buffer to map
112  * @dir: DMA transfer direction
113  *
114  * Ensure that any data held in the cache is appropriately discarded
115  * or written back.
116  *
117  * The device owns this memory once this call has completed.  The CPU
118  * can regain ownership by calling dma_unmap_single() or
119  * dma_sync_single_for_cpu().
120  */
121 #ifndef CONFIG_DMABOUNCE
122 static inline dma_addr_t
123 dma_map_single(struct device *dev, void *cpu_addr, size_t size,
124                enum dma_data_direction dir)
125 {
126         consistent_sync(cpu_addr, size, dir);
127         return __virt_to_bus((unsigned long)cpu_addr);
128 }
129 #else
130 extern dma_addr_t dma_map_single(struct device *,void *, size_t, enum dma_data_direction);
131 #endif
132
133 /**
134  * dma_map_page - map a portion of a page for streaming DMA
135  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
136  * @page: page that buffer resides in
137  * @offset: offset into page for start of buffer
138  * @size: size of buffer to map
139  * @dir: DMA transfer direction
140  *
141  * Ensure that any data held in the cache is appropriately discarded
142  * or written back.
143  *
144  * The device owns this memory once this call has completed.  The CPU
145  * can regain ownership by calling dma_unmap_page() or
146  * dma_sync_single_for_cpu().
147  */
148 static inline dma_addr_t
149 dma_map_page(struct device *dev, struct page *page,
150              unsigned long offset, size_t size,
151              enum dma_data_direction dir)
152 {
153         return dma_map_single(dev, page_address(page) + offset, size, (int)dir);
154 }
155
156 /**
157  * dma_unmap_single - unmap a single buffer previously mapped
158  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
159  * @handle: DMA address of buffer
160  * @size: size of buffer to map
161  * @dir: DMA transfer direction
162  *
163  * Unmap a single streaming mode DMA translation.  The handle and size
164  * must match what was provided in the previous dma_map_single() call.
165  * All other usages are undefined.
166  *
167  * After this call, reads by the CPU to the buffer are guaranteed to see
168  * whatever the device wrote there.
169  */
170 #ifndef CONFIG_DMABOUNCE
171 static inline void
172 dma_unmap_single(struct device *dev, dma_addr_t handle, size_t size,
173                  enum dma_data_direction dir)
174 {
175         /* nothing to do */
176 }
177 #else
178 extern void dma_unmap_single(struct device *, dma_addr_t, size_t, enum dma_data_direction);
179 #endif
180
181 /**
182  * dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
183  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
184  * @handle: DMA address of buffer
185  * @size: size of buffer to map
186  * @dir: DMA transfer direction
187  *
188  * Unmap a single streaming mode DMA translation.  The handle and size
189  * must match what was provided in the previous dma_map_single() call.
190  * All other usages are undefined.
191  *
192  * After this call, reads by the CPU to the buffer are guaranteed to see
193  * whatever the device wrote there.
194  */
195 static inline void
196 dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
197                enum dma_data_direction dir)
198 {
199         dma_unmap_single(dev, handle, size, (int)dir);
200 }
201
202 /**
203  * dma_map_sg - map a set of SG buffers for streaming mode DMA
204  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
205  * @sg: list of buffers
206  * @nents: number of buffers to map
207  * @dir: DMA transfer direction
208  *
209  * Map a set of buffers described by scatterlist in streaming
210  * mode for DMA.  This is the scatter-gather version of the
211  * above dma_map_single interface.  Here the scatter gather list
212  * elements are each tagged with the appropriate dma address
213  * and length.  They are obtained via sg_dma_{address,length}(SG).
214  *
215  * NOTE: An implementation may be able to use a smaller number of
216  *       DMA address/length pairs than there are SG table elements.
217  *       (for example via virtual mapping capabilities)
218  *       The routine returns the number of addr/length pairs actually
219  *       used, at most nents.
220  *
221  * Device ownership issues as mentioned above for dma_map_single are
222  * the same here.
223  */
224 #ifndef CONFIG_DMABOUNCE
225 static inline int
226 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
227            enum dma_data_direction dir)
228 {
229         int i;
230
231         for (i = 0; i < nents; i++, sg++) {
232                 char *virt;
233
234                 sg->dma_address = page_to_bus(sg->page) + sg->offset;
235                 virt = page_address(sg->page) + sg->offset;
236                 consistent_sync(virt, sg->length, dir);
237         }
238
239         return nents;
240 }
241 #else
242 extern int dma_map_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
243 #endif
244
245 /**
246  * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
247  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
248  * @sg: list of buffers
249  * @nents: number of buffers to map
250  * @dir: DMA transfer direction
251  *
252  * Unmap a set of streaming mode DMA translations.
253  * Again, CPU read rules concerning calls here are the same as for
254  * dma_unmap_single() above.
255  */
256 #ifndef CONFIG_DMABOUNCE
257 static inline void
258 dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
259              enum dma_data_direction dir)
260 {
261
262         /* nothing to do */
263 }
264 #else
265 extern void dma_unmap_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
266 #endif
267
268
269 /**
270  * dma_sync_single_for_cpu
271  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
272  * @handle: DMA address of buffer
273  * @size: size of buffer to map
274  * @dir: DMA transfer direction
275  *
276  * Make physical memory consistent for a single streaming mode DMA
277  * translation after a transfer.
278  *
279  * If you perform a dma_map_single() but wish to interrogate the
280  * buffer using the cpu, yet do not wish to teardown the PCI dma
281  * mapping, you must call this function before doing so.  At the
282  * next point you give the PCI dma address back to the card, you
283  * must first the perform a dma_sync_for_device, and then the
284  * device again owns the buffer.
285  */
286 #ifndef CONFIG_DMABOUNCE
287 static inline void
288 dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle, size_t size,
289                         enum dma_data_direction dir)
290 {
291         consistent_sync((void *)__bus_to_virt(handle), size, dir);
292 }
293
294 static inline void
295 dma_sync_single_for_device(struct device *dev, dma_addr_t handle, size_t size,
296                            enum dma_data_direction dir)
297 {
298         consistent_sync((void *)__bus_to_virt(handle), size, dir);
299 }
300 #else
301 extern void dma_sync_single_for_cpu(struct device*, dma_addr_t, size_t, enum dma_data_direction);
302 extern void dma_sync_single_for_device(struct device*, dma_addr_t, size_t, enum dma_data_direction);
303 #endif
304
305
306 /**
307  * dma_sync_sg_for_cpu
308  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
309  * @sg: list of buffers
310  * @nents: number of buffers to map
311  * @dir: DMA transfer direction
312  *
313  * Make physical memory consistent for a set of streaming
314  * mode DMA translations after a transfer.
315  *
316  * The same as dma_sync_single_for_* but for a scatter-gather list,
317  * same rules and usage.
318  */
319 #ifndef CONFIG_DMABOUNCE
320 static inline void
321 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
322                     enum dma_data_direction dir)
323 {
324         int i;
325
326         for (i = 0; i < nents; i++, sg++) {
327                 char *virt = page_address(sg->page) + sg->offset;
328                 consistent_sync(virt, sg->length, dir);
329         }
330 }
331
332 static inline void
333 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
334                        enum dma_data_direction dir)
335 {
336         int i;
337
338         for (i = 0; i < nents; i++, sg++) {
339                 char *virt = page_address(sg->page) + sg->offset;
340                 consistent_sync(virt, sg->length, dir);
341         }
342 }
343 #else
344 extern void dma_sync_sg_for_cpu(struct device*, struct scatterlist*, int, enum dma_data_direction);
345 extern void dma_sync_sg_for_device(struct device*, struct scatterlist*, int, enum dma_data_direction);
346 #endif
347
348 #ifdef CONFIG_DMABOUNCE
349 /*
350  * For SA-1111, IXP425, and ADI systems  the dma-mapping functions are "magic"
351  * and utilize bounce buffers as needed to work around limited DMA windows.
352  *
353  * On the SA-1111, a bug limits DMA to only certain regions of RAM.
354  * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
355  * On some ADI engineering sytems, PCI inbound window is 32MB (12MB total RAM)
356  *
357  * The following are helper functions used by the dmabounce subystem
358  *
359  */
360
361 /**
362  * dmabounce_register_dev
363  *
364  * @dev: valid struct device pointer
365  * @small_buf_size: size of buffers to use with small buffer pool
366  * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
367  *
368  * This function should be called by low-level platform code to register
369  * a device as requireing DMA buffer bouncing. The function will allocate
370  * appropriate DMA pools for the device.
371  *
372  */
373 extern int dmabounce_register_dev(struct device *, unsigned long, unsigned long);
374
375 /**
376  * dmabounce_unregister_dev
377  *
378  * @dev: valid struct device pointer
379  *
380  * This function should be called by low-level platform code when device
381  * that was previously registered with dmabounce_register_dev is removed
382  * from the system.
383  *
384  */
385 extern void dmabounce_unregister_dev(struct device *);
386
387 /**
388  * dma_needs_bounce
389  *
390  * @dev: valid struct device pointer
391  * @dma_handle: dma_handle of unbounced buffer
392  * @size: size of region being mapped
393  *
394  * Platforms that utilize the dmabounce mechanism must implement
395  * this function.
396  *
397  * The dmabounce routines call this function whenever a dma-mapping
398  * is requested to determine whether a given buffer needs to be bounced
399  * or not. The function must return 0 if the the buffer is OK for
400  * DMA access and 1 if the buffer needs to be bounced.
401  *
402  */
403 extern int dma_needs_bounce(struct device*, dma_addr_t, size_t);
404 #endif /* CONFIG_DMABOUNCE */
405
406 #endif /* __KERNEL__ */
407 #endif