1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/device.h>
9 #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS
11 struct pcim_iomap_devres {
12 void __iomem *table[PCIM_IOMAP_MAX];
16 static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
18 struct resource **res = ptr;
20 pci_unmap_iospace(*res);
24 * devm_pci_remap_iospace - Managed pci_remap_iospace()
25 * @dev: Generic device to remap IO address for
26 * @res: Resource describing the I/O space
27 * @phys_addr: physical address of range to be mapped
29 * Managed pci_remap_iospace(). Map is automatically unmapped on driver
32 int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
33 phys_addr_t phys_addr)
35 const struct resource **ptr;
38 ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
42 error = pci_remap_iospace(res, phys_addr);
52 EXPORT_SYMBOL(devm_pci_remap_iospace);
55 * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
56 * @dev: Generic device to remap IO address for
57 * @offset: Resource address to map
60 * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
63 void __iomem *devm_pci_remap_cfgspace(struct device *dev,
64 resource_size_t offset,
67 void __iomem **ptr, *addr;
69 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
73 addr = pci_remap_cfgspace(offset, size);
82 EXPORT_SYMBOL(devm_pci_remap_cfgspace);
85 * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
86 * @dev: generic device to handle the resource for
87 * @res: configuration space resource to be handled
89 * Checks that a resource is a valid memory region, requests the memory
90 * region and ioremaps with pci_remap_cfgspace() API that ensures the
91 * proper PCI configuration space memory attributes are guaranteed.
93 * All operations are managed and will be undone on driver detach.
95 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
96 * on failure. Usage example::
98 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
99 * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
101 * return PTR_ERR(base);
103 void __iomem *devm_pci_remap_cfg_resource(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");
114 return IOMEM_ERR_PTR(-EINVAL);
117 size = resource_size(res);
120 name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev),
123 name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
125 return IOMEM_ERR_PTR(-ENOMEM);
127 if (!devm_request_mem_region(dev, res->start, size, name)) {
128 dev_err(dev, "can't request region for resource %pR\n", res);
129 return IOMEM_ERR_PTR(-EBUSY);
132 dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
134 dev_err(dev, "ioremap failed for resource %pR\n", res);
135 devm_release_mem_region(dev, res->start, size);
136 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
141 EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
144 * pcim_set_mwi - a device-managed pci_set_mwi()
145 * @dev: the PCI device for which MWI is enabled
147 * Managed pci_set_mwi().
149 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
151 int pcim_set_mwi(struct pci_dev *dev)
153 struct pci_devres *dr;
155 dr = find_pci_dr(dev);
160 return pci_set_mwi(dev);
162 EXPORT_SYMBOL(pcim_set_mwi);
165 static void pcim_release(struct device *gendev, void *res)
167 struct pci_dev *dev = to_pci_dev(gendev);
168 struct pci_devres *this = res;
171 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
172 if (this->region_mask & (1 << i))
173 pci_release_region(dev, i);
178 if (this->restore_intx)
179 pci_intx(dev, this->orig_intx);
181 if (this->enabled && !this->pinned)
182 pci_disable_device(dev);
186 * TODO: After the last four callers in pci.c are ported, find_pci_dr()
187 * needs to be made static again.
189 struct pci_devres *find_pci_dr(struct pci_dev *pdev)
191 if (pci_is_managed(pdev))
192 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
196 static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
198 struct pci_devres *dr, *new_dr;
200 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
204 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
207 return devres_get(&pdev->dev, new_dr, NULL, NULL);
211 * pcim_enable_device - Managed pci_enable_device()
212 * @pdev: PCI device to be initialized
214 * Managed pci_enable_device().
216 int pcim_enable_device(struct pci_dev *pdev)
218 struct pci_devres *dr;
221 dr = get_pci_dr(pdev);
227 rc = pci_enable_device(pdev);
229 pdev->is_managed = 1;
234 EXPORT_SYMBOL(pcim_enable_device);
237 * pcim_pin_device - Pin managed PCI device
238 * @pdev: PCI device to pin
240 * Pin managed PCI device @pdev. Pinned device won't be disabled on
241 * driver detach. @pdev must have been enabled with
242 * pcim_enable_device().
244 void pcim_pin_device(struct pci_dev *pdev)
246 struct pci_devres *dr;
248 dr = find_pci_dr(pdev);
249 WARN_ON(!dr || !dr->enabled);
253 EXPORT_SYMBOL(pcim_pin_device);
255 static void pcim_iomap_release(struct device *gendev, void *res)
257 struct pci_dev *dev = to_pci_dev(gendev);
258 struct pcim_iomap_devres *this = res;
261 for (i = 0; i < PCIM_IOMAP_MAX; i++)
263 pci_iounmap(dev, this->table[i]);
267 * pcim_iomap_table - access iomap allocation table
268 * @pdev: PCI device to access iomap table for
270 * Access iomap allocation table for @dev. If iomap table doesn't
271 * exist and @pdev is managed, it will be allocated. All iomaps
272 * recorded in the iomap table are automatically unmapped on driver
275 * This function might sleep when the table is first allocated but can
276 * be safely called without context and guaranteed to succeed once
279 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
281 struct pcim_iomap_devres *dr, *new_dr;
283 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
287 new_dr = devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL,
288 dev_to_node(&pdev->dev));
291 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
294 EXPORT_SYMBOL(pcim_iomap_table);
297 * pcim_iomap - Managed pcim_iomap()
298 * @pdev: PCI device to iomap for
300 * @maxlen: Maximum length of iomap
302 * Managed pci_iomap(). Map is automatically unmapped on driver
305 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
309 BUG_ON(bar >= PCIM_IOMAP_MAX);
311 tbl = (void __iomem **)pcim_iomap_table(pdev);
312 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
315 tbl[bar] = pci_iomap(pdev, bar, maxlen);
318 EXPORT_SYMBOL(pcim_iomap);
321 * pcim_iounmap - Managed pci_iounmap()
322 * @pdev: PCI device to iounmap for
323 * @addr: Address to unmap
325 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
327 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
332 pci_iounmap(pdev, addr);
334 tbl = (void __iomem **)pcim_iomap_table(pdev);
337 for (i = 0; i < PCIM_IOMAP_MAX; i++)
338 if (tbl[i] == addr) {
344 EXPORT_SYMBOL(pcim_iounmap);
347 * pcim_iomap_regions - Request and iomap PCI BARs
348 * @pdev: PCI device to map IO resources for
349 * @mask: Mask of BARs to request and iomap
350 * @name: Name used when requesting regions
352 * Request and iomap regions specified by @mask.
354 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
356 void __iomem * const *iomap;
359 iomap = pcim_iomap_table(pdev);
363 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
366 if (!(mask & (1 << i)))
370 len = pci_resource_len(pdev, i);
374 rc = pci_request_region(pdev, i, name);
379 if (!pcim_iomap(pdev, i, 0))
386 pci_release_region(pdev, i);
389 if (!(mask & (1 << i)))
391 pcim_iounmap(pdev, iomap[i]);
392 pci_release_region(pdev, i);
397 EXPORT_SYMBOL(pcim_iomap_regions);
400 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
401 * @pdev: PCI device to map IO resources for
402 * @mask: Mask of BARs to iomap
403 * @name: Name used when requesting regions
405 * Request all PCI BARs and iomap regions specified by @mask.
407 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
410 int request_mask = ((1 << 6) - 1) & ~mask;
413 rc = pci_request_selected_regions(pdev, request_mask, name);
417 rc = pcim_iomap_regions(pdev, mask, name);
419 pci_release_selected_regions(pdev, request_mask);
422 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
425 * pcim_iounmap_regions - Unmap and release PCI BARs
426 * @pdev: PCI device to map IO resources for
427 * @mask: Mask of BARs to unmap and release
429 * Unmap and release regions specified by @mask.
431 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
433 void __iomem * const *iomap;
436 iomap = pcim_iomap_table(pdev);
440 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
441 if (!(mask & (1 << i)))
444 pcim_iounmap(pdev, iomap[i]);
445 pci_release_region(pdev, i);
448 EXPORT_SYMBOL(pcim_iounmap_regions);