cache_utils.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. void spi_flash_init_lock()
  39. {
  40. s_flash_op_mutex = xSemaphoreCreateMutex();
  41. }
  42. void spi_flash_op_lock()
  43. {
  44. xSemaphoreTake(s_flash_op_mutex, portMAX_DELAY);
  45. }
  46. void spi_flash_op_unlock()
  47. {
  48. xSemaphoreGive(s_flash_op_mutex);
  49. }
  50. void IRAM_ATTR spi_flash_op_block_func(void* arg)
  51. {
  52. // Disable scheduler on this CPU
  53. vTaskSuspendAll();
  54. // Restore interrupts that aren't located in IRAM
  55. esp_intr_noniram_disable();
  56. uint32_t cpuid = (uint32_t) arg;
  57. // Disable cache so that flash operation can start
  58. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  59. s_flash_op_can_start = true;
  60. while (!s_flash_op_complete) {
  61. // until we have a way to use interrupts for inter-CPU communication,
  62. // busy loop here and wait for the other CPU to finish flash operation
  63. }
  64. // Flash operation is complete, re-enable cache
  65. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  66. // Restore interrupts that aren't located in IRAM
  67. esp_intr_noniram_enable();
  68. // Re-enable scheduler
  69. xTaskResumeAll();
  70. }
  71. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
  72. {
  73. spi_flash_op_lock();
  74. const uint32_t cpuid = xPortGetCoreID();
  75. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  76. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  77. // Scheduler hasn't been started yet, it means that spi_flash API is being
  78. // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
  79. // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
  80. // which is in IRAM. So it is safe to disable cache for the other_cpuid here.
  81. assert(other_cpuid == 1);
  82. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  83. } else {
  84. // Signal to the spi_flash_op_block_task on the other CPU that we need it to
  85. // disable cache there and block other tasks from executing.
  86. s_flash_op_can_start = false;
  87. s_flash_op_complete = false;
  88. esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void*) other_cpuid);
  89. while (!s_flash_op_can_start) {
  90. // Busy loop and wait for spi_flash_op_block_func to disable cache
  91. // on the other CPU
  92. }
  93. // Disable scheduler on CPU cpuid
  94. vTaskSuspendAll();
  95. // This is guaranteed to run on CPU <cpuid> because the other CPU is now
  96. // occupied by highest priority task
  97. assert(xPortGetCoreID() == cpuid);
  98. }
  99. // Kill interrupts that aren't located in IRAM
  100. esp_intr_noniram_disable();
  101. // Disable cache on this CPU as well
  102. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  103. }
  104. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
  105. {
  106. const uint32_t cpuid = xPortGetCoreID();
  107. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  108. // Re-enable cache on this CPU
  109. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  110. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  111. // Scheduler is not running yet — this means we are running on PRO CPU.
  112. // other_cpuid is APP CPU, and it is either in reset or is spinning in
  113. // user_start_cpu1, which is in IRAM. So we can simply reenable cache.
  114. assert(other_cpuid == 1);
  115. spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
  116. } else {
  117. // Signal to spi_flash_op_block_task that flash operation is complete
  118. s_flash_op_complete = true;
  119. // Resume tasks on the current CPU
  120. xTaskResumeAll();
  121. }
  122. // Release API lock
  123. spi_flash_op_unlock();
  124. // Re-enable non-iram interrupts
  125. esp_intr_noniram_enable();
  126. }
  127. #else // CONFIG_FREERTOS_UNICORE
  128. void spi_flash_init_lock()
  129. {
  130. }
  131. void spi_flash_op_lock()
  132. {
  133. vTaskSuspendAll();
  134. }
  135. void spi_flash_op_unlock()
  136. {
  137. xTaskResumeAll();
  138. }
  139. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
  140. {
  141. esp_intr_noniram_disable();
  142. spi_flash_op_lock();
  143. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  144. }
  145. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
  146. {
  147. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  148. spi_flash_op_unlock();
  149. esp_intr_noniram_enable();
  150. }
  151. #endif // CONFIG_FREERTOS_UNICORE
  152. /**
  153. * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
  154. * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
  155. * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
  156. */
  157. static const uint32_t cache_mask = DPORT_APP_CACHE_MASK_OPSDRAM | DPORT_APP_CACHE_MASK_DROM0 |
  158. DPORT_APP_CACHE_MASK_DRAM1 | DPORT_APP_CACHE_MASK_IROM0 |
  159. DPORT_APP_CACHE_MASK_IRAM1 | DPORT_APP_CACHE_MASK_IRAM0;
  160. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state)
  161. {
  162. uint32_t ret = 0;
  163. if (cpuid == 0) {
  164. ret |= GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
  165. while (GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
  166. ;
  167. }
  168. SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  169. } else {
  170. ret |= GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
  171. while (GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
  172. ;
  173. }
  174. SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  175. }
  176. *saved_state = ret;
  177. }
  178. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  179. {
  180. if (cpuid == 0) {
  181. SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  182. SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  183. } else {
  184. SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  185. SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  186. }
  187. }