blob: 8ff69dfd725af3705e316d7140ecd2870331abc6 [file] [log] [blame]
Mathieu Poirier68ffe392018-01-17 10:52:13 -07001/*
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#include <linux/err.h>
11#include <linux/list.h>
12#include <stdlib.h>
13#include <opencsd/c_api/opencsd_c_api.h>
14#include <opencsd/etmv4/trc_pkt_types_etmv4.h>
15#include <opencsd/ocsd_if_types.h>
16
17#include "cs-etm.h"
18#include "cs-etm-decoder.h"
19#include "intlist.h"
20#include "util.h"
21
22#define MAX_BUFFER 1024
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
35struct cs_etm_decoder {
36 void *data;
37 void (*packet_printer)(const char *msg);
38 bool trace_on;
39 dcd_tree_handle_t dcd_tree;
40 cs_etm_mem_cb_type mem_access;
41 ocsd_datapath_resp_t prev_return;
42 u32 packet_count;
43 u32 head;
44 u32 tail;
45 struct cs_etm_packet packet_buffer[MAX_BUFFER];
46};
47
Mathieu Poirier290598b2018-01-17 10:52:15 -070048static u32
49cs_etm_decoder__mem_access(const void *context,
50 const ocsd_vaddr_t address,
51 const ocsd_mem_space_acc_t mem_space __maybe_unused,
52 const u32 req_size,
53 u8 *buffer)
54{
55 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
56
57 return decoder->mem_access(decoder->data,
58 address,
59 req_size,
60 buffer);
61}
62
63int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder,
64 u64 start, u64 end,
65 cs_etm_mem_cb_type cb_func)
66{
67 decoder->mem_access = cb_func;
68
69 if (ocsd_dt_add_callback_mem_acc(decoder->dcd_tree, start, end,
70 OCSD_MEM_SPACE_ANY,
71 cs_etm_decoder__mem_access, decoder))
72 return -1;
73
74 return 0;
75}
76
77int cs_etm_decoder__reset(struct cs_etm_decoder *decoder)
78{
79 ocsd_datapath_resp_t dp_ret;
80
Robert Walkere573e972018-02-14 11:24:39 +000081 decoder->prev_return = OCSD_RESP_CONT;
82
Mathieu Poirier290598b2018-01-17 10:52:15 -070083 dp_ret = ocsd_dt_process_data(decoder->dcd_tree, OCSD_OP_RESET,
84 0, 0, NULL, NULL);
85 if (OCSD_DATA_RESP_IS_FATAL(dp_ret))
86 return -1;
87
88 return 0;
89}
90
91int cs_etm_decoder__get_packet(struct cs_etm_decoder *decoder,
92 struct cs_etm_packet *packet)
93{
94 if (!decoder || !packet)
95 return -EINVAL;
96
97 /* Nothing to do, might as well just return */
98 if (decoder->packet_count == 0)
99 return 0;
100
101 *packet = decoder->packet_buffer[decoder->head];
102
103 decoder->head = (decoder->head + 1) & (MAX_BUFFER - 1);
104
105 decoder->packet_count--;
106
107 return 1;
108}
109
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700110static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params,
111 ocsd_etmv4_cfg *config)
112{
113 config->reg_configr = params->etmv4.reg_configr;
114 config->reg_traceidr = params->etmv4.reg_traceidr;
115 config->reg_idr0 = params->etmv4.reg_idr0;
116 config->reg_idr1 = params->etmv4.reg_idr1;
117 config->reg_idr2 = params->etmv4.reg_idr2;
118 config->reg_idr8 = params->etmv4.reg_idr8;
119 config->reg_idr9 = 0;
120 config->reg_idr10 = 0;
121 config->reg_idr11 = 0;
122 config->reg_idr12 = 0;
123 config->reg_idr13 = 0;
124 config->arch_ver = ARCH_V8;
125 config->core_prof = profile_CortexA;
126}
127
128static void cs_etm_decoder__print_str_cb(const void *p_context,
129 const char *msg,
130 const int str_len)
131{
132 if (p_context && str_len)
133 ((struct cs_etm_decoder *)p_context)->packet_printer(msg);
134}
135
136static int
137cs_etm_decoder__init_def_logger_printing(struct cs_etm_decoder_params *d_params,
138 struct cs_etm_decoder *decoder)
139{
140 int ret = 0;
141
142 if (d_params->packet_printer == NULL)
143 return -1;
144
145 decoder->packet_printer = d_params->packet_printer;
146
147 /*
148 * Set up a library default logger to process any printers
149 * (packet/raw frame) we add later.
150 */
151 ret = ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
152 if (ret != 0)
153 return -1;
154
155 /* no stdout / err / file output */
156 ret = ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
157 if (ret != 0)
158 return -1;
159
160 /*
161 * Set the string CB for the default logger, passes strings to
162 * perf print logger.
163 */
164 ret = ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
165 (void *)decoder,
166 cs_etm_decoder__print_str_cb);
167 if (ret != 0)
168 ret = -1;
169
170 return 0;
171}
172
173#ifdef CS_LOG_RAW_FRAMES
174static void
175cs_etm_decoder__init_raw_frame_logging(struct cs_etm_decoder_params *d_params,
176 struct cs_etm_decoder *decoder)
177{
178 /* Only log these during a --dump operation */
179 if (d_params->operation == CS_ETM_OPERATION_PRINT) {
180 /* set up a library default logger to process the
181 * raw frame printer we add later
182 */
183 ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
184
185 /* no stdout / err / file output */
186 ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
187
188 /* set the string CB for the default logger,
189 * passes strings to perf print logger.
190 */
191 ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
192 (void *)decoder,
193 cs_etm_decoder__print_str_cb);
194
195 /* use the built in library printer for the raw frames */
196 ocsd_dt_set_raw_frame_printer(decoder->dcd_tree,
197 CS_RAW_DEBUG_FLAGS);
198 }
199}
200#else
201static void
202cs_etm_decoder__init_raw_frame_logging(
203 struct cs_etm_decoder_params *d_params __maybe_unused,
204 struct cs_etm_decoder *decoder __maybe_unused)
205{
206}
207#endif
208
209static int cs_etm_decoder__create_packet_printer(struct cs_etm_decoder *decoder,
210 const char *decoder_name,
211 void *trace_config)
212{
213 u8 csid;
214
215 if (ocsd_dt_create_decoder(decoder->dcd_tree, decoder_name,
216 OCSD_CREATE_FLG_PACKET_PROC,
217 trace_config, &csid))
218 return -1;
219
220 if (ocsd_dt_set_pkt_protocol_printer(decoder->dcd_tree, csid, 0))
221 return -1;
222
223 return 0;
224}
225
226static int
227cs_etm_decoder__create_etm_packet_printer(struct cs_etm_trace_params *t_params,
228 struct cs_etm_decoder *decoder)
229{
230 const char *decoder_name;
231 ocsd_etmv4_cfg trace_config_etmv4;
232 void *trace_config;
233
234 switch (t_params->protocol) {
235 case CS_ETM_PROTO_ETMV4i:
236 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
237 decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
238 trace_config = &trace_config_etmv4;
239 break;
240 default:
241 return -1;
242 }
243
244 return cs_etm_decoder__create_packet_printer(decoder,
245 decoder_name,
246 trace_config);
247}
248
249static void cs_etm_decoder__clear_buffer(struct cs_etm_decoder *decoder)
250{
251 int i;
252
253 decoder->head = 0;
254 decoder->tail = 0;
255 decoder->packet_count = 0;
256 for (i = 0; i < MAX_BUFFER; i++) {
257 decoder->packet_buffer[i].start_addr = 0xdeadbeefdeadbeefUL;
Robert Walkere573e972018-02-14 11:24:39 +0000258 decoder->packet_buffer[i].end_addr = 0xdeadbeefdeadbeefUL;
259 decoder->packet_buffer[i].last_instr_taken_branch = false;
260 decoder->packet_buffer[i].exc = false;
261 decoder->packet_buffer[i].exc_ret = false;
262 decoder->packet_buffer[i].cpu = INT_MIN;
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700263 }
264}
265
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700266static ocsd_datapath_resp_t
267cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder,
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700268 const u8 trace_chan_id,
269 enum cs_etm_sample_type sample_type)
270{
271 u32 et = 0;
272 struct int_node *inode = NULL;
273
274 if (decoder->packet_count >= MAX_BUFFER - 1)
275 return OCSD_RESP_FATAL_SYS_ERR;
276
277 /* Search the RB tree for the cpu associated with this traceID */
278 inode = intlist__find(traceid_list, trace_chan_id);
279 if (!inode)
280 return OCSD_RESP_FATAL_SYS_ERR;
281
282 et = decoder->tail;
Robert Walkere573e972018-02-14 11:24:39 +0000283 et = (et + 1) & (MAX_BUFFER - 1);
284 decoder->tail = et;
285 decoder->packet_count++;
286
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700287 decoder->packet_buffer[et].sample_type = sample_type;
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700288 decoder->packet_buffer[et].exc = false;
289 decoder->packet_buffer[et].exc_ret = false;
290 decoder->packet_buffer[et].cpu = *((int *)inode->priv);
Robert Walkere573e972018-02-14 11:24:39 +0000291 decoder->packet_buffer[et].start_addr = 0xdeadbeefdeadbeefUL;
292 decoder->packet_buffer[et].end_addr = 0xdeadbeefdeadbeefUL;
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700293
294 if (decoder->packet_count == MAX_BUFFER - 1)
295 return OCSD_RESP_WAIT;
296
297 return OCSD_RESP_CONT;
298}
299
Robert Walkere573e972018-02-14 11:24:39 +0000300static ocsd_datapath_resp_t
301cs_etm_decoder__buffer_range(struct cs_etm_decoder *decoder,
302 const ocsd_generic_trace_elem *elem,
303 const uint8_t trace_chan_id)
304{
305 int ret = 0;
306 struct cs_etm_packet *packet;
307
308 ret = cs_etm_decoder__buffer_packet(decoder, trace_chan_id,
309 CS_ETM_RANGE);
310 if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
311 return ret;
312
313 packet = &decoder->packet_buffer[decoder->tail];
314
315 packet->start_addr = elem->st_addr;
316 packet->end_addr = elem->en_addr;
317 switch (elem->last_i_type) {
318 case OCSD_INSTR_BR:
319 case OCSD_INSTR_BR_INDIRECT:
320 packet->last_instr_taken_branch = elem->last_instr_exec;
321 break;
322 case OCSD_INSTR_ISB:
323 case OCSD_INSTR_DSB_DMB:
324 case OCSD_INSTR_OTHER:
325 default:
326 packet->last_instr_taken_branch = false;
327 break;
328 }
329
330 return ret;
331
332}
333
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700334static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
335 const void *context,
336 const ocsd_trc_index_t indx __maybe_unused,
337 const u8 trace_chan_id __maybe_unused,
338 const ocsd_generic_trace_elem *elem)
339{
340 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
341 struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
342
343 switch (elem->elem_type) {
344 case OCSD_GEN_TRC_ELEM_UNKNOWN:
345 break;
346 case OCSD_GEN_TRC_ELEM_NO_SYNC:
347 decoder->trace_on = false;
348 break;
349 case OCSD_GEN_TRC_ELEM_TRACE_ON:
350 decoder->trace_on = true;
351 break;
352 case OCSD_GEN_TRC_ELEM_INSTR_RANGE:
Robert Walkere573e972018-02-14 11:24:39 +0000353 resp = cs_etm_decoder__buffer_range(decoder, elem,
354 trace_chan_id);
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700355 break;
356 case OCSD_GEN_TRC_ELEM_EXCEPTION:
357 decoder->packet_buffer[decoder->tail].exc = true;
358 break;
359 case OCSD_GEN_TRC_ELEM_EXCEPTION_RET:
360 decoder->packet_buffer[decoder->tail].exc_ret = true;
361 break;
362 case OCSD_GEN_TRC_ELEM_PE_CONTEXT:
363 case OCSD_GEN_TRC_ELEM_EO_TRACE:
364 case OCSD_GEN_TRC_ELEM_ADDR_NACC:
365 case OCSD_GEN_TRC_ELEM_TIMESTAMP:
366 case OCSD_GEN_TRC_ELEM_CYCLE_COUNT:
367 case OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN:
368 case OCSD_GEN_TRC_ELEM_EVENT:
369 case OCSD_GEN_TRC_ELEM_SWTRACE:
370 case OCSD_GEN_TRC_ELEM_CUSTOM:
371 default:
372 break;
373 }
374
375 return resp;
376}
377
378static int cs_etm_decoder__create_etm_packet_decoder(
379 struct cs_etm_trace_params *t_params,
380 struct cs_etm_decoder *decoder)
381{
382 const char *decoder_name;
383 ocsd_etmv4_cfg trace_config_etmv4;
384 void *trace_config;
385 u8 csid;
386
387 switch (t_params->protocol) {
388 case CS_ETM_PROTO_ETMV4i:
389 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
390 decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
391 trace_config = &trace_config_etmv4;
392 break;
393 default:
394 return -1;
395 }
396
397 if (ocsd_dt_create_decoder(decoder->dcd_tree,
398 decoder_name,
399 OCSD_CREATE_FLG_FULL_DECODER,
400 trace_config, &csid))
401 return -1;
402
403 if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree,
404 cs_etm_decoder__gen_trace_elem_printer,
405 decoder))
406 return -1;
407
408 return 0;
409}
410
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700411static int
412cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params,
413 struct cs_etm_trace_params *t_params,
414 struct cs_etm_decoder *decoder)
415{
416 if (d_params->operation == CS_ETM_OPERATION_PRINT)
417 return cs_etm_decoder__create_etm_packet_printer(t_params,
418 decoder);
Mathieu Poirierc9a01a12018-01-17 10:52:14 -0700419 else if (d_params->operation == CS_ETM_OPERATION_DECODE)
420 return cs_etm_decoder__create_etm_packet_decoder(t_params,
421 decoder);
422
Mathieu Poirier68ffe392018-01-17 10:52:13 -0700423 return -1;
424}
425
426struct cs_etm_decoder *
427cs_etm_decoder__new(int num_cpu, struct cs_etm_decoder_params *d_params,
428 struct cs_etm_trace_params t_params[])
429{
430 struct cs_etm_decoder *decoder;
431 ocsd_dcd_tree_src_t format;
432 u32 flags;
433 int i, ret;
434
435 if ((!t_params) || (!d_params))
436 return NULL;
437
438 decoder = zalloc(sizeof(*decoder));
439
440 if (!decoder)
441 return NULL;
442
443 decoder->data = d_params->data;
444 decoder->prev_return = OCSD_RESP_CONT;
445 cs_etm_decoder__clear_buffer(decoder);
446 format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED :
447 OCSD_TRC_SRC_SINGLE);
448 flags = 0;
449 flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0);
450 flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0);
451 flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0);
452
453 /*
454 * Drivers may add barrier frames when used with perf, set up to
455 * handle this. Barriers const of FSYNC packet repeated 4 times.
456 */
457 flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC;
458
459 /* Create decode tree for the data source */
460 decoder->dcd_tree = ocsd_create_dcd_tree(format, flags);
461
462 if (decoder->dcd_tree == 0)
463 goto err_free_decoder;
464
465 /* init library print logging support */
466 ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder);
467 if (ret != 0)
468 goto err_free_decoder_tree;
469
470 /* init raw frame logging if required */
471 cs_etm_decoder__init_raw_frame_logging(d_params, decoder);
472
473 for (i = 0; i < num_cpu; i++) {
474 ret = cs_etm_decoder__create_etm_decoder(d_params,
475 &t_params[i],
476 decoder);
477 if (ret != 0)
478 goto err_free_decoder_tree;
479 }
480
481 return decoder;
482
483err_free_decoder_tree:
484 ocsd_destroy_dcd_tree(decoder->dcd_tree);
485err_free_decoder:
486 free(decoder);
487 return NULL;
488}
489
490int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder,
491 u64 indx, const u8 *buf,
492 size_t len, size_t *consumed)
493{
494 int ret = 0;
495 ocsd_datapath_resp_t cur = OCSD_RESP_CONT;
496 ocsd_datapath_resp_t prev_return = decoder->prev_return;
497 size_t processed = 0;
498 u32 count;
499
500 while (processed < len) {
501 if (OCSD_DATA_RESP_IS_WAIT(prev_return)) {
502 cur = ocsd_dt_process_data(decoder->dcd_tree,
503 OCSD_OP_FLUSH,
504 0,
505 0,
506 NULL,
507 NULL);
508 } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) {
509 cur = ocsd_dt_process_data(decoder->dcd_tree,
510 OCSD_OP_DATA,
511 indx + processed,
512 len - processed,
513 &buf[processed],
514 &count);
515 processed += count;
516 } else {
517 ret = -EINVAL;
518 break;
519 }
520
521 /*
522 * Return to the input code if the packet buffer is full.
523 * Flushing will get done once the packet buffer has been
524 * processed.
525 */
526 if (OCSD_DATA_RESP_IS_WAIT(cur))
527 break;
528
529 prev_return = cur;
530 }
531
532 decoder->prev_return = cur;
533 *consumed = processed;
534
535 return ret;
536}
537
538void cs_etm_decoder__free(struct cs_etm_decoder *decoder)
539{
540 if (!decoder)
541 return;
542
543 ocsd_destroy_dcd_tree(decoder->dcd_tree);
544 decoder->dcd_tree = NULL;
545 free(decoder);
546}