Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2020-2022 Arm Limited. All rights reserved. |
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 | #include "message_handler.hpp" |
| 20 | |
| 21 | #include "cmsis_compiler.h" |
| 22 | |
| 23 | #ifdef ETHOSU |
| 24 | #include <ethosu_driver.h> |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 25 | #include <pmu_ethosu.h> |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #include "FreeRTOS.h" |
| 29 | #include "queue.h" |
| 30 | #include "semphr.h" |
| 31 | |
| 32 | #include <cstring> |
| 33 | #include <inttypes.h> |
| 34 | |
| 35 | using namespace EthosU; |
| 36 | using namespace MessageQueue; |
| 37 | |
| 38 | namespace MessageHandler { |
| 39 | |
| 40 | /**************************************************************************** |
| 41 | * IncomingMessageHandler |
| 42 | ****************************************************************************/ |
| 43 | |
| 44 | IncomingMessageHandler::IncomingMessageHandler(ethosu_core_queue &_messageQueue, |
| 45 | Mailbox::Mailbox &_mailbox, |
| 46 | QueueHandle_t _inferenceQueue, |
| 47 | QueueHandle_t _outputQueue) : |
| 48 | messageQueue(_messageQueue), |
| 49 | mailbox(_mailbox), inferenceQueue(_inferenceQueue), outputQueue(_outputQueue) { |
| 50 | mailbox.registerCallback(handleIrq, reinterpret_cast<void *>(this)); |
| 51 | semaphore = xSemaphoreCreateBinary(); |
| 52 | } |
| 53 | |
| 54 | void IncomingMessageHandler::run() { |
| 55 | while (true) { |
| 56 | // Wait for event |
| 57 | xSemaphoreTake(semaphore, portMAX_DELAY); |
| 58 | |
| 59 | // Handle all messages in queue |
| 60 | while (handleMessage()) {} |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void IncomingMessageHandler::handleIrq(void *userArg) { |
| 65 | IncomingMessageHandler *_this = reinterpret_cast<IncomingMessageHandler *>(userArg); |
| 66 | xSemaphoreGive(_this->semaphore); |
| 67 | } |
| 68 | |
| 69 | void IncomingMessageHandler::queueErrorAndResetQueue(ethosu_core_msg_err_type type, const char *message) { |
| 70 | OutputMessage msg(ETHOSU_CORE_MSG_ERR); |
| 71 | msg.data.error.type = type; |
| 72 | |
| 73 | for (size_t i = 0; i < sizeof(msg.data.error.msg) && message[i]; i++) { |
| 74 | msg.data.error.msg[i] = message[i]; |
| 75 | } |
| 76 | |
| 77 | xQueueSend(outputQueue, &msg, portMAX_DELAY); |
| 78 | messageQueue.reset(); |
| 79 | } |
| 80 | |
| 81 | bool IncomingMessageHandler::handleMessage() { |
| 82 | struct ethosu_core_msg msg; |
| 83 | |
| 84 | if (messageQueue.available() == 0) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Read msg header |
| 89 | // Only process a complete message header, else send error message |
| 90 | // and reset queue |
| 91 | if (!messageQueue.read(msg)) { |
| 92 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_SIZE, "Failed to read a complete header"); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | printf("Msg: header magic=%" PRIX32 ", type=%" PRIu32 ", length=%" PRIu32 "\n", msg.magic, msg.type, msg.length); |
| 97 | |
| 98 | if (msg.magic != ETHOSU_CORE_MSG_MAGIC) { |
| 99 | printf("Msg: Invalid Magic\n"); |
| 100 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_MAGIC, "Invalid magic"); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | switch (msg.type) { |
| 105 | case ETHOSU_CORE_MSG_PING: { |
| 106 | printf("Msg: Ping\n"); |
| 107 | |
| 108 | OutputMessage message(ETHOSU_CORE_MSG_PONG); |
| 109 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 110 | break; |
| 111 | } |
| 112 | case ETHOSU_CORE_MSG_ERR: { |
| 113 | ethosu_core_msg_err error; |
| 114 | |
| 115 | if (!messageQueue.read(error)) { |
| 116 | printf("ERROR: Msg: Failed to receive error message\n"); |
| 117 | } else { |
| 118 | printf("Msg: Received an error response, type=%" PRIu32 ", msg=\"%s\"\n", error.type, error.msg); |
| 119 | } |
| 120 | |
| 121 | messageQueue.reset(); |
| 122 | return false; |
| 123 | } |
| 124 | case ETHOSU_CORE_MSG_VERSION_REQ: { |
| 125 | printf("Msg: Version request\n"); |
| 126 | |
| 127 | OutputMessage message(ETHOSU_CORE_MSG_VERSION_RSP); |
| 128 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 129 | break; |
| 130 | } |
| 131 | case ETHOSU_CORE_MSG_CAPABILITIES_REQ: { |
| 132 | ethosu_core_capabilities_req capabilities; |
| 133 | |
| 134 | if (!messageQueue.read(capabilities)) { |
| 135 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "CapabilitiesReq. Failed to read payload"); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | printf("Msg: Capabilities request.user_arg=0x%" PRIx64 "\n", capabilities.user_arg); |
| 140 | |
| 141 | OutputMessage message(ETHOSU_CORE_MSG_CAPABILITIES_RSP); |
| 142 | message.data.userArg = capabilities.user_arg; |
| 143 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 144 | break; |
| 145 | } |
| 146 | case ETHOSU_CORE_MSG_INFERENCE_REQ: { |
| 147 | ethosu_core_inference_req inference; |
| 148 | |
| 149 | if (!messageQueue.read(inference)) { |
| 150 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "InferenceReq. Failed to read payload"); |
| 151 | break; |
| 152 | } |
| 153 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 154 | printf("Msg: InferenceReq. user_arg=0x%" PRIx64 ", network={0x%" PRIx32 ", %" PRIu32 "}, \n", |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 155 | inference.user_arg, |
| 156 | inference.network.ptr, |
| 157 | inference.network.size); |
| 158 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 159 | printf("ifm_count=%" PRIu32 ", ifm=[", inference.ifm_count); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 160 | for (uint32_t i = 0; i < inference.ifm_count; ++i) { |
| 161 | if (i > 0) { |
| 162 | printf(", "); |
| 163 | } |
| 164 | |
| 165 | printf("{0x%" PRIx32 ", %" PRIu32 "}", inference.ifm[i].ptr, inference.ifm[i].size); |
| 166 | } |
| 167 | printf("]"); |
| 168 | |
| 169 | printf(", ofm_count=%" PRIu32 ", ofm=[", inference.ofm_count); |
| 170 | for (uint32_t i = 0; i < inference.ofm_count; ++i) { |
| 171 | if (i > 0) { |
| 172 | printf(", "); |
| 173 | } |
| 174 | |
| 175 | printf("{0x%" PRIx32 ", %" PRIu32 "}", inference.ofm[i].ptr, inference.ofm[i].size); |
| 176 | } |
| 177 | printf("]\n"); |
| 178 | |
| 179 | xQueueSend(inferenceQueue, &inference, portMAX_DELAY); |
| 180 | break; |
| 181 | } |
| 182 | default: { |
| 183 | char errMsg[128]; |
| 184 | |
| 185 | snprintf(&errMsg[0], |
| 186 | sizeof(errMsg), |
| 187 | "Msg: Unknown type: %" PRIu32 " with payload length %" PRIu32 " bytes\n", |
| 188 | msg.type, |
| 189 | msg.length); |
| 190 | |
| 191 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_UNSUPPORTED_TYPE, errMsg); |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | /**************************************************************************** |
| 201 | * InferenceHandler |
| 202 | ****************************************************************************/ |
| 203 | |
| 204 | InferenceHandler::InferenceHandler(uint8_t *tensorArena, |
| 205 | size_t arenaSize, |
| 206 | QueueHandle_t _inferenceQueue, |
| 207 | QueueHandle_t _outputQueue) : |
| 208 | inferenceQueue(_inferenceQueue), |
| 209 | outputQueue(_outputQueue), inference(tensorArena, arenaSize) {} |
| 210 | |
| 211 | void InferenceHandler::run() { |
| 212 | while (true) { |
| 213 | ethosu_core_inference_req req; |
| 214 | |
| 215 | if (pdTRUE != xQueueReceive(inferenceQueue, &req, portMAX_DELAY)) { |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | OutputMessage msg(ETHOSU_CORE_MSG_INFERENCE_RSP); |
| 220 | runInference(req, msg.data.inference); |
| 221 | |
| 222 | xQueueSend(outputQueue, &msg, portMAX_DELAY); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void InferenceHandler::runInference(ethosu_core_inference_req &req, ethosu_core_inference_rsp &rsp) { |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 227 | currentReq = &req; |
| 228 | currentRsp = &rsp; |
| 229 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 230 | /* |
| 231 | * Setup inference job |
| 232 | */ |
| 233 | |
| 234 | InferenceProcess::DataPtr networkModel(reinterpret_cast<void *>(req.network.ptr), req.network.size); |
| 235 | |
| 236 | std::vector<InferenceProcess::DataPtr> ifm; |
| 237 | for (uint32_t i = 0; i < req.ifm_count; ++i) { |
| 238 | ifm.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ifm[i].ptr), req.ifm[i].size)); |
| 239 | } |
| 240 | |
| 241 | std::vector<InferenceProcess::DataPtr> ofm; |
| 242 | for (uint32_t i = 0; i < req.ofm_count; ++i) { |
| 243 | ofm.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ofm[i].ptr), req.ofm[i].size)); |
| 244 | } |
| 245 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 246 | InferenceProcess::InferenceJob job("job", networkModel, ifm, ofm, {}, -1, this); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * Run inference |
| 250 | */ |
| 251 | |
| 252 | job.invalidate(); |
| 253 | bool failed = inference.runJob(job); |
| 254 | job.clean(); |
| 255 | |
| 256 | /* |
| 257 | * Send inference response |
| 258 | */ |
| 259 | |
| 260 | rsp.user_arg = req.user_arg; |
| 261 | rsp.ofm_count = job.output.size(); |
| 262 | rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; |
| 263 | |
| 264 | for (size_t i = 0; i < job.output.size(); ++i) { |
| 265 | rsp.ofm_size[i] = job.output[i].size; |
| 266 | } |
| 267 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 268 | currentReq = nullptr; |
| 269 | currentRsp = nullptr; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /**************************************************************************** |
| 273 | * OutgoingMessageHandler |
| 274 | ****************************************************************************/ |
| 275 | |
| 276 | OutgoingMessageHandler::OutgoingMessageHandler(ethosu_core_queue &_messageQueue, |
| 277 | Mailbox::Mailbox &_mailbox, |
| 278 | QueueHandle_t _outputQueue) : |
| 279 | messageQueue(_messageQueue), |
| 280 | mailbox(_mailbox), outputQueue(_outputQueue) { |
| 281 | readCapabilties(capabilities); |
| 282 | } |
| 283 | |
| 284 | void OutgoingMessageHandler::run() { |
| 285 | while (true) { |
| 286 | OutputMessage message; |
| 287 | if (pdTRUE != xQueueReceive(outputQueue, &message, portMAX_DELAY)) { |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | switch (message.type) { |
| 292 | case ETHOSU_CORE_MSG_INFERENCE_RSP: |
| 293 | sendInferenceRsp(message.data.inference); |
| 294 | break; |
| 295 | case ETHOSU_CORE_MSG_CAPABILITIES_RSP: |
| 296 | sendCapabilitiesRsp(message.data.userArg); |
| 297 | break; |
| 298 | case ETHOSU_CORE_MSG_VERSION_RSP: |
| 299 | sendVersionRsp(); |
| 300 | break; |
| 301 | case ETHOSU_CORE_MSG_PONG: |
| 302 | sendPong(); |
| 303 | break; |
| 304 | case ETHOSU_CORE_MSG_ERR: |
| 305 | sendErrorRsp(message.data.error); |
| 306 | break; |
| 307 | default: |
| 308 | printf("Dropping unknown outcome of type %d\n", message.type); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void OutgoingMessageHandler::sendPong() { |
| 315 | if (!messageQueue.write(ETHOSU_CORE_MSG_PONG)) { |
| 316 | printf("ERROR: Msg: Failed to write pong response. No mailbox message sent\n"); |
| 317 | } else { |
| 318 | mailbox.sendMessage(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void OutgoingMessageHandler::sendVersionRsp() { |
| 323 | ethosu_core_msg_version version = { |
| 324 | ETHOSU_CORE_MSG_VERSION_MAJOR, |
| 325 | ETHOSU_CORE_MSG_VERSION_MINOR, |
| 326 | ETHOSU_CORE_MSG_VERSION_PATCH, |
| 327 | 0, |
| 328 | }; |
| 329 | |
| 330 | if (!messageQueue.write(ETHOSU_CORE_MSG_VERSION_RSP, version)) { |
| 331 | printf("ERROR: Failed to write version response. No mailbox message sent\n"); |
| 332 | } else { |
| 333 | mailbox.sendMessage(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void OutgoingMessageHandler::sendCapabilitiesRsp(uint64_t userArg) { |
| 338 | capabilities.user_arg = userArg; |
| 339 | |
| 340 | if (!messageQueue.write(ETHOSU_CORE_MSG_CAPABILITIES_RSP, capabilities)) { |
| 341 | printf("ERROR: Failed to write capabilities response. No mailbox message sent\n"); |
| 342 | } else { |
| 343 | mailbox.sendMessage(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void OutgoingMessageHandler::sendInferenceRsp(ethosu_core_inference_rsp &inference) { |
| 348 | if (!messageQueue.write(ETHOSU_CORE_MSG_INFERENCE_RSP, inference)) { |
| 349 | printf("ERROR: Msg: Failed to write inference response. No mailbox message sent\n"); |
| 350 | } else { |
| 351 | mailbox.sendMessage(); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void OutgoingMessageHandler::sendErrorRsp(ethosu_core_msg_err &error) { |
| 356 | printf("ERROR: Msg: \"%s\"\n", error.msg); |
| 357 | |
| 358 | if (!messageQueue.write(ETHOSU_CORE_MSG_ERR, error)) { |
| 359 | printf("ERROR: Msg: Failed to write error response. No mailbox message sent\n"); |
| 360 | } else { |
| 361 | mailbox.sendMessage(); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void OutgoingMessageHandler::readCapabilties(ethosu_core_msg_capabilities_rsp &rsp) { |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 366 | rsp = {}; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 367 | |
| 368 | #ifdef ETHOSU |
| 369 | struct ethosu_driver_version version; |
| 370 | ethosu_get_driver_version(&version); |
| 371 | |
| 372 | struct ethosu_hw_info info; |
| 373 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
| 374 | ethosu_get_hw_info(drv, &info); |
| 375 | ethosu_release_driver(drv); |
| 376 | |
| 377 | rsp.user_arg = 0; |
| 378 | rsp.version_status = info.version.version_status; |
| 379 | rsp.version_minor = info.version.version_minor; |
| 380 | rsp.version_major = info.version.version_major; |
| 381 | rsp.product_major = info.version.product_major; |
| 382 | rsp.arch_patch_rev = info.version.arch_patch_rev; |
| 383 | rsp.arch_minor_rev = info.version.arch_minor_rev; |
| 384 | rsp.arch_major_rev = info.version.arch_major_rev; |
| 385 | rsp.driver_patch_rev = version.patch; |
| 386 | rsp.driver_minor_rev = version.minor; |
| 387 | rsp.driver_major_rev = version.major; |
| 388 | rsp.macs_per_cc = info.cfg.macs_per_cc; |
| 389 | rsp.cmd_stream_version = info.cfg.cmd_stream_version; |
| 390 | rsp.custom_dma = info.cfg.custom_dma; |
| 391 | #endif |
| 392 | } |
| 393 | |
| 394 | } // namespace MessageHandler |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame^] | 395 | |
| 396 | extern "C" void ethosu_inference_begin(struct ethosu_driver *drv, void *userArg) { |
| 397 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 398 | |
| 399 | // Calculate maximum number of events |
| 400 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 401 | |
| 402 | // Enable PMU |
| 403 | ETHOSU_PMU_Enable(drv); |
| 404 | |
| 405 | // Configure and enable events |
| 406 | for (int i = 0; i < numEvents; i++) { |
| 407 | ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast<ethosu_pmu_event_type>(self->currentReq->pmu_event_config[i])); |
| 408 | ETHOSU_PMU_CNTR_Enable(drv, 1 << i); |
| 409 | } |
| 410 | |
| 411 | // Enable cycle counter |
| 412 | if (self->currentReq->pmu_cycle_counter_enable) { |
| 413 | ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); |
| 414 | ETHOSU_PMU_CYCCNT_Reset(drv); |
| 415 | } |
| 416 | |
| 417 | // Reset all counters |
| 418 | ETHOSU_PMU_EVCNTR_ALL_Reset(drv); |
| 419 | } |
| 420 | |
| 421 | extern "C" void ethosu_inference_end(struct ethosu_driver *drv, void *userArg) { |
| 422 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 423 | |
| 424 | // Get cycle counter |
| 425 | self->currentRsp->pmu_cycle_counter_enable = self->currentReq->pmu_cycle_counter_enable; |
| 426 | if (self->currentReq->pmu_cycle_counter_enable) { |
| 427 | self->currentRsp->pmu_cycle_counter_count = ETHOSU_PMU_Get_CCNTR(drv); |
| 428 | } |
| 429 | |
| 430 | // Calculate maximum number of events |
| 431 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 432 | |
| 433 | // Get event counters |
| 434 | int i; |
| 435 | for (i = 0; i < numEvents; i++) { |
| 436 | self->currentRsp->pmu_event_config[i] = self->currentReq->pmu_event_config[i]; |
| 437 | self->currentRsp->pmu_event_count[i] = ETHOSU_PMU_Get_EVCNTR(drv, i); |
| 438 | } |
| 439 | |
| 440 | for (; i < ETHOSU_CORE_PMU_MAX; i++) { |
| 441 | self->currentRsp->pmu_event_config[i] = 0; |
| 442 | self->currentRsp->pmu_event_count[i] = 0; |
| 443 | } |
| 444 | |
| 445 | // Disable PMU |
| 446 | ETHOSU_PMU_Disable(drv); |
| 447 | } |