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