blob: 469d4019f795bd072b0aa4ba109d075b5377f55c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/cache.h>
20#include <asm/io.h>
21
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000022#include <dma-coherence.h>
23
Kevin Cernekee3807ef32009-04-23 17:25:12 -070024static inline unsigned long dma_addr_to_virt(struct device *dev,
25 dma_addr_t dma_addr)
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010026{
Kevin Cernekee3807ef32009-04-23 17:25:12 -070027 unsigned long addr = plat_dma_addr_to_phys(dev, dma_addr);
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010028
29 return (unsigned long)phys_to_virt(addr);
30}
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * Warning on the terminology - Linux calls an uncached area coherent;
34 * MIPS terminology calls memory areas with hardware maintained coherency
35 * coherent.
36 */
37
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000038static inline int cpu_is_noncoherent_r10000(struct device *dev)
39{
40 return !plat_device_is_coherent(dev) &&
Ralf Baechle10cc3522007-10-11 23:46:15 +010041 (current_cpu_type() == CPU_R10000 ||
42 current_cpu_type() == CPU_R12000);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000043}
44
Ralf Baechlecce335a2007-11-03 02:05:43 +000045static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
46{
Ralf Baechlea2e715a2010-09-02 23:22:23 +020047 gfp_t dma_flag;
48
Ralf Baechlecce335a2007-11-03 02:05:43 +000049 /* ignore region specifiers */
50 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
51
Ralf Baechlea2e715a2010-09-02 23:22:23 +020052#ifdef CONFIG_ISA
Ralf Baechlecce335a2007-11-03 02:05:43 +000053 if (dev == NULL)
Ralf Baechlea2e715a2010-09-02 23:22:23 +020054 dma_flag = __GFP_DMA;
Ralf Baechlecce335a2007-11-03 02:05:43 +000055 else
56#endif
Ralf Baechlea2e715a2010-09-02 23:22:23 +020057#if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
Ralf Baechlecce335a2007-11-03 02:05:43 +000058 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Ralf Baechlea2e715a2010-09-02 23:22:23 +020059 dma_flag = __GFP_DMA;
60 else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
61 dma_flag = __GFP_DMA32;
Ralf Baechlecce335a2007-11-03 02:05:43 +000062 else
63#endif
Ralf Baechlea2e715a2010-09-02 23:22:23 +020064#if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
65 if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
66 dma_flag = __GFP_DMA32;
67 else
68#endif
69#if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
70 if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
71 dma_flag = __GFP_DMA;
72 else
73#endif
74 dma_flag = 0;
Ralf Baechlecce335a2007-11-03 02:05:43 +000075
76 /* Don't invoke OOM killer */
77 gfp |= __GFP_NORETRY;
78
Ralf Baechlea2e715a2010-09-02 23:22:23 +020079 return gfp | dma_flag;
Ralf Baechlecce335a2007-11-03 02:05:43 +000080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082void *dma_alloc_noncoherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -040083 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 void *ret;
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000086
Ralf Baechlecce335a2007-11-03 02:05:43 +000087 gfp = massage_gfp_flags(dev, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ret = (void *) __get_free_pages(gfp, get_order(size));
90
91 if (ret != NULL) {
92 memset(ret, 0, size);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000093 *dma_handle = plat_map_dma_mem(dev, ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96 return ret;
97}
98
99EXPORT_SYMBOL(dma_alloc_noncoherent);
100
101void *dma_alloc_coherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -0400102 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 void *ret;
105
Yoichi Yuasaf8ac0422009-06-04 00:16:04 +0900106 if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
107 return ret;
108
Ralf Baechlecce335a2007-11-03 02:05:43 +0000109 gfp = massage_gfp_flags(dev, gfp);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000110
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000111 ret = (void *) __get_free_pages(gfp, get_order(size));
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (ret) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000114 memset(ret, 0, size);
115 *dma_handle = plat_map_dma_mem(dev, ret, size);
116
117 if (!plat_device_is_coherent(dev)) {
118 dma_cache_wback_inv((unsigned long) ret, size);
119 ret = UNCAC_ADDR(ret);
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 return ret;
124}
125
126EXPORT_SYMBOL(dma_alloc_coherent);
127
128void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
129 dma_addr_t dma_handle)
130{
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700131 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 free_pages((unsigned long) vaddr, get_order(size));
133}
134
135EXPORT_SYMBOL(dma_free_noncoherent);
136
137void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
138 dma_addr_t dma_handle)
139{
140 unsigned long addr = (unsigned long) vaddr;
Yoichi Yuasaf8ac0422009-06-04 00:16:04 +0900141 int order = get_order(size);
142
143 if (dma_release_from_coherent(dev, order, vaddr))
144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700146 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
David Daney11531ac2008-12-10 18:14:45 -0800147
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000148 if (!plat_device_is_coherent(dev))
149 addr = CAC_ADDR(addr);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 free_pages(addr, get_order(size));
152}
153
154EXPORT_SYMBOL(dma_free_coherent);
155
156static inline void __dma_sync(unsigned long addr, size_t size,
157 enum dma_data_direction direction)
158{
159 switch (direction) {
160 case DMA_TO_DEVICE:
161 dma_cache_wback(addr, size);
162 break;
163
164 case DMA_FROM_DEVICE:
165 dma_cache_inv(addr, size);
166 break;
167
168 case DMA_BIDIRECTIONAL:
169 dma_cache_wback_inv(addr, size);
170 break;
171
172 default:
173 BUG();
174 }
175}
176
177dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
178 enum dma_data_direction direction)
179{
180 unsigned long addr = (unsigned long) ptr;
181
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000182 if (!plat_device_is_coherent(dev))
183 __dma_sync(addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000185 return plat_map_dma_mem(dev, ptr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188EXPORT_SYMBOL(dma_map_single);
189
190void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
191 enum dma_data_direction direction)
192{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000193 if (cpu_is_noncoherent_r10000(dev))
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700194 __dma_sync(dma_addr_to_virt(dev, dma_addr), size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000195 direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700197 plat_unmap_dma_mem(dev, dma_addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200EXPORT_SYMBOL(dma_unmap_single);
201
202int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
203 enum dma_data_direction direction)
204{
205 int i;
206
207 BUG_ON(direction == DMA_NONE);
208
209 for (i = 0; i < nents; i++, sg++) {
210 unsigned long addr;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700211
Jens Axboe58b053e2007-10-22 20:02:46 +0200212 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000213 if (!plat_device_is_coherent(dev) && addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200214 __dma_sync(addr, sg->length, direction);
Thomas Bogendoerferfbd56042007-05-18 14:32:36 +0200215 sg->dma_address = plat_map_dma_mem(dev,
Jens Axboe58b053e2007-10-22 20:02:46 +0200216 (void *)addr, sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
219 return nents;
220}
221
222EXPORT_SYMBOL(dma_map_sg);
223
224dma_addr_t dma_map_page(struct device *dev, struct page *page,
225 unsigned long offset, size_t size, enum dma_data_direction direction)
226{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 BUG_ON(direction == DMA_NONE);
228
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000229 if (!plat_device_is_coherent(dev)) {
230 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000232 addr = (unsigned long) page_address(page) + offset;
Atsushi Nemoto4f29c052009-01-23 00:42:11 +0900233 __dma_sync(addr, size, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000234 }
235
236 return plat_map_dma_mem_page(dev, page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239EXPORT_SYMBOL(dma_map_page);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
242 enum dma_data_direction direction)
243{
244 unsigned long addr;
245 int i;
246
247 BUG_ON(direction == DMA_NONE);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 for (i = 0; i < nhwentries; i++, sg++) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000250 if (!plat_device_is_coherent(dev) &&
251 direction != DMA_TO_DEVICE) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200252 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000253 if (addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200254 __dma_sync(addr, sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000255 }
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700256 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258}
259
260EXPORT_SYMBOL(dma_unmap_sg);
261
262void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
263 size_t size, enum dma_data_direction direction)
264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700266
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000267 if (cpu_is_noncoherent_r10000(dev)) {
268 unsigned long addr;
269
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700270 addr = dma_addr_to_virt(dev, dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000271 __dma_sync(addr, size, direction);
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275EXPORT_SYMBOL(dma_sync_single_for_cpu);
276
277void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
278 size_t size, enum dma_data_direction direction)
279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 BUG_ON(direction == DMA_NONE);
281
David Daney843aef42008-12-11 15:33:36 -0800282 plat_extra_sync_for_device(dev);
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100283 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000284 unsigned long addr;
285
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700286 addr = dma_addr_to_virt(dev, dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000287 __dma_sync(addr, size, direction);
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291EXPORT_SYMBOL(dma_sync_single_for_device);
292
293void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
294 unsigned long offset, size_t size, enum dma_data_direction direction)
295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 BUG_ON(direction == DMA_NONE);
297
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000298 if (cpu_is_noncoherent_r10000(dev)) {
299 unsigned long addr;
300
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700301 addr = dma_addr_to_virt(dev, dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000302 __dma_sync(addr + offset, size, direction);
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
307
308void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
309 unsigned long offset, size_t size, enum dma_data_direction direction)
310{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 BUG_ON(direction == DMA_NONE);
312
David Daney843aef42008-12-11 15:33:36 -0800313 plat_extra_sync_for_device(dev);
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100314 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000315 unsigned long addr;
316
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700317 addr = dma_addr_to_virt(dev, dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000318 __dma_sync(addr + offset, size, direction);
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322EXPORT_SYMBOL(dma_sync_single_range_for_device);
323
324void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
325 enum dma_data_direction direction)
326{
327 int i;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 BUG_ON(direction == DMA_NONE);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000332 for (i = 0; i < nelems; i++, sg++) {
Ralf Baechle5b648a92007-03-02 11:42:11 +0000333 if (cpu_is_noncoherent_r10000(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200334 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000335 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339EXPORT_SYMBOL(dma_sync_sg_for_cpu);
340
341void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
342 enum dma_data_direction direction)
343{
344 int i;
345
346 BUG_ON(direction == DMA_NONE);
347
348 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000349 for (i = 0; i < nelems; i++, sg++) {
350 if (!plat_device_is_coherent(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200351 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000352 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356EXPORT_SYMBOL(dma_sync_sg_for_device);
357
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700358int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David Daney843aef42008-12-11 15:33:36 -0800360 return plat_dma_mapping_error(dev, dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363EXPORT_SYMBOL(dma_mapping_error);
364
365int dma_supported(struct device *dev, u64 mask)
366{
David Daney843aef42008-12-11 15:33:36 -0800367 return plat_dma_supported(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370EXPORT_SYMBOL(dma_supported);
371
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800372void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000373 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000375 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David Daney843aef42008-12-11 15:33:36 -0800377 plat_extra_sync_for_device(dev);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000378 if (!plat_device_is_coherent(dev))
Thomas Bogendoerferc7c6b392007-11-27 19:31:33 +0100379 __dma_sync((unsigned long)vaddr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382EXPORT_SYMBOL(dma_cache_sync);