esp_ipc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include "esp_err.h"
  11. #include "esp_ipc.h"
  12. #include "esp_private/esp_ipc_isr.h"
  13. #include "esp_attr.h"
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "freertos/semphr.h"
  17. #if !defined(CONFIG_FREERTOS_UNICORE) || defined(CONFIG_APPTRACE_GCOV_ENABLE)
  18. #if CONFIG_COMPILER_OPTIMIZATION_NONE
  19. #define IPC_STACK_SIZE (CONFIG_ESP_IPC_TASK_STACK_SIZE + 0x100)
  20. #else
  21. #define IPC_STACK_SIZE (CONFIG_ESP_IPC_TASK_STACK_SIZE)
  22. #endif //CONFIG_COMPILER_OPTIMIZATION_NONE
  23. static DRAM_ATTR StaticSemaphore_t s_ipc_mutex_buffer[portNUM_PROCESSORS];
  24. static DRAM_ATTR StaticSemaphore_t s_ipc_sem_buffer[portNUM_PROCESSORS];
  25. static DRAM_ATTR StaticSemaphore_t s_ipc_ack_buffer[portNUM_PROCESSORS];
  26. static TaskHandle_t s_ipc_task_handle[portNUM_PROCESSORS];
  27. static SemaphoreHandle_t s_ipc_mutex[portNUM_PROCESSORS]; // This mutex is used as a global lock for esp_ipc_* APIs
  28. static SemaphoreHandle_t s_ipc_sem[portNUM_PROCESSORS]; // Two semaphores used to wake each of ipc tasks
  29. static SemaphoreHandle_t s_ipc_ack[portNUM_PROCESSORS]; // Semaphore used to acknowledge that task was woken up,
  30. // or function has finished running
  31. static volatile esp_ipc_func_t s_func[portNUM_PROCESSORS]; // Function which should be called by high priority task
  32. static void * volatile s_func_arg[portNUM_PROCESSORS]; // Argument to pass into s_func
  33. typedef enum {
  34. IPC_WAIT_FOR_START,
  35. IPC_WAIT_FOR_END
  36. } esp_ipc_wait_t;
  37. static volatile esp_ipc_wait_t s_ipc_wait[portNUM_PROCESSORS];// This variable tells high priority task when it should give
  38. // s_ipc_ack semaphore: before s_func is called, or
  39. // after it returns
  40. #if CONFIG_APPTRACE_GCOV_ENABLE
  41. static volatile esp_ipc_func_t s_gcov_func = NULL; // Gcov dump starter function which should be called by high priority task
  42. static void * volatile s_gcov_func_arg; // Argument to pass into s_gcov_func
  43. #endif
  44. static void IRAM_ATTR ipc_task(void* arg)
  45. {
  46. const int cpuid = (int) arg;
  47. assert(cpuid == xPortGetCoreID());
  48. #ifdef CONFIG_ESP_IPC_ISR_ENABLE
  49. esp_ipc_isr_init();
  50. #endif
  51. while (true) {
  52. // Wait for IPC to be initiated.
  53. // This will be indicated by giving the semaphore corresponding to
  54. // this CPU.
  55. if (xSemaphoreTake(s_ipc_sem[cpuid], portMAX_DELAY) != pdTRUE) {
  56. // TODO: when can this happen?
  57. abort();
  58. }
  59. #if CONFIG_APPTRACE_GCOV_ENABLE
  60. if (s_gcov_func) {
  61. (*s_gcov_func)(s_gcov_func_arg);
  62. s_gcov_func = NULL;
  63. /* we can not interfer with IPC calls so no need for further processing */
  64. continue;
  65. }
  66. #endif
  67. if (s_func[cpuid]) {
  68. // we need to cache s_func, s_func_arg and s_ipc_wait variables locally because they can be changed by a subsequent IPC call.
  69. esp_ipc_func_t func = s_func[cpuid];
  70. s_func[cpuid] = NULL;
  71. void* arg = s_func_arg[cpuid];
  72. esp_ipc_wait_t ipc_wait = s_ipc_wait[cpuid];
  73. if (ipc_wait == IPC_WAIT_FOR_START) {
  74. xSemaphoreGive(s_ipc_ack[cpuid]);
  75. }
  76. (*func)(arg);
  77. if (ipc_wait == IPC_WAIT_FOR_END) {
  78. xSemaphoreGive(s_ipc_ack[cpuid]);
  79. }
  80. }
  81. }
  82. // TODO: currently this is unreachable code. Introduce esp_ipc_uninit
  83. // function which will signal to both tasks that they can shut down.
  84. // Not critical at this point, we don't have a use case for stopping
  85. // IPC yet.
  86. // Also need to delete the semaphore here.
  87. vTaskDelete(NULL);
  88. }
  89. /*
  90. * Initialize inter-processor call module. This function is called automatically
  91. * on CPU start and should not be called from the application.
  92. *
  93. * This function start two tasks, one on each CPU. These tasks are started
  94. * with high priority. These tasks are normally inactive, waiting until one of
  95. * the esp_ipc_call_* functions to be used. One of these tasks will be
  96. * woken up to execute the callback provided to esp_ipc_call_nonblocking or
  97. * esp_ipc_call_blocking.
  98. */
  99. static void esp_ipc_init(void) __attribute__((constructor));
  100. static void esp_ipc_init(void)
  101. {
  102. char task_name[configMAX_TASK_NAME_LEN];
  103. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  104. snprintf(task_name, sizeof(task_name), "ipc%d", i);
  105. s_ipc_mutex[i] = xSemaphoreCreateMutexStatic(&s_ipc_mutex_buffer[i]);
  106. s_ipc_ack[i] = xSemaphoreCreateBinaryStatic(&s_ipc_ack_buffer[i]);
  107. s_ipc_sem[i] = xSemaphoreCreateBinaryStatic(&s_ipc_sem_buffer[i]);
  108. portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, IPC_STACK_SIZE, (void*) i,
  109. configMAX_PRIORITIES - 1, &s_ipc_task_handle[i], i);
  110. assert(res == pdTRUE);
  111. (void)res;
  112. }
  113. }
  114. static esp_err_t esp_ipc_call_and_wait(uint32_t cpu_id, esp_ipc_func_t func, void* arg, esp_ipc_wait_t wait_for)
  115. {
  116. if (cpu_id >= portNUM_PROCESSORS) {
  117. return ESP_ERR_INVALID_ARG;
  118. }
  119. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  120. return ESP_ERR_INVALID_STATE;
  121. }
  122. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  123. TaskHandle_t task_handler = xTaskGetCurrentTaskHandle();
  124. UBaseType_t priority_of_current_task = uxTaskPriorityGet(task_handler);
  125. UBaseType_t priority_of_running_ipc_task = uxTaskPriorityGet(s_ipc_task_handle[cpu_id]);
  126. if (priority_of_running_ipc_task < priority_of_current_task) {
  127. vTaskPrioritySet(s_ipc_task_handle[cpu_id], priority_of_current_task);
  128. }
  129. xSemaphoreTake(s_ipc_mutex[cpu_id], portMAX_DELAY);
  130. vTaskPrioritySet(s_ipc_task_handle[cpu_id], priority_of_current_task);
  131. #else
  132. xSemaphoreTake(s_ipc_mutex[0], portMAX_DELAY);
  133. #endif
  134. s_func[cpu_id] = func;
  135. s_func_arg[cpu_id] = arg;
  136. s_ipc_wait[cpu_id] = wait_for;
  137. xSemaphoreGive(s_ipc_sem[cpu_id]);
  138. xSemaphoreTake(s_ipc_ack[cpu_id], portMAX_DELAY);
  139. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  140. xSemaphoreGive(s_ipc_mutex[cpu_id]);
  141. #else
  142. xSemaphoreGive(s_ipc_mutex[0]);
  143. #endif
  144. return ESP_OK;
  145. }
  146. esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  147. {
  148. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
  149. }
  150. esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  151. {
  152. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
  153. }
  154. // currently this is only called from gcov component
  155. #if CONFIG_APPTRACE_GCOV_ENABLE
  156. esp_err_t esp_ipc_start_gcov_from_isr(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  157. {
  158. portBASE_TYPE ret = pdFALSE;
  159. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  160. return ESP_ERR_INVALID_STATE;
  161. }
  162. /* Lock IPC to avoid interferring with normal IPC calls, e.g.
  163. avoid situation when esp_ipc_start_gcov_from_isr() is called from IRQ
  164. in the middle of IPC call between `s_func` and `s_func_arg` modification. See esp_ipc_call_and_wait() */
  165. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  166. ret = xSemaphoreTakeFromISR(s_ipc_mutex[cpu_id], NULL);
  167. #else
  168. ret = xSemaphoreTakeFromISR(s_ipc_mutex[0], NULL);
  169. #endif
  170. if (ret != pdTRUE) {
  171. return ESP_ERR_TIMEOUT;
  172. }
  173. s_gcov_func = func;
  174. s_gcov_func_arg = arg;
  175. ret = xSemaphoreGiveFromISR(s_ipc_sem[cpu_id], NULL);
  176. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  177. xSemaphoreGiveFromISR(s_ipc_mutex[cpu_id], NULL);
  178. #else
  179. xSemaphoreGiveFromISR(s_ipc_mutex[0], NULL);
  180. #endif
  181. return ret == pdTRUE ? ESP_OK : ESP_FAIL;
  182. }
  183. #endif // CONFIG_APPTRACE_GCOV_ENABLE
  184. #endif // !defined(CONFIG_FREERTOS_UNICORE) || defined(CONFIG_APPTRACE_GCOV_ENABLE)