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_channels(struct snd_sof_dev *sdev, 731 struct snd_pcm_hw_params *params, 732 struct sof_ipc4_copier *ipc4_copier) 733 { 734 struct sof_ipc4_pin_format *pin_fmts = ipc4_copier->available_fmt.input_pin_fmts; 735 struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 736 int num_input_formats = ipc4_copier->available_fmt.num_input_formats; 737 unsigned int fe_channels = params_channels(params); 738 bool fe_be_match = false; 739 bool single_be_channels = true; 740 unsigned int be_channels, val; 741 int i; 742 743 if (WARN_ON_ONCE(!num_input_formats)) 744 return -EINVAL; 745 746 /* 747 * Copier does not change channels, so we 748 * need to only consider the input pin information. 749 */ 750 be_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(pin_fmts[0].audio_fmt.fmt_cfg); 751 for (i = 0; i < num_input_formats; i++) { 752 val = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(pin_fmts[i].audio_fmt.fmt_cfg); 753 754 if (val != be_channels) 755 single_be_channels = false; 756 757 if (val == fe_channels) { 758 fe_be_match = true; 759 break; 760 } 761 } 762 763 /* 764 * If channels is different than FE channels, topology must contain a 765 * module which can change the number of channels. But we do require 766 * topology to define a single channels in the DAI copier config in 767 * this case (FE channels may be variable). 768 */ 769 if (!fe_be_match) { 770 if (!single_be_channels) { 771 dev_err(sdev->dev, "Unable to select channels for DAI link\n"); 772 return -EINVAL; 773 } 774 775 channels->min = be_channels; 776 channels->max = be_channels; 777 } 778 779 return 0; 780 } 781 782 static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, 783 struct snd_pcm_hw_params *params) 784 { 785 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 786 struct snd_sof_dai *dai = snd_sof_find_dai(component, rtd->dai_link->name); 787 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 788 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 789 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 790 struct sof_ipc4_audio_format *ipc4_fmt; 791 struct sof_ipc4_copier *ipc4_copier; 792 bool single_bitdepth = false; 793 u32 valid_bits = 0; 794 int dir, ret; 795 796 if (!dai) { 797 dev_err(component->dev, "%s: No DAI found with name %s\n", __func__, 798 rtd->dai_link->name); 799 return -EINVAL; 800 } 801 802 ipc4_copier = dai->private; 803 if (!ipc4_copier) { 804 dev_err(component->dev, "%s: No private data found for DAI %s\n", 805 __func__, rtd->dai_link->name); 806 return -EINVAL; 807 } 808 809 for_each_pcm_streams(dir) { 810 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, dir); 811 812 if (w) { 813 struct sof_ipc4_available_audio_format *available_fmt = 814 &ipc4_copier->available_fmt; 815 struct snd_sof_widget *swidget = w->dobj.private; 816 struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget; 817 struct sof_ipc4_pipeline *pipeline = pipe_widget->private; 818 819 /* Chain DMA does not use copiers, so no fixup needed */ 820 if (pipeline->use_chain_dma) 821 return 0; 822 823 if (dir == SNDRV_PCM_STREAM_PLAYBACK) { 824 if (sof_ipc4_copier_is_single_bitdepth(sdev, 825 available_fmt->output_pin_fmts, 826 available_fmt->num_output_formats)) { 827 ipc4_fmt = &available_fmt->output_pin_fmts->audio_fmt; 828 single_bitdepth = true; 829 } 830 } else { 831 if (sof_ipc4_copier_is_single_bitdepth(sdev, 832 available_fmt->input_pin_fmts, 833 available_fmt->num_input_formats)) { 834 ipc4_fmt = &available_fmt->input_pin_fmts->audio_fmt; 835 single_bitdepth = true; 836 } 837 } 838 } 839 } 840 841 ret = sof_ipc4_pcm_dai_link_fixup_rate(sdev, params, ipc4_copier); 842 if (ret) 843 return ret; 844 845 ret = sof_ipc4_pcm_dai_link_fixup_channels(sdev, params, ipc4_copier); 846 if (ret) 847 return ret; 848 849 if (single_bitdepth) { 850 snd_mask_none(fmt); 851 valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(ipc4_fmt->fmt_cfg); 852 dev_dbg(component->dev, "Set %s to %d bit format\n", dai->name, valid_bits); 853 } 854 855 /* Set format if it is specified */ 856 switch (valid_bits) { 857 case 16: 858 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); 859 break; 860 case 24: 861 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 862 break; 863 case 32: 864 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE); 865 break; 866 default: 867 break; 868 } 869 870 if (ipc4_copier->dai_type == SOF_DAI_INTEL_SSP) 871 return ipc4_ssp_dai_config_pcm_params_match(sdev, 872 (char *)rtd->dai_link->name, 873 params); 874 875 return 0; 876 } 877 878 static void sof_ipc4_pcm_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm) 879 { 880 struct snd_sof_pcm_stream_pipeline_list *pipeline_list; 881 struct sof_ipc4_pcm_stream_priv *stream_priv; 882 int stream; 883 884 for_each_pcm_streams(stream) { 885 pipeline_list = &spcm->stream[stream].pipeline_list; 886 kfree(pipeline_list->pipelines); 887 pipeline_list->pipelines = NULL; 888 889 stream_priv = spcm->stream[stream].private; 890 kfree(stream_priv->time_info); 891 kfree(spcm->stream[stream].private); 892 spcm->stream[stream].private = NULL; 893 } 894 } 895 896 static int sof_ipc4_pcm_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm) 897 { 898 struct snd_sof_pcm_stream_pipeline_list *pipeline_list; 899 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 900 struct sof_ipc4_pcm_stream_priv *stream_priv; 901 struct sof_ipc4_timestamp_info *time_info; 902 bool support_info = true; 903 u32 abi_version; 904 u32 abi_offset; 905 int stream; 906 907 abi_offset = offsetof(struct sof_ipc4_fw_registers, abi_ver); 908 sof_mailbox_read(sdev, sdev->fw_info_box.offset + abi_offset, &abi_version, 909 sizeof(abi_version)); 910 911 if (abi_version < SOF_IPC4_FW_REGS_ABI_VER) 912 support_info = false; 913 914 /* For delay reporting the get_host_byte_counter callback is needed */ 915 if (!sof_ops(sdev) || !sof_ops(sdev)->get_host_byte_counter) 916 support_info = false; 917 918 for_each_pcm_streams(stream) { 919 pipeline_list = &spcm->stream[stream].pipeline_list; 920 921 /* allocate memory for max number of pipeline IDs */ 922 pipeline_list->pipelines = kcalloc(ipc4_data->max_num_pipelines, 923 sizeof(*pipeline_list->pipelines), 924 GFP_KERNEL); 925 if (!pipeline_list->pipelines) { 926 sof_ipc4_pcm_free(sdev, spcm); 927 return -ENOMEM; 928 } 929 930 stream_priv = kzalloc(sizeof(*stream_priv), GFP_KERNEL); 931 if (!stream_priv) { 932 sof_ipc4_pcm_free(sdev, spcm); 933 return -ENOMEM; 934 } 935 936 spcm->stream[stream].private = stream_priv; 937 938 /* Delay reporting is only supported on playback */ 939 if (!support_info || stream == SNDRV_PCM_STREAM_CAPTURE) 940 continue; 941 942 time_info = kzalloc(sizeof(*time_info), GFP_KERNEL); 943 if (!time_info) { 944 sof_ipc4_pcm_free(sdev, spcm); 945 return -ENOMEM; 946 } 947 948 stream_priv->time_info = time_info; 949 } 950 951 return 0; 952 } 953 954 static void sof_ipc4_build_time_info(struct snd_sof_dev *sdev, struct snd_sof_pcm_stream *sps) 955 { 956 struct sof_ipc4_copier *host_copier = NULL; 957 struct sof_ipc4_copier *dai_copier = NULL; 958 struct sof_ipc4_llp_reading_slot llp_slot; 959 struct sof_ipc4_timestamp_info *time_info; 960 struct snd_soc_dapm_widget *widget; 961 struct snd_sof_dai *dai; 962 int i; 963 964 /* find host & dai to locate info in memory window */ 965 for_each_dapm_widgets(sps->list, i, widget) { 966 struct snd_sof_widget *swidget = widget->dobj.private; 967 968 if (!swidget) 969 continue; 970 971 if (WIDGET_IS_AIF(swidget->widget->id)) { 972 host_copier = swidget->private; 973 } else if (WIDGET_IS_DAI(swidget->widget->id)) { 974 dai = swidget->private; 975 dai_copier = dai->private; 976 } 977 } 978 979 /* both host and dai copier must be valid for time_info */ 980 if (!host_copier || !dai_copier) { 981 dev_err(sdev->dev, "host or dai copier are not found\n"); 982 return; 983 } 984 985 time_info = sof_ipc4_sps_to_time_info(sps); 986 time_info->host_copier = host_copier; 987 time_info->dai_copier = dai_copier; 988 time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers, 989 llp_gpdma_reading_slots) + sdev->fw_info_box.offset; 990 991 /* find llp slot used by current dai */ 992 for (i = 0; i < SOF_IPC4_MAX_LLP_GPDMA_READING_SLOTS; i++) { 993 sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot)); 994 if (llp_slot.node_id == dai_copier->data.gtw_cfg.node_id) 995 break; 996 997 time_info->llp_offset += sizeof(llp_slot); 998 } 999 1000 if (i < SOF_IPC4_MAX_LLP_GPDMA_READING_SLOTS) 1001 return; 1002 1003 /* if no llp gpdma slot is used, check aggregated sdw slot */ 1004 time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers, 1005 llp_sndw_reading_slots) + sdev->fw_info_box.offset; 1006 for (i = 0; i < SOF_IPC4_MAX_LLP_SNDW_READING_SLOTS; i++) { 1007 sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot)); 1008 if (llp_slot.node_id == dai_copier->data.gtw_cfg.node_id) 1009 break; 1010 1011 time_info->llp_offset += sizeof(llp_slot); 1012 } 1013 1014 if (i < SOF_IPC4_MAX_LLP_SNDW_READING_SLOTS) 1015 return; 1016 1017 /* check EVAD slot */ 1018 time_info->llp_offset = offsetof(struct sof_ipc4_fw_registers, 1019 llp_evad_reading_slot) + sdev->fw_info_box.offset; 1020 sof_mailbox_read(sdev, time_info->llp_offset, &llp_slot, sizeof(llp_slot)); 1021 if (llp_slot.node_id != dai_copier->data.gtw_cfg.node_id) 1022 time_info->llp_offset = 0; 1023 } 1024 1025 static int sof_ipc4_pcm_hw_params(struct snd_soc_component *component, 1026 struct snd_pcm_substream *substream, 1027 struct snd_pcm_hw_params *params, 1028 struct snd_sof_platform_stream_params *platform_params) 1029 { 1030 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 1031 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1032 struct sof_ipc4_timestamp_info *time_info; 1033 struct snd_sof_pcm *spcm; 1034 1035 spcm = snd_sof_find_spcm_dai(component, rtd); 1036 if (!spcm) 1037 return -EINVAL; 1038 1039 time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]); 1040 /* delay calculation is not supported by current fw_reg ABI */ 1041 if (!time_info) 1042 return 0; 1043 1044 time_info->stream_start_offset = SOF_IPC4_INVALID_STREAM_POSITION; 1045 time_info->llp_offset = 0; 1046 1047 sof_ipc4_build_time_info(sdev, &spcm->stream[substream->stream]); 1048 1049 return 0; 1050 } 1051 1052 static int sof_ipc4_get_stream_start_offset(struct snd_sof_dev *sdev, 1053 struct snd_pcm_substream *substream, 1054 struct snd_sof_pcm_stream *sps, 1055 struct sof_ipc4_timestamp_info *time_info) 1056 { 1057 struct sof_ipc4_copier *host_copier = time_info->host_copier; 1058 struct sof_ipc4_copier *dai_copier = time_info->dai_copier; 1059 struct sof_ipc4_pipeline_registers ppl_reg; 1060 u32 dai_sample_size; 1061 u32 ch, node_index; 1062 u32 offset; 1063 1064 if (!host_copier || !dai_copier) 1065 return -EINVAL; 1066 1067 if (host_copier->data.gtw_cfg.node_id == SOF_IPC4_INVALID_NODE_ID) { 1068 return -EINVAL; 1069 } else if (host_copier->data.gtw_cfg.node_id == SOF_IPC4_CHAIN_DMA_NODE_ID) { 1070 /* 1071 * While the firmware does not supports time_info reporting for 1072 * streams using ChainDMA, it is granted that ChainDMA can only 1073 * be used on Host+Link pairs where the link position is 1074 * accessible from the host side. 1075 * 1076 * Enable delay calculation in case of ChainDMA via host 1077 * accessible registers. 1078 * 1079 * The ChainDMA uses 2x 1ms ping-pong buffer, dai side starts 1080 * when 1ms data is available 1081 */ 1082 time_info->stream_start_offset = substream->runtime->rate / MSEC_PER_SEC; 1083 goto out; 1084 } 1085 1086 node_index = SOF_IPC4_NODE_INDEX(host_copier->data.gtw_cfg.node_id); 1087 offset = offsetof(struct sof_ipc4_fw_registers, pipeline_regs) + node_index * sizeof(ppl_reg); 1088 sof_mailbox_read(sdev, sdev->fw_info_box.offset + offset, &ppl_reg, sizeof(ppl_reg)); 1089 if (ppl_reg.stream_start_offset == SOF_IPC4_INVALID_STREAM_POSITION) 1090 return -EINVAL; 1091 1092 ch = dai_copier->data.out_format.fmt_cfg; 1093 ch = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(ch); 1094 dai_sample_size = (dai_copier->data.out_format.bit_depth >> 3) * ch; 1095 1096 /* convert offsets to frame count */ 1097 time_info->stream_start_offset = ppl_reg.stream_start_offset; 1098 do_div(time_info->stream_start_offset, dai_sample_size); 1099 time_info->stream_end_offset = ppl_reg.stream_end_offset; 1100 do_div(time_info->stream_end_offset, dai_sample_size); 1101 1102 out: 1103 /* 1104 * Calculate the wrap boundary need to be used for delay calculation 1105 * The host counter is in bytes, it will wrap earlier than the frames 1106 * based link counter. 1107 */ 1108 time_info->boundary = div64_u64(~((u64)0), 1109 frames_to_bytes(substream->runtime, 1)); 1110 /* Initialize the delay value to 0 (no delay) */ 1111 time_info->delay = 0; 1112 1113 return 0; 1114 } 1115 1116 static int sof_ipc4_pcm_pointer(struct snd_soc_component *component, 1117 struct snd_pcm_substream *substream, 1118 snd_pcm_uframes_t *pointer) 1119 { 1120 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 1121 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1122 struct sof_ipc4_timestamp_info *time_info; 1123 struct sof_ipc4_llp_reading_slot llp; 1124 snd_pcm_uframes_t head_cnt, tail_cnt; 1125 struct snd_sof_pcm_stream *sps; 1126 u64 dai_cnt, host_cnt, host_ptr; 1127 struct snd_sof_pcm *spcm; 1128 int ret; 1129 1130 spcm = snd_sof_find_spcm_dai(component, rtd); 1131 if (!spcm) 1132 return -EOPNOTSUPP; 1133 1134 sps = &spcm->stream[substream->stream]; 1135 time_info = sof_ipc4_sps_to_time_info(sps); 1136 if (!time_info) 1137 return -EOPNOTSUPP; 1138 1139 /* 1140 * stream_start_offset is updated to memory window by FW based on 1141 * pipeline statistics and it may be invalid if host query happens before 1142 * the statistics is complete. And it will not change after the first initiailization. 1143 */ 1144 if (time_info->stream_start_offset == SOF_IPC4_INVALID_STREAM_POSITION) { 1145 ret = sof_ipc4_get_stream_start_offset(sdev, substream, sps, time_info); 1146 if (ret < 0) 1147 return -EOPNOTSUPP; 1148 } 1149 1150 /* For delay calculation we need the host counter */ 1151 host_cnt = snd_sof_pcm_get_host_byte_counter(sdev, component, substream); 1152 host_ptr = host_cnt; 1153 1154 /* convert the host_cnt to frames */ 1155 host_cnt = div64_u64(host_cnt, frames_to_bytes(substream->runtime, 1)); 1156 1157 /* 1158 * If the LLP counter is not reported by firmware in the SRAM window 1159 * then read the dai (link) counter via host accessible means if 1160 * available. 1161 */ 1162 if (!time_info->llp_offset) { 1163 dai_cnt = snd_sof_pcm_get_dai_frame_counter(sdev, component, substream); 1164 if (!dai_cnt) 1165 return -EOPNOTSUPP; 1166 } else { 1167 sof_mailbox_read(sdev, time_info->llp_offset, &llp, sizeof(llp)); 1168 dai_cnt = ((u64)llp.reading.llp_u << 32) | llp.reading.llp_l; 1169 } 1170 dai_cnt += time_info->stream_end_offset; 1171 1172 /* In two cases dai dma counter is not accurate 1173 * (1) dai pipeline is started before host pipeline 1174 * (2) multiple streams mixed into one. Each stream has the same dai dma 1175 * counter 1176 * 1177 * Firmware calculates correct stream_start_offset for all cases 1178 * including above two. 1179 * Driver subtracts stream_start_offset from dai dma counter to get 1180 * accurate one 1181 */ 1182 1183 /* 1184 * On stream start the dai counter might not yet have reached the 1185 * stream_start_offset value which means that no frames have left the 1186 * DSP yet from the audio stream (on playback, capture streams have 1187 * offset of 0 as we start capturing right away). 1188 * In this case we need to adjust the distance between the counters by 1189 * increasing the host counter by (offset - dai_counter). 1190 * Otherwise the dai_counter needs to be adjusted to reflect the number 1191 * of valid frames passed on the DAI side. 1192 * 1193 * The delay is the difference between the counters on the two 1194 * sides of the DSP. 1195 */ 1196 if (dai_cnt < time_info->stream_start_offset) { 1197 host_cnt += time_info->stream_start_offset - dai_cnt; 1198 dai_cnt = 0; 1199 } else { 1200 dai_cnt -= time_info->stream_start_offset; 1201 } 1202 1203 /* Wrap the dai counter at the boundary where the host counter wraps */ 1204 div64_u64_rem(dai_cnt, time_info->boundary, &dai_cnt); 1205 1206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1207 head_cnt = host_cnt; 1208 tail_cnt = dai_cnt; 1209 } else { 1210 head_cnt = dai_cnt; 1211 tail_cnt = host_cnt; 1212 } 1213 1214 if (head_cnt < tail_cnt) { 1215 time_info->delay = time_info->boundary - tail_cnt + head_cnt; 1216 goto out; 1217 } 1218 1219 time_info->delay = head_cnt - tail_cnt; 1220 1221 out: 1222 /* 1223 * Convert the host byte counter to PCM pointer which wraps in buffer 1224 * and it is in frames 1225 */ 1226 div64_u64_rem(host_ptr, snd_pcm_lib_buffer_bytes(substream), &host_ptr); 1227 *pointer = bytes_to_frames(substream->runtime, host_ptr); 1228 1229 return 0; 1230 } 1231 1232 static snd_pcm_sframes_t sof_ipc4_pcm_delay(struct snd_soc_component *component, 1233 struct snd_pcm_substream *substream) 1234 { 1235 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1236 struct sof_ipc4_timestamp_info *time_info; 1237 struct snd_sof_pcm *spcm; 1238 1239 spcm = snd_sof_find_spcm_dai(component, rtd); 1240 if (!spcm) 1241 return 0; 1242 1243 time_info = sof_ipc4_sps_to_time_info(&spcm->stream[substream->stream]); 1244 /* 1245 * Report the stored delay value calculated in the pointer callback. 1246 * In the unlikely event that the calculation was skipped/aborted, the 1247 * default 0 delay returned. 1248 */ 1249 if (time_info) 1250 return time_info->delay; 1251 1252 /* No delay information available, report 0 as delay */ 1253 return 0; 1254 1255 } 1256 1257 const struct sof_ipc_pcm_ops ipc4_pcm_ops = { 1258 .hw_params = sof_ipc4_pcm_hw_params, 1259 .trigger = sof_ipc4_pcm_trigger, 1260 .hw_free = sof_ipc4_pcm_hw_free, 1261 .dai_link_fixup = sof_ipc4_pcm_dai_link_fixup, 1262 .pcm_setup = sof_ipc4_pcm_setup, 1263 .pcm_free = sof_ipc4_pcm_free, 1264 .pointer = sof_ipc4_pcm_pointer, 1265 .delay = sof_ipc4_pcm_delay, 1266 .ipc_first_on_start = true, 1267 .platform_stop_during_hw_free = true, 1268 }; 1269