cache_utils.c 11 KB

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