diff --git a/drivers/gpu/drm/apple/dcp-fabric-topology-test.c b/drivers/gpu/drm/apple/dcp-fabric-topology-test.c index f1c62a6f010ae..fef6d8b8d6ff4 100644 --- a/drivers/gpu/drm/apple/dcp-fabric-topology-test.c +++ b/drivers/gpu/drm/apple/dcp-fabric-topology-test.c @@ -462,6 +462,8 @@ static struct topology_rgb8 *topology_rgb8_init(struct kunit *test) if (i != RGB8_NODATA) platform_set_drvdata(t->pdev[i], &t->dcp[i]); } + /* A 10-bit pipeline that is not on the default method set */ + t->dcp[RGB8_PLAIN].hw.iomfb_method_profile = DCP_IOMFB_METHODS_H17G; t->dcp[RGB8_EXT26].hw.iomfb_method_profile = DCP_IOMFB_METHODS_J613_EXT26; /* An ext26 method set that cannot be reached: no drvdata */ t->dcp[RGB8_NODATA].hw.iomfb_method_profile = DCP_IOMFB_METHODS_J613_EXT26; @@ -491,6 +493,9 @@ static void topology_rgb8_only_test(struct kunit *test) struct platform_device *nodata_first[] = { t->pdev[RGB8_NODATA], t->pdev[RGB8_PLAIN], t->pdev[RGB8_EXT26], }; + struct platform_device *ext26_first[] = { + t->pdev[RGB8_EXT26], t->pdev[RGB8_PLAIN], t->pdev[RGB8_NODATA], + }; struct platform_device *plain = t->pdev[RGB8_PLAIN]; struct platform_device *ext26 = t->pdev[RGB8_EXT26]; struct platform_device *nodata = t->pdev[RGB8_NODATA]; @@ -505,6 +510,7 @@ static void topology_rgb8_only_test(struct kunit *test) /* Any pipeline: an ext26 candidate after a 10-bit one */ KUNIT_EXPECT_TRUE(test, apple_typec_port_rgb8_only(all, 3, NULL, both)); KUNIT_EXPECT_TRUE(test, apple_typec_port_rgb8_only(nodata_first, 3, NULL, both)); + KUNIT_EXPECT_TRUE(test, apple_typec_port_rgb8_only(ext26_first, 3, NULL, both)); /* One pipeline: only its own method set counts */ KUNIT_EXPECT_FALSE(test, apple_typec_port_rgb8_only(all, 3, plain, both)); diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index 22148f031868f..33c7da16d6605 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -2259,7 +2259,6 @@ static int dcp_typec_route_apply(struct typec_mux_dev *mux, * A T8132 dcpext has no connector of its own: it connects * from the Type-C reconnect work, as for a tunnel. */ - dcp->typec_reconnect_tries = 0; dcp_queue_typec_reconnect(dcp, 0); } } else if (hpd && dp_data && (dp_data->status & DP_STATUS_IRQ_HPD)) { @@ -3160,7 +3159,6 @@ static void dcp_tb_queue_reconnect(void *data) struct dcp_tb_attach_context *ctx = data; struct apple_dcp *dcp = ctx->best->dcp; - dcp->typec_reconnect_tries = 0; dcp_queue_typec_reconnect(dcp, 0); } @@ -4194,7 +4192,6 @@ static int dcp_follow_attach(void *data, unsigned int index, bool restore) dcp->typec_generation++; dcp->typec_follow_start = true; dcp->typec_follow_gen = dcp->typec_generation; - dcp->typec_reconnect_tries = 0; dcp->placeholder_retried = false; reinit_completion(&dcp->typec_iomfb_hpd_ready); WRITE_ONCE(dcp->typec_crtc_off, true); diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 81de9acc87461..c0efe84a3067d 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1214,7 +1214,8 @@ struct apple_dcp { /* IOMFB reports its video interface ready after DPTX link training. */ struct completion typec_iomfb_hpd_ready; struct delayed_work typec_reconnect_wq; - u32 typec_reconnect_tries; + u32 typec_reconnect_tries; /* hpd_mutex */ + u32 typec_reconnect_generation; /* hpd_mutex; independent of the fabric session */ struct dcp_fabric_session typec_reconnect_session; /* hpd_mutex */ /* hpd_mutex: what DRM reports for the external display, and when */ struct dcp_announce announce; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 6abd5cc9fda60..16c1fa28afe65 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -2583,6 +2583,9 @@ static void dcp_queue_typec_reconnect_locked(struct apple_dcp *dcp, return; dcp->typec_reconnect_session = dcp_session_locked(dcp); } + /* Each request owns its budget; retries re-arm the work directly. */ + dcp->typec_reconnect_generation++; + dcp->typec_reconnect_tries = 0; mod_delayed_work(system_freezable_wq, &dcp->typec_reconnect_wq, delay); } @@ -3188,7 +3191,6 @@ void dcp_external_ready(struct apple_dcp *dcp) { if (READ_ONCE(dcp->typec_cable_connected)) { guard(mutex)(&dcp->hpd_mutex); - dcp->typec_reconnect_tries = 0; dcp_queue_typec_reconnect_locked(dcp, 0); } else if (dcp->hdmi_hpd && dcp->active && gpiod_get_value_cansleep(dcp->hdmi_hpd)) { @@ -3321,7 +3323,6 @@ void dcp_external_retry_work(struct work_struct *work) if (ret) return; } - dcp->typec_reconnect_tries = 0; } guard(mutex)(&dcp->hpd_mutex); dcp_queue_typec_reconnect_locked(dcp, 0); @@ -3921,6 +3922,8 @@ static void dcp_typec_reconnect_work(struct work_struct *work) container_of(to_delayed_work(work), struct apple_dcp, typec_reconnect_wq); struct dcp_fabric_session session; + u32 generation; + unsigned int tries, limit; int ret; /* @@ -3938,12 +3941,19 @@ static void dcp_typec_reconnect_work(struct work_struct *work) READ_ONCE(dcp->typec_follow_retiring)) return; + scoped_guard(mutex, &dcp->hpd_mutex) { + /* A newer request is queued; leave its deadline and budget alone. */ + if (delayed_work_pending(&dcp->typec_reconnect_wq)) + return; + generation = dcp->typec_reconnect_generation; + session = dcp->typec_reconnect_session; + } + if (dcp_typec_sessionless(dcp)) { ret = dcp_dptx_connect(dcp, 0); } else { /* Only the session this work was queued for may connect. */ scoped_guard(mutex, &dcp->hpd_mutex) { - session = dcp->typec_reconnect_session; if (!dcp_session_valid_locked(dcp, session, true)) return; } @@ -3957,25 +3967,28 @@ static void dcp_typec_reconnect_work(struct work_struct *work) return; } } - if (!ret) { - dcp->typec_reconnect_tries = 0; + if (!ret) return; - } /* A display held for this link will not get ready. */ if (READ_ONCE(dcp->external_error)) dcp_announce_kick(dcp); /* A T8132 dcpext connects once per tunnel. */ - if (++dcp->typec_reconnect_tries < - (dcp_uses_t6020_tunnel_flow(dcp) || dcp_is_t8132_ext(dcp) ? - 1 : DPTX_RECONNECT_RETRIES)) { - mod_delayed_work(system_freezable_wq, &dcp->typec_reconnect_wq, - DPTX_RECONNECT_DELAY); - return; + limit = dcp_uses_t6020_tunnel_flow(dcp) || dcp_is_t8132_ext(dcp) ? + 1 : DPTX_RECONNECT_RETRIES; + scoped_guard(mutex, &dcp->hpd_mutex) { + if (generation != dcp->typec_reconnect_generation) + return; + tries = ++dcp->typec_reconnect_tries; + if (tries < limit) { + mod_delayed_work(system_freezable_wq, &dcp->typec_reconnect_wq, + DPTX_RECONNECT_DELAY); + return; + } } - dev_err(dcp->dev, "Type-C DPTX reconnect failed after %u retries: %d\n", - dcp->typec_reconnect_tries, ret); + dev_err(dcp->dev, "Type-C DPTX reconnect failed after %u attempts: %d\n", + tries, ret); } /** @@ -4022,7 +4035,6 @@ static void dcp_typec_attach_locked(struct apple_dcp *dcp) dcp->typec_generation++; WRITE_ONCE(dcp->typec_cable_connected, true); dcp_firmware_metadata_connection(dcp, dcp->typec_generation, true); - dcp->typec_reconnect_tries = 0; dcp->placeholder_retried = false; cancel_delayed_work(&dcp->typec_reconnect_wq); /* A newly attached display gets its own retries. */ @@ -5641,7 +5653,6 @@ void dcp_poweron(struct platform_device *pdev) */ if (READ_ONCE(dcp->typec_cable_connected)) { cancel_delayed_work(&dcp->typec_reconnect_wq); - dcp->typec_reconnect_tries = 0; /* the modeset of this commit follows, see dcpep_cb_hotplug() */ WRITE_ONCE(dcp->typec_poweron_hpd, true); ret = dcp_dptx_connect(dcp, 0); diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py index 3941f8bd0aebb..c58d11b8038aa 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py @@ -5,6 +5,156 @@ import re from common import block, apple_function, atc_set_mode_prototypes, internal_inline, atc_mux_without_pm, source_defines, ATC_POST_TUNABLE, tipd_pm_header, firmware_lineage, atc_restore_helpers, atc_cio_helpers, atc_cfg_helpers +def reconnect(root): + source = (root / 'drivers/gpu/drm/apple/dcp.c').read_text() + defines = '\n'.join(line for line in source.splitlines() + if line.startswith('#define DPTX_RECONNECT_')) + production = '\n'.join(block(source, marker) for marker in ( + 'static void dcp_queue_typec_reconnect_locked(', + 'static void dcp_typec_reconnect_work(')) + return r''' +#include +#include +#include +#include +#include +#include +typedef uint32_t u32; +struct work_struct { int unused; }; +struct delayed_work { struct work_struct work; bool pending; unsigned long delay; }; +struct dcp_fabric_session { unsigned id; }; +struct apple_dcp { + int hpd_mutex; void *dev; bool crashed, external, typec_cable_connected; + bool typec_follow_retiring, external_error, sessionless, tunnel, t8132; + u32 typec_reconnect_generation, typec_reconnect_tries; + struct dcp_fabric_session typec_reconnect_session; + struct delayed_work typec_reconnect_wq; +}; +struct lock_guard { int *lock; bool run; }; +static struct lock_guard take_lock(int *lock) +{ assert(!*lock); *lock = 1; return (struct lock_guard){lock, true}; } +static void drop_lock(struct lock_guard *g) { assert(*g->lock); *g->lock = 0; } +#define scoped_guard(kind, lock) \ + for (struct lock_guard g __attribute__((cleanup(drop_lock))) = take_lock(lock); \ + g.run; g.run = false) +#define lockdep_assert_held(lock) assert(*(lock)) +#define READ_ONCE(x) (x) +#define container_of(p, type, member) ((type *)((char *)(p) - offsetof(type, member))) +#define to_delayed_work(p) container_of(p, struct delayed_work, work) +#define delayed_work_pending(w) ((w)->pending) +#define msecs_to_jiffies(x) (x) +#define system_freezable_wq 0 +#define dcp_typec_sessionless(d) ((d)->sessionless) +#define dcp_j714s_ext(d) false +#define dcpext_scanout_terminal(d) false +#define dcp_is_t8132_ext(d) ((d)->t8132) +#define dcp_uses_t6020_tunnel_flow(d) ((d)->tunnel) +#define dev_warn(...) ((void)0) +static unsigned calls, errors, kicks, tiles; +static bool replace, stale; +static int connect_result; +#define dev_err(...) ((void)++errors) +#define dcp_announce_kick(d) ((void)++kicks) +static struct dcp_fabric_session dcp_session_locked(struct apple_dcp *d) +{ assert(d->hpd_mutex); return (struct dcp_fabric_session){1}; } +static bool dcp_session_valid_locked(struct apple_dcp *d, + struct dcp_fabric_session s, bool cable) +{ assert(d->hpd_mutex && cable); return !stale && s.id == 1; } +static void mod_delayed_work(int queue, struct delayed_work *w, unsigned long delay) +{ (void)queue; w->pending = true; w->delay = delay; } +static void dcp_queue_typec_reconnect_locked(struct apple_dcp *, unsigned long); +static int dcp_dptx_connect(struct apple_dcp *d, unsigned port) +{ + assert(!d->hpd_mutex && port == 0); calls++; + if (replace) { + replace = false; + scoped_guard(mutex, &d->hpd_mutex) { dcp_queue_typec_reconnect_locked(d, 0); } + } + return connect_result; +} +static int dcp_dptx_connect_raw(struct apple_dcp *d, unsigned port, void *unused, + struct dcp_fabric_session *s, bool cable, bool recovery) +{ assert(!unused && s->id == 1 && cable && !recovery); return dcp_dptx_connect(d, port); } +static int dcp_dptx_connect_tile(struct apple_dcp *d) +{ assert(!d->hpd_mutex); tiles++; return 0; } +''' + defines + '\n' + production + r''' +static void request(struct apple_dcp *d) +{ + scoped_guard(mutex, &d->hpd_mutex) { dcp_queue_typec_reconnect_locked(d, 0); } +} +static void run(struct apple_dcp *d) +{ + assert(d->typec_reconnect_wq.pending); + d->typec_reconnect_wq.pending = false; + dcp_typec_reconnect_work(&d->typec_reconnect_wq.work); + assert(!d->hpd_mutex); +} +int main(void) +{ + for (unsigned sessionless = 0; sessionless < 2; sessionless++) { + for (unsigned flow = 0; flow < 3; flow++) { + struct apple_dcp d = {.sessionless = sessionless, .tunnel = flow == 1, + .t8132 = flow == 2, .typec_cable_connected = true}; + unsigned limit = flow ? 1 : DPTX_RECONNECT_RETRIES; + calls = errors = kicks = tiles = 0; + connect_result = -ETIMEDOUT; + request(&d); + for (unsigned n = 1; n <= limit; n++) { + run(&d); + assert(d.typec_reconnect_tries == n); + assert(d.typec_reconnect_wq.pending == (n < limit)); + if (n < limit) assert(d.typec_reconnect_wq.delay == DPTX_RECONNECT_DELAY); + } + assert(calls == limit && errors == 1); + request(&d); + assert(d.typec_reconnect_tries == 0); + + /* A dispatched old worker must not adopt a replacement already pending. */ + dcp_typec_reconnect_work(&d.typec_reconnect_wq.work); + assert(calls == limit && d.typec_reconnect_tries == 0); + assert(d.typec_reconnect_wq.pending && d.typec_reconnect_wq.delay == 0); + + /* Replace the request during connect without changing its fabric session. */ + replace = true; + run(&d); + assert(calls == limit + 1 && errors == 1 && d.typec_reconnect_tries == 0); + assert(d.typec_reconnect_wq.pending && d.typec_reconnect_wq.delay == 0); + for (unsigned n = 0; n < limit; n++) run(&d); + assert(calls == 2 * limit + 1 && errors == 2); + + /* Success issues the same calls and leaves a newer request untouched. */ + request(&d); + connect_result = 0; + unsigned before = calls, before_tiles = tiles; + run(&d); + assert(calls == before + 1 && tiles == before_tiles + !sessionless); + assert(!d.typec_reconnect_wq.pending && d.typec_reconnect_tries == 0); + assert(errors == 2 && kicks == 0); + request(&d); + replace = true; + run(&d); + assert(d.typec_reconnect_wq.pending && d.typec_reconnect_wq.delay == 0); + assert(d.typec_reconnect_tries == 0 && errors == 2); + run(&d); + assert(!d.typec_reconnect_wq.pending); + + if (!sessionless) { + request(&d); + stale = true; + before = calls; + run(&d); + assert(calls == before && errors == 2); + stale = false; + } + printf("PASS: sessionless=%u flow=%u budget, overlap, pending, success\n", + sessionless, flow); + } + } + return 0; +} +''' + + def disconnect(root): # Copyright 2026 Ryan Murray diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/run.py b/tools/testing/selftests/drivers/gpu/apple-dcp/run.py index 2d111c5bed964..8b8b5f3ff0b15 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/run.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/run.py @@ -56,6 +56,7 @@ def main(): 'orientation-faults', 'backend-change', 'safe-first', 'failed-flip-return', 'live-park')) tests = [ + ('lifecycle', 'typec-reconnect-budget', lifecycle.reconnect, ((),), ()), ('display', 'panel-colorimetry', lambda r: colorimetry.panel(r, output), ((),), ('-Wno-unused-function', '-Wno-unused-const-variable')), ('audio', 'compact-audio-reply-bounds', audio.reply_bounds, ((),), ()), ('audio', 'cec-only-av-endpoint', audio.cec_only_endpoint, ((),), ()),