4 #include <linux/module.h>
6 void devm_ioremap_release(struct device
*dev
, void *res
)
8 iounmap(*(void __iomem
**)res
);
11 static int devm_ioremap_match(struct device
*dev
, void *res
, void *match_data
)
13 return *(void **)res
== match_data
;
17 * devm_ioremap - Managed ioremap()
18 * @dev: Generic device to remap IO address for
19 * @offset: BUS offset to map
22 * Managed ioremap(). Map is automatically unmapped on driver detach.
24 void __iomem
*devm_ioremap(struct device
*dev
, resource_size_t offset
,
27 void __iomem
**ptr
, *addr
;
29 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
33 addr
= ioremap(offset
, size
);
42 EXPORT_SYMBOL(devm_ioremap
);
45 * devm_ioremap_nocache - Managed ioremap_nocache()
46 * @dev: Generic device to remap IO address for
47 * @offset: BUS offset to map
50 * Managed ioremap_nocache(). Map is automatically unmapped on driver
53 void __iomem
*devm_ioremap_nocache(struct device
*dev
, resource_size_t offset
,
56 void __iomem
**ptr
, *addr
;
58 ptr
= devres_alloc(devm_ioremap_release
, sizeof(*ptr
), GFP_KERNEL
);
62 addr
= ioremap_nocache(offset
, size
);
71 EXPORT_SYMBOL(devm_ioremap_nocache
);
74 * devm_iounmap - Managed iounmap()
75 * @dev: Generic device to unmap for
76 * @addr: Address to unmap
78 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
80 void devm_iounmap(struct device
*dev
, void __iomem
*addr
)
82 WARN_ON(devres_destroy(dev
, devm_ioremap_release
, devm_ioremap_match
,
86 EXPORT_SYMBOL(devm_iounmap
);
89 * devm_request_and_ioremap() - Check, request region, and ioremap resource
90 * @dev: Generic device to handle the resource for
91 * @res: resource to be handled
93 * Takes all necessary steps to ioremap a mem resource. Uses managed device, so
94 * everything is undone on driver detach. Checks arguments, so you can feed
95 * it the result from e.g. platform_get_resource() directly. Returns the
96 * remapped pointer or NULL on error. Usage example:
98 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
99 * base = devm_request_and_ioremap(&pdev->dev, res);
101 * return -EADDRNOTAVAIL;
103 void __iomem
*devm_request_and_ioremap(struct device
*dev
,
104 struct resource
*res
)
106 resource_size_t size
;
108 void __iomem
*dest_ptr
;
112 if (!res
|| resource_type(res
) != IORESOURCE_MEM
) {
113 dev_err(dev
, "invalid resource\n");
117 size
= resource_size(res
);
118 name
= res
->name
?: dev_name(dev
);
120 if (!devm_request_mem_region(dev
, res
->start
, size
, name
)) {
121 dev_err(dev
, "can't request region for resource %pR\n", res
);
125 if (res
->flags
& IORESOURCE_CACHEABLE
)
126 dest_ptr
= devm_ioremap(dev
, res
->start
, size
);
128 dest_ptr
= devm_ioremap_nocache(dev
, res
->start
, size
);
131 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
132 devm_release_mem_region(dev
, res
->start
, size
);
137 EXPORT_SYMBOL(devm_request_and_ioremap
);
139 #ifdef CONFIG_HAS_IOPORT
141 * Generic iomap devres
143 static void devm_ioport_map_release(struct device
*dev
, void *res
)
145 ioport_unmap(*(void __iomem
**)res
);
148 static int devm_ioport_map_match(struct device
*dev
, void *res
,
151 return *(void **)res
== match_data
;
155 * devm_ioport_map - Managed ioport_map()
156 * @dev: Generic device to map ioport for
158 * @nr: Number of ports to map
160 * Managed ioport_map(). Map is automatically unmapped on driver
163 void __iomem
* devm_ioport_map(struct device
*dev
, unsigned long port
,
166 void __iomem
**ptr
, *addr
;
168 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
172 addr
= ioport_map(port
, nr
);
175 devres_add(dev
, ptr
);
181 EXPORT_SYMBOL(devm_ioport_map
);
184 * devm_ioport_unmap - Managed ioport_unmap()
185 * @dev: Generic device to unmap for
186 * @addr: Address to unmap
188 * Managed ioport_unmap(). @addr must have been mapped using
191 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
194 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
195 devm_ioport_map_match
, (void *)addr
));
197 EXPORT_SYMBOL(devm_ioport_unmap
);
203 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
205 struct pcim_iomap_devres
{
206 void __iomem
*table
[PCIM_IOMAP_MAX
];
209 static void pcim_iomap_release(struct device
*gendev
, void *res
)
211 struct pci_dev
*dev
= container_of(gendev
, struct pci_dev
, dev
);
212 struct pcim_iomap_devres
*this = res
;
215 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
217 pci_iounmap(dev
, this->table
[i
]);
221 * pcim_iomap_table - access iomap allocation table
222 * @pdev: PCI device to access iomap table for
224 * Access iomap allocation table for @dev. If iomap table doesn't
225 * exist and @pdev is managed, it will be allocated. All iomaps
226 * recorded in the iomap table are automatically unmapped on driver
229 * This function might sleep when the table is first allocated but can
230 * be safely called without context and guaranteed to succed once
233 void __iomem
* const * pcim_iomap_table(struct pci_dev
*pdev
)
235 struct pcim_iomap_devres
*dr
, *new_dr
;
237 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
241 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
244 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
247 EXPORT_SYMBOL(pcim_iomap_table
);
250 * pcim_iomap - Managed pcim_iomap()
251 * @pdev: PCI device to iomap for
253 * @maxlen: Maximum length of iomap
255 * Managed pci_iomap(). Map is automatically unmapped on driver
258 void __iomem
* pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
262 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
264 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
265 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
268 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
271 EXPORT_SYMBOL(pcim_iomap
);
274 * pcim_iounmap - Managed pci_iounmap()
275 * @pdev: PCI device to iounmap for
276 * @addr: Address to unmap
278 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
280 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
285 pci_iounmap(pdev
, addr
);
287 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
290 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
291 if (tbl
[i
] == addr
) {
297 EXPORT_SYMBOL(pcim_iounmap
);
300 * pcim_iomap_regions - Request and iomap PCI BARs
301 * @pdev: PCI device to map IO resources for
302 * @mask: Mask of BARs to request and iomap
303 * @name: Name used when requesting regions
305 * Request and iomap regions specified by @mask.
307 int pcim_iomap_regions(struct pci_dev
*pdev
, u16 mask
, const char *name
)
309 void __iomem
* const *iomap
;
312 iomap
= pcim_iomap_table(pdev
);
316 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
319 if (!(mask
& (1 << i
)))
323 len
= pci_resource_len(pdev
, i
);
327 rc
= pci_request_region(pdev
, i
, name
);
332 if (!pcim_iomap(pdev
, i
, 0))
339 pci_release_region(pdev
, i
);
342 if (!(mask
& (1 << i
)))
344 pcim_iounmap(pdev
, iomap
[i
]);
345 pci_release_region(pdev
, i
);
350 EXPORT_SYMBOL(pcim_iomap_regions
);
353 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
354 * @pdev: PCI device to map IO resources for
355 * @mask: Mask of BARs to iomap
356 * @name: Name used when requesting regions
358 * Request all PCI BARs and iomap regions specified by @mask.
360 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, u16 mask
,
363 int request_mask
= ((1 << 6) - 1) & ~mask
;
366 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
370 rc
= pcim_iomap_regions(pdev
, mask
, name
);
372 pci_release_selected_regions(pdev
, request_mask
);
375 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
378 * pcim_iounmap_regions - Unmap and release PCI BARs
379 * @pdev: PCI device to map IO resources for
380 * @mask: Mask of BARs to unmap and release
382 * Unmap and release regions specified by @mask.
384 void pcim_iounmap_regions(struct pci_dev
*pdev
, u16 mask
)
386 void __iomem
* const *iomap
;
389 iomap
= pcim_iomap_table(pdev
);
393 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
394 if (!(mask
& (1 << i
)))
397 pcim_iounmap(pdev
, iomap
[i
]);
398 pci_release_region(pdev
, i
);
401 EXPORT_SYMBOL(pcim_iounmap_regions
);
402 #endif /* CONFIG_PCI */
403 #endif /* CONFIG_HAS_IOPORT */