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
6 * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
7 * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org>
8 * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
11 #include <linux/types.h>
12 #include <linux/dma-mapping.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/scatterlist.h>
18 #include <asm/cache.h>
21 #include <dma-coherence.h>
23 static inline unsigned long dma_addr_to_virt(dma_addr_t dma_addr
)
25 unsigned long addr
= plat_dma_addr_to_phys(dma_addr
);
27 return (unsigned long)phys_to_virt(addr
);
31 * Warning on the terminology - Linux calls an uncached area coherent;
32 * MIPS terminology calls memory areas with hardware maintained coherency
36 static inline int cpu_is_noncoherent_r10000(struct device
*dev
)
38 return !plat_device_is_coherent(dev
) &&
39 (current_cpu_type() == CPU_R10000
||
40 current_cpu_type() == CPU_R12000
);
43 void *dma_alloc_noncoherent(struct device
*dev
, size_t size
,
44 dma_addr_t
* dma_handle
, gfp_t gfp
)
48 /* ignore region specifiers */
49 gfp
&= ~(__GFP_DMA
| __GFP_HIGHMEM
);
51 if (dev
== NULL
|| (dev
->coherent_dma_mask
< 0xffffffff))
53 ret
= (void *) __get_free_pages(gfp
, get_order(size
));
57 *dma_handle
= plat_map_dma_mem(dev
, ret
, size
);
63 EXPORT_SYMBOL(dma_alloc_noncoherent
);
65 void *dma_alloc_coherent(struct device
*dev
, size_t size
,
66 dma_addr_t
* dma_handle
, gfp_t gfp
)
70 /* ignore region specifiers */
71 gfp
&= ~(__GFP_DMA
| __GFP_HIGHMEM
);
73 if (dev
== NULL
|| (dev
->coherent_dma_mask
< 0xffffffff))
75 ret
= (void *) __get_free_pages(gfp
, get_order(size
));
79 *dma_handle
= plat_map_dma_mem(dev
, ret
, size
);
81 if (!plat_device_is_coherent(dev
)) {
82 dma_cache_wback_inv((unsigned long) ret
, size
);
83 ret
= UNCAC_ADDR(ret
);
90 EXPORT_SYMBOL(dma_alloc_coherent
);
92 void dma_free_noncoherent(struct device
*dev
, size_t size
, void *vaddr
,
93 dma_addr_t dma_handle
)
95 free_pages((unsigned long) vaddr
, get_order(size
));
98 EXPORT_SYMBOL(dma_free_noncoherent
);
100 void dma_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
101 dma_addr_t dma_handle
)
103 unsigned long addr
= (unsigned long) vaddr
;
105 if (!plat_device_is_coherent(dev
))
106 addr
= CAC_ADDR(addr
);
108 free_pages(addr
, get_order(size
));
111 EXPORT_SYMBOL(dma_free_coherent
);
113 static inline void __dma_sync(unsigned long addr
, size_t size
,
114 enum dma_data_direction direction
)
118 dma_cache_wback(addr
, size
);
121 case DMA_FROM_DEVICE
:
122 dma_cache_inv(addr
, size
);
125 case DMA_BIDIRECTIONAL
:
126 dma_cache_wback_inv(addr
, size
);
134 dma_addr_t
dma_map_single(struct device
*dev
, void *ptr
, size_t size
,
135 enum dma_data_direction direction
)
137 unsigned long addr
= (unsigned long) ptr
;
139 if (!plat_device_is_coherent(dev
))
140 __dma_sync(addr
, size
, direction
);
142 return plat_map_dma_mem(dev
, ptr
, size
);
145 EXPORT_SYMBOL(dma_map_single
);
147 void dma_unmap_single(struct device
*dev
, dma_addr_t dma_addr
, size_t size
,
148 enum dma_data_direction direction
)
150 if (cpu_is_noncoherent_r10000(dev
))
151 __dma_sync(dma_addr_to_virt(dma_addr
), size
,
154 plat_unmap_dma_mem(dma_addr
);
157 EXPORT_SYMBOL(dma_unmap_single
);
159 int dma_map_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
160 enum dma_data_direction direction
)
164 BUG_ON(direction
== DMA_NONE
);
166 for (i
= 0; i
< nents
; i
++, sg
++) {
169 addr
= (unsigned long) sg_virt(sg
);
170 if (!plat_device_is_coherent(dev
) && addr
)
171 __dma_sync(addr
, sg
->length
, direction
);
172 sg
->dma_address
= plat_map_dma_mem(dev
,
173 (void *)addr
, sg
->length
);
179 EXPORT_SYMBOL(dma_map_sg
);
181 dma_addr_t
dma_map_page(struct device
*dev
, struct page
*page
,
182 unsigned long offset
, size_t size
, enum dma_data_direction direction
)
184 BUG_ON(direction
== DMA_NONE
);
186 if (!plat_device_is_coherent(dev
)) {
189 addr
= (unsigned long) page_address(page
) + offset
;
190 dma_cache_wback_inv(addr
, size
);
193 return plat_map_dma_mem_page(dev
, page
) + offset
;
196 EXPORT_SYMBOL(dma_map_page
);
198 void dma_unmap_page(struct device
*dev
, dma_addr_t dma_address
, size_t size
,
199 enum dma_data_direction direction
)
201 BUG_ON(direction
== DMA_NONE
);
203 if (!plat_device_is_coherent(dev
) && direction
!= DMA_TO_DEVICE
) {
206 addr
= plat_dma_addr_to_phys(dma_address
);
207 dma_cache_wback_inv(addr
, size
);
210 plat_unmap_dma_mem(dma_address
);
213 EXPORT_SYMBOL(dma_unmap_page
);
215 void dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
, int nhwentries
,
216 enum dma_data_direction direction
)
221 BUG_ON(direction
== DMA_NONE
);
223 for (i
= 0; i
< nhwentries
; i
++, sg
++) {
224 if (!plat_device_is_coherent(dev
) &&
225 direction
!= DMA_TO_DEVICE
) {
226 addr
= (unsigned long) sg_virt(sg
);
228 __dma_sync(addr
, sg
->length
, direction
);
230 plat_unmap_dma_mem(sg
->dma_address
);
234 EXPORT_SYMBOL(dma_unmap_sg
);
236 void dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
237 size_t size
, enum dma_data_direction direction
)
239 BUG_ON(direction
== DMA_NONE
);
241 if (cpu_is_noncoherent_r10000(dev
)) {
244 addr
= dma_addr_to_virt(dma_handle
);
245 __dma_sync(addr
, size
, direction
);
249 EXPORT_SYMBOL(dma_sync_single_for_cpu
);
251 void dma_sync_single_for_device(struct device
*dev
, dma_addr_t dma_handle
,
252 size_t size
, enum dma_data_direction direction
)
254 BUG_ON(direction
== DMA_NONE
);
256 if (!plat_device_is_coherent(dev
)) {
259 addr
= dma_addr_to_virt(dma_handle
);
260 __dma_sync(addr
, size
, direction
);
264 EXPORT_SYMBOL(dma_sync_single_for_device
);
266 void dma_sync_single_range_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
267 unsigned long offset
, size_t size
, enum dma_data_direction direction
)
269 BUG_ON(direction
== DMA_NONE
);
271 if (cpu_is_noncoherent_r10000(dev
)) {
274 addr
= dma_addr_to_virt(dma_handle
);
275 __dma_sync(addr
+ offset
, size
, direction
);
279 EXPORT_SYMBOL(dma_sync_single_range_for_cpu
);
281 void dma_sync_single_range_for_device(struct device
*dev
, dma_addr_t dma_handle
,
282 unsigned long offset
, size_t size
, enum dma_data_direction direction
)
284 BUG_ON(direction
== DMA_NONE
);
286 if (!plat_device_is_coherent(dev
)) {
289 addr
= dma_addr_to_virt(dma_handle
);
290 __dma_sync(addr
+ offset
, size
, direction
);
294 EXPORT_SYMBOL(dma_sync_single_range_for_device
);
296 void dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
, int nelems
,
297 enum dma_data_direction direction
)
301 BUG_ON(direction
== DMA_NONE
);
303 /* Make sure that gcc doesn't leave the empty loop body. */
304 for (i
= 0; i
< nelems
; i
++, sg
++) {
305 if (cpu_is_noncoherent_r10000(dev
))
306 __dma_sync((unsigned long)page_address(sg_page(sg
)),
307 sg
->length
, direction
);
308 plat_unmap_dma_mem(sg
->dma_address
);
312 EXPORT_SYMBOL(dma_sync_sg_for_cpu
);
314 void dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
, int nelems
,
315 enum dma_data_direction direction
)
319 BUG_ON(direction
== DMA_NONE
);
321 /* Make sure that gcc doesn't leave the empty loop body. */
322 for (i
= 0; i
< nelems
; i
++, sg
++) {
323 if (!plat_device_is_coherent(dev
))
324 __dma_sync((unsigned long)page_address(sg_page(sg
)),
325 sg
->length
, direction
);
326 plat_unmap_dma_mem(sg
->dma_address
);
330 EXPORT_SYMBOL(dma_sync_sg_for_device
);
332 int dma_mapping_error(dma_addr_t dma_addr
)
337 EXPORT_SYMBOL(dma_mapping_error
);
339 int dma_supported(struct device
*dev
, u64 mask
)
342 * we fall back to GFP_DMA when the mask isn't all 1s,
343 * so we can't guarantee allocations that must be
344 * within a tighter range than GFP_DMA..
346 if (mask
< 0x00ffffff)
352 EXPORT_SYMBOL(dma_supported
);
354 int dma_is_consistent(struct device
*dev
, dma_addr_t dma_addr
)
356 return plat_device_is_coherent(dev
);
359 EXPORT_SYMBOL(dma_is_consistent
);
361 void dma_cache_sync(struct device
*dev
, void *vaddr
, size_t size
,
362 enum dma_data_direction direction
)
364 BUG_ON(direction
== DMA_NONE
);
366 if (!plat_device_is_coherent(dev
))
367 dma_cache_wback_inv((unsigned long)vaddr
, size
);
370 EXPORT_SYMBOL(dma_cache_sync
);