1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 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 34 #define CS_ETM_INVAL_ADDR 0xdeadbeefdeadbeefUL 35 36 struct cs_etm_decoder { 37 void *data; 38 void (*packet_printer)(const char *msg); 39 dcd_tree_handle_t dcd_tree; 40 cs_etm_mem_cb_type mem_access; 41 ocsd_datapath_resp_t prev_return; 42 u32 packet_count; 43 u32 head; 44 u32 tail; 45 struct cs_etm_packet packet_buffer[MAX_BUFFER]; 46 }; 47 48 static u32 49 cs_etm_decoder__mem_access(const void *context, 50 const ocsd_vaddr_t address, 51 const ocsd_mem_space_acc_t mem_space __maybe_unused, 52 const u32 req_size, 53 u8 *buffer) 54 { 55 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context; 56 57 return decoder->mem_access(decoder->data, 58 address, 59 req_size, 60 buffer); 61 } 62 63 int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder, 64 u64 start, u64 end, 65 cs_etm_mem_cb_type cb_func) 66 { 67 decoder->mem_access = cb_func; 68 69 if (ocsd_dt_add_callback_mem_acc(decoder->dcd_tree, start, end, 70 OCSD_MEM_SPACE_ANY, 71 cs_etm_decoder__mem_access, decoder)) 72 return -1; 73 74 return 0; 75 } 76 77 int cs_etm_decoder__reset(struct cs_etm_decoder *decoder) 78 { 79 ocsd_datapath_resp_t dp_ret; 80 81 decoder->prev_return = OCSD_RESP_CONT; 82 83 dp_ret = ocsd_dt_process_data(decoder->dcd_tree, OCSD_OP_RESET, 84 0, 0, NULL, NULL); 85 if (OCSD_DATA_RESP_IS_FATAL(dp_ret)) 86 return -1; 87 88 return 0; 89 } 90 91 int cs_etm_decoder__get_packet(struct cs_etm_decoder *decoder, 92 struct cs_etm_packet *packet) 93 { 94 if (!decoder || !packet) 95 return -EINVAL; 96 97 /* Nothing to do, might as well just return */ 98 if (decoder->packet_count == 0) 99 return 0; 100 /* 101 * The queueing process in function cs_etm_decoder__buffer_packet() 102 * increments the tail *before* using it. This is somewhat counter 103 * intuitive but it has the advantage of centralizing tail management 104 * at a single location. Because of that we need to follow the same 105 * heuristic with the head, i.e we increment it before using its 106 * value. Otherwise the first element of the packet queue is not 107 * used. 108 */ 109 decoder->head = (decoder->head + 1) & (MAX_BUFFER - 1); 110 111 *packet = decoder->packet_buffer[decoder->head]; 112 113 decoder->packet_count--; 114 115 return 1; 116 } 117 118 static int cs_etm_decoder__gen_etmv3_config(struct cs_etm_trace_params *params, 119 ocsd_etmv3_cfg *config) 120 { 121 config->reg_idr = params->etmv3.reg_idr; 122 config->reg_ctrl = params->etmv3.reg_ctrl; 123 config->reg_ccer = params->etmv3.reg_ccer; 124 config->reg_trc_id = params->etmv3.reg_trc_id; 125 config->arch_ver = ARCH_V7; 126 config->core_prof = profile_CortexA; 127 128 return 0; 129 } 130 131 static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params, 132 ocsd_etmv4_cfg *config) 133 { 134 config->reg_configr = params->etmv4.reg_configr; 135 config->reg_traceidr = params->etmv4.reg_traceidr; 136 config->reg_idr0 = params->etmv4.reg_idr0; 137 config->reg_idr1 = params->etmv4.reg_idr1; 138 config->reg_idr2 = params->etmv4.reg_idr2; 139 config->reg_idr8 = params->etmv4.reg_idr8; 140 config->reg_idr9 = 0; 141 config->reg_idr10 = 0; 142 config->reg_idr11 = 0; 143 config->reg_idr12 = 0; 144 config->reg_idr13 = 0; 145 config->arch_ver = ARCH_V8; 146 config->core_prof = profile_CortexA; 147 } 148 149 static void cs_etm_decoder__print_str_cb(const void *p_context, 150 const char *msg, 151 const int str_len) 152 { 153 if (p_context && str_len) 154 ((struct cs_etm_decoder *)p_context)->packet_printer(msg); 155 } 156 157 static int 158 cs_etm_decoder__init_def_logger_printing(struct cs_etm_decoder_params *d_params, 159 struct cs_etm_decoder *decoder) 160 { 161 int ret = 0; 162 163 if (d_params->packet_printer == NULL) 164 return -1; 165 166 decoder->packet_printer = d_params->packet_printer; 167 168 /* 169 * Set up a library default logger to process any printers 170 * (packet/raw frame) we add later. 171 */ 172 ret = ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1); 173 if (ret != 0) 174 return -1; 175 176 /* no stdout / err / file output */ 177 ret = ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL); 178 if (ret != 0) 179 return -1; 180 181 /* 182 * Set the string CB for the default logger, passes strings to 183 * perf print logger. 184 */ 185 ret = ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree, 186 (void *)decoder, 187 cs_etm_decoder__print_str_cb); 188 if (ret != 0) 189 ret = -1; 190 191 return 0; 192 } 193 194 #ifdef CS_LOG_RAW_FRAMES 195 static void 196 cs_etm_decoder__init_raw_frame_logging(struct cs_etm_decoder_params *d_params, 197 struct cs_etm_decoder *decoder) 198 { 199 /* Only log these during a --dump operation */ 200 if (d_params->operation == CS_ETM_OPERATION_PRINT) { 201 /* set up a library default logger to process the 202 * raw frame printer we add later 203 */ 204 ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1); 205 206 /* no stdout / err / file output */ 207 ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL); 208 209 /* set the string CB for the default logger, 210 * passes strings to perf print logger. 211 */ 212 ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree, 213 (void *)decoder, 214 cs_etm_decoder__print_str_cb); 215 216 /* use the built in library printer for the raw frames */ 217 ocsd_dt_set_raw_frame_printer(decoder->dcd_tree, 218 CS_RAW_DEBUG_FLAGS); 219 } 220 } 221 #else 222 static void 223 cs_etm_decoder__init_raw_frame_logging( 224 struct cs_etm_decoder_params *d_params __maybe_unused, 225 struct cs_etm_decoder *decoder __maybe_unused) 226 { 227 } 228 #endif 229 230 static int cs_etm_decoder__create_packet_printer(struct cs_etm_decoder *decoder, 231 const char *decoder_name, 232 void *trace_config) 233 { 234 u8 csid; 235 236 if (ocsd_dt_create_decoder(decoder->dcd_tree, decoder_name, 237 OCSD_CREATE_FLG_PACKET_PROC, 238 trace_config, &csid)) 239 return -1; 240 241 if (ocsd_dt_set_pkt_protocol_printer(decoder->dcd_tree, csid, 0)) 242 return -1; 243 244 return 0; 245 } 246 247 static int 248 cs_etm_decoder__create_etm_packet_printer(struct cs_etm_trace_params *t_params, 249 struct cs_etm_decoder *decoder) 250 { 251 const char *decoder_name; 252 ocsd_etmv3_cfg config_etmv3; 253 ocsd_etmv4_cfg trace_config_etmv4; 254 void *trace_config; 255 256 switch (t_params->protocol) { 257 case CS_ETM_PROTO_ETMV3: 258 case CS_ETM_PROTO_PTM: 259 cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3); 260 decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ? 261 OCSD_BUILTIN_DCD_ETMV3 : 262 OCSD_BUILTIN_DCD_PTM; 263 trace_config = &config_etmv3; 264 break; 265 case CS_ETM_PROTO_ETMV4i: 266 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4); 267 decoder_name = OCSD_BUILTIN_DCD_ETMV4I; 268 trace_config = &trace_config_etmv4; 269 break; 270 default: 271 return -1; 272 } 273 274 return cs_etm_decoder__create_packet_printer(decoder, 275 decoder_name, 276 trace_config); 277 } 278 279 static void cs_etm_decoder__clear_buffer(struct cs_etm_decoder *decoder) 280 { 281 int i; 282 283 decoder->head = 0; 284 decoder->tail = 0; 285 decoder->packet_count = 0; 286 for (i = 0; i < MAX_BUFFER; i++) { 287 decoder->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN; 288 decoder->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR; 289 decoder->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR; 290 decoder->packet_buffer[i].instr_count = 0; 291 decoder->packet_buffer[i].last_instr_taken_branch = false; 292 decoder->packet_buffer[i].last_instr_size = 0; 293 decoder->packet_buffer[i].cpu = INT_MIN; 294 } 295 } 296 297 static ocsd_datapath_resp_t 298 cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder, 299 const u8 trace_chan_id, 300 enum cs_etm_sample_type sample_type) 301 { 302 u32 et = 0; 303 struct int_node *inode = NULL; 304 305 if (decoder->packet_count >= MAX_BUFFER - 1) 306 return OCSD_RESP_FATAL_SYS_ERR; 307 308 /* Search the RB tree for the cpu associated with this traceID */ 309 inode = intlist__find(traceid_list, trace_chan_id); 310 if (!inode) 311 return OCSD_RESP_FATAL_SYS_ERR; 312 313 et = decoder->tail; 314 et = (et + 1) & (MAX_BUFFER - 1); 315 decoder->tail = et; 316 decoder->packet_count++; 317 318 decoder->packet_buffer[et].sample_type = sample_type; 319 decoder->packet_buffer[et].isa = CS_ETM_ISA_UNKNOWN; 320 decoder->packet_buffer[et].cpu = *((int *)inode->priv); 321 decoder->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR; 322 decoder->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR; 323 decoder->packet_buffer[et].instr_count = 0; 324 decoder->packet_buffer[et].last_instr_taken_branch = false; 325 decoder->packet_buffer[et].last_instr_size = 0; 326 327 if (decoder->packet_count == MAX_BUFFER - 1) 328 return OCSD_RESP_WAIT; 329 330 return OCSD_RESP_CONT; 331 } 332 333 static ocsd_datapath_resp_t 334 cs_etm_decoder__buffer_range(struct cs_etm_decoder *decoder, 335 const ocsd_generic_trace_elem *elem, 336 const uint8_t trace_chan_id) 337 { 338 int ret = 0; 339 struct cs_etm_packet *packet; 340 341 ret = cs_etm_decoder__buffer_packet(decoder, trace_chan_id, 342 CS_ETM_RANGE); 343 if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT) 344 return ret; 345 346 packet = &decoder->packet_buffer[decoder->tail]; 347 348 switch (elem->isa) { 349 case ocsd_isa_aarch64: 350 packet->isa = CS_ETM_ISA_A64; 351 break; 352 case ocsd_isa_arm: 353 packet->isa = CS_ETM_ISA_A32; 354 break; 355 case ocsd_isa_thumb2: 356 packet->isa = CS_ETM_ISA_T32; 357 break; 358 case ocsd_isa_tee: 359 case ocsd_isa_jazelle: 360 case ocsd_isa_custom: 361 case ocsd_isa_unknown: 362 default: 363 packet->isa = CS_ETM_ISA_UNKNOWN; 364 } 365 366 packet->start_addr = elem->st_addr; 367 packet->end_addr = elem->en_addr; 368 packet->instr_count = elem->num_instr_range; 369 370 switch (elem->last_i_type) { 371 case OCSD_INSTR_BR: 372 case OCSD_INSTR_BR_INDIRECT: 373 packet->last_instr_taken_branch = elem->last_instr_exec; 374 break; 375 case OCSD_INSTR_ISB: 376 case OCSD_INSTR_DSB_DMB: 377 case OCSD_INSTR_OTHER: 378 default: 379 packet->last_instr_taken_branch = false; 380 break; 381 } 382 383 packet->last_instr_size = elem->last_instr_sz; 384 385 return ret; 386 } 387 388 static ocsd_datapath_resp_t 389 cs_etm_decoder__buffer_discontinuity(struct cs_etm_decoder *decoder, 390 const uint8_t trace_chan_id) 391 { 392 return cs_etm_decoder__buffer_packet(decoder, trace_chan_id, 393 CS_ETM_DISCONTINUITY); 394 } 395 396 static ocsd_datapath_resp_t 397 cs_etm_decoder__buffer_exception(struct cs_etm_decoder *decoder, 398 const uint8_t trace_chan_id) 399 { 400 return cs_etm_decoder__buffer_packet(decoder, trace_chan_id, 401 CS_ETM_EXCEPTION); 402 } 403 404 static ocsd_datapath_resp_t 405 cs_etm_decoder__buffer_exception_ret(struct cs_etm_decoder *decoder, 406 const uint8_t trace_chan_id) 407 { 408 return cs_etm_decoder__buffer_packet(decoder, trace_chan_id, 409 CS_ETM_EXCEPTION_RET); 410 } 411 412 static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( 413 const void *context, 414 const ocsd_trc_index_t indx __maybe_unused, 415 const u8 trace_chan_id __maybe_unused, 416 const ocsd_generic_trace_elem *elem) 417 { 418 ocsd_datapath_resp_t resp = OCSD_RESP_CONT; 419 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context; 420 421 switch (elem->elem_type) { 422 case OCSD_GEN_TRC_ELEM_UNKNOWN: 423 break; 424 case OCSD_GEN_TRC_ELEM_EO_TRACE: 425 case OCSD_GEN_TRC_ELEM_NO_SYNC: 426 case OCSD_GEN_TRC_ELEM_TRACE_ON: 427 resp = cs_etm_decoder__buffer_discontinuity(decoder, 428 trace_chan_id); 429 break; 430 case OCSD_GEN_TRC_ELEM_INSTR_RANGE: 431 resp = cs_etm_decoder__buffer_range(decoder, elem, 432 trace_chan_id); 433 break; 434 case OCSD_GEN_TRC_ELEM_EXCEPTION: 435 resp = cs_etm_decoder__buffer_exception(decoder, 436 trace_chan_id); 437 break; 438 case OCSD_GEN_TRC_ELEM_EXCEPTION_RET: 439 resp = cs_etm_decoder__buffer_exception_ret(decoder, 440 trace_chan_id); 441 break; 442 case OCSD_GEN_TRC_ELEM_PE_CONTEXT: 443 case OCSD_GEN_TRC_ELEM_ADDR_NACC: 444 case OCSD_GEN_TRC_ELEM_TIMESTAMP: 445 case OCSD_GEN_TRC_ELEM_CYCLE_COUNT: 446 case OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN: 447 case OCSD_GEN_TRC_ELEM_EVENT: 448 case OCSD_GEN_TRC_ELEM_SWTRACE: 449 case OCSD_GEN_TRC_ELEM_CUSTOM: 450 default: 451 break; 452 } 453 454 return resp; 455 } 456 457 static int cs_etm_decoder__create_etm_packet_decoder( 458 struct cs_etm_trace_params *t_params, 459 struct cs_etm_decoder *decoder) 460 { 461 const char *decoder_name; 462 ocsd_etmv3_cfg config_etmv3; 463 ocsd_etmv4_cfg trace_config_etmv4; 464 void *trace_config; 465 u8 csid; 466 467 switch (t_params->protocol) { 468 case CS_ETM_PROTO_ETMV3: 469 case CS_ETM_PROTO_PTM: 470 cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3); 471 decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ? 472 OCSD_BUILTIN_DCD_ETMV3 : 473 OCSD_BUILTIN_DCD_PTM; 474 trace_config = &config_etmv3; 475 break; 476 case CS_ETM_PROTO_ETMV4i: 477 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4); 478 decoder_name = OCSD_BUILTIN_DCD_ETMV4I; 479 trace_config = &trace_config_etmv4; 480 break; 481 default: 482 return -1; 483 } 484 485 if (ocsd_dt_create_decoder(decoder->dcd_tree, 486 decoder_name, 487 OCSD_CREATE_FLG_FULL_DECODER, 488 trace_config, &csid)) 489 return -1; 490 491 if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree, 492 cs_etm_decoder__gen_trace_elem_printer, 493 decoder)) 494 return -1; 495 496 return 0; 497 } 498 499 static int 500 cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params, 501 struct cs_etm_trace_params *t_params, 502 struct cs_etm_decoder *decoder) 503 { 504 if (d_params->operation == CS_ETM_OPERATION_PRINT) 505 return cs_etm_decoder__create_etm_packet_printer(t_params, 506 decoder); 507 else if (d_params->operation == CS_ETM_OPERATION_DECODE) 508 return cs_etm_decoder__create_etm_packet_decoder(t_params, 509 decoder); 510 511 return -1; 512 } 513 514 struct cs_etm_decoder * 515 cs_etm_decoder__new(int num_cpu, struct cs_etm_decoder_params *d_params, 516 struct cs_etm_trace_params t_params[]) 517 { 518 struct cs_etm_decoder *decoder; 519 ocsd_dcd_tree_src_t format; 520 u32 flags; 521 int i, ret; 522 523 if ((!t_params) || (!d_params)) 524 return NULL; 525 526 decoder = zalloc(sizeof(*decoder)); 527 528 if (!decoder) 529 return NULL; 530 531 decoder->data = d_params->data; 532 decoder->prev_return = OCSD_RESP_CONT; 533 cs_etm_decoder__clear_buffer(decoder); 534 format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED : 535 OCSD_TRC_SRC_SINGLE); 536 flags = 0; 537 flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0); 538 flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0); 539 flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0); 540 541 /* 542 * Drivers may add barrier frames when used with perf, set up to 543 * handle this. Barriers const of FSYNC packet repeated 4 times. 544 */ 545 flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC; 546 547 /* Create decode tree for the data source */ 548 decoder->dcd_tree = ocsd_create_dcd_tree(format, flags); 549 550 if (decoder->dcd_tree == 0) 551 goto err_free_decoder; 552 553 /* init library print logging support */ 554 ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder); 555 if (ret != 0) 556 goto err_free_decoder_tree; 557 558 /* init raw frame logging if required */ 559 cs_etm_decoder__init_raw_frame_logging(d_params, decoder); 560 561 for (i = 0; i < num_cpu; i++) { 562 ret = cs_etm_decoder__create_etm_decoder(d_params, 563 &t_params[i], 564 decoder); 565 if (ret != 0) 566 goto err_free_decoder_tree; 567 } 568 569 return decoder; 570 571 err_free_decoder_tree: 572 ocsd_destroy_dcd_tree(decoder->dcd_tree); 573 err_free_decoder: 574 free(decoder); 575 return NULL; 576 } 577 578 int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder, 579 u64 indx, const u8 *buf, 580 size_t len, size_t *consumed) 581 { 582 int ret = 0; 583 ocsd_datapath_resp_t cur = OCSD_RESP_CONT; 584 ocsd_datapath_resp_t prev_return = decoder->prev_return; 585 size_t processed = 0; 586 u32 count; 587 588 while (processed < len) { 589 if (OCSD_DATA_RESP_IS_WAIT(prev_return)) { 590 cur = ocsd_dt_process_data(decoder->dcd_tree, 591 OCSD_OP_FLUSH, 592 0, 593 0, 594 NULL, 595 NULL); 596 } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) { 597 cur = ocsd_dt_process_data(decoder->dcd_tree, 598 OCSD_OP_DATA, 599 indx + processed, 600 len - processed, 601 &buf[processed], 602 &count); 603 processed += count; 604 } else { 605 ret = -EINVAL; 606 break; 607 } 608 609 /* 610 * Return to the input code if the packet buffer is full. 611 * Flushing will get done once the packet buffer has been 612 * processed. 613 */ 614 if (OCSD_DATA_RESP_IS_WAIT(cur)) 615 break; 616 617 prev_return = cur; 618 } 619 620 decoder->prev_return = cur; 621 *consumed = processed; 622 623 return ret; 624 } 625 626 void cs_etm_decoder__free(struct cs_etm_decoder *decoder) 627 { 628 if (!decoder) 629 return; 630 631 ocsd_destroy_dcd_tree(decoder->dcd_tree); 632 decoder->dcd_tree = NULL; 633 free(decoder); 634 } 635