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/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/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index 92faaa3d4f801..3b865e3feaded 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; }; @@ -365,7 +369,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. */ @@ -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; @@ -399,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); @@ -443,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); @@ -588,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); @@ -599,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) { /* @@ -620,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); @@ -650,6 +713,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; @@ -686,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); @@ -831,6 +878,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; @@ -857,16 +905,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; } @@ -909,6 +958,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 +1040,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 +1467,47 @@ 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++) { + 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 (held) + rpm_puts++; + if (preselected) + 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 +1575,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 +1593,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 && @@ -1642,6 +1742,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 +1825,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, 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-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-test.c b/drivers/phy/apple/atc-test.c index e2c880e4c071f..fc7590eed6eb4 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,89 @@ 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_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); + 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); + KUNIT_EXPECT_FALSE(test, atcphy->pd_state_forgotten); +} + static void atcphy_test_t8103_usb3_machine_scope(struct kunit *test) { static const struct { @@ -1539,45 +1622,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)); + + /* 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)); + 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[] = { @@ -1636,6 +1737,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 d5912f1e71605..8237e43688a27 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; @@ -2483,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) @@ -3009,8 +3013,6 @@ 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_dp_stop(struct apple_atcphy *atcphy) { const struct atcphy_mode_configuration *mode_cfg; @@ -3018,10 +3020,13 @@ static int atcphy_dp_stop(struct apple_atcphy *atcphy) u32 reg; guard(mutex)(&atcphy->lock); - if (atcphy->mode_dirty || atcphy->pipe_pending) - return -EBUSY; + ret = atcphy_restore_after_pd_off(atcphy); + if (ret) + return ret; if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) return -EBUSY; + 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,8 +3034,6 @@ static int atcphy_dp_stop(struct apple_atcphy *atcphy) } if (atcphy->dp_link_rate < 0) return 0; - - atcphy_restore_after_pd_off(atcphy); mode_cfg = atcphy_get_mode_config(atcphy, atcphy->mode); core_set32(atcphy, ACIOPHY_LANE_DP_CFG_BLK_TX_DP_CTRL0, DP_PMA_BYTECLK_RESET); @@ -3063,13 +3066,18 @@ static int atcphy_dp_configure(struct apple_atcphy *atcphy, enum atcphy_dp_link_ u32 reg; guard(mutex)(&atcphy->lock); + 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; - /* the AUSPLL is in use as the Thunderbolt DP tunnel pixel clock */ - if (atcphy->tunnel_clock_on || atcphy->tunnel_saved) + if (atcphy->mode != APPLE_ATCPHY_MODE_DP && + atcphy->mode != APPLE_ATCPHY_MODE_USB3_DP) return -EBUSY; - atcphy_restore_after_pd_off(atcphy); mode_cfg = atcphy_get_mode_config(atcphy, atcphy->mode); cfg = &dp_lr_config[lr]; @@ -4181,19 +4189,23 @@ 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; 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 - 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)); + 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) @@ -4691,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: @@ -4864,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; } @@ -4948,11 +4965,18 @@ 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; 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) { @@ -4989,7 +5013,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,12 +5022,19 @@ 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); + /* + * 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); @@ -5013,9 +5044,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 +5061,17 @@ 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->t8103_dummy_pending = false; + atcphy->t8103_usb3_retry = false; atcphy->pipe_state = ATCPHY_PIPEHANDLER_STATE_DUMMY; } dev_dbg(atcphy->dev, @@ -5052,20 +5093,64 @@ 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 (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) { + dev_warn(atcphy->dev, + "Failed to restore mode %d after its domain was off: %d\n", + mode, ret); + atcphy->mode = mode; + return ret; + } + } else { + atcphy->mode_dirty = false; + } + + WRITE_ONCE(atcphy->pd_was_off, false); + WRITE_ONCE(atcphy->pd_state_forgotten, 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); + 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; @@ -5076,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; @@ -5094,9 +5183,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 && @@ -5191,10 +5283,33 @@ 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); - atcphy_restore_after_pd_off(atcphy); + 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_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; + + /* + * 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 @@ -5306,14 +5421,34 @@ 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); + 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; + + 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 (atcphy->usb_off_pending) return -EBUSY; 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; /* @@ -5326,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; } } @@ -5540,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; @@ -5548,11 +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->hw->dp_t8122 && - (atcphy->mode_dirty || !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: @@ -5568,13 +5720,45 @@ 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) { 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 +5799,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", @@ -5624,8 +5810,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); @@ -5695,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, }; @@ -5728,13 +5921,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) @@ -5746,7 +5939,19 @@ 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); + 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 (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)) && atcphy->tunnel_source[dpin] != source) @@ -5842,10 +6047,21 @@ 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; } + 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 (rate && (atcphy->mode_dirty || atcphy->pipe_pending)) + return -EBUSY; if (!rate) { if (apple_atc_tunnel_is_t8103_style()) { /* @@ -5894,6 +6110,16 @@ 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); + + 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) { @@ -6266,7 +6492,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 +6515,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); @@ -7131,9 +7362,33 @@ 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; - atcphy_restore_after_pd_off(atcphy); + 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; + + /* + * 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) { + 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 @@ -7147,8 +7402,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); @@ -8471,8 +8725,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; } @@ -8587,8 +8843,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 +8856,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 +8877,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 +8960,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); @@ -9157,6 +9419,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); } @@ -9170,8 +9433,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; @@ -9221,8 +9486,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; @@ -9566,6 +9831,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); @@ -9656,9 +9922,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-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/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..60c38f664b868 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, pd_state_forgotten; struct { void *usb3_dp; } tunables; }; struct phy { struct apple_atcphy *data; }; @@ -177,7 +178,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/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( diff --git a/tools/asahi/t6030-clock/fixture.c b/tools/asahi/t6030-clock/fixture.c index 3c5b73f77e02a..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,6 +39,7 @@ struct apple_atcphy { struct { u8 *core; } regs; + 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]; @@ -105,8 +107,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..112bd91fbcd2b 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,8 +119,8 @@ 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 cio_tunables, t8132_dp_alt, fixed_usb2, hibernated, pd_was_off, idle_rpm; + 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; bool pd_hold_sleep, pd_hold_released; @@ -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]; @@ -232,12 +233,12 @@ 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; } 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..0e5009dd58df0 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; + 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; @@ -235,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) @@ -266,7 +269,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..ce6b3e8112488 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, 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; @@ -163,7 +165,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); @@ -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, 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; @@ -1989,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; @@ -2015,7 +2021,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. */ @@ -2377,7 +2383,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, 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; @@ -2450,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) @@ -2461,6 +2470,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) @@ -2712,7 +2722,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, 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]; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py b/tools/testing/selftests/drivers/gpu/apple-dcp/quiesce.py index 682a79c00769f..18e956a08520e 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; + 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; @@ -180,7 +183,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. */