1 // SPDX-License-Identifier: GPL-2.0
3 * Arm Statistical Profiling Extensions (SPE) support
4 * Copyright (c) 2017-2018, Arm Ltd.
12 #include "arm-spe-pkt-decoder.h"
14 #define BIT(n) (1ULL << (n))
16 #define NS_FLAG BIT(63)
17 #define EL_FLAG (BIT(62) | BIT(61))
19 #define SPE_HEADER0_PAD 0x0
20 #define SPE_HEADER0_END 0x1
21 #define SPE_HEADER0_ADDRESS 0x30 /* address packet (short) */
22 #define SPE_HEADER0_ADDRESS_MASK 0x38
23 #define SPE_HEADER0_COUNTER 0x18 /* counter packet (short) */
24 #define SPE_HEADER0_COUNTER_MASK 0x38
25 #define SPE_HEADER0_TIMESTAMP 0x71
26 #define SPE_HEADER0_TIMESTAMP 0x71
27 #define SPE_HEADER0_EVENTS 0x2
28 #define SPE_HEADER0_EVENTS_MASK 0xf
29 #define SPE_HEADER0_SOURCE 0x3
30 #define SPE_HEADER0_SOURCE_MASK 0xf
31 #define SPE_HEADER0_CONTEXT 0x24
32 #define SPE_HEADER0_CONTEXT_MASK 0x3c
33 #define SPE_HEADER0_OP_TYPE 0x8
34 #define SPE_HEADER0_OP_TYPE_MASK 0x3c
35 #define SPE_HEADER1_ALIGNMENT 0x0
36 #define SPE_HEADER1_ADDRESS 0xb0 /* address packet (extended) */
37 #define SPE_HEADER1_ADDRESS_MASK 0xf8
38 #define SPE_HEADER1_COUNTER 0x98 /* counter packet (extended) */
39 #define SPE_HEADER1_COUNTER_MASK 0xf8
41 #if __BYTE_ORDER == __BIG_ENDIAN
42 #define le16_to_cpu bswap_16
43 #define le32_to_cpu bswap_32
44 #define le64_to_cpu bswap_64
45 #define memcpy_le64(d, s, n) do { \
46 memcpy((d), (s), (n)); \
47 *(d) = le64_to_cpu(*(d)); \
53 #define memcpy_le64 memcpy
56 static const char * const arm_spe_packet_name
[] = {
57 [ARM_SPE_PAD
] = "PAD",
58 [ARM_SPE_END
] = "END",
59 [ARM_SPE_TIMESTAMP
] = "TS",
60 [ARM_SPE_ADDRESS
] = "ADDR",
61 [ARM_SPE_COUNTER
] = "LAT",
62 [ARM_SPE_CONTEXT
] = "CONTEXT",
63 [ARM_SPE_OP_TYPE
] = "OP-TYPE",
64 [ARM_SPE_EVENTS
] = "EVENTS",
65 [ARM_SPE_DATA_SOURCE
] = "DATA-SOURCE",
68 const char *arm_spe_pkt_name(enum arm_spe_pkt_type type
)
70 return arm_spe_packet_name
[type
];
73 /* return ARM SPE payload size from its encoding,
74 * which is in bits 5:4 of the byte.
80 static int payloadlen(unsigned char byte
)
82 return 1 << ((byte
& 0x30) >> 4);
85 static int arm_spe_get_payload(const unsigned char *buf
, size_t len
,
86 struct arm_spe_pkt
*packet
)
88 size_t payload_len
= payloadlen(buf
[0]);
90 if (len
< 1 + payload_len
)
91 return ARM_SPE_NEED_MORE_BYTES
;
95 switch (payload_len
) {
96 case 1: packet
->payload
= *(uint8_t *)buf
; break;
97 case 2: packet
->payload
= le16_to_cpu(*(uint16_t *)buf
); break;
98 case 4: packet
->payload
= le32_to_cpu(*(uint32_t *)buf
); break;
99 case 8: packet
->payload
= le64_to_cpu(*(uint64_t *)buf
); break;
100 default: return ARM_SPE_BAD_PACKET
;
103 return 1 + payload_len
;
106 static int arm_spe_get_pad(struct arm_spe_pkt
*packet
)
108 packet
->type
= ARM_SPE_PAD
;
112 static int arm_spe_get_alignment(const unsigned char *buf
, size_t len
,
113 struct arm_spe_pkt
*packet
)
115 unsigned int alignment
= 1 << ((buf
[0] & 0xf) + 1);
118 return ARM_SPE_NEED_MORE_BYTES
;
120 packet
->type
= ARM_SPE_PAD
;
121 return alignment
- (((uintptr_t)buf
) & (alignment
- 1));
124 static int arm_spe_get_end(struct arm_spe_pkt
*packet
)
126 packet
->type
= ARM_SPE_END
;
130 static int arm_spe_get_timestamp(const unsigned char *buf
, size_t len
,
131 struct arm_spe_pkt
*packet
)
133 packet
->type
= ARM_SPE_TIMESTAMP
;
134 return arm_spe_get_payload(buf
, len
, packet
);
137 static int arm_spe_get_events(const unsigned char *buf
, size_t len
,
138 struct arm_spe_pkt
*packet
)
140 int ret
= arm_spe_get_payload(buf
, len
, packet
);
142 packet
->type
= ARM_SPE_EVENTS
;
144 /* we use index to identify Events with a less number of
145 * comparisons in arm_spe_pkt_desc(): E.g., the LLC-ACCESS,
146 * LLC-REFILL, and REMOTE-ACCESS events are identified iff
149 packet
->index
= ret
- 1;
154 static int arm_spe_get_data_source(const unsigned char *buf
, size_t len
,
155 struct arm_spe_pkt
*packet
)
157 packet
->type
= ARM_SPE_DATA_SOURCE
;
158 return arm_spe_get_payload(buf
, len
, packet
);
161 static int arm_spe_get_context(const unsigned char *buf
, size_t len
,
162 struct arm_spe_pkt
*packet
)
164 packet
->type
= ARM_SPE_CONTEXT
;
165 packet
->index
= buf
[0] & 0x3;
167 return arm_spe_get_payload(buf
, len
, packet
);
170 static int arm_spe_get_op_type(const unsigned char *buf
, size_t len
,
171 struct arm_spe_pkt
*packet
)
173 packet
->type
= ARM_SPE_OP_TYPE
;
174 packet
->index
= buf
[0] & 0x3;
175 return arm_spe_get_payload(buf
, len
, packet
);
178 static int arm_spe_get_counter(const unsigned char *buf
, size_t len
,
179 const unsigned char ext_hdr
, struct arm_spe_pkt
*packet
)
182 return ARM_SPE_NEED_MORE_BYTES
;
184 packet
->type
= ARM_SPE_COUNTER
;
186 packet
->index
= ((buf
[0] & 0x3) << 3) | (buf
[1] & 0x7);
188 packet
->index
= buf
[0] & 0x7;
190 packet
->payload
= le16_to_cpu(*(uint16_t *)(buf
+ 1));
192 return 1 + ext_hdr
+ 2;
195 static int arm_spe_get_addr(const unsigned char *buf
, size_t len
,
196 const unsigned char ext_hdr
, struct arm_spe_pkt
*packet
)
199 return ARM_SPE_NEED_MORE_BYTES
;
201 packet
->type
= ARM_SPE_ADDRESS
;
203 packet
->index
= ((buf
[0] & 0x3) << 3) | (buf
[1] & 0x7);
205 packet
->index
= buf
[0] & 0x7;
207 memcpy_le64(&packet
->payload
, buf
+ 1, 8);
209 return 1 + ext_hdr
+ 8;
212 static int arm_spe_do_get_packet(const unsigned char *buf
, size_t len
,
213 struct arm_spe_pkt
*packet
)
217 memset(packet
, 0, sizeof(struct arm_spe_pkt
));
220 return ARM_SPE_NEED_MORE_BYTES
;
223 if (byte
== SPE_HEADER0_PAD
)
224 return arm_spe_get_pad(packet
);
225 else if (byte
== SPE_HEADER0_END
) /* no timestamp at end of record */
226 return arm_spe_get_end(packet
);
227 else if (byte
& 0xc0 /* 0y11xxxxxx */) {
229 if ((byte
& SPE_HEADER0_ADDRESS_MASK
) == SPE_HEADER0_ADDRESS
)
230 return arm_spe_get_addr(buf
, len
, 0, packet
);
231 if ((byte
& SPE_HEADER0_COUNTER_MASK
) == SPE_HEADER0_COUNTER
)
232 return arm_spe_get_counter(buf
, len
, 0, packet
);
234 if (byte
== SPE_HEADER0_TIMESTAMP
)
235 return arm_spe_get_timestamp(buf
, len
, packet
);
236 else if ((byte
& SPE_HEADER0_EVENTS_MASK
) == SPE_HEADER0_EVENTS
)
237 return arm_spe_get_events(buf
, len
, packet
);
238 else if ((byte
& SPE_HEADER0_SOURCE_MASK
) == SPE_HEADER0_SOURCE
)
239 return arm_spe_get_data_source(buf
, len
, packet
);
240 else if ((byte
& SPE_HEADER0_CONTEXT_MASK
) == SPE_HEADER0_CONTEXT
)
241 return arm_spe_get_context(buf
, len
, packet
);
242 else if ((byte
& SPE_HEADER0_OP_TYPE_MASK
) == SPE_HEADER0_OP_TYPE
)
243 return arm_spe_get_op_type(buf
, len
, packet
);
244 } else if ((byte
& 0xe0) == 0x20 /* 0y001xxxxx */) {
247 if (byte
== SPE_HEADER1_ALIGNMENT
)
248 return arm_spe_get_alignment(buf
, len
, packet
);
249 else if ((byte
& SPE_HEADER1_ADDRESS_MASK
) == SPE_HEADER1_ADDRESS
)
250 return arm_spe_get_addr(buf
, len
, 1, packet
);
251 else if ((byte
& SPE_HEADER1_COUNTER_MASK
) == SPE_HEADER1_COUNTER
)
252 return arm_spe_get_counter(buf
, len
, 1, packet
);
255 return ARM_SPE_BAD_PACKET
;
258 int arm_spe_get_packet(const unsigned char *buf
, size_t len
,
259 struct arm_spe_pkt
*packet
)
263 ret
= arm_spe_do_get_packet(buf
, len
, packet
);
264 /* put multiple consecutive PADs on the same line, up to
265 * the fixed-width output format of 16 bytes per line.
267 if (ret
> 0 && packet
->type
== ARM_SPE_PAD
) {
268 while (ret
< 16 && len
> (size_t)ret
&& !buf
[ret
])
274 int arm_spe_pkt_desc(const struct arm_spe_pkt
*packet
, char *buf
,
277 int ret
, ns
, el
, idx
= packet
->index
;
278 unsigned long long payload
= packet
->payload
;
279 const char *name
= arm_spe_pkt_name(packet
->type
);
281 switch (packet
->type
) {
285 return snprintf(buf
, buf_len
, "%s", name
);
286 case ARM_SPE_EVENTS
: {
287 size_t blen
= buf_len
;
290 ret
= snprintf(buf
, buf_len
, "EV");
294 ret
= snprintf(buf
, buf_len
, " EXCEPTION-GEN");
299 ret
= snprintf(buf
, buf_len
, " RETIRED");
304 ret
= snprintf(buf
, buf_len
, " L1D-ACCESS");
309 ret
= snprintf(buf
, buf_len
, " L1D-REFILL");
313 if (payload
& 0x10) {
314 ret
= snprintf(buf
, buf_len
, " TLB-ACCESS");
318 if (payload
& 0x20) {
319 ret
= snprintf(buf
, buf_len
, " TLB-REFILL");
323 if (payload
& 0x40) {
324 ret
= snprintf(buf
, buf_len
, " NOT-TAKEN");
328 if (payload
& 0x80) {
329 ret
= snprintf(buf
, buf_len
, " MISPRED");
334 if (payload
& 0x100) {
335 ret
= snprintf(buf
, buf_len
, " LLC-ACCESS");
339 if (payload
& 0x200) {
340 ret
= snprintf(buf
, buf_len
, " LLC-REFILL");
344 if (payload
& 0x400) {
345 ret
= snprintf(buf
, buf_len
, " REMOTE-ACCESS");
353 return buf_len
- blen
;
355 case ARM_SPE_OP_TYPE
:
357 case 0: return snprintf(buf
, buf_len
, "%s", payload
& 0x1 ?
358 "COND-SELECT" : "INSN-OTHER");
360 size_t blen
= buf_len
;
363 ret
= snprintf(buf
, buf_len
, "ST");
365 ret
= snprintf(buf
, buf_len
, "LD");
370 ret
= snprintf(buf
, buf_len
, " AT");
375 ret
= snprintf(buf
, buf_len
, " EXCL");
379 if (payload
& 0x10) {
380 ret
= snprintf(buf
, buf_len
, " AR");
384 } else if (payload
& 0x4) {
385 ret
= snprintf(buf
, buf_len
, " SIMD-FP");
392 return buf_len
- blen
;
395 size_t blen
= buf_len
;
397 ret
= snprintf(buf
, buf_len
, "B");
401 ret
= snprintf(buf
, buf_len
, " COND");
406 ret
= snprintf(buf
, buf_len
, " IND");
413 return buf_len
- blen
;
417 case ARM_SPE_DATA_SOURCE
:
418 case ARM_SPE_TIMESTAMP
:
419 return snprintf(buf
, buf_len
, "%s %lld", name
, payload
);
420 case ARM_SPE_ADDRESS
:
423 case 1: ns
= !!(packet
->payload
& NS_FLAG
);
424 el
= (packet
->payload
& EL_FLAG
) >> 61;
425 payload
&= ~(0xffULL
<< 56);
426 return snprintf(buf
, buf_len
, "%s 0x%llx el%d ns=%d",
427 (idx
== 1) ? "TGT" : "PC", payload
, el
, ns
);
428 case 2: return snprintf(buf
, buf_len
, "VA 0x%llx", payload
);
429 case 3: ns
= !!(packet
->payload
& NS_FLAG
);
430 payload
&= ~(0xffULL
<< 56);
431 return snprintf(buf
, buf_len
, "PA 0x%llx ns=%d",
435 case ARM_SPE_CONTEXT
:
436 return snprintf(buf
, buf_len
, "%s 0x%lx el%d", name
,
437 (unsigned long)payload
, idx
+ 1);
438 case ARM_SPE_COUNTER
: {
439 size_t blen
= buf_len
;
441 ret
= snprintf(buf
, buf_len
, "%s %d ", name
,
442 (unsigned short)payload
);
446 case 0: ret
= snprintf(buf
, buf_len
, "TOT"); break;
447 case 1: ret
= snprintf(buf
, buf_len
, "ISSUE"); break;
448 case 2: ret
= snprintf(buf
, buf_len
, "XLAT"); break;
454 return buf_len
- blen
;
460 return snprintf(buf
, buf_len
, "%s 0x%llx (%d)",
461 name
, payload
, packet
->index
);