blob: 4fc1a0fbe0074e154c64a89f8f40d90bd6e1bd91 [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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098EXPORT_SYMBOL(dma_alloc_noncoherent);
99
David Daney48e1fd52010-10-01 13:27:32 -0700100static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -0400101 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 void *ret;
104
Yoichi Yuasaf8ac0422009-06-04 00:16:04 +0900105 if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
106 return ret;
107
Ralf Baechlecce335a2007-11-03 02:05:43 +0000108 gfp = massage_gfp_flags(dev, gfp);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000109
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000110 ret = (void *) __get_free_pages(gfp, get_order(size));
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (ret) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000113 memset(ret, 0, size);
114 *dma_handle = plat_map_dma_mem(dev, ret, size);
115
116 if (!plat_device_is_coherent(dev)) {
117 dma_cache_wback_inv((unsigned long) ret, size);
118 ret = UNCAC_ADDR(ret);
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
122 return ret;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
127 dma_addr_t dma_handle)
128{
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700129 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 free_pages((unsigned long) vaddr, get_order(size));
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132EXPORT_SYMBOL(dma_free_noncoherent);
133
David Daney48e1fd52010-10-01 13:27:32 -0700134static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 dma_addr_t dma_handle)
136{
137 unsigned long addr = (unsigned long) vaddr;
Yoichi Yuasaf8ac0422009-06-04 00:16:04 +0900138 int order = get_order(size);
139
140 if (dma_release_from_coherent(dev, order, vaddr))
141 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700143 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
David Daney11531ac2008-12-10 18:14:45 -0800144
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000145 if (!plat_device_is_coherent(dev))
146 addr = CAC_ADDR(addr);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 free_pages(addr, get_order(size));
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static inline void __dma_sync(unsigned long addr, size_t size,
152 enum dma_data_direction direction)
153{
154 switch (direction) {
155 case DMA_TO_DEVICE:
156 dma_cache_wback(addr, size);
157 break;
158
159 case DMA_FROM_DEVICE:
160 dma_cache_inv(addr, size);
161 break;
162
163 case DMA_BIDIRECTIONAL:
164 dma_cache_wback_inv(addr, size);
165 break;
166
167 default:
168 BUG();
169 }
170}
171
David Daney48e1fd52010-10-01 13:27:32 -0700172static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
173 size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000175 if (cpu_is_noncoherent_r10000(dev))
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700176 __dma_sync(dma_addr_to_virt(dev, dma_addr), size,
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000177 direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700179 plat_unmap_dma_mem(dev, dma_addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
David Daney48e1fd52010-10-01 13:27:32 -0700182static int mips_dma_map_sg(struct device *dev, struct scatterlist *sg,
183 int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int i;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 for (i = 0; i < nents; i++, sg++) {
188 unsigned long addr;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700189
Jens Axboe58b053e2007-10-22 20:02:46 +0200190 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000191 if (!plat_device_is_coherent(dev) && addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200192 __dma_sync(addr, sg->length, direction);
Thomas Bogendoerferfbd56042007-05-18 14:32:36 +0200193 sg->dma_address = plat_map_dma_mem(dev,
Jens Axboe58b053e2007-10-22 20:02:46 +0200194 (void *)addr, sg->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197 return nents;
198}
199
David Daney48e1fd52010-10-01 13:27:32 -0700200static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
201 unsigned long offset, size_t size, enum dma_data_direction direction,
202 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
David Daney48e1fd52010-10-01 13:27:32 -0700204 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
David Daney48e1fd52010-10-01 13:27:32 -0700206 addr = (unsigned long) page_address(page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David Daney48e1fd52010-10-01 13:27:32 -0700208 if (!plat_device_is_coherent(dev))
Atsushi Nemoto4f29c052009-01-23 00:42:11 +0900209 __dma_sync(addr, size, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000210
David Daney48e1fd52010-10-01 13:27:32 -0700211 return plat_map_dma_mem(dev, (void *)addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
David Daney48e1fd52010-10-01 13:27:32 -0700214static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
215 int nhwentries, enum dma_data_direction direction,
216 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 unsigned long addr;
219 int i;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 for (i = 0; i < nhwentries; i++, sg++) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000222 if (!plat_device_is_coherent(dev) &&
223 direction != DMA_TO_DEVICE) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200224 addr = (unsigned long) sg_virt(sg);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000225 if (addr)
Jens Axboe58b053e2007-10-22 20:02:46 +0200226 __dma_sync(addr, sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000227 }
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700228 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230}
231
David Daney48e1fd52010-10-01 13:27:32 -0700232static void mips_dma_sync_single_for_cpu(struct device *dev,
233 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000235 if (cpu_is_noncoherent_r10000(dev)) {
236 unsigned long addr;
237
Kevin Cernekee3807ef32009-04-23 17:25:12 -0700238 addr = dma_addr_to_virt(dev, dma_handle);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000239 __dma_sync(addr, size, direction);
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
David Daney48e1fd52010-10-01 13:27:32 -0700243static void mips_dma_sync_single_for_device(struct device *dev,
244 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
David Daney843aef42008-12-11 15:33:36 -0800246 plat_extra_sync_for_device(dev);
Thomas Bogendoerfer9b43fb62007-02-23 19:58:48 +0100247 if (!plat_device_is_coherent(dev)) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000248 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
David Daney48e1fd52010-10-01 13:27:32 -0700255static void mips_dma_sync_sg_for_cpu(struct device *dev,
256 struct scatterlist *sg, int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 int i;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000261 for (i = 0; i < nelems; i++, sg++) {
Ralf Baechle5b648a92007-03-02 11:42:11 +0000262 if (cpu_is_noncoherent_r10000(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200263 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000264 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
David Daney48e1fd52010-10-01 13:27:32 -0700268static void mips_dma_sync_sg_for_device(struct device *dev,
269 struct scatterlist *sg, int nelems, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 int i;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Make sure that gcc doesn't leave the empty loop body. */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000274 for (i = 0; i < nelems; i++, sg++) {
275 if (!plat_device_is_coherent(dev))
Jens Axboe58b053e2007-10-22 20:02:46 +0200276 __dma_sync((unsigned long)page_address(sg_page(sg)),
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000277 sg->length, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
David Daney48e1fd52010-10-01 13:27:32 -0700281int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
David Daney843aef42008-12-11 15:33:36 -0800283 return plat_dma_mapping_error(dev, dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
David Daney48e1fd52010-10-01 13:27:32 -0700286int mips_dma_supported(struct device *dev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
David Daney843aef42008-12-11 15:33:36 -0800288 return plat_dma_supported(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
David Daney48e1fd52010-10-01 13:27:32 -0700291void mips_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
292 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000294 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
David Daney843aef42008-12-11 15:33:36 -0800296 plat_extra_sync_for_device(dev);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000297 if (!plat_device_is_coherent(dev))
Thomas Bogendoerferc7c6b392007-11-27 19:31:33 +0100298 __dma_sync((unsigned long)vaddr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
David Daney48e1fd52010-10-01 13:27:32 -0700301static struct dma_map_ops mips_default_dma_map_ops = {
302 .alloc_coherent = mips_dma_alloc_coherent,
303 .free_coherent = mips_dma_free_coherent,
304 .map_page = mips_dma_map_page,
305 .unmap_page = mips_dma_unmap_page,
306 .map_sg = mips_dma_map_sg,
307 .unmap_sg = mips_dma_unmap_sg,
308 .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
309 .sync_single_for_device = mips_dma_sync_single_for_device,
310 .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
311 .sync_sg_for_device = mips_dma_sync_sg_for_device,
312 .mapping_error = mips_dma_mapping_error,
313 .dma_supported = mips_dma_supported
314};
315
316struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
317EXPORT_SYMBOL(mips_dma_map_ops);
318
319#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
320
321static int __init mips_dma_init(void)
322{
323 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
324
325 return 0;
326}
327fs_initcall(mips_dma_init);