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
)
83 WARN_ON(devres_destroy(dev
, devm_ioremap_release
, devm_ioremap_match
,
86 EXPORT_SYMBOL(devm_iounmap
);
88 #ifdef CONFIG_HAS_IOPORT
90 * Generic iomap devres
92 static void devm_ioport_map_release(struct device
*dev
, void *res
)
94 ioport_unmap(*(void __iomem
**)res
);
97 static int devm_ioport_map_match(struct device
*dev
, void *res
,
100 return *(void **)res
== match_data
;
104 * devm_ioport_map - Managed ioport_map()
105 * @dev: Generic device to map ioport for
107 * @nr: Number of ports to map
109 * Managed ioport_map(). Map is automatically unmapped on driver
112 void __iomem
* devm_ioport_map(struct device
*dev
, unsigned long port
,
115 void __iomem
**ptr
, *addr
;
117 ptr
= devres_alloc(devm_ioport_map_release
, sizeof(*ptr
), GFP_KERNEL
);
121 addr
= ioport_map(port
, nr
);
124 devres_add(dev
, ptr
);
130 EXPORT_SYMBOL(devm_ioport_map
);
133 * devm_ioport_unmap - Managed ioport_unmap()
134 * @dev: Generic device to unmap for
135 * @addr: Address to unmap
137 * Managed ioport_unmap(). @addr must have been mapped using
140 void devm_ioport_unmap(struct device
*dev
, void __iomem
*addr
)
143 WARN_ON(devres_destroy(dev
, devm_ioport_map_release
,
144 devm_ioport_map_match
, (void *)addr
));
146 EXPORT_SYMBOL(devm_ioport_unmap
);
152 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
154 struct pcim_iomap_devres
{
155 void __iomem
*table
[PCIM_IOMAP_MAX
];
158 static void pcim_iomap_release(struct device
*gendev
, void *res
)
160 struct pci_dev
*dev
= container_of(gendev
, struct pci_dev
, dev
);
161 struct pcim_iomap_devres
*this = res
;
164 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
166 pci_iounmap(dev
, this->table
[i
]);
170 * pcim_iomap_table - access iomap allocation table
171 * @pdev: PCI device to access iomap table for
173 * Access iomap allocation table for @dev. If iomap table doesn't
174 * exist and @pdev is managed, it will be allocated. All iomaps
175 * recorded in the iomap table are automatically unmapped on driver
178 * This function might sleep when the table is first allocated but can
179 * be safely called without context and guaranteed to succed once
182 void __iomem
* const * pcim_iomap_table(struct pci_dev
*pdev
)
184 struct pcim_iomap_devres
*dr
, *new_dr
;
186 dr
= devres_find(&pdev
->dev
, pcim_iomap_release
, NULL
, NULL
);
190 new_dr
= devres_alloc(pcim_iomap_release
, sizeof(*new_dr
), GFP_KERNEL
);
193 dr
= devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
196 EXPORT_SYMBOL(pcim_iomap_table
);
199 * pcim_iomap - Managed pcim_iomap()
200 * @pdev: PCI device to iomap for
202 * @maxlen: Maximum length of iomap
204 * Managed pci_iomap(). Map is automatically unmapped on driver
207 void __iomem
* pcim_iomap(struct pci_dev
*pdev
, int bar
, unsigned long maxlen
)
211 BUG_ON(bar
>= PCIM_IOMAP_MAX
);
213 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
214 if (!tbl
|| tbl
[bar
]) /* duplicate mappings not allowed */
217 tbl
[bar
] = pci_iomap(pdev
, bar
, maxlen
);
220 EXPORT_SYMBOL(pcim_iomap
);
223 * pcim_iounmap - Managed pci_iounmap()
224 * @pdev: PCI device to iounmap for
225 * @addr: Address to unmap
227 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
229 void pcim_iounmap(struct pci_dev
*pdev
, void __iomem
*addr
)
234 pci_iounmap(pdev
, addr
);
236 tbl
= (void __iomem
**)pcim_iomap_table(pdev
);
239 for (i
= 0; i
< PCIM_IOMAP_MAX
; i
++)
240 if (tbl
[i
] == addr
) {
246 EXPORT_SYMBOL(pcim_iounmap
);
249 * pcim_iomap_regions - Request and iomap PCI BARs
250 * @pdev: PCI device to map IO resources for
251 * @mask: Mask of BARs to request and iomap
252 * @name: Name used when requesting regions
254 * Request and iomap regions specified by @mask.
256 int pcim_iomap_regions(struct pci_dev
*pdev
, u16 mask
, const char *name
)
258 void __iomem
* const *iomap
;
261 iomap
= pcim_iomap_table(pdev
);
265 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
268 if (!(mask
& (1 << i
)))
272 len
= pci_resource_len(pdev
, i
);
276 rc
= pci_request_region(pdev
, i
, name
);
281 if (!pcim_iomap(pdev
, i
, 0))
288 pci_release_region(pdev
, i
);
291 if (!(mask
& (1 << i
)))
293 pcim_iounmap(pdev
, iomap
[i
]);
294 pci_release_region(pdev
, i
);
299 EXPORT_SYMBOL(pcim_iomap_regions
);
302 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
303 * @pdev: PCI device to map IO resources for
304 * @mask: Mask of BARs to iomap
305 * @name: Name used when requesting regions
307 * Request all PCI BARs and iomap regions specified by @mask.
309 int pcim_iomap_regions_request_all(struct pci_dev
*pdev
, u16 mask
,
312 int request_mask
= ((1 << 6) - 1) & ~mask
;
315 rc
= pci_request_selected_regions(pdev
, request_mask
, name
);
319 rc
= pcim_iomap_regions(pdev
, mask
, name
);
321 pci_release_selected_regions(pdev
, request_mask
);
324 EXPORT_SYMBOL(pcim_iomap_regions_request_all
);
327 * pcim_iounmap_regions - Unmap and release PCI BARs
328 * @pdev: PCI device to map IO resources for
329 * @mask: Mask of BARs to unmap and release
331 * Unmap and release regions specified by @mask.
333 void pcim_iounmap_regions(struct pci_dev
*pdev
, u16 mask
)
335 void __iomem
* const *iomap
;
338 iomap
= pcim_iomap_table(pdev
);
342 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
343 if (!(mask
& (1 << i
)))
346 pcim_iounmap(pdev
, iomap
[i
]);
347 pci_release_region(pdev
, i
);
350 EXPORT_SYMBOL(pcim_iounmap_regions
);