forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopier.c
More file actions
1219 lines (1006 loc) · 33.9 KB
/
copier.c
File metadata and controls
1219 lines (1006 loc) · 33.9 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) 2021 Intel Corporation. All rights reserved.
//
// Author: Rander Wang <rander.wang@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 <sof/common.h>
#include <rtos/panic.h>
#include <rtos/interrupt.h>
#include <sof/ipc/msg.h>
#include <sof/ipc/topology.h>
#include <rtos/interrupt.h>
#include <rtos/timer.h>
#include <rtos/cache.h>
#include <rtos/init.h>
#include <sof/lib/memory.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <rtos/string.h>
#include <sof/ut.h>
#include <sof/trace/trace.h>
#include <ipc4/alh.h>
#include <ipc4/base-config.h>
#include <ipc4/module.h>
#include <ipc4/error_status.h>
#include <ipc4/gateway.h>
#include <ipc4/fw_reg.h>
#include <ipc/dai.h>
#include <user/trace.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <sof/audio/module_adapter/module/generic.h>
#include "copier.h"
#include "host_copier.h"
#include "dai_copier.h"
#include "ipcgtw_copier.h"
#if CONFIG_INTEL_ADSP_MIC_PRIVACY
#include <zephyr/drivers/mic_privacy/intel/mic_privacy.h>
#endif
#if CONFIG_ZEPHYR_NATIVE_DRIVERS
#include <zephyr/drivers/dai.h>
#endif
LOG_MODULE_REGISTER(copier, CONFIG_SOF_LOG_LEVEL);
/* this id aligns windows driver requirement to support windows driver */
SOF_DEFINE_REG_UUID(copier);
#if CONFIG_INTEL_ADSP_MIC_PRIVACY
static void mic_privacy_event(void *arg, enum notify_id type, void *data)
{
struct mic_privacy_data *mic_priv_data = arg;
struct mic_privacy_settings *mic_privacy_settings = data;
if (type == NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE) {
LOG_INF("state1 = %d, state2 = %d ",
mic_privacy_settings->mic_privacy_state, mic_priv_data->mic_privacy_state);
if (mic_privacy_settings->mic_privacy_state == MIC_PRIV_UNMUTED) {
if (mic_priv_data->mic_privacy_state == MIC_PRIV_MUTED) {
mic_priv_data->mic_privacy_state = MIC_PRIV_FADE_IN;
LOG_INF("switch to FADE_IN");
}
} else {
/* In case when mute would be triggered before copier instantiation. */
if (mic_priv_data->mic_privacy_state != MIC_PRIV_MUTED) {
mic_priv_data->mic_privacy_state = MIC_PRIV_FADE_OUT;
LOG_INF("switch to FADE_OUT");
}
}
mic_priv_data->max_ramp_time_in_ms = (mic_privacy_settings->max_ramp_time * 1000) /
ADSP_RTC_FREQUENCY;
}
}
static int mic_privacy_configure(struct processing_module *mod, struct copier_data *cd)
{
struct mic_privacy_data *mic_priv_data;
int ret;
mic_priv_data = mod_zalloc(mod, sizeof(struct mic_privacy_data));
if (!mic_priv_data)
return -ENOMEM;
if (cd->gtw_type == ipc4_gtw_dmic)
mic_privacy_enable_dmic_irq(true);
mic_priv_data->audio_freq = cd->config.base.audio_fmt.sampling_frequency;
uint32_t zeroing_wait_time = (mic_privacy_get_dma_zeroing_wait_time() * 1000) /
ADSP_RTC_FREQUENCY;
ret = copier_gain_set_params(mod->dev, &mic_priv_data->mic_priv_gain_params,
zeroing_wait_time, SOF_DAI_INTEL_NONE);
if (ret != 0) {
mod_free(mod, mic_priv_data);
return ret;
}
cd->mic_priv = mic_priv_data;
ret = notifier_register(cd->mic_priv, NULL, NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE,
mic_privacy_event, 0);
if (ret != 0)
mod_free(mod, mic_priv_data);
return ret;
}
static void mic_privacy_free(struct processing_module *mod)
{
struct copier_data *cd = module_get_private_data(mod);
if (cd->gtw_type == ipc4_gtw_dmic)
mic_privacy_enable_dmic_irq(false);
notifier_unregister(cd->mic_priv, NULL, NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE);
mod_free(mod, cd->mic_priv);
}
#endif
__cold static int copier_init(struct processing_module *mod)
{
union ipc4_connector_node_id node_id;
struct copier_data *cd;
struct comp_dev *dev = mod->dev;
struct module_data *md = &mod->priv;
struct ipc4_copier_module_cfg *copier = (struct ipc4_copier_module_cfg *)md->cfg.init_data;
size_t cfg_total_size = sizeof(*copier);
size_t gtw_cfg_var_size = 0;
int i, ret = 0;
assert_can_be_cold();
if (copier->gtw_cfg.config_length > 1) {
/* one word already included in gateway_cfg struct hence subtraction */
gtw_cfg_var_size += (copier->gtw_cfg.config_length - 1) << 2;
cfg_total_size += gtw_cfg_var_size;
}
cd = mod_zalloc(mod, sizeof(*cd) + gtw_cfg_var_size);
if (!cd)
return -ENOMEM;
md->private = cd;
if (memcpy_s(&cd->config, cfg_total_size, copier, cfg_total_size) < 0) {
ret = -EINVAL;
goto error;
}
for (i = 0; i < IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT; i++)
cd->out_fmt[i] = cd->config.out_fmt;
node_id = copier->gtw_cfg.node_id;
/* copier is linked to gateway */
if (node_id.dw != IPC4_INVALID_NODE_ID) {
cd->direction = get_gateway_direction(node_id.f.dma_type);
switch (node_id.f.dma_type) {
case ipc4_hda_host_output_class:
case ipc4_hda_host_input_class:
ret = copier_host_create(mod, copier, dev->pipeline);
if (ret < 0) {
comp_err(dev, "unable to create host");
goto error;
}
#if CONFIG_INTEL_ADSP_MIC_PRIVACY
if (cd->direction == SOF_IPC_STREAM_CAPTURE &&
node_id.f.dma_type == ipc4_hda_host_output_class) {
ret = mic_privacy_configure(mod, cd);
if (ret < 0) {
comp_err(dev, "unable to configure mic privacy");
goto error;
}
}
#endif
break;
case ipc4_hda_link_output_class:
case ipc4_hda_link_input_class:
case ipc4_dmic_link_input_class:
case ipc4_i2s_link_output_class:
case ipc4_i2s_link_input_class:
case ipc4_alh_link_output_class:
case ipc4_alh_link_input_class:
case ipc4_alh_uaol_stream_link_output_class:
case ipc4_alh_uaol_stream_link_input_class:
ret = copier_dai_create(dev, cd, copier, dev->pipeline);
if (ret < 0) {
comp_err(dev, "unable to create dai");
goto error;
}
#if CONFIG_INTEL_ADSP_MIC_PRIVACY
if (cd->direction == SOF_IPC_STREAM_CAPTURE) {
ret = mic_privacy_configure(mod, cd);
if (ret < 0) {
comp_err(dev, "unable to configure mic privacy");
goto error;
}
}
#endif
break;
#if CONFIG_IPC4_GATEWAY
case ipc4_ipc_output_class:
case ipc4_ipc_input_class:
ret = copier_ipcgtw_create(mod, copier, dev->pipeline);
if (ret < 0) {
comp_err(dev, "unable to create IPC gateway");
goto error;
}
break;
#endif
default:
comp_err(dev, "unsupported dma type %x", (uint32_t)node_id.f.dma_type);
ret = -EINVAL;
goto error;
};
dev->direction_set = true;
} else {
cd->gtw_type = ipc4_gtw_none;
/* set max sink count for module copier */
mod->max_sinks = IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT;
}
dev->direction = cd->direction;
dev->state = COMP_STATE_READY;
return 0;
error:
mod_free(mod, cd);
return ret;
}
__cold static int copier_free(struct processing_module *mod)
{
struct copier_data *cd = module_get_private_data(mod);
struct comp_dev *dev = mod->dev;
assert_can_be_cold();
#if CONFIG_INTEL_ADSP_MIC_PRIVACY
mic_privacy_free(mod);
#endif
switch (dev->ipc_config.type) {
case SOF_COMP_HOST:
if (!cd->ipc_gtw)
copier_host_free(mod);
else
/* handle gtw case */
copier_ipcgtw_free(mod);
break;
case SOF_COMP_DAI:
copier_dai_free(mod);
break;
default:
break;
}
mod_free(mod, cd);
return 0;
}
static int copier_params(struct processing_module *mod);
static int copier_prepare(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
struct copier_data *cd = module_get_private_data(mod);
struct comp_dev *dev = mod->dev;
int ret;
ret = copier_params(mod);
if (ret < 0)
return ret;
comp_info(dev, "entry");
switch (dev->ipc_config.type) {
case SOF_COMP_HOST:
if (!cd->ipc_gtw) {
ret = host_common_prepare(cd->hd);
if (ret < 0)
return ret;
}
break;
case SOF_COMP_DAI:
ret = copier_dai_prepare(dev, cd);
if (ret < 0)
return ret;
break;
default:
break;
}
if (!cd->endpoint_num) {
/* set up format conversion function for pin 0, for other pins (if any)
* format is set in IPC4_COPIER_MODULE_CFG_PARAM_SET_SINK_FORMAT handler
*/
cd->converter[0] = get_converter_func(&cd->config.base.audio_fmt,
&cd->config.out_fmt, ipc4_gtw_none,
ipc4_bidirection, DUMMY_CHMAP);
if (!cd->converter[0]) {
comp_err(dev, "can't support for in format %d, out format %d",
cd->config.base.audio_fmt.depth, cd->config.out_fmt.depth);
return -EINVAL;
}
}
return 0;
}
static int copier_reset(struct processing_module *mod)
{
struct copier_data *cd = module_get_private_data(mod);
struct ipc4_pipeline_registers pipe_reg;
struct comp_dev *dev = mod->dev;
comp_dbg(dev, "entry");
cd->input_total_data_processed = 0;
cd->output_total_data_processed = 0;
switch (dev->ipc_config.type) {
case SOF_COMP_HOST:
if (!cd->ipc_gtw)
host_common_reset(cd->hd, dev->state);
else
copier_ipcgtw_reset(dev);
break;
case SOF_COMP_DAI:
copier_dai_reset(cd, dev);
break;
default:
break;
}
if (cd->pipeline_reg_offset) {
pipe_reg.stream_start_offset = (uint64_t)-1;
pipe_reg.stream_end_offset = (uint64_t)-1;
mailbox_sw_regs_write(cd->pipeline_reg_offset, &pipe_reg, sizeof(pipe_reg));
}
return 0;
}
static int copier_comp_trigger(struct comp_dev *dev, int cmd)
{
struct processing_module *mod = comp_mod(dev);
struct copier_data *cd = module_get_private_data(mod);
struct sof_ipc_stream_posn posn;
struct comp_dev *dai_copier;
struct comp_buffer *buffer;
uint32_t latency;
int ret;
comp_dbg(dev, "entry");
ret = comp_set_state(dev, cmd);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
switch (dev->ipc_config.type) {
case SOF_COMP_HOST:
if (!cd->ipc_gtw) {
ret = host_common_trigger(cd->hd, dev, cmd);
if (ret < 0)
return ret;
}
break;
case SOF_COMP_DAI:
ret = copier_dai_trigger(cd, dev, cmd);
break;
default:
break;
}
/* For capture cd->pipeline_reg_offset == 0 */
if (!cd->endpoint_num || !cd->pipeline_reg_offset)
return 0;
dai_copier = pipeline_get_dai_comp_latency(dev->pipeline->pipeline_id, &latency);
if (!dai_copier) {
/*
* If the (playback) stream does not have a dai, the offset
* calculation is skipped
*/
comp_info(dev,
"No dai copier found, start/end offset is not calculated");
return 0;
}
/* dai is in another pipeline and it is not prepared or active */
if (dai_copier->state <= COMP_STATE_READY) {
struct ipc4_pipeline_registers pipe_reg;
comp_warn(dev, "dai is not ready");
pipe_reg.stream_start_offset = 0;
pipe_reg.stream_end_offset = 0;
mailbox_sw_regs_write(cd->pipeline_reg_offset, &pipe_reg, sizeof(pipe_reg));
return 0;
}
comp_position(dai_copier, &posn);
/* update stream start and end offset for running message in host copier
* host driver uses DMA link counter to calculate stream position, but it is
* not fit for the following 3 cases:
* (1) dai is enabled before host since they are in different pipelines
* (2) multiple stream are mixed into one dai
* (3) one stream is paused but the dai is still working with other stream
*
* host uses stream_start_offset & stream_end_offset to deal with such cases:
* if (stream_start_offset) {
* position = DMA counter - stream_start_offset
* if (stream_stop_offset)
* position = stream_stop_offset -stream_start_offset;
* } else {
* position = 0;
* }
* When host component is started stream_start_offset is set to DMA counter
* plus the latency from host to dai since the accurate DMA counter should be
* used when the data arrives dai from host.
*
* When pipeline is paused, stream_end_offset will save current DMA counter
* for resume case
*
* When pipeline is resumed, calculate stream_start_offset based on current
* DMA counter and stream_end_offset
*/
if (cmd == COMP_TRIGGER_START) {
struct ipc4_pipeline_registers pipe_reg;
if (list_is_empty(&dai_copier->bsource_list)) {
comp_err(dev, "No source buffer bound to dai_copier");
return -EINVAL;
}
buffer = comp_dev_get_first_data_producer(dai_copier);
pipe_reg.stream_start_offset = posn.dai_posn +
latency * audio_stream_period_bytes(&buffer->stream, dev->frames);
pipe_reg.stream_end_offset = 0;
mailbox_sw_regs_write(cd->pipeline_reg_offset, &pipe_reg, sizeof(pipe_reg));
} else if (cmd == COMP_TRIGGER_PAUSE) {
uint64_t stream_end_offset;
stream_end_offset = posn.dai_posn;
mailbox_sw_regs_write(cd->pipeline_reg_offset + sizeof(uint64_t),
&stream_end_offset, sizeof(stream_end_offset));
} else if (cmd == COMP_TRIGGER_RELEASE) {
struct ipc4_pipeline_registers pipe_reg;
pipe_reg.stream_start_offset = mailbox_sw_reg_read64(cd->pipeline_reg_offset);
pipe_reg.stream_end_offset = mailbox_sw_reg_read64(cd->pipeline_reg_offset +
sizeof(pipe_reg.stream_start_offset));
pipe_reg.stream_start_offset += posn.dai_posn - pipe_reg.stream_end_offset;
if (list_is_empty(&dai_copier->bsource_list)) {
comp_err(dev, "No source buffer bound to dai_copier");
return -EINVAL;
}
buffer = comp_dev_get_first_data_producer(dai_copier);
pipe_reg.stream_start_offset += latency *
audio_stream_period_bytes(&buffer->stream, dev->frames);
mailbox_sw_regs_write(cd->pipeline_reg_offset, &pipe_reg.stream_start_offset,
sizeof(pipe_reg.stream_start_offset));
}
return ret;
}
static int do_conversion_copy(struct comp_dev *dev,
struct copier_data *cd,
struct comp_buffer *src,
struct comp_buffer *sink,
struct comp_copy_limits *processed_data)
{
int i;
/* buffer params might be not yet configured by component on another pipeline */
if (!audio_buffer_hw_params_configured(&src->audio_buffer) ||
!audio_buffer_hw_params_configured(&sink->audio_buffer))
return 0;
comp_get_copy_limits(src, sink, processed_data);
/*
* Buffer ID is constructed as IPC4_COMP_ID(src_queue, dst_queue).
* From the buffer's perspective, copier's sink is the source,
* so we use IPC4_SRC_QUEUE_ID() to get the correct copier sink index.
*/
i = IPC4_SRC_QUEUE_ID(buf_get_id(sink));
if (i >= IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT)
return -EINVAL;
buffer_stream_invalidate(src, processed_data->source_bytes);
cd->converter[i](&src->stream, 0, &sink->stream, 0,
processed_data->frames * audio_stream_get_channels(&src->stream),
DUMMY_CHMAP);
buffer_stream_writeback(sink, processed_data->sink_bytes);
comp_update_buffer_produce(sink, processed_data->sink_bytes);
return 0;
}
static int copier_copy_to_sinks(struct copier_data *cd, struct comp_dev *dev,
struct comp_buffer *src_c,
struct comp_copy_limits *processed_data)
{
struct comp_buffer *sink;
int ret = 0;
/* module copy, one source to multiple sink buffers */
comp_dev_for_each_consumer(dev, sink) {
struct comp_dev *sink_dev;
sink_dev = comp_buffer_get_sink_component(sink);
processed_data->sink_bytes = 0;
if (sink_dev->state == COMP_STATE_ACTIVE) {
ret = do_conversion_copy(dev, cd, src_c, sink, processed_data);
cd->output_total_data_processed += processed_data->sink_bytes;
}
if (ret < 0) {
comp_err(dev, "failed to copy buffer for comp %x",
dev->ipc_config.id);
break;
}
}
if (!ret) {
comp_update_buffer_consume(src_c, processed_data->source_bytes);
/* module copy case with endpoint_num == 0 or src_c as source buffer */
if (!cd->endpoint_num || cd->bsource_buffer)
cd->input_total_data_processed += processed_data->source_bytes;
}
return ret;
}
static int copier_module_copy(struct processing_module *mod,
struct input_stream_buffer *input_buffers, int num_input_buffers,
struct output_stream_buffer *output_buffers, int num_output_buffers)
{
struct copier_data *cd = module_get_private_data(mod);
struct comp_buffer *src_c;
struct comp_copy_limits processed_data;
int i;
if (!num_input_buffers || !num_output_buffers)
return 0;
src_c = container_of(input_buffers[0].data, struct comp_buffer, stream);
processed_data.source_bytes = 0;
/* convert format and copy to each active sink */
for (i = 0; i < num_output_buffers; i++) {
struct comp_buffer *sink_c;
struct comp_dev *sink_dev;
sink_c = container_of(output_buffers[i].data, struct comp_buffer, stream);
sink_dev = comp_buffer_get_sink_component(sink_c);
processed_data.sink_bytes = 0;
if (sink_dev->state == COMP_STATE_ACTIVE) {
uint32_t source_samples;
int sink_queue_id;
/*
* Buffer ID is constructed as IPC4_COMP_ID(src_queue, dst_queue).
* From the buffer's perspective, copier's sink is the source,
* so we use IPC4_SRC_QUEUE_ID() to get the correct copier sink index.
*/
sink_queue_id = IPC4_SRC_QUEUE_ID(buf_get_id(sink_c));
if (sink_queue_id >= IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT)
return -EINVAL;
comp_get_copy_limits(src_c, sink_c, &processed_data);
source_samples = processed_data.frames *
audio_stream_get_channels(input_buffers[0].data);
cd->converter[sink_queue_id](input_buffers[0].data, 0,
output_buffers[i].data, 0,
source_samples, DUMMY_CHMAP);
output_buffers[i].size = processed_data.sink_bytes;
cd->output_total_data_processed += processed_data.sink_bytes;
}
}
input_buffers[0].consumed = processed_data.source_bytes;
return 0;
}
static int copier_multi_endpoint_dai_copy(struct copier_data *cd, struct comp_dev *dev)
{
struct comp_copy_limits processed_data;
struct comp_buffer *src;
int ret;
processed_data.source_bytes = 0;
if (!cd->bsource_buffer) {
/* gateway(s) as input */
ret = dai_zephyr_multi_endpoint_copy(cd->dd, dev, cd->multi_endpoint_buffer,
cd->endpoint_num);
if (ret < 0)
return ret;
ret = copier_copy_to_sinks(cd, dev, cd->multi_endpoint_buffer, &processed_data);
return ret;
}
/* component as input */
if (list_is_empty(&dev->bsource_list)) {
comp_err(dev, "No source buffer bound");
return -EINVAL;
}
src = comp_dev_get_first_data_producer(dev);
/* gateway(s) on output */
ret = do_conversion_copy(dev, cd, src, cd->multi_endpoint_buffer, &processed_data);
if (ret < 0)
return ret;
ret = dai_zephyr_multi_endpoint_copy(cd->dd, dev, cd->multi_endpoint_buffer,
cd->endpoint_num);
if (!ret) {
comp_update_buffer_consume(src, processed_data.source_bytes);
cd->input_total_data_processed += processed_data.source_bytes;
}
return ret;
}
/* Copier has one input and one or more outputs. Maximum of one gateway can be connected
* to copier or no gateway connected at all. Gateway can only be connected to either input
* pin 0 (the only input) or output pin 0. With or without connected gateway it is also
* possible to have component(s) connected on input and/or output pins.
*
* A special exception is a multichannel ALH gateway case. These are multiple gateways
* but should be treated like a single gateway to satisfy rules above. Data from such
* gateways has to be multiplexed into single stream (for input gateways) or demultiplexed
* from single stream (for output gateways) so such gateways work kind of like a single
* gateway, i.e., produce/consume single stream.
*/
static int copier_process(struct processing_module *mod,
struct input_stream_buffer *input_buffers, int num_input_buffers,
struct output_stream_buffer *output_buffers, int num_output_buffers)
{
struct copier_data *cd = module_get_private_data(mod);
struct comp_dev *dev = mod->dev;
comp_dbg(dev, "entry");
switch (dev->ipc_config.type) {
case SOF_COMP_HOST:
if (!cd->ipc_gtw)
return host_common_copy(cd->hd, dev, copier_host_dma_cb);
/* do nothing in the gateway copier case */
return 0;
case SOF_COMP_DAI:
if (cd->endpoint_num == 1)
return dai_common_copy(cd->dd[0], dev, cd->converter);
return copier_multi_endpoint_dai_copy(cd, dev);
default:
break;
}
/* module copier case */
return copier_module_copy(mod, input_buffers, num_input_buffers, output_buffers,
num_output_buffers);
}
static int copier_params(struct processing_module *mod)
{
struct sof_ipc_stream_params *params = mod->stream_params;
struct copier_data *cd = module_get_private_data(mod);
struct comp_dev *dev = mod->dev;
int i, ret = 0;
comp_dbg(dev, "entry");
copier_update_params(cd, dev, params);
for (i = 0; i < cd->endpoint_num; i++) {
switch (dev->ipc_config.type) {
case SOF_COMP_HOST:
if (!cd->ipc_gtw)
ret = copier_host_params(cd, dev, params);
else
/* handle gtw case */
ret = copier_ipcgtw_params(cd->ipcgtw_data, dev, params);
break;
case SOF_COMP_DAI:
ret = copier_dai_params(cd, dev, params, i);
break;
default:
break;
}
if (ret < 0)
break;
}
return ret;
}
__cold static int copier_set_sink_fmt(struct comp_dev *dev, const void *data,
int max_data_size)
{
const struct ipc4_copier_config_set_sink_format *sink_fmt = data;
struct processing_module *mod = comp_mod(dev);
struct copier_data *cd = module_get_private_data(mod);
uint32_t chmap;
assert_can_be_cold();
if (max_data_size < sizeof(*sink_fmt)) {
comp_err(dev, "error: max_data_size %d should be bigger than %d", max_data_size,
sizeof(*sink_fmt));
return -EINVAL;
}
if (sink_fmt->sink_id >= IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT) {
comp_err(dev, "error: sink id %d is out of range", sink_fmt->sink_id);
return -EINVAL;
}
if (memcmp(&cd->config.base.audio_fmt, &sink_fmt->source_fmt,
sizeof(sink_fmt->source_fmt))) {
comp_err(dev, "error: source fmt should be equal to input fmt");
return -EINVAL;
}
if (cd->endpoint_num && cd->bsource_buffer &&
sink_fmt->sink_id == IPC4_COPIER_GATEWAY_PIN) {
comp_err(dev, "can't change gateway format");
return -EINVAL;
}
cd->out_fmt[sink_fmt->sink_id] = sink_fmt->sink_fmt;
if (cd->endpoint_num > 0 && dev->ipc_config.type == SOF_COMP_DAI)
chmap = cd->dd[0]->chmap;
else
chmap = DUMMY_CHMAP;
cd->converter[sink_fmt->sink_id] = get_converter_func(&sink_fmt->source_fmt,
&sink_fmt->sink_fmt, ipc4_gtw_none,
ipc4_bidirection, chmap);
return 0;
}
__cold static int set_attenuation(struct comp_dev *dev, uint32_t data_offset, const char *data)
{
struct processing_module *mod = comp_mod(dev);
struct copier_data *cd = module_get_private_data(mod);
uint32_t attenuation;
enum sof_ipc_frame valid_fmt, frame_fmt;
assert_can_be_cold();
/* only support attenuation in format of 32bit */
if (data_offset > sizeof(uint32_t)) {
comp_err(dev, "attenuation data size %d is incorrect", data_offset);
return -EINVAL;
}
attenuation = *(const uint32_t *)data;
if (attenuation > 31) {
comp_err(dev, "attenuation %d is out of range", attenuation);
return -EINVAL;
}
audio_stream_fmt_conversion(cd->config.out_fmt.depth,
cd->config.out_fmt.valid_bit_depth,
&frame_fmt, &valid_fmt,
cd->config.out_fmt.s_type);
if (frame_fmt < SOF_IPC_FRAME_S24_4LE) {
comp_err(dev, "frame_fmt %d isn't supported by attenuation",
frame_fmt);
return -EINVAL;
}
cd->attenuation = attenuation;
return 0;
}
__cold static int set_chmap(struct comp_dev *dev, const void *data, size_t data_size)
{
const struct ipc4_copier_config_channel_map *chmap_cfg = data;
struct processing_module *mod = comp_mod(dev);
struct copier_data *cd = module_get_private_data(mod);
enum ipc4_direction_type dir;
struct ipc4_audio_format in_fmt = cd->config.base.audio_fmt;
struct ipc4_audio_format out_fmt = cd->config.out_fmt;
pcm_converter_func process;
pcm_converter_func converters[IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT];
int i;
uint32_t irq_flags;
assert_can_be_cold();
if (data_size < sizeof(*chmap_cfg)) {
comp_err(dev, "Wrong payload size: %d", data_size);
return -EINVAL;
}
if (cd->endpoint_num == 0 || dev->ipc_config.type != SOF_COMP_DAI) {
comp_err(dev, "Only DAI gateway supports changing chmap");
return -EINVAL;
}
comp_info(dev, "New chmap requested: %x", chmap_cfg->channel_map);
if (!cd->dd[0]->dma_buffer) {
/* DMA buffer not yet created. Remember the chmap, it will be used
* later in .params() handler.
*
* The assignment should be atomic as LL thread can preempt this IPC thread.
*/
cd->dd[0]->chmap = chmap_cfg->channel_map;
return 0;
}
copier_dai_adjust_params(cd, &in_fmt, &out_fmt);
dir = (cd->direction == SOF_IPC_STREAM_PLAYBACK) ?
ipc4_playback : ipc4_capture;
process = get_converter_func(&in_fmt, &out_fmt, cd->gtw_type, dir, chmap_cfg->channel_map);
if (!process) {
comp_err(dev, "No gtw converter func found!");
return -EINVAL;
}
/* Channel map is same for all sinks. However, as sinks allowed to have different
* sample formats, get new convert/remap function for each sink.
*/
for (i = 0; i < IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT; i++) {
if (cd->converter[i]) {
converters[i] = get_converter_func(&in_fmt, &cd->out_fmt[i],
ipc4_gtw_none, ipc4_bidirection,
chmap_cfg->channel_map);
/* Do not report an error if converter not found as sinks could be
* bound/unbound on a fly and out_fmt[i] may contain obsolete data.
*/
} else {
converters[i] = NULL;
}
}
/* Atomically update chmap, process and converters */
irq_local_disable(irq_flags);
cd->dd[0]->chmap = chmap_cfg->channel_map;
cd->dd[0]->process = process;
for (i = 0; i < IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT; i++)
cd->converter[i] = converters[i];
irq_local_enable(irq_flags);
return 0;
}
__cold static int copier_set_configuration(struct processing_module *mod,
uint32_t config_id,
enum module_cfg_fragment_position pos,
uint32_t data_offset_size,
const uint8_t *fragment, size_t fragment_size,
uint8_t *response,
size_t response_size)
{
struct comp_dev *dev = mod->dev;
assert_can_be_cold();
comp_dbg(dev, "copier_set_config()");
switch (config_id) {
case IPC4_COPIER_MODULE_CFG_PARAM_SET_SINK_FORMAT:
return copier_set_sink_fmt(dev, fragment, fragment_size);
case IPC4_COPIER_MODULE_CFG_ATTENUATION:
return set_attenuation(dev, fragment_size, (const char *)fragment);
case IPC4_COPIER_MODULE_CFG_PARAM_CHANNEL_MAP:
return set_chmap(dev, fragment, fragment_size);
default:
return -EINVAL;
}
}
static inline void convert_u64_to_u32s(uint64_t val, uint32_t *val_l, uint32_t *val_h)
{
*val_l = (uint32_t)(val & 0xffffffff);
*val_h = (uint32_t)((val >> 32) & 0xffffffff);
}
__cold static int copier_get_configuration(struct processing_module *mod,
uint32_t config_id, uint32_t *data_offset_size,
uint8_t *fragment, size_t fragment_size)
{
struct copier_data *cd = module_get_private_data(mod);
struct ipc4_llp_reading_extended llp_ext;
struct comp_dev *dev = mod->dev;
struct sof_ipc_stream_posn posn;
struct ipc4_llp_reading llp;
assert_can_be_cold();
if (cd->ipc_gtw)
return 0;
switch (config_id) {
case IPC4_COPIER_MODULE_CFG_PARAM_LLP_READING:
if (!cd->endpoint_num ||
comp_get_endpoint_type(dev) !=
COMP_ENDPOINT_DAI) {
comp_err(dev, "Invalid component type");
return -EINVAL;
}
if (*data_offset_size < sizeof(struct ipc4_llp_reading)) {
comp_err(dev, "Config size %d is inadequate", *data_offset_size);
return -EINVAL;
}
*data_offset_size = sizeof(struct ipc4_llp_reading);
memset(&llp, 0, sizeof(llp));
if (dev->state != COMP_STATE_ACTIVE) {
memcpy_s(fragment, sizeof(llp), &llp, sizeof(llp));
return 0;
}
/* get llp from dai */
comp_position(dev, &posn);
convert_u64_to_u32s(posn.comp_posn, &llp.llp_l, &llp.llp_u);
convert_u64_to_u32s(posn.wallclock, &llp.wclk_l, &llp.wclk_u);
memcpy_s(fragment, sizeof(llp), &llp, sizeof(llp));
return 0;
case IPC4_COPIER_MODULE_CFG_PARAM_LLP_READING_EXTENDED:
if (!cd->endpoint_num ||
comp_get_endpoint_type(dev) !=
COMP_ENDPOINT_DAI) {
comp_err(dev, "Invalid component type");
return -EINVAL;
}
if (*data_offset_size < sizeof(struct ipc4_llp_reading_extended)) {
comp_err(dev, "Config size %d is inadequate", *data_offset_size);
return -EINVAL;
}
*data_offset_size = sizeof(struct ipc4_llp_reading_extended);
memset(&llp_ext, 0, sizeof(llp_ext));
if (dev->state != COMP_STATE_ACTIVE) {
memcpy_s(fragment, sizeof(llp_ext), &llp_ext, sizeof(llp_ext));
return 0;
}
/* get llp from dai */
comp_position(dev, &posn);
convert_u64_to_u32s(posn.comp_posn, &llp_ext.llp_reading.llp_l,
&llp_ext.llp_reading.llp_u);
convert_u64_to_u32s(posn.wallclock, &llp_ext.llp_reading.wclk_l,
&llp_ext.llp_reading.wclk_u);
convert_u64_to_u32s(posn.dai_posn, &llp_ext.tpd_low, &llp_ext.tpd_high);