diff --git a/sound/soc/sof/ipc4-compress.c b/sound/soc/sof/ipc4-compress.c index 97a4ec7519055e..a4cbe7a22986fb 100644 --- a/sound/soc/sof/ipc4-compress.c +++ b/sound/soc/sof/ipc4-compress.c @@ -28,8 +28,8 @@ struct sof_ipc4_compr_init_data { u32 dir; } __packed __aligned(4); -static struct sof_ipc4_process * -sof_ipc4_compr_get_module(struct snd_sof_pcm *spcm, int dir) +static struct snd_sof_widget * +sof_ipc4_compr_get_swidget(struct snd_sof_pcm *spcm, int dir) { int id = dir ? snd_soc_dapm_encoder : snd_soc_dapm_decoder; struct snd_sof_pcm_stream *sps = &spcm->stream[dir]; @@ -40,16 +40,21 @@ sof_ipc4_compr_get_module(struct snd_sof_pcm *spcm, int dir) for_each_dapm_widgets(sps->list, i, widget) { struct snd_sof_widget *swidget = widget->dobj.private; - if (!swidget) - continue; - - if (swidget->widget->id == id) - return swidget->private; + if (swidget && swidget->widget->id == id) + return swidget; } return NULL; } +static struct sof_ipc4_process * +sof_ipc4_compr_get_module(struct snd_sof_pcm *spcm, int dir) +{ + struct snd_sof_widget *swidget = sof_ipc4_compr_get_swidget(spcm, dir); + + return swidget ? swidget->private : NULL; +} + static u32 sof_ipc4_compr_calc_min_fragment_size(struct snd_sof_pcm_stream *sps) { u32 host_buffer_estimate; @@ -559,6 +564,107 @@ static int sof_ipc4_compr_get_params(struct snd_soc_component *component, return 0; } +/* + * On DRAIN, zero the free part of the playback ring (from the application + * write position up to the DSP read position). The host DMA free-runs around + * the ring, so after the last valid byte the firmware reads whatever is left in + * the buffer: after the first wrap this is stale data from the previous lap, + * which decoders try to decode. With the area zeroed the end of the stream is + * followed by zeros, as it is for streams that never wrapped the ring. + */ +static void sof_ipc4_compr_drain_clear(struct snd_compr_stream *cstream) +{ + struct snd_compr_runtime *crtd = cstream->runtime; + u64 pending, offset, n; + u64 free_bytes; + + if (cstream->direction != SND_COMPRESS_PLAYBACK || !crtd->dma_area) + return; + + pending = crtd->total_bytes_available - crtd->total_bytes_transferred; + if (pending >= crtd->buffer_size) + return; + + free_bytes = crtd->buffer_size - pending; + div64_u64_rem(crtd->total_bytes_available, crtd->buffer_size, &offset); + n = crtd->buffer_size - offset; + + if (free_bytes <= n) { + memset(crtd->dma_area + offset, 0, free_bytes); + } else { + memset(crtd->dma_area + offset, 0, n); + memset(crtd->dma_area, 0, free_bytes - n); + } +} + +/* + * Stream control parameters for compress modules. + * + * The parameters are sent as struct module_param records. Stream control + * parameters have SOF_IPC4_COMPR_CTRL_ID in the upper 16 bits of the id and + * the control in the lower 16 bits. No codec specific parameter uses this id, + * so a firmware without support for these controls skips them. + */ +#define SOF_IPC4_COMPR_CTRL_ID 0xffff + +/* Absolute byte offset of the end of the stream (stream size), payload: u64 */ +#define SOF_IPC4_COMPR_CTRL_STREAM_END 1 + +#define SOF_IPC4_COMPR_CTRL_PARAM_ID(ctrl) ((SOF_IPC4_COMPR_CTRL_ID << 16) | (ctrl)) + +struct sof_ipc4_compr_ctrl_u64 { + u32 id; + u32 size; /* size of the whole record */ + u64 value; +} __packed __aligned(4); + +/* + * Tell the decoder the total number of bytes in the stream so it can + * feed the codec with the exact amount of remaining data and finish decoding + * at the real end of the stream instead of relying on what follows it in the + * buffer. + */ +static void sof_ipc4_compr_drain(struct snd_sof_dev *sdev, + struct snd_sof_pcm *spcm, + struct snd_compr_stream *cstream) +{ + const struct sof_ipc_ops *iops = sdev->ipc->ops; + struct sof_ipc4_compr_ctrl_u64 param; + int dir = cstream->direction; + struct sof_ipc4_fw_module *fw_module; + struct snd_sof_widget *swidget; + struct sof_ipc4_msg msg = {{ 0 }}; + int ret; + + if (dir != SND_COMPRESS_PLAYBACK) + return; + + swidget = sof_ipc4_compr_get_swidget(spcm, dir); + if (!swidget || !swidget->module_info) + return; + + fw_module = swidget->module_info; + + param.id = SOF_IPC4_COMPR_CTRL_PARAM_ID(SOF_IPC4_COMPR_CTRL_STREAM_END); + param.size = sizeof(param); + param.value = cstream->runtime->total_bytes_available; + + msg.primary = fw_module->man4_module_entry.id; + msg.primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id); + msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); + msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.data_ptr = ¶m; + msg.data_size = sizeof(param); + + sof_ipc4_compr_drain_clear(cstream); + + ret = iops->set_get_data(sdev, &msg, msg.data_size, true); + if (ret < 0) + spcm_err(spcm, dir, "failed to send stream end: %d\n", ret); + else + spcm_dbg(spcm, dir, "stream end: %llu\n", param.value); +} + static int sof_ipc4_compr_trigger(struct snd_soc_component *component, struct snd_compr_stream *cstream, int cmd) { @@ -576,6 +682,8 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, return -EINVAL; } + spcm_dbg(spcm, dir, "Entry: trigger (cmd: %d)\n", cmd); + switch (cmd) { case SNDRV_PCM_TRIGGER_START: /* @@ -601,6 +709,7 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, break; case SND_COMPR_TRIGGER_DRAIN: spcm->pending_stop[dir] = true; + sof_ipc4_compr_drain(sdev, spcm, cstream); break; case SND_COMPR_TRIGGER_PARTIAL_DRAIN: case SND_COMPR_TRIGGER_NEXT_TRACK: @@ -612,8 +721,6 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, return 0; } - spcm_dbg(spcm, dir, "Entry: trigger (cmd: %d)\n", cmd); - ret = pcm_ops->trigger(component, NULL, spcm, cmd, dir); if (ret < 0) { spcm_err(spcm, dir, "pcm_ops->trigger failed for cmd %d\n", cmd);