Repository navigation
Expand file tree
/
Copy pathremote_server.c
More file actions
6134 lines (5401 loc) · 204 KB
/
Copy pathremote_server.c
File metadata and controls
6134 lines (5401 loc) · 204 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) 2026 Intel Corporation. All rights reserved.
//
// Network Server Interface (sof-remote TCP protocol) for SOF Testbench
#if !defined(__XTENSA__) && !defined(CONFIG_XT_SIMULATOR)
#include <arpa/inet.h>
#include <dlfcn.h>
#include <errno.h>
#include <math.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/component.h>
#include <sof/audio/buffer.h>
#include <sof/ipc/topology.h>
#include <sof/ipc/driver.h>
#include <sof/lib/cpu.h>
#include <sof/list.h>
#include <platform/lib/ll_schedule.h>
#include <platform/lib/telemetry_sched.h>
#include <ipc4/header.h>
#include <ipc4/module.h>
#include <ipc4/pipeline.h>
#include <sof_versions.h>
#include "testbench/sof_remote_proto.h"
#include "testbench/remote_server.h"
#include "testbench/topology_ipc4.h"
#include "testbench/trace.h"
#include "testbench/file.h"
#include "testbench/file_ipc4.h"
#include "testbench/utils.h"
#include "../../src/audio/copier/copier.h"
/* IPC4 BaseFW Large Config Parameter IDs */
#define TB_IPC4_FW_CONFIG 0
#define TB_IPC4_HW_CONFIG_GET 1
#define TB_IPC4_MODULES_INFO_GET 9
#define TB_IPC4_PIPELINE_LIST_INFO_GET 12
#define TB_IPC4_MEMORY_STATE_INFO_GET 14
#define TB_IPC4_POWER_STATE_INFO_GET 15
#define TB_IPC4_PERF_MEASUREMENTS_STATE 17
#define TB_IPC4_GLOBAL_PERF_DATA 18
#define TB_IPC4_EXTENDED_GLOBAL_PERF_DATA 30
/* PCM Capture Ring Buffer Capacity (in S16_LE samples: 48000 * 2ch * 2 sec = 192000 samples) */
#define TB_PCM_CAPTURE_RING_SAMPLES 192000
struct system_tick_info_wire {
uint32_t count;
uint32_t last_time_elapsed;
uint32_t max_time_elapsed;
uint32_t last_ccount;
uint32_t avg_utilization;
uint32_t peak_utilization;
uint32_t peak_utilization_4k;
uint32_t peak_utilization_8k;
} __attribute__((packed));
struct perf_data_item_comp_wire {
uint32_t resource_id;
uint32_t power_mode : 1;
uint32_t is_removed : 1;
uint32_t rsvd : 30;
uint32_t peak_kcps;
uint32_t avg_kcps;
uint64_t total_iteration_count;
uint64_t total_cycles_consumed;
} __attribute__((packed));
static pthread_mutex_t g_tb_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t g_tb_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t g_pcm_ring_lock = PTHREAD_MUTEX_INITIALIZER;
static struct testbench_prm *g_tp;
static int g_srv_fd = -1;
static pthread_t g_accept_thread;
static volatile bool g_srv_running;
static volatile bool g_quit_requested;
static volatile bool g_dsp_paused;
static volatile int g_step_ticks_remaining;
static __thread bool g_in_remote_rpc;
static __thread bool g_client_accepts_compressed;
static uint32_t g_perf_meas_state = 2; /* 2 = STARTED by default */
/* Per-core telemetry state */
static struct system_tick_info_wire g_core_ticks[CONFIG_CORE_COUNT];
static uint64_t g_total_ticks;
/* Emulated SRAM Windows */
static uint8_t g_win0_fw_regs[4096];
static uint8_t g_win1_outbox[4096];
static uint8_t g_win2_debug[65536];
static uint8_t g_win3_perf[8192];
static uint8_t g_win3_legacy[4096];
static char g_last_shell_output[4096];
static uint32_t g_shell_seq;
/* Tone generator & PCM capture state */
static volatile bool g_tone_running;
static char g_pcm_device[32] = "hw:0,0";
static uint32_t g_tone_freq_hz = 1000;
static uint32_t g_pcm_rate = 48000;
static uint32_t g_pcm_channels = 2;
static uint32_t g_pcm_api_used = SOF_AUDIO_API_ALSA;
static uint32_t g_pcm_format = SOF_PCM_FMT_S16_LE;
static uint32_t g_pcm_xruns;
static uint64_t g_pcm_frames_processed;
static uint32_t g_pcm_last_rms;
static uint32_t g_pcm_last_peak;
static int16_t g_pcm_capture_ring[TB_PCM_CAPTURE_RING_SAMPLES];
static size_t g_pcm_ring_write_idx;
static size_t g_pcm_ring_avail;
/* Cached Kcontrol state for SOF_NET_OP_KCTL_LIST / GET / SET */
#define TB_REMOTE_MAX_KCTLS 64
struct tb_kctl_shadow {
bool initialized;
int64_t values[8];
uint32_t bytes_len;
uint8_t bytes_data[512];
};
static struct tb_kctl_shadow g_kctl_shadow[TB_REMOTE_MAX_KCTLS];
/* Dynamically loaded LLEXT / shared-library modules */
#define TB_MAX_LOADED_LLEXTS 16
struct tb_loaded_llext {
bool active;
uint32_t lib_id;
uint32_t data_size;
char name[16];
char path[128];
void *dl_handle;
};
static struct tb_loaded_llext g_loaded_llexts[TB_MAX_LOADED_LLEXTS];
static int read_exact(int fd, void *buf, size_t len)
{
uint8_t *p = (uint8_t *)buf;
size_t rem = len;
while (rem > 0) {
ssize_t n = recv(fd, p, rem, 0);
if (n <= 0) {
if (n < 0 && errno == EINTR)
continue;
return -1;
}
p += n;
rem -= (size_t)n;
}
return 0;
}
static int write_exact(int fd, const void *buf, size_t len)
{
const uint8_t *p = (const uint8_t *)buf;
size_t rem = len;
while (rem > 0) {
ssize_t n = send(fd, p, rem, MSG_NOSIGNAL);
if (n <= 0) {
if (n < 0 && errno == EINTR)
continue;
return -1;
}
p += n;
rem -= (size_t)n;
}
return 0;
}
static int send_reply(int sock, uint16_t opcode, uint32_t req_id,
int32_t status, const void *payload, uint32_t payload_len)
{
uint16_t base_op = (uint16_t)(opcode & SOF_NET_OP_MASK);
struct sof_net_hdr hdr = {
.magic = SOF_NET_MAGIC,
.version = SOF_NET_VERSION,
.opcode = base_op,
.req_id = req_id,
.payload_len = payload ? payload_len : 0,
.status = status,
};
if (g_client_accepts_compressed && payload && payload_len >= 128U) {
uint8_t *cbuf = malloc(payload_len);
if (cbuf) {
uint32_t clen = sof_net_compress((const uint8_t *)payload,
payload_len, cbuf, payload_len);
if (clen > 0 && clen < payload_len) {
int rc;
hdr.opcode = (uint16_t)(base_op | SOF_NET_OP_FLAG_COMPRESSED);
hdr.payload_len = clen;
rc = write_exact(sock, &hdr, sizeof(hdr));
if (rc == 0)
rc = write_exact(sock, cbuf, clen);
free(cbuf);
return rc;
}
free(cbuf);
}
}
if (write_exact(sock, &hdr, sizeof(hdr)) < 0)
return -1;
if (payload && payload_len > 0) {
if (write_exact(sock, payload, payload_len) < 0)
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------
* PCM Output Recording & Tone State Hooks (called from file.c)
* -------------------------------------------------------------------------
*/
bool tb_remote_is_tone_active(uint32_t *freq_hz)
{
if (!g_tone_running)
return false;
if (freq_hz)
*freq_hz = g_tone_freq_hz ? g_tone_freq_hz : 1000;
return true;
}
void tb_remote_record_output_samples_s16(const int16_t *samples, size_t count,
int channels, int rate)
{
double sum_sq = 0.0;
int16_t peak = 0;
size_t i;
if (!samples || count == 0)
return;
pthread_mutex_lock(&g_pcm_ring_lock);
if (channels > 0)
g_pcm_channels = (uint32_t)channels;
if (rate > 0)
g_pcm_rate = (uint32_t)rate;
for (i = 0; i < count; i++) {
int16_t v = samples[i];
int16_t av = v < 0 ? -v : v;
if (av > peak)
peak = av;
sum_sq += (double)v * (double)v;
g_pcm_capture_ring[g_pcm_ring_write_idx] = v;
g_pcm_ring_write_idx = (g_pcm_ring_write_idx + 1) % TB_PCM_CAPTURE_RING_SAMPLES;
if (g_pcm_ring_avail < TB_PCM_CAPTURE_RING_SAMPLES)
g_pcm_ring_avail++;
}
g_pcm_last_rms = (uint32_t)sqrt(sum_sq / (double)count);
g_pcm_last_peak = (uint32_t)peak;
if (g_pcm_channels > 0)
g_pcm_frames_processed += count / g_pcm_channels;
pthread_mutex_unlock(&g_pcm_ring_lock);
}
void tb_remote_record_output_samples_s32(const int32_t *samples, size_t count,
int channels, int rate, int shift_to_s16)
{
double sum_sq = 0.0;
int16_t peak = 0;
size_t i;
if (!samples || count == 0)
return;
pthread_mutex_lock(&g_pcm_ring_lock);
if (channels > 0)
g_pcm_channels = (uint32_t)channels;
if (rate > 0)
g_pcm_rate = (uint32_t)rate;
for (i = 0; i < count; i++) {
int32_t s32 = samples[i] >> shift_to_s16;
int16_t v;
int16_t av;
if (s32 > 32767)
s32 = 32767;
else if (s32 < -32768)
s32 = -32768;
v = (int16_t)s32;
av = v == -32768 ? 32767 : (v < 0 ? -v : v);
if (av > peak)
peak = av;
sum_sq += (double)v * (double)v;
g_pcm_capture_ring[g_pcm_ring_write_idx] = v;
g_pcm_ring_write_idx = (g_pcm_ring_write_idx + 1) % TB_PCM_CAPTURE_RING_SAMPLES;
if (g_pcm_ring_avail < TB_PCM_CAPTURE_RING_SAMPLES)
g_pcm_ring_avail++;
}
g_pcm_last_rms = (uint32_t)sqrt(sum_sq / (double)count);
g_pcm_last_peak = (uint32_t)peak;
if (g_pcm_channels > 0)
g_pcm_frames_processed += count / g_pcm_channels;
pthread_mutex_unlock(&g_pcm_ring_lock);
}
/* -------------------------------------------------------------------------
* Emulated SRAM Windows, Device Info & Telemetry Synthesis
* -------------------------------------------------------------------------
*/
static struct tplg_comp_info *find_comp_info_by_ipc_id(struct testbench_prm *tp, uint32_t ipc_id)
{
struct list_item *item;
int i;
if (!tp)
return NULL;
if (tp->widget_list.next) {
list_for_item(item, &tp->widget_list) {
struct tplg_comp_info *ci = container_of(item, struct tplg_comp_info, item);
uint32_t cid = (uint32_t)IPC4_COMP_ID(ci->module_id, ci->instance_id);
if (cid == ipc_id)
return ci;
}
list_for_item(item, &tp->widget_list) {
struct tplg_comp_info *ci = container_of(item, struct tplg_comp_info, item);
if ((uint32_t)ci->id == ipc_id)
return ci;
}
}
if (!tp->info)
return NULL;
for (i = 0; i < tp->info_elems; i++) {
uint32_t cid = (uint32_t)IPC4_COMP_ID(tp->info[i].module_id,
tp->info[i].instance_id);
if (cid == ipc_id)
return &tp->info[i];
}
for (i = 0; i < tp->info_elems; i++) {
if ((uint32_t)tp->info[i].id == ipc_id)
return &tp->info[i];
}
return NULL;
}
/* Cached widget names so post-run sched-trace / sched-telem queries resolve names */
struct tb_cached_widget_name {
uint32_t comp_id;
char name[32];
};
static struct tb_cached_widget_name g_cached_widget_names[128];
static uint32_t g_num_cached_widget_names;
static void cache_active_widget_names(struct testbench_prm *tp)
{
struct list_item *item;
int i;
if (!tp)
return;
if (tp->widget_list.next && !list_is_empty(&tp->widget_list)) {
g_num_cached_widget_names = 0;
list_for_item(item, &tp->widget_list) {
struct tplg_comp_info *ci = container_of(item, struct tplg_comp_info, item);
uint32_t cid = (uint32_t)IPC4_COMP_ID(ci->module_id, ci->instance_id);
if (!ci->name || g_num_cached_widget_names >= 128)
continue;
g_cached_widget_names[g_num_cached_widget_names].comp_id = cid;
snprintf(g_cached_widget_names[g_num_cached_widget_names].name,
sizeof(g_cached_widget_names[0].name), "%s", ci->name);
g_num_cached_widget_names++;
}
} else if (tp->info && tp->info_elems > 0) {
g_num_cached_widget_names = 0;
for (i = 0; i < tp->info_elems && g_num_cached_widget_names < 128; i++) {
uint32_t cid = (uint32_t)IPC4_COMP_ID(tp->info[i].module_id,
tp->info[i].instance_id);
if (!tp->info[i].name)
continue;
g_cached_widget_names[g_num_cached_widget_names].comp_id = cid;
snprintf(g_cached_widget_names[g_num_cached_widget_names].name,
sizeof(g_cached_widget_names[0].name), "%s", tp->info[i].name);
g_num_cached_widget_names++;
}
}
}
static const char *resolve_comp_widget_name(struct testbench_prm *tp, uint32_t ipc_id)
{
struct tplg_comp_info *ci = find_comp_info_by_ipc_id(tp, ipc_id);
uint32_t i;
if (ci && ci->name)
return ci->name;
for (i = 0; i < g_num_cached_widget_names; i++) {
if (g_cached_widget_names[i].comp_id == ipc_id &&
g_cached_widget_names[i].name[0])
return g_cached_widget_names[i].name;
}
return "comp";
}
static void populate_cdev_info(struct testbench_prm *tp, struct sof_cdev_info *info)
{
struct host_sched_telemetry *st = host_telem_get();
const char *base_tplg = "";
int i;
memset(info, 0, sizeof(*info));
info->abi_version = SOF_CDEV_ABI_VERSION;
#if CONFIG_IPC_MAJOR_4
info->ipc_type = 1;
#else
info->ipc_type = 0;
#endif
info->fw_state = 7; /* SOF_FW_BOOT_COMPLETE */
info->dsp_power_state = g_dsp_paused ? 4 : 1; /* 1=D0, 4=D3 */
info->pm_runtime_active = g_dsp_paused ? 0 : 1;
info->num_cores = st ? st->hw_cfg.num_cores : CONFIG_CORE_COUNT;
info->enabled_cores_mask = arch_cpu_enabled_cores();
info->max_payload_size = SOF_IPC_MSG_MAX_SIZE;
info->fw_ver_major = SOF_MAJOR;
info->fw_ver_minor = SOF_MINOR;
info->fw_ver_micro = SOF_MICRO;
info->fw_ver_build = SOF_BUILD;
info->hw_ip_version = (st && st->hw_cfg.hw_ip_version) ? st->hw_cfg.hw_ip_version : 4;
snprintf(info->platform, sizeof(info->platform), "%s",
(st && st->hw_cfg.platform_name[0]) ? st->hw_cfg.platform_name : "sof-testbench4");
if (tp && tp->tplg_file) {
const char *slash = strrchr(tp->tplg_file, '/');
base_tplg = slash ? (slash + 1) : tp->tplg_file;
}
snprintf(info->fw_filename, sizeof(info->fw_filename), "%s", base_tplg);
/* 4 SRAM windows */
info->windows[0].win_id = SOF_CDEV_WIN_FW_REGS;
info->windows[0].bar_offset = 0x000000;
info->windows[0].size = sizeof(g_win0_fw_regs);
info->windows[0].flags = 1;
info->windows[1].win_id = SOF_CDEV_WIN_OUTBOX;
info->windows[1].bar_offset = 0x001000;
info->windows[1].size = sizeof(g_win1_outbox);
info->windows[1].flags = 1;
info->windows[2].win_id = SOF_CDEV_WIN_DEBUG;
info->windows[2].bar_offset = 0x010000;
info->windows[2].size = sizeof(g_win2_debug);
info->windows[2].flags = 1;
info->windows[3].win_id = SOF_CDEV_WIN_PERF;
info->windows[3].bar_offset = 0x020000;
info->windows[3].size = sizeof(g_win3_perf);
info->windows[3].flags = 1;
/* Window 2 Debug Slots */
for (i = 0; i < SOF_CDEV_MAX_DEBUG_SLOTS; i++) {
info->debug_slots[i].slot_index = i;
info->debug_slots[i].win_offset = (i + 1) * 4096;
info->debug_slots[i].bar_offset = 0x010000 + (i + 1) * 4096;
info->debug_slots[i].size = 4096;
}
info->debug_slots[0].res_id = 0x00000001;
info->debug_slots[0].type = SOF_CDEV_SLOT_TELEMETRY;
info->debug_slots[0].vma = 0xA0011000;
info->debug_slots[1].res_id = 0x00000002;
info->debug_slots[1].type = SOF_CDEV_SLOT_SHELL;
info->debug_slots[1].vma = 0xA0012000;
info->debug_slots[2].res_id = 0x00000003;
info->debug_slots[2].type = SOF_CDEV_SLOT_DEBUG_LOG;
info->debug_slots[2].vma = 0xA0013000;
}
static uint32_t build_modules_info_tlv(struct testbench_prm *tp, uint8_t *out, uint32_t cap)
{
/*
* Matches TelemetryParser.parse_modules_info_tlv in tools/softop/backend.py:
* uint32_t mod_count
* followed by mod_count * 116-byte sof_man_module entries:
* bytes 0..1: uint16_t module_id
* bytes 2..3: uint16_t state_flags
* bytes 4..11: char name[8]
*/
uint32_t seen_mod_ids[64];
char seen_names[64][8];
uint32_t count = 0;
int i;
if (cap < 4)
return 0;
memset(out, 0, cap);
/* Always include module_id 0 = basefw */
seen_mod_ids[count] = 0;
memset(seen_names[count], 0, 8);
memcpy(seen_names[count], "basefw", 6);
count++;
if (tp && tp->widget_list.next) {
struct list_item *item;
list_for_item(item, &tp->widget_list) {
struct tplg_comp_info *ci = container_of(item, struct tplg_comp_info, item);
uint32_t mid = (uint32_t)ci->module_id;
bool dup = false;
uint32_t j;
if (count >= 64)
break;
if (mid == 0 || !ci->name)
continue;
for (j = 0; j < count; j++) {
if (seen_mod_ids[j] == mid) {
dup = true;
break;
}
}
if (dup)
continue;
seen_mod_ids[count] = mid;
memset(seen_names[count], 0, 8);
const char *dot = strchr(ci->name, '.');
size_t nlen = dot ? (size_t)(dot - ci->name) : strlen(ci->name);
if (nlen > 7)
nlen = 7;
memcpy(seen_names[count], ci->name, nlen);
count++;
}
} else if (tp && tp->info) {
for (i = 0; i < tp->info_elems && count < 64; i++) {
struct tplg_comp_info *ci = &tp->info[i];
uint32_t mid = (uint32_t)ci->module_id;
bool dup = false;
uint32_t j;
if (mid == 0 || !ci->name)
continue;
for (j = 0; j < count; j++) {
if (seen_mod_ids[j] == mid) {
dup = true;
break;
}
}
if (dup)
continue;
seen_mod_ids[count] = mid;
memset(seen_names[count], 0, 8);
/* Extract prefix before '.' from widget name (e.g. "gain.1.1" -> "gain") */
const char *dot = strchr(ci->name, '.');
size_t nlen = dot ? (size_t)(dot - ci->name) : strlen(ci->name);
if (nlen > 7)
nlen = 7;
memcpy(seen_names[count], ci->name, nlen);
count++;
}
}
/* Include any dynamically loaded LLEXT modules */
for (i = 0; i < TB_MAX_LOADED_LLEXTS && count < 64; i++) {
if (!g_loaded_llexts[i].active)
continue;
seen_mod_ids[count] = 0x100 + g_loaded_llexts[i].lib_id;
memset(seen_names[count], 0, 8);
strncpy(seen_names[count], g_loaded_llexts[i].name, 7);
count++;
}
uint32_t total_len = 4 + count * 116;
if (total_len > cap) {
count = (cap - 4) / 116;
total_len = 4 + count * 116;
}
memcpy(out, &count, sizeof(uint32_t));
for (uint32_t idx = 0; idx < count; idx++) {
uint8_t *ent = out + 4 + idx * 116;
uint16_t mid16 = (uint16_t)seen_mod_ids[idx];
uint16_t flg16 = 1;
memcpy(ent + 0, &mid16, 2);
memcpy(ent + 2, &flg16, 2);
memcpy(ent + 4, seen_names[idx], 8);
}
return total_len;
}
static uint32_t build_power_state_tlv(uint8_t *out, uint32_t cap)
{
/*
* TLV 0: IPC4_ACTIVE_CORES_MASK (uint32_t)
* TLV 1: IPC4_CORE_KCPS (uint32_t[num_cores])
*/
struct host_sched_telemetry *st = host_telem_get();
uint32_t num_cores = (st && st->hw_cfg.num_cores <= CONFIG_CORE_COUNT) ?
st->hw_cfg.num_cores : CONFIG_CORE_COUNT;
uint32_t core_kcps[CONFIG_CORE_COUNT];
uint32_t mask = arch_cpu_enabled_cores();
uint32_t off = 0;
uint32_t c;
if (cap < 8 + 4 + 8 + num_cores * sizeof(uint32_t))
return 0;
for (c = 0; c < num_cores; c++)
core_kcps[c] = g_core_ticks[c].avg_utilization;
/* Tuple 0: type=0, len=4, val=mask */
uint32_t t0_type = 0, t0_len = 4;
memcpy(out + off, &t0_type, 4); off += 4;
memcpy(out + off, &t0_len, 4); off += 4;
memcpy(out + off, &mask, 4); off += 4;
/* Tuple 1: type=1, len=num_cores*4, val=core_kcps */
uint32_t t1_type = 1, t1_len = num_cores * sizeof(uint32_t);
memcpy(out + off, &t1_type, 4); off += 4;
memcpy(out + off, &t1_len, 4); off += 4;
memcpy(out + off, core_kcps, t1_len); off += t1_len;
return off;
}
static uint32_t build_memory_state_tlv(struct testbench_prm *tp, uint8_t *out, uint32_t cap)
{
/*
* Matches TelemetryParser.parse_memory_state_tlv in tools/softop/backend.py:
* TLV type 1 (HPSRAM) and TLV type 0 (LPSRAM)
* Payload layout:
* uint32_t free_phys_pages
* uint32_t ebb_dwords (1 or 3 depending on bank_count)
* uint32_t pgctl_masks[ebb_dwords] (bit=0 powered ON, bit=1 gated)
* uint32_t bank_count (configured HPSRAM/LPSRAM 64 KB EBB banks)
* uint16_t bank_alloc_mask[bank_count]
*/
struct host_sched_telemetry *st = host_telem_get();
struct tb_heap_usage_record recs[64];
int rec_count = 0;
size_t total_heap_bytes = 0;
int i;
if (tp) {
memset(recs, 0, sizeof(recs));
tb_collect_heap_usage(tp, recs, &rec_count);
for (i = 0; i < rec_count && i < 64; i++)
total_heap_bytes += recs[i].heap_max;
}
if (st && st->hw_cfg.hpsram_used_bytes > total_heap_bytes)
total_heap_bytes = st->hw_cfg.hpsram_used_bytes;
uint32_t off = 0;
size_t hp_total_bytes = (st && st->hw_cfg.hpsram_size_bytes >= 65536UL) ?
st->hw_cfg.hpsram_size_bytes : (16UL * 65536UL);
uint32_t hp_banks = (uint32_t)((hp_total_bytes + 65535UL) / 65536UL);
uint32_t hp_used_banks = (uint32_t)((total_heap_bytes + 65535UL) / 65536UL);
if (hp_banks > 96)
hp_banks = 96;
if (hp_used_banks == 0 && total_heap_bytes > 0)
hp_used_banks = 1;
if (hp_used_banks < 1)
hp_used_banks = 1;
if (hp_used_banks > hp_banks)
hp_used_banks = hp_banks;
uint32_t hp_ebb_dwords = (hp_banks + 31U) / 32U;
uint32_t hp_free_pages = (hp_banks - hp_used_banks) * 16U;
uint32_t hp_val_len = 12U + hp_ebb_dwords * 4U + hp_banks * sizeof(uint16_t);
if (off + 8U + hp_val_len <= cap) {
uint32_t ttype = 1;
memcpy(out + off, &ttype, 4); off += 4;
memcpy(out + off, &hp_val_len, 4); off += 4;
memcpy(out + off, &hp_free_pages, 4); off += 4;
memcpy(out + off, &hp_ebb_dwords, 4); off += 4;
for (uint32_t d = 0; d < hp_ebb_dwords; d++) {
uint32_t pgctl = 0xFFFFFFFFU;
for (uint32_t bit = 0; bit < 32U; bit++) {
if (d * 32U + bit < hp_used_banks)
pgctl &= ~(1U << bit);
}
memcpy(out + off, &pgctl, 4);
off += 4;
}
memcpy(out + off, &hp_banks, 4); off += 4;
for (uint32_t b = 0; b < hp_banks; b++) {
uint16_t amask = (b < hp_used_banks) ? 0xFFFF : 0x0000;
memcpy(out + off, &amask, 2);
off += 2;
}
}
/* Build LPSRAM (ttype = 0) */
size_t lp_total_bytes = (st && st->hw_cfg.lpsram_size_bytes >= 65536UL) ?
st->hw_cfg.lpsram_size_bytes : 65536UL;
size_t lp_used_bytes = st ? st->hw_cfg.lpsram_used_bytes : 0;
uint32_t lp_banks = (uint32_t)((lp_total_bytes + 65535UL) / 65536UL);
uint32_t lp_used_banks = (uint32_t)((lp_used_bytes + 65535UL) / 65536UL);
if (lp_banks > 32)
lp_banks = 32;
if (lp_used_banks > lp_banks)
lp_used_banks = lp_banks;
uint32_t lp_pgctl = (lp_used_banks >= 32) ? 0U : ~((1U << lp_used_banks) - 1U);
uint32_t lp_free_pages = (lp_banks - lp_used_banks) * 16U;
uint32_t lp_ebb_dwords = 1;
uint32_t lp_val_len = 16 + lp_banks * sizeof(uint16_t);
if (off + 8 + lp_val_len <= cap) {
uint32_t ttype = 0;
memcpy(out + off, &ttype, 4); off += 4;
memcpy(out + off, &lp_val_len, 4); off += 4;
memcpy(out + off, &lp_free_pages, 4); off += 4;
memcpy(out + off, &lp_ebb_dwords, 4); off += 4;
memcpy(out + off, &lp_pgctl, 4); off += 4;
memcpy(out + off, &lp_banks, 4); off += 4;
for (uint32_t b = 0; b < lp_banks; b++) {
uint16_t amask = (b < lp_used_banks) ? 0xFFFF : 0x0000;
memcpy(out + off, &amask, 2);
off += 2;
}
}
return off;
}
static uint32_t build_hw_config_tlv(uint8_t *out, uint32_t cap)
{
/*
* IPC4_HW_CONFIG_GET (param_id = 1) TLVs:
* 0: VER_HW_CFG (uint32_t)
* 1: DSP_CORES (uint32_t)
* 2: MEM_PAGE_BYTES (uint32_t = 4096)
* 3: TOTAL_PHYS_MEM_PAGES(uint32_t)
* 6: EBB_SIZE_BYTES (uint32_t = 65536)
* 7: EBB_TOTAL_COUNT (uint32_t = HPSRAM banks)
* 9: HP_EBB_COUNT (uint32_t = HPSRAM banks)
* 10: LP_EBB_COUNT (uint32_t = LPSRAM banks)
* 11: DSP_CLOCK_HZ (uint32_t = core 0 freq Hz)
*/
struct host_sched_telemetry *st = host_telem_get();
uint32_t hw_ver = st ? st->hw_cfg.hw_ip_version : 4;
uint32_t cores = st ? st->hw_cfg.num_cores : CONFIG_CORE_COUNT;
uint32_t page_sz = 4096;
uint32_t ebb_sz = 65536;
uint32_t hp_ebbs = st ? (uint32_t)(st->hw_cfg.hpsram_size_bytes / 65536UL) : 47;
uint32_t lp_ebbs = st ? (uint32_t)(st->hw_cfg.lpsram_size_bytes / 65536UL) : 1;
uint32_t tot_pages = (hp_ebbs + lp_ebbs) * 16U;
uint32_t clk_hz = st ? st->sim_cfg.core_freq_hz[0] : (uint32_t)TB_DSP_NOMINAL_HZ;
const uint32_t tuples[][2] = {
{ 0, hw_ver },
{ 1, cores },
{ 2, page_sz },
{ 3, tot_pages },
{ 6, ebb_sz },
{ 7, hp_ebbs },
{ 9, hp_ebbs },
{ 10, lp_ebbs },
{ 11, clk_hz },
};
uint32_t off = 0;
size_t i;
for (i = 0; i < ARRAY_SIZE(tuples); i++) {
uint32_t t = tuples[i][0];
uint32_t l = 4;
uint32_t v = tuples[i][1];
if (off + 12 > cap)
break;
memcpy(out + off, &t, 4); off += 4;
memcpy(out + off, &l, 4); off += 4;
memcpy(out + off, &v, 4); off += 4;
}
return off;
}
static uint32_t build_ext_perf_tlv(struct testbench_prm *tp, uint8_t *out, uint32_t cap)
{
/*
* Builds uint32_t count + struct perf_data_item_comp_wire[count] (32-byte stride)
* Also populates g_win3_perf for Window 3 reads.
*/
struct ipc *ipc = sof_get() ? sof_get()->ipc : NULL;
struct list_item *clist;
uint32_t count = 0;
uint32_t max_items = (cap > 4) ? ((cap - 4) / sizeof(struct perf_data_item_comp_wire)) : 0;
struct perf_data_item_comp_wire *items = (struct perf_data_item_comp_wire *)(out + 4);
struct perf_data_item_comp_wire *legacy_items =
(struct perf_data_item_comp_wire *)g_win3_legacy;
struct sof_tel_mw3_comp_perf *win3_items = (struct sof_tel_mw3_comp_perf *)g_win3_perf;
struct sof_tel_mod_hot *mod_hots =
(struct sof_tel_mod_hot *)(g_win3_perf + SOF_TEL_MW3_MOD_HOT_OFFSET);
struct sof_tel_mod_cold *mod_colds =
(struct sof_tel_mod_cold *)(g_win3_perf + SOF_TEL_MW3_MOD_COLD_OFFSET);
struct sof_tel_io_perf_item *io_items =
(struct sof_tel_io_perf_item *)(g_win3_perf + SOF_TEL_MW3_IO_PERF_OFFSET);
struct sof_tel_mw3_hdr *mw3_hdr =
(struct sof_tel_mw3_hdr *)(g_win3_perf + SOF_TEL_MW3_HDR_OFFSET);
uint32_t legacy_count = 0;
uint32_t win3_count = 0;
uint32_t io_count = 0;
uint32_t enabled_mask = arch_cpu_enabled_cores();
int c;
(void)tp;
if (max_items == 0)
return 0;
memset(out, 0, cap);
memset(g_win3_perf, 0, sizeof(g_win3_perf));
memset(g_win3_legacy, 0, sizeof(g_win3_legacy));
/* 1. Per-core summary items (module_id == 0, instance_id == core_id) */
struct host_sched_telemetry *st = host_telem_get();
for (c = 0; c < CONFIG_CORE_COUNT && count < max_items; c++) {
if (!(enabled_mask & BIT(c)) && !(st && st->cores[c].active))
continue;
struct perf_data_item_comp_wire *it = &items[count++];
it->resource_id = IPC4_COMP_ID(0, c);
it->power_mode = 0;
it->is_removed = 0;
if (st && st->cores[c].active) {
it->avg_kcps = st->cores[c].ll_avg_kcps + st->cores[c].dp_avg_kcps;
it->peak_kcps = st->cores[c].ll_peak_kcps + st->cores[c].dp_peak_kcps;
it->total_iteration_count = st->cores[c].ll_tick_count +
st->cores[c].dp_run_count;
it->total_cycles_consumed = st->cores[c].ll_total_cycles +
st->cores[c].dp_total_cycles;
} else {
it->avg_kcps = g_core_ticks[c].avg_utilization;
it->peak_kcps = g_core_ticks[c].peak_utilization;
it->total_iteration_count = g_core_ticks[c].count;
it->total_cycles_consumed = (uint64_t)g_core_ticks[c].count *
g_core_ticks[c].last_time_elapsed;
}
}
/* 2. Per-component items from active SOF components & real telemetry */
if (ipc) {
list_for_item(clist, &ipc->comp_list) {
struct ipc_comp_dev *icd = container_of(clist, struct ipc_comp_dev, list);
if (icd->type != COMP_TYPE_COMPONENT || !icd->cd)
continue;
if (count >= max_items)
break;
struct perf_data_item_comp_wire *it = &items[count++];
struct host_mod_telem *mt = NULL;
uint32_t i;
if (st) {
for (i = 0; i < st->num_mods; i++) {
if (st->mods[i].active &&
st->mods[i].comp_id == icd->id) {
mt = &st->mods[i];
break;
}
}
}
it->resource_id = icd->id;
it->power_mode = (icd->cd->ipc_config.proc_domain ==
COMP_PROCESSING_DOMAIN_DP) ? 1 : 0;
it->is_removed = 0;
if (mt && mt->iterations > 0) {
it->avg_kcps = mt->avg_kcps;
it->peak_kcps = mt->peak_kcps;
it->total_iteration_count = mt->iterations;
it->total_cycles_consumed = mt->total_cycles;
} else {
uint32_t core = icd->core < CONFIG_CORE_COUNT ? icd->core : 0;
uint32_t iters = g_core_ticks[core].count;
it->avg_kcps = 100;
it->peak_kcps = 200;
it->total_iteration_count = iters;
it->total_cycles_consumed = (uint64_t)iters * it->avg_kcps;
}
if (legacy_count < (sizeof(g_win3_legacy) / sizeof(*legacy_items)))
legacy_items[legacy_count++] = *it;
if (win3_count < SOF_TEL_MW3_MAX_COMP_ITEMS) {
uint32_t widx = win3_count++;
uint32_t mod_heap = (uint32_t)sizeof(struct comp_dev);
if (icd->cd->mod) {
size_t hwm = 0;
module_adapter_heap_usage(icd->cd->mod, &hwm);
if (hwm > 0)
mod_heap = (uint32_t)hwm;
}
win3_items[widx].resource_id = it->resource_id;
win3_items[widx].power_mode = it->power_mode;
win3_items[widx].rsvd =
(uint32_t)((it->total_iteration_count >> 32) & 0x3FFFFFFFU);
win3_items[widx].is_removed = it->is_removed;
win3_items[widx].peak_kcps = it->peak_kcps;
win3_items[widx].avg_kcps = it->avg_kcps;
win3_items[widx].total_iteration_count =
(uint32_t)it->total_iteration_count;
win3_items[widx].total_cycles_consumed =
it->total_cycles_consumed;
mod_hots[widx].cpc_violation_count =
mt ? (uint16_t)(mt->cpc_violations & 0xFFFFU) : 0;
mod_hots[widx].xrun_count =
mt ? (uint16_t)(mt->error_count & 0xFFFFU) : 0;
mod_colds[widx].resource_id = icd->id;
mod_colds[widx].heap_usage = mod_heap;
mod_colds[widx].heap_high_water_mark = mod_heap;
mod_colds[widx].configured_cpc =
(mt && mt->effective_cpc) ? mt->effective_cpc :
(mt && mt->cpc ? mt->cpc : 6400U);
if (icd->cd->ipc_config.type == SOF_COMP_HOST ||
icd->cd->ipc_config.type == SOF_COMP_DAI ||
icd->cd->ipc_config.type == SOF_COMP_FILEREAD ||
icd->cd->ipc_config.type == SOF_COMP_FILEWRITE) {
if (io_count < SOF_TEL_MW3_MAX_IO_ITEMS) {
io_items[io_count].resource_id = icd->id;
io_items[io_count].power_mode = it->power_mode;
io_items[io_count].perf_usage = it->avg_kcps;
io_count++;
}
}
}
}
}
/* Populate PMW2 footer directory at 0x1FC0 */
mw3_hdr->magic = SOF_TEL_MW3_MAGIC;
mw3_hdr->version = SOF_TEL_MW3_VERSION;
mw3_hdr->max_comp_entries = SOF_TEL_MW3_MAX_COMP_ITEMS;
mw3_hdr->max_io_entries = SOF_TEL_MW3_MAX_IO_ITEMS;
mw3_hdr->mod_hot_offset = SOF_TEL_MW3_MOD_HOT_OFFSET;
mw3_hdr->mod_cold_offset = SOF_TEL_MW3_MOD_COLD_OFFSET;
mw3_hdr->io_perf_offset = SOF_TEL_MW3_IO_PERF_OFFSET;
memcpy(out, &count, sizeof(uint32_t));
return 4 + count * sizeof(struct perf_data_item_comp_wire);
}
static void refresh_emulated_windows(struct testbench_prm *tp)
{