2 * SPDX-License-Identifier: GPL-2.0
4 * Copyright(C) 2015-2018 Linaro Limited.
6 * Author: Tor Jeremiassen <tor@ti.com>
7 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
10 #include <linux/bitops.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/types.h>
21 #include "cs-etm-decoder/cs-etm-decoder.h"
29 #include "thread_map.h"
30 #include "thread-stack.h"
33 #define MAX_TIMESTAMP (~0ULL)
35 struct cs_etm_auxtrace {
36 struct auxtrace auxtrace;
37 struct auxtrace_queues queues;
38 struct auxtrace_heap heap;
39 struct itrace_synth_opts synth_opts;
40 struct perf_session *session;
41 struct machine *machine;
42 struct thread *unknown_thread;
51 u64 branches_sample_type;
55 unsigned int pmu_type;
59 struct cs_etm_auxtrace *etm;
60 struct thread *thread;
61 struct cs_etm_decoder *decoder;
62 struct auxtrace_buffer *buffer;
63 const struct cs_etm_state *state;
64 union perf_event *event_buf;
65 unsigned int queue_nr;
73 static void cs_etm__packet_dump(const char *pkt_string)
75 const char *color = PERF_COLOR_BLUE;
76 int len = strlen(pkt_string);
78 if (len && (pkt_string[len-1] == '\n'))
79 color_fprintf(stdout, color, " %s", pkt_string);
81 color_fprintf(stdout, color, " %s\n", pkt_string);
86 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
87 struct auxtrace_buffer *buffer)
90 const char *color = PERF_COLOR_BLUE;
91 struct cs_etm_decoder_params d_params;
92 struct cs_etm_trace_params *t_params;
93 struct cs_etm_decoder *decoder;
94 size_t buffer_used = 0;
96 fprintf(stdout, "\n");
97 color_fprintf(stdout, color,
98 ". ... CoreSight ETM Trace data: size %zu bytes\n",
101 /* Use metadata to fill in trace parameters for trace decoder */
102 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
103 for (i = 0; i < etm->num_cpu; i++) {
104 t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
105 t_params[i].etmv4.reg_idr0 = etm->metadata[i][CS_ETMV4_TRCIDR0];
106 t_params[i].etmv4.reg_idr1 = etm->metadata[i][CS_ETMV4_TRCIDR1];
107 t_params[i].etmv4.reg_idr2 = etm->metadata[i][CS_ETMV4_TRCIDR2];
108 t_params[i].etmv4.reg_idr8 = etm->metadata[i][CS_ETMV4_TRCIDR8];
109 t_params[i].etmv4.reg_configr =
110 etm->metadata[i][CS_ETMV4_TRCCONFIGR];
111 t_params[i].etmv4.reg_traceidr =
112 etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
115 /* Set decoder parameters to simply print the trace packets */
116 d_params.packet_printer = cs_etm__packet_dump;
117 d_params.operation = CS_ETM_OPERATION_PRINT;
118 d_params.formatted = true;
119 d_params.fsyncs = false;
120 d_params.hsyncs = false;
121 d_params.frame_aligned = true;
123 decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
132 ret = cs_etm_decoder__process_data_block(
133 decoder, buffer->offset,
134 &((u8 *)buffer->data)[buffer_used],
135 buffer->size - buffer_used, &consumed);
139 buffer_used += consumed;
140 } while (buffer_used < buffer->size);
142 cs_etm_decoder__free(decoder);
145 static int cs_etm__flush_events(struct perf_session *session,
146 struct perf_tool *tool)
153 static void cs_etm__free_queue(void *priv)
155 struct cs_etm_queue *etmq = priv;
160 static void cs_etm__free_events(struct perf_session *session)
163 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
164 struct cs_etm_auxtrace,
166 struct auxtrace_queues *queues = &aux->queues;
168 for (i = 0; i < queues->nr_queues; i++) {
169 cs_etm__free_queue(queues->queue_array[i].priv);
170 queues->queue_array[i].priv = NULL;
173 auxtrace_queues__free(queues);
176 static void cs_etm__free(struct perf_session *session)
179 struct int_node *inode, *tmp;
180 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
181 struct cs_etm_auxtrace,
183 cs_etm__free_events(session);
184 session->auxtrace = NULL;
186 /* First remove all traceID/CPU# nodes for the RB tree */
187 intlist__for_each_entry_safe(inode, tmp, traceid_list)
188 intlist__remove(traceid_list, inode);
189 /* Then the RB tree itself */
190 intlist__delete(traceid_list);
192 for (i = 0; i < aux->num_cpu; i++)
193 zfree(&aux->metadata[i]);
195 zfree(&aux->metadata);
199 static int cs_etm__process_event(struct perf_session *session,
200 union perf_event *event,
201 struct perf_sample *sample,
202 struct perf_tool *tool)
211 static int cs_etm__process_auxtrace_event(struct perf_session *session,
212 union perf_event *event,
213 struct perf_tool *tool __maybe_unused)
215 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
216 struct cs_etm_auxtrace,
218 if (!etm->data_queued) {
219 struct auxtrace_buffer *buffer;
221 int fd = perf_data__fd(session->data);
222 bool is_pipe = perf_data__is_pipe(session->data);
228 data_offset = lseek(fd, 0, SEEK_CUR);
229 if (data_offset == -1)
233 err = auxtrace_queues__add_event(&etm->queues, session,
234 event, data_offset, &buffer);
239 if (auxtrace_buffer__get_data(buffer, fd)) {
240 cs_etm__dump_event(etm, buffer);
241 auxtrace_buffer__put_data(buffer);
248 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
250 struct perf_evsel *evsel;
251 struct perf_evlist *evlist = etm->session->evlist;
252 bool timeless_decoding = true;
255 * Circle through the list of event and complain if we find one
256 * with the time bit set.
258 evlist__for_each_entry(evlist, evsel) {
259 if ((evsel->attr.sample_type & PERF_SAMPLE_TIME))
260 timeless_decoding = false;
263 return timeless_decoding;
266 static const char * const cs_etm_global_header_fmts[] = {
267 [CS_HEADER_VERSION_0] = " Header version %llx\n",
268 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
269 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
272 static const char * const cs_etm_priv_fmts[] = {
273 [CS_ETM_MAGIC] = " Magic number %llx\n",
274 [CS_ETM_CPU] = " CPU %lld\n",
275 [CS_ETM_ETMCR] = " ETMCR %llx\n",
276 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
277 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
278 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
281 static const char * const cs_etmv4_priv_fmts[] = {
282 [CS_ETM_MAGIC] = " Magic number %llx\n",
283 [CS_ETM_CPU] = " CPU %lld\n",
284 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
285 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
286 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
287 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
288 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
289 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
290 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
293 static void cs_etm__print_auxtrace_info(u64 *val, int num)
297 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
298 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
300 for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
301 if (val[i] == __perf_cs_etmv3_magic)
302 for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
303 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
304 else if (val[i] == __perf_cs_etmv4_magic)
305 for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
306 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
308 /* failure.. return */
313 int cs_etm__process_auxtrace_info(union perf_event *event,
314 struct perf_session *session)
316 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
317 struct cs_etm_auxtrace *etm = NULL;
318 struct int_node *inode;
319 unsigned int pmu_type;
320 int event_header_size = sizeof(struct perf_event_header);
321 int info_header_size;
322 int total_size = auxtrace_info->header.size;
325 int err = 0, idx = -1;
327 u64 *ptr, *hdr = NULL;
328 u64 **metadata = NULL;
331 * sizeof(auxtrace_info_event::type) +
332 * sizeof(auxtrace_info_event::reserved) == 8
334 info_header_size = 8;
336 if (total_size < (event_header_size + info_header_size))
339 priv_size = total_size - event_header_size - info_header_size;
341 /* First the global part */
342 ptr = (u64 *) auxtrace_info->priv;
344 /* Look for version '0' of the header */
348 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
352 /* Extract header information - see cs-etm.h for format */
353 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
355 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
356 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
360 * Create an RB tree for traceID-CPU# tuple. Since the conversion has
361 * to be made for each packet that gets decoded, optimizing access in
362 * anything other than a sequential array is worth doing.
364 traceid_list = intlist__new(NULL);
370 metadata = zalloc(sizeof(*metadata) * num_cpu);
373 goto err_free_traceid_list;
377 * The metadata is stored in the auxtrace_info section and encodes
378 * the configuration of the ARM embedded trace macrocell which is
379 * required by the trace decoder to properly decode the trace due
380 * to its highly compressed nature.
382 for (j = 0; j < num_cpu; j++) {
383 if (ptr[i] == __perf_cs_etmv3_magic) {
384 metadata[j] = zalloc(sizeof(*metadata[j]) *
388 goto err_free_metadata;
390 for (k = 0; k < CS_ETM_PRIV_MAX; k++)
391 metadata[j][k] = ptr[i + k];
393 /* The traceID is our handle */
394 idx = metadata[j][CS_ETM_ETMTRACEIDR];
395 i += CS_ETM_PRIV_MAX;
396 } else if (ptr[i] == __perf_cs_etmv4_magic) {
397 metadata[j] = zalloc(sizeof(*metadata[j]) *
401 goto err_free_metadata;
403 for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
404 metadata[j][k] = ptr[i + k];
406 /* The traceID is our handle */
407 idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
408 i += CS_ETMV4_PRIV_MAX;
411 /* Get an RB node for this CPU */
412 inode = intlist__findnew(traceid_list, idx);
414 /* Something went wrong, no need to continue */
416 err = PTR_ERR(inode);
417 goto err_free_metadata;
421 * The node for that CPU should not be taken.
422 * Back out if that's the case.
426 goto err_free_metadata;
428 /* All good, associate the traceID with the CPU# */
429 inode->priv = &metadata[j][CS_ETM_CPU];
433 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
434 * CS_ETMV4_PRIV_MAX mark how many double words are in the
435 * global metadata, and each cpu's metadata respectively.
436 * The following tests if the correct number of double words was
437 * present in the auxtrace info section.
439 if (i * 8 != priv_size) {
441 goto err_free_metadata;
444 etm = zalloc(sizeof(*etm));
448 goto err_free_metadata;
451 err = auxtrace_queues__init(&etm->queues);
455 etm->session = session;
456 etm->machine = &session->machines.host;
458 etm->num_cpu = num_cpu;
459 etm->pmu_type = pmu_type;
460 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
461 etm->metadata = metadata;
462 etm->auxtrace_type = auxtrace_info->type;
463 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
465 etm->auxtrace.process_event = cs_etm__process_event;
466 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
467 etm->auxtrace.flush_events = cs_etm__flush_events;
468 etm->auxtrace.free_events = cs_etm__free_events;
469 etm->auxtrace.free = cs_etm__free;
470 session->auxtrace = &etm->auxtrace;
473 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
477 err = auxtrace_queues__process_index(&etm->queues, session);
479 goto err_free_queues;
481 etm->data_queued = etm->queues.populated;
486 auxtrace_queues__free(&etm->queues);
487 session->auxtrace = NULL;
491 /* No need to check @metadata[j], free(NULL) is supported */
492 for (j = 0; j < num_cpu; j++)
495 err_free_traceid_list:
496 intlist__delete(traceid_list);