From b08e1731ecc65b66b1e9ce28db90a35d1f05b040 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 23:57:58 +1100 Subject: [PATCH 1/3] usb: apple: finish Type-C route and link recovery follow-ups Release the OF changeset bookkeeping after permanent route disables and skip repeated disables before allocation. Cover both node reference balance and repeated refusal in the topology KUnit suite. The installed properties remain owned by OF. Model the active USB4 PIPE backend before host shutdown and exercise mux disconnect on each supported PHY sequence. Correct the Type-C shutdown comment to describe PIPE parking and mux ownership. Keep discovery-based refill of the consecutive link-lost budget, but bound automatic recovery with a separate per-port rate limiter: three cycles per one-minute window. Domain reconstruction and a briefly discovered peer must not refill that limit. Signed-off-by: Ryan Murray --- .../gpu/drm/apple/dcp-fabric-topology-test.c | 47 +++++++++++++++++-- drivers/gpu/drm/apple/dcp-fabric.c | 24 ++++++++-- drivers/thunderbolt/apple.c | 15 ++++++ drivers/usb/typec/tipd/core.c | 5 +- tools/asahi/atc-usb3-tests.py | 21 +++++++-- 5 files changed, 98 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-fabric-topology-test.c b/drivers/gpu/drm/apple/dcp-fabric-topology-test.c index f1c62a6f010ae..fb06870ace9aa 100644 --- a/drivers/gpu/drm/apple/dcp-fabric-topology-test.c +++ b/drivers/gpu/drm/apple/dcp-fabric-topology-test.c @@ -403,13 +403,53 @@ static void topology_route_skip_test(struct kunit *test) KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cs); KUNIT_EXPECT_FALSE(test, of_device_is_available(route_np)); - /* A changeset still applied must not be destroyed */ + /* A repeated refusal neither allocates a changeset nor replaces status. */ + KUNIT_EXPECT_NULL(test, dcp_typec_routes_disable_nodes(dev, NULL, route_np)); + KUNIT_EXPECT_NULL(test, dcp_typec_routes_disable_nodes(dev, route_np->parent, NULL)); + KUNIT_EXPECT_FALSE(test, of_device_is_available(route_np)); + + /* + * A changeset still applied must not be destroyed here: retain it so + * the test can revert before overlay removal. + */ KUNIT_ASSERT_EQ(test, of_changeset_revert(cs), 0); - of_changeset_destroy(cs); - kfree(cs); + dcp_typec_routes_disable_free(cs); KUNIT_EXPECT_TRUE(test, of_device_is_available(route_np)); } +/* Permanent changes retain their properties, but not changeset node refs. */ +static void topology_route_disable_cleanup_test(struct kunit *test) +{ + struct device_node *route_np; + struct property *status; + struct device *dev; + unsigned int refs; + + KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, dcp_route_supplier_test)); + dev = kunit_device_register(test, "dcp-route-disable"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); + dev->of_node = of_find_node_by_path("/dcp-route-supplier-test/dcp"); + KUNIT_ASSERT_NOT_NULL(test, dev->of_node); + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, topology_route_supplier_put, dev), 0); + route_np = of_find_node_by_path("/dcp-route-supplier-test/dcp/typec-routes/route@1"); + KUNIT_ASSERT_NOT_NULL(test, route_np); + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, topology_route_node_put, route_np), 0); + refs = kref_read(&route_np->kobj.kref); + + dcp_typec_routes_disable_dev(dev); + KUNIT_EXPECT_FALSE(test, of_device_is_available(route_np)); + KUNIT_EXPECT_EQ(test, kref_read(&route_np->kobj.kref), refs); + status = of_find_property(route_np, "status", NULL); + KUNIT_ASSERT_NOT_NULL(test, status); + KUNIT_EXPECT_STREQ(test, status->value, "disabled"); + + dcp_typec_routes_disable_dev(dev); + KUNIT_EXPECT_PTR_EQ(test, of_find_property(route_np, "status", NULL), status); + KUNIT_EXPECT_EQ(test, kref_read(&route_np->kobj.kref), refs); +} + /* * apple_typec_port_rgb8_only(): a Type-C port is 8-bit RGB only if one of * the pipelines in scope (all of them, or only @pipeline) is a candidate for @@ -533,6 +573,7 @@ static struct kunit_case dcp_topology_cases[] = { KUNIT_CASE(topology_connector_group_test), KUNIT_CASE(topology_route_supplier_test), KUNIT_CASE(topology_route_skip_test), + KUNIT_CASE(topology_route_disable_cleanup_test), KUNIT_CASE(topology_rgb8_only_test), {} }; diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index 22148f031868f..2157cd4ade581 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -4850,8 +4850,9 @@ static bool dcp_typec_route_supplier_available(struct device *dev, const char *l * Disables @route, or every available route under @routes if @route is NULL, * in the device tree, in one changeset, for good. A connector passes over * unavailable routes when it looks for its display routes, so the port no - * longer waits for them. Returns the applied changeset, which stays (the live - * tree refers to it), or an ERR_PTR. + * longer waits for them. Returns the applied changeset, NULL if nothing was + * available, or an ERR_PTR. The caller must destroy and free the changeset; + * destroying it does not revert the changes or free the live properties. */ static struct of_changeset *dcp_typec_routes_disable_nodes(struct device *dev, struct device_node *routes, @@ -4861,6 +4862,9 @@ static struct of_changeset *dcp_typec_routes_disable_nodes(struct device *dev, struct device_node *np; int ret = 0; + if (route ? !of_device_is_available(route) : !dcp_typec_routes_available(routes)) + return NULL; + cs = kzalloc_obj(*cs); if (!cs) return ERR_PTR(-ENOMEM); @@ -4890,6 +4894,14 @@ static struct of_changeset *dcp_typec_routes_disable_nodes(struct device *dev, return cs; } +static void dcp_typec_routes_disable_free(struct of_changeset *cs) +{ + if (IS_ERR_OR_NULL(cs)) + return; + of_changeset_destroy(cs); + kfree(cs); +} + /* * Which of @dev's suppliers for its Type-C route @name ("typecN") has a * disabled node: "DP PHY", "display crossbar", or NULL if neither. Such a @@ -4933,6 +4945,7 @@ static int dcp_register_typec_routes_present(void *data) int ret; for_each_available_child_of_node_scoped(routes, route_np) { + struct of_changeset *cs; struct apple_dcp_typec_route *route; struct device_node *endpoint __free(device_node) = NULL; struct device_node *connector_np; @@ -4964,8 +4977,11 @@ static int dcp_register_typec_routes_present(void *data) return -ENOMEM; /* Skipped even if disabling it failed (logged): it cannot register */ - if (dcp_typec_route_skip(dev, route_np, name)) + cs = dcp_typec_route_skip(dev, route_np, name); + if (cs) { + dcp_typec_routes_disable_free(cs); continue; + } route = &dcp->typec_routes[dcp->nr_typec_routes]; route->dcp = dcp; @@ -5092,7 +5108,7 @@ void dcp_typec_routes_disable_dev(struct device *dev) of_get_child_by_name(dev->of_node, "typec-routes"); if (routes) - dcp_typec_routes_disable_nodes(dev, routes, NULL); + dcp_typec_routes_disable_free(dcp_typec_routes_disable_nodes(dev, routes, NULL)); } int dcp_register_typec_routes(struct apple_dcp *dcp) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index cb3a94977182a..06da2b98d0a48 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -101,6 +101,7 @@ #include #include #include +#include #include #include #include @@ -354,6 +355,11 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { */ #define APPLE_CIO_LINK_LOST_MS 3000 #define APPLE_CIO_LINK_LOST_CYCLES 3 +/* + * Recovery policy: three automatic port cycles per one-minute window, even + * when a flapping peer completes discovery between link losses. + */ +#define APPLE_CIO_LINK_LOST_INTERVAL (60 * HZ) static bool resume_recovery; module_param(resume_recovery, bool, 0644); @@ -569,6 +575,7 @@ struct apple_cio { unsigned int start_gen; /* protected by lock: bumped by every start */ unsigned int link_lost_gen; /* start_gen when the peer went away */ unsigned int link_lost_cycles; /* protected by lock */ + struct ratelimit_state link_lost_limit; /* survives domain rebuilds */ u64 link_lost_cycle_activity; /* remote activity of this domain at the last cycle */ bool link_lost_restart; /* protected by lock: cycle requested */ struct notifier_block pm_notifier; @@ -3964,6 +3971,12 @@ static void apple_cio_link_lost_work(struct work_struct *work) acio->sleep_peer_wanted = false; return; } + /* Discovery refills the consecutive budget, not this rate limit. */ + if (!__ratelimit(&acio->link_lost_limit)) { + dev_warn(acio->dev, "peer link keeps dropping, port restart rate limit reached\n"); + acio->sleep_peer_wanted = false; + return; + } acio->link_lost_cycles++; WRITE_ONCE(acio->link_lost_cycle_activity, activity); acio->link_lost_restart = true; @@ -4749,6 +4762,8 @@ static int apple_cio_probe(struct platform_device *pdev) apple_cio_pcie_tunnel_work); INIT_DELAYED_WORK(&acio->resume_link_work, apple_cio_resume_link_work); INIT_DELAYED_WORK(&acio->link_lost_work, apple_cio_link_lost_work); + ratelimit_state_init(&acio->link_lost_limit, APPLE_CIO_LINK_LOST_INTERVAL, + APPLE_CIO_LINK_LOST_CYCLES); INIT_DELAYED_WORK(&acio->sleep_restart_work, apple_cio_sleep_restart_work); INIT_WORK(&acio->sleep_dp_undo_work, apple_cio_sleep_dp_undo_work); init_completion(&acio->nhi_boot_completion); diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index d9ece372d36a4..c469e5b47b96b 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -1310,8 +1310,9 @@ static void cd321x_update_work(struct work_struct *work) /* * ACIO carries the tunneled PCIe reset handshake over the still-live * Type-C data path. Close a disappearing USB4/TBT session before dropping - * the USB role; otherwise the role transition tears down the ATC lanes and - * PCIe-C cannot quiesce its hierarchy before NHI removal. + * the USB role, so PCIe-C can quiesce its hierarchy before NHI removal. + * Stopping the USB role parks its PIPE; the Type-C mux releases the ATC + * lanes after the USB4/TBT session has closed. */ if ((!new_connected || was_disconnected) && (!cd321x->state_valid || cd321x->state.mode != TYPEC_STATE_SAFE)) { diff --git a/tools/asahi/atc-usb3-tests.py b/tools/asahi/atc-usb3-tests.py index 1403fa2a637c1..972dbd8deb517 100644 --- a/tools/asahi/atc-usb3-tests.py +++ b/tools/asahi/atc-usb3-tests.py @@ -9,7 +9,7 @@ T600x/T8112/T602x still match the T8103 PHY fallback but keep their previous single-poll/BIST/role behavior. OF compatible lookup is a modeled boundary. The DT-to-driver match contract is checked from the actual sources. -CIO ownership during USB host shutdown is covered on T8103 and T8132; T8132 lane setup +CIO ownership, PIPE parking and mux disconnect are covered on every modeled generation; T8132 lane setup and electrical behavior are not modeled. Hardware qualification remains necessary. Run --mutations for sensitivity checks. """ @@ -421,17 +421,22 @@ def block(source, marker): static void usb_stop_keeps_cio_until_mux_disconnect(void) { const enum atcphy_mode modes[] = { APPLE_ATCPHY_MODE_TBT, APPLE_ATCPHY_MODE_USB4 }; /* Every generation: T8112 and T600x run the T8103 PHY sequence. */ - const enum atcphy_generation gens[] = { ATCPHY_GENERATION_T8103, ATCPHY_GENERATION_T8132 }; - for (unsigned g=0;g<2;g++) for (unsigned i=0;i<2;i++) { + const enum atcphy_generation gens[] = { ATCPHY_GENERATION_T8103, ATCPHY_GENERATION_T8122, + ATCPHY_GENERATION_T8132 }; + for (unsigned g=0;gmode) && !atcphy->mode_dirty', 'atcphy->hw->gen == ATCPHY_GENERATION_T8132 && atcphy_mode_is_cio(atcphy->mode) && !atcphy->mode_dirty'), + 'usb4-stop-skips-pipe-park': ('atcphy_usb3_power_off', + 'ret = atcphy_park_pipehandler(atcphy, true);', + 'ret = atcphy->pipe_state == ATCPHY_PIPEHANDLER_STATE_USB4 ? 0 : atcphy_park_pipehandler(atcphy, true);'), + 'cio-mux-disconnect-keeps-lanes': ('atcphy_mux_switch', + 'ret = atcphy_configure(atcphy, target_mode);\n\tif (ret)', + 'ret = target_mode == APPLE_ATCPHY_MODE_OFF && atcphy_mode_is_cio(atcphy->mode) ? 0 : atcphy_configure(atcphy, target_mode);\n\tif (ret)'), 't8132-device-role-refused': ('atcphy_usb3_set_mode', 'if (usb2_device) {', 'if (usb2_device) {\n\t\t\treturn -EOPNOTSUPP;'), From 3130666ed186eb4070ebee19980594d252773ed7 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 29 Nov 2026 00:01:32 +1100 Subject: [PATCH 2/3] drm/apple: keep refusing routes with disabled suppliers An idempotent disable can return NULL when another refusal already disabled the route. Keep that distinct from an available supplier: the route must still be skipped. Cover a repeated supplier refusal in KUnit. Signed-off-by: Ryan Murray --- drivers/gpu/drm/apple/dcp-fabric-topology-test.c | 2 ++ drivers/gpu/drm/apple/dcp-fabric.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/apple/dcp-fabric-topology-test.c b/drivers/gpu/drm/apple/dcp-fabric-topology-test.c index fb06870ace9aa..4f42e76942f8f 100644 --- a/drivers/gpu/drm/apple/dcp-fabric-topology-test.c +++ b/drivers/gpu/drm/apple/dcp-fabric-topology-test.c @@ -406,6 +406,8 @@ static void topology_route_skip_test(struct kunit *test) /* A repeated refusal neither allocates a changeset nor replaces status. */ KUNIT_EXPECT_NULL(test, dcp_typec_routes_disable_nodes(dev, NULL, route_np)); KUNIT_EXPECT_NULL(test, dcp_typec_routes_disable_nodes(dev, route_np->parent, NULL)); + KUNIT_EXPECT_EQ(test, PTR_ERR(dcp_typec_route_skip(dev, route_np, "typec1")), + -ENODEV); KUNIT_EXPECT_FALSE(test, of_device_is_available(route_np)); /* diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index 2157cd4ade581..d0c11273b71ca 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -4928,11 +4928,14 @@ static struct of_changeset *dcp_typec_route_skip(struct device *dev, const char *name) { const char *what = dcp_typec_route_supplier_disabled(dev, name); + struct of_changeset *cs; if (!what) return NULL; dev_warn(dev, "%pOF: its %s is disabled, route skipped\n", route_np, what); - return dcp_typec_routes_disable_nodes(dev, NULL, route_np); + cs = dcp_typec_routes_disable_nodes(dev, NULL, route_np); + /* A concurrent refusal may already have disabled it; still skip it. */ + return cs ?: ERR_PTR(-ENODEV); } static int dcp_register_typec_routes_present(void *data) From 77ed8556a2cf33457abc5ae51db40046468c61cb Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 3 Dec 2026 23:45:29 +1100 Subject: [PATCH 3/3] thunderbolt: apple: check a rate-limited lost link again after the window The per-port limit of three automatic restarts a minute returned for good on the fourth lost link. A flapping peer that then settled never got its link back: on a J616s with a host-to-host USB4 peer, the peer's controller came back each time, but the J616s port stayed without a link until a replug, because only a restart from this side brings it up again. Re-arm the link-lost check for the end of the window when the limit refuses a restart. A peer that is present by then is left alone; one that is still missing gets a normal restart, inside a fresh window. Also keep the ratelimit's "callbacks suppressed" report quiet next to our own warning. Signed-off-by: Ryan Murray --- drivers/thunderbolt/apple.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 06da2b98d0a48..ca11e5085cc6e 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -357,7 +357,9 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { #define APPLE_CIO_LINK_LOST_CYCLES 3 /* * Recovery policy: three automatic port cycles per one-minute window, even - * when a flapping peer completes discovery between link losses. + * when a flapping peer completes discovery between link losses. A loss past + * the limit is checked again when the window ends, so a peer that settles + * still gets its link back without a replug. */ #define APPLE_CIO_LINK_LOST_INTERVAL (60 * HZ) @@ -3973,8 +3975,16 @@ static void apple_cio_link_lost_work(struct work_struct *work) } /* Discovery refills the consecutive budget, not this rate limit. */ if (!__ratelimit(&acio->link_lost_limit)) { - dev_warn(acio->dev, "peer link keeps dropping, port restart rate limit reached\n"); + dev_warn(acio->dev, + "peer link keeps dropping, next port restart check in %d s\n", + APPLE_CIO_LINK_LOST_INTERVAL / HZ); acio->sleep_peer_wanted = false; + /* + * Look again once the window is over: a peer that has + * settled by then gets its port restart. + */ + mod_delayed_work(system_freezable_wq, &acio->link_lost_work, + APPLE_CIO_LINK_LOST_INTERVAL); return; } acio->link_lost_cycles++; @@ -4764,6 +4774,7 @@ static int apple_cio_probe(struct platform_device *pdev) INIT_DELAYED_WORK(&acio->link_lost_work, apple_cio_link_lost_work); ratelimit_state_init(&acio->link_lost_limit, APPLE_CIO_LINK_LOST_INTERVAL, APPLE_CIO_LINK_LOST_CYCLES); + ratelimit_set_flags(&acio->link_lost_limit, RATELIMIT_MSG_ON_RELEASE); INIT_DELAYED_WORK(&acio->sleep_restart_work, apple_cio_sleep_restart_work); INIT_WORK(&acio->sleep_dp_undo_work, apple_cio_sleep_dp_undo_work); init_completion(&acio->nhi_boot_completion);