From patchwork Fri Apr 11 00:24:59 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Noever X-Patchwork-Id: 3965671 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D5A6F9F336 for ; Fri, 11 Apr 2014 00:28:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EC0C02081B for ; Fri, 11 Apr 2014 00:28:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CB4D9207F3 for ; Fri, 11 Apr 2014 00:28:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759015AbaDKA2d (ORCPT ); Thu, 10 Apr 2014 20:28:33 -0400 Received: from mail-ee0-f54.google.com ([74.125.83.54]:64523 "EHLO mail-ee0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753811AbaDKAZy (ORCPT ); Thu, 10 Apr 2014 20:25:54 -0400 Received: by mail-ee0-f54.google.com with SMTP id d49so3515247eek.41 for ; Thu, 10 Apr 2014 17:25:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=d5xrHa9JlqE6YUp8xVge9YnjhBH4Np4AaZshYyciKhM=; b=lL7PqwP8ejPqVkie4ncKmFA3OFUakGph6BiF33jwwcS2jI36T5ELDczUkhziHVpTlv pBqxC18jrueMF0JbqJbOMgn3+FEPji0lvBtq0Chl/G+uB3bkXpJEMTVhR26z9jvk7FQ2 +x937wYcM0yDIZPdgRS8Oy/KTclNw3cV6jy6wtTK8DVPzTMyx93cfWv7lf+SNZJ1gIF4 A6qciyfEjGBfybvqyVPBiRl9l4DmTEMnQY8ItwugU72raXke5GJkFVB7fY50U5L06C2d QesvJyXZ9OgOcvqwPwkwG6uT9ssQhgwPRV8KuZsP2/9wT4doVdO7cgCCtjd7DsuCyWpw BzhQ== X-Received: by 10.14.2.68 with SMTP id 44mr6717958eee.63.1397175952610; Thu, 10 Apr 2014 17:25:52 -0700 (PDT) Received: from localhost.localdomain (77-58-151-250.dclient.hispeed.ch. [77.58.151.250]) by mx.google.com with ESMTPSA id g3sm13535401eet.35.2014.04.10.17.25.51 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Apr 2014 17:25:52 -0700 (PDT) From: Andreas Noever To: linux-kernel@vger.kernel.org, Matthew Garrett Cc: Daniel J Blueman , Bjorn Helgaas , linux-pci@vger.kernel.org, Andreas Noever Subject: [PATCH v2 12/14] pci: Suspend/resume support for appel thunderbolt Date: Fri, 11 Apr 2014 02:24:59 +0200 Message-Id: <1397175901-4023-13-git-send-email-andreas.noever@gmail.com> X-Mailer: git-send-email 1.9.2 In-Reply-To: <1397175901-4023-1-git-send-email-andreas.noever@gmail.com> References: <1397175901-4023-1-git-send-email-andreas.noever@gmail.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch makes changes to the pcieport driver to support thunderbolt suspend/resume on apple systems. We need to perform two different actions during suspend and resume: The whole controller has to be powered down before suspend. If this is not done then the NHI device will be gone after resume if a thunderbolt device was plugged in while suspending. The controller represents itself as multiple PCI devices/bridges. To power it down we hook into the upstream bridge of the controller and call the magic ACPI methods. Power will be restored automatically during resume (by the firmware presumably). During resume we have to wait for the NHI do reestablish all pci tunnels. Since there is no parent-child relationship between the NHI and the bridges we have to explicitly wait for them using device_pm_wait_for_dev. We do this in the resume_noirq phase of the downstream bridges of the controller (which lead into the thunderbolt tunnels). Signed-off-by: Andreas Noever --- drivers/pci/pcie/portdrv_pci.c | 117 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/drivers/pci/pcie/portdrv_pci.c b/drivers/pci/pcie/portdrv_pci.c index 0d8fdc4..6d33666 100644 --- a/drivers/pci/pcie/portdrv_pci.c +++ b/drivers/pci/pcie/portdrv_pci.c @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "portdrv.h" #include "aer/aerdrv.h" @@ -79,6 +82,114 @@ static int pcie_portdrv_restore_config(struct pci_dev *dev) } #ifdef CONFIG_PM + +#if defined(CONFIG_THUNDERBOLT) || defined(CONFIG_THUNDERBOLT_MODULE) + +bool is_thunderbolt(struct pci_dev *pdev) +{ + if (!dmi_match(DMI_PRODUCT_NAME, "MacBookPro10,1")) + return false; + return pdev->vendor == PCI_VENDOR_ID_INTEL && pdev->device == 0x1547; +} + +static void shutdown_thunderbolt_controller(struct pci_dev *pdev) +{ + /* + * The thunderbolt controller consists of a pcie switch with downstream + * bridges leading to the NHI (native host interface) and to the tunnel + * pci bridges. + * + * Without the following quirk the NHI will not resume if a tb device was + * plugged in before suspend. + * + * This quirk cuts power to the whole chip. Therefore we have to apply it + * during suspend_noirq of the upstream bridge. + * + * Power is automagically restored before resume. No action is needed. + */ + acpi_handle bridge, SXIO, SXFP, SXLV; + if (!is_thunderbolt(pdev)) + return; + if (pci_pcie_type(pdev) != PCI_EXP_TYPE_UPSTREAM) + return; + bridge = ACPI_HANDLE(&pdev->dev); + if (!bridge) + return; + /* + * TB bridges in external devices might have the same device id as those + * on the host, but they will not have the associated ACPI methods. This + * implicitly checks that we are at the right bridge. + */ + if (ACPI_FAILURE(acpi_get_handle(bridge, "DSB0.NHI0.SXIO", &SXIO)) + || ACPI_FAILURE(acpi_get_handle(bridge, "DSB0.NHI0.SXFP", &SXFP)) + || ACPI_FAILURE(acpi_get_handle(bridge, "DSB0.NHI0.SXLV", &SXLV))) + return; + dev_info(&pdev->dev, "cutting power to thunderbolt controller...\n"); + + /* save registers manually, the pci core won't be able to do later */ + pci_save_state(pdev); + pci_prepare_to_sleep(pdev); + + /* magic sequence */ + acpi_execute_simple_method(SXIO, NULL, 1); + acpi_execute_simple_method(SXFP, NULL, 0); + msleep(300); + acpi_execute_simple_method(SXLV, NULL, 0); + acpi_execute_simple_method(SXIO, NULL, 0); + acpi_execute_simple_method(SXLV, NULL, 0); +} + +static void wait_for_thunderbolt_controller(struct pci_dev *pdev) +{ + struct pci_dev *sibling = NULL; + struct pci_dev *nhi = NULL; + /* + * During suspend the thunderbolt controller is reset and all pci + * tunnels are lost. The NHI driver will try to reestablish all tunnels + * during resume. We have to manually wait for the NHI since there is + * no parent child relationship between the NHI and the tunneled + * bridges. + */ + if (!is_thunderbolt(pdev)) + return; + if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM) + return; + /* + * Find the NHI and confirm that we are a bridge on the tb host + * controller and not on a tb endpoint. + */ + sibling = pci_get_slot(pdev->bus, 0x0); + if (sibling == pdev) + goto out; /* we are the downstream bridge to the NHI */ + if (!sibling || !sibling->subordinate) + goto out; + nhi = pci_get_slot(sibling->subordinate, 0x0); + if (!nhi) + goto out; + if (nhi->vendor != PCI_VENDOR_ID_INTEL || nhi->device != 0x1547 + || nhi->subsystem_vendor != 0x2222 + || nhi->subsystem_device != 0x1111) + goto out; + dev_info(&pdev->dev, "wating for thunderbolt to reestablish pci tunnels...\n"); + device_pm_wait_for_dev(&pdev->dev, &nhi->dev); +out: + pci_dev_put(sibling); + pci_dev_put(nhi); +} + +#else + +static void shutdown_thunderbolt_controller(struct pci_dev *pdev) { } +static void wait_for_thunderbolt_controller(struct pci_dev *pdev) { } + +#endif + +int pcie_port_suspend_noirq(struct device *dev) +{ + shutdown_thunderbolt_controller(to_pci_dev(dev)); + return 0; +} + static int pcie_port_resume_noirq(struct device *dev) { struct pci_dev *pdev = to_pci_dev(dev); @@ -90,6 +201,9 @@ static int pcie_port_resume_noirq(struct device *dev) */ if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT) pcie_clear_root_pme_status(pdev); + + wait_for_thunderbolt_controller(pdev); + return 0; } @@ -171,7 +285,10 @@ static const struct dev_pm_ops pcie_portdrv_pm_ops = { .thaw = pcie_port_device_resume, .poweroff = pcie_port_device_suspend, .restore = pcie_port_device_resume, + .suspend_noirq = pcie_port_suspend_noirq, .resume_noirq = pcie_port_resume_noirq, + .poweroff_noirq = pcie_port_suspend_noirq, + .restore_noirq = pcie_port_resume_noirq, .runtime_suspend = pcie_port_runtime_suspend, .runtime_resume = pcie_port_runtime_resume, .runtime_idle = pcie_port_runtime_idle,