From 944b9be10bc772ee35d5740114506a0c1a29bd98 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 26 Nov 2026 08:38:31 +1100 Subject: [PATCH 1/2] drm/apple: afk: retire a superseded singleton AV service On the classic AV endpoint, DCP firmware 27.2 announces its DCPAVSimpleVideoInterface and DCPAVAudioInterface again on fresh channels every time the display comes back (DPMS on, wake, re-attach) and never closes the previous channels. The old instances stayed enabled, so their slots were never reclaimed. After eight attaches a Mac mini (M2) used all 16 service slots of the endpoint, logged "no reusable service slots" and lost monitor audio until reboot. The compact path already replaces such re-announced services in place (afk_compact_slot()). Do the same on the classic path for services that are singletons, one live instance per endpoint: an announcement on a new channel retires the previous instance, and its slot is reclaimed once its last user lets go. The successor's init takes over the service's state, as before. Firmware that closes the old channel first is unaffected: its instance is already torn down. Mark the classic AV video and audio interfaces as singletons, and add a KUnit test that re-announces them many more times than there are slots. Signed-off-by: Ryan Murray --- drivers/gpu/drm/apple/afk-service-test.c | 70 +++++++++++++++++++ drivers/gpu/drm/apple/afk.c | 10 +++ drivers/gpu/drm/apple/afk.h | 28 ++++++++ drivers/gpu/drm/apple/av.c | 2 + .../selftests/drivers/gpu/apple-dcp/edid.py | 4 +- 5 files changed, 112 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/afk-service-test.c b/drivers/gpu/drm/apple/afk-service-test.c index 06db01a8f84d3..ef0920a28dbe8 100644 --- a/drivers/gpu/drm/apple/afk-service-test.c +++ b/drivers/gpu/drm/apple/afk-service-test.c @@ -266,11 +266,81 @@ static void afk_service_declined_channel_test(struct kunit *test) KUNIT_EXPECT_TRUE(test, afk_service_retired(service, 3)); } +static const struct apple_epic_service_ops singleton_video_ops = { + .name = "singleton-video", + .reusable = true, + .singleton = true, +}; + +static const struct apple_epic_service_ops singleton_audio_ops = { + .name = "singleton-audio", + .reusable = true, + .singleton = true, +}; + +/* + * The firmware announces its AV video and audio interfaces again on fresh + * channels on every display attach and never closes the old ones. Announce + * them as the endpoint's receive path does: supersede, then take the first + * reclaimable slot. The superseded audio instance stays pinned by its owner + * until the successor's init drops it. + */ +static struct apple_epic_service * +afk_test_announce(struct apple_epic_service *slots, unsigned int nr, + const struct apple_epic_service_ops *ops, u32 channel) +{ + unsigned int i; + + for (i = 0; i < nr; i++) + afk_service_supersede(&slots[i], ops, channel); + for (i = 0; i < nr; i++) + if (afk_service_reinit(&slots[i], NULL, ops, channel)) + return &slots[i]; + return NULL; +} + +static void afk_service_singleton_reannounce_test(struct kunit *test) +{ + struct apple_epic_service *slots, *video, *audio, *owned = NULL; + const unsigned int nr = 4; + unsigned int i; + u32 channel = 5; + + slots = kunit_kcalloc(test, nr, sizeof(*slots), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, slots); + for (i = 0; i < nr; i++) + spin_lock_init(&slots[i].lock); + + /* Many more attaches than slots: the budget stays bounded. */ + for (i = 0; i < 4 * AFK_MAX_CHANNEL; i++, channel += 4) { + video = afk_test_announce(slots, nr, &singleton_video_ops, channel); + KUNIT_ASSERT_NOT_NULL(test, video); + KUNIT_EXPECT_TRUE(test, afk_service_matches(video, channel)); + audio = afk_test_announce(slots, nr, &singleton_audio_ops, channel + 2); + KUNIT_ASSERT_NOT_NULL(test, audio); + /* The audio owner pins the new instance and lets go of the old one. */ + KUNIT_ASSERT_PTR_EQ(test, afk_service_get(audio), audio); + if (owned) + afk_service_put(owned); + owned = audio; + } + + /* A different service is never superseded by a singleton announcement. */ + KUNIT_EXPECT_FALSE(test, afk_service_supersede(owned, &singleton_video_ops, 1)); + /* Nor is the instance announced on the same channel. */ + KUNIT_EXPECT_FALSE(test, afk_service_supersede(owned, &singleton_audio_ops, + owned->channel)); + /* Only singleton services are superseded. */ + KUNIT_EXPECT_FALSE(test, afk_service_supersede(owned, &reusable_ops, 1)); + afk_service_put(owned); +} + static struct kunit_case afk_service_cases[] = { KUNIT_CASE(afk_service_admission_test), KUNIT_CASE(dptx_service_identity_test), KUNIT_CASE(dptx_service_duplicate_close_test), KUNIT_CASE(afk_service_many_generations_test), + KUNIT_CASE(afk_service_singleton_reannounce_test), KUNIT_CASE(afk_service_reader_pin_test), KUNIT_CASE(afk_service_pending_reply_test), KUNIT_CASE(afk_service_completed_reader_test), diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 096d1685ae03b..84532778e9998 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -1885,6 +1885,16 @@ static void afk_recv_handle_init(struct apple_dcp_afkep *ep, u32 channel, goto free; } + for (ch_idx = 0; ch_idx < ep->num_channels; ch_idx++) { + struct apple_epic_service *old = &ep->services[ch_idx]; + + if (afk_service_supersede(old, ops, channel)) { + dev_info(ep->dcp->dev, + "AFK[ep:%02x]: %s on channel %u supersedes channel %u\n", + ep->endpoint, service_name, channel, old->channel); + afk_remove_service_debugfs(old); + } + } for (ch_idx = 0; ch_idx < AFK_MAX_CHANNEL; ch_idx++) { struct apple_epic_service *candidate = &ep->services[ch_idx]; diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index fef5067af705e..4d8c41ccdae17 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -258,6 +258,34 @@ static inline void afk_service_retire_channel(struct apple_epic_service *service spin_unlock_irqrestore(&service->lock, flags); } +/* + * A singleton service has one live instance per endpoint. The firmware + * announces it again on a fresh channel when the display is attached again, + * without closing the previous channel (as for compact services, see + * afk_compact_slot()). The announcement on @channel then supersedes + * @service: retire it, so that its slot is reclaimed once its last user + * lets go. The successor's init takes over the service's state. + */ +static inline bool afk_service_supersede(struct apple_epic_service *service, + const struct apple_epic_service_ops *ops, + u32 channel) +{ + unsigned long flags; + bool superseded; + + if (!ops->singleton) + return false; + spin_lock_irqsave(&service->lock, flags); + superseded = service->ops == ops && !service->torndown && + service->channel != channel; + if (superseded) { + service->enabled = false; + service->torndown = true; + } + spin_unlock_irqrestore(&service->lock, flags); + return superseded; +} + static inline bool afk_service_matches(struct apple_epic_service *service, u32 channel) { diff --git a/drivers/gpu/drm/apple/av.c b/drivers/gpu/drm/apple/av.c index f685bce973e5a..f285070f33f3e 100644 --- a/drivers/gpu/drm/apple/av.c +++ b/drivers/gpu/drm/apple/av.c @@ -1060,12 +1060,14 @@ static const struct apple_epic_service_ops avep_ops[] = { { .name = "DCPAVSimpleVideoInterface", .reusable = true, + .singleton = true, .init = av_interface_init, .teardown = av_interface_teardown, }, { .name = "DCPAVAudioInterface", .reusable = true, + .singleton = true, .init = av_audiosrv_init, .report = av_audiosrv_report, .teardown = av_audiosrv_teardown, diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index db428743d7d12..6fbf19d2a6329 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -85,7 +85,7 @@ def function(file,signature): struct apple_dcp;struct apple_dcp_afkep;struct apple_epic_service; void afk_service_retry(struct apple_dcp_afkep *ep); /* open_later: a firmware 27 panel service that sends its own OPEN; none is here. */ -struct apple_epic_service_ops { char name[32];bool reusable,optional,open_later;void (*init)(struct apple_epic_service *,const char *,const char *,s64);int (*prepare)(struct apple_epic_service *,const char *,const char *,s64);int (*revalidate)(struct apple_epic_service *,const char *,const char *,s64);void (*init_props)(struct apple_epic_service *,s64,const void *,size_t);void (*teardown)(struct apple_epic_service *); }; +struct apple_epic_service_ops { char name[32];bool reusable,optional,open_later,singleton;void (*init)(struct apple_epic_service *,const char *,const char *,s64);int (*prepare)(struct apple_epic_service *,const char *,const char *,s64);int (*revalidate)(struct apple_epic_service *,const char *,const char *,s64);void (*init_props)(struct apple_epic_service *,s64,const void *,size_t);void (*teardown)(struct apple_epic_service *); }; struct epic_cmd_info { u16 tag;u8 type;size_t reply_length,reply_len;bool reply_len_valid;void *rxbuf,*txbuf;dma_addr_t rxbuf_dma,txbuf_dma;size_t rxlen,txlen;u32 retcode;bool done,free_on_ack;int *completion; }; struct apple_epic_service { struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops; @@ -183,7 +183,7 @@ def function(file,signature): function('epic/dpavservep.c','static void dcpavserv_teardown('),function('epic/dpavservep.c','void dpavservep_detach('), function('afk.c','static const struct apple_epic_service_ops *\nafk_match_service('), function('afk.c','static struct apple_epic_service *afk_epic_find_service('), - function('afk.h','static inline bool afk_service_retired('),function('afk.c','static bool afk_epic_channel_retired('), + function('afk.h','static inline bool afk_service_retired('),function('afk.h','static inline bool afk_service_supersede('),function('afk.c','static bool afk_epic_channel_retired('), function('afk.c','static void afk_discard_pending_open('),function('afk.c','static void afk_retry_service_init('),function('afk.c','static void afk_service_worker('),function('afk.c','static void afk_recv_handle_init('), function('epic/dpavservep.c','static int dcpdpserv_prepare('), function('epic/dpavservep.c','static void dcpdpserv_teardown('),function('afk.c','static void afk_recv_handle_teardown('), From 5938e9e5e9b63e58941ece4cf271f2b48c597e0e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 26 Nov 2026 10:33:07 +1100 Subject: [PATCH 2/2] tools/asahi: dcpext26-startup-tests: model the singleton supersede afk_recv_handle_init() now supersedes the previous instance of a singleton service and drops its debugfs entry. Give the host model the ops flag, the helper and a debugfs stub, as the apple-dcp model has. Signed-off-by: Ryan Murray --- tools/asahi/dcpext26-startup-tests.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/asahi/dcpext26-startup-tests.py b/tools/asahi/dcpext26-startup-tests.py index c896fb3715666..6d2fba8014404 100644 --- a/tools/asahi/dcpext26-startup-tests.py +++ b/tools/asahi/dcpext26-startup-tests.py @@ -91,7 +91,7 @@ def run(name,code,reject=False): #define MAX_PENDING_CMDS 16 struct dcp_parse_ctx {void *dcp;const void *blob;u32 pos,len;}; struct apple_epic_service;struct apple_dcp_afkep; -struct apple_epic_service_ops {const char *name;bool reusable,optional,open_later;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_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;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;int enable_completion;}; struct apple_dcp {const void *push_out_prefill;bool native,ext26_services_open,v27_j714s,v27_sleep_transport,dcpext;int fw_compat,index,dptx_controller_ready;struct {unsigned num_dptx_ports;int iomfb_method_profile,firmware_compat;bool afk_announced_names;} hw;struct dptx_port dptxport[2];void *dev;struct apple_dcp_afkep *systemep,*ibootep,*dptx_controller_ep,*dcpavservep,*dptxep,*ext26_aux[3];}; @@ -108,6 +108,7 @@ def run(name,code,reject=False): static unsigned opened[256][16],queued,published; static int open_error; static void afk_populate_service_debugfs(struct apple_epic_service *s){} +static void afk_remove_service_debugfs(struct apple_epic_service *s){} static void afk_recv_handle_teardown(struct apple_dcp_afkep *e,u32 c){} static void afk_retry_service_init(struct apple_dcp_afkep *ep); void afk_service_retry(struct apple_dcp_afkep *ep); @@ -119,7 +120,7 @@ def run(name,code,reject=False): # Drop the audio-only helper region from the extracted prefix. identity=re.sub(r'#if IS_ENABLED\(CONFIG_DRM_APPLE_AUDIO\).*?#endif','',identity,flags=re.S) identity+=block(parser,'struct iterator {')+'\n'+fn(parser,'iterator_begin')+'\n'+fn(parser,'parse')+'\n'+fn(parser,'parse_epic_service_init')+'\n' -for name in ['afk_service_reclaimable','afk_service_reinit','afk_service_discard','afk_service_publish','afk_service_matches']: +for name in ['afk_service_reclaimable','afk_service_reinit','afk_service_discard','afk_service_publish','afk_service_matches','afk_service_supersede']: identity+=fn(h,name)+'\n' identity+=fn(dptx,'dptx_native_controllers_ready')+'\n'+fn(dptx,'dptx_native_controller_prepare')+'\n'+fn(dptx,'dptx_controller_service_init')+'\n' for name in ['afk_match_service','afk_epic_find_service','afk_discard_pending_open','afk_retry_service_init','afk_service_retry']: