1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2022 Intel Corporation. All rights reserved. 7 // 8 // 9 #include <linux/bitfield.h> 10 #include <uapi/sound/sof/tokens.h> 11 #include <sound/pcm_params.h> 12 #include <sound/sof/ext_manifest4.h> 13 #include <sound/intel-nhlt.h> 14 #include "sof-priv.h" 15 #include "sof-audio.h" 16 #include "ipc4-priv.h" 17 #include "ipc4-topology.h" 18 #include "ops.h" 19 20 /* 21 * The ignore_cpc flag can be used to ignore the CPC value for all modules by 22 * using 0 instead. 23 * The CPC is sent to the firmware along with the SOF_IPC4_MOD_INIT_INSTANCE 24 * message and it is used for clock scaling. 25 * 0 as CPC value will instruct the firmware to use maximum frequency, thus 26 * deactivating the clock scaling. 27 */ 28 static bool ignore_cpc; 29 module_param_named(ipc4_ignore_cpc, ignore_cpc, bool, 0444); 30 MODULE_PARM_DESC(ipc4_ignore_cpc, 31 "Ignore CPC values. This option will disable clock scaling in firmware."); 32 33 #define SOF_IPC4_GAIN_PARAM_ID 0 34 #define SOF_IPC4_TPLG_ABI_SIZE 6 35 #define SOF_IPC4_CHAIN_DMA_BUF_SIZE_MS 2 36 37 static DEFINE_IDA(alh_group_ida); 38 static DEFINE_IDA(pipeline_ida); 39 40 static const struct sof_topology_token ipc4_sched_tokens[] = { 41 {SOF_TKN_SCHED_LP_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 42 offsetof(struct sof_ipc4_pipeline, lp_mode)}, 43 {SOF_TKN_SCHED_USE_CHAIN_DMA, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 44 offsetof(struct sof_ipc4_pipeline, use_chain_dma)}, 45 {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 46 offsetof(struct sof_ipc4_pipeline, core_id)}, 47 }; 48 49 static const struct sof_topology_token pipeline_tokens[] = { 50 {SOF_TKN_SCHED_DYNAMIC_PIPELINE, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 51 offsetof(struct snd_sof_widget, dynamic_pipeline_widget)}, 52 }; 53 54 static const struct sof_topology_token ipc4_comp_tokens[] = { 55 {SOF_TKN_COMP_IS_PAGES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 56 offsetof(struct sof_ipc4_base_module_cfg, is_pages)}, 57 }; 58 59 static const struct sof_topology_token ipc4_in_audio_format_tokens[] = { 60 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 61 offsetof(struct sof_ipc4_pin_format, audio_fmt.sampling_frequency)}, 62 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_BIT_DEPTH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 63 offsetof(struct sof_ipc4_pin_format, audio_fmt.bit_depth)}, 64 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_CH_MAP, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 65 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_map)}, 66 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_CH_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 67 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_cfg)}, 68 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_INTERLEAVING_STYLE, SND_SOC_TPLG_TUPLE_TYPE_WORD, 69 get_token_u32, offsetof(struct sof_ipc4_pin_format, 70 audio_fmt.interleaving_style)}, 71 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_FMT_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 72 offsetof(struct sof_ipc4_pin_format, audio_fmt.fmt_cfg)}, 73 {SOF_TKN_CAVS_AUDIO_FORMAT_INPUT_PIN_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 74 offsetof(struct sof_ipc4_pin_format, pin_index)}, 75 {SOF_TKN_CAVS_AUDIO_FORMAT_IBS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 76 offsetof(struct sof_ipc4_pin_format, buffer_size)}, 77 }; 78 79 static const struct sof_topology_token ipc4_out_audio_format_tokens[] = { 80 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 81 offsetof(struct sof_ipc4_pin_format, audio_fmt.sampling_frequency)}, 82 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_BIT_DEPTH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 83 offsetof(struct sof_ipc4_pin_format, audio_fmt.bit_depth)}, 84 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_CH_MAP, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 85 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_map)}, 86 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_CH_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 87 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_cfg)}, 88 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_INTERLEAVING_STYLE, SND_SOC_TPLG_TUPLE_TYPE_WORD, 89 get_token_u32, offsetof(struct sof_ipc4_pin_format, 90 audio_fmt.interleaving_style)}, 91 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_FMT_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 92 offsetof(struct sof_ipc4_pin_format, audio_fmt.fmt_cfg)}, 93 {SOF_TKN_CAVS_AUDIO_FORMAT_OUTPUT_PIN_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 94 offsetof(struct sof_ipc4_pin_format, pin_index)}, 95 {SOF_TKN_CAVS_AUDIO_FORMAT_OBS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 96 offsetof(struct sof_ipc4_pin_format, buffer_size)}, 97 }; 98 99 static const struct sof_topology_token ipc4_copier_deep_buffer_tokens[] = { 100 {SOF_TKN_INTEL_COPIER_DEEP_BUFFER_DMA_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 0}, 101 }; 102 103 static const struct sof_topology_token ipc4_copier_tokens[] = { 104 {SOF_TKN_INTEL_COPIER_NODE_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 0}, 105 }; 106 107 static const struct sof_topology_token ipc4_audio_fmt_num_tokens[] = { 108 {SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 109 offsetof(struct sof_ipc4_available_audio_format, num_input_formats)}, 110 {SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 111 offsetof(struct sof_ipc4_available_audio_format, num_output_formats)}, 112 }; 113 114 static const struct sof_topology_token dai_tokens[] = { 115 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 116 offsetof(struct sof_ipc4_copier, dai_type)}, 117 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 118 offsetof(struct sof_ipc4_copier, dai_index)}, 119 }; 120 121 /* Component extended tokens */ 122 static const struct sof_topology_token comp_ext_tokens[] = { 123 {SOF_TKN_COMP_UUID, SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid, 124 offsetof(struct snd_sof_widget, uuid)}, 125 {SOF_TKN_COMP_CORE_ID, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 126 offsetof(struct snd_sof_widget, core)}, 127 }; 128 129 static const struct sof_topology_token gain_tokens[] = { 130 {SOF_TKN_GAIN_RAMP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, 131 get_token_u32, offsetof(struct sof_ipc4_gain_params, curve_type)}, 132 {SOF_TKN_GAIN_RAMP_DURATION, 133 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 134 offsetof(struct sof_ipc4_gain_params, curve_duration_l)}, 135 {SOF_TKN_GAIN_VAL, SND_SOC_TPLG_TUPLE_TYPE_WORD, 136 get_token_u32, offsetof(struct sof_ipc4_gain_params, init_val)}, 137 }; 138 139 /* SRC */ 140 static const struct sof_topology_token src_tokens[] = { 141 {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 142 offsetof(struct sof_ipc4_src_data, sink_rate)}, 143 }; 144 145 static const struct sof_token_info ipc4_token_list[SOF_TOKEN_COUNT] = { 146 [SOF_DAI_TOKENS] = {"DAI tokens", dai_tokens, ARRAY_SIZE(dai_tokens)}, 147 [SOF_PIPELINE_TOKENS] = {"Pipeline tokens", pipeline_tokens, ARRAY_SIZE(pipeline_tokens)}, 148 [SOF_SCHED_TOKENS] = {"Scheduler tokens", ipc4_sched_tokens, 149 ARRAY_SIZE(ipc4_sched_tokens)}, 150 [SOF_COMP_EXT_TOKENS] = {"Comp extended tokens", comp_ext_tokens, 151 ARRAY_SIZE(comp_ext_tokens)}, 152 [SOF_COMP_TOKENS] = {"IPC4 Component tokens", 153 ipc4_comp_tokens, ARRAY_SIZE(ipc4_comp_tokens)}, 154 [SOF_IN_AUDIO_FORMAT_TOKENS] = {"IPC4 Input Audio format tokens", 155 ipc4_in_audio_format_tokens, ARRAY_SIZE(ipc4_in_audio_format_tokens)}, 156 [SOF_OUT_AUDIO_FORMAT_TOKENS] = {"IPC4 Output Audio format tokens", 157 ipc4_out_audio_format_tokens, ARRAY_SIZE(ipc4_out_audio_format_tokens)}, 158 [SOF_COPIER_DEEP_BUFFER_TOKENS] = {"IPC4 Copier deep buffer tokens", 159 ipc4_copier_deep_buffer_tokens, ARRAY_SIZE(ipc4_copier_deep_buffer_tokens)}, 160 [SOF_COPIER_TOKENS] = {"IPC4 Copier tokens", ipc4_copier_tokens, 161 ARRAY_SIZE(ipc4_copier_tokens)}, 162 [SOF_AUDIO_FMT_NUM_TOKENS] = {"IPC4 Audio format number tokens", 163 ipc4_audio_fmt_num_tokens, ARRAY_SIZE(ipc4_audio_fmt_num_tokens)}, 164 [SOF_GAIN_TOKENS] = {"Gain tokens", gain_tokens, ARRAY_SIZE(gain_tokens)}, 165 [SOF_SRC_TOKENS] = {"SRC tokens", src_tokens, ARRAY_SIZE(src_tokens)}, 166 }; 167 168 static void sof_ipc4_dbg_audio_format(struct device *dev, struct sof_ipc4_pin_format *pin_fmt, 169 int num_formats) 170 { 171 int i; 172 173 for (i = 0; i < num_formats; i++) { 174 struct sof_ipc4_audio_format *fmt = &pin_fmt[i].audio_fmt; 175 dev_dbg(dev, 176 "Pin index #%d: %uHz, %ubit (ch_map %#x ch_cfg %u interleaving_style %u fmt_cfg %#x) buffer size %d\n", 177 pin_fmt[i].pin_index, fmt->sampling_frequency, fmt->bit_depth, fmt->ch_map, 178 fmt->ch_cfg, fmt->interleaving_style, fmt->fmt_cfg, 179 pin_fmt[i].buffer_size); 180 } 181 } 182 183 static const struct sof_ipc4_audio_format * 184 sof_ipc4_get_input_pin_audio_fmt(struct snd_sof_widget *swidget, int pin_index) 185 { 186 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext; 187 struct sof_ipc4_process *process; 188 int i; 189 190 if (swidget->id != snd_soc_dapm_effect) { 191 struct sof_ipc4_base_module_cfg *base = swidget->private; 192 193 /* For non-process modules, base module config format is used for all input pins */ 194 return &base->audio_fmt; 195 } 196 197 process = swidget->private; 198 199 /* 200 * For process modules without base config extension, base module config 201 * format is used for all input pins 202 */ 203 if (process->init_config != SOF_IPC4_MODULE_INIT_CONFIG_TYPE_BASE_CFG_WITH_EXT) 204 return &process->base_config.audio_fmt; 205 206 base_cfg_ext = process->base_config_ext; 207 208 /* 209 * If there are multiple input formats available for a pin, the first available format 210 * is chosen. 211 */ 212 for (i = 0; i < base_cfg_ext->num_input_pin_fmts; i++) { 213 struct sof_ipc4_pin_format *pin_format = &base_cfg_ext->pin_formats[i]; 214 215 if (pin_format->pin_index == pin_index) 216 return &pin_format->audio_fmt; 217 } 218 219 return NULL; 220 } 221 222 /** 223 * sof_ipc4_get_audio_fmt - get available audio formats from swidget->tuples 224 * @scomp: pointer to pointer to SOC component 225 * @swidget: pointer to struct snd_sof_widget containing tuples 226 * @available_fmt: pointer to struct sof_ipc4_available_audio_format being filling in 227 * @module_base_cfg: Pointer to the base_config in the module init IPC payload 228 * 229 * Return: 0 if successful 230 */ 231 static int sof_ipc4_get_audio_fmt(struct snd_soc_component *scomp, 232 struct snd_sof_widget *swidget, 233 struct sof_ipc4_available_audio_format *available_fmt, 234 struct sof_ipc4_base_module_cfg *module_base_cfg) 235 { 236 struct sof_ipc4_pin_format *in_format = NULL; 237 struct sof_ipc4_pin_format *out_format; 238 int ret; 239 240 ret = sof_update_ipc_object(scomp, available_fmt, 241 SOF_AUDIO_FMT_NUM_TOKENS, swidget->tuples, 242 swidget->num_tuples, sizeof(*available_fmt), 1); 243 if (ret) { 244 dev_err(scomp->dev, "Failed to parse audio format token count\n"); 245 return ret; 246 } 247 248 if (!available_fmt->num_input_formats && !available_fmt->num_output_formats) { 249 dev_err(scomp->dev, "No input/output pin formats set in topology\n"); 250 return -EINVAL; 251 } 252 253 dev_dbg(scomp->dev, 254 "Number of input audio formats: %d. Number of output audio formats: %d\n", 255 available_fmt->num_input_formats, available_fmt->num_output_formats); 256 257 /* set is_pages in the module's base_config */ 258 ret = sof_update_ipc_object(scomp, module_base_cfg, SOF_COMP_TOKENS, swidget->tuples, 259 swidget->num_tuples, sizeof(*module_base_cfg), 1); 260 if (ret) { 261 dev_err(scomp->dev, "parse comp tokens for %s failed, error: %d\n", 262 swidget->widget->name, ret); 263 return ret; 264 } 265 266 dev_dbg(scomp->dev, "widget %s: is_pages: %d\n", swidget->widget->name, 267 module_base_cfg->is_pages); 268 269 if (available_fmt->num_input_formats) { 270 in_format = kcalloc(available_fmt->num_input_formats, 271 sizeof(*in_format), GFP_KERNEL); 272 if (!in_format) 273 return -ENOMEM; 274 available_fmt->input_pin_fmts = in_format; 275 276 ret = sof_update_ipc_object(scomp, in_format, 277 SOF_IN_AUDIO_FORMAT_TOKENS, swidget->tuples, 278 swidget->num_tuples, sizeof(*in_format), 279 available_fmt->num_input_formats); 280 if (ret) { 281 dev_err(scomp->dev, "parse input audio fmt tokens failed %d\n", ret); 282 goto err_in; 283 } 284 285 dev_dbg(scomp->dev, "Input audio formats for %s\n", swidget->widget->name); 286 sof_ipc4_dbg_audio_format(scomp->dev, in_format, 287 available_fmt->num_input_formats); 288 } 289 290 if (available_fmt->num_output_formats) { 291 out_format = kcalloc(available_fmt->num_output_formats, sizeof(*out_format), 292 GFP_KERNEL); 293 if (!out_format) { 294 ret = -ENOMEM; 295 goto err_in; 296 } 297 298 ret = sof_update_ipc_object(scomp, out_format, 299 SOF_OUT_AUDIO_FORMAT_TOKENS, swidget->tuples, 300 swidget->num_tuples, sizeof(*out_format), 301 available_fmt->num_output_formats); 302 if (ret) { 303 dev_err(scomp->dev, "parse output audio fmt tokens failed\n"); 304 goto err_out; 305 } 306 307 available_fmt->output_pin_fmts = out_format; 308 dev_dbg(scomp->dev, "Output audio formats for %s\n", swidget->widget->name); 309 sof_ipc4_dbg_audio_format(scomp->dev, out_format, 310 available_fmt->num_output_formats); 311 } 312 313 return 0; 314 315 err_out: 316 kfree(out_format); 317 err_in: 318 kfree(in_format); 319 available_fmt->input_pin_fmts = NULL; 320 return ret; 321 } 322 323 /* release the memory allocated in sof_ipc4_get_audio_fmt */ 324 static void sof_ipc4_free_audio_fmt(struct sof_ipc4_available_audio_format *available_fmt) 325 326 { 327 kfree(available_fmt->output_pin_fmts); 328 available_fmt->output_pin_fmts = NULL; 329 kfree(available_fmt->input_pin_fmts); 330 available_fmt->input_pin_fmts = NULL; 331 } 332 333 static void sof_ipc4_widget_free_comp_pipeline(struct snd_sof_widget *swidget) 334 { 335 kfree(swidget->private); 336 } 337 338 static int sof_ipc4_widget_set_module_info(struct snd_sof_widget *swidget) 339 { 340 struct snd_soc_component *scomp = swidget->scomp; 341 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 342 343 swidget->module_info = sof_ipc4_find_module_by_uuid(sdev, &swidget->uuid); 344 345 if (swidget->module_info) 346 return 0; 347 348 dev_err(sdev->dev, "failed to find module info for widget %s with UUID %pUL\n", 349 swidget->widget->name, &swidget->uuid); 350 return -EINVAL; 351 } 352 353 static int sof_ipc4_widget_setup_msg(struct snd_sof_widget *swidget, struct sof_ipc4_msg *msg) 354 { 355 struct sof_ipc4_fw_module *fw_module; 356 uint32_t type; 357 int ret; 358 359 ret = sof_ipc4_widget_set_module_info(swidget); 360 if (ret) 361 return ret; 362 363 fw_module = swidget->module_info; 364 365 msg->primary = fw_module->man4_module_entry.id; 366 msg->primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE); 367 msg->primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 368 msg->primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 369 370 msg->extension = SOF_IPC4_MOD_EXT_CORE_ID(swidget->core); 371 372 type = (fw_module->man4_module_entry.type & SOF_IPC4_MODULE_DP) ? 1 : 0; 373 msg->extension |= SOF_IPC4_MOD_EXT_DOMAIN(type); 374 375 return 0; 376 } 377 378 static void sof_ipc4_widget_update_kcontrol_module_id(struct snd_sof_widget *swidget) 379 { 380 struct snd_soc_component *scomp = swidget->scomp; 381 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 382 struct sof_ipc4_fw_module *fw_module = swidget->module_info; 383 struct snd_sof_control *scontrol; 384 385 /* update module ID for all kcontrols for this widget */ 386 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 387 if (scontrol->comp_id == swidget->comp_id) { 388 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 389 struct sof_ipc4_msg *msg = &cdata->msg; 390 391 msg->primary |= fw_module->man4_module_entry.id; 392 } 393 } 394 } 395 396 static int sof_ipc4_widget_setup_pcm(struct snd_sof_widget *swidget) 397 { 398 struct sof_ipc4_available_audio_format *available_fmt; 399 struct snd_soc_component *scomp = swidget->scomp; 400 struct sof_ipc4_copier *ipc4_copier; 401 int node_type = 0; 402 int ret; 403 404 ipc4_copier = kzalloc(sizeof(*ipc4_copier), GFP_KERNEL); 405 if (!ipc4_copier) 406 return -ENOMEM; 407 408 swidget->private = ipc4_copier; 409 available_fmt = &ipc4_copier->available_fmt; 410 411 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name); 412 413 ret = sof_ipc4_get_audio_fmt(scomp, swidget, available_fmt, 414 &ipc4_copier->data.base_config); 415 if (ret) 416 goto free_copier; 417 418 /* 419 * This callback is used by host copier and module-to-module copier, 420 * and only host copier needs to set gtw_cfg. 421 */ 422 if (!WIDGET_IS_AIF(swidget->id)) 423 goto skip_gtw_cfg; 424 425 ret = sof_update_ipc_object(scomp, &node_type, 426 SOF_COPIER_TOKENS, swidget->tuples, 427 swidget->num_tuples, sizeof(node_type), 1); 428 429 if (ret) { 430 dev_err(scomp->dev, "parse host copier node type token failed %d\n", 431 ret); 432 goto free_available_fmt; 433 } 434 dev_dbg(scomp->dev, "host copier '%s' node_type %u\n", swidget->widget->name, node_type); 435 436 skip_gtw_cfg: 437 ipc4_copier->gtw_attr = kzalloc(sizeof(*ipc4_copier->gtw_attr), GFP_KERNEL); 438 if (!ipc4_copier->gtw_attr) { 439 ret = -ENOMEM; 440 goto free_available_fmt; 441 } 442 443 ipc4_copier->copier_config = (uint32_t *)ipc4_copier->gtw_attr; 444 ipc4_copier->data.gtw_cfg.config_length = 445 sizeof(struct sof_ipc4_gtw_attributes) >> 2; 446 447 switch (swidget->id) { 448 case snd_soc_dapm_aif_in: 449 case snd_soc_dapm_aif_out: 450 ipc4_copier->data.gtw_cfg.node_id = SOF_IPC4_NODE_TYPE(node_type); 451 break; 452 case snd_soc_dapm_buffer: 453 ipc4_copier->data.gtw_cfg.node_id = SOF_IPC4_INVALID_NODE_ID; 454 ipc4_copier->ipc_config_size = 0; 455 break; 456 default: 457 dev_err(scomp->dev, "invalid widget type %d\n", swidget->id); 458 ret = -EINVAL; 459 goto free_gtw_attr; 460 } 461 462 /* set up module info and message header */ 463 ret = sof_ipc4_widget_setup_msg(swidget, &ipc4_copier->msg); 464 if (ret) 465 goto free_gtw_attr; 466 467 return 0; 468 469 free_gtw_attr: 470 kfree(ipc4_copier->gtw_attr); 471 free_available_fmt: 472 sof_ipc4_free_audio_fmt(available_fmt); 473 free_copier: 474 kfree(ipc4_copier); 475 swidget->private = NULL; 476 return ret; 477 } 478 479 static void sof_ipc4_widget_free_comp_pcm(struct snd_sof_widget *swidget) 480 { 481 struct sof_ipc4_copier *ipc4_copier = swidget->private; 482 struct sof_ipc4_available_audio_format *available_fmt; 483 484 if (!ipc4_copier) 485 return; 486 487 available_fmt = &ipc4_copier->available_fmt; 488 kfree(available_fmt->output_pin_fmts); 489 kfree(ipc4_copier->gtw_attr); 490 kfree(ipc4_copier); 491 swidget->private = NULL; 492 } 493 494 static int sof_ipc4_widget_setup_comp_dai(struct snd_sof_widget *swidget) 495 { 496 struct sof_ipc4_available_audio_format *available_fmt; 497 struct snd_soc_component *scomp = swidget->scomp; 498 struct snd_sof_dai *dai = swidget->private; 499 struct sof_ipc4_copier *ipc4_copier; 500 struct snd_sof_widget *pipe_widget; 501 struct sof_ipc4_pipeline *pipeline; 502 int node_type = 0; 503 int ret; 504 505 ipc4_copier = kzalloc(sizeof(*ipc4_copier), GFP_KERNEL); 506 if (!ipc4_copier) 507 return -ENOMEM; 508 509 available_fmt = &ipc4_copier->available_fmt; 510 511 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name); 512 513 ret = sof_ipc4_get_audio_fmt(scomp, swidget, available_fmt, 514 &ipc4_copier->data.base_config); 515 if (ret) 516 goto free_copier; 517 518 ret = sof_update_ipc_object(scomp, &node_type, 519 SOF_COPIER_TOKENS, swidget->tuples, 520 swidget->num_tuples, sizeof(node_type), 1); 521 if (ret) { 522 dev_err(scomp->dev, "parse dai node type failed %d\n", ret); 523 goto free_available_fmt; 524 } 525 526 ret = sof_update_ipc_object(scomp, ipc4_copier, 527 SOF_DAI_TOKENS, swidget->tuples, 528 swidget->num_tuples, sizeof(u32), 1); 529 if (ret) { 530 dev_err(scomp->dev, "parse dai copier node token failed %d\n", ret); 531 goto free_available_fmt; 532 } 533 534 dev_dbg(scomp->dev, "dai %s node_type %u dai_type %u dai_index %d\n", swidget->widget->name, 535 node_type, ipc4_copier->dai_type, ipc4_copier->dai_index); 536 537 ipc4_copier->data.gtw_cfg.node_id = SOF_IPC4_NODE_TYPE(node_type); 538 539 pipe_widget = swidget->spipe->pipe_widget; 540 pipeline = pipe_widget->private; 541 if (pipeline->use_chain_dma && ipc4_copier->dai_type != SOF_DAI_INTEL_HDA) { 542 dev_err(scomp->dev, 543 "Bad DAI type '%d', Chained DMA is only supported by HDA DAIs (%d).\n", 544 ipc4_copier->dai_type, SOF_DAI_INTEL_HDA); 545 ret = -ENODEV; 546 goto free_available_fmt; 547 } 548 549 switch (ipc4_copier->dai_type) { 550 case SOF_DAI_INTEL_ALH: 551 { 552 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 553 struct sof_ipc4_alh_configuration_blob *blob; 554 struct snd_soc_dapm_path *p; 555 struct snd_sof_widget *w; 556 int src_num = 0; 557 558 snd_soc_dapm_widget_for_each_source_path(swidget->widget, p) 559 src_num++; 560 561 if (swidget->id == snd_soc_dapm_dai_in && src_num == 0) { 562 /* 563 * The blob will not be used if the ALH copier is playback direction 564 * and doesn't connect to any source. 565 * It is fine to call kfree(ipc4_copier->copier_config) since 566 * ipc4_copier->copier_config is null. 567 */ 568 ret = 0; 569 break; 570 } 571 572 blob = kzalloc(sizeof(*blob), GFP_KERNEL); 573 if (!blob) { 574 ret = -ENOMEM; 575 goto free_available_fmt; 576 } 577 578 list_for_each_entry(w, &sdev->widget_list, list) { 579 if (w->widget->sname && 580 strcmp(w->widget->sname, swidget->widget->sname)) 581 continue; 582 583 blob->alh_cfg.device_count++; 584 } 585 586 ipc4_copier->copier_config = (uint32_t *)blob; 587 ipc4_copier->data.gtw_cfg.config_length = sizeof(*blob) >> 2; 588 break; 589 } 590 case SOF_DAI_INTEL_SSP: 591 /* set SSP DAI index as the node_id */ 592 ipc4_copier->data.gtw_cfg.node_id |= 593 SOF_IPC4_NODE_INDEX_INTEL_SSP(ipc4_copier->dai_index); 594 break; 595 case SOF_DAI_INTEL_DMIC: 596 /* set DMIC DAI index as the node_id */ 597 ipc4_copier->data.gtw_cfg.node_id |= 598 SOF_IPC4_NODE_INDEX_INTEL_DMIC(ipc4_copier->dai_index); 599 break; 600 default: 601 ipc4_copier->gtw_attr = kzalloc(sizeof(*ipc4_copier->gtw_attr), GFP_KERNEL); 602 if (!ipc4_copier->gtw_attr) { 603 ret = -ENOMEM; 604 goto free_available_fmt; 605 } 606 607 ipc4_copier->copier_config = (uint32_t *)ipc4_copier->gtw_attr; 608 ipc4_copier->data.gtw_cfg.config_length = 609 sizeof(struct sof_ipc4_gtw_attributes) >> 2; 610 break; 611 } 612 613 dai->scomp = scomp; 614 dai->private = ipc4_copier; 615 616 /* set up module info and message header */ 617 ret = sof_ipc4_widget_setup_msg(swidget, &ipc4_copier->msg); 618 if (ret) 619 goto free_copier_config; 620 621 return 0; 622 623 free_copier_config: 624 kfree(ipc4_copier->copier_config); 625 free_available_fmt: 626 sof_ipc4_free_audio_fmt(available_fmt); 627 free_copier: 628 kfree(ipc4_copier); 629 dai->private = NULL; 630 dai->scomp = NULL; 631 return ret; 632 } 633 634 static void sof_ipc4_widget_free_comp_dai(struct snd_sof_widget *swidget) 635 { 636 struct sof_ipc4_available_audio_format *available_fmt; 637 struct snd_sof_dai *dai = swidget->private; 638 struct sof_ipc4_copier *ipc4_copier; 639 640 if (!dai) 641 return; 642 643 if (!dai->private) { 644 kfree(dai); 645 swidget->private = NULL; 646 return; 647 } 648 649 ipc4_copier = dai->private; 650 available_fmt = &ipc4_copier->available_fmt; 651 652 kfree(available_fmt->output_pin_fmts); 653 if (ipc4_copier->dai_type != SOF_DAI_INTEL_SSP && 654 ipc4_copier->dai_type != SOF_DAI_INTEL_DMIC) 655 kfree(ipc4_copier->copier_config); 656 kfree(dai->private); 657 kfree(dai); 658 swidget->private = NULL; 659 } 660 661 static int sof_ipc4_widget_setup_comp_pipeline(struct snd_sof_widget *swidget) 662 { 663 struct snd_soc_component *scomp = swidget->scomp; 664 struct sof_ipc4_pipeline *pipeline; 665 struct snd_sof_pipeline *spipe = swidget->spipe; 666 int ret; 667 668 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL); 669 if (!pipeline) 670 return -ENOMEM; 671 672 ret = sof_update_ipc_object(scomp, pipeline, SOF_SCHED_TOKENS, swidget->tuples, 673 swidget->num_tuples, sizeof(*pipeline), 1); 674 if (ret) { 675 dev_err(scomp->dev, "parsing scheduler tokens failed\n"); 676 goto err; 677 } 678 679 swidget->core = pipeline->core_id; 680 spipe->core_mask |= BIT(pipeline->core_id); 681 682 if (pipeline->use_chain_dma) { 683 dev_dbg(scomp->dev, "Set up chain DMA for %s\n", swidget->widget->name); 684 swidget->private = pipeline; 685 return 0; 686 } 687 688 /* parse one set of pipeline tokens */ 689 ret = sof_update_ipc_object(scomp, swidget, SOF_PIPELINE_TOKENS, swidget->tuples, 690 swidget->num_tuples, sizeof(*swidget), 1); 691 if (ret) { 692 dev_err(scomp->dev, "parsing pipeline tokens failed\n"); 693 goto err; 694 } 695 696 /* TODO: Get priority from topology */ 697 pipeline->priority = 0; 698 699 dev_dbg(scomp->dev, "pipeline '%s': id %d, pri %d, core_id %u, lp mode %d\n", 700 swidget->widget->name, swidget->pipeline_id, 701 pipeline->priority, pipeline->core_id, pipeline->lp_mode); 702 703 swidget->private = pipeline; 704 705 pipeline->msg.primary = SOF_IPC4_GLB_PIPE_PRIORITY(pipeline->priority); 706 pipeline->msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_CREATE_PIPELINE); 707 pipeline->msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 708 pipeline->msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG); 709 710 pipeline->msg.extension = pipeline->lp_mode; 711 pipeline->msg.extension |= SOF_IPC4_GLB_PIPE_EXT_CORE_ID(pipeline->core_id); 712 pipeline->state = SOF_IPC4_PIPE_UNINITIALIZED; 713 714 return 0; 715 err: 716 kfree(pipeline); 717 return ret; 718 } 719 720 static int sof_ipc4_widget_setup_comp_pga(struct snd_sof_widget *swidget) 721 { 722 struct snd_soc_component *scomp = swidget->scomp; 723 struct sof_ipc4_gain *gain; 724 int ret; 725 726 gain = kzalloc(sizeof(*gain), GFP_KERNEL); 727 if (!gain) 728 return -ENOMEM; 729 730 swidget->private = gain; 731 732 gain->data.params.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK; 733 gain->data.params.init_val = SOF_IPC4_VOL_ZERO_DB; 734 735 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &gain->available_fmt, &gain->data.base_config); 736 if (ret) 737 goto err; 738 739 ret = sof_update_ipc_object(scomp, &gain->data.params, SOF_GAIN_TOKENS, 740 swidget->tuples, swidget->num_tuples, sizeof(gain->data), 1); 741 if (ret) { 742 dev_err(scomp->dev, "Parsing gain tokens failed\n"); 743 goto err; 744 } 745 746 dev_dbg(scomp->dev, 747 "pga widget %s: ramp type: %d, ramp duration %d, initial gain value: %#x\n", 748 swidget->widget->name, gain->data.params.curve_type, 749 gain->data.params.curve_duration_l, gain->data.params.init_val); 750 751 ret = sof_ipc4_widget_setup_msg(swidget, &gain->msg); 752 if (ret) 753 goto err; 754 755 sof_ipc4_widget_update_kcontrol_module_id(swidget); 756 757 return 0; 758 err: 759 sof_ipc4_free_audio_fmt(&gain->available_fmt); 760 kfree(gain); 761 swidget->private = NULL; 762 return ret; 763 } 764 765 static void sof_ipc4_widget_free_comp_pga(struct snd_sof_widget *swidget) 766 { 767 struct sof_ipc4_gain *gain = swidget->private; 768 769 if (!gain) 770 return; 771 772 sof_ipc4_free_audio_fmt(&gain->available_fmt); 773 kfree(swidget->private); 774 swidget->private = NULL; 775 } 776 777 static int sof_ipc4_widget_setup_comp_mixer(struct snd_sof_widget *swidget) 778 { 779 struct snd_soc_component *scomp = swidget->scomp; 780 struct sof_ipc4_mixer *mixer; 781 int ret; 782 783 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name); 784 785 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); 786 if (!mixer) 787 return -ENOMEM; 788 789 swidget->private = mixer; 790 791 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &mixer->available_fmt, 792 &mixer->base_config); 793 if (ret) 794 goto err; 795 796 ret = sof_ipc4_widget_setup_msg(swidget, &mixer->msg); 797 if (ret) 798 goto err; 799 800 return 0; 801 err: 802 sof_ipc4_free_audio_fmt(&mixer->available_fmt); 803 kfree(mixer); 804 swidget->private = NULL; 805 return ret; 806 } 807 808 static int sof_ipc4_widget_setup_comp_src(struct snd_sof_widget *swidget) 809 { 810 struct snd_soc_component *scomp = swidget->scomp; 811 struct snd_sof_pipeline *spipe = swidget->spipe; 812 struct sof_ipc4_src *src; 813 int ret; 814 815 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name); 816 817 src = kzalloc(sizeof(*src), GFP_KERNEL); 818 if (!src) 819 return -ENOMEM; 820 821 swidget->private = src; 822 823 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &src->available_fmt, 824 &src->data.base_config); 825 if (ret) 826 goto err; 827 828 ret = sof_update_ipc_object(scomp, &src->data, SOF_SRC_TOKENS, swidget->tuples, 829 swidget->num_tuples, sizeof(*src), 1); 830 if (ret) { 831 dev_err(scomp->dev, "Parsing SRC tokens failed\n"); 832 goto err; 833 } 834 835 spipe->core_mask |= BIT(swidget->core); 836 837 dev_dbg(scomp->dev, "SRC sink rate %d\n", src->data.sink_rate); 838 839 ret = sof_ipc4_widget_setup_msg(swidget, &src->msg); 840 if (ret) 841 goto err; 842 843 return 0; 844 err: 845 sof_ipc4_free_audio_fmt(&src->available_fmt); 846 kfree(src); 847 swidget->private = NULL; 848 return ret; 849 } 850 851 static void sof_ipc4_widget_free_comp_src(struct snd_sof_widget *swidget) 852 { 853 struct sof_ipc4_src *src = swidget->private; 854 855 if (!src) 856 return; 857 858 sof_ipc4_free_audio_fmt(&src->available_fmt); 859 kfree(swidget->private); 860 swidget->private = NULL; 861 } 862 863 static void sof_ipc4_widget_free_comp_mixer(struct snd_sof_widget *swidget) 864 { 865 struct sof_ipc4_mixer *mixer = swidget->private; 866 867 if (!mixer) 868 return; 869 870 sof_ipc4_free_audio_fmt(&mixer->available_fmt); 871 kfree(swidget->private); 872 swidget->private = NULL; 873 } 874 875 /* 876 * Add the process modules support. The process modules are defined as snd_soc_dapm_effect modules. 877 */ 878 static int sof_ipc4_widget_setup_comp_process(struct snd_sof_widget *swidget) 879 { 880 struct snd_soc_component *scomp = swidget->scomp; 881 struct sof_ipc4_fw_module *fw_module; 882 struct snd_sof_pipeline *spipe = swidget->spipe; 883 struct sof_ipc4_process *process; 884 void *cfg; 885 int ret; 886 887 process = kzalloc(sizeof(*process), GFP_KERNEL); 888 if (!process) 889 return -ENOMEM; 890 891 swidget->private = process; 892 893 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &process->available_fmt, 894 &process->base_config); 895 if (ret) 896 goto err; 897 898 ret = sof_ipc4_widget_setup_msg(swidget, &process->msg); 899 if (ret) 900 goto err; 901 902 /* parse process init module payload config type from module info */ 903 fw_module = swidget->module_info; 904 process->init_config = FIELD_GET(SOF_IPC4_MODULE_INIT_CONFIG_MASK, 905 fw_module->man4_module_entry.type); 906 907 process->ipc_config_size = sizeof(struct sof_ipc4_base_module_cfg); 908 909 /* allocate memory for base config extension if needed */ 910 if (process->init_config == SOF_IPC4_MODULE_INIT_CONFIG_TYPE_BASE_CFG_WITH_EXT) { 911 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext; 912 u32 ext_size = struct_size(base_cfg_ext, pin_formats, 913 size_add(swidget->num_input_pins, 914 swidget->num_output_pins)); 915 916 base_cfg_ext = kzalloc(ext_size, GFP_KERNEL); 917 if (!base_cfg_ext) { 918 ret = -ENOMEM; 919 goto free_available_fmt; 920 } 921 922 base_cfg_ext->num_input_pin_fmts = swidget->num_input_pins; 923 base_cfg_ext->num_output_pin_fmts = swidget->num_output_pins; 924 process->base_config_ext = base_cfg_ext; 925 process->base_config_ext_size = ext_size; 926 process->ipc_config_size += ext_size; 927 } 928 929 cfg = kzalloc(process->ipc_config_size, GFP_KERNEL); 930 if (!cfg) { 931 ret = -ENOMEM; 932 goto free_base_cfg_ext; 933 } 934 935 process->ipc_config_data = cfg; 936 937 sof_ipc4_widget_update_kcontrol_module_id(swidget); 938 939 /* set pipeline core mask to keep track of the core the module is scheduled to run on */ 940 spipe->core_mask |= BIT(swidget->core); 941 942 return 0; 943 free_base_cfg_ext: 944 kfree(process->base_config_ext); 945 process->base_config_ext = NULL; 946 free_available_fmt: 947 sof_ipc4_free_audio_fmt(&process->available_fmt); 948 err: 949 kfree(process); 950 swidget->private = NULL; 951 return ret; 952 } 953 954 static void sof_ipc4_widget_free_comp_process(struct snd_sof_widget *swidget) 955 { 956 struct sof_ipc4_process *process = swidget->private; 957 958 if (!process) 959 return; 960 961 kfree(process->ipc_config_data); 962 kfree(process->base_config_ext); 963 sof_ipc4_free_audio_fmt(&process->available_fmt); 964 kfree(swidget->private); 965 swidget->private = NULL; 966 } 967 968 static void 969 sof_ipc4_update_resource_usage(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget, 970 struct sof_ipc4_base_module_cfg *base_config) 971 { 972 struct sof_ipc4_fw_module *fw_module = swidget->module_info; 973 struct snd_sof_widget *pipe_widget; 974 struct sof_ipc4_pipeline *pipeline; 975 int task_mem, queue_mem; 976 int ibs, bss, total; 977 978 ibs = base_config->ibs; 979 bss = base_config->is_pages; 980 981 task_mem = SOF_IPC4_PIPELINE_OBJECT_SIZE; 982 task_mem += SOF_IPC4_MODULE_INSTANCE_LIST_ITEM_SIZE + bss; 983 984 if (fw_module->man4_module_entry.type & SOF_IPC4_MODULE_LL) { 985 task_mem += SOF_IPC4_FW_ROUNDUP(SOF_IPC4_LL_TASK_OBJECT_SIZE); 986 task_mem += SOF_IPC4_FW_MAX_QUEUE_COUNT * SOF_IPC4_MODULE_INSTANCE_LIST_ITEM_SIZE; 987 task_mem += SOF_IPC4_LL_TASK_LIST_ITEM_SIZE; 988 } else { 989 task_mem += SOF_IPC4_FW_ROUNDUP(SOF_IPC4_DP_TASK_OBJECT_SIZE); 990 task_mem += SOF_IPC4_DP_TASK_LIST_SIZE; 991 } 992 993 ibs = SOF_IPC4_FW_ROUNDUP(ibs); 994 queue_mem = SOF_IPC4_FW_MAX_QUEUE_COUNT * (SOF_IPC4_DATA_QUEUE_OBJECT_SIZE + ibs); 995 996 total = SOF_IPC4_FW_PAGE(task_mem + queue_mem); 997 998 pipe_widget = swidget->spipe->pipe_widget; 999 pipeline = pipe_widget->private; 1000 pipeline->mem_usage += total; 1001 1002 /* Update base_config->cpc from the module manifest */ 1003 sof_ipc4_update_cpc_from_manifest(sdev, fw_module, base_config); 1004 1005 if (ignore_cpc) { 1006 dev_dbg(sdev->dev, "%s: ibs / obs: %u / %u, forcing cpc to 0 from %u\n", 1007 swidget->widget->name, base_config->ibs, base_config->obs, 1008 base_config->cpc); 1009 base_config->cpc = 0; 1010 } else { 1011 dev_dbg(sdev->dev, "%s: ibs / obs / cpc: %u / %u / %u\n", 1012 swidget->widget->name, base_config->ibs, base_config->obs, 1013 base_config->cpc); 1014 } 1015 } 1016 1017 static int sof_ipc4_widget_assign_instance_id(struct snd_sof_dev *sdev, 1018 struct snd_sof_widget *swidget) 1019 { 1020 struct sof_ipc4_fw_module *fw_module = swidget->module_info; 1021 int max_instances = fw_module->man4_module_entry.instance_max_count; 1022 1023 swidget->instance_id = ida_alloc_max(&fw_module->m_ida, max_instances, GFP_KERNEL); 1024 if (swidget->instance_id < 0) { 1025 dev_err(sdev->dev, "failed to assign instance id for widget %s", 1026 swidget->widget->name); 1027 return swidget->instance_id; 1028 } 1029 1030 return 0; 1031 } 1032 1033 /* update hw_params based on the audio stream format */ 1034 static int sof_ipc4_update_hw_params(struct snd_sof_dev *sdev, struct snd_pcm_hw_params *params, 1035 struct sof_ipc4_audio_format *fmt) 1036 { 1037 snd_pcm_format_t snd_fmt; 1038 struct snd_interval *i; 1039 struct snd_mask *m; 1040 int valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg); 1041 unsigned int channels, rate; 1042 1043 switch (valid_bits) { 1044 case 16: 1045 snd_fmt = SNDRV_PCM_FORMAT_S16_LE; 1046 break; 1047 case 24: 1048 snd_fmt = SNDRV_PCM_FORMAT_S24_LE; 1049 break; 1050 case 32: 1051 snd_fmt = SNDRV_PCM_FORMAT_S32_LE; 1052 break; 1053 default: 1054 dev_err(sdev->dev, "invalid PCM valid_bits %d\n", valid_bits); 1055 return -EINVAL; 1056 } 1057 1058 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 1059 snd_mask_none(m); 1060 snd_mask_set_format(m, snd_fmt); 1061 1062 rate = fmt->sampling_frequency; 1063 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 1064 i->min = rate; 1065 i->max = rate; 1066 1067 channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(fmt->fmt_cfg); 1068 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 1069 i->min = channels; 1070 i->max = channels; 1071 1072 return 0; 1073 } 1074 1075 static bool sof_ipc4_is_single_format(struct snd_sof_dev *sdev, 1076 struct sof_ipc4_pin_format *pin_fmts, u32 pin_fmts_size) 1077 { 1078 struct sof_ipc4_audio_format *fmt; 1079 u32 rate, channels, valid_bits; 1080 int i; 1081 1082 fmt = &pin_fmts[0].audio_fmt; 1083 rate = fmt->sampling_frequency; 1084 channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(fmt->fmt_cfg); 1085 valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg); 1086 1087 /* check if all output formats in topology are the same */ 1088 for (i = 1; i < pin_fmts_size; i++) { 1089 u32 _rate, _channels, _valid_bits; 1090 1091 fmt = &pin_fmts[i].audio_fmt; 1092 _rate = fmt->sampling_frequency; 1093 _channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(fmt->fmt_cfg); 1094 _valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg); 1095 1096 if (_rate != rate || _channels != channels || _valid_bits != valid_bits) 1097 return false; 1098 } 1099 1100 return true; 1101 } 1102 1103 static int sof_ipc4_init_output_audio_fmt(struct snd_sof_dev *sdev, 1104 struct sof_ipc4_base_module_cfg *base_config, 1105 struct sof_ipc4_available_audio_format *available_fmt, 1106 u32 out_ref_rate, u32 out_ref_channels, 1107 u32 out_ref_valid_bits) 1108 { 1109 struct sof_ipc4_audio_format *out_fmt; 1110 bool single_format; 1111 int i; 1112 1113 if (!available_fmt->num_output_formats) 1114 return -EINVAL; 1115 1116 single_format = sof_ipc4_is_single_format(sdev, available_fmt->output_pin_fmts, 1117 available_fmt->num_output_formats); 1118 1119 /* pick the first format if there's only one available or if all formats are the same */ 1120 if (single_format) { 1121 base_config->obs = available_fmt->output_pin_fmts[0].buffer_size; 1122 return 0; 1123 } 1124 1125 /* 1126 * if there are multiple output formats, then choose the output format that matches 1127 * the reference params 1128 */ 1129 for (i = 0; i < available_fmt->num_output_formats; i++) { 1130 u32 _out_rate, _out_channels, _out_valid_bits; 1131 1132 out_fmt = &available_fmt->output_pin_fmts[i].audio_fmt; 1133 _out_rate = out_fmt->sampling_frequency; 1134 _out_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(out_fmt->fmt_cfg); 1135 _out_valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(out_fmt->fmt_cfg); 1136 1137 if (_out_rate == out_ref_rate && _out_channels == out_ref_channels && 1138 _out_valid_bits == out_ref_valid_bits) { 1139 base_config->obs = available_fmt->output_pin_fmts[i].buffer_size; 1140 return i; 1141 } 1142 } 1143 1144 return -EINVAL; 1145 } 1146 1147 static int sof_ipc4_get_valid_bits(struct snd_sof_dev *sdev, struct snd_pcm_hw_params *params) 1148 { 1149 switch (params_format(params)) { 1150 case SNDRV_PCM_FORMAT_S16_LE: 1151 return 16; 1152 case SNDRV_PCM_FORMAT_S24_LE: 1153 return 24; 1154 case SNDRV_PCM_FORMAT_S32_LE: 1155 return 32; 1156 default: 1157 dev_err(sdev->dev, "invalid pcm frame format %d\n", params_format(params)); 1158 return -EINVAL; 1159 } 1160 } 1161 1162 static int sof_ipc4_init_input_audio_fmt(struct snd_sof_dev *sdev, 1163 struct snd_sof_widget *swidget, 1164 struct sof_ipc4_base_module_cfg *base_config, 1165 struct snd_pcm_hw_params *params, 1166 struct sof_ipc4_available_audio_format *available_fmt) 1167 { 1168 struct sof_ipc4_pin_format *pin_fmts = available_fmt->input_pin_fmts; 1169 u32 pin_fmts_size = available_fmt->num_input_formats; 1170 u32 valid_bits; 1171 u32 channels; 1172 u32 rate; 1173 bool single_format; 1174 int sample_valid_bits; 1175 int i = 0; 1176 1177 if (!available_fmt->num_input_formats) { 1178 dev_err(sdev->dev, "no input formats for %s\n", swidget->widget->name); 1179 return -EINVAL; 1180 } 1181 1182 single_format = sof_ipc4_is_single_format(sdev, available_fmt->input_pin_fmts, 1183 available_fmt->num_input_formats); 1184 if (single_format) 1185 goto in_fmt; 1186 1187 sample_valid_bits = sof_ipc4_get_valid_bits(sdev, params); 1188 if (sample_valid_bits < 0) 1189 return sample_valid_bits; 1190 1191 /* 1192 * Search supported input audio formats with pin index 0 to match rate, channels and 1193 * sample_valid_bits from reference params 1194 */ 1195 for (i = 0; i < pin_fmts_size; i++) { 1196 struct sof_ipc4_audio_format *fmt = &pin_fmts[i].audio_fmt; 1197 1198 if (pin_fmts[i].pin_index) 1199 continue; 1200 1201 rate = fmt->sampling_frequency; 1202 channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(fmt->fmt_cfg); 1203 valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg); 1204 if (params_rate(params) == rate && params_channels(params) == channels && 1205 sample_valid_bits == valid_bits) { 1206 dev_dbg(sdev->dev, "matched audio format index for %uHz, %ubit, %u channels: %d\n", 1207 rate, valid_bits, channels, i); 1208 break; 1209 } 1210 } 1211 1212 if (i == pin_fmts_size) { 1213 dev_err(sdev->dev, "%s: Unsupported audio format: %uHz, %ubit, %u channels\n", 1214 __func__, params_rate(params), sample_valid_bits, params_channels(params)); 1215 return -EINVAL; 1216 } 1217 1218 in_fmt: 1219 /* copy input format */ 1220 if (available_fmt->num_input_formats && i < available_fmt->num_input_formats) { 1221 memcpy(&base_config->audio_fmt, &available_fmt->input_pin_fmts[i].audio_fmt, 1222 sizeof(struct sof_ipc4_audio_format)); 1223 1224 /* set base_cfg ibs/obs */ 1225 base_config->ibs = available_fmt->input_pin_fmts[i].buffer_size; 1226 1227 dev_dbg(sdev->dev, "Init input audio formats for %s\n", swidget->widget->name); 1228 sof_ipc4_dbg_audio_format(sdev->dev, &available_fmt->input_pin_fmts[i], 1); 1229 } 1230 1231 return i; 1232 } 1233 1234 static void sof_ipc4_unprepare_copier_module(struct snd_sof_widget *swidget) 1235 { 1236 struct sof_ipc4_copier *ipc4_copier = NULL; 1237 struct snd_sof_widget *pipe_widget; 1238 struct sof_ipc4_pipeline *pipeline; 1239 1240 /* reset pipeline memory usage */ 1241 pipe_widget = swidget->spipe->pipe_widget; 1242 pipeline = pipe_widget->private; 1243 pipeline->mem_usage = 0; 1244 1245 if (WIDGET_IS_AIF(swidget->id) || swidget->id == snd_soc_dapm_buffer) { 1246 if (pipeline->use_chain_dma) { 1247 pipeline->msg.primary = 0; 1248 pipeline->msg.extension = 0; 1249 } 1250 ipc4_copier = swidget->private; 1251 } else if (WIDGET_IS_DAI(swidget->id)) { 1252 struct snd_sof_dai *dai = swidget->private; 1253 1254 ipc4_copier = dai->private; 1255 1256 if (pipeline->use_chain_dma) { 1257 pipeline->msg.primary = 0; 1258 pipeline->msg.extension = 0; 1259 } 1260 1261 if (ipc4_copier->dai_type == SOF_DAI_INTEL_ALH) { 1262 struct sof_ipc4_copier_data *copier_data = &ipc4_copier->data; 1263 struct sof_ipc4_alh_configuration_blob *blob; 1264 unsigned int group_id; 1265 1266 blob = (struct sof_ipc4_alh_configuration_blob *)ipc4_copier->copier_config; 1267 if (blob->alh_cfg.device_count > 1) { 1268 group_id = SOF_IPC4_NODE_INDEX(ipc4_copier->data.gtw_cfg.node_id) - 1269 ALH_MULTI_GTW_BASE; 1270 ida_free(&alh_group_ida, group_id); 1271 } 1272 1273 /* clear the node ID */ 1274 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK; 1275 } 1276 } 1277 1278 if (ipc4_copier) { 1279 kfree(ipc4_copier->ipc_config_data); 1280 ipc4_copier->ipc_config_data = NULL; 1281 ipc4_copier->ipc_config_size = 0; 1282 } 1283 } 1284 1285 #if IS_ENABLED(CONFIG_ACPI) && IS_ENABLED(CONFIG_SND_INTEL_NHLT) 1286 static int snd_sof_get_hw_config_params(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, 1287 int *sample_rate, int *channel_count, int *bit_depth) 1288 { 1289 struct snd_soc_tplg_hw_config *hw_config; 1290 struct snd_sof_dai_link *slink; 1291 bool dai_link_found = false; 1292 bool hw_cfg_found = false; 1293 int i; 1294 1295 /* get current hw_config from link */ 1296 list_for_each_entry(slink, &sdev->dai_link_list, list) { 1297 if (!strcmp(slink->link->name, dai->name)) { 1298 dai_link_found = true; 1299 break; 1300 } 1301 } 1302 1303 if (!dai_link_found) { 1304 dev_err(sdev->dev, "%s: no DAI link found for DAI %s\n", __func__, dai->name); 1305 return -EINVAL; 1306 } 1307 1308 for (i = 0; i < slink->num_hw_configs; i++) { 1309 hw_config = &slink->hw_configs[i]; 1310 if (dai->current_config == le32_to_cpu(hw_config->id)) { 1311 hw_cfg_found = true; 1312 break; 1313 } 1314 } 1315 1316 if (!hw_cfg_found) { 1317 dev_err(sdev->dev, "%s: no matching hw_config found for DAI %s\n", __func__, 1318 dai->name); 1319 return -EINVAL; 1320 } 1321 1322 *bit_depth = le32_to_cpu(hw_config->tdm_slot_width); 1323 *channel_count = le32_to_cpu(hw_config->tdm_slots); 1324 *sample_rate = le32_to_cpu(hw_config->fsync_rate); 1325 1326 dev_dbg(sdev->dev, "sample rate: %d sample width: %d channels: %d\n", 1327 *sample_rate, *bit_depth, *channel_count); 1328 1329 return 0; 1330 } 1331 1332 static int snd_sof_get_nhlt_endpoint_data(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, 1333 struct snd_pcm_hw_params *params, u32 dai_index, 1334 u32 linktype, u8 dir, u32 **dst, u32 *len) 1335 { 1336 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 1337 struct nhlt_specific_cfg *cfg; 1338 int sample_rate, channel_count; 1339 int bit_depth, ret; 1340 u32 nhlt_type; 1341 1342 /* convert to NHLT type */ 1343 switch (linktype) { 1344 case SOF_DAI_INTEL_DMIC: 1345 nhlt_type = NHLT_LINK_DMIC; 1346 bit_depth = params_width(params); 1347 channel_count = params_channels(params); 1348 sample_rate = params_rate(params); 1349 break; 1350 case SOF_DAI_INTEL_SSP: 1351 nhlt_type = NHLT_LINK_SSP; 1352 ret = snd_sof_get_hw_config_params(sdev, dai, &sample_rate, &channel_count, 1353 &bit_depth); 1354 if (ret < 0) 1355 return ret; 1356 break; 1357 default: 1358 return 0; 1359 } 1360 1361 dev_dbg(sdev->dev, "dai index %d nhlt type %d direction %d\n", 1362 dai_index, nhlt_type, dir); 1363 1364 /* find NHLT blob with matching params */ 1365 cfg = intel_nhlt_get_endpoint_blob(sdev->dev, ipc4_data->nhlt, dai_index, nhlt_type, 1366 bit_depth, bit_depth, channel_count, sample_rate, 1367 dir, 0); 1368 1369 if (!cfg) { 1370 dev_err(sdev->dev, 1371 "no matching blob for sample rate: %d sample width: %d channels: %d\n", 1372 sample_rate, bit_depth, channel_count); 1373 return -EINVAL; 1374 } 1375 1376 /* config length should be in dwords */ 1377 *len = cfg->size >> 2; 1378 *dst = (u32 *)cfg->caps; 1379 1380 return 0; 1381 } 1382 #else 1383 static int snd_sof_get_nhlt_endpoint_data(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, 1384 struct snd_pcm_hw_params *params, u32 dai_index, 1385 u32 linktype, u8 dir, u32 **dst, u32 *len) 1386 { 1387 return 0; 1388 } 1389 #endif 1390 1391 static bool sof_ipc4_copier_is_single_format(struct snd_sof_dev *sdev, 1392 struct sof_ipc4_pin_format *pin_fmts, 1393 u32 pin_fmts_size) 1394 { 1395 struct sof_ipc4_audio_format *fmt; 1396 u32 valid_bits; 1397 int i; 1398 1399 fmt = &pin_fmts[0].audio_fmt; 1400 valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg); 1401 1402 /* check if all output formats in topology are the same */ 1403 for (i = 1; i < pin_fmts_size; i++) { 1404 u32 _valid_bits; 1405 1406 fmt = &pin_fmts[i].audio_fmt; 1407 _valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg); 1408 1409 if (_valid_bits != valid_bits) 1410 return false; 1411 } 1412 1413 return true; 1414 } 1415 1416 static int 1417 sof_ipc4_prepare_copier_module(struct snd_sof_widget *swidget, 1418 struct snd_pcm_hw_params *fe_params, 1419 struct snd_sof_platform_stream_params *platform_params, 1420 struct snd_pcm_hw_params *pipeline_params, int dir) 1421 { 1422 struct sof_ipc4_available_audio_format *available_fmt; 1423 struct snd_soc_component *scomp = swidget->scomp; 1424 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1425 struct sof_ipc4_copier_data *copier_data; 1426 struct snd_pcm_hw_params *ref_params; 1427 struct sof_ipc4_copier *ipc4_copier; 1428 struct snd_sof_dai *dai; 1429 u32 gtw_cfg_config_length; 1430 u32 dma_config_tlv_size = 0; 1431 void **ipc_config_data; 1432 int *ipc_config_size; 1433 u32 **data; 1434 int ipc_size, ret, out_ref_valid_bits; 1435 u32 out_ref_rate, out_ref_channels; 1436 u32 deep_buffer_dma_ms = 0; 1437 int output_fmt_index; 1438 bool single_output_format; 1439 1440 dev_dbg(sdev->dev, "copier %s, type %d", swidget->widget->name, swidget->id); 1441 1442 switch (swidget->id) { 1443 case snd_soc_dapm_aif_in: 1444 case snd_soc_dapm_aif_out: 1445 { 1446 struct sof_ipc4_gtw_attributes *gtw_attr; 1447 struct snd_sof_widget *pipe_widget; 1448 struct sof_ipc4_pipeline *pipeline; 1449 1450 /* parse the deep buffer dma size */ 1451 ret = sof_update_ipc_object(scomp, &deep_buffer_dma_ms, 1452 SOF_COPIER_DEEP_BUFFER_TOKENS, swidget->tuples, 1453 swidget->num_tuples, sizeof(u32), 1); 1454 if (ret) { 1455 dev_err(scomp->dev, "Failed to parse deep buffer dma size for %s\n", 1456 swidget->widget->name); 1457 return ret; 1458 } 1459 1460 ipc4_copier = (struct sof_ipc4_copier *)swidget->private; 1461 gtw_attr = ipc4_copier->gtw_attr; 1462 copier_data = &ipc4_copier->data; 1463 available_fmt = &ipc4_copier->available_fmt; 1464 1465 pipe_widget = swidget->spipe->pipe_widget; 1466 pipeline = pipe_widget->private; 1467 1468 if (pipeline->use_chain_dma) { 1469 u32 host_dma_id; 1470 u32 fifo_size; 1471 1472 host_dma_id = platform_params->stream_tag - 1; 1473 pipeline->msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_HOST_ID(host_dma_id); 1474 1475 /* Set SCS bit for S16_LE format only */ 1476 if (params_format(fe_params) == SNDRV_PCM_FORMAT_S16_LE) 1477 pipeline->msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_SCS_MASK; 1478 1479 /* 1480 * Despite its name the bitfield 'fifo_size' is used to define DMA buffer 1481 * size. The expression calculates 2ms buffer size. 1482 */ 1483 fifo_size = DIV_ROUND_UP((SOF_IPC4_CHAIN_DMA_BUF_SIZE_MS * 1484 params_rate(fe_params) * 1485 params_channels(fe_params) * 1486 params_physical_width(fe_params)), 8000); 1487 pipeline->msg.extension |= SOF_IPC4_GLB_EXT_CHAIN_DMA_FIFO_SIZE(fifo_size); 1488 1489 /* 1490 * Chain DMA does not support stream timestamping, set node_id to invalid 1491 * to skip the code in sof_ipc4_get_stream_start_offset(). 1492 */ 1493 copier_data->gtw_cfg.node_id = SOF_IPC4_INVALID_NODE_ID; 1494 1495 return 0; 1496 } 1497 1498 /* 1499 * Use the input_pin_fmts to match pcm params for playback and the output_pin_fmts 1500 * for capture. 1501 */ 1502 if (dir == SNDRV_PCM_STREAM_PLAYBACK) 1503 ref_params = fe_params; 1504 else 1505 ref_params = pipeline_params; 1506 1507 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK; 1508 copier_data->gtw_cfg.node_id |= 1509 SOF_IPC4_NODE_INDEX(platform_params->stream_tag - 1); 1510 1511 /* set gateway attributes */ 1512 gtw_attr->lp_buffer_alloc = pipeline->lp_mode; 1513 break; 1514 } 1515 case snd_soc_dapm_dai_in: 1516 case snd_soc_dapm_dai_out: 1517 { 1518 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; 1519 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 1520 1521 if (pipeline->use_chain_dma) 1522 return 0; 1523 1524 dai = swidget->private; 1525 1526 ipc4_copier = (struct sof_ipc4_copier *)dai->private; 1527 copier_data = &ipc4_copier->data; 1528 available_fmt = &ipc4_copier->available_fmt; 1529 1530 /* 1531 * When there is format conversion within a pipeline, the number of supported 1532 * output formats is typically limited to just 1 for the DAI copiers. But when there 1533 * is no format conversion, the DAI copiers input format must match that of the 1534 * FE hw_params for capture and the pipeline params for playback. 1535 */ 1536 if (dir == SNDRV_PCM_STREAM_PLAYBACK) 1537 ref_params = pipeline_params; 1538 else 1539 ref_params = fe_params; 1540 1541 ret = snd_sof_get_nhlt_endpoint_data(sdev, dai, fe_params, ipc4_copier->dai_index, 1542 ipc4_copier->dai_type, dir, 1543 &ipc4_copier->copier_config, 1544 &copier_data->gtw_cfg.config_length); 1545 if (ret < 0) 1546 return ret; 1547 1548 break; 1549 } 1550 case snd_soc_dapm_buffer: 1551 { 1552 ipc4_copier = (struct sof_ipc4_copier *)swidget->private; 1553 copier_data = &ipc4_copier->data; 1554 available_fmt = &ipc4_copier->available_fmt; 1555 ref_params = pipeline_params; 1556 1557 break; 1558 } 1559 default: 1560 dev_err(sdev->dev, "unsupported type %d for copier %s", 1561 swidget->id, swidget->widget->name); 1562 return -EINVAL; 1563 } 1564 1565 /* set input and output audio formats */ 1566 ret = sof_ipc4_init_input_audio_fmt(sdev, swidget, &copier_data->base_config, ref_params, 1567 available_fmt); 1568 if (ret < 0) 1569 return ret; 1570 1571 /* set the reference params for output format selection */ 1572 single_output_format = sof_ipc4_copier_is_single_format(sdev, 1573 available_fmt->output_pin_fmts, 1574 available_fmt->num_output_formats); 1575 switch (swidget->id) { 1576 case snd_soc_dapm_aif_in: 1577 case snd_soc_dapm_dai_out: 1578 case snd_soc_dapm_buffer: 1579 { 1580 struct sof_ipc4_audio_format *in_fmt; 1581 1582 in_fmt = &available_fmt->input_pin_fmts[ret].audio_fmt; 1583 out_ref_rate = in_fmt->sampling_frequency; 1584 out_ref_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(in_fmt->fmt_cfg); 1585 1586 if (!single_output_format) 1587 out_ref_valid_bits = 1588 SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(in_fmt->fmt_cfg); 1589 break; 1590 } 1591 case snd_soc_dapm_aif_out: 1592 case snd_soc_dapm_dai_in: 1593 out_ref_rate = params_rate(fe_params); 1594 out_ref_channels = params_channels(fe_params); 1595 if (!single_output_format) { 1596 out_ref_valid_bits = sof_ipc4_get_valid_bits(sdev, fe_params); 1597 if (out_ref_valid_bits < 0) 1598 return out_ref_valid_bits; 1599 } 1600 break; 1601 default: 1602 /* 1603 * Unsupported type should be caught by the former switch default 1604 * case, this should never happen in reality. 1605 */ 1606 return -EINVAL; 1607 } 1608 1609 /* 1610 * if the output format is the same across all available output formats, choose 1611 * that as the reference. 1612 */ 1613 if (single_output_format) { 1614 struct sof_ipc4_audio_format *out_fmt; 1615 1616 out_fmt = &available_fmt->output_pin_fmts[0].audio_fmt; 1617 out_ref_valid_bits = 1618 SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(out_fmt->fmt_cfg); 1619 } 1620 1621 dev_dbg(sdev->dev, "copier %s: reference output rate %d, channels %d valid_bits %d\n", 1622 swidget->widget->name, out_ref_rate, out_ref_channels, out_ref_valid_bits); 1623 1624 output_fmt_index = sof_ipc4_init_output_audio_fmt(sdev, &copier_data->base_config, 1625 available_fmt, out_ref_rate, 1626 out_ref_channels, out_ref_valid_bits); 1627 if (output_fmt_index < 0) { 1628 dev_err(sdev->dev, "Failed to initialize output format for %s", 1629 swidget->widget->name); 1630 return output_fmt_index; 1631 } 1632 1633 /* 1634 * Set the output format. Current topology defines pin 0 input and output formats in pairs. 1635 * This assumes that the pin 0 formats are defined before all other pins. 1636 * So pick the output audio format with the same index as the chosen 1637 * input format. This logic will need to be updated when the format definitions 1638 * in topology change. 1639 */ 1640 memcpy(&copier_data->out_format, 1641 &available_fmt->output_pin_fmts[output_fmt_index].audio_fmt, 1642 sizeof(struct sof_ipc4_audio_format)); 1643 dev_dbg(sdev->dev, "Output audio format for %s\n", swidget->widget->name); 1644 sof_ipc4_dbg_audio_format(sdev->dev, &available_fmt->output_pin_fmts[output_fmt_index], 1); 1645 1646 switch (swidget->id) { 1647 case snd_soc_dapm_dai_in: 1648 case snd_soc_dapm_dai_out: 1649 { 1650 /* 1651 * Only SOF_DAI_INTEL_ALH needs copier_data to set blob. 1652 * That's why only ALH dai's blob is set after sof_ipc4_init_input_audio_fmt 1653 */ 1654 if (ipc4_copier->dai_type == SOF_DAI_INTEL_ALH) { 1655 struct sof_ipc4_alh_configuration_blob *blob; 1656 struct sof_ipc4_copier_data *alh_data; 1657 struct sof_ipc4_copier *alh_copier; 1658 struct snd_sof_widget *w; 1659 u32 ch_count = 0; 1660 u32 ch_mask = 0; 1661 u32 ch_map; 1662 u32 step; 1663 u32 mask; 1664 int i; 1665 1666 blob = (struct sof_ipc4_alh_configuration_blob *)ipc4_copier->copier_config; 1667 1668 blob->gw_attr.lp_buffer_alloc = 0; 1669 1670 /* Get channel_mask from ch_map */ 1671 ch_map = copier_data->base_config.audio_fmt.ch_map; 1672 for (i = 0; ch_map; i++) { 1673 if ((ch_map & 0xf) != 0xf) { 1674 ch_mask |= BIT(i); 1675 ch_count++; 1676 } 1677 ch_map >>= 4; 1678 } 1679 1680 step = ch_count / blob->alh_cfg.device_count; 1681 mask = GENMASK(step - 1, 0); 1682 /* 1683 * Set each gtw_cfg.node_id to blob->alh_cfg.mapping[] 1684 * for all widgets with the same stream name 1685 */ 1686 i = 0; 1687 list_for_each_entry(w, &sdev->widget_list, list) { 1688 if (w->widget->sname && 1689 strcmp(w->widget->sname, swidget->widget->sname)) 1690 continue; 1691 1692 dai = w->private; 1693 alh_copier = (struct sof_ipc4_copier *)dai->private; 1694 alh_data = &alh_copier->data; 1695 blob->alh_cfg.mapping[i].device = alh_data->gtw_cfg.node_id; 1696 /* 1697 * Set the same channel mask for playback as the audio data is 1698 * duplicated for all speakers. For capture, split the channels 1699 * among the aggregated DAIs. For example, with 4 channels on 2 1700 * aggregated DAIs, the channel_mask should be 0x3 and 0xc for the 1701 * two DAI's. 1702 * The channel masks used depend on the cpu_dais used in the 1703 * dailink at the machine driver level, which actually comes from 1704 * the tables in soc_acpi files depending on the _ADR and devID 1705 * registers for each codec. 1706 */ 1707 if (w->id == snd_soc_dapm_dai_in) 1708 blob->alh_cfg.mapping[i].channel_mask = ch_mask; 1709 else 1710 blob->alh_cfg.mapping[i].channel_mask = mask << (step * i); 1711 1712 i++; 1713 } 1714 if (blob->alh_cfg.device_count > 1) { 1715 int group_id; 1716 1717 group_id = ida_alloc_max(&alh_group_ida, ALH_MULTI_GTW_COUNT - 1, 1718 GFP_KERNEL); 1719 1720 if (group_id < 0) 1721 return group_id; 1722 1723 /* add multi-gateway base */ 1724 group_id += ALH_MULTI_GTW_BASE; 1725 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK; 1726 copier_data->gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX(group_id); 1727 } 1728 } 1729 } 1730 } 1731 1732 /* modify the input params for the next widget */ 1733 ret = sof_ipc4_update_hw_params(sdev, pipeline_params, &copier_data->out_format); 1734 if (ret) 1735 return ret; 1736 1737 /* 1738 * Set the gateway dma_buffer_size to 2ms buffer size to meet the FW expectation. In the 1739 * deep buffer case, set the dma_buffer_size depending on the deep_buffer_dma_ms set 1740 * in topology. 1741 */ 1742 switch (swidget->id) { 1743 case snd_soc_dapm_dai_in: 1744 copier_data->gtw_cfg.dma_buffer_size = 1745 SOF_IPC4_MIN_DMA_BUFFER_SIZE * copier_data->base_config.ibs; 1746 break; 1747 case snd_soc_dapm_aif_in: 1748 copier_data->gtw_cfg.dma_buffer_size = 1749 max((u32)SOF_IPC4_MIN_DMA_BUFFER_SIZE, deep_buffer_dma_ms) * 1750 copier_data->base_config.ibs; 1751 break; 1752 case snd_soc_dapm_dai_out: 1753 case snd_soc_dapm_aif_out: 1754 copier_data->gtw_cfg.dma_buffer_size = 1755 SOF_IPC4_MIN_DMA_BUFFER_SIZE * copier_data->base_config.obs; 1756 break; 1757 default: 1758 break; 1759 } 1760 1761 data = &ipc4_copier->copier_config; 1762 ipc_config_size = &ipc4_copier->ipc_config_size; 1763 ipc_config_data = &ipc4_copier->ipc_config_data; 1764 1765 /* config_length is DWORD based */ 1766 gtw_cfg_config_length = copier_data->gtw_cfg.config_length * 4; 1767 ipc_size = sizeof(*copier_data) + gtw_cfg_config_length; 1768 1769 if (ipc4_copier->dma_config_tlv.type == SOF_IPC4_GTW_DMA_CONFIG_ID && 1770 ipc4_copier->dma_config_tlv.length) { 1771 dma_config_tlv_size = sizeof(ipc4_copier->dma_config_tlv) + 1772 ipc4_copier->dma_config_tlv.dma_config.dma_priv_config_size; 1773 1774 /* paranoia check on TLV size/length */ 1775 if (dma_config_tlv_size != ipc4_copier->dma_config_tlv.length + 1776 sizeof(uint32_t) * 2) { 1777 dev_err(sdev->dev, "Invalid configuration, TLV size %d length %d\n", 1778 dma_config_tlv_size, ipc4_copier->dma_config_tlv.length); 1779 return -EINVAL; 1780 } 1781 1782 ipc_size += dma_config_tlv_size; 1783 1784 /* we also need to increase the size at the gtw level */ 1785 copier_data->gtw_cfg.config_length += dma_config_tlv_size / 4; 1786 } 1787 1788 dev_dbg(sdev->dev, "copier %s, IPC size is %d", swidget->widget->name, ipc_size); 1789 1790 *ipc_config_data = kzalloc(ipc_size, GFP_KERNEL); 1791 if (!*ipc_config_data) 1792 return -ENOMEM; 1793 1794 *ipc_config_size = ipc_size; 1795 1796 /* update pipeline memory usage */ 1797 sof_ipc4_update_resource_usage(sdev, swidget, &copier_data->base_config); 1798 1799 /* copy IPC data */ 1800 memcpy(*ipc_config_data, (void *)copier_data, sizeof(*copier_data)); 1801 if (gtw_cfg_config_length) 1802 memcpy(*ipc_config_data + sizeof(*copier_data), 1803 *data, gtw_cfg_config_length); 1804 1805 /* add DMA Config TLV, if configured */ 1806 if (dma_config_tlv_size) 1807 memcpy(*ipc_config_data + sizeof(*copier_data) + 1808 gtw_cfg_config_length, 1809 &ipc4_copier->dma_config_tlv, dma_config_tlv_size); 1810 1811 /* 1812 * Restore gateway config length now that IPC payload is prepared. This avoids 1813 * counting the DMA CONFIG TLV multiple times 1814 */ 1815 copier_data->gtw_cfg.config_length = gtw_cfg_config_length / 4; 1816 1817 return 0; 1818 } 1819 1820 static int sof_ipc4_prepare_gain_module(struct snd_sof_widget *swidget, 1821 struct snd_pcm_hw_params *fe_params, 1822 struct snd_sof_platform_stream_params *platform_params, 1823 struct snd_pcm_hw_params *pipeline_params, int dir) 1824 { 1825 struct snd_soc_component *scomp = swidget->scomp; 1826 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1827 struct sof_ipc4_gain *gain = swidget->private; 1828 struct sof_ipc4_available_audio_format *available_fmt = &gain->available_fmt; 1829 struct sof_ipc4_audio_format *in_fmt; 1830 u32 out_ref_rate, out_ref_channels, out_ref_valid_bits; 1831 int ret; 1832 1833 ret = sof_ipc4_init_input_audio_fmt(sdev, swidget, &gain->data.base_config, 1834 pipeline_params, available_fmt); 1835 if (ret < 0) 1836 return ret; 1837 1838 in_fmt = &available_fmt->input_pin_fmts[ret].audio_fmt; 1839 out_ref_rate = in_fmt->sampling_frequency; 1840 out_ref_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(in_fmt->fmt_cfg); 1841 out_ref_valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(in_fmt->fmt_cfg); 1842 1843 ret = sof_ipc4_init_output_audio_fmt(sdev, &gain->data.base_config, available_fmt, 1844 out_ref_rate, out_ref_channels, out_ref_valid_bits); 1845 if (ret < 0) { 1846 dev_err(sdev->dev, "Failed to initialize output format for %s", 1847 swidget->widget->name); 1848 return ret; 1849 } 1850 1851 /* update pipeline memory usage */ 1852 sof_ipc4_update_resource_usage(sdev, swidget, &gain->data.base_config); 1853 1854 return 0; 1855 } 1856 1857 static int sof_ipc4_prepare_mixer_module(struct snd_sof_widget *swidget, 1858 struct snd_pcm_hw_params *fe_params, 1859 struct snd_sof_platform_stream_params *platform_params, 1860 struct snd_pcm_hw_params *pipeline_params, int dir) 1861 { 1862 struct snd_soc_component *scomp = swidget->scomp; 1863 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1864 struct sof_ipc4_mixer *mixer = swidget->private; 1865 struct sof_ipc4_available_audio_format *available_fmt = &mixer->available_fmt; 1866 struct sof_ipc4_audio_format *in_fmt; 1867 u32 out_ref_rate, out_ref_channels, out_ref_valid_bits; 1868 int ret; 1869 1870 ret = sof_ipc4_init_input_audio_fmt(sdev, swidget, &mixer->base_config, 1871 pipeline_params, available_fmt); 1872 if (ret < 0) 1873 return ret; 1874 1875 in_fmt = &available_fmt->input_pin_fmts[ret].audio_fmt; 1876 out_ref_rate = in_fmt->sampling_frequency; 1877 out_ref_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(in_fmt->fmt_cfg); 1878 out_ref_valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(in_fmt->fmt_cfg); 1879 1880 ret = sof_ipc4_init_output_audio_fmt(sdev, &mixer->base_config, available_fmt, 1881 out_ref_rate, out_ref_channels, out_ref_valid_bits); 1882 if (ret < 0) { 1883 dev_err(sdev->dev, "Failed to initialize output format for %s", 1884 swidget->widget->name); 1885 return ret; 1886 } 1887 1888 /* update pipeline memory usage */ 1889 sof_ipc4_update_resource_usage(sdev, swidget, &mixer->base_config); 1890 1891 return 0; 1892 } 1893 1894 static int sof_ipc4_prepare_src_module(struct snd_sof_widget *swidget, 1895 struct snd_pcm_hw_params *fe_params, 1896 struct snd_sof_platform_stream_params *platform_params, 1897 struct snd_pcm_hw_params *pipeline_params, int dir) 1898 { 1899 struct snd_soc_component *scomp = swidget->scomp; 1900 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1901 struct sof_ipc4_src *src = swidget->private; 1902 struct sof_ipc4_available_audio_format *available_fmt = &src->available_fmt; 1903 struct sof_ipc4_audio_format *out_audio_fmt; 1904 struct sof_ipc4_audio_format *in_audio_fmt; 1905 u32 out_ref_rate, out_ref_channels, out_ref_valid_bits; 1906 int output_format_index, input_format_index; 1907 1908 input_format_index = sof_ipc4_init_input_audio_fmt(sdev, swidget, &src->data.base_config, 1909 pipeline_params, available_fmt); 1910 if (input_format_index < 0) 1911 return input_format_index; 1912 1913 /* 1914 * For playback, the SRC sink rate will be configured based on the requested output 1915 * format, which is restricted to only deal with DAI's with a single format for now. 1916 */ 1917 if (dir == SNDRV_PCM_STREAM_PLAYBACK && available_fmt->num_output_formats > 1) { 1918 dev_err(sdev->dev, "Invalid number of output formats: %d for SRC %s\n", 1919 available_fmt->num_output_formats, swidget->widget->name); 1920 return -EINVAL; 1921 } 1922 1923 /* 1924 * SRC does not perform format conversion, so the output channels and valid bit depth must 1925 * be the same as that of the input. 1926 */ 1927 in_audio_fmt = &available_fmt->input_pin_fmts[input_format_index].audio_fmt; 1928 out_ref_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(in_audio_fmt->fmt_cfg); 1929 out_ref_valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(in_audio_fmt->fmt_cfg); 1930 1931 /* 1932 * For capture, the SRC module should convert the rate to match the rate requested by the 1933 * PCM hw_params. Set the reference params based on the fe_params unconditionally as it 1934 * will be ignored for playback anyway. 1935 */ 1936 out_ref_rate = params_rate(fe_params); 1937 1938 output_format_index = sof_ipc4_init_output_audio_fmt(sdev, &src->data.base_config, 1939 available_fmt, out_ref_rate, 1940 out_ref_channels, out_ref_valid_bits); 1941 if (output_format_index < 0) { 1942 dev_err(sdev->dev, "Failed to initialize output format for %s", 1943 swidget->widget->name); 1944 return output_format_index; 1945 } 1946 1947 /* update pipeline memory usage */ 1948 sof_ipc4_update_resource_usage(sdev, swidget, &src->data.base_config); 1949 1950 out_audio_fmt = &available_fmt->output_pin_fmts[output_format_index].audio_fmt; 1951 src->data.sink_rate = out_audio_fmt->sampling_frequency; 1952 1953 /* update pipeline_params for sink widgets */ 1954 return sof_ipc4_update_hw_params(sdev, pipeline_params, out_audio_fmt); 1955 } 1956 1957 static int 1958 sof_ipc4_process_set_pin_formats(struct snd_sof_widget *swidget, int pin_type) 1959 { 1960 struct sof_ipc4_process *process = swidget->private; 1961 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext = process->base_config_ext; 1962 struct sof_ipc4_available_audio_format *available_fmt = &process->available_fmt; 1963 struct sof_ipc4_pin_format *pin_format, *format_list_to_search; 1964 struct snd_soc_component *scomp = swidget->scomp; 1965 int num_pins, format_list_count; 1966 int pin_format_offset = 0; 1967 int i, j; 1968 1969 /* set number of pins, offset of pin format and format list to search based on pin type */ 1970 if (pin_type == SOF_PIN_TYPE_INPUT) { 1971 num_pins = swidget->num_input_pins; 1972 format_list_to_search = available_fmt->input_pin_fmts; 1973 format_list_count = available_fmt->num_input_formats; 1974 } else { 1975 num_pins = swidget->num_output_pins; 1976 pin_format_offset = swidget->num_input_pins; 1977 format_list_to_search = available_fmt->output_pin_fmts; 1978 format_list_count = available_fmt->num_output_formats; 1979 } 1980 1981 for (i = pin_format_offset; i < num_pins + pin_format_offset; i++) { 1982 pin_format = &base_cfg_ext->pin_formats[i]; 1983 1984 /* Pin 0 audio formats are derived from the base config input/output format */ 1985 if (i == pin_format_offset) { 1986 if (pin_type == SOF_PIN_TYPE_INPUT) { 1987 pin_format->buffer_size = process->base_config.ibs; 1988 pin_format->audio_fmt = process->base_config.audio_fmt; 1989 } else { 1990 pin_format->buffer_size = process->base_config.obs; 1991 pin_format->audio_fmt = process->output_format; 1992 } 1993 continue; 1994 } 1995 1996 /* 1997 * For all other pins, find the pin formats from those set in topology. If there 1998 * is more than one format specified for a pin, this will pick the first available 1999 * one. 2000 */ 2001 for (j = 0; j < format_list_count; j++) { 2002 struct sof_ipc4_pin_format *pin_format_item = &format_list_to_search[j]; 2003 2004 if (pin_format_item->pin_index == i - pin_format_offset) { 2005 *pin_format = *pin_format_item; 2006 break; 2007 } 2008 } 2009 2010 if (j == format_list_count) { 2011 dev_err(scomp->dev, "%s pin %d format not found for %s\n", 2012 (pin_type == SOF_PIN_TYPE_INPUT) ? "input" : "output", 2013 i - pin_format_offset, swidget->widget->name); 2014 return -EINVAL; 2015 } 2016 } 2017 2018 return 0; 2019 } 2020 2021 static int sof_ipc4_process_add_base_cfg_extn(struct snd_sof_widget *swidget) 2022 { 2023 int ret, i; 2024 2025 /* copy input and output pin formats */ 2026 for (i = 0; i <= SOF_PIN_TYPE_OUTPUT; i++) { 2027 ret = sof_ipc4_process_set_pin_formats(swidget, i); 2028 if (ret < 0) 2029 return ret; 2030 } 2031 2032 return 0; 2033 } 2034 2035 static int sof_ipc4_prepare_process_module(struct snd_sof_widget *swidget, 2036 struct snd_pcm_hw_params *fe_params, 2037 struct snd_sof_platform_stream_params *platform_params, 2038 struct snd_pcm_hw_params *pipeline_params, int dir) 2039 { 2040 struct snd_soc_component *scomp = swidget->scomp; 2041 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2042 struct sof_ipc4_process *process = swidget->private; 2043 struct sof_ipc4_available_audio_format *available_fmt = &process->available_fmt; 2044 struct sof_ipc4_audio_format *in_fmt; 2045 u32 out_ref_rate, out_ref_channels, out_ref_valid_bits; 2046 void *cfg = process->ipc_config_data; 2047 int output_fmt_index; 2048 int ret; 2049 2050 ret = sof_ipc4_init_input_audio_fmt(sdev, swidget, &process->base_config, 2051 pipeline_params, available_fmt); 2052 if (ret < 0) 2053 return ret; 2054 2055 in_fmt = &available_fmt->input_pin_fmts[ret].audio_fmt; 2056 out_ref_rate = in_fmt->sampling_frequency; 2057 out_ref_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(in_fmt->fmt_cfg); 2058 out_ref_valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(in_fmt->fmt_cfg); 2059 2060 output_fmt_index = sof_ipc4_init_output_audio_fmt(sdev, &process->base_config, 2061 available_fmt, out_ref_rate, 2062 out_ref_channels, out_ref_valid_bits); 2063 if (output_fmt_index < 0 && available_fmt->num_output_formats) { 2064 dev_err(sdev->dev, "Failed to initialize output format for %s", 2065 swidget->widget->name); 2066 return output_fmt_index; 2067 } 2068 2069 /* copy Pin 0 output format */ 2070 if (available_fmt->num_output_formats && 2071 output_fmt_index < available_fmt->num_output_formats && 2072 !available_fmt->output_pin_fmts[output_fmt_index].pin_index) { 2073 memcpy(&process->output_format, 2074 &available_fmt->output_pin_fmts[output_fmt_index].audio_fmt, 2075 sizeof(struct sof_ipc4_audio_format)); 2076 2077 /* modify the pipeline params with the pin 0 output format */ 2078 ret = sof_ipc4_update_hw_params(sdev, pipeline_params, &process->output_format); 2079 if (ret) 2080 return ret; 2081 } 2082 2083 /* update pipeline memory usage */ 2084 sof_ipc4_update_resource_usage(sdev, swidget, &process->base_config); 2085 2086 /* ipc_config_data is composed of the base_config followed by an optional extension */ 2087 memcpy(cfg, &process->base_config, sizeof(struct sof_ipc4_base_module_cfg)); 2088 cfg += sizeof(struct sof_ipc4_base_module_cfg); 2089 2090 if (process->init_config == SOF_IPC4_MODULE_INIT_CONFIG_TYPE_BASE_CFG_WITH_EXT) { 2091 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext = process->base_config_ext; 2092 2093 ret = sof_ipc4_process_add_base_cfg_extn(swidget); 2094 if (ret < 0) 2095 return ret; 2096 2097 memcpy(cfg, base_cfg_ext, process->base_config_ext_size); 2098 } 2099 2100 return 0; 2101 } 2102 2103 static int sof_ipc4_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 2104 { 2105 struct sof_ipc4_control_data *control_data; 2106 struct sof_ipc4_msg *msg; 2107 int i; 2108 2109 scontrol->size = struct_size(control_data, chanv, scontrol->num_channels); 2110 2111 /* scontrol->ipc_control_data will be freed in sof_control_unload */ 2112 scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL); 2113 if (!scontrol->ipc_control_data) 2114 return -ENOMEM; 2115 2116 control_data = scontrol->ipc_control_data; 2117 control_data->index = scontrol->index; 2118 2119 msg = &control_data->msg; 2120 msg->primary = SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_LARGE_CONFIG_SET); 2121 msg->primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 2122 msg->primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 2123 2124 msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_GAIN_PARAM_ID); 2125 2126 /* set default volume values to 0dB in control */ 2127 for (i = 0; i < scontrol->num_channels; i++) { 2128 control_data->chanv[i].channel = i; 2129 control_data->chanv[i].value = SOF_IPC4_VOL_ZERO_DB; 2130 } 2131 2132 return 0; 2133 } 2134 2135 static int sof_ipc4_control_load_bytes(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 2136 { 2137 struct sof_ipc4_control_data *control_data; 2138 struct sof_ipc4_msg *msg; 2139 int ret; 2140 2141 if (scontrol->max_size < (sizeof(*control_data) + sizeof(struct sof_abi_hdr))) { 2142 dev_err(sdev->dev, "insufficient size for a bytes control %s: %zu.\n", 2143 scontrol->name, scontrol->max_size); 2144 return -EINVAL; 2145 } 2146 2147 if (scontrol->priv_size > scontrol->max_size - sizeof(*control_data)) { 2148 dev_err(sdev->dev, "scontrol %s bytes data size %zu exceeds max %zu.\n", 2149 scontrol->name, scontrol->priv_size, 2150 scontrol->max_size - sizeof(*control_data)); 2151 return -EINVAL; 2152 } 2153 2154 scontrol->size = sizeof(struct sof_ipc4_control_data) + scontrol->priv_size; 2155 2156 scontrol->ipc_control_data = kzalloc(scontrol->max_size, GFP_KERNEL); 2157 if (!scontrol->ipc_control_data) 2158 return -ENOMEM; 2159 2160 control_data = scontrol->ipc_control_data; 2161 control_data->index = scontrol->index; 2162 if (scontrol->priv_size > 0) { 2163 memcpy(control_data->data, scontrol->priv, scontrol->priv_size); 2164 kfree(scontrol->priv); 2165 scontrol->priv = NULL; 2166 2167 if (control_data->data->magic != SOF_IPC4_ABI_MAGIC) { 2168 dev_err(sdev->dev, "Wrong ABI magic (%#x) for control: %s\n", 2169 control_data->data->magic, scontrol->name); 2170 ret = -EINVAL; 2171 goto err; 2172 } 2173 2174 /* TODO: check the ABI version */ 2175 2176 if (control_data->data->size + sizeof(struct sof_abi_hdr) != 2177 scontrol->priv_size) { 2178 dev_err(sdev->dev, "Control %s conflict in bytes %zu vs. priv size %zu.\n", 2179 scontrol->name, 2180 control_data->data->size + sizeof(struct sof_abi_hdr), 2181 scontrol->priv_size); 2182 ret = -EINVAL; 2183 goto err; 2184 } 2185 } 2186 2187 msg = &control_data->msg; 2188 msg->primary = SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_LARGE_CONFIG_SET); 2189 msg->primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 2190 msg->primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 2191 2192 return 0; 2193 2194 err: 2195 kfree(scontrol->ipc_control_data); 2196 scontrol->ipc_control_data = NULL; 2197 return ret; 2198 } 2199 2200 static int sof_ipc4_control_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 2201 { 2202 switch (scontrol->info_type) { 2203 case SND_SOC_TPLG_CTL_VOLSW: 2204 case SND_SOC_TPLG_CTL_VOLSW_SX: 2205 case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 2206 return sof_ipc4_control_load_volume(sdev, scontrol); 2207 case SND_SOC_TPLG_CTL_BYTES: 2208 return sof_ipc4_control_load_bytes(sdev, scontrol); 2209 default: 2210 break; 2211 } 2212 2213 return 0; 2214 } 2215 2216 static int sof_ipc4_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 2217 { 2218 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; 2219 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 2220 struct sof_ipc4_pipeline *pipeline; 2221 struct sof_ipc4_msg *msg; 2222 void *ipc_data = NULL; 2223 u32 ipc_size = 0; 2224 int ret; 2225 2226 switch (swidget->id) { 2227 case snd_soc_dapm_scheduler: 2228 pipeline = swidget->private; 2229 2230 if (pipeline->use_chain_dma) { 2231 dev_warn(sdev->dev, "use_chain_dma set for scheduler %s", 2232 swidget->widget->name); 2233 return 0; 2234 } 2235 2236 dev_dbg(sdev->dev, "pipeline: %d memory pages: %d\n", swidget->pipeline_id, 2237 pipeline->mem_usage); 2238 2239 msg = &pipeline->msg; 2240 msg->primary |= pipeline->mem_usage; 2241 2242 swidget->instance_id = ida_alloc_max(&pipeline_ida, ipc4_data->max_num_pipelines, 2243 GFP_KERNEL); 2244 if (swidget->instance_id < 0) { 2245 dev_err(sdev->dev, "failed to assign pipeline id for %s: %d\n", 2246 swidget->widget->name, swidget->instance_id); 2247 return swidget->instance_id; 2248 } 2249 msg->primary &= ~SOF_IPC4_GLB_PIPE_INSTANCE_MASK; 2250 msg->primary |= SOF_IPC4_GLB_PIPE_INSTANCE_ID(swidget->instance_id); 2251 break; 2252 case snd_soc_dapm_aif_in: 2253 case snd_soc_dapm_aif_out: 2254 case snd_soc_dapm_buffer: 2255 { 2256 struct sof_ipc4_copier *ipc4_copier = swidget->private; 2257 2258 pipeline = pipe_widget->private; 2259 if (pipeline->use_chain_dma) 2260 return 0; 2261 2262 ipc_size = ipc4_copier->ipc_config_size; 2263 ipc_data = ipc4_copier->ipc_config_data; 2264 2265 msg = &ipc4_copier->msg; 2266 break; 2267 } 2268 case snd_soc_dapm_dai_in: 2269 case snd_soc_dapm_dai_out: 2270 { 2271 struct snd_sof_dai *dai = swidget->private; 2272 struct sof_ipc4_copier *ipc4_copier = dai->private; 2273 2274 pipeline = pipe_widget->private; 2275 if (pipeline->use_chain_dma) 2276 return 0; 2277 2278 ipc_size = ipc4_copier->ipc_config_size; 2279 ipc_data = ipc4_copier->ipc_config_data; 2280 2281 msg = &ipc4_copier->msg; 2282 break; 2283 } 2284 case snd_soc_dapm_pga: 2285 { 2286 struct sof_ipc4_gain *gain = swidget->private; 2287 2288 ipc_size = sizeof(gain->data); 2289 ipc_data = &gain->data; 2290 2291 msg = &gain->msg; 2292 break; 2293 } 2294 case snd_soc_dapm_mixer: 2295 { 2296 struct sof_ipc4_mixer *mixer = swidget->private; 2297 2298 ipc_size = sizeof(mixer->base_config); 2299 ipc_data = &mixer->base_config; 2300 2301 msg = &mixer->msg; 2302 break; 2303 } 2304 case snd_soc_dapm_src: 2305 { 2306 struct sof_ipc4_src *src = swidget->private; 2307 2308 ipc_size = sizeof(src->data); 2309 ipc_data = &src->data; 2310 2311 msg = &src->msg; 2312 break; 2313 } 2314 case snd_soc_dapm_effect: 2315 { 2316 struct sof_ipc4_process *process = swidget->private; 2317 2318 if (!process->ipc_config_size) { 2319 dev_err(sdev->dev, "module %s has no config data!\n", 2320 swidget->widget->name); 2321 return -EINVAL; 2322 } 2323 2324 ipc_size = process->ipc_config_size; 2325 ipc_data = process->ipc_config_data; 2326 2327 msg = &process->msg; 2328 break; 2329 } 2330 default: 2331 dev_err(sdev->dev, "widget type %d not supported", swidget->id); 2332 return -EINVAL; 2333 } 2334 2335 if (swidget->id != snd_soc_dapm_scheduler) { 2336 ret = sof_ipc4_widget_assign_instance_id(sdev, swidget); 2337 if (ret < 0) { 2338 dev_err(sdev->dev, "failed to assign instance id for %s\n", 2339 swidget->widget->name); 2340 return ret; 2341 } 2342 2343 msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK; 2344 msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id); 2345 2346 msg->extension &= ~SOF_IPC4_MOD_EXT_PARAM_SIZE_MASK; 2347 msg->extension |= ipc_size >> 2; 2348 2349 msg->extension &= ~SOF_IPC4_MOD_EXT_PPL_ID_MASK; 2350 msg->extension |= SOF_IPC4_MOD_EXT_PPL_ID(pipe_widget->instance_id); 2351 } 2352 dev_dbg(sdev->dev, "Create widget %s instance %d - pipe %d - core %d\n", 2353 swidget->widget->name, swidget->instance_id, swidget->pipeline_id, swidget->core); 2354 2355 msg->data_size = ipc_size; 2356 msg->data_ptr = ipc_data; 2357 2358 ret = sof_ipc_tx_message_no_reply(sdev->ipc, msg, ipc_size); 2359 if (ret < 0) { 2360 dev_err(sdev->dev, "failed to create module %s\n", swidget->widget->name); 2361 2362 if (swidget->id != snd_soc_dapm_scheduler) { 2363 struct sof_ipc4_fw_module *fw_module = swidget->module_info; 2364 2365 ida_free(&fw_module->m_ida, swidget->instance_id); 2366 } else { 2367 ida_free(&pipeline_ida, swidget->instance_id); 2368 } 2369 } 2370 2371 return ret; 2372 } 2373 2374 static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 2375 { 2376 struct sof_ipc4_fw_module *fw_module = swidget->module_info; 2377 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 2378 int ret = 0; 2379 2380 mutex_lock(&ipc4_data->pipeline_state_mutex); 2381 2382 /* freeing a pipeline frees all the widgets associated with it */ 2383 if (swidget->id == snd_soc_dapm_scheduler) { 2384 struct sof_ipc4_pipeline *pipeline = swidget->private; 2385 struct sof_ipc4_msg msg = {{ 0 }}; 2386 u32 header; 2387 2388 if (pipeline->use_chain_dma) { 2389 dev_warn(sdev->dev, "use_chain_dma set for scheduler %s", 2390 swidget->widget->name); 2391 mutex_unlock(&ipc4_data->pipeline_state_mutex); 2392 return 0; 2393 } 2394 2395 header = SOF_IPC4_GLB_PIPE_INSTANCE_ID(swidget->instance_id); 2396 header |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_DELETE_PIPELINE); 2397 header |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 2398 header |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG); 2399 2400 msg.primary = header; 2401 2402 ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0); 2403 if (ret < 0) 2404 dev_err(sdev->dev, "failed to free pipeline widget %s\n", 2405 swidget->widget->name); 2406 2407 pipeline->mem_usage = 0; 2408 pipeline->state = SOF_IPC4_PIPE_UNINITIALIZED; 2409 ida_free(&pipeline_ida, swidget->instance_id); 2410 swidget->instance_id = -EINVAL; 2411 } else { 2412 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; 2413 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 2414 2415 if (!pipeline->use_chain_dma) 2416 ida_free(&fw_module->m_ida, swidget->instance_id); 2417 } 2418 2419 mutex_unlock(&ipc4_data->pipeline_state_mutex); 2420 2421 return ret; 2422 } 2423 2424 static int sof_ipc4_get_queue_id(struct snd_sof_widget *src_widget, 2425 struct snd_sof_widget *sink_widget, bool pin_type) 2426 { 2427 struct snd_sof_widget *current_swidget; 2428 struct snd_soc_component *scomp; 2429 struct ida *queue_ida; 2430 const char *buddy_name; 2431 char **pin_binding; 2432 u32 num_pins; 2433 int i; 2434 2435 if (pin_type == SOF_PIN_TYPE_OUTPUT) { 2436 current_swidget = src_widget; 2437 pin_binding = src_widget->output_pin_binding; 2438 queue_ida = &src_widget->output_queue_ida; 2439 num_pins = src_widget->num_output_pins; 2440 buddy_name = sink_widget->widget->name; 2441 } else { 2442 current_swidget = sink_widget; 2443 pin_binding = sink_widget->input_pin_binding; 2444 queue_ida = &sink_widget->input_queue_ida; 2445 num_pins = sink_widget->num_input_pins; 2446 buddy_name = src_widget->widget->name; 2447 } 2448 2449 scomp = current_swidget->scomp; 2450 2451 if (num_pins < 1) { 2452 dev_err(scomp->dev, "invalid %s num_pins: %d for queue allocation for %s\n", 2453 (pin_type == SOF_PIN_TYPE_OUTPUT ? "output" : "input"), 2454 num_pins, current_swidget->widget->name); 2455 return -EINVAL; 2456 } 2457 2458 /* If there is only one input/output pin, queue id must be 0 */ 2459 if (num_pins == 1) 2460 return 0; 2461 2462 /* Allocate queue ID from pin binding array if it is defined in topology. */ 2463 if (pin_binding) { 2464 for (i = 0; i < num_pins; i++) { 2465 if (!strcmp(pin_binding[i], buddy_name)) 2466 return i; 2467 } 2468 /* 2469 * Fail if no queue ID found from pin binding array, so that we don't 2470 * mixed use pin binding array and ida for queue ID allocation. 2471 */ 2472 dev_err(scomp->dev, "no %s queue id found from pin binding array for %s\n", 2473 (pin_type == SOF_PIN_TYPE_OUTPUT ? "output" : "input"), 2474 current_swidget->widget->name); 2475 return -EINVAL; 2476 } 2477 2478 /* If no pin binding array specified in topology, use ida to allocate one */ 2479 return ida_alloc_max(queue_ida, num_pins, GFP_KERNEL); 2480 } 2481 2482 static void sof_ipc4_put_queue_id(struct snd_sof_widget *swidget, int queue_id, 2483 bool pin_type) 2484 { 2485 struct ida *queue_ida; 2486 char **pin_binding; 2487 int num_pins; 2488 2489 if (pin_type == SOF_PIN_TYPE_OUTPUT) { 2490 pin_binding = swidget->output_pin_binding; 2491 queue_ida = &swidget->output_queue_ida; 2492 num_pins = swidget->num_output_pins; 2493 } else { 2494 pin_binding = swidget->input_pin_binding; 2495 queue_ida = &swidget->input_queue_ida; 2496 num_pins = swidget->num_input_pins; 2497 } 2498 2499 /* Nothing to free if queue ID is not allocated with ida. */ 2500 if (num_pins == 1 || pin_binding) 2501 return; 2502 2503 ida_free(queue_ida, queue_id); 2504 } 2505 2506 static int sof_ipc4_set_copier_sink_format(struct snd_sof_dev *sdev, 2507 struct snd_sof_widget *src_widget, 2508 struct snd_sof_widget *sink_widget, 2509 int sink_id) 2510 { 2511 struct sof_ipc4_copier_config_set_sink_format format; 2512 const struct sof_ipc_ops *iops = sdev->ipc->ops; 2513 struct sof_ipc4_base_module_cfg *src_config; 2514 const struct sof_ipc4_audio_format *pin_fmt; 2515 struct sof_ipc4_fw_module *fw_module; 2516 struct sof_ipc4_msg msg = {{ 0 }}; 2517 2518 dev_dbg(sdev->dev, "%s set copier sink %d format\n", 2519 src_widget->widget->name, sink_id); 2520 2521 if (WIDGET_IS_DAI(src_widget->id)) { 2522 struct snd_sof_dai *dai = src_widget->private; 2523 2524 src_config = dai->private; 2525 } else { 2526 src_config = src_widget->private; 2527 } 2528 2529 fw_module = src_widget->module_info; 2530 2531 format.sink_id = sink_id; 2532 memcpy(&format.source_fmt, &src_config->audio_fmt, sizeof(format.source_fmt)); 2533 2534 pin_fmt = sof_ipc4_get_input_pin_audio_fmt(sink_widget, sink_id); 2535 if (!pin_fmt) { 2536 dev_err(sdev->dev, "Unable to get pin %d format for %s", 2537 sink_id, sink_widget->widget->name); 2538 return -EINVAL; 2539 } 2540 2541 memcpy(&format.sink_fmt, pin_fmt, sizeof(format.sink_fmt)); 2542 2543 msg.data_size = sizeof(format); 2544 msg.data_ptr = &format; 2545 2546 msg.primary = fw_module->man4_module_entry.id; 2547 msg.primary |= SOF_IPC4_MOD_INSTANCE(src_widget->instance_id); 2548 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 2549 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 2550 2551 msg.extension = 2552 SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_COPIER_MODULE_CFG_PARAM_SET_SINK_FORMAT); 2553 2554 return iops->set_get_data(sdev, &msg, msg.data_size, true); 2555 } 2556 2557 static int sof_ipc4_route_setup(struct snd_sof_dev *sdev, struct snd_sof_route *sroute) 2558 { 2559 struct snd_sof_widget *src_widget = sroute->src_widget; 2560 struct snd_sof_widget *sink_widget = sroute->sink_widget; 2561 struct snd_sof_widget *src_pipe_widget = src_widget->spipe->pipe_widget; 2562 struct snd_sof_widget *sink_pipe_widget = sink_widget->spipe->pipe_widget; 2563 struct sof_ipc4_fw_module *src_fw_module = src_widget->module_info; 2564 struct sof_ipc4_fw_module *sink_fw_module = sink_widget->module_info; 2565 struct sof_ipc4_pipeline *src_pipeline = src_pipe_widget->private; 2566 struct sof_ipc4_pipeline *sink_pipeline = sink_pipe_widget->private; 2567 struct sof_ipc4_msg msg = {{ 0 }}; 2568 u32 header, extension; 2569 int ret; 2570 2571 /* no route set up if chain DMA is used */ 2572 if (src_pipeline->use_chain_dma || sink_pipeline->use_chain_dma) { 2573 if (!src_pipeline->use_chain_dma || !sink_pipeline->use_chain_dma) { 2574 dev_err(sdev->dev, 2575 "use_chain_dma must be set for both src %s and sink %s pipelines\n", 2576 src_widget->widget->name, sink_widget->widget->name); 2577 return -EINVAL; 2578 } 2579 return 0; 2580 } 2581 2582 if (!src_fw_module || !sink_fw_module) { 2583 dev_err(sdev->dev, 2584 "cannot bind %s -> %s, no firmware module for: %s%s\n", 2585 src_widget->widget->name, sink_widget->widget->name, 2586 src_fw_module ? "" : " source", 2587 sink_fw_module ? "" : " sink"); 2588 2589 return -ENODEV; 2590 } 2591 2592 sroute->src_queue_id = sof_ipc4_get_queue_id(src_widget, sink_widget, 2593 SOF_PIN_TYPE_OUTPUT); 2594 if (sroute->src_queue_id < 0) { 2595 dev_err(sdev->dev, "failed to get queue ID for source widget: %s\n", 2596 src_widget->widget->name); 2597 return sroute->src_queue_id; 2598 } 2599 2600 sroute->dst_queue_id = sof_ipc4_get_queue_id(src_widget, sink_widget, 2601 SOF_PIN_TYPE_INPUT); 2602 if (sroute->dst_queue_id < 0) { 2603 dev_err(sdev->dev, "failed to get queue ID for sink widget: %s\n", 2604 sink_widget->widget->name); 2605 sof_ipc4_put_queue_id(src_widget, sroute->src_queue_id, 2606 SOF_PIN_TYPE_OUTPUT); 2607 return sroute->dst_queue_id; 2608 } 2609 2610 /* Pin 0 format is already set during copier module init */ 2611 if (sroute->src_queue_id > 0 && WIDGET_IS_COPIER(src_widget->id)) { 2612 ret = sof_ipc4_set_copier_sink_format(sdev, src_widget, sink_widget, 2613 sroute->src_queue_id); 2614 if (ret < 0) { 2615 dev_err(sdev->dev, "failed to set sink format for %s source queue ID %d\n", 2616 src_widget->widget->name, sroute->src_queue_id); 2617 goto out; 2618 } 2619 } 2620 2621 dev_dbg(sdev->dev, "bind %s:%d -> %s:%d\n", 2622 src_widget->widget->name, sroute->src_queue_id, 2623 sink_widget->widget->name, sroute->dst_queue_id); 2624 2625 header = src_fw_module->man4_module_entry.id; 2626 header |= SOF_IPC4_MOD_INSTANCE(src_widget->instance_id); 2627 header |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_BIND); 2628 header |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 2629 header |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 2630 2631 extension = sink_fw_module->man4_module_entry.id; 2632 extension |= SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(sink_widget->instance_id); 2633 extension |= SOF_IPC4_MOD_EXT_DST_MOD_QUEUE_ID(sroute->dst_queue_id); 2634 extension |= SOF_IPC4_MOD_EXT_SRC_MOD_QUEUE_ID(sroute->src_queue_id); 2635 2636 msg.primary = header; 2637 msg.extension = extension; 2638 2639 ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0); 2640 if (ret < 0) { 2641 dev_err(sdev->dev, "failed to bind modules %s:%d -> %s:%d\n", 2642 src_widget->widget->name, sroute->src_queue_id, 2643 sink_widget->widget->name, sroute->dst_queue_id); 2644 goto out; 2645 } 2646 2647 return ret; 2648 2649 out: 2650 sof_ipc4_put_queue_id(src_widget, sroute->src_queue_id, SOF_PIN_TYPE_OUTPUT); 2651 sof_ipc4_put_queue_id(sink_widget, sroute->dst_queue_id, SOF_PIN_TYPE_INPUT); 2652 return ret; 2653 } 2654 2655 static int sof_ipc4_route_free(struct snd_sof_dev *sdev, struct snd_sof_route *sroute) 2656 { 2657 struct snd_sof_widget *src_widget = sroute->src_widget; 2658 struct snd_sof_widget *sink_widget = sroute->sink_widget; 2659 struct sof_ipc4_fw_module *src_fw_module = src_widget->module_info; 2660 struct sof_ipc4_fw_module *sink_fw_module = sink_widget->module_info; 2661 struct sof_ipc4_msg msg = {{ 0 }}; 2662 struct snd_sof_widget *src_pipe_widget = src_widget->spipe->pipe_widget; 2663 struct snd_sof_widget *sink_pipe_widget = sink_widget->spipe->pipe_widget; 2664 struct sof_ipc4_pipeline *src_pipeline = src_pipe_widget->private; 2665 struct sof_ipc4_pipeline *sink_pipeline = sink_pipe_widget->private; 2666 u32 header, extension; 2667 int ret = 0; 2668 2669 /* no route is set up if chain DMA is used */ 2670 if (src_pipeline->use_chain_dma || sink_pipeline->use_chain_dma) 2671 return 0; 2672 2673 dev_dbg(sdev->dev, "unbind modules %s:%d -> %s:%d\n", 2674 src_widget->widget->name, sroute->src_queue_id, 2675 sink_widget->widget->name, sroute->dst_queue_id); 2676 2677 /* 2678 * routes belonging to the same pipeline will be disconnected by the FW when the pipeline 2679 * is freed. So avoid sending this IPC which will be ignored by the FW anyway. 2680 */ 2681 if (src_widget->spipe->pipe_widget == sink_widget->spipe->pipe_widget) 2682 goto out; 2683 2684 header = src_fw_module->man4_module_entry.id; 2685 header |= SOF_IPC4_MOD_INSTANCE(src_widget->instance_id); 2686 header |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_UNBIND); 2687 header |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 2688 header |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 2689 2690 extension = sink_fw_module->man4_module_entry.id; 2691 extension |= SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(sink_widget->instance_id); 2692 extension |= SOF_IPC4_MOD_EXT_DST_MOD_QUEUE_ID(sroute->dst_queue_id); 2693 extension |= SOF_IPC4_MOD_EXT_SRC_MOD_QUEUE_ID(sroute->src_queue_id); 2694 2695 msg.primary = header; 2696 msg.extension = extension; 2697 2698 ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0); 2699 if (ret < 0) 2700 dev_err(sdev->dev, "failed to unbind modules %s:%d -> %s:%d\n", 2701 src_widget->widget->name, sroute->src_queue_id, 2702 sink_widget->widget->name, sroute->dst_queue_id); 2703 out: 2704 sof_ipc4_put_queue_id(sink_widget, sroute->dst_queue_id, SOF_PIN_TYPE_INPUT); 2705 sof_ipc4_put_queue_id(src_widget, sroute->src_queue_id, SOF_PIN_TYPE_OUTPUT); 2706 2707 return ret; 2708 } 2709 2710 static int sof_ipc4_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget, 2711 unsigned int flags, struct snd_sof_dai_config_data *data) 2712 { 2713 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; 2714 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 2715 struct snd_sof_dai *dai = swidget->private; 2716 struct sof_ipc4_gtw_attributes *gtw_attr; 2717 struct sof_ipc4_copier_data *copier_data; 2718 struct sof_ipc4_copier *ipc4_copier; 2719 2720 if (!dai || !dai->private) { 2721 dev_err(sdev->dev, "Invalid DAI or DAI private data for %s\n", 2722 swidget->widget->name); 2723 return -EINVAL; 2724 } 2725 2726 ipc4_copier = (struct sof_ipc4_copier *)dai->private; 2727 copier_data = &ipc4_copier->data; 2728 2729 if (!data) 2730 return 0; 2731 2732 switch (ipc4_copier->dai_type) { 2733 case SOF_DAI_INTEL_HDA: 2734 if (pipeline->use_chain_dma) { 2735 pipeline->msg.primary &= ~SOF_IPC4_GLB_CHAIN_DMA_LINK_ID_MASK; 2736 pipeline->msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_LINK_ID(data->dai_data); 2737 break; 2738 } 2739 gtw_attr = ipc4_copier->gtw_attr; 2740 gtw_attr->lp_buffer_alloc = pipeline->lp_mode; 2741 fallthrough; 2742 case SOF_DAI_INTEL_ALH: 2743 /* 2744 * Do not clear the node ID when this op is invoked with 2745 * SOF_DAI_CONFIG_FLAGS_HW_FREE. It is needed to free the group_ida during 2746 * unprepare. 2747 */ 2748 if (flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS) { 2749 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK; 2750 copier_data->gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX(data->dai_data); 2751 } 2752 break; 2753 case SOF_DAI_INTEL_DMIC: 2754 case SOF_DAI_INTEL_SSP: 2755 /* nothing to do for SSP/DMIC */ 2756 break; 2757 default: 2758 dev_err(sdev->dev, "%s: unsupported dai type %d\n", __func__, 2759 ipc4_copier->dai_type); 2760 return -EINVAL; 2761 } 2762 2763 return 0; 2764 } 2765 2766 static int sof_ipc4_parse_manifest(struct snd_soc_component *scomp, int index, 2767 struct snd_soc_tplg_manifest *man) 2768 { 2769 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2770 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 2771 struct sof_manifest_tlv *manifest_tlv; 2772 struct sof_manifest *manifest; 2773 u32 size = le32_to_cpu(man->priv.size); 2774 u8 *man_ptr = man->priv.data; 2775 u32 len_check; 2776 int i; 2777 2778 if (!size || size < SOF_IPC4_TPLG_ABI_SIZE) { 2779 dev_err(scomp->dev, "%s: Invalid topology ABI size: %u\n", 2780 __func__, size); 2781 return -EINVAL; 2782 } 2783 2784 manifest = (struct sof_manifest *)man_ptr; 2785 2786 dev_info(scomp->dev, 2787 "Topology: ABI %d:%d:%d Kernel ABI %u:%u:%u\n", 2788 le16_to_cpu(manifest->abi_major), le16_to_cpu(manifest->abi_minor), 2789 le16_to_cpu(manifest->abi_patch), 2790 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH); 2791 2792 /* TODO: Add ABI compatibility check */ 2793 2794 /* no more data after the ABI version */ 2795 if (size <= SOF_IPC4_TPLG_ABI_SIZE) 2796 return 0; 2797 2798 manifest_tlv = manifest->items; 2799 len_check = sizeof(struct sof_manifest); 2800 for (i = 0; i < le16_to_cpu(manifest->count); i++) { 2801 len_check += sizeof(struct sof_manifest_tlv) + le32_to_cpu(manifest_tlv->size); 2802 if (len_check > size) 2803 return -EINVAL; 2804 2805 switch (le32_to_cpu(manifest_tlv->type)) { 2806 case SOF_MANIFEST_DATA_TYPE_NHLT: 2807 /* no NHLT in BIOS, so use the one from topology manifest */ 2808 if (ipc4_data->nhlt) 2809 break; 2810 ipc4_data->nhlt = devm_kmemdup(sdev->dev, manifest_tlv->data, 2811 le32_to_cpu(manifest_tlv->size), GFP_KERNEL); 2812 if (!ipc4_data->nhlt) 2813 return -ENOMEM; 2814 break; 2815 default: 2816 dev_warn(scomp->dev, "Skipping unknown manifest data type %d\n", 2817 manifest_tlv->type); 2818 break; 2819 } 2820 man_ptr += sizeof(struct sof_manifest_tlv) + le32_to_cpu(manifest_tlv->size); 2821 manifest_tlv = (struct sof_manifest_tlv *)man_ptr; 2822 } 2823 2824 return 0; 2825 } 2826 2827 static int sof_ipc4_dai_get_clk(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type) 2828 { 2829 struct sof_ipc4_copier *ipc4_copier = dai->private; 2830 struct snd_soc_tplg_hw_config *hw_config; 2831 struct snd_sof_dai_link *slink; 2832 bool dai_link_found = false; 2833 bool hw_cfg_found = false; 2834 int i; 2835 2836 if (!ipc4_copier) 2837 return 0; 2838 2839 list_for_each_entry(slink, &sdev->dai_link_list, list) { 2840 if (!strcmp(slink->link->name, dai->name)) { 2841 dai_link_found = true; 2842 break; 2843 } 2844 } 2845 2846 if (!dai_link_found) { 2847 dev_err(sdev->dev, "no DAI link found for DAI %s\n", dai->name); 2848 return -EINVAL; 2849 } 2850 2851 for (i = 0; i < slink->num_hw_configs; i++) { 2852 hw_config = &slink->hw_configs[i]; 2853 if (dai->current_config == le32_to_cpu(hw_config->id)) { 2854 hw_cfg_found = true; 2855 break; 2856 } 2857 } 2858 2859 if (!hw_cfg_found) { 2860 dev_err(sdev->dev, "no matching hw_config found for DAI %s\n", dai->name); 2861 return -EINVAL; 2862 } 2863 2864 switch (ipc4_copier->dai_type) { 2865 case SOF_DAI_INTEL_SSP: 2866 switch (clk_type) { 2867 case SOF_DAI_CLK_INTEL_SSP_MCLK: 2868 return le32_to_cpu(hw_config->mclk_rate); 2869 case SOF_DAI_CLK_INTEL_SSP_BCLK: 2870 return le32_to_cpu(hw_config->bclk_rate); 2871 default: 2872 dev_err(sdev->dev, "Invalid clk type for SSP %d\n", clk_type); 2873 break; 2874 } 2875 break; 2876 default: 2877 dev_err(sdev->dev, "DAI type %d not supported yet!\n", ipc4_copier->dai_type); 2878 break; 2879 } 2880 2881 return -EINVAL; 2882 } 2883 2884 static int sof_ipc4_tear_down_all_pipelines(struct snd_sof_dev *sdev, bool verify) 2885 { 2886 struct snd_sof_pcm *spcm; 2887 int dir, ret; 2888 2889 /* 2890 * This function is called during system suspend, we need to make sure 2891 * that all streams have been freed up. 2892 * Freeing might have been skipped when xrun happened just at the start 2893 * of the suspend and it sent a SNDRV_PCM_TRIGGER_STOP to the active 2894 * stream. This will call sof_pcm_stream_free() with 2895 * free_widget_list = false which will leave the kernel and firmware out 2896 * of sync during suspend/resume. 2897 * 2898 * This will also make sure that paused streams handled correctly. 2899 */ 2900 list_for_each_entry(spcm, &sdev->pcm_list, list) { 2901 for_each_pcm_streams(dir) { 2902 struct snd_pcm_substream *substream = spcm->stream[dir].substream; 2903 2904 if (!substream || !substream->runtime || spcm->stream[dir].suspend_ignored) 2905 continue; 2906 2907 if (spcm->stream[dir].list) { 2908 ret = sof_pcm_stream_free(sdev, substream, spcm, dir, true); 2909 if (ret < 0) 2910 return ret; 2911 } 2912 } 2913 } 2914 return 0; 2915 } 2916 2917 static int sof_ipc4_link_setup(struct snd_sof_dev *sdev, struct snd_soc_dai_link *link) 2918 { 2919 if (link->no_pcm) 2920 return 0; 2921 2922 /* 2923 * set default trigger order for all links. Exceptions to 2924 * the rule will be handled in sof_pcm_dai_link_fixup() 2925 * For playback, the sequence is the following: start BE, 2926 * start FE, stop FE, stop BE; for Capture the sequence is 2927 * inverted start FE, start BE, stop BE, stop FE 2928 */ 2929 link->trigger[SNDRV_PCM_STREAM_PLAYBACK] = SND_SOC_DPCM_TRIGGER_POST; 2930 link->trigger[SNDRV_PCM_STREAM_CAPTURE] = SND_SOC_DPCM_TRIGGER_PRE; 2931 2932 return 0; 2933 } 2934 2935 static enum sof_tokens common_copier_token_list[] = { 2936 SOF_COMP_TOKENS, 2937 SOF_AUDIO_FMT_NUM_TOKENS, 2938 SOF_IN_AUDIO_FORMAT_TOKENS, 2939 SOF_OUT_AUDIO_FORMAT_TOKENS, 2940 SOF_COPIER_DEEP_BUFFER_TOKENS, 2941 SOF_COPIER_TOKENS, 2942 SOF_COMP_EXT_TOKENS, 2943 }; 2944 2945 static enum sof_tokens pipeline_token_list[] = { 2946 SOF_SCHED_TOKENS, 2947 SOF_PIPELINE_TOKENS, 2948 }; 2949 2950 static enum sof_tokens dai_token_list[] = { 2951 SOF_COMP_TOKENS, 2952 SOF_AUDIO_FMT_NUM_TOKENS, 2953 SOF_IN_AUDIO_FORMAT_TOKENS, 2954 SOF_OUT_AUDIO_FORMAT_TOKENS, 2955 SOF_COPIER_TOKENS, 2956 SOF_DAI_TOKENS, 2957 SOF_COMP_EXT_TOKENS, 2958 }; 2959 2960 static enum sof_tokens pga_token_list[] = { 2961 SOF_COMP_TOKENS, 2962 SOF_GAIN_TOKENS, 2963 SOF_AUDIO_FMT_NUM_TOKENS, 2964 SOF_IN_AUDIO_FORMAT_TOKENS, 2965 SOF_OUT_AUDIO_FORMAT_TOKENS, 2966 SOF_COMP_EXT_TOKENS, 2967 }; 2968 2969 static enum sof_tokens mixer_token_list[] = { 2970 SOF_COMP_TOKENS, 2971 SOF_AUDIO_FMT_NUM_TOKENS, 2972 SOF_IN_AUDIO_FORMAT_TOKENS, 2973 SOF_OUT_AUDIO_FORMAT_TOKENS, 2974 SOF_COMP_EXT_TOKENS, 2975 }; 2976 2977 static enum sof_tokens src_token_list[] = { 2978 SOF_COMP_TOKENS, 2979 SOF_SRC_TOKENS, 2980 SOF_AUDIO_FMT_NUM_TOKENS, 2981 SOF_IN_AUDIO_FORMAT_TOKENS, 2982 SOF_OUT_AUDIO_FORMAT_TOKENS, 2983 SOF_COMP_EXT_TOKENS, 2984 }; 2985 2986 static enum sof_tokens process_token_list[] = { 2987 SOF_COMP_TOKENS, 2988 SOF_AUDIO_FMT_NUM_TOKENS, 2989 SOF_IN_AUDIO_FORMAT_TOKENS, 2990 SOF_OUT_AUDIO_FORMAT_TOKENS, 2991 SOF_COMP_EXT_TOKENS, 2992 }; 2993 2994 static const struct sof_ipc_tplg_widget_ops tplg_ipc4_widget_ops[SND_SOC_DAPM_TYPE_COUNT] = { 2995 [snd_soc_dapm_aif_in] = {sof_ipc4_widget_setup_pcm, sof_ipc4_widget_free_comp_pcm, 2996 common_copier_token_list, ARRAY_SIZE(common_copier_token_list), 2997 NULL, sof_ipc4_prepare_copier_module, 2998 sof_ipc4_unprepare_copier_module}, 2999 [snd_soc_dapm_aif_out] = {sof_ipc4_widget_setup_pcm, sof_ipc4_widget_free_comp_pcm, 3000 common_copier_token_list, ARRAY_SIZE(common_copier_token_list), 3001 NULL, sof_ipc4_prepare_copier_module, 3002 sof_ipc4_unprepare_copier_module}, 3003 [snd_soc_dapm_dai_in] = {sof_ipc4_widget_setup_comp_dai, sof_ipc4_widget_free_comp_dai, 3004 dai_token_list, ARRAY_SIZE(dai_token_list), NULL, 3005 sof_ipc4_prepare_copier_module, 3006 sof_ipc4_unprepare_copier_module}, 3007 [snd_soc_dapm_dai_out] = {sof_ipc4_widget_setup_comp_dai, sof_ipc4_widget_free_comp_dai, 3008 dai_token_list, ARRAY_SIZE(dai_token_list), NULL, 3009 sof_ipc4_prepare_copier_module, 3010 sof_ipc4_unprepare_copier_module}, 3011 [snd_soc_dapm_buffer] = {sof_ipc4_widget_setup_pcm, sof_ipc4_widget_free_comp_pcm, 3012 common_copier_token_list, ARRAY_SIZE(common_copier_token_list), 3013 NULL, sof_ipc4_prepare_copier_module, 3014 sof_ipc4_unprepare_copier_module}, 3015 [snd_soc_dapm_scheduler] = {sof_ipc4_widget_setup_comp_pipeline, 3016 sof_ipc4_widget_free_comp_pipeline, 3017 pipeline_token_list, ARRAY_SIZE(pipeline_token_list), NULL, 3018 NULL, NULL}, 3019 [snd_soc_dapm_pga] = {sof_ipc4_widget_setup_comp_pga, sof_ipc4_widget_free_comp_pga, 3020 pga_token_list, ARRAY_SIZE(pga_token_list), NULL, 3021 sof_ipc4_prepare_gain_module, 3022 NULL}, 3023 [snd_soc_dapm_mixer] = {sof_ipc4_widget_setup_comp_mixer, sof_ipc4_widget_free_comp_mixer, 3024 mixer_token_list, ARRAY_SIZE(mixer_token_list), 3025 NULL, sof_ipc4_prepare_mixer_module, 3026 NULL}, 3027 [snd_soc_dapm_src] = {sof_ipc4_widget_setup_comp_src, sof_ipc4_widget_free_comp_src, 3028 src_token_list, ARRAY_SIZE(src_token_list), 3029 NULL, sof_ipc4_prepare_src_module, 3030 NULL}, 3031 [snd_soc_dapm_effect] = {sof_ipc4_widget_setup_comp_process, 3032 sof_ipc4_widget_free_comp_process, 3033 process_token_list, ARRAY_SIZE(process_token_list), 3034 NULL, sof_ipc4_prepare_process_module, 3035 NULL}, 3036 }; 3037 3038 const struct sof_ipc_tplg_ops ipc4_tplg_ops = { 3039 .widget = tplg_ipc4_widget_ops, 3040 .token_list = ipc4_token_list, 3041 .control_setup = sof_ipc4_control_setup, 3042 .control = &tplg_ipc4_control_ops, 3043 .widget_setup = sof_ipc4_widget_setup, 3044 .widget_free = sof_ipc4_widget_free, 3045 .route_setup = sof_ipc4_route_setup, 3046 .route_free = sof_ipc4_route_free, 3047 .dai_config = sof_ipc4_dai_config, 3048 .parse_manifest = sof_ipc4_parse_manifest, 3049 .dai_get_clk = sof_ipc4_dai_get_clk, 3050 .tear_down_all_pipelines = sof_ipc4_tear_down_all_pipelines, 3051 .link_setup = sof_ipc4_link_setup, 3052 }; 3053