Thunderbolt support for Apple MBP
Subsystems (2)
Andreas Noever
maintainerv2 cover Thunderbolt support for Apple MBP
Hi
This is version 2 of my Thunderbolt driver for Apple hardware (see [1] for v1). The short story is that Apple decided to implement hotplug support in a driver instead of the firmware. The firmware will only initialize coldplugged devices. Hotplugged devices do not work.
This series adds support for single device hotplug (no chaining) for the Cactus Ridge C4 Thunderbolt controller (device id 0x1547, present on MacBookPro10,1). As far as I can tell newer generations work quite similar and it should be possible to add support for more controllers with some work.
Changes from v1:
- suspend/hibernate support (yeah)
- general cleanups and refactoring
Quoted 1 line
From my perspective the driver is working quite well and is ready for merging
(maybe someone can tell me whom I should ask to get a review?). I have taken some care to not stomp on tunnels setup by the firmware for coldplugged devices. If this works as intendend then there should be no regressions (coldplugged devices continue to work and simple hotplugged devices will start working). In particular the Apple Ethernet adapter works very well (and this is probably the most used TB device out there).
Some notes:
- The patch requires Matthew's acpi_osi fix [2]
- @pci, Bjorn: I hope patch 12 is acceptable. If there is a better way to do this please let me know.
- Thunderbolt displays: I have a pretty good idea on how they should work, but I did not yet write any code to support them. Also judging by the replies I
got for v1 of the patch it seems that there are some graphics driver problems even with coldplugged tb displays (at least on MBP models with discrete graphics cards).
- Hibernate gotcha: The amount of available memory reported by the firmware depends on whether a tb device is connected. This makes Linux refuse to
restore the saved image. This has nothing to do with this driver, but you might run into it during testing.
Thanks,
Andreas
Andreas Noever (14):
thunderbolt: Add initial cactus ridge NHI support
thunderbolt: Add control channel interface
thunderbolt: Setup control channel
thunderbolt: Add tb_regs.h
thunderbolt: Initialize root switch and ports
thunderbolt: Add thunderbolt capability handling
thunderbolt: Enable plug events
thunderbolt: Scan for downstream switches
thunderbolt: Handle hotplug events
thunderbolt: Add path setup code.
thunderbolt: Add support for simple pci tunnels.
pci: Suspend/resume support for appel thunderbolt
thunderbolt: Read switch uid from EEPROM
thunderbolt: Add suspend/hibernate support
drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/pci/pcie/portdrv_pci.c | 117 +++++++ drivers/thunderbolt/Kconfig | 12 + drivers/thunderbolt/Makefile | 3 + drivers/thunderbolt/cap.c | 116 +++++++ drivers/thunderbolt/ctl.c | 731 +++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/ctl.h | 75 ++++ drivers/thunderbolt/eeprom.c | 189 ++++++++++ drivers/thunderbolt/nhi.c | 671 +++++++++++++++++++++++++++++++++++ drivers/thunderbolt/nhi.h | 114 ++++++ drivers/thunderbolt/nhi_regs.h | 101 ++++++ drivers/thunderbolt/path.c | 215 ++++++++++++ drivers/thunderbolt/switch.c | 492 ++++++++++++++++++++++++++ drivers/thunderbolt/tb.c | 431 +++++++++++++++++++++++ drivers/thunderbolt/tb.h | 266 ++++++++++++++ drivers/thunderbolt/tb_regs.h | 213 ++++++++++++ drivers/thunderbolt/tunnel_pci.c | 232 +++++++++++++ drivers/thunderbolt/tunnel_pci.h | 30 ++ 19 files changed, 4011 insertions(+) create mode 100644 drivers/thunderbolt/Kconfig create mode 100644 drivers/thunderbolt/Makefile create mode 100644 drivers/thunderbolt/cap.c create mode 100644 drivers/thunderbolt/ctl.c create mode 100644 drivers/thunderbolt/ctl.h create mode 100644 drivers/thunderbolt/eeprom.c create mode 100644 drivers/thunderbolt/nhi.c create mode 100644 drivers/thunderbolt/nhi.h create mode 100644 drivers/thunderbolt/nhi_regs.h create mode 100644 drivers/thunderbolt/path.c create mode 100644 drivers/thunderbolt/switch.c create mode 100644 drivers/thunderbolt/tb.c create mode 100644 drivers/thunderbolt/tb.h create mode 100644 drivers/thunderbolt/tb_regs.h create mode 100644 drivers/thunderbolt/tunnel_pci.c create mode 100644 drivers/thunderbolt/tunnel_pci.h
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 1/14 thunderbolt: Add initial cactus ridge NHI support
Thunderbolt hotplug is supposed to be handled by the firmware. But Apple decided to implement thunderbolt at the operating system level. The firmare only initializes thunderbolt devices that are present at boot time. This driver enables hotplug of thunderbolt of non-chained thunderbolt devices on Apple systems with a cactus ridge 4C (2 ports) controller (MacBookPro10,1).
This first patch adds the Kconfig file as well the parts of the driver which talk directly to the hardware (that is pci device setup, interrupt handling and RX/TX ring management).
--- drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/thunderbolt/Kconfig | 12 + drivers/thunderbolt/Makefile | 3 + drivers/thunderbolt/nhi.c | 625 +++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/nhi.h | 114 ++++++++ drivers/thunderbolt/nhi_regs.h | 101 +++++++ 7 files changed, 858 insertions(+) create mode 100644 drivers/thunderbolt/Kconfig create mode 100644 drivers/thunderbolt/Makefile create mode 100644 drivers/thunderbolt/nhi.c create mode 100644 drivers/thunderbolt/nhi.h create mode 100644 drivers/thunderbolt/nhi_regs.h
drivers/Kconfig +2 -0
@@ -174,4 +174,6 @@ source "drivers/powercap/Kconfig" source "drivers/mcb/Kconfig" +source "drivers/thunderbolt/Kconfig" + endmenu
drivers/Makefile +1 -0
@@ -157,3 +157,4 @@ obj-$(CONFIG_NTB) += ntb/ obj-$(CONFIG_FMC) += fmc/ obj-$(CONFIG_POWERCAP) += powercap/ obj-$(CONFIG_MCB) += mcb/ +obj-$(CONFIG_THUNDERBOLT) += thunderbolt/
drivers/thunderbolt/Kconfig new file +12 -0
@@ -0,0 +1,12 @@ +menuconfig THUNDERBOLT + tristate "Thunderbolt support for Apple devices" + default no + help + Cactus Ridge Thunderbolt Controller driver + This driver is required if you want to hotplug Thunderbolt devices on + Apple hardware. + + Device chaining is currently not supported. + + To compile this driver a module, choose M here. The module will be + called thunderbolt.
drivers/thunderbolt/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-${CONFIG_THUNDERBOLT} := thunderbolt.o +thunderbolt-objs := nhi.o +
drivers/thunderbolt/nhi.c new file +625 -0
625 lines not on this page · raw message
drivers/thunderbolt/nhi.h new file +114 -0
114 lines not on this page · raw message
drivers/thunderbolt/nhi_regs.h new file +101 -0
101 lines not on this page · raw message
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 2/14 thunderbolt: Add control channel interface
In reply to Andreas NoeverThunderbolt devices are configured by reading/writing into their configuration space (similar to pci). This is done by sending packets through the NHI (native host interface) onto the control channel.
This patch handles the low level packet based protocol and exposes higher level operations like tb_cfg_read/tb_cfg_write.
--- drivers/thunderbolt/Makefile | 2 +- drivers/thunderbolt/ctl.c | 731 +++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/ctl.h | 75 +++++ 3 files changed, 807 insertions(+), 1 deletion(-) create mode 100644 drivers/thunderbolt/ctl.c create mode 100644 drivers/thunderbolt/ctl.h
drivers/thunderbolt/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-${CONFIG_THUNDERBOLT} := thunderbolt.o -thunderbolt-objs := nhi.o +thunderbolt-objs := nhi.o ctl.o
drivers/thunderbolt/ctl.c new file +731 -0
731 lines not on this page · raw message
drivers/thunderbolt/ctl.h new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 3/14 thunderbolt: Setup control channel
In reply to Andreas NoeverAdd struct tb which will contain our view of the thunderbolt bus. For now it just contains a pointer to the control channel and a workqueue for hotplug events.
Add thunderbolt_alloc_and_start() and thunderbolt_shutdown_and_free() which are responsible for setup and teardown of struct tb.
--- drivers/thunderbolt/Makefile | 2 +- drivers/thunderbolt/nhi.c | 18 +++++- drivers/thunderbolt/tb.c | 134 +++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.h | 35 +++++++++++ 4 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 drivers/thunderbolt/tb.c create mode 100644 drivers/thunderbolt/tb.h
drivers/thunderbolt/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-${CONFIG_THUNDERBOLT} := thunderbolt.o -thunderbolt-objs := nhi.o ctl.o +thunderbolt-objs := nhi.o ctl.o tb.o
drivers/thunderbolt/nhi.c +16 -2
@@ -16,6 +16,7 @@#include "nhi.h" #include "nhi_regs.h" +#include "tb.h" #define RING_TYPE (ring) ((ring)->is_tx ? "TX ring" : "RX ring")
@@ -517,6 +518,7 @@ static void nhi_shutdown(struct tb_nhi *nhi)static int nhi_probe (struct pci_dev *pdev,const struct pci_device_id *id) {struct tb_nhi *nhi; +struct tb *tb;int res; res =pcim_enable_device (pdev);
@@ -575,14 +577,26 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)/* magic value - clock related? */ iowrite32 (3906250 /10000 , nhi->iobase +0x38c00 ); -pci_set_drvdata (pdev, nhi); +dev_info (&nhi->pdev->dev,"NHI initialized, starting thunderbolt\n" ); + tb =thunderbolt_alloc_and_start (nhi); +if (!tb) { +/* +* At this point the RX/TX rings might already have been +* activated. Do a proper shutdown. +*/ +nhi_shutdown (nhi); +return -EIO ; + } +pci_set_drvdata (pdev, tb);return 0 ; }static void nhi_remove (struct pci_dev *pdev) { -struct tb_nhi *nhi =pci_get_drvdata (pdev); +struct tb *tb =pci_get_drvdata (pdev); +struct tb_nhi *nhi = tb->nhi; +thunderbolt_shutdown_and_free (tb);nhi_shutdown (nhi); }
drivers/thunderbolt/tb.c new file +134 -0
134 lines not on this page · raw message
drivers/thunderbolt/tb.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* Thunderbolt Cactus Ridge driver - bus logic (NHI independent) +* +* Copyright (c) 2014 Andreas Noever <[email protected]> +*/ + +#ifndef TB_H_ +#define TB_H_ + +#include "ctl.h" + +/** +* struct tb - main thunderbolt bus structure +*/ +struct tb { +struct mutex lock;/* +* Big lock. Must be held when accessing cfg or +* any struct tb_switch / struct tb_port. +*/ +struct tb_nhi *nhi; +struct tb_ctl *ctl; +struct workqueue_struct *wq;/* ordered workqueue for plug events */ +bool hotplug_active;/* +* tb_handle_hotplug will stop progressing plug +* events and exit if this is not seet (it needs to +* acquire the lock one more time). Used to drain +* wq after cfg has been paused. +*/ + +}; + +struct tb *thunderbolt_alloc_and_start (struct tb_nhi *nhi); +void thunderbolt_shutdown_and_free (struct tb *tb); + +#endif
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 4/14 thunderbolt: Add tb_regs.h
In reply to Andreas NoeverEvery thunderbolt device consists (logically) of a switch with multiple ports. Every port contains up to four config regions (HOPS, PORT, SWITCH, COUNTERS) which are used to configure the device.
The tb_regs.h file contains all known registers and capabilities from these config regions.
--- drivers/thunderbolt/tb.c | 5 + drivers/thunderbolt/tb_regs.h | 213 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 drivers/thunderbolt/tb_regs.h
drivers/thunderbolt/tb.c +5 -0
@@ -9,6 +9,7 @@#include <linux/delay.h> #include "tb.h" +#include "tb_regs.h" /* hotplug handling */
@@ -100,6 +101,10 @@ struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi) {struct tb *tb; +BUILD_BUG_ON (sizeof (struct tb_regs_switch_header ) !=5 *4 ); +BUILD_BUG_ON (sizeof (struct tb_regs_port_header ) !=8 *4 ); +BUILD_BUG_ON (sizeof (struct tb_regs_hop ) !=2 *4 ); + tb =kzalloc (sizeof (*tb),GFP_KERNEL );if (!tb)return NULL ;
drivers/thunderbolt/tb_regs.h new file +213 -0
213 lines not on this page · raw message
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 5/14 thunderbolt: Initialize root switch and ports
In reply to Andreas NoeverThis patch adds the structures tb_switch and tb_port as well as code to initialize the root switch.
--- drivers/thunderbolt/Makefile | 2 +- drivers/thunderbolt/switch.c | 186 +++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.c | 8 ++ drivers/thunderbolt/tb.h | 138 ++++++++++++++++++++++++++++++++ 4 files changed, 333 insertions(+), 1 deletion(-) create mode 100644 drivers/thunderbolt/switch.c
drivers/thunderbolt/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-${CONFIG_THUNDERBOLT} := thunderbolt.o -thunderbolt-objs := nhi.o ctl.o tb.o +thunderbolt-objs := nhi.o ctl.o tb.o switch.o
drivers/thunderbolt/switch.c new file +186 -0
186 lines not on this page · raw message
drivers/thunderbolt/tb.c +8 -0
@@ -71,6 +71,10 @@ void thunderbolt_shutdown_and_free(struct tb *tb) {mutex_lock (&tb->lock); +if (tb->root_switch) +tb_switch_free (tb->root_switch); + tb->root_switch =NULL ; +if (tb->ctl) {tb_ctl_stop (tb->ctl);tb_ctl_free (tb->ctl);
@@ -126,6 +130,10 @@ struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi)*/ tb_ctl_start (tb->ctl); + tb->root_switch =tb_switch_alloc (tb,0 ); +if (!tb->root_switch) + goto err_locked; +/* Allow tb_handle_hotplug to progress events */ tb->hotplug_active = true;mutex_unlock (&tb->lock);
drivers/thunderbolt/tb.h +138 -0
160 lines not on this page · raw message
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 6/14 thunderbolt: Add thunderbolt capability handling
In reply to Andreas NoeverThunderbolt config areas contain capability lists similar to those found on pci devices. This patch introduces a tb_find_cap utility method to search for capabilities.
--- drivers/thunderbolt/Makefile | 2 +- drivers/thunderbolt/cap.c | 116 +++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.h | 2 + 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 drivers/thunderbolt/cap.c
drivers/thunderbolt/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-${CONFIG_THUNDERBOLT} := thunderbolt.o -thunderbolt-objs := nhi.o ctl.o tb.o switch.o +thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o
drivers/thunderbolt/cap.c new file +116 -0
116 lines not on this page · raw message
drivers/thunderbolt/tb.h +2 -0
@@ -159,6 +159,8 @@ void thunderbolt_shutdown_and_free(struct tb *tb);struct tb_switch *tb_switch_alloc (struct tb *tb,u64 route);void tb_switch_free (struct tb_switch *sw); +int tb_find_cap (struct tb_port *port,enum tb_cfg_space space,u32 value); +static inline int tb_route_length (u64 route) {
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 7/14 thunderbolt: Enable plug events
In reply to Andreas NoeverThunderbolt switches have a plug events capability. This patch adds the tb_plug_events_active method and uses it to activate plug events during switch allocation.
--- drivers/thunderbolt/switch.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.h | 1 + 2 files changed, 47 insertions(+)
drivers/thunderbolt/switch.c +46 -0
@@ -99,6 +99,39 @@ static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) }/** +* tb_plug_events_active() - enable/disable plug events on a switch +* +* Also configures a sane plug_events_delay of 255ms. +* +* Return: Returns 0 on success or an error code on failure. +*/ +static int tb_plug_events_active (struct tb_switch *sw,bool active) +{ +u32 data; +int res; + + sw->config.plug_events_delay =0xff ; + res =tb_sw_write (sw, ((u32 *) &sw->config) +4 ,TB_CFG_SWITCH ,4 ,1 ); +if (res) +return res; + + res =tb_sw_read (sw, &data,TB_CFG_SWITCH , sw->cap_plug_events +1 ,1 ); +if (res) +return res; + +if (active) { + data = data &0xFFFFFF83 ; +if (sw->config.device_id ==0x1547 ) + data |=4 ; + }else { + data = data |0x7c ; + } +return tb_sw_write (sw, &data,TB_CFG_SWITCH , + sw->cap_plug_events +1 ,1 ); +} + + +/** * tb_switch_free() - free a tb_switch and all downstream switches */ void tb_switch_free (struct tb_switch *sw)
@@ -113,6 +146,8 @@ void tb_switch_free(struct tb_switch *sw) sw->ports[i].remote =NULL ; } +tb_plug_events_active (sw, false); +kfree (sw->ports);kfree (sw); }
@@ -125,6 +160,7 @@ void tb_switch_free(struct tb_switch *sw)struct tb_switch *tb_switch_alloc (struct tb *tb,u64 route) {int i; +int cap;struct tb_switch *sw;int upstream_port =tb_cfg_get_upstream_port (tb->ctl, route);if (upstream_port <0 )
@@ -177,6 +213,16 @@ struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route)/* TODO: I2C, IECS, EEPROM, link controller */ + cap =tb_find_cap (&sw->ports[0 ],TB_CFG_SWITCH ,TB_CAP_PLUG_EVENTS ); +if (cap <0 ) { +tb_sw_warn (sw,"cannot find TB_CAP_PLUG_EVENTS aborting\n" ); + goto err; + } + sw->cap_plug_events = cap; + +if (tb_plug_events_active (sw, true)) + goto err; +return sw; err:kfree (sw->ports);
drivers/thunderbolt/tb.h +1 -0
@@ -19,6 +19,7 @@ struct tb_switch {struct tb_regs_switch_header config;struct tb_port *ports;struct tb *tb; +int cap_plug_events;/* offset, zero if not found */ };/**
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 8/14 thunderbolt: Scan for downstream switches
In reply to Andreas NoeverAdd utility methods tb_port_state and tb_wait_for_port. Add tb_scan_switch which recursively checks for downstream switches.
--- drivers/thunderbolt/switch.c | 97 ++++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.c | 44 ++++++++++++++++++++ drivers/thunderbolt/tb.h | 16 ++++++++ 3 files changed, 157 insertions(+)
drivers/thunderbolt/switch.c +97 -0
@@ -53,6 +53,92 @@ static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port) }/** +* tb_port_state() - get connectedness state of a port +* +* The port must have a TB_CAP_PHY (i.e. it should be a real port). +* +* Return: Returns an enum tb_port_state on success or an error code on failure. +*/ +static int tb_port_state (struct tb_port *port) +{ +struct tb_cap_phy phy; +int res; +if (port->cap_phy ==0 ) { +tb_port_WARN (port,"does not have a PHY\n" ); +return -EINVAL ; + } + res =tb_port_read (port, &phy,TB_CFG_PORT , port->cap_phy,2 ); +if (res) +return res; +return phy.state; +} + +/** +* tb_wait_for_port() - wait for a port to become ready +* +* Wait up to 1 second for a port to reach state TB_PORT_UP. If +* wait_if_unplugged is set then we also wait if the port is in state +* TB_PORT_UNPLUGGED (it takes a while for the device to be registered after +* switch resume). Otherwise we only wait if a device is registered but the link +* has not yet been established. +* +* Return: Returns an error code on failure. Returns 0 if the port is not +* connected or failed to reach state TB_PORT_UP within one second. Returns 1 +* if the port is connected and in state TB_PORT_UP. +*/ +int tb_wait_for_port (struct tb_port *port,bool wait_if_unplugged) +{ +int retries =10 ; +int state; +if (!port->cap_phy) { +tb_port_WARN (port,"does not have PHY\n" ); +return -EINVAL ; + } +if (tb_is_upstream_port (port)) { +tb_port_WARN (port,"is the upstream port\n" ); +return -EINVAL ; + } + +while (retries--) { + state =tb_port_state (port); +if (state <0 ) +return state; +if (state ==TB_PORT_DISABLED ) { +tb_port_info (port,"is disabled (state: 0)\n" ); +return 0 ; + } +if (state ==TB_PORT_UNPLUGGED ) { +if (wait_if_unplugged) { +/* used during resume */ +tb_port_info (port, +"is unplugged (state: 7), retrying...\n" ); +msleep (100 ); +continue ; + } +tb_port_info (port,"is unplugged (state: 7)\n" ); +return 0 ; + } +if (state ==TB_PORT_UP ) { +tb_port_info (port, +"is connected, link is up (state: 2)\n" ); +return 1 ; + } + +/* +* After plug-in the state is TB_PORT_CONNECTING. Give it some +* time. +*/ +tb_port_info (port, +"is connected, link is not up (state: %d), retrying...\n" , + state); +msleep (100 ); + } +tb_port_warn (port, +"failed to reach state TB_PORT_UP. Ignoring port...\n" ); +return 0 ; +} + +/** * tb_init_port() - initialize a port * * This is a helper method for tb_switch_alloc. Does not check or initialize
@@ -63,6 +149,7 @@ static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)static int tb_init_port (struct tb_switch *sw,u8 port_nr) {int res; +int cap;struct tb_port *port = &sw->ports[port_nr]; port->sw = sw; port->port = port_nr;
@@ -71,6 +158,16 @@ static int tb_init_port(struct tb_switch *sw, u8 port_nr)if (res)return res; +/* Port 0 is the switch itself and has no PHY. */ +if (port->config.type ==TB_TYPE_PORT && port_nr !=0 ) { + cap =tb_find_cap (port,TB_CFG_PORT ,TB_CAP_PHY ); + +if (cap >0 ) + port->cap_phy = cap; +else +tb_port_WARN (port,"non switch port without a PHY\n" ); + } +tb_dump_port (sw->tb, &port->config);/* TODO: Read dual link port, DP port and more from EEPROM. */
drivers/thunderbolt/tb.c +44 -0
56 lines not on this page · raw message
drivers/thunderbolt/tb.h +16 -0
@@ -29,6 +29,7 @@ struct tb_port {struct tb_regs_port_header config;struct tb_switch *sw;struct tb_port *remote;/* remote port, NULL if not connected */ +int cap_phy;/* offset, zero if not found */ u8 port;/* port number on switch */ };
@@ -160,6 +161,8 @@ void thunderbolt_shutdown_and_free(struct tb *tb);struct tb_switch *tb_switch_alloc (struct tb *tb,u64 route);void tb_switch_free (struct tb_switch *sw); +int tb_wait_for_port (struct tb_port *port,bool wait_if_unplugged); +int tb_find_cap (struct tb_port *port,enum tb_cfg_space space,u32 value);
@@ -173,4 +176,17 @@ static inline bool tb_is_upstream_port(struct tb_port *port) return port == tb_upstream_port(port->sw); } +/** +* tb_downstream_route() - get route to downstream switch +* +* Port must not be the upstream port (otherwise a loop is created). +* +* Return: Returns a route to the switch behind @port. +*/ +static inline u64 tb_downstream_route (struct tb_port *port) +{ +return tb_route (port->sw) + | ((u64 ) port->port << (port->sw->config.depth *8 )); +} +#endif
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 9/14 thunderbolt: Handle hotplug events
In reply to Andreas NoeverWe reveive a plug event callback whenever a thunderbolt device is added or removed. This patch fills in the tb_handle_hotplug method and starts reacting to these events by adding/removing switches from the hierarchy.
--- drivers/thunderbolt/switch.c | 42 +++++++++++++++++++++++++++++++++++++++- drivers/thunderbolt/tb.c | 46 +++++++++++++++++++++++++++++++++++++++++++- drivers/thunderbolt/tb.h | 3 +++ 3 files changed, 89 insertions(+), 2 deletions(-)
drivers/thunderbolt/switch.c +41 -1
@@ -195,6 +195,24 @@ static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) sw->__unknown1, sw->__unknown4); } +struct tb_switch *get_switch_at_route (struct tb_switch *sw,u64 route) +{ +u8 next_port = route;/* +* Routes use a stride of 8 bits, +* eventhough a port index has 6 bits at most. +* */ +if (route ==0 ) +return sw; +if (next_port > sw->config.max_port_number) +return 0 ; +if (tb_is_upstream_port (&sw->ports[next_port])) +return 0 ; +if (!sw->ports[next_port].remote) +return 0 ; +return get_switch_at_route (sw->ports[next_port].remote->sw, + route >>TB_ROUTE_SHIFT ); +} +/** * tb_plug_events_active() - enable/disable plug events on a switch *
@@ -243,7 +261,8 @@ void tb_switch_free(struct tb_switch *sw) sw->ports[i].remote =NULL ; } -tb_plug_events_active (sw, false); +if (!sw->is_unplugged) +tb_plug_events_active (sw, false);kfree (sw->ports);kfree (sw);
@@ -327,3 +346,24 @@ err:return NULL ; } +/** +* tb_sw_set_unpplugged() - set is_unplugged on switch and downstream switches +*/ +void tb_sw_set_unpplugged (struct tb_switch *sw) +{ +int i; +if (sw == sw->tb->root_switch) { +tb_sw_WARN (sw,"cannot unplug root switch\n" ); +return ; + } +if (sw->is_unplugged) { +tb_sw_WARN (sw,"is_unplugged already set\n" ); +return ; + } + sw->is_unplugged = true; +for (i =0 ; i <= sw->config.max_port_number; i++) { +if (!tb_is_upstream_port (&sw->ports[i]) && sw->ports[i].remote) +tb_sw_set_unpplugged (sw->ports[i].remote->sw); + } +} +
drivers/thunderbolt/tb.c +45 -1
56 lines not on this page · raw message
drivers/thunderbolt/tb.h +3 -0
@@ -20,6 +20,7 @@ struct tb_switch {struct tb_port *ports;struct tb *tb;int cap_plug_events;/* offset, zero if not found */ +bool is_unplugged;/* unplugged, will go away */ };/**
@@ -160,6 +161,8 @@ void thunderbolt_shutdown_and_free(struct tb *tb);struct tb_switch *tb_switch_alloc (struct tb *tb,u64 route);void tb_switch_free (struct tb_switch *sw); +void tb_sw_set_unpplugged (struct tb_switch *sw); +struct tb_switch *get_switch_at_route (struct tb_switch *sw,u64 route);int tb_wait_for_port (struct tb_port *port,bool wait_if_unplugged);
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 10/14 thunderbolt: Add path setup code.
In reply to Andreas NoeverA thunderbolt path is a unidirectional channel between two thunderbolt ports. Two such paths are needed to establish a pci tunnel.
This patch introduces struct tb_path as well as a set of tb_path_* methods which are used do activate & deactive paths.
--- drivers/thunderbolt/Makefile | 2 +- drivers/thunderbolt/path.c | 215 +++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/switch.c | 34 +++++++ drivers/thunderbolt/tb.h | 62 +++++++++++++ 4 files changed, 312 insertions(+), 1 deletion(-) create mode 100644 drivers/thunderbolt/path.c
drivers/thunderbolt/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-${CONFIG_THUNDERBOLT} := thunderbolt.o -thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o +thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o
drivers/thunderbolt/path.c new file +215 -0
215 lines not on this page · raw message
drivers/thunderbolt/switch.c +34 -0
@@ -139,6 +139,40 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged) }/** +* tb_port_add_nfc_credits() - add/remove non flow controlled credits to port +* +* Change the number of NFC credits allocated to @port by @credits. To remove +* NFC credits pass a negative amount of credits. +* +* Return: Returns 0 on success or an error code on failure. +*/ +int tb_port_add_nfc_credits (struct tb_port *port,int credits) +{ +if (credits ==0 ) +return 0 ; +tb_port_info (port, +"adding %#x NFC credits (%#x -> %#x)" , + credits, + port->config.nfc_credits, + port->config.nfc_credits + credits); + port->config.nfc_credits += credits; +return tb_port_write (port, &port->config.nfc_credits, +TB_CFG_PORT ,4 ,1 ); +} + +/** +* tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER +* +* Return: Returns 0 on success or an error code on failure. +*/ +int tb_port_clear_counter (struct tb_port *port,int counter) +{ +u32 zero[3 ] = {0 ,0 ,0 }; +tb_port_info (port,"clearing counter %d\n" , counter); +return tb_port_write (port, zero,TB_CFG_COUNTERS ,3 * counter,3 ); +} + +/** * tb_init_port() - initialize a port * * This is a helper method for tb_switch_alloc. Does not check or initialize
drivers/thunderbolt/tb.h +62 -0
77 lines not on this page · raw message
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 11/14 thunderbolt: Add support for simple pci tunnels.
In reply to Andreas NoeverA pci downstream and pci upstream port can be connected through a tunnel. To establish the tunnel we have to setup two unidirectional paths between the two ports.
Right now we only support paths with two hops (i.e. no chaining) and at most one pci device per thunderbolt device.
--- drivers/thunderbolt/Makefile | 2 +- drivers/thunderbolt/tb.c | 135 +++++++++++++++++++++++ drivers/thunderbolt/tb.h | 1 + drivers/thunderbolt/tunnel_pci.c | 232 +++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tunnel_pci.h | 30 +++++ 5 files changed, 399 insertions(+), 1 deletion(-) create mode 100644 drivers/thunderbolt/tunnel_pci.c create mode 100644 drivers/thunderbolt/tunnel_pci.h
drivers/thunderbolt/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-${CONFIG_THUNDERBOLT} := thunderbolt.o -thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o +thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o tunnel_pci.o
drivers/thunderbolt/tb.c +135 -0
179 lines not on this page · raw message
drivers/thunderbolt/tb.h +1 -0
@@ -100,6 +100,7 @@ struct tb {struct tb_ctl *ctl;struct workqueue_struct *wq;/* ordered workqueue for plug events */ struct tb_switch *root_switch; +struct list_head tunnel_list;/* list of active PCIe tunnels */ bool hotplug_active;/* * tb_handle_hotplug will stop progressing plug * events and exit if this is not seet (it needs to
drivers/thunderbolt/tunnel_pci.c new file +232 -0
232 lines not on this page · raw message
drivers/thunderbolt/tunnel_pci.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* Thunderbolt Cactus Ridge driver - PCIe tunnel +* +* Copyright (c) 2014 Andreas Noever <[email protected]> +*/ + +#ifndef TB_PCI_H_ +#define TB_PCI_H_ + +#include "tb.h" + +struct tb_pci_tunnel { +struct tb *tb; +struct tb_port *up_port; +struct tb_port *down_port; +struct tb_path *path_to_up; +struct tb_path *path_to_down; +struct list_head list; +}; + +struct tb_pci_tunnel *tb_pci_alloc (struct tb *tb,struct tb_port *up, +struct tb_port *down); +void tb_pci_free (struct tb_pci_tunnel *tunnel); +int tb_pci_activate (struct tb_pci_tunnel *tunnel); +int tb_pci_restart (struct tb_pci_tunnel *tunnel); +void tb_pci_deactivate (struct tb_pci_tunnel *tunnel); +bool tb_pci_is_invalid (struct tb_pci_tunnel *tunnel); + +#endif +
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 12/14 pci: Suspend/resume support for appel thunderbolt
In reply to Andreas NoeverThis 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).
--- drivers/pci/pcie/portdrv_pci.c | 117 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+)
drivers/pci/pcie/portdrv_pci.c +117 -0
@@ -17,6 +17,9 @@#include <linux/aer.h> #include <linux/dmi.h> #include <linux/pci-aspm.h> +#include <linux/delay.h> +#include <linux/dmi.h> +#include <linux/acpi.h> #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,
Signature · 1 line
1.9.2
Bjorn Helgaas
maintainerRe: [PATCH v2 12/14] pci: Suspend/resume support for appel thunderbolt
[+cc Rafael]
Quoted 74 lines · Andreas Noever
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 theresume_noirqphase of the downstream bridges of the controller (which lead into the thunderbolt tunnels).Signed-off-by Andreas Noever <[email protected]>--- drivers/pci/pcie/portdrv_pci.c | 117 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+)drivers/pci/pcie/portdrv_pci.c +35
-0@@ -17,6 +17,9 @@#include <linux/aer.h> #include <linux/dmi.h> #include <linux/pci-aspm.h> +#include <linux/delay.h> +#include <linux/dmi.h> +#include <linux/acpi.h> #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)
drivers/pci/pcie/portdrv_pci.c static int pcie_portdrv_restore_config(struct pci_dev *dev) · line 126
Quoted 10 lines
drivers/pci/pcie/portdrv_pci.c +10
-0+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 ;
Hmm, this is pretty ugly because the ACPI names are up to the BIOS writer so we can't really rely on them.
You could put this sort of thing in a quirk, but I wouldn't be happy about putting it in a generic place like this. Same with the magic sequence below.
Maybe a quirk that sets function pointers that we could call here?
Quoted 93 lines
drivers/pci/pcie/portdrv_pci.c +72
-0+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,Signature · 1 line
1.9.2
Andreas Noever
maintainerRe: [PATCH v2 12/14] pci: Suspend/resume support for appel thunderbolt
Quoted 82 lines · Bjorn Helgaas
[+cc Rafael]
Quoted 79 lines · Andreas Noever
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 theresume_noirqphase of the downstream bridges of the controller (which lead into the thunderbolt tunnels).Signed-off-by Andreas Noever <[email protected]>--- drivers/pci/pcie/portdrv_pci.c | 117 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+)drivers/pci/pcie/portdrv_pci.c +40
-0@@ -17,6 +17,9 @@#include <linux/aer.h> #include <linux/dmi.h> #include <linux/pci-aspm.h> +#include <linux/delay.h> +#include <linux/dmi.h> +#include <linux/acpi.h> #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.
Quoted 8 lines
drivers/pci/pcie/portdrv_pci.c static int pcie_portdrv_restore_config(struct pci_dev *dev) · line 126
Quoted 5 lines
drivers/pci/pcie/portdrv_pci.c +5
-0+*/ +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 ;Hmm, this is pretty ugly because the ACPI names are up to the BIOS writer so we can't really rely on them.
These methods just write to the ACPI GPIO area. I guess I could do the same directly and hardcode the pins, but that would tie it to the specific MacBook model (different models use different GPIOs, the MacPro even has 3 controllers, all using diferent GPIOs). The Apple driver uses these methods as well, so I think that we can assume that they are stable. Worst case is that they go away and the device will not resume (which is what happens now).
There is also an XRPE method (TRPE on newer machine) which is used for runtime power management (setting up out of band hp notifications, power on/off and pcie link retraining). That one is much more complex and I would rather not replicate that code (If I get to the point where I want to implement runtime power management for the controller).
Quoted 5 lines
You could put this sort of thing in a quirk, but I wouldn't be happy about putting it in a generic place like this. Same with the magic sequence below.
Maybe a quirk that sets function pointers that we could call here?
Hm, I did not realize that there where resume/suspend pci quirks. I just checked and which point they are called and at least for resume we are fine. pci_fixup_resume_early gets called just before the dev->pm->resume_noirq callback. But there seems to be no corresponding fixup phase for suspend_late/poweroff_late? I have to check again whether poweroff_late is even necessary. Can we add a quirks section for suspend_late?
Quoted 93 lines
Quoted 93 lines
drivers/pci/pcie/portdrv_pci.c +72
-1+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, --1.9.2
Andreas Noever
maintainerPATCH v2 13/14 thunderbolt: Read switch uid from EEPROM
In reply to Andreas NoeverAdd eeprom access code and read the uid during switch initialization. The UID will be used to check device identity after suspend.
--- drivers/thunderbolt/Makefile | 2 +- drivers/thunderbolt/eeprom.c | 189 +++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/switch.c | 5 ++ drivers/thunderbolt/tb.h | 3 + 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 drivers/thunderbolt/eeprom.c
drivers/thunderbolt/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-${CONFIG_THUNDERBOLT} := thunderbolt.o -thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o tunnel_pci.o +thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o tunnel_pci.o eeprom.o
drivers/thunderbolt/eeprom.c new file +189 -0
189 lines not on this page · raw message
drivers/thunderbolt/switch.c +5 -0
@@ -370,6 +370,11 @@ struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route) } sw->cap_plug_events = cap; +if (tb_eeprom_read_uid (sw, &sw->uid)) +tb_sw_warn (sw,"could not read uid from eeprom\n" ); +else +tb_sw_info (sw,"uid: %#llx\n" , sw->uid); +if (tb_plug_events_active (sw, true)) goto err;
drivers/thunderbolt/tb.h +3 -0
@@ -19,6 +19,7 @@ struct tb_switch {struct tb_regs_switch_header config;struct tb_port *ports;struct tb *tb; +u64 uid;int cap_plug_events;/* offset, zero if not found */ bool is_unplugged;/* unplugged, will go away */ };
@@ -231,6 +232,8 @@ int tb_path_activate(struct tb_path *path);void tb_path_deactivate (struct tb_path *path);bool tb_path_is_invalid (struct tb_path *path); +int tb_eeprom_read_uid (struct tb_switch *sw,u64 *uid); +static inline int tb_route_length (u64 route) {
Signature · 1 line
1.9.2
Andreas Noever
maintainerPATCH v2 14/14 thunderbolt: Add suspend/hibernate support
In reply to Andreas NoeverWe use _noirq since we have to restore the pci tunnels before the pci core wakes the tunneled devices.
--- drivers/thunderbolt/nhi.c | 32 +++++++++++++++++ drivers/thunderbolt/switch.c | 84 ++++++++++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.c | 61 ++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.h | 5 +++ 4 files changed, 182 insertions(+)
drivers/thunderbolt/nhi.c +32 -0
@@ -7,6 +7,7 @@* Copyright (c) 2014 Andreas Noever <[email protected]> */ +#include <linux/pm_runtime.h> #include <linux/slab.h> #include <linux/errno.h> #include <linux/pci.h>
@@ -492,6 +493,22 @@ static irqreturn_t nhi_msi(int irq, void *data)return IRQ_HANDLED ; } +static int nhi_suspend_noirq (struct device *dev) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct tb *tb =pci_get_drvdata (pdev); +thunderbolt_suspend (tb); +return 0 ; +} + +static int nhi_resume_noirq (struct device *dev) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct tb *tb =pci_get_drvdata (pdev); +thunderbolt_resume (tb); +return 0 ; +} +static void nhi_shutdown (struct tb_nhi *nhi) {int i;
@@ -600,6 +617,20 @@ static void nhi_remove(struct pci_dev *pdev)nhi_shutdown (nhi); } +/* +* The tunneled pci bridges are siblings of us. Use _noirq such that the tunnels +* can be reactivate asap after a suspend. +*/ +static const struct dev_pm_ops nhi_pm_ops = { + .suspend_noirq = nhi_suspend_noirq, + .resume_noirq = nhi_resume_noirq, + .freeze_noirq = nhi_suspend_noirq,/* +* we just disable hotplug, the +* pci-tunnels stay alive. +*/ + .restore_noirq = nhi_resume_noirq, +}; +struct pci_device_id nhi_ids[] = { {/*
@@ -621,6 +652,7 @@ static struct pci_driver nhi_driver = { .id_table = nhi_ids, .probe = nhi_probe, .remove = nhi_remove, + .driver.pm = &nhi_pm_ops, };static int __init nhi_init (void )
drivers/thunderbolt/switch.c +84 -0
93 lines not on this page · raw message
drivers/thunderbolt/tb.c +61 -0
70 lines not on this page · raw message
drivers/thunderbolt/tb.h +5 -0
@@ -214,9 +214,14 @@ static inline int tb_port_write(struct tb_port *port, void *buffer,struct tb *thunderbolt_alloc_and_start (struct tb_nhi *nhi);void thunderbolt_shutdown_and_free (struct tb *tb); +void thunderbolt_suspend (struct tb *tb); +void thunderbolt_resume (struct tb *tb);struct tb_switch *tb_switch_alloc (struct tb *tb,u64 route);void tb_switch_free (struct tb_switch *sw); +void tb_switch_suspend (struct tb_switch *sw); +int tb_switch_resume (struct tb_switch *sw); +int tb_switch_reset (struct tb *tb,u64 route);void tb_sw_set_unpplugged (struct tb_switch *sw);struct tb_switch *get_switch_at_route (struct tb_switch *sw,u64 route);
Signature · 1 line
1.9.2
Quoted 24 lines · Andreas Noever
Hi
This is version 2 of my Thunderbolt driver for Apple hardware (see [1] for v1). The short story is that Apple decided to implement hotplug support in a driver instead of the firmware. The firmware will only initialize coldplugged devices. Hotplugged devices do not work.
This series adds support for single device hotplug (no chaining) for the Cactus Ridge C4 Thunderbolt controller (device id
0x1547, present on MacBookPro10,1). As far as I can tell newer generations work quite similar and it should be possible to add support for more controllers with some work.Changes from v1:
- suspend/hibernate support (yeah)
- general cleanups and refactoringQuoted 1 line
From my perspective the driver is working quite well and is ready for merging
(maybe someone can tell me whom I should ask to get a review?). I have taken some care to not stomp on tunnels setup by the firmware for coldplugged devices. If this works as intendend then there should be no regressions (coldplugged devices continue to work and simple hotplugged devices will start working). In particular the Apple Ethernet adapter works very well (and this is probably the most used TB device out there).
Quoted 13 lines
Some notes:
- The patch requires Matthew'sacpi_osifix [2]
- @pci, Bjorn: I hope patch 12 is acceptable. If there is a better way to do this please let me know.
- Thunderbolt displays: I have a pretty good idea on how they should work, but I did not yet write any code to support them. Also judging by the replies I
got for v1 of the patch it seems that there are some graphics driver problems even with coldplugged tb displays (at least on MBP models with discrete graphics cards).
- Hibernate gotcha: The amount of available memory reported by the firmware depends on whether a tb device is connected. This makes Linux refuse to
restore the saved image. This has nothing to do with this driver, but you might run into it during testing.
Very nice work with all of this, I never thought I would ever see this happen for Linux. Of course, I now can't find my thunderbolt ethernet device around here anymore to test this out, I'll go get another one soon...
thanks,
greg k-h