// SPDX-License-Identifier: GPL-2.0 #include <linux/device.h> #include <linux/pci.h> #include "pci.h" /* * On the state of PCI's devres implementation: * * The older devres API for PCI has two significant problems: * * 1. It is very strongly tied to the statically allocated mapping table in * struct pcim_iomap_devres below. This is mostly solved in the sense of the * pcim_ functions in this file providing things like ranged mapping by * bypassing this table, whereas the functions that were present in the old * API still enter the mapping addresses into the table for users of the old * API. * * 2. The region-request-functions in pci.c do become managed IF the device has * been enabled with pcim_enable_device() instead of pci_enable_device(). * This resulted in the API becoming inconsistent: Some functions have an * obviously managed counter-part (e.g., pci_iomap() <-> pcim_iomap()), * whereas some don't and are never managed, while others don't and are * _sometimes_ managed (e.g. pci_request_region()). * * Consequently, in the new API, region requests performed by the pcim_ * functions are automatically cleaned up through the devres callback * pcim_addr_resource_release(). * * Users of pcim_enable_device() + pci_*region*() are redirected in * pci.c to the managed functions here in this file. This isn't exactly * perfect, but the only alternative way would be to port ALL drivers * using said combination to pcim_ functions. * * TODO: * Remove the legacy table entirely once all calls to pcim_iomap_table() in * the kernel have been removed. */ /* * Legacy struct storing addresses to whole mapped BARs. */ struct pcim_iomap_devres { … }; /* Used to restore the old INTx state on driver detach. */ struct pcim_intx_devres { … }; enum pcim_addr_devres_type { … }; /* * This struct envelops IO or MEM addresses, i.e., mappings and region * requests, because those are very frequently requested and released * together. */ struct pcim_addr_devres { … }; static inline void pcim_addr_devres_clear(struct pcim_addr_devres *res) { … } /* * The following functions, __pcim_*_region*, exist as counterparts to the * versions from pci.c - which, unfortunately, can be in "hybrid mode", i.e., * sometimes managed, sometimes not. * * To separate the APIs cleanly, we define our own, simplified versions here. */ /** * __pcim_request_region_range - Request a ranged region * @pdev: PCI device the region belongs to * @bar: BAR the range is within * @offset: offset from the BAR's start address * @maxlen: length in bytes, beginning at @offset * @name: name associated with the request * @req_flags: flags for the request, e.g., for kernel-exclusive requests * * Returns: 0 on success, a negative error code on failure. * * Request a range within a device's PCI BAR. Sanity check the input. */ static int __pcim_request_region_range(struct pci_dev *pdev, int bar, unsigned long offset, unsigned long maxlen, const char *name, int req_flags) { … } static void __pcim_release_region_range(struct pci_dev *pdev, int bar, unsigned long offset, unsigned long maxlen) { … } static int __pcim_request_region(struct pci_dev *pdev, int bar, const char *name, int flags) { … } static void __pcim_release_region(struct pci_dev *pdev, int bar) { … } static void pcim_addr_resource_release(struct device *dev, void *resource_raw) { … } static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pdev) { … } /* Just for consistency and readability. */ static inline void pcim_addr_devres_free(struct pcim_addr_devres *res) { … } /* * Used by devres to identify a pcim_addr_devres. */ static int pcim_addr_resources_match(struct device *dev, void *a_raw, void *b_raw) { … } static void devm_pci_unmap_iospace(struct device *dev, void *ptr) { … } /** * devm_pci_remap_iospace - Managed pci_remap_iospace() * @dev: Generic device to remap IO address for * @res: Resource describing the I/O space * @phys_addr: physical address of range to be mapped * * Managed pci_remap_iospace(). Map is automatically unmapped on driver * detach. */ int devm_pci_remap_iospace(struct device *dev, const struct resource *res, phys_addr_t phys_addr) { … } EXPORT_SYMBOL(…); /** * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace() * @dev: Generic device to remap IO address for * @offset: Resource address to map * @size: Size of map * * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver * detach. */ void __iomem *devm_pci_remap_cfgspace(struct device *dev, resource_size_t offset, resource_size_t size) { … } EXPORT_SYMBOL(…); /** * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource * @dev: generic device to handle the resource for * @res: configuration space resource to be handled * * Checks that a resource is a valid memory region, requests the memory * region and ioremaps with pci_remap_cfgspace() API that ensures the * proper PCI configuration space memory attributes are guaranteed. * * All operations are managed and will be undone on driver detach. * * Returns a pointer to the remapped memory or an IOMEM_ERR_PTR() encoded error * code on failure. Usage example:: * * res = platform_get_resource(pdev, IORESOURCE_MEM, 0); * base = devm_pci_remap_cfg_resource(&pdev->dev, res); * if (IS_ERR(base)) * return PTR_ERR(base); */ void __iomem *devm_pci_remap_cfg_resource(struct device *dev, struct resource *res) { … } EXPORT_SYMBOL(…); static void __pcim_clear_mwi(void *pdev_raw) { … } /** * pcim_set_mwi - a device-managed pci_set_mwi() * @pdev: the PCI device for which MWI is enabled * * Managed pci_set_mwi(). * * RETURNS: An appropriate -ERRNO error value on error, or zero for success. */ int pcim_set_mwi(struct pci_dev *pdev) { … } EXPORT_SYMBOL(…); static inline bool mask_contains_bar(int mask, int bar) { … } /* * This is a copy of pci_intx() used to bypass the problem of recursive * function calls due to the hybrid nature of pci_intx(). */ static void __pcim_intx(struct pci_dev *pdev, int enable) { … } static void pcim_intx_restore(struct device *dev, void *data) { … } static struct pcim_intx_devres *get_or_create_intx_devres(struct device *dev) { … } /** * pcim_intx - managed pci_intx() * @pdev: the PCI device to operate on * @enable: boolean: whether to enable or disable PCI INTx * * Returns: 0 on success, -ENOMEM on error. * * Enable/disable PCI INTx for device @pdev. * Restore the original state on driver detach. */ int pcim_intx(struct pci_dev *pdev, int enable) { … } static void pcim_disable_device(void *pdev_raw) { … } /** * pcim_enable_device - Managed pci_enable_device() * @pdev: PCI device to be initialized * * Returns: 0 on success, negative error code on failure. * * Managed pci_enable_device(). Device will automatically be disabled on * driver detach. */ int pcim_enable_device(struct pci_dev *pdev) { … } EXPORT_SYMBOL(…); /** * pcim_pin_device - Pin managed PCI device * @pdev: PCI device to pin * * Pin managed PCI device @pdev. Pinned device won't be disabled on driver * detach. @pdev must have been enabled with pcim_enable_device(). */ void pcim_pin_device(struct pci_dev *pdev) { … } EXPORT_SYMBOL(…); static void pcim_iomap_release(struct device *gendev, void *res) { … } /** * pcim_iomap_table - access iomap allocation table (DEPRECATED) * @pdev: PCI device to access iomap table for * * Returns: * Const pointer to array of __iomem pointers on success, NULL on failure. * * Access iomap allocation table for @dev. If iomap table doesn't * exist and @pdev is managed, it will be allocated. All iomaps * recorded in the iomap table are automatically unmapped on driver * detach. * * This function might sleep when the table is first allocated but can * be safely called without context and guaranteed to succeed once * allocated. * * This function is DEPRECATED. Do not use it in new code. Instead, obtain a * mapping's address directly from one of the pcim_* mapping functions. For * example: * void __iomem \*mappy = pcim_iomap(pdev, bar, length); */ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) { … } EXPORT_SYMBOL(…); /* * Fill the legacy mapping-table, so that drivers using the old API can * still get a BAR's mapping address through pcim_iomap_table(). */ static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev, void __iomem *mapping, int bar) { … } /* * Remove a mapping. The table only contains whole-BAR mappings, so this will * never interfere with ranged mappings. */ static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev, void __iomem *addr) { … } /* * The same as pcim_remove_mapping_from_legacy_table(), but identifies the * mapping by its BAR index. */ static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, int bar) { … } /** * pcim_iomap - Managed pcim_iomap() * @pdev: PCI device to iomap for * @bar: BAR to iomap * @maxlen: Maximum length of iomap * * Returns: __iomem pointer on success, NULL on failure. * * Managed pci_iomap(). Map is automatically unmapped on driver detach. If * desired, unmap manually only with pcim_iounmap(). * * This SHOULD only be used once per BAR. * * NOTE: * Contrary to the other pcim_* functions, this function does not return an * IOMEM_ERR_PTR() on failure, but a simple NULL. This is done for backwards * compatibility. */ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen) { … } EXPORT_SYMBOL(…); /** * pcim_iounmap - Managed pci_iounmap() * @pdev: PCI device to iounmap for * @addr: Address to unmap * * Managed pci_iounmap(). @addr must have been mapped using a pcim_* mapping * function. */ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) { … } EXPORT_SYMBOL(…); /** * pcim_iomap_region - Request and iomap a PCI BAR * @pdev: PCI device to map IO resources for * @bar: Index of a BAR to map * @name: Name associated with the request * * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. * * Mapping and region will get automatically released on driver detach. If * desired, release manually only with pcim_iounmap_region(). */ static void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar, const char *name) { … } /** * pcim_iounmap_region - Unmap and release a PCI BAR * @pdev: PCI device to operate on * @bar: Index of BAR to unmap and release * * Unmap a BAR and release its region manually. Only pass BARs that were * previously mapped by pcim_iomap_region(). */ static void pcim_iounmap_region(struct pci_dev *pdev, int bar) { … } /** * pcim_iomap_regions - Request and iomap PCI BARs * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to request and iomap * @name: Name associated with the requests * * Returns: 0 on success, negative error code on failure. * * Request and iomap regions specified by @mask. */ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) { … } EXPORT_SYMBOL(…); static int _pcim_request_region(struct pci_dev *pdev, int bar, const char *name, int request_flags) { … } /** * pcim_request_region - Request a PCI BAR * @pdev: PCI device to requestion region for * @bar: Index of BAR to request * @name: Name associated with the request * * Returns: 0 on success, a negative error code on failure. * * Request region specified by @bar. * * The region will automatically be released on driver detach. If desired, * release manually only with pcim_release_region(). */ int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) { … } /** * pcim_request_region_exclusive - Request a PCI BAR exclusively * @pdev: PCI device to requestion region for * @bar: Index of BAR to request * @name: Name associated with the request * * Returns: 0 on success, a negative error code on failure. * * Request region specified by @bar exclusively. * * The region will automatically be released on driver detach. If desired, * release manually only with pcim_release_region(). */ int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name) { … } /** * pcim_release_region - Release a PCI BAR * @pdev: PCI device to operate on * @bar: Index of BAR to release * * Release a region manually that was previously requested by * pcim_request_region(). */ void pcim_release_region(struct pci_dev *pdev, int bar) { … } /** * pcim_release_all_regions - Release all regions of a PCI-device * @pdev: the PCI device * * Release all regions previously requested through pcim_request_region() * or pcim_request_all_regions(). * * Can be called from any context, i.e., not necessarily as a counterpart to * pcim_request_all_regions(). */ static void pcim_release_all_regions(struct pci_dev *pdev) { … } /** * pcim_request_all_regions - Request all regions * @pdev: PCI device to map IO resources for * @name: name associated with the request * * Returns: 0 on success, negative error code on failure. * * Requested regions will automatically be released at driver detach. If * desired, release individual regions with pcim_release_region() or all of * them at once with pcim_release_all_regions(). */ static int pcim_request_all_regions(struct pci_dev *pdev, const char *name) { … } /** * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones * (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to iomap * @name: Name associated with the requests * * Returns: 0 on success, negative error code on failure. * * Request all PCI BARs and iomap regions specified by @mask. * * To release these resources manually, call pcim_release_region() for the * regions and pcim_iounmap() for the mappings. * * This function is DEPRECATED. Don't use it in new code. Instead, use one * of the pcim_* region request functions in combination with a pcim_* * mapping function. */ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name) { … } EXPORT_SYMBOL(…); /** * pcim_iounmap_regions - Unmap and release PCI BARs * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to unmap and release * * Unmap and release regions specified by @mask. */ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) { … } EXPORT_SYMBOL(…); /** * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR * @pdev: PCI device to map IO resources for * @bar: Index of the BAR * @offset: Offset from the begin of the BAR * @len: Length in bytes for the mapping * * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. * * Creates a new IO-Mapping within the specified @bar, ranging from @offset to * @offset + @len. * * The mapping will automatically get unmapped on driver detach. If desired, * release manually only with pcim_iounmap(). */ void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, unsigned long offset, unsigned long len) { … } EXPORT_SYMBOL(…);