From 5e7dcf85729ed9f8718e5691b14b08a977689793 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Tue, 1 Dec 2026 12:40:13 -0500 Subject: [PATCH 1/6] drm/apple: dcp: fix non-retained firmware-27 s2r_ext link withdrawal dcp_s2r_ext_suspend() was the only caller of dcp_external_disconnect() not guarded by dcp_is_retained_external(). On non-retained firmware-27 external DCPs (such as T8112/T8103/T8132-v27 dcpext), routing link teardown through dcp_external_stop_locked() caused two failures during deep sleep with a USB-C display attached: 1. dptxport_set_hpd(false) ran with v27_hpd_low_ms == 0 (1000 ms timeout) instead of the 15 s firmware-27 withdrawal bound used by dcp-v27-sleep.inc (V27_EXT_WITHDRAW_MS), and gave up without calling dptxport_release_display() when HPD-low failed or timed out. 2. Any error in release_link permanently latched dcp->external_error for the rest of the boot even though non-retained s2r_ext pipes have no retained epoch and stop the firmware cold on sleep, turning a single timeout into an immediate -110 refusal on every subsequent suspend. Split the suspend link-withdrawal step into dcp_s2r_ext_disconnect(): - Keep dcp_external_disconnect(dcp, false) unchanged when dcp_is_retained_external(dcp) is true. - For non-retained s2r_ext pipes, disconnect audio, mark attached Type-C outputs typec_crtc_off before dropping HPD (restoring it on failure), withdraw split port 1 (when active) then port 0 with DCP_V27_EXT_WITHDRAW_MS (15000 ms, shared with V27_EXT_WITHDRAW_MS) for DCP_FIRMWARE_V_27_0, still call dcp_dptx_disconnect() if HPD-low fails (matching dcp_poweroff() and dcp_t6040_ext_link_down()), and return any release error cleanly without latching dcp->external_error or mutating dcp->external_stopped. Add host test tools/asahi/dcp-s2r-ext-disconnect-tests.py (registered in tools/asahi/run-host-tests.py) exercising the extracted production code against mocks and six negative-control mutations. Signed-off-by: Satya Benson --- drivers/gpu/drm/apple/dcp-v27-sleep.inc | 2 +- drivers/gpu/drm/apple/dcp.c | 81 +++- tools/asahi/dcp-s2r-ext-disconnect-tests.py | 392 ++++++++++++++++++++ tools/asahi/run-host-tests.py | 4 + 4 files changed, 477 insertions(+), 2 deletions(-) create mode 100755 tools/asahi/dcp-s2r-ext-disconnect-tests.py diff --git a/drivers/gpu/drm/apple/dcp-v27-sleep.inc b/drivers/gpu/drm/apple/dcp-v27-sleep.inc index d186cb021b387..4d3b49f5042c7 100644 --- a/drivers/gpu/drm/apple/dcp-v27-sleep.inc +++ b/drivers/gpu/drm/apple/dcp-v27-sleep.inc @@ -481,7 +481,7 @@ static int dcp_v27_step(struct dcp_v27_sleep *s, const char *what, int ret) * its own wait on the DP device, 10 s; the call's usual 1 s wait gives up before it and * refuses the sleep. */ -#define V27_EXT_WITHDRAW_MS 15000 +#define V27_EXT_WITHDRAW_MS DCP_V27_EXT_WITHDRAW_MS /* * An external display processor's display on a USB-C port goes before the sleep, as macOS diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 50bcff5415ed6..a7ee51cbfa6df 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10430,6 +10430,85 @@ static void dcp_s2r_hdmi_irq_enable(struct apple_dcp *dcp) } } +/* + * Firmware 27 external DCPs can hold an HPD-low answer for up to 10 s when a + * sink was not reported or its pipe is already off; share the 15 s bound with + * dcp-v27-sleep.inc's V27_EXT_WITHDRAW_MS. + */ +#define DCP_V27_EXT_WITHDRAW_MS 15000 + +static int dcp_s2r_ext_withdraw_port(struct apple_dcp *dcp, u32 port) +{ + struct apple_epic_service *service; + unsigned int hpd_ms = dcp->fw_compat == DCP_FIRMWARE_V_27_0 ? + DCP_V27_EXT_WITHDRAW_MS : MSEC_PER_SEC; + int hpd_ret, ret; + + scoped_guard(mutex, &dcp->hpd_mutex) { + if (!dptx_service_available(&dcp->dptxport[port]) || + !dcp->dptxport[port].connected) + return 0; + service = dcp->dptxport[port].service; + } + + hpd_ret = dptxport_set_hpd_timeout(service, false, hpd_ms); + if (hpd_ret) + dev_warn(dcp->dev, + "DPTX port %u HPD low failed before sleep (%u ms): %d\n", + port, hpd_ms, hpd_ret); + + ret = dcp_dptx_disconnect(dcp, port); + if (ret) + dev_warn(dcp->dev, + "DPTX port %u display release failed before sleep: %d\n", + port, ret); + return ret; +} + +/* + * Withdraw an attached display's DPTX link before stopping the firmware for + * deep sleep. Retained-external (H17P/ext26) pipes keep their own stop path + * (dcp_external_disconnect()). Non-retained firmware-27 external pipes do not + * hold a retained epoch: give HPD-low the firmware-27 withdrawal bound, still + * attempt display release if HPD-low fails (matching dcp_poweroff()), and never + * latch dcp->external_error on a suspend-time failure so later suspends can + * retry cleanly. + */ +static int dcp_s2r_ext_disconnect(struct apple_dcp *dcp) +{ + bool set_crtc_off = false; + bool prev_crtc_off = false; + int ret; + + if (dcp_is_retained_external(dcp)) + return dcp_external_disconnect(dcp, false); + + if (dcp->avep) + av_service_disconnect(dcp); + + if (dcp_is_typec_output(dcp) && READ_ONCE(dcp->typec_cable_connected)) { + prev_crtc_off = READ_ONCE(dcp->typec_crtc_off); + WRITE_ONCE(dcp->typec_crtc_off, true); + set_crtc_off = true; + } + + if (READ_ONCE(dcp->split.active)) { + ret = dcp_s2r_ext_withdraw_port(dcp, 1); + if (ret) + goto err_restore; + } + ret = dcp_s2r_ext_withdraw_port(dcp, 0); + if (ret) + goto err_restore; + + return 0; + +err_restore: + if (set_crtc_off) + WRITE_ONCE(dcp->typec_crtc_off, prev_crtc_off); + return ret; +} + static int dcp_s2r_ext_suspend(struct apple_dcp *dcp) { int ret; @@ -10470,7 +10549,7 @@ static int dcp_s2r_ext_suspend(struct apple_dcp *dcp) dcp_disable_typec_work(dcp, false); dcp_s2r_hdmi_irq_disable(dcp); - ret = dcp_external_disconnect(dcp, false); + ret = dcp_s2r_ext_disconnect(dcp); if (ret) { dcp_enable_typec_work(dcp); dcp_s2r_hdmi_irq_enable(dcp); diff --git a/tools/asahi/dcp-s2r-ext-disconnect-tests.py b/tools/asahi/dcp-s2r-ext-disconnect-tests.py new file mode 100755 index 0000000000000..36aac8d558a4e --- /dev/null +++ b/tools/asahi/dcp-s2r-ext-disconnect-tests.py @@ -0,0 +1,392 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Run the real dcp_s2r_ext_disconnect() link-withdrawal path against mocks. + +dcp_s2r_ext_suspend() stops an external DCP firmware cold across deep sleep. +Retained-external (H17P/ext26) pipes still use dcp_external_disconnect(), +whereas non-retained firmware-27 external pipes (e.g. T8112/T8103/T8132-v27 +dcpext) have no retained epoch: they must give HPD-low the firmware-27 +withdrawal timeout, still call dcp_dptx_disconnect() if HPD-low fails, restore +typec_crtc_off on failure, and never latch dcp->external_error so later +suspends can retry cleanly. + +--mutation names a production-code defect that the test matrix must kill. +""" +import argparse +import re +import subprocess +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +DCP = "drivers/gpu/drm/apple/dcp.c" + +MUTATIONS = { + # Old bug: route every s2r_ext pipe through dcp_external_disconnect(). + "always-external": ( + "\tret = dcp_s2r_ext_disconnect(dcp);\n", + "\tret = dcp_external_disconnect(dcp, false);\n", + ), + # Cap firmware-27 HPD-low wait at 1 s instead of DCP_V27_EXT_WITHDRAW_MS. + "short-timeout": ( + "DCP_V27_EXT_WITHDRAW_MS : MSEC_PER_SEC;", + "MSEC_PER_SEC : MSEC_PER_SEC;", + ), + # Give up before calling dcp_dptx_disconnect() when HPD-low times out. + "abort-on-hpd-err": ( + "\tif (hpd_ret)\n\t\tdev_warn(", + "\tif (hpd_ret)\n\t\treturn hpd_ret;\n\tif (0)\n\t\tdev_warn(", + ), + # Leave typec_crtc_off set when sleep withdrawal fails. + "no-restore-crtc-off": ( + "\tif (set_crtc_off)\n\t\tWRITE_ONCE(dcp->typec_crtc_off, prev_crtc_off);\n", + "\tif (0 && set_crtc_off)\n\t\tWRITE_ONCE(dcp->typec_crtc_off, prev_crtc_off);\n", + ), + # Bypass retained-external routing for H17P. + "skip-retained": ( + "\tbool set_crtc_off = false;\n\tbool prev_crtc_off = false;\n\tint ret;\n\n\tif (dcp_is_retained_external(dcp))\n\t\treturn dcp_external_disconnect(dcp, false);\n", + "\tbool set_crtc_off = false;\n\tbool prev_crtc_off = false;\n\tint ret;\n", + ), + # Ignore tiled split port 1 during sleep link withdrawal. + "skip-split": ( + "\tif (READ_ONCE(dcp->split.active)) {\n", + "\tif (0 && READ_ONCE(dcp->split.active)) {\n", + ), +} + + +def block(text, start): + opening = text.index("{", start) + depth = 0 + for end in range(opening, len(text)): + depth += text[end] == "{" + depth -= text[end] == "}" + if not depth: + return text[start:end + 1] + raise AssertionError("unbalanced production block") + + +def function(text, name): + match = re.search(r"\n((?:static )?[^\n;]*\b" + name + r"\([^;{}]*\)\n)\{", text) + assert match, name + return block(text, match.start(1)) + + +HARNESS = r""" +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define MSEC_PER_SEC 1000U +#define DCP_FIRMWARE_V_27_0 27 +#define DCP_FIRMWARE_H17P 17 +#define dev_warn(...) ((void)0) +#define scoped_guard(kind, lock) for (int _once = (assert(!*(lock)), *(lock) = 1, 1); _once; *(lock) = 0, _once = 0) + +struct device { int unused; }; +struct apple_epic_service { u32 port; }; +struct dptx_port { + struct apple_epic_service *service; + bool connected; +}; +struct apple_dcp { + struct device *dev; + int fw_compat; + int hpd_mutex; + void *avep; + struct { bool active; } split; + struct dptx_port dptxport[2]; + bool retained_external; + bool typec_output; + bool typec_cable_connected; + bool typec_crtc_off; + bool external_stopped; + int external_error; +}; + +static struct apple_dcp *active_dcp; +static char trace[128]; +static size_t trace_len; +static unsigned int last_hpd_ms[2]; +static int external_disconnect_ret; +static int hpd_ret_for_port[2]; +static int disconnect_ret_for_port[2]; +static bool hpd_saw_crtc_off[2]; + +static void note(const char *s) +{ + size_t n = strlen(s); + assert(trace_len + n < sizeof(trace)); + memcpy(trace + trace_len, s, n); + trace_len += n; + trace[trace_len] = 0; +} + +static void reset_mocks(struct apple_dcp *dcp) +{ + active_dcp = dcp; + trace_len = 0; + trace[0] = 0; + memset(last_hpd_ms, 0, sizeof(last_hpd_ms)); + memset(hpd_ret_for_port, 0, sizeof(hpd_ret_for_port)); + memset(disconnect_ret_for_port, 0, sizeof(disconnect_ret_for_port)); + memset(hpd_saw_crtc_off, 0, sizeof(hpd_saw_crtc_off)); + external_disconnect_ret = 0; +} + +static bool dcp_is_retained_external(struct apple_dcp *dcp) { return dcp->retained_external; } +static bool dcp_is_typec_output(struct apple_dcp *dcp) { return dcp->typec_output; } +static bool dptx_service_available(struct dptx_port *p) { return p->service != NULL; } +static void av_service_disconnect(struct apple_dcp *dcp) { (void)dcp; note("A"); } + +static int dcp_external_disconnect(struct apple_dcp *dcp, bool physical) +{ + assert(!physical); + note("E"); + if (external_disconnect_ret) + dcp->external_error = external_disconnect_ret; + else + dcp->external_stopped = true; + return external_disconnect_ret; +} + +static int dptxport_set_hpd_timeout(struct apple_epic_service *service, bool hpd, + unsigned int timeout_ms) +{ + char buf[8]; + + assert(!hpd); + assert(active_dcp->hpd_mutex == 0); + last_hpd_ms[service->port] = timeout_ms; + hpd_saw_crtc_off[service->port] = active_dcp->typec_crtc_off; + snprintf(buf, sizeof(buf), "H%u", service->port); + note(buf); + return hpd_ret_for_port[service->port]; +} + +static int dcp_dptx_disconnect(struct apple_dcp *dcp, u32 port) +{ + char buf[8]; + + assert(dcp->hpd_mutex == 0); + snprintf(buf, sizeof(buf), "D%u", port); + note(buf); + if (!disconnect_ret_for_port[port]) + dcp->dptxport[port].connected = false; + return disconnect_ret_for_port[port]; +} +""" + +TESTS = r""" +static int suspend_disconnect_call(struct apple_dcp *dcp) +{ + int ret; +__SUSPEND_CALL__ + return ret; +} + +int main(void) +{ + struct device dev = {0}; + struct apple_epic_service svc0 = { .port = 0 }; + struct apple_epic_service svc1 = { .port = 1 }; + int av_tok = 1; + + /* 1. Retained-external (H17P) still uses dcp_external_disconnect(). */ + { + struct apple_dcp dcp = { + .dev = &dev, + .fw_compat = DCP_FIRMWARE_H17P, + .retained_external = true, + .typec_output = true, + .typec_cable_connected = true, + .dptxport = { { .service = &svc0, .connected = true } }, + }; + reset_mocks(&dcp); + assert(suspend_disconnect_call(&dcp) == 0); + assert(strcmp(trace, "E") == 0); + assert(dcp.external_stopped); + assert(dcp.external_error == 0); + + reset_mocks(&dcp); + external_disconnect_ret = -ETIMEDOUT; + assert(suspend_disconnect_call(&dcp) == -ETIMEDOUT); + assert(strcmp(trace, "E") == 0); + assert(dcp.external_error == -ETIMEDOUT); + } + + /* 2. Non-retained fw 27.2 Type-C: 15 s HPD-low timeout, typec_crtc_off set first. */ + { + struct apple_dcp dcp = { + .dev = &dev, + .fw_compat = DCP_FIRMWARE_V_27_0, + .avep = &av_tok, + .typec_output = true, + .typec_cable_connected = true, + .dptxport = { { .service = &svc0, .connected = true } }, + }; + reset_mocks(&dcp); + assert(suspend_disconnect_call(&dcp) == 0); + assert(strcmp(trace, "AH0D0") == 0); + assert(last_hpd_ms[0] == 15000U); + assert(hpd_saw_crtc_off[0]); + assert(dcp.typec_crtc_off); + assert(!dcp.dptxport[0].connected); + assert(!dcp.external_stopped); + assert(dcp.external_error == 0); + } + + /* 3. HPD-low timeout still releases the display and lets sleep succeed. */ + { + struct apple_dcp dcp = { + .dev = &dev, + .fw_compat = DCP_FIRMWARE_V_27_0, + .typec_output = true, + .typec_cable_connected = true, + .dptxport = { { .service = &svc0, .connected = true } }, + }; + reset_mocks(&dcp); + hpd_ret_for_port[0] = -ETIMEDOUT; + assert(suspend_disconnect_call(&dcp) == 0); + assert(strcmp(trace, "H0D0") == 0); + assert(last_hpd_ms[0] == 15000U); + assert(!dcp.dptxport[0].connected); + assert(dcp.external_error == 0); + } + + /* 4. Release failure refuses sleep without latching external_error; retry works. */ + { + struct apple_dcp dcp = { + .dev = &dev, + .fw_compat = DCP_FIRMWARE_V_27_0, + .typec_output = true, + .typec_cable_connected = true, + .typec_crtc_off = false, + .dptxport = { { .service = &svc0, .connected = true } }, + }; + reset_mocks(&dcp); + hpd_ret_for_port[0] = -ETIMEDOUT; + disconnect_ret_for_port[0] = -ETIMEDOUT; + assert(suspend_disconnect_call(&dcp) == -ETIMEDOUT); + assert(strcmp(trace, "H0D0") == 0); + assert(dcp.external_error == 0); + assert(!dcp.external_stopped); + assert(!dcp.typec_crtc_off); + assert(dcp.dptxport[0].connected); + + /* Cycle 2 retries cleanly instead of being wedged for the rest of the boot. */ + reset_mocks(&dcp); + assert(suspend_disconnect_call(&dcp) == 0); + assert(strcmp(trace, "H0D0") == 0); + assert(!dcp.dptxport[0].connected); + assert(dcp.typec_crtc_off); + } + + /* 5. Tiled split withdraws port 1 before port 0. */ + { + struct apple_dcp dcp = { + .dev = &dev, + .fw_compat = DCP_FIRMWARE_V_27_0, + .split = { .active = true }, + .typec_output = true, + .typec_cable_connected = true, + .dptxport = { + { .service = &svc0, .connected = true }, + { .service = &svc1, .connected = true }, + }, + }; + reset_mocks(&dcp); + assert(suspend_disconnect_call(&dcp) == 0); + assert(strcmp(trace, "H1D1H0D0") == 0); + assert(last_hpd_ms[1] == 15000U && last_hpd_ms[0] == 15000U); + assert(!dcp.dptxport[0].connected && !dcp.dptxport[1].connected); + } + + /* 6. Idle external DCP (no display attached): no DPTX calls, no state mutation. */ + { + struct apple_dcp dcp = { + .dev = &dev, + .fw_compat = DCP_FIRMWARE_V_27_0, + .typec_output = true, + .typec_cable_connected = false, + }; + reset_mocks(&dcp); + assert(suspend_disconnect_call(&dcp) == 0); + assert(strcmp(trace, "") == 0); + assert(!dcp.typec_crtc_off); + assert(!dcp.external_stopped); + } + + /* 7. Non-Type-C output withdraws link without touching typec_crtc_off. */ + { + struct apple_dcp dcp = { + .dev = &dev, + .fw_compat = DCP_FIRMWARE_V_27_0, + .typec_output = false, + .typec_cable_connected = false, + .dptxport = { { .service = &svc0, .connected = true } }, + }; + reset_mocks(&dcp); + assert(suspend_disconnect_call(&dcp) == 0); + assert(strcmp(trace, "H0D0") == 0); + assert(!dcp.typec_crtc_off); + assert(!dcp.dptxport[0].connected); + } + + return 0; +} +""" + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--mutation", choices=sorted(MUTATIONS)) + args = parser.parse_args() + + source = (ROOT / DCP).read_text() + if args.mutation: + old, new = MUTATIONS[args.mutation] + assert source.count(old) == 1, f"mutation {args.mutation!r} did not match once" + source = source.replace(old, new, 1) + + define = re.search(r"^#define DCP_V27_EXT_WITHDRAW_MS\t\d+$", source, re.M) + assert define, "missing DCP_V27_EXT_WITHDRAW_MS" + + suspend = function(source, "dcp_s2r_ext_suspend") + call = re.search(r"^\tret = dcp_(?:s2r_ext|external)_disconnect\([^\n]+\);$", suspend, re.M) + assert call, "missing disconnect call in dcp_s2r_ext_suspend" + + code = "\n".join([ + HARNESS, + define.group(0), + function(source, "dcp_s2r_ext_withdraw_port"), + function(source, "dcp_s2r_ext_disconnect"), + TESTS.replace("__SUSPEND_CALL__", call.group(0)), + ]) + + with tempfile.TemporaryDirectory() as directory: + c_path = Path(directory) / "s2r_ext_disconnect.c" + exe_path = c_path.with_suffix("") + c_path.write_text(code) + subprocess.run( + ["clang", "-std=gnu11", "-Wall", "-Werror", "-Wno-unused-function", + "-fsanitize=address,undefined", str(c_path), "-o", str(exe_path)], + check=True, + ) + result = subprocess.run([str(exe_path)]) + if result.returncode: + sys.exit(1) + + print("PASS dcp_s2r_ext_disconnect: retained-external preserved; fw27 15 s HPD-low, " + "release after HPD error, no external_error latch, crtc_off restore, split port order") + + +if __name__ == "__main__": + main() diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index ed3ae0c762582..7a325b2196da6 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -267,6 +267,10 @@ def mutations(flag, names, base, expect=KILL): 'dcp-hdmi-sleep-unplug-tests.py': [([], PASS)] + mutations('--mutation', ('keep-native', 'gpio-error', 'ignore-borrowed', 'no-fabric', 'no-fabric-lock', 'no-resume-check'), []), + # Non-retained firmware-27 s2r_ext link withdrawal before deep sleep. + 'dcp-s2r-ext-disconnect-tests.py': [([], PASS)] + + mutations('--mutation', ('always-external', 'short-timeout', 'abort-on-hpd-err', + 'no-restore-crtc-off', 'skip-retained', 'skip-split'), []), # A fixed (non-Type-C) output takes its DP AV service again after a sleep. 'dpavserv-admission-tests.py': [([], PASS)] + mutations('--mutation', ('old-cable-wait', 'ignore-generation', 'ignore-cable', From cb39f7a8f1eb3881ebbd80f2698cd706f4985851 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 2 Dec 2026 02:27:13 -0500 Subject: [PATCH 2/6] drm/apple: dcp: route primary external-output DCPs through s2r_ext A primary DCP driving an external output (fixed DP/HDMI PHY, Type-C display routes, or an explicit HDMI/DP/USB connector type) has no internal panel: dcp_poweroff() already turns the display pipe off on DPMS off and DRM suspend. Routing it through internal-panel dcp_s2r_capable() sends a redundant set_power_state(0) and times out waiting for D572 (-16), skips HDMI HPD IRQ disable and DPTX link withdrawal before sleep, and bypasses fixed-HDMI reconnect on wake. Add dcp_s2r_primary_external() so primary external-output DCPs leave dcp_s2r_capable() and (when s2r_no_secure_port is set) enter dcp_s2r_ext_capable(). Move dcp_s2r_pm_register() after fixed_phy and Type-C route setup in dcp_platform_probe_pipe() so the S2R PM notifier sees the populated external-output state. Internal eDP panels stay on dcp_s2r_capable() unchanged. Add host tests and mutation checks in tools/asahi/dcp-s2r-primary-ext-tests.py. Signed-off-by: Satya Benson --- drivers/gpu/drm/apple/dcp.c | 32 ++- tools/asahi/dcp-s2r-primary-ext-tests.py | 332 +++++++++++++++++++++++ tools/asahi/run-host-tests.py | 5 + 3 files changed, 365 insertions(+), 4 deletions(-) create mode 100755 tools/asahi/dcp-s2r-primary-ext-tests.py diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 6697e15fa09eb..fe44a0e1b70fa 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -9145,9 +9145,6 @@ static int dcp_platform_probe_pipe(struct platform_device *pdev) } platform_set_drvdata(pdev, dcp); - ret = dcp_s2r_pm_register(dcp); - if (ret) - return ret; if (iomfb_is_m4(dcp)) { dcp->fixed_connector_type = DRM_MODE_CONNECTOR_eDP; @@ -9396,6 +9393,10 @@ static int dcp_platform_probe_pipe(struct platform_device *pdev) routes_failed = true; } + ret = dcp_s2r_pm_register(dcp); + if (ret) + return ret; + /* * The manual T6030 external path is not part of the panel's DRM * device. Joining that component set would hold the internal screen @@ -10262,8 +10263,28 @@ void dcp_s2r_dart_power(struct apple_dcp *dcp, u32 on) complete(&dcp->s2r_dart_done); } +/* + * A primary DCP driving an external output (fixed DP/HDMI PHY, Type-C display + * routes, or an explicit HDMI/DP/USB connector type) has no internal panel: + * dcp_poweroff() already turns the display pipe off on DPMS off and DRM + * suspend, so deep sleep must not send an internal-panel set_power_state(0)/D572 + * quiesce or wake it with dcp_s2r_display_on(). Route it through the external + * S2R path instead so its link is withdrawn before sleep and reconnected after + * wake. + */ +static bool dcp_s2r_primary_external(const struct apple_dcp *dcp) +{ + return dcp->hw.s2r && + (dcp_has_external_output(dcp) || + dcp->fixed_connector_type == DRM_MODE_CONNECTOR_HDMIA || + dcp->fixed_connector_type == DRM_MODE_CONNECTOR_DisplayPort || + dcp->fixed_connector_type == DRM_MODE_CONNECTOR_USB); +} + static bool dcp_s2r_capable(struct apple_dcp *dcp) { + if (dcp_s2r_primary_external(dcp)) + return false; /* * Firmware 27.2 and 27.3 run as V_27_0 with their own tables. 27.2's has the AP link's * release (A550); a table without a tag for it can't stop: an empty tag panics the @@ -10497,7 +10518,10 @@ static int dcp_s2r_stop_firmware(struct apple_dcp *dcp) */ static bool dcp_s2r_ext_capable(struct apple_dcp *dcp) { - if (!dcp->hw.s2r_ext || !of_property_present(dcp->dev->of_node, "apple,display-power")) + bool ext_hw = dcp->hw.s2r_ext || + (dcp_s2r_primary_external(dcp) && dcp->hw.s2r_no_secure_port); + + if (!ext_hw || !of_property_present(dcp->dev->of_node, "apple,display-power")) return false; if (dcp->fw_compat == DCP_FIRMWARE_V_27_0) /* diff --git a/tools/asahi/dcp-s2r-primary-ext-tests.py b/tools/asahi/dcp-s2r-primary-ext-tests.py new file mode 100755 index 0000000000000..3e80352f7bad9 --- /dev/null +++ b/tools/asahi/dcp-s2r-primary-ext-tests.py @@ -0,0 +1,332 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Verify primary external-output DCP S2R routing against the real helpers. + +A primary DCP that drives an external output (e.g. fixed HDMI-A with dp-phy, +direct HDMI-A connector, or Type-C display routes) has no internal panel: its +pipe is powered off by dcp_poweroff() on DPMS off and DRM suspend, so deep +sleep must route it through dcp_s2r_ext_capable() rather than internal-panel +dcp_s2r_capable() (which sends set_power_state(0)/D572 and wakes with +dcp_s2r_display_on()). Internal eDP panels stay on dcp_s2r_capable(), and +dcp_s2r_pm_register() runs after fixed_phy and Type-C routes are probed. + +--mutation names a production-code defect that the test matrix must kill. +""" +import argparse +import re +import subprocess +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +DCP = "drivers/gpu/drm/apple/dcp.c" +DCP_HDR = "drivers/gpu/drm/apple/dcp-internal.h" + +MUTATIONS = { + # Old bug: primary external DCP stays on internal-panel dcp_s2r_capable(). + "keep-internal": ( + "\tif (dcp_s2r_primary_external(dcp))\n\t\treturn false;\n", + "", + ), + # Drop primary external routing from dcp_s2r_ext_capable(). + "drop-ext-route": ( + "\tbool ext_hw = dcp->hw.s2r_ext ||\n" + "\t\t (dcp_s2r_primary_external(dcp) && dcp->hw.s2r_no_secure_port);\n", + "\tbool ext_hw = dcp->hw.s2r_ext;\n", + ), + # Ignore fixed_phy / external output when classifying primary DCP. + "ignore-ext-output": ( + "\t (dcp_has_external_output(dcp) ||\n", + "\t (false ||\n", + ), + # Ignore HDMI-A connector without fixed_phy (e.g. direct HDMI on primary DCP). + "ignore-hdmia": ( + "\t\tdcp->fixed_connector_type == DRM_MODE_CONNECTOR_HDMIA ||\n", + "\t\tfalse ||\n", + ), + # Route secure-port primary external DCPs into ext path that lacks secure restart. + "ignore-secure-port": ( + "(dcp_s2r_primary_external(dcp) && dcp->hw.s2r_no_secure_port);", + "dcp_s2r_primary_external(dcp);", + ), + # Register S2R PM notifier before fixed_phy/Type-C routes are probed. + "early-pm-register": ( + "\tret = dcp_s2r_pm_register(dcp);\n\tif (ret)\n\t\treturn ret;\n\n\t/*\n\t * The manual T6030", + "\t/*\n\t * The manual T6030", + ), +} + + +def block(text, start): + opening = text.index("{", start) + depth = 0 + for end in range(opening, len(text)): + depth += text[end] == "{" + depth -= text[end] == "}" + if not depth: + return text[start:end + 1] + raise AssertionError("unbalanced production block") + + +def function(text, name): + match = re.search(r"\n((?:static (?:inline )?)?[^\n;]*\b" + name + r"\([^;{}]*\)\n)\{", text) + assert match, name + return block(text, match.start(1)) + + +HARNESS = r""" +#include +#include +#include +#include + +#define DCP_FIRMWARE_V_27_0 27 +#define DCP_FIRMWARE_H17P 17 +#define DCP_FIRMWARE_V_26_6 26 + +#define DRM_MODE_CONNECTOR_Unknown 0 +#define DRM_MODE_CONNECTOR_DisplayPort 10 +#define DRM_MODE_CONNECTOR_HDMIA 11 +#define DRM_MODE_CONNECTOR_eDP 14 +#define DRM_MODE_CONNECTOR_USB 20 + +enum { dcpep_unnamed_500 = 0 }; + +struct device_node { + bool has_display_power; +}; +struct device { + struct device_node *of_node; +}; +struct dcp_v27_method { + char tag[8]; +}; +struct dcp_s2r_layout { + int dummy; +}; +struct apple_dcp_hw { + bool s2r; + bool s2r_ext; + bool s2r_no_secure_port; + bool s2r_untested; + const struct dcp_s2r_layout *s2r_layout; +}; +struct apple_dcp { + struct device *dev; + struct apple_dcp_hw hw; + int fw_compat; + bool dcpext; + unsigned int nr_typec_routes; + void *fixed_phy; + int fixed_connector_type; + bool s2r_dt_layout_ok; + const struct dcp_v27_method *v27_methods; +}; + +static bool s2r_untested = true; + +static bool of_property_present(const struct device_node *np, const char *name) +{ + return np && strcmp(name, "apple,display-power") == 0 && np->has_display_power; +} +""" + +TESTS = r""" +int main(void) +{ + struct device_node np_pwr = { .has_display_power = true }; + struct device_node np_nopwr = { .has_display_power = false }; + struct device dev_pwr = { .of_node = &np_pwr }; + struct device dev_nopwr = { .of_node = &np_nopwr }; + struct dcp_s2r_layout layout = { .dummy = 1 }; + struct dcp_v27_method methods_ok[1] = { { .tag = "A500" } }; + struct dcp_v27_method methods_empty[1] = { { .tag = "" } }; + int phy_tok = 1; + + /* 1. Internal eDP panel on primary DCP stays on dcp_s2r_capable(). */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .fixed_connector_type = DRM_MODE_CONNECTOR_eDP, + .v27_methods = methods_ok, + }; + assert(dcp_s2r_capable(&dcp)); + assert(!dcp_s2r_ext_capable(&dcp)); + } + + /* 2. Probe-time internal panel (connector still Unknown) stays internal. */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .fixed_connector_type = DRM_MODE_CONNECTOR_Unknown, + .v27_methods = methods_ok, + }; + assert(dcp_s2r_capable(&dcp)); + assert(!dcp_s2r_ext_capable(&dcp)); + } + + /* 3. Primary DCP with fixed HDMI-A PHY routes to dcp_s2r_ext_capable(). */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .fixed_phy = &phy_tok, + .fixed_connector_type = DRM_MODE_CONNECTOR_HDMIA, + .v27_methods = methods_ok, + }; + assert(!dcp_s2r_capable(&dcp)); + assert(dcp_s2r_ext_capable(&dcp)); + } + + /* 4. Primary DCP with fixed PHY even before connector type is set routes to ext. */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .fixed_phy = &phy_tok, + .fixed_connector_type = DRM_MODE_CONNECTOR_Unknown, + .v27_methods = methods_ok, + }; + assert(!dcp_s2r_capable(&dcp)); + assert(dcp_s2r_ext_capable(&dcp)); + } + + /* 5. Primary DCP with direct HDMI-A (no fixed_phy) routes to dcp_s2r_ext_capable(). */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .fixed_connector_type = DRM_MODE_CONNECTOR_HDMIA, + .v27_methods = methods_ok, + }; + assert(!dcp_s2r_capable(&dcp)); + assert(dcp_s2r_ext_capable(&dcp)); + } + + /* 6. Primary DCP with Type-C routes goes to dcp_s2r_ext_capable(). */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .nr_typec_routes = 2, + .fixed_connector_type = DRM_MODE_CONNECTOR_Unknown, + .v27_methods = methods_ok, + }; + assert(!dcp_s2r_capable(&dcp)); + assert(dcp_s2r_ext_capable(&dcp)); + } + + /* 7. Primary external DCP with a secure port stays out of ext path (no secure restart). */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = false, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .fixed_phy = &phy_tok, + .fixed_connector_type = DRM_MODE_CONNECTOR_HDMIA, + .v27_methods = methods_ok, + }; + assert(!dcp_s2r_capable(&dcp)); + assert(!dcp_s2r_ext_capable(&dcp)); + } + + /* 8. Missing A500 method tag or apple,display-power disables both paths. */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .fixed_phy = &phy_tok, + .fixed_connector_type = DRM_MODE_CONNECTOR_HDMIA, + .v27_methods = methods_empty, + }; + assert(!dcp_s2r_capable(&dcp)); + assert(!dcp_s2r_ext_capable(&dcp)); + + dcp.v27_methods = methods_ok; + dcp.dev = &dev_nopwr; + assert(!dcp_s2r_capable(&dcp)); + assert(!dcp_s2r_ext_capable(&dcp)); + } + + /* 9. Native external DCP (hw.s2r_ext) unchanged. */ + { + struct apple_dcp dcp = { + .dev = &dev_pwr, + .hw = { .s2r_ext = true, .s2r_no_secure_port = true, .s2r_layout = &layout }, + .fw_compat = DCP_FIRMWARE_V_27_0, + .dcpext = true, + .v27_methods = methods_ok, + }; + assert(!dcp_s2r_capable(&dcp)); + assert(dcp_s2r_ext_capable(&dcp)); + } + + return 0; +} +""" + + +def check_probe_order(source): + probe = function(source, "dcp_platform_probe_pipe") + phy_pos = probe.find("dcp->fixed_phy = dcp->phy;") + routes_pos = probe.find("dcp_register_typec_routes(dcp)") + pm_pos = probe.find("dcp_s2r_pm_register(dcp)") + assert phy_pos >= 0 and routes_pos >= 0, "probe missing fixed_phy or route setup" + if pm_pos < 0 or pm_pos < phy_pos or pm_pos < routes_pos: + print("FAIL line 1: dcp_s2r_pm_register() must run after fixed_phy and Type-C routes", + file=sys.stderr) + sys.exit(1) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--mutation", choices=sorted(MUTATIONS)) + args = parser.parse_args() + + source = (ROOT / DCP).read_text() + header = (ROOT / DCP_HDR).read_text() + if args.mutation: + old, new = MUTATIONS[args.mutation] + assert source.count(old) == 1, f"mutation {args.mutation!r} did not match once" + source = source.replace(old, new, 1) + + check_probe_order(source) + + code = "\n".join([ + HARNESS, + function(header, "dcp_has_external_output"), + function(source, "dcp_s2r_primary_external"), + function(source, "dcp_s2r_capable"), + function(source, "dcp_s2r_ext_capable"), + TESTS, + ]) + + with tempfile.TemporaryDirectory() as directory: + c_path = Path(directory) / "s2r_primary_ext.c" + exe_path = c_path.with_suffix("") + c_path.write_text(code) + subprocess.run( + ["clang", "-std=gnu11", "-Wall", "-Werror", "-Wno-unused-function", + "-fsanitize=address,undefined", str(c_path), "-o", str(exe_path)], + check=True, + ) + result = subprocess.run([str(exe_path)]) + if result.returncode: + sys.exit(1) + + print("PASS dcp_s2r_primary_ext: internal eDP kept on s2r; primary fixed-PHY/HDMI-A/Type-C " + "routed to s2r_ext; secure-port guarded; PM notifier registered after route probe") + + +if __name__ == "__main__": + main() diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index ea21ddc152a1f..24241dd58dea0 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -281,6 +281,11 @@ def mutations(flag, names, base, expect=KILL): 'dcp-s2r-ext-disconnect-tests.py': [([], PASS)] + mutations('--mutation', ('always-external', 'short-timeout', 'abort-on-hpd-err', 'no-restore-crtc-off', 'skip-retained', 'skip-split'), []), + # Primary DCPs with external outputs route to dcp_s2r_ext_capable(). + 'dcp-s2r-primary-ext-tests.py': [([], PASS)] + + mutations('--mutation', ('keep-internal', 'drop-ext-route', 'ignore-ext-output', + 'ignore-hdmia', 'ignore-secure-port', + 'early-pm-register'), []), # A fixed (non-Type-C) output takes its DP AV service again after a sleep. 'dpavserv-admission-tests.py': [([], PASS)] + mutations('--mutation', ('old-cable-wait', 'ignore-generation', 'ignore-cable', From 875c1cec941a1267bf5fcea09eabd2c3807aa02f Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 2 Dec 2026 02:24:25 -0500 Subject: [PATCH 3/6] drm/apple: dcp: quiesce s2r_ext before idle and keep live firmware on refusal When dcp_s2r_ext_suspend() withdraws an attached external display link immediately before system sleep, the firmware emits hotplug, AFK teardown, IOMFB callback, and OSLOG counter traffic right before A500 and RTKit idle. Unlike dcp_s2r_quiesce(), dcp_s2r_ext_suspend() did not flush hotplug work, AFK endpoint workqueues, or RTKit RX, did not wait for IOMFB channels to go idle before or after A500, and did not poll and retry RTKit idle once. In addition, when apple_rtkit_sleep_stop() failed with the CPU still running (control RUN set, not crashed), dcp_s2r_ext_suspend() unconditionally latched dcp->crashed = true without undoing AP idle or re-enabling presentation and link work. The next sleep then entered dcp_s2r_stop_crashed(), clearing RUN under live firmware and crashing warm restart. Drain hotplug work, RTKit RX, AFK endpoint workqueues, and in-flight IOMFB channels before A500, wait for IOMFB channels and RTKit RX after A500, poll and retry RTKit idle once when the firmware is still live, and on final idle refusal with a running non-crashed CPU restore AP power, unpark the route, unpause presentation, re-enable Type-C work and HDMI HPD (reconnecting fixed HDMI when applicable), and leave dcp->crashed clear (matching dcp_s2r_quiesce()). Add host test tools/asahi/dcp-s2r-ext-idle-tests.py (registered in tools/asahi/run-host-tests.py) exercising the extracted production functions against mocks and six negative-control mutations. Signed-off-by: Satya Benson --- drivers/gpu/drm/apple/dcp.c | 99 +++- tools/asahi/dcp-s2r-ext-idle-tests.py | 624 ++++++++++++++++++++++++++ tools/asahi/run-host-tests.py | 5 + 3 files changed, 717 insertions(+), 11 deletions(-) create mode 100755 tools/asahi/dcp-s2r-ext-idle-tests.py diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index fe44a0e1b70fa..7182d238f66ca 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10711,6 +10711,89 @@ static int dcp_s2r_ext_disconnect(struct apple_dcp *dcp) return ret; } +static void dcp_s2r_ext_quiesce_endpoints(struct apple_dcp *dcp) +{ + struct apple_connector *con = READ_ONCE(dcp->connector); + struct apple_connector *port = READ_ONCE(dcp->typec_connector); + struct apple_dcp_afkep *eps[] = { + dcp->systemep, dcp->ibootep, dcp->dptx_controller_ep, + dcp->dptxep, dcp->dcpavservep, dcp->avep, dcp->dpavctrlep, + }; + unsigned int i; + + if (con) + flush_work(&con->hotplug_wq); + if (port && port != con) + flush_work(&port->hotplug_wq); + + apple_rtkit_poll(dcp->rtk); + apple_rtkit_flush_rx(dcp->rtk); + for (i = 0; i < ARRAY_SIZE(eps); i++) + if (!IS_ERR_OR_NULL(eps[i]) && eps[i]->wq) + flush_workqueue(eps[i]->wq); + apple_rtkit_flush_rx(dcp->rtk); + if (iomfb_wait_channels_idle(dcp, 100)) + dev_warn(dcp->dev, "firmware calls still in flight before A500\n"); +} + +static int dcp_s2r_ext_idle_stop(struct apple_dcp *dcp) +{ + void __iomem *control = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL; + void __iomem *status = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_STATUS; + u32 ctrl, stat; + bool running; + int ret; + + /* Firmware callbacks and log counters after A500 must be answered before idle. */ + usleep_range(5000, 6000); + if (iomfb_wait_channels_idle(dcp, 100)) + dev_warn(dcp->dev, "firmware calls still in flight before idle\n"); + apple_rtkit_poll(dcp->rtk); + apple_rtkit_flush_rx(dcp->rtk); + + ret = apple_rtkit_sleep_stop(dcp->rtk, control); + if (ret && !READ_ONCE(dcp->crashed) && + (readl(control) & APPLE_DCP_COPROC_CPU_CONTROL_RUN)) { + dev_warn(dcp->dev, + "firmware refused idle: %d, control %#x status %#x: retrying once\n", + ret, readl(control), readl(status)); + apple_rtkit_poll(dcp->rtk); + apple_rtkit_flush_rx(dcp->rtk); + msleep(30); + ret = apple_rtkit_sleep_stop(dcp->rtk, control); + } + if (!ret) + return 0; + + ctrl = readl(control); + stat = readl(status); + running = !READ_ONCE(dcp->crashed) && + (ctrl & APPLE_DCP_COPROC_CPU_CONTROL_RUN); + dev_err(dcp->dev, "firmware did not go idle: %d, control %#x status %#x\n", + ret, ctrl, stat); + dcp_typec_route_unpark(dcp); + if (running) { + /* + * Refused while the CPU still runs: leave the firmware alive + * instead of marking it crashed and stopping it cold on the + * next sleep (matching dcp_s2r_quiesce()). + */ + dcp_s2r_idle_undo(dcp); + iomfb_pause_presentation(dcp, false); + dcp_enable_typec_work(dcp); + if (!dcp_is_typec_output(dcp) && dcp->hdmi_hpd && + !READ_ONCE(dcp->active_typec_route)) + dcp_fixed_hdmi_reconnect(dcp, false); + dcp_s2r_hdmi_irq_enable(dcp); + dev_warn(dcp->dev, "external firmware left running after refused idle\n"); + return (ret == -ETIMEDOUT || ret == -ETIME) ? -EBUSY : ret; + } + + WRITE_ONCE(dcp->crashed, true); + /* Keep typec work and hdmi_hpd_irq disabled while firmware is crashed. */ + return ret == -ETIMEDOUT ? -EBUSY : ret; +} + static int dcp_s2r_ext_suspend(struct apple_dcp *dcp) { int ret; @@ -10770,6 +10853,8 @@ static int dcp_s2r_ext_suspend(struct apple_dcp *dcp) return -EBUSY; } + dcp_s2r_ext_quiesce_endpoints(dcp); + /* The display's link went above: its crossbar output follows, as on macOS. */ dcp_typec_route_park(dcp); @@ -10782,18 +10867,10 @@ static int dcp_s2r_ext_suspend(struct apple_dcp *dcp) /* Keep typec work and hdmi_hpd_irq disabled while firmware is crashed. */ return ret == -ETIMEDOUT ? -EBUSY : ret; } - usleep_range(4000, 5000); - ret = apple_rtkit_sleep_stop(dcp->rtk, dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL); - if (ret) { - dev_err(dcp->dev, "firmware did not go idle: %d, control %#x status %#x\n", ret, - readl(dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL), - readl(dcp->coproc_reg + APPLE_DCP_COPROC_CPU_STATUS)); - WRITE_ONCE(dcp->crashed, true); - dcp_typec_route_unpark(dcp); - /* Keep typec work and hdmi_hpd_irq disabled while firmware is crashed. */ - return ret == -ETIMEDOUT ? -EBUSY : ret; - } + ret = dcp_s2r_ext_idle_stop(dcp); + if (ret) + return ret; dcp_s2r_find_darts(dcp); dcp->s2r_stopped = true; diff --git a/tools/asahi/dcp-s2r-ext-idle-tests.py b/tools/asahi/dcp-s2r-ext-idle-tests.py new file mode 100755 index 0000000000000..766cdbdf8e332 --- /dev/null +++ b/tools/asahi/dcp-s2r-ext-idle-tests.py @@ -0,0 +1,624 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Run the real dcp_s2r_ext quiesce and idle-refusal paths against mocks. + +On external firmware-27 DCPs, dcp_s2r_ext_suspend() disconnects the DPTX link +and audio service immediately before sending A500 and idling RTKit. The +firmware's teardown hotplug work, AFK endpoint replies, OSLOG counters, and +IOMFB callbacks must drain before A500 and before idle. If idle is still +refused while the CPU continues running (control RUN set, not crashed), the +driver must restore AP power and leave the firmware live instead of marking +dcp->crashed = true and cold-stopping live firmware on the next sleep. +""" + +from __future__ import annotations + +import argparse +import re +import subprocess +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +DCP_C = ROOT / "drivers/gpu/drm/apple/dcp.c" + +MUTATIONS: dict[str, tuple[str, str]] = { + "skip-quiesce": ( + "\tdcp_s2r_ext_quiesce_endpoints(dcp);\n", + "\t/* mutation: skip endpoint/channel quiesce before A500 */\n", + ), + "skip-post-a500-idle": ( + "\tif (iomfb_wait_channels_idle(dcp, 100))\n" + "\t\tdev_warn(dcp->dev, \"firmware calls still in flight before idle\\n\");\n" + "\tapple_rtkit_poll(dcp->rtk);\n", + "\t/* mutation: skip post-A500 iomfb_wait_channels_idle */\n" + "\tapple_rtkit_poll(dcp->rtk);\n", + ), + "skip-retry": ( + "\tif (ret && !READ_ONCE(dcp->crashed) &&\n" + "\t (readl(control) & APPLE_DCP_COPROC_CPU_CONTROL_RUN)) {\n", + "\tif (0 && ret && !READ_ONCE(dcp->crashed) &&\n" + "\t (readl(control) & APPLE_DCP_COPROC_CPU_CONTROL_RUN)) {\n", + ), + "mark-live-crashed": ( + "\tif (running) {\n", + "\tif (0 && running) {\n", + ), + "skip-idle-undo": ( + "\t\tdcp_s2r_idle_undo(dcp);\n", + "\t\t/* mutation: skip dcp_s2r_idle_undo */\n", + ), + "skip-fixed-hdmi-reconnect": ( + "\t\tif (!dcp_is_typec_output(dcp) && dcp->hdmi_hpd &&\n" + "\t\t !READ_ONCE(dcp->active_typec_route))\n" + "\t\t\tdcp_fixed_hdmi_reconnect(dcp, false);\n", + "\t\t/* mutation: skip fixed HDMI reconnect */\n", + ), +} + +HARNESS = r""" +#include +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +#define __iomem +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define IS_ERR_OR_NULL(p) ((p) == NULL || (uintptr_t)(p) >= (uintptr_t)-4095) +#define msecs_to_jiffies(ms) (ms) + +#define APPLE_DCP_COPROC_CPU_CONTROL 0x00 +#define APPLE_DCP_COPROC_CPU_STATUS 0x04 +#define APPLE_DCP_COPROC_CPU_CONTROL_RUN (1u << 4) + +#define dev_dbg(dev, fmt, ...) do { (void)(dev); if (0) printf(fmt, ##__VA_ARGS__); } while (0) +#define dev_info(dev, fmt, ...) do { (void)(dev); if (0) printf(fmt, ##__VA_ARGS__); } while (0) +#define dev_warn(dev, fmt, ...) do { (void)(dev); if (0) printf(fmt, ##__VA_ARGS__); } while (0) +#define dev_err(dev, fmt, ...) do { (void)(dev); if (0) printf(fmt, ##__VA_ARGS__); } while (0) + +struct device { int dummy; }; +struct work_struct { int id; }; +struct delayed_work { int id; }; +struct workqueue_struct { int id; }; +struct apple_rtkit { int id; }; +struct gpio_desc { int dummy; }; + +struct apple_connector { + struct work_struct hotplug_wq; +}; + +struct apple_dcp_afkep { + struct workqueue_struct *wq; +}; + +struct apple_dcp { + struct device *dev; + struct apple_rtkit *rtk; + uint8_t *coproc_reg; + bool s2r_fw_down; + bool s2r_stopped; + bool crashed; + bool active_typec_route; + bool typec_cable_connected; + bool is_typec; + struct gpio_desc *hdmi_hpd; + struct apple_connector *connector; + struct apple_connector *typec_connector; + struct delayed_work swap_watchdog_wq; + struct apple_dcp_afkep *systemep; + struct apple_dcp_afkep *ibootep; + struct apple_dcp_afkep *dptx_controller_ep; + struct apple_dcp_afkep *dptxep; + struct apple_dcp_afkep *dcpavservep; + struct apple_dcp_afkep *avep; + struct apple_dcp_afkep *dpavctrlep; +}; + +enum event_kind { + EV_DISCONNECT, + EV_QUEUE_SETTLE, + EV_FLUSH_WORK, + EV_RTKIT_POLL, + EV_RTKIT_FLUSH_RX, + EV_FLUSH_WQ, + EV_CHANNELS_IDLE, + EV_PARK, + EV_UNPARK, + EV_A500, + EV_SLEEP_STOP, + EV_MSLEEP, + EV_IDLE_UNDO, + EV_PAUSE_ON, + EV_PAUSE_OFF, + EV_TYPEC_ENABLE, + EV_FIXED_HDMI_RECONNECT, + EV_HDMI_IRQ_ENABLE, + EV_STOP_CRASHED, +}; + +struct event { + enum event_kind kind; + int arg; +}; + +static struct event log_events[128]; +static size_t log_len; + +static void record(enum event_kind kind, int arg) +{ + assert(log_len < ARRAY_SIZE(log_events)); + log_events[log_len++] = (struct event){ .kind = kind, .arg = arg }; +} + +static u32 fake_regs[2]; +static int sleep_stop_plan[4]; +static int sleep_stop_calls; +static bool crash_on_first_idle; +static bool clear_run_on_first_idle; +static int wait_channels_idle_calls; +static int rtkit_poll_calls; +static int rtkit_flush_rx_calls; +static int flush_work_calls; +static int flush_wq_calls; +static int park_count; +static int unpark_count; +static int idle_undo_calls; +static int pause_calls; +static int unpause_calls; +static int typec_disable_calls; +static int typec_enable_calls; +static int hdmi_irq_disable_calls; +static int hdmi_irq_enable_calls; +static int fixed_hdmi_reconnect_calls; +static int stop_crashed_calls; +static int stop_streams_calls; + +static void reset_mocks(void) +{ + log_len = 0; + fake_regs[0] = APPLE_DCP_COPROC_CPU_CONTROL_RUN; + fake_regs[1] = 0x2d; + memset(sleep_stop_plan, 0, sizeof(sleep_stop_plan)); + sleep_stop_calls = 0; + crash_on_first_idle = false; + clear_run_on_first_idle = false; + wait_channels_idle_calls = 0; + rtkit_poll_calls = 0; + rtkit_flush_rx_calls = 0; + flush_work_calls = 0; + flush_wq_calls = 0; + park_count = 0; + unpark_count = 0; + idle_undo_calls = 0; + pause_calls = 0; + unpause_calls = 0; + typec_disable_calls = 0; + typec_enable_calls = 0; + hdmi_irq_disable_calls = 0; + hdmi_irq_enable_calls = 0; + fixed_hdmi_reconnect_calls = 0; + stop_crashed_calls = 0; + stop_streams_calls = 0; +} + +static u32 readl(void *addr) +{ + uintptr_t off = (uintptr_t)addr - (uintptr_t)fake_regs; + + assert(off == 0 || off == 4); + return fake_regs[off / 4]; +} + +static void flush_work(struct work_struct *w) +{ + flush_work_calls++; + record(EV_FLUSH_WORK, w->id); +} + +static void flush_workqueue(struct workqueue_struct *wq) +{ + flush_wq_calls++; + record(EV_FLUSH_WQ, wq->id); +} + +static void cancel_delayed_work_sync(struct delayed_work *w) +{ + (void)w; +} + +static void usleep_range(unsigned long min, unsigned long max) +{ + (void)min; + (void)max; +} + +static void msleep(unsigned int ms) +{ + record(EV_MSLEEP, (int)ms); +} + +static int apple_rtkit_poll(struct apple_rtkit *rtk) +{ + (void)rtk; + rtkit_poll_calls++; + record(EV_RTKIT_POLL, 0); + return 0; +} + +static void apple_rtkit_flush_rx(struct apple_rtkit *rtk) +{ + (void)rtk; + rtkit_flush_rx_calls++; + record(EV_RTKIT_FLUSH_RX, 0); +} + +static int iomfb_wait_channels_idle(struct apple_dcp *dcp, unsigned int ms) +{ + (void)dcp; + wait_channels_idle_calls++; + record(EV_CHANNELS_IDLE, (int)ms); + return 0; +} + +static int apple_rtkit_sleep_stop(struct apple_rtkit *rtk, void *control) +{ + int ret = sleep_stop_plan[sleep_stop_calls < 4 ? sleep_stop_calls : 3]; + + (void)rtk; + (void)control; + sleep_stop_calls++; + record(EV_SLEEP_STOP, ret); + if (sleep_stop_calls == 1 && clear_run_on_first_idle) + fake_regs[0] = 0; + if (!ret) { + fake_regs[0] = 0; + fake_regs[1] = 0x6e; + } + return ret; +} + +static struct apple_dcp *active_dcp; + +static int apple_rtkit_sleep_stop_wrap(struct apple_rtkit *rtk, void *control) +{ + int ret = apple_rtkit_sleep_stop(rtk, control); + + if (sleep_stop_calls == 1 && crash_on_first_idle) + active_dcp->crashed = true; + return ret; +} +#define apple_rtkit_sleep_stop apple_rtkit_sleep_stop_wrap + +static bool dcp_is_typec_output(struct apple_dcp *dcp) { return dcp->is_typec; } +static bool dcp_s2r_stops_crashed(struct apple_dcp *dcp) { return dcp->crashed && dcp->rtk; } +static int dcp_s2r_stop_crashed(struct apple_dcp *dcp) +{ + (void)dcp; + stop_crashed_calls++; + record(EV_STOP_CRASHED, 0); + return 0; +} +static void dcp_s2r_stop_failed_start(struct apple_dcp *dcp) { (void)dcp; } +static void dcp_s2r_find_darts(struct apple_dcp *dcp) { (void)dcp; } +static int dcp_s2r_stop_streams(struct apple_dcp *dcp) +{ + (void)dcp; + stop_streams_calls++; + return 0; +} +static void dcp_s2r_restart_failed_stop(struct apple_dcp *dcp) { (void)dcp; } +static void dcp_pmc_forget(struct apple_dcp *dcp) { (void)dcp; } +static void device_set_wakeup_path(struct device *dev) { (void)dev; } +static void dcp_disable_typec_work(struct apple_dcp *dcp, bool sync) +{ + (void)dcp; + (void)sync; + typec_disable_calls++; +} +static void dcp_enable_typec_work(struct apple_dcp *dcp) +{ + (void)dcp; + typec_enable_calls++; + record(EV_TYPEC_ENABLE, 0); +} +static void dcp_s2r_hdmi_irq_disable(struct apple_dcp *dcp) +{ + (void)dcp; + hdmi_irq_disable_calls++; +} +static void dcp_s2r_hdmi_irq_enable(struct apple_dcp *dcp) +{ + (void)dcp; + hdmi_irq_enable_calls++; + record(EV_HDMI_IRQ_ENABLE, 0); +} +static int dcp_s2r_ext_disconnect(struct apple_dcp *dcp) +{ + (void)dcp; + record(EV_DISCONNECT, 0); + return 0; +} +static void dcp_drain_for_sleep(struct apple_dcp *dcp) { (void)dcp; } +static void iomfb_pause_presentation(struct apple_dcp *dcp, bool pause) +{ + (void)dcp; + if (pause) { + pause_calls++; + record(EV_PAUSE_ON, 0); + } else { + unpause_calls++; + record(EV_PAUSE_OFF, 0); + } +} +static void dcp_typec_end_unplugged_for_sleep(struct apple_dcp *dcp) { (void)dcp; } +static int iomfb_queue_settle(struct apple_dcp *dcp, unsigned long timeout) +{ + (void)dcp; + (void)timeout; + record(EV_QUEUE_SETTLE, 0); + return 0; +} +static void dcp_typec_route_park(struct apple_dcp *dcp) +{ + (void)dcp; + park_count++; + record(EV_PARK, 0); +} +static void dcp_typec_route_unpark(struct apple_dcp *dcp) +{ + (void)dcp; + unpark_count++; + record(EV_UNPARK, 0); +} +static void dcp_edr_tick_stop(struct apple_dcp *dcp) { (void)dcp; } +static int dcp_s2r_500(struct apple_dcp *dcp) +{ + (void)dcp; + record(EV_A500, 0); + return 0; +} +static void dcp_s2r_idle_undo(struct apple_dcp *dcp) +{ + (void)dcp; + idle_undo_calls++; + record(EV_IDLE_UNDO, 0); +} +static void dcp_fixed_hdmi_reconnect(struct apple_dcp *dcp, bool poweroff_unplugged) +{ + (void)dcp; + assert(!poweroff_unplugged); + fixed_hdmi_reconnect_calls++; + record(EV_FIXED_HDMI_RECONNECT, 0); +} + +/* Extracted production functions from drivers/gpu/drm/apple/dcp.c */ +%s + +static int find_event_after(size_t start, enum event_kind kind) +{ + for (size_t i = start; i < log_len; i++) { + if (log_events[i].kind == kind) + return (int)i; + } + return -1; +} + +static void init_dcp(struct apple_dcp *dcp, struct device *dev, struct apple_rtkit *rtk, + struct apple_connector *con, struct apple_connector *tcon, + struct apple_dcp_afkep *ep1, struct apple_dcp_afkep *ep2, + struct workqueue_struct *wq1, struct workqueue_struct *wq2) +{ + memset(dcp, 0, sizeof(*dcp)); + con->hotplug_wq.id = 1; + tcon->hotplug_wq.id = 2; + wq1->id = 10; + wq2->id = 20; + ep1->wq = wq1; + ep2->wq = wq2; + + dcp->dev = dev; + dcp->rtk = rtk; + dcp->coproc_reg = (uint8_t *)fake_regs; + dcp->connector = con; + dcp->typec_connector = tcon; + dcp->dptxep = ep1; + dcp->avep = ep2; + dcp->systemep = (struct apple_dcp_afkep *)(uintptr_t)-ENODEV; + active_dcp = dcp; +} + +int main(void) +{ + struct device dev = { 0 }; + struct apple_rtkit rtk = { 0 }; + struct apple_connector con, tcon; + struct apple_dcp_afkep ep1, ep2; + struct workqueue_struct wq1, wq2; + struct gpio_desc hpd = { 0 }; + struct apple_dcp dcp; + int ret; + + /* 1. Clean suspend drains hotplug, RTKit RX, AFK wqs, and IOMFB channels before A500 and idle. */ + reset_mocks(); + init_dcp(&dcp, &dev, &rtk, &con, &tcon, &ep1, &ep2, &wq1, &wq2); + dcp.is_typec = true; + ret = dcp_s2r_ext_suspend(&dcp); + assert(ret == 0); + assert(dcp.s2r_stopped && dcp.s2r_fw_down && !dcp.crashed); + assert(flush_work_calls == 2); + assert(flush_wq_calls == 2); + assert(wait_channels_idle_calls == 2); + assert(sleep_stop_calls == 1); + { + int settle = find_event_after(0, EV_QUEUE_SETTLE); + int pre_ch = find_event_after(settle + 1, EV_CHANNELS_IDLE); + int park = find_event_after(pre_ch + 1, EV_PARK); + int a500 = find_event_after(park + 1, EV_A500); + int post_ch = find_event_after(a500 + 1, EV_CHANNELS_IDLE); + int stop = find_event_after(post_ch + 1, EV_SLEEP_STOP); + assert(settle >= 0 && pre_ch > settle && park > pre_ch && + a500 > park && post_ch > a500 && stop > post_ch); + } + + /* 2. First idle refused (-ETIME), retry after poll+flush+msleep(30) succeeds. */ + reset_mocks(); + init_dcp(&dcp, &dev, &rtk, &con, &tcon, &ep1, &ep2, &wq1, &wq2); + dcp.typec_connector = &con; /* duplicate connector deduplicated */ + sleep_stop_plan[0] = -ETIME; + sleep_stop_plan[1] = 0; + ret = dcp_s2r_ext_suspend(&dcp); + assert(ret == 0); + assert(flush_work_calls == 1); + assert(sleep_stop_calls == 2); + assert(dcp.s2r_stopped && dcp.s2r_fw_down && !dcp.crashed); + assert(idle_undo_calls == 0); + { + int first = find_event_after(0, EV_SLEEP_STOP); + int slp = find_event_after(first + 1, EV_MSLEEP); + int second = find_event_after(slp + 1, EV_SLEEP_STOP); + assert(first >= 0 && slp > first && log_events[slp].arg == 30 && second > slp); + } + + /* + * 3. Type-C external DCP refuses both idles while CPU still runs: + * leave firmware alive (crashed=false), undo AP idle, restore presentation + * and Type-C work, and ensure a second suspend does NOT cold-stop it. + */ + reset_mocks(); + init_dcp(&dcp, &dev, &rtk, &con, &tcon, &ep1, &ep2, &wq1, &wq2); + dcp.is_typec = true; + sleep_stop_plan[0] = -ETIME; + sleep_stop_plan[1] = -ETIME; + ret = dcp_s2r_ext_suspend(&dcp); + assert(ret == -EBUSY); + assert(!dcp.crashed); + assert(!dcp.s2r_stopped && !dcp.s2r_fw_down); + assert(sleep_stop_calls == 2); + assert(unpark_count == 1); + assert(idle_undo_calls == 1); + assert(unpause_calls == 1); + assert(typec_enable_calls == 1); + assert(hdmi_irq_enable_calls == 1); + assert(fixed_hdmi_reconnect_calls == 0); + /* Next cycle must NOT enter dcp_s2r_stop_crashed(). */ + sleep_stop_calls = 0; + sleep_stop_plan[0] = 0; + ret = dcp_s2r_ext_suspend(&dcp); + assert(ret == 0); + assert(stop_crashed_calls == 0); + + /* + * 4. Fixed HDMI-A primary DCP refuses both idles while CPU still runs: + * reconnect fixed HDMI before re-enabling HDMI HPD IRQ, keep live. + */ + reset_mocks(); + init_dcp(&dcp, &dev, &rtk, &con, &tcon, &ep1, &ep2, &wq1, &wq2); + dcp.is_typec = false; + dcp.hdmi_hpd = &hpd; + sleep_stop_plan[0] = -ETIME; + sleep_stop_plan[1] = -ETIME; + ret = dcp_s2r_ext_suspend(&dcp); + assert(ret == -EBUSY); + assert(!dcp.crashed); + assert(idle_undo_calls == 1); + assert(fixed_hdmi_reconnect_calls == 1); + assert(hdmi_irq_enable_calls == 1); + { + int rc = find_event_after(0, EV_FIXED_HDMI_RECONNECT); + int irq = find_event_after(rc + 1, EV_HDMI_IRQ_ENABLE); + assert(rc >= 0 && irq > rc); + } + + /* 5. Real firmware crash during first idle: no retry, mark crashed, keep disabled. */ + reset_mocks(); + init_dcp(&dcp, &dev, &rtk, &con, &tcon, &ep1, &ep2, &wq1, &wq2); + dcp.is_typec = true; + crash_on_first_idle = true; + sleep_stop_plan[0] = -EIO; + ret = dcp_s2r_ext_suspend(&dcp); + assert(ret == -EIO); + assert(dcp.crashed); + assert(sleep_stop_calls == 1); + assert(unpark_count == 1); + assert(idle_undo_calls == 0); + assert(unpause_calls == 0); + assert(typec_enable_calls == 0); + assert(hdmi_irq_enable_calls == 0); + + /* 6. CPU RUN bit cleared on first idle failure: treated as stopped/crashed, no retry. */ + reset_mocks(); + init_dcp(&dcp, &dev, &rtk, &con, &tcon, &ep1, &ep2, &wq1, &wq2); + dcp.is_typec = false; + dcp.hdmi_hpd = &hpd; + clear_run_on_first_idle = true; + sleep_stop_plan[0] = -ETIMEDOUT; + ret = dcp_s2r_ext_suspend(&dcp); + assert(ret == -EBUSY); + assert(dcp.crashed); + assert(sleep_stop_calls == 1); + assert(idle_undo_calls == 0); + assert(fixed_hdmi_reconnect_calls == 0); + assert(hdmi_irq_enable_calls == 0); + + return 0; +} +""" + + +def function(source: str, name: str) -> str: + match = re.search(rf"^(?:static )?[\w\s\*]+ {re.escape(name)}\([^\n]*\n\{{", source, re.M) + assert match, f"missing function {name}" + depth = 0 + for idx in range(match.start(), len(source)): + if source[idx] == "{": + depth += 1 + elif source[idx] == "}": + depth -= 1 + if depth == 0: + return source[match.start() : idx + 1] + raise AssertionError(f"unterminated function {name}") + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--mutation", choices=sorted(MUTATIONS), help="inject a broken mutation") + args = parser.parse_args() + + source = DCP_C.read_text() + extracted = "\n\n".join( + [ + function(source, "dcp_s2r_ext_quiesce_endpoints"), + function(source, "dcp_s2r_ext_idle_stop"), + function(source, "dcp_s2r_ext_suspend"), + ] + ) + if args.mutation: + old, new = MUTATIONS[args.mutation] + assert old in extracted, f"mutation target missing for {args.mutation}" + extracted = extracted.replace(old, new, 1) + + with tempfile.TemporaryDirectory(prefix="dcp-s2r-ext-idle-") as directory: + c_path = Path(directory) / "s2r_ext_idle.c" + bin_path = Path(directory) / "s2r_ext_idle" + c_path.write_text(HARNESS % extracted) + subprocess.run( + ["cc", "-Wall", "-Wextra", "-Werror", "-O2", str(c_path), "-o", str(bin_path)], + check=True, + ) + subprocess.run([str(bin_path)], check=True) + + print( + "PASS dcp_s2r_ext_idle: pre-A500/pre-idle quiesce ordered; single idle retry; " + "live refusal preserves firmware and restores Type-C/HDMI; crash stays latched" + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 24241dd58dea0..b7f14eaf0a005 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -286,6 +286,11 @@ def mutations(flag, names, base, expect=KILL): + mutations('--mutation', ('keep-internal', 'drop-ext-route', 'ignore-ext-output', 'ignore-hdmia', 'ignore-secure-port', 'early-pm-register'), []), + # External s2r_ext quiesce before A500/idle and live-firmware preservation on idle refusal. + 'dcp-s2r-ext-idle-tests.py': [([], PASS)] + + mutations('--mutation', ('skip-quiesce', 'skip-post-a500-idle', 'skip-retry', + 'mark-live-crashed', 'skip-idle-undo', + 'skip-fixed-hdmi-reconnect'), []), # A fixed (non-Type-C) output takes its DP AV service again after a sleep. 'dpavserv-admission-tests.py': [([], PASS)] + mutations('--mutation', ('old-cable-wait', 'ignore-generation', 'ignore-cable', From 93455540e786afce023f6e9610bd8b26834ac80e Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 2 Dec 2026 09:00:03 -0500 Subject: [PATCH 4/6] tools/asahi: let dcp-s2r-ext-idle mutation runs fail at C assert Under run-host-tests.py, killed mutation runs must fail via a runtime assertion without emitting a Python traceback (BROKEN_SIGN). 1. Pass -Wno-unused-function to cc only when --mutation is set so call-deleting mutations (skip-quiesce, skip-idle-undo, skip-fixed-hdmi-reconnect) compile and fail at their C assertions instead of failing compile with -Werror=unused-function. 2. Check the C test binary return code explicitly instead of passing check=True to subprocess.run(), so assertion aborts exit non-zero without printing a Python CalledProcessError traceback. No kernel code changes. Signed-off-by: Satya Benson --- tools/asahi/dcp-s2r-ext-idle-tests.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tools/asahi/dcp-s2r-ext-idle-tests.py b/tools/asahi/dcp-s2r-ext-idle-tests.py index 766cdbdf8e332..8e448194b9c95 100755 --- a/tools/asahi/dcp-s2r-ext-idle-tests.py +++ b/tools/asahi/dcp-s2r-ext-idle-tests.py @@ -607,11 +607,14 @@ def main() -> int: c_path = Path(directory) / "s2r_ext_idle.c" bin_path = Path(directory) / "s2r_ext_idle" c_path.write_text(HARNESS % extracted) - subprocess.run( - ["cc", "-Wall", "-Wextra", "-Werror", "-O2", str(c_path), "-o", str(bin_path)], - check=True, - ) - subprocess.run([str(bin_path)], check=True) + cc_cmd = ["cc", "-Wall", "-Wextra", "-Werror", "-O2"] + if args.mutation: + cc_cmd.append("-Wno-unused-function") + cc_cmd.extend([str(c_path), "-o", str(bin_path)]) + subprocess.run(cc_cmd, check=True) + run = subprocess.run([str(bin_path)]) + if run.returncode != 0: + return run.returncode if run.returncode > 0 else 1 print( "PASS dcp_s2r_ext_idle: pre-A500/pre-idle quiesce ordered; single idle retry; " From 3e291035614d424c52e8160f4ab82eaa5741eda0 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 19:13:32 -0500 Subject: [PATCH 5/6] drm/apple: dcp: rearm the Type-C hotplug wait in dcp_s2r_ext_disconnect() Commit 2a8d8087827f ("drm/apple: dcp: rearm Type-C IOMFB HPD completion on deep-sleep DPTX release") rearms typec_iomfb_hpd_ready in dcp_external_stop_locked(), before the HPD low that releases a Type-C link for deep sleep. dcp_s2r_ext_disconnect() takes the same non-retained pipes past dcp_external_stop_locked() and sets typec_crtc_off without that rearm. The merge of the two is textually clean and drops the fix: dcp_poweron() then finds the completion from before the sleep, does not wait for the hotplug after the wake, and sets the mode before the firmware has described the display again. Rearm the completion where typec_crtc_off is set, before either port's HPD goes low. Check it in dcp-s2r-ext-disconnect-tests.py, with a mutation that drops it: the KUnit case of that commit calls dcp_external_stop_locked(), so it passes either way. Fixes: 5e7dcf85729e ("drm/apple: dcp: fix non-retained firmware-27 s2r_ext link withdrawal") Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/dcp.c | 2 ++ tools/asahi/dcp-s2r-ext-disconnect-tests.py | 33 ++++++++++++++++++--- tools/asahi/run-host-tests.py | 3 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index aa9c3647d6d46..3c3e71c6a38a6 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10877,6 +10877,8 @@ static int dcp_s2r_ext_disconnect(struct apple_dcp *dcp) av_service_disconnect(dcp); if (dcp_is_typec_output(dcp) && READ_ONCE(dcp->typec_cable_connected)) { + /* dcp_poweron() must wait for the hotplug after the wake, not an older one. */ + reinit_completion(&dcp->typec_iomfb_hpd_ready); prev_crtc_off = READ_ONCE(dcp->typec_crtc_off); WRITE_ONCE(dcp->typec_crtc_off, true); set_crtc_off = true; diff --git a/tools/asahi/dcp-s2r-ext-disconnect-tests.py b/tools/asahi/dcp-s2r-ext-disconnect-tests.py index 36aac8d558a4e..68cad147c50a2 100755 --- a/tools/asahi/dcp-s2r-ext-disconnect-tests.py +++ b/tools/asahi/dcp-s2r-ext-disconnect-tests.py @@ -8,7 +8,8 @@ dcpext) have no retained epoch: they must give HPD-low the firmware-27 withdrawal timeout, still call dcp_dptx_disconnect() if HPD-low fails, restore typec_crtc_off on failure, and never latch dcp->external_error so later -suspends can retry cleanly. +suspends can retry cleanly. A Type-C pipe also rearms typec_iomfb_hpd_ready +before HPD goes low: dcp_poweron() waits on it after the wake. --mutation names a production-code defect that the test matrix must kill. """ @@ -43,6 +44,11 @@ "\tif (set_crtc_off)\n\t\tWRITE_ONCE(dcp->typec_crtc_off, prev_crtc_off);\n", "\tif (0 && set_crtc_off)\n\t\tWRITE_ONCE(dcp->typec_crtc_off, prev_crtc_off);\n", ), + # Leave the hotplug completion signaled from before the sleep. + "no-hpd-rearm": ( + "\t\treinit_completion(&dcp->typec_iomfb_hpd_ready);\n\t\tprev_crtc_off =", + "\t\tprev_crtc_off =", + ), # Bypass retained-external routing for H17P. "skip-retained": ( "\tbool set_crtc_off = false;\n\tbool prev_crtc_off = false;\n\tint ret;\n\n\tif (dcp_is_retained_external(dcp))\n\t\treturn dcp_external_disconnect(dcp, false);\n", @@ -91,6 +97,7 @@ def function(text, name): #define scoped_guard(kind, lock) for (int _once = (assert(!*(lock)), *(lock) = 1, 1); _once; *(lock) = 0, _once = 0) struct device { int unused; }; +struct completion { bool done; }; struct apple_epic_service { u32 port; }; struct dptx_port { struct apple_epic_service *service; @@ -107,6 +114,7 @@ def function(text, name): bool typec_output; bool typec_cable_connected; bool typec_crtc_off; + struct completion typec_iomfb_hpd_ready; bool external_stopped; int external_error; }; @@ -119,6 +127,7 @@ def function(text, name): static int hpd_ret_for_port[2]; static int disconnect_ret_for_port[2]; static bool hpd_saw_crtc_off[2]; +static bool hpd_saw_ready[2]; static void note(const char *s) { @@ -138,9 +147,11 @@ def function(text, name): memset(hpd_ret_for_port, 0, sizeof(hpd_ret_for_port)); memset(disconnect_ret_for_port, 0, sizeof(disconnect_ret_for_port)); memset(hpd_saw_crtc_off, 0, sizeof(hpd_saw_crtc_off)); + memset(hpd_saw_ready, 0, sizeof(hpd_saw_ready)); external_disconnect_ret = 0; } +static void reinit_completion(struct completion *c) { c->done = false; } static bool dcp_is_retained_external(struct apple_dcp *dcp) { return dcp->retained_external; } static bool dcp_is_typec_output(struct apple_dcp *dcp) { return dcp->typec_output; } static bool dptx_service_available(struct dptx_port *p) { return p->service != NULL; } @@ -166,6 +177,7 @@ def function(text, name): assert(active_dcp->hpd_mutex == 0); last_hpd_ms[service->port] = timeout_ms; hpd_saw_crtc_off[service->port] = active_dcp->typec_crtc_off; + hpd_saw_ready[service->port] = active_dcp->typec_iomfb_hpd_ready.done; snprintf(buf, sizeof(buf), "H%u", service->port); note(buf); return hpd_ret_for_port[service->port]; @@ -207,11 +219,13 @@ def function(text, name): .retained_external = true, .typec_output = true, .typec_cable_connected = true, + .typec_iomfb_hpd_ready = { .done = true }, .dptxport = { { .service = &svc0, .connected = true } }, }; reset_mocks(&dcp); assert(suspend_disconnect_call(&dcp) == 0); assert(strcmp(trace, "E") == 0); + assert(dcp.typec_iomfb_hpd_ready.done); assert(dcp.external_stopped); assert(dcp.external_error == 0); @@ -222,7 +236,10 @@ def function(text, name): assert(dcp.external_error == -ETIMEDOUT); } - /* 2. Non-retained fw 27.2 Type-C: 15 s HPD-low timeout, typec_crtc_off set first. */ + /* + * 2. Non-retained fw 27.2 Type-C: 15 s HPD-low timeout, typec_crtc_off set and + * typec_iomfb_hpd_ready rearmed first. + */ { struct apple_dcp dcp = { .dev = &dev, @@ -230,6 +247,7 @@ def function(text, name): .avep = &av_tok, .typec_output = true, .typec_cable_connected = true, + .typec_iomfb_hpd_ready = { .done = true }, .dptxport = { { .service = &svc0, .connected = true } }, }; reset_mocks(&dcp); @@ -237,7 +255,9 @@ def function(text, name): assert(strcmp(trace, "AH0D0") == 0); assert(last_hpd_ms[0] == 15000U); assert(hpd_saw_crtc_off[0]); + assert(!hpd_saw_ready[0]); assert(dcp.typec_crtc_off); + assert(!dcp.typec_iomfb_hpd_ready.done); assert(!dcp.dptxport[0].connected); assert(!dcp.external_stopped); assert(dcp.external_error == 0); @@ -297,6 +317,7 @@ def function(text, name): .split = { .active = true }, .typec_output = true, .typec_cable_connected = true, + .typec_iomfb_hpd_ready = { .done = true }, .dptxport = { { .service = &svc0, .connected = true }, { .service = &svc1, .connected = true }, @@ -306,6 +327,7 @@ def function(text, name): assert(suspend_disconnect_call(&dcp) == 0); assert(strcmp(trace, "H1D1H0D0") == 0); assert(last_hpd_ms[1] == 15000U && last_hpd_ms[0] == 15000U); + assert(!hpd_saw_ready[1] && !hpd_saw_ready[0]); assert(!dcp.dptxport[0].connected && !dcp.dptxport[1].connected); } @@ -324,19 +346,21 @@ def function(text, name): assert(!dcp.external_stopped); } - /* 7. Non-Type-C output withdraws link without touching typec_crtc_off. */ + /* 7. Non-Type-C output withdraws link without touching the Type-C state. */ { struct apple_dcp dcp = { .dev = &dev, .fw_compat = DCP_FIRMWARE_V_27_0, .typec_output = false, .typec_cable_connected = false, + .typec_iomfb_hpd_ready = { .done = true }, .dptxport = { { .service = &svc0, .connected = true } }, }; reset_mocks(&dcp); assert(suspend_disconnect_call(&dcp) == 0); assert(strcmp(trace, "H0D0") == 0); assert(!dcp.typec_crtc_off); + assert(dcp.typec_iomfb_hpd_ready.done); assert(!dcp.dptxport[0].connected); } @@ -385,7 +409,8 @@ def main(): sys.exit(1) print("PASS dcp_s2r_ext_disconnect: retained-external preserved; fw27 15 s HPD-low, " - "release after HPD error, no external_error latch, crtc_off restore, split port order") + "release after HPD error, no external_error latch, crtc_off restore, " + "hotplug wait rearmed, split port order") if __name__ == "__main__": diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 5410545d2142f..2428dd341db8e 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -309,7 +309,8 @@ def mutations(flag, names, base, expect=KILL): # Non-retained firmware-27 s2r_ext link withdrawal before deep sleep. 'dcp-s2r-ext-disconnect-tests.py': [([], PASS)] + mutations('--mutation', ('always-external', 'short-timeout', 'abort-on-hpd-err', - 'no-restore-crtc-off', 'skip-retained', 'skip-split'), []), + 'no-restore-crtc-off', 'no-hpd-rearm', 'skip-retained', + 'skip-split'), []), # Primary DCPs with external outputs route to dcp_s2r_ext_capable(). 'dcp-s2r-primary-ext-tests.py': [([], PASS)] + mutations('--mutation', ('keep-internal', 'drop-ext-route', 'ignore-ext-output', From b3b1fc5c06af81e44be7c008cda1f04c73ce8431 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 19:13:33 -0500 Subject: [PATCH 6/6] tools/asahi: dcp-s2r-ext-idle: stub dcp_ext_idle_suspend() dcp_s2r_ext_suspend() on main calls dcp_ext_idle_suspend() first, so the extracted function no longer compiles in the harness. Stub it. Signed-off-by: DjDeveloperr --- tools/asahi/dcp-s2r-ext-idle-tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/asahi/dcp-s2r-ext-idle-tests.py b/tools/asahi/dcp-s2r-ext-idle-tests.py index 8e448194b9c95..9d7f01fe27aa2 100755 --- a/tools/asahi/dcp-s2r-ext-idle-tests.py +++ b/tools/asahi/dcp-s2r-ext-idle-tests.py @@ -296,6 +296,7 @@ #define apple_rtkit_sleep_stop apple_rtkit_sleep_stop_wrap static bool dcp_is_typec_output(struct apple_dcp *dcp) { return dcp->is_typec; } +static int dcp_ext_idle_suspend(struct apple_dcp *dcp) { (void)dcp; return 0; } static bool dcp_s2r_stops_crashed(struct apple_dcp *dcp) { return dcp->crashed && dcp->rtk; } static int dcp_s2r_stop_crashed(struct apple_dcp *dcp) {