aboutsummaryrefslogtreecommitdiffstats
path: root/arch/i386/kernel/pci-dma.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/i386/kernel/pci-dma.c')
-rw-r--r--arch/i386/kernel/pci-dma.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/arch/i386/kernel/pci-dma.c b/arch/i386/kernel/pci-dma.c
new file mode 100644
index 000000000000..4de2e03c7b45
--- /dev/null
+++ b/arch/i386/kernel/pci-dma.c
@@ -0,0 +1,147 @@
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 <asm/io.h>
15
16struct dma_coherent_mem {
17 void *virt_base;
18 u32 device_base;
19 int size;
20 int flags;
21 unsigned long *bitmap;
22};
23
24void *dma_alloc_coherent(struct device *dev, size_t size,
25 dma_addr_t *dma_handle, unsigned int __nocast gfp)
26{
27 void *ret;
28 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
29 int order = get_order(size);
30 /* ignore region specifiers */
31 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
32
33 if (mem) {
34 int page = bitmap_find_free_region(mem->bitmap, mem->size,
35 order);
36 if (page >= 0) {
37 *dma_handle = mem->device_base + (page << PAGE_SHIFT);
38 ret = mem->virt_base + (page << PAGE_SHIFT);
39 memset(ret, 0, size);
40 return ret;
41 }
42 if (mem->flags & DMA_MEMORY_EXCLUSIVE)
43 return NULL;
44 }
45
46 if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
47 gfp |= GFP_DMA;
48
49 ret = (void *)__get_free_pages(gfp, order);
50
51 if (ret != NULL) {
52 memset(ret, 0, size);
53 *dma_handle = virt_to_phys(ret);
54 }
55 return ret;
56}
57
58void dma_free_coherent(struct device *dev, size_t size,
59 void *vaddr, dma_addr_t dma_handle)
60{
61 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
62 int order = get_order(size);
63
64 if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
65 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
66
67 bitmap_release_region(mem->bitmap, page, order);
68 } else
69 free_pages((unsigned long)vaddr, order);
70}
71
72int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
73 dma_addr_t device_addr, size_t size, int flags)
74{
75 void __iomem *mem_base;
76 int pages = size >> PAGE_SHIFT;
77 int bitmap_size = (pages + 31)/32;
78
79 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
80 goto out;
81 if (!size)
82 goto out;
83 if (dev->dma_mem)
84 goto out;
85
86 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
87
88 mem_base = ioremap(bus_addr, size);
89 if (!mem_base)
90 goto out;
91
92 dev->dma_mem = kmalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
93 if (!dev->dma_mem)
94 goto out;
95 memset(dev->dma_mem, 0, sizeof(struct dma_coherent_mem));
96 dev->dma_mem->bitmap = kmalloc(bitmap_size, GFP_KERNEL);
97 if (!dev->dma_mem->bitmap)
98 goto free1_out;
99 memset(dev->dma_mem->bitmap, 0, bitmap_size);
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->bitmap);
113 out:
114 return 0;
115}
116EXPORT_SYMBOL(dma_declare_coherent_memory);
117
118void 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}
129EXPORT_SYMBOL(dma_release_declared_memory);
130
131void *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}
147EXPORT_SYMBOL(dma_mark_declared_memory_occupied);