]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/pci-dma_32.c
x86: merge iommu initialization parameters
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / pci-dma_32.c
1 /*
2  * Dynamic DMA mapping support.
3  *
4  * On i386 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
10 #include <linux/types.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/pci.h>
14 #include <linux/module.h>
15 #include <asm/io.h>
16
17 /* For i386, we make it point to the NULL address */
18 dma_addr_t bad_dma_address __read_mostly = 0x0;
19 EXPORT_SYMBOL(bad_dma_address);
20
21 struct dma_coherent_mem {
22         void            *virt_base;
23         u32             device_base;
24         int             size;
25         int             flags;
26         unsigned long   *bitmap;
27 };
28
29 void *dma_alloc_coherent(struct device *dev, size_t size,
30                            dma_addr_t *dma_handle, gfp_t gfp)
31 {
32         void *ret;
33         struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
34         int order = get_order(size);
35         /* ignore region specifiers */
36         gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
37
38         if (mem) {
39                 int page = bitmap_find_free_region(mem->bitmap, mem->size,
40                                                      order);
41                 if (page >= 0) {
42                         *dma_handle = mem->device_base + (page << PAGE_SHIFT);
43                         ret = mem->virt_base + (page << PAGE_SHIFT);
44                         memset(ret, 0, size);
45                         return ret;
46                 }
47                 if (mem->flags & DMA_MEMORY_EXCLUSIVE)
48                         return NULL;
49         }
50
51         if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
52                 gfp |= GFP_DMA;
53
54         ret = (void *)__get_free_pages(gfp, order);
55
56         if (ret != NULL) {
57                 memset(ret, 0, size);
58                 *dma_handle = virt_to_phys(ret);
59         }
60         return ret;
61 }
62 EXPORT_SYMBOL(dma_alloc_coherent);
63
64 void dma_free_coherent(struct device *dev, size_t size,
65                          void *vaddr, dma_addr_t dma_handle)
66 {
67         struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
68         int order = get_order(size);
69
70         WARN_ON(irqs_disabled());       /* for portability */
71         if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
72                 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
73
74                 bitmap_release_region(mem->bitmap, page, order);
75         } else
76                 free_pages((unsigned long)vaddr, order);
77 }
78 EXPORT_SYMBOL(dma_free_coherent);
79
80 int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
81                                 dma_addr_t device_addr, size_t size, int flags)
82 {
83         void __iomem *mem_base = NULL;
84         int pages = size >> PAGE_SHIFT;
85         int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
86
87         if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
88                 goto out;
89         if (!size)
90                 goto out;
91         if (dev->dma_mem)
92                 goto out;
93
94         /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
95
96         mem_base = ioremap(bus_addr, size);
97         if (!mem_base)
98                 goto out;
99
100         dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
101         if (!dev->dma_mem)
102                 goto out;
103         dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
104         if (!dev->dma_mem->bitmap)
105                 goto free1_out;
106
107         dev->dma_mem->virt_base = mem_base;
108         dev->dma_mem->device_base = device_addr;
109         dev->dma_mem->size = pages;
110         dev->dma_mem->flags = flags;
111
112         if (flags & DMA_MEMORY_MAP)
113                 return DMA_MEMORY_MAP;
114
115         return DMA_MEMORY_IO;
116
117  free1_out:
118         kfree(dev->dma_mem);
119  out:
120         if (mem_base)
121                 iounmap(mem_base);
122         return 0;
123 }
124 EXPORT_SYMBOL(dma_declare_coherent_memory);
125
126 void dma_release_declared_memory(struct device *dev)
127 {
128         struct dma_coherent_mem *mem = dev->dma_mem;
129         
130         if(!mem)
131                 return;
132         dev->dma_mem = NULL;
133         iounmap(mem->virt_base);
134         kfree(mem->bitmap);
135         kfree(mem);
136 }
137 EXPORT_SYMBOL(dma_release_declared_memory);
138
139 void *dma_mark_declared_memory_occupied(struct device *dev,
140                                         dma_addr_t device_addr, size_t size)
141 {
142         struct dma_coherent_mem *mem = dev->dma_mem;
143         int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
144         int pos, err;
145
146         if (!mem)
147                 return ERR_PTR(-EINVAL);
148
149         pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
150         err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
151         if (err != 0)
152                 return ERR_PTR(err);
153         return mem->virt_base + (pos << PAGE_SHIFT);
154 }
155 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);