ccomp_timer_impl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include "ccomp_timer_impl.h"
  17. #include "esp_intr_alloc.h"
  18. #include "esp_log.h"
  19. #include "esp_attr.h"
  20. #include "eri.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/portmacro.h"
  23. #include "esp_freertos_hooks.h"
  24. #include "perfmon.h"
  25. #include "xtensa/core-macros.h"
  26. #include "xtensa/xt_perf_consts.h"
  27. #include "xtensa-debug-module.h"
  28. #include "esp_ipc.h"
  29. #if CONFIG_IDF_TARGET_ESP32
  30. #include "esp32/clk.h"
  31. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  32. #include "esp32s2beta/clk.h"
  33. #endif
  34. #define D_STALL_COUNTER_ID 0
  35. #define I_STALL_COUNTER_ID 1
  36. typedef enum
  37. {
  38. PERF_TIMER_UNINIT = 0, // timer has not been initialized yet
  39. PERF_TIMER_IDLE, // timer has been initialized but is not tracking elapsed time
  40. PERF_TIMER_ACTIVE // timer is tracking elapsed time
  41. } ccomp_timer_state_t;
  42. typedef struct
  43. {
  44. int i_ovfl; // number of times instruction stall counter has overflowed
  45. int d_ovfl; // number of times data stall counter has overflowed
  46. uint32_t last_ccount; // last CCOUNT value, updated every os tick
  47. ccomp_timer_state_t state; // state of the timer
  48. intr_handle_t intr_handle; // handle to allocated handler for perfmon counter overflows, so that it can be freed during deinit
  49. int64_t ccount; // accumulated processors cycles during the time when timer is active
  50. } ccomp_timer_status_t;
  51. // Each core has its independent timer
  52. ccomp_timer_status_t s_status[] = {
  53. (ccomp_timer_status_t){
  54. .i_ovfl = 0,
  55. .d_ovfl = 0,
  56. .ccount = 0,
  57. .last_ccount = 0,
  58. .state = PERF_TIMER_UNINIT,
  59. .intr_handle = NULL,
  60. },
  61. (ccomp_timer_status_t){
  62. .i_ovfl = 0,
  63. .d_ovfl = 0,
  64. .ccount = 0,
  65. .last_ccount = 0,
  66. .state = PERF_TIMER_UNINIT,
  67. .intr_handle = NULL
  68. }
  69. };
  70. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  71. static void IRAM_ATTR update_ccount(void)
  72. {
  73. if (s_status[xPortGetCoreID()].state == PERF_TIMER_ACTIVE) {
  74. int64_t new_ccount = xthal_get_ccount();
  75. if (new_ccount > s_status[xPortGetCoreID()].last_ccount) {
  76. s_status[xPortGetCoreID()].ccount += new_ccount - s_status[xPortGetCoreID()].last_ccount;
  77. } else {
  78. // CCOUNT has wrapped around
  79. s_status[xPortGetCoreID()].ccount += new_ccount + (UINT32_MAX - s_status[xPortGetCoreID()].last_ccount);
  80. }
  81. s_status[xPortGetCoreID()].last_ccount = new_ccount;
  82. }
  83. }
  84. static void inline update_overflow(int id, int *cnt)
  85. {
  86. uint32_t pmstat = eri_read(ERI_PERFMON_PMSTAT0 + id * sizeof(int32_t));
  87. if (pmstat & PMSTAT_OVFL) {
  88. *cnt += 1;
  89. // Clear overflow and PerfMonInt asserted bits. The only valid bits in PMSTAT is the ones we're trying to clear. So it should be
  90. // ok to just modify the whole register.
  91. eri_write(ERI_PERFMON_PMSTAT0 + id, ~0x0);
  92. }
  93. }
  94. static void IRAM_ATTR perf_counter_overflow_handler(void *args)
  95. {
  96. update_overflow(D_STALL_COUNTER_ID, &s_status[xPortGetCoreID()].d_ovfl);
  97. update_overflow(I_STALL_COUNTER_ID, &s_status[xPortGetCoreID()].i_ovfl);
  98. }
  99. static void set_perfmon_interrupt(bool enable)
  100. {
  101. uint32_t d_pmctrl = eri_read(ERI_PERFMON_PMCTRL0 + D_STALL_COUNTER_ID * sizeof(int32_t));
  102. uint32_t i_pmctrl = eri_read(ERI_PERFMON_PMCTRL0 + I_STALL_COUNTER_ID * sizeof(int32_t));
  103. if (enable) {
  104. d_pmctrl |= PMCTRL_INTEN;
  105. i_pmctrl |= PMCTRL_INTEN;
  106. }
  107. else {
  108. d_pmctrl &= ~PMCTRL_INTEN;
  109. i_pmctrl &= ~PMCTRL_INTEN;
  110. }
  111. eri_write(ERI_PERFMON_PMCTRL0 + D_STALL_COUNTER_ID * sizeof(int32_t), d_pmctrl);
  112. eri_write(ERI_PERFMON_PMCTRL0 + I_STALL_COUNTER_ID * sizeof(int32_t), i_pmctrl);
  113. }
  114. static void intr_alloc(void* params)
  115. {
  116. int *id = (int*) params;
  117. // Keep track of how many times each counter has overflowed.
  118. esp_intr_alloc(ETS_INTERNAL_PROFILING_INTR_SOURCE, 0,
  119. perf_counter_overflow_handler, NULL, &s_status[*id].intr_handle);
  120. }
  121. // Linker seems to be smart enough to drop this if ccomp_timer APIs are not used.
  122. static __attribute__((constructor)) void ccomp_timer_impl_start_init(void)
  123. {
  124. #if !CONFIG_FREERTOS_UNICORE
  125. for (int i = 0; i < portNUM_PROCESSORS; i++) {
  126. esp_ipc_call_blocking(i, intr_alloc, &i);
  127. }
  128. #else
  129. int i = 0;
  130. intr_alloc(&i);
  131. #endif
  132. }
  133. esp_err_t ccomp_timer_impl_init(void)
  134. {
  135. xtensa_perfmon_init(D_STALL_COUNTER_ID,
  136. XTPERF_CNT_D_STALL,
  137. XTPERF_MASK_D_STALL_BUSY, 0, -1);
  138. xtensa_perfmon_init(I_STALL_COUNTER_ID,
  139. XTPERF_CNT_I_STALL,
  140. XTPERF_MASK_I_STALL_BUSY, 0, -1);
  141. set_perfmon_interrupt(true);
  142. s_status[xPortGetCoreID()].state = PERF_TIMER_IDLE;
  143. return ESP_OK;
  144. }
  145. esp_err_t ccomp_timer_impl_deinit(void)
  146. {
  147. set_perfmon_interrupt(false);
  148. s_status[xPortGetCoreID()].intr_handle = NULL;
  149. s_status[xPortGetCoreID()].state = PERF_TIMER_UNINIT;
  150. return ESP_OK;
  151. }
  152. esp_err_t ccomp_timer_impl_start(void)
  153. {
  154. s_status[xPortGetCoreID()].state = PERF_TIMER_ACTIVE;
  155. s_status[xPortGetCoreID()].last_ccount = xthal_get_ccount();
  156. // Update elapsed cycles every OS tick
  157. esp_register_freertos_tick_hook_for_cpu(update_ccount, xPortGetCoreID());
  158. xtensa_perfmon_start();
  159. return ESP_OK;
  160. }
  161. esp_err_t IRAM_ATTR ccomp_timer_impl_stop(void)
  162. {
  163. xtensa_perfmon_stop();
  164. esp_deregister_freertos_tick_hook_for_cpu(update_ccount, xPortGetCoreID());
  165. update_ccount();
  166. s_status[xPortGetCoreID()].state = PERF_TIMER_IDLE;
  167. return ESP_OK;
  168. }
  169. int64_t IRAM_ATTR ccomp_timer_impl_get_time(void)
  170. {
  171. update_ccount();
  172. int64_t d_stalls = xtensa_perfmon_value(D_STALL_COUNTER_ID) +
  173. s_status[xPortGetCoreID()].d_ovfl * (1 << sizeof(int32_t));
  174. int64_t i_stalls = xtensa_perfmon_value(I_STALL_COUNTER_ID) +
  175. s_status[xPortGetCoreID()].i_ovfl * (1 << sizeof(int32_t));
  176. int64_t stalls = d_stalls + i_stalls;
  177. int64_t cycles = s_status[xPortGetCoreID()].ccount;
  178. return ((cycles - stalls) * 1000000) / esp_clk_cpu_freq();
  179. }
  180. esp_err_t ccomp_timer_impl_reset(void)
  181. {
  182. xtensa_perfmon_reset(D_STALL_COUNTER_ID);
  183. xtensa_perfmon_reset(I_STALL_COUNTER_ID);
  184. s_status[xPortGetCoreID()].d_ovfl = 0;
  185. s_status[xPortGetCoreID()].i_ovfl = 0;
  186. s_status[xPortGetCoreID()].ccount = 0;
  187. s_status[xPortGetCoreID()].last_ccount = 0;
  188. return ESP_OK;
  189. }
  190. bool ccomp_timer_impl_is_init(void)
  191. {
  192. return s_status[xPortGetCoreID()].state != PERF_TIMER_UNINIT;
  193. }
  194. bool IRAM_ATTR ccomp_timer_impl_is_active(void)
  195. {
  196. return s_status[xPortGetCoreID()].state == PERF_TIMER_ACTIVE;
  197. }
  198. void IRAM_ATTR ccomp_timer_impl_lock(void)
  199. {
  200. portENTER_CRITICAL(&s_lock);
  201. }
  202. void IRAM_ATTR ccomp_timer_impl_unlock(void)
  203. {
  204. portEXIT_CRITICAL(&s_lock);
  205. }