This source file includes following definitions.
- pci_epc_mem_get_order
- __pci_epc_mem_init
- pci_epc_mem_exit
- pci_epc_mem_alloc_addr
- pci_epc_mem_free_addr
1
2
3
4
5
6
7
8
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12
13 #include <linux/pci-epc.h>
14
15
16
17
18
19
20
21
22
23 static int pci_epc_mem_get_order(struct pci_epc_mem *mem, size_t size)
24 {
25 int order;
26 unsigned int page_shift = ilog2(mem->page_size);
27
28 size--;
29 size >>= page_shift;
30 #if BITS_PER_LONG == 32
31 order = fls(size);
32 #else
33 order = fls64(size);
34 #endif
35 return order;
36 }
37
38
39
40
41
42
43
44
45
46
47
48 int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_base, size_t size,
49 size_t page_size)
50 {
51 int ret;
52 struct pci_epc_mem *mem;
53 unsigned long *bitmap;
54 unsigned int page_shift;
55 int pages;
56 int bitmap_size;
57
58 if (page_size < PAGE_SIZE)
59 page_size = PAGE_SIZE;
60
61 page_shift = ilog2(page_size);
62 pages = size >> page_shift;
63 bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
64
65 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
66 if (!mem) {
67 ret = -ENOMEM;
68 goto err;
69 }
70
71 bitmap = kzalloc(bitmap_size, GFP_KERNEL);
72 if (!bitmap) {
73 ret = -ENOMEM;
74 goto err_mem;
75 }
76
77 mem->bitmap = bitmap;
78 mem->phys_base = phys_base;
79 mem->page_size = page_size;
80 mem->pages = pages;
81 mem->size = size;
82 mutex_init(&mem->lock);
83
84 epc->mem = mem;
85
86 return 0;
87
88 err_mem:
89 kfree(mem);
90
91 err:
92 return ret;
93 }
94 EXPORT_SYMBOL_GPL(__pci_epc_mem_init);
95
96
97
98
99
100
101
102
103 void pci_epc_mem_exit(struct pci_epc *epc)
104 {
105 struct pci_epc_mem *mem = epc->mem;
106
107 epc->mem = NULL;
108 kfree(mem->bitmap);
109 kfree(mem);
110 }
111 EXPORT_SYMBOL_GPL(pci_epc_mem_exit);
112
113
114
115
116
117
118
119
120
121
122 void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
123 phys_addr_t *phys_addr, size_t size)
124 {
125 int pageno;
126 void __iomem *virt_addr = NULL;
127 struct pci_epc_mem *mem = epc->mem;
128 unsigned int page_shift = ilog2(mem->page_size);
129 int order;
130
131 size = ALIGN(size, mem->page_size);
132 order = pci_epc_mem_get_order(mem, size);
133
134 mutex_lock(&mem->lock);
135 pageno = bitmap_find_free_region(mem->bitmap, mem->pages, order);
136 if (pageno < 0)
137 goto ret;
138
139 *phys_addr = mem->phys_base + (pageno << page_shift);
140 virt_addr = ioremap(*phys_addr, size);
141 if (!virt_addr)
142 bitmap_release_region(mem->bitmap, pageno, order);
143
144 ret:
145 mutex_unlock(&mem->lock);
146 return virt_addr;
147 }
148 EXPORT_SYMBOL_GPL(pci_epc_mem_alloc_addr);
149
150
151
152
153
154
155
156
157
158
159 void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
160 void __iomem *virt_addr, size_t size)
161 {
162 int pageno;
163 struct pci_epc_mem *mem = epc->mem;
164 unsigned int page_shift = ilog2(mem->page_size);
165 int order;
166
167 iounmap(virt_addr);
168 pageno = (phys_addr - mem->phys_base) >> page_shift;
169 size = ALIGN(size, mem->page_size);
170 order = pci_epc_mem_get_order(mem, size);
171 mutex_lock(&mem->lock);
172 bitmap_release_region(mem->bitmap, pageno, order);
173 mutex_unlock(&mem->lock);
174 }
175 EXPORT_SYMBOL_GPL(pci_epc_mem_free_addr);
176
177 MODULE_DESCRIPTION("PCI EPC Address Space Management");
178 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
179 MODULE_LICENSE("GPL v2");