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);
48 /* Fill highest byte if bits [48..55] is 0xff */
49 if (SPE_ADDR_PKT_ADDR_GET_BYTE_6(payload) == 0xffULL)
50 payload |= 0xffULL << SPE_ADDR_PKT_ADDR_BYTE7_SHIFT;
52 /* Data access physical address */
53 } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS) {
54 /* Clean highest byte */
55 payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
57 pr_err("unsupported address packet index: 0x%x\n", index);
63 struct arm_spe_decoder *arm_spe_decoder_new(struct arm_spe_params *params)
65 struct arm_spe_decoder *decoder;
67 if (!params->get_trace)
70 decoder = zalloc(sizeof(struct arm_spe_decoder));
74 decoder->get_trace = params->get_trace;
75 decoder->data = params->data;
80 void arm_spe_decoder_free(struct arm_spe_decoder *decoder)
85 static int arm_spe_get_data(struct arm_spe_decoder *decoder)
87 struct arm_spe_buffer buffer = { .buf = 0, };
90 pr_debug("Getting more data\n");
91 ret = decoder->get_trace(&buffer, decoder->data);
95 decoder->buf = buffer.buf;
96 decoder->len = buffer.len;
99 pr_debug("No more data\n");
104 static int arm_spe_get_next_packet(struct arm_spe_decoder *decoder)
110 ret = arm_spe_get_data(decoder);
112 /* Failed to read out trace data */
117 ret = arm_spe_get_packet(decoder->buf, decoder->len,
120 /* Move forward for 1 byte */
128 } while (decoder->packet.type == ARM_SPE_PAD);
133 static int arm_spe_read_record(struct arm_spe_decoder *decoder)
139 memset(&decoder->record, 0x0, sizeof(decoder->record));
142 err = arm_spe_get_next_packet(decoder);
146 idx = decoder->packet.index;
147 payload = decoder->packet.payload;
149 switch (decoder->packet.type) {
150 case ARM_SPE_TIMESTAMP:
151 decoder->record.timestamp = payload;
155 case ARM_SPE_ADDRESS:
156 ip = arm_spe_calc_ip(idx, payload);
157 if (idx == SPE_ADDR_PKT_HDR_INDEX_INS)
158 decoder->record.from_ip = ip;
159 else if (idx == SPE_ADDR_PKT_HDR_INDEX_BRANCH)
160 decoder->record.to_ip = ip;
162 case ARM_SPE_COUNTER:
164 case ARM_SPE_CONTEXT:
166 case ARM_SPE_OP_TYPE:
169 if (payload & BIT(EV_L1D_REFILL))
170 decoder->record.type |= ARM_SPE_L1D_MISS;
172 if (payload & BIT(EV_L1D_ACCESS))
173 decoder->record.type |= ARM_SPE_L1D_ACCESS;
175 if (payload & BIT(EV_TLB_WALK))
176 decoder->record.type |= ARM_SPE_TLB_MISS;
178 if (payload & BIT(EV_TLB_ACCESS))
179 decoder->record.type |= ARM_SPE_TLB_ACCESS;
181 if ((idx == 2 || idx == 4 || idx == 8) &&
182 (payload & BIT(EV_LLC_MISS)))
183 decoder->record.type |= ARM_SPE_LLC_MISS;
185 if ((idx == 2 || idx == 4 || idx == 8) &&
186 (payload & BIT(EV_LLC_ACCESS)))
187 decoder->record.type |= ARM_SPE_LLC_ACCESS;
189 if ((idx == 2 || idx == 4 || idx == 8) &&
190 (payload & BIT(EV_REMOTE_ACCESS)))
191 decoder->record.type |= ARM_SPE_REMOTE_ACCESS;
193 if (payload & BIT(EV_MISPRED))
194 decoder->record.type |= ARM_SPE_BRANCH_MISS;
197 case ARM_SPE_DATA_SOURCE:
204 pr_err("Get packet error!\n");
212 int arm_spe_decode(struct arm_spe_decoder *decoder)
214 return arm_spe_read_record(decoder);