Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright(C) 2015-2018 Linaro Limited.
0004  *
0005  * Author: Tor Jeremiassen <tor@ti.com>
0006  * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
0007  */
0008 
0009 #include <asm/bug.h>
0010 #include <linux/coresight-pmu.h>
0011 #include <linux/err.h>
0012 #include <linux/list.h>
0013 #include <linux/zalloc.h>
0014 #include <stdlib.h>
0015 #include <opencsd/c_api/opencsd_c_api.h>
0016 
0017 #include "cs-etm.h"
0018 #include "cs-etm-decoder.h"
0019 #include "debug.h"
0020 #include "intlist.h"
0021 
0022 /* use raw logging */
0023 #ifdef CS_DEBUG_RAW
0024 #define CS_LOG_RAW_FRAMES
0025 #ifdef CS_RAW_PACKED
0026 #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT | \
0027                 OCSD_DFRMTR_PACKED_RAW_OUT)
0028 #else
0029 #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT)
0030 #endif
0031 #endif
0032 
0033 struct cs_etm_decoder {
0034     void *data;
0035     void (*packet_printer)(const char *msg);
0036     bool suppress_printing;
0037     dcd_tree_handle_t dcd_tree;
0038     cs_etm_mem_cb_type mem_access;
0039     ocsd_datapath_resp_t prev_return;
0040     const char *decoder_name;
0041 };
0042 
0043 static u32
0044 cs_etm_decoder__mem_access(const void *context,
0045                const ocsd_vaddr_t address,
0046                const ocsd_mem_space_acc_t mem_space __maybe_unused,
0047                const u8 trace_chan_id,
0048                const u32 req_size,
0049                u8 *buffer)
0050 {
0051     struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
0052 
0053     return decoder->mem_access(decoder->data, trace_chan_id,
0054                    address, req_size, buffer);
0055 }
0056 
0057 int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder,
0058                       u64 start, u64 end,
0059                       cs_etm_mem_cb_type cb_func)
0060 {
0061     decoder->mem_access = cb_func;
0062 
0063     if (ocsd_dt_add_callback_trcid_mem_acc(decoder->dcd_tree, start, end,
0064                            OCSD_MEM_SPACE_ANY,
0065                            cs_etm_decoder__mem_access,
0066                            decoder))
0067         return -1;
0068 
0069     return 0;
0070 }
0071 
0072 int cs_etm_decoder__reset(struct cs_etm_decoder *decoder)
0073 {
0074     ocsd_datapath_resp_t dp_ret;
0075 
0076     decoder->prev_return = OCSD_RESP_CONT;
0077     decoder->suppress_printing = true;
0078     dp_ret = ocsd_dt_process_data(decoder->dcd_tree, OCSD_OP_RESET,
0079                       0, 0, NULL, NULL);
0080     decoder->suppress_printing = false;
0081     if (OCSD_DATA_RESP_IS_FATAL(dp_ret))
0082         return -1;
0083 
0084     return 0;
0085 }
0086 
0087 int cs_etm_decoder__get_packet(struct cs_etm_packet_queue *packet_queue,
0088                    struct cs_etm_packet *packet)
0089 {
0090     if (!packet_queue || !packet)
0091         return -EINVAL;
0092 
0093     /* Nothing to do, might as well just return */
0094     if (packet_queue->packet_count == 0)
0095         return 0;
0096     /*
0097      * The queueing process in function cs_etm_decoder__buffer_packet()
0098      * increments the tail *before* using it.  This is somewhat counter
0099      * intuitive but it has the advantage of centralizing tail management
0100      * at a single location.  Because of that we need to follow the same
0101      * heuristic with the head, i.e we increment it before using its
0102      * value.  Otherwise the first element of the packet queue is not
0103      * used.
0104      */
0105     packet_queue->head = (packet_queue->head + 1) &
0106                  (CS_ETM_PACKET_MAX_BUFFER - 1);
0107 
0108     *packet = packet_queue->packet_buffer[packet_queue->head];
0109 
0110     packet_queue->packet_count--;
0111 
0112     return 1;
0113 }
0114 
0115 static int cs_etm_decoder__gen_etmv3_config(struct cs_etm_trace_params *params,
0116                         ocsd_etmv3_cfg *config)
0117 {
0118     config->reg_idr = params->etmv3.reg_idr;
0119     config->reg_ctrl = params->etmv3.reg_ctrl;
0120     config->reg_ccer = params->etmv3.reg_ccer;
0121     config->reg_trc_id = params->etmv3.reg_trc_id;
0122     config->arch_ver = ARCH_V7;
0123     config->core_prof = profile_CortexA;
0124 
0125     return 0;
0126 }
0127 
0128 #define TRCIDR1_TRCARCHMIN_SHIFT 4
0129 #define TRCIDR1_TRCARCHMIN_MASK  GENMASK(7, 4)
0130 #define TRCIDR1_TRCARCHMIN(x)    (((x) & TRCIDR1_TRCARCHMIN_MASK) >> TRCIDR1_TRCARCHMIN_SHIFT)
0131 
0132 static enum _ocsd_arch_version cs_etm_decoder__get_etmv4_arch_ver(u32 reg_idr1)
0133 {
0134     /*
0135      * For ETMv4 if the trace minor version is 4 or more then we can assume
0136      * the architecture is ARCH_AA64 rather than just V8.
0137      * ARCH_V8 = V8 architecture
0138      * ARCH_AA64 = Min v8r3 plus additional AA64 PE features
0139      */
0140     return TRCIDR1_TRCARCHMIN(reg_idr1) >= 4 ? ARCH_AA64 : ARCH_V8;
0141 }
0142 
0143 static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params,
0144                          ocsd_etmv4_cfg *config)
0145 {
0146     config->reg_configr = params->etmv4.reg_configr;
0147     config->reg_traceidr = params->etmv4.reg_traceidr;
0148     config->reg_idr0 = params->etmv4.reg_idr0;
0149     config->reg_idr1 = params->etmv4.reg_idr1;
0150     config->reg_idr2 = params->etmv4.reg_idr2;
0151     config->reg_idr8 = params->etmv4.reg_idr8;
0152     config->reg_idr9 = 0;
0153     config->reg_idr10 = 0;
0154     config->reg_idr11 = 0;
0155     config->reg_idr12 = 0;
0156     config->reg_idr13 = 0;
0157     config->arch_ver = cs_etm_decoder__get_etmv4_arch_ver(params->etmv4.reg_idr1);
0158     config->core_prof = profile_CortexA;
0159 }
0160 
0161 static void cs_etm_decoder__gen_ete_config(struct cs_etm_trace_params *params,
0162                        ocsd_ete_cfg *config)
0163 {
0164     config->reg_configr = params->ete.reg_configr;
0165     config->reg_traceidr = params->ete.reg_traceidr;
0166     config->reg_idr0 = params->ete.reg_idr0;
0167     config->reg_idr1 = params->ete.reg_idr1;
0168     config->reg_idr2 = params->ete.reg_idr2;
0169     config->reg_idr8 = params->ete.reg_idr8;
0170     config->reg_devarch = params->ete.reg_devarch;
0171     config->arch_ver = ARCH_AA64;
0172     config->core_prof = profile_CortexA;
0173 }
0174 
0175 static void cs_etm_decoder__print_str_cb(const void *p_context,
0176                      const char *msg,
0177                      const int str_len)
0178 {
0179     const struct cs_etm_decoder *decoder = p_context;
0180 
0181     if (p_context && str_len && !decoder->suppress_printing)
0182         decoder->packet_printer(msg);
0183 }
0184 
0185 static int
0186 cs_etm_decoder__init_def_logger_printing(struct cs_etm_decoder_params *d_params,
0187                      struct cs_etm_decoder *decoder)
0188 {
0189     int ret = 0;
0190 
0191     if (d_params->packet_printer == NULL)
0192         return -1;
0193 
0194     decoder->packet_printer = d_params->packet_printer;
0195 
0196     /*
0197      * Set up a library default logger to process any printers
0198      * (packet/raw frame) we add later.
0199      */
0200     ret = ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
0201     if (ret != 0)
0202         return -1;
0203 
0204     /* no stdout / err / file output */
0205     ret = ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
0206     if (ret != 0)
0207         return -1;
0208 
0209     /*
0210      * Set the string CB for the default logger, passes strings to
0211      * perf print logger.
0212      */
0213     ret = ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
0214                           (void *)decoder,
0215                           cs_etm_decoder__print_str_cb);
0216     if (ret != 0)
0217         ret = -1;
0218 
0219     return 0;
0220 }
0221 
0222 #ifdef CS_LOG_RAW_FRAMES
0223 static void
0224 cs_etm_decoder__init_raw_frame_logging(struct cs_etm_decoder_params *d_params,
0225                        struct cs_etm_decoder *decoder)
0226 {
0227     /* Only log these during a --dump operation */
0228     if (d_params->operation == CS_ETM_OPERATION_PRINT) {
0229         /* set up a library default logger to process the
0230          *  raw frame printer we add later
0231          */
0232         ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
0233 
0234         /* no stdout / err / file output */
0235         ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
0236 
0237         /* set the string CB for the default logger,
0238          * passes strings to perf print logger.
0239          */
0240         ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
0241                         (void *)decoder,
0242                         cs_etm_decoder__print_str_cb);
0243 
0244         /* use the built in library printer for the raw frames */
0245         ocsd_dt_set_raw_frame_printer(decoder->dcd_tree,
0246                           CS_RAW_DEBUG_FLAGS);
0247     }
0248 }
0249 #else
0250 static void
0251 cs_etm_decoder__init_raw_frame_logging(
0252         struct cs_etm_decoder_params *d_params __maybe_unused,
0253         struct cs_etm_decoder *decoder __maybe_unused)
0254 {
0255 }
0256 #endif
0257 
0258 static ocsd_datapath_resp_t
0259 cs_etm_decoder__do_soft_timestamp(struct cs_etm_queue *etmq,
0260                   struct cs_etm_packet_queue *packet_queue,
0261                   const uint8_t trace_chan_id)
0262 {
0263     /* No timestamp packet has been received, nothing to do */
0264     if (!packet_queue->cs_timestamp)
0265         return OCSD_RESP_CONT;
0266 
0267     packet_queue->cs_timestamp = packet_queue->next_cs_timestamp;
0268 
0269     /* Estimate the timestamp for the next range packet */
0270     packet_queue->next_cs_timestamp += packet_queue->instr_count;
0271     packet_queue->instr_count = 0;
0272 
0273     /* Tell the front end which traceid_queue needs attention */
0274     cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id);
0275 
0276     return OCSD_RESP_WAIT;
0277 }
0278 
0279 static ocsd_datapath_resp_t
0280 cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq,
0281                   const ocsd_generic_trace_elem *elem,
0282                   const uint8_t trace_chan_id,
0283                   const ocsd_trc_index_t indx)
0284 {
0285     struct cs_etm_packet_queue *packet_queue;
0286 
0287     /* First get the packet queue for this traceID */
0288     packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id);
0289     if (!packet_queue)
0290         return OCSD_RESP_FATAL_SYS_ERR;
0291 
0292     /*
0293      * We've seen a timestamp packet before - simply record the new value.
0294      * Function do_soft_timestamp() will report the value to the front end,
0295      * hence asking the decoder to keep decoding rather than stopping.
0296      */
0297     if (packet_queue->cs_timestamp) {
0298         packet_queue->next_cs_timestamp = elem->timestamp;
0299         return OCSD_RESP_CONT;
0300     }
0301 
0302 
0303     if (!elem->timestamp) {
0304         /*
0305          * Zero timestamps can be seen due to misconfiguration or hardware bugs.
0306          * Warn once, and don't try to subtract instr_count as it would result in an
0307          * underflow.
0308          */
0309         packet_queue->cs_timestamp = 0;
0310         if (!cs_etm__etmq_is_timeless(etmq))
0311             pr_warning_once("Zero Coresight timestamp found at Idx:%" OCSD_TRC_IDX_STR
0312                     ". Decoding may be improved by prepending 'Z' to your current --itrace arguments.\n",
0313                     indx);
0314 
0315     } else if (packet_queue->instr_count > elem->timestamp) {
0316         /*
0317          * Sanity check that the elem->timestamp - packet_queue->instr_count would not
0318          * result in an underflow. Warn and clamp at 0 if it would.
0319          */
0320         packet_queue->cs_timestamp = 0;
0321         pr_err("Timestamp calculation underflow at Idx:%" OCSD_TRC_IDX_STR "\n", indx);
0322     } else {
0323         /*
0324          * This is the first timestamp we've seen since the beginning of traces
0325          * or a discontinuity.  Since timestamps packets are generated *after*
0326          * range packets have been generated, we need to estimate the time at
0327          * which instructions started by subtracting the number of instructions
0328          * executed to the timestamp.
0329          */
0330         packet_queue->cs_timestamp = elem->timestamp - packet_queue->instr_count;
0331     }
0332     packet_queue->next_cs_timestamp = elem->timestamp;
0333     packet_queue->instr_count = 0;
0334 
0335     /* Tell the front end which traceid_queue needs attention */
0336     cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id);
0337 
0338     /* Halt processing until we are being told to proceed */
0339     return OCSD_RESP_WAIT;
0340 }
0341 
0342 static void
0343 cs_etm_decoder__reset_timestamp(struct cs_etm_packet_queue *packet_queue)
0344 {
0345     packet_queue->cs_timestamp = 0;
0346     packet_queue->next_cs_timestamp = 0;
0347     packet_queue->instr_count = 0;
0348 }
0349 
0350 static ocsd_datapath_resp_t
0351 cs_etm_decoder__buffer_packet(struct cs_etm_packet_queue *packet_queue,
0352                   const u8 trace_chan_id,
0353                   enum cs_etm_sample_type sample_type)
0354 {
0355     u32 et = 0;
0356     int cpu;
0357 
0358     if (packet_queue->packet_count >= CS_ETM_PACKET_MAX_BUFFER - 1)
0359         return OCSD_RESP_FATAL_SYS_ERR;
0360 
0361     if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
0362         return OCSD_RESP_FATAL_SYS_ERR;
0363 
0364     et = packet_queue->tail;
0365     et = (et + 1) & (CS_ETM_PACKET_MAX_BUFFER - 1);
0366     packet_queue->tail = et;
0367     packet_queue->packet_count++;
0368 
0369     packet_queue->packet_buffer[et].sample_type = sample_type;
0370     packet_queue->packet_buffer[et].isa = CS_ETM_ISA_UNKNOWN;
0371     packet_queue->packet_buffer[et].cpu = cpu;
0372     packet_queue->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR;
0373     packet_queue->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR;
0374     packet_queue->packet_buffer[et].instr_count = 0;
0375     packet_queue->packet_buffer[et].last_instr_taken_branch = false;
0376     packet_queue->packet_buffer[et].last_instr_size = 0;
0377     packet_queue->packet_buffer[et].last_instr_type = 0;
0378     packet_queue->packet_buffer[et].last_instr_subtype = 0;
0379     packet_queue->packet_buffer[et].last_instr_cond = 0;
0380     packet_queue->packet_buffer[et].flags = 0;
0381     packet_queue->packet_buffer[et].exception_number = UINT32_MAX;
0382     packet_queue->packet_buffer[et].trace_chan_id = trace_chan_id;
0383 
0384     if (packet_queue->packet_count == CS_ETM_PACKET_MAX_BUFFER - 1)
0385         return OCSD_RESP_WAIT;
0386 
0387     return OCSD_RESP_CONT;
0388 }
0389 
0390 static ocsd_datapath_resp_t
0391 cs_etm_decoder__buffer_range(struct cs_etm_queue *etmq,
0392                  struct cs_etm_packet_queue *packet_queue,
0393                  const ocsd_generic_trace_elem *elem,
0394                  const uint8_t trace_chan_id)
0395 {
0396     int ret = 0;
0397     struct cs_etm_packet *packet;
0398 
0399     ret = cs_etm_decoder__buffer_packet(packet_queue, trace_chan_id,
0400                         CS_ETM_RANGE);
0401     if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
0402         return ret;
0403 
0404     packet = &packet_queue->packet_buffer[packet_queue->tail];
0405 
0406     switch (elem->isa) {
0407     case ocsd_isa_aarch64:
0408         packet->isa = CS_ETM_ISA_A64;
0409         break;
0410     case ocsd_isa_arm:
0411         packet->isa = CS_ETM_ISA_A32;
0412         break;
0413     case ocsd_isa_thumb2:
0414         packet->isa = CS_ETM_ISA_T32;
0415         break;
0416     case ocsd_isa_tee:
0417     case ocsd_isa_jazelle:
0418     case ocsd_isa_custom:
0419     case ocsd_isa_unknown:
0420     default:
0421         packet->isa = CS_ETM_ISA_UNKNOWN;
0422     }
0423 
0424     packet->start_addr = elem->st_addr;
0425     packet->end_addr = elem->en_addr;
0426     packet->instr_count = elem->num_instr_range;
0427     packet->last_instr_type = elem->last_i_type;
0428     packet->last_instr_subtype = elem->last_i_subtype;
0429     packet->last_instr_cond = elem->last_instr_cond;
0430 
0431     if (elem->last_i_type == OCSD_INSTR_BR || elem->last_i_type == OCSD_INSTR_BR_INDIRECT)
0432         packet->last_instr_taken_branch = elem->last_instr_exec;
0433     else
0434         packet->last_instr_taken_branch = false;
0435 
0436     packet->last_instr_size = elem->last_instr_sz;
0437 
0438     /* per-thread scenario, no need to generate a timestamp */
0439     if (cs_etm__etmq_is_timeless(etmq))
0440         goto out;
0441 
0442     /*
0443      * The packet queue is full and we haven't seen a timestamp (had we
0444      * seen one the packet queue wouldn't be full).  Let the front end
0445      * deal with it.
0446      */
0447     if (ret == OCSD_RESP_WAIT)
0448         goto out;
0449 
0450     packet_queue->instr_count += elem->num_instr_range;
0451     /* Tell the front end we have a new timestamp to process */
0452     ret = cs_etm_decoder__do_soft_timestamp(etmq, packet_queue,
0453                         trace_chan_id);
0454 out:
0455     return ret;
0456 }
0457 
0458 static ocsd_datapath_resp_t
0459 cs_etm_decoder__buffer_discontinuity(struct cs_etm_packet_queue *queue,
0460                      const uint8_t trace_chan_id)
0461 {
0462     /*
0463      * Something happened and who knows when we'll get new traces so
0464      * reset time statistics.
0465      */
0466     cs_etm_decoder__reset_timestamp(queue);
0467     return cs_etm_decoder__buffer_packet(queue, trace_chan_id,
0468                          CS_ETM_DISCONTINUITY);
0469 }
0470 
0471 static ocsd_datapath_resp_t
0472 cs_etm_decoder__buffer_exception(struct cs_etm_packet_queue *queue,
0473                  const ocsd_generic_trace_elem *elem,
0474                  const uint8_t trace_chan_id)
0475 {   int ret = 0;
0476     struct cs_etm_packet *packet;
0477 
0478     ret = cs_etm_decoder__buffer_packet(queue, trace_chan_id,
0479                         CS_ETM_EXCEPTION);
0480     if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
0481         return ret;
0482 
0483     packet = &queue->packet_buffer[queue->tail];
0484     packet->exception_number = elem->exception_number;
0485 
0486     return ret;
0487 }
0488 
0489 static ocsd_datapath_resp_t
0490 cs_etm_decoder__buffer_exception_ret(struct cs_etm_packet_queue *queue,
0491                      const uint8_t trace_chan_id)
0492 {
0493     return cs_etm_decoder__buffer_packet(queue, trace_chan_id,
0494                          CS_ETM_EXCEPTION_RET);
0495 }
0496 
0497 static ocsd_datapath_resp_t
0498 cs_etm_decoder__set_tid(struct cs_etm_queue *etmq,
0499             struct cs_etm_packet_queue *packet_queue,
0500             const ocsd_generic_trace_elem *elem,
0501             const uint8_t trace_chan_id)
0502 {
0503     pid_t tid = -1;
0504     static u64 pid_fmt;
0505     int ret;
0506 
0507     /*
0508      * As all the ETMs run at the same exception level, the system should
0509      * have the same PID format crossing CPUs.  So cache the PID format
0510      * and reuse it for sequential decoding.
0511      */
0512     if (!pid_fmt) {
0513         ret = cs_etm__get_pid_fmt(trace_chan_id, &pid_fmt);
0514         if (ret)
0515             return OCSD_RESP_FATAL_SYS_ERR;
0516     }
0517 
0518     /*
0519      * Process the PE_CONTEXT packets if we have a valid contextID or VMID.
0520      * If the kernel is running at EL2, the PID is traced in CONTEXTIDR_EL2
0521      * as VMID, Bit ETM_OPT_CTXTID2 is set in this case.
0522      */
0523     switch (pid_fmt) {
0524     case BIT(ETM_OPT_CTXTID):
0525         if (elem->context.ctxt_id_valid)
0526             tid = elem->context.context_id;
0527         break;
0528     case BIT(ETM_OPT_CTXTID2):
0529         if (elem->context.vmid_valid)
0530             tid = elem->context.vmid;
0531         break;
0532     default:
0533         break;
0534     }
0535 
0536     if (tid == -1)
0537         return OCSD_RESP_CONT;
0538 
0539     if (cs_etm__etmq_set_tid(etmq, tid, trace_chan_id))
0540         return OCSD_RESP_FATAL_SYS_ERR;
0541 
0542     /*
0543      * A timestamp is generated after a PE_CONTEXT element so make sure
0544      * to rely on that coming one.
0545      */
0546     cs_etm_decoder__reset_timestamp(packet_queue);
0547 
0548     return OCSD_RESP_CONT;
0549 }
0550 
0551 static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
0552                 const void *context,
0553                 const ocsd_trc_index_t indx,
0554                 const u8 trace_chan_id __maybe_unused,
0555                 const ocsd_generic_trace_elem *elem)
0556 {
0557     ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
0558     struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
0559     struct cs_etm_queue *etmq = decoder->data;
0560     struct cs_etm_packet_queue *packet_queue;
0561 
0562     /* First get the packet queue for this traceID */
0563     packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id);
0564     if (!packet_queue)
0565         return OCSD_RESP_FATAL_SYS_ERR;
0566 
0567     switch (elem->elem_type) {
0568     case OCSD_GEN_TRC_ELEM_UNKNOWN:
0569         break;
0570     case OCSD_GEN_TRC_ELEM_EO_TRACE:
0571     case OCSD_GEN_TRC_ELEM_NO_SYNC:
0572     case OCSD_GEN_TRC_ELEM_TRACE_ON:
0573         resp = cs_etm_decoder__buffer_discontinuity(packet_queue,
0574                                 trace_chan_id);
0575         break;
0576     case OCSD_GEN_TRC_ELEM_INSTR_RANGE:
0577         resp = cs_etm_decoder__buffer_range(etmq, packet_queue, elem,
0578                             trace_chan_id);
0579         break;
0580     case OCSD_GEN_TRC_ELEM_EXCEPTION:
0581         resp = cs_etm_decoder__buffer_exception(packet_queue, elem,
0582                             trace_chan_id);
0583         break;
0584     case OCSD_GEN_TRC_ELEM_EXCEPTION_RET:
0585         resp = cs_etm_decoder__buffer_exception_ret(packet_queue,
0586                                 trace_chan_id);
0587         break;
0588     case OCSD_GEN_TRC_ELEM_TIMESTAMP:
0589         resp = cs_etm_decoder__do_hard_timestamp(etmq, elem,
0590                              trace_chan_id,
0591                              indx);
0592         break;
0593     case OCSD_GEN_TRC_ELEM_PE_CONTEXT:
0594         resp = cs_etm_decoder__set_tid(etmq, packet_queue,
0595                            elem, trace_chan_id);
0596         break;
0597     /* Unused packet types */
0598     case OCSD_GEN_TRC_ELEM_I_RANGE_NOPATH:
0599     case OCSD_GEN_TRC_ELEM_ADDR_NACC:
0600     case OCSD_GEN_TRC_ELEM_CYCLE_COUNT:
0601     case OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN:
0602     case OCSD_GEN_TRC_ELEM_EVENT:
0603     case OCSD_GEN_TRC_ELEM_SWTRACE:
0604     case OCSD_GEN_TRC_ELEM_CUSTOM:
0605     case OCSD_GEN_TRC_ELEM_SYNC_MARKER:
0606     case OCSD_GEN_TRC_ELEM_MEMTRANS:
0607     default:
0608         break;
0609     }
0610 
0611     return resp;
0612 }
0613 
0614 static int
0615 cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params,
0616                    struct cs_etm_trace_params *t_params,
0617                    struct cs_etm_decoder *decoder)
0618 {
0619     ocsd_etmv3_cfg config_etmv3;
0620     ocsd_etmv4_cfg trace_config_etmv4;
0621     ocsd_ete_cfg trace_config_ete;
0622     void *trace_config;
0623     u8 csid;
0624 
0625     switch (t_params->protocol) {
0626     case CS_ETM_PROTO_ETMV3:
0627     case CS_ETM_PROTO_PTM:
0628         cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3);
0629         decoder->decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ?
0630                             OCSD_BUILTIN_DCD_ETMV3 :
0631                             OCSD_BUILTIN_DCD_PTM;
0632         trace_config = &config_etmv3;
0633         break;
0634     case CS_ETM_PROTO_ETMV4i:
0635         cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
0636         decoder->decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
0637         trace_config = &trace_config_etmv4;
0638         break;
0639     case CS_ETM_PROTO_ETE:
0640         cs_etm_decoder__gen_ete_config(t_params, &trace_config_ete);
0641         decoder->decoder_name = OCSD_BUILTIN_DCD_ETE;
0642         trace_config = &trace_config_ete;
0643         break;
0644     default:
0645         return -1;
0646     }
0647 
0648     if (d_params->operation == CS_ETM_OPERATION_DECODE) {
0649         if (ocsd_dt_create_decoder(decoder->dcd_tree,
0650                        decoder->decoder_name,
0651                        OCSD_CREATE_FLG_FULL_DECODER,
0652                        trace_config, &csid))
0653             return -1;
0654 
0655         if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree,
0656                            cs_etm_decoder__gen_trace_elem_printer,
0657                            decoder))
0658             return -1;
0659 
0660         return 0;
0661     } else if (d_params->operation == CS_ETM_OPERATION_PRINT) {
0662         if (ocsd_dt_create_decoder(decoder->dcd_tree, decoder->decoder_name,
0663                        OCSD_CREATE_FLG_PACKET_PROC,
0664                        trace_config, &csid))
0665             return -1;
0666 
0667         if (ocsd_dt_set_pkt_protocol_printer(decoder->dcd_tree, csid, 0))
0668             return -1;
0669 
0670         return 0;
0671     }
0672 
0673     return -1;
0674 }
0675 
0676 struct cs_etm_decoder *
0677 cs_etm_decoder__new(int decoders, struct cs_etm_decoder_params *d_params,
0678             struct cs_etm_trace_params t_params[])
0679 {
0680     struct cs_etm_decoder *decoder;
0681     ocsd_dcd_tree_src_t format;
0682     u32 flags;
0683     int i, ret;
0684 
0685     if ((!t_params) || (!d_params))
0686         return NULL;
0687 
0688     decoder = zalloc(sizeof(*decoder));
0689 
0690     if (!decoder)
0691         return NULL;
0692 
0693     decoder->data = d_params->data;
0694     decoder->prev_return = OCSD_RESP_CONT;
0695     format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED :
0696                      OCSD_TRC_SRC_SINGLE);
0697     flags = 0;
0698     flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0);
0699     flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0);
0700     flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0);
0701 
0702     /*
0703      * Drivers may add barrier frames when used with perf, set up to
0704      * handle this. Barriers const of FSYNC packet repeated 4 times.
0705      */
0706     flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC;
0707 
0708     /* Create decode tree for the data source */
0709     decoder->dcd_tree = ocsd_create_dcd_tree(format, flags);
0710 
0711     if (decoder->dcd_tree == 0)
0712         goto err_free_decoder;
0713 
0714     /* init library print logging support */
0715     ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder);
0716     if (ret != 0)
0717         goto err_free_decoder;
0718 
0719     /* init raw frame logging if required */
0720     cs_etm_decoder__init_raw_frame_logging(d_params, decoder);
0721 
0722     for (i = 0; i < decoders; i++) {
0723         ret = cs_etm_decoder__create_etm_decoder(d_params,
0724                              &t_params[i],
0725                              decoder);
0726         if (ret != 0)
0727             goto err_free_decoder;
0728     }
0729 
0730     return decoder;
0731 
0732 err_free_decoder:
0733     cs_etm_decoder__free(decoder);
0734     return NULL;
0735 }
0736 
0737 int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder,
0738                        u64 indx, const u8 *buf,
0739                        size_t len, size_t *consumed)
0740 {
0741     int ret = 0;
0742     ocsd_datapath_resp_t cur = OCSD_RESP_CONT;
0743     ocsd_datapath_resp_t prev_return = decoder->prev_return;
0744     size_t processed = 0;
0745     u32 count;
0746 
0747     while (processed < len) {
0748         if (OCSD_DATA_RESP_IS_WAIT(prev_return)) {
0749             cur = ocsd_dt_process_data(decoder->dcd_tree,
0750                            OCSD_OP_FLUSH,
0751                            0,
0752                            0,
0753                            NULL,
0754                            NULL);
0755         } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) {
0756             cur = ocsd_dt_process_data(decoder->dcd_tree,
0757                            OCSD_OP_DATA,
0758                            indx + processed,
0759                            len - processed,
0760                            &buf[processed],
0761                            &count);
0762             processed += count;
0763         } else {
0764             ret = -EINVAL;
0765             break;
0766         }
0767 
0768         /*
0769          * Return to the input code if the packet buffer is full.
0770          * Flushing will get done once the packet buffer has been
0771          * processed.
0772          */
0773         if (OCSD_DATA_RESP_IS_WAIT(cur))
0774             break;
0775 
0776         prev_return = cur;
0777     }
0778 
0779     decoder->prev_return = cur;
0780     *consumed = processed;
0781 
0782     return ret;
0783 }
0784 
0785 void cs_etm_decoder__free(struct cs_etm_decoder *decoder)
0786 {
0787     if (!decoder)
0788         return;
0789 
0790     ocsd_destroy_dcd_tree(decoder->dcd_tree);
0791     decoder->dcd_tree = NULL;
0792     free(decoder);
0793 }
0794 
0795 const char *cs_etm_decoder__get_name(struct cs_etm_decoder *decoder)
0796 {
0797     return decoder->decoder_name;
0798 }