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(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
38 addr
= ioremap(offset
, size
);
41 addr
= ioremap_uc(offset
, size
);
44 addr
= ioremap_wc(offset
, size
);
47 addr
= ioremap_np(offset
, size
);
61 * devm_ioremap - Managed ioremap()
62 * @dev: Generic device to remap IO address for
63 * @offset: Resource address to map
66 * Managed ioremap(). Map is automatically unmapped on driver detach.
68 void __iomem
*devm_ioremap(struct device
*dev
, resource_size_t offset
,
71 return __devm_ioremap(dev
, offset
, size
, DEVM_IOREMAP
);
73 EXPORT_SYMBOL(devm_ioremap
);
76 * devm_ioremap_uc - Managed ioremap_uc()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to map
81 * Managed ioremap_uc(). Map is automatically unmapped on driver detach.
83 void __iomem
*devm_ioremap_uc(struct device
*dev
, resource_size_t offset
,
86 return __devm_ioremap(dev
, offset
, size
, DEVM_IOREMAP_UC
);
88 EXPORT_SYMBOL_GPL(devm_ioremap_uc
);
91 * devm_ioremap_wc - Managed ioremap_wc()
92 * @dev: Generic device to remap IO address for
93 * @offset: Resource address to map
96 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
98 void __iomem
*devm_ioremap_wc(struct device
*dev
, resource_size_t offset
,
101 return __devm_ioremap(dev
, offset
, size
, DEVM_IOREMAP_WC
);
103 EXPORT_SYMBOL(devm_ioremap_wc
);
106 * devm_ioremap_np - Managed ioremap_np()
107 * @dev: Generic device to remap IO address for
108 * @offset: Resource address to map
111 * Managed ioremap_np(). Map is automatically unmapped on driver detach.
113 void __iomem
*devm_ioremap_np(struct device
*dev
, resource_size_t offset
,
114 resource_size_t size
)
116 return __devm_ioremap(dev
, offset
, size
, DEVM_IOREMAP_NP
);
118 EXPORT_SYMBOL(devm_ioremap_np
);
121 * devm_iounmap - Managed iounmap()
122 * @dev: Generic device to unmap for
123 * @addr: Address to unmap
125 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
127 void devm_iounmap(struct device
*dev
, void __iomem
*addr
)
129 WARN_ON(devres_destroy(dev
, devm_ioremap_release
, devm_ioremap_match
,
130 (__force
void *)addr
));
133 EXPORT_SYMBOL(devm_iounmap
);
135 static void __iomem
*
136 __devm_ioremap_resource(struct device
*dev
, const struct resource
*res
,
137 enum devm_ioremap_type type
)
139 resource_size_t size
;
140 void __iomem
*dest_ptr
;
145 if (!res
|| resource_type(res
) != IORESOURCE_MEM
) {
146 dev_err(dev
, "invalid resource\n");
147 return IOMEM_ERR_PTR(-EINVAL
);
150 if (type
== DEVM_IOREMAP
&& res
->flags
& IORESOURCE_MEM_NONPOSTED
)
151 type
= DEVM_IOREMAP_NP
;
153 size
= resource_size(res
);
156 pretty_name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s %s",
157 dev_name(dev
), res
->name
);
159 pretty_name
= devm_kstrdup(dev
, dev_name(dev
), GFP_KERNEL
);
161 dev_err(dev
, "can't generate pretty name for resource %pR\n", res
);
162 return IOMEM_ERR_PTR(-ENOMEM
);
165 if (!devm_request_mem_region(dev
, res
->start
, size
, pretty_name
)) {
166 dev_err(dev
, "can't request region for resource %pR\n", res
);
167 return IOMEM_ERR_PTR(-EBUSY
);
170 dest_ptr
= __devm_ioremap(dev
, res
->start
, size
, type
);
172 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
173 devm_release_mem_region(dev
, res
->start
, size
);
174 dest_ptr
= IOMEM_ERR_PTR(-ENOMEM
);
181 * devm_ioremap_resource() - check, request region, and ioremap resource
182 * @dev: generic device to handle the resource for
183 * @res: resource to be handled
185 * Checks that a resource is a valid memory region, requests the memory
186 * region and ioremaps it. All operations are managed and will be undone
191 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192 * base = devm_ioremap_resource(&pdev->dev, res);
194 * return PTR_ERR(base);
196 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
199 void __iomem
*devm_ioremap_resource(struct device
*dev
,
200 const struct resource
*res
)
202 return __devm_ioremap_resource(dev
, res
, DEVM_IOREMAP
);
204 EXPORT_SYMBOL(devm_ioremap_resource
);
207 * devm_ioremap_resource_wc() - write-combined variant of
208 * devm_ioremap_resource()
209 * @dev: generic device to handle the resource for
210 * @res: resource to be handled
212 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
215 void __iomem
*devm_ioremap_resource_wc(struct device
*dev
,
216 const struct resource
*res
)
218 return __devm_ioremap_resource(dev
, res
, DEVM_IOREMAP_WC
);
222 * devm_of_iomap - Requests a resource and maps the memory mapped IO
223 * for a given device_node managed by a given device
225 * Checks that a resource is a valid memory region, requests the memory
226 * region and ioremaps it. All operations are managed and will be undone
227 * on driver detach of the device.
229 * This is to be used when a device requests/maps resources described
230 * by other device tree nodes (children or otherwise).
232 * @dev: The device "managing" the resource
233 * @node: The device-tree node where the resource resides
234 * @index: index of the MMIO range in the "reg" property
235 * @size: Returns the size of the resource (pass NULL if not needed)
239 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
241 * return PTR_ERR(base);
243 * Please Note: This is not a one-to-one replacement for of_iomap() because the
244 * of_iomap() function does not track whether the region is already mapped. If
245 * two drivers try to map the same memory, the of_iomap() function will succeed
246 * but the devm_of_iomap() function will return -EBUSY.
248 * Return: a pointer to the requested and mapped memory or an ERR_PTR() encoded
249 * error code on failure.
251 void __iomem
*devm_of_iomap(struct device
*dev
, struct device_node
*node
, int index
,
252 resource_size_t
*size
)
256 if (of_address_to_resource(node
, index
, &res
))
257 return IOMEM_ERR_PTR(-EINVAL
);
259 *size
= resource_size(&res
);
260 return devm_ioremap_resource(dev
, &res
);
262 EXPORT_SYMBOL(devm_of_iomap
);
264 #ifdef CONFIG_HAS_IOPORT_MAP
266 * Generic iomap devres
268 static void devm_ioport_map_release(struct device
*dev
, void *res
)
270 ioport_unmap(*(void __iomem
**)res
);
273 static int devm_ioport_map_match(struct device
*dev
, void *res
,
276 return *(void **)res
== match_data
;
280 * devm_ioport_map - Managed ioport_map()
281 * @dev: Generic device to map ioport for
283 * @nr: Number of ports to map
285 * Managed ioport_map(). Map is automatically unmapped on driver
288 * Return: a pointer to the remapped memory or NULL on failure.
290 void __iomem
*devm_ioport_map(struct device
*dev
, unsigned long port
,
293 void __iomem
**ptr
, *addr
;
295 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
299 addr
= ioport_map(port
, nr
);
302 devres_add(dev
, ptr
);
308 EXPORT_SYMBOL(devm_ioport_map
);
311 * devm_ioport_unmap - Managed ioport_unmap()
312 * @dev: Generic device to unmap for
313 * @addr: Address to unmap
315 * Managed ioport_unmap(). @addr must have been mapped using
318 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
321 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
322 devm_ioport_map_match
, (__force
void *)addr
));
324 EXPORT_SYMBOL(devm_ioport_unmap
);
325 #endif /* CONFIG_HAS_IOPORT_MAP */
331 #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS
333 struct pcim_iomap_devres
{
334 void __iomem
*table
[PCIM_IOMAP_MAX
];
337 static void pcim_iomap_release(struct device
*gendev
, void *res
)
339 struct pci_dev
*dev
= to_pci_dev(gendev
);
340 struct pcim_iomap_devres
*this = res
;
343 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
345 pci_iounmap(dev
, this->table
[i
]);
349 * pcim_iomap_table - access iomap allocation table
350 * @pdev: PCI device to access iomap table for
352 * Access iomap allocation table for @dev. If iomap table doesn't
353 * exist and @pdev is managed, it will be allocated. All iomaps
354 * recorded in the iomap table are automatically unmapped on driver
357 * This function might sleep when the table is first allocated but can
358 * be safely called without context and guaranteed to succed once
361 void __iomem
* const *pcim_iomap_table(struct pci_dev
*pdev
)
363 struct pcim_iomap_devres
*dr
, *new_dr
;
365 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
369 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
372 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
375 EXPORT_SYMBOL(pcim_iomap_table
);
378 * pcim_iomap - Managed pcim_iomap()
379 * @pdev: PCI device to iomap for
381 * @maxlen: Maximum length of iomap
383 * Managed pci_iomap(). Map is automatically unmapped on driver
386 void __iomem
*pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
390 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
392 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
393 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
396 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
399 EXPORT_SYMBOL(pcim_iomap
);
402 * pcim_iounmap - Managed pci_iounmap()
403 * @pdev: PCI device to iounmap for
404 * @addr: Address to unmap
406 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
408 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
413 pci_iounmap(pdev
, addr
);
415 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
418 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
419 if (tbl
[i
] == addr
) {
425 EXPORT_SYMBOL(pcim_iounmap
);
428 * pcim_iomap_regions - Request and iomap PCI BARs
429 * @pdev: PCI device to map IO resources for
430 * @mask: Mask of BARs to request and iomap
431 * @name: Name used when requesting regions
433 * Request and iomap regions specified by @mask.
435 int pcim_iomap_regions(struct pci_dev
*pdev
, int mask
, const char *name
)
437 void __iomem
* const *iomap
;
440 iomap
= pcim_iomap_table(pdev
);
444 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
447 if (!(mask
& (1 << i
)))
451 len
= pci_resource_len(pdev
, i
);
455 rc
= pci_request_region(pdev
, i
, name
);
460 if (!pcim_iomap(pdev
, i
, 0))
467 pci_release_region(pdev
, i
);
470 if (!(mask
& (1 << i
)))
472 pcim_iounmap(pdev
, iomap
[i
]);
473 pci_release_region(pdev
, i
);
478 EXPORT_SYMBOL(pcim_iomap_regions
);
481 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
482 * @pdev: PCI device to map IO resources for
483 * @mask: Mask of BARs to iomap
484 * @name: Name used when requesting regions
486 * Request all PCI BARs and iomap regions specified by @mask.
488 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, int mask
,
491 int request_mask
= ((1 << 6) - 1) & ~mask
;
494 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
498 rc
= pcim_iomap_regions(pdev
, mask
, name
);
500 pci_release_selected_regions(pdev
, request_mask
);
503 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
506 * pcim_iounmap_regions - Unmap and release PCI BARs
507 * @pdev: PCI device to map IO resources for
508 * @mask: Mask of BARs to unmap and release
510 * Unmap and release regions specified by @mask.
512 void pcim_iounmap_regions(struct pci_dev
*pdev
, int mask
)
514 void __iomem
* const *iomap
;
517 iomap
= pcim_iomap_table(pdev
);
521 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++) {
522 if (!(mask
& (1 << i
)))
525 pcim_iounmap(pdev
, iomap
[i
]);
526 pci_release_region(pdev
, i
);
529 EXPORT_SYMBOL(pcim_iounmap_regions
);
530 #endif /* CONFIG_PCI */