1 // SPDX-License-Identifier: GPL-2.0
3 * Arm Statistical Profiling Extensions (SPE) support
4 * Copyright (c) 2017-2018, Arm Ltd.
11 #include <linux/bitops.h>
14 #include "arm-spe-pkt-decoder.h"
16 #if __BYTE_ORDER == __BIG_ENDIAN
17 #define le16_to_cpu bswap_16
18 #define le32_to_cpu bswap_32
19 #define le64_to_cpu bswap_64
20 #define memcpy_le64(d, s, n) do { \
21 memcpy((d), (s), (n)); \
22 *(d) = le64_to_cpu(*(d)); \
28 #define memcpy_le64 memcpy
31 static const char * const arm_spe_packet_name
[] = {
32 [ARM_SPE_PAD
] = "PAD",
33 [ARM_SPE_END
] = "END",
34 [ARM_SPE_TIMESTAMP
] = "TS",
35 [ARM_SPE_ADDRESS
] = "ADDR",
36 [ARM_SPE_COUNTER
] = "LAT",
37 [ARM_SPE_CONTEXT
] = "CONTEXT",
38 [ARM_SPE_OP_TYPE
] = "OP-TYPE",
39 [ARM_SPE_EVENTS
] = "EVENTS",
40 [ARM_SPE_DATA_SOURCE
] = "DATA-SOURCE",
43 const char *arm_spe_pkt_name(enum arm_spe_pkt_type type
)
45 return arm_spe_packet_name
[type
];
49 * Extracts the field "sz" from header bits and converts to bytes:
55 static unsigned int arm_spe_payload_len(unsigned char hdr
)
57 return 1U << ((hdr
& GENMASK_ULL(5, 4)) >> 4);
60 static int arm_spe_get_payload(const unsigned char *buf
, size_t len
,
61 unsigned char ext_hdr
,
62 struct arm_spe_pkt
*packet
)
64 size_t payload_len
= arm_spe_payload_len(buf
[ext_hdr
]);
66 if (len
< 1 + ext_hdr
+ payload_len
)
67 return ARM_SPE_NEED_MORE_BYTES
;
71 switch (payload_len
) {
72 case 1: packet
->payload
= *(uint8_t *)buf
; break;
73 case 2: packet
->payload
= le16_to_cpu(*(uint16_t *)buf
); break;
74 case 4: packet
->payload
= le32_to_cpu(*(uint32_t *)buf
); break;
75 case 8: packet
->payload
= le64_to_cpu(*(uint64_t *)buf
); break;
76 default: return ARM_SPE_BAD_PACKET
;
79 return 1 + ext_hdr
+ payload_len
;
82 static int arm_spe_get_pad(struct arm_spe_pkt
*packet
)
84 packet
->type
= ARM_SPE_PAD
;
88 static int arm_spe_get_alignment(const unsigned char *buf
, size_t len
,
89 struct arm_spe_pkt
*packet
)
91 unsigned int alignment
= 1 << ((buf
[0] & 0xf) + 1);
94 return ARM_SPE_NEED_MORE_BYTES
;
96 packet
->type
= ARM_SPE_PAD
;
97 return alignment
- (((uintptr_t)buf
) & (alignment
- 1));
100 static int arm_spe_get_end(struct arm_spe_pkt
*packet
)
102 packet
->type
= ARM_SPE_END
;
106 static int arm_spe_get_timestamp(const unsigned char *buf
, size_t len
,
107 struct arm_spe_pkt
*packet
)
109 packet
->type
= ARM_SPE_TIMESTAMP
;
110 return arm_spe_get_payload(buf
, len
, 0, packet
);
113 static int arm_spe_get_events(const unsigned char *buf
, size_t len
,
114 struct arm_spe_pkt
*packet
)
116 packet
->type
= ARM_SPE_EVENTS
;
118 /* we use index to identify Events with a less number of
119 * comparisons in arm_spe_pkt_desc(): E.g., the LLC-ACCESS,
120 * LLC-REFILL, and REMOTE-ACCESS events are identified if
123 packet
->index
= arm_spe_payload_len(buf
[0]);
125 return arm_spe_get_payload(buf
, len
, 0, packet
);
128 static int arm_spe_get_data_source(const unsigned char *buf
, size_t len
,
129 struct arm_spe_pkt
*packet
)
131 packet
->type
= ARM_SPE_DATA_SOURCE
;
132 return arm_spe_get_payload(buf
, len
, 0, packet
);
135 static int arm_spe_get_context(const unsigned char *buf
, size_t len
,
136 struct arm_spe_pkt
*packet
)
138 packet
->type
= ARM_SPE_CONTEXT
;
139 packet
->index
= SPE_CTX_PKT_HDR_INDEX(buf
[0]);
140 return arm_spe_get_payload(buf
, len
, 0, packet
);
143 static int arm_spe_get_op_type(const unsigned char *buf
, size_t len
,
144 struct arm_spe_pkt
*packet
)
146 packet
->type
= ARM_SPE_OP_TYPE
;
147 packet
->index
= SPE_OP_PKT_HDR_CLASS(buf
[0]);
148 return arm_spe_get_payload(buf
, len
, 0, packet
);
151 static int arm_spe_get_counter(const unsigned char *buf
, size_t len
,
152 const unsigned char ext_hdr
, struct arm_spe_pkt
*packet
)
154 packet
->type
= ARM_SPE_COUNTER
;
157 packet
->index
= SPE_HDR_EXTENDED_INDEX(buf
[0], buf
[1]);
159 packet
->index
= SPE_HDR_SHORT_INDEX(buf
[0]);
161 return arm_spe_get_payload(buf
, len
, ext_hdr
, packet
);
164 static int arm_spe_get_addr(const unsigned char *buf
, size_t len
,
165 const unsigned char ext_hdr
, struct arm_spe_pkt
*packet
)
167 packet
->type
= ARM_SPE_ADDRESS
;
170 packet
->index
= SPE_HDR_EXTENDED_INDEX(buf
[0], buf
[1]);
172 packet
->index
= SPE_HDR_SHORT_INDEX(buf
[0]);
174 return arm_spe_get_payload(buf
, len
, ext_hdr
, packet
);
177 static int arm_spe_do_get_packet(const unsigned char *buf
, size_t len
,
178 struct arm_spe_pkt
*packet
)
181 unsigned char ext_hdr
= 0;
183 memset(packet
, 0, sizeof(struct arm_spe_pkt
));
186 return ARM_SPE_NEED_MORE_BYTES
;
190 if (hdr
== SPE_HEADER0_PAD
)
191 return arm_spe_get_pad(packet
);
193 if (hdr
== SPE_HEADER0_END
) /* no timestamp at end of record */
194 return arm_spe_get_end(packet
);
196 if (hdr
== SPE_HEADER0_TIMESTAMP
)
197 return arm_spe_get_timestamp(buf
, len
, packet
);
199 if ((hdr
& SPE_HEADER0_MASK1
) == SPE_HEADER0_EVENTS
)
200 return arm_spe_get_events(buf
, len
, packet
);
202 if ((hdr
& SPE_HEADER0_MASK1
) == SPE_HEADER0_SOURCE
)
203 return arm_spe_get_data_source(buf
, len
, packet
);
205 if ((hdr
& SPE_HEADER0_MASK2
) == SPE_HEADER0_CONTEXT
)
206 return arm_spe_get_context(buf
, len
, packet
);
208 if ((hdr
& SPE_HEADER0_MASK2
) == SPE_HEADER0_OP_TYPE
)
209 return arm_spe_get_op_type(buf
, len
, packet
);
211 if ((hdr
& SPE_HEADER0_MASK2
) == SPE_HEADER0_EXTENDED
) {
212 /* 16-bit extended format header */
216 if (hdr
== SPE_HEADER1_ALIGNMENT
)
217 return arm_spe_get_alignment(buf
, len
, packet
);
221 * The short format header's byte 0 or the extended format header's
222 * byte 1 has been assigned to 'hdr', which uses the same encoding for
223 * address packet and counter packet, so don't need to distinguish if
224 * it's short format or extended format and handle in once.
226 if ((hdr
& SPE_HEADER0_MASK3
) == SPE_HEADER0_ADDRESS
)
227 return arm_spe_get_addr(buf
, len
, ext_hdr
, packet
);
229 if ((hdr
& SPE_HEADER0_MASK3
) == SPE_HEADER0_COUNTER
)
230 return arm_spe_get_counter(buf
, len
, ext_hdr
, packet
);
232 return ARM_SPE_BAD_PACKET
;
235 int arm_spe_get_packet(const unsigned char *buf
, size_t len
,
236 struct arm_spe_pkt
*packet
)
240 ret
= arm_spe_do_get_packet(buf
, len
, packet
);
241 /* put multiple consecutive PADs on the same line, up to
242 * the fixed-width output format of 16 bytes per line.
244 if (ret
> 0 && packet
->type
== ARM_SPE_PAD
) {
245 while (ret
< 16 && len
> (size_t)ret
&& !buf
[ret
])
251 static int arm_spe_pkt_out_string(int *err
, char **buf_p
, size_t *blen
,
252 const char *fmt
, ...)
257 /* Bail out if any error occurred */
262 ret
= vsnprintf(*buf_p
, *blen
, fmt
, ap
);
270 * A return value of *blen or more means that the output was
271 * truncated and the buffer is overrun.
273 } else if ((size_t)ret
>= *blen
) {
274 (*buf_p
)[*blen
- 1] = '\0';
277 * Set *err to 'ret' to avoid overflow if tries to
278 * fill this buffer sequentially.
290 static int arm_spe_pkt_desc_event(const struct arm_spe_pkt
*packet
,
291 char *buf
, size_t buf_len
)
293 u64 payload
= packet
->payload
;
296 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "EV");
298 if (payload
& BIT(EV_EXCEPTION_GEN
))
299 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " EXCEPTION-GEN");
300 if (payload
& BIT(EV_RETIRED
))
301 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " RETIRED");
302 if (payload
& BIT(EV_L1D_ACCESS
))
303 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " L1D-ACCESS");
304 if (payload
& BIT(EV_L1D_REFILL
))
305 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " L1D-REFILL");
306 if (payload
& BIT(EV_TLB_ACCESS
))
307 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " TLB-ACCESS");
308 if (payload
& BIT(EV_TLB_WALK
))
309 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " TLB-REFILL");
310 if (payload
& BIT(EV_NOT_TAKEN
))
311 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " NOT-TAKEN");
312 if (payload
& BIT(EV_MISPRED
))
313 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " MISPRED");
314 if (payload
& BIT(EV_LLC_ACCESS
))
315 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " LLC-ACCESS");
316 if (payload
& BIT(EV_LLC_MISS
))
317 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " LLC-REFILL");
318 if (payload
& BIT(EV_REMOTE_ACCESS
))
319 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " REMOTE-ACCESS");
320 if (payload
& BIT(EV_ALIGNMENT
))
321 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " ALIGNMENT");
322 if (payload
& BIT(EV_PARTIAL_PREDICATE
))
323 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " SVE-PARTIAL-PRED");
324 if (payload
& BIT(EV_EMPTY_PREDICATE
))
325 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " SVE-EMPTY-PRED");
330 static int arm_spe_pkt_desc_op_type(const struct arm_spe_pkt
*packet
,
331 char *buf
, size_t buf_len
)
333 u64 payload
= packet
->payload
;
336 switch (packet
->index
) {
337 case SPE_OP_PKT_HDR_CLASS_OTHER
:
338 if (SPE_OP_PKT_IS_OTHER_SVE_OP(payload
)) {
339 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "SVE-OTHER");
341 /* SVE effective vector length */
342 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " EVLEN %d",
343 SPE_OP_PKG_SVE_EVL(payload
));
345 if (payload
& SPE_OP_PKT_SVE_FP
)
346 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " FP");
347 if (payload
& SPE_OP_PKT_SVE_PRED
)
348 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " PRED");
350 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "OTHER");
351 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " %s",
352 payload
& SPE_OP_PKT_COND
?
353 "COND-SELECT" : "INSN-OTHER");
356 case SPE_OP_PKT_HDR_CLASS_LD_ST_ATOMIC
:
357 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
,
358 payload
& 0x1 ? "ST" : "LD");
360 if (SPE_OP_PKT_IS_LDST_ATOMIC(payload
)) {
361 if (payload
& SPE_OP_PKT_AT
)
362 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " AT");
363 if (payload
& SPE_OP_PKT_EXCL
)
364 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " EXCL");
365 if (payload
& SPE_OP_PKT_AR
)
366 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " AR");
369 switch (SPE_OP_PKT_LDST_SUBCLASS_GET(payload
)) {
370 case SPE_OP_PKT_LDST_SUBCLASS_SIMD_FP
:
371 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " SIMD-FP");
373 case SPE_OP_PKT_LDST_SUBCLASS_GP_REG
:
374 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " GP-REG");
376 case SPE_OP_PKT_LDST_SUBCLASS_UNSPEC_REG
:
377 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " UNSPEC-REG");
379 case SPE_OP_PKT_LDST_SUBCLASS_NV_SYSREG
:
380 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " NV-SYSREG");
386 if (SPE_OP_PKT_IS_LDST_SVE(payload
)) {
387 /* SVE effective vector length */
388 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " EVLEN %d",
389 SPE_OP_PKG_SVE_EVL(payload
));
391 if (payload
& SPE_OP_PKT_SVE_PRED
)
392 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " PRED");
393 if (payload
& SPE_OP_PKT_SVE_SG
)
394 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " SG");
397 case SPE_OP_PKT_HDR_CLASS_BR_ERET
:
398 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "B");
400 if (payload
& SPE_OP_PKT_COND
)
401 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " COND");
403 if (SPE_OP_PKT_IS_INDIRECT_BRANCH(payload
))
404 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, " IND");
416 static int arm_spe_pkt_desc_addr(const struct arm_spe_pkt
*packet
,
417 char *buf
, size_t buf_len
)
419 int ns
, el
, idx
= packet
->index
;
421 u64 payload
= packet
->payload
;
425 case SPE_ADDR_PKT_HDR_INDEX_INS
:
426 case SPE_ADDR_PKT_HDR_INDEX_BRANCH
:
427 ns
= !!SPE_ADDR_PKT_GET_NS(payload
);
428 el
= SPE_ADDR_PKT_GET_EL(payload
);
429 payload
= SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload
);
430 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
,
431 "%s 0x%llx el%d ns=%d",
432 (idx
== 1) ? "TGT" : "PC", payload
, el
, ns
);
434 case SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT
:
435 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
,
436 "VA 0x%llx", payload
);
438 case SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS
:
439 ns
= !!SPE_ADDR_PKT_GET_NS(payload
);
440 ch
= !!SPE_ADDR_PKT_GET_CH(payload
);
441 pat
= SPE_ADDR_PKT_GET_PAT(payload
);
442 payload
= SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload
);
443 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
,
444 "PA 0x%llx ns=%d ch=%d pat=%x",
445 payload
, ns
, ch
, pat
);
456 static int arm_spe_pkt_desc_counter(const struct arm_spe_pkt
*packet
,
457 char *buf
, size_t buf_len
)
459 u64 payload
= packet
->payload
;
460 const char *name
= arm_spe_pkt_name(packet
->type
);
463 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "%s %d ", name
,
464 (unsigned short)payload
);
466 switch (packet
->index
) {
467 case SPE_CNT_PKT_HDR_INDEX_TOTAL_LAT
:
468 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "TOT");
470 case SPE_CNT_PKT_HDR_INDEX_ISSUE_LAT
:
471 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "ISSUE");
473 case SPE_CNT_PKT_HDR_INDEX_TRANS_LAT
:
474 arm_spe_pkt_out_string(&err
, &buf
, &buf_len
, "XLAT");
483 int arm_spe_pkt_desc(const struct arm_spe_pkt
*packet
, char *buf
,
486 int idx
= packet
->index
;
487 unsigned long long payload
= packet
->payload
;
488 const char *name
= arm_spe_pkt_name(packet
->type
);
489 char *buf_orig
= buf
;
490 size_t blen
= buf_len
;
493 switch (packet
->type
) {
497 arm_spe_pkt_out_string(&err
, &buf
, &blen
, "%s", name
);
500 err
= arm_spe_pkt_desc_event(packet
, buf
, buf_len
);
502 case ARM_SPE_OP_TYPE
:
503 err
= arm_spe_pkt_desc_op_type(packet
, buf
, buf_len
);
505 case ARM_SPE_DATA_SOURCE
:
506 case ARM_SPE_TIMESTAMP
:
507 arm_spe_pkt_out_string(&err
, &buf
, &blen
, "%s %lld", name
, payload
);
509 case ARM_SPE_ADDRESS
:
510 err
= arm_spe_pkt_desc_addr(packet
, buf
, buf_len
);
512 case ARM_SPE_CONTEXT
:
513 arm_spe_pkt_out_string(&err
, &buf
, &blen
, "%s 0x%lx el%d",
514 name
, (unsigned long)payload
, idx
+ 1);
516 case ARM_SPE_COUNTER
:
517 err
= arm_spe_pkt_desc_counter(packet
, buf
, buf_len
);
520 /* Unknown packet type */
525 /* Output raw data if detect any error */
528 arm_spe_pkt_out_string(&err
, &buf_orig
, &buf_len
, "%s 0x%llx (%d)",
529 name
, payload
, packet
->index
);