2 * arch/sh/mm/consistent.c
4 * Copyright (C) 2004 - 2007 Paul Mundt
6 * Declared coherent memory functions based on arch/x86/kernel/pci-dma_32.c
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
13 #include <linux/dma-mapping.h>
14 #include <asm/cacheflush.h>
15 #include <asm/addrspace.h>
18 struct dma_coherent_mem {
23 unsigned long *bitmap;
26 void *dma_alloc_coherent(struct device *dev, size_t size,
27 dma_addr_t *dma_handle, gfp_t gfp)
29 void *ret, *ret_nocache;
30 int order = get_order(size);
32 if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
35 ret = (void *)__get_free_pages(gfp, order);
41 * Pages from the page allocator may have data present in
42 * cache. So flush the cache before using uncached memory.
44 dma_cache_sync(dev, ret, size, DMA_BIDIRECTIONAL);
46 ret_nocache = ioremap_nocache(virt_to_phys(ret), size);
48 free_pages((unsigned long)ret, order);
52 *dma_handle = virt_to_phys(ret);
55 EXPORT_SYMBOL(dma_alloc_coherent);
57 void dma_free_coherent(struct device *dev, size_t size,
58 void *vaddr, dma_addr_t dma_handle)
60 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
61 int order = get_order(size);
63 if (!dma_release_from_coherent(dev, order, vaddr)) {
64 WARN_ON(irqs_disabled()); /* for portability */
65 BUG_ON(mem && mem->flags & DMA_MEMORY_EXCLUSIVE);
66 free_pages((unsigned long)phys_to_virt(dma_handle), order);
70 EXPORT_SYMBOL(dma_free_coherent);
72 void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
73 enum dma_data_direction direction)
78 void *p1addr = (void*) P1SEGADDR((unsigned long)vaddr);
82 case DMA_FROM_DEVICE: /* invalidate only */
83 __flush_invalidate_region(p1addr, size);
85 case DMA_TO_DEVICE: /* writeback only */
86 __flush_wback_region(p1addr, size);
88 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
89 __flush_purge_region(p1addr, size);
95 EXPORT_SYMBOL(dma_cache_sync);