[PATCH 14/15] PCI: Add iterators for devices and resources in bus windows
From: Ilpo Järvinen
Date: Tue Dec 01 2026 - 11:02:10 EST
A transparent (subtractive decode) bridge also forwards addresses that
fall outside of its own windows, so the resources of the devices behind
it may reside directly in the windows of the bus the bridge is on. Code
that looks for the resources within the windows of a bus has to walk the
devices behind transparent bridges too.
Add pci_bus_for_each_window_dev() that walks the devices on a bus and on
the buses below its transparent bridges, and
pci_bus_for_each_window_resource() that walks the resources of those
devices that reside directly in a given window of the bus.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@xxxxxxxxxxxxxxx>
---
drivers/pci/bus.c | 33 +++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 22 ++++++++++++++++++++++
2 files changed, 55 insertions(+)
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index 655ed53436d3..f70b4cfc07bf 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -490,6 +490,39 @@ void pci_walk_bus_locked(struct pci_bus *top, int (*cb)(struct pci_dev *, void *
__pci_walk_bus(top, cb, userdata);
}
+/* Return the device after @dev for pci_bus_for_each_window_dev() */
+struct pci_dev *pci_bus_next_window_dev(struct pci_bus *bus,
+ struct pci_dev *dev)
+{
+ struct pci_dev *child;
+ struct pci_bus *b;
+
+ if (!dev)
+ return list_first_entry_or_null(&bus->devices, struct pci_dev,
+ bus_list);
+
+ if (dev->transparent && dev->subordinate) {
+ child = list_first_entry_or_null(&dev->subordinate->devices,
+ struct pci_dev, bus_list);
+ if (child)
+ return child;
+ }
+
+ /*
+ * Ascend back from transparent buses, but no higher than the original
+ * bus.
+ */
+ b = dev->bus;
+ while (list_is_last(&dev->bus_list, &b->devices)) {
+ if (b == bus)
+ return NULL;
+ dev = b->self;
+ b = dev->bus;
+ }
+
+ return list_next_entry(dev, bus_list);
+}
+
struct pci_bus *pci_bus_get(struct pci_bus *bus)
{
if (bus)
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 77efa4c5c960..9d0cbe6c8991 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -6,6 +6,7 @@
#include <linux/align.h>
#include <linux/bitfield.h>
#include <linux/pci.h>
+#include <linux/util_macros.h>
#include <trace/events/pci.h>
struct pcie_tlp_log;
@@ -546,6 +547,27 @@ bool pci_bus_clip_resource(struct pci_dev *dev, int idx);
void pci_walk_bus_locked(struct pci_bus *top,
int (*cb)(struct pci_dev *, void *),
void *userdata);
+struct pci_dev *pci_bus_next_window_dev(struct pci_bus *bus,
+ struct pci_dev *dev);
+
+/*
+ * Iterate over the devices whose resources may reside directly in the
+ * windows of @bus: the devices on @bus and, recursively, the devices
+ * below its transparent bridges. A transparent bridge also forwards
+ * accesses outside of its own windows.
+ */
+#define pci_bus_for_each_window_dev(bus, dev) \
+ for (dev = pci_bus_next_window_dev(bus, NULL); dev; \
+ dev = pci_bus_next_window_dev(bus, dev))
+
+/*
+ * Iterate over the resources of the devices of
+ * pci_bus_for_each_window_dev() that reside directly in window @win.
+ */
+#define pci_bus_for_each_window_resource(bus, win, dev, res) \
+ pci_bus_for_each_window_dev(bus, dev) \
+ pci_dev_for_each_resource(dev, res) \
+ for_each_if((res)->parent == (win))
const char *pci_resource_name(struct pci_dev *dev, unsigned int i);
bool pci_resource_is_optional(const struct pci_dev *dev, int resno);
--
2.47.3