Skip to contentSkip to conversation

Add Apple T2 NHI device links

PATCH Superseded Atharva Tiwari 5 participants 11 messages Jul 27 – Aug 7

Subsystems (11)
x86 USB PCI x86 platform drivers +7 more subsystems

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add Apple T2 NHI device links

This series adds a cached flag to detect Apple T2 systems, then uses it to create device links for the Thunderbolt NHI.

Changes in v5:
- Used Icelake root PCI IDs for detection of root ports instead of TRP

Changes in v4:
- Limited TRP check to T2 Icelake systems only
- Optimized has_apple_t2_chip check
- Included pci.h to drivers/thunderbolt/tb.c
- Used __free(pci_dev_put) to avoid label

Changes in v3:
- Fix build errors with non-x86 machines
- Used __initconst for apple_t2_devices
- Removed UEFI_QUIRK_SKIP_CERT as its unused now
- Used IS_ENABLED(CONFIG_ACPI) in tb_apple_add_links for the T2 part
- Removed !bid check to remove warning with -Waddress

Changes in v2:
- Used a less generic name for has_t2_chip
- Used DMI instead of PCI for has_apple_t2_chip to avoid PCI problems
- Initialized ret in tb_apple_add_links

Link to v4:
https://lore.kernel.org/all/[email protected]/

Link to v3:
https://lore.kernel.org/all/[email protected]/

Link to v2:
https://lore.kernel.org/all/[email protected]/

Link to v1:
https://lore.kernel.org/all/[email protected]/

Atharva Tiwari (2):
treewide: Add a flag to detect the Apple T2 chip
thunderbolt: Add device links for Apple T2 NHI

 arch/x86/kernel/quirks.c                      | 37 +++++++++++++
 drivers/thunderbolt/tb.c                      | 54 ++++++++++++++++++-
 include/linux/platform_data/x86/apple.h       |  5 ++
 .../platform_certs/keyring_handler.h          |  8 ---
 security/integrity/platform_certs/load_uefi.c | 38 +++----------
 5 files changed, 102 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/2 treewide: Add a flag to detect the Apple T2 chip

Add a flag to detect Apple T2 chips on Intel Macs. Cache the result to avoid repeated checks. That will be used in upcoming patches.

---
 arch/x86/kernel/quirks.c                      | 37 ++++++++++++++++++
 include/linux/platform_data/x86/apple.h       |  5 +++
 .../platform_certs/keyring_handler.h          |  8 ----
 security/integrity/platform_certs/load_uefi.c | 38 ++++---------------
 4 files changed, 50 insertions(+), 38 deletions(-)
arch/x86/kernel/quirks.c +37 -0
index a92f18db9610..6651e5d4d106 100644
@@ -664,8 +664,45 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras
 bool x86_apple_machine;
 EXPORT_SYMBOL(x86_apple_machine);
 
+bool has_apple_t2_chip;
+EXPORT_SYMBOL(has_apple_t2_chip);
+
+static const char * const __initconst apple_t2_models[] = {
+	"MacBookPro15,1",
+	"MacBookPro15,2",
+	"MacBookPro15,3",
+	"MacBookPro15,4",
+	"MacBookPro16,1",
+	"MacBookPro16,2",
+	"MacBookPro16,3",
+	"MacBookPro16,4",
+	"MacBookAir8,1",
+	"MacBookAir8,2",
+	"MacBookAir9,1",
+	"Macmini8,1",
+	"MacPro7,1",
+	"iMac20,1",
+	"iMac20,2",
+	"iMacPro1,1",
+};
+
 void __init early_platform_quirks(void)
 {
 	x86_apple_machine = dmi_match(DMI_SYS_VENDOR, "Apple Inc.") ||
 			    dmi_match(DMI_SYS_VENDOR, "Apple Computer, Inc.");
+
+	if (x86_apple_machine) {
+		const char *product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
+		int i;
+
+		if (!product_name)
+			return;
+
+		for (i = 0; i < ARRAY_SIZE(apple_t2_models); i++) {
+			if (!strcmp(product_name, apple_t2_models[i])) {
+				has_apple_t2_chip = true;
+				break;
+			}
+		}
+	}
 }
include/linux/platform_data/x86/apple.h +5 -0
index 079e816c3c21..47b27dceab18 100644
@@ -6,8 +6,13 @@
  * x86_apple_machine - whether the machine is an x86 Apple Macintosh
  */
 extern bool x86_apple_machine;
+/**
+ * has_apple_t2_chip - whether the machine has the Apple T2 chip
+ */
+extern bool has_apple_t2_chip;
 #else
 #define x86_apple_machine false
+#define has_apple_t2_chip false
 #endif
 
 #endif
security/integrity/platform_certs/keyring_handler.h +0 -8
index f92895cc50f6..a355f4400179 100644
@@ -45,11 +45,3 @@ efi_element_handler_t get_handler_for_code_signing_keys(const efi_guid_t *sig_ty
 efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type);
 
 #endif
-
-#ifndef UEFI_QUIRK_SKIP_CERT
-#define UEFI_QUIRK_SKIP_CERT(vendor, product) \
-		 .matches = { \
-			DMI_MATCH(DMI_BOARD_VENDOR, vendor), \
-			DMI_MATCH(DMI_PRODUCT_NAME, product), \
-		},
-#endif
security/integrity/platform_certs/load_uefi.c +8 -30
index c0d6948446c3..b4096d4c828d 100644
@@ -3,42 +3,16 @@
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/cred.h>
-#include <linux/dmi.h>
 #include <linux/err.h>
 #include <linux/efi.h>
 #include <linux/slab.h>
 #include <linux/ima.h>
+#include <linux/platform_data/x86/apple.h>
 #include <keys/asymmetric-type.h>
 #include <keys/system_keyring.h>
 #include "../integrity.h"
 #include "keyring_handler.h"
 
-/*
- * On T2 Macs reading the db and dbx efi variables to load UEFI Secure Boot
- * certificates causes occurrence of a page fault in Apple's firmware and
- * a crash disabling EFI runtime services. The following quirk skips reading
- * these variables.
- */
-static const struct dmi_system_id uefi_skip_cert[] = {
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,3") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,4") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,3") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,4") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir9,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "Macmini8,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacPro7,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMacPro1,1") },
-	{ }
-};
-
 /*
  * Look to see if a UEFI variable called MokIgnoreDB exists and return true if
  * it does.
@@ -165,10 +139,14 @@ static int __init load_uefi_certs(void)
 	unsigned long dbsize = 0, dbxsize = 0, mokxsize = 0;
 	efi_status_t status;
 	int rc = 0;
-	const struct dmi_system_id *dmi_id;
 
-	dmi_id = dmi_first_match(uefi_skip_cert);
-	if (dmi_id) {
+	/*
+	 * On T2 Macs reading the db and dbx efi variables to load UEFI Secure Boot
+	 * certificates causes occurrence of a page fault in Apple's firmware and
+	 * a crash disabling EFI runtime services. The following quirk skips reading
+	 * these variables.
+	 */
+	if (has_apple_t2_chip) {
 		pr_err("Reading UEFI Secure Boot Certs is not supported on T2 Macs.\n");
 		return false;
 	}
Signature · 1 line
2.43.0
#5 lore raw

PATCH v5 2/2 thunderbolt: Add device links for Apple T2 NHI

In reply to Atharva Tiwari

Ice Lake Thunderbolt NHI on T2 Macs (2018–2020). The NHI and its associated PCIe Root Ports all sit directly on the Root Complex with no upstream port. Identify the tunneled PCIe Root Ports by their PCI IDs and create device links back to the NHI so that PCIe tunnels can be re-established after sleep.

And on other T2 Thunderbolt NHI's, like Titan Ridge, the default method is used to add device links.

---
 drivers/thunderbolt/tb.c | 54 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
drivers/thunderbolt/tb.c +52 -2
index c69c323e6952..9b80e9339728 100644
@@ -9,6 +9,7 @@
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/delay.h>
+#include <linux/pci.h>
 #include <linux/pm_runtime.h>
 #include <linux/platform_data/x86/apple.h>
 
@@ -3305,21 +3306,70 @@ static const struct tb_cm_ops tb_cm_ops = {
 static bool tb_apple_add_links(struct tb_nhi *nhi)
 {
 	struct pci_dev *upstream, *pdev;
-	bool ret;
+	bool ret = false;
 
 	if (!x86_apple_machine)
 		return false;
 
+	/* On T2 Macs with Ice Lake Thunderbolt NHIs,
+	 * identify the tunneled PCIe Root Ports by their PCI IDs
+	 * and create device links so that
+	 * PCIe tunnels can be re-established after sleep.
+	 */
+	if (has_apple_t2_chip && (nhi->pdev->device == PCI_DEVICE_ID_INTEL_ICL_NHI0 ||
+				nhi->pdev->device == PCI_DEVICE_ID_INTEL_ICL_NHI1)) {
+		unsigned int slot, func;
+		const struct device_link *link;
+
+		for (slot = 0; slot < 32; slot++) {
+			for (func = 0; func < 8; func++) {
+				struct pci_dev *current_pdev __free(pci_dev_put) =
+					pci_get_slot(nhi->pdev->bus, PCI_DEVFN(slot, func));
+				if (!current_pdev)
+					continue;
+
+				if (!pci_is_pcie(current_pdev) || pci_pcie_type(current_pdev) !=
+						PCI_EXP_TYPE_ROOT_PORT)
+					continue;
+
+				switch (current_pdev->device) {
+				case 0x8a1d:
+				case 0x8a1f:
+				case 0x8a21:
+				case 0x8a23:
+					break;
+				default:
+					continue;
+				}
+
+				link = device_link_add(&current_pdev->dev, &nhi->pdev->dev,
+						DL_FLAG_AUTOREMOVE_SUPPLIER |
+						DL_FLAG_PM_RUNTIME);
+				if (link) {
+					dev_dbg(&nhi->pdev->dev, "created link from %s\n",
+						dev_name(&current_pdev->dev));
+					ret = true;
+				} else {
+					dev_warn(&nhi->pdev->dev,
+						"device link creation from %s failed\n",
+						dev_name(&current_pdev->dev));
+				}
+			}
+		}
+		return ret;
+	}
+
 	switch (nhi->pdev->device) {
 	case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
 	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
+	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
+	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
 		break;
 	default:
 		return false;
 	}
-
 	upstream = pci_upstream_bridge(nhi->pdev);
 	while (upstream) {
 		if (!pci_is_pcie(upstream))
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v5 2/2] thunderbolt: Add device links for Apple T2 NHI

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Ice Lake Thunderbolt NHI on T2 Macs (2018–2020). The NHI and its associated PCIe Root Ports all sit directly on the Root Complex with no upstream port. Identify the tunneled PCIe Root Ports by their PCI IDs and create device links back to the NHI so that PCIe tunnels can be re-established after sleep.

Quoted 7 lines

And on other T2 Thunderbolt NHI's, like Titan Ridge, the default method is used to add device links.

Sashiko has reviewed this patch and found no issues. It looks great!

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

Re: [PATCH v5 1/2] treewide: Add a flag to detect the Apple T2 chip

In reply to Atharva Tiwari
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Add a flag to detect Apple T2 chips on Intel Macs. Cache the result to avoid repeated checks. That will be used in upcoming patches.

Sashiko has reviewed this patch and found no issues. It looks great!

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

Re: [PATCH v5 1/2] treewide: Add a flag to detect the Apple T2 chip

In reply to Atharva Tiwari
Quoted 17 lines · Atharva Tiwari

Add a flag to detect Apple T2 chips on Intel Macs. Cache the result to avoid repeated checks. That will be used in upcoming patches.

---
 arch/x86/kernel/quirks.c                      | 37 ++++++++++++++++++
 include/linux/platform_data/x86/apple.h       |  5 +++
 .../platform_certs/keyring_handler.h          |  8 ----
 security/integrity/platform_certs/load_uefi.c | 38 ++++---------------
 4 files changed, 50 insertions(+), 38 deletions(-)
arch/x86/kernel/quirks.c +0 -0
index a92f18db9610..6651e5d4d106 100644

arch/x86/kernel/quirks.c DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras · line 668

Quoted 5 lines
arch/x86/kernel/quirks.c +2 -0
@@ -664,8 +664,45 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras
 bool x86_apple_machine;
 EXPORT_SYMBOL(x86_apple_machine);
 
+bool has_apple_t2_chip;
+EXPORT_SYMBOL(has_apple_t2_chip);

I'm guessing this is parrotting the non-GPL export of x86_apple_machine from

630b3aff8a51 ("treewide: Consolidate Apple DMI checks")

Lemme add those folks and ask why aren't those exports GPL only?

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#7 lore raw

Re: [PATCH v5 2/2] thunderbolt: Add device links for Apple T2 NHI

In reply to Atharva Tiwari
Quoted 22 lines · Atharva Tiwari

Ice Lake Thunderbolt NHI on T2 Macs (2018–2020). The NHI and its associated PCIe Root Ports all sit directly on the Root Complex with no upstream port. Identify the tunneled PCIe Root Ports by their PCI IDs and create device links back to the NHI so that PCIe tunnels can be re-established after sleep.

And on other T2 Thunderbolt NHI's, like Titan Ridge, the default method is used to add device links.

---
 drivers/thunderbolt/tb.c | 54 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
drivers/thunderbolt/tb.c +0 -0
index c69c323e6952..9b80e9339728 100644

drivers/thunderbolt/tb.c line 12

Quoted 4 lines
drivers/thunderbolt/tb.c +1 -0
@@ -9,6 +9,7 @@
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/delay.h>
+#include <linux/pci.h>

We should not be calling PCI functions anymore from the SW CM core. We have pci.c for that.

drivers/thunderbolt/tb.c static const struct tb_cm_ops tb_cm_ops = { · line 3306

Quoted 5 lines
drivers/thunderbolt/tb.c +0 -0
 #include <linux/pm_runtime.h>
 #include <linux/platform_data/x86/apple.h>
 
@@ -3305,21 +3306,70 @@ static const struct tb_cm_ops tb_cm_ops = {
 static bool tb_apple_add_links(struct tb_nhi *nhi)

I wonder if we can put this to pci.c, rename it to tb_pci_add_links() instead.

drivers/thunderbolt/tb.c static const struct tb_cm_ops tb_cm_ops = { · line 3312

Quoted 7 lines
drivers/thunderbolt/tb.c +1 -1
 {
 	struct pci_dev *upstream, *pdev;
-	bool ret;
+	bool ret = false;
 
 	if (!x86_apple_machine)
 		return false;

BTW, why you need to differentiate T2 vs. the rest of Apple x86? Don't this variable do?

Quoted 5 lines
drivers/thunderbolt/tb.c +4 -0
+	/* On T2 Macs with Ice Lake Thunderbolt NHIs,
+	 * identify the tunneled PCIe Root Ports by their PCI IDs
+	 * and create device links so that
+	 * PCIe tunnels can be re-established after sleep.

drivers/thunderbolt/tb.c static const struct tb_cm_ops tb_cm_ops = { · line 3327

Quoted 10 lines
drivers/thunderbolt/tb.c +10 -0
+	 */
+	if (has_apple_t2_chip && (nhi->pdev->device == PCI_DEVICE_ID_INTEL_ICL_NHI0 ||
+				nhi->pdev->device == PCI_DEVICE_ID_INTEL_ICL_NHI1)) {
+		unsigned int slot, func;
+		const struct device_link *link;
+
+		for (slot = 0; slot < 32; slot++) {
+			for (func = 0; func < 8; func++) {
+				struct pci_dev *current_pdev __free(pci_dev_put) =
+					pci_get_slot(nhi->pdev->bus, PCI_DEVFN(slot, func));

I'm not fan of __free() and the like so let's not use it here.

Also you don't need to scan all the slots. Just look for the tunneled downstream ports based on their PCI IDs like we do already.

Quoted 36 lines
drivers/thunderbolt/tb.c +34 -0
+				if (!current_pdev)
+					continue;
+
+				if (!pci_is_pcie(current_pdev) || pci_pcie_type(current_pdev) !=
+						PCI_EXP_TYPE_ROOT_PORT)
+					continue;
+
+				switch (current_pdev->device) {
+				case 0x8a1d:
+				case 0x8a1f:
+				case 0x8a21:
+				case 0x8a23:
+					break;
+				default:
+					continue;
+				}
+
+				link = device_link_add(&current_pdev->dev, &nhi->pdev->dev,
+						DL_FLAG_AUTOREMOVE_SUPPLIER |
+						DL_FLAG_PM_RUNTIME);
+				if (link) {
+					dev_dbg(&nhi->pdev->dev, "created link from %s\n",
+						dev_name(&current_pdev->dev));
+					ret = true;
+				} else {
+					dev_warn(&nhi->pdev->dev,
+						"device link creation from %s failed\n",
+						dev_name(&current_pdev->dev));
+				}
+			}
+		}
+		return ret;
+	}
+
 	switch (nhi->pdev->device) {
 	case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:

drivers/thunderbolt/tb.c static const struct tb_cm_ops tb_cm_ops = { · line 3322

Quoted 10 lines
drivers/thunderbolt/tb.c +2 -1
 	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
+	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
+	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
 		break;
 	default:
 		return false;
 	}
-

This is unrelated change.

Quoted 5 lines
drivers/thunderbolt/tb.c +0 -0
 	upstream = pci_upstream_bridge(nhi->pdev);
 	while (upstream) {
 		if (!pci_is_pcie(upstream))
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v5 2/2] thunderbolt: Add device links for Apple T2 NHI

Quoted 2 lines

We should not be calling PCI functions anymore from the SW CM core. We have pci.c for that.

That is outside the scope of this patch, this should be another patch.

Quoted 2 lines

I wonder if we can put this to pci.c, rename it to tb_pci_add_links() instead.

Same thing as before, its outside the scope of this patch.

Quoted 2 lines

BTW, why you need to differentiate T2 vs. the rest of Apple x86? Don't this variable do?

Because the Patchset is specifically for T2 Macs.

Quoted 4 lines

I'm not fan of __free() and the like so let's not use it here.

Also you don't need to scan all the slots. Just look for the tunneled downstream ports based on their PCI IDs like we do already.

Could you please point me to the "like we do already" code you're referring to?

Quoted 1 line

This is unrelated change.

Its not. the patch specifically says T2 macs, and Titan ridge is only on T2 Macs so we dont need to use has_apple_t2_chip for that.

#9 lore raw

Re: [PATCH v5 2/2] thunderbolt: Add device links for Apple T2 NHI

Hi,

Quoted 4 lines · Atharva Tiwari
Quoted 2 lines

We should not be calling PCI functions anymore from the SW CM core. We have pci.c for that.

That is outside the scope of this patch, this should be another patch.

Agree. It should be preparatory patch for the series.

Quoted 9 lines
Quoted 2 lines

I wonder if we can put this to pci.c, rename it to tb_pci_add_links() instead.

Same thing as before, its outside the scope of this patch.

Quoted 2 lines

BTW, why you need to differentiate T2 vs. the rest of Apple x86? Don't this variable do?

Because the Patchset is specifically for T2 Macs.

Yes but don't x86_apple_machine is true for T2 macs as well? Why need a separate variable?

Quoted 6 lines
Quoted 4 lines

I'm not fan of __free() and the like so let's not use it here.

Also you don't need to scan all the slots. Just look for the tunneled downstream ports based on their PCI IDs like we do already.

Could you please point me to the "like we do already" code you're referring to?

Yes see below.

Quoted 4 lines
Quoted 1 line

This is unrelated change.

Its not. the patch specifically says T2 macs, and Titan ridge is only on T2 Macs so we dont need to use has_apple_t2_chip for that.

I meant the extra empty line that your patch adds.

In addition to rename and move to pci.c (as separate patch) something like this (completely untested) I had in mind. Does that make sense? It does not add the Titan Ridge IDs, you can add them to the discrete part then.

drivers/thunderbolt/tb.c +82 -34
index ddeba0861a2b..3ad6ece6b808 100644
@@ -3397,32 +3397,30 @@ static const struct tb_cm_ops tb_cm_ops = {
 	.disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
 };
 
-/*
- * During suspend the Thunderbolt controller is reset and all PCIe
- * tunnels are lost. The NHI driver will try to reestablish all tunnels
- * during resume. This adds device links between the tunneled PCIe
- * downstream ports and the NHI so that the device core will make sure
- * NHI is resumed first before the rest.
- */
-static bool tb_apple_add_links(struct tb_nhi *nhi)
+static bool add_link(struct device *dev, struct device *nhi)
 {
-	struct pci_dev *nhi_pdev = to_pci_dev(nhi->dev);
-	struct pci_dev *upstream, *pdev;
-	bool ret;
+	const struct device_link *link;
 
-	if (!x86_apple_machine)
-		return false;
-
-	switch (nhi_pdev->device) {
-	case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
-	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
-	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
-	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
-		break;
-	default:
-		return false;
+	link = device_link_add(dev, nhi, DL_FLAG_AUTOREMOVE_SUPPLIER |
+				DL_FLAG_PM_RUNTIME);
+	if (link) {
+		dev_dbg(nhi, "created link from %s\n",
+			dev_name(dev));
+		return true;
+	} else {
+		dev_warn(nhi, "device link creation from %s failed\n",
+			 dev_name(dev));
 	}
 
+	return false;
+}
+
+static bool
+tb_pci_add_links_discrete(struct tb_nhi *nhi, struct pci_dev *nhi_pdev)
+{
+	struct pci_dev *upstream, *pdev;
+	bool ret;
+
 	upstream = pci_upstream_bridge(nhi_pdev);
 	while (upstream) {
 		if (!pci_is_pcie(upstream))
@@ -3442,30 +3440,80 @@ static bool tb_apple_add_links(struct tb_nhi *nhi)
 	 */
 	ret = false;
 	for_each_pci_bridge(pdev, upstream->subordinate) {
-		const struct device_link *link;
-
 		if (!pci_is_pcie(pdev))
 			continue;
 		if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
 		    !pdev->is_pciehp)
 			continue;
 
-		link = device_link_add(&pdev->dev, nhi->dev,
-				       DL_FLAG_AUTOREMOVE_SUPPLIER |
-				       DL_FLAG_PM_RUNTIME);
-		if (link) {
-			dev_dbg(nhi->dev, "created link from %s\n",
-				dev_name(&pdev->dev));
-			ret = true;
-		} else {
-			dev_warn(nhi->dev, "device link creation from %s failed\n",
-				 dev_name(&pdev->dev));
+		ret = add_link(&pdev->dev, nhi->dev);
+	}
+
+	return ret;
+}
+
+static bool
+tb_pci_add_links_integrated(struct tb_nhi *nhi, struct pci_dev *nhi_pdev)
+{
+	struct pci_bus *bus = nhi_pdev->bus;
+	struct pci_dev *pdev;
+	bool ret = false;
+
+	/*
+	 * On intergrated the tunneled PCIe root ports are directly
+	 * under the host bridge.
+	 */
+	if (!pci_is_root_bus(bus))
+		return false;
+
+	for_each_pci_bridge(pdev, bus) {
+		switch (nhi_pdev->device) {
+		case PCI_DEVICE_ID_INTEL_ICL_NHI0:
+			if (pdev->device == 0x8a1d || pdev->device == 0x8a1fb)
+				ret = add_link(&pdev->dev, nhi->dev);
+			break;
+		case PCI_DEVICE_ID_INTEL_ICL_NHI1:
+			if (pdev->device == 0x8a21 || pdev->device == 0x8a23)
+				ret = add_link(&pdev->dev, nhi->dev);
+			break;
+		default:
+			break;
 		}
 	}
 
 	return ret;
 }
 
+/*
+ * During suspend the Thunderbolt controller is reset and all PCIe
+ * tunnels are lost. The NHI driver will try to reestablish all tunnels
+ * during resume. This adds device links between the tunneled PCIe
+ * downstream ports and the NHI so that the device core will make sure
+ * NHI is resumed first before the rest.
+ */
+static bool tb_apple_add_links(struct tb_nhi *nhi)
+{
+	struct pci_dev *nhi_pdev = to_pci_dev(nhi->dev);
+
+	if (!x86_apple_machine)
+		return false;
+
+	switch (nhi_pdev->device) {
+	case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
+	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
+	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
+	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
+		return tb_pci_add_links_discrete(nhi, nhi_pdev);
+
+	case PCI_DEVICE_ID_INTEL_ICL_NHI0:
+	case PCI_DEVICE_ID_INTEL_ICL_NHI1:
+		return tb_pci_add_links_integrated(nhi, nhi_pdev);
+
+	default:
+		return false;
+	}
+}
+
 struct tb *tb_probe(struct tb_nhi *nhi)
 {
 	struct tb_cm *tcm;
#10 lore raw

Re: [PATCH v5 2/2] thunderbolt: Add device links for Apple T2 NHI

Quoted 1 line

Agree. It should be preparatory patch for the series.

I think ur ideas should be implimeneted AFTER the patch, because this patch adds a kind-of important feature to T2 macs, and well ur ideas are for code quality, and well it takes time for patches to upstream

#4 lore raw

Re: [PATCH v5 1/2] treewide: Add a flag to detect the Apple T2 chip

In reply to Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 3 lines · Atharva Tiwari

Add a flag to detect Apple T2 chips on Intel Macs. Cache the result to avoid repeated checks. That will be used in upcoming patches.

Why don't you use match_string()?

...

Quoted 11 lines

arch/x86/kernel/quirks.c DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras · line 668

Quoted 5 lines

bool x86_apple_machine;
EXPORT_SYMBOL(x86_apple_machine);

arch/x86/kernel/quirks.c +2 -0
+bool has_apple_t2_chip;
+EXPORT_SYMBOL(has_apple_t2_chip);

I'm guessing this is parrotting the non-GPL export of x86_apple_machine from

630b3aff8a51 ("treewide: Consolidate Apple DMI checks")

Lemme add those folks and ask why aren't those exports GPL only?

I don't remember the details, but brief grepping shows no impediments (from my p.o.v.) to make them GPL. Maybe Lukas knows more?

Signature · 2 lines
With Best Regards,
Andy Shevchenko