cache_utils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. assert(s_flash_op_mutex != NULL);
  45. }
  46. void spi_flash_op_lock()
  47. {
  48. xSemaphoreTake(s_flash_op_mutex, portMAX_DELAY);
  49. }
  50. void spi_flash_op_unlock()
  51. {
  52. xSemaphoreGive(s_flash_op_mutex);
  53. }
  54. /*
  55. If you're going to modify this, keep in mind that while the flash caches of the pro and app
  56. cpu are separate, the psram cache is *not*. If one of the CPUs returns from a flash routine
  57. with its cache enabled but the other CPUs cache is not enabled yet, you will have problems
  58. when accessing psram from the former CPU.
  59. */
  60. void IRAM_ATTR spi_flash_op_block_func(void* arg)
  61. {
  62. // Disable scheduler on this CPU
  63. vTaskSuspendAll();
  64. // Restore interrupts that aren't located in IRAM
  65. esp_intr_noniram_disable();
  66. uint32_t cpuid = (uint32_t) arg;
  67. // s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
  68. // CPU may reset the flag back to false before IPC task has a chance to check it
  69. // (if it is preempted by an ISR taking non-trivial amount of time)
  70. s_flash_op_complete = false;
  71. s_flash_op_can_start = true;
  72. while (!s_flash_op_complete) {
  73. // busy loop here and wait for the other CPU to finish flash operation
  74. }
  75. // Flash operation is complete, re-enable cache
  76. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  77. // Restore interrupts that aren't located in IRAM
  78. esp_intr_noniram_enable();
  79. // Re-enable scheduler
  80. xTaskResumeAll();
  81. }
  82. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
  83. {
  84. spi_flash_op_lock();
  85. const uint32_t cpuid = xPortGetCoreID();
  86. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  87. #ifndef NDEBUG
  88. // For sanity check later: record the CPU which has started doing flash operation
  89. assert(s_flash_op_cpu == -1);
  90. s_flash_op_cpu = cpuid;
  91. #endif
  92. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  93. // Scheduler hasn't been started yet, it means that spi_flash API is being
  94. // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
  95. // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
  96. // which is in IRAM. So it is safe to disable cache for the other_cpuid here.
  97. assert(other_cpuid == 1);
  98. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  99. } else {
  100. // Signal to the spi_flash_op_block_task on the other CPU that we need it to
  101. // disable cache there and block other tasks from executing.
  102. s_flash_op_can_start = false;
  103. esp_err_t ret = esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void*) other_cpuid);
  104. assert(ret == ESP_OK);
  105. while (!s_flash_op_can_start) {
  106. // Busy loop and wait for spi_flash_op_block_func to disable cache
  107. // on the other CPU
  108. }
  109. // Disable scheduler on the current CPU
  110. vTaskSuspendAll();
  111. // This is guaranteed to run on CPU <cpuid> because the other CPU is now
  112. // occupied by highest priority task
  113. assert(xPortGetCoreID() == cpuid);
  114. }
  115. // Kill interrupts that aren't located in IRAM
  116. esp_intr_noniram_disable();
  117. // This CPU executes this routine, with non-IRAM interrupts and the scheduler
  118. // disabled. The other CPU is spinning in the spi_flash_op_block_func task, also
  119. // with non-iram interrupts and the scheduler disabled. None of these CPUs will
  120. // touch external RAM or flash this way, so we can safely disable caches.
  121. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  122. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  123. }
  124. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
  125. {
  126. const uint32_t cpuid = xPortGetCoreID();
  127. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  128. #ifndef NDEBUG
  129. // Sanity check: flash operation ends on the same CPU as it has started
  130. assert(cpuid == s_flash_op_cpu);
  131. // More sanity check: if scheduler isn't started, only CPU0 can call this.
  132. assert(!(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED && cpuid != 0));
  133. s_flash_op_cpu = -1;
  134. #endif
  135. // Re-enable cache on both CPUs. After this, cache (flash and external RAM) should work again.
  136. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  137. spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
  138. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  139. // Signal to spi_flash_op_block_task that flash operation is complete
  140. s_flash_op_complete = true;
  141. }
  142. // Re-enable non-iram interrupts
  143. esp_intr_noniram_enable();
  144. // Resume tasks on the current CPU, if the scheduler has started.
  145. // NOTE: enabling non-IRAM interrupts has to happen before this,
  146. // because once the scheduler has started, due to preemption the
  147. // current task can end up being moved to the other CPU.
  148. // But esp_intr_noniram_enable has to be called on the same CPU which
  149. // called esp_intr_noniram_disable
  150. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  151. xTaskResumeAll();
  152. }
  153. // Release API lock
  154. spi_flash_op_unlock();
  155. }
  156. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os()
  157. {
  158. const uint32_t cpuid = xPortGetCoreID();
  159. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  160. // do not care about other CPU, it was halted upon entering panic handler
  161. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  162. // Kill interrupts that aren't located in IRAM
  163. esp_intr_noniram_disable();
  164. // Disable cache on this CPU as well
  165. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  166. }
  167. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os()
  168. {
  169. const uint32_t cpuid = xPortGetCoreID();
  170. // Re-enable cache on this CPU
  171. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  172. // Re-enable non-iram interrupts
  173. esp_intr_noniram_enable();
  174. }
  175. #else // CONFIG_FREERTOS_UNICORE
  176. void spi_flash_init_lock()
  177. {
  178. }
  179. void spi_flash_op_lock()
  180. {
  181. vTaskSuspendAll();
  182. }
  183. void spi_flash_op_unlock()
  184. {
  185. xTaskResumeAll();
  186. }
  187. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
  188. {
  189. spi_flash_op_lock();
  190. esp_intr_noniram_disable();
  191. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  192. }
  193. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
  194. {
  195. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  196. esp_intr_noniram_enable();
  197. spi_flash_op_unlock();
  198. }
  199. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os()
  200. {
  201. // Kill interrupts that aren't located in IRAM
  202. esp_intr_noniram_disable();
  203. // Disable cache on this CPU as well
  204. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  205. }
  206. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os()
  207. {
  208. // Re-enable cache on this CPU
  209. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  210. // Re-enable non-iram interrupts
  211. esp_intr_noniram_enable();
  212. }
  213. #endif // CONFIG_FREERTOS_UNICORE
  214. /**
  215. * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
  216. * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
  217. * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
  218. */
  219. static const uint32_t cache_mask = DPORT_APP_CACHE_MASK_OPSDRAM | DPORT_APP_CACHE_MASK_DROM0 |
  220. DPORT_APP_CACHE_MASK_DRAM1 | DPORT_APP_CACHE_MASK_IROM0 |
  221. DPORT_APP_CACHE_MASK_IRAM1 | DPORT_APP_CACHE_MASK_IRAM0;
  222. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state)
  223. {
  224. uint32_t ret = 0;
  225. if (cpuid == 0) {
  226. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
  227. while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
  228. ;
  229. }
  230. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  231. } else {
  232. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
  233. while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
  234. ;
  235. }
  236. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  237. }
  238. *saved_state = ret;
  239. }
  240. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  241. {
  242. if (cpuid == 0) {
  243. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  244. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  245. } else {
  246. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  247. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  248. }
  249. }
  250. IRAM_ATTR bool spi_flash_cache_enabled()
  251. {
  252. bool result = (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE) != 0);
  253. #if portNUM_PROCESSORS == 2
  254. result = result && (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE) != 0);
  255. #endif
  256. return result;
  257. }