xref: /openbmc/linux/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
14db25f66STan Xiaojun /* SPDX-License-Identifier: GPL-2.0 */
24db25f66STan Xiaojun /*
34db25f66STan Xiaojun  * Arm Statistical Profiling Extensions (SPE) support
44db25f66STan Xiaojun  * Copyright (c) 2017-2018, Arm Ltd.
54db25f66STan Xiaojun  */
64db25f66STan Xiaojun 
74db25f66STan Xiaojun #ifndef INCLUDE__ARM_SPE_PKT_DECODER_H__
84db25f66STan Xiaojun #define INCLUDE__ARM_SPE_PKT_DECODER_H__
94db25f66STan Xiaojun 
104db25f66STan Xiaojun #include <stddef.h>
114db25f66STan Xiaojun #include <stdint.h>
124db25f66STan Xiaojun 
134db25f66STan Xiaojun #define ARM_SPE_PKT_DESC_MAX		256
144db25f66STan Xiaojun 
154db25f66STan Xiaojun #define ARM_SPE_NEED_MORE_BYTES		-1
164db25f66STan Xiaojun #define ARM_SPE_BAD_PACKET		-2
174db25f66STan Xiaojun 
18a54ca194STan Xiaojun #define ARM_SPE_PKT_MAX_SZ		16
19a54ca194STan Xiaojun 
204db25f66STan Xiaojun enum arm_spe_pkt_type {
214db25f66STan Xiaojun 	ARM_SPE_BAD,
224db25f66STan Xiaojun 	ARM_SPE_PAD,
234db25f66STan Xiaojun 	ARM_SPE_END,
244db25f66STan Xiaojun 	ARM_SPE_TIMESTAMP,
254db25f66STan Xiaojun 	ARM_SPE_ADDRESS,
264db25f66STan Xiaojun 	ARM_SPE_COUNTER,
274db25f66STan Xiaojun 	ARM_SPE_CONTEXT,
284db25f66STan Xiaojun 	ARM_SPE_OP_TYPE,
294db25f66STan Xiaojun 	ARM_SPE_EVENTS,
304db25f66STan Xiaojun 	ARM_SPE_DATA_SOURCE,
314db25f66STan Xiaojun };
324db25f66STan Xiaojun 
334db25f66STan Xiaojun struct arm_spe_pkt {
344db25f66STan Xiaojun 	enum arm_spe_pkt_type	type;
354db25f66STan Xiaojun 	unsigned char		index;
364db25f66STan Xiaojun 	uint64_t		payload;
374db25f66STan Xiaojun };
384db25f66STan Xiaojun 
3911695142SLeo Yan /* Short header (HEADER0) and extended header (HEADER1) */
4011695142SLeo Yan #define SPE_HEADER0_PAD				0x0
4111695142SLeo Yan #define SPE_HEADER0_END				0x1
4211695142SLeo Yan #define SPE_HEADER0_TIMESTAMP			0x71
4311695142SLeo Yan /* Mask for event & data source */
4411695142SLeo Yan #define SPE_HEADER0_MASK1			(GENMASK_ULL(7, 6) | GENMASK_ULL(3, 0))
4511695142SLeo Yan #define SPE_HEADER0_EVENTS			0x42
4611695142SLeo Yan #define SPE_HEADER0_SOURCE			0x43
4711695142SLeo Yan /* Mask for context & operation */
4811695142SLeo Yan #define SPE_HEADER0_MASK2			GENMASK_ULL(7, 2)
4911695142SLeo Yan #define SPE_HEADER0_CONTEXT			0x64
5011695142SLeo Yan #define SPE_HEADER0_OP_TYPE			0x48
5111695142SLeo Yan /* Mask for extended format */
5211695142SLeo Yan #define SPE_HEADER0_EXTENDED			0x20
5311695142SLeo Yan /* Mask for address & counter */
5411695142SLeo Yan #define SPE_HEADER0_MASK3			GENMASK_ULL(7, 3)
5511695142SLeo Yan #define SPE_HEADER0_ADDRESS			0xb0
5611695142SLeo Yan #define SPE_HEADER0_COUNTER			0x98
5711695142SLeo Yan #define SPE_HEADER1_ALIGNMENT			0x0
5811695142SLeo Yan 
5909935ca7SLeo Yan #define SPE_HDR_SHORT_INDEX(h)			((h) & GENMASK_ULL(2, 0))
6009935ca7SLeo Yan #define SPE_HDR_EXTENDED_INDEX(h0, h1)		(((h0) & GENMASK_ULL(1, 0)) << 3 | \
6109935ca7SLeo Yan 						 SPE_HDR_SHORT_INDEX(h1))
62a54ca194STan Xiaojun 
6309935ca7SLeo Yan /* Address packet header */
6409935ca7SLeo Yan #define SPE_ADDR_PKT_HDR_INDEX_INS		0x0
6509935ca7SLeo Yan #define SPE_ADDR_PKT_HDR_INDEX_BRANCH		0x1
6609935ca7SLeo Yan #define SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT	0x2
6709935ca7SLeo Yan #define SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS	0x3
687105311cSRob Herring #define SPE_ADDR_PKT_HDR_INDEX_PREV_BRANCH	0x4
6909935ca7SLeo Yan 
7009935ca7SLeo Yan /* Address packet payload */
7109935ca7SLeo Yan #define SPE_ADDR_PKT_ADDR_BYTE7_SHIFT		56
7209935ca7SLeo Yan #define SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(v)	((v) & GENMASK_ULL(55, 0))
7309935ca7SLeo Yan #define SPE_ADDR_PKT_ADDR_GET_BYTE_6(v)		(((v) & GENMASK_ULL(55, 48)) >> 48)
7409935ca7SLeo Yan 
7509935ca7SLeo Yan #define SPE_ADDR_PKT_GET_NS(v)			(((v) & BIT_ULL(63)) >> 63)
7609935ca7SLeo Yan #define SPE_ADDR_PKT_GET_EL(v)			(((v) & GENMASK_ULL(62, 61)) >> 61)
773601e605SAndre Przywara #define SPE_ADDR_PKT_GET_CH(v)			(((v) & BIT_ULL(62)) >> 62)
783601e605SAndre Przywara #define SPE_ADDR_PKT_GET_PAT(v)			(((v) & GENMASK_ULL(59, 56)) >> 56)
7909935ca7SLeo Yan 
8009935ca7SLeo Yan #define SPE_ADDR_PKT_EL0			0
8109935ca7SLeo Yan #define SPE_ADDR_PKT_EL1			1
8209935ca7SLeo Yan #define SPE_ADDR_PKT_EL2			2
8309935ca7SLeo Yan #define SPE_ADDR_PKT_EL3			3
84a54ca194STan Xiaojun 
856550149eSLeo Yan /* Context packet header */
866550149eSLeo Yan #define SPE_CTX_PKT_HDR_INDEX(h)		((h) & GENMASK_ULL(1, 0))
876550149eSLeo Yan 
88d158aa40SLeo Yan /* Counter packet header */
89d158aa40SLeo Yan #define SPE_CNT_PKT_HDR_INDEX_TOTAL_LAT		0x0
90d158aa40SLeo Yan #define SPE_CNT_PKT_HDR_INDEX_ISSUE_LAT		0x1
91d158aa40SLeo Yan #define SPE_CNT_PKT_HDR_INDEX_TRANS_LAT		0x2
92d158aa40SLeo Yan 
93889d1a67SLeo Yan /* Event packet payload */
94889d1a67SLeo Yan enum arm_spe_events {
95889d1a67SLeo Yan 	EV_EXCEPTION_GEN	= 0,
96889d1a67SLeo Yan 	EV_RETIRED		= 1,
97889d1a67SLeo Yan 	EV_L1D_ACCESS		= 2,
98889d1a67SLeo Yan 	EV_L1D_REFILL		= 3,
99889d1a67SLeo Yan 	EV_TLB_ACCESS		= 4,
100889d1a67SLeo Yan 	EV_TLB_WALK		= 5,
101889d1a67SLeo Yan 	EV_NOT_TAKEN		= 6,
102889d1a67SLeo Yan 	EV_MISPRED		= 7,
103889d1a67SLeo Yan 	EV_LLC_ACCESS		= 8,
104889d1a67SLeo Yan 	EV_LLC_MISS		= 9,
105889d1a67SLeo Yan 	EV_REMOTE_ACCESS	= 10,
106889d1a67SLeo Yan 	EV_ALIGNMENT		= 11,
107889d1a67SLeo Yan 	EV_PARTIAL_PREDICATE	= 17,
108889d1a67SLeo Yan 	EV_EMPTY_PREDICATE	= 18,
109889d1a67SLeo Yan };
110889d1a67SLeo Yan 
111e771218fSLeo Yan /* Operation packet header */
112e771218fSLeo Yan #define SPE_OP_PKT_HDR_CLASS(h)			((h) & GENMASK_ULL(1, 0))
113e771218fSLeo Yan #define SPE_OP_PKT_HDR_CLASS_OTHER		0x0
114e771218fSLeo Yan #define SPE_OP_PKT_HDR_CLASS_LD_ST_ATOMIC	0x1
115e771218fSLeo Yan #define SPE_OP_PKT_HDR_CLASS_BR_ERET		0x2
116e771218fSLeo Yan 
11705e91e7fSWei Li #define SPE_OP_PKT_IS_OTHER_SVE_OP(v)		(((v) & (BIT(7) | BIT(3) | BIT(0))) == 0x8)
11805e91e7fSWei Li 
119e771218fSLeo Yan #define SPE_OP_PKT_COND				BIT(0)
120e771218fSLeo Yan 
121e771218fSLeo Yan #define SPE_OP_PKT_LDST_SUBCLASS_GET(v)		((v) & GENMASK_ULL(7, 1))
122e771218fSLeo Yan #define SPE_OP_PKT_LDST_SUBCLASS_GP_REG		0x0
123e771218fSLeo Yan #define SPE_OP_PKT_LDST_SUBCLASS_SIMD_FP	0x4
124e771218fSLeo Yan #define SPE_OP_PKT_LDST_SUBCLASS_UNSPEC_REG	0x10
125e771218fSLeo Yan #define SPE_OP_PKT_LDST_SUBCLASS_NV_SYSREG	0x30
126*34fb6040SRob Herring #define SPE_OP_PKT_LDST_SUBCLASS_MTE_TAG	0x14
127*34fb6040SRob Herring #define SPE_OP_PKT_LDST_SUBCLASS_MEMCPY		0x20
128*34fb6040SRob Herring #define SPE_OP_PKT_LDST_SUBCLASS_MEMSET		0x25
129e771218fSLeo Yan 
130e771218fSLeo Yan #define SPE_OP_PKT_IS_LDST_ATOMIC(v)		(((v) & (GENMASK_ULL(7, 5) | BIT(1))) == 0x2)
131e771218fSLeo Yan 
132e771218fSLeo Yan #define SPE_OP_PKT_AR				BIT(4)
133e771218fSLeo Yan #define SPE_OP_PKT_EXCL				BIT(3)
134e771218fSLeo Yan #define SPE_OP_PKT_AT				BIT(2)
135e771218fSLeo Yan #define SPE_OP_PKT_ST				BIT(0)
136e771218fSLeo Yan 
13705e91e7fSWei Li #define SPE_OP_PKT_IS_LDST_SVE(v)		(((v) & (BIT(3) | BIT(1))) == 0x8)
13805e91e7fSWei Li 
13905e91e7fSWei Li #define SPE_OP_PKT_SVE_SG			BIT(7)
14005e91e7fSWei Li /*
14105e91e7fSWei Li  * SVE effective vector length (EVL) is stored in byte 0 bits [6:4];
14205e91e7fSWei Li  * the length is rounded up to a power of two and use 32 as one step,
14305e91e7fSWei Li  * so EVL calculation is:
14405e91e7fSWei Li  *
14505e91e7fSWei Li  *   32 * (2 ^ bits [6:4]) = 32 << (bits [6:4])
14605e91e7fSWei Li  */
14705e91e7fSWei Li #define SPE_OP_PKG_SVE_EVL(v)			(32 << (((v) & GENMASK_ULL(6, 4)) >> 4))
14805e91e7fSWei Li #define SPE_OP_PKT_SVE_PRED			BIT(2)
14905e91e7fSWei Li #define SPE_OP_PKT_SVE_FP			BIT(1)
15005e91e7fSWei Li 
151e771218fSLeo Yan #define SPE_OP_PKT_IS_INDIRECT_BRANCH(v)	(((v) & GENMASK_ULL(7, 1)) == 0x2)
152e771218fSLeo Yan 
1534db25f66STan Xiaojun const char *arm_spe_pkt_name(enum arm_spe_pkt_type);
1544db25f66STan Xiaojun 
1554db25f66STan Xiaojun int arm_spe_get_packet(const unsigned char *buf, size_t len,
1564db25f66STan Xiaojun 		       struct arm_spe_pkt *packet);
1574db25f66STan Xiaojun 
1584db25f66STan Xiaojun int arm_spe_pkt_desc(const struct arm_spe_pkt *packet, char *buf, size_t len);
1594db25f66STan Xiaojun #endif
160