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