cache_utils.c 11 KB

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