1 // SPDX-License-Identifier: GPL-2.0
3 * arm_spe_decoder.c: ARM SPE support
15 #include <linux/compiler.h>
16 #include <linux/zalloc.h>
18 #include "../auxtrace.h"
22 #include "arm-spe-decoder.h"
25 #define BIT(n) (1UL << (n))
28 static u64 arm_spe_calc_ip(int index, u64 payload)
30 u8 *addr = (u8 *)&payload;
33 /* Instruction virtual address or Branch target address */
34 if (index == SPE_ADDR_PKT_HDR_INDEX_INS ||
35 index == SPE_ADDR_PKT_HDR_INDEX_BRANCH) {
36 ns = addr[7] & SPE_ADDR_PKT_NS;
37 el = (addr[7] & SPE_ADDR_PKT_EL_MASK) >> SPE_ADDR_PKT_EL_OFFSET;
39 /* Fill highest byte for EL1 or EL2 (VHE) mode */
40 if (ns && (el == SPE_ADDR_PKT_EL1 || el == SPE_ADDR_PKT_EL2))
42 /* Clean highest byte for other cases */
46 /* Data access virtual address */
47 } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT) {
49 /* Fill highest byte if bits [48..55] is 0xff */
52 /* Otherwise, cleanup tags */
56 /* Data access physical address */
57 } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS) {
61 pr_err("unsupported address packet index: 0x%x\n", index);
67 struct arm_spe_decoder *arm_spe_decoder_new(struct arm_spe_params *params)
69 struct arm_spe_decoder *decoder;
71 if (!params->get_trace)
74 decoder = zalloc(sizeof(struct arm_spe_decoder));
78 decoder->get_trace = params->get_trace;
79 decoder->data = params->data;
84 void arm_spe_decoder_free(struct arm_spe_decoder *decoder)
89 static int arm_spe_get_data(struct arm_spe_decoder *decoder)
91 struct arm_spe_buffer buffer = { .buf = 0, };
94 pr_debug("Getting more data\n");
95 ret = decoder->get_trace(&buffer, decoder->data);
99 decoder->buf = buffer.buf;
100 decoder->len = buffer.len;
103 pr_debug("No more data\n");
108 static int arm_spe_get_next_packet(struct arm_spe_decoder *decoder)
114 ret = arm_spe_get_data(decoder);
116 /* Failed to read out trace data */
121 ret = arm_spe_get_packet(decoder->buf, decoder->len,
124 /* Move forward for 1 byte */
132 } while (decoder->packet.type == ARM_SPE_PAD);
137 static int arm_spe_read_record(struct arm_spe_decoder *decoder)
143 memset(&decoder->record, 0x0, sizeof(decoder->record));
146 err = arm_spe_get_next_packet(decoder);
150 idx = decoder->packet.index;
151 payload = decoder->packet.payload;
153 switch (decoder->packet.type) {
154 case ARM_SPE_TIMESTAMP:
155 decoder->record.timestamp = payload;
159 case ARM_SPE_ADDRESS:
160 ip = arm_spe_calc_ip(idx, payload);
161 if (idx == SPE_ADDR_PKT_HDR_INDEX_INS)
162 decoder->record.from_ip = ip;
163 else if (idx == SPE_ADDR_PKT_HDR_INDEX_BRANCH)
164 decoder->record.to_ip = ip;
166 case ARM_SPE_COUNTER:
168 case ARM_SPE_CONTEXT:
170 case ARM_SPE_OP_TYPE:
173 if (payload & BIT(EV_L1D_REFILL))
174 decoder->record.type |= ARM_SPE_L1D_MISS;
176 if (payload & BIT(EV_L1D_ACCESS))
177 decoder->record.type |= ARM_SPE_L1D_ACCESS;
179 if (payload & BIT(EV_TLB_WALK))
180 decoder->record.type |= ARM_SPE_TLB_MISS;
182 if (payload & BIT(EV_TLB_ACCESS))
183 decoder->record.type |= ARM_SPE_TLB_ACCESS;
185 if ((idx == 2 || idx == 4 || idx == 8) &&
186 (payload & BIT(EV_LLC_MISS)))
187 decoder->record.type |= ARM_SPE_LLC_MISS;
189 if ((idx == 2 || idx == 4 || idx == 8) &&
190 (payload & BIT(EV_LLC_ACCESS)))
191 decoder->record.type |= ARM_SPE_LLC_ACCESS;
193 if ((idx == 2 || idx == 4 || idx == 8) &&
194 (payload & BIT(EV_REMOTE_ACCESS)))
195 decoder->record.type |= ARM_SPE_REMOTE_ACCESS;
197 if (payload & BIT(EV_MISPRED))
198 decoder->record.type |= ARM_SPE_BRANCH_MISS;
201 case ARM_SPE_DATA_SOURCE:
208 pr_err("Get packet error!\n");
216 int arm_spe_decode(struct arm_spe_decoder *decoder)
218 return arm_spe_read_record(decoder);