Skip to contentSkip to conversation

PCI: Allow for future resource expansion on initial root bus scan

PATCH Superseded Mika Westerberg 3 participants 12 messages Aug 16, 2022 – Sep 1, 2022 PCI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover PCI: Allow for future resource expansion on initial root bus scan

Hi,

The series works around an issue found on some Dell systems where booting with Thunderbolt/USB4 devices connected the BIOS leaves some of the PCIe devices unconfigured. If the connected devices that are not configured have PCIe hotplug ports as well the initial root bus scan only reserves the minimum amount of resources to them making any expansion happening later impossible.

We do already distribute the "spare" resources between hotplug ports on hot-add but we have not done that upon the initial scan. The first three patches make the initial root bus scan path to do the same.

The additional three patches are just a small cleanups that can be applied separately too.

The related bug: https://bugzilla.kernel.org/show_bug.cgi?id=216000.

Mika Westerberg (6):
PCI: Fix used_buses calculation in pci_scan_child_bus_extend()
PCI: Pass available buses also when the bridge is already configured
PCI: Distribute available resources for root buses too
PCI: Remove two unnecessary empty lines in pci_scan_child_bus_extend()
PCI: Fix typo in pci_scan_child_bus_extend()
PCI: Fix indentation in pci_bridge_distribute_available_resources()

 drivers/pci/probe.c     |  13 +-
 drivers/pci/setup-bus.c | 290 ++++++++++++++++++++++++----------------
 2 files changed, 181 insertions(+), 122 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/6 PCI: Fix used_buses calculation in pci_scan_child_bus_extend()

The 'cmax' and 'max' are in wrong order and that might cause the used_buses to become negative in certain cases. It should be the same as we do in the block following this one. Fix this.

Fixes 3374c545c27c ("PCI: Account for all bridges on bus when distributing bus numbers")
---
 drivers/pci/probe.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +2 -2
index c5286b027f00..4f940dcd102c 100644
@@ -2920,8 +2920,8 @@ static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
 		 * hotplug bridges too much during the second scan below.
 		 */
 		used_buses++;
-		if (cmax - max > 1)
-			used_buses += cmax - max - 1;
+		if (max - cmax > 1)
+			used_buses += max - cmax - 1;
 	}
 
 	/* Scan bridges that need to be reconfigured */
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/6 PCI: Pass available buses also when the bridge is already configured

In reply to Mika Westerberg

If some part of the PCI topology is already configured (by the boot firmware) but not all, and it includes hotplug bridges, we may need to extend the bus resources of those bridges to accommondate any future hotplugs too - in the same way we already do with normal hotplug case.

For this reason pass the available busses to pci_scan_child_bus_extend() even when the bridge in question is already configured. This allows the bus allocation code to use these available buses to extend the possible hotplug bridges below.

---
 drivers/pci/probe.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/pci/probe.c +3 -2
index 4f940dcd102c..86130926a74f 100644
@@ -1297,7 +1297,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 
 	if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
 	    !is_cardbus && !broken) {
-		unsigned int cmax;
+		unsigned int cmax, buses;
 
 		/*
 		 * Bus already configured by firmware, process it in the
@@ -1322,7 +1322,8 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 			child->bridge_ctl = bctl;
 		}
 
-		cmax = pci_scan_child_bus(child);
+		buses = subordinate - secondary;
+		cmax = pci_scan_child_bus_extend(child, buses);
 		if (cmax > subordinate)
 			pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
 				 subordinate, cmax);
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/6 PCI: Distribute available resources for root buses too

In reply to Mika Westerberg

Currently we distribute the spare resources only upon hot-add so if there are PCI devices connected already when the initial root bus scan is done, and they have not been fully configured by the BIOS, we may end up allocating resources just enough to cover only what is currently there. If some of those devices are hotplug bridges themselves we do not leave any additional resource space for future expansion.

For this reason distribute the available resources for root buses too to make this work the same way we do in the normal hotplug case.

---
The diffstat is quite big here but this is due the fact that we move the pci_assign_unassigned_root_bus_resources() below pci_bridge_distribute_available_resources() so we can call it without adding forward declaration.

 drivers/pci/setup-bus.c | 288 ++++++++++++++++++++++++----------------
 1 file changed, 174 insertions(+), 114 deletions(-)
drivers/pci/setup-bus.c +174 -114
index 8cb68e6f6ef9..df9fc974b313 100644
@@ -1745,119 +1745,6 @@ static enum enable_type pci_realloc_detect(struct pci_bus *bus,
 }
 #endif
 
-/*
- * First try will not touch PCI bridge res.
- * Second and later try will clear small leaf bridge res.
- * Will stop till to the max depth if can not find good one.
- */
-void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
-{
-	LIST_HEAD(realloc_head);
-	/* List of resources that want additional resources */
-	struct list_head *add_list = NULL;
-	int tried_times = 0;
-	enum release_type rel_type = leaf_only;
-	LIST_HEAD(fail_head);
-	struct pci_dev_resource *fail_res;
-	int pci_try_num = 1;
-	enum enable_type enable_local;
-
-	/* Don't realloc if asked to do so */
-	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
-	if (pci_realloc_enabled(enable_local)) {
-		int max_depth = pci_bus_get_depth(bus);
-
-		pci_try_num = max_depth + 1;
-		dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
-			 max_depth, pci_try_num);
-	}
-
-again:
-	/*
-	 * Last try will use add_list, otherwise will try good to have as must
-	 * have, so can realloc parent bridge resource
-	 */
-	if (tried_times + 1 == pci_try_num)
-		add_list = &realloc_head;
-	/*
-	 * Depth first, calculate sizes and alignments of all subordinate buses.
-	 */
-	__pci_bus_size_bridges(bus, add_list);
-
-	/* Depth last, allocate resources and update the hardware. */
-	__pci_bus_assign_resources(bus, add_list, &fail_head);
-	if (add_list)
-		BUG_ON(!list_empty(add_list));
-	tried_times++;
-
-	/* Any device complain? */
-	if (list_empty(&fail_head))
-		goto dump;
-
-	if (tried_times >= pci_try_num) {
-		if (enable_local == undefined)
-			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
-		else if (enable_local == auto_enabled)
-			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
-
-		free_list(&fail_head);
-		goto dump;
-	}
-
-	dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
-		 tried_times + 1);
-
-	/* Third times and later will not check if it is leaf */
-	if ((tried_times + 1) > 2)
-		rel_type = whole_subtree;
-
-	/*
-	 * Try to release leaf bridge's resources that doesn't fit resource of
-	 * child device under that bridge.
-	 */
-	list_for_each_entry(fail_res, &fail_head, list)
-		pci_bus_release_bridge_resources(fail_res->dev->bus,
-						 fail_res->flags & PCI_RES_TYPE_MASK,
-						 rel_type);
-
-	/* Restore size and flags */
-	list_for_each_entry(fail_res, &fail_head, list) {
-		struct resource *res = fail_res->res;
-		int idx;
-
-		res->start = fail_res->start;
-		res->end = fail_res->end;
-		res->flags = fail_res->flags;
-
-		if (pci_is_bridge(fail_res->dev)) {
-			idx = res - &fail_res->dev->resource[0];
-			if (idx >= PCI_BRIDGE_RESOURCES &&
-			    idx <= PCI_BRIDGE_RESOURCE_END)
-				res->flags = 0;
-		}
-	}
-	free_list(&fail_head);
-
-	goto again;
-
-dump:
-	/* Dump the resource on buses */
-	pci_bus_dump_resources(bus);
-}
-
-void __init pci_assign_unassigned_resources(void)
-{
-	struct pci_bus *root_bus;
-
-	list_for_each_entry(root_bus, &pci_root_buses, node) {
-		pci_assign_unassigned_root_bus_resources(root_bus);
-
-		/* Make sure the root bridge has a companion ACPI device */
-		if (ACPI_HANDLE(root_bus->bridge))
-			acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
-	}
-}
-
 static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
 				 struct list_head *add_list,
 				 resource_size_t new_size)
@@ -1881,7 +1768,10 @@ static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
 	}
 
 	res->end = res->start + new_size - 1;
-	remove_from_list(add_list, res);
+
+	/* If the resource is part of the add_list remove it now */
+	if (add_list)
+		remove_from_list(add_list, res);
 }
 
 static void pci_bus_distribute_available_resources(struct pci_bus *bus,
@@ -2036,6 +1926,8 @@ static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
 	if (!bridge->is_hotplug_bridge)
 		return;
 
+	pci_dbg(bridge, "distributing available resources\n");
+
 	/* Take the initial extra resources from the hotplug port */
 	available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
 	available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
@@ -2047,6 +1939,174 @@ static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
 					       available_mmio_pref);
 }
 
+static bool pci_bridge_resources_not_assigned(struct pci_dev *dev)
+{
+	const struct resource *r;
+
+	/*
+	 * Check the child device's resources and if they are not yet
+	 * assigned it means we are configuring them (not the boot
+	 * firmware) so we should be able to extend the upstream
+	 * bridge's (that's the hotplug downstream PCIe port) resources
+	 * in the same way we do with the normal hotplug case.
+	 */
+	r = &dev->resource[PCI_BRIDGE_IO_WINDOW];
+	if (!r->flags || !(r->flags & IORESOURCE_STARTALIGN))
+		return false;
+	r = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
+	if (!r->flags || !(r->flags & IORESOURCE_STARTALIGN))
+		return false;
+	r = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
+	if (!r->flags || !(r->flags & IORESOURCE_STARTALIGN))
+		return false;
+
+	return true;
+}
+
+static void pci_root_bus_distribute_available_resources(struct pci_bus *bus,
+							struct list_head *add_list)
+{
+	struct pci_dev *dev, *bridge = bus->self;
+
+	for_each_pci_bridge(dev, bus) {
+		struct pci_bus *b;
+
+		b = dev->subordinate;
+		if (!b)
+			continue;
+
+		/*
+		 * Need to check "bridge" here too because it is NULL
+		 * in case of root bus.
+		 */
+		if (bridge && pci_bridge_resources_not_assigned(dev)) {
+			pci_bridge_distribute_available_resources(bridge, add_list);
+			/*
+			 * There is only PCIe upstream port on the bus
+			 * so we don't need to go futher.
+			 */
+			return;
+		}
+
+		pci_root_bus_distribute_available_resources(b, add_list);
+	}
+}
+
+/*
+ * First try will not touch PCI bridge res.
+ * Second and later try will clear small leaf bridge res.
+ * Will stop till to the max depth if can not find good one.
+ */
+void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
+{
+	LIST_HEAD(realloc_head);
+	/* List of resources that want additional resources */
+	struct list_head *add_list = NULL;
+	int tried_times = 0;
+	enum release_type rel_type = leaf_only;
+	LIST_HEAD(fail_head);
+	struct pci_dev_resource *fail_res;
+	int pci_try_num = 1;
+	enum enable_type enable_local;
+
+	/* Don't realloc if asked to do so */
+	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
+	if (pci_realloc_enabled(enable_local)) {
+		int max_depth = pci_bus_get_depth(bus);
+
+		pci_try_num = max_depth + 1;
+		dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
+			 max_depth, pci_try_num);
+	}
+
+again:
+	/*
+	 * Last try will use add_list, otherwise will try good to have as must
+	 * have, so can realloc parent bridge resource
+	 */
+	if (tried_times + 1 == pci_try_num)
+		add_list = &realloc_head;
+	/*
+	 * Depth first, calculate sizes and alignments of all subordinate buses.
+	 */
+	__pci_bus_size_bridges(bus, add_list);
+
+	pci_root_bus_distribute_available_resources(bus, add_list);
+
+	/* Depth last, allocate resources and update the hardware. */
+	__pci_bus_assign_resources(bus, add_list, &fail_head);
+	if (add_list)
+		BUG_ON(!list_empty(add_list));
+	tried_times++;
+
+	/* Any device complain? */
+	if (list_empty(&fail_head))
+		goto dump;
+
+	if (tried_times >= pci_try_num) {
+		if (enable_local == undefined)
+			dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
+		else if (enable_local == auto_enabled)
+			dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
+
+		free_list(&fail_head);
+		goto dump;
+	}
+
+	dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
+		 tried_times + 1);
+
+	/* Third times and later will not check if it is leaf */
+	if ((tried_times + 1) > 2)
+		rel_type = whole_subtree;
+
+	/*
+	 * Try to release leaf bridge's resources that doesn't fit resource of
+	 * child device under that bridge.
+	 */
+	list_for_each_entry(fail_res, &fail_head, list)
+		pci_bus_release_bridge_resources(fail_res->dev->bus,
+						 fail_res->flags & PCI_RES_TYPE_MASK,
+						 rel_type);
+
+	/* Restore size and flags */
+	list_for_each_entry(fail_res, &fail_head, list) {
+		struct resource *res = fail_res->res;
+		int idx;
+
+		res->start = fail_res->start;
+		res->end = fail_res->end;
+		res->flags = fail_res->flags;
+
+		if (pci_is_bridge(fail_res->dev)) {
+			idx = res - &fail_res->dev->resource[0];
+			if (idx >= PCI_BRIDGE_RESOURCES &&
+			    idx <= PCI_BRIDGE_RESOURCE_END)
+				res->flags = 0;
+		}
+	}
+	free_list(&fail_head);
+
+	goto again;
+
+dump:
+	/* Dump the resource on buses */
+	pci_bus_dump_resources(bus);
+}
+
+void __init pci_assign_unassigned_resources(void)
+{
+	struct pci_bus *root_bus;
+
+	list_for_each_entry(root_bus, &pci_root_buses, node) {
+		pci_assign_unassigned_root_bus_resources(root_bus);
+
+		/* Make sure the root bridge has a companion ACPI device */
+		if (ACPI_HANDLE(root_bus->bridge))
+			acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
+	}
+}
+
 void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
 {
 	struct pci_bus *parent = bridge->subordinate;
Signature · 1 line
2.35.1
#4 lore raw

Re: [PATCH 3/6] PCI: Distribute available resources for root buses too

Quoted 10 lines · Mika Westerberg

Currently we distribute the spare resources only upon hot-add so if there are PCI devices connected already when the initial root bus scan is done, and they have not been fully configured by the BIOS, we may end up allocating resources just enough to cover only what is currently there. If some of those devices are hotplug bridges themselves we do not leave any additional resource space for future expansion.

For this reason distribute the available resources for root buses too to make this work the same way we do in the normal hotplug case.

Quoted 8 lines

---
The diffstat is quite big here but this is due the fact that we move the pci_assign_unassigned_root_bus_resources() below pci_bridge_distribute_available_resources() so we can call it without adding forward declaration.

Perhaps you can split to move and actual update?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

PATCH 4/6 PCI: Remove two unnecessary empty lines in pci_scan_child_bus_extend()

In reply to Mika Westerberg

These are not needed and are against the normal coding style anyway.

---
 drivers/pci/probe.c | 2 --
 1 file changed, 2 deletions(-)
drivers/pci/probe.c +0 -2
index 86130926a74f..8f25deb6b763 100644
@@ -2930,7 +2930,6 @@ static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
 		unsigned int buses = 0;
 
 		if (!hotplug_bridges && normal_bridges == 1) {
-
 			/*
 			 * There is only one bridge on the bus (upstream
 			 * port) so it gets all available buses which it
@@ -2939,7 +2938,6 @@ static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
 			 */
 			buses = available_buses;
 		} else if (dev->is_hotplug_bridge) {
-
 			/*
 			 * Distribute the extra buses between hotplug
 			 * bridges if any.
Signature · 1 line
2.35.1
#6 lore raw

PATCH 5/6 PCI: Fix typo in pci_scan_child_bus_extend()

In reply to Mika Westerberg

Should be 'if' not 'of'. Fix this.

---
 drivers/pci/probe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/probe.c +1 -1
index 8f25deb6b763..b66fa42c4b1f 100644
@@ -2956,7 +2956,7 @@ static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
 	/*
 	 * Make sure a hotplug bridge has at least the minimum requested
 	 * number of buses but allow it to grow up to the maximum available
-	 * bus number of there is room.
+	 * bus number if there is room.
 	 */
 	if (bus->self && bus->self->is_hotplug_bridge) {
 		used_buses = max_t(unsigned int, available_buses,
Signature · 1 line
2.35.1
#7 lore raw

PATCH 6/6 PCI: Fix indentation in pci_bridge_distribute_available_resources()

In reply to Mika Westerberg

Correct the indentation according the coding style.

---
 drivers/pci/setup-bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/setup-bus.c +1 -1
index df9fc974b313..dc6a30ee6edf 100644
@@ -1919,7 +1919,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 }
 
 static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
-						     struct list_head *add_list)
+						      struct list_head *add_list)
 {
 	struct resource available_io, available_mmio, available_mmio_pref;
 
Signature · 1 line
2.35.1
#8 lore raw

Re: [PATCH 6/6] PCI: Fix indentation in pci_bridge_distribute_available_resources()

Quoted 1 line · Mika Westerberg

Correct the indentation according the coding style.

I believe this can be squashed to patch 4 with combined description about fixing indentation (blank lines are also kinda indentations).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Tested-by on the v1 series
#9 lore raw
In reply to Mika Westerberg
Quoted 27 lines · Mika Westerberg

Hi,

The series works around an issue found on some Dell systems where booting with Thunderbolt/USB4 devices connected the BIOS leaves some of the PCIe devices unconfigured. If the connected devices that are not configured have PCIe hotplug ports as well the initial root bus scan only reserves the minimum amount of resources to them making any expansion happening later impossible.

We do already distribute the "spare" resources between hotplug ports on hot-add but we have not done that upon the initial scan. The first three patches make the initial root bus scan path to do the same.

The additional three patches are just a small cleanups that can be applied separately too.

The related bug: https://bugzilla.kernel.org/show_bug.cgi?id=216000.

Mika Westerberg (6):
PCI: Fix used_buses calculation in pci_scan_child_bus_extend()
PCI: Pass available buses also when the bridge is already configured
PCI: Distribute available resources for root buses too
PCI: Remove two unnecessary empty lines in pci_scan_child_bus_extend()
PCI: Fix typo in pci_scan_child_bus_extend()
PCI: Fix indentation in pci_bridge_distribute_available_resources()

Quoted 7 lines
 drivers/pci/probe.c     |  13 +-
 drivers/pci/setup-bus.c | 290 ++++++++++++++++++++++++----------------
 2 files changed, 181 insertions(+), 122 deletions(-)

--
2.35.1

Reviewed-by on the v1 series
#10 lore raw
In reply to Mika Westerberg
Quoted 9 lines · Mika Westerberg

Hi,

The series works around an issue found on some Dell systems where booting with Thunderbolt/USB4 devices connected the BIOS leaves some of the PCIe devices unconfigured. If the connected devices that are not configured have PCIe hotplug ports as well the initial root bus scan only reserves the minimum amount of resources to them making any expansion happening later impossible.

Quoted 8 lines

We do already distribute the "spare" resources between hotplug ports on hot-add but we have not done that upon the initial scan. The first three patches make the initial root bus scan path to do the same.

The additional three patches are just a small cleanups that can be applied separately too.

The related bug: https://bugzilla.kernel.org/show_bug.cgi?id=216000.

With split and squash or not, LGTM,

Quoted 15 lines

Mika Westerberg (6):
PCI: Fix used_buses calculation in pci_scan_child_bus_extend()
PCI: Pass available buses also when the bridge is already configured
PCI: Distribute available resources for root buses too
PCI: Remove two unnecessary empty lines in pci_scan_child_bus_extend()
PCI: Fix typo in pci_scan_child_bus_extend()
PCI: Fix indentation in pci_bridge_distribute_available_resources()

 drivers/pci/probe.c     |  13 +-
 drivers/pci/setup-bus.c | 290 ++++++++++++++++++++++++----------------
 2 files changed, 181 insertions(+), 122 deletions(-)
Signature · 1 line
2.35.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw
Quoted 14 lines · Andy Shevchenko
Quoted 13 lines · Mika Westerberg

Hi,

The series works around an issue found on some Dell systems where booting with Thunderbolt/USB4 devices connected the BIOS leaves some of the PCIe devices unconfigured. If the connected devices that are not configured have PCIe hotplug ports as well the initial root bus scan only reserves the minimum amount of resources to them making any expansion happening later impossible.

We do already distribute the "spare" resources between hotplug ports on hot-add but we have not done that upon the initial scan. The first three patches make the initial root bus scan path to do the same.

Quoted 7 lines
Quoted 4 lines

The additional three patches are just a small cleanups that can be applied separately too.

The related bug: https://bugzilla.kernel.org/show_bug.cgi?id=216000.

With split and squash or not, LGTM,

Thanks Andy!

I will do the changes you suggested in v2.