Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2022 Arm Limited. |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | /**************************************************************************** |
| 20 | * Includes |
| 21 | ****************************************************************************/ |
| 22 | |
| 23 | #include "FreeRTOS.h" |
| 24 | #include "queue.h" |
| 25 | #include "semphr.h" |
| 26 | #include "task.h" |
| 27 | |
| 28 | #include <inttypes.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | #include "ethosu_core_interface.h" |
| 32 | #include "message_handler.hpp" |
| 33 | #include "message_queue.hpp" |
| 34 | #include <mailbox.hpp> |
| 35 | |
| 36 | #if defined(MHU_V2) |
| 37 | #include <mhu_v2.hpp> |
| 38 | #elif defined(MHU_JUNO) |
| 39 | #include <mhu_juno.hpp> |
| 40 | #else |
| 41 | #include <mhu_dummy.hpp> |
| 42 | #endif |
| 43 | |
| 44 | /* Disable semihosting */ |
| 45 | __asm(".global __use_no_semihosting\n\t"); |
| 46 | |
| 47 | using namespace EthosU; |
| 48 | using namespace MessageHandler; |
| 49 | |
| 50 | /**************************************************************************** |
| 51 | * Defines |
| 52 | ****************************************************************************/ |
| 53 | |
| 54 | // Nr. of tasks to process inferences with, reserves driver & runs inference (Normally 1 per NPU, but not a must) |
Kristofer Jonsson | a739d21 | 2022-05-05 12:11:52 +0200 | [diff] [blame] | 55 | #if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0 |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 56 | constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT; |
| 57 | #else |
| 58 | constexpr size_t NUM_PARALLEL_TASKS = 1; |
| 59 | #endif |
| 60 | |
| 61 | // TensorArena static initialisation |
Kristofer Jonsson | a739d21 | 2022-05-05 12:11:52 +0200 | [diff] [blame] | 62 | constexpr size_t arenaSize = TENSOR_ARENA_SIZE; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 63 | |
| 64 | __attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[NUM_PARALLEL_TASKS][arenaSize]; |
| 65 | |
| 66 | // Message queue from remote host |
| 67 | __attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue; |
| 68 | |
| 69 | // Message queue to remote host |
| 70 | __attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue; |
| 71 | |
| 72 | namespace { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 73 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 74 | // Mailbox driver |
| 75 | #ifdef MHU_V2 |
| 76 | Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase |
| 77 | #elif defined(MHU_JUNO) |
| 78 | Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS); |
| 79 | #else |
| 80 | Mailbox::MHUDummy mailbox; |
| 81 | #endif |
| 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | /**************************************************************************** |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 86 | * Override new operators to call in FreeRTOS allocator |
| 87 | ****************************************************************************/ |
| 88 | |
| 89 | void *operator new(size_t size) { |
| 90 | return pvPortMalloc(size); |
| 91 | } |
| 92 | |
| 93 | void *operator new[](size_t size) { |
| 94 | return pvPortMalloc(size); |
| 95 | } |
| 96 | |
| 97 | void operator delete(void *ptr) { |
| 98 | vPortFree(ptr); |
| 99 | } |
| 100 | |
Davide Grohmann | bbbd916 | 2022-05-09 15:15:20 +0200 | [diff] [blame] | 101 | void operator delete(void *ptr, std::size_t) { |
| 102 | vPortFree(ptr); |
| 103 | } |
| 104 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 105 | void operator delete[](void *ptr) { |
| 106 | vPortFree(ptr); |
| 107 | } |
| 108 | |
Davide Grohmann | bbbd916 | 2022-05-09 15:15:20 +0200 | [diff] [blame] | 109 | void operator delete[](void *ptr, std::size_t) { |
| 110 | vPortFree(ptr); |
| 111 | } |
| 112 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 113 | /**************************************************************************** |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 114 | * Mutex & Semaphore |
| 115 | ****************************************************************************/ |
| 116 | |
| 117 | extern "C" { |
| 118 | |
| 119 | void *ethosu_mutex_create(void) { |
| 120 | return xSemaphoreCreateMutex(); |
| 121 | } |
| 122 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 123 | int ethosu_mutex_lock(void *mutex) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 124 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 125 | if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { |
| 126 | printf("Error: Failed to lock mutex.\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 132 | int ethosu_mutex_unlock(void *mutex) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 133 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 134 | if (xSemaphoreGive(handle) != pdTRUE) { |
| 135 | printf("Error: Failed to unlock mutex.\n"); |
| 136 | return -1; |
| 137 | } |
| 138 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void *ethosu_semaphore_create(void) { |
| 142 | return xSemaphoreCreateBinary(); |
| 143 | } |
| 144 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 145 | int ethosu_semaphore_take(void *sem) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 146 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 147 | if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { |
| 148 | printf("Error: Failed to take semaphore.\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 154 | int ethosu_semaphore_give(void *sem) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 155 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 156 | if (xPortIsInsideInterrupt()) { |
| 157 | if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) { |
| 158 | printf("Error: Failed to give semaphore from ISR.\n"); |
| 159 | return -1; |
| 160 | } |
| 161 | } else { |
| 162 | /* A FreeRTOS binary semaphore is fundamentally a queue that can only hold one item. If the queue is full, |
| 163 | * xSemaphoreGive will return a pdFALSE value. Ignoring the return value in here, as a semaphore give failure |
| 164 | * does not affect the application correctness. */ |
| 165 | if (xSemaphoreGive(handle) != pdTRUE) { |
| 166 | // do nothing |
| 167 | } |
| 168 | } |
| 169 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | /**************************************************************************** |
| 174 | * Application |
| 175 | ****************************************************************************/ |
Per Åstrand | b055072 | 2022-05-18 13:56:26 +0200 | [diff] [blame^] | 176 | namespace { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 177 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 178 | struct TaskParams { |
| 179 | TaskParams() : |
| 180 | messageNotify(xSemaphoreCreateBinary()), |
| 181 | inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()), |
| 182 | inferenceOutputQueue(xQueueCreate(10, sizeof(ethosu_core_inference_rsp))) {} |
| 183 | |
| 184 | SemaphoreHandle_t messageNotify; |
| 185 | // Used to pass inference requests to the inference runner task |
| 186 | std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue; |
| 187 | // Queue for message responses to the remote host |
| 188 | QueueHandle_t inferenceOutputQueue; |
| 189 | }; |
| 190 | |
| 191 | struct InferenceTaskParams { |
| 192 | TaskParams *taskParams; |
| 193 | uint8_t *arena; |
| 194 | }; |
| 195 | |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 196 | #ifdef MHU_IRQ |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 197 | void mailboxIrqHandler() { |
| 198 | mailbox.handleMessage(); |
| 199 | } |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 200 | #endif |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 201 | |
| 202 | void inferenceTask(void *pvParameters) { |
| 203 | printf("Starting inference task\n"); |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 204 | InferenceTaskParams *params = reinterpret_cast<InferenceTaskParams *>(pvParameters); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 205 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 206 | InferenceHandler process(params->arena, |
| 207 | arenaSize, |
| 208 | params->taskParams->inferenceInputQueue, |
| 209 | params->taskParams->inferenceOutputQueue, |
| 210 | params->taskParams->messageNotify); |
| 211 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 212 | process.run(); |
| 213 | } |
| 214 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 215 | void messageTask(void *pvParameters) { |
| 216 | printf("Starting message task\n"); |
| 217 | TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 218 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 219 | IncomingMessageHandler process(*inputMessageQueue.toQueue(), |
| 220 | *outputMessageQueue.toQueue(), |
| 221 | mailbox, |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 222 | params->inferenceInputQueue, |
| 223 | params->inferenceOutputQueue, |
| 224 | params->messageNotify); |
Kristofer Jonsson | d89ee0d | 2022-04-01 15:41:06 +0200 | [diff] [blame] | 225 | |
| 226 | #ifdef MHU_IRQ |
| 227 | // Register mailbox interrupt handler |
| 228 | NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler); |
| 229 | NVIC_EnableIRQ((IRQn_Type)MHU_IRQ); |
| 230 | #endif |
| 231 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 232 | process.run(); |
| 233 | } |
| 234 | |
Per Åstrand | b055072 | 2022-05-18 13:56:26 +0200 | [diff] [blame^] | 235 | /* |
| 236 | * Keep task parameters as global data as FreeRTOS resets the stack when the |
| 237 | * scheduler is started. |
| 238 | */ |
| 239 | TaskParams taskParams; |
| 240 | InferenceTaskParams infParams[NUM_PARALLEL_TASKS]; |
| 241 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 242 | } // namespace |
| 243 | |
| 244 | // FreeRTOS application. NOTE: Additional tasks may require increased heap size. |
| 245 | int main() { |
| 246 | BaseType_t ret; |
| 247 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 248 | if (!mailbox.verifyHardware()) { |
| 249 | printf("Failed to verify mailbox hardware\n"); |
| 250 | return 1; |
| 251 | } |
| 252 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 253 | // Task for handling incoming /outgoing messages from the remote host |
| 254 | ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 255 | if (ret != pdPASS) { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 256 | printf("Failed to create 'messageTask'\n"); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | // One inference task for each NPU |
| 261 | for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) { |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 262 | infParams[n].taskParams = &taskParams; |
| 263 | infParams[n].arena = reinterpret_cast<uint8_t *>(&tensorArena[n]); |
| 264 | ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &infParams[n], 3, nullptr); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 265 | if (ret != pdPASS) { |
| 266 | printf("Failed to create 'inferenceTask%d'\n", n); |
| 267 | return ret; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Start Scheduler |
| 272 | vTaskStartScheduler(); |
| 273 | |
| 274 | return 1; |
| 275 | } |