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
,
165 const struct resource
*res
)
167 resource_size_t size
;
169 void __iomem
*dest_ptr
;
173 if (!res
|| resource_type(res
) != IORESOURCE_MEM
) {
174 dev_err(dev
, "invalid resource\n");
175 return IOMEM_ERR_PTR(-EINVAL
);
178 size
= resource_size(res
);
179 name
= res
->name
?: dev_name(dev
);
181 if (!devm_request_mem_region(dev
, res
->start
, size
, name
)) {
182 dev_err(dev
, "can't request region for resource %pR\n", res
);
183 return IOMEM_ERR_PTR(-EBUSY
);
186 dest_ptr
= devm_ioremap(dev
, res
->start
, size
);
188 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
189 devm_release_mem_region(dev
, res
->start
, size
);
190 dest_ptr
= IOMEM_ERR_PTR(-ENOMEM
);
195 EXPORT_SYMBOL(devm_ioremap_resource
);
197 #ifdef CONFIG_HAS_IOPORT_MAP
199 * Generic iomap devres
201 static void devm_ioport_map_release(struct device
*dev
, void *res
)
203 ioport_unmap(*(void __iomem
**)res
);
206 static int devm_ioport_map_match(struct device
*dev
, void *res
,
209 return *(void **)res
== match_data
;
213 * devm_ioport_map - Managed ioport_map()
214 * @dev: Generic device to map ioport for
216 * @nr: Number of ports to map
218 * Managed ioport_map(). Map is automatically unmapped on driver
221 void __iomem
*devm_ioport_map(struct device
*dev
, unsigned long port
,
224 void __iomem
**ptr
, *addr
;
226 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
230 addr
= ioport_map(port
, nr
);
233 devres_add(dev
, ptr
);
239 EXPORT_SYMBOL(devm_ioport_map
);
242 * devm_ioport_unmap - Managed ioport_unmap()
243 * @dev: Generic device to unmap for
244 * @addr: Address to unmap
246 * Managed ioport_unmap(). @addr must have been mapped using
249 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
252 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
253 devm_ioport_map_match
, (__force
void *)addr
));
255 EXPORT_SYMBOL(devm_ioport_unmap
);
256 #endif /* CONFIG_HAS_IOPORT_MAP */
262 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
264 struct pcim_iomap_devres
{
265 void __iomem
*table
[PCIM_IOMAP_MAX
];
268 static void pcim_iomap_release(struct device
*gendev
, void *res
)
270 struct pci_dev
*dev
= to_pci_dev(gendev
);
271 struct pcim_iomap_devres
*this = res
;
274 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
276 pci_iounmap(dev
, this->table
[i
]);
280 * pcim_iomap_table - access iomap allocation table
281 * @pdev: PCI device to access iomap table for
283 * Access iomap allocation table for @dev. If iomap table doesn't
284 * exist and @pdev is managed, it will be allocated. All iomaps
285 * recorded in the iomap table are automatically unmapped on driver
288 * This function might sleep when the table is first allocated but can
289 * be safely called without context and guaranteed to succed once
292 void __iomem
* const *pcim_iomap_table(struct pci_dev
*pdev
)
294 struct pcim_iomap_devres
*dr
, *new_dr
;
296 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
300 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
303 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
306 EXPORT_SYMBOL(pcim_iomap_table
);
309 * pcim_iomap - Managed pcim_iomap()
310 * @pdev: PCI device to iomap for
312 * @maxlen: Maximum length of iomap
314 * Managed pci_iomap(). Map is automatically unmapped on driver
317 void __iomem
*pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
321 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
323 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
324 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
327 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
330 EXPORT_SYMBOL(pcim_iomap
);
333 * pcim_iounmap - Managed pci_iounmap()
334 * @pdev: PCI device to iounmap for
335 * @addr: Address to unmap
337 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
339 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
344 pci_iounmap(pdev
, addr
);
346 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
349 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
350 if (tbl
[i
] == addr
) {
356 EXPORT_SYMBOL(pcim_iounmap
);
359 * pcim_iomap_regions - Request and iomap PCI BARs
360 * @pdev: PCI device to map IO resources for
361 * @mask: Mask of BARs to request and iomap
362 * @name: Name used when requesting regions
364 * Request and iomap regions specified by @mask.
366 int pcim_iomap_regions(struct pci_dev
*pdev
, int mask
, const char *name
)
368 void __iomem
* const *iomap
;
371 iomap
= pcim_iomap_table(pdev
);
375 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
378 if (!(mask
& (1 << i
)))
382 len
= pci_resource_len(pdev
, i
);
386 rc
= pci_request_region(pdev
, i
, name
);
391 if (!pcim_iomap(pdev
, i
, 0))
398 pci_release_region(pdev
, i
);
401 if (!(mask
& (1 << i
)))
403 pcim_iounmap(pdev
, iomap
[i
]);
404 pci_release_region(pdev
, i
);
409 EXPORT_SYMBOL(pcim_iomap_regions
);
412 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
413 * @pdev: PCI device to map IO resources for
414 * @mask: Mask of BARs to iomap
415 * @name: Name used when requesting regions
417 * Request all PCI BARs and iomap regions specified by @mask.
419 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, int mask
,
422 int request_mask
= ((1 << 6) - 1) & ~mask
;
425 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
429 rc
= pcim_iomap_regions(pdev
, mask
, name
);
431 pci_release_selected_regions(pdev
, request_mask
);
434 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
437 * pcim_iounmap_regions - Unmap and release PCI BARs
438 * @pdev: PCI device to map IO resources for
439 * @mask: Mask of BARs to unmap and release
441 * Unmap and release regions specified by @mask.
443 void pcim_iounmap_regions(struct pci_dev
*pdev
, int mask
)
445 void __iomem
* const *iomap
;
448 iomap
= pcim_iomap_table(pdev
);
452 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++) {
453 if (!(mask
& (1 << i
)))
456 pcim_iounmap(pdev
, iomap
[i
]);
457 pci_release_region(pdev
, i
);
460 EXPORT_SYMBOL(pcim_iounmap_regions
);
461 #endif /* CONFIG_PCI */