xref: /openbmc/linux/tools/perf/util/cs-etm.h (revision 8d3031d3)
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 
10fe55ba18SJames Clark #include "debug.h"
11440a23b3SMathieu Poirier #include "util/event.h"
12965e176fSArnaldo Carvalho de Melo #include <linux/bits.h>
13440a23b3SMathieu Poirier 
14f2a39fe8SArnaldo Carvalho de Melo struct perf_session;
157bfc1544SJames Clark struct perf_pmu;
16f2a39fe8SArnaldo Carvalho de Melo 
174d39c89fSIngo Molnar /*
184d39c89fSIngo Molnar  * Versioning header in case things need to change in the future.  That way
19a818c563SMathieu Poirier  * decoding of old snapshot is still possible.
20a818c563SMathieu Poirier  */
21a818c563SMathieu Poirier enum {
22a818c563SMathieu Poirier 	/* Starting with 0x0 */
2342b2b570SMike Leach 	CS_HEADER_VERSION,
24a818c563SMathieu Poirier 	/* PMU->type (32 bit), total # of CPUs (32 bit) */
25a818c563SMathieu Poirier 	CS_PMU_TYPE_CPUS,
26a818c563SMathieu Poirier 	CS_ETM_SNAPSHOT,
2742b2b570SMike Leach 	CS_HEADER_VERSION_MAX,
28a818c563SMathieu Poirier };
29a818c563SMathieu Poirier 
3042b2b570SMike Leach /*
3142b2b570SMike Leach  * Update the version for new format.
3242b2b570SMike Leach  *
3309277295SMike Leach  * Version 1: format adds a param count to the per cpu metadata.
3442b2b570SMike Leach  * This allows easy adding of new metadata parameters.
3542b2b570SMike Leach  * Requires that new params always added after current ones.
3642b2b570SMike Leach  * Also allows client reader to handle file versions that are different by
3742b2b570SMike Leach  * checking the number of params in the file vs the number expected.
3809277295SMike Leach  *
3909277295SMike Leach  * Version 2: Drivers will use PERF_RECORD_AUX_OUTPUT_HW_ID to output
4009277295SMike Leach  * CoreSight Trace ID. ...TRACEIDR metadata will be set to legacy values
4109277295SMike Leach  * but with addition flags.
4242b2b570SMike Leach  */
4309277295SMike Leach #define CS_HEADER_CURRENT_VERSION	2
4442b2b570SMike Leach 
45a818c563SMathieu Poirier /* Beginning of header common to both ETMv3 and V4 */
46a818c563SMathieu Poirier enum {
47a818c563SMathieu Poirier 	CS_ETM_MAGIC,
48a818c563SMathieu Poirier 	CS_ETM_CPU,
4942b2b570SMike Leach 	/* Number of trace config params in following ETM specific block */
5042b2b570SMike Leach 	CS_ETM_NR_TRC_PARAMS,
5142b2b570SMike Leach 	CS_ETM_COMMON_BLK_MAX_V1,
52a818c563SMathieu Poirier };
53a818c563SMathieu Poirier 
54a818c563SMathieu Poirier /* ETMv3/PTM metadata */
55a818c563SMathieu Poirier enum {
56a818c563SMathieu Poirier 	/* Dynamic, configurable parameters */
5742b2b570SMike Leach 	CS_ETM_ETMCR = CS_ETM_COMMON_BLK_MAX_V1,
58a818c563SMathieu Poirier 	CS_ETM_ETMTRACEIDR,
59a818c563SMathieu Poirier 	/* RO, taken from sysFS */
60a818c563SMathieu Poirier 	CS_ETM_ETMCCER,
61a818c563SMathieu Poirier 	CS_ETM_ETMIDR,
62a818c563SMathieu Poirier 	CS_ETM_PRIV_MAX,
63a818c563SMathieu Poirier };
64a818c563SMathieu Poirier 
6542b2b570SMike Leach /* define fixed version 0 length - allow new format reader to read old files. */
6642b2b570SMike Leach #define CS_ETM_NR_TRC_PARAMS_V0 (CS_ETM_ETMIDR - CS_ETM_ETMCR + 1)
6742b2b570SMike Leach 
68a818c563SMathieu Poirier /* ETMv4 metadata */
69a818c563SMathieu Poirier enum {
70a818c563SMathieu Poirier 	/* Dynamic, configurable parameters */
7142b2b570SMike Leach 	CS_ETMV4_TRCCONFIGR = CS_ETM_COMMON_BLK_MAX_V1,
72a818c563SMathieu Poirier 	CS_ETMV4_TRCTRACEIDR,
73a818c563SMathieu Poirier 	/* RO, taken from sysFS */
74a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR0,
75a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR1,
76a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR2,
77a818c563SMathieu Poirier 	CS_ETMV4_TRCIDR8,
78a818c563SMathieu Poirier 	CS_ETMV4_TRCAUTHSTATUS,
792e2f7ceeSGerman Gomez 	CS_ETMV4_TS_SOURCE,
80a818c563SMathieu Poirier 	CS_ETMV4_PRIV_MAX,
81a818c563SMathieu Poirier };
82a818c563SMathieu Poirier 
8342b2b570SMike Leach /* define fixed version 0 length - allow new format reader to read old files. */
8442b2b570SMike Leach #define CS_ETMV4_NR_TRC_PARAMS_V0 (CS_ETMV4_TRCAUTHSTATUS - CS_ETMV4_TRCCONFIGR + 1)
8542b2b570SMike Leach 
8696dce7f4SLeo Yan /*
8751ba8811SJames Clark  * ETE metadata is ETMv4 plus TRCDEVARCH register and doesn't support header V0 since it was
8851ba8811SJames Clark  * added in header V1
8951ba8811SJames Clark  */
9051ba8811SJames Clark enum {
91326163c5SGerman Gomez 	/* Dynamic, configurable parameters */
92326163c5SGerman Gomez 	CS_ETE_TRCCONFIGR = CS_ETM_COMMON_BLK_MAX_V1,
93326163c5SGerman Gomez 	CS_ETE_TRCTRACEIDR,
94326163c5SGerman Gomez 	/* RO, taken from sysFS */
95326163c5SGerman Gomez 	CS_ETE_TRCIDR0,
96326163c5SGerman Gomez 	CS_ETE_TRCIDR1,
97326163c5SGerman Gomez 	CS_ETE_TRCIDR2,
98326163c5SGerman Gomez 	CS_ETE_TRCIDR8,
99326163c5SGerman Gomez 	CS_ETE_TRCAUTHSTATUS,
100326163c5SGerman Gomez 	CS_ETE_TRCDEVARCH,
1012e2f7ceeSGerman Gomez 	CS_ETE_TS_SOURCE,
10251ba8811SJames Clark 	CS_ETE_PRIV_MAX
10351ba8811SJames Clark };
10451ba8811SJames Clark 
10551ba8811SJames Clark /*
10609277295SMike Leach  * Check for valid CoreSight trace ID. If an invalid value is present in the metadata,
10709277295SMike Leach  * then IDs are present in the hardware ID packet in the data file.
10809277295SMike Leach  */
10909277295SMike Leach #define CS_IS_VALID_TRACE_ID(id) ((id > 0) && (id < 0x70))
11009277295SMike Leach 
11109277295SMike Leach /*
11296dce7f4SLeo Yan  * ETMv3 exception encoding number:
1134d39c89fSIngo Molnar  * See Embedded Trace Macrocell specification (ARM IHI 0014Q)
11496dce7f4SLeo Yan  * table 7-12 Encoding of Exception[3:0] for non-ARMv7-M processors.
11596dce7f4SLeo Yan  */
11696dce7f4SLeo Yan enum {
11796dce7f4SLeo Yan 	CS_ETMV3_EXC_NONE = 0,
11896dce7f4SLeo Yan 	CS_ETMV3_EXC_DEBUG_HALT = 1,
11996dce7f4SLeo Yan 	CS_ETMV3_EXC_SMC = 2,
12096dce7f4SLeo Yan 	CS_ETMV3_EXC_HYP = 3,
12196dce7f4SLeo Yan 	CS_ETMV3_EXC_ASYNC_DATA_ABORT = 4,
12296dce7f4SLeo Yan 	CS_ETMV3_EXC_JAZELLE_THUMBEE = 5,
12396dce7f4SLeo Yan 	CS_ETMV3_EXC_PE_RESET = 8,
12496dce7f4SLeo Yan 	CS_ETMV3_EXC_UNDEFINED_INSTR = 9,
12596dce7f4SLeo Yan 	CS_ETMV3_EXC_SVC = 10,
12696dce7f4SLeo Yan 	CS_ETMV3_EXC_PREFETCH_ABORT = 11,
12796dce7f4SLeo Yan 	CS_ETMV3_EXC_DATA_FAULT = 12,
12896dce7f4SLeo Yan 	CS_ETMV3_EXC_GENERIC = 13,
12996dce7f4SLeo Yan 	CS_ETMV3_EXC_IRQ = 14,
13096dce7f4SLeo Yan 	CS_ETMV3_EXC_FIQ = 15,
13196dce7f4SLeo Yan };
13296dce7f4SLeo Yan 
13396dce7f4SLeo Yan /*
13496dce7f4SLeo Yan  * ETMv4 exception encoding number:
13596dce7f4SLeo Yan  * See ARM Embedded Trace Macrocell Architecture Specification (ARM IHI 0064D)
13696dce7f4SLeo Yan  * table 6-12 Possible values for the TYPE field in an Exception instruction
13796dce7f4SLeo Yan  * trace packet, for ARMv7-A/R and ARMv8-A/R PEs.
13896dce7f4SLeo Yan  */
13996dce7f4SLeo Yan enum {
14096dce7f4SLeo Yan 	CS_ETMV4_EXC_RESET = 0,
14196dce7f4SLeo Yan 	CS_ETMV4_EXC_DEBUG_HALT = 1,
14296dce7f4SLeo Yan 	CS_ETMV4_EXC_CALL = 2,
14396dce7f4SLeo Yan 	CS_ETMV4_EXC_TRAP = 3,
14496dce7f4SLeo Yan 	CS_ETMV4_EXC_SYSTEM_ERROR = 4,
14596dce7f4SLeo Yan 	CS_ETMV4_EXC_INST_DEBUG = 6,
14696dce7f4SLeo Yan 	CS_ETMV4_EXC_DATA_DEBUG = 7,
14796dce7f4SLeo Yan 	CS_ETMV4_EXC_ALIGNMENT = 10,
14896dce7f4SLeo Yan 	CS_ETMV4_EXC_INST_FAULT = 11,
14996dce7f4SLeo Yan 	CS_ETMV4_EXC_DATA_FAULT = 12,
15096dce7f4SLeo Yan 	CS_ETMV4_EXC_IRQ = 14,
15196dce7f4SLeo Yan 	CS_ETMV4_EXC_FIQ = 15,
15296dce7f4SLeo Yan 	CS_ETMV4_EXC_END = 31,
15396dce7f4SLeo Yan };
15496dce7f4SLeo Yan 
1555f7cb035SMathieu Poirier enum cs_etm_sample_type {
1565f7cb035SMathieu Poirier 	CS_ETM_EMPTY,
1575f7cb035SMathieu Poirier 	CS_ETM_RANGE,
1585f7cb035SMathieu Poirier 	CS_ETM_DISCONTINUITY,
1595f7cb035SMathieu Poirier 	CS_ETM_EXCEPTION,
1605f7cb035SMathieu Poirier 	CS_ETM_EXCEPTION_RET,
1615f7cb035SMathieu Poirier };
1625f7cb035SMathieu Poirier 
1635f7cb035SMathieu Poirier enum cs_etm_isa {
1645f7cb035SMathieu Poirier 	CS_ETM_ISA_UNKNOWN,
1655f7cb035SMathieu Poirier 	CS_ETM_ISA_A64,
1665f7cb035SMathieu Poirier 	CS_ETM_ISA_A32,
1675f7cb035SMathieu Poirier 	CS_ETM_ISA_T32,
1685f7cb035SMathieu Poirier };
1695f7cb035SMathieu Poirier 
1705f7cb035SMathieu Poirier struct cs_etm_queue;
1715f7cb035SMathieu Poirier 
1725f7cb035SMathieu Poirier struct cs_etm_packet {
1735f7cb035SMathieu Poirier 	enum cs_etm_sample_type sample_type;
1745f7cb035SMathieu Poirier 	enum cs_etm_isa isa;
1755f7cb035SMathieu Poirier 	u64 start_addr;
1765f7cb035SMathieu Poirier 	u64 end_addr;
1775f7cb035SMathieu Poirier 	u32 instr_count;
1785f7cb035SMathieu Poirier 	u32 last_instr_type;
1795f7cb035SMathieu Poirier 	u32 last_instr_subtype;
1805f7cb035SMathieu Poirier 	u32 flags;
1815f7cb035SMathieu Poirier 	u32 exception_number;
1821764ce06SJames Clark 	bool last_instr_cond;
1831764ce06SJames Clark 	bool last_instr_taken_branch;
1845f7cb035SMathieu Poirier 	u8 last_instr_size;
1855f7cb035SMathieu Poirier 	u8 trace_chan_id;
1865f7cb035SMathieu Poirier 	int cpu;
1875f7cb035SMathieu Poirier };
1885f7cb035SMathieu Poirier 
1895f7cb035SMathieu Poirier #define CS_ETM_PACKET_MAX_BUFFER 1024
1905f7cb035SMathieu Poirier 
191c7bfa2fdSMathieu Poirier /*
192c7bfa2fdSMathieu Poirier  * When working with per-thread scenarios the process under trace can
193c7bfa2fdSMathieu Poirier  * be scheduled on any CPU and as such, more than one traceID may be
194c7bfa2fdSMathieu Poirier  * associated with the same process.  Since a traceID of '0' is illegal
195c7bfa2fdSMathieu Poirier  * as per the CoreSight architecture, use that specific value to
196c7bfa2fdSMathieu Poirier  * identify the queue where all packets (with any traceID) are
197c7bfa2fdSMathieu Poirier  * aggregated.
198c7bfa2fdSMathieu Poirier  */
199c7bfa2fdSMathieu Poirier #define CS_ETM_PER_THREAD_TRACEID 0
200c7bfa2fdSMathieu Poirier 
2015f7cb035SMathieu Poirier struct cs_etm_packet_queue {
2025f7cb035SMathieu Poirier 	u32 packet_count;
2035f7cb035SMathieu Poirier 	u32 head;
2045f7cb035SMathieu Poirier 	u32 tail;
205675f302fSMathieu Poirier 	u32 instr_count;
206a7fe9a44SGerman Gomez 	u64 cs_timestamp; /* Timestamp from trace data, converted to ns if possible */
207aadd6ba4SJames Clark 	u64 next_cs_timestamp;
2085f7cb035SMathieu Poirier 	struct cs_etm_packet packet_buffer[CS_ETM_PACKET_MAX_BUFFER];
2095f7cb035SMathieu Poirier };
2105f7cb035SMathieu Poirier 
211a818c563SMathieu Poirier #define KiB(x) ((x) * 1024)
212a818c563SMathieu Poirier #define MiB(x) ((x) * 1024 * 1024)
213a818c563SMathieu Poirier 
2145f7cb035SMathieu Poirier #define CS_ETM_INVAL_ADDR 0xdeadbeefdeadbeefUL
2155f7cb035SMathieu Poirier 
2163399ad9aSMathieu Poirier #define BMVAL(val, lsb, msb)	((val & GENMASK(msb, lsb)) >> lsb)
2173399ad9aSMathieu Poirier 
21842b2b570SMike Leach #define CS_ETM_HEADER_SIZE (CS_HEADER_VERSION_MAX * sizeof(u64))
219a818c563SMathieu Poirier 
2202507a3d9SMathieu Poirier #define __perf_cs_etmv3_magic 0x3030303030303030ULL
2212507a3d9SMathieu Poirier #define __perf_cs_etmv4_magic 0x4040404040404040ULL
22251ba8811SJames Clark #define __perf_cs_ete_magic   0x5050505050505050ULL
223a818c563SMathieu Poirier #define CS_ETMV3_PRIV_SIZE (CS_ETM_PRIV_MAX * sizeof(u64))
224a818c563SMathieu Poirier #define CS_ETMV4_PRIV_SIZE (CS_ETMV4_PRIV_MAX * sizeof(u64))
22551ba8811SJames Clark #define CS_ETE_PRIV_SIZE (CS_ETE_PRIV_MAX * sizeof(u64))
226a818c563SMathieu Poirier 
22755c1de99SJames Clark #define INFO_HEADER_SIZE (sizeof(((struct perf_record_auxtrace_info *)0)->type) + \
22855c1de99SJames Clark 			  sizeof(((struct perf_record_auxtrace_info *)0)->reserved__))
22955c1de99SJames Clark 
23015d4371bSJames Clark /* CoreSight trace ID is currently the bottom 7 bits of the value */
23115d4371bSJames Clark #define CORESIGHT_TRACE_ID_VAL_MASK	GENMASK(6, 0)
23215d4371bSJames Clark 
23315d4371bSJames Clark /*
23415d4371bSJames Clark  * perf record will set the legacy meta data values as unused initially.
23515d4371bSJames Clark  * This allows perf report to manage the decoders created when dynamic
23615d4371bSJames Clark  * allocation in operation.
23715d4371bSJames Clark  */
23815d4371bSJames Clark #define CORESIGHT_TRACE_ID_UNUSED_FLAG	BIT(31)
23915d4371bSJames Clark 
24015d4371bSJames Clark /* Value to set for unused trace ID values */
24115d4371bSJames Clark #define CORESIGHT_TRACE_ID_UNUSED_VAL	0x7F
24215d4371bSJames Clark 
243440a23b3SMathieu Poirier int cs_etm__process_auxtrace_info(union perf_event *event,
244440a23b3SMathieu Poirier 				  struct perf_session *session);
2457bfc1544SJames Clark struct perf_event_attr *cs_etm_get_default_config(struct perf_pmu *pmu);
24655c1de99SJames Clark 
2475414b532SJames Clark enum cs_etm_pid_fmt {
2485414b532SJames Clark 	CS_ETM_PIDFMT_NONE,
2495414b532SJames Clark 	CS_ETM_PIDFMT_CTXTID,
2505414b532SJames Clark 	CS_ETM_PIDFMT_CTXTID2
2515414b532SJames Clark };
2525414b532SJames Clark 
25355c1de99SJames Clark #ifdef HAVE_CSTRACE_SUPPORT
254*8d3031d3SJames Clark #include <opencsd/ocsd_if_types.h>
25595c6fe97SLeo Yan int cs_etm__get_cpu(u8 trace_chan_id, int *cpu);
256*8d3031d3SJames Clark enum cs_etm_pid_fmt cs_etm__get_pid_fmt(struct cs_etm_queue *etmq);
257*8d3031d3SJames Clark int cs_etm__etmq_set_tid_el(struct cs_etm_queue *etmq, pid_t tid,
258*8d3031d3SJames Clark 			    u8 trace_chan_id, ocsd_ex_level el);
259675f302fSMathieu Poirier bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq);
260675f302fSMathieu Poirier void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
261675f302fSMathieu Poirier 					      u8 trace_chan_id);
2625f7cb035SMathieu Poirier struct cs_etm_packet_queue
263c7bfa2fdSMathieu Poirier *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id);
26455c1de99SJames Clark int cs_etm__process_auxtrace_info_full(union perf_event *event __maybe_unused,
26555c1de99SJames Clark 				       struct perf_session *session __maybe_unused);
266a7fe9a44SGerman Gomez u64 cs_etm__convert_sample_time(struct cs_etm_queue *etmq, u64 cs_timestamp);
267440a23b3SMathieu Poirier #else
268440a23b3SMathieu Poirier static inline int
cs_etm__process_auxtrace_info_full(union perf_event * event __maybe_unused,struct perf_session * session __maybe_unused)26955c1de99SJames Clark cs_etm__process_auxtrace_info_full(union perf_event *event __maybe_unused,
270440a23b3SMathieu Poirier 				   struct perf_session *session __maybe_unused)
271440a23b3SMathieu Poirier {
272fe55ba18SJames Clark 	pr_err("\nCS ETM Trace: OpenCSD is not linked in, please recompile with CORESIGHT=1\n");
273440a23b3SMathieu Poirier 	return -1;
274440a23b3SMathieu Poirier }
275440a23b3SMathieu Poirier #endif
276440a23b3SMathieu Poirier 
277a818c563SMathieu Poirier #endif
278