blob: bed56f1ac83709887aa1c4b60922d9fa6602d354 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
Ralf Baechle9a88cbb2006-11-16 02:56:12 +00007 * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
9 */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000012#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
14#include <linux/module.h>
Jens Axboe4fcc47a2007-10-23 12:32:34 +020015#include <linux/scatterlist.h>
Ralf Baechle6e86b0b2007-10-29 19:35:33 +000016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/cache.h>
19#include <asm/io.h>
20
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000021#include <dma-coherence.h>
22
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010023static inline unsigned long dma_addr_to_virt(dma_addr_t dma_addr)
24{
25 unsigned long addr = plat_dma_addr_to_phys(dma_addr);
26
27 return (unsigned long)phys_to_virt(addr);
28}
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * Warning on the terminology - Linux calls an uncached area coherent;
32 * MIPS terminology calls memory areas with hardware maintained coherency
33 * coherent.
34 */
35
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000036static inline int cpu_is_noncoherent_r10000(struct device *dev)
37{
38 return !plat_device_is_coherent(dev) &&
Ralf Baechle10cc3522007-10-11 23:46:15 +010039 (current_cpu_type() == CPU_R10000 ||
40 current_cpu_type() == CPU_R12000);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000041}
42
Ralf Baechlecce335ae2007-11-03 02:05:43 +000043static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
44{
45 /* ignore region specifiers */
46 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
47
Thomas Bogendoerfer32016712007-12-30 12:45:40 +010048#ifdef CONFIG_ZONE_DMA
Ralf Baechlecce335ae2007-11-03 02:05:43 +000049 if (dev == NULL)
50 gfp |= __GFP_DMA;
51 else if (dev->coherent_dma_mask < DMA_BIT_MASK(24))
52 gfp |= __GFP_DMA;
53 else
54#endif
55#ifdef CONFIG_ZONE_DMA32
56 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
57 gfp |= __GFP_DMA32;
58 else
59#endif
60 ;
61
62 /* Don't invoke OOM killer */
63 gfp |= __GFP_NORETRY;
64
65 return gfp;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068void *dma_alloc_noncoherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -040069 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 void *ret;
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000072
Ralf Baechlecce335ae2007-11-03 02:05:43 +000073 gfp = massage_gfp_flags(dev, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 ret = (void *) __get_free_pages(gfp, get_order(size));
76
77 if (ret != NULL) {
78 memset(ret, 0, size);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000079 *dma_handle = plat_map_dma_mem(dev, ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
82 return ret;
83}
84
85EXPORT_SYMBOL(dma_alloc_noncoherent);
86
87void *dma_alloc_coherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -040088 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 void *ret;
91
Ralf Baechlecce335ae2007-11-03 02:05:43 +000092 gfp = massage_gfp_flags(dev, gfp);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000093
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000094 ret = (void *) __get_free_pages(gfp, get_order(size));
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (ret) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000097 memset(ret, 0, size);
98 *dma_handle = plat_map_dma_mem(dev, ret, size);
99
100 if (!plat_device_is_coherent(dev)) {
101 dma_cache_wback_inv((unsigned long) ret, size);
102 ret = UNCAC_ADDR(ret);
103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 return ret;
107}
108
109EXPORT_SYMBOL(dma_alloc_coherent);
110
111void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
112 dma_addr_t dma_handle)
113{
David Daney843aef42008-12-11 15:33:36 -0800114 plat_unmap_dma_mem(dev, dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 free_pages((unsigned long) vaddr, get_order(size));
116}
117
118EXPORT_SYMBOL(dma_free_noncoherent);
119
120void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
121 dma_addr_t dma_handle)
122{
123 unsigned long addr = (unsigned long) vaddr;
124
David Daney843aef42008-12-11 15:33:36 -0800125 plat_unmap_dma_mem(dev, dma_handle);
David Daney11531ac2008-12-10 18:14:45 -0800126
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000127 if (!plat_device_is_coherent(dev))
128 addr = CAC_ADDR(addr);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 free_pages(addr, get_order(size));
131}
132
133EXPORT_SYMBOL(dma_free_coherent);
134
135static inline void __dma_sync(unsigned long addr, size_t size,
136 enum dma_data_direction direction)
137{
138 switch (direction) {
139 case DMA_TO_DEVICE:
140 dma_cache_wback(addr, size);
141 break;
142
143 case DMA_FROM_DEVICE:
144 dma_cache_inv(addr, size);
145 break;
146
147 case DMA_BIDIRECTIONAL:
148 dma_cache_wback_inv(addr, size);
149 break;
150
151 default:
152 BUG();
153 }
154}
155
156dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
157 enum dma_data_direction direction)
158{
159 unsigned long addr = (unsigned long) ptr;
160
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000161 if (!plat_device_is_coherent(dev))
162 __dma_sync(addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000164 return plat_map_dma_mem(dev, ptr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167EXPORT_SYMBOL(dma_map_single);
168
169void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
170 enum dma_data_direction direction)
171{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000172 if (cpu_is_noncoherent_r10000(dev))
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100173 __dma_sync(dma_addr_to_virt(dma_addr), size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000174 direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David Daney843aef42008-12-11 15:33:36 -0800176 plat_unmap_dma_mem(dev, dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179EXPORT_SYMBOL(dma_unmap_single);
180
181int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
182 enum dma_data_direction direction)
183{
184 int i;
185
186 BUG_ON(direction == DMA_NONE);
187
188 for (i = 0; i < nents; i++, sg++) {
189 unsigned long addr;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700190
Jens Axboe58b053e2007-10-22 20:02:46 +0200191 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000192 if (!plat_device_is_coherent(dev) && addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200193 __dma_sync(addr, sg->length, direction);
Thomas Bogendoerferfbd56042007-05-18 14:32:36 +0200194 sg->dma_address = plat_map_dma_mem(dev,
Jens Axboe58b053e2007-10-22 20:02:46 +0200195 (void *)addr, sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
198 return nents;
199}
200
201EXPORT_SYMBOL(dma_map_sg);
202
203dma_addr_t dma_map_page(struct device *dev, struct page *page,
204 unsigned long offset, size_t size, enum dma_data_direction direction)
205{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 BUG_ON(direction == DMA_NONE);
207
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000208 if (!plat_device_is_coherent(dev)) {
209 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000211 addr = (unsigned long) page_address(page) + offset;
212 dma_cache_wback_inv(addr, size);
213 }
214
215 return plat_map_dma_mem_page(dev, page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218EXPORT_SYMBOL(dma_map_page);
219
220void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
221 enum dma_data_direction direction)
222{
223 BUG_ON(direction == DMA_NONE);
224
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000225 if (!plat_device_is_coherent(dev) && direction != DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 unsigned long addr;
227
Jan Nikitenkod7001192008-11-28 08:52:58 +0100228 addr = dma_addr_to_virt(dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 dma_cache_wback_inv(addr, size);
230 }
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000231
David Daney843aef42008-12-11 15:33:36 -0800232 plat_unmap_dma_mem(dev, dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235EXPORT_SYMBOL(dma_unmap_page);
236
237void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
238 enum dma_data_direction direction)
239{
240 unsigned long addr;
241 int i;
242
243 BUG_ON(direction == DMA_NONE);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 for (i = 0; i < nhwentries; i++, sg++) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000246 if (!plat_device_is_coherent(dev) &&
247 direction != DMA_TO_DEVICE) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200248 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000249 if (addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200250 __dma_sync(addr, sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000251 }
David Daney843aef42008-12-11 15:33:36 -0800252 plat_unmap_dma_mem(dev, sg->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254}
255
256EXPORT_SYMBOL(dma_unmap_sg);
257
258void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
259 size_t size, enum dma_data_direction direction)
260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700262
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000263 if (cpu_is_noncoherent_r10000(dev)) {
264 unsigned long addr;
265
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100266 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000267 __dma_sync(addr, size, direction);
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271EXPORT_SYMBOL(dma_sync_single_for_cpu);
272
273void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
274 size_t size, enum dma_data_direction direction)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 BUG_ON(direction == DMA_NONE);
277
David Daney843aef42008-12-11 15:33:36 -0800278 plat_extra_sync_for_device(dev);
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100279 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000280 unsigned long addr;
281
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100282 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000283 __dma_sync(addr, size, direction);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287EXPORT_SYMBOL(dma_sync_single_for_device);
288
289void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
290 unsigned long offset, size_t size, enum dma_data_direction direction)
291{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 BUG_ON(direction == DMA_NONE);
293
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000294 if (cpu_is_noncoherent_r10000(dev)) {
295 unsigned long addr;
296
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100297 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000298 __dma_sync(addr + offset, size, direction);
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
303
304void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
305 unsigned long offset, size_t size, enum dma_data_direction direction)
306{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 BUG_ON(direction == DMA_NONE);
308
David Daney843aef42008-12-11 15:33:36 -0800309 plat_extra_sync_for_device(dev);
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100310 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000311 unsigned long addr;
312
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100313 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000314 __dma_sync(addr + offset, size, direction);
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318EXPORT_SYMBOL(dma_sync_single_range_for_device);
319
320void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
321 enum dma_data_direction direction)
322{
323 int i;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000328 for (i = 0; i < nelems; i++, sg++) {
Ralf Baechle5b648a92007-03-02 11:42:11 +0000329 if (cpu_is_noncoherent_r10000(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200330 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000331 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335EXPORT_SYMBOL(dma_sync_sg_for_cpu);
336
337void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
338 enum dma_data_direction direction)
339{
340 int i;
341
342 BUG_ON(direction == DMA_NONE);
343
344 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000345 for (i = 0; i < nelems; i++, sg++) {
346 if (!plat_device_is_coherent(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200347 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000348 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352EXPORT_SYMBOL(dma_sync_sg_for_device);
353
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700354int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
David Daney843aef42008-12-11 15:33:36 -0800356 return plat_dma_mapping_error(dev, dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359EXPORT_SYMBOL(dma_mapping_error);
360
361int dma_supported(struct device *dev, u64 mask)
362{
David Daney843aef42008-12-11 15:33:36 -0800363 return plat_dma_supported(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366EXPORT_SYMBOL(dma_supported);
367
Ralf Baechlef67637e2006-12-06 20:38:54 -0800368int dma_is_consistent(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000370 return plat_device_is_coherent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373EXPORT_SYMBOL(dma_is_consistent);
374
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800375void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000376 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000378 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Daney843aef42008-12-11 15:33:36 -0800380 plat_extra_sync_for_device(dev);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000381 if (!plat_device_is_coherent(dev))
Thomas Bogendoerferc7c6b392007-11-27 19:31:33 +0100382 __dma_sync((unsigned long)vaddr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385EXPORT_SYMBOL(dma_cache_sync);