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/cleanup.h> 10 #include <linux/acpi.h> 11 #include <acpi/nhlt.h> 12 #include <sound/pcm_params.h> 13 #include <sound/soc.h> 14 #include "avs.h" 15 #include "control.h" 16 #include "path.h" 17 #include "topology.h" 18 19 /* Must be called with adev->comp_list_mutex held. */ 20 static struct avs_tplg * 21 avs_path_find_tplg(struct avs_dev *adev, const char *name) 22 { 23 struct avs_soc_component *acomp; 24 25 list_for_each_entry(acomp, &adev->comp_list, node) 26 if (!strcmp(acomp->tplg->name, name)) 27 return acomp->tplg; 28 return NULL; 29 } 30 31 static struct avs_path_module * 32 avs_path_find_module(struct avs_path_pipeline *ppl, u32 template_id) 33 { 34 struct avs_path_module *mod; 35 36 list_for_each_entry(mod, &ppl->mod_list, node) 37 if (mod->template->id == template_id) 38 return mod; 39 return NULL; 40 } 41 42 static struct avs_path_pipeline * 43 avs_path_find_pipeline(struct avs_path *path, u32 template_id) 44 { 45 struct avs_path_pipeline *ppl; 46 47 list_for_each_entry(ppl, &path->ppl_list, node) 48 if (ppl->template->id == template_id) 49 return ppl; 50 return NULL; 51 } 52 53 static struct avs_path * 54 avs_path_find_path(struct avs_dev *adev, const char *name, u32 template_id) 55 { 56 struct avs_tplg_path_template *pos, *template = NULL; 57 struct avs_tplg *tplg; 58 struct avs_path *path; 59 60 tplg = avs_path_find_tplg(adev, name); 61 if (!tplg) 62 return NULL; 63 64 list_for_each_entry(pos, &tplg->path_tmpl_list, node) { 65 if (pos->id == template_id) { 66 template = pos; 67 break; 68 } 69 } 70 if (!template) 71 return NULL; 72 73 guard(spinlock)(&adev->path_list_lock); 74 /* Only one variant of given path template may be instantiated at a time. */ 75 list_for_each_entry(path, &adev->path_list, node) { 76 if (path->template->owner == template) 77 return path; 78 } 79 80 return NULL; 81 } 82 83 static 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 snd_pcm_hw_params_bits(params) == fmt->valid_bit_depth); 90 } 91 92 static struct avs_tplg_path * 93 avs_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 static struct avs_tplg_path *avs_condpath_find_variant(struct avs_dev *adev, 117 struct avs_tplg_path_template *template, 118 struct avs_path *source, 119 struct avs_path *sink) 120 { 121 struct avs_tplg_path *variant; 122 123 list_for_each_entry(variant, &template->path_list, node) { 124 if (variant->source_path_id == source->template->id && 125 variant->sink_path_id == sink->template->id) 126 return variant; 127 } 128 129 return NULL; 130 } 131 132 static bool avs_tplg_path_template_id_equal(struct avs_tplg_path_template_id *id, 133 struct avs_tplg_path_template_id *id2) 134 { 135 return id->id == id2->id && !sysfs_streq(id->tplg_name, id2->tplg_name); 136 } 137 138 static struct avs_path *avs_condpath_find_match(struct avs_dev *adev, 139 struct avs_tplg_path_template *template, 140 struct avs_path *path, int dir) 141 { 142 struct avs_tplg_path_template_id *id, *id2; 143 144 if (dir) { 145 id = &template->source; 146 id2 = &template->sink; 147 } else { 148 id = &template->sink; 149 id2 = &template->source; 150 } 151 152 /* Check whether this path is either source or sink of condpath template. */ 153 if (id->id != path->template->owner->id || 154 strcmp(id->tplg_name, path->template->owner->owner->name)) 155 return NULL; 156 157 /* Unidirectional condpaths are allowed. */ 158 if (avs_tplg_path_template_id_equal(id, id2)) 159 return path; 160 161 /* Now find the counterpart. */ 162 return avs_path_find_path(adev, id2->tplg_name, id2->id); 163 } 164 165 static struct acpi_nhlt_config * 166 avs_nhlt_config_or_default(struct avs_dev *adev, struct avs_tplg_module *t); 167 168 int avs_path_set_constraint(struct avs_dev *adev, struct avs_tplg_path_template *template, 169 struct snd_pcm_hw_constraint_list *rate_list, 170 struct snd_pcm_hw_constraint_list *channels_list, 171 struct snd_pcm_hw_constraint_list *sample_bits_list) 172 { 173 struct avs_tplg_path *path_template; 174 unsigned int *rlist, *clist, *slist; 175 size_t i; 176 177 i = 0; 178 list_for_each_entry(path_template, &template->path_list, node) 179 i++; 180 181 rlist = kcalloc(i, sizeof(*rlist), GFP_KERNEL); 182 clist = kcalloc(i, sizeof(*clist), GFP_KERNEL); 183 slist = kcalloc(i, sizeof(*slist), GFP_KERNEL); 184 if (!rlist || !clist || !slist) 185 return -ENOMEM; 186 187 i = 0; 188 list_for_each_entry(path_template, &template->path_list, node) { 189 struct avs_tplg_pipeline *pipeline_template; 190 191 list_for_each_entry(pipeline_template, &path_template->ppl_list, node) { 192 struct avs_tplg_module *module_template; 193 194 list_for_each_entry(module_template, &pipeline_template->mod_list, node) { 195 const guid_t *type = &module_template->cfg_ext->type; 196 struct acpi_nhlt_config *blob; 197 198 if (!guid_equal(type, &AVS_COPIER_MOD_UUID) && 199 !guid_equal(type, &AVS_WOVHOSTM_MOD_UUID)) 200 continue; 201 202 switch (module_template->cfg_ext->copier.dma_type) { 203 case AVS_DMA_DMIC_LINK_INPUT: 204 case AVS_DMA_I2S_LINK_OUTPUT: 205 case AVS_DMA_I2S_LINK_INPUT: 206 break; 207 default: 208 continue; 209 } 210 211 if (!module_template->nhlt_config) { 212 blob = avs_nhlt_config_or_default(adev, module_template); 213 if (IS_ERR(blob)) 214 continue; 215 } 216 217 rlist[i] = path_template->fe_fmt->sampling_freq; 218 clist[i] = path_template->fe_fmt->num_channels; 219 slist[i] = path_template->fe_fmt->bit_depth; 220 i++; 221 } 222 } 223 } 224 225 if (i) { 226 rate_list->count = i; 227 rate_list->list = rlist; 228 channels_list->count = i; 229 channels_list->list = clist; 230 sample_bits_list->count = i; 231 sample_bits_list->list = slist; 232 } else { 233 kfree(rlist); 234 kfree(clist); 235 kfree(slist); 236 } 237 238 return i; 239 } 240 241 static void avs_init_node_id(union avs_connector_node_id *node_id, 242 struct avs_tplg_modcfg_ext *te, u32 dma_id) 243 { 244 node_id->val = 0; 245 node_id->dma_type = te->copier.dma_type; 246 247 switch (node_id->dma_type) { 248 case AVS_DMA_DMIC_LINK_INPUT: 249 case AVS_DMA_I2S_LINK_OUTPUT: 250 case AVS_DMA_I2S_LINK_INPUT: 251 /* Gateway's virtual index is statically assigned in the topology. */ 252 node_id->vindex = te->copier.vindex.val; 253 break; 254 255 case AVS_DMA_HDA_HOST_OUTPUT: 256 case AVS_DMA_HDA_HOST_INPUT: 257 /* Gateway's virtual index is dynamically assigned with DMA ID */ 258 node_id->vindex = dma_id; 259 break; 260 261 case AVS_DMA_HDA_LINK_OUTPUT: 262 case AVS_DMA_HDA_LINK_INPUT: 263 node_id->vindex = te->copier.vindex.val | dma_id; 264 break; 265 266 default: 267 *node_id = INVALID_NODE_ID; 268 break; 269 } 270 } 271 272 /* Every BLOB contains at least gateway attributes. */ 273 static struct acpi_nhlt_config *default_blob = (struct acpi_nhlt_config *)&(u32[2]) {4}; 274 275 static struct acpi_nhlt_config * 276 avs_nhlt_config_or_default(struct avs_dev *adev, struct avs_tplg_module *t) 277 { 278 struct acpi_nhlt_format_config *fmtcfg; 279 struct avs_tplg_modcfg_ext *te; 280 struct avs_audio_format *fmt; 281 int link_type, dev_type; 282 int bus_id, dir; 283 284 te = t->cfg_ext; 285 286 switch (te->copier.dma_type) { 287 case AVS_DMA_I2S_LINK_OUTPUT: 288 link_type = ACPI_NHLT_LINKTYPE_SSP; 289 dev_type = ACPI_NHLT_DEVICETYPE_CODEC; 290 bus_id = te->copier.vindex.i2s.instance; 291 dir = SNDRV_PCM_STREAM_PLAYBACK; 292 fmt = te->copier.out_fmt; 293 break; 294 295 case AVS_DMA_I2S_LINK_INPUT: 296 link_type = ACPI_NHLT_LINKTYPE_SSP; 297 dev_type = ACPI_NHLT_DEVICETYPE_CODEC; 298 bus_id = te->copier.vindex.i2s.instance; 299 dir = SNDRV_PCM_STREAM_CAPTURE; 300 fmt = t->in_fmt; 301 break; 302 303 case AVS_DMA_DMIC_LINK_INPUT: 304 link_type = ACPI_NHLT_LINKTYPE_PDM; 305 dev_type = -1; /* ignored */ 306 bus_id = 0; 307 dir = SNDRV_PCM_STREAM_CAPTURE; 308 fmt = t->in_fmt; 309 break; 310 311 default: 312 return default_blob; 313 } 314 315 /* Override format selection if necessary. */ 316 if (te->copier.blob_fmt) 317 fmt = te->copier.blob_fmt; 318 319 fmtcfg = acpi_nhlt_find_fmtcfg(link_type, dev_type, dir, bus_id, 320 fmt->num_channels, fmt->sampling_freq, fmt->valid_bit_depth, 321 fmt->bit_depth); 322 if (!fmtcfg) { 323 dev_warn(adev->dev, "Endpoint format configuration not found.\n"); 324 return ERR_PTR(-ENOENT); 325 } 326 327 if (fmtcfg->config.capabilities_size < default_blob->capabilities_size) 328 return ERR_PTR(-ETOOSMALL); 329 /* The firmware expects the payload to be DWORD-aligned. */ 330 if (fmtcfg->config.capabilities_size % sizeof(u32)) 331 return ERR_PTR(-EINVAL); 332 333 return &fmtcfg->config; 334 } 335 336 static int avs_append_dma_cfg(struct avs_dev *adev, struct avs_copier_gtw_cfg *gtw, 337 struct avs_tplg_module *t, u32 dma_id, size_t *cfg_size) 338 { 339 u32 dma_type = t->cfg_ext->copier.dma_type; 340 struct avs_dma_cfg *dma; 341 struct avs_tlv *tlv; 342 size_t tlv_size; 343 344 if (!avs_platattr_test(adev, ALTHDA)) 345 return 0; 346 347 switch (dma_type) { 348 case AVS_DMA_HDA_HOST_OUTPUT: 349 case AVS_DMA_HDA_HOST_INPUT: 350 case AVS_DMA_HDA_LINK_OUTPUT: 351 case AVS_DMA_HDA_LINK_INPUT: 352 return 0; 353 default: 354 break; 355 } 356 357 tlv_size = sizeof(*tlv) + sizeof(*dma); 358 if (*cfg_size + tlv_size > AVS_MAILBOX_SIZE) 359 return -E2BIG; 360 361 /* DMA config is a TLV tailing the existing payload. */ 362 tlv = (struct avs_tlv *)>w->config.blob[gtw->config_length]; 363 tlv->type = AVS_GTW_DMA_CONFIG_ID; 364 tlv->length = sizeof(*dma); 365 366 dma = (struct avs_dma_cfg *)tlv->value; 367 memset(dma, 0, sizeof(*dma)); 368 dma->dma_method = AVS_DMA_METHOD_HDA; 369 dma->pre_allocated = true; 370 dma->dma_channel_id = dma_id; 371 dma->stream_id = dma_id + 1; 372 373 gtw->config_length += tlv_size / sizeof(u32); 374 *cfg_size += tlv_size; 375 376 return 0; 377 } 378 379 static int avs_fill_gtw_config(struct avs_dev *adev, struct avs_copier_gtw_cfg *gtw, 380 struct avs_tplg_module *t, u32 dma_id, size_t *cfg_size) 381 { 382 struct acpi_nhlt_config *blob; 383 size_t gtw_size; 384 385 if (t->nhlt_config) 386 blob = t->nhlt_config->blob; 387 else 388 blob = avs_nhlt_config_or_default(adev, t); 389 if (IS_ERR(blob)) 390 return PTR_ERR(blob); 391 392 gtw_size = blob->capabilities_size; 393 if (*cfg_size + gtw_size > AVS_MAILBOX_SIZE) 394 return -E2BIG; 395 396 gtw->config_length = gtw_size / sizeof(u32); 397 memcpy(gtw->config.blob, blob->capabilities, blob->capabilities_size); 398 *cfg_size += gtw_size; 399 400 return avs_append_dma_cfg(adev, gtw, t, dma_id, cfg_size); 401 } 402 403 static int avs_copier_create(struct avs_dev *adev, struct avs_path_module *mod) 404 { 405 struct avs_tplg_module *t = mod->template; 406 struct avs_tplg_modcfg_ext *te; 407 struct avs_copier_cfg *cfg; 408 size_t cfg_size; 409 u32 dma_id; 410 int ret; 411 412 te = t->cfg_ext; 413 cfg = adev->modcfg_buf; 414 dma_id = mod->owner->owner->dma_id; 415 cfg_size = offsetof(struct avs_copier_cfg, gtw_cfg.config); 416 417 ret = avs_fill_gtw_config(adev, &cfg->gtw_cfg, t, dma_id, &cfg_size); 418 if (ret) 419 return ret; 420 421 cfg->base.cpc = t->cfg_base->cpc; 422 cfg->base.ibs = t->cfg_base->ibs; 423 cfg->base.obs = t->cfg_base->obs; 424 cfg->base.is_pages = t->cfg_base->is_pages; 425 cfg->base.audio_fmt = *t->in_fmt; 426 cfg->out_fmt = *te->copier.out_fmt; 427 cfg->feature_mask = te->copier.feature_mask; 428 avs_init_node_id(&cfg->gtw_cfg.node_id, te, dma_id); 429 cfg->gtw_cfg.dma_buffer_size = te->copier.dma_buffer_size; 430 mod->gtw_attrs = cfg->gtw_cfg.config.attrs; 431 432 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, t->core_id, 433 t->domain, cfg, cfg_size, &mod->instance_id); 434 return ret; 435 } 436 437 static int avs_whm_create(struct avs_dev *adev, struct avs_path_module *mod) 438 { 439 struct avs_tplg_module *t = mod->template; 440 struct avs_tplg_modcfg_ext *te; 441 struct avs_whm_cfg *cfg; 442 size_t cfg_size; 443 u32 dma_id; 444 int ret; 445 446 te = t->cfg_ext; 447 cfg = adev->modcfg_buf; 448 dma_id = mod->owner->owner->dma_id; 449 cfg_size = offsetof(struct avs_whm_cfg, gtw_cfg.config); 450 451 ret = avs_fill_gtw_config(adev, &cfg->gtw_cfg, t, dma_id, &cfg_size); 452 if (ret) 453 return ret; 454 455 cfg->base.cpc = t->cfg_base->cpc; 456 cfg->base.ibs = t->cfg_base->ibs; 457 cfg->base.obs = t->cfg_base->obs; 458 cfg->base.is_pages = t->cfg_base->is_pages; 459 cfg->base.audio_fmt = *t->in_fmt; 460 cfg->ref_fmt = *te->whm.ref_fmt; 461 cfg->out_fmt = *te->whm.out_fmt; 462 cfg->wake_tick_period = te->whm.wake_tick_period; 463 avs_init_node_id(&cfg->gtw_cfg.node_id, te, dma_id); 464 cfg->gtw_cfg.dma_buffer_size = te->whm.dma_buffer_size; 465 mod->gtw_attrs = cfg->gtw_cfg.config.attrs; 466 467 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, t->core_id, 468 t->domain, cfg, cfg_size, &mod->instance_id); 469 return ret; 470 } 471 472 static struct soc_mixer_control *avs_get_module_control(struct avs_path_module *mod, 473 const char *name) 474 { 475 struct avs_tplg_module *t = mod->template; 476 struct avs_tplg_path_template *path_tmpl; 477 struct snd_soc_dapm_widget *w; 478 int i; 479 480 path_tmpl = t->owner->owner->owner; 481 w = path_tmpl->w; 482 483 for (i = 0; i < w->num_kcontrols; i++) { 484 struct avs_control_data *ctl_data; 485 struct soc_mixer_control *mc; 486 487 mc = (struct soc_mixer_control *)w->kcontrols[i]->private_value; 488 ctl_data = (struct avs_control_data *)mc->dobj.private; 489 if (ctl_data->id == t->ctl_id && strstr(w->kcontrols[i]->id.name, name)) 490 return mc; 491 } 492 493 return NULL; 494 } 495 496 int avs_peakvol_set_volume(struct avs_dev *adev, struct avs_path_module *mod, 497 struct soc_mixer_control *mc, long *input) 498 { 499 struct avs_volume_cfg vols[SND_SOC_TPLG_MAX_CHAN] = {{0}}; 500 struct avs_control_data *ctl_data; 501 struct avs_tplg_module *t; 502 int ret, i; 503 504 ctl_data = mc->dobj.private; 505 t = mod->template; 506 if (!input) 507 input = ctl_data->values; 508 509 if (mc->num_channels) { 510 for (i = 0; i < mc->num_channels; i++) { 511 vols[i].channel_id = i; 512 vols[i].target_volume = input[i]; 513 vols[i].curve_type = t->cfg_ext->peakvol.curve_type; 514 vols[i].curve_duration = t->cfg_ext->peakvol.curve_duration; 515 } 516 517 ret = avs_ipc_peakvol_set_volumes(adev, mod->module_id, mod->instance_id, vols, 518 mc->num_channels); 519 return AVS_IPC_RET(ret); 520 } 521 522 /* Target all channels if no individual selected. */ 523 vols[0].channel_id = AVS_ALL_CHANNELS_MASK; 524 vols[0].target_volume = input[0]; 525 vols[0].curve_type = t->cfg_ext->peakvol.curve_type; 526 vols[0].curve_duration = t->cfg_ext->peakvol.curve_duration; 527 528 ret = avs_ipc_peakvol_set_volume(adev, mod->module_id, mod->instance_id, &vols[0]); 529 return AVS_IPC_RET(ret); 530 } 531 532 int avs_peakvol_set_mute(struct avs_dev *adev, struct avs_path_module *mod, 533 struct soc_mixer_control *mc, long *input) 534 { 535 struct avs_mute_cfg mutes[SND_SOC_TPLG_MAX_CHAN] = {{0}}; 536 struct avs_control_data *ctl_data; 537 struct avs_tplg_module *t; 538 int ret, i; 539 540 ctl_data = mc->dobj.private; 541 t = mod->template; 542 if (!input) 543 input = ctl_data->values; 544 545 if (mc->num_channels) { 546 for (i = 0; i < mc->num_channels; i++) { 547 mutes[i].channel_id = i; 548 mutes[i].mute = !input[i]; 549 mutes[i].curve_type = t->cfg_ext->peakvol.curve_type; 550 mutes[i].curve_duration = t->cfg_ext->peakvol.curve_duration; 551 } 552 553 ret = avs_ipc_peakvol_set_mutes(adev, mod->module_id, mod->instance_id, mutes, 554 mc->num_channels); 555 return AVS_IPC_RET(ret); 556 } 557 558 /* Target all channels if no individual selected. */ 559 mutes[0].channel_id = AVS_ALL_CHANNELS_MASK; 560 mutes[0].mute = !input[0]; 561 mutes[0].curve_type = t->cfg_ext->peakvol.curve_type; 562 mutes[0].curve_duration = t->cfg_ext->peakvol.curve_duration; 563 564 ret = avs_ipc_peakvol_set_mute(adev, mod->module_id, mod->instance_id, &mutes[0]); 565 return AVS_IPC_RET(ret); 566 } 567 568 static int avs_peakvol_create(struct avs_dev *adev, struct avs_path_module *mod) 569 { 570 struct avs_tplg_module *t = mod->template; 571 struct soc_mixer_control *mc; 572 struct avs_peakvol_cfg *cfg; 573 size_t cfg_size; 574 int ret; 575 576 cfg_size = struct_size(cfg, vols, 1); 577 if (cfg_size > AVS_MAILBOX_SIZE) 578 return -EINVAL; 579 580 cfg = adev->modcfg_buf; 581 memset(cfg, 0, cfg_size); 582 cfg->base.cpc = t->cfg_base->cpc; 583 cfg->base.ibs = t->cfg_base->ibs; 584 cfg->base.obs = t->cfg_base->obs; 585 cfg->base.is_pages = t->cfg_base->is_pages; 586 cfg->base.audio_fmt = *t->in_fmt; 587 cfg->vols[0].channel_id = AVS_ALL_CHANNELS_MASK; 588 cfg->vols[0].target_volume = S32_MAX; 589 cfg->vols[0].curve_type = t->cfg_ext->peakvol.curve_type; 590 cfg->vols[0].curve_duration = t->cfg_ext->peakvol.curve_duration; 591 592 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, t->core_id, 593 t->domain, cfg, cfg_size, &mod->instance_id); 594 if (ret) 595 return ret; 596 597 /* Now configure both VOLUME and MUTE parameters. */ 598 mc = avs_get_module_control(mod, "Volume"); 599 if (mc) { 600 ret = avs_peakvol_set_volume(adev, mod, mc, NULL); 601 if (ret) 602 return ret; 603 } 604 605 mc = avs_get_module_control(mod, "Switch"); 606 if (mc) 607 return avs_peakvol_set_mute(adev, mod, mc, NULL); 608 return 0; 609 } 610 611 static int avs_updown_mix_create(struct avs_dev *adev, struct avs_path_module *mod) 612 { 613 struct avs_tplg_module *t = mod->template; 614 struct avs_updown_mixer_cfg cfg; 615 int i; 616 617 cfg.base.cpc = t->cfg_base->cpc; 618 cfg.base.ibs = t->cfg_base->ibs; 619 cfg.base.obs = t->cfg_base->obs; 620 cfg.base.is_pages = t->cfg_base->is_pages; 621 cfg.base.audio_fmt = *t->in_fmt; 622 cfg.out_channel_config = t->cfg_ext->updown_mix.out_channel_config; 623 cfg.coefficients_select = t->cfg_ext->updown_mix.coefficients_select; 624 for (i = 0; i < AVS_COEFF_CHANNELS_MAX; i++) 625 cfg.coefficients[i] = t->cfg_ext->updown_mix.coefficients[i]; 626 cfg.channel_map = t->cfg_ext->updown_mix.channel_map; 627 628 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 629 t->core_id, t->domain, &cfg, sizeof(cfg), 630 &mod->instance_id); 631 } 632 633 static int avs_src_create(struct avs_dev *adev, struct avs_path_module *mod) 634 { 635 struct avs_tplg_module *t = mod->template; 636 struct avs_src_cfg cfg; 637 638 cfg.base.cpc = t->cfg_base->cpc; 639 cfg.base.ibs = t->cfg_base->ibs; 640 cfg.base.obs = t->cfg_base->obs; 641 cfg.base.is_pages = t->cfg_base->is_pages; 642 cfg.base.audio_fmt = *t->in_fmt; 643 cfg.out_freq = t->cfg_ext->src.out_freq; 644 645 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 646 t->core_id, t->domain, &cfg, sizeof(cfg), 647 &mod->instance_id); 648 } 649 650 static int avs_asrc_create(struct avs_dev *adev, struct avs_path_module *mod) 651 { 652 struct avs_tplg_module *t = mod->template; 653 struct avs_asrc_cfg cfg; 654 655 memset(&cfg, 0, sizeof(cfg)); 656 cfg.base.cpc = t->cfg_base->cpc; 657 cfg.base.ibs = t->cfg_base->ibs; 658 cfg.base.obs = t->cfg_base->obs; 659 cfg.base.is_pages = t->cfg_base->is_pages; 660 cfg.base.audio_fmt = *t->in_fmt; 661 cfg.out_freq = t->cfg_ext->asrc.out_freq; 662 cfg.mode = t->cfg_ext->asrc.mode; 663 cfg.disable_jitter_buffer = t->cfg_ext->asrc.disable_jitter_buffer; 664 665 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 666 t->core_id, t->domain, &cfg, sizeof(cfg), 667 &mod->instance_id); 668 } 669 670 static int avs_aec_create(struct avs_dev *adev, struct avs_path_module *mod) 671 { 672 struct avs_tplg_module *t = mod->template; 673 struct avs_aec_cfg cfg; 674 675 cfg.base.cpc = t->cfg_base->cpc; 676 cfg.base.ibs = t->cfg_base->ibs; 677 cfg.base.obs = t->cfg_base->obs; 678 cfg.base.is_pages = t->cfg_base->is_pages; 679 cfg.base.audio_fmt = *t->in_fmt; 680 cfg.ref_fmt = *t->cfg_ext->aec.ref_fmt; 681 cfg.out_fmt = *t->cfg_ext->aec.out_fmt; 682 cfg.cpc_lp_mode = t->cfg_ext->aec.cpc_lp_mode; 683 684 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 685 t->core_id, t->domain, &cfg, sizeof(cfg), 686 &mod->instance_id); 687 } 688 689 static int avs_mux_create(struct avs_dev *adev, struct avs_path_module *mod) 690 { 691 struct avs_tplg_module *t = mod->template; 692 struct avs_mux_cfg cfg; 693 694 cfg.base.cpc = t->cfg_base->cpc; 695 cfg.base.ibs = t->cfg_base->ibs; 696 cfg.base.obs = t->cfg_base->obs; 697 cfg.base.is_pages = t->cfg_base->is_pages; 698 cfg.base.audio_fmt = *t->in_fmt; 699 cfg.ref_fmt = *t->cfg_ext->mux.ref_fmt; 700 cfg.out_fmt = *t->cfg_ext->mux.out_fmt; 701 702 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 703 t->core_id, t->domain, &cfg, sizeof(cfg), 704 &mod->instance_id); 705 } 706 707 static int avs_wov_create(struct avs_dev *adev, struct avs_path_module *mod) 708 { 709 struct avs_tplg_module *t = mod->template; 710 struct avs_wov_cfg cfg; 711 712 cfg.base.cpc = t->cfg_base->cpc; 713 cfg.base.ibs = t->cfg_base->ibs; 714 cfg.base.obs = t->cfg_base->obs; 715 cfg.base.is_pages = t->cfg_base->is_pages; 716 cfg.base.audio_fmt = *t->in_fmt; 717 cfg.cpc_lp_mode = t->cfg_ext->wov.cpc_lp_mode; 718 719 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 720 t->core_id, t->domain, &cfg, sizeof(cfg), 721 &mod->instance_id); 722 } 723 724 static int avs_micsel_create(struct avs_dev *adev, struct avs_path_module *mod) 725 { 726 struct avs_tplg_module *t = mod->template; 727 struct avs_micsel_cfg cfg; 728 729 cfg.base.cpc = t->cfg_base->cpc; 730 cfg.base.ibs = t->cfg_base->ibs; 731 cfg.base.obs = t->cfg_base->obs; 732 cfg.base.is_pages = t->cfg_base->is_pages; 733 cfg.base.audio_fmt = *t->in_fmt; 734 cfg.out_fmt = *t->cfg_ext->micsel.out_fmt; 735 736 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 737 t->core_id, t->domain, &cfg, sizeof(cfg), 738 &mod->instance_id); 739 } 740 741 static int avs_modbase_create(struct avs_dev *adev, struct avs_path_module *mod) 742 { 743 struct avs_tplg_module *t = mod->template; 744 struct avs_modcfg_base cfg; 745 746 cfg.cpc = t->cfg_base->cpc; 747 cfg.ibs = t->cfg_base->ibs; 748 cfg.obs = t->cfg_base->obs; 749 cfg.is_pages = t->cfg_base->is_pages; 750 cfg.audio_fmt = *t->in_fmt; 751 752 return avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 753 t->core_id, t->domain, &cfg, sizeof(cfg), 754 &mod->instance_id); 755 } 756 757 static int avs_modext_create(struct avs_dev *adev, struct avs_path_module *mod) 758 { 759 struct avs_tplg_module *t = mod->template; 760 struct avs_tplg_modcfg_ext *tcfg = t->cfg_ext; 761 struct avs_modcfg_ext *cfg; 762 size_t cfg_size, num_pins; 763 int ret, i; 764 765 num_pins = tcfg->generic.num_input_pins + tcfg->generic.num_output_pins; 766 cfg_size = struct_size(cfg, pin_fmts, num_pins); 767 768 if (cfg_size > AVS_MAILBOX_SIZE) 769 return -EINVAL; 770 771 cfg = adev->modcfg_buf; 772 memset(cfg, 0, cfg_size); 773 cfg->base.cpc = t->cfg_base->cpc; 774 cfg->base.ibs = t->cfg_base->ibs; 775 cfg->base.obs = t->cfg_base->obs; 776 cfg->base.is_pages = t->cfg_base->is_pages; 777 cfg->base.audio_fmt = *t->in_fmt; 778 cfg->num_input_pins = tcfg->generic.num_input_pins; 779 cfg->num_output_pins = tcfg->generic.num_output_pins; 780 781 /* configure pin formats */ 782 for (i = 0; i < num_pins; i++) { 783 struct avs_tplg_pin_format *tpin = &tcfg->generic.pin_fmts[i]; 784 struct avs_pin_format *pin = &cfg->pin_fmts[i]; 785 786 pin->pin_index = tpin->pin_index; 787 pin->iobs = tpin->iobs; 788 pin->audio_fmt = *tpin->fmt; 789 } 790 791 ret = avs_dsp_init_module(adev, mod->module_id, mod->owner->instance_id, 792 t->core_id, t->domain, cfg, cfg_size, 793 &mod->instance_id); 794 return ret; 795 } 796 797 static int avs_probe_create(struct avs_dev *adev, struct avs_path_module *mod) 798 { 799 dev_err(adev->dev, "Probe module can't be instantiated by topology"); 800 return -EINVAL; 801 } 802 803 struct avs_module_create { 804 guid_t *guid; 805 int (*create)(struct avs_dev *adev, struct avs_path_module *mod); 806 }; 807 808 static struct avs_module_create avs_module_create[] = { 809 { &AVS_MIXIN_MOD_UUID, avs_modbase_create }, 810 { &AVS_MIXOUT_MOD_UUID, avs_modbase_create }, 811 { &AVS_KPBUFF_MOD_UUID, avs_modbase_create }, 812 { &AVS_COPIER_MOD_UUID, avs_copier_create }, 813 { &AVS_PEAKVOL_MOD_UUID, avs_peakvol_create }, 814 { &AVS_GAIN_MOD_UUID, avs_peakvol_create }, 815 { &AVS_MICSEL_MOD_UUID, avs_micsel_create }, 816 { &AVS_MUX_MOD_UUID, avs_mux_create }, 817 { &AVS_UPDWMIX_MOD_UUID, avs_updown_mix_create }, 818 { &AVS_SRCINTC_MOD_UUID, avs_src_create }, 819 { &AVS_AEC_MOD_UUID, avs_aec_create }, 820 { &AVS_ASRC_MOD_UUID, avs_asrc_create }, 821 { &AVS_INTELWOV_MOD_UUID, avs_wov_create }, 822 { &AVS_PROBE_MOD_UUID, avs_probe_create }, 823 { &AVS_WOVHOSTM_MOD_UUID, avs_whm_create }, 824 }; 825 826 static int avs_path_module_type_create(struct avs_dev *adev, struct avs_path_module *mod) 827 { 828 const guid_t *type = &mod->template->cfg_ext->type; 829 830 for (int i = 0; i < ARRAY_SIZE(avs_module_create); i++) 831 if (guid_equal(type, avs_module_create[i].guid)) 832 return avs_module_create[i].create(adev, mod); 833 834 return avs_modext_create(adev, mod); 835 } 836 837 static int avs_path_module_send_init_configs(struct avs_dev *adev, struct avs_path_module *mod) 838 { 839 struct avs_soc_component *acomp; 840 841 acomp = to_avs_soc_component(mod->template->owner->owner->owner->owner->comp); 842 843 u32 num_ids = mod->template->num_config_ids; 844 u32 *ids = mod->template->config_ids; 845 846 for (int i = 0; i < num_ids; i++) { 847 struct avs_tplg_init_config *config = &acomp->tplg->init_configs[ids[i]]; 848 size_t len = config->length; 849 void *data = config->data; 850 u32 param = config->param; 851 int ret; 852 853 ret = avs_ipc_set_large_config(adev, mod->module_id, mod->instance_id, 854 param, data, len); 855 if (ret) { 856 dev_err(adev->dev, "send initial module config failed: %d\n", ret); 857 return AVS_IPC_RET(ret); 858 } 859 } 860 861 return 0; 862 } 863 864 static void avs_path_module_free(struct avs_dev *adev, struct avs_path_module *mod) 865 { 866 kfree(mod); 867 } 868 869 static struct avs_path_module * 870 avs_path_module_create(struct avs_dev *adev, 871 struct avs_path_pipeline *owner, 872 struct avs_tplg_module *template) 873 { 874 struct avs_path_module *mod; 875 int module_id, ret; 876 877 module_id = avs_get_module_id(adev, &template->cfg_ext->type); 878 if (module_id < 0) 879 return ERR_PTR(module_id); 880 881 mod = kzalloc_obj(*mod); 882 if (!mod) 883 return ERR_PTR(-ENOMEM); 884 885 mod->template = template; 886 mod->module_id = module_id; 887 mod->owner = owner; 888 INIT_LIST_HEAD(&mod->node); 889 890 ret = avs_path_module_type_create(adev, mod); 891 if (ret) { 892 dev_err(adev->dev, "module-type create failed: %d\n", ret); 893 kfree(mod); 894 return ERR_PTR(ret); 895 } 896 897 ret = avs_path_module_send_init_configs(adev, mod); 898 if (ret) { 899 kfree(mod); 900 return ERR_PTR(ret); 901 } 902 903 return mod; 904 } 905 906 static int avs_path_binding_arm(struct avs_dev *adev, struct avs_path_binding *binding) 907 { 908 struct avs_path_module *this_mod, *target_mod; 909 struct avs_path_pipeline *target_ppl; 910 struct avs_path *target_path; 911 struct avs_tplg_binding *t; 912 913 t = binding->template; 914 this_mod = avs_path_find_module(binding->owner, 915 t->mod_id); 916 if (!this_mod) { 917 dev_err(adev->dev, "path mod %d not found\n", t->mod_id); 918 return -EINVAL; 919 } 920 921 /* update with target_tplg_name too */ 922 target_path = avs_path_find_path(adev, t->target_tplg_name, 923 t->target_path_tmpl_id); 924 if (!target_path) { 925 dev_err(adev->dev, "target path %s:%d not found\n", 926 t->target_tplg_name, t->target_path_tmpl_id); 927 return -EINVAL; 928 } 929 930 target_ppl = avs_path_find_pipeline(target_path, 931 t->target_ppl_id); 932 if (!target_ppl) { 933 dev_err(adev->dev, "target ppl %d not found\n", t->target_ppl_id); 934 return -EINVAL; 935 } 936 937 target_mod = avs_path_find_module(target_ppl, t->target_mod_id); 938 if (!target_mod) { 939 dev_err(adev->dev, "target mod %d not found\n", t->target_mod_id); 940 return -EINVAL; 941 } 942 943 if (t->is_sink) { 944 binding->sink = this_mod; 945 binding->sink_pin = t->mod_pin; 946 binding->source = target_mod; 947 binding->source_pin = t->target_mod_pin; 948 } else { 949 binding->sink = target_mod; 950 binding->sink_pin = t->target_mod_pin; 951 binding->source = this_mod; 952 binding->source_pin = t->mod_pin; 953 } 954 955 return 0; 956 } 957 958 static void avs_path_binding_free(struct avs_dev *adev, struct avs_path_binding *binding) 959 { 960 kfree(binding); 961 } 962 963 static struct avs_path_binding *avs_path_binding_create(struct avs_dev *adev, 964 struct avs_path_pipeline *owner, 965 struct avs_tplg_binding *t) 966 { 967 struct avs_path_binding *binding; 968 969 binding = kzalloc_obj(*binding); 970 if (!binding) 971 return ERR_PTR(-ENOMEM); 972 973 binding->template = t; 974 binding->owner = owner; 975 INIT_LIST_HEAD(&binding->node); 976 977 return binding; 978 } 979 980 static int avs_path_pipeline_arm(struct avs_dev *adev, 981 struct avs_path_pipeline *ppl) 982 { 983 struct avs_path_module *mod; 984 985 list_for_each_entry(mod, &ppl->mod_list, node) { 986 struct avs_path_module *source, *sink; 987 int ret; 988 989 /* 990 * Only one module (so it's implicitly last) or it is the last 991 * one, either way we don't have next module to bind it to. 992 */ 993 if (mod == list_last_entry(&ppl->mod_list, 994 struct avs_path_module, node)) 995 break; 996 997 /* bind current module to next module on list */ 998 source = mod; 999 sink = list_next_entry(mod, node); 1000 1001 ret = avs_ipc_bind(adev, source->module_id, source->instance_id, 1002 sink->module_id, sink->instance_id, 0, 0); 1003 if (ret) 1004 return AVS_IPC_RET(ret); 1005 } 1006 1007 return 0; 1008 } 1009 1010 static void avs_path_pipeline_free(struct avs_dev *adev, 1011 struct avs_path_pipeline *ppl) 1012 { 1013 struct avs_path_binding *binding, *bsave; 1014 struct avs_path_module *mod, *save; 1015 1016 list_for_each_entry_safe(binding, bsave, &ppl->binding_list, node) { 1017 list_del(&binding->node); 1018 avs_path_binding_free(adev, binding); 1019 } 1020 1021 avs_dsp_delete_pipeline(adev, ppl->instance_id); 1022 1023 /* Unload resources occupied by owned modules */ 1024 list_for_each_entry_safe(mod, save, &ppl->mod_list, node) { 1025 avs_dsp_delete_module(adev, mod->module_id, mod->instance_id, 1026 mod->owner->instance_id, 1027 mod->template->core_id); 1028 avs_path_module_free(adev, mod); 1029 } 1030 1031 list_del(&ppl->node); 1032 kfree(ppl); 1033 } 1034 1035 static struct avs_path_pipeline * 1036 avs_path_pipeline_create(struct avs_dev *adev, struct avs_path *owner, 1037 struct avs_tplg_pipeline *template) 1038 { 1039 struct avs_path_pipeline *ppl; 1040 struct avs_tplg_pplcfg *cfg = template->cfg; 1041 struct avs_tplg_module *tmod; 1042 int ret, i; 1043 1044 ppl = kzalloc_obj(*ppl); 1045 if (!ppl) 1046 return ERR_PTR(-ENOMEM); 1047 1048 ppl->template = template; 1049 ppl->owner = owner; 1050 INIT_LIST_HEAD(&ppl->binding_list); 1051 INIT_LIST_HEAD(&ppl->mod_list); 1052 INIT_LIST_HEAD(&ppl->node); 1053 1054 ret = avs_dsp_create_pipeline(adev, cfg->req_size, cfg->priority, 1055 cfg->lp, cfg->attributes, 1056 &ppl->instance_id); 1057 if (ret) { 1058 dev_err(adev->dev, "error creating pipeline %d\n", ret); 1059 kfree(ppl); 1060 return ERR_PTR(ret); 1061 } 1062 1063 list_for_each_entry(tmod, &template->mod_list, node) { 1064 struct avs_path_module *mod; 1065 1066 mod = avs_path_module_create(adev, ppl, tmod); 1067 if (IS_ERR(mod)) { 1068 ret = PTR_ERR(mod); 1069 dev_err(adev->dev, "error creating module %d\n", ret); 1070 goto init_err; 1071 } 1072 1073 list_add_tail(&mod->node, &ppl->mod_list); 1074 } 1075 1076 for (i = 0; i < template->num_bindings; i++) { 1077 struct avs_path_binding *binding; 1078 1079 binding = avs_path_binding_create(adev, ppl, template->bindings[i]); 1080 if (IS_ERR(binding)) { 1081 ret = PTR_ERR(binding); 1082 dev_err(adev->dev, "error creating binding %d\n", ret); 1083 goto init_err; 1084 } 1085 1086 list_add_tail(&binding->node, &ppl->binding_list); 1087 } 1088 1089 return ppl; 1090 1091 init_err: 1092 avs_path_pipeline_free(adev, ppl); 1093 return ERR_PTR(ret); 1094 } 1095 1096 static int avs_path_init(struct avs_dev *adev, struct avs_path *path, 1097 struct avs_tplg_path *template, u32 dma_id) 1098 { 1099 struct avs_tplg_pipeline *tppl; 1100 1101 path->owner = adev; 1102 path->template = template; 1103 path->dma_id = dma_id; 1104 INIT_LIST_HEAD(&path->ppl_list); 1105 INIT_LIST_HEAD(&path->node); 1106 INIT_LIST_HEAD(&path->source_list); 1107 INIT_LIST_HEAD(&path->sink_list); 1108 INIT_LIST_HEAD(&path->source_node); 1109 INIT_LIST_HEAD(&path->sink_node); 1110 1111 /* create all the pipelines */ 1112 list_for_each_entry(tppl, &template->ppl_list, node) { 1113 struct avs_path_pipeline *ppl; 1114 1115 ppl = avs_path_pipeline_create(adev, path, tppl); 1116 if (IS_ERR(ppl)) 1117 return PTR_ERR(ppl); 1118 1119 list_add_tail(&ppl->node, &path->ppl_list); 1120 } 1121 1122 spin_lock(&adev->path_list_lock); 1123 list_add_tail(&path->node, &adev->path_list); 1124 spin_unlock(&adev->path_list_lock); 1125 1126 return 0; 1127 } 1128 1129 static int avs_path_arm(struct avs_dev *adev, struct avs_path *path) 1130 { 1131 struct avs_path_pipeline *ppl; 1132 struct avs_path_binding *binding; 1133 int ret; 1134 1135 list_for_each_entry(ppl, &path->ppl_list, node) { 1136 /* 1137 * Arm all ppl bindings before binding internal modules 1138 * as it costs no IPCs which isn't true for the latter. 1139 */ 1140 list_for_each_entry(binding, &ppl->binding_list, node) { 1141 ret = avs_path_binding_arm(adev, binding); 1142 if (ret < 0) 1143 return ret; 1144 } 1145 1146 ret = avs_path_pipeline_arm(adev, ppl); 1147 if (ret < 0) 1148 return ret; 1149 } 1150 1151 return 0; 1152 } 1153 1154 static void avs_path_free_unlocked(struct avs_path *path) 1155 { 1156 struct avs_path_pipeline *ppl, *save; 1157 1158 spin_lock(&path->owner->path_list_lock); 1159 list_del(&path->node); 1160 spin_unlock(&path->owner->path_list_lock); 1161 1162 list_for_each_entry_safe(ppl, save, &path->ppl_list, node) 1163 avs_path_pipeline_free(path->owner, ppl); 1164 1165 kfree(path); 1166 } 1167 1168 static struct avs_path *avs_path_create_unlocked(struct avs_dev *adev, u32 dma_id, 1169 struct avs_tplg_path *template) 1170 { 1171 struct avs_path *path; 1172 int ret; 1173 1174 path = kzalloc_obj(*path); 1175 if (!path) 1176 return ERR_PTR(-ENOMEM); 1177 1178 ret = avs_path_init(adev, path, template, dma_id); 1179 if (ret < 0) 1180 goto err; 1181 1182 ret = avs_path_arm(adev, path); 1183 if (ret < 0) 1184 goto err; 1185 1186 path->state = AVS_PPL_STATE_INVALID; 1187 return path; 1188 err: 1189 avs_path_free_unlocked(path); 1190 return ERR_PTR(ret); 1191 } 1192 1193 static void avs_condpath_free(struct avs_dev *adev, struct avs_path *path) 1194 { 1195 int ret; 1196 1197 list_del(&path->source_node); 1198 list_del(&path->sink_node); 1199 1200 ret = avs_path_reset(path); 1201 if (ret < 0) 1202 dev_err(adev->dev, "reset condpath failed: %d\n", ret); 1203 1204 ret = avs_path_unbind(path); 1205 if (ret < 0) 1206 dev_err(adev->dev, "unbind condpath failed: %d\n", ret); 1207 1208 avs_path_free_unlocked(path); 1209 } 1210 1211 static struct avs_path *avs_condpath_create(struct avs_dev *adev, 1212 struct avs_tplg_path *template, 1213 struct avs_path *source, 1214 struct avs_path *sink) 1215 { 1216 struct avs_path *path; 1217 int ret; 1218 1219 path = avs_path_create_unlocked(adev, 0, template); 1220 if (IS_ERR(path)) 1221 return path; 1222 1223 ret = avs_path_bind(path); 1224 if (ret) 1225 goto err_bind; 1226 1227 ret = avs_path_reset(path); 1228 if (ret) 1229 goto err_reset; 1230 1231 path->source = source; 1232 path->sink = sink; 1233 list_add_tail(&path->source_node, &source->source_list); 1234 list_add_tail(&path->sink_node, &sink->sink_list); 1235 1236 return path; 1237 1238 err_reset: 1239 avs_path_unbind(path); 1240 err_bind: 1241 avs_path_free_unlocked(path); 1242 return ERR_PTR(ret); 1243 } 1244 1245 static int avs_condpaths_walk(struct avs_dev *adev, struct avs_path *path, int dir) 1246 { 1247 struct avs_soc_component *acomp; 1248 struct avs_path *source, *sink; 1249 struct avs_path **other; 1250 1251 if (dir) { 1252 source = path; 1253 other = &sink; 1254 } else { 1255 sink = path; 1256 other = &source; 1257 } 1258 1259 list_for_each_entry(acomp, &adev->comp_list, node) { 1260 for (int i = 0; i < acomp->tplg->num_condpath_tmpls; i++) { 1261 struct avs_tplg_path_template *template; 1262 struct avs_tplg_path *variant; 1263 struct avs_path *cpath; 1264 1265 template = &acomp->tplg->condpath_tmpls[i]; 1266 1267 /* Do not create unidirectional condpaths twice. */ 1268 if (avs_tplg_path_template_id_equal(&template->source, 1269 &template->sink) && dir) 1270 continue; 1271 1272 *other = avs_condpath_find_match(adev, template, path, dir); 1273 if (!*other) 1274 continue; 1275 1276 variant = avs_condpath_find_variant(adev, template, source, sink); 1277 if (!variant) 1278 continue; 1279 1280 cpath = avs_condpath_create(adev, variant, source, sink); 1281 if (IS_ERR(cpath)) 1282 return PTR_ERR(cpath); 1283 } 1284 } 1285 1286 return 0; 1287 } 1288 1289 /* Caller responsible for holding adev->path_mutex. */ 1290 static int avs_condpaths_walk_all(struct avs_dev *adev, struct avs_path *path) 1291 { 1292 int ret; 1293 1294 ret = avs_condpaths_walk(adev, path, SNDRV_PCM_STREAM_CAPTURE); 1295 if (ret) 1296 return ret; 1297 1298 return avs_condpaths_walk(adev, path, SNDRV_PCM_STREAM_PLAYBACK); 1299 } 1300 1301 void avs_path_free(struct avs_path *path) 1302 { 1303 struct avs_path *cpath, *csave; 1304 struct avs_dev *adev = path->owner; 1305 1306 guard(mutex)(&adev->path_mutex); 1307 1308 /* Free all condpaths this path spawned. */ 1309 list_for_each_entry_safe(cpath, csave, &path->source_list, source_node) 1310 avs_condpath_free(path->owner, cpath); 1311 list_for_each_entry_safe(cpath, csave, &path->sink_list, sink_node) 1312 avs_condpath_free(path->owner, cpath); 1313 1314 avs_path_free_unlocked(path); 1315 } 1316 1317 struct avs_path *avs_path_create(struct avs_dev *adev, u32 dma_id, 1318 struct avs_tplg_path_template *template, 1319 struct snd_pcm_hw_params *fe_params, 1320 struct snd_pcm_hw_params *be_params) 1321 { 1322 struct avs_tplg_path *variant; 1323 struct avs_path *path; 1324 int ret; 1325 1326 variant = avs_path_find_variant(adev, template, fe_params, be_params); 1327 if (!variant) { 1328 dev_err(adev->dev, "no matching variant found\n"); 1329 return ERR_PTR(-ENOENT); 1330 } 1331 1332 /* Serialize path and its components creation. */ 1333 guard(mutex)(&adev->path_mutex); 1334 /* Satisfy needs of avs_path_find_tplg(). */ 1335 guard(mutex)(&adev->comp_list_mutex); 1336 1337 path = avs_path_create_unlocked(adev, dma_id, variant); 1338 if (IS_ERR(path)) 1339 return path; 1340 1341 ret = avs_condpaths_walk_all(adev, path); 1342 if (ret) { 1343 avs_path_free_unlocked(path); 1344 path = ERR_PTR(ret); 1345 } 1346 1347 return path; 1348 } 1349 1350 static int avs_path_bind_prepare(struct avs_dev *adev, 1351 struct avs_path_binding *binding) 1352 { 1353 const struct avs_audio_format *src_fmt, *sink_fmt; 1354 struct avs_tplg_module *tsource = binding->source->template; 1355 struct avs_path_module *source = binding->source; 1356 int ret; 1357 1358 /* 1359 * only copier modules about to be bound 1360 * to output pin other than 0 need preparation 1361 */ 1362 if (!binding->source_pin) 1363 return 0; 1364 if (!guid_equal(&tsource->cfg_ext->type, &AVS_COPIER_MOD_UUID)) 1365 return 0; 1366 1367 src_fmt = tsource->in_fmt; 1368 sink_fmt = binding->sink->template->in_fmt; 1369 1370 ret = avs_ipc_copier_set_sink_format(adev, source->module_id, 1371 source->instance_id, binding->source_pin, 1372 src_fmt, sink_fmt); 1373 if (ret) { 1374 dev_err(adev->dev, "config copier failed: %d\n", ret); 1375 return AVS_IPC_RET(ret); 1376 } 1377 1378 return 0; 1379 } 1380 1381 int avs_path_bind(struct avs_path *path) 1382 { 1383 struct avs_path_pipeline *ppl; 1384 struct avs_dev *adev = path->owner; 1385 int ret; 1386 1387 list_for_each_entry(ppl, &path->ppl_list, node) { 1388 struct avs_path_binding *binding; 1389 1390 list_for_each_entry(binding, &ppl->binding_list, node) { 1391 struct avs_path_module *source, *sink; 1392 1393 source = binding->source; 1394 sink = binding->sink; 1395 1396 ret = avs_path_bind_prepare(adev, binding); 1397 if (ret < 0) 1398 return ret; 1399 1400 ret = avs_ipc_bind(adev, source->module_id, 1401 source->instance_id, sink->module_id, 1402 sink->instance_id, binding->sink_pin, 1403 binding->source_pin); 1404 if (ret) { 1405 dev_err(adev->dev, "bind path failed: %d\n", ret); 1406 return AVS_IPC_RET(ret); 1407 } 1408 } 1409 } 1410 1411 return 0; 1412 } 1413 1414 int avs_path_unbind(struct avs_path *path) 1415 { 1416 struct avs_path_pipeline *ppl; 1417 struct avs_dev *adev = path->owner; 1418 int ret; 1419 1420 list_for_each_entry(ppl, &path->ppl_list, node) { 1421 struct avs_path_binding *binding; 1422 1423 list_for_each_entry(binding, &ppl->binding_list, node) { 1424 struct avs_path_module *source, *sink; 1425 1426 source = binding->source; 1427 sink = binding->sink; 1428 1429 ret = avs_ipc_unbind(adev, source->module_id, 1430 source->instance_id, sink->module_id, 1431 sink->instance_id, binding->sink_pin, 1432 binding->source_pin); 1433 if (ret) { 1434 dev_err(adev->dev, "unbind path failed: %d\n", ret); 1435 return AVS_IPC_RET(ret); 1436 } 1437 } 1438 } 1439 1440 return 0; 1441 } 1442 1443 int avs_path_reset(struct avs_path *path) 1444 { 1445 struct avs_path_pipeline *ppl; 1446 struct avs_dev *adev = path->owner; 1447 int ret; 1448 1449 if (path->state == AVS_PPL_STATE_RESET) 1450 return 0; 1451 1452 list_for_each_entry(ppl, &path->ppl_list, node) { 1453 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id, 1454 AVS_PPL_STATE_RESET); 1455 if (ret) { 1456 dev_err(adev->dev, "reset path failed: %d\n", ret); 1457 path->state = AVS_PPL_STATE_INVALID; 1458 return AVS_IPC_RET(ret); 1459 } 1460 } 1461 1462 path->state = AVS_PPL_STATE_RESET; 1463 return 0; 1464 } 1465 1466 static int avs_condpath_pause(struct avs_dev *adev, struct avs_path *cpath) 1467 { 1468 struct avs_path_pipeline *ppl; 1469 int ret; 1470 1471 if (cpath->state == AVS_PPL_STATE_PAUSED) 1472 return 0; 1473 1474 list_for_each_entry_reverse(ppl, &cpath->ppl_list, node) { 1475 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id, AVS_PPL_STATE_PAUSED); 1476 if (ret) { 1477 dev_err(adev->dev, "pause cpath failed: %d\n", ret); 1478 cpath->state = AVS_PPL_STATE_INVALID; 1479 return AVS_IPC_RET(ret); 1480 } 1481 } 1482 1483 cpath->state = AVS_PPL_STATE_PAUSED; 1484 return 0; 1485 } 1486 1487 static void avs_condpaths_pause(struct avs_dev *adev, struct avs_path *path) 1488 { 1489 struct avs_path *cpath; 1490 1491 guard(mutex)(&adev->path_mutex); 1492 1493 /* If either source or sink stops, so do the attached conditional paths. */ 1494 list_for_each_entry(cpath, &path->source_list, source_node) 1495 avs_condpath_pause(adev, cpath); 1496 list_for_each_entry(cpath, &path->sink_list, sink_node) 1497 avs_condpath_pause(adev, cpath); 1498 } 1499 1500 int avs_path_pause(struct avs_path *path) 1501 { 1502 struct avs_path_pipeline *ppl; 1503 struct avs_dev *adev = path->owner; 1504 int ret; 1505 1506 if (path->state == AVS_PPL_STATE_PAUSED) 1507 return 0; 1508 1509 avs_condpaths_pause(adev, path); 1510 1511 list_for_each_entry_reverse(ppl, &path->ppl_list, node) { 1512 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id, 1513 AVS_PPL_STATE_PAUSED); 1514 if (ret) { 1515 dev_err(adev->dev, "pause path failed: %d\n", ret); 1516 path->state = AVS_PPL_STATE_INVALID; 1517 return AVS_IPC_RET(ret); 1518 } 1519 } 1520 1521 path->state = AVS_PPL_STATE_PAUSED; 1522 return 0; 1523 } 1524 1525 static int avs_condpath_run(struct avs_dev *adev, struct avs_path *cpath, int trigger) 1526 { 1527 struct avs_path_pipeline *ppl; 1528 int ret; 1529 1530 if (cpath->state == AVS_PPL_STATE_RUNNING) 1531 return 0; 1532 1533 list_for_each_entry(ppl, &cpath->ppl_list, node) { 1534 if (ppl->template->cfg->trigger != trigger) 1535 continue; 1536 1537 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id, AVS_PPL_STATE_RUNNING); 1538 if (ret) { 1539 dev_err(adev->dev, "run cpath failed: %d\n", ret); 1540 cpath->state = AVS_PPL_STATE_INVALID; 1541 return AVS_IPC_RET(ret); 1542 } 1543 } 1544 1545 cpath->state = AVS_PPL_STATE_RUNNING; 1546 return 0; 1547 } 1548 1549 static void avs_condpaths_run(struct avs_dev *adev, struct avs_path *path, int trigger) 1550 { 1551 struct avs_path *cpath; 1552 1553 guard(mutex)(&adev->path_mutex); 1554 1555 /* Run conditional paths only if source and sink are both running. */ 1556 list_for_each_entry(cpath, &path->source_list, source_node) 1557 if (cpath->source->state == AVS_PPL_STATE_RUNNING && 1558 cpath->sink->state == AVS_PPL_STATE_RUNNING) 1559 avs_condpath_run(adev, cpath, trigger); 1560 1561 list_for_each_entry(cpath, &path->sink_list, sink_node) 1562 if (cpath->source->state == AVS_PPL_STATE_RUNNING && 1563 cpath->sink->state == AVS_PPL_STATE_RUNNING) 1564 avs_condpath_run(adev, cpath, trigger); 1565 } 1566 1567 int avs_path_run(struct avs_path *path, int trigger) 1568 { 1569 struct avs_path_pipeline *ppl; 1570 struct avs_dev *adev = path->owner; 1571 int ret; 1572 1573 if (path->state == AVS_PPL_STATE_RUNNING && trigger == AVS_TPLG_TRIGGER_AUTO) 1574 return 0; 1575 1576 list_for_each_entry(ppl, &path->ppl_list, node) { 1577 if (ppl->template->cfg->trigger != trigger) 1578 continue; 1579 1580 ret = avs_ipc_set_pipeline_state(adev, ppl->instance_id, 1581 AVS_PPL_STATE_RUNNING); 1582 if (ret) { 1583 dev_err(adev->dev, "run path failed: %d\n", ret); 1584 path->state = AVS_PPL_STATE_INVALID; 1585 return AVS_IPC_RET(ret); 1586 } 1587 } 1588 1589 path->state = AVS_PPL_STATE_RUNNING; 1590 1591 /* Granular pipeline triggering not intended for conditional paths. */ 1592 if (trigger == AVS_TPLG_TRIGGER_AUTO) 1593 avs_condpaths_run(adev, path, trigger); 1594 1595 return 0; 1596 } 1597