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/scatterlist.h>
16 #include <linux/string.h>
18 #include <asm/cache.h>
21 #include <dma-coherence.h>
23 static inline unsigned long dma_addr_to_virt(struct device
*dev
,
26 unsigned long addr
= plat_dma_addr_to_phys(dev
, dma_addr
);
28 return (unsigned long)phys_to_virt(addr
);
32 * Warning on the terminology - Linux calls an uncached area coherent;
33 * MIPS terminology calls memory areas with hardware maintained coherency
37 static inline int cpu_is_noncoherent_r10000(struct device
*dev
)
39 return !plat_device_is_coherent(dev
) &&
40 (current_cpu_type() == CPU_R10000
||
41 current_cpu_type() == CPU_R12000
);
44 static gfp_t
massage_gfp_flags(const struct device
*dev
, gfp_t gfp
)
46 /* ignore region specifiers */
47 gfp
&= ~(__GFP_DMA
| __GFP_DMA32
| __GFP_HIGHMEM
);
49 #ifdef CONFIG_ZONE_DMA
52 else if (dev
->coherent_dma_mask
< DMA_BIT_MASK(24))
56 #ifdef CONFIG_ZONE_DMA32
57 if (dev
->coherent_dma_mask
< DMA_BIT_MASK(32))
63 /* Don't invoke OOM killer */
69 void *dma_alloc_noncoherent(struct device
*dev
, size_t size
,
70 dma_addr_t
* dma_handle
, gfp_t gfp
)
74 gfp
= massage_gfp_flags(dev
, gfp
);
76 ret
= (void *) __get_free_pages(gfp
, get_order(size
));
80 *dma_handle
= plat_map_dma_mem(dev
, ret
, size
);
86 EXPORT_SYMBOL(dma_alloc_noncoherent
);
88 void *dma_alloc_coherent(struct device
*dev
, size_t size
,
89 dma_addr_t
* dma_handle
, gfp_t gfp
)
93 gfp
= massage_gfp_flags(dev
, gfp
);
95 ret
= (void *) __get_free_pages(gfp
, get_order(size
));
99 *dma_handle
= plat_map_dma_mem(dev
, ret
, size
);
101 if (!plat_device_is_coherent(dev
)) {
102 dma_cache_wback_inv((unsigned long) ret
, size
);
103 ret
= UNCAC_ADDR(ret
);
110 EXPORT_SYMBOL(dma_alloc_coherent
);
112 void dma_free_noncoherent(struct device
*dev
, size_t size
, void *vaddr
,
113 dma_addr_t dma_handle
)
115 plat_unmap_dma_mem(dev
, dma_handle
, size
, DMA_BIDIRECTIONAL
);
116 free_pages((unsigned long) vaddr
, get_order(size
));
119 EXPORT_SYMBOL(dma_free_noncoherent
);
121 void dma_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
122 dma_addr_t dma_handle
)
124 unsigned long addr
= (unsigned long) vaddr
;
126 plat_unmap_dma_mem(dev
, dma_handle
, size
, DMA_BIDIRECTIONAL
);
128 if (!plat_device_is_coherent(dev
))
129 addr
= CAC_ADDR(addr
);
131 free_pages(addr
, get_order(size
));
134 EXPORT_SYMBOL(dma_free_coherent
);
136 static inline void __dma_sync(unsigned long addr
, size_t size
,
137 enum dma_data_direction direction
)
141 dma_cache_wback(addr
, size
);
144 case DMA_FROM_DEVICE
:
145 dma_cache_inv(addr
, size
);
148 case DMA_BIDIRECTIONAL
:
149 dma_cache_wback_inv(addr
, size
);
157 dma_addr_t
dma_map_single(struct device
*dev
, void *ptr
, size_t size
,
158 enum dma_data_direction direction
)
160 unsigned long addr
= (unsigned long) ptr
;
162 if (!plat_device_is_coherent(dev
))
163 __dma_sync(addr
, size
, direction
);
165 return plat_map_dma_mem(dev
, ptr
, size
);
168 EXPORT_SYMBOL(dma_map_single
);
170 void dma_unmap_single(struct device
*dev
, dma_addr_t dma_addr
, size_t size
,
171 enum dma_data_direction direction
)
173 if (cpu_is_noncoherent_r10000(dev
))
174 __dma_sync(dma_addr_to_virt(dev
, dma_addr
), size
,
177 plat_unmap_dma_mem(dev
, dma_addr
, size
, direction
);
180 EXPORT_SYMBOL(dma_unmap_single
);
182 int dma_map_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
183 enum dma_data_direction direction
)
187 BUG_ON(direction
== DMA_NONE
);
189 for (i
= 0; i
< nents
; i
++, sg
++) {
192 addr
= (unsigned long) sg_virt(sg
);
193 if (!plat_device_is_coherent(dev
) && addr
)
194 __dma_sync(addr
, sg
->length
, direction
);
195 sg
->dma_address
= plat_map_dma_mem(dev
,
196 (void *)addr
, sg
->length
);
202 EXPORT_SYMBOL(dma_map_sg
);
204 dma_addr_t
dma_map_page(struct device
*dev
, struct page
*page
,
205 unsigned long offset
, size_t size
, enum dma_data_direction direction
)
207 BUG_ON(direction
== DMA_NONE
);
209 if (!plat_device_is_coherent(dev
)) {
212 addr
= (unsigned long) page_address(page
) + offset
;
213 __dma_sync(addr
, size
, direction
);
216 return plat_map_dma_mem_page(dev
, page
) + offset
;
219 EXPORT_SYMBOL(dma_map_page
);
221 void dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
, int nhwentries
,
222 enum dma_data_direction direction
)
227 BUG_ON(direction
== DMA_NONE
);
229 for (i
= 0; i
< nhwentries
; i
++, sg
++) {
230 if (!plat_device_is_coherent(dev
) &&
231 direction
!= DMA_TO_DEVICE
) {
232 addr
= (unsigned long) sg_virt(sg
);
234 __dma_sync(addr
, sg
->length
, direction
);
236 plat_unmap_dma_mem(dev
, sg
->dma_address
, sg
->length
, direction
);
240 EXPORT_SYMBOL(dma_unmap_sg
);
242 void dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
243 size_t size
, enum dma_data_direction direction
)
245 BUG_ON(direction
== DMA_NONE
);
247 if (cpu_is_noncoherent_r10000(dev
)) {
250 addr
= dma_addr_to_virt(dev
, dma_handle
);
251 __dma_sync(addr
, size
, direction
);
255 EXPORT_SYMBOL(dma_sync_single_for_cpu
);
257 void dma_sync_single_for_device(struct device
*dev
, dma_addr_t dma_handle
,
258 size_t size
, enum dma_data_direction direction
)
260 BUG_ON(direction
== DMA_NONE
);
262 plat_extra_sync_for_device(dev
);
263 if (!plat_device_is_coherent(dev
)) {
266 addr
= dma_addr_to_virt(dev
, dma_handle
);
267 __dma_sync(addr
, size
, direction
);
271 EXPORT_SYMBOL(dma_sync_single_for_device
);
273 void 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
)
276 BUG_ON(direction
== DMA_NONE
);
278 if (cpu_is_noncoherent_r10000(dev
)) {
281 addr
= dma_addr_to_virt(dev
, dma_handle
);
282 __dma_sync(addr
+ offset
, size
, direction
);
286 EXPORT_SYMBOL(dma_sync_single_range_for_cpu
);
288 void 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
)
291 BUG_ON(direction
== DMA_NONE
);
293 plat_extra_sync_for_device(dev
);
294 if (!plat_device_is_coherent(dev
)) {
297 addr
= dma_addr_to_virt(dev
, dma_handle
);
298 __dma_sync(addr
+ offset
, size
, direction
);
302 EXPORT_SYMBOL(dma_sync_single_range_for_device
);
304 void dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
, int nelems
,
305 enum dma_data_direction direction
)
309 BUG_ON(direction
== DMA_NONE
);
311 /* Make sure that gcc doesn't leave the empty loop body. */
312 for (i
= 0; i
< nelems
; i
++, sg
++) {
313 if (cpu_is_noncoherent_r10000(dev
))
314 __dma_sync((unsigned long)page_address(sg_page(sg
)),
315 sg
->length
, direction
);
319 EXPORT_SYMBOL(dma_sync_sg_for_cpu
);
321 void dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
, int nelems
,
322 enum dma_data_direction direction
)
326 BUG_ON(direction
== DMA_NONE
);
328 /* Make sure that gcc doesn't leave the empty loop body. */
329 for (i
= 0; i
< nelems
; i
++, sg
++) {
330 if (!plat_device_is_coherent(dev
))
331 __dma_sync((unsigned long)page_address(sg_page(sg
)),
332 sg
->length
, direction
);
336 EXPORT_SYMBOL(dma_sync_sg_for_device
);
338 int dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
340 return plat_dma_mapping_error(dev
, dma_addr
);
343 EXPORT_SYMBOL(dma_mapping_error
);
345 int dma_supported(struct device
*dev
, u64 mask
)
347 return plat_dma_supported(dev
, mask
);
350 EXPORT_SYMBOL(dma_supported
);
352 int dma_is_consistent(struct device
*dev
, dma_addr_t dma_addr
)
354 return plat_device_is_coherent(dev
);
357 EXPORT_SYMBOL(dma_is_consistent
);
359 void dma_cache_sync(struct device
*dev
, void *vaddr
, size_t size
,
360 enum dma_data_direction direction
)
362 BUG_ON(direction
== DMA_NONE
);
364 plat_extra_sync_for_device(dev
);
365 if (!plat_device_is_coherent(dev
))
366 __dma_sync((unsigned long)vaddr
, size
, direction
);
369 EXPORT_SYMBOL(dma_cache_sync
);