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