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
);
137 * devm_ioremap_resource() - check, request region, and ioremap resource
138 * @dev: generic device to handle the resource for
139 * @res: resource to be handled
141 * Checks that a resource is a valid memory region, requests the memory
142 * region and ioremaps it. All operations are managed and will be undone
145 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
146 * on failure. Usage example:
148 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 * base = devm_ioremap_resource(&pdev->dev, res);
151 * return PTR_ERR(base);
153 void __iomem
*devm_ioremap_resource(struct device
*dev
,
154 const struct resource
*res
)
156 resource_size_t size
;
157 void __iomem
*dest_ptr
;
161 if (!res
|| resource_type(res
) != IORESOURCE_MEM
) {
162 dev_err(dev
, "invalid resource\n");
163 return IOMEM_ERR_PTR(-EINVAL
);
166 size
= resource_size(res
);
168 if (!devm_request_mem_region(dev
, res
->start
, size
, dev_name(dev
))) {
169 dev_err(dev
, "can't request region for resource %pR\n", res
);
170 return IOMEM_ERR_PTR(-EBUSY
);
173 dest_ptr
= devm_ioremap(dev
, res
->start
, size
);
175 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
176 devm_release_mem_region(dev
, res
->start
, size
);
177 dest_ptr
= IOMEM_ERR_PTR(-ENOMEM
);
182 EXPORT_SYMBOL(devm_ioremap_resource
);
185 * devm_of_iomap - Requests a resource and maps the memory mapped IO
186 * for a given device_node managed by a given device
188 * Checks that a resource is a valid memory region, requests the memory
189 * region and ioremaps it. All operations are managed and will be undone
190 * on driver detach of the device.
192 * This is to be used when a device requests/maps resources described
193 * by other device tree nodes (children or otherwise).
195 * @dev: The device "managing" the resource
196 * @node: The device-tree node where the resource resides
197 * @index: index of the MMIO range in the "reg" property
198 * @size: Returns the size of the resource (pass NULL if not needed)
199 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
200 * error code on failure. Usage example:
202 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
204 * return PTR_ERR(base);
206 void __iomem
*devm_of_iomap(struct device
*dev
, struct device_node
*node
, int index
,
207 resource_size_t
*size
)
211 if (of_address_to_resource(node
, index
, &res
))
212 return IOMEM_ERR_PTR(-EINVAL
);
214 *size
= resource_size(&res
);
215 return devm_ioremap_resource(dev
, &res
);
217 EXPORT_SYMBOL(devm_of_iomap
);
219 #ifdef CONFIG_HAS_IOPORT_MAP
221 * Generic iomap devres
223 static void devm_ioport_map_release(struct device
*dev
, void *res
)
225 ioport_unmap(*(void __iomem
**)res
);
228 static int devm_ioport_map_match(struct device
*dev
, void *res
,
231 return *(void **)res
== match_data
;
235 * devm_ioport_map - Managed ioport_map()
236 * @dev: Generic device to map ioport for
238 * @nr: Number of ports to map
240 * Managed ioport_map(). Map is automatically unmapped on driver
243 void __iomem
*devm_ioport_map(struct device
*dev
, unsigned long port
,
246 void __iomem
**ptr
, *addr
;
248 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
252 addr
= ioport_map(port
, nr
);
255 devres_add(dev
, ptr
);
261 EXPORT_SYMBOL(devm_ioport_map
);
264 * devm_ioport_unmap - Managed ioport_unmap()
265 * @dev: Generic device to unmap for
266 * @addr: Address to unmap
268 * Managed ioport_unmap(). @addr must have been mapped using
271 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
274 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
275 devm_ioport_map_match
, (__force
void *)addr
));
277 EXPORT_SYMBOL(devm_ioport_unmap
);
278 #endif /* CONFIG_HAS_IOPORT_MAP */
284 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
286 struct pcim_iomap_devres
{
287 void __iomem
*table
[PCIM_IOMAP_MAX
];
290 static void pcim_iomap_release(struct device
*gendev
, void *res
)
292 struct pci_dev
*dev
= to_pci_dev(gendev
);
293 struct pcim_iomap_devres
*this = res
;
296 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
298 pci_iounmap(dev
, this->table
[i
]);
302 * pcim_iomap_table - access iomap allocation table
303 * @pdev: PCI device to access iomap table for
305 * Access iomap allocation table for @dev. If iomap table doesn't
306 * exist and @pdev is managed, it will be allocated. All iomaps
307 * recorded in the iomap table are automatically unmapped on driver
310 * This function might sleep when the table is first allocated but can
311 * be safely called without context and guaranteed to succed once
314 void __iomem
* const *pcim_iomap_table(struct pci_dev
*pdev
)
316 struct pcim_iomap_devres
*dr
, *new_dr
;
318 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
322 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
325 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
328 EXPORT_SYMBOL(pcim_iomap_table
);
331 * pcim_iomap - Managed pcim_iomap()
332 * @pdev: PCI device to iomap for
334 * @maxlen: Maximum length of iomap
336 * Managed pci_iomap(). Map is automatically unmapped on driver
339 void __iomem
*pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
343 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
345 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
346 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
349 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
352 EXPORT_SYMBOL(pcim_iomap
);
355 * pcim_iounmap - Managed pci_iounmap()
356 * @pdev: PCI device to iounmap for
357 * @addr: Address to unmap
359 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
361 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
366 pci_iounmap(pdev
, addr
);
368 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
371 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
372 if (tbl
[i
] == addr
) {
378 EXPORT_SYMBOL(pcim_iounmap
);
381 * pcim_iomap_regions - Request and iomap PCI BARs
382 * @pdev: PCI device to map IO resources for
383 * @mask: Mask of BARs to request and iomap
384 * @name: Name used when requesting regions
386 * Request and iomap regions specified by @mask.
388 int pcim_iomap_regions(struct pci_dev
*pdev
, int mask
, const char *name
)
390 void __iomem
* const *iomap
;
393 iomap
= pcim_iomap_table(pdev
);
397 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
400 if (!(mask
& (1 << i
)))
404 len
= pci_resource_len(pdev
, i
);
408 rc
= pci_request_region(pdev
, i
, name
);
413 if (!pcim_iomap(pdev
, i
, 0))
420 pci_release_region(pdev
, i
);
423 if (!(mask
& (1 << i
)))
425 pcim_iounmap(pdev
, iomap
[i
]);
426 pci_release_region(pdev
, i
);
431 EXPORT_SYMBOL(pcim_iomap_regions
);
434 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
435 * @pdev: PCI device to map IO resources for
436 * @mask: Mask of BARs to iomap
437 * @name: Name used when requesting regions
439 * Request all PCI BARs and iomap regions specified by @mask.
441 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, int mask
,
444 int request_mask
= ((1 << 6) - 1) & ~mask
;
447 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
451 rc
= pcim_iomap_regions(pdev
, mask
, name
);
453 pci_release_selected_regions(pdev
, request_mask
);
456 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
459 * pcim_iounmap_regions - Unmap and release PCI BARs
460 * @pdev: PCI device to map IO resources for
461 * @mask: Mask of BARs to unmap and release
463 * Unmap and release regions specified by @mask.
465 void pcim_iounmap_regions(struct pci_dev
*pdev
, int mask
)
467 void __iomem
* const *iomap
;
470 iomap
= pcim_iomap_table(pdev
);
474 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++) {
475 if (!(mask
& (1 << i
)))
478 pcim_iounmap(pdev
, iomap
[i
]);
479 pci_release_region(pdev
, i
);
482 EXPORT_SYMBOL(pcim_iounmap_regions
);
483 #endif /* CONFIG_PCI */