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> |
| 24 | |
| 25 | #include <cstddef> |
| 26 | #include <cstdio> |
| 27 | |
| 28 | namespace MessageProcess { |
| 29 | |
| 30 | template <uint32_t SIZE> |
| 31 | struct Queue { |
| 32 | ethosu_core_queue_header header; |
| 33 | uint8_t data[SIZE]; |
| 34 | |
| 35 | constexpr Queue() : header({SIZE, 0, 0}) {} |
| 36 | |
| 37 | constexpr ethosu_core_queue *toQueue() { |
| 38 | return reinterpret_cast<ethosu_core_queue *>(&header); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | class QueueImpl { |
| 43 | public: |
| 44 | struct Vec { |
| 45 | const void *base; |
| 46 | size_t length; |
| 47 | }; |
| 48 | |
| 49 | QueueImpl(ethosu_core_queue &queue); |
| 50 | |
| 51 | bool empty() const; |
| 52 | size_t available() const; |
| 53 | size_t capacity() const; |
| 54 | bool read(uint8_t *dst, uint32_t length); |
| 55 | bool write(const Vec *vec, size_t length); |
| 56 | bool write(const uint32_t type, const void *src = nullptr, uint32_t length = 0); |
| 57 | |
| 58 | template <typename T> |
| 59 | bool read(T &dst) { |
| 60 | return read(reinterpret_cast<uint8_t *>(&dst), sizeof(dst)); |
| 61 | } |
| 62 | |
| 63 | template <typename T> |
| 64 | bool write(const uint32_t type, const T &src) { |
| 65 | return write(type, reinterpret_cast<const void *>(&src), sizeof(src)); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | ethosu_core_queue &queue; |
| 70 | }; |
| 71 | |
| 72 | class MessageProcess { |
| 73 | public: |
| 74 | MessageProcess(ethosu_core_queue &in, ethosu_core_queue &out, InferenceProcess::InferenceProcess &inferenceProcess); |
| 75 | |
| 76 | void run(); |
| 77 | void handleIrq(); |
| 78 | bool handleMessage(); |
| 79 | void sendPong(); |
| 80 | void sendInferenceRsp(uint64_t userArg, size_t ofmSize, bool failed); |
| 81 | |
| 82 | private: |
| 83 | QueueImpl queueIn; |
| 84 | QueueImpl queueOut; |
| 85 | InferenceProcess::InferenceProcess &inferenceProcess; |
| 86 | }; |
| 87 | |
| 88 | } // namespace MessageProcess |
| 89 | |
| 90 | #endif |