Skip to content
Closed
113 changes: 100 additions & 13 deletions drivers/gpu/drm/apple/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ struct dcp_audio {

struct snd_pcm_chmap_elem selected_chmap;
struct dcp_sound_cookie selected_cookie;
/* stream runs without a sink, see dcpaud_void_start() */
bool void_running;
ktime_t void_start;
u64 void_periods;
struct delayed_work void_work;
void *elements;
void *productattrs;

Expand Down Expand Up @@ -81,7 +86,7 @@ static const struct snd_pcm_hardware dcp_pcm_hw = {
* configured, as HDMI PCMs on other platforms can. Userspace (PipeWire,
* PulseAudio) probes the PCM once when the card appears and drops the
* output for good if that fails, so a monitor attached later would never
* show up. Starting the stream still fails until a sink is connected.
* show up. The stream then plays into the void, see dcpaud_void_start().
*/
static const struct snd_pcm_hardware dcp_pcm_hw_unplugged = {
.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
Expand Down Expand Up @@ -352,12 +357,10 @@ static int dcp_pcm_hw_params(struct snd_pcm_substream *substream,
struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
int ret;

if (dcpaud->open_unplugged)
/* no sink: the stream runs without DMA, see dcpaud_void_start() */
if (dcpaud->open_unplugged || !dcpaud_connection_up(dcpaud))
return 0;

if (!dcpaud_connection_up(dcpaud))
return -ENXIO;

ret = dcpaud_select_cookie(dcpaud, params);
if (ret < 0)
return ret;
Expand Down Expand Up @@ -397,16 +400,79 @@ static int dcp_pcm_prepare(struct snd_pcm_substream *substream)
struct dcp_audio *dcpaud = substream->pcm->private_data;

/* alsa-lib prepares right after hw_params; see dcp_pcm_hw_unplugged */
if (dcpaud->open_unplugged)
if (dcpaud->open_unplugged || !dcpaud_connection_up(dcpaud))
return 0;

if (!dcpaud_connection_up(dcpaud))
return -ENXIO;

return dcp_audiosrv_prepare(dcpaud->dcp_dev,
&dcpaud->selected_cookie);
}

static u64 dcpaud_void_period_ns(struct snd_pcm_runtime *runtime)
{
return div_u64((u64)runtime->period_size * NSEC_PER_SEC, runtime->rate);
}

static void dcpaud_void_work(struct work_struct *work)
{
struct dcp_audio *dcpaud = container_of(to_delayed_work(work),
struct dcp_audio, void_work);
struct snd_pcm_substream *substream = dcpaud->substream;
ktime_t next;
s64 delay;

snd_pcm_period_elapsed(substream);

snd_pcm_stream_lock_irq(substream);
if (dcpaud->void_running) {
/* the next period boundary, so that periods do not drift */
next = ktime_add_ns(dcpaud->void_start,
++dcpaud->void_periods *
dcpaud_void_period_ns(substream->runtime));
delay = ktime_to_ns(ktime_sub(next, ktime_get()));
schedule_delayed_work(&dcpaud->void_work,
delay > 0 ? nsecs_to_jiffies(delay) : 0);
}
snd_pcm_stream_unlock_irq(substream);
}

/*
* Without a sink, DCP has no audio link to start. Failing START does not
* stop userspace from trying: PipeWire retries it on every wakeup, and a
* stream that never starts never makes it wait, so it retries at about
* 130000 times a second for as long as something plays (a monitor
* without audio, or none attached). Play into the void instead, as HDMI
* PCMs elsewhere do: the stream runs on the clock, without DMA, and its
* samples are dropped. A sink attached later gets the next stream opened.
*/
static int dcpaud_void_start(struct snd_pcm_substream *substream)
{
struct dcp_audio *dcpaud = substream->pcm->private_data;
u64 period_ns = dcpaud_void_period_ns(substream->runtime);

dev_dbg(dcpaud->dev, "no sink, playing into the void\n");
dcpaud->void_running = true;
dcpaud->void_start = ktime_get();
dcpaud->void_periods = 0;
schedule_delayed_work(&dcpaud->void_work, nsecs_to_jiffies(period_ns));

return 0;
}

static snd_pcm_uframes_t dcp_pcm_pointer(struct snd_pcm_substream *substream)
{
struct dcp_audio *dcpaud = substream->pcm->private_data;
struct snd_pcm_runtime *runtime = substream->runtime;
u64 frames;

if (!dcpaud->void_running)
return snd_dmaengine_pcm_pointer(substream);

frames = mul_u64_u32_div(ktime_to_ns(ktime_sub(ktime_get(),
dcpaud->void_start)),
runtime->rate, NSEC_PER_SEC);
return do_div(frames, runtime->buffer_size);
}

static int dcp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
struct dcp_audio *dcpaud = substream->pcm->private_data;
Expand All @@ -415,8 +481,8 @@ static int dcp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_RESUME:
if (!dcpaud_connection_up(dcpaud))
return -ENXIO;
if (dcpaud->open_unplugged || !dcpaud_connection_up(dcpaud))
return dcpaud_void_start(substream);

WARN_ON(pm_runtime_get_sync(dcpaud->dev) < 0);
ret = dcp_audiosrv_startlink(dcpaud->dcp_dev,
Expand All @@ -427,6 +493,11 @@ static int dcp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)

case SNDRV_PCM_TRIGGER_STOP:
case SNDRV_PCM_TRIGGER_SUSPEND:
if (dcpaud->void_running) {
/* sync_stop waits for the work */
dcpaud->void_running = false;
return 0;
}
break;

default:
Expand Down Expand Up @@ -455,14 +526,28 @@ static int dcp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
return 0;
}

/*
* ALSA calls this once a stream has stopped, outside the stream lock, before
* the next hw_params, prepare or hw_free and at close.
*/
static int dcp_pcm_sync_stop(struct snd_pcm_substream *substream)
{
struct dcp_audio *dcpaud = substream->pcm->private_data;

cancel_delayed_work_sync(&dcpaud->void_work);

return 0;
}

struct snd_pcm_ops dcp_playback_ops = {
.open = dcp_pcm_open,
.close = dcp_pcm_close,
.hw_params = dcp_pcm_hw_params,
.hw_free = dcp_pcm_hw_free,
.prepare = dcp_pcm_prepare,
.trigger = dcp_pcm_trigger,
.pointer = snd_dmaengine_pcm_pointer,
.sync_stop = dcp_pcm_sync_stop,
.pointer = dcp_pcm_pointer,
};

// Transitional workaround: for the chmap control TLV, advertise options
Expand Down Expand Up @@ -544,7 +629,8 @@ static void dcpaud_report_hotplug(struct dcp_audio *dcpaud, bool connected)

if (!connected) {
snd_pcm_stream_lock(substream);
if (substream->runtime)
/* a stream without a sink plays on, see dcpaud_void_start() */
if (substream->runtime && !dcpaud->void_running)
snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
snd_pcm_stream_unlock(substream);
}
Expand Down Expand Up @@ -756,6 +842,7 @@ static int dcpaud_probe(struct platform_device *pdev)

dcpaud->dev = &pdev->dev;
mutex_init(&dcpaud->data_lock);
INIT_DELAYED_WORK(&dcpaud->void_work, dcpaud_void_work);
platform_set_drvdata(pdev, dcpaud);

return component_add(&pdev->dev, &dcpaud_comp_ops);
Expand Down
12 changes: 12 additions & 0 deletions drivers/gpu/drm/apple/dcp-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ void dcp_swap_watchdog_complete(struct apple_dcp *dcp);
bool dcp_is_usb4_output(struct apple_dcp *dcp);
void dcp_retry_placeholder_edid(struct apple_dcp *dcp,
const struct drm_edid *drm_edid);
bool dcp_typec_relink(struct apple_dcp *dcp);
bool dcp_typec_sink_relink(struct apple_dcp *dcp);

struct dcpav_service_epic;

Expand Down Expand Up @@ -333,6 +335,12 @@ struct apple_dcp {
u64 typec_generation; /* hpd_mutex: identifies the current connection */
u64 placeholder_generation;
struct delayed_work placeholder_edid_wq;
/* replugs replayed for a Type-C sink that lost its link */
u32 relink_tries;
bool relink_active;
unsigned long relink_done;
u64 relink_generation;
struct delayed_work typec_relink_wq;
bool use_timestamps;
bool vrr_enabled;
struct dcp_set_digital_out_mode_req mode;
Expand Down Expand Up @@ -371,6 +379,8 @@ struct apple_dcp {

/* Workqueue for sending vblank events when a dcp swap is not possible */
struct work_struct vblank_wq;
/* connector physical dimensions, deferred from the RTKit RX worker */
struct work_struct dimensions_wq;

/* Completes a Type-C swap that DCP dropped, and recovers the pipe. */
struct delayed_work swap_watchdog_wq;
Expand Down Expand Up @@ -440,6 +450,8 @@ struct apple_dcp {
bool tb_clock_ok;
/* CRTC powered off while the Type-C cable stays attached */
bool typec_crtc_off;
/* dcp_poweron() reconnects DPTX; its own modeset follows the HPD */
bool typec_poweron_hpd;
/* IOMFB reports its video interface ready after DPTX link training. */
struct completion typec_iomfb_hpd_ready;
struct delayed_work typec_reconnect_wq;
Expand Down
Loading