]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/i386/kernel/pci-dma.c
[PATCH] i386: Page-align the GDT
[mirror_ubuntu-artful-kernel.git] / arch / i386 / kernel / pci-dma.c
CommitLineData
1da177e4
LT
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>
129f6946 14#include <linux/module.h>
1da177e4
LT
15#include <asm/io.h>
16
17struct dma_coherent_mem {
18 void *virt_base;
19 u32 device_base;
20 int size;
21 int flags;
22 unsigned long *bitmap;
23};
24
25void *dma_alloc_coherent(struct device *dev, size_t size,
dd0fc66f 26 dma_addr_t *dma_handle, gfp_t gfp)
1da177e4
LT
27{
28 void *ret;
29 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
30 int order = get_order(size);
31 /* ignore region specifiers */
32 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
33
34 if (mem) {
35 int page = bitmap_find_free_region(mem->bitmap, mem->size,
36 order);
37 if (page >= 0) {
38 *dma_handle = mem->device_base + (page << PAGE_SHIFT);
39 ret = mem->virt_base + (page << PAGE_SHIFT);
40 memset(ret, 0, size);
41 return ret;
42 }
43 if (mem->flags & DMA_MEMORY_EXCLUSIVE)
44 return NULL;
45 }
46
47 if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
48 gfp |= GFP_DMA;
49
50 ret = (void *)__get_free_pages(gfp, order);
51
52 if (ret != NULL) {
53 memset(ret, 0, size);
54 *dma_handle = virt_to_phys(ret);
55 }
56 return ret;
57}
129f6946 58EXPORT_SYMBOL(dma_alloc_coherent);
1da177e4
LT
59
60void 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}
129f6946 73EXPORT_SYMBOL(dma_free_coherent);
1da177e4
LT
74
75int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
76 dma_addr_t device_addr, size_t size, int flags)
77{
039d09a8 78 void __iomem *mem_base = NULL;
1da177e4
LT
79 int pages = size >> PAGE_SHIFT;
80 int bitmap_size = (pages + 31)/32;
81
82 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
83 goto out;
84 if (!size)
85 goto out;
86 if (dev->dma_mem)
87 goto out;
88
89 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
90
91 mem_base = ioremap(bus_addr, size);
92 if (!mem_base)
93 goto out;
94
116780fc 95 dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
1da177e4
LT
96 if (!dev->dma_mem)
97 goto out;
116780fc 98 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1da177e4
LT
99 if (!dev->dma_mem->bitmap)
100 goto free1_out;
1da177e4
LT
101
102 dev->dma_mem->virt_base = mem_base;
103 dev->dma_mem->device_base = device_addr;
104 dev->dma_mem->size = pages;
105 dev->dma_mem->flags = flags;
106
107 if (flags & DMA_MEMORY_MAP)
108 return DMA_MEMORY_MAP;
109
110 return DMA_MEMORY_IO;
111
112 free1_out:
3a0ee2ce 113 kfree(dev->dma_mem);
1da177e4 114 out:
039d09a8
AL
115 if (mem_base)
116 iounmap(mem_base);
1da177e4
LT
117 return 0;
118}
119EXPORT_SYMBOL(dma_declare_coherent_memory);
120
121void dma_release_declared_memory(struct device *dev)
122{
123 struct dma_coherent_mem *mem = dev->dma_mem;
124
125 if(!mem)
126 return;
127 dev->dma_mem = NULL;
128 iounmap(mem->virt_base);
129 kfree(mem->bitmap);
130 kfree(mem);
131}
132EXPORT_SYMBOL(dma_release_declared_memory);
133
134void *dma_mark_declared_memory_occupied(struct device *dev,
135 dma_addr_t device_addr, size_t size)
136{
137 struct dma_coherent_mem *mem = dev->dma_mem;
138 int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
139 int pos, err;
140
141 if (!mem)
142 return ERR_PTR(-EINVAL);
143
144 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
145 err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
146 if (err != 0)
147 return ERR_PTR(err);
148 return mem->virt_base + (pos << PAGE_SHIFT);
149}
150EXPORT_SYMBOL(dma_mark_declared_memory_occupied);