Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions drivers/phy/apple/atc-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,10 @@ static void atcphy_test_t8132_rpm_busy(struct kunit *test)
}

#ifdef CONFIG_SUSPEND
/* An epoch with a host survives suspend-to-idle only; without one, or incomplete, never. */
/*
* An epoch with DWC3 running, or in CIO mode with the domain held, survives
* suspend-to-idle only; without either, or incomplete, never.
*/
static void atcphy_test_t8132_system_suspend(struct kunit *test)
{
struct apple_atcphy *atcphy = atcphy_test_alloc_powered_off(test);
Expand All @@ -1316,12 +1319,14 @@ static void atcphy_test_t8132_system_suspend(struct kunit *test)
KUNIT_EXPECT_EQ(test, atcphy_suspend(atcphy->dev), -EBUSY);
atcphy->mode_dirty = false;

/* Without a host, as in a USB3 device role, nothing holds the domain */
/* With DWC3 held in reset, nothing holds the domain */
atcphy->t8132_common_owned = true;
KUNIT_EXPECT_EQ(test, atcphy_suspend(atcphy->dev), -EBUSY);
KUNIT_EXPECT_FALSE(test, atcphy->t8132_suspended);

/* A running DWC3 does, as a host or as a device */
atcphy->host_active = true;
atcphy->dwc3_running = true;
KUNIT_EXPECT_EQ(test, atcphy_suspend(atcphy->dev), 0);
KUNIT_EXPECT_TRUE(test, atcphy->t8132_suspended);
KUNIT_EXPECT_EQ(test, atcphy_resume(atcphy->dev), 0);
Expand All @@ -1331,6 +1336,29 @@ static void atcphy_test_t8132_system_suspend(struct kunit *test)
KUNIT_EXPECT_EQ(test, atcphy_suspend(atcphy->dev), -EBUSY);
KUNIT_EXPECT_FALSE(test, atcphy->t8132_suspended);

/* In CIO mode, without DWC3, only a held domain survives suspend-to-idle */
atcphy->host_active = false;
atcphy->dwc3_running = false;
atcphy->mode = APPLE_ATCPHY_MODE_USB4;
pm_suspend_target_state = PM_SUSPEND_TO_IDLE;
KUNIT_EXPECT_EQ(test, atcphy_suspend(atcphy->dev), -EBUSY);
KUNIT_EXPECT_FALSE(test, atcphy->pd_hold_sleep);

/* Only the pointer is used: a held domain is never given up to genpd */
atcphy->pd_hold = atcphy->dev;
KUNIT_EXPECT_EQ(test, atcphy_suspend(atcphy->dev), 0);
KUNIT_EXPECT_TRUE(test, atcphy->pd_hold_sleep);
scoped_guard(mutex, &atcphy->lock)
atcphy_pd_hold_release(atcphy);
KUNIT_EXPECT_FALSE(test, atcphy->pd_hold_released);
KUNIT_EXPECT_EQ(test, atcphy_resume(atcphy->dev), 0);
KUNIT_EXPECT_FALSE(test, atcphy->t8132_suspended);
KUNIT_EXPECT_FALSE(test, atcphy->pd_hold_sleep);

pm_suspend_target_state = PM_SUSPEND_MEM;
KUNIT_EXPECT_EQ(test, atcphy_suspend(atcphy->dev), -EBUSY);
KUNIT_EXPECT_FALSE(test, atcphy->pd_hold_sleep);

pm_suspend_target_state = target;
}
#endif
Expand Down
147 changes: 139 additions & 8 deletions drivers/phy/apple/atc.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,9 @@ struct atcphy_cio_sleep {
* it when the PHY is brought back up after a power-off
* @host_active: dwc3 has selected host mode on the USB3 PHY; a fixed-hub port
* then routes the PIPE itself on later Type-C mode changes
* @dwc3_running: dwc3 is out of the reset this PHY provides, in either role; follows
* the reset controller's assert/deassert only, not the register (the
* sleep hibernation steps toggle DWC3_RESET_N within one controller run)
* @usb3_port_off: The SuperSpeed root port of a fixed-hub controller was powered
* off for a PIPE or lane change and is not powered on again yet
* @usb3_port_work: Retries the power-on of @usb3_port_off after a USB failure
Expand Down Expand Up @@ -943,6 +946,13 @@ struct atcphy_cio_sleep {
* @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
* @pd_hold: Second, syscore, consumer of the PHY's own power domain (T8132), so
* that a CIO-mode port can keep the domain on across suspend-to-idle,
* see atcphy_cio_can_hold()
* @pd_hold_sleep: This suspend-to-idle keeps the domain on through @pd_hold;
* under @lock
* @pd_hold_released: @pd_hold is counted as suspended for this system sleep;
* under @lock
* @regs: Memory-mapped registers
* @regs.core: Core registers
* @regs.axi2af: AXI to Apple Fabric interface registers
Expand Down Expand Up @@ -1036,6 +1046,7 @@ struct apple_atcphy {
u8 dp_usb3_lane;
enum atcphy_mode typec_mode;
bool host_active;
bool dwc3_running;
/*
* The controller sleeps in place for this system sleep, the SuperSpeed side is checked
* against the mux after it, and the controller is in hibernation now.
Expand Down Expand Up @@ -1082,6 +1093,9 @@ struct apple_atcphy {
bool pd_was_off;
bool idle_rpm;
bool rpm_held;
struct device *pd_hold;
bool pd_hold_sleep;
bool pd_hold_released;

bool tunnel_dual_stream;
bool tunnel_routes_present;
Expand Down Expand Up @@ -1708,6 +1722,18 @@ static bool atcphy_mode_is_cio(enum atcphy_mode mode)
return mode == APPLE_ATCPHY_MODE_TBT || mode == APPLE_ATCPHY_MODE_USB4;
}

/*
* Whether a port in CIO mode can keep its domain, and with it the PHY state the
* USB4 router runs on, across suspend-to-idle: only by holding it itself, see
* atcphy_probe_pd_hold(). Not through a router: a board need not have one (the
* J604 has CIO tunables and no ACIO), and one does not hold this domain. The
* hold keeps the parent, ATCn_COMMON, on as well.
*/
static bool atcphy_cio_can_hold(const struct apple_atcphy *atcphy)
{
return atcphy_mode_is_cio(atcphy->mode) && atcphy->pd_hold;
}

/*
* The PIPE backend a mode needs. T8132 carries no USB3 over USB4 yet, so its
* PIPE stays on the dummy backend in CIO, as in a mode without USB3. Routed to
Expand Down Expand Up @@ -6211,6 +6237,7 @@ static void _atcphy_dwc3_reset_assert(struct apple_atcphy *atcphy)
{
lockdep_assert_held(&atcphy->lock);

atcphy->dwc3_running = false;
clear32(atcphy->regs.pipehandler + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N);
set32(atcphy->regs.pipehandler + PIPEHANDLER_AON_GEN,
PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN);
Expand Down Expand Up @@ -6261,6 +6288,7 @@ static int atcphy_dwc3_reset_deassert(struct reset_controller_dev *rcdev, unsign
clear32(atcphy->regs.pipehandler + PIPEHANDLER_AON_GEN,
PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN);
set32(atcphy->regs.pipehandler + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N);
atcphy->dwc3_running = true;

return 0;
}
Expand Down Expand Up @@ -8436,6 +8464,40 @@ static void atcphy_pd_notifier_remove(void *data)
dev_pm_genpd_remove_notifier(data);
}

static void atcphy_pd_hold_remove(void *data)
{
dev_pm_domain_detach(data, false);
}

/*
* The PHY's own domain, ATCn_USB, is held by nothing else across system sleep
* while it runs a USB4 link: the ACIO holds the CIO and PCIe states and, through
* them, ATCn_COMMON, and DWC3, held in reset, lets its domain go. So attach a
* second consumer to it, as the ACIO does for its own states: a syscore device
* the system sleep leaves alone, whose share of the domain the PHY gives up in
* its noirq suspend unless it keeps the domain on, see atcphy_sleep_suspend_noirq().
* Runtime suspended, it leaves runtime PM of the domain to the PHY.
*/
static int atcphy_probe_pd_hold(struct apple_atcphy *atcphy)
{
struct device *hold;

if (!atcphy->t8132_ss || !atcphy->cio_tunables)
return 0;
hold = genpd_dev_pm_attach_by_id(atcphy->dev, 0);
if (IS_ERR(hold) && PTR_ERR(hold) == -EPROBE_DEFER)
return -EPROBE_DEFER;
if (IS_ERR_OR_NULL(hold)) {
dev_info(atcphy->dev,
"no power domain to hold (%d): USB4 is not kept across suspend-to-idle\n",
PTR_ERR_OR_ZERO(hold));
return 0;
}
dev_pm_syscore_device(hold, true);
atcphy->pd_hold = hold;
return devm_add_action_or_reset(atcphy->dev, atcphy_pd_hold_remove, hold);
}

static void atcphy_usb2_repeater_exit(void *data)
{
phy_exit(data);
Expand Down Expand Up @@ -8609,6 +8671,9 @@ static int atcphy_probe(struct platform_device *pdev)
return ret;
}
followed = atcphy_probe_pd_notifier(atcphy);
ret = atcphy_probe_pd_hold(atcphy);
if (ret)
return ret;

/*
* Only a PHY that follows its domain, advertises idle power-off (or
Expand Down Expand Up @@ -9513,6 +9578,7 @@ static int atcphy_suspend(struct device *dev)
return 0;

guard(mutex)(&atcphy->lock);
atcphy->pd_hold_sleep = false;
/* An incomplete common epoch has no qualified sleep handoff. */
if (atcphy->mode_dirty)
return -EBUSY;
Expand All @@ -9521,13 +9587,19 @@ static int atcphy_suspend(struct device *dev)
return 0;
}
/*
* An active one survives suspend-to-idle while a host runs on it: DWC3
* then keeps the power domain on across system sleep, so the link stays
* as it is. Without a host, or in a deeper sleep state, it is lost.
* An active one survives suspend-to-idle while DWC3 runs on it, as a
* host or as a device: the glue then keeps the controller, and with it
* the power domain this PHY shares, on across system sleep, so the link
* stays as it is. So does one in CIO mode, whose domain the PHY holds
* itself until the resume: the router's power states do not keep it.
* Without either, or in a deeper sleep state, it is lost.
*/
if (atcphy->t8132_common_owned &&
(!atcphy->host_active || pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
(pm_suspend_target_state != PM_SUSPEND_TO_IDLE ||
!(atcphy->dwc3_running || atcphy_cio_can_hold(atcphy))))
return -EBUSY;
atcphy->pd_hold_sleep = pm_suspend_target_state == PM_SUSPEND_TO_IDLE &&
atcphy_cio_can_hold(atcphy);
/* Do not admit a new epoch after this check and before resume. */
atcphy->t8132_suspended = true;

Expand All @@ -9547,9 +9619,16 @@ static int atcphy_resume(struct device *dev)
if (atcphy->cio_sleep.prepared)
return atcphy->cio_sleep.changed && !atcphy->cio_sleep.restored ? -EIO : 0;
atcphy->t8132_suspended = false;
if (atcphy->t8132_common_owned && atcphy->host_active &&
atcphy->pd_hold_sleep = false;
/*
* Suspend admitted a common epoch only with DWC3 running, in either role,
* or in CIO mode with the domain held: neither should have lost it.
*/
if (((atcphy->t8132_common_owned && atcphy->dwc3_running) ||
atcphy_mode_is_cio(atcphy->mode)) &&
READ_ONCE(atcphy->pd_was_off))
dev_warn(dev, "power domain went off under an active USB3 link\n");
dev_warn(dev, "power domain went off under an active %s link\n",
atcphy_mode_is_cio(atcphy->mode) ? "USB4" : "USB3");
/* DP tunnel pixel clocks are then set up again at their next rate change. */
if (atcphy->tunnel_users && READ_ONCE(atcphy->pd_was_off))
dev_dbg(dev, "power domain went off across sleep under DP tunnel clocks (users %#x)\n",
Expand Down Expand Up @@ -9577,7 +9656,8 @@ static void atcphy_shutdown(struct platform_device *pdev)
atcphy->t8132_suspended = true;
if (!atcphy->t8132_common_owned)
return;
if (atcphy->t8132_failed || atcphy->host_active || atcphy->pipe_pending ||
/* DWC3 still out of reset, in either role, means its shutdown did not finish. */
if (atcphy->t8132_failed || atcphy->dwc3_running || atcphy->pipe_pending ||
atcphy->pipe_state != ATCPHY_PIPEHANDLER_STATE_DUMMY) {
dev_err(atcphy->dev, "Shutdown retains incomplete USB3 common ownership\n");
return;
Expand All @@ -9587,9 +9667,60 @@ static void atcphy_shutdown(struct platform_device *pdev)
dev_err(atcphy->dev, "USB3 common shutdown failed: %d\n", ret);
}

/*
* Give up the hold's share of the domain for this sleep, unless the PHY keeps
* the domain on: genpd then powers it off once its other devices suspend, as
* before. Not for hibernation, whose noirq phases genpd counts differently.
*/
static void atcphy_pd_hold_release(struct apple_atcphy *atcphy)
{
lockdep_assert_held(&atcphy->lock);
if (!atcphy->pd_hold || atcphy->pd_hold_sleep)
return;
dev_pm_genpd_suspend(atcphy->pd_hold);
atcphy->pd_hold_released = true;
}

static void atcphy_pd_hold_reclaim(struct apple_atcphy *atcphy)
{
lockdep_assert_held(&atcphy->lock);
if (!atcphy->pd_hold_released)
return;
dev_pm_genpd_resume(atcphy->pd_hold);
atcphy->pd_hold_released = false;
}

/* No resume_noirq follows a failed suspend_noirq: release only on success. */
static int atcphy_sleep_suspend_noirq(struct device *dev)
{
struct apple_atcphy *atcphy = dev_get_drvdata(dev);
int ret;

ret = atcphy_suspend_noirq(dev);
if (ret)
return ret;
guard(mutex)(&atcphy->lock);
atcphy_pd_hold_release(atcphy);
return 0;
}

static int atcphy_sleep_resume_noirq(struct device *dev)
{
struct apple_atcphy *atcphy = dev_get_drvdata(dev);

scoped_guard(mutex, &atcphy->lock)
atcphy_pd_hold_reclaim(atcphy);
return atcphy_resume_noirq(dev);
}

static const struct dev_pm_ops atcphy_pm_ops = {
SYSTEM_SLEEP_PM_OPS(atcphy_suspend, atcphy_resume)
NOIRQ_SYSTEM_SLEEP_PM_OPS(atcphy_suspend_noirq, atcphy_resume_noirq)
.suspend_noirq = pm_sleep_ptr(atcphy_sleep_suspend_noirq),
.resume_noirq = pm_sleep_ptr(atcphy_sleep_resume_noirq),
.freeze_noirq = pm_sleep_ptr(atcphy_suspend_noirq),
.thaw_noirq = pm_sleep_ptr(atcphy_resume_noirq),
.poweroff_noirq = pm_sleep_ptr(atcphy_suspend_noirq),
.restore_noirq = pm_sleep_ptr(atcphy_resume_noirq),
};

static const struct atcphy_hw atcphy_hw_t8103 = {
Expand Down
18 changes: 12 additions & 6 deletions tools/asahi/atc-usb3-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def block(source, marker):
enum atcphy_pipehandler_state pipe_state;
enum atcphy_mode typec_mode;
bool pipe_pending, t8103_dummy_pending, t8103_usb3_retry, t8103_usb3_workaround, mode_dirty, usb_off_pending;
bool host_active, usb3_port_off, dp_only, ss_tunables, fixed_usb2, fixed_usb3, t8140_dp_tunables, dp_tunables, cio_only, usb3_tunables;
bool host_active, dwc3_running, usb3_port_off, dp_only, ss_tunables, fixed_usb2, fixed_usb3, t8140_dp_tunables, dp_tunables, cio_only, usb3_tunables;
/* 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). */
Expand Down Expand Up @@ -402,16 +402,16 @@ def block(source, marker):
static void reset_release_retries_before_writes(void) {
setup_legacy_usb3();unlock_ret=-ETIMEDOUT;assert(atcphy_park_live_t8103_pipehandler(&p)==-ETIMEDOUT);
u32 aon=BIT(14)|PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN;writel(aon,memory+PIPEHANDLER_AON_GEN);
assert(atcphy_dwc3_reset_deassert(&p.rcdev,0)==-ETIMEDOUT);assert(readl(memory+PIPEHANDLER_AON_GEN)==aon);
unlock_ret=0;assert(!atcphy_dwc3_reset_deassert(&p.rcdev,0));assert(!p.t8103_dummy_pending&&p.t8103_usb3_retry);
assert(atcphy_dwc3_reset_deassert(&p.rcdev,0)==-ETIMEDOUT);assert(readl(memory+PIPEHANDLER_AON_GEN)==aon&&!p.dwc3_running);
unlock_ret=0;assert(!atcphy_dwc3_reset_deassert(&p.rcdev,0));assert(!p.t8103_dummy_pending&&p.t8103_usb3_retry&&p.dwc3_running);
assert(readl(memory+PIPEHANDLER_AON_GEN)==((aon&~PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN)|PIPEHANDLER_AON_GEN_DWC3_RESET_N));
assert(!atcphy_usb3_set_mode(&phy,PHY_MODE_USB_HOST,0));assert(!p.t8103_usb3_retry&&p.pipe_state==ATCPHY_PIPEHANDLER_STATE_USB3);
}
static void reset_assert_failure_and_stop(void) {
setup_legacy_usb3();unlock_ret=-ETIMEDOUT;assert(atcphy_park_live_t8103_pipehandler(&p)==-ETIMEDOUT);
u32 aon=BIT(14)|PIPEHANDLER_AON_GEN_DWC3_RESET_N;writel(aon,memory+PIPEHANDLER_AON_GEN);
assert(atcphy_dwc3_reset_assert(&p.rcdev,0)==-ETIMEDOUT);assert(readl(memory+PIPEHANDLER_AON_GEN)==aon&&!usb2_power_off_calls);
unlock_ret=0;assert(!atcphy_dwc3_reset_assert(&p.rcdev,0));assert(!p.t8103_dummy_pending&&!p.t8103_usb3_retry&&usb2_power_off_calls==1);
u32 aon=BIT(14)|PIPEHANDLER_AON_GEN_DWC3_RESET_N;writel(aon,memory+PIPEHANDLER_AON_GEN);p.dwc3_running=true;
assert(atcphy_dwc3_reset_assert(&p.rcdev,0)==-ETIMEDOUT);assert(readl(memory+PIPEHANDLER_AON_GEN)==aon&&!usb2_power_off_calls&&p.dwc3_running);
unlock_ret=0;assert(!atcphy_dwc3_reset_assert(&p.rcdev,0));assert(!p.t8103_dummy_pending&&!p.t8103_usb3_retry&&usb2_power_off_calls==1&&!p.dwc3_running);
}
static void power_off_preserves_dp_and_finishes_pending(void) {
setup_legacy_usb3();unlock_ret=-ETIMEDOUT;assert(atcphy_park_live_t8103_pipehandler(&p)==-ETIMEDOUT);p.mode=APPLE_ATCPHY_MODE_USB3_DP;
Expand Down Expand Up @@ -989,6 +989,12 @@ def harness(source):
'clear32(atcphy->regs.pipehandler + PIPEHANDLER_AON_GEN, '
'PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN);\n'
'\tif (atcphy->t8103_dummy_pending) {'),
'reset-release-not-running': ('atcphy_dwc3_reset_deassert',
'atcphy->dwc3_running = true;',
'(void)0;'),
'reset-assert-keeps-running': ('void _atcphy_dwc3_reset_assert',
'atcphy->dwc3_running = false;',
'(void)0;'),
'reset-no-retry-clear': ('atcphy_dwc3_reset_assert',
'atcphy->t8103_usb3_retry = false;',
'(void)0;'),
Expand Down
Loading
Loading