perf arm-spe: Refactor address packet handling
[linux-2.6-microblaze.git] / tools / perf / util / arm-spe-decoder / arm-spe-decoder.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arm_spe_decoder.c: ARM SPE support
4  */
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <stdbool.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <linux/bitops.h>
16 #include <linux/compiler.h>
17 #include <linux/zalloc.h>
18
19 #include "../auxtrace.h"
20 #include "../debug.h"
21 #include "../util.h"
22
23 #include "arm-spe-decoder.h"
24
25 static u64 arm_spe_calc_ip(int index, u64 payload)
26 {
27         u64 ns, el;
28
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);
34
35                 /* Clean highest byte */
36                 payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
37
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;
41
42         /* Data access virtual address */
43         } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT) {
44
45                 /* Clean tags */
46                 payload = SPE_ADDR_PKT_ADDR_GET_BYTES_0_6(payload);
47
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;
51
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);
56         } else {
57                 pr_err("unsupported address packet index: 0x%x\n", index);
58         }
59
60         return payload;
61 }
62
63 struct arm_spe_decoder *arm_spe_decoder_new(struct arm_spe_params *params)
64 {
65         struct arm_spe_decoder *decoder;
66
67         if (!params->get_trace)
68                 return NULL;
69
70         decoder = zalloc(sizeof(struct arm_spe_decoder));
71         if (!decoder)
72                 return NULL;
73
74         decoder->get_trace = params->get_trace;
75         decoder->data = params->data;
76
77         return decoder;
78 }
79
80 void arm_spe_decoder_free(struct arm_spe_decoder *decoder)
81 {
82         free(decoder);
83 }
84
85 static int arm_spe_get_data(struct arm_spe_decoder *decoder)
86 {
87         struct arm_spe_buffer buffer = { .buf = 0, };
88         int ret;
89
90         pr_debug("Getting more data\n");
91         ret = decoder->get_trace(&buffer, decoder->data);
92         if (ret < 0)
93                 return ret;
94
95         decoder->buf = buffer.buf;
96         decoder->len = buffer.len;
97
98         if (!decoder->len)
99                 pr_debug("No more data\n");
100
101         return decoder->len;
102 }
103
104 static int arm_spe_get_next_packet(struct arm_spe_decoder *decoder)
105 {
106         int ret;
107
108         do {
109                 if (!decoder->len) {
110                         ret = arm_spe_get_data(decoder);
111
112                         /* Failed to read out trace data */
113                         if (ret <= 0)
114                                 return ret;
115                 }
116
117                 ret = arm_spe_get_packet(decoder->buf, decoder->len,
118                                          &decoder->packet);
119                 if (ret <= 0) {
120                         /* Move forward for 1 byte */
121                         decoder->buf += 1;
122                         decoder->len -= 1;
123                         return -EBADMSG;
124                 }
125
126                 decoder->buf += ret;
127                 decoder->len -= ret;
128         } while (decoder->packet.type == ARM_SPE_PAD);
129
130         return 1;
131 }
132
133 static int arm_spe_read_record(struct arm_spe_decoder *decoder)
134 {
135         int err;
136         int idx;
137         u64 payload, ip;
138
139         memset(&decoder->record, 0x0, sizeof(decoder->record));
140
141         while (1) {
142                 err = arm_spe_get_next_packet(decoder);
143                 if (err <= 0)
144                         return err;
145
146                 idx = decoder->packet.index;
147                 payload = decoder->packet.payload;
148
149                 switch (decoder->packet.type) {
150                 case ARM_SPE_TIMESTAMP:
151                         decoder->record.timestamp = payload;
152                         return 1;
153                 case ARM_SPE_END:
154                         return 1;
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;
161                         break;
162                 case ARM_SPE_COUNTER:
163                         break;
164                 case ARM_SPE_CONTEXT:
165                         break;
166                 case ARM_SPE_OP_TYPE:
167                         break;
168                 case ARM_SPE_EVENTS:
169                         if (payload & BIT(EV_L1D_REFILL))
170                                 decoder->record.type |= ARM_SPE_L1D_MISS;
171
172                         if (payload & BIT(EV_L1D_ACCESS))
173                                 decoder->record.type |= ARM_SPE_L1D_ACCESS;
174
175                         if (payload & BIT(EV_TLB_WALK))
176                                 decoder->record.type |= ARM_SPE_TLB_MISS;
177
178                         if (payload & BIT(EV_TLB_ACCESS))
179                                 decoder->record.type |= ARM_SPE_TLB_ACCESS;
180
181                         if ((idx == 2 || idx == 4 || idx == 8) &&
182                             (payload & BIT(EV_LLC_MISS)))
183                                 decoder->record.type |= ARM_SPE_LLC_MISS;
184
185                         if ((idx == 2 || idx == 4 || idx == 8) &&
186                             (payload & BIT(EV_LLC_ACCESS)))
187                                 decoder->record.type |= ARM_SPE_LLC_ACCESS;
188
189                         if ((idx == 2 || idx == 4 || idx == 8) &&
190                             (payload & BIT(EV_REMOTE_ACCESS)))
191                                 decoder->record.type |= ARM_SPE_REMOTE_ACCESS;
192
193                         if (payload & BIT(EV_MISPRED))
194                                 decoder->record.type |= ARM_SPE_BRANCH_MISS;
195
196                         break;
197                 case ARM_SPE_DATA_SOURCE:
198                         break;
199                 case ARM_SPE_BAD:
200                         break;
201                 case ARM_SPE_PAD:
202                         break;
203                 default:
204                         pr_err("Get packet error!\n");
205                         return -1;
206                 }
207         }
208
209         return 0;
210 }
211
212 int arm_spe_decode(struct arm_spe_decoder *decoder)
213 {
214         return arm_spe_read_record(decoder);
215 }