cc18a1e8c212b94323feeb28c3f90ace6b8ab0dd
[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         u8 *addr = (u8 *)&payload;
28         int ns, el;
29
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;
35
36                 /* Fill highest byte for EL1 or EL2 (VHE) mode */
37                 if (ns && (el == SPE_ADDR_PKT_EL1 || el == SPE_ADDR_PKT_EL2))
38                         addr[7] = 0xff;
39                 /* Clean highest byte for other cases */
40                 else
41                         addr[7] = 0x0;
42
43         /* Data access virtual address */
44         } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_VIRT) {
45
46                 /* Fill highest byte if bits [48..55] is 0xff */
47                 if (addr[6] == 0xff)
48                         addr[7] = 0xff;
49                 /* Otherwise, cleanup tags */
50                 else
51                         addr[7] = 0x0;
52
53         /* Data access physical address */
54         } else if (index == SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS) {
55                 /* Cleanup byte 7 */
56                 addr[7] = 0x0;
57         } else {
58                 pr_err("unsupported address packet index: 0x%x\n", index);
59         }
60
61         return payload;
62 }
63
64 struct arm_spe_decoder *arm_spe_decoder_new(struct arm_spe_params *params)
65 {
66         struct arm_spe_decoder *decoder;
67
68         if (!params->get_trace)
69                 return NULL;
70
71         decoder = zalloc(sizeof(struct arm_spe_decoder));
72         if (!decoder)
73                 return NULL;
74
75         decoder->get_trace = params->get_trace;
76         decoder->data = params->data;
77
78         return decoder;
79 }
80
81 void arm_spe_decoder_free(struct arm_spe_decoder *decoder)
82 {
83         free(decoder);
84 }
85
86 static int arm_spe_get_data(struct arm_spe_decoder *decoder)
87 {
88         struct arm_spe_buffer buffer = { .buf = 0, };
89         int ret;
90
91         pr_debug("Getting more data\n");
92         ret = decoder->get_trace(&buffer, decoder->data);
93         if (ret < 0)
94                 return ret;
95
96         decoder->buf = buffer.buf;
97         decoder->len = buffer.len;
98
99         if (!decoder->len)
100                 pr_debug("No more data\n");
101
102         return decoder->len;
103 }
104
105 static int arm_spe_get_next_packet(struct arm_spe_decoder *decoder)
106 {
107         int ret;
108
109         do {
110                 if (!decoder->len) {
111                         ret = arm_spe_get_data(decoder);
112
113                         /* Failed to read out trace data */
114                         if (ret <= 0)
115                                 return ret;
116                 }
117
118                 ret = arm_spe_get_packet(decoder->buf, decoder->len,
119                                          &decoder->packet);
120                 if (ret <= 0) {
121                         /* Move forward for 1 byte */
122                         decoder->buf += 1;
123                         decoder->len -= 1;
124                         return -EBADMSG;
125                 }
126
127                 decoder->buf += ret;
128                 decoder->len -= ret;
129         } while (decoder->packet.type == ARM_SPE_PAD);
130
131         return 1;
132 }
133
134 static int arm_spe_read_record(struct arm_spe_decoder *decoder)
135 {
136         int err;
137         int idx;
138         u64 payload, ip;
139
140         memset(&decoder->record, 0x0, sizeof(decoder->record));
141
142         while (1) {
143                 err = arm_spe_get_next_packet(decoder);
144                 if (err <= 0)
145                         return err;
146
147                 idx = decoder->packet.index;
148                 payload = decoder->packet.payload;
149
150                 switch (decoder->packet.type) {
151                 case ARM_SPE_TIMESTAMP:
152                         decoder->record.timestamp = payload;
153                         return 1;
154                 case ARM_SPE_END:
155                         return 1;
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;
162                         break;
163                 case ARM_SPE_COUNTER:
164                         break;
165                 case ARM_SPE_CONTEXT:
166                         break;
167                 case ARM_SPE_OP_TYPE:
168                         break;
169                 case ARM_SPE_EVENTS:
170                         if (payload & BIT(EV_L1D_REFILL))
171                                 decoder->record.type |= ARM_SPE_L1D_MISS;
172
173                         if (payload & BIT(EV_L1D_ACCESS))
174                                 decoder->record.type |= ARM_SPE_L1D_ACCESS;
175
176                         if (payload & BIT(EV_TLB_WALK))
177                                 decoder->record.type |= ARM_SPE_TLB_MISS;
178
179                         if (payload & BIT(EV_TLB_ACCESS))
180                                 decoder->record.type |= ARM_SPE_TLB_ACCESS;
181
182                         if ((idx == 2 || idx == 4 || idx == 8) &&
183                             (payload & BIT(EV_LLC_MISS)))
184                                 decoder->record.type |= ARM_SPE_LLC_MISS;
185
186                         if ((idx == 2 || idx == 4 || idx == 8) &&
187                             (payload & BIT(EV_LLC_ACCESS)))
188                                 decoder->record.type |= ARM_SPE_LLC_ACCESS;
189
190                         if ((idx == 2 || idx == 4 || idx == 8) &&
191                             (payload & BIT(EV_REMOTE_ACCESS)))
192                                 decoder->record.type |= ARM_SPE_REMOTE_ACCESS;
193
194                         if (payload & BIT(EV_MISPRED))
195                                 decoder->record.type |= ARM_SPE_BRANCH_MISS;
196
197                         break;
198                 case ARM_SPE_DATA_SOURCE:
199                         break;
200                 case ARM_SPE_BAD:
201                         break;
202                 case ARM_SPE_PAD:
203                         break;
204                 default:
205                         pr_err("Get packet error!\n");
206                         return -1;
207                 }
208         }
209
210         return 0;
211 }
212
213 int arm_spe_decode(struct arm_spe_decoder *decoder)
214 {
215         return arm_spe_read_record(decoder);
216 }