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