1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/export.h>
7 #include <linux/of_address.h>
9 enum devm_ioremap_type {
15 void devm_ioremap_release(struct device *dev, void *res)
17 iounmap(*(void __iomem **)res);
20 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
22 return *(void **)res == match_data;
25 static void __iomem *__devm_ioremap(struct device *dev, resource_size_t offset,
27 enum devm_ioremap_type type)
29 void __iomem **ptr, *addr = NULL;
31 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
37 addr = ioremap(offset, size);
40 addr = ioremap_nocache(offset, size);
43 addr = ioremap_wc(offset, size);
57 * devm_ioremap - Managed ioremap()
58 * @dev: Generic device to remap IO address for
59 * @offset: Resource address to map
62 * Managed ioremap(). Map is automatically unmapped on driver detach.
64 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
67 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
69 EXPORT_SYMBOL(devm_ioremap);
72 * devm_ioremap_nocache - Managed ioremap_nocache()
73 * @dev: Generic device to remap IO address for
74 * @offset: Resource address to map
77 * Managed ioremap_nocache(). Map is automatically unmapped on driver
80 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
83 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NC);
85 EXPORT_SYMBOL(devm_ioremap_nocache);
88 * devm_ioremap_wc - Managed ioremap_wc()
89 * @dev: Generic device to remap IO address for
90 * @offset: Resource address to map
93 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
95 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
98 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
100 EXPORT_SYMBOL(devm_ioremap_wc);
103 * devm_iounmap - Managed iounmap()
104 * @dev: Generic device to unmap for
105 * @addr: Address to unmap
107 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
109 void devm_iounmap(struct device *dev, void __iomem *addr)
111 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
112 (__force void *)addr));
115 EXPORT_SYMBOL(devm_iounmap);
117 static void __iomem *
118 __devm_ioremap_resource(struct device *dev, const struct resource *res,
119 enum devm_ioremap_type type)
121 resource_size_t size;
122 void __iomem *dest_ptr;
126 if (!res || resource_type(res) != IORESOURCE_MEM) {
127 dev_err(dev, "invalid resource\n");
128 return IOMEM_ERR_PTR(-EINVAL);
131 size = resource_size(res);
133 if (!devm_request_mem_region(dev, res->start, size, dev_name(dev))) {
134 dev_err(dev, "can't request region for resource %pR\n", res);
135 return IOMEM_ERR_PTR(-EBUSY);
138 dest_ptr = __devm_ioremap(dev, res->start, size, type);
140 dev_err(dev, "ioremap failed for resource %pR\n", res);
141 devm_release_mem_region(dev, res->start, size);
142 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
149 * devm_ioremap_resource() - check, request region, and ioremap resource
150 * @dev: generic device to handle the resource for
151 * @res: resource to be handled
153 * Checks that a resource is a valid memory region, requests the memory
154 * region and ioremaps it. All operations are managed and will be undone
157 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
158 * on failure. Usage example:
160 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
161 * base = devm_ioremap_resource(&pdev->dev, res);
163 * return PTR_ERR(base);
165 void __iomem *devm_ioremap_resource(struct device *dev,
166 const struct resource *res)
168 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP);
170 EXPORT_SYMBOL(devm_ioremap_resource);
173 * devm_ioremap_resource_wc() - write-combined variant of
174 * devm_ioremap_resource()
175 * @dev: generic device to handle the resource for
176 * @res: resource to be handled
178 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
179 * on failure. Usage example:
181 void __iomem *devm_ioremap_resource_wc(struct device *dev,
182 const struct resource *res)
184 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP_WC);
188 * devm_of_iomap - Requests a resource and maps the memory mapped IO
189 * for a given device_node managed by a given device
191 * Checks that a resource is a valid memory region, requests the memory
192 * region and ioremaps it. All operations are managed and will be undone
193 * on driver detach of the device.
195 * This is to be used when a device requests/maps resources described
196 * by other device tree nodes (children or otherwise).
198 * @dev: The device "managing" the resource
199 * @node: The device-tree node where the resource resides
200 * @index: index of the MMIO range in the "reg" property
201 * @size: Returns the size of the resource (pass NULL if not needed)
202 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
203 * error code on failure. Usage example:
205 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
207 * return PTR_ERR(base);
209 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
210 resource_size_t *size)
214 if (of_address_to_resource(node, index, &res))
215 return IOMEM_ERR_PTR(-EINVAL);
217 *size = resource_size(&res);
218 return devm_ioremap_resource(dev, &res);
220 EXPORT_SYMBOL(devm_of_iomap);
222 #ifdef CONFIG_HAS_IOPORT_MAP
224 * Generic iomap devres
226 static void devm_ioport_map_release(struct device *dev, void *res)
228 ioport_unmap(*(void __iomem **)res);
231 static int devm_ioport_map_match(struct device *dev, void *res,
234 return *(void **)res == match_data;
238 * devm_ioport_map - Managed ioport_map()
239 * @dev: Generic device to map ioport for
241 * @nr: Number of ports to map
243 * Managed ioport_map(). Map is automatically unmapped on driver
246 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
249 void __iomem **ptr, *addr;
251 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
255 addr = ioport_map(port, nr);
258 devres_add(dev, ptr);
264 EXPORT_SYMBOL(devm_ioport_map);
267 * devm_ioport_unmap - Managed ioport_unmap()
268 * @dev: Generic device to unmap for
269 * @addr: Address to unmap
271 * Managed ioport_unmap(). @addr must have been mapped using
274 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
277 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
278 devm_ioport_map_match, (__force void *)addr));
280 EXPORT_SYMBOL(devm_ioport_unmap);
281 #endif /* CONFIG_HAS_IOPORT_MAP */
287 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
289 struct pcim_iomap_devres {
290 void __iomem *table[PCIM_IOMAP_MAX];
293 static void pcim_iomap_release(struct device *gendev, void *res)
295 struct pci_dev *dev = to_pci_dev(gendev);
296 struct pcim_iomap_devres *this = res;
299 for (i = 0; i < PCIM_IOMAP_MAX; i++)
301 pci_iounmap(dev, this->table[i]);
305 * pcim_iomap_table - access iomap allocation table
306 * @pdev: PCI device to access iomap table for
308 * Access iomap allocation table for @dev. If iomap table doesn't
309 * exist and @pdev is managed, it will be allocated. All iomaps
310 * recorded in the iomap table are automatically unmapped on driver
313 * This function might sleep when the table is first allocated but can
314 * be safely called without context and guaranteed to succed once
317 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
319 struct pcim_iomap_devres *dr, *new_dr;
321 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
325 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
328 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
331 EXPORT_SYMBOL(pcim_iomap_table);
334 * pcim_iomap - Managed pcim_iomap()
335 * @pdev: PCI device to iomap for
337 * @maxlen: Maximum length of iomap
339 * Managed pci_iomap(). Map is automatically unmapped on driver
342 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
346 BUG_ON(bar >= PCIM_IOMAP_MAX);
348 tbl = (void __iomem **)pcim_iomap_table(pdev);
349 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
352 tbl[bar] = pci_iomap(pdev, bar, maxlen);
355 EXPORT_SYMBOL(pcim_iomap);
358 * pcim_iounmap - Managed pci_iounmap()
359 * @pdev: PCI device to iounmap for
360 * @addr: Address to unmap
362 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
364 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
369 pci_iounmap(pdev, addr);
371 tbl = (void __iomem **)pcim_iomap_table(pdev);
374 for (i = 0; i < PCIM_IOMAP_MAX; i++)
375 if (tbl[i] == addr) {
381 EXPORT_SYMBOL(pcim_iounmap);
384 * pcim_iomap_regions - Request and iomap PCI BARs
385 * @pdev: PCI device to map IO resources for
386 * @mask: Mask of BARs to request and iomap
387 * @name: Name used when requesting regions
389 * Request and iomap regions specified by @mask.
391 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
393 void __iomem * const *iomap;
396 iomap = pcim_iomap_table(pdev);
400 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
403 if (!(mask & (1 << i)))
407 len = pci_resource_len(pdev, i);
411 rc = pci_request_region(pdev, i, name);
416 if (!pcim_iomap(pdev, i, 0))
423 pci_release_region(pdev, i);
426 if (!(mask & (1 << i)))
428 pcim_iounmap(pdev, iomap[i]);
429 pci_release_region(pdev, i);
434 EXPORT_SYMBOL(pcim_iomap_regions);
437 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
438 * @pdev: PCI device to map IO resources for
439 * @mask: Mask of BARs to iomap
440 * @name: Name used when requesting regions
442 * Request all PCI BARs and iomap regions specified by @mask.
444 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
447 int request_mask = ((1 << 6) - 1) & ~mask;
450 rc = pci_request_selected_regions(pdev, request_mask, name);
454 rc = pcim_iomap_regions(pdev, mask, name);
456 pci_release_selected_regions(pdev, request_mask);
459 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
462 * pcim_iounmap_regions - Unmap and release PCI BARs
463 * @pdev: PCI device to map IO resources for
464 * @mask: Mask of BARs to unmap and release
466 * Unmap and release regions specified by @mask.
468 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
470 void __iomem * const *iomap;
473 iomap = pcim_iomap_table(pdev);
477 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
478 if (!(mask & (1 << i)))
481 pcim_iounmap(pdev, iomap[i]);
482 pci_release_region(pdev, i);
485 EXPORT_SYMBOL(pcim_iounmap_regions);
486 #endif /* CONFIG_PCI */