Skip to content

Commit

Permalink
ASoC: SOF: Intel: hda-sdw-bpt: add CHAIN_DMA support
Browse files Browse the repository at this point in the history
When the firmware is involved, the data can be transferred with a
CHAIN_DMA on LNL+.

The CHAIN_DMA needs to be programmed before the DMAs per the
documentation. The states are not exactly symmetrical, on stop we must
do a PAUSE and RESET.

The FIFO size of 10ms was determined experimentally. With the minimum
of 2ms, errors were reported by the codec, likely because of xruns.

The code flow deals with the two TX and RX CHAIN_DMAs in symmetrical
ways, i.e.
alloc TX
alloc RX
enable TX
enable RX
disable RX
disable TX
free RX
free TX

Signed-off-by: Pierre-Louis Bossart <[email protected]>
  • Loading branch information
plbossart committed Jan 24, 2024
1 parent afdfe60 commit 3a9c2b9
Showing 1 changed file with 142 additions and 4 deletions.
146 changes: 142 additions & 4 deletions sound/soc/sof/intel/hda-sdw-bpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,82 @@
#include <sound/hda-mlink.h>
#include <sound/hda-sdw-bpt.h>
#include <sound/sof.h>
#include <sound/sof/ipc4/header.h>
#include "../ops.h"
#include "../sof-priv.h"
#include "../ipc4-priv.h"
#include "hda.h"

#define BPT_FREQUENCY 192000
#define BPT_MULTIPLIER ((BPT_FREQUENCY / 48000) - 1)
#define BPT_CHAIN_DMA_FIFO_MS 10
/*
* This routine is directly inspired by sof_ipc4_chain_dma_trigger(),
* with major simplifications since there are no pipelines defined
* and no dependency on ALSA hw_params
*/
static int chain_dma_trigger(struct snd_sof_dev *sdev,
unsigned int stream_tag,
int direction,
int state)
{
struct sof_ipc4_fw_data *ipc4_data = sdev->private;
bool allocate, enable, set_fifo_size;
struct sof_ipc4_msg msg = {{ 0 }};
int dma_id;

if (sdev->pdata->ipc_type != SOF_IPC_TYPE_4)
return -ENOTSUPP;

switch (state) {
case SOF_IPC4_PIPE_RUNNING: /* Allocate and start the chain */
allocate = true;
enable = true;
set_fifo_size = true;
break;
case SOF_IPC4_PIPE_PAUSED: /* Stop the chain */
allocate = true;
enable = false;
set_fifo_size = false;
break;
case SOF_IPC4_PIPE_RESET: /* Deallocate chain resources and remove the chain */
allocate = false;
enable = false;
set_fifo_size = false;
break;
default:
dev_err(sdev->dev, "Unexpected state %d", state);
return -EINVAL;
}

msg.primary = SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_CHAIN_DMA);
msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);

/* for BPT/BRA we can use the same stream tag for host and link */
dma_id = stream_tag - 1;
if (direction == SNDRV_PCM_STREAM_CAPTURE)
dma_id += ipc4_data->num_playback_streams;

msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_HOST_ID(dma_id);
msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_LINK_ID(dma_id);

/* For BPT/BRA we use 32 bits so SCS is not set */

/* CHAIN DMA needs at least 2ms */
if (set_fifo_size)
msg.extension |= SOF_IPC4_GLB_EXT_CHAIN_DMA_FIFO_SIZE(BPT_FREQUENCY / 1000 *
BPT_CHAIN_DMA_FIFO_MS *
sizeof(u32));

if (allocate)
msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_ALLOCATE_MASK;

if (enable)
msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_ENABLE_MASK;

return sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0);
}

static int hda_sdw_bpt_dma_prepare(struct device *dev,
struct hdac_ext_stream **sdw_bpt_stream,
Expand Down Expand Up @@ -53,6 +123,34 @@ static int hda_sdw_bpt_dma_prepare(struct device *dev,
}
*sdw_bpt_stream = bpt_stream;

if (!sdev->dspless_mode_selected) {
struct hdac_stream *hstream;
u32 mask;
int ret;

/* the chain DMA needs to be programmed before the DMAs */
ret = chain_dma_trigger(sdev,
hdac_stream(bpt_stream)->stream_tag,
hdac_stream(bpt_stream)->direction,
SOF_IPC4_PIPE_RUNNING);
if (ret < 0) {
dev_err(sdev->dev, "%s: chain_dma_trigger failed: %d\n",
__func__, ret);
return ret;
}

/* decouple host and link DMA if the DSP is used */
hstream = &bpt_stream->hstream;
mask = BIT(hstream->index);

snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
mask, mask);

snd_hdac_ext_stream_reset(bpt_stream);

snd_hdac_ext_stream_setup(bpt_stream, format);
}

if (hdac_stream(bpt_stream)->direction == SNDRV_PCM_STREAM_PLAYBACK) {
struct hdac_bus *bus = sof_to_bus(sdev);
struct hdac_ext_link *hlink;
Expand All @@ -63,6 +161,7 @@ static int hda_sdw_bpt_dma_prepare(struct device *dev,

snd_hdac_ext_bus_link_set_stream_id(hlink, stream_tag);
}

return 0;
}

Expand All @@ -71,6 +170,8 @@ static int hda_sdw_bpt_dma_deprepare(struct device *dev,
struct snd_dma_buffer *dmab_bdl)
{
struct snd_sof_dev *sdev = dev_get_drvdata(dev);
struct hdac_stream *hstream;
u32 mask;
int ret;

ret = hda_cl_cleanup(sdev->dev, dmab_bdl, sdw_bpt_stream);
Expand All @@ -91,6 +192,25 @@ static int hda_sdw_bpt_dma_deprepare(struct device *dev,
snd_hdac_ext_bus_link_clear_stream_id(hlink, stream_tag);
}

if (!sdev->dspless_mode_selected) {

/* Release CHAIN_DMA resources */
ret = chain_dma_trigger(sdev,
hdac_stream(sdw_bpt_stream)->stream_tag,
hdac_stream(sdw_bpt_stream)->direction,
SOF_IPC4_PIPE_RESET);
if (ret < 0)
dev_err(sdev->dev, "%s: chain_dma_trigger PIPE_RESET failed: %d\n",
__func__, ret);

/* couple host and link DMA */
hstream = &sdw_bpt_stream->hstream;
mask = BIT(hstream->index);

snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
SOF_HDA_REG_PP_PPCTL, mask, 0);
}

return 0;
}

Expand All @@ -103,8 +223,13 @@ static int hda_sdw_bpt_dma_enable(struct device *dev,
ret = hda_cl_trigger(sdev->dev, sdw_bpt_stream, SNDRV_PCM_TRIGGER_START);
if (ret < 0) {
dev_err(sdev->dev, "%s: SDW BPT DMA trigger start failed\n", __func__);

return ret;
}

if (!sdev->dspless_mode_selected)
snd_hdac_ext_stream_start(sdw_bpt_stream);

return 0;
}

Expand All @@ -114,11 +239,24 @@ static int hda_sdw_bpt_dma_disable(struct device *dev,
struct snd_sof_dev *sdev = dev_get_drvdata(dev);
int ret;

if (!sdev->dspless_mode_selected) {
snd_hdac_ext_stream_clear(sdw_bpt_stream);

ret = chain_dma_trigger(sdev,
hdac_stream(sdw_bpt_stream)->stream_tag,
hdac_stream(sdw_bpt_stream)->direction,
SOF_IPC4_PIPE_PAUSED);
if (ret < 0)
dev_err(sdev->dev, "%s: chain_dma_trigger PIPE_PAUSED failed: %d\n",
__func__, ret);
}

ret = hda_cl_trigger(sdev->dev, sdw_bpt_stream, SNDRV_PCM_TRIGGER_STOP);
if (ret < 0) {
dev_err(sdev->dev, "%s: SDW BPT DMA trigger stop failed\n", __func__);
return ret;
}

return 0;
}

Expand Down Expand Up @@ -314,12 +452,12 @@ int hda_sdw_bpt_close(struct device *dev,
int ret1;

ret = hda_sdw_bpt_dma_deprepare(dev,
bpt_tx_stream,
dmab_tx_bdl);
bpt_rx_stream,
dmab_rx_bdl);

ret1 = hda_sdw_bpt_dma_deprepare(dev,
bpt_rx_stream,
dmab_rx_bdl);
bpt_tx_stream,
dmab_tx_bdl);
if (!ret)
ret = ret1;

Expand Down

0 comments on commit 3a9c2b9

Please sign in to comment.