split dev_queue
[cor.git] / lib / devres.c
blobf56070cf970b6a4ff9c760e598dceec489f2ec59
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 #include <linux/of_address.h>
9 enum devm_ioremap_type {
10 DEVM_IOREMAP = 0,
11 DEVM_IOREMAP_NC,
12 DEVM_IOREMAP_UC,
13 DEVM_IOREMAP_WC,
16 void devm_ioremap_release(struct device *dev, void *res)
18 iounmap(*(void __iomem **)res);
21 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
23 return *(void **)res == match_data;
26 static void __iomem *__devm_ioremap(struct device *dev, resource_size_t offset,
27 resource_size_t size,
28 enum devm_ioremap_type type)
30 void __iomem **ptr, *addr = NULL;
32 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
33 if (!ptr)
34 return NULL;
36 switch (type) {
37 case DEVM_IOREMAP:
38 addr = ioremap(offset, size);
39 break;
40 case DEVM_IOREMAP_NC:
41 addr = ioremap_nocache(offset, size);
42 break;
43 case DEVM_IOREMAP_UC:
44 addr = ioremap_uc(offset, size);
45 break;
46 case DEVM_IOREMAP_WC:
47 addr = ioremap_wc(offset, size);
48 break;
51 if (addr) {
52 *ptr = addr;
53 devres_add(dev, ptr);
54 } else
55 devres_free(ptr);
57 return addr;
60 /**
61 * devm_ioremap - Managed ioremap()
62 * @dev: Generic device to remap IO address for
63 * @offset: Resource address to map
64 * @size: Size of map
66 * Managed ioremap(). Map is automatically unmapped on driver detach.
68 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
69 resource_size_t size)
71 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
73 EXPORT_SYMBOL(devm_ioremap);
75 /**
76 * devm_ioremap_uc - Managed ioremap_uc()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to map
79 * @size: Size of map
81 * Managed ioremap_uc(). Map is automatically unmapped on driver detach.
83 void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
84 resource_size_t size)
86 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_UC);
88 EXPORT_SYMBOL_GPL(devm_ioremap_uc);
90 /**
91 * devm_ioremap_nocache - Managed ioremap_nocache()
92 * @dev: Generic device to remap IO address for
93 * @offset: Resource address to map
94 * @size: Size of map
96 * Managed ioremap_nocache(). Map is automatically unmapped on driver
97 * detach.
99 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
100 resource_size_t size)
102 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NC);
104 EXPORT_SYMBOL(devm_ioremap_nocache);
107 * devm_ioremap_wc - Managed ioremap_wc()
108 * @dev: Generic device to remap IO address for
109 * @offset: Resource address to map
110 * @size: Size of map
112 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
114 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
115 resource_size_t size)
117 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
119 EXPORT_SYMBOL(devm_ioremap_wc);
122 * devm_iounmap - Managed iounmap()
123 * @dev: Generic device to unmap for
124 * @addr: Address to unmap
126 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
128 void devm_iounmap(struct device *dev, void __iomem *addr)
130 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
131 (__force void *)addr));
132 iounmap(addr);
134 EXPORT_SYMBOL(devm_iounmap);
136 static void __iomem *
137 __devm_ioremap_resource(struct device *dev, const struct resource *res,
138 enum devm_ioremap_type type)
140 resource_size_t size;
141 void __iomem *dest_ptr;
143 BUG_ON(!dev);
145 if (!res || resource_type(res) != IORESOURCE_MEM) {
146 dev_err(dev, "invalid resource\n");
147 return IOMEM_ERR_PTR(-EINVAL);
150 size = resource_size(res);
152 if (!devm_request_mem_region(dev, res->start, size, dev_name(dev))) {
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, type);
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;
168 * devm_ioremap_resource() - check, request region, and ioremap resource
169 * @dev: generic device to handle the resource for
170 * @res: resource to be handled
172 * Checks that a resource is a valid memory region, requests the memory
173 * region and ioremaps it. All operations are managed and will be undone
174 * on driver detach.
176 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
177 * on failure. Usage example:
179 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 * base = devm_ioremap_resource(&pdev->dev, res);
181 * if (IS_ERR(base))
182 * return PTR_ERR(base);
184 void __iomem *devm_ioremap_resource(struct device *dev,
185 const struct resource *res)
187 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP);
189 EXPORT_SYMBOL(devm_ioremap_resource);
192 * devm_ioremap_resource_wc() - write-combined variant of
193 * devm_ioremap_resource()
194 * @dev: generic device to handle the resource for
195 * @res: resource to be handled
197 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
198 * on failure. Usage example:
200 void __iomem *devm_ioremap_resource_wc(struct device *dev,
201 const struct resource *res)
203 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP_WC);
207 * devm_of_iomap - Requests a resource and maps the memory mapped IO
208 * for a given device_node managed by a given device
210 * Checks that a resource is a valid memory region, requests the memory
211 * region and ioremaps it. All operations are managed and will be undone
212 * on driver detach of the device.
214 * This is to be used when a device requests/maps resources described
215 * by other device tree nodes (children or otherwise).
217 * @dev: The device "managing" the resource
218 * @node: The device-tree node where the resource resides
219 * @index: index of the MMIO range in the "reg" property
220 * @size: Returns the size of the resource (pass NULL if not needed)
221 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
222 * error code on failure. Usage example:
224 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
225 * if (IS_ERR(base))
226 * return PTR_ERR(base);
228 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
229 resource_size_t *size)
231 struct resource res;
233 if (of_address_to_resource(node, index, &res))
234 return IOMEM_ERR_PTR(-EINVAL);
235 if (size)
236 *size = resource_size(&res);
237 return devm_ioremap_resource(dev, &res);
239 EXPORT_SYMBOL(devm_of_iomap);
241 #ifdef CONFIG_HAS_IOPORT_MAP
243 * Generic iomap devres
245 static void devm_ioport_map_release(struct device *dev, void *res)
247 ioport_unmap(*(void __iomem **)res);
250 static int devm_ioport_map_match(struct device *dev, void *res,
251 void *match_data)
253 return *(void **)res == match_data;
257 * devm_ioport_map - Managed ioport_map()
258 * @dev: Generic device to map ioport for
259 * @port: Port to map
260 * @nr: Number of ports to map
262 * Managed ioport_map(). Map is automatically unmapped on driver
263 * detach.
265 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
266 unsigned int nr)
268 void __iomem **ptr, *addr;
270 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
271 if (!ptr)
272 return NULL;
274 addr = ioport_map(port, nr);
275 if (addr) {
276 *ptr = addr;
277 devres_add(dev, ptr);
278 } else
279 devres_free(ptr);
281 return addr;
283 EXPORT_SYMBOL(devm_ioport_map);
286 * devm_ioport_unmap - Managed ioport_unmap()
287 * @dev: Generic device to unmap for
288 * @addr: Address to unmap
290 * Managed ioport_unmap(). @addr must have been mapped using
291 * devm_ioport_map().
293 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
295 ioport_unmap(addr);
296 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
297 devm_ioport_map_match, (__force void *)addr));
299 EXPORT_SYMBOL(devm_ioport_unmap);
300 #endif /* CONFIG_HAS_IOPORT_MAP */
302 #ifdef CONFIG_PCI
304 * PCI iomap devres
306 #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS
308 struct pcim_iomap_devres {
309 void __iomem *table[PCIM_IOMAP_MAX];
312 static void pcim_iomap_release(struct device *gendev, void *res)
314 struct pci_dev *dev = to_pci_dev(gendev);
315 struct pcim_iomap_devres *this = res;
316 int i;
318 for (i = 0; i < PCIM_IOMAP_MAX; i++)
319 if (this->table[i])
320 pci_iounmap(dev, this->table[i]);
324 * pcim_iomap_table - access iomap allocation table
325 * @pdev: PCI device to access iomap table for
327 * Access iomap allocation table for @dev. If iomap table doesn't
328 * exist and @pdev is managed, it will be allocated. All iomaps
329 * recorded in the iomap table are automatically unmapped on driver
330 * detach.
332 * This function might sleep when the table is first allocated but can
333 * be safely called without context and guaranteed to succed once
334 * allocated.
336 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
338 struct pcim_iomap_devres *dr, *new_dr;
340 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
341 if (dr)
342 return dr->table;
344 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
345 if (!new_dr)
346 return NULL;
347 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
348 return dr->table;
350 EXPORT_SYMBOL(pcim_iomap_table);
353 * pcim_iomap - Managed pcim_iomap()
354 * @pdev: PCI device to iomap for
355 * @bar: BAR to iomap
356 * @maxlen: Maximum length of iomap
358 * Managed pci_iomap(). Map is automatically unmapped on driver
359 * detach.
361 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
363 void __iomem **tbl;
365 BUG_ON(bar >= PCIM_IOMAP_MAX);
367 tbl = (void __iomem **)pcim_iomap_table(pdev);
368 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
369 return NULL;
371 tbl[bar] = pci_iomap(pdev, bar, maxlen);
372 return tbl[bar];
374 EXPORT_SYMBOL(pcim_iomap);
377 * pcim_iounmap - Managed pci_iounmap()
378 * @pdev: PCI device to iounmap for
379 * @addr: Address to unmap
381 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
383 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
385 void __iomem **tbl;
386 int i;
388 pci_iounmap(pdev, addr);
390 tbl = (void __iomem **)pcim_iomap_table(pdev);
391 BUG_ON(!tbl);
393 for (i = 0; i < PCIM_IOMAP_MAX; i++)
394 if (tbl[i] == addr) {
395 tbl[i] = NULL;
396 return;
398 WARN_ON(1);
400 EXPORT_SYMBOL(pcim_iounmap);
403 * pcim_iomap_regions - Request and iomap PCI BARs
404 * @pdev: PCI device to map IO resources for
405 * @mask: Mask of BARs to request and iomap
406 * @name: Name used when requesting regions
408 * Request and iomap regions specified by @mask.
410 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
412 void __iomem * const *iomap;
413 int i, rc;
415 iomap = pcim_iomap_table(pdev);
416 if (!iomap)
417 return -ENOMEM;
419 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
420 unsigned long len;
422 if (!(mask & (1 << i)))
423 continue;
425 rc = -EINVAL;
426 len = pci_resource_len(pdev, i);
427 if (!len)
428 goto err_inval;
430 rc = pci_request_region(pdev, i, name);
431 if (rc)
432 goto err_inval;
434 rc = -ENOMEM;
435 if (!pcim_iomap(pdev, i, 0))
436 goto err_region;
439 return 0;
441 err_region:
442 pci_release_region(pdev, i);
443 err_inval:
444 while (--i >= 0) {
445 if (!(mask & (1 << i)))
446 continue;
447 pcim_iounmap(pdev, iomap[i]);
448 pci_release_region(pdev, i);
451 return rc;
453 EXPORT_SYMBOL(pcim_iomap_regions);
456 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
457 * @pdev: PCI device to map IO resources for
458 * @mask: Mask of BARs to iomap
459 * @name: Name used when requesting regions
461 * Request all PCI BARs and iomap regions specified by @mask.
463 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
464 const char *name)
466 int request_mask = ((1 << 6) - 1) & ~mask;
467 int rc;
469 rc = pci_request_selected_regions(pdev, request_mask, name);
470 if (rc)
471 return rc;
473 rc = pcim_iomap_regions(pdev, mask, name);
474 if (rc)
475 pci_release_selected_regions(pdev, request_mask);
476 return rc;
478 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
481 * pcim_iounmap_regions - Unmap and release PCI BARs
482 * @pdev: PCI device to map IO resources for
483 * @mask: Mask of BARs to unmap and release
485 * Unmap and release regions specified by @mask.
487 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
489 void __iomem * const *iomap;
490 int i;
492 iomap = pcim_iomap_table(pdev);
493 if (!iomap)
494 return;
496 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
497 if (!(mask & (1 << i)))
498 continue;
500 pcim_iounmap(pdev, iomap[i]);
501 pci_release_region(pdev, i);
504 EXPORT_SYMBOL(pcim_iounmap_regions);
505 #endif /* CONFIG_PCI */