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_nocache(offset
, size
);
44 addr
= ioremap_uc(offset
, size
);
47 addr
= ioremap_wc(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_nocache - Managed ioremap_nocache()
92 * @dev: Generic device to remap IO address for
93 * @offset: Resource address to map
96 * Managed ioremap_nocache(). Map is automatically unmapped on driver
99 void __iomem
*devm_ioremap_nocache(struct device
*dev
, resource_size_t offset
,
100 resource_size_t size
)
102 return __devm_ioremap(dev
, offset
, size
, DEVM_IOREMAP_NC
);
104 EXPORT_SYMBOL(devm_ioremap_nocache
);
107 * devm_ioremap_wc - Managed ioremap_wc()
108 * @dev: Generic device to remap IO address for
109 * @offset: Resource address to map
112 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
114 void __iomem
*devm_ioremap_wc(struct device
*dev
, resource_size_t offset
,
115 resource_size_t size
)
117 return __devm_ioremap(dev
, offset
, size
, DEVM_IOREMAP_WC
);
119 EXPORT_SYMBOL(devm_ioremap_wc
);
122 * devm_iounmap - Managed iounmap()
123 * @dev: Generic device to unmap for
124 * @addr: Address to unmap
126 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
128 void devm_iounmap(struct device
*dev
, void __iomem
*addr
)
130 WARN_ON(devres_destroy(dev
, devm_ioremap_release
, devm_ioremap_match
,
131 (__force
void *)addr
));
134 EXPORT_SYMBOL(devm_iounmap
);
136 static void __iomem
*
137 __devm_ioremap_resource(struct device
*dev
, const struct resource
*res
,
138 enum devm_ioremap_type type
)
140 resource_size_t size
;
141 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 size
= resource_size(res
);
152 if (!devm_request_mem_region(dev
, res
->start
, size
, dev_name(dev
))) {
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
, type
);
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
);
168 * devm_ioremap_resource() - check, request region, and ioremap resource
169 * @dev: generic device to handle the resource for
170 * @res: resource to be handled
172 * Checks that a resource is a valid memory region, requests the memory
173 * region and ioremaps it. All operations are managed and will be undone
176 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
177 * on failure. Usage example:
179 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 * base = devm_ioremap_resource(&pdev->dev, res);
182 * return PTR_ERR(base);
184 void __iomem
*devm_ioremap_resource(struct device
*dev
,
185 const struct resource
*res
)
187 return __devm_ioremap_resource(dev
, res
, DEVM_IOREMAP
);
189 EXPORT_SYMBOL(devm_ioremap_resource
);
192 * devm_ioremap_resource_wc() - write-combined variant of
193 * devm_ioremap_resource()
194 * @dev: generic device to handle the resource for
195 * @res: resource to be handled
197 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
198 * on failure. Usage example:
200 void __iomem
*devm_ioremap_resource_wc(struct device
*dev
,
201 const struct resource
*res
)
203 return __devm_ioremap_resource(dev
, res
, DEVM_IOREMAP_WC
);
207 * devm_of_iomap - Requests a resource and maps the memory mapped IO
208 * for a given device_node managed by a given device
210 * Checks that a resource is a valid memory region, requests the memory
211 * region and ioremaps it. All operations are managed and will be undone
212 * on driver detach of the device.
214 * This is to be used when a device requests/maps resources described
215 * by other device tree nodes (children or otherwise).
217 * @dev: The device "managing" the resource
218 * @node: The device-tree node where the resource resides
219 * @index: index of the MMIO range in the "reg" property
220 * @size: Returns the size of the resource (pass NULL if not needed)
221 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
222 * error code on failure. Usage example:
224 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
226 * return PTR_ERR(base);
228 void __iomem
*devm_of_iomap(struct device
*dev
, struct device_node
*node
, int index
,
229 resource_size_t
*size
)
233 if (of_address_to_resource(node
, index
, &res
))
234 return IOMEM_ERR_PTR(-EINVAL
);
236 *size
= resource_size(&res
);
237 return devm_ioremap_resource(dev
, &res
);
239 EXPORT_SYMBOL(devm_of_iomap
);
241 #ifdef CONFIG_HAS_IOPORT_MAP
243 * Generic iomap devres
245 static void devm_ioport_map_release(struct device
*dev
, void *res
)
247 ioport_unmap(*(void __iomem
**)res
);
250 static int devm_ioport_map_match(struct device
*dev
, void *res
,
253 return *(void **)res
== match_data
;
257 * devm_ioport_map - Managed ioport_map()
258 * @dev: Generic device to map ioport for
260 * @nr: Number of ports to map
262 * Managed ioport_map(). Map is automatically unmapped on driver
265 void __iomem
*devm_ioport_map(struct device
*dev
, unsigned long port
,
268 void __iomem
**ptr
, *addr
;
270 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
274 addr
= ioport_map(port
, nr
);
277 devres_add(dev
, ptr
);
283 EXPORT_SYMBOL(devm_ioport_map
);
286 * devm_ioport_unmap - Managed ioport_unmap()
287 * @dev: Generic device to unmap for
288 * @addr: Address to unmap
290 * Managed ioport_unmap(). @addr must have been mapped using
293 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
296 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
297 devm_ioport_map_match
, (__force
void *)addr
));
299 EXPORT_SYMBOL(devm_ioport_unmap
);
300 #endif /* CONFIG_HAS_IOPORT_MAP */
306 #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS
308 struct pcim_iomap_devres
{
309 void __iomem
*table
[PCIM_IOMAP_MAX
];
312 static void pcim_iomap_release(struct device
*gendev
, void *res
)
314 struct pci_dev
*dev
= to_pci_dev(gendev
);
315 struct pcim_iomap_devres
*this = res
;
318 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
320 pci_iounmap(dev
, this->table
[i
]);
324 * pcim_iomap_table - access iomap allocation table
325 * @pdev: PCI device to access iomap table for
327 * Access iomap allocation table for @dev. If iomap table doesn't
328 * exist and @pdev is managed, it will be allocated. All iomaps
329 * recorded in the iomap table are automatically unmapped on driver
332 * This function might sleep when the table is first allocated but can
333 * be safely called without context and guaranteed to succed once
336 void __iomem
* const *pcim_iomap_table(struct pci_dev
*pdev
)
338 struct pcim_iomap_devres
*dr
, *new_dr
;
340 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
344 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
347 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
350 EXPORT_SYMBOL(pcim_iomap_table
);
353 * pcim_iomap - Managed pcim_iomap()
354 * @pdev: PCI device to iomap for
356 * @maxlen: Maximum length of iomap
358 * Managed pci_iomap(). Map is automatically unmapped on driver
361 void __iomem
*pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
365 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
367 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
368 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
371 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
374 EXPORT_SYMBOL(pcim_iomap
);
377 * pcim_iounmap - Managed pci_iounmap()
378 * @pdev: PCI device to iounmap for
379 * @addr: Address to unmap
381 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
383 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
388 pci_iounmap(pdev
, addr
);
390 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
393 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
394 if (tbl
[i
] == addr
) {
400 EXPORT_SYMBOL(pcim_iounmap
);
403 * pcim_iomap_regions - Request and iomap PCI BARs
404 * @pdev: PCI device to map IO resources for
405 * @mask: Mask of BARs to request and iomap
406 * @name: Name used when requesting regions
408 * Request and iomap regions specified by @mask.
410 int pcim_iomap_regions(struct pci_dev
*pdev
, int mask
, const char *name
)
412 void __iomem
* const *iomap
;
415 iomap
= pcim_iomap_table(pdev
);
419 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
422 if (!(mask
& (1 << i
)))
426 len
= pci_resource_len(pdev
, i
);
430 rc
= pci_request_region(pdev
, i
, name
);
435 if (!pcim_iomap(pdev
, i
, 0))
442 pci_release_region(pdev
, i
);
445 if (!(mask
& (1 << i
)))
447 pcim_iounmap(pdev
, iomap
[i
]);
448 pci_release_region(pdev
, i
);
453 EXPORT_SYMBOL(pcim_iomap_regions
);
456 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
457 * @pdev: PCI device to map IO resources for
458 * @mask: Mask of BARs to iomap
459 * @name: Name used when requesting regions
461 * Request all PCI BARs and iomap regions specified by @mask.
463 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, int mask
,
466 int request_mask
= ((1 << 6) - 1) & ~mask
;
469 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
473 rc
= pcim_iomap_regions(pdev
, mask
, name
);
475 pci_release_selected_regions(pdev
, request_mask
);
478 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
481 * pcim_iounmap_regions - Unmap and release PCI BARs
482 * @pdev: PCI device to map IO resources for
483 * @mask: Mask of BARs to unmap and release
485 * Unmap and release regions specified by @mask.
487 void pcim_iounmap_regions(struct pci_dev
*pdev
, int mask
)
489 void __iomem
* const *iomap
;
492 iomap
= pcim_iomap_table(pdev
);
496 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++) {
497 if (!(mask
& (1 << i
)))
500 pcim_iounmap(pdev
, iomap
[i
]);
501 pci_release_region(pdev
, i
);
504 EXPORT_SYMBOL(pcim_iounmap_regions
);
505 #endif /* CONFIG_PCI */