From 63d875d9e91bc3404f9f6d3cce490b231fdafae6 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 16:32:28 -0500 Subject: [PATCH 01/11] bus: simple-pm-bus: bind T6030 PMGR register banks t6030.dtsi lists its PMGR register banks as "apple,t6030-pmgr", "apple,t8103-pmgr", "syscon", "simple-mfd". Because simple_pm_bus_probe() refuses ONLY_BUS matches that are not the first compatible string, the T6030 PMGR banks never bound, leaving sync_state() permanently pending on ps_atc{0,1,2,3}_common (due to pmgr_mini) and blocking genpd from clearing GENPD_FLAG_DEFER_OFF. Add "apple,t6030-pmgr" to simple_pm_bus_of_match[] and extend tools/asahi/pmgr-bus-tests.py with T6030 positive and negative checks. Signed-off-by: DjDeveloperr --- drivers/bus/simple-pm-bus.c | 1 + tools/asahi/pmgr-bus-tests.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/bus/simple-pm-bus.c b/drivers/bus/simple-pm-bus.c index b3545bb256ef5..57183c3a199e7 100644 --- a/drivers/bus/simple-pm-bus.c +++ b/drivers/bus/simple-pm-bus.c @@ -153,6 +153,7 @@ static const struct of_device_id simple_pm_bus_of_match[] = { { .compatible = "apple,t8132-pmgr", .data = ONLY_BUS }, { .compatible = "apple,t6000-pmgr", .data = ONLY_BUS }, { .compatible = "apple,t6020-pmgr", .data = ONLY_BUS }, + { .compatible = "apple,t6030-pmgr", .data = ONLY_BUS }, { .compatible = "apple,t6041-pmgr", .data = ONLY_BUS }, { .compatible = "fsl,ls1021a-scfg", }, { .compatible = "fsl,ls1043a-scfg", }, diff --git a/tools/asahi/pmgr-bus-tests.py b/tools/asahi/pmgr-bus-tests.py index 821e394441a27..94fc6fb1201df 100644 --- a/tools/asahi/pmgr-bus-tests.py +++ b/tools/asahi/pmgr-bus-tests.py @@ -88,6 +88,9 @@ struct device_node t8122 = { { "apple,t8122-pmgr", "apple,t8103-pmgr", "syscon", "simple-mfd" } }; + struct device_node t6030 = { + { "apple,t6030-pmgr", "apple,t8103-pmgr", "syscon", "simple-mfd" } + }; struct device_node t8103 = { { "apple,t8103-pmgr", "apple,pmgr", "syscon", "simple-mfd" } }; @@ -98,6 +101,7 @@ }; check(&t6041, 0, false); check(&t8122, 0, false); + check(&t6030, 0, false); check(&t8103, 0, false); check(&bus, 0, false); check(&dedicated, -ENODEV, false); @@ -119,7 +123,7 @@ def main(): source.index('MODULE_DEVICE_TABLE(of, simple_pm_bus_of_match)')] with tempfile.TemporaryDirectory(prefix='pmgr-bus-') as tmp: tmp = Path(tmp) - for removed in (None, 'apple,t6041-pmgr', 'apple,t8122-pmgr'): + for removed in (None, 'apple,t6041-pmgr', 'apple,t8122-pmgr', 'apple,t6030-pmgr'): selected = table if removed: selected = re.sub( From 07e06833439c9b74a954d58e624ad16eb34d0a51 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 16:32:28 -0500 Subject: [PATCH 02/11] mux: apple-display-crossbar: enable runtime PM on T6030 On T6030, the four ATC display crossbars sit in ps_atc{0,1,2,3}_usb and previously fell back to the non-RPM T6020 hw table, holding the ATC USB and common power domains powered continuously even when no output is selected. Add apple_dpxbar_hw_t6030 with t6030_flow, no_tunable, and runtime_pm enabled and match "apple,t6030-display-crossbar" ahead of the T6020 fallback, matching T8122, T8132, and T6041. Signed-off-by: DjDeveloperr --- drivers/mux/apple-display-crossbar.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index 92faaa3d4f801..8d5dba6f0533a 100644 --- a/drivers/mux/apple-display-crossbar.c +++ b/drivers/mux/apple-display-crossbar.c @@ -365,7 +365,7 @@ static int apple_dpxbar_t6030_dpin_set(struct mux_control *mux, int state) /* * A crossbar in an ATC power domain that switches off at runtime (T8122, - * T8132, T6041): the domain is held on while an output is selected, and only + * T8132, T6030, T6041): the domain is held on while an output is selected, and only * then are its registers touched. The mux core serialises the calls for one * output. */ @@ -1642,6 +1642,20 @@ static const struct apple_dpxbar_hw apple_dpxbar_hw_t8122 = { .runtime_pm = true, }; +/* + * T6030 (M3 Pro): one crossbar per ATC, using the T602X register map and + * T6030 clock-on-link flow, while letting the ATC USB power domain switch + * off at runtime when no output is selected; probe touches no register. + */ +static const struct apple_dpxbar_hw apple_dpxbar_hw_t6030 = { + .n_mux = MUX_MAX, + .n_ufp = 9, + .ops = &apple_dpxbar_t602x_ops, + .t6030_flow = true, + .no_tunable = true, + .runtime_pm = true, +}; + /* * T6040/T6041 (M4 Pro/Max): one crossbar per ATC (ADT atcN-dpxbar, * "atc-dpxbar,t604x"), 0x4000 bytes, no tunable. The block uses the T6030 @@ -1711,6 +1725,10 @@ static const struct of_device_id apple_dpxbar_ids[] = { .compatible = "apple,t8122-display-crossbar", .data = &apple_dpxbar_hw_t8122, }, + { + .compatible = "apple,t6030-display-crossbar", + .data = &apple_dpxbar_hw_t6030, + }, { .compatible = "apple,t6020-display-crossbar", .data = &apple_dpxbar_hw_t6020, From 59c39170ce9ce32fd60de46bf977ec49fd3003ad Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 16:32:28 -0500 Subject: [PATCH 03/11] phy: apple: atc: enable T6030 idle power-off Enable runtime idle power-off for T6030 (M3 Pro) ATC PHYs: - Guard apple_atc_dp_tunnel_rate_source() against touching core MMIO when any idle_rpm PHY is outside USB4/TBT mode (matching the existing T8132 guard; leaving CIO mode already stops the tunnel clock). - Allow DP-only PHYs in atcphy_pd_can_power_off() when their attached power domain (ps_atcN_common) lacks apple,always-on even though its fabric bus parent (ps_lw30) is always-on. - Advertise apple,idle-power-off on atcphy0, atcphy1, atcphy2, and the HDMI DP-only atcphy3 in t6030.dtsi (atcphy_hw_t6030 already sets restore_after_pd_off and sleep_drops_common), and drop the apple,always-on override on ps_atc3_common in t603x-j514s-j516s-hdmi.dtsi now that atcphy3 and atcphy3_xbar manage ps_atc3_common dynamically and restore after power loss. - Extend atc-idle-pm-test.dtso and atcphy_test_idle_power_off_dt() in atc-test.c with T6030 USB-C and DP-only positive/negative cases. Signed-off-by: DjDeveloperr --- arch/arm64/boot/dts/apple/t6030.dtsi | 4 ++ .../dts/apple/t603x-j514s-j516s-hdmi.dtsi | 5 --- drivers/phy/apple/atc-idle-pm-test.dtso | 28 +++++++++++++ drivers/phy/apple/atc-test.c | 40 ++++++++++++++----- drivers/phy/apple/atc.c | 22 ++++++---- 5 files changed, 75 insertions(+), 24 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6030.dtsi b/arch/arm64/boot/dts/apple/t6030.dtsi index 602989d8c1115..bc04a4e889112 100644 --- a/arch/arm64/boot/dts/apple/t6030.dtsi +++ b/arch/arm64/boot/dts/apple/t6030.dtsi @@ -2291,6 +2291,7 @@ orientation-switch; mode-switch; + apple,idle-power-off; power-domains = <&ps_atc0_usb>; }; @@ -2467,6 +2468,7 @@ orientation-switch; mode-switch; + apple,idle-power-off; power-domains = <&ps_atc1_usb>; }; @@ -2643,6 +2645,7 @@ orientation-switch; mode-switch; + apple,idle-power-off; power-domains = <&ps_atc2_usb>; }; @@ -2665,6 +2668,7 @@ orientation-switch; mode-switch; + apple,idle-power-off; power-domains = <&ps_atc3_common>; status = "disabled"; }; diff --git a/arch/arm64/boot/dts/apple/t603x-j514s-j516s-hdmi.dtsi b/arch/arm64/boot/dts/apple/t603x-j514s-j516s-hdmi.dtsi index a9f5ceae6fa1c..c6e680b30bcb1 100644 --- a/arch/arm64/boot/dts/apple/t603x-j514s-j516s-hdmi.dtsi +++ b/arch/arm64/boot/dts/apple/t603x-j514s-j516s-hdmi.dtsi @@ -102,8 +102,3 @@ &sio_mbox { status = "okay"; }; - -/* Nothing else holds ATC3 on across sleep, which loses the PHY state. */ -&ps_atc3_common { - apple,always-on; -}; diff --git a/drivers/phy/apple/atc-idle-pm-test.dtso b/drivers/phy/apple/atc-idle-pm-test.dtso index 0ed48f196c74d..df58e5df39768 100644 --- a/drivers/phy/apple/atc-idle-pm-test.dtso +++ b/drivers/phy/apple/atc-idle-pm-test.dtso @@ -94,9 +94,37 @@ power-domains = <&ps_atc1_usb>; }; + ps_t6030_lw30: power-controller@c { + #power-domain-cells = <0>; + apple,always-on; + }; + + ps_t6030_atc3_common: power-controller@d { + #power-domain-cells = <0>; + power-domains = <&ps_t6030_lw30>; + }; + + ps_t6030_atc3_common_aon: power-controller@e { + #power-domain-cells = <0>; + apple,always-on; + power-domains = <&ps_t6030_lw30>; + }; + atcphy0-t8132 { compatible = "apple,t8132-atcphy"; power-domains = <&ps_t8132_usb>; }; + + atcphy3-t6030-dp { + compatible = "apple,t6030-atcphy"; + apple,idle-power-off; + power-domains = <&ps_t6030_atc3_common>; + }; + + atcphy3-t6030-dp-aon { + compatible = "apple,t6030-atcphy"; + apple,idle-power-off; + power-domains = <&ps_t6030_atc3_common_aon>; + }; }; }; diff --git a/drivers/phy/apple/atc-test.c b/drivers/phy/apple/atc-test.c index e2c880e4c071f..c90e15df1f85c 100644 --- a/drivers/phy/apple/atc-test.c +++ b/drivers/phy/apple/atc-test.c @@ -1539,45 +1539,63 @@ static void atcphy_test_idle_power_off_dt(struct kunit *test) of_node_put_kunit(test, np); hw = atcphy_test_match_hw(test, np); KUNIT_EXPECT_PTR_EQ(test, hw, &atcphy_hw_t8103); - KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np, false)); np = of_find_node_by_path("/atc-idle-pm-test/atcphy1-t8112-capable"); KUNIT_ASSERT_NOT_NULL(test, np); of_node_put_kunit(test, np); hw = atcphy_test_match_hw(test, np); - KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(hw, np)); + KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(hw, np, false)); np = of_find_node_by_path("/atc-idle-pm-test/atcphy0-t8103"); KUNIT_ASSERT_NOT_NULL(test, np); of_node_put_kunit(test, np); hw = atcphy_test_match_hw(test, np); - KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(hw, np)); - /* T8122 uses the same DT opt-in as T8103. */ - KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(&atcphy_hw_t8122, np)); + KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(hw, np, false)); + /* T8122 and T6030 use the same DT opt-in as T8103. */ + KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(&atcphy_hw_t8122, np, false)); + KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(&atcphy_hw_t6030, np, false)); /* Variants without state restoration never idle-power-off even if DT asks. */ - KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t8152, np)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t8152, np, false)); np = of_find_node_by_path("/atc-idle-pm-test/atcphy0-t8103-legacy"); KUNIT_ASSERT_NOT_NULL(test, np); of_node_put_kunit(test, np); hw = atcphy_test_match_hw(test, np); - KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np)); - KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t8122, np)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np, false)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t8122, np, false)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t6030, np, false)); np = of_find_node_by_path("/atc-idle-pm-test/atcphy0-t8103-aon"); KUNIT_ASSERT_NOT_NULL(test, np); of_node_put_kunit(test, np); hw = atcphy_test_match_hw(test, np); - KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np, false)); np = of_find_node_by_path("/atc-idle-pm-test/atcphy0-t8132"); KUNIT_ASSERT_NOT_NULL(test, np); of_node_put_kunit(test, np); hw = atcphy_test_match_hw(test, np); KUNIT_EXPECT_PTR_EQ(test, hw, &atcphy_hw_t8132); - KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(hw, np)); + KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(hw, np, false)); - KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t8103, NULL)); + /* T6030 DP-only PHY attaches directly to ps_atc3_common under always-on ps_lw30. */ + np = of_find_node_by_path("/atc-idle-pm-test/atcphy3-t6030-dp"); + KUNIT_ASSERT_NOT_NULL(test, np); + of_node_put_kunit(test, np); + hw = atcphy_test_match_hw(test, np); + KUNIT_EXPECT_PTR_EQ(test, hw, &atcphy_hw_t6030); + KUNIT_EXPECT_TRUE(test, atcphy_can_idle_power_off(hw, np, true)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np, false)); + + np = of_find_node_by_path("/atc-idle-pm-test/atcphy3-t6030-dp-aon"); + KUNIT_ASSERT_NOT_NULL(test, np); + of_node_put_kunit(test, np); + hw = atcphy_test_match_hw(test, np); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(hw, np, true)); + + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t8103, NULL, false)); + KUNIT_EXPECT_FALSE(test, atcphy_can_idle_power_off(&atcphy_hw_t6030, NULL, true)); } static struct kunit_case atcphy_mux_test_cases[] = { diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index d5912f1e71605..af39500cccb16 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -5728,13 +5728,13 @@ int apple_atc_dp_tunnel_rate_source(struct phy *phy, unsigned int source, return -EOPNOTSUPP; guard(mutex)(&atcphy->lock); /* - * The T8132 ATC domain goes off at runtime unless the PHY is in a CIO - * mode, and leaving one stops the tunnel clock (atcphy_configure_t8132()): - * outside one there is nothing to stop and no register to touch. + * An idle-power-off ATC domain goes off at runtime unless the PHY is in + * a CIO mode, and leaving one stops the tunnel clock: outside one there + * is nothing to stop and no register to touch. */ if (atcphy->cio_sleep.changed && !atcphy->cio_sleep.restored) return -EBUSY; - if (atcphy->hw->gen == ATCPHY_GENERATION_T8132 && + if ((atcphy->hw->gen == ATCPHY_GENERATION_T8132 || atcphy->idle_rpm) && atcphy->mode != APPLE_ATCPHY_MODE_USB4 && atcphy->mode != APPLE_ATCPHY_MODE_TBT) { if (rate) @@ -8587,8 +8587,11 @@ static int atcphy_probe_power_domains(struct apple_atcphy *atcphy) * neither the domain itself nor all of its parent domains carry * apple,always-on (where ps_atcN_usb has ps_atcN_usb_aon plus ps_atcN_common, * idle power-off drops ps_atcN_common while ps_atcN_usb_aon stays on for DWC3). + * A DP-only PHY attaches directly to ps_atcN_common, whose parent is an + * always-on SoC fabric bus, so the domain itself lacking apple,always-on + * suffices. */ -static bool atcphy_pd_can_power_off(const struct device_node *np) +static bool atcphy_pd_can_power_off(const struct device_node *np, bool dp_only) { struct device_node *pd_np __free(device_node) = of_parse_phandle(np, "power-domains", 0); struct of_phandle_iterator it; @@ -8597,6 +8600,8 @@ static bool atcphy_pd_can_power_off(const struct device_node *np) if (!pd_np || of_property_read_bool(pd_np, "apple,always-on")) return false; + if (dp_only) + return true; of_for_each_phandle(&it, err, pd_np, "power-domains", "#power-domain-cells", -1) { has_parents = true; if (!of_property_read_bool(it.node, "apple,always-on")) { @@ -8616,14 +8621,15 @@ static bool atcphy_pd_can_power_off(const struct device_node *np) * hierarchy allows power-off. */ static bool atcphy_can_idle_power_off(const struct atcphy_hw *hw, - const struct device_node *np) + const struct device_node *np, + bool dp_only) { if (!hw->restore_after_pd_off) return false; if (!hw->idle_power_off && !of_property_read_bool(np, "apple,idle-power-off")) return false; - return atcphy_pd_can_power_off(np); + return atcphy_pd_can_power_off(np, dp_only); } static void atcphy_sleep_finish_work(struct work_struct *work); @@ -8698,7 +8704,7 @@ static int atcphy_probe(struct platform_device *pdev) * hierarchy may let it drop. Before the PHYs are created, so that they * follow runtime PM too. */ - if (followed && atcphy_can_idle_power_off(atcphy->hw, dev->of_node)) { + if (followed && atcphy_can_idle_power_off(atcphy->hw, dev->of_node, atcphy->dp_only)) { pm_runtime_set_autosuspend_delay(dev, ATCPHY_AUTOSUSPEND_MS); pm_runtime_use_autosuspend(dev); ret = devm_pm_runtime_set_active_enabled(dev); From cd646ab6417585e92eef2d773f00ac6f9fa16f25 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 18:02:19 -0500 Subject: [PATCH 04/11] phy: apple: atc: propagate cold-restore errors and release crossbar RPM on unbind Propagate failures from atcphy_restore_after_pd_off() instead of clearing pd_was_off early and letting callers proceed: - Defer clearing pd_was_off until cold restore completes cleanly. - Preserve atcphy->mode on power-off, PIPE-parking, and mode-replay failures; keep pipe_pending set rather than falsely recording ATCPHY_PIPEHANDLER_STATE_DUMMY when PIPE parking fails. - Propagate the error across callers including atcphy_dwc3_reset_deassert() and atcphy_resume(), and update host-test stubs/fixtures accordingly. - Add KUnit regression tests covering T6030 cold-restore PIPE parking timeout and DP-only mode-replay failure. In apple-display-crossbar, register a devm cleanup action right after devm_pm_runtime_enable() to serialize against active mux operations, mark the provider removed, and drop any runtime-PM references still held for selected or preselected outputs before runtime PM is disabled on unbind. Signed-off-by: DjDeveloperr --- drivers/mux/apple-display-crossbar.c | 52 +++++++++- drivers/phy/apple/atc-test.c | 89 ++++++++++++++++- drivers/phy/apple/atc.c | 99 +++++++++++++------ tools/asahi/atc-dp-drive-tests.py | 2 +- tools/asahi/atc-usb3-tests.py | 2 +- tools/asahi/t6030-clock/fixture.c | 4 +- .../selftests/drivers/apple/atc-t8132-usb3.py | 2 +- .../drivers/gpu/apple-dcp/dwc3/mocks.c | 2 +- .../selftests/drivers/gpu/apple-dcp/phy.py | 4 +- .../drivers/gpu/apple-dcp/quiesce.py | 2 +- 10 files changed, 216 insertions(+), 42 deletions(-) diff --git a/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index 8d5dba6f0533a..0609d7e11502d 100644 --- a/drivers/mux/apple-display-crossbar.c +++ b/drivers/mux/apple-display-crossbar.c @@ -9,7 +9,9 @@ #include #include +#include #include +#include #include #include #include @@ -132,6 +134,8 @@ struct apple_dpxbar { bool uhbr[T8142_MUX_MAX]; bool enabled[T8142_MUX_MAX]; bool faulted; + bool removed; + struct mutex rpm_lock; spinlock_t lock; }; @@ -379,6 +383,10 @@ static int apple_dpxbar_rpm_set(struct mux_control *mux, int state) if (state != MUX_IDLE_DISCONNECT && (state < 0 || state >= 9)) return -EINVAL; + + guard(mutex)(&xbar->rpm_lock); + if (xbar->removed) + return state == MUX_IDLE_DISCONNECT ? 0 : -ENODEV; if (state == MUX_IDLE_DISCONNECT && !test_bit(index, &xbar->rpm_held)) { xbar->selected_dispext[index] = MUX_IDLE_DISCONNECT; return 0; @@ -650,6 +658,10 @@ static int apple_dpxbar_set(struct mux_control *mux, int state) bool rpm_temp = false; unsigned int rpm_puts = 0; + guard(mutex)(&dpxbar->rpm_lock); + if (dpxbar->removed) + return enable ? -ENODEV : 0; + if (dpxbar->hw->runtime_pm) { if (!enable && dpxbar->probed) { bool need_mmio; @@ -909,6 +921,10 @@ int apple_dpxbar_preselect(struct mux_control *mux, int state) unsigned int rpm_puts = 0; bool rpm_temp = false; + guard(mutex)(&dpxbar->rpm_lock); + if (dpxbar->removed) + return state == MUX_IDLE_DISCONNECT ? 0 : -ENODEV; + if (dpxbar->hw->runtime_pm) { if (state == MUX_IDLE_DISCONNECT) { bool active; @@ -987,7 +1003,9 @@ int apple_dpxbar_tunnel_select_source(struct mux_control *mux, int state) xbar = mux_chip_priv(mux->chip); spin_lock_irqsave(&xbar->lock, flags); - if (xbar->selected_dispext[index] >= 0 && + if (xbar->removed) { + ret = -ENODEV; + } else if (xbar->selected_dispext[index] >= 0 && xbar->selected_dispext[index] != state) { ret = -EBUSY; } else if (xbar->hw->runtime_pm && xbar->selected_dispext[index] < 0) { @@ -1412,6 +1430,34 @@ static void apple_dpxbar_domain_release(void *data) dev_pm_domain_detach(pd, true); } +/* + * Release any runtime-PM references still held for selected or preselected + * outputs when the provider unbinds, before devm_pm_runtime_enable() disables + * runtime PM. Any consumer handle still alive sees xbar->removed and becomes + * a no-op on deselect instead of touching unmapped registers or PM state. + */ +static void apple_dpxbar_rpm_release(void *data) +{ + struct apple_dpxbar *xbar = data; + unsigned long flags; + unsigned int i, rpm_puts = 0; + + guard(mutex)(&xbar->rpm_lock); + spin_lock_irqsave(&xbar->lock, flags); + xbar->removed = true; + for (i = 0; i < xbar->hw->n_mux; i++) { + if (test_and_clear_bit(i, &xbar->rpm_held)) + rpm_puts++; + if (test_and_clear_bit(i, &xbar->rpm_preselect)) + rpm_puts++; + xbar->selected_dispext[i] = MUX_IDLE_DISCONNECT; + } + spin_unlock_irqrestore(&xbar->lock, flags); + + while (rpm_puts--) + pm_runtime_put_sync_suspend(xbar->dev); +} + static void apple_dpxbar_common_domain_release(void *data) { struct apple_dpxbar *xbar = data; @@ -1479,6 +1525,7 @@ static int apple_dpxbar_probe(struct platform_device *pdev) dpxbar = mux_chip_priv(mux_chip); dpxbar->hw = hw; mux_chip->ops = hw->ops; + mutex_init(&dpxbar->rpm_lock); spin_lock_init(&dpxbar->lock); dpxbar->dev = dev; @@ -1496,6 +1543,9 @@ static int apple_dpxbar_probe(struct platform_device *pdev) ret = devm_pm_runtime_enable(dev); if (ret) return ret; + ret = devm_add_action_or_reset(dev, apple_dpxbar_rpm_release, dpxbar); + if (ret) + return ret; } if (hw->ops == &apple_dpxbar_ops && !hw->no_tunable && diff --git a/drivers/phy/apple/atc-test.c b/drivers/phy/apple/atc-test.c index c90e15df1f85c..cbb1b1f189b79 100644 --- a/drivers/phy/apple/atc-test.c +++ b/drivers/phy/apple/atc-test.c @@ -1437,7 +1437,7 @@ static void atcphy_test_t8132_restore_idle(struct kunit *test) test->priv = &config; kunit_activate_static_stub(test, atcphy_configure, atcphy_test_configure_record); scoped_guard(mutex, &atcphy->lock) - atcphy_restore_after_pd_off(atcphy); + KUNIT_EXPECT_EQ(test, atcphy_restore_after_pd_off(atcphy), 0); KUNIT_EXPECT_FALSE(test, atcphy->pd_was_off); KUNIT_EXPECT_EQ(test, config.calls, 0U); @@ -1454,7 +1454,7 @@ static void atcphy_test_t8132_restore_idle(struct kunit *test) /* Nothing to do once the state is back. */ scoped_guard(mutex, &atcphy->lock) - atcphy_restore_after_pd_off(atcphy); + KUNIT_EXPECT_EQ(test, atcphy_restore_after_pd_off(atcphy), 0); KUNIT_EXPECT_EQ(test, config.calls, 0U); } @@ -1467,7 +1467,7 @@ static void atcphy_test_t8132_restore_mode(struct kunit *test) test->priv = &config; kunit_activate_static_stub(test, atcphy_configure, atcphy_test_configure_record); scoped_guard(mutex, &atcphy->lock) - atcphy_restore_after_pd_off(atcphy); + KUNIT_EXPECT_EQ(test, atcphy_restore_after_pd_off(atcphy), 0); KUNIT_EXPECT_EQ(test, config.calls, 1U); KUNIT_EXPECT_EQ(test, config.lane_requests, 1U); @@ -1476,6 +1476,87 @@ static void atcphy_test_t8132_restore_mode(struct kunit *test) PIPEHANDLER_AON_GEN_DWC3_RESET_N); } +static int atcphy_test_configure_timeout(struct apple_atcphy *atcphy, enum atcphy_mode mode) +{ + struct kunit *test = kunit_get_current_test(); + struct atcphy_test_configure *config = test->priv; + + lockdep_assert_held(&atcphy->lock); + config->calls++; + return -ETIMEDOUT; +} + +static struct apple_atcphy *atcphy_test_alloc_t6030_pd_off(struct kunit *test, + enum atcphy_mode mode, + bool dp_only) +{ + struct apple_atcphy *atcphy = atcphy_test_alloc_powered_off(test); + void *core = kunit_kzalloc(test, ATCPHY_MISC + 4, GFP_KERNEL); + void *pipehandler = kunit_kzalloc(test, PIPEHANDLER_T8132_USB2_CONFIG + 4, GFP_KERNEL); + void *usb2phy = kunit_kzalloc(test, USB2PHY_MISCTUNE + 4, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, core); + KUNIT_ASSERT_NOT_NULL(test, pipehandler); + KUNIT_ASSERT_NOT_NULL(test, usb2phy); + atcphy->regs.core = (__force void __iomem *)core; + atcphy->regs.pipehandler = (__force void __iomem *)pipehandler; + atcphy->regs.usb2phy = (__force void __iomem *)usb2phy; + atcphy->hw = &atcphy_hw_t6030; + atcphy->dp_only = dp_only; + atcphy->mode = mode; + atcphy->pd_was_off = true; + dev_set_drvdata(atcphy->dev, atcphy); + + return atcphy; +} + +/* + * A failed PIPE park during T6030 cold restore must keep pd_was_off pending, + * avoid falsely recording DUMMY, and prevent DWC3 reset deassert from releasing + * the controller. + */ +static void atcphy_test_t6030_restore_pipe_park_fail(struct kunit *test) +{ + struct apple_atcphy *atcphy = atcphy_test_alloc_t6030_pd_off(test, APPLE_ATCPHY_MODE_OFF, + false); + + atcphy->pipe_state = ATCPHY_PIPEHANDLER_STATE_USB3; + writel(PIPEHANDLER_LOCK_EN, atcphy->regs.pipehandler + PIPEHANDLER_LOCK_ACK); + + KUNIT_EXPECT_EQ(test, atcphy_dwc3_reset_deassert(&atcphy->rcdev, 0), -ETIMEDOUT); + KUNIT_EXPECT_TRUE(test, atcphy->pd_was_off); + KUNIT_EXPECT_TRUE(test, atcphy->pipe_pending); + KUNIT_EXPECT_NE(test, atcphy->pipe_state, ATCPHY_PIPEHANDLER_STATE_DUMMY); + KUNIT_EXPECT_FALSE(test, atcphy->dwc3_running); + KUNIT_EXPECT_FALSE(test, readl(atcphy->regs.pipehandler + PIPEHANDLER_AON_GEN) & + PIPEHANDLER_AON_GEN_DWC3_RESET_N); +} + +/* + * A failed mode replay during DP-only resume must preserve the target mode and + * pd_was_off, propagate the error, and replay the mode on the next restore. + */ +static void atcphy_test_t6030_restore_dp_mode_fail(struct kunit *test) +{ + struct apple_atcphy *atcphy = atcphy_test_alloc_t6030_pd_off(test, APPLE_ATCPHY_MODE_DP, + true); + struct atcphy_test_configure config = {}; + + test->priv = &config; + kunit_activate_static_stub(test, atcphy_configure, atcphy_test_configure_timeout); + KUNIT_EXPECT_EQ(test, atcphy_resume(atcphy->dev), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, config.calls, 1U); + KUNIT_EXPECT_EQ(test, atcphy->mode, APPLE_ATCPHY_MODE_DP); + KUNIT_EXPECT_TRUE(test, atcphy->pd_was_off); + + kunit_activate_static_stub(test, atcphy_configure, atcphy_test_configure_record); + KUNIT_EXPECT_EQ(test, atcphy_resume(atcphy->dev), 0); + KUNIT_EXPECT_EQ(test, config.calls, 2U); + KUNIT_EXPECT_EQ(test, config.lane_requests, 1U); + KUNIT_EXPECT_EQ(test, atcphy->mode, APPLE_ATCPHY_MODE_DP); + KUNIT_EXPECT_FALSE(test, atcphy->pd_was_off); +} + static void atcphy_test_t8103_usb3_machine_scope(struct kunit *test) { static const struct { @@ -1654,6 +1735,8 @@ static struct kunit_case atcphy_t8132_pm_test_cases[] = { KUNIT_CASE(atcphy_test_t8132_rpm_hold), KUNIT_CASE(atcphy_test_t8132_restore_idle), KUNIT_CASE(atcphy_test_t8132_restore_mode), + KUNIT_CASE(atcphy_test_t6030_restore_pipe_park_fail), + KUNIT_CASE(atcphy_test_t6030_restore_dp_mode_fail), #ifdef CONFIG_SUSPEND KUNIT_CASE(atcphy_test_t8132_system_suspend), #endif diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index af39500cccb16..8914f07498ea6 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -3009,7 +3009,7 @@ static int atcphy_auspll_apb_command(struct apple_atcphy *atcphy, u32 command) return ret; } -static void atcphy_restore_after_pd_off(struct apple_atcphy *atcphy); +static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy); static int atcphy_dp_stop(struct apple_atcphy *atcphy) { @@ -3030,7 +3030,9 @@ static int atcphy_dp_stop(struct apple_atcphy *atcphy) if (atcphy->dp_link_rate < 0) return 0; - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; mode_cfg = atcphy_get_mode_config(atcphy, atcphy->mode); core_set32(atcphy, ACIOPHY_LANE_DP_CFG_BLK_TX_DP_CTRL0, DP_PMA_BYTECLK_RESET); @@ -3069,7 +3071,9 @@ static int atcphy_dp_configure(struct apple_atcphy *atcphy, enum atcphy_dp_link_ /* the AUSPLL is in use as the Thunderbolt DP tunnel pixel clock */ if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) return -EBUSY; - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; mode_cfg = atcphy_get_mode_config(atcphy, atcphy->mode); cfg = &dp_lr_config[lr]; @@ -4989,7 +4993,7 @@ static void atcphy_forget_common_state(struct apple_atcphy *atcphy) * On T8132 the domain also switches off at runtime, once an idle PHY and its * USB controller have both suspended, see atcphy_rpm_get(). */ -static void atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) +static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) { enum atcphy_mode mode = atcphy->mode; int ret; @@ -4998,10 +5002,9 @@ static void atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) /* The CIO host restores this epoch before restarting its controller. */ if (atcphy->cio_sleep.prepared) - return; + return 0; if (!atcphy->hw->restore_after_pd_off || !READ_ONCE(atcphy->pd_was_off)) - return; - WRITE_ONCE(atcphy->pd_was_off, false); + return 0; atcphy_forget_common_state(atcphy); @@ -5013,9 +5016,12 @@ static void atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) /* As at probe, M4 acquires its common bank only on USB3 admission. */ if (!atcphy->hw->usb2_only) { ret = atcphy_power_off(atcphy); - if (ret) + if (ret) { dev_warn(atcphy->dev, "PHY did not power down after its domain was off: %d\n", ret); + atcphy->mode = mode; + return ret; + } } atcphy->mode = APPLE_ATCPHY_MODE_OFF; @@ -5027,10 +5033,15 @@ static void atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) atcphy_setup_pipehandler(atcphy); } else { ret = atcphy_configure_pipehandler_dummy(atcphy, false); - if (ret) + if (ret) { dev_warn(atcphy->dev, "Failed to park the PIPE after its domain was off: %d\n", ret); + atcphy->pipe_pending = true; + atcphy->mode = mode; + return ret; + } + atcphy->pipe_pending = false; atcphy->pipe_state = ATCPHY_PIPEHANDLER_STATE_DUMMY; } dev_dbg(atcphy->dev, @@ -5052,20 +5063,30 @@ static void atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) set32(atcphy->regs.usb2phy + USB2PHY_SIG, atcphy_usb2_sig_host(atcphy)); } - if (mode == APPLE_ATCPHY_MODE_OFF) - return; - ret = atcphy_configure(atcphy, mode); - if (ret) - dev_warn(atcphy->dev, "Failed to restore mode %d after its domain was off: %d\n", - mode, ret); + if (mode != APPLE_ATCPHY_MODE_OFF) { + ret = atcphy_configure(atcphy, mode); + if (ret) { + dev_warn(atcphy->dev, + "Failed to restore mode %d after its domain was off: %d\n", + mode, ret); + atcphy->mode = mode; + return ret; + } + } + + WRITE_ONCE(atcphy->pd_was_off, false); + return 0; } static int atcphy_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode) { struct apple_atcphy *atcphy = phy_get_drvdata(phy); + int ret; guard(mutex)(&atcphy->lock); - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; if (mode != PHY_MODE_USB_HOST && mode != PHY_MODE_USB_DEVICE) return -EINVAL; @@ -5094,9 +5115,12 @@ static int atcphy_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode static int atcphy_usb2_init(struct phy *phy) { struct apple_atcphy *atcphy = phy_get_drvdata(phy); + int ret; guard(mutex)(&atcphy->lock); - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; if (atcphy->usb_off_pending) return -EBUSY; if (atcphy->pipe_pending && @@ -5194,7 +5218,9 @@ static int atcphy_usb3_power_off(struct phy *phy) int ret; guard(mutex)(&atcphy->lock); - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; /* * dwc3 has left its role: nothing drives the PIPE any more, and the @@ -5306,7 +5332,9 @@ static int atcphy_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode int ret; guard(mutex)(&atcphy->lock); - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; if (atcphy->usb_off_pending) return -EBUSY; @@ -5574,7 +5602,9 @@ static int atcphy_dpphy_configure_t8122(struct apple_atcphy *atcphy, int ret; guard(mutex)(&atcphy->lock); - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; if (opts->set_lanes) return -EINVAL; @@ -5615,7 +5645,9 @@ static int atcphy_dpphy_configure(struct phy *phy, union phy_configure_opts *opt if (atcphy->mode != APPLE_ATCPHY_MODE_DP && atcphy->mode != APPLE_ATCPHY_MODE_USB3_DP) return -EOPNOTSUPP; - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; ret = atcphy_dp_set_rate_t8122(atcphy, opts->link_rate); if (ret) dev_err(atcphy->dev, "DP link rate %u failed: %d\n", @@ -5746,7 +5778,9 @@ int apple_atc_dp_tunnel_rate_source(struct phy *phy, unsigned int source, atcphy->tunnel_dpin_rate[dpin] = 0; return 0; } - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; /* A stale source must not retune or stop another source's clock. */ if ((atcphy->tunnel_users & BIT(dpin)) && atcphy->tunnel_source[dpin] != source) @@ -6266,7 +6300,9 @@ static int atcphy_dwc3_reset_assert(struct reset_controller_dev *rcdev, unsigned int ret; guard(mutex)(&atcphy->lock); - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; /* The controller is only reset once it has stopped. */ atcphy->host_active = false; @@ -6287,9 +6323,12 @@ static int atcphy_dwc3_reset_assert(struct reset_controller_dev *rcdev, unsigned static int atcphy_dwc3_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id) { struct apple_atcphy *atcphy = container_of(rcdev, struct apple_atcphy, rcdev); + int ret; guard(mutex)(&atcphy->lock); - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; if (atcphy->t8103_dummy_pending) { int ret = atcphy_park_live_t8103_pipehandler(atcphy); @@ -7133,7 +7172,9 @@ static int atcphy_mux_update_to(struct apple_atcphy *atcphy, enum atcphy_mode ta scoped_guard(mutex, &atcphy->lock) { if (atcphy->cio_sleep.prepared) return atcphy_mux_is_noop(atcphy, target_mode) ? 0 : -EBUSY; - atcphy_restore_after_pd_off(atcphy); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; /* * Only a lane or PIPE change, or a SuperSpeed port still owed * its power, needs the USB3 root hub below. Taking it means @@ -9227,8 +9268,8 @@ static void atcphy_sleep_ss_up(struct apple_atcphy *atcphy) return; atcphy->sleep_ss_down = false; mode = atcphy->typec_mode; - atcphy_restore_after_pd_off(atcphy); - if (atcphy_mux_is_noop(atcphy, mode)) { + ret = atcphy_restore_after_pd_off(atcphy); + if (!ret && atcphy_mux_is_noop(atcphy, mode)) { dev_dbg(atcphy->dev, "SuperSpeed side already in mode %d after the sleep\n", mode); return; @@ -9662,9 +9703,7 @@ static int atcphy_resume(struct device *dev) */ if (atcphy->idle_rpm && pm_runtime_status_suspended(dev)) return 0; - atcphy_restore_after_pd_off(atcphy); - - return 0; + return atcphy_restore_after_pd_off(atcphy); } /* Consumer device links shut DWC3 down before this supplier is visited. */ diff --git a/tools/asahi/atc-dp-drive-tests.py b/tools/asahi/atc-dp-drive-tests.py index 7e3efeceaf2c9..06f49562c3f84 100755 --- a/tools/asahi/atc-dp-drive-tests.py +++ b/tools/asahi/atc-dp-drive-tests.py @@ -86,7 +86,7 @@ def function(source, marker): static bool atcphy_t8132_dp_on(struct apple_atcphy *a) { return dp_on; } static int j700_dp_configure(struct apple_atcphy *a, struct phy_configure_opts_dp *o) { calls_other++; return 0; } static int atcphy_dpphy_configure_t8122(struct apple_atcphy *a, struct phy_configure_opts_dp *o) { calls_other++; return 0; } -static void atcphy_restore_after_pd_off(struct apple_atcphy *a) { } +static int atcphy_restore_after_pd_off(struct apple_atcphy *a) { return 0; } static int atcphy_dp_set_rate_t8122(struct apple_atcphy *a, u32 r) { calls_other++; return 0; } static int atcphy_rpm_get(struct apple_atcphy *a) { return 0; } static void atcphy_rpm_put(struct apple_atcphy *a) { } diff --git a/tools/asahi/atc-usb3-tests.py b/tools/asahi/atc-usb3-tests.py index ee3e5dcce5961..bddb5ea0b0679 100644 --- a/tools/asahi/atc-usb3-tests.py +++ b/tools/asahi/atc-usb3-tests.py @@ -177,7 +177,7 @@ def block(source, marker): } static struct apple_atcphy *phy_get_drvdata(struct phy *f) { return f->data; } static struct apple_atcphy *typec_mux_get_drvdata(struct typec_mux_dev *m) { return m->data; } -static void atcphy_restore_after_pd_off(struct apple_atcphy *a) { (void)a; restore_calls++; } +static int atcphy_restore_after_pd_off(struct apple_atcphy *a) { (void)a; restore_calls++; return 0; } static int atcphy_usb2_power_off(struct apple_atcphy *a) { (void)a; usb2_power_off_calls++; return 0; } diff --git a/tools/asahi/t6030-clock/fixture.c b/tools/asahi/t6030-clock/fixture.c index 3c5b73f77e02a..49f7de9886226 100644 --- a/tools/asahi/t6030-clock/fixture.c +++ b/tools/asahi/t6030-clock/fixture.c @@ -38,6 +38,7 @@ struct apple_atcphy { struct { u8 *core; } regs; + bool idle_rpm; bool tunnel_clock_on; u8 tunnel_rate, tunnel_users, tunnel_dpin_rate[2]; unsigned tunnel_source[2]; @@ -105,8 +106,9 @@ static bool of_machine_is_compatible(const char *s) { return !strcmp(s, machine); } -static void atcphy_restore_after_pd_off(struct apple_atcphy *p) +static int atcphy_restore_after_pd_off(struct apple_atcphy *p) { + return 0; } static void udelay(unsigned us) { diff --git a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py index 974da75b7b9d2..b39e0ccdbeeec 100755 --- a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py +++ b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py @@ -237,7 +237,7 @@ def main(): static const struct atcphy_mode_configuration * atcphy_get_mode_config(struct apple_atcphy *a, enum atcphy_mode m) { (void)a; (void)m; assert(0); return 0; } static inline bool pm_runtime_status_suspended(struct device *d) { (void)d; return rpm_suspended; } -static inline void atcphy_restore_after_pd_off(struct apple_atcphy *a) { (void)a; } +static inline int atcphy_restore_after_pd_off(struct apple_atcphy *a) { (void)a; return 0; } static void apple_tunable_apply(void *regs, struct apple_tunable *t) { (void)regs; 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 ee9109dd2b7cf..9b52fe2d5db08 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c @@ -266,7 +266,7 @@ static inline bool apple_t8132_usb_host_first(void) #define WRITE_ONCE(p, v) ((p) = (v)) static void _atcphy_dwc3_reset_assert(struct apple_atcphy *p); static void atcphy_usb2_power_off(struct apple_atcphy *p); -static void atcphy_restore_after_pd_off(struct apple_atcphy *p); +static int atcphy_restore_after_pd_off(struct apple_atcphy *p); static int atcphy_power_off(struct apple_atcphy *p); static int atcphy_configure(struct apple_atcphy *p, enum atcphy_mode mode); diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py index 4ec9f3aa85f42..8c134ebb8681b 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py @@ -163,7 +163,7 @@ def function(source, name): #ifndef WRITE_ONCE #define WRITE_ONCE(v, x) ((v) = (x)) #endif -static void atcphy_restore_after_pd_off(struct apple_atcphy *); +static int atcphy_restore_after_pd_off(struct apple_atcphy *); static int atcphy_power_off(struct apple_atcphy *); static int atcphy_configure(struct apple_atcphy *, enum atcphy_mode); @@ -2015,7 +2015,7 @@ def function(name): polls == fail_poll || !(condition) ? -ETIMEDOUT : 0; }) /* Lane configuration is outside this test: count calls and commit its result. */ /* Observation is read-only and separately tested; retain production caller control flow. */ -static void __attribute__((unused)) atcphy_restore_after_pd_off(struct apple_atcphy *p) { assert(!p->pd_was_off); } +static int __attribute__((unused)) atcphy_restore_after_pd_off(struct apple_atcphy *p) { assert(!p->pd_was_off); return 0; } static int atcphy_configure(struct apple_atcphy *p, enum atcphy_mode mode); /* These units exercise older PHY generations. Never accept a T8132 or T8152 dispatch. */ diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py index 682a79c00769f..e19f171869804 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py @@ -180,7 +180,7 @@ def make_unit(source, parent=False, reference=None): static struct apple_atcphy ordinary_phy; static struct phy usb_phy={.data=&ordinary_phy}; static unsigned phy_warns,configure_calls,lane_requests; -static void atcphy_restore_after_pd_off(struct apple_atcphy *p) { assert(!p->pd_was_off); } +static int atcphy_restore_after_pd_off(struct apple_atcphy *p) { assert(!p->pd_was_off); return 0; } static int atcphy_park_pipehandler(struct apple_atcphy *p, bool keep_dp) { (void)keep_dp;p->pipe_state=ATCPHY_PIPEHANDLER_STATE_DUMMY;return 0; } static int atcphy_configure_pipehandler(struct apple_atcphy *p,bool on) { (void)p;(void)on;assert(false);return -EINVAL; } /* Role exit parks the ordinary PIPE first; a live legacy park is never owed here. */ From 5d0dc0a48f7f1ee5598527e5d77628a7c52f1dec Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 18:33:08 -0500 Subject: [PATCH 05/11] phy: apple: atc: reconcile pending restore flags and quiesce crossbar on unbind Address cold-restore and crossbar-unbind edge cases: - Clear t8103_dummy_pending alongside pipe_pending once cold restore parks the PIPE on a freshly powered-up domain, so T8103 reset deassert retries are not stuck returning -EBUSY. - Discharge pending J700 usb_off_pending shutdowns after domain loss and baseline atcphy_power_off() instead of replaying the pre-OFF mode into the usb_off_pending guard. - In apple_dpxbar_rpm_release(), call apple_dpxbar_t6030_link(..., false) for any selected T6030 output while its runtime-PM hold is still owned so shared-domain registers do not retain stale enables or FIFO clocks across provider unbind/rebind. - Capture apple_dpxbar_t8103_link_up() debug register reads under dpxbar->lock before unlocking. Signed-off-by: DjDeveloperr --- drivers/mux/apple-display-crossbar.c | 15 ++++++++++----- drivers/phy/apple/atc.c | 12 ++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index 0609d7e11502d..9725d49bf443b 100644 --- a/drivers/mux/apple-display-crossbar.c +++ b/drivers/mux/apple-display-crossbar.c @@ -843,6 +843,7 @@ static int apple_dpxbar_t8103_link_up(struct mux_control *mux) { struct apple_dpxbar *dpxbar; u32 dispext_bit, atc_bit; + u32 atc_en = 0, wr_stat = 0, rd_stat = 0, out_stat = 0; unsigned long flags; unsigned int index; int ret; @@ -869,16 +870,17 @@ static int apple_dpxbar_t8103_link_up(struct mux_control *mux) udelay(10); dpxbar_set32(dpxbar, FIFO_RD_PCLK1_EN, dispext_bit); } + atc_en = readl(dpxbar->regs + CROSSBAR_ATC_EN); + wr_stat = readl(dpxbar->regs + FIFO_WR_DPTX_CLK_EN_STAT); + rd_stat = readl(dpxbar->regs + FIFO_RD_PCLK1_EN_STAT); + out_stat = readl(dpxbar->regs + OUT_PCLK1_EN_STAT); } spin_unlock_irqrestore(&dpxbar->lock, flags); if (ret) return ret; dev_dbg(dpxbar->dev, "link up: ATC_EN=%08x STAT WR=%08x RD=%08x OUT=%08x\n", - readl(dpxbar->regs + CROSSBAR_ATC_EN), - readl(dpxbar->regs + FIFO_WR_DPTX_CLK_EN_STAT), - readl(dpxbar->regs + FIFO_RD_PCLK1_EN_STAT), - readl(dpxbar->regs + OUT_PCLK1_EN_STAT)); + atc_en, wr_stat, rd_stat, out_stat); return 0; } @@ -1446,8 +1448,11 @@ static void apple_dpxbar_rpm_release(void *data) spin_lock_irqsave(&xbar->lock, flags); xbar->removed = true; for (i = 0; i < xbar->hw->n_mux; i++) { - if (test_and_clear_bit(i, &xbar->rpm_held)) + if (test_and_clear_bit(i, &xbar->rpm_held)) { + if (xbar->t6030_dpin && xbar->selected_dispext[i] >= 0) + apple_dpxbar_t6030_link(xbar, i, false); rpm_puts++; + } if (test_and_clear_bit(i, &xbar->rpm_preselect)) rpm_puts++; xbar->selected_dispext[i] = MUX_IDLE_DISCONNECT; diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index 8914f07498ea6..ca49eb93f1fe9 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -5042,6 +5042,7 @@ static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) return ret; } atcphy->pipe_pending = false; + atcphy->t8103_dummy_pending = false; atcphy->pipe_state = ATCPHY_PIPEHANDLER_STATE_DUMMY; } dev_dbg(atcphy->dev, @@ -5063,6 +5064,17 @@ static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) set32(atcphy->regs.usb2phy + USB2PHY_SIG, atcphy_usb2_sig_host(atcphy)); } + if (atcphy->usb_off_pending) { + /* + * Domain loss and atcphy_power_off() above already brought the + * PHY down to its reset baseline; discharge the pending OFF + * rather than replaying the pre-OFF mode. + */ + atcphy->usb_off_pending = false; + atcphy->usb_off_drained = false; + mode = APPLE_ATCPHY_MODE_OFF; + } + if (mode != APPLE_ATCPHY_MODE_OFF) { ret = atcphy_configure(atcphy, mode); if (ret) { From 2c9b8b9d4865932f7bfc30be0a5c1a1905f29826 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 18:55:14 -0500 Subject: [PATCH 06/11] phy: apple: atc: fix cold DP retry, J700 AUX unwind, and crossbar unbind Address three state-recovery edge cases across cold restore and crossbar unbind: - Run pending atcphy_restore_after_pd_off() before the mode_dirty/pipe_pending guards in atcphy_dp_configure() and atcphy_dp_stop() so a transient PIPE timeout during cold restore does not trap subsequent DP retries or stop in -EBUSY without retrying restore. - Track whether cached common/AUX register ownership was already forgotten for the current domain-loss event (pd_state_forgotten) so retrying an incomplete replay without a second domain loss preserves partially acquired ownership (such as J700 aux_power_requested) for atcphy_power_off() to unwind cleanly. - Extract locked T8103 and T602x disconnect helpers in apple-display-crossbar and run the matching layout disconnect sequence from apple_dpxbar_rpm_release() while the runtime-PM hold is still owned, instead of only quiescing T6030. Signed-off-by: DjDeveloperr --- drivers/mux/apple-display-crossbar.c | 187 +++++++++++------- drivers/phy/apple/atc-test.c | 2 + drivers/phy/apple/atc.c | 43 ++-- tools/asahi/atc-cio-sleep-tests.c | 2 +- .../selftests/drivers/apple/atc-t8132-usb3.py | 2 +- .../drivers/gpu/apple-dcp/dwc3/mocks.c | 2 +- .../selftests/drivers/gpu/apple-dcp/phy.py | 8 +- .../drivers/gpu/apple-dcp/quiesce.py | 2 +- 8 files changed, 157 insertions(+), 91 deletions(-) diff --git a/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index 9725d49bf443b..3b865e3feaded 100644 --- a/drivers/mux/apple-display-crossbar.c +++ b/drivers/mux/apple-display-crossbar.c @@ -407,6 +407,56 @@ static int apple_dpxbar_rpm_set(struct mux_control *mux, int state) return ret; } +static void apple_dpxbar_t602x_disconnect_locked(struct apple_dpxbar *dpxbar, + unsigned int index) +{ + u32 atc_bit = t602x_atc_bit(index); + u32 mux_mask = t602x_mux_mask(index); + /* J700 physical DP retains the original T602x clock-gate sequence. */ + bool j700_physical = index == MUX_DPPHY && of_machine_is_compatible("apple,j700"); + u32 prev_dispext_bit, prev_dispext_bit_en; + + if (dpxbar->selected_dispext[index] < 0) + return; + + prev_dispext_bit = 1 << dpxbar->selected_dispext[index]; + prev_dispext_bit_en = 1 << (2 * dpxbar->selected_dispext[index]); + + dpxbar_clear32(dpxbar, T602X_FIFO_RD_UNK_EN, prev_dispext_bit); + dpxbar_clear32(dpxbar, T602X_FIFO_WR_DPTX_CLK_EN, prev_dispext_bit); + dpxbar_clear32(dpxbar, + index == MUX_DPIN1 ? T602X_REG_010 : T602X_REG_00C, + prev_dispext_bit); + + dpxbar_clear32(dpxbar, + index == MUX_DPIN1 ? FIFO_RD_PCLK1_EN : T602X_REG_01C, + atc_bit); + + dpxbar_clear32(dpxbar, T602X_FIFO_WR_UNK_EN, prev_dispext_bit); + dpxbar_clear32(dpxbar, T602X_REG_018, + prev_dispext_bit_en * + (index == MUX_DPIN1 ? 3 : 1)); + + dpxbar_clear32(dpxbar, T602X_FIFO_RD_N_CLK_EN, + atc_bit * (index == MUX_DPIN1 ? 3 : 1)); + + dpxbar_set32(dpxbar, T602X_FIFO_WR_N_CLK_EN, prev_dispext_bit); + dpxbar_set32(dpxbar, T602X_REG_014, 0x4); + + /* Native T602x DPIN0 teardown restores the read reset at +0x24. */ + dpxbar_set32(dpxbar, j700_physical ? FIFO_RD_PCLK1_EN : + T602X_FIFO_RD_PCLK2_EN, atc_bit); + + dpxbar_clear32(dpxbar, T602X_REG_034, atc_bit); + if (!j700_physical) { + dpxbar_clear32(dpxbar, CROSSBAR_ATC_EN, atc_bit); + dpxbar_clear32(dpxbar, CROSSBAR_DISPEXT_EN, prev_dispext_bit); + } + dpxbar_mask32(dpxbar, T602X_REG_030, mux_mask, 0); + + dpxbar->selected_dispext[index] = -1; +} + static int apple_dpxbar_t602x_select(struct mux_control *mux, int state) { struct apple_dpxbar *dpxbar = mux_chip_priv(mux->chip); @@ -451,44 +501,7 @@ static int apple_dpxbar_t602x_select(struct mux_control *mux, int state) } } - if (dpxbar->selected_dispext[index] >= 0) { - u32 prev_dispext_bit = 1 << dpxbar->selected_dispext[index]; - u32 prev_dispext_bit_en = 1 << (2 * dpxbar->selected_dispext[index]); - - dpxbar_clear32(dpxbar, T602X_FIFO_RD_UNK_EN, prev_dispext_bit); - dpxbar_clear32(dpxbar, T602X_FIFO_WR_DPTX_CLK_EN, prev_dispext_bit); - dpxbar_clear32(dpxbar, - index == MUX_DPIN1 ? T602X_REG_010 : T602X_REG_00C, - prev_dispext_bit); - - dpxbar_clear32(dpxbar, - index == MUX_DPIN1 ? FIFO_RD_PCLK1_EN : T602X_REG_01C, - atc_bit); - - dpxbar_clear32(dpxbar, T602X_FIFO_WR_UNK_EN, prev_dispext_bit); - dpxbar_clear32(dpxbar, T602X_REG_018, - prev_dispext_bit_en * - (index == MUX_DPIN1 ? 3 : 1)); - - dpxbar_clear32(dpxbar, T602X_FIFO_RD_N_CLK_EN, - atc_bit * (index == MUX_DPIN1 ? 3 : 1)); - - dpxbar_set32(dpxbar, T602X_FIFO_WR_N_CLK_EN, prev_dispext_bit); - dpxbar_set32(dpxbar, T602X_REG_014, 0x4); - - /* Native T602x DPIN0 teardown restores the read reset at +0x24. */ - dpxbar_set32(dpxbar, j700_physical ? FIFO_RD_PCLK1_EN : - T602X_FIFO_RD_PCLK2_EN, atc_bit); - - dpxbar_clear32(dpxbar, T602X_REG_034, atc_bit); - if (!j700_physical) { - dpxbar_clear32(dpxbar, CROSSBAR_ATC_EN, atc_bit); - dpxbar_clear32(dpxbar, CROSSBAR_DISPEXT_EN, prev_dispext_bit); - } - dpxbar_mask32(dpxbar, T602X_REG_030, mux_mask, 0); - - dpxbar->selected_dispext[index] = -1; - } + apple_dpxbar_t602x_disconnect_locked(dpxbar, index); if (enable) { dpxbar_mask32(dpxbar, T602X_REG_030, mux_mask, mux_val); @@ -596,6 +609,54 @@ static bool apple_dpxbar_cycle_slip(struct apple_dpxbar *dpxbar, unsigned int in return index == MUX_DPPHY || !dpxbar->hw->dpin_no_cycle_slip; } +static void apple_dpxbar_t8103_disconnect_locked(struct apple_dpxbar *dpxbar, + unsigned int index, u32 mux_set) +{ + u32 atc_bit, mux_mask; + + switch (index) { + case MUX_DPPHY: + mux_mask = CROSSBAR_MUX_CTRL_DPPHY_SELECT0 | + CROSSBAR_MUX_CTRL_DPPHY_SELECT1; + atc_bit = ATC_DPPHY; + break; + case MUX_DPIN0: + mux_mask = CROSSBAR_MUX_CTRL_DPIN0_SELECT0 | + CROSSBAR_MUX_CTRL_DPIN0_SELECT1; + atc_bit = ATC_DPIN0; + break; + case MUX_DPIN1: + mux_mask = CROSSBAR_MUX_CTRL_DPIN1_SELECT0 | + CROSSBAR_MUX_CTRL_DPIN1_SELECT1; + atc_bit = ATC_DPIN1; + break; + default: + return; + } + + dpxbar_set32(dpxbar, OUT_N_CLK_EN, atc_bit); + dpxbar_clear32(dpxbar, OUT_UNK_EN, atc_bit); + dpxbar_clear32(dpxbar, OUT_PCLK1_EN, atc_bit); + dpxbar_clear32(dpxbar, CROSSBAR_ATC_EN, atc_bit); + + if (dpxbar->selected_dispext[index] >= 0) { + u32 prev_dispext_bit = 1 << dpxbar->selected_dispext[index]; + u32 prev_dispext_bit_en = 1 << (2 * dpxbar->selected_dispext[index]); + + dpxbar_set32(dpxbar, FIFO_WR_N_CLK_EN, prev_dispext_bit); + dpxbar_set32(dpxbar, FIFO_RD_N_CLK_EN, prev_dispext_bit); + dpxbar_clear32(dpxbar, FIFO_WR_UNK_EN, prev_dispext_bit); + dpxbar_clear32(dpxbar, FIFO_RD_UNK_EN, prev_dispext_bit_en); + dpxbar_clear32(dpxbar, FIFO_WR_DPTX_CLK_EN, prev_dispext_bit); + dpxbar_clear32(dpxbar, FIFO_RD_PCLK1_EN, prev_dispext_bit); + dpxbar_clear32(dpxbar, CROSSBAR_DISPEXT_EN, prev_dispext_bit); + + dpxbar->selected_dispext[index] = -1; + } + + dpxbar_mask32(dpxbar, CROSSBAR_MUX_CTRL, mux_mask, mux_set); +} + static int apple_dpxbar_set(struct mux_control *mux, int state) { struct apple_dpxbar *dpxbar = mux_chip_priv(mux->chip); @@ -607,7 +668,7 @@ static int apple_dpxbar_set(struct mux_control *mux, int state) unsigned int atc_bit; bool enable; int ret = 0; - u32 mux_mask, mux_set; + u32 mux_set; if (state == MUX_IDLE_DISCONNECT) { /* @@ -628,24 +689,18 @@ static int apple_dpxbar_set(struct mux_control *mux, int state) switch (index) { case MUX_DPPHY: - mux_mask = CROSSBAR_MUX_CTRL_DPPHY_SELECT0 | - CROSSBAR_MUX_CTRL_DPPHY_SELECT1; mux_set = FIELD_PREP(CROSSBAR_MUX_CTRL_DPPHY_SELECT0, mux_state) | FIELD_PREP(CROSSBAR_MUX_CTRL_DPPHY_SELECT1, mux_state); atc_bit = ATC_DPPHY; break; case MUX_DPIN0: - mux_mask = CROSSBAR_MUX_CTRL_DPIN0_SELECT0 | - CROSSBAR_MUX_CTRL_DPIN0_SELECT1; mux_set = FIELD_PREP(CROSSBAR_MUX_CTRL_DPIN0_SELECT0, mux_state) | FIELD_PREP(CROSSBAR_MUX_CTRL_DPIN0_SELECT1, mux_state); atc_bit = ATC_DPIN0; break; case MUX_DPIN1: - mux_mask = CROSSBAR_MUX_CTRL_DPIN1_SELECT0 | - CROSSBAR_MUX_CTRL_DPIN1_SELECT1; mux_set = FIELD_PREP(CROSSBAR_MUX_CTRL_DPIN1_SELECT0, mux_state) | FIELD_PREP(CROSSBAR_MUX_CTRL_DPIN1_SELECT1, mux_state); @@ -698,27 +753,7 @@ static int apple_dpxbar_set(struct mux_control *mux, int state) } } - dpxbar_set32(dpxbar, OUT_N_CLK_EN, atc_bit); - dpxbar_clear32(dpxbar, OUT_UNK_EN, atc_bit); - dpxbar_clear32(dpxbar, OUT_PCLK1_EN, atc_bit); - dpxbar_clear32(dpxbar, CROSSBAR_ATC_EN, atc_bit); - - if (dpxbar->selected_dispext[index] >= 0) { - u32 prev_dispext_bit = 1 << dpxbar->selected_dispext[index]; - u32 prev_dispext_bit_en = 1 << (2 * dpxbar->selected_dispext[index]); - - dpxbar_set32(dpxbar, FIFO_WR_N_CLK_EN, prev_dispext_bit); - dpxbar_set32(dpxbar, FIFO_RD_N_CLK_EN, prev_dispext_bit); - dpxbar_clear32(dpxbar, FIFO_WR_UNK_EN, prev_dispext_bit); - dpxbar_clear32(dpxbar, FIFO_RD_UNK_EN, prev_dispext_bit_en); - dpxbar_clear32(dpxbar, FIFO_WR_DPTX_CLK_EN, prev_dispext_bit); - dpxbar_clear32(dpxbar, FIFO_RD_PCLK1_EN, prev_dispext_bit); - dpxbar_clear32(dpxbar, CROSSBAR_DISPEXT_EN, prev_dispext_bit); - - dpxbar->selected_dispext[index] = -1; - } - - dpxbar_mask32(dpxbar, CROSSBAR_MUX_CTRL, mux_mask, mux_set); + apple_dpxbar_t8103_disconnect_locked(dpxbar, index, mux_set); if (enable) { dpxbar_clear32(dpxbar, FIFO_WR_N_CLK_EN, dispext_bit); @@ -1448,12 +1483,22 @@ static void apple_dpxbar_rpm_release(void *data) spin_lock_irqsave(&xbar->lock, flags); xbar->removed = true; for (i = 0; i < xbar->hw->n_mux; i++) { - if (test_and_clear_bit(i, &xbar->rpm_held)) { - if (xbar->t6030_dpin && xbar->selected_dispext[i] >= 0) - apple_dpxbar_t6030_link(xbar, i, false); - rpm_puts++; + bool held = test_and_clear_bit(i, &xbar->rpm_held); + bool preselected = test_and_clear_bit(i, &xbar->rpm_preselect); + + if (held || preselected) { + if (xbar->t6030_dpin) { + if (held && xbar->selected_dispext[i] >= 0) + apple_dpxbar_t6030_link(xbar, i, false); + } else if (xbar->hw->ops->set == apple_dpxbar_set) { + apple_dpxbar_t8103_disconnect_locked(xbar, i, 0); + } else if (held && xbar->selected_dispext[i] >= 0) { + apple_dpxbar_t602x_disconnect_locked(xbar, i); + } } - if (test_and_clear_bit(i, &xbar->rpm_preselect)) + if (held) + rpm_puts++; + if (preselected) rpm_puts++; xbar->selected_dispext[i] = MUX_IDLE_DISCONNECT; } diff --git a/drivers/phy/apple/atc-test.c b/drivers/phy/apple/atc-test.c index cbb1b1f189b79..fc7590eed6eb4 100644 --- a/drivers/phy/apple/atc-test.c +++ b/drivers/phy/apple/atc-test.c @@ -1548,6 +1548,7 @@ static void atcphy_test_t6030_restore_dp_mode_fail(struct kunit *test) KUNIT_EXPECT_EQ(test, config.calls, 1U); KUNIT_EXPECT_EQ(test, atcphy->mode, APPLE_ATCPHY_MODE_DP); KUNIT_EXPECT_TRUE(test, atcphy->pd_was_off); + KUNIT_EXPECT_TRUE(test, atcphy->pd_state_forgotten); kunit_activate_static_stub(test, atcphy_configure, atcphy_test_configure_record); KUNIT_EXPECT_EQ(test, atcphy_resume(atcphy->dev), 0); @@ -1555,6 +1556,7 @@ static void atcphy_test_t6030_restore_dp_mode_fail(struct kunit *test) KUNIT_EXPECT_EQ(test, config.lane_requests, 1U); KUNIT_EXPECT_EQ(test, atcphy->mode, APPLE_ATCPHY_MODE_DP); KUNIT_EXPECT_FALSE(test, atcphy->pd_was_off); + KUNIT_EXPECT_FALSE(test, atcphy->pd_state_forgotten); } static void atcphy_test_t8103_usb3_machine_scope(struct kunit *test) diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index ca49eb93f1fe9..17ea3df52aaea 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -943,6 +943,7 @@ struct atcphy_cio_sleep { * @pd_nb: Power domain notifier, registered with restore_after_pd_off * @pd_was_off: The power domain was switched off since the state was last * brought back; set from the notifier, cleared under @lock + * @pd_state_forgotten: Cached state lost on @pd_was_off was already forgotten * @idle_rpm: An idle PHY runtime-suspends, see atcphy_rpm_get() * @rpm_held: A runtime PM reference is held while busy, see atcphy_rpm_busy(); * under @lock @@ -1091,6 +1092,7 @@ struct apple_atcphy { struct notifier_block pd_nb; bool pd_was_off; + bool pd_state_forgotten; bool idle_rpm; bool rpm_held; struct device *pd_hold; @@ -3018,10 +3020,16 @@ static int atcphy_dp_stop(struct apple_atcphy *atcphy) u32 reg; guard(mutex)(&atcphy->lock); - if (atcphy->mode_dirty || atcphy->pipe_pending) - return -EBUSY; if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) return -EBUSY; + if (atcphy->mode == APPLE_ATCPHY_MODE_DP || + atcphy->mode == APPLE_ATCPHY_MODE_USB3_DP) { + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; + } + if (atcphy->mode_dirty || atcphy->pipe_pending) + return -EBUSY; if (atcphy->mode != APPLE_ATCPHY_MODE_DP && atcphy->mode != APPLE_ATCPHY_MODE_USB3_DP) { atcphy->dp_link_rate = -1; @@ -3029,10 +3037,6 @@ static int atcphy_dp_stop(struct apple_atcphy *atcphy) } if (atcphy->dp_link_rate < 0) return 0; - - ret = atcphy_restore_after_pd_off(atcphy); - if (ret) - return ret; mode_cfg = atcphy_get_mode_config(atcphy, atcphy->mode); core_set32(atcphy, ACIOPHY_LANE_DP_CFG_BLK_TX_DP_CTRL0, DP_PMA_BYTECLK_RESET); @@ -3065,15 +3069,15 @@ static int atcphy_dp_configure(struct apple_atcphy *atcphy, enum atcphy_dp_link_ u32 reg; guard(mutex)(&atcphy->lock); - /* A failed transition may have asserted the PHY resets already. */ - if (atcphy->mode_dirty || atcphy->pipe_pending) - return -EBUSY; /* the AUSPLL is in use as the Thunderbolt DP tunnel pixel clock */ if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) return -EBUSY; ret = atcphy_restore_after_pd_off(atcphy); if (ret) return ret; + /* A failed transition may have asserted the PHY resets already. */ + if (atcphy->mode_dirty || atcphy->pipe_pending) + return -EBUSY; mode_cfg = atcphy_get_mode_config(atcphy, atcphy->mode); cfg = &dp_lr_config[lr]; @@ -5006,7 +5010,15 @@ static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) if (!atcphy->hw->restore_after_pd_off || !READ_ONCE(atcphy->pd_was_off)) return 0; - atcphy_forget_common_state(atcphy); + /* + * Forget cached register ownership once per actual domain loss; a retry + * after an incomplete replay must keep state acquired during that replay + * so atcphy_power_off() can unwind it before clamping the core. + */ + if (!READ_ONCE(atcphy->pd_state_forgotten)) { + atcphy_forget_common_state(atcphy); + WRITE_ONCE(atcphy->pd_state_forgotten, true); + } if (!atcphy->dp_only) { _atcphy_dwc3_reset_assert(atcphy); @@ -5087,6 +5099,7 @@ static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) } WRITE_ONCE(atcphy->pd_was_off, false); + WRITE_ONCE(atcphy->pd_state_forgotten, false); return 0; } @@ -8524,8 +8537,10 @@ static int atcphy_pd_notify(struct notifier_block *nb, unsigned long action, voi { struct apple_atcphy *atcphy = container_of(nb, struct apple_atcphy, pd_nb); - if (action == GENPD_NOTIFY_OFF) + if (action == GENPD_NOTIFY_OFF) { + WRITE_ONCE(atcphy->pd_state_forgotten, false); WRITE_ONCE(atcphy->pd_was_off, true); + } return NOTIFY_OK; } @@ -9216,6 +9231,7 @@ static void atcphy_sleep_lost_common(struct apple_atcphy *atcphy) !of_device_is_compatible(atcphy->np, "apple,t8142-atcphy")) || !atcphy->hw->restore_after_pd_off || !pm_resume_via_firmware()) return; + WRITE_ONCE(atcphy->pd_state_forgotten, false); WRITE_ONCE(atcphy->pd_was_off, true); dev_dbg(atcphy->dev, "mode %d lost in the sleep: setting it up again\n", atcphy->mode); } @@ -9229,8 +9245,10 @@ static int atcphy_resume_noirq(struct device *dev) if (atcphy->cio_sleep.prepared) return 0; /* A real sleep took T8132's PHY state behind its power domain's back. */ - if (atcphy->hw->gen == ATCPHY_GENERATION_T8132 && pm_resume_via_firmware()) + if (atcphy->hw->gen == ATCPHY_GENERATION_T8132 && pm_resume_via_firmware()) { + WRITE_ONCE(atcphy->pd_state_forgotten, false); WRITE_ONCE(atcphy->pd_was_off, true); + } if (!atcphy->hibernated) { atcphy_sleep_lost_common(atcphy); return 0; @@ -9625,6 +9643,7 @@ int apple_atcphy_cio_on(struct phy *phy) } s->restored = true; WRITE_ONCE(atcphy->pd_was_off, false); + WRITE_ONCE(atcphy->pd_state_forgotten, false); return 0; } EXPORT_SYMBOL_GPL(apple_atcphy_cio_on); diff --git a/tools/asahi/atc-cio-sleep-tests.c b/tools/asahi/atc-cio-sleep-tests.c index f52425783c3ba..be341b799f09f 100644 --- a/tools/asahi/atc-cio-sleep-tests.c +++ b/tools/asahi/atc-cio-sleep-tests.c @@ -30,7 +30,7 @@ struct apple_atcphy { int lock; bool t8132_common_owned, cio_tunables, t8132_failed, mode_dirty, pipe_pending; - bool swap_lanes, host_active, tunnel_clock_on, tunnel_ack_stale, pd_was_off, + bool swap_lanes, host_active, tunnel_clock_on, tunnel_ack_stale, pd_was_off, pd_state_forgotten, t8132_suspended; unsigned int pipe_state; enum atcphy_mode mode; diff --git a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py index b39e0ccdbeeec..0cba6abd0a73f 100755 --- a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py +++ b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py @@ -120,7 +120,7 @@ def main(): struct device *dev; bool t8132_ss, t8132_common_owned, t8132_suspended, t8132_failed, host_active, dwc3_running; bool ss_tunables, mode_dirty, pipe_pending, swap_lanes; - bool cio_tunables, t8132_dp_alt, fixed_usb2, hibernated, pd_was_off, idle_rpm; + bool cio_tunables, t8132_dp_alt, fixed_usb2, hibernated, pd_was_off, pd_state_forgotten, idle_rpm; bool t8103_dummy_pending, t8103_usb3_workaround, fixed_usb3, cio_only; struct device *pd_hold; bool pd_hold_sleep, pd_hold_released; 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 9b52fe2d5db08..998ab1f90db82 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c @@ -21,7 +21,7 @@ struct apple_atcphy { struct reset_controller_dev rcdev; void *dev; int lock, dp_link_rate; - bool swap_lanes, tunnel_clock_on, tunnel_saved, dp_only, pd_was_off; + bool swap_lanes, tunnel_clock_on, tunnel_saved, dp_only, pd_was_off, pd_state_forgotten; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py index 8c134ebb8681b..512b36cc13223 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py @@ -60,7 +60,7 @@ def function(source, name): bool pipe_pending, usb_off_pending, usb_off_drained; bool usb3_port_off, park_owed; bool t8103_dummy_pending, t8103_usb3_retry, t8103_usb3_workaround; - bool dp_only, pd_was_off, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; /* T8132 idle runtime PM; these PHYs never power down while idle. */ bool idle_rpm, rpm_held; unsigned int tunnel_rate, tunnel_users; @@ -1966,7 +1966,7 @@ def function(name): struct { bool prepared; } cio_sleep; struct { void *usb3_dp, *dp_lioa, *dp_common_config; } tunables; - bool dp_only, pd_was_off, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; unsigned int pairs, rate; } dp_t8122; const struct atcphy_hw *hw; @@ -2377,7 +2377,7 @@ def function(source, name): struct { bool prepared; } cio_sleep; struct { void *usb3_dp, *dp_lioa, *dp_common_config; } tunables; - bool dp_only, pd_was_off, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; unsigned int pairs, rate; } dp_t8122; const struct atcphy_hw *hw; @@ -2712,7 +2712,7 @@ def aux(root): struct { bool prepared; } cio_sleep; struct { void *usb3_dp, *dp_lioa, *dp_common_config; } tunables; - bool dp_only, pd_was_off, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; unsigned int pairs, rate; } dp_t8122; struct { unsigned char *core; } regs; bool aux_power_requested; }; struct event { u32 offset, mask, value; } events[32]; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py index e19f171869804..b6b1f962ccc4b 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py @@ -162,7 +162,7 @@ def make_unit(source, parent=False, reference=None): struct { void *usb2phy, *pipehandler; } regs; bool fixed_usb2,cio_only,host_active,dwc3_running,mode_dirty,pipe_pending,usb_off_pending,ss_tunables,usb3_tunables,dp_tunables,cio_tunables,t8132_dp_alt; bool usb2_device; - bool usb3_port_off,park_owed,dp_only,pd_was_off; + bool usb3_port_off,park_owed,dp_only,pd_was_off,pd_state_forgotten; bool t8103_dummy_pending,t8103_usb3_retry,t8103_usb3_workaround; enum atcphy_mode mode,typec_mode; enum atcphy_pipehandler_state pipe_state; From 8843ffd50af14e5d9fa23fc6275ae1412ec74922 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 19:18:57 -0500 Subject: [PATCH 07/11] phy: apple: atc: complete cold restore on SAFE, DP stop, and J700 DP retries Close four remaining cold-restore recovery edge cases across T6030, T8103, and J700: - When a Type-C mux update requests SAFE (APPLE_ATCPHY_MODE_OFF) after a power-domain loss on a non-T8132-common-owned PHY, restore directly to OFF and clear mode_dirty rather than failing to replay an obsolete previous mode. - Clear stale t8103_usb3_retry alongside t8103_dummy_pending once cold PIPE parking succeeds so a later same-mode USB3 mux event cannot reconnect the PIPE while DWC3 is held in reset. - Run atcphy_restore_after_pd_off() unconditionally at the start of atcphy_dp_stop() and atcphy_dp_configure() so pending cold restores in USB3 and OFF modes are completed before dirty/pending guards. - Call atcphy_restore_after_pd_off() at the start of j700_dp_configure() (and wrap the J700 DP configure path with atcphy_rpm_get/put) so rate and stop retries recover from an incomplete AUX replay instead of returning -EBUSY, and stub atcphy_restore_after_pd_off() in the apple-dcp unwind host selftest. Signed-off-by: DjDeveloperr --- drivers/phy/apple/atc-j700-dp.h | 3 ++ drivers/phy/apple/atc.c | 52 +++++++++++++------ .../selftests/drivers/gpu/apple-dcp/phy.py | 1 + 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/drivers/phy/apple/atc-j700-dp.h b/drivers/phy/apple/atc-j700-dp.h index 55cd6fdeb4b7e..ae187df5526d3 100644 --- a/drivers/phy/apple/atc-j700-dp.h +++ b/drivers/phy/apple/atc-j700-dp.h @@ -475,6 +475,9 @@ static int j700_dp_configure(struct apple_atcphy *p, const struct phy_configure_ int rate = -1, ret; guard(mutex)(&p->lock); + ret = atcphy_restore_after_pd_off(p); + if (ret) + return ret; if ((o->set_rate && !o->link_rate) || (o->set_lanes && !o->lanes)) return o->set_voltages ? -EINVAL : j700_dp_stop(p); /* The last completed mode cannot describe a partially changed lane route. */ diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index 17ea3df52aaea..954568c217062 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -2485,6 +2485,8 @@ static int j700_pma_idle(struct apple_atcphy *atcphy, unsigned int lane) return 0; } +static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy); + #include "atc-j700-dp.h" static int atcphy_configure_lanes(struct apple_atcphy *atcphy, enum atcphy_mode mode) @@ -3011,8 +3013,6 @@ static int atcphy_auspll_apb_command(struct apple_atcphy *atcphy, u32 command) return ret; } -static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy); - static int atcphy_dp_stop(struct apple_atcphy *atcphy) { const struct atcphy_mode_configuration *mode_cfg; @@ -3020,14 +3020,11 @@ static int atcphy_dp_stop(struct apple_atcphy *atcphy) u32 reg; guard(mutex)(&atcphy->lock); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) return -EBUSY; - if (atcphy->mode == APPLE_ATCPHY_MODE_DP || - atcphy->mode == APPLE_ATCPHY_MODE_USB3_DP) { - ret = atcphy_restore_after_pd_off(atcphy); - if (ret) - return ret; - } if (atcphy->mode_dirty || atcphy->pipe_pending) return -EBUSY; if (atcphy->mode != APPLE_ATCPHY_MODE_DP && @@ -3069,12 +3066,12 @@ static int atcphy_dp_configure(struct apple_atcphy *atcphy, enum atcphy_dp_link_ u32 reg; guard(mutex)(&atcphy->lock); - /* the AUSPLL is in use as the Thunderbolt DP tunnel pixel clock */ - if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) - return -EBUSY; ret = atcphy_restore_after_pd_off(atcphy); if (ret) return ret; + /* the AUSPLL is in use as the Thunderbolt DP tunnel pixel clock */ + if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) + return -EBUSY; /* A failed transition may have asserted the PHY resets already. */ if (atcphy->mode_dirty || atcphy->pipe_pending) return -EBUSY; @@ -5055,6 +5052,7 @@ static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) } atcphy->pipe_pending = false; atcphy->t8103_dummy_pending = false; + atcphy->t8103_usb3_retry = false; atcphy->pipe_state = ATCPHY_PIPEHANDLER_STATE_DUMMY; } dev_dbg(atcphy->dev, @@ -5096,6 +5094,8 @@ static int atcphy_restore_after_pd_off(struct apple_atcphy *atcphy) atcphy->mode = mode; return ret; } + } else { + atcphy->mode_dirty = false; } WRITE_ONCE(atcphy->pd_was_off, false); @@ -5681,8 +5681,15 @@ static int atcphy_dpphy_configure(struct phy *phy, union phy_configure_opts *opt } return opts->set_lanes ? -EINVAL : 0; } - if (atcphy->hw->has_t8140_dp) - return j700_dp_configure(atcphy, opts); + if (atcphy->hw->has_t8140_dp) { + int ret = atcphy_rpm_get(atcphy); + + if (ret < 0) + return ret; + ret = j700_dp_configure(atcphy, opts); + atcphy_rpm_put(atcphy); + return ret; + } if (atcphy->hw->dp_t8122) { int ret = atcphy_rpm_get(atcphy); @@ -7197,9 +7204,22 @@ static int atcphy_mux_update_to(struct apple_atcphy *atcphy, enum atcphy_mode ta scoped_guard(mutex, &atcphy->lock) { if (atcphy->cio_sleep.prepared) return atcphy_mux_is_noop(atcphy, target_mode) ? 0 : -EBUSY; - ret = atcphy_restore_after_pd_off(atcphy); - if (ret) - return ret; + if (target_mode == APPLE_ATCPHY_MODE_OFF && !atcphy->t8132_common_owned && + atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) { + enum atcphy_mode prev_mode = atcphy->mode; + + /* Reaching SAFE/OFF must not fail replaying a mode being left behind. */ + atcphy->mode = APPLE_ATCPHY_MODE_OFF; + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) { + atcphy->mode = prev_mode; + return ret; + } + } else { + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; + } /* * Only a lane or PIPE change, or a SuperSpeed port still owed * its power, needs the USB3 root hub below. Taking it means diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py index 512b36cc13223..e33156b27db36 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py @@ -2461,6 +2461,7 @@ def function(source, name): #endif static int atcphy_power_off(struct apple_atcphy *); static int atcphy_configure(struct apple_atcphy *, enum atcphy_mode); +static int __attribute__((unused)) atcphy_restore_after_pd_off(struct apple_atcphy *p) { assert(!p->pd_was_off); return 0; } /* These units exercise older PHY generations. Never accept a T8132 or T8152 dispatch. */ static int __attribute__((unused)) atcphy_configure_t8132(struct apple_atcphy *p, enum atcphy_mode mode) From 5bea9d8aed1d43f81930ff656c8613d9a0509b80 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 19:44:49 -0500 Subject: [PATCH 08/11] phy: apple: atc: skip obsolete mode replay on superseding mux switch When a valid superseding Type-C mux update arrives after a power-domain off event, restore to the baseline OFF state before switching rather than replaying the old mode first. This avoids failing J700 fixed-hub SAFE (which maps to USB2) or T6030 DP-to-USB2 transitions on obsolete DP AUX or RCAL bring-up, while preserving the current mode if the target mode is rejected by atcphy_mux_check(). Also check that the restored mode supports physical DisplayPort before programming the AUSPLL in atcphy_dp_configure(), and invalidate T602x tunnel ownership flags, saved registers, DP IN rates, and cached DP link rate in atcphy_forget_common_state() on domain loss. Update the apple-dcp host selftest struct stubs to carry the invalidated tunnel fields. Signed-off-by: DjDeveloperr --- drivers/phy/apple/atc.c | 26 +++++++++++++++---- .../drivers/gpu/apple-dcp/dwc3/mocks.c | 4 ++- .../selftests/drivers/gpu/apple-dcp/phy.py | 20 +++++++++----- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index 954568c217062..12c5859c35040 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -3075,6 +3075,9 @@ static int atcphy_dp_configure(struct apple_atcphy *atcphy, enum atcphy_dp_link_ /* A failed transition may have asserted the PHY resets already. */ if (atcphy->mode_dirty || atcphy->pipe_pending) return -EBUSY; + if (atcphy->mode != APPLE_ATCPHY_MODE_DP && + atcphy->mode != APPLE_ATCPHY_MODE_USB3_DP) + return -EBUSY; mode_cfg = atcphy_get_mode_config(atcphy, atcphy->mode); cfg = &dp_lr_config[lr]; @@ -4957,7 +4960,13 @@ static void atcphy_forget_common_state(struct apple_atcphy *atcphy) atcphy->dp_t8122.pll = false; atcphy->dp_t8122.pairs = 0; atcphy->dp_t8122.rate = 0; + atcphy->dp_link_rate = -1; atcphy->tunnel_clock_on = false; + atcphy->tunnel_attempted = false; + atcphy->tunnel_saved = false; + memset(atcphy->tunnel_saved_regs, 0, sizeof(atcphy->tunnel_saved_regs)); + atcphy->tunnel_dpin_rate[0] = 0; + atcphy->tunnel_dpin_rate[1] = 0; atcphy->tunnel_rate = 0; atcphy->tunnel_users = 0; if (atcphy->hw->has_t8140_dp) { @@ -7202,13 +7211,21 @@ static int atcphy_mux_update_to(struct apple_atcphy *atcphy, enum atcphy_mode ta int ret; scoped_guard(mutex, &atcphy->lock) { + enum atcphy_mode eff_mode = atcphy->usb2_device ? + atcphy_usb2_device_mode(target_mode) : target_mode; + if (atcphy->cio_sleep.prepared) return atcphy_mux_is_noop(atcphy, target_mode) ? 0 : -EBUSY; - if (target_mode == APPLE_ATCPHY_MODE_OFF && !atcphy->t8132_common_owned && - atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) { + if (eff_mode != atcphy->mode && !atcphy->t8132_common_owned && + atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off) && + !atcphy_mux_check(atcphy, eff_mode)) { enum atcphy_mode prev_mode = atcphy->mode; - /* Reaching SAFE/OFF must not fail replaying a mode being left behind. */ + /* + * Do not fail replaying a mode that this valid update is + * about to leave behind; restore to the baseline OFF state + * so the switch below applies @target_mode directly. + */ atcphy->mode = APPLE_ATCPHY_MODE_OFF; ret = atcphy_restore_after_pd_off(atcphy); if (ret) { @@ -7233,8 +7250,7 @@ static int atcphy_mux_update_to(struct apple_atcphy *atcphy, enum atcphy_mode ta return 0; } /* Turn down a mode the PHY cannot run before USB3 goes down for it. */ - ret = atcphy_mux_check(atcphy, atcphy->usb2_device ? - atcphy_usb2_device_mode(target_mode) : target_mode); + ret = atcphy_mux_check(atcphy, eff_mode); if (ret) return ret; in_mode = atcphy_mux_in_mode(atcphy, target_mode); 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 998ab1f90db82..602a6b56adaca 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c @@ -21,7 +21,9 @@ struct apple_atcphy { struct reset_controller_dev rcdev; void *dev; int lock, dp_link_rate; - bool swap_lanes, tunnel_clock_on, tunnel_saved, dp_only, pd_was_off, pd_state_forgotten; + bool swap_lanes, tunnel_clock_on, tunnel_attempted, tunnel_saved, dp_only, pd_was_off, pd_state_forgotten; + u32 tunnel_saved_regs[12]; + u8 tunnel_dpin_rate[2]; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py index e33156b27db36..ecfb11ac66ad0 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py @@ -60,7 +60,9 @@ def function(source, name): bool pipe_pending, usb_off_pending, usb_off_drained; bool usb3_port_off, park_owed; bool t8103_dummy_pending, t8103_usb3_retry, t8103_usb3_workaround; - bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on, tunnel_attempted, tunnel_saved; + u32 tunnel_saved_regs[12]; + unsigned char tunnel_dpin_rate[2]; /* T8132 idle runtime PM; these PHYs never power down while idle. */ bool idle_rpm, rpm_held; unsigned int tunnel_rate, tunnel_users; @@ -1033,8 +1035,6 @@ def adverse(root): production = ('#define T8103_HOST_BIST ' + str(int('ATCPHY_GENERATION_T8103' not in dispatch and 't8103_usb3_workaround' not in dispatch)) + '\n' + production) - production = production.replace('bool pipe_pending, usb_off_pending, usb_off_drained;', - 'bool pipe_pending, usb_off_pending, usb_off_drained, tunnel_saved;', 1) production += '\n' + ns['block'](ns['atc'], 'enum atcphy_lane {') + ';' production += '\n' + ns['block'](ns['atc'], 'struct atcphy_dp_link_rate_configuration {') + ';' production += '\n' + ns['block'](ns['atc'], 'static const struct atcphy_dp_link_rate_configuration dp_lr_config[] = {') + ';' @@ -1966,7 +1966,10 @@ def function(name): struct { bool prepared; } cio_sleep; struct { void *usb3_dp, *dp_lioa, *dp_common_config; } tunables; - bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on, tunnel_attempted, tunnel_saved; + u32 tunnel_saved_regs[12]; + unsigned char tunnel_dpin_rate[2]; + int dp_link_rate; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; unsigned int pairs, rate; } dp_t8122; const struct atcphy_hw *hw; @@ -2377,7 +2380,9 @@ def function(source, name): struct { bool prepared; } cio_sleep; struct { void *usb3_dp, *dp_lioa, *dp_common_config; } tunables; - bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on, tunnel_attempted, tunnel_saved; + u32 tunnel_saved_regs[12]; + unsigned char tunnel_dpin_rate[2]; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; unsigned int pairs, rate; } dp_t8122; const struct atcphy_hw *hw; @@ -2713,7 +2718,10 @@ def aux(root): struct { bool prepared; } cio_sleep; struct { void *usb3_dp, *dp_lioa, *dp_common_config; } tunables; - bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on; + bool dp_only, pd_was_off, pd_state_forgotten, tunnel_clock_on, tunnel_attempted, tunnel_saved; + u32 tunnel_saved_regs[12]; + unsigned char tunnel_dpin_rate[2]; + int dp_link_rate; unsigned int tunnel_rate, tunnel_users; struct { bool aux, pll; unsigned int pairs, rate; } dp_t8122; struct { unsigned char *core; } regs; bool aux_power_requested; }; struct event { u32 offset, mask, value; } events[32]; From 2ad5fc98dfaed7a87d6fe13763a25f3cb998dbd1 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 20:04:46 -0500 Subject: [PATCH 09/11] phy: apple: atc: skip obsolete USB3 replay on teardown and gate tunnel/validate on cold restore Avoid replaying an obsolete pre-domain-loss USB3 mode in atcphy_usb3_power_off() and atcphy_usb3_set_mode() when tearing down to OFF or falling back to USB2 device mode on PHYs with restore_after_pd_off, preventing failed RCAL polls from blocking phy_power_off() and pinning PHY power_count and runtime-PM references. Also run atcphy_restore_after_pd_off() and reject dirty/pending PIPE state in apple_atc_dp_tunnel_rate(), apple_atc_dp_tunnel_rate_source(), and apple_atc_dp_tunnel_open(), clear tunnel_dpin_rate[] on idle-rpm stop, and report zero usable DP lanes in atcphy_dpphy_validate() while a cold restore is pending or incomplete. Signed-off-by: DjDeveloperr --- drivers/phy/apple/atc.c | 73 ++++++++++++++++--- tools/asahi/atc-usb3-tests.py | 5 +- tools/asahi/t6030-clock/fixture.c | 2 +- .../selftests/drivers/gpu/apple-dcp/phy.py | 3 + .../drivers/gpu/apple-dcp/quiesce.py | 7 +- 5 files changed, 76 insertions(+), 14 deletions(-) diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index 12c5859c35040..f8a6f6f7ff780 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -5249,12 +5249,32 @@ static const struct phy_ops apple_atc_usb2_phy_ops = { static int atcphy_usb3_power_off(struct phy *phy) { struct apple_atcphy *atcphy = phy_get_drvdata(phy); + bool keep_mode; int ret; guard(mutex)(&atcphy->lock); - ret = atcphy_restore_after_pd_off(atcphy); - if (ret) - return ret; + keep_mode = (atcphy_mode_is_cio(atcphy->mode) && !atcphy->mode_dirty) || + (atcphy_modes[atcphy->mode].enable_dp_aux && + atcphy->hw->gen != ATCPHY_GENERATION_T8132); + if (!keep_mode && !atcphy->t8132_common_owned && + atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) { + enum atcphy_mode prev_mode = atcphy->mode; + + /* + * Teardown is taking this PHY to OFF; do not replay an + * obsolete pre-domain-loss USB mode first. + */ + atcphy->mode = APPLE_ATCPHY_MODE_OFF; + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) { + atcphy->mode = prev_mode; + return ret; + } + } else { + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; + } /* * dwc3 has left its role: nothing drives the PIPE any more, and the @@ -5363,12 +5383,29 @@ static int atcphy_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode { struct apple_atcphy *atcphy = phy_get_drvdata(phy); const bool usb2_device = atcphy->hw->no_ss_device && mode == PHY_MODE_USB_DEVICE; + enum atcphy_mode usb2_fallback = APPLE_ATCPHY_MODE_OFF; + bool skipped_ss_replay = false; int ret; guard(mutex)(&atcphy->lock); - ret = atcphy_restore_after_pd_off(atcphy); - if (ret) - return ret; + if (usb2_device && !atcphy->usb_off_pending && !atcphy->t8132_common_owned && + atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off) && + atcphy_usb2_device_mode(atcphy->mode) != atcphy->mode) { + enum atcphy_mode prev_mode = atcphy->mode; + + usb2_fallback = atcphy_usb2_device_mode(prev_mode); + atcphy->mode = APPLE_ATCPHY_MODE_OFF; + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) { + atcphy->mode = prev_mode; + return ret; + } + skipped_ss_replay = true; + } else { + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; + } if (atcphy->usb_off_pending) return -EBUSY; @@ -5399,7 +5436,8 @@ static int atcphy_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode break; case PHY_MODE_USB_DEVICE: if (usb2_device) { - enum atcphy_mode usb2 = atcphy_usb2_device_mode(atcphy->mode); + enum atcphy_mode usb2 = skipped_ss_replay ? usb2_fallback : + atcphy_usb2_device_mode(atcphy->mode); /* * No SuperSpeed device mode: serve the device role on @@ -5610,8 +5648,9 @@ static int atcphy_dpphy_validate(struct phy *phy, enum phy_mode mode, int submod /* The Type-C mux owns lane setup; validate observes its completed epoch. */ guard(mutex)(&atcphy->lock); - if (atcphy->hw->dp_t8122 && - (atcphy->mode_dirty || !atcphy->dp_t8122.aux)) { + if (atcphy->mode_dirty || atcphy->pipe_pending || + (atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) || + (atcphy->hw->dp_t8122 && !atcphy->dp_t8122.aux)) { opts->lanes = 0; return 0; } @@ -5822,6 +5861,8 @@ int apple_atc_dp_tunnel_rate_source(struct phy *phy, unsigned int source, ret = atcphy_restore_after_pd_off(atcphy); if (ret) return ret; + if (atcphy->mode_dirty || atcphy->pipe_pending) + return -EBUSY; /* A stale source must not retune or stop another source's clock. */ if ((atcphy->tunnel_users & BIT(dpin)) && atcphy->tunnel_source[dpin] != source) @@ -5917,10 +5958,16 @@ int apple_atc_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate) atcphy->mode != APPLE_ATCPHY_MODE_TBT) { if (!rate) { atcphy->tunnel_users &= ~BIT(dpin); + atcphy->tunnel_dpin_rate[dpin] = 0; return 0; } return -EBUSY; } + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; + if (atcphy->mode_dirty || atcphy->pipe_pending) + return -EBUSY; if (!rate) { if (apple_atc_tunnel_is_t8103_style()) { /* @@ -5969,6 +6016,14 @@ int apple_atc_dp_tunnel_open(struct phy *phy) return -EINVAL; atcphy = phy_get_drvdata(phy); guard(mutex)(&atcphy->lock); + { + int ret = atcphy_restore_after_pd_off(atcphy); + + if (ret) + return ret; + } + if (atcphy->mode_dirty || atcphy->pipe_pending) + return -EBUSY; if (atcphy->mode != APPLE_ATCPHY_MODE_USB4 && atcphy->mode != APPLE_ATCPHY_MODE_TBT) return -EBUSY; if (!atcphy->tunnel_clock_on) { diff --git a/tools/asahi/atc-usb3-tests.py b/tools/asahi/atc-usb3-tests.py index bddb5ea0b0679..786e673a5a099 100644 --- a/tools/asahi/atc-usb3-tests.py +++ b/tools/asahi/atc-usb3-tests.py @@ -75,12 +75,13 @@ def block(source, marker): #define fallthrough __attribute__((fallthrough)) #define WARN_ON_ONCE(cond) do { if (cond) warnings++; } while (0) #define container_of(ptr,type,member) ((type *)((char *)(ptr) - offsetof(type,member))) +#define READ_ONCE(x) (x) ''' STUBS = r''' struct atcphy_hw { enum atcphy_generation gen; bool has_t8140_dp, dp_t8122, has_usb4, park_pipe_unlocked, park_dummy_phy, usb2_only; - bool clear_boot_hold, dp_banked_tunables, pipe_lock_unacked, no_ss_device; }; + bool clear_boot_hold, dp_banked_tunables, pipe_lock_unacked, no_ss_device, restore_after_pd_off; }; struct device_node { const char *const *compatible; unsigned int count; }; static bool of_device_is_compatible(const struct device_node *root, const char *compatible) { if (!root) return false; @@ -104,7 +105,7 @@ def block(source, marker): /* T8132 ownership is tested without entering its lane-setup backend. */ bool t8132_ss, t8132_common_owned, t8132_failed, cio_tunables, t8132_dp_alt; /* Device role on a PHY without SuperSpeed device mode (hw->no_ss_device). */ - bool usb2_device; + bool usb2_device, pd_was_off; struct { void *usb3_dp; } tunables; }; struct phy { struct apple_atcphy *data; }; diff --git a/tools/asahi/t6030-clock/fixture.c b/tools/asahi/t6030-clock/fixture.c index 49f7de9886226..63e999894ac5a 100644 --- a/tools/asahi/t6030-clock/fixture.c +++ b/tools/asahi/t6030-clock/fixture.c @@ -38,7 +38,7 @@ struct apple_atcphy { struct { u8 *core; } regs; - bool idle_rpm; + bool idle_rpm, mode_dirty, pipe_pending; bool tunnel_clock_on; u8 tunnel_rate, tunnel_users, tunnel_dpin_rate[2]; unsigned tunnel_source[2]; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py index ecfb11ac66ad0..61f615beed3fa 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py @@ -1992,6 +1992,9 @@ def function(name): parts.append(source[start:source.index('\n};', start) + 3]) parts.append(atc_cio_helpers(source)) parts.append(r''' +#ifndef READ_ONCE +#define READ_ONCE(x) (x) +#endif static u32 pipe_regs[0x1000 / 4], core_regs[0x30000 / 4]; static unsigned int polls, fail_poll, mux_writes, lane_calls; static const char *last_poll; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py index b6b1f962ccc4b..1ba7552ed26fd 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py @@ -149,12 +149,15 @@ def make_unit(source, parent=False, reference=None): #define WARN_ON_ONCE(x) ({ bool hit=(x);phy_warns+=hit;hit; }) #define fallthrough __attribute__((fallthrough)) #define KUNIT_STATIC_STUB_REDIRECT(...) ((void)0) +#ifndef READ_ONCE +#define READ_ONCE(x) (x) +#endif enum atcphy_mode { APPLE_ATCPHY_MODE_OFF, APPLE_ATCPHY_MODE_USB2, APPLE_ATCPHY_MODE_USB3, APPLE_ATCPHY_MODE_USB3_DP, APPLE_ATCPHY_MODE_DP, APPLE_ATCPHY_MODE_TBT, APPLE_ATCPHY_MODE_USB4 }; enum atcphy_pipehandler_state { ATCPHY_PIPEHANDLER_STATE_DUMMY, ATCPHY_PIPEHANDLER_STATE_USB3, ATCPHY_PIPEHANDLER_STATE_USB4 }; GENERATION_ENUM -struct atcphy_hw { GENERATION_FIELD bool has_usb4,has_t8140_dp,dp_t8122; u32 usb2phy_sig_host; }; +struct atcphy_hw { GENERATION_FIELD bool has_usb4,has_t8140_dp,dp_t8122,restore_after_pd_off; u32 usb2phy_sig_host; }; struct apple_atcphy { struct { void *usb3_dp; } tunables; void *dev; int lock; @@ -162,7 +165,7 @@ def make_unit(source, parent=False, reference=None): struct { void *usb2phy, *pipehandler; } regs; bool fixed_usb2,cio_only,host_active,dwc3_running,mode_dirty,pipe_pending,usb_off_pending,ss_tunables,usb3_tunables,dp_tunables,cio_tunables,t8132_dp_alt; bool usb2_device; - bool usb3_port_off,park_owed,dp_only,pd_was_off,pd_state_forgotten; + bool usb3_port_off,park_owed,dp_only,pd_was_off,pd_state_forgotten,t8132_common_owned; bool t8103_dummy_pending,t8103_usb3_retry,t8103_usb3_workaround; enum atcphy_mode mode,typec_mode; enum atcphy_pipehandler_state pipe_state; From d29ca17ae21c33856a03baf8cb48174212841f8b Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 20:37:12 -0500 Subject: [PATCH 10/11] phy: apple: atc: harden USB2 fallback, T8132 teardown, tunnel stop, and AUX state Fix six cold-restore and teardown edge cases across T8122/T6030, T8132, and T600x ATC PHYs: 1. Avoid replaying obsolete SuperSpeed modes during USB2-only device startup in atcphy_usb2_set_mode() so DWC3 can deassert reset without stalling on SuperSpeed RCAL after domain loss. 2. Restore directly to the USB2 fallback target inside atcphy_restore_after_pd_off() in atcphy_usb2_set_mode() and atcphy_usb3_set_mode(), restoring the prior mode on error so transient failures remain retryable with or without a second domain loss. 3. Clear t8132_common_owned once per domain loss in atcphy_forget_common_state() and allow pre-epoch teardown/fallback/mux shortcuts when the T8132 domain went off before any common touch, preventing pinned power_count and runtime-PM references. 4. Allow rate == 0 tunnel clock teardown to proceed when mode_dirty or pipe_pending is set (and short-circuit cleanly while pd_was_off) in apple_atc_dp_tunnel_rate_source() and apple_atc_dp_tunnel_rate(). 5. Record dp_t8122.aux = true in atcphy_dp_aux_on_t8122() only when atc_t8122_dp_aux_is_on() confirms power-up, turning AUX off on failure so DP validation reports 0 lanes instead of stale lane counts. 6. Check for CIO mode before calling atcphy_restore_after_pd_off() in apple_atc_dp_tunnel_open() so an idle OFF PHY is not woken or touched. Signed-off-by: DjDeveloperr --- drivers/phy/apple/atc.c | 74 ++++++++++++++----- tools/asahi/atc-usb3-tests.py | 2 +- tools/asahi/t6030-clock/fixture.c | 3 +- .../selftests/drivers/apple/atc-t8132-usb3.py | 5 +- .../drivers/gpu/apple-dcp/quiesce.py | 2 +- 5 files changed, 63 insertions(+), 23 deletions(-) diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index f8a6f6f7ff780..6df3395b08985 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -4195,13 +4195,16 @@ static void atcphy_dp_aux_on_t8122(struct apple_atcphy *atcphy) lockdep_assert_held(&atcphy->lock); atc_t8122_dp_aux_on(core); - atcphy->dp_t8122.aux = true; - if (atc_t8122_dp_aux_is_on(core)) + if (atc_t8122_dp_aux_is_on(core)) { + atcphy->dp_t8122.aux = true; dev_dbg(atcphy->dev, "DP AUX on (mode %d, swapped %d)\n", atcphy->mode, atcphy->swap_lanes); - else + } else { + atcphy->dp_t8122.aux = false; dev_warn(atcphy->dev, "DP AUX did not power up (ctrl=%08x pwr=%08x)\n", readl(core + ATC_T8122_AUX_CTRL), readl(core + ATC_T8122_AUX_PWR)); + atc_t8122_dp_aux_off(core); + } } static int atcphy_dp_link_stop_t8122(struct apple_atcphy *atcphy) @@ -4956,6 +4959,7 @@ static void atcphy_rpm_release(void *data) /* What a power-down of the common block takes: the DisplayPort side and a tunnel clock. */ static void atcphy_forget_common_state(struct apple_atcphy *atcphy) { + atcphy->t8132_common_owned = false; atcphy->dp_t8122.aux = false; atcphy->dp_t8122.pll = false; atcphy->dp_t8122.pairs = 0; @@ -5118,9 +5122,29 @@ static int atcphy_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode int ret; guard(mutex)(&atcphy->lock); - ret = atcphy_restore_after_pd_off(atcphy); - if (ret) - return ret; + if (mode == PHY_MODE_USB_DEVICE && atcphy->hw->no_ss_device && + !atcphy->usb_off_pending && !atcphy->t8132_failed && + (!atcphy->t8132_common_owned || !READ_ONCE(atcphy->pd_state_forgotten)) && + atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off) && + atcphy_usb2_device_mode(atcphy->mode) != atcphy->mode) { + enum atcphy_mode prev_mode = atcphy->mode; + + /* + * Device startup on a port without SuperSpeed device mode does not + * need to replay an obsolete SuperSpeed mode before dwc3 leaves reset. + */ + atcphy->mode = atcphy_usb2_device_mode(prev_mode); + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) { + atcphy->mode = prev_mode; + return ret; + } + atcphy->usb2_device = true; + } else { + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; + } if (mode != PHY_MODE_USB_HOST && mode != PHY_MODE_USB_DEVICE) return -EINVAL; @@ -5256,7 +5280,8 @@ static int atcphy_usb3_power_off(struct phy *phy) keep_mode = (atcphy_mode_is_cio(atcphy->mode) && !atcphy->mode_dirty) || (atcphy_modes[atcphy->mode].enable_dp_aux && atcphy->hw->gen != ATCPHY_GENERATION_T8132); - if (!keep_mode && !atcphy->t8132_common_owned && + if (!keep_mode && !atcphy->t8132_failed && + (!atcphy->t8132_common_owned || !READ_ONCE(atcphy->pd_state_forgotten)) && atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) { enum atcphy_mode prev_mode = atcphy->mode; @@ -5383,24 +5408,22 @@ static int atcphy_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode { struct apple_atcphy *atcphy = phy_get_drvdata(phy); const bool usb2_device = atcphy->hw->no_ss_device && mode == PHY_MODE_USB_DEVICE; - enum atcphy_mode usb2_fallback = APPLE_ATCPHY_MODE_OFF; - bool skipped_ss_replay = false; int ret; guard(mutex)(&atcphy->lock); - if (usb2_device && !atcphy->usb_off_pending && !atcphy->t8132_common_owned && + if (usb2_device && !atcphy->usb_off_pending && !atcphy->t8132_failed && + (!atcphy->t8132_common_owned || !READ_ONCE(atcphy->pd_state_forgotten)) && atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off) && atcphy_usb2_device_mode(atcphy->mode) != atcphy->mode) { enum atcphy_mode prev_mode = atcphy->mode; - usb2_fallback = atcphy_usb2_device_mode(prev_mode); - atcphy->mode = APPLE_ATCPHY_MODE_OFF; + atcphy->mode = atcphy_usb2_device_mode(prev_mode); ret = atcphy_restore_after_pd_off(atcphy); if (ret) { atcphy->mode = prev_mode; return ret; } - skipped_ss_replay = true; + atcphy->usb2_device = true; } else { ret = atcphy_restore_after_pd_off(atcphy); if (ret) @@ -5436,8 +5459,7 @@ static int atcphy_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode break; case PHY_MODE_USB_DEVICE: if (usb2_device) { - enum atcphy_mode usb2 = skipped_ss_replay ? usb2_fallback : - atcphy_usb2_device_mode(atcphy->mode); + enum atcphy_mode usb2 = atcphy_usb2_device_mode(atcphy->mode); /* * No SuperSpeed device mode: serve the device role on @@ -5858,10 +5880,18 @@ int apple_atc_dp_tunnel_rate_source(struct phy *phy, unsigned int source, atcphy->tunnel_dpin_rate[dpin] = 0; return 0; } + if (!rate && atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) { + if ((atcphy->tunnel_users & BIT(dpin)) && + atcphy->tunnel_source[dpin] != source) + return -ESTALE; + atcphy->tunnel_users &= ~BIT(dpin); + atcphy->tunnel_dpin_rate[dpin] = 0; + return 0; + } ret = atcphy_restore_after_pd_off(atcphy); if (ret) return ret; - if (atcphy->mode_dirty || atcphy->pipe_pending) + if (rate && (atcphy->mode_dirty || atcphy->pipe_pending)) return -EBUSY; /* A stale source must not retune or stop another source's clock. */ if ((atcphy->tunnel_users & BIT(dpin)) && @@ -5963,10 +5993,15 @@ int apple_atc_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate) } return -EBUSY; } + if (!rate && atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) { + atcphy->tunnel_users &= ~BIT(dpin); + atcphy->tunnel_dpin_rate[dpin] = 0; + return 0; + } ret = atcphy_restore_after_pd_off(atcphy); if (ret) return ret; - if (atcphy->mode_dirty || atcphy->pipe_pending) + if (rate && (atcphy->mode_dirty || atcphy->pipe_pending)) return -EBUSY; if (!rate) { if (apple_atc_tunnel_is_t8103_style()) { @@ -6016,6 +6051,8 @@ int apple_atc_dp_tunnel_open(struct phy *phy) return -EINVAL; atcphy = phy_get_drvdata(phy); guard(mutex)(&atcphy->lock); + if (atcphy->mode != APPLE_ATCPHY_MODE_USB4 && atcphy->mode != APPLE_ATCPHY_MODE_TBT) + return -EBUSY; { int ret = atcphy_restore_after_pd_off(atcphy); @@ -7271,7 +7308,8 @@ static int atcphy_mux_update_to(struct apple_atcphy *atcphy, enum atcphy_mode ta if (atcphy->cio_sleep.prepared) return atcphy_mux_is_noop(atcphy, target_mode) ? 0 : -EBUSY; - if (eff_mode != atcphy->mode && !atcphy->t8132_common_owned && + if (eff_mode != atcphy->mode && !atcphy->t8132_failed && + (!atcphy->t8132_common_owned || !READ_ONCE(atcphy->pd_state_forgotten)) && atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off) && !atcphy_mux_check(atcphy, eff_mode)) { enum atcphy_mode prev_mode = atcphy->mode; diff --git a/tools/asahi/atc-usb3-tests.py b/tools/asahi/atc-usb3-tests.py index 786e673a5a099..60c38f664b868 100644 --- a/tools/asahi/atc-usb3-tests.py +++ b/tools/asahi/atc-usb3-tests.py @@ -105,7 +105,7 @@ def block(source, marker): /* T8132 ownership is tested without entering its lane-setup backend. */ bool t8132_ss, t8132_common_owned, t8132_failed, cio_tunables, t8132_dp_alt; /* Device role on a PHY without SuperSpeed device mode (hw->no_ss_device). */ - bool usb2_device, pd_was_off; + bool usb2_device, pd_was_off, pd_state_forgotten; struct { void *usb3_dp; } tunables; }; struct phy { struct apple_atcphy *data; }; diff --git a/tools/asahi/t6030-clock/fixture.c b/tools/asahi/t6030-clock/fixture.c index 63e999894ac5a..6805388c5a7fc 100644 --- a/tools/asahi/t6030-clock/fixture.c +++ b/tools/asahi/t6030-clock/fixture.c @@ -30,6 +30,7 @@ typedef uint64_t u64; /* REGISTER_DEFINES */ struct atcphy_hw { enum atcphy_generation gen; + bool restore_after_pd_off; }; struct apple_atcphy { const struct atcphy_hw *hw; @@ -38,7 +39,7 @@ struct apple_atcphy { struct { u8 *core; } regs; - bool idle_rpm, mode_dirty, pipe_pending; + bool idle_rpm, mode_dirty, pipe_pending, pd_was_off; bool tunnel_clock_on; u8 tunnel_rate, tunnel_users, tunnel_dpin_rate[2]; unsigned tunnel_source[2]; diff --git a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py index 0cba6abd0a73f..31c7cd0a20687 100755 --- a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py +++ b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py @@ -106,7 +106,7 @@ def main(): ''' declarations = r''' struct apple_tunable { unsigned id; }; -struct atcphy_hw { int gen; bool usb2_only, has_usb2phy_reg, park_pipe_unlocked; u32 usb2phy_sig_host; +struct atcphy_hw { int gen; bool usb2_only, has_usb2phy_reg, park_pipe_unlocked, no_ss_device, restore_after_pd_off; u32 usb2phy_sig_host; u32 cfg_offset, sync_reset; }; enum suspend_state { PM_SUSPEND_TO_IDLE = 1, PM_SUSPEND_MEM = 3 }; static enum suspend_state pm_suspend_target_state; @@ -119,7 +119,7 @@ def main(): const struct atcphy_hw *hw; struct device *dev; bool t8132_ss, t8132_common_owned, t8132_suspended, t8132_failed, host_active, dwc3_running; - bool ss_tunables, mode_dirty, pipe_pending, swap_lanes; + bool ss_tunables, mode_dirty, pipe_pending, swap_lanes, usb_off_pending, usb2_device; bool cio_tunables, t8132_dp_alt, fixed_usb2, hibernated, pd_was_off, pd_state_forgotten, idle_rpm; bool t8103_dummy_pending, t8103_usb3_workaround, fixed_usb3, cio_only; struct device *pd_hold; @@ -153,6 +153,7 @@ def main(): static void dev_pm_genpd_suspend(struct device *d) { assert(d == &hold_dev); pd_suspended++; pd_sync_off(); } static void dev_pm_genpd_resume(struct device *d) { assert(d == &hold_dev); pd_off = false; pd_suspended--; } static struct apple_atcphy *phy_get_drvdata(struct phy *p) { return p->atc; } +static int atcphy_usb2_device_mode(int m) { return (m == 2 || m == 3) ? 1 : m; } /* T8152 only: these rows run T8132, which never reaches it. */ static void atcphy_lanes_down_t8152(struct apple_atcphy *a) { (void)a; assert(0); } static u32 core[0x24000/4], usb2[0x4000/4], pipe_regs[0x4000/4]; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py index 1ba7552ed26fd..18e956a08520e 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py @@ -165,7 +165,7 @@ def make_unit(source, parent=False, reference=None): struct { void *usb2phy, *pipehandler; } regs; bool fixed_usb2,cio_only,host_active,dwc3_running,mode_dirty,pipe_pending,usb_off_pending,ss_tunables,usb3_tunables,dp_tunables,cio_tunables,t8132_dp_alt; bool usb2_device; - bool usb3_port_off,park_owed,dp_only,pd_was_off,pd_state_forgotten,t8132_common_owned; + bool usb3_port_off,park_owed,dp_only,pd_was_off,pd_state_forgotten,t8132_common_owned,t8132_failed; bool t8103_dummy_pending,t8103_usb3_retry,t8103_usb3_workaround; enum atcphy_mode mode,typec_mode; enum atcphy_pipehandler_state pipe_state; From 81e7c252b41540341601941ec83869c1f2d718e3 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 4 Dec 2026 20:54:49 -0500 Subject: [PATCH 11/11] phy: apple: atc: propagate DP AUX failures, validate DP safely, and balance USB2 gadget state Address five remaining DP validation, AUX recovery, and USB2 device fallback edge cases: 1. In atcphy_dpphy_validate(), return 0 lanes without touching hardware for non-DP modes, restore pending cold DP/USB3_DP modes on pd_was_off (via atcphy_rpm_dpphy_validate()), and return -EBUSY (rather than 0 with 0 lanes, which DPTX converts into a 4-lane reply) when a DP mode is dirty, PIPE-pending, or missing AUX power on T8122/T6030 or T8132. 2. Return -ETIMEDOUT from atcphy_dp_aux_on_t8122() when AUX fails to power up, keeping mode_dirty set in atcphy_configure() and atcphy_configure_t8132() so same-mode retries re-run AUX power-up. 3. Track usb2_device across both PHY_MODE_USB_DEVICE and PHY_MODE_USB_HOST in atcphy_usb2_set_mode() on no_ss_device ports so gadget startup from OFF+typec_mode=USB3 configures USB2 in atcphy_usb2_init(), while DWC3 gadget-start rollback to HOST clears usb2_device and a subsequent host usb3_set_mode() still lifts USB2 back to typec_mode. Signed-off-by: DjDeveloperr --- drivers/phy/apple/atc.c | 93 +++++++++++++++---- .../selftests/drivers/apple/atc-t8132-usb3.py | 2 +- .../drivers/gpu/apple-dcp/dwc3/mocks.c | 3 +- .../selftests/drivers/gpu/apple-dcp/phy.py | 5 +- 4 files changed, 82 insertions(+), 21 deletions(-) diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index 6df3395b08985..8237e43688a27 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -4189,7 +4189,7 @@ static unsigned int atcphy_dp_lane_flags(const struct apple_atcphy *atcphy, (link_rate == 8100 ? ATC_T8132_DP_NO_LDOCLK_BYPASS : 0); } -static void atcphy_dp_aux_on_t8122(struct apple_atcphy *atcphy) +static int atcphy_dp_aux_on_t8122(struct apple_atcphy *atcphy) { void __iomem *core = atcphy->regs.core; @@ -4199,12 +4199,13 @@ static void atcphy_dp_aux_on_t8122(struct apple_atcphy *atcphy) atcphy->dp_t8122.aux = true; dev_dbg(atcphy->dev, "DP AUX on (mode %d, swapped %d)\n", atcphy->mode, atcphy->swap_lanes); - } else { - atcphy->dp_t8122.aux = false; - dev_warn(atcphy->dev, "DP AUX did not power up (ctrl=%08x pwr=%08x)\n", - readl(core + ATC_T8122_AUX_CTRL), readl(core + ATC_T8122_AUX_PWR)); - atc_t8122_dp_aux_off(core); + return 0; } + atcphy->dp_t8122.aux = false; + dev_warn(atcphy->dev, "DP AUX did not power up (ctrl=%08x pwr=%08x)\n", + readl(core + ATC_T8122_AUX_CTRL), readl(core + ATC_T8122_AUX_PWR)); + atc_t8122_dp_aux_off(core); + return -ETIMEDOUT; } static int atcphy_dp_link_stop_t8122(struct apple_atcphy *atcphy) @@ -4702,7 +4703,9 @@ static int atcphy_configure_t8132(struct apple_atcphy *atcphy, enum atcphy_mode /* AUX comes up after RCAL; the DCP's link rate starts the main link. */ if ((mode == APPLE_ATCPHY_MODE_USB3_DP || dp_only) && atcphy_t8132_dp_on(atcphy)) { atcphy->mode = mode; - atcphy_dp_aux_on_t8122(atcphy); + ret = atcphy_dp_aux_on_t8122(atcphy); + if (ret) + return ret; } done: @@ -4875,13 +4878,16 @@ static int atcphy_configure(struct apple_atcphy *atcphy, enum atcphy_mode mode) } } + /* T8122: the AUX block needs the PHY out of reset and calibrated */ + if (atcphy->hw->dp_t8122 && atcphy_modes[mode].enable_dp_aux) { + ret = atcphy_dp_aux_on_t8122(atcphy); + if (ret) + return ret; + } + atcphy->mode = mode; atcphy->mode_dirty = false; - /* T8122: the AUX block needs the PHY out of reset and calibrated */ - if (atcphy->hw->dp_t8122 && atcphy_modes[mode].enable_dp_aux) - atcphy_dp_aux_on_t8122(atcphy); - return 0; } @@ -5155,9 +5161,13 @@ static int atcphy_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode switch (mode) { case PHY_MODE_USB_HOST: set32(atcphy->regs.usb2phy + USB2PHY_SIG, atcphy_usb2_sig_host(atcphy)); + if (atcphy->hw->no_ss_device) + atcphy->usb2_device = false; break; case PHY_MODE_USB_DEVICE: clear32(atcphy->regs.usb2phy + USB2PHY_SIG, atcphy_usb2_sig_host(atcphy)); + if (atcphy->hw->no_ss_device) + atcphy->usb2_device = true; break; default: return -EINVAL; @@ -5435,7 +5445,10 @@ static int atcphy_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode switch (mode) { case PHY_MODE_USB_HOST: - if (atcphy->usb2_device) { + if (atcphy->usb2_device || + (atcphy->hw->no_ss_device && atcphy->mode == APPLE_ATCPHY_MODE_USB2 && + atcphy_usb2_device_mode(atcphy->typec_mode) != atcphy->typec_mode)) { + bool was_usb2_device = atcphy->usb2_device; enum atcphy_mode target = atcphy->typec_mode; /* @@ -5448,7 +5461,7 @@ static int atcphy_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode atcphy_usb2_device_mode(target) != target) { ret = atcphy_mux_switch(atcphy, target); if (ret) { - atcphy->usb2_device = true; + atcphy->usb2_device = was_usb2_device; return ret; } } @@ -5662,6 +5675,7 @@ static int atcphy_dpphy_validate(struct phy *phy, enum phy_mode mode, int submod { struct phy_configure_opts_dp *opts = &opts_->dp; struct apple_atcphy *atcphy = phy_get_drvdata(phy); + int ret; if (mode != PHY_MODE_DP) return -EINVAL; @@ -5670,12 +5684,27 @@ static int atcphy_dpphy_validate(struct phy *phy, enum phy_mode mode, int submod /* The Type-C mux owns lane setup; validate observes its completed epoch. */ guard(mutex)(&atcphy->lock); - if (atcphy->mode_dirty || atcphy->pipe_pending || - (atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) || - (atcphy->hw->dp_t8122 && !atcphy->dp_t8122.aux)) { + if (!atcphy_modes[atcphy->mode].enable_dp_aux) { + opts->lanes = 0; + return 0; + } + if (atcphy->hw->restore_after_pd_off && READ_ONCE(atcphy->pd_was_off)) { + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) { + opts->lanes = 0; + return ret; + } + } + if (!atcphy_modes[atcphy->mode].enable_dp_aux) { opts->lanes = 0; return 0; } + if (atcphy->mode_dirty || atcphy->pipe_pending || + ((atcphy->hw->dp_t8122 || atcphy_t8132_dp_on(atcphy)) && + !atcphy->dp_t8122.aux)) { + opts->lanes = 0; + return -EBUSY; + } switch (atcphy->mode) { case APPLE_ATCPHY_MODE_USB3_DP: @@ -5691,6 +5720,36 @@ static int atcphy_dpphy_validate(struct phy *phy, enum phy_mode mode, int submod return 0; } +static int atcphy_rpm_dpphy_validate(struct phy *phy, enum phy_mode mode, int submode, + union phy_configure_opts *opts) +{ + struct apple_atcphy *atcphy = phy_get_drvdata(phy); + bool need_rpm; + int ret; + + if (mode != PHY_MODE_DP) + return -EINVAL; + if (submode != 0 && !atcphy_t8132_dp_on(atcphy)) + return -EINVAL; + + scoped_guard(mutex, &atcphy->lock) + need_rpm = atcphy->idle_rpm && + atcphy_modes[atcphy->mode].enable_dp_aux && + atcphy->hw->restore_after_pd_off && + READ_ONCE(atcphy->pd_was_off); + if (need_rpm) { + ret = atcphy_rpm_get(atcphy); + if (ret) { + opts->dp.lanes = 0; + return ret; + } + } + ret = atcphy_dpphy_validate(phy, mode, submode, opts); + if (need_rpm) + atcphy_rpm_put(atcphy); + return ret; +} + static int atcphy_dpphy_configure_t8122(struct apple_atcphy *atcphy, struct phy_configure_opts_dp *opts) { @@ -5829,7 +5888,7 @@ static int atcphy_dpphy_configure(struct phy *phy, union phy_configure_opts *opt static const struct phy_ops apple_atc_dp_phy_ops = { .owner = THIS_MODULE, .configure = atcphy_dpphy_configure, - .validate = atcphy_dpphy_validate, + .validate = atcphy_rpm_dpphy_validate, .set_mode = atcphy_dpphy_set_mode, }; diff --git a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py index 31c7cd0a20687..112bd91fbcd2b 100755 --- a/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py +++ b/tools/testing/selftests/drivers/apple/atc-t8132-usb3.py @@ -233,7 +233,7 @@ def main(): /* No DP tunnel clock, DP alt mode or CIO runs on this port: the stops are no-ops. */ static int atc_t8122_tunnel_stop(struct apple_atcphy *a) { (void)a; tunnel_stops++; return 0; } static void atcphy_dp_stop_t8122(struct apple_atcphy *a) { (void)a; dp_stops++; } -static void atcphy_dp_aux_on_t8122(struct apple_atcphy *a) { (void)a; assert(0); } +static int atcphy_dp_aux_on_t8122(struct apple_atcphy *a) { (void)a; assert(0); return 0; } static void atcphy_t8132_cio_start(struct apple_atcphy *a) { (void)a; assert(0); } static const struct atcphy_mode_configuration * atcphy_get_mode_config(struct apple_atcphy *a, enum atcphy_mode m) { (void)a; (void)m; assert(0); return 0; } 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 602a6b56adaca..0e5009dd58df0 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c @@ -237,9 +237,10 @@ static void atcphy_dp_stop_t8122(struct apple_atcphy *p) assert(!p->dp_t8122.aux && !p->dp_t8122.pll); } -static void atcphy_dp_aux_on_t8122(struct apple_atcphy *p) +static int atcphy_dp_aux_on_t8122(struct apple_atcphy *p) { assert(!p->hw->dp_t8122); + return 0; } static bool of_machine_is_compatible(const char *name) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py index 61f615beed3fa..ce6b3e8112488 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/phy.py @@ -2458,8 +2458,9 @@ def function(source, name): { assert(!p->tunnel_clock_on && !p->tunnel_users); return 0; } static void atcphy_dp_stop_t8122(struct apple_atcphy *p) { assert(!p->dp_t8122.aux && !p->dp_t8122.pll); } -static void atcphy_dp_aux_on_t8122(struct apple_atcphy *p) -{ assert(!p->hw->dp_t8122); } +static int atcphy_dp_aux_on_t8122(struct apple_atcphy *p) +{ assert(!p->hw->dp_t8122); return 0; +} static bool of_machine_is_compatible(const char *name) { (void)name; return false; } #ifndef READ_ONCE #define READ_ONCE(x) (x)