1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/export.h>
7 #include <linux/of_address.h>
9 enum devm_ioremap_type {
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,
28 enum devm_ioremap_type type)
30 void __iomem **ptr, *addr = NULL;
32 ptr = devres_alloc_node(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL,
39 addr = ioremap(offset, size);
42 addr = ioremap_uc(offset, size);
45 addr = ioremap_wc(offset, size);
48 addr = ioremap_np(offset, size);
62 * devm_ioremap - Managed ioremap()
63 * @dev: Generic device to remap IO address for
64 * @offset: Resource address to map
67 * Managed ioremap(). Map is automatically unmapped on driver detach.
69 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
72 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
74 EXPORT_SYMBOL(devm_ioremap);
77 * devm_ioremap_uc - Managed ioremap_uc()
78 * @dev: Generic device to remap IO address for
79 * @offset: Resource address to map
82 * Managed ioremap_uc(). Map is automatically unmapped on driver detach.
84 void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
87 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_UC);
89 EXPORT_SYMBOL_GPL(devm_ioremap_uc);
92 * devm_ioremap_wc - Managed ioremap_wc()
93 * @dev: Generic device to remap IO address for
94 * @offset: Resource address to map
97 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
99 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
100 resource_size_t size)
102 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
104 EXPORT_SYMBOL(devm_ioremap_wc);
107 * devm_ioremap_np - Managed ioremap_np()
108 * @dev: Generic device to remap IO address for
109 * @offset: Resource address to map
112 * Managed ioremap_np(). Map is automatically unmapped on driver detach.
114 void __iomem *devm_ioremap_np(struct device *dev, resource_size_t offset,
115 resource_size_t size)
117 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NP);
119 EXPORT_SYMBOL(devm_ioremap_np);
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));
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;
146 if (!res || resource_type(res) != IORESOURCE_MEM) {
147 dev_err(dev, "invalid resource\n");
148 return IOMEM_ERR_PTR(-EINVAL);
151 if (type == DEVM_IOREMAP && res->flags & IORESOURCE_MEM_NONPOSTED)
152 type = DEVM_IOREMAP_NP;
154 size = resource_size(res);
157 pretty_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
158 dev_name(dev), res->name);
160 pretty_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
162 dev_err(dev, "can't generate pretty name for resource %pR\n", res);
163 return IOMEM_ERR_PTR(-ENOMEM);
166 if (!devm_request_mem_region(dev, res->start, size, pretty_name)) {
167 dev_err(dev, "can't request region for resource %pR\n", res);
168 return IOMEM_ERR_PTR(-EBUSY);
171 dest_ptr = __devm_ioremap(dev, res->start, size, type);
173 dev_err(dev, "ioremap failed for resource %pR\n", res);
174 devm_release_mem_region(dev, res->start, size);
175 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
182 * devm_ioremap_resource() - check, request region, and ioremap resource
183 * @dev: generic device to handle the resource for
184 * @res: resource to be handled
186 * Checks that a resource is a valid memory region, requests the memory
187 * region and ioremaps it. All operations are managed and will be undone
192 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 * base = devm_ioremap_resource(&pdev->dev, res);
195 * return PTR_ERR(base);
197 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
200 void __iomem *devm_ioremap_resource(struct device *dev,
201 const struct resource *res)
203 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP);
205 EXPORT_SYMBOL(devm_ioremap_resource);
208 * devm_ioremap_resource_wc() - write-combined variant of
209 * devm_ioremap_resource()
210 * @dev: generic device to handle the resource for
211 * @res: resource to be handled
213 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
216 void __iomem *devm_ioremap_resource_wc(struct device *dev,
217 const struct resource *res)
219 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP_WC);
223 * devm_of_iomap - Requests a resource and maps the memory mapped IO
224 * for a given device_node managed by a given device
226 * Checks that a resource is a valid memory region, requests the memory
227 * region and ioremaps it. All operations are managed and will be undone
228 * on driver detach of the device.
230 * This is to be used when a device requests/maps resources described
231 * by other device tree nodes (children or otherwise).
233 * @dev: The device "managing" the resource
234 * @node: The device-tree node where the resource resides
235 * @index: index of the MMIO range in the "reg" property
236 * @size: Returns the size of the resource (pass NULL if not needed)
240 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
242 * return PTR_ERR(base);
244 * Please Note: This is not a one-to-one replacement for of_iomap() because the
245 * of_iomap() function does not track whether the region is already mapped. If
246 * two drivers try to map the same memory, the of_iomap() function will succeed
247 * but the devm_of_iomap() function will return -EBUSY.
249 * Return: a pointer to the requested and mapped memory or an ERR_PTR() encoded
250 * error code on failure.
252 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
253 resource_size_t *size)
257 if (of_address_to_resource(node, index, &res))
258 return IOMEM_ERR_PTR(-EINVAL);
260 *size = resource_size(&res);
261 return devm_ioremap_resource(dev, &res);
263 EXPORT_SYMBOL(devm_of_iomap);
265 #ifdef CONFIG_HAS_IOPORT_MAP
267 * Generic iomap devres
269 static void devm_ioport_map_release(struct device *dev, void *res)
271 ioport_unmap(*(void __iomem **)res);
274 static int devm_ioport_map_match(struct device *dev, void *res,
277 return *(void **)res == match_data;
281 * devm_ioport_map - Managed ioport_map()
282 * @dev: Generic device to map ioport for
284 * @nr: Number of ports to map
286 * Managed ioport_map(). Map is automatically unmapped on driver
289 * Return: a pointer to the remapped memory or NULL on failure.
291 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
294 void __iomem **ptr, *addr;
296 ptr = devres_alloc_node(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL,
301 addr = ioport_map(port, nr);
304 devres_add(dev, ptr);
310 EXPORT_SYMBOL(devm_ioport_map);
313 * devm_ioport_unmap - Managed ioport_unmap()
314 * @dev: Generic device to unmap for
315 * @addr: Address to unmap
317 * Managed ioport_unmap(). @addr must have been mapped using
320 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
323 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
324 devm_ioport_map_match, (__force void *)addr));
326 EXPORT_SYMBOL(devm_ioport_unmap);
327 #endif /* CONFIG_HAS_IOPORT_MAP */
333 #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS
335 struct pcim_iomap_devres {
336 void __iomem *table[PCIM_IOMAP_MAX];
339 static void pcim_iomap_release(struct device *gendev, void *res)
341 struct pci_dev *dev = to_pci_dev(gendev);
342 struct pcim_iomap_devres *this = res;
345 for (i = 0; i < PCIM_IOMAP_MAX; i++)
347 pci_iounmap(dev, this->table[i]);
351 * pcim_iomap_table - access iomap allocation table
352 * @pdev: PCI device to access iomap table for
354 * Access iomap allocation table for @dev. If iomap table doesn't
355 * exist and @pdev is managed, it will be allocated. All iomaps
356 * recorded in the iomap table are automatically unmapped on driver
359 * This function might sleep when the table is first allocated but can
360 * be safely called without context and guaranteed to succeed once
363 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
365 struct pcim_iomap_devres *dr, *new_dr;
367 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
371 new_dr = devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL,
372 dev_to_node(&pdev->dev));
375 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
378 EXPORT_SYMBOL(pcim_iomap_table);
381 * pcim_iomap - Managed pcim_iomap()
382 * @pdev: PCI device to iomap for
384 * @maxlen: Maximum length of iomap
386 * Managed pci_iomap(). Map is automatically unmapped on driver
389 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
393 BUG_ON(bar >= PCIM_IOMAP_MAX);
395 tbl = (void __iomem **)pcim_iomap_table(pdev);
396 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
399 tbl[bar] = pci_iomap(pdev, bar, maxlen);
402 EXPORT_SYMBOL(pcim_iomap);
405 * pcim_iounmap - Managed pci_iounmap()
406 * @pdev: PCI device to iounmap for
407 * @addr: Address to unmap
409 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
411 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
416 pci_iounmap(pdev, addr);
418 tbl = (void __iomem **)pcim_iomap_table(pdev);
421 for (i = 0; i < PCIM_IOMAP_MAX; i++)
422 if (tbl[i] == addr) {
428 EXPORT_SYMBOL(pcim_iounmap);
431 * pcim_iomap_regions - Request and iomap PCI BARs
432 * @pdev: PCI device to map IO resources for
433 * @mask: Mask of BARs to request and iomap
434 * @name: Name used when requesting regions
436 * Request and iomap regions specified by @mask.
438 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
440 void __iomem * const *iomap;
443 iomap = pcim_iomap_table(pdev);
447 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
450 if (!(mask & (1 << i)))
454 len = pci_resource_len(pdev, i);
458 rc = pci_request_region(pdev, i, name);
463 if (!pcim_iomap(pdev, i, 0))
470 pci_release_region(pdev, i);
473 if (!(mask & (1 << i)))
475 pcim_iounmap(pdev, iomap[i]);
476 pci_release_region(pdev, i);
481 EXPORT_SYMBOL(pcim_iomap_regions);
484 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
485 * @pdev: PCI device to map IO resources for
486 * @mask: Mask of BARs to iomap
487 * @name: Name used when requesting regions
489 * Request all PCI BARs and iomap regions specified by @mask.
491 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
494 int request_mask = ((1 << 6) - 1) & ~mask;
497 rc = pci_request_selected_regions(pdev, request_mask, name);
501 rc = pcim_iomap_regions(pdev, mask, name);
503 pci_release_selected_regions(pdev, request_mask);
506 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
509 * pcim_iounmap_regions - Unmap and release PCI BARs
510 * @pdev: PCI device to map IO resources for
511 * @mask: Mask of BARs to unmap and release
513 * Unmap and release regions specified by @mask.
515 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
517 void __iomem * const *iomap;
520 iomap = pcim_iomap_table(pdev);
524 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
525 if (!(mask & (1 << i)))
528 pcim_iounmap(pdev, iomap[i]);
529 pci_release_region(pdev, i);
532 EXPORT_SYMBOL(pcim_iounmap_regions);
533 #endif /* CONFIG_PCI */
535 static void devm_arch_phys_ac_add_release(struct device *dev, void *res)
537 arch_phys_wc_del(*((int *)res));
541 * devm_arch_phys_wc_add - Managed arch_phys_wc_add()
542 * @dev: Managed device
543 * @base: Memory base address
544 * @size: Size of memory range
546 * Adds a WC MTRR using arch_phys_wc_add() and sets up a release callback.
547 * See arch_phys_wc_add() for more information.
549 int devm_arch_phys_wc_add(struct device *dev, unsigned long base, unsigned long size)
554 mtrr = devres_alloc_node(devm_arch_phys_ac_add_release, sizeof(*mtrr), GFP_KERNEL,
559 ret = arch_phys_wc_add(base, size);
566 devres_add(dev, mtrr);
570 EXPORT_SYMBOL(devm_arch_phys_wc_add);
572 struct arch_io_reserve_memtype_wc_devres {
573 resource_size_t start;
574 resource_size_t size;
577 static void devm_arch_io_free_memtype_wc_release(struct device *dev, void *res)
579 const struct arch_io_reserve_memtype_wc_devres *this = res;
581 arch_io_free_memtype_wc(this->start, this->size);
585 * devm_arch_io_reserve_memtype_wc - Managed arch_io_reserve_memtype_wc()
586 * @dev: Managed device
587 * @start: Memory base address
588 * @size: Size of memory range
590 * Reserves a memory range with WC caching using arch_io_reserve_memtype_wc()
591 * and sets up a release callback See arch_io_reserve_memtype_wc() for more
594 int devm_arch_io_reserve_memtype_wc(struct device *dev, resource_size_t start,
595 resource_size_t size)
597 struct arch_io_reserve_memtype_wc_devres *dr;
600 dr = devres_alloc_node(devm_arch_io_free_memtype_wc_release, sizeof(*dr), GFP_KERNEL,
605 ret = arch_io_reserve_memtype_wc(start, size);
617 EXPORT_SYMBOL(devm_arch_io_reserve_memtype_wc);