1 // SPDX-License-Identifier: GPL-2.0
3 * arm_spe_decoder.c: ARM SPE support
15 #include <linux/bitops.h>
16 #include <linux/compiler.h>
17 #include <linux/zalloc.h>
19 #include "../auxtrace.h"
23 #include "arm-spe-decoder.h"
25 static u64
arm_spe_calc_ip(int index
, u64 payload
)
29 /* Instruction virtual address or Branch target address */
30 if (index
== SPE_ADDR_PKT_HDR_INDEX_INS
||
31 index
== SPE_ADDR_PKT_HDR_INDEX_BRANCH
) {
32 ns
= SPE_ADDR_PKT_GET_NS(payload
);
33 el
= SPE_ADDR_PKT_GET_EL(payload
);
35 /* Clean highest byte */
36 payload
= SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload
);
38 /* Fill highest byte for EL1 or EL2 (VHE) mode */
39 if (ns
&& (el
== SPE_ADDR_PKT_EL1
|| el
== SPE_ADDR_PKT_EL2
))
40 payload
|= 0xffULL
<< SPE_ADDR_PKT_ADDR_BYTE7_SHIFT
;
42 /* Data access virtual address */
43 } else if (index
== SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT
) {
46 payload
= SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload
);
49 * Armv8 ARM (ARM DDI 0487F.c), chapter "D10.2.1 Address packet"
50 * defines the data virtual address payload format, the top byte
51 * (bits [63:56]) is assigned as top-byte tag; so we only can
52 * retrieve address value from bits [55:0].
54 * According to Documentation/arm64/memory.rst, if detects the
55 * specific pattern in bits [55:52] of payload which falls in
56 * the kernel space, should fixup the top byte and this allows
57 * perf tool to parse DSO symbol for data address correctly.
59 * For this reason, if detects the bits [55:52] is 0xf, will
60 * fill 0xff into the top byte.
62 val
= SPE_ADDR_PKT_ADDR_GET_BYTE_6(payload
);
63 if ((val
& 0xf0ULL
) == 0xf0ULL
)
64 payload
|= 0xffULL
<< SPE_ADDR_PKT_ADDR_BYTE7_SHIFT
;
66 /* Data access physical address */
67 } else if (index
== SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS
) {
68 /* Clean highest byte */
69 payload
= SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload
);
71 pr_err("unsupported address packet index: 0x%x\n", index
);
77 struct arm_spe_decoder
*arm_spe_decoder_new(struct arm_spe_params
*params
)
79 struct arm_spe_decoder
*decoder
;
81 if (!params
->get_trace
)
84 decoder
= zalloc(sizeof(struct arm_spe_decoder
));
88 decoder
->get_trace
= params
->get_trace
;
89 decoder
->data
= params
->data
;
94 void arm_spe_decoder_free(struct arm_spe_decoder
*decoder
)
99 static int arm_spe_get_data(struct arm_spe_decoder
*decoder
)
101 struct arm_spe_buffer buffer
= { .buf
= 0, };
104 pr_debug("Getting more data\n");
105 ret
= decoder
->get_trace(&buffer
, decoder
->data
);
109 decoder
->buf
= buffer
.buf
;
110 decoder
->len
= buffer
.len
;
113 pr_debug("No more data\n");
118 static int arm_spe_get_next_packet(struct arm_spe_decoder
*decoder
)
124 ret
= arm_spe_get_data(decoder
);
126 /* Failed to read out trace data */
131 ret
= arm_spe_get_packet(decoder
->buf
, decoder
->len
,
134 /* Move forward for 1 byte */
142 } while (decoder
->packet
.type
== ARM_SPE_PAD
);
147 static int arm_spe_read_record(struct arm_spe_decoder
*decoder
)
153 memset(&decoder
->record
, 0x0, sizeof(decoder
->record
));
156 err
= arm_spe_get_next_packet(decoder
);
160 idx
= decoder
->packet
.index
;
161 payload
= decoder
->packet
.payload
;
163 switch (decoder
->packet
.type
) {
164 case ARM_SPE_TIMESTAMP
:
165 decoder
->record
.timestamp
= payload
;
169 case ARM_SPE_ADDRESS
:
170 ip
= arm_spe_calc_ip(idx
, payload
);
171 if (idx
== SPE_ADDR_PKT_HDR_INDEX_INS
)
172 decoder
->record
.from_ip
= ip
;
173 else if (idx
== SPE_ADDR_PKT_HDR_INDEX_BRANCH
)
174 decoder
->record
.to_ip
= ip
;
176 case ARM_SPE_COUNTER
:
178 case ARM_SPE_CONTEXT
:
180 case ARM_SPE_OP_TYPE
:
183 if (payload
& BIT(EV_L1D_REFILL
))
184 decoder
->record
.type
|= ARM_SPE_L1D_MISS
;
186 if (payload
& BIT(EV_L1D_ACCESS
))
187 decoder
->record
.type
|= ARM_SPE_L1D_ACCESS
;
189 if (payload
& BIT(EV_TLB_WALK
))
190 decoder
->record
.type
|= ARM_SPE_TLB_MISS
;
192 if (payload
& BIT(EV_TLB_ACCESS
))
193 decoder
->record
.type
|= ARM_SPE_TLB_ACCESS
;
195 if (payload
& BIT(EV_LLC_MISS
))
196 decoder
->record
.type
|= ARM_SPE_LLC_MISS
;
198 if (payload
& BIT(EV_LLC_ACCESS
))
199 decoder
->record
.type
|= ARM_SPE_LLC_ACCESS
;
201 if (payload
& BIT(EV_REMOTE_ACCESS
))
202 decoder
->record
.type
|= ARM_SPE_REMOTE_ACCESS
;
204 if (payload
& BIT(EV_MISPRED
))
205 decoder
->record
.type
|= ARM_SPE_BRANCH_MISS
;
208 case ARM_SPE_DATA_SOURCE
:
215 pr_err("Get packet error!\n");
223 int arm_spe_decode(struct arm_spe_decoder
*decoder
)
225 return arm_spe_read_record(decoder
);