]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - lib/devres.c
idr: Fix idr_alloc_u32 on 32-bit systems
[mirror_ubuntu-bionic-kernel.git] / lib / devres.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/err.h>
3 #include <linux/pci.h>
4 #include <linux/io.h>
5 #include <linux/gfp.h>
6 #include <linux/export.h>
7
8 void devm_ioremap_release(struct device *dev, void *res)
9 {
10 iounmap(*(void __iomem **)res);
11 }
12
13 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
14 {
15 return *(void **)res == match_data;
16 }
17
18 /**
19 * devm_ioremap - Managed ioremap()
20 * @dev: Generic device to remap IO address for
21 * @offset: Resource address to map
22 * @size: Size of map
23 *
24 * Managed ioremap(). Map is automatically unmapped on driver detach.
25 */
26 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
27 resource_size_t size)
28 {
29 void __iomem **ptr, *addr;
30
31 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
32 if (!ptr)
33 return NULL;
34
35 addr = ioremap(offset, size);
36 if (addr) {
37 *ptr = addr;
38 devres_add(dev, ptr);
39 } else
40 devres_free(ptr);
41
42 return addr;
43 }
44 EXPORT_SYMBOL(devm_ioremap);
45
46 /**
47 * devm_ioremap_uc - Managed ioremap_uc()
48 * @dev: Generic device to remap IO address for
49 * @offset: Resource address to map
50 * @size: Size of map
51 *
52 * Managed ioremap_uc(). Map is automatically unmapped on driver detach.
53 */
54 void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
55 resource_size_t size)
56 {
57 void __iomem **ptr, *addr;
58
59 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
60 if (!ptr)
61 return NULL;
62
63 addr = ioremap_uc(offset, size);
64 if (addr) {
65 *ptr = addr;
66 devres_add(dev, ptr);
67 } else
68 devres_free(ptr);
69
70 return addr;
71 }
72 EXPORT_SYMBOL(devm_ioremap_uc);
73
74
75 /**
76 * devm_ioremap_nocache - Managed ioremap_nocache()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to map
79 * @size: Size of map
80 *
81 * Managed ioremap_nocache(). Map is automatically unmapped on driver
82 * detach.
83 */
84 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
85 resource_size_t size)
86 {
87 void __iomem **ptr, *addr;
88
89 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
90 if (!ptr)
91 return NULL;
92
93 addr = ioremap_nocache(offset, size);
94 if (addr) {
95 *ptr = addr;
96 devres_add(dev, ptr);
97 } else
98 devres_free(ptr);
99
100 return addr;
101 }
102 EXPORT_SYMBOL(devm_ioremap_nocache);
103
104 /**
105 * devm_ioremap_wc - Managed ioremap_wc()
106 * @dev: Generic device to remap IO address for
107 * @offset: Resource address to map
108 * @size: Size of map
109 *
110 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
111 */
112 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
113 resource_size_t size)
114 {
115 void __iomem **ptr, *addr;
116
117 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
118 if (!ptr)
119 return NULL;
120
121 addr = ioremap_wc(offset, size);
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_ioremap_wc);
131
132 /**
133 * devm_iounmap - Managed iounmap()
134 * @dev: Generic device to unmap for
135 * @addr: Address to unmap
136 *
137 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
138 */
139 void devm_iounmap(struct device *dev, void __iomem *addr)
140 {
141 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
142 (__force void *)addr));
143 iounmap(addr);
144 }
145 EXPORT_SYMBOL(devm_iounmap);
146
147 /**
148 * devm_ioremap_resource() - check, request region, and ioremap resource
149 * @dev: generic device to handle the resource for
150 * @res: resource to be handled
151 *
152 * Checks that a resource is a valid memory region, requests the memory
153 * region and ioremaps it. All operations are managed and will be undone
154 * on driver detach.
155 *
156 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
157 * on failure. Usage example:
158 *
159 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
160 * base = devm_ioremap_resource(&pdev->dev, res);
161 * if (IS_ERR(base))
162 * return PTR_ERR(base);
163 */
164 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res)
165 {
166 resource_size_t size;
167 const char *name;
168 void __iomem *dest_ptr;
169
170 BUG_ON(!dev);
171
172 if (!res || resource_type(res) != IORESOURCE_MEM) {
173 dev_err(dev, "invalid resource\n");
174 return IOMEM_ERR_PTR(-EINVAL);
175 }
176
177 size = resource_size(res);
178 name = res->name ?: dev_name(dev);
179
180 if (!devm_request_mem_region(dev, res->start, size, name)) {
181 dev_err(dev, "can't request region for resource %pR\n", res);
182 return IOMEM_ERR_PTR(-EBUSY);
183 }
184
185 dest_ptr = devm_ioremap(dev, res->start, size);
186 if (!dest_ptr) {
187 dev_err(dev, "ioremap failed for resource %pR\n", res);
188 devm_release_mem_region(dev, res->start, size);
189 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
190 }
191
192 return dest_ptr;
193 }
194 EXPORT_SYMBOL(devm_ioremap_resource);
195
196 #ifdef CONFIG_HAS_IOPORT_MAP
197 /*
198 * Generic iomap devres
199 */
200 static void devm_ioport_map_release(struct device *dev, void *res)
201 {
202 ioport_unmap(*(void __iomem **)res);
203 }
204
205 static int devm_ioport_map_match(struct device *dev, void *res,
206 void *match_data)
207 {
208 return *(void **)res == match_data;
209 }
210
211 /**
212 * devm_ioport_map - Managed ioport_map()
213 * @dev: Generic device to map ioport for
214 * @port: Port to map
215 * @nr: Number of ports to map
216 *
217 * Managed ioport_map(). Map is automatically unmapped on driver
218 * detach.
219 */
220 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
221 unsigned int nr)
222 {
223 void __iomem **ptr, *addr;
224
225 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
226 if (!ptr)
227 return NULL;
228
229 addr = ioport_map(port, nr);
230 if (addr) {
231 *ptr = addr;
232 devres_add(dev, ptr);
233 } else
234 devres_free(ptr);
235
236 return addr;
237 }
238 EXPORT_SYMBOL(devm_ioport_map);
239
240 /**
241 * devm_ioport_unmap - Managed ioport_unmap()
242 * @dev: Generic device to unmap for
243 * @addr: Address to unmap
244 *
245 * Managed ioport_unmap(). @addr must have been mapped using
246 * devm_ioport_map().
247 */
248 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
249 {
250 ioport_unmap(addr);
251 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
252 devm_ioport_map_match, (__force void *)addr));
253 }
254 EXPORT_SYMBOL(devm_ioport_unmap);
255 #endif /* CONFIG_HAS_IOPORT_MAP */
256
257 #ifdef CONFIG_PCI
258 /*
259 * PCI iomap devres
260 */
261 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
262
263 struct pcim_iomap_devres {
264 void __iomem *table[PCIM_IOMAP_MAX];
265 };
266
267 static void pcim_iomap_release(struct device *gendev, void *res)
268 {
269 struct pci_dev *dev = to_pci_dev(gendev);
270 struct pcim_iomap_devres *this = res;
271 int i;
272
273 for (i = 0; i < PCIM_IOMAP_MAX; i++)
274 if (this->table[i])
275 pci_iounmap(dev, this->table[i]);
276 }
277
278 /**
279 * pcim_iomap_table - access iomap allocation table
280 * @pdev: PCI device to access iomap table for
281 *
282 * Access iomap allocation table for @dev. If iomap table doesn't
283 * exist and @pdev is managed, it will be allocated. All iomaps
284 * recorded in the iomap table are automatically unmapped on driver
285 * detach.
286 *
287 * This function might sleep when the table is first allocated but can
288 * be safely called without context and guaranteed to succed once
289 * allocated.
290 */
291 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
292 {
293 struct pcim_iomap_devres *dr, *new_dr;
294
295 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
296 if (dr)
297 return dr->table;
298
299 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
300 if (!new_dr)
301 return NULL;
302 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
303 return dr->table;
304 }
305 EXPORT_SYMBOL(pcim_iomap_table);
306
307 /**
308 * pcim_iomap - Managed pcim_iomap()
309 * @pdev: PCI device to iomap for
310 * @bar: BAR to iomap
311 * @maxlen: Maximum length of iomap
312 *
313 * Managed pci_iomap(). Map is automatically unmapped on driver
314 * detach.
315 */
316 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
317 {
318 void __iomem **tbl;
319
320 BUG_ON(bar >= PCIM_IOMAP_MAX);
321
322 tbl = (void __iomem **)pcim_iomap_table(pdev);
323 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
324 return NULL;
325
326 tbl[bar] = pci_iomap(pdev, bar, maxlen);
327 return tbl[bar];
328 }
329 EXPORT_SYMBOL(pcim_iomap);
330
331 /**
332 * pcim_iounmap - Managed pci_iounmap()
333 * @pdev: PCI device to iounmap for
334 * @addr: Address to unmap
335 *
336 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
337 */
338 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
339 {
340 void __iomem **tbl;
341 int i;
342
343 pci_iounmap(pdev, addr);
344
345 tbl = (void __iomem **)pcim_iomap_table(pdev);
346 BUG_ON(!tbl);
347
348 for (i = 0; i < PCIM_IOMAP_MAX; i++)
349 if (tbl[i] == addr) {
350 tbl[i] = NULL;
351 return;
352 }
353 WARN_ON(1);
354 }
355 EXPORT_SYMBOL(pcim_iounmap);
356
357 /**
358 * pcim_iomap_regions - Request and iomap PCI BARs
359 * @pdev: PCI device to map IO resources for
360 * @mask: Mask of BARs to request and iomap
361 * @name: Name used when requesting regions
362 *
363 * Request and iomap regions specified by @mask.
364 */
365 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
366 {
367 void __iomem * const *iomap;
368 int i, rc;
369
370 iomap = pcim_iomap_table(pdev);
371 if (!iomap)
372 return -ENOMEM;
373
374 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
375 unsigned long len;
376
377 if (!(mask & (1 << i)))
378 continue;
379
380 rc = -EINVAL;
381 len = pci_resource_len(pdev, i);
382 if (!len)
383 goto err_inval;
384
385 rc = pci_request_region(pdev, i, name);
386 if (rc)
387 goto err_inval;
388
389 rc = -ENOMEM;
390 if (!pcim_iomap(pdev, i, 0))
391 goto err_region;
392 }
393
394 return 0;
395
396 err_region:
397 pci_release_region(pdev, i);
398 err_inval:
399 while (--i >= 0) {
400 if (!(mask & (1 << i)))
401 continue;
402 pcim_iounmap(pdev, iomap[i]);
403 pci_release_region(pdev, i);
404 }
405
406 return rc;
407 }
408 EXPORT_SYMBOL(pcim_iomap_regions);
409
410 /**
411 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
412 * @pdev: PCI device to map IO resources for
413 * @mask: Mask of BARs to iomap
414 * @name: Name used when requesting regions
415 *
416 * Request all PCI BARs and iomap regions specified by @mask.
417 */
418 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
419 const char *name)
420 {
421 int request_mask = ((1 << 6) - 1) & ~mask;
422 int rc;
423
424 rc = pci_request_selected_regions(pdev, request_mask, name);
425 if (rc)
426 return rc;
427
428 rc = pcim_iomap_regions(pdev, mask, name);
429 if (rc)
430 pci_release_selected_regions(pdev, request_mask);
431 return rc;
432 }
433 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
434
435 /**
436 * pcim_iounmap_regions - Unmap and release PCI BARs
437 * @pdev: PCI device to map IO resources for
438 * @mask: Mask of BARs to unmap and release
439 *
440 * Unmap and release regions specified by @mask.
441 */
442 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
443 {
444 void __iomem * const *iomap;
445 int i;
446
447 iomap = pcim_iomap_table(pdev);
448 if (!iomap)
449 return;
450
451 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
452 if (!(mask & (1 << i)))
453 continue;
454
455 pcim_iounmap(pdev, iomap[i]);
456 pci_release_region(pdev, i);
457 }
458 }
459 EXPORT_SYMBOL(pcim_iounmap_regions);
460 #endif /* CONFIG_PCI */