blob: c8b98fa2299736a2f76decf2d3f5458d41974131 [file] [log] [blame]
Mathieu Poirier8a9fd832018-04-18 16:05:18 -06001// SPDX-License-Identifier: GPL-2.0
Mathieu Poirier68ffe392018-01-17 10:52:13 -07002/*
Mathieu Poirier68ffe392018-01-17 10:52:13 -07003 * Copyright(C) 2015-2018 Linaro Limited.
4 *
5 * Author: Tor Jeremiassen <tor@ti.com>
6 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
7 */
8
9#include <linux/err.h>
10#include <linux/list.h>
11#include <stdlib.h>
12#include <opencsd/c_api/opencsd_c_api.h>
13#include <opencsd/etmv4/trc_pkt_types_etmv4.h>
14#include <opencsd/ocsd_if_types.h>
15
16#include "cs-etm.h"
17#include "cs-etm-decoder.h"
18#include "intlist.h"
19#include "util.h"
20
21#define MAX_BUFFER 1024
22
23/* use raw logging */
24#ifdef CS_DEBUG_RAW
25#define CS_LOG_RAW_FRAMES
26#ifdef CS_RAW_PACKED
27#define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT | \
28 OCSD_DFRMTR_PACKED_RAW_OUT)
29#else
30#define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT)
31#endif
32#endif
33
34struct cs_etm_decoder {
35 void *data;
36 void (*packet_printer)(const char *msg);
37 bool trace_on;
38 dcd_tree_handle_t dcd_tree;
39 cs_etm_mem_cb_type mem_access;
40 ocsd_datapath_resp_t prev_return;
41 u32 packet_count;
42 u32 head;
43 u32 tail;
44 struct cs_etm_packet packet_buffer[MAX_BUFFER];
45};
46
Mathieu Poirier290598b2018-01-17 10:52:15 -070047static u32
48cs_etm_decoder__mem_access(const void *context,
49 const ocsd_vaddr_t address,
50 const ocsd_mem_space_acc_t mem_space __maybe_unused,
51 const u32 req_size,
52 u8 *buffer)
53{
54 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
55
56 return decoder->mem_access(decoder->data,
57 address,
58 req_size,
59 buffer);
60}
61
62int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder,
63 u64 start, u64 end,
64 cs_etm_mem_cb_type cb_func)
65{
66 decoder->mem_access = cb_func;
67
68 if (ocsd_dt_add_callback_mem_acc(decoder->dcd_tree, start, end,
69 OCSD_MEM_SPACE_ANY,
70 cs_etm_decoder__mem_access, decoder))
71 return -1;
72
73 return 0;
74}
75
76int cs_etm_decoder__reset(struct cs_etm_decoder *decoder)
77{
78 ocsd_datapath_resp_t dp_ret;
79
Robert Walkere573e972018-02-14 11:24:39 +000080 decoder->prev_return = OCSD_RESP_CONT;
81
Mathieu Poirier290598b2018-01-17 10:52:15 -070082 dp_ret = ocsd_dt_process_data(decoder->dcd_tree, OCSD_OP_RESET,
83 0, 0, NULL, NULL);
84 if (OCSD_DATA_RESP_IS_FATAL(dp_ret))
85 return -1;
86
87 return 0;
88}
89
90int cs_etm_decoder__get_packet(struct cs_etm_decoder *decoder,
91 struct cs_etm_packet *packet)
92{
93 if (!decoder || !packet)
94 return -EINVAL;
95
96 /* Nothing to do, might as well just return */
97 if (decoder->packet_count == 0)
98 return 0;
99
100 *packet = decoder->packet_buffer[decoder->head];
101
102 decoder->head = (decoder->head + 1) & (MAX_BUFFER - 1);
103
104 decoder->packet_count--;
105
106 return 1;
107}
108
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700109static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params,
110 ocsd_etmv4_cfg *config)
111{
112 config->reg_configr = params->etmv4.reg_configr;
113 config->reg_traceidr = params->etmv4.reg_traceidr;
114 config->reg_idr0 = params->etmv4.reg_idr0;
115 config->reg_idr1 = params->etmv4.reg_idr1;
116 config->reg_idr2 = params->etmv4.reg_idr2;
117 config->reg_idr8 = params->etmv4.reg_idr8;
118 config->reg_idr9 = 0;
119 config->reg_idr10 = 0;
120 config->reg_idr11 = 0;
121 config->reg_idr12 = 0;
122 config->reg_idr13 = 0;
123 config->arch_ver = ARCH_V8;
124 config->core_prof = profile_CortexA;
125}
126
127static void cs_etm_decoder__print_str_cb(const void *p_context,
128 const char *msg,
129 const int str_len)
130{
131 if (p_context && str_len)
132 ((struct cs_etm_decoder *)p_context)->packet_printer(msg);
133}
134
135static int
136cs_etm_decoder__init_def_logger_printing(struct cs_etm_decoder_params *d_params,
137 struct cs_etm_decoder *decoder)
138{
139 int ret = 0;
140
141 if (d_params->packet_printer == NULL)
142 return -1;
143
144 decoder->packet_printer = d_params->packet_printer;
145
146 /*
147 * Set up a library default logger to process any printers
148 * (packet/raw frame) we add later.
149 */
150 ret = ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
151 if (ret != 0)
152 return -1;
153
154 /* no stdout / err / file output */
155 ret = ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
156 if (ret != 0)
157 return -1;
158
159 /*
160 * Set the string CB for the default logger, passes strings to
161 * perf print logger.
162 */
163 ret = ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
164 (void *)decoder,
165 cs_etm_decoder__print_str_cb);
166 if (ret != 0)
167 ret = -1;
168
169 return 0;
170}
171
172#ifdef CS_LOG_RAW_FRAMES
173static void
174cs_etm_decoder__init_raw_frame_logging(struct cs_etm_decoder_params *d_params,
175 struct cs_etm_decoder *decoder)
176{
177 /* Only log these during a --dump operation */
178 if (d_params->operation == CS_ETM_OPERATION_PRINT) {
179 /* set up a library default logger to process the
180 * raw frame printer we add later
181 */
182 ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
183
184 /* no stdout / err / file output */
185 ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
186
187 /* set the string CB for the default logger,
188 * passes strings to perf print logger.
189 */
190 ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
191 (void *)decoder,
192 cs_etm_decoder__print_str_cb);
193
194 /* use the built in library printer for the raw frames */
195 ocsd_dt_set_raw_frame_printer(decoder->dcd_tree,
196 CS_RAW_DEBUG_FLAGS);
197 }
198}
199#else
200static void
201cs_etm_decoder__init_raw_frame_logging(
202 struct cs_etm_decoder_params *d_params __maybe_unused,
203 struct cs_etm_decoder *decoder __maybe_unused)
204{
205}
206#endif
207
208static int cs_etm_decoder__create_packet_printer(struct cs_etm_decoder *decoder,
209 const char *decoder_name,
210 void *trace_config)
211{
212 u8 csid;
213
214 if (ocsd_dt_create_decoder(decoder->dcd_tree, decoder_name,
215 OCSD_CREATE_FLG_PACKET_PROC,
216 trace_config, &csid))
217 return -1;
218
219 if (ocsd_dt_set_pkt_protocol_printer(decoder->dcd_tree, csid, 0))
220 return -1;
221
222 return 0;
223}
224
225static int
226cs_etm_decoder__create_etm_packet_printer(struct cs_etm_trace_params *t_params,
227 struct cs_etm_decoder *decoder)
228{
229 const char *decoder_name;
230 ocsd_etmv4_cfg trace_config_etmv4;
231 void *trace_config;
232
233 switch (t_params->protocol) {
234 case CS_ETM_PROTO_ETMV4i:
235 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
236 decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
237 trace_config = &trace_config_etmv4;
238 break;
239 default:
240 return -1;
241 }
242
243 return cs_etm_decoder__create_packet_printer(decoder,
244 decoder_name,
245 trace_config);
246}
247
248static void cs_etm_decoder__clear_buffer(struct cs_etm_decoder *decoder)
249{
250 int i;
251
252 decoder->head = 0;
253 decoder->tail = 0;
254 decoder->packet_count = 0;
255 for (i = 0; i < MAX_BUFFER; i++) {
256 decoder->packet_buffer[i].start_addr = 0xdeadbeefdeadbeefUL;
Robert Walkere573e972018-02-14 11:24:39 +0000257 decoder->packet_buffer[i].end_addr = 0xdeadbeefdeadbeefUL;
258 decoder->packet_buffer[i].last_instr_taken_branch = false;
259 decoder->packet_buffer[i].exc = false;
260 decoder->packet_buffer[i].exc_ret = false;
261 decoder->packet_buffer[i].cpu = INT_MIN;
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700262 }
263}
264
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700265static ocsd_datapath_resp_t
266cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder,
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700267 const u8 trace_chan_id,
268 enum cs_etm_sample_type sample_type)
269{
270 u32 et = 0;
271 struct int_node *inode = NULL;
272
273 if (decoder->packet_count >= MAX_BUFFER - 1)
274 return OCSD_RESP_FATAL_SYS_ERR;
275
276 /* Search the RB tree for the cpu associated with this traceID */
277 inode = intlist__find(traceid_list, trace_chan_id);
278 if (!inode)
279 return OCSD_RESP_FATAL_SYS_ERR;
280
281 et = decoder->tail;
Robert Walkere573e972018-02-14 11:24:39 +0000282 et = (et + 1) & (MAX_BUFFER - 1);
283 decoder->tail = et;
284 decoder->packet_count++;
285
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700286 decoder->packet_buffer[et].sample_type = sample_type;
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700287 decoder->packet_buffer[et].exc = false;
288 decoder->packet_buffer[et].exc_ret = false;
289 decoder->packet_buffer[et].cpu = *((int *)inode->priv);
Robert Walkere573e972018-02-14 11:24:39 +0000290 decoder->packet_buffer[et].start_addr = 0xdeadbeefdeadbeefUL;
291 decoder->packet_buffer[et].end_addr = 0xdeadbeefdeadbeefUL;
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700292
293 if (decoder->packet_count == MAX_BUFFER - 1)
294 return OCSD_RESP_WAIT;
295
296 return OCSD_RESP_CONT;
297}
298
Robert Walkere573e972018-02-14 11:24:39 +0000299static ocsd_datapath_resp_t
300cs_etm_decoder__buffer_range(struct cs_etm_decoder *decoder,
301 const ocsd_generic_trace_elem *elem,
302 const uint8_t trace_chan_id)
303{
304 int ret = 0;
305 struct cs_etm_packet *packet;
306
307 ret = cs_etm_decoder__buffer_packet(decoder, trace_chan_id,
308 CS_ETM_RANGE);
309 if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
310 return ret;
311
312 packet = &decoder->packet_buffer[decoder->tail];
313
314 packet->start_addr = elem->st_addr;
315 packet->end_addr = elem->en_addr;
316 switch (elem->last_i_type) {
317 case OCSD_INSTR_BR:
318 case OCSD_INSTR_BR_INDIRECT:
319 packet->last_instr_taken_branch = elem->last_instr_exec;
320 break;
321 case OCSD_INSTR_ISB:
322 case OCSD_INSTR_DSB_DMB:
323 case OCSD_INSTR_OTHER:
324 default:
325 packet->last_instr_taken_branch = false;
326 break;
327 }
328
329 return ret;
Robert Walker256e7512018-02-14 11:24:40 +0000330}
Robert Walkere573e972018-02-14 11:24:39 +0000331
Robert Walker256e7512018-02-14 11:24:40 +0000332static ocsd_datapath_resp_t
333cs_etm_decoder__buffer_trace_on(struct cs_etm_decoder *decoder,
334 const uint8_t trace_chan_id)
335{
336 return cs_etm_decoder__buffer_packet(decoder, trace_chan_id,
337 CS_ETM_TRACE_ON);
Robert Walkere573e972018-02-14 11:24:39 +0000338}
339
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700340static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
341 const void *context,
342 const ocsd_trc_index_t indx __maybe_unused,
343 const u8 trace_chan_id __maybe_unused,
344 const ocsd_generic_trace_elem *elem)
345{
346 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
347 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
348
349 switch (elem->elem_type) {
350 case OCSD_GEN_TRC_ELEM_UNKNOWN:
351 break;
352 case OCSD_GEN_TRC_ELEM_NO_SYNC:
353 decoder->trace_on = false;
354 break;
355 case OCSD_GEN_TRC_ELEM_TRACE_ON:
Robert Walker256e7512018-02-14 11:24:40 +0000356 resp = cs_etm_decoder__buffer_trace_on(decoder,
357 trace_chan_id);
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700358 decoder->trace_on = true;
359 break;
360 case OCSD_GEN_TRC_ELEM_INSTR_RANGE:
Robert Walkere573e972018-02-14 11:24:39 +0000361 resp = cs_etm_decoder__buffer_range(decoder, elem,
362 trace_chan_id);
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700363 break;
364 case OCSD_GEN_TRC_ELEM_EXCEPTION:
365 decoder->packet_buffer[decoder->tail].exc = true;
366 break;
367 case OCSD_GEN_TRC_ELEM_EXCEPTION_RET:
368 decoder->packet_buffer[decoder->tail].exc_ret = true;
369 break;
370 case OCSD_GEN_TRC_ELEM_PE_CONTEXT:
371 case OCSD_GEN_TRC_ELEM_EO_TRACE:
372 case OCSD_GEN_TRC_ELEM_ADDR_NACC:
373 case OCSD_GEN_TRC_ELEM_TIMESTAMP:
374 case OCSD_GEN_TRC_ELEM_CYCLE_COUNT:
375 case OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN:
376 case OCSD_GEN_TRC_ELEM_EVENT:
377 case OCSD_GEN_TRC_ELEM_SWTRACE:
378 case OCSD_GEN_TRC_ELEM_CUSTOM:
379 default:
380 break;
381 }
382
383 return resp;
384}
385
386static int cs_etm_decoder__create_etm_packet_decoder(
387 struct cs_etm_trace_params *t_params,
388 struct cs_etm_decoder *decoder)
389{
390 const char *decoder_name;
391 ocsd_etmv4_cfg trace_config_etmv4;
392 void *trace_config;
393 u8 csid;
394
395 switch (t_params->protocol) {
396 case CS_ETM_PROTO_ETMV4i:
397 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
398 decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
399 trace_config = &trace_config_etmv4;
400 break;
401 default:
402 return -1;
403 }
404
405 if (ocsd_dt_create_decoder(decoder->dcd_tree,
406 decoder_name,
407 OCSD_CREATE_FLG_FULL_DECODER,
408 trace_config, &csid))
409 return -1;
410
411 if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree,
412 cs_etm_decoder__gen_trace_elem_printer,
413 decoder))
414 return -1;
415
416 return 0;
417}
418
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700419static int
420cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params,
421 struct cs_etm_trace_params *t_params,
422 struct cs_etm_decoder *decoder)
423{
424 if (d_params->operation == CS_ETM_OPERATION_PRINT)
425 return cs_etm_decoder__create_etm_packet_printer(t_params,
426 decoder);
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700427 else if (d_params->operation == CS_ETM_OPERATION_DECODE)
428 return cs_etm_decoder__create_etm_packet_decoder(t_params,
429 decoder);
430
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700431 return -1;
432}
433
434struct cs_etm_decoder *
435cs_etm_decoder__new(int num_cpu, struct cs_etm_decoder_params *d_params,
436 struct cs_etm_trace_params t_params[])
437{
438 struct cs_etm_decoder *decoder;
439 ocsd_dcd_tree_src_t format;
440 u32 flags;
441 int i, ret;
442
443 if ((!t_params) || (!d_params))
444 return NULL;
445
446 decoder = zalloc(sizeof(*decoder));
447
448 if (!decoder)
449 return NULL;
450
451 decoder->data = d_params->data;
452 decoder->prev_return = OCSD_RESP_CONT;
453 cs_etm_decoder__clear_buffer(decoder);
454 format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED :
455 OCSD_TRC_SRC_SINGLE);
456 flags = 0;
457 flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0);
458 flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0);
459 flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0);
460
461 /*
462 * Drivers may add barrier frames when used with perf, set up to
463 * handle this. Barriers const of FSYNC packet repeated 4 times.
464 */
465 flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC;
466
467 /* Create decode tree for the data source */
468 decoder->dcd_tree = ocsd_create_dcd_tree(format, flags);
469
470 if (decoder->dcd_tree == 0)
471 goto err_free_decoder;
472
473 /* init library print logging support */
474 ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder);
475 if (ret != 0)
476 goto err_free_decoder_tree;
477
478 /* init raw frame logging if required */
479 cs_etm_decoder__init_raw_frame_logging(d_params, decoder);
480
481 for (i = 0; i < num_cpu; i++) {
482 ret = cs_etm_decoder__create_etm_decoder(d_params,
483 &t_params[i],
484 decoder);
485 if (ret != 0)
486 goto err_free_decoder_tree;
487 }
488
489 return decoder;
490
491err_free_decoder_tree:
492 ocsd_destroy_dcd_tree(decoder->dcd_tree);
493err_free_decoder:
494 free(decoder);
495 return NULL;
496}
497
498int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder,
499 u64 indx, const u8 *buf,
500 size_t len, size_t *consumed)
501{
502 int ret = 0;
503 ocsd_datapath_resp_t cur = OCSD_RESP_CONT;
504 ocsd_datapath_resp_t prev_return = decoder->prev_return;
505 size_t processed = 0;
506 u32 count;
507
508 while (processed < len) {
509 if (OCSD_DATA_RESP_IS_WAIT(prev_return)) {
510 cur = ocsd_dt_process_data(decoder->dcd_tree,
511 OCSD_OP_FLUSH,
512 0,
513 0,
514 NULL,
515 NULL);
516 } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) {
517 cur = ocsd_dt_process_data(decoder->dcd_tree,
518 OCSD_OP_DATA,
519 indx + processed,
520 len - processed,
521 &buf[processed],
522 &count);
523 processed += count;
524 } else {
525 ret = -EINVAL;
526 break;
527 }
528
529 /*
530 * Return to the input code if the packet buffer is full.
531 * Flushing will get done once the packet buffer has been
532 * processed.
533 */
534 if (OCSD_DATA_RESP_IS_WAIT(cur))
535 break;
536
537 prev_return = cur;
538 }
539
540 decoder->prev_return = cur;
541 *consumed = processed;
542
543 return ret;
544}
545
546void cs_etm_decoder__free(struct cs_etm_decoder *decoder)
547{
548 if (!decoder)
549 return;
550
551 ocsd_destroy_dcd_tree(decoder->dcd_tree);
552 decoder->dcd_tree = NULL;
553 free(decoder);
554}