Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Arm Limited. All rights reserved. |
| 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 | #ifndef MESSAGE_PROCESS_H |
| 20 | #define MESSAGE_PROCESS_H |
| 21 | |
| 22 | #include <ethosu_core_interface.h> |
| 23 | #include <inference_process.hpp> |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 24 | #include <mailbox.hpp> |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 25 | |
| 26 | #include <cstddef> |
| 27 | #include <cstdio> |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 28 | #include <vector> |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 29 | |
| 30 | namespace MessageProcess { |
| 31 | |
| 32 | template <uint32_t SIZE> |
| 33 | struct Queue { |
| 34 | ethosu_core_queue_header header; |
| 35 | uint8_t data[SIZE]; |
| 36 | |
| 37 | constexpr Queue() : header({SIZE, 0, 0}) {} |
| 38 | |
| 39 | constexpr ethosu_core_queue *toQueue() { |
| 40 | return reinterpret_cast<ethosu_core_queue *>(&header); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | class QueueImpl { |
| 45 | public: |
| 46 | struct Vec { |
| 47 | const void *base; |
| 48 | size_t length; |
| 49 | }; |
| 50 | |
| 51 | QueueImpl(ethosu_core_queue &queue); |
| 52 | |
| 53 | bool empty() const; |
| 54 | size_t available() const; |
| 55 | size_t capacity() const; |
| 56 | bool read(uint8_t *dst, uint32_t length); |
| 57 | bool write(const Vec *vec, size_t length); |
| 58 | bool write(const uint32_t type, const void *src = nullptr, uint32_t length = 0); |
Per Åstrand | dc28b13 | 2020-09-28 13:02:18 +0200 | [diff] [blame] | 59 | bool skip(uint32_t length); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 60 | |
| 61 | template <typename T> |
| 62 | bool read(T &dst) { |
| 63 | return read(reinterpret_cast<uint8_t *>(&dst), sizeof(dst)); |
| 64 | } |
| 65 | |
| 66 | template <typename T> |
Per Åstrand | dc28b13 | 2020-09-28 13:02:18 +0200 | [diff] [blame] | 67 | bool readOrSkip(T &dst, uint32_t expectedLength) { |
| 68 | if (expectedLength == sizeof(dst)) { |
| 69 | return read(reinterpret_cast<uint8_t *>(&dst), sizeof(dst)); |
| 70 | } else { |
| 71 | return skip(expectedLength); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | template <typename T> |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 76 | bool write(const uint32_t type, const T &src) { |
| 77 | return write(type, reinterpret_cast<const void *>(&src), sizeof(src)); |
| 78 | } |
| 79 | |
| 80 | private: |
Kristofer Jonsson | 2cbaaa9 | 2020-11-19 16:14:46 +0100 | [diff] [blame] | 81 | void cleanHeader() const; |
| 82 | void cleanHeaderData() const; |
| 83 | void invalidateHeader() const; |
| 84 | void invalidateHeaderData() const; |
| 85 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 86 | ethosu_core_queue &queue; |
| 87 | }; |
| 88 | |
| 89 | class MessageProcess { |
| 90 | public: |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 91 | MessageProcess(ethosu_core_queue &in, |
| 92 | ethosu_core_queue &out, |
| 93 | Mailbox::Mailbox &mbox, |
| 94 | InferenceProcess::InferenceProcess &inferenceProcess); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 95 | |
| 96 | void run(); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 97 | bool handleMessage(); |
| 98 | void sendPong(); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 99 | void sendInferenceRsp(uint64_t userArg, std::vector<InferenceProcess::DataPtr> &ofm, bool failed); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | QueueImpl queueIn; |
| 103 | QueueImpl queueOut; |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 104 | Mailbox::Mailbox &mailbox; |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 105 | InferenceProcess::InferenceProcess &inferenceProcess; |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 106 | void handleIrq(); |
| 107 | static void mailboxCallback(void *userArg); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace MessageProcess |
| 111 | |
| 112 | #endif |