blob: 2909efa163899e0ab0c6398c0d4489cc836bd580 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef ASMARM_DMA_MAPPING_H
2#define ASMARM_DMA_MAPPING_H
3
4#ifdef __KERNEL__
5
Russell King98ed7d42008-08-10 12:10:49 +01006#include <linux/mm_types.h>
Jens Axboedee9ba82007-10-23 12:37:59 +02007#include <linux/scatterlist.h>
Russell King24056f52011-01-03 11:29:28 +00008#include <linux/dma-debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +040010#include <asm-generic/dma-coherent.h>
Russell King98ed7d42008-08-10 12:10:49 +010011#include <asm/memory.h>
12
Marek Szyprowski1dc8f002012-02-29 14:45:28 +010013#define DMA_ERROR_CODE (~0)
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +010014extern struct dma_map_ops arm_dma_ops;
15
16static inline struct dma_map_ops *get_dma_ops(struct device *dev)
17{
18 if (dev && dev->archdata.dma_ops)
19 return dev->archdata.dma_ops;
20 return &arm_dma_ops;
21}
22
23static inline void set_dma_ops(struct device *dev, struct dma_map_ops *ops)
24{
25 BUG_ON(!dev);
26 dev->archdata.dma_ops = ops;
27}
28
29#include <asm-generic/dma-mapping-common.h>
30
31static inline int dma_set_mask(struct device *dev, u64 mask)
32{
33 return get_dma_ops(dev)->set_dma_mask(dev, mask);
34}
Marek Szyprowski1dc8f002012-02-29 14:45:28 +010035
Russell King9eedd962011-01-03 00:00:17 +000036#ifdef __arch_page_to_dma
37#error Please update to __arch_pfn_to_dma
38#endif
39
Russell King98ed7d42008-08-10 12:10:49 +010040/*
Russell King9eedd962011-01-03 00:00:17 +000041 * dma_to_pfn/pfn_to_dma/dma_to_virt/virt_to_dma are architecture private
42 * functions used internally by the DMA-mapping API to provide DMA
43 * addresses. They must not be used by drivers.
Russell King98ed7d42008-08-10 12:10:49 +010044 */
Russell King9eedd962011-01-03 00:00:17 +000045#ifndef __arch_pfn_to_dma
46static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
Nicolas Pitre58edb512008-09-09 15:54:13 -040047{
Russell King9eedd962011-01-03 00:00:17 +000048 return (dma_addr_t)__pfn_to_bus(pfn);
Nicolas Pitre58edb512008-09-09 15:54:13 -040049}
Russell King98ed7d42008-08-10 12:10:49 +010050
Russell King9eedd962011-01-03 00:00:17 +000051static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
Russell Kingef1baed2009-10-31 16:07:16 +000052{
Russell King9eedd962011-01-03 00:00:17 +000053 return __bus_to_pfn(addr);
Russell Kingef1baed2009-10-31 16:07:16 +000054}
55
Russell King98ed7d42008-08-10 12:10:49 +010056static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
57{
Catalin Marinas01f461a2011-08-23 13:59:14 +010058 return (void *)__bus_to_virt((unsigned long)addr);
Russell King98ed7d42008-08-10 12:10:49 +010059}
60
61static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
62{
63 return (dma_addr_t)__virt_to_bus((unsigned long)(addr));
64}
65#else
Russell King9eedd962011-01-03 00:00:17 +000066static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
Russell King98ed7d42008-08-10 12:10:49 +010067{
Russell King9eedd962011-01-03 00:00:17 +000068 return __arch_pfn_to_dma(dev, pfn);
Russell King98ed7d42008-08-10 12:10:49 +010069}
70
Russell King9eedd962011-01-03 00:00:17 +000071static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
Russell Kingef1baed2009-10-31 16:07:16 +000072{
Russell King9eedd962011-01-03 00:00:17 +000073 return __arch_dma_to_pfn(dev, addr);
Russell Kingef1baed2009-10-31 16:07:16 +000074}
75
Russell King98ed7d42008-08-10 12:10:49 +010076static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
77{
78 return __arch_dma_to_virt(dev, addr);
79}
80
81static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
82{
83 return __arch_virt_to_dma(dev, addr);
84}
85#endif
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +040086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Russell King18eabe22009-10-31 16:52:16 +000088 * The DMA API is built upon the notion of "buffer ownership". A buffer
89 * is either exclusively owned by the CPU (and therefore may be accessed
90 * by it) or exclusively owned by the DMA device. These helper functions
91 * represent the transitions between these two ownership states.
92 *
Russell King4ea0d732009-11-24 16:27:17 +000093 * Note, however, that on later ARMs, this notion does not work due to
94 * speculative prefetches. We model our approach on the assumption that
95 * the CPU does do speculative prefetches, which means we clean caches
96 * before transfers and delay cache invalidation until transfer completion.
97 *
98 * Private support functions: these are not part of the API and are
99 * liable to change. Drivers must not use these.
Russell King18eabe22009-10-31 16:52:16 +0000100 */
101static inline void __dma_single_cpu_to_dev(const void *kaddr, size_t size,
102 enum dma_data_direction dir)
103{
Russell King4ea0d732009-11-24 16:27:17 +0000104 extern void ___dma_single_cpu_to_dev(const void *, size_t,
105 enum dma_data_direction);
106
Russell King18eabe22009-10-31 16:52:16 +0000107 if (!arch_is_coherent())
Russell King4ea0d732009-11-24 16:27:17 +0000108 ___dma_single_cpu_to_dev(kaddr, size, dir);
Russell King18eabe22009-10-31 16:52:16 +0000109}
110
111static inline void __dma_single_dev_to_cpu(const void *kaddr, size_t size,
112 enum dma_data_direction dir)
113{
Russell King4ea0d732009-11-24 16:27:17 +0000114 extern void ___dma_single_dev_to_cpu(const void *, size_t,
115 enum dma_data_direction);
116
117 if (!arch_is_coherent())
118 ___dma_single_dev_to_cpu(kaddr, size, dir);
Russell King18eabe22009-10-31 16:52:16 +0000119}
120
121static inline void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
122 size_t size, enum dma_data_direction dir)
123{
Russell King4ea0d732009-11-24 16:27:17 +0000124 extern void ___dma_page_cpu_to_dev(struct page *, unsigned long,
125 size_t, enum dma_data_direction);
126
Russell King18eabe22009-10-31 16:52:16 +0000127 if (!arch_is_coherent())
Russell King4ea0d732009-11-24 16:27:17 +0000128 ___dma_page_cpu_to_dev(page, off, size, dir);
Russell King18eabe22009-10-31 16:52:16 +0000129}
130
131static inline void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
132 size_t size, enum dma_data_direction dir)
133{
Russell King4ea0d732009-11-24 16:27:17 +0000134 extern void ___dma_page_dev_to_cpu(struct page *, unsigned long,
135 size_t, enum dma_data_direction);
136
137 if (!arch_is_coherent())
138 ___dma_page_dev_to_cpu(page, off, size, dir);
Russell King18eabe22009-10-31 16:52:16 +0000139}
140
Russell King022ae532011-07-08 21:26:59 +0100141extern int dma_supported(struct device *, u64);
142extern int dma_set_mask(struct device *, u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * DMA errors are defined by all-bits-set in the DMA address.
145 */
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700146static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Marek Szyprowski1dc8f002012-02-29 14:45:28 +0100148 return dma_addr == DMA_ERROR_CODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Russell Kingf454aa62007-02-12 19:26:05 +0000151/*
152 * Dummy noncoherent implementation. We don't provide a dma_cache_sync
153 * function so drivers using this API are highlighted with build warnings.
154 */
Russell King3216a972008-09-25 22:23:31 +0100155static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
156 dma_addr_t *handle, gfp_t gfp)
Russell Kingf454aa62007-02-12 19:26:05 +0000157{
158 return NULL;
159}
160
Russell King3216a972008-09-25 22:23:31 +0100161static inline void dma_free_noncoherent(struct device *dev, size_t size,
162 void *cpu_addr, dma_addr_t handle)
Russell Kingf454aa62007-02-12 19:26:05 +0000163{
164}
165
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166
167/*
168 * dma_coherent_pre_ops - barrier functions for coherent memory before DMA.
169 * A barrier is required to ensure memory operations are complete before the
170 * initiation of a DMA xfer.
171 * If the coherent memory is Strongly Ordered
172 * - pre ARMv7 and 8x50 guarantees ordering wrt other mem accesses
173 * - ARMv7 guarantees ordering only within a 1KB block, so we need a barrier
174 * If coherent memory is normal then we need a barrier to prevent
175 * reordering
176 */
177static inline void dma_coherent_pre_ops(void)
178{
179#if COHERENT_IS_NORMAL == 1
180 dmb();
181#else
182 if (arch_is_coherent())
183 dmb();
184 else
185 barrier();
186#endif
187}
188/*
189 * dma_post_coherent_ops - barrier functions for coherent memory after DMA.
190 * If the coherent memory is Strongly Ordered we dont need a barrier since
191 * there are no speculative fetches to Strongly Ordered memory.
192 * If coherent memory is normal then we need a barrier to prevent reordering
193 */
194static inline void dma_coherent_post_ops(void)
195{
196#if COHERENT_IS_NORMAL == 1
197 dmb();
198#else
199 if (arch_is_coherent())
200 dmb();
201 else
202 barrier();
203#endif
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/**
207 * dma_alloc_coherent - allocate consistent memory for DMA
208 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
209 * @size: required memory size
210 * @handle: bus-specific DMA address
211 *
212 * Allocate some uncached, unbuffered memory for a device for
213 * performing DMA. This function allocates pages, and will
214 * return the CPU-viewed address, and sets @handle to be the
215 * device-viewed address.
216 */
Russell King3216a972008-09-25 22:23:31 +0100217extern void *dma_alloc_coherent(struct device *, size_t, dma_addr_t *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/**
220 * dma_free_coherent - free memory allocated by dma_alloc_coherent
221 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
222 * @size: size of memory originally requested in dma_alloc_coherent
223 * @cpu_addr: CPU-view address returned from dma_alloc_coherent
224 * @handle: device-view address returned from dma_alloc_coherent
225 *
226 * Free (and unmap) a DMA buffer previously allocated by
227 * dma_alloc_coherent().
228 *
229 * References to memory and mappings associated with cpu_addr/handle
230 * during and after this call executing are illegal.
231 */
Russell King3216a972008-09-25 22:23:31 +0100232extern void dma_free_coherent(struct device *, size_t, void *, dma_addr_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/**
235 * dma_mmap_coherent - map a coherent DMA allocation into user space
236 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
237 * @vma: vm_area_struct describing requested user mapping
238 * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
239 * @handle: device-view address returned from dma_alloc_coherent
240 * @size: size of memory originally requested in dma_alloc_coherent
241 *
242 * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
243 * into user space. The coherent DMA buffer must not be freed by the
244 * driver until the user space mapping has been released.
245 */
Russell King3216a972008-09-25 22:23:31 +0100246int dma_mmap_coherent(struct device *, struct vm_area_struct *,
247 void *, dma_addr_t, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249
250/**
251 * dma_alloc_writecombine - allocate writecombining memory for DMA
252 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
253 * @size: required memory size
254 * @handle: bus-specific DMA address
255 *
256 * Allocate some uncached, buffered memory for a device for
257 * performing DMA. This function allocates pages, and will
258 * return the CPU-viewed address, and sets @handle to be the
259 * device-viewed address.
260 */
Russell King3216a972008-09-25 22:23:31 +0100261extern void *dma_alloc_writecombine(struct device *, size_t, dma_addr_t *,
262 gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264#define dma_free_writecombine(dev,size,cpu_addr,handle) \
265 dma_free_coherent(dev,size,cpu_addr,handle)
266
Russell King3216a972008-09-25 22:23:31 +0100267int dma_mmap_writecombine(struct device *, struct vm_area_struct *,
268 void *, dma_addr_t, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jon Medhurst99d17172011-08-02 17:28:27 +0100270/*
271 * This can be called during boot to increase the size of the consistent
272 * DMA region above it's default value of 2MB. It must be called before the
273 * memory allocator is initialised, i.e. before any core_initcall.
274 */
275extern void __init init_consistent_dma_size(unsigned long size);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Russell King8c8a0ec2008-09-25 21:52:49 +0100278#ifdef CONFIG_DMABOUNCE
279/*
280 * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
281 * and utilize bounce buffers as needed to work around limited DMA windows.
282 *
283 * On the SA-1111, a bug limits DMA to only certain regions of RAM.
284 * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
285 * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
286 *
287 * The following are helper functions used by the dmabounce subystem
288 *
289 */
290
291/**
292 * dmabounce_register_dev
293 *
294 * @dev: valid struct device pointer
295 * @small_buf_size: size of buffers to use with small buffer pool
296 * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
Russell King0703ed22011-07-04 08:32:21 +0100297 * @needs_bounce_fn: called to determine whether buffer needs bouncing
Russell King8c8a0ec2008-09-25 21:52:49 +0100298 *
299 * This function should be called by low-level platform code to register
300 * a device as requireing DMA buffer bouncing. The function will allocate
301 * appropriate DMA pools for the device.
Russell King8c8a0ec2008-09-25 21:52:49 +0100302 */
Russell King3216a972008-09-25 22:23:31 +0100303extern int dmabounce_register_dev(struct device *, unsigned long,
Russell King0703ed22011-07-04 08:32:21 +0100304 unsigned long, int (*)(struct device *, dma_addr_t, size_t));
Russell King8c8a0ec2008-09-25 21:52:49 +0100305
306/**
307 * dmabounce_unregister_dev
308 *
309 * @dev: valid struct device pointer
310 *
311 * This function should be called by low-level platform code when device
312 * that was previously registered with dmabounce_register_dev is removed
313 * from the system.
314 *
315 */
316extern void dmabounce_unregister_dev(struct device *);
317
Russell King8c8a0ec2008-09-25 21:52:49 +0100318/*
Russell King125ab122008-09-25 22:16:22 +0100319 * The DMA API, implemented by dmabounce.c. See below for descriptions.
320 */
Russell King24056f52011-01-03 11:29:28 +0000321extern dma_addr_t __dma_map_page(struct device *, struct page *,
Russell King3216a972008-09-25 22:23:31 +0100322 unsigned long, size_t, enum dma_data_direction);
Russell King24056f52011-01-03 11:29:28 +0000323extern void __dma_unmap_page(struct device *, dma_addr_t, size_t,
Russell King3216a972008-09-25 22:23:31 +0100324 enum dma_data_direction);
Russell King125ab122008-09-25 22:16:22 +0100325
326/*
Russell King8c8a0ec2008-09-25 21:52:49 +0100327 * Private functions
328 */
Marek Szyprowski2d0d5ba2012-02-10 19:55:20 +0100329int dmabounce_sync_for_cpu(struct device *, dma_addr_t, size_t, enum dma_data_direction);
330int dmabounce_sync_for_device(struct device *, dma_addr_t, size_t, enum dma_data_direction);
Russell King8c8a0ec2008-09-25 21:52:49 +0100331#else
Russell King9fa76792008-11-13 14:33:51 +0000332static inline int dmabounce_sync_for_cpu(struct device *d, dma_addr_t addr,
Marek Szyprowski2d0d5ba2012-02-10 19:55:20 +0100333 size_t size, enum dma_data_direction dir)
Russell King9fa76792008-11-13 14:33:51 +0000334{
335 return 1;
336}
337
338static inline int dmabounce_sync_for_device(struct device *d, dma_addr_t addr,
Marek Szyprowski2d0d5ba2012-02-10 19:55:20 +0100339 size_t size, enum dma_data_direction dir)
Russell King9fa76792008-11-13 14:33:51 +0000340{
341 return 1;
342}
Russell King8c8a0ec2008-09-25 21:52:49 +0100343
344
Russell King24056f52011-01-03 11:29:28 +0000345static inline dma_addr_t __dma_map_page(struct device *dev, struct page *page,
346 unsigned long offset, size_t size, enum dma_data_direction dir)
347{
348 __dma_page_cpu_to_dev(page, offset, size, dir);
349 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
350}
351
Russell King24056f52011-01-03 11:29:28 +0000352static inline void __dma_unmap_page(struct device *dev, dma_addr_t handle,
353 size_t size, enum dma_data_direction dir)
354{
355 __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
356 handle & ~PAGE_MASK, size, dir);
357}
358#endif /* CONFIG_DMABOUNCE */
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/**
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 * dma_cache_pre_ops - clean or invalidate cache before dma transfer is
362 * initiated and perform a barrier operation.
363 * @virtual_addr: A kernel logical or kernel virtual address
364 * @size: size of buffer to map
365 * @dir: DMA transfer direction
366 *
367 * Ensure that any data held in the cache is appropriately discarded
368 * or written back.
369 *
370 */
371static inline void dma_cache_pre_ops(void *virtual_addr,
372 size_t size, enum dma_data_direction dir)
373{
374 extern void ___dma_single_cpu_to_dev(const void *, size_t,
375 enum dma_data_direction);
376
377 BUG_ON(!valid_dma_direction(dir));
378
379 if (!arch_is_coherent())
380 ___dma_single_cpu_to_dev(virtual_addr, size, dir);
381}
382
383/**
384 * dma_cache_post_ops - clean or invalidate cache after dma transfer is
385 * initiated and perform a barrier operation.
386 * @virtual_addr: A kernel logical or kernel virtual address
387 * @size: size of buffer to map
388 * @dir: DMA transfer direction
389 *
390 * Ensure that any data held in the cache is appropriately discarded
391 * or written back.
392 *
393 */
394static inline void dma_cache_post_ops(void *virtual_addr,
395 size_t size, enum dma_data_direction dir)
396{
397 extern void ___dma_single_cpu_to_dev(const void *, size_t,
398 enum dma_data_direction);
399
400 BUG_ON(!valid_dma_direction(dir));
401
402 if (arch_has_speculative_dfetch() && !arch_is_coherent()
403 && dir != DMA_TO_DEVICE)
404 /*
405 * Treat DMA_BIDIRECTIONAL and DMA_FROM_DEVICE
406 * identically: invalidate
407 */
408 ___dma_single_cpu_to_dev(virtual_addr,
409 size, DMA_FROM_DEVICE);
410}
Russell Kingafd1a322008-09-25 16:30:57 +0100411/*
412 * The scatter list versions of the above methods.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100414extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
415 enum dma_data_direction, struct dma_attrs *attrs);
416extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
417 enum dma_data_direction, struct dma_attrs *attrs);
418extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
Russell King3216a972008-09-25 22:23:31 +0100419 enum dma_data_direction);
Marek Szyprowskie9bb4d12012-02-10 19:55:20 +0100420extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
Russell King3216a972008-09-25 22:23:31 +0100421 enum dma_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#endif /* __KERNEL__ */
424#endif