esp_ipc.c 7.1 KB

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