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>
28 #include "thread_map.h"
29 #include "thread-stack.h"
32 #define MAX_TIMESTAMP (~0ULL)
34 struct cs_etm_auxtrace {
35 struct auxtrace auxtrace;
36 struct auxtrace_queues queues;
37 struct auxtrace_heap heap;
38 struct itrace_synth_opts synth_opts;
39 struct perf_session *session;
40 struct machine *machine;
41 struct thread *unknown_thread;
50 u64 branches_sample_type;
54 unsigned int pmu_type;
58 struct cs_etm_auxtrace *etm;
59 struct thread *thread;
60 struct cs_etm_decoder *decoder;
61 struct auxtrace_buffer *buffer;
62 const struct cs_etm_state *state;
63 union perf_event *event_buf;
64 unsigned int queue_nr;
72 static int cs_etm__flush_events(struct perf_session *session,
73 struct perf_tool *tool)
80 static void cs_etm__free_queue(void *priv)
82 struct cs_etm_queue *etmq = priv;
87 static void cs_etm__free_events(struct perf_session *session)
90 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
91 struct cs_etm_auxtrace,
93 struct auxtrace_queues *queues = &aux->queues;
95 for (i = 0; i < queues->nr_queues; i++) {
96 cs_etm__free_queue(queues->queue_array[i].priv);
97 queues->queue_array[i].priv = NULL;
100 auxtrace_queues__free(queues);
103 static void cs_etm__free(struct perf_session *session)
106 struct int_node *inode, *tmp;
107 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
108 struct cs_etm_auxtrace,
110 cs_etm__free_events(session);
111 session->auxtrace = NULL;
113 /* First remove all traceID/CPU# nodes for the RB tree */
114 intlist__for_each_entry_safe(inode, tmp, traceid_list)
115 intlist__remove(traceid_list, inode);
116 /* Then the RB tree itself */
117 intlist__delete(traceid_list);
119 for (i = 0; i < aux->num_cpu; i++)
120 zfree(&aux->metadata[i]);
122 zfree(&aux->metadata);
126 static int cs_etm__process_event(struct perf_session *session,
127 union perf_event *event,
128 struct perf_sample *sample,
129 struct perf_tool *tool)
138 static int cs_etm__process_auxtrace_event(struct perf_session *session,
139 union perf_event *event,
140 struct perf_tool *tool)
148 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
150 struct perf_evsel *evsel;
151 struct perf_evlist *evlist = etm->session->evlist;
152 bool timeless_decoding = true;
155 * Circle through the list of event and complain if we find one
156 * with the time bit set.
158 evlist__for_each_entry(evlist, evsel) {
159 if ((evsel->attr.sample_type & PERF_SAMPLE_TIME))
160 timeless_decoding = false;
163 return timeless_decoding;
166 static const char * const cs_etm_global_header_fmts[] = {
167 [CS_HEADER_VERSION_0] = " Header version %llx\n",
168 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
169 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
172 static const char * const cs_etm_priv_fmts[] = {
173 [CS_ETM_MAGIC] = " Magic number %llx\n",
174 [CS_ETM_CPU] = " CPU %lld\n",
175 [CS_ETM_ETMCR] = " ETMCR %llx\n",
176 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
177 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
178 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
181 static const char * const cs_etmv4_priv_fmts[] = {
182 [CS_ETM_MAGIC] = " Magic number %llx\n",
183 [CS_ETM_CPU] = " CPU %lld\n",
184 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
185 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
186 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
187 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
188 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
189 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
190 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
193 static void cs_etm__print_auxtrace_info(u64 *val, int num)
197 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
198 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
200 for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
201 if (val[i] == __perf_cs_etmv3_magic)
202 for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
203 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
204 else if (val[i] == __perf_cs_etmv4_magic)
205 for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
206 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
208 /* failure.. return */
213 int cs_etm__process_auxtrace_info(union perf_event *event,
214 struct perf_session *session)
216 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
217 struct cs_etm_auxtrace *etm = NULL;
218 struct int_node *inode;
219 unsigned int pmu_type;
220 int event_header_size = sizeof(struct perf_event_header);
221 int info_header_size;
222 int total_size = auxtrace_info->header.size;
225 int err = 0, idx = -1;
227 u64 *ptr, *hdr = NULL;
228 u64 **metadata = NULL;
231 * sizeof(auxtrace_info_event::type) +
232 * sizeof(auxtrace_info_event::reserved) == 8
234 info_header_size = 8;
236 if (total_size < (event_header_size + info_header_size))
239 priv_size = total_size - event_header_size - info_header_size;
241 /* First the global part */
242 ptr = (u64 *) auxtrace_info->priv;
244 /* Look for version '0' of the header */
248 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
252 /* Extract header information - see cs-etm.h for format */
253 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
255 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
256 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
260 * Create an RB tree for traceID-CPU# tuple. Since the conversion has
261 * to be made for each packet that gets decoded, optimizing access in
262 * anything other than a sequential array is worth doing.
264 traceid_list = intlist__new(NULL);
270 metadata = zalloc(sizeof(*metadata) * num_cpu);
273 goto err_free_traceid_list;
277 * The metadata is stored in the auxtrace_info section and encodes
278 * the configuration of the ARM embedded trace macrocell which is
279 * required by the trace decoder to properly decode the trace due
280 * to its highly compressed nature.
282 for (j = 0; j < num_cpu; j++) {
283 if (ptr[i] == __perf_cs_etmv3_magic) {
284 metadata[j] = zalloc(sizeof(*metadata[j]) *
288 goto err_free_metadata;
290 for (k = 0; k < CS_ETM_PRIV_MAX; k++)
291 metadata[j][k] = ptr[i + k];
293 /* The traceID is our handle */
294 idx = metadata[j][CS_ETM_ETMTRACEIDR];
295 i += CS_ETM_PRIV_MAX;
296 } else if (ptr[i] == __perf_cs_etmv4_magic) {
297 metadata[j] = zalloc(sizeof(*metadata[j]) *
301 goto err_free_metadata;
303 for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
304 metadata[j][k] = ptr[i + k];
306 /* The traceID is our handle */
307 idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
308 i += CS_ETMV4_PRIV_MAX;
311 /* Get an RB node for this CPU */
312 inode = intlist__findnew(traceid_list, idx);
314 /* Something went wrong, no need to continue */
316 err = PTR_ERR(inode);
317 goto err_free_metadata;
321 * The node for that CPU should not be taken.
322 * Back out if that's the case.
326 goto err_free_metadata;
328 /* All good, associate the traceID with the CPU# */
329 inode->priv = &metadata[j][CS_ETM_CPU];
333 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
334 * CS_ETMV4_PRIV_MAX mark how many double words are in the
335 * global metadata, and each cpu's metadata respectively.
336 * The following tests if the correct number of double words was
337 * present in the auxtrace info section.
339 if (i * 8 != priv_size) {
341 goto err_free_metadata;
344 etm = zalloc(sizeof(*etm));
348 goto err_free_metadata;
351 err = auxtrace_queues__init(&etm->queues);
355 etm->session = session;
356 etm->machine = &session->machines.host;
358 etm->num_cpu = num_cpu;
359 etm->pmu_type = pmu_type;
360 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
361 etm->metadata = metadata;
362 etm->auxtrace_type = auxtrace_info->type;
363 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
365 etm->auxtrace.process_event = cs_etm__process_event;
366 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
367 etm->auxtrace.flush_events = cs_etm__flush_events;
368 etm->auxtrace.free_events = cs_etm__free_events;
369 etm->auxtrace.free = cs_etm__free;
370 session->auxtrace = &etm->auxtrace;
373 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
377 err = auxtrace_queues__process_index(&etm->queues, session);
379 goto err_free_queues;
381 etm->data_queued = etm->queues.populated;
386 auxtrace_queues__free(&etm->queues);
387 session->auxtrace = NULL;
391 /* No need to check @metadata[j], free(NULL) is supported */
392 for (j = 0; j < num_cpu; j++)
395 err_free_traceid_list:
396 intlist__delete(traceid_list);