4 #include <linux/export.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_ioremap_resource() - check, request region, and ioremap resource
90 * @dev: generic device to handle the resource for
91 * @res: resource to be handled
93 * Checks that a resource is a valid memory region, requests the memory region
94 * and ioremaps it either as cacheable or as non-cacheable memory depending on
95 * the resource's flags. All operations are managed and will be undone on
98 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
99 * on failure. Usage example:
101 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
102 * base = devm_ioremap_resource(&pdev->dev, res);
104 * return PTR_ERR(base);
106 void __iomem
*devm_ioremap_resource(struct device
*dev
, struct resource
*res
)
108 resource_size_t size
;
110 void __iomem
*dest_ptr
;
114 if (!res
|| resource_type(res
) != IORESOURCE_MEM
) {
115 dev_err(dev
, "invalid resource\n");
116 return ERR_PTR(-EINVAL
);
119 size
= resource_size(res
);
120 name
= res
->name
?: dev_name(dev
);
122 if (!devm_request_mem_region(dev
, res
->start
, size
, name
)) {
123 dev_err(dev
, "can't request region for resource %pR\n", res
);
124 return ERR_PTR(-EBUSY
);
127 if (res
->flags
& IORESOURCE_CACHEABLE
)
128 dest_ptr
= devm_ioremap(dev
, res
->start
, size
);
130 dest_ptr
= devm_ioremap_nocache(dev
, res
->start
, size
);
133 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
134 devm_release_mem_region(dev
, res
->start
, size
);
135 dest_ptr
= ERR_PTR(-ENOMEM
);
140 EXPORT_SYMBOL(devm_ioremap_resource
);
143 * devm_request_and_ioremap() - Check, request region, and ioremap resource
144 * @dev: Generic device to handle the resource for
145 * @res: resource to be handled
147 * Takes all necessary steps to ioremap a mem resource. Uses managed device, so
148 * everything is undone on driver detach. Checks arguments, so you can feed
149 * it the result from e.g. platform_get_resource() directly. Returns the
150 * remapped pointer or NULL on error. Usage example:
152 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
153 * base = devm_request_and_ioremap(&pdev->dev, res);
155 * return -EADDRNOTAVAIL;
157 void __iomem
*devm_request_and_ioremap(struct device
*device
,
158 struct resource
*res
)
160 void __iomem
*dest_ptr
;
162 dest_ptr
= devm_ioremap_resource(device
, res
);
163 if (IS_ERR(dest_ptr
))
168 EXPORT_SYMBOL(devm_request_and_ioremap
);
170 #ifdef CONFIG_HAS_IOPORT
172 * Generic iomap devres
174 static void devm_ioport_map_release(struct device
*dev
, void *res
)
176 ioport_unmap(*(void __iomem
**)res
);
179 static int devm_ioport_map_match(struct device
*dev
, void *res
,
182 return *(void **)res
== match_data
;
186 * devm_ioport_map - Managed ioport_map()
187 * @dev: Generic device to map ioport for
189 * @nr: Number of ports to map
191 * Managed ioport_map(). Map is automatically unmapped on driver
194 void __iomem
* devm_ioport_map(struct device
*dev
, unsigned long port
,
197 void __iomem
**ptr
, *addr
;
199 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
203 addr
= ioport_map(port
, nr
);
206 devres_add(dev
, ptr
);
212 EXPORT_SYMBOL(devm_ioport_map
);
215 * devm_ioport_unmap - Managed ioport_unmap()
216 * @dev: Generic device to unmap for
217 * @addr: Address to unmap
219 * Managed ioport_unmap(). @addr must have been mapped using
222 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
225 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
226 devm_ioport_map_match
, (void *)addr
));
228 EXPORT_SYMBOL(devm_ioport_unmap
);
234 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
236 struct pcim_iomap_devres
{
237 void __iomem
*table
[PCIM_IOMAP_MAX
];
240 static void pcim_iomap_release(struct device
*gendev
, void *res
)
242 struct pci_dev
*dev
= container_of(gendev
, struct pci_dev
, dev
);
243 struct pcim_iomap_devres
*this = res
;
246 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
248 pci_iounmap(dev
, this->table
[i
]);
252 * pcim_iomap_table - access iomap allocation table
253 * @pdev: PCI device to access iomap table for
255 * Access iomap allocation table for @dev. If iomap table doesn't
256 * exist and @pdev is managed, it will be allocated. All iomaps
257 * recorded in the iomap table are automatically unmapped on driver
260 * This function might sleep when the table is first allocated but can
261 * be safely called without context and guaranteed to succed once
264 void __iomem
* const * pcim_iomap_table(struct pci_dev
*pdev
)
266 struct pcim_iomap_devres
*dr
, *new_dr
;
268 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
272 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
275 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
278 EXPORT_SYMBOL(pcim_iomap_table
);
281 * pcim_iomap - Managed pcim_iomap()
282 * @pdev: PCI device to iomap for
284 * @maxlen: Maximum length of iomap
286 * Managed pci_iomap(). Map is automatically unmapped on driver
289 void __iomem
* pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
293 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
295 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
296 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
299 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
302 EXPORT_SYMBOL(pcim_iomap
);
305 * pcim_iounmap - Managed pci_iounmap()
306 * @pdev: PCI device to iounmap for
307 * @addr: Address to unmap
309 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
311 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
316 pci_iounmap(pdev
, addr
);
318 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
321 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
322 if (tbl
[i
] == addr
) {
328 EXPORT_SYMBOL(pcim_iounmap
);
331 * pcim_iomap_regions - Request and iomap PCI BARs
332 * @pdev: PCI device to map IO resources for
333 * @mask: Mask of BARs to request and iomap
334 * @name: Name used when requesting regions
336 * Request and iomap regions specified by @mask.
338 int pcim_iomap_regions(struct pci_dev
*pdev
, int mask
, const char *name
)
340 void __iomem
* const *iomap
;
343 iomap
= pcim_iomap_table(pdev
);
347 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
350 if (!(mask
& (1 << i
)))
354 len
= pci_resource_len(pdev
, i
);
358 rc
= pci_request_region(pdev
, i
, name
);
363 if (!pcim_iomap(pdev
, i
, 0))
370 pci_release_region(pdev
, i
);
373 if (!(mask
& (1 << i
)))
375 pcim_iounmap(pdev
, iomap
[i
]);
376 pci_release_region(pdev
, i
);
381 EXPORT_SYMBOL(pcim_iomap_regions
);
384 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
385 * @pdev: PCI device to map IO resources for
386 * @mask: Mask of BARs to iomap
387 * @name: Name used when requesting regions
389 * Request all PCI BARs and iomap regions specified by @mask.
391 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, int mask
,
394 int request_mask
= ((1 << 6) - 1) & ~mask
;
397 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
401 rc
= pcim_iomap_regions(pdev
, mask
, name
);
403 pci_release_selected_regions(pdev
, request_mask
);
406 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
409 * pcim_iounmap_regions - Unmap and release PCI BARs
410 * @pdev: PCI device to map IO resources for
411 * @mask: Mask of BARs to unmap and release
413 * Unmap and release regions specified by @mask.
415 void pcim_iounmap_regions(struct pci_dev
*pdev
, int mask
)
417 void __iomem
* const *iomap
;
420 iomap
= pcim_iomap_table(pdev
);
424 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
425 if (!(mask
& (1 << i
)))
428 pcim_iounmap(pdev
, iomap
[i
]);
429 pci_release_region(pdev
, i
);
432 EXPORT_SYMBOL(pcim_iounmap_regions
);
433 #endif /* CONFIG_PCI */
434 #endif /* CONFIG_HAS_IOPORT */