xref: /openbmc/linux/tools/perf/util/cs-etm.c (revision 2e4555b0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(C) 2015-2018 Linaro Limited.
4  *
5  * Author: Tor Jeremiassen <tor@ti.com>
6  * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/coresight-pmu.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/types.h>
15 #include <linux/zalloc.h>
16 
17 #include <opencsd/ocsd_if_types.h>
18 #include <stdlib.h>
19 
20 #include "auxtrace.h"
21 #include "color.h"
22 #include "cs-etm.h"
23 #include "cs-etm-decoder/cs-etm-decoder.h"
24 #include "debug.h"
25 #include "dso.h"
26 #include "evlist.h"
27 #include "intlist.h"
28 #include "machine.h"
29 #include "map.h"
30 #include "perf.h"
31 #include "session.h"
32 #include "map_symbol.h"
33 #include "branch.h"
34 #include "symbol.h"
35 #include "tool.h"
36 #include "thread.h"
37 #include "thread-stack.h"
38 #include "tsc.h"
39 #include <tools/libc_compat.h>
40 #include "util/synthetic-events.h"
41 
42 struct cs_etm_auxtrace {
43 	struct auxtrace auxtrace;
44 	struct auxtrace_queues queues;
45 	struct auxtrace_heap heap;
46 	struct itrace_synth_opts synth_opts;
47 	struct perf_session *session;
48 	struct machine *machine;
49 	struct thread *unknown_thread;
50 	struct perf_tsc_conversion tc;
51 
52 	u8 timeless_decoding;
53 	u8 snapshot_mode;
54 	u8 data_queued;
55 	u8 has_virtual_ts; /* Virtual/Kernel timestamps in the trace. */
56 
57 	int num_cpu;
58 	u64 latest_kernel_timestamp;
59 	u32 auxtrace_type;
60 	u64 branches_sample_type;
61 	u64 branches_id;
62 	u64 instructions_sample_type;
63 	u64 instructions_sample_period;
64 	u64 instructions_id;
65 	u64 **metadata;
66 	unsigned int pmu_type;
67 };
68 
69 struct cs_etm_traceid_queue {
70 	u8 trace_chan_id;
71 	pid_t pid, tid;
72 	u64 period_instructions;
73 	size_t last_branch_pos;
74 	union perf_event *event_buf;
75 	struct thread *thread;
76 	struct branch_stack *last_branch;
77 	struct branch_stack *last_branch_rb;
78 	struct cs_etm_packet *prev_packet;
79 	struct cs_etm_packet *packet;
80 	struct cs_etm_packet_queue packet_queue;
81 };
82 
83 struct cs_etm_queue {
84 	struct cs_etm_auxtrace *etm;
85 	struct cs_etm_decoder *decoder;
86 	struct auxtrace_buffer *buffer;
87 	unsigned int queue_nr;
88 	u8 pending_timestamp_chan_id;
89 	u64 offset;
90 	const unsigned char *buf;
91 	size_t buf_len, buf_used;
92 	/* Conversion between traceID and index in traceid_queues array */
93 	struct intlist *traceid_queues_list;
94 	struct cs_etm_traceid_queue **traceid_queues;
95 };
96 
97 /* RB tree for quick conversion between traceID and metadata pointers */
98 static struct intlist *traceid_list;
99 
100 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm);
101 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
102 					   pid_t tid);
103 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
104 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
105 
106 /* PTMs ETMIDR [11:8] set to b0011 */
107 #define ETMIDR_PTM_VERSION 0x00000300
108 
109 /*
110  * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
111  * work with.  One option is to modify to auxtrace_heap_XYZ() API or simply
112  * encode the etm queue number as the upper 16 bit and the channel as
113  * the lower 16 bit.
114  */
115 #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id)	\
116 		      (queue_nr << 16 | trace_chan_id)
117 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
118 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
119 
120 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
121 {
122 	etmidr &= ETMIDR_PTM_VERSION;
123 
124 	if (etmidr == ETMIDR_PTM_VERSION)
125 		return CS_ETM_PROTO_PTM;
126 
127 	return CS_ETM_PROTO_ETMV3;
128 }
129 
130 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
131 {
132 	struct int_node *inode;
133 	u64 *metadata;
134 
135 	inode = intlist__find(traceid_list, trace_chan_id);
136 	if (!inode)
137 		return -EINVAL;
138 
139 	metadata = inode->priv;
140 	*magic = metadata[CS_ETM_MAGIC];
141 	return 0;
142 }
143 
144 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
145 {
146 	struct int_node *inode;
147 	u64 *metadata;
148 
149 	inode = intlist__find(traceid_list, trace_chan_id);
150 	if (!inode)
151 		return -EINVAL;
152 
153 	metadata = inode->priv;
154 	*cpu = (int)metadata[CS_ETM_CPU];
155 	return 0;
156 }
157 
158 /*
159  * The returned PID format is presented by two bits:
160  *
161  *   Bit ETM_OPT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced;
162  *   Bit ETM_OPT_CTXTID2: CONTEXTIDR_EL2 is traced.
163  *
164  * It's possible that the two bits ETM_OPT_CTXTID and ETM_OPT_CTXTID2
165  * are enabled at the same time when the session runs on an EL2 kernel.
166  * This means the CONTEXTIDR_EL1 and CONTEXTIDR_EL2 both will be
167  * recorded in the trace data, the tool will selectively use
168  * CONTEXTIDR_EL2 as PID.
169  */
170 int cs_etm__get_pid_fmt(u8 trace_chan_id, u64 *pid_fmt)
171 {
172 	struct int_node *inode;
173 	u64 *metadata, val;
174 
175 	inode = intlist__find(traceid_list, trace_chan_id);
176 	if (!inode)
177 		return -EINVAL;
178 
179 	metadata = inode->priv;
180 
181 	if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
182 		val = metadata[CS_ETM_ETMCR];
183 		/* CONTEXTIDR is traced */
184 		if (val & BIT(ETM_OPT_CTXTID))
185 			*pid_fmt = BIT(ETM_OPT_CTXTID);
186 	} else {
187 		val = metadata[CS_ETMV4_TRCCONFIGR];
188 		/* CONTEXTIDR_EL2 is traced */
189 		if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT)))
190 			*pid_fmt = BIT(ETM_OPT_CTXTID2);
191 		/* CONTEXTIDR_EL1 is traced */
192 		else if (val & BIT(ETM4_CFG_BIT_CTXTID))
193 			*pid_fmt = BIT(ETM_OPT_CTXTID);
194 	}
195 
196 	return 0;
197 }
198 
199 static int cs_etm__map_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
200 {
201 	struct int_node *inode;
202 
203 	/* Get an RB node for this CPU */
204 	inode = intlist__findnew(traceid_list, trace_chan_id);
205 
206 	/* Something went wrong, no need to continue */
207 	if (!inode)
208 		return -ENOMEM;
209 
210 	/*
211 	 * The node for that CPU should not be taken.
212 	 * Back out if that's the case.
213 	 */
214 	if (inode->priv)
215 		return -EINVAL;
216 
217 	/* All good, associate the traceID with the metadata pointer */
218 	inode->priv = cpu_metadata;
219 
220 	return 0;
221 }
222 
223 static int cs_etm__metadata_get_trace_id(u8 *trace_chan_id, u64 *cpu_metadata)
224 {
225 	u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
226 
227 	switch (cs_etm_magic) {
228 	case __perf_cs_etmv3_magic:
229 		*trace_chan_id = (u8)(cpu_metadata[CS_ETM_ETMTRACEIDR] &
230 				      CORESIGHT_TRACE_ID_VAL_MASK);
231 		break;
232 	case __perf_cs_etmv4_magic:
233 	case __perf_cs_ete_magic:
234 		*trace_chan_id = (u8)(cpu_metadata[CS_ETMV4_TRCTRACEIDR] &
235 				      CORESIGHT_TRACE_ID_VAL_MASK);
236 		break;
237 	default:
238 		return -EINVAL;
239 	}
240 	return 0;
241 }
242 
243 /*
244  * update metadata trace ID from the value found in the AUX_HW_INFO packet.
245  * This will also clear the CORESIGHT_TRACE_ID_UNUSED_FLAG flag if present.
246  */
247 static int cs_etm__metadata_set_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
248 {
249 	u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
250 
251 	switch (cs_etm_magic) {
252 	case __perf_cs_etmv3_magic:
253 		 cpu_metadata[CS_ETM_ETMTRACEIDR] = trace_chan_id;
254 		break;
255 	case __perf_cs_etmv4_magic:
256 	case __perf_cs_ete_magic:
257 		cpu_metadata[CS_ETMV4_TRCTRACEIDR] = trace_chan_id;
258 		break;
259 
260 	default:
261 		return -EINVAL;
262 	}
263 	return 0;
264 }
265 
266 /*
267  * FIELD_GET (linux/bitfield.h) not available outside kernel code,
268  * and the header contains too many dependencies to just copy over,
269  * so roll our own based on the original
270  */
271 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
272 #define FIELD_GET(_mask, _reg)						\
273 	({								\
274 		(typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
275 	})
276 
277 /*
278  * Handle the PERF_RECORD_AUX_OUTPUT_HW_ID event.
279  *
280  * The payload associates the Trace ID and the CPU.
281  * The routine is tolerant of seeing multiple packets with the same association,
282  * but a CPU / Trace ID association changing during a session is an error.
283  */
284 static int cs_etm__process_aux_output_hw_id(struct perf_session *session,
285 					    union perf_event *event)
286 {
287 	struct cs_etm_auxtrace *etm;
288 	struct perf_sample sample;
289 	struct int_node *inode;
290 	struct evsel *evsel;
291 	u64 *cpu_data;
292 	u64 hw_id;
293 	int cpu, version, err;
294 	u8 trace_chan_id, curr_chan_id;
295 
296 	/* extract and parse the HW ID */
297 	hw_id = event->aux_output_hw_id.hw_id;
298 	version = FIELD_GET(CS_AUX_HW_ID_VERSION_MASK, hw_id);
299 	trace_chan_id = FIELD_GET(CS_AUX_HW_ID_TRACE_ID_MASK, hw_id);
300 
301 	/* check that we can handle this version */
302 	if (version > CS_AUX_HW_ID_CURR_VERSION)
303 		return -EINVAL;
304 
305 	/* get access to the etm metadata */
306 	etm = container_of(session->auxtrace, struct cs_etm_auxtrace, auxtrace);
307 	if (!etm || !etm->metadata)
308 		return -EINVAL;
309 
310 	/* parse the sample to get the CPU */
311 	evsel = evlist__event2evsel(session->evlist, event);
312 	if (!evsel)
313 		return -EINVAL;
314 	err = evsel__parse_sample(evsel, event, &sample);
315 	if (err)
316 		return err;
317 	cpu = sample.cpu;
318 	if (cpu == -1) {
319 		/* no CPU in the sample - possibly recorded with an old version of perf */
320 		pr_err("CS_ETM: no CPU AUX_OUTPUT_HW_ID sample. Use compatible perf to record.");
321 		return -EINVAL;
322 	}
323 
324 	/* See if the ID is mapped to a CPU, and it matches the current CPU */
325 	inode = intlist__find(traceid_list, trace_chan_id);
326 	if (inode) {
327 		cpu_data = inode->priv;
328 		if ((int)cpu_data[CS_ETM_CPU] != cpu) {
329 			pr_err("CS_ETM: map mismatch between HW_ID packet CPU and Trace ID\n");
330 			return -EINVAL;
331 		}
332 
333 		/* check that the mapped ID matches */
334 		err = cs_etm__metadata_get_trace_id(&curr_chan_id, cpu_data);
335 		if (err)
336 			return err;
337 		if (curr_chan_id != trace_chan_id) {
338 			pr_err("CS_ETM: mismatch between CPU trace ID and HW_ID packet ID\n");
339 			return -EINVAL;
340 		}
341 
342 		/* mapped and matched - return OK */
343 		return 0;
344 	}
345 
346 	/* not one we've seen before - lets map it */
347 	cpu_data = etm->metadata[cpu];
348 	err = cs_etm__map_trace_id(trace_chan_id, cpu_data);
349 	if (err)
350 		return err;
351 
352 	/*
353 	 * if we are picking up the association from the packet, need to plug
354 	 * the correct trace ID into the metadata for setting up decoders later.
355 	 */
356 	err = cs_etm__metadata_set_trace_id(trace_chan_id, cpu_data);
357 	return err;
358 }
359 
360 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
361 					      u8 trace_chan_id)
362 {
363 	/*
364 	 * When a timestamp packet is encountered the backend code
365 	 * is stopped so that the front end has time to process packets
366 	 * that were accumulated in the traceID queue.  Since there can
367 	 * be more than one channel per cs_etm_queue, we need to specify
368 	 * what traceID queue needs servicing.
369 	 */
370 	etmq->pending_timestamp_chan_id = trace_chan_id;
371 }
372 
373 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
374 				      u8 *trace_chan_id)
375 {
376 	struct cs_etm_packet_queue *packet_queue;
377 
378 	if (!etmq->pending_timestamp_chan_id)
379 		return 0;
380 
381 	if (trace_chan_id)
382 		*trace_chan_id = etmq->pending_timestamp_chan_id;
383 
384 	packet_queue = cs_etm__etmq_get_packet_queue(etmq,
385 						     etmq->pending_timestamp_chan_id);
386 	if (!packet_queue)
387 		return 0;
388 
389 	/* Acknowledge pending status */
390 	etmq->pending_timestamp_chan_id = 0;
391 
392 	/* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
393 	return packet_queue->cs_timestamp;
394 }
395 
396 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
397 {
398 	int i;
399 
400 	queue->head = 0;
401 	queue->tail = 0;
402 	queue->packet_count = 0;
403 	for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
404 		queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
405 		queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
406 		queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
407 		queue->packet_buffer[i].instr_count = 0;
408 		queue->packet_buffer[i].last_instr_taken_branch = false;
409 		queue->packet_buffer[i].last_instr_size = 0;
410 		queue->packet_buffer[i].last_instr_type = 0;
411 		queue->packet_buffer[i].last_instr_subtype = 0;
412 		queue->packet_buffer[i].last_instr_cond = 0;
413 		queue->packet_buffer[i].flags = 0;
414 		queue->packet_buffer[i].exception_number = UINT32_MAX;
415 		queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
416 		queue->packet_buffer[i].cpu = INT_MIN;
417 	}
418 }
419 
420 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
421 {
422 	int idx;
423 	struct int_node *inode;
424 	struct cs_etm_traceid_queue *tidq;
425 	struct intlist *traceid_queues_list = etmq->traceid_queues_list;
426 
427 	intlist__for_each_entry(inode, traceid_queues_list) {
428 		idx = (int)(intptr_t)inode->priv;
429 		tidq = etmq->traceid_queues[idx];
430 		cs_etm__clear_packet_queue(&tidq->packet_queue);
431 	}
432 }
433 
434 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
435 				      struct cs_etm_traceid_queue *tidq,
436 				      u8 trace_chan_id)
437 {
438 	int rc = -ENOMEM;
439 	struct auxtrace_queue *queue;
440 	struct cs_etm_auxtrace *etm = etmq->etm;
441 
442 	cs_etm__clear_packet_queue(&tidq->packet_queue);
443 
444 	queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
445 	tidq->tid = queue->tid;
446 	tidq->pid = -1;
447 	tidq->trace_chan_id = trace_chan_id;
448 
449 	tidq->packet = zalloc(sizeof(struct cs_etm_packet));
450 	if (!tidq->packet)
451 		goto out;
452 
453 	tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
454 	if (!tidq->prev_packet)
455 		goto out_free;
456 
457 	if (etm->synth_opts.last_branch) {
458 		size_t sz = sizeof(struct branch_stack);
459 
460 		sz += etm->synth_opts.last_branch_sz *
461 		      sizeof(struct branch_entry);
462 		tidq->last_branch = zalloc(sz);
463 		if (!tidq->last_branch)
464 			goto out_free;
465 		tidq->last_branch_rb = zalloc(sz);
466 		if (!tidq->last_branch_rb)
467 			goto out_free;
468 	}
469 
470 	tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
471 	if (!tidq->event_buf)
472 		goto out_free;
473 
474 	return 0;
475 
476 out_free:
477 	zfree(&tidq->last_branch_rb);
478 	zfree(&tidq->last_branch);
479 	zfree(&tidq->prev_packet);
480 	zfree(&tidq->packet);
481 out:
482 	return rc;
483 }
484 
485 static struct cs_etm_traceid_queue
486 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
487 {
488 	int idx;
489 	struct int_node *inode;
490 	struct intlist *traceid_queues_list;
491 	struct cs_etm_traceid_queue *tidq, **traceid_queues;
492 	struct cs_etm_auxtrace *etm = etmq->etm;
493 
494 	if (etm->timeless_decoding)
495 		trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
496 
497 	traceid_queues_list = etmq->traceid_queues_list;
498 
499 	/*
500 	 * Check if the traceid_queue exist for this traceID by looking
501 	 * in the queue list.
502 	 */
503 	inode = intlist__find(traceid_queues_list, trace_chan_id);
504 	if (inode) {
505 		idx = (int)(intptr_t)inode->priv;
506 		return etmq->traceid_queues[idx];
507 	}
508 
509 	/* We couldn't find a traceid_queue for this traceID, allocate one */
510 	tidq = malloc(sizeof(*tidq));
511 	if (!tidq)
512 		return NULL;
513 
514 	memset(tidq, 0, sizeof(*tidq));
515 
516 	/* Get a valid index for the new traceid_queue */
517 	idx = intlist__nr_entries(traceid_queues_list);
518 	/* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
519 	inode = intlist__findnew(traceid_queues_list, trace_chan_id);
520 	if (!inode)
521 		goto out_free;
522 
523 	/* Associate this traceID with this index */
524 	inode->priv = (void *)(intptr_t)idx;
525 
526 	if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
527 		goto out_free;
528 
529 	/* Grow the traceid_queues array by one unit */
530 	traceid_queues = etmq->traceid_queues;
531 	traceid_queues = reallocarray(traceid_queues,
532 				      idx + 1,
533 				      sizeof(*traceid_queues));
534 
535 	/*
536 	 * On failure reallocarray() returns NULL and the original block of
537 	 * memory is left untouched.
538 	 */
539 	if (!traceid_queues)
540 		goto out_free;
541 
542 	traceid_queues[idx] = tidq;
543 	etmq->traceid_queues = traceid_queues;
544 
545 	return etmq->traceid_queues[idx];
546 
547 out_free:
548 	/*
549 	 * Function intlist__remove() removes the inode from the list
550 	 * and delete the memory associated to it.
551 	 */
552 	intlist__remove(traceid_queues_list, inode);
553 	free(tidq);
554 
555 	return NULL;
556 }
557 
558 struct cs_etm_packet_queue
559 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
560 {
561 	struct cs_etm_traceid_queue *tidq;
562 
563 	tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
564 	if (tidq)
565 		return &tidq->packet_queue;
566 
567 	return NULL;
568 }
569 
570 static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
571 				struct cs_etm_traceid_queue *tidq)
572 {
573 	struct cs_etm_packet *tmp;
574 
575 	if (etm->synth_opts.branches || etm->synth_opts.last_branch ||
576 	    etm->synth_opts.instructions) {
577 		/*
578 		 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
579 		 * the next incoming packet.
580 		 */
581 		tmp = tidq->packet;
582 		tidq->packet = tidq->prev_packet;
583 		tidq->prev_packet = tmp;
584 	}
585 }
586 
587 static void cs_etm__packet_dump(const char *pkt_string)
588 {
589 	const char *color = PERF_COLOR_BLUE;
590 	int len = strlen(pkt_string);
591 
592 	if (len && (pkt_string[len-1] == '\n'))
593 		color_fprintf(stdout, color, "	%s", pkt_string);
594 	else
595 		color_fprintf(stdout, color, "	%s\n", pkt_string);
596 
597 	fflush(stdout);
598 }
599 
600 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
601 					  struct cs_etm_auxtrace *etm, int idx,
602 					  u32 etmidr)
603 {
604 	u64 **metadata = etm->metadata;
605 
606 	t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
607 	t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
608 	t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
609 }
610 
611 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
612 					  struct cs_etm_auxtrace *etm, int idx)
613 {
614 	u64 **metadata = etm->metadata;
615 
616 	t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
617 	t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
618 	t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
619 	t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
620 	t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
621 	t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
622 	t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
623 }
624 
625 static void cs_etm__set_trace_param_ete(struct cs_etm_trace_params *t_params,
626 					  struct cs_etm_auxtrace *etm, int idx)
627 {
628 	u64 **metadata = etm->metadata;
629 
630 	t_params[idx].protocol = CS_ETM_PROTO_ETE;
631 	t_params[idx].ete.reg_idr0 = metadata[idx][CS_ETE_TRCIDR0];
632 	t_params[idx].ete.reg_idr1 = metadata[idx][CS_ETE_TRCIDR1];
633 	t_params[idx].ete.reg_idr2 = metadata[idx][CS_ETE_TRCIDR2];
634 	t_params[idx].ete.reg_idr8 = metadata[idx][CS_ETE_TRCIDR8];
635 	t_params[idx].ete.reg_configr = metadata[idx][CS_ETE_TRCCONFIGR];
636 	t_params[idx].ete.reg_traceidr = metadata[idx][CS_ETE_TRCTRACEIDR];
637 	t_params[idx].ete.reg_devarch = metadata[idx][CS_ETE_TRCDEVARCH];
638 }
639 
640 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
641 				     struct cs_etm_auxtrace *etm,
642 				     int decoders)
643 {
644 	int i;
645 	u32 etmidr;
646 	u64 architecture;
647 
648 	for (i = 0; i < decoders; i++) {
649 		architecture = etm->metadata[i][CS_ETM_MAGIC];
650 
651 		switch (architecture) {
652 		case __perf_cs_etmv3_magic:
653 			etmidr = etm->metadata[i][CS_ETM_ETMIDR];
654 			cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
655 			break;
656 		case __perf_cs_etmv4_magic:
657 			cs_etm__set_trace_param_etmv4(t_params, etm, i);
658 			break;
659 		case __perf_cs_ete_magic:
660 			cs_etm__set_trace_param_ete(t_params, etm, i);
661 			break;
662 		default:
663 			return -EINVAL;
664 		}
665 	}
666 
667 	return 0;
668 }
669 
670 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
671 				       struct cs_etm_queue *etmq,
672 				       enum cs_etm_decoder_operation mode,
673 				       bool formatted)
674 {
675 	int ret = -EINVAL;
676 
677 	if (!(mode < CS_ETM_OPERATION_MAX))
678 		goto out;
679 
680 	d_params->packet_printer = cs_etm__packet_dump;
681 	d_params->operation = mode;
682 	d_params->data = etmq;
683 	d_params->formatted = formatted;
684 	d_params->fsyncs = false;
685 	d_params->hsyncs = false;
686 	d_params->frame_aligned = true;
687 
688 	ret = 0;
689 out:
690 	return ret;
691 }
692 
693 static void cs_etm__dump_event(struct cs_etm_queue *etmq,
694 			       struct auxtrace_buffer *buffer)
695 {
696 	int ret;
697 	const char *color = PERF_COLOR_BLUE;
698 	size_t buffer_used = 0;
699 
700 	fprintf(stdout, "\n");
701 	color_fprintf(stdout, color,
702 		     ". ... CoreSight %s Trace data: size %#zx bytes\n",
703 		     cs_etm_decoder__get_name(etmq->decoder), buffer->size);
704 
705 	do {
706 		size_t consumed;
707 
708 		ret = cs_etm_decoder__process_data_block(
709 				etmq->decoder, buffer->offset,
710 				&((u8 *)buffer->data)[buffer_used],
711 				buffer->size - buffer_used, &consumed);
712 		if (ret)
713 			break;
714 
715 		buffer_used += consumed;
716 	} while (buffer_used < buffer->size);
717 
718 	cs_etm_decoder__reset(etmq->decoder);
719 }
720 
721 static int cs_etm__flush_events(struct perf_session *session,
722 				struct perf_tool *tool)
723 {
724 	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
725 						   struct cs_etm_auxtrace,
726 						   auxtrace);
727 	if (dump_trace)
728 		return 0;
729 
730 	if (!tool->ordered_events)
731 		return -EINVAL;
732 
733 	if (etm->timeless_decoding)
734 		return cs_etm__process_timeless_queues(etm, -1);
735 
736 	return cs_etm__process_queues(etm);
737 }
738 
739 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
740 {
741 	int idx;
742 	uintptr_t priv;
743 	struct int_node *inode, *tmp;
744 	struct cs_etm_traceid_queue *tidq;
745 	struct intlist *traceid_queues_list = etmq->traceid_queues_list;
746 
747 	intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
748 		priv = (uintptr_t)inode->priv;
749 		idx = priv;
750 
751 		/* Free this traceid_queue from the array */
752 		tidq = etmq->traceid_queues[idx];
753 		thread__zput(tidq->thread);
754 		zfree(&tidq->event_buf);
755 		zfree(&tidq->last_branch);
756 		zfree(&tidq->last_branch_rb);
757 		zfree(&tidq->prev_packet);
758 		zfree(&tidq->packet);
759 		zfree(&tidq);
760 
761 		/*
762 		 * Function intlist__remove() removes the inode from the list
763 		 * and delete the memory associated to it.
764 		 */
765 		intlist__remove(traceid_queues_list, inode);
766 	}
767 
768 	/* Then the RB tree itself */
769 	intlist__delete(traceid_queues_list);
770 	etmq->traceid_queues_list = NULL;
771 
772 	/* finally free the traceid_queues array */
773 	zfree(&etmq->traceid_queues);
774 }
775 
776 static void cs_etm__free_queue(void *priv)
777 {
778 	struct cs_etm_queue *etmq = priv;
779 
780 	if (!etmq)
781 		return;
782 
783 	cs_etm_decoder__free(etmq->decoder);
784 	cs_etm__free_traceid_queues(etmq);
785 	free(etmq);
786 }
787 
788 static void cs_etm__free_events(struct perf_session *session)
789 {
790 	unsigned int i;
791 	struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
792 						   struct cs_etm_auxtrace,
793 						   auxtrace);
794 	struct auxtrace_queues *queues = &aux->queues;
795 
796 	for (i = 0; i < queues->nr_queues; i++) {
797 		cs_etm__free_queue(queues->queue_array[i].priv);
798 		queues->queue_array[i].priv = NULL;
799 	}
800 
801 	auxtrace_queues__free(queues);
802 }
803 
804 static void cs_etm__free(struct perf_session *session)
805 {
806 	int i;
807 	struct int_node *inode, *tmp;
808 	struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
809 						   struct cs_etm_auxtrace,
810 						   auxtrace);
811 	cs_etm__free_events(session);
812 	session->auxtrace = NULL;
813 
814 	/* First remove all traceID/metadata nodes for the RB tree */
815 	intlist__for_each_entry_safe(inode, tmp, traceid_list)
816 		intlist__remove(traceid_list, inode);
817 	/* Then the RB tree itself */
818 	intlist__delete(traceid_list);
819 
820 	for (i = 0; i < aux->num_cpu; i++)
821 		zfree(&aux->metadata[i]);
822 
823 	thread__zput(aux->unknown_thread);
824 	zfree(&aux->metadata);
825 	zfree(&aux);
826 }
827 
828 static bool cs_etm__evsel_is_auxtrace(struct perf_session *session,
829 				      struct evsel *evsel)
830 {
831 	struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
832 						   struct cs_etm_auxtrace,
833 						   auxtrace);
834 
835 	return evsel->core.attr.type == aux->pmu_type;
836 }
837 
838 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
839 {
840 	struct machine *machine;
841 
842 	machine = etmq->etm->machine;
843 
844 	if (address >= machine__kernel_start(machine)) {
845 		if (machine__is_host(machine))
846 			return PERF_RECORD_MISC_KERNEL;
847 		else
848 			return PERF_RECORD_MISC_GUEST_KERNEL;
849 	} else {
850 		if (machine__is_host(machine))
851 			return PERF_RECORD_MISC_USER;
852 		else if (perf_guest)
853 			return PERF_RECORD_MISC_GUEST_USER;
854 		else
855 			return PERF_RECORD_MISC_HYPERVISOR;
856 	}
857 }
858 
859 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
860 			      u64 address, size_t size, u8 *buffer)
861 {
862 	u8  cpumode;
863 	u64 offset;
864 	int len;
865 	struct thread *thread;
866 	struct machine *machine;
867 	struct addr_location al;
868 	struct dso *dso;
869 	struct cs_etm_traceid_queue *tidq;
870 
871 	if (!etmq)
872 		return 0;
873 
874 	machine = etmq->etm->machine;
875 	cpumode = cs_etm__cpu_mode(etmq, address);
876 	tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
877 	if (!tidq)
878 		return 0;
879 
880 	thread = tidq->thread;
881 	if (!thread) {
882 		if (cpumode != PERF_RECORD_MISC_KERNEL)
883 			return 0;
884 		thread = etmq->etm->unknown_thread;
885 	}
886 
887 	dso = map__dso(al.map);
888 
889 	if (!thread__find_map(thread, cpumode, address, &al) || !dso)
890 		return 0;
891 
892 	if (dso->data.status == DSO_DATA_STATUS_ERROR &&
893 	    dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))
894 		return 0;
895 
896 	offset = al.map->map_ip(al.map, address);
897 
898 	map__load(al.map);
899 
900 	len = dso__data_read_offset(dso, machine, offset, buffer, size);
901 
902 	if (len <= 0) {
903 		ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
904 				 "              Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
905 		if (!dso->auxtrace_warned) {
906 			pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
907 				    address,
908 				    dso->long_name ? dso->long_name : "Unknown");
909 			dso->auxtrace_warned = true;
910 		}
911 		return 0;
912 	}
913 
914 	return len;
915 }
916 
917 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
918 						bool formatted)
919 {
920 	struct cs_etm_decoder_params d_params;
921 	struct cs_etm_trace_params  *t_params = NULL;
922 	struct cs_etm_queue *etmq;
923 	/*
924 	 * Each queue can only contain data from one CPU when unformatted, so only one decoder is
925 	 * needed.
926 	 */
927 	int decoders = formatted ? etm->num_cpu : 1;
928 
929 	etmq = zalloc(sizeof(*etmq));
930 	if (!etmq)
931 		return NULL;
932 
933 	etmq->traceid_queues_list = intlist__new(NULL);
934 	if (!etmq->traceid_queues_list)
935 		goto out_free;
936 
937 	/* Use metadata to fill in trace parameters for trace decoder */
938 	t_params = zalloc(sizeof(*t_params) * decoders);
939 
940 	if (!t_params)
941 		goto out_free;
942 
943 	if (cs_etm__init_trace_params(t_params, etm, decoders))
944 		goto out_free;
945 
946 	/* Set decoder parameters to decode trace packets */
947 	if (cs_etm__init_decoder_params(&d_params, etmq,
948 					dump_trace ? CS_ETM_OPERATION_PRINT :
949 						     CS_ETM_OPERATION_DECODE,
950 					formatted))
951 		goto out_free;
952 
953 	etmq->decoder = cs_etm_decoder__new(decoders, &d_params,
954 					    t_params);
955 
956 	if (!etmq->decoder)
957 		goto out_free;
958 
959 	/*
960 	 * Register a function to handle all memory accesses required by
961 	 * the trace decoder library.
962 	 */
963 	if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
964 					      0x0L, ((u64) -1L),
965 					      cs_etm__mem_access))
966 		goto out_free_decoder;
967 
968 	zfree(&t_params);
969 	return etmq;
970 
971 out_free_decoder:
972 	cs_etm_decoder__free(etmq->decoder);
973 out_free:
974 	intlist__delete(etmq->traceid_queues_list);
975 	free(etmq);
976 
977 	return NULL;
978 }
979 
980 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
981 			       struct auxtrace_queue *queue,
982 			       unsigned int queue_nr,
983 			       bool formatted)
984 {
985 	struct cs_etm_queue *etmq = queue->priv;
986 
987 	if (list_empty(&queue->head) || etmq)
988 		return 0;
989 
990 	etmq = cs_etm__alloc_queue(etm, formatted);
991 
992 	if (!etmq)
993 		return -ENOMEM;
994 
995 	queue->priv = etmq;
996 	etmq->etm = etm;
997 	etmq->queue_nr = queue_nr;
998 	etmq->offset = 0;
999 
1000 	return 0;
1001 }
1002 
1003 static int cs_etm__queue_first_cs_timestamp(struct cs_etm_auxtrace *etm,
1004 					    struct cs_etm_queue *etmq,
1005 					    unsigned int queue_nr)
1006 {
1007 	int ret = 0;
1008 	unsigned int cs_queue_nr;
1009 	u8 trace_chan_id;
1010 	u64 cs_timestamp;
1011 
1012 	/*
1013 	 * We are under a CPU-wide trace scenario.  As such we need to know
1014 	 * when the code that generated the traces started to execute so that
1015 	 * it can be correlated with execution on other CPUs.  So we get a
1016 	 * handle on the beginning of traces and decode until we find a
1017 	 * timestamp.  The timestamp is then added to the auxtrace min heap
1018 	 * in order to know what nibble (of all the etmqs) to decode first.
1019 	 */
1020 	while (1) {
1021 		/*
1022 		 * Fetch an aux_buffer from this etmq.  Bail if no more
1023 		 * blocks or an error has been encountered.
1024 		 */
1025 		ret = cs_etm__get_data_block(etmq);
1026 		if (ret <= 0)
1027 			goto out;
1028 
1029 		/*
1030 		 * Run decoder on the trace block.  The decoder will stop when
1031 		 * encountering a CS timestamp, a full packet queue or the end of
1032 		 * trace for that block.
1033 		 */
1034 		ret = cs_etm__decode_data_block(etmq);
1035 		if (ret)
1036 			goto out;
1037 
1038 		/*
1039 		 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
1040 		 * the timestamp calculation for us.
1041 		 */
1042 		cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
1043 
1044 		/* We found a timestamp, no need to continue. */
1045 		if (cs_timestamp)
1046 			break;
1047 
1048 		/*
1049 		 * We didn't find a timestamp so empty all the traceid packet
1050 		 * queues before looking for another timestamp packet, either
1051 		 * in the current data block or a new one.  Packets that were
1052 		 * just decoded are useless since no timestamp has been
1053 		 * associated with them.  As such simply discard them.
1054 		 */
1055 		cs_etm__clear_all_packet_queues(etmq);
1056 	}
1057 
1058 	/*
1059 	 * We have a timestamp.  Add it to the min heap to reflect when
1060 	 * instructions conveyed by the range packets of this traceID queue
1061 	 * started to execute.  Once the same has been done for all the traceID
1062 	 * queues of each etmq, redenring and decoding can start in
1063 	 * chronological order.
1064 	 *
1065 	 * Note that packets decoded above are still in the traceID's packet
1066 	 * queue and will be processed in cs_etm__process_queues().
1067 	 */
1068 	cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
1069 	ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
1070 out:
1071 	return ret;
1072 }
1073 
1074 static inline
1075 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
1076 				 struct cs_etm_traceid_queue *tidq)
1077 {
1078 	struct branch_stack *bs_src = tidq->last_branch_rb;
1079 	struct branch_stack *bs_dst = tidq->last_branch;
1080 	size_t nr = 0;
1081 
1082 	/*
1083 	 * Set the number of records before early exit: ->nr is used to
1084 	 * determine how many branches to copy from ->entries.
1085 	 */
1086 	bs_dst->nr = bs_src->nr;
1087 
1088 	/*
1089 	 * Early exit when there is nothing to copy.
1090 	 */
1091 	if (!bs_src->nr)
1092 		return;
1093 
1094 	/*
1095 	 * As bs_src->entries is a circular buffer, we need to copy from it in
1096 	 * two steps.  First, copy the branches from the most recently inserted
1097 	 * branch ->last_branch_pos until the end of bs_src->entries buffer.
1098 	 */
1099 	nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
1100 	memcpy(&bs_dst->entries[0],
1101 	       &bs_src->entries[tidq->last_branch_pos],
1102 	       sizeof(struct branch_entry) * nr);
1103 
1104 	/*
1105 	 * If we wrapped around at least once, the branches from the beginning
1106 	 * of the bs_src->entries buffer and until the ->last_branch_pos element
1107 	 * are older valid branches: copy them over.  The total number of
1108 	 * branches copied over will be equal to the number of branches asked by
1109 	 * the user in last_branch_sz.
1110 	 */
1111 	if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
1112 		memcpy(&bs_dst->entries[nr],
1113 		       &bs_src->entries[0],
1114 		       sizeof(struct branch_entry) * tidq->last_branch_pos);
1115 	}
1116 }
1117 
1118 static inline
1119 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
1120 {
1121 	tidq->last_branch_pos = 0;
1122 	tidq->last_branch_rb->nr = 0;
1123 }
1124 
1125 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
1126 					 u8 trace_chan_id, u64 addr)
1127 {
1128 	u8 instrBytes[2];
1129 
1130 	cs_etm__mem_access(etmq, trace_chan_id, addr,
1131 			   ARRAY_SIZE(instrBytes), instrBytes);
1132 	/*
1133 	 * T32 instruction size is indicated by bits[15:11] of the first
1134 	 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
1135 	 * denote a 32-bit instruction.
1136 	 */
1137 	return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
1138 }
1139 
1140 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
1141 {
1142 	/* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1143 	if (packet->sample_type == CS_ETM_DISCONTINUITY)
1144 		return 0;
1145 
1146 	return packet->start_addr;
1147 }
1148 
1149 static inline
1150 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
1151 {
1152 	/* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1153 	if (packet->sample_type == CS_ETM_DISCONTINUITY)
1154 		return 0;
1155 
1156 	return packet->end_addr - packet->last_instr_size;
1157 }
1158 
1159 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
1160 				     u64 trace_chan_id,
1161 				     const struct cs_etm_packet *packet,
1162 				     u64 offset)
1163 {
1164 	if (packet->isa == CS_ETM_ISA_T32) {
1165 		u64 addr = packet->start_addr;
1166 
1167 		while (offset) {
1168 			addr += cs_etm__t32_instr_size(etmq,
1169 						       trace_chan_id, addr);
1170 			offset--;
1171 		}
1172 		return addr;
1173 	}
1174 
1175 	/* Assume a 4 byte instruction size (A32/A64) */
1176 	return packet->start_addr + offset * 4;
1177 }
1178 
1179 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
1180 					  struct cs_etm_traceid_queue *tidq)
1181 {
1182 	struct branch_stack *bs = tidq->last_branch_rb;
1183 	struct branch_entry *be;
1184 
1185 	/*
1186 	 * The branches are recorded in a circular buffer in reverse
1187 	 * chronological order: we start recording from the last element of the
1188 	 * buffer down.  After writing the first element of the stack, move the
1189 	 * insert position back to the end of the buffer.
1190 	 */
1191 	if (!tidq->last_branch_pos)
1192 		tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1193 
1194 	tidq->last_branch_pos -= 1;
1195 
1196 	be       = &bs->entries[tidq->last_branch_pos];
1197 	be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1198 	be->to	 = cs_etm__first_executed_instr(tidq->packet);
1199 	/* No support for mispredict */
1200 	be->flags.mispred = 0;
1201 	be->flags.predicted = 1;
1202 
1203 	/*
1204 	 * Increment bs->nr until reaching the number of last branches asked by
1205 	 * the user on the command line.
1206 	 */
1207 	if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1208 		bs->nr += 1;
1209 }
1210 
1211 static int cs_etm__inject_event(union perf_event *event,
1212 			       struct perf_sample *sample, u64 type)
1213 {
1214 	event->header.size = perf_event__sample_event_size(sample, type, 0);
1215 	return perf_event__synthesize_sample(event, type, 0, sample);
1216 }
1217 
1218 
1219 static int
1220 cs_etm__get_trace(struct cs_etm_queue *etmq)
1221 {
1222 	struct auxtrace_buffer *aux_buffer = etmq->buffer;
1223 	struct auxtrace_buffer *old_buffer = aux_buffer;
1224 	struct auxtrace_queue *queue;
1225 
1226 	queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1227 
1228 	aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1229 
1230 	/* If no more data, drop the previous auxtrace_buffer and return */
1231 	if (!aux_buffer) {
1232 		if (old_buffer)
1233 			auxtrace_buffer__drop_data(old_buffer);
1234 		etmq->buf_len = 0;
1235 		return 0;
1236 	}
1237 
1238 	etmq->buffer = aux_buffer;
1239 
1240 	/* If the aux_buffer doesn't have data associated, try to load it */
1241 	if (!aux_buffer->data) {
1242 		/* get the file desc associated with the perf data file */
1243 		int fd = perf_data__fd(etmq->etm->session->data);
1244 
1245 		aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1246 		if (!aux_buffer->data)
1247 			return -ENOMEM;
1248 	}
1249 
1250 	/* If valid, drop the previous buffer */
1251 	if (old_buffer)
1252 		auxtrace_buffer__drop_data(old_buffer);
1253 
1254 	etmq->buf_used = 0;
1255 	etmq->buf_len = aux_buffer->size;
1256 	etmq->buf = aux_buffer->data;
1257 
1258 	return etmq->buf_len;
1259 }
1260 
1261 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
1262 				    struct cs_etm_traceid_queue *tidq)
1263 {
1264 	if ((!tidq->thread) && (tidq->tid != -1))
1265 		tidq->thread = machine__find_thread(etm->machine, -1,
1266 						    tidq->tid);
1267 
1268 	if (tidq->thread)
1269 		tidq->pid = tidq->thread->pid_;
1270 }
1271 
1272 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1273 			 pid_t tid, u8 trace_chan_id)
1274 {
1275 	int cpu, err = -EINVAL;
1276 	struct cs_etm_auxtrace *etm = etmq->etm;
1277 	struct cs_etm_traceid_queue *tidq;
1278 
1279 	tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1280 	if (!tidq)
1281 		return err;
1282 
1283 	if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1284 		return err;
1285 
1286 	err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1287 	if (err)
1288 		return err;
1289 
1290 	tidq->tid = tid;
1291 	thread__zput(tidq->thread);
1292 
1293 	cs_etm__set_pid_tid_cpu(etm, tidq);
1294 	return 0;
1295 }
1296 
1297 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1298 {
1299 	return !!etmq->etm->timeless_decoding;
1300 }
1301 
1302 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1303 			      u64 trace_chan_id,
1304 			      const struct cs_etm_packet *packet,
1305 			      struct perf_sample *sample)
1306 {
1307 	/*
1308 	 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1309 	 * packet, so directly bail out with 'insn_len' = 0.
1310 	 */
1311 	if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1312 		sample->insn_len = 0;
1313 		return;
1314 	}
1315 
1316 	/*
1317 	 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1318 	 * cs_etm__t32_instr_size().
1319 	 */
1320 	if (packet->isa == CS_ETM_ISA_T32)
1321 		sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1322 							  sample->ip);
1323 	/* Otherwise, A64 and A32 instruction size are always 32-bit. */
1324 	else
1325 		sample->insn_len = 4;
1326 
1327 	cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1328 			   sample->insn_len, (void *)sample->insn);
1329 }
1330 
1331 u64 cs_etm__convert_sample_time(struct cs_etm_queue *etmq, u64 cs_timestamp)
1332 {
1333 	struct cs_etm_auxtrace *etm = etmq->etm;
1334 
1335 	if (etm->has_virtual_ts)
1336 		return tsc_to_perf_time(cs_timestamp, &etm->tc);
1337 	else
1338 		return cs_timestamp;
1339 }
1340 
1341 static inline u64 cs_etm__resolve_sample_time(struct cs_etm_queue *etmq,
1342 					       struct cs_etm_traceid_queue *tidq)
1343 {
1344 	struct cs_etm_auxtrace *etm = etmq->etm;
1345 	struct cs_etm_packet_queue *packet_queue = &tidq->packet_queue;
1346 
1347 	if (etm->timeless_decoding)
1348 		return 0;
1349 	else if (etm->has_virtual_ts)
1350 		return packet_queue->cs_timestamp;
1351 	else
1352 		return etm->latest_kernel_timestamp;
1353 }
1354 
1355 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1356 					    struct cs_etm_traceid_queue *tidq,
1357 					    u64 addr, u64 period)
1358 {
1359 	int ret = 0;
1360 	struct cs_etm_auxtrace *etm = etmq->etm;
1361 	union perf_event *event = tidq->event_buf;
1362 	struct perf_sample sample = {.ip = 0,};
1363 
1364 	event->sample.header.type = PERF_RECORD_SAMPLE;
1365 	event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1366 	event->sample.header.size = sizeof(struct perf_event_header);
1367 
1368 	/* Set time field based on etm auxtrace config. */
1369 	sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1370 
1371 	sample.ip = addr;
1372 	sample.pid = tidq->pid;
1373 	sample.tid = tidq->tid;
1374 	sample.id = etmq->etm->instructions_id;
1375 	sample.stream_id = etmq->etm->instructions_id;
1376 	sample.period = period;
1377 	sample.cpu = tidq->packet->cpu;
1378 	sample.flags = tidq->prev_packet->flags;
1379 	sample.cpumode = event->sample.header.misc;
1380 
1381 	cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1382 
1383 	if (etm->synth_opts.last_branch)
1384 		sample.branch_stack = tidq->last_branch;
1385 
1386 	if (etm->synth_opts.inject) {
1387 		ret = cs_etm__inject_event(event, &sample,
1388 					   etm->instructions_sample_type);
1389 		if (ret)
1390 			return ret;
1391 	}
1392 
1393 	ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1394 
1395 	if (ret)
1396 		pr_err(
1397 			"CS ETM Trace: failed to deliver instruction event, error %d\n",
1398 			ret);
1399 
1400 	return ret;
1401 }
1402 
1403 /*
1404  * The cs etm packet encodes an instruction range between a branch target
1405  * and the next taken branch. Generate sample accordingly.
1406  */
1407 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1408 				       struct cs_etm_traceid_queue *tidq)
1409 {
1410 	int ret = 0;
1411 	struct cs_etm_auxtrace *etm = etmq->etm;
1412 	struct perf_sample sample = {.ip = 0,};
1413 	union perf_event *event = tidq->event_buf;
1414 	struct dummy_branch_stack {
1415 		u64			nr;
1416 		u64			hw_idx;
1417 		struct branch_entry	entries;
1418 	} dummy_bs;
1419 	u64 ip;
1420 
1421 	ip = cs_etm__last_executed_instr(tidq->prev_packet);
1422 
1423 	event->sample.header.type = PERF_RECORD_SAMPLE;
1424 	event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1425 	event->sample.header.size = sizeof(struct perf_event_header);
1426 
1427 	/* Set time field based on etm auxtrace config. */
1428 	sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1429 
1430 	sample.ip = ip;
1431 	sample.pid = tidq->pid;
1432 	sample.tid = tidq->tid;
1433 	sample.addr = cs_etm__first_executed_instr(tidq->packet);
1434 	sample.id = etmq->etm->branches_id;
1435 	sample.stream_id = etmq->etm->branches_id;
1436 	sample.period = 1;
1437 	sample.cpu = tidq->packet->cpu;
1438 	sample.flags = tidq->prev_packet->flags;
1439 	sample.cpumode = event->sample.header.misc;
1440 
1441 	cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1442 			  &sample);
1443 
1444 	/*
1445 	 * perf report cannot handle events without a branch stack
1446 	 */
1447 	if (etm->synth_opts.last_branch) {
1448 		dummy_bs = (struct dummy_branch_stack){
1449 			.nr = 1,
1450 			.hw_idx = -1ULL,
1451 			.entries = {
1452 				.from = sample.ip,
1453 				.to = sample.addr,
1454 			},
1455 		};
1456 		sample.branch_stack = (struct branch_stack *)&dummy_bs;
1457 	}
1458 
1459 	if (etm->synth_opts.inject) {
1460 		ret = cs_etm__inject_event(event, &sample,
1461 					   etm->branches_sample_type);
1462 		if (ret)
1463 			return ret;
1464 	}
1465 
1466 	ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1467 
1468 	if (ret)
1469 		pr_err(
1470 		"CS ETM Trace: failed to deliver instruction event, error %d\n",
1471 		ret);
1472 
1473 	return ret;
1474 }
1475 
1476 struct cs_etm_synth {
1477 	struct perf_tool dummy_tool;
1478 	struct perf_session *session;
1479 };
1480 
1481 static int cs_etm__event_synth(struct perf_tool *tool,
1482 			       union perf_event *event,
1483 			       struct perf_sample *sample __maybe_unused,
1484 			       struct machine *machine __maybe_unused)
1485 {
1486 	struct cs_etm_synth *cs_etm_synth =
1487 		      container_of(tool, struct cs_etm_synth, dummy_tool);
1488 
1489 	return perf_session__deliver_synth_event(cs_etm_synth->session,
1490 						 event, NULL);
1491 }
1492 
1493 static int cs_etm__synth_event(struct perf_session *session,
1494 			       struct perf_event_attr *attr, u64 id)
1495 {
1496 	struct cs_etm_synth cs_etm_synth;
1497 
1498 	memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1499 	cs_etm_synth.session = session;
1500 
1501 	return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1502 					   &id, cs_etm__event_synth);
1503 }
1504 
1505 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1506 				struct perf_session *session)
1507 {
1508 	struct evlist *evlist = session->evlist;
1509 	struct evsel *evsel;
1510 	struct perf_event_attr attr;
1511 	bool found = false;
1512 	u64 id;
1513 	int err;
1514 
1515 	evlist__for_each_entry(evlist, evsel) {
1516 		if (evsel->core.attr.type == etm->pmu_type) {
1517 			found = true;
1518 			break;
1519 		}
1520 	}
1521 
1522 	if (!found) {
1523 		pr_debug("No selected events with CoreSight Trace data\n");
1524 		return 0;
1525 	}
1526 
1527 	memset(&attr, 0, sizeof(struct perf_event_attr));
1528 	attr.size = sizeof(struct perf_event_attr);
1529 	attr.type = PERF_TYPE_HARDWARE;
1530 	attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1531 	attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1532 			    PERF_SAMPLE_PERIOD;
1533 	if (etm->timeless_decoding)
1534 		attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1535 	else
1536 		attr.sample_type |= PERF_SAMPLE_TIME;
1537 
1538 	attr.exclude_user = evsel->core.attr.exclude_user;
1539 	attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1540 	attr.exclude_hv = evsel->core.attr.exclude_hv;
1541 	attr.exclude_host = evsel->core.attr.exclude_host;
1542 	attr.exclude_guest = evsel->core.attr.exclude_guest;
1543 	attr.sample_id_all = evsel->core.attr.sample_id_all;
1544 	attr.read_format = evsel->core.attr.read_format;
1545 
1546 	/* create new id val to be a fixed offset from evsel id */
1547 	id = evsel->core.id[0] + 1000000000;
1548 
1549 	if (!id)
1550 		id = 1;
1551 
1552 	if (etm->synth_opts.branches) {
1553 		attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1554 		attr.sample_period = 1;
1555 		attr.sample_type |= PERF_SAMPLE_ADDR;
1556 		err = cs_etm__synth_event(session, &attr, id);
1557 		if (err)
1558 			return err;
1559 		etm->branches_sample_type = attr.sample_type;
1560 		etm->branches_id = id;
1561 		id += 1;
1562 		attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1563 	}
1564 
1565 	if (etm->synth_opts.last_branch) {
1566 		attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1567 		/*
1568 		 * We don't use the hardware index, but the sample generation
1569 		 * code uses the new format branch_stack with this field,
1570 		 * so the event attributes must indicate that it's present.
1571 		 */
1572 		attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
1573 	}
1574 
1575 	if (etm->synth_opts.instructions) {
1576 		attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1577 		attr.sample_period = etm->synth_opts.period;
1578 		etm->instructions_sample_period = attr.sample_period;
1579 		err = cs_etm__synth_event(session, &attr, id);
1580 		if (err)
1581 			return err;
1582 		etm->instructions_sample_type = attr.sample_type;
1583 		etm->instructions_id = id;
1584 		id += 1;
1585 	}
1586 
1587 	return 0;
1588 }
1589 
1590 static int cs_etm__sample(struct cs_etm_queue *etmq,
1591 			  struct cs_etm_traceid_queue *tidq)
1592 {
1593 	struct cs_etm_auxtrace *etm = etmq->etm;
1594 	int ret;
1595 	u8 trace_chan_id = tidq->trace_chan_id;
1596 	u64 instrs_prev;
1597 
1598 	/* Get instructions remainder from previous packet */
1599 	instrs_prev = tidq->period_instructions;
1600 
1601 	tidq->period_instructions += tidq->packet->instr_count;
1602 
1603 	/*
1604 	 * Record a branch when the last instruction in
1605 	 * PREV_PACKET is a branch.
1606 	 */
1607 	if (etm->synth_opts.last_branch &&
1608 	    tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1609 	    tidq->prev_packet->last_instr_taken_branch)
1610 		cs_etm__update_last_branch_rb(etmq, tidq);
1611 
1612 	if (etm->synth_opts.instructions &&
1613 	    tidq->period_instructions >= etm->instructions_sample_period) {
1614 		/*
1615 		 * Emit instruction sample periodically
1616 		 * TODO: allow period to be defined in cycles and clock time
1617 		 */
1618 
1619 		/*
1620 		 * Below diagram demonstrates the instruction samples
1621 		 * generation flows:
1622 		 *
1623 		 *    Instrs     Instrs       Instrs       Instrs
1624 		 *   Sample(n)  Sample(n+1)  Sample(n+2)  Sample(n+3)
1625 		 *    |            |            |            |
1626 		 *    V            V            V            V
1627 		 *   --------------------------------------------------
1628 		 *            ^                                  ^
1629 		 *            |                                  |
1630 		 *         Period                             Period
1631 		 *    instructions(Pi)                   instructions(Pi')
1632 		 *
1633 		 *            |                                  |
1634 		 *            \---------------- -----------------/
1635 		 *                             V
1636 		 *                 tidq->packet->instr_count
1637 		 *
1638 		 * Instrs Sample(n...) are the synthesised samples occurring
1639 		 * every etm->instructions_sample_period instructions - as
1640 		 * defined on the perf command line.  Sample(n) is being the
1641 		 * last sample before the current etm packet, n+1 to n+3
1642 		 * samples are generated from the current etm packet.
1643 		 *
1644 		 * tidq->packet->instr_count represents the number of
1645 		 * instructions in the current etm packet.
1646 		 *
1647 		 * Period instructions (Pi) contains the number of
1648 		 * instructions executed after the sample point(n) from the
1649 		 * previous etm packet.  This will always be less than
1650 		 * etm->instructions_sample_period.
1651 		 *
1652 		 * When generate new samples, it combines with two parts
1653 		 * instructions, one is the tail of the old packet and another
1654 		 * is the head of the new coming packet, to generate
1655 		 * sample(n+1); sample(n+2) and sample(n+3) consume the
1656 		 * instructions with sample period.  After sample(n+3), the rest
1657 		 * instructions will be used by later packet and it is assigned
1658 		 * to tidq->period_instructions for next round calculation.
1659 		 */
1660 
1661 		/*
1662 		 * Get the initial offset into the current packet instructions;
1663 		 * entry conditions ensure that instrs_prev is less than
1664 		 * etm->instructions_sample_period.
1665 		 */
1666 		u64 offset = etm->instructions_sample_period - instrs_prev;
1667 		u64 addr;
1668 
1669 		/* Prepare last branches for instruction sample */
1670 		if (etm->synth_opts.last_branch)
1671 			cs_etm__copy_last_branch_rb(etmq, tidq);
1672 
1673 		while (tidq->period_instructions >=
1674 				etm->instructions_sample_period) {
1675 			/*
1676 			 * Calculate the address of the sampled instruction (-1
1677 			 * as sample is reported as though instruction has just
1678 			 * been executed, but PC has not advanced to next
1679 			 * instruction)
1680 			 */
1681 			addr = cs_etm__instr_addr(etmq, trace_chan_id,
1682 						  tidq->packet, offset - 1);
1683 			ret = cs_etm__synth_instruction_sample(
1684 				etmq, tidq, addr,
1685 				etm->instructions_sample_period);
1686 			if (ret)
1687 				return ret;
1688 
1689 			offset += etm->instructions_sample_period;
1690 			tidq->period_instructions -=
1691 				etm->instructions_sample_period;
1692 		}
1693 	}
1694 
1695 	if (etm->synth_opts.branches) {
1696 		bool generate_sample = false;
1697 
1698 		/* Generate sample for tracing on packet */
1699 		if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1700 			generate_sample = true;
1701 
1702 		/* Generate sample for branch taken packet */
1703 		if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1704 		    tidq->prev_packet->last_instr_taken_branch)
1705 			generate_sample = true;
1706 
1707 		if (generate_sample) {
1708 			ret = cs_etm__synth_branch_sample(etmq, tidq);
1709 			if (ret)
1710 				return ret;
1711 		}
1712 	}
1713 
1714 	cs_etm__packet_swap(etm, tidq);
1715 
1716 	return 0;
1717 }
1718 
1719 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1720 {
1721 	/*
1722 	 * When the exception packet is inserted, whether the last instruction
1723 	 * in previous range packet is taken branch or not, we need to force
1724 	 * to set 'prev_packet->last_instr_taken_branch' to true.  This ensures
1725 	 * to generate branch sample for the instruction range before the
1726 	 * exception is trapped to kernel or before the exception returning.
1727 	 *
1728 	 * The exception packet includes the dummy address values, so don't
1729 	 * swap PACKET with PREV_PACKET.  This keeps PREV_PACKET to be useful
1730 	 * for generating instruction and branch samples.
1731 	 */
1732 	if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1733 		tidq->prev_packet->last_instr_taken_branch = true;
1734 
1735 	return 0;
1736 }
1737 
1738 static int cs_etm__flush(struct cs_etm_queue *etmq,
1739 			 struct cs_etm_traceid_queue *tidq)
1740 {
1741 	int err = 0;
1742 	struct cs_etm_auxtrace *etm = etmq->etm;
1743 
1744 	/* Handle start tracing packet */
1745 	if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1746 		goto swap_packet;
1747 
1748 	if (etmq->etm->synth_opts.last_branch &&
1749 	    etmq->etm->synth_opts.instructions &&
1750 	    tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1751 		u64 addr;
1752 
1753 		/* Prepare last branches for instruction sample */
1754 		cs_etm__copy_last_branch_rb(etmq, tidq);
1755 
1756 		/*
1757 		 * Generate a last branch event for the branches left in the
1758 		 * circular buffer at the end of the trace.
1759 		 *
1760 		 * Use the address of the end of the last reported execution
1761 		 * range
1762 		 */
1763 		addr = cs_etm__last_executed_instr(tidq->prev_packet);
1764 
1765 		err = cs_etm__synth_instruction_sample(
1766 			etmq, tidq, addr,
1767 			tidq->period_instructions);
1768 		if (err)
1769 			return err;
1770 
1771 		tidq->period_instructions = 0;
1772 
1773 	}
1774 
1775 	if (etm->synth_opts.branches &&
1776 	    tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1777 		err = cs_etm__synth_branch_sample(etmq, tidq);
1778 		if (err)
1779 			return err;
1780 	}
1781 
1782 swap_packet:
1783 	cs_etm__packet_swap(etm, tidq);
1784 
1785 	/* Reset last branches after flush the trace */
1786 	if (etm->synth_opts.last_branch)
1787 		cs_etm__reset_last_branch_rb(tidq);
1788 
1789 	return err;
1790 }
1791 
1792 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1793 			     struct cs_etm_traceid_queue *tidq)
1794 {
1795 	int err;
1796 
1797 	/*
1798 	 * It has no new packet coming and 'etmq->packet' contains the stale
1799 	 * packet which was set at the previous time with packets swapping;
1800 	 * so skip to generate branch sample to avoid stale packet.
1801 	 *
1802 	 * For this case only flush branch stack and generate a last branch
1803 	 * event for the branches left in the circular buffer at the end of
1804 	 * the trace.
1805 	 */
1806 	if (etmq->etm->synth_opts.last_branch &&
1807 	    etmq->etm->synth_opts.instructions &&
1808 	    tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1809 		u64 addr;
1810 
1811 		/* Prepare last branches for instruction sample */
1812 		cs_etm__copy_last_branch_rb(etmq, tidq);
1813 
1814 		/*
1815 		 * Use the address of the end of the last reported execution
1816 		 * range.
1817 		 */
1818 		addr = cs_etm__last_executed_instr(tidq->prev_packet);
1819 
1820 		err = cs_etm__synth_instruction_sample(
1821 			etmq, tidq, addr,
1822 			tidq->period_instructions);
1823 		if (err)
1824 			return err;
1825 
1826 		tidq->period_instructions = 0;
1827 	}
1828 
1829 	return 0;
1830 }
1831 /*
1832  * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1833  *			   if need be.
1834  * Returns:	< 0	if error
1835  *		= 0	if no more auxtrace_buffer to read
1836  *		> 0	if the current buffer isn't empty yet
1837  */
1838 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1839 {
1840 	int ret;
1841 
1842 	if (!etmq->buf_len) {
1843 		ret = cs_etm__get_trace(etmq);
1844 		if (ret <= 0)
1845 			return ret;
1846 		/*
1847 		 * We cannot assume consecutive blocks in the data file
1848 		 * are contiguous, reset the decoder to force re-sync.
1849 		 */
1850 		ret = cs_etm_decoder__reset(etmq->decoder);
1851 		if (ret)
1852 			return ret;
1853 	}
1854 
1855 	return etmq->buf_len;
1856 }
1857 
1858 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1859 				 struct cs_etm_packet *packet,
1860 				 u64 end_addr)
1861 {
1862 	/* Initialise to keep compiler happy */
1863 	u16 instr16 = 0;
1864 	u32 instr32 = 0;
1865 	u64 addr;
1866 
1867 	switch (packet->isa) {
1868 	case CS_ETM_ISA_T32:
1869 		/*
1870 		 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1871 		 *
1872 		 *  b'15         b'8
1873 		 * +-----------------+--------+
1874 		 * | 1 1 0 1 1 1 1 1 |  imm8  |
1875 		 * +-----------------+--------+
1876 		 *
1877 		 * According to the specification, it only defines SVC for T32
1878 		 * with 16 bits instruction and has no definition for 32bits;
1879 		 * so below only read 2 bytes as instruction size for T32.
1880 		 */
1881 		addr = end_addr - 2;
1882 		cs_etm__mem_access(etmq, trace_chan_id, addr,
1883 				   sizeof(instr16), (u8 *)&instr16);
1884 		if ((instr16 & 0xFF00) == 0xDF00)
1885 			return true;
1886 
1887 		break;
1888 	case CS_ETM_ISA_A32:
1889 		/*
1890 		 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1891 		 *
1892 		 *  b'31 b'28 b'27 b'24
1893 		 * +---------+---------+-------------------------+
1894 		 * |  !1111  | 1 1 1 1 |        imm24            |
1895 		 * +---------+---------+-------------------------+
1896 		 */
1897 		addr = end_addr - 4;
1898 		cs_etm__mem_access(etmq, trace_chan_id, addr,
1899 				   sizeof(instr32), (u8 *)&instr32);
1900 		if ((instr32 & 0x0F000000) == 0x0F000000 &&
1901 		    (instr32 & 0xF0000000) != 0xF0000000)
1902 			return true;
1903 
1904 		break;
1905 	case CS_ETM_ISA_A64:
1906 		/*
1907 		 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1908 		 *
1909 		 *  b'31               b'21           b'4     b'0
1910 		 * +-----------------------+---------+-----------+
1911 		 * | 1 1 0 1 0 1 0 0 0 0 0 |  imm16  | 0 0 0 0 1 |
1912 		 * +-----------------------+---------+-----------+
1913 		 */
1914 		addr = end_addr - 4;
1915 		cs_etm__mem_access(etmq, trace_chan_id, addr,
1916 				   sizeof(instr32), (u8 *)&instr32);
1917 		if ((instr32 & 0xFFE0001F) == 0xd4000001)
1918 			return true;
1919 
1920 		break;
1921 	case CS_ETM_ISA_UNKNOWN:
1922 	default:
1923 		break;
1924 	}
1925 
1926 	return false;
1927 }
1928 
1929 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1930 			       struct cs_etm_traceid_queue *tidq, u64 magic)
1931 {
1932 	u8 trace_chan_id = tidq->trace_chan_id;
1933 	struct cs_etm_packet *packet = tidq->packet;
1934 	struct cs_etm_packet *prev_packet = tidq->prev_packet;
1935 
1936 	if (magic == __perf_cs_etmv3_magic)
1937 		if (packet->exception_number == CS_ETMV3_EXC_SVC)
1938 			return true;
1939 
1940 	/*
1941 	 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1942 	 * HVC cases; need to check if it's SVC instruction based on
1943 	 * packet address.
1944 	 */
1945 	if (magic == __perf_cs_etmv4_magic) {
1946 		if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1947 		    cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1948 					 prev_packet->end_addr))
1949 			return true;
1950 	}
1951 
1952 	return false;
1953 }
1954 
1955 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1956 				       u64 magic)
1957 {
1958 	struct cs_etm_packet *packet = tidq->packet;
1959 
1960 	if (magic == __perf_cs_etmv3_magic)
1961 		if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1962 		    packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1963 		    packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1964 		    packet->exception_number == CS_ETMV3_EXC_IRQ ||
1965 		    packet->exception_number == CS_ETMV3_EXC_FIQ)
1966 			return true;
1967 
1968 	if (magic == __perf_cs_etmv4_magic)
1969 		if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1970 		    packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1971 		    packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1972 		    packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1973 		    packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1974 		    packet->exception_number == CS_ETMV4_EXC_IRQ ||
1975 		    packet->exception_number == CS_ETMV4_EXC_FIQ)
1976 			return true;
1977 
1978 	return false;
1979 }
1980 
1981 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1982 				      struct cs_etm_traceid_queue *tidq,
1983 				      u64 magic)
1984 {
1985 	u8 trace_chan_id = tidq->trace_chan_id;
1986 	struct cs_etm_packet *packet = tidq->packet;
1987 	struct cs_etm_packet *prev_packet = tidq->prev_packet;
1988 
1989 	if (magic == __perf_cs_etmv3_magic)
1990 		if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1991 		    packet->exception_number == CS_ETMV3_EXC_HYP ||
1992 		    packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1993 		    packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1994 		    packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1995 		    packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1996 		    packet->exception_number == CS_ETMV3_EXC_GENERIC)
1997 			return true;
1998 
1999 	if (magic == __perf_cs_etmv4_magic) {
2000 		if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
2001 		    packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
2002 		    packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
2003 		    packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
2004 			return true;
2005 
2006 		/*
2007 		 * For CS_ETMV4_EXC_CALL, except SVC other instructions
2008 		 * (SMC, HVC) are taken as sync exceptions.
2009 		 */
2010 		if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2011 		    !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
2012 					  prev_packet->end_addr))
2013 			return true;
2014 
2015 		/*
2016 		 * ETMv4 has 5 bits for exception number; if the numbers
2017 		 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
2018 		 * they are implementation defined exceptions.
2019 		 *
2020 		 * For this case, simply take it as sync exception.
2021 		 */
2022 		if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
2023 		    packet->exception_number <= CS_ETMV4_EXC_END)
2024 			return true;
2025 	}
2026 
2027 	return false;
2028 }
2029 
2030 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
2031 				    struct cs_etm_traceid_queue *tidq)
2032 {
2033 	struct cs_etm_packet *packet = tidq->packet;
2034 	struct cs_etm_packet *prev_packet = tidq->prev_packet;
2035 	u8 trace_chan_id = tidq->trace_chan_id;
2036 	u64 magic;
2037 	int ret;
2038 
2039 	switch (packet->sample_type) {
2040 	case CS_ETM_RANGE:
2041 		/*
2042 		 * Immediate branch instruction without neither link nor
2043 		 * return flag, it's normal branch instruction within
2044 		 * the function.
2045 		 */
2046 		if (packet->last_instr_type == OCSD_INSTR_BR &&
2047 		    packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
2048 			packet->flags = PERF_IP_FLAG_BRANCH;
2049 
2050 			if (packet->last_instr_cond)
2051 				packet->flags |= PERF_IP_FLAG_CONDITIONAL;
2052 		}
2053 
2054 		/*
2055 		 * Immediate branch instruction with link (e.g. BL), this is
2056 		 * branch instruction for function call.
2057 		 */
2058 		if (packet->last_instr_type == OCSD_INSTR_BR &&
2059 		    packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2060 			packet->flags = PERF_IP_FLAG_BRANCH |
2061 					PERF_IP_FLAG_CALL;
2062 
2063 		/*
2064 		 * Indirect branch instruction with link (e.g. BLR), this is
2065 		 * branch instruction for function call.
2066 		 */
2067 		if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2068 		    packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2069 			packet->flags = PERF_IP_FLAG_BRANCH |
2070 					PERF_IP_FLAG_CALL;
2071 
2072 		/*
2073 		 * Indirect branch instruction with subtype of
2074 		 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
2075 		 * function return for A32/T32.
2076 		 */
2077 		if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2078 		    packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
2079 			packet->flags = PERF_IP_FLAG_BRANCH |
2080 					PERF_IP_FLAG_RETURN;
2081 
2082 		/*
2083 		 * Indirect branch instruction without link (e.g. BR), usually
2084 		 * this is used for function return, especially for functions
2085 		 * within dynamic link lib.
2086 		 */
2087 		if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2088 		    packet->last_instr_subtype == OCSD_S_INSTR_NONE)
2089 			packet->flags = PERF_IP_FLAG_BRANCH |
2090 					PERF_IP_FLAG_RETURN;
2091 
2092 		/* Return instruction for function return. */
2093 		if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2094 		    packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
2095 			packet->flags = PERF_IP_FLAG_BRANCH |
2096 					PERF_IP_FLAG_RETURN;
2097 
2098 		/*
2099 		 * Decoder might insert a discontinuity in the middle of
2100 		 * instruction packets, fixup prev_packet with flag
2101 		 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
2102 		 */
2103 		if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
2104 			prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2105 					      PERF_IP_FLAG_TRACE_BEGIN;
2106 
2107 		/*
2108 		 * If the previous packet is an exception return packet
2109 		 * and the return address just follows SVC instruction,
2110 		 * it needs to calibrate the previous packet sample flags
2111 		 * as PERF_IP_FLAG_SYSCALLRET.
2112 		 */
2113 		if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
2114 					   PERF_IP_FLAG_RETURN |
2115 					   PERF_IP_FLAG_INTERRUPT) &&
2116 		    cs_etm__is_svc_instr(etmq, trace_chan_id,
2117 					 packet, packet->start_addr))
2118 			prev_packet->flags = PERF_IP_FLAG_BRANCH |
2119 					     PERF_IP_FLAG_RETURN |
2120 					     PERF_IP_FLAG_SYSCALLRET;
2121 		break;
2122 	case CS_ETM_DISCONTINUITY:
2123 		/*
2124 		 * The trace is discontinuous, if the previous packet is
2125 		 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
2126 		 * for previous packet.
2127 		 */
2128 		if (prev_packet->sample_type == CS_ETM_RANGE)
2129 			prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2130 					      PERF_IP_FLAG_TRACE_END;
2131 		break;
2132 	case CS_ETM_EXCEPTION:
2133 		ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
2134 		if (ret)
2135 			return ret;
2136 
2137 		/* The exception is for system call. */
2138 		if (cs_etm__is_syscall(etmq, tidq, magic))
2139 			packet->flags = PERF_IP_FLAG_BRANCH |
2140 					PERF_IP_FLAG_CALL |
2141 					PERF_IP_FLAG_SYSCALLRET;
2142 		/*
2143 		 * The exceptions are triggered by external signals from bus,
2144 		 * interrupt controller, debug module, PE reset or halt.
2145 		 */
2146 		else if (cs_etm__is_async_exception(tidq, magic))
2147 			packet->flags = PERF_IP_FLAG_BRANCH |
2148 					PERF_IP_FLAG_CALL |
2149 					PERF_IP_FLAG_ASYNC |
2150 					PERF_IP_FLAG_INTERRUPT;
2151 		/*
2152 		 * Otherwise, exception is caused by trap, instruction &
2153 		 * data fault, or alignment errors.
2154 		 */
2155 		else if (cs_etm__is_sync_exception(etmq, tidq, magic))
2156 			packet->flags = PERF_IP_FLAG_BRANCH |
2157 					PERF_IP_FLAG_CALL |
2158 					PERF_IP_FLAG_INTERRUPT;
2159 
2160 		/*
2161 		 * When the exception packet is inserted, since exception
2162 		 * packet is not used standalone for generating samples
2163 		 * and it's affiliation to the previous instruction range
2164 		 * packet; so set previous range packet flags to tell perf
2165 		 * it is an exception taken branch.
2166 		 */
2167 		if (prev_packet->sample_type == CS_ETM_RANGE)
2168 			prev_packet->flags = packet->flags;
2169 		break;
2170 	case CS_ETM_EXCEPTION_RET:
2171 		/*
2172 		 * When the exception return packet is inserted, since
2173 		 * exception return packet is not used standalone for
2174 		 * generating samples and it's affiliation to the previous
2175 		 * instruction range packet; so set previous range packet
2176 		 * flags to tell perf it is an exception return branch.
2177 		 *
2178 		 * The exception return can be for either system call or
2179 		 * other exception types; unfortunately the packet doesn't
2180 		 * contain exception type related info so we cannot decide
2181 		 * the exception type purely based on exception return packet.
2182 		 * If we record the exception number from exception packet and
2183 		 * reuse it for exception return packet, this is not reliable
2184 		 * due the trace can be discontinuity or the interrupt can
2185 		 * be nested, thus the recorded exception number cannot be
2186 		 * used for exception return packet for these two cases.
2187 		 *
2188 		 * For exception return packet, we only need to distinguish the
2189 		 * packet is for system call or for other types.  Thus the
2190 		 * decision can be deferred when receive the next packet which
2191 		 * contains the return address, based on the return address we
2192 		 * can read out the previous instruction and check if it's a
2193 		 * system call instruction and then calibrate the sample flag
2194 		 * as needed.
2195 		 */
2196 		if (prev_packet->sample_type == CS_ETM_RANGE)
2197 			prev_packet->flags = PERF_IP_FLAG_BRANCH |
2198 					     PERF_IP_FLAG_RETURN |
2199 					     PERF_IP_FLAG_INTERRUPT;
2200 		break;
2201 	case CS_ETM_EMPTY:
2202 	default:
2203 		break;
2204 	}
2205 
2206 	return 0;
2207 }
2208 
2209 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
2210 {
2211 	int ret = 0;
2212 	size_t processed = 0;
2213 
2214 	/*
2215 	 * Packets are decoded and added to the decoder's packet queue
2216 	 * until the decoder packet processing callback has requested that
2217 	 * processing stops or there is nothing left in the buffer.  Normal
2218 	 * operations that stop processing are a timestamp packet or a full
2219 	 * decoder buffer queue.
2220 	 */
2221 	ret = cs_etm_decoder__process_data_block(etmq->decoder,
2222 						 etmq->offset,
2223 						 &etmq->buf[etmq->buf_used],
2224 						 etmq->buf_len,
2225 						 &processed);
2226 	if (ret)
2227 		goto out;
2228 
2229 	etmq->offset += processed;
2230 	etmq->buf_used += processed;
2231 	etmq->buf_len -= processed;
2232 
2233 out:
2234 	return ret;
2235 }
2236 
2237 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
2238 					 struct cs_etm_traceid_queue *tidq)
2239 {
2240 	int ret;
2241 	struct cs_etm_packet_queue *packet_queue;
2242 
2243 	packet_queue = &tidq->packet_queue;
2244 
2245 	/* Process each packet in this chunk */
2246 	while (1) {
2247 		ret = cs_etm_decoder__get_packet(packet_queue,
2248 						 tidq->packet);
2249 		if (ret <= 0)
2250 			/*
2251 			 * Stop processing this chunk on
2252 			 * end of data or error
2253 			 */
2254 			break;
2255 
2256 		/*
2257 		 * Since packet addresses are swapped in packet
2258 		 * handling within below switch() statements,
2259 		 * thus setting sample flags must be called
2260 		 * prior to switch() statement to use address
2261 		 * information before packets swapping.
2262 		 */
2263 		ret = cs_etm__set_sample_flags(etmq, tidq);
2264 		if (ret < 0)
2265 			break;
2266 
2267 		switch (tidq->packet->sample_type) {
2268 		case CS_ETM_RANGE:
2269 			/*
2270 			 * If the packet contains an instruction
2271 			 * range, generate instruction sequence
2272 			 * events.
2273 			 */
2274 			cs_etm__sample(etmq, tidq);
2275 			break;
2276 		case CS_ETM_EXCEPTION:
2277 		case CS_ETM_EXCEPTION_RET:
2278 			/*
2279 			 * If the exception packet is coming,
2280 			 * make sure the previous instruction
2281 			 * range packet to be handled properly.
2282 			 */
2283 			cs_etm__exception(tidq);
2284 			break;
2285 		case CS_ETM_DISCONTINUITY:
2286 			/*
2287 			 * Discontinuity in trace, flush
2288 			 * previous branch stack
2289 			 */
2290 			cs_etm__flush(etmq, tidq);
2291 			break;
2292 		case CS_ETM_EMPTY:
2293 			/*
2294 			 * Should not receive empty packet,
2295 			 * report error.
2296 			 */
2297 			pr_err("CS ETM Trace: empty packet\n");
2298 			return -EINVAL;
2299 		default:
2300 			break;
2301 		}
2302 	}
2303 
2304 	return ret;
2305 }
2306 
2307 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2308 {
2309 	int idx;
2310 	struct int_node *inode;
2311 	struct cs_etm_traceid_queue *tidq;
2312 	struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2313 
2314 	intlist__for_each_entry(inode, traceid_queues_list) {
2315 		idx = (int)(intptr_t)inode->priv;
2316 		tidq = etmq->traceid_queues[idx];
2317 
2318 		/* Ignore return value */
2319 		cs_etm__process_traceid_queue(etmq, tidq);
2320 
2321 		/*
2322 		 * Generate an instruction sample with the remaining
2323 		 * branchstack entries.
2324 		 */
2325 		cs_etm__flush(etmq, tidq);
2326 	}
2327 }
2328 
2329 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
2330 {
2331 	int err = 0;
2332 	struct cs_etm_traceid_queue *tidq;
2333 
2334 	tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2335 	if (!tidq)
2336 		return -EINVAL;
2337 
2338 	/* Go through each buffer in the queue and decode them one by one */
2339 	while (1) {
2340 		err = cs_etm__get_data_block(etmq);
2341 		if (err <= 0)
2342 			return err;
2343 
2344 		/* Run trace decoder until buffer consumed or end of trace */
2345 		do {
2346 			err = cs_etm__decode_data_block(etmq);
2347 			if (err)
2348 				return err;
2349 
2350 			/*
2351 			 * Process each packet in this chunk, nothing to do if
2352 			 * an error occurs other than hoping the next one will
2353 			 * be better.
2354 			 */
2355 			err = cs_etm__process_traceid_queue(etmq, tidq);
2356 
2357 		} while (etmq->buf_len);
2358 
2359 		if (err == 0)
2360 			/* Flush any remaining branch stack entries */
2361 			err = cs_etm__end_block(etmq, tidq);
2362 	}
2363 
2364 	return err;
2365 }
2366 
2367 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2368 					   pid_t tid)
2369 {
2370 	unsigned int i;
2371 	struct auxtrace_queues *queues = &etm->queues;
2372 
2373 	for (i = 0; i < queues->nr_queues; i++) {
2374 		struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2375 		struct cs_etm_queue *etmq = queue->priv;
2376 		struct cs_etm_traceid_queue *tidq;
2377 
2378 		if (!etmq)
2379 			continue;
2380 
2381 		tidq = cs_etm__etmq_get_traceid_queue(etmq,
2382 						CS_ETM_PER_THREAD_TRACEID);
2383 
2384 		if (!tidq)
2385 			continue;
2386 
2387 		if ((tid == -1) || (tidq->tid == tid)) {
2388 			cs_etm__set_pid_tid_cpu(etm, tidq);
2389 			cs_etm__run_decoder(etmq);
2390 		}
2391 	}
2392 
2393 	return 0;
2394 }
2395 
2396 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm)
2397 {
2398 	int ret = 0;
2399 	unsigned int cs_queue_nr, queue_nr, i;
2400 	u8 trace_chan_id;
2401 	u64 cs_timestamp;
2402 	struct auxtrace_queue *queue;
2403 	struct cs_etm_queue *etmq;
2404 	struct cs_etm_traceid_queue *tidq;
2405 
2406 	/*
2407 	 * Pre-populate the heap with one entry from each queue so that we can
2408 	 * start processing in time order across all queues.
2409 	 */
2410 	for (i = 0; i < etm->queues.nr_queues; i++) {
2411 		etmq = etm->queues.queue_array[i].priv;
2412 		if (!etmq)
2413 			continue;
2414 
2415 		ret = cs_etm__queue_first_cs_timestamp(etm, etmq, i);
2416 		if (ret)
2417 			return ret;
2418 	}
2419 
2420 	while (1) {
2421 		if (!etm->heap.heap_cnt)
2422 			goto out;
2423 
2424 		/* Take the entry at the top of the min heap */
2425 		cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2426 		queue_nr = TO_QUEUE_NR(cs_queue_nr);
2427 		trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2428 		queue = &etm->queues.queue_array[queue_nr];
2429 		etmq = queue->priv;
2430 
2431 		/*
2432 		 * Remove the top entry from the heap since we are about
2433 		 * to process it.
2434 		 */
2435 		auxtrace_heap__pop(&etm->heap);
2436 
2437 		tidq  = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2438 		if (!tidq) {
2439 			/*
2440 			 * No traceID queue has been allocated for this traceID,
2441 			 * which means something somewhere went very wrong.  No
2442 			 * other choice than simply exit.
2443 			 */
2444 			ret = -EINVAL;
2445 			goto out;
2446 		}
2447 
2448 		/*
2449 		 * Packets associated with this timestamp are already in
2450 		 * the etmq's traceID queue, so process them.
2451 		 */
2452 		ret = cs_etm__process_traceid_queue(etmq, tidq);
2453 		if (ret < 0)
2454 			goto out;
2455 
2456 		/*
2457 		 * Packets for this timestamp have been processed, time to
2458 		 * move on to the next timestamp, fetching a new auxtrace_buffer
2459 		 * if need be.
2460 		 */
2461 refetch:
2462 		ret = cs_etm__get_data_block(etmq);
2463 		if (ret < 0)
2464 			goto out;
2465 
2466 		/*
2467 		 * No more auxtrace_buffers to process in this etmq, simply
2468 		 * move on to another entry in the auxtrace_heap.
2469 		 */
2470 		if (!ret)
2471 			continue;
2472 
2473 		ret = cs_etm__decode_data_block(etmq);
2474 		if (ret)
2475 			goto out;
2476 
2477 		cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2478 
2479 		if (!cs_timestamp) {
2480 			/*
2481 			 * Function cs_etm__decode_data_block() returns when
2482 			 * there is no more traces to decode in the current
2483 			 * auxtrace_buffer OR when a timestamp has been
2484 			 * encountered on any of the traceID queues.  Since we
2485 			 * did not get a timestamp, there is no more traces to
2486 			 * process in this auxtrace_buffer.  As such empty and
2487 			 * flush all traceID queues.
2488 			 */
2489 			cs_etm__clear_all_traceid_queues(etmq);
2490 
2491 			/* Fetch another auxtrace_buffer for this etmq */
2492 			goto refetch;
2493 		}
2494 
2495 		/*
2496 		 * Add to the min heap the timestamp for packets that have
2497 		 * just been decoded.  They will be processed and synthesized
2498 		 * during the next call to cs_etm__process_traceid_queue() for
2499 		 * this queue/traceID.
2500 		 */
2501 		cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2502 		ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2503 	}
2504 
2505 out:
2506 	return ret;
2507 }
2508 
2509 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2510 					union perf_event *event)
2511 {
2512 	struct thread *th;
2513 
2514 	if (etm->timeless_decoding)
2515 		return 0;
2516 
2517 	/*
2518 	 * Add the tid/pid to the log so that we can get a match when
2519 	 * we get a contextID from the decoder.
2520 	 */
2521 	th = machine__findnew_thread(etm->machine,
2522 				     event->itrace_start.pid,
2523 				     event->itrace_start.tid);
2524 	if (!th)
2525 		return -ENOMEM;
2526 
2527 	thread__put(th);
2528 
2529 	return 0;
2530 }
2531 
2532 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2533 					   union perf_event *event)
2534 {
2535 	struct thread *th;
2536 	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2537 
2538 	/*
2539 	 * Context switch in per-thread mode are irrelevant since perf
2540 	 * will start/stop tracing as the process is scheduled.
2541 	 */
2542 	if (etm->timeless_decoding)
2543 		return 0;
2544 
2545 	/*
2546 	 * SWITCH_IN events carry the next process to be switched out while
2547 	 * SWITCH_OUT events carry the process to be switched in.  As such
2548 	 * we don't care about IN events.
2549 	 */
2550 	if (!out)
2551 		return 0;
2552 
2553 	/*
2554 	 * Add the tid/pid to the log so that we can get a match when
2555 	 * we get a contextID from the decoder.
2556 	 */
2557 	th = machine__findnew_thread(etm->machine,
2558 				     event->context_switch.next_prev_pid,
2559 				     event->context_switch.next_prev_tid);
2560 	if (!th)
2561 		return -ENOMEM;
2562 
2563 	thread__put(th);
2564 
2565 	return 0;
2566 }
2567 
2568 static int cs_etm__process_event(struct perf_session *session,
2569 				 union perf_event *event,
2570 				 struct perf_sample *sample,
2571 				 struct perf_tool *tool)
2572 {
2573 	u64 sample_kernel_timestamp;
2574 	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2575 						   struct cs_etm_auxtrace,
2576 						   auxtrace);
2577 
2578 	if (dump_trace)
2579 		return 0;
2580 
2581 	if (!tool->ordered_events) {
2582 		pr_err("CoreSight ETM Trace requires ordered events\n");
2583 		return -EINVAL;
2584 	}
2585 
2586 	if (sample->time && (sample->time != (u64) -1))
2587 		sample_kernel_timestamp = sample->time;
2588 	else
2589 		sample_kernel_timestamp = 0;
2590 
2591 	/*
2592 	 * Don't wait for cs_etm__flush_events() in per-thread/timeless mode to start the decode. We
2593 	 * need the tid of the PERF_RECORD_EXIT event to assign to the synthesised samples because
2594 	 * ETM_OPT_CTXTID is not enabled.
2595 	 */
2596 	if (etm->timeless_decoding &&
2597 	    event->header.type == PERF_RECORD_EXIT)
2598 		return cs_etm__process_timeless_queues(etm,
2599 						       event->fork.tid);
2600 
2601 	if (event->header.type == PERF_RECORD_ITRACE_START)
2602 		return cs_etm__process_itrace_start(etm, event);
2603 	else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2604 		return cs_etm__process_switch_cpu_wide(etm, event);
2605 
2606 	if (!etm->timeless_decoding && event->header.type == PERF_RECORD_AUX) {
2607 		/*
2608 		 * Record the latest kernel timestamp available in the header
2609 		 * for samples so that synthesised samples occur from this point
2610 		 * onwards.
2611 		 */
2612 		etm->latest_kernel_timestamp = sample_kernel_timestamp;
2613 	}
2614 
2615 	return 0;
2616 }
2617 
2618 static void dump_queued_data(struct cs_etm_auxtrace *etm,
2619 			     struct perf_record_auxtrace *event)
2620 {
2621 	struct auxtrace_buffer *buf;
2622 	unsigned int i;
2623 	/*
2624 	 * Find all buffers with same reference in the queues and dump them.
2625 	 * This is because the queues can contain multiple entries of the same
2626 	 * buffer that were split on aux records.
2627 	 */
2628 	for (i = 0; i < etm->queues.nr_queues; ++i)
2629 		list_for_each_entry(buf, &etm->queues.queue_array[i].head, list)
2630 			if (buf->reference == event->reference)
2631 				cs_etm__dump_event(etm->queues.queue_array[i].priv, buf);
2632 }
2633 
2634 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2635 					  union perf_event *event,
2636 					  struct perf_tool *tool __maybe_unused)
2637 {
2638 	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2639 						   struct cs_etm_auxtrace,
2640 						   auxtrace);
2641 	if (!etm->data_queued) {
2642 		struct auxtrace_buffer *buffer;
2643 		off_t  data_offset;
2644 		int fd = perf_data__fd(session->data);
2645 		bool is_pipe = perf_data__is_pipe(session->data);
2646 		int err;
2647 		int idx = event->auxtrace.idx;
2648 
2649 		if (is_pipe)
2650 			data_offset = 0;
2651 		else {
2652 			data_offset = lseek(fd, 0, SEEK_CUR);
2653 			if (data_offset == -1)
2654 				return -errno;
2655 		}
2656 
2657 		err = auxtrace_queues__add_event(&etm->queues, session,
2658 						 event, data_offset, &buffer);
2659 		if (err)
2660 			return err;
2661 
2662 		/*
2663 		 * Knowing if the trace is formatted or not requires a lookup of
2664 		 * the aux record so only works in non-piped mode where data is
2665 		 * queued in cs_etm__queue_aux_records(). Always assume
2666 		 * formatted in piped mode (true).
2667 		 */
2668 		err = cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2669 					  idx, true);
2670 		if (err)
2671 			return err;
2672 
2673 		if (dump_trace)
2674 			if (auxtrace_buffer__get_data(buffer, fd)) {
2675 				cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer);
2676 				auxtrace_buffer__put_data(buffer);
2677 			}
2678 	} else if (dump_trace)
2679 		dump_queued_data(etm, &event->auxtrace);
2680 
2681 	return 0;
2682 }
2683 
2684 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
2685 {
2686 	struct evsel *evsel;
2687 	struct evlist *evlist = etm->session->evlist;
2688 	bool timeless_decoding = true;
2689 
2690 	/* Override timeless mode with user input from --itrace=Z */
2691 	if (etm->synth_opts.timeless_decoding)
2692 		return true;
2693 
2694 	/*
2695 	 * Circle through the list of event and complain if we find one
2696 	 * with the time bit set.
2697 	 */
2698 	evlist__for_each_entry(evlist, evsel) {
2699 		if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
2700 			timeless_decoding = false;
2701 	}
2702 
2703 	return timeless_decoding;
2704 }
2705 
2706 /*
2707  * Read a single cpu parameter block from the auxtrace_info priv block.
2708  *
2709  * For version 1 there is a per cpu nr_params entry. If we are handling
2710  * version 1 file, then there may be less, the same, or more params
2711  * indicated by this value than the compile time number we understand.
2712  *
2713  * For a version 0 info block, there are a fixed number, and we need to
2714  * fill out the nr_param value in the metadata we create.
2715  */
2716 static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
2717 				    int out_blk_size, int nr_params_v0)
2718 {
2719 	u64 *metadata = NULL;
2720 	int hdr_version;
2721 	int nr_in_params, nr_out_params, nr_cmn_params;
2722 	int i, k;
2723 
2724 	metadata = zalloc(sizeof(*metadata) * out_blk_size);
2725 	if (!metadata)
2726 		return NULL;
2727 
2728 	/* read block current index & version */
2729 	i = *buff_in_offset;
2730 	hdr_version = buff_in[CS_HEADER_VERSION];
2731 
2732 	if (!hdr_version) {
2733 	/* read version 0 info block into a version 1 metadata block  */
2734 		nr_in_params = nr_params_v0;
2735 		metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC];
2736 		metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU];
2737 		metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params;
2738 		/* remaining block params at offset +1 from source */
2739 		for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2740 			metadata[k + 1] = buff_in[i + k];
2741 		/* version 0 has 2 common params */
2742 		nr_cmn_params = 2;
2743 	} else {
2744 	/* read version 1 info block - input and output nr_params may differ */
2745 		/* version 1 has 3 common params */
2746 		nr_cmn_params = 3;
2747 		nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS];
2748 
2749 		/* if input has more params than output - skip excess */
2750 		nr_out_params = nr_in_params + nr_cmn_params;
2751 		if (nr_out_params > out_blk_size)
2752 			nr_out_params = out_blk_size;
2753 
2754 		for (k = CS_ETM_MAGIC; k < nr_out_params; k++)
2755 			metadata[k] = buff_in[i + k];
2756 
2757 		/* record the actual nr params we copied */
2758 		metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2759 	}
2760 
2761 	/* adjust in offset by number of in params used */
2762 	i += nr_in_params + nr_cmn_params;
2763 	*buff_in_offset = i;
2764 	return metadata;
2765 }
2766 
2767 /**
2768  * Puts a fragment of an auxtrace buffer into the auxtrace queues based
2769  * on the bounds of aux_event, if it matches with the buffer that's at
2770  * file_offset.
2771  *
2772  * Normally, whole auxtrace buffers would be added to the queue. But we
2773  * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
2774  * is reset across each buffer, so splitting the buffers up in advance has
2775  * the same effect.
2776  */
2777 static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz,
2778 				      struct perf_record_aux *aux_event, struct perf_sample *sample)
2779 {
2780 	int err;
2781 	char buf[PERF_SAMPLE_MAX_SIZE];
2782 	union perf_event *auxtrace_event_union;
2783 	struct perf_record_auxtrace *auxtrace_event;
2784 	union perf_event auxtrace_fragment;
2785 	__u64 aux_offset, aux_size;
2786 	__u32 idx;
2787 	bool formatted;
2788 
2789 	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2790 						   struct cs_etm_auxtrace,
2791 						   auxtrace);
2792 
2793 	/*
2794 	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
2795 	 * from looping through the auxtrace index.
2796 	 */
2797 	err = perf_session__peek_event(session, file_offset, buf,
2798 				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
2799 	if (err)
2800 		return err;
2801 	auxtrace_event = &auxtrace_event_union->auxtrace;
2802 	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
2803 		return -EINVAL;
2804 
2805 	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
2806 		auxtrace_event->header.size != sz) {
2807 		return -EINVAL;
2808 	}
2809 
2810 	/*
2811 	 * In per-thread mode, auxtrace CPU is set to -1, but TID will be set instead. See
2812 	 * auxtrace_mmap_params__set_idx(). However, the sample AUX event will contain a
2813 	 * CPU as we set this always for the AUX_OUTPUT_HW_ID event.
2814 	 * So now compare only TIDs if auxtrace CPU is -1, and CPUs if auxtrace CPU is not -1.
2815 	 * Return 'not found' if mismatch.
2816 	 */
2817 	if (auxtrace_event->cpu == (__u32) -1) {
2818 		if (auxtrace_event->tid != sample->tid)
2819 			return 1;
2820 	} else if (auxtrace_event->cpu != sample->cpu)
2821 		return 1;
2822 
2823 	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
2824 		/*
2825 		 * Clamp size in snapshot mode. The buffer size is clamped in
2826 		 * __auxtrace_mmap__read() for snapshots, so the aux record size doesn't reflect
2827 		 * the buffer size.
2828 		 */
2829 		aux_size = min(aux_event->aux_size, auxtrace_event->size);
2830 
2831 		/*
2832 		 * In this mode, the head also points to the end of the buffer so aux_offset
2833 		 * needs to have the size subtracted so it points to the beginning as in normal mode
2834 		 */
2835 		aux_offset = aux_event->aux_offset - aux_size;
2836 	} else {
2837 		aux_size = aux_event->aux_size;
2838 		aux_offset = aux_event->aux_offset;
2839 	}
2840 
2841 	if (aux_offset >= auxtrace_event->offset &&
2842 	    aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
2843 		/*
2844 		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
2845 		 * based on the sizes of the aux event, and queue that fragment.
2846 		 */
2847 		auxtrace_fragment.auxtrace = *auxtrace_event;
2848 		auxtrace_fragment.auxtrace.size = aux_size;
2849 		auxtrace_fragment.auxtrace.offset = aux_offset;
2850 		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
2851 
2852 		pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
2853 			  " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
2854 		err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
2855 						 file_offset, NULL);
2856 		if (err)
2857 			return err;
2858 
2859 		idx = auxtrace_event->idx;
2860 		formatted = !(aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW);
2861 		return cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2862 					   idx, formatted);
2863 	}
2864 
2865 	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
2866 	return 1;
2867 }
2868 
2869 static int cs_etm__process_aux_hw_id_cb(struct perf_session *session, union perf_event *event,
2870 					u64 offset __maybe_unused, void *data __maybe_unused)
2871 {
2872 	/* look to handle PERF_RECORD_AUX_OUTPUT_HW_ID early to ensure decoders can be set up */
2873 	if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) {
2874 		(*(int *)data)++; /* increment found count */
2875 		return cs_etm__process_aux_output_hw_id(session, event);
2876 	}
2877 	return 0;
2878 }
2879 
2880 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
2881 					u64 offset __maybe_unused, void *data __maybe_unused)
2882 {
2883 	struct perf_sample sample;
2884 	int ret;
2885 	struct auxtrace_index_entry *ent;
2886 	struct auxtrace_index *auxtrace_index;
2887 	struct evsel *evsel;
2888 	size_t i;
2889 
2890 	/* Don't care about any other events, we're only queuing buffers for AUX events */
2891 	if (event->header.type != PERF_RECORD_AUX)
2892 		return 0;
2893 
2894 	if (event->header.size < sizeof(struct perf_record_aux))
2895 		return -EINVAL;
2896 
2897 	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
2898 	if (!event->aux.aux_size)
2899 		return 0;
2900 
2901 	/*
2902 	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
2903 	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
2904 	 */
2905 	evsel = evlist__event2evsel(session->evlist, event);
2906 	if (!evsel)
2907 		return -EINVAL;
2908 	ret = evsel__parse_sample(evsel, event, &sample);
2909 	if (ret)
2910 		return ret;
2911 
2912 	/*
2913 	 * Loop through the auxtrace index to find the buffer that matches up with this aux event.
2914 	 */
2915 	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
2916 		for (i = 0; i < auxtrace_index->nr; i++) {
2917 			ent = &auxtrace_index->entries[i];
2918 			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
2919 							 ent->sz, &event->aux, &sample);
2920 			/*
2921 			 * Stop search on error or successful values. Continue search on
2922 			 * 1 ('not found')
2923 			 */
2924 			if (ret != 1)
2925 				return ret;
2926 		}
2927 	}
2928 
2929 	/*
2930 	 * Couldn't find the buffer corresponding to this aux record, something went wrong. Warn but
2931 	 * don't exit with an error because it will still be possible to decode other aux records.
2932 	 */
2933 	pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
2934 	       " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
2935 	return 0;
2936 }
2937 
2938 static int cs_etm__queue_aux_records(struct perf_session *session)
2939 {
2940 	struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
2941 								struct auxtrace_index, list);
2942 	if (index && index->nr > 0)
2943 		return perf_session__peek_events(session, session->header.data_offset,
2944 						 session->header.data_size,
2945 						 cs_etm__queue_aux_records_cb, NULL);
2946 
2947 	/*
2948 	 * We would get here if there are no entries in the index (either no auxtrace
2949 	 * buffers or no index at all). Fail silently as there is the possibility of
2950 	 * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still
2951 	 * false.
2952 	 *
2953 	 * In that scenario, buffers will not be split by AUX records.
2954 	 */
2955 	return 0;
2956 }
2957 
2958 #define HAS_PARAM(j, type, param) (metadata[(j)][CS_ETM_NR_TRC_PARAMS] <= \
2959 				  (CS_##type##_##param - CS_ETM_COMMON_BLK_MAX_V1))
2960 
2961 /*
2962  * Loop through the ETMs and complain if we find at least one where ts_source != 1 (virtual
2963  * timestamps).
2964  */
2965 static bool cs_etm__has_virtual_ts(u64 **metadata, int num_cpu)
2966 {
2967 	int j;
2968 
2969 	for (j = 0; j < num_cpu; j++) {
2970 		switch (metadata[j][CS_ETM_MAGIC]) {
2971 		case __perf_cs_etmv4_magic:
2972 			if (HAS_PARAM(j, ETMV4, TS_SOURCE) || metadata[j][CS_ETMV4_TS_SOURCE] != 1)
2973 				return false;
2974 			break;
2975 		case __perf_cs_ete_magic:
2976 			if (HAS_PARAM(j, ETE, TS_SOURCE) || metadata[j][CS_ETE_TS_SOURCE] != 1)
2977 				return false;
2978 			break;
2979 		default:
2980 			/* Unknown / unsupported magic number. */
2981 			return false;
2982 		}
2983 	}
2984 	return true;
2985 }
2986 
2987 /* map trace ids to correct metadata block, from information in metadata */
2988 static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata)
2989 {
2990 	u64 cs_etm_magic;
2991 	u8 trace_chan_id;
2992 	int i, err;
2993 
2994 	for (i = 0; i < num_cpu; i++) {
2995 		cs_etm_magic = metadata[i][CS_ETM_MAGIC];
2996 		switch (cs_etm_magic) {
2997 		case __perf_cs_etmv3_magic:
2998 			metadata[i][CS_ETM_ETMTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
2999 			trace_chan_id = (u8)(metadata[i][CS_ETM_ETMTRACEIDR]);
3000 			break;
3001 		case __perf_cs_etmv4_magic:
3002 		case __perf_cs_ete_magic:
3003 			metadata[i][CS_ETMV4_TRCTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3004 			trace_chan_id = (u8)(metadata[i][CS_ETMV4_TRCTRACEIDR]);
3005 			break;
3006 		default:
3007 			/* unknown magic number */
3008 			return -EINVAL;
3009 		}
3010 		err = cs_etm__map_trace_id(trace_chan_id, metadata[i]);
3011 		if (err)
3012 			return err;
3013 	}
3014 	return 0;
3015 }
3016 
3017 /*
3018  * If we found AUX_HW_ID packets, then set any metadata marked as unused to the
3019  * unused value to reduce the number of unneeded decoders created.
3020  */
3021 static int cs_etm__clear_unused_trace_ids_metadata(int num_cpu, u64 **metadata)
3022 {
3023 	u64 cs_etm_magic;
3024 	int i;
3025 
3026 	for (i = 0; i < num_cpu; i++) {
3027 		cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3028 		switch (cs_etm_magic) {
3029 		case __perf_cs_etmv3_magic:
3030 			if (metadata[i][CS_ETM_ETMTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3031 				metadata[i][CS_ETM_ETMTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3032 			break;
3033 		case __perf_cs_etmv4_magic:
3034 		case __perf_cs_ete_magic:
3035 			if (metadata[i][CS_ETMV4_TRCTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3036 				metadata[i][CS_ETMV4_TRCTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3037 			break;
3038 		default:
3039 			/* unknown magic number */
3040 			return -EINVAL;
3041 		}
3042 	}
3043 	return 0;
3044 }
3045 
3046 int cs_etm__process_auxtrace_info_full(union perf_event *event,
3047 				       struct perf_session *session)
3048 {
3049 	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
3050 	struct cs_etm_auxtrace *etm = NULL;
3051 	struct perf_record_time_conv *tc = &session->time_conv;
3052 	int event_header_size = sizeof(struct perf_event_header);
3053 	int total_size = auxtrace_info->header.size;
3054 	int priv_size = 0;
3055 	int num_cpu;
3056 	int err = 0;
3057 	int aux_hw_id_found;
3058 	int i, j;
3059 	u64 *ptr = NULL;
3060 	u64 **metadata = NULL;
3061 
3062 	/*
3063 	 * Create an RB tree for traceID-metadata tuple.  Since the conversion
3064 	 * has to be made for each packet that gets decoded, optimizing access
3065 	 * in anything other than a sequential array is worth doing.
3066 	 */
3067 	traceid_list = intlist__new(NULL);
3068 	if (!traceid_list)
3069 		return -ENOMEM;
3070 
3071 	/* First the global part */
3072 	ptr = (u64 *) auxtrace_info->priv;
3073 	num_cpu = ptr[CS_PMU_TYPE_CPUS] & 0xffffffff;
3074 	metadata = zalloc(sizeof(*metadata) * num_cpu);
3075 	if (!metadata) {
3076 		err = -ENOMEM;
3077 		goto err_free_traceid_list;
3078 	}
3079 
3080 	/* Start parsing after the common part of the header */
3081 	i = CS_HEADER_VERSION_MAX;
3082 
3083 	/*
3084 	 * The metadata is stored in the auxtrace_info section and encodes
3085 	 * the configuration of the ARM embedded trace macrocell which is
3086 	 * required by the trace decoder to properly decode the trace due
3087 	 * to its highly compressed nature.
3088 	 */
3089 	for (j = 0; j < num_cpu; j++) {
3090 		if (ptr[i] == __perf_cs_etmv3_magic) {
3091 			metadata[j] =
3092 				cs_etm__create_meta_blk(ptr, &i,
3093 							CS_ETM_PRIV_MAX,
3094 							CS_ETM_NR_TRC_PARAMS_V0);
3095 		} else if (ptr[i] == __perf_cs_etmv4_magic) {
3096 			metadata[j] =
3097 				cs_etm__create_meta_blk(ptr, &i,
3098 							CS_ETMV4_PRIV_MAX,
3099 							CS_ETMV4_NR_TRC_PARAMS_V0);
3100 		} else if (ptr[i] == __perf_cs_ete_magic) {
3101 			metadata[j] = cs_etm__create_meta_blk(ptr, &i, CS_ETE_PRIV_MAX, -1);
3102 		} else {
3103 			ui__error("CS ETM Trace: Unrecognised magic number %#"PRIx64". File could be from a newer version of perf.\n",
3104 				  ptr[i]);
3105 			err = -EINVAL;
3106 			goto err_free_metadata;
3107 		}
3108 
3109 		if (!metadata[j]) {
3110 			err = -ENOMEM;
3111 			goto err_free_metadata;
3112 		}
3113 	}
3114 
3115 	/*
3116 	 * Each of CS_HEADER_VERSION_MAX, CS_ETM_PRIV_MAX and
3117 	 * CS_ETMV4_PRIV_MAX mark how many double words are in the
3118 	 * global metadata, and each cpu's metadata respectively.
3119 	 * The following tests if the correct number of double words was
3120 	 * present in the auxtrace info section.
3121 	 */
3122 	priv_size = total_size - event_header_size - INFO_HEADER_SIZE;
3123 	if (i * 8 != priv_size) {
3124 		err = -EINVAL;
3125 		goto err_free_metadata;
3126 	}
3127 
3128 	etm = zalloc(sizeof(*etm));
3129 
3130 	if (!etm) {
3131 		err = -ENOMEM;
3132 		goto err_free_metadata;
3133 	}
3134 
3135 	err = auxtrace_queues__init(&etm->queues);
3136 	if (err)
3137 		goto err_free_etm;
3138 
3139 	if (session->itrace_synth_opts->set) {
3140 		etm->synth_opts = *session->itrace_synth_opts;
3141 	} else {
3142 		itrace_synth_opts__set_default(&etm->synth_opts,
3143 				session->itrace_synth_opts->default_no_sample);
3144 		etm->synth_opts.callchain = false;
3145 	}
3146 
3147 	etm->session = session;
3148 	etm->machine = &session->machines.host;
3149 
3150 	etm->num_cpu = num_cpu;
3151 	etm->pmu_type = (unsigned int) ((ptr[CS_PMU_TYPE_CPUS] >> 32) & 0xffffffff);
3152 	etm->snapshot_mode = (ptr[CS_ETM_SNAPSHOT] != 0);
3153 	etm->metadata = metadata;
3154 	etm->auxtrace_type = auxtrace_info->type;
3155 	etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
3156 
3157 	/* Use virtual timestamps if all ETMs report ts_source = 1 */
3158 	etm->has_virtual_ts = cs_etm__has_virtual_ts(metadata, num_cpu);
3159 
3160 	if (!etm->has_virtual_ts)
3161 		ui__warning("Virtual timestamps are not enabled, or not supported by the traced system.\n"
3162 			    "The time field of the samples will not be set accurately.\n\n");
3163 
3164 	etm->auxtrace.process_event = cs_etm__process_event;
3165 	etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3166 	etm->auxtrace.flush_events = cs_etm__flush_events;
3167 	etm->auxtrace.free_events = cs_etm__free_events;
3168 	etm->auxtrace.free = cs_etm__free;
3169 	etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3170 	session->auxtrace = &etm->auxtrace;
3171 
3172 	etm->unknown_thread = thread__new(999999999, 999999999);
3173 	if (!etm->unknown_thread) {
3174 		err = -ENOMEM;
3175 		goto err_free_queues;
3176 	}
3177 
3178 	/*
3179 	 * Initialize list node so that at thread__zput() we can avoid
3180 	 * segmentation fault at list_del_init().
3181 	 */
3182 	INIT_LIST_HEAD(&etm->unknown_thread->node);
3183 
3184 	err = thread__set_comm(etm->unknown_thread, "unknown", 0);
3185 	if (err)
3186 		goto err_delete_thread;
3187 
3188 	if (thread__init_maps(etm->unknown_thread, etm->machine)) {
3189 		err = -ENOMEM;
3190 		goto err_delete_thread;
3191 	}
3192 
3193 	etm->tc.time_shift = tc->time_shift;
3194 	etm->tc.time_mult = tc->time_mult;
3195 	etm->tc.time_zero = tc->time_zero;
3196 	if (event_contains(*tc, time_cycles)) {
3197 		etm->tc.time_cycles = tc->time_cycles;
3198 		etm->tc.time_mask = tc->time_mask;
3199 		etm->tc.cap_user_time_zero = tc->cap_user_time_zero;
3200 		etm->tc.cap_user_time_short = tc->cap_user_time_short;
3201 	}
3202 	err = cs_etm__synth_events(etm, session);
3203 	if (err)
3204 		goto err_delete_thread;
3205 
3206 	/*
3207 	 * Map Trace ID values to CPU metadata.
3208 	 *
3209 	 * Trace metadata will always contain Trace ID values from the legacy algorithm. If the
3210 	 * files has been recorded by a "new" perf updated to handle AUX_HW_ID then the metadata
3211 	 * ID value will also have the CORESIGHT_TRACE_ID_UNUSED_FLAG set.
3212 	 *
3213 	 * The updated kernel drivers that use AUX_HW_ID to sent Trace IDs will attempt to use
3214 	 * the same IDs as the old algorithm as far as is possible, unless there are clashes
3215 	 * in which case a different value will be used. This means an older perf may still
3216 	 * be able to record and read files generate on a newer system.
3217 	 *
3218 	 * For a perf able to interpret AUX_HW_ID packets we first check for the presence of
3219 	 * those packets. If they are there then the values will be mapped and plugged into
3220 	 * the metadata. We then set any remaining metadata values with the used flag to a
3221 	 * value CORESIGHT_TRACE_ID_UNUSED_VAL - which indicates no decoder is required.
3222 	 *
3223 	 * If no AUX_HW_ID packets are present - which means a file recorded on an old kernel
3224 	 * then we map Trace ID values to CPU directly from the metadata - clearing any unused
3225 	 * flags if present.
3226 	 */
3227 
3228 	/* first scan for AUX_OUTPUT_HW_ID records to map trace ID values to CPU metadata */
3229 	aux_hw_id_found = 0;
3230 	err = perf_session__peek_events(session, session->header.data_offset,
3231 					session->header.data_size,
3232 					cs_etm__process_aux_hw_id_cb, &aux_hw_id_found);
3233 	if (err)
3234 		goto err_delete_thread;
3235 
3236 	/* if HW ID found then clear any unused metadata ID values */
3237 	if (aux_hw_id_found)
3238 		err = cs_etm__clear_unused_trace_ids_metadata(num_cpu, metadata);
3239 	/* otherwise, this is a file with metadata values only, map from metadata */
3240 	else
3241 		err = cs_etm__map_trace_ids_metadata(num_cpu, metadata);
3242 
3243 	if (err)
3244 		goto err_delete_thread;
3245 
3246 	err = cs_etm__queue_aux_records(session);
3247 	if (err)
3248 		goto err_delete_thread;
3249 
3250 	etm->data_queued = etm->queues.populated;
3251 	return 0;
3252 
3253 err_delete_thread:
3254 	thread__zput(etm->unknown_thread);
3255 err_free_queues:
3256 	auxtrace_queues__free(&etm->queues);
3257 	session->auxtrace = NULL;
3258 err_free_etm:
3259 	zfree(&etm);
3260 err_free_metadata:
3261 	/* No need to check @metadata[j], free(NULL) is supported */
3262 	for (j = 0; j < num_cpu; j++)
3263 		zfree(&metadata[j]);
3264 	zfree(&metadata);
3265 err_free_traceid_list:
3266 	intlist__delete(traceid_list);
3267 	return err;
3268 }
3269