sync hh.org
[hh.org.git] / include / asm-arm / dma-mapping.h
blobead6ea6a60ee20b38259c971049daee31ab5929a
1 #ifndef ASMARM_DMA_MAPPING_H
2 #define ASMARM_DMA_MAPPING_H
4 #ifdef __KERNEL__
6 #include <linux/mm.h> /* need struct page */
8 #include <asm/scatterlist.h>
10 #define ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
11 extern int
12 dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
13 dma_addr_t device_addr, size_t size, int flags);
15 extern void
16 dma_release_declared_memory(struct device *dev);
18 extern void *
19 dma_mark_declared_memory_occupied(struct device *dev,
20 dma_addr_t device_addr, size_t size);
23 * DMA-consistent mapping functions. These allocate/free a region of
24 * uncached, unwrite-buffered mapped memory space for use with DMA
25 * devices. This is the "generic" version. The PCI specific version
26 * is in pci.h
28 * Note: Drivers should NOT use this function directly, as it will break
29 * platforms with CONFIG_DMABOUNCE.
30 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
32 extern void consistent_sync(void *kaddr, size_t size, int rw);
35 * Return whether the given device DMA address mask can be supported
36 * properly. For example, if your device can only drive the low 24-bits
37 * during bus mastering, then you would pass 0x00ffffff as the mask
38 * to this function.
40 * FIXME: This should really be a platform specific issue - we should
41 * return false if GFP_DMA allocations may not satisfy the supplied 'mask'.
43 static inline int dma_supported(struct device *dev, u64 mask)
45 return dev->dma_mask && *dev->dma_mask != 0;
48 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
50 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
51 return -EIO;
53 *dev->dma_mask = dma_mask;
55 return 0;
58 static inline int dma_get_cache_alignment(void)
60 return 32;
63 static inline int dma_is_consistent(dma_addr_t handle)
65 return !!arch_is_coherent();
69 * DMA errors are defined by all-bits-set in the DMA address.
71 static inline int dma_mapping_error(dma_addr_t dma_addr)
73 return dma_addr == ~0;
76 /**
77 * dma_alloc_coherent - allocate consistent memory for DMA
78 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
79 * @size: required memory size
80 * @handle: bus-specific DMA address
82 * Allocate some uncached, unbuffered memory for a device for
83 * performing DMA. This function allocates pages, and will
84 * return the CPU-viewed address, and sets @handle to be the
85 * device-viewed address.
87 extern void *
88 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp);
90 /**
91 * dma_free_coherent - free memory allocated by dma_alloc_coherent
92 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
93 * @size: size of memory originally requested in dma_alloc_coherent
94 * @cpu_addr: CPU-view address returned from dma_alloc_coherent
95 * @handle: device-view address returned from dma_alloc_coherent
97 * Free (and unmap) a DMA buffer previously allocated by
98 * dma_alloc_coherent().
100 * References to memory and mappings associated with cpu_addr/handle
101 * during and after this call executing are illegal.
103 extern void
104 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
105 dma_addr_t handle);
108 * dma_mmap_coherent - map a coherent DMA allocation into user space
109 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
110 * @vma: vm_area_struct describing requested user mapping
111 * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
112 * @handle: device-view address returned from dma_alloc_coherent
113 * @size: size of memory originally requested in dma_alloc_coherent
115 * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
116 * into user space. The coherent DMA buffer must not be freed by the
117 * driver until the user space mapping has been released.
119 int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
120 void *cpu_addr, dma_addr_t handle, size_t size);
124 * dma_alloc_writecombine - allocate writecombining memory for DMA
125 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
126 * @size: required memory size
127 * @handle: bus-specific DMA address
129 * Allocate some uncached, buffered memory for a device for
130 * performing DMA. This function allocates pages, and will
131 * return the CPU-viewed address, and sets @handle to be the
132 * device-viewed address.
134 extern void *
135 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp);
137 #define dma_free_writecombine(dev,size,cpu_addr,handle) \
138 dma_free_coherent(dev,size,cpu_addr,handle)
140 int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
141 void *cpu_addr, dma_addr_t handle, size_t size);
145 * dma_map_single - map a single buffer for streaming DMA
146 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
147 * @cpu_addr: CPU direct mapped address of buffer
148 * @size: size of buffer to map
149 * @dir: DMA transfer direction
151 * Ensure that any data held in the cache is appropriately discarded
152 * or written back.
154 * The device owns this memory once this call has completed. The CPU
155 * can regain ownership by calling dma_unmap_single() or
156 * dma_sync_single_for_cpu().
158 #ifndef CONFIG_DMABOUNCE
159 static inline dma_addr_t
160 dma_map_single(struct device *dev, void *cpu_addr, size_t size,
161 enum dma_data_direction dir)
163 if (!arch_is_coherent())
164 consistent_sync(cpu_addr, size, dir);
166 return virt_to_dma(dev, (unsigned long)cpu_addr);
168 #else
169 extern dma_addr_t dma_map_single(struct device *,void *, size_t, enum dma_data_direction);
170 #endif
173 * dma_map_page - map a portion of a page for streaming DMA
174 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
175 * @page: page that buffer resides in
176 * @offset: offset into page for start of buffer
177 * @size: size of buffer to map
178 * @dir: DMA transfer direction
180 * Ensure that any data held in the cache is appropriately discarded
181 * or written back.
183 * The device owns this memory once this call has completed. The CPU
184 * can regain ownership by calling dma_unmap_page() or
185 * dma_sync_single_for_cpu().
187 static inline dma_addr_t
188 dma_map_page(struct device *dev, struct page *page,
189 unsigned long offset, size_t size,
190 enum dma_data_direction dir)
192 return dma_map_single(dev, page_address(page) + offset, size, (int)dir);
196 * dma_unmap_single - unmap a single buffer previously mapped
197 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
198 * @handle: DMA address of buffer
199 * @size: size of buffer to map
200 * @dir: DMA transfer direction
202 * Unmap a single streaming mode DMA translation. The handle and size
203 * must match what was provided in the previous dma_map_single() call.
204 * All other usages are undefined.
206 * After this call, reads by the CPU to the buffer are guaranteed to see
207 * whatever the device wrote there.
209 #ifndef CONFIG_DMABOUNCE
210 static inline void
211 dma_unmap_single(struct device *dev, dma_addr_t handle, size_t size,
212 enum dma_data_direction dir)
214 /* nothing to do */
216 #else
217 extern void dma_unmap_single(struct device *, dma_addr_t, size_t, enum dma_data_direction);
218 #endif
221 * dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
222 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
223 * @handle: DMA address of buffer
224 * @size: size of buffer to map
225 * @dir: DMA transfer direction
227 * Unmap a single streaming mode DMA translation. The handle and size
228 * must match what was provided in the previous dma_map_single() call.
229 * All other usages are undefined.
231 * After this call, reads by the CPU to the buffer are guaranteed to see
232 * whatever the device wrote there.
234 static inline void
235 dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
236 enum dma_data_direction dir)
238 dma_unmap_single(dev, handle, size, (int)dir);
242 * dma_map_sg - map a set of SG buffers for streaming mode DMA
243 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
244 * @sg: list of buffers
245 * @nents: number of buffers to map
246 * @dir: DMA transfer direction
248 * Map a set of buffers described by scatterlist in streaming
249 * mode for DMA. This is the scatter-gather version of the
250 * above dma_map_single interface. Here the scatter gather list
251 * elements are each tagged with the appropriate dma address
252 * and length. They are obtained via sg_dma_{address,length}(SG).
254 * NOTE: An implementation may be able to use a smaller number of
255 * DMA address/length pairs than there are SG table elements.
256 * (for example via virtual mapping capabilities)
257 * The routine returns the number of addr/length pairs actually
258 * used, at most nents.
260 * Device ownership issues as mentioned above for dma_map_single are
261 * the same here.
263 #ifndef CONFIG_DMABOUNCE
264 static inline int
265 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
266 enum dma_data_direction dir)
268 int i;
270 for (i = 0; i < nents; i++, sg++) {
271 char *virt;
273 sg->dma_address = page_to_dma(dev, sg->page) + sg->offset;
274 virt = page_address(sg->page) + sg->offset;
276 if (!arch_is_coherent())
277 consistent_sync(virt, sg->length, dir);
280 return nents;
282 #else
283 extern int dma_map_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
284 #endif
287 * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
288 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
289 * @sg: list of buffers
290 * @nents: number of buffers to map
291 * @dir: DMA transfer direction
293 * Unmap a set of streaming mode DMA translations.
294 * Again, CPU read rules concerning calls here are the same as for
295 * dma_unmap_single() above.
297 #ifndef CONFIG_DMABOUNCE
298 static inline void
299 dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
300 enum dma_data_direction dir)
303 /* nothing to do */
305 #else
306 extern void dma_unmap_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
307 #endif
311 * dma_sync_single_for_cpu
312 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
313 * @handle: DMA address of buffer
314 * @size: size of buffer to map
315 * @dir: DMA transfer direction
317 * Make physical memory consistent for a single streaming mode DMA
318 * translation after a transfer.
320 * If you perform a dma_map_single() but wish to interrogate the
321 * buffer using the cpu, yet do not wish to teardown the PCI dma
322 * mapping, you must call this function before doing so. At the
323 * next point you give the PCI dma address back to the card, you
324 * must first the perform a dma_sync_for_device, and then the
325 * device again owns the buffer.
327 #ifndef CONFIG_DMABOUNCE
328 static inline void
329 dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle, size_t size,
330 enum dma_data_direction dir)
332 if (!arch_is_coherent())
333 consistent_sync((void *)dma_to_virt(dev, handle), size, dir);
336 static inline void
337 dma_sync_single_for_device(struct device *dev, dma_addr_t handle, size_t size,
338 enum dma_data_direction dir)
340 if (!arch_is_coherent())
341 consistent_sync((void *)dma_to_virt(dev, handle), size, dir);
343 #else
344 extern void dma_sync_single_for_cpu(struct device*, dma_addr_t, size_t, enum dma_data_direction);
345 extern void dma_sync_single_for_device(struct device*, dma_addr_t, size_t, enum dma_data_direction);
346 #endif
350 * dma_sync_sg_for_cpu
351 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
352 * @sg: list of buffers
353 * @nents: number of buffers to map
354 * @dir: DMA transfer direction
356 * Make physical memory consistent for a set of streaming
357 * mode DMA translations after a transfer.
359 * The same as dma_sync_single_for_* but for a scatter-gather list,
360 * same rules and usage.
362 #ifndef CONFIG_DMABOUNCE
363 static inline void
364 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
365 enum dma_data_direction dir)
367 int i;
369 for (i = 0; i < nents; i++, sg++) {
370 char *virt = page_address(sg->page) + sg->offset;
371 if (!arch_is_coherent())
372 consistent_sync(virt, sg->length, dir);
376 static inline void
377 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
378 enum dma_data_direction dir)
380 int i;
382 for (i = 0; i < nents; i++, sg++) {
383 char *virt = page_address(sg->page) + sg->offset;
384 if (!arch_is_coherent())
385 consistent_sync(virt, sg->length, dir);
388 #else
389 extern void dma_sync_sg_for_cpu(struct device*, struct scatterlist*, int, enum dma_data_direction);
390 extern void dma_sync_sg_for_device(struct device*, struct scatterlist*, int, enum dma_data_direction);
391 #endif
393 #ifdef CONFIG_DMABOUNCE
395 * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
396 * and utilize bounce buffers as needed to work around limited DMA windows.
398 * On the SA-1111, a bug limits DMA to only certain regions of RAM.
399 * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
400 * On some ADI engineering sytems, PCI inbound window is 32MB (12MB total RAM)
402 * The following are helper functions used by the dmabounce subystem
407 * dmabounce_register_dev
409 * @dev: valid struct device pointer
410 * @small_buf_size: size of buffers to use with small buffer pool
411 * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
413 * This function should be called by low-level platform code to register
414 * a device as requireing DMA buffer bouncing. The function will allocate
415 * appropriate DMA pools for the device.
418 extern int dmabounce_register_dev(struct device *, unsigned long, unsigned long);
421 * dmabounce_unregister_dev
423 * @dev: valid struct device pointer
425 * This function should be called by low-level platform code when device
426 * that was previously registered with dmabounce_register_dev is removed
427 * from the system.
430 extern void dmabounce_unregister_dev(struct device *);
433 * dma_needs_bounce
435 * @dev: valid struct device pointer
436 * @dma_handle: dma_handle of unbounced buffer
437 * @size: size of region being mapped
439 * Platforms that utilize the dmabounce mechanism must implement
440 * this function.
442 * The dmabounce routines call this function whenever a dma-mapping
443 * is requested to determine whether a given buffer needs to be bounced
444 * or not. The function must return 0 if the the buffer is OK for
445 * DMA access and 1 if the buffer needs to be bounced.
448 extern int dma_needs_bounce(struct device*, dma_addr_t, size_t);
449 #endif /* CONFIG_DMABOUNCE */
451 #endif /* __KERNEL__ */
452 #endif