summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlpo Järvinen <ilpo.jarvinen@linux.intel.com>2024-12-16 19:56:17 +0200
committerBjorn Helgaas <bhelgaas@google.com>2025-02-18 15:40:53 -0600
commite4728eed24a32f275106c498669a9d3177f9611e (patch)
tree0d03fba8b2266204a5322905611e605988c67473
parent2bd0c72117841f7fb82aab7f4a0d65e66ef9543e (diff)
PCI: Add pci_resource_num() helper
A few places in PCI code, mainly in setup-bus.c, need to reverse lookup the index of a resource in pci_dev's resource array. Create pci_resource_num() helper to avoid repeating the pointer arithmetic trick used to calculate the index. Link: https://lore.kernel.org/r/20241216175632.4175-11-ilpo.jarvinen@linux.intel.com Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Tested-by: Xiaochun Lee <lixc17@lenovo.com>
-rw-r--r--drivers/pci/pci.h24
-rw-r--r--drivers/pci/setup-bus.c10
2 files changed, 28 insertions, 6 deletions
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 89599e63f60c..996185abd30c 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -334,6 +334,28 @@ void pci_walk_bus_locked(struct pci_bus *top,
const char *pci_resource_name(struct pci_dev *dev, unsigned int i);
+/**
+ * pci_resource_num - Reverse lookup resource number from device resources
+ * @dev: PCI device
+ * @res: Resource to lookup index for (MUST be a @dev's resource)
+ *
+ * Perform reverse lookup to determine the resource number for @res within
+ * @dev resource array. NOTE: The caller is responsible for ensuring @res is
+ * among @dev's resources!
+ *
+ * Returns: resource number.
+ */
+static inline int pci_resource_num(const struct pci_dev *dev,
+ const struct resource *res)
+{
+ int resno = res - &dev->resource[0];
+
+ /* Passing a resource that is not among dev's resources? */
+ WARN_ON_ONCE(resno >= PCI_NUM_RESOURCES);
+
+ return resno;
+}
+
void pci_reassigndev_resource_alignment(struct pci_dev *dev);
void pci_disable_bridge_window(struct pci_dev *dev);
struct pci_bus *pci_bus_get(struct pci_bus *bus);
@@ -693,7 +715,7 @@ unsigned long pci_cardbus_resource_alignment(struct resource *);
static inline resource_size_t pci_resource_alignment(struct pci_dev *dev,
struct resource *res)
{
- int resno = res - dev->resource;
+ int resno = pci_resource_num(dev, res);
if (pci_resource_is_iov(resno))
return pci_sriov_resource_alignment(dev, resno);
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index eaeaf09cd91d..524a6381b25b 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -242,7 +242,7 @@ static void reassign_resources_sorted(struct list_head *realloc_head,
if (!found_match) /* Just skip */
continue;
- idx = res - &add_res->dev->resource[0];
+ idx = pci_resource_num(add_res->dev, res);
res_name = pci_resource_name(add_res->dev, idx);
add_size = add_res->add_size;
align = add_res->min_align;
@@ -284,7 +284,7 @@ static void assign_requested_resources_sorted(struct list_head *head,
list_for_each_entry(dev_res, head, list) {
res = dev_res->res;
- idx = res - &dev_res->dev->resource[0];
+ idx = pci_resource_num(dev_res->dev, res);
if (!resource_size(res))
continue;
@@ -2210,7 +2210,7 @@ again:
res->flags = fail_res->flags;
if (pci_is_bridge(fail_res->dev)) {
- idx = res - &fail_res->dev->resource[0];
+ idx = pci_resource_num(fail_res->dev, res);
if (idx >= PCI_BRIDGE_RESOURCES &&
idx <= PCI_BRIDGE_RESOURCE_END)
res->flags = 0;
@@ -2294,7 +2294,7 @@ again:
res->flags = fail_res->flags;
if (pci_is_bridge(fail_res->dev)) {
- idx = res - &fail_res->dev->resource[0];
+ idx = pci_resource_num(fail_res->dev, res);
if (idx >= PCI_BRIDGE_RESOURCES &&
idx <= PCI_BRIDGE_RESOURCE_END)
res->flags = 0;
@@ -2401,7 +2401,7 @@ cleanup:
struct resource *res = dev_res->res;
bridge = dev_res->dev;
- i = res - bridge->resource;
+ i = pci_resource_num(bridge, res);
res->start = dev_res->start;
res->end = dev_res->end;