forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdai-zephyr.c
More file actions
2031 lines (1683 loc) · 54.7 KB
/
dai-zephyr.c
File metadata and controls
2031 lines (1683 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
// Keyon Jie <yang.jie@linux.intel.com>
#include <sof/audio/buffer.h>
#include <sof/audio/component_ext.h>
#include <sof/audio/format.h>
#include <sof/audio/pipeline.h>
#include <module/module/base.h>
#include <sof/common.h>
#include <rtos/panic.h>
#include <sof/ipc/msg.h>
#include <rtos/interrupt.h>
#include <rtos/timer.h>
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <rtos/init.h>
#include <sof/lib/dai.h>
#include <sof/lib/memory.h> /* for SHARED_DATA */
#include <sof/lib/notifier.h>
#include <sof/lib/uuid.h>
#include <sof/lib/dma.h>
#include <sof/list.h>
#include <rtos/spinlock.h>
#include <rtos/string.h>
#include <sof/ut.h>
#include <sof/trace/trace.h>
#include <ipc/dai.h>
#include <ipc/stream.h>
#include <ipc/topology.h>
#include <user/trace.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#if CONFIG_XRUN_NOTIFICATIONS_ENABLE
#include <ipc4/notification.h>
#endif
#include "copier/copier.h"
#include "copier/dai_copier.h"
#include "copier/copier_gain.h"
#include <zephyr/device.h>
#include <zephyr/drivers/dai.h>
#include <sof/debug/telemetry/performance_monitor.h>
/* note: if this macro is not defined
* then that means the HOST and the DSP
* have the same view of the address space.
*/
#ifndef local_to_host
#define local_to_host(addr) (addr)
#endif /* local_to_host */
static const struct comp_driver comp_dai;
LOG_MODULE_REGISTER(dai_comp, CONFIG_SOF_LOG_LEVEL);
SOF_DEFINE_REG_UUID(dai);
#if CONFIG_COMP_DAI_GROUP
static int dai_comp_trigger_internal(struct dai_data *dd, struct comp_dev *dev, int cmd);
static void dai_atomic_trigger(void *arg, enum notify_id type, void *data)
{
struct comp_dev *dev = arg;
struct dai_data *dd = comp_get_drvdata(dev);
struct dai_group *group = dd->group;
/* Atomic context set by the last DAI to receive trigger command */
group->trigger_ret = dai_comp_trigger_internal(dd, dev, group->trigger_cmd);
}
/* Assign DAI to a group */
__cold int dai_assign_group(struct dai_data *dd, struct comp_dev *dev, uint32_t group_id)
{
assert_can_be_cold();
if (dd->group) {
if (dd->group->group_id != group_id) {
comp_err(dev, "DAI already in group %d, requested %d",
dd->group->group_id, group_id);
return -EINVAL;
}
/* No need to re-assign to the same group, do nothing */
return 0;
}
dd->group = dai_group_get(group_id, DAI_CREAT);
if (!dd->group) {
comp_err(dev, "failed to assign group %d",
group_id);
return -EINVAL;
}
comp_dbg(dev, "group %d num %d",
group_id, dd->group->num_dais);
/* Register for the atomic trigger event */
notifier_register(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER,
dai_atomic_trigger, 0);
return 0;
}
#endif
static int dai_trigger_op(struct dai *dai, int cmd, int direction)
{
const struct device *dev = dai->dev;
enum dai_trigger_cmd zephyr_cmd;
switch (cmd) {
case COMP_TRIGGER_STOP:
zephyr_cmd = DAI_TRIGGER_STOP;
break;
case COMP_TRIGGER_START:
case COMP_TRIGGER_RELEASE:
zephyr_cmd = DAI_TRIGGER_START;
break;
case COMP_TRIGGER_PAUSE:
zephyr_cmd = DAI_TRIGGER_PAUSE;
break;
case COMP_TRIGGER_PRE_START:
case COMP_TRIGGER_PRE_RELEASE:
zephyr_cmd = DAI_TRIGGER_PRE_START;
break;
default:
return -EINVAL;
}
return dai_trigger(dev, direction, zephyr_cmd);
}
/* called from src/ipc/ipc3/handler.c and src/ipc/ipc4/dai.c */
__cold int dai_set_config(struct dai *dai, struct ipc_config_dai *common_config,
const void *spec_config, size_t size)
{
const struct device *dev = dai->dev;
const struct sof_ipc_dai_config *sof_cfg = spec_config;
struct dai_config cfg = {0};
const void *cfg_params;
bool is_blob;
assert_can_be_cold();
cfg.dai_index = common_config->dai_index;
is_blob = common_config->is_config_blob;
cfg.format = sof_cfg->format;
cfg.options = sof_cfg->flags;
cfg.rate = common_config->sampling_frequency;
switch (common_config->type) {
case SOF_DAI_INTEL_SSP:
cfg.type = is_blob ? DAI_INTEL_SSP_NHLT : DAI_INTEL_SSP;
cfg_params = is_blob ? spec_config : &sof_cfg->ssp;
dai_set_link_hda_config(&cfg.link_config,
common_config, cfg_params);
/* Store tdm slot group index*/
cfg.tdm_slot_group = common_config->dai_index & 0xF;
break;
case SOF_DAI_INTEL_ALH:
cfg.type = is_blob ? DAI_INTEL_ALH_NHLT : DAI_INTEL_ALH;
cfg_params = is_blob ? spec_config : &sof_cfg->alh;
break;
case SOF_DAI_INTEL_DMIC:
cfg.type = is_blob ? DAI_INTEL_DMIC_NHLT : DAI_INTEL_DMIC;
cfg_params = is_blob ? spec_config : &sof_cfg->dmic;
dai_set_link_hda_config(&cfg.link_config,
common_config, cfg_params);
break;
case SOF_DAI_INTEL_HDA:
cfg.type = is_blob ? DAI_INTEL_HDA_NHLT : DAI_INTEL_HDA;
cfg_params = is_blob ? spec_config : &sof_cfg->hda;
break;
case SOF_DAI_IMX_SAI:
cfg.type = DAI_IMX_SAI;
cfg_params = &sof_cfg->sai;
break;
case SOF_DAI_IMX_ESAI:
cfg.type = DAI_IMX_ESAI;
cfg_params = &sof_cfg->esai;
break;
case SOF_DAI_IMX_MICFIL:
cfg.type = DAI_IMX_MICFIL;
cfg_params = &sof_cfg->micfil;
break;
case SOF_DAI_INTEL_UAOL:
cfg.type = DAI_INTEL_UAOL;
cfg.channels = common_config->gtw_fmt->channels_count;
/*
* FIXME: The spec says HW expects container size here, not valid_bit_depth.
* However, tests fail if container size is used and work fine with
* valid_bit_depth. Needs investigation. Perhaps tests have a bug?
*/
cfg.word_size = common_config->gtw_fmt->valid_bit_depth;
cfg_params = spec_config;
dai_set_link_hda_config(&cfg.link_config, common_config, spec_config);
break;
default:
return -EINVAL;
}
return dai_config_set(dev, &cfg, cfg_params, size);
}
/* called from ipc/ipc3/dai.c */
int dai_get_handshake(struct dai *dai, int direction, int stream_id)
{
k_spinlock_key_t key = k_spin_lock(&dai->lock);
const struct dai_properties *props = dai_get_properties(dai->dev, direction,
stream_id);
int hs_id = props->dma_hs_id;
k_spin_unlock(&dai->lock, key);
return hs_id;
}
/* called from ipc/ipc3/dai.c and ipc/ipc4/dai.c */
int dai_get_fifo_depth(struct dai *dai, int direction)
{
const struct dai_properties *props;
k_spinlock_key_t key;
int fifo_depth;
if (!dai)
return 0;
key = k_spin_lock(&dai->lock);
props = dai_get_properties(dai->dev, direction, 0);
fifo_depth = props->fifo_depth;
k_spin_unlock(&dai->lock, key);
return fifo_depth;
}
int dai_get_stream_id(struct dai *dai, int direction)
{
k_spinlock_key_t key = k_spin_lock(&dai->lock);
const struct dai_properties *props = dai_get_properties(dai->dev, direction, 0);
int stream_id = props->stream_id;
k_spin_unlock(&dai->lock, key);
return stream_id;
}
static int dai_get_fifo(struct dai *dai, int direction, int stream_id)
{
k_spinlock_key_t key = k_spin_lock(&dai->lock);
const struct dai_properties *props = dai_get_properties(dai->dev, direction,
stream_id);
int fifo_address = props->fifo_address;
k_spin_unlock(&dai->lock, key);
return fifo_address;
}
/* this is called by DMA driver every time descriptor has completed */
static enum sof_dma_cb_status
dai_dma_cb(struct dai_data *dd, struct comp_dev *dev, uint32_t bytes,
pcm_converter_func *converter)
{
enum sof_dma_cb_status dma_status = SOF_DMA_CB_STATUS_RELOAD;
int ret;
comp_dbg(dev, "entry");
/* stop dma copy for pause/stop/xrun */
if (dev->state != COMP_STATE_ACTIVE || dd->xrun) {
/* stop the DAI */
dai_trigger_op(dd->dai, COMP_TRIGGER_STOP, dev->direction);
/* tell DMA not to reload */
dma_status = SOF_DMA_CB_STATUS_END;
}
/* is our pipeline handling an XRUN ? */
if (dd->xrun) {
/* make sure we only playback silence during an XRUN */
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
/* fill buffer with silence */
buffer_zero(dd->dma_buffer);
return dma_status;
}
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
#if CONFIG_IPC_MAJOR_4
/*
* copy from local buffer to all sinks that are not gateway buffers
* using the right PCM converter function.
*/
struct comp_buffer *sink;
comp_dev_for_each_consumer(dev, sink) {
struct comp_dev *sink_dev;
int j;
if (sink == dd->dma_buffer)
continue;
sink_dev = comp_buffer_get_sink_component(sink);
j = IPC4_SRC_QUEUE_ID(buf_get_id(sink));
if (j >= IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT) {
comp_err(dev, "Sink queue ID: %d >= max output pin count: %d\n",
j, IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT);
ret = -EINVAL;
continue;
}
if (!converter[j]) {
comp_err(dev, "No PCM converter for sink queue %d\n", j);
ret = -EINVAL;
continue;
}
if (sink_dev && sink_dev->state == COMP_STATE_ACTIVE &&
audio_buffer_hw_params_configured(&sink->audio_buffer)) {
ret = stream_copy_from_no_consume(dev, dd->local_buffer, sink,
converter[j], bytes, dd->chmap);
}
}
#endif
ret = dma_buffer_copy_to(dd->local_buffer, dd->dma_buffer,
dd->process, bytes, dd->chmap);
} else {
audio_stream_invalidate(&dd->dma_buffer->stream, bytes);
/*
* The PCM converter functions used during DMA buffer copy can never fail,
* so no need to check the return value of stream_copy_from_no_consume().
*/
ret = stream_copy_from_no_consume(dev, dd->dma_buffer, dd->local_buffer,
dd->process, bytes, dd->chmap);
#if CONFIG_IPC_MAJOR_4
/* Apply gain to the local buffer */
if (dd->ipc_config.apply_gain) {
ret = copier_gain_input(dev, dd->local_buffer, dd->gain_data,
GAIN_ADD, bytes);
if (ret)
comp_err(dev, "copier_gain_input() failed err=%d", ret);
buffer_stream_writeback(dd->local_buffer, bytes);
}
/* Skip in case of endpoint DAI devices created by the copier */
if (converter) {
/*
* copy from DMA buffer to all sink buffers using the right PCM converter
* function
*/
struct comp_buffer *sink;
comp_dev_for_each_consumer(dev, sink) {
struct comp_dev *sink_dev;
int j;
/* this has been handled above already */
if (sink == dd->local_buffer)
continue;
sink_dev = comp_buffer_get_sink_component(sink);
j = IPC4_SRC_QUEUE_ID(buf_get_id(sink));
if (j >= IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT) {
comp_err(dev, "Sink queue ID: %d >= max output pin count: %d\n",
j, IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT);
ret = -EINVAL;
continue;
}
if (!converter[j]) {
comp_err(dev, "No PCM converter for sink queue %d\n", j);
ret = -EINVAL;
continue;
}
if (sink_dev && sink_dev->state == COMP_STATE_ACTIVE &&
audio_buffer_hw_params_configured(&sink->audio_buffer))
ret = stream_copy_from_no_consume(dev, dd->dma_buffer,
sink, converter[j],
bytes, dd->chmap);
}
}
#endif
audio_stream_consume(&dd->dma_buffer->stream, bytes);
}
/* assert dma_buffer_copy succeed */
if (ret < 0) {
struct comp_buffer *source_c, *sink_c;
source_c = dev->direction == SOF_IPC_STREAM_PLAYBACK ?
dd->local_buffer : dd->dma_buffer;
sink_c = dev->direction == SOF_IPC_STREAM_PLAYBACK ?
dd->dma_buffer : dd->local_buffer;
comp_err(dev, "dma buffer copy failed, dir %d bytes %d avail %d free %d",
dev->direction, bytes,
audio_stream_get_avail_samples(&source_c->stream) *
audio_stream_frame_bytes(&source_c->stream),
audio_stream_get_free_samples(&sink_c->stream) *
audio_stream_frame_bytes(&sink_c->stream));
} else {
/* update host position (in bytes offset) for drivers */
dd->total_data_processed += bytes;
}
#ifdef CONFIG_SOF_TELEMETRY_IO_PERFORMANCE_MEASUREMENTS
/* Increment performance counters */
io_perf_monitor_update_data(dd->io_perf_bytes_count, bytes);
#endif
return dma_status;
}
/* this is called by DMA driver every time descriptor has completed */
static enum sof_dma_cb_status
dai_dma_multi_endpoint_cb(struct dai_data *dd, struct comp_dev *dev, uint32_t frames,
struct comp_buffer *multi_endpoint_buffer)
{
enum sof_dma_cb_status dma_status = SOF_DMA_CB_STATUS_RELOAD;
uint32_t i, bytes;
comp_dbg(dev, "entry");
/* stop dma copy for pause/stop/xrun */
if (dev->state != COMP_STATE_ACTIVE || dd->xrun) {
/* stop the DAI */
dai_trigger_op(dd->dai, COMP_TRIGGER_STOP, dev->direction);
/* tell DMA not to reload */
dma_status = SOF_DMA_CB_STATUS_END;
}
/* is our pipeline handling an XRUN ? */
if (dd->xrun) {
/* make sure we only playback silence during an XRUN */
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
/* fill buffer with silence */
buffer_zero(dd->dma_buffer);
return dma_status;
}
bytes = frames * audio_stream_frame_bytes(&dd->dma_buffer->stream);
if (dev->direction == SOF_IPC_STREAM_CAPTURE)
audio_stream_invalidate(&dd->dma_buffer->stream, bytes);
assert(dd->channel_copy);
/* copy all channels one by one */
for (i = 0; i < audio_stream_get_channels(&dd->dma_buffer->stream); i++) {
uint32_t multi_buf_channel = audio_buffer_get_chmap(&dd->dma_buffer->audio_buffer,
i);
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
dd->channel_copy(&multi_endpoint_buffer->stream, multi_buf_channel,
&dd->dma_buffer->stream, i, frames);
else
dd->channel_copy(&dd->dma_buffer->stream, i,
&multi_endpoint_buffer->stream, multi_buf_channel,
frames);
}
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
audio_stream_writeback(&dd->dma_buffer->stream, bytes);
audio_stream_produce(&dd->dma_buffer->stream, bytes);
} else {
audio_stream_consume(&dd->dma_buffer->stream, bytes);
}
/* update host position (in bytes offset) for drivers */
dd->total_data_processed += bytes;
return dma_status;
}
__cold int dai_common_new(struct dai_data *dd, struct comp_dev *dev,
const struct ipc_config_dai *dai_cfg)
{
uint32_t dir;
assert_can_be_cold();
dd->dai = dai_get(dai_cfg->type, dai_cfg->dai_index, DAI_CREAT);
if (!dd->dai) {
comp_err(dev, "dai_get() failed to create DAI.");
return -ENODEV;
}
dd->ipc_config = *dai_cfg;
/* request GP LP DMA with shared access privilege */
dir = dai_cfg->direction == SOF_IPC_STREAM_PLAYBACK ?
SOF_DMA_DIR_MEM_TO_DEV : SOF_DMA_DIR_DEV_TO_MEM;
dd->dma = sof_dma_get(dir, dd->dai->dma_caps, dd->dai->dma_dev, SOF_DMA_ACCESS_SHARED);
if (!dd->dma) {
dai_put(dd->dai);
comp_err(dev, "dma_get() failed to get shared access to DMA.");
return -ENODEV;
}
k_spinlock_init(&dd->dai->lock);
dma_sg_init(&dd->config.elem_array);
dd->xrun = 0;
dd->chan = NULL;
/* I/O performance init, keep it last so the function does not reach this in case
* of return on error, so that we do not waste a slot
*/
#ifdef CONFIG_SOF_TELEMETRY_IO_PERFORMANCE_MEASUREMENTS
enum io_perf_data_item_id perf_type;
enum io_perf_data_item_dir perf_dir;
switch (dai_cfg->type) {
case SOF_DAI_INTEL_SSP:
perf_type = IO_PERF_I2S_ID;
break;
case SOF_DAI_INTEL_ALH:
perf_type = IO_PERF_SOUND_WIRE_ID;
break;
case SOF_DAI_INTEL_DMIC:
perf_type = IO_PERF_DMIC_ID;
break;
case SOF_DAI_INTEL_HDA:
perf_type = IO_PERF_HDA_ID;
break;
default:
perf_type = IO_PERF_INVALID_ID;
comp_warn(dev, "Unsupported DAI type");
}
if (dai_cfg->direction == SOF_IPC_STREAM_PLAYBACK)
perf_dir = IO_PERF_OUTPUT_DIRECTION;
else
perf_dir = IO_PERF_INPUT_DIRECTION;
/* ignore perf meas init on case of other dai types */
if (perf_type != IO_PERF_INVALID_ID) {
struct io_perf_data_item init_data = {perf_type,
cpu_get_id(),
perf_dir,
IO_PERF_POWERED_UP_ENABLED,
IO_PERF_D0IX_POWER_MODE,
0, 0, 0 };
io_perf_monitor_init_data(&dd->io_perf_bytes_count, &init_data);
}
#endif
return 0;
}
__cold static struct comp_dev *dai_new(const struct comp_driver *drv,
const struct comp_ipc_config *config,
const void *spec)
{
struct comp_dev *dev;
const struct ipc_config_dai *dai_cfg = spec;
struct dai_data *dd;
int ret;
assert_can_be_cold();
comp_cl_dbg(&comp_dai, "dai_new()");
dev = comp_alloc(drv, sizeof(*dev));
if (!dev)
return NULL;
dev->ipc_config = *config;
dd = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, sizeof(*dd));
if (!dd)
goto e_data;
comp_set_drvdata(dev, dd);
ret = dai_common_new(dd, dev, dai_cfg);
if (ret < 0)
goto error;
dd->chmap = DUMMY_CHMAP;
dev->state = COMP_STATE_READY;
return dev;
error:
rfree(dd);
e_data:
comp_free_device(dev);
return NULL;
}
__cold void dai_common_free(struct dai_data *dd)
{
assert_can_be_cold();
#ifdef CONFIG_SOF_TELEMETRY_IO_PERFORMANCE_MEASUREMENTS
io_perf_monitor_release_slot(dd->io_perf_bytes_count);
#endif
if (dd->group)
dai_group_put(dd->group);
if (dd->chan) {
sof_dma_release_channel(dd->dma, dd->chan->index);
dd->chan->dev_data = NULL;
}
sof_dma_put(dd->dma);
dai_release_llp_slot(dd);
dai_put(dd->dai);
rfree(dd->dai_spec_config);
}
__cold static void dai_free(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
assert_can_be_cold();
if (dd->group)
notifier_unregister(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER);
dai_common_free(dd);
rfree(dd);
comp_free_device(dev);
}
int dai_common_get_hw_params(struct dai_data *dd, struct comp_dev *dev,
struct sof_ipc_stream_params *params, int dir)
{
struct dai_config cfg;
int ret;
comp_dbg(dev, "entry");
ret = dai_config_get(dd->dai->dev, &cfg, dir);
if (ret)
return ret;
params->rate = cfg.rate;
params->buffer_fmt = 0;
params->channels = cfg.channels;
/* dai_comp_get_hw_params() function fetches hardware dai parameters,
* which then are propagating back through the pipeline, so that any
* component can convert specific stream parameter. Here, we overwrite
* frame_fmt hardware parameter as DAI component is able to convert
* stream with different frame_fmt's (using pcm converter)
*/
params->frame_fmt = dev->ipc_config.frame_fmt;
return ret;
}
__cold static int dai_comp_get_hw_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params,
int dir)
{
struct dai_data *dd = comp_get_drvdata(dev);
assert_can_be_cold();
return dai_common_get_hw_params(dd, dev, params, dir);
}
static int dai_verify_params(struct dai_data *dd, struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct sof_ipc_stream_params hw_params;
int ret;
memset(&hw_params, 0, sizeof(hw_params));
ret = dai_common_get_hw_params(dd, dev, &hw_params, params->direction);
if (ret < 0) {
comp_err(dev, "failed ret %d", ret);
return ret;
}
/* checks whether pcm parameters match hardware DAI parameter set
* during dai_set_config(). If hardware parameter is equal to 0, it
* means that it can vary, so any value is acceptable. We do not check
* format parameter, because DAI is able to change format using
* pcm_converter functions.
*/
if (hw_params.rate && hw_params.rate != params->rate) {
comp_err(dev, "pcm rate parameter %d does not match hardware rate %d",
params->rate, hw_params.rate);
return -EINVAL;
}
if (hw_params.channels && hw_params.channels != params->channels) {
comp_err(dev, "pcm channels parameter %d does not match hardware channels %d",
params->channels, hw_params.channels);
return -EINVAL;
}
/* set component period frames */
component_set_nearest_period_frames(dev, params->rate);
return 0;
}
static int dai_get_dma_slot(struct dai_data *dd, struct comp_dev *dev, uint32_t *slot)
{
struct dai_config cfg;
int ret;
int hs;
ret = dai_config_get(dd->dai->dev, &cfg, dev->direction);
if (ret < 0) {
comp_err(dev, "failed to fetch DAI configuration");
return ret;
}
hs = dai_get_handshake(dd->dai, dev->direction, dd->stream_id);
if (ret < 0) {
comp_err(dev, "failed to fetch DAI handshake");
return ret;
}
switch (cfg.type) {
case DAI_IMX_SAI:
case DAI_IMX_ESAI:
case DAI_IMX_MICFIL:
*slot = (hs & GENMASK(15, 8)) >> 8;
break;
default:
*slot = hs;
break;
}
return 0;
}
static int dai_set_sg_config(struct dai_data *dd, struct comp_dev *dev, uint32_t period_bytes,
uint32_t period_count)
{
struct dma_sg_config *config = &dd->config;
uint32_t local_fmt = audio_stream_get_frm_fmt(&dd->local_buffer->stream);
uint32_t dma_fmt = audio_stream_get_frm_fmt(&dd->dma_buffer->stream);
uint32_t fifo, max_block_count, buf_size;
int err = 0;
/* set up DMA configuration */
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
dd->process = pcm_get_conversion_function(local_fmt, dma_fmt);
config->direction = SOF_DMA_DIR_MEM_TO_DEV;
err = dai_get_dma_slot(dd, dev, &config->dest_dev);
if (err < 0)
return err;
} else {
dd->process = pcm_get_conversion_function(dma_fmt, local_fmt);
config->direction = SOF_DMA_DIR_DEV_TO_MEM;
err = dai_get_dma_slot(dd, dev, &config->src_dev);
if (err < 0)
return err;
}
if (!dd->process) {
comp_err(dev, "converter NULL: local fmt %d dma fmt %d\n",
local_fmt, dma_fmt);
return -EINVAL;
}
if (dd->dai->type == SOF_DAI_INTEL_DMIC) {
/* For DMIC the DMA src and dest widths should always be 4 bytes
* due to 32 bit FIFO packer. Setting width to 2 bytes for
* 16 bit format would result in recording at double rate.
*/
config->src_width = 4;
config->dest_width = 4;
} else {
config->src_width = get_sample_bytes(dma_fmt);
config->dest_width = config->src_width;
}
config->cyclic = 1;
config->irq_disabled = pipeline_is_timer_driven(dev->pipeline);
config->is_scheduling_source = comp_is_scheduling_source(dev);
config->period = dev->pipeline->period;
comp_dbg(dev, "dest_dev = %d stream_id = %d src_width = %d dest_width = %d",
config->dest_dev, dd->stream_id, config->src_width, config->dest_width);
if (!config->elem_array.elems) {
fifo = dai_get_fifo(dd->dai, dev->direction, dd->stream_id);
comp_dbg(dev, "fifo 0x%x", fifo);
err = sof_dma_get_attribute(dd->dma, DMA_ATTR_MAX_BLOCK_COUNT, &max_block_count);
if (err < 0) {
comp_err(dev, "can't get max block count, err = %d",
err);
goto out;
}
if (!max_block_count) {
comp_err(dev, "invalid max-block-count of zero");
goto out;
}
if (max_block_count < period_count) {
comp_dbg(dev, "unsupported period count %d",
period_count);
buf_size = period_count * period_bytes;
do {
if (IS_ALIGNED(buf_size, max_block_count)) {
period_count = max_block_count;
period_bytes = buf_size / period_count;
break;
} else {
comp_warn(dev, "alignment error for buf_size = %d, block count = %d",
buf_size, max_block_count);
}
} while (--max_block_count > 0);
}
err = dma_sg_alloc(&config->elem_array, SOF_MEM_FLAG_USER,
config->direction,
period_count,
period_bytes,
(uintptr_t)audio_stream_get_addr(&dd->dma_buffer->stream),
fifo);
if (err < 0) {
comp_err(dev, "sg alloc failed period_count %d period_bytes %d err = %d",
period_count, period_bytes, err);
return err;
}
}
out:
return err;
}
static int dai_set_dma_config(struct dai_data *dd, struct comp_dev *dev)
{
struct dma_sg_config *config = &dd->config;
struct dma_config *dma_cfg;
struct dma_block_config *dma_block_cfg;
struct dma_block_config *prev = NULL;
int i;
comp_dbg(dev, "entry");
dma_cfg = rballoc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
sizeof(struct dma_config));
if (!dma_cfg) {
comp_err(dev, "dma_cfg allocation failed");
return -ENOMEM;
}
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
dma_cfg->channel_direction = MEMORY_TO_PERIPHERAL;
else
dma_cfg->channel_direction = PERIPHERAL_TO_MEMORY;
dma_cfg->source_data_size = config->src_width;
dma_cfg->dest_data_size = config->dest_width;
if (config->burst_elems)
dma_cfg->source_burst_length = config->burst_elems;
else
dma_cfg->source_burst_length = 8;
dma_cfg->dest_burst_length = dma_cfg->source_burst_length;
dma_cfg->cyclic = config->cyclic;
dma_cfg->user_data = NULL;
dma_cfg->dma_callback = NULL;
dma_cfg->block_count = config->elem_array.count;
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
dma_cfg->dma_slot = config->dest_dev;
else
dma_cfg->dma_slot = config->src_dev;
dma_block_cfg = rballoc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
sizeof(struct dma_block_config) * dma_cfg->block_count);
if (!dma_block_cfg) {
rfree(dma_cfg);
comp_err(dev, "dma_block_config allocation failed");
return -ENOMEM;
}
dma_cfg->head_block = dma_block_cfg;
for (i = 0; i < dma_cfg->block_count; i++) {
dma_block_cfg->dest_scatter_en = config->scatter;
dma_block_cfg->block_size = config->elem_array.elems[i].size;
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
dma_block_cfg->source_address =
local_to_host(config->elem_array.elems[i].src);
dma_block_cfg->dest_address =
config->elem_array.elems[i].dest;
dma_block_cfg->source_addr_adj = DMA_ADDR_ADJ_DECREMENT;
dma_block_cfg->dest_addr_adj = DMA_ADDR_ADJ_INCREMENT;
} else {
dma_block_cfg->source_address =
config->elem_array.elems[i].src;
dma_block_cfg->dest_address =
local_to_host(config->elem_array.elems[i].dest);
dma_block_cfg->source_addr_adj = DMA_ADDR_ADJ_INCREMENT;
dma_block_cfg->dest_addr_adj = DMA_ADDR_ADJ_DECREMENT;
}
prev = dma_block_cfg;
prev->next_block = ++dma_block_cfg;
}
if (prev)
prev->next_block = dma_cfg->head_block;
dd->z_config = dma_cfg;
return 0;
}
static int dai_set_dma_buffer(struct dai_data *dd, struct comp_dev *dev,
const struct sof_ipc_stream_params *params,
uint32_t *pb, uint32_t *pc)
{
struct sof_ipc_stream_params hw_params = *params;
uint32_t frame_size;
uint32_t period_count;
uint32_t period_bytes;
uint32_t buffer_size;
uint32_t buffer_size_preferred;
uint32_t addr_align;
uint32_t align;
int err;
comp_dbg(dev, "entry");
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
dd->local_buffer = comp_dev_get_first_data_producer(dev);
else
dd->local_buffer = comp_dev_get_first_data_consumer(dev);
/* check if already configured */
if (dev->state == COMP_STATE_PREPARE) {
comp_info(dev, "component has been already configured.");
return 0;
}
/* can set params on only init state */
if (dev->state != COMP_STATE_READY) {
comp_err(dev, "comp state %d, expected COMP_STATE_READY.",
dev->state);
return -EINVAL;
}
err = sof_dma_get_attribute(dd->dma, DMA_ATTR_BUFFER_ADDRESS_ALIGNMENT, &addr_align);
if (err < 0) {
comp_err(dev, "can't get dma buffer addr align, err = %d",
err);
return err;
}
err = sof_dma_get_attribute(dd->dma, DMA_ATTR_BUFFER_SIZE_ALIGNMENT, &align);
if (err < 0 || !align) {
comp_err(dev, "no valid dma align, err = %d, align = %u",
err, align);
return -EINVAL;
}
/* calculate frame size */
frame_size = get_frame_bytes(dev->ipc_config.frame_fmt, params->channels);
/* calculate period size */
period_bytes = dev->frames * frame_size;
if (!period_bytes) {
comp_err(dev, "invalid period_bytes.");
return -EINVAL;
}
dd->period_bytes = period_bytes;
*pb = period_bytes;
/* calculate DMA buffer size */
period_count = dd->dma->plat_data.period_count;
#if CONFIG_IPC_MAJOR_4
struct ipc4_copier_module_cfg *copier_cfg = dd->dai_spec_config;
uint32_t dma_buff_length_periods;
/* copier ibs/obs is set to size of one period */