Repository navigation
Expand file tree
/
Copy pathcopier.c
More file actions
1216 lines (1014 loc) · 34.4 KB
/
Copy pathcopier.c
File metadata and controls
1216 lines (1014 loc) · 34.4 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 <sof/debug/panic.h>
#include <sof/drivers/interrupt.h>
#include <sof/ipc/msg.h>
#include <sof/ipc/topology.h>
#include <sof/drivers/interrupt.h>
#include <sof/drivers/timer.h>
#include <sof/lib/alloc.h>
#include <sof/lib/cache.h>
#include <sof/lib/memory.h>
#include <sof/lib/notifier.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <sof/string.h>
#include <sof/ut.h>
#include <sof/trace/trace.h>
#include <ipc4/alh.h>
#include <ipc4/copier.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>
static const struct comp_driver comp_copier;
LOG_MODULE_REGISTER(copier, CONFIG_SOF_LOG_LEVEL);
/* this id aligns windows driver requirement to support windows driver */
/* 9ba00c83-ca12-4a83-943c-1fa2e82f9dda */
DECLARE_SOF_RT_UUID("copier", copier_comp_uuid, 0x9ba00c83, 0xca12, 0x4a83,
0x94, 0x3c, 0x1f, 0xa2, 0xe8, 0x2f, 0x9d, 0xda);
DECLARE_TR_CTX(copier_comp_tr, SOF_UUID(copier_comp_uuid), LOG_LEVEL_INFO);
struct copier_data {
/* Must be the 1st field, function ipc4_create_buffer casts components private data
* as ipc4_base_module_cfg!
*/
struct ipc4_copier_module_cfg config;
struct comp_dev *endpoint[IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT];
struct comp_buffer *endpoint_buffer[IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT];
uint32_t endpoint_num;
bool bsource_buffer;
int direction;
/* sample data >> attenuation in range of [1 - 31] */
uint32_t attenuation;
/* pipeline register offset in memory windows 0 */
uint32_t pipeline_reg_offset;
uint64_t host_position;
struct ipc4_audio_format out_fmt[IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT];
pcm_converter_func converter[IPC4_COPIER_MODULE_OUTPUT_PINS_COUNT];
};
static pcm_converter_func get_converter_func(struct ipc4_audio_format *in_fmt,
struct ipc4_audio_format *out_fmt,
enum ipc4_gateway_type type,
enum ipc4_direction_type);
static void create_endpoint_buffer(struct comp_dev *parent_dev,
struct copier_data *cd,
struct comp_ipc_config *config,
struct ipc4_copier_module_cfg *copier_cfg,
enum ipc4_gateway_type type,
int index)
{
enum sof_ipc_frame in_frame_fmt, out_frame_fmt;
enum sof_ipc_frame in_valid_fmt, out_valid_fmt;
enum sof_ipc_frame valid_fmt;
struct sof_ipc_buffer ipc_buf;
uint32_t buf_size;
uint32_t mask;
int i;
audio_stream_fmt_conversion(copier_cfg->base.audio_fmt.depth,
copier_cfg->base.audio_fmt.valid_bit_depth,
&in_frame_fmt,
&in_valid_fmt,
copier_cfg->base.audio_fmt.s_type);
audio_stream_fmt_conversion(copier_cfg->out_fmt.depth,
copier_cfg->out_fmt.valid_bit_depth,
&out_frame_fmt,
&out_valid_fmt,
copier_cfg->out_fmt.s_type);
/* playback case:
*
* --> copier0 -----> buf1 ----> .... bufn --------> copier1
* | /|\ |conversion |
* \|/ |conversion \|/ \|/
* host-> endpoint buffer0 endpoint buffer1 -> dai -->
*
* capture case:
*
* copier1 <------ bufn <---- .... buf1 <------- copier0 <--
* | |conversion /|\ |
* \|/ \|/ |conversion \|/
* <-- host <- endpoint buffer1 endpoint buffer0 <- dai
*
* According to above graph, the format of endpoint buffer
* depends on stream direction and component type.
*/
if (cd->direction == SOF_IPC_STREAM_PLAYBACK) {
if (config->type == SOF_COMP_HOST) {
config->frame_fmt = in_frame_fmt;
valid_fmt = in_valid_fmt;
buf_size = copier_cfg->base.ibs * 2;
} else {
config->frame_fmt = out_frame_fmt;
valid_fmt = out_valid_fmt;
buf_size = copier_cfg->base.obs * 2;
}
mask = copier_cfg->out_fmt.ch_map;
} else {
if (config->type == SOF_COMP_HOST) {
config->frame_fmt = out_frame_fmt;
valid_fmt = out_valid_fmt;
buf_size = copier_cfg->base.obs * 2;
} else {
config->frame_fmt = in_frame_fmt;
valid_fmt = in_valid_fmt;
buf_size = copier_cfg->base.ibs * 2;
}
mask = copier_cfg->base.audio_fmt.ch_map;
}
parent_dev->ipc_config.frame_fmt = config->frame_fmt;
memset(&ipc_buf, 0, sizeof(ipc_buf));
ipc_buf.size = buf_size;
ipc_buf.comp.pipeline_id = config->pipeline_id;
ipc_buf.comp.core = config->core;
cd->endpoint_buffer[cd->endpoint_num] = buffer_new(&ipc_buf);
cd->endpoint_buffer[cd->endpoint_num]->stream.channels =
copier_cfg->base.audio_fmt.channels_count;
cd->endpoint_buffer[cd->endpoint_num]->stream.rate =
copier_cfg->base.audio_fmt.sampling_frequency;
cd->endpoint_buffer[cd->endpoint_num]->stream.frame_fmt = config->frame_fmt;
cd->endpoint_buffer[cd->endpoint_num]->stream.valid_sample_fmt = valid_fmt;
cd->endpoint_buffer[cd->endpoint_num]->buffer_fmt =
copier_cfg->base.audio_fmt.interleaving_style;
if (type == ipc4_gtw_alh) {
struct sof_alh_configuration_blob *alh_blob;
if (copier_cfg->gtw_cfg.config_length) {
alh_blob = (struct sof_alh_configuration_blob *)
copier_cfg->gtw_cfg.config_data;
mask = alh_blob->alh_cfg.mapping[index].channel_mask;
}
}
for (i = 0; i < SOF_IPC_MAX_CHANNELS; i++)
cd->endpoint_buffer[cd->endpoint_num]->chmap[i] = (mask >> i * 4) & 0xf;
}
/* if copier is linked to host gateway, it will manage host dma.
* Sof host component can support this case so copier reuses host
* component to support host gateway.
*/
static int create_host(struct comp_dev *parent_dev, struct copier_data *cd,
struct comp_ipc_config *config,
struct ipc4_copier_module_cfg *copier_cfg,
int dir)
{
struct sof_uuid host = {0x8b9d100c, 0x6d78, 0x418f, {0x90, 0xa3, 0xe0,
0xe8, 0x05, 0xd0, 0x85, 0x2b}};
struct ipc_config_host ipc_host;
const struct comp_driver *drv;
struct comp_dev *dev;
drv = ipc4_get_drv((uint8_t *)&host);
if (!drv)
return -EINVAL;
config->type = SOF_COMP_HOST;
create_endpoint_buffer(parent_dev, cd, config, copier_cfg, ipc4_gtw_host, 0);
memset(&ipc_host, 0, sizeof(ipc_host));
ipc_host.direction = dir;
dev = drv->ops.create(drv, config, &ipc_host);
if (!dev)
return -EINVAL;
list_init(&dev->bsource_list);
list_init(&dev->bsink_list);
if (cd->direction == SOF_IPC_STREAM_PLAYBACK) {
comp_buffer_connect(dev, config->core, cd->endpoint_buffer[cd->endpoint_num],
PPL_CONN_DIR_COMP_TO_BUFFER);
cd->bsource_buffer = false;
} else {
comp_buffer_connect(dev, config->core, cd->endpoint_buffer[cd->endpoint_num],
PPL_CONN_DIR_BUFFER_TO_COMP);
cd->bsource_buffer = true;
}
cd->converter[IPC4_COPIER_GATEWAY_PIN] =
get_converter_func(&copier_cfg->base.audio_fmt,
&copier_cfg->out_fmt,
ipc4_gtw_host, IPC4_DIRECTION(dir));
if (!cd->converter[IPC4_COPIER_GATEWAY_PIN]) {
comp_err(parent_dev, "failed to get converter for host, dir %d", dir);
return -EINVAL;
}
cd->endpoint[cd->endpoint_num++] = dev;
return 0;
}
static int init_dai(struct comp_dev *parent_dev,
const struct comp_driver *drv,
struct comp_ipc_config *config,
struct ipc4_copier_module_cfg *copier,
struct pipeline *pipeline,
struct ipc_config_dai *dai,
enum ipc4_gateway_type type,
int index)
{
struct comp_dev *dev;
struct copier_data *cd;
int ret;
cd = comp_get_drvdata(parent_dev);
create_endpoint_buffer(parent_dev, cd, config, copier, type, index);
dev = drv->ops.create(drv, config, dai);
if (!dev)
return -EINVAL;
if (dai->direction == SOF_IPC_STREAM_PLAYBACK)
pipeline->sink_comp = dev;
else
pipeline->source_comp = dev;
pipeline->sched_id = config->id;
list_init(&dev->bsource_list);
list_init(&dev->bsink_list);
ret = comp_dai_config(dev, dai, copier);
if (ret < 0)
return ret;
if (dai->direction == SOF_IPC_STREAM_PLAYBACK) {
comp_buffer_connect(dev, config->core, cd->endpoint_buffer[cd->endpoint_num],
PPL_CONN_DIR_BUFFER_TO_COMP);
cd->bsource_buffer = true;
} else {
comp_buffer_connect(dev, config->core, cd->endpoint_buffer[cd->endpoint_num],
PPL_CONN_DIR_COMP_TO_BUFFER);
cd->bsource_buffer = false;
}
cd->converter[IPC4_COPIER_GATEWAY_PIN] =
get_converter_func(&copier->base.audio_fmt, &copier->out_fmt, type,
IPC4_DIRECTION(dai->direction));
if (!cd->converter[IPC4_COPIER_GATEWAY_PIN]) {
comp_err(parent_dev, "failed to get converter type %d, dir %d",
type, dai->direction);
return -EINVAL;
}
cd->endpoint[cd->endpoint_num++] = dev;
return 0;
}
/* if copier is linked to non-host gateway, it will manage link dma,
* ssp, dmic or alh. Sof dai component can support this case so copier
* reuses dai component to support non-host gateway.
*/
static int create_dai(struct comp_dev *parent_dev, struct copier_data *cd,
struct comp_ipc_config *config,
struct ipc4_copier_module_cfg *copier,
struct pipeline *pipeline)
{
struct sof_uuid id = {0xc2b00d27, 0xffbc, 0x4150, {0xa5, 0x1a, 0x24,
0x5c, 0x79, 0xc5, 0xe5, 0x4b}};
int dai_index[IPC4_ALH_MAX_NUMBER_OF_GTW];
struct sof_alh_configuration_blob *alh_blob;
union ipc4_connector_node_id node_id;
enum ipc4_gateway_type type;
const struct comp_driver *drv;
struct ipc_config_dai dai;
int dai_count;
int ret;
int i;
drv = ipc4_get_drv((uint8_t *)&id);
if (!drv)
return -EINVAL;
config->type = SOF_COMP_DAI;
memset(&dai, 0, sizeof(dai));
dai_count = 1;
node_id.dw = copier->gtw_cfg.node_id;
dai_index[dai_count - 1] = node_id.f.v_index;
dai.direction = node_id.f.dma_type % 2;
dai.is_config_blob = true;
switch (node_id.f.dma_type) {
case ipc4_hda_link_output_class:
case ipc4_hda_link_input_class:
dai.type = SOF_DAI_INTEL_HDA;
dai.is_config_blob = true;
type = ipc4_gtw_link;
break;
case ipc4_i2s_link_output_class:
case ipc4_i2s_link_input_class:
dai_index[dai_count - 1] = (dai_index[dai_count - 1] >> 4) & 0xF;
dai.type = SOF_DAI_INTEL_SSP;
dai.is_config_blob = true;
type = ipc4_gtw_ssp;
break;
case ipc4_alh_link_output_class:
case ipc4_alh_link_input_class:
dai.type = SOF_DAI_INTEL_ALH;
dai.is_config_blob = true;
type = ipc4_gtw_alh;
/* copier
* {
* gtw_cfg
* {
* gtw_node_id;
* config_length;
* config_data
* {
* count;
* {
* node_id; \\ normal gtw id
* mask;
* } mapping[MAX_ALH_COUNT];
* }
* }
* }
*/
/* get gtw node id in config data */
if (copier->gtw_cfg.config_length) {
alh_blob = (struct sof_alh_configuration_blob *)copier->gtw_cfg.config_data;
dai_count = alh_blob->alh_cfg.count;
for (i = 0; i < dai_count; i++)
dai_index[i] =
IPC4_ALH_DAI_INDEX(alh_blob->alh_cfg.mapping[i].alh_id);
} else {
dai_index[dai_count - 1] = IPC4_ALH_DAI_INDEX(node_id.f.v_index);
}
break;
case ipc4_dmic_link_input_class:
dai.type = SOF_DAI_INTEL_DMIC;
dai.is_config_blob = true;
type = ipc4_gtw_dmic;
break;
default:
return -EINVAL;
}
for (i = 0; i < dai_count; i++) {
dai.dai_index = dai_index[i];
ret = init_dai(parent_dev, drv, config, copier, pipeline, &dai, type, i);
if (ret) {
comp_err(parent_dev, "failed to create dai");
break;
}
}
return ret;
}
static void init_pipeline_reg(struct copier_data *cd)
{
union ipc4_connector_node_id node_id;
struct ipc4_pipeline_registers pipe_reg;
int gateway_id;
node_id.dw = cd->config.gtw_cfg.node_id;
gateway_id = node_id.f.v_index;
/* pipeline position is stored in memory windows 0 at the following offset
* please check struct ipc4_fw_registers definition. The number of
* pipeline reg depends on the host dma count for playback
*/
cd->pipeline_reg_offset = offsetof(struct ipc4_fw_registers, pipeline_regs);
cd->pipeline_reg_offset += gateway_id * sizeof(struct ipc4_pipeline_registers);
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));
}
static struct comp_dev *copier_new(const struct comp_driver *drv,
struct comp_ipc_config *config,
void *spec)
{
struct ipc4_copier_module_cfg *copier = spec;
union ipc4_connector_node_id node_id;
struct ipc_comp_dev *ipc_pipe;
struct ipc *ipc = ipc_get();
struct copier_data *cd;
struct comp_dev *dev;
size_t size, config_size;
comp_cl_dbg(&comp_copier, "copier_new()");
dev = comp_alloc(drv, sizeof(*dev));
if (!dev)
return NULL;
dev->ipc_config = *config;
config_size = copier->gtw_cfg.config_length * sizeof(uint32_t);
dcache_invalidate_region((__sparse_force char __sparse_cache *)spec + sizeof(*copier),
config_size);
size = sizeof(*cd);
cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, size);
if (!cd)
goto error;
size = sizeof(*copier);
mailbox_hostbox_read(&cd->config, size, 0, size);
cd->out_fmt[0] = cd->config.out_fmt;
comp_set_drvdata(dev, cd);
list_init(&dev->bsource_list);
list_init(&dev->bsink_list);
ipc_pipe = ipc_get_comp_by_ppl_id(ipc, COMP_TYPE_PIPELINE, config->pipeline_id);
if (!ipc_pipe) {
comp_cl_err(&comp_copier, "pipeline %d is not existed", config->pipeline_id);
goto error_cd;
}
dev->pipeline = ipc_pipe->pipeline;
/* copier is linked to gateway */
if (copier->gtw_cfg.node_id != IPC4_INVALID_NODE_ID) {
node_id.dw = copier->gtw_cfg.node_id;
cd->direction = node_id.f.dma_type % 2;
switch (node_id.f.dma_type) {
case ipc4_hda_host_output_class:
case ipc4_hda_host_input_class:
if (create_host(dev, cd, config, copier, cd->direction)) {
comp_cl_err(&comp_copier, "unenable to create host");
goto error_cd;
}
if (cd->direction == SOF_IPC_STREAM_PLAYBACK) {
ipc_pipe->pipeline->source_comp = dev;
init_pipeline_reg(cd);
} else {
ipc_pipe->pipeline->sink_comp = dev;
}
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:
if (create_dai(dev, cd, config, copier, ipc_pipe->pipeline)) {
comp_cl_err(&comp_copier, "unenable to create dai");
goto error_cd;
}
if (cd->direction == SOF_IPC_STREAM_PLAYBACK)
ipc_pipe->pipeline->sink_comp = dev;
else
ipc_pipe->pipeline->source_comp = dev;
break;
default:
comp_cl_err(&comp_copier, "unsupported dma type %x",
(uint32_t)node_id.f.dma_type);
goto error_cd;
};
}
dev->direction = cd->direction;
dev->state = COMP_STATE_READY;
return dev;
error_cd:
rfree(cd);
error:
rfree(dev);
return NULL;
}
static void copier_free(struct comp_dev *dev)
{
struct copier_data *cd = comp_get_drvdata(dev);
int i;
for (i = 0; i < cd->endpoint_num; i++) {
cd->endpoint[i]->drv->ops.free(cd->endpoint[i]);
buffer_free(cd->endpoint_buffer[i]);
}
rfree(cd);
rfree(dev);
}
/* In sof normal format conversion path, sample size should be equal
* to container size except format of S24_LE. In ipc4 case, sample
* size can be different with container size. This function is used to
* check conversion mode.
*/
static bool use_no_container_convert_function(enum sof_ipc_frame in,
enum sof_ipc_frame valid_in_bits,
enum sof_ipc_frame out,
enum sof_ipc_frame valid_out_bits)
{
/* valid sample size is equal to container size, go normal path */
if (in == valid_in_bits && out == valid_out_bits) {
if (in == SOF_IPC_FRAME_S24_3LE || out == SOF_IPC_FRAME_S24_3LE)
return false;
return true;
}
return false;
}
static pcm_converter_func get_converter_func(struct ipc4_audio_format *in_fmt,
struct ipc4_audio_format *out_fmt,
enum ipc4_gateway_type type,
enum ipc4_direction_type dir)
{
enum sof_ipc_frame in, in_valid, out, out_valid;
audio_stream_fmt_conversion(in_fmt->depth, in_fmt->valid_bit_depth, &in, &in_valid,
in_fmt->s_type);
audio_stream_fmt_conversion(out_fmt->depth, out_fmt->valid_bit_depth, &out, &out_valid,
out_fmt->s_type);
/* check container & sample size */
if (use_no_container_convert_function(in, in_valid, out, out_valid))
return pcm_get_conversion_function(in, out);
else
return pcm_get_conversion_vc_function(in, in_valid, out, out_valid, type, dir);
}
static int copier_prepare(struct comp_dev *dev)
{
struct copier_data *cd = comp_get_drvdata(dev);
int ret;
comp_dbg(dev, "copier_prepare()");
/* cannot configure DAI while active */
if (dev->state == COMP_STATE_ACTIVE) {
comp_info(dev, "copier_config_prepare(): Component is in active state.");
return 0;
}
ret = comp_set_state(dev, COMP_TRIGGER_PREPARE);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
if (cd->endpoint_num) {
int i;
for (i = 0; i < cd->endpoint_num; i++) {
ret = cd->endpoint[i]->drv->ops.prepare(cd->endpoint[i]);
if (ret < 0)
break;
}
} else {
/* set up format conversion function */
cd->converter[0] = get_converter_func(&cd->config.base.audio_fmt,
&cd->config.out_fmt, ipc4_gtw_none,
ipc4_bidirection);
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);
ret = -EINVAL;
}
}
return ret;
}
static int copier_reset(struct comp_dev *dev)
{
struct copier_data *cd = comp_get_drvdata(dev);
struct ipc4_pipeline_registers pipe_reg;
int ret = 0;
comp_dbg(dev, "copier_reset()");
if (dev->state == COMP_STATE_ACTIVE) {
comp_info(dev, "copier_config(): Component is in active state. Ignore resetting");
return 0;
}
if (cd->endpoint_num) {
int i;
for (i = 0; i < cd->endpoint_num; i++) {
ret = cd->endpoint[i]->drv->ops.reset(cd->endpoint[i]);
if (ret < 0)
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));
}
comp_set_state(dev, COMP_TRIGGER_RESET);
return ret;
}
static int copier_comp_trigger(struct comp_dev *dev, int cmd)
{
struct copier_data *cd = comp_get_drvdata(dev);
struct sof_ipc_stream_posn posn;
struct comp_dev *dai_copier;
struct copier_data *dai_cd;
struct comp_buffer *buffer;
uint32_t latency;
int ret;
comp_dbg(dev, "copier_comp_trigger()");
ret = comp_set_state(dev, cmd);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
if (cd->endpoint) {
int i;
for (i = 0; i < cd->endpoint_num; i++) {
ret = cd->endpoint[i]->drv->ops.trigger(cd->endpoint[i], cmd);
if (ret < 0)
break;
}
}
if (ret < 0 || !cd->endpoint_num || !cd->pipeline_reg_offset)
return ret;
dai_copier = pipeline_get_dai_comp(dev->pipeline->pipeline_id, &latency);
if (!dai_copier) {
comp_err(dev, "failed to find dai comp");
return ret;
}
dai_cd = comp_get_drvdata(dai_copier);
/* dai is in another pipeline and it is not prepared or active */
if (dai_copier->state <= COMP_STATE_READY ||
dai_cd->endpoint[IPC4_COPIER_GATEWAY_PIN]->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_cd->endpoint[IPC4_COPIER_GATEWAY_PIN], &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;
buffer = list_first_item(&dai_copier->bsource_list, struct comp_buffer, sink_list);
pipe_reg.stream_start_offset = posn.dai_posn + latency * buffer->stream.size;
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;
buffer = list_first_item(&dai_copier->bsource_list, struct comp_buffer, sink_list);
pipe_reg.stream_start_offset += latency * buffer->stream.size;
mailbox_sw_regs_write(cd->pipeline_reg_offset, &pipe_reg.stream_start_offset,
sizeof(pipe_reg.stream_start_offset));
}
return ret;
}
static inline int apply_attenuation(struct comp_dev *dev, struct copier_data *cd,
struct comp_buffer *sink, int frame)
{
uint32_t buff_frag = 0;
uint32_t *dst;
int i;
/* only support attenuation in format of 32bit */
switch (sink->stream.frame_fmt) {
case SOF_IPC_FRAME_S16_LE:
comp_err(dev, "16bit sample isn't supported by attenuation");
return -EINVAL;
case SOF_IPC_FRAME_S24_4LE:
case SOF_IPC_FRAME_S32_LE:
for (i = 0; i < frame * sink->stream.channels; i++) {
dst = audio_stream_read_frag_s32(&sink->stream, buff_frag);
*dst >>= cd->attenuation;
buff_frag++;
}
return 0;
default:
comp_err(dev, "unsupported format %d for attenuation", sink->stream.frame_fmt);
return -EINVAL;
}
}
static int do_conversion_copy(struct comp_dev *dev,
struct copier_data *cd,
struct comp_buffer *src,
struct comp_buffer *sink,
uint32_t *src_copy_bytes)
{
struct comp_copy_limits c;
int i;
int ret;
comp_get_copy_limits_with_lock(src, sink, &c);
*src_copy_bytes = c.source_bytes;
i = IPC4_SINK_QUEUE_ID(sink->id);
buffer_stream_invalidate(src, c.source_bytes);
cd->converter[i](&src->stream, 0, &sink->stream, 0, c.frames * sink->stream.channels);
if (cd->attenuation) {
ret = apply_attenuation(dev, cd, sink, c.frames);
if (ret < 0)
return ret;
}
buffer_stream_writeback(sink, c.sink_bytes);
comp_update_buffer_produce(sink, c.sink_bytes);
return 0;
}
/* copy and process stream data from source to sink buffers */
static int copier_copy(struct comp_dev *dev)
{
struct copier_data *cd = comp_get_drvdata(dev);
int ret;
comp_dbg(dev, "copier_copy()");
/* process gateway case */
if (cd->endpoint_num) {
struct comp_buffer *src;
struct comp_buffer *sink;
uint32_t src_copy_bytes;
int i;
if (!cd->bsource_buffer) {
sink = list_first_item(&dev->bsink_list, struct comp_buffer, source_list);
for (i = 0; i < cd->endpoint_num; i++) {
ret = cd->endpoint[i]->drv->ops.copy(cd->endpoint[i]);
if (ret < 0)
return ret;
ret = do_conversion_copy(dev, cd, cd->endpoint_buffer[i], sink,
&src_copy_bytes);
if (ret < 0)
return ret;
comp_update_buffer_consume(cd->endpoint_buffer[i], src_copy_bytes);
}
} else {
src = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list);
for (i = 0; i < cd->endpoint_num; i++) {
ret = do_conversion_copy(dev, cd, src, cd->endpoint_buffer[i],
&src_copy_bytes);
if (ret < 0)
return ret;
ret = cd->endpoint[i]->drv->ops.copy(cd->endpoint[i]);
if (ret < 0)
return ret;
}
comp_update_buffer_consume(src, src_copy_bytes);
}
} else {
/* do format conversion */
struct comp_buffer *src;
struct comp_buffer *sink;
struct list_item *sink_list;
uint32_t src_bytes = 0;
src = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list);
/* do format conversion for each sink buffer */
list_for_item(sink_list, &dev->bsink_list) {
sink = container_of(sink_list, struct comp_buffer, source_list);
ret = do_conversion_copy(dev, cd, src, sink, &src_bytes);
if (ret < 0) {
comp_err(dev, "failed to copy buffer for comp %x",
dev->ipc_config.id);
return ret;
}
}
comp_update_buffer_consume(src, src_bytes);
ret = 0;
}
return ret;
}
static void update_internal_comp(struct comp_dev *parent, struct comp_dev *child)
{
child->period = parent->period;
child->pipeline = parent->pipeline;
child->priority = parent->priority;
child->direction = parent->direction;
}
/* configure the DMA params */
static int copier_params(struct comp_dev *dev, struct sof_ipc_stream_params *params)
{
struct copier_data *cd = comp_get_drvdata(dev);
union ipc4_connector_node_id node_id;
struct comp_buffer *sink;
struct comp_buffer *source;
struct list_item *sink_list;
int ret = 0;
int i;
comp_dbg(dev, "copier_params()");
memset(params, 0, sizeof(*params));
params->direction = cd->direction;
params->channels = cd->config.base.audio_fmt.channels_count;
params->rate = cd->config.base.audio_fmt.sampling_frequency;
params->sample_container_bytes = cd->config.base.audio_fmt.depth;
params->sample_valid_bytes = cd->config.base.audio_fmt.valid_bit_depth;
node_id.dw = cd->config.gtw_cfg.node_id;
params->stream_tag = node_id.f.v_index + 1;
params->frame_fmt = dev->ipc_config.frame_fmt;
params->buffer_fmt = cd->config.base.audio_fmt.interleaving_style;
params->buffer.size = cd->config.base.ibs;
/* disable ipc3 stream position */
params->no_stream_position = 1;
/* update each sink format */
list_for_item(sink_list, &dev->bsink_list) {
int j;
sink = container_of(sink_list, struct comp_buffer, source_list);
j = IPC4_SINK_QUEUE_ID(sink->id);
sink->stream.channels = cd->out_fmt[j].channels_count;
sink->stream.rate = cd->out_fmt[j].sampling_frequency;
audio_stream_fmt_conversion(cd->out_fmt[j].depth,
cd->out_fmt[j].valid_bit_depth,
&sink->stream.frame_fmt,
&sink->stream.valid_sample_fmt,
cd->out_fmt[j].s_type);
sink->buffer_fmt = cd->out_fmt[j].interleaving_style;
for (i = 0; i < SOF_IPC_MAX_CHANNELS; i++)
sink->chmap[i] = (cd->out_fmt[j].ch_map >> i * 4) & 0xf;
sink->hw_params_configured = true;
}
/* update the source format
* used only for rare cases where two pipelines are connected by a shared
* buffer and 2 copiers, this will set source format only for shared buffers
* for a short time when the second pipeline already started
* and the first one is not ready yet along with sink buffers params
*/
if (!list_is_empty(&dev->bsource_list)) {
source = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list);
if (!source->hw_params_configured) {
struct ipc4_audio_format in_fmt;
in_fmt = cd->config.base.audio_fmt;
source->stream.channels = in_fmt.channels_count;
source->stream.rate = in_fmt.sampling_frequency;
audio_stream_fmt_conversion(in_fmt.depth,
in_fmt.valid_bit_depth,
&source->stream.frame_fmt,
&source->stream.valid_sample_fmt,
in_fmt.s_type);
source->buffer_fmt = in_fmt.interleaving_style;
for (i = 0; i < SOF_IPC_MAX_CHANNELS; i++)
source->chmap[i] = (in_fmt.ch_map >> i * 4) & 0xf;
source->hw_params_configured = true;
}
}
if (cd->endpoint_num) {
for (i = 0; i < cd->endpoint_num; i++) {
update_internal_comp(dev, cd->endpoint[i]);
ret = cd->endpoint[i]->drv->ops.params(cd->endpoint[i], params);
if (ret < 0)
break;
}
}
return ret;
}
static int copier_set_sink_fmt(struct comp_dev *dev, void *data,
int max_data_size)
{
struct ipc4_copier_config_set_sink_format *sink_fmt;
struct copier_data *cd = comp_get_drvdata(dev);
sink_fmt = (struct ipc4_copier_config_set_sink_format *)data;
dcache_invalidate_region((__sparse_force void __sparse_cache *)sink_fmt, sizeof(*sink_fmt));
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));