cs-etm-decoder.c 14 KB

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