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)
27 u8 *addr = (u8 *)&payload;
30 /* Instruction virtual address or Branch target address */
31 if (index == SPE_ADDR_PKT_HDR_INDEX_INS ||
32 index == SPE_ADDR_PKT_HDR_INDEX_BRANCH) {
33 ns = addr[7] & SPE_ADDR_PKT_NS;
34 el = (addr[7] & SPE_ADDR_PKT_EL_MASK) >> SPE_ADDR_PKT_EL_OFFSET;
36 /* Fill highest byte for EL1 or EL2 (VHE) mode */
37 if (ns && (el == SPE_ADDR_PKT_EL1 || el == SPE_ADDR_PKT_EL2))
39 /* Clean highest byte for other cases */
43 /* Data access virtual address */
44 } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT) {
46 /* Fill highest byte if bits [48..55] is 0xff */
49 /* Otherwise, cleanup tags */
53 /* Data access physical address */
54 } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS) {
58 pr_err("unsupported address packet index: 0x%x\n", index);
64 struct arm_spe_decoder *arm_spe_decoder_new(struct arm_spe_params *params)
66 struct arm_spe_decoder *decoder;
68 if (!params->get_trace)
71 decoder = zalloc(sizeof(struct arm_spe_decoder));
75 decoder->get_trace = params->get_trace;
76 decoder->data = params->data;
81 void arm_spe_decoder_free(struct arm_spe_decoder *decoder)
86 static int arm_spe_get_data(struct arm_spe_decoder *decoder)
88 struct arm_spe_buffer buffer = { .buf = 0, };
91 pr_debug("Getting more data\n");
92 ret = decoder->get_trace(&buffer, decoder->data);
96 decoder->buf = buffer.buf;
97 decoder->len = buffer.len;
100 pr_debug("No more data\n");
105 static int arm_spe_get_next_packet(struct arm_spe_decoder *decoder)
111 ret = arm_spe_get_data(decoder);
113 /* Failed to read out trace data */
118 ret = arm_spe_get_packet(decoder->buf, decoder->len,
121 /* Move forward for 1 byte */
129 } while (decoder->packet.type == ARM_SPE_PAD);
134 static int arm_spe_read_record(struct arm_spe_decoder *decoder)
140 memset(&decoder->record, 0x0, sizeof(decoder->record));
143 err = arm_spe_get_next_packet(decoder);
147 idx = decoder->packet.index;
148 payload = decoder->packet.payload;
150 switch (decoder->packet.type) {
151 case ARM_SPE_TIMESTAMP:
152 decoder->record.timestamp = payload;
156 case ARM_SPE_ADDRESS:
157 ip = arm_spe_calc_ip(idx, payload);
158 if (idx == SPE_ADDR_PKT_HDR_INDEX_INS)
159 decoder->record.from_ip = ip;
160 else if (idx == SPE_ADDR_PKT_HDR_INDEX_BRANCH)
161 decoder->record.to_ip = ip;
163 case ARM_SPE_COUNTER:
165 case ARM_SPE_CONTEXT:
167 case ARM_SPE_OP_TYPE:
170 if (payload & BIT(EV_L1D_REFILL))
171 decoder->record.type |= ARM_SPE_L1D_MISS;
173 if (payload & BIT(EV_L1D_ACCESS))
174 decoder->record.type |= ARM_SPE_L1D_ACCESS;
176 if (payload & BIT(EV_TLB_WALK))
177 decoder->record.type |= ARM_SPE_TLB_MISS;
179 if (payload & BIT(EV_TLB_ACCESS))
180 decoder->record.type |= ARM_SPE_TLB_ACCESS;
182 if ((idx == 2 || idx == 4 || idx == 8) &&
183 (payload & BIT(EV_LLC_MISS)))
184 decoder->record.type |= ARM_SPE_LLC_MISS;
186 if ((idx == 2 || idx == 4 || idx == 8) &&
187 (payload & BIT(EV_LLC_ACCESS)))
188 decoder->record.type |= ARM_SPE_LLC_ACCESS;
190 if ((idx == 2 || idx == 4 || idx == 8) &&
191 (payload & BIT(EV_REMOTE_ACCESS)))
192 decoder->record.type |= ARM_SPE_REMOTE_ACCESS;
194 if (payload & BIT(EV_MISPRED))
195 decoder->record.type |= ARM_SPE_BRANCH_MISS;
198 case ARM_SPE_DATA_SOURCE:
205 pr_err("Get packet error!\n");
213 int arm_spe_decode(struct arm_spe_decoder *decoder)
215 return arm_spe_read_record(decoder);