1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/device.h>
7 * On the state of PCI's devres implementation:
9 * The older devres API for PCI has two significant problems:
11 * 1. It is very strongly tied to the statically allocated mapping table in
12 * struct pcim_iomap_devres below. This is mostly solved in the sense of the
13 * pcim_ functions in this file providing things like ranged mapping by
14 * bypassing this table, whereas the functions that were present in the old
15 * API still enter the mapping addresses into the table for users of the old
18 * 2. The region-request-functions in pci.c do become managed IF the device has
19 * been enabled with pcim_enable_device() instead of pci_enable_device().
20 * This resulted in the API becoming inconsistent: Some functions have an
21 * obviously managed counter-part (e.g., pci_iomap() <-> pcim_iomap()),
22 * whereas some don't and are never managed, while others don't and are
23 * _sometimes_ managed (e.g. pci_request_region()).
25 * Consequently, in the new API, region requests performed by the pcim_
26 * functions are automatically cleaned up through the devres callback
27 * pcim_addr_resource_release().
29 * Users of pcim_enable_device() + pci_*region*() are redirected in
30 * pci.c to the managed functions here in this file. This isn't exactly
31 * perfect, but the only alternative way would be to port ALL drivers
32 * using said combination to pcim_ functions.
35 * Remove the legacy table entirely once all calls to pcim_iomap_table() in
36 * the kernel have been removed.
40 * Legacy struct storing addresses to whole mapped BARs.
42 struct pcim_iomap_devres {
43 void __iomem *table[PCI_STD_NUM_BARS];
46 /* Used to restore the old INTx state on driver detach. */
47 struct pcim_intx_devres {
51 enum pcim_addr_devres_type {
52 /* Default initializer. */
53 PCIM_ADDR_DEVRES_TYPE_INVALID,
55 /* A requested region spanning an entire BAR. */
56 PCIM_ADDR_DEVRES_TYPE_REGION,
59 * A requested region spanning an entire BAR, and a mapping for
62 PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING,
65 * A mapping within a BAR, either spanning the whole BAR or just a
66 * range. Without a requested region.
68 PCIM_ADDR_DEVRES_TYPE_MAPPING,
72 * This struct envelops IO or MEM addresses, i.e., mappings and region
73 * requests, because those are very frequently requested and released
76 struct pcim_addr_devres {
77 enum pcim_addr_devres_type type;
78 void __iomem *baseaddr;
84 static inline void pcim_addr_devres_clear(struct pcim_addr_devres *res)
86 memset(res, 0, sizeof(*res));
91 * The following functions, __pcim_*_region*, exist as counterparts to the
92 * versions from pci.c - which, unfortunately, can be in "hybrid mode", i.e.,
93 * sometimes managed, sometimes not.
95 * To separate the APIs cleanly, we define our own, simplified versions here.
99 * __pcim_request_region_range - Request a ranged region
100 * @pdev: PCI device the region belongs to
101 * @bar: BAR the range is within
102 * @offset: offset from the BAR's start address
103 * @maxlen: length in bytes, beginning at @offset
104 * @name: name associated with the request
105 * @req_flags: flags for the request, e.g., for kernel-exclusive requests
107 * Returns: 0 on success, a negative error code on failure.
109 * Request a range within a device's PCI BAR. Sanity check the input.
111 static int __pcim_request_region_range(struct pci_dev *pdev, int bar,
112 unsigned long offset,
113 unsigned long maxlen,
114 const char *name, int req_flags)
116 resource_size_t start = pci_resource_start(pdev, bar);
117 resource_size_t len = pci_resource_len(pdev, bar);
118 unsigned long dev_flags = pci_resource_flags(pdev, bar);
120 if (start == 0 || len == 0) /* Unused BAR. */
128 if (len > maxlen && maxlen != 0)
131 if (dev_flags & IORESOURCE_IO) {
132 if (!request_region(start, len, name))
134 } else if (dev_flags & IORESOURCE_MEM) {
135 if (!__request_mem_region(start, len, name, req_flags))
138 /* That's not a device we can request anything on. */
145 static void __pcim_release_region_range(struct pci_dev *pdev, int bar,
146 unsigned long offset,
147 unsigned long maxlen)
149 resource_size_t start = pci_resource_start(pdev, bar);
150 resource_size_t len = pci_resource_len(pdev, bar);
151 unsigned long flags = pci_resource_flags(pdev, bar);
153 if (len <= offset || start == 0)
156 if (len == 0 || maxlen == 0) /* This an unused BAR. Do nothing. */
165 if (flags & IORESOURCE_IO)
166 release_region(start, len);
167 else if (flags & IORESOURCE_MEM)
168 release_mem_region(start, len);
171 static int __pcim_request_region(struct pci_dev *pdev, int bar,
172 const char *name, int flags)
174 unsigned long offset = 0;
175 unsigned long len = pci_resource_len(pdev, bar);
177 return __pcim_request_region_range(pdev, bar, offset, len, name, flags);
180 static void __pcim_release_region(struct pci_dev *pdev, int bar)
182 unsigned long offset = 0;
183 unsigned long len = pci_resource_len(pdev, bar);
185 __pcim_release_region_range(pdev, bar, offset, len);
188 static void pcim_addr_resource_release(struct device *dev, void *resource_raw)
190 struct pci_dev *pdev = to_pci_dev(dev);
191 struct pcim_addr_devres *res = resource_raw;
194 case PCIM_ADDR_DEVRES_TYPE_REGION:
195 __pcim_release_region(pdev, res->bar);
197 case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING:
198 pci_iounmap(pdev, res->baseaddr);
199 __pcim_release_region(pdev, res->bar);
201 case PCIM_ADDR_DEVRES_TYPE_MAPPING:
202 pci_iounmap(pdev, res->baseaddr);
209 static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pdev)
211 struct pcim_addr_devres *res;
213 res = devres_alloc_node(pcim_addr_resource_release, sizeof(*res),
214 GFP_KERNEL, dev_to_node(&pdev->dev));
216 pcim_addr_devres_clear(res);
220 /* Just for consistency and readability. */
221 static inline void pcim_addr_devres_free(struct pcim_addr_devres *res)
227 * Used by devres to identify a pcim_addr_devres.
229 static int pcim_addr_resources_match(struct device *dev,
230 void *a_raw, void *b_raw)
232 struct pcim_addr_devres *a, *b;
237 if (a->type != b->type)
241 case PCIM_ADDR_DEVRES_TYPE_REGION:
242 case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING:
243 return a->bar == b->bar;
244 case PCIM_ADDR_DEVRES_TYPE_MAPPING:
245 return a->baseaddr == b->baseaddr;
251 static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
253 struct resource **res = ptr;
255 pci_unmap_iospace(*res);
259 * devm_pci_remap_iospace - Managed pci_remap_iospace()
260 * @dev: Generic device to remap IO address for
261 * @res: Resource describing the I/O space
262 * @phys_addr: physical address of range to be mapped
264 * Managed pci_remap_iospace(). Map is automatically unmapped on driver
267 int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
268 phys_addr_t phys_addr)
270 const struct resource **ptr;
273 ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
277 error = pci_remap_iospace(res, phys_addr);
282 devres_add(dev, ptr);
287 EXPORT_SYMBOL(devm_pci_remap_iospace);
290 * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
291 * @dev: Generic device to remap IO address for
292 * @offset: Resource address to map
295 * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
298 void __iomem *devm_pci_remap_cfgspace(struct device *dev,
299 resource_size_t offset,
300 resource_size_t size)
302 void __iomem **ptr, *addr;
304 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
308 addr = pci_remap_cfgspace(offset, size);
311 devres_add(dev, ptr);
317 EXPORT_SYMBOL(devm_pci_remap_cfgspace);
320 * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
321 * @dev: generic device to handle the resource for
322 * @res: configuration space resource to be handled
324 * Checks that a resource is a valid memory region, requests the memory
325 * region and ioremaps with pci_remap_cfgspace() API that ensures the
326 * proper PCI configuration space memory attributes are guaranteed.
328 * All operations are managed and will be undone on driver detach.
330 * Returns a pointer to the remapped memory or an IOMEM_ERR_PTR() encoded error
331 * code on failure. Usage example::
333 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
334 * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
336 * return PTR_ERR(base);
338 void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
339 struct resource *res)
341 resource_size_t size;
343 void __iomem *dest_ptr;
347 if (!res || resource_type(res) != IORESOURCE_MEM) {
348 dev_err(dev, "invalid resource\n");
349 return IOMEM_ERR_PTR(-EINVAL);
352 size = resource_size(res);
355 name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev),
358 name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
360 return IOMEM_ERR_PTR(-ENOMEM);
362 if (!devm_request_mem_region(dev, res->start, size, name)) {
363 dev_err(dev, "can't request region for resource %pR\n", res);
364 return IOMEM_ERR_PTR(-EBUSY);
367 dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
369 dev_err(dev, "ioremap failed for resource %pR\n", res);
370 devm_release_mem_region(dev, res->start, size);
371 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
376 EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
378 static void __pcim_clear_mwi(void *pdev_raw)
380 struct pci_dev *pdev = pdev_raw;
386 * pcim_set_mwi - a device-managed pci_set_mwi()
387 * @pdev: the PCI device for which MWI is enabled
389 * Managed pci_set_mwi().
391 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
393 int pcim_set_mwi(struct pci_dev *pdev)
397 ret = devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev);
401 ret = pci_set_mwi(pdev);
403 devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev);
407 EXPORT_SYMBOL(pcim_set_mwi);
409 static inline bool mask_contains_bar(int mask, int bar)
411 return mask & BIT(bar);
415 * This is a copy of pci_intx() used to bypass the problem of recursive
416 * function calls due to the hybrid nature of pci_intx().
418 static void __pcim_intx(struct pci_dev *pdev, int enable)
420 u16 pci_command, new;
422 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
425 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
427 new = pci_command | PCI_COMMAND_INTX_DISABLE;
429 if (new != pci_command)
430 pci_write_config_word(pdev, PCI_COMMAND, new);
433 static void pcim_intx_restore(struct device *dev, void *data)
435 struct pci_dev *pdev = to_pci_dev(dev);
436 struct pcim_intx_devres *res = data;
438 __pcim_intx(pdev, res->orig_intx);
441 static struct pcim_intx_devres *get_or_create_intx_devres(struct device *dev)
443 struct pcim_intx_devres *res;
445 res = devres_find(dev, pcim_intx_restore, NULL, NULL);
449 res = devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL);
451 devres_add(dev, res);
457 * pcim_intx - managed pci_intx()
458 * @pdev: the PCI device to operate on
459 * @enable: boolean: whether to enable or disable PCI INTx
461 * Returns: 0 on success, -ENOMEM on error.
463 * Enable/disable PCI INTx for device @pdev.
464 * Restore the original state on driver detach.
466 int pcim_intx(struct pci_dev *pdev, int enable)
468 struct pcim_intx_devres *res;
470 res = get_or_create_intx_devres(&pdev->dev);
474 res->orig_intx = !enable;
475 __pcim_intx(pdev, enable);
480 static void pcim_disable_device(void *pdev_raw)
482 struct pci_dev *pdev = pdev_raw;
485 pci_disable_device(pdev);
489 * pcim_enable_device - Managed pci_enable_device()
490 * @pdev: PCI device to be initialized
492 * Returns: 0 on success, negative error code on failure.
494 * Managed pci_enable_device(). Device will automatically be disabled on
497 int pcim_enable_device(struct pci_dev *pdev)
501 ret = devm_add_action(&pdev->dev, pcim_disable_device, pdev);
506 * We prefer removing the action in case of an error over
507 * devm_add_action_or_reset() because the latter could theoretically be
508 * disturbed by users having pinned the device too soon.
510 ret = pci_enable_device(pdev);
512 devm_remove_action(&pdev->dev, pcim_disable_device, pdev);
516 pdev->is_managed = true;
520 EXPORT_SYMBOL(pcim_enable_device);
523 * pcim_pin_device - Pin managed PCI device
524 * @pdev: PCI device to pin
526 * Pin managed PCI device @pdev. Pinned device won't be disabled on driver
527 * detach. @pdev must have been enabled with pcim_enable_device().
529 void pcim_pin_device(struct pci_dev *pdev)
533 EXPORT_SYMBOL(pcim_pin_device);
535 static void pcim_iomap_release(struct device *gendev, void *res)
538 * Do nothing. This is legacy code.
540 * Cleanup of the mappings is now done directly through the callbacks
541 * registered when creating them.
546 * pcim_iomap_table - access iomap allocation table (DEPRECATED)
547 * @pdev: PCI device to access iomap table for
550 * Const pointer to array of __iomem pointers on success, NULL on failure.
552 * Access iomap allocation table for @dev. If iomap table doesn't
553 * exist and @pdev is managed, it will be allocated. All iomaps
554 * recorded in the iomap table are automatically unmapped on driver
557 * This function might sleep when the table is first allocated but can
558 * be safely called without context and guaranteed to succeed once
561 * This function is DEPRECATED. Do not use it in new code. Instead, obtain a
562 * mapping's address directly from one of the pcim_* mapping functions. For
564 * void __iomem \*mappy = pcim_iomap(pdev, bar, length);
566 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
568 struct pcim_iomap_devres *dr, *new_dr;
570 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
574 new_dr = devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL,
575 dev_to_node(&pdev->dev));
578 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
581 EXPORT_SYMBOL(pcim_iomap_table);
584 * Fill the legacy mapping-table, so that drivers using the old API can
585 * still get a BAR's mapping address through pcim_iomap_table().
587 static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev,
588 void __iomem *mapping, int bar)
590 void __iomem **legacy_iomap_table;
592 if (bar >= PCI_STD_NUM_BARS)
595 legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
596 if (!legacy_iomap_table)
599 /* The legacy mechanism doesn't allow for duplicate mappings. */
600 WARN_ON(legacy_iomap_table[bar]);
602 legacy_iomap_table[bar] = mapping;
608 * Remove a mapping. The table only contains whole-BAR mappings, so this will
609 * never interfere with ranged mappings.
611 static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev,
615 void __iomem **legacy_iomap_table;
617 legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
618 if (!legacy_iomap_table)
621 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
622 if (legacy_iomap_table[bar] == addr) {
623 legacy_iomap_table[bar] = NULL;
630 * The same as pcim_remove_mapping_from_legacy_table(), but identifies the
631 * mapping by its BAR index.
633 static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, int bar)
635 void __iomem **legacy_iomap_table;
637 if (bar >= PCI_STD_NUM_BARS)
640 legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
641 if (!legacy_iomap_table)
644 legacy_iomap_table[bar] = NULL;
648 * pcim_iomap - Managed pcim_iomap()
649 * @pdev: PCI device to iomap for
651 * @maxlen: Maximum length of iomap
653 * Returns: __iomem pointer on success, NULL on failure.
655 * Managed pci_iomap(). Map is automatically unmapped on driver detach. If
656 * desired, unmap manually only with pcim_iounmap().
658 * This SHOULD only be used once per BAR.
661 * Contrary to the other pcim_* functions, this function does not return an
662 * IOMEM_ERR_PTR() on failure, but a simple NULL. This is done for backwards
665 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
667 void __iomem *mapping;
668 struct pcim_addr_devres *res;
670 res = pcim_addr_devres_alloc(pdev);
673 res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
675 mapping = pci_iomap(pdev, bar, maxlen);
678 res->baseaddr = mapping;
680 if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0)
683 devres_add(&pdev->dev, res);
687 pci_iounmap(pdev, mapping);
689 pcim_addr_devres_free(res);
692 EXPORT_SYMBOL(pcim_iomap);
695 * pcim_iounmap - Managed pci_iounmap()
696 * @pdev: PCI device to iounmap for
697 * @addr: Address to unmap
699 * Managed pci_iounmap(). @addr must have been mapped using a pcim_* mapping
702 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
704 struct pcim_addr_devres res_searched;
706 pcim_addr_devres_clear(&res_searched);
707 res_searched.type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
708 res_searched.baseaddr = addr;
710 if (devres_release(&pdev->dev, pcim_addr_resource_release,
711 pcim_addr_resources_match, &res_searched) != 0) {
712 /* Doesn't exist. User passed nonsense. */
716 pcim_remove_mapping_from_legacy_table(pdev, addr);
718 EXPORT_SYMBOL(pcim_iounmap);
721 * pcim_iomap_region - Request and iomap a PCI BAR
722 * @pdev: PCI device to map IO resources for
723 * @bar: Index of a BAR to map
724 * @name: Name associated with the request
726 * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure.
728 * Mapping and region will get automatically released on driver detach. If
729 * desired, release manually only with pcim_iounmap_region().
731 static void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar,
735 struct pcim_addr_devres *res;
737 res = pcim_addr_devres_alloc(pdev);
739 return IOMEM_ERR_PTR(-ENOMEM);
741 res->type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING;
744 ret = __pcim_request_region(pdev, bar, name, 0);
748 res->baseaddr = pci_iomap(pdev, bar, 0);
749 if (!res->baseaddr) {
754 devres_add(&pdev->dev, res);
755 return res->baseaddr;
758 __pcim_release_region(pdev, bar);
760 pcim_addr_devres_free(res);
762 return IOMEM_ERR_PTR(ret);
766 * pcim_iounmap_region - Unmap and release a PCI BAR
767 * @pdev: PCI device to operate on
768 * @bar: Index of BAR to unmap and release
770 * Unmap a BAR and release its region manually. Only pass BARs that were
771 * previously mapped by pcim_iomap_region().
773 static void pcim_iounmap_region(struct pci_dev *pdev, int bar)
775 struct pcim_addr_devres res_searched;
777 pcim_addr_devres_clear(&res_searched);
778 res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING;
779 res_searched.bar = bar;
781 devres_release(&pdev->dev, pcim_addr_resource_release,
782 pcim_addr_resources_match, &res_searched);
786 * pcim_iomap_regions - Request and iomap PCI BARs
787 * @pdev: PCI device to map IO resources for
788 * @mask: Mask of BARs to request and iomap
789 * @name: Name associated with the requests
791 * Returns: 0 on success, negative error code on failure.
793 * Request and iomap regions specified by @mask.
795 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
799 void __iomem *mapping;
801 for (bar = 0; bar < DEVICE_COUNT_RESOURCE; bar++) {
802 if (!mask_contains_bar(mask, bar))
805 mapping = pcim_iomap_region(pdev, bar, name);
806 if (IS_ERR(mapping)) {
807 ret = PTR_ERR(mapping);
810 ret = pcim_add_mapping_to_legacy_table(pdev, mapping, bar);
819 pcim_iounmap_region(pdev, bar);
820 pcim_remove_bar_from_legacy_table(pdev, bar);
825 EXPORT_SYMBOL(pcim_iomap_regions);
827 static int _pcim_request_region(struct pci_dev *pdev, int bar, const char *name,
831 struct pcim_addr_devres *res;
833 res = pcim_addr_devres_alloc(pdev);
836 res->type = PCIM_ADDR_DEVRES_TYPE_REGION;
839 ret = __pcim_request_region(pdev, bar, name, request_flags);
841 pcim_addr_devres_free(res);
845 devres_add(&pdev->dev, res);
850 * pcim_request_region - Request a PCI BAR
851 * @pdev: PCI device to requestion region for
852 * @bar: Index of BAR to request
853 * @name: Name associated with the request
855 * Returns: 0 on success, a negative error code on failure.
857 * Request region specified by @bar.
859 * The region will automatically be released on driver detach. If desired,
860 * release manually only with pcim_release_region().
862 int pcim_request_region(struct pci_dev *pdev, int bar, const char *name)
864 return _pcim_request_region(pdev, bar, name, 0);
868 * pcim_request_region_exclusive - Request a PCI BAR exclusively
869 * @pdev: PCI device to requestion region for
870 * @bar: Index of BAR to request
871 * @name: Name associated with the request
873 * Returns: 0 on success, a negative error code on failure.
875 * Request region specified by @bar exclusively.
877 * The region will automatically be released on driver detach. If desired,
878 * release manually only with pcim_release_region().
880 int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name)
882 return _pcim_request_region(pdev, bar, name, IORESOURCE_EXCLUSIVE);
886 * pcim_release_region - Release a PCI BAR
887 * @pdev: PCI device to operate on
888 * @bar: Index of BAR to release
890 * Release a region manually that was previously requested by
891 * pcim_request_region().
893 void pcim_release_region(struct pci_dev *pdev, int bar)
895 struct pcim_addr_devres res_searched;
897 pcim_addr_devres_clear(&res_searched);
898 res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION;
899 res_searched.bar = bar;
901 devres_release(&pdev->dev, pcim_addr_resource_release,
902 pcim_addr_resources_match, &res_searched);
907 * pcim_release_all_regions - Release all regions of a PCI-device
908 * @pdev: the PCI device
910 * Release all regions previously requested through pcim_request_region()
911 * or pcim_request_all_regions().
913 * Can be called from any context, i.e., not necessarily as a counterpart to
914 * pcim_request_all_regions().
916 static void pcim_release_all_regions(struct pci_dev *pdev)
920 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
921 pcim_release_region(pdev, bar);
925 * pcim_request_all_regions - Request all regions
926 * @pdev: PCI device to map IO resources for
927 * @name: name associated with the request
929 * Returns: 0 on success, negative error code on failure.
931 * Requested regions will automatically be released at driver detach. If
932 * desired, release individual regions with pcim_release_region() or all of
933 * them at once with pcim_release_all_regions().
935 static int pcim_request_all_regions(struct pci_dev *pdev, const char *name)
940 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
941 ret = pcim_request_region(pdev, bar, name);
949 pcim_release_all_regions(pdev);
955 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
957 * @pdev: PCI device to map IO resources for
958 * @mask: Mask of BARs to iomap
959 * @name: Name associated with the requests
961 * Returns: 0 on success, negative error code on failure.
963 * Request all PCI BARs and iomap regions specified by @mask.
965 * To release these resources manually, call pcim_release_region() for the
966 * regions and pcim_iounmap() for the mappings.
968 * This function is DEPRECATED. Don't use it in new code. Instead, use one
969 * of the pcim_* region request functions in combination with a pcim_*
972 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
977 void __iomem **legacy_iomap_table;
979 ret = pcim_request_all_regions(pdev, name);
983 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
984 if (!mask_contains_bar(mask, bar))
986 if (!pcim_iomap(pdev, bar, 0))
994 * If bar is larger than 0, then pcim_iomap() above has most likely
995 * failed because of -EINVAL. If it is equal 0, most likely the table
996 * couldn't be created, indicating -ENOMEM.
998 ret = bar > 0 ? -EINVAL : -ENOMEM;
999 legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
1002 pcim_iounmap(pdev, legacy_iomap_table[bar]);
1004 pcim_release_all_regions(pdev);
1008 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
1011 * pcim_iounmap_regions - Unmap and release PCI BARs
1012 * @pdev: PCI device to map IO resources for
1013 * @mask: Mask of BARs to unmap and release
1015 * Unmap and release regions specified by @mask.
1017 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
1021 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1022 if (!mask_contains_bar(mask, i))
1025 pcim_iounmap_region(pdev, i);
1026 pcim_remove_bar_from_legacy_table(pdev, i);
1029 EXPORT_SYMBOL(pcim_iounmap_regions);
1032 * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR
1033 * @pdev: PCI device to map IO resources for
1034 * @bar: Index of the BAR
1035 * @offset: Offset from the begin of the BAR
1036 * @len: Length in bytes for the mapping
1038 * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure.
1040 * Creates a new IO-Mapping within the specified @bar, ranging from @offset to
1043 * The mapping will automatically get unmapped on driver detach. If desired,
1044 * release manually only with pcim_iounmap().
1046 void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar,
1047 unsigned long offset, unsigned long len)
1049 void __iomem *mapping;
1050 struct pcim_addr_devres *res;
1052 res = pcim_addr_devres_alloc(pdev);
1054 return IOMEM_ERR_PTR(-ENOMEM);
1056 mapping = pci_iomap_range(pdev, bar, offset, len);
1058 pcim_addr_devres_free(res);
1059 return IOMEM_ERR_PTR(-EINVAL);
1062 res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
1063 res->baseaddr = mapping;
1066 * Ranged mappings don't get added to the legacy-table, since the table
1067 * only ever keeps track of whole BARs.
1070 devres_add(&pdev->dev, res);
1073 EXPORT_SYMBOL(pcim_iomap_range);