cache_utils.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2015-2016 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 <stdlib.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <freertos/FreeRTOS.h>
  19. #include <freertos/task.h>
  20. #include <freertos/semphr.h>
  21. #include <rom/spi_flash.h>
  22. #include <rom/cache.h>
  23. #include <soc/soc.h>
  24. #include <soc/dport_reg.h>
  25. #include "sdkconfig.h"
  26. #include "esp_ipc.h"
  27. #include "esp_attr.h"
  28. #include "esp_intr_alloc.h"
  29. #include "esp_spi_flash.h"
  30. #include "esp_log.h"
  31. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state);
  32. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
  33. static uint32_t s_flash_op_cache_state[2];
  34. #ifndef CONFIG_FREERTOS_UNICORE
  35. static SemaphoreHandle_t s_flash_op_mutex;
  36. static volatile bool s_flash_op_can_start = false;
  37. static volatile bool s_flash_op_complete = false;
  38. #ifndef NDEBUG
  39. static volatile int s_flash_op_cpu = -1;
  40. #endif
  41. void spi_flash_init_lock()
  42. {
  43. s_flash_op_mutex = xSemaphoreCreateMutex();
  44. }
  45. void spi_flash_op_lock()
  46. {
  47. xSemaphoreTake(s_flash_op_mutex, portMAX_DELAY);
  48. }
  49. void spi_flash_op_unlock()
  50. {
  51. xSemaphoreGive(s_flash_op_mutex);
  52. }
  53. void IRAM_ATTR spi_flash_op_block_func(void* arg)
  54. {
  55. // Disable scheduler on this CPU
  56. vTaskSuspendAll();
  57. // Restore interrupts that aren't located in IRAM
  58. esp_intr_noniram_disable();
  59. uint32_t cpuid = (uint32_t) arg;
  60. // Disable cache so that flash operation can start
  61. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  62. s_flash_op_can_start = true;
  63. while (!s_flash_op_complete) {
  64. // until we have a way to use interrupts for inter-CPU communication,
  65. // busy loop here and wait for the other CPU to finish flash operation
  66. }
  67. // Flash operation is complete, re-enable cache
  68. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  69. // Restore interrupts that aren't located in IRAM
  70. esp_intr_noniram_enable();
  71. // Re-enable scheduler
  72. xTaskResumeAll();
  73. }
  74. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
  75. {
  76. spi_flash_op_lock();
  77. const uint32_t cpuid = xPortGetCoreID();
  78. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  79. #ifndef NDEBUG
  80. // For sanity check later: record the CPU which has started doing flash operation
  81. assert(s_flash_op_cpu == -1);
  82. s_flash_op_cpu = cpuid;
  83. #endif
  84. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  85. // Scheduler hasn't been started yet, it means that spi_flash API is being
  86. // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
  87. // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
  88. // which is in IRAM. So it is safe to disable cache for the other_cpuid here.
  89. assert(other_cpuid == 1);
  90. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  91. } else {
  92. // Signal to the spi_flash_op_block_task on the other CPU that we need it to
  93. // disable cache there and block other tasks from executing.
  94. s_flash_op_can_start = false;
  95. s_flash_op_complete = false;
  96. esp_err_t ret = esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void*) other_cpuid);
  97. assert(ret == ESP_OK);
  98. while (!s_flash_op_can_start) {
  99. // Busy loop and wait for spi_flash_op_block_func to disable cache
  100. // on the other CPU
  101. }
  102. // Disable scheduler on the current CPU
  103. vTaskSuspendAll();
  104. // This is guaranteed to run on CPU <cpuid> because the other CPU is now
  105. // occupied by highest priority task
  106. assert(xPortGetCoreID() == cpuid);
  107. }
  108. // Kill interrupts that aren't located in IRAM
  109. esp_intr_noniram_disable();
  110. // Disable cache on this CPU as well
  111. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  112. }
  113. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
  114. {
  115. const uint32_t cpuid = xPortGetCoreID();
  116. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  117. #ifndef NDEBUG
  118. // Sanity check: flash operation ends on the same CPU as it has started
  119. assert(cpuid == s_flash_op_cpu);
  120. s_flash_op_cpu = -1;
  121. #endif
  122. // Re-enable cache on this CPU
  123. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  124. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  125. // Scheduler is not running yet — this means we are running on PRO CPU.
  126. // other_cpuid is APP CPU, and it is either in reset or is spinning in
  127. // user_start_cpu1, which is in IRAM. So we can simply reenable cache.
  128. assert(other_cpuid == 1);
  129. spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
  130. } else {
  131. // Signal to spi_flash_op_block_task that flash operation is complete
  132. s_flash_op_complete = true;
  133. }
  134. // Re-enable non-iram interrupts
  135. esp_intr_noniram_enable();
  136. // Resume tasks on the current CPU, if the scheduler has started.
  137. // NOTE: enabling non-IRAM interrupts has to happen before this,
  138. // because once the scheduler has started, due to preemption the
  139. // current task can end up being moved to the other CPU.
  140. // But esp_intr_noniram_enable has to be called on the same CPU which
  141. // called esp_intr_noniram_disable
  142. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  143. xTaskResumeAll();
  144. }
  145. // Release API lock
  146. spi_flash_op_unlock();
  147. }
  148. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os()
  149. {
  150. const uint32_t cpuid = xPortGetCoreID();
  151. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  152. // do not care about other CPU, it was halted upon entering panic handler
  153. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  154. // Kill interrupts that aren't located in IRAM
  155. esp_intr_noniram_disable();
  156. // Disable cache on this CPU as well
  157. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  158. }
  159. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os()
  160. {
  161. const uint32_t cpuid = xPortGetCoreID();
  162. // Re-enable cache on this CPU
  163. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  164. // Re-enable non-iram interrupts
  165. esp_intr_noniram_enable();
  166. }
  167. #else // CONFIG_FREERTOS_UNICORE
  168. void spi_flash_init_lock()
  169. {
  170. }
  171. void spi_flash_op_lock()
  172. {
  173. vTaskSuspendAll();
  174. }
  175. void spi_flash_op_unlock()
  176. {
  177. xTaskResumeAll();
  178. }
  179. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
  180. {
  181. spi_flash_op_lock();
  182. esp_intr_noniram_disable();
  183. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  184. }
  185. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
  186. {
  187. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  188. esp_intr_noniram_enable();
  189. spi_flash_op_unlock();
  190. }
  191. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os()
  192. {
  193. // Kill interrupts that aren't located in IRAM
  194. esp_intr_noniram_disable();
  195. // Disable cache on this CPU as well
  196. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  197. }
  198. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os()
  199. {
  200. // Re-enable cache on this CPU
  201. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  202. // Re-enable non-iram interrupts
  203. esp_intr_noniram_enable();
  204. }
  205. #endif // CONFIG_FREERTOS_UNICORE
  206. /**
  207. * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
  208. * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
  209. * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
  210. */
  211. static const uint32_t cache_mask = DPORT_APP_CACHE_MASK_OPSDRAM | DPORT_APP_CACHE_MASK_DROM0 |
  212. DPORT_APP_CACHE_MASK_DRAM1 | DPORT_APP_CACHE_MASK_IROM0 |
  213. DPORT_APP_CACHE_MASK_IRAM1 | DPORT_APP_CACHE_MASK_IRAM0;
  214. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state)
  215. {
  216. uint32_t ret = 0;
  217. if (cpuid == 0) {
  218. ret |= GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
  219. while (GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
  220. ;
  221. }
  222. SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  223. } else {
  224. ret |= GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
  225. while (GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
  226. ;
  227. }
  228. SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  229. }
  230. *saved_state = ret;
  231. }
  232. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  233. {
  234. if (cpuid == 0) {
  235. SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  236. SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  237. } else {
  238. SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  239. SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  240. }
  241. }
  242. IRAM_ATTR bool spi_flash_cache_enabled()
  243. {
  244. return REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE)
  245. && REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE);
  246. }