Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright(c) 2021 Intel Corporation
4//
5// Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6// Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7//
8
9#include <linux/acpi.h>
10#include <acpi/nhlt.h>
11#include <sound/pcm_params.h>
12#include <sound/soc.h>
13#include "avs.h"
14#include "control.h"
15#include "path.h"
16#include "topology.h"
17
18/* Must be called with adev->comp_list_mutex held. */
19static struct avs_tplg *
20avs_path_find_tplg(struct avs_dev *adev, const char *name)
21{
22 struct avs_soc_component *acomp;
23
24 list_for_each_entry(acomp, &adev->comp_list, node)
25 if (!strcmp(acomp->tplg->name, name))
26 return acomp->tplg;
27 return NULL;
28}
29
30static struct avs_path_module *
31avs_path_find_module(struct avs_path_pipeline *ppl, u32 template_id)
32{
33 struct avs_path_module *mod;
34
35 list_for_each_entry(mod, &ppl->mod_list, node)
36 if (mod->template->id == template_id)
37 return mod;
38 return NULL;
39}
40
41static struct avs_path_pipeline *
42avs_path_find_pipeline(struct avs_path *path, u32 template_id)
43{
44 struct avs_path_pipeline *ppl;
45
46 list_for_each_entry(ppl, &path->ppl_list, node)
47 if (ppl->template->id == template_id)
48 return ppl;
49 return NULL;
50}
51
52static struct avs_path *
53avs_path_find_path(struct avs_dev *adev, const char *name, u32 template_id)
54{
55 struct avs_tplg_path_template *pos, *template = NULL;
56 struct avs_tplg *tplg;
57 struct avs_path *path;
58
59 tplg = avs_path_find_tplg(adev, name);
60 if (!tplg)
61 return NULL;
62
63 list_for_each_entry(pos, &tplg->path_tmpl_list, node) {
64 if (pos->id == template_id) {
65 template = pos;
66 break;
67 }
68 }
69 if (!template)
70 return NULL;
71
72 spin_lock(&adev->path_list_lock);
73 /* Only one variant of given path template may be instantiated at a time. */
74 list_for_each_entry(path, &adev->path_list, node) {
75 if (path->template->owner == template) {
76 spin_unlock(&adev->path_list_lock);
77 return path;
78 }
79 }
80
81 spin_unlock(&adev->path_list_lock);
82 return NULL;
83}
84
85static bool avs_test_hw_params(struct snd_pcm_hw_params *params,
86 struct avs_audio_format *fmt)
87{
88 return (params_rate(params) == fmt->sampling_freq &&
89 params_channels(params) == fmt->num_channels &&
90 params_physical_width(params) == fmt->bit_depth &&
91 snd_pcm_hw_params_bits(params) == fmt->valid_bit_depth);
92}
93
94static struct avs_tplg_path *
95avs_path_find_variant(struct avs_dev *adev,
96 struct avs_tplg_path_template *template,
97 struct snd_pcm_hw_params *fe_params,
98 struct snd_pcm_hw_params *be_params)
99{
100 struct avs_tplg_path *variant;
101
102 list_for_each_entry(variant, &template->path_list, node) {
103 dev_dbg(adev->dev, "check FE rate %d chn %d vbd %d bd %d\n",
104 variant->fe_fmt->sampling_freq, variant->fe_fmt->num_channels,
105 variant->fe_fmt->valid_bit_depth, variant->fe_fmt->bit_depth);
106 dev_dbg(adev->dev, "check BE rate %d chn %d vbd %d bd %d\n",
107 variant->be_fmt->sampling_freq, variant->be_fmt->num_channels,
108 variant->be_fmt->valid_bit_depth, variant->be_fmt->bit_depth);
109
110 if (variant->fe_fmt && avs_test_hw_params(fe_params, variant->fe_fmt) &&
111 variant->be_fmt && avs_test_hw_params(be_params, variant->be_fmt))
112 return variant;
113 }
114
115 return NULL;
116}
117
118__maybe_unused
119static bool avs_dma_type_is_host(u32 dma_type)
120{
121 return dma_type == AVS_DMA_HDA_HOST_OUTPUT ||
122 dma_type == AVS_DMA_HDA_HOST_INPUT;
123}
124
125__maybe_unused
126static bool avs_dma_type_is_link(u32 dma_type)
127{
128 return !avs_dma_type_is_host(dma_type);
129}
130
131__maybe_unused
132static bool avs_dma_type_is_output(u32 dma_type)
133{
134 return dma_type == AVS_DMA_HDA_HOST_OUTPUT ||
135 dma_type == AVS_DMA_HDA_LINK_OUTPUT ||
136 dma_type == AVS_DMA_I2S_LINK_OUTPUT;
137}
138
139__maybe_unused
140static bool avs_dma_type_is_input(u32 dma_type)
141{
142 return !avs_dma_type_is_output(dma_type);
143}
144
145static int avs_copier_create(struct avs_dev *adev, struct avs_path_module *mod)
146{
147 struct avs_tplg_module *t = mod->template;
148 struct avs_copier_cfg *cfg;
149 struct acpi_nhlt_format_config *ep_blob;
150 struct acpi_nhlt_endpoint *ep;
151 union avs_connector_node_id node_id = {0};
152 size_t cfg_size, data_size;
153 void *data = NULL;
154 u32 dma_type;
155 int ret;
156
157 data_size = sizeof(cfg->gtw_cfg.config);
158 dma_type = t->cfg_ext->copier.dma_type;
159 node_id.dma_type = dma_type;
160
161 switch (dma_type) {
162 struct avs_audio_format *fmt;
163 int direction;
164
165 case AVS_DMA_I2S_LINK_OUTPUT:
166 case AVS_DMA_I2S_LINK_INPUT:
167 if (avs_dma_type_is_input(dma_type))
168 direction = SNDRV_PCM_STREAM_CAPTURE;
169 else
170 direction = SNDRV_PCM_STREAM_PLAYBACK;
171
172 if (t->cfg_ext->copier.blob_fmt)
173 fmt = t->cfg_ext->copier.blob_fmt;
174 else if (direction == SNDRV_PCM_STREAM_CAPTURE)
175 fmt = t->in_fmt;
176 else
177 fmt = t->cfg_ext->copier.out_fmt;
178
179 ep = acpi_nhlt_find_endpoint(ACPI_NHLT_LINKTYPE_SSP,
180 ACPI_NHLT_DEVICETYPE_CODEC, direction,
181 t->cfg_ext->copier.vindex.i2s.instance);
182 ep_blob = acpi_nhlt_endpoint_find_fmtcfg(ep, fmt->num_channels, fmt->sampling_freq,
183 fmt->valid_bit_depth, fmt->bit_depth);
184 if (!ep_blob) {
185 dev_err(adev->dev, "no I2S ep_blob found\n");
186 return -ENOENT;
187 }
188
189 data = ep_blob->config.capabilities;
190 data_size = ep_blob->config.capabilities_size;
191 /* I2S gateway's vindex is statically assigned in topology */
192 node_id.vindex = t->cfg_ext->copier.vindex.val;
193
194 break;
195
196 case AVS_DMA_DMIC_LINK_INPUT:
197 direction = SNDRV_PCM_STREAM_CAPTURE;
198
199 if (t->cfg_ext->copier.blob_fmt)
200 fmt = t->cfg_ext->copier.blob_fmt;
201 else
202 fmt = t->in_fmt;
203
204 ep = acpi_nhlt_find_endpoint(ACPI_NHLT_LINKTYPE_PDM, -1, direction, 0);
205 ep_blob = acpi_nhlt_endpoint_find_fmtcfg(ep, fmt->num_channels, fmt->sampling_freq,
206 fmt->valid_bit_depth, fmt->bit_depth);
207 if (!ep_blob) {
208 dev_err(adev->dev, "no DMIC ep_blob found\n");
209 return -ENOENT;
210 }
211
212 data = ep_blob->config.capabilities;
213 data_size = ep_blob->config.capabilities_size;
214 /* DMIC gateway's vindex is statically assigned in topology */
215 node_id.vindex = t->cfg_ext->copier.vindex.val;
216
217 break;
218
219 case AVS_DMA_HDA_HOST_OUTPUT:
220 case AVS_DMA_HDA_HOST_INPUT:
221 /* HOST gateway's vindex is dynamically assigned with DMA id */
222 node_id.vindex = mod->owner->owner->dma_id;
223 break;
224
225 case AVS_DMA_HDA_LINK_OUTPUT:
226 case AVS_DMA_HDA_LINK_INPUT:
227 node_id.vindex = t->cfg_ext->copier.vindex.val |
228 mod->owner->owner->dma_id;
229 break;
230
231 case INVALID_OBJECT_ID:
232 default:
233 node_id = INVALID_NODE_ID;
234 break;
235 }
236
237 cfg_size = offsetof(struct avs_copier_cfg, gtw_cfg.config) + data_size;
238 if (cfg_size > AVS_MAILBOX_SIZE)
239 return -EINVAL;
240
241 cfg = adev->modcfg_buf;
242 memset(cfg, 0, cfg_size);
243 cfg->base.cpc = t->cfg_base->cpc;
244 cfg->base.ibs = t->cfg_base->ibs;
245 cfg->base.obs = t->cfg_base->obs;
246 cfg->base.is_pages = t->cfg_base->is_pages;
247 cfg->base.audio_fmt = *t->in_fmt;
248 cfg->out_fmt = *t->cfg_ext->copier.out_fmt;
249 cfg->feature_mask = t->cfg_ext->copier.feature_mask;
250 cfg->gtw_cfg.node_id = node_id;
251 cfg->gtw_cfg.dma_buffer_size = t->cfg_ext->copier.dma_buffer_size;
252 /* config_length in DWORDs */
253 cfg->gtw_cfg.config_length = DIV_ROUND_UP(data_size, 4);
254 if (data)
255 memcpy(&cfg->gtw_cfg.config.blob, data, data_size);
256
257 mod->gtw_attrs = cfg->gtw_cfg.config.attrs;
258
259 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
260 t->core_id, t->domain, cfg, cfg_size,
261 &mod->instance_id);
262 return ret;
263}
264
265static struct avs_control_data *avs_get_module_control(struct avs_path_module *mod)
266{
267 struct avs_tplg_module *t = mod->template;
268 struct avs_tplg_path_template *path_tmpl;
269 struct snd_soc_dapm_widget *w;
270 int i;
271
272 path_tmpl = t->owner->owner->owner;
273 w = path_tmpl->w;
274
275 for (i = 0; i < w->num_kcontrols; i++) {
276 struct avs_control_data *ctl_data;
277 struct soc_mixer_control *mc;
278
279 mc = (struct soc_mixer_control *)w->kcontrols[i]->private_value;
280 ctl_data = (struct avs_control_data *)mc->dobj.private;
281 if (ctl_data->id == t->ctl_id)
282 return ctl_data;
283 }
284
285 return NULL;
286}
287
288static int avs_peakvol_create(struct avs_dev *adev, struct avs_path_module *mod)
289{
290 struct avs_tplg_module *t = mod->template;
291 struct avs_control_data *ctl_data;
292 struct avs_peakvol_cfg *cfg;
293 int volume = S32_MAX;
294 size_t cfg_size;
295 int ret;
296
297 ctl_data = avs_get_module_control(mod);
298 if (ctl_data)
299 volume = ctl_data->volume;
300
301 /* As 2+ channels controls are unsupported, have a single block for all channels. */
302 cfg_size = struct_size(cfg, vols, 1);
303 if (cfg_size > AVS_MAILBOX_SIZE)
304 return -EINVAL;
305
306 cfg = adev->modcfg_buf;
307 memset(cfg, 0, cfg_size);
308 cfg->base.cpc = t->cfg_base->cpc;
309 cfg->base.ibs = t->cfg_base->ibs;
310 cfg->base.obs = t->cfg_base->obs;
311 cfg->base.is_pages = t->cfg_base->is_pages;
312 cfg->base.audio_fmt = *t->in_fmt;
313 cfg->vols[0].target_volume = volume;
314 cfg->vols[0].channel_id = AVS_ALL_CHANNELS_MASK;
315 cfg->vols[0].curve_type = AVS_AUDIO_CURVE_NONE;
316 cfg->vols[0].curve_duration = 0;
317
318 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, t->core_id,
319 t->domain, cfg, cfg_size, &mod->instance_id);
320
321 return ret;
322}
323
324static int avs_updown_mix_create(struct avs_dev *adev, struct avs_path_module *mod)
325{
326 struct avs_tplg_module *t = mod->template;
327 struct avs_updown_mixer_cfg cfg;
328 int i;
329
330 cfg.base.cpc = t->cfg_base->cpc;
331 cfg.base.ibs = t->cfg_base->ibs;
332 cfg.base.obs = t->cfg_base->obs;
333 cfg.base.is_pages = t->cfg_base->is_pages;
334 cfg.base.audio_fmt = *t->in_fmt;
335 cfg.out_channel_config = t->cfg_ext->updown_mix.out_channel_config;
336 cfg.coefficients_select = t->cfg_ext->updown_mix.coefficients_select;
337 for (i = 0; i < AVS_CHANNELS_MAX; i++)
338 cfg.coefficients[i] = t->cfg_ext->updown_mix.coefficients[i];
339 cfg.channel_map = t->cfg_ext->updown_mix.channel_map;
340
341 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
342 t->core_id, t->domain, &cfg, sizeof(cfg),
343 &mod->instance_id);
344}
345
346static int avs_src_create(struct avs_dev *adev, struct avs_path_module *mod)
347{
348 struct avs_tplg_module *t = mod->template;
349 struct avs_src_cfg cfg;
350
351 cfg.base.cpc = t->cfg_base->cpc;
352 cfg.base.ibs = t->cfg_base->ibs;
353 cfg.base.obs = t->cfg_base->obs;
354 cfg.base.is_pages = t->cfg_base->is_pages;
355 cfg.base.audio_fmt = *t->in_fmt;
356 cfg.out_freq = t->cfg_ext->src.out_freq;
357
358 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
359 t->core_id, t->domain, &cfg, sizeof(cfg),
360 &mod->instance_id);
361}
362
363static int avs_asrc_create(struct avs_dev *adev, struct avs_path_module *mod)
364{
365 struct avs_tplg_module *t = mod->template;
366 struct avs_asrc_cfg cfg;
367
368 memset(&cfg, 0, sizeof(cfg));
369 cfg.base.cpc = t->cfg_base->cpc;
370 cfg.base.ibs = t->cfg_base->ibs;
371 cfg.base.obs = t->cfg_base->obs;
372 cfg.base.is_pages = t->cfg_base->is_pages;
373 cfg.base.audio_fmt = *t->in_fmt;
374 cfg.out_freq = t->cfg_ext->asrc.out_freq;
375 cfg.mode = t->cfg_ext->asrc.mode;
376 cfg.disable_jitter_buffer = t->cfg_ext->asrc.disable_jitter_buffer;
377
378 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
379 t->core_id, t->domain, &cfg, sizeof(cfg),
380 &mod->instance_id);
381}
382
383static int avs_aec_create(struct avs_dev *adev, struct avs_path_module *mod)
384{
385 struct avs_tplg_module *t = mod->template;
386 struct avs_aec_cfg cfg;
387
388 cfg.base.cpc = t->cfg_base->cpc;
389 cfg.base.ibs = t->cfg_base->ibs;
390 cfg.base.obs = t->cfg_base->obs;
391 cfg.base.is_pages = t->cfg_base->is_pages;
392 cfg.base.audio_fmt = *t->in_fmt;
393 cfg.ref_fmt = *t->cfg_ext->aec.ref_fmt;
394 cfg.out_fmt = *t->cfg_ext->aec.out_fmt;
395 cfg.cpc_lp_mode = t->cfg_ext->aec.cpc_lp_mode;
396
397 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
398 t->core_id, t->domain, &cfg, sizeof(cfg),
399 &mod->instance_id);
400}
401
402static int avs_mux_create(struct avs_dev *adev, struct avs_path_module *mod)
403{
404 struct avs_tplg_module *t = mod->template;
405 struct avs_mux_cfg cfg;
406
407 cfg.base.cpc = t->cfg_base->cpc;
408 cfg.base.ibs = t->cfg_base->ibs;
409 cfg.base.obs = t->cfg_base->obs;
410 cfg.base.is_pages = t->cfg_base->is_pages;
411 cfg.base.audio_fmt = *t->in_fmt;
412 cfg.ref_fmt = *t->cfg_ext->mux.ref_fmt;
413 cfg.out_fmt = *t->cfg_ext->mux.out_fmt;
414
415 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
416 t->core_id, t->domain, &cfg, sizeof(cfg),
417 &mod->instance_id);
418}
419
420static int avs_wov_create(struct avs_dev *adev, struct avs_path_module *mod)
421{
422 struct avs_tplg_module *t = mod->template;
423 struct avs_wov_cfg cfg;
424
425 cfg.base.cpc = t->cfg_base->cpc;
426 cfg.base.ibs = t->cfg_base->ibs;
427 cfg.base.obs = t->cfg_base->obs;
428 cfg.base.is_pages = t->cfg_base->is_pages;
429 cfg.base.audio_fmt = *t->in_fmt;
430 cfg.cpc_lp_mode = t->cfg_ext->wov.cpc_lp_mode;
431
432 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
433 t->core_id, t->domain, &cfg, sizeof(cfg),
434 &mod->instance_id);
435}
436
437static int avs_micsel_create(struct avs_dev *adev, struct avs_path_module *mod)
438{
439 struct avs_tplg_module *t = mod->template;
440 struct avs_micsel_cfg cfg;
441
442 cfg.base.cpc = t->cfg_base->cpc;
443 cfg.base.ibs = t->cfg_base->ibs;
444 cfg.base.obs = t->cfg_base->obs;
445 cfg.base.is_pages = t->cfg_base->is_pages;
446 cfg.base.audio_fmt = *t->in_fmt;
447 cfg.out_fmt = *t->cfg_ext->micsel.out_fmt;
448
449 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
450 t->core_id, t->domain, &cfg, sizeof(cfg),
451 &mod->instance_id);
452}
453
454static int avs_modbase_create(struct avs_dev *adev, struct avs_path_module *mod)
455{
456 struct avs_tplg_module *t = mod->template;
457 struct avs_modcfg_base cfg;
458
459 cfg.cpc = t->cfg_base->cpc;
460 cfg.ibs = t->cfg_base->ibs;
461 cfg.obs = t->cfg_base->obs;
462 cfg.is_pages = t->cfg_base->is_pages;
463 cfg.audio_fmt = *t->in_fmt;
464
465 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
466 t->core_id, t->domain, &cfg, sizeof(cfg),
467 &mod->instance_id);
468}
469
470static int avs_modext_create(struct avs_dev *adev, struct avs_path_module *mod)
471{
472 struct avs_tplg_module *t = mod->template;
473 struct avs_tplg_modcfg_ext *tcfg = t->cfg_ext;
474 struct avs_modcfg_ext *cfg;
475 size_t cfg_size, num_pins;
476 int ret, i;
477
478 num_pins = tcfg->generic.num_input_pins + tcfg->generic.num_output_pins;
479 cfg_size = struct_size(cfg, pin_fmts, num_pins);
480
481 if (cfg_size > AVS_MAILBOX_SIZE)
482 return -EINVAL;
483
484 cfg = adev->modcfg_buf;
485 memset(cfg, 0, cfg_size);
486 cfg->base.cpc = t->cfg_base->cpc;
487 cfg->base.ibs = t->cfg_base->ibs;
488 cfg->base.obs = t->cfg_base->obs;
489 cfg->base.is_pages = t->cfg_base->is_pages;
490 cfg->base.audio_fmt = *t->in_fmt;
491 cfg->num_input_pins = tcfg->generic.num_input_pins;
492 cfg->num_output_pins = tcfg->generic.num_output_pins;
493
494 /* configure pin formats */
495 for (i = 0; i < num_pins; i++) {
496 struct avs_tplg_pin_format *tpin = &tcfg->generic.pin_fmts[i];
497 struct avs_pin_format *pin = &cfg->pin_fmts[i];
498
499 pin->pin_index = tpin->pin_index;
500 pin->iobs = tpin->iobs;
501 pin->audio_fmt = *tpin->fmt;
502 }
503
504 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
505 t->core_id, t->domain, cfg, cfg_size,
506 &mod->instance_id);
507 return ret;
508}
509
510static int avs_probe_create(struct avs_dev *adev, struct avs_path_module *mod)
511{
512 dev_err(adev->dev, "Probe module can't be instantiated by topology");
513 return -EINVAL;
514}
515
516struct avs_module_create {
517 guid_t *guid;
518 int (*create)(struct avs_dev *adev, struct avs_path_module *mod);
519};
520
521static struct avs_module_create avs_module_create[] = {
522 { &AVS_MIXIN_MOD_UUID, avs_modbase_create },
523 { &AVS_MIXOUT_MOD_UUID, avs_modbase_create },
524 { &AVS_KPBUFF_MOD_UUID, avs_modbase_create },
525 { &AVS_COPIER_MOD_UUID, avs_copier_create },
526 { &AVS_PEAKVOL_MOD_UUID, avs_peakvol_create },
527 { &AVS_GAIN_MOD_UUID, avs_peakvol_create },
528 { &AVS_MICSEL_MOD_UUID, avs_micsel_create },
529 { &AVS_MUX_MOD_UUID, avs_mux_create },
530 { &AVS_UPDWMIX_MOD_UUID, avs_updown_mix_create },
531 { &AVS_SRCINTC_MOD_UUID, avs_src_create },
532 { &AVS_AEC_MOD_UUID, avs_aec_create },
533 { &AVS_ASRC_MOD_UUID, avs_asrc_create },
534 { &AVS_INTELWOV_MOD_UUID, avs_wov_create },
535 { &AVS_PROBE_MOD_UUID, avs_probe_create },
536};
537
538static int avs_path_module_type_create(struct avs_dev *adev, struct avs_path_module *mod)
539{
540 const guid_t *type = &mod->template->cfg_ext->type;
541
542 for (int i = 0; i < ARRAY_SIZE(avs_module_create); i++)
543 if (guid_equal(type, avs_module_create[i].guid))
544 return avs_module_create[i].create(adev, mod);
545
546 return avs_modext_create(adev, mod);
547}
548
549static int avs_path_module_send_init_configs(struct avs_dev *adev, struct avs_path_module *mod)
550{
551 struct avs_soc_component *acomp;
552
553 acomp = to_avs_soc_component(mod->template->owner->owner->owner->owner->comp);
554
555 u32 num_ids = mod->template->num_config_ids;
556 u32 *ids = mod->template->config_ids;
557
558 for (int i = 0; i < num_ids; i++) {
559 struct avs_tplg_init_config *config = &acomp->tplg->init_configs[ids[i]];
560 size_t len = config->length;
561 void *data = config->data;
562 u32 param = config->param;
563 int ret;
564
565 ret = avs_ipc_set_large_config(adev, mod->module_id, mod->instance_id,
566 param, data, len);
567 if (ret) {
568 dev_err(adev->dev, "send initial module config failed: %d\n", ret);
569 return AVS_IPC_RET(ret);
570 }
571 }
572
573 return 0;
574}
575
576static void avs_path_module_free(struct avs_dev *adev, struct avs_path_module *mod)
577{
578 kfree(mod);
579}
580
581static struct avs_path_module *
582avs_path_module_create(struct avs_dev *adev,
583 struct avs_path_pipeline *owner,
584 struct avs_tplg_module *template)
585{
586 struct avs_path_module *mod;
587 int module_id, ret;
588
589 module_id = avs_get_module_id(adev, &template->cfg_ext->type);
590 if (module_id < 0)
591 return ERR_PTR(module_id);
592
593 mod = kzalloc(sizeof(*mod), GFP_KERNEL);
594 if (!mod)
595 return ERR_PTR(-ENOMEM);
596
597 mod->template = template;
598 mod->module_id = module_id;
599 mod->owner = owner;
600 INIT_LIST_HEAD(&mod->node);
601
602 ret = avs_path_module_type_create(adev, mod);
603 if (ret) {
604 dev_err(adev->dev, "module-type create failed: %d\n", ret);
605 kfree(mod);
606 return ERR_PTR(ret);
607 }
608
609 ret = avs_path_module_send_init_configs(adev, mod);
610 if (ret) {
611 kfree(mod);
612 return ERR_PTR(ret);
613 }
614
615 return mod;
616}
617
618static int avs_path_binding_arm(struct avs_dev *adev, struct avs_path_binding *binding)
619{
620 struct avs_path_module *this_mod, *target_mod;
621 struct avs_path_pipeline *target_ppl;
622 struct avs_path *target_path;
623 struct avs_tplg_binding *t;
624
625 t = binding->template;
626 this_mod = avs_path_find_module(binding->owner,
627 t->mod_id);
628 if (!this_mod) {
629 dev_err(adev->dev, "path mod %d not found\n", t->mod_id);
630 return -EINVAL;
631 }
632
633 /* update with target_tplg_name too */
634 target_path = avs_path_find_path(adev, t->target_tplg_name,
635 t->target_path_tmpl_id);
636 if (!target_path) {
637 dev_err(adev->dev, "target path %s:%d not found\n",
638 t->target_tplg_name, t->target_path_tmpl_id);
639 return -EINVAL;
640 }
641
642 target_ppl = avs_path_find_pipeline(target_path,
643 t->target_ppl_id);
644 if (!target_ppl) {
645 dev_err(adev->dev, "target ppl %d not found\n", t->target_ppl_id);
646 return -EINVAL;
647 }
648
649 target_mod = avs_path_find_module(target_ppl, t->target_mod_id);
650 if (!target_mod) {
651 dev_err(adev->dev, "target mod %d not found\n", t->target_mod_id);
652 return -EINVAL;
653 }
654
655 if (t->is_sink) {
656 binding->sink = this_mod;
657 binding->sink_pin = t->mod_pin;
658 binding->source = target_mod;
659 binding->source_pin = t->target_mod_pin;
660 } else {
661 binding->sink = target_mod;
662 binding->sink_pin = t->target_mod_pin;
663 binding->source = this_mod;
664 binding->source_pin = t->mod_pin;
665 }
666
667 return 0;
668}
669
670static void avs_path_binding_free(struct avs_dev *adev, struct avs_path_binding *binding)
671{
672 kfree(binding);
673}
674
675static struct avs_path_binding *avs_path_binding_create(struct avs_dev *adev,
676 struct avs_path_pipeline *owner,
677 struct avs_tplg_binding *t)
678{
679 struct avs_path_binding *binding;
680
681 binding = kzalloc(sizeof(*binding), GFP_KERNEL);
682 if (!binding)
683 return ERR_PTR(-ENOMEM);
684
685 binding->template = t;
686 binding->owner = owner;
687 INIT_LIST_HEAD(&binding->node);
688
689 return binding;
690}
691
692static int avs_path_pipeline_arm(struct avs_dev *adev,
693 struct avs_path_pipeline *ppl)
694{
695 struct avs_path_module *mod;
696
697 list_for_each_entry(mod, &ppl->mod_list, node) {
698 struct avs_path_module *source, *sink;
699 int ret;
700
701 /*
702 * Only one module (so it's implicitly last) or it is the last
703 * one, either way we don't have next module to bind it to.
704 */
705 if (mod == list_last_entry(&ppl->mod_list,
706 struct avs_path_module, node))
707 break;
708
709 /* bind current module to next module on list */
710 source = mod;
711 sink = list_next_entry(mod, node);
712
713 ret = avs_ipc_bind(adev, source->module_id, source->instance_id,
714 sink->module_id, sink->instance_id, 0, 0);
715 if (ret)
716 return AVS_IPC_RET(ret);
717 }
718
719 return 0;
720}
721
722static void avs_path_pipeline_free(struct avs_dev *adev,
723 struct avs_path_pipeline *ppl)
724{
725 struct avs_path_binding *binding, *bsave;
726 struct avs_path_module *mod, *save;
727
728 list_for_each_entry_safe(binding, bsave, &ppl->binding_list, node) {
729 list_del(&binding->node);
730 avs_path_binding_free(adev, binding);
731 }
732
733 avs_dsp_delete_pipeline(adev, ppl->instance_id);
734
735 /* Unload resources occupied by owned modules */
736 list_for_each_entry_safe(mod, save, &ppl->mod_list, node) {
737 avs_dsp_delete_module(adev, mod->module_id, mod->instance_id,
738 mod->owner->instance_id,
739 mod->template->core_id);
740 avs_path_module_free(adev, mod);
741 }
742
743 list_del(&ppl->node);
744 kfree(ppl);
745}
746
747static struct avs_path_pipeline *
748avs_path_pipeline_create(struct avs_dev *adev, struct avs_path *owner,
749 struct avs_tplg_pipeline *template)
750{
751 struct avs_path_pipeline *ppl;
752 struct avs_tplg_pplcfg *cfg = template->cfg;
753 struct avs_tplg_module *tmod;
754 int ret, i;
755
756 ppl = kzalloc(sizeof(*ppl), GFP_KERNEL);
757 if (!ppl)
758 return ERR_PTR(-ENOMEM);
759
760 ppl->template = template;
761 ppl->owner = owner;
762 INIT_LIST_HEAD(&ppl->binding_list);
763 INIT_LIST_HEAD(&ppl->mod_list);
764 INIT_LIST_HEAD(&ppl->node);
765
766 ret = avs_dsp_create_pipeline(adev, cfg->req_size, cfg->priority,
767 cfg->lp, cfg->attributes,
768 &ppl->instance_id);
769 if (ret) {
770 dev_err(adev->dev, "error creating pipeline %d\n", ret);
771 kfree(ppl);
772 return ERR_PTR(ret);
773 }
774
775 list_for_each_entry(tmod, &template->mod_list, node) {
776 struct avs_path_module *mod;
777
778 mod = avs_path_module_create(adev, ppl, tmod);
779 if (IS_ERR(mod)) {
780 ret = PTR_ERR(mod);
781 dev_err(adev->dev, "error creating module %d\n", ret);
782 goto init_err;
783 }
784
785 list_add_tail(&mod->node, &ppl->mod_list);
786 }
787
788 for (i = 0; i < template->num_bindings; i++) {
789 struct avs_path_binding *binding;
790
791 binding = avs_path_binding_create(adev, ppl, template->bindings[i]);
792 if (IS_ERR(binding)) {
793 ret = PTR_ERR(binding);
794 dev_err(adev->dev, "error creating binding %d\n", ret);
795 goto init_err;
796 }
797
798 list_add_tail(&binding->node, &ppl->binding_list);
799 }
800
801 return ppl;
802
803init_err:
804 avs_path_pipeline_free(adev, ppl);
805 return ERR_PTR(ret);
806}
807
808static int avs_path_init(struct avs_dev *adev, struct avs_path *path,
809 struct avs_tplg_path *template, u32 dma_id)
810{
811 struct avs_tplg_pipeline *tppl;
812
813 path->owner = adev;
814 path->template = template;
815 path->dma_id = dma_id;
816 INIT_LIST_HEAD(&path->ppl_list);
817 INIT_LIST_HEAD(&path->node);
818
819 /* create all the pipelines */
820 list_for_each_entry(tppl, &template->ppl_list, node) {
821 struct avs_path_pipeline *ppl;
822
823 ppl = avs_path_pipeline_create(adev, path, tppl);
824 if (IS_ERR(ppl))
825 return PTR_ERR(ppl);
826
827 list_add_tail(&ppl->node, &path->ppl_list);
828 }
829
830 spin_lock(&adev->path_list_lock);
831 list_add_tail(&path->node, &adev->path_list);
832 spin_unlock(&adev->path_list_lock);
833
834 return 0;
835}
836
837static int avs_path_arm(struct avs_dev *adev, struct avs_path *path)
838{
839 struct avs_path_pipeline *ppl;
840 struct avs_path_binding *binding;
841 int ret;
842
843 list_for_each_entry(ppl, &path->ppl_list, node) {
844 /*
845 * Arm all ppl bindings before binding internal modules
846 * as it costs no IPCs which isn't true for the latter.
847 */
848 list_for_each_entry(binding, &ppl->binding_list, node) {
849 ret = avs_path_binding_arm(adev, binding);
850 if (ret < 0)
851 return ret;
852 }
853
854 ret = avs_path_pipeline_arm(adev, ppl);
855 if (ret < 0)
856 return ret;
857 }
858
859 return 0;
860}
861
862static void avs_path_free_unlocked(struct avs_path *path)
863{
864 struct avs_path_pipeline *ppl, *save;
865
866 spin_lock(&path->owner->path_list_lock);
867 list_del(&path->node);
868 spin_unlock(&path->owner->path_list_lock);
869
870 list_for_each_entry_safe(ppl, save, &path->ppl_list, node)
871 avs_path_pipeline_free(path->owner, ppl);
872
873 kfree(path);
874}
875
876static struct avs_path *avs_path_create_unlocked(struct avs_dev *adev, u32 dma_id,
877 struct avs_tplg_path *template)
878{
879 struct avs_path *path;
880 int ret;
881
882 path = kzalloc(sizeof(*path), GFP_KERNEL);
883 if (!path)
884 return ERR_PTR(-ENOMEM);
885
886 ret = avs_path_init(adev, path, template, dma_id);
887 if (ret < 0)
888 goto err;
889
890 ret = avs_path_arm(adev, path);
891 if (ret < 0)
892 goto err;
893
894 path->state = AVS_PPL_STATE_INVALID;
895 return path;
896err:
897 avs_path_free_unlocked(path);
898 return ERR_PTR(ret);
899}
900
901void avs_path_free(struct avs_path *path)
902{
903 struct avs_dev *adev = path->owner;
904
905 mutex_lock(&adev->path_mutex);
906 avs_path_free_unlocked(path);
907 mutex_unlock(&adev->path_mutex);
908}
909
910struct avs_path *avs_path_create(struct avs_dev *adev, u32 dma_id,
911 struct avs_tplg_path_template *template,
912 struct snd_pcm_hw_params *fe_params,
913 struct snd_pcm_hw_params *be_params)
914{
915 struct avs_tplg_path *variant;
916 struct avs_path *path;
917
918 variant = avs_path_find_variant(adev, template, fe_params, be_params);
919 if (!variant) {
920 dev_err(adev->dev, "no matching variant found\n");
921 return ERR_PTR(-ENOENT);
922 }
923
924 /* Serialize path and its components creation. */
925 mutex_lock(&adev->path_mutex);
926 /* Satisfy needs of avs_path_find_tplg(). */
927 mutex_lock(&adev->comp_list_mutex);
928
929 path = avs_path_create_unlocked(adev, dma_id, variant);
930
931 mutex_unlock(&adev->comp_list_mutex);
932 mutex_unlock(&adev->path_mutex);
933
934 return path;
935}
936
937static int avs_path_bind_prepare(struct avs_dev *adev,
938 struct avs_path_binding *binding)
939{
940 const struct avs_audio_format *src_fmt, *sink_fmt;
941 struct avs_tplg_module *tsource = binding->source->template;
942 struct avs_path_module *source = binding->source;
943 int ret;
944
945 /*
946 * only copier modules about to be bound
947 * to output pin other than 0 need preparation
948 */
949 if (!binding->source_pin)
950 return 0;
951 if (!guid_equal(&tsource->cfg_ext->type, &AVS_COPIER_MOD_UUID))
952 return 0;
953
954 src_fmt = tsource->in_fmt;
955 sink_fmt = binding->sink->template->in_fmt;
956
957 ret = avs_ipc_copier_set_sink_format(adev, source->module_id,
958 source->instance_id, binding->source_pin,
959 src_fmt, sink_fmt);
960 if (ret) {
961 dev_err(adev->dev, "config copier failed: %d\n", ret);
962 return AVS_IPC_RET(ret);
963 }
964
965 return 0;
966}
967
968int avs_path_bind(struct avs_path *path)
969{
970 struct avs_path_pipeline *ppl;
971 struct avs_dev *adev = path->owner;
972 int ret;
973
974 list_for_each_entry(ppl, &path->ppl_list, node) {
975 struct avs_path_binding *binding;
976
977 list_for_each_entry(binding, &ppl->binding_list, node) {
978 struct avs_path_module *source, *sink;
979
980 source = binding->source;
981 sink = binding->sink;
982
983 ret = avs_path_bind_prepare(adev, binding);
984 if (ret < 0)
985 return ret;
986
987 ret = avs_ipc_bind(adev, source->module_id,
988 source->instance_id, sink->module_id,
989 sink->instance_id, binding->sink_pin,
990 binding->source_pin);
991 if (ret) {
992 dev_err(adev->dev, "bind path failed: %d\n", ret);
993 return AVS_IPC_RET(ret);
994 }
995 }
996 }
997
998 return 0;
999}
1000
1001int avs_path_unbind(struct avs_path *path)
1002{
1003 struct avs_path_pipeline *ppl;
1004 struct avs_dev *adev = path->owner;
1005 int ret;
1006
1007 list_for_each_entry(ppl, &path->ppl_list, node) {
1008 struct avs_path_binding *binding;
1009
1010 list_for_each_entry(binding, &ppl->binding_list, node) {
1011 struct avs_path_module *source, *sink;
1012
1013 source = binding->source;
1014 sink = binding->sink;
1015
1016 ret = avs_ipc_unbind(adev, source->module_id,
1017 source->instance_id, sink->module_id,
1018 sink->instance_id, binding->sink_pin,
1019 binding->source_pin);
1020 if (ret) {
1021 dev_err(adev->dev, "unbind path failed: %d\n", ret);
1022 return AVS_IPC_RET(ret);
1023 }
1024 }
1025 }
1026
1027 return 0;
1028}
1029
1030int avs_path_reset(struct avs_path *path)
1031{
1032 struct avs_path_pipeline *ppl;
1033 struct avs_dev *adev = path->owner;
1034 int ret;
1035
1036 if (path->state == AVS_PPL_STATE_RESET)
1037 return 0;
1038
1039 list_for_each_entry(ppl, &path->ppl_list, node) {
1040 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
1041 AVS_PPL_STATE_RESET);
1042 if (ret) {
1043 dev_err(adev->dev, "reset path failed: %d\n", ret);
1044 path->state = AVS_PPL_STATE_INVALID;
1045 return AVS_IPC_RET(ret);
1046 }
1047 }
1048
1049 path->state = AVS_PPL_STATE_RESET;
1050 return 0;
1051}
1052
1053int avs_path_pause(struct avs_path *path)
1054{
1055 struct avs_path_pipeline *ppl;
1056 struct avs_dev *adev = path->owner;
1057 int ret;
1058
1059 if (path->state == AVS_PPL_STATE_PAUSED)
1060 return 0;
1061
1062 list_for_each_entry_reverse(ppl, &path->ppl_list, node) {
1063 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
1064 AVS_PPL_STATE_PAUSED);
1065 if (ret) {
1066 dev_err(adev->dev, "pause path failed: %d\n", ret);
1067 path->state = AVS_PPL_STATE_INVALID;
1068 return AVS_IPC_RET(ret);
1069 }
1070 }
1071
1072 path->state = AVS_PPL_STATE_PAUSED;
1073 return 0;
1074}
1075
1076int avs_path_run(struct avs_path *path, int trigger)
1077{
1078 struct avs_path_pipeline *ppl;
1079 struct avs_dev *adev = path->owner;
1080 int ret;
1081
1082 if (path->state == AVS_PPL_STATE_RUNNING && trigger == AVS_TPLG_TRIGGER_AUTO)
1083 return 0;
1084
1085 list_for_each_entry(ppl, &path->ppl_list, node) {
1086 if (ppl->template->cfg->trigger != trigger)
1087 continue;
1088
1089 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
1090 AVS_PPL_STATE_RUNNING);
1091 if (ret) {
1092 dev_err(adev->dev, "run path failed: %d\n", ret);
1093 path->state = AVS_PPL_STATE_INVALID;
1094 return AVS_IPC_RET(ret);
1095 }
1096 }
1097
1098 path->state = AVS_PPL_STATE_RUNNING;
1099 return 0;
1100}
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright(c) 2021 Intel Corporation. All rights reserved.
4//
5// Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6// Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7//
8
9#include <sound/intel-nhlt.h>
10#include <sound/pcm_params.h>
11#include <sound/soc.h>
12#include "avs.h"
13#include "path.h"
14#include "topology.h"
15
16/* Must be called with adev->comp_list_mutex held. */
17static struct avs_tplg *
18avs_path_find_tplg(struct avs_dev *adev, const char *name)
19{
20 struct avs_soc_component *acomp;
21
22 list_for_each_entry(acomp, &adev->comp_list, node)
23 if (!strcmp(acomp->tplg->name, name))
24 return acomp->tplg;
25 return NULL;
26}
27
28static struct avs_path_module *
29avs_path_find_module(struct avs_path_pipeline *ppl, u32 template_id)
30{
31 struct avs_path_module *mod;
32
33 list_for_each_entry(mod, &ppl->mod_list, node)
34 if (mod->template->id == template_id)
35 return mod;
36 return NULL;
37}
38
39static struct avs_path_pipeline *
40avs_path_find_pipeline(struct avs_path *path, u32 template_id)
41{
42 struct avs_path_pipeline *ppl;
43
44 list_for_each_entry(ppl, &path->ppl_list, node)
45 if (ppl->template->id == template_id)
46 return ppl;
47 return NULL;
48}
49
50static struct avs_path *
51avs_path_find_path(struct avs_dev *adev, const char *name, u32 template_id)
52{
53 struct avs_tplg_path_template *pos, *template = NULL;
54 struct avs_tplg *tplg;
55 struct avs_path *path;
56
57 tplg = avs_path_find_tplg(adev, name);
58 if (!tplg)
59 return NULL;
60
61 list_for_each_entry(pos, &tplg->path_tmpl_list, node) {
62 if (pos->id == template_id) {
63 template = pos;
64 break;
65 }
66 }
67 if (!template)
68 return NULL;
69
70 spin_lock(&adev->path_list_lock);
71 /* Only one variant of given path template may be instantiated at a time. */
72 list_for_each_entry(path, &adev->path_list, node) {
73 if (path->template->owner == template) {
74 spin_unlock(&adev->path_list_lock);
75 return path;
76 }
77 }
78
79 spin_unlock(&adev->path_list_lock);
80 return NULL;
81}
82
83static bool avs_test_hw_params(struct snd_pcm_hw_params *params,
84 struct avs_audio_format *fmt)
85{
86 return (params_rate(params) == fmt->sampling_freq &&
87 params_channels(params) == fmt->num_channels &&
88 params_physical_width(params) == fmt->bit_depth &&
89 params_width(params) == fmt->valid_bit_depth);
90}
91
92static struct avs_tplg_path *
93avs_path_find_variant(struct avs_dev *adev,
94 struct avs_tplg_path_template *template,
95 struct snd_pcm_hw_params *fe_params,
96 struct snd_pcm_hw_params *be_params)
97{
98 struct avs_tplg_path *variant;
99
100 list_for_each_entry(variant, &template->path_list, node) {
101 dev_dbg(adev->dev, "check FE rate %d chn %d vbd %d bd %d\n",
102 variant->fe_fmt->sampling_freq, variant->fe_fmt->num_channels,
103 variant->fe_fmt->valid_bit_depth, variant->fe_fmt->bit_depth);
104 dev_dbg(adev->dev, "check BE rate %d chn %d vbd %d bd %d\n",
105 variant->be_fmt->sampling_freq, variant->be_fmt->num_channels,
106 variant->be_fmt->valid_bit_depth, variant->be_fmt->bit_depth);
107
108 if (variant->fe_fmt && avs_test_hw_params(fe_params, variant->fe_fmt) &&
109 variant->be_fmt && avs_test_hw_params(be_params, variant->be_fmt))
110 return variant;
111 }
112
113 return NULL;
114}
115
116__maybe_unused
117static bool avs_dma_type_is_host(u32 dma_type)
118{
119 return dma_type == AVS_DMA_HDA_HOST_OUTPUT ||
120 dma_type == AVS_DMA_HDA_HOST_INPUT;
121}
122
123__maybe_unused
124static bool avs_dma_type_is_link(u32 dma_type)
125{
126 return !avs_dma_type_is_host(dma_type);
127}
128
129__maybe_unused
130static bool avs_dma_type_is_output(u32 dma_type)
131{
132 return dma_type == AVS_DMA_HDA_HOST_OUTPUT ||
133 dma_type == AVS_DMA_HDA_LINK_OUTPUT ||
134 dma_type == AVS_DMA_I2S_LINK_OUTPUT;
135}
136
137__maybe_unused
138static bool avs_dma_type_is_input(u32 dma_type)
139{
140 return !avs_dma_type_is_output(dma_type);
141}
142
143static int avs_copier_create(struct avs_dev *adev, struct avs_path_module *mod)
144{
145 struct nhlt_acpi_table *nhlt = adev->nhlt;
146 struct avs_tplg_module *t = mod->template;
147 struct avs_copier_cfg *cfg;
148 struct nhlt_specific_cfg *ep_blob;
149 union avs_connector_node_id node_id = {0};
150 size_t cfg_size, data_size = 0;
151 void *data = NULL;
152 u32 dma_type;
153 int ret;
154
155 dma_type = t->cfg_ext->copier.dma_type;
156 node_id.dma_type = dma_type;
157
158 switch (dma_type) {
159 struct avs_audio_format *fmt;
160 int direction;
161
162 case AVS_DMA_I2S_LINK_OUTPUT:
163 case AVS_DMA_I2S_LINK_INPUT:
164 if (avs_dma_type_is_input(dma_type))
165 direction = SNDRV_PCM_STREAM_CAPTURE;
166 else
167 direction = SNDRV_PCM_STREAM_PLAYBACK;
168
169 if (t->cfg_ext->copier.blob_fmt)
170 fmt = t->cfg_ext->copier.blob_fmt;
171 else if (direction == SNDRV_PCM_STREAM_CAPTURE)
172 fmt = t->in_fmt;
173 else
174 fmt = t->cfg_ext->copier.out_fmt;
175
176 ep_blob = intel_nhlt_get_endpoint_blob(adev->dev,
177 nhlt, t->cfg_ext->copier.vindex.i2s.instance,
178 NHLT_LINK_SSP, fmt->valid_bit_depth, fmt->bit_depth,
179 fmt->num_channels, fmt->sampling_freq, direction,
180 NHLT_DEVICE_I2S);
181 if (!ep_blob) {
182 dev_err(adev->dev, "no I2S ep_blob found\n");
183 return -ENOENT;
184 }
185
186 data = ep_blob->caps;
187 data_size = ep_blob->size;
188 /* I2S gateway's vindex is statically assigned in topology */
189 node_id.vindex = t->cfg_ext->copier.vindex.val;
190
191 break;
192
193 case AVS_DMA_DMIC_LINK_INPUT:
194 direction = SNDRV_PCM_STREAM_CAPTURE;
195
196 if (t->cfg_ext->copier.blob_fmt)
197 fmt = t->cfg_ext->copier.blob_fmt;
198 else
199 fmt = t->in_fmt;
200
201 ep_blob = intel_nhlt_get_endpoint_blob(adev->dev, nhlt, 0,
202 NHLT_LINK_DMIC, fmt->valid_bit_depth,
203 fmt->bit_depth, fmt->num_channels,
204 fmt->sampling_freq, direction, NHLT_DEVICE_DMIC);
205 if (!ep_blob) {
206 dev_err(adev->dev, "no DMIC ep_blob found\n");
207 return -ENOENT;
208 }
209
210 data = ep_blob->caps;
211 data_size = ep_blob->size;
212 /* DMIC gateway's vindex is statically assigned in topology */
213 node_id.vindex = t->cfg_ext->copier.vindex.val;
214
215 break;
216
217 case AVS_DMA_HDA_HOST_OUTPUT:
218 case AVS_DMA_HDA_HOST_INPUT:
219 /* HOST gateway's vindex is dynamically assigned with DMA id */
220 node_id.vindex = mod->owner->owner->dma_id;
221 break;
222
223 case AVS_DMA_HDA_LINK_OUTPUT:
224 case AVS_DMA_HDA_LINK_INPUT:
225 node_id.vindex = t->cfg_ext->copier.vindex.val |
226 mod->owner->owner->dma_id;
227 break;
228
229 case INVALID_OBJECT_ID:
230 default:
231 node_id = INVALID_NODE_ID;
232 break;
233 }
234
235 cfg_size = sizeof(*cfg) + data_size;
236 /* Every config-BLOB contains gateway attributes. */
237 if (data_size)
238 cfg_size -= sizeof(cfg->gtw_cfg.config.attrs);
239
240 cfg = kzalloc(cfg_size, GFP_KERNEL);
241 if (!cfg)
242 return -ENOMEM;
243
244 cfg->base.cpc = t->cfg_base->cpc;
245 cfg->base.ibs = t->cfg_base->ibs;
246 cfg->base.obs = t->cfg_base->obs;
247 cfg->base.is_pages = t->cfg_base->is_pages;
248 cfg->base.audio_fmt = *t->in_fmt;
249 cfg->out_fmt = *t->cfg_ext->copier.out_fmt;
250 cfg->feature_mask = t->cfg_ext->copier.feature_mask;
251 cfg->gtw_cfg.node_id = node_id;
252 cfg->gtw_cfg.dma_buffer_size = t->cfg_ext->copier.dma_buffer_size;
253 /* config_length in DWORDs */
254 cfg->gtw_cfg.config_length = DIV_ROUND_UP(data_size, 4);
255 if (data)
256 memcpy(&cfg->gtw_cfg.config, data, data_size);
257
258 mod->gtw_attrs = cfg->gtw_cfg.config.attrs;
259
260 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
261 t->core_id, t->domain, cfg, cfg_size,
262 &mod->instance_id);
263 kfree(cfg);
264 return ret;
265}
266
267static int avs_updown_mix_create(struct avs_dev *adev, struct avs_path_module *mod)
268{
269 struct avs_tplg_module *t = mod->template;
270 struct avs_updown_mixer_cfg cfg;
271 int i;
272
273 cfg.base.cpc = t->cfg_base->cpc;
274 cfg.base.ibs = t->cfg_base->ibs;
275 cfg.base.obs = t->cfg_base->obs;
276 cfg.base.is_pages = t->cfg_base->is_pages;
277 cfg.base.audio_fmt = *t->in_fmt;
278 cfg.out_channel_config = t->cfg_ext->updown_mix.out_channel_config;
279 cfg.coefficients_select = t->cfg_ext->updown_mix.coefficients_select;
280 for (i = 0; i < AVS_CHANNELS_MAX; i++)
281 cfg.coefficients[i] = t->cfg_ext->updown_mix.coefficients[i];
282 cfg.channel_map = t->cfg_ext->updown_mix.channel_map;
283
284 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
285 t->core_id, t->domain, &cfg, sizeof(cfg),
286 &mod->instance_id);
287}
288
289static int avs_src_create(struct avs_dev *adev, struct avs_path_module *mod)
290{
291 struct avs_tplg_module *t = mod->template;
292 struct avs_src_cfg cfg;
293
294 cfg.base.cpc = t->cfg_base->cpc;
295 cfg.base.ibs = t->cfg_base->ibs;
296 cfg.base.obs = t->cfg_base->obs;
297 cfg.base.is_pages = t->cfg_base->is_pages;
298 cfg.base.audio_fmt = *t->in_fmt;
299 cfg.out_freq = t->cfg_ext->src.out_freq;
300
301 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
302 t->core_id, t->domain, &cfg, sizeof(cfg),
303 &mod->instance_id);
304}
305
306static int avs_asrc_create(struct avs_dev *adev, struct avs_path_module *mod)
307{
308 struct avs_tplg_module *t = mod->template;
309 struct avs_asrc_cfg cfg;
310
311 cfg.base.cpc = t->cfg_base->cpc;
312 cfg.base.ibs = t->cfg_base->ibs;
313 cfg.base.obs = t->cfg_base->obs;
314 cfg.base.is_pages = t->cfg_base->is_pages;
315 cfg.base.audio_fmt = *t->in_fmt;
316 cfg.out_freq = t->cfg_ext->asrc.out_freq;
317 cfg.mode = t->cfg_ext->asrc.mode;
318 cfg.disable_jitter_buffer = t->cfg_ext->asrc.disable_jitter_buffer;
319
320 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
321 t->core_id, t->domain, &cfg, sizeof(cfg),
322 &mod->instance_id);
323}
324
325static int avs_aec_create(struct avs_dev *adev, struct avs_path_module *mod)
326{
327 struct avs_tplg_module *t = mod->template;
328 struct avs_aec_cfg cfg;
329
330 cfg.base.cpc = t->cfg_base->cpc;
331 cfg.base.ibs = t->cfg_base->ibs;
332 cfg.base.obs = t->cfg_base->obs;
333 cfg.base.is_pages = t->cfg_base->is_pages;
334 cfg.base.audio_fmt = *t->in_fmt;
335 cfg.ref_fmt = *t->cfg_ext->aec.ref_fmt;
336 cfg.out_fmt = *t->cfg_ext->aec.out_fmt;
337 cfg.cpc_lp_mode = t->cfg_ext->aec.cpc_lp_mode;
338
339 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
340 t->core_id, t->domain, &cfg, sizeof(cfg),
341 &mod->instance_id);
342}
343
344static int avs_mux_create(struct avs_dev *adev, struct avs_path_module *mod)
345{
346 struct avs_tplg_module *t = mod->template;
347 struct avs_mux_cfg cfg;
348
349 cfg.base.cpc = t->cfg_base->cpc;
350 cfg.base.ibs = t->cfg_base->ibs;
351 cfg.base.obs = t->cfg_base->obs;
352 cfg.base.is_pages = t->cfg_base->is_pages;
353 cfg.base.audio_fmt = *t->in_fmt;
354 cfg.ref_fmt = *t->cfg_ext->mux.ref_fmt;
355 cfg.out_fmt = *t->cfg_ext->mux.out_fmt;
356
357 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
358 t->core_id, t->domain, &cfg, sizeof(cfg),
359 &mod->instance_id);
360}
361
362static int avs_wov_create(struct avs_dev *adev, struct avs_path_module *mod)
363{
364 struct avs_tplg_module *t = mod->template;
365 struct avs_wov_cfg cfg;
366
367 cfg.base.cpc = t->cfg_base->cpc;
368 cfg.base.ibs = t->cfg_base->ibs;
369 cfg.base.obs = t->cfg_base->obs;
370 cfg.base.is_pages = t->cfg_base->is_pages;
371 cfg.base.audio_fmt = *t->in_fmt;
372 cfg.cpc_lp_mode = t->cfg_ext->wov.cpc_lp_mode;
373
374 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
375 t->core_id, t->domain, &cfg, sizeof(cfg),
376 &mod->instance_id);
377}
378
379static int avs_micsel_create(struct avs_dev *adev, struct avs_path_module *mod)
380{
381 struct avs_tplg_module *t = mod->template;
382 struct avs_micsel_cfg cfg;
383
384 cfg.base.cpc = t->cfg_base->cpc;
385 cfg.base.ibs = t->cfg_base->ibs;
386 cfg.base.obs = t->cfg_base->obs;
387 cfg.base.is_pages = t->cfg_base->is_pages;
388 cfg.base.audio_fmt = *t->in_fmt;
389 cfg.out_fmt = *t->cfg_ext->micsel.out_fmt;
390
391 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
392 t->core_id, t->domain, &cfg, sizeof(cfg),
393 &mod->instance_id);
394}
395
396static int avs_modbase_create(struct avs_dev *adev, struct avs_path_module *mod)
397{
398 struct avs_tplg_module *t = mod->template;
399 struct avs_modcfg_base cfg;
400
401 cfg.cpc = t->cfg_base->cpc;
402 cfg.ibs = t->cfg_base->ibs;
403 cfg.obs = t->cfg_base->obs;
404 cfg.is_pages = t->cfg_base->is_pages;
405 cfg.audio_fmt = *t->in_fmt;
406
407 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
408 t->core_id, t->domain, &cfg, sizeof(cfg),
409 &mod->instance_id);
410}
411
412static int avs_modext_create(struct avs_dev *adev, struct avs_path_module *mod)
413{
414 struct avs_tplg_module *t = mod->template;
415 struct avs_tplg_modcfg_ext *tcfg = t->cfg_ext;
416 struct avs_modcfg_ext *cfg;
417 size_t cfg_size, num_pins;
418 int ret, i;
419
420 num_pins = tcfg->generic.num_input_pins + tcfg->generic.num_output_pins;
421 cfg_size = sizeof(*cfg) + sizeof(*cfg->pin_fmts) * num_pins;
422
423 cfg = kzalloc(cfg_size, GFP_KERNEL);
424 if (!cfg)
425 return -ENOMEM;
426
427 cfg->base.cpc = t->cfg_base->cpc;
428 cfg->base.ibs = t->cfg_base->ibs;
429 cfg->base.obs = t->cfg_base->obs;
430 cfg->base.is_pages = t->cfg_base->is_pages;
431 cfg->base.audio_fmt = *t->in_fmt;
432 cfg->num_input_pins = tcfg->generic.num_input_pins;
433 cfg->num_output_pins = tcfg->generic.num_output_pins;
434
435 /* configure pin formats */
436 for (i = 0; i < num_pins; i++) {
437 struct avs_tplg_pin_format *tpin = &tcfg->generic.pin_fmts[i];
438 struct avs_pin_format *pin = &cfg->pin_fmts[i];
439
440 pin->pin_index = tpin->pin_index;
441 pin->iobs = tpin->iobs;
442 pin->audio_fmt = *tpin->fmt;
443 }
444
445 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id,
446 t->core_id, t->domain, cfg, cfg_size,
447 &mod->instance_id);
448 kfree(cfg);
449 return ret;
450}
451
452static int avs_probe_create(struct avs_dev *adev, struct avs_path_module *mod)
453{
454 dev_err(adev->dev, "Probe module can't be instantiated by topology");
455 return -EINVAL;
456}
457
458struct avs_module_create {
459 guid_t *guid;
460 int (*create)(struct avs_dev *adev, struct avs_path_module *mod);
461};
462
463static struct avs_module_create avs_module_create[] = {
464 { &AVS_MIXIN_MOD_UUID, avs_modbase_create },
465 { &AVS_MIXOUT_MOD_UUID, avs_modbase_create },
466 { &AVS_KPBUFF_MOD_UUID, avs_modbase_create },
467 { &AVS_COPIER_MOD_UUID, avs_copier_create },
468 { &AVS_MICSEL_MOD_UUID, avs_micsel_create },
469 { &AVS_MUX_MOD_UUID, avs_mux_create },
470 { &AVS_UPDWMIX_MOD_UUID, avs_updown_mix_create },
471 { &AVS_SRCINTC_MOD_UUID, avs_src_create },
472 { &AVS_AEC_MOD_UUID, avs_aec_create },
473 { &AVS_ASRC_MOD_UUID, avs_asrc_create },
474 { &AVS_INTELWOV_MOD_UUID, avs_wov_create },
475 { &AVS_PROBE_MOD_UUID, avs_probe_create },
476};
477
478static int avs_path_module_type_create(struct avs_dev *adev, struct avs_path_module *mod)
479{
480 const guid_t *type = &mod->template->cfg_ext->type;
481
482 for (int i = 0; i < ARRAY_SIZE(avs_module_create); i++)
483 if (guid_equal(type, avs_module_create[i].guid))
484 return avs_module_create[i].create(adev, mod);
485
486 return avs_modext_create(adev, mod);
487}
488
489static void avs_path_module_free(struct avs_dev *adev, struct avs_path_module *mod)
490{
491 kfree(mod);
492}
493
494static struct avs_path_module *
495avs_path_module_create(struct avs_dev *adev,
496 struct avs_path_pipeline *owner,
497 struct avs_tplg_module *template)
498{
499 struct avs_path_module *mod;
500 int module_id, ret;
501
502 module_id = avs_get_module_id(adev, &template->cfg_ext->type);
503 if (module_id < 0)
504 return ERR_PTR(module_id);
505
506 mod = kzalloc(sizeof(*mod), GFP_KERNEL);
507 if (!mod)
508 return ERR_PTR(-ENOMEM);
509
510 mod->template = template;
511 mod->module_id = module_id;
512 mod->owner = owner;
513 INIT_LIST_HEAD(&mod->node);
514
515 ret = avs_path_module_type_create(adev, mod);
516 if (ret) {
517 dev_err(adev->dev, "module-type create failed: %d\n", ret);
518 kfree(mod);
519 return ERR_PTR(ret);
520 }
521
522 return mod;
523}
524
525static int avs_path_binding_arm(struct avs_dev *adev, struct avs_path_binding *binding)
526{
527 struct avs_path_module *this_mod, *target_mod;
528 struct avs_path_pipeline *target_ppl;
529 struct avs_path *target_path;
530 struct avs_tplg_binding *t;
531
532 t = binding->template;
533 this_mod = avs_path_find_module(binding->owner,
534 t->mod_id);
535 if (!this_mod) {
536 dev_err(adev->dev, "path mod %d not found\n", t->mod_id);
537 return -EINVAL;
538 }
539
540 /* update with target_tplg_name too */
541 target_path = avs_path_find_path(adev, t->target_tplg_name,
542 t->target_path_tmpl_id);
543 if (!target_path) {
544 dev_err(adev->dev, "target path %s:%d not found\n",
545 t->target_tplg_name, t->target_path_tmpl_id);
546 return -EINVAL;
547 }
548
549 target_ppl = avs_path_find_pipeline(target_path,
550 t->target_ppl_id);
551 if (!target_ppl) {
552 dev_err(adev->dev, "target ppl %d not found\n", t->target_ppl_id);
553 return -EINVAL;
554 }
555
556 target_mod = avs_path_find_module(target_ppl, t->target_mod_id);
557 if (!target_mod) {
558 dev_err(adev->dev, "target mod %d not found\n", t->target_mod_id);
559 return -EINVAL;
560 }
561
562 if (t->is_sink) {
563 binding->sink = this_mod;
564 binding->sink_pin = t->mod_pin;
565 binding->source = target_mod;
566 binding->source_pin = t->target_mod_pin;
567 } else {
568 binding->sink = target_mod;
569 binding->sink_pin = t->target_mod_pin;
570 binding->source = this_mod;
571 binding->source_pin = t->mod_pin;
572 }
573
574 return 0;
575}
576
577static void avs_path_binding_free(struct avs_dev *adev, struct avs_path_binding *binding)
578{
579 kfree(binding);
580}
581
582static struct avs_path_binding *avs_path_binding_create(struct avs_dev *adev,
583 struct avs_path_pipeline *owner,
584 struct avs_tplg_binding *t)
585{
586 struct avs_path_binding *binding;
587
588 binding = kzalloc(sizeof(*binding), GFP_KERNEL);
589 if (!binding)
590 return ERR_PTR(-ENOMEM);
591
592 binding->template = t;
593 binding->owner = owner;
594 INIT_LIST_HEAD(&binding->node);
595
596 return binding;
597}
598
599static int avs_path_pipeline_arm(struct avs_dev *adev,
600 struct avs_path_pipeline *ppl)
601{
602 struct avs_path_module *mod;
603
604 list_for_each_entry(mod, &ppl->mod_list, node) {
605 struct avs_path_module *source, *sink;
606 int ret;
607
608 /*
609 * Only one module (so it's implicitly last) or it is the last
610 * one, either way we don't have next module to bind it to.
611 */
612 if (mod == list_last_entry(&ppl->mod_list,
613 struct avs_path_module, node))
614 break;
615
616 /* bind current module to next module on list */
617 source = mod;
618 sink = list_next_entry(mod, node);
619 if (!source || !sink)
620 return -EINVAL;
621
622 ret = avs_ipc_bind(adev, source->module_id, source->instance_id,
623 sink->module_id, sink->instance_id, 0, 0);
624 if (ret)
625 return AVS_IPC_RET(ret);
626 }
627
628 return 0;
629}
630
631static void avs_path_pipeline_free(struct avs_dev *adev,
632 struct avs_path_pipeline *ppl)
633{
634 struct avs_path_binding *binding, *bsave;
635 struct avs_path_module *mod, *save;
636
637 list_for_each_entry_safe(binding, bsave, &ppl->binding_list, node) {
638 list_del(&binding->node);
639 avs_path_binding_free(adev, binding);
640 }
641
642 avs_dsp_delete_pipeline(adev, ppl->instance_id);
643
644 /* Unload resources occupied by owned modules */
645 list_for_each_entry_safe(mod, save, &ppl->mod_list, node) {
646 avs_dsp_delete_module(adev, mod->module_id, mod->instance_id,
647 mod->owner->instance_id,
648 mod->template->core_id);
649 avs_path_module_free(adev, mod);
650 }
651
652 list_del(&ppl->node);
653 kfree(ppl);
654}
655
656static struct avs_path_pipeline *
657avs_path_pipeline_create(struct avs_dev *adev, struct avs_path *owner,
658 struct avs_tplg_pipeline *template)
659{
660 struct avs_path_pipeline *ppl;
661 struct avs_tplg_pplcfg *cfg = template->cfg;
662 struct avs_tplg_module *tmod;
663 int ret, i;
664
665 ppl = kzalloc(sizeof(*ppl), GFP_KERNEL);
666 if (!ppl)
667 return ERR_PTR(-ENOMEM);
668
669 ppl->template = template;
670 ppl->owner = owner;
671 INIT_LIST_HEAD(&ppl->binding_list);
672 INIT_LIST_HEAD(&ppl->mod_list);
673 INIT_LIST_HEAD(&ppl->node);
674
675 ret = avs_dsp_create_pipeline(adev, cfg->req_size, cfg->priority,
676 cfg->lp, cfg->attributes,
677 &ppl->instance_id);
678 if (ret) {
679 dev_err(adev->dev, "error creating pipeline %d\n", ret);
680 kfree(ppl);
681 return ERR_PTR(ret);
682 }
683
684 list_for_each_entry(tmod, &template->mod_list, node) {
685 struct avs_path_module *mod;
686
687 mod = avs_path_module_create(adev, ppl, tmod);
688 if (IS_ERR(mod)) {
689 ret = PTR_ERR(mod);
690 dev_err(adev->dev, "error creating module %d\n", ret);
691 goto init_err;
692 }
693
694 list_add_tail(&mod->node, &ppl->mod_list);
695 }
696
697 for (i = 0; i < template->num_bindings; i++) {
698 struct avs_path_binding *binding;
699
700 binding = avs_path_binding_create(adev, ppl, template->bindings[i]);
701 if (IS_ERR(binding)) {
702 ret = PTR_ERR(binding);
703 dev_err(adev->dev, "error creating binding %d\n", ret);
704 goto init_err;
705 }
706
707 list_add_tail(&binding->node, &ppl->binding_list);
708 }
709
710 return ppl;
711
712init_err:
713 avs_path_pipeline_free(adev, ppl);
714 return ERR_PTR(ret);
715}
716
717static int avs_path_init(struct avs_dev *adev, struct avs_path *path,
718 struct avs_tplg_path *template, u32 dma_id)
719{
720 struct avs_tplg_pipeline *tppl;
721
722 path->owner = adev;
723 path->template = template;
724 path->dma_id = dma_id;
725 INIT_LIST_HEAD(&path->ppl_list);
726 INIT_LIST_HEAD(&path->node);
727
728 /* create all the pipelines */
729 list_for_each_entry(tppl, &template->ppl_list, node) {
730 struct avs_path_pipeline *ppl;
731
732 ppl = avs_path_pipeline_create(adev, path, tppl);
733 if (IS_ERR(ppl))
734 return PTR_ERR(ppl);
735
736 list_add_tail(&ppl->node, &path->ppl_list);
737 }
738
739 spin_lock(&adev->path_list_lock);
740 list_add_tail(&path->node, &adev->path_list);
741 spin_unlock(&adev->path_list_lock);
742
743 return 0;
744}
745
746static int avs_path_arm(struct avs_dev *adev, struct avs_path *path)
747{
748 struct avs_path_pipeline *ppl;
749 struct avs_path_binding *binding;
750 int ret;
751
752 list_for_each_entry(ppl, &path->ppl_list, node) {
753 /*
754 * Arm all ppl bindings before binding internal modules
755 * as it costs no IPCs which isn't true for the latter.
756 */
757 list_for_each_entry(binding, &ppl->binding_list, node) {
758 ret = avs_path_binding_arm(adev, binding);
759 if (ret < 0)
760 return ret;
761 }
762
763 ret = avs_path_pipeline_arm(adev, ppl);
764 if (ret < 0)
765 return ret;
766 }
767
768 return 0;
769}
770
771static void avs_path_free_unlocked(struct avs_path *path)
772{
773 struct avs_path_pipeline *ppl, *save;
774
775 spin_lock(&path->owner->path_list_lock);
776 list_del(&path->node);
777 spin_unlock(&path->owner->path_list_lock);
778
779 list_for_each_entry_safe(ppl, save, &path->ppl_list, node)
780 avs_path_pipeline_free(path->owner, ppl);
781
782 kfree(path);
783}
784
785static struct avs_path *avs_path_create_unlocked(struct avs_dev *adev, u32 dma_id,
786 struct avs_tplg_path *template)
787{
788 struct avs_path *path;
789 int ret;
790
791 path = kzalloc(sizeof(*path), GFP_KERNEL);
792 if (!path)
793 return ERR_PTR(-ENOMEM);
794
795 ret = avs_path_init(adev, path, template, dma_id);
796 if (ret < 0)
797 goto err;
798
799 ret = avs_path_arm(adev, path);
800 if (ret < 0)
801 goto err;
802
803 path->state = AVS_PPL_STATE_INVALID;
804 return path;
805err:
806 avs_path_free_unlocked(path);
807 return ERR_PTR(ret);
808}
809
810void avs_path_free(struct avs_path *path)
811{
812 struct avs_dev *adev = path->owner;
813
814 mutex_lock(&adev->path_mutex);
815 avs_path_free_unlocked(path);
816 mutex_unlock(&adev->path_mutex);
817}
818
819struct avs_path *avs_path_create(struct avs_dev *adev, u32 dma_id,
820 struct avs_tplg_path_template *template,
821 struct snd_pcm_hw_params *fe_params,
822 struct snd_pcm_hw_params *be_params)
823{
824 struct avs_tplg_path *variant;
825 struct avs_path *path;
826
827 variant = avs_path_find_variant(adev, template, fe_params, be_params);
828 if (!variant) {
829 dev_err(adev->dev, "no matching variant found\n");
830 return ERR_PTR(-ENOENT);
831 }
832
833 /* Serialize path and its components creation. */
834 mutex_lock(&adev->path_mutex);
835 /* Satisfy needs of avs_path_find_tplg(). */
836 mutex_lock(&adev->comp_list_mutex);
837
838 path = avs_path_create_unlocked(adev, dma_id, variant);
839
840 mutex_unlock(&adev->comp_list_mutex);
841 mutex_unlock(&adev->path_mutex);
842
843 return path;
844}
845
846static int avs_path_bind_prepare(struct avs_dev *adev,
847 struct avs_path_binding *binding)
848{
849 const struct avs_audio_format *src_fmt, *sink_fmt;
850 struct avs_tplg_module *tsource = binding->source->template;
851 struct avs_path_module *source = binding->source;
852 int ret;
853
854 /*
855 * only copier modules about to be bound
856 * to output pin other than 0 need preparation
857 */
858 if (!binding->source_pin)
859 return 0;
860 if (!guid_equal(&tsource->cfg_ext->type, &AVS_COPIER_MOD_UUID))
861 return 0;
862
863 src_fmt = tsource->in_fmt;
864 sink_fmt = binding->sink->template->in_fmt;
865
866 ret = avs_ipc_copier_set_sink_format(adev, source->module_id,
867 source->instance_id, binding->source_pin,
868 src_fmt, sink_fmt);
869 if (ret) {
870 dev_err(adev->dev, "config copier failed: %d\n", ret);
871 return AVS_IPC_RET(ret);
872 }
873
874 return 0;
875}
876
877int avs_path_bind(struct avs_path *path)
878{
879 struct avs_path_pipeline *ppl;
880 struct avs_dev *adev = path->owner;
881 int ret;
882
883 list_for_each_entry(ppl, &path->ppl_list, node) {
884 struct avs_path_binding *binding;
885
886 list_for_each_entry(binding, &ppl->binding_list, node) {
887 struct avs_path_module *source, *sink;
888
889 source = binding->source;
890 sink = binding->sink;
891
892 ret = avs_path_bind_prepare(adev, binding);
893 if (ret < 0)
894 return ret;
895
896 ret = avs_ipc_bind(adev, source->module_id,
897 source->instance_id, sink->module_id,
898 sink->instance_id, binding->sink_pin,
899 binding->source_pin);
900 if (ret) {
901 dev_err(adev->dev, "bind path failed: %d\n", ret);
902 return AVS_IPC_RET(ret);
903 }
904 }
905 }
906
907 return 0;
908}
909
910int avs_path_unbind(struct avs_path *path)
911{
912 struct avs_path_pipeline *ppl;
913 struct avs_dev *adev = path->owner;
914 int ret;
915
916 list_for_each_entry(ppl, &path->ppl_list, node) {
917 struct avs_path_binding *binding;
918
919 list_for_each_entry(binding, &ppl->binding_list, node) {
920 struct avs_path_module *source, *sink;
921
922 source = binding->source;
923 sink = binding->sink;
924
925 ret = avs_ipc_unbind(adev, source->module_id,
926 source->instance_id, sink->module_id,
927 sink->instance_id, binding->sink_pin,
928 binding->source_pin);
929 if (ret) {
930 dev_err(adev->dev, "unbind path failed: %d\n", ret);
931 return AVS_IPC_RET(ret);
932 }
933 }
934 }
935
936 return 0;
937}
938
939int avs_path_reset(struct avs_path *path)
940{
941 struct avs_path_pipeline *ppl;
942 struct avs_dev *adev = path->owner;
943 int ret;
944
945 if (path->state == AVS_PPL_STATE_RESET)
946 return 0;
947
948 list_for_each_entry(ppl, &path->ppl_list, node) {
949 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
950 AVS_PPL_STATE_RESET);
951 if (ret) {
952 dev_err(adev->dev, "reset path failed: %d\n", ret);
953 path->state = AVS_PPL_STATE_INVALID;
954 return AVS_IPC_RET(ret);
955 }
956 }
957
958 path->state = AVS_PPL_STATE_RESET;
959 return 0;
960}
961
962int avs_path_pause(struct avs_path *path)
963{
964 struct avs_path_pipeline *ppl;
965 struct avs_dev *adev = path->owner;
966 int ret;
967
968 if (path->state == AVS_PPL_STATE_PAUSED)
969 return 0;
970
971 list_for_each_entry_reverse(ppl, &path->ppl_list, node) {
972 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
973 AVS_PPL_STATE_PAUSED);
974 if (ret) {
975 dev_err(adev->dev, "pause path failed: %d\n", ret);
976 path->state = AVS_PPL_STATE_INVALID;
977 return AVS_IPC_RET(ret);
978 }
979 }
980
981 path->state = AVS_PPL_STATE_PAUSED;
982 return 0;
983}
984
985int avs_path_run(struct avs_path *path, int trigger)
986{
987 struct avs_path_pipeline *ppl;
988 struct avs_dev *adev = path->owner;
989 int ret;
990
991 if (path->state == AVS_PPL_STATE_RUNNING && trigger == AVS_TPLG_TRIGGER_AUTO)
992 return 0;
993
994 list_for_each_entry(ppl, &path->ppl_list, node) {
995 if (ppl->template->cfg->trigger != trigger)
996 continue;
997
998 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id,
999 AVS_PPL_STATE_RUNNING);
1000 if (ret) {
1001 dev_err(adev->dev, "run path failed: %d\n", ret);
1002 path->state = AVS_PPL_STATE_INVALID;
1003 return AVS_IPC_RET(ret);
1004 }
1005 }
1006
1007 path->state = AVS_PPL_STATE_RUNNING;
1008 return 0;
1009}