perf cs-etm: Initialise architecture based on TRCIDR1
[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 #define TRCIDR1_TRCARCHMIN_SHIFT 4
130 #define TRCIDR1_TRCARCHMIN_MASK  GENMASK(7, 4)
131 #define TRCIDR1_TRCARCHMIN(x)    (((x) & TRCIDR1_TRCARCHMIN_MASK) >> TRCIDR1_TRCARCHMIN_SHIFT)
132
133 static enum _ocsd_arch_version cs_etm_decoder__get_etmv4_arch_ver(u32 reg_idr1)
134 {
135         /*
136          * For ETMv4 if the trace minor version is 4 or more then we can assume
137          * the architecture is ARCH_AA64 rather than just V8.
138          * ARCH_V8 = V8 architecture
139          * ARCH_AA64 = Min v8r3 plus additional AA64 PE features
140          */
141         return TRCIDR1_TRCARCHMIN(reg_idr1) >= 4 ? ARCH_AA64 : ARCH_V8;
142 }
143
144 static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params,
145                                              ocsd_etmv4_cfg *config)
146 {
147         config->reg_configr = params->etmv4.reg_configr;
148         config->reg_traceidr = params->etmv4.reg_traceidr;
149         config->reg_idr0 = params->etmv4.reg_idr0;
150         config->reg_idr1 = params->etmv4.reg_idr1;
151         config->reg_idr2 = params->etmv4.reg_idr2;
152         config->reg_idr8 = params->etmv4.reg_idr8;
153         config->reg_idr9 = 0;
154         config->reg_idr10 = 0;
155         config->reg_idr11 = 0;
156         config->reg_idr12 = 0;
157         config->reg_idr13 = 0;
158         config->arch_ver = cs_etm_decoder__get_etmv4_arch_ver(params->etmv4.reg_idr1);
159         config->core_prof = profile_CortexA;
160 }
161
162 static void cs_etm_decoder__print_str_cb(const void *p_context,
163                                          const char *msg,
164                                          const int str_len)
165 {
166         const struct cs_etm_decoder *decoder = p_context;
167
168         if (p_context && str_len && !decoder->suppress_printing)
169                 decoder->packet_printer(msg);
170 }
171
172 static int
173 cs_etm_decoder__init_def_logger_printing(struct cs_etm_decoder_params *d_params,
174                                          struct cs_etm_decoder *decoder)
175 {
176         int ret = 0;
177
178         if (d_params->packet_printer == NULL)
179                 return -1;
180
181         decoder->packet_printer = d_params->packet_printer;
182
183         /*
184          * Set up a library default logger to process any printers
185          * (packet/raw frame) we add later.
186          */
187         ret = ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
188         if (ret != 0)
189                 return -1;
190
191         /* no stdout / err / file output */
192         ret = ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
193         if (ret != 0)
194                 return -1;
195
196         /*
197          * Set the string CB for the default logger, passes strings to
198          * perf print logger.
199          */
200         ret = ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
201                                               (void *)decoder,
202                                               cs_etm_decoder__print_str_cb);
203         if (ret != 0)
204                 ret = -1;
205
206         return 0;
207 }
208
209 #ifdef CS_LOG_RAW_FRAMES
210 static void
211 cs_etm_decoder__init_raw_frame_logging(struct cs_etm_decoder_params *d_params,
212                                        struct cs_etm_decoder *decoder)
213 {
214         /* Only log these during a --dump operation */
215         if (d_params->operation == CS_ETM_OPERATION_PRINT) {
216                 /* set up a library default logger to process the
217                  *  raw frame printer we add later
218                  */
219                 ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
220
221                 /* no stdout / err / file output */
222                 ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
223
224                 /* set the string CB for the default logger,
225                  * passes strings to perf print logger.
226                  */
227                 ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
228                                                 (void *)decoder,
229                                                 cs_etm_decoder__print_str_cb);
230
231                 /* use the built in library printer for the raw frames */
232                 ocsd_dt_set_raw_frame_printer(decoder->dcd_tree,
233                                               CS_RAW_DEBUG_FLAGS);
234         }
235 }
236 #else
237 static void
238 cs_etm_decoder__init_raw_frame_logging(
239                 struct cs_etm_decoder_params *d_params __maybe_unused,
240                 struct cs_etm_decoder *decoder __maybe_unused)
241 {
242 }
243 #endif
244
245 static ocsd_datapath_resp_t
246 cs_etm_decoder__do_soft_timestamp(struct cs_etm_queue *etmq,
247                                   struct cs_etm_packet_queue *packet_queue,
248                                   const uint8_t trace_chan_id)
249 {
250         /* No timestamp packet has been received, nothing to do */
251         if (!packet_queue->cs_timestamp)
252                 return OCSD_RESP_CONT;
253
254         packet_queue->cs_timestamp = packet_queue->next_cs_timestamp;
255
256         /* Estimate the timestamp for the next range packet */
257         packet_queue->next_cs_timestamp += packet_queue->instr_count;
258         packet_queue->instr_count = 0;
259
260         /* Tell the front end which traceid_queue needs attention */
261         cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id);
262
263         return OCSD_RESP_WAIT;
264 }
265
266 static ocsd_datapath_resp_t
267 cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq,
268                                   const ocsd_generic_trace_elem *elem,
269                                   const uint8_t trace_chan_id,
270                                   const ocsd_trc_index_t indx)
271 {
272         struct cs_etm_packet_queue *packet_queue;
273
274         /* First get the packet queue for this traceID */
275         packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id);
276         if (!packet_queue)
277                 return OCSD_RESP_FATAL_SYS_ERR;
278
279         /*
280          * We've seen a timestamp packet before - simply record the new value.
281          * Function do_soft_timestamp() will report the value to the front end,
282          * hence asking the decoder to keep decoding rather than stopping.
283          */
284         if (packet_queue->cs_timestamp) {
285                 packet_queue->next_cs_timestamp = elem->timestamp;
286                 return OCSD_RESP_CONT;
287         }
288
289
290         if (!elem->timestamp) {
291                 /*
292                  * Zero timestamps can be seen due to misconfiguration or hardware bugs.
293                  * Warn once, and don't try to subtract instr_count as it would result in an
294                  * underflow.
295                  */
296                 packet_queue->cs_timestamp = 0;
297                 if (!cs_etm__etmq_is_timeless(etmq))
298                         pr_warning_once("Zero Coresight timestamp found at Idx:%" OCSD_TRC_IDX_STR
299                                         ". Decoding may be improved by prepending 'Z' to your current --itrace arguments.\n",
300                                         indx);
301
302         } else if (packet_queue->instr_count > elem->timestamp) {
303                 /*
304                  * Sanity check that the elem->timestamp - packet_queue->instr_count would not
305                  * result in an underflow. Warn and clamp at 0 if it would.
306                  */
307                 packet_queue->cs_timestamp = 0;
308                 pr_err("Timestamp calculation underflow at Idx:%" OCSD_TRC_IDX_STR "\n", indx);
309         } else {
310                 /*
311                  * This is the first timestamp we've seen since the beginning of traces
312                  * or a discontinuity.  Since timestamps packets are generated *after*
313                  * range packets have been generated, we need to estimate the time at
314                  * which instructions started by subtracting the number of instructions
315                  * executed to the timestamp.
316                  */
317                 packet_queue->cs_timestamp = elem->timestamp - packet_queue->instr_count;
318         }
319         packet_queue->next_cs_timestamp = elem->timestamp;
320         packet_queue->instr_count = 0;
321
322         /* Tell the front end which traceid_queue needs attention */
323         cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id);
324
325         /* Halt processing until we are being told to proceed */
326         return OCSD_RESP_WAIT;
327 }
328
329 static void
330 cs_etm_decoder__reset_timestamp(struct cs_etm_packet_queue *packet_queue)
331 {
332         packet_queue->cs_timestamp = 0;
333         packet_queue->next_cs_timestamp = 0;
334         packet_queue->instr_count = 0;
335 }
336
337 static ocsd_datapath_resp_t
338 cs_etm_decoder__buffer_packet(struct cs_etm_packet_queue *packet_queue,
339                               const u8 trace_chan_id,
340                               enum cs_etm_sample_type sample_type)
341 {
342         u32 et = 0;
343         int cpu;
344
345         if (packet_queue->packet_count >= CS_ETM_PACKET_MAX_BUFFER - 1)
346                 return OCSD_RESP_FATAL_SYS_ERR;
347
348         if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
349                 return OCSD_RESP_FATAL_SYS_ERR;
350
351         et = packet_queue->tail;
352         et = (et + 1) & (CS_ETM_PACKET_MAX_BUFFER - 1);
353         packet_queue->tail = et;
354         packet_queue->packet_count++;
355
356         packet_queue->packet_buffer[et].sample_type = sample_type;
357         packet_queue->packet_buffer[et].isa = CS_ETM_ISA_UNKNOWN;
358         packet_queue->packet_buffer[et].cpu = cpu;
359         packet_queue->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR;
360         packet_queue->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR;
361         packet_queue->packet_buffer[et].instr_count = 0;
362         packet_queue->packet_buffer[et].last_instr_taken_branch = false;
363         packet_queue->packet_buffer[et].last_instr_size = 0;
364         packet_queue->packet_buffer[et].last_instr_type = 0;
365         packet_queue->packet_buffer[et].last_instr_subtype = 0;
366         packet_queue->packet_buffer[et].last_instr_cond = 0;
367         packet_queue->packet_buffer[et].flags = 0;
368         packet_queue->packet_buffer[et].exception_number = UINT32_MAX;
369         packet_queue->packet_buffer[et].trace_chan_id = trace_chan_id;
370
371         if (packet_queue->packet_count == CS_ETM_PACKET_MAX_BUFFER - 1)
372                 return OCSD_RESP_WAIT;
373
374         return OCSD_RESP_CONT;
375 }
376
377 static ocsd_datapath_resp_t
378 cs_etm_decoder__buffer_range(struct cs_etm_queue *etmq,
379                              struct cs_etm_packet_queue *packet_queue,
380                              const ocsd_generic_trace_elem *elem,
381                              const uint8_t trace_chan_id)
382 {
383         int ret = 0;
384         struct cs_etm_packet *packet;
385
386         ret = cs_etm_decoder__buffer_packet(packet_queue, trace_chan_id,
387                                             CS_ETM_RANGE);
388         if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
389                 return ret;
390
391         packet = &packet_queue->packet_buffer[packet_queue->tail];
392
393         switch (elem->isa) {
394         case ocsd_isa_aarch64:
395                 packet->isa = CS_ETM_ISA_A64;
396                 break;
397         case ocsd_isa_arm:
398                 packet->isa = CS_ETM_ISA_A32;
399                 break;
400         case ocsd_isa_thumb2:
401                 packet->isa = CS_ETM_ISA_T32;
402                 break;
403         case ocsd_isa_tee:
404         case ocsd_isa_jazelle:
405         case ocsd_isa_custom:
406         case ocsd_isa_unknown:
407         default:
408                 packet->isa = CS_ETM_ISA_UNKNOWN;
409         }
410
411         packet->start_addr = elem->st_addr;
412         packet->end_addr = elem->en_addr;
413         packet->instr_count = elem->num_instr_range;
414         packet->last_instr_type = elem->last_i_type;
415         packet->last_instr_subtype = elem->last_i_subtype;
416         packet->last_instr_cond = elem->last_instr_cond;
417
418         if (elem->last_i_type == OCSD_INSTR_BR || elem->last_i_type == OCSD_INSTR_BR_INDIRECT)
419                 packet->last_instr_taken_branch = elem->last_instr_exec;
420         else
421                 packet->last_instr_taken_branch = false;
422
423         packet->last_instr_size = elem->last_instr_sz;
424
425         /* per-thread scenario, no need to generate a timestamp */
426         if (cs_etm__etmq_is_timeless(etmq))
427                 goto out;
428
429         /*
430          * The packet queue is full and we haven't seen a timestamp (had we
431          * seen one the packet queue wouldn't be full).  Let the front end
432          * deal with it.
433          */
434         if (ret == OCSD_RESP_WAIT)
435                 goto out;
436
437         packet_queue->instr_count += elem->num_instr_range;
438         /* Tell the front end we have a new timestamp to process */
439         ret = cs_etm_decoder__do_soft_timestamp(etmq, packet_queue,
440                                                 trace_chan_id);
441 out:
442         return ret;
443 }
444
445 static ocsd_datapath_resp_t
446 cs_etm_decoder__buffer_discontinuity(struct cs_etm_packet_queue *queue,
447                                      const uint8_t trace_chan_id)
448 {
449         /*
450          * Something happened and who knows when we'll get new traces so
451          * reset time statistics.
452          */
453         cs_etm_decoder__reset_timestamp(queue);
454         return cs_etm_decoder__buffer_packet(queue, trace_chan_id,
455                                              CS_ETM_DISCONTINUITY);
456 }
457
458 static ocsd_datapath_resp_t
459 cs_etm_decoder__buffer_exception(struct cs_etm_packet_queue *queue,
460                                  const ocsd_generic_trace_elem *elem,
461                                  const uint8_t trace_chan_id)
462 {       int ret = 0;
463         struct cs_etm_packet *packet;
464
465         ret = cs_etm_decoder__buffer_packet(queue, trace_chan_id,
466                                             CS_ETM_EXCEPTION);
467         if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
468                 return ret;
469
470         packet = &queue->packet_buffer[queue->tail];
471         packet->exception_number = elem->exception_number;
472
473         return ret;
474 }
475
476 static ocsd_datapath_resp_t
477 cs_etm_decoder__buffer_exception_ret(struct cs_etm_packet_queue *queue,
478                                      const uint8_t trace_chan_id)
479 {
480         return cs_etm_decoder__buffer_packet(queue, trace_chan_id,
481                                              CS_ETM_EXCEPTION_RET);
482 }
483
484 static ocsd_datapath_resp_t
485 cs_etm_decoder__set_tid(struct cs_etm_queue *etmq,
486                         struct cs_etm_packet_queue *packet_queue,
487                         const ocsd_generic_trace_elem *elem,
488                         const uint8_t trace_chan_id)
489 {
490         pid_t tid = -1;
491         static u64 pid_fmt;
492         int ret;
493
494         /*
495          * As all the ETMs run at the same exception level, the system should
496          * have the same PID format crossing CPUs.  So cache the PID format
497          * and reuse it for sequential decoding.
498          */
499         if (!pid_fmt) {
500                 ret = cs_etm__get_pid_fmt(trace_chan_id, &pid_fmt);
501                 if (ret)
502                         return OCSD_RESP_FATAL_SYS_ERR;
503         }
504
505         /*
506          * Process the PE_CONTEXT packets if we have a valid contextID or VMID.
507          * If the kernel is running at EL2, the PID is traced in CONTEXTIDR_EL2
508          * as VMID, Bit ETM_OPT_CTXTID2 is set in this case.
509          */
510         switch (pid_fmt) {
511         case BIT(ETM_OPT_CTXTID):
512                 if (elem->context.ctxt_id_valid)
513                         tid = elem->context.context_id;
514                 break;
515         case BIT(ETM_OPT_CTXTID2):
516                 if (elem->context.vmid_valid)
517                         tid = elem->context.vmid;
518                 break;
519         default:
520                 break;
521         }
522
523         if (tid == -1)
524                 return OCSD_RESP_CONT;
525
526         if (cs_etm__etmq_set_tid(etmq, tid, trace_chan_id))
527                 return OCSD_RESP_FATAL_SYS_ERR;
528
529         /*
530          * A timestamp is generated after a PE_CONTEXT element so make sure
531          * to rely on that coming one.
532          */
533         cs_etm_decoder__reset_timestamp(packet_queue);
534
535         return OCSD_RESP_CONT;
536 }
537
538 static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
539                                 const void *context,
540                                 const ocsd_trc_index_t indx,
541                                 const u8 trace_chan_id __maybe_unused,
542                                 const ocsd_generic_trace_elem *elem)
543 {
544         ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
545         struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
546         struct cs_etm_queue *etmq = decoder->data;
547         struct cs_etm_packet_queue *packet_queue;
548
549         /* First get the packet queue for this traceID */
550         packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id);
551         if (!packet_queue)
552                 return OCSD_RESP_FATAL_SYS_ERR;
553
554         switch (elem->elem_type) {
555         case OCSD_GEN_TRC_ELEM_UNKNOWN:
556                 break;
557         case OCSD_GEN_TRC_ELEM_EO_TRACE:
558         case OCSD_GEN_TRC_ELEM_NO_SYNC:
559         case OCSD_GEN_TRC_ELEM_TRACE_ON:
560                 resp = cs_etm_decoder__buffer_discontinuity(packet_queue,
561                                                             trace_chan_id);
562                 break;
563         case OCSD_GEN_TRC_ELEM_INSTR_RANGE:
564                 resp = cs_etm_decoder__buffer_range(etmq, packet_queue, elem,
565                                                     trace_chan_id);
566                 break;
567         case OCSD_GEN_TRC_ELEM_EXCEPTION:
568                 resp = cs_etm_decoder__buffer_exception(packet_queue, elem,
569                                                         trace_chan_id);
570                 break;
571         case OCSD_GEN_TRC_ELEM_EXCEPTION_RET:
572                 resp = cs_etm_decoder__buffer_exception_ret(packet_queue,
573                                                             trace_chan_id);
574                 break;
575         case OCSD_GEN_TRC_ELEM_TIMESTAMP:
576                 resp = cs_etm_decoder__do_hard_timestamp(etmq, elem,
577                                                          trace_chan_id,
578                                                          indx);
579                 break;
580         case OCSD_GEN_TRC_ELEM_PE_CONTEXT:
581                 resp = cs_etm_decoder__set_tid(etmq, packet_queue,
582                                                elem, trace_chan_id);
583                 break;
584         /* Unused packet types */
585         case OCSD_GEN_TRC_ELEM_I_RANGE_NOPATH:
586         case OCSD_GEN_TRC_ELEM_ADDR_NACC:
587         case OCSD_GEN_TRC_ELEM_CYCLE_COUNT:
588         case OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN:
589         case OCSD_GEN_TRC_ELEM_EVENT:
590         case OCSD_GEN_TRC_ELEM_SWTRACE:
591         case OCSD_GEN_TRC_ELEM_CUSTOM:
592         case OCSD_GEN_TRC_ELEM_SYNC_MARKER:
593         case OCSD_GEN_TRC_ELEM_MEMTRANS:
594         default:
595                 break;
596         }
597
598         return resp;
599 }
600
601 static int
602 cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params,
603                                    struct cs_etm_trace_params *t_params,
604                                    struct cs_etm_decoder *decoder)
605 {
606         const char *decoder_name;
607         ocsd_etmv3_cfg config_etmv3;
608         ocsd_etmv4_cfg trace_config_etmv4;
609         void *trace_config;
610         u8 csid;
611
612         switch (t_params->protocol) {
613         case CS_ETM_PROTO_ETMV3:
614         case CS_ETM_PROTO_PTM:
615                 cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3);
616                 decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ?
617                                                         OCSD_BUILTIN_DCD_ETMV3 :
618                                                         OCSD_BUILTIN_DCD_PTM;
619                 trace_config = &config_etmv3;
620                 break;
621         case CS_ETM_PROTO_ETMV4i:
622                 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
623                 decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
624                 trace_config = &trace_config_etmv4;
625                 break;
626         default:
627                 return -1;
628         }
629
630         if (d_params->operation == CS_ETM_OPERATION_DECODE) {
631                 if (ocsd_dt_create_decoder(decoder->dcd_tree,
632                                            decoder_name,
633                                            OCSD_CREATE_FLG_FULL_DECODER,
634                                            trace_config, &csid))
635                         return -1;
636
637                 if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree,
638                                                cs_etm_decoder__gen_trace_elem_printer,
639                                                decoder))
640                         return -1;
641
642                 return 0;
643         } else if (d_params->operation == CS_ETM_OPERATION_PRINT) {
644                 if (ocsd_dt_create_decoder(decoder->dcd_tree, decoder_name,
645                                            OCSD_CREATE_FLG_PACKET_PROC,
646                                            trace_config, &csid))
647                         return -1;
648
649                 if (ocsd_dt_set_pkt_protocol_printer(decoder->dcd_tree, csid, 0))
650                         return -1;
651
652                 return 0;
653         }
654
655         return -1;
656 }
657
658 struct cs_etm_decoder *
659 cs_etm_decoder__new(int decoders, struct cs_etm_decoder_params *d_params,
660                     struct cs_etm_trace_params t_params[])
661 {
662         struct cs_etm_decoder *decoder;
663         ocsd_dcd_tree_src_t format;
664         u32 flags;
665         int i, ret;
666
667         if ((!t_params) || (!d_params))
668                 return NULL;
669
670         decoder = zalloc(sizeof(*decoder));
671
672         if (!decoder)
673                 return NULL;
674
675         decoder->data = d_params->data;
676         decoder->prev_return = OCSD_RESP_CONT;
677         format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED :
678                                          OCSD_TRC_SRC_SINGLE);
679         flags = 0;
680         flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0);
681         flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0);
682         flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0);
683
684         /*
685          * Drivers may add barrier frames when used with perf, set up to
686          * handle this. Barriers const of FSYNC packet repeated 4 times.
687          */
688         flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC;
689
690         /* Create decode tree for the data source */
691         decoder->dcd_tree = ocsd_create_dcd_tree(format, flags);
692
693         if (decoder->dcd_tree == 0)
694                 goto err_free_decoder;
695
696         /* init library print logging support */
697         ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder);
698         if (ret != 0)
699                 goto err_free_decoder;
700
701         /* init raw frame logging if required */
702         cs_etm_decoder__init_raw_frame_logging(d_params, decoder);
703
704         for (i = 0; i < decoders; i++) {
705                 ret = cs_etm_decoder__create_etm_decoder(d_params,
706                                                          &t_params[i],
707                                                          decoder);
708                 if (ret != 0)
709                         goto err_free_decoder;
710         }
711
712         return decoder;
713
714 err_free_decoder:
715         cs_etm_decoder__free(decoder);
716         return NULL;
717 }
718
719 int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder,
720                                        u64 indx, const u8 *buf,
721                                        size_t len, size_t *consumed)
722 {
723         int ret = 0;
724         ocsd_datapath_resp_t cur = OCSD_RESP_CONT;
725         ocsd_datapath_resp_t prev_return = decoder->prev_return;
726         size_t processed = 0;
727         u32 count;
728
729         while (processed < len) {
730                 if (OCSD_DATA_RESP_IS_WAIT(prev_return)) {
731                         cur = ocsd_dt_process_data(decoder->dcd_tree,
732                                                    OCSD_OP_FLUSH,
733                                                    0,
734                                                    0,
735                                                    NULL,
736                                                    NULL);
737                 } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) {
738                         cur = ocsd_dt_process_data(decoder->dcd_tree,
739                                                    OCSD_OP_DATA,
740                                                    indx + processed,
741                                                    len - processed,
742                                                    &buf[processed],
743                                                    &count);
744                         processed += count;
745                 } else {
746                         ret = -EINVAL;
747                         break;
748                 }
749
750                 /*
751                  * Return to the input code if the packet buffer is full.
752                  * Flushing will get done once the packet buffer has been
753                  * processed.
754                  */
755                 if (OCSD_DATA_RESP_IS_WAIT(cur))
756                         break;
757
758                 prev_return = cur;
759         }
760
761         decoder->prev_return = cur;
762         *consumed = processed;
763
764         return ret;
765 }
766
767 void cs_etm_decoder__free(struct cs_etm_decoder *decoder)
768 {
769         if (!decoder)
770                 return;
771
772         ocsd_destroy_dcd_tree(decoder->dcd_tree);
773         decoder->dcd_tree = NULL;
774         free(decoder);
775 }