Skip to content
44 changes: 44 additions & 0 deletions drivers/gpu/drm/apple/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions drivers/gpu/drm/apple/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/apple/av-lifetime-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
145 changes: 121 additions & 24 deletions drivers/gpu/drm/apple/av.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -216,16 +228,17 @@ 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);

/* 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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

/*
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()). */
Expand Down Expand Up @@ -1188,24 +1260,27 @@ 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) {
dev_err(dcp->dev, "error closing audio service: %d\n", ret);
}
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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
/*
Expand All @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions drivers/gpu/drm/apple/dcp-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Loading
Loading