1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2022 Intel Corporation. All rights reserved.
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>
15 #include "sof-audio.h"
16 #include "ipc4-priv.h"
17 #include "ipc4-topology.h"
20 #define SOF_IPC4_GAIN_PARAM_ID 0
21 #define SOF_IPC4_TPLG_ABI_SIZE 6
22 #define SOF_IPC4_CHAIN_DMA_BUF_SIZE_MS 2
24 static DEFINE_IDA(alh_group_ida);
25 static DEFINE_IDA(pipeline_ida);
27 static const struct sof_topology_token ipc4_sched_tokens[] = {
28 {SOF_TKN_SCHED_LP_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
29 offsetof(struct sof_ipc4_pipeline, lp_mode)},
30 {SOF_TKN_SCHED_USE_CHAIN_DMA, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
31 offsetof(struct sof_ipc4_pipeline, use_chain_dma)},
32 {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
33 offsetof(struct sof_ipc4_pipeline, core_id)},
36 static const struct sof_topology_token pipeline_tokens[] = {
37 {SOF_TKN_SCHED_DYNAMIC_PIPELINE, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
38 offsetof(struct snd_sof_widget, dynamic_pipeline_widget)},
41 static const struct sof_topology_token ipc4_comp_tokens[] = {
42 {SOF_TKN_COMP_CPC, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
43 offsetof(struct sof_ipc4_base_module_cfg, cpc)},
44 {SOF_TKN_COMP_IS_PAGES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
45 offsetof(struct sof_ipc4_base_module_cfg, is_pages)},
48 static const struct sof_topology_token ipc4_in_audio_format_tokens[] = {
49 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
50 offsetof(struct sof_ipc4_pin_format, audio_fmt.sampling_frequency)},
51 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_BIT_DEPTH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
52 offsetof(struct sof_ipc4_pin_format, audio_fmt.bit_depth)},
53 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_CH_MAP, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
54 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_map)},
55 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_CH_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
56 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_cfg)},
57 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_INTERLEAVING_STYLE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
58 get_token_u32, offsetof(struct sof_ipc4_pin_format,
59 audio_fmt.interleaving_style)},
60 {SOF_TKN_CAVS_AUDIO_FORMAT_IN_FMT_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
61 offsetof(struct sof_ipc4_pin_format, audio_fmt.fmt_cfg)},
62 {SOF_TKN_CAVS_AUDIO_FORMAT_PIN_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
63 offsetof(struct sof_ipc4_pin_format, pin_index)},
64 {SOF_TKN_CAVS_AUDIO_FORMAT_IBS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
65 offsetof(struct sof_ipc4_pin_format, buffer_size)},
68 static const struct sof_topology_token ipc4_out_audio_format_tokens[] = {
69 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
70 offsetof(struct sof_ipc4_pin_format, audio_fmt.sampling_frequency)},
71 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_BIT_DEPTH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
72 offsetof(struct sof_ipc4_pin_format, audio_fmt.bit_depth)},
73 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_CH_MAP, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
74 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_map)},
75 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_CH_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
76 offsetof(struct sof_ipc4_pin_format, audio_fmt.ch_cfg)},
77 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_INTERLEAVING_STYLE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
78 get_token_u32, offsetof(struct sof_ipc4_pin_format,
79 audio_fmt.interleaving_style)},
80 {SOF_TKN_CAVS_AUDIO_FORMAT_OUT_FMT_CFG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
81 offsetof(struct sof_ipc4_pin_format, audio_fmt.fmt_cfg)},
82 {SOF_TKN_CAVS_AUDIO_FORMAT_PIN_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
83 offsetof(struct sof_ipc4_pin_format, pin_index)},
84 {SOF_TKN_CAVS_AUDIO_FORMAT_OBS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
85 offsetof(struct sof_ipc4_pin_format, buffer_size)},
88 static const struct sof_topology_token ipc4_copier_deep_buffer_tokens[] = {
89 {SOF_TKN_INTEL_COPIER_DEEP_BUFFER_DMA_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 0},
92 static const struct sof_topology_token ipc4_copier_tokens[] = {
93 {SOF_TKN_INTEL_COPIER_NODE_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 0},
96 static const struct sof_topology_token ipc4_audio_fmt_num_tokens[] = {
97 {SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
98 offsetof(struct sof_ipc4_available_audio_format, num_input_formats)},
99 {SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
100 offsetof(struct sof_ipc4_available_audio_format, num_output_formats)},
103 static const struct sof_topology_token dai_tokens[] = {
104 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
105 offsetof(struct sof_ipc4_copier, dai_type)},
106 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
107 offsetof(struct sof_ipc4_copier, dai_index)},
110 /* Component extended tokens */
111 static const struct sof_topology_token comp_ext_tokens[] = {
112 {SOF_TKN_COMP_UUID, SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid,
113 offsetof(struct snd_sof_widget, uuid)},
114 {SOF_TKN_COMP_CORE_ID, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
115 offsetof(struct snd_sof_widget, core)},
118 static const struct sof_topology_token gain_tokens[] = {
119 {SOF_TKN_GAIN_RAMP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
120 get_token_u32, offsetof(struct sof_ipc4_gain_data, curve_type)},
121 {SOF_TKN_GAIN_RAMP_DURATION,
122 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
123 offsetof(struct sof_ipc4_gain_data, curve_duration_l)},
124 {SOF_TKN_GAIN_VAL, SND_SOC_TPLG_TUPLE_TYPE_WORD,
125 get_token_u32, offsetof(struct sof_ipc4_gain_data, init_val)},
129 static const struct sof_topology_token src_tokens[] = {
130 {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
131 offsetof(struct sof_ipc4_src, sink_rate)},
134 static const struct sof_token_info ipc4_token_list[SOF_TOKEN_COUNT] = {
135 [SOF_DAI_TOKENS] = {"DAI tokens", dai_tokens, ARRAY_SIZE(dai_tokens)},
136 [SOF_PIPELINE_TOKENS] = {"Pipeline tokens", pipeline_tokens, ARRAY_SIZE(pipeline_tokens)},
137 [SOF_SCHED_TOKENS] = {"Scheduler tokens", ipc4_sched_tokens,
138 ARRAY_SIZE(ipc4_sched_tokens)},
139 [SOF_COMP_EXT_TOKENS] = {"Comp extended tokens", comp_ext_tokens,
140 ARRAY_SIZE(comp_ext_tokens)},
141 [SOF_COMP_TOKENS] = {"IPC4 Component tokens",
142 ipc4_comp_tokens, ARRAY_SIZE(ipc4_comp_tokens)},
143 [SOF_IN_AUDIO_FORMAT_TOKENS] = {"IPC4 Input Audio format tokens",
144 ipc4_in_audio_format_tokens, ARRAY_SIZE(ipc4_in_audio_format_tokens)},
145 [SOF_OUT_AUDIO_FORMAT_TOKENS] = {"IPC4 Output Audio format tokens",
146 ipc4_out_audio_format_tokens, ARRAY_SIZE(ipc4_out_audio_format_tokens)},
147 [SOF_COPIER_DEEP_BUFFER_TOKENS] = {"IPC4 Copier deep buffer tokens",
148 ipc4_copier_deep_buffer_tokens, ARRAY_SIZE(ipc4_copier_deep_buffer_tokens)},
149 [SOF_COPIER_TOKENS] = {"IPC4 Copier tokens", ipc4_copier_tokens,
150 ARRAY_SIZE(ipc4_copier_tokens)},
151 [SOF_AUDIO_FMT_NUM_TOKENS] = {"IPC4 Audio format number tokens",
152 ipc4_audio_fmt_num_tokens, ARRAY_SIZE(ipc4_audio_fmt_num_tokens)},
153 [SOF_GAIN_TOKENS] = {"Gain tokens", gain_tokens, ARRAY_SIZE(gain_tokens)},
154 [SOF_SRC_TOKENS] = {"SRC tokens", src_tokens, ARRAY_SIZE(src_tokens)},
157 static void sof_ipc4_dbg_audio_format(struct device *dev, struct sof_ipc4_pin_format *pin_fmt,
162 for (i = 0; i < num_formats; i++) {
163 struct sof_ipc4_audio_format *fmt = &pin_fmt[i].audio_fmt;
165 "Pin index #%d: %uHz, %ubit (ch_map %#x ch_cfg %u interleaving_style %u fmt_cfg %#x) buffer size %d\n",
166 pin_fmt[i].pin_index, fmt->sampling_frequency, fmt->bit_depth, fmt->ch_map,
167 fmt->ch_cfg, fmt->interleaving_style, fmt->fmt_cfg,
168 pin_fmt[i].buffer_size);
172 static const struct sof_ipc4_audio_format *
173 sof_ipc4_get_input_pin_audio_fmt(struct snd_sof_widget *swidget, int pin_index)
175 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext;
176 struct sof_ipc4_process *process;
179 if (swidget->id != snd_soc_dapm_effect) {
180 struct sof_ipc4_base_module_cfg *base = swidget->private;
182 /* For non-process modules, base module config format is used for all input pins */
183 return &base->audio_fmt;
186 process = swidget->private;
187 base_cfg_ext = process->base_config_ext;
190 * If there are multiple input formats available for a pin, the first available format
193 for (i = 0; i < base_cfg_ext->num_input_pin_fmts; i++) {
194 struct sof_ipc4_pin_format *pin_format = &base_cfg_ext->pin_formats[i];
196 if (pin_format->pin_index == pin_index)
197 return &pin_format->audio_fmt;
204 * sof_ipc4_get_audio_fmt - get available audio formats from swidget->tuples
205 * @scomp: pointer to pointer to SOC component
206 * @swidget: pointer to struct snd_sof_widget containing tuples
207 * @available_fmt: pointer to struct sof_ipc4_available_audio_format being filling in
208 * @module_base_cfg: Pointer to the base_config in the module init IPC payload
210 * Return: 0 if successful
212 static int sof_ipc4_get_audio_fmt(struct snd_soc_component *scomp,
213 struct snd_sof_widget *swidget,
214 struct sof_ipc4_available_audio_format *available_fmt,
215 struct sof_ipc4_base_module_cfg *module_base_cfg)
217 struct sof_ipc4_pin_format *in_format = NULL;
218 struct sof_ipc4_pin_format *out_format;
221 ret = sof_update_ipc_object(scomp, available_fmt,
222 SOF_AUDIO_FMT_NUM_TOKENS, swidget->tuples,
223 swidget->num_tuples, sizeof(available_fmt), 1);
225 dev_err(scomp->dev, "Failed to parse audio format token count\n");
229 if (!available_fmt->num_input_formats && !available_fmt->num_output_formats) {
230 dev_err(scomp->dev, "No input/output pin formats set in topology\n");
235 "Number of input audio formats: %d. Number of output audio formats: %d\n",
236 available_fmt->num_input_formats, available_fmt->num_output_formats);
238 /* set cpc and is_pages in the module's base_config */
239 ret = sof_update_ipc_object(scomp, module_base_cfg, SOF_COMP_TOKENS, swidget->tuples,
240 swidget->num_tuples, sizeof(*module_base_cfg), 1);
242 dev_err(scomp->dev, "parse comp tokens for %s failed, error: %d\n",
243 swidget->widget->name, ret);
247 dev_dbg(scomp->dev, "widget %s cpc: %d is_pages: %d\n",
248 swidget->widget->name, module_base_cfg->cpc, module_base_cfg->is_pages);
250 if (available_fmt->num_input_formats) {
251 in_format = kcalloc(available_fmt->num_input_formats,
252 sizeof(*in_format), GFP_KERNEL);
255 available_fmt->input_pin_fmts = in_format;
257 ret = sof_update_ipc_object(scomp, in_format,
258 SOF_IN_AUDIO_FORMAT_TOKENS, swidget->tuples,
259 swidget->num_tuples, sizeof(*in_format),
260 available_fmt->num_input_formats);
262 dev_err(scomp->dev, "parse input audio fmt tokens failed %d\n", ret);
266 dev_dbg(scomp->dev, "Input audio formats for %s\n", swidget->widget->name);
267 sof_ipc4_dbg_audio_format(scomp->dev, in_format,
268 available_fmt->num_input_formats);
271 if (available_fmt->num_output_formats) {
272 out_format = kcalloc(available_fmt->num_output_formats, sizeof(*out_format),
279 ret = sof_update_ipc_object(scomp, out_format,
280 SOF_OUT_AUDIO_FORMAT_TOKENS, swidget->tuples,
281 swidget->num_tuples, sizeof(*out_format),
282 available_fmt->num_output_formats);
284 dev_err(scomp->dev, "parse output audio fmt tokens failed\n");
288 available_fmt->output_pin_fmts = out_format;
289 dev_dbg(scomp->dev, "Output audio formats for %s\n", swidget->widget->name);
290 sof_ipc4_dbg_audio_format(scomp->dev, out_format,
291 available_fmt->num_output_formats);
300 available_fmt->input_pin_fmts = NULL;
304 /* release the memory allocated in sof_ipc4_get_audio_fmt */
305 static void sof_ipc4_free_audio_fmt(struct sof_ipc4_available_audio_format *available_fmt)
308 kfree(available_fmt->output_pin_fmts);
309 available_fmt->output_pin_fmts = NULL;
310 kfree(available_fmt->input_pin_fmts);
311 available_fmt->input_pin_fmts = NULL;
314 static void sof_ipc4_widget_free_comp_pipeline(struct snd_sof_widget *swidget)
316 kfree(swidget->private);
319 static int sof_ipc4_widget_set_module_info(struct snd_sof_widget *swidget)
321 struct snd_soc_component *scomp = swidget->scomp;
322 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
324 swidget->module_info = sof_ipc4_find_module_by_uuid(sdev, &swidget->uuid);
326 if (swidget->module_info)
329 dev_err(sdev->dev, "failed to find module info for widget %s with UUID %pUL\n",
330 swidget->widget->name, &swidget->uuid);
334 static int sof_ipc4_widget_setup_msg(struct snd_sof_widget *swidget, struct sof_ipc4_msg *msg)
336 struct sof_ipc4_fw_module *fw_module;
340 ret = sof_ipc4_widget_set_module_info(swidget);
344 fw_module = swidget->module_info;
346 msg->primary = fw_module->man4_module_entry.id;
347 msg->primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE);
348 msg->primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
349 msg->primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
351 msg->extension = SOF_IPC4_MOD_EXT_CORE_ID(swidget->core);
353 type = (fw_module->man4_module_entry.type & SOF_IPC4_MODULE_DP) ? 1 : 0;
354 msg->extension |= SOF_IPC4_MOD_EXT_DOMAIN(type);
359 static void sof_ipc4_widget_update_kcontrol_module_id(struct snd_sof_widget *swidget)
361 struct snd_soc_component *scomp = swidget->scomp;
362 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
363 struct sof_ipc4_fw_module *fw_module = swidget->module_info;
364 struct snd_sof_control *scontrol;
366 /* update module ID for all kcontrols for this widget */
367 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
368 if (scontrol->comp_id == swidget->comp_id) {
369 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
370 struct sof_ipc4_msg *msg = &cdata->msg;
372 msg->primary |= fw_module->man4_module_entry.id;
377 static int sof_ipc4_widget_setup_pcm(struct snd_sof_widget *swidget)
379 struct sof_ipc4_available_audio_format *available_fmt;
380 struct snd_soc_component *scomp = swidget->scomp;
381 struct sof_ipc4_copier *ipc4_copier;
385 ipc4_copier = kzalloc(sizeof(*ipc4_copier), GFP_KERNEL);
389 swidget->private = ipc4_copier;
390 available_fmt = &ipc4_copier->available_fmt;
392 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name);
394 ret = sof_ipc4_get_audio_fmt(scomp, swidget, available_fmt,
395 &ipc4_copier->data.base_config);
400 * This callback is used by host copier and module-to-module copier,
401 * and only host copier needs to set gtw_cfg.
403 if (!WIDGET_IS_AIF(swidget->id))
406 ret = sof_update_ipc_object(scomp, &node_type,
407 SOF_COPIER_TOKENS, swidget->tuples,
408 swidget->num_tuples, sizeof(node_type), 1);
411 dev_err(scomp->dev, "parse host copier node type token failed %d\n",
413 goto free_available_fmt;
415 dev_dbg(scomp->dev, "host copier '%s' node_type %u\n", swidget->widget->name, node_type);
418 ipc4_copier->gtw_attr = kzalloc(sizeof(*ipc4_copier->gtw_attr), GFP_KERNEL);
419 if (!ipc4_copier->gtw_attr) {
421 goto free_available_fmt;
424 ipc4_copier->copier_config = (uint32_t *)ipc4_copier->gtw_attr;
425 ipc4_copier->data.gtw_cfg.config_length =
426 sizeof(struct sof_ipc4_gtw_attributes) >> 2;
428 switch (swidget->id) {
429 case snd_soc_dapm_aif_in:
430 case snd_soc_dapm_aif_out:
431 ipc4_copier->data.gtw_cfg.node_id = SOF_IPC4_NODE_TYPE(node_type);
433 case snd_soc_dapm_buffer:
434 ipc4_copier->data.gtw_cfg.node_id = SOF_IPC4_INVALID_NODE_ID;
435 ipc4_copier->ipc_config_size = 0;
438 dev_err(scomp->dev, "invalid widget type %d\n", swidget->id);
443 /* set up module info and message header */
444 ret = sof_ipc4_widget_setup_msg(swidget, &ipc4_copier->msg);
451 kfree(ipc4_copier->gtw_attr);
453 sof_ipc4_free_audio_fmt(available_fmt);
456 swidget->private = NULL;
460 static void sof_ipc4_widget_free_comp_pcm(struct snd_sof_widget *swidget)
462 struct sof_ipc4_copier *ipc4_copier = swidget->private;
463 struct sof_ipc4_available_audio_format *available_fmt;
468 available_fmt = &ipc4_copier->available_fmt;
469 kfree(available_fmt->output_pin_fmts);
470 kfree(ipc4_copier->gtw_attr);
472 swidget->private = NULL;
475 static int sof_ipc4_widget_setup_comp_dai(struct snd_sof_widget *swidget)
477 struct sof_ipc4_available_audio_format *available_fmt;
478 struct snd_soc_component *scomp = swidget->scomp;
479 struct snd_sof_dai *dai = swidget->private;
480 struct sof_ipc4_copier *ipc4_copier;
481 struct snd_sof_widget *pipe_widget;
482 struct sof_ipc4_pipeline *pipeline;
486 ipc4_copier = kzalloc(sizeof(*ipc4_copier), GFP_KERNEL);
490 available_fmt = &ipc4_copier->available_fmt;
492 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name);
494 ret = sof_ipc4_get_audio_fmt(scomp, swidget, available_fmt,
495 &ipc4_copier->data.base_config);
499 ret = sof_update_ipc_object(scomp, &node_type,
500 SOF_COPIER_TOKENS, swidget->tuples,
501 swidget->num_tuples, sizeof(node_type), 1);
503 dev_err(scomp->dev, "parse dai node type failed %d\n", ret);
504 goto free_available_fmt;
507 ret = sof_update_ipc_object(scomp, ipc4_copier,
508 SOF_DAI_TOKENS, swidget->tuples,
509 swidget->num_tuples, sizeof(u32), 1);
511 dev_err(scomp->dev, "parse dai copier node token failed %d\n", ret);
512 goto free_available_fmt;
515 dev_dbg(scomp->dev, "dai %s node_type %u dai_type %u dai_index %d\n", swidget->widget->name,
516 node_type, ipc4_copier->dai_type, ipc4_copier->dai_index);
518 ipc4_copier->data.gtw_cfg.node_id = SOF_IPC4_NODE_TYPE(node_type);
520 pipe_widget = swidget->spipe->pipe_widget;
521 pipeline = pipe_widget->private;
522 if (pipeline->use_chain_dma && ipc4_copier->dai_type != SOF_DAI_INTEL_HDA) {
524 "Bad DAI type '%d', Chained DMA is only supported by HDA DAIs (%d).\n",
525 ipc4_copier->dai_type, SOF_DAI_INTEL_HDA);
527 goto free_available_fmt;
530 switch (ipc4_copier->dai_type) {
531 case SOF_DAI_INTEL_ALH:
533 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
534 struct sof_ipc4_alh_configuration_blob *blob;
535 struct snd_soc_dapm_path *p;
536 struct snd_sof_widget *w;
539 snd_soc_dapm_widget_for_each_source_path(swidget->widget, p)
542 if (swidget->id == snd_soc_dapm_dai_in && src_num == 0) {
544 * The blob will not be used if the ALH copier is playback direction
545 * and doesn't connect to any source.
546 * It is fine to call kfree(ipc4_copier->copier_config) since
547 * ipc4_copier->copier_config is null.
553 blob = kzalloc(sizeof(*blob), GFP_KERNEL);
556 goto free_available_fmt;
559 list_for_each_entry(w, &sdev->widget_list, list) {
560 if (w->widget->sname &&
561 strcmp(w->widget->sname, swidget->widget->sname))
564 blob->alh_cfg.count++;
567 ipc4_copier->copier_config = (uint32_t *)blob;
568 ipc4_copier->data.gtw_cfg.config_length = sizeof(*blob) >> 2;
571 case SOF_DAI_INTEL_SSP:
572 /* set SSP DAI index as the node_id */
573 ipc4_copier->data.gtw_cfg.node_id |=
574 SOF_IPC4_NODE_INDEX_INTEL_SSP(ipc4_copier->dai_index);
576 case SOF_DAI_INTEL_DMIC:
577 /* set DMIC DAI index as the node_id */
578 ipc4_copier->data.gtw_cfg.node_id |=
579 SOF_IPC4_NODE_INDEX_INTEL_DMIC(ipc4_copier->dai_index);
582 ipc4_copier->gtw_attr = kzalloc(sizeof(*ipc4_copier->gtw_attr), GFP_KERNEL);
583 if (!ipc4_copier->gtw_attr) {
585 goto free_available_fmt;
588 ipc4_copier->copier_config = (uint32_t *)ipc4_copier->gtw_attr;
589 ipc4_copier->data.gtw_cfg.config_length =
590 sizeof(struct sof_ipc4_gtw_attributes) >> 2;
595 dai->private = ipc4_copier;
597 /* set up module info and message header */
598 ret = sof_ipc4_widget_setup_msg(swidget, &ipc4_copier->msg);
600 goto free_copier_config;
605 kfree(ipc4_copier->copier_config);
607 sof_ipc4_free_audio_fmt(available_fmt);
615 static void sof_ipc4_widget_free_comp_dai(struct snd_sof_widget *swidget)
617 struct sof_ipc4_available_audio_format *available_fmt;
618 struct snd_sof_dai *dai = swidget->private;
619 struct sof_ipc4_copier *ipc4_copier;
626 swidget->private = NULL;
630 ipc4_copier = dai->private;
631 available_fmt = &ipc4_copier->available_fmt;
633 kfree(available_fmt->output_pin_fmts);
634 if (ipc4_copier->dai_type != SOF_DAI_INTEL_SSP &&
635 ipc4_copier->dai_type != SOF_DAI_INTEL_DMIC)
636 kfree(ipc4_copier->copier_config);
639 swidget->private = NULL;
642 static int sof_ipc4_widget_setup_comp_pipeline(struct snd_sof_widget *swidget)
644 struct snd_soc_component *scomp = swidget->scomp;
645 struct sof_ipc4_pipeline *pipeline;
648 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
652 ret = sof_update_ipc_object(scomp, pipeline, SOF_SCHED_TOKENS, swidget->tuples,
653 swidget->num_tuples, sizeof(*pipeline), 1);
655 dev_err(scomp->dev, "parsing scheduler tokens failed\n");
659 swidget->core = pipeline->core_id;
661 if (pipeline->use_chain_dma) {
662 dev_dbg(scomp->dev, "Set up chain DMA for %s\n", swidget->widget->name);
663 swidget->private = pipeline;
667 /* parse one set of pipeline tokens */
668 ret = sof_update_ipc_object(scomp, swidget, SOF_PIPELINE_TOKENS, swidget->tuples,
669 swidget->num_tuples, sizeof(*swidget), 1);
671 dev_err(scomp->dev, "parsing pipeline tokens failed\n");
675 /* TODO: Get priority from topology */
676 pipeline->priority = 0;
678 dev_dbg(scomp->dev, "pipeline '%s': id %d, pri %d, core_id %u, lp mode %d\n",
679 swidget->widget->name, swidget->pipeline_id,
680 pipeline->priority, pipeline->core_id, pipeline->lp_mode);
682 swidget->private = pipeline;
684 pipeline->msg.primary = SOF_IPC4_GLB_PIPE_PRIORITY(pipeline->priority);
685 pipeline->msg.primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_CREATE_PIPELINE);
686 pipeline->msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
687 pipeline->msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);
689 pipeline->msg.extension = pipeline->lp_mode;
690 pipeline->msg.extension |= SOF_IPC4_GLB_PIPE_EXT_CORE_ID(pipeline->core_id);
691 pipeline->state = SOF_IPC4_PIPE_UNINITIALIZED;
699 static int sof_ipc4_widget_setup_comp_pga(struct snd_sof_widget *swidget)
701 struct snd_soc_component *scomp = swidget->scomp;
702 struct sof_ipc4_gain *gain;
705 gain = kzalloc(sizeof(*gain), GFP_KERNEL);
709 swidget->private = gain;
711 gain->data.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK;
712 gain->data.init_val = SOF_IPC4_VOL_ZERO_DB;
714 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &gain->available_fmt, &gain->base_config);
718 ret = sof_update_ipc_object(scomp, &gain->data, SOF_GAIN_TOKENS, swidget->tuples,
719 swidget->num_tuples, sizeof(gain->data), 1);
721 dev_err(scomp->dev, "Parsing gain tokens failed\n");
726 "pga widget %s: ramp type: %d, ramp duration %d, initial gain value: %#x, cpc %d\n",
727 swidget->widget->name, gain->data.curve_type, gain->data.curve_duration_l,
728 gain->data.init_val, gain->base_config.cpc);
730 ret = sof_ipc4_widget_setup_msg(swidget, &gain->msg);
734 sof_ipc4_widget_update_kcontrol_module_id(swidget);
738 sof_ipc4_free_audio_fmt(&gain->available_fmt);
740 swidget->private = NULL;
744 static void sof_ipc4_widget_free_comp_pga(struct snd_sof_widget *swidget)
746 struct sof_ipc4_gain *gain = swidget->private;
751 sof_ipc4_free_audio_fmt(&gain->available_fmt);
752 kfree(swidget->private);
753 swidget->private = NULL;
756 static int sof_ipc4_widget_setup_comp_mixer(struct snd_sof_widget *swidget)
758 struct snd_soc_component *scomp = swidget->scomp;
759 struct sof_ipc4_mixer *mixer;
762 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name);
764 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
768 swidget->private = mixer;
770 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &mixer->available_fmt,
771 &mixer->base_config);
775 ret = sof_ipc4_widget_setup_msg(swidget, &mixer->msg);
781 sof_ipc4_free_audio_fmt(&mixer->available_fmt);
783 swidget->private = NULL;
787 static int sof_ipc4_widget_setup_comp_src(struct snd_sof_widget *swidget)
789 struct snd_soc_component *scomp = swidget->scomp;
790 struct sof_ipc4_src *src;
793 dev_dbg(scomp->dev, "Updating IPC structure for %s\n", swidget->widget->name);
795 src = kzalloc(sizeof(*src), GFP_KERNEL);
799 swidget->private = src;
801 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &src->available_fmt, &src->base_config);
805 ret = sof_update_ipc_object(scomp, src, SOF_SRC_TOKENS, swidget->tuples,
806 swidget->num_tuples, sizeof(*src), 1);
808 dev_err(scomp->dev, "Parsing SRC tokens failed\n");
812 dev_dbg(scomp->dev, "SRC sink rate %d\n", src->sink_rate);
814 ret = sof_ipc4_widget_setup_msg(swidget, &src->msg);
820 sof_ipc4_free_audio_fmt(&src->available_fmt);
822 swidget->private = NULL;
826 static void sof_ipc4_widget_free_comp_src(struct snd_sof_widget *swidget)
828 struct sof_ipc4_src *src = swidget->private;
833 sof_ipc4_free_audio_fmt(&src->available_fmt);
834 kfree(swidget->private);
835 swidget->private = NULL;
838 static void sof_ipc4_widget_free_comp_mixer(struct snd_sof_widget *swidget)
840 struct sof_ipc4_mixer *mixer = swidget->private;
845 sof_ipc4_free_audio_fmt(&mixer->available_fmt);
846 kfree(swidget->private);
847 swidget->private = NULL;
851 * Add the process modules support. The process modules are defined as snd_soc_dapm_effect modules.
853 static int sof_ipc4_widget_setup_comp_process(struct snd_sof_widget *swidget)
855 struct snd_soc_component *scomp = swidget->scomp;
856 struct sof_ipc4_fw_module *fw_module;
857 struct sof_ipc4_process *process;
861 process = kzalloc(sizeof(*process), GFP_KERNEL);
865 swidget->private = process;
867 ret = sof_ipc4_get_audio_fmt(scomp, swidget, &process->available_fmt,
868 &process->base_config);
872 ret = sof_ipc4_widget_setup_msg(swidget, &process->msg);
876 /* parse process init module payload config type from module info */
877 fw_module = swidget->module_info;
878 process->init_config = FIELD_GET(SOF_IPC4_MODULE_INIT_CONFIG_MASK,
879 fw_module->man4_module_entry.type);
881 process->ipc_config_size = sizeof(struct sof_ipc4_base_module_cfg);
883 /* allocate memory for base config extension if needed */
884 if (process->init_config == SOF_IPC4_MODULE_INIT_CONFIG_TYPE_BASE_CFG_WITH_EXT) {
885 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext;
886 u32 ext_size = struct_size(base_cfg_ext, pin_formats,
887 swidget->num_input_pins + swidget->num_output_pins);
889 base_cfg_ext = kzalloc(ext_size, GFP_KERNEL);
892 goto free_available_fmt;
895 base_cfg_ext->num_input_pin_fmts = swidget->num_input_pins;
896 base_cfg_ext->num_output_pin_fmts = swidget->num_output_pins;
897 process->base_config_ext = base_cfg_ext;
898 process->base_config_ext_size = ext_size;
899 process->ipc_config_size += ext_size;
902 cfg = kzalloc(process->ipc_config_size, GFP_KERNEL);
905 goto free_base_cfg_ext;
908 process->ipc_config_data = cfg;
910 sof_ipc4_widget_update_kcontrol_module_id(swidget);
914 kfree(process->base_config_ext);
915 process->base_config_ext = NULL;
917 sof_ipc4_free_audio_fmt(&process->available_fmt);
920 swidget->private = NULL;
924 static void sof_ipc4_widget_free_comp_process(struct snd_sof_widget *swidget)
926 struct sof_ipc4_process *process = swidget->private;
931 kfree(process->ipc_config_data);
932 kfree(process->base_config_ext);
933 sof_ipc4_free_audio_fmt(&process->available_fmt);
934 kfree(swidget->private);
935 swidget->private = NULL;
939 sof_ipc4_update_pipeline_mem_usage(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
940 struct sof_ipc4_base_module_cfg *base_config)
942 struct sof_ipc4_fw_module *fw_module = swidget->module_info;
943 struct snd_sof_widget *pipe_widget;
944 struct sof_ipc4_pipeline *pipeline;
945 int task_mem, queue_mem;
948 ibs = base_config->ibs;
949 bss = base_config->is_pages;
951 task_mem = SOF_IPC4_PIPELINE_OBJECT_SIZE;
952 task_mem += SOF_IPC4_MODULE_INSTANCE_LIST_ITEM_SIZE + bss;
954 if (fw_module->man4_module_entry.type & SOF_IPC4_MODULE_LL) {
955 task_mem += SOF_IPC4_FW_ROUNDUP(SOF_IPC4_LL_TASK_OBJECT_SIZE);
956 task_mem += SOF_IPC4_FW_MAX_QUEUE_COUNT * SOF_IPC4_MODULE_INSTANCE_LIST_ITEM_SIZE;
957 task_mem += SOF_IPC4_LL_TASK_LIST_ITEM_SIZE;
959 task_mem += SOF_IPC4_FW_ROUNDUP(SOF_IPC4_DP_TASK_OBJECT_SIZE);
960 task_mem += SOF_IPC4_DP_TASK_LIST_SIZE;
963 ibs = SOF_IPC4_FW_ROUNDUP(ibs);
964 queue_mem = SOF_IPC4_FW_MAX_QUEUE_COUNT * (SOF_IPC4_DATA_QUEUE_OBJECT_SIZE + ibs);
966 total = SOF_IPC4_FW_PAGE(task_mem + queue_mem);
968 pipe_widget = swidget->spipe->pipe_widget;
969 pipeline = pipe_widget->private;
970 pipeline->mem_usage += total;
973 static int sof_ipc4_widget_assign_instance_id(struct snd_sof_dev *sdev,
974 struct snd_sof_widget *swidget)
976 struct sof_ipc4_fw_module *fw_module = swidget->module_info;
977 int max_instances = fw_module->man4_module_entry.instance_max_count;
979 swidget->instance_id = ida_alloc_max(&fw_module->m_ida, max_instances, GFP_KERNEL);
980 if (swidget->instance_id < 0) {
981 dev_err(sdev->dev, "failed to assign instance id for widget %s",
982 swidget->widget->name);
983 return swidget->instance_id;
989 /* update hw_params based on the audio stream format */
990 static int sof_ipc4_update_hw_params(struct snd_sof_dev *sdev, struct snd_pcm_hw_params *params,
991 struct sof_ipc4_audio_format *fmt)
993 snd_pcm_format_t snd_fmt;
994 struct snd_interval *i;
996 int valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg);
997 unsigned int channels, rate;
999 switch (valid_bits) {
1001 snd_fmt = SNDRV_PCM_FORMAT_S16_LE;
1004 snd_fmt = SNDRV_PCM_FORMAT_S24_LE;
1007 snd_fmt = SNDRV_PCM_FORMAT_S32_LE;
1010 dev_err(sdev->dev, "invalid PCM valid_bits %d\n", valid_bits);
1014 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1016 snd_mask_set_format(m, snd_fmt);
1018 rate = fmt->sampling_frequency;
1019 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1023 channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(fmt->fmt_cfg);
1024 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1031 static int sof_ipc4_init_audio_fmt(struct snd_sof_dev *sdev,
1032 struct snd_sof_widget *swidget,
1033 struct sof_ipc4_base_module_cfg *base_config,
1034 struct snd_pcm_hw_params *params,
1035 struct sof_ipc4_available_audio_format *available_fmt,
1036 struct sof_ipc4_pin_format *pin_fmts, u32 pin_fmts_size)
1041 int sample_valid_bits;
1045 dev_err(sdev->dev, "no reference formats for %s\n", swidget->widget->name);
1049 switch (params_format(params)) {
1050 case SNDRV_PCM_FORMAT_S16_LE:
1051 sample_valid_bits = 16;
1053 case SNDRV_PCM_FORMAT_S24_LE:
1054 sample_valid_bits = 24;
1056 case SNDRV_PCM_FORMAT_S32_LE:
1057 sample_valid_bits = 32;
1060 dev_err(sdev->dev, "invalid pcm frame format %d\n", params_format(params));
1064 if (!pin_fmts_size) {
1065 dev_err(sdev->dev, "no formats available for %s\n", swidget->widget->name);
1070 * Search supported audio formats with pin index 0 to match rate, channels ,and
1071 * sample_valid_bytes from runtime params
1073 for (i = 0; i < pin_fmts_size; i++) {
1074 struct sof_ipc4_audio_format *fmt = &pin_fmts[i].audio_fmt;
1076 if (pin_fmts[i].pin_index)
1079 rate = fmt->sampling_frequency;
1080 channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(fmt->fmt_cfg);
1081 valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(fmt->fmt_cfg);
1082 if (params_rate(params) == rate && params_channels(params) == channels &&
1083 sample_valid_bits == valid_bits) {
1084 dev_dbg(sdev->dev, "matched audio format index for %uHz, %ubit, %u channels: %d\n",
1085 rate, valid_bits, channels, i);
1090 if (i == pin_fmts_size) {
1091 dev_err(sdev->dev, "%s: Unsupported audio format: %uHz, %ubit, %u channels\n",
1092 __func__, params_rate(params), sample_valid_bits, params_channels(params));
1096 /* copy input format */
1097 if (available_fmt->num_input_formats && i < available_fmt->num_input_formats) {
1098 memcpy(&base_config->audio_fmt, &available_fmt->input_pin_fmts[i].audio_fmt,
1099 sizeof(struct sof_ipc4_audio_format));
1101 /* set base_cfg ibs/obs */
1102 base_config->ibs = available_fmt->input_pin_fmts[i].buffer_size;
1104 dev_dbg(sdev->dev, "Init input audio formats for %s\n", swidget->widget->name);
1105 sof_ipc4_dbg_audio_format(sdev->dev, &available_fmt->input_pin_fmts[i], 1);
1108 if (available_fmt->num_output_formats && i < available_fmt->num_output_formats)
1109 base_config->obs = available_fmt->output_pin_fmts[i].buffer_size;
1111 /* Return the index of the matched format */
1115 static void sof_ipc4_unprepare_copier_module(struct snd_sof_widget *swidget)
1117 struct sof_ipc4_copier *ipc4_copier = NULL;
1118 struct snd_sof_widget *pipe_widget;
1119 struct sof_ipc4_pipeline *pipeline;
1121 /* reset pipeline memory usage */
1122 pipe_widget = swidget->spipe->pipe_widget;
1123 pipeline = pipe_widget->private;
1124 pipeline->mem_usage = 0;
1126 if (WIDGET_IS_AIF(swidget->id) || swidget->id == snd_soc_dapm_buffer) {
1127 if (pipeline->use_chain_dma) {
1128 pipeline->msg.primary = 0;
1129 pipeline->msg.extension = 0;
1131 ipc4_copier = swidget->private;
1132 } else if (WIDGET_IS_DAI(swidget->id)) {
1133 struct snd_sof_dai *dai = swidget->private;
1135 ipc4_copier = dai->private;
1137 if (pipeline->use_chain_dma) {
1138 pipeline->msg.primary = 0;
1139 pipeline->msg.extension = 0;
1142 if (ipc4_copier->dai_type == SOF_DAI_INTEL_ALH) {
1143 struct sof_ipc4_copier_data *copier_data = &ipc4_copier->data;
1144 struct sof_ipc4_alh_configuration_blob *blob;
1145 unsigned int group_id;
1147 blob = (struct sof_ipc4_alh_configuration_blob *)ipc4_copier->copier_config;
1148 if (blob->alh_cfg.count > 1) {
1149 group_id = SOF_IPC4_NODE_INDEX(ipc4_copier->data.gtw_cfg.node_id) -
1151 ida_free(&alh_group_ida, group_id);
1154 /* clear the node ID */
1155 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
1160 kfree(ipc4_copier->ipc_config_data);
1161 ipc4_copier->ipc_config_data = NULL;
1162 ipc4_copier->ipc_config_size = 0;
1166 #if IS_ENABLED(CONFIG_ACPI) && IS_ENABLED(CONFIG_SND_INTEL_NHLT)
1167 static int snd_sof_get_hw_config_params(struct snd_sof_dev *sdev, struct snd_sof_dai *dai,
1168 int *sample_rate, int *channel_count, int *bit_depth)
1170 struct snd_soc_tplg_hw_config *hw_config;
1171 struct snd_sof_dai_link *slink;
1172 bool dai_link_found = false;
1173 bool hw_cfg_found = false;
1176 /* get current hw_config from link */
1177 list_for_each_entry(slink, &sdev->dai_link_list, list) {
1178 if (!strcmp(slink->link->name, dai->name)) {
1179 dai_link_found = true;
1184 if (!dai_link_found) {
1185 dev_err(sdev->dev, "%s: no DAI link found for DAI %s\n", __func__, dai->name);
1189 for (i = 0; i < slink->num_hw_configs; i++) {
1190 hw_config = &slink->hw_configs[i];
1191 if (dai->current_config == le32_to_cpu(hw_config->id)) {
1192 hw_cfg_found = true;
1197 if (!hw_cfg_found) {
1198 dev_err(sdev->dev, "%s: no matching hw_config found for DAI %s\n", __func__,
1203 *bit_depth = le32_to_cpu(hw_config->tdm_slot_width);
1204 *channel_count = le32_to_cpu(hw_config->tdm_slots);
1205 *sample_rate = le32_to_cpu(hw_config->fsync_rate);
1207 dev_dbg(sdev->dev, "sample rate: %d sample width: %d channels: %d\n",
1208 *sample_rate, *bit_depth, *channel_count);
1213 static int snd_sof_get_nhlt_endpoint_data(struct snd_sof_dev *sdev, struct snd_sof_dai *dai,
1214 struct snd_pcm_hw_params *params, u32 dai_index,
1215 u32 linktype, u8 dir, u32 **dst, u32 *len)
1217 struct sof_ipc4_fw_data *ipc4_data = sdev->private;
1218 struct nhlt_specific_cfg *cfg;
1219 int sample_rate, channel_count;
1223 /* convert to NHLT type */
1225 case SOF_DAI_INTEL_DMIC:
1226 nhlt_type = NHLT_LINK_DMIC;
1227 bit_depth = params_width(params);
1228 channel_count = params_channels(params);
1229 sample_rate = params_rate(params);
1231 case SOF_DAI_INTEL_SSP:
1232 nhlt_type = NHLT_LINK_SSP;
1233 ret = snd_sof_get_hw_config_params(sdev, dai, &sample_rate, &channel_count,
1242 dev_dbg(sdev->dev, "dai index %d nhlt type %d direction %d\n",
1243 dai_index, nhlt_type, dir);
1245 /* find NHLT blob with matching params */
1246 cfg = intel_nhlt_get_endpoint_blob(sdev->dev, ipc4_data->nhlt, dai_index, nhlt_type,
1247 bit_depth, bit_depth, channel_count, sample_rate,
1252 "no matching blob for sample rate: %d sample width: %d channels: %d\n",
1253 sample_rate, bit_depth, channel_count);
1257 /* config length should be in dwords */
1258 *len = cfg->size >> 2;
1259 *dst = (u32 *)cfg->caps;
1264 static int snd_sof_get_nhlt_endpoint_data(struct snd_sof_dev *sdev, struct snd_sof_dai *dai,
1265 struct snd_pcm_hw_params *params, u32 dai_index,
1266 u32 linktype, u8 dir, u32 **dst, u32 *len)
1272 static int ipc4_set_fmt_mask(struct snd_mask *fmt, unsigned int bit_depth)
1274 switch (bit_depth) {
1276 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
1279 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
1282 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
1291 static int ipc4_copier_set_capture_fmt(struct snd_sof_dev *sdev,
1292 struct snd_pcm_hw_params *pipeline_params,
1293 struct snd_pcm_hw_params *fe_params,
1294 struct sof_ipc4_available_audio_format *available_fmt)
1296 struct sof_ipc4_audio_format *audio_fmt;
1297 unsigned int sample_valid_bits;
1298 bool multiple_formats = false;
1299 bool fe_format_match = false;
1300 struct snd_mask *fmt;
1303 for (i = 0; i < available_fmt->num_output_formats; i++) {
1306 audio_fmt = &available_fmt->output_pin_fmts[i].audio_fmt;
1307 val = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(audio_fmt->fmt_cfg);
1310 sample_valid_bits = val;
1311 else if (sample_valid_bits != val)
1312 multiple_formats = true;
1314 if (snd_pcm_format_width(params_format(fe_params)) == val)
1315 fe_format_match = true;
1318 fmt = hw_param_mask(pipeline_params, SNDRV_PCM_HW_PARAM_FORMAT);
1321 if (multiple_formats) {
1322 if (fe_format_match) {
1323 /* multiple formats defined and one matches FE */
1324 snd_mask_set_format(fmt, params_format(fe_params));
1328 dev_err(sdev->dev, "Multiple audio formats for single dai_out not supported\n");
1332 return ipc4_set_fmt_mask(fmt, sample_valid_bits);
1336 sof_ipc4_prepare_copier_module(struct snd_sof_widget *swidget,
1337 struct snd_pcm_hw_params *fe_params,
1338 struct snd_sof_platform_stream_params *platform_params,
1339 struct snd_pcm_hw_params *pipeline_params, int dir)
1341 struct sof_ipc4_available_audio_format *available_fmt;
1342 struct snd_soc_component *scomp = swidget->scomp;
1343 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1344 struct sof_ipc4_pin_format *format_list_to_search;
1345 struct sof_ipc4_copier_data *copier_data;
1346 struct snd_pcm_hw_params *ref_params;
1347 struct sof_ipc4_copier *ipc4_copier;
1348 struct snd_sof_dai *dai;
1349 struct snd_mask *fmt;
1350 int out_sample_valid_bits;
1351 void **ipc_config_data;
1352 int *ipc_config_size;
1355 u32 deep_buffer_dma_ms = 0;
1356 u32 format_list_count;
1358 dev_dbg(sdev->dev, "copier %s, type %d", swidget->widget->name, swidget->id);
1360 switch (swidget->id) {
1361 case snd_soc_dapm_aif_in:
1362 case snd_soc_dapm_aif_out:
1364 struct sof_ipc4_gtw_attributes *gtw_attr;
1365 struct snd_sof_widget *pipe_widget;
1366 struct sof_ipc4_pipeline *pipeline;
1368 /* parse the deep buffer dma size */
1369 ret = sof_update_ipc_object(scomp, &deep_buffer_dma_ms,
1370 SOF_COPIER_DEEP_BUFFER_TOKENS, swidget->tuples,
1371 swidget->num_tuples, sizeof(u32), 1);
1373 dev_err(scomp->dev, "Failed to parse deep buffer dma size for %s\n",
1374 swidget->widget->name);
1378 ipc4_copier = (struct sof_ipc4_copier *)swidget->private;
1379 gtw_attr = ipc4_copier->gtw_attr;
1380 copier_data = &ipc4_copier->data;
1381 available_fmt = &ipc4_copier->available_fmt;
1383 pipe_widget = swidget->spipe->pipe_widget;
1384 pipeline = pipe_widget->private;
1386 if (pipeline->use_chain_dma) {
1390 host_dma_id = platform_params->stream_tag - 1;
1391 pipeline->msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_HOST_ID(host_dma_id);
1393 /* Set SCS bit for S16_LE format only */
1394 if (params_format(fe_params) == SNDRV_PCM_FORMAT_S16_LE)
1395 pipeline->msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_SCS_MASK;
1398 * Despite its name the bitfield 'fifo_size' is used to define DMA buffer
1399 * size. The expression calculates 2ms buffer size.
1401 fifo_size = DIV_ROUND_UP((SOF_IPC4_CHAIN_DMA_BUF_SIZE_MS *
1402 params_rate(fe_params) *
1403 params_channels(fe_params) *
1404 params_physical_width(fe_params)), 8000);
1405 pipeline->msg.extension |= SOF_IPC4_GLB_EXT_CHAIN_DMA_FIFO_SIZE(fifo_size);
1408 * Chain DMA does not support stream timestamping, set node_id to invalid
1409 * to skip the code in sof_ipc4_get_stream_start_offset().
1411 copier_data->gtw_cfg.node_id = SOF_IPC4_INVALID_NODE_ID;
1417 * Use the input_pin_fmts to match pcm params for playback and the output_pin_fmts
1420 if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
1421 format_list_to_search = available_fmt->input_pin_fmts;
1422 format_list_count = available_fmt->num_input_formats;
1424 format_list_to_search = available_fmt->output_pin_fmts;
1425 format_list_count = available_fmt->num_output_formats;
1428 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
1429 copier_data->gtw_cfg.node_id |=
1430 SOF_IPC4_NODE_INDEX(platform_params->stream_tag - 1);
1432 /* set gateway attributes */
1433 gtw_attr->lp_buffer_alloc = pipeline->lp_mode;
1434 ref_params = fe_params;
1437 case snd_soc_dapm_dai_in:
1438 case snd_soc_dapm_dai_out:
1440 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget;
1441 struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
1443 if (pipeline->use_chain_dma)
1446 dai = swidget->private;
1448 ipc4_copier = (struct sof_ipc4_copier *)dai->private;
1449 copier_data = &ipc4_copier->data;
1450 available_fmt = &ipc4_copier->available_fmt;
1451 if (dir == SNDRV_PCM_STREAM_CAPTURE) {
1452 format_list_to_search = available_fmt->output_pin_fmts;
1453 format_list_count = available_fmt->num_output_formats;
1455 ret = ipc4_copier_set_capture_fmt(sdev, pipeline_params, fe_params,
1460 format_list_to_search = available_fmt->input_pin_fmts;
1461 format_list_count = available_fmt->num_input_formats;
1464 ref_params = pipeline_params;
1466 ret = snd_sof_get_nhlt_endpoint_data(sdev, dai, fe_params, ipc4_copier->dai_index,
1467 ipc4_copier->dai_type, dir,
1468 &ipc4_copier->copier_config,
1469 &copier_data->gtw_cfg.config_length);
1475 case snd_soc_dapm_buffer:
1477 ipc4_copier = (struct sof_ipc4_copier *)swidget->private;
1478 copier_data = &ipc4_copier->data;
1479 available_fmt = &ipc4_copier->available_fmt;
1481 /* Use the input formats to match pcm params */
1482 format_list_to_search = available_fmt->input_pin_fmts;
1483 format_list_count = available_fmt->num_input_formats;
1484 ref_params = pipeline_params;
1489 dev_err(sdev->dev, "unsupported type %d for copier %s",
1490 swidget->id, swidget->widget->name);
1494 /* set input and output audio formats */
1495 ret = sof_ipc4_init_audio_fmt(sdev, swidget, &copier_data->base_config, ref_params,
1496 available_fmt, format_list_to_search, format_list_count);
1501 * Set the output format. Current topology defines pin 0 input and output formats in pairs.
1502 * This assumes that the pin 0 formats are defined before all other pins.
1503 * So pick the output audio format with the same index as the chosen
1504 * input format. This logic will need to be updated when the format definitions
1505 * in topology change.
1507 memcpy(&copier_data->out_format, &available_fmt->output_pin_fmts[ret].audio_fmt,
1508 sizeof(struct sof_ipc4_audio_format));
1509 dev_dbg(sdev->dev, "Output audio format for %s\n", swidget->widget->name);
1510 sof_ipc4_dbg_audio_format(sdev->dev, &available_fmt->output_pin_fmts[ret], 1);
1512 switch (swidget->id) {
1513 case snd_soc_dapm_dai_in:
1514 case snd_soc_dapm_dai_out:
1517 * Only SOF_DAI_INTEL_ALH needs copier_data to set blob.
1518 * That's why only ALH dai's blob is set after sof_ipc4_init_audio_fmt
1520 if (ipc4_copier->dai_type == SOF_DAI_INTEL_ALH) {
1521 struct sof_ipc4_alh_configuration_blob *blob;
1522 struct sof_ipc4_copier_data *alh_data;
1523 struct sof_ipc4_copier *alh_copier;
1524 struct snd_sof_widget *w;
1532 blob = (struct sof_ipc4_alh_configuration_blob *)ipc4_copier->copier_config;
1534 blob->gw_attr.lp_buffer_alloc = 0;
1536 /* Get channel_mask from ch_map */
1537 ch_map = copier_data->base_config.audio_fmt.ch_map;
1538 for (i = 0; ch_map; i++) {
1539 if ((ch_map & 0xf) != 0xf) {
1546 step = ch_count / blob->alh_cfg.count;
1547 mask = GENMASK(step - 1, 0);
1549 * Set each gtw_cfg.node_id to blob->alh_cfg.mapping[]
1550 * for all widgets with the same stream name
1553 list_for_each_entry(w, &sdev->widget_list, list) {
1554 if (w->widget->sname &&
1555 strcmp(w->widget->sname, swidget->widget->sname))
1559 alh_copier = (struct sof_ipc4_copier *)dai->private;
1560 alh_data = &alh_copier->data;
1561 blob->alh_cfg.mapping[i].alh_id = alh_data->gtw_cfg.node_id;
1563 * Set the same channel mask for playback as the audio data is
1564 * duplicated for all speakers. For capture, split the channels
1565 * among the aggregated DAIs. For example, with 4 channels on 2
1566 * aggregated DAIs, the channel_mask should be 0x3 and 0xc for the
1568 * The channel masks used depend on the cpu_dais used in the
1569 * dailink at the machine driver level, which actually comes from
1570 * the tables in soc_acpi files depending on the _ADR and devID
1571 * registers for each codec.
1573 if (w->id == snd_soc_dapm_dai_in)
1574 blob->alh_cfg.mapping[i].channel_mask = ch_mask;
1576 blob->alh_cfg.mapping[i].channel_mask = mask << (step * i);
1580 if (blob->alh_cfg.count > 1) {
1583 group_id = ida_alloc_max(&alh_group_ida, ALH_MULTI_GTW_COUNT - 1,
1589 /* add multi-gateway base */
1590 group_id += ALH_MULTI_GTW_BASE;
1591 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
1592 copier_data->gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX(group_id);
1598 /* modify the input params for the next widget */
1599 fmt = hw_param_mask(pipeline_params, SNDRV_PCM_HW_PARAM_FORMAT);
1600 out_sample_valid_bits =
1601 SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(copier_data->out_format.fmt_cfg);
1603 ret = ipc4_set_fmt_mask(fmt, out_sample_valid_bits);
1608 * Set the gateway dma_buffer_size to 2ms buffer size to meet the FW expectation. In the
1609 * deep buffer case, set the dma_buffer_size depending on the deep_buffer_dma_ms set
1612 switch (swidget->id) {
1613 case snd_soc_dapm_dai_in:
1614 copier_data->gtw_cfg.dma_buffer_size =
1615 SOF_IPC4_MIN_DMA_BUFFER_SIZE * copier_data->base_config.ibs;
1617 case snd_soc_dapm_aif_in:
1618 copier_data->gtw_cfg.dma_buffer_size =
1619 max((u32)SOF_IPC4_MIN_DMA_BUFFER_SIZE, deep_buffer_dma_ms) *
1620 copier_data->base_config.ibs;
1622 case snd_soc_dapm_dai_out:
1623 case snd_soc_dapm_aif_out:
1624 copier_data->gtw_cfg.dma_buffer_size =
1625 SOF_IPC4_MIN_DMA_BUFFER_SIZE * copier_data->base_config.obs;
1631 data = &ipc4_copier->copier_config;
1632 ipc_config_size = &ipc4_copier->ipc_config_size;
1633 ipc_config_data = &ipc4_copier->ipc_config_data;
1635 /* config_length is DWORD based */
1636 ipc_size = sizeof(*copier_data) + copier_data->gtw_cfg.config_length * 4;
1638 dev_dbg(sdev->dev, "copier %s, IPC size is %d", swidget->widget->name, ipc_size);
1640 *ipc_config_data = kzalloc(ipc_size, GFP_KERNEL);
1641 if (!*ipc_config_data)
1644 *ipc_config_size = ipc_size;
1647 memcpy(*ipc_config_data, (void *)copier_data, sizeof(*copier_data));
1648 if (copier_data->gtw_cfg.config_length)
1649 memcpy(*ipc_config_data + sizeof(*copier_data),
1650 *data, copier_data->gtw_cfg.config_length * 4);
1652 /* update pipeline memory usage */
1653 sof_ipc4_update_pipeline_mem_usage(sdev, swidget, &copier_data->base_config);
1658 static int sof_ipc4_prepare_gain_module(struct snd_sof_widget *swidget,
1659 struct snd_pcm_hw_params *fe_params,
1660 struct snd_sof_platform_stream_params *platform_params,
1661 struct snd_pcm_hw_params *pipeline_params, int dir)
1663 struct snd_soc_component *scomp = swidget->scomp;
1664 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1665 struct sof_ipc4_gain *gain = swidget->private;
1666 struct sof_ipc4_available_audio_format *available_fmt = &gain->available_fmt;
1669 ret = sof_ipc4_init_audio_fmt(sdev, swidget, &gain->base_config,
1670 pipeline_params, available_fmt,
1671 available_fmt->input_pin_fmts,
1672 available_fmt->num_input_formats);
1676 /* update pipeline memory usage */
1677 sof_ipc4_update_pipeline_mem_usage(sdev, swidget, &gain->base_config);
1682 static int sof_ipc4_prepare_mixer_module(struct snd_sof_widget *swidget,
1683 struct snd_pcm_hw_params *fe_params,
1684 struct snd_sof_platform_stream_params *platform_params,
1685 struct snd_pcm_hw_params *pipeline_params, int dir)
1687 struct snd_soc_component *scomp = swidget->scomp;
1688 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1689 struct sof_ipc4_mixer *mixer = swidget->private;
1690 struct sof_ipc4_available_audio_format *available_fmt = &mixer->available_fmt;
1693 ret = sof_ipc4_init_audio_fmt(sdev, swidget, &mixer->base_config,
1694 pipeline_params, available_fmt,
1695 available_fmt->input_pin_fmts,
1696 available_fmt->num_input_formats);
1700 /* update pipeline memory usage */
1701 sof_ipc4_update_pipeline_mem_usage(sdev, swidget, &mixer->base_config);
1706 static int sof_ipc4_prepare_src_module(struct snd_sof_widget *swidget,
1707 struct snd_pcm_hw_params *fe_params,
1708 struct snd_sof_platform_stream_params *platform_params,
1709 struct snd_pcm_hw_params *pipeline_params, int dir)
1711 struct snd_soc_component *scomp = swidget->scomp;
1712 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1713 struct sof_ipc4_src *src = swidget->private;
1714 struct sof_ipc4_available_audio_format *available_fmt = &src->available_fmt;
1715 struct snd_interval *rate;
1718 ret = sof_ipc4_init_audio_fmt(sdev, swidget, &src->base_config,
1719 pipeline_params, available_fmt,
1720 available_fmt->input_pin_fmts,
1721 available_fmt->num_input_formats);
1725 /* update pipeline memory usage */
1726 sof_ipc4_update_pipeline_mem_usage(sdev, swidget, &src->base_config);
1728 /* update pipeline_params for sink widgets */
1729 rate = hw_param_interval(pipeline_params, SNDRV_PCM_HW_PARAM_RATE);
1730 rate->min = src->sink_rate;
1731 rate->max = rate->min;
1737 sof_ipc4_process_set_pin_formats(struct snd_sof_widget *swidget, int pin_type)
1739 struct sof_ipc4_process *process = swidget->private;
1740 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext = process->base_config_ext;
1741 struct sof_ipc4_available_audio_format *available_fmt = &process->available_fmt;
1742 struct sof_ipc4_pin_format *pin_format, *format_list_to_search;
1743 struct snd_soc_component *scomp = swidget->scomp;
1744 int num_pins, format_list_count;
1745 int pin_format_offset = 0;
1748 /* set number of pins, offset of pin format and format list to search based on pin type */
1749 if (pin_type == SOF_PIN_TYPE_INPUT) {
1750 num_pins = swidget->num_input_pins;
1751 format_list_to_search = available_fmt->input_pin_fmts;
1752 format_list_count = available_fmt->num_input_formats;
1754 num_pins = swidget->num_output_pins;
1755 pin_format_offset = swidget->num_input_pins;
1756 format_list_to_search = available_fmt->output_pin_fmts;
1757 format_list_count = available_fmt->num_output_formats;
1760 for (i = pin_format_offset; i < num_pins + pin_format_offset; i++) {
1761 pin_format = &base_cfg_ext->pin_formats[i];
1763 /* Pin 0 audio formats are derived from the base config input/output format */
1764 if (i == pin_format_offset) {
1765 if (pin_type == SOF_PIN_TYPE_INPUT) {
1766 pin_format->buffer_size = process->base_config.ibs;
1767 pin_format->audio_fmt = process->base_config.audio_fmt;
1769 pin_format->buffer_size = process->base_config.obs;
1770 pin_format->audio_fmt = process->output_format;
1776 * For all other pins, find the pin formats from those set in topology. If there
1777 * is more than one format specified for a pin, this will pick the first available
1780 for (j = 0; j < format_list_count; j++) {
1781 struct sof_ipc4_pin_format *pin_format_item = &format_list_to_search[j];
1783 if (pin_format_item->pin_index == i - pin_format_offset) {
1784 *pin_format = *pin_format_item;
1789 if (j == format_list_count) {
1790 dev_err(scomp->dev, "%s pin %d format not found for %s\n",
1791 (pin_type == SOF_PIN_TYPE_INPUT) ? "input" : "output",
1792 i - pin_format_offset, swidget->widget->name);
1800 static int sof_ipc4_process_add_base_cfg_extn(struct snd_sof_widget *swidget)
1804 /* copy input and output pin formats */
1805 for (i = 0; i <= SOF_PIN_TYPE_OUTPUT; i++) {
1806 ret = sof_ipc4_process_set_pin_formats(swidget, i);
1814 static int sof_ipc4_prepare_process_module(struct snd_sof_widget *swidget,
1815 struct snd_pcm_hw_params *fe_params,
1816 struct snd_sof_platform_stream_params *platform_params,
1817 struct snd_pcm_hw_params *pipeline_params, int dir)
1819 struct snd_soc_component *scomp = swidget->scomp;
1820 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1821 struct sof_ipc4_process *process = swidget->private;
1822 struct sof_ipc4_available_audio_format *available_fmt = &process->available_fmt;
1823 void *cfg = process->ipc_config_data;
1826 ret = sof_ipc4_init_audio_fmt(sdev, swidget, &process->base_config,
1827 pipeline_params, available_fmt,
1828 available_fmt->input_pin_fmts,
1829 available_fmt->num_input_formats);
1833 /* copy Pin 0 output format */
1834 if (available_fmt->num_output_formats && ret < available_fmt->num_output_formats &&
1835 !available_fmt->output_pin_fmts[ret].pin_index) {
1836 memcpy(&process->output_format, &available_fmt->output_pin_fmts[ret].audio_fmt,
1837 sizeof(struct sof_ipc4_audio_format));
1839 /* modify the pipeline params with the pin 0 output format */
1840 ret = sof_ipc4_update_hw_params(sdev, pipeline_params, &process->output_format);
1845 /* update pipeline memory usage */
1846 sof_ipc4_update_pipeline_mem_usage(sdev, swidget, &process->base_config);
1848 /* ipc_config_data is composed of the base_config followed by an optional extension */
1849 memcpy(cfg, &process->base_config, sizeof(struct sof_ipc4_base_module_cfg));
1850 cfg += sizeof(struct sof_ipc4_base_module_cfg);
1852 if (process->init_config == SOF_IPC4_MODULE_INIT_CONFIG_TYPE_BASE_CFG_WITH_EXT) {
1853 struct sof_ipc4_base_module_cfg_ext *base_cfg_ext = process->base_config_ext;
1855 ret = sof_ipc4_process_add_base_cfg_extn(swidget);
1859 memcpy(cfg, base_cfg_ext, process->base_config_ext_size);
1865 static int sof_ipc4_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1867 struct sof_ipc4_control_data *control_data;
1868 struct sof_ipc4_msg *msg;
1871 scontrol->size = struct_size(control_data, chanv, scontrol->num_channels);
1873 /* scontrol->ipc_control_data will be freed in sof_control_unload */
1874 scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1875 if (!scontrol->ipc_control_data)
1878 control_data = scontrol->ipc_control_data;
1879 control_data->index = scontrol->index;
1881 msg = &control_data->msg;
1882 msg->primary = SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_LARGE_CONFIG_SET);
1883 msg->primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
1884 msg->primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
1886 msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_GAIN_PARAM_ID);
1888 /* set default volume values to 0dB in control */
1889 for (i = 0; i < scontrol->num_channels; i++) {
1890 control_data->chanv[i].channel = i;
1891 control_data->chanv[i].value = SOF_IPC4_VOL_ZERO_DB;
1897 static int sof_ipc4_control_load_bytes(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1899 struct sof_ipc4_control_data *control_data;
1900 struct sof_ipc4_msg *msg;
1903 if (scontrol->max_size < (sizeof(*control_data) + sizeof(struct sof_abi_hdr))) {
1904 dev_err(sdev->dev, "insufficient size for a bytes control %s: %zu.\n",
1905 scontrol->name, scontrol->max_size);
1909 if (scontrol->priv_size > scontrol->max_size - sizeof(*control_data)) {
1910 dev_err(sdev->dev, "scontrol %s bytes data size %zu exceeds max %zu.\n",
1911 scontrol->name, scontrol->priv_size,
1912 scontrol->max_size - sizeof(*control_data));
1916 scontrol->size = sizeof(struct sof_ipc4_control_data) + scontrol->priv_size;
1918 scontrol->ipc_control_data = kzalloc(scontrol->max_size, GFP_KERNEL);
1919 if (!scontrol->ipc_control_data)
1922 control_data = scontrol->ipc_control_data;
1923 control_data->index = scontrol->index;
1924 if (scontrol->priv_size > 0) {
1925 memcpy(control_data->data, scontrol->priv, scontrol->priv_size);
1926 kfree(scontrol->priv);
1927 scontrol->priv = NULL;
1929 if (control_data->data->magic != SOF_IPC4_ABI_MAGIC) {
1930 dev_err(sdev->dev, "Wrong ABI magic (%#x) for control: %s\n",
1931 control_data->data->magic, scontrol->name);
1936 /* TODO: check the ABI version */
1938 if (control_data->data->size + sizeof(struct sof_abi_hdr) !=
1939 scontrol->priv_size) {
1940 dev_err(sdev->dev, "Control %s conflict in bytes %zu vs. priv size %zu.\n",
1942 control_data->data->size + sizeof(struct sof_abi_hdr),
1943 scontrol->priv_size);
1949 msg = &control_data->msg;
1950 msg->primary = SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_LARGE_CONFIG_SET);
1951 msg->primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
1952 msg->primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
1957 kfree(scontrol->ipc_control_data);
1958 scontrol->ipc_control_data = NULL;
1962 static int sof_ipc4_control_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1964 switch (scontrol->info_type) {
1965 case SND_SOC_TPLG_CTL_VOLSW:
1966 case SND_SOC_TPLG_CTL_VOLSW_SX:
1967 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1968 return sof_ipc4_control_load_volume(sdev, scontrol);
1969 case SND_SOC_TPLG_CTL_BYTES:
1970 return sof_ipc4_control_load_bytes(sdev, scontrol);
1978 static int sof_ipc4_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
1980 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget;
1981 struct sof_ipc4_fw_data *ipc4_data = sdev->private;
1982 struct sof_ipc4_pipeline *pipeline;
1983 struct sof_ipc4_msg *msg;
1984 void *ipc_data = NULL;
1988 switch (swidget->id) {
1989 case snd_soc_dapm_scheduler:
1990 pipeline = swidget->private;
1992 if (pipeline->use_chain_dma) {
1993 dev_warn(sdev->dev, "use_chain_dma set for scheduler %s",
1994 swidget->widget->name);
1998 dev_dbg(sdev->dev, "pipeline: %d memory pages: %d\n", swidget->pipeline_id,
1999 pipeline->mem_usage);
2001 msg = &pipeline->msg;
2002 msg->primary |= pipeline->mem_usage;
2004 swidget->instance_id = ida_alloc_max(&pipeline_ida, ipc4_data->max_num_pipelines,
2006 if (swidget->instance_id < 0) {
2007 dev_err(sdev->dev, "failed to assign pipeline id for %s: %d\n",
2008 swidget->widget->name, swidget->instance_id);
2009 return swidget->instance_id;
2011 msg->primary &= ~SOF_IPC4_GLB_PIPE_INSTANCE_MASK;
2012 msg->primary |= SOF_IPC4_GLB_PIPE_INSTANCE_ID(swidget->instance_id);
2014 case snd_soc_dapm_aif_in:
2015 case snd_soc_dapm_aif_out:
2016 case snd_soc_dapm_buffer:
2018 struct sof_ipc4_copier *ipc4_copier = swidget->private;
2020 pipeline = pipe_widget->private;
2021 if (pipeline->use_chain_dma)
2024 ipc_size = ipc4_copier->ipc_config_size;
2025 ipc_data = ipc4_copier->ipc_config_data;
2027 msg = &ipc4_copier->msg;
2030 case snd_soc_dapm_dai_in:
2031 case snd_soc_dapm_dai_out:
2033 struct snd_sof_dai *dai = swidget->private;
2034 struct sof_ipc4_copier *ipc4_copier = dai->private;
2036 pipeline = pipe_widget->private;
2037 if (pipeline->use_chain_dma)
2040 ipc_size = ipc4_copier->ipc_config_size;
2041 ipc_data = ipc4_copier->ipc_config_data;
2043 msg = &ipc4_copier->msg;
2046 case snd_soc_dapm_pga:
2048 struct sof_ipc4_gain *gain = swidget->private;
2050 ipc_size = sizeof(struct sof_ipc4_base_module_cfg) +
2051 sizeof(struct sof_ipc4_gain_data);
2057 case snd_soc_dapm_mixer:
2059 struct sof_ipc4_mixer *mixer = swidget->private;
2061 ipc_size = sizeof(mixer->base_config);
2062 ipc_data = &mixer->base_config;
2067 case snd_soc_dapm_src:
2069 struct sof_ipc4_src *src = swidget->private;
2071 ipc_size = sizeof(struct sof_ipc4_base_module_cfg) + sizeof(src->sink_rate);
2077 case snd_soc_dapm_effect:
2079 struct sof_ipc4_process *process = swidget->private;
2081 if (!process->ipc_config_size) {
2082 dev_err(sdev->dev, "module %s has no config data!\n",
2083 swidget->widget->name);
2087 ipc_size = process->ipc_config_size;
2088 ipc_data = process->ipc_config_data;
2090 msg = &process->msg;
2094 dev_err(sdev->dev, "widget type %d not supported", swidget->id);
2098 if (swidget->id != snd_soc_dapm_scheduler) {
2099 ret = sof_ipc4_widget_assign_instance_id(sdev, swidget);
2101 dev_err(sdev->dev, "failed to assign instance id for %s\n",
2102 swidget->widget->name);
2106 msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK;
2107 msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id);
2109 msg->extension &= ~SOF_IPC4_MOD_EXT_PARAM_SIZE_MASK;
2110 msg->extension |= ipc_size >> 2;
2112 msg->extension &= ~SOF_IPC4_MOD_EXT_PPL_ID_MASK;
2113 msg->extension |= SOF_IPC4_MOD_EXT_PPL_ID(pipe_widget->instance_id);
2115 dev_dbg(sdev->dev, "Create widget %s instance %d - pipe %d - core %d\n",
2116 swidget->widget->name, swidget->instance_id, swidget->pipeline_id, swidget->core);
2118 msg->data_size = ipc_size;
2119 msg->data_ptr = ipc_data;
2121 ret = sof_ipc_tx_message_no_reply(sdev->ipc, msg, ipc_size);
2123 dev_err(sdev->dev, "failed to create module %s\n", swidget->widget->name);
2125 if (swidget->id != snd_soc_dapm_scheduler) {
2126 struct sof_ipc4_fw_module *fw_module = swidget->module_info;
2128 ida_free(&fw_module->m_ida, swidget->instance_id);
2130 ida_free(&pipeline_ida, swidget->instance_id);
2137 static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
2139 struct sof_ipc4_fw_module *fw_module = swidget->module_info;
2140 struct sof_ipc4_fw_data *ipc4_data = sdev->private;
2143 mutex_lock(&ipc4_data->pipeline_state_mutex);
2145 /* freeing a pipeline frees all the widgets associated with it */
2146 if (swidget->id == snd_soc_dapm_scheduler) {
2147 struct sof_ipc4_pipeline *pipeline = swidget->private;
2148 struct sof_ipc4_msg msg = {{ 0 }};
2151 if (pipeline->use_chain_dma) {
2152 dev_warn(sdev->dev, "use_chain_dma set for scheduler %s",
2153 swidget->widget->name);
2154 mutex_unlock(&ipc4_data->pipeline_state_mutex);
2158 header = SOF_IPC4_GLB_PIPE_INSTANCE_ID(swidget->instance_id);
2159 header |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_DELETE_PIPELINE);
2160 header |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
2161 header |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);
2163 msg.primary = header;
2165 ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0);
2167 dev_err(sdev->dev, "failed to free pipeline widget %s\n",
2168 swidget->widget->name);
2170 pipeline->mem_usage = 0;
2171 pipeline->state = SOF_IPC4_PIPE_UNINITIALIZED;
2172 ida_free(&pipeline_ida, swidget->instance_id);
2174 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget;
2175 struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
2177 if (!pipeline->use_chain_dma)
2178 ida_free(&fw_module->m_ida, swidget->instance_id);
2181 mutex_unlock(&ipc4_data->pipeline_state_mutex);
2186 static int sof_ipc4_get_queue_id(struct snd_sof_widget *src_widget,
2187 struct snd_sof_widget *sink_widget, bool pin_type)
2189 struct snd_sof_widget *current_swidget;
2190 struct snd_soc_component *scomp;
2191 struct ida *queue_ida;
2192 const char *buddy_name;
2197 if (pin_type == SOF_PIN_TYPE_OUTPUT) {
2198 current_swidget = src_widget;
2199 pin_binding = src_widget->output_pin_binding;
2200 queue_ida = &src_widget->output_queue_ida;
2201 num_pins = src_widget->num_output_pins;
2202 buddy_name = sink_widget->widget->name;
2204 current_swidget = sink_widget;
2205 pin_binding = sink_widget->input_pin_binding;
2206 queue_ida = &sink_widget->input_queue_ida;
2207 num_pins = sink_widget->num_input_pins;
2208 buddy_name = src_widget->widget->name;
2211 scomp = current_swidget->scomp;
2214 dev_err(scomp->dev, "invalid %s num_pins: %d for queue allocation for %s\n",
2215 (pin_type == SOF_PIN_TYPE_OUTPUT ? "output" : "input"),
2216 num_pins, current_swidget->widget->name);
2220 /* If there is only one input/output pin, queue id must be 0 */
2224 /* Allocate queue ID from pin binding array if it is defined in topology. */
2226 for (i = 0; i < num_pins; i++) {
2227 if (!strcmp(pin_binding[i], buddy_name))
2231 * Fail if no queue ID found from pin binding array, so that we don't
2232 * mixed use pin binding array and ida for queue ID allocation.
2234 dev_err(scomp->dev, "no %s queue id found from pin binding array for %s\n",
2235 (pin_type == SOF_PIN_TYPE_OUTPUT ? "output" : "input"),
2236 current_swidget->widget->name);
2240 /* If no pin binding array specified in topology, use ida to allocate one */
2241 return ida_alloc_max(queue_ida, num_pins, GFP_KERNEL);
2244 static void sof_ipc4_put_queue_id(struct snd_sof_widget *swidget, int queue_id,
2247 struct ida *queue_ida;
2251 if (pin_type == SOF_PIN_TYPE_OUTPUT) {
2252 pin_binding = swidget->output_pin_binding;
2253 queue_ida = &swidget->output_queue_ida;
2254 num_pins = swidget->num_output_pins;
2256 pin_binding = swidget->input_pin_binding;
2257 queue_ida = &swidget->input_queue_ida;
2258 num_pins = swidget->num_input_pins;
2261 /* Nothing to free if queue ID is not allocated with ida. */
2262 if (num_pins == 1 || pin_binding)
2265 ida_free(queue_ida, queue_id);
2268 static int sof_ipc4_set_copier_sink_format(struct snd_sof_dev *sdev,
2269 struct snd_sof_widget *src_widget,
2270 struct snd_sof_widget *sink_widget,
2273 struct sof_ipc4_copier_config_set_sink_format format;
2274 const struct sof_ipc_ops *iops = sdev->ipc->ops;
2275 struct sof_ipc4_base_module_cfg *src_config;
2276 const struct sof_ipc4_audio_format *pin_fmt;
2277 struct sof_ipc4_fw_module *fw_module;
2278 struct sof_ipc4_msg msg = {{ 0 }};
2280 dev_dbg(sdev->dev, "%s set copier sink %d format\n",
2281 src_widget->widget->name, sink_id);
2283 if (WIDGET_IS_DAI(src_widget->id)) {
2284 struct snd_sof_dai *dai = src_widget->private;
2286 src_config = dai->private;
2288 src_config = src_widget->private;
2291 fw_module = src_widget->module_info;
2293 format.sink_id = sink_id;
2294 memcpy(&format.source_fmt, &src_config->audio_fmt, sizeof(format.source_fmt));
2296 pin_fmt = sof_ipc4_get_input_pin_audio_fmt(sink_widget, sink_id);
2298 dev_err(sdev->dev, "Unable to get pin %d format for %s",
2299 sink_id, sink_widget->widget->name);
2303 memcpy(&format.sink_fmt, pin_fmt, sizeof(format.sink_fmt));
2305 msg.data_size = sizeof(format);
2306 msg.data_ptr = &format;
2308 msg.primary = fw_module->man4_module_entry.id;
2309 msg.primary |= SOF_IPC4_MOD_INSTANCE(src_widget->instance_id);
2310 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
2311 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
2314 SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_COPIER_MODULE_CFG_PARAM_SET_SINK_FORMAT);
2316 return iops->set_get_data(sdev, &msg, msg.data_size, true);
2319 static int sof_ipc4_route_setup(struct snd_sof_dev *sdev, struct snd_sof_route *sroute)
2321 struct snd_sof_widget *src_widget = sroute->src_widget;
2322 struct snd_sof_widget *sink_widget = sroute->sink_widget;
2323 struct snd_sof_widget *src_pipe_widget = src_widget->spipe->pipe_widget;
2324 struct snd_sof_widget *sink_pipe_widget = sink_widget->spipe->pipe_widget;
2325 struct sof_ipc4_fw_module *src_fw_module = src_widget->module_info;
2326 struct sof_ipc4_fw_module *sink_fw_module = sink_widget->module_info;
2327 struct sof_ipc4_pipeline *src_pipeline = src_pipe_widget->private;
2328 struct sof_ipc4_pipeline *sink_pipeline = sink_pipe_widget->private;
2329 struct sof_ipc4_msg msg = {{ 0 }};
2330 u32 header, extension;
2333 /* no route set up if chain DMA is used */
2334 if (src_pipeline->use_chain_dma || sink_pipeline->use_chain_dma) {
2335 if (!src_pipeline->use_chain_dma || !sink_pipeline->use_chain_dma) {
2337 "use_chain_dma must be set for both src %s and sink %s pipelines\n",
2338 src_widget->widget->name, sink_widget->widget->name);
2344 if (!src_fw_module || !sink_fw_module) {
2346 "cannot bind %s -> %s, no firmware module for: %s%s\n",
2347 src_widget->widget->name, sink_widget->widget->name,
2348 src_fw_module ? "" : " source",
2349 sink_fw_module ? "" : " sink");
2354 sroute->src_queue_id = sof_ipc4_get_queue_id(src_widget, sink_widget,
2355 SOF_PIN_TYPE_OUTPUT);
2356 if (sroute->src_queue_id < 0) {
2357 dev_err(sdev->dev, "failed to get queue ID for source widget: %s\n",
2358 src_widget->widget->name);
2359 return sroute->src_queue_id;
2362 sroute->dst_queue_id = sof_ipc4_get_queue_id(src_widget, sink_widget,
2363 SOF_PIN_TYPE_INPUT);
2364 if (sroute->dst_queue_id < 0) {
2365 dev_err(sdev->dev, "failed to get queue ID for sink widget: %s\n",
2366 sink_widget->widget->name);
2367 sof_ipc4_put_queue_id(src_widget, sroute->src_queue_id,
2368 SOF_PIN_TYPE_OUTPUT);
2369 return sroute->dst_queue_id;
2372 /* Pin 0 format is already set during copier module init */
2373 if (sroute->src_queue_id > 0 && WIDGET_IS_COPIER(src_widget->id)) {
2374 ret = sof_ipc4_set_copier_sink_format(sdev, src_widget, sink_widget,
2375 sroute->src_queue_id);
2377 dev_err(sdev->dev, "failed to set sink format for %s source queue ID %d\n",
2378 src_widget->widget->name, sroute->src_queue_id);
2383 dev_dbg(sdev->dev, "bind %s:%d -> %s:%d\n",
2384 src_widget->widget->name, sroute->src_queue_id,
2385 sink_widget->widget->name, sroute->dst_queue_id);
2387 header = src_fw_module->man4_module_entry.id;
2388 header |= SOF_IPC4_MOD_INSTANCE(src_widget->instance_id);
2389 header |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_BIND);
2390 header |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
2391 header |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
2393 extension = sink_fw_module->man4_module_entry.id;
2394 extension |= SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(sink_widget->instance_id);
2395 extension |= SOF_IPC4_MOD_EXT_DST_MOD_QUEUE_ID(sroute->dst_queue_id);
2396 extension |= SOF_IPC4_MOD_EXT_SRC_MOD_QUEUE_ID(sroute->src_queue_id);
2398 msg.primary = header;
2399 msg.extension = extension;
2401 ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0);
2403 dev_err(sdev->dev, "failed to bind modules %s:%d -> %s:%d\n",
2404 src_widget->widget->name, sroute->src_queue_id,
2405 sink_widget->widget->name, sroute->dst_queue_id);
2412 sof_ipc4_put_queue_id(src_widget, sroute->src_queue_id, SOF_PIN_TYPE_OUTPUT);
2413 sof_ipc4_put_queue_id(sink_widget, sroute->dst_queue_id, SOF_PIN_TYPE_INPUT);
2417 static int sof_ipc4_route_free(struct snd_sof_dev *sdev, struct snd_sof_route *sroute)
2419 struct snd_sof_widget *src_widget = sroute->src_widget;
2420 struct snd_sof_widget *sink_widget = sroute->sink_widget;
2421 struct sof_ipc4_fw_module *src_fw_module = src_widget->module_info;
2422 struct sof_ipc4_fw_module *sink_fw_module = sink_widget->module_info;
2423 struct sof_ipc4_msg msg = {{ 0 }};
2424 struct snd_sof_widget *src_pipe_widget = src_widget->spipe->pipe_widget;
2425 struct snd_sof_widget *sink_pipe_widget = sink_widget->spipe->pipe_widget;
2426 struct sof_ipc4_pipeline *src_pipeline = src_pipe_widget->private;
2427 struct sof_ipc4_pipeline *sink_pipeline = sink_pipe_widget->private;
2428 u32 header, extension;
2431 /* no route is set up if chain DMA is used */
2432 if (src_pipeline->use_chain_dma || sink_pipeline->use_chain_dma)
2435 dev_dbg(sdev->dev, "unbind modules %s:%d -> %s:%d\n",
2436 src_widget->widget->name, sroute->src_queue_id,
2437 sink_widget->widget->name, sroute->dst_queue_id);
2440 * routes belonging to the same pipeline will be disconnected by the FW when the pipeline
2441 * is freed. So avoid sending this IPC which will be ignored by the FW anyway.
2443 if (src_widget->spipe->pipe_widget == sink_widget->spipe->pipe_widget)
2446 header = src_fw_module->man4_module_entry.id;
2447 header |= SOF_IPC4_MOD_INSTANCE(src_widget->instance_id);
2448 header |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_UNBIND);
2449 header |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
2450 header |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
2452 extension = sink_fw_module->man4_module_entry.id;
2453 extension |= SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(sink_widget->instance_id);
2454 extension |= SOF_IPC4_MOD_EXT_DST_MOD_QUEUE_ID(sroute->dst_queue_id);
2455 extension |= SOF_IPC4_MOD_EXT_SRC_MOD_QUEUE_ID(sroute->src_queue_id);
2457 msg.primary = header;
2458 msg.extension = extension;
2460 ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0);
2462 dev_err(sdev->dev, "failed to unbind modules %s:%d -> %s:%d\n",
2463 src_widget->widget->name, sroute->src_queue_id,
2464 sink_widget->widget->name, sroute->dst_queue_id);
2466 sof_ipc4_put_queue_id(sink_widget, sroute->dst_queue_id, SOF_PIN_TYPE_INPUT);
2467 sof_ipc4_put_queue_id(src_widget, sroute->src_queue_id, SOF_PIN_TYPE_OUTPUT);
2472 static int sof_ipc4_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
2473 unsigned int flags, struct snd_sof_dai_config_data *data)
2475 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget;
2476 struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
2477 struct snd_sof_dai *dai = swidget->private;
2478 struct sof_ipc4_gtw_attributes *gtw_attr;
2479 struct sof_ipc4_copier_data *copier_data;
2480 struct sof_ipc4_copier *ipc4_copier;
2482 if (!dai || !dai->private) {
2483 dev_err(sdev->dev, "Invalid DAI or DAI private data for %s\n",
2484 swidget->widget->name);
2488 ipc4_copier = (struct sof_ipc4_copier *)dai->private;
2489 copier_data = &ipc4_copier->data;
2494 switch (ipc4_copier->dai_type) {
2495 case SOF_DAI_INTEL_HDA:
2496 if (pipeline->use_chain_dma) {
2497 pipeline->msg.primary &= ~SOF_IPC4_GLB_CHAIN_DMA_LINK_ID_MASK;
2498 pipeline->msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_LINK_ID(data->dai_data);
2501 gtw_attr = ipc4_copier->gtw_attr;
2502 gtw_attr->lp_buffer_alloc = pipeline->lp_mode;
2503 pipeline->skip_during_fe_trigger = true;
2505 case SOF_DAI_INTEL_ALH:
2507 * Do not clear the node ID when this op is invoked with
2508 * SOF_DAI_CONFIG_FLAGS_HW_FREE. It is needed to free the group_ida during
2511 if (flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS) {
2512 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
2513 copier_data->gtw_cfg.node_id |= SOF_IPC4_NODE_INDEX(data->dai_data);
2516 case SOF_DAI_INTEL_DMIC:
2517 case SOF_DAI_INTEL_SSP:
2518 /* nothing to do for SSP/DMIC */
2521 dev_err(sdev->dev, "%s: unsupported dai type %d\n", __func__,
2522 ipc4_copier->dai_type);
2529 static int sof_ipc4_parse_manifest(struct snd_soc_component *scomp, int index,
2530 struct snd_soc_tplg_manifest *man)
2532 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2533 struct sof_ipc4_fw_data *ipc4_data = sdev->private;
2534 struct sof_manifest_tlv *manifest_tlv;
2535 struct sof_manifest *manifest;
2536 u32 size = le32_to_cpu(man->priv.size);
2537 u8 *man_ptr = man->priv.data;
2541 if (!size || size < SOF_IPC4_TPLG_ABI_SIZE) {
2542 dev_err(scomp->dev, "%s: Invalid topology ABI size: %u\n",
2547 manifest = (struct sof_manifest *)man_ptr;
2549 dev_info(scomp->dev,
2550 "Topology: ABI %d:%d:%d Kernel ABI %u:%u:%u\n",
2551 le16_to_cpu(manifest->abi_major), le16_to_cpu(manifest->abi_minor),
2552 le16_to_cpu(manifest->abi_patch),
2553 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
2555 /* TODO: Add ABI compatibility check */
2557 /* no more data after the ABI version */
2558 if (size <= SOF_IPC4_TPLG_ABI_SIZE)
2561 manifest_tlv = manifest->items;
2562 len_check = sizeof(struct sof_manifest);
2563 for (i = 0; i < le16_to_cpu(manifest->count); i++) {
2564 len_check += sizeof(struct sof_manifest_tlv) + le32_to_cpu(manifest_tlv->size);
2565 if (len_check > size)
2568 switch (le32_to_cpu(manifest_tlv->type)) {
2569 case SOF_MANIFEST_DATA_TYPE_NHLT:
2570 /* no NHLT in BIOS, so use the one from topology manifest */
2571 if (ipc4_data->nhlt)
2573 ipc4_data->nhlt = devm_kmemdup(sdev->dev, manifest_tlv->data,
2574 le32_to_cpu(manifest_tlv->size), GFP_KERNEL);
2575 if (!ipc4_data->nhlt)
2579 dev_warn(scomp->dev, "Skipping unknown manifest data type %d\n",
2580 manifest_tlv->type);
2583 man_ptr += sizeof(struct sof_manifest_tlv) + le32_to_cpu(manifest_tlv->size);
2584 manifest_tlv = (struct sof_manifest_tlv *)man_ptr;
2590 static int sof_ipc4_dai_get_clk(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type)
2592 struct sof_ipc4_copier *ipc4_copier = dai->private;
2593 struct snd_soc_tplg_hw_config *hw_config;
2594 struct snd_sof_dai_link *slink;
2595 bool dai_link_found = false;
2596 bool hw_cfg_found = false;
2602 list_for_each_entry(slink, &sdev->dai_link_list, list) {
2603 if (!strcmp(slink->link->name, dai->name)) {
2604 dai_link_found = true;
2609 if (!dai_link_found) {
2610 dev_err(sdev->dev, "no DAI link found for DAI %s\n", dai->name);
2614 for (i = 0; i < slink->num_hw_configs; i++) {
2615 hw_config = &slink->hw_configs[i];
2616 if (dai->current_config == le32_to_cpu(hw_config->id)) {
2617 hw_cfg_found = true;
2622 if (!hw_cfg_found) {
2623 dev_err(sdev->dev, "no matching hw_config found for DAI %s\n", dai->name);
2627 switch (ipc4_copier->dai_type) {
2628 case SOF_DAI_INTEL_SSP:
2630 case SOF_DAI_CLK_INTEL_SSP_MCLK:
2631 return le32_to_cpu(hw_config->mclk_rate);
2632 case SOF_DAI_CLK_INTEL_SSP_BCLK:
2633 return le32_to_cpu(hw_config->bclk_rate);
2635 dev_err(sdev->dev, "Invalid clk type for SSP %d\n", clk_type);
2640 dev_err(sdev->dev, "DAI type %d not supported yet!\n", ipc4_copier->dai_type);
2647 static int sof_ipc4_tear_down_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2649 struct snd_sof_pcm *spcm;
2653 * This function is called during system suspend, we need to make sure
2654 * that all streams have been freed up.
2655 * Freeing might have been skipped when xrun happened just at the start
2656 * of the suspend and it sent a SNDRV_PCM_TRIGGER_STOP to the active
2657 * stream. This will call sof_pcm_stream_free() with
2658 * free_widget_list = false which will leave the kernel and firmware out
2659 * of sync during suspend/resume.
2661 * This will also make sure that paused streams handled correctly.
2663 list_for_each_entry(spcm, &sdev->pcm_list, list) {
2664 for_each_pcm_streams(dir) {
2665 struct snd_pcm_substream *substream = spcm->stream[dir].substream;
2667 if (!substream || !substream->runtime || spcm->stream[dir].suspend_ignored)
2670 if (spcm->stream[dir].list) {
2671 ret = sof_pcm_stream_free(sdev, substream, spcm, dir, true);
2680 static int sof_ipc4_link_setup(struct snd_sof_dev *sdev, struct snd_soc_dai_link *link)
2686 * set default trigger order for all links. Exceptions to
2687 * the rule will be handled in sof_pcm_dai_link_fixup()
2688 * For playback, the sequence is the following: start BE,
2689 * start FE, stop FE, stop BE; for Capture the sequence is
2690 * inverted start FE, start BE, stop BE, stop FE
2692 link->trigger[SNDRV_PCM_STREAM_PLAYBACK] = SND_SOC_DPCM_TRIGGER_POST;
2693 link->trigger[SNDRV_PCM_STREAM_CAPTURE] = SND_SOC_DPCM_TRIGGER_PRE;
2698 static enum sof_tokens common_copier_token_list[] = {
2700 SOF_AUDIO_FMT_NUM_TOKENS,
2701 SOF_IN_AUDIO_FORMAT_TOKENS,
2702 SOF_OUT_AUDIO_FORMAT_TOKENS,
2703 SOF_COPIER_DEEP_BUFFER_TOKENS,
2705 SOF_COMP_EXT_TOKENS,
2708 static enum sof_tokens pipeline_token_list[] = {
2710 SOF_PIPELINE_TOKENS,
2713 static enum sof_tokens dai_token_list[] = {
2715 SOF_AUDIO_FMT_NUM_TOKENS,
2716 SOF_IN_AUDIO_FORMAT_TOKENS,
2717 SOF_OUT_AUDIO_FORMAT_TOKENS,
2720 SOF_COMP_EXT_TOKENS,
2723 static enum sof_tokens pga_token_list[] = {
2726 SOF_AUDIO_FMT_NUM_TOKENS,
2727 SOF_IN_AUDIO_FORMAT_TOKENS,
2728 SOF_OUT_AUDIO_FORMAT_TOKENS,
2729 SOF_COMP_EXT_TOKENS,
2732 static enum sof_tokens mixer_token_list[] = {
2734 SOF_AUDIO_FMT_NUM_TOKENS,
2735 SOF_IN_AUDIO_FORMAT_TOKENS,
2736 SOF_OUT_AUDIO_FORMAT_TOKENS,
2737 SOF_COMP_EXT_TOKENS,
2740 static enum sof_tokens src_token_list[] = {
2743 SOF_AUDIO_FMT_NUM_TOKENS,
2744 SOF_IN_AUDIO_FORMAT_TOKENS,
2745 SOF_OUT_AUDIO_FORMAT_TOKENS,
2746 SOF_COMP_EXT_TOKENS,
2749 static enum sof_tokens process_token_list[] = {
2751 SOF_AUDIO_FMT_NUM_TOKENS,
2752 SOF_IN_AUDIO_FORMAT_TOKENS,
2753 SOF_OUT_AUDIO_FORMAT_TOKENS,
2754 SOF_COMP_EXT_TOKENS,
2757 static const struct sof_ipc_tplg_widget_ops tplg_ipc4_widget_ops[SND_SOC_DAPM_TYPE_COUNT] = {
2758 [snd_soc_dapm_aif_in] = {sof_ipc4_widget_setup_pcm, sof_ipc4_widget_free_comp_pcm,
2759 common_copier_token_list, ARRAY_SIZE(common_copier_token_list),
2760 NULL, sof_ipc4_prepare_copier_module,
2761 sof_ipc4_unprepare_copier_module},
2762 [snd_soc_dapm_aif_out] = {sof_ipc4_widget_setup_pcm, sof_ipc4_widget_free_comp_pcm,
2763 common_copier_token_list, ARRAY_SIZE(common_copier_token_list),
2764 NULL, sof_ipc4_prepare_copier_module,
2765 sof_ipc4_unprepare_copier_module},
2766 [snd_soc_dapm_dai_in] = {sof_ipc4_widget_setup_comp_dai, sof_ipc4_widget_free_comp_dai,
2767 dai_token_list, ARRAY_SIZE(dai_token_list), NULL,
2768 sof_ipc4_prepare_copier_module,
2769 sof_ipc4_unprepare_copier_module},
2770 [snd_soc_dapm_dai_out] = {sof_ipc4_widget_setup_comp_dai, sof_ipc4_widget_free_comp_dai,
2771 dai_token_list, ARRAY_SIZE(dai_token_list), NULL,
2772 sof_ipc4_prepare_copier_module,
2773 sof_ipc4_unprepare_copier_module},
2774 [snd_soc_dapm_buffer] = {sof_ipc4_widget_setup_pcm, sof_ipc4_widget_free_comp_pcm,
2775 common_copier_token_list, ARRAY_SIZE(common_copier_token_list),
2776 NULL, sof_ipc4_prepare_copier_module,
2777 sof_ipc4_unprepare_copier_module},
2778 [snd_soc_dapm_scheduler] = {sof_ipc4_widget_setup_comp_pipeline,
2779 sof_ipc4_widget_free_comp_pipeline,
2780 pipeline_token_list, ARRAY_SIZE(pipeline_token_list), NULL,
2782 [snd_soc_dapm_pga] = {sof_ipc4_widget_setup_comp_pga, sof_ipc4_widget_free_comp_pga,
2783 pga_token_list, ARRAY_SIZE(pga_token_list), NULL,
2784 sof_ipc4_prepare_gain_module,
2786 [snd_soc_dapm_mixer] = {sof_ipc4_widget_setup_comp_mixer, sof_ipc4_widget_free_comp_mixer,
2787 mixer_token_list, ARRAY_SIZE(mixer_token_list),
2788 NULL, sof_ipc4_prepare_mixer_module,
2790 [snd_soc_dapm_src] = {sof_ipc4_widget_setup_comp_src, sof_ipc4_widget_free_comp_src,
2791 src_token_list, ARRAY_SIZE(src_token_list),
2792 NULL, sof_ipc4_prepare_src_module,
2794 [snd_soc_dapm_effect] = {sof_ipc4_widget_setup_comp_process,
2795 sof_ipc4_widget_free_comp_process,
2796 process_token_list, ARRAY_SIZE(process_token_list),
2797 NULL, sof_ipc4_prepare_process_module,
2801 const struct sof_ipc_tplg_ops ipc4_tplg_ops = {
2802 .widget = tplg_ipc4_widget_ops,
2803 .token_list = ipc4_token_list,
2804 .control_setup = sof_ipc4_control_setup,
2805 .control = &tplg_ipc4_control_ops,
2806 .widget_setup = sof_ipc4_widget_setup,
2807 .widget_free = sof_ipc4_widget_free,
2808 .route_setup = sof_ipc4_route_setup,
2809 .route_free = sof_ipc4_route_free,
2810 .dai_config = sof_ipc4_dai_config,
2811 .parse_manifest = sof_ipc4_parse_manifest,
2812 .dai_get_clk = sof_ipc4_dai_get_clk,
2813 .tear_down_all_pipelines = sof_ipc4_tear_down_all_pipelines,
2814 .link_setup = sof_ipc4_link_setup,