/
handler.c
1683 lines (1427 loc) · 46.7 KB
/
handler.c
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 <liam.r.girdwood@linux.intel.com>
// Author: Rander Wang <rander.wang@linux.intel.com>
/*
* IPC (InterProcessor Communication) provides a method of two way
* communication between the host processor and the DSP. The IPC used here
* utilises a shared mailbox and door bell between the host and DSP.
*
*/
#include <sof/audio/buffer.h>
#include <sof/audio/component_ext.h>
#include <sof/audio/pipeline.h>
#include <sof/boot_test.h>
#include <sof/common.h>
#include <sof/ipc/topology.h>
#include <sof/ipc/common.h>
#include <sof/ipc/msg.h>
#include <sof/ipc/driver.h>
#include <sof/lib/mailbox.h>
#include <sof/lib/pm_runtime.h>
#include <sof/math/numbers.h>
#include <sof/tlv.h>
#include <sof/trace/trace.h>
#include <ipc4/error_status.h>
#include <ipc/header.h>
#include <ipc4/module.h>
#include <ipc4/pipeline.h>
#include <ipc4/notification.h>
#include <ipc/trace.h>
#include <user/trace.h>
#include <rtos/atomic.h>
#include <rtos/kernel.h>
#include <sof/trace/dma-trace.h>
#include <sof/lib_manager.h>
#if CONFIG_SOF_BOOT_TEST
/* CONFIG_SOF_BOOT_TEST depends on Zephyr */
#include <zephyr/ztest.h>
#endif
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "../audio/copier/ipcgtw_copier.h"
/* Command format errors during fuzzing are reported for virtually all
* commands, and the resulting flood of logging becomes a severe
* performance penalty (i.e. we get a lot less fuzzing done per CPU
* cycle).
*/
#ifdef CONFIG_ARCH_POSIX_LIBFUZZER
#define ipc_cmd_err(...)
#else
#define ipc_cmd_err(...) tr_err(__VA_ARGS__)
#endif
LOG_MODULE_DECLARE(ipc, CONFIG_SOF_LOG_LEVEL);
struct ipc4_msg_data {
struct ipc_cmd_hdr msg_in; /* local copy of current message from host header */
struct ipc_cmd_hdr msg_out; /* local copy of current message to host header */
atomic_t delayed_reply;
uint32_t delayed_error;
};
static struct ipc4_msg_data msg_data;
/* fw sends a fw ipc message to send the status of the last host ipc message */
static struct ipc_msg msg_reply = {0, 0, 0, 0, LIST_INIT(msg_reply.list)};
static struct ipc_msg msg_notify = {0, 0, 0, 0, LIST_INIT(msg_notify.list)};
#if CONFIG_LIBRARY
static inline struct ipc4_message_request *ipc4_get_message_request(void)
{
struct ipc *ipc = ipc_get();
return (struct ipc4_message_request *)ipc->comp_data;
}
static inline void ipc4_send_reply(struct ipc4_message_reply *reply)
{
struct ipc *ipc = ipc_get();
memcpy((char *)ipc->comp_data, reply, sizeof(*reply));
}
static inline const struct ipc4_pipeline_set_state_data *ipc4_get_pipeline_data(void)
{
const struct ipc4_pipeline_set_state_data *ppl_data;
struct ipc *ipc = ipc_get();
ppl_data = (const struct ipc4_pipeline_set_state_data *)ipc->comp_data;
return ppl_data;
}
#else
static inline struct ipc4_message_request *ipc4_get_message_request(void)
{
/* ignoring _hdr as it does not contain valid data in IPC4/IDC case */
return ipc_from_hdr(&msg_data.msg_in);
}
static inline void ipc4_send_reply(struct ipc4_message_reply *reply)
{
struct ipc *ipc = ipc_get();
char *data = ipc->comp_data;
ipc_msg_send(&msg_reply, data, true);
}
static inline const struct ipc4_pipeline_set_state_data *ipc4_get_pipeline_data(void)
{
const struct ipc4_pipeline_set_state_data *ppl_data;
ppl_data = (const struct ipc4_pipeline_set_state_data *)MAILBOX_HOSTBOX_BASE;
dcache_invalidate_region((__sparse_force void __sparse_cache *)ppl_data,
sizeof(*ppl_data));
return ppl_data;
}
#endif
/*
* Global IPC Operations.
*/
static int ipc4_new_pipeline(struct ipc4_message_request *ipc4)
{
struct ipc *ipc = ipc_get();
return ipc_pipeline_new(ipc, (ipc_pipe_new *)ipc4);
}
static int ipc4_delete_pipeline(struct ipc4_message_request *ipc4)
{
struct ipc4_pipeline_delete *pipe;
struct ipc *ipc = ipc_get();
pipe = (struct ipc4_pipeline_delete *)ipc4;
tr_dbg(&ipc_tr, "ipc4 delete pipeline %x:", (uint32_t)pipe->primary.r.instance_id);
return ipc_pipeline_free(ipc, pipe->primary.r.instance_id);
}
static int ipc4_comp_params(struct comp_dev *current,
struct comp_buffer *calling_buf,
struct pipeline_walk_context *ctx, int dir)
{
struct pipeline_data *ppl_data = ctx->comp_data;
int err;
/* don't do any params if current is running */
if (current->state == COMP_STATE_ACTIVE)
return 0;
/* Stay on the current pipeline */
if (current->pipeline != ((struct pipeline_data *)ctx->comp_data)->p)
return 0;
err = comp_params(current, &ppl_data->params->params);
if (err < 0 || err == PPL_STATUS_PATH_STOP)
return err;
return pipeline_for_each_comp(current, ctx, dir);
}
static int ipc4_pipeline_params(struct pipeline *p, struct comp_dev *host)
{
struct sof_ipc_pcm_params hw_params = {{ 0 }};
struct pipeline_data data = {
.start = host,
.params = &hw_params,
.p = p,
};
struct pipeline_walk_context param_ctx = {
.comp_func = ipc4_comp_params,
.comp_data = &data,
.skip_incomplete = true,
};
return param_ctx.comp_func(host, NULL, ¶m_ctx, host->direction);
}
static int ipc4_pcm_params(struct ipc_comp_dev *pcm_dev)
{
int err, reset_err;
/* sanity check comp */
if (!pcm_dev->cd->pipeline) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d pipeline not found", pcm_dev->id);
return -EINVAL;
}
/* configure pipeline audio params */
err = ipc4_pipeline_params(pcm_dev->cd->pipeline, pcm_dev->cd);
if (err < 0) {
ipc_cmd_err(&ipc_tr, "ipc: pipe %d comp %d params failed %d",
pcm_dev->cd->pipeline->pipeline_id,
pcm_dev->cd->pipeline->comp_id, err);
goto error;
}
/* prepare pipeline audio params */
err = pipeline_prepare(pcm_dev->cd->pipeline, pcm_dev->cd);
if (err < 0) {
ipc_cmd_err(&ipc_tr, "ipc: pipe %d comp %d prepare failed %d",
pcm_dev->cd->pipeline->pipeline_id,
pcm_dev->cd->pipeline->comp_id, err);
goto error;
}
return 0;
error:
reset_err = pipeline_reset(pcm_dev->cd->pipeline, pcm_dev->cd);
if (reset_err < 0)
ipc_cmd_err(&ipc_tr, "ipc: pipe %d comp %d reset failed %d",
pcm_dev->cd->pipeline->pipeline_id,
pcm_dev->cd->pipeline->comp_id, reset_err);
return err;
}
static bool is_any_ppl_active(void)
{
struct ipc_comp_dev *icd;
struct list_item *clist;
list_for_item(clist, &ipc_get()->comp_list) {
icd = container_of(clist, struct ipc_comp_dev, list);
if (icd->type != COMP_TYPE_PIPELINE)
continue;
if (icd->pipeline->status == COMP_STATE_ACTIVE)
return true;
}
return false;
}
static struct ipc_comp_dev *pipeline_get_host_dev(struct ipc_comp_dev *ppl_icd)
{
struct ipc_comp_dev *host_dev;
struct ipc *ipc = ipc_get();
int host_id;
/* If the source component's direction is not set but the sink's direction is,
* this block will copy the direction from the sink to the source component and
* mark the source's direction as set.
*/
if (!ppl_icd->pipeline->source_comp->direction_set &&
ppl_icd->pipeline->sink_comp->direction_set) {
ppl_icd->pipeline->source_comp->direction =
ppl_icd->pipeline->sink_comp->direction;
ppl_icd->pipeline->source_comp->direction_set = true;
}
/* If the sink component's direction is not set but the source's direction is,
* this block will copy the direction from the source to the sink component and
* mark the sink's direction as set.
*/
if (!ppl_icd->pipeline->sink_comp->direction_set &&
ppl_icd->pipeline->source_comp->direction_set) {
ppl_icd->pipeline->sink_comp->direction =
ppl_icd->pipeline->source_comp->direction;
ppl_icd->pipeline->sink_comp->direction_set = true;
}
if (ppl_icd->pipeline->source_comp->direction == SOF_IPC_STREAM_PLAYBACK)
host_id = ppl_icd->pipeline->source_comp->ipc_config.id;
else
host_id = ppl_icd->pipeline->sink_comp->ipc_config.id;
host_dev = ipc_get_comp_by_id(ipc, host_id);
if (!host_dev)
ipc_cmd_err(&ipc_tr, "comp host with ID %d not found", host_id);
return host_dev;
}
/* Ipc4 pipeline message <------> ipc3 pipeline message
* RUNNING <-------> TRIGGER START
* INIT + PAUSED <-------> PIPELINE COMPLETE
* INIT + RESET <-------> PIPELINE COMPLETE
* PAUSED <-------> TRIGGER_PAUSE
* RESET <-------> TRIGGER_STOP + RESET
* EOS(end of stream) <-------> NOT SUPPORTED YET
*
* IPC4 pipeline state machine
*
* INIT
* | \
* | __\|
* |
* | RESET
* | _ _
* | /| |\
* | / /\
* \|/ |/_ / \
* RUNNING <--> PAUSE _ / \
* / \ /|\ |\ / \
* / \ | \/ \
* / \ | /\ \
* / \ | / \ \
* |/_ _\| | / \ _\|
* ERROR Stop EOS |______\ SAVE
* /
*/
int ipc4_pipeline_prepare(struct ipc_comp_dev *ppl_icd, uint32_t cmd)
{
struct ipc_comp_dev *host = NULL;
struct ipc *ipc = ipc_get();
int status;
int ret = 0;
status = ppl_icd->pipeline->status;
tr_dbg(&ipc_tr, "pipeline %d: initial state: %d, cmd: %d", ppl_icd->id,
status, cmd);
switch (cmd) {
case SOF_IPC4_PIPELINE_STATE_RUNNING:
/* init params when pipeline is complete or reset */
switch (status) {
case COMP_STATE_ACTIVE:
case COMP_STATE_PAUSED:
/* No action needed */
break;
case COMP_STATE_READY:
host = pipeline_get_host_dev(ppl_icd);
if (!host)
return IPC4_INVALID_RESOURCE_ID;
tr_dbg(&ipc_tr, "pipeline %d: set params", ppl_icd->id);
ret = ipc4_pcm_params(host);
if (ret < 0)
return IPC4_INVALID_REQUEST;
break;
default:
ipc_cmd_err(&ipc_tr,
"pipeline %d: Invalid state for RUNNING: %d",
ppl_icd->id, status);
return IPC4_INVALID_REQUEST;
}
break;
case SOF_IPC4_PIPELINE_STATE_RESET:
switch (status) {
case COMP_STATE_INIT:
tr_dbg(&ipc_tr, "pipeline %d: reset from init", ppl_icd->id);
ret = ipc4_pipeline_complete(ipc, ppl_icd->id, cmd);
if (ret < 0)
ret = IPC4_INVALID_REQUEST;
break;
case COMP_STATE_READY:
case COMP_STATE_ACTIVE:
case COMP_STATE_PAUSED:
/* No action needed */
break;
default:
ipc_cmd_err(&ipc_tr,
"pipeline %d: Invalid state for RESET: %d",
ppl_icd->id, status);
return IPC4_INVALID_REQUEST;
}
break;
case SOF_IPC4_PIPELINE_STATE_PAUSED:
switch (status) {
case COMP_STATE_INIT:
tr_dbg(&ipc_tr, "pipeline %d: pause from init", ppl_icd->id);
ret = ipc4_pipeline_complete(ipc, ppl_icd->id, cmd);
if (ret < 0)
ret = IPC4_INVALID_REQUEST;
break;
default:
/* No action needed */
break;
}
break;
/* special case- TODO */
case SOF_IPC4_PIPELINE_STATE_EOS:
if (status != COMP_STATE_ACTIVE)
return IPC4_INVALID_REQUEST;
COMPILER_FALLTHROUGH;
case SOF_IPC4_PIPELINE_STATE_SAVED:
case SOF_IPC4_PIPELINE_STATE_ERROR_STOP:
default:
ipc_cmd_err(&ipc_tr, "pipeline %d: unsupported trigger cmd: %d",
ppl_icd->id, cmd);
return IPC4_INVALID_REQUEST;
}
return ret;
}
int ipc4_pipeline_trigger(struct ipc_comp_dev *ppl_icd, uint32_t cmd, bool *delayed)
{
struct ipc_comp_dev *host;
int status;
int ret;
status = ppl_icd->pipeline->status;
tr_dbg(&ipc_tr, "pipeline %d: initial state: %d, cmd: %d", ppl_icd->id,
status, cmd);
if (status == COMP_STATE_INIT)
return 0;
host = pipeline_get_host_dev(ppl_icd);
if (!host)
return IPC4_INVALID_RESOURCE_ID;
switch (cmd) {
case SOF_IPC4_PIPELINE_STATE_RUNNING:
/* init params when pipeline is complete or reset */
switch (status) {
case COMP_STATE_ACTIVE:
/* nothing to do if the pipeline is already running */
return 0;
case COMP_STATE_READY:
case COMP_STATE_PREPARE:
cmd = COMP_TRIGGER_PRE_START;
break;
case COMP_STATE_PAUSED:
cmd = COMP_TRIGGER_PRE_RELEASE;
break;
default:
ipc_cmd_err(&ipc_tr,
"pipeline %d: Invalid state for RUNNING: %d",
ppl_icd->id, status);
return IPC4_INVALID_REQUEST;
}
break;
case SOF_IPC4_PIPELINE_STATE_RESET:
switch (status) {
case COMP_STATE_ACTIVE:
case COMP_STATE_PAUSED:
cmd = COMP_TRIGGER_STOP;
break;
default:
return 0;
}
break;
case SOF_IPC4_PIPELINE_STATE_PAUSED:
switch (status) {
case COMP_STATE_INIT:
case COMP_STATE_READY:
case COMP_STATE_PAUSED:
return 0;
default:
cmd = COMP_TRIGGER_PAUSE;
break;
}
break;
default:
ipc_cmd_err(&ipc_tr, "pipeline %d: unsupported trigger cmd: %d",
ppl_icd->id, cmd);
return IPC4_INVALID_REQUEST;
}
/* trigger the component */
ret = pipeline_trigger(host->cd->pipeline, host->cd, cmd);
if (ret < 0) {
ipc_cmd_err(&ipc_tr, "pipeline %d: trigger cmd %d failed with: %d",
ppl_icd->id, cmd, ret);
ret = IPC4_PIPELINE_STATE_NOT_SET;
} else if (ret == PPL_STATUS_SCHEDULED) {
tr_dbg(&ipc_tr, "pipeline %d: trigger cmd %d is delayed",
ppl_icd->id, cmd);
*delayed = true;
ret = 0;
} else if (cmd == COMP_TRIGGER_STOP) {
/*
* reset the pipeline components if STOP trigger is executed in
* the same thread.
* Otherwise, the pipeline will be reset after the STOP trigger
* has finished executing in the pipeline task.
*/
ret = pipeline_reset(host->cd->pipeline, host->cd);
if (ret < 0)
ret = IPC4_INVALID_REQUEST;
}
return ret;
}
static void ipc_compound_pre_start(int msg_id)
{
/* ipc thread will wait for all scheduled tasks to be complete
* Use a reference count to check status of these tasks.
*/
atomic_add(&msg_data.delayed_reply, 1);
}
static void ipc_compound_post_start(uint32_t msg_id, int ret, bool delayed)
{
if (ret) {
ipc_cmd_err(&ipc_tr, "failed to process msg %d status %d", msg_id, ret);
atomic_set(&msg_data.delayed_reply, 0);
return;
}
/* decrease counter if it is not scheduled by another thread */
if (!delayed)
atomic_sub(&msg_data.delayed_reply, 1);
}
static void ipc_compound_msg_done(uint32_t msg_id, int error)
{
if (!atomic_read(&msg_data.delayed_reply)) {
ipc_cmd_err(&ipc_tr, "unexpected delayed reply");
return;
}
atomic_sub(&msg_data.delayed_reply, 1);
/* error reported in delayed pipeline task */
if (error < 0) {
if (msg_id == SOF_IPC4_GLB_SET_PIPELINE_STATE)
msg_data.delayed_error = IPC4_PIPELINE_STATE_NOT_SET;
}
}
static int ipc_wait_for_compound_msg(void)
{
int try_count = 30;
while (atomic_read(&msg_data.delayed_reply)) {
k_sleep(Z_TIMEOUT_US(250));
if (!try_count--) {
atomic_set(&msg_data.delayed_reply, 0);
ipc_cmd_err(&ipc_tr, "ipc4: failed to wait schedule thread");
return IPC4_FAILURE;
}
}
return IPC4_SUCCESS;
}
const struct ipc4_pipeline_set_state_data *ipc4_get_pipeline_data_wrapper(void)
{
const struct ipc4_pipeline_set_state_data *ppl_data;
ppl_data = ipc4_get_pipeline_data();
return ppl_data;
}
static int ipc4_set_pipeline_state(struct ipc4_message_request *ipc4)
{
const struct ipc4_pipeline_set_state_data *ppl_data;
struct ipc4_pipeline_set_state state;
struct ipc_comp_dev *ppl_icd;
struct ipc *ipc = ipc_get();
uint32_t cmd, ppl_count;
uint32_t id = 0;
const uint32_t *ppl_id;
bool use_idc = false;
uint32_t idx;
int ret = 0;
int i;
state.primary.dat = ipc4->primary.dat;
state.extension.dat = ipc4->extension.dat;
cmd = state.primary.r.ppl_state;
ppl_data = ipc4_get_pipeline_data();
if (state.extension.r.multi_ppl) {
ppl_count = ppl_data->pipelines_count;
ppl_id = ppl_data->ppl_id;
dcache_invalidate_region((__sparse_force void __sparse_cache *)ppl_id,
sizeof(int) * ppl_count);
} else {
ppl_count = 1;
id = state.primary.r.ppl_id;
ppl_id = &id;
}
for (i = 0; i < ppl_count; i++) {
ppl_icd = ipc_get_comp_by_ppl_id(ipc, COMP_TYPE_PIPELINE,
ppl_id[i], IPC_COMP_IGNORE_REMOTE);
if (!ppl_icd) {
tr_err(&ipc_tr, "ipc: comp %d not found", ppl_id[i]);
return IPC4_INVALID_RESOURCE_ID;
}
if (i) {
if (ppl_icd->core != idx)
use_idc = true;
} else {
idx = ppl_icd->core;
}
}
/* Run the prepare phase on the pipelines */
for (i = 0; i < ppl_count; i++) {
ppl_icd = ipc_get_comp_by_ppl_id(ipc, COMP_TYPE_PIPELINE,
ppl_id[i], IPC_COMP_IGNORE_REMOTE);
if (!ppl_icd) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d not found", ppl_id[i]);
return IPC4_INVALID_RESOURCE_ID;
}
/* Pass IPC to target core
* or use idc if more than one core used
*/
if (!cpu_is_me(ppl_icd->core)) {
if (use_idc) {
struct idc_msg msg = { IDC_MSG_PPL_STATE,
IDC_MSG_PPL_STATE_EXT(ppl_id[i],
IDC_PPL_STATE_PHASE_PREPARE),
ppl_icd->core,
sizeof(cmd), &cmd, };
ret = idc_send_msg(&msg, IDC_BLOCKING);
} else {
return ipc4_process_on_core(ppl_icd->core, false);
}
} else {
ret = ipc4_pipeline_prepare(ppl_icd, cmd);
}
if (ret != 0)
return ret;
}
/* Run the trigger phase on the pipelines */
for (i = 0; i < ppl_count; i++) {
bool delayed = false;
ppl_icd = ipc_get_comp_by_ppl_id(ipc, COMP_TYPE_PIPELINE,
ppl_id[i], IPC_COMP_IGNORE_REMOTE);
if (!ppl_icd) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d not found", ppl_id[i]);
return IPC4_INVALID_RESOURCE_ID;
}
/* Pass IPC to target core
* or use idc if more than one core used
*/
if (!cpu_is_me(ppl_icd->core)) {
if (use_idc) {
struct idc_msg msg = { IDC_MSG_PPL_STATE,
IDC_MSG_PPL_STATE_EXT(ppl_id[i],
IDC_PPL_STATE_PHASE_TRIGGER),
ppl_icd->core,
sizeof(cmd), &cmd, };
ret = idc_send_msg(&msg, IDC_BLOCKING);
} else {
return ipc4_process_on_core(ppl_icd->core, false);
}
} else {
ipc_compound_pre_start(state.primary.r.type);
ret = ipc4_pipeline_trigger(ppl_icd, cmd, &delayed);
ipc_compound_post_start(state.primary.r.type, ret, delayed);
if (delayed) {
/* To maintain pipeline order for triggers, we must
* do a blocking wait until trigger is processed.
* This will add a max delay of 'ppl_count' LL ticks
* to process the full trigger list.
*/
if (ipc_wait_for_compound_msg() != 0) {
ipc_cmd_err(&ipc_tr, "ipc4: fail with delayed trigger");
return IPC4_FAILURE;
}
}
}
if (ret != 0)
return ret;
}
return ret;
}
#if CONFIG_LIBRARY_MANAGER
static int ipc4_load_library(struct ipc4_message_request *ipc4)
{
struct ipc4_module_load_library library;
int ret;
library.header.dat = ipc4->primary.dat;
ret = lib_manager_load_library(library.header.r.dma_id, library.header.r.lib_id,
ipc4->primary.r.type);
if (ret != 0)
return (ret == -EINVAL) ? IPC4_ERROR_INVALID_PARAM : IPC4_FAILURE;
return IPC4_SUCCESS;
}
#endif
static int ipc4_process_chain_dma(struct ipc4_message_request *ipc4)
{
#if CONFIG_COMP_CHAIN_DMA
struct ipc_comp_dev *cdma_comp;
struct ipc *ipc = ipc_get();
struct ipc4_chain_dma cdma;
int comp_id;
int ret;
ret = memcpy_s(&cdma, sizeof(cdma), ipc4, sizeof(*ipc4));
if (ret < 0)
return IPC4_FAILURE;
comp_id = IPC4_COMP_ID(cdma.primary.r.host_dma_id + IPC4_MAX_MODULE_COUNT, 0);
cdma_comp = ipc_get_comp_by_id(ipc, comp_id);
if (!cdma_comp) {
/*
* Nothing to do when the chainDMA is not allocated and asked to
* be freed
*/
if (!cdma.primary.r.allocate && !cdma.primary.r.enable)
return IPC4_SUCCESS;
ret = ipc4_chain_manager_create(&cdma);
if (ret < 0)
return IPC4_FAILURE;
cdma_comp = ipc_get_comp_by_id(ipc, comp_id);
if (!cdma_comp) {
return IPC4_FAILURE;
}
ret = ipc4_chain_dma_state(cdma_comp->cd, &cdma);
if (ret < 0) {
comp_free(cdma_comp->cd);
return IPC4_FAILURE;
}
return IPC4_SUCCESS;
}
ret = ipc4_chain_dma_state(cdma_comp->cd, &cdma);
if (ret < 0)
return IPC4_INVALID_CHAIN_STATE_TRANSITION;
if (!cdma.primary.r.allocate && !cdma.primary.r.enable)
list_item_del(&cdma_comp->list);
return IPC4_SUCCESS;
#else
return IPC4_UNAVAILABLE;
#endif
}
static int ipc4_process_ipcgtw_cmd(struct ipc4_message_request *ipc4)
{
#if CONFIG_IPC4_GATEWAY
struct ipc *ipc = ipc_get();
uint32_t reply_size = 0;
int err;
/* NOTE: reply implementation is messy! First, reply payload is copied
* to ipc->comp_data buffer. Then, new buffer is allocated and assigned
* to msg_reply.tx_data. ipc_msg_send() copies payload from ipc->comp_data
* to msg_reply.tx_data. Then, ipc_prepare_to_send() copies payload from
* msg_reply.tx_data to memory window and frees msg_reply.tx_data. That is
* quite weird: seems one extra copying can be eliminated.
*/
err = copier_ipcgtw_process((const struct ipc4_ipcgtw_cmd *)ipc4, ipc->comp_data,
&reply_size);
/* reply size is returned in header extension dword */
msg_reply.extension = reply_size;
if (reply_size > 0) {
msg_reply.tx_data = rballoc(0, SOF_MEM_CAPS_RAM, reply_size);
if (msg_reply.tx_data) {
msg_reply.tx_size = reply_size;
} else {
ipc_cmd_err(&ipc_tr, "failed to allocate %u bytes for msg_reply.tx_data",
reply_size);
msg_reply.extension = 0;
return IPC4_OUT_OF_MEMORY;
}
}
return err < 0 ? IPC4_FAILURE : IPC4_SUCCESS;
#else
ipc_cmd_err(&ipc_tr, "CONFIG_IPC4_GATEWAY is disabled");
return IPC4_UNAVAILABLE;
#endif
}
static int ipc4_process_glb_message(struct ipc4_message_request *ipc4)
{
uint32_t type;
int ret;
type = ipc4->primary.r.type;
switch (type) {
case SOF_IPC4_GLB_BOOT_CONFIG:
case SOF_IPC4_GLB_ROM_CONTROL:
case SOF_IPC4_GLB_PERF_MEASUREMENTS_CMD:
case SOF_IPC4_GLB_LOAD_MULTIPLE_MODULES:
case SOF_IPC4_GLB_UNLOAD_MULTIPLE_MODULES:
ipc_cmd_err(&ipc_tr, "not implemented ipc message type %d", type);
ret = IPC4_UNAVAILABLE;
break;
case SOF_IPC4_GLB_CHAIN_DMA:
ret = ipc4_process_chain_dma(ipc4);
break;
/* pipeline settings */
case SOF_IPC4_GLB_CREATE_PIPELINE:
ret = ipc4_new_pipeline(ipc4);
break;
case SOF_IPC4_GLB_DELETE_PIPELINE:
ret = ipc4_delete_pipeline(ipc4);
break;
case SOF_IPC4_GLB_SET_PIPELINE_STATE:
ret = ipc4_set_pipeline_state(ipc4);
break;
case SOF_IPC4_GLB_GET_PIPELINE_STATE:
case SOF_IPC4_GLB_GET_PIPELINE_CONTEXT_SIZE:
case SOF_IPC4_GLB_SAVE_PIPELINE:
case SOF_IPC4_GLB_RESTORE_PIPELINE:
ipc_cmd_err(&ipc_tr, "not implemented ipc message type %d", type);
ret = IPC4_UNAVAILABLE;
break;
/* Loads library (using Code Load or HD/A Host Output DMA) */
#ifdef CONFIG_LIBRARY_MANAGER
case SOF_IPC4_GLB_LOAD_LIBRARY:
ret = ipc4_load_library(ipc4);
break;
case SOF_IPC4_GLB_LOAD_LIBRARY_PREPARE:
ret = ipc4_load_library(ipc4);
break;
#endif
case SOF_IPC4_GLB_INTERNAL_MESSAGE:
ipc_cmd_err(&ipc_tr, "not implemented ipc message type %d", type);
ret = IPC4_UNAVAILABLE;
break;
/* Notification (FW to SW driver) */
case SOF_IPC4_GLB_NOTIFICATION:
ipc_cmd_err(&ipc_tr, "not implemented ipc message type %d", type);
ret = IPC4_UNAVAILABLE;
break;
case SOF_IPC4_GLB_IPCGATEWAY_CMD:
ret = ipc4_process_ipcgtw_cmd(ipc4);
break;
default:
ipc_cmd_err(&ipc_tr, "unsupported ipc message type %d", type);
ret = IPC4_UNAVAILABLE;
break;
}
return ret;
}
/*
* Ipc4 Module message <------> ipc3 module message
* init module <-------> create component
* bind modules <-------> connect components
* module set_large_config <-------> component cmd
* delete module <-------> free component
*/
static int ipc4_init_module_instance(struct ipc4_message_request *ipc4)
{
struct ipc4_module_init_instance module_init;
struct comp_dev *dev;
/* we only need the common header here, all we have from the IPC */
int ret = memcpy_s(&module_init, sizeof(module_init), ipc4, sizeof(*ipc4));
if (ret < 0)
return IPC4_FAILURE;
tr_dbg(&ipc_tr,
"ipc4_init_module_instance %x : %x",
(uint32_t)module_init.primary.r.module_id,
(uint32_t)module_init.primary.r.instance_id);
/* Pass IPC to target core */
if (!cpu_is_me(module_init.extension.r.core_id))
return ipc4_process_on_core(module_init.extension.r.core_id, false);
dev = comp_new_ipc4(&module_init);
if (!dev) {
ipc_cmd_err(&ipc_tr, "error: failed to init module %x : %x",
(uint32_t)module_init.primary.r.module_id,
(uint32_t)module_init.primary.r.instance_id);
return IPC4_MOD_NOT_INITIALIZED;
}
return 0;
}
static int ipc4_bind_module_instance(struct ipc4_message_request *ipc4)
{
struct ipc4_module_bind_unbind bu;
struct ipc *ipc = ipc_get();
int ret = memcpy_s(&bu, sizeof(bu), ipc4, sizeof(*ipc4));
if (ret < 0)
return IPC4_FAILURE;
tr_dbg(&ipc_tr, "ipc4_bind_module_instance %x : %x with %x : %x",
(uint32_t)bu.primary.r.module_id, (uint32_t)bu.primary.r.instance_id,
(uint32_t)bu.extension.r.dst_module_id, (uint32_t)bu.extension.r.dst_instance_id);
return ipc_comp_connect(ipc, (ipc_pipe_comp_connect *)&bu);
}
static int ipc4_unbind_module_instance(struct ipc4_message_request *ipc4)
{
struct ipc4_module_bind_unbind bu;
struct ipc *ipc = ipc_get();
int ret = memcpy_s(&bu, sizeof(bu), ipc4, sizeof(*ipc4));
if (ret < 0)
return IPC4_FAILURE;
tr_dbg(&ipc_tr, "ipc4_unbind_module_instance %x : %x with %x : %x",
(uint32_t)bu.primary.r.module_id, (uint32_t)bu.primary.r.instance_id,
(uint32_t)bu.extension.r.dst_module_id, (uint32_t)bu.extension.r.dst_instance_id);
return ipc_comp_disconnect(ipc, (ipc_pipe_comp_connect *)&bu);
}
static int ipc4_get_vendor_config_module_instance(struct comp_dev *dev,
const struct comp_driver *drv,
bool init_block,
bool final_block,
uint32_t *data_off_size,
char *data_out,
const char *data_in)
{
const struct sof_tl * const input_tl = (struct sof_tl *)data_in;
int ret;
struct ipc4_vendor_error *error;
if (init_block && final_block) {
/* we use data_off_size as in/out,
* save value to new variable so it can be used as out size
*/
const int tl_count = *data_off_size / sizeof(struct sof_tl);
size_t produced_data = 0;
for (int i = 0; i < tl_count; i++) {
/* we go to next output tlv with each iteration */
uint32_t data_off_size_local;
struct sof_tlv *output_tlv = (struct sof_tlv *)(data_out + produced_data);
if (produced_data + input_tl[i].max_length > MAILBOX_DSPBOX_SIZE) {
tr_err(&ipc_tr, "error: response payload bigger than DSPBOX size");
return IPC4_FAILURE;
}
/* local size is in/out: max msg len goes in, msg len goes out */
data_off_size_local = input_tl[i].max_length;
ret = drv->ops.get_large_config(dev, input_tl[i].type,
true,
true,
&data_off_size_local, output_tlv->value);
if (ret) {
/* This is how the reference firmware handled error here. Currently
* no memory is allocated for output in case of error,
* so this may be obsolete.
*/
error = (struct ipc4_vendor_error *)data_out;
error->param_idx = input_tl[i].type;
error->err_code = IPC4_FAILURE;
*data_off_size = sizeof(*error);
ipc_cmd_err(&ipc_tr, "error: get_large_config returned %d", ret);
return IPC4_FAILURE;
}
/* update header */
output_tlv->type = input_tl[i].type;
output_tlv->length = data_off_size_local;
produced_data += data_off_size_local + sizeof(*output_tlv);
}
*data_off_size = produced_data;
} else {
char *output_buffer;
struct sof_tlv *tl_header;