5 #include <linux/export.h>
7 void devm_ioremap_release(struct device
*dev
, void *res
)
9 iounmap(*(void __iomem
**)res
);
12 static int devm_ioremap_match(struct device
*dev
, void *res
, void *match_data
)
14 return *(void **)res
== match_data
;
18 * devm_ioremap - Managed ioremap()
19 * @dev: Generic device to remap IO address for
20 * @offset: BUS offset to map
23 * Managed ioremap(). Map is automatically unmapped on driver detach.
25 void __iomem
*devm_ioremap(struct device
*dev
, resource_size_t offset
,
28 void __iomem
**ptr
, *addr
;
30 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
34 addr
= ioremap(offset
, size
);
43 EXPORT_SYMBOL(devm_ioremap
);
46 * devm_ioremap_nocache - Managed ioremap_nocache()
47 * @dev: Generic device to remap IO address for
48 * @offset: BUS offset to map
51 * Managed ioremap_nocache(). Map is automatically unmapped on driver
54 void __iomem
*devm_ioremap_nocache(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_nocache(offset
, size
);
72 EXPORT_SYMBOL(devm_ioremap_nocache
);
75 * devm_iounmap - Managed iounmap()
76 * @dev: Generic device to unmap for
77 * @addr: Address to unmap
79 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
81 void devm_iounmap(struct device
*dev
, void __iomem
*addr
)
83 WARN_ON(devres_destroy(dev
, devm_ioremap_release
, devm_ioremap_match
,
84 (__force
void *)addr
));
87 EXPORT_SYMBOL(devm_iounmap
);
89 #define IOMEM_ERR_PTR(err) (__force void __iomem *)ERR_PTR(err)
92 * devm_ioremap_resource() - check, request region, and ioremap resource
93 * @dev: generic device to handle the resource for
94 * @res: resource to be handled
96 * Checks that a resource is a valid memory region, requests the memory region
97 * and ioremaps it either as cacheable or as non-cacheable memory depending on
98 * the resource's flags. All operations are managed and will be undone on
101 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
102 * on failure. Usage example:
104 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105 * base = devm_ioremap_resource(&pdev->dev, res);
107 * return PTR_ERR(base);
109 void __iomem
*devm_ioremap_resource(struct device
*dev
, struct resource
*res
)
111 resource_size_t size
;
113 void __iomem
*dest_ptr
;
117 if (!res
|| resource_type(res
) != IORESOURCE_MEM
) {
118 dev_err(dev
, "invalid resource\n");
119 return IOMEM_ERR_PTR(-EINVAL
);
122 size
= resource_size(res
);
123 name
= res
->name
?: dev_name(dev
);
125 if (!devm_request_mem_region(dev
, res
->start
, size
, name
)) {
126 dev_err(dev
, "can't request region for resource %pR\n", res
);
127 return IOMEM_ERR_PTR(-EBUSY
);
130 if (res
->flags
& IORESOURCE_CACHEABLE
)
131 dest_ptr
= devm_ioremap(dev
, res
->start
, size
);
133 dest_ptr
= devm_ioremap_nocache(dev
, res
->start
, size
);
136 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
137 devm_release_mem_region(dev
, res
->start
, size
);
138 dest_ptr
= IOMEM_ERR_PTR(-ENOMEM
);
143 EXPORT_SYMBOL(devm_ioremap_resource
);
145 #ifdef CONFIG_HAS_IOPORT_MAP
147 * Generic iomap devres
149 static void devm_ioport_map_release(struct device
*dev
, void *res
)
151 ioport_unmap(*(void __iomem
**)res
);
154 static int devm_ioport_map_match(struct device
*dev
, void *res
,
157 return *(void **)res
== match_data
;
161 * devm_ioport_map - Managed ioport_map()
162 * @dev: Generic device to map ioport for
164 * @nr: Number of ports to map
166 * Managed ioport_map(). Map is automatically unmapped on driver
169 void __iomem
*devm_ioport_map(struct device
*dev
, unsigned long port
,
172 void __iomem
**ptr
, *addr
;
174 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
178 addr
= ioport_map(port
, nr
);
181 devres_add(dev
, ptr
);
187 EXPORT_SYMBOL(devm_ioport_map
);
190 * devm_ioport_unmap - Managed ioport_unmap()
191 * @dev: Generic device to unmap for
192 * @addr: Address to unmap
194 * Managed ioport_unmap(). @addr must have been mapped using
197 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
200 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
201 devm_ioport_map_match
, (__force
void *)addr
));
203 EXPORT_SYMBOL(devm_ioport_unmap
);
204 #endif /* CONFIG_HAS_IOPORT_MAP */
210 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
212 struct pcim_iomap_devres
{
213 void __iomem
*table
[PCIM_IOMAP_MAX
];
216 static void pcim_iomap_release(struct device
*gendev
, void *res
)
218 struct pci_dev
*dev
= container_of(gendev
, struct pci_dev
, dev
);
219 struct pcim_iomap_devres
*this = res
;
222 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
224 pci_iounmap(dev
, this->table
[i
]);
228 * pcim_iomap_table - access iomap allocation table
229 * @pdev: PCI device to access iomap table for
231 * Access iomap allocation table for @dev. If iomap table doesn't
232 * exist and @pdev is managed, it will be allocated. All iomaps
233 * recorded in the iomap table are automatically unmapped on driver
236 * This function might sleep when the table is first allocated but can
237 * be safely called without context and guaranteed to succed once
240 void __iomem
* const *pcim_iomap_table(struct pci_dev
*pdev
)
242 struct pcim_iomap_devres
*dr
, *new_dr
;
244 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
248 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
251 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
254 EXPORT_SYMBOL(pcim_iomap_table
);
257 * pcim_iomap - Managed pcim_iomap()
258 * @pdev: PCI device to iomap for
260 * @maxlen: Maximum length of iomap
262 * Managed pci_iomap(). Map is automatically unmapped on driver
265 void __iomem
*pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
269 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
271 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
272 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
275 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
278 EXPORT_SYMBOL(pcim_iomap
);
281 * pcim_iounmap - Managed pci_iounmap()
282 * @pdev: PCI device to iounmap for
283 * @addr: Address to unmap
285 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
287 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
292 pci_iounmap(pdev
, addr
);
294 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
297 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
298 if (tbl
[i
] == addr
) {
304 EXPORT_SYMBOL(pcim_iounmap
);
307 * pcim_iomap_regions - Request and iomap PCI BARs
308 * @pdev: PCI device to map IO resources for
309 * @mask: Mask of BARs to request and iomap
310 * @name: Name used when requesting regions
312 * Request and iomap regions specified by @mask.
314 int pcim_iomap_regions(struct pci_dev
*pdev
, int mask
, const char *name
)
316 void __iomem
* const *iomap
;
319 iomap
= pcim_iomap_table(pdev
);
323 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
326 if (!(mask
& (1 << i
)))
330 len
= pci_resource_len(pdev
, i
);
334 rc
= pci_request_region(pdev
, i
, name
);
339 if (!pcim_iomap(pdev
, i
, 0))
346 pci_release_region(pdev
, i
);
349 if (!(mask
& (1 << i
)))
351 pcim_iounmap(pdev
, iomap
[i
]);
352 pci_release_region(pdev
, i
);
357 EXPORT_SYMBOL(pcim_iomap_regions
);
360 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
361 * @pdev: PCI device to map IO resources for
362 * @mask: Mask of BARs to iomap
363 * @name: Name used when requesting regions
365 * Request all PCI BARs and iomap regions specified by @mask.
367 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, int mask
,
370 int request_mask
= ((1 << 6) - 1) & ~mask
;
373 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
377 rc
= pcim_iomap_regions(pdev
, mask
, name
);
379 pci_release_selected_regions(pdev
, request_mask
);
382 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
385 * pcim_iounmap_regions - Unmap and release PCI BARs
386 * @pdev: PCI device to map IO resources for
387 * @mask: Mask of BARs to unmap and release
389 * Unmap and release regions specified by @mask.
391 void pcim_iounmap_regions(struct pci_dev
*pdev
, int mask
)
393 void __iomem
* const *iomap
;
396 iomap
= pcim_iomap_table(pdev
);
400 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
401 if (!(mask
& (1 << i
)))
404 pcim_iounmap(pdev
, iomap
[i
]);
405 pci_release_region(pdev
, i
);
408 EXPORT_SYMBOL(pcim_iounmap_regions
);
409 #endif /* CONFIG_PCI */