diff --git a/include/sound/sof/ipc4/header.h b/include/sound/sof/ipc4/header.h index 0f6f1de3eee90f..f57e53c1385fd5 100644 --- a/include/sound/sof/ipc4/header.h +++ b/include/sound/sof/ipc4/header.h @@ -410,6 +410,8 @@ 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, + SOF_IPC4_FW_PARAM_SOF_LIBRARY_UNLOAD = 38, }; enum sof_ipc4_fw_config_params { @@ -456,8 +458,12 @@ 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) +#define SOF_IPC4_SOF_CAP_LIBRARY_UNLOAD BIT(1) + /* Payload data for SOF_IPC4_MOD_SET_DX */ struct sof_ipc4_dx_state_info { /* core(s) to apply the change */ diff --git a/include/uapi/sound/sof/tokens.h b/include/uapi/sound/sof/tokens.h index d42adbef04781b..f866b6a55ca8a3 100644 --- a/include/uapi/sound/sof/tokens.h +++ b/include/uapi/sound/sof/tokens.h @@ -233,4 +233,12 @@ #define SOF_TKN_AMD_ACP_SDW_RATE 2100 #define SOF_TKN_AMD_ACP_SDW_CH 2101 +/* MEDIA CODEC */ +#define SOF_TKN_MEDIA_CODEC_ID 2200 +#define SOF_TKN_MEDIA_CODEC_MIN_RATE 2201 +#define SOF_TKN_MEDIA_CODEC_MAX_RATE 2202 +#define SOF_TKN_MEDIA_CODEC_CHANNELS 2203 +#define SOF_TKN_MEDIA_CODEC_BIT_DEPTH 2204 +#define SOF_TKN_MEDIA_CODEC_MODULE_UUID 2205 + #endif diff --git a/sound/soc/sof/ipc4-compress.c b/sound/soc/sof/ipc4-compress.c index 97a4ec7519055e..995ac46bcd3076 100644 --- a/sound/soc/sof/ipc4-compress.c +++ b/sound/soc/sof/ipc4-compress.c @@ -28,28 +28,49 @@ 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_codec_swidget(struct snd_sof_dev *sdev, + 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]; + struct snd_sof_widget *host_swidget; + struct snd_sof_widget *swidget; struct snd_soc_dapm_widget *widget; int i; - /* Find the (first) compr module in path */ - for_each_dapm_widgets(sps->list, i, widget) { - struct snd_sof_widget *swidget = widget->dobj.private; + if (sps->list) { + /* Find the (first) compr module in active path */ + for_each_dapm_widgets(sps->list, i, widget) { + swidget = widget->dobj.private; - if (!swidget) - continue; + if (swidget && swidget->widget->id == id) + return swidget; + } + return NULL; + } + + host_swidget = snd_sof_find_swidget_by_comp_id(sdev, sps->comp_id); + if (!host_swidget) + return NULL; - if (swidget->widget->id == id) - return swidget->private; + list_for_each_entry(swidget, &sdev->widget_list, list) { + if (swidget->pipeline_id == host_swidget->pipeline_id && + swidget->id == id) + return swidget; } return NULL; } +static struct sof_ipc4_process * +sof_ipc4_compr_get_module(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir) +{ + struct snd_sof_widget *swidget = sof_ipc4_compr_get_codec_swidget(sdev, 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; @@ -192,6 +213,24 @@ struct sof_ipc4_codec_info_data { u32 items[]; } __packed __aligned(4); +static void sof_ipc4_compr_caps_add_codec(struct snd_sof_pcm *spcm, int dir, + struct snd_compr_caps *caps, u32 id) +{ + int i; + + for (i = 0; i < caps->num_codecs; i++) { + if (caps->codecs[i] == id) + return; + } + + if (caps->num_codecs < ARRAY_SIZE(caps->codecs)) { + spcm_dbg(spcm, dir, "codec#%d: %u\n", caps->num_codecs, id); + caps->codecs[caps->num_codecs++] = id; + } else { + spcm_dbg(spcm, dir, "codec#%d: %u ignored\n", caps->num_codecs, id); + } +} + static int sof_ipc4_compr_get_caps(struct snd_soc_component *component, struct snd_compr_stream *cstream, struct snd_compr_caps *caps) @@ -199,7 +238,7 @@ 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 snd_sof_widget *codec_swidget; int dir = cstream->direction; u64 max_buffer_size; u64 max_fragments; @@ -210,30 +249,34 @@ static int sof_ipc4_compr_get_caps(struct snd_soc_component *component, 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; - } - - for (i = 0; i < codec_info->count; i++) { - int _dir = SOF_IPC4_CODEC_INFO_GET_DIR(codec_info->items[i]); + scoped_guard(mutex, &ipc4_data->codec_info_mutex) { + struct sof_ipc4_codec_info_data *codec_info = ipc4_data->codec_info; - if (_dir == dir) { - int id = SOF_IPC4_CODEC_INFO_GET_ID(codec_info->items[i]); + if (codec_info) { + for (i = 0; i < codec_info->count; i++) { + int _dir = SOF_IPC4_CODEC_INFO_GET_DIR(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); + if (_dir == dir) + sof_ipc4_compr_caps_add_codec(spcm, dir, caps, + SOF_IPC4_CODEC_INFO_GET_ID(codec_info->items[i])); } } } + codec_swidget = sof_ipc4_compr_get_codec_swidget(sdev, spcm, dir); + if (codec_swidget) { + for (i = 0; i < codec_swidget->num_media_formats; i++) + sof_ipc4_compr_caps_add_codec(spcm, dir, caps, + codec_swidget->media_formats[i].codec_id); + } + + /* No compress support available in firmware or topology */ + if (!caps->num_codecs) { + spcm_err(spcm, dir, + "Compress is not supported (no codecs available)\n"); + return -EINVAL; + } + caps->direction = dir; caps->min_fragment_size = sof_ipc4_compr_calc_min_fragment_size(&spcm->stream[dir]); @@ -319,9 +362,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; @@ -337,6 +384,112 @@ sof_ipc4_compr_codec_supported(struct snd_sof_dev *sdev, int codec_id, int dir) return false; } +static u32 sof_ipc4_compr_codec_bit_depth(const struct snd_codec *codec) +{ + switch (codec->id) { + case SND_AUDIOCODEC_PCM: + { + int width = snd_pcm_format_width((snd_pcm_format_t)codec->format); + + return width > 0 ? width : 0; + } + case SND_AUDIOCODEC_VORBIS: + return 16; + case SND_AUDIOCODEC_FLAC: + return codec->options.flac_d.sample_size; + default: + if (codec->format) { + int width = snd_pcm_format_width((snd_pcm_format_t)codec->format); + + if (width > 0) + return width; + } + return 0; + } +} + +static bool sof_ipc4_compr_media_fmt_matches(const struct sof_media_codec_fmt *fmt, + const struct snd_codec *codec, + u32 bit_depth) +{ + if (fmt->codec_id != codec->id) + return false; + + if (codec->sample_rate) { + if (fmt->min_rate && codec->sample_rate < fmt->min_rate) + return false; + if (fmt->max_rate && codec->sample_rate > fmt->max_rate) + return false; + } + + if (codec->ch_out && fmt->channels && fmt->channels != codec->ch_out) + return false; + + if (bit_depth && fmt->bit_depth && fmt->bit_depth != bit_depth) + return false; + + return true; +} + +static bool sof_ipc4_compr_has_matching_fmt(struct snd_sof_widget *codec_swidget, + const struct snd_codec *codec) +{ + u32 bit_depth = sof_ipc4_compr_codec_bit_depth(codec); + int i; + + for (i = 0; i < codec_swidget->num_media_formats; i++) { + if (sof_ipc4_compr_media_fmt_matches(&codec_swidget->media_formats[i], + codec, bit_depth)) + return true; + } + + return false; +} + +static int sof_ipc4_compr_resolve_media_module(struct snd_sof_dev *sdev, + struct snd_sof_widget *codec_swidget, + const struct snd_codec *codec) +{ + u32 bit_depth = sof_ipc4_compr_codec_bit_depth(codec); + struct sof_ipc4_fw_module *fw_module = NULL; + int i, ret; + + /* Tier 1: prefer a matching module UUID already resident in DSP memory */ + for (i = 0; i < codec_swidget->num_media_formats; i++) { + const struct sof_media_codec_fmt *fmt = &codec_swidget->media_formats[i]; + + if (!sof_ipc4_compr_media_fmt_matches(fmt, codec, bit_depth)) + continue; + + fw_module = sof_ipc4_find_loaded_module_by_uuid(sdev, &fmt->module_uuid); + if (fw_module) + break; + } + + /* Tier 2: boot DSP if needed and demand-load (or purge+load) candidate UUIDs */ + if (!fw_module) { + ret = snd_sof_boot_dsp_firmware(sdev); + if (ret) + return ret; + + for (i = 0; i < codec_swidget->num_media_formats; i++) { + const struct sof_media_codec_fmt *fmt = &codec_swidget->media_formats[i]; + + if (!sof_ipc4_compr_media_fmt_matches(fmt, codec, bit_depth)) + continue; + + fw_module = sof_ipc4_find_module_by_uuid(sdev, &fmt->module_uuid); + if (fw_module) + break; + } + } + + if (!fw_module) + return -EINVAL; + + return sof_ipc4_process_bind_module(sdev, codec_swidget, fw_module); +} + static int sof_ipc4_compr_set_params(struct snd_soc_component *component, struct snd_compr_stream *cstream, struct snd_compr_params *params) @@ -348,6 +501,7 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, struct snd_sof_platform_stream_params *platform_params; struct sof_ipc4_timestamp_info *time_info; struct snd_compr_params *compr_params; + struct snd_sof_widget *codec_swidget; struct snd_sof_widget *host_swidget; struct sof_ipc4_process *process; struct snd_pcm_hw_params p = {0}; @@ -368,7 +522,13 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, return -ENODEV; } - if (!sof_ipc4_compr_codec_supported(sdev, params->codec.id, dir)) { + codec_swidget = sof_ipc4_compr_get_codec_swidget(sdev, spcm, dir); + if (codec_swidget && codec_swidget->num_media_formats) { + if (!sof_ipc4_compr_has_matching_fmt(codec_swidget, ¶ms->codec)) { + spcm_err(spcm, dir, "Unsupported codec id: %u\n", params->codec.id); + return -EINVAL; + } + } else 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; } @@ -388,6 +548,15 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, return ret; } + if (codec_swidget && codec_swidget->num_media_formats) { + ret = sof_ipc4_compr_resolve_media_module(sdev, codec_swidget, ¶ms->codec); + if (ret) { + spcm_err(spcm, dir, "Failed to resolve module for codec id %u: %d\n", + params->codec.id, ret); + return ret; + } + } + /* save the compress params */ compr_params = &spcm->cparams[dir]; memcpy(compr_params, params, sizeof(*params)); @@ -468,7 +637,7 @@ static int sof_ipc4_compr_set_params(struct snd_soc_component *component, goto free_pages; } - process = sof_ipc4_compr_get_module(spcm, dir); + process = sof_ipc4_compr_get_module(sdev, spcm, dir); if (!process) { ret = -EINVAL; goto free_list; @@ -559,6 +728,49 @@ static int sof_ipc4_compr_get_params(struct snd_soc_component *component, return 0; } +/* + * On DRAIN, hand the host copier the total committed gateway byte count + * (crtd->total_bytes_available). The HDA compress gateway free-runs its write + * pointer around the ring, so the firmware cannot detect end-of-input from the + * gateway alone - it flags EOS once it has delivered this many bytes downstream. + */ +static void sof_ipc4_compr_send_drain_bytes(struct snd_sof_dev *sdev, + struct snd_sof_pcm *spcm, int dir, + struct snd_compr_stream *cstream) +{ + const struct sof_ipc_ops *iops = sdev->ipc->ops; + struct snd_compr_runtime *crtd = cstream->runtime; + struct sof_ipc4_fw_module *fw_module; + struct snd_sof_widget *host_swidget; + struct sof_ipc4_msg msg = {{ 0 }}; + u64 drain_bytes; + int ret; + + host_swidget = snd_sof_find_swidget_by_comp_id(sdev, spcm->stream[dir].comp_id); + if (!host_swidget || !host_swidget->module_info) { + spcm_err(spcm, dir, "drain: no host widget for comp_id %d\n", + spcm->stream[dir].comp_id); + return; + } + fw_module = host_swidget->module_info; + drain_bytes = crtd->total_bytes_available; + + msg.primary = fw_module->man4_module_entry.id; + msg.primary |= SOF_IPC4_MOD_INSTANCE(host_swidget->instance_id); + msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); + msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.extension = + SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_COPIER_MODULE_CFG_PARAM_DRAIN_BYTES); + msg.data_ptr = &drain_bytes; + msg.data_size = sizeof(drain_bytes); + + ret = iops->set_get_data(sdev, &msg, msg.data_size, true); + if (ret < 0) + spcm_err(spcm, dir, "drain: failed to set drain_bytes: %d\n", ret); + else + spcm_dbg(spcm, dir, "drain: sent drain_bytes=%llu\n", drain_bytes); +} + static int sof_ipc4_compr_trigger(struct snd_soc_component *component, struct snd_compr_stream *cstream, int cmd) { @@ -601,6 +813,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_send_drain_bytes(sdev, spcm, dir, cstream); break; case SND_COMPR_TRIGGER_PARTIAL_DRAIN: case SND_COMPR_TRIGGER_NEXT_TRACK: diff --git a/sound/soc/sof/ipc4-loader.c b/sound/soc/sof/ipc4-loader.c index 1da51f7490e579..63b58f6730d687 100644 --- a/sound/soc/sof/ipc4-loader.c +++ b/sound/soc/sof/ipc4-loader.c @@ -179,7 +179,8 @@ static size_t sof_ipc4_fw_parse_basefw_ext_man(struct snd_sof_dev *sdev) } static int sof_ipc4_load_library(struct snd_sof_dev *sdev, unsigned long lib_id, - const char *lib_filename, bool optional) + const char *lib_filename, bool optional, + bool unloadable) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; struct sof_ipc4_fw_library *fw_lib; @@ -227,6 +228,7 @@ static int sof_ipc4_load_library(struct snd_sof_dev *sdev, unsigned long lib_id, fw_lib->sof_fw.payload_offset = payload_offset; fw_lib->id = lib_id; + fw_lib->unloadable = unloadable; /* Fix up the module ID numbers within the library */ for (i = 0; i < fw_lib->num_modules; i++) @@ -240,6 +242,12 @@ static int sof_ipc4_load_library(struct snd_sof_dev *sdev, unsigned long lib_id, if (unlikely(ret)) goto release; + 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: @@ -312,7 +320,7 @@ int sof_ipc4_complete_split_release(struct snd_sof_dev *sdev) return -ENOMEM; } - ret = sof_ipc4_load_library(sdev, lib_id, lib_filename, true); + ret = sof_ipc4_load_library(sdev, lib_id, lib_filename, true, false); if (ret) dev_warn(sdev->dev, "%s: Failed to load %s: %d\n", __func__, lib_filename, ret); @@ -344,20 +352,128 @@ static int sof_ipc4_load_library_by_uuid(struct snd_sof_dev *sdev, if (!lib_filename) return -ENOMEM; - ret = sof_ipc4_load_library(sdev, lib_id, lib_filename, false); + ret = sof_ipc4_load_library(sdev, lib_id, lib_filename, false, true); kfree(lib_filename); return ret; } -struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev, - const guid_t *uuid) +static bool sof_ipc4_fw_library_is_idle(struct sof_ipc4_fw_library *fw_lib) +{ + int i; + + if (!fw_lib || !fw_lib->id || !fw_lib->unloadable) + return false; + + for (i = 0; i < fw_lib->num_modules; i++) { + if (!ida_is_empty(&fw_lib->modules[i].m_ida)) + return false; + } + + return true; +} + +static void sof_ipc4_detach_library_widgets(struct snd_sof_dev *sdev, + struct sof_ipc4_fw_library *fw_lib) +{ + struct sof_ipc4_fw_module *mods_end = fw_lib->modules + fw_lib->num_modules; + struct snd_sof_widget *swidget; + + if (!fw_lib->modules || !fw_lib->num_modules) + return; + + list_for_each_entry(swidget, &sdev->widget_list, list) { + struct sof_ipc4_fw_module *fw_module = swidget->module_info; + + if (fw_module >= fw_lib->modules && fw_module < mods_end) + swidget->module_info = NULL; + } +} + +int sof_ipc4_unload_library(struct snd_sof_dev *sdev, unsigned long lib_id) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + const struct sof_ipc_ops *iops = sdev->ipc->ops; + struct sof_ipc4_fw_library *fw_lib; + struct sof_ipc4_msg msg = {{ 0 }}; + u32 id_payload; + int ret; + + if (!ipc4_data->library_unload_supported) + return -EOPNOTSUPP; + + if (!lib_id) + return -EINVAL; + + fw_lib = xa_load(&ipc4_data->fw_lib_xa, lib_id); + if (!fw_lib) + return -ENOENT; + + if (!sof_ipc4_fw_library_is_idle(fw_lib)) + return -EBUSY; + + id_payload = lib_id; + 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_LIBRARY_UNLOAD); + msg.data_ptr = &id_payload; + msg.data_size = sizeof(id_payload); + + ret = iops->set_get_data(sdev, &msg, msg.data_size, true); + if (ret) { + dev_warn(sdev->dev, "Failed to unload library id %lu: %d\n", lib_id, ret); + return ret; + } + + dev_dbg(sdev->dev, "Unloaded library id %lu\n", lib_id); + sof_ipc4_detach_library_widgets(sdev, fw_lib); + xa_erase(&ipc4_data->fw_lib_xa, lib_id); + release_firmware(fw_lib->sof_fw.fw); + devm_kfree(sdev->dev, fw_lib->modules); + devm_kfree(sdev->dev, fw_lib); + + ret = sof_ipc4_query_sof_info(sdev); + if (ret) + dev_warn(sdev->dev, + "Failed to refresh runtime SOF info after unloading lib %lu: %d\n", + lib_id, ret); + + return 0; +} + +static int sof_ipc4_purge_idle_library(struct snd_sof_dev *sdev, unsigned long *freed_id) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; struct sof_ipc4_fw_library *fw_lib; unsigned long lib_id; - int i, ret; + + if (!ipc4_data->library_unload_supported) + return -EOPNOTSUPP; + + xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, 1) { + if (!sof_ipc4_fw_library_is_idle(fw_lib)) + continue; + + if (!sof_ipc4_unload_library(sdev, lib_id)) { + if (freed_id) + *freed_id = lib_id; + return 0; + } + } + + return -EBUSY; +} + +struct sof_ipc4_fw_module *sof_ipc4_find_loaded_module_by_uuid(struct snd_sof_dev *sdev, + const guid_t *uuid) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_fw_library *fw_lib; + unsigned long lib_id; + int i; if (guid_is_null(uuid)) return NULL; @@ -369,28 +485,52 @@ struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev } } - /* - * Do not attempt to load external library in case the maximum number of - * firmware libraries have been already loaded - */ - if ((lib_id + 1) == ipc4_data->max_libs_count) { - dev_err(sdev->dev, - "%s: Maximum allowed number of libraries reached (%u)\n", - __func__, ipc4_data->max_libs_count); - return NULL; + return NULL; +} + +struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev, + const guid_t *uuid) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_fw_library *fw_lib; + struct sof_ipc4_fw_module *fw_module; + unsigned long lib_id = 0; + int i, ret; + + fw_module = sof_ipc4_find_loaded_module_by_uuid(sdev, uuid); + if (fw_module || guid_is_null(uuid)) + return fw_module; + + for (lib_id = 1; lib_id < ipc4_data->max_libs_count; lib_id++) { + if (!xa_load(&ipc4_data->fw_lib_xa, lib_id)) + break; + } + + if (lib_id >= ipc4_data->max_libs_count) { + ret = sof_ipc4_purge_idle_library(sdev, &lib_id); + if (ret) { + dev_err(sdev->dev, + "%s: Maximum allowed number of libraries reached (%u)\n", + __func__, ipc4_data->max_libs_count); + return NULL; + } } /* The module cannot be found, try to load it as a library */ - ret = sof_ipc4_load_library_by_uuid(sdev, lib_id + 1, uuid); + ret = sof_ipc4_load_library_by_uuid(sdev, lib_id, uuid); + if (ret && !sof_ipc4_purge_idle_library(sdev, NULL)) + ret = sof_ipc4_load_library_by_uuid(sdev, lib_id, uuid); if (ret) return NULL; /* Look for the module in the newly loaded library, it should be available now */ - xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, lib_id) { - for (i = 0; i < fw_lib->num_modules; i++) { - if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid)) - return &fw_lib->modules[i]; - } + fw_lib = xa_load(&ipc4_data->fw_lib_xa, lib_id); + if (!fw_lib) + return NULL; + + for (i = 0; i < fw_lib->num_modules; i++) { + if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid)) + return &fw_lib->modules[i]; } return NULL; @@ -417,36 +557,138 @@ 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 (new_codec_info) { + dev_err(sdev->dev, "Duplicate codec info tuple\n"); + ret = -EINVAL; + goto out_free; + } + + if (count) { + new_codec_info = kmemdup(tuple->value, tuple->size, GFP_KERNEL); + if (!new_codec_info) { + ret = -ENOMEM; + goto out_free; + } + } + dev_dbg(sdev->dev, "SOF codec_info %s: %u items\n", + replace ? "refreshed" : "parsed", count); break; + } + case SOF_IPC4_SOF_CAPS: + { + u32 caps; + + if (tuple->size < sizeof(u32)) { + dev_err(sdev->dev, "Invalid SOF caps tuple size %u\n", + tuple->size); + ret = -EINVAL; + goto out_free; + } + caps = *(const u32 *)tuple->value; + ipc4_data->sof_info_runtime_supported = + !!(caps & SOF_IPC4_SOF_CAP_RUNTIME_SOF_INFO); + ipc4_data->library_unload_supported = + !!(caps & SOF_IPC4_SOF_CAP_LIBRARY_UNLOAD); + dev_dbg(sdev->dev, "SOF caps: 0x%x (runtime_info=%d, lib_unload=%d)\n", + caps, ipc4_data->sof_info_runtime_supported, + ipc4_data->library_unload_supported); + 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 +767,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; @@ -579,10 +821,12 @@ int sof_ipc4_reload_fw_libraries(struct snd_sof_dev *sdev) { struct sof_ipc4_fw_data *ipc4_data = sdev->private; struct sof_ipc4_fw_library *fw_lib; + bool has_ext_libs = false; unsigned long lib_id; int ret = 0; xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, 1) { + has_ext_libs = true; ret = ipc4_data->load_library(sdev, fw_lib, true); if (ret) { dev_err(sdev->dev, "%s: Failed to reload library: %s, %d\n", @@ -591,6 +835,10 @@ int sof_ipc4_reload_fw_libraries(struct snd_sof_dev *sdev) } } + if (!ret && has_ext_libs && !ipc4_data->libraries_restored && + 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..2c63147e8fd5d1 100644 --- a/sound/soc/sof/ipc4-priv.h +++ b/sound/soc/sof/ipc4-priv.h @@ -49,6 +49,7 @@ struct sof_ipc4_fw_module { * are not supported * @num_modules : Number of FW modules in the library * @modules: Array of FW modules + * @unloadable: True for demand-loaded UUID libraries that may be purged */ struct sof_ipc4_fw_library { struct sof_firmware sof_fw; @@ -56,6 +57,7 @@ struct sof_ipc4_fw_library { u32 id; int num_modules; struct sof_ipc4_fw_module *modules; + bool unloadable; }; struct snd_ipc4_nhlt { @@ -83,6 +85,11 @@ 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 + * @library_unload_supported: Firmware supports unloading idle libraries via + * SOF_IPC4_FW_PARAM_SOF_LIBRARY_UNLOAD * @load_library: Callback function for platform dependent library loading * @pipeline_state_mutex: Mutex to protect pipeline triggers, ref counts, states and deletion */ @@ -100,6 +107,9 @@ 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; + bool library_unload_supported; int (*load_library)(struct snd_sof_dev *sdev, struct sof_ipc4_fw_library *fw_lib, bool reload); @@ -145,7 +155,11 @@ 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); +int sof_ipc4_unload_library(struct snd_sof_dev *sdev, unsigned long lib_id); +struct sof_ipc4_fw_module *sof_ipc4_find_loaded_module_by_uuid(struct snd_sof_dev *sdev, + const guid_t *uuid); 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-topology.c b/sound/soc/sof/ipc4-topology.c index 4e72a1a93d34aa..828a64fd4044ff 100644 --- a/sound/soc/sof/ipc4-topology.c +++ b/sound/soc/sof/ipc4-topology.c @@ -194,6 +194,21 @@ static const struct sof_topology_token asrc_tokens[] = { offsetof(struct sof_ipc4_asrc_data, asrc_mode)}, }; +static const struct sof_topology_token media_codec_tokens[] = { + {SOF_TKN_MEDIA_CODEC_ID, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_media_codec_fmt, codec_id)}, + {SOF_TKN_MEDIA_CODEC_MIN_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_media_codec_fmt, min_rate)}, + {SOF_TKN_MEDIA_CODEC_MAX_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_media_codec_fmt, max_rate)}, + {SOF_TKN_MEDIA_CODEC_CHANNELS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_media_codec_fmt, channels)}, + {SOF_TKN_MEDIA_CODEC_BIT_DEPTH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, + offsetof(struct sof_media_codec_fmt, bit_depth)}, + {SOF_TKN_MEDIA_CODEC_MODULE_UUID, SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid, + offsetof(struct sof_media_codec_fmt, module_uuid)}, +}; + static const struct sof_token_info ipc4_token_list[SOF_TOKEN_COUNT] = { [SOF_DAI_TOKENS] = {"DAI tokens", dai_tokens, ARRAY_SIZE(dai_tokens)}, [SOF_PIPELINE_TOKENS] = {"Pipeline tokens", pipeline_tokens, ARRAY_SIZE(pipeline_tokens)}, @@ -216,6 +231,8 @@ static const struct sof_token_info ipc4_token_list[SOF_TOKEN_COUNT] = { [SOF_GAIN_TOKENS] = {"Gain tokens", gain_tokens, ARRAY_SIZE(gain_tokens)}, [SOF_SRC_TOKENS] = {"SRC tokens", src_tokens, ARRAY_SIZE(src_tokens)}, [SOF_ASRC_TOKENS] = {"ASRC tokens", asrc_tokens, ARRAY_SIZE(asrc_tokens)}, + [SOF_MEDIA_CODEC_TOKENS] = {"IPC4 Media codec tokens", media_codec_tokens, + ARRAY_SIZE(media_codec_tokens)}, }; struct snd_sof_widget *sof_ipc4_find_swidget_by_ids(struct snd_sof_dev *sdev, @@ -227,7 +244,7 @@ struct snd_sof_widget *sof_ipc4_find_swidget_by_ids(struct snd_sof_dev *sdev, struct sof_ipc4_fw_module *fw_module = swidget->module_info; /* Only active module instances have valid instance_id */ - if (!swidget->use_count) + if (!swidget->use_count || !fw_module) continue; if (fw_module && fw_module->man4_module_entry.id == module_id && @@ -562,27 +579,32 @@ static int sof_ipc4_widget_set_module_info(struct snd_sof_widget *swidget) struct snd_soc_component *scomp = swidget->scomp; struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); - swidget->module_info = sof_ipc4_find_module_by_uuid(sdev, &swidget->uuid); + if (swidget->id == snd_soc_dapm_decoder && swidget->num_media_formats) + swidget->module_info = sof_ipc4_find_loaded_module_by_uuid(sdev, &swidget->uuid); + else + swidget->module_info = sof_ipc4_find_module_by_uuid(sdev, &swidget->uuid); if (swidget->module_info) return 0; + /* + * Media decoder container widgets carry candidate codec UUIDs in + * media_formats[] and may resolve and load their module on stream + * configuration if the default UUID is not resident at topology load. + */ + if (swidget->id == snd_soc_dapm_decoder && swidget->num_media_formats) + return 0; + dev_err(sdev->dev, "failed to find module info for widget %s with UUID %pUL\n", swidget->widget->name, &swidget->uuid); return -EINVAL; } -static int sof_ipc4_widget_setup_msg(struct snd_sof_widget *swidget, struct sof_ipc4_msg *msg) +static void sof_ipc4_widget_init_msg_header(struct snd_sof_widget *swidget, + struct sof_ipc4_fw_module *fw_module, + struct sof_ipc4_msg *msg) { - struct sof_ipc4_fw_module *fw_module; - uint32_t type; - int ret; - - ret = sof_ipc4_widget_set_module_info(swidget); - if (ret) - return ret; - - fw_module = swidget->module_info; + u32 type; msg->primary = fw_module->man4_module_entry.id; msg->primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE); @@ -603,6 +625,22 @@ static int sof_ipc4_widget_setup_msg(struct snd_sof_widget *swidget, struct sof_ break; } msg->extension |= SOF_IPC4_MOD_EXT_DOMAIN(type); +} + +static int sof_ipc4_widget_setup_msg(struct snd_sof_widget *swidget, struct sof_ipc4_msg *msg) +{ + struct sof_ipc4_fw_module *fw_module; + int ret; + + ret = sof_ipc4_widget_set_module_info(swidget); + if (ret) + return ret; + + fw_module = swidget->module_info; + if (!fw_module) + return 0; + + sof_ipc4_widget_init_msg_header(swidget, fw_module, msg); return 0; } @@ -614,12 +652,16 @@ static void sof_ipc4_widget_update_kcontrol_module_id(struct snd_sof_widget *swi struct sof_ipc4_fw_module *fw_module = swidget->module_info; struct snd_sof_control *scontrol; + if (!fw_module) + return; + /* update module ID for all kcontrols for this widget */ list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { if (scontrol->comp_id == swidget->comp_id) { struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; struct sof_ipc4_msg *msg = &cdata->msg; + msg->primary &= ~SOF_IPC4_MOD_ID_MASK; msg->primary |= fw_module->man4_module_entry.id; } } @@ -1294,16 +1336,71 @@ static void sof_ipc4_widget_free_comp_mixer(struct snd_sof_widget *swidget) swidget->private = NULL; } +int sof_ipc4_process_bind_module(struct snd_sof_dev *sdev, + struct snd_sof_widget *swidget, + struct sof_ipc4_fw_module *fw_module) +{ + struct sof_ipc4_process *process = swidget->private; + struct sof_ipc4_base_module_cfg_ext *base_cfg_ext = NULL; + u32 base_cfg_ext_size = 0; + u32 ipc_config_size; + u32 init_config; + void *cfg; + + if (!process || !fw_module) + return -EINVAL; + + if (swidget->module_info == fw_module && process->ipc_config_data) + return 0; + + init_config = FIELD_GET(SOF_IPC4_MODULE_INIT_CONFIG_MASK, + fw_module->man4_module_entry.type); + ipc_config_size = sizeof(struct sof_ipc4_base_module_cfg); + + if (init_config == SOF_IPC4_MODULE_INIT_CONFIG_TYPE_BASE_CFG_WITH_EXT) { + base_cfg_ext_size = struct_size(base_cfg_ext, pin_formats, + size_add(swidget->num_input_pins, + swidget->num_output_pins)); + base_cfg_ext = kzalloc(base_cfg_ext_size, GFP_KERNEL); + if (!base_cfg_ext) + return -ENOMEM; + + base_cfg_ext->num_input_pin_fmts = swidget->num_input_pins; + base_cfg_ext->num_output_pin_fmts = swidget->num_output_pins; + ipc_config_size += base_cfg_ext_size; + } + + cfg = kzalloc(ipc_config_size, GFP_KERNEL); + if (!cfg) { + kfree(base_cfg_ext); + return -ENOMEM; + } + + kfree(process->ipc_config_data); + kfree(process->base_config_ext); + + swidget->module_info = fw_module; + process->init_config = init_config; + process->base_config_ext = base_cfg_ext; + process->base_config_ext_size = base_cfg_ext_size; + process->ipc_config_data = cfg; + process->ipc_config_size = ipc_config_size; + + sof_ipc4_widget_init_msg_header(swidget, fw_module, &process->msg); + sof_ipc4_widget_update_kcontrol_module_id(swidget); + + return 0; +} + /* * Add the process modules support. The process modules are defined as snd_soc_dapm_effect modules. */ static int sof_ipc4_widget_setup_comp_process(struct snd_sof_widget *swidget) { struct snd_soc_component *scomp = swidget->scomp; - struct sof_ipc4_fw_module *fw_module; + struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); struct snd_sof_pipeline *spipe = swidget->spipe; struct sof_ipc4_process *process; - void *cfg; int ret; process = kzalloc_obj(*process); @@ -1319,52 +1416,18 @@ static int sof_ipc4_widget_setup_comp_process(struct snd_sof_widget *swidget) ret = sof_ipc4_widget_setup_msg(swidget, &process->msg); if (ret) - goto err; - - /* parse process init module payload config type from module info */ - fw_module = swidget->module_info; - process->init_config = FIELD_GET(SOF_IPC4_MODULE_INIT_CONFIG_MASK, - fw_module->man4_module_entry.type); - - process->ipc_config_size = sizeof(struct sof_ipc4_base_module_cfg); - - /* allocate memory for base config extension if needed */ - if (process->init_config == SOF_IPC4_MODULE_INIT_CONFIG_TYPE_BASE_CFG_WITH_EXT) { - struct sof_ipc4_base_module_cfg_ext *base_cfg_ext; - u32 ext_size = struct_size(base_cfg_ext, pin_formats, - size_add(swidget->num_input_pins, - swidget->num_output_pins)); + goto free_available_fmt; - base_cfg_ext = kzalloc(ext_size, GFP_KERNEL); - if (!base_cfg_ext) { - ret = -ENOMEM; + if (swidget->module_info) { + ret = sof_ipc4_process_bind_module(sdev, swidget, swidget->module_info); + if (ret) goto free_available_fmt; - } - - base_cfg_ext->num_input_pin_fmts = swidget->num_input_pins; - base_cfg_ext->num_output_pin_fmts = swidget->num_output_pins; - process->base_config_ext = base_cfg_ext; - process->base_config_ext_size = ext_size; - process->ipc_config_size += ext_size; - } - - cfg = kzalloc(process->ipc_config_size, GFP_KERNEL); - if (!cfg) { - ret = -ENOMEM; - goto free_base_cfg_ext; } - process->ipc_config_data = cfg; - - sof_ipc4_widget_update_kcontrol_module_id(swidget); - /* set pipeline core mask to keep track of the core the module is scheduled to run on */ spipe->core_mask |= BIT(swidget->core); return 0; -free_base_cfg_ext: - kfree(process->base_config_ext); - process->base_config_ext = NULL; free_available_fmt: sof_ipc4_free_audio_fmt(&process->available_fmt); err: @@ -2987,6 +3050,12 @@ static int sof_ipc4_prepare_process_module(struct snd_sof_widget *swidget, int input_fmt_index = 0; int ret; + if (!swidget->module_info || !cfg) { + dev_err(sdev->dev, "module %s is not bound to a firmware module\n", + swidget->widget->name); + return -ENODEV; + } + if (available_fmt->num_input_formats) { if (swidget->prepared) { if (!available_fmt->num_output_formats) @@ -3767,7 +3836,7 @@ static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; struct sof_ipc4_pipeline *pipeline = pipe_widget->private; - if (!pipeline->use_chain_dma) + if (!pipeline->use_chain_dma && fw_module) ida_free(&fw_module->m_ida, swidget->instance_id); } @@ -4364,6 +4433,7 @@ static enum sof_tokens process_token_list[] = { SOF_AUDIO_FMT_NUM_TOKENS, SOF_IN_AUDIO_FORMAT_TOKENS, SOF_OUT_AUDIO_FORMAT_TOKENS, + SOF_MEDIA_CODEC_TOKENS, SOF_COMP_EXT_TOKENS, }; diff --git a/sound/soc/sof/ipc4-topology.h b/sound/soc/sof/ipc4-topology.h index b538b4183897a9..f347d9d65a61fd 100644 --- a/sound/soc/sof/ipc4-topology.h +++ b/sound/soc/sof/ipc4-topology.h @@ -119,6 +119,13 @@ enum sof_ipc4_copier_module_config_params { * is connected to Gateway */ SOF_IPC4_COPIER_MODULE_CFG_ATTENUATION, + /* + * SOF extension (compress drain): tell the host copier the total committed + * gateway byte count so it can flag end-of-stream once it has delivered + * exactly that many bytes. The free-running HDA gateway write pointer never + * settles, so it cannot serve as a drain boundary on its own. Payload: u64. + */ + SOF_IPC4_COPIER_MODULE_CFG_PARAM_DRAIN_BYTES = 100, }; /* Scheduling domain, unset, Low Latency, or Data Processing */ @@ -550,7 +557,11 @@ struct sof_ipc4_process { size_t init_ext_module_size; }; +struct sof_ipc4_fw_module; bool sof_ipc4_copier_is_single_bitdepth(struct snd_sof_dev *sdev, struct sof_ipc4_pin_format *pin_fmts, u32 pin_fmts_size); +int sof_ipc4_process_bind_module(struct snd_sof_dev *sdev, + struct snd_sof_widget *swidget, + struct sof_ipc4_fw_module *fw_module); #endif 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) diff --git a/sound/soc/sof/sof-audio.h b/sound/soc/sof/sof-audio.h index 10b41ab18b7990..2428be492e5fa3 100644 --- a/sound/soc/sof/sof-audio.h +++ b/sound/soc/sof/sof-audio.h @@ -294,11 +294,30 @@ enum sof_tokens { SOF_ACPI2S_TOKENS, SOF_MICFIL_TOKENS, SOF_ACP_SDW_TOKENS, + SOF_MEDIA_CODEC_TOKENS, /* this should be the last */ SOF_TOKEN_COUNT, }; +/** + * struct sof_media_codec_fmt - Media decoder format-to-module-UUID mapping + * @codec_id: Compressed codec ID (SND_AUDIOCODEC_*) + * @min_rate: Minimum supported sample rate in Hz + * @max_rate: Maximum supported sample rate in Hz + * @channels: Supported channel count (0 matches any) + * @bit_depth: Supported bit depth (0 matches any) + * @module_uuid: Candidate firmware module UUID supporting this format + */ +struct sof_media_codec_fmt { + u32 codec_id; + u32 min_rate; + u32 max_rate; + u32 channels; + u32 bit_depth; + guid_t module_uuid; +}; + /** * struct sof_topology_token - SOF topology token definition * @token: Token number @@ -480,6 +499,9 @@ struct snd_sof_widget { int num_tuples; struct snd_sof_tuple *tuples; + int num_media_formats; + struct sof_media_codec_fmt *media_formats; + /* * The allowed range for num_input/output_pins is [0, SOF_WIDGET_MAX_NUM_PINS]. * Widgets may have zero input or output pins, for example the tone widget has diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c index 750916b57def74..c5dccae294724d 100644 --- a/sound/soc/sof/topology.c +++ b/sound/soc/sof/topology.c @@ -1225,6 +1225,102 @@ static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int n return -EINVAL; } +static int sof_parse_media_codec_tokens(struct snd_soc_component *scomp, + struct snd_sof_widget *swidget, + const struct sof_topology_token *tokens, + int num_tokens, + struct snd_soc_tplg_vendor_array *array, + int array_size) +{ + struct sof_media_codec_fmt *fmts; + struct snd_soc_tplg_vendor_array *arr; + int num_fmts = 0; + int rem = array_size; + int t; + + /* Count rows using the per-entry codec_id token */ + for (arr = array; rem > 0;) { + int asize; + + if (rem < (int)sizeof(*arr)) + return -EINVAL; + + asize = le32_to_cpu(arr->size); + if (asize < (int)sizeof(*arr) || asize > rem) + return -EINVAL; + + if (le32_to_cpu(arr->type) == SND_SOC_TPLG_TUPLE_TYPE_WORD) { + u32 num_elems = le32_to_cpu(arr->num_elems); + u32 i; + + if (struct_size(arr, value, num_elems) > asize) + return -EINVAL; + + for (i = 0; i < num_elems; i++) { + if (le32_to_cpu(arr->value[i].token) == + SOF_TKN_MEDIA_CODEC_ID) + num_fmts++; + } + } + + rem -= asize; + arr = (struct snd_soc_tplg_vendor_array *)((u8 *)arr + asize); + } + + if (!num_fmts) + return 0; + + fmts = kcalloc(num_fmts, sizeof(*fmts), GFP_KERNEL); + if (!fmts) + return -ENOMEM; + + for (t = 0; t < num_tokens; t++) { + const struct sof_topology_token *tok = &tokens[t]; + int idx = 0; + + rem = array_size; + for (arr = array; rem > 0 && idx < num_fmts;) { + int asize = le32_to_cpu(arr->size); + u32 num_elems = le32_to_cpu(arr->num_elems); + u32 i; + + if (tok->type == SND_SOC_TPLG_TUPLE_TYPE_UUID && + le32_to_cpu(arr->type) == SND_SOC_TPLG_TUPLE_TYPE_UUID) { + if (struct_size(arr, uuid, num_elems) > asize) { + kfree(fmts); + return -EINVAL; + } + for (i = 0; i < num_elems && idx < num_fmts; i++) { + struct snd_soc_tplg_vendor_uuid_elem *ue = &arr->uuid[i]; + + if (le32_to_cpu(ue->token) != tok->token) + continue; + tok->get_token(ue, &fmts[idx], tok->offset); + idx++; + } + } else if (tok->type == SND_SOC_TPLG_TUPLE_TYPE_WORD && + le32_to_cpu(arr->type) == SND_SOC_TPLG_TUPLE_TYPE_WORD) { + for (i = 0; i < num_elems && idx < num_fmts; i++) { + struct snd_soc_tplg_vendor_value_elem *ve = &arr->value[i]; + + if (le32_to_cpu(ve->token) != tok->token) + continue; + tok->get_token(ve, &fmts[idx], tok->offset); + idx++; + } + } + + rem -= asize; + arr = (struct snd_soc_tplg_vendor_array *)((u8 *)arr + asize); + } + } + + swidget->media_formats = fmts; + swidget->num_media_formats = num_fmts; + + return 0; +} + static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget, struct snd_soc_tplg_dapm_widget *tw, enum sof_tokens *object_token_list, int count) @@ -1280,6 +1376,20 @@ static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_s goto err; } + continue; + case SOF_MEDIA_CODEC_TOKENS: + ret = sof_parse_media_codec_tokens(scomp, swidget, + token_list[object_token_list[i]].tokens, + token_list[object_token_list[i]].count, + private->array, + le32_to_cpu(private->size)); + if (ret < 0) { + dev_err(scomp->dev, "Failed parsing %s for widget %s\n", + token_list[object_token_list[i]].name, + swidget->widget->name); + goto err; + } + continue; case SOF_IN_AUDIO_FORMAT_TOKENS: num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS, @@ -1332,6 +1442,9 @@ static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_s return 0; err: + kfree(swidget->media_formats); + swidget->media_formats = NULL; + swidget->num_media_formats = 0; kfree(swidget->tuples); return ret; } @@ -1629,6 +1742,7 @@ static int sof_widget_ready(struct snd_soc_component *scomp, int index, return ret; free: kfree(swidget->private); + kfree(swidget->media_formats); kfree(swidget->tuples); widget_free: kfree(swidget); @@ -1736,6 +1850,7 @@ static int sof_widget_unload(struct snd_soc_component *scomp, sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT); sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT); + kfree(swidget->media_formats); kfree(swidget->tuples); /* remove and free swidget object */