From: Andre Eikmeyer <[email protected]>

Icelake 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. Apple's ACPI tables name Thunderbolt root
ports as TRP0, TRP1, etc. Find them and create device links back
to the NHI so that PCIe tunnels can be re-established after sleep.

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

Co-developed-by: Atharva Tiwari <[email protected]>
Signed-off-by: Atharva Tiwari <[email protected]>

Signed-off-by: Andre Eikmeyer <[email protected]>
---
 drivers/thunderbolt/tb.c | 58 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 2 deletions(-)

diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index c69c323e6952..625451ff3ea0 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -6,9 +6,11 @@
  * Copyright (C) 2019, Intel Corporation
  */
 
+#include <linux/acpi.h>
 #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 +3307,73 @@ 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 Icelake thunderbolt NHI's.
+        * the root ports are stored in ACPI as TRP0,
+        * TRP1, etc. Find them and create device links
+        * so that PCIe tunnels can be re-established after
+        * sleep.
+        */
+#if IS_ENABLED(CONFIG_ACPI)
+       if (has_apple_t2_chip && (nhi->pdev->device == 
PCI_DEVICE_ID_INTEL_ICL_NHI0 ||
+                               nhi->pdev->device == 
PCI_DEVICE_ID_INTEL_ICL_NHI1)) {
+               struct acpi_device *adev;
+               unsigned int slot, func;
+               const struct device_link *link;
+               const char *bid;
+
+               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;
+
+                               adev = ACPI_COMPANION(&current_pdev->dev);
+                               if (!adev)
+                                       continue;
+
+                               bid = acpi_device_bid(adev);
+                               if (strncmp(bid, "TRP", 3) != 0)
+                                       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;
+       }
+#endif
+
        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))
-- 
2.43.0


Reply via email to