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