Make PCI's devres API more consistent
Subsystems (4)
v7 cover Make PCI's devres API more consistent
Hello Bjorn,
I tried to meet your requests from the last feedback round as much as possible. Especially, I removed a lot of code, made almost all interfaces private and cut the series into smaller chunks where possible.
Splitting it even smaller is unfortunately not possible because of the Linux kernel build chain's rule on dead / unused code.
See also the changelog below.
Please tell me if that's enough to move forward here.
Regards,
P.
Changes in v7:
- Split the entire series in smaller, more atomic chunks / patches
(Bjorn)
- Remove functions (such as pcim_iomap_region_range()) that do not yet
have a user (Bjorn)
- Don't export interfaces publicly anymore, except for
pcim_iomap_range(), needed by vboxvideo (Bjorn)
- Mention the actual (vboxvideo) bug in "PCI: Warn users..." commit
(Bjorn) - Drop docstring warnings on PCI-internal functions (Bjorn)
- Rework docstring warnings
- Fix spelling in a few places. Rewrapp paragraphs (Bjorn)
Changes in v6:
- Restructure the cleanup in pcim_iomap_regions_request_all() so that it doesn't trigger a (false positive) test robot warning. No behavior change intended. (Dan Carpenter)
Changes in v5:
- Add Hans's Reviewed-by to vboxvideo patch (Hans de Goede)
- Remove stable-kernel from CC in vboxvideo patch (Hans de Goede)
Changes in v4:
- Rebase against linux-next
Changes in v3:
- Use the term "PCI devres API" at some forgotten places.
- Fix more grammar errors in patch #3.
- Remove the comment advising to call (the outdated) pcim_intx() in pci.c
- Rename __pcim_request_region_range() flags-field "exclusive" to "req_flags", since this is what the int actually represents.
- Remove the call to pcim_region_request() from patch #10. (Hans)
Changes in v2:
- Make commit head lines congruent with PCI's style (Bjorn)
- Add missing error checks for devm_add_action(). (Andy)
- Repair the "Returns: " marks for docu generation (Andy)
- Initialize the addr_devres struct with memset(). (Andy)
- Make pcim_intx() a PCI-internal function so that new drivers won't be encouraged to use the outdated pci_intx() mechanism. (Andy / Philipp)
- Fix grammar and spelling (Bjorn)
- Be more precise on why pcim_iomap_table() is problematic (Bjorn)
- Provide the actual structs' and functions' names in the commit messages (Bjorn)
- Remove redundant variable initializers (Andy)
- Regroup PM bitfield members in struct pci_dev (Andy)
- Make pcim_intx() visible only for the PCI subsystem so that new drivers won't use this outdated API (Andy, Myself)
- Add a NOTE to pcim_iomap() to warn about this function being the onee
xception that does just return NULL.
- Consistently use the term "PCI devres API"; also in Patch #10 (Bjorn)
¡Hola!
PCI's devres API suffers several weaknesses:
1. There are functions prefixed with pcim_. Those are always managed counterparts to never-managed functions prefixed with pci_ – or so one would like to think. There are some apparently unmanaged functions (all region-request / release functions, and pci_intx()) which suddenly become managed once the user has initialized the device with pcim_enable_device() instead of pci_enable_device(). This "sometimes yes, sometimes no" nature of those functions is confusing and therefore bug-provoking. In fact, it has already caused a bug in DRM. The last patch in this series fixes that bug.
2. iomappings: Instead of giving each mapping its own callback, the existing API uses a statically allocated struct tracking one mapping per bar. This is not extensible. Especially, you can't create _ranged_ managed mappings that way, which many drivers want.
3. Managed request functions only exist as "plural versions" with a bit-mask as a parameter. That's quite over-engineered considering that each user only ever mapps one, maybe two bars.
This series:
- add a set of new "singular" devres functions that use devres the way its intended, with one callback per resource.
- deprecates the existing iomap-table mechanism.
- deprecates the hybrid nature of pci_ functions.
- preserves backwards compatibility so that drivers using the existing API won't notice any changes.
- adds documentation, especially some warning users about the complicated nature of PCI's devres.
Note that this series is based on my "unify pci_iounmap"-series from a few weeks ago. [1]
I tested this on a x86 VM with a simple pci test-device with two regions. Operates and reserves resources as intended on my system. Kasan and kmemleak didn't find any problems.
I believe this series cleans the API up as much as possible without having to port all existing drivers to the new API. Especially, I think that this implementation is easy to extend if the need for new managed functions arises :)
Greetings,
P.
Philipp Stanner (13):
PCI: Add and use devres helper for bit masks
PCI: Add devres helpers for iomap table
PCI: Reimplement plural devres functions
PCI: Deprecate two surplus devres functions
PCI: Make devres region requests consistent
PCI: Warn users about complicated devres nature
PCI: Move dev-enabled status bit to struct pci_dev
PCI: Move pinned status bit to struct pci_dev
PCI: Give pcim_set_mwi() its own devres callback
PCI: Give pci(m)_intx its own devres callback
PCI: Remove legacy pcim_release()
PCI: Add pcim_iomap_range()
drm/vboxvideo: fix mapping leaks
drivers/gpu/drm/vboxvideo/vbox_main.c | 20 +- drivers/pci/devres.c | 897 +++++++++++++++++++++----- drivers/pci/iomap.c | 16 + drivers/pci/pci.c | 107 ++- drivers/pci/pci.h | 23 +- include/linux/pci.h | 6 +- 6 files changed, 864 insertions(+), 205 deletions(-)
Signature · 1 line
2.45.0
PATCH v7 2/13 PCI: Add devres helpers for iomap table
The iomap-table administrated by pcim_iomap_table() has its entries set and unset at several places throughout devres.c using manual iterations which are effectively code duplications.
This can be done in a centralized, reusable manner.
Providing these new functions here and using them where (already) possible will allow for using them in subsequent cleanup steps to simplify the PCI devres API.
Implement helper functions to add mappings to the table and to remove them again. Use them where applicable.
--- drivers/pci/devres.c | 77 +++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 19 deletions(-)
drivers/pci/devres.c +58 -19
@@ -297,6 +297,52 @@ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) }EXPORT_SYMBOL (pcim_iomap_table); +/* +* 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,short bar) +{ +void __iomem **legacy_iomap_table; + +if (bar >=PCI_STD_NUM_BARS ) +return -EINVAL ; + + legacy_iomap_table = (void __iomem **)pcim_iomap_table (pdev); +if (!legacy_iomap_table) +return -ENOMEM ; + +/* The legacy mechanism doesn't allow for duplicate mappings. */ +WARN_ON (legacy_iomap_table[bar]); + + legacy_iomap_table[bar] = mapping; + +return 0 ; +} + +/* +* Removes 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) +{ +short bar; +void __iomem **legacy_iomap_table; + + legacy_iomap_table = (void __iomem **)pcim_iomap_table (pdev); +if (!legacy_iomap_table) +return ; + +for (bar =0 ; bar <PCI_STD_NUM_BARS ; bar++) { +if (legacy_iomap_table[bar] == addr) { + legacy_iomap_table[bar] =NULL ; +return ; + } + } +} +/** * pcim_iomap - Managed pcim_iomap() * @pdev: PCI device to iomap for
@@ -308,16 +354,20 @@ EXPORT_SYMBOL(pcim_iomap_table);*/ void __iomem *pcim_iomap (struct pci_dev *pdev,int bar,unsigned long maxlen) { -void __iomem **tbl; +void __iomem *mapping; -BUG_ON (bar >=PCIM_IOMAP_MAX ); - - tbl = (void __iomem **)pcim_iomap_table (pdev); -if (!tbl || tbl[bar])/* duplicate mappings not allowed */ + mapping =pci_iomap (pdev, bar, maxlen); +if (!mapping)return NULL ; - tbl[bar] =pci_iomap (pdev, bar, maxlen); -return tbl[bar]; +if (pcim_add_mapping_to_legacy_table (pdev, mapping, bar) !=0 ) + goto err_table; + +return mapping; + +err_table: +pci_iounmap (pdev, mapping); +return NULL ; }EXPORT_SYMBOL (pcim_iomap);
@@ -330,20 +380,9 @@ EXPORT_SYMBOL(pcim_iomap);*/ void pcim_iounmap (struct pci_dev *pdev,void __iomem *addr) { -void __iomem **tbl; -int i; -pci_iounmap (pdev, addr); - tbl = (void __iomem **)pcim_iomap_table (pdev); -BUG_ON (!tbl); - -for (i =0 ; i <PCIM_IOMAP_MAX ; i++) -if (tbl[i] == addr) { - tbl[i] =NULL ; -return ; - } -WARN_ON (1 ); +pcim_remove_mapping_from_legacy_table (pdev, addr); }EXPORT_SYMBOL (pcim_iounmap);
Signature · 1 line
2.45.0
PATCH v7 5/13 PCI: Make devres region requests consistent
In reply to Philipp StannerNow that pure managed region request functions are available, the implementation of the hybrid-functions which are only sometimes managed can be made more consistent and readable by wrapping those always-managed functions.
Implement pcim_request_region_exclusive() as a PCI-internal helper. Have the PCI request / release functions call their pcim_ counterparts. Remove the now surplus region_mask from struct pci_devres.
--- drivers/pci/devres.c | 52 ++++++++++++++++++++++---------------------- drivers/pci/pci.c | 45 +++++++++++++------------------------- drivers/pci/pci.h | 10 ++++----- 3 files changed, 45 insertions(+), 62 deletions(-)
drivers/pci/devres.c +26 -26
@@ -22,18 +22,15 @@* _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(), while requests performed by -* pcim_enable_device() + pci_*region*() are automatically cleaned up -* through the for-loop in pcim_release(). +* pcim_addr_resource_release(). +* Users utilizing 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 1: +* TODO: * Remove the legacy table entirely once all calls to pcim_iomap_table() in * the kernel have been removed. -* -* TODO 2: -* Port everyone calling pcim_enable_device() + pci_*region*() to using the -* pcim_ functions. Then, remove all devres functionality from pci_*region*() -* functions and remove the associated cleanups described above in point #2. */ /*
@@ -394,21 +391,6 @@ static void pcim_release(struct device *gendev, void *res) {struct pci_dev *dev =to_pci_dev (gendev);struct pci_devres *this = res; -int i; - -/* -* This is legacy code. -* All regions requested by a pcim_ function do get released through -* pcim_addr_resource_release(). Thanks to the hybrid nature of the pci_ -* region-request functions, this for-loop has to release the regions -* if they have been requested by such a function. -* -* TODO: Remove this once all users of pcim_enable_device() PLUS -* pci-region-request-functions have been ported to pcim_ functions. -*/ -for (i =0 ; i <DEVICE_COUNT_RESOURCE ; i++) -if (mask_contains_bar (this->region_mask, i)) -pci_release_region (dev, i);if (this->mwi)pci_clear_mwi (dev);
@@ -816,11 +798,29 @@ static int _pcim_request_region(struct pci_dev *pdev, int bar, const char *name,* The region will automatically be released on driver detach. If desired, * release manually only with pcim_release_region(). */ -static int pcim_request_region (struct pci_dev *pdev,int bar,const char *name) +int pcim_request_region (struct pci_dev *pdev,int bar,const char *name) {return _pcim_request_region (pdev, bar, name,0 ); } +/** +* 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) +{ +return _pcim_request_region (pdev, bar, name,IORESOURCE_EXCLUSIVE ); +} +/** * pcim_release_region - Release a PCI BAR * @pdev: PCI device to operate on
@@ -829,7 +829,7 @@ static int pcim_request_region(struct pci_dev *pdev, int bar, const char *name)* Release a region manually that was previously requested by * pcim_request_region(). */ -static void pcim_release_region (struct pci_dev *pdev,int bar) +void pcim_release_region (struct pci_dev *pdev,int bar) {struct pcim_addr_devres res_searched;
drivers/pci/pci.c +15 -30
69 lines not on this page · raw message
drivers/pci/pci.h +4 -6
@@ -828,16 +828,14 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; - -/* -* TODO: remove the region_mask once everyone calling -* pcim_enable_device() + pci_*region*() is ported to pcim_ functions. -*/ -u32 region_mask; };struct pci_devres *find_pci_dr (struct pci_dev *pdev); +int pcim_request_region (struct pci_dev *pdev,int bar,const char *name); +int pcim_request_region_exclusive (struct pci_dev *pdev,int bar,const char *name); +void pcim_release_region (struct pci_dev *pdev,int bar); +/* * Config Address for PCI Configuration Mechanism #1 *
Signature · 1 line
2.45.0
PATCH v7 3/13 PCI: Reimplement plural devres functions
In reply to Philipp StannerWhen the original PCI devres API was implemented, priority was given to the creation of a set of "plural functions" such as pcim_request_regions(). These functions have bit masks as parameters to specify which BARs shall get mapped. Most users, however, only use those to map 1-3 BARs.
A complete set of "singular functions" does not exist.
As functions mapping / requesting multiple BARs at once have (almost) no mechanism in C to return the resources to the caller of the plural function, the PCI devres API utilizes the iomap-table administrated by the function pcim_iomap_table().
The entire PCI devres API was strongly tied to that table which only allows for mapping whole, complete BARs, as the BAR's index is used as table index. Consequently, it's not possible to, e.g., have a pcim_iomap_range() function with that mechanism.
An additional problem is hat the PCI devres API has been ipmlemented in a sort of "hybrid-mode": Some unmanaged functions have managed counterparts (e.g.: pci_iomap() <-> pcim_iomap()), making their managed nature obvious to the programmer. However, the region-request functions in pci.c, prefixed with pci_, behave either managed or unmanaged, depending on whether pci_enable_device() or pcim_enable_device() has been called in advance.
This hybrid API is confusing and should be more cleanly separated by providing always-managed functions prefixed with pcim_.
Thus, the existing PCI devres API is not desirable because:
a) The vast majority of the users of the plural functions only ever sets a single bit in the bit mask, consequently making them singular functions anyways.
b) There is no mechanism to request / iomap only part of a BAR.
c) The iomap-table mechanism is over-engineered and complicated. Even worse, some users index over the table administration function directly:
void __iomem *mapping = pcim_iomap_table(pdev)[my_index]; This can not perform bounds checks; an invalid index won't cause return of -EINVAL or even NULL, resulting in undefined behavior.
d) region-request functions being sometimes managed and sometimes not is bug-provoking.
Implement a set of internal helper functions that don't have the problem of a hybrid nature that their counter parts in pci.c have. Write those helpers in a generic manner so that they can easily be extended to, e.g., ranged mappings and requests.
Implement a set of singular functions that use devres as it's intended and use those singular functions to reimplement the plural functions.
--- drivers/pci/devres.c | 601 ++++++++++++++++++++++++++++++++++++++----- drivers/pci/pci.c | 20 ++ drivers/pci/pci.h | 5 + 3 files changed, 559 insertions(+), 67 deletions(-)
drivers/pci/devres.c +534 -67
@@ -4,14 +4,238 @@#include "pci.h" /* -* PCI iomap devres +* 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, wheras 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(), while requests performed by +* pcim_enable_device() + pci_*region*() are automatically cleaned up +* through the for-loop in pcim_release(). +* +* TODO 1: +* Remove the legacy table entirely once all calls to pcim_iomap_table() in +* the kernel have been removed. +* +* TODO 2: +* Port everyone calling pcim_enable_device() + pci_*region*() to using the +* pcim_ functions. Then, remove all devres functionality from pci_*region*() +* functions and remove the associated cleanups described above in point #2. */ -#define PCIM_IOMAP_MAX PCI_STD_NUM_BARS +/* +* Legacy struct storing addresses to whole mapped BARs. +*/ struct pcim_iomap_devres { -void __iomem *table[PCIM_IOMAP_MAX ]; +void __iomem *table[PCI_STD_NUM_BARS ]; +}; + +enum pcim_addr_devres_type { +/* Default initializer. */ +PCIM_ADDR_DEVRES_TYPE_INVALID , + +/* A region spanning an entire BAR. */ +PCIM_ADDR_DEVRES_TYPE_REGION , + +/* A region spanning an entire BAR, and a mapping for that whole BAR. */ +PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING , + +/* +* A mapping within a BAR, either spanning the whole BAR or just a range. +* Without a requested region. +*/ +PCIM_ADDR_DEVRES_TYPE_MAPPING , +}; + +/* +* This struct envelopes IO or MEM addresses, that means mappings and region +* requests, because those are very frequently requested and released together. +*/ +struct pcim_addr_devres { +enum pcim_addr_devres_type type; +void __iomem *baseaddr; +unsigned long offset; +unsigned long len; +short bar; }; +static inline void pcim_addr_devres_clear (struct pcim_addr_devres *res) +{ +memset (res,0 ,sizeof (*res)); + res->bar =-1 ; +} + +/* +* 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: The BAR the region 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. For example for kernel-exclusive requests. +* +* Returns: 0 on success, a negative error code on failure. +* +* Request a ranged region within a device's PCI BAR. This function performs +* sanity checks on 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) +{ +resource_size_t start =pci_resource_start (pdev, bar); +resource_size_t len =pci_resource_len (pdev, bar); +unsigned long dev_flags =pci_resource_flags (pdev, bar); + +if (start ==0 || len ==0 )/* That's an unused BAR. */ +return 0 ; +if (len <= offset) +return -EINVAL ; + + start += offset; + len -= offset; + +if (len > maxlen && maxlen !=0 ) + len = maxlen; + +if (dev_flags &IORESOURCE_IO ) { +if (!request_region (start, len, name)) +return -EBUSY ; + }else if (dev_flags &IORESOURCE_MEM ) { +if (!__request_mem_region (start, len, name, req_flags)) +return -EBUSY ; + }else { +/* That's not a device we can request anything on. */ +return -ENODEV ; + } + +return 0 ; +} + +static void __pcim_release_region_range (struct pci_dev *pdev,int bar, +unsigned long offset,unsigned long maxlen) +{ +resource_size_t start =pci_resource_start (pdev, bar); +resource_size_t len =pci_resource_len (pdev, bar); +unsigned long flags =pci_resource_flags (pdev, bar); + +if (len <= offset || start ==0 ) +return ; + +if (len ==0 || maxlen ==0 )/* This an unused BAR. Do nothing. */ +return ; + + start += offset; + len -= offset; + +if (len > maxlen) + len = maxlen; + +if (flags &IORESOURCE_IO ) +release_region (start, len); +else if (flags &IORESOURCE_MEM ) +release_mem_region (start, len); +} + +static int __pcim_request_region (struct pci_dev *pdev,int bar, +const char *name,int flags) +{ +unsigned long offset =0 ; +unsigned long len =pci_resource_len (pdev, bar); + +return __pcim_request_region_range (pdev, bar, offset, len, name, flags); +} + +static void __pcim_release_region (struct pci_dev *pdev,int bar) +{ +unsigned long offset =0 ; +unsigned long len =pci_resource_len (pdev, bar); + +__pcim_release_region_range (pdev, bar, offset, len); +} + +static void pcim_addr_resource_release (struct device *dev,void *resource_raw) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct pcim_addr_devres *res = resource_raw; + +switch (res->type) { +case PCIM_ADDR_DEVRES_TYPE_REGION : +__pcim_release_region (pdev, res->bar); +break ; +case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING : +pci_iounmap (pdev, res->baseaddr); +__pcim_release_region (pdev, res->bar); +break ; +case PCIM_ADDR_DEVRES_TYPE_MAPPING : +pci_iounmap (pdev, res->baseaddr); +break ; +default : +break ; + } +} + +static struct pcim_addr_devres *pcim_addr_devres_alloc (struct pci_dev *pdev) +{ +struct pcim_addr_devres *res; + + res =devres_alloc_node (pcim_addr_resource_release,sizeof (*res), +GFP_KERNEL ,dev_to_node (&pdev->dev)); +if (res) +pcim_addr_devres_clear (res); +return res; +} + +/* Just for consistency and readability. */ +static inline void pcim_addr_devres_free (struct pcim_addr_devres *res) +{ +devres_free (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) +{ +struct pcim_addr_devres *a, *b; + + a = a_raw; + b = b_raw; + +if (a->type != b->type) +return 0 ; + +switch (a->type) { +case PCIM_ADDR_DEVRES_TYPE_REGION : +case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING : +return a->bar == b->bar; +case PCIM_ADDR_DEVRES_TYPE_MAPPING : +return a->baseaddr == b->baseaddr; +default : +return 0 ; + } +}static void devm_pci_unmap_iospace (struct device *dev,void *ptr) {
@@ -92,8 +316,8 @@ EXPORT_SYMBOL(devm_pci_remap_cfgspace);* * All operations are managed and will be undone on driver detach. * -* Returns a pointer to the remapped memory or an ERR_PTR() encoded error code -* on failure. Usage example:: +* 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);
@@ -172,6 +396,16 @@ static void pcim_release(struct device *gendev, void *res)struct pci_devres *this = res;int i; +/* +* This is legacy code. +* All regions requested by a pcim_ function do get released through +* pcim_addr_resource_release(). Thanks to the hybrid nature of the pci_ +* region-request functions, this for-loop has to release the regions +* if they have been requested by such a function. +* +* TODO: Remove this once all users of pcim_enable_device() PLUS +* pci-region-request-functions have been ported to pcim_ functions. +*/ for (i =0 ; i <DEVICE_COUNT_RESOURCE ; i++)if (mask_contains_bar (this->region_mask, i))pci_release_region (dev, i);
@@ -258,19 +492,21 @@ EXPORT_SYMBOL(pcim_pin_device);static void pcim_iomap_release (struct device *gendev,void *res) { -struct pci_dev *dev =to_pci_dev (gendev); -struct pcim_iomap_devres *this = res; -int i; - -for (i =0 ; i <PCIM_IOMAP_MAX ; i++) -if (this->table[i]) -pci_iounmap (dev, this->table[i]); +/* +* Do nothing. This is legacy code. +* +* Cleanup of the mappings is now done directly through the callbacks +* registered when creating them. +*/ }/** * pcim_iomap_table - access iomap allocation table * @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
@@ -343,30 +579,67 @@ static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev, } } +/* +* 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,short bar) +{ +void __iomem **legacy_iomap_table; + +if (bar >=PCI_STD_NUM_BARS ) +return ; + + legacy_iomap_table = (void __iomem **)pcim_iomap_table (pdev); +if (!legacy_iomap_table) +return ; + + legacy_iomap_table[bar] =NULL ; +} +/** * pcim_iomap - Managed pcim_iomap() * @pdev: PCI device to iomap for * @bar: BAR to iomap * @maxlen: Maximum length of iomap * -* Managed pci_iomap(). Map is automatically unmapped on driver -* detach. +* 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) {void __iomem *mapping; +struct pcim_addr_devres *res; + + res =pcim_addr_devres_alloc (pdev); +if (!res) +return NULL ; + res->type =PCIM_ADDR_DEVRES_TYPE_MAPPING ; mapping =pci_iomap (pdev, bar, maxlen);if (!mapping) -return NULL ; + goto err_iomap; + res->baseaddr = mapping;if (pcim_add_mapping_to_legacy_table (pdev, mapping, bar) !=0 ) goto err_table; +devres_add (&pdev->dev, res);return mapping; err_table:pci_iounmap (pdev, mapping); +err_iomap: +pcim_addr_devres_free (res);return NULL ; }EXPORT_SYMBOL (pcim_iomap);
@@ -376,91 +649,290 @@ EXPORT_SYMBOL(pcim_iomap);* @pdev: PCI device to iounmap for * @addr: Address to unmap * -* Managed pci_iounmap(). @addr must have been mapped using pcim_iomap(). +* Managed pci_iounmap(). @addr must have been mapped using a pcim_* mapping +* function. */ void pcim_iounmap (struct pci_dev *pdev,void __iomem *addr) { -pci_iounmap (pdev, addr); +struct pcim_addr_devres res_searched; + +pcim_addr_devres_clear (&res_searched); + res_searched.type =PCIM_ADDR_DEVRES_TYPE_MAPPING ; + res_searched.baseaddr = addr; + +if (devres_release (&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched) !=0 ) { +/* Doesn't exist. User passed nonsense. */ +return ; + }pcim_remove_mapping_from_legacy_table (pdev, addr); }EXPORT_SYMBOL (pcim_iounmap); +/** +* 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) +{ +int ret; +struct pcim_addr_devres *res; + + res =pcim_addr_devres_alloc (pdev); +if (!res) +return IOMEM_ERR_PTR (-ENOMEM ); + + res->type =PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING ; + res->bar = bar; + + ret =__pcim_request_region (pdev, bar, name,0 ); +if (ret !=0 ) + goto err_region; + + res->baseaddr =pci_iomap (pdev, bar,0 ); +if (!res->baseaddr) { + ret = -EINVAL ; + goto err_iomap; + } + +devres_add (&pdev->dev, res); +return res->baseaddr; + +err_iomap: +__pcim_release_region (pdev, bar); +err_region: +pcim_addr_devres_free (res); + +return IOMEM_ERR_PTR (ret); +} + +/** +* 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) +{ +struct pcim_addr_devres res_searched; + +pcim_addr_devres_clear (&res_searched); + res_searched.type =PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING ; + res_searched.bar = bar; + +devres_release (&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} +/** * 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 used when requesting regions +* @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) { -void __iomem *const *iomap; -int i, rc; +int ret; +short bar; +void __iomem *mapping; - iomap =pcim_iomap_table (pdev); -if (!iomap) -return -ENOMEM ; +for (bar =0 ; bar <DEVICE_COUNT_RESOURCE ; bar++) { +if (!mask_contains_bar (mask, bar)) +continue ; -for (i =0 ; i <DEVICE_COUNT_RESOURCE ; i++) { -unsigned long len; + mapping =pcim_iomap_region (pdev, bar, name); +if (IS_ERR (mapping)) { + ret =PTR_ERR (mapping); + goto err; + } + ret =pcim_add_mapping_to_legacy_table (pdev, mapping, bar); +if (ret !=0 ) + goto err; + } -if (!mask_contains_bar (mask, i)) -continue ; +return 0 ; - rc = -EINVAL ; - len =pci_resource_len (pdev, i); -if (!len) - goto err_inval; +err: +while (--bar >=0 ) { +pcim_iounmap_region (pdev, bar); +pcim_remove_bar_from_legacy_table (pdev, bar); + } + +return ret; +} +EXPORT_SYMBOL (pcim_iomap_regions); + +static int _pcim_request_region (struct pci_dev *pdev,int bar,const char *name, +int request_flags) +{ +int ret; +struct pcim_addr_devres *res; - rc =pci_request_region (pdev, i, name); -if (rc) - goto err_inval; + res =pcim_addr_devres_alloc (pdev); +if (!res) +return -ENOMEM ; + res->type =PCIM_ADDR_DEVRES_TYPE_REGION ; + res->bar = bar; - rc = -ENOMEM ; -if (!pcim_iomap (pdev, i,0 )) - goto err_region; + ret =__pcim_request_region (pdev, bar, name, request_flags); +if (ret !=0 ) { +pcim_addr_devres_free (res); +return ret; } +devres_add (&pdev->dev, res);return 0 ; +} - err_region: -pci_release_region (pdev, i); - err_inval: -while (--i >=0 ) { -if (!mask_contains_bar (mask, i)) -continue ; -pcim_iounmap (pdev, iomap[i]); -pci_release_region (pdev, i); +/** +* 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(). +*/ +static int pcim_request_region (struct pci_dev *pdev,int bar,const char *name) +{ +return _pcim_request_region (pdev, bar, name,0 ); +} + +/** +* 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(). +*/ +static void pcim_release_region (struct pci_dev *pdev,int bar) +{ +struct pcim_addr_devres res_searched; + +pcim_addr_devres_clear (&res_searched); + res_searched.type =PCIM_ADDR_DEVRES_TYPE_REGION ; + res_searched.bar = bar; + +devres_release (&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} + + +/** +* pcim_release_all_regions - Release all regions of a PCI-device +* @pdev: the PCI device +* +* Will 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) +{ +short bar; + +for (bar =0 ; bar <PCI_STD_NUM_BARS ; bar++) +pcim_release_region (pdev, bar); +} + +/** +* 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) +{ +int ret; +short bar; + +for (bar =0 ; bar <PCI_STD_NUM_BARS ; bar++) { + ret =pcim_request_region (pdev, bar, name); +if (ret !=0 ) + goto err; } -return rc; +return 0 ; + +err: +pcim_release_all_regions (pdev); + +return ret; } -EXPORT_SYMBOL (pcim_iomap_regions);/** * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to iomap -* @name: Name used when requesting regions +* @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. */ int pcim_iomap_regions_request_all (struct pci_dev *pdev,int mask,const char *name) { -int request_mask = ((1 <<6 ) -1 ) & ~mask; -int rc; +short bar; +int ret; +void __iomem **legacy_iomap_table; - rc =pci_request_selected_regions (pdev, request_mask, name); -if (rc) -return rc; + ret =pcim_request_all_regions (pdev, name); +if (ret !=0 ) +return ret; - rc =pcim_iomap_regions (pdev, mask, name); -if (rc) -pci_release_selected_regions (pdev, request_mask); -return rc; +for (bar =0 ; bar <PCI_STD_NUM_BARS ; bar++) { +if (!mask_contains_bar (mask, bar)) +continue ; +if (!pcim_iomap (pdev, bar,0 )) + goto err; + } + +return 0 ; + +err: +/* +* If bar is larger than 0, then pcim_iomap() above has most likely +* failed because of -EINVAL. If it is equal 0, most likely the table +* couldn't be created, indicating -ENOMEM. +*/ + ret = bar >0 ? -EINVAL : -ENOMEM ; + legacy_iomap_table = (void __iomem **)pcim_iomap_table (pdev); + +while (--bar >=0 ) +pcim_iounmap (pdev, legacy_iomap_table[bar]); + +pcim_release_all_regions (pdev); + +return ret; }EXPORT_SYMBOL (pcim_iomap_regions_request_all);
@@ -473,19 +945,14 @@ EXPORT_SYMBOL(pcim_iomap_regions_request_all);*/ void pcim_iounmap_regions (struct pci_dev *pdev,int mask) { -void __iomem *const *iomap; -int i; - - iomap =pcim_iomap_table (pdev); -if (!iomap) -return ; +short bar; -for (i =0 ; i <PCIM_IOMAP_MAX ; i++) { -if (!mask_contains_bar (mask, i)) +for (bar =0 ; bar <PCI_STD_NUM_BARS ; bar++) { +if (!mask_contains_bar (mask, bar))continue ; -pcim_iounmap (pdev, iomap[i]); -pci_release_region (pdev, i); +pcim_iounmap_region (pdev, bar); +pcim_remove_bar_from_legacy_table (pdev, bar); } }EXPORT_SYMBOL (pcim_iounmap_regions);
drivers/pci/pci.c +20 -0
@@ -3880,6 +3880,16 @@ void pci_release_region(struct pci_dev *pdev, int bar)release_mem_region (pci_resource_start (pdev, bar),pci_resource_len (pdev, bar)); +/* +* This devres utility makes this function sometimes managed +* (when pcim_enable_device() has been called before). +* This is bad because it conflicts with the pcim_ functions being +* exclusively responsible for managed pci. Its "sometimes yes, sometimes +* no" nature can cause bugs. +* +* TODO: Remove this once all users that use pcim_enable_device() PLUS +* a region request function have been ported to using pcim_ functions. +*/ dr =find_pci_dr (pdev);if (dr) dr->region_mask &= ~(1 << bar);
@@ -3924,6 +3934,16 @@ static int __pci_request_region(struct pci_dev *pdev, int bar, goto err_out; } +/* +* This devres utility makes this function sometimes managed +* (when pcim_enable_device() has been called before). +* This is bad because it conflicts with the pcim_ functions being +* exclusively responsible for managed pci. Its "sometimes yes, sometimes +* no" nature can cause bugs. +* +* TODO: Remove this once all users that use pcim_enable_device() PLUS +* a region request function have been ported to using pcim_ functions. +*/ dr =find_pci_dr (pdev);if (dr) dr->region_mask |=1 << bar;
drivers/pci/pci.h +5 -0
@@ -828,6 +828,11 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; + +/* +* TODO: remove the region_mask once everyone calling +* pcim_enable_device() + pci_*region*() is ported to pcim_ functions. +*/ u32 region_mask; };
Signature · 1 line
2.45.0
PATCH v7 7/13 PCI: Move dev-enabled status bit to struct pci_dev
In reply to Philipp StannerThe bit describing whether the PCI device is currently enabled is stored in struct pci_devres. Besides this struct being subject of a cleanup process, struct pci_device is in general the right place to store this information, since it is not devres-specific.
Move the 'enabled' boolean bit to struct pci_dev.
--- drivers/pci/devres.c | 11 ++++------- drivers/pci/pci.c | 17 ++++++++++------- drivers/pci/pci.h | 1 - include/linux/pci.h | 1 + 4 files changed, 15 insertions(+), 15 deletions(-)
drivers/pci/devres.c +4 -7
@@ -398,7 +398,7 @@ static void pcim_release(struct device *gendev, void *res)if (this->restore_intx)pci_intx (dev, this->orig_intx); -if (this->enabled && !this->pinned) +if (!this->pinned)pci_disable_device (dev); }
@@ -441,14 +441,11 @@ int pcim_enable_device(struct pci_dev *pdev) dr =get_pci_dr (pdev);if (unlikely (!dr))return -ENOMEM ; -if (dr->enabled) -return 0 ; rc =pci_enable_device (pdev); -if (!rc) { +if (!rc) pdev->is_managed =1 ; - dr->enabled =1 ; - } +return rc; }EXPORT_SYMBOL (pcim_enable_device);
@@ -466,7 +463,7 @@ void pcim_pin_device(struct pci_dev *pdev)struct pci_devres *dr; dr =find_pci_dr (pdev); -WARN_ON (!dr || !dr->enabled); +WARN_ON (!dr || !pdev->enabled);if (dr) dr->pinned =1 ; }
drivers/pci/pci.c +10 -7
@@ -2011,6 +2011,9 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)u16 cmd;u8 pin; +if (dev->enabled) +return 0 ; + err =pci_set_power_state (dev,PCI_D0 );if (err <0 && err != -EIO )return err;
@@ -2025,7 +2028,7 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)pci_fixup_device (pci_fixup_enable, dev);if (dev->msi_enabled || dev->msix_enabled) -return 0 ; + goto success_out;pci_read_config_byte (dev,PCI_INTERRUPT_PIN , &pin);if (pin) {
@@ -2035,6 +2038,8 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) cmd & ~PCI_COMMAND_INTX_DISABLE); } +success_out: + dev->enabled = true;return 0 ; }
@@ -2193,6 +2198,9 @@ static void do_pci_disable_device(struct pci_dev *dev) {u16 pci_command; +if (!dev->enabled) +return ; +pci_read_config_word (dev,PCI_COMMAND , &pci_command);if (pci_command &PCI_COMMAND_MASTER ) { pci_command &= ~PCI_COMMAND_MASTER ;
@@ -2200,6 +2208,7 @@ static void do_pci_disable_device(struct pci_dev *dev) }pcibios_disable_device (dev); + dev->enabled = false; }/**
@@ -2227,12 +2236,6 @@ void pci_disable_enabled_device(struct pci_dev *dev)*/ void pci_disable_device (struct pci_dev *dev) { -struct pci_devres *dr; - - dr =find_pci_dr (dev); -if (dr) - dr->enabled =0 ; -dev_WARN_ONCE (&dev->dev,atomic_read (&dev->enable_cnt) <=0 ,"disabling already-disabled device" );
drivers/pci/pci.h +0 -1
@@ -823,7 +823,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)* then remove them from here. */ struct pci_devres { -unsigned int enabled:1 ;unsigned int pinned:1 ;unsigned int orig_intx:1 ;unsigned int restore_intx:1 ;
include/linux/pci.h +1 -0
@@ -367,6 +367,7 @@ struct pci_dev {this is D0-D3, D0 being fully functional, and D3 being off. */ u8 pm_cap;/* PM capability offset */ + unsigned int enabled:1; /* Whether this dev is enabled */ unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int pme_support:5; /* Bitmask of states from which PME#can be generated */
Signature · 1 line
2.45.0
PATCH v7 6/13 PCI: Warn users about complicated devres nature
In reply to Philipp StannerThe PCI region-request functions become managed functions when pcim_enable_device() has been called previously instead of pci_enable_device().
This has already caused a bug (in 8558de401b5f) by confusing users, who came to believe that all pci functions, such as pci_iomap_range(), suddenly are managed that way.
This is not the case.
Add comments to the relevant functions' docstrings that warn users about this behavior.
--- drivers/pci/iomap.c | 16 ++++++++++++++++ drivers/pci/pci.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-)
drivers/pci/iomap.c +16 -0
@@ -23,6 +23,10 @@* * @maxlen specifies the maximum length to map. If you want to get access to * the complete BAR from offset to the end, pass %0 here. +* +* NOTE: +* This function is never managed, even if you initialized with +* pcim_enable_device(). * */ void __iomem *pci_iomap_range(struct pci_dev *dev, int bar,
@@ -63,6 +67,10 @@ EXPORT_SYMBOL(pci_iomap_range);* * @maxlen specifies the maximum length to map. If you want to get access to * the complete BAR from offset to the end, pass %0 here. +* +* NOTE: +* This function is never managed, even if you initialized with +* pcim_enable_device(). * */ void __iomem *pci_iomap_wc_range(struct pci_dev *dev, int bar,
@@ -106,6 +114,10 @@ EXPORT_SYMBOL_GPL(pci_iomap_wc_range);* * @maxlen specifies the maximum length to map. If you want to get access to * the complete BAR without checking for its length first, pass %0 here. +* +* NOTE: +* This function is never managed, even if you initialized with +* pcim_enable_device(). If you need automatic cleanup, use pcim_iomap(). * */ void __iomem *pci_iomap (struct pci_dev *dev,int bar,unsigned long maxlen) {
@@ -127,6 +139,10 @@ EXPORT_SYMBOL(pci_iomap);* * @maxlen specifies the maximum length to map. If you want to get access to * the complete BAR without checking for its length first, pass %0 here. +* +* NOTE: +* This function is never managed, even if you initialized with +* pcim_enable_device(). * */ void __iomem *pci_iomap_wc (struct pci_dev *dev,int bar,unsigned long maxlen) {
drivers/pci/pci.c +41 -1
96 lines not on this page · raw message
Signature · 1 line
2.45.0
PATCH v7 8/13 PCI: Move pinned status bit to struct pci_dev
In reply to Philipp StannerThe bit describing whether the PCI device is currently pinned is stored in struct pci_devres. To clean up and simplify the PCI devres API, it's better if this information is stored in struct pci_dev.
This will later permit simplifying pcim_enable_device().
Move the 'pinned' boolean bit to struct pci_dev.
Restructure bits in struct pci_dev so the pm / pme fields are next to each other.
--- drivers/pci/devres.c | 14 ++++---------- drivers/pci/pci.h | 1 - include/linux/pci.h | 5 +++-- 3 files changed, 7 insertions(+), 13 deletions(-)
drivers/pci/devres.c +4 -10
@@ -398,7 +398,7 @@ static void pcim_release(struct device *gendev, void *res)if (this->restore_intx)pci_intx (dev, this->orig_intx); -if (!this->pinned) +if (!dev->pinned)pci_disable_device (dev); }
@@ -454,18 +454,12 @@ EXPORT_SYMBOL(pcim_enable_device);* 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(). +* 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) { -struct pci_devres *dr; - - dr =find_pci_dr (pdev); -WARN_ON (!dr || !pdev->enabled); -if (dr) - dr->pinned =1 ; + pdev->pinned = true; }EXPORT_SYMBOL (pcim_pin_device);
drivers/pci/pci.h +0 -1
@@ -823,7 +823,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)* then remove them from here. */ struct pci_devres { -unsigned int pinned:1 ;unsigned int orig_intx:1 ;unsigned int restore_intx:1 ;unsigned int mwi:1 ;
include/linux/pci.h +3 -2
@@ -367,11 +367,12 @@ struct pci_dev {this is D0-D3, D0 being fully functional, and D3 being off. */ u8 pm_cap;/* PM capability offset */ - unsigned int enabled:1; /* Whether this dev is enabled */ - unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int pme_support:5; /* Bitmask of states from which PME#can be generated */ unsigned int pme_poll:1; /* Poll device's PME status bit */ + unsigned int enabled:1; /* Whether this dev is enabled */ + unsigned int pinned:1; /* Whether this dev is pinned */ + unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int d1_support:1; /* Low power state D1 is supported */ unsigned int d2_support:1; /* Low power state D2 is supported */ unsigned int no_d1d2:1; /* D1 and D2 are forbidden */
Signature · 1 line
2.45.0
PATCH v7 10/13 PCI: Give pci(m)_intx its own devres callback
In reply to Philipp Stannerpci_intx() is one of the functions that have "hybrid mode" (i.e., sometimes managed, sometimes not). Providing a separate pcim_intx() function with its own device resource and cleanup callback allows for removing further large parts of the legacy PCI devres implementation.
As in the region-request-functions, pci_intx() has to call into its managed counterpart for backwards compatibility.
As pci_intx() is an outdated function, pcim_intx() shall not be made visible to drivers via a public API.
Implement pcim_intx() with its own device resource. Make pci_intx() call pcim_intx() in the managed case.
--- drivers/pci/devres.c | 76 ++++++++++++++++++++++++++++++++++++-------- drivers/pci/pci.c | 23 ++++++++------ drivers/pci/pci.h | 7 ++-- 3 files changed, 80 insertions(+), 26 deletions(-)
drivers/pci/devres.c +62 -14
@@ -40,6 +40,11 @@ struct pcim_iomap_devres {void __iomem *table[PCI_STD_NUM_BARS ]; }; +/* Used to restore the old intx state on driver detach. */ +struct pcim_intx_devres { +int orig_intx; +}; +enum pcim_addr_devres_type {/* Default initializer. */ PCIM_ADDR_DEVRES_TYPE_INVALID ,
@@ -392,32 +397,75 @@ int pcim_set_mwi(struct pci_dev *pdev) }EXPORT_SYMBOL (pcim_set_mwi); +static inline bool mask_contains_bar (int mask,int bar) {return mask &BIT (bar); } -static void pcim_release (struct device *gendev,void *res) +static void pcim_intx_restore (struct device *dev,void *data) { -struct pci_dev *dev =to_pci_dev (gendev); -struct pci_devres *this = res; +struct pci_dev *pdev =to_pci_dev (dev); +struct pcim_intx_devres *res = data; -if (this->restore_intx) -pci_intx (dev, this->orig_intx); +pci_intx (pdev, res->orig_intx); +} -if (!dev->pinned) -pci_disable_device (dev); +static struct pcim_intx_devres *get_or_create_intx_devres (struct device *dev) +{ +struct pcim_intx_devres *res; + + res =devres_find (dev, pcim_intx_restore,NULL ,NULL ); +if (res) +return res; + + res =devres_alloc (pcim_intx_restore,sizeof (*res),GFP_KERNEL ); +if (res) +devres_add (dev, res); + +return res; } -/* -* TODO: After the last four callers in pci.c are ported, find_pci_dr() -* needs to be made static again. +/** +* 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. +* +* Enables/disables PCI INTx for device @pdev. +* Restores the original state on driver detach. */ -struct pci_devres *find_pci_dr (struct pci_dev *pdev) +int pcim_intx (struct pci_dev *pdev,int enable) { -if (pci_is_managed (pdev)) -return devres_find (&pdev->dev, pcim_release,NULL ,NULL ); -return NULL ; +u16 pci_command, new; +struct pcim_intx_devres *res; + + res =get_or_create_intx_devres (&pdev->dev); +if (!res) +return -ENOMEM ; + + res->orig_intx = !enable; + +pci_read_config_word (pdev,PCI_COMMAND , &pci_command); + +if (enable) + new = pci_command & ~PCI_COMMAND_INTX_DISABLE ; +else + new = pci_command |PCI_COMMAND_INTX_DISABLE ; + +if (new != pci_command) +pci_write_config_word (pdev,PCI_COMMAND , new); + +return 0 ; +} + +static void pcim_release (struct device *gendev,void *res) +{ +struct pci_dev *dev =to_pci_dev (gendev); + +if (!dev->pinned) +pci_disable_device (dev); }static struct pci_devres *get_pci_dr (struct pci_dev *pdev)
drivers/pci/pci.c +13 -10
@@ -4444,11 +4444,23 @@ void pci_disable_parity(struct pci_dev *dev)* This is a "hybrid" function: It's normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. This hybrid feature is * DEPRECATED! +* +* Use pcim_intx() if you need a managed version. */ void pci_intx (struct pci_dev *pdev,int enable) {u16 pci_command, new; +/* +* This is done for backwards compatibility, because the old PCI devres +* API had a mode in which this function became managed if the dev had +* been enabled with pcim_enable_device() instead of pci_enable_device(). +*/ +if (pci_is_managed (pdev)) { +WARN_ON_ONCE (pcim_intx (pdev, enable) !=0 ); +return ; + } +pci_read_config_word (pdev,PCI_COMMAND , &pci_command);if (enable)
@@ -4456,17 +4468,8 @@ void pci_intx(struct pci_dev *pdev, int enable)else new = pci_command |PCI_COMMAND_INTX_DISABLE ; -if (new != pci_command) { -struct pci_devres *dr; - +if (new != pci_command)pci_write_config_word (pdev,PCI_COMMAND , new); - - dr =find_pci_dr (pdev); -if (dr && !dr->restore_intx) { - dr->restore_intx =1 ; - dr->orig_intx = !enable; - } - } }EXPORT_SYMBOL_GPL (pci_intx);
drivers/pci/pci.h +5 -2
@@ -823,11 +823,14 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)* then remove them from here. */ struct pci_devres { -unsigned int orig_intx:1 ; -unsigned int restore_intx:1 ; +/* +* TODO: +* This struct is now surplus. Remove it by refactoring pci/devres.c +*/ };struct pci_devres *find_pci_dr (struct pci_dev *pdev); +int pcim_intx (struct pci_dev *dev,int enable);int pcim_request_region (struct pci_dev *pdev,int bar,const char *name);int pcim_request_region_exclusive (struct pci_dev *pdev,int bar,const char *name);
Signature · 1 line
2.45.0
PATCH v7 11/13 PCI: Remove legacy pcim_release()
In reply to Philipp StannerThanks to preceding cleanup steps, pcim_release() is now not needed anymore and can be replaced by pcim_disable_device(), which is the exact counterpart to pcim_enable_device().
This permits removing further parts of the old PCI devres implementation.
Replace pcim_release() with pcim_disable_device(). Remove the now surplus function get_pci_dr(). Remove the struct pci_devres from pci.h. Remove the now surplus function find_pci_dr().
--- drivers/pci/devres.c | 53 +++++++++++++++++++++----------------------- drivers/pci/pci.h | 18 --------------- 2 files changed, 25 insertions(+), 46 deletions(-)
drivers/pci/devres.c +25 -28
@@ -460,48 +460,45 @@ int pcim_intx(struct pci_dev *pdev, int enable)return 0 ; } -static void pcim_release (struct device *gendev,void *res) +static void pcim_disable_device (void *pdev_raw) { -struct pci_dev *dev =to_pci_dev (gendev); - -if (!dev->pinned) -pci_disable_device (dev); -} - -static struct pci_devres *get_pci_dr (struct pci_dev *pdev) -{ -struct pci_devres *dr, *new_dr; - - dr =devres_find (&pdev->dev, pcim_release,NULL ,NULL ); -if (dr) -return dr; +struct pci_dev *pdev = pdev_raw; - new_dr =devres_alloc (pcim_release,sizeof (*new_dr),GFP_KERNEL ); -if (!new_dr) -return NULL ; -return devres_get (&pdev->dev, new_dr,NULL ,NULL ); +if (!pdev->pinned) +pci_disable_device (pdev); }/** * pcim_enable_device - Managed pci_enable_device() * @pdev: PCI device to be initialized * -* Managed pci_enable_device(). +* 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) { -struct pci_devres *dr; -int rc; +int ret; - dr =get_pci_dr (pdev); -if (unlikely (!dr)) -return -ENOMEM ; + ret =devm_add_action (&pdev->dev, pcim_disable_device, pdev); +if (ret !=0 ) +return ret; - rc =pci_enable_device (pdev); -if (!rc) - pdev->is_managed =1 ; +/* +* We prefer removing the action in case of an error over +* devm_add_action_or_reset() because the later could theoretically be +* disturbed by users having pinned the device too soon. +*/ + ret =pci_enable_device (pdev); +if (ret !=0 ) { +devm_remove_action (&pdev->dev, pcim_disable_device, pdev); +return ret; + } -return rc; + pdev->is_managed = true; + +return ret; }EXPORT_SYMBOL (pcim_enable_device);
drivers/pci/pci.h +0 -18
@@ -812,24 +812,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) }#endif -/* -* Managed PCI resources. This manages device on/off, INTx/MSI/MSI-X -* on/off and BAR regions. pci_dev itself records MSI/MSI-X status, so -* there's no need to track it separately. pci_devres is initialized -* when a device is enabled using managed PCI device enable interface. -* -* TODO: Struct pci_devres and find_pci_dr() only need to be here because -* they're used in pci.c. Port or move these functions to devres.c and -* then remove them from here. -*/ -struct pci_devres { -/* -* TODO: -* This struct is now surplus. Remove it by refactoring pci/devres.c -*/ -}; - -struct pci_devres *find_pci_dr (struct pci_dev *pdev);int pcim_intx (struct pci_dev *dev,int enable);int pcim_request_region (struct pci_dev *pdev,int bar,const char *name);
Signature · 1 line
2.45.0
PATCH v7 12/13 PCI: Add pcim_iomap_range()
In reply to Philipp StannerThe only managed mapping function currently is pcim_iomap() which doesn't allow for mapping an area starting at a certain offset, which many drivers want.
Add pcim_iomap_range() as an exported function.
--- drivers/pci/devres.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 2 ++ 2 files changed, 46 insertions(+)
drivers/pci/devres.c +44 -0
@@ -1007,3 +1007,47 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) } }EXPORT_SYMBOL (pcim_iounmap_regions); + +/** +* 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) +{ +void __iomem *mapping; +struct pcim_addr_devres *res; + + res =pcim_addr_devres_alloc (pdev); +if (!res) +return IOMEM_ERR_PTR (-ENOMEM ); + + mapping =pci_iomap_range (pdev, bar, offset, len); +if (!mapping) { +pcim_addr_devres_free (res); +return IOMEM_ERR_PTR (-EINVAL ); + } + + res->type =PCIM_ADDR_DEVRES_TYPE_MAPPING ; + res->baseaddr = mapping; + +/* +* Ranged mappings don't get added to the legacy-table, since the table +* only ever keeps track of whole BARs. +*/ + +devres_add (&pdev->dev, res); +return mapping; +} +EXPORT_SYMBOL (pcim_iomap_range);
include/linux/pci.h +2 -0
@@ -2329,6 +2329,8 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name);int pcim_iomap_regions_request_all (struct pci_dev *pdev,int mask,const char *name);void pcim_iounmap_regions (struct pci_dev *pdev,int mask); +void __iomem *pcim_iomap_range (struct pci_dev *pdev,int bar, +unsigned long offset,unsigned long len);extern int pci_pci_problems;#define PCIPCI_FAIL 1/* No PCI PCI DMA */
Signature · 1 line
2.45.0
PATCH v7 9/13 PCI: Give pcim_set_mwi() its own devres callback
In reply to Philipp StannerManaging pci_set_mwi() with devres can easily be done with its own callback, without the necessity to store any state about it in a device-related struct.
Remove the MWI state from struct pci_devres.
Give pcim_set_mwi() a separate devres-callback.
--- drivers/pci/devres.c | 29 ++++++++++++++++++----------- drivers/pci/pci.h | 1 - 2 files changed, 18 insertions(+), 12 deletions(-)
drivers/pci/devres.c +18 -11
@@ -361,24 +361,34 @@ void __iomem *devm_pci_remap_cfg_resource(struct device *dev, }EXPORT_SYMBOL (devm_pci_remap_cfg_resource); +static void __pcim_clear_mwi (void *pdev_raw) +{ +struct pci_dev *pdev = pdev_raw; + +pci_clear_mwi (pdev); +} +/** * pcim_set_mwi - a device-managed pci_set_mwi() -* @dev: the PCI device for which MWI is enabled +* @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 *dev) +int pcim_set_mwi (struct pci_dev *pdev) { -struct pci_devres *dr; +int ret; - dr =find_pci_dr (dev); -if (!dr) -return -ENOMEM ; + ret =devm_add_action (&pdev->dev, __pcim_clear_mwi, pdev); +if (ret !=0 ) +return ret; + + ret =pci_set_mwi (pdev); +if (ret !=0 ) +devm_remove_action (&pdev->dev, __pcim_clear_mwi, pdev); - dr->mwi =1 ; -return pci_set_mwi (dev); +return ret; }EXPORT_SYMBOL (pcim_set_mwi);
@@ -392,9 +402,6 @@ static void pcim_release(struct device *gendev, void *res)struct pci_dev *dev =to_pci_dev (gendev);struct pci_devres *this = res; -if (this->mwi) -pci_clear_mwi (dev); -if (this->restore_intx)pci_intx (dev, this->orig_intx);
drivers/pci/pci.h +0 -1
@@ -825,7 +825,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)struct pci_devres {unsigned int orig_intx:1 ;unsigned int restore_intx:1 ; -unsigned int mwi:1 ; };struct pci_devres *find_pci_dr (struct pci_dev *pdev);
Signature · 1 line
2.45.0
PATCH v7 13/13 drm/vboxvideo: fix mapping leaks
In reply to Philipp StannerWhen the PCI devres API was introduced to this driver, it was wrongly assumed that initializing the device with pcim_enable_device() instead of pci_enable_device() will make all PCI functions managed.
This is wrong and was caused by the quite confusing PCI devres API in which some, but not all, functions become managed that way.
The function pci_iomap_range() is never managed.
Replace pci_iomap_range() with the actually managed function pcim_iomap_range().
--- drivers/gpu/drm/vboxvideo/vbox_main.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
drivers/gpu/drm/vboxvideo/vbox_main.c +9 -11
@@ -42,12 +42,11 @@ static int vbox_accel_init(struct vbox_private *vbox)/* Take a command buffer for each screen from the end of usable VRAM. */ vbox->available_vram_size -= vbox->num_crtcs *VBVA_MIN_BUFFER_SIZE ; - vbox->vbva_buffers =pci_iomap_range (pdev,0 , - vbox->available_vram_size, - vbox->num_crtcs * -VBVA_MIN_BUFFER_SIZE ); -if (!vbox->vbva_buffers) -return -ENOMEM ; + vbox->vbva_buffers =pcim_iomap_range ( + pdev,0 , vbox->available_vram_size, + vbox->num_crtcs *VBVA_MIN_BUFFER_SIZE ); +if (IS_ERR (vbox->vbva_buffers)) +return PTR_ERR (vbox->vbva_buffers);for (i =0 ; i < vbox->num_crtcs; ++i) { vbva_setup_buffer_context(&vbox->vbva_info[i],
@@ -116,11 +115,10 @@ int vbox_hw_init(struct vbox_private *vbox)DRM_INFO ("VRAM %08x\n" , vbox->full_vram_size);/* Map guest-heap at end of vram */ - vbox->guest_heap = -pci_iomap_range (pdev,0 ,GUEST_HEAP_OFFSET (vbox), -GUEST_HEAP_SIZE ); -if (!vbox->guest_heap) -return -ENOMEM ; + vbox->guest_heap =pcim_iomap_range (pdev,0 , +GUEST_HEAP_OFFSET (vbox),GUEST_HEAP_SIZE ); +if (IS_ERR (vbox->guest_heap)) +return PTR_ERR (vbox->guest_heap);/* Create guest-heap mem-pool use 2^4 = 16 byte chunks */ vbox->guest_pool = devm_gen_pool_create(vbox->ddev.dev, 4, -1,
Signature · 1 line
2.45.0
PATCH v7 1/13 PCI: Add and use devres helper for bit masks
In reply to Philipp StannerThe current derves implementation uses manual shift operations to check whether a bit in a mask is set. The code can be made more readable by writing a small helper function for that.
Implement mask_contains_bar() and use it where applicable.
--- drivers/pci/devres.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/pci/devres.c +8 -4
@@ -161,6 +161,10 @@ int pcim_set_mwi(struct pci_dev *dev) }EXPORT_SYMBOL (pcim_set_mwi); +static inline bool mask_contains_bar (int mask,int bar) +{ +return mask &BIT (bar); +}static void pcim_release (struct device *gendev,void *res) {
@@ -169,7 +173,7 @@ static void pcim_release(struct device *gendev, void *res)int i;for (i =0 ; i <DEVICE_COUNT_RESOURCE ; i++) -if (this->region_mask & (1 << i)) +if (mask_contains_bar (this->region_mask, i))pci_release_region (dev, i);if (this->mwi)
@@ -363,7 +367,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)for (i =0 ; i <DEVICE_COUNT_RESOURCE ; i++) {unsigned long len; -if (!(mask & (1 << i))) +if (!mask_contains_bar (mask, i))continue ; rc = -EINVAL ;
@@ -386,7 +390,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)pci_release_region (pdev, i); err_inval:while (--i >=0 ) { -if (!(mask & (1 << i))) +if (!mask_contains_bar (mask, i))continue ;pcim_iounmap (pdev, iomap[i]);pci_release_region (pdev, i);
@@ -438,7 +442,7 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask)return ;for (i =0 ; i <PCIM_IOMAP_MAX ; i++) { -if (!(mask & (1 << i))) +if (!mask_contains_bar (mask, i))continue ;pcim_iounmap (pdev, iomap[i]);
Signature · 1 line
2.45.0
PATCH v7 4/13 PCI: Deprecate two surplus devres functions
In reply to Philipp Stannerpcim_iomap_table() should not be used anymore because it contributed to the PCI devres API being designed contrary to devres's design goals.
pcim_iomap_regions_request_all() is a surplus, complicated function that can easily be replaced by using a pcim_* request function in combination with a pcim_* mapping function.
Mark pcim_iomap_table() and pcim_iomap_regions_request_all() as deprecated in the function documentation.
--- drivers/pci/devres.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/pci/devres.c +10 -2
@@ -501,7 +501,7 @@ static void pcim_iomap_release(struct device *gendev, void *res) }/** -* pcim_iomap_table - access iomap allocation table +* pcim_iomap_table - access iomap allocation table (DEPRECATED) * @pdev: PCI device to access iomap table for * * Returns:
@@ -515,6 +515,11 @@ static void pcim_iomap_release(struct device *gendev, void *res)* 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, barnr, length); */ void __iomem *const *pcim_iomap_table (struct pci_dev *pdev) {
@@ -886,7 +891,7 @@ 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 +* 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
@@ -897,6 +902,9 @@ static int pcim_request_all_regions(struct pci_dev *pdev, const char *name)* * 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)
Signature · 1 line
2.45.0
Re: [PATCH v7 07/13] PCI: Move dev-enabled status bit to struct pci_dev
In reply to Philipp StannerQuoted 6 lines · Philipp Stanner
The bit describing whether the PCI device is currently enabled is stored in struct
pci_devres. Besides this struct being subject of a cleanup process, structpci_deviceis in general the right place to store this information, since it is not devres-specific.Move the 'enabled' boolean bit to struct
pci_dev.
I think this (and the similar 'pinned' patch) appeared in v6.
It sounds plausible to have this in struct pci_dev, but it's confusing to have both:
pci_dev.enabled
pci_dev.enable_cnt, used by pci_is_enabled()
I haven't looked hard enough to see whether both are required. If they are, I think we should rename "enabled" to something descriptive enough to make it obviously different from "enable_cnt".
Quoted 137 lines
Signed-off-by Philipp Stanner <[email protected]>--- drivers/pci/devres.c | 11 ++++------- drivers/pci/pci.c | 17 ++++++++++------- drivers/pci/pci.h | 1 - include/linux/pci.h | 1 + 4 files changed, 15 insertions(+), 15 deletions(-)drivers/pci/devres.c +4
-7@@ -398,7 +398,7 @@ static void pcim_release(struct device *gendev, void *res)if (this->restore_intx)pci_intx (dev, this->orig_intx); -if (this->enabled && !this->pinned) +if (!this->pinned)pci_disable_device (dev); }@@ -441,14 +441,11 @@ int pcim_enable_device(struct pci_dev *pdev) dr =get_pci_dr (pdev);if (unlikely (!dr))return -ENOMEM ; -if (dr->enabled) -return 0 ; rc =pci_enable_device (pdev); -if (!rc) { +if (!rc) pdev->is_managed =1 ; - dr->enabled =1 ; - } +return rc; }EXPORT_SYMBOL (pcim_enable_device);@@ -466,7 +463,7 @@ void pcim_pin_device(struct pci_dev *pdev)struct pci_devres *dr; dr =find_pci_dr (pdev); -WARN_ON (!dr || !dr->enabled); +WARN_ON (!dr || !pdev->enabled);if (dr) dr->pinned =1 ; }drivers/pci/pci.c +10
-7@@ -2011,6 +2011,9 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)u16 cmd;u8 pin; +if (dev->enabled) +return 0 ; + err =pci_set_power_state (dev,PCI_D0 );if (err <0 && err != -EIO )return err;@@ -2025,7 +2028,7 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)pci_fixup_device (pci_fixup_enable, dev);if (dev->msi_enabled || dev->msix_enabled) -return 0 ; + goto success_out;pci_read_config_byte (dev,PCI_INTERRUPT_PIN , &pin);if (pin) {@@ -2035,6 +2038,8 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) cmd & ~PCI_COMMAND_INTX_DISABLE); } +success_out: + dev->enabled = true;return 0 ; }@@ -2193,6 +2198,9 @@ static void do_pci_disable_device(struct pci_dev *dev) {u16 pci_command; +if (!dev->enabled) +return ; +pci_read_config_word (dev,PCI_COMMAND , &pci_command);if (pci_command &PCI_COMMAND_MASTER ) { pci_command &= ~PCI_COMMAND_MASTER ;@@ -2200,6 +2208,7 @@ static void do_pci_disable_device(struct pci_dev *dev) }pcibios_disable_device (dev); + dev->enabled = false; }/** @@ -2227,12 +2236,6 @@ void pci_disable_enabled_device(struct pci_dev *dev)*/ void pci_disable_device (struct pci_dev *dev) { -struct pci_devres *dr; - - dr =find_pci_dr (dev); -if (dr) - dr->enabled =0 ; -dev_WARN_ONCE (&dev->dev,atomic_read (&dev->enable_cnt) <=0 ,"disabling already-disabled device" );drivers/pci/pci.h +0
-1@@ -823,7 +823,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)* then remove them from here. */ struct pci_devres { -unsigned int enabled:1 ;unsigned int pinned:1 ;unsigned int orig_intx:1 ;unsigned int restore_intx:1 ;include/linux/pci.h +1
-0@@ -367,6 +367,7 @@ struct pci_dev {this is D0-D3, D0 being fully functional, and D3 being off. */ u8 pm_cap;/* PM capability offset */ + unsigned int enabled:1; /* Whether this dev is enabled */ unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int pme_support:5; /* Bitmask of states from which PME#can be generated */ Signature · 1 line
2.45.0
Re: [PATCH v7 07/13] PCI: Move dev-enabled status bit to struct pci_dev
Quoted 9 lines · Bjorn Helgaas
Quoted 8 lines · Philipp Stanner
The bit describing whether the PCI device is currently enabled is stored
in structpci_devres. Besides this struct being subject of a cleanup
process, structpci_deviceis in general the right place to store this
information, since it is not devres-specific.
Quoted 3 lines
Quoted 1 line
Move the 'enabled' boolean bit to struct
pci_dev.I think this (and the similar 'pinned' patch) appeared in v6.
Yes. This patch and its brothers serve to remove members from struct pci_devres step by step, so it can ultimately be removed, so that we won't have a generic devres struct anymore, but actual resource-specific structs.
Quoted 11 lines
It sounds plausible to have this in struct
pci_dev, but it's confusing
to have both:pci_dev.enabled
pci_dev.enable_cnt, used bypci_is_enabled()I haven't looked hard enough to see whether both are required. If they are, I think we should rename "enabled" to something descriptive enough to make it obviously different from "
enable_cnt".
I took a look at it and I think we can actually drop "enabled" and use "enable_cnt" for everything. That would even simplify things more, I'd say.
Let me provide that in v8.
P.
Quoted 154 lines
Quoted 153 lines
Signed-off-by Philipp Stanner <[email protected]>---
drivers/pci/devres.c | 11 ++++-------
drivers/pci/pci.c | 17 ++++++++++-------
drivers/pci/pci.h | 1 -
include/linux/pci.h | 1 +4 files changed, 15 insertions(+), 15 deletions(-)drivers/pci/devres.c +0
-0@@ -398,7 +398,7 @@ static void pcim_release(struct device *gendev, void *res) if (this->restore_intx) pci_intx(dev, this->orig_intx);
- if (this->enabled&& !this->pinned)
+ if (!this->pinned)
pci_disable_device(dev);
}drivers/pci/devres.c +0
-2@@ -441,14 +441,11 @@ int pcim_enable_device(struct pci_dev *pdev) dr = get_pci_dr(pdev); if (unlikely(!dr)) return -ENOMEM; - if (dr->enabled) - return 0;
rc =pci_enable_device(pdev);
- if (!rc) {
+ if (!rc)
pdev->is_managed = 1;
- dr->enabled = 1;
- }
+
return rc;
}
EXPORT_SYMBOL(pcim_enable_device);drivers/pci/devres.c +0
-0@@ -466,7 +463,7 @@ void pcim_pin_device(struct pci_dev *pdev) struct pci_devres *dr;
dr =find_pci_dr(pdev);- WARN_ON(!dr || !dr->enabled); + WARN_ON(!dr || !pdev->enabled);if (dr)
dr->pinned = 1;
}drivers/pci/pci.c +0
-0@@ -2011,6 +2011,9 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) u16 cmd; u8 pin;
+ if (dev->enabled)
+ return 0;
+
err =pci_set_power_state(dev, PCI_D0);
if (err < 0 && err !=-EIO)
return err;drivers/pci/pci.c +0
-0@@ -2025,7 +2028,7 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) pci_fixup_device(pci_fixup_enable, dev);
if (dev->msi_enabled||dev->msix_enabled) - return 0;
+ gotosuccess_out;
pci_read_config_byte(dev,PCI_INTERRUPT_PIN, &pin); if (pin) {drivers/pci/pci.c +0
-0@@ -2035,6 +2038,8 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)cmd & ~PCI_COMMAND_INTX_DISABLE);
}
+success_out:
+ dev->enabled = true;
return 0;
}drivers/pci/pci.c +0
-0@@ -2193,6 +2198,9 @@ static void do_pci_disable_device(struct pci_dev *dev) { u16 pci_command;
+ if (!dev->enabled)
+ return;
+
pci_read_config_word(dev,PCI_COMMAND,&pci_command); if (pci_command&PCI_COMMAND_MASTER) {pci_command&= ~PCI_COMMAND_MASTER;drivers/pci/pci.c +0
-0@@ -2200,6 +2208,7 @@ static void do_pci_disable_device(struct pci_dev *dev) }
pcibios_disable_device(dev);
+ dev->enabled = false;
}
/**drivers/pci/pci.c +0
-6@@ -2227,12 +2236,6 @@ void pci_disable_enabled_device(structpci_dev *dev) */ void pci_disable_device(struct pci_dev *dev) { - struct pci_devres *dr; - - dr = find_pci_dr(dev); - if (dr) - dr->enabled = 0; -dev_WARN_ONCE(&dev->dev,
atomic_read(&dev->enable_cnt)<= 0,
"disabling already-disabled device");drivers/pci/pci.h +0
-1@@ -823,7 +823,6 @@ static inline pci_power_tmid_pci_get_power_state(struct pci_dev *pdev) * then remove them from here. */ struct pci_devres { - unsigned int enabled:1; unsigned int pinned:1; unsigned int orig_intx:1;unsigned int restore_intx:1;
include/linux/pci.h +0
-0@@ -367,6 +367,7 @@ struct pci_dev {this is D0-D3, D0 being fully
functional, and D3 being off. */
u8 pm_cap; /* PM capability offset */ + unsigned int enabled:1; /* Whether this dev is enabled */
unsigned int imm_ready:1; /* Supports Immediate Readiness */
unsigned int pme_support:5; /* Bitmask of states from which PME#
can be generated */Signature · 1 line
2.45.0
Quoted 119 lines · Philipp Stanner
Hello Bjorn,
I tried to meet your requests from the last feedback round as much as possible. Especially, I removed a lot of code, made almost all interfaces private and cut the series into smaller chunks where possible.
Splitting it even smaller is unfortunately not possible because of the Linux kernel build chain's rule on dead / unused code.
See also the changelog below.
Please tell me if that's enough to move forward here.
Regards,
P.Changes in v7:
- Split the entire series in smaller, more atomic chunks / patches(Bjorn) - Remove functions (such as pcim_iomap_region_range()) that do not yet have a user (Bjorn) - Don't export interfaces publicly anymore, except for pcim_iomap_range(), needed by vboxvideo (Bjorn) - Mention the actual (vboxvideo) bug in "PCI: Warn users..." commit (Bjorn)- Drop docstring warnings on PCI-internal functions (Bjorn)
- Rework docstring warnings
- Fix spelling in a few places. Rewrapp paragraphs (Bjorn)Changes in v6:
- Restructure the cleanup inpcim_iomap_regions_request_all()so that it doesn't trigger a (false positive) test robot warning. No behavior change intended. (Dan Carpenter)Changes in v5:
- Add Hans's Reviewed-by to vboxvideo patch (Hans de Goede)
- Remove stable-kernel from CC in vboxvideo patch (Hans de Goede)Changes in v4:
- Rebase against linux-nextChanges in v3:
- Use the term "PCI devres API" at some forgotten places.
- Fix more grammar errors in patch #3.
- Remove the comment advising to call (the outdated)pcim_intx()inpci.c
- Rename__pcim_request_region_range()flags-field "exclusive" to "req_flags", since this is what the int actually represents.
- Remove the call topcim_region_request()from patch #10. (Hans)Changes in v2:
- Make commit head lines congruent with PCI's style (Bjorn)
- Add missing error checks fordevm_add_action(). (Andy)
- Repair the "Returns: " marks for docu generation (Andy)
- Initialize theaddr_devresstruct withmemset(). (Andy)
- Makepcim_intx()a PCI-internal function so that new drivers won't be encouraged to use the outdatedpci_intx()mechanism. (Andy / Philipp)
- Fix grammar and spelling (Bjorn)
- Be more precise on whypcim_iomap_table()is problematic (Bjorn)
- Provide the actual structs' and functions' names in the commit messages (Bjorn)
- Remove redundant variable initializers (Andy)
- Regroup PM bitfield members in structpci_dev(Andy)
- Makepcim_intx()visible only for the PCI subsystem so that new drivers won't use this outdated API (Andy, Myself)
- Add a NOTE topcim_iomap()to warn about this function being the onee
xception that does just return NULL.
- Consistently use the term "PCI devres API"; also in Patch #10 (Bjorn)¡Hola!
PCI's devres API suffers several weaknesses:
1. There are functions prefixed with
pcim_. Those are always managed counterparts to never-managed functions prefixed withpci_– or so one would like to think. There are some apparently unmanaged functions (all region-request / release functions, andpci_intx()) which suddenly become managed once the user has initialized the device withpcim_enable_device()instead ofpci_enable_device(). This "sometimes yes, sometimes no" nature of those functions is confusing and therefore bug-provoking. In fact, it has already caused a bug in DRM. The last patch in this series fixes that bug.
2. iomappings: Instead of giving each mapping its own callback, the existing API uses a statically allocated struct tracking one mapping per bar. This is not extensible. Especially, you can't create _ranged_ managed mappings that way, which many drivers want.
3. Managed request functions only exist as "plural versions" with a bit-mask as a parameter. That's quite over-engineered considering that each user only ever mapps one, maybe two bars.This series:
- add a set of new "singular" devres functions that use devres the way its intended, with one callback per resource.
- deprecates the existing iomap-table mechanism.
- deprecates the hybrid nature ofpci_functions.
- preserves backwards compatibility so that drivers using the existing API won't notice any changes.
- adds documentation, especially some warning users about the complicated nature of PCI's devres.Note that this series is based on my "unify pci_iounmap"-series from a few weeks ago. [1]
I tested this on a x86 VM with a simple pci test-device with two regions. Operates and reserves resources as intended on my system. Kasan and kmemleak didn't find any problems.
I believe this series cleans the API up as much as possible without having to port all existing drivers to the new API. Especially, I think that this implementation is easy to extend if the need for new managed functions arises :)
Greetings,
P.
Quoted 7 lines
Philipp Stanner (13):
PCI: Add and use devres helper for bit masks
PCI: Add devres helpers for iomap table
PCI: Reimplement plural devres functions
PCI: Deprecate two surplus devres functions
PCI: Make devres region requests consistent
PCI: Warn users about complicated devres nature
Applied the above to pci/devres for v6.11 with minor comment and whitespace tweaks. Will watch for updates for the ones below to consolidate "enabled" and "enable_cnt".
Quoted 19 lines
PCI: Move dev-enabled status bit to struct
pci_dev
PCI: Move pinned status bit to structpci_dev
PCI: Givepcim_set_mwi()its own devres callback
PCI: Give pci(m)_intx its own devres callback
PCI: Remove legacypcim_release()
PCI: Addpcim_iomap_range()
drm/vboxvideo: fix mapping leaksdrivers/gpu/drm/vboxvideo/vbox_main.c | 20 +- drivers/pci/devres.c | 897 +++++++++++++++++++++----- drivers/pci/iomap.c | 16 + drivers/pci/pci.c | 107 ++- drivers/pci/pci.h | 23 +- include/linux/pci.h | 6 +- 6 files changed, 864 insertions(+), 205 deletions(-)Signature · 1 line
2.45.0
Re: [PATCH v7 03/13] PCI: Reimplement plural devres functions
In reply to Philipp StannerQuoted 112 lines · Philipp Stanner
When the original PCI devres API was implemented, priority was given to the creation of a set of "plural functions" such as
pcim_request_regions(). These functions have bit masks as parameters to specify which BARs shall get mapped. Most users, however, only use those to map 1-3 BARs.A complete set of "singular functions" does not exist.
As functions mapping / requesting multiple BARs at once have (almost) no mechanism in C to return the resources to the caller of the plural function, the PCI devres API utilizes the iomap-table administrated by the function
pcim_iomap_table().The entire PCI devres API was strongly tied to that table which only allows for mapping whole, complete BARs, as the BAR's index is used as table index. Consequently, it's not possible to, e.g., have a
pcim_iomap_range()function with that mechanism.An additional problem is hat the PCI devres API has been ipmlemented in a sort of "hybrid-mode": Some unmanaged functions have managed counterparts (e.g.:
pci_iomap()<->pcim_iomap()), making their managed nature obvious to the programmer. However, the region-request functions inpci.c, prefixed withpci_, behave either managed or unmanaged, depending on whetherpci_enable_device()orpcim_enable_device()has been called in advance.This hybrid API is confusing and should be more cleanly separated by providing always-managed functions prefixed with
pcim_.Thus, the existing PCI devres API is not desirable because:
a) The vast majority of the users of the plural functions only ever sets a single bit in the bit mask, consequently making them singular functions anyways.
b) There is no mechanism to request / iomap only part of a BAR.
c) The iomap-table mechanism is over-engineered and complicated. Even worse, some users index over the table administration function directly:
void__iomem*mapping = pcim_iomap_table(pdev)[my_index]; This can not perform bounds checks; an invalid index won't cause return of-EINVALor even NULL, resulting in undefined behavior.
d) region-request functions being sometimes managed and sometimes not is bug-provoking.Implement a set of internal helper functions that don't have the problem of a hybrid nature that their counter parts in
pci.chave. Write those helpers in a generic manner so that they can easily be extended to, e.g., ranged mappings and requests.Implement a set of singular functions that use devres as it's intended and use those singular functions to reimplement the plural functions.
Signed-off-by Philipp Stanner <[email protected]>/*
- * PCI iomap devres
+ * 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, wheras 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(), while requests performed by + * pcim_enable_device() + pci_*region*() are automatically cleaned up + * through the for-loop in pcim_release().+ *
+ * TODO 1:
+ * Remove the legacy table entirely once all calls topcim_iomap_table()in
+ * the kernel have been removed.
+ *
+ * TODO 2:
+ * Port everyone callingpcim_enable_device()+ pci_*region*() to using the
+ *pcim_functions. Then, remove all devres functionality from pci_*region*()
+ * functions and remove the associated cleanups described above in point #2. */drivers/pci/devres.c +21
-2-#define PCIM_IOMAP_MAX PCI_STD_NUM_BARS +/* +* Legacy struct storing addresses to whole mapped BARs. +*/ struct pcim_iomap_devres { -void __iomem *table[PCIM_IOMAP_MAX ]; +void __iomem *table[PCI_STD_NUM_BARS ]; +}; +/** +* __pcim_request_region_range - Request a ranged region +* @pdev: PCI device the region belongs to +* @bar: The BAR the region 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. For example for kernel-exclusive requests. +* +* Returns: 0 on success, a negative error code on failure. +* +* Request a ranged region within a device's PCI BAR. This function performs +* sanity checks on the input. +*/ +static int __pcim_request_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long maxlen,
drivers/pci/devres.c line 114
Quoted 10 lines
drivers/pci/devres.c +10
-0+ const char *name, int req_flags) +{ +resource_size_t start =pci_resource_start (pdev, bar); +resource_size_t len =pci_resource_len (pdev, bar); +unsigned long dev_flags =pci_resource_flags (pdev, bar); + +if (start ==0 || len ==0 )/* That's an unused BAR. */ +return 0 ; +if (len <= offset) +return -EINVAL ;
Extra space.
Quoted 47 lines
drivers/pci/devres.c +45
-0+ + start += offset; + len -= offset; + +if (len > maxlen && maxlen !=0 ) + len = maxlen; + +if (dev_flags &IORESOURCE_IO ) { +if (!request_region (start, len, name)) +return -EBUSY ; + }else if (dev_flags &IORESOURCE_MEM ) { +if (!__request_mem_region (start, len, name, req_flags)) +return -EBUSY ; + }else { +/* That's not a device we can request anything on. */ +return -ENODEV ; + } + +return 0 ; +} + +static void __pcim_release_region_range (struct pci_dev *pdev,int bar, +unsigned long offset,unsigned long maxlen) +{ +resource_size_t start =pci_resource_start (pdev, bar); +resource_size_t len =pci_resource_len (pdev, bar); +unsigned long flags =pci_resource_flags (pdev, bar); + +if (len <= offset || start ==0 ) +return ; + +if (len ==0 || maxlen ==0 )/* This an unused BAR. Do nothing. */ +return ; + + start += offset; + len -= offset; + +if (len > maxlen) + len = maxlen; + +if (flags &IORESOURCE_IO ) +release_region (start, len); +else if (flags &IORESOURCE_MEM ) +release_mem_region (start, len); +}
drivers/pci/devres.c EXPORT_SYMBOL(pcim_iomap_regions_request_all); · line 948
Quoted 10 lines
drivers/pci/devres.c +1
-6@@ -473,19 +945,14 @@ EXPORT_SYMBOL(pcim_iomap_regions_request_all);*/ void pcim_iounmap_regions (struct pci_dev *pdev,int mask) { -void __iomem *const *iomap; -int i; - - iomap =pcim_iomap_table (pdev); -if (!iomap) -return ; +short bar;
The current best practice is to use unsigned for loop vars that will never be negative.
I don't entirely follow what is reasoning behind making it short instead of unsigned int?
drivers/pci/devres.c EXPORT_SYMBOL(pcim_iomap_regions_request_all); · line 950
Quoted 3 lines
drivers/pci/devres.c +1
-2-for (i =0 ; i <PCIM_IOMAP_MAX ; i++) { -if (!mask_contains_bar (mask, i)) +for (bar =0 ; bar <PCI_STD_NUM_BARS ; bar++) {
Is this change minimal if it contains variable renames like this? Was "i" not "bar" even if it was given as a parameter to mask_contains_bar()?
Signature · 1 line
i.
Quoted 31 lines
drivers/pci/devres.c +3
-2+if (!mask_contains_bar (mask, bar))continue ; -pcim_iounmap (pdev, iomap[i]); -pci_release_region (pdev, i); +pcim_iounmap_region (pdev, bar); +pcim_remove_bar_from_legacy_table (pdev, bar); } }EXPORT_SYMBOL (pcim_iounmap_regions);drivers/pci/pci.c +10
-0@@ -3880,6 +3880,16 @@ void pci_release_region(struct pci_dev *pdev, int bar)release_mem_region (pci_resource_start (pdev, bar),pci_resource_len (pdev, bar)); +/* +* This devres utility makes this function sometimes managed +* (when pcim_enable_device() has been called before). +* This is bad because it conflicts with the pcim_ functions being +* exclusively responsible for managed pci. Its "sometimes yes, sometimes +* no" nature can cause bugs. +* +* TODO: Remove this once all users that use pcim_enable_device() PLUS +* a region request function have been ported to using pcim_ functions. +*/ dr =find_pci_dr (pdev);if (dr) dr->region_mask &= ~(1 << bar);
Re: [PATCH v7 01/13] PCI: Add and use devres helper for bit masks
In reply to Philipp StannerQuoted 16 lines · Philipp Stanner
The current derves implementation uses manual shift operations to check whether a bit in a mask is set. The code can be made more readable by writing a small helper function for that.
Implement
mask_contains_bar()and use it where applicable.Signed-off-by Philipp Stanner <[email protected]>--- drivers/pci/devres.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)drivers/pci/devres.c +0
-0
drivers/pci/devres.c int pcim_set_mwi(struct pci_dev *dev) · line 164
Quoted 4 lines
drivers/pci/devres.c +1
-0@@ -161,6 +161,10 @@ int pcim_set_mwi(struct pci_dev *dev) }EXPORT_SYMBOL (pcim_set_mwi); +static inline bool mask_contains_bar (int mask,int bar)
Why these are signed? Using & for signed values is an indication that the types should have been unsigned. The typing change has ripple effects to caller-side typing.
Quoted 3 lines
drivers/pci/devres.c +3
-0+{ +return mask &BIT (bar); +}
Signature · 1 line
i.
Quoted 40 lines
drivers/pci/devres.c +4
-4static void pcim_release (struct device *gendev,void *res) {@@ -169,7 +173,7 @@ static void pcim_release(struct device *gendev, void *res)int i;for (i =0 ; i <DEVICE_COUNT_RESOURCE ; i++) -if (this->region_mask & (1 << i)) +if (mask_contains_bar (this->region_mask, i))pci_release_region (dev, i);if (this->mwi)@@ -363,7 +367,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)for (i =0 ; i <DEVICE_COUNT_RESOURCE ; i++) {unsigned long len; -if (!(mask & (1 << i))) +if (!mask_contains_bar (mask, i))continue ; rc = -EINVAL ;@@ -386,7 +390,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)pci_release_region (pdev, i); err_inval:while (--i >=0 ) { -if (!(mask & (1 << i))) +if (!mask_contains_bar (mask, i))continue ;pcim_iounmap (pdev, iomap[i]);pci_release_region (pdev, i);@@ -438,7 +442,7 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask)return ;for (i =0 ; i <PCIM_IOMAP_MAX ; i++) { -if (!(mask & (1 << i))) +if (!mask_contains_bar (mask, i))continue ;pcim_iounmap (pdev, iomap[i]);
Re: [PATCH v7 09/13] PCI: Give pcim_set_mwi() its own devres callback
In reply to Philipp StannerQuoted 43 lines · Philipp Stanner
Managing
pci_set_mwi()with devres can easily be done with its own callback, without the necessity to store any state about it in a device-related struct.Remove the MWI state from struct
pci_devres.
Givepcim_set_mwi()a separate devres-callback.Signed-off-by Philipp Stanner <[email protected]>--- drivers/pci/devres.c | 29 ++++++++++++++++++----------- drivers/pci/pci.h | 1 - 2 files changed, 18 insertions(+), 12 deletions(-)drivers/pci/devres.c +10
-3@@ -361,24 +361,34 @@ void __iomem *devm_pci_remap_cfg_resource(struct device *dev, }EXPORT_SYMBOL (devm_pci_remap_cfg_resource); +static void __pcim_clear_mwi (void *pdev_raw) +{ +struct pci_dev *pdev = pdev_raw; + +pci_clear_mwi (pdev); +} +/** * pcim_set_mwi - a device-managed pci_set_mwi() -* @dev: the PCI device for which MWI is enabled +* @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 *dev) +int pcim_set_mwi (struct pci_dev *pdev) { -struct pci_devres *dr; +int ret;
drivers/pci/devres.c void __iomem *devm_pci_remap_cfg_resource(struct device *dev, · line 389
Quoted 10 lines
drivers/pci/devres.c +7
-3- dr =find_pci_dr (dev); -if (!dr) -return -ENOMEM ; + ret =devm_add_action (&pdev->dev, __pcim_clear_mwi, pdev); +if (ret !=0 ) +return ret; + + ret =pci_set_mwi (pdev); +if (ret !=0 ) +devm_remove_action (&pdev->dev, __pcim_clear_mwi, pdev);
I'm sorry if this is a stupid question but why this cannot use devm_add_action_or_reset()?
Quoted 5 lines
drivers/pci/devres.c +1
-2- dr->mwi =1 ; -return pci_set_mwi (dev); +return ret; }EXPORT_SYMBOL (pcim_set_mwi);
Signature · 1 line
i.
Re: [PATCH v7 10/13] PCI: Give pci(m)_intx its own devres callback
In reply to Philipp StannerQuoted 26 lines · Philipp Stanner
pci_intx()is one of the functions that have "hybrid mode" (i.e., sometimes managed, sometimes not). Providing a separatepcim_intx()function with its own device resource and cleanup callback allows for removing further large parts of the legacy PCI devres implementation.As in the region-request-functions,
pci_intx()has to call into its managed counterpart for backwards compatibility.As
pci_intx()is an outdated function,pcim_intx()shall not be made visible to drivers via a public API.Implement
pcim_intx()with its own device resource. Makepci_intx()callpcim_intx()in the managed case.Signed-off-by Philipp Stanner <[email protected]>--- drivers/pci/devres.c | 76 ++++++++++++++++++++++++++++++++++++-------- drivers/pci/pci.c | 23 ++++++++------ drivers/pci/pci.h | 7 ++-- 3 files changed, 80 insertions(+), 26 deletions(-)drivers/pci/devres.c +0
-0
drivers/pci/devres.c struct pcim_iomap_devres { · line 43
Quoted 4 lines
drivers/pci/devres.c +1
-0@@ -40,6 +40,11 @@ struct pcim_iomap_devres {void __iomem *table[PCI_STD_NUM_BARS ]; }; +/* Used to restore the old intx state on driver detach. */
INTx
drivers/pci/devres.c int pcim_set_mwi(struct pci_dev *pdev) · line 401
Quoted 13 lines
drivers/pci/devres.c +5
-0+struct pcim_intx_devres { +int orig_intx; +}; +enum pcim_addr_devres_type {/* Default initializer. */ PCIM_ADDR_DEVRES_TYPE_INVALID ,@@ -392,32 +397,75 @@ int pcim_set_mwi(struct pci_dev *pdev) }EXPORT_SYMBOL (pcim_set_mwi); +static inline bool mask_contains_bar (int mask,int bar)
Stray change.
Signature · 1 line
i.
Quoted 153 lines
drivers/pci/devres.c +56
-14{return mask &BIT (bar); } -static void pcim_release (struct device *gendev,void *res) +static void pcim_intx_restore (struct device *dev,void *data) { -struct pci_dev *dev =to_pci_dev (gendev); -struct pci_devres *this = res; +struct pci_dev *pdev =to_pci_dev (dev); +struct pcim_intx_devres *res = data; -if (this->restore_intx) -pci_intx (dev, this->orig_intx); +pci_intx (pdev, res->orig_intx); +} -if (!dev->pinned) -pci_disable_device (dev); +static struct pcim_intx_devres *get_or_create_intx_devres (struct device *dev) +{ +struct pcim_intx_devres *res; + + res =devres_find (dev, pcim_intx_restore,NULL ,NULL ); +if (res) +return res; + + res =devres_alloc (pcim_intx_restore,sizeof (*res),GFP_KERNEL ); +if (res) +devres_add (dev, res); + +return res; } -/* -* TODO: After the last four callers in pci.c are ported, find_pci_dr() -* needs to be made static again. +/** +* 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. +* +* Enables/disables PCI INTx for device @pdev. +* Restores the original state on driver detach. */ -struct pci_devres *find_pci_dr (struct pci_dev *pdev) +int pcim_intx (struct pci_dev *pdev,int enable) { -if (pci_is_managed (pdev)) -return devres_find (&pdev->dev, pcim_release,NULL ,NULL ); -return NULL ; +u16 pci_command, new; +struct pcim_intx_devres *res; + + res =get_or_create_intx_devres (&pdev->dev); +if (!res) +return -ENOMEM ; + + res->orig_intx = !enable; + +pci_read_config_word (pdev,PCI_COMMAND , &pci_command); + +if (enable) + new = pci_command & ~PCI_COMMAND_INTX_DISABLE ; +else + new = pci_command |PCI_COMMAND_INTX_DISABLE ; + +if (new != pci_command) +pci_write_config_word (pdev,PCI_COMMAND , new); + +return 0 ; +} + +static void pcim_release (struct device *gendev,void *res) +{ +struct pci_dev *dev =to_pci_dev (gendev); + +if (!dev->pinned) +pci_disable_device (dev); }static struct pci_devres *get_pci_dr (struct pci_dev *pdev)drivers/pci/pci.c +13
-10@@ -4444,11 +4444,23 @@ void pci_disable_parity(struct pci_dev *dev)* This is a "hybrid" function: It's normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. This hybrid feature is * DEPRECATED! +* +* Use pcim_intx() if you need a managed version. */ void pci_intx (struct pci_dev *pdev,int enable) {u16 pci_command, new; +/* +* This is done for backwards compatibility, because the old PCI devres +* API had a mode in which this function became managed if the dev had +* been enabled with pcim_enable_device() instead of pci_enable_device(). +*/ +if (pci_is_managed (pdev)) { +WARN_ON_ONCE (pcim_intx (pdev, enable) !=0 ); +return ; + } +pci_read_config_word (pdev,PCI_COMMAND , &pci_command);if (enable)@@ -4456,17 +4468,8 @@ void pci_intx(struct pci_dev *pdev, int enable)else new = pci_command |PCI_COMMAND_INTX_DISABLE ; -if (new != pci_command) { -struct pci_devres *dr; - +if (new != pci_command)pci_write_config_word (pdev,PCI_COMMAND , new); - - dr =find_pci_dr (pdev); -if (dr && !dr->restore_intx) { - dr->restore_intx =1 ; - dr->orig_intx = !enable; - } - } }EXPORT_SYMBOL_GPL (pci_intx);drivers/pci/pci.h +5
-2@@ -823,11 +823,14 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)* then remove them from here. */ struct pci_devres { -unsigned int orig_intx:1 ; -unsigned int restore_intx:1 ; +/* +* TODO: +* This struct is now surplus. Remove it by refactoring pci/devres.c +*/ };struct pci_devres *find_pci_dr (struct pci_dev *pdev); +int pcim_intx (struct pci_dev *dev,int enable);int pcim_request_region (struct pci_dev *pdev,int bar,const char *name);int pcim_request_region_exclusive (struct pci_dev *pdev,int bar,const char *name);
Re: [PATCH v7 03/13] PCI: Reimplement plural devres functions
In reply to Ilpo JärvinenQuoted 8 lines · Ilpo Järvinen
Quoted 5 lines · Philipp Stanner
When the original PCI devres API was implemented, priority was given to the creation of a set of "plural functions" such as
pcim_request_regions(). These functions have bit masks as parameters to specify which BARs shall get mapped. Most users, however, only use those to map 1-3 BARs.
Quoted 14 lines
drivers/pci/devres.c line 114
Quoted 12 lines
drivers/pci/devres.c +12
-0+static int __pcim_request_region_range (struct pci_dev *pdev,int bar, +unsigned long offset,unsigned long maxlen, +const char *name,int req_flags) +{ +resource_size_t start =pci_resource_start (pdev, bar); +resource_size_t len =pci_resource_len (pdev, bar); +unsigned long dev_flags =pci_resource_flags (pdev, bar); + +if (start ==0 || len ==0 )/* That's an unused BAR. */ +return 0 ; +if (len <= offset) +return -EINVAL ;Extra space.
Thanks for reviewing this. I dropped the space locally in the v9 series.
Quoted 13 lines
drivers/pci/devres.c EXPORT_SYMBOL(pcim_iomap_regions_request_all); · line 948
Quoted 9 lines
void
pcim_iounmap_regions(struct pci_dev *pdev, int mask){drivers/pci/devres.c +1
-6-void __iomem *const *iomap; -int i; - - iomap =pcim_iomap_table (pdev); -if (!iomap) -return ; +short bar;The current best practice is to use unsigned for loop vars that will never be negative.
Quoted 2 lines
I don't entirely follow what is reasoning behind making it short instead of unsigned int?
Existing interfaces like pcim_iomap() take "int bar". I locally changed all the BAR indices to "int". We can make everything unsigned later if worthwhile.
Quoted 7 lines
drivers/pci/devres.c EXPORT_SYMBOL(pcim_iomap_regions_request_all); · line 950
Quoted 3 lines
drivers/pci/devres.c +1
-2-for (i =0 ; i <PCIM_IOMAP_MAX ; i++) { -if (!mask_contains_bar (mask, i)) +for (bar =0 ; bar <PCI_STD_NUM_BARS ; bar++) {Is this change minimal if it contains variable renames like this? Was "i" not "bar" even if it was given as a parameter to
mask_contains_bar()?
Replaced locally with "i".
Bjorn
Re: [PATCH v7 10/13] PCI: Give pci(m)_intx its own devres callback
In reply to Ilpo JärvinenQuoted 10 lines · Ilpo Järvinen
Quoted 6 lines · Philipp Stanner
pci_intx()is one of the functions that have "hybrid mode" (i.e., sometimes managed, sometimes not). Providing a separatepcim_intx()function with its own device resource and cleanup callback allows for removing further large parts of the legacy PCI devres implementation.+/* Used to restore the old intx state on driver detach. */
INTx
Updated locally in v9 series.
Quoted 8 lines
drivers/pci/devres.c int pcim_set_mwi(struct pci_dev *pdev) · line 401
Quoted 6 lines
drivers/pci/devres.c +1
-0@@ -392,32 +397,75 @@ int pcim_set_mwi(struct pci_dev *pdev) }EXPORT_SYMBOL (pcim_set_mwi); +static inline bool mask_contains_bar (int mask,int bar)Stray change.
Updated locally in v9 series.
Re: [PATCH v7 09/13] PCI: Give pcim_set_mwi() its own devres callback
In reply to Ilpo JärvinenQuoted 53 lines · Ilpo Järvinen
Quoted 51 lines · Philipp Stanner
Managing
pci_set_mwi()with devres can easily be done with its own callback, without the necessity to store any state about it in a device-related struct.Remove the MWI state from struct
pci_devres.
Givepcim_set_mwi()a separate devres-callback.Signed-off-by Philipp Stanner <[email protected]>---
drivers/pci/devres.c | 29 ++++++++++++++++++----------- drivers/pci/pci.h | 1 -2 files changed, 18 insertions(+), 12 deletions(-)drivers/pci/devres.c +0
-0@@ -361,24 +361,34 @@ void __iomem *devm_pci_remap_cfg_resource(struct device *dev, } EXPORT_SYMBOL(devm_pci_remap_cfg_resource);drivers/pci/devres.c +7
-0+static void __pcim_clear_mwi (void *pdev_raw) +{ + struct pci_dev *pdev = pdev_raw; + + pci_clear_mwi(pdev); +} +/**
*pcim_set_mwi- a device-managedpci_set_mwi()
- * @dev: the PCI device for which MWI is enabled
+ * @pdev: the PCI device for which MWI is enabled
*
* Managedpci_set_mwi().
*
* RETURNS: An appropriate-ERRNOerror value on error, or zero for success.
*/drivers/pci/devres.c +1
-1-int pcim_set_mwi (struct pci_dev *dev) +int pcim_set_mwi (struct pci_dev *pdev){
- structpci_devres*dr;
+ int ret;
- dr =find_pci_dr(dev);
- if (!dr)
- return-ENOMEM;
+ ret =devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev); + if (ret != 0)
+ return ret;
Quoted 8 lines
Quoted 5 lines
+
+ ret =pci_set_mwi(pdev);
+ if (ret != 0)
+ devm_remove_action(&pdev->dev,__pcim_clear_mwi, pdev);I'm sorry if this is a stupid question but why this cannot use
devm_add_action_or_reset()?
For MWI that could be done.
This is basically just consistent with the new pcim_enable_device() in patch No.11 where devm_add_action_or_reset() could collide with pcim_pin_device().
We could squash usage of devm_add_action_or_reset() in here. I don't care.
P.
Quoted 7 lines
Quoted 6 lines
- dr->mwi = 1;
- returnpci_set_mwi(dev);
+ return ret;
}
EXPORT_SYMBOL(pcim_set_mwi);