]>
Commit | Line | Data |
---|---|---|
5ea81769 AV |
1 | #include <linux/pci.h> |
2 | #include <linux/io.h> | |
5a0e3ad6 | 3 | #include <linux/gfp.h> |
5ea81769 AV |
4 | #include <linux/module.h> |
5 | ||
b41e5fff | 6 | void devm_ioremap_release(struct device *dev, void *res) |
5ea81769 AV |
7 | { |
8 | iounmap(*(void __iomem **)res); | |
9 | } | |
10 | ||
11 | static int devm_ioremap_match(struct device *dev, void *res, void *match_data) | |
12 | { | |
13 | return *(void **)res == match_data; | |
14 | } | |
15 | ||
16 | /** | |
17 | * devm_ioremap - Managed ioremap() | |
18 | * @dev: Generic device to remap IO address for | |
19 | * @offset: BUS offset to map | |
20 | * @size: Size of map | |
21 | * | |
22 | * Managed ioremap(). Map is automatically unmapped on driver detach. | |
23 | */ | |
4f452e8a | 24 | void __iomem *devm_ioremap(struct device *dev, resource_size_t offset, |
5ea81769 AV |
25 | unsigned long size) |
26 | { | |
27 | void __iomem **ptr, *addr; | |
28 | ||
29 | ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); | |
30 | if (!ptr) | |
31 | return NULL; | |
32 | ||
33 | addr = ioremap(offset, size); | |
34 | if (addr) { | |
35 | *ptr = addr; | |
36 | devres_add(dev, ptr); | |
37 | } else | |
38 | devres_free(ptr); | |
39 | ||
40 | return addr; | |
41 | } | |
42 | EXPORT_SYMBOL(devm_ioremap); | |
43 | ||
44 | /** | |
45 | * devm_ioremap_nocache - Managed ioremap_nocache() | |
46 | * @dev: Generic device to remap IO address for | |
47 | * @offset: BUS offset to map | |
48 | * @size: Size of map | |
49 | * | |
50 | * Managed ioremap_nocache(). Map is automatically unmapped on driver | |
51 | * detach. | |
52 | */ | |
4f452e8a | 53 | void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset, |
5ea81769 AV |
54 | unsigned long size) |
55 | { | |
56 | void __iomem **ptr, *addr; | |
57 | ||
58 | ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); | |
59 | if (!ptr) | |
60 | return NULL; | |
61 | ||
62 | addr = ioremap_nocache(offset, size); | |
63 | if (addr) { | |
64 | *ptr = addr; | |
65 | devres_add(dev, ptr); | |
66 | } else | |
67 | devres_free(ptr); | |
68 | ||
69 | return addr; | |
70 | } | |
71 | EXPORT_SYMBOL(devm_ioremap_nocache); | |
72 | ||
73 | /** | |
74 | * devm_iounmap - Managed iounmap() | |
75 | * @dev: Generic device to unmap for | |
76 | * @addr: Address to unmap | |
77 | * | |
78 | * Managed iounmap(). @addr must have been mapped using devm_ioremap*(). | |
79 | */ | |
80 | void devm_iounmap(struct device *dev, void __iomem *addr) | |
81 | { | |
82 | iounmap(addr); | |
83 | WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match, | |
84 | (void *)addr)); | |
85 | } | |
86 | EXPORT_SYMBOL(devm_iounmap); | |
87 | ||
88 | #ifdef CONFIG_HAS_IOPORT | |
89 | /* | |
90 | * Generic iomap devres | |
91 | */ | |
92 | static void devm_ioport_map_release(struct device *dev, void *res) | |
93 | { | |
94 | ioport_unmap(*(void __iomem **)res); | |
95 | } | |
96 | ||
97 | static int devm_ioport_map_match(struct device *dev, void *res, | |
98 | void *match_data) | |
99 | { | |
100 | return *(void **)res == match_data; | |
101 | } | |
102 | ||
103 | /** | |
104 | * devm_ioport_map - Managed ioport_map() | |
105 | * @dev: Generic device to map ioport for | |
106 | * @port: Port to map | |
107 | * @nr: Number of ports to map | |
108 | * | |
109 | * Managed ioport_map(). Map is automatically unmapped on driver | |
110 | * detach. | |
111 | */ | |
112 | void __iomem * devm_ioport_map(struct device *dev, unsigned long port, | |
113 | unsigned int nr) | |
114 | { | |
115 | void __iomem **ptr, *addr; | |
116 | ||
117 | ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL); | |
118 | if (!ptr) | |
119 | return NULL; | |
120 | ||
121 | addr = ioport_map(port, nr); | |
122 | if (addr) { | |
123 | *ptr = addr; | |
124 | devres_add(dev, ptr); | |
125 | } else | |
126 | devres_free(ptr); | |
127 | ||
128 | return addr; | |
129 | } | |
130 | EXPORT_SYMBOL(devm_ioport_map); | |
131 | ||
132 | /** | |
133 | * devm_ioport_unmap - Managed ioport_unmap() | |
134 | * @dev: Generic device to unmap for | |
135 | * @addr: Address to unmap | |
136 | * | |
137 | * Managed ioport_unmap(). @addr must have been mapped using | |
138 | * devm_ioport_map(). | |
139 | */ | |
140 | void devm_ioport_unmap(struct device *dev, void __iomem *addr) | |
141 | { | |
142 | ioport_unmap(addr); | |
143 | WARN_ON(devres_destroy(dev, devm_ioport_map_release, | |
144 | devm_ioport_map_match, (void *)addr)); | |
145 | } | |
146 | EXPORT_SYMBOL(devm_ioport_unmap); | |
147 | ||
148 | #ifdef CONFIG_PCI | |
149 | /* | |
150 | * PCI iomap devres | |
151 | */ | |
152 | #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE | |
153 | ||
154 | struct pcim_iomap_devres { | |
155 | void __iomem *table[PCIM_IOMAP_MAX]; | |
156 | }; | |
157 | ||
158 | static void pcim_iomap_release(struct device *gendev, void *res) | |
159 | { | |
160 | struct pci_dev *dev = container_of(gendev, struct pci_dev, dev); | |
161 | struct pcim_iomap_devres *this = res; | |
162 | int i; | |
163 | ||
164 | for (i = 0; i < PCIM_IOMAP_MAX; i++) | |
165 | if (this->table[i]) | |
166 | pci_iounmap(dev, this->table[i]); | |
167 | } | |
168 | ||
169 | /** | |
170 | * pcim_iomap_table - access iomap allocation table | |
171 | * @pdev: PCI device to access iomap table for | |
172 | * | |
173 | * Access iomap allocation table for @dev. If iomap table doesn't | |
174 | * exist and @pdev is managed, it will be allocated. All iomaps | |
175 | * recorded in the iomap table are automatically unmapped on driver | |
176 | * detach. | |
177 | * | |
178 | * This function might sleep when the table is first allocated but can | |
179 | * be safely called without context and guaranteed to succed once | |
180 | * allocated. | |
181 | */ | |
182 | void __iomem * const * pcim_iomap_table(struct pci_dev *pdev) | |
183 | { | |
184 | struct pcim_iomap_devres *dr, *new_dr; | |
185 | ||
186 | dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL); | |
187 | if (dr) | |
188 | return dr->table; | |
189 | ||
190 | new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL); | |
191 | if (!new_dr) | |
192 | return NULL; | |
193 | dr = devres_get(&pdev->dev, new_dr, NULL, NULL); | |
194 | return dr->table; | |
195 | } | |
196 | EXPORT_SYMBOL(pcim_iomap_table); | |
197 | ||
198 | /** | |
199 | * pcim_iomap - Managed pcim_iomap() | |
200 | * @pdev: PCI device to iomap for | |
201 | * @bar: BAR to iomap | |
202 | * @maxlen: Maximum length of iomap | |
203 | * | |
204 | * Managed pci_iomap(). Map is automatically unmapped on driver | |
205 | * detach. | |
206 | */ | |
207 | void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen) | |
208 | { | |
209 | void __iomem **tbl; | |
210 | ||
211 | BUG_ON(bar >= PCIM_IOMAP_MAX); | |
212 | ||
213 | tbl = (void __iomem **)pcim_iomap_table(pdev); | |
214 | if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ | |
215 | return NULL; | |
216 | ||
217 | tbl[bar] = pci_iomap(pdev, bar, maxlen); | |
218 | return tbl[bar]; | |
219 | } | |
220 | EXPORT_SYMBOL(pcim_iomap); | |
221 | ||
222 | /** | |
223 | * pcim_iounmap - Managed pci_iounmap() | |
224 | * @pdev: PCI device to iounmap for | |
225 | * @addr: Address to unmap | |
226 | * | |
227 | * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap(). | |
228 | */ | |
229 | void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) | |
230 | { | |
231 | void __iomem **tbl; | |
232 | int i; | |
233 | ||
234 | pci_iounmap(pdev, addr); | |
235 | ||
236 | tbl = (void __iomem **)pcim_iomap_table(pdev); | |
237 | BUG_ON(!tbl); | |
238 | ||
239 | for (i = 0; i < PCIM_IOMAP_MAX; i++) | |
240 | if (tbl[i] == addr) { | |
241 | tbl[i] = NULL; | |
242 | return; | |
243 | } | |
244 | WARN_ON(1); | |
245 | } | |
246 | EXPORT_SYMBOL(pcim_iounmap); | |
247 | ||
248 | /** | |
249 | * pcim_iomap_regions - Request and iomap PCI BARs | |
250 | * @pdev: PCI device to map IO resources for | |
251 | * @mask: Mask of BARs to request and iomap | |
252 | * @name: Name used when requesting regions | |
253 | * | |
254 | * Request and iomap regions specified by @mask. | |
255 | */ | |
256 | int pcim_iomap_regions(struct pci_dev *pdev, u16 mask, const char *name) | |
257 | { | |
258 | void __iomem * const *iomap; | |
259 | int i, rc; | |
260 | ||
261 | iomap = pcim_iomap_table(pdev); | |
262 | if (!iomap) | |
263 | return -ENOMEM; | |
264 | ||
265 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { | |
266 | unsigned long len; | |
267 | ||
268 | if (!(mask & (1 << i))) | |
269 | continue; | |
270 | ||
271 | rc = -EINVAL; | |
272 | len = pci_resource_len(pdev, i); | |
273 | if (!len) | |
274 | goto err_inval; | |
275 | ||
276 | rc = pci_request_region(pdev, i, name); | |
277 | if (rc) | |
fb4d64e7 | 278 | goto err_inval; |
5ea81769 AV |
279 | |
280 | rc = -ENOMEM; | |
281 | if (!pcim_iomap(pdev, i, 0)) | |
fb4d64e7 | 282 | goto err_region; |
5ea81769 AV |
283 | } |
284 | ||
285 | return 0; | |
286 | ||
5ea81769 AV |
287 | err_region: |
288 | pci_release_region(pdev, i); | |
289 | err_inval: | |
290 | while (--i >= 0) { | |
fb4d64e7 FD |
291 | if (!(mask & (1 << i))) |
292 | continue; | |
5ea81769 AV |
293 | pcim_iounmap(pdev, iomap[i]); |
294 | pci_release_region(pdev, i); | |
295 | } | |
296 | ||
297 | return rc; | |
298 | } | |
299 | EXPORT_SYMBOL(pcim_iomap_regions); | |
ec04b075 | 300 | |
916fbfb7 TH |
301 | /** |
302 | * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones | |
303 | * @pdev: PCI device to map IO resources for | |
304 | * @mask: Mask of BARs to iomap | |
305 | * @name: Name used when requesting regions | |
306 | * | |
307 | * Request all PCI BARs and iomap regions specified by @mask. | |
308 | */ | |
309 | int pcim_iomap_regions_request_all(struct pci_dev *pdev, u16 mask, | |
310 | const char *name) | |
311 | { | |
312 | int request_mask = ((1 << 6) - 1) & ~mask; | |
313 | int rc; | |
314 | ||
315 | rc = pci_request_selected_regions(pdev, request_mask, name); | |
316 | if (rc) | |
317 | return rc; | |
318 | ||
319 | rc = pcim_iomap_regions(pdev, mask, name); | |
320 | if (rc) | |
321 | pci_release_selected_regions(pdev, request_mask); | |
322 | return rc; | |
323 | } | |
324 | EXPORT_SYMBOL(pcim_iomap_regions_request_all); | |
325 | ||
ec04b075 TH |
326 | /** |
327 | * pcim_iounmap_regions - Unmap and release PCI BARs | |
328 | * @pdev: PCI device to map IO resources for | |
329 | * @mask: Mask of BARs to unmap and release | |
330 | * | |
4d45ada3 | 331 | * Unmap and release regions specified by @mask. |
ec04b075 TH |
332 | */ |
333 | void pcim_iounmap_regions(struct pci_dev *pdev, u16 mask) | |
334 | { | |
335 | void __iomem * const *iomap; | |
336 | int i; | |
337 | ||
338 | iomap = pcim_iomap_table(pdev); | |
339 | if (!iomap) | |
340 | return; | |
341 | ||
342 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { | |
343 | if (!(mask & (1 << i))) | |
344 | continue; | |
345 | ||
346 | pcim_iounmap(pdev, iomap[i]); | |
347 | pci_release_region(pdev, i); | |
348 | } | |
349 | } | |
350 | EXPORT_SYMBOL(pcim_iounmap_regions); | |
5ea81769 AV |
351 | #endif |
352 | #endif |