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