esp_ipc.c 7.6 KB

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