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