From fa7920d06eabde8d7e8f15211b62800d9eee8d50 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 22:00:43 -0500 Subject: [PATCH 1/2] drm/apple: m4: keep the DPTX ports across an external DCP's stop A monitor plugged into a T8132 Mac on firmware 26.6.2 more than 5 s after boot stays dark, and its external DCP is lost until a reboot: external DCP restart failed at DPTX ready: -110 Retained DCP session quarantined: -110 The wake from the runtime idle stop takes the CPU out of reset, withdraws both DPTX ports and then waits 1 s for the firmware to announce them again. The firmware does not: apple_rtkit_wake_retained() restarts the image it kept, with no INIT and no endpoint start, so its AFK endpoints and the services on them are the ones of before the stop. On a J713 with 26.6.2 no AFK message arrives and the wait times out. That the firmware keeps the ports was known: one left connected across the stop is still connected when the CPU restarts, see dcp_t8132_sleep_release(). And until the idle stop was added, a port that was announced again could not bind, as dptx_service_bind() refuses a port that has a service. All the 26.6.2 deep sleeps that brought an external display back did so over the service of before the sleep. A deep sleep stops and wakes the DCP with the same two functions, so the same wait fails the wake of every deep sleep with a live external DCP. s2idle leaves the firmware running and is not affected. Keep the ports published over the stop and wait for nothing at the wake, as before. What may use a stopped DCP is already fenced: AFK transmission is closed until the wake is done, and a route wakes the DCP before its first DPTX call. Nothing needs to bind a second service to a port, or a second service to an interface, so remove the code that did: rebind_allowed, dptx_service_rebind(), compact_rebind_gen and compact_start_err. Receive words held back over the wake are still replayed, and calls left over from an earlier teardown still dropped. The new m4-ext-wake test runs the stop and both wakes with no announcement. Fixes: e2b3bb83b459 ("drm/apple: m4: power down idle external DCPs at runtime") Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk.c | 28 +--- drivers/gpu/drm/apple/afk.h | 2 - drivers/gpu/drm/apple/dptx-service.h | 13 +- drivers/gpu/drm/apple/dptxep.c | 9 +- drivers/gpu/drm/apple/dptxep.h | 2 - drivers/gpu/drm/apple/iomfb_m4.c | 99 ++------------ tools/asahi/afk-compact-start-tests.py | 19 +-- tools/asahi/dcpext26-startup-tests.py | 4 +- .../drivers/gpu/apple-afkv2/test-admission.py | 2 +- .../selftests/drivers/gpu/apple-dcp/m4.py | 121 ++++++++++++++++++ .../selftests/drivers/gpu/apple-dcp/run.py | 1 + 11 files changed, 151 insertions(+), 149 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 643781f3ad029..7464beae74175 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -1127,24 +1127,11 @@ static void afk_recv_handle_compact_init(struct apple_dcp_afkep *ep, ep->endpoint); return; } - service = afk_epic_find_service(ep, channel); - if (service) { - u32 gen = READ_ONCE(ep->dcp->afk_power_gen); - - if (service->power_gen == gen || - READ_ONCE(ep->compact_rebind_gen) != gen) { - dev_warn(ep->dcp->dev, - "AFK[ep:%02x]: duplicate compact service on interface %u\n", - ep->endpoint, channel); - return; - } - dev_info(ep->dcp->dev, - "AFK[ep:%02x]: retiring stale compact service %s on interface %u\n", - ep->endpoint, service->name, channel); - afk_remove_service_debugfs(service); - afk_service_retire(service); - if (service->ops->teardown) - service->ops->teardown(service); + if (afk_epic_find_service(ep, channel)) { + dev_warn(ep->dcp->dev, + "AFK[ep:%02x]: duplicate compact service on interface %u\n", + ep->endpoint, channel); + return; } memcpy(wire_name, payload, AFK_COMPACT_NAME_LEN); wire_name[AFK_COMPACT_NAME_LEN] = '\0'; @@ -1206,12 +1193,7 @@ static void afk_recv_handle_compact_init(struct apple_dcp_afkep *ep, dev_err(ep->dcp->dev, "AFK[ep:%02x]: failed to start compact service %u: %d\n", ep->endpoint, channel, ret); - spin_lock_irqsave(&ep->lock, flags); - if (!ep->compact_start_err) - WRITE_ONCE(ep->compact_start_err, ret); - spin_unlock_irqrestore(&ep->lock, flags); afk_service_discard(service); - wake_up_all(&ep->service_wq); return; } } diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index 7e17d5188c218..ce4583a100691 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -607,8 +607,6 @@ struct apple_dcp_afkep { u32 rx_admitted; /* lock: prefix of rx_ring admitted before rx_paused. */ bool rx_work_queued; /* lock */ bool rx_failed; /* lock: early RX ring overflowed; never reopen TX. */ - int compact_start_err; /* lock: first compact protocol start failure. */ - u32 compact_rebind_gen; /* power_gen authorized to retire same-channel services. */ /* Compact EPIC, detected from the first compact message (desktop 26.x). */ bool compact; diff --git a/drivers/gpu/drm/apple/dptx-service.h b/drivers/gpu/drm/apple/dptx-service.h index 9a8bb97374856..9fbfbb74c45b1 100644 --- a/drivers/gpu/drm/apple/dptx-service.h +++ b/drivers/gpu/drm/apple/dptx-service.h @@ -38,15 +38,6 @@ static inline int dptx_service_bind(struct apple_epic_service *service, return 0; } -static inline int dptx_service_rebind(struct apple_epic_service *service, - struct dptx_port *port, s64 unit) -{ - port->unit = unit; - /* No owner is published until OPEN and AFK publication succeed. */ - service->cookie = port; - return 0; -} - static inline void dptx_service_publish(struct apple_epic_service *service) { struct dptx_port *port = service->cookie; @@ -65,9 +56,7 @@ static inline void dptx_service_unbind(struct apple_epic_service *service) spin_lock_irqsave(&service->lock, flags); if (port && port->service == service) { - /* Retain route eligibility across an authorized reset rebind. */ - if (!READ_ONCE(port->rebind_allowed)) - WRITE_ONCE(port->closed, true); + WRITE_ONCE(port->closed, true); /* Make terminal state visible to readers of availability. */ smp_store_release(&port->enabled, false); WRITE_ONCE(port->connected, false); diff --git a/drivers/gpu/drm/apple/dptxep.c b/drivers/gpu/drm/apple/dptxep.c index 60e1a31aa773a..2d148f5db00d9 100644 --- a/drivers/gpu/drm/apple/dptxep.c +++ b/drivers/gpu/drm/apple/dptxep.c @@ -1120,19 +1120,12 @@ static int dptxport_prepare(struct apple_epic_service *service, const char *name * without a class. Unit 0 is opened last, see dptxep_init_desktop(). */ if (READ_ONCE(service->ep->compact)) { - bool rebind; - /* Bind by name: another service on this endpoint is not a port. */ if ((unit != 0 && unit != 1) || !name || strcmp(name, "dcpdptx-port-epic")) return -EINVAL; - rebind = READ_ONCE(dcp->dptxport[unit].rebind_allowed); - if (!unit && !rebind) + if (!unit) service->defer_start = true; trace_dptxport_init(dcp, unit); - if (rebind) { - WRITE_ONCE(dcp->dptxport[unit].rebind_allowed, false); - return dptx_service_rebind(service, &dcp->dptxport[unit], unit); - } return dptx_service_bind(service, &dcp->dptxport[unit], unit); } diff --git a/drivers/gpu/drm/apple/dptxep.h b/drivers/gpu/drm/apple/dptxep.h index 8d3a40dd7afc9..316cb78975643 100644 --- a/drivers/gpu/drm/apple/dptxep.h +++ b/drivers/gpu/drm/apple/dptxep.h @@ -60,8 +60,6 @@ struct dptx_port { bool enabled, connected; /* A published session closed; unlike initial discovery, this is terminal. */ bool closed; - /* T8132 dcpext restarted from CPU reset: allow rebinding on fresh announcement. */ - bool rebind_allowed; /* Compact initial discovery: hold port->enabled until startup completes. */ bool staged; struct completion enable_completion; diff --git a/drivers/gpu/drm/apple/iomfb_m4.c b/drivers/gpu/drm/apple/iomfb_m4.c index 8aa08addb6bdc..e3e9df51c575c 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.c +++ b/drivers/gpu/drm/apple/iomfb_m4.c @@ -387,8 +387,6 @@ static void iomfb_m4_ext_afk_block_tx(struct iomfb_m4 *dcp, unsigned int count); static void dcp_m4_failed(struct iomfb_m4 *dcp, int error) { - /* Pairs with smp_store_release() in dptxep_init_desktop(). */ - struct apple_dcp_afkep *dptxep = smp_load_acquire(&dcp->owner->dptxep); unsigned long flags; bool first; @@ -409,8 +407,6 @@ static void dcp_m4_failed(struct iomfb_m4 *dcp, int error) dev_err(dcp->dev, "Retained DCP session quarantined: %d\n", error); complete_all(&dcp->reply); dcp_m4_runtime_wake(dcp); - if (dptxep) - wake_up_all(&dptxep->service_wq); } static int dcp_m4_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) @@ -5875,26 +5871,6 @@ static void iomfb_m4_ext_afk_resume_rx(struct iomfb_m4 *dcp, bool replay) afk_resume_rx(iomfb_m4_ext_afk_ep(dcp, i), replay); } -static bool iomfb_m4_ext_dptx0_started(struct iomfb_m4 *dcp, u32 gen) -{ - struct dptx_port *port = &dcp->owner->dptxport[0]; - struct apple_epic_service *service; - - if (!dptx_service_available(port)) - return false; - service = READ_ONCE(port->service); - return service && READ_ONCE(service->power_gen) == gen && - READ_ONCE(service->started); -} - -static bool iomfb_m4_ext_dptx0_ready(struct iomfb_m4 *dcp, - struct apple_dcp_afkep *dptxep, u32 gen) -{ - return READ_ONCE(dcp->error) || - READ_ONCE(dptxep->compact_start_err) || - iomfb_m4_ext_dptx0_started(dcp, gen); -} - static void dcp_m4_ext_reset_swap_state(struct dcp_m4_runtime *r) { if (!r) @@ -5916,65 +5892,29 @@ static void dcp_m4_ext_reset_swap_state(struct dcp_m4_runtime *r) r->ext_win[i].on_screen = false; } +/* + * The CPU came from reset, but its firmware goes on with the image it kept: + * the AFK endpoints and the DPTX port services on them are as they were, and + * are not announced again. A sink's AV service is, once its link is back. + */ static int iomfb_m4_ext_reset_finish(struct iomfb_m4 *dcp) { struct apple_dcp_afkep *dptxep = iomfb_m4_ext_afk_ep(dcp, ARRAY_SIZE(dcp->probe_ep)); - bool wait_dptx = dptxep && READ_ONCE(dptxep->compact); unsigned long flags; - u32 gen; spin_lock_irqsave(&dcp->state_lock, flags); dcp->ext_avserv = NULL; spin_unlock_irqrestore(&dcp->state_lock, flags); dcp_m4_ext_reset_swap_state(dcp->runtime); - if (dptxep) { + if (dptxep) afk_compact_orphans_drop(dptxep); - spin_lock_irqsave(&dptxep->lock, flags); - WRITE_ONCE(dptxep->compact_start_err, 0); - spin_unlock_irqrestore(&dptxep->lock, flags); - } WRITE_ONCE(dcp->owner->t8132_release_late, false); - gen = READ_ONCE(dcp->owner->afk_power_gen) + 1; - WRITE_ONCE(dcp->owner->afk_power_gen, gen); - for (unsigned int i = 0; i < DCP_M4_EXT_AFK_SLOTS; i++) { - struct apple_dcp_afkep *ep = iomfb_m4_ext_afk_ep(dcp, i); - - if (ep) { - afk_compact_rx_reset(ep); - WRITE_ONCE(ep->compact_rebind_gen, gen); - } - } - for (unsigned int i = 0; i < ARRAY_SIZE(dcp->owner->dptxport); i++) { - struct dptx_port *port = &dcp->owner->dptxport[i]; - - if (wait_dptx) { - /* Pairs with smp_load_acquire() in dptx_service_available(). */ - smp_store_release(&port->enabled, false); - WRITE_ONCE(port->connected, false); - reinit_completion(&port->enable_completion); - } - WRITE_ONCE(port->rebind_allowed, true); - } - /* Replay new-session AFK words deferred during retained wake once RTKit is ON. */ + for (unsigned int i = 0; i < DCP_M4_EXT_AFK_SLOTS; i++) + afk_compact_rx_reset(iomfb_m4_ext_afk_ep(dcp, i)); + /* Replay AFK words deferred during retained wake once RTKit is ON. */ iomfb_m4_ext_afk_resume_rx(dcp, true); - if (wait_dptx) { - long left = wait_event_timeout(dptxep->service_wq, - iomfb_m4_ext_dptx0_ready(dcp, dptxep, gen), - msecs_to_jiffies(1000)); - int err; - - afk_flush_rx(dptxep); - err = READ_ONCE(dcp->error); - if (!err) - err = READ_ONCE(dptxep->compact_start_err); - if (err) - return err; - if (!iomfb_m4_ext_dptx0_started(dcp, gen)) - return left ? -EIO : -ETIMEDOUT; - } else { - afk_flush_rx(dptxep); - } + afk_flush_rx(dptxep); return READ_ONCE(dcp->error); } @@ -6102,24 +6042,11 @@ static int iomfb_m4_ext_stop(struct iomfb_m4 *dcp, bool runtime) } if (ret) goto unwind; - /* - * Publish ext_idle_stopped before clearing port->enabled so route - * selection never observes a gap while the DCP transitions to idle. - */ - smp_store_release(&dcp->ext_idle_stopped, runtime); - /* The restarted firmware announces the sink's AV and DPTX services anew. */ + WRITE_ONCE(dcp->ext_idle_stopped, runtime); + /* The restarted firmware announces the sink's AV service anew. */ spin_lock_irqsave(&dcp->state_lock, flags); dcp->ext_avserv = NULL; spin_unlock_irqrestore(&dcp->state_lock, flags); - if (dcp->owner->dptxep && READ_ONCE(dcp->owner->dptxep->compact)) { - for (unsigned int i = 0; i < ARRAY_SIZE(dcp->owner->dptxport); i++) { - struct dptx_port *port = &dcp->owner->dptxport[i]; - - /* Pairs with smp_load_acquire() in dptx_service_available(). */ - smp_store_release(&port->enabled, false); - WRITE_ONCE(port->connected, false); - } - } dcp->gate_cycles++; dcp->gate_off_us = ktime_us_delta(ktime_get(), start); if (runtime) { @@ -6248,7 +6175,7 @@ static int iomfb_m4_ext_start(struct iomfb_m4 *dcp, bool runtime) } dcp->ext_stopped = false; if (!ret) { - step = "DPTX ready"; + step = "AFK resume"; ret = iomfb_m4_ext_reset_finish(dcp); } if (!ret) { diff --git a/tools/asahi/afk-compact-start-tests.py b/tools/asahi/afk-compact-start-tests.py index 373cb1f095ed3..70cfd59ee36d3 100755 --- a/tools/asahi/afk-compact-start-tests.py +++ b/tools/asahi/afk-compact-start-tests.py @@ -32,7 +32,7 @@ MUTATIONS = { 'no-discard-on-start-fail': ( AFK_C, - '\t\t\tafk_service_discard(service);\n\t\t\twake_up_all(&ep->service_wq);\n\t\t\treturn;\n\t\t}\n\t}\n\n\tafk_service_publish(service);', + '\t\t\tafk_service_discard(service);\n\t\t\treturn;\n\t\t}\n\t}\n\n\tafk_service_publish(service);', '\t\t\t(void)ret;\n\t\t}\n\t}\n\n\tafk_service_publish(service);', ), 'publish-before-start': ( @@ -91,9 +91,9 @@ ), 'dptxport-no-defer-unit0': ( DPTX_C, - '\t\tif (!unit && !rebind)\n' + '\t\tif (!unit)\n' '\t\t\tservice->defer_start = true;', - '\t\tif (false && !unit && !rebind)\n' + '\t\tif (false && !unit)\n' '\t\t\tservice->defer_start = true;', ), } @@ -241,7 +241,6 @@ def function(text, name): bool enabled; bool connected; bool closed; - bool rebind_allowed; unsigned int enable_completion; }; @@ -263,8 +262,6 @@ def function(text, name): bool compact; spinlock_t lock; unsigned int tx_active; - int compact_start_err; - u32 compact_rebind_gen; const struct apple_epic_service_ops *ops; struct apple_epic_service services[AFK_MAX_CHANNEL]; u32 num_channels; @@ -604,8 +601,7 @@ def function(text, name): CHECK(prepare_calls == 1 && !prepare_saw_enabled); CHECK(send_compact_calls == 1 && !send_saw_enabled); CHECK(teardown_calls == 1 && teardown_saw_cookie); - CHECK(init_calls == 0 && ep.compact_start_err == -EIO && - ep.service_wq == 1 && !ep.services[0].debugfs); + CHECK(init_calls == 0 && ep.service_wq == 0 && !ep.services[0].debugfs); CHECK(!ep.services[0].enabled && ep.services[0].torndown && !ep.services[0].started); CHECK(!ep.services[0].ops && !ep.services[0].cookie && !ep.services[0].users); CHECK(!afk_epic_find_service(&ep, 5) && afk_service_retired(&ep.services[0], 5)); @@ -618,8 +614,7 @@ def function(text, name): send_compact_ret = -ETIMEDOUT; announce(&ep, 9, "dispext0:noprep-svc:0"); CHECK(send_compact_calls == 1 && !send_saw_enabled); - CHECK(teardown_calls == 1 && init_calls == 0 && - ep.compact_start_err == -ETIMEDOUT && ep.service_wq == 1); + CHECK(teardown_calls == 1 && init_calls == 0 && ep.service_wq == 0); CHECK(!ep.services[0].enabled && ep.services[0].torndown && !ep.services[0].ops); CHECK(!afk_epic_find_service(&ep, 9) && afk_service_retired(&ep.services[0], 9)); @@ -628,8 +623,7 @@ def function(text, name): reset_ep(&dcp, &desk, &ep, 0x25, test_ops); send_compact_ret = -EPROTO; announce(&ep, 11, "dispext0:aux-svc:0"); - CHECK(send_compact_calls == 1 && init_calls == 0 && - ep.compact_start_err == -EPROTO && ep.service_wq == 1); + CHECK(send_compact_calls == 1 && init_calls == 0 && ep.service_wq == 0); CHECK(!ep.services[0].enabled && ep.services[0].torndown && !ep.services[0].ops); CHECK(!afk_epic_find_service(&ep, 11)); @@ -845,7 +839,6 @@ def main(): function(sources[AFK_H], 'afk_service_publish'), function(sources[DPTX_H], 'dptx_service_port'), function(sources[DPTX_H], 'dptx_service_bind'), - function(sources[DPTX_H], 'dptx_service_rebind'), function(sources[DPTX_H], 'dptx_service_publish'), function(sources[DPTX_H], 'dptx_service_unbind'), function(sources[AFK_C], 'afk_epic_find_service'), diff --git a/tools/asahi/dcpext26-startup-tests.py b/tools/asahi/dcpext26-startup-tests.py index 75d342c9be395..092a7859945e8 100644 --- a/tools/asahi/dcpext26-startup-tests.py +++ b/tools/asahi/dcpext26-startup-tests.py @@ -94,7 +94,7 @@ def run(name,code,reject=False): struct apple_epic_service;struct apple_dcp_afkep; struct apple_epic_service_ops {const char *name;bool reusable,optional,open_later,singleton;int (*prepare)(struct apple_epic_service *,const char *,const char *,s64);int (*revalidate)(struct apple_epic_service *,const char *,const char *,s64);void (*init)(struct apple_epic_service *,const char *,const char *,s64);void (*teardown)(struct apple_epic_service *);void (*init_props)(struct apple_epic_service *,s64,const void *,size_t);}; struct apple_epic_service {struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops;bool enabled,torndown,pending_open,defer_start,started;unsigned users;int lock;u32 channel;void *cookie;const char *pending_name,*pending_class;s64 pending_unit;int cmd_map[1];}; -struct dptx_port {struct apple_epic_service *service;s64 unit;bool enabled,connected,closed,rebind_allowed;int enable_completion;}; +struct dptx_port {struct apple_epic_service *service;s64 unit;bool enabled,connected,closed;int enable_completion;}; struct apple_dcp { const void *push_out_prefill; bool native,ext26_services_open,v27_j714s,v27_sleep_transport,dcpext; @@ -141,7 +141,7 @@ def run(name,code,reject=False): for name in ['afk_match_service','afk_epic_find_service','afk_discard_pending_open','afk_retry_service_init','afk_service_retry']: identity+=fn(afk,name)+'\n' service=(A/'dptx-service.h').read_text() -for name in ['dptx_service_port','dptx_service_bind','dptx_service_rebind','dptx_service_publish']: +for name in ['dptx_service_port','dptx_service_bind','dptx_service_publish']: identity+=fn(service,name)+'\n' identity+=fn(dcp,'dcp_is_external_h17p')+'\n' identity+=fn(dptx,'dptxport_prepare')+'\n'+fn(dcp,'dcp_ext26_open_services')+'\n'+fn(afk,'afk_recv_handle_init')+r''' diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/test-admission.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/test-admission.py index c9949ae5cc0e1..015d4ac7f3f5e 100755 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/test-admission.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/test-admission.py @@ -55,7 +55,7 @@ def extract(file, name): struct apple_dcp_afkep *ep; unsigned long cmd_map[1]; }; struct dptx_port { - bool enabled, connected, closed, rebind_allowed; int enable_completion; u32 unit; + bool enabled, connected, closed; int enable_completion; u32 unit; struct apple_epic_service *service; }; static void reinit_completion(int *p) { *p = 0; } diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/m4.py b/tools/testing/selftests/drivers/gpu/apple-dcp/m4.py index 2609da8596417..3e838be7d8a59 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/m4.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/m4.py @@ -708,6 +708,127 @@ def gate(root): ''' +def ext_wake(root): + """The stop and retained wake of an external DCP: the firmware keeps its + AFK session, so the DPTX ports stay and the wake waits for no announcement.""" + backend = (root / APPLE / 'iomfb_m4.c').read_text() + return PREFIX + r''' +#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x,v) ((x)=(v)) +#define dev_dbg(...) ((void)0) +#define dev_info(...) ((void)0) +#define dev_warn(...) ((void)0) +#define dev_err(d,...) ((void)(d),(void)fprintf(stderr,__VA_ARGS__)) +#define spin_lock_irqsave(p,f) do { (void)(p); (f)=0; } while(0) +#define spin_unlock_irqrestore(p,f) do { (void)(p); (void)(f); } while(0) +#define lockdep_assert_held(l) ((void)(l)) +#define DCP_M4_EXT_AFK_SLOTS 33 +typedef u32 __le32; +typedef int64_t s64; +typedef s64 ktime_t; +static ktime_t clock_ns; +static ktime_t ktime_get(void) { return clock_ns += 1000; } +static s64 ktime_us_delta(ktime_t later,ktime_t earlier) { return (later-earlier)/1000; } +struct device { int unused; }; +struct apple_rtkit { int unused; }; +struct apple_dart_cio { int unused; }; +struct apple_dcp_afkep { int unused; }; +struct dptx_port { bool enabled,connected; void *service; }; +struct apple_dcp { bool t8132_release_late; struct dptx_port dptxport[2]; struct apple_dcp_afkep *dptxep; }; +struct dcp_m4_runtime { unsigned count; bool ext_qn; }; +struct iomfb_m4 { + struct apple_dcp *owner; + struct device *dev; + struct apple_rtkit *rtkit; + struct dcp_m4_runtime *runtime; + struct apple_dcp_afkep *probe_ep[32]; + struct apple_dart_cio *ext_darts[2]; + void *ext_avserv; + int lock,state_lock,error; + struct { unsigned calls; } transaction; + bool gated,ext_stopped,ext_idle_stopped,ext_dart_down; + u64 gate_cycles; + s64 gate_off_us,gate_on_us; +}; +static char steps[512]; +static void trace(const char *s) { strcat(steps,s); strcat(steps," "); } +static bool tx_open=true,rx_open=true,wake_fails; +static void dcp_m4_failed(struct iomfb_m4 *d,int error) { if(!d->error) d->error=error; } +static int dcp_m4_runtime_live(struct dcp_m4_runtime *r) { (void)r; return 0; } +static int dcp_m4_runtime_call(struct dcp_m4_runtime *r,unsigned method,bool remote, + const void *in,u32 in_size,void *out,u32 out_size) +{ (void)r;(void)remote;(void)in;(void)in_size;(void)out;(void)out_size; assert(method==500); trace("A500"); return 0; } +static int dcp_m4_runtime_drain(struct dcp_m4_runtime *r,unsigned n) { (void)r;(void)n; return 0; } +static void dcp_m4_ext_reset_swap_state(struct dcp_m4_runtime *r) { (void)r; } +static struct apple_dcp_afkep *iomfb_m4_ext_afk_ep(struct iomfb_m4 *d,unsigned i) { return i==32?d->owner->dptxep:d->probe_ep[i]; } +static int iomfb_m4_ext_afk_pause_tx(struct iomfb_m4 *d,unsigned *n) { (void)d; tx_open=false; *n=DCP_M4_EXT_AFK_SLOTS; return 0; } +static int iomfb_m4_ext_afk_open_tx(struct iomfb_m4 *d,unsigned n,bool ungate) { (void)n; tx_open=true; if(ungate) d->gated=false; return 0; } +static int iomfb_m4_ext_afk_recheck(struct iomfb_m4 *d) { (void)d; return 0; } +static int iomfb_m4_ext_afk_rx_check(struct iomfb_m4 *d) { (void)d; return 0; } +static void iomfb_m4_ext_afk_pause_rx(struct iomfb_m4 *d) { (void)d; rx_open=false; } +static void iomfb_m4_ext_afk_resume_rx(struct iomfb_m4 *d,bool replay) { (void)d; assert(replay); rx_open=true; } +static void afk_compact_orphans_drop(struct apple_dcp_afkep *e) { (void)e; } +static void afk_compact_rx_reset(struct apple_dcp_afkep *e) { (void)e; } +static void afk_flush_rx(struct apple_dcp_afkep *e) { (void)e; } +static int apple_rtkit_idle(struct apple_rtkit *r) { (void)r; trace("idle"); return 0; } +static void apple_rtkit_hold_rx(struct apple_rtkit *r) { (void)r; } +static void apple_rtkit_resume_rx(struct apple_rtkit *r) { (void)r; } +static int apple_rtkit_poll(struct apple_rtkit *r) { (void)r; return 0; } +static void apple_rtkit_flush_rx(struct apple_rtkit *r) { (void)r; } +static int apple_rtkit_wake(struct apple_rtkit *r) { (void)r; return 0; } +static int iomfb_m4_cpu_run(struct iomfb_m4 *d,bool run) { (void)d; trace(run?"run":"stop"); return 0; } +static int iomfb_m4_cpu_start(void *d) { return iomfb_m4_cpu_run(d,true); } +static int apple_rtkit_wake_retained(struct apple_rtkit *r,int (*start)(void *),void *ctx) +{ (void)r; assert(!rx_open && !tx_open); trace("wake"); return wake_fails?-ETIMEDOUT:start(ctx); } +static int apple_pmgr_ps_consumer_check(struct device *d) { (void)d; return 0; } +static int apple_pmgr_ps_consumer_off(struct device *d) { (void)d; trace("cpu-off"); return 0; } +static int apple_pmgr_ps_consumer_on(struct device *d) { (void)d; trace("cpu-on"); return 0; } +static bool apple_pmgr_ps_consumer_is_off(struct device *d) { (void)d; return true; } +static int apple_dart_cio_prepare(struct apple_dart_cio *d) { (void)d; return 0; } +static int apple_dart_cio_prepare_runtime(struct apple_dart_cio *d) { (void)d; return 0; } +static int iomfb_m4_ext_dart_off(struct apple_dart_cio *d) { (void)d; trace("dart-off"); return 0; } +static int apple_dart_cio_on(struct apple_dart_cio *d) { (void)d; trace("dart-on"); return 0; } +static int apple_dart_cio_finish(struct apple_dart_cio *d) { (void)d; return 0; } +''' + '\n'.join(block(backend, name) for name in ( + 'static int iomfb_m4_ext_reset_finish(', 'static int iomfb_m4_ext_stop(', + 'static int iomfb_m4_ext_start(')) + r''' +int main(void) +{ + struct apple_dcp_afkep ep={0}; + struct apple_dcp owner={.dptxep=&ep,.dptxport[0]={.enabled=true,.service=&ep}}; + struct device dev={0}; + struct apple_rtkit rtk={0}; + struct apple_dart_cio darts[2]={{0}}; + struct dcp_m4_runtime r={0}; + struct iomfb_m4 dcp={.owner=&owner,.dev=&dev,.rtkit=&rtk,.runtime=&r, + .ext_darts={&darts[0],&darts[1]},.ext_dart_down=true}; + + /* Idle, then a deep sleep: no service is announced during either wake. */ + for(int runtime=1;runtime>=0;runtime--) { + steps[0]=0; + assert(!iomfb_m4_ext_stop(&dcp,runtime)); + assert(!strcmp(steps,"A500 idle stop dart-off dart-off cpu-off ")); + assert(dcp.ext_stopped && dcp.gated && dcp.ext_idle_stopped==runtime && !tx_open); + assert(owner.dptxport[0].enabled && owner.dptxport[0].service==&ep); + steps[0]=0; + assert(!iomfb_m4_ext_start(&dcp,runtime)); + assert(!strcmp(steps,"cpu-on dart-on dart-on wake run ")); + assert(!dcp.ext_stopped && !dcp.gated && !dcp.ext_idle_stopped && !dcp.error); + assert(owner.dptxport[0].enabled && owner.dptxport[0].service==&ep); + assert(tx_open && rx_open); + } + /* A wake that fails still ends the session. */ + assert(!iomfb_m4_ext_stop(&dcp,true)); + wake_fails=true; + assert(iomfb_m4_ext_start(&dcp,true)==-ETIMEDOUT); + assert(dcp.error==-ETIMEDOUT && dcp.gated && !tx_open && !rx_open); + puts("PASS an external DCP's DPTX ports stay across its stop and retained wake, idle or asleep"); + return 0; +} +''' + + def blank(root): backend = (root / APPLE / 'iomfb_m4.c').read_text() backlight = (root / APPLE / 'dcp_backlight.c').read_text() diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/run.py b/tools/testing/selftests/drivers/gpu/apple-dcp/run.py index a5d887fb62a72..80dddd29b1f88 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/run.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/run.py @@ -91,6 +91,7 @@ def main(): ('m4', 'm4-producer-retirement', m4.prepare, ((),), ('-Wno-unused-function',)), ('m4', 'm4-dpms-retirement', m4.blank, ((),), ('-Wno-unused-function', '-Wno-unused-const-variable')), ('m4', 'm4-power-gate', m4.gate, ((),), ('-Wno-unused-function',)), + ('m4', 'm4-ext-wake', m4.ext_wake, ((),), ('-Wno-unused-function',)), ('m4', 'm4-dart-consumer', m4.dart, ((),), ('-Wno-unused-function',)), ('m4', 'm4-panel-timing', m4.timing, ((),), ('-Wno-unused-function', '-Wno-unused-const-variable')), ('boot', 'oslog-events', boot.oslog, ((),), ()), From aad3a8509d3d6ea949ba9879b28c31926ac63db0 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 22:02:57 -0500 Subject: [PATCH 2/2] drm/apple: fabric: pass over a failed external DCP for DP alt mode A quarantined T8132 external DCP cannot drive a display until a reboot, but the route selection for a direct DP alt mode display does not look at that: services_ready is only worked out for tunnels. A replug therefore takes the failed DCP again, as the port's last pipeline or the lowest free one, and fails with its error each time, while the other external DCP is idle. Work out services_ready for direct streams too, from the session's failure alone, and let only a pipeline with it take one. A stream with no other pipeline waits, as it does when the others drive displays, and a modeset no longer moves a stream to the failed pipeline. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/dcp-fabric-core.c | 14 ++++++++++---- drivers/gpu/drm/apple/dcp-fabric-test.c | 23 +++++++++++++++++++++++ drivers/gpu/drm/apple/dcp-fabric.c | 6 +++--- drivers/gpu/drm/apple/iomfb_m4.c | 8 ++++++++ drivers/gpu/drm/apple/iomfb_m4.h | 2 ++ tools/asahi/tunnel-route-tests.py | 1 + tools/asahi/tunnel-route/fixture.c | 13 ++++++++++++- 7 files changed, 59 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-fabric-core.c b/drivers/gpu/drm/apple/dcp-fabric-core.c index 4bc3884db5b14..251c8957339df 100644 --- a/drivers/gpu/drm/apple/dcp-fabric-core.c +++ b/drivers/gpu/drm/apple/dcp-fabric-core.c @@ -176,6 +176,14 @@ bool dcp_fabric_movable(const struct dcp_fabric_route *owner, } EXPORT_SYMBOL_GPL(dcp_fabric_movable); +/* Can a direct DP-alt stream take @route now? */ +static bool dcp_fabric_direct_available(const struct dcp_fabric_route *route, + const struct dcp_fabric_policy *policy) +{ + return !route->tunnel_only && route->pipeline->services_ready && + dcp_fabric_available(route->pipeline, policy); +} + struct dcp_fabric_route * dcp_fabric_free_route(const struct dcp_fabric_port *port, const struct dcp_fabric_policy *policy) @@ -186,8 +194,7 @@ dcp_fabric_free_route(const struct dcp_fabric_port *port, for (route = port->routes; route; route = route->next) { unsigned int score; - if (route->tunnel_only || - !dcp_fabric_available(route->pipeline, policy)) + if (!dcp_fabric_direct_available(route, policy)) continue; score = dcp_fabric_score(route->pipeline, policy); if (score < best_score) { @@ -195,8 +202,7 @@ dcp_fabric_free_route(const struct dcp_fabric_port *port, best_score = score; } } - if (!last || last->tunnel_only || - !dcp_fabric_available(last->pipeline, policy)) + if (!last || !dcp_fabric_direct_available(last, policy)) return best; if (!policy->dual_stream && best && best_score < dcp_fabric_score(last->pipeline, policy)) diff --git a/drivers/gpu/drm/apple/dcp-fabric-test.c b/drivers/gpu/drm/apple/dcp-fabric-test.c index 6e3f69ba68c80..6989460ce4a77 100644 --- a/drivers/gpu/drm/apple/dcp-fabric-test.c +++ b/drivers/gpu/drm/apple/dcp-fabric-test.c @@ -2115,6 +2115,28 @@ static void fabric_per_dpin_slot_test(struct kunit *test) KUNIT_EXPECT_EQ(test, dcp_fabric_tunnel_slot(&port, 3, true), -EBUSY); } +/* A pipeline whose services are gone never takes a direct DP-alt stream. */ +static void fabric_direct_services_test(struct kunit *test) +{ + struct fabric_fixture f; + + /* Pipeline 1 would win, also as the port's last one. */ + fabric_init(&f, false); + f.port[0].preferred = &f.route[0][1]; + f.pipeline[1].services_ready = false; + KUNIT_EXPECT_PTR_EQ(test, dcp_fabric_free_route(&f.port[0], &f.policy), + &f.route[0][0]); + /* Its peer drives another display: the stream waits. */ + f.pipeline[0].owned = true; + KUNIT_EXPECT_PTR_EQ(test, dcp_fabric_free_route(&f.port[0], &f.policy), + NULL); + /* A modeset does not move the stream there either. */ + KUNIT_EXPECT_EQ(test, + dcp_fabric_follow(&f.route[0][0], &f.route[0][1], + NULL, NULL, false, &f.policy), + DCP_FABRIC_FOLLOW_REFUSE); +} + /* A tunnel-only route never takes a direct DP-alt stream. */ static void fabric_tunnel_only_test(struct kunit *test) { @@ -2667,6 +2689,7 @@ static struct kunit_case fabric_tests[] = { KUNIT_CASE(fabric_t6030_plan_test), KUNIT_CASE(fabric_t6030_follow_test), KUNIT_CASE(fabric_per_dpin_slot_test), + KUNIT_CASE(fabric_direct_services_test), KUNIT_CASE(fabric_tunnel_only_test), KUNIT_CASE(fabric_tunnel_only_first_test), KUNIT_CASE(fabric_reclaim_transaction_test), diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index 58105cc3ac6c9..1b13c0dfe1ff1 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -1600,8 +1600,8 @@ static void dcp_fabric_snapshot_port(struct apple_dcp_typec_port *port, dcp_typec_safe_held(dcp); pipeline->tunnel_held = dcp_typec_tunnel_held(dcp); pipeline->presence = dcp_hdmi_presence(dcp); - pipeline->services_ready = !tunnel || - dcp_tb_services_ready(dcp); + pipeline->services_ready = tunnel ? dcp_tb_services_ready(dcp) : + !iomfb_m4_ext_failed(dcp); pipeline->external = dcp->external; route->core.pipeline = pipeline; route->core.tunnel = route->tunnel; @@ -2098,7 +2098,7 @@ static int dcp_typec_port_busy(struct apple_dcp_typec_port *port, port->busy_noted = true; of_property_read_string(port->connector_np, "label", &label); dev_info(dcp->dev, - "display on %s waits for a free external display pipeline (each one drives another display); it lights up on its own when one of those displays is unplugged\n", + "display on %s waits for a free external display pipeline (each one drives another display or has failed); it lights up on its own when one of those displays is unplugged\n", label ?: "a USB-C port"); return -ENOSPC; } diff --git a/drivers/gpu/drm/apple/iomfb_m4.c b/drivers/gpu/drm/apple/iomfb_m4.c index e3e9df51c575c..7d5cd74bbfb5e 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.c +++ b/drivers/gpu/drm/apple/iomfb_m4.c @@ -6290,6 +6290,14 @@ void iomfb_m4_ext_idle_kick(struct apple_dcp *owner) mutex_unlock(&dcp->lock); } +/* An external DCP whose session was quarantined: no display until a reboot. */ +bool iomfb_m4_ext_failed(struct apple_dcp *owner) +{ + struct iomfb_m4 *dcp = owner->v26_6; + + return dcp && dcp->external && READ_ONCE(dcp->error); +} + bool iomfb_m4_ext_route_ready(struct apple_dcp *owner) { struct iomfb_m4 *dcp = owner->v26_6; diff --git a/drivers/gpu/drm/apple/iomfb_m4.h b/drivers/gpu/drm/apple/iomfb_m4.h index dcdcc9a6026b3..ac4339a175709 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.h +++ b/drivers/gpu/drm/apple/iomfb_m4.h @@ -82,6 +82,7 @@ void iomfb_m4_ext_relink_wait(struct apple_dcp *dcp, unsigned long deadline); int iomfb_m4_ext_relink_end(struct apple_dcp *dcp); bool iomfb_m4_ext_stopped(struct apple_dcp *dcp); bool iomfb_m4_sleep_stopped(struct apple_dcp *dcp); +bool iomfb_m4_ext_failed(struct apple_dcp *dcp); bool iomfb_m4_ext_route_ready(struct apple_dcp *dcp); void iomfb_m4_ext_idle_kick(struct apple_dcp *dcp); int iomfb_m4_ext_idle_wake(struct apple_dcp *dcp); @@ -173,6 +174,7 @@ static inline void iomfb_m4_ext_relink_wait(struct apple_dcp *dcp, unsigned long static inline int iomfb_m4_ext_relink_end(struct apple_dcp *dcp) { return 0; } static inline bool iomfb_m4_ext_stopped(struct apple_dcp *dcp) { return false; } static inline bool iomfb_m4_sleep_stopped(struct apple_dcp *dcp) { return false; } +static inline bool iomfb_m4_ext_failed(struct apple_dcp *dcp) { return false; } static inline bool iomfb_m4_ext_route_ready(struct apple_dcp *dcp) { return false; } static inline void iomfb_m4_ext_idle_kick(struct apple_dcp *dcp) { } static inline int iomfb_m4_ext_idle_wake(struct apple_dcp *dcp) { return 0; } diff --git a/tools/asahi/tunnel-route-tests.py b/tools/asahi/tunnel-route-tests.py index b18d7e8543ac5..a70f2ec34be3f 100644 --- a/tools/asahi/tunnel-route-tests.py +++ b/tools/asahi/tunnel-route-tests.py @@ -22,6 +22,7 @@ MUTATIONS = { 'clock-ignored': ('& BIT(dpin)))', '& 0))'), 'snapshot-ignores-yield': ('port->dp_wanted && !port->typec_yielded;', 'port->dp_wanted;'), + 'snapshot-ignores-failure': ('!iomfb_m4_ext_failed(dcp)', '!iomfb_m4_ext_failed(dcp) || true'), 'snapshot-two-slots': ('i < ARRAY_SIZE(port->slots); i++)\n\t\tport->core.owner[i]', 'i < 2; i++)\n\t\tport->core.owner[i]'), } diff --git a/tools/asahi/tunnel-route/fixture.c b/tools/asahi/tunnel-route/fixture.c index f5f7eb7cecf52..4433a57d4bac3 100644 --- a/tools/asahi/tunnel-route/fixture.c +++ b/tools/asahi/tunnel-route/fixture.c @@ -61,7 +61,7 @@ struct apple_dcp_typec_port { struct apple_connector *connector, *secondary_connector; bool dp_wanted, dp_hpd, typec_yielded; }; -static bool t6030, t8132_ext; +static bool t6030, t8132_ext, ext_failed; static bool of_machine_is_compatible(const char *compatible) { assert(!strcmp(compatible, "apple,t6030")); @@ -93,6 +93,10 @@ static bool dcp_tb_services_ready(struct apple_dcp *d) { return true; } +static bool iomfb_m4_ext_failed(struct apple_dcp *d) +{ + return ext_failed; +} /* PRODUCTION_FUNCTIONS */ int main(void) { @@ -200,6 +204,13 @@ int main(void) assert(!dcp_typec_tunnel_ctl(&r, DCP_FABRIC_DPIN_MAX)); } } + /* A direct stream does not take a pipeline whose session failed. */ + dcp_fabric_snapshot_port(&port, false); + assert(dcp.fabric.services_ready); + ext_failed = true; + dcp_fabric_snapshot_port(&port, false); + assert(!dcp.fabric.services_ready); + ext_failed = false; /* The snapshot carries every owner slot, DP IN 2 and 3's own included. */ for (unsigned int slot = 0; slot < DCP_FABRIC_DPIN_MAX; slot++) { memset(port.slots, 0, sizeof(port.slots));