ccomp_timer_impl_xtensa.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #if CONFIG_IDF_TARGET_ESP32
  28. #include "esp32/clk.h"
  29. #elif CONFIG_IDF_TARGET_ESP32S2
  30. #include "esp32s2/clk.h"
  31. #elif CONFIG_IDF_TARGET_ESP32S3
  32. #include "esp32s3/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. esp_err_t ccomp_timer_impl_init(void)
  115. {
  116. // Keep track of how many times each counter has overflowed.
  117. esp_err_t err = esp_intr_alloc(ETS_INTERNAL_PROFILING_INTR_SOURCE, 0,
  118. perf_counter_overflow_handler, NULL, &s_status[xPortGetCoreID()].intr_handle);
  119. if (err != ESP_OK) {
  120. return err;
  121. }
  122. xtensa_perfmon_init(D_STALL_COUNTER_ID,
  123. XTPERF_CNT_D_STALL,
  124. XTPERF_MASK_D_STALL_BUSY, 0, -1);
  125. xtensa_perfmon_init(I_STALL_COUNTER_ID,
  126. XTPERF_CNT_I_STALL,
  127. XTPERF_MASK_I_STALL_BUSY, 0, -1);
  128. set_perfmon_interrupt(true);
  129. s_status[xPortGetCoreID()].state = PERF_TIMER_IDLE;
  130. return ESP_OK;
  131. }
  132. esp_err_t ccomp_timer_impl_deinit(void)
  133. {
  134. set_perfmon_interrupt(false);
  135. esp_err_t err = esp_intr_free(s_status[xPortGetCoreID()].intr_handle);
  136. if (err != ESP_OK) {
  137. return err;
  138. }
  139. s_status[xPortGetCoreID()].intr_handle = NULL;
  140. s_status[xPortGetCoreID()].state = PERF_TIMER_UNINIT;
  141. return ESP_OK;
  142. }
  143. esp_err_t ccomp_timer_impl_start(void)
  144. {
  145. s_status[xPortGetCoreID()].state = PERF_TIMER_ACTIVE;
  146. s_status[xPortGetCoreID()].last_ccount = xthal_get_ccount();
  147. // Update elapsed cycles every OS tick
  148. esp_register_freertos_tick_hook_for_cpu(update_ccount, xPortGetCoreID());
  149. xtensa_perfmon_start();
  150. return ESP_OK;
  151. }
  152. esp_err_t IRAM_ATTR ccomp_timer_impl_stop(void)
  153. {
  154. xtensa_perfmon_stop();
  155. esp_deregister_freertos_tick_hook_for_cpu(update_ccount, xPortGetCoreID());
  156. update_ccount();
  157. s_status[xPortGetCoreID()].state = PERF_TIMER_IDLE;
  158. return ESP_OK;
  159. }
  160. int64_t IRAM_ATTR ccomp_timer_impl_get_time(void)
  161. {
  162. update_ccount();
  163. int64_t d_stalls = xtensa_perfmon_value(D_STALL_COUNTER_ID) +
  164. s_status[xPortGetCoreID()].d_ovfl * (1 << sizeof(int32_t));
  165. int64_t i_stalls = xtensa_perfmon_value(I_STALL_COUNTER_ID) +
  166. s_status[xPortGetCoreID()].i_ovfl * (1 << sizeof(int32_t));
  167. int64_t stalls = d_stalls + i_stalls;
  168. int64_t cycles = s_status[xPortGetCoreID()].ccount;
  169. return ((cycles - stalls) * 1000000) / esp_clk_cpu_freq();
  170. }
  171. esp_err_t ccomp_timer_impl_reset(void)
  172. {
  173. xtensa_perfmon_reset(D_STALL_COUNTER_ID);
  174. xtensa_perfmon_reset(I_STALL_COUNTER_ID);
  175. s_status[xPortGetCoreID()].d_ovfl = 0;
  176. s_status[xPortGetCoreID()].i_ovfl = 0;
  177. s_status[xPortGetCoreID()].ccount = 0;
  178. s_status[xPortGetCoreID()].last_ccount = 0;
  179. return ESP_OK;
  180. }
  181. bool ccomp_timer_impl_is_init(void)
  182. {
  183. return s_status[xPortGetCoreID()].state != PERF_TIMER_UNINIT;
  184. }
  185. bool IRAM_ATTR ccomp_timer_impl_is_active(void)
  186. {
  187. return s_status[xPortGetCoreID()].state == PERF_TIMER_ACTIVE;
  188. }
  189. void IRAM_ATTR ccomp_timer_impl_lock(void)
  190. {
  191. portENTER_CRITICAL(&s_lock);
  192. }
  193. void IRAM_ATTR ccomp_timer_impl_unlock(void)
  194. {
  195. portEXIT_CRITICAL(&s_lock);
  196. }