diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index 4d9b4ac0d8346..39302a7ad9ab9 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -32,6 +32,9 @@ #include #include "dwc3-apple-state.h" + +/* drivers/usb/typec/tipd, through symbol_get(): no load dependency on that module. */ +int tipd_port_replug(struct usb_role_switch *sw, u8 seconds); #include "glue.h" #include "io.h" @@ -227,6 +230,11 @@ struct dwc3_apple { bool sleep_phy_wait; /* The start after a sleep was left to the role switch, which may also come too early. */ bool sleep_phy_pending; + /* + * TEST (J714s, T6050 Type-C): a SuperSpeed device was on the host when a sleep stopped it; + * the start after that sleep looks for it once, see dwc3_apple_ss_check(). + */ + bool ss_retry; /* * J714s: a host on a USB4 router's USB3 tunnel, kept suspended in place through the * router's sleep (see dwc3_apple_tunnel_keep()): the PHY's result once taken, the role the @@ -1069,11 +1077,15 @@ static int dwc3_apple_exit(struct dwc3_apple *appledwc); /* A hub that hasn't enumerated by then didn't come back. */ #define DWC3_APPLE_HUB_CHECK_MS 5000 -/* Any way a fixed-hub host came out of a sleep, its hub must be there a little later. */ +/* + * Any way a fixed-hub host came out of a sleep, its hub must be there a little later. TEST + * (J714s): so must a Type-C host's SuperSpeed device, see dwc3_apple_ss_check(). + */ static void dwc3_apple_check_hub_later(struct dwc3_apple *appledwc) { lockdep_assert_held(&appledwc->lock); - if (appledwc->fixed_hub && !appledwc->shutting_down && !appledwc->sleep_dead && + if ((appledwc->fixed_hub || appledwc->ss_retry) && + !appledwc->shutting_down && !appledwc->sleep_dead && !appledwc->sleep_stopped && appledwc->state == DWC3_APPLE_HOST && appledwc->core_active && !appledwc->core_exit_pending && READ_ONCE(appledwc->sleep_phase) == DWC3_APPLE_SLEEP_IDLE) @@ -1213,6 +1225,147 @@ static unsigned long dwc3_apple_hub_wake_ports(struct dwc3_apple *appledwc) return ports; } +/* + * TEST (J714s, T6050 Type-C): the device on the USB2 root port, referenced for the caller, + * whether one is on the SuperSpeed root port, and with @portsc the xHCI's first PORTSC words, + * @nports of them, at most two. Takes the xHCI device's lock and the root hubs'. + */ +static int dwc3_apple_ss_ports(struct dwc3_apple *appledwc, struct usb_device **usb2, bool *ss, + u32 *portsc, unsigned int *nports) +{ + struct platform_device *xhci = appledwc->dwc.xhci; + struct usb_hcd *hcd; + int ret = -ENODEV; + + lockdep_assert_held(&appledwc->lock); + *usb2 = NULL; + *ss = false; + if (nports) + *nports = 0; + if (!xhci) + return -ENODEV; + + device_lock(&xhci->dev); + hcd = xhci->dev.driver ? platform_get_drvdata(xhci) : NULL; + if (hcd && hcd->shared_hcd) { + struct usb_hcd *hcds[] = { hcd, hcd->shared_hcd }; + + for (unsigned int i = 0; i < ARRAY_SIZE(hcds); i++) { + struct usb_device *root = hcds[i]->self.root_hub, *child; + + if (!root) + continue; + usb_lock_device(root); + child = usb_hub_find_child(root, 1); + if (root->speed >= USB_SPEED_SUPER) + *ss = child != NULL; + else if (child && !*usb2) + *usb2 = usb_get_dev(child); + usb_unlock_device(root); + } + /* CAPLENGTH, then HCSPARAMS1's MaxPorts, read as words. */ + if (portsc && nports && hcd->regs) { + void __iomem *op = hcd->regs + (readl(hcd->regs) & 0xff); + + *nports = min(readl(hcd->regs + 4) >> 24, 2U); + for (unsigned int i = 0; i < *nports; i++) + portsc[i] = readl(op + 0x400 + 0x10 * i); + } + ret = 0; + } + device_unlock(&xhci->dev); + + return ret; +} + +/* TEST (J714s): a T6050 Type-C host, whose SuperSpeed device is looked for after a sleep. */ +static bool dwc3_apple_ss_port(struct dwc3_apple *appledwc) +{ + return !appledwc->fixed_hub && appledwc->role_sw && dwc3_apple_sleep_board() && + of_device_is_compatible(appledwc->dev->of_node, "apple,t6050-dwc3"); +} + +/* TEST (J714s): as a sleep stops such a host, whether a SuperSpeed device is on it. */ +static bool dwc3_apple_ss_seen(struct dwc3_apple *appledwc) +{ + struct usb_device *usb2; + bool ss; + + lockdep_assert_held(&appledwc->lock); + if (!dwc3_apple_ss_port(appledwc) || appledwc->state != DWC3_APPLE_HOST || + !appledwc->core_active || appledwc->core_exit_pending || + dwc3_apple_ss_ports(appledwc, &usb2, &ss, NULL, NULL)) + return false; + usb_put_dev(usb2); + + return ss; +} + +/* + * TEST (J714s, T6050 Type-C): the USB3 hub behind a DP alt mode port (Dell S2725QC: 0bda:5409 + * and 0bda:0409, PHY mode 3, DP2) brings its SuperSpeed side up 0.26 to 0.28 s after its USB2 + * side enumerates, in 28 of the 29 wakes that had it and at all but two boots that had it, + * even where xHCI had been running for 1.8 s by then. In 12 of 41 wakes, and at one boot, + * that one attempt failed and nothing made another; the USB2 hub stayed. The log shows those + * wakes as the good ones: one start of the PHY's mode 3, the PIPE switch without a warning, + * xHCI, the USB2 hub, within a few ms, with or without the wait for the PHY. Once after the + * start that follows a sleep in which a SuperSpeed device was there: if none is back while a + * USB2 device is, log the xHCI's PORTSC words and the core's LTSSM: which side gave up (the + * device in Rx.Detect, or the host's link) decides the fix, which is then taken from what + * macOS does on that port. Log only: nothing is reset, and neither the PHY nor the controller + * is touched (in mode 3 the USB3 PHY's power-off stops the whole mode with the PIPE, the + * display's link included, combo_quiesce()). + */ +static void dwc3_apple_ss_check(struct dwc3_apple *appledwc) +{ + struct usb_device *usb2 = NULL; + u32 portsc[2] = {}, ltssm = 0; + unsigned int nports = 0; + bool ss = false; + + scoped_guard(mutex, &appledwc->lock) { + if (!appledwc->ss_retry) + return; + appledwc->ss_retry = false; + if (appledwc->shutting_down || appledwc->sleep_dead || appledwc->sleep_stopped || + !appledwc->core_active || appledwc->core_exit_pending || + READ_ONCE(appledwc->sleep_phase) != DWC3_APPLE_SLEEP_IDLE || + appledwc->state != DWC3_APPLE_HOST || + dwc3_apple_ss_ports(appledwc, &usb2, &ss, portsc, &nports)) + return; + if (ss || !usb2) { + if (ss) + dev_info(appledwc->dev, + "TEST: SuperSpeed device back after the sleep\n"); + usb_put_dev(usb2); + return; + } + ltssm = dwc3_readl(&appledwc->dwc, DWC3_GDBGLTSSM); + } + + /* + * J714s: no host-side reset brings that SuperSpeed side back (a USB2 reset, a root port + * warm reset or power cycle, a PIPE reset, xHCI rebuilt: 8 of 8 lost wakes, 11-18); a + * replug does. Have the port controller replug the port, once: the display on it goes dark + * for a few seconds, and both of the hub's sides enumerate again from the attach. The + * replug runs on the port controller's work, after this check. + */ + { + int (*replug)(struct usb_role_switch *sw, u8 seconds) = symbol_get(tipd_port_replug); + int ret = -ENOENT; + + if (replug) { + ret = replug(appledwc->role_sw, 1); + symbol_put(tipd_port_replug); + } + dev_warn(appledwc->dev, + "SuperSpeed side of %04x:%04x not back after the sleep (PORTSC %#010x %#010x (%u), LTSSM %#010x): port replug requested (%d)\n", + le16_to_cpu(usb2->descriptor.idVendor), le16_to_cpu(usb2->descriptor.idProduct), + portsc[0], portsc[1], nports, ltssm, ret); + } + usb_put_dev(usb2); +} + /* * Once after a sleep: if the hub isn't there, stop the host and start it again, with the * hub's link down for a moment first, as unbinding and binding the driver does. This also @@ -1224,6 +1377,12 @@ static void dwc3_apple_hub_check(struct work_struct *work) hub_check); int ret; + /* TEST (J714s): a Type-C host's SuperSpeed device after a sleep. */ + if (!appledwc->fixed_hub) { + dwc3_apple_ss_check(appledwc); + return; + } + guard(mutex)(&appledwc->lock); if (appledwc->shutting_down || appledwc->sleep_dead || appledwc->sleep_stopped || !appledwc->core_active || appledwc->core_exit_pending || @@ -1334,6 +1493,7 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role ro case USB_ROLE_NONE: /* Nothing to do if no cable is connected */ appledwc->sleep_phy_pending = false; + appledwc->ss_retry = false; return 0; case USB_ROLE_HOST: case USB_ROLE_DEVICE: @@ -1342,11 +1502,16 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role ro * before the port controller's mode request. The error goes back as it is, so the * port controller tries again, without a line in the log. */ + /* TEST (J714s): only the start after a sleep looks for its SuperSpeed device. */ + if (!appledwc->sleep_phy_pending) + appledwc->ss_retry = false; appledwc->sleep_phy_wait = appledwc->sleep_phy_pending; ret = dwc3_apple_init(appledwc, dwc3_apple_state_for_role(role)); appledwc->sleep_phy_wait = false; if (ret != -ENOLINK) appledwc->sleep_phy_pending = false; + if (!ret) + dwc3_apple_check_hub_later(appledwc); return ret; default: dev_err(appledwc->dev, "Invalid target role: %d\n", role); @@ -1403,6 +1568,8 @@ static int dwc3_apple_sleep_stop(struct dwc3_apple *appledwc) appledwc->sleep_state = appledwc->state; appledwc->sleep_wakeup = device_may_wakeup(appledwc->dev); appledwc->sleep_stopped = true; + /* TEST (J714s): looked for once after the start, see dwc3_apple_ss_check(). */ + appledwc->ss_retry = dwc3_apple_ss_seen(appledwc); } ret = dwc3_apple_exit(appledwc); if (!ret) diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 6ad1d66a67fb9..161108a2d448b 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -3475,6 +3475,143 @@ static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status) return 0; } +/* The ports tipd_port_replug() looks up by the USB role switch their data role drives. */ +static LIST_HEAD(tipd_ports); +static DEFINE_MUTEX(tipd_ports_lock); + +static void tipd_replug_work(struct work_struct *work); + +static void tipd_port_list_add(struct tps6598x *tps) +{ + INIT_LIST_HEAD(&tps->port_node); + INIT_WORK(&tps->replug_work, tipd_replug_work); + if (!tps->role_sw) + return; + mutex_lock(&tipd_ports_lock); + list_add_tail(&tps->port_node, &tipd_ports); + mutex_unlock(&tipd_ports_lock); +} + +static void tipd_port_list_del(struct tps6598x *tps) +{ + if (!tps->port_node.next) + return; + mutex_lock(&tipd_ports_lock); + list_del_init(&tps->port_node); + mutex_unlock(&tipd_ports_lock); + cancel_work_sync(&tps->replug_work); +} + +/* The J714s's port controllers take "DISC" unlocked with this key, the reversed target identifier. */ +static const u8 tipd_replug_key_j714s[4] = { '4', '1', '7', 'J' }; + +static bool tipd_replug_supported(struct tps6598x *tps) +{ + return of_machine_is_compatible("apple,j714s") && + device_is_compatible(tps->dev, "apple,sn201202x"); +} + +/* + * As cd321x_reset_inherited_connection() does it: unlocked, "DISC", the app mode back, locked + * again. The port lock is held only for the commands: meanwhile the interrupt handler sees the + * partner go and come back, and sets the mux, the alternate modes and the data role again. + * The commands are read with the gap the sleep's own command is (CD321X_SSPS_READ_GAP_US): read + * back to back while the partner attaches again, the relock ended late, or in SPMI parity faults + * and -EIO. A relock that fails is tried once more. + */ +#define TIPD_REPLUG_RELOCK_TRIES 2 +static void tipd_replug_work(struct work_struct *work) +{ + struct tps6598x *tps = container_of(work, struct tps6598x, replug_work); + static const u8 locked[4]; + u8 seconds = tps->replug_seconds, result = 0; + unsigned long timeout; + int ret, lock_ret, mode = -1; + unsigned int tries; + + mutex_lock(&tps->lock); + ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(tipd_replug_key_j714s), tipd_replug_key_j714s, + sizeof(result), &result, 1000, 0, CD321X_SSPS_READ_GAP_US); + if (!ret && result) + ret = -EIO; + if (!ret) + ret = tps6598x_exec_cmd_gap(tps, "DISC", sizeof(seconds), &seconds, 0, NULL, 5000, 0, + CD321X_SSPS_READ_GAP_US); + mutex_unlock(&tps->lock); + + if (!ret) { + msleep(seconds * 1000 + 100); + timeout = jiffies + msecs_to_jiffies(2000); + do { + mutex_lock(&tps->lock); + mode = tps6598x_check_mode(tps); + mutex_unlock(&tps->lock); + if (mode == TPS_MODE_APP || mode == TPS_MODE_APP1) + break; + msleep(20); + } while (time_before(jiffies, timeout)); + if (mode != TPS_MODE_APP && mode != TPS_MODE_APP1) + ret = mode < 0 ? mode : -ETIMEDOUT; + } + + for (tries = 1; ; tries++) { + mutex_lock(&tps->lock); + lock_ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(locked), locked, 0, NULL, 5000, 0, + CD321X_SSPS_READ_GAP_US); + mutex_unlock(&tps->lock); + if (!lock_ret || tries >= TIPD_REPLUG_RELOCK_TRIES) + break; + msleep(500); + } + + if (ret || lock_ret) + dev_warn(tps->dev, "port replug for its partner failed: %d (unlock result %u), locked again: %d (try %u)\n", + ret, result, lock_ret, tries); + else + dev_info(tps->dev, "port replugged for its partner (%u s disconnected), locked again (try %u)\n", + seconds, tries); +} + +/** + * tipd_port_replug - disconnect and reattach a port, as a cable pulled and plugged back does + * @sw: the USB role switch the port drives + * @seconds: how long the port stays disconnected + * + * The controller's "DISC" command: it detaches the partner for @seconds and attaches it again + * on its own, with a new contract, its alternate modes entered and the mux and the data role set + * again. For a partner that a system sleep left in a state only a replug ends: on the J714s a + * DP alt mode hub whose SuperSpeed side does not come back after some wakes (dwc3-apple). A + * display on the port goes dark meanwhile. The replug runs on the port's own work, after this + * returns: 0 when it is queued, -EBUSY if one already is, -EOPNOTSUPP on a controller that is + * not known to take it (the J714s's SN201202x only), -ENODEV if no port drives @sw. + */ +int tipd_port_replug(struct usb_role_switch *sw, u8 seconds) +{ + struct tps6598x *tps; + int ret = -ENODEV; + + mutex_lock(&tipd_ports_lock); + list_for_each_entry(tps, &tipd_ports, port_node) { + if (tps->role_sw != sw) + continue; + ret = -EOPNOTSUPP; + if (!tipd_replug_supported(tps)) + break; + ret = -EBUSY; + if (work_pending(&tps->replug_work)) + break; + tps->replug_seconds = seconds; + ret = queue_work(system_freezable_wq, &tps->replug_work) ? 0 : -EBUSY; + dev_info(tps->dev, "port replug for its partner requested (%u s disconnected): %d\n", + seconds, ret); + break; + } + mutex_unlock(&tipd_ports_lock); + + return ret; +} +EXPORT_SYMBOL_GPL(tipd_port_replug); + int tipd_init(struct tps6598x *tps) { struct fwnode_handle *fwnode; @@ -3640,6 +3777,7 @@ int tipd_init(struct tps6598x *tps) } tipd_debugfs_register(tps); + tipd_port_list_add(tps); if (tps->data->ready) tps->data->ready(tps); @@ -3673,6 +3811,7 @@ void tipd_remove(struct tps6598x *tps) { int ret = 0; + tipd_port_list_del(tps); tipd_debugfs_unregister(tps); if (!tps->irq) diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index 42692ced9ad3c..ed968a4b6aaaa 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -357,6 +357,10 @@ struct tipd_data { struct tps6598x { /* Native H17 routes retain ENOSPC admission and their own HPD path. */ u8 h17_display:1; + /* On the list tipd_port_replug() looks a port up on, by its role switch; its replug. */ + struct list_head port_node; + struct work_struct replug_work; + u8 replug_seconds; struct device *dev; struct regmap *regmap; struct mutex lock; /* device lock */ diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c index 2461f130fa7f1..cc3197c1b738a 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c @@ -102,6 +102,8 @@ struct dwc3_apple { bool pre_core_rpm; /* J714s only: the start after a sleep waits for its Type-C PHY; never set here. */ bool sleep_phy_wait, sleep_phy_pending; + /* J714s only: a SuperSpeed device looked for after a sleep; never set here. */ + bool ss_retry; bool dt_dis_u3_susphy; unsigned long usb2_power_off_pending, usb3_power_off_pending; /* The PM sleep epoch (dwc3_apple_pm_notify()); the fixtures stay IDLE. */ @@ -371,6 +373,11 @@ static void dwc3_apple_setup_cio(struct dwc3_apple *a) (void)a; setup_cio++; } +/* The check after a sleep is only for a fixed hub or a J714s SuperSpeed device; neither here. */ +static void dwc3_apple_check_hub_later(struct dwc3_apple *a) +{ + assert(!a->fixed_hub && !a->ss_retry); +} /* The hibernation block's register setup; teardown order doesn't depend on it. */ static void __attribute__((unused)) dwc3_apple_setup_hibernation(struct dwc3_apple *a) { diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py index 46c5bf457d02f..9257b85dec643 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py @@ -120,6 +120,8 @@ def make_unit(source, parent=False, reference=None): static void dcp_typec_pipeline_freed(void) { event('F'); } static void power_supply_changed(void *p) { (void)p; } static void tipd_debugfs_unregister(struct tps6598x *t) { (void)t; } +/* The replug port list and its work (tipd_port_replug()) are outside these fixtures. */ +static void tipd_port_list_del(struct tps6598x *t) { (void)t; } static void cancel_delayed_work_sync(void *w) { (void)w; } static void devm_free_irq(void *d,int i,void *t) { (void)d;(void)i;(void)t; } static void typec_mux_put(struct typec_mux *m) { (void)m; } diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py b/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py index 90005ca179a62..d6410057b5ae6 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py @@ -425,6 +425,7 @@ def probe(root): static void queue_delayed_work(int wq,int *work,unsigned delay) { (void)wq;(void)work;(void)delay; } static bool device_property_read_bool(void *d,const char *p) { (void)d;(void)p;return false; } static void tipd_debugfs_register(struct tps6598x *t) { (void)t; } +static void tipd_port_list_add(struct tps6598x *t) { (void)t; } static int register_port(struct tps6598x *t,struct fwnode_handle *f) { (void)t;(void)f;return register_error; } static bool read_data_status(struct tps6598x *t) { (void)t;return true; } static int connect(struct tps6598x *t,u32 status) { (void)t;(void)status;return connect_error; }