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 <asm/bug.h> 10 #include <linux/coresight-pmu.h> 11 #include <linux/err.h> 12 #include <linux/list.h> 13 #include <linux/zalloc.h> 14 #include <stdlib.h> 15 #include <opencsd/c_api/opencsd_c_api.h> 16 #include <opencsd/etmv4/trc_pkt_types_etmv4.h> 17 #include <opencsd/ocsd_if_types.h> 18 19 #include "cs-etm.h" 20 #include "cs-etm-decoder.h" 21 #include "debug.h" 22 #include "intlist.h" 23 24 /* use raw logging */ 25 #ifdef CS_DEBUG_RAW 26 #define CS_LOG_RAW_FRAMES 27 #ifdef CS_RAW_PACKED 28 #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT | \ 29 OCSD_DFRMTR_PACKED_RAW_OUT) 30 #else 31 #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT) 32 #endif 33 #endif 34 35 struct cs_etm_decoder { 36 void *data; 37 void (*packet_printer)(const char *msg); 38 bool suppress_printing; 39 dcd_tree_handle_t dcd_tree; 40 cs_etm_mem_cb_type mem_access; 41 ocsd_datapath_resp_t prev_return; 42 }; 43 44 static u32 45 cs_etm_decoder__mem_access(const void *context, 46 const ocsd_vaddr_t address, 47 const ocsd_mem_space_acc_t mem_space __maybe_unused, 48 const u8 trace_chan_id, 49 const u32 req_size, 50 u8 *buffer) 51 { 52 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context; 53 54 return decoder->mem_access(decoder->data, trace_chan_id, 55 address, req_size, buffer); 56 } 57 58 int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder, 59 u64 start, u64 end, 60 cs_etm_mem_cb_type cb_func) 61 { 62 decoder->mem_access = cb_func; 63 64 if (ocsd_dt_add_callback_trcid_mem_acc(decoder->dcd_tree, start, end, 65 OCSD_MEM_SPACE_ANY, 66 cs_etm_decoder__mem_access, 67 decoder)) 68 return -1; 69 70 return 0; 71 } 72 73 int cs_etm_decoder__reset(struct cs_etm_decoder *decoder) 74 { 75 ocsd_datapath_resp_t dp_ret; 76 77 decoder->prev_return = OCSD_RESP_CONT; 78 decoder->suppress_printing = true; 79 dp_ret = ocsd_dt_process_data(decoder->dcd_tree, OCSD_OP_RESET, 80 0, 0, NULL, NULL); 81 decoder->suppress_printing = false; 82 if (OCSD_DATA_RESP_IS_FATAL(dp_ret)) 83 return -1; 84 85 return 0; 86 } 87 88 int cs_etm_decoder__get_packet(struct cs_etm_packet_queue *packet_queue, 89 struct cs_etm_packet *packet) 90 { 91 if (!packet_queue || !packet) 92 return -EINVAL; 93 94 /* Nothing to do, might as well just return */ 95 if (packet_queue->packet_count == 0) 96 return 0; 97 /* 98 * The queueing process in function cs_etm_decoder__buffer_packet() 99 * increments the tail *before* using it. This is somewhat counter 100 * intuitive but it has the advantage of centralizing tail management 101 * at a single location. Because of that we need to follow the same 102 * heuristic with the head, i.e we increment it before using its 103 * value. Otherwise the first element of the packet queue is not 104 * used. 105 */ 106 packet_queue->head = (packet_queue->head + 1) & 107 (CS_ETM_PACKET_MAX_BUFFER - 1); 108 109 *packet = packet_queue->packet_buffer[packet_queue->head]; 110 111 packet_queue->packet_count--; 112 113 return 1; 114 } 115 116 static int cs_etm_decoder__gen_etmv3_config(struct cs_etm_trace_params *params, 117 ocsd_etmv3_cfg *config) 118 { 119 config->reg_idr = params->etmv3.reg_idr; 120 config->reg_ctrl = params->etmv3.reg_ctrl; 121 config->reg_ccer = params->etmv3.reg_ccer; 122 config->reg_trc_id = params->etmv3.reg_trc_id; 123 config->arch_ver = ARCH_V7; 124 config->core_prof = profile_CortexA; 125 126 return 0; 127 } 128 129 static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params, 130 ocsd_etmv4_cfg *config) 131 { 132 config->reg_configr = params->etmv4.reg_configr; 133 config->reg_traceidr = params->etmv4.reg_traceidr; 134 config->reg_idr0 = params->etmv4.reg_idr0; 135 config->reg_idr1 = params->etmv4.reg_idr1; 136 config->reg_idr2 = params->etmv4.reg_idr2; 137 config->reg_idr8 = params->etmv4.reg_idr8; 138 config->reg_idr9 = 0; 139 config->reg_idr10 = 0; 140 config->reg_idr11 = 0; 141 config->reg_idr12 = 0; 142 config->reg_idr13 = 0; 143 config->arch_ver = ARCH_V8; 144 config->core_prof = profile_CortexA; 145 } 146 147 static void cs_etm_decoder__print_str_cb(const void *p_context, 148 const char *msg, 149 const int str_len) 150 { 151 const struct cs_etm_decoder *decoder = p_context; 152 153 if (p_context && str_len && !decoder->suppress_printing) 154 decoder->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 ocsd_datapath_resp_t 280 cs_etm_decoder__do_soft_timestamp(struct cs_etm_queue *etmq, 281 struct cs_etm_packet_queue *packet_queue, 282 const uint8_t trace_chan_id) 283 { 284 /* No timestamp packet has been received, nothing to do */ 285 if (!packet_queue->cs_timestamp) 286 return OCSD_RESP_CONT; 287 288 packet_queue->cs_timestamp = packet_queue->next_cs_timestamp; 289 290 /* Estimate the timestamp for the next range packet */ 291 packet_queue->next_cs_timestamp += packet_queue->instr_count; 292 packet_queue->instr_count = 0; 293 294 /* Tell the front end which traceid_queue needs attention */ 295 cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id); 296 297 return OCSD_RESP_WAIT; 298 } 299 300 static ocsd_datapath_resp_t 301 cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq, 302 const ocsd_generic_trace_elem *elem, 303 const uint8_t trace_chan_id, 304 const ocsd_trc_index_t indx) 305 { 306 struct cs_etm_packet_queue *packet_queue; 307 308 /* First get the packet queue for this traceID */ 309 packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id); 310 if (!packet_queue) 311 return OCSD_RESP_FATAL_SYS_ERR; 312 313 /* 314 * We've seen a timestamp packet before - simply record the new value. 315 * Function do_soft_timestamp() will report the value to the front end, 316 * hence asking the decoder to keep decoding rather than stopping. 317 */ 318 if (packet_queue->cs_timestamp) { 319 packet_queue->next_cs_timestamp = elem->timestamp; 320 return OCSD_RESP_CONT; 321 } 322 323 324 if (!elem->timestamp) { 325 /* 326 * Zero timestamps can be seen due to misconfiguration or hardware bugs. 327 * Warn once, and don't try to subtract instr_count as it would result in an 328 * underflow. 329 */ 330 packet_queue->cs_timestamp = 0; 331 WARN_ONCE(true, "Zero Coresight timestamp found at Idx:%" OCSD_TRC_IDX_STR 332 ". Decoding may be improved with --itrace=Z...\n", indx); 333 } else if (packet_queue->instr_count > elem->timestamp) { 334 /* 335 * Sanity check that the elem->timestamp - packet_queue->instr_count would not 336 * result in an underflow. Warn and clamp at 0 if it would. 337 */ 338 packet_queue->cs_timestamp = 0; 339 pr_err("Timestamp calculation underflow at Idx:%" OCSD_TRC_IDX_STR "\n", indx); 340 } else { 341 /* 342 * This is the first timestamp we've seen since the beginning of traces 343 * or a discontinuity. Since timestamps packets are generated *after* 344 * range packets have been generated, we need to estimate the time at 345 * which instructions started by subtracting the number of instructions 346 * executed to the timestamp. 347 */ 348 packet_queue->cs_timestamp = elem->timestamp - packet_queue->instr_count; 349 } 350 packet_queue->next_cs_timestamp = elem->timestamp; 351 packet_queue->instr_count = 0; 352 353 /* Tell the front end which traceid_queue needs attention */ 354 cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id); 355 356 /* Halt processing until we are being told to proceed */ 357 return OCSD_RESP_WAIT; 358 } 359 360 static void 361 cs_etm_decoder__reset_timestamp(struct cs_etm_packet_queue *packet_queue) 362 { 363 packet_queue->cs_timestamp = 0; 364 packet_queue->next_cs_timestamp = 0; 365 packet_queue->instr_count = 0; 366 } 367 368 static ocsd_datapath_resp_t 369 cs_etm_decoder__buffer_packet(struct cs_etm_packet_queue *packet_queue, 370 const u8 trace_chan_id, 371 enum cs_etm_sample_type sample_type) 372 { 373 u32 et = 0; 374 int cpu; 375 376 if (packet_queue->packet_count >= CS_ETM_PACKET_MAX_BUFFER - 1) 377 return OCSD_RESP_FATAL_SYS_ERR; 378 379 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0) 380 return OCSD_RESP_FATAL_SYS_ERR; 381 382 et = packet_queue->tail; 383 et = (et + 1) & (CS_ETM_PACKET_MAX_BUFFER - 1); 384 packet_queue->tail = et; 385 packet_queue->packet_count++; 386 387 packet_queue->packet_buffer[et].sample_type = sample_type; 388 packet_queue->packet_buffer[et].isa = CS_ETM_ISA_UNKNOWN; 389 packet_queue->packet_buffer[et].cpu = cpu; 390 packet_queue->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR; 391 packet_queue->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR; 392 packet_queue->packet_buffer[et].instr_count = 0; 393 packet_queue->packet_buffer[et].last_instr_taken_branch = false; 394 packet_queue->packet_buffer[et].last_instr_size = 0; 395 packet_queue->packet_buffer[et].last_instr_type = 0; 396 packet_queue->packet_buffer[et].last_instr_subtype = 0; 397 packet_queue->packet_buffer[et].last_instr_cond = 0; 398 packet_queue->packet_buffer[et].flags = 0; 399 packet_queue->packet_buffer[et].exception_number = UINT32_MAX; 400 packet_queue->packet_buffer[et].trace_chan_id = trace_chan_id; 401 402 if (packet_queue->packet_count == CS_ETM_PACKET_MAX_BUFFER - 1) 403 return OCSD_RESP_WAIT; 404 405 return OCSD_RESP_CONT; 406 } 407 408 static ocsd_datapath_resp_t 409 cs_etm_decoder__buffer_range(struct cs_etm_queue *etmq, 410 struct cs_etm_packet_queue *packet_queue, 411 const ocsd_generic_trace_elem *elem, 412 const uint8_t trace_chan_id) 413 { 414 int ret = 0; 415 struct cs_etm_packet *packet; 416 417 ret = cs_etm_decoder__buffer_packet(packet_queue, trace_chan_id, 418 CS_ETM_RANGE); 419 if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT) 420 return ret; 421 422 packet = &packet_queue->packet_buffer[packet_queue->tail]; 423 424 switch (elem->isa) { 425 case ocsd_isa_aarch64: 426 packet->isa = CS_ETM_ISA_A64; 427 break; 428 case ocsd_isa_arm: 429 packet->isa = CS_ETM_ISA_A32; 430 break; 431 case ocsd_isa_thumb2: 432 packet->isa = CS_ETM_ISA_T32; 433 break; 434 case ocsd_isa_tee: 435 case ocsd_isa_jazelle: 436 case ocsd_isa_custom: 437 case ocsd_isa_unknown: 438 default: 439 packet->isa = CS_ETM_ISA_UNKNOWN; 440 } 441 442 packet->start_addr = elem->st_addr; 443 packet->end_addr = elem->en_addr; 444 packet->instr_count = elem->num_instr_range; 445 packet->last_instr_type = elem->last_i_type; 446 packet->last_instr_subtype = elem->last_i_subtype; 447 packet->last_instr_cond = elem->last_instr_cond; 448 449 if (elem->last_i_type == OCSD_INSTR_BR || elem->last_i_type == OCSD_INSTR_BR_INDIRECT) 450 packet->last_instr_taken_branch = elem->last_instr_exec; 451 else 452 packet->last_instr_taken_branch = false; 453 454 packet->last_instr_size = elem->last_instr_sz; 455 456 /* per-thread scenario, no need to generate a timestamp */ 457 if (cs_etm__etmq_is_timeless(etmq)) 458 goto out; 459 460 /* 461 * The packet queue is full and we haven't seen a timestamp (had we 462 * seen one the packet queue wouldn't be full). Let the front end 463 * deal with it. 464 */ 465 if (ret == OCSD_RESP_WAIT) 466 goto out; 467 468 packet_queue->instr_count += elem->num_instr_range; 469 /* Tell the front end we have a new timestamp to process */ 470 ret = cs_etm_decoder__do_soft_timestamp(etmq, packet_queue, 471 trace_chan_id); 472 out: 473 return ret; 474 } 475 476 static ocsd_datapath_resp_t 477 cs_etm_decoder__buffer_discontinuity(struct cs_etm_packet_queue *queue, 478 const uint8_t trace_chan_id) 479 { 480 /* 481 * Something happened and who knows when we'll get new traces so 482 * reset time statistics. 483 */ 484 cs_etm_decoder__reset_timestamp(queue); 485 return cs_etm_decoder__buffer_packet(queue, trace_chan_id, 486 CS_ETM_DISCONTINUITY); 487 } 488 489 static ocsd_datapath_resp_t 490 cs_etm_decoder__buffer_exception(struct cs_etm_packet_queue *queue, 491 const ocsd_generic_trace_elem *elem, 492 const uint8_t trace_chan_id) 493 { int ret = 0; 494 struct cs_etm_packet *packet; 495 496 ret = cs_etm_decoder__buffer_packet(queue, trace_chan_id, 497 CS_ETM_EXCEPTION); 498 if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT) 499 return ret; 500 501 packet = &queue->packet_buffer[queue->tail]; 502 packet->exception_number = elem->exception_number; 503 504 return ret; 505 } 506 507 static ocsd_datapath_resp_t 508 cs_etm_decoder__buffer_exception_ret(struct cs_etm_packet_queue *queue, 509 const uint8_t trace_chan_id) 510 { 511 return cs_etm_decoder__buffer_packet(queue, trace_chan_id, 512 CS_ETM_EXCEPTION_RET); 513 } 514 515 static ocsd_datapath_resp_t 516 cs_etm_decoder__set_tid(struct cs_etm_queue *etmq, 517 struct cs_etm_packet_queue *packet_queue, 518 const ocsd_generic_trace_elem *elem, 519 const uint8_t trace_chan_id) 520 { 521 pid_t tid = -1; 522 static u64 pid_fmt; 523 int ret; 524 525 /* 526 * As all the ETMs run at the same exception level, the system should 527 * have the same PID format crossing CPUs. So cache the PID format 528 * and reuse it for sequential decoding. 529 */ 530 if (!pid_fmt) { 531 ret = cs_etm__get_pid_fmt(trace_chan_id, &pid_fmt); 532 if (ret) 533 return OCSD_RESP_FATAL_SYS_ERR; 534 } 535 536 /* 537 * Process the PE_CONTEXT packets if we have a valid contextID or VMID. 538 * If the kernel is running at EL2, the PID is traced in CONTEXTIDR_EL2 539 * as VMID, Bit ETM_OPT_CTXTID2 is set in this case. 540 */ 541 switch (pid_fmt) { 542 case BIT(ETM_OPT_CTXTID): 543 if (elem->context.ctxt_id_valid) 544 tid = elem->context.context_id; 545 break; 546 case BIT(ETM_OPT_CTXTID2): 547 if (elem->context.vmid_valid) 548 tid = elem->context.vmid; 549 break; 550 default: 551 break; 552 } 553 554 if (tid == -1) 555 return OCSD_RESP_CONT; 556 557 if (cs_etm__etmq_set_tid(etmq, tid, trace_chan_id)) 558 return OCSD_RESP_FATAL_SYS_ERR; 559 560 /* 561 * A timestamp is generated after a PE_CONTEXT element so make sure 562 * to rely on that coming one. 563 */ 564 cs_etm_decoder__reset_timestamp(packet_queue); 565 566 return OCSD_RESP_CONT; 567 } 568 569 static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( 570 const void *context, 571 const ocsd_trc_index_t indx, 572 const u8 trace_chan_id __maybe_unused, 573 const ocsd_generic_trace_elem *elem) 574 { 575 ocsd_datapath_resp_t resp = OCSD_RESP_CONT; 576 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context; 577 struct cs_etm_queue *etmq = decoder->data; 578 struct cs_etm_packet_queue *packet_queue; 579 580 /* First get the packet queue for this traceID */ 581 packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id); 582 if (!packet_queue) 583 return OCSD_RESP_FATAL_SYS_ERR; 584 585 switch (elem->elem_type) { 586 case OCSD_GEN_TRC_ELEM_UNKNOWN: 587 break; 588 case OCSD_GEN_TRC_ELEM_EO_TRACE: 589 case OCSD_GEN_TRC_ELEM_NO_SYNC: 590 case OCSD_GEN_TRC_ELEM_TRACE_ON: 591 resp = cs_etm_decoder__buffer_discontinuity(packet_queue, 592 trace_chan_id); 593 break; 594 case OCSD_GEN_TRC_ELEM_INSTR_RANGE: 595 resp = cs_etm_decoder__buffer_range(etmq, packet_queue, elem, 596 trace_chan_id); 597 break; 598 case OCSD_GEN_TRC_ELEM_EXCEPTION: 599 resp = cs_etm_decoder__buffer_exception(packet_queue, elem, 600 trace_chan_id); 601 break; 602 case OCSD_GEN_TRC_ELEM_EXCEPTION_RET: 603 resp = cs_etm_decoder__buffer_exception_ret(packet_queue, 604 trace_chan_id); 605 break; 606 case OCSD_GEN_TRC_ELEM_TIMESTAMP: 607 resp = cs_etm_decoder__do_hard_timestamp(etmq, elem, 608 trace_chan_id, 609 indx); 610 break; 611 case OCSD_GEN_TRC_ELEM_PE_CONTEXT: 612 resp = cs_etm_decoder__set_tid(etmq, packet_queue, 613 elem, trace_chan_id); 614 break; 615 /* Unused packet types */ 616 case OCSD_GEN_TRC_ELEM_I_RANGE_NOPATH: 617 case OCSD_GEN_TRC_ELEM_ADDR_NACC: 618 case OCSD_GEN_TRC_ELEM_CYCLE_COUNT: 619 case OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN: 620 case OCSD_GEN_TRC_ELEM_EVENT: 621 case OCSD_GEN_TRC_ELEM_SWTRACE: 622 case OCSD_GEN_TRC_ELEM_CUSTOM: 623 case OCSD_GEN_TRC_ELEM_SYNC_MARKER: 624 case OCSD_GEN_TRC_ELEM_MEMTRANS: 625 default: 626 break; 627 } 628 629 return resp; 630 } 631 632 static int cs_etm_decoder__create_etm_packet_decoder( 633 struct cs_etm_trace_params *t_params, 634 struct cs_etm_decoder *decoder) 635 { 636 const char *decoder_name; 637 ocsd_etmv3_cfg config_etmv3; 638 ocsd_etmv4_cfg trace_config_etmv4; 639 void *trace_config; 640 u8 csid; 641 642 switch (t_params->protocol) { 643 case CS_ETM_PROTO_ETMV3: 644 case CS_ETM_PROTO_PTM: 645 cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3); 646 decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ? 647 OCSD_BUILTIN_DCD_ETMV3 : 648 OCSD_BUILTIN_DCD_PTM; 649 trace_config = &config_etmv3; 650 break; 651 case CS_ETM_PROTO_ETMV4i: 652 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4); 653 decoder_name = OCSD_BUILTIN_DCD_ETMV4I; 654 trace_config = &trace_config_etmv4; 655 break; 656 default: 657 return -1; 658 } 659 660 if (ocsd_dt_create_decoder(decoder->dcd_tree, 661 decoder_name, 662 OCSD_CREATE_FLG_FULL_DECODER, 663 trace_config, &csid)) 664 return -1; 665 666 if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree, 667 cs_etm_decoder__gen_trace_elem_printer, 668 decoder)) 669 return -1; 670 671 return 0; 672 } 673 674 static int 675 cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params, 676 struct cs_etm_trace_params *t_params, 677 struct cs_etm_decoder *decoder) 678 { 679 if (d_params->operation == CS_ETM_OPERATION_PRINT) 680 return cs_etm_decoder__create_etm_packet_printer(t_params, 681 decoder); 682 else if (d_params->operation == CS_ETM_OPERATION_DECODE) 683 return cs_etm_decoder__create_etm_packet_decoder(t_params, 684 decoder); 685 686 return -1; 687 } 688 689 struct cs_etm_decoder * 690 cs_etm_decoder__new(int decoders, struct cs_etm_decoder_params *d_params, 691 struct cs_etm_trace_params t_params[]) 692 { 693 struct cs_etm_decoder *decoder; 694 ocsd_dcd_tree_src_t format; 695 u32 flags; 696 int i, ret; 697 698 if ((!t_params) || (!d_params)) 699 return NULL; 700 701 decoder = zalloc(sizeof(*decoder)); 702 703 if (!decoder) 704 return NULL; 705 706 decoder->data = d_params->data; 707 decoder->prev_return = OCSD_RESP_CONT; 708 format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED : 709 OCSD_TRC_SRC_SINGLE); 710 flags = 0; 711 flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0); 712 flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0); 713 flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0); 714 715 /* 716 * Drivers may add barrier frames when used with perf, set up to 717 * handle this. Barriers const of FSYNC packet repeated 4 times. 718 */ 719 flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC; 720 721 /* Create decode tree for the data source */ 722 decoder->dcd_tree = ocsd_create_dcd_tree(format, flags); 723 724 if (decoder->dcd_tree == 0) 725 goto err_free_decoder; 726 727 /* init library print logging support */ 728 ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder); 729 if (ret != 0) 730 goto err_free_decoder; 731 732 /* init raw frame logging if required */ 733 cs_etm_decoder__init_raw_frame_logging(d_params, decoder); 734 735 for (i = 0; i < decoders; i++) { 736 ret = cs_etm_decoder__create_etm_decoder(d_params, 737 &t_params[i], 738 decoder); 739 if (ret != 0) 740 goto err_free_decoder; 741 } 742 743 return decoder; 744 745 err_free_decoder: 746 cs_etm_decoder__free(decoder); 747 return NULL; 748 } 749 750 int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder, 751 u64 indx, const u8 *buf, 752 size_t len, size_t *consumed) 753 { 754 int ret = 0; 755 ocsd_datapath_resp_t cur = OCSD_RESP_CONT; 756 ocsd_datapath_resp_t prev_return = decoder->prev_return; 757 size_t processed = 0; 758 u32 count; 759 760 while (processed < len) { 761 if (OCSD_DATA_RESP_IS_WAIT(prev_return)) { 762 cur = ocsd_dt_process_data(decoder->dcd_tree, 763 OCSD_OP_FLUSH, 764 0, 765 0, 766 NULL, 767 NULL); 768 } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) { 769 cur = ocsd_dt_process_data(decoder->dcd_tree, 770 OCSD_OP_DATA, 771 indx + processed, 772 len - processed, 773 &buf[processed], 774 &count); 775 processed += count; 776 } else { 777 ret = -EINVAL; 778 break; 779 } 780 781 /* 782 * Return to the input code if the packet buffer is full. 783 * Flushing will get done once the packet buffer has been 784 * processed. 785 */ 786 if (OCSD_DATA_RESP_IS_WAIT(cur)) 787 break; 788 789 prev_return = cur; 790 } 791 792 decoder->prev_return = cur; 793 *consumed = processed; 794 795 return ret; 796 } 797 798 void cs_etm_decoder__free(struct cs_etm_decoder *decoder) 799 { 800 if (!decoder) 801 return; 802 803 ocsd_destroy_dcd_tree(decoder->dcd_tree); 804 decoder->dcd_tree = NULL; 805 free(decoder); 806 } 807