cache_utils.c 6.9 KB

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