some kmalloc/memset ->kzalloc (tree wide)
[linux-2.6.git] / arch / cris / arch-v32 / drivers / pci / dma.c
1 /*
2  * Dynamic DMA mapping support.
3  *
4  * On cris there is no hardware dynamic DMA address translation,
5  * so consistent alloc/free are merely page allocation/freeing.
6  * The rest of the dynamic DMA mapping interface is implemented
7  * in asm/pci.h.
8  *
9  * Borrowed from i386.
10  */
11
12 #include <linux/types.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
15 #include <linux/pci.h>
16 #include <asm/io.h>
17
18 struct dma_coherent_mem {
19         void            *virt_base;
20         u32             device_base;
21         int             size;
22         int             flags;
23         unsigned long   *bitmap;
24 };
25
26 void *dma_alloc_coherent(struct device *dev, size_t size,
27                            dma_addr_t *dma_handle, gfp_t gfp)
28 {
29         void *ret;
30         struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
31         int order = get_order(size);
32         /* ignore region specifiers */
33         gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
34
35         if (mem) {
36                 int page = bitmap_find_free_region(mem->bitmap, mem->size,
37                                                      order);
38                 if (page >= 0) {
39                         *dma_handle = mem->device_base + (page << PAGE_SHIFT);
40                         ret = mem->virt_base + (page << PAGE_SHIFT);
41                         memset(ret, 0, size);
42                         return ret;
43                 }
44                 if (mem->flags & DMA_MEMORY_EXCLUSIVE)
45                         return NULL;
46         }
47
48         if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
49                 gfp |= GFP_DMA;
50
51         ret = (void *)__get_free_pages(gfp, order);
52
53         if (ret != NULL) {
54                 memset(ret, 0, size);
55                 *dma_handle = virt_to_phys(ret);
56         }
57         return ret;
58 }
59
60 void dma_free_coherent(struct device *dev, size_t size,
61                          void *vaddr, dma_addr_t dma_handle)
62 {
63         struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
64         int order = get_order(size);
65
66         if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
67                 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
68
69                 bitmap_release_region(mem->bitmap, page, order);
70         } else
71                 free_pages((unsigned long)vaddr, order);
72 }
73
74 int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
75                                 dma_addr_t device_addr, size_t size, int flags)
76 {
77         void __iomem *mem_base;
78         int pages = size >> PAGE_SHIFT;
79         int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
80
81         if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
82                 goto out;
83         if (!size)
84                 goto out;
85         if (dev->dma_mem)
86                 goto out;
87
88         /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
89
90         mem_base = ioremap(bus_addr, size);
91         if (!mem_base)
92                 goto out;
93
94         dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
95         if (!dev->dma_mem)
96                 goto out;
97         dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
98         if (!dev->dma_mem->bitmap)
99                 goto free1_out;
100
101         dev->dma_mem->virt_base = mem_base;
102         dev->dma_mem->device_base = device_addr;
103         dev->dma_mem->size = pages;
104         dev->dma_mem->flags = flags;
105
106         if (flags & DMA_MEMORY_MAP)
107                 return DMA_MEMORY_MAP;
108
109         return DMA_MEMORY_IO;
110
111  free1_out:
112         kfree(dev->dma_mem);
113  out:
114         return 0;
115 }
116 EXPORT_SYMBOL(dma_declare_coherent_memory);
117
118 void dma_release_declared_memory(struct device *dev)
119 {
120         struct dma_coherent_mem *mem = dev->dma_mem;
121
122         if(!mem)
123                 return;
124         dev->dma_mem = NULL;
125         iounmap(mem->virt_base);
126         kfree(mem->bitmap);
127         kfree(mem);
128 }
129 EXPORT_SYMBOL(dma_release_declared_memory);
130
131 void *dma_mark_declared_memory_occupied(struct device *dev,
132                                         dma_addr_t device_addr, size_t size)
133 {
134         struct dma_coherent_mem *mem = dev->dma_mem;
135         int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
136         int pos, err;
137
138         if (!mem)
139                 return ERR_PTR(-EINVAL);
140
141         pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
142         err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
143         if (err != 0)
144                 return ERR_PTR(err);
145         return mem->virt_base + (pos << PAGE_SHIFT);
146 }
147 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);