dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs
@ 2023-11-03 19:07 Mario Limonciello
  2023-11-03 19:07 ` [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable() Mario Limonciello
                   ` (9 more replies)
  0 siblings, 10 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

Downstream drivers are getting the wrong values from
pcie_bandwidth_available() which is causing problems for performance
of eGPUs.

This series overhauls Thunderbolt related device detection and uses
the changes to change the behavior of pcie_bandwidth_available().

NOTE: This series is currently based on top of v6.6 + this change that
      will be merged for 6.7:
Link: https://patchwork.freedesktop.org/patch/564738/

v1->v2:
 * Rename is_thunderbolt
 * Look for _DSD instead of link
 * Drop pci_is_thunderbolt_attached() from all drivers
 * Adjust links
 * Adjust commit messages
 * Add quirk for Tiger Lake

Mario Limonciello (9):
  drm/nouveau: Switch from pci_is_thunderbolt_attached() to
    dev_is_removable()
  drm/radeon: Switch from pci_is_thunderbolt_attached() to
    dev_is_removable()
  PCI: Drop pci_is_thunderbolt_attached()
  PCI: Move the `PCI_CLASS_SERIAL_USB_USB4` definition to common header
  PCI: pciehp: Move check for is_thunderbolt into a quirk
  PCI: Rename is_thunderbolt to is_tunneled
  PCI: ACPI: Detect PCIe root ports that are used for tunneling
  PCI: Exclude PCIe ports used for tunneling in
    pcie_bandwidth_available()
  PCI: Add a quirk to mark 0x8086 : 0x9a23 as supporting PCIe tunneling

 drivers/gpu/drm/nouveau/nouveau_vga.c  |  6 +-
 drivers/gpu/drm/radeon/radeon_device.c |  4 +-
 drivers/gpu/drm/radeon/radeon_kms.c    |  2 +-
 drivers/pci/hotplug/pciehp_hpc.c       |  6 +-
 drivers/pci/pci-acpi.c                 | 16 ++++++
 drivers/pci/pci.c                      | 76 +++++++++++++++++---------
 drivers/pci/probe.c                    |  2 +-
 drivers/pci/quirks.c                   | 31 +++++++++++
 drivers/platform/x86/apple-gmux.c      |  2 +-
 drivers/thunderbolt/nhi.h              |  2 -
 include/linux/pci.h                    | 25 +--------
 include/linux/pci_ids.h                |  1 +
 12 files changed, 109 insertions(+), 64 deletions(-)

-- 
2.34.1


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

* [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable()
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-06 12:25   ` Ilpo Järvinen
  2023-11-03 19:07 ` [PATCH v2 2/9] drm/radeon: " Mario Limonciello
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

pci_is_thunderbolt_attached() only works for Intel TBT devices. Switch to
using dev_is_removable() to be able to detect USB4 devices as well.

Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/gpu/drm/nouveau/nouveau_vga.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nouveau_vga.c b/drivers/gpu/drm/nouveau/nouveau_vga.c
index f8bf0ec26844..14215b7ca187 100644
--- a/drivers/gpu/drm/nouveau/nouveau_vga.c
+++ b/drivers/gpu/drm/nouveau/nouveau_vga.c
@@ -94,8 +94,8 @@ nouveau_vga_init(struct nouveau_drm *drm)
 
 	vga_client_register(pdev, nouveau_vga_set_decode);
 
-	/* don't register Thunderbolt eGPU with vga_switcheroo */
-	if (pci_is_thunderbolt_attached(pdev))
+	/* don't register USB4/Thunderbolt eGPU with vga_switcheroo */
+	if (dev_is_removable(&pdev->dev))
 		return;
 
 	vga_switcheroo_register_client(pdev, &nouveau_switcheroo_ops, runtime);
@@ -118,7 +118,7 @@ nouveau_vga_fini(struct nouveau_drm *drm)
 
 	vga_client_unregister(pdev);
 
-	if (pci_is_thunderbolt_attached(pdev))
+	if (dev_is_removable(&pdev->dev))
 		return;
 
 	vga_switcheroo_unregister_client(pdev);
-- 
2.34.1


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

* [PATCH v2 2/9] drm/radeon: Switch from pci_is_thunderbolt_attached() to dev_is_removable()
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
  2023-11-03 19:07 ` [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable() Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-06 12:27   ` Ilpo Järvinen
  2023-11-03 19:07 ` [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached() Mario Limonciello
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

pci_is_thunderbolt_attached() only works for Intel TBT devices. Switch to
using dev_is_removable() to be able to detect USB4 devices as well.

Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/gpu/drm/radeon/radeon_device.c | 4 ++--
 drivers/gpu/drm/radeon/radeon_kms.c    | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/radeon/radeon_device.c b/drivers/gpu/drm/radeon/radeon_device.c
index afbb3a80c0c6..ba0ca0694d18 100644
--- a/drivers/gpu/drm/radeon/radeon_device.c
+++ b/drivers/gpu/drm/radeon/radeon_device.c
@@ -1429,7 +1429,7 @@ int radeon_device_init(struct radeon_device *rdev,
 
 	if (rdev->flags & RADEON_IS_PX)
 		runtime = true;
-	if (!pci_is_thunderbolt_attached(rdev->pdev))
+	if (!dev_is_removable(&rdev->pdev->dev))
 		vga_switcheroo_register_client(rdev->pdev,
 					       &radeon_switcheroo_ops, runtime);
 	if (runtime)
@@ -1519,7 +1519,7 @@ void radeon_device_fini(struct radeon_device *rdev)
 	radeon_bo_evict_vram(rdev);
 	radeon_audio_component_fini(rdev);
 	radeon_fini(rdev);
-	if (!pci_is_thunderbolt_attached(rdev->pdev))
+	if (!dev_is_removable(&rdev->pdev->dev))
 		vga_switcheroo_unregister_client(rdev->pdev);
 	if (rdev->flags & RADEON_IS_PX)
 		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
diff --git a/drivers/gpu/drm/radeon/radeon_kms.c b/drivers/gpu/drm/radeon/radeon_kms.c
index a16590c6247f..ead912a58ab8 100644
--- a/drivers/gpu/drm/radeon/radeon_kms.c
+++ b/drivers/gpu/drm/radeon/radeon_kms.c
@@ -138,7 +138,7 @@ int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
 	if ((radeon_runtime_pm != 0) &&
 	    radeon_has_atpx() &&
 	    ((flags & RADEON_IS_IGP) == 0) &&
-	    !pci_is_thunderbolt_attached(pdev))
+	    !dev_is_removable(&pdev->dev))
 		flags |= RADEON_IS_PX;
 
 	/* radeon_device_init should report only fatal error
-- 
2.34.1


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

* [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached()
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
  2023-11-03 19:07 ` [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable() Mario Limonciello
  2023-11-03 19:07 ` [PATCH v2 2/9] drm/radeon: " Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-04  0:37   ` kernel test robot
  2023-11-06 12:33   ` Ilpo Järvinen
  2023-11-03 19:07 ` [PATCH v2 4/9] PCI: Move the `PCI_CLASS_SERIAL_USB_USB4` definition to common header Mario Limonciello
                   ` (6 subsequent siblings)
  9 siblings, 2 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

All callers have switched to dev_is_removable() for detecting
hotpluggable PCIe devices.

Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 include/linux/pci.h | 22 ----------------------
 1 file changed, 22 deletions(-)

diff --git a/include/linux/pci.h b/include/linux/pci.h
index b56417276042..530b0a360514 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -2616,28 +2616,6 @@ static inline bool pci_ari_enabled(struct pci_bus *bus)
 	return bus->self && bus->self->ari_enabled;
 }
 
-/**
- * pci_is_thunderbolt_attached - whether device is on a Thunderbolt daisy chain
- * @pdev: PCI device to check
- *
- * Walk upwards from @pdev and check for each encountered bridge if it's part
- * of a Thunderbolt controller.  Reaching the host bridge means @pdev is not
- * Thunderbolt-attached.  (But rather soldered to the mainboard usually.)
- */
-static inline bool pci_is_thunderbolt_attached(struct pci_dev *pdev)
-{
-	struct pci_dev *parent = pdev;
-
-	if (pdev->is_thunderbolt)
-		return true;
-
-	while ((parent = pci_upstream_bridge(parent)))
-		if (parent->is_thunderbolt)
-			return true;
-
-	return false;
-}
-
 #if defined(CONFIG_PCIEPORTBUS) || defined(CONFIG_EEH)
 void pci_uevent_ers(struct pci_dev *pdev, enum  pci_ers_result err_type);
 #endif
-- 
2.34.1


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

* [PATCH v2 4/9] PCI: Move the `PCI_CLASS_SERIAL_USB_USB4` definition to common header
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
                   ` (2 preceding siblings ...)
  2023-11-03 19:07 ` [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached() Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-03 19:07 ` [PATCH v2 5/9] PCI: pciehp: Move check for is_thunderbolt into a quirk Mario Limonciello
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

`PCI_CLASS_SERIAL_USB_USB4` may be used by code outside of thunderbolt.
Move the declaration into the common pci_ids.h header.

Acked-by: Mika Westerberberg <mika.westerberg@linux.intel.com>
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/thunderbolt/nhi.h | 2 --
 include/linux/pci_ids.h   | 1 +
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 0f029ce75882..675ddefe283c 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -91,6 +91,4 @@ extern const struct tb_nhi_ops icl_nhi_ops;
 #define PCI_DEVICE_ID_INTEL_RPL_NHI0			0xa73e
 #define PCI_DEVICE_ID_INTEL_RPL_NHI1			0xa76d
 
-#define PCI_CLASS_SERIAL_USB_USB4			0x0c0340
-
 #endif
diff --git a/include/linux/pci_ids.h b/include/linux/pci_ids.h
index 91b457de262e..1fc8b5e72f80 100644
--- a/include/linux/pci_ids.h
+++ b/include/linux/pci_ids.h
@@ -121,6 +121,7 @@
 #define PCI_CLASS_SERIAL_USB_OHCI	0x0c0310
 #define PCI_CLASS_SERIAL_USB_EHCI	0x0c0320
 #define PCI_CLASS_SERIAL_USB_XHCI	0x0c0330
+#define PCI_CLASS_SERIAL_USB_USB4	0x0c0340
 #define PCI_CLASS_SERIAL_USB_DEVICE	0x0c03fe
 #define PCI_CLASS_SERIAL_FIBER		0x0c04
 #define PCI_CLASS_SERIAL_SMBUS		0x0c05
-- 
2.34.1


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

* [PATCH v2 5/9] PCI: pciehp: Move check for is_thunderbolt into a quirk
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
                   ` (3 preceding siblings ...)
  2023-11-03 19:07 ` [PATCH v2 4/9] PCI: Move the `PCI_CLASS_SERIAL_USB_USB4` definition to common header Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-06 12:41   ` Ilpo Järvinen
  2023-11-03 19:07 ` [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled Mario Limonciello
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

commit 493fb50e958c ("PCI: pciehp: Assume NoCompl+ for Thunderbolt
ports") added a check into pciehp code to explicitly set NoCompl+
for all Intel Thunderbolt controllers, including those that don't
need it.

This overloaded the purpose of the `is_thunderbolt` member of
`struct pci_device` because that means that any controller that
identifies as thunderbolt would set NoCompl+ even if it doesn't
suffer this deficiency. As that commit helpfully specifies all the
controllers with the problem, move them into a PCI quirk.

Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/pci/hotplug/pciehp_hpc.c |  6 +-----
 drivers/pci/quirks.c             | 20 ++++++++++++++++++++
 include/linux/pci.h              |  1 +
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
index fd713abdfb9f..23a92d681d1c 100644
--- a/drivers/pci/hotplug/pciehp_hpc.c
+++ b/drivers/pci/hotplug/pciehp_hpc.c
@@ -991,11 +991,7 @@ struct controller *pcie_init(struct pcie_device *dev)
 	if (pdev->hotplug_user_indicators)
 		slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
 
-	/*
-	 * We assume no Thunderbolt controllers support Command Complete events,
-	 * but some controllers falsely claim they do.
-	 */
-	if (pdev->is_thunderbolt)
+	if (pdev->no_command_complete)
 		slot_cap |= PCI_EXP_SLTCAP_NCCS;
 
 	ctrl->slot_cap = slot_cap;
diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index eeec1d6f9023..4bbf6e33ca11 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -3807,6 +3807,26 @@ DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PORT_RIDGE,
 			quirk_thunderbolt_hotplug_msi);
 
+/*
+ * We assume no Thunderbolt controllers support Command Complete events,
+ * but some controllers falsely claim they do.
+ */
+static void quirk_thunderbolt_command_complete(struct pci_dev *pdev)
+{
+	pdev->no_command_complete = 1;
+}
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
+			quirk_thunderbolt_command_complete);
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EAGLE_RIDGE,
+			quirk_thunderbolt_command_complete);
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LIGHT_PEAK,
+			quirk_thunderbolt_command_complete);
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
+			quirk_thunderbolt_command_complete);
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C,
+			quirk_thunderbolt_command_complete);
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PORT_RIDGE,
+			quirk_thunderbolt_command_complete);
 #ifdef CONFIG_ACPI
 /*
  * Apple: Shutdown Cactus Ridge Thunderbolt controller.
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 530b0a360514..439c2dac8a3e 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -441,6 +441,7 @@ struct pci_dev {
 	unsigned int	is_hotplug_bridge:1;
 	unsigned int	shpc_managed:1;		/* SHPC owned by shpchp */
 	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */
+	unsigned int	no_command_complete:1;	/* No command completion */
 	/*
 	 * Devices marked being untrusted are the ones that can potentially
 	 * execute DMA attacks and similar. They are typically connected
-- 
2.34.1


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

* [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
                   ` (4 preceding siblings ...)
  2023-11-03 19:07 ` [PATCH v2 5/9] PCI: pciehp: Move check for is_thunderbolt into a quirk Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-03 19:38   ` Hans de Goede
  2023-11-05 17:39   ` Lukas Wunner
  2023-11-03 19:07 ` [PATCH v2 7/9] PCI: ACPI: Detect PCIe root ports that are used for tunneling Mario Limonciello
                   ` (3 subsequent siblings)
  9 siblings, 2 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

The `is_thunderbolt` bit has been used to indicate that a PCIe device
contained the Intel VSEC which is used by various parts of the kernel
to change behavior. To later allow usage with USB4 controllers as well,
rename this to `is_tunneled`.

Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/pci/pci.c                 | 2 +-
 drivers/pci/probe.c               | 2 +-
 drivers/platform/x86/apple-gmux.c | 2 +-
 include/linux/pci.h               | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 59c01d68c6d5..d9aa5a39f585 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -3032,7 +3032,7 @@ bool pci_bridge_d3_possible(struct pci_dev *bridge)
 			return true;
 
 		/* Even the oldest 2010 Thunderbolt controller supports D3. */
-		if (bridge->is_thunderbolt)
+		if (bridge->is_tunneled)
 			return true;
 
 		/* Platform might know better if the bridge supports D3 */
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 795534589b98..518413d15402 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1597,7 +1597,7 @@ static void set_pcie_thunderbolt(struct pci_dev *dev)
 	/* Is the device part of a Thunderbolt controller? */
 	vsec = pci_find_vsec_capability(dev, PCI_VENDOR_ID_INTEL, PCI_VSEC_ID_INTEL_TBT);
 	if (vsec)
-		dev->is_thunderbolt = 1;
+		dev->is_tunneled = 1;
 }
 
 static void set_pcie_untrusted(struct pci_dev *dev)
diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c
index 1417e230edbd..20315aa4463a 100644
--- a/drivers/platform/x86/apple-gmux.c
+++ b/drivers/platform/x86/apple-gmux.c
@@ -774,7 +774,7 @@ static int gmux_resume(struct device *dev)
 
 static int is_thunderbolt(struct device *dev, void *data)
 {
-	return to_pci_dev(dev)->is_thunderbolt;
+	return to_pci_dev(dev)->is_tunneled;
 }
 
 static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 439c2dac8a3e..b1724f25fb02 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -440,7 +440,7 @@ struct pci_dev {
 	unsigned int	is_virtfn:1;
 	unsigned int	is_hotplug_bridge:1;
 	unsigned int	shpc_managed:1;		/* SHPC owned by shpchp */
-	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */
+	unsigned int	is_tunneled:1;		/* Tunneled TBT or USB4 link */
 	unsigned int	no_command_complete:1;	/* No command completion */
 	/*
 	 * Devices marked being untrusted are the ones that can potentially
-- 
2.34.1


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

* [PATCH v2 7/9] PCI: ACPI: Detect PCIe root ports that are used for tunneling
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
                   ` (5 preceding siblings ...)
  2023-11-03 19:07 ` [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-03 19:07 ` [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available() Mario Limonciello
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

USB4 routers support a feature called "PCIe tunneling". This
allows PCIe traffic to be transmitted over USB4 fabric.

PCIe root ports that are used in this fashion can be discovered
by device specific data that specifies the USB4 router they are
connected to. For the PCI core, the specific connection information
doesn't matter, but it's interesting to know that this root port is
used for tunneling traffic. This will allow other decisions to be
made based upon it.

Detect the `usb4-host-interface` _DSD and if it's found save it
into the `is_tunneling` bit in `struct pci_device`.

Link: https://www.usb.org/document-library/usb4r-specification-v20
      USB4 V2 with Errata and ECN through June 2023
      Section 2.2.10.3
Link: https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/usb4-acpi-requirements#port-mapping-_dsd-for-usb-3x-and-pcie
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/pci/pci-acpi.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c
index 05b7357bd258..81dbfd335f34 100644
--- a/drivers/pci/pci-acpi.c
+++ b/drivers/pci/pci-acpi.c
@@ -1414,12 +1414,28 @@ static void pci_acpi_set_external_facing(struct pci_dev *dev)
 		dev->external_facing = 1;
 }
 
+static void pci_acpi_set_tunneling(struct pci_dev *dev)
+{
+	if (!pci_is_pcie(dev))
+		return;
+
+	switch (pci_pcie_type(dev)) {
+	case PCI_EXP_TYPE_ROOT_PORT:
+	case PCI_EXP_TYPE_DOWNSTREAM:
+		dev->is_tunneled = device_property_present(&dev->dev, "usb4-host-interface");
+		break;
+	default:
+		return;
+	}
+}
+
 void pci_acpi_setup(struct device *dev, struct acpi_device *adev)
 {
 	struct pci_dev *pci_dev = to_pci_dev(dev);
 
 	pci_acpi_optimize_delay(pci_dev, adev->handle);
 	pci_acpi_set_external_facing(pci_dev);
+	pci_acpi_set_tunneling(pci_dev);
 	pci_acpi_add_edr_notifier(pci_dev);
 
 	pci_acpi_add_pm_notifier(adev, pci_dev);
-- 
2.34.1


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

* [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
                   ` (6 preceding siblings ...)
  2023-11-03 19:07 ` [PATCH v2 7/9] PCI: ACPI: Detect PCIe root ports that are used for tunneling Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-04  6:57   ` Lazar, Lijo
                     ` (2 more replies)
  2023-11-03 19:07 ` [PATCH v2 9/9] PCI: Add a quirk to mark 0x8086 : 0x9a23 as supporting PCIe tunneling Mario Limonciello
  2023-11-03 19:20 ` [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Bjorn Helgaas
  9 siblings, 3 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

The USB4 spec specifies that PCIe ports that are used for tunneling
PCIe traffic over USB4 fabric will be hardcoded to advertise 2.5GT/s and
behave as a PCIe Gen1 device. The actual performance of these ports is
controlled by the fabric implementation.

Downstream drivers such as amdgpu which utilize pcie_bandwidth_available()
to program the device will always find the PCIe ports used for
tunneling as a limiting factor potentially leading to incorrect
performance decisions.

To prevent problems in downstream drivers check explicitly for ports
being used for PCIe tunneling and skip them when looking for bandwidth
limitations of the hierarchy. If the only device connected is a root port
used for tunneling then report that device.

Downstream drivers could make this change on their own but then they
wouldn't be able to detect other potential speed bottlenecks from the
hierarchy without duplicating pcie_bandwidth_available() logic.

Link: https://gitlab.freedesktop.org/drm/amd/-/issues/2925#note_2145860
Link: https://www.usb.org/document-library/usb4r-specification-v20
      USB4 V2 with Errata and ECN through June 2023
      Section 11.2.1
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/pci/pci.c | 74 +++++++++++++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 25 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index d9aa5a39f585..15e37164ce56 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -6223,6 +6223,35 @@ int pcie_set_mps(struct pci_dev *dev, int mps)
 }
 EXPORT_SYMBOL(pcie_set_mps);
 
+static u32 pcie_calc_bw_limits(struct pci_dev *dev, u32 bw,
+			       struct pci_dev **limiting_dev,
+			       enum pci_bus_speed *speed,
+			       enum pcie_link_width *width)
+{
+	enum pcie_link_width next_width;
+	enum pci_bus_speed next_speed;
+	u32 next_bw;
+	u16 lnksta;
+
+	pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
+	next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
+	next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
+	next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
+
+	/* Check if current device limits the total bandwidth */
+	if (!bw || next_bw <= bw) {
+		bw = next_bw;
+		if (limiting_dev)
+			*limiting_dev = dev;
+		if (speed)
+			*speed = next_speed;
+		if (width)
+			*width = next_width;
+	}
+
+	return bw;
+}
+
 /**
  * pcie_bandwidth_available - determine minimum link settings of a PCIe
  *			      device and its bandwidth limitation
@@ -6236,47 +6265,42 @@ EXPORT_SYMBOL(pcie_set_mps);
  * limiting_dev, speed, and width pointers are supplied) information about
  * that point.  The bandwidth returned is in Mb/s, i.e., megabits/second of
  * raw bandwidth.
+ *
+ * This excludes the bandwidth calculation that has been returned from a
+ * PCIe device used for transmitting tunneled PCIe traffic over a Thunderbolt
+ * or USB4 link that is part of larger hierarchy. The calculation is excluded
+ * because the USB4 specification specifies that the max speed returned from
+ * PCIe configuration registers for the tunneling link is always PCI 1x 2.5 GT/s.
+ * When only tunneled devices are present, the bandwidth returned is the
+ * bandwidth available from the first tunneled device.
  */
 u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
 			     enum pci_bus_speed *speed,
 			     enum pcie_link_width *width)
 {
-	u16 lnksta;
-	enum pci_bus_speed next_speed;
-	enum pcie_link_width next_width;
-	u32 bw, next_bw;
+	struct pci_dev *tdev = NULL;
+	u32 bw = 0;
 
 	if (speed)
 		*speed = PCI_SPEED_UNKNOWN;
 	if (width)
 		*width = PCIE_LNK_WIDTH_UNKNOWN;
 
-	bw = 0;
-
 	while (dev) {
-		pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
-
-		next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
-		next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
-			PCI_EXP_LNKSTA_NLW_SHIFT;
-
-		next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
-
-		/* Check if current device limits the total bandwidth */
-		if (!bw || next_bw <= bw) {
-			bw = next_bw;
-
-			if (limiting_dev)
-				*limiting_dev = dev;
-			if (speed)
-				*speed = next_speed;
-			if (width)
-				*width = next_width;
+		if (dev->is_tunneled) {
+			if (!tdev)
+				tdev = dev;
+			goto skip;
 		}
-
+		bw = pcie_calc_bw_limits(dev, bw, limiting_dev, speed, width);
+skip:
 		dev = pci_upstream_bridge(dev);
 	}
 
+	/* If nothing "faster" found on link, limit to first tunneled device */
+	if (tdev && !bw)
+		bw = pcie_calc_bw_limits(tdev, bw, limiting_dev, speed, width);
+
 	return bw;
 }
 EXPORT_SYMBOL(pcie_bandwidth_available);
-- 
2.34.1


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

* [PATCH v2 9/9] PCI: Add a quirk to mark 0x8086 : 0x9a23 as supporting PCIe tunneling
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
                   ` (7 preceding siblings ...)
  2023-11-03 19:07 ` [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available() Mario Limonciello
@ 2023-11-03 19:07 ` Mario Limonciello
  2023-11-03 19:20 ` [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Bjorn Helgaas
  9 siblings, 0 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-03 19:07 UTC (permalink / raw)
  To: Karol Herbst, Lyude Paul, Alex Deucher, Christian König,
	Bjorn Helgaas, Hans de Goede, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Xinhui Pan,
	open list, Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

The PCI root port used for tunneling USB4 traffic on Tiger Lake is
is not marked as tunneling but has the same limitations as other
PCIe root ports used for tunneling.

This causes pcie_bandwidth_available() to treat it as the limiting
device in the PCI hierarchy and downstream driver to program devices
incorrectly as a result.

Add a quirk to mark the device as tunneling so that it will be skipped
in pcie_bandwidth_available() like other TBT3/USB4 root ports and bridges.

Link: https://gitlab.freedesktop.org/drm/amd/-/issues/2885
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/pci/quirks.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index 4bbf6e33ca11..0f124e075834 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -3827,6 +3827,17 @@ DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C
 			quirk_thunderbolt_command_complete);
 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PORT_RIDGE,
 			quirk_thunderbolt_command_complete);
+
+/*
+ * PCIe root port associated with the integrated controller is used for PCIe
+ * tunneling but can't be detected using ACPI.
+ */
+static void quirk_thunderbolt_tunneling(struct pci_dev *pdev)
+{
+	pdev->is_tunneled = 1;
+}
+DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x9a23,
+			quirk_thunderbolt_tunneling);
 #ifdef CONFIG_ACPI
 /*
  * Apple: Shutdown Cactus Ridge Thunderbolt controller.
-- 
2.34.1


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

* Re: [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs
  2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
                   ` (8 preceding siblings ...)
  2023-11-03 19:07 ` [PATCH v2 9/9] PCI: Add a quirk to mark 0x8086 : 0x9a23 as supporting PCIe tunneling Mario Limonciello
@ 2023-11-03 19:20 ` Bjorn Helgaas
  9 siblings, 0 replies; 32+ messages in thread
From: Bjorn Helgaas @ 2023-11-03 19:20 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: Xinhui Pan, Karol Herbst, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Marek Behún,
	Yehezkel Bernat, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Ilpo Järvinen, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, Hans de Goede, Bjorn Helgaas, Mika Westerberg,
	open list:THUNDERBOLT DRIVER, open list, Alex Deucher,
	Pali Rohár, Christian König, Maciej W . Rozycki

On Fri, Nov 03, 2023 at 02:07:49PM -0500, Mario Limonciello wrote:
> Downstream drivers are getting the wrong values from
> pcie_bandwidth_available() which is causing problems for performance
> of eGPUs.
> 
> This series overhauls Thunderbolt related device detection and uses
> the changes to change the behavior of pcie_bandwidth_available().
> 
> NOTE: This series is currently based on top of v6.6 + this change that
>       will be merged for 6.7:
> Link: https://patchwork.freedesktop.org/patch/564738/

Thanks, Mario, I'll look at this soon after v6.7-rc1 (probably Nov
12), so the amdgpu patch should be in mainline by then.

> v1->v2:
>  * Rename is_thunderbolt
>  * Look for _DSD instead of link
>  * Drop pci_is_thunderbolt_attached() from all drivers
>  * Adjust links
>  * Adjust commit messages
>  * Add quirk for Tiger Lake
> 
> Mario Limonciello (9):
>   drm/nouveau: Switch from pci_is_thunderbolt_attached() to
>     dev_is_removable()
>   drm/radeon: Switch from pci_is_thunderbolt_attached() to
>     dev_is_removable()
>   PCI: Drop pci_is_thunderbolt_attached()
>   PCI: Move the `PCI_CLASS_SERIAL_USB_USB4` definition to common header
>   PCI: pciehp: Move check for is_thunderbolt into a quirk
>   PCI: Rename is_thunderbolt to is_tunneled
>   PCI: ACPI: Detect PCIe root ports that are used for tunneling
>   PCI: Exclude PCIe ports used for tunneling in
>     pcie_bandwidth_available()
>   PCI: Add a quirk to mark 0x8086 : 0x9a23 as supporting PCIe tunneling
> 
>  drivers/gpu/drm/nouveau/nouveau_vga.c  |  6 +-
>  drivers/gpu/drm/radeon/radeon_device.c |  4 +-
>  drivers/gpu/drm/radeon/radeon_kms.c    |  2 +-
>  drivers/pci/hotplug/pciehp_hpc.c       |  6 +-
>  drivers/pci/pci-acpi.c                 | 16 ++++++
>  drivers/pci/pci.c                      | 76 +++++++++++++++++---------
>  drivers/pci/probe.c                    |  2 +-
>  drivers/pci/quirks.c                   | 31 +++++++++++
>  drivers/platform/x86/apple-gmux.c      |  2 +-
>  drivers/thunderbolt/nhi.h              |  2 -
>  include/linux/pci.h                    | 25 +--------
>  include/linux/pci_ids.h                |  1 +
>  12 files changed, 109 insertions(+), 64 deletions(-)
> 
> -- 
> 2.34.1
> 

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

* Re: [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled
  2023-11-03 19:07 ` [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled Mario Limonciello
@ 2023-11-03 19:38   ` Hans de Goede
  2023-11-05 17:39   ` Lukas Wunner
  1 sibling, 0 replies; 32+ messages in thread
From: Hans de Goede @ 2023-11-03 19:38 UTC (permalink / raw)
  To: Mario Limonciello, Karol Herbst, Lyude Paul, Alex Deucher,
	Christian König, Bjorn Helgaas, Ilpo Järvinen,
	Mika Westerberg, Lukas Wunner
  Cc: Marek Behún, open list:THUNDERBOLT DRIVER, open list:ACPI,
	Rafael J . Wysocki, Michael Jamet, Pali Rohár, Xinhui Pan,
	Manivannan Sadhasivam, open list,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mark Gross, Yehezkel Bernat,
	open list:PCI SUBSYSTEM, Danilo Krummrich,
	open list:RADEON and AMDGPU DRM DRIVERS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Andreas Noever, Maciej W . Rozycki

Hi,

On 11/3/23 20:07, Mario Limonciello wrote:
> The `is_thunderbolt` bit has been used to indicate that a PCIe device
> contained the Intel VSEC which is used by various parts of the kernel
> to change behavior. To later allow usage with USB4 controllers as well,
> rename this to `is_tunneled`.
> 
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>

Here is my ack for the trivial drivers/platform/x86/apple-gmux.c change:

Acked-by: Hans de Goede <hdegoede@redhat.com>

Bjorn, feel free to route this through the PCI tree.

Regards,

Hans




> ---
>  drivers/pci/pci.c                 | 2 +-
>  drivers/pci/probe.c               | 2 +-
>  drivers/platform/x86/apple-gmux.c | 2 +-
>  include/linux/pci.h               | 2 +-
>  4 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 59c01d68c6d5..d9aa5a39f585 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -3032,7 +3032,7 @@ bool pci_bridge_d3_possible(struct pci_dev *bridge)
>  			return true;
>  
>  		/* Even the oldest 2010 Thunderbolt controller supports D3. */
> -		if (bridge->is_thunderbolt)
> +		if (bridge->is_tunneled)
>  			return true;
>  
>  		/* Platform might know better if the bridge supports D3 */
> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
> index 795534589b98..518413d15402 100644
> --- a/drivers/pci/probe.c
> +++ b/drivers/pci/probe.c
> @@ -1597,7 +1597,7 @@ static void set_pcie_thunderbolt(struct pci_dev *dev)
>  	/* Is the device part of a Thunderbolt controller? */
>  	vsec = pci_find_vsec_capability(dev, PCI_VENDOR_ID_INTEL, PCI_VSEC_ID_INTEL_TBT);
>  	if (vsec)
> -		dev->is_thunderbolt = 1;
> +		dev->is_tunneled = 1;
>  }
>  
>  static void set_pcie_untrusted(struct pci_dev *dev)
> diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c
> index 1417e230edbd..20315aa4463a 100644
> --- a/drivers/platform/x86/apple-gmux.c
> +++ b/drivers/platform/x86/apple-gmux.c
> @@ -774,7 +774,7 @@ static int gmux_resume(struct device *dev)
>  
>  static int is_thunderbolt(struct device *dev, void *data)
>  {
> -	return to_pci_dev(dev)->is_thunderbolt;
> +	return to_pci_dev(dev)->is_tunneled;
>  }
>  
>  static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
> diff --git a/include/linux/pci.h b/include/linux/pci.h
> index 439c2dac8a3e..b1724f25fb02 100644
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -440,7 +440,7 @@ struct pci_dev {
>  	unsigned int	is_virtfn:1;
>  	unsigned int	is_hotplug_bridge:1;
>  	unsigned int	shpc_managed:1;		/* SHPC owned by shpchp */
> -	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */
> +	unsigned int	is_tunneled:1;		/* Tunneled TBT or USB4 link */
>  	unsigned int	no_command_complete:1;	/* No command completion */
>  	/*
>  	 * Devices marked being untrusted are the ones that can potentially


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

* Re: [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached()
  2023-11-03 19:07 ` [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached() Mario Limonciello
@ 2023-11-04  0:37   ` kernel test robot
  2023-11-06 12:33   ` Ilpo Järvinen
  1 sibling, 0 replies; 32+ messages in thread
From: kernel test robot @ 2023-11-04  0:37 UTC (permalink / raw)
  To: Mario Limonciello, Karol Herbst, Lyude Paul, Alex Deucher,
	Christian König, Bjorn Helgaas, Hans de Goede,
	Ilpo Järvinen, Mika Westerberg, Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Rafael J . Wysocki,
	open list:PCI SUBSYSTEM,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, oe-kbuild-all, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Maciej W . Rozycki

Hi Mario,

kernel test robot noticed the following build errors:

[auto build test ERROR on pci/for-linus]
[also build test ERROR on drm-misc/drm-misc-next westeri-thunderbolt/next rafael-pm/linux-next rafael-pm/acpi-bus linus/master rafael-pm/devprop v6.6 next-20231103]
[cannot apply to pci/next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mario-Limonciello/drm-nouveau-Switch-from-pci_is_thunderbolt_attached-to-dev_is_removable/20231104-030945
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git for-linus
patch link:    https://lore.kernel.org/r/20231103190758.82911-4-mario.limonciello%40amd.com
patch subject: [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached()
config: loongarch-randconfig-002-20231104 (https://download.01.org/0day-ci/archive/20231104/202311040800.zpVIwNrB-lkp@intel.com/config)
compiler: loongarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231104/202311040800.zpVIwNrB-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202311040800.zpVIwNrB-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/gpu/drm/amd/amdgpu/nbio_v2_3.c: In function 'nbio_v2_3_enable_aspm':
>> drivers/gpu/drm/amd/amdgpu/nbio_v2_3.c:364:21: error: implicit declaration of function 'pci_is_thunderbolt_attached' [-Werror=implicit-function-declaration]
     364 |                 if (pci_is_thunderbolt_attached(adev->pdev))
         |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
   cc1: some warnings being treated as errors
--
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c: In function 'amdgpu_device_ip_early_init':
>> drivers/gpu/drm/amd/amdgpu/amdgpu_device.c:2118:14: error: implicit declaration of function 'pci_is_thunderbolt_attached' [-Werror=implicit-function-declaration]
    2118 |             !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
         |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~
   cc1: some warnings being treated as errors


vim +/pci_is_thunderbolt_attached +364 drivers/gpu/drm/amd/amdgpu/nbio_v2_3.c

f1213b15976881d Evan Quan 2020-08-18  350  
f1213b15976881d Evan Quan 2020-08-18  351  static void nbio_v2_3_enable_aspm(struct amdgpu_device *adev,
f1213b15976881d Evan Quan 2020-08-18  352  				  bool enable)
f1213b15976881d Evan Quan 2020-08-18  353  {
f1213b15976881d Evan Quan 2020-08-18  354  	uint32_t def, data;
f1213b15976881d Evan Quan 2020-08-18  355  
f1213b15976881d Evan Quan 2020-08-18  356  	def = data = RREG32_PCIE(smnPCIE_LC_CNTL);
f1213b15976881d Evan Quan 2020-08-18  357  
f1213b15976881d Evan Quan 2020-08-18  358  	if (enable) {
f1213b15976881d Evan Quan 2020-08-18  359  		/* Disable ASPM L0s/L1 first */
f1213b15976881d Evan Quan 2020-08-18  360  		data &= ~(PCIE_LC_CNTL__LC_L0S_INACTIVITY_MASK | PCIE_LC_CNTL__LC_L1_INACTIVITY_MASK);
f1213b15976881d Evan Quan 2020-08-18  361  
f1213b15976881d Evan Quan 2020-08-18  362  		data |= NAVI10_PCIE__LC_L0S_INACTIVITY_DEFAULT << PCIE_LC_CNTL__LC_L0S_INACTIVITY__SHIFT;
f1213b15976881d Evan Quan 2020-08-18  363  
f1213b15976881d Evan Quan 2020-08-18 @364  		if (pci_is_thunderbolt_attached(adev->pdev))
f1213b15976881d Evan Quan 2020-08-18  365  			data |= NAVI10_PCIE__LC_L1_INACTIVITY_TBT_DEFAULT  << PCIE_LC_CNTL__LC_L1_INACTIVITY__SHIFT;
f1213b15976881d Evan Quan 2020-08-18  366  		else
f1213b15976881d Evan Quan 2020-08-18  367  			data |= NAVI10_PCIE__LC_L1_INACTIVITY_DEFAULT << PCIE_LC_CNTL__LC_L1_INACTIVITY__SHIFT;
f1213b15976881d Evan Quan 2020-08-18  368  
f1213b15976881d Evan Quan 2020-08-18  369  		data &= ~PCIE_LC_CNTL__LC_PMI_TO_L1_DIS_MASK;
f1213b15976881d Evan Quan 2020-08-18  370  	} else {
f1213b15976881d Evan Quan 2020-08-18  371  		/* Disbale ASPM L1 */
f1213b15976881d Evan Quan 2020-08-18  372  		data &= ~PCIE_LC_CNTL__LC_L1_INACTIVITY_MASK;
f1213b15976881d Evan Quan 2020-08-18  373  		/* Disable ASPM TxL0s */
f1213b15976881d Evan Quan 2020-08-18  374  		data &= ~PCIE_LC_CNTL__LC_L0S_INACTIVITY_MASK;
f1213b15976881d Evan Quan 2020-08-18  375  		/* Disable ACPI L1 */
f1213b15976881d Evan Quan 2020-08-18  376  		data |= PCIE_LC_CNTL__LC_PMI_TO_L1_DIS_MASK;
f1213b15976881d Evan Quan 2020-08-18  377  	}
f1213b15976881d Evan Quan 2020-08-18  378  
f1213b15976881d Evan Quan 2020-08-18  379  	if (def != data)
f1213b15976881d Evan Quan 2020-08-18  380  		WREG32_PCIE(smnPCIE_LC_CNTL, data);
f1213b15976881d Evan Quan 2020-08-18  381  }
f1213b15976881d Evan Quan 2020-08-18  382  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-03 19:07 ` [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available() Mario Limonciello
@ 2023-11-04  6:57   ` Lazar, Lijo
  2023-11-06 12:52   ` Ilpo Järvinen
  2023-11-06 18:10   ` Lukas Wunner
  2 siblings, 0 replies; 32+ messages in thread
From: Lazar, Lijo @ 2023-11-04  6:57 UTC (permalink / raw)
  To: Mario Limonciello, Karol Herbst, Lyude Paul, Alex Deucher,
	Christian König, Bjorn Helgaas, Hans de Goede,
	Ilpo Järvinen, Mika Westerberg, Lukas Wunner
  Cc: Marek Behún, Xinhui Pan, Rafael J . Wysocki, Michael Jamet,
	open list:PCI SUBSYSTEM, Yehezkel Bernat,
	open list:THUNDERBOLT DRIVER, open list,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mark Gross, open list:ACPI,
	Danilo Krummrich, open list:RADEON and AMDGPU DRM DRIVERS,
	Manivannan Sadhasivam,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	Andreas Noever, Pali Rohár, Maciej W . Rozycki



On 11/4/2023 12:37 AM, Mario Limonciello wrote:
> The USB4 spec specifies that PCIe ports that are used for tunneling
> PCIe traffic over USB4 fabric will be hardcoded to advertise 2.5GT/s and
> behave as a PCIe Gen1 device. The actual performance of these ports is
> controlled by the fabric implementation.

The code below ties a generic term 'tunneling' to USB4 spec. I think it 
should be something like if (is_USB4 && is_tunneled), exclude from 
bandwidth calculations - it should specifically identify usb4 based 
tunneling rather than applying to all 'tunneled' cases.

Thanks,
Lijo

> 
> Downstream drivers such as amdgpu which utilize pcie_bandwidth_available()
> to program the device will always find the PCIe ports used for
> tunneling as a limiting factor potentially leading to incorrect
> performance decisions.
> 
> To prevent problems in downstream drivers check explicitly for ports
> being used for PCIe tunneling and skip them when looking for bandwidth
> limitations of the hierarchy. If the only device connected is a root port
> used for tunneling then report that device.
> 
> Downstream drivers could make this change on their own but then they
> wouldn't be able to detect other potential speed bottlenecks from the
> hierarchy without duplicating pcie_bandwidth_available() logic.
> 
> Link: https://gitlab.freedesktop.org/drm/amd/-/issues/2925#note_2145860
> Link: https://www.usb.org/document-library/usb4r-specification-v20
>        USB4 V2 with Errata and ECN through June 2023
>        Section 11.2.1
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>   drivers/pci/pci.c | 74 +++++++++++++++++++++++++++++++----------------
>   1 file changed, 49 insertions(+), 25 deletions(-)
> 
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index d9aa5a39f585..15e37164ce56 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -6223,6 +6223,35 @@ int pcie_set_mps(struct pci_dev *dev, int mps)
>   }
>   EXPORT_SYMBOL(pcie_set_mps);
>   
> +static u32 pcie_calc_bw_limits(struct pci_dev *dev, u32 bw,
> +			       struct pci_dev **limiting_dev,
> +			       enum pci_bus_speed *speed,
> +			       enum pcie_link_width *width)
> +{
> +	enum pcie_link_width next_width;
> +	enum pci_bus_speed next_speed;
> +	u32 next_bw;
> +	u16 lnksta;
> +
> +	pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
> +	next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
> +	next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
> +	next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
> +
> +	/* Check if current device limits the total bandwidth */
> +	if (!bw || next_bw <= bw) {
> +		bw = next_bw;
> +		if (limiting_dev)
> +			*limiting_dev = dev;
> +		if (speed)
> +			*speed = next_speed;
> +		if (width)
> +			*width = next_width;
> +	}
> +
> +	return bw;
> +}
> +
>   /**
>    * pcie_bandwidth_available - determine minimum link settings of a PCIe
>    *			      device and its bandwidth limitation
> @@ -6236,47 +6265,42 @@ EXPORT_SYMBOL(pcie_set_mps);
>    * limiting_dev, speed, and width pointers are supplied) information about
>    * that point.  The bandwidth returned is in Mb/s, i.e., megabits/second of
>    * raw bandwidth.
> + *
> + * This excludes the bandwidth calculation that has been returned from a
> + * PCIe device used for transmitting tunneled PCIe traffic over a Thunderbolt
> + * or USB4 link that is part of larger hierarchy. The calculation is excluded
> + * because the USB4 specification specifies that the max speed returned from
> + * PCIe configuration registers for the tunneling link is always PCI 1x 2.5 GT/s.
> + * When only tunneled devices are present, the bandwidth returned is the
> + * bandwidth available from the first tunneled device.
>    */
>   u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
>   			     enum pci_bus_speed *speed,
>   			     enum pcie_link_width *width)
>   {
> -	u16 lnksta;
> -	enum pci_bus_speed next_speed;
> -	enum pcie_link_width next_width;
> -	u32 bw, next_bw;
> +	struct pci_dev *tdev = NULL;
> +	u32 bw = 0;
>   
>   	if (speed)
>   		*speed = PCI_SPEED_UNKNOWN;
>   	if (width)
>   		*width = PCIE_LNK_WIDTH_UNKNOWN;
>   
> -	bw = 0;
> -
>   	while (dev) {
> -		pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
> -
> -		next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
> -		next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
> -			PCI_EXP_LNKSTA_NLW_SHIFT;
> -
> -		next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
> -
> -		/* Check if current device limits the total bandwidth */
> -		if (!bw || next_bw <= bw) {
> -			bw = next_bw;
> -
> -			if (limiting_dev)
> -				*limiting_dev = dev;
> -			if (speed)
> -				*speed = next_speed;
> -			if (width)
> -				*width = next_width;
> +		if (dev->is_tunneled) {
> +			if (!tdev)
> +				tdev = dev;
> +			goto skip;
>   		}
> -
> +		bw = pcie_calc_bw_limits(dev, bw, limiting_dev, speed, width);
> +skip:
>   		dev = pci_upstream_bridge(dev);
>   	}
>   
> +	/* If nothing "faster" found on link, limit to first tunneled device */
> +	if (tdev && !bw)
> +		bw = pcie_calc_bw_limits(tdev, bw, limiting_dev, speed, width);
> +
>   	return bw;
>   }
>   EXPORT_SYMBOL(pcie_bandwidth_available);

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

* Re: [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled
  2023-11-03 19:07 ` [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled Mario Limonciello
  2023-11-03 19:38   ` Hans de Goede
@ 2023-11-05 17:39   ` Lukas Wunner
  2023-11-06 16:59     ` Mario Limonciello
  1 sibling, 1 reply; 32+ messages in thread
From: Lukas Wunner @ 2023-11-05 17:39 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Ilpo Järvinen, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, Hans de Goede, Bjorn Helgaas, Mika Westerberg,
	Xinhui Pan, open list, Yehezkel Bernat, Pali Rohár,
	Christian König, Maciej W . Rozycki

On Fri, Nov 03, 2023 at 02:07:55PM -0500, Mario Limonciello wrote:
> The `is_thunderbolt` bit has been used to indicate that a PCIe device
> contained the Intel VSEC which is used by various parts of the kernel
> to change behavior. To later allow usage with USB4 controllers as well,
> rename this to `is_tunneled`.

This doesn't seem to make sense.  is_thunderbolt indicates that a device
is part of a Thunderbolt controller.  See the code comment:

> -	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */

A Thunderbolt controller is not necessarily tunneled.  The PCIe switch,
NHI and XHCI of the Thunderbolt host controller are not tunneled at all.

Thanks,

Lukas

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

* Re: [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable()
  2023-11-03 19:07 ` [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable() Mario Limonciello
@ 2023-11-06 12:25   ` Ilpo Järvinen
  2023-11-06 16:47     ` Mika Westerberg
  0 siblings, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-11-06 12:25 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On Fri, 3 Nov 2023, Mario Limonciello wrote:

> pci_is_thunderbolt_attached() only works for Intel TBT devices. Switch to
> using dev_is_removable() to be able to detect USB4 devices as well.

Please extend this with more details. I had to lookup the TBT change to 
be able to make any guess why you're doing this (and it's still a guess 
at best).

-- 
 i.


> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/gpu/drm/nouveau/nouveau_vga.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nouveau_vga.c b/drivers/gpu/drm/nouveau/nouveau_vga.c
> index f8bf0ec26844..14215b7ca187 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_vga.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_vga.c
> @@ -94,8 +94,8 @@ nouveau_vga_init(struct nouveau_drm *drm)
>  
>  	vga_client_register(pdev, nouveau_vga_set_decode);
>  
> -	/* don't register Thunderbolt eGPU with vga_switcheroo */
> -	if (pci_is_thunderbolt_attached(pdev))
> +	/* don't register USB4/Thunderbolt eGPU with vga_switcheroo */
> +	if (dev_is_removable(&pdev->dev))
>  		return;
>  
>  	vga_switcheroo_register_client(pdev, &nouveau_switcheroo_ops, runtime);
> @@ -118,7 +118,7 @@ nouveau_vga_fini(struct nouveau_drm *drm)
>  
>  	vga_client_unregister(pdev);
>  
> -	if (pci_is_thunderbolt_attached(pdev))
> +	if (dev_is_removable(&pdev->dev))
>  		return;
>  
>  	vga_switcheroo_unregister_client(pdev);
> 

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

* Re: [PATCH v2 2/9] drm/radeon: Switch from pci_is_thunderbolt_attached() to dev_is_removable()
  2023-11-03 19:07 ` [PATCH v2 2/9] drm/radeon: " Mario Limonciello
@ 2023-11-06 12:27   ` Ilpo Järvinen
  0 siblings, 0 replies; 32+ messages in thread
From: Ilpo Järvinen @ 2023-11-06 12:27 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On Fri, 3 Nov 2023, Mario Limonciello wrote:

> pci_is_thunderbolt_attached() only works for Intel TBT devices. Switch to
> using dev_is_removable() to be able to detect USB4 devices as well.

Same here as with 1/9.

-- 
 i.

> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/gpu/drm/radeon/radeon_device.c | 4 ++--
>  drivers/gpu/drm/radeon/radeon_kms.c    | 2 +-
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/radeon/radeon_device.c b/drivers/gpu/drm/radeon/radeon_device.c
> index afbb3a80c0c6..ba0ca0694d18 100644
> --- a/drivers/gpu/drm/radeon/radeon_device.c
> +++ b/drivers/gpu/drm/radeon/radeon_device.c
> @@ -1429,7 +1429,7 @@ int radeon_device_init(struct radeon_device *rdev,
>  
>  	if (rdev->flags & RADEON_IS_PX)
>  		runtime = true;
> -	if (!pci_is_thunderbolt_attached(rdev->pdev))
> +	if (!dev_is_removable(&rdev->pdev->dev))
>  		vga_switcheroo_register_client(rdev->pdev,
>  					       &radeon_switcheroo_ops, runtime);
>  	if (runtime)
> @@ -1519,7 +1519,7 @@ void radeon_device_fini(struct radeon_device *rdev)
>  	radeon_bo_evict_vram(rdev);
>  	radeon_audio_component_fini(rdev);
>  	radeon_fini(rdev);
> -	if (!pci_is_thunderbolt_attached(rdev->pdev))
> +	if (!dev_is_removable(&rdev->pdev->dev))
>  		vga_switcheroo_unregister_client(rdev->pdev);
>  	if (rdev->flags & RADEON_IS_PX)
>  		vga_switcheroo_fini_domain_pm_ops(rdev->dev);
> diff --git a/drivers/gpu/drm/radeon/radeon_kms.c b/drivers/gpu/drm/radeon/radeon_kms.c
> index a16590c6247f..ead912a58ab8 100644
> --- a/drivers/gpu/drm/radeon/radeon_kms.c
> +++ b/drivers/gpu/drm/radeon/radeon_kms.c
> @@ -138,7 +138,7 @@ int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
>  	if ((radeon_runtime_pm != 0) &&
>  	    radeon_has_atpx() &&
>  	    ((flags & RADEON_IS_IGP) == 0) &&
> -	    !pci_is_thunderbolt_attached(pdev))
> +	    !dev_is_removable(&pdev->dev))
>  		flags |= RADEON_IS_PX;
>  
>  	/* radeon_device_init should report only fatal error
> 

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

* Re: [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached()
  2023-11-03 19:07 ` [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached() Mario Limonciello
  2023-11-04  0:37   ` kernel test robot
@ 2023-11-06 12:33   ` Ilpo Järvinen
  2023-11-06 16:46     ` Mario Limonciello
  1 sibling, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-11-06 12:33 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On Fri, 3 Nov 2023, Mario Limonciello wrote:

> All callers have switched to dev_is_removable() for detecting
> hotpluggable PCIe devices.
> 
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  include/linux/pci.h | 22 ----------------------
>  1 file changed, 22 deletions(-)
> 
> diff --git a/include/linux/pci.h b/include/linux/pci.h
> index b56417276042..530b0a360514 100644
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -2616,28 +2616,6 @@ static inline bool pci_ari_enabled(struct pci_bus *bus)
>  	return bus->self && bus->self->ari_enabled;
>  }
>  
> -/**
> - * pci_is_thunderbolt_attached - whether device is on a Thunderbolt daisy chain
> - * @pdev: PCI device to check
> - *
> - * Walk upwards from @pdev and check for each encountered bridge if it's part
> - * of a Thunderbolt controller.  Reaching the host bridge means @pdev is not
> - * Thunderbolt-attached.  (But rather soldered to the mainboard usually.)
> - */
> -static inline bool pci_is_thunderbolt_attached(struct pci_dev *pdev)
> -{
> -	struct pci_dev *parent = pdev;
> -
> -	if (pdev->is_thunderbolt)
> -		return true;
> -
> -	while ((parent = pci_upstream_bridge(parent)))
> -		if (parent->is_thunderbolt)
> -			return true;
> -
> -	return false;
> -}
> -
>  #if defined(CONFIG_PCIEPORTBUS) || defined(CONFIG_EEH)
>  void pci_uevent_ers(struct pci_dev *pdev, enum  pci_ers_result err_type);
>  #endif
> 

I don't think all callers have been removed. Ah, lkp has caught the same 
problem.

-- 
 i.


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

* Re: [PATCH v2 5/9] PCI: pciehp: Move check for is_thunderbolt into a quirk
  2023-11-03 19:07 ` [PATCH v2 5/9] PCI: pciehp: Move check for is_thunderbolt into a quirk Mario Limonciello
@ 2023-11-06 12:41   ` Ilpo Järvinen
  2023-11-06 16:50     ` Mario Limonciello
  0 siblings, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-11-06 12:41 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On Fri, 3 Nov 2023, Mario Limonciello wrote:

> commit 493fb50e958c ("PCI: pciehp: Assume NoCompl+ for Thunderbolt
> ports") added a check into pciehp code to explicitly set NoCompl+
> for all Intel Thunderbolt controllers, including those that don't
> need it.
> 
> This overloaded the purpose of the `is_thunderbolt` member of
> `struct pci_device` because that means that any controller that
> identifies as thunderbolt would set NoCompl+ even if it doesn't
> suffer this deficiency. As that commit helpfully specifies all the
> controllers with the problem, move them into a PCI quirk.
> 
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/pci/hotplug/pciehp_hpc.c |  6 +-----
>  drivers/pci/quirks.c             | 20 ++++++++++++++++++++
>  include/linux/pci.h              |  1 +
>  3 files changed, 22 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
> index fd713abdfb9f..23a92d681d1c 100644
> --- a/drivers/pci/hotplug/pciehp_hpc.c
> +++ b/drivers/pci/hotplug/pciehp_hpc.c
> @@ -991,11 +991,7 @@ struct controller *pcie_init(struct pcie_device *dev)
>  	if (pdev->hotplug_user_indicators)
>  		slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
>  
> -	/*
> -	 * We assume no Thunderbolt controllers support Command Complete events,
> -	 * but some controllers falsely claim they do.
> -	 */
> -	if (pdev->is_thunderbolt)
> +	if (pdev->no_command_complete)
>  		slot_cap |= PCI_EXP_SLTCAP_NCCS;
>  
>  	ctrl->slot_cap = slot_cap;
> diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
> index eeec1d6f9023..4bbf6e33ca11 100644
> --- a/drivers/pci/quirks.c
> +++ b/drivers/pci/quirks.c
> @@ -3807,6 +3807,26 @@ DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
>  DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PORT_RIDGE,
>  			quirk_thunderbolt_hotplug_msi);
>  
> +/*
> + * We assume no Thunderbolt controllers support Command Complete events,
> + * but some controllers falsely claim they do.

IMO, this wording makes little sense with the new code. How about taking 
some text from the original commit's changelog:

/*
 * Certain Thunderbolt 1 controllers falsely claim to support Command 
 * Completed events.
 */

The code change looks fine.

-- 
 i.

> + */
> +static void quirk_thunderbolt_command_complete(struct pci_dev *pdev)
> +{
> +	pdev->no_command_complete = 1;
> +}
> +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
> +			quirk_thunderbolt_command_complete);
> +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EAGLE_RIDGE,
> +			quirk_thunderbolt_command_complete);
> +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LIGHT_PEAK,
> +			quirk_thunderbolt_command_complete);
> +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
> +			quirk_thunderbolt_command_complete);
> +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C,
> +			quirk_thunderbolt_command_complete);
> +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PORT_RIDGE,
> +			quirk_thunderbolt_command_complete);
>  #ifdef CONFIG_ACPI
>  /*
>   * Apple: Shutdown Cactus Ridge Thunderbolt controller.
> diff --git a/include/linux/pci.h b/include/linux/pci.h
> index 530b0a360514..439c2dac8a3e 100644
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -441,6 +441,7 @@ struct pci_dev {
>  	unsigned int	is_hotplug_bridge:1;
>  	unsigned int	shpc_managed:1;		/* SHPC owned by shpchp */
>  	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */
> +	unsigned int	no_command_complete:1;	/* No command completion */
>  	/*
>  	 * Devices marked being untrusted are the ones that can potentially
>  	 * execute DMA attacks and similar. They are typically connected
> 


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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-03 19:07 ` [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available() Mario Limonciello
  2023-11-04  6:57   ` Lazar, Lijo
@ 2023-11-06 12:52   ` Ilpo Järvinen
  2023-11-06 16:51     ` Mario Limonciello
  2023-11-06 18:10   ` Lukas Wunner
  2 siblings, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-11-06 12:52 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On Fri, 3 Nov 2023, Mario Limonciello wrote:

> The USB4 spec specifies that PCIe ports that are used for tunneling
> PCIe traffic over USB4 fabric will be hardcoded to advertise 2.5GT/s and
> behave as a PCIe Gen1 device. The actual performance of these ports is
> controlled by the fabric implementation.
> 
> Downstream drivers such as amdgpu which utilize pcie_bandwidth_available()
> to program the device will always find the PCIe ports used for
> tunneling as a limiting factor potentially leading to incorrect
> performance decisions.
> 
> To prevent problems in downstream drivers check explicitly for ports
> being used for PCIe tunneling and skip them when looking for bandwidth
> limitations of the hierarchy. If the only device connected is a root port
> used for tunneling then report that device.
> 
> Downstream drivers could make this change on their own but then they
> wouldn't be able to detect other potential speed bottlenecks from the
> hierarchy without duplicating pcie_bandwidth_available() logic.
> 
> Link: https://gitlab.freedesktop.org/drm/amd/-/issues/2925#note_2145860
> Link: https://www.usb.org/document-library/usb4r-specification-v20
>       USB4 V2 with Errata and ECN through June 2023
>       Section 11.2.1
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/pci/pci.c | 74 +++++++++++++++++++++++++++++++----------------
>  1 file changed, 49 insertions(+), 25 deletions(-)
> 
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index d9aa5a39f585..15e37164ce56 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -6223,6 +6223,35 @@ int pcie_set_mps(struct pci_dev *dev, int mps)
>  }
>  EXPORT_SYMBOL(pcie_set_mps);
>  
> +static u32 pcie_calc_bw_limits(struct pci_dev *dev, u32 bw,
> +			       struct pci_dev **limiting_dev,
> +			       enum pci_bus_speed *speed,
> +			       enum pcie_link_width *width)
> +{
> +	enum pcie_link_width next_width;
> +	enum pci_bus_speed next_speed;
> +	u32 next_bw;
> +	u16 lnksta;
> +
> +	pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
> +	next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
> +	next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
> +	next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
> +
> +	/* Check if current device limits the total bandwidth */
> +	if (!bw || next_bw <= bw) {
> +		bw = next_bw;
> +		if (limiting_dev)
> +			*limiting_dev = dev;
> +		if (speed)
> +			*speed = next_speed;
> +		if (width)
> +			*width = next_width;
> +	}
> +
> +	return bw;
> +}
> +
>  /**
>   * pcie_bandwidth_available - determine minimum link settings of a PCIe
>   *			      device and its bandwidth limitation
> @@ -6236,47 +6265,42 @@ EXPORT_SYMBOL(pcie_set_mps);
>   * limiting_dev, speed, and width pointers are supplied) information about
>   * that point.  The bandwidth returned is in Mb/s, i.e., megabits/second of
>   * raw bandwidth.
> + *
> + * This excludes the bandwidth calculation that has been returned from a
> + * PCIe device used for transmitting tunneled PCIe traffic over a Thunderbolt
> + * or USB4 link that is part of larger hierarchy. The calculation is excluded
> + * because the USB4 specification specifies that the max speed returned from
> + * PCIe configuration registers for the tunneling link is always PCI 1x 2.5 GT/s.
> + * When only tunneled devices are present, the bandwidth returned is the
> + * bandwidth available from the first tunneled device.
>   */
>  u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
>  			     enum pci_bus_speed *speed,
>  			     enum pcie_link_width *width)
>  {
> -	u16 lnksta;
> -	enum pci_bus_speed next_speed;
> -	enum pcie_link_width next_width;
> -	u32 bw, next_bw;
> +	struct pci_dev *tdev = NULL;
> +	u32 bw = 0;
>  
>  	if (speed)
>  		*speed = PCI_SPEED_UNKNOWN;
>  	if (width)
>  		*width = PCIE_LNK_WIDTH_UNKNOWN;
>  
> -	bw = 0;
> -
>  	while (dev) {
> -		pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
> -
> -		next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
> -		next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
> -			PCI_EXP_LNKSTA_NLW_SHIFT;
> -
> -		next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
> -
> -		/* Check if current device limits the total bandwidth */
> -		if (!bw || next_bw <= bw) {
> -			bw = next_bw;
> -
> -			if (limiting_dev)
> -				*limiting_dev = dev;
> -			if (speed)
> -				*speed = next_speed;
> -			if (width)
> -				*width = next_width;
> +		if (dev->is_tunneled) {
> +			if (!tdev)
> +				tdev = dev;
> +			goto skip;
>  		}
> -
> +		bw = pcie_calc_bw_limits(dev, bw, limiting_dev, speed, width);
> +skip:
>  		dev = pci_upstream_bridge(dev);
>  	}
>  
> +	/* If nothing "faster" found on link, limit to first tunneled device */
> +	if (tdev && !bw)
> +		bw = pcie_calc_bw_limits(tdev, bw, limiting_dev, speed, width);
> +
>  	return bw;
>  }
>  EXPORT_SYMBOL(pcie_bandwidth_available);
> 

This patch should be split into two, where one just moves the code to the 
new function.

Also note that this will conflict with the FIELD_GET() changes (try to 
not reintroduce non-FIELD_GET() code when you rebase this on top of 
v6.7-rc1 :-)).

-- 
 i.


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

* Re: [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached()
  2023-11-06 12:33   ` Ilpo Järvinen
@ 2023-11-06 16:46     ` Mario Limonciello
  0 siblings, 0 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-06 16:46 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On 11/6/2023 06:33, Ilpo Järvinen wrote:
> On Fri, 3 Nov 2023, Mario Limonciello wrote:
> 
>> All callers have switched to dev_is_removable() for detecting
>> hotpluggable PCIe devices.
>>
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>> ---
>>   include/linux/pci.h | 22 ----------------------
>>   1 file changed, 22 deletions(-)
>>
>> diff --git a/include/linux/pci.h b/include/linux/pci.h
>> index b56417276042..530b0a360514 100644
>> --- a/include/linux/pci.h
>> +++ b/include/linux/pci.h
>> @@ -2616,28 +2616,6 @@ static inline bool pci_ari_enabled(struct pci_bus *bus)
>>   	return bus->self && bus->self->ari_enabled;
>>   }
>>   
>> -/**
>> - * pci_is_thunderbolt_attached - whether device is on a Thunderbolt daisy chain
>> - * @pdev: PCI device to check
>> - *
>> - * Walk upwards from @pdev and check for each encountered bridge if it's part
>> - * of a Thunderbolt controller.  Reaching the host bridge means @pdev is not
>> - * Thunderbolt-attached.  (But rather soldered to the mainboard usually.)
>> - */
>> -static inline bool pci_is_thunderbolt_attached(struct pci_dev *pdev)
>> -{
>> -	struct pci_dev *parent = pdev;
>> -
>> -	if (pdev->is_thunderbolt)
>> -		return true;
>> -
>> -	while ((parent = pci_upstream_bridge(parent)))
>> -		if (parent->is_thunderbolt)
>> -			return true;
>> -
>> -	return false;
>> -}
>> -
>>   #if defined(CONFIG_PCIEPORTBUS) || defined(CONFIG_EEH)
>>   void pci_uevent_ers(struct pci_dev *pdev, enum  pci_ers_result err_type);
>>   #endif
>>
> 
> I don't think all callers have been removed. Ah, lkp has caught the same
> problem.
> 

As I mentioned in the cover letter this series is done on 6.6 + a patch 
going into 6.7-rc1.  The LKP report will drop off when I rebase the 
series on 6.7-rc1.

As it's not yet in Linus' tree here is that patch so you can see it:

https://gitlab.freedesktop.org/agd5f/linux/-/commit/7b1c6263eaf4fd64ffe1cafdc504a42ee4bfbb33

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

* Re: [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable()
  2023-11-06 12:25   ` Ilpo Järvinen
@ 2023-11-06 16:47     ` Mika Westerberg
  2023-11-06 16:49       ` Mario Limonciello
  0 siblings, 1 reply; 32+ messages in thread
From: Mika Westerberg @ 2023-11-06 16:47 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Alex Deucher, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich, open list:PCI SUBSYSTEM, Manivannan Sadhasivam,
	Michael Jamet, Mark Gross, Hans de Goede, Bjorn Helgaas,
	Xinhui Pan, open list, Yehezkel Bernat, Pali Rohár,
	Christian König, Maciej W . Rozycki

On Mon, Nov 06, 2023 at 02:25:24PM +0200, Ilpo Järvinen wrote:
> On Fri, 3 Nov 2023, Mario Limonciello wrote:
> 
> > pci_is_thunderbolt_attached() only works for Intel TBT devices. Switch to
> > using dev_is_removable() to be able to detect USB4 devices as well.
> 
> Please extend this with more details. I had to lookup the TBT change to 
> be able to make any guess why you're doing this (and it's still a guess 
> at best).

Also please write it as Thunderbolt not TBT so that it is clear what we
are talking about. Ditto to all patches.

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

* Re: [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable()
  2023-11-06 16:47     ` Mika Westerberg
@ 2023-11-06 16:49       ` Mario Limonciello
  0 siblings, 0 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-06 16:49 UTC (permalink / raw)
  To: Mika Westerberg, Ilpo Järvinen
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Xinhui Pan, open list, Yehezkel Bernat,
	Pali Rohár, Christian König, Maciej W . Rozycki

On 11/6/2023 10:47, Mika Westerberg wrote:
> On Mon, Nov 06, 2023 at 02:25:24PM +0200, Ilpo Järvinen wrote:
>> On Fri, 3 Nov 2023, Mario Limonciello wrote:
>>
>>> pci_is_thunderbolt_attached() only works for Intel TBT devices. Switch to
>>> using dev_is_removable() to be able to detect USB4 devices as well.
>>
>> Please extend this with more details. I had to lookup the TBT change to
>> be able to make any guess why you're doing this (and it's still a guess
>> at best).
> 
> Also please write it as Thunderbolt not TBT so that it is clear what we
> are talking about. Ditto to all patches.

Ack, thanks I will add more detail to these first few patches and make 
that s/TBT/Thunderbolt/ change when I rebase this on 6.7-rc1.


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

* Re: [PATCH v2 5/9] PCI: pciehp: Move check for is_thunderbolt into a quirk
  2023-11-06 12:41   ` Ilpo Järvinen
@ 2023-11-06 16:50     ` Mario Limonciello
  0 siblings, 0 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-06 16:50 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On 11/6/2023 06:41, Ilpo Järvinen wrote:
> On Fri, 3 Nov 2023, Mario Limonciello wrote:
> 
>> commit 493fb50e958c ("PCI: pciehp: Assume NoCompl+ for Thunderbolt
>> ports") added a check into pciehp code to explicitly set NoCompl+
>> for all Intel Thunderbolt controllers, including those that don't
>> need it.
>>
>> This overloaded the purpose of the `is_thunderbolt` member of
>> `struct pci_device` because that means that any controller that
>> identifies as thunderbolt would set NoCompl+ even if it doesn't
>> suffer this deficiency. As that commit helpfully specifies all the
>> controllers with the problem, move them into a PCI quirk.
>>
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>> ---
>>   drivers/pci/hotplug/pciehp_hpc.c |  6 +-----
>>   drivers/pci/quirks.c             | 20 ++++++++++++++++++++
>>   include/linux/pci.h              |  1 +
>>   3 files changed, 22 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
>> index fd713abdfb9f..23a92d681d1c 100644
>> --- a/drivers/pci/hotplug/pciehp_hpc.c
>> +++ b/drivers/pci/hotplug/pciehp_hpc.c
>> @@ -991,11 +991,7 @@ struct controller *pcie_init(struct pcie_device *dev)
>>   	if (pdev->hotplug_user_indicators)
>>   		slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
>>   
>> -	/*
>> -	 * We assume no Thunderbolt controllers support Command Complete events,
>> -	 * but some controllers falsely claim they do.
>> -	 */
>> -	if (pdev->is_thunderbolt)
>> +	if (pdev->no_command_complete)
>>   		slot_cap |= PCI_EXP_SLTCAP_NCCS;
>>   
>>   	ctrl->slot_cap = slot_cap;
>> diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
>> index eeec1d6f9023..4bbf6e33ca11 100644
>> --- a/drivers/pci/quirks.c
>> +++ b/drivers/pci/quirks.c
>> @@ -3807,6 +3807,26 @@ DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
>>   DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PORT_RIDGE,
>>   			quirk_thunderbolt_hotplug_msi);
>>   
>> +/*
>> + * We assume no Thunderbolt controllers support Command Complete events,
>> + * but some controllers falsely claim they do.
> 
> IMO, this wording makes little sense with the new code. How about taking
> some text from the original commit's changelog:
> 
> /*
>   * Certain Thunderbolt 1 controllers falsely claim to support Command
>   * Completed events.
>   */
> 
> The code change looks fine.
> 

Ack, will change.

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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-06 12:52   ` Ilpo Järvinen
@ 2023-11-06 16:51     ` Mario Limonciello
  0 siblings, 0 replies; 32+ messages in thread
From: Mario Limonciello @ 2023-11-06 16:51 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Mika Westerberg, Xinhui Pan, open list,
	Yehezkel Bernat, Pali Rohár, Christian König,
	Maciej W . Rozycki

On 11/6/2023 06:52, Ilpo Järvinen wrote:
> On Fri, 3 Nov 2023, Mario Limonciello wrote:
> 
>> The USB4 spec specifies that PCIe ports that are used for tunneling
>> PCIe traffic over USB4 fabric will be hardcoded to advertise 2.5GT/s and
>> behave as a PCIe Gen1 device. The actual performance of these ports is
>> controlled by the fabric implementation.
>>
>> Downstream drivers such as amdgpu which utilize pcie_bandwidth_available()
>> to program the device will always find the PCIe ports used for
>> tunneling as a limiting factor potentially leading to incorrect
>> performance decisions.
>>
>> To prevent problems in downstream drivers check explicitly for ports
>> being used for PCIe tunneling and skip them when looking for bandwidth
>> limitations of the hierarchy. If the only device connected is a root port
>> used for tunneling then report that device.
>>
>> Downstream drivers could make this change on their own but then they
>> wouldn't be able to detect other potential speed bottlenecks from the
>> hierarchy without duplicating pcie_bandwidth_available() logic.
>>
>> Link: https://gitlab.freedesktop.org/drm/amd/-/issues/2925#note_2145860
>> Link: https://www.usb.org/document-library/usb4r-specification-v20
>>        USB4 V2 with Errata and ECN through June 2023
>>        Section 11.2.1
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>> ---
>>   drivers/pci/pci.c | 74 +++++++++++++++++++++++++++++++----------------
>>   1 file changed, 49 insertions(+), 25 deletions(-)
>>
>> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
>> index d9aa5a39f585..15e37164ce56 100644
>> --- a/drivers/pci/pci.c
>> +++ b/drivers/pci/pci.c
>> @@ -6223,6 +6223,35 @@ int pcie_set_mps(struct pci_dev *dev, int mps)
>>   }
>>   EXPORT_SYMBOL(pcie_set_mps);
>>   
>> +static u32 pcie_calc_bw_limits(struct pci_dev *dev, u32 bw,
>> +			       struct pci_dev **limiting_dev,
>> +			       enum pci_bus_speed *speed,
>> +			       enum pcie_link_width *width)
>> +{
>> +	enum pcie_link_width next_width;
>> +	enum pci_bus_speed next_speed;
>> +	u32 next_bw;
>> +	u16 lnksta;
>> +
>> +	pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
>> +	next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
>> +	next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
>> +	next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
>> +
>> +	/* Check if current device limits the total bandwidth */
>> +	if (!bw || next_bw <= bw) {
>> +		bw = next_bw;
>> +		if (limiting_dev)
>> +			*limiting_dev = dev;
>> +		if (speed)
>> +			*speed = next_speed;
>> +		if (width)
>> +			*width = next_width;
>> +	}
>> +
>> +	return bw;
>> +}
>> +
>>   /**
>>    * pcie_bandwidth_available - determine minimum link settings of a PCIe
>>    *			      device and its bandwidth limitation
>> @@ -6236,47 +6265,42 @@ EXPORT_SYMBOL(pcie_set_mps);
>>    * limiting_dev, speed, and width pointers are supplied) information about
>>    * that point.  The bandwidth returned is in Mb/s, i.e., megabits/second of
>>    * raw bandwidth.
>> + *
>> + * This excludes the bandwidth calculation that has been returned from a
>> + * PCIe device used for transmitting tunneled PCIe traffic over a Thunderbolt
>> + * or USB4 link that is part of larger hierarchy. The calculation is excluded
>> + * because the USB4 specification specifies that the max speed returned from
>> + * PCIe configuration registers for the tunneling link is always PCI 1x 2.5 GT/s.
>> + * When only tunneled devices are present, the bandwidth returned is the
>> + * bandwidth available from the first tunneled device.
>>    */
>>   u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
>>   			     enum pci_bus_speed *speed,
>>   			     enum pcie_link_width *width)
>>   {
>> -	u16 lnksta;
>> -	enum pci_bus_speed next_speed;
>> -	enum pcie_link_width next_width;
>> -	u32 bw, next_bw;
>> +	struct pci_dev *tdev = NULL;
>> +	u32 bw = 0;
>>   
>>   	if (speed)
>>   		*speed = PCI_SPEED_UNKNOWN;
>>   	if (width)
>>   		*width = PCIE_LNK_WIDTH_UNKNOWN;
>>   
>> -	bw = 0;
>> -
>>   	while (dev) {
>> -		pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
>> -
>> -		next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
>> -		next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
>> -			PCI_EXP_LNKSTA_NLW_SHIFT;
>> -
>> -		next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
>> -
>> -		/* Check if current device limits the total bandwidth */
>> -		if (!bw || next_bw <= bw) {
>> -			bw = next_bw;
>> -
>> -			if (limiting_dev)
>> -				*limiting_dev = dev;
>> -			if (speed)
>> -				*speed = next_speed;
>> -			if (width)
>> -				*width = next_width;
>> +		if (dev->is_tunneled) {
>> +			if (!tdev)
>> +				tdev = dev;
>> +			goto skip;
>>   		}
>> -
>> +		bw = pcie_calc_bw_limits(dev, bw, limiting_dev, speed, width);
>> +skip:
>>   		dev = pci_upstream_bridge(dev);
>>   	}
>>   
>> +	/* If nothing "faster" found on link, limit to first tunneled device */
>> +	if (tdev && !bw)
>> +		bw = pcie_calc_bw_limits(tdev, bw, limiting_dev, speed, width);
>> +
>>   	return bw;
>>   }
>>   EXPORT_SYMBOL(pcie_bandwidth_available);
>>
> 
> This patch should be split into two, where one just moves the code to the
> new function.

Good idea, thanks.

> 
> Also note that this will conflict with the FIELD_GET() changes (try to
> not reintroduce non-FIELD_GET() code when you rebase this on top of
> v6.7-rc1 :-)).
> 

Sure, will adjust for that when it's rebased to 6.7-rc1.

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

* Re: [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled
  2023-11-05 17:39   ` Lukas Wunner
@ 2023-11-06 16:59     ` Mario Limonciello
  2023-11-06 18:18       ` Lukas Wunner
  0 siblings, 1 reply; 32+ messages in thread
From: Mario Limonciello @ 2023-11-06 16:59 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Ilpo Järvinen, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, Hans de Goede, Bjorn Helgaas, Mika Westerberg,
	Xinhui Pan, open list, Yehezkel Bernat, Pali Rohár,
	Christian König, Maciej W . Rozycki

On 11/5/2023 11:39, Lukas Wunner wrote:
> On Fri, Nov 03, 2023 at 02:07:55PM -0500, Mario Limonciello wrote:
>> The `is_thunderbolt` bit has been used to indicate that a PCIe device
>> contained the Intel VSEC which is used by various parts of the kernel
>> to change behavior. To later allow usage with USB4 controllers as well,
>> rename this to `is_tunneled`.
> 
> This doesn't seem to make sense.  is_thunderbolt indicates that a device
> is part of a Thunderbolt controller.  See the code comment:
> 
>> -	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */
> 
> A Thunderbolt controller is not necessarily tunneled.  The PCIe switch,
> NHI and XHCI of the Thunderbolt host controller are not tunneled at all.
> 
> Thanks,
> 
> Lukas

I could really use some clarification which PCIe devices actually 
contain the Intel VSEC.

Is it in all 3 of those PCIe devices and not just the switch?

If so, I think I would rather introduce a separate bit.  So after this 
series we would have:

is_tunneled:1
is_thunderbolt:1
no_command_complete:1

* TBT1 devices would set no_command_complete
   - The consumer would be pcie_init()
* All devices with the Intel VSEC would set is_thunderbolt and the two 
consumers would be:
  - apple-gmux.c
  - pci_bridge_d3_possible()
* USB4 devices and PCIe switches with the VSEC would set is_tunneled.


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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-03 19:07 ` [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available() Mario Limonciello
  2023-11-04  6:57   ` Lazar, Lijo
  2023-11-06 12:52   ` Ilpo Järvinen
@ 2023-11-06 18:10   ` Lukas Wunner
  2023-11-06 18:44     ` Mario Limonciello
  2 siblings, 1 reply; 32+ messages in thread
From: Lukas Wunner @ 2023-11-06 18:10 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Ilpo Järvinen, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, Hans de Goede, Bjorn Helgaas, Mika Westerberg,
	Xinhui Pan, open list, Yehezkel Bernat, Pali Rohár,
	Christian König, Maciej W . Rozycki

On Fri, Nov 03, 2023 at 02:07:57PM -0500, Mario Limonciello wrote:
> The USB4 spec specifies that PCIe ports that are used for tunneling
> PCIe traffic over USB4 fabric will be hardcoded to advertise 2.5GT/s and
> behave as a PCIe Gen1 device. The actual performance of these ports is
> controlled by the fabric implementation.
> 
> Downstream drivers such as amdgpu which utilize pcie_bandwidth_available()
> to program the device will always find the PCIe ports used for
> tunneling as a limiting factor potentially leading to incorrect
> performance decisions.
> 
> To prevent problems in downstream drivers check explicitly for ports
> being used for PCIe tunneling and skip them when looking for bandwidth
> limitations of the hierarchy. If the only device connected is a root port
> used for tunneling then report that device.

I think a better approach would be to define three new bandwidths for
Thunderbolt in enum pci_bus_speed and add appropriate descriptions in
pci_speed_string().  Those three bandwidths would be 10 GBit/s for
Thunderbolt 1, 20 GBit/s for Thunderbolt 2, 40 GBit/s for Thunderbolt 3
and 4.

Code to determine the Thunderbolt generation from the PCI ID already exists
in tb_switch_get_generation().

This will not only address the amdgpu issue you're trying to solve,
but also emit an accurate speed from __pcie_print_link_status().

The speed you're reporting with your approach is not necessarily
accurate because the next non-tunneled device in the hierarchy might
be connected with a far higher PCIe speed than what the Thunderbolt
fabric allows.

Thanks,

Lukas

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

* Re: [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled
  2023-11-06 16:59     ` Mario Limonciello
@ 2023-11-06 18:18       ` Lukas Wunner
  0 siblings, 0 replies; 32+ messages in thread
From: Lukas Wunner @ 2023-11-06 18:18 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Ilpo Järvinen, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, Hans de Goede, Bjorn Helgaas, Mika Westerberg,
	Xinhui Pan, open list, Yehezkel Bernat, Pali Rohár,
	Christian König, Maciej W . Rozycki

On Mon, Nov 06, 2023 at 10:59:13AM -0600, Mario Limonciello wrote:
> On 11/5/2023 11:39, Lukas Wunner wrote:
> > On Fri, Nov 03, 2023 at 02:07:55PM -0500, Mario Limonciello wrote:
> > > The `is_thunderbolt` bit has been used to indicate that a PCIe device
> > > contained the Intel VSEC which is used by various parts of the kernel
> > > to change behavior. To later allow usage with USB4 controllers as well,
> > > rename this to `is_tunneled`.
> > 
> > This doesn't seem to make sense.  is_thunderbolt indicates that a device
> > is part of a Thunderbolt controller.  See the code comment:
> > 
> > > -	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */
> > 
> > A Thunderbolt controller is not necessarily tunneled.  The PCIe switch,
> > NHI and XHCI of the Thunderbolt host controller are not tunneled at all.
> 
> I could really use some clarification which PCIe devices actually contain
> the Intel VSEC.
> 
> Is it in all 3 of those PCIe devices and not just the switch?

Yes, I've just double-checked Light Ridge, Cactus Ridge, Alpine Ridge.

Thanks,

Lukas

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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-06 18:10   ` Lukas Wunner
@ 2023-11-06 18:44     ` Mario Limonciello
  2023-11-06 18:56       ` Lukas Wunner
  0 siblings, 1 reply; 32+ messages in thread
From: Mario Limonciello @ 2023-11-06 18:44 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Ilpo Järvinen, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, Hans de Goede, Bjorn Helgaas, Mika Westerberg,
	Xinhui Pan, open list, Yehezkel Bernat, Pali Rohár,
	Christian König, Maciej W . Rozycki

On 11/6/2023 12:10, Lukas Wunner wrote:
> On Fri, Nov 03, 2023 at 02:07:57PM -0500, Mario Limonciello wrote:
>> The USB4 spec specifies that PCIe ports that are used for tunneling
>> PCIe traffic over USB4 fabric will be hardcoded to advertise 2.5GT/s and
>> behave as a PCIe Gen1 device. The actual performance of these ports is
>> controlled by the fabric implementation.
>>
>> Downstream drivers such as amdgpu which utilize pcie_bandwidth_available()
>> to program the device will always find the PCIe ports used for
>> tunneling as a limiting factor potentially leading to incorrect
>> performance decisions.
>>
>> To prevent problems in downstream drivers check explicitly for ports
>> being used for PCIe tunneling and skip them when looking for bandwidth
>> limitations of the hierarchy. If the only device connected is a root port
>> used for tunneling then report that device.
> 
> I think a better approach would be to define three new bandwidths for
> Thunderbolt in enum pci_bus_speed and add appropriate descriptions in
> pci_speed_string().  Those three bandwidths would be 10 GBit/s for
> Thunderbolt 1, 20 GBit/s for Thunderbolt 2, 40 GBit/s for Thunderbolt 3
> and 4.

It's an interesting idea, but there's a few short comings I can think of.

1) The USB4 specification doesn't actually require 40GB/s support, this 
is only a Thunderbolt 4 requirement.

https://www.tomsguide.com/features/thunderbolt-4-vs-usb4-whats-the-difference

The TBT/USB4 link speed can be discovered, but it's not a property of 
the *switch* not of the PCIe tunneling port.

Tangentially related; the link speed is currently symmetric but there 
are two sysfs files.  Mika left a comment in 
drivers/thunderbolt/switch.c it may be asymmetric in the future. So we 
may need to keep that in mind on any design that builds on top of them.

On an AMD Phoenix system connected to a TBT3 Alpine Ridge based eGPU 
enclosure I can see:

$ cat /sys/bus/thunderbolt/devices/1-0/generation
4
$ cat /sys/bus/thunderbolt/devices/1-2/generation
3
$ cat /sys/bus/thunderbolt/devices/1-2/tx_speed
20.0 Gb/s
$ cat /sys/bus/thunderbolt/devices/1-2/rx_speed
20.0 Gb/s

2) This works until you end up with USB4v2 which supports 80GBit/s.

So this might mean an extra 80GB/s enum and porting some variation of 
usb4_switch_version() outside of the thunderbolt driver so that PCI core 
can use it too.

> 
> Code to determine the Thunderbolt generation from the PCI ID already exists
> in tb_switch_get_generation().

As 'thunderbolt' can be a module or built in, we need to bring code into 
PCI core so that it works in early boot before it loads.

On the presumption that no more "new" TBT3 devices will be released to 
the market I suppose that *a lot* of that code could come over to PCI 
core especially if we can bring some variation of usb4_switch_version() 
over too.

The other option is that we can stop allowing thunderbolt as a module 
and require it to be built-in. If we do this we can export symbols from 
it and use some of them in PCI core too.

> 
> This will not only address the amdgpu issue you're trying to solve,
> but also emit an accurate speed from __pcie_print_link_status().
> 
> The speed you're reporting with your approach is not necessarily
> accurate because the next non-tunneled device in the hierarchy might
> be connected with a far higher PCIe speed than what the Thunderbolt
> fabric allows.
>  > Thanks,
> 
> Lukas


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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-06 18:44     ` Mario Limonciello
@ 2023-11-06 18:56       ` Lukas Wunner
  2023-11-07  5:45         ` Mika Westerberg
  0 siblings, 1 reply; 32+ messages in thread
From: Lukas Wunner @ 2023-11-06 18:56 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Andreas Noever, Alex Deucher,
	Marek Behún, open list:RADEON and AMDGPU DRM DRIVERS,
	open list:ACPI, Danilo Krummrich, open list:PCI SUBSYSTEM,
	Ilpo Järvinen, Manivannan Sadhasivam, Michael Jamet,
	Mark Gross, Hans de Goede, Bjorn Helgaas, Mika Westerberg,
	Xinhui Pan, open list, Yehezkel Bernat, Pali Rohár,
	Christian König, Maciej W . Rozycki

On Mon, Nov 06, 2023 at 12:44:25PM -0600, Mario Limonciello wrote:
> Tangentially related; the link speed is currently symmetric but there are
> two sysfs files.  Mika left a comment in drivers/thunderbolt/switch.c it may
> be asymmetric in the future. So we may need to keep that in mind on any
> design that builds on top of them.

Aren't asymmetric Thunderbolt speeds just a DisplayPort thing?


> As 'thunderbolt' can be a module or built in, we need to bring code into PCI
> core so that it works in early boot before it loads.

tb_switch_get_generation() is small enough that it could be moved to the
PCI core.  I doubt that we need to make thunderbolt built-in only
or move a large amount of code to the PCI core.

Thanks,

Lukas

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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-06 18:56       ` Lukas Wunner
@ 2023-11-07  5:45         ` Mika Westerberg
  2023-11-07  6:24           ` Mika Westerberg
  0 siblings, 1 reply; 32+ messages in thread
From: Mika Westerberg @ 2023-11-07  5:45 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Alex Deucher, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich, open list:PCI SUBSYSTEM, Ilpo Järvinen,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Xinhui Pan, open list, Yehezkel Bernat,
	Pali Rohár, Christian König, Maciej W . Rozycki

Hi,

On Mon, Nov 06, 2023 at 07:56:52PM +0100, Lukas Wunner wrote:
> On Mon, Nov 06, 2023 at 12:44:25PM -0600, Mario Limonciello wrote:
> > Tangentially related; the link speed is currently symmetric but there are
> > two sysfs files.  Mika left a comment in drivers/thunderbolt/switch.c it may
> > be asymmetric in the future. So we may need to keep that in mind on any
> > design that builds on top of them.
> 
> Aren't asymmetric Thunderbolt speeds just a DisplayPort thing?

No, they affect the whole fabric. We have the initial code for
asymmetric switching in v6.7-rc1.

> > As 'thunderbolt' can be a module or built in, we need to bring code into PCI
> > core so that it works in early boot before it loads.
> 
> tb_switch_get_generation() is small enough that it could be moved to the
> PCI core.  I doubt that we need to make thunderbolt built-in only
> or move a large amount of code to the PCI core.

If at all possible I would like to avoid this and littering PCI side
with non-PCI stuff. There could be other similar "mediums" in the future
where you can transfer packets of "native" protocols such as PCIe so
instead of making it Thunderbolt/USB4 specific it should be generic
enough to support future extensions.

In case of Thunderbolt/USB4 there is no real way to figure out how much
bandwidth each PCIe tunnel gets (it is kind of bulk traffic that gets
what is left from isochronous protocols) so I would not even try that
and instead use the real PCIe links in pcie_bandwidth_available() and
skip all the "virtual" ones.

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

* Re: [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available()
  2023-11-07  5:45         ` Mika Westerberg
@ 2023-11-07  6:24           ` Mika Westerberg
  0 siblings, 0 replies; 32+ messages in thread
From: Mika Westerberg @ 2023-11-07  6:24 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: open list:THUNDERBOLT DRIVER, Karol Herbst, Rafael J . Wysocki,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:DRM DRIVER FOR NVIDIA GEFORCE/QUADRO GPUS,
	open list:X86 PLATFORM DRIVERS, Mario Limonciello,
	Andreas Noever, Alex Deucher, Marek Behún,
	open list:RADEON and AMDGPU DRM DRIVERS, open list:ACPI,
	Danilo Krummrich, open list:PCI SUBSYSTEM, Ilpo Järvinen,
	Manivannan Sadhasivam, Michael Jamet, Mark Gross, Hans de Goede,
	Bjorn Helgaas, Xinhui Pan, open list, Yehezkel Bernat,
	Pali Rohár, Christian König, Maciej W . Rozycki

On Tue, Nov 07, 2023 at 07:45:26AM +0200, Mika Westerberg wrote:
> Hi,
> 
> On Mon, Nov 06, 2023 at 07:56:52PM +0100, Lukas Wunner wrote:
> > On Mon, Nov 06, 2023 at 12:44:25PM -0600, Mario Limonciello wrote:
> > > Tangentially related; the link speed is currently symmetric but there are
> > > two sysfs files.  Mika left a comment in drivers/thunderbolt/switch.c it may
> > > be asymmetric in the future. So we may need to keep that in mind on any
> > > design that builds on top of them.
> > 
> > Aren't asymmetric Thunderbolt speeds just a DisplayPort thing?
> 
> No, they affect the whole fabric. We have the initial code for
> asymmetric switching in v6.7-rc1.
> 
> > > As 'thunderbolt' can be a module or built in, we need to bring code into PCI
> > > core so that it works in early boot before it loads.
> > 
> > tb_switch_get_generation() is small enough that it could be moved to the
> > PCI core.  I doubt that we need to make thunderbolt built-in only
> > or move a large amount of code to the PCI core.
> 
> If at all possible I would like to avoid this and littering PCI side
> with non-PCI stuff. There could be other similar "mediums" in the future
> where you can transfer packets of "native" protocols such as PCIe so
> instead of making it Thunderbolt/USB4 specific it should be generic
> enough to support future extensions.
> 
> In case of Thunderbolt/USB4 there is no real way to figure out how much
> bandwidth each PCIe tunnel gets (it is kind of bulk traffic that gets
> what is left from isochronous protocols) so I would not even try that
> and instead use the real PCIe links in pcie_bandwidth_available() and
> skip all the "virtual" ones.

Actually can we call the new function something like pci_link_is_virtual()
instead and make pcie_bandwidth_available() call it? That would be more
future proof IMHO.

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

end of thread, other threads:[~2023-11-07  6:24 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-03 19:07 [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Mario Limonciello
2023-11-03 19:07 ` [PATCH v2 1/9] drm/nouveau: Switch from pci_is_thunderbolt_attached() to dev_is_removable() Mario Limonciello
2023-11-06 12:25   ` Ilpo Järvinen
2023-11-06 16:47     ` Mika Westerberg
2023-11-06 16:49       ` Mario Limonciello
2023-11-03 19:07 ` [PATCH v2 2/9] drm/radeon: " Mario Limonciello
2023-11-06 12:27   ` Ilpo Järvinen
2023-11-03 19:07 ` [PATCH v2 3/9] PCI: Drop pci_is_thunderbolt_attached() Mario Limonciello
2023-11-04  0:37   ` kernel test robot
2023-11-06 12:33   ` Ilpo Järvinen
2023-11-06 16:46     ` Mario Limonciello
2023-11-03 19:07 ` [PATCH v2 4/9] PCI: Move the `PCI_CLASS_SERIAL_USB_USB4` definition to common header Mario Limonciello
2023-11-03 19:07 ` [PATCH v2 5/9] PCI: pciehp: Move check for is_thunderbolt into a quirk Mario Limonciello
2023-11-06 12:41   ` Ilpo Järvinen
2023-11-06 16:50     ` Mario Limonciello
2023-11-03 19:07 ` [PATCH v2 6/9] PCI: Rename is_thunderbolt to is_tunneled Mario Limonciello
2023-11-03 19:38   ` Hans de Goede
2023-11-05 17:39   ` Lukas Wunner
2023-11-06 16:59     ` Mario Limonciello
2023-11-06 18:18       ` Lukas Wunner
2023-11-03 19:07 ` [PATCH v2 7/9] PCI: ACPI: Detect PCIe root ports that are used for tunneling Mario Limonciello
2023-11-03 19:07 ` [PATCH v2 8/9] PCI: Exclude PCIe ports used for tunneling in pcie_bandwidth_available() Mario Limonciello
2023-11-04  6:57   ` Lazar, Lijo
2023-11-06 12:52   ` Ilpo Järvinen
2023-11-06 16:51     ` Mario Limonciello
2023-11-06 18:10   ` Lukas Wunner
2023-11-06 18:44     ` Mario Limonciello
2023-11-06 18:56       ` Lukas Wunner
2023-11-07  5:45         ` Mika Westerberg
2023-11-07  6:24           ` Mika Westerberg
2023-11-03 19:07 ` [PATCH v2 9/9] PCI: Add a quirk to mark 0x8086 : 0x9a23 as supporting PCIe tunneling Mario Limonciello
2023-11-03 19:20 ` [PATCH v2 0/9] Improvements to pcie_bandwidth_available() for eGPUs Bjorn Helgaas

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).