Skip to contentSkip to conversation

Fix VFCT matching after PCI bus renumbering

PATCH Superseded Mario Limonciello 4 participants 10 messages Nov 12 – Nov 19

Subsystems (3)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v7 cover Fix VFCT matching after PCI bus renumbering

amdgpu and radeon pick the VFCT image for a GPU by comparing the PCI bus number recorded by the BIOS at POST with the device's bus number. When the kernel renumbers buses (e.g. pci=realloc), the runtime number no longer matches the one in VFCT.

commit 11c141672045f ("drm/amdgpu: Fix VFCT bus number matching with soft filter") and its radeon counterpart worked around this by accepting an entry whenever everything but the bus number matched. That picks the wrong image when a system has two identical GPUs.

Instead, have the PCI core remember the secondary bus number the firmware assigned to each bridge, and make amdgpu and radeon match the VFCT bus number against it again, using the bridge above the GPU. The value is captured once, when the bridge windows are first read, so renumbering and later rescans don't overwrite it.

Patch 1 adds the cached firmware secondary bus number to struct pci_dev. Patches 2 and 3 convert amdgpu and radeon to use it and drop the soft filter.

---
Changes in v7:
- Reword the cover letter and the PCI patch to describe the current design:
the firmware secondary bus number is cached on the bridge pci_dev when its bridge windows are first read, and amdgpu/radeon match VFCT against it.
- No code changes.
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
- Capture the firmware secondary bus number when reading bridge windows and ignore unconfigured values so rescans cannot replace it with runtime data (Sashiko).
- No change needed in the VFCT consumers; they continue to read the cached value from the upstream bridge.
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- Record the firmware secondary bus number only on the bridge struct pci_dev, guard it with a validity bit, and keep it across rescans (Bjorn, Lijo).
- Drop pci_bus::number_initial and have amdgpu/radeon read the saved bridge number when matching VFCT entries.

Changes in v4:
- Retain secondary bus bus number for a bridge across rescans, and the child bus is initialized from it during allocation, before registration callbacks run. (Sashiko)
- Rename variable to clarify it's for bridges (Lijo)
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Preserve the firmware bus number across bridge renumbering, capturing the secondary bus number before pci_scan_bridge_extend() clears it.
- Adjust member name in all patches
- Add tags
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- pci_alloc_dev(NULL) remains safe.
- For buses behind a reassigned bridge, the PCI core now saves the firmware secondary bus number before clearing the bridge configuration and carries it into the newly allocated bus. Devices discovered there inherit that original number.
- Add changes for radeon too.
- Link to v1: https://patch.msgid.link/[email protected]

---
Mario Limonciello (3):
PCI: Cache bridge firmware secondary bus number drm/amdgpu: Match VFCT using initial PCI bus number drm/radeon: Match VFCT using initial PCI bus number

 drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c | 23 ++++++++++-------------
 drivers/gpu/drm/radeon/radeon_bios.c     | 25 +++++++++++--------------
 drivers/pci/probe.c                      | 16 ++++++++++++++--
 include/linux/pci.h                      |  4 ++++
 4 files changed, 39 insertions(+), 29 deletions(-)

---
base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
change-id: 20261019-vfct-regression-2142fc52852a

Best regards,

Signature · 1 line
Mario Limonciello <[email protected]>
#1 lore raw

PATCH v7 1/3 PCI: Cache bridge firmware secondary bus number

Drivers such as amdgpu and radeon need the bus number firmware assigned to a device to match it against firmware tables like VFCT. The PCI core may renumber buses (e.g. with pci=realloc), after which the runtime number no longer reflects what firmware programmed.

Record the firmware secondary bus number on the bridge's struct pci_dev the first time pci_read_bridge_windows() reads the bridge's bus numbers, which happens before any renumbering. Devices behind the bridge can then retrieve the original bus number via their upstream bridge.

Only store the value if it is usable: ignore unconfigured (0) and 0xff secondaries. Use a separate "sampled" flag so that later rescans, which would read the renumbered runtime value, never overwrite the cached number, and a "valid" flag so users can tell whether anything was recorded.

---
 drivers/pci/probe.c | 16 ++++++++++++++--
 include/linux/pci.h |  4 ++++
 2 files changed, 18 insertions(+), 2 deletions(-)
drivers/pci/probe.c +14 -2
index 27008e2ea5afc..f20a4b8f7a23b 100644
@@ -505,14 +505,26 @@ static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res,
 static void pci_read_bridge_windows(struct pci_dev *bridge)
 {
 	u32 buses;
+	u8 secondary;
+	u8 subordinate;
 	u16 io;
 	u32 pmem, tmp;
 	struct resource res;
 
 	pci_read_config_dword(bridge, PCI_PRIMARY_BUS, &buses);
 	res.flags = IORESOURCE_BUS;
-	res.start = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
-	res.end = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);
+	secondary = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
+	subordinate = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);
+	res.start = secondary;
+	res.end = subordinate;
+
+	if (!bridge->fw_sec_bus_sampled) {
+		bridge->fw_sec_bus_sampled = true;
+		if (secondary > 0 && secondary < 0xff) {
+			bridge->fw_sec_busnr = secondary;
+			bridge->fw_sec_bus_valid = true;
+		}
+	}
 	pci_info(bridge, "PCI bridge to %pR%s\n", &res,
 		 bridge->transparent ? " (subtractive decode)" : "");
 
include/linux/pci.h +4 -0
index 5ffcf5ce8c966..045157488b8e7 100644
@@ -363,6 +363,8 @@ struct pci_dev {
 	struct pci_slot	*slot;		/* Physical slot this device is in */
 
 	unsigned int	devfn;		/* Encoded device & function index */
+	/* Firmware secondary bus number saved across bridge scan passes */
+	unsigned char	fw_sec_busnr;
 	unsigned short	vendor;
 	unsigned short	device;
 	unsigned short	subsystem_vendor;
@@ -414,6 +416,8 @@ struct pci_dev {
 	unsigned int	pme_poll:1;	/* Poll device's PME status bit */
 	unsigned int	pinned:1;	/* Whether this dev is pinned */
 	unsigned int	config_rrs_sv:1; /* Config RRS software visibility */
+	unsigned int	fw_sec_bus_valid:1; /* Firmware secondary bus number usable */
+	unsigned int	fw_sec_bus_sampled:1; /* Firmware secondary bus number captured */
 	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 */
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v7 1/3] PCI: Cache bridge firmware secondary bus number

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] PCI: dead fw_sec_bus_sampled in pci_read_bridge_windows() and log mismatch --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] PCI: dead fw_sec_bus_sampled in pci_read_bridge_windows() and log mismatch
--

commit d3720492f32e5a284ef73b3dc9f69d02b13df2c4
Author: Mario Limonciello <[email protected]>

PCI: Cache bridge firmware secondary bus number

This commit caches the firmware-assigned secondary bus number on a PCI bridge's struct pci_dev in pci_read_bridge_windows() before bus renumbering can occur.

Quoted 20 lines
drivers/pci/probe.c +4 -2
index 27008e2ea5afc..f20a4b8f7a23b 100644
@@ -505,14 +505,26 @@ static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res,
 static void pci_read_bridge_windows(struct pci_dev *bridge)
 {
 	u32 buses;
+	u8 secondary;
+	u8 subordinate;
 	u16 io;
 	u32 pmem, tmp;
 	struct resource res;
 
 	pci_read_config_dword(bridge, PCI_PRIMARY_BUS, &buses);
 	res.flags = IORESOURCE_BUS;
-	res.start = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
-	res.end = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);
+	secondary = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
+	subordinate = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);

drivers/pci/probe.c static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res, · line 527

Quoted 10 lines
drivers/pci/probe.c +10 -0
+	res.start = secondary;
+	res.end = subordinate;
+
+	if (!bridge->fw_sec_bus_sampled) {
+		bridge->fw_sec_bus_sampled = true;
+		if (secondary > 0 && secondary < 0xff) {
+			bridge->fw_sec_busnr = secondary;
+			bridge->fw_sec_bus_valid = true;
+		}
+	}

[Severity: Low]
As reported by Lazar, Lijo on v5 [1], is bridge->fw_sec_bus_sampled needed here?

pci_read_bridge_windows() is only called from pci_setup_device() when pci_scan_device() first allocates and initializes a new struct pci_dev. On later bus rescans, pci_scan_single_device() finds the existing device via pci_get_slot() and returns early without calling pci_setup_device() or pci_read_bridge_windows(). Because pci_read_bridge_windows() runs at most once per bridge instance on a zero-initialized struct pci_dev, fw_sec_bus_sampled is always false on entry and is never read elsewhere, making the bitfield and the commit message's rescan explanation unnecessary. In addition, since bus 0 is the root bus and fw_sec_busnr is only set when secondary > 0 && secondary < 0xff, fw_sec_bus_valid is redundant with checking whether fw_sec_busnr is non-zero.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v7 1/3] PCI: Cache bridge firmware secondary bus number

In reply to Mario Limonciello
Quoted 42 lines · Mario Limonciello

Drivers such as amdgpu and radeon need the bus number firmware assigned to a device to match it against firmware tables like VFCT. The PCI core may renumber buses (e.g. with pci=realloc), after which the runtime number no longer reflects what firmware programmed.

Record the firmware secondary bus number on the bridge's struct pci_dev the first time pci_read_bridge_windows() reads the bridge's bus numbers, which happens before any renumbering. Devices behind the bridge can then retrieve the original bus number via their upstream bridge.

Only store the value if it is usable: ignore unconfigured (0) and 0xff secondaries. Use a separate "sampled" flag so that later rescans, which would read the renumbered runtime value, never overwrite the cached number, and a "valid" flag so users can tell whether anything was recorded.

---
  drivers/pci/probe.c | 16 ++++++++++++++--
  include/linux/pci.h |  4 ++++
  2 files changed, 18 insertions(+), 2 deletions(-)
drivers/pci/probe.c +4 -2
index 27008e2ea5afc..f20a4b8f7a23b 100644
@@ -505,14 +505,26 @@ static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res,
  static void pci_read_bridge_windows(struct pci_dev *bridge)
  {
  	u32 buses;
+	u8 secondary;
+	u8 subordinate;
  	u16 io;
  	u32 pmem, tmp;
  	struct resource res;
 
  	pci_read_config_dword(bridge, PCI_PRIMARY_BUS, &buses);
  	res.flags = IORESOURCE_BUS;
-	res.start = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
-	res.end = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);
+	secondary = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
+	subordinate = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);

drivers/pci/probe.c static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res, · line 527

Quoted 10 lines
drivers/pci/probe.c +10 -0
+	res.start = secondary;
+	res.end = subordinate;
+
+	if (!bridge->fw_sec_bus_sampled) {
+		bridge->fw_sec_bus_sampled = true;
+		if (secondary > 0 && secondary < 0xff) {
+			bridge->fw_sec_busnr = secondary;
+			bridge->fw_sec_bus_valid = true;
+		}
+	}

I believe the original suggestion from Bjorn [1] to do this in pci_read_bridge_windows is to avoid these extra checks as it's one-time, and also to make sure it is done only for bridge devices.

[1] https://patchwork.freedesktop.org/patch/763534/

Thanks,
Lijo

Quoted 26 lines
drivers/pci/probe.c +0 -0
  	pci_info(bridge, "PCI bridge to %pR%s\n", &res,
  		 bridge->transparent ? " (subtractive decode)" : "");
 
include/linux/pci.h +4 -0
index 5ffcf5ce8c966..045157488b8e7 100644
@@ -363,6 +363,8 @@ struct pci_dev {
  	struct pci_slot	*slot;		/* Physical slot this device is in */
 
  	unsigned int	devfn;		/* Encoded device & function index */
+	/* Firmware secondary bus number saved across bridge scan passes */
+	unsigned char	fw_sec_busnr;
  	unsigned short	vendor;
  	unsigned short	device;
  	unsigned short	subsystem_vendor;
@@ -414,6 +416,8 @@ struct pci_dev {
  	unsigned int	pme_poll:1;	/* Poll device's PME status bit */
  	unsigned int	pinned:1;	/* Whether this dev is pinned */
  	unsigned int	config_rrs_sv:1; /* Config RRS software visibility */
+	unsigned int	fw_sec_bus_valid:1; /* Firmware secondary bus number usable */
+	unsigned int	fw_sec_bus_sampled:1; /* Firmware secondary bus number captured */
  	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 */
#4 lore raw

Re: [PATCH v7 1/3] PCI: Cache bridge firmware secondary bus number

In reply to Mario Limonciello

[+cc Lijo, Reza]

Quoted 38 lines · Mario Limonciello

Drivers such as amdgpu and radeon need the bus number firmware assigned to a device to match it against firmware tables like VFCT. The PCI core may renumber buses (e.g. with pci=realloc), after which the runtime number no longer reflects what firmware programmed.

Record the firmware secondary bus number on the bridge's struct pci_dev the first time pci_read_bridge_windows() reads the bridge's bus numbers, which happens before any renumbering. Devices behind the bridge can then retrieve the original bus number via their upstream bridge.

Only store the value if it is usable: ignore unconfigured (0) and 0xff secondaries. Use a separate "sampled" flag so that later rescans, which would read the renumbered runtime value, never overwrite the cached number, and a "valid" flag so users can tell whether anything was recorded.

---
 drivers/pci/probe.c | 16 ++++++++++++++--
 include/linux/pci.h |  4 ++++
 2 files changed, 18 insertions(+), 2 deletions(-)
drivers/pci/probe.c +2 -0
index 27008e2ea5afc..f20a4b8f7a23b 100644
@@ -505,14 +505,26 @@ static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res,
 static void pci_read_bridge_windows(struct pci_dev *bridge)
 {
 	u32 buses;
+	u8 secondary;
+	u8 subordinate;
 	u16 io;
 	u32 pmem, tmp;
 	struct resource res;
 
 	pci_read_config_dword(bridge, PCI_PRIMARY_BUS, &buses);
 	res.flags = IORESOURCE_BUS;

drivers/pci/probe.c static void pci_read_bridge_mmio_pref(struct pci_dev *dev, struct resource *res, · line 523

Quoted 10 lines
drivers/pci/probe.c +8 -2
-	res.start = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
-	res.end = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);
+	secondary = FIELD_GET(PCI_SECONDARY_BUS_MASK, buses);
+	subordinate = FIELD_GET(PCI_SUBORDINATE_BUS_MASK, buses);
+	res.start = secondary;
+	res.end = subordinate;
+
+	if (!bridge->fw_sec_bus_sampled) {
+		bridge->fw_sec_bus_sampled = true;
+		if (secondary > 0 && secondary < 0xff) {

A bridge with secondary==subordinate==0xff (with a single bus 0xff below the bridge) is actually valid, isn't it?

I guess we could check for a config read failure with PCI_POSSIBLE_ERROR(buses), but that would open the can of worms about what we should do with the *rest* of this function if it failed.

I'm not sure we need to check "secondary > 0" either; obviously a bridge with secondary bus 0 is not valid, but if the idea is to match firmware's idea of a bus number, it seems like we need to capture the bridge configuration at FW/OS handoff.

IIUC, the sashiko review [1] suggests that neither fw_sec_bus_sampled nor fw_sec_bus_valid is actually necessary.

[1] https://lore.kernel.org/all/[email protected]

Quoted 33 lines
drivers/pci/probe.c +4 -0
+			bridge->fw_sec_busnr = secondary;
+			bridge->fw_sec_bus_valid = true;
+		}
+	}
 	pci_info(bridge, "PCI bridge to %pR%s\n", &res,
 		 bridge->transparent ? " (subtractive decode)" : "");
 
include/linux/pci.h +4 -0
index 5ffcf5ce8c966..045157488b8e7 100644
@@ -363,6 +363,8 @@ struct pci_dev {
 	struct pci_slot	*slot;		/* Physical slot this device is in */
 
 	unsigned int	devfn;		/* Encoded device & function index */
+	/* Firmware secondary bus number saved across bridge scan passes */
+	unsigned char	fw_sec_busnr;
 	unsigned short	vendor;
 	unsigned short	device;
 	unsigned short	subsystem_vendor;
@@ -414,6 +416,8 @@ struct pci_dev {
 	unsigned int	pme_poll:1;	/* Poll device's PME status bit */
 	unsigned int	pinned:1;	/* Whether this dev is pinned */
 	unsigned int	config_rrs_sv:1; /* Config RRS software visibility */
+	unsigned int	fw_sec_bus_valid:1; /* Firmware secondary bus number usable */
+	unsigned int	fw_sec_bus_sampled:1; /* Firmware secondary bus number captured */
 	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 */
Signature · 2 lines
-- 
2.43.0
#5 lore raw

PATCH v7 2/3 drm/amdgpu: Match VFCT using initial PCI bus number

In reply to Mario Limonciello

VFCT records the PCI bus number from BIOS POST. The current bus number can change after PCI core renumbers the bus, while accepting a mismatch based only on device identity can select the wrong VFCT image when matching devices are present. Compare against the bus number captured from the firmware bus assignment before bridge renumbering.

Fixes 11c141672045f ("drm/amdgpu: Fix VFCT bus number matching with soft filter")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +10 -13
index 08ed1eb695586..0ee4c6462c4d0 100644
@@ -386,16 +386,17 @@ static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
  * VFCT entries contain the PCI bus number as recorded during BIOS POST.
  * On systems where the kernel renumbers PCI buses (e.g. pci=realloc or
  * resource conflicts), the runtime bus number may differ from the POST
- * value.  Match by device identity (vendor + device + function) and use
- * the bus number as a preference: exact bus match is preferred, but when
- * the bus numbers disagree we accept the entry if the device identity
- * matches.
+ * value.  Match using the bus number captured when the device was
+ * discovered, before any bus renumbering.
  *
  * Returns: 0 on match, -ENODEV on no match
  */
 static int amdgpu_acpi_vfct_match(struct amdgpu_device *adev,
 				  VFCT_IMAGE_HEADER *vhdr)
 {
+	struct pci_dev *bridge = adev->pdev->bus->self;
+	unsigned char initial_bus;
+
 	/* Vendor and device IDs must always match */
 	if (vhdr->VendorID != adev->pdev->vendor ||
 	    vhdr->DeviceID != adev->pdev->device)
@@ -405,16 +406,12 @@ static int amdgpu_acpi_vfct_match(struct amdgpu_device *adev,
 	    vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn))
 		return -ENODEV;
 
-	/* Exact bus number match - preferred */
-	if (vhdr->PCIBus == adev->pdev->bus->number)
-		return 0;
+	if (bridge && bridge->fw_sec_bus_valid)
+		initial_bus = bridge->fw_sec_busnr;
+	else
+		initial_bus = adev->pdev->bus->number;
 
-	/* Bus mismatch but device identity matches (PCI renumbering case) */
-	dev_notice(adev->dev,
-		   "VFCT bus number mismatch: table %u != runtime %u, matching by device identity (vendor 0x%04x device 0x%04x)\n",
-		   vhdr->PCIBus, adev->pdev->bus->number,
-		   adev->pdev->vendor, adev->pdev->device);
-	return 0;
+	return vhdr->PCIBus == initial_bus ? 0 : -ENODEV;
 }
 
 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v7 2/3] drm/amdgpu: Match VFCT using initial PCI bus number

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

VFCT records the PCI bus number from BIOS POST. The current bus number can change after PCI core renumbers the bus, while accepting a mismatch based only on device identity can select the wrong VFCT image when matching devices are present. Compare against the bus number captured from the firmware bus assignment before bridge renumbering.

Fixes 11c141672045f ("drm/amdgpu: Fix VFCT bus number matching with soft filter")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH v7 2/3] drm/amdgpu: Match VFCT using initial PCI bus number

In reply to Mario Limonciello

[+cc Lijo]

Quoted 25 lines · Mario Limonciello

VFCT records the PCI bus number from BIOS POST. The current bus number can change after PCI core renumbers the bus, while accepting a mismatch based only on device identity can select the wrong VFCT image when matching devices are present. Compare against the bus number captured from the firmware bus assignment before bridge renumbering.

Fixes 11c141672045f ("drm/amdgpu: Fix VFCT bus number matching with soft filter")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +0 -3
index 08ed1eb695586..0ee4c6462c4d0 100644
@@ -386,16 +386,17 @@ static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
  * VFCT entries contain the PCI bus number as recorded during BIOS POST.
  * On systems where the kernel renumbers PCI buses (e.g. pci=realloc or
  * resource conflicts), the runtime bus number may differ from the POST
- * value.  Match by device identity (vendor + device + function) and use
- * the bus number as a preference: exact bus match is preferred, but when
- * the bus numbers disagree we accept the entry if the device identity

drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev) · line 397

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +3 -1
- * matches.
+ * value.  Match using the bus number captured when the device was
+ * discovered, before any bus renumbering.
  *
  * Returns: 0 on match, -ENODEV on no match
  */
 static int amdgpu_acpi_vfct_match(struct amdgpu_device *adev,
 				  VFCT_IMAGE_HEADER *vhdr)
 {
+	struct pci_dev *bridge = adev->pdev->bus->self;

pci_upstream_bridge() does this and handles the case where pdev is an SR-IOV VF (that case probably never applies here, but we can still avoid that assumption).

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +2 -0
+	unsigned char initial_bus;
+
 	/* Vendor and device IDs must always match */
 	if (vhdr->VendorID != adev->pdev->vendor ||
 	    vhdr->DeviceID != adev->pdev->device)

drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c static int amdgpu_acpi_vfct_match(struct amdgpu_device *adev, · line 412

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +4 -3
@@ -405,16 +406,12 @@ static int amdgpu_acpi_vfct_match(struct amdgpu_device *adev,
 	    vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn))
 		return -ENODEV;
 
-	/* Exact bus number match - preferred */
-	if (vhdr->PCIBus == adev->pdev->bus->number)
-		return 0;
+	if (bridge && bridge->fw_sec_bus_valid)
+		initial_bus = bridge->fw_sec_busnr;
+	else
+		initial_bus = adev->pdev->bus->number;

Why can't we use bridge->fw_sec_busnr unconditionally? The bus number at FW/OS handoff is the only bus number firmware could possibly know about.

It seems like this should just be:

if (vhdr->PCIBus == bridge->fw_sec_busnr &&

      vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&
      vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn))
          return 0;        /* match */

return -ENODEV; /* no match */

It seems like the bus/device/function match should be solid by itself, so I don't know why you need to check Vendor/Device IDs (but I don't object if you do).

Maybe this boolean style is typical in DRM, and it does match the strcmp() style, but it seems weird to me to write

if (!amdgpu_acpi_vfct_match(...))

    /* do the matching case */
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +1 -6
-	/* Bus mismatch but device identity matches (PCI renumbering case) */
-	dev_notice(adev->dev,
-		   "VFCT bus number mismatch: table %u != runtime %u, matching by device identity (vendor 0x%04x device 0x%04x)\n",
-		   vhdr->PCIBus, adev->pdev->bus->number,
-		   adev->pdev->vendor, adev->pdev->device);
-	return 0;
+	return vhdr->PCIBus == initial_bus ? 0 : -ENODEV;
 }
 
 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
 
Signature · 1 line
2.43.0
#8 lore raw

PATCH v7 3/3 drm/radeon: Match VFCT using initial PCI bus number

In reply to Mario Limonciello

VFCT records the PCI bus number from BIOS POST. The current bus number can change after PCI core renumbers the bus, while accepting a mismatch based only on device identity can select the wrong VFCT image when matching devices are present. Compare against the bus number captured from the firmware bus assignment before bridge renumbering.

Fixes 4059e2f02c8a ("drm/radeon: Fix VFCT bus number matching with soft filter")
---
 drivers/gpu/drm/radeon/radeon_bios.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/gpu/drm/radeon/radeon_bios.c +11 -14
index 1da6bf1c17ff3..707566e376d05 100644
@@ -609,16 +609,17 @@ static bool radeon_read_disabled_bios(struct radeon_device *rdev)
  * VFCT entries contain the PCI bus number as recorded during BIOS POST.
  * On systems where the kernel renumbers PCI buses (e.g. pci=realloc or
  * resource conflicts), the runtime bus number may differ from the POST
- * value.  Match by device identity (vendor + device + function) and use
- * the bus number as a preference: exact bus match is preferred, but when
- * the bus numbers disagree we accept the entry if the device identity
- * matches.
+ * value.  Match using the bus number captured when the device was
+ * discovered, before any bus renumbering.
  *
  * Returns: 0 on match, -ENODEV on no match
  */
 static int radeon_acpi_vfct_match(struct radeon_device *rdev,
-				  VFCT_IMAGE_HEADER *vhdr)
+				   VFCT_IMAGE_HEADER *vhdr)
 {
+	struct pci_dev *bridge = rdev->pdev->bus->self;
+	unsigned char initial_bus;
+
 	/* Vendor and device IDs must always match */
 	if (vhdr->VendorID != rdev->pdev->vendor ||
 	    vhdr->DeviceID != rdev->pdev->device)
@@ -628,16 +629,12 @@ static int radeon_acpi_vfct_match(struct radeon_device *rdev,
 	    vhdr->PCIFunction != PCI_FUNC(rdev->pdev->devfn))
 		return -ENODEV;
 
-	/* Exact bus number match - preferred */
-	if (vhdr->PCIBus == rdev->pdev->bus->number)
-		return 0;
+	if (bridge && bridge->fw_sec_bus_valid)
+		initial_bus = bridge->fw_sec_busnr;
+	else
+		initial_bus = rdev->pdev->bus->number;
 
-	/* Bus mismatch but device identity matches (PCI renumbering case) */
-	dev_notice(&rdev->pdev->dev,
-		   "VFCT bus number mismatch: table %u != runtime %u, matching by device identity (vendor 0x%04x device 0x%04x)\n",
-		   vhdr->PCIBus, rdev->pdev->bus->number,
-		   rdev->pdev->vendor, rdev->pdev->device);
-	return 0;
+	return vhdr->PCIBus == initial_bus ? 0 : -ENODEV;
 }
 
 static bool radeon_acpi_vfct_bios(struct radeon_device *rdev)
Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH v7 3/3] drm/radeon: Match VFCT using initial PCI bus number

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

VFCT records the PCI bus number from BIOS POST. The current bus number can change after PCI core renumbers the bus, while accepting a mismatch based only on device identity can select the wrong VFCT image when matching devices are present. Compare against the bus number captured from the firmware bus assignment before bridge renumbering.

Fixes 4059e2f02c8a ("drm/radeon: Fix VFCT bus number matching with soft filter")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3