diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index b07f3d0043ad9..f62089caf6677 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -1240,6 +1240,7 @@ static void dcp_hdmi_capacity_available(void *ctx) { dcp_typec_pipeline_freed(); dcp_typec_move_starved_locked(); + dcp_fabric_hybrid_kick(ctx, 0); } /* @@ -1272,6 +1273,63 @@ void dcp_fabric_hdmi_unplugged(struct apple_dcp *dcp) dcp_fabric_hdmi_unplugged_locked(dcp); } +static void dcp_hdmi_release_unplugged_locked(struct apple_dcp *dcp, + const char *log_msg) +{ + bool connected; + + lockdep_assert_held(&dcp_typec_fabric_lock); + + if (dcp->active_typec_route) + return; + + connected = dcp->connector && READ_ONCE(dcp->connector->connected); + if (!connected && !READ_ONCE(dcp->dptxport[0].connected)) + return; + + if (!dcp->hdmi_hpd || gpiod_get_value_cansleep(dcp->hdmi_hpd) != 0) + return; + + if (!connected) { + dcp_dptx_disconnect(dcp, 0); + return; + } + + if (log_msg) + dev_info(dcp->dev, "%s\n", log_msg); + dcp_hdmi_disconnect(dcp); + dcp_fabric_hdmi_unplugged_locked(dcp); +} + +/* + * Release a fixed HDMI link on system suspend when the display is gone or + * when a T6030 native output needs the link down for resume-side PHY and + * crossbar reinit. Hold the fabric lock across the ownership check, port + * release and presence update so a Type-C route cannot take the same DPTX + * port during the release. + */ +void dcp_hdmi_suspend(struct apple_dcp *dcp) +{ + guard(mutex)(&dcp_typec_fabric_lock); + + if (dcp->active_typec_route) + return; + + if (dcp->external_native) { + bool unplugged = dcp->connector && + READ_ONCE(dcp->connector->connected) && + dcp->hdmi_hpd && + gpiod_get_value_cansleep(dcp->hdmi_hpd) == 0; + + dcp_hdmi_disconnect(dcp); + if (unplugged) + dcp_fabric_hdmi_unplugged_locked(dcp); + return; + } + + dcp_hdmi_release_unplugged_locked(dcp, NULL); +} + /* * Release a fixed HDMI link unplugged while its HPD IRQ was masked. Hold * the fabric lock from the ownership check through the presence update so @@ -1281,14 +1339,7 @@ void dcp_hdmi_unplugged_in_sleep(struct apple_dcp *dcp) { guard(mutex)(&dcp_typec_fabric_lock); - if (dcp->active_typec_route || !dcp->connector || - !READ_ONCE(dcp->connector->connected) || - gpiod_get_value_cansleep(dcp->hdmi_hpd) != 0) - return; - - dev_info(dcp->dev, "HDMI display unplugged during sleep\n"); - dcp_hdmi_disconnect(dcp); - dcp_fabric_hdmi_unplugged_locked(dcp); + dcp_hdmi_release_unplugged_locked(dcp, "HDMI display unplugged during sleep"); } static void dcp_hdmi_settle_work(struct work_struct *work) diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 4a696e8dc1b7a..c29723448af5a 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10886,12 +10886,7 @@ static int dcp_platform_suspend(struct device *dev) * its PHY and crossbar up again (dcp_fabric_hdmi_reinit()), * which needs the link down, so it is still released. */ - if (!dcp->active_typec_route && - (dcp->external_native || - gpiod_get_value_cansleep(dcp->hdmi_hpd) <= 0)) { - disconnected_hpd_event(dcp->connector); - dcp_dptx_disconnect(dcp, 0); - } + dcp_hdmi_suspend(dcp); } /* * Set the device as a wakeup device, which forces its power diff --git a/drivers/gpu/drm/apple/dcp.h b/drivers/gpu/drm/apple/dcp.h index f2745dde65580..5c583b886e385 100644 --- a/drivers/gpu/drm/apple/dcp.h +++ b/drivers/gpu/drm/apple/dcp.h @@ -200,6 +200,7 @@ bool apple_t8122_25g83_board(void); void dcp_typec_hybrid_first_dt(void); void dcp_fabric_hdmi_unplugged(struct apple_dcp *dcp); +void dcp_hdmi_suspend(struct apple_dcp *dcp); void dcp_hdmi_unplugged_in_sleep(struct apple_dcp *dcp); void dcp_hdmi_disconnect(struct apple_dcp *dcp); void dcp_fabric_hybrid_kick(struct apple_dcp *dcp, unsigned int delay_ms); diff --git a/tools/asahi/dcp-hdmi-sleep-unplug-tests.py b/tools/asahi/dcp-hdmi-sleep-unplug-tests.py index b1ef7a00daa23..6cdfcc3947f50 100755 --- a/tools/asahi/dcp-hdmi-sleep-unplug-tests.py +++ b/tools/asahi/dcp-hdmi-sleep-unplug-tests.py @@ -10,9 +10,12 @@ because its resume sets the PHY and crossbar up again. The fixture compiles the HDMI block of dcp_platform_suspend() and the -fabric's dcp_hdmi_unplugged_in_sleep() against mocks. It checks which -outputs are released and that the fabric lock covers the resume release. ---mutation names a change to the production code that the matrix must kill. +fabric's dcp_hdmi_release_unplugged_locked(), dcp_hdmi_suspend() and +dcp_hdmi_unplugged_in_sleep() against mocks. It checks which outputs are +released, that negative GPIO errors are not treated as unplugged, that +disable_irq() runs outside dcp_typec_fabric_lock, and that the fabric lock +covers both suspend and resume releases. --mutation names a change to the +production code that the matrix must kill. """ import argparse import re @@ -26,21 +29,52 @@ FABRIC = 'drivers/gpu/drm/apple/dcp-fabric.c' RESUME_CALL = '\tif (dcp->hdmi_hpd_irq)\n\t\tdcp_hdmi_unplugged_in_sleep(dcp);\n' +# (target, old, new) where target is 'dcp', 'release', 'suspend', or 'resume' MUTATIONS = { # Keep a T6030 native output's link, which its resume reinit tears down. - 'keep-native': ('(dcp->external_native ||\n\t\t gpiod_get_value_cansleep(dcp->hdmi_hpd) <= 0)', - '(gpiod_get_value_cansleep(dcp->hdmi_hpd) <= 0)'), + 'keep-native': ('suspend', + '\tif (dcp->external_native) {\n', + '\tif (false && dcp->external_native) {\n'), # Release on a failed GPIO read, which cannot establish absence. - 'gpio-error': ('gpiod_get_value_cansleep(dcp->hdmi_hpd) != 0)\n\t\treturn;', - 'gpiod_get_value_cansleep(dcp->hdmi_hpd) > 0)\n\t\treturn;'), + 'gpio-error': ('release', + 'gpiod_get_value_cansleep(dcp->hdmi_hpd) != 0)', + 'gpiod_get_value_cansleep(dcp->hdmi_hpd) > 0)'), # Release the HDMI connector while a Type-C display owns the pipeline. - 'ignore-borrowed': ('if (dcp->active_typec_route || !dcp->connector ||', - 'if (!dcp->connector ||'), + 'ignore-borrowed': ('release', + '\tif (dcp->active_typec_route)\n\t\treturn;\n', + ''), + # Release a borrowed T6030 native pipeline at suspend. + 'suspend-ignore-borrowed': ('suspend', + '\tif (dcp->active_typec_route)\n\t\treturn;\n', + ''), # Leave the hybrid fabric thinking HDMI is still there. - 'no-fabric': ('\tdcp_fabric_hdmi_unplugged_locked(dcp);\n', ''), - 'no-fabric-lock': ('\tguard(mutex)(&dcp_typec_fabric_lock);\n', ''), + 'no-fabric': ('release', + '\tdcp_fabric_hdmi_unplugged_locked(dcp);\n', + ''), + # Leave the hybrid fabric stale when an unplugged T6030 native HDMI suspends. + 'no-native-unplug-fabric': ('suspend', + '\t\tif (unplugged)\n\t\t\tdcp_fabric_hdmi_unplugged_locked(dcp);\n', + '\t\t(void)unplugged;\n'), + # Treat a GPIO error as unplugged on a T6030 native output at suspend. + 'native-unplug-gpio-error': ('suspend', + 'gpiod_get_value_cansleep(dcp->hdmi_hpd) == 0;', + 'gpiod_get_value_cansleep(dcp->hdmi_hpd) <= 0;'), + # Drop the fabric lock on resume. + 'no-fabric-lock': ('resume', + '\tguard(mutex)(&dcp_typec_fabric_lock);\n', + ''), + # Drop the fabric lock on suspend. + 'no-suspend-fabric-lock': ('suspend', + '\tguard(mutex)(&dcp_typec_fabric_lock);\n', + ''), # Never look for an unplug after the sleep. - 'no-resume-check': (RESUME_CALL, ''), + 'no-resume-check': ('dcp', RESUME_CALL, ''), + # Never release an unplugged or native HDMI link at suspend. + 'no-suspend-check': ('dcp', '\t\tdcp_hdmi_suspend(dcp);\n', ''), + # Leave a stale DPTX link connected when HPD is low without a DRM connector. + 'no-stale-dptx-release': ('release', + '\tif (!connected) {\n\t\tdcp_dptx_disconnect(dcp, 0);\n\t\treturn;\n\t}\n', + '\tif (!connected)\n\t\treturn;\n'), } def block(text, start): @@ -67,20 +101,38 @@ def function(text, name): struct device { int unused; }; struct gpio_desc { int level; }; struct apple_connector { bool connected; }; +struct dptx_port { bool connected; }; struct apple_dcp { struct device *dev; void *active_typec_route; struct apple_connector *connector; + struct dptx_port dptxport[1]; struct gpio_desc *hdmi_hpd; int hdmi_hpd_irq; bool external_native; }; static unsigned hpd_events, disconnects, fabric_unplugs, irq_off; -static int gpiod_get_value_cansleep(struct gpio_desc *g) { return g->level; } -static void disable_irq(int irq) { irq_off++; } -static void disconnected_hpd_event(struct apple_connector *c) { c->connected = false; hpd_events++; } -static int dcp_dptx_disconnect(struct apple_dcp *d, int port) { disconnects++; return 0; } static int dcp_typec_fabric_lock; +static int gpiod_get_value_cansleep(struct gpio_desc *g) { return g->level; } +static void disable_irq(int irq) +{ + assert(!dcp_typec_fabric_lock); + irq_off++; +} +static void disconnected_hpd_event(struct apple_connector *c) +{ + assert(dcp_typec_fabric_lock); + if (c) + c->connected = false; + hpd_events++; +} +static int dcp_dptx_disconnect(struct apple_dcp *d, int port) +{ + assert(dcp_typec_fabric_lock); + d->dptxport[port].connected = false; + disconnects++; + return 0; +} static int *hold_lock(int *lock) { assert(!*lock); @@ -89,6 +141,7 @@ def function(text, name): } static void release_lock(int **lock) { **lock = 0; } #define guard(kind) int *held_##kind __attribute__((cleanup(release_lock))) = hold_lock +#define lockdep_assert_held(lock) assert(*(lock)) static void dcp_hdmi_disconnect(struct apple_dcp *dcp) { assert(dcp_typec_fabric_lock); @@ -118,15 +171,17 @@ def function(text, name): int route; #define SETUP(name, L, N) do { case_name = name; reset(); con.connected = true; \ - hpd.level = L; dcp = (struct apple_dcp){ .connector = &con, .hdmi_hpd = &hpd, \ + hpd.level = L; dcp = (struct apple_dcp){ .connector = &con, \ + .dptxport = { { .connected = true } }, .hdmi_hpd = &hpd, \ .hdmi_hpd_irq = 7, .external_native = N }; } while (0) /* Attached through the sleep: kept, lit again on wake. */ SETUP("kept", 1, false); suspend_hdmi(&dcp); - CHECK(irq_off == 1 && con.connected && !disconnects); + CHECK(irq_off == 1 && con.connected && !disconnects && !fabric_unplugs && + !dcp_typec_fabric_lock); dcp_hdmi_unplugged_in_sleep(&dcp); - CHECK(con.connected && !disconnects && !fabric_unplugs); + CHECK(con.connected && !disconnects && !fabric_unplugs && !dcp_typec_fabric_lock); /* Unplugged during the sleep: released on resume, fabric told. */ SETUP("unplugged-in-sleep", 1, false); @@ -135,19 +190,43 @@ def function(text, name): dcp_hdmi_unplugged_in_sleep(&dcp); CHECK(!con.connected && hpd_events == 1 && disconnects == 1 && fabric_unplugs == 1); - /* Gone before the sleep: released at suspend, nothing on resume. */ + /* Gone before the sleep: released at suspend and fabric told, nothing on resume. */ SETUP("gone-before", 0, false); suspend_hdmi(&dcp); - CHECK(!con.connected && disconnects == 1); + CHECK(!con.connected && hpd_events == 1 && disconnects == 1 && fabric_unplugs == 1); dcp_hdmi_unplugged_in_sleep(&dcp); - CHECK(disconnects == 1 && hpd_events == 1 && !fabric_unplugs); + CHECK(disconnects == 1 && hpd_events == 1 && fabric_unplugs == 1); - /* A failed GPIO read cannot establish absence. */ + /* Already disconnected and idle: untouched, no spurious fabric unplug hold. */ + SETUP("already-disconnected", 0, false); + con.connected = false; + dcp.dptxport[0].connected = false; + suspend_hdmi(&dcp); + CHECK(!con.connected && !hpd_events && !disconnects && !fabric_unplugs); + dcp_hdmi_unplugged_in_sleep(&dcp); + CHECK(!hpd_events && !disconnects && !fabric_unplugs); + + /* Stale DPTX link without a connected DRM connector: port released, no fabric unplug. */ + SETUP("stale-dptx", 0, false); + con.connected = false; + suspend_hdmi(&dcp); + CHECK(!dcp.dptxport[0].connected && !hpd_events && disconnects == 1 && !fabric_unplugs); + dcp_hdmi_unplugged_in_sleep(&dcp); + CHECK(disconnects == 1 && !hpd_events && !fabric_unplugs); + + /* A failed GPIO read at suspend cannot establish absence. */ + SETUP("suspend-gpio-error", -5, false); + suspend_hdmi(&dcp); + CHECK(irq_off == 1 && con.connected && !hpd_events && !disconnects && !fabric_unplugs); + dcp_hdmi_unplugged_in_sleep(&dcp); + CHECK(con.connected && !hpd_events && !disconnects && !fabric_unplugs); + + /* A failed GPIO read at resume cannot establish absence. */ SETUP("gpio-error", 1, false); suspend_hdmi(&dcp); hpd.level = -5; dcp_hdmi_unplugged_in_sleep(&dcp); - CHECK(con.connected && !disconnects); + CHECK(con.connected && !disconnects && !fabric_unplugs); /* A Type-C display owns the pipeline: the HDMI side is not touched. */ SETUP("borrowed", 0, false); @@ -156,18 +235,39 @@ def function(text, name): dcp_hdmi_unplugged_in_sleep(&dcp); CHECK(con.connected && !disconnects && !hpd_events && !fabric_unplugs); - /* T6030 native output: released at suspend even while attached. */ + /* A Type-C display owns a T6030 native pipeline: HDMI suspend leaves it alone. */ + SETUP("borrowed-native", 1, true); + dcp.active_typec_route = &route; + suspend_hdmi(&dcp); + dcp_hdmi_unplugged_in_sleep(&dcp); + CHECK(con.connected && !disconnects && !hpd_events && !fabric_unplugs); + + /* T6030 native output attached: released at suspend for reinit, not marked unplugged. */ SETUP("native", 1, true); suspend_hdmi(&dcp); - CHECK(!con.connected && disconnects == 1); + CHECK(!con.connected && disconnects == 1 && !fabric_unplugs); + dcp_hdmi_unplugged_in_sleep(&dcp); + CHECK(disconnects == 1 && !fabric_unplugs); + + /* T6030 native output unplugged before sleep: released and fabric told at suspend. */ + SETUP("native-unplugged", 0, true); + suspend_hdmi(&dcp); + CHECK(!con.connected && disconnects == 1 && fabric_unplugs == 1); dcp_hdmi_unplugged_in_sleep(&dcp); - CHECK(disconnects == 1); + CHECK(disconnects == 1 && fabric_unplugs == 1); + + /* T6030 native output with GPIO error: released for reinit, not marked unplugged. */ + SETUP("native-gpio-error", -5, true); + suspend_hdmi(&dcp); + CHECK(!con.connected && disconnects == 1 && !fabric_unplugs); + dcp_hdmi_unplugged_in_sleep(&dcp); + CHECK(disconnects == 1 && !fabric_unplugs); if (failures) return 1; puts("PASS HDMI sleep: attached kept, unplugged-in-sleep released on resume, " - "gone-before released at suspend, GPIO error and borrowed pipeline untouched, " - "T6030 native released"); + "gone-before released and fabric notified at suspend, GPIO errors and borrowed " + "pipelines untouched, T6030 native released"); return 0; } ''' @@ -178,17 +278,19 @@ def main(): args = parser.parse_args() source = (ROOT / DCP).read_text() - unplug = function((ROOT / FABRIC).read_text(), 'dcp_hdmi_unplugged_in_sleep') + fabric = (ROOT / FABRIC).read_text() + parts = { + 'dcp': source, + 'release': function(fabric, 'dcp_hdmi_release_unplugged_locked'), + 'suspend': function(fabric, 'dcp_hdmi_suspend'), + 'resume': function(fabric, 'dcp_hdmi_unplugged_in_sleep'), + } if args.mutation: - old, new = MUTATIONS[args.mutation] - if old in unplug: - assert unplug.count(old) == 1, args.mutation - unplug = unplug.replace(old, new) - else: - assert source.count(old) == 1, args.mutation - source = source.replace(old, new) - - resume = function(source, 'dcp_platform_resume') + target, old, new = MUTATIONS[args.mutation] + assert parts[target].count(old) == 1, args.mutation + parts[target] = parts[target].replace(old, new) + + resume = function(parts['dcp'], 'dcp_platform_resume') # The release runs while the HPD IRQ is still masked: an edge from then # on is replayed when dcp_fabric_run_resume() enables it. if RESUME_CALL not in resume or \ @@ -196,9 +298,10 @@ def main(): print('FAIL line 0 (resume: no unplugged-in-sleep release before the HPD IRQ comes back)') sys.exit(1) - suspend = function(source, 'dcp_platform_suspend') + suspend = function(parts['dcp'], 'dcp_platform_suspend') hdmi = block(suspend, suspend.index('\tif (dcp->hdmi_hpd_irq) {\n\t\tdisable_irq')) - code = HARNESS + unplug + CHECKS.replace('SUSPEND', hdmi) + code = (HARNESS + parts['release'] + '\n' + parts['suspend'] + '\n' + + parts['resume'] + CHECKS.replace('SUSPEND', hdmi)) with tempfile.TemporaryDirectory() as directory: path = Path(directory) / 'check.c' diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index a3c33382e0fb1..548ae1b1edcc6 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -219,8 +219,12 @@ def mutations(flag, names, base, expect=KILL): + mutations('--mutation', ('no-route', 'no-own-phy', 'class-device', 'no-err-check'), []), # An HDMI display kept through a sleep but unplugged during it is released on resume. 'dcp-hdmi-sleep-unplug-tests.py': [([], PASS)] - + mutations('--mutation', ('keep-native', 'gpio-error', 'ignore-borrowed', 'no-fabric', - 'no-fabric-lock', 'no-resume-check'), []), + + mutations('--mutation', ('keep-native', 'gpio-error', 'ignore-borrowed', + 'suspend-ignore-borrowed', 'no-fabric', + 'no-native-unplug-fabric', 'native-unplug-gpio-error', + 'no-fabric-lock', 'no-suspend-fabric-lock', + 'no-resume-check', 'no-suspend-check', + 'no-stale-dptx-release'), []), # 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',