xref: /openbmc/linux/tools/perf/util/cs-etm.h (revision f2a39fe8)
18a9fd832SMathieu Poirier /* SPDX-License-Identifier: GPL-2.0 */
2a818c563SMathieu Poirier /*
3a818c563SMathieu Poirier  * Copyright(C) 2015 Linaro Limited. All rights reserved.
4a818c563SMathieu Poirier  * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
5a818c563SMathieu Poirier  */
6a818c563SMathieu Poirier 
7a818c563SMathieu Poirier #ifndef INCLUDE__UTIL_PERF_CS_ETM_H__
8a818c563SMathieu Poirier #define INCLUDE__UTIL_PERF_CS_ETM_H__
9a818c563SMathieu Poirier 
10440a23b3SMathieu Poirier #include "util/event.h"
11965e176fSArnaldo Carvalho de Melo #include <linux/bits.h>
12440a23b3SMathieu Poirier 
13f2a39fe8SArnaldo Carvalho de Melo struct perf_session;
14f2a39fe8SArnaldo Carvalho de Melo 
15a818c563SMathieu Poirier /* Versionning header in case things need tro change in the future.  That way
16a818c563SMathieu Poirier  * decoding of old snapshot is still possible.
17a818c563SMathieu Poirier  */
18a818c563SMathieu Poirier enum {
19a818c563SMathieu Poirier 	/* Starting with 0x0 */
20a818c563SMathieu Poirier 	CS_HEADER_VERSION_0,
21a818c563SMathieu Poirier 	/* PMU->type (32 bit), total # of CPUs (32 bit) */
22a818c563SMathieu Poirier 	CS_PMU_TYPE_CPUS,
23a818c563SMathieu Poirier 	CS_ETM_SNAPSHOT,
24a818c563SMathieu Poirier 	CS_HEADER_VERSION_0_MAX,
25a818c563SMathieu Poirier };
26a818c563SMathieu Poirier 
27a818c563SMathieu Poirier /* Beginning of header common to both ETMv3 and V4 */
28a818c563SMathieu Poirier enum {
29a818c563SMathieu Poirier 	CS_ETM_MAGIC,
30a818c563SMathieu Poirier 	CS_ETM_CPU,
31a818c563SMathieu Poirier };
32a818c563SMathieu Poirier 
33a818c563SMathieu Poirier /* ETMv3/PTM metadata */
34a818c563SMathieu Poirier enum {
35a818c563SMathieu Poirier 	/* Dynamic, configurable parameters */
36a818c563SMathieu Poirier 	CS_ETM_ETMCR = CS_ETM_CPU + 1,
37a818c563SMathieu Poirier 	CS_ETM_ETMTRACEIDR,
38a818c563SMathieu Poirier 	/* RO, taken from sysFS */
39a818c563SMathieu Poirier 	CS_ETM_ETMCCER,
40a818c563SMathieu Poirier 	CS_ETM_ETMIDR,
41a818c563SMathieu Poirier 	CS_ETM_PRIV_MAX,
42a818c563SMathieu Poirier };
43a818c563SMathieu Poirier 
44a818c563SMathieu Poirier /* ETMv4 metadata */
45a818c563SMathieu Poirier enum {
46a818c563SMathieu Poirier 	/* Dynamic, configurable parameters */
47a818c563SMathieu Poirier 	CS_ETMV4_TRCCONFIGR = CS_ETM_CPU + 1,
48a818c563SMathieu Poirier 	CS_ETMV4_TRCTRACEIDR,
49a818c563SMathieu Poirier 	/* RO, taken from sysFS */
50a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR0,
51a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR1,
52a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR2,
53a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR8,
54a818c563SMathieu Poirier 	CS_ETMV4_TRCAUTHSTATUS,
55a818c563SMathieu Poirier 	CS_ETMV4_PRIV_MAX,
56a818c563SMathieu Poirier };
57a818c563SMathieu Poirier 
5896dce7f4SLeo Yan /*
5996dce7f4SLeo Yan  * ETMv3 exception encoding number:
6096dce7f4SLeo Yan  * See Embedded Trace Macrocell spcification (ARM IHI 0014Q)
6196dce7f4SLeo Yan  * table 7-12 Encoding of Exception[3:0] for non-ARMv7-M processors.
6296dce7f4SLeo Yan  */
6396dce7f4SLeo Yan enum {
6496dce7f4SLeo Yan 	CS_ETMV3_EXC_NONE = 0,
6596dce7f4SLeo Yan 	CS_ETMV3_EXC_DEBUG_HALT = 1,
6696dce7f4SLeo Yan 	CS_ETMV3_EXC_SMC = 2,
6796dce7f4SLeo Yan 	CS_ETMV3_EXC_HYP = 3,
6896dce7f4SLeo Yan 	CS_ETMV3_EXC_ASYNC_DATA_ABORT = 4,
6996dce7f4SLeo Yan 	CS_ETMV3_EXC_JAZELLE_THUMBEE = 5,
7096dce7f4SLeo Yan 	CS_ETMV3_EXC_PE_RESET = 8,
7196dce7f4SLeo Yan 	CS_ETMV3_EXC_UNDEFINED_INSTR = 9,
7296dce7f4SLeo Yan 	CS_ETMV3_EXC_SVC = 10,
7396dce7f4SLeo Yan 	CS_ETMV3_EXC_PREFETCH_ABORT = 11,
7496dce7f4SLeo Yan 	CS_ETMV3_EXC_DATA_FAULT = 12,
7596dce7f4SLeo Yan 	CS_ETMV3_EXC_GENERIC = 13,
7696dce7f4SLeo Yan 	CS_ETMV3_EXC_IRQ = 14,
7796dce7f4SLeo Yan 	CS_ETMV3_EXC_FIQ = 15,
7896dce7f4SLeo Yan };
7996dce7f4SLeo Yan 
8096dce7f4SLeo Yan /*
8196dce7f4SLeo Yan  * ETMv4 exception encoding number:
8296dce7f4SLeo Yan  * See ARM Embedded Trace Macrocell Architecture Specification (ARM IHI 0064D)
8396dce7f4SLeo Yan  * table 6-12 Possible values for the TYPE field in an Exception instruction
8496dce7f4SLeo Yan  * trace packet, for ARMv7-A/R and ARMv8-A/R PEs.
8596dce7f4SLeo Yan  */
8696dce7f4SLeo Yan enum {
8796dce7f4SLeo Yan 	CS_ETMV4_EXC_RESET = 0,
8896dce7f4SLeo Yan 	CS_ETMV4_EXC_DEBUG_HALT = 1,
8996dce7f4SLeo Yan 	CS_ETMV4_EXC_CALL = 2,
9096dce7f4SLeo Yan 	CS_ETMV4_EXC_TRAP = 3,
9196dce7f4SLeo Yan 	CS_ETMV4_EXC_SYSTEM_ERROR = 4,
9296dce7f4SLeo Yan 	CS_ETMV4_EXC_INST_DEBUG = 6,
9396dce7f4SLeo Yan 	CS_ETMV4_EXC_DATA_DEBUG = 7,
9496dce7f4SLeo Yan 	CS_ETMV4_EXC_ALIGNMENT = 10,
9596dce7f4SLeo Yan 	CS_ETMV4_EXC_INST_FAULT = 11,
9696dce7f4SLeo Yan 	CS_ETMV4_EXC_DATA_FAULT = 12,
9796dce7f4SLeo Yan 	CS_ETMV4_EXC_IRQ = 14,
9896dce7f4SLeo Yan 	CS_ETMV4_EXC_FIQ = 15,
9996dce7f4SLeo Yan 	CS_ETMV4_EXC_END = 31,
10096dce7f4SLeo Yan };
10196dce7f4SLeo Yan 
1025f7cb035SMathieu Poirier enum cs_etm_sample_type {
1035f7cb035SMathieu Poirier 	CS_ETM_EMPTY,
1045f7cb035SMathieu Poirier 	CS_ETM_RANGE,
1055f7cb035SMathieu Poirier 	CS_ETM_DISCONTINUITY,
1065f7cb035SMathieu Poirier 	CS_ETM_EXCEPTION,
1075f7cb035SMathieu Poirier 	CS_ETM_EXCEPTION_RET,
1085f7cb035SMathieu Poirier };
1095f7cb035SMathieu Poirier 
1105f7cb035SMathieu Poirier enum cs_etm_isa {
1115f7cb035SMathieu Poirier 	CS_ETM_ISA_UNKNOWN,
1125f7cb035SMathieu Poirier 	CS_ETM_ISA_A64,
1135f7cb035SMathieu Poirier 	CS_ETM_ISA_A32,
1145f7cb035SMathieu Poirier 	CS_ETM_ISA_T32,
1155f7cb035SMathieu Poirier };
1165f7cb035SMathieu Poirier 
11795c6fe97SLeo Yan /* RB tree for quick conversion between traceID and metadata pointers */
118cd8bfd8cSTor Jeremiassen struct intlist *traceid_list;
119cd8bfd8cSTor Jeremiassen 
1205f7cb035SMathieu Poirier struct cs_etm_queue;
1215f7cb035SMathieu Poirier 
1225f7cb035SMathieu Poirier struct cs_etm_packet {
1235f7cb035SMathieu Poirier 	enum cs_etm_sample_type sample_type;
1245f7cb035SMathieu Poirier 	enum cs_etm_isa isa;
1255f7cb035SMathieu Poirier 	u64 start_addr;
1265f7cb035SMathieu Poirier 	u64 end_addr;
1275f7cb035SMathieu Poirier 	u32 instr_count;
1285f7cb035SMathieu Poirier 	u32 last_instr_type;
1295f7cb035SMathieu Poirier 	u32 last_instr_subtype;
1305f7cb035SMathieu Poirier 	u32 flags;
1315f7cb035SMathieu Poirier 	u32 exception_number;
1325f7cb035SMathieu Poirier 	u8 last_instr_cond;
1335f7cb035SMathieu Poirier 	u8 last_instr_taken_branch;
1345f7cb035SMathieu Poirier 	u8 last_instr_size;
1355f7cb035SMathieu Poirier 	u8 trace_chan_id;
1365f7cb035SMathieu Poirier 	int cpu;
1375f7cb035SMathieu Poirier };
1385f7cb035SMathieu Poirier 
1395f7cb035SMathieu Poirier #define CS_ETM_PACKET_MAX_BUFFER 1024
1405f7cb035SMathieu Poirier 
141c7bfa2fdSMathieu Poirier /*
142c7bfa2fdSMathieu Poirier  * When working with per-thread scenarios the process under trace can
143c7bfa2fdSMathieu Poirier  * be scheduled on any CPU and as such, more than one traceID may be
144c7bfa2fdSMathieu Poirier  * associated with the same process.  Since a traceID of '0' is illegal
145c7bfa2fdSMathieu Poirier  * as per the CoreSight architecture, use that specific value to
146c7bfa2fdSMathieu Poirier  * identify the queue where all packets (with any traceID) are
147c7bfa2fdSMathieu Poirier  * aggregated.
148c7bfa2fdSMathieu Poirier  */
149c7bfa2fdSMathieu Poirier #define CS_ETM_PER_THREAD_TRACEID 0
150c7bfa2fdSMathieu Poirier 
1515f7cb035SMathieu Poirier struct cs_etm_packet_queue {
1525f7cb035SMathieu Poirier 	u32 packet_count;
1535f7cb035SMathieu Poirier 	u32 head;
1545f7cb035SMathieu Poirier 	u32 tail;
155675f302fSMathieu Poirier 	u32 instr_count;
156675f302fSMathieu Poirier 	u64 timestamp;
157675f302fSMathieu Poirier 	u64 next_timestamp;
1585f7cb035SMathieu Poirier 	struct cs_etm_packet packet_buffer[CS_ETM_PACKET_MAX_BUFFER];
1595f7cb035SMathieu Poirier };
1605f7cb035SMathieu Poirier 
161a818c563SMathieu Poirier #define KiB(x) ((x) * 1024)
162a818c563SMathieu Poirier #define MiB(x) ((x) * 1024 * 1024)
163a818c563SMathieu Poirier 
1645f7cb035SMathieu Poirier #define CS_ETM_INVAL_ADDR 0xdeadbeefdeadbeefUL
1655f7cb035SMathieu Poirier 
1663399ad9aSMathieu Poirier #define BMVAL(val, lsb, msb)	((val & GENMASK(msb, lsb)) >> lsb)
1673399ad9aSMathieu Poirier 
168a818c563SMathieu Poirier #define CS_ETM_HEADER_SIZE (CS_HEADER_VERSION_0_MAX * sizeof(u64))
169a818c563SMathieu Poirier 
1702507a3d9SMathieu Poirier #define __perf_cs_etmv3_magic 0x3030303030303030ULL
1712507a3d9SMathieu Poirier #define __perf_cs_etmv4_magic 0x4040404040404040ULL
172a818c563SMathieu Poirier #define CS_ETMV3_PRIV_SIZE (CS_ETM_PRIV_MAX * sizeof(u64))
173a818c563SMathieu Poirier #define CS_ETMV4_PRIV_SIZE (CS_ETMV4_PRIV_MAX * sizeof(u64))
174a818c563SMathieu Poirier 
175440a23b3SMathieu Poirier #ifdef HAVE_CSTRACE_SUPPORT
176440a23b3SMathieu Poirier int cs_etm__process_auxtrace_info(union perf_event *event,
177440a23b3SMathieu Poirier 				  struct perf_session *session);
17895c6fe97SLeo Yan int cs_etm__get_cpu(u8 trace_chan_id, int *cpu);
1790a6be300SMathieu Poirier int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1800a6be300SMathieu Poirier 			 pid_t tid, u8 trace_chan_id);
181675f302fSMathieu Poirier bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq);
182675f302fSMathieu Poirier void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
183675f302fSMathieu Poirier 					      u8 trace_chan_id);
1845f7cb035SMathieu Poirier struct cs_etm_packet_queue
185c7bfa2fdSMathieu Poirier *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id);
186440a23b3SMathieu Poirier #else
187440a23b3SMathieu Poirier static inline int
188440a23b3SMathieu Poirier cs_etm__process_auxtrace_info(union perf_event *event __maybe_unused,
189440a23b3SMathieu Poirier 			      struct perf_session *session __maybe_unused)
190440a23b3SMathieu Poirier {
191440a23b3SMathieu Poirier 	return -1;
192440a23b3SMathieu Poirier }
19395c6fe97SLeo Yan 
19495c6fe97SLeo Yan static inline int cs_etm__get_cpu(u8 trace_chan_id __maybe_unused,
19595c6fe97SLeo Yan 				  int *cpu __maybe_unused)
19695c6fe97SLeo Yan {
19795c6fe97SLeo Yan 	return -1;
19895c6fe97SLeo Yan }
1995f7cb035SMathieu Poirier 
2000a6be300SMathieu Poirier static inline int cs_etm__etmq_set_tid(
2010a6be300SMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused,
2020a6be300SMathieu Poirier 				pid_t tid __maybe_unused,
2030a6be300SMathieu Poirier 				u8 trace_chan_id __maybe_unused)
2040a6be300SMathieu Poirier {
2050a6be300SMathieu Poirier 	return -1;
2060a6be300SMathieu Poirier }
2070a6be300SMathieu Poirier 
208675f302fSMathieu Poirier static inline bool cs_etm__etmq_is_timeless(
209675f302fSMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused)
210675f302fSMathieu Poirier {
211675f302fSMathieu Poirier 	/* What else to return? */
212675f302fSMathieu Poirier 	return true;
213675f302fSMathieu Poirier }
214675f302fSMathieu Poirier 
215675f302fSMathieu Poirier static inline void cs_etm__etmq_set_traceid_queue_timestamp(
216675f302fSMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused,
217675f302fSMathieu Poirier 				u8 trace_chan_id __maybe_unused) {}
218675f302fSMathieu Poirier 
2195f7cb035SMathieu Poirier static inline struct cs_etm_packet_queue *cs_etm__etmq_get_packet_queue(
220c7bfa2fdSMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused,
221c7bfa2fdSMathieu Poirier 				u8 trace_chan_id __maybe_unused)
2225f7cb035SMathieu Poirier {
2235f7cb035SMathieu Poirier 	return NULL;
2245f7cb035SMathieu Poirier }
225440a23b3SMathieu Poirier #endif
226440a23b3SMathieu Poirier 
227a818c563SMathieu Poirier #endif
228