blob: b0b034c4654f8987f108fcf62ccf5d4e11f2ba0c [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>
15#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <asm/cache.h>
18#include <asm/io.h>
19
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000020#include <dma-coherence.h>
21
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010022static inline unsigned long dma_addr_to_virt(dma_addr_t dma_addr)
23{
24 unsigned long addr = plat_dma_addr_to_phys(dma_addr);
25
26 return (unsigned long)phys_to_virt(addr);
27}
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * Warning on the terminology - Linux calls an uncached area coherent;
31 * MIPS terminology calls memory areas with hardware maintained coherency
32 * coherent.
33 */
34
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000035static inline int cpu_is_noncoherent_r10000(struct device *dev)
36{
37 return !plat_device_is_coherent(dev) &&
Ralf Baechle10cc3522007-10-11 23:46:15 +010038 (current_cpu_type() == CPU_R10000 ||
39 current_cpu_type() == CPU_R12000);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000040}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042void *dma_alloc_noncoherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -040043 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 void *ret;
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 /* ignore region specifiers */
48 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
49
50 if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
51 gfp |= GFP_DMA;
52 ret = (void *) __get_free_pages(gfp, get_order(size));
53
54 if (ret != NULL) {
55 memset(ret, 0, size);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000056 *dma_handle = plat_map_dma_mem(dev, ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58
59 return ret;
60}
61
62EXPORT_SYMBOL(dma_alloc_noncoherent);
63
64void *dma_alloc_coherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -040065 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 void *ret;
68
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000069 /* ignore region specifiers */
70 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
71
72 if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
73 gfp |= GFP_DMA;
74 ret = (void *) __get_free_pages(gfp, get_order(size));
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (ret) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000077 memset(ret, 0, size);
78 *dma_handle = plat_map_dma_mem(dev, ret, size);
79
80 if (!plat_device_is_coherent(dev)) {
81 dma_cache_wback_inv((unsigned long) ret, size);
82 ret = UNCAC_ADDR(ret);
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 return ret;
87}
88
89EXPORT_SYMBOL(dma_alloc_coherent);
90
91void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
92 dma_addr_t dma_handle)
93{
94 free_pages((unsigned long) vaddr, get_order(size));
95}
96
97EXPORT_SYMBOL(dma_free_noncoherent);
98
99void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
100 dma_addr_t dma_handle)
101{
102 unsigned long addr = (unsigned long) vaddr;
103
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000104 if (!plat_device_is_coherent(dev))
105 addr = CAC_ADDR(addr);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 free_pages(addr, get_order(size));
108}
109
110EXPORT_SYMBOL(dma_free_coherent);
111
112static inline void __dma_sync(unsigned long addr, size_t size,
113 enum dma_data_direction direction)
114{
115 switch (direction) {
116 case DMA_TO_DEVICE:
117 dma_cache_wback(addr, size);
118 break;
119
120 case DMA_FROM_DEVICE:
121 dma_cache_inv(addr, size);
122 break;
123
124 case DMA_BIDIRECTIONAL:
125 dma_cache_wback_inv(addr, size);
126 break;
127
128 default:
129 BUG();
130 }
131}
132
133dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
134 enum dma_data_direction direction)
135{
136 unsigned long addr = (unsigned long) ptr;
137
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000138 if (!plat_device_is_coherent(dev))
139 __dma_sync(addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000141 return plat_map_dma_mem(dev, ptr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144EXPORT_SYMBOL(dma_map_single);
145
146void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
147 enum dma_data_direction direction)
148{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000149 if (cpu_is_noncoherent_r10000(dev))
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100150 __dma_sync(dma_addr_to_virt(dma_addr), size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000151 direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000153 plat_unmap_dma_mem(dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156EXPORT_SYMBOL(dma_unmap_single);
157
158int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
159 enum dma_data_direction direction)
160{
161 int i;
162
163 BUG_ON(direction == DMA_NONE);
164
165 for (i = 0; i < nents; i++, sg++) {
166 unsigned long addr;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700167
Jens Axboe58b053e2007-10-22 20:02:46 +0200168 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000169 if (!plat_device_is_coherent(dev) && addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200170 __dma_sync(addr, sg->length, direction);
Thomas Bogendoerferfbd56042007-05-18 14:32:36 +0200171 sg->dma_address = plat_map_dma_mem(dev,
Jens Axboe58b053e2007-10-22 20:02:46 +0200172 (void *)addr, sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
175 return nents;
176}
177
178EXPORT_SYMBOL(dma_map_sg);
179
180dma_addr_t dma_map_page(struct device *dev, struct page *page,
181 unsigned long offset, size_t size, enum dma_data_direction direction)
182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 BUG_ON(direction == DMA_NONE);
184
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000185 if (!plat_device_is_coherent(dev)) {
186 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000188 addr = (unsigned long) page_address(page) + offset;
189 dma_cache_wback_inv(addr, size);
190 }
191
192 return plat_map_dma_mem_page(dev, page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195EXPORT_SYMBOL(dma_map_page);
196
197void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
198 enum dma_data_direction direction)
199{
200 BUG_ON(direction == DMA_NONE);
201
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000202 if (!plat_device_is_coherent(dev) && direction != DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned long addr;
204
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000205 addr = plat_dma_addr_to_phys(dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 dma_cache_wback_inv(addr, size);
207 }
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000208
209 plat_unmap_dma_mem(dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212EXPORT_SYMBOL(dma_unmap_page);
213
214void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
215 enum dma_data_direction direction)
216{
217 unsigned long addr;
218 int i;
219
220 BUG_ON(direction == DMA_NONE);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 for (i = 0; i < nhwentries; i++, sg++) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000223 if (!plat_device_is_coherent(dev) &&
224 direction != DMA_TO_DEVICE) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200225 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000226 if (addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200227 __dma_sync(addr, sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000228 }
229 plat_unmap_dma_mem(sg->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231}
232
233EXPORT_SYMBOL(dma_unmap_sg);
234
235void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
236 size_t size, enum dma_data_direction direction)
237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700239
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000240 if (cpu_is_noncoherent_r10000(dev)) {
241 unsigned long addr;
242
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100243 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000244 __dma_sync(addr, size, direction);
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248EXPORT_SYMBOL(dma_sync_single_for_cpu);
249
250void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
251 size_t size, enum dma_data_direction direction)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 BUG_ON(direction == DMA_NONE);
254
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100255 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000256 unsigned long addr;
257
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100258 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000259 __dma_sync(addr, size, direction);
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263EXPORT_SYMBOL(dma_sync_single_for_device);
264
265void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
266 unsigned long offset, size_t size, enum dma_data_direction direction)
267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 BUG_ON(direction == DMA_NONE);
269
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000270 if (cpu_is_noncoherent_r10000(dev)) {
271 unsigned long addr;
272
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100273 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000274 __dma_sync(addr + offset, size, direction);
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
279
280void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
281 unsigned long offset, size_t size, enum dma_data_direction direction)
282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 BUG_ON(direction == DMA_NONE);
284
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100285 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000286 unsigned long addr;
287
Franck Bui-Huuc9d06962007-03-19 17:36:42 +0100288 addr = dma_addr_to_virt(dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000289 __dma_sync(addr + offset, size, direction);
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293EXPORT_SYMBOL(dma_sync_single_range_for_device);
294
295void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
296 enum dma_data_direction direction)
297{
298 int i;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000303 for (i = 0; i < nelems; i++, sg++) {
Ralf Baechle5b648a92007-03-02 11:42:11 +0000304 if (cpu_is_noncoherent_r10000(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200305 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000306 sg->length, direction);
307 plat_unmap_dma_mem(sg->dma_address);
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311EXPORT_SYMBOL(dma_sync_sg_for_cpu);
312
313void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
314 enum dma_data_direction direction)
315{
316 int i;
317
318 BUG_ON(direction == DMA_NONE);
319
320 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000321 for (i = 0; i < nelems; i++, sg++) {
322 if (!plat_device_is_coherent(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200323 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000324 sg->length, direction);
325 plat_unmap_dma_mem(sg->dma_address);
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329EXPORT_SYMBOL(dma_sync_sg_for_device);
330
331int dma_mapping_error(dma_addr_t dma_addr)
332{
333 return 0;
334}
335
336EXPORT_SYMBOL(dma_mapping_error);
337
338int dma_supported(struct device *dev, u64 mask)
339{
340 /*
341 * we fall back to GFP_DMA when the mask isn't all 1s,
342 * so we can't guarantee allocations that must be
343 * within a tighter range than GFP_DMA..
344 */
345 if (mask < 0x00ffffff)
346 return 0;
347
348 return 1;
349}
350
351EXPORT_SYMBOL(dma_supported);
352
Ralf Baechlef67637e2006-12-06 20:38:54 -0800353int dma_is_consistent(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000355 return plat_device_is_coherent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358EXPORT_SYMBOL(dma_is_consistent);
359
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800360void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000361 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000363 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000365 if (!plat_device_is_coherent(dev))
366 dma_cache_wback_inv((unsigned long)vaddr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369EXPORT_SYMBOL(dma_cache_sync);