ccomp_timer_impl.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "esp_freertos_hooks.h"
  23. #include "perfmon.h"
  24. #include "xtensa/core-macros.h"
  25. #include "xtensa/xt_perf_consts.h"
  26. #include "xtensa-debug-module.h"
  27. #include "esp_ipc.h"
  28. #if CONFIG_IDF_TARGET_ESP32
  29. #include "esp32/clk.h"
  30. #elif CONFIG_IDF_TARGET_ESP32S2
  31. #include "esp32s2/clk.h"
  32. #endif
  33. #define D_STALL_COUNTER_ID 0
  34. #define I_STALL_COUNTER_ID 1
  35. typedef enum
  36. {
  37. PERF_TIMER_UNINIT = 0, // timer has not been initialized yet
  38. PERF_TIMER_IDLE, // timer has been initialized but is not tracking elapsed time
  39. PERF_TIMER_ACTIVE // timer is tracking elapsed time
  40. } ccomp_timer_state_t;
  41. typedef struct
  42. {
  43. int i_ovfl; // number of times instruction stall counter has overflowed
  44. int d_ovfl; // number of times data stall counter has overflowed
  45. uint32_t last_ccount; // last CCOUNT value, updated every os tick
  46. ccomp_timer_state_t state; // state of the timer
  47. intr_handle_t intr_handle; // handle to allocated handler for perfmon counter overflows, so that it can be freed during deinit
  48. int64_t ccount; // accumulated processors cycles during the time when timer is active
  49. } ccomp_timer_status_t;
  50. // Each core has its independent timer
  51. ccomp_timer_status_t s_status[] = {
  52. (ccomp_timer_status_t){
  53. .i_ovfl = 0,
  54. .d_ovfl = 0,
  55. .ccount = 0,
  56. .last_ccount = 0,
  57. .state = PERF_TIMER_UNINIT,
  58. .intr_handle = NULL,
  59. },
  60. (ccomp_timer_status_t){
  61. .i_ovfl = 0,
  62. .d_ovfl = 0,
  63. .ccount = 0,
  64. .last_ccount = 0,
  65. .state = PERF_TIMER_UNINIT,
  66. .intr_handle = NULL
  67. }
  68. };
  69. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  70. static void IRAM_ATTR update_ccount(void)
  71. {
  72. if (s_status[xPortGetCoreID()].state == PERF_TIMER_ACTIVE) {
  73. int64_t new_ccount = xthal_get_ccount();
  74. if (new_ccount > s_status[xPortGetCoreID()].last_ccount) {
  75. s_status[xPortGetCoreID()].ccount += new_ccount - s_status[xPortGetCoreID()].last_ccount;
  76. } else {
  77. // CCOUNT has wrapped around
  78. s_status[xPortGetCoreID()].ccount += new_ccount + (UINT32_MAX - s_status[xPortGetCoreID()].last_ccount);
  79. }
  80. s_status[xPortGetCoreID()].last_ccount = new_ccount;
  81. }
  82. }
  83. static void inline update_overflow(int id, int *cnt)
  84. {
  85. uint32_t pmstat = eri_read(ERI_PERFMON_PMSTAT0 + id * sizeof(int32_t));
  86. if (pmstat & PMSTAT_OVFL) {
  87. *cnt += 1;
  88. // Clear overflow and PerfMonInt asserted bits. The only valid bits in PMSTAT is the ones we're trying to clear. So it should be
  89. // ok to just modify the whole register.
  90. eri_write(ERI_PERFMON_PMSTAT0 + id, ~0x0);
  91. }
  92. }
  93. static void IRAM_ATTR perf_counter_overflow_handler(void *args)
  94. {
  95. update_overflow(D_STALL_COUNTER_ID, &s_status[xPortGetCoreID()].d_ovfl);
  96. update_overflow(I_STALL_COUNTER_ID, &s_status[xPortGetCoreID()].i_ovfl);
  97. }
  98. static void set_perfmon_interrupt(bool enable)
  99. {
  100. uint32_t d_pmctrl = eri_read(ERI_PERFMON_PMCTRL0 + D_STALL_COUNTER_ID * sizeof(int32_t));
  101. uint32_t i_pmctrl = eri_read(ERI_PERFMON_PMCTRL0 + I_STALL_COUNTER_ID * sizeof(int32_t));
  102. if (enable) {
  103. d_pmctrl |= PMCTRL_INTEN;
  104. i_pmctrl |= PMCTRL_INTEN;
  105. }
  106. else {
  107. d_pmctrl &= ~PMCTRL_INTEN;
  108. i_pmctrl &= ~PMCTRL_INTEN;
  109. }
  110. eri_write(ERI_PERFMON_PMCTRL0 + D_STALL_COUNTER_ID * sizeof(int32_t), d_pmctrl);
  111. eri_write(ERI_PERFMON_PMCTRL0 + I_STALL_COUNTER_ID * sizeof(int32_t), i_pmctrl);
  112. }
  113. esp_err_t ccomp_timer_impl_init(void)
  114. {
  115. // Keep track of how many times each counter has overflowed.
  116. esp_err_t err = esp_intr_alloc(ETS_INTERNAL_PROFILING_INTR_SOURCE, 0,
  117. perf_counter_overflow_handler, NULL, &s_status[xPortGetCoreID()].intr_handle);
  118. if (err != ESP_OK) {
  119. return err;
  120. }
  121. xtensa_perfmon_init(D_STALL_COUNTER_ID,
  122. XTPERF_CNT_D_STALL,
  123. XTPERF_MASK_D_STALL_BUSY, 0, -1);
  124. xtensa_perfmon_init(I_STALL_COUNTER_ID,
  125. XTPERF_CNT_I_STALL,
  126. XTPERF_MASK_I_STALL_BUSY, 0, -1);
  127. set_perfmon_interrupt(true);
  128. s_status[xPortGetCoreID()].state = PERF_TIMER_IDLE;
  129. return ESP_OK;
  130. }
  131. esp_err_t ccomp_timer_impl_deinit(void)
  132. {
  133. set_perfmon_interrupt(false);
  134. esp_err_t err = esp_intr_free(s_status[xPortGetCoreID()].intr_handle);
  135. if (err != ESP_OK) {
  136. return err;
  137. }
  138. s_status[xPortGetCoreID()].intr_handle = NULL;
  139. s_status[xPortGetCoreID()].state = PERF_TIMER_UNINIT;
  140. return ESP_OK;
  141. }
  142. esp_err_t ccomp_timer_impl_start(void)
  143. {
  144. s_status[xPortGetCoreID()].state = PERF_TIMER_ACTIVE;
  145. s_status[xPortGetCoreID()].last_ccount = xthal_get_ccount();
  146. // Update elapsed cycles every OS tick
  147. esp_register_freertos_tick_hook_for_cpu(update_ccount, xPortGetCoreID());
  148. xtensa_perfmon_start();
  149. return ESP_OK;
  150. }
  151. esp_err_t IRAM_ATTR ccomp_timer_impl_stop(void)
  152. {
  153. xtensa_perfmon_stop();
  154. esp_deregister_freertos_tick_hook_for_cpu(update_ccount, xPortGetCoreID());
  155. update_ccount();
  156. s_status[xPortGetCoreID()].state = PERF_TIMER_IDLE;
  157. return ESP_OK;
  158. }
  159. int64_t IRAM_ATTR ccomp_timer_impl_get_time(void)
  160. {
  161. update_ccount();
  162. int64_t d_stalls = xtensa_perfmon_value(D_STALL_COUNTER_ID) +
  163. s_status[xPortGetCoreID()].d_ovfl * (1 << sizeof(int32_t));
  164. int64_t i_stalls = xtensa_perfmon_value(I_STALL_COUNTER_ID) +
  165. s_status[xPortGetCoreID()].i_ovfl * (1 << sizeof(int32_t));
  166. int64_t stalls = d_stalls + i_stalls;
  167. int64_t cycles = s_status[xPortGetCoreID()].ccount;
  168. return ((cycles - stalls) * 1000000) / esp_clk_cpu_freq();
  169. }
  170. esp_err_t ccomp_timer_impl_reset(void)
  171. {
  172. xtensa_perfmon_reset(D_STALL_COUNTER_ID);
  173. xtensa_perfmon_reset(I_STALL_COUNTER_ID);
  174. s_status[xPortGetCoreID()].d_ovfl = 0;
  175. s_status[xPortGetCoreID()].i_ovfl = 0;
  176. s_status[xPortGetCoreID()].ccount = 0;
  177. s_status[xPortGetCoreID()].last_ccount = 0;
  178. return ESP_OK;
  179. }
  180. bool ccomp_timer_impl_is_init(void)
  181. {
  182. return s_status[xPortGetCoreID()].state != PERF_TIMER_UNINIT;
  183. }
  184. bool IRAM_ATTR ccomp_timer_impl_is_active(void)
  185. {
  186. return s_status[xPortGetCoreID()].state == PERF_TIMER_ACTIVE;
  187. }
  188. void IRAM_ATTR ccomp_timer_impl_lock(void)
  189. {
  190. portENTER_CRITICAL(&s_lock);
  191. }
  192. void IRAM_ATTR ccomp_timer_impl_unlock(void)
  193. {
  194. portEXIT_CRITICAL(&s_lock);
  195. }