1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2022 Intel Corporation 7 // 8 9 #include <sound/pcm_params.h> 10 #include <sound/sof/ipc4/header.h> 11 #include "sof-audio.h" 12 #include "sof-priv.h" 13 #include "ops.h" 14 #include "ipc4-priv.h" 15 #include "ipc4-topology.h" 16 #include "ipc4-fw-reg.h" 17 18 /** 19 * struct sof_ipc4_timestamp_info - IPC4 timestamp info 20 * @host_copier: the host copier of the pcm stream 21 * @dai_copier: the dai copier of the pcm stream 22 * @stream_start_offset: reported by fw in memory window (converted to frames) 23 * @stream_end_offset: reported by fw in memory window (converted to frames) 24 * @llp_offset: llp offset in memory window 25 * @boundary: wrap boundary should be used for the LLP frame counter 26 * @delay: Calculated and stored in pointer callback. The stored value is 27 * returned in the delay callback. 28 */ 29 struct sof_ipc4_timestamp_info { 30 struct sof_ipc4_copier *host_copier; 31 struct sof_ipc4_copier *dai_copier; 32 u64 stream_start_offset; 33 u64 stream_end_offset; 34 u32 llp_offset; 35 36 u64 boundary; 37 snd_pcm_sframes_t delay; 38 }; 39 40 /** 41 * struct sof_ipc4_pcm_stream_priv - IPC4 specific private data 42 * @time_info: pointer to time info struct if it is supported, otherwise NULL 43 * @chain_dma_allocated: indicates the ChainDMA allocation state 44 */ 45 struct sof_ipc4_pcm_stream_priv { 46 struct sof_ipc4_timestamp_info *time_info; 47 48 bool chain_dma_allocated; 49 }; 50 51 static inline struct sof_ipc4_timestamp_info * 52 sof_ipc4_sps_to_time_info(struct snd_sof_pcm_stream *sps) 53 { 54 struct sof_ipc4_pcm_stream_priv *stream_priv = sps->private; 55 56 return stream_priv->time_info; 57 } 58 59 static 60 char *sof_ipc4_set_multi_pipeline_state_debug(struct snd_sof_dev *sdev, char *buf, size_t size, 61 struct ipc4_pipeline_set_state_data *trigger_list) 62 { 63 int i, offset = 0; 64 65 for (i = 0; i < trigger_list->count; i++) { 66 offset += snprintf(buf + offset, size - offset, " %d", 67 trigger_list->pipeline_instance_ids[i]); 68 69 if (offset >= size - 1) { 70 buf[size - 1] = '\0'; 71 break; 72 } 73 } 74 return buf; 75 } 76 77 static int sof_ipc4_set_multi_pipeline_state(struct snd_sof_dev *sdev, u32 state, 78 struct ipc4_pipeline_set_state_data *trigger_list) 79 { 80 struct sof_ipc4_msg msg = {{ 0 }}; 81 u32 primary, ipc_size; 82 char debug_buf[32]; 83 84 /* trigger a single pipeline */ 85 if (trigger_list->count == 1) 86 return sof_ipc4_set_pipeline_state(sdev, trigger_list->pipeline_instance_ids[0], 87 state); 88 89 dev_dbg(sdev->dev, "Set pipelines %s to state %d%s", 90 sof_ipc4_set_multi_pipeline_state_debug(sdev, debug_buf, sizeof(debug_buf), 91 trigger_list), 92 state, sof_ipc4_pipeline_state_str(state)); 93 94 primary = state; 95 primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_SET_PIPELINE_STATE); 96 primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 97 primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG); 98 msg.primary = primary; 99 100 /* trigger multiple pipelines with a single IPC */ 101 msg.extension = SOF_IPC4_GLB_PIPE_STATE_EXT_MULTI; 102 103 /* ipc_size includes the count and the pipeline IDs for the number of pipelines */ 104 ipc_size = sizeof(u32) * (trigger_list->count + 1); 105 msg.data_size = ipc_size; 106 msg.data_ptr = trigger_list; 107 108 return sof_ipc_tx_message_no_reply(sdev->ipc, &msg, ipc_size); 109 } 110 111 int sof_ipc4_set_pipeline_state(struct snd_sof_dev *sdev, u32 instance_id, u32 state) 112 { 113 struct sof_ipc4_msg msg = {{ 0 }}; 114 u32 primary; 115 116 dev_dbg(sdev->dev, "Set pipeline %d to state %d%s", instance_id, state, 117 sof_ipc4_pipeline_state_str(state)); 118 119 primary = state; 120 primary |= SOF_IPC4_GLB_PIPE_STATE_ID(instance_id); 121 primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_SET_PIPELINE_STATE); 122 primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 123 primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG); 124 125 msg.primary = primary; 126 127 return sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0); 128 } 129 EXPORT_SYMBOL(sof_ipc4_set_pipeline_state); 130 131 static void sof_ipc4_add_pipeline_by_priority(struct ipc4_pipeline_set_state_data *trigger_list, 132 struct snd_sof_widget *pipe_widget, 133 s8 *pipe_priority, bool ascend) 134 { 135 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 136 int i, j; 137 138 for (i = 0; i < trigger_list->count; i++) { 139 /* add pipeline from low priority to high */ 140 if (ascend && pipeline->priority < pipe_priority[i]) 141 break; 142 /* add pipeline from high priority to low */ 143 else if (!ascend && pipeline->priority > pipe_priority[i]) 144 break; 145 } 146 147 for (j = trigger_list->count - 1; j >= i; j--) { 148 trigger_list->pipeline_instance_ids[j + 1] = trigger_list->pipeline_instance_ids[j]; 149 pipe_priority[j + 1] = pipe_priority[j]; 150 } 151 152 trigger_list->pipeline_instance_ids[i] = pipe_widget->instance_id; 153 trigger_list->count++; 154 pipe_priority[i] = pipeline->priority; 155 } 156 157 static void 158 sof_ipc4_add_pipeline_to_trigger_list(struct snd_sof_dev *sdev, int state, 159 struct snd_sof_pipeline *spipe, 160 struct ipc4_pipeline_set_state_data *trigger_list, 161 s8 *pipe_priority) 162 { 163 struct snd_sof_widget *pipe_widget = spipe->pipe_widget; 164 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 165 166 if (pipeline->skip_during_fe_trigger && state != SOF_IPC4_PIPE_RESET) 167 return; 168 169 switch (state) { 170 case SOF_IPC4_PIPE_RUNNING: 171 /* 172 * Trigger pipeline if all PCMs containing it are paused or if it is RUNNING 173 * for the first time 174 */ 175 if (spipe->started_count == spipe->paused_count) 176 sof_ipc4_add_pipeline_by_priority(trigger_list, pipe_widget, pipe_priority, 177 false); 178 break; 179 case SOF_IPC4_PIPE_RESET: 180 /* RESET if the pipeline is neither running nor paused */ 181 if (!spipe->started_count && !spipe->paused_count) 182 sof_ipc4_add_pipeline_by_priority(trigger_list, pipe_widget, pipe_priority, 183 true); 184 break; 185 case SOF_IPC4_PIPE_PAUSED: 186 /* Pause the pipeline only when its started_count is 1 more than paused_count */ 187 if (spipe->paused_count == (spipe->started_count - 1)) 188 sof_ipc4_add_pipeline_by_priority(trigger_list, pipe_widget, pipe_priority, 189 true); 190 break; 191 default: 192 break; 193 } 194 } 195 196 static void 197 sof_ipc4_update_pipeline_state(struct snd_sof_dev *sdev, int state, int cmd, 198 struct snd_sof_pipeline *spipe, 199 struct ipc4_pipeline_set_state_data *trigger_list) 200 { 201 struct snd_sof_widget *pipe_widget = spipe->pipe_widget; 202 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 203 int i; 204 205 if (pipeline->skip_during_fe_trigger && state != SOF_IPC4_PIPE_RESET) 206 return; 207 208 /* set state for pipeline if it was just triggered */ 209 for (i = 0; i < trigger_list->count; i++) { 210 if (trigger_list->pipeline_instance_ids[i] == pipe_widget->instance_id) { 211 pipeline->state = state; 212 break; 213 } 214 } 215 216 switch (state) { 217 case SOF_IPC4_PIPE_PAUSED: 218 switch (cmd) { 219 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 220 /* 221 * increment paused_count if the PAUSED is the final state during 222 * the PAUSE trigger 223 */ 224 spipe->paused_count++; 225 break; 226 case SNDRV_PCM_TRIGGER_STOP: 227 case SNDRV_PCM_TRIGGER_SUSPEND: 228 /* 229 * decrement started_count if PAUSED is the final state during the 230 * STOP trigger 231 */ 232 spipe->started_count--; 233 break; 234 default: 235 break; 236 } 237 break; 238 case SOF_IPC4_PIPE_RUNNING: 239 switch (cmd) { 240 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 241 /* decrement paused_count for RELEASE */ 242 spipe->paused_count--; 243 break; 244 case SNDRV_PCM_TRIGGER_START: 245 case SNDRV_PCM_TRIGGER_RESUME: 246 /* increment started_count for START/RESUME */ 247 spipe->started_count++; 248 break; 249 default: 250 break; 251 } 252 break; 253 default: 254 break; 255 } 256 } 257 258 /* 259 * The picture below represents the pipeline state machine wrt PCM actions corresponding to the 260 * triggers and ioctls 261 * +---------------+ 262 * | | 263 * | INIT | 264 * | | 265 * +-------+-------+ 266 * | 267 * | 268 * | START 269 * | 270 * | 271 * +----------------+ +------v-------+ +-------------+ 272 * | | START | | HW_FREE | | 273 * | RUNNING <-------------+ PAUSED +--------------> + RESET | 274 * | | PAUSE | | | | 275 * +------+---------+ RELEASE +---------+----+ +-------------+ 276 * | ^ 277 * | | 278 * | | 279 * | | 280 * | PAUSE | 281 * +---------------------------------+ 282 * STOP/SUSPEND 283 * 284 * Note that during system suspend, the suspend trigger is followed by a hw_free in 285 * sof_pcm_trigger(). So, the final state during suspend would be RESET. 286 * Also, since the SOF driver doesn't support full resume, streams would be restarted with the 287 * prepare ioctl before the START trigger. 288 */ 289 290 /* 291 * Chained DMA is a special case where there is no processing on 292 * DSP. The samples are just moved over by host side DMA to a single 293 * buffer on DSP and directly from there to link DMA. However, the 294 * model on SOF driver has two notional pipelines, one at host DAI, 295 * and another at link DAI. They both shall have the use_chain_dma 296 * attribute. 297 */ 298 299 static int sof_ipc4_chain_dma_trigger(struct snd_sof_dev *sdev, 300 struct snd_sof_pcm *spcm, int direction, 301 struct snd_sof_pcm_stream_pipeline_list *pipeline_list, 302 int state, int cmd) 303 { 304 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 305 struct sof_ipc4_pcm_stream_priv *stream_priv; 306 bool allocate, enable, set_fifo_size; 307 struct sof_ipc4_msg msg = {{ 0 }}; 308 int ret, i; 309 310 stream_priv = spcm->stream[direction].private; 311 312 switch (state) { 313 case SOF_IPC4_PIPE_RUNNING: /* Allocate and start chained dma */ 314 allocate = true; 315 enable = true; 316 /* 317 * SOF assumes creation of a new stream from the presence of fifo_size 318 * in the message, so we must leave it out in pause release case. 319 */ 320 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) 321 set_fifo_size = false; 322 else 323 set_fifo_size = true; 324 break; 325 case SOF_IPC4_PIPE_PAUSED: /* Disable chained DMA. */ 326 allocate = true; 327 enable = false; 328 set_fifo_size = false; 329 break; 330 case SOF_IPC4_PIPE_RESET: /* Disable and free chained DMA. */ 331 332 /* ChainDMA can only be reset if it has been allocated */ 333 if (!stream_priv->chain_dma_allocated) 334 return 0; 335 336 allocate = false; 337 enable = false; 338 set_fifo_size = false; 339 break; 340 default: 341 spcm_err(spcm, direction, "Unexpected pipeline state %d\n", state); 342 return -EINVAL; 343 } 344 345 msg.primary = SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_CHAIN_DMA); 346 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 347 msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG); 348 349 /* 350 * To set-up the DMA chain, the host DMA ID and SCS setting 351 * are retrieved from the host pipeline configuration. Likewise 352 * the link DMA ID and fifo_size are retrieved from the link 353 * pipeline configuration. 354 */ 355 for (i = 0; i < pipeline_list->count; i++) { 356 struct snd_sof_pipeline *spipe = pipeline_list->pipelines[i]; 357 struct snd_sof_widget *pipe_widget = spipe->pipe_widget; 358 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 359 360 if (!pipeline->use_chain_dma) { 361 spcm_err(spcm, direction, 362 "All pipelines in chained DMA path should have use_chain_dma attribute set."); 363 return -EINVAL; 364 } 365 366 msg.primary |= pipeline->msg.primary; 367 368 /* Add fifo_size (actually DMA buffer size) field to the message */ 369 if (set_fifo_size) 370 msg.extension |= pipeline->msg.extension; 371 } 372 373 if (direction == SNDRV_PCM_STREAM_CAPTURE) { 374 /* 375 * For ChainDMA the DMA ids are unique with the following mapping: 376 * playback: 0 - (num_playback_streams - 1) 377 * capture: num_playback_streams - (num_playback_streams + 378 * num_capture_streams - 1) 379 * 380 * Add the num_playback_streams offset to the DMA ids stored in 381 * msg.primary in case capture 382 */ 383 msg.primary += SOF_IPC4_GLB_CHAIN_DMA_HOST_ID(ipc4_data->num_playback_streams); 384 msg.primary += SOF_IPC4_GLB_CHAIN_DMA_LINK_ID(ipc4_data->num_playback_streams); 385 } 386 387 if (allocate) 388 msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_ALLOCATE_MASK; 389 390 if (enable) 391 msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_ENABLE_MASK; 392 393 ret = sof_ipc_tx_message_no_reply(sdev->ipc, &msg, 0); 394 /* Update the ChainDMA allocation state */ 395 if (!ret) 396 stream_priv->chain_dma_allocated = allocate; 397 398 return ret; 399 } 400 401 static int sof_ipc4_trigger_pipelines(struct snd_soc_component *component, 402 struct snd_pcm_substream *substream, int state, int cmd) 403 { 404 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 405 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 406 struct snd_sof_pcm_stream_pipeline_list *pipeline_list; 407 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 408 struct ipc4_pipeline_set_state_data *trigger_list; 409 struct snd_sof_widget *pipe_widget; 410 struct sof_ipc4_pipeline *pipeline; 411 struct snd_sof_pipeline *spipe; 412 struct snd_sof_pcm *spcm; 413 u8 *pipe_priority; 414 int ret; 415 int i; 416 417 spcm = snd_sof_find_spcm_dai(component, rtd); 418 if (!spcm) 419 return -EINVAL; 420 421 spcm_dbg(spcm, substream->stream, "cmd: %d, state: %d\n", cmd, state); 422 423 pipeline_list = &spcm->stream[substream->stream].pipeline_list; 424 425 /* nothing to trigger if the list is empty */ 426 if (!pipeline_list->pipelines || !pipeline_list->count) 427 return 0; 428 429 spipe = pipeline_list->pipelines[0]; 430 pipe_widget = spipe->pipe_widget; 431 pipeline = pipe_widget->private; 432 433 /* 434 * If use_chain_dma attribute is set we proceed to chained DMA 435 * trigger function that handles the rest for the substream. 436 */ 437 if (pipeline->use_chain_dma) { 438 struct sof_ipc4_timestamp_info *time_info; 439 440 time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]); 441 442 ret = sof_ipc4_chain_dma_trigger(sdev, spcm, substream->stream, 443 pipeline_list, state, cmd); 444 if (ret || !time_info) 445 return ret; 446 447 if (state == SOF_IPC4_PIPE_PAUSED) { 448 /* 449 * Record the DAI position for delay reporting 450 * To handle multiple pause/resume/xrun we need to add 451 * the positions to simulate how the firmware behaves 452 */ 453 u64 pos = snd_sof_pcm_get_dai_frame_counter(sdev, component, 454 substream); 455 456 time_info->stream_end_offset += pos; 457 } else if (state == SOF_IPC4_PIPE_RESET) { 458 /* Reset the end offset as the stream is stopped */ 459 time_info->stream_end_offset = 0; 460 } 461 462 return 0; 463 } 464 465 /* allocate memory for the pipeline data */ 466 trigger_list = kzalloc(struct_size(trigger_list, pipeline_instance_ids, 467 pipeline_list->count), GFP_KERNEL); 468 if (!trigger_list) 469 return -ENOMEM; 470 471 pipe_priority = kzalloc(pipeline_list->count, GFP_KERNEL); 472 if (!pipe_priority) { 473 kfree(trigger_list); 474 return -ENOMEM; 475 } 476 477 mutex_lock(&ipc4_data->pipeline_state_mutex); 478 479 /* 480 * IPC4 requires pipelines to be triggered in order starting at the sink and 481 * walking all the way to the source. So traverse the pipeline_list in the order 482 * sink->source when starting PCM's and in the reverse order to pause/stop PCM's. 483 * Skip the pipelines that have their skip_during_fe_trigger flag set. If there is a fork 484 * in the pipeline, the order of triggering between the left/right paths will be 485 * indeterministic. But the sink->source trigger order sink->source would still be 486 * guaranteed for each fork independently. 487 */ 488 if (state == SOF_IPC4_PIPE_RUNNING || state == SOF_IPC4_PIPE_RESET) 489 for (i = pipeline_list->count - 1; i >= 0; i--) { 490 spipe = pipeline_list->pipelines[i]; 491 sof_ipc4_add_pipeline_to_trigger_list(sdev, state, spipe, trigger_list, 492 pipe_priority); 493 } 494 else 495 for (i = 0; i < pipeline_list->count; i++) { 496 spipe = pipeline_list->pipelines[i]; 497 sof_ipc4_add_pipeline_to_trigger_list(sdev, state, spipe, trigger_list, 498 pipe_priority); 499 } 500 501 /* return if all pipelines are in the requested state already */ 502 if (!trigger_list->count) { 503 ret = 0; 504 goto free; 505 } 506 507 /* no need to pause before reset or before pause release */ 508 if (state == SOF_IPC4_PIPE_RESET || cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) 509 goto skip_pause_transition; 510 511 /* 512 * set paused state for pipelines if the final state is PAUSED or when the pipeline 513 * is set to RUNNING for the first time after the PCM is started. 514 */ 515 ret = sof_ipc4_set_multi_pipeline_state(sdev, SOF_IPC4_PIPE_PAUSED, trigger_list); 516 if (ret < 0) { 517 spcm_err(spcm, substream->stream, "failed to pause all pipelines\n"); 518 goto free; 519 } 520 521 /* update PAUSED state for all pipelines just triggered */ 522 for (i = 0; i < pipeline_list->count ; i++) { 523 spipe = pipeline_list->pipelines[i]; 524 sof_ipc4_update_pipeline_state(sdev, SOF_IPC4_PIPE_PAUSED, cmd, spipe, 525 trigger_list); 526 } 527 528 /* return if this is the final state */ 529 if (state == SOF_IPC4_PIPE_PAUSED) { 530 struct sof_ipc4_timestamp_info *time_info; 531 532 /* 533 * Invalidate the stream_start_offset to make sure that it is 534 * going to be updated if the stream resumes 535 */ 536 time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]); 537 if (time_info) 538 time_info->stream_start_offset = SOF_IPC4_INVALID_STREAM_POSITION; 539 540 goto free; 541 } 542 skip_pause_transition: 543 /* else set the RUNNING/RESET state in the DSP */ 544 ret = sof_ipc4_set_multi_pipeline_state(sdev, state, trigger_list); 545 if (ret < 0) { 546 spcm_err(spcm, substream->stream, 547 "failed to set final state %d for all pipelines\n", 548 state); 549 /* 550 * workaround: if the firmware is crashed while setting the 551 * pipelines to reset state we must ignore the error code and 552 * reset it to 0. 553 * Since the firmware is crashed we will not send IPC messages 554 * and we are going to see errors printed, but the state of the 555 * widgets will be correct for the next boot. 556 */ 557 if (sdev->fw_state != SOF_FW_CRASHED || state != SOF_IPC4_PIPE_RESET) 558 goto free; 559 560 ret = 0; 561 } 562 563 /* update RUNNING/RESET state for all pipelines that were just triggered */ 564 for (i = 0; i < pipeline_list->count; i++) { 565 spipe = pipeline_list->pipelines[i]; 566 sof_ipc4_update_pipeline_state(sdev, state, cmd, spipe, trigger_list); 567 } 568 569 free: 570 mutex_unlock(&ipc4_data->pipeline_state_mutex); 571 kfree(trigger_list); 572 kfree(pipe_priority); 573 return ret; 574 } 575 576 static int sof_ipc4_pcm_trigger(struct snd_soc_component *component, 577 struct snd_pcm_substream *substream, int cmd) 578 { 579 int state; 580 581 /* determine the pipeline state */ 582 switch (cmd) { 583 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 584 case SNDRV_PCM_TRIGGER_RESUME: 585 case SNDRV_PCM_TRIGGER_START: 586 state = SOF_IPC4_PIPE_RUNNING; 587 break; 588 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 589 case SNDRV_PCM_TRIGGER_SUSPEND: 590 case SNDRV_PCM_TRIGGER_STOP: 591 state = SOF_IPC4_PIPE_PAUSED; 592 break; 593 default: 594 dev_err(component->dev, "%s: unhandled trigger cmd %d\n", __func__, cmd); 595 return -EINVAL; 596 } 597 598 /* set the pipeline state */ 599 return sof_ipc4_trigger_pipelines(component, substream, state, cmd); 600 } 601 602 static int sof_ipc4_pcm_hw_free(struct snd_soc_component *component, 603 struct snd_pcm_substream *substream) 604 { 605 /* command is not relevant with RESET, so just pass 0 */ 606 return sof_ipc4_trigger_pipelines(component, substream, SOF_IPC4_PIPE_RESET, 0); 607 } 608 609 static int ipc4_ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, 610 const char *link_name, 611 struct snd_pcm_hw_params *params) 612 { 613 struct snd_sof_dai_link *slink; 614 struct snd_sof_dai *dai; 615 bool dai_link_found = false; 616 int current_config = -1; 617 bool partial_match; 618 int i; 619 620 list_for_each_entry(slink, &sdev->dai_link_list, list) { 621 if (!strcmp(slink->link->name, link_name)) { 622 dai_link_found = true; 623 break; 624 } 625 } 626 627 if (!dai_link_found) 628 return 0; 629 630 /* 631 * Find the first best matching hardware config: 632 * rate + format + channels are matching 633 * rate + channel are matching 634 * 635 * The copier cannot do rate and/or channel conversion. 636 */ 637 for (i = 0; i < slink->num_hw_configs; i++) { 638 struct snd_soc_tplg_hw_config *hw_config = &slink->hw_configs[i]; 639 640 if (params_rate(params) == le32_to_cpu(hw_config->fsync_rate) && 641 params_width(params) == le32_to_cpu(hw_config->tdm_slot_width) && 642 params_channels(params) == le32_to_cpu(hw_config->tdm_slots)) { 643 current_config = le32_to_cpu(hw_config->id); 644 partial_match = false; 645 /* best match found */ 646 break; 647 } else if (current_config < 0 && 648 params_rate(params) == le32_to_cpu(hw_config->fsync_rate) && 649 params_channels(params) == le32_to_cpu(hw_config->tdm_slots)) { 650 current_config = le32_to_cpu(hw_config->id); 651 partial_match = true; 652 /* keep looking for better match */ 653 } 654 } 655 656 if (current_config < 0) { 657 dev_err(sdev->dev, 658 "%s: No suitable hw_config found for %s (num_hw_configs: %d)\n", 659 __func__, slink->link->name, slink->num_hw_configs); 660 return -EINVAL; 661 } 662 663 dev_dbg(sdev->dev, 664 "hw_config for %s: %d (num_hw_configs: %d) with %s match\n", 665 slink->link->name, current_config, slink->num_hw_configs, 666 partial_match ? "partial" : "full"); 667 list_for_each_entry(dai, &sdev->dai_list, list) 668 if (!strcmp(slink->link->name, dai->name)) 669 dai->current_config = current_config; 670 671 return 0; 672 } 673 674 /* 675 * Fixup DAI link parameters for sampling rate based on 676 * DAI copier configuration. 677 */ 678 static int sof_ipc4_pcm_dai_link_fixup_rate(struct snd_sof_dev *sdev, 679 struct snd_pcm_hw_params *params, 680 struct sof_ipc4_copier *ipc4_copier) 681 { 682 struct sof_ipc4_pin_format *pin_fmts = ipc4_copier->available_fmt.input_pin_fmts; 683 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 684 int num_input_formats = ipc4_copier->available_fmt.num_input_formats; 685 unsigned int fe_rate = params_rate(params); 686 bool fe_be_rate_match = false; 687 bool single_be_rate = true; 688 unsigned int be_rate; 689 int i; 690 691 if (WARN_ON_ONCE(!num_input_formats)) 692 return -EINVAL; 693 694 /* 695 * Copier does not change sampling rate, so we 696 * need to only consider the input pin information. 697 */ 698 be_rate = pin_fmts[0].audio_fmt.sampling_frequency; 699 for (i = 0; i < num_input_formats; i++) { 700 unsigned int val = pin_fmts[i].audio_fmt.sampling_frequency; 701 702 if (val != be_rate) 703 single_be_rate = false; 704 705 if (val == fe_rate) { 706 fe_be_rate_match = true; 707 break; 708 } 709 } 710 711 /* 712 * If rate is different than FE rate, topology must 713 * contain an SRC. But we do require topology to 714 * define a single rate in the DAI copier config in 715 * this case (FE rate may be variable). 716 */ 717 if (!fe_be_rate_match) { 718 if (!single_be_rate) { 719 dev_err(sdev->dev, "Unable to select sampling rate for DAI link\n"); 720 return -EINVAL; 721 } 722 723 rate->min = be_rate; 724 rate->max = rate->min; 725 } 726 727 return 0; 728 } 729 730 static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, 731 struct snd_pcm_hw_params *params) 732 { 733 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 734 struct snd_sof_dai *dai = snd_sof_find_dai(component, rtd->dai_link->name); 735 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 736 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 737 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 738 struct sof_ipc4_audio_format *ipc4_fmt; 739 struct sof_ipc4_copier *ipc4_copier; 740 bool single_bitdepth = false; 741 u32 valid_bits = 0; 742 int dir, ret; 743 744 if (!dai) { 745 dev_err(component->dev, "%s: No DAI found with name %s\n", __func__, 746 rtd->dai_link->name); 747 return -EINVAL; 748 } 749 750 ipc4_copier = dai->private; 751 if (!ipc4_copier) { 752 dev_err(component->dev, "%s: No private data found for DAI %s\n", 753 __func__, rtd->dai_link->name); 754 return -EINVAL; 755 } 756 757 for_each_pcm_streams(dir) { 758 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, dir); 759 760 if (w) { 761 struct sof_ipc4_available_audio_format *available_fmt = 762 &ipc4_copier->available_fmt; 763 struct snd_sof_widget *swidget = w->dobj.private; 764 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; 765 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 766 767 /* Chain DMA does not use copiers, so no fixup needed */ 768 if (pipeline->use_chain_dma) 769 return 0; 770 771 if (dir == SNDRV_PCM_STREAM_PLAYBACK) { 772 if (sof_ipc4_copier_is_single_bitdepth(sdev, 773 available_fmt->output_pin_fmts, 774 available_fmt->num_output_formats)) { 775 ipc4_fmt = &available_fmt->output_pin_fmts->audio_fmt; 776 single_bitdepth = true; 777 } 778 } else { 779 if (sof_ipc4_copier_is_single_bitdepth(sdev, 780 available_fmt->input_pin_fmts, 781 available_fmt->num_input_formats)) { 782 ipc4_fmt = &available_fmt->input_pin_fmts->audio_fmt; 783 single_bitdepth = true; 784 } 785 } 786 } 787 } 788 789 ret = sof_ipc4_pcm_dai_link_fixup_rate(sdev, params, ipc4_copier); 790 if (ret) 791 return ret; 792 793 if (single_bitdepth) { 794 snd_mask_none(fmt); 795 valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(ipc4_fmt->fmt_cfg); 796 dev_dbg(component->dev, "Set %s to %d bit format\n", dai->name, valid_bits); 797 } 798 799 /* Set format if it is specified */ 800 switch (valid_bits) { 801 case 16: 802 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); 803 break; 804 case 24: 805 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 806 break; 807 case 32: 808 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE); 809 break; 810 default: 811 break; 812 } 813 814 if (ipc4_copier->dai_type == SOF_DAI_INTEL_SSP) 815 return ipc4_ssp_dai_config_pcm_params_match(sdev, 816 (char *)rtd->dai_link->name, 817 params); 818 819 return 0; 820 } 821 822 static void sof_ipc4_pcm_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm) 823 { 824 struct snd_sof_pcm_stream_pipeline_list *pipeline_list; 825 struct sof_ipc4_pcm_stream_priv *stream_priv; 826 int stream; 827 828 for_each_pcm_streams(stream) { 829 pipeline_list = &spcm->stream[stream].pipeline_list; 830 kfree(pipeline_list->pipelines); 831 pipeline_list->pipelines = NULL; 832 833 stream_priv = spcm->stream[stream].private; 834 kfree(stream_priv->time_info); 835 kfree(spcm->stream[stream].private); 836 spcm->stream[stream].private = NULL; 837 } 838 } 839 840 static int sof_ipc4_pcm_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm) 841 { 842 struct snd_sof_pcm_stream_pipeline_list *pipeline_list; 843 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 844 struct sof_ipc4_pcm_stream_priv *stream_priv; 845 struct sof_ipc4_timestamp_info *time_info; 846 bool support_info = true; 847 u32 abi_version; 848 u32 abi_offset; 849 int stream; 850 851 abi_offset = offsetof(struct sof_ipc4_fw_registers, abi_ver); 852 sof_mailbox_read(sdev, sdev->fw_info_box.offset + abi_offset, &abi_version, 853 sizeof(abi_version)); 854 855 if (abi_version < SOF_IPC4_FW_REGS_ABI_VER) 856 support_info = false; 857 858 /* For delay reporting the get_host_byte_counter callback is needed */ 859 if (!sof_ops(sdev) || !sof_ops(sdev)->get_host_byte_counter) 860 support_info = false; 861 862 for_each_pcm_streams(stream) { 863 pipeline_list = &spcm->stream[stream].pipeline_list; 864 865 /* allocate memory for max number of pipeline IDs */ 866 pipeline_list->pipelines = kcalloc(ipc4_data->max_num_pipelines, 867 sizeof(*pipeline_list->pipelines), 868 GFP_KERNEL); 869 if (!pipeline_list->pipelines) { 870 sof_ipc4_pcm_free(sdev, spcm); 871 return -ENOMEM; 872 } 873 874 stream_priv = kzalloc(sizeof(*stream_priv), GFP_KERNEL); 875 if (!stream_priv) { 876 sof_ipc4_pcm_free(sdev, spcm); 877 return -ENOMEM; 878 } 879 880 spcm->stream[stream].private = stream_priv; 881 882 /* Delay reporting is only supported on playback */ 883 if (!support_info || stream == SNDRV_PCM_STREAM_CAPTURE) 884 continue; 885 886 time_info = kzalloc(sizeof(*time_info), GFP_KERNEL); 887 if (!time_info) { 888 sof_ipc4_pcm_free(sdev, spcm); 889 return -ENOMEM; 890 } 891 892 stream_priv->time_info = time_info; 893 } 894 895 return 0; 896 } 897 898 static void sof_ipc4_build_time_info(struct snd_sof_dev *sdev, struct snd_sof_pcm_stream *sps) 899 { 900 struct sof_ipc4_copier *host_copier = NULL; 901 struct sof_ipc4_copier *dai_copier = NULL; 902 struct sof_ipc4_llp_reading_slot llp_slot; 903 struct sof_ipc4_timestamp_info *time_info; 904 struct snd_soc_dapm_widget *widget; 905 struct snd_sof_dai *dai; 906 int i; 907 908 /* find host & dai to locate info in memory window */ 909 for_each_dapm_widgets(sps->list, i, widget) { 910 struct snd_sof_widget *swidget = widget->dobj.private; 911 912 if (!swidget) 913 continue; 914 915 if (WIDGET_IS_AIF(swidget->widget->id)) { 916 host_copier = swidget->private; 917 } else if (WIDGET_IS_DAI(swidget->widget->id)) { 918 dai = swidget->private; 919 dai_copier = dai->private; 920 } 921 } 922 923 /* both host and dai copier must be valid for time_info */ 924 if (!host_copier || !dai_copier) { 925 dev_err(sdev->dev, "host or dai copier are not found\n"); 926 return; 927 } 928 929 time_info = sof_ipc4_sps_to_time_info(sps); 930 time_info->host_copier = host_copier; 931 time_info->dai_copier = dai_copier; 932 time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers, 933 llp_gpdma_reading_slots) + sdev->fw_info_box.offset; 934 935 /* find llp slot used by current dai */ 936 for (i = 0; i < SOF_IPC4_MAX_LLP_GPDMA_READING_SLOTS; i++) { 937 sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot)); 938 if (llp_slot.node_id == dai_copier->data.gtw_cfg.node_id) 939 break; 940 941 time_info->llp_offset += sizeof(llp_slot); 942 } 943 944 if (i < SOF_IPC4_MAX_LLP_GPDMA_READING_SLOTS) 945 return; 946 947 /* if no llp gpdma slot is used, check aggregated sdw slot */ 948 time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers, 949 llp_sndw_reading_slots) + sdev->fw_info_box.offset; 950 for (i = 0; i < SOF_IPC4_MAX_LLP_SNDW_READING_SLOTS; i++) { 951 sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot)); 952 if (llp_slot.node_id == dai_copier->data.gtw_cfg.node_id) 953 break; 954 955 time_info->llp_offset += sizeof(llp_slot); 956 } 957 958 if (i < SOF_IPC4_MAX_LLP_SNDW_READING_SLOTS) 959 return; 960 961 /* check EVAD slot */ 962 time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers, 963 llp_evad_reading_slot) + sdev->fw_info_box.offset; 964 sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot)); 965 if (llp_slot.node_id != dai_copier->data.gtw_cfg.node_id) 966 time_info->llp_offset = 0; 967 } 968 969 static int sof_ipc4_pcm_hw_params(struct snd_soc_component *component, 970 struct snd_pcm_substream *substream, 971 struct snd_pcm_hw_params *params, 972 struct snd_sof_platform_stream_params *platform_params) 973 { 974 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 975 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 976 struct sof_ipc4_timestamp_info *time_info; 977 struct snd_sof_pcm *spcm; 978 979 spcm = snd_sof_find_spcm_dai(component, rtd); 980 if (!spcm) 981 return -EINVAL; 982 983 time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]); 984 /* delay calculation is not supported by current fw_reg ABI */ 985 if (!time_info) 986 return 0; 987 988 time_info->stream_start_offset = SOF_IPC4_INVALID_STREAM_POSITION; 989 time_info->llp_offset = 0; 990 991 sof_ipc4_build_time_info(sdev, &spcm->stream[substream->stream]); 992 993 return 0; 994 } 995 996 static int sof_ipc4_get_stream_start_offset(struct snd_sof_dev *sdev, 997 struct snd_pcm_substream *substream, 998 struct snd_sof_pcm_stream *sps, 999 struct sof_ipc4_timestamp_info *time_info) 1000 { 1001 struct sof_ipc4_copier *host_copier = time_info->host_copier; 1002 struct sof_ipc4_copier *dai_copier = time_info->dai_copier; 1003 struct sof_ipc4_pipeline_registers ppl_reg; 1004 u32 dai_sample_size; 1005 u32 ch, node_index; 1006 u32 offset; 1007 1008 if (!host_copier || !dai_copier) 1009 return -EINVAL; 1010 1011 if (host_copier->data.gtw_cfg.node_id == SOF_IPC4_INVALID_NODE_ID) { 1012 return -EINVAL; 1013 } else if (host_copier->data.gtw_cfg.node_id == SOF_IPC4_CHAIN_DMA_NODE_ID) { 1014 /* 1015 * While the firmware does not supports time_info reporting for 1016 * streams using ChainDMA, it is granted that ChainDMA can only 1017 * be used on Host+Link pairs where the link position is 1018 * accessible from the host side. 1019 * 1020 * Enable delay calculation in case of ChainDMA via host 1021 * accessible registers. 1022 * 1023 * The ChainDMA uses 2x 1ms ping-pong buffer, dai side starts 1024 * when 1ms data is available 1025 */ 1026 time_info->stream_start_offset = substream->runtime->rate / MSEC_PER_SEC; 1027 goto out; 1028 } 1029 1030 node_index = SOF_IPC4_NODE_INDEX(host_copier->data.gtw_cfg.node_id); 1031 offset = offsetof(struct sof_ipc4_fw_registers, pipeline_regs) + node_index * sizeof(ppl_reg); 1032 sof_mailbox_read(sdev, sdev->fw_info_box.offset + offset, &ppl_reg, sizeof(ppl_reg)); 1033 if (ppl_reg.stream_start_offset == SOF_IPC4_INVALID_STREAM_POSITION) 1034 return -EINVAL; 1035 1036 ch = dai_copier->data.out_format.fmt_cfg; 1037 ch = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(ch); 1038 dai_sample_size = (dai_copier->data.out_format.bit_depth >> 3) * ch; 1039 1040 /* convert offsets to frame count */ 1041 time_info->stream_start_offset = ppl_reg.stream_start_offset; 1042 do_div(time_info->stream_start_offset, dai_sample_size); 1043 time_info->stream_end_offset = ppl_reg.stream_end_offset; 1044 do_div(time_info->stream_end_offset, dai_sample_size); 1045 1046 out: 1047 /* 1048 * Calculate the wrap boundary need to be used for delay calculation 1049 * The host counter is in bytes, it will wrap earlier than the frames 1050 * based link counter. 1051 */ 1052 time_info->boundary = div64_u64(~((u64)0), 1053 frames_to_bytes(substream->runtime, 1)); 1054 /* Initialize the delay value to 0 (no delay) */ 1055 time_info->delay = 0; 1056 1057 return 0; 1058 } 1059 1060 static int sof_ipc4_pcm_pointer(struct snd_soc_component *component, 1061 struct snd_pcm_substream *substream, 1062 snd_pcm_uframes_t *pointer) 1063 { 1064 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 1065 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1066 struct sof_ipc4_timestamp_info *time_info; 1067 struct sof_ipc4_llp_reading_slot llp; 1068 snd_pcm_uframes_t head_cnt, tail_cnt; 1069 struct snd_sof_pcm_stream *sps; 1070 u64 dai_cnt, host_cnt, host_ptr; 1071 struct snd_sof_pcm *spcm; 1072 int ret; 1073 1074 spcm = snd_sof_find_spcm_dai(component, rtd); 1075 if (!spcm) 1076 return -EOPNOTSUPP; 1077 1078 sps = &spcm->stream[substream->stream]; 1079 time_info = sof_ipc4_sps_to_time_info(sps); 1080 if (!time_info) 1081 return -EOPNOTSUPP; 1082 1083 /* 1084 * stream_start_offset is updated to memory window by FW based on 1085 * pipeline statistics and it may be invalid if host query happens before 1086 * the statistics is complete. And it will not change after the first initiailization. 1087 */ 1088 if (time_info->stream_start_offset == SOF_IPC4_INVALID_STREAM_POSITION) { 1089 ret = sof_ipc4_get_stream_start_offset(sdev, substream, sps, time_info); 1090 if (ret < 0) 1091 return -EOPNOTSUPP; 1092 } 1093 1094 /* For delay calculation we need the host counter */ 1095 host_cnt = snd_sof_pcm_get_host_byte_counter(sdev, component, substream); 1096 host_ptr = host_cnt; 1097 1098 /* convert the host_cnt to frames */ 1099 host_cnt = div64_u64(host_cnt, frames_to_bytes(substream->runtime, 1)); 1100 1101 /* 1102 * If the LLP counter is not reported by firmware in the SRAM window 1103 * then read the dai (link) counter via host accessible means if 1104 * available. 1105 */ 1106 if (!time_info->llp_offset) { 1107 dai_cnt = snd_sof_pcm_get_dai_frame_counter(sdev, component, substream); 1108 if (!dai_cnt) 1109 return -EOPNOTSUPP; 1110 } else { 1111 sof_mailbox_read(sdev, time_info->llp_offset, &llp, sizeof(llp)); 1112 dai_cnt = ((u64)llp.reading.llp_u << 32) | llp.reading.llp_l; 1113 } 1114 dai_cnt += time_info->stream_end_offset; 1115 1116 /* In two cases dai dma counter is not accurate 1117 * (1) dai pipeline is started before host pipeline 1118 * (2) multiple streams mixed into one. Each stream has the same dai dma 1119 * counter 1120 * 1121 * Firmware calculates correct stream_start_offset for all cases 1122 * including above two. 1123 * Driver subtracts stream_start_offset from dai dma counter to get 1124 * accurate one 1125 */ 1126 1127 /* 1128 * On stream start the dai counter might not yet have reached the 1129 * stream_start_offset value which means that no frames have left the 1130 * DSP yet from the audio stream (on playback, capture streams have 1131 * offset of 0 as we start capturing right away). 1132 * In this case we need to adjust the distance between the counters by 1133 * increasing the host counter by (offset - dai_counter). 1134 * Otherwise the dai_counter needs to be adjusted to reflect the number 1135 * of valid frames passed on the DAI side. 1136 * 1137 * The delay is the difference between the counters on the two 1138 * sides of the DSP. 1139 */ 1140 if (dai_cnt < time_info->stream_start_offset) { 1141 host_cnt += time_info->stream_start_offset - dai_cnt; 1142 dai_cnt = 0; 1143 } else { 1144 dai_cnt -= time_info->stream_start_offset; 1145 } 1146 1147 /* Wrap the dai counter at the boundary where the host counter wraps */ 1148 div64_u64_rem(dai_cnt, time_info->boundary, &dai_cnt); 1149 1150 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1151 head_cnt = host_cnt; 1152 tail_cnt = dai_cnt; 1153 } else { 1154 head_cnt = dai_cnt; 1155 tail_cnt = host_cnt; 1156 } 1157 1158 if (head_cnt < tail_cnt) { 1159 time_info->delay = time_info->boundary - tail_cnt + head_cnt; 1160 goto out; 1161 } 1162 1163 time_info->delay = head_cnt - tail_cnt; 1164 1165 out: 1166 /* 1167 * Convert the host byte counter to PCM pointer which wraps in buffer 1168 * and it is in frames 1169 */ 1170 div64_u64_rem(host_ptr, snd_pcm_lib_buffer_bytes(substream), &host_ptr); 1171 *pointer = bytes_to_frames(substream->runtime, host_ptr); 1172 1173 return 0; 1174 } 1175 1176 static snd_pcm_sframes_t sof_ipc4_pcm_delay(struct snd_soc_component *component, 1177 struct snd_pcm_substream *substream) 1178 { 1179 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1180 struct sof_ipc4_timestamp_info *time_info; 1181 struct snd_sof_pcm *spcm; 1182 1183 spcm = snd_sof_find_spcm_dai(component, rtd); 1184 if (!spcm) 1185 return 0; 1186 1187 time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]); 1188 /* 1189 * Report the stored delay value calculated in the pointer callback. 1190 * In the unlikely event that the calculation was skipped/aborted, the 1191 * default 0 delay returned. 1192 */ 1193 if (time_info) 1194 return time_info->delay; 1195 1196 /* No delay information available, report 0 as delay */ 1197 return 0; 1198 1199 } 1200 1201 const struct sof_ipc_pcm_ops ipc4_pcm_ops = { 1202 .hw_params = sof_ipc4_pcm_hw_params, 1203 .trigger = sof_ipc4_pcm_trigger, 1204 .hw_free = sof_ipc4_pcm_hw_free, 1205 .dai_link_fixup = sof_ipc4_pcm_dai_link_fixup, 1206 .pcm_setup = sof_ipc4_pcm_setup, 1207 .pcm_free = sof_ipc4_pcm_free, 1208 .pointer = sof_ipc4_pcm_pointer, 1209 .delay = sof_ipc4_pcm_delay, 1210 .ipc_first_on_start = true, 1211 .platform_stop_during_hw_free = true, 1212 }; 1213