Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / avr32 / mm / dma-coherent.c
blob50cdb5b10f0fc7b8bdfae25ffb9b32d5655463de
1 /*
2 * Copyright (C) 2004-2006 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/dma-mapping.h>
10 #include <linux/gfp.h>
11 #include <linux/export.h>
13 #include <asm/addrspace.h>
14 #include <asm/cacheflush.h>
16 void dma_cache_sync(struct device *dev, void *vaddr, size_t size, int direction)
19 * No need to sync an uncached area
21 if (PXSEG(vaddr) == P2SEG)
22 return;
24 switch (direction) {
25 case DMA_FROM_DEVICE: /* invalidate only */
26 invalidate_dcache_region(vaddr, size);
27 break;
28 case DMA_TO_DEVICE: /* writeback only */
29 clean_dcache_region(vaddr, size);
30 break;
31 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
32 flush_dcache_region(vaddr, size);
33 break;
34 default:
35 BUG();
38 EXPORT_SYMBOL(dma_cache_sync);
40 static struct page *__dma_alloc(struct device *dev, size_t size,
41 dma_addr_t *handle, gfp_t gfp)
43 struct page *page, *free, *end;
44 int order;
46 /* Following is a work-around (a.k.a. hack) to prevent pages
47 * with __GFP_COMP being passed to split_page() which cannot
48 * handle them. The real problem is that this flag probably
49 * should be 0 on AVR32 as it is not supported on this
50 * platform--see CONFIG_HUGETLB_PAGE. */
51 gfp &= ~(__GFP_COMP);
53 size = PAGE_ALIGN(size);
54 order = get_order(size);
56 page = alloc_pages(gfp, order);
57 if (!page)
58 return NULL;
59 split_page(page, order);
62 * When accessing physical memory with valid cache data, we
63 * get a cache hit even if the virtual memory region is marked
64 * as uncached.
66 * Since the memory is newly allocated, there is no point in
67 * doing a writeback. If the previous owner cares, he should
68 * have flushed the cache before releasing the memory.
70 invalidate_dcache_region(phys_to_virt(page_to_phys(page)), size);
72 *handle = page_to_bus(page);
73 free = page + (size >> PAGE_SHIFT);
74 end = page + (1 << order);
77 * Free any unused pages
79 while (free < end) {
80 __free_page(free);
81 free++;
84 return page;
87 static void __dma_free(struct device *dev, size_t size,
88 struct page *page, dma_addr_t handle)
90 struct page *end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
92 while (page < end)
93 __free_page(page++);
96 void *dma_alloc_coherent(struct device *dev, size_t size,
97 dma_addr_t *handle, gfp_t gfp)
99 struct page *page;
100 void *ret = NULL;
102 page = __dma_alloc(dev, size, handle, gfp);
103 if (page)
104 ret = phys_to_uncached(page_to_phys(page));
106 return ret;
108 EXPORT_SYMBOL(dma_alloc_coherent);
110 void dma_free_coherent(struct device *dev, size_t size,
111 void *cpu_addr, dma_addr_t handle)
113 void *addr = phys_to_cached(uncached_to_phys(cpu_addr));
114 struct page *page;
116 pr_debug("dma_free_coherent addr %p (phys %08lx) size %u\n",
117 cpu_addr, (unsigned long)handle, (unsigned)size);
118 BUG_ON(!virt_addr_valid(addr));
119 page = virt_to_page(addr);
120 __dma_free(dev, size, page, handle);
122 EXPORT_SYMBOL(dma_free_coherent);
124 void *dma_alloc_writecombine(struct device *dev, size_t size,
125 dma_addr_t *handle, gfp_t gfp)
127 struct page *page;
128 dma_addr_t phys;
130 page = __dma_alloc(dev, size, handle, gfp);
131 if (!page)
132 return NULL;
134 phys = page_to_phys(page);
135 *handle = phys;
137 /* Now, map the page into P3 with write-combining turned on */
138 return __ioremap(phys, size, _PAGE_BUFFER);
140 EXPORT_SYMBOL(dma_alloc_writecombine);
142 void dma_free_writecombine(struct device *dev, size_t size,
143 void *cpu_addr, dma_addr_t handle)
145 struct page *page;
147 iounmap(cpu_addr);
149 page = phys_to_page(handle);
150 __dma_free(dev, size, page, handle);
152 EXPORT_SYMBOL(dma_free_writecombine);