forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.c
More file actions
1634 lines (1357 loc) · 41.1 KB
/
handler.c
File metadata and controls
1634 lines (1357 loc) · 41.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
// Keyon Jie <yang.jie@linux.intel.com>
/*
* 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/common.h>
#include <sof/debug/gdb/gdb.h>
#include <rtos/panic.h>
#include <rtos/idc.h>
#include <rtos/interrupt.h>
#include <sof/ipc/common.h>
#include <sof/ipc/msg.h>
#include <sof/ipc/driver.h>
#include <sof/ipc/schedule.h>
#include <sof/lib/agent.h>
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <sof/lib/mailbox.h>
#include <sof/lib/mm_heap.h>
#include <sof/lib/pm_runtime.h>
#include <sof/list.h>
#include <sof/platform.h>
#include <rtos/string.h>
#include <rtos/string_macro.h>
#if CONFIG_TRACE
#include <sof/trace/dma-trace.h>
#endif
#include <sof/trace/trace.h>
#include <ipc/control.h>
#include <ipc/dai.h>
#include <ipc/debug.h>
#include <ipc/header.h>
#include <ipc/pm.h>
#include <ipc/stream.h>
#include <ipc/topology.h>
#include <ipc/trace.h>
#include <user/trace.h>
#include <ipc/probe.h>
#include <sof/probe/probe.h>
#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.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);
#if CONFIG_CAVS
#include <ipc/header-intel-cavs.h>
#include <cavs/drivers/sideband-ipc.h>
#define CAVS_IPC_TYPE_S(x) ((x) & CAVS_IPC_TYPE_MASK)
#endif
#define iGS(x) ((x) & SOF_GLB_TYPE_MASK)
#define iCS(x) ((x) & SOF_CMD_TYPE_MASK)
/* FIXME: assert() is most likely turned off in production builds
* https://open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm
*/
#define _IPC_COPY_CMD(rx, tx, rx_size) \
do { \
int ___ret; \
if (rx_size > tx->size) { \
___ret = memcpy_s(rx, rx_size, tx, tx->size); \
assert(!___ret); \
bzero((char *)rx + tx->size, rx_size - tx->size);\
tr_dbg(&ipc_tr, "ipc: hdr 0x%x rx (%zu) > tx (%d)",\
rx->cmd, rx_size, tx->size); \
} else if (tx->size > rx_size) { \
___ret = memcpy_s(rx, rx_size, tx, rx_size); \
assert(!___ret); \
tr_warn(&ipc_tr, "ipc: hdr 0x%x tx (%d) > rx (%zu)",\
rx->cmd, tx->size, rx_size); \
} else { \
___ret = memcpy_s(rx, rx_size, tx, rx_size); \
assert(!___ret); \
} \
} while (0)
/* copies whole message from Tx to Rx, follows above ABI rules */
#define IPC_COPY_CMD(rx, tx) \
_IPC_COPY_CMD(((struct sof_ipc_cmd_hdr *)&rx), \
((struct sof_ipc_cmd_hdr *)tx), \
sizeof(rx))
struct ipc_cmd_hdr *mailbox_validate(void)
{
struct sof_ipc_cmd_hdr *hdr = ipc_get()->comp_data;
/* read component values from the inbox */
mailbox_hostbox_read(hdr, SOF_IPC_MSG_MAX_SIZE, 0, sizeof(*hdr));
/* validate component header */
if (hdr->size < sizeof(*hdr) || hdr->size > SOF_IPC_MSG_MAX_SIZE) {
#ifndef CONFIG_ARCH_POSIX_LIBFUZZER
ipc_cmd_err(&ipc_tr, "ipc: invalid size 0x%x", hdr->size);
#endif
return NULL;
}
/* read rest of component data */
mailbox_hostbox_read(hdr + 1, SOF_IPC_MSG_MAX_SIZE - sizeof(*hdr),
sizeof(*hdr), hdr->size - sizeof(*hdr));
return ipc_to_hdr(hdr);
}
/*
* Stream IPC Operations.
*/
#if CONFIG_HOST_PTABLE
/* check if a pipeline is hostless when walking downstream */
static bool is_hostless_downstream(struct comp_dev *current)
{
struct comp_buffer *buffer;
/* check if current is a HOST comp */
if (current->ipc_config.type == SOF_COMP_HOST ||
current->ipc_config.type == SOF_COMP_SG_HOST)
return false;
/* check if the pipeline has a HOST comp downstream */
comp_dev_for_each_consumer(current, buffer) {
/* don't go downstream if this component is not connected */
if (!comp_buffer_get_sink_component(buffer))
continue;
/* dont go downstream if this comp belongs to another pipe */
if (comp_buffer_get_sink_component(buffer)->ipc_config.pipeline_id !=
current->ipc_config.pipeline_id)
continue;
/* return if there's a host comp downstream */
if (!is_hostless_downstream(comp_buffer_get_sink_component(buffer)))
return false;
}
return true;
}
/* check if a pipeline is hostless when walking upstream */
static bool is_hostless_upstream(struct comp_dev *current)
{
struct comp_buffer *buffer;
/* check if current is a HOST comp */
if (current->ipc_config.type == SOF_COMP_HOST ||
current->ipc_config.type == SOF_COMP_SG_HOST)
return false;
/* check if the pipeline has a HOST comp upstream */
comp_dev_for_each_producer(current, buffer) {
/* don't go upstream if this component is not connected */
if (!comp_buffer_get_source_component(buffer))
continue;
/* dont go upstream if this comp belongs to another pipeline */
if (comp_buffer_get_source_component(buffer)->ipc_config.pipeline_id !=
current->ipc_config.pipeline_id)
continue;
/* return if there is a host comp upstream */
if (!is_hostless_upstream(comp_buffer_get_source_component(buffer)))
return false;
}
return true;
}
#endif
/* allocate a new stream */
static int ipc_stream_pcm_params(uint32_t stream)
{
#if CONFIG_HOST_PTABLE
struct dma_sg_elem_array elem_array;
uint32_t ring_size;
enum comp_copy_type copy_type = COMP_COPY_ONE_SHOT;
struct comp_dev *cd;
uint32_t direction;
#endif
struct ipc *ipc = ipc_get();
struct sof_ipc_pcm_params pcm_params;
struct sof_ipc_pcm_params_reply reply;
struct ipc_comp_dev *pcm_dev;
struct sof_ipc_stream_posn posn;
int err, reset_err;
/* copy message with ABI safe method */
IPC_COPY_CMD(pcm_params, ipc->comp_data);
/* get the pcm_dev */
pcm_dev = ipc_get_comp_by_id(ipc, pcm_params.comp_id);
if (!pcm_dev) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d not found", pcm_params.comp_id);
return -ENODEV;
}
/* check core */
if (!cpu_is_me(pcm_dev->core))
return ipc_process_on_core(pcm_dev->core, false);
tr_dbg(&ipc_tr, "ipc: comp %d -> params", pcm_params.comp_id);
/* sanity check comp */
if (!pcm_dev->cd->pipeline) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d pipeline not found",
pcm_params.comp_id);
return -EINVAL;
}
/* sanity check for pcm_params size */
if (pcm_params.hdr.size !=
sizeof(pcm_params) + pcm_params.params.ext_data_length) {
ipc_cmd_err(&ipc_tr, "pcm_params invalid size, hdr.size=%d, ext_data_len=%d",
pcm_params.hdr.size, pcm_params.params.ext_data_length);
return -EINVAL;
}
/* sanity check for pcm_params.params size */
if (pcm_params.params.hdr.size !=
sizeof(pcm_params.params) + pcm_params.params.ext_data_length) {
ipc_cmd_err(&ipc_tr, "pcm_params.params invalid size, hdr.size=%d, ext_data_len=%d",
pcm_params.params.hdr.size, pcm_params.params.ext_data_length);
return -EINVAL;
}
if (sizeof(pcm_params) + pcm_params.params.ext_data_length > SOF_IPC_MSG_MAX_SIZE) {
ipc_cmd_err(&ipc_tr, "pcm_params ext_data_length invalid size %d max allowed %zu",
pcm_params.params.ext_data_length,
SOF_IPC_MSG_MAX_SIZE - sizeof(pcm_params));
return -EINVAL;
}
switch (pcm_dev->cd->pipeline->status) {
case COMP_STATE_ACTIVE:
case COMP_STATE_PRE_ACTIVE:
/*
* IPC4 has a use-case when a PCM parameter change request can
* be sent on an active pipeline, ignore it
*/
pipe_dbg(pcm_dev->cd->pipeline,
"ipc: ignore PCM param change request on an active pipeline");
return 0;
}
#if CONFIG_HOST_PTABLE
cd = pcm_dev->cd;
/*
* walk in both directions to check if the pipeline is hostless
* skip page table set up if it is
*/
if (is_hostless_downstream(cd) && is_hostless_upstream(cd))
goto pipe_params;
err = comp_get_attribute(cd, COMP_ATTR_COPY_DIR, &direction);
if (err < 0)
goto error;
err = ipc_process_host_buffer(ipc, &pcm_params.params.buffer,
direction,
&elem_array,
&ring_size);
if (err < 0)
goto error;
err = comp_set_attribute(cd, COMP_ATTR_HOST_BUFFER, &elem_array);
if (err < 0) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d host buffer failed %d",
pcm_params.comp_id, err);
goto error;
}
/* TODO: should be extracted to platform specific code */
err = comp_set_attribute(cd, COMP_ATTR_COPY_TYPE, ©_type);
if (err < 0) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d setting copy type failed %d",
pcm_params.comp_id, err);
goto error;
}
pipe_params:
#endif
/* configure pipeline audio params */
err = pipeline_params(pcm_dev->cd->pipeline, pcm_dev->cd,
(struct sof_ipc_pcm_params *)ipc_get()->comp_data);
if (err < 0) {
ipc_cmd_err(&ipc_tr, "ipc: pipe %d comp %d params failed %d",
pcm_dev->cd->pipeline->pipeline_id,
pcm_params.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_params.comp_id, err);
goto error;
}
/* write component values to the outbox */
reply.rhdr.hdr.size = sizeof(reply);
reply.rhdr.hdr.cmd = stream;
reply.rhdr.error = 0;
reply.comp_id = pcm_params.comp_id;
reply.posn_offset = pcm_dev->cd->pipeline->posn_offset;
/* reset position value before send ipc */
memset(&posn, 0, sizeof(posn));
mailbox_stream_write(reply.posn_offset, &posn, sizeof(posn));
mailbox_hostbox_write(0, &reply, sizeof(reply));
return 1;
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_params.comp_id, reset_err);
return err;
}
/* free stream resources */
static int ipc_stream_pcm_free(uint32_t header)
{
struct ipc *ipc = ipc_get();
struct sof_ipc_stream free_req;
struct ipc_comp_dev *pcm_dev;
int ret;
/* copy message with ABI safe method */
IPC_COPY_CMD(free_req, ipc->comp_data);
/* get the pcm_dev */
pcm_dev = ipc_get_comp_by_id(ipc, free_req.comp_id);
if (!pcm_dev) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d not found", free_req.comp_id);
return -ENODEV;
}
/* check core */
if (!cpu_is_me(pcm_dev->core))
return ipc_process_on_core(pcm_dev->core, false);
tr_dbg(&ipc_tr, "ipc: comp %d -> free", free_req.comp_id);
/* sanity check comp */
if (!pcm_dev->cd->pipeline) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d pipeline not found",
free_req.comp_id);
return -EINVAL;
}
/* reset the pipeline */
ret = pipeline_reset(pcm_dev->cd->pipeline, pcm_dev->cd);
return ret;
}
/* get stream position */
static int ipc_stream_position(uint32_t header)
{
struct ipc *ipc = ipc_get();
struct sof_ipc_stream stream;
struct sof_ipc_stream_posn posn;
struct ipc_comp_dev *pcm_dev;
/* copy message with ABI safe method */
IPC_COPY_CMD(stream, ipc->comp_data);
/* get the pcm_dev */
pcm_dev = ipc_get_comp_by_id(ipc, stream.comp_id);
if (!pcm_dev) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d not found", stream.comp_id);
return -ENODEV;
}
/* check core */
if (!cpu_is_me(pcm_dev->core))
return ipc_process_on_core(pcm_dev->core, false);
tr_info(&ipc_tr, "ipc: comp %d -> position", stream.comp_id);
memset(&posn, 0, sizeof(posn));
/* set message fields - TODO; get others */
posn.rhdr.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_POSITION |
stream.comp_id;
posn.rhdr.hdr.size = sizeof(posn);
posn.comp_id = stream.comp_id;
if (!pcm_dev->cd->pipeline) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d pipeline not found",
stream.comp_id);
return -EINVAL;
}
/* get the stream positions and timestamps */
pipeline_get_timestamp(pcm_dev->cd->pipeline, pcm_dev->cd, &posn);
/* copy positions to stream region */
mailbox_stream_write(pcm_dev->cd->pipeline->posn_offset,
&posn, sizeof(posn));
return 1;
}
static int ipc_stream_trigger(uint32_t header)
{
struct ipc *ipc = ipc_get();
struct ipc_comp_dev *pcm_dev;
struct sof_ipc_stream stream;
uint32_t ipc_command = iCS(header);
uint32_t cmd;
int ret;
/* copy message with ABI safe method */
IPC_COPY_CMD(stream, ipc->comp_data);
/* get the pcm_dev */
pcm_dev = ipc_get_comp_by_id(ipc, stream.comp_id);
if (!pcm_dev) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d not found", stream.comp_id);
return -ENODEV;
}
/* check core */
if (!cpu_is_me(pcm_dev->core))
return ipc_process_on_core(pcm_dev->core, false);
tr_dbg(&ipc_tr, "ipc: comp %d -> trigger cmd 0x%x",
stream.comp_id, ipc_command);
switch (ipc_command) {
case SOF_IPC_STREAM_TRIG_START:
cmd = COMP_TRIGGER_PRE_START;
break;
case SOF_IPC_STREAM_TRIG_STOP:
cmd = COMP_TRIGGER_STOP;
break;
case SOF_IPC_STREAM_TRIG_PAUSE:
cmd = COMP_TRIGGER_PAUSE;
break;
case SOF_IPC_STREAM_TRIG_RELEASE:
cmd = COMP_TRIGGER_PRE_RELEASE;
break;
/* XRUN is special case- TODO */
case SOF_IPC_STREAM_TRIG_XRUN:
return 0;
default:
ipc_cmd_err(&ipc_tr, "ipc: invalid trigger cmd 0x%x", ipc_command);
return -ENODEV;
}
if (!pcm_dev->cd->pipeline) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d pipeline not found",
stream.comp_id);
return -EINVAL;
}
if (pcm_dev->type != COMP_TYPE_COMPONENT) {
ipc_cmd_err(&ipc_tr, "ipc: comp %d not stream (type %d)",
stream.comp_id, pcm_dev->type);
return -EINVAL;
}
/*
* Trigger the component: timer domain pipelines offload some trigger
* operations in their pipeline tasks, in which case IPC response to
* the host can be delayed. DMA domain pipelines always trigger
* synchronously.
*/
if (pipeline_is_timer_driven(pcm_dev->cd->pipeline)) {
k_spinlock_key_t key;
key = k_spin_lock(&ipc->lock);
ipc->task_mask |= IPC_TASK_IN_THREAD;
k_spin_unlock(&ipc->lock, key);
ret = pipeline_trigger(pcm_dev->cd->pipeline, pcm_dev->cd, cmd);
if (ret <= 0) {
key = k_spin_lock(&ipc->lock);
ipc->task_mask &= ~IPC_TASK_IN_THREAD;
k_spin_unlock(&ipc->lock, key);
}
} else {
ret = pipeline_trigger_run(pcm_dev->cd->pipeline, pcm_dev->cd, cmd);
}
if (ret < 0)
ipc_cmd_err(&ipc_tr, "ipc: comp %d trigger 0x%x failed %d",
stream.comp_id, ipc_command, ret);
return ret;
}
static int ipc_glb_stream_message(uint32_t header)
{
uint32_t cmd = iCS(header);
switch (cmd) {
case SOF_IPC_STREAM_PCM_PARAMS:
return ipc_stream_pcm_params(header);
case SOF_IPC_STREAM_PCM_FREE:
return ipc_stream_pcm_free(header);
case SOF_IPC_STREAM_TRIG_START:
case SOF_IPC_STREAM_TRIG_STOP:
case SOF_IPC_STREAM_TRIG_PAUSE:
case SOF_IPC_STREAM_TRIG_RELEASE:
case SOF_IPC_STREAM_TRIG_DRAIN:
case SOF_IPC_STREAM_TRIG_XRUN:
return ipc_stream_trigger(header);
case SOF_IPC_STREAM_POSITION:
return ipc_stream_position(header);
default:
ipc_cmd_err(&ipc_tr, "ipc: unknown stream cmd 0x%x", cmd);
return -EINVAL;
}
}
/*
* DAI IPC Operations.
*/
static void build_dai_config(struct sof_ipc_dai_config *config,
struct ipc_config_dai *config_dai)
{
memset(config_dai, 0, sizeof(*config_dai));
config_dai->dai_index = config->dai_index;
config_dai->direction = -1;
config_dai->format = config->format;
config_dai->group_id = config->group_id;
config_dai->type = config->type;
}
static int ipc_dai_config_set(struct sof_ipc_dai_config *config,
struct ipc_config_dai *config_dai)
{
struct dai *dai;
int ret;
/* get DAI */
dai = dai_get(config->type, config->dai_index, 0 /* existing only */);
if (!dai) {
ipc_cmd_err(&ipc_tr, "ipc: dai %d,%d not found", config->type,
config->dai_index);
return -ENODEV;
}
/* configure DAI */
ret = dai_set_config(dai, config_dai, config);
dai_put(dai); /* free ref immediately */
if (ret < 0) {
ipc_cmd_err(&ipc_tr, "ipc: dai %d,%d config failed %d", config->type,
config->dai_index, ret);
return ret;
}
return 0;
}
/*
* DAI config occurs in several steps (and can be optimised)
* 1) IPC arrived from host
* 2) Primary core configures the DAI driver HW config via drv->set_config()
* 3) Target core then calls comp->dai_config()
* 4) Stream params IPC then calls dai_params() which calls
* ipc_dai_data_config() followed by dai_verify_params() to validate
* stream params with physical DAI HW config.
*/
static int ipc_msg_dai_config(uint32_t header)
{
struct ipc *ipc = ipc_get();
struct ipc_config_dai config_dai;
struct sof_ipc_dai_config config;
int ret;
/* copy message with ABI safe method */
IPC_COPY_CMD(config, ipc->comp_data);
tr_info(&ipc_tr, "ipc: dai %d.%d -> config ", config.type,
config.dai_index);
/* set common configuration */
build_dai_config(&config, &config_dai);
/* only primary core configures dai */
if (cpu_get_id() == PLATFORM_PRIMARY_CORE_ID) {
ret = ipc_dai_config_set(&config, &config_dai);
if (ret < 0)
return ret;
}
/* send params to all DAI components who use that physical DAI */
return ipc_comp_dai_config(ipc, &config_dai, ipc->comp_data);
}
static int ipc_glb_dai_message(uint32_t header)
{
uint32_t cmd = iCS(header);
switch (cmd) {
case SOF_IPC_DAI_CONFIG:
return ipc_msg_dai_config(header);
case SOF_IPC_DAI_LOOPBACK:
//return ipc_comp_set_value(header, COMP_CMD_LOOPBACK);
default:
ipc_cmd_err(&ipc_tr, "ipc: unknown DAI cmd 0x%x", cmd);
return -EINVAL;
}
}
/*
* PM IPC Operations.
*/
static int ipc_pm_context_size(uint32_t header)
{
struct sof_ipc_pm_ctx pm_ctx;
tr_info(&ipc_tr, "ipc: pm -> size");
bzero(&pm_ctx, sizeof(pm_ctx));
/* TODO: calculate the context and size of host buffers required */
/* write the context to the host driver */
//mailbox_hostbox_write(0, &pm_ctx, sizeof(pm_ctx));
return 0;
}
static int ipc_pm_context_save(uint32_t header)
{
//struct sof_ipc_pm_ctx *pm_ctx = _ipc->comp_data;
tr_info(&ipc_tr, "ipc: pm -> save");
sa_exit(sof_get());
/* do platform specific suspending */
platform_context_save(sof_get());
#if !defined(CONFIG_LIBRARY) && !defined(CONFIG_ZEPHYR_POSIX)
/* TODO: check we are inactive - all streams are suspended */
/* TODO: mask ALL platform interrupts except DMA */
/* mask all DSP interrupts */
arch_irq_lock();
/* TODO: mask ALL platform interrupts inc DMA */
/* TODO: clear any outstanding platform IRQs - TODO refine */
/* TODO: stop ALL timers */
platform_timer_stop(timer_get());
/* TODO: disable SSP and DMA HW */
/* TODO: save the context */
//reply.entries_no = 0;
/* write the context to the host driver */
//mailbox_hostbox_write(0, pm_ctx, sizeof(*pm_ctx));
#endif
ipc_get()->pm_prepare_D3 = 1;
return 0;
}
static int ipc_pm_context_restore(uint32_t header)
{
//struct sof_ipc_pm_ctx *pm_ctx = _ipc->comp_data;
tr_info(&ipc_tr, "ipc: pm -> restore");
ipc_get()->pm_prepare_D3 = 0;
/* restore context placeholder */
//mailbox_hostbox_write(0, pm_ctx, sizeof(*pm_ctx));
return 0;
}
static int ipc_pm_core_enable(uint32_t header)
{
struct sof_ipc_pm_core_config pm_core_config;
int ret;
int i = 0;
/* copy message with ABI safe method */
IPC_COPY_CMD(pm_core_config, ipc_get()->comp_data);
/* check if core enable mask is valid */
if (pm_core_config.enable_mask > MASK(CONFIG_CORE_COUNT - 1, 0)) {
ipc_cmd_err(&ipc_tr, "ipc: CONFIG_CORE_COUNT: %d < core enable mask: %d",
CONFIG_CORE_COUNT, pm_core_config.enable_mask);
return -EINVAL;
}
tr_info(&ipc_tr, "ipc: pm core mask 0x%x -> enable",
pm_core_config.enable_mask);
for (i = 0; i < CONFIG_CORE_COUNT; i++) {
if (i != PLATFORM_PRIMARY_CORE_ID) {
if (pm_core_config.enable_mask & (1 << i)) {
ret = cpu_enable_core(i);
if (ret < 0) {
ipc_cmd_err(&ipc_tr, "Failed to enable core %d", i);
return ret;
}
} else {
cpu_disable_core(i);
}
}
}
return 0;
}
static int ipc_pm_gate(uint32_t header)
{
struct sof_ipc_pm_gate pm_gate;
IPC_COPY_CMD(pm_gate, ipc_get()->comp_data);
tr_info(&ipc_tr, "ipc: pm gate flags 0x%x", pm_gate.flags);
/* pause dma trace firstly if needed */
if (pm_gate.flags & SOF_PM_NO_TRACE)
trace_off();
if (pm_gate.flags & SOF_PM_PPG) {
pm_runtime_disable(PM_RUNTIME_DSP, PLATFORM_PRIMARY_CORE_ID);
} else {
/* before we enable pm runtime and perform D0->D0ix flow
* (primary core powers off secondary cores in
* platform_pg_int_handler) we have to prepare all secondary
* cores data for powering off (disable interrupt, perform
* cache writeback).
*/
pm_runtime_enable(PM_RUNTIME_DSP, PLATFORM_PRIMARY_CORE_ID);
}
/* resume dma trace if needed */
if (!(pm_gate.flags & SOF_PM_NO_TRACE))
trace_on();
return 0;
}
static int ipc_glb_pm_message(uint32_t header)
{
uint32_t cmd = iCS(header);
switch (cmd) {
case SOF_IPC_PM_CTX_SAVE:
return ipc_pm_context_save(header);
case SOF_IPC_PM_CTX_RESTORE:
return ipc_pm_context_restore(header);
case SOF_IPC_PM_CTX_SIZE:
return ipc_pm_context_size(header);
case SOF_IPC_PM_CORE_ENABLE:
return ipc_pm_core_enable(header);
case SOF_IPC_PM_GATE:
return ipc_pm_gate(header);
case SOF_IPC_PM_CLK_SET:
case SOF_IPC_PM_CLK_GET:
case SOF_IPC_PM_CLK_REQ:
default:
ipc_cmd_err(&ipc_tr, "ipc: unknown pm cmd 0x%x", cmd);
return -EINVAL;
}
}
#if CONFIG_TRACE
/*
* Debug IPC Operations.
*/
static void ipc_dma_trace_free(uint32_t header)
{
struct dma_trace_data *dmat = dma_trace_data_get();
dma_trace_disable(dmat);
}
static int ipc_dma_trace_config(uint32_t header)
{
#if CONFIG_HOST_PTABLE
struct dma_sg_elem_array elem_array;
uint32_t ring_size;
#endif
struct dma_trace_data *dmat = dma_trace_data_get();
struct ipc *ipc = ipc_get();
struct sof_ipc_dma_trace_params_ext params;
int err;
if (!dmat) {
mtrace_printf(LOG_LEVEL_ERROR,
"ipc_dma_trace_config failed: dmat not initialized");
return -ENOMEM;
}
/* copy message with ABI safe method */
IPC_COPY_CMD(params, ipc->comp_data);
if (iCS(header) == SOF_IPC_TRACE_DMA_PARAMS_EXT)
/* As version 5.12 Linux sends the monotonic
* ktime_get(). Search for
* "SOF_IPC_TRACE_DMA_PARAMS_EXT" in your particular
* kernel version.
*/
dmat->time_delta = k_ns_to_cyc_near64(params.timestamp_ns) - sof_cycle_get_64();
else
dmat->time_delta = 0;
#if CONFIG_HOST_PTABLE
err = ipc_process_host_buffer(ipc, ¶ms.buffer,
SOF_IPC_STREAM_CAPTURE,
&elem_array,
&ring_size);
if (err < 0)
goto processing_error;
err = dma_trace_host_buffer(dmat, &elem_array, ring_size);
if (err < 0) {
ipc_cmd_err(&ipc_tr, "ipc: trace failed to set host buffers %d",
err);
goto error;
}
#else
/* stream tag of capture stream for DMA trace */
dmat->stream_tag = params.stream_tag;
/* host buffer size for DMA trace */
dmat->host_size = params.buffer.size;
#endif
err = dma_trace_enable(dmat);
if (err < 0) {
ipc_cmd_err(&ipc_tr, "ipc: failed to enable trace %d", err);
goto error;
}
return 0;
error:
#if CONFIG_HOST_PTABLE
dma_sg_free(&elem_array);
processing_error:
#endif
return err;
}
static int ipc_trace_filter_update(uint32_t header)
{
struct sof_ipc_trace_filter_elem *next_elem;
struct sof_ipc_trace_filter_elem *elem;
struct sof_ipc_trace_filter_elem *end;
struct sof_ipc_trace_filter *packet;
struct trace_filter filter;
struct ipc *ipc = ipc_get();
int ret = 0;
int cnt;
packet = ipc->comp_data;
/* validation, packet->hdr.size has already been compared with SOF_IPC_MSG_MAX_SIZE */
if (sizeof(*packet) + sizeof(*elem) * packet->elem_cnt != packet->hdr.size) {
ipc_cmd_err(&ipc_tr, "trace_filter_update failed, elem_cnt %d is inconsistent with hdr.size %d",
packet->elem_cnt, packet->hdr.size);
return -EINVAL;
}
tr_info(&ipc_tr, "ipc: trace_filter_update received, size %d elems",
packet->elem_cnt);
elem = packet->elems;
end = &packet->elems[packet->elem_cnt];
/* read each filter set and update selected components trace settings */
while (elem != end) {
next_elem = trace_filter_fill(elem, end, &filter);
if (!next_elem)
return -EINVAL;
cnt = trace_filter_update(&filter);
if (cnt < 0) {
ipc_cmd_err(&ipc_tr, "trace_filter_update failed for UUID key 0x%X, comp %d.%d and log level %d",
filter.uuid_id, filter.pipe_id, filter.comp_id,
filter.log_level);
ret = cnt;
} else {
tr_info(&ipc_tr, "trace_filter_update for UUID key 0x%X, comp %d.%d affected %d components",
filter.uuid_id, filter.pipe_id, filter.comp_id,
cnt);
}
elem = next_elem;
}
return ret;
}
static int ipc_glb_trace_message(uint32_t header)
{
uint32_t cmd = iCS(header);
tr_info(&ipc_tr, "ipc: debug cmd 0x%x", cmd);
switch (cmd) {
case SOF_IPC_TRACE_DMA_PARAMS:
case SOF_IPC_TRACE_DMA_PARAMS_EXT:
return ipc_dma_trace_config(header);
case SOF_IPC_TRACE_DMA_FREE:
ipc_dma_trace_free(header);
return 0;
case SOF_IPC_TRACE_FILTER_UPDATE:
return ipc_trace_filter_update(header);
default:
ipc_cmd_err(&ipc_tr, "ipc: unknown debug cmd 0x%x", cmd);
return -EINVAL;
}
}
#else
static int ipc_glb_trace_message(uint32_t header)
{
/* Return success, as the protocol provides no way to inform
* the kernel that we don't support dtrace. It will just see
* no output.
*/
return 0;
}
#endif
static int ipc_glb_gdb_debug(uint32_t header)
{
/* no further information needs to be extracted from header */
(void) header;
#if CONFIG_GDBSTUB
ipc_enter_gdb = true;
return 0;
// TODO: remove old GDB stub?
#elif CONFIG_GDB_DEBUG
gdb_init_debug_exception();
gdb_init();
/* TODO: this asm should be in arch/include/debug/debug.h
* with a generic name and trigger debug exception
*/
asm volatile("_break 0, 0");
return 0;
#else
return -EINVAL;
#endif
}
#if CONFIG_PROBE
static inline int ipc_probe_init(uint32_t header)
{
struct sof_ipc_probe_dma_add_params *params = ipc_get()->comp_data;
int dma_provided = params->num_elems;
tr_dbg(&ipc_tr, "entry");