]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - lib/devres.c
devres: allow const resource arguments
[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,
165 const struct resource *res)
166 {
167 resource_size_t size;
168 const char *name;
169 void __iomem *dest_ptr;
170
171 BUG_ON(!dev);
172
173 if (!res || resource_type(res) != IORESOURCE_MEM) {
174 dev_err(dev, "invalid resource\n");
175 return IOMEM_ERR_PTR(-EINVAL);
176 }
177
178 size = resource_size(res);
179 name = res->name ?: dev_name(dev);
180
181 if (!devm_request_mem_region(dev, res->start, size, name)) {
182 dev_err(dev, "can't request region for resource %pR\n", res);
183 return IOMEM_ERR_PTR(-EBUSY);
184 }
185
186 dest_ptr = devm_ioremap(dev, res->start, size);
187 if (!dest_ptr) {
188 dev_err(dev, "ioremap failed for resource %pR\n", res);
189 devm_release_mem_region(dev, res->start, size);
190 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
191 }
192
193 return dest_ptr;
194 }
195 EXPORT_SYMBOL(devm_ioremap_resource);
196
197 #ifdef CONFIG_HAS_IOPORT_MAP
198 /*
199 * Generic iomap devres
200 */
201 static void devm_ioport_map_release(struct device *dev, void *res)
202 {
203 ioport_unmap(*(void __iomem **)res);
204 }
205
206 static int devm_ioport_map_match(struct device *dev, void *res,
207 void *match_data)
208 {
209 return *(void **)res == match_data;
210 }
211
212 /**
213 * devm_ioport_map - Managed ioport_map()
214 * @dev: Generic device to map ioport for
215 * @port: Port to map
216 * @nr: Number of ports to map
217 *
218 * Managed ioport_map(). Map is automatically unmapped on driver
219 * detach.
220 */
221 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
222 unsigned int nr)
223 {
224 void __iomem **ptr, *addr;
225
226 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
227 if (!ptr)
228 return NULL;
229
230 addr = ioport_map(port, nr);
231 if (addr) {
232 *ptr = addr;
233 devres_add(dev, ptr);
234 } else
235 devres_free(ptr);
236
237 return addr;
238 }
239 EXPORT_SYMBOL(devm_ioport_map);
240
241 /**
242 * devm_ioport_unmap - Managed ioport_unmap()
243 * @dev: Generic device to unmap for
244 * @addr: Address to unmap
245 *
246 * Managed ioport_unmap(). @addr must have been mapped using
247 * devm_ioport_map().
248 */
249 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
250 {
251 ioport_unmap(addr);
252 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
253 devm_ioport_map_match, (__force void *)addr));
254 }
255 EXPORT_SYMBOL(devm_ioport_unmap);
256 #endif /* CONFIG_HAS_IOPORT_MAP */
257
258 #ifdef CONFIG_PCI
259 /*
260 * PCI iomap devres
261 */
262 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
263
264 struct pcim_iomap_devres {
265 void __iomem *table[PCIM_IOMAP_MAX];
266 };
267
268 static void pcim_iomap_release(struct device *gendev, void *res)
269 {
270 struct pci_dev *dev = to_pci_dev(gendev);
271 struct pcim_iomap_devres *this = res;
272 int i;
273
274 for (i = 0; i < PCIM_IOMAP_MAX; i++)
275 if (this->table[i])
276 pci_iounmap(dev, this->table[i]);
277 }
278
279 /**
280 * pcim_iomap_table - access iomap allocation table
281 * @pdev: PCI device to access iomap table for
282 *
283 * Access iomap allocation table for @dev. If iomap table doesn't
284 * exist and @pdev is managed, it will be allocated. All iomaps
285 * recorded in the iomap table are automatically unmapped on driver
286 * detach.
287 *
288 * This function might sleep when the table is first allocated but can
289 * be safely called without context and guaranteed to succed once
290 * allocated.
291 */
292 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
293 {
294 struct pcim_iomap_devres *dr, *new_dr;
295
296 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
297 if (dr)
298 return dr->table;
299
300 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
301 if (!new_dr)
302 return NULL;
303 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
304 return dr->table;
305 }
306 EXPORT_SYMBOL(pcim_iomap_table);
307
308 /**
309 * pcim_iomap - Managed pcim_iomap()
310 * @pdev: PCI device to iomap for
311 * @bar: BAR to iomap
312 * @maxlen: Maximum length of iomap
313 *
314 * Managed pci_iomap(). Map is automatically unmapped on driver
315 * detach.
316 */
317 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
318 {
319 void __iomem **tbl;
320
321 BUG_ON(bar >= PCIM_IOMAP_MAX);
322
323 tbl = (void __iomem **)pcim_iomap_table(pdev);
324 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
325 return NULL;
326
327 tbl[bar] = pci_iomap(pdev, bar, maxlen);
328 return tbl[bar];
329 }
330 EXPORT_SYMBOL(pcim_iomap);
331
332 /**
333 * pcim_iounmap - Managed pci_iounmap()
334 * @pdev: PCI device to iounmap for
335 * @addr: Address to unmap
336 *
337 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
338 */
339 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
340 {
341 void __iomem **tbl;
342 int i;
343
344 pci_iounmap(pdev, addr);
345
346 tbl = (void __iomem **)pcim_iomap_table(pdev);
347 BUG_ON(!tbl);
348
349 for (i = 0; i < PCIM_IOMAP_MAX; i++)
350 if (tbl[i] == addr) {
351 tbl[i] = NULL;
352 return;
353 }
354 WARN_ON(1);
355 }
356 EXPORT_SYMBOL(pcim_iounmap);
357
358 /**
359 * pcim_iomap_regions - Request and iomap PCI BARs
360 * @pdev: PCI device to map IO resources for
361 * @mask: Mask of BARs to request and iomap
362 * @name: Name used when requesting regions
363 *
364 * Request and iomap regions specified by @mask.
365 */
366 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
367 {
368 void __iomem * const *iomap;
369 int i, rc;
370
371 iomap = pcim_iomap_table(pdev);
372 if (!iomap)
373 return -ENOMEM;
374
375 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
376 unsigned long len;
377
378 if (!(mask & (1 << i)))
379 continue;
380
381 rc = -EINVAL;
382 len = pci_resource_len(pdev, i);
383 if (!len)
384 goto err_inval;
385
386 rc = pci_request_region(pdev, i, name);
387 if (rc)
388 goto err_inval;
389
390 rc = -ENOMEM;
391 if (!pcim_iomap(pdev, i, 0))
392 goto err_region;
393 }
394
395 return 0;
396
397 err_region:
398 pci_release_region(pdev, i);
399 err_inval:
400 while (--i >= 0) {
401 if (!(mask & (1 << i)))
402 continue;
403 pcim_iounmap(pdev, iomap[i]);
404 pci_release_region(pdev, i);
405 }
406
407 return rc;
408 }
409 EXPORT_SYMBOL(pcim_iomap_regions);
410
411 /**
412 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
413 * @pdev: PCI device to map IO resources for
414 * @mask: Mask of BARs to iomap
415 * @name: Name used when requesting regions
416 *
417 * Request all PCI BARs and iomap regions specified by @mask.
418 */
419 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
420 const char *name)
421 {
422 int request_mask = ((1 << 6) - 1) & ~mask;
423 int rc;
424
425 rc = pci_request_selected_regions(pdev, request_mask, name);
426 if (rc)
427 return rc;
428
429 rc = pcim_iomap_regions(pdev, mask, name);
430 if (rc)
431 pci_release_selected_regions(pdev, request_mask);
432 return rc;
433 }
434 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
435
436 /**
437 * pcim_iounmap_regions - Unmap and release PCI BARs
438 * @pdev: PCI device to map IO resources for
439 * @mask: Mask of BARs to unmap and release
440 *
441 * Unmap and release regions specified by @mask.
442 */
443 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
444 {
445 void __iomem * const *iomap;
446 int i;
447
448 iomap = pcim_iomap_table(pdev);
449 if (!iomap)
450 return;
451
452 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
453 if (!(mask & (1 << i)))
454 continue;
455
456 pcim_iounmap(pdev, iomap[i]);
457 pci_release_region(pdev, i);
458 }
459 }
460 EXPORT_SYMBOL(pcim_iounmap_regions);
461 #endif /* CONFIG_PCI */