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 <linux/zalloc.h> 12 #include <stdlib.h> 13 #include <opencsd/c_api/opencsd_c_api.h> 14 #include <opencsd/etmv4/trc_pkt_types_etmv4.h> 15 #include <opencsd/ocsd_if_types.h> 16 17 #include "cs-etm.h" 18 #include "cs-etm-decoder.h" 19 #include "intlist.h" 20 #include "util.h" 21 22 /* use raw logging */ 23 #ifdef CS_DEBUG_RAW 24 #define CS_LOG_RAW_FRAMES 25 #ifdef CS_RAW_PACKED 26 #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT | \ 27 OCSD_DFRMTR_PACKED_RAW_OUT) 28 #else 29 #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT) 30 #endif 31 #endif 32 33 struct cs_etm_decoder { 34 void *data; 35 void (*packet_printer)(const char *msg); 36 dcd_tree_handle_t dcd_tree; 37 cs_etm_mem_cb_type mem_access; 38 ocsd_datapath_resp_t prev_return; 39 }; 40 41 static u32 42 cs_etm_decoder__mem_access(const void *context, 43 const ocsd_vaddr_t address, 44 const ocsd_mem_space_acc_t mem_space __maybe_unused, 45 const u8 trace_chan_id, 46 const u32 req_size, 47 u8 *buffer) 48 { 49 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context; 50 51 return decoder->mem_access(decoder->data, trace_chan_id, 52 address, req_size, buffer); 53 } 54 55 int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder, 56 u64 start, u64 end, 57 cs_etm_mem_cb_type cb_func) 58 { 59 decoder->mem_access = cb_func; 60 61 if (ocsd_dt_add_callback_trcid_mem_acc(decoder->dcd_tree, start, end, 62 OCSD_MEM_SPACE_ANY, 63 cs_etm_decoder__mem_access, 64 decoder)) 65 return -1; 66 67 return 0; 68 } 69 70 int cs_etm_decoder__reset(struct cs_etm_decoder *decoder) 71 { 72 ocsd_datapath_resp_t dp_ret; 73 74 decoder->prev_return = OCSD_RESP_CONT; 75 76 dp_ret = ocsd_dt_process_data(decoder->dcd_tree, OCSD_OP_RESET, 77 0, 0, NULL, NULL); 78 if (OCSD_DATA_RESP_IS_FATAL(dp_ret)) 79 return -1; 80 81 return 0; 82 } 83 84 int cs_etm_decoder__get_packet(struct cs_etm_packet_queue *packet_queue, 85 struct cs_etm_packet *packet) 86 { 87 if (!packet_queue || !packet) 88 return -EINVAL; 89 90 /* Nothing to do, might as well just return */ 91 if (packet_queue->packet_count == 0) 92 return 0; 93 /* 94 * The queueing process in function cs_etm_decoder__buffer_packet() 95 * increments the tail *before* using it. This is somewhat counter 96 * intuitive but it has the advantage of centralizing tail management 97 * at a single location. Because of that we need to follow the same 98 * heuristic with the head, i.e we increment it before using its 99 * value. Otherwise the first element of the packet queue is not 100 * used. 101 */ 102 packet_queue->head = (packet_queue->head + 1) & 103 (CS_ETM_PACKET_MAX_BUFFER - 1); 104 105 *packet = packet_queue->packet_buffer[packet_queue->head]; 106 107 packet_queue->packet_count--; 108 109 return 1; 110 } 111 112 static int cs_etm_decoder__gen_etmv3_config(struct cs_etm_trace_params *params, 113 ocsd_etmv3_cfg *config) 114 { 115 config->reg_idr = params->etmv3.reg_idr; 116 config->reg_ctrl = params->etmv3.reg_ctrl; 117 config->reg_ccer = params->etmv3.reg_ccer; 118 config->reg_trc_id = params->etmv3.reg_trc_id; 119 config->arch_ver = ARCH_V7; 120 config->core_prof = profile_CortexA; 121 122 return 0; 123 } 124 125 static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params, 126 ocsd_etmv4_cfg *config) 127 { 128 config->reg_configr = params->etmv4.reg_configr; 129 config->reg_traceidr = params->etmv4.reg_traceidr; 130 config->reg_idr0 = params->etmv4.reg_idr0; 131 config->reg_idr1 = params->etmv4.reg_idr1; 132 config->reg_idr2 = params->etmv4.reg_idr2; 133 config->reg_idr8 = params->etmv4.reg_idr8; 134 config->reg_idr9 = 0; 135 config->reg_idr10 = 0; 136 config->reg_idr11 = 0; 137 config->reg_idr12 = 0; 138 config->reg_idr13 = 0; 139 config->arch_ver = ARCH_V8; 140 config->core_prof = profile_CortexA; 141 } 142 143 static void cs_etm_decoder__print_str_cb(const void *p_context, 144 const char *msg, 145 const int str_len) 146 { 147 if (p_context && str_len) 148 ((struct cs_etm_decoder *)p_context)->packet_printer(msg); 149 } 150 151 static int 152 cs_etm_decoder__init_def_logger_printing(struct cs_etm_decoder_params *d_params, 153 struct cs_etm_decoder *decoder) 154 { 155 int ret = 0; 156 157 if (d_params->packet_printer == NULL) 158 return -1; 159 160 decoder->packet_printer = d_params->packet_printer; 161 162 /* 163 * Set up a library default logger to process any printers 164 * (packet/raw frame) we add later. 165 */ 166 ret = ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1); 167 if (ret != 0) 168 return -1; 169 170 /* no stdout / err / file output */ 171 ret = ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL); 172 if (ret != 0) 173 return -1; 174 175 /* 176 * Set the string CB for the default logger, passes strings to 177 * perf print logger. 178 */ 179 ret = ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree, 180 (void *)decoder, 181 cs_etm_decoder__print_str_cb); 182 if (ret != 0) 183 ret = -1; 184 185 return 0; 186 } 187 188 #ifdef CS_LOG_RAW_FRAMES 189 static void 190 cs_etm_decoder__init_raw_frame_logging(struct cs_etm_decoder_params *d_params, 191 struct cs_etm_decoder *decoder) 192 { 193 /* Only log these during a --dump operation */ 194 if (d_params->operation == CS_ETM_OPERATION_PRINT) { 195 /* set up a library default logger to process the 196 * raw frame printer we add later 197 */ 198 ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1); 199 200 /* no stdout / err / file output */ 201 ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL); 202 203 /* set the string CB for the default logger, 204 * passes strings to perf print logger. 205 */ 206 ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree, 207 (void *)decoder, 208 cs_etm_decoder__print_str_cb); 209 210 /* use the built in library printer for the raw frames */ 211 ocsd_dt_set_raw_frame_printer(decoder->dcd_tree, 212 CS_RAW_DEBUG_FLAGS); 213 } 214 } 215 #else 216 static void 217 cs_etm_decoder__init_raw_frame_logging( 218 struct cs_etm_decoder_params *d_params __maybe_unused, 219 struct cs_etm_decoder *decoder __maybe_unused) 220 { 221 } 222 #endif 223 224 static int cs_etm_decoder__create_packet_printer(struct cs_etm_decoder *decoder, 225 const char *decoder_name, 226 void *trace_config) 227 { 228 u8 csid; 229 230 if (ocsd_dt_create_decoder(decoder->dcd_tree, decoder_name, 231 OCSD_CREATE_FLG_PACKET_PROC, 232 trace_config, &csid)) 233 return -1; 234 235 if (ocsd_dt_set_pkt_protocol_printer(decoder->dcd_tree, csid, 0)) 236 return -1; 237 238 return 0; 239 } 240 241 static int 242 cs_etm_decoder__create_etm_packet_printer(struct cs_etm_trace_params *t_params, 243 struct cs_etm_decoder *decoder) 244 { 245 const char *decoder_name; 246 ocsd_etmv3_cfg config_etmv3; 247 ocsd_etmv4_cfg trace_config_etmv4; 248 void *trace_config; 249 250 switch (t_params->protocol) { 251 case CS_ETM_PROTO_ETMV3: 252 case CS_ETM_PROTO_PTM: 253 cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3); 254 decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ? 255 OCSD_BUILTIN_DCD_ETMV3 : 256 OCSD_BUILTIN_DCD_PTM; 257 trace_config = &config_etmv3; 258 break; 259 case CS_ETM_PROTO_ETMV4i: 260 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4); 261 decoder_name = OCSD_BUILTIN_DCD_ETMV4I; 262 trace_config = &trace_config_etmv4; 263 break; 264 default: 265 return -1; 266 } 267 268 return cs_etm_decoder__create_packet_printer(decoder, 269 decoder_name, 270 trace_config); 271 } 272 273 static ocsd_datapath_resp_t 274 cs_etm_decoder__do_soft_timestamp(struct cs_etm_queue *etmq, 275 struct cs_etm_packet_queue *packet_queue, 276 const uint8_t trace_chan_id) 277 { 278 /* No timestamp packet has been received, nothing to do */ 279 if (!packet_queue->timestamp) 280 return OCSD_RESP_CONT; 281 282 packet_queue->timestamp = packet_queue->next_timestamp; 283 284 /* Estimate the timestamp for the next range packet */ 285 packet_queue->next_timestamp += packet_queue->instr_count; 286 packet_queue->instr_count = 0; 287 288 /* Tell the front end which traceid_queue needs attention */ 289 cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id); 290 291 return OCSD_RESP_WAIT; 292 } 293 294 static ocsd_datapath_resp_t 295 cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq, 296 const ocsd_generic_trace_elem *elem, 297 const uint8_t trace_chan_id) 298 { 299 struct cs_etm_packet_queue *packet_queue; 300 301 /* First get the packet queue for this traceID */ 302 packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id); 303 if (!packet_queue) 304 return OCSD_RESP_FATAL_SYS_ERR; 305 306 /* 307 * We've seen a timestamp packet before - simply record the new value. 308 * Function do_soft_timestamp() will report the value to the front end, 309 * hence asking the decoder to keep decoding rather than stopping. 310 */ 311 if (packet_queue->timestamp) { 312 packet_queue->next_timestamp = elem->timestamp; 313 return OCSD_RESP_CONT; 314 } 315 316 /* 317 * This is the first timestamp we've seen since the beginning of traces 318 * or a discontinuity. Since timestamps packets are generated *after* 319 * range packets have been generated, we need to estimate the time at 320 * which instructions started by substracting the number of instructions 321 * executed to the timestamp. 322 */ 323 packet_queue->timestamp = elem->timestamp - packet_queue->instr_count; 324 packet_queue->next_timestamp = elem->timestamp; 325 packet_queue->instr_count = 0; 326 327 /* Tell the front end which traceid_queue needs attention */ 328 cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id); 329 330 /* Halt processing until we are being told to proceed */ 331 return OCSD_RESP_WAIT; 332 } 333 334 static void 335 cs_etm_decoder__reset_timestamp(struct cs_etm_packet_queue *packet_queue) 336 { 337 packet_queue->timestamp = 0; 338 packet_queue->next_timestamp = 0; 339 packet_queue->instr_count = 0; 340 } 341 342 static ocsd_datapath_resp_t 343 cs_etm_decoder__buffer_packet(struct cs_etm_packet_queue *packet_queue, 344 const u8 trace_chan_id, 345 enum cs_etm_sample_type sample_type) 346 { 347 u32 et = 0; 348 int cpu; 349 350 if (packet_queue->packet_count >= CS_ETM_PACKET_MAX_BUFFER - 1) 351 return OCSD_RESP_FATAL_SYS_ERR; 352 353 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0) 354 return OCSD_RESP_FATAL_SYS_ERR; 355 356 et = packet_queue->tail; 357 et = (et + 1) & (CS_ETM_PACKET_MAX_BUFFER - 1); 358 packet_queue->tail = et; 359 packet_queue->packet_count++; 360 361 packet_queue->packet_buffer[et].sample_type = sample_type; 362 packet_queue->packet_buffer[et].isa = CS_ETM_ISA_UNKNOWN; 363 packet_queue->packet_buffer[et].cpu = cpu; 364 packet_queue->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR; 365 packet_queue->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR; 366 packet_queue->packet_buffer[et].instr_count = 0; 367 packet_queue->packet_buffer[et].last_instr_taken_branch = false; 368 packet_queue->packet_buffer[et].last_instr_size = 0; 369 packet_queue->packet_buffer[et].last_instr_type = 0; 370 packet_queue->packet_buffer[et].last_instr_subtype = 0; 371 packet_queue->packet_buffer[et].last_instr_cond = 0; 372 packet_queue->packet_buffer[et].flags = 0; 373 packet_queue->packet_buffer[et].exception_number = UINT32_MAX; 374 packet_queue->packet_buffer[et].trace_chan_id = trace_chan_id; 375 376 if (packet_queue->packet_count == CS_ETM_PACKET_MAX_BUFFER - 1) 377 return OCSD_RESP_WAIT; 378 379 return OCSD_RESP_CONT; 380 } 381 382 static ocsd_datapath_resp_t 383 cs_etm_decoder__buffer_range(struct cs_etm_queue *etmq, 384 struct cs_etm_packet_queue *packet_queue, 385 const ocsd_generic_trace_elem *elem, 386 const uint8_t trace_chan_id) 387 { 388 int ret = 0; 389 struct cs_etm_packet *packet; 390 391 ret = cs_etm_decoder__buffer_packet(packet_queue, trace_chan_id, 392 CS_ETM_RANGE); 393 if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT) 394 return ret; 395 396 packet = &packet_queue->packet_buffer[packet_queue->tail]; 397 398 switch (elem->isa) { 399 case ocsd_isa_aarch64: 400 packet->isa = CS_ETM_ISA_A64; 401 break; 402 case ocsd_isa_arm: 403 packet->isa = CS_ETM_ISA_A32; 404 break; 405 case ocsd_isa_thumb2: 406 packet->isa = CS_ETM_ISA_T32; 407 break; 408 case ocsd_isa_tee: 409 case ocsd_isa_jazelle: 410 case ocsd_isa_custom: 411 case ocsd_isa_unknown: 412 default: 413 packet->isa = CS_ETM_ISA_UNKNOWN; 414 } 415 416 packet->start_addr = elem->st_addr; 417 packet->end_addr = elem->en_addr; 418 packet->instr_count = elem->num_instr_range; 419 packet->last_instr_type = elem->last_i_type; 420 packet->last_instr_subtype = elem->last_i_subtype; 421 packet->last_instr_cond = elem->last_instr_cond; 422 423 switch (elem->last_i_type) { 424 case OCSD_INSTR_BR: 425 case OCSD_INSTR_BR_INDIRECT: 426 packet->last_instr_taken_branch = elem->last_instr_exec; 427 break; 428 case OCSD_INSTR_ISB: 429 case OCSD_INSTR_DSB_DMB: 430 case OCSD_INSTR_WFI_WFE: 431 case OCSD_INSTR_OTHER: 432 default: 433 packet->last_instr_taken_branch = false; 434 break; 435 } 436 437 packet->last_instr_size = elem->last_instr_sz; 438 439 /* per-thread scenario, no need to generate a timestamp */ 440 if (cs_etm__etmq_is_timeless(etmq)) 441 goto out; 442 443 /* 444 * The packet queue is full and we haven't seen a timestamp (had we 445 * seen one the packet queue wouldn't be full). Let the front end 446 * deal with it. 447 */ 448 if (ret == OCSD_RESP_WAIT) 449 goto out; 450 451 packet_queue->instr_count += elem->num_instr_range; 452 /* Tell the front end we have a new timestamp to process */ 453 ret = cs_etm_decoder__do_soft_timestamp(etmq, packet_queue, 454 trace_chan_id); 455 out: 456 return ret; 457 } 458 459 static ocsd_datapath_resp_t 460 cs_etm_decoder__buffer_discontinuity(struct cs_etm_packet_queue *queue, 461 const uint8_t trace_chan_id) 462 { 463 /* 464 * Something happened and who knows when we'll get new traces so 465 * reset time statistics. 466 */ 467 cs_etm_decoder__reset_timestamp(queue); 468 return cs_etm_decoder__buffer_packet(queue, trace_chan_id, 469 CS_ETM_DISCONTINUITY); 470 } 471 472 static ocsd_datapath_resp_t 473 cs_etm_decoder__buffer_exception(struct cs_etm_packet_queue *queue, 474 const ocsd_generic_trace_elem *elem, 475 const uint8_t trace_chan_id) 476 { int ret = 0; 477 struct cs_etm_packet *packet; 478 479 ret = cs_etm_decoder__buffer_packet(queue, trace_chan_id, 480 CS_ETM_EXCEPTION); 481 if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT) 482 return ret; 483 484 packet = &queue->packet_buffer[queue->tail]; 485 packet->exception_number = elem->exception_number; 486 487 return ret; 488 } 489 490 static ocsd_datapath_resp_t 491 cs_etm_decoder__buffer_exception_ret(struct cs_etm_packet_queue *queue, 492 const uint8_t trace_chan_id) 493 { 494 return cs_etm_decoder__buffer_packet(queue, trace_chan_id, 495 CS_ETM_EXCEPTION_RET); 496 } 497 498 static ocsd_datapath_resp_t 499 cs_etm_decoder__set_tid(struct cs_etm_queue *etmq, 500 struct cs_etm_packet_queue *packet_queue, 501 const ocsd_generic_trace_elem *elem, 502 const uint8_t trace_chan_id) 503 { 504 pid_t tid; 505 506 /* Ignore PE_CONTEXT packets that don't have a valid contextID */ 507 if (!elem->context.ctxt_id_valid) 508 return OCSD_RESP_CONT; 509 510 tid = elem->context.context_id; 511 if (cs_etm__etmq_set_tid(etmq, tid, trace_chan_id)) 512 return OCSD_RESP_FATAL_SYS_ERR; 513 514 /* 515 * A timestamp is generated after a PE_CONTEXT element so make sure 516 * to rely on that coming one. 517 */ 518 cs_etm_decoder__reset_timestamp(packet_queue); 519 520 return OCSD_RESP_CONT; 521 } 522 523 static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( 524 const void *context, 525 const ocsd_trc_index_t indx __maybe_unused, 526 const u8 trace_chan_id __maybe_unused, 527 const ocsd_generic_trace_elem *elem) 528 { 529 ocsd_datapath_resp_t resp = OCSD_RESP_CONT; 530 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context; 531 struct cs_etm_queue *etmq = decoder->data; 532 struct cs_etm_packet_queue *packet_queue; 533 534 /* First get the packet queue for this traceID */ 535 packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id); 536 if (!packet_queue) 537 return OCSD_RESP_FATAL_SYS_ERR; 538 539 switch (elem->elem_type) { 540 case OCSD_GEN_TRC_ELEM_UNKNOWN: 541 break; 542 case OCSD_GEN_TRC_ELEM_EO_TRACE: 543 case OCSD_GEN_TRC_ELEM_NO_SYNC: 544 case OCSD_GEN_TRC_ELEM_TRACE_ON: 545 resp = cs_etm_decoder__buffer_discontinuity(packet_queue, 546 trace_chan_id); 547 break; 548 case OCSD_GEN_TRC_ELEM_INSTR_RANGE: 549 resp = cs_etm_decoder__buffer_range(etmq, packet_queue, elem, 550 trace_chan_id); 551 break; 552 case OCSD_GEN_TRC_ELEM_EXCEPTION: 553 resp = cs_etm_decoder__buffer_exception(packet_queue, elem, 554 trace_chan_id); 555 break; 556 case OCSD_GEN_TRC_ELEM_EXCEPTION_RET: 557 resp = cs_etm_decoder__buffer_exception_ret(packet_queue, 558 trace_chan_id); 559 break; 560 case OCSD_GEN_TRC_ELEM_TIMESTAMP: 561 resp = cs_etm_decoder__do_hard_timestamp(etmq, elem, 562 trace_chan_id); 563 break; 564 case OCSD_GEN_TRC_ELEM_PE_CONTEXT: 565 resp = cs_etm_decoder__set_tid(etmq, packet_queue, 566 elem, trace_chan_id); 567 break; 568 case OCSD_GEN_TRC_ELEM_ADDR_NACC: 569 case OCSD_GEN_TRC_ELEM_CYCLE_COUNT: 570 case OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN: 571 case OCSD_GEN_TRC_ELEM_EVENT: 572 case OCSD_GEN_TRC_ELEM_SWTRACE: 573 case OCSD_GEN_TRC_ELEM_CUSTOM: 574 default: 575 break; 576 } 577 578 return resp; 579 } 580 581 static int cs_etm_decoder__create_etm_packet_decoder( 582 struct cs_etm_trace_params *t_params, 583 struct cs_etm_decoder *decoder) 584 { 585 const char *decoder_name; 586 ocsd_etmv3_cfg config_etmv3; 587 ocsd_etmv4_cfg trace_config_etmv4; 588 void *trace_config; 589 u8 csid; 590 591 switch (t_params->protocol) { 592 case CS_ETM_PROTO_ETMV3: 593 case CS_ETM_PROTO_PTM: 594 cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3); 595 decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ? 596 OCSD_BUILTIN_DCD_ETMV3 : 597 OCSD_BUILTIN_DCD_PTM; 598 trace_config = &config_etmv3; 599 break; 600 case CS_ETM_PROTO_ETMV4i: 601 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4); 602 decoder_name = OCSD_BUILTIN_DCD_ETMV4I; 603 trace_config = &trace_config_etmv4; 604 break; 605 default: 606 return -1; 607 } 608 609 if (ocsd_dt_create_decoder(decoder->dcd_tree, 610 decoder_name, 611 OCSD_CREATE_FLG_FULL_DECODER, 612 trace_config, &csid)) 613 return -1; 614 615 if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree, 616 cs_etm_decoder__gen_trace_elem_printer, 617 decoder)) 618 return -1; 619 620 return 0; 621 } 622 623 static int 624 cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params, 625 struct cs_etm_trace_params *t_params, 626 struct cs_etm_decoder *decoder) 627 { 628 if (d_params->operation == CS_ETM_OPERATION_PRINT) 629 return cs_etm_decoder__create_etm_packet_printer(t_params, 630 decoder); 631 else if (d_params->operation == CS_ETM_OPERATION_DECODE) 632 return cs_etm_decoder__create_etm_packet_decoder(t_params, 633 decoder); 634 635 return -1; 636 } 637 638 struct cs_etm_decoder * 639 cs_etm_decoder__new(int num_cpu, struct cs_etm_decoder_params *d_params, 640 struct cs_etm_trace_params t_params[]) 641 { 642 struct cs_etm_decoder *decoder; 643 ocsd_dcd_tree_src_t format; 644 u32 flags; 645 int i, ret; 646 647 if ((!t_params) || (!d_params)) 648 return NULL; 649 650 decoder = zalloc(sizeof(*decoder)); 651 652 if (!decoder) 653 return NULL; 654 655 decoder->data = d_params->data; 656 decoder->prev_return = OCSD_RESP_CONT; 657 format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED : 658 OCSD_TRC_SRC_SINGLE); 659 flags = 0; 660 flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0); 661 flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0); 662 flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0); 663 664 /* 665 * Drivers may add barrier frames when used with perf, set up to 666 * handle this. Barriers const of FSYNC packet repeated 4 times. 667 */ 668 flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC; 669 670 /* Create decode tree for the data source */ 671 decoder->dcd_tree = ocsd_create_dcd_tree(format, flags); 672 673 if (decoder->dcd_tree == 0) 674 goto err_free_decoder; 675 676 /* init library print logging support */ 677 ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder); 678 if (ret != 0) 679 goto err_free_decoder; 680 681 /* init raw frame logging if required */ 682 cs_etm_decoder__init_raw_frame_logging(d_params, decoder); 683 684 for (i = 0; i < num_cpu; i++) { 685 ret = cs_etm_decoder__create_etm_decoder(d_params, 686 &t_params[i], 687 decoder); 688 if (ret != 0) 689 goto err_free_decoder; 690 } 691 692 return decoder; 693 694 err_free_decoder: 695 cs_etm_decoder__free(decoder); 696 return NULL; 697 } 698 699 int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder, 700 u64 indx, const u8 *buf, 701 size_t len, size_t *consumed) 702 { 703 int ret = 0; 704 ocsd_datapath_resp_t cur = OCSD_RESP_CONT; 705 ocsd_datapath_resp_t prev_return = decoder->prev_return; 706 size_t processed = 0; 707 u32 count; 708 709 while (processed < len) { 710 if (OCSD_DATA_RESP_IS_WAIT(prev_return)) { 711 cur = ocsd_dt_process_data(decoder->dcd_tree, 712 OCSD_OP_FLUSH, 713 0, 714 0, 715 NULL, 716 NULL); 717 } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) { 718 cur = ocsd_dt_process_data(decoder->dcd_tree, 719 OCSD_OP_DATA, 720 indx + processed, 721 len - processed, 722 &buf[processed], 723 &count); 724 processed += count; 725 } else { 726 ret = -EINVAL; 727 break; 728 } 729 730 /* 731 * Return to the input code if the packet buffer is full. 732 * Flushing will get done once the packet buffer has been 733 * processed. 734 */ 735 if (OCSD_DATA_RESP_IS_WAIT(cur)) 736 break; 737 738 prev_return = cur; 739 } 740 741 decoder->prev_return = cur; 742 *consumed = processed; 743 744 return ret; 745 } 746 747 void cs_etm_decoder__free(struct cs_etm_decoder *decoder) 748 { 749 if (!decoder) 750 return; 751 752 ocsd_destroy_dcd_tree(decoder->dcd_tree); 753 decoder->dcd_tree = NULL; 754 free(decoder); 755 } 756