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