esp_etm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <sys/cdefs.h>
  8. #include <sys/lock.h>
  9. #include "sdkconfig.h"
  10. #if CONFIG_ETM_ENABLE_DEBUG_LOG
  11. // The local log level must be defined before including esp_log.h
  12. // Set the maximum log level for this source file
  13. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  14. #endif
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "soc/soc_caps.h"
  18. #include "soc/periph_defs.h"
  19. #include "esp_log.h"
  20. #include "esp_check.h"
  21. #include "esp_heap_caps.h"
  22. #include "esp_etm.h"
  23. #include "hal/etm_hal.h"
  24. #include "hal/etm_ll.h"
  25. #include "esp_private/periph_ctrl.h"
  26. #include "esp_private/etm_interface.h"
  27. #define ETM_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
  28. #if CONFIG_IDF_TARGET_ESP32P4
  29. // Reset and Clock Control registers are mixing with other peripherals, so we need to use a critical section
  30. #define ETM_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
  31. #else
  32. #define ETM_RCC_ATOMIC()
  33. #endif
  34. static const char *TAG = "etm";
  35. typedef struct etm_platform_t etm_platform_t;
  36. typedef struct etm_group_t etm_group_t;
  37. typedef struct esp_etm_channel_t esp_etm_channel_t;
  38. struct etm_platform_t {
  39. _lock_t mutex; // platform level mutex lock
  40. etm_group_t *groups[SOC_ETM_GROUPS]; // etm group pool
  41. int group_ref_counts[SOC_ETM_GROUPS]; // reference count used to protect group install/uninstall
  42. };
  43. struct etm_group_t {
  44. int group_id; // hardware group id
  45. etm_hal_context_t hal; // hardware abstraction layer context
  46. portMUX_TYPE spinlock; // to protect per-group register level concurrent access
  47. esp_etm_channel_t *chans[SOC_ETM_CHANNELS_PER_GROUP];
  48. };
  49. typedef enum {
  50. ETM_CHAN_FSM_INIT,
  51. ETM_CHAN_FSM_ENABLE,
  52. } etm_chan_fsm_t;
  53. struct esp_etm_channel_t {
  54. int chan_id; // Channel ID
  55. etm_group_t *group; // which group this channel belongs to
  56. etm_chan_fsm_t fsm; // record ETM channel's driver state
  57. esp_etm_event_handle_t event; // which event is connect to the channel
  58. esp_etm_task_handle_t task; // which task is connect to the channel
  59. };
  60. // ETM driver platform, it's always a singleton
  61. static etm_platform_t s_platform;
  62. static etm_group_t *etm_acquire_group_handle(int group_id)
  63. {
  64. bool new_group = false;
  65. etm_group_t *group = NULL;
  66. // prevent install ETM group concurrently
  67. _lock_acquire(&s_platform.mutex);
  68. if (!s_platform.groups[group_id]) {
  69. group = heap_caps_calloc(1, sizeof(etm_group_t), ETM_MEM_ALLOC_CAPS);
  70. if (group) {
  71. new_group = true;
  72. s_platform.groups[group_id] = group; // register to platform
  73. // initialize ETM group members
  74. group->group_id = group_id;
  75. group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  76. // enable bus clock for the ETM registers
  77. ETM_RCC_ATOMIC() {
  78. etm_ll_enable_bus_clock(group_id, true);
  79. etm_ll_reset_register(group_id);
  80. }
  81. // initialize HAL context
  82. etm_hal_init(&group->hal);
  83. }
  84. } else {
  85. group = s_platform.groups[group_id];
  86. }
  87. if (group) {
  88. // someone acquired the group handle means we have a new object that refer to this group
  89. s_platform.group_ref_counts[group_id]++;
  90. }
  91. _lock_release(&s_platform.mutex);
  92. if (new_group) {
  93. ESP_LOGD(TAG, "new group (%d) at %p", group_id, group);
  94. }
  95. return group;
  96. }
  97. static void etm_release_group_handle(etm_group_t *group)
  98. {
  99. int group_id = group->group_id;
  100. bool do_deinitialize = false;
  101. _lock_acquire(&s_platform.mutex);
  102. s_platform.group_ref_counts[group_id]--;
  103. if (s_platform.group_ref_counts[group_id] == 0) {
  104. assert(s_platform.groups[group_id]);
  105. do_deinitialize = true;
  106. s_platform.groups[group_id] = NULL; // deregister from platform
  107. // disable the bus clock for the ETM registers
  108. ETM_RCC_ATOMIC() {
  109. etm_ll_enable_bus_clock(group_id, false);
  110. }
  111. }
  112. _lock_release(&s_platform.mutex);
  113. if (do_deinitialize) {
  114. free(group);
  115. ESP_LOGD(TAG, "del group (%d)", group_id);
  116. }
  117. }
  118. static esp_err_t etm_chan_register_to_group(esp_etm_channel_t *chan)
  119. {
  120. etm_group_t *group = NULL;
  121. int chan_id = -1;
  122. for (int i = 0; i < SOC_ETM_GROUPS; i++) {
  123. group = etm_acquire_group_handle(i);
  124. ESP_RETURN_ON_FALSE(group, ESP_ERR_NO_MEM, TAG, "no mem for group (%d)", i);
  125. // loop to search free channel in the group
  126. portENTER_CRITICAL(&group->spinlock);
  127. for (int j = 0; j < SOC_ETM_CHANNELS_PER_GROUP; j++) {
  128. if (!group->chans[j]) {
  129. chan_id = j;
  130. group->chans[j] = chan;
  131. break;
  132. }
  133. }
  134. portEXIT_CRITICAL(&group->spinlock);
  135. if (chan_id < 0) {
  136. etm_release_group_handle(group);
  137. group = NULL;
  138. } else {
  139. chan->chan_id = chan_id;
  140. chan->group = group;
  141. break;
  142. }
  143. }
  144. ESP_RETURN_ON_FALSE(chan_id != -1, ESP_ERR_NOT_FOUND, TAG, "no free channel");
  145. return ESP_OK;
  146. }
  147. static void etm_chan_unregister_from_group(esp_etm_channel_t *chan)
  148. {
  149. etm_group_t *group = chan->group;
  150. int chan_id = chan->chan_id;
  151. portENTER_CRITICAL(&group->spinlock);
  152. group->chans[chan_id] = NULL;
  153. portEXIT_CRITICAL(&group->spinlock);
  154. // channel has a reference on group, release it now
  155. etm_release_group_handle(group);
  156. }
  157. static esp_err_t etm_chan_destroy(esp_etm_channel_t *chan)
  158. {
  159. if (chan->group) {
  160. etm_chan_unregister_from_group(chan);
  161. }
  162. free(chan);
  163. return ESP_OK;
  164. }
  165. esp_err_t esp_etm_new_channel(const esp_etm_channel_config_t *config, esp_etm_channel_handle_t *ret_chan)
  166. {
  167. #if CONFIG_ETM_ENABLE_DEBUG_LOG
  168. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  169. #endif
  170. esp_err_t ret = ESP_OK;
  171. esp_etm_channel_t *chan = NULL;
  172. ESP_GOTO_ON_FALSE(config && ret_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid args");
  173. chan = heap_caps_calloc(1, sizeof(esp_etm_channel_t), ETM_MEM_ALLOC_CAPS);
  174. ESP_GOTO_ON_FALSE(chan, ESP_ERR_NO_MEM, err, TAG, "no mem for channel");
  175. // register channel to the group, one group can have multiple channels
  176. ESP_GOTO_ON_ERROR(etm_chan_register_to_group(chan), err, TAG, "register channel failed");
  177. etm_group_t *group = chan->group;
  178. int group_id = group->group_id;
  179. int chan_id = chan->chan_id;
  180. chan->fsm = ETM_CHAN_FSM_INIT;
  181. ESP_LOGD(TAG, "new etm channel (%d,%d) at %p", group_id, chan_id, chan);
  182. *ret_chan = chan;
  183. return ESP_OK;
  184. err:
  185. if (chan) {
  186. etm_chan_destroy(chan);
  187. }
  188. return ret;
  189. }
  190. esp_err_t esp_etm_del_channel(esp_etm_channel_handle_t chan)
  191. {
  192. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid args");
  193. ESP_RETURN_ON_FALSE(chan->fsm == ETM_CHAN_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "channel is not in init state");
  194. etm_group_t *group = chan->group;
  195. int group_id = group->group_id;
  196. int chan_id = chan->chan_id;
  197. // disconnect the channel from any event or task
  198. etm_ll_channel_set_event(group->hal.regs, chan_id, 0);
  199. etm_ll_channel_set_task(group->hal.regs, chan_id, 0);
  200. ESP_LOGD(TAG, "del etm channel (%d,%d)", group_id, chan_id);
  201. // recycle memory resource
  202. ESP_RETURN_ON_ERROR(etm_chan_destroy(chan), TAG, "destroy etm channel failed");
  203. return ESP_OK;
  204. }
  205. esp_err_t esp_etm_channel_enable(esp_etm_channel_handle_t chan)
  206. {
  207. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  208. ESP_RETURN_ON_FALSE(chan->fsm == ETM_CHAN_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "channel is not in init state");
  209. etm_group_t *group = chan->group;
  210. etm_ll_enable_channel(group->hal.regs, chan->chan_id);
  211. chan->fsm = ETM_CHAN_FSM_ENABLE;
  212. return ESP_OK;
  213. }
  214. esp_err_t esp_etm_channel_disable(esp_etm_channel_handle_t chan)
  215. {
  216. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  217. ESP_RETURN_ON_FALSE(chan->fsm == ETM_CHAN_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "channel not in enable state");
  218. etm_group_t *group = chan->group;
  219. etm_ll_disable_channel(group->hal.regs, chan->chan_id);
  220. chan->fsm = ETM_CHAN_FSM_INIT;
  221. return ESP_OK;
  222. }
  223. esp_err_t esp_etm_channel_connect(esp_etm_channel_handle_t chan, esp_etm_event_handle_t event, esp_etm_task_handle_t task)
  224. {
  225. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  226. etm_group_t *group = chan->group;
  227. uint32_t event_id = 0;
  228. uint32_t task_id = 0;
  229. // if the event/task is NULL, then the channel will disconnect from the event/task
  230. if (event) {
  231. event_id = event->event_id;
  232. }
  233. if (task) {
  234. task_id = task->task_id;
  235. }
  236. etm_ll_channel_set_event(group->hal.regs, chan->chan_id, event_id);
  237. etm_ll_channel_set_task(group->hal.regs, chan->chan_id, task_id);
  238. chan->event = event;
  239. chan->task = task;
  240. ESP_LOGD(TAG, "event %"PRIu32" => channel %d", event_id, chan->chan_id);
  241. ESP_LOGD(TAG, "channel %d => task %"PRIu32, chan->chan_id, task_id);
  242. return ESP_OK;
  243. }
  244. esp_err_t esp_etm_del_event(esp_etm_event_handle_t event)
  245. {
  246. ESP_RETURN_ON_FALSE(event, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  247. return event->del(event);
  248. }
  249. esp_err_t esp_etm_del_task(esp_etm_task_handle_t task)
  250. {
  251. ESP_RETURN_ON_FALSE(task, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  252. return task->del(task);
  253. }
  254. esp_err_t esp_etm_dump(FILE *out_stream)
  255. {
  256. etm_group_t *group = NULL;
  257. esp_etm_channel_handle_t etm_chan = NULL;
  258. ESP_RETURN_ON_FALSE(out_stream, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  259. fprintf(out_stream, "===========ETM Dump Start==========\r\n");
  260. char line[80];
  261. size_t len = sizeof(line);
  262. for (int i = 0; i < SOC_ETM_GROUPS; i++) {
  263. group = etm_acquire_group_handle(i);
  264. ESP_RETURN_ON_FALSE(group, ESP_ERR_NO_MEM, TAG, "no mem for group (%d)", i);
  265. etm_hal_context_t *hal = &group->hal;
  266. for (int j = 0; j < SOC_ETM_CHANNELS_PER_GROUP; j++) {
  267. bool print_line = true;
  268. portENTER_CRITICAL(&group->spinlock);
  269. etm_chan = group->chans[j];
  270. if (etm_ll_is_channel_enabled(hal->regs, j)) {
  271. if (!etm_chan) {
  272. // in case the etm driver is bypassed and some channel is enabled in another way (e.g. by hal driver)
  273. snprintf(line, len, "channel %d is enabled but not recorded\r\n", j);
  274. } else {
  275. // print which event and task the channel is connected to
  276. snprintf(line, len, "channel %d: event %"PRIu32" ==> task %"PRIu32"\r\n", j,
  277. etm_chan->event ? etm_chan->event->event_id : 0,
  278. etm_chan->task ? etm_chan->task->task_id : 0);
  279. }
  280. } else {
  281. if (etm_chan) {
  282. // channel is created, but not enabled by `esp_etm_channel_enable` yet
  283. snprintf(line, len, "channel %d is created but not enabled\r\n", j);
  284. } else {
  285. // a free channel, don't print anything
  286. print_line = false;
  287. }
  288. }
  289. portEXIT_CRITICAL(&group->spinlock);
  290. if (print_line) {
  291. fputs(line, out_stream);
  292. }
  293. }
  294. etm_release_group_handle(group);
  295. }
  296. fprintf(out_stream, "===========ETM Dump End============\r\n");
  297. return ESP_OK;
  298. }