diff --git a/include/sound/sof/ipc4/header.h b/include/sound/sof/ipc4/header.h index 0f6f1de3eee90f..d36c7ebb8d2e95 100644 --- a/include/sound/sof/ipc4/header.h +++ b/include/sound/sof/ipc4/header.h @@ -410,6 +410,7 @@ enum sof_ipc4_base_fw_params { SOF_IPC4_FW_PARAM_LIBRARIES_INFO_GET = 16, SOF_IPC4_FW_PARAM_SYSTEM_TIME = 20, SOF_IPC4_FW_PARAM_MIC_PRIVACY_STATE_CHANGE = 35, + SOF_IPC4_FW_PARAM_SOF_INFO_GET = 37, }; enum sof_ipc4_fw_config_params { @@ -456,8 +457,11 @@ struct sof_ipc4_fw_version { */ enum ipc4_fw_sof_info_params { SOF_IPC4_SOF_CODEC_INFO, + SOF_IPC4_SOF_CAPS, }; +#define SOF_IPC4_SOF_CAP_RUNTIME_SOF_INFO BIT(0) + /* Payload data for SOF_IPC4_MOD_SET_DX */ struct sof_ipc4_dx_state_info { /* core(s) to apply the change */ diff --git a/sound/soc/sof/ipc4-compress.c b/sound/soc/sof/ipc4-compress.c index 97a4ec7519055e..7ea0bfe8703064 100644 --- a/sound/soc/sof/ipc4-compress.c +++ b/sound/soc/sof/ipc4-compress.c @@ -192,6 +192,21 @@ struct sof_ipc4_codec_info_data { u32 items[]; } __packed __aligned(4); +static int sof_ipc4_compr_refresh_codec_info(struct snd_sof_dev *sdev) +{ + int ret; + + /* + * Ensure the DSP is booted (on-demand boot) and runtime codec + * capabilities are synchronized before consulting codec_info. + */ + ret = snd_sof_boot_dsp_firmware(sdev); + if (ret) + return ret; + + return sof_ipc4_query_sof_info(sdev); +} + static int sof_ipc4_compr_get_caps(struct snd_soc_component *component, struct snd_compr_stream *cstream, struct snd_compr_caps *caps) @@ -199,37 +214,47 @@ static int sof_ipc4_compr_get_caps(struct snd_soc_component *component, struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); struct snd_soc_pcm_runtime *rtd = cstream->private_data; struct sof_ipc4_fw_data *ipc4_data = sdev->private; - struct sof_ipc4_codec_info_data *codec_info = ipc4_data->codec_info; + struct sof_ipc4_codec_info_data *codec_info; int dir = cstream->direction; u64 max_buffer_size; u64 max_fragments; struct snd_sof_pcm *spcm; - int i; + int i, ret; spcm = snd_sof_find_spcm_dai(component, rtd); if (!spcm) return -EINVAL; - /* No compress support available in booted firmware */ - if (!codec_info || !codec_info->count) { - spcm_err(spcm, dir, - "Compress is not supported (no codecs available)\n"); - return -EINVAL; - } + ret = sof_ipc4_compr_refresh_codec_info(sdev); + if (ret) + return ret; - for (i = 0; i < codec_info->count; i++) { - int _dir = SOF_IPC4_CODEC_INFO_GET_DIR(codec_info->items[i]); + { + guard(mutex)(&ipc4_data->codec_info_mutex); + + codec_info = ipc4_data->codec_info; - if (_dir == dir) { - int id = SOF_IPC4_CODEC_INFO_GET_ID(codec_info->items[i]); + /* No compress support available in booted firmware */ + if (!codec_info || !codec_info->count) { + spcm_err(spcm, dir, + "Compress is not supported (no codecs available)\n"); + return -EINVAL; + } - if (caps->num_codecs < ARRAY_SIZE(caps->codecs)) { - spcm_dbg(spcm, dir, "codec#%d: %d\n", - caps->num_codecs, id); - caps->codecs[caps->num_codecs++] = id; - } else { - spcm_dbg(spcm, dir, "codec#%d: %d ignored\n", - caps->num_codecs, id); + for (i = 0; i < codec_info->count; i++) { + int _dir = SOF_IPC4_CODEC_INFO_GET_DIR(codec_info->items[i]); + + if (_dir == dir) { + int id = SOF_IPC4_CODEC_INFO_GET_ID(codec_info->items[i]); + + if (caps->num_codecs < ARRAY_SIZE(caps->codecs)) { + spcm_dbg(spcm, dir, "codec#%d: %d\n", + caps->num_codecs, id); + caps->codecs[caps->num_codecs++] = id; + } else { + spcm_dbg(spcm, dir, "codec#%d: %d ignored\n", + caps->num_codecs, id); + } } } } @@ -319,9 +344,13 @@ static bool sof_ipc4_compr_codec_supported(struct snd_sof_dev *sdev, int codec_id, int dir) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; - struct sof_ipc4_codec_info_data *codec_info = ipc4_data->codec_info; + struct sof_ipc4_codec_info_data *codec_info; int i; + guard(mutex)(&ipc4_data->codec_info_mutex); + + codec_info = ipc4_data->codec_info; + /* No compress support available in booted firmware */ if (!codec_info || !codec_info->count) return false; @@ -368,6 +397,10 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, return -ENODEV; } + ret = sof_ipc4_compr_refresh_codec_info(sdev); + if (ret) + return ret; + if (!sof_ipc4_compr_codec_supported(sdev, params->codec.id, dir)) { spcm_err(spcm, dir, "Unsupported codec id: %u\n", params->codec.id); return -EINVAL; @@ -486,14 +519,6 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, process->init_ext_module_data = compr_data; process->init_ext_module_size = sizeof(*compr_data); - /* - * Make sure that the DSP is booted up, which might not be the - * case if the on-demand DSP boot is used - */ - ret = snd_sof_boot_dsp_firmware(sdev); - if (ret) - goto clear_init_ext; - /* set the host DMA ID */ if (tplg_ops && tplg_ops->host_config) tplg_ops->host_config(sdev, host_swidget, platform_params); diff --git a/sound/soc/sof/ipc4-loader.c b/sound/soc/sof/ipc4-loader.c index 1da51f7490e579..189c7d6f3462b8 100644 --- a/sound/soc/sof/ipc4-loader.c +++ b/sound/soc/sof/ipc4-loader.c @@ -240,6 +240,14 @@ static int sof_ipc4_load_library(struct snd_sof_dev *sdev, unsigned long lib_id, if (unlikely(ret)) goto release; + if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) { + ret = sof_ipc4_query_sof_info(sdev); + if (ret) + dev_warn(sdev->dev, + "%s: Failed to refresh runtime SOF info after loading %s: %d\n", + __func__, lib_filename, ret); + } + return 0; release: @@ -417,36 +425,121 @@ static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev) return 0; } -static int sof_ipc4_query_sof_info(struct snd_sof_dev *sdev, - void *sof_info_data, u32 sof_info_size) +static int sof_ipc4_parse_sof_info(struct snd_sof_dev *sdev, + void *sof_info_data, u32 sof_info_size, + bool replace) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; + void *new_codec_info = NULL; struct sof_ipc4_tuple *tuple; + size_t tuple_size; size_t offset = 0; + int ret = 0; - /* - * No size validation is needed as the sof_info data is coming from - * firmware as part of the firmware config, it is guarantied to be - * correct. - */ while (offset < sof_info_size) { + if (sof_info_size - offset < sizeof(*tuple)) { + dev_err(sdev->dev, "Invalid SOF info tuple header at offset %zu\n", + offset); + ret = -EINVAL; + goto out_free; + } + tuple = (struct sof_ipc4_tuple *)((u8 *)sof_info_data + offset); + tuple_size = sizeof(*tuple) + tuple->size; + if (tuple_size < sizeof(*tuple) || tuple_size > sof_info_size - offset) { + dev_err(sdev->dev, "Invalid SOF info tuple size %u at offset %zu\n", + tuple->size, offset); + ret = -EINVAL; + goto out_free; + } switch (tuple->type) { case SOF_IPC4_SOF_CODEC_INFO: - ipc4_data->codec_info = devm_kmemdup(sdev->dev, tuple->value, - tuple->size, GFP_KERNEL); - if (!ipc4_data->codec_info) - return -ENOMEM; + { + u32 count; + + if (tuple->size < sizeof(u32)) { + dev_err(sdev->dev, "Invalid codec info tuple size %u\n", + tuple->size); + ret = -EINVAL; + goto out_free; + } + + count = *(const u32 *)tuple->value; + if (count > (tuple->size - sizeof(u32)) / sizeof(u32)) { + dev_err(sdev->dev, + "Invalid codec info item count %u for size %u\n", + count, tuple->size); + ret = -EINVAL; + goto out_free; + } + + if (count) { + kfree(new_codec_info); + new_codec_info = kmemdup(tuple->value, tuple->size, GFP_KERNEL); + if (!new_codec_info) { + ret = -ENOMEM; + goto out_free; + } + } + break; + } + case SOF_IPC4_SOF_CAPS: + if (tuple->size < sizeof(u32)) { + dev_err(sdev->dev, "Invalid SOF caps tuple size %u\n", + tuple->size); + ret = -EINVAL; + goto out_free; + } + if (*(const u32 *)tuple->value & SOF_IPC4_SOF_CAP_RUNTIME_SOF_INFO) + ipc4_data->sof_info_runtime_supported = true; break; default: break; } - offset += sizeof(*tuple) + tuple->size; + offset += tuple_size; } - return 0; + if (replace || new_codec_info) { + guard(mutex)(&ipc4_data->codec_info_mutex); + kfree(ipc4_data->codec_info); + ipc4_data->codec_info = new_codec_info; + new_codec_info = NULL; + } + +out_free: + kfree(new_codec_info); + return ret; +} + +int sof_ipc4_query_sof_info(struct snd_sof_dev *sdev) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + const struct sof_ipc_ops *iops = sdev->ipc->ops; + struct sof_ipc4_msg msg = {{ 0 }}; + int ret; + + if (!ipc4_data->sof_info_runtime_supported) + return 0; + + msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); + msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID); + msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID); + msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_SOF_INFO_GET); + + msg.data_size = sdev->ipc->max_payload_size; + msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL); + if (!msg.data_ptr) + return -ENOMEM; + + ret = iops->set_get_data(sdev, &msg, msg.data_size, false); + if (!ret) + ret = sof_ipc4_parse_sof_info(sdev, msg.data_ptr, msg.data_size, true); + + kfree(msg.data_ptr); + return ret; } int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) @@ -525,7 +618,7 @@ int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) ipc4_data->libraries_restored = ipc4_data->fw_context_save; break; case SOF_IPC4_FW_CFG_SOF_INFO: - ret = sof_ipc4_query_sof_info(sdev, tuple->value, tuple->size); + ret = sof_ipc4_parse_sof_info(sdev, tuple->value, tuple->size, false); if (ret) goto out; break; @@ -536,6 +629,12 @@ int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) offset += sizeof(*tuple) + tuple->size; } + if (ipc4_data->sof_info_runtime_supported) { + ret = sof_ipc4_query_sof_info(sdev); + if (ret) + goto out; + } + /* Get the hardware configuration */ msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); @@ -591,6 +690,9 @@ int sof_ipc4_reload_fw_libraries(struct snd_sof_dev *sdev) } } + if (!ret && ipc4_data->sof_info_runtime_supported) + ret = sof_ipc4_query_sof_info(sdev); + return ret; } diff --git a/sound/soc/sof/ipc4-priv.h b/sound/soc/sof/ipc4-priv.h index 31f7d68029ce71..b9d9a206744f19 100644 --- a/sound/soc/sof/ipc4-priv.h +++ b/sound/soc/sof/ipc4-priv.h @@ -83,6 +83,9 @@ struct snd_ipc4_nhlt { * * @codec_info: Information about the available codecs in booted firmware. The * data is to be used by the code for compressed support. + * @codec_info_mutex: Mutex protecting updates and reads of @codec_info + * @sof_info_runtime_supported: Firmware supports runtime SOF info queries via + * SOF_IPC4_FW_PARAM_SOF_INFO_GET * @load_library: Callback function for platform dependent library loading * @pipeline_state_mutex: Mutex to protect pipeline triggers, ref counts, states and deletion */ @@ -100,6 +103,8 @@ struct sof_ipc4_fw_data { struct list_head nhlt_list; void *codec_info; + struct mutex codec_info_mutex; /* protect runtime updates to codec_info */ + bool sof_info_runtime_supported; int (*load_library)(struct snd_sof_dev *sdev, struct sof_ipc4_fw_library *fw_lib, bool reload); @@ -145,6 +150,7 @@ int sof_ipc4_mtrace_update_pos(struct snd_sof_dev *sdev, int core); int sof_ipc4_complete_split_release(struct snd_sof_dev *sdev); int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev); +int sof_ipc4_query_sof_info(struct snd_sof_dev *sdev); int sof_ipc4_reload_fw_libraries(struct snd_sof_dev *sdev); struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev, const guid_t *uuid); diff --git a/sound/soc/sof/ipc4.c b/sound/soc/sof/ipc4.c index bf3a122ee651e8..a43932a43852bb 100644 --- a/sound/soc/sof/ipc4.c +++ b/sound/soc/sof/ipc4.c @@ -942,6 +942,7 @@ static int sof_ipc4_init(struct snd_sof_dev *sdev) int inbox_offset; mutex_init(&ipc4_data->pipeline_state_mutex); + mutex_init(&ipc4_data->codec_info_mutex); xa_init_flags(&ipc4_data->fw_lib_xa, XA_FLAGS_ALLOC); @@ -992,6 +993,11 @@ static void sof_ipc4_exit(struct snd_sof_dev *sdev) } xa_destroy(&ipc4_data->fw_lib_xa); + + mutex_lock(&ipc4_data->codec_info_mutex); + kfree(ipc4_data->codec_info); + ipc4_data->codec_info = NULL; + mutex_unlock(&ipc4_data->codec_info_mutex); } static int sof_ipc4_post_boot(struct snd_sof_dev *sdev)