Skip to content

phy: apple: t6040: keep a running coprocessor's domains on through s2idle - #2855

Merged
RyanTheTide merged 4 commits into
integration/aurorafrom
j616s-usb3-s2idle/s2idle-hold
Nov 23, 2026
Merged

RyanTheTide merged 4 commits into
integration/aurorafrom
j616s-usb3-s2idle/s2idle-hold

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On the 16" MacBook Pro M4 Pro (T6040), a Type-C port with a running PHY coprocessor loses its link for good after a suspend-to-idle. This happens in USB4/Thunderbolt (CIO) mode and in plain USB3 host mode. On the first renegotiation after the sleep the coprocessor no longer answers, and every later start fails until a reboot:

phy-apple-t6040-usb2 382a90000.phy: RTKit: AP idle not acknowledged: -62
phy-apple-t6040-usb2 382a90000.phy: USB3: completed hard recovery with 50 ms off dwell
phy-apple-t6040-usb2 382a90000.phy: RTKit: endpoint discovery failed: -62
phy-apple-t6040-usb2 38aa90000.phy: RTKit: AP idle not acknowledged: -62
phy-apple-t6040-usb2 38aa90000.phy: USB3 PHY bring-up failed (-11), USB2 only

Nothing stops a running PHY coprocessor for s2idle, but genpd powers off its domain (atcN_common_dp, and atcN_phymxwrap above it) in the noirq phase, under the running coprocessor. With PMGR debug on, both domains go to state 0 inside every sleep. #2664 deals with the same class of bug on T6050.

Change

  • The PHY decides in its own .suspend and .suspend_noirq whether its coprocessor is running, on every suspend pass (dark wakes included). If it is, it calls device_set_awake_path(), as apple-dart and btcentauri already do.
  • The four atcN_common_dp domains get apple,sleep-wakeup-path, so pmgr sets GENPD_FLAG_ACTIVE_WAKEUP on them even when the boot loader left them off at pmgr probe. Today the flag is only set for domains that were on at probe. genpd then keeps the domain, and phymxwrap above it, on for a device on the awake path. Domains whose PHY is not on the awake path still power off.
  • The binding text for apple,sleep-wakeup-path now also covers a consumer that keeps running through the whole sleep.
  • The first commit's PM notifier and its dev_pm_syscore_device() are removed by the last commit. The PM core's normal prepare/late/noirq handling, including disabling runtime PM, now applies to the device.

Sleeps that the platform firmware takes part in behave as before (pm_suspend_no_platform()). Nothing is gated on chip, board or firmware, there are no parameters, and the default command line is enough.

Testing

J616s, default fleet command line. Before = fleet kernel 7.3.0-aurora-fleet81. After = the same tree plus this PR's four commits (the driver, DT, pmgr and genpd sources are identical to this branch). Each row: 15 s RTC s2idle cycles plus one pm_test=devices cycle, then a port watch. CIO port = phy@382a90000 in atc0_common_dp, USB4 link to a Mac mini M2. Plain-USB3 port = phy@38aa90000 in atc1_common_dp, linked to a MacBook Air M4 in USB device role (the J616s is USB host, and its USB3 coprocessor runs).

PHY domain off inside a sleep while its coprocessor runs after the sleeps
before (3 sleeps) yes, both ports, every sleep both ports dead: CIO 8/8 hard recoveries fail (-62), USB3 falls back to USB2 on every attach (9x), 4 Type-C config failures
after, CIO port running, production DTB (3) never renegotiation recovered, 0 x -62, 0 config failures
after, CIO + USB3 running, production DTB (3) never (atc0 and atc1 0 off transitions) 0 x -62, 0 config failures; USB3 re-tuned cleanly on each later re-attach
after, CIO + USB3 running, DT property (aura-try DTB-only trial, 3) never (atc0 and atc1 0 off transitions) 0 x -62, 0 config failures
  • In every "after" row the idle ports' domains (atc2, atc3: no consumer in this DT) still power off and on inside each sleep, so the hold has no idle cost.
  • With and without the DT property the USB3 port is held. On these warm boots the boot loader leaves atc0..2_common_dp on at pmgr probe (a boot with pmgr dynamic debug shows their first transition is to off), so pmgr's existing "active at probe" branch already sets the flag. The property covers the case where the loader leaves a consumer's domain off. I could not produce that case on this J616s from software.
  • checkpatch --strict: clean on all four commits. No SELinux denials, no failed units on the test boots.
  • The only kernel errors on these boots are dwc3-apple ...: genpd_prepare returns -11, which the fleet kernel logs the same way.

Still to run

  • A consumer domain that is off at pmgr probe and is started later (the DT property's own case). On the J616s only atc3 is off at probe, and nothing in its DT uses it. Producing the case on a PHY domain needs a boot where the loader leaves it off, for example a cold boot. That needs a power cycle, which I can't do from software here.
  • A coprocessor started between .suspend and noirq, on hardware. The J616s Type-C controllers refuse the DISC command, and a PD data-role swap is refused too, so I can't trigger a late attach from software. The REVIEW-WAVE-6 KVM model covers the .suspend_noirq decision (S7/S8).
  • An s2idle drain number. The J616s is permanently on AC, and its battery gauge charged during the sleep even with force-discharge set, so I have no valid measurement. This needs a T6040/T6041 Mac on battery only, or an inline meter.
  • DP (pin D) across s2idle. Nothing in the lab has a DP partner: all of the J616s's DP and HDMI connectors are disconnected. This needs a T604x Mac with a USB-C/DP monitor or dock. With this change a running DP coprocessor is held the same way.
  • Other T6040/T6041 machines (J614s, J773s, J575c). None are in the lab. Their DTBs get the property through the shared t604x-pmgr.dtsi.
Details for reviewers

Commits:

  • a9454a5 (original): PM notifier + dev_pm_syscore_device(). The last commit removes it.
  • 73dea0d dt-bindings: apple,sleep-wakeup-path description also covers a consumer that runs through the sleep. No change in meaning for the existing user (T8132 sio_cpu).
  • c1c5e67 arm64: dts: apple,sleep-wakeup-path on ps_atc0..3_common_dp in t604x-pmgr.dtsi.
  • e1b79fc phy: .suspend and .suspend_noirq decide. .suspend takes the PHY lock. .suspend_noirq uses mutex_trylock(): a caller holding the lock may be waiting for a coprocessor reply that cannot arrive in noirq, so if the lock is busy the device goes on the awake path for that sleep instead of blocking. genpd's genpd_finish_suspend() runs the driver's noirq callback before it checks device_awake_path(), so a coprocessor started after .suspend is still caught. power.wakeup_path is cleared in device_prepare() on every pass, so nothing needs undoing at resume.

Review items at a9454a5:

  1. Sampling once at PM_SUSPEND_PREPARE missed late and dark-wake attaches: fixed. The decision now runs on every suspend_devices_and_enter() pass, and again in noirq.
  2. dev_pm_syscore_device() skipped the PM core's handling: removed. The REVIEW-WAVE-5 probe had already shown that noirq pm_runtime_suspend returns -EACCES in this tree, so this was not an active hole, but the flag is gone either way.
  3. Plain USB3 and DP: plain USB3 has the same bug (before row above), so holding is required in USB3 mode too. DP uses the same coprocessor and is held the same way; no row yet.
  4. Rows: see Testing and Still to run.

Per-row domain transitions inside sleeps (PS atcN_* lines between PM: suspend entry and PM: suspend exit; one off + one on per sleep that reaches noirq; pm_test=devices stops before noirq):

  • before: atc0..3 common_dp and phymxwrap, 6 lines each (3 sleeps).
  • after, every row: atc0 0, atc1 0 while its USB3 coprocessor runs; idle atc2/atc3 4 each (2 RTC sleeps).
  • Domain map from the DT and the genpd summary: phy@382a90000 -> atc0_common_dp (phandle 0xb6), phy@38aa90000 -> atc1_common_dp (0xc0), dwc3 38a280000.usb -> atc1_usb. An earlier revision of this text read the atc2 lines as the USB3 port and said the DT commit was needed for it. That was wrong: atc2 is an idle port.

The DT trial was an aura-try boot of the production Stage 2 with only its two DTBs changed: four apple,sleep-wakeup-path; lines each, and a dts diff shows nothing else. The trial was never committed; afterwards the production Stage 2 hash was verified unchanged.

Assisted-by: LLM (Claude:Opus 5.5)

…idle

In suspend-to-idle the Type-C link stays up: the USB4/Thunderbolt
router holds its own power domains out of the system transition while
a cable is set up, and nothing stops a running PHY coprocessor. genpd
still powered off this PHY's domain (common_dp, and phymxwrap above it)
in its noirq phase, under the running coprocessor. The link died
shortly after resume ("SBX disconnected"), and the detach that followed
found a coprocessor that no longer answered:

  RTKit: AP idle not acknowledged: -62
  USB3: PHY coprocessor idle failed: -62
  ...
  RTKit: endpoint discovery failed: -62
  sn201202x 1-0c: failed to configure Type-C connection: -62

From then on every start failed the same way until a reboot, hard
recoveries included, although a hard recovery brings back a port that
never slept.

Hold the PHY device out of the whole suspend-to-idle transition, from
before its prepare to after its complete, while its coprocessor runs,
so genpd leaves its domains on. Sleeps the platform firmware takes part
in are left as they were (pm_suspend_no_platform() is false there).

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at a9454a5.

The diagnosis looks right and the hold/release pairing is balanced, but the check happens once at the wrong time, and the cost of holding the domains on hasn't been measured.

  1. drivers/phy/apple/t6040-usb2.c:2150-2160: "coprocessor running" is sampled only at PM_SUSPEND_PREPARE. The sn201202x event IRQ stays live through system sleep, so a coprocessor can start after that point and is not held. Two cases: an attach between PREPARE and dpm_suspend, and an attach during a dark wake (the dark-wake loop in enter_state() re-runs suspend_devices_and_enter() without the notifier). An example of the second is a dock plugged into a sleeping machine. The next noirq then powers common_dp off under a running coprocessor, which is the original bug. The body says hard recovery handles this. It does not: hard recovery was shown to work only when the coprocessor was not running at power-off, and this is the opposite case. Please decide at suspend time instead. The tree already has a mechanism for this: call device_set_awake_path() from a .suspend callback while the coprocessor runs (as apple-dart.c and btcentauri.c do), and mark atc*_common_dp/phymxwrap apple,sleep-wakeup-path so pmgr sets GENPD_FLAG_ACTIVE_WAKEUP. That covers dark wakes and late attaches, and the notifier, the syscore flag and the mutex in the PM path all go away.
  2. :2155: dev_pm_syscore_device() also leaves the device's runtime PM enabled through late/noirq. common_dp has no other consumer, so its prepared_count stays 0. If the cable is pulled while the machine sleeps, the detach's pm_runtime_put() can power common_dp off at runtime in the middle of the noirq/s2idle phase. Hard recovery (pm_runtime_force_suspend) can also run there. Neither path is exercised (the body lists cable-pull-in-sleep as not run). The approach in item 1 avoids this.
  3. Power: with a USB3/USB4 partner attached, common_dp and phymxwrap now stay on and the coprocessor keeps running for the whole s2idle. There is no drain row. Please add a before/after s2idle power row on J616s with the USB4 partner attached, using a long sleep, kept separate from the 15 s functional cycles.
  4. Plain USB3 mode (no router holding its own domains) now takes the same hold. Should it, or should that mode instead power the coprocessor down cleanly (t6040_usb3_firmware_power_down) before sleep? A row is owed either way.
  5. Rows owed at this head: J616s s2idle short sleeps (USB4 partner, plus a USB3-only device), attach during sleep or a dark wake, cable pull during sleep, and a long-sleep drain row. The same driver binds on J575c (t6041-j575c-atc3.dtsi) and J773s (t6040-j773s-usb-front.dtsi), so each needs one s2idle cycle with a partner attached.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial pre-PR checklist (§1–§6) review at a9454a5477b4c3be07c6b962e5f61fbfcb9731a7 (host-tests: SUCCESS).

Verdict: REQUEST CHANGES — the root-cause diagnosis is solid and matches the T6050 #2664 class of bug, and the commit/PR hygiene (§4, §6) is clean, but the PM_SUSPEND_PREPARE + dev_pm_syscore_device() mechanism has real suspend/resume state-machine gaps and key §1/§5 hardware evidence is still owed.

Blocking findings

  1. Late-attach and dark-wake sampling gap (drivers/phy/apple/t6040-usb2.c:2145-2171):

    • t6040_pm_notify() samples tphy->rtk && apple_rtkit_is_running(tphy->rtk) only once at PM_SUSPEND_PREPARE (before task freezing and before dpm_prepare()/dpm_suspend()).
    • Two live paths can start the PHY coprocessor after PM_SUSPEND_PREPARE while s2idle_held remains false:
      1. A Type-C / USB4 / USB3 attach between PM_SUSPEND_PREPARE and dpm_suspend_noirq().
      2. A dock/partner attach during a dark wake: enter_state() (kernel/power/suspend.c) loops while (!error && pm_dark_wake()) suspend_devices_and_enter(state); without re-invoking PM_SUSPEND_PREPARE, so the notifier never runs again before the next dpm_suspend_noirq().
    • In both cases genpd cuts atc*_common_dp and atc*_phymxwrap under a running coprocessor, reproducing the original unrecoverable AP idle -62 wedge. The reviewer note claiming hard recovery covers post-PREPARE starts contradicts the PR's own data (hard recovery fails 16/16 when power is cut under a running coprocessor; it only recovers when the coprocessor was stopped beforehand).
  2. Runtime PM stays active through suspend_late/noirq under dev_pm_syscore_device() (drivers/phy/apple/t6040-usb2.c:2155):

    • Unlike #2664 (which holds domains in suspend_late and freezes mode/gate transitions under dp->lock), dev_pm_syscore_device(tphy->dev, true) skips dpm_prepare() and dpm_suspend_late() for tphy->dev, so the PM core never takes its prepare runtime-PM reference or calls pm_runtime_disable().
    • Because sn201202x keeps its event IRQ live (IRQF_NO_SUSPEND), a cable pull or recovery path during sleep can invoke pm_runtime_put(tphy->dev) (via t6040_cio_down_locked() / t6040_usb2_reset_assert()) or pm_runtime_force_suspend(tphy->dev) (via t6040_usb3_hard_recover()) mid-sleep, dropping common_dp/phymxwrap in the noirq window or leaving s2idle_held desynchronized from actual coprocessor state.
    • Recommended fix: align with Ryan's comment and the existing in-tree mechanism — evaluate tphy->rtk && apple_rtkit_is_running(tphy->rtk) in a .suspend callback (when pm_suspend_no_platform() is true) and call device_set_awake_path(dev) (as drivers/iommu/apple-dart.c:7196 does), paired with apple,sleep-wakeup-path on ps_atc*_common_dp / ps_atc*_phymxwrap so pmgr-pwrstate.c:1028-1035 sets GENPD_FLAG_ACTIVE_WAKEUP. That evaluates live on every suspend entry (including late attaches and dark wakes), keeps PM core runtime-PM disablement intact, and removes the PM notifier and dev_pm_syscore_device() flag.
  3. Plain USB3 and DP pin D behavior across s2idle:

    • apple_rtkit_is_running(tphy->rtk) is true not only for CIO (USB4/TB), but also for plain USB3 host mode (t6040_usb3_up_locked()) and 2-lane DP pin D (t6040_dp_power_on()). Plain USB3 has no USB4 router keeping sibling domains awake; it needs an explicit decision (hold vs. clean t6040_usb3_firmware_power_down() before sleep) and a test row either way.
  4. Missing §1 / §5 hardware evidence and power-drain measurement:

    • No before/after long-sleep s2idle power-drain row on J616s with the USB4 partner attached to quantify keeping common_dp + phymxwrap + coprocessor powered through s2idle.
    • Missing functional rows for: plain USB3 across s2idle, cable pull during sleep, and attach during sleep / dark wake.
    • apple,t6040-atcphy also binds on T6041 (J575c, t6041-j575c-atc3.dtsi) and J773s (t6040-j773s-usb-front.dtsi), plus J614s; s2idle coverage on the other T6040/T6041 boards is still owed per §1 / §5.

§1–§6 Checklist Summary

  • §1 No model gating: Pass on code (no chip/board/firmware gates; pm_suspend_no_platform() leaves firmware sleep untouched); broader T6040/T6041 hardware verification owed.
  • §2 Production-ready: Blocked — PM_SUSPEND_PREPARE sampling gap and dev_pm_syscore_device() runtime-PM bypass need rework.
  • §3 Safety: Blocked — runtime-PM / pm_runtime_force_suspend() hazard during noirq on cable-pull-in-sleep.
  • §4 Clean-room & attribution: Pass (human author/committer + human Signed-off-by: only, zero AI trailers in commits, Assisted-by: in PR body).
  • §5 Evidence: Partial — strong J616s USB4 before/after + pm_test=devices + fault-injection + 0 errors/denials/failed units, but missing drain row, plain-USB3, cable-pull/attach-in-sleep, and J575c/J773s rows.
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Executed review at a9454a5: a model of t6040_pm_notify() run through real s2idle cycles of this tree's PM core (suspend.c with its dark-wake loop, drivers/base/power/main.c, genpd), next to the awake-path alternative from the review above. Result: one change still needed (item 1 of that review holds, with a probe). Item 2 does not hold for this tree.

1. A coprocessor started during a dark wake is not held, and its domain goes off under it (confirmed). enter_state() re-runs suspend_devices_and_enter() after a dark wake without the PM notifiers (suspend.c:629-633). apple_sleep_port_wake() makes a cable or charger wake with the lid closed a dark wake (via macsmc.c:1120), so "dock plugged into a closed, sleeping laptop" takes exactly this path. In the probe, with the coprocessor stopped at PREPARE and started in the dark wake, the PR's domain is powered off during the re-sleep with the coprocessor running (S5 below). The same sequence with device_set_awake_path() from .suspend and GENPD_FLAG_ACTIVE_WAKEUP on the domain keeps it on, because that is decided again on every pass. A coprocessor already running at PREPARE stays held through the dark wake (S6), so the body is right about that case. The sentence about hard recovery covering a later start is still wrong for the dark-wake case, because there the coprocessor is running when the power goes.

2. Runtime PM during the transition is not a hole in this tree. device_prepare() takes pm_runtime_get_noresume() before its syscore early return (main.c:2211 vs 2220), and device_suspend_late() calls pm_runtime_disable() before its syscore skip. In the probe, a main-phase pm_runtime_put_sync() leaves the device active, and pm_runtime_suspend() from a noirq callback returns -EACCES (S2). The domain goes off only after dpm_complete(), once the transition is over. So a cable pull while asleep can't power common_dp off in noirq through runtime PM. The cable-pull row is still worth having on hardware, but this is not the reason.

3. Checked and fine: pm_suspend_no_platform() is valid at PREPARE here, because enter_state() sets the flag ahead of the notifiers (suspend.c:612-614). common_dp is a subdomain of phymxwrap (t604x-pmgr.dtsi:434), and the mailbox is the only device in phymxwrap, so holding common_dp keeps phymxwrap on through sd_count, as the body says. PREPARE/POST pairing is balanced on a failed prepare.

Suggested direction, as in the review above: decide in a .suspend callback (device_set_awake_path() while apple_rtkit_is_running()), and mark atc*_common_dp with apple,sleep-wakeup-path. pmgr then sets GENPD_FLAG_ACTIVE_WAKEUP (pmgr-pwrstate.c:1028-1034). That drops the notifier, the syscore toggle and the PM-path mutex, and it covers dark wakes. Hardware rows owed are as listed above.

Probe results and method

PR tree + a local-only probe (not part of any branch), x86_64 tiny config (PM core is arch-independent; ACPI off so s2idle has no platform ops), KVM. Two fake common_dp domains: pd_a held by a copy of t6040_pm_notify(), pd_b with the awake path. A helper device arms an hrtimer (pm_system_wakeup() after 400 ms) in suspend_noirq, sets pm_set_dark_wake() in resume_noirq as macsmc does, and does the attach (coprocessor started) or detach (runtime put) in the scenario's phase.

S1 running at PREPARE, plain s2idle         | PR pd_a: off=0                      | awake pd_b: off=0
S2 detach during the transition (held)      | main put_sync=0, still active; noirq pm_runtime_suspend=-13; pd_a off only after dpm_complete, coprocessor stopped
S5 stopped at PREPARE, attach in dark wake  | PR pd_a: OFF in cycle 2, coprocessor RUNNING (off_while_running=1) | awake pd_b: off_while_running=0
S6 running at PREPARE, dark wake, re-sleep  | PR pd_a: off=0 (both cycles)        | awake pd_b: off=0

Side observation, not about this PR and seen in the model only: if the wake that ends the first pass leaves pm_abort_suspend pending (a plain pm_system_wakeup()), the dark-wake re-entry aborts at once with -EBUSY ("Some devices failed to suspend, or early wake event detected") and ends as a full wake (S3/S4). Nothing clears it between enter_state()'s dark-wake passes. S5/S6 cancel the timer's wake in resume_noirq to model a re-entry that proceeds. Whether the hardware SMC wake leaves it pending was not checked here.

Probe source (rw5_probe.c, local only)
// SPDX-License-Identifier: GPL-2.0
/*
 * REVIEW-WAVE-5 local probe (never committed): models linux#2855 a9454a54
 * t6040_pm_notify() (dev_pm_syscore_device hold sampled at PM_SUSPEND_PREPARE)
 * against the awake-path alternative (device_set_awake_path from .suspend +
 * GENPD_FLAG_ACTIVE_WAKEUP), on fake genpd domains, through real s2idle cycles
 * incl. enter_state()'s dark-wake loop.
 *   phy0 in pd_a: PR notifier logic (copied).   phy1 in pd_b: awake-path.
 *   helper (no domain): sets dark wake in resume_noirq (as macsmc does), does the
 *   scenario's "attach"/"detach", arms the wake hrtimer in suspend_noirq.
 */
#include <linux/hrtimer.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/pm_domain.h>
#include <linux/pm_runtime.h>
#include <linux/pm_wakeup.h>
#include <linux/suspend.h>

static struct generic_pm_domain pd[2];
static struct platform_device *phy[2], *helper;
static bool running[2];			/* "coprocessor running" */
static int offs[2], off_running[2], ons[2];
static bool held;			/* PR's s2idle_held */
static int scenario, cycle, dark_left;
static bool cancel_abort;	/* model a re-entry the pending wake does not abort */
static int r_main_put, r_noirq_suspend, st_main, st_noirq;
static struct hrtimer wake_timer;

static int pd_off(struct generic_pm_domain *g)
{
	int i = g == &pd[1];

	offs[i]++;
	if (running[i])
		off_running[i]++;
	pr_info("rw5: %s OFF (cycle %d, coprocessor %s)%s\n", g->name, cycle,
		running[i] ? "RUNNING" : "stopped", running[i] ? " <== BUG" : "");
	return 0;
}

static int pd_on(struct generic_pm_domain *g)
{
	ons[g == &pd[1]]++;
	pr_info("rw5: %s ON (cycle %d)\n", g->name, cycle);
	return 0;
}

/* --- PR logic, copied from t6040_pm_notify() (mutex dropped: single-threaded probe) --- */
static int pr_notify(struct notifier_block *nb, unsigned long action, void *data)
{
	switch (action) {
	case PM_SUSPEND_PREPARE:
		if (!pm_suspend_no_platform())
			break;
		if (running[0]) {
			dev_pm_syscore_device(&phy[0]->dev, true);
			held = true;
			pr_info("rw5: PREPARE: phy0 held (syscore)\n");
		} else {
			pr_info("rw5: PREPARE: phy0 not running, not held\n");
		}
		break;
	case PM_POST_SUSPEND:
		if (held) {
			dev_pm_syscore_device(&phy[0]->dev, false);
			held = false;
			pr_info("rw5: POST_SUSPEND: phy0 released\n");
		}
		break;
	}
	return NOTIFY_DONE;
}
static struct notifier_block pr_nb = { .notifier_call = pr_notify };

static enum hrtimer_restart wake_fn(struct hrtimer *t)
{
	pm_system_wakeup();
	return HRTIMER_NORESTART;
}

static int phy_suspend(struct device *dev)
{
	if (dev == &phy[1]->dev && running[1])
		device_set_awake_path(dev);
	return 0;
}

static int helper_suspend(struct device *dev)
{
	cycle++;
	if (scenario == 2) {	/* detach while phy0 is held, main suspend phase */
		running[0] = false;
		r_main_put = pm_runtime_put_sync(&phy[0]->dev);
		st_main = pm_runtime_status_suspended(&phy[0]->dev);
	}
	return 0;
}

static int helper_suspend_noirq(struct device *dev)
{
	if (scenario == 2) {
		r_noirq_suspend = pm_runtime_suspend(&phy[0]->dev);
		st_noirq = pm_runtime_status_suspended(&phy[0]->dev);
	}
	hrtimer_start(&wake_timer, ms_to_ktime(400), HRTIMER_MODE_REL);
	return 0;
}

static int helper_resume_noirq(struct device *dev)
{
	if (dark_left > 0) {
		dark_left--;
		pm_set_dark_wake();
		if (cancel_abort)
			pm_system_cancel_wakeup();
		pr_info("rw5: resume_noirq: dark wake set (cycle %d)%s\n", cycle,
			cancel_abort ? ", pending wake cancelled" : "");
	}
	return 0;
}

static int helper_resume(struct device *dev)
{
	if ((scenario == 3 || scenario == 5) && pm_dark_wake()) {	/* attach during the dark wake */
		running[0] = running[1] = true;
		pr_info("rw5: dark wake: attach, coprocessors started\n");
	}
	return 0;
}

static int rpm_cb(struct device *dev) { return 0; }

static const struct dev_pm_ops phy_pm = {
	.suspend = phy_suspend,
	RUNTIME_PM_OPS(rpm_cb, rpm_cb, NULL)
};
static const struct dev_pm_ops helper_pm = {
	.suspend = helper_suspend,
	.resume = helper_resume,
	.suspend_noirq = helper_suspend_noirq,
	.resume_noirq = helper_resume_noirq,
};

static struct platform_driver phy_drv = { .driver = { .name = "rw5phy", .pm = &phy_pm } };
static struct platform_driver helper_drv = { .driver = { .name = "rw5helper", .pm = &helper_pm } };

static void run(int sc, bool run0, bool run1, int dark, const char *what)
{
	int i, ret;

	scenario = sc;
	running[0] = run0;
	running[1] = run1;
	dark_left = dark;
	cycle = 0;
	for (i = 0; i < 2; i++)
		offs[i] = off_running[i] = ons[i] = 0;
	pr_info("rw5: === S%d: %s ===\n", sc, what);
	ret = pm_suspend(PM_SUSPEND_TO_IDLE);
	pr_info("rw5: S%d RESULT pm_suspend=%d cycles=%d | PR(pd_a): off=%d off_while_running=%d on=%d | awake-path(pd_b): off=%d off_while_running=%d on=%d\n",
		sc, ret, cycle, offs[0], off_running[0], ons[0], offs[1], off_running[1], ons[1]);
}

static void reset_rpm(void)
{
	int i;

	for (i = 0; i < 2; i++)
		if (pm_runtime_status_suspended(&phy[i]->dev) ||
		    atomic_read(&phy[i]->dev.power.usage_count) == 0)
			pm_runtime_get_sync(&phy[i]->dev);
}

static int __init rw5_probe_init(void)
{
	int i;

	hrtimer_setup(&wake_timer, wake_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	pd[0].name = "pd_a_common_dp_PR";
	pd[1].name = "pd_b_common_dp_awake";
	pd[1].flags = GENPD_FLAG_ACTIVE_WAKEUP;	/* what apple,sleep-wakeup-path sets */
	for (i = 0; i < 2; i++) {
		pd[i].power_off = pd_off;
		pd[i].power_on = pd_on;
		pm_genpd_init(&pd[i], NULL, false);
		phy[i] = platform_device_register_simple("rw5phy", i, NULL, 0);
		pm_genpd_add_device(&pd[i], &phy[i]->dev);
	}
	helper = platform_device_register_simple("rw5helper", -1, NULL, 0);
	platform_driver_register(&phy_drv);
	platform_driver_register(&helper_drv);
	for (i = 0; i < 2; i++) {
		pm_runtime_enable(&phy[i]->dev);
		pm_runtime_get_sync(&phy[i]->dev);
	}
	register_pm_notifier(&pr_nb);

	run(1, true, true, 0, "coprocessor running at PREPARE, plain s2idle");
	reset_rpm();
	run(2, true, true, 0, "running at PREPARE, detach (runtime put) during the transition");
	pr_info("rw5: S2 detail: main-phase pm_runtime_put_sync=%d phy0_suspended=%d | noirq pm_runtime_suspend=%d phy0_suspended=%d\n",
		r_main_put, st_main, r_noirq_suspend, st_noirq);
	pr_info("rw5: S2 after POST: phy0 rpm suspended=%d usage=%d\n",
		pm_runtime_status_suspended(&phy[0]->dev), atomic_read(&phy[0]->dev.power.usage_count));
	reset_rpm();
	run(3, false, false, 1, "not running at PREPARE, attach during a dark wake, sleep again");
	reset_rpm();
	run(4, true, true, 1, "running at PREPARE, dark wake, sleep again");
	cancel_abort = true;
	reset_rpm();
	run(5, false, false, 1, "as S3, re-entry not aborted: attach during a dark wake, sleep again");
	reset_rpm();
	run(6, true, true, 1, "as S4, re-entry not aborted: running at PREPARE, dark wake, sleep again");
	pr_info("rw5: DONE\n");
	return 0;
}
late_initcall_sync(rw5_probe_init);

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

A follow-up on the direction suggested in the reviews above (.suspend + device_set_awake_path() + apple,sleep-wakeup-path), from the same KVM model of this tree's PM core, so the rework covers one more window.

Deciding the awake path in .suspend alone leaves a gap. Deciding it (again) in .suspend_noirq closes it.

  • S7: the coprocessor starts after every device's .suspend has run (modelled in a .suspend_late) but before noirq, while the attach IRQ is still live. With the awake path set only from .suspend, genpd powers the domain off under the running coprocessor (off_while_running=1). That is the same failure as the PREPARE notifier at a9454a5.
  • S8: the same sequence, with the PHY also calling device_set_awake_path() from its .suspend_noirq while the coprocessor runs. The domain stays on (off_while_running=0). In this tree genpd_finish_suspend() runs the driver's noirq callback before it checks device_awake_path() (drivers/pmdomain/core.c around 1588-1592), so a decision made in .suspend_noirq is the last one that counts.
  • S1-S6 come out as in the earlier executed pass, including the dark-wake attach case (S5) that a per-pass callback fixes.

Suggestion: make the decision in .suspend_noirq, or in both .suspend and .suspend_noirq, and put apple,sleep-wakeup-path on the common_dp domains of every T604x board that has this PHY, not only the J616s. This is a model. Whether the T6040 attach path can actually start the coprocessor between .suspend and noirq on hardware is not shown, so a row that attaches a cable during the sleep transition would settle it.

How it was run

PR tree a9454a5 plus the earlier pass's local probe (never committed), extended with S7/S8. x86_64 allnoconfig with SUSPEND/PM_SLEEP/PM_GENERIC_DOMAINS, booted with qemu-system-x86_64 -enable-kvm. pm_suspend(PM_SUSPEND_TO_IDLE) cycles run through the real suspend.c, drivers/base/power/main.c and genpd. pd_a models this PR's PREPARE hold. pd_b models GENPD_FLAG_ACTIVE_WAKEUP (what pmgr sets for apple,sleep-wakeup-path) plus device_set_awake_path().

S7 RESULT pm_suspend=0 cycles=1 | PR(pd_a): off=1 off_while_running=1 on=1 | awake-path(pd_b): off=1 off_while_running=1 on=1
S8 RESULT pm_suspend=0 cycles=1 | PR(pd_a): off=1 off_while_running=1 on=1 | awake-path(pd_b): off=0 off_while_running=0 on=0

…unning consumer

apple,sleep-wakeup-path was written for a consumer that the sleep's
final commit stops after the noirq phase. A consumer that keeps running
through the whole sleep while it is on the wakeup path needs the same
thing from the OS: keep the domain on through system sleep, also when it
was off when the power controller probed. Say so in the description.
No change in meaning for existing users.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
…-path

The Type-C PHY coprocessor under atcN_common_dp keeps running through
suspend-to-idle while a link is up. Mark the four common_dp domains
apple,sleep-wakeup-path so that genpd keeps one on while the PHY puts
itself on the awake path, also when the domain was off when the power
controller probed (no cable at boot). phymxwrap above it stays on
through its subdomain count. Nothing changes for a PHY that is not on
the awake path.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
… path

The PM notifier sampled "coprocessor running" once, at
PM_SUSPEND_PREPARE. A coprocessor started later was not held: an attach
between PREPARE and the device's suspend, or an attach during a dark
wake, where enter_state() runs suspend_devices_and_enter() again without
the notifiers (a cable or charger plugged into a closed, sleeping
machine). genpd then powered common_dp off under it in noirq, which is
the original bug. dev_pm_syscore_device() also kept the device out of
the PM core's prepare/late handling altogether.

Decide in the device's own .suspend instead, on every suspend pass, and
put the device on the awake path with device_set_awake_path() while its
coprocessor runs; common_dp carries apple,sleep-wakeup-path, so genpd
keeps it (and phymxwrap above it) on. Decide again in .suspend_noirq,
which genpd runs before its own awake-path check, for a coprocessor
started after .suspend. In noirq a caller holding the PHY lock may be
waiting for a coprocessor reply that cannot arrive; the outcome is then
unknown, so the domains stay on for that sleep rather than block.

The notifier, the syscore flag and the held state go away. Sleeps the
platform firmware takes part in are left as they were
(pm_suspend_no_platform() is false there).

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Reworked as suggested, in three new commits on top (no force push), head e1b79fc:

  • The PM notifier and dev_pm_syscore_device() are gone. The PHY now decides in its own .suspend and again in .suspend_noirq, on every suspend pass (dark wakes included), and calls device_set_awake_path() while its coprocessor runs.
  • ps_atc0..3_common_dp get apple,sleep-wakeup-path, and the binding text now covers a consumer that runs through the whole sleep.
  • Plain USB3: the fleet kernel wedges the USB3 port exactly like the CIO port (AP idle -62, then "USB3 PHY bring-up failed, USB2 only" on every attach), so holding in USB3 mode is required too.
  • J616s rows are in the updated description. The DT property turned out to be necessary: without it, a domain that was off when pmgr probed (USB3 port, no partner at boot) was still cut while the PHY was on the awake path. With the property, idle ports' domains still power off.

Still owed and listed in the body: a with-property sleep while the USB3 coprocessor runs, a hardware late attach (the J616s controllers refuse DISC), a drain number (the J616s is on AC and its gauge charges in s2idle), and other T604x boards.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at e1b79fc.

Three commits were added on top of a9454a5: the binding text (73dea0d), apple,sleep-wakeup-path on ps_atc0..3_common_dp (c1c5e67), and the driver rework (e1b79fc). The rework removes the PM notifier and dev_pm_syscore_device(), and decides in .suspend and again in .suspend_noirq through device_set_awake_path().

Our points:

  1. The decision was sampled once at PREPARE, so dark-wake and late attaches were missed. Fixed. The decision now runs on every suspend_devices_and_enter() pass, and again in noirq, which genpd consults after the driver callback (S7/S8 of the model). The noirq path uses mutex_trylock() and holds the device when the lock is busy. That is the right fail-safe direction: it can cost one sleep's power, never the link. I checked that the PHY node has a single power domain (common_dp), so the flag lands on the device genpd actually checks. drvdata is set before anything in probe can fail. wakeup_path is reset each pass by the PM core, so nothing has to be undone at resume.
  2. The syscore runtime-PM concern is moot: the flag is gone, and the model showed it was not a hole in any case.
  3. Drain row: still owed. The J616s is on AC and its gauge charges in s2idle, so this needs a T6040/T6041 on battery only, or an inline meter.
  4. Plain USB3: answered. The before row shows the same wedge in USB3 mode, so holding it there is required. The DT property was shown to be necessary (without it, a domain off at pmgr probe is still cut).
  5. Rows: the J616s CIO port and the with-property trial are done (domain never off while running, idle ports still off, 0 x -62). Still open: a with-property sleep while the plain-USB3 coprocessor runs, an attach or cable pull during sleep on hardware, DP across s2idle, and J575c/J773s/J614s (none in the lab).

No new issues in the code. Nit: the series still adds the notifier in a9454a5 and removes it in e1b79fc. Please squash it into one commit before merge (or squash-merge), so the history does not carry a mechanism the tree never ships. There are no gates or parameters, the domain choice comes from DT, and there are no agent trailers.

Verdict: NITS. The code is ready.

Before merge (RyanTheTide/lab on J616s):

  • A with-property s2idle while the USB3 coprocessor runs.
  • An attach and a cable pull while asleep.

Wanted, but not blocking:

  • The battery drain number (needs a T604x on battery).
  • One s2idle cycle with a partner on J575c or J773s from an outside owner.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial pre-PR checklist (§1–§6) re-review at e1b79fc07d8e86e9db631754c573a3b894e49238 (CI: host-tests and dt-checks green; MERGEABLE / CLEAN), following up on my prior CHANGES_REQUESTED review at a9454a5477.

Verdict: REQUEST CHANGES — all code and architecture blockers from a9454a5477 are RESOLVED, and the code is ready to ship. The sole remaining blocker for merge is the §5 J616s hardware verification row for the exact path added by commit c1c5e672 (apple,sleep-wakeup-path keeping a port that was off at pmgr probe — plain USB3 host mode — alive through s2idle).

Status of prior a9454a5477 blockers

  1. Late-attach & dark-wake PM_SUSPEND_PREPARE sampling gap — RESOLVED:
    • e1b79fc0 removes the PM notifier and dev_pm_syscore_device().
    • t6040_usb2_suspend() and t6040_usb2_suspend_noirq() call t6040_keep_running_cpu_awake(), re-evaluating tphy->rtk && apple_rtkit_is_running(tphy->rtk) under tphy->lock on every suspend pass (including dark wakes) and again in noirq (failing safe to device_set_awake_path() if mutex_trylock() is contended so noirq never blocks on an in-flight coprocessor reply).
    • Paired with apple,sleep-wakeup-path on ps_atc0..3_common_dp (c1c5e672) and updated dt-bindings (73dea0df), pmgr-pwrstate.c:1028-1035 sets GENPD_FLAG_ACTIVE_WAKEUP even when the domain was off at probe. Verified against T604x DTS files: active atcphy0..2 and J575c atcphy3 (via t604x-extdisp.dtsi) all sit in ps_atcN_common_dp as subdomains of ps_atcN_phymxwrap (t6040-j773s-usb-front.dtsi's atcphy3 is currently status = "disabled").
  2. dev_pm_syscore_device() runtime-PM bypass — RESOLVED:
    • Flag removed; standard PM core prepare/late/noirq runtime-PM handling applies.
  3. Plain USB3 host mode decision — ADDRESSED IN CODE, HARDWARE ROW STILL OWED:
    • The J616s before-row now confirms plain USB3 suffers the exact same AP idle -62 / permanent USB2 fallback wedge across s2idle, and the without-DT-property row confirms why c1c5e672 is needed when a port's domain was off at pmgr probe.

Remaining blocking finding

  1. Missing §5 hardware row for the with-DT-property plain-USB3 path (Still to run):
    • In the Testing table, the with-DT-property trial exercised CIO (atc0), while the USB3 partner did not present a USB device during that slot — leaving the exact scenario commit c1c5e672 fixes (a domain that was off at pmgr probe staying powered across s2idle with apple,sleep-wakeup-path while the plain-USB3 coprocessor runs) unverified on hardware.
    • Before merge, run on J616s with the DT property: (a) s2idle cycle while the plain-USB3 coprocessor is running (confirming common_dp/phymxwrap stay on, 0x -62, USB3 stays alive post-resume, and idle ports still power off), and (b) cable-pull / attach while asleep if reproducible on J616s, with 0 new kernel errors / 0 SELinux denials / 0 failed units.

Non-blocking notes

  • Squash on merge (nit): Commit 1 (a9454a54) adds the PM notifier + dev_pm_syscore_device() and Commit 4 (e1b79fc0) removes them; please squash-merge (or squash before merge) so integration/aurora does not carry dead intermediate commits.
  • Deferred follow-up rows (non-blocking given documented lab constraints): battery-only s2idle power-drain measurement (J616s is AC-only and charges in s2idle), DP pin D (no DP partner in lab), and J575c/J773s/J614s coverage (boards not in lab; inherit via t604x-pmgr.dtsi).

§1–§6 Checklist Summary

  • §1 No model gating: Pass on code (no chip/board/firmware gates; DT-driven via apple,sleep-wakeup-path; pm_suspend_no_platform() preserves firmware sleep).
  • §2 Production-ready: Pass on code (notifier/syscore rework is clean; default cmdline; no params/hacks).
  • §3 Safety: Pass (mutex_trylock fail-safe in noirq, standard PM core runtime-PM disablement restored).
  • §4 Clean-room & attribution: Pass (all 4 commits carry human author/committer + human Signed-off-by: only, zero AI trailers in commits, Assisted-by: in PR body).
  • §5 Evidence: Blocked on one remaining J616s row (with-DT-property plain-USB3 s2idle + sleep cable-pull/attach).
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

New J616s rows at e1b79fc (no code change). I've updated the PR body; it also corrects an earlier claim.

  • With the DT property, USB3 coprocessor running (the owed row): 3 sleeps (2 x 15 s RTC + pm_test=devices). The USB3 PHY's domain and the CIO PHY's domain never power off inside a sleep. 0 x -62, 0 Type-C config failures. The idle ports still power off.
  • Same condition, production DTB without the property: also held, 0 x -62.
  • Correction: the plain-USB3 PHY phy@38aa90000 sits in atc1_common_dp (phandle 0xc0; the genpd summary agrees), not in atc2. The earlier "without the property the USB3 port is still cut" row was reading atc2, which is an idle port with no consumer. The USB3 port was held there too.
  • Why the property doesn't change these rows: a boot with pmgr dynamic debug shows that the boot loader leaves atc0..2_common_dp on at pmgr probe. That means pmgr's existing "active at probe" branch already sets GENPD_FLAG_ACTIVE_WAKEUP on these domains. The property only matters when a consumer's domain is off at probe. On the J616s only atc3 is, and it has no consumer. I can't produce that case from software here; it would need a cold boot that leaves a PHY domain off. So the DT commit is defensive against boot-loader state, and these rows neither prove nor disprove that it is needed.
  • Not possible in our lab: DP pin D (no DP partner: all DP and HDMI connectors on the J616s are disconnected; needs a T604x Mac with a USB-C/DP monitor or dock); late attach between .suspend and noirq (tipd DISC and PD data-role swaps are both refused; the KVM model S7/S8 covers it); drain (the J616s is always on AC); J614s/J773s/J575c (none in the lab).

To get the USB3 condition, the Air has to be the USB device. Data-role swaps were refused on both sides, but the side that attaches later takes the device role, so one plain reboot of the Air while the J616s was up was enough.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal adversarial pre-PR checklist (§1–§6) re-review at e1b79fc07d8e86e9db631754c573a3b894e49238 (CI: Host tests and Device tree checks both SUCCESS; MERGEABLE / CLEAN).

Verdict: APPROVE (ready for MERGETRAIN-LINUX4)

The sole remaining §5 blocker from my prior review at e1b79fc0 (the J616s with-DT-property plain-USB3 s2idle row) is now resolved, and Ryan's 03:24:26Z correction + updated PR body cleanly explain the underlying PMGR behavior:

  1. J616s with-DT-property plain-USB3 + CIO s2idle row (done):
    • 3 sleeps (2 x 15 s RTC + pm_test=devices) with both CIO (phy@382a90000 in atc0_common_dp) and plain USB3 (phy@38aa90000 in atc1_common_dp) coprocessors running: 0 off transitions on atc0/atc1, 0 x -62, 0 Type-C config failures, idle ports (atc2/atc3) still power off inside each sleep, 0 SELinux denials, 0 failed units, 0 new kernel errors.
  2. Why production DTB also held — and why commit c1c5e672 (apple,sleep-wakeup-path) is still genuinely needed:
    • Checked drivers/pmdomain/apple/pmgr-pwrstate.c:1026-1035: on J616s warm reboots, the boot loader leaves atc0..2_common_dp powered on (active == true) at pmgr probe, so the existing else if (active ...) branch already sets GENPD_FLAG_ACTIVE_WAKEUP. (The earlier misread came from reading idle atc2 instead of atc1.)
    • Commit c1c5e672 (apple,sleep-wakeup-path on ps_atc0..3_common_dp) + 73dea0df remain essential whenever the boot loader leaves a port's common_dp off at probe (e.g., cold boot with no cable attached, or atc3 on J575c): without it, pmgr-pwrstate.c:1028-1035 never sets GENPD_FLAG_ACTIVE_WAKEUP, so a coprocessor started post-probe would still have common_dp cut in noirq despite device_set_awake_path(). Idle domains still power off cleanly with the property set, so there is zero idle cost.
  3. T6040 / T6041 shared topology check:
    • Verified t6041.dtsi -> t604x.dtsi -> t604x-pmgr.dtsi shares the property across all T6040 (J614s, J616s, J773s) and T6041 (J614c, J616c, J575c) boards.
    • Checked ps_atc3_common_dp specifically: on J575c (t6041-j575c.dts), t604x-extdisp.dtsi:322-323 wires power-domains = <&ps_atc3_phymxwrap>; before t6041-j575c-atc3.dtsi enables it (and on J773s, atcphy3 stays status = "disabled"). Every active apple,t6040-atcphy on T6040/T6041 has the exact same common_dp -> phymxwrap subdomain relationship, with no T6041-specific risk.
    • Per policy, J616s (T6040) + the KVM PM-core model (S1–S8 covering dark-wake and .suspend-to-noirq attaches) stand in cleanly without parking on unowned T6040/T6041 boards, AC-only drain limits, or unavailable DP partners (all plainly documented under Still to run).

Non-blocking Note

  • Squash on merge (nit): Commit 1 (a9454a54) adds the PM notifier + dev_pm_syscore_device() and Commit 4 (e1b79fc0) removes them; squash-merge (or squash before merge) so integration/aurora does not carry the superseded mechanism.

§1–§6 Checklist Summary

  • §1 No model gating: Pass (DT-driven apple,sleep-wakeup-path, pm_suspend_no_platform() preserves firmware sleep).
  • §2 Production-ready: Pass (default cmdline, no hacks/params).
  • §3 Safety: Pass (mutex_trylock fail-safe in noirq, normal PM core runtime-PM handling).
  • §4 Clean-room & attribution: Pass (all 4 commits carry human identity + Signed-off-by:, 0 AI commit trailers, Assisted-by: in PR body).
  • §5 Evidence & CI: Pass (J616s before/after CIO + plain-USB3 + with/without DT property + idle-off + 0/0/0, KVM S1–S8 model, host-tests & dt-checks green).
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide
RyanTheTide merged commit a4b742a into integration/aurora Nov 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants