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_nocache - Managed ioremap_nocache()
48 * @dev: Generic device to remap IO address for
49 * @offset: Resource address to map
52 * Managed ioremap_nocache(). Map is automatically unmapped on driver
55 void __iomem
*devm_ioremap_nocache(struct device
*dev
, resource_size_t offset
,
58 void __iomem
**ptr
, *addr
;
60 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
64 addr
= ioremap_nocache(offset
, size
);
73 EXPORT_SYMBOL(devm_ioremap_nocache
);
76 * devm_ioremap_wc - Managed ioremap_wc()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to 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
,
86 void __iomem
**ptr
, *addr
;
88 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
92 addr
= ioremap_wc(offset
, size
);
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
));
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
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);
133 * return PTR_ERR(base);
135 void __iomem
*devm_ioremap_resource(struct device
*dev
,
136 const struct resource
*res
)
138 resource_size_t size
;
140 void __iomem
*dest_ptr
;
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
);
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
);
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
,
180 return *(void **)res
== match_data
;
184 * devm_ioport_map - Managed ioport_map()
185 * @dev: Generic device to map ioport for
187 * @nr: Number of ports to map
189 * Managed ioport_map(). Map is automatically unmapped on driver
192 void __iomem
*devm_ioport_map(struct device
*dev
, unsigned long port
,
195 void __iomem
**ptr
, *addr
;
197 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
201 addr
= ioport_map(port
, nr
);
204 devres_add(dev
, ptr
);
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
220 void devm_ioport_unmap(struct device
*dev
, void __iomem
*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 */
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
;
245 for (i
= 0; i
< PCIM_IOMAP_MAX
; 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
259 * This function might sleep when the table is first allocated but can
260 * be safely called without context and guaranteed to succed once
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
);
271 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
274 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
277 EXPORT_SYMBOL(pcim_iomap_table
);
280 * pcim_iomap - Managed pcim_iomap()
281 * @pdev: PCI device to iomap for
283 * @maxlen: Maximum length of iomap
285 * Managed pci_iomap(). Map is automatically unmapped on driver
288 void __iomem
*pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
292 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
294 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
295 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
298 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
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
)
315 pci_iounmap(pdev
, addr
);
317 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
320 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
321 if (tbl
[i
] == addr
) {
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
;
342 iomap
= pcim_iomap_table(pdev
);
346 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
349 if (!(mask
& (1 << i
)))
353 len
= pci_resource_len(pdev
, i
);
357 rc
= pci_request_region(pdev
, i
, name
);
362 if (!pcim_iomap(pdev
, i
, 0))
369 pci_release_region(pdev
, i
);
372 if (!(mask
& (1 << i
)))
374 pcim_iounmap(pdev
, iomap
[i
]);
375 pci_release_region(pdev
, i
);
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
,
393 int request_mask
= ((1 << 6) - 1) & ~mask
;
396 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
400 rc
= pcim_iomap_regions(pdev
, mask
, name
);
402 pci_release_selected_regions(pdev
, request_mask
);
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
;
419 iomap
= pcim_iomap_table(pdev
);
423 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++) {
424 if (!(mask
& (1 << i
)))
427 pcim_iounmap(pdev
, iomap
[i
]);
428 pci_release_region(pdev
, i
);
431 EXPORT_SYMBOL(pcim_iounmap_regions
);
432 #endif /* CONFIG_PCI */