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