blob: 602c2a473d9b834c859c7872e34bad9cd81c6105 [file] [log] [blame]
Kristofer Jonsson641c0912020-08-31 11:34:14 +02001/*
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ärd44398c82020-10-06 14:18:28 +020024#include <mailbox.hpp>
Kristofer Jonsson641c0912020-08-31 11:34:14 +020025
26#include <cstddef>
27#include <cstdio>
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020028#include <vector>
Kristofer Jonsson641c0912020-08-31 11:34:14 +020029
30namespace MessageProcess {
31
32template <uint32_t SIZE>
33struct 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
44class QueueImpl {
45public:
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 Åstranddc28b132020-09-28 13:02:18 +020059 bool skip(uint32_t length);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020060
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 Åstranddc28b132020-09-28 13:02:18 +020067 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 Jonsson641c0912020-08-31 11:34:14 +020076 bool write(const uint32_t type, const T &src) {
77 return write(type, reinterpret_cast<const void *>(&src), sizeof(src));
78 }
79
80private:
Kristofer Jonsson2cbaaa92020-11-19 16:14:46 +010081 void cleanHeader() const;
82 void cleanHeaderData() const;
83 void invalidateHeader() const;
84 void invalidateHeaderData() const;
85
Kristofer Jonsson641c0912020-08-31 11:34:14 +020086 ethosu_core_queue &queue;
87};
88
89class MessageProcess {
90public:
Jonny Svärd44398c82020-10-06 14:18:28 +020091 MessageProcess(ethosu_core_queue &in,
92 ethosu_core_queue &out,
93 Mailbox::Mailbox &mbox,
94 InferenceProcess::InferenceProcess &inferenceProcess);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020095
96 void run();
Kristofer Jonsson641c0912020-08-31 11:34:14 +020097 bool handleMessage();
98 void sendPong();
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020099 void sendInferenceRsp(uint64_t userArg, std::vector<InferenceProcess::DataPtr> &ofm, bool failed);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200100
101private:
102 QueueImpl queueIn;
103 QueueImpl queueOut;
Jonny Svärd44398c82020-10-06 14:18:28 +0200104 Mailbox::Mailbox &mailbox;
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200105 InferenceProcess::InferenceProcess &inferenceProcess;
Jonny Svärd44398c82020-10-06 14:18:28 +0200106 void handleIrq();
107 static void mailboxCallback(void *userArg);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200108};
109
110} // namespace MessageProcess
111
112#endif