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