xref: /openbmc/linux/tools/perf/util/cs-etm-base.c (revision faffb083)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * File for any parts of the Coresight decoding that don't require
4  * OpenCSD.
5  */
6 
7 #include <errno.h>
8 #include <inttypes.h>
9 
10 #include "cs-etm.h"
11 
12 static const char * const cs_etm_global_header_fmts[] = {
13 	[CS_HEADER_VERSION]	= "	Header version		       %llx\n",
14 	[CS_PMU_TYPE_CPUS]	= "	PMU type/num cpus	       %llx\n",
15 	[CS_ETM_SNAPSHOT]	= "	Snapshot		       %llx\n",
16 };
17 
18 static const char * const cs_etm_priv_fmts[] = {
19 	[CS_ETM_MAGIC]		= "	Magic number		       %llx\n",
20 	[CS_ETM_CPU]		= "	CPU			       %lld\n",
21 	[CS_ETM_NR_TRC_PARAMS]	= "	NR_TRC_PARAMS		       %llx\n",
22 	[CS_ETM_ETMCR]		= "	ETMCR			       %llx\n",
23 	[CS_ETM_ETMTRACEIDR]	= "	ETMTRACEIDR		       %llx\n",
24 	[CS_ETM_ETMCCER]	= "	ETMCCER			       %llx\n",
25 	[CS_ETM_ETMIDR]		= "	ETMIDR			       %llx\n",
26 };
27 
28 static const char * const cs_etmv4_priv_fmts[] = {
29 	[CS_ETM_MAGIC]		= "	Magic number		       %llx\n",
30 	[CS_ETM_CPU]		= "	CPU			       %lld\n",
31 	[CS_ETM_NR_TRC_PARAMS]	= "	NR_TRC_PARAMS		       %llx\n",
32 	[CS_ETMV4_TRCCONFIGR]	= "	TRCCONFIGR		       %llx\n",
33 	[CS_ETMV4_TRCTRACEIDR]	= "	TRCTRACEIDR		       %llx\n",
34 	[CS_ETMV4_TRCIDR0]	= "	TRCIDR0			       %llx\n",
35 	[CS_ETMV4_TRCIDR1]	= "	TRCIDR1			       %llx\n",
36 	[CS_ETMV4_TRCIDR2]	= "	TRCIDR2			       %llx\n",
37 	[CS_ETMV4_TRCIDR8]	= "	TRCIDR8			       %llx\n",
38 	[CS_ETMV4_TRCAUTHSTATUS] = "	TRCAUTHSTATUS		       %llx\n",
39 	[CS_ETE_TRCDEVARCH]	= "	TRCDEVARCH                     %llx\n"
40 };
41 
42 static const char * const param_unk_fmt =
43 	"	Unknown parameter [%d]	       %"PRIx64"\n";
44 static const char * const magic_unk_fmt =
45 	"	Magic number Unknown	       %"PRIx64"\n";
46 
47 static int cs_etm__print_cpu_metadata_v0(u64 *val, int *offset)
48 {
49 	int i = *offset, j, nr_params = 0, fmt_offset;
50 	u64 magic;
51 
52 	/* check magic value */
53 	magic = val[i + CS_ETM_MAGIC];
54 	if ((magic != __perf_cs_etmv3_magic) &&
55 	    (magic != __perf_cs_etmv4_magic)) {
56 		/* failure - note bad magic value */
57 		fprintf(stdout, magic_unk_fmt, magic);
58 		return -EINVAL;
59 	}
60 
61 	/* print common header block */
62 	fprintf(stdout, cs_etm_priv_fmts[CS_ETM_MAGIC], val[i++]);
63 	fprintf(stdout, cs_etm_priv_fmts[CS_ETM_CPU], val[i++]);
64 
65 	if (magic == __perf_cs_etmv3_magic) {
66 		nr_params = CS_ETM_NR_TRC_PARAMS_V0;
67 		fmt_offset = CS_ETM_ETMCR;
68 		/* after common block, offset format index past NR_PARAMS */
69 		for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
70 			fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
71 	} else if (magic == __perf_cs_etmv4_magic) {
72 		nr_params = CS_ETMV4_NR_TRC_PARAMS_V0;
73 		fmt_offset = CS_ETMV4_TRCCONFIGR;
74 		/* after common block, offset format index past NR_PARAMS */
75 		for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
76 			fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
77 	}
78 	*offset = i;
79 	return 0;
80 }
81 
82 static int cs_etm__print_cpu_metadata_v1(u64 *val, int *offset)
83 {
84 	int i = *offset, j, total_params = 0;
85 	u64 magic;
86 
87 	magic = val[i + CS_ETM_MAGIC];
88 	/* total params to print is NR_PARAMS + common block size for v1 */
89 	total_params = val[i + CS_ETM_NR_TRC_PARAMS] + CS_ETM_COMMON_BLK_MAX_V1;
90 
91 	if (magic == __perf_cs_etmv3_magic) {
92 		for (j = 0; j < total_params; j++, i++) {
93 			/* if newer record - could be excess params */
94 			if (j >= CS_ETM_PRIV_MAX)
95 				fprintf(stdout, param_unk_fmt, j, val[i]);
96 			else
97 				fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
98 		}
99 	} else if (magic == __perf_cs_etmv4_magic || magic == __perf_cs_ete_magic) {
100 		/*
101 		 * ETE and ETMv4 can be printed in the same block because the number of parameters
102 		 * is saved and they share the list of parameter names. ETE is also only supported
103 		 * in V1 files.
104 		 */
105 		for (j = 0; j < total_params; j++, i++) {
106 			/* if newer record - could be excess params */
107 			if (j >= CS_ETE_PRIV_MAX)
108 				fprintf(stdout, param_unk_fmt, j, val[i]);
109 			else
110 				fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
111 		}
112 	} else {
113 		/* failure - note bad magic value and error out */
114 		fprintf(stdout, magic_unk_fmt, magic);
115 		return -EINVAL;
116 	}
117 	*offset = i;
118 	return 0;
119 }
120 
121 static void cs_etm__print_auxtrace_info(u64 *val, int num)
122 {
123 	int i, cpu = 0, version, err;
124 
125 	version = val[0];
126 
127 	for (i = 0; i < CS_HEADER_VERSION_MAX; i++)
128 		fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
129 
130 	for (i = CS_HEADER_VERSION_MAX; cpu < num; cpu++) {
131 		if (version == 0)
132 			err = cs_etm__print_cpu_metadata_v0(val, &i);
133 		else if (version == 1)
134 			err = cs_etm__print_cpu_metadata_v1(val, &i);
135 		if (err)
136 			return;
137 	}
138 }
139 
140 /*
141  * Do some basic checks and print the auxtrace info header before calling
142  * into cs_etm__process_auxtrace_info_full() which requires OpenCSD to be
143  * linked in. This allows some basic debugging if OpenCSD is missing.
144  */
145 int cs_etm__process_auxtrace_info(union perf_event *event,
146 				  struct perf_session *session)
147 {
148 	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
149 	int event_header_size = sizeof(struct perf_event_header);
150 	int num_cpu;
151 	u64 *ptr = NULL;
152 	u64 hdr_version;
153 
154 	if (auxtrace_info->header.size < (event_header_size + INFO_HEADER_SIZE))
155 		return -EINVAL;
156 
157 	/* First the global part */
158 	ptr = (u64 *) auxtrace_info->priv;
159 
160 	/* Look for version of the header */
161 	hdr_version = ptr[0];
162 	if (hdr_version > CS_HEADER_CURRENT_VERSION) {
163 		pr_err("\nCS ETM Trace: Unknown Header Version = %#" PRIx64, hdr_version);
164 		pr_err(", version supported <= %x\n", CS_HEADER_CURRENT_VERSION);
165 		return -EINVAL;
166 	}
167 
168 	if (dump_trace) {
169 		num_cpu = ptr[CS_PMU_TYPE_CPUS] & 0xffffffff;
170 		cs_etm__print_auxtrace_info(ptr, num_cpu);
171 	}
172 
173 	return cs_etm__process_auxtrace_info_full(event, session);
174 }
175