blob: 7e48e76148aa73aea2de549fb3e9439d16fb6f82 [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
Kevin Cernekee3807ef32009-04-23 17:25:12 -070023static inline unsigned long dma_addr_to_virt(struct device *dev,
24 dma_addr_t dma_addr)
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010025{
Kevin Cernekee3807ef32009-04-23 17:25:12 -070026 unsigned long addr = plat_dma_addr_to_phys(dev, dma_addr);
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010027
28 return (unsigned long)phys_to_virt(addr);
29}
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
32 * Warning on the terminology - Linux calls an uncached area coherent;
33 * MIPS terminology calls memory areas with hardware maintained coherency
34 * coherent.
35 */
36
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000037static inline int cpu_is_noncoherent_r10000(struct device *dev)
38{
39 return !plat_device_is_coherent(dev) &&
Ralf Baechle10cc3522007-10-11 23:46:15 +010040 (current_cpu_type() == CPU_R10000 ||
41 current_cpu_type() == CPU_R12000);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000042}
43
Ralf Baechlecce335a2007-11-03 02:05:43 +000044static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
45{
46 /* ignore region specifiers */
47 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
48
Thomas Bogendoerfer32016712007-12-30 12:45:40 +010049#ifdef CONFIG_ZONE_DMA
Ralf Baechlecce335a2007-11-03 02:05:43 +000050 if (dev == NULL)
51 gfp |= __GFP_DMA;
52 else if (dev->coherent_dma_mask < DMA_BIT_MASK(24))
53 gfp |= __GFP_DMA;
54 else
55#endif
56#ifdef CONFIG_ZONE_DMA32
57 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
58 gfp |= __GFP_DMA32;
59 else
60#endif
61 ;
62
63 /* Don't invoke OOM killer */
64 gfp |= __GFP_NORETRY;
65
66 return gfp;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069void *dma_alloc_noncoherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -040070 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 void *ret;
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000073
Ralf Baechlecce335a2007-11-03 02:05:43 +000074 gfp = massage_gfp_flags(dev, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 ret = (void *) __get_free_pages(gfp, get_order(size));
77
78 if (ret != NULL) {
79 memset(ret, 0, size);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000080 *dma_handle = plat_map_dma_mem(dev, ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
83 return ret;
84}
85
86EXPORT_SYMBOL(dma_alloc_noncoherent);
87
88void *dma_alloc_coherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -040089 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 void *ret;
92
Ralf Baechlecce335a2007-11-03 02:05:43 +000093 gfp = massage_gfp_flags(dev, gfp);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000094
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000095 ret = (void *) __get_free_pages(gfp, get_order(size));
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (ret) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000098 memset(ret, 0, size);
99 *dma_handle = plat_map_dma_mem(dev, ret, size);
100
101 if (!plat_device_is_coherent(dev)) {
102 dma_cache_wback_inv((unsigned long) ret, size);
103 ret = UNCAC_ADDR(ret);
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
107 return ret;
108}
109
110EXPORT_SYMBOL(dma_alloc_coherent);
111
112void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
113 dma_addr_t dma_handle)
114{
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700115 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 free_pages((unsigned long) vaddr, get_order(size));
117}
118
119EXPORT_SYMBOL(dma_free_noncoherent);
120
121void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
122 dma_addr_t dma_handle)
123{
124 unsigned long addr = (unsigned long) vaddr;
125
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700126 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
David Daney11531ac2008-12-10 18:14:45 -0800127
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000128 if (!plat_device_is_coherent(dev))
129 addr = CAC_ADDR(addr);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 free_pages(addr, get_order(size));
132}
133
134EXPORT_SYMBOL(dma_free_coherent);
135
136static inline void __dma_sync(unsigned long addr, size_t size,
137 enum dma_data_direction direction)
138{
139 switch (direction) {
140 case DMA_TO_DEVICE:
141 dma_cache_wback(addr, size);
142 break;
143
144 case DMA_FROM_DEVICE:
145 dma_cache_inv(addr, size);
146 break;
147
148 case DMA_BIDIRECTIONAL:
149 dma_cache_wback_inv(addr, size);
150 break;
151
152 default:
153 BUG();
154 }
155}
156
157dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
158 enum dma_data_direction direction)
159{
160 unsigned long addr = (unsigned long) ptr;
161
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000162 if (!plat_device_is_coherent(dev))
163 __dma_sync(addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000165 return plat_map_dma_mem(dev, ptr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168EXPORT_SYMBOL(dma_map_single);
169
170void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
171 enum dma_data_direction direction)
172{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000173 if (cpu_is_noncoherent_r10000(dev))
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700174 __dma_sync(dma_addr_to_virt(dev, dma_addr), size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000175 direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700177 plat_unmap_dma_mem(dev, dma_addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180EXPORT_SYMBOL(dma_unmap_single);
181
182int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
183 enum dma_data_direction direction)
184{
185 int i;
186
187 BUG_ON(direction == DMA_NONE);
188
189 for (i = 0; i < nents; i++, sg++) {
190 unsigned long addr;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700191
Jens Axboe58b053e2007-10-22 20:02:46 +0200192 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000193 if (!plat_device_is_coherent(dev) && addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200194 __dma_sync(addr, sg->length, direction);
Thomas Bogendoerferfbd56042007-05-18 14:32:36 +0200195 sg->dma_address = plat_map_dma_mem(dev,
Jens Axboe58b053e2007-10-22 20:02:46 +0200196 (void *)addr, sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
199 return nents;
200}
201
202EXPORT_SYMBOL(dma_map_sg);
203
204dma_addr_t dma_map_page(struct device *dev, struct page *page,
205 unsigned long offset, size_t size, enum dma_data_direction direction)
206{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 BUG_ON(direction == DMA_NONE);
208
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000209 if (!plat_device_is_coherent(dev)) {
210 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000212 addr = (unsigned long) page_address(page) + offset;
Atsushi Nemoto4f29c052009-01-23 00:42:11 +0900213 __dma_sync(addr, size, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000214 }
215
216 return plat_map_dma_mem_page(dev, page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219EXPORT_SYMBOL(dma_map_page);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
222 enum dma_data_direction direction)
223{
224 unsigned long addr;
225 int i;
226
227 BUG_ON(direction == DMA_NONE);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 for (i = 0; i < nhwentries; i++, sg++) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000230 if (!plat_device_is_coherent(dev) &&
231 direction != DMA_TO_DEVICE) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200232 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000233 if (addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200234 __dma_sync(addr, sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000235 }
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700236 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238}
239
240EXPORT_SYMBOL(dma_unmap_sg);
241
242void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
243 size_t size, enum dma_data_direction direction)
244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700246
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000247 if (cpu_is_noncoherent_r10000(dev)) {
248 unsigned long addr;
249
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700250 addr = dma_addr_to_virt(dev, dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000251 __dma_sync(addr, size, direction);
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255EXPORT_SYMBOL(dma_sync_single_for_cpu);
256
257void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
258 size_t size, enum dma_data_direction direction)
259{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 BUG_ON(direction == DMA_NONE);
261
David Daney843aef42008-12-11 15:33:36 -0800262 plat_extra_sync_for_device(dev);
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100263 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000264 unsigned long addr;
265
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700266 addr = dma_addr_to_virt(dev, 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_device);
272
273void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
274 unsigned long offset, size_t size, enum dma_data_direction direction)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 BUG_ON(direction == DMA_NONE);
277
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000278 if (cpu_is_noncoherent_r10000(dev)) {
279 unsigned long addr;
280
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700281 addr = dma_addr_to_virt(dev, dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000282 __dma_sync(addr + offset, size, direction);
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
287
288void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
289 unsigned long offset, size_t size, enum dma_data_direction direction)
290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 BUG_ON(direction == DMA_NONE);
292
David Daney843aef42008-12-11 15:33:36 -0800293 plat_extra_sync_for_device(dev);
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100294 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000295 unsigned long addr;
296
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700297 addr = dma_addr_to_virt(dev, 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_device);
303
304void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
305 enum dma_data_direction direction)
306{
307 int i;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000312 for (i = 0; i < nelems; i++, sg++) {
Ralf Baechle5b648a92007-03-02 11:42:11 +0000313 if (cpu_is_noncoherent_r10000(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200314 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000315 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319EXPORT_SYMBOL(dma_sync_sg_for_cpu);
320
321void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
322 enum dma_data_direction direction)
323{
324 int i;
325
326 BUG_ON(direction == DMA_NONE);
327
328 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000329 for (i = 0; i < nelems; i++, sg++) {
330 if (!plat_device_is_coherent(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200331 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000332 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336EXPORT_SYMBOL(dma_sync_sg_for_device);
337
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700338int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
David Daney843aef42008-12-11 15:33:36 -0800340 return plat_dma_mapping_error(dev, dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343EXPORT_SYMBOL(dma_mapping_error);
344
345int dma_supported(struct device *dev, u64 mask)
346{
David Daney843aef42008-12-11 15:33:36 -0800347 return plat_dma_supported(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350EXPORT_SYMBOL(dma_supported);
351
Ralf Baechlef67637e2006-12-06 20:38:54 -0800352int dma_is_consistent(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000354 return plat_device_is_coherent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357EXPORT_SYMBOL(dma_is_consistent);
358
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800359void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000360 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000362 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David Daney843aef42008-12-11 15:33:36 -0800364 plat_extra_sync_for_device(dev);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000365 if (!plat_device_is_coherent(dev))
Thomas Bogendoerferc7c6b392007-11-27 19:31:33 +0100366 __dma_sync((unsigned long)vaddr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369EXPORT_SYMBOL(dma_cache_sync);