From b3d63e0fcadcfa12235cbaf04189464c2c792880 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:00:30 -0600 Subject: [PATCH 1/2] usb: typec: tipd: close the USB4 session before a reboot or power off On a 14-inch M5 Max with a Studio Display on the Thunderbolt port, a reboot could hang for minutes. systemd's last step, device_shutdown(), blocked in the Thunderbolt NHI teardown. A cable event closed the ACIO session from the CD321x update work after the PCIe-C controller had begun to shut down, so the PCIe-C reset handshake of the tunnel timed out ("failed to quiesce PCIe-C before NHI shutdown: -110") and device_shutdown() waited behind it. Close the session earlier, while the system still runs and the lanes carry the handshake. A reboot notifier runs before system_state changes and before device_shutdown(). For SYS_RESTART, SYS_HALT and SYS_POWER_OFF it disables the port's interrupt, stops its work, and sends TYPEC_THUNDERBOLT_SWITCH_OFF to the router. After that no cable event can reach the router, so the late close never starts. The notifier logs once before and once after the close, so a reboot log shows whether the session was closed in order. Add two KUnit tests: a reboot sends OFF and stops the port's update work, and any other notifier action leaves the port alone. Tested: KUnit (cd321x suites on arm64 under qemu): 122 of 122 pass, and with the OFF call removed the new reboot test fails. On a 14-inch M5 Max with a Studio Display docked, three plain reboots with this change returned in 33, 32 and 37 seconds. Each log shows "closing the USB4/Thunderbolt session before shutdown", the PCIe-C quiesce, "closed the USB4/Thunderbolt session before shutdown: 0", then "Preparing for reboot", and the display and Thunderbolt device were back after each boot. Not tested: kexec. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/usb/typec/tipd/cd321x-tbt-test.c | 50 +++++++++++++++++++++++ drivers/usb/typec/tipd/core.c | 52 ++++++++++++++++++++++++ drivers/usb/typec/tipd/tps6598x.h | 6 +++ 3 files changed, 108 insertions(+) diff --git a/drivers/usb/typec/tipd/cd321x-tbt-test.c b/drivers/usb/typec/tipd/cd321x-tbt-test.c index 5ed8a2ab136ce..8e1d83d18546c 100644 --- a/drivers/usb/typec/tipd/cd321x-tbt-test.c +++ b/drivers/usb/typec/tipd/cd321x-tbt-test.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -101,6 +102,8 @@ struct cd321x_tbt_test { struct device *dev; bool keep_waiting; unsigned int keep_waiting_calls; + unsigned int set_calls; + int last_state; }; /* Stands in for the driver core's answer on whether to keep waiting. */ @@ -143,6 +146,10 @@ static int cd321x_tbt_test_init(struct kunit *test) static int cd321x_tbt_test_set(struct typec_thunderbolt_switch_dev *sw, const struct typec_thunderbolt_switch_data *data) { + struct cd321x_tbt_test *ctx = kunit_get_current_test()->priv; + + ctx->set_calls++; + ctx->last_state = data->state; return 0; } @@ -406,6 +413,47 @@ static void cd321x_tbt_router_rebinds_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, cd->tbt_notifier_registered); } +/* A port with a live router, as the cable events find it. */ +static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test) +{ + struct cd321x *cd = cd321x_tbt_test_late_port(test); + int ret; + + ret = cd321x_tbt_watch_late(cd, software_node_fwnode(&cd321x_test_connector)); + KUNIT_ASSERT_EQ(test, ret, 0); + flush_work(&cd->tbt_attach_work); + cd321x_tbt_test_add_router(test, &cd321x_test_router); + flush_work(&cd->tbt_attach_work); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cd->tbt_switch); + scoped_guard(mutex, &cd->tps.lock) + cd->pm.phase = CD321X_PM_RUNNING; + return cd; +} + +/* A reboot closes the router's session at once and keeps cable events away. */ +static void cd321x_tbt_reboot_closes_session_test(struct kunit *test) +{ + struct cd321x_tbt_test *ctx = test->priv; + struct cd321x *cd = cd321x_tbt_test_live_port(test); + + KUNIT_EXPECT_EQ(test, cd321x_reboot_notify(&cd->reboot_nb, SYS_RESTART, NULL), + NOTIFY_OK); + KUNIT_EXPECT_EQ(test, ctx->set_calls, 1); + KUNIT_EXPECT_EQ(test, ctx->last_state, TYPEC_THUNDERBOLT_SWITCH_OFF); + KUNIT_EXPECT_FALSE(test, cd321x_pm_can_update(&cd->pm)); +} + +/* Only a reboot, halt or power off closes the session. */ +static void cd321x_tbt_reboot_other_action_test(struct kunit *test) +{ + struct cd321x_tbt_test *ctx = test->priv; + struct cd321x *cd = cd321x_tbt_test_live_port(test); + + KUNIT_EXPECT_EQ(test, cd321x_reboot_notify(&cd->reboot_nb, 0, NULL), NOTIFY_DONE); + KUNIT_EXPECT_EQ(test, ctx->set_calls, 0); + KUNIT_EXPECT_TRUE(test, cd321x_pm_can_update(&cd->pm)); +} + static struct kunit_case cd321x_tbt_cases[] = { KUNIT_CASE(cd321x_tbt_no_router_test), KUNIT_CASE(cd321x_tbt_router_bound_test), @@ -416,6 +464,8 @@ static struct kunit_case cd321x_tbt_cases[] = { KUNIT_CASE(cd321x_tbt_other_router_binds_test), KUNIT_CASE(cd321x_tbt_router_binds_after_removal_test), KUNIT_CASE(cd321x_tbt_router_rebinds_test), + KUNIT_CASE(cd321x_tbt_reboot_closes_session_test), + KUNIT_CASE(cd321x_tbt_reboot_other_action_test), {} }; diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 9298672c02aa0..148712d49d56b 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -2602,6 +2603,45 @@ VISIBLE_IF_KUNIT void cd321x_stop_work(struct cd321x *cd321x) } EXPORT_SYMBOL_IF_KUNIT(cd321x_stop_work); +/* + * Close the port's USB4/Thunderbolt session while the system still runs. A + * cable event that closes it inside device_shutdown() makes pid 1 wait for the + * PCIe-C reset handshake of an ACIO that is already going down. Stopping the + * work first keeps every later event away from the router. + */ +VISIBLE_IF_KUNIT int cd321x_reboot_close(struct cd321x *cd321x) +{ + struct typec_thunderbolt_switch_data off = { + .state = TYPEC_THUNDERBOLT_SWITCH_OFF, + }; + + cd321x_stop_work(cd321x); + return typec_thunderbolt_switch_set(cd321x->tbt_switch, &off); +} +EXPORT_SYMBOL_IF_KUNIT(cd321x_reboot_close); + +VISIBLE_IF_KUNIT int cd321x_reboot_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct cd321x *cd321x = container_of(nb, struct cd321x, reboot_nb); + struct tps6598x *tps = &cd321x->tps; + int ret; + + if (action != SYS_RESTART && action != SYS_HALT && action != SYS_POWER_OFF) + return NOTIFY_DONE; + + if (tps->irq) + disable_irq(tps->irq); + if (cd321x->tbt_switch) + dev_info(tps->dev, "closing the USB4/Thunderbolt session before shutdown\n"); + ret = cd321x_reboot_close(cd321x); + if (cd321x->tbt_switch) + dev_info(tps->dev, "closed the USB4/Thunderbolt session before shutdown: %d\n", + ret); + return NOTIFY_OK; +} +EXPORT_SYMBOL_IF_KUNIT(cd321x_reboot_notify); + /* Separate so that the KUnit tests can choose the driver core's answer. */ VISIBLE_IF_KUNIT bool cd321x_tbt_switch_keep_waiting(struct device *dev) { @@ -2821,6 +2861,13 @@ cd321x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode) cd321x_tbt_watch_late(cd321x, fwnode)) dev_warn(tps->dev, "cannot watch for the USB4/Thunderbolt router\n"); + cd321x->reboot_nb.notifier_call = cd321x_reboot_notify; + cd321x->reboot_nb.priority = INT_MAX; + if (register_reboot_notifier(&cd321x->reboot_nb)) + dev_warn(tps->dev, "cannot close the USB4/Thunderbolt session at shutdown\n"); + else + cd321x->reboot_nb_registered = true; + return 0; err_put_switch: @@ -2852,6 +2899,11 @@ cd321x_unregister_port(struct tps6598x *tps) { struct cd321x *cd321x = container_of(tps, struct cd321x, tps); + if (cd321x->reboot_nb_registered) { + unregister_reboot_notifier(&cd321x->reboot_nb); + cd321x->reboot_nb_registered = false; + } + cd321x_stop_work(cd321x); fwnode_handle_put(cd321x->connector_fwnode); cd321x->connector_fwnode = NULL; diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index a9bc1d7383008..f92476ee009b9 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -466,6 +466,9 @@ struct cd321x { struct notifier_block tbt_watch; struct work_struct tbt_attach_work; bool tbt_watch_registered; + /* Closes the USB4/Thunderbolt session before device_shutdown() */ + struct notifier_block reboot_nb; + bool reboot_nb_registered; atomic_t link_event; atomic_t ready_event; atomic_t restore_event; @@ -570,6 +573,9 @@ void cd321x_mux_gave_up(struct cd321x *cd321x, unsigned int retries, unsigned in void cd321x_init_work(struct cd321x *cd321x); int cd321x_init_mux(struct cd321x *cd321x); void cd321x_stop_work(struct cd321x *cd321x); +int cd321x_reboot_close(struct cd321x *cd321x); +int cd321x_reboot_notify(struct notifier_block *nb, unsigned long action, + void *data); bool cd321x_tbt_switch_keep_waiting(struct device *dev); struct typec_thunderbolt_switch * cd321x_tbt_switch_get(struct device *dev, struct fwnode_handle *fwnode, From 367a01b24c0ea5047595e0dc3ee24e140ed57564 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:29:43 -0600 Subject: [PATCH 2/2] selftests: apple-dcp: probe fixture models the reboot notifier cd321x_register_port() now sets reboot_nb up and registers it, so the extracted probe no longer compiled in the typec-probe and probe-mux-pm-dispatch units. Give the fixture's struct cd321x the two new members, give notifier_block a priority, and mock register and unregister. Check that probe registers the notifier with cd321x_reboot_notify and that every case unregisters it. --- .../selftests/drivers/gpu/apple-dcp/pm.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py b/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py index 4dcdc35ed409b..0c403fa4e7c0d 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py @@ -493,7 +493,7 @@ def function(text, name): struct typec_mux_state { void *alt,*data; unsigned long mode; }; struct device; struct tps6598x { int lock; void *port; struct device *dev; }; -struct notifier_block { int (*notifier_call)(struct notifier_block *,unsigned long,void *); }; +struct notifier_block { int (*notifier_call)(struct notifier_block *,unsigned long,void *); int priority; }; ''' pm = tipd_pm_header(root) mocks = r''' @@ -762,6 +762,27 @@ def function(text, name): cd321x_unregister_port(&c.tps);balanced(); ''', 1) + # A port that closes its USB4 session before a reboot registers a reboot notifier at probe. + if 'register_reboot_notifier(' in core: + member = ' bool state_valid,tbt_notifier_registered;\n' + assert mocks.count(member) == 1 + mocks = mocks.replace(member, member + ' struct notifier_block reboot_nb;\n bool reboot_nb_registered;\n', 1) + mocks += r''' +#ifndef INT_MAX +#define INT_MAX 0x7fffffff +#endif +static int reboots; +static int cd321x_reboot_notify(struct notifier_block *n,unsigned long a,void *d) { (void)n;(void)a;(void)d;return 0; } +static int register_reboot_notifier(struct notifier_block *n) { (void)n;reboots++;return 0; } +static void unregister_reboot_notifier(struct notifier_block *n) { (void)n;assert(reboots>0);reboots--; } +''' + idle = ' assert(!ports&&!altmodes&&!muxes&&!switches&&!notifiers);\n}' + assert controls.count(idle) == 1 + controls = controls.replace(idle, idle[:-2] + '\n assert(!reboots);\n}', 1) + probed = ' reset(&c);assert(cd321x_register_port(&c.tps,&connector)==0&&c.state_valid);\n' + assert controls.count(probed) == 1 + controls = controls.replace(probed, probed + ' assert(reboots==1&&c.reboot_nb_registered&&c.reboot_nb.notifier_call==cd321x_reboot_notify);\n', 1) + return prefix + pm + mocks + production + '\n#define TEST_DEFER 1\n#define TEST_FWNODE 1\n' + controls