mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / lib / devres.c
blob40a8b12a8b6b9237f34a63d90f4566a2b24fd154
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>
8 void devm_ioremap_release(struct device *dev, void *res)
10 iounmap(*(void __iomem **)res);
13 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
15 return *(void **)res == match_data;
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
24 * Managed ioremap(). Map is automatically unmapped on driver detach.
26 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
27 resource_size_t size)
29 void __iomem **ptr, *addr;
31 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
32 if (!ptr)
33 return NULL;
35 addr = ioremap(offset, size);
36 if (addr) {
37 *ptr = addr;
38 devres_add(dev, ptr);
39 } else
40 devres_free(ptr);
42 return addr;
44 EXPORT_SYMBOL(devm_ioremap);
46 /**
47 * devm_ioremap_nocache - Managed ioremap_nocache()
48 * @dev: Generic device to remap IO address for
49 * @offset: Resource address to map
50 * @size: Size of map
52 * Managed ioremap_nocache(). Map is automatically unmapped on driver
53 * detach.
55 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
56 resource_size_t size)
58 void __iomem **ptr, *addr;
60 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
61 if (!ptr)
62 return NULL;
64 addr = ioremap_nocache(offset, size);
65 if (addr) {
66 *ptr = addr;
67 devres_add(dev, ptr);
68 } else
69 devres_free(ptr);
71 return addr;
73 EXPORT_SYMBOL(devm_ioremap_nocache);
75 /**
76 * devm_ioremap_wc - Managed ioremap_wc()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to map
79 * @size: Size of map
81 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
83 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
84 resource_size_t size)
86 void __iomem **ptr, *addr;
88 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
89 if (!ptr)
90 return NULL;
92 addr = ioremap_wc(offset, size);
93 if (addr) {
94 *ptr = addr;
95 devres_add(dev, ptr);
96 } else
97 devres_free(ptr);
99 return addr;
101 EXPORT_SYMBOL(devm_ioremap_wc);
104 * devm_iounmap - Managed iounmap()
105 * @dev: Generic device to unmap for
106 * @addr: Address to unmap
108 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
110 void devm_iounmap(struct device *dev, void __iomem *addr)
112 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
113 (__force void *)addr));
114 iounmap(addr);
116 EXPORT_SYMBOL(devm_iounmap);
119 * devm_ioremap_resource() - check, request region, and ioremap resource
120 * @dev: generic device to handle the resource for
121 * @res: resource to be handled
123 * Checks that a resource is a valid memory region, requests the memory
124 * region and ioremaps it. All operations are managed and will be undone
125 * on driver detach.
127 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
128 * on failure. Usage example:
130 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 * base = devm_ioremap_resource(&pdev->dev, res);
132 * if (IS_ERR(base))
133 * return PTR_ERR(base);
135 void __iomem *devm_ioremap_resource(struct device *dev,
136 const struct resource *res)
138 resource_size_t size;
139 const char *name;
140 void __iomem *dest_ptr;
142 BUG_ON(!dev);
144 if (!res || resource_type(res) != IORESOURCE_MEM) {
145 dev_err(dev, "invalid resource\n");
146 return IOMEM_ERR_PTR(-EINVAL);
149 size = resource_size(res);
150 name = res->name ?: dev_name(dev);
152 if (!devm_request_mem_region(dev, res->start, size, name)) {
153 dev_err(dev, "can't request region for resource %pR\n", res);
154 return IOMEM_ERR_PTR(-EBUSY);
157 dest_ptr = devm_ioremap(dev, res->start, size);
158 if (!dest_ptr) {
159 dev_err(dev, "ioremap failed for resource %pR\n", res);
160 devm_release_mem_region(dev, res->start, size);
161 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
164 return dest_ptr;
166 EXPORT_SYMBOL(devm_ioremap_resource);
168 #ifdef CONFIG_HAS_IOPORT_MAP
170 * Generic iomap devres
172 static void devm_ioport_map_release(struct device *dev, void *res)
174 ioport_unmap(*(void __iomem **)res);
177 static int devm_ioport_map_match(struct device *dev, void *res,
178 void *match_data)
180 return *(void **)res == match_data;
184 * devm_ioport_map - Managed ioport_map()
185 * @dev: Generic device to map ioport for
186 * @port: Port to map
187 * @nr: Number of ports to map
189 * Managed ioport_map(). Map is automatically unmapped on driver
190 * detach.
192 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
193 unsigned int nr)
195 void __iomem **ptr, *addr;
197 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
198 if (!ptr)
199 return NULL;
201 addr = ioport_map(port, nr);
202 if (addr) {
203 *ptr = addr;
204 devres_add(dev, ptr);
205 } else
206 devres_free(ptr);
208 return addr;
210 EXPORT_SYMBOL(devm_ioport_map);
213 * devm_ioport_unmap - Managed ioport_unmap()
214 * @dev: Generic device to unmap for
215 * @addr: Address to unmap
217 * Managed ioport_unmap(). @addr must have been mapped using
218 * devm_ioport_map().
220 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
222 ioport_unmap(addr);
223 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
224 devm_ioport_map_match, (__force void *)addr));
226 EXPORT_SYMBOL(devm_ioport_unmap);
227 #endif /* CONFIG_HAS_IOPORT_MAP */
229 #ifdef CONFIG_PCI
231 * PCI iomap devres
233 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
235 struct pcim_iomap_devres {
236 void __iomem *table[PCIM_IOMAP_MAX];
239 static void pcim_iomap_release(struct device *gendev, void *res)
241 struct pci_dev *dev = to_pci_dev(gendev);
242 struct pcim_iomap_devres *this = res;
243 int i;
245 for (i = 0; i < PCIM_IOMAP_MAX; i++)
246 if (this->table[i])
247 pci_iounmap(dev, this->table[i]);
251 * pcim_iomap_table - access iomap allocation table
252 * @pdev: PCI device to access iomap table for
254 * Access iomap allocation table for @dev. If iomap table doesn't
255 * exist and @pdev is managed, it will be allocated. All iomaps
256 * recorded in the iomap table are automatically unmapped on driver
257 * detach.
259 * This function might sleep when the table is first allocated but can
260 * be safely called without context and guaranteed to succed once
261 * allocated.
263 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
265 struct pcim_iomap_devres *dr, *new_dr;
267 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
268 if (dr)
269 return dr->table;
271 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
272 if (!new_dr)
273 return NULL;
274 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
275 return dr->table;
277 EXPORT_SYMBOL(pcim_iomap_table);
280 * pcim_iomap - Managed pcim_iomap()
281 * @pdev: PCI device to iomap for
282 * @bar: BAR to iomap
283 * @maxlen: Maximum length of iomap
285 * Managed pci_iomap(). Map is automatically unmapped on driver
286 * detach.
288 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
290 void __iomem **tbl;
292 BUG_ON(bar >= PCIM_IOMAP_MAX);
294 tbl = (void __iomem **)pcim_iomap_table(pdev);
295 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
296 return NULL;
298 tbl[bar] = pci_iomap(pdev, bar, maxlen);
299 return tbl[bar];
301 EXPORT_SYMBOL(pcim_iomap);
304 * pcim_iounmap - Managed pci_iounmap()
305 * @pdev: PCI device to iounmap for
306 * @addr: Address to unmap
308 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
310 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
312 void __iomem **tbl;
313 int i;
315 pci_iounmap(pdev, addr);
317 tbl = (void __iomem **)pcim_iomap_table(pdev);
318 BUG_ON(!tbl);
320 for (i = 0; i < PCIM_IOMAP_MAX; i++)
321 if (tbl[i] == addr) {
322 tbl[i] = NULL;
323 return;
325 WARN_ON(1);
327 EXPORT_SYMBOL(pcim_iounmap);
330 * pcim_iomap_regions - Request and iomap PCI BARs
331 * @pdev: PCI device to map IO resources for
332 * @mask: Mask of BARs to request and iomap
333 * @name: Name used when requesting regions
335 * Request and iomap regions specified by @mask.
337 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
339 void __iomem * const *iomap;
340 int i, rc;
342 iomap = pcim_iomap_table(pdev);
343 if (!iomap)
344 return -ENOMEM;
346 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
347 unsigned long len;
349 if (!(mask & (1 << i)))
350 continue;
352 rc = -EINVAL;
353 len = pci_resource_len(pdev, i);
354 if (!len)
355 goto err_inval;
357 rc = pci_request_region(pdev, i, name);
358 if (rc)
359 goto err_inval;
361 rc = -ENOMEM;
362 if (!pcim_iomap(pdev, i, 0))
363 goto err_region;
366 return 0;
368 err_region:
369 pci_release_region(pdev, i);
370 err_inval:
371 while (--i >= 0) {
372 if (!(mask & (1 << i)))
373 continue;
374 pcim_iounmap(pdev, iomap[i]);
375 pci_release_region(pdev, i);
378 return rc;
380 EXPORT_SYMBOL(pcim_iomap_regions);
383 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
384 * @pdev: PCI device to map IO resources for
385 * @mask: Mask of BARs to iomap
386 * @name: Name used when requesting regions
388 * Request all PCI BARs and iomap regions specified by @mask.
390 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
391 const char *name)
393 int request_mask = ((1 << 6) - 1) & ~mask;
394 int rc;
396 rc = pci_request_selected_regions(pdev, request_mask, name);
397 if (rc)
398 return rc;
400 rc = pcim_iomap_regions(pdev, mask, name);
401 if (rc)
402 pci_release_selected_regions(pdev, request_mask);
403 return rc;
405 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
408 * pcim_iounmap_regions - Unmap and release PCI BARs
409 * @pdev: PCI device to map IO resources for
410 * @mask: Mask of BARs to unmap and release
412 * Unmap and release regions specified by @mask.
414 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
416 void __iomem * const *iomap;
417 int i;
419 iomap = pcim_iomap_table(pdev);
420 if (!iomap)
421 return;
423 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
424 if (!(mask & (1 << i)))
425 continue;
427 pcim_iounmap(pdev, iomap[i]);
428 pci_release_region(pdev, i);
431 EXPORT_SYMBOL(pcim_iounmap_regions);
432 #endif /* CONFIG_PCI */