diff --git a/include/sound/sof/ipc4/header.h b/include/sound/sof/ipc4/header.h index 0f6f1de3eee90f..a2e1a98ff9a899 100644 --- a/include/sound/sof/ipc4/header.h +++ b/include/sound/sof/ipc4/header.h @@ -545,6 +545,9 @@ enum sof_ipc4_notification_type { SOF_IPC4_NOTIFY_ASYNC_MSG_SRVC_MESSAGE, SOF_IPC4_NOTIFY_TYPE_LAST, + + /* no host DMA: period elapsed notification */ + SOF_IPC4_NOTIFY_HOST_PERIOD_ELAPSED = 200, }; enum sof_ipc4_resource_type { diff --git a/sound/soc/sof/imx/Kconfig b/sound/soc/sof/imx/Kconfig index 09d88ce5b9e6e5..8b693340220cf9 100644 --- a/sound/soc/sof/imx/Kconfig +++ b/sound/soc/sof/imx/Kconfig @@ -16,6 +16,7 @@ config SND_SOC_SOF_IMX_COMMON select SND_SOC_SOF_OF_DEV select SND_SOC_SOF select SND_SOC_SOF_IPC3 + select SND_SOC_SOF_IPC4 select SND_SOC_SOF_XTENSA select SND_SOC_SOF_COMPRESS help diff --git a/sound/soc/sof/imx/imx-common.c b/sound/soc/sof/imx/imx-common.c index 436fe49246ba80..a129c9761e6762 100644 --- a/sound/soc/sof/imx/imx-common.c +++ b/sound/soc/sof/imx/imx-common.c @@ -5,13 +5,18 @@ // Common helpers for the audio DSP on i.MX8 #include +#include #include #include #include #include +#include #include #include "../ops.h" +#include "../sof-priv.h" +#include "../ipc4-priv.h" +#include "../ipc4-fw-reg.h" #include "imx-common.h" @@ -84,10 +89,44 @@ static void imx_handle_reply(struct imx_dsp_ipc *ipc) { struct snd_sof_dev *sdev = imx_dsp_get_data(ipc); + /* IPC4 replies arrive through the request doorbell */ + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) + return; + guard(spinlock_irqsave)(&sdev->ipc_lock); snd_sof_ipc_process_reply(sdev, 0); } +#if IS_ENABLED(CONFIG_SND_SOC_SOF_IPC4) +static void imx_handle_ipc4_msg(struct snd_sof_dev *sdev) +{ + struct sof_ipc4_msg *ipc4_msg; + u64 header; + + /* the 8-byte header precedes the DSP -> host box payload */ + sof_mailbox_read(sdev, sdev->dsp_box.offset - IMX_IPC4_HDR_SIZE, + &header, sizeof(header)); + + if (lower_32_bits(header) & SOF_IPC4_MSG_DIR_MASK) { + guard(spinlock_irqsave)(&sdev->ipc_lock); + + if (sdev->msg) { + ipc4_msg = sdev->msg->reply_data; + ipc4_msg->header_u64 = header; + } + + snd_sof_ipc_process_reply(sdev, 0); + } else { + struct sof_ipc4_msg notification_data = {{ 0 }}; + + notification_data.header_u64 = header; + sdev->ipc->msg.rx_data = ¬ification_data; + snd_sof_ipc_msgs_rx(sdev); + sdev->ipc->msg.rx_data = NULL; + } +} +#endif + static void imx_handle_request(struct imx_dsp_ipc *ipc) { struct snd_sof_dev *sdev; @@ -106,6 +145,13 @@ static void imx_handle_request(struct imx_dsp_ipc *ipc) } } +#if IS_ENABLED(CONFIG_SND_SOC_SOF_IPC4) + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { + imx_handle_ipc4_msg(sdev); + return; + } +#endif + snd_sof_ipc_msgs_rx(sdev); } @@ -118,7 +164,24 @@ static int imx_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg) { struct imx_common_data *common = sdev->pdata->hw_pdata; - sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data, msg->msg_size); + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { + struct sof_ipc4_msg *msg_data = msg->msg_data; + + /* the 8-byte header precedes the host -> DSP box payload */ + sof_mailbox_write(sdev, + sdev->host_box.offset - IMX_IPC4_HDR_SIZE, + &msg_data->header_u64, + sizeof(msg_data->header_u64)); + + if (msg_data->data_size) + sof_mailbox_write(sdev, sdev->host_box.offset, + msg_data->data_ptr, + msg_data->data_size); + } else { + sof_mailbox_write(sdev, sdev->host_box.offset, + msg->msg_data, msg->msg_size); + } + imx_dsp_ring_doorbell(common->ipc_handle, 0x0); return 0; @@ -128,7 +191,11 @@ static int imx_get_bar_index(struct snd_sof_dev *sdev, u32 type) { switch (type) { case SOF_FW_BLK_TYPE_IRAM: + case SOF_FW_BLK_TYPE_DRAM: case SOF_FW_BLK_TYPE_SRAM: + /* i.MX95 maps IRAM/DRAM/SRAM through a single window */ + if (!sdev->bar[type]) + return sdev->mailbox_bar; return type; default: return -EINVAL; @@ -137,11 +204,30 @@ static int imx_get_bar_index(struct snd_sof_dev *sdev, u32 type) static int imx_get_mailbox_offset(struct snd_sof_dev *sdev) { + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) + return get_chip_info(sdev)->ipc_info.ipc4_inbox_offset; + return get_chip_info(sdev)->ipc_info.boot_mbox_offset; } static int imx_get_window_offset(struct snd_sof_dev *sdev, u32 id) { + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { + const struct imx_ipc_info *ipc_info = &get_chip_info(sdev)->ipc_info; + + switch (id) { + case SOF_IPC4_INBOX_WINDOW_IDX: + /* fw_info_box lives in the SW register window */ + return ipc_info->ipc4_fw_regs_offset; + case SOF_IPC4_OUTBOX_WINDOW_IDX: + return ipc_info->ipc4_outbox_offset; + case SOF_IPC4_DEBUG_WINDOW_IDX: + return ipc_info->ipc4_debug_offset; + default: + return -EINVAL; + } + } + return get_chip_info(sdev)->ipc_info.window_offset; } @@ -412,7 +498,10 @@ static int imx_probe(struct snd_sof_dev *sdev) sdev->num_cores = 1; sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM; - sdev->dsp_box.offset = get_chip_info(sdev)->ipc_info.boot_mbox_offset; + + /* IPC4 box offsets are set by sof_ipc4_init() */ + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_3) + sdev->dsp_box.offset = get_chip_info(sdev)->ipc_info.boot_mbox_offset; return imx_chip_probe(sdev); } @@ -433,10 +522,204 @@ static void imx_remove(struct snd_sof_dev *sdev) } } +#if IS_ENABLED(CONFIG_SND_SOC_SOF_IPC4) +/* copy the Reef payload that follows the extended manifest and $AM1 header */ +static int imx_ipc4_load_fw_to_dsp(struct snd_sof_dev *sdev) +{ + u32 payload_offset = sdev->basefw.payload_offset; + const struct firmware *fw = sdev->basefw.fw; + const struct sof_man4_fw_binary_header *man4_hdr; + const struct snd_sof_fw_header *header; + const struct snd_sof_mod_hdr *module; + size_t remaining; + u32 man4_hdr_offset; + int count, ret; + + if (!fw) + return -EINVAL; + + man4_hdr_offset = payload_offset + IMX_MAN4_FW_HDR_OFFSET; + if (fw->size < man4_hdr_offset + sizeof(*man4_hdr)) { + dev_err(sdev->dev, "firmware too small for $AM1 header (size %zu)\n", + fw->size); + return -EINVAL; + } + + man4_hdr = (const struct sof_man4_fw_binary_header *) + (fw->data + man4_hdr_offset); + + payload_offset = man4_hdr_offset + man4_hdr->len + + man4_hdr->num_module_entries * sizeof(struct sof_man4_module); + + if (fw->size < payload_offset + sizeof(*header)) { + dev_err(sdev->dev, "firmware too small for payload header\n"); + return -EINVAL; + } + + header = (const struct snd_sof_fw_header *)(fw->data + payload_offset); + + if (memcmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE)) { + dev_err(sdev->dev, "invalid firmware payload signature\n"); + return -EINVAL; + } + + module = (const struct snd_sof_mod_hdr *)(fw->data + payload_offset + + sizeof(*header)); + remaining = fw->size - sizeof(*header) - payload_offset; + + for (count = 0; count < header->num_modules; count++) { + const struct snd_sof_blk_hdr *block; + int blk; + + if (remaining < sizeof(*module)) { + dev_err(sdev->dev, "not enough data for a module\n"); + return -EINVAL; + } + + block = (const struct snd_sof_blk_hdr *)((const u8 *)module + + sizeof(*module)); + remaining -= sizeof(*module); + + for (blk = 0; blk < module->num_blocks; blk++) { + u32 offset; + + if (remaining < sizeof(*block)) { + dev_err(sdev->dev, "not enough data for block\n"); + return -EINVAL; + } + remaining -= sizeof(*block); + + switch (block->type) { + case SOF_FW_BLK_TYPE_IRAM: + case SOF_FW_BLK_TYPE_DRAM: + case SOF_FW_BLK_TYPE_SRAM: + offset = block->offset; + break; + default: + /* skip anything the memcpy loader can't handle */ + if (remaining < block->size) + return -EINVAL; + remaining -= block->size; + block = (const struct snd_sof_blk_hdr *) + ((const u8 *)block + sizeof(*block) + + block->size); + continue; + } + + if (block->size % sizeof(u32)) { + dev_err(sdev->dev, "unaligned block size %#x\n", + block->size); + return -EINVAL; + } + + ret = snd_sof_dsp_block_write(sdev, block->type, offset, + (void *)(block + 1), + block->size); + if (ret < 0) { + dev_err(sdev->dev, + "failed to write block type %#x\n", + block->type); + return ret; + } + + if (remaining < block->size) + return -EINVAL; + remaining -= block->size; + + block = (const struct snd_sof_blk_hdr *) + ((const u8 *)block + sizeof(*block) + block->size); + } + + module = (const struct snd_sof_mod_hdr *)block; + } + + return 0; +} + +/* The generic IPC4 loader has no load_fw_to_dsp(), so copy before run */ +static int imx_pre_fw_run(struct snd_sof_dev *sdev) +{ + if (sdev->pdata->ipc_type != SOF_IPC_TYPE_4) + return 0; + + return imx_ipc4_load_fw_to_dsp(sdev); +} + +int imx_ipc4_init_data(struct snd_sof_dev *sdev) +{ + struct sof_ipc4_fw_data *ipc4_data; + + if (sdev->pdata->ipc_type != SOF_IPC_TYPE_4) + return 0; + + ipc4_data = devm_kzalloc(sdev->dev, sizeof(*ipc4_data), GFP_KERNEL); + if (!ipc4_data) + return -ENOMEM; + + ipc4_data->manifest_fw_hdr_offset = IMX_MAN4_FW_HDR_OFFSET; + ipc4_data->mtrace_type = SOF_IPC4_MTRACE_NOT_AVAILABLE; + ipc4_data->max_libs_count = 1; + + sdev->private = ipc4_data; + + return 0; +} +EXPORT_SYMBOL(imx_ipc4_init_data); +#else +int imx_ipc4_init_data(struct snd_sof_dev *sdev) +{ + return 0; +} +EXPORT_SYMBOL(imx_ipc4_init_data); + +static int imx_pre_fw_run(struct snd_sof_dev *sdev) +{ + return 0; +} +#endif /* CONFIG_SND_SOC_SOF_IPC4 */ + +/* no hardware stream tags: derive the host gateway id from device and direction */ +static int imx_pcm_hw_params(struct snd_sof_dev *sdev, + struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_sof_platform_stream_params *platform_params) +{ + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { + platform_params->stream_tag = substream->pcm->device * 2 + substream->stream + 1; + + platform_params->use_phy_address = true; + platform_params->phy_addr = substream->runtime->dma_addr; + } + + return 0; +} + +/* The firmware publishes a byte counter per host gateway in fw_registers */ +static u64 imx_get_host_byte_counter(struct snd_sof_dev *sdev, + struct snd_soc_component *component, + struct snd_pcm_substream *substream) +{ + unsigned int host_dma_id = substream->pcm->device * 2 + substream->stream; + u64 counter = 0; + u32 offset; + + if (host_dma_id >= SOF_IPC4_MAX_HOST_BYTE_CNT_SLOTS) + return 0; + + offset = sdev->fw_info_box.offset + + offsetof(struct sof_ipc4_fw_registers, host_byte_cnt) + + host_dma_id * sizeof(u64); + + sof_mailbox_read(sdev, offset, &counter, sizeof(counter)); + + return counter; +} + const struct snd_sof_dsp_ops sof_imx_ops = { .probe = imx_probe, .remove = imx_remove, + .pre_fw_run = imx_pre_fw_run, .run = imx_chip_core_kick, .reset = imx_chip_core_reset, @@ -460,6 +743,9 @@ const struct snd_sof_dsp_ops sof_imx_ops = { .pcm_open = sof_stream_pcm_open, .pcm_close = sof_stream_pcm_close, + .pcm_hw_params = imx_pcm_hw_params, + .get_host_byte_counter = imx_get_host_byte_counter, + .pcm_contiguous_buffer = true, .runtime_suspend = imx_runtime_suspend, .runtime_resume = imx_runtime_resume, diff --git a/sound/soc/sof/imx/imx-common.h b/sound/soc/sof/imx/imx-common.h index 9bd711dbb5d03f..7a32521a8e0545 100644 --- a/sound/soc/sof/imx/imx-common.h +++ b/sound/soc/sof/imx/imx-common.h @@ -13,6 +13,12 @@ #define EXCEPT_MAX_HDR_SIZE 0x400 #define IMX8_STACK_DUMP_SIZE 32 +/* IPC4 header slot preceding each mailbox payload, matches the firmware */ +#define IMX_IPC4_HDR_SIZE 8 + +/* gap between the extended manifest and the $AM1 header, reserved by rimage */ +#define IMX_MAN4_FW_HDR_OFFSET 48 + /* chip_info refers to the data stored in struct sof_dev_desc's chip_info */ #define get_chip_info(sdev)\ ((const struct imx_chip_info *)((sdev)->pdata->desc->chip_info)) @@ -47,6 +53,30 @@ static struct sof_dev_desc sof_of_##mach_name##_desc = { \ .ops_init = mach_ops_init, \ } +/* as IMX_SOF_DEV_DESC() but also advertising IPC4; IPC3 stays the default */ +#define IMX_SOF_DEV_DESC_IPC4(mach_name, of_machs, \ + mach_chip_info, mach_ops, mach_ops_init) \ +static struct sof_dev_desc sof_of_##mach_name##_desc = { \ + .of_machines = of_machs, \ + .chip_info = mach_chip_info, \ + .ipc_supported_mask = BIT(SOF_IPC_TYPE_3) | BIT(SOF_IPC_TYPE_4), \ + .ipc_default = SOF_IPC_TYPE_3, \ + .default_fw_path = { \ + [SOF_IPC_TYPE_3] = "imx/sof", \ + [SOF_IPC_TYPE_4] = "imx/sof-ipc4", \ + }, \ + .default_tplg_path = { \ + [SOF_IPC_TYPE_3] = "imx/sof-tplg", \ + [SOF_IPC_TYPE_4] = "imx/sof-ipc4-tplg", \ + }, \ + .default_fw_filename = { \ + [SOF_IPC_TYPE_3] = "sof-" #mach_name ".ri", \ + [SOF_IPC_TYPE_4] = "sof-" #mach_name ".ri", \ + }, \ + .ops = mach_ops, \ + .ops_init = mach_ops_init, \ +} + /* to be used alongside IMX_SOF_DEV_DESC() */ #define IMX_SOF_DEV_DESC_NAME(mach_name) sof_of_##mach_name##_desc @@ -77,6 +107,11 @@ struct imx_ipc_info { int boot_mbox_offset; /* offset to region at which the mailboxes start */ int window_offset; + /* IPC4 only: window offsets relative to the SRAM bar */ + int ipc4_inbox_offset; /* DSP -> host message box */ + int ipc4_outbox_offset; /* host -> DSP message box */ + int ipc4_debug_offset; + int ipc4_fw_regs_offset; /* struct sof_ipc4_fw_registers */ }; struct imx_chip_ops { @@ -166,4 +201,6 @@ void imx8_dump(struct snd_sof_dev *sdev, u32 flags); extern const struct snd_sof_dsp_ops sof_imx_ops; +int imx_ipc4_init_data(struct snd_sof_dev *sdev); + #endif diff --git a/sound/soc/sof/imx/imx8.c b/sound/soc/sof/imx/imx8.c index 0820724dbb3491..f24e07e92e2e68 100644 --- a/sound/soc/sof/imx/imx8.c +++ b/sound/soc/sof/imx/imx8.c @@ -174,6 +174,14 @@ static int imx8m_run(struct snd_sof_dev *sdev) return reset_control_deassert(chip->run_stall); } +/* stall the core before its clocks are cut so a later reload recovers */ +static int imx8m_shutdown(struct snd_sof_dev *sdev) +{ + struct imx8m_chip_data *chip = get_chip_pdata(sdev); + + return reset_control_assert(chip->run_stall); +} + static int imx8m_probe(struct snd_sof_dev *sdev) { struct imx_common_data *common; @@ -299,7 +307,7 @@ static int imx8_ops_init(struct snd_sof_dev *sdev) sof_imx8_ops.drv = get_chip_info(sdev)->drv; sof_imx8_ops.num_drv = get_chip_info(sdev)->num_drv; - return 0; + return imx_ipc4_init_data(sdev); } static const struct imx_chip_ops imx8_chip_ops = { @@ -318,6 +326,7 @@ static const struct imx_chip_ops imx8m_chip_ops = { .probe = imx8m_probe, .core_kick = imx8m_run, .core_reset = imx8m_reset, + .core_shutdown = imx8m_shutdown, }; static const struct imx_chip_ops imx8ulp_chip_ops = { @@ -349,6 +358,11 @@ static const struct imx_chip_info imx8_chip_info = { .has_panic_code = true, .boot_mbox_offset = 0x800000, .window_offset = 0x800000, + /* IPC4 window layout, same as imx8m */ + .ipc4_inbox_offset = 0x800000 + IMX_IPC4_HDR_SIZE, + .ipc4_outbox_offset = 0x801000 + IMX_IPC4_HDR_SIZE, + .ipc4_debug_offset = 0x802000, + .ipc4_fw_regs_offset = 0x802800, }, .memory = imx8_memory_regions, .drv = imx8_dai, @@ -361,6 +375,11 @@ static const struct imx_chip_info imx8x_chip_info = { .has_panic_code = true, .boot_mbox_offset = 0x800000, .window_offset = 0x800000, + /* IPC4 window layout, same as imx8m */ + .ipc4_inbox_offset = 0x800000 + IMX_IPC4_HDR_SIZE, + .ipc4_outbox_offset = 0x801000 + IMX_IPC4_HDR_SIZE, + .ipc4_debug_offset = 0x802000, + .ipc4_fw_regs_offset = 0x802800, }, .memory = imx8_memory_regions, .drv = imx8_dai, @@ -373,6 +392,11 @@ static const struct imx_chip_info imx8m_chip_info = { .has_panic_code = true, .boot_mbox_offset = 0x800000, .window_offset = 0x800000, + /* IPC4 window layout, each box payload follows an 8-byte header slot */ + .ipc4_inbox_offset = 0x800000 + IMX_IPC4_HDR_SIZE, + .ipc4_outbox_offset = 0x801000 + IMX_IPC4_HDR_SIZE, + .ipc4_debug_offset = 0x802000, + .ipc4_fw_regs_offset = 0x802800, }, .memory = imx8m_memory_regions, .drv = imx8m_dai, @@ -385,6 +409,11 @@ static const struct imx_chip_info imx8ulp_chip_info = { .has_panic_code = true, .boot_mbox_offset = 0x800000, .window_offset = 0x800000, + /* IPC4 window layout, as imx8m but with a 0x2800 debug window */ + .ipc4_inbox_offset = 0x800000 + IMX_IPC4_HDR_SIZE, + .ipc4_outbox_offset = 0x801000 + IMX_IPC4_HDR_SIZE, + .ipc4_debug_offset = 0x802000, + .ipc4_fw_regs_offset = 0x804800, }, .has_dma_reserved = true, .memory = imx8ulp_memory_regions, @@ -442,10 +471,10 @@ static struct snd_sof_of_mach sof_imx8_machs[] = { {} }; -IMX_SOF_DEV_DESC(imx8, sof_imx8_machs, &imx8_chip_info, &sof_imx8_ops, imx8_ops_init); -IMX_SOF_DEV_DESC(imx8x, sof_imx8_machs, &imx8x_chip_info, &sof_imx8_ops, imx8_ops_init); -IMX_SOF_DEV_DESC(imx8m, sof_imx8_machs, &imx8m_chip_info, &sof_imx8_ops, imx8_ops_init); -IMX_SOF_DEV_DESC(imx8ulp, sof_imx8_machs, &imx8ulp_chip_info, &sof_imx8_ops, imx8_ops_init); +IMX_SOF_DEV_DESC_IPC4(imx8, sof_imx8_machs, &imx8_chip_info, &sof_imx8_ops, imx8_ops_init); +IMX_SOF_DEV_DESC_IPC4(imx8x, sof_imx8_machs, &imx8x_chip_info, &sof_imx8_ops, imx8_ops_init); +IMX_SOF_DEV_DESC_IPC4(imx8m, sof_imx8_machs, &imx8m_chip_info, &sof_imx8_ops, imx8_ops_init); +IMX_SOF_DEV_DESC_IPC4(imx8ulp, sof_imx8_machs, &imx8ulp_chip_info, &sof_imx8_ops, imx8_ops_init); static const struct of_device_id sof_of_imx8_ids[] = { { diff --git a/sound/soc/sof/imx/imx9.c b/sound/soc/sof/imx/imx9.c index e56e8a1c80222e..cc34bc24c6294b 100644 --- a/sound/soc/sof/imx/imx9.c +++ b/sound/soc/sof/imx/imx9.c @@ -25,13 +25,14 @@ static int imx95_ops_init(struct snd_sof_dev *sdev) sof_imx9_ops.drv = get_chip_info(sdev)->drv; sof_imx9_ops.num_drv = get_chip_info(sdev)->num_drv; - return 0; + return imx_ipc4_init_data(sdev); } static int imx95_chip_probe(struct snd_sof_dev *sdev) { struct platform_device *pdev; struct resource *res; + int ret; pdev = to_platform_device(sdev->dev); @@ -40,8 +41,17 @@ static int imx95_chip_probe(struct snd_sof_dev *sdev) return dev_err_probe(sdev->dev, -ENODEV, "failed to fetch SRAM region\n"); - return scmi_imx_lmm_reset_vector_set(IMX95_M7_LM_ID, IMX95_M7_CPU_ID, - 0, res->start); + /* the reset vector can only be set on a stopped LM, fails if already off */ + scmi_imx_lmm_operation(IMX95_M7_LM_ID, SCMI_IMX_LMM_SHUTDOWN, + SCMI_IMX_LMM_OP_FORCEFUL); + + ret = scmi_imx_lmm_reset_vector_set(IMX95_M7_LM_ID, IMX95_M7_CPU_ID, + 0, res->start); + if (ret) + return dev_err_probe(sdev->dev, ret, + "failed to set M7 reset vector\n"); + + return 0; } static int imx95_core_kick(struct snd_sof_dev *sdev) @@ -71,6 +81,11 @@ static const struct imx_chip_info imx95_chip_info = { .ipc_info = { .boot_mbox_offset = 0x6001000, .window_offset = 0x6000000, + /* IPC4 window layout; unlike i.MX8MP the host -> DSP box is first */ + .ipc4_inbox_offset = 0x6001000 + IMX_IPC4_HDR_SIZE, + .ipc4_outbox_offset = 0x6000000 + IMX_IPC4_HDR_SIZE, + .ipc4_debug_offset = 0x6003000, + .ipc4_fw_regs_offset = 0x6003800, }, .has_dma_reserved = true, .memory = imx95_memory_regions, @@ -89,7 +104,7 @@ static struct snd_sof_of_mach sof_imx9_machs[] = { } }; -IMX_SOF_DEV_DESC(imx95, sof_imx9_machs, &imx95_chip_info, &sof_imx9_ops, imx95_ops_init); +IMX_SOF_DEV_DESC_IPC4(imx95, sof_imx9_machs, &imx95_chip_info, &sof_imx9_ops, imx95_ops_init); static const struct of_device_id sof_of_imx9_ids[] = { { diff --git a/sound/soc/sof/ipc4-fw-reg.h b/sound/soc/sof/ipc4-fw-reg.h index 7b85a364a6a6c4..9308b95eec5436 100644 --- a/sound/soc/sof/ipc4-fw-reg.h +++ b/sound/soc/sof/ipc4-fw-reg.h @@ -100,6 +100,9 @@ struct sof_ipc4_llp_reading_slot { /* Number of GPDMA LLP Reading slots in FW Regs. */ #define SOF_IPC4_MAX_LLP_GPDMA_READING_SLOTS 24 +/* Number of host byte counter slots in FW Regs, indexed by host gateway id */ +#define SOF_IPC4_MAX_HOST_BYTE_CNT_SLOTS 2 + /* Number of Aggregated SNDW Reading slots in FW Regs. */ #define SOF_IPC4_MAX_LLP_SNDW_READING_SLOTS 15 @@ -135,7 +138,10 @@ struct sof_ipc4_fw_registers { u32 rom_info; u32 abi_ver; u8 slave_core_sts[SOF_IPC4_MAX_SUPPORTED_ADSP_CORES]; - u32 rsvd2[6]; + u32 rsvd2[2]; + + /* host byte counter per host gateway, carved out of rsvd2 */ + u64 host_byte_cnt[SOF_IPC4_MAX_HOST_BYTE_CNT_SLOTS]; struct sof_ipc4_pipeline_registers pipeline_regs[SOF_IPC4_MAX_PIPELINE_REG_SLOTS]; diff --git a/sound/soc/sof/ipc4-pcm.c b/sound/soc/sof/ipc4-pcm.c index cbf948d8479148..c74632362c91bc 100644 --- a/sound/soc/sof/ipc4-pcm.c +++ b/sound/soc/sof/ipc4-pcm.c @@ -1182,6 +1182,14 @@ static int sof_ipc4_pcm_pointer(struct snd_soc_component *component, if (!spcm) return -EOPNOTSUPP; + /* without link DMA the host byte counter alone gives the position */ + if (sof_ops(sdev)->get_host_byte_counter && !sof_ops(sdev)->get_dai_frame_counter) { + host_ptr = snd_sof_pcm_get_host_byte_counter(sdev, component, substream); + div64_u64_rem(host_ptr, snd_pcm_lib_buffer_bytes(substream), &host_ptr); + *pointer = bytes_to_frames(substream->runtime, host_ptr); + return 0; + } + sps = &spcm->stream[substream->stream]; time_info = sof_ipc4_sps_to_time_info(sps); if (!time_info) diff --git a/sound/soc/sof/ipc4-topology.c b/sound/soc/sof/ipc4-topology.c index 4e72a1a93d34aa..03590f88bd90a6 100644 --- a/sound/soc/sof/ipc4-topology.c +++ b/sound/soc/sof/ipc4-topology.c @@ -748,10 +748,22 @@ static int sof_ipc4_widget_setup_pcm(struct snd_sof_widget *swidget) ipc4_copier->data.copier_feature_mask |= BIT(SOF_IPC4_COPIER_FAST_MODE); skip_gtw_cfg: - ipc4_copier->gtw_attr = kzalloc_obj(*ipc4_copier->gtw_attr); - if (!ipc4_copier->gtw_attr) { - ret = -ENOMEM; - goto free_available_fmt; + /* host copiers get room for the host config TLV, filled at prepare */ + if (WIDGET_IS_AIF(swidget->id)) { + struct sof_ipc4_imx_host_config_blob *blob; + + blob = kzalloc_obj(*blob); + if (!blob) { + ret = -ENOMEM; + goto free_available_fmt; + } + ipc4_copier->gtw_attr = &blob->gw_attr; + } else { + ipc4_copier->gtw_attr = kzalloc_obj(*ipc4_copier->gtw_attr); + if (!ipc4_copier->gtw_attr) { + ret = -ENOMEM; + goto free_available_fmt; + } } ipc4_copier->copier_config = (uint32_t *)ipc4_copier->gtw_attr; @@ -925,6 +937,26 @@ static int sof_ipc4_widget_setup_comp_dai(struct snd_sof_widget *swidget) ipc4_copier->data.gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX_INTEL_DMIC(ipc4_copier->dai_index); break; + case SOF_DAI_IMX_SAI: { + struct sof_ipc4_imx_dai_config_blob *blob; + + /* no DAI_CONFIG IPC in IPC4: send the SAI config as a TLV after gtw_attr */ + ipc4_copier->data.gtw_cfg.node_id |= + SOF_IPC4_NODE_INDEX(ipc4_copier->dai_index); + + blob = kzalloc_obj(*blob); + if (!blob) { + ret = -ENOMEM; + goto free_available_fmt; + } + blob->tlv_type = SOF_IPC4_GTW_IMX_DAI_CONFIG_ID; + blob->tlv_length = sizeof(blob->dai_config); + + ipc4_copier->gtw_attr = &blob->gw_attr; + ipc4_copier->copier_config = (uint32_t *)blob; + ipc4_copier->data.gtw_cfg.config_length = sizeof(*blob) >> 2; + break; + } default: ipc4_copier->gtw_attr = kzalloc_obj(*ipc4_copier->gtw_attr); if (!ipc4_copier->gtw_attr) { @@ -2078,6 +2110,76 @@ sof_ipc4_adjust_params_to_dai_format(struct snd_sof_dev *sdev, return 0; } +/* build the SAI config TLV from the BE link hw_config, as IPC3 sof_link_sai_load() */ +static int sof_ipc4_imx_dai_config(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, + struct sof_ipc4_copier *ipc4_copier) +{ + struct sof_ipc4_imx_dai_config_blob *blob = (void *)ipc4_copier->copier_config; + struct snd_soc_tplg_hw_config *hw_config = NULL; + struct sof_ipc_dai_config *cfg = &blob->dai_config; + struct snd_sof_dai_link *slink; + bool found = false; + int i; + + list_for_each_entry(slink, &sdev->dai_link_list, list) { + if (!strcmp(slink->link->name, dai->name)) { + found = true; + break; + } + } + if (!found) { + dev_err(sdev->dev, "no DAI link found for DAI %s\n", dai->name); + return -EINVAL; + } + + found = false; + for (i = 0; i < slink->num_hw_configs; i++) { + if (dai->current_config == le32_to_cpu(slink->hw_configs[i].id)) { + hw_config = &slink->hw_configs[i]; + found = true; + break; + } + } + if (!found) { + dev_err(sdev->dev, "no matching hw_config for DAI %s\n", dai->name); + return -EINVAL; + } + + memset(cfg, 0, sizeof(*cfg)); + cfg->type = SOF_DAI_IMX_SAI; + cfg->dai_index = ipc4_copier->dai_index; + + /* base format; SOF_DAI_FMT_* match SND_SOC_DAIFMT_* values */ + cfg->format = le32_to_cpu(hw_config->fmt) & SOF_DAI_FMT_FORMAT_MASK; + + /* clock provider (wrt codec) */ + if (hw_config->bclk_provider == SND_SOC_TPLG_BCLK_CP) + cfg->format |= (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP) ? + SOF_DAI_FMT_CBP_CFP : SOF_DAI_FMT_CBP_CFC; + else + cfg->format |= (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP) ? + SOF_DAI_FMT_CBC_CFP : SOF_DAI_FMT_CBC_CFC; + + /* clock inversion */ + if (hw_config->invert_bclk) + cfg->format |= hw_config->invert_fsync ? + SOF_DAI_FMT_IB_IF : SOF_DAI_FMT_IB_NF; + else + cfg->format |= hw_config->invert_fsync ? + SOF_DAI_FMT_NB_IF : SOF_DAI_FMT_NB_NF; + + cfg->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate); + cfg->sai.bclk_rate = le32_to_cpu(hw_config->bclk_rate); + cfg->sai.fsync_rate = le32_to_cpu(hw_config->fsync_rate); + cfg->sai.mclk_direction = hw_config->mclk_direction; + cfg->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots); + cfg->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width); + cfg->sai.tx_slots = le32_to_cpu(hw_config->tx_slots); + cfg->sai.rx_slots = le32_to_cpu(hw_config->rx_slots); + + return 0; +} + static int sof_ipc4_prepare_dai_copier(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, struct snd_pcm_hw_params *params, int dir) @@ -2121,11 +2223,20 @@ sof_ipc4_prepare_dai_copier(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, ipc4_copier->dai_type, dir, &ipc4_copier->copier_config, &copier_data->gtw_cfg.config_length); + if (ret) + return ret; + + /* SAI clock and format go to the firmware in config_data */ + if (ipc4_copier->dai_type == SOF_DAI_IMX_SAI) { + ret = sof_ipc4_imx_dai_config(sdev, dai, ipc4_copier); + if (ret) + return ret; + } + /* Update the params to reflect the changes made in this function */ - if (!ret) - *params = dai_params; + *params = dai_params; - return ret; + return 0; } static void sof_ipc4_host_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget, @@ -2298,6 +2409,22 @@ _sof_ipc4_prepare_copier_module(struct snd_sof_widget *swidget, /* set gateway attributes */ gtw_attr->lp_buffer_alloc = pipeline->lp_mode; + + /* without host DMA the firmware needs the host buffer address */ + if (platform_params->use_phy_address) { + struct sof_ipc4_imx_host_config_blob *blob = + container_of(gtw_attr, struct sof_ipc4_imx_host_config_blob, + gw_attr); + + blob->tlv_type = SOF_IPC4_GTW_IMX_HOST_CONFIG_ID; + blob->tlv_length = sizeof(blob->phy_addr) + + sizeof(blob->dma_buffer_size) + + sizeof(blob->host_period_bytes); + blob->phy_addr = platform_params->phy_addr; + blob->dma_buffer_size = params_buffer_bytes(fe_params); + blob->host_period_bytes = params_period_bytes(fe_params); + copier_data->gtw_cfg.config_length = sizeof(*blob) >> 2; + } break; } case snd_soc_dapm_dai_in: @@ -4132,7 +4259,8 @@ static int sof_ipc4_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget * break; case SOF_DAI_INTEL_DMIC: case SOF_DAI_INTEL_SSP: - /* nothing to do for SSP/DMIC */ + case SOF_DAI_IMX_SAI: + /* nothing to do for SSP/DMIC/SAI */ break; default: dev_err(sdev->dev, "%s: unsupported dai type %d\n", __func__, diff --git a/sound/soc/sof/ipc4-topology.h b/sound/soc/sof/ipc4-topology.h index b538b4183897a9..94b8e7104e07bd 100644 --- a/sound/soc/sof/ipc4-topology.h +++ b/sound/soc/sof/ipc4-topology.h @@ -10,6 +10,7 @@ #define __INCLUDE_SOUND_SOF_IPC4_TOPOLOGY_H__ #include +#include #define SOF_IPC4_FW_PAGE_SIZE BIT(12) #define SOF_IPC4_FW_PAGE(x) ((((x) + BIT(12) - 1) & ~(BIT(12) - 1)) >> 12) @@ -253,6 +254,18 @@ struct sof_ipc4_gtw_attributes { uint32_t rsvd : 30; }; +/* host copier TLV describing the host PCM buffer, for platforms without host DMA */ +#define SOF_IPC4_GTW_IMX_HOST_CONFIG_ID 0x2001 + +struct sof_ipc4_imx_host_config_blob { + struct sof_ipc4_gtw_attributes gw_attr; + u32 tlv_type; + u32 tlv_length; + u32 phy_addr; + u32 dma_buffer_size; + u32 host_period_bytes; +} __packed; + /** * struct sof_ipc4_dma_device_stream_ch_map: abstract representation of * channel mapping to DMAs @@ -317,6 +330,16 @@ struct sof_ipc4_dma_config_tlv { struct sof_ipc4_dma_config dma_config; } __packed; +/* i.MX DAI copier TLV carrying the DAI config */ +#define SOF_IPC4_GTW_IMX_DAI_CONFIG_ID 0x2000 + +struct sof_ipc4_imx_dai_config_blob { + struct sof_ipc4_gtw_attributes gw_attr; + u32 tlv_type; + u32 tlv_length; + struct sof_ipc_dai_config dai_config; +} __packed; + /** struct sof_ipc4_alh_configuration_blob: ALH blob * @gw_attr: Gateway attributes * @alh_cfg: ALH configuration data diff --git a/sound/soc/sof/ipc4.c b/sound/soc/sof/ipc4.c index bf3a122ee651e8..27777e34c1829b 100644 --- a/sound/soc/sof/ipc4.c +++ b/sound/soc/sof/ipc4.c @@ -800,6 +800,26 @@ static void sof_ipc4_module_notification_handler(struct snd_sof_dev *sdev, } } +/* no host DMA interrupt on this platform: the DSP notifies once per host period */ +static void sof_ipc4_host_period_elapsed(struct snd_sof_dev *sdev, u32 gtw_id) +{ + struct snd_sof_pcm_stream *stream; + unsigned int device = gtw_id >> 1; + unsigned int direction = gtw_id & 1; + struct snd_sof_pcm *spcm; + + list_for_each_entry(spcm, &sdev->pcm_list, list) { + if (le32_to_cpu(spcm->pcm.pcm_id) != device) + continue; + + stream = &spcm->stream[direction]; + if (stream->substream && stream->substream->runtime && + !stream->substream->runtime->no_period_wakeup) + snd_sof_pcm_period_elapsed(stream->substream); + return; + } +} + static void sof_ipc4_rx_msg(struct snd_sof_dev *sdev) { struct sof_ipc4_msg *ipc4_msg = sdev->ipc->msg.rx_data; @@ -840,6 +860,9 @@ static void sof_ipc4_rx_msg(struct snd_sof_dev *sdev) case SOF_IPC4_NOTIFY_EXCEPTION_CAUGHT: snd_sof_dsp_panic(sdev, 0, true); break; + case SOF_IPC4_NOTIFY_HOST_PERIOD_ELAPSED: + sof_ipc4_host_period_elapsed(sdev, ipc4_msg->extension); + break; case SOF_IPC4_NOTIFY_MODULE_NOTIFICATION: data_size = sizeof(struct sof_ipc4_notify_module_data); handler_func = sof_ipc4_module_notification_handler; diff --git a/sound/soc/sof/pcm.c b/sound/soc/sof/pcm.c index 0161c0ae8bbdb4..be162bd3f0f29e 100644 --- a/sound/soc/sof/pcm.c +++ b/sound/soc/sof/pcm.c @@ -682,7 +682,9 @@ static int sof_pcm_new(struct snd_soc_component *component, caps->name, caps->buffer_size_min, caps->buffer_size_max); snd_pcm_set_managed_buffer(pcm->streams[stream].substream, - SNDRV_DMA_TYPE_DEV_SG, sdev->dev, + sof_ops(sdev)->pcm_contiguous_buffer ? + SNDRV_DMA_TYPE_DEV : SNDRV_DMA_TYPE_DEV_SG, + sdev->dev, 0, le32_to_cpu(caps->buffer_size_max)); /* Set the PCM device name for HDMI playback */ @@ -721,7 +723,9 @@ static int sof_pcm_new(struct snd_soc_component *component, caps->name, caps->buffer_size_min, caps->buffer_size_max); snd_pcm_set_managed_buffer(pcm->streams[stream].substream, - SNDRV_DMA_TYPE_DEV_SG, sdev->dev, + sof_ops(sdev)->pcm_contiguous_buffer ? + SNDRV_DMA_TYPE_DEV : SNDRV_DMA_TYPE_DEV_SG, + sdev->dev, 0, le32_to_cpu(caps->buffer_size_max)); return 0; diff --git a/sound/soc/sof/sof-priv.h b/sound/soc/sof/sof-priv.h index 83e4d83cc27cbb..3c95505d5b6599 100644 --- a/sound/soc/sof/sof-priv.h +++ b/sound/soc/sof/sof-priv.h @@ -370,6 +370,9 @@ struct snd_sof_dsp_ops { bool (*is_chain_dma_supported)(struct snd_sof_dev *sdev, u32 dai_type); /* optional */ + /* use contiguous, coherent PCM buffers (SNDRV_DMA_TYPE_DEV) */ + bool pcm_contiguous_buffer; /* optional */ + /* ALSA HW info flags, will be stored in snd_pcm_runtime.hw.info */ u32 hw_info;