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/platform_device.h>
14 #include <linux/dma-mapping.h>
15 #include <asm/cacheflush.h>
16 #include <asm/addrspace.h>
19 struct dma_coherent_mem {
24 unsigned long *bitmap;
27 void *dma_alloc_coherent(struct device *dev, size_t size,
28 dma_addr_t *dma_handle, gfp_t gfp)
30 void *ret, *ret_nocache;
31 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
32 int order = get_order(size);
35 int page = bitmap_find_free_region(mem->bitmap, mem->size,
38 *dma_handle = mem->device_base + (page << PAGE_SHIFT);
39 ret = mem->virt_base + (page << PAGE_SHIFT);
43 if (mem->flags & DMA_MEMORY_EXCLUSIVE)
47 ret = (void *)__get_free_pages(gfp, order);
53 * Pages from the page allocator may have data present in
54 * cache. So flush the cache before using uncached memory.
56 dma_cache_sync(dev, ret, size, DMA_BIDIRECTIONAL);
58 ret_nocache = ioremap_nocache(virt_to_phys(ret), size);
60 free_pages((unsigned long)ret, order);
64 *dma_handle = virt_to_phys(ret);
67 EXPORT_SYMBOL(dma_alloc_coherent);
69 void dma_free_coherent(struct device *dev, size_t size,
70 void *vaddr, dma_addr_t dma_handle)
72 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
73 int order = get_order(size);
75 if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
76 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
78 bitmap_release_region(mem->bitmap, page, order);
80 WARN_ON(irqs_disabled()); /* for portability */
81 BUG_ON(mem && mem->flags & DMA_MEMORY_EXCLUSIVE);
82 free_pages((unsigned long)phys_to_virt(dma_handle), order);
86 EXPORT_SYMBOL(dma_free_coherent);
88 int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
89 dma_addr_t device_addr, size_t size, int flags)
91 void __iomem *mem_base = NULL;
92 int pages = size >> PAGE_SHIFT;
93 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
95 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
102 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
104 mem_base = ioremap_nocache(bus_addr, size);
108 dev->dma_mem = kmalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
111 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
112 if (!dev->dma_mem->bitmap)
115 dev->dma_mem->virt_base = mem_base;
116 dev->dma_mem->device_base = device_addr;
117 dev->dma_mem->size = pages;
118 dev->dma_mem->flags = flags;
120 if (flags & DMA_MEMORY_MAP)
121 return DMA_MEMORY_MAP;
123 return DMA_MEMORY_IO;
132 EXPORT_SYMBOL(dma_declare_coherent_memory);
134 void dma_release_declared_memory(struct device *dev)
136 struct dma_coherent_mem *mem = dev->dma_mem;
141 iounmap(mem->virt_base);
145 EXPORT_SYMBOL(dma_release_declared_memory);
147 void *dma_mark_declared_memory_occupied(struct device *dev,
148 dma_addr_t device_addr, size_t size)
150 struct dma_coherent_mem *mem = dev->dma_mem;
151 int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
155 return ERR_PTR(-EINVAL);
157 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
158 err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
161 return mem->virt_base + (pos << PAGE_SHIFT);
163 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
165 void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
166 enum dma_data_direction direction)
168 #ifdef CONFIG_CPU_SH5
169 void *p1addr = vaddr;
171 void *p1addr = (void*) P1SEGADDR((unsigned long)vaddr);
175 case DMA_FROM_DEVICE: /* invalidate only */
176 __flush_invalidate_region(p1addr, size);
178 case DMA_TO_DEVICE: /* writeback only */
179 __flush_wback_region(p1addr, size);
181 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
182 __flush_purge_region(p1addr, size);
188 EXPORT_SYMBOL(dma_cache_sync);
190 int platform_resource_setup_memory(struct platform_device *pdev,
191 char *name, unsigned long memsize)
194 dma_addr_t dma_handle;
197 r = pdev->resource + pdev->num_resources - 1;
199 pr_warning("%s: unable to find empty space for resource\n",
204 buf = dma_alloc_coherent(NULL, memsize, &dma_handle, GFP_KERNEL);
206 pr_warning("%s: unable to allocate memory\n", name);
210 memset(buf, 0, memsize);
212 r->flags = IORESOURCE_MEM;
213 r->start = dma_handle;
214 r->end = r->start + memsize - 1;