]>
Commit | Line | Data |
---|---|---|
5ea81769 AV |
1 | #include <linux/pci.h> |
2 | #include <linux/io.h> | |
5a0e3ad6 | 3 | #include <linux/gfp.h> |
8bc3bcc9 | 4 | #include <linux/export.h> |
5ea81769 | 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 | { | |
5ea81769 AV |
82 | WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match, |
83 | (void *)addr)); | |
ae891a1b | 84 | iounmap(addr); |
5ea81769 AV |
85 | } |
86 | EXPORT_SYMBOL(devm_iounmap); | |
87 | ||
72f8c0bf | 88 | /** |
75096579 TR |
89 | * devm_ioremap_resource() - check, request region, and ioremap resource |
90 | * @dev: generic device to handle the resource for | |
72f8c0bf WS |
91 | * @res: resource to be handled |
92 | * | |
75096579 TR |
93 | * Checks that a resource is a valid memory region, requests the memory region |
94 | * and ioremaps it either as cacheable or as non-cacheable memory depending on | |
95 | * the resource's flags. All operations are managed and will be undone on | |
96 | * driver detach. | |
97 | * | |
98 | * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code | |
99 | * on failure. Usage example: | |
72f8c0bf WS |
100 | * |
101 | * res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
75096579 TR |
102 | * base = devm_ioremap_resource(&pdev->dev, res); |
103 | * if (IS_ERR(base)) | |
104 | * return PTR_ERR(base); | |
72f8c0bf | 105 | */ |
75096579 | 106 | void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res) |
72f8c0bf WS |
107 | { |
108 | resource_size_t size; | |
109 | const char *name; | |
110 | void __iomem *dest_ptr; | |
111 | ||
112 | BUG_ON(!dev); | |
113 | ||
114 | if (!res || resource_type(res) != IORESOURCE_MEM) { | |
115 | dev_err(dev, "invalid resource\n"); | |
75096579 | 116 | return ERR_PTR(-EINVAL); |
72f8c0bf WS |
117 | } |
118 | ||
119 | size = resource_size(res); | |
120 | name = res->name ?: dev_name(dev); | |
121 | ||
122 | if (!devm_request_mem_region(dev, res->start, size, name)) { | |
123 | dev_err(dev, "can't request region for resource %pR\n", res); | |
75096579 | 124 | return ERR_PTR(-EBUSY); |
72f8c0bf WS |
125 | } |
126 | ||
127 | if (res->flags & IORESOURCE_CACHEABLE) | |
128 | dest_ptr = devm_ioremap(dev, res->start, size); | |
129 | else | |
130 | dest_ptr = devm_ioremap_nocache(dev, res->start, size); | |
131 | ||
132 | if (!dest_ptr) { | |
133 | dev_err(dev, "ioremap failed for resource %pR\n", res); | |
134 | devm_release_mem_region(dev, res->start, size); | |
75096579 | 135 | dest_ptr = ERR_PTR(-ENOMEM); |
72f8c0bf WS |
136 | } |
137 | ||
138 | return dest_ptr; | |
139 | } | |
75096579 TR |
140 | EXPORT_SYMBOL(devm_ioremap_resource); |
141 | ||
142 | /** | |
143 | * devm_request_and_ioremap() - Check, request region, and ioremap resource | |
144 | * @dev: Generic device to handle the resource for | |
145 | * @res: resource to be handled | |
146 | * | |
147 | * Takes all necessary steps to ioremap a mem resource. Uses managed device, so | |
148 | * everything is undone on driver detach. Checks arguments, so you can feed | |
149 | * it the result from e.g. platform_get_resource() directly. Returns the | |
150 | * remapped pointer or NULL on error. Usage example: | |
151 | * | |
152 | * res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
153 | * base = devm_request_and_ioremap(&pdev->dev, res); | |
154 | * if (!base) | |
155 | * return -EADDRNOTAVAIL; | |
156 | */ | |
157 | void __iomem *devm_request_and_ioremap(struct device *device, | |
158 | struct resource *res) | |
159 | { | |
160 | void __iomem *dest_ptr; | |
161 | ||
162 | dest_ptr = devm_ioremap_resource(device, res); | |
163 | if (IS_ERR(dest_ptr)) | |
164 | return NULL; | |
165 | ||
166 | return dest_ptr; | |
167 | } | |
72f8c0bf WS |
168 | EXPORT_SYMBOL(devm_request_and_ioremap); |
169 | ||
5ea81769 AV |
170 | #ifdef CONFIG_HAS_IOPORT |
171 | /* | |
172 | * Generic iomap devres | |
173 | */ | |
174 | static void devm_ioport_map_release(struct device *dev, void *res) | |
175 | { | |
176 | ioport_unmap(*(void __iomem **)res); | |
177 | } | |
178 | ||
179 | static int devm_ioport_map_match(struct device *dev, void *res, | |
180 | void *match_data) | |
181 | { | |
182 | return *(void **)res == match_data; | |
183 | } | |
184 | ||
185 | /** | |
186 | * devm_ioport_map - Managed ioport_map() | |
187 | * @dev: Generic device to map ioport for | |
188 | * @port: Port to map | |
189 | * @nr: Number of ports to map | |
190 | * | |
191 | * Managed ioport_map(). Map is automatically unmapped on driver | |
192 | * detach. | |
193 | */ | |
194 | void __iomem * devm_ioport_map(struct device *dev, unsigned long port, | |
195 | unsigned int nr) | |
196 | { | |
197 | void __iomem **ptr, *addr; | |
198 | ||
199 | ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL); | |
200 | if (!ptr) | |
201 | return NULL; | |
202 | ||
203 | addr = ioport_map(port, nr); | |
204 | if (addr) { | |
205 | *ptr = addr; | |
206 | devres_add(dev, ptr); | |
207 | } else | |
208 | devres_free(ptr); | |
209 | ||
210 | return addr; | |
211 | } | |
212 | EXPORT_SYMBOL(devm_ioport_map); | |
213 | ||
214 | /** | |
215 | * devm_ioport_unmap - Managed ioport_unmap() | |
216 | * @dev: Generic device to unmap for | |
217 | * @addr: Address to unmap | |
218 | * | |
219 | * Managed ioport_unmap(). @addr must have been mapped using | |
220 | * devm_ioport_map(). | |
221 | */ | |
222 | void devm_ioport_unmap(struct device *dev, void __iomem *addr) | |
223 | { | |
224 | ioport_unmap(addr); | |
225 | WARN_ON(devres_destroy(dev, devm_ioport_map_release, | |
226 | devm_ioport_map_match, (void *)addr)); | |
227 | } | |
228 | EXPORT_SYMBOL(devm_ioport_unmap); | |
229 | ||
230 | #ifdef CONFIG_PCI | |
231 | /* | |
232 | * PCI iomap devres | |
233 | */ | |
234 | #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE | |
235 | ||
236 | struct pcim_iomap_devres { | |
237 | void __iomem *table[PCIM_IOMAP_MAX]; | |
238 | }; | |
239 | ||
240 | static void pcim_iomap_release(struct device *gendev, void *res) | |
241 | { | |
242 | struct pci_dev *dev = container_of(gendev, struct pci_dev, dev); | |
243 | struct pcim_iomap_devres *this = res; | |
244 | int i; | |
245 | ||
246 | for (i = 0; i < PCIM_IOMAP_MAX; i++) | |
247 | if (this->table[i]) | |
248 | pci_iounmap(dev, this->table[i]); | |
249 | } | |
250 | ||
251 | /** | |
252 | * pcim_iomap_table - access iomap allocation table | |
253 | * @pdev: PCI device to access iomap table for | |
254 | * | |
255 | * Access iomap allocation table for @dev. If iomap table doesn't | |
256 | * exist and @pdev is managed, it will be allocated. All iomaps | |
257 | * recorded in the iomap table are automatically unmapped on driver | |
258 | * detach. | |
259 | * | |
260 | * This function might sleep when the table is first allocated but can | |
261 | * be safely called without context and guaranteed to succed once | |
262 | * allocated. | |
263 | */ | |
264 | void __iomem * const * pcim_iomap_table(struct pci_dev *pdev) | |
265 | { | |
266 | struct pcim_iomap_devres *dr, *new_dr; | |
267 | ||
268 | dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL); | |
269 | if (dr) | |
270 | return dr->table; | |
271 | ||
272 | new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL); | |
273 | if (!new_dr) | |
274 | return NULL; | |
275 | dr = devres_get(&pdev->dev, new_dr, NULL, NULL); | |
276 | return dr->table; | |
277 | } | |
278 | EXPORT_SYMBOL(pcim_iomap_table); | |
279 | ||
280 | /** | |
281 | * pcim_iomap - Managed pcim_iomap() | |
282 | * @pdev: PCI device to iomap for | |
283 | * @bar: BAR to iomap | |
284 | * @maxlen: Maximum length of iomap | |
285 | * | |
286 | * Managed pci_iomap(). Map is automatically unmapped on driver | |
287 | * detach. | |
288 | */ | |
289 | void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen) | |
290 | { | |
291 | void __iomem **tbl; | |
292 | ||
293 | BUG_ON(bar >= PCIM_IOMAP_MAX); | |
294 | ||
295 | tbl = (void __iomem **)pcim_iomap_table(pdev); | |
296 | if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ | |
297 | return NULL; | |
298 | ||
299 | tbl[bar] = pci_iomap(pdev, bar, maxlen); | |
300 | return tbl[bar]; | |
301 | } | |
302 | EXPORT_SYMBOL(pcim_iomap); | |
303 | ||
304 | /** | |
305 | * pcim_iounmap - Managed pci_iounmap() | |
306 | * @pdev: PCI device to iounmap for | |
307 | * @addr: Address to unmap | |
308 | * | |
309 | * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap(). | |
310 | */ | |
311 | void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) | |
312 | { | |
313 | void __iomem **tbl; | |
314 | int i; | |
315 | ||
316 | pci_iounmap(pdev, addr); | |
317 | ||
318 | tbl = (void __iomem **)pcim_iomap_table(pdev); | |
319 | BUG_ON(!tbl); | |
320 | ||
321 | for (i = 0; i < PCIM_IOMAP_MAX; i++) | |
322 | if (tbl[i] == addr) { | |
323 | tbl[i] = NULL; | |
324 | return; | |
325 | } | |
326 | WARN_ON(1); | |
327 | } | |
328 | EXPORT_SYMBOL(pcim_iounmap); | |
329 | ||
330 | /** | |
331 | * pcim_iomap_regions - Request and iomap PCI BARs | |
332 | * @pdev: PCI device to map IO resources for | |
333 | * @mask: Mask of BARs to request and iomap | |
334 | * @name: Name used when requesting regions | |
335 | * | |
336 | * Request and iomap regions specified by @mask. | |
337 | */ | |
fb7ebfe4 | 338 | int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) |
5ea81769 AV |
339 | { |
340 | void __iomem * const *iomap; | |
341 | int i, rc; | |
342 | ||
343 | iomap = pcim_iomap_table(pdev); | |
344 | if (!iomap) | |
345 | return -ENOMEM; | |
346 | ||
347 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { | |
348 | unsigned long len; | |
349 | ||
350 | if (!(mask & (1 << i))) | |
351 | continue; | |
352 | ||
353 | rc = -EINVAL; | |
354 | len = pci_resource_len(pdev, i); | |
355 | if (!len) | |
356 | goto err_inval; | |
357 | ||
358 | rc = pci_request_region(pdev, i, name); | |
359 | if (rc) | |
fb4d64e7 | 360 | goto err_inval; |
5ea81769 AV |
361 | |
362 | rc = -ENOMEM; | |
363 | if (!pcim_iomap(pdev, i, 0)) | |
fb4d64e7 | 364 | goto err_region; |
5ea81769 AV |
365 | } |
366 | ||
367 | return 0; | |
368 | ||
5ea81769 AV |
369 | err_region: |
370 | pci_release_region(pdev, i); | |
371 | err_inval: | |
372 | while (--i >= 0) { | |
fb4d64e7 FD |
373 | if (!(mask & (1 << i))) |
374 | continue; | |
5ea81769 AV |
375 | pcim_iounmap(pdev, iomap[i]); |
376 | pci_release_region(pdev, i); | |
377 | } | |
378 | ||
379 | return rc; | |
380 | } | |
381 | EXPORT_SYMBOL(pcim_iomap_regions); | |
ec04b075 | 382 | |
916fbfb7 TH |
383 | /** |
384 | * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones | |
385 | * @pdev: PCI device to map IO resources for | |
386 | * @mask: Mask of BARs to iomap | |
387 | * @name: Name used when requesting regions | |
388 | * | |
389 | * Request all PCI BARs and iomap regions specified by @mask. | |
390 | */ | |
fb7ebfe4 | 391 | int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, |
916fbfb7 TH |
392 | const char *name) |
393 | { | |
394 | int request_mask = ((1 << 6) - 1) & ~mask; | |
395 | int rc; | |
396 | ||
397 | rc = pci_request_selected_regions(pdev, request_mask, name); | |
398 | if (rc) | |
399 | return rc; | |
400 | ||
401 | rc = pcim_iomap_regions(pdev, mask, name); | |
402 | if (rc) | |
403 | pci_release_selected_regions(pdev, request_mask); | |
404 | return rc; | |
405 | } | |
406 | EXPORT_SYMBOL(pcim_iomap_regions_request_all); | |
407 | ||
ec04b075 TH |
408 | /** |
409 | * pcim_iounmap_regions - Unmap and release PCI BARs | |
410 | * @pdev: PCI device to map IO resources for | |
411 | * @mask: Mask of BARs to unmap and release | |
412 | * | |
4d45ada3 | 413 | * Unmap and release regions specified by @mask. |
ec04b075 | 414 | */ |
fb7ebfe4 | 415 | void pcim_iounmap_regions(struct pci_dev *pdev, int mask) |
ec04b075 TH |
416 | { |
417 | void __iomem * const *iomap; | |
418 | int i; | |
419 | ||
420 | iomap = pcim_iomap_table(pdev); | |
421 | if (!iomap) | |
422 | return; | |
423 | ||
424 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { | |
425 | if (!(mask & (1 << i))) | |
426 | continue; | |
427 | ||
428 | pcim_iounmap(pdev, iomap[i]); | |
429 | pci_release_region(pdev, i); | |
430 | } | |
431 | } | |
432 | EXPORT_SYMBOL(pcim_iounmap_regions); | |
571806a9 WS |
433 | #endif /* CONFIG_PCI */ |
434 | #endif /* CONFIG_HAS_IOPORT */ |