1 /*
2  * SPDX-License-Identifier: GPL-2.0
3  *
4  * Copyright(C) 2015-2018 Linaro Limited.
5  *
6  * Author: Tor Jeremiassen <tor@ti.com>
7  * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
8  */
9 
10 #ifndef INCLUDE__CS_ETM_DECODER_H__
11 #define INCLUDE__CS_ETM_DECODER_H__
12 
13 #include <linux/types.h>
14 #include <stdio.h>
15 
16 struct cs_etm_decoder;
17 
18 enum cs_etm_sample_type {
19 	CS_ETM_EMPTY,
20 	CS_ETM_RANGE,
21 	CS_ETM_DISCONTINUITY,
22 	CS_ETM_EXCEPTION,
23 	CS_ETM_EXCEPTION_RET,
24 };
25 
26 enum cs_etm_isa {
27 	CS_ETM_ISA_UNKNOWN,
28 	CS_ETM_ISA_A64,
29 	CS_ETM_ISA_A32,
30 	CS_ETM_ISA_T32,
31 };
32 
33 struct cs_etm_packet {
34 	enum cs_etm_sample_type sample_type;
35 	enum cs_etm_isa isa;
36 	u64 start_addr;
37 	u64 end_addr;
38 	u32 instr_count;
39 	u32 last_instr_type;
40 	u32 last_instr_subtype;
41 	u32 flags;
42 	u32 exception_number;
43 	u8 last_instr_cond;
44 	u8 last_instr_taken_branch;
45 	u8 last_instr_size;
46 	u8 trace_chan_id;
47 	int cpu;
48 };
49 
50 struct cs_etm_queue;
51 
52 typedef u32 (*cs_etm_mem_cb_type)(struct cs_etm_queue *, u64,
53 				  size_t, u8 *);
54 
55 struct cs_etmv3_trace_params {
56 	u32 reg_ctrl;
57 	u32 reg_trc_id;
58 	u32 reg_ccer;
59 	u32 reg_idr;
60 };
61 
62 struct cs_etmv4_trace_params {
63 	u32 reg_idr0;
64 	u32 reg_idr1;
65 	u32 reg_idr2;
66 	u32 reg_idr8;
67 	u32 reg_configr;
68 	u32 reg_traceidr;
69 };
70 
71 struct cs_etm_trace_params {
72 	int protocol;
73 	union {
74 		struct cs_etmv3_trace_params etmv3;
75 		struct cs_etmv4_trace_params etmv4;
76 	};
77 };
78 
79 struct cs_etm_decoder_params {
80 	int operation;
81 	void (*packet_printer)(const char *msg);
82 	cs_etm_mem_cb_type mem_acc_cb;
83 	u8 formatted;
84 	u8 fsyncs;
85 	u8 hsyncs;
86 	u8 frame_aligned;
87 	void *data;
88 };
89 
90 /*
91  * The following enums are indexed starting with 1 to align with the
92  * open source coresight trace decoder library.
93  */
94 enum {
95 	CS_ETM_PROTO_ETMV3 = 1,
96 	CS_ETM_PROTO_ETMV4i,
97 	CS_ETM_PROTO_ETMV4d,
98 	CS_ETM_PROTO_PTM,
99 };
100 
101 enum cs_etm_decoder_operation {
102 	CS_ETM_OPERATION_PRINT = 1,
103 	CS_ETM_OPERATION_DECODE,
104 	CS_ETM_OPERATION_MAX,
105 };
106 
107 int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder,
108 				       u64 indx, const u8 *buf,
109 				       size_t len, size_t *consumed);
110 
111 struct cs_etm_decoder *
112 cs_etm_decoder__new(int num_cpu,
113 		    struct cs_etm_decoder_params *d_params,
114 		    struct cs_etm_trace_params t_params[]);
115 
116 void cs_etm_decoder__free(struct cs_etm_decoder *decoder);
117 
118 int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder,
119 				      u64 start, u64 end,
120 				      cs_etm_mem_cb_type cb_func);
121 
122 int cs_etm_decoder__get_packet(struct cs_etm_decoder *decoder,
123 			       struct cs_etm_packet *packet);
124 
125 int cs_etm_decoder__reset(struct cs_etm_decoder *decoder);
126 
127 #endif /* INCLUDE__CS_ETM_DECODER_H__ */
128