-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathhelper.c
More file actions
1453 lines (1217 loc) · 42.6 KB
/
helper.c
File metadata and controls
1453 lines (1217 loc) · 42.6 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: Liam Girdwood <[email protected]>
// Author: Rander Wang <[email protected]>
#include <sof/audio/buffer.h>
#include <sof/audio/component.h>
#include <sof/audio/audio_buffer.h>
#include <sof/audio/ring_buffer.h>
#include <sof/audio/component_ext.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/pipeline.h>
#include <module/module/base.h>
#include <sof/common.h>
#include <rtos/interrupt.h>
#include <sof/ipc/topology.h>
#include <sof/ipc/common.h>
#include <ipc/dai.h>
#include <sof/ipc/msg.h>
#include <sof/lib/mailbox.h>
#include <sof/lib/memory.h>
#include <sof/list.h>
#include <sof/platform.h>
#include <sof/schedule/dp_schedule.h>
#include <sof/schedule/ll_schedule_domain.h>
#include <rtos/symbol.h>
#include <rtos/wait.h>
/* TODO: Remove platform-specific code, see https://github.com/thesofproject/sof/issues/7549 */
#if defined(CONFIG_SOC_SERIES_INTEL_ADSP_ACE) || defined(CONFIG_INTEL_ADSP_CAVS)
#define RIMAGE_MANIFEST 1
#endif
#ifdef RIMAGE_MANIFEST
#include <adsp_memory.h>
#endif
#include <rtos/sof.h>
#include <rtos/spinlock.h>
#include <rimage/cavs/cavs_ext_manifest.h>
#include <rimage/sof/user/manifest.h>
#include <ipc4/base-config.h>
#include <ipc/header.h>
#include <ipc4/notification.h>
#include <ipc4/pipeline.h>
#include <ipc4/module.h>
#include <ipc4/error_status.h>
#include <sof/lib_manager.h>
#include <sof/tlv.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <sof/debug/telemetry/telemetry.h>
#include <sof/debug/telemetry/performance_monitor.h>
LOG_MODULE_DECLARE(ipc, CONFIG_SOF_LOG_LEVEL);
extern struct tr_ctx comp_tr;
static const struct comp_driver *ipc4_get_drv(const void *uuid);
static int ipc4_add_comp_dev(struct comp_dev *dev);
void ipc_build_stream_posn(struct sof_ipc_stream_posn *posn, uint32_t type,
uint32_t id)
{
memset(posn, 0, sizeof(*posn));
}
void ipc_build_comp_event(struct sof_ipc_comp_event *event, uint32_t type,
uint32_t id)
{
}
bool ipc_trigger_trace_xfer(uint32_t avail)
{
return avail >= DMA_TRACE_LOCAL_SIZE / 2;
}
void ipc_build_trace_posn(struct sof_ipc_dma_trace_posn *posn)
{
posn->rhdr.hdr.cmd = SOF_IPC4_NOTIF_HEADER(SOF_IPC4_NOTIFY_LOG_BUFFER_STATUS);
posn->rhdr.hdr.size = 0;
}
#if CONFIG_LIBRARY
static inline char *ipc4_get_comp_new_data(void)
{
struct ipc *ipc = ipc_get();
char *data = (char *)ipc->comp_data + sizeof(struct ipc4_module_init_instance);
return data;
}
static const struct comp_driver *ipc4_library_get_comp_drv(char *data)
{
return ipc4_get_drv(data);
}
#else
__cold static inline char *ipc4_get_comp_new_data(void)
{
assert_can_be_cold();
return (char *)MAILBOX_HOSTBOX_BASE;
}
#endif
/* Only called from ipc4_init_module_instance(), which is __cold */
__cold struct comp_dev *comp_new_ipc4(struct ipc4_module_init_instance *module_init)
{
struct comp_ipc_config ipc_config;
const struct comp_driver *drv;
struct comp_dev *dev;
uint32_t comp_id;
char *data;
assert_can_be_cold();
comp_id = IPC4_COMP_ID(module_init->primary.r.module_id,
module_init->primary.r.instance_id);
if (ipc4_get_comp_dev(comp_id)) {
tr_err(&ipc_tr, "comp 0x%x exists", comp_id);
return NULL;
}
if (module_init->extension.r.core_id >= CONFIG_CORE_COUNT) {
tr_err(&ipc_tr, "ipc: comp->core = %u", (uint32_t)module_init->extension.r.core_id);
return NULL;
}
memset(&ipc_config, 0, sizeof(ipc_config));
ipc_config.id = comp_id;
ipc_config.pipeline_id = module_init->extension.r.ppl_instance_id;
ipc_config.core = module_init->extension.r.core_id;
ipc_config.ipc_config_size = module_init->extension.r.param_block_size * sizeof(uint32_t);
ipc_config.ipc_extended_init = module_init->extension.r.extended_init;
if (ipc_config.ipc_config_size > MAILBOX_HOSTBOX_SIZE) {
tr_err(&ipc_tr, "IPC payload size %u too big for the message window",
ipc_config.ipc_config_size);
return NULL;
}
#ifdef CONFIG_DCACHE_LINE_SIZE
if (!IS_ENABLED(CONFIG_LIBRARY))
sys_cache_data_invd_range((__sparse_force void __sparse_cache *)
MAILBOX_HOSTBOX_BASE,
ALIGN_UP(ipc_config.ipc_config_size,
CONFIG_DCACHE_LINE_SIZE));
#endif
data = ipc4_get_comp_new_data();
#if CONFIG_LIBRARY
ipc_config.ipc_config_size -= sizeof(struct sof_uuid);
drv = ipc4_library_get_comp_drv(data + ipc_config.ipc_config_size);
#else
drv = ipc4_get_comp_drv(IPC4_MOD_ID(comp_id));
#endif
if (!drv)
return NULL;
#if CONFIG_ZEPHYR_DP_SCHEDULER
if (module_init->extension.r.proc_domain)
ipc_config.proc_domain = COMP_PROCESSING_DOMAIN_DP;
else
ipc_config.proc_domain = COMP_PROCESSING_DOMAIN_LL;
#else /* CONFIG_ZEPHYR_DP_SCHEDULER */
if (module_init->extension.r.proc_domain) {
tr_err(&ipc_tr, "ipc: DP scheduling is disabled, cannot create comp 0x%x", comp_id);
return NULL;
}
ipc_config.proc_domain = COMP_PROCESSING_DOMAIN_LL;
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */
if (drv->type == SOF_COMP_MODULE_ADAPTER) {
const struct ipc_config_process spec = {
.data = (const unsigned char *)data,
.size = ipc_config.ipc_config_size,
};
dev = drv->ops.create(drv, &ipc_config, (const void *)&spec);
} else {
dev = drv->ops.create(drv, &ipc_config, (const void *)data);
}
if (!dev)
return NULL;
list_init(&dev->bsource_list);
list_init(&dev->bsink_list);
#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS
/* init global performance measurement */
dev->perf_data.perf_data_item = perf_data_getnext();
/* this can be null, just no performance measurements in this case */
if (dev->perf_data.perf_data_item) {
dev->perf_data.perf_data_item->item.resource_id = comp_id;
if (perf_meas_get_state() != IPC4_PERF_MEASUREMENTS_DISABLED)
comp_init_performance_data(dev);
}
#endif
ipc4_add_comp_dev(dev);
comp_update_ibs_obs_cpc(dev);
return dev;
}
/* Called from ipc4_set_pipeline_state(), so cannot be cold */
struct ipc_comp_dev *ipc_get_comp_by_ppl_id(struct ipc *ipc, uint16_t type,
uint32_t ppl_id,
uint32_t ignore_remote)
{
struct ipc_comp_dev *icd;
struct list_item *clist;
list_for_item(clist, &ipc->comp_list) {
icd = container_of(clist, struct ipc_comp_dev, list);
if (icd->type != type)
continue;
/* For IPC4, ipc_comp_dev.id field is equal to Pipeline ID
* in case of type COMP_TYPE_PIPELINE - can check directly here
*/
if (type == COMP_TYPE_PIPELINE) {
if (icd->id == ppl_id)
return icd;
} else {
if ((!cpu_is_me(icd->core)) && ignore_remote)
continue;
if (ipc_comp_pipe_id(icd) == ppl_id)
return icd;
}
}
return NULL;
}
/*
* This function currently only decodes the payload and prints out
* data it finds, but it does not store it anywhere.
*/
__cold static int ipc4_create_pipeline_payload_decode(char *data,
struct create_pipeline_params *pparams)
{
const struct ipc4_pipeline_ext_payload *hdr =
(struct ipc4_pipeline_ext_payload *)data;
const struct ipc4_pipeline_ext_object *obj;
#ifdef CONFIG_DCACHE_LINE_SIZE
size_t cache_line_size = CONFIG_DCACHE_LINE_SIZE;
size_t hdr_cache_size = ALIGN_UP(sizeof(*hdr), cache_line_size);
#endif
bool last_object;
size_t size;
#ifdef CONFIG_DCACHE_LINE_SIZE
if (!IS_ENABLED(CONFIG_LIBRARY))
sys_cache_data_invd_range((__sparse_force void __sparse_cache *)data,
hdr_cache_size);
#endif
size = hdr->payload_words * sizeof(uint32_t);
last_object = !hdr->data_obj_array;
if (size < sizeof(*hdr)) {
tr_err(&ipc_tr, "Payload size too small: %u : %#x", hdr->payload_words,
*((uint32_t *)hdr));
return -EINVAL;
}
if (size > MAILBOX_HOSTBOX_SIZE) {
tr_err(&ipc_tr, "Payload size too large: %u : %#x", hdr->payload_words,
*((uint32_t *)hdr));
}
tr_info(&ipc_tr, "payload size %u array %u: %#x", hdr->payload_words, hdr->data_obj_array,
*((uint32_t *)hdr));
#ifdef CONFIG_DCACHE_LINE_SIZE
if (!IS_ENABLED(CONFIG_LIBRARY) && ALIGN_UP(size, cache_line_size) > hdr_cache_size)
sys_cache_data_invd_range((__sparse_force void __sparse_cache *)
((char *)data + hdr_cache_size),
ALIGN_UP(size, cache_line_size) - hdr_cache_size);
#endif
obj = (const struct ipc4_pipeline_ext_object *)(hdr + 1);
while (!last_object) {
const struct ipc4_pipeline_ext_object *next_obj;
/* Check if there is space for the object header */
if ((char *)(obj + 1) - data > size) {
tr_err(&ipc_tr, "obj header overflow, %u > %zu",
(char *)(obj + 1) - data, size);
return -EINVAL;
}
/* Calculate would be next object position and check if current object fits */
next_obj = (const struct ipc4_pipeline_ext_object *)
(((const uint32_t *)(obj + 1)) + obj->object_words);
if ((char *)next_obj - data > size) {
tr_err(&ipc_tr, "object size overflow, %u > %zu",
(char *)next_obj - data, size);
return -EINVAL;
}
switch (obj->object_id) {
case IPC4_GLB_PIPE_EXT_OBJ_ID_MEM_DATA:
{
/* Get mem_data struct that follows the obj struct */
const struct ipc4_pipeline_ext_obj_mem_data *mem_data =
(const struct ipc4_pipeline_ext_obj_mem_data *)(obj + 1);
if (obj->object_words * sizeof(uint32_t) < sizeof(*mem_data)) {
tr_err(&ipc_tr, "mem_data object does not fit %zu < %zu",
obj->object_words * sizeof(uint32_t), sizeof(*mem_data));
break;
}
pparams->mem_data = mem_data;
tr_info(&ipc_tr,
"init_ext_obj_mem_data domain %u stack %u interim %u lifetime %u shared %u",
mem_data->domain_id, mem_data->stack_bytes,
mem_data->interim_heap_bytes, mem_data->lifetime_heap_bytes,
mem_data->shared_bytes);
break;
}
default:
tr_warn(&ipc_tr, "Unknown ext init object id %u of %u words",
obj->object_id, obj->object_words);
}
/* Read the last object flag from obj header */
last_object = obj->last_object;
/* Move to next object */
obj = next_obj;
}
return 0;
}
__cold static int ipc4_create_pipeline(struct ipc4_pipeline_create *pipe_desc,
struct create_pipeline_params *pparams)
{
struct ipc_comp_dev *ipc_pipe;
struct pipeline *pipe;
struct ipc *ipc = ipc_get();
assert_can_be_cold();
/* check whether pipeline id is already taken or in use */
ipc_pipe = ipc_get_pipeline_by_id(ipc, pipe_desc->primary.r.instance_id);
if (ipc_pipe) {
tr_err(&ipc_tr, "ipc: comp id is already taken, pipe_desc->instance_id = %u",
(uint32_t)pipe_desc->primary.r.instance_id);
return IPC4_INVALID_RESOURCE_ID;
}
/* create the pipeline */
pipe = pipeline_new(NULL, pipe_desc->primary.r.instance_id,
pipe_desc->primary.r.ppl_priority, 0, pparams);
if (!pipe) {
tr_err(&ipc_tr, "ipc: pipeline_new() failed");
return IPC4_OUT_OF_MEMORY;
}
pipe->time_domain = SOF_TIME_DOMAIN_TIMER;
pipe->period = LL_TIMER_PERIOD_US;
/* sched_id is set in FW so initialize it to a invalid value */
pipe->sched_id = 0xFFFFFFFF;
pipe->core = pipe_desc->extension.r.core_id;
/* allocate the IPC pipeline container */
ipc_pipe = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
sizeof(struct ipc_comp_dev));
if (!ipc_pipe) {
pipeline_free(pipe);
return IPC4_OUT_OF_MEMORY;
}
ipc_pipe->pipeline = pipe;
ipc_pipe->type = COMP_TYPE_PIPELINE;
ipc_pipe->id = pipe_desc->primary.r.instance_id;
ipc_pipe->core = pipe_desc->extension.r.core_id;
ipc_pipe->pipeline->attributes = pipe_desc->extension.r.attributes;
/* add new pipeline to the list */
list_item_append(&ipc_pipe->list, &ipc->comp_list);
return IPC4_SUCCESS;
}
#if CONFIG_LIBRARY
static inline char *ipc4_get_pipe_create_data(void)
{
struct ipc *ipc = ipc_get();
char *data = (char *)ipc->comp_data + sizeof(struct ipc4_pipeline_create);
return data;
}
#else
__cold static inline char *ipc4_get_pipe_create_data(void)
{
assert_can_be_cold();
return (char *)MAILBOX_HOSTBOX_BASE;
}
#endif
/* Only called from ipc4_new_pipeline(), which is __cold */
__cold int ipc_pipeline_new(struct ipc *ipc, ipc_pipe_new *_pipe_desc)
{
struct ipc4_pipeline_create *pipe_desc = ipc_from_pipe_new(_pipe_desc);
struct create_pipeline_params pparams = { 0 };
bool valid_pparams = false;
assert_can_be_cold();
tr_dbg(&ipc_tr, "ipc: pipeline id = %u", (uint32_t)pipe_desc->primary.r.instance_id);
/* pass IPC to target core */
if (!cpu_is_me(pipe_desc->extension.r.core_id))
return ipc4_process_on_core(pipe_desc->extension.r.core_id, false);
if (pipe_desc->extension.r.payload) {
char *data;
int ret;
data = ipc4_get_pipe_create_data();
ret = ipc4_create_pipeline_payload_decode(data, &pparams);
if (ret == 0)
valid_pparams = true;
}
return ipc4_create_pipeline(pipe_desc, valid_pparams ? &pparams : NULL);
}
__cold static inline int ipc_comp_free_remote(struct comp_dev *dev)
{
struct idc_msg msg = { IDC_MSG_FREE, IDC_MSG_FREE_EXT(dev->ipc_config.id),
dev->ipc_config.core,};
assert_can_be_cold();
return idc_send_msg(&msg, IDC_BLOCKING);
}
__cold static int ipc_pipeline_module_free(uint32_t pipeline_id)
{
struct ipc *ipc = ipc_get();
struct ipc_comp_dev *icd;
int ret;
assert_can_be_cold();
icd = ipc_get_comp_by_ppl_id(ipc, COMP_TYPE_COMPONENT, pipeline_id, IPC_COMP_ALL);
while (icd) {
struct comp_buffer *buffer;
struct comp_buffer *safe;
/* free sink buffer allocated by current component in bind function */
comp_dev_for_each_consumer_safe(icd->cd, buffer, safe) {
pipeline_disconnect(icd->cd, buffer, PPL_CONN_DIR_COMP_TO_BUFFER);
struct comp_dev *sink = comp_buffer_get_sink_component(buffer);
/* free the buffer only when the sink module has also been disconnected */
if (!sink)
buffer_free(buffer);
}
/* free source buffer allocated by current component in bind function */
comp_dev_for_each_producer_safe(icd->cd, buffer, safe) {
pipeline_disconnect(icd->cd, buffer, PPL_CONN_DIR_BUFFER_TO_COMP);
struct comp_dev *source = comp_buffer_get_source_component(buffer);
/* free the buffer only when the source module has also been disconnected */
if (!source)
buffer_free(buffer);
}
if (!cpu_is_me(icd->core))
ret = ipc_comp_free_remote(icd->cd);
else
ret = ipc_comp_free(ipc, icd->id);
if (ret)
return IPC4_INVALID_RESOURCE_STATE;
icd = ipc_get_comp_by_ppl_id(ipc, COMP_TYPE_COMPONENT, pipeline_id, IPC_COMP_ALL);
}
return IPC4_SUCCESS;
}
/* Only called from ipc4_delete_pipeline(), which is __cold */
__cold int ipc_pipeline_free(struct ipc *ipc, uint32_t comp_id)
{
struct ipc_comp_dev *ipc_pipe;
int ret;
assert_can_be_cold();
/* check whether pipeline exists */
ipc_pipe = ipc_get_pipeline_by_id(ipc, comp_id);
if (!ipc_pipe)
return IPC4_INVALID_RESOURCE_ID;
/* Pass IPC to target core */
if (!cpu_is_me(ipc_pipe->core))
return ipc4_process_on_core(ipc_pipe->core, false);
ret = ipc_pipeline_module_free(ipc_pipe->pipeline->pipeline_id);
if (ret != IPC4_SUCCESS) {
tr_err(&ipc_tr, "module free () failed");
return ret;
}
/* free buffer, delete all tasks and remove from list */
ret = pipeline_free(ipc_pipe->pipeline);
if (ret < 0) {
tr_err(&ipc_tr, "pipeline_free() failed");
return IPC4_INVALID_RESOURCE_STATE;
}
ipc_pipe->pipeline = NULL;
list_item_del(&ipc_pipe->list);
rfree(ipc_pipe);
return IPC4_SUCCESS;
}
__cold static struct comp_buffer *ipc4_create_buffer(struct comp_dev *src, bool is_shared,
uint32_t buf_size, uint32_t src_queue,
uint32_t dst_queue, struct k_heap *heap)
{
struct sof_ipc_buffer ipc_buf;
assert_can_be_cold();
memset(&ipc_buf, 0, sizeof(ipc_buf));
ipc_buf.size = buf_size;
ipc_buf.comp.id = IPC4_COMP_ID(src_queue, dst_queue);
ipc_buf.comp.pipeline_id = src->ipc_config.pipeline_id;
ipc_buf.comp.core = cpu_get_id();
return buffer_new(heap, &ipc_buf, is_shared);
}
#if CONFIG_CROSS_CORE_STREAM
/*
* Disabling interrupts to block next LL cycle works much faster comparing using
* of condition variable and mutex. Since same core binding is the most typical
* case, let's use slower cond_var blocking mechanism only for not so typical
* cross-core binding.
*
* Note, disabling interrupts to block LL for cross-core binding case will not work
* as .bind() handlers are called on corresponding cores using IDC tasks. IDC requires
* interrupts to be enabled. Only disabling timer interrupt instead of all interrupts
* might work. However, as CPU could go to some power down mode while waiting for
* blocking IDC call response, it's not clear how safe is to assume CPU can wakeup
* without timer interrupt. It depends on blocking IDC waiting implementation. That
* is why additional cond_var mechanism to block LL was introduced which does not
* disable any interrupts.
*/
#define ll_block(cross_core_bind, flags) \
do { \
if (cross_core_bind) \
domain_block(sof_get()->platform_timer_domain); \
else \
irq_local_disable(flags); \
} while (0)
#define ll_unblock(cross_core_bind, flags) \
do { \
if (cross_core_bind) \
domain_unblock(sof_get()->platform_timer_domain); \
else \
irq_local_enable(flags); \
} while (0)
/* Calling both ll_block() and ll_wait_finished_on_core() makes sure LL will not start its
* next cycle and its current cycle on specified core has finished.
*/
static int ll_wait_finished_on_core(struct comp_dev *dev)
{
/* To make sure (blocked) LL has finished its current cycle, it is
* enough to send any blocking IDC to the core. Since IDC task has lower
* priority then LL thread and cannot preempt it, execution of IDC task
* happens when LL thread is not active waiting for its next cycle.
*/
int ret;
struct ipc4_base_module_cfg dummy;
if (cpu_is_me(dev->ipc_config.core))
return 0;
/* Any blocking IDC that does not change component state could be utilized */
ret = comp_ipc4_get_attribute_remote(dev, COMP_ATTR_BASE_CONFIG, &dummy);
if (ret < 0) {
tr_err(&ipc_tr, "comp_ipc4_get_attribute_remote() failed for module %#x",
dev_comp_id(dev));
return ret;
}
return 0;
}
#else
#define ll_block(cross_core_bind, flags) irq_local_disable(flags)
#define ll_unblock(cross_core_bind, flags) irq_local_enable(flags)
#endif
/* Only called from ipc4_bind_module_instance(), which is __cold */
__cold int ipc_comp_connect(struct ipc *ipc, ipc_pipe_comp_connect *_connect)
{
struct ipc4_module_bind_unbind *bu;
struct bind_info bind_data;
struct comp_buffer *buffer;
struct comp_dev *source;
struct comp_dev *sink;
struct ipc4_base_module_cfg source_src_cfg;
struct ipc4_base_module_cfg sink_src_cfg;
uint32_t flags = 0;
uint32_t ibs = 0;
uint32_t obs = 0;
uint32_t buf_size;
int src_id, sink_id;
int ret;
assert_can_be_cold();
bu = (struct ipc4_module_bind_unbind *)_connect;
src_id = IPC4_COMP_ID(bu->primary.r.module_id, bu->primary.r.instance_id);
sink_id = IPC4_COMP_ID(bu->extension.r.dst_module_id, bu->extension.r.dst_instance_id);
source = ipc4_get_comp_dev(src_id);
sink = ipc4_get_comp_dev(sink_id);
if (!source || !sink) {
tr_err(&ipc_tr, "failed to find src %x, or dst %x", src_id, sink_id);
return IPC4_INVALID_RESOURCE_ID;
}
struct k_heap *dp_heap;
#if CONFIG_ZEPHYR_DP_SCHEDULER
bool src_is_dp = source->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP;
bool sink_is_dp = sink->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP;
bool dp_to_dp = src_is_dp && sink_is_dp;
struct comp_dev *dp;
if (sink_is_dp)
dp = sink;
else if (src_is_dp)
dp = source;
else
dp = NULL;
dp_heap = dp && dp->mod ? dp->mod->priv.resources.heap : NULL;
#else
dp_heap = NULL;
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */
bool cross_core_bind = source->ipc_config.core != sink->ipc_config.core;
/* If both components are on same core -- process IPC on that core,
* otherwise stay on core 0
*/
if (!cpu_is_me(source->ipc_config.core) && !cross_core_bind)
return ipc4_process_on_core(source->ipc_config.core, false);
if (source->drv->type == SOF_COMP_MODULE_ADAPTER) {
struct processing_module *srcmod = comp_mod(source);
struct module_config *srccfg = &srcmod->priv.cfg;
/* get obs from the base config extension if the src queue ID is non-zero */
if (bu->extension.r.src_queue && bu->extension.r.src_queue < srccfg->nb_output_pins)
obs = srccfg->output_pins[bu->extension.r.src_queue].obs;
}
/* get obs from base config if src queue ID is 0 or if base config extn is missing */
if (!obs) {
/* these might call comp_ipc4_get_attribute_remote() if necessary */
ret = comp_get_attribute(source, COMP_ATTR_BASE_CONFIG, &source_src_cfg);
if (ret < 0) {
tr_err(&ipc_tr, "failed to get base config for src module %#x",
dev_comp_id(source));
return IPC4_FAILURE;
}
obs = source_src_cfg.obs;
}
if (sink->drv->type == SOF_COMP_MODULE_ADAPTER) {
struct processing_module *dstmod = comp_mod(sink);
struct module_config *dstcfg = &dstmod->priv.cfg;
/* get ibs from the base config extension if the sink queue ID is non-zero */
if (bu->extension.r.dst_queue && bu->extension.r.dst_queue < dstcfg->nb_input_pins)
ibs = dstcfg->input_pins[bu->extension.r.dst_queue].ibs;
}
/* get ibs from base config if sink queue ID is 0 or if base config extn is missing */
if (!ibs) {
ret = comp_get_attribute(sink, COMP_ATTR_BASE_CONFIG, &sink_src_cfg);
if (ret < 0) {
tr_err(&ipc_tr, "failed to get base config for sink module %#x",
dev_comp_id(sink));
return IPC4_FAILURE;
}
ibs = sink_src_cfg.ibs;
}
/* create a buffer
* in case of LL -> LL or LL->DP
* The ibs / obs should be equal between components. However, some modules
* (like kpb) produce different data sizes on output pins. Unfortunately, only
* one ibs/obs can be specified in the modules configuration. To avoid creating
* too small a buffer, we choose the maximum value between ibs and obs.
*
* size = 2*max(obs of source module, ibs of destination module)
* (obs and ibs is single buffer size)
* in case of DP -> LL or DP -> DP
* size = 2*ibs of destination module. DP queue will handle obs of DP module
*/
if (source->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_LL)
buf_size = MAX(ibs, obs) * 2;
else
buf_size = ibs * 2;
buffer = ipc4_create_buffer(source, cross_core_bind, buf_size, bu->extension.r.src_queue,
bu->extension.r.dst_queue, dp_heap);
if (!buffer) {
tr_err(&ipc_tr, "failed to allocate buffer to bind %#x to %#x", src_id, sink_id);
return IPC4_OUT_OF_MEMORY;
}
#if CONFIG_ZEPHYR_DP_SCHEDULER
if (dp_heap) {
struct dp_heap_user *dp_user = container_of(dp_heap, struct dp_heap_user, heap);
dp_user->client_count++;
}
#endif
/*
* set min_free_space and min_available in sink/src api of created buffer.
* buffer is connected like:
* source_module -> (sink_ifc) BUFFER (source_ifc) -> sink_module
*
* source_module needs to set its OBS (out buffer size)
* as min_free_space in buffer's sink ifc
* sink_module needs to set its IBS (input buffer size)
* as min_available in buffer's source ifc
*/
sink_set_min_free_space(audio_buffer_get_sink(&buffer->audio_buffer), obs);
source_set_min_available(audio_buffer_get_source(&buffer->audio_buffer), ibs);
#if CONFIG_ZEPHYR_DP_SCHEDULER
struct ring_buffer *ring_buffer = NULL;
if (src_is_dp || sink_is_dp) {
struct processing_module *srcmod = comp_mod(source);
struct module_data *src_module_data = &srcmod->priv;
struct processing_module *dstmod = comp_mod(sink);
struct module_data *dst_module_data = &dstmod->priv;
bool is_shared = audio_buffer_is_shared(&buffer->audio_buffer);
uint32_t buf_id = buf_get_id(buffer);
/*
* Handle cases where the size of the ring buffer depends on the
* in_buff_size/out_buff_size advertised by the module. E.g. in the case of the
* compressed decoder module, the in/out_buff_size is determined during module
* init based on the decoder implementation. Also note that the size passed here
* is only for the ring buffer. The size of intermediate buffer created above is
* unchanged.
*/
ring_buffer = ring_buffer_create(dp, MAX(ibs, dst_module_data->mpd.in_buff_size),
MAX(obs, src_module_data->mpd.out_buff_size),
is_shared, buf_id);
if (!ring_buffer) {
buffer_free(buffer);
return IPC4_OUT_OF_MEMORY;
}
/* track ring_buffer as a client of the DP heap */
if (dp_heap) {
struct dp_heap_user *dp_rb_user =
container_of(dp_heap, struct dp_heap_user, heap);
dp_rb_user->client_count++;
}
/* data destination module needs to use ring_buffer */
audio_buffer_attach_secondary_buffer(&buffer->audio_buffer, dp == source,
&ring_buffer->audio_buffer);
/*
* DP-to-DP binding: both source and sink are DP modules.
* A second ring_buffer is needed on the other side of the comp_buffer
* so each DP module has its own lock-free ring_buffer interface.
* Data flows: src_DP → ring_buf_src → comp_buffer → ring_buf_sink → sink_DP
* The comp_buffer acts as the intermediary synced during LL cycles.
*/
if (dp_to_dp) {
struct ring_buffer *ring_buffer2;
struct k_heap *src_heap = source->mod->priv.resources.heap;
ring_buffer2 =
ring_buffer_create(source,
MAX(ibs, dst_module_data->mpd.in_buff_size),
MAX(obs, src_module_data->mpd.out_buff_size),
is_shared, buf_id);
if (!ring_buffer2) {
buffer_free(buffer);
return IPC4_OUT_OF_MEMORY;
}
/* track ring_buffer2 on source's DP heap */
if (src_heap) {
struct dp_heap_user *src_dp_user =
container_of(src_heap, struct dp_heap_user, heap);
src_dp_user->client_count++;
}
/* attach second ring_buffer on the source side */
audio_buffer_attach_secondary_buffer(&buffer->audio_buffer, dp != source,
&ring_buffer2->audio_buffer);
}
}
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */
/*
* Connect and bind the buffer to both source and sink components with LL processing been
* blocked on corresponding core(s) to prevent IPC or IDC task getting preempted which
* could result in buffers being only half connected when a pipeline task gets executed.
*/
ll_block(cross_core_bind, flags);
if (cross_core_bind) {
#if CONFIG_CROSS_CORE_STREAM
/* Make sure LL has finished on both cores */
if (!cpu_is_me(source->ipc_config.core))
if (ll_wait_finished_on_core(source) < 0)
goto free;
if (!cpu_is_me(sink->ipc_config.core))
if (ll_wait_finished_on_core(sink) < 0)
goto free;
#else
tr_err(&ipc_tr, "Cross-core binding is disabled");
goto free;
#endif
}
ret = comp_buffer_connect(source, source->ipc_config.core, buffer,
PPL_CONN_DIR_COMP_TO_BUFFER);
if (ret < 0) {
tr_err(&ipc_tr, "failed to connect src %#x to internal buffer", src_id);
goto free;
}
ret = comp_buffer_connect(sink, sink->ipc_config.core, buffer,
PPL_CONN_DIR_BUFFER_TO_COMP);
if (ret < 0) {
tr_err(&ipc_tr, "failed to connect internal buffer to sink %#x", sink_id);
goto e_sink_connect;
}
/* these might call comp_ipc4_bind_remote() if necessary */
bind_data.ipc4_data = bu;
bind_data.bind_type = COMP_BIND_TYPE_SINK;
bind_data.sink = audio_buffer_get_sink(&buffer->audio_buffer);
ret = comp_bind(source, &bind_data);
if (ret < 0)
goto e_src_bind;
bind_data.bind_type = COMP_BIND_TYPE_SOURCE;
bind_data.source = audio_buffer_get_source(&buffer->audio_buffer);
ret = comp_bind(sink, &bind_data);
if (ret < 0)
goto e_sink_bind;
/* update direction for sink component if it is not set already */
if (!sink->direction_set && source->direction_set) {
sink->direction = source->direction;
sink->direction_set = true;
}
/* update direction for source component if it is not set already */
if (!source->direction_set && sink->direction_set) {
source->direction = sink->direction;
source->direction_set = true;
}
ll_unblock(cross_core_bind, flags);
return IPC4_SUCCESS;
e_sink_bind:
/* sink bind was already called */
bind_data.bind_type = COMP_BIND_TYPE_SINK;
bind_data.sink = audio_buffer_get_sink(&buffer->audio_buffer);
comp_unbind(source, &bind_data);
e_src_bind:
pipeline_disconnect(sink, buffer, PPL_CONN_DIR_BUFFER_TO_COMP);
e_sink_connect:
pipeline_disconnect(source, buffer, PPL_CONN_DIR_COMP_TO_BUFFER);
free:
ll_unblock(cross_core_bind, flags);
buffer_free(buffer);
return IPC4_INVALID_RESOURCE_STATE;
}
/* when both module instances are part of the same pipeline Unbind IPC would
* be ignored since FW does not support changing internal topology of pipeline
* during run-time. The only way to change pipeline topology is to delete the whole
* pipeline and create it in modified form.
*/
/* Only called from ipc4_unbind_module_instance(), which is __cold */
__cold int ipc_comp_disconnect(struct ipc *ipc, ipc_pipe_comp_connect *_connect)
{
struct ipc4_module_bind_unbind *bu;
struct comp_buffer *buffer = NULL;
struct comp_buffer *buf;
struct comp_dev *src, *sink;
uint32_t src_id, sink_id, buffer_id;
uint32_t flags = 0;
int ret, ret1;
bool cross_core_unbind;
struct bind_info unbind_data;
assert_can_be_cold();
bu = (struct ipc4_module_bind_unbind *)_connect;
src_id = IPC4_COMP_ID(bu->primary.r.module_id, bu->primary.r.instance_id);
sink_id = IPC4_COMP_ID(bu->extension.r.dst_module_id, bu->extension.r.dst_instance_id);
src = ipc4_get_comp_dev(src_id);
sink = ipc4_get_comp_dev(sink_id);
if (!src || !sink) {
tr_err(&ipc_tr, "failed to find src %x, or dst %x", src_id, sink_id);
return IPC4_INVALID_RESOURCE_ID;
}
cross_core_unbind = src->ipc_config.core != sink->ipc_config.core;
/* Pass IPC to target core if both modules has the same target core,
* otherwise stay on core 0
*/
if (!cpu_is_me(src->ipc_config.core) && !cross_core_unbind)
return ipc4_process_on_core(src->ipc_config.core, false);
buffer_id = IPC4_COMP_ID(bu->extension.r.src_queue, bu->extension.r.dst_queue);
comp_dev_for_each_consumer(src, buf) {
bool found = buf_get_id(buf) == buffer_id;
if (found) {
buffer = buf;
break;
}
}
if (!buffer)
return IPC4_INVALID_RESOURCE_ID;
/*
* Disconnect and unbind buffer from source/sink components and continue to free the buffer
* even in case of errors. Block LL processing during disconnect and unbinding to prevent
* IPC or IDC task getting preempted which could result in buffers being only half connected
* when a pipeline task gets executed.
*/
ll_block(cross_core_unbind, flags);
if (cross_core_unbind) {
#if CONFIG_CROSS_CORE_STREAM
/* Make sure LL has finished on both cores */
if (!cpu_is_me(src->ipc_config.core))
if (ll_wait_finished_on_core(src) < 0) {
ll_unblock(cross_core_unbind, flags);
return IPC4_FAILURE;
}
if (!cpu_is_me(sink->ipc_config.core))
if (ll_wait_finished_on_core(sink) < 0) {
ll_unblock(cross_core_unbind, flags);
return IPC4_FAILURE;
}
#else
tr_err(&ipc_tr, "Cross-core binding is disabled");
ll_unblock(cross_core_unbind, flags);
return IPC4_FAILURE;
#endif
}
pipeline_disconnect(src, buffer, PPL_CONN_DIR_COMP_TO_BUFFER);
pipeline_disconnect(sink, buffer, PPL_CONN_DIR_BUFFER_TO_COMP);
/* these might call comp_ipc4_bind_remote() if necessary */
unbind_data.ipc4_data = bu;
unbind_data.bind_type = COMP_BIND_TYPE_SINK;
unbind_data.sink = audio_buffer_get_sink(&buffer->audio_buffer);
ret = comp_unbind(src, &unbind_data);
unbind_data.bind_type = COMP_BIND_TYPE_SOURCE;
unbind_data.source = audio_buffer_get_source(&buffer->audio_buffer);