1 // SPDX-License-Identifier: GPL-2.0
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
;
19 * devm_ioremap - Managed ioremap()
20 * @dev: Generic device to remap IO address for
21 * @offset: Resource address to map
24 * Managed ioremap(). Map is automatically unmapped on driver detach.
26 void __iomem
*devm_ioremap(struct device
*dev
, resource_size_t offset
,
29 void __iomem
**ptr
, *addr
;
31 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
35 addr
= ioremap(offset
, size
);
44 EXPORT_SYMBOL(devm_ioremap
);
47 * devm_ioremap_uc - Managed ioremap_uc()
48 * @dev: Generic device to remap IO address for
49 * @offset: Resource address to map
52 * Managed ioremap_uc(). Map is automatically unmapped on driver detach.
54 void __iomem
*devm_ioremap_uc(struct device
*dev
, resource_size_t offset
,
57 void __iomem
**ptr
, *addr
;
59 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
63 addr
= ioremap_uc(offset
, size
);
72 EXPORT_SYMBOL(devm_ioremap_uc
);
76 * devm_ioremap_nocache - Managed ioremap_nocache()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to map
81 * Managed ioremap_nocache(). Map is automatically unmapped on driver
84 void __iomem
*devm_ioremap_nocache(struct device
*dev
, resource_size_t offset
,
87 void __iomem
**ptr
, *addr
;
89 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
93 addr
= ioremap_nocache(offset
, size
);
102 EXPORT_SYMBOL(devm_ioremap_nocache
);
105 * devm_ioremap_wc - Managed ioremap_wc()
106 * @dev: Generic device to remap IO address for
107 * @offset: Resource address to map
110 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
112 void __iomem
*devm_ioremap_wc(struct device
*dev
, resource_size_t offset
,
113 resource_size_t size
)
115 void __iomem
**ptr
, *addr
;
117 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
121 addr
= ioremap_wc(offset
, size
);
124 devres_add(dev
, ptr
);
130 EXPORT_SYMBOL(devm_ioremap_wc
);
133 * devm_iounmap - Managed iounmap()
134 * @dev: Generic device to unmap for
135 * @addr: Address to unmap
137 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
139 void devm_iounmap(struct device
*dev
, void __iomem
*addr
)
141 WARN_ON(devres_destroy(dev
, devm_ioremap_release
, devm_ioremap_match
,
142 (__force
void *)addr
));
145 EXPORT_SYMBOL(devm_iounmap
);
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
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
156 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
157 * on failure. Usage example:
159 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
160 * base = devm_ioremap_resource(&pdev->dev, res);
162 * return PTR_ERR(base);
164 void __iomem
*devm_ioremap_resource(struct device
*dev
, struct resource
*res
)
166 resource_size_t size
;
168 void __iomem
*dest_ptr
;
172 if (!res
|| resource_type(res
) != IORESOURCE_MEM
) {
173 dev_err(dev
, "invalid resource\n");
174 return IOMEM_ERR_PTR(-EINVAL
);
177 size
= resource_size(res
);
178 name
= res
->name
?: dev_name(dev
);
180 if (!devm_request_mem_region(dev
, res
->start
, size
, name
)) {
181 dev_err(dev
, "can't request region for resource %pR\n", res
);
182 return IOMEM_ERR_PTR(-EBUSY
);
185 dest_ptr
= devm_ioremap(dev
, res
->start
, size
);
187 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
188 devm_release_mem_region(dev
, res
->start
, size
);
189 dest_ptr
= IOMEM_ERR_PTR(-ENOMEM
);
194 EXPORT_SYMBOL(devm_ioremap_resource
);
196 #ifdef CONFIG_HAS_IOPORT_MAP
198 * Generic iomap devres
200 static void devm_ioport_map_release(struct device
*dev
, void *res
)
202 ioport_unmap(*(void __iomem
**)res
);
205 static int devm_ioport_map_match(struct device
*dev
, void *res
,
208 return *(void **)res
== match_data
;
212 * devm_ioport_map - Managed ioport_map()
213 * @dev: Generic device to map ioport for
215 * @nr: Number of ports to map
217 * Managed ioport_map(). Map is automatically unmapped on driver
220 void __iomem
*devm_ioport_map(struct device
*dev
, unsigned long port
,
223 void __iomem
**ptr
, *addr
;
225 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
229 addr
= ioport_map(port
, nr
);
232 devres_add(dev
, ptr
);
238 EXPORT_SYMBOL(devm_ioport_map
);
241 * devm_ioport_unmap - Managed ioport_unmap()
242 * @dev: Generic device to unmap for
243 * @addr: Address to unmap
245 * Managed ioport_unmap(). @addr must have been mapped using
248 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
251 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
252 devm_ioport_map_match
, (__force
void *)addr
));
254 EXPORT_SYMBOL(devm_ioport_unmap
);
255 #endif /* CONFIG_HAS_IOPORT_MAP */
261 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
263 struct pcim_iomap_devres
{
264 void __iomem
*table
[PCIM_IOMAP_MAX
];
267 static void pcim_iomap_release(struct device
*gendev
, void *res
)
269 struct pci_dev
*dev
= to_pci_dev(gendev
);
270 struct pcim_iomap_devres
*this = res
;
273 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
275 pci_iounmap(dev
, this->table
[i
]);
279 * pcim_iomap_table - access iomap allocation table
280 * @pdev: PCI device to access iomap table for
282 * Access iomap allocation table for @dev. If iomap table doesn't
283 * exist and @pdev is managed, it will be allocated. All iomaps
284 * recorded in the iomap table are automatically unmapped on driver
287 * This function might sleep when the table is first allocated but can
288 * be safely called without context and guaranteed to succed once
291 void __iomem
* const *pcim_iomap_table(struct pci_dev
*pdev
)
293 struct pcim_iomap_devres
*dr
, *new_dr
;
295 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
299 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
302 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
305 EXPORT_SYMBOL(pcim_iomap_table
);
308 * pcim_iomap - Managed pcim_iomap()
309 * @pdev: PCI device to iomap for
311 * @maxlen: Maximum length of iomap
313 * Managed pci_iomap(). Map is automatically unmapped on driver
316 void __iomem
*pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
320 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
322 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
323 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
326 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
329 EXPORT_SYMBOL(pcim_iomap
);
332 * pcim_iounmap - Managed pci_iounmap()
333 * @pdev: PCI device to iounmap for
334 * @addr: Address to unmap
336 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
338 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
343 pci_iounmap(pdev
, addr
);
345 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
348 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
349 if (tbl
[i
] == addr
) {
355 EXPORT_SYMBOL(pcim_iounmap
);
358 * pcim_iomap_regions - Request and iomap PCI BARs
359 * @pdev: PCI device to map IO resources for
360 * @mask: Mask of BARs to request and iomap
361 * @name: Name used when requesting regions
363 * Request and iomap regions specified by @mask.
365 int pcim_iomap_regions(struct pci_dev
*pdev
, int mask
, const char *name
)
367 void __iomem
* const *iomap
;
370 iomap
= pcim_iomap_table(pdev
);
374 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
377 if (!(mask
& (1 << i
)))
381 len
= pci_resource_len(pdev
, i
);
385 rc
= pci_request_region(pdev
, i
, name
);
390 if (!pcim_iomap(pdev
, i
, 0))
397 pci_release_region(pdev
, i
);
400 if (!(mask
& (1 << i
)))
402 pcim_iounmap(pdev
, iomap
[i
]);
403 pci_release_region(pdev
, i
);
408 EXPORT_SYMBOL(pcim_iomap_regions
);
411 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
412 * @pdev: PCI device to map IO resources for
413 * @mask: Mask of BARs to iomap
414 * @name: Name used when requesting regions
416 * Request all PCI BARs and iomap regions specified by @mask.
418 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, int mask
,
421 int request_mask
= ((1 << 6) - 1) & ~mask
;
424 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
428 rc
= pcim_iomap_regions(pdev
, mask
, name
);
430 pci_release_selected_regions(pdev
, request_mask
);
433 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
436 * pcim_iounmap_regions - Unmap and release PCI BARs
437 * @pdev: PCI device to map IO resources for
438 * @mask: Mask of BARs to unmap and release
440 * Unmap and release regions specified by @mask.
442 void pcim_iounmap_regions(struct pci_dev
*pdev
, int mask
)
444 void __iomem
* const *iomap
;
447 iomap
= pcim_iomap_table(pdev
);
451 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++) {
452 if (!(mask
& (1 << i
)))
455 pcim_iounmap(pdev
, iomap
[i
]);
456 pci_release_region(pdev
, i
);
459 EXPORT_SYMBOL(pcim_iounmap_regions
);
460 #endif /* CONFIG_PCI */