Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / blackfin / kernel / dma-mapping.c
blobf0d1118f1825bf9c3ce5f280f45fabcdbd3e0391
1 /*
2 * Dynamic DMA mapping support
4 * Copyright 2005-2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later
7 */
9 #include <linux/types.h>
10 #include <linux/gfp.h>
11 #include <linux/string.h>
12 #include <linux/spinlock.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/scatterlist.h>
15 #include <linux/export.h>
17 static spinlock_t dma_page_lock;
18 static unsigned long *dma_page;
19 static unsigned int dma_pages;
20 static unsigned long dma_base;
21 static unsigned long dma_size;
22 static unsigned int dma_initialized;
24 static void dma_alloc_init(unsigned long start, unsigned long end)
26 spin_lock_init(&dma_page_lock);
27 dma_initialized = 0;
29 dma_page = (unsigned long *)__get_free_page(GFP_KERNEL);
30 memset(dma_page, 0, PAGE_SIZE);
31 dma_base = PAGE_ALIGN(start);
32 dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start);
33 dma_pages = dma_size >> PAGE_SHIFT;
34 memset((void *)dma_base, 0, DMA_UNCACHED_REGION);
35 dma_initialized = 1;
37 printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__,
38 dma_page, dma_pages, dma_base);
41 static inline unsigned int get_pages(size_t size)
43 return ((size - 1) >> PAGE_SHIFT) + 1;
46 static unsigned long __alloc_dma_pages(unsigned int pages)
48 unsigned long ret = 0, flags;
49 int i, count = 0;
51 if (dma_initialized == 0)
52 dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend);
54 spin_lock_irqsave(&dma_page_lock, flags);
56 for (i = 0; i < dma_pages;) {
57 if (test_bit(i++, dma_page) == 0) {
58 if (++count == pages) {
59 while (count--)
60 __set_bit(--i, dma_page);
62 ret = dma_base + (i << PAGE_SHIFT);
63 break;
65 } else
66 count = 0;
68 spin_unlock_irqrestore(&dma_page_lock, flags);
69 return ret;
72 static void __free_dma_pages(unsigned long addr, unsigned int pages)
74 unsigned long page = (addr - dma_base) >> PAGE_SHIFT;
75 unsigned long flags;
76 int i;
78 if ((page + pages) > dma_pages) {
79 printk(KERN_ERR "%s: freeing outside range.\n", __func__);
80 BUG();
83 spin_lock_irqsave(&dma_page_lock, flags);
84 for (i = page; i < page + pages; i++)
85 __clear_bit(i, dma_page);
87 spin_unlock_irqrestore(&dma_page_lock, flags);
90 void *dma_alloc_coherent(struct device *dev, size_t size,
91 dma_addr_t *dma_handle, gfp_t gfp)
93 void *ret;
95 ret = (void *)__alloc_dma_pages(get_pages(size));
97 if (ret) {
98 memset(ret, 0, size);
99 *dma_handle = virt_to_phys(ret);
102 return ret;
104 EXPORT_SYMBOL(dma_alloc_coherent);
106 void
107 dma_free_coherent(struct device *dev, size_t size, void *vaddr,
108 dma_addr_t dma_handle)
110 __free_dma_pages((unsigned long)vaddr, get_pages(size));
112 EXPORT_SYMBOL(dma_free_coherent);
115 * Streaming DMA mappings
117 void __dma_sync(dma_addr_t addr, size_t size,
118 enum dma_data_direction dir)
120 __dma_sync_inline(addr, size, dir);
122 EXPORT_SYMBOL(__dma_sync);
125 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
126 enum dma_data_direction direction)
128 int i;
130 for (i = 0; i < nents; i++, sg++) {
131 sg->dma_address = (dma_addr_t) sg_virt(sg);
132 __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
135 return nents;
137 EXPORT_SYMBOL(dma_map_sg);
139 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
140 int nelems, enum dma_data_direction direction)
142 int i;
144 for (i = 0; i < nelems; i++, sg++) {
145 sg->dma_address = (dma_addr_t) sg_virt(sg);
146 __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
149 EXPORT_SYMBOL(dma_sync_sg_for_device);