xref: /openbmc/linux/tools/perf/util/cs-etm.h (revision 47f0d94c)
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 */
2042b2b570SMike Leach 	CS_HEADER_VERSION,
21a818c563SMathieu Poirier 	/* PMU->type (32 bit), total # of CPUs (32 bit) */
22a818c563SMathieu Poirier 	CS_PMU_TYPE_CPUS,
23a818c563SMathieu Poirier 	CS_ETM_SNAPSHOT,
2442b2b570SMike Leach 	CS_HEADER_VERSION_MAX,
25a818c563SMathieu Poirier };
26a818c563SMathieu Poirier 
2742b2b570SMike Leach /*
2842b2b570SMike Leach  * Update the version for new format.
2942b2b570SMike Leach  *
3042b2b570SMike Leach  * New version 1 format adds a param count to the per cpu metadata.
3142b2b570SMike Leach  * This allows easy adding of new metadata parameters.
3242b2b570SMike Leach  * Requires that new params always added after current ones.
3342b2b570SMike Leach  * Also allows client reader to handle file versions that are different by
3442b2b570SMike Leach  * checking the number of params in the file vs the number expected.
3542b2b570SMike Leach  */
3642b2b570SMike Leach #define CS_HEADER_CURRENT_VERSION 1
3742b2b570SMike Leach 
38a818c563SMathieu Poirier /* Beginning of header common to both ETMv3 and V4 */
39a818c563SMathieu Poirier enum {
40a818c563SMathieu Poirier 	CS_ETM_MAGIC,
41a818c563SMathieu Poirier 	CS_ETM_CPU,
4242b2b570SMike Leach 	/* Number of trace config params in following ETM specific block */
4342b2b570SMike Leach 	CS_ETM_NR_TRC_PARAMS,
4442b2b570SMike Leach 	CS_ETM_COMMON_BLK_MAX_V1,
45a818c563SMathieu Poirier };
46a818c563SMathieu Poirier 
47a818c563SMathieu Poirier /* ETMv3/PTM metadata */
48a818c563SMathieu Poirier enum {
49a818c563SMathieu Poirier 	/* Dynamic, configurable parameters */
5042b2b570SMike Leach 	CS_ETM_ETMCR = CS_ETM_COMMON_BLK_MAX_V1,
51a818c563SMathieu Poirier 	CS_ETM_ETMTRACEIDR,
52a818c563SMathieu Poirier 	/* RO, taken from sysFS */
53a818c563SMathieu Poirier 	CS_ETM_ETMCCER,
54a818c563SMathieu Poirier 	CS_ETM_ETMIDR,
55a818c563SMathieu Poirier 	CS_ETM_PRIV_MAX,
56a818c563SMathieu Poirier };
57a818c563SMathieu Poirier 
5842b2b570SMike Leach /* define fixed version 0 length - allow new format reader to read old files. */
5942b2b570SMike Leach #define CS_ETM_NR_TRC_PARAMS_V0 (CS_ETM_ETMIDR - CS_ETM_ETMCR + 1)
6042b2b570SMike Leach 
61a818c563SMathieu Poirier /* ETMv4 metadata */
62a818c563SMathieu Poirier enum {
63a818c563SMathieu Poirier 	/* Dynamic, configurable parameters */
6442b2b570SMike Leach 	CS_ETMV4_TRCCONFIGR = CS_ETM_COMMON_BLK_MAX_V1,
65a818c563SMathieu Poirier 	CS_ETMV4_TRCTRACEIDR,
66a818c563SMathieu Poirier 	/* RO, taken from sysFS */
67a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR0,
68a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR1,
69a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR2,
70a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR8,
71a818c563SMathieu Poirier 	CS_ETMV4_TRCAUTHSTATUS,
72a818c563SMathieu Poirier 	CS_ETMV4_PRIV_MAX,
73a818c563SMathieu Poirier };
74a818c563SMathieu Poirier 
7542b2b570SMike Leach /* define fixed version 0 length - allow new format reader to read old files. */
7642b2b570SMike Leach #define CS_ETMV4_NR_TRC_PARAMS_V0 (CS_ETMV4_TRCAUTHSTATUS - CS_ETMV4_TRCCONFIGR + 1)
7742b2b570SMike Leach 
7896dce7f4SLeo Yan /*
7996dce7f4SLeo Yan  * ETMv3 exception encoding number:
8096dce7f4SLeo Yan  * See Embedded Trace Macrocell spcification (ARM IHI 0014Q)
8196dce7f4SLeo Yan  * table 7-12 Encoding of Exception[3:0] for non-ARMv7-M processors.
8296dce7f4SLeo Yan  */
8396dce7f4SLeo Yan enum {
8496dce7f4SLeo Yan 	CS_ETMV3_EXC_NONE = 0,
8596dce7f4SLeo Yan 	CS_ETMV3_EXC_DEBUG_HALT = 1,
8696dce7f4SLeo Yan 	CS_ETMV3_EXC_SMC = 2,
8796dce7f4SLeo Yan 	CS_ETMV3_EXC_HYP = 3,
8896dce7f4SLeo Yan 	CS_ETMV3_EXC_ASYNC_DATA_ABORT = 4,
8996dce7f4SLeo Yan 	CS_ETMV3_EXC_JAZELLE_THUMBEE = 5,
9096dce7f4SLeo Yan 	CS_ETMV3_EXC_PE_RESET = 8,
9196dce7f4SLeo Yan 	CS_ETMV3_EXC_UNDEFINED_INSTR = 9,
9296dce7f4SLeo Yan 	CS_ETMV3_EXC_SVC = 10,
9396dce7f4SLeo Yan 	CS_ETMV3_EXC_PREFETCH_ABORT = 11,
9496dce7f4SLeo Yan 	CS_ETMV3_EXC_DATA_FAULT = 12,
9596dce7f4SLeo Yan 	CS_ETMV3_EXC_GENERIC = 13,
9696dce7f4SLeo Yan 	CS_ETMV3_EXC_IRQ = 14,
9796dce7f4SLeo Yan 	CS_ETMV3_EXC_FIQ = 15,
9896dce7f4SLeo Yan };
9996dce7f4SLeo Yan 
10096dce7f4SLeo Yan /*
10196dce7f4SLeo Yan  * ETMv4 exception encoding number:
10296dce7f4SLeo Yan  * See ARM Embedded Trace Macrocell Architecture Specification (ARM IHI 0064D)
10396dce7f4SLeo Yan  * table 6-12 Possible values for the TYPE field in an Exception instruction
10496dce7f4SLeo Yan  * trace packet, for ARMv7-A/R and ARMv8-A/R PEs.
10596dce7f4SLeo Yan  */
10696dce7f4SLeo Yan enum {
10796dce7f4SLeo Yan 	CS_ETMV4_EXC_RESET = 0,
10896dce7f4SLeo Yan 	CS_ETMV4_EXC_DEBUG_HALT = 1,
10996dce7f4SLeo Yan 	CS_ETMV4_EXC_CALL = 2,
11096dce7f4SLeo Yan 	CS_ETMV4_EXC_TRAP = 3,
11196dce7f4SLeo Yan 	CS_ETMV4_EXC_SYSTEM_ERROR = 4,
11296dce7f4SLeo Yan 	CS_ETMV4_EXC_INST_DEBUG = 6,
11396dce7f4SLeo Yan 	CS_ETMV4_EXC_DATA_DEBUG = 7,
11496dce7f4SLeo Yan 	CS_ETMV4_EXC_ALIGNMENT = 10,
11596dce7f4SLeo Yan 	CS_ETMV4_EXC_INST_FAULT = 11,
11696dce7f4SLeo Yan 	CS_ETMV4_EXC_DATA_FAULT = 12,
11796dce7f4SLeo Yan 	CS_ETMV4_EXC_IRQ = 14,
11896dce7f4SLeo Yan 	CS_ETMV4_EXC_FIQ = 15,
11996dce7f4SLeo Yan 	CS_ETMV4_EXC_END = 31,
12096dce7f4SLeo Yan };
12196dce7f4SLeo Yan 
1225f7cb035SMathieu Poirier enum cs_etm_sample_type {
1235f7cb035SMathieu Poirier 	CS_ETM_EMPTY,
1245f7cb035SMathieu Poirier 	CS_ETM_RANGE,
1255f7cb035SMathieu Poirier 	CS_ETM_DISCONTINUITY,
1265f7cb035SMathieu Poirier 	CS_ETM_EXCEPTION,
1275f7cb035SMathieu Poirier 	CS_ETM_EXCEPTION_RET,
1285f7cb035SMathieu Poirier };
1295f7cb035SMathieu Poirier 
1305f7cb035SMathieu Poirier enum cs_etm_isa {
1315f7cb035SMathieu Poirier 	CS_ETM_ISA_UNKNOWN,
1325f7cb035SMathieu Poirier 	CS_ETM_ISA_A64,
1335f7cb035SMathieu Poirier 	CS_ETM_ISA_A32,
1345f7cb035SMathieu Poirier 	CS_ETM_ISA_T32,
1355f7cb035SMathieu Poirier };
1365f7cb035SMathieu Poirier 
1375f7cb035SMathieu Poirier struct cs_etm_queue;
1385f7cb035SMathieu Poirier 
1395f7cb035SMathieu Poirier struct cs_etm_packet {
1405f7cb035SMathieu Poirier 	enum cs_etm_sample_type sample_type;
1415f7cb035SMathieu Poirier 	enum cs_etm_isa isa;
1425f7cb035SMathieu Poirier 	u64 start_addr;
1435f7cb035SMathieu Poirier 	u64 end_addr;
1445f7cb035SMathieu Poirier 	u32 instr_count;
1455f7cb035SMathieu Poirier 	u32 last_instr_type;
1465f7cb035SMathieu Poirier 	u32 last_instr_subtype;
1475f7cb035SMathieu Poirier 	u32 flags;
1485f7cb035SMathieu Poirier 	u32 exception_number;
1495f7cb035SMathieu Poirier 	u8 last_instr_cond;
1505f7cb035SMathieu Poirier 	u8 last_instr_taken_branch;
1515f7cb035SMathieu Poirier 	u8 last_instr_size;
1525f7cb035SMathieu Poirier 	u8 trace_chan_id;
1535f7cb035SMathieu Poirier 	int cpu;
1545f7cb035SMathieu Poirier };
1555f7cb035SMathieu Poirier 
1565f7cb035SMathieu Poirier #define CS_ETM_PACKET_MAX_BUFFER 1024
1575f7cb035SMathieu Poirier 
158c7bfa2fdSMathieu Poirier /*
159c7bfa2fdSMathieu Poirier  * When working with per-thread scenarios the process under trace can
160c7bfa2fdSMathieu Poirier  * be scheduled on any CPU and as such, more than one traceID may be
161c7bfa2fdSMathieu Poirier  * associated with the same process.  Since a traceID of '0' is illegal
162c7bfa2fdSMathieu Poirier  * as per the CoreSight architecture, use that specific value to
163c7bfa2fdSMathieu Poirier  * identify the queue where all packets (with any traceID) are
164c7bfa2fdSMathieu Poirier  * aggregated.
165c7bfa2fdSMathieu Poirier  */
166c7bfa2fdSMathieu Poirier #define CS_ETM_PER_THREAD_TRACEID 0
167c7bfa2fdSMathieu Poirier 
1685f7cb035SMathieu Poirier struct cs_etm_packet_queue {
1695f7cb035SMathieu Poirier 	u32 packet_count;
1705f7cb035SMathieu Poirier 	u32 head;
1715f7cb035SMathieu Poirier 	u32 tail;
172675f302fSMathieu Poirier 	u32 instr_count;
173675f302fSMathieu Poirier 	u64 timestamp;
174675f302fSMathieu Poirier 	u64 next_timestamp;
1755f7cb035SMathieu Poirier 	struct cs_etm_packet packet_buffer[CS_ETM_PACKET_MAX_BUFFER];
1765f7cb035SMathieu Poirier };
1775f7cb035SMathieu Poirier 
178a818c563SMathieu Poirier #define KiB(x) ((x) * 1024)
179a818c563SMathieu Poirier #define MiB(x) ((x) * 1024 * 1024)
180a818c563SMathieu Poirier 
1815f7cb035SMathieu Poirier #define CS_ETM_INVAL_ADDR 0xdeadbeefdeadbeefUL
1825f7cb035SMathieu Poirier 
1833399ad9aSMathieu Poirier #define BMVAL(val, lsb, msb)	((val & GENMASK(msb, lsb)) >> lsb)
1843399ad9aSMathieu Poirier 
18542b2b570SMike Leach #define CS_ETM_HEADER_SIZE (CS_HEADER_VERSION_MAX * sizeof(u64))
186a818c563SMathieu Poirier 
1872507a3d9SMathieu Poirier #define __perf_cs_etmv3_magic 0x3030303030303030ULL
1882507a3d9SMathieu Poirier #define __perf_cs_etmv4_magic 0x4040404040404040ULL
189a818c563SMathieu Poirier #define CS_ETMV3_PRIV_SIZE (CS_ETM_PRIV_MAX * sizeof(u64))
190a818c563SMathieu Poirier #define CS_ETMV4_PRIV_SIZE (CS_ETMV4_PRIV_MAX * sizeof(u64))
191a818c563SMathieu Poirier 
192440a23b3SMathieu Poirier #ifdef HAVE_CSTRACE_SUPPORT
193440a23b3SMathieu Poirier int cs_etm__process_auxtrace_info(union perf_event *event,
194440a23b3SMathieu Poirier 				  struct perf_session *session);
19595c6fe97SLeo Yan int cs_etm__get_cpu(u8 trace_chan_id, int *cpu);
196*47f0d94cSLeo Yan int cs_etm__get_pid_fmt(u8 trace_chan_id, u64 *pid_fmt);
1970a6be300SMathieu Poirier int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1980a6be300SMathieu Poirier 			 pid_t tid, u8 trace_chan_id);
199675f302fSMathieu Poirier bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq);
200675f302fSMathieu Poirier void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
201675f302fSMathieu Poirier 					      u8 trace_chan_id);
2025f7cb035SMathieu Poirier struct cs_etm_packet_queue
203c7bfa2fdSMathieu Poirier *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id);
204440a23b3SMathieu Poirier #else
205440a23b3SMathieu Poirier static inline int
206440a23b3SMathieu Poirier cs_etm__process_auxtrace_info(union perf_event *event __maybe_unused,
207440a23b3SMathieu Poirier 			      struct perf_session *session __maybe_unused)
208440a23b3SMathieu Poirier {
209440a23b3SMathieu Poirier 	return -1;
210440a23b3SMathieu Poirier }
21195c6fe97SLeo Yan 
21295c6fe97SLeo Yan static inline int cs_etm__get_cpu(u8 trace_chan_id __maybe_unused,
21395c6fe97SLeo Yan 				  int *cpu __maybe_unused)
21495c6fe97SLeo Yan {
21595c6fe97SLeo Yan 	return -1;
21695c6fe97SLeo Yan }
2175f7cb035SMathieu Poirier 
2180a6be300SMathieu Poirier static inline int cs_etm__etmq_set_tid(
2190a6be300SMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused,
2200a6be300SMathieu Poirier 				pid_t tid __maybe_unused,
2210a6be300SMathieu Poirier 				u8 trace_chan_id __maybe_unused)
2220a6be300SMathieu Poirier {
2230a6be300SMathieu Poirier 	return -1;
2240a6be300SMathieu Poirier }
2250a6be300SMathieu Poirier 
226675f302fSMathieu Poirier static inline bool cs_etm__etmq_is_timeless(
227675f302fSMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused)
228675f302fSMathieu Poirier {
229675f302fSMathieu Poirier 	/* What else to return? */
230675f302fSMathieu Poirier 	return true;
231675f302fSMathieu Poirier }
232675f302fSMathieu Poirier 
233675f302fSMathieu Poirier static inline void cs_etm__etmq_set_traceid_queue_timestamp(
234675f302fSMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused,
235675f302fSMathieu Poirier 				u8 trace_chan_id __maybe_unused) {}
236675f302fSMathieu Poirier 
2375f7cb035SMathieu Poirier static inline struct cs_etm_packet_queue *cs_etm__etmq_get_packet_queue(
238c7bfa2fdSMathieu Poirier 				struct cs_etm_queue *etmq __maybe_unused,
239c7bfa2fdSMathieu Poirier 				u8 trace_chan_id __maybe_unused)
2405f7cb035SMathieu Poirier {
2415f7cb035SMathieu Poirier 	return NULL;
2425f7cb035SMathieu Poirier }
243440a23b3SMathieu Poirier #endif
244440a23b3SMathieu Poirier 
245a818c563SMathieu Poirier #endif
246