3 #include <linux/module.h>
5 void devm_ioremap_release(struct device *dev, void *res)
7 iounmap(*(void __iomem **)res);
10 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
12 return *(void **)res == match_data;
16 * devm_ioremap - Managed ioremap()
17 * @dev: Generic device to remap IO address for
18 * @offset: BUS offset to map
21 * Managed ioremap(). Map is automatically unmapped on driver detach.
23 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
26 void __iomem **ptr, *addr;
28 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
32 addr = ioremap(offset, size);
41 EXPORT_SYMBOL(devm_ioremap);
44 * devm_ioremap_nocache - Managed ioremap_nocache()
45 * @dev: Generic device to remap IO address for
46 * @offset: BUS offset to map
49 * Managed ioremap_nocache(). Map is automatically unmapped on driver
52 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
55 void __iomem **ptr, *addr;
57 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
61 addr = ioremap_nocache(offset, size);
70 EXPORT_SYMBOL(devm_ioremap_nocache);
73 * devm_iounmap - Managed iounmap()
74 * @dev: Generic device to unmap for
75 * @addr: Address to unmap
77 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
79 void devm_iounmap(struct device *dev, void __iomem *addr)
82 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
85 EXPORT_SYMBOL(devm_iounmap);
87 #ifdef CONFIG_HAS_IOPORT
89 * Generic iomap devres
91 static void devm_ioport_map_release(struct device *dev, void *res)
93 ioport_unmap(*(void __iomem **)res);
96 static int devm_ioport_map_match(struct device *dev, void *res,
99 return *(void **)res == match_data;
103 * devm_ioport_map - Managed ioport_map()
104 * @dev: Generic device to map ioport for
106 * @nr: Number of ports to map
108 * Managed ioport_map(). Map is automatically unmapped on driver
111 void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
114 void __iomem **ptr, *addr;
116 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
120 addr = ioport_map(port, nr);
123 devres_add(dev, ptr);
129 EXPORT_SYMBOL(devm_ioport_map);
132 * devm_ioport_unmap - Managed ioport_unmap()
133 * @dev: Generic device to unmap for
134 * @addr: Address to unmap
136 * Managed ioport_unmap(). @addr must have been mapped using
139 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
142 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
143 devm_ioport_map_match, (void *)addr));
145 EXPORT_SYMBOL(devm_ioport_unmap);
151 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
153 struct pcim_iomap_devres {
154 void __iomem *table[PCIM_IOMAP_MAX];
157 static void pcim_iomap_release(struct device *gendev, void *res)
159 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
160 struct pcim_iomap_devres *this = res;
163 for (i = 0; i < PCIM_IOMAP_MAX; i++)
165 pci_iounmap(dev, this->table[i]);
169 * pcim_iomap_table - access iomap allocation table
170 * @pdev: PCI device to access iomap table for
172 * Access iomap allocation table for @dev. If iomap table doesn't
173 * exist and @pdev is managed, it will be allocated. All iomaps
174 * recorded in the iomap table are automatically unmapped on driver
177 * This function might sleep when the table is first allocated but can
178 * be safely called without context and guaranteed to succed once
181 void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
183 struct pcim_iomap_devres *dr, *new_dr;
185 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
189 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
192 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
195 EXPORT_SYMBOL(pcim_iomap_table);
198 * pcim_iomap - Managed pcim_iomap()
199 * @pdev: PCI device to iomap for
201 * @maxlen: Maximum length of iomap
203 * Managed pci_iomap(). Map is automatically unmapped on driver
206 void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
210 BUG_ON(bar >= PCIM_IOMAP_MAX);
212 tbl = (void __iomem **)pcim_iomap_table(pdev);
213 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
216 tbl[bar] = pci_iomap(pdev, bar, maxlen);
219 EXPORT_SYMBOL(pcim_iomap);
222 * pcim_iounmap - Managed pci_iounmap()
223 * @pdev: PCI device to iounmap for
224 * @addr: Address to unmap
226 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
228 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
233 pci_iounmap(pdev, addr);
235 tbl = (void __iomem **)pcim_iomap_table(pdev);
238 for (i = 0; i < PCIM_IOMAP_MAX; i++)
239 if (tbl[i] == addr) {
245 EXPORT_SYMBOL(pcim_iounmap);
248 * pcim_iomap_regions - Request and iomap PCI BARs
249 * @pdev: PCI device to map IO resources for
250 * @mask: Mask of BARs to request and iomap
251 * @name: Name used when requesting regions
253 * Request and iomap regions specified by @mask.
255 int pcim_iomap_regions(struct pci_dev *pdev, u16 mask, const char *name)
257 void __iomem * const *iomap;
260 iomap = pcim_iomap_table(pdev);
264 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
267 if (!(mask & (1 << i)))
271 len = pci_resource_len(pdev, i);
275 rc = pci_request_region(pdev, i, name);
280 if (!pcim_iomap(pdev, i, 0))
287 pci_release_region(pdev, i);
290 if (!(mask & (1 << i)))
292 pcim_iounmap(pdev, iomap[i]);
293 pci_release_region(pdev, i);
298 EXPORT_SYMBOL(pcim_iomap_regions);
301 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
302 * @pdev: PCI device to map IO resources for
303 * @mask: Mask of BARs to iomap
304 * @name: Name used when requesting regions
306 * Request all PCI BARs and iomap regions specified by @mask.
308 int pcim_iomap_regions_request_all(struct pci_dev *pdev, u16 mask,
311 int request_mask = ((1 << 6) - 1) & ~mask;
314 rc = pci_request_selected_regions(pdev, request_mask, name);
318 rc = pcim_iomap_regions(pdev, mask, name);
320 pci_release_selected_regions(pdev, request_mask);
323 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
326 * pcim_iounmap_regions - Unmap and release PCI BARs
327 * @pdev: PCI device to map IO resources for
328 * @mask: Mask of BARs to unmap and release
330 * Unamp and release regions specified by @mask.
332 void pcim_iounmap_regions(struct pci_dev *pdev, u16 mask)
334 void __iomem * const *iomap;
337 iomap = pcim_iomap_table(pdev);
341 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
342 if (!(mask & (1 << i)))
345 pcim_iounmap(pdev, iomap[i]);
346 pci_release_region(pdev, i);
349 EXPORT_SYMBOL(pcim_iounmap_regions);