projects/linux·blob master · refs

809 lines · 23.3 KiB

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