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