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 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 35 | #define XSTRINGIFY(src) #src |
| 36 | #define STRINGIFY(src) XSTRINGIFY(src) |
| 37 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 38 | using namespace EthosU; |
| 39 | using namespace MessageQueue; |
| 40 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 41 | /**************************************************************************** |
| 42 | * Models |
| 43 | ****************************************************************************/ |
| 44 | |
| 45 | namespace { |
| 46 | #if defined(__has_include) |
| 47 | |
| 48 | #if __has_include(STRINGIFY(MODEL_0)) |
| 49 | namespace Model0 { |
| 50 | #include STRINGIFY(MODEL_0) |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | #if __has_include(STRINGIFY(MODEL_1)) |
| 55 | namespace Model1 { |
| 56 | #include STRINGIFY(MODEL_1) |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | #if __has_include(STRINGIFY(MODEL_2)) |
| 61 | namespace Model2 { |
| 62 | #include STRINGIFY(MODEL_2) |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | #if __has_include(STRINGIFY(MODEL_3)) |
| 67 | namespace Model3 { |
| 68 | #include STRINGIFY(MODEL_3) |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | #endif |
| 73 | } // namespace |
| 74 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 75 | namespace MessageHandler { |
| 76 | |
| 77 | /**************************************************************************** |
| 78 | * IncomingMessageHandler |
| 79 | ****************************************************************************/ |
| 80 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 81 | namespace { |
| 82 | bool getNetwork(const ethosu_core_buffer &buffer, void *&data, size_t &size) { |
| 83 | data = reinterpret_cast<void *>(buffer.ptr); |
| 84 | size = buffer.size; |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | bool getNetwork(const uint32_t index, void *&data, size_t &size) { |
| 89 | switch (index) { |
| 90 | #if __has_include(STRINGIFY(MODEL_0)) |
| 91 | case 0: |
| 92 | data = reinterpret_cast<void *>(Model0::networkModel); |
| 93 | size = sizeof(Model0::networkModel); |
| 94 | break; |
| 95 | #endif |
| 96 | |
| 97 | #if __has_include(STRINGIFY(MODEL_1)) |
| 98 | case 1: |
| 99 | data = reinterpret_cast<void *>(Model1::networkModel); |
| 100 | size = sizeof(Model1::networkModel); |
| 101 | break; |
| 102 | #endif |
| 103 | |
| 104 | #if __has_include(STRINGIFY(MODEL_2)) |
| 105 | case 2: |
| 106 | data = reinterpret_cast<void *>(Model2::networkModel); |
| 107 | size = sizeof(Model2::networkModel); |
| 108 | break; |
| 109 | #endif |
| 110 | |
| 111 | #if __has_include(STRINGIFY(MODEL_3)) |
| 112 | case 3: |
| 113 | data = reinterpret_cast<void *>(Model3::networkModel); |
| 114 | size = sizeof(Model3::networkModel); |
| 115 | break; |
| 116 | #endif |
| 117 | |
| 118 | default: |
| 119 | printf("Error: Network model index out of range. index=%u\n", index); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | bool getNetwork(const ethosu_core_network_buffer &buffer, void *&data, size_t &size) { |
| 127 | switch (buffer.type) { |
| 128 | case ETHOSU_CORE_NETWORK_BUFFER: |
| 129 | return getNetwork(buffer.buffer, data, size); |
| 130 | case ETHOSU_CORE_NETWORK_INDEX: |
| 131 | return getNetwork(buffer.index, data, size); |
| 132 | default: |
| 133 | printf("Error: Unsupported network model type. type=%u\n", buffer.type); |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | }; // namespace |
| 138 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 139 | IncomingMessageHandler::IncomingMessageHandler(ethosu_core_queue &_messageQueue, |
| 140 | Mailbox::Mailbox &_mailbox, |
| 141 | QueueHandle_t _inferenceQueue, |
| 142 | QueueHandle_t _outputQueue) : |
| 143 | messageQueue(_messageQueue), |
| 144 | mailbox(_mailbox), inferenceQueue(_inferenceQueue), outputQueue(_outputQueue) { |
| 145 | mailbox.registerCallback(handleIrq, reinterpret_cast<void *>(this)); |
| 146 | semaphore = xSemaphoreCreateBinary(); |
| 147 | } |
| 148 | |
| 149 | void IncomingMessageHandler::run() { |
| 150 | while (true) { |
| 151 | // Wait for event |
| 152 | xSemaphoreTake(semaphore, portMAX_DELAY); |
| 153 | |
| 154 | // Handle all messages in queue |
| 155 | while (handleMessage()) {} |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void IncomingMessageHandler::handleIrq(void *userArg) { |
| 160 | IncomingMessageHandler *_this = reinterpret_cast<IncomingMessageHandler *>(userArg); |
| 161 | xSemaphoreGive(_this->semaphore); |
| 162 | } |
| 163 | |
| 164 | void IncomingMessageHandler::queueErrorAndResetQueue(ethosu_core_msg_err_type type, const char *message) { |
| 165 | OutputMessage msg(ETHOSU_CORE_MSG_ERR); |
| 166 | msg.data.error.type = type; |
| 167 | |
| 168 | for (size_t i = 0; i < sizeof(msg.data.error.msg) && message[i]; i++) { |
| 169 | msg.data.error.msg[i] = message[i]; |
| 170 | } |
| 171 | |
| 172 | xQueueSend(outputQueue, &msg, portMAX_DELAY); |
| 173 | messageQueue.reset(); |
| 174 | } |
| 175 | |
| 176 | bool IncomingMessageHandler::handleMessage() { |
| 177 | struct ethosu_core_msg msg; |
| 178 | |
| 179 | if (messageQueue.available() == 0) { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // Read msg header |
| 184 | // Only process a complete message header, else send error message |
| 185 | // and reset queue |
| 186 | if (!messageQueue.read(msg)) { |
| 187 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_SIZE, "Failed to read a complete header"); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | printf("Msg: header magic=%" PRIX32 ", type=%" PRIu32 ", length=%" PRIu32 "\n", msg.magic, msg.type, msg.length); |
| 192 | |
| 193 | if (msg.magic != ETHOSU_CORE_MSG_MAGIC) { |
| 194 | printf("Msg: Invalid Magic\n"); |
| 195 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_MAGIC, "Invalid magic"); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | switch (msg.type) { |
| 200 | case ETHOSU_CORE_MSG_PING: { |
| 201 | printf("Msg: Ping\n"); |
| 202 | |
| 203 | OutputMessage message(ETHOSU_CORE_MSG_PONG); |
| 204 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 205 | break; |
| 206 | } |
| 207 | case ETHOSU_CORE_MSG_ERR: { |
| 208 | ethosu_core_msg_err error; |
| 209 | |
| 210 | if (!messageQueue.read(error)) { |
| 211 | printf("ERROR: Msg: Failed to receive error message\n"); |
| 212 | } else { |
| 213 | printf("Msg: Received an error response, type=%" PRIu32 ", msg=\"%s\"\n", error.type, error.msg); |
| 214 | } |
| 215 | |
| 216 | messageQueue.reset(); |
| 217 | return false; |
| 218 | } |
| 219 | case ETHOSU_CORE_MSG_VERSION_REQ: { |
| 220 | printf("Msg: Version request\n"); |
| 221 | |
| 222 | OutputMessage message(ETHOSU_CORE_MSG_VERSION_RSP); |
| 223 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 224 | break; |
| 225 | } |
| 226 | case ETHOSU_CORE_MSG_CAPABILITIES_REQ: { |
| 227 | ethosu_core_capabilities_req capabilities; |
| 228 | |
| 229 | if (!messageQueue.read(capabilities)) { |
| 230 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "CapabilitiesReq. Failed to read payload"); |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | printf("Msg: Capabilities request.user_arg=0x%" PRIx64 "\n", capabilities.user_arg); |
| 235 | |
| 236 | OutputMessage message(ETHOSU_CORE_MSG_CAPABILITIES_RSP); |
| 237 | message.data.userArg = capabilities.user_arg; |
| 238 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 239 | break; |
| 240 | } |
| 241 | case ETHOSU_CORE_MSG_INFERENCE_REQ: { |
| 242 | ethosu_core_inference_req inference; |
| 243 | |
| 244 | if (!messageQueue.read(inference)) { |
| 245 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "InferenceReq. Failed to read payload"); |
| 246 | break; |
| 247 | } |
| 248 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 249 | printf("Msg: InferenceReq. user_arg=0x%" PRIx64 ", network_type=%" PRIu32 ", ", |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 250 | inference.user_arg, |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 251 | inference.network.type); |
| 252 | |
| 253 | if (inference.network.type == ETHOSU_CORE_NETWORK_BUFFER) { |
| 254 | printf("network.buffer={0x%" PRIx32 ", %" PRIu32 "},\n", |
| 255 | inference.network.buffer.ptr, |
| 256 | inference.network.buffer.size); |
| 257 | } else { |
| 258 | printf("network.index=%" PRIu32 ",\n", inference.network.index); |
| 259 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 260 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 261 | printf("ifm_count=%" PRIu32 ", ifm=[", inference.ifm_count); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 262 | for (uint32_t i = 0; i < inference.ifm_count; ++i) { |
| 263 | if (i > 0) { |
| 264 | printf(", "); |
| 265 | } |
| 266 | |
| 267 | printf("{0x%" PRIx32 ", %" PRIu32 "}", inference.ifm[i].ptr, inference.ifm[i].size); |
| 268 | } |
| 269 | printf("]"); |
| 270 | |
| 271 | printf(", ofm_count=%" PRIu32 ", ofm=[", inference.ofm_count); |
| 272 | for (uint32_t i = 0; i < inference.ofm_count; ++i) { |
| 273 | if (i > 0) { |
| 274 | printf(", "); |
| 275 | } |
| 276 | |
| 277 | printf("{0x%" PRIx32 ", %" PRIu32 "}", inference.ofm[i].ptr, inference.ofm[i].size); |
| 278 | } |
| 279 | printf("]\n"); |
| 280 | |
| 281 | xQueueSend(inferenceQueue, &inference, portMAX_DELAY); |
| 282 | break; |
| 283 | } |
| 284 | default: { |
| 285 | char errMsg[128]; |
| 286 | |
| 287 | snprintf(&errMsg[0], |
| 288 | sizeof(errMsg), |
| 289 | "Msg: Unknown type: %" PRIu32 " with payload length %" PRIu32 " bytes\n", |
| 290 | msg.type, |
| 291 | msg.length); |
| 292 | |
| 293 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_UNSUPPORTED_TYPE, errMsg); |
| 294 | |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | /**************************************************************************** |
| 303 | * InferenceHandler |
| 304 | ****************************************************************************/ |
| 305 | |
| 306 | InferenceHandler::InferenceHandler(uint8_t *tensorArena, |
| 307 | size_t arenaSize, |
| 308 | QueueHandle_t _inferenceQueue, |
| 309 | QueueHandle_t _outputQueue) : |
| 310 | inferenceQueue(_inferenceQueue), |
| 311 | outputQueue(_outputQueue), inference(tensorArena, arenaSize) {} |
| 312 | |
| 313 | void InferenceHandler::run() { |
| 314 | while (true) { |
| 315 | ethosu_core_inference_req req; |
| 316 | |
| 317 | if (pdTRUE != xQueueReceive(inferenceQueue, &req, portMAX_DELAY)) { |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | OutputMessage msg(ETHOSU_CORE_MSG_INFERENCE_RSP); |
| 322 | runInference(req, msg.data.inference); |
| 323 | |
| 324 | xQueueSend(outputQueue, &msg, portMAX_DELAY); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | 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] | 329 | currentReq = &req; |
| 330 | currentRsp = &rsp; |
| 331 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 332 | /* |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 333 | * Run inference |
| 334 | */ |
| 335 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 336 | InferenceProcess::InferenceJob job; |
| 337 | bool failed = getInferenceJob(req, job); |
| 338 | |
| 339 | if (!failed) { |
| 340 | job.invalidate(); |
| 341 | failed = inference.runJob(job); |
| 342 | job.clean(); |
| 343 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 344 | |
| 345 | /* |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 346 | * Print PMU counters |
| 347 | */ |
| 348 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 349 | if (!failed) { |
| 350 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 351 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 352 | for (int i = 0; i < numEvents; i++) { |
| 353 | printf("ethosu_pmu_cntr%d : %" PRIu32 "\n", i, rsp.pmu_event_count[i]); |
| 354 | } |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 355 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 356 | if (rsp.pmu_cycle_counter_enable) { |
| 357 | printf("ethosu_pmu_cycle_cntr : %" PRIu64 " cycles\n", rsp.pmu_cycle_counter_count); |
| 358 | } |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 362 | * Send inference response |
| 363 | */ |
| 364 | |
| 365 | rsp.user_arg = req.user_arg; |
| 366 | rsp.ofm_count = job.output.size(); |
| 367 | rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; |
| 368 | |
| 369 | for (size_t i = 0; i < job.output.size(); ++i) { |
| 370 | rsp.ofm_size[i] = job.output[i].size; |
| 371 | } |
| 372 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 373 | currentReq = nullptr; |
| 374 | currentRsp = nullptr; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame^] | 377 | bool InferenceHandler::getInferenceJob(const ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job) { |
| 378 | bool failed = getNetwork(req.network, job.networkModel.data, job.networkModel.size); |
| 379 | if (failed) { |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | for (uint32_t i = 0; i < req.ifm_count; ++i) { |
| 384 | job.input.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ifm[i].ptr), req.ifm[i].size)); |
| 385 | } |
| 386 | |
| 387 | for (uint32_t i = 0; i < req.ofm_count; ++i) { |
| 388 | job.output.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ofm[i].ptr), req.ofm[i].size)); |
| 389 | } |
| 390 | |
| 391 | job.externalContext = this; |
| 392 | |
| 393 | return false; |
| 394 | } |
| 395 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 396 | /**************************************************************************** |
| 397 | * OutgoingMessageHandler |
| 398 | ****************************************************************************/ |
| 399 | |
| 400 | OutgoingMessageHandler::OutgoingMessageHandler(ethosu_core_queue &_messageQueue, |
| 401 | Mailbox::Mailbox &_mailbox, |
| 402 | QueueHandle_t _outputQueue) : |
| 403 | messageQueue(_messageQueue), |
| 404 | mailbox(_mailbox), outputQueue(_outputQueue) { |
| 405 | readCapabilties(capabilities); |
| 406 | } |
| 407 | |
| 408 | void OutgoingMessageHandler::run() { |
| 409 | while (true) { |
| 410 | OutputMessage message; |
| 411 | if (pdTRUE != xQueueReceive(outputQueue, &message, portMAX_DELAY)) { |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | switch (message.type) { |
| 416 | case ETHOSU_CORE_MSG_INFERENCE_RSP: |
| 417 | sendInferenceRsp(message.data.inference); |
| 418 | break; |
| 419 | case ETHOSU_CORE_MSG_CAPABILITIES_RSP: |
| 420 | sendCapabilitiesRsp(message.data.userArg); |
| 421 | break; |
| 422 | case ETHOSU_CORE_MSG_VERSION_RSP: |
| 423 | sendVersionRsp(); |
| 424 | break; |
| 425 | case ETHOSU_CORE_MSG_PONG: |
| 426 | sendPong(); |
| 427 | break; |
| 428 | case ETHOSU_CORE_MSG_ERR: |
| 429 | sendErrorRsp(message.data.error); |
| 430 | break; |
| 431 | default: |
| 432 | printf("Dropping unknown outcome of type %d\n", message.type); |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void OutgoingMessageHandler::sendPong() { |
| 439 | if (!messageQueue.write(ETHOSU_CORE_MSG_PONG)) { |
| 440 | printf("ERROR: Msg: Failed to write pong response. No mailbox message sent\n"); |
| 441 | } else { |
| 442 | mailbox.sendMessage(); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | void OutgoingMessageHandler::sendVersionRsp() { |
| 447 | ethosu_core_msg_version version = { |
| 448 | ETHOSU_CORE_MSG_VERSION_MAJOR, |
| 449 | ETHOSU_CORE_MSG_VERSION_MINOR, |
| 450 | ETHOSU_CORE_MSG_VERSION_PATCH, |
| 451 | 0, |
| 452 | }; |
| 453 | |
| 454 | if (!messageQueue.write(ETHOSU_CORE_MSG_VERSION_RSP, version)) { |
| 455 | printf("ERROR: Failed to write version response. No mailbox message sent\n"); |
| 456 | } else { |
| 457 | mailbox.sendMessage(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void OutgoingMessageHandler::sendCapabilitiesRsp(uint64_t userArg) { |
| 462 | capabilities.user_arg = userArg; |
| 463 | |
| 464 | if (!messageQueue.write(ETHOSU_CORE_MSG_CAPABILITIES_RSP, capabilities)) { |
| 465 | printf("ERROR: Failed to write capabilities response. No mailbox message sent\n"); |
| 466 | } else { |
| 467 | mailbox.sendMessage(); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | void OutgoingMessageHandler::sendInferenceRsp(ethosu_core_inference_rsp &inference) { |
| 472 | if (!messageQueue.write(ETHOSU_CORE_MSG_INFERENCE_RSP, inference)) { |
| 473 | printf("ERROR: Msg: Failed to write inference response. No mailbox message sent\n"); |
| 474 | } else { |
| 475 | mailbox.sendMessage(); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void OutgoingMessageHandler::sendErrorRsp(ethosu_core_msg_err &error) { |
| 480 | printf("ERROR: Msg: \"%s\"\n", error.msg); |
| 481 | |
| 482 | if (!messageQueue.write(ETHOSU_CORE_MSG_ERR, error)) { |
| 483 | printf("ERROR: Msg: Failed to write error response. No mailbox message sent\n"); |
| 484 | } else { |
| 485 | mailbox.sendMessage(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void OutgoingMessageHandler::readCapabilties(ethosu_core_msg_capabilities_rsp &rsp) { |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 490 | rsp = {}; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 491 | |
| 492 | #ifdef ETHOSU |
| 493 | struct ethosu_driver_version version; |
| 494 | ethosu_get_driver_version(&version); |
| 495 | |
| 496 | struct ethosu_hw_info info; |
| 497 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
| 498 | ethosu_get_hw_info(drv, &info); |
| 499 | ethosu_release_driver(drv); |
| 500 | |
| 501 | rsp.user_arg = 0; |
| 502 | rsp.version_status = info.version.version_status; |
| 503 | rsp.version_minor = info.version.version_minor; |
| 504 | rsp.version_major = info.version.version_major; |
| 505 | rsp.product_major = info.version.product_major; |
| 506 | rsp.arch_patch_rev = info.version.arch_patch_rev; |
| 507 | rsp.arch_minor_rev = info.version.arch_minor_rev; |
| 508 | rsp.arch_major_rev = info.version.arch_major_rev; |
| 509 | rsp.driver_patch_rev = version.patch; |
| 510 | rsp.driver_minor_rev = version.minor; |
| 511 | rsp.driver_major_rev = version.major; |
| 512 | rsp.macs_per_cc = info.cfg.macs_per_cc; |
| 513 | rsp.cmd_stream_version = info.cfg.cmd_stream_version; |
| 514 | rsp.custom_dma = info.cfg.custom_dma; |
| 515 | #endif |
| 516 | } |
| 517 | |
| 518 | } // namespace MessageHandler |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 519 | |
| 520 | extern "C" void ethosu_inference_begin(struct ethosu_driver *drv, void *userArg) { |
| 521 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 522 | |
| 523 | // Calculate maximum number of events |
| 524 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 525 | |
| 526 | // Enable PMU |
| 527 | ETHOSU_PMU_Enable(drv); |
| 528 | |
| 529 | // Configure and enable events |
| 530 | for (int i = 0; i < numEvents; i++) { |
| 531 | ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast<ethosu_pmu_event_type>(self->currentReq->pmu_event_config[i])); |
| 532 | ETHOSU_PMU_CNTR_Enable(drv, 1 << i); |
| 533 | } |
| 534 | |
| 535 | // Enable cycle counter |
| 536 | if (self->currentReq->pmu_cycle_counter_enable) { |
Kristofer Jonsson | b9d7ded | 2022-02-04 13:10:04 +0100 | [diff] [blame] | 537 | ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(drv, ETHOSU_PMU_NPU_IDLE); |
| 538 | ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(drv, ETHOSU_PMU_NPU_ACTIVE); |
| 539 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 540 | ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); |
| 541 | ETHOSU_PMU_CYCCNT_Reset(drv); |
| 542 | } |
| 543 | |
| 544 | // Reset all counters |
| 545 | ETHOSU_PMU_EVCNTR_ALL_Reset(drv); |
| 546 | } |
| 547 | |
| 548 | extern "C" void ethosu_inference_end(struct ethosu_driver *drv, void *userArg) { |
| 549 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 550 | |
| 551 | // Get cycle counter |
| 552 | self->currentRsp->pmu_cycle_counter_enable = self->currentReq->pmu_cycle_counter_enable; |
| 553 | if (self->currentReq->pmu_cycle_counter_enable) { |
| 554 | self->currentRsp->pmu_cycle_counter_count = ETHOSU_PMU_Get_CCNTR(drv); |
| 555 | } |
| 556 | |
| 557 | // Calculate maximum number of events |
| 558 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 559 | |
| 560 | // Get event counters |
| 561 | int i; |
| 562 | for (i = 0; i < numEvents; i++) { |
| 563 | self->currentRsp->pmu_event_config[i] = self->currentReq->pmu_event_config[i]; |
| 564 | self->currentRsp->pmu_event_count[i] = ETHOSU_PMU_Get_EVCNTR(drv, i); |
| 565 | } |
| 566 | |
| 567 | for (; i < ETHOSU_CORE_PMU_MAX; i++) { |
| 568 | self->currentRsp->pmu_event_config[i] = 0; |
| 569 | self->currentRsp->pmu_event_count[i] = 0; |
| 570 | } |
| 571 | |
| 572 | // Disable PMU |
| 573 | ETHOSU_PMU_Disable(drv); |
| 574 | } |