linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lukas Wunner <lukas@wunner.de>
To: Bjorn Helgaas <helgaas@kernel.org>
Cc: Andreas Noever <andreas.noever@gmail.com>, linux-pci@vger.kernel.org
Subject: [PATCH v6 1/8] PCI: Recognize Thunderbolt devices
Date: Sun, 12 Feb 2017 17:07:45 +0100	[thread overview]
Message-ID: <fee5154ef747e34684f316a9edf1ec7f635fd0f8.1486913733.git.lukas@wunner.de> (raw)
In-Reply-To: <cover.1486913733.git.lukas@wunner.de>

Detect on device probe whether a PCI device is part of a Thunderbolt
daisy chain (i.e. it is either part of a Thunderbolt controller or part
of the hierarchy below a Thunderbolt controller).

Intel uses a Vendor-Specific Extended Capability (VSEC) with ID 0x1234
on devices belonging to a Thunderbolt controller.  Detect presence of
this VSEC on the device itself or on one of its parents and cache it in
a newly added is_thunderbolt bit in struct pci_dev.

The necessity arises from the following:

* To power off Thunderbolt controllers on Macs when nothing is plugged
  in, we need to allow runtime PM on their PCIe ports in
  pci_bridge_d3_possible().  For this we need a way to recognize them.

* Dual GPU MacBook Pros introduced 2011+ can no longer switch external
  DisplayPort ports between GPUs.  (They're no longer just used for DP
  but have become combined DP/Thunderbolt ports.)  The driver to switch
  the ports, drivers/platform/x86/apple-gmux.c, needs to detect presence
  of a Thunderbolt controller and, if found, keep external ports
  permanently switched to the discrete GPU.

* If an external Thunderbolt GPU is connected to a dual GPU laptop (Mac
  or not), that GPU is currently registered with vga_switcheroo even
  though it can neither drive the laptop's panel nor be powered off by
  the platform.  To vga_switcheroo it will appear as if two discrete
  GPUs are present.  As a result, when the external GPU is runtime
  suspended, vga_switcheroo will cut power to the internal discrete GPU
  which may not be runtime suspended at all at this moment.  The
  solution is to not register external GPUs with vga_switcheroo, which
  necessitates a way to recognize if they're part of a Thunderbolt daisy
  chain.

Cc: Andreas Noever <andreas.noever@gmail.com>
Cc: Tomas Winkler <tomas.winkler@intel.com>
Cc: Amir Levy <amir.jer.levy@intel.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
---
 drivers/pci/pci.h   |  2 ++
 drivers/pci/probe.c | 32 ++++++++++++++++++++++++++++++++
 include/linux/pci.h |  1 +
 3 files changed, 35 insertions(+)

diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index cb17db242f30..45c2b8144911 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -3,6 +3,8 @@
 
 #define PCI_FIND_CAP_TTL	48
 
+#define PCI_VSEC_ID_INTEL_TBT	0x1234	/* Thunderbolt */
+
 extern const unsigned char pcie_link_speed[];
 
 bool pcie_cap_has_lnkctl(const struct pci_dev *dev);
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 204960e70333..2fcbc535786e 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1208,6 +1208,35 @@ void set_pcie_hotplug_bridge(struct pci_dev *pdev)
 		pdev->is_hotplug_bridge = 1;
 }
 
+static void set_pcie_thunderbolt(struct pci_dev *dev)
+{
+	struct pci_dev *parent = dev;
+	int vsec = 0;
+	u32 header;
+
+	/* Is the device part of a Thunderbolt controller? */
+	while ((vsec = pci_find_next_ext_capability(dev, vsec,
+						    PCI_EXT_CAP_ID_VNDR))) {
+		pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
+		if (dev->vendor == PCI_VENDOR_ID_INTEL &&
+		    PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
+			dev->is_thunderbolt = 1;
+			return;
+		}
+	}
+
+	/*
+	 * Is the device attached with Thunderbolt?  Walk upwards and check for
+	 * each encountered bridge if it's part of a Thunderbolt controller.
+	 * Reaching the host bridge means dev is soldered to the mainboard.
+	 */
+	while ((parent = pci_upstream_bridge(parent)))
+		if (parent->is_thunderbolt) {
+			dev->is_thunderbolt = 1;
+			return;
+		}
+}
+
 /**
  * pci_ext_cfg_is_aliased - is ext config space just an alias of std config?
  * @dev: PCI device
@@ -1360,6 +1389,9 @@ int pci_setup_device(struct pci_dev *dev)
 	/* need to have dev->class ready */
 	dev->cfg_size = pci_cfg_space_size(dev);
 
+	/* need to have dev->cfg_size ready */
+	set_pcie_thunderbolt(dev);
+
 	/* "Unknown power state" */
 	dev->current_state = PCI_UNKNOWN;
 
diff --git a/include/linux/pci.h b/include/linux/pci.h
index e2d1a124216a..3c775e8498f1 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -358,6 +358,7 @@ struct pci_dev {
 	unsigned int	is_virtfn:1;
 	unsigned int	reset_fn:1;
 	unsigned int    is_hotplug_bridge:1;
+	unsigned int	is_thunderbolt:1; /* part of Thunderbolt daisy chain */
 	unsigned int    __aer_firmware_first_valid:1;
 	unsigned int	__aer_firmware_first:1;
 	unsigned int	broken_intx_masking:1;
-- 
2.11.0

  parent reply	other threads:[~2017-02-12 16:07 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-12 16:07 [PATCH v6 0/8] Runtime PM for Thunderbolt on Macs Lukas Wunner
2017-02-12 16:07 ` [PATCH v6 4/8] Revert "PM / Runtime: Remove the exported function pm_children_suspended()" Lukas Wunner
2017-02-12 16:07 ` [PATCH v6 7/8] thunderbolt: Power down controller when idle Lukas Wunner
2017-02-12 16:07 ` [PATCH v6 2/8] PCI: Allow runtime PM on Thunderbolt ports Lukas Wunner
2017-02-14 20:38   ` Bjorn Helgaas
2017-02-15 12:13     ` Lukas Wunner
2017-02-17 16:06   ` Bjorn Helgaas
2017-02-12 16:07 ` [PATCH v6 8/8] thunderbolt: Runtime suspend NHI when idle Lukas Wunner
2017-02-12 16:07 ` Lukas Wunner [this message]
2017-02-17 15:29   ` [PATCH v6 1/8] PCI: Recognize Thunderbolt devices Bjorn Helgaas
2017-02-18  9:12     ` Lukas Wunner
2017-02-19  4:27       ` Bjorn Helgaas
2017-02-20 11:49         ` Lukas Wunner
2017-02-21 22:55           ` Bjorn Helgaas
2017-02-12 16:07 ` [PATCH v6 3/8] PCI: pciehp: Reinstate runtime PM on Thunderbolt hotplug ports Lukas Wunner
2017-02-14 22:59   ` Bjorn Helgaas
2017-02-18  9:25     ` Lukas Wunner
2017-02-19  0:16       ` Bjorn Helgaas
2017-02-20 12:04         ` Lukas Wunner
2017-02-12 16:07 ` [PATCH v6 5/8] PM: Make requirements of dev_pm_domain_set() more precise Lukas Wunner
2017-02-12 16:07 ` [PATCH v6 6/8] PM / sleep: Define constant for direct_complete Lukas Wunner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fee5154ef747e34684f316a9edf1ec7f635fd0f8.1486913733.git.lukas@wunner.de \
    --to=lukas@wunner.de \
    --cc=andreas.noever@gmail.com \
    --cc=helgaas@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).