From f923c3868957ac4476ab918be517078f8d54c77e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 04:16:59 +1100 Subject: [PATCH 1/2] drm/apple: audio: end a stream whose service instance was replaced The firmware publishes a successor of the DP audio service whenever the display link is set up again, also with the sink connected throughout. The successor comes up plugged, so the card sees no unplug and a running stream is not stopped. The link the stream started on went with the old instance: its DMA stops and the stream waits in RUNNING until the PCM is reopened. On the M2 Mac mini (firmware 27.2) this happens after every short system sleep. The HDMI monitor drops HPD for ~1.7 s as it wakes, and ~10.7 s after the resume the firmware announces DCPAVAudioInterface on a new channel. A stream started in between stops after ~1.5 s. Record the instance a stream's link starts on, read under the lock the start call takes the instance with. Each newly opened instance connects the card; a stream still running on an older instance then ends as an xrun, and userspace prepares it again on the successor. A stream playing into the void (no sink) has no link and is left alone. The instance comes from the service's own generation count, not from a firmware or board test. Signed-off-by: Ryan Murray --- drivers/gpu/drm/apple/audio-legacy-test.c | 101 +++++++++++++++++++++- drivers/gpu/drm/apple/audio.c | 47 +++++++++- drivers/gpu/drm/apple/audio.h | 2 +- drivers/gpu/drm/apple/av.c | 16 +++- 4 files changed, 159 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/apple/audio-legacy-test.c b/drivers/gpu/drm/apple/audio-legacy-test.c index fb082c244ee2..99249c702a36 100644 --- a/drivers/gpu/drm/apple/audio-legacy-test.c +++ b/drivers/gpu/drm/apple/audio-legacy-test.c @@ -20,6 +20,8 @@ struct dcpaud_legacy_test { /* The runtime PM usage count when the terminate was waited for */ int usage_at_drain; int startlink_err; + /* The published service instance, as dcp_audiosrv_instance() reports it */ + u64 instance; size_t elements_asked; size_t catalog; struct device *dev; @@ -76,8 +78,10 @@ static enum dma_status dcpaud_legacy_status(struct dma_chan *chan, dma_cookie_t return DMA_COMPLETE; } -static int dcpaud_legacy_startlink(struct device *dev, struct dcp_sound_cookie *cookie) +static int dcpaud_legacy_startlink(struct device *dev, struct dcp_sound_cookie *cookie, u64 *gen) { + if (gen) + *gen = dcpaud_legacy_current->instance; dcpaud_legacy_record('T'); return dcpaud_legacy_current->startlink_err; } @@ -241,6 +245,96 @@ static void dcpaud_legacy_void_takes_no_reference(struct kunit *test) KUNIT_EXPECT_EQ(test, dcpaud_legacy_usage(audio), 0); } +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) +static u64 dcpaud_legacy_instance(struct device *dev) +{ + return dcpaud_legacy_current->instance; +} + +/* A started stream, RUNNING as the PCM core has it, on service instance 5 */ +static struct snd_pcm_substream *dcpaud_legacy_running(struct kunit *test, + struct dcp_audio **audio) +{ + struct snd_pcm_substream *sub = dcpaud_legacy_setup(test, audio); + struct snd_pcm_runtime *runtime = sub->runtime; + + runtime->status = kunit_kzalloc(test, sizeof(*runtime->status), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, runtime->status); + init_waitqueue_head(&runtime->sleep); + init_waitqueue_head(&runtime->tsleep); + sub->ops = &dcp_playback_ops; + INIT_WORK(&(*audio)->replaced_work, dcpaud_replaced_work); + dcpaud_legacy_current->instance = 5; + kunit_activate_static_stub(test, dcp_audiosrv_instance, dcpaud_legacy_instance); + KUNIT_ASSERT_EQ(test, dcp_pcm_trigger(sub, SNDRV_PCM_TRIGGER_START), 0); + runtime->state = SNDRV_PCM_STATE_RUNNING; + runtime->status->state = SNDRV_PCM_STATE_RUNNING; + return sub; +} + +/* + * The firmware published a successor of the service with the sink still + * connected: the stream's link went with the old instance. The connect that + * follows the successor's Open ends the stream as an xrun (DMA terminate and + * link stop, as on any STOP); sync_stop then puts the PM reference. + */ +static void dcpaud_legacy_replaced_instance_ends_stream(struct kunit *test) +{ + struct dcp_audio *audio; + struct snd_pcm_substream *sub = dcpaud_legacy_running(test, &audio); + + KUNIT_EXPECT_EQ(test, audio->start_gen, 5ULL); + dcpaud_legacy_current->instance = 6; + dcpaud_replaced_work(&audio->replaced_work); + KUNIT_EXPECT_EQ(test, (int)sub->runtime->state, (int)SNDRV_PCM_STATE_XRUN); + KUNIT_EXPECT_STREQ(test, dcpaud_legacy_current->sequence, "TQXS"); + KUNIT_ASSERT_EQ(test, dcp_pcm_sync_stop(sub), 0); + KUNIT_EXPECT_EQ(test, dcpaud_legacy_usage(audio), 0); + + /* Prepared again by userspace, the next START is the successor's */ + KUNIT_ASSERT_EQ(test, dcp_pcm_trigger(sub, SNDRV_PCM_TRIGGER_START), 0); + KUNIT_EXPECT_EQ(test, audio->start_gen, 6ULL); + KUNIT_ASSERT_EQ(test, dcp_pcm_trigger(sub, SNDRV_PCM_TRIGGER_STOP), 0); + KUNIT_ASSERT_EQ(test, dcp_pcm_sync_stop(sub), 0); +} + +/* A connect with the stream's own instance still published leaves it playing */ +static void dcpaud_legacy_same_instance_keeps_stream(struct kunit *test) +{ + struct dcp_audio *audio; + struct snd_pcm_substream *sub = dcpaud_legacy_running(test, &audio); + + dcpaud_replaced_work(&audio->replaced_work); + KUNIT_EXPECT_EQ(test, (int)sub->runtime->state, (int)SNDRV_PCM_STATE_RUNNING); + KUNIT_EXPECT_STREQ(test, dcpaud_legacy_current->sequence, "TQ"); + KUNIT_ASSERT_EQ(test, dcp_pcm_trigger(sub, SNDRV_PCM_TRIGGER_STOP), 0); + KUNIT_ASSERT_EQ(test, dcp_pcm_sync_stop(sub), 0); + KUNIT_EXPECT_EQ(test, dcpaud_legacy_usage(audio), 0); +} + +/* A stream playing into the void has no link to lose */ +static void dcpaud_legacy_replaced_instance_keeps_void(struct kunit *test) +{ + struct dcp_audio *audio; + struct snd_pcm_substream *sub = dcpaud_legacy_setup(test, &audio); + + sub->runtime->status = kunit_kzalloc(test, sizeof(*sub->runtime->status), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, sub->runtime->status); + sub->ops = &dcp_playback_ops; + INIT_WORK(&audio->replaced_work, dcpaud_replaced_work); + kunit_activate_static_stub(test, dcp_audiosrv_instance, dcpaud_legacy_instance); + audio->open_unplugged = true; + sub->runtime->period_size = 480000; + KUNIT_ASSERT_EQ(test, dcp_pcm_trigger(sub, SNDRV_PCM_TRIGGER_START), 0); + sub->runtime->state = SNDRV_PCM_STATE_RUNNING; + dcpaud_legacy_current->instance = 6; + dcpaud_replaced_work(&audio->replaced_work); + KUNIT_EXPECT_EQ(test, (int)sub->runtime->state, (int)SNDRV_PCM_STATE_RUNNING); + KUNIT_ASSERT_EQ(test, dcp_pcm_trigger(sub, SNDRV_PCM_TRIGGER_STOP), 0); + KUNIT_ASSERT_EQ(test, dcp_pcm_sync_stop(sub), 0); +} +#endif + static int dcpaud_legacy_get_elements(struct device *dev, void *elements, size_t maxsize) { struct dcpaud_legacy_test *t = dcpaud_legacy_current; @@ -291,6 +385,11 @@ static struct kunit_case dcpaud_legacy_cases[] = { KUNIT_CASE(dcpaud_legacy_failed_start), KUNIT_CASE(dcpaud_legacy_void_takes_no_reference), KUNIT_CASE(dcpaud_legacy_elements_capacity), +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + KUNIT_CASE(dcpaud_legacy_replaced_instance_ends_stream), + KUNIT_CASE(dcpaud_legacy_same_instance_keeps_stream), + KUNIT_CASE(dcpaud_legacy_replaced_instance_keeps_void), +#endif {} }; diff --git a/drivers/gpu/drm/apple/audio.c b/drivers/gpu/drm/apple/audio.c index d425e0fe88b9..4f68fdce1c39 100644 --- a/drivers/gpu/drm/apple/audio.c +++ b/drivers/gpu/drm/apple/audio.c @@ -108,6 +108,12 @@ struct dcp_audio { struct delayed_work void_work; /* The stream holds a runtime PM reference, put in sync_stop; stream lock */ bool pm_held; + /* The service instance the stream's link started on; stream lock */ + u64 start_gen; +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + /* Ends a stream on a replaced instance, see dcpaud_replaced_work() */ + struct work_struct replaced_work; +#endif void *elements; void *productattrs; @@ -738,7 +744,7 @@ static int dcp_pcm_trigger(struct snd_pcm_substream *substream, int cmd) dcpaud->pm_held = true; } ret = dcp_audiosrv_startlink(dcpaud->dcp_dev, - &dcpaud->selected_cookie); + &dcpaud->selected_cookie, &dcpaud->start_gen); if (ret < 0) return ret; break; @@ -1905,6 +1911,36 @@ static int dcpaud_init_snd_card(struct dcp_audio *dcpaud) return ret; } +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) +/* + * The firmware publishes a successor of the audio service while the display + * link is set up again, also with the sink connected throughout (after a + * wake, the monitor's HPD pulse). The successor comes up plugged, so the card + * sees no unplug; the link a running stream started on went with the old + * instance, its DMA stops and the stream would wait forever. It ends as an + * xrun instead, and userspace prepares it again on the successor. + */ +static void dcpaud_replaced_work(struct work_struct *work) +{ + struct dcp_audio *dcpaud = container_of(work, struct dcp_audio, replaced_work); + struct snd_pcm_substream *substream = dcpaud->substream; + + if (!substream || !dcpaud->dcp_dev) + return; + /* + * Under the stream lock (a mutex: the PCM is nonatomic), as START + * takes the instance under it: no START completes in between. + */ + snd_pcm_stream_lock_irq(substream); + if (substream->runtime && snd_pcm_running(substream) && !dcpaud->void_running && + dcpaud->start_gen != dcp_audiosrv_instance(dcpaud->dcp_dev)) { + dev_dbg(dcpaud->dev, "stream's link went with its service\n"); + snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); + } + snd_pcm_stream_unlock_irq(substream); +} +#endif + void dcpaud_connect(struct platform_device *pdev, bool connected) { struct dcp_audio *dcpaud = platform_get_drvdata(pdev); @@ -1918,6 +1954,11 @@ void dcpaud_connect(struct platform_device *pdev, bool connected) mutex_lock(&dcpaud->data_lock); dcpaud_report_hotplug(dcpaud, connected, eld); +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + /* A connect follows each newly opened instance; see dcpaud_replaced_work() */ + if (connected && !dcpaud->native_link && dcpaud->card) + schedule_work(&dcpaud->replaced_work); +#endif } /* @@ -2075,6 +2116,9 @@ static void dcpaud_comp_unbind(struct device *dev, struct device *main, { struct dcp_audio *dcpaud = dev_get_drvdata(dev); +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + cancel_work_sync(&dcpaud->replaced_work); +#endif /* snd_card_free_when_closed() checks for NULL */ snd_card_free_when_closed(dcpaud->card); @@ -2143,6 +2187,7 @@ static int dcpaud_probe(struct platform_device *pdev) #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) mutex_init(&dcpaud->link_lock); INIT_WORK(&dcpaud->stale_work, dcpaud_stale_work); + INIT_WORK(&dcpaud->replaced_work, dcpaud_replaced_work); #endif INIT_DELAYED_WORK(&dcpaud->void_work, dcpaud_void_work); platform_set_drvdata(pdev, dcpaud); diff --git a/drivers/gpu/drm/apple/audio.h b/drivers/gpu/drm/apple/audio.h index 6d60750cc315..189abae53b07 100644 --- a/drivers/gpu/drm/apple/audio.h +++ b/drivers/gpu/drm/apple/audio.h @@ -8,7 +8,7 @@ struct platform_device; struct dcp_sound_cookie; int dcp_audiosrv_prepare(struct device *dev, struct dcp_sound_cookie *cookie); -int dcp_audiosrv_startlink(struct device *dev, struct dcp_sound_cookie *cookie); +int dcp_audiosrv_startlink(struct device *dev, struct dcp_sound_cookie *cookie, u64 *gen); int dcp_audiosrv_stoplink(struct device *dev); int dcp_audiosrv_unprepare(struct device *dev); int dcp_audiosrv_get_elements(struct device *dev, void *elements, size_t maxsize); diff --git a/drivers/gpu/drm/apple/av.c b/drivers/gpu/drm/apple/av.c index c147284af240..3496f5ccdfb5 100644 --- a/drivers/gpu/drm/apple/av.c +++ b/drivers/gpu/drm/apple/av.c @@ -264,16 +264,21 @@ int dcp_audiosrv_prepare(struct device *dev, struct dcp_sound_cookie *cookie) return ret; } -int dcp_audiosrv_startlink(struct device *dev, struct dcp_sound_cookie *cookie) +/* @gen: the instance the link starts on (dcp_audiosrv_instance()), or NULL */ +int dcp_audiosrv_startlink(struct device *dev, struct dcp_sound_cookie *cookie, u64 *gen) { struct apple_dcp *dcp; struct audiosrv_data *asrv; int ret; - KUNIT_STATIC_STUB_REDIRECT(dcp_audiosrv_startlink, dev, cookie); + KUNIT_STATIC_STUB_REDIRECT(dcp_audiosrv_startlink, dev, cookie, gen); dcp = dev_get_drvdata(dev); asrv = dcp->audiosrv; down_write(&asrv->srv_rwsem); +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + if (gen) + *gen = asrv->gen; +#endif ret = afk_service_call(asrv->srv, 0, asrv->cmds.start_link, cookie, sizeof(*cookie), 64 - sizeof(*cookie), NULL, 0, 64); @@ -361,9 +366,12 @@ static int av_audiosrv_call(struct apple_epic_service *srv, u32 command, const v /* The audio service instance now published, as dcp_audiosrv_replaced() takes it. */ u64 dcp_audiosrv_instance(struct device *dev) { - struct apple_dcp *dcp = dev_get_drvdata(dev); - struct audiosrv_data *asrv = dcp->audiosrv; + struct apple_dcp *dcp; + struct audiosrv_data *asrv; + KUNIT_STATIC_STUB_REDIRECT(dcp_audiosrv_instance, dev); + dcp = dev_get_drvdata(dev); + asrv = dcp->audiosrv; if (!asrv) return 0; guard(rwsem_read)(&asrv->srv_rwsem); From efa194704a2919b0c01c4725a3161c2463d52aca Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 08:38:27 +1100 Subject: [PATCH 2/2] drm/apple: audio: queue the replaced-instance work only while bound The DCP unbinds after its DP audio component and may still connect the card meanwhile. Cancelling the work at unbind left it free to be queued again by such a connect, and the work then acted on the substream of a card being freed. Keep the work disabled outside a bound card: it starts disabled, is enabled once the card is registered, and is disabled (waiting for a running one) at unbind before the card is freed. A connect while it is disabled queues nothing. Signed-off-by: Ryan Murray --- drivers/gpu/drm/apple/audio.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/apple/audio.c b/drivers/gpu/drm/apple/audio.c index 4f68fdce1c39..d2333b9340b3 100644 --- a/drivers/gpu/drm/apple/audio.c +++ b/drivers/gpu/drm/apple/audio.c @@ -113,6 +113,8 @@ struct dcp_audio { #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) /* Ends a stream on a replaced instance, see dcpaud_replaced_work() */ struct work_struct replaced_work; + /* replaced_work is enabled: the card is bound, see dcpaud_comp_unbind() */ + bool replaced_enabled; #endif void *elements; void *productattrs; @@ -2097,6 +2099,11 @@ static int dcpaud_comp_bind(struct device *dev, struct device *main, void *data) ret = dcpaud_init_snd_card(dcpaud); if (!ret) { +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + /* Connects may queue it from now on, with a card to act on. */ + enable_work(&dcpaud->replaced_work); + dcpaud->replaced_enabled = true; +#endif dcpaud_expose_debugfs_blob(dcpaud, "selected_cookie", &dcpaud->selected_cookie, sizeof(dcpaud->selected_cookie)); dcpaud_expose_debugfs_blob(dcpaud, "elements", dcpaud->elements, @@ -2117,7 +2124,15 @@ static void dcpaud_comp_unbind(struct device *dev, struct device *main, struct dcp_audio *dcpaud = dev_get_drvdata(dev); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) - cancel_work_sync(&dcpaud->replaced_work); + /* + * The DCP unbinds after its audio and may still connect: a connect + * from now on queues nothing, and a queued or running work ends + * before the card goes. + */ + if (dcpaud->replaced_enabled) { + disable_work_sync(&dcpaud->replaced_work); + dcpaud->replaced_enabled = false; + } #endif /* snd_card_free_when_closed() checks for NULL */ snd_card_free_when_closed(dcpaud->card); @@ -2188,6 +2203,8 @@ static int dcpaud_probe(struct platform_device *pdev) mutex_init(&dcpaud->link_lock); INIT_WORK(&dcpaud->stale_work, dcpaud_stale_work); INIT_WORK(&dcpaud->replaced_work, dcpaud_replaced_work); + /* Enabled once the card is bound, see dcpaud_comp_bind() */ + disable_work(&dcpaud->replaced_work); #endif INIT_DELAYED_WORK(&dcpaud->void_work, dcpaud_void_work); platform_set_drvdata(pdev, dcpaud);