diff --git a/drivers/gpu/drm/apple/audio.c b/drivers/gpu/drm/apple/audio.c index 1d6c5f73627d6..7ffc144811901 100644 --- a/drivers/gpu/drm/apple/audio.c +++ b/drivers/gpu/drm/apple/audio.c @@ -410,6 +410,9 @@ static int dcp_pcm_open(struct snd_pcm_substream *substream) .flags = SND_DMAENGINE_PCM_DAI_FLAG_PACK, }; struct snd_pcm_hardware hw; +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + u64 gen; +#endif int ret; mutex_lock(&dcpaud->data_lock); @@ -428,7 +431,48 @@ static int dcp_pcm_open(struct snd_pcm_substream *substream) goto refine; } +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + gen = dcp_audiosrv_instance(dcpaud->dcp_dev); +#endif ret = dcpaud_read_remote_info(dcpaud); +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + /* + * The firmware announces the audio service again while the display + * link is set up again (a modeset, at boot), and refuses calls to the + * instance it is replacing meanwhile. Userspace probes the PCM once + * and drops an output whose open fails, so read from the successor. + * The sink is taken as the successor reports it. A refusal is -EINVAL + * (a non-zero retcode); a missing service, a timeout or no memory is + * not waited out. + */ + if (ret == -EINVAL && dcp_audiosrv_replaced(dcpaud->dcp_dev, gen)) { + dev_dbg(dcpaud->dev, "sink info read failed on a replaced instance (%d), retrying\n", + ret); + mutex_lock(&dcpaud->data_lock); + dcpaud->open_unplugged = !dcpaud->connected; + dcpaud->open_cookie = dcpaud->connection_cookie; + mutex_unlock(&dcpaud->data_lock); + if (dcpaud->open_unplugged) { + hw = dcp_pcm_hw_unplugged; + goto refine; + } + ret = dcpaud_read_remote_info(dcpaud); + } +#endif + if (ret == -ENODEV) { + /* + * No instance answers: the service went and its unplug is on the + * way. Open as unplugged, as before the plug, and with a cookie + * no connection has, so that no stream op of this open takes the + * sink as connected. + */ + mutex_lock(&dcpaud->data_lock); + dcpaud->open_unplugged = true; + dcpaud->open_cookie = dcpaud->connection_cookie - 1; + mutex_unlock(&dcpaud->data_lock); + hw = dcp_pcm_hw_unplugged; + goto refine; + } if (ret < 0) return ret; diff --git a/drivers/gpu/drm/apple/audio.h b/drivers/gpu/drm/apple/audio.h index 0ba2ba225af5e..baf83a50eea61 100644 --- a/drivers/gpu/drm/apple/audio.h +++ b/drivers/gpu/drm/apple/audio.h @@ -47,6 +47,9 @@ struct dcp_audio_link_format { bool dcp_audiosrv_link_by_format(struct device *dev); bool dcp_audiosrv_native_link(struct device *dev); +/* The published audio service instance; whether it was replaced since @gen */ +u64 dcp_audiosrv_instance(struct device *dev); +bool dcp_audiosrv_replaced(struct device *dev, u64 gen); int dcp_audiosrv_open(struct device *dev); void dcp_audiosrv_close(struct device *dev); int dcp_audiosrv_link(struct device *dev, enum dcp_audio_link op, diff --git a/drivers/gpu/drm/apple/av-lifetime-test.c b/drivers/gpu/drm/apple/av-lifetime-test.c index 1a466dfbbaca9..621f06c2ce887 100644 --- a/drivers/gpu/drm/apple/av-lifetime-test.c +++ b/drivers/gpu/drm/apple/av-lifetime-test.c @@ -160,6 +160,7 @@ static struct av_life_test *av_life_setup(struct kunit *test, work_func_t owner_ init_waitqueue_head(&t->asrv->gen_wq); #endif INIT_WORK(&t->asrv->start_av_service_wq, owner_fn); + INIT_DELAYED_WORK(&t->asrv->open_check_wq, owner_fn); INIT_WORK(&t->asrv->terminate_wq, owner_fn); INIT_WORK(&t->dcp->external_audio_work, av_life_owner_fn); t->asrv->works_live = true; diff --git a/drivers/gpu/drm/apple/av.c b/drivers/gpu/drm/apple/av.c index efca053134e1a..038ab36454178 100644 --- a/drivers/gpu/drm/apple/av.c +++ b/drivers/gpu/drm/apple/av.c @@ -65,6 +65,9 @@ static const struct dcp_av_audio_cmds dcp_av_audio_cmds_v13_5 = { .get_product_attrs = 18, }; +/* How long an Open refused by an instance being replaced waits for the next one */ +#define AV_AUDIOSRV_REPLACE_MS 1000 + /* * Firmware 27's external role (the T6050 DCPEXTs), as macOS 27 drives it on * the M5 Pro: the 13.5 numbers without the started and stopping @@ -133,6 +136,13 @@ struct audiosrv_data { bool warned_get_elements; bool warned_get_product_attrs; bool is_open; + /* + * A classic Open the firmware refused (-EINVAL), judged once the + * instance has had time to be replaced (av_work_open_check()). + */ + struct delayed_work open_check_wq; + struct device *open_check_dev; + int open_refused; /* INIT_WORK has run for the embedded owner works. */ bool works_live; /* Report and service-start admission. Writers hold srv_rwsem. */ @@ -185,9 +195,9 @@ static void av_audiosrv_init(struct apple_epic_service *service, const char *nam down_write(&asrv->srv_rwsem); old = asrv->srv; - asrv->srv = service; + WRITE_ONCE(asrv->srv, service); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) - asrv->gen++; + WRITE_ONCE(asrv->gen, asrv->gen + 1); #endif queue_start = !asrv->reports_stopped; up_write(&asrv->srv_rwsem); @@ -198,6 +208,8 @@ static void av_audiosrv_init(struct apple_epic_service *service, const char *nam afk_service_put(old); asrv->plugged = true; + /* A classic Open is its instance's: the one published now is opened afresh. */ + asrv->is_open = false; /* A new instance, maybe a new sink: its catalog is asked for afresh. */ WRITE_ONCE(asrv->elements_empty, false); mutex_unlock(&asrv->plug_lock); @@ -216,9 +228,9 @@ static void av_audiosrv_teardown(struct apple_epic_service *service) down_write(&asrv->srv_rwsem); owned = asrv->srv == service; if (owned) { - asrv->srv = NULL; + WRITE_ONCE(asrv->srv, NULL); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) - asrv->gen++; + WRITE_ONCE(asrv->gen, asrv->gen + 1); #endif } up_write(&asrv->srv_rwsem); @@ -226,6 +238,7 @@ static void av_audiosrv_teardown(struct apple_epic_service *service) /* A stale generation must not unplug its successor. */ if (owned) { asrv->plugged = false; + asrv->is_open = false; if (asrv->audio_dev) dcpaud_disconnect(asrv->audio_dev); } @@ -339,8 +352,37 @@ static int av_audiosrv_call(struct apple_epic_service *srv, u32 command, const v return afk_service_call(srv, 0, command, body, body_len, body_pad, reply, reply_len, 0); } -/* How long an Open refused by an instance being replaced waits for the next one */ -#define AV_AUDIOSRV_REPLACE_MS 1000 +/* 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; + + if (!asrv) + return 0; + guard(rwsem_read)(&asrv->srv_rwsem); + return asrv->gen; +} + +/* + * Whether instance @gen has a published successor, waiting for one as + * dcp_audiosrv_open() does: the firmware refuses calls to an instance it is + * replacing, and publishes the successor within some hundred milliseconds. + */ +bool dcp_audiosrv_replaced(struct device *dev, u64 gen) +{ + struct apple_dcp *dcp = dev_get_drvdata(dev); + struct audiosrv_data *asrv = dcp->audiosrv; + + if (!asrv) + return false; + if (wait_event_interruptible_timeout(asrv->gen_wq, + READ_ONCE(asrv->srv) && READ_ONCE(asrv->gen) != gen, + msecs_to_jiffies(AV_AUDIOSRV_REPLACE_MS)) < 0) + return false; + guard(rwsem_read)(&asrv->srv_rwsem); + return asrv->srv && asrv->gen != gen; +} /* * The 14.7 external role: the service is open while a PCM is (see @@ -1039,10 +1081,19 @@ static const struct apple_epic_service_ops avep_ops[] = { {} }; +/* Whether @service is the published instance; plug_lock held, then srv_rwsem */ +static bool av_audiosrv_published(struct audiosrv_data *asrv, + struct apple_epic_service *service) +{ + guard(rwsem_read)(&asrv->srv_rwsem); + return asrv->srv == service; +} + void av_service_connect(struct apple_dcp *dcp) { struct apple_epic_service *service; struct audiosrv_data *asrv = dcp->audiosrv; + bool opened; int ret; /* @@ -1061,7 +1112,9 @@ void av_service_connect(struct apple_dcp *dcp) /* open AV audio service */ dev_info(dcp->dev, "%s: starting audio service, plugged:%d\n", __func__, asrv->plugged); - if (asrv->is_open) + scoped_guard(mutex, &asrv->plug_lock) + opened = asrv->is_open; + if (opened) goto out; #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) @@ -1096,16 +1149,34 @@ void av_service_connect(struct apple_dcp *dcp) ret = afk_service_call(service, 0, asrv->cmds.open, NULL, 0, 32, NULL, 0, 32); + if (ret == -EINVAL) { + /* + * The firmware refuses an Open on an instance it is replacing (a + * resume, a modeset), and publishes the successor, whose own + * connect opens it, within AV_AUDIOSRV_REPLACE_MS. Judge the + * refusal then, so a lasting one is still reported. + */ + WRITE_ONCE(asrv->open_refused, ret); + mod_delayed_work(system_wq, &asrv->open_check_wq, + msecs_to_jiffies(AV_AUDIOSRV_REPLACE_MS)); + goto out; + } if (ret) { dev_err(dcp->dev, "error opening audio service: %d\n", ret); goto out; } - mutex_lock(&asrv->plug_lock); - asrv->is_open = true; - - if (asrv->audio_dev) - dcpaud_connect(asrv->audio_dev, asrv->plugged); - mutex_unlock(&asrv->plug_lock); + /* + * The Open ran without locks: it is this instance's only if the + * instance is still the published one. A successor published meanwhile + * is opened by its own connect. + */ + scoped_guard(mutex, &asrv->plug_lock) { + if (av_audiosrv_published(asrv, service)) { + asrv->is_open = true; + if (asrv->audio_dev) + dcpaud_connect(asrv->audio_dev, asrv->plugged); + } + } out: afk_service_put(service); } @@ -1137,6 +1208,7 @@ static void __av_service_disconnect(struct apple_dcp *dcp, const u64 *gen) { struct apple_epic_service *service; struct audiosrv_data *asrv = dcp->audiosrv; + bool opened; int ret; /* A CEC-only endpoint has no audio owner (av_service_connect()). */ @@ -1188,16 +1260,15 @@ static void __av_service_disconnect(struct apple_dcp *dcp, const u64 *gen) goto out; } #endif - if (!asrv->is_open) + /* Only the published instance's Open is closed, and only once. */ + scoped_guard(mutex, &asrv->plug_lock) { + opened = asrv->is_open && av_audiosrv_published(asrv, service); + if (opened && asrv->audio_dev) + dcpaud_disconnect(asrv->audio_dev); + } + if (!opened) goto out; - mutex_lock(&asrv->plug_lock); - - if (asrv->audio_dev) - dcpaud_disconnect(asrv->audio_dev); - - mutex_unlock(&asrv->plug_lock); - ret = afk_service_call(service, 0, asrv->cmds.close, NULL, 0, 16, NULL, 0, 16); if (ret) { @@ -1205,7 +1276,11 @@ static void __av_service_disconnect(struct apple_dcp *dcp, const u64 *gen) } if (service->torndown) afk_service_disable(service); - asrv->is_open = false; + /* A successor published meanwhile keeps its own state. */ + scoped_guard(mutex, &asrv->plug_lock) { + if (av_audiosrv_published(asrv, service)) + asrv->is_open = false; + } out: afk_service_put(service); } @@ -1252,8 +1327,28 @@ static void av_work_service_start(struct work_struct *work) av_service_connect(dcp); } +/* A refused classic Open, AV_AUDIOSRV_REPLACE_MS later (av_service_connect()) */ +static void av_work_open_check(struct work_struct *work) +{ + struct audiosrv_data *asrv = + container_of(to_delayed_work(work), struct audiosrv_data, open_check_wq); + int ret = READ_ONCE(asrv->open_refused); + bool opened, gone; + + scoped_guard(mutex, &asrv->plug_lock) { + opened = asrv->is_open; + gone = !READ_ONCE(asrv->srv); + } + if (opened || gone) + dev_dbg(asrv->open_check_dev, "audio service Open refused (%d) by an instance since %s\n", + ret, opened ? "replaced" : "removed"); + else + dev_err(asrv->open_check_dev, "error opening audio service: %d\n", ret); +} + static void av_audiosrv_flush_owner(struct audiosrv_data *asrv) { + flush_delayed_work(&asrv->open_check_wq); flush_work(&asrv->start_av_service_wq); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) flush_work(&asrv->terminate_wq); @@ -1282,6 +1377,7 @@ static bool av_audiosrv_drain_owner(struct audiosrv_data *asrv) */ if (first_stop) { disable_work_sync(&asrv->start_av_service_wq); + disable_delayed_work_sync(&asrv->open_check_wq); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) disable_work_sync(&asrv->terminate_wq); #endif @@ -1382,6 +1478,7 @@ void av_audiosrv_reports_restart(struct apple_dcp *dcp) } asrv->work_disabled = false; enable_work(&asrv->start_av_service_wq); + enable_delayed_work(&asrv->open_check_wq); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) enable_work(&asrv->terminate_wq); #endif @@ -1452,11 +1549,9 @@ int avep_init(struct apple_dcp *dcp) case DCP_FIRMWARE_V_26_6_DESKTOP: audiosrv_data->cmds = dcp_av_audio_cmds_v13_5; break; -#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) case DCP_FIRMWARE_V_27_0: audiosrv_data->cmds = dcp_av_audio_cmds_v27; break; -#endif default: #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) /* @@ -1476,6 +1571,8 @@ int avep_init(struct apple_dcp *dcp) } INIT_WORK(&audiosrv_data->start_av_service_wq, av_work_service_start); + INIT_DELAYED_WORK(&audiosrv_data->open_check_wq, av_work_open_check); + audiosrv_data->open_check_dev = dcp->dev; #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) INIT_WORK(&audiosrv_data->terminate_wq, av_work_service_terminate); mutex_init(&audiosrv_data->open_lock); diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index bd48c63790371..5908ef0d3ea3b 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1406,12 +1406,20 @@ static inline bool dcp_is_v14_7_external(const struct apple_dcp *dcp) } /* - * Firmware 27's external processors (the T6050 DCPEXTs): their DP/HDMI - * audio service is the desktop one, on endpoint 0x2b (see av.c). + * Firmware 27's external processors with its own DPTX endpoint layout (the + * T6050 and firmware-27 T6041 DCPEXTs, ports on DPTX_ENDPOINT_V2): their + * DP/HDMI audio service is the desktop one, on endpoint 0x2b (see av.c). + * + * A firmware-27 DCP on the classic layout (the T8112 DCPs, whose 27.2 + * firmware advertises its ports on DPTX_ENDPOINT, dcp_v27_dptx_layout()) + * has the classic audio service on AV_ENDPOINT: there 0x2b is HDCP only. + * The layout is read as set at probe, so that the DP audio driver, which + * binds before the DCP starts, sees the same role as the DCP. */ static inline bool dcp_is_v27_external(const struct apple_dcp *dcp) { - return dcp->fw_compat == DCP_FIRMWARE_V_27_0 && dcp->hw.num_dptx_ports; + return dcp->fw_compat == DCP_FIRMWARE_V_27_0 && dcp->hw.num_dptx_ports && + dcp->hw.dptx_endpoint == DPTX_ENDPOINT_V2; } /* The 14.7 internal panel (J514S/J516S), presented through DCPLink. */ diff --git a/tools/asahi/dcpaud-open-fallback-tests.py b/tools/asahi/dcpaud-open-fallback-tests.py new file mode 100755 index 0000000000000..b2a5befe567bb --- /dev/null +++ b/tools/asahi/dcpaud-open-fallback-tests.py @@ -0,0 +1,224 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Run the real dcp_pcm_open() over plugged, unplugged, replaced and gone sinks. + +PipeWire and PulseAudio probe a PCM once when its card appears and drop the +output for good if the open fails. dcp_pcm_open() opens an unplugged sink +with a fixed hardware description for that reason. The firmware refuses +calls to an audio service instance it is replacing (it announces the service +again while the display link is set up again): the open then reads from the +successor. A service that is gone opens as unplugged with a cookie no +connection has, so no later stream op (hw_free, native start) takes the sink as +connected. Any other failure still fails the open. + +The fixture compiles dcp_pcm_open() from drivers/gpu/drm/apple/audio.c against +mocks of the ALSA and DMA calls. --mutation names a change to the production +function that the matrix must kill. +""" +import argparse +import re +import subprocess +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +AUDIO = 'drivers/gpu/drm/apple/audio.c' + +MUTATIONS = { + # Fail the open when the instance is being replaced. + 'no-retry': ('\tif (ret == -EINVAL && dcp_audiosrv_replaced(dcpaud->dcp_dev, gen)) {', + '\tif (0 && gen) {'), + # Wait for a successor on any failure, not only a refusal. + 'wait-any': ('\tif (ret == -EINVAL && dcp_audiosrv_replaced(dcpaud->dcp_dev, gen)) {', + '\tif (ret < 0 && dcp_audiosrv_replaced(dcpaud->dcp_dev, gen)) {'), + # Fail the open when the service is gone. + 'gone-fails': ('\tif (ret == -ENODEV) {\n\t\t/*\n\t\t * No instance answers', + '\tif (0) {\n\t\t/*\n\t\t * No instance answers'), + # Keep the live cookie on a gone service: later ops take it as connected. + 'keep-cookie': ('\t\tdcpaud->open_cookie = dcpaud->connection_cookie - 1;\n', ''), + # Turn every failure into an unplugged open. + 'mask-all': ('\tif (ret == -ENODEV) {\n\t\t/*\n\t\t * No instance answers', + '\tif (ret < 0) {\n\t\t/*\n\t\t * No instance answers'), +} + +def block(text, start): + opening = text.index('{', start) + depth = 0 + for end in range(opening, len(text)): + depth += text[end] == '{' + depth -= text[end] == '}' + if not depth: + return text[start:end + 1] + raise AssertionError('unbalanced production block') + +def function(text, name): + match = re.search(r'\n(static [^\n;]*\b' + name + r'\([^;{}]*\)\n)\{', text) + assert match, name + return block(text, match.start(1)) + +HARNESS = r''' +#include +#include +#include +#include +#include +#define dev_dbg(...) ((void)0) +#define IS_ENABLED(x) 1 +typedef unsigned long long u64; +#define SNDRV_PCM_INFO_MMAP 1u +#define SNDRV_PCM_INFO_MMAP_VALID 2u +#define SNDRV_PCM_INFO_INTERLEAVED 4u +#define SNDRV_PCM_INFO_RESUME 8u +#define SNDRV_PCM_HW_PARAM_FORMAT 0 +#define SNDRV_PCM_HW_PARAM_CHANNELS 1 +#define SNDRV_PCM_HW_PARAM_RATE 2 +#define SND_DMAENGINE_PCM_DAI_FLAG_PACK 1 +#define DCPAUD_PERIODS_MAX 64 +#define UNPLUGGED_FORMATS 0x700u +#define PLUGGED_FORMATS 0x7u +struct device { int unused; }; +struct mutex { int held; }; +static void mutex_lock(struct mutex *m) { m->held++; } +static void mutex_unlock(struct mutex *m) { m->held--; } +struct snd_pcm_hardware { unsigned int info; unsigned long long formats; unsigned int channels_min, channels_max; + unsigned int periods_min, periods_max; size_t period_bytes_min, period_bytes_max, buffer_bytes_max, fifo_size; }; +struct snd_pcm_runtime { struct snd_pcm_hardware hw; }; +struct snd_pcm { void *private_data; }; +struct snd_pcm_substream { struct snd_pcm *pcm; struct snd_pcm_runtime *runtime; }; +struct snd_dmaengine_dai_dma_data { unsigned int flags; }; +struct dma_chan { int unused; }; +struct dcp_audio { struct device *dev, *dcp_dev; struct mutex data_lock; bool connected, open_unplugged; + unsigned int open_cookie, connection_cookie; struct dma_chan *chan; }; +static const struct snd_pcm_hardware dcp_pcm_hw = { .formats = PLUGGED_FORMATS }; +static const struct snd_pcm_hardware dcp_pcm_hw_unplugged = { .formats = UNPLUGGED_FORMATS }; +static int init_error, info_errors[2], reads, rules, dma_opens; +static bool replaced, successor_connected; +static struct dcp_audio *cur; +static int dcpaud_init_dma(struct dcp_audio *d) { return init_error; } +static int dcpaud_read_remote_info(struct dcp_audio *d) { return info_errors[reads++ ? 1 : 0]; } +static u64 dcp_audiosrv_instance(struct device *d) { return 4; } +static bool dcp_audiosrv_replaced(struct device *d, u64 gen) +{ + if (gen != 4) + return false; + if (replaced) { + /* the teardown reported the unplug, the successor its sink (or not yet) */ + cur->connected = successor_connected; + cur->connection_cookie += successor_connected ? 1 : 0; + } + return replaced; +} +static int dcpaud_rule_format(void *p, void *r) { return 0; } +static int dcpaud_rule_channels(void *p, void *r) { return 0; } +static int dcpaud_rule_rate(void *p, void *r) { return 0; } +static int snd_pcm_hw_rule_add(struct snd_pcm_runtime *r, unsigned int c, int v, void *f, void *p, ...) +{ rules++; return 0; } +static int snd_dmaengine_pcm_refine_runtime_hwparams(struct snd_pcm_substream *s, + struct snd_dmaengine_dai_dma_data *d, struct snd_pcm_hardware *h, struct dma_chan *c) { return 0; } +static int snd_dmaengine_pcm_open(struct snd_pcm_substream *s, struct dma_chan *c) { dma_opens++; return 0; } +''' + +CHECKS = r''' +static int failures; +#define CHECK(c) do { if (!(c)) { printf("FAIL line %d (%s: %s)\n", __LINE__, name, #c); failures++; } } while (0) + +static int run(bool connected, int ierr, int e0, int e1, bool repl, bool succ, + struct dcp_audio *d, struct snd_pcm_runtime *rt) +{ + static struct snd_pcm pcm; + struct snd_pcm_substream s = { .pcm = &pcm, .runtime = rt }; + + *d = (struct dcp_audio){ .connected = connected, .connection_cookie = 7 }; + *rt = (struct snd_pcm_runtime){ 0 }; + pcm.private_data = d; + cur = d; + init_error = ierr; info_errors[0] = e0; info_errors[1] = e1; + replaced = repl; successor_connected = succ; + reads = rules = dma_opens = 0; + return dcp_pcm_open(&s); +} + +int main(void) +{ + struct dcp_audio d; + struct snd_pcm_runtime rt; + const char *name; + + name = "plugged"; + CHECK(run(true, 0, 0, 0, false, false, &d, &rt) == 0); + CHECK(!d.open_unplugged && rules == 3 && dma_opens == 1 && reads == 1); + CHECK(rt.hw.formats == PLUGGED_FORMATS && dcpaud_connection_up(&d) && !d.data_lock.held); + + name = "unplugged"; + CHECK(run(false, 0, 0, 0, false, false, &d, &rt) == 0); + CHECK(d.open_unplugged && !rules && !reads && rt.hw.formats == UNPLUGGED_FORMATS); + CHECK(!dcpaud_connection_up(&d)); + + name = "replaced, successor plugged"; + CHECK(run(true, 0, -EINVAL, 0, true, true, &d, &rt) == 0); + CHECK(!d.open_unplugged && reads == 2 && rules == 3 && dma_opens == 1); + CHECK(rt.hw.formats == PLUGGED_FORMATS && dcpaud_connection_up(&d) && d.open_cookie == 8); + + name = "replaced, successor not yet plugged"; + CHECK(run(true, 0, -EINVAL, 0, true, false, &d, &rt) == 0); + CHECK(d.open_unplugged && reads == 1 && !rules && !dcpaud_connection_up(&d)); + + name = "replaced, successor refuses too"; + CHECK(run(true, 0, -EINVAL, -EINVAL, true, true, &d, &rt) == -EINVAL); + CHECK(!dma_opens); + + name = "stable service refuses"; + CHECK(run(true, 0, -EINVAL, 0, false, false, &d, &rt) == -EINVAL); + CHECK(!dma_opens && reads == 1); + + name = "persistent error"; + CHECK(run(true, 0, -ENOMEM, 0, false, false, &d, &rt) == -ENOMEM); + CHECK(!dma_opens); + + name = "no memory, a successor published: not waited for"; + CHECK(run(true, 0, -ENOMEM, 0, true, true, &d, &rt) == -ENOMEM); + CHECK(!dma_opens && reads == 1); + + name = "service gone"; + CHECK(run(true, 0, -ENODEV, 0, false, false, &d, &rt) == 0); + CHECK(d.open_unplugged && !rules && dma_opens == 1 && rt.hw.formats == UNPLUGGED_FORMATS); + /* hw_free and native start test only this: the sink is not this open's */ + CHECK(!dcpaud_connection_up(&d) && !d.data_lock.held); + + name = "no-dma"; + CHECK(run(true, -EPROBE_DEFER_, 0, 0, false, false, &d, &rt) == -EPROBE_DEFER_); + CHECK(!dma_opens && !d.data_lock.held); + + if (failures) + return 1; + puts("PASS dcp_pcm_open: plugged, unplugged, replaced instance (successor plugged, not yet, " + "refusing), stable refusal and persistent error fail, gone service opens unplugged " + "and never connected, DMA failure fails"); + return 0; +} +''' + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--mutation', choices=sorted(MUTATIONS)) + args = parser.parse_args() + + source = (ROOT / AUDIO).read_text() + if args.mutation: + old, new = MUTATIONS[args.mutation] + assert source.count(old) == 1, args.mutation + source = source.replace(old, new) + + code = (HARNESS + '#define EPROBE_DEFER_ 517\n' + function(source, 'dcpaud_connection_up') + + function(source, 'dcp_pcm_open') + CHECKS) + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'check.c' + path.write_text(code) + subprocess.run(['clang', '-std=gnu11', '-Wall', '-Werror', '-Wno-unused-function', + '-Wno-unreachable-code', '-fsanitize=address,undefined', str(path), + '-o', str(path.with_suffix(''))], check=True) + sys.exit(subprocess.run([str(path.with_suffix(''))]).returncode) + +if __name__ == '__main__': + main() diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 874ddbc5e1d1a..662a9e73af43f 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -181,6 +181,8 @@ def mutations(flag, names, base, expect=KILL): 'missing-control-firmware', 'missing-control-descriptor', 'missing-owner-ipc', 'missing-owner-firmware', 'missing-owner-descriptor', 'missing-owner-arena'), out()), + # A DP audio PCM open reads a replaced service's successor; a gone service opens unplugged. + 'dcpaud-open-fallback-tests.py': [([], PASS)] + mutations('--mutation', ('no-retry', 'wait-any', 'gone-fails', 'keep-cookie', 'mask-all'), []), # a7638d51880c, the parent of the fix, ran pm_runtime_force_suspend in the late # slot, so the dp-audio domain dropped under a running DCP. 'dcpaud-sleep-hold-tests.py': [(out(), PASS)] diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/audio.py b/tools/testing/selftests/drivers/gpu/apple-dcp/audio.py index 39d02e69271dd..25524680b7ea5 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/audio.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/audio.py @@ -124,6 +124,7 @@ def cec_only_endpoint(root): source = (root / 'drivers/gpu/drm/apple/av.c').read_text() prefix = r''' #include +#include #include #include #include @@ -134,18 +135,22 @@ def cec_only_endpoint(root): #define READ_ONCE(x) (x) #define WRITE_ONCE(x, v) ((x) = (v)) #define dev_info(d, ...) ((void)(d)) -#define dev_err(d, ...) ((void)(d)) +static unsigned errs, armed; +#define dev_err(d, ...) ((void)(d), errs++) +#define AV_AUDIOSRV_REPLACE_MS 1000 +static int system_wq; +static unsigned long msecs_to_jiffies(unsigned m) {return m;} +#define mod_delayed_work(wq, w, j) ((void)(wq), (void)(w), (void)(j), armed++) struct device {int unused;}; struct platform_device {int unused;}; struct lock {int held;}; static unsigned takes; static struct lock *lock_take(struct lock *l) {assert(!l->held); l->held = 1; takes++; return l;} static void lock_drop(struct lock **l) {if (*l) (*l)->held = 0;} +#define guard(type) struct lock *_g __attribute__((cleanup(lock_drop))) = lock_take #define scoped_guard(type, l) \ for (struct lock *_guard __attribute__((cleanup(lock_drop))) = lock_take(l), *_once = _guard; \ _once; _once = NULL) -static void mutex_lock(struct lock *l) {lock_take(l);} -static void mutex_unlock(struct lock *l) {l->held = 0;} struct apple_epic_service {bool torndown; unsigned pins;}; struct apple_dcp_afkep {int endpoint;}; struct audiosrv_data { @@ -155,9 +160,14 @@ def cec_only_endpoint(root): u64 gen, v27_boot_gen; struct platform_device *audio_dev; struct {u32 open, close;} cmds; + int open_check_wq, open_refused; + struct device *open_check_dev; }; struct apple_dcp {struct device *dev; struct apple_dcp_afkep *avep; struct audiosrv_data *audiosrv;}; static bool native, v27; +static int call_ret, hook; +static struct audiosrv_data *hook_asrv; +static struct apple_epic_service *hook_next; static unsigned calls, last_cmd, connects, disconnects, releases, closes, v27_starts; static bool dcp_audiosrv_native_link(struct device *dev) {(void)dev; return native;} static bool dcp_is_v14_7_external(const struct apple_dcp *dcp) {(void)dcp; return false;} @@ -176,10 +186,18 @@ def cec_only_endpoint(root): size_t pad, void *out, size_t room, size_t out_pad) { (void)group; (void)data; (void)len; (void)pad; (void)out; (void)room; (void)out_pad; - assert(s && s->pins); calls++; last_cmd = cmd; return 0; + assert(s && s->pins); calls++; last_cmd = cmd; + /* A successor published while the RPC runs (and, at a Close, opened) */ + if (hook) { + hook_asrv->srv = hook_next; + hook_asrv->is_open = hook == 2; + hook = 0; + } + return call_ret; } ''' - return prefix + block(source, 'void av_service_connect(') + '\n' + block( + return prefix + block(source, 'static bool av_audiosrv_published(') + '\n' + block( + source, 'void av_service_connect(') + '\n' + block( source, 'static void __av_service_disconnect(') + '\n' + block( source, 'void av_service_disconnect(') + r''' int main(void) @@ -224,6 +242,41 @@ def cec_only_endpoint(root): av_service_connect(&dcp); av_service_disconnect(&dcp); assert(connects == 3 && disconnects == 2 && calls == 2); + + /* + * Classic link, the firmware refuses the Open (an instance being + * replaced): judged later, not reported now; any other error is. + */ + native = v27 = false; + asrv.srv = &srv; + call_ret = -EINVAL; + av_service_connect(&dcp); + assert(calls == 3 && !asrv.is_open && armed == 1 && !errs && asrv.open_refused == -EINVAL && + connects == 3); + call_ret = -ETIMEDOUT; + av_service_connect(&dcp); + assert(calls == 4 && !asrv.is_open && armed == 1 && errs == 1); + call_ret = 0; + av_service_connect(&dcp); + assert(calls == 5 && asrv.is_open && connects == 4); + + /* A Close whose instance is replaced meanwhile leaves the successor's Open. */ + struct apple_epic_service next = {0}; + hook_asrv = &asrv; + hook_next = &next; + hook = 2; + av_service_disconnect(&dcp); + assert(calls == 6 && last_cmd == 5 && asrv.srv == &next && asrv.is_open); + + /* An Open whose instance is replaced meanwhile is not the successor's. */ + asrv.srv = &srv; + asrv.is_open = false; + hook_next = &next; + hook = 1; + av_service_connect(&dcp); + assert(calls == 7 && last_cmd == 4 && asrv.srv == &next && !asrv.is_open && connects == 4); + assert(!next.pins); + asrv.srv = &srv; assert(!srv.pins && !asrv.srv_rwsem.held && !asrv.plug_lock.held); return 0; } diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index 476c35ff89f7d..1b7bbedd5c691 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -101,7 +101,7 @@ def function(file,signature): struct apple_dcp { void *dev;int fw_compat;struct apple_dcp_afkep *dcpavservep;struct apple_connector *connector; bool external_physically_absent,hpd_mutex,quiescing,typec_cable_connected;u64 typec_generation;unsigned int nr_modes; - struct { int iomfb_method_profile,firmware_compat; bool fw_14_7_endpoints,afk_announced_names; unsigned int num_dptx_ports; } hw; bool ext_dead,external_native; + struct { int iomfb_method_profile,firmware_compat; bool fw_14_7_endpoints,afk_announced_names; unsigned int num_dptx_ports; unsigned char dptx_endpoint; } hw; bool ext_dead,external_native; struct { bool lock,enabled;u64 epoch;int enable_completion;struct apple_epic_service *service,*opened; u64 opened_epoch,close_gen,terminated_epoch,close_epoch;bool opening;struct work_struct close_wq; } dcpavserv; /* A J714s firmware 27 processor's: this one is not. */ @@ -109,6 +109,7 @@ def function(file,signature): /* Not a dcpext: none of these scenarios is an external processor. */ bool dcpext; }; +#define DPTX_ENDPOINT_V2 0x2c '''+internal_inline(root,'dcp_fw_is_14_7')+internal_inline(root,'dcp_is_v14_7_external')+internal_inline(root,'dcp_is_v27_external')+r'''struct epic_cmd { u32 retcode;u64 rxbuf,txbuf;u32 rxlen,txlen; }; static unsigned int dma_frees,opens;