linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 0/3] PCI: distribute resources for root buses
@ 2023-01-12 10:59 Mika Westerberg
  2023-01-12 10:59 ` [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly Mika Westerberg
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mika Westerberg @ 2023-01-12 10:59 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: Rafael J . Wysocki, Andy Shevchenko, Jonathan Cameron,
	Lukas Wunner, Chris Chiu, Alexander Motin, Nicholas Johnson,
	Mika Westerberg, linux-pci

Hi all,

This is fifth iteration of the patch series trying to solve the problem
reported by Chris Chiu [1]. In summary the current resource distribution
code does not cover the initial device enumeration so if we find
unconfigured bridges they get the bare minimum.

In addition to that it turned out the current resource distribution code
does not take into account possible multifunction devices and/or other
devices on the bus. The patch 1/2 tries to make it more generic. I've
tested it on QEMU following the topology Jonathan is using and also in a
a couple of systems with Thunderbolt controller and complex topologies
to make sure it still keeps working.

The previous versions of the series can be found:

v4: https://lore.kernel.org/linux-pci/20230104091635.63331-1-mika.westerberg@linux.intel.com/

v3: https://lore.kernel.org/linux-pci/20221130112221.66612-1-mika.westerberg@linux.intel.com/

v2: https://lore.kernel.org/linux-pci/20221114115953.40236-1-mika.westerberg@linux.intel.com/
v1: https://lore.kernel.org/linux-pci/20221103103254.30497-1-mika.westerberg@linux.intel.com/

Changes from v4:
  * Split the alignment fix into separate patch
  * Create helper for removing resources
  * Skip VF BARs when removing
  * Add check for 32-bit/64-bit prefetchable resource so that we account
    them correctly.
  * Update comments and commit log slightly according to review
    comments.
  * Did not add the "lookup for hotplug bridges below non-hotplug ones".
    It turned out to be non-trivial. The current code works for the
    "common" case of Thunderbolt/USB4 PCIe switches but we may need to
    revisit this if there is a real need.

Changes from v3:
  * Make it more generic and not depend on how many bridges there are
    on the bus.

Changes from v2:
  * Make both patches to work with PCI devices too (do not expect that
    the bridge is always first device on the bus).
  * Allow distribution with bridges that do not have all resource
    windows programmed (therefore the patch 2/2 is not revert anymore)
  * I did not add the tags from Rafael and Jonathan because the code is
    not exactly the same anymore so was not sure if they still apply.

Changes from v1:
  * Re-worded the commit message to hopefully explain the problem better
  * Added Link: to the bug report
  * Update the comment according to Bjorn's suggestion
  * Dropped the ->multifunction check
  * Use %#llx in log format.

[1] https://bugzilla.kernel.org/show_bug.cgi?id=216000

Mika Westerberg (3):
  PCI: Align extra resources for hotplug bridges properly
  PCI: Take other bus devices into account when distributing resources
  PCI: Distribute available resources for root buses too

 drivers/pci/setup-bus.c | 232 ++++++++++++++++++++++++++++------------
 1 file changed, 166 insertions(+), 66 deletions(-)

-- 
2.39.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly
  2023-01-12 10:59 [PATCH v5 0/3] PCI: distribute resources for root buses Mika Westerberg
@ 2023-01-12 10:59 ` Mika Westerberg
  2023-01-26 16:53   ` Alexander Motin
  2023-01-12 10:59 ` [PATCH v5 2/3] PCI: Take other bus devices into account when distributing resources Mika Westerberg
  2023-01-12 11:00 ` [PATCH v5 3/3] PCI: Distribute available resources for root buses too Mika Westerberg
  2 siblings, 1 reply; 6+ messages in thread
From: Mika Westerberg @ 2023-01-12 10:59 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: Rafael J . Wysocki, Andy Shevchenko, Jonathan Cameron,
	Lukas Wunner, Chris Chiu, Alexander Motin, Nicholas Johnson,
	Mika Westerberg, linux-pci

After division the extra resource space per hotplug bridge may not be
aligned according to the window alignment so do that before passing it
down for further distribution.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
 drivers/pci/setup-bus.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index b4096598dbcb..34a74bc581b0 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1891,6 +1891,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 	 * resource space between hotplug bridges.
 	 */
 	for_each_pci_bridge(dev, bus) {
+		struct resource *res;
 		struct pci_bus *b;
 
 		b = dev->subordinate;
@@ -1902,16 +1903,28 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 		 * hotplug-capable downstream ports taking alignment into
 		 * account.
 		 */
-		io.end = io.start + io_per_hp - 1;
-		mmio.end = mmio.start + mmio_per_hp - 1;
-		mmio_pref.end = mmio_pref.start + mmio_pref_per_hp - 1;
+		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
+		align = pci_resource_alignment(dev, res);
+		io.end = align ? io.start + ALIGN_DOWN(io_per_hp, align) - 1
+			       : io.start + io_per_hp - 1;
+
+		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
+		align = pci_resource_alignment(dev, res);
+		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_hp, align) - 1
+				 : mmio.start + io_per_hp - 1;
+
+		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
+		align = pci_resource_alignment(dev, res);
+		mmio_pref.end = align ? mmio_pref.start +
+					ALIGN_DOWN(mmio_pref_per_hp, align) - 1
+				      : mmio_pref.start + mmio_pref_per_hp;
 
 		pci_bus_distribute_available_resources(b, add_list, io, mmio,
 						       mmio_pref);
 
-		io.start += io_per_hp;
-		mmio.start += mmio_per_hp;
-		mmio_pref.start += mmio_pref_per_hp;
+		io.start += io.end + 1;
+		mmio.start += mmio.end + 1;
+		mmio_pref.start += mmio_pref.end + 1;
 	}
 }
 
-- 
2.39.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v5 2/3] PCI: Take other bus devices into account when distributing resources
  2023-01-12 10:59 [PATCH v5 0/3] PCI: distribute resources for root buses Mika Westerberg
  2023-01-12 10:59 ` [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly Mika Westerberg
@ 2023-01-12 10:59 ` Mika Westerberg
  2023-01-12 11:00 ` [PATCH v5 3/3] PCI: Distribute available resources for root buses too Mika Westerberg
  2 siblings, 0 replies; 6+ messages in thread
From: Mika Westerberg @ 2023-01-12 10:59 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: Rafael J . Wysocki, Andy Shevchenko, Jonathan Cameron,
	Lukas Wunner, Chris Chiu, Alexander Motin, Nicholas Johnson,
	Mika Westerberg, linux-pci

A PCI bridge may reside on a bus with other devices as well. The
resource distribution code does not take this into account properly and
therefore it expands the bridge resource windows too much, not leaving
space for the other devices (or functions of a multifunction device) and
this leads to an issue that Jonathan reported. He runs QEMU with the
following topology (QEMU parameters):

 -device pcie-root-port,port=0,id=root_port13,chassis=0,slot=2  \
 -device x3130-upstream,id=sw1,bus=root_port13,multifunction=on \
 -device e1000,bus=root_port13,addr=0.1                         \
 -device xio3130-downstream,id=fun1,bus=sw1,chassis=0,slot=3    \
 -device e1000,bus=fun1

The first e1000 NIC here is another function in the switch upstream
port. This leads to following errors:

  pci 0000:00:04.0: bridge window [mem 0x10200000-0x103fffff] to [bus 02-04]
  pci 0000:02:00.0: bridge window [mem 0x10200000-0x103fffff] to [bus 03-04]
  pci 0000:02:00.1: BAR 0: failed to assign [mem size 0x00020000]
  e1000 0000:02:00.1: can't ioremap BAR 0: [??? 0x00000000 flags 0x0]

Fix this by taking into account bridge windows, device BARs and SR-IOV
PF BARs on the bus (PF BARs include space for VF BARS so only account PF
BARs), including the ones belonging to bridges themselves if it has any.

Link: https://lore.kernel.org/linux-pci/20221014124553.0000696f@huawei.com/
Link: https://lore.kernel.org/linux-pci/6053736d-1923-41e7-def9-7585ce1772d9@ixsystems.com/
Reported-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reported-by: Alexander Motin <mav@ixsystems.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
 drivers/pci/setup-bus.c | 172 ++++++++++++++++++++++++----------------
 1 file changed, 102 insertions(+), 70 deletions(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 34a74bc581b0..30b5388f4a1a 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1765,12 +1765,65 @@ static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
 		add_size = size - new_size;
 		pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
 			&add_size);
+	} else {
+		return;
 	}
 
 	res->end = res->start + new_size - 1;
 	remove_from_list(add_list, res);
 }
 
+static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
+				struct resource *res)
+{
+	resource_size_t size, align, tmp;
+
+	size = resource_size(res);
+	if (!size)
+		return;
+
+	align = pci_resource_alignment(dev, res);
+	align = align ? ALIGN(avail->start, align) - avail->start : 0;
+	tmp = align + size;
+	avail->start = min(avail->start + tmp, avail->end + 1);
+}
+
+static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
+				 struct resource *mmio, struct resource *mmio_pref)
+{
+	int i;
+
+	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
+		struct resource *res = &dev->resource[i];
+
+		if (resource_type(res) == IORESOURCE_IO) {
+			remove_dev_resource(io, dev, res);
+		} else if (resource_type(res) == IORESOURCE_MEM) {
+			/*
+			 * Make sure prefetchable memory is reduced from
+			 * the correct resource. Specifically we put
+			 * 32-bit prefetchable memory in non-prefetchable
+			 * window if there is an 64-bit pretchable window.
+			 *
+			 * See comments in __pci_bus_size_bridges() for
+			 * more information.
+			 */
+			if ((res->flags & IORESOURCE_PREFETCH) &&
+			    ((res->flags & IORESOURCE_MEM_64) ==
+			     (mmio_pref->flags & IORESOURCE_MEM_64)))
+				remove_dev_resource(mmio_pref, dev, res);
+			else
+				remove_dev_resource(mmio, dev, res);
+		}
+	}
+}
+
+/*
+ * io, mmio and mmio_pref contain the total amount of bridge window
+ * space available. This includes the minimal space needed to cover all
+ * the existing devices on the bus and the possible extra space that can
+ * be shared with the bridges.
+ */
 static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 					    struct list_head *add_list,
 					    struct resource io,
@@ -1780,7 +1833,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 	unsigned int normal_bridges = 0, hotplug_bridges = 0;
 	struct resource *io_res, *mmio_res, *mmio_pref_res;
 	struct pci_dev *dev, *bridge = bus->self;
-	resource_size_t io_per_hp, mmio_per_hp, mmio_pref_per_hp, align;
+	resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
 
 	io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
 	mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
@@ -1824,100 +1877,79 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 			normal_bridges++;
 	}
 
-	/*
-	 * There is only one bridge on the bus so it gets all available
-	 * resources which it can then distribute to the possible hotplug
-	 * bridges below.
-	 */
-	if (hotplug_bridges + normal_bridges == 1) {
-		dev = list_first_entry(&bus->devices, struct pci_dev, bus_list);
-		if (dev->subordinate)
-			pci_bus_distribute_available_resources(dev->subordinate,
-				add_list, io, mmio, mmio_pref);
-		return;
-	}
-
-	if (hotplug_bridges == 0)
+	if (!(hotplug_bridges + normal_bridges))
 		return;
 
 	/*
-	 * Calculate the total amount of extra resource space we can
-	 * pass to bridges below this one.  This is basically the
-	 * extra space reduced by the minimal required space for the
-	 * non-hotplug bridges.
+	 * Calculate the amount of space we can forward from "bus" to
+	 * any downstream buses, i.e., the space left over after
+	 * assigning the BARs and windows on "bus".
 	 */
-	for_each_pci_bridge(dev, bus) {
-		resource_size_t used_size;
-		struct resource *res;
-
-		if (dev->is_hotplug_bridge)
-			continue;
-
-		/*
-		 * Reduce the available resource space by what the
-		 * bridge and devices below it occupy.
-		 */
-		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
-		align = pci_resource_alignment(dev, res);
-		align = align ? ALIGN(io.start, align) - io.start : 0;
-		used_size = align + resource_size(res);
-		if (!res->parent)
-			io.start = min(io.start + used_size, io.end + 1);
-
-		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
-		align = pci_resource_alignment(dev, res);
-		align = align ? ALIGN(mmio.start, align) - mmio.start : 0;
-		used_size = align + resource_size(res);
-		if (!res->parent)
-			mmio.start = min(mmio.start + used_size, mmio.end + 1);
-
-		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
-		align = pci_resource_alignment(dev, res);
-		align = align ? ALIGN(mmio_pref.start, align) -
-			mmio_pref.start : 0;
-		used_size = align + resource_size(res);
-		if (!res->parent)
-			mmio_pref.start = min(mmio_pref.start + used_size,
-				mmio_pref.end + 1);
+	list_for_each_entry(dev, &bus->devices, bus_list) {
+		if (!dev->is_virtfn)
+			remove_dev_resources(dev, &io, &mmio, &mmio_pref);
 	}
 
-	io_per_hp = div64_ul(resource_size(&io), hotplug_bridges);
-	mmio_per_hp = div64_ul(resource_size(&mmio), hotplug_bridges);
-	mmio_pref_per_hp = div64_ul(resource_size(&mmio_pref),
-		hotplug_bridges);
-
 	/*
-	 * Go over devices on this bus and distribute the remaining
-	 * resource space between hotplug bridges.
+	 * If there is at least one hotplug bridge on this bus it gets
+	 * all the extra resource space that was left after the
+	 * reductions above.
+	 *
+	 * If there are no hotplug bridges the extra resource space is
+	 * split between non-hotplug bridges. This is to allow possible
+	 * hotplug bridges below them to get the extra space as well.
 	 */
+	if (hotplug_bridges) {
+		io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
+		mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
+		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
+					   hotplug_bridges);
+	} else {
+		io_per_b = div64_ul(resource_size(&io), normal_bridges);
+		mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
+		mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
+					   normal_bridges);
+	}
+
 	for_each_pci_bridge(dev, bus) {
 		struct resource *res;
 		struct pci_bus *b;
 
 		b = dev->subordinate;
-		if (!b || !dev->is_hotplug_bridge)
+		if (!b)
+			continue;
+		if (hotplug_bridges && !dev->is_hotplug_bridge)
 			continue;
 
+		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
 		/*
-		 * Distribute available extra resources equally between
-		 * hotplug-capable downstream ports taking alignment into
-		 * account.
+		 * Make sure the split resource space is properly
+		 * aligned for bridge windows (align it down to avoid
+		 * going above what is available).
 		 */
-		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
 		align = pci_resource_alignment(dev, res);
-		io.end = align ? io.start + ALIGN_DOWN(io_per_hp, align) - 1
-			       : io.start + io_per_hp - 1;
+		io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
+			       : io.start + io_per_b - 1;
+		/*
+		 * The x_per_b holds the extra resource space that can
+		 * be added for each bridge but there is the minimal
+		 * already reserved as well so adjust x.start down
+		 * accordingly to cover the whole space.
+		 */
+		io.start -= resource_size(res);
 
 		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
 		align = pci_resource_alignment(dev, res);
-		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_hp, align) - 1
-				 : mmio.start + io_per_hp - 1;
+		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
+				 : mmio.start + io_per_b - 1;
+		mmio.start -= resource_size(res);
 
 		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
 		align = pci_resource_alignment(dev, res);
 		mmio_pref.end = align ? mmio_pref.start +
-					ALIGN_DOWN(mmio_pref_per_hp, align) - 1
-				      : mmio_pref.start + mmio_pref_per_hp;
+					ALIGN_DOWN(mmio_pref_per_b, align) - 1
+				      : mmio_pref.start + mmio_pref_per_b;
+		mmio_pref.start -= resource_size(res);
 
 		pci_bus_distribute_available_resources(b, add_list, io, mmio,
 						       mmio_pref);
-- 
2.39.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v5 3/3] PCI: Distribute available resources for root buses too
  2023-01-12 10:59 [PATCH v5 0/3] PCI: distribute resources for root buses Mika Westerberg
  2023-01-12 10:59 ` [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly Mika Westerberg
  2023-01-12 10:59 ` [PATCH v5 2/3] PCI: Take other bus devices into account when distributing resources Mika Westerberg
@ 2023-01-12 11:00 ` Mika Westerberg
  2 siblings, 0 replies; 6+ messages in thread
From: Mika Westerberg @ 2023-01-12 11:00 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: Rafael J . Wysocki, Andy Shevchenko, Jonathan Cameron,
	Lukas Wunner, Chris Chiu, Alexander Motin, Nicholas Johnson,
	Mika Westerberg, linux-pci

Previously we distributed spare resources only upon hot-add, so if the
initial root bus scan found devices that had not been fully configured by
the BIOS, we allocated only enough resources to cover what was then
present. If some of those devices were hotplug bridges, we did not leave
any additional resource space for future expansion.

Distribute the available resources for root buses, too, to make this work
the same way as the normal hotplug case.

This is a new version of the patch after the revert due to the regression
reported by Jonathan Cameron. This one changes pci_bridge_resources_not_assigned()
to work with bridges that do not have all the resource windows
programmed by the boot firmware (previously we expected all I/O, memory
and prefetchable memory were all programmed).

Link: https://bugzilla.kernel.org/show_bug.cgi?id=216000
Link: https://lore.kernel.org/r/20220905080232.36087-5-mika.westerberg@linux.intel.com
Reported-by: Chris Chiu <chris.chiu@canonical.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
 drivers/pci/setup-bus.c | 57 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 30b5388f4a1a..7c5747e4dca1 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1770,7 +1770,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 remove_dev_resource(struct resource *avail, struct pci_dev *dev,
@@ -1968,6 +1971,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];
@@ -1979,6 +1984,54 @@ 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 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);
+		else
+			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.
@@ -2018,6 +2071,8 @@ void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
 	 */
 	__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)
-- 
2.39.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly
  2023-01-12 10:59 ` [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly Mika Westerberg
@ 2023-01-26 16:53   ` Alexander Motin
  2023-01-27  6:45     ` Mika Westerberg
  0 siblings, 1 reply; 6+ messages in thread
From: Alexander Motin @ 2023-01-26 16:53 UTC (permalink / raw)
  To: Mika Westerberg, Bjorn Helgaas
  Cc: Rafael J . Wysocki, Andy Shevchenko, Jonathan Cameron,
	Lukas Wunner, Chris Chiu, Nicholas Johnson, linux-pci

Hi Mika,

Unfortunately my system was de-racked meanwhile, so it will take few 
more days for me to test this.  So far I only successfully built it on 
my 5.15.79 kernel.  Meanwhile some comments below:

On 12.01.2023 05:59, Mika Westerberg wrote:
> After division the extra resource space per hotplug bridge may not be
> aligned according to the window alignment so do that before passing it
> down for further distribution.
> 
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> ---
>   drivers/pci/setup-bus.c | 25 +++++++++++++++++++------
>   1 file changed, 19 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index b4096598dbcb..34a74bc581b0 100644
> --- a/drivers/pci/setup-bus.c
> +++ b/drivers/pci/setup-bus.c
> @@ -1891,6 +1891,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
>   	 * resource space between hotplug bridges.
>   	 */
>   	for_each_pci_bridge(dev, bus) {
> +		struct resource *res;
>   		struct pci_bus *b;
>   
>   		b = dev->subordinate;
> @@ -1902,16 +1903,28 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
>   		 * hotplug-capable downstream ports taking alignment into
>   		 * account.
>   		 */
> -		io.end = io.start + io_per_hp - 1;
> -		mmio.end = mmio.start + mmio_per_hp - 1;
> -		mmio_pref.end = mmio_pref.start + mmio_pref_per_hp - 1;
> +		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
> +		align = pci_resource_alignment(dev, res);
> +		io.end = align ? io.start + ALIGN_DOWN(io_per_hp, align) - 1
> +			       : io.start + io_per_hp - 1;

Not bug probably, but if we align x_per_b down for one bridge, we could 
be able to increase it for other(s).

> +
> +		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
> +		align = pci_resource_alignment(dev, res);
> +		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_hp, align) - 1
> +				 : mmio.start + io_per_hp - 1;

Here is a typo, it should be mmio_per_hp here   ^^^.

> +
> +		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
> +		align = pci_resource_alignment(dev, res);
> +		mmio_pref.end = align ? mmio_pref.start +
> +					ALIGN_DOWN(mmio_pref_per_hp, align) - 1
> +				      : mmio_pref.start + mmio_pref_per_hp;
>   
>   		pci_bus_distribute_available_resources(b, add_list, io, mmio,
>   						       mmio_pref);
>   
> -		io.start += io_per_hp;
> -		mmio.start += mmio_per_hp;
> -		mmio_pref.start += mmio_pref_per_hp;
> +		io.start += io.end + 1;
> +		mmio.start += mmio.end + 1;
> +		mmio_pref.start += mmio_pref.end + 1;
>   	}
>   }
>   

-- 
Alexander Motin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly
  2023-01-26 16:53   ` Alexander Motin
@ 2023-01-27  6:45     ` Mika Westerberg
  0 siblings, 0 replies; 6+ messages in thread
From: Mika Westerberg @ 2023-01-27  6:45 UTC (permalink / raw)
  To: Alexander Motin
  Cc: Bjorn Helgaas, Rafael J . Wysocki, Andy Shevchenko,
	Jonathan Cameron, Lukas Wunner, Chris Chiu, Nicholas Johnson,
	linux-pci

Hi,

On Thu, Jan 26, 2023 at 11:53:55AM -0500, Alexander Motin wrote:
> Hi Mika,
> 
> Unfortunately my system was de-racked meanwhile, so it will take few more
> days for me to test this.  So far I only successfully built it on my 5.15.79
> kernel.  Meanwhile some comments below:

Okay, take your time and thanks for reviewing!

> 
> On 12.01.2023 05:59, Mika Westerberg wrote:
> > After division the extra resource space per hotplug bridge may not be
> > aligned according to the window alignment so do that before passing it
> > down for further distribution.
> > 
> > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> > ---
> >   drivers/pci/setup-bus.c | 25 +++++++++++++++++++------
> >   1 file changed, 19 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> > index b4096598dbcb..34a74bc581b0 100644
> > --- a/drivers/pci/setup-bus.c
> > +++ b/drivers/pci/setup-bus.c
> > @@ -1891,6 +1891,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
> >   	 * resource space between hotplug bridges.
> >   	 */
> >   	for_each_pci_bridge(dev, bus) {
> > +		struct resource *res;
> >   		struct pci_bus *b;
> >   		b = dev->subordinate;
> > @@ -1902,16 +1903,28 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
> >   		 * hotplug-capable downstream ports taking alignment into
> >   		 * account.
> >   		 */
> > -		io.end = io.start + io_per_hp - 1;
> > -		mmio.end = mmio.start + mmio_per_hp - 1;
> > -		mmio_pref.end = mmio_pref.start + mmio_pref_per_hp - 1;
> > +		res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
> > +		align = pci_resource_alignment(dev, res);
> > +		io.end = align ? io.start + ALIGN_DOWN(io_per_hp, align) - 1
> > +			       : io.start + io_per_hp - 1;
> 
> Not bug probably, but if we align x_per_b down for one bridge, we could be
> able to increase it for other(s).

Yeah, the down align is just to make sure we don't run over what is
there because of the splitting. We could for example leave the
"leftovers" to the last bridge or so but I don't think we want to do it
in this patch series to avoid any additional bugs creeping in. Unless
you guys think it needs to be done, of course.

> > +
> > +		res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
> > +		align = pci_resource_alignment(dev, res);
> > +		mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_hp, align) - 1
> > +				 : mmio.start + io_per_hp - 1;
> 
> Here is a typo, it should be mmio_per_hp here   ^^^.

Good catch! I went over these specifically for things like this but I
still missed it :( Will fix in next version.

> > +
> > +		res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
> > +		align = pci_resource_alignment(dev, res);
> > +		mmio_pref.end = align ? mmio_pref.start +
> > +					ALIGN_DOWN(mmio_pref_per_hp, align) - 1
> > +				      : mmio_pref.start + mmio_pref_per_hp;
> >   		pci_bus_distribute_available_resources(b, add_list, io, mmio,
> >   						       mmio_pref);
> > -		io.start += io_per_hp;
> > -		mmio.start += mmio_per_hp;
> > -		mmio_pref.start += mmio_pref_per_hp;
> > +		io.start += io.end + 1;
> > +		mmio.start += mmio.end + 1;
> > +		mmio_pref.start += mmio_pref.end + 1;
> >   	}
> >   }
> 
> -- 
> Alexander Motin

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-01-27  6:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-12 10:59 [PATCH v5 0/3] PCI: distribute resources for root buses Mika Westerberg
2023-01-12 10:59 ` [PATCH v5 1/3] PCI: Align extra resources for hotplug bridges properly Mika Westerberg
2023-01-26 16:53   ` Alexander Motin
2023-01-27  6:45     ` Mika Westerberg
2023-01-12 10:59 ` [PATCH v5 2/3] PCI: Take other bus devices into account when distributing resources Mika Westerberg
2023-01-12 11:00 ` [PATCH v5 3/3] PCI: Distribute available resources for root buses too Mika Westerberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).