diff --git a/arch/arm64/boot/dts/apple/t8122-j615.dts b/arch/arm64/boot/dts/apple/t8122-j615.dts index 8c3add040f8d9..77544fc7ee03c 100644 --- a/arch/arm64/boot/dts/apple/t8122-j615.dts +++ b/arch/arm64/boot/dts/apple/t8122-j615.dts @@ -10,11 +10,21 @@ /dts-v1/; #include "t8122.dtsi" +#include "t8122-usb4.dtsi" #include "t8122-j613-j615.dtsi" / { compatible = "apple,j615", "apple,t8122", "apple,arm-platform"; model = "Apple MacBook Air (15-inch, M3, 2024)"; + + aliases { + usb4-0-pcie = &usb4_0_pcie; + usb4-0-pcie-port = &usb4_0_pcie_port; + usb4-1-pcie = &usb4_1_pcie; + usb4-1-pcie-port = &usb4_1_pcie_port; + usb4-0-pcie-adapter = &usb4_0_acio; + usb4-1-pcie-adapter = &usb4_1_acio; + }; }; &dcp { @@ -152,3 +162,6 @@ /* Per-device factory data from this Mac's own iBoot system container. */ firmware-name = "apple/mesacal-j615.bin"; }; + +/* Older Stage 2 loaders prune the shared ATC0 suppliers of this unused node. */ +/delete-node/ &usb_otg0; diff --git a/arch/arm64/boot/dts/apple/t8122-usb4.dtsi b/arch/arm64/boot/dts/apple/t8122-usb4.dtsi index ce69d7ec74f9c..fccb62addf109 100644 --- a/arch/arm64/boot/dts/apple/t8122-usb4.dtsi +++ b/arch/arm64/boot/dts/apple/t8122-usb4.dtsi @@ -15,6 +15,7 @@ interrupt-parent = <&aic>; interrupts = ; #iommu-cells = <1>; + power-domains = <&ps_atc0_cio_pcie>; /* No bootloader fills this one; the tunnel's cold init needs it. */ apple,tunable = <0x20c 0x800001b7 0x800001b7>, @@ -115,6 +116,7 @@ interrupt-parent = <&aic>; interrupts = ; #iommu-cells = <1>; + power-domains = <&ps_atc1_cio_pcie>; /* No bootloader fills this one; the tunnel's cold init needs it. */ apple,tunable = <0x20c 0x800001b7 0x800001b7>, diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 8242b75609c0a..1d376a6584f40 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -4644,9 +4644,13 @@ static int apple_cio_probe(struct platform_device *pdev) acio->dev = &pdev->dev; acio->np = dev->of_node; acio->hw = of_device_get_match_data(dev); - /* Only J613 has qualified the T8122 router policy and resources. */ + /* + * The T8122 router policy starts the router through its MxWrap CPU + * block. A device tree (or an older loader's handoff tree) that does + * not describe that block keeps the T8103 policy. + */ if (acio->hw == &apple_cio_t8122_hw && - !of_machine_is_compatible("apple,j613")) + of_property_match_string(acio->np, "reg-names", "cpu") < 0) acio->hw = &apple_cio_t8103_hw; if (!acio->hw) return -EINVAL; @@ -4857,6 +4861,8 @@ static int apple_cio_probe(struct platform_device *pdev) .fwnode = pdev->dev.fwnode, .set = apple_cio_tbt_switch_set, .drvdata = acio, + /* A strict power cycle is ordered by the PD driver (sleep and shutdown). */ + .pd_ordered_stop = acio->hw->strict_power_cycle, }; /* A consumer cannot start a host before its failure listener is ready. */ scoped_guard(mutex, &acio->lock) { diff --git a/drivers/usb/typec/mux.c b/drivers/usb/typec/mux.c index 8e830fff78fe7..49fee90e9809d 100644 --- a/drivers/usb/typec/mux.c +++ b/drivers/usb/typec/mux.c @@ -891,6 +891,24 @@ bool typec_thunderbolt_switch_is_live(struct typec_thunderbolt_switch *sw) } EXPORT_SYMBOL_GPL(typec_thunderbolt_switch_is_live); +/** + * typec_thunderbolt_switch_pd_ordered_stop - Whether the port driver stops the switch + * @sw: USB4/Thunderbolt switch + * + * Returns true if the bound provider asked the Type-C port driver to stop + * it before tasks freeze and at shutdown (see struct + * typec_thunderbolt_switch_desc), false for any other or no provider. + */ +bool typec_thunderbolt_switch_pd_ordered_stop(struct typec_thunderbolt_switch *sw) +{ + if (IS_ERR_OR_NULL(sw)) + return false; + + guard(rwsem_read)(&sw->sw_dev->set_lock); + return sw->sw_dev->set && sw->sw_dev->pd_ordered_stop; +} +EXPORT_SYMBOL_GPL(typec_thunderbolt_switch_pd_ordered_stop); + /** * typec_thunderbolt_switch_set - Forward cable details to USB4/Thunderbolt switch * @sw: USB4/Thunderbolt switch @@ -1057,6 +1075,7 @@ typec_thunderbolt_switch_register(struct device *parent, init_rwsem(&sw_dev->set_lock); sw_dev->owner = parent->driver ? parent->driver->owner : NULL; sw_dev->set = desc->set; + sw_dev->pd_ordered_stop = desc->pd_ordered_stop; device_initialize(&sw_dev->dev); sw_dev->dev.parent = parent; diff --git a/drivers/usb/typec/mux.h b/drivers/usb/typec/mux.h index 9ce77aecf7bac..501c7aa4010a7 100644 --- a/drivers/usb/typec/mux.h +++ b/drivers/usb/typec/mux.h @@ -43,6 +43,7 @@ struct typec_thunderbolt_switch_dev { struct module *owner; typec_thunderbolt_switch_set_fn_t set; struct atomic_notifier_head notifiers; + bool pd_ordered_stop; }; #define to_typec_thunderbolt_switch_dev(_dev_) \ diff --git a/drivers/usb/typec/tipd/cd321x-tbt-test.c b/drivers/usb/typec/tipd/cd321x-tbt-test.c index 8e1d83d18546c..b0131305e00a7 100644 --- a/drivers/usb/typec/tipd/cd321x-tbt-test.c +++ b/drivers/usb/typec/tipd/cd321x-tbt-test.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -285,13 +286,15 @@ static struct cd321x *cd321x_tbt_test_late_port(struct kunit *test) return cd; } -static void cd321x_tbt_test_add_router(struct kunit *test, - const struct software_node *node) +static void cd321x_tbt_test_add_router_ordered(struct kunit *test, + const struct software_node *node, + bool pd_ordered_stop) { struct typec_thunderbolt_switch_desc desc = { .fwnode = software_node_fwnode(node), .set = cd321x_tbt_test_set, .name = node->name, + .pd_ordered_stop = pd_ordered_stop, }; struct typec_thunderbolt_switch_dev *sw_dev; struct device *router; @@ -306,6 +309,12 @@ static void cd321x_tbt_test_add_router(struct kunit *test, KUNIT_ASSERT_EQ(test, ret, 0); } +static void cd321x_tbt_test_add_router(struct kunit *test, + const struct software_node *node) +{ + cd321x_tbt_test_add_router_ordered(test, node, false); +} + /* A router that registers after the port gave up is taken; the cable is reread. */ static void cd321x_tbt_router_binds_late_test(struct kunit *test) { @@ -414,7 +423,7 @@ static void cd321x_tbt_router_rebinds_test(struct kunit *test) } /* A port with a live router, as the cable events find it. */ -static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test) +static struct cd321x *cd321x_tbt_test_port(struct kunit *test, bool pd_ordered_stop) { struct cd321x *cd = cd321x_tbt_test_late_port(test); int ret; @@ -422,7 +431,7 @@ static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test) 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); + cd321x_tbt_test_add_router_ordered(test, &cd321x_test_router, pd_ordered_stop); flush_work(&cd->tbt_attach_work); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cd->tbt_switch); scoped_guard(mutex, &cd->tps.lock) @@ -430,6 +439,11 @@ static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test) return cd; } +static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test) +{ + return cd321x_tbt_test_port(test, false); +} + /* 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) { @@ -454,6 +468,57 @@ static void cd321x_tbt_reboot_other_action_test(struct kunit *test) KUNIT_EXPECT_TRUE(test, cd321x_pm_can_update(&cd->pm)); } +/* A router that stops on its own is not stopped by the port before sleep. */ +static void cd321x_tbt_sleep_router_stops_itself_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_session_pm(&cd->session_pm, PM_SUSPEND_PREPARE, NULL), + NOTIFY_DONE); + KUNIT_EXPECT_EQ(test, ctx->set_calls, 0); + KUNIT_EXPECT_EQ(test, cd321x_session_pm(&cd->session_pm, PM_POST_SUSPEND, NULL), + NOTIFY_OK); + KUNIT_EXPECT_EQ(test, ctx->set_calls, 0); +} + +/* A router that asks for it is put to sleep by the port and woken after. */ +static void cd321x_tbt_sleep_router_ordered_test(struct kunit *test) +{ + struct cd321x_tbt_test *ctx = test->priv; + struct cd321x *cd = cd321x_tbt_test_port(test, true); + + KUNIT_EXPECT_EQ(test, cd321x_session_pm(&cd->session_pm, PM_SUSPEND_PREPARE, NULL), + NOTIFY_OK); + KUNIT_EXPECT_EQ(test, ctx->set_calls, 1); + KUNIT_EXPECT_EQ(test, ctx->last_state, TYPEC_THUNDERBOLT_SWITCH_SLEEP); + /* The port is gated for sleep; there is no controller to reread after it. */ + scoped_guard(mutex, &cd->tps.lock) { + KUNIT_EXPECT_EQ(test, cd->pm.phase, CD321X_PM_PREPARED); + cd->pm.phase = CD321X_PM_INITIALIZING; + } + KUNIT_EXPECT_EQ(test, cd321x_session_pm(&cd->session_pm, PM_POST_SUSPEND, NULL), + NOTIFY_OK); + KUNIT_EXPECT_EQ(test, ctx->set_calls, 2); + KUNIT_EXPECT_EQ(test, ctx->last_state, TYPEC_THUNDERBOLT_SWITCH_WAKE); +} + +/* A plain TPS6598x has no struct cd321x around it: its shutdown stops nothing. */ +static void cd321x_tbt_shutdown_plain_port_test(struct kunit *test) +{ + static const struct tipd_data plain = { + .tps_struct_size = sizeof(struct tps6598x), + }; + struct cd321x_tbt_test *ctx = test->priv; + struct tps6598x *tps = kunit_kzalloc(test, sizeof(*tps), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, tps); + tps->data = &plain; + mutex_init(&tps->lock); + tipd_shutdown(tps); + KUNIT_EXPECT_EQ(test, ctx->set_calls, 0); +} + static struct kunit_case cd321x_tbt_cases[] = { KUNIT_CASE(cd321x_tbt_no_router_test), KUNIT_CASE(cd321x_tbt_router_bound_test), @@ -466,6 +531,9 @@ static struct kunit_case cd321x_tbt_cases[] = { KUNIT_CASE(cd321x_tbt_router_rebinds_test), KUNIT_CASE(cd321x_tbt_reboot_closes_session_test), KUNIT_CASE(cd321x_tbt_reboot_other_action_test), + KUNIT_CASE(cd321x_tbt_sleep_router_stops_itself_test), + KUNIT_CASE(cd321x_tbt_sleep_router_ordered_test), + KUNIT_CASE(cd321x_tbt_shutdown_plain_port_test), {} }; diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index bc7929fec09f9..a18fbdd5704f4 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -2619,34 +2619,45 @@ static void cd321x_session_restore(struct cd321x *cd) cd321x_complete(&cd->tps); } -static int cd321x_session_pm(struct notifier_block *nb, unsigned long event, void *unused) +VISIBLE_IF_KUNIT int cd321x_session_pm(struct notifier_block *nb, unsigned long event, void *unused) { struct cd321x *cd = container_of(nb, struct cd321x, session_pm); int ret; switch (event) { case PM_SUSPEND_PREPARE: + /* Only a switch that asked for it is stopped from here. */ + scoped_guard(mutex, &cd->tps.lock) + cd->session_pm_armed = + typec_thunderbolt_switch_pd_ordered_stop(cd->tbt_switch); + if (!cd->session_pm_armed) + return NOTIFY_DONE; ret = cd321x_session_stop(cd, true); - if (ret) + if (ret) { cd321x_session_restore(cd); + /* The PM core skips our POST_SUSPEND after a failed PREPARE. */ + cd->session_pm_armed = false; + } return notifier_from_errno(ret); case PM_POST_SUSPEND: - cd321x_session_restore(cd); + if (cd->session_pm_armed) + cd321x_session_restore(cd); + cd->session_pm_armed = false; break; } return NOTIFY_OK; } +EXPORT_SYMBOL_IF_KUNIT(cd321x_session_pm); static void cd321x_ready(struct tps6598x *tps) { struct cd321x *cd321x = container_of(tps, struct cd321x, tps); - if (of_machine_is_compatible("apple,j613")) { - cd321x->session_pm.notifier_call = cd321x_session_pm; - cd321x->session_pm.priority = 100; - if (!register_pm_notifier(&cd321x->session_pm)) - cd321x->session_pm_registered = true; - } + /* The switch may bind later; each suspend asks it again. */ + cd321x->session_pm.notifier_call = cd321x_session_pm; + cd321x->session_pm.priority = 100; + if (!register_pm_notifier(&cd321x->session_pm)) + cd321x->session_pm_registered = true; guard(mutex)(&tps->lock); /* Include failures reported while the port was still being initialized. */ @@ -3939,11 +3950,20 @@ EXPORT_SYMBOL_GPL(tipd_init); void tipd_shutdown(struct tps6598x *tps) { + struct cd321x *cd; + bool ordered; int ret; - if (!of_machine_is_compatible("apple,j613")) + /* Only a port embedded in a struct cd321x has a USB4 session to stop. */ + if (tps->data->tps_struct_size < sizeof(struct cd321x)) + return; + cd = container_of(tps, struct cd321x, tps); + + scoped_guard(mutex, &tps->lock) + ordered = typec_thunderbolt_switch_pd_ordered_stop(cd->tbt_switch); + if (!ordered) return; - ret = cd321x_session_stop(container_of(tps, struct cd321x, tps), false); + ret = cd321x_session_stop(cd, false); if (ret) dev_err(tps->dev, "USB4 shutdown incomplete: %d\n", ret); } diff --git a/drivers/usb/typec/tipd/i2c.c b/drivers/usb/typec/tipd/i2c.c index 7e63d2dfc04c7..2eed9be9210f8 100644 --- a/drivers/usb/typec/tipd/i2c.c +++ b/drivers/usb/typec/tipd/i2c.c @@ -93,6 +93,12 @@ static int __maybe_unused tps6598x_resume(struct device *dev) return ret; } +/* Stops a USB4 session first when its switch asks for a PD-ordered stop. */ +static void tps6598x_shutdown_i2c(struct i2c_client *client) +{ + tipd_shutdown(i2c_get_clientdata(client)); +} + static const struct dev_pm_ops tps6598x_pm_ops = { .prepare = pm_sleep_ptr(tipd_prepare), .complete = pm_sleep_ptr(tipd_complete), @@ -122,6 +128,7 @@ static struct i2c_driver tps6598x_i2c_driver = { }, .probe = tps6598x_probe_i2c, .remove = tps6598x_remove_i2c, + .shutdown = tps6598x_shutdown_i2c, .id_table = tps6598x_id, }; module_i2c_driver(tps6598x_i2c_driver); diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index b4344131ee322..743c8d62416a7 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -448,6 +448,7 @@ struct cd321x_status { struct cd321x { struct notifier_block session_pm; bool session_pm_registered; + bool session_pm_armed; bool session_closed; struct tps6598x tps; @@ -588,6 +589,7 @@ 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); +int cd321x_session_pm(struct notifier_block *nb, unsigned long event, void *unused); bool cd321x_tbt_switch_keep_waiting(struct device *dev); struct typec_thunderbolt_switch * cd321x_tbt_switch_get(struct device *dev, struct fwnode_handle *fwnode, diff --git a/include/linux/usb/typec_mux.h b/include/linux/usb/typec_mux.h index bf7fe40be1501..4cb9c2d1bff24 100644 --- a/include/linux/usb/typec_mux.h +++ b/include/linux/usb/typec_mux.h @@ -243,12 +243,16 @@ typedef int (*typec_thunderbolt_switch_set_fn_t)(struct typec_thunderbolt_switch * @set: Callback invoked with the cable details on connection changes * @name: Optional switch name, the parent device name is used if not set * @drvdata: Private data + * @pd_ordered_stop: The switch must be stopped by the Type-C port driver, + * before tasks freeze and at shutdown, ahead of the USB role and PHY + * (TYPEC_THUNDERBOLT_SWITCH_SLEEP / _OFF), instead of on its own */ struct typec_thunderbolt_switch_desc { struct fwnode_handle *fwnode; typec_thunderbolt_switch_set_fn_t set; const char *name; void *drvdata; + bool pd_ordered_stop; }; #if IS_ENABLED(CONFIG_TYPEC) @@ -257,6 +261,7 @@ struct typec_thunderbolt_switch * fwnode_typec_thunderbolt_switch_get(struct fwnode_handle *fwnode); void typec_thunderbolt_switch_put(struct typec_thunderbolt_switch *sw); bool typec_thunderbolt_switch_is_live(struct typec_thunderbolt_switch *sw); +bool typec_thunderbolt_switch_pd_ordered_stop(struct typec_thunderbolt_switch *sw); int typec_thunderbolt_switch_set(struct typec_thunderbolt_switch *sw, const struct typec_thunderbolt_switch_data *data); int typec_thunderbolt_switch_register_notifier(struct typec_thunderbolt_switch *sw, @@ -312,6 +317,12 @@ typec_thunderbolt_switch_is_live(struct typec_thunderbolt_switch *sw) return false; } +static inline bool +typec_thunderbolt_switch_pd_ordered_stop(struct typec_thunderbolt_switch *sw) +{ + return false; +} + static inline int typec_thunderbolt_switch_register_notifier(struct typec_thunderbolt_switch *sw, struct notifier_block *nb)