blob: 851be040e1d54985f1e01a108982525a93dfc2c3 [file] [log] [blame]
Kristofer Jonsson641c0912020-08-31 11:34:14 +02001/*
Anton Moberge348f8f2021-03-31 11:08:58 +02002 * Copyright (c) 2020-2021 Arm Limited. All rights reserved.
Kristofer Jonsson641c0912020-08-31 11:34:14 +02003 *
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 {
Per Åstrand4edc1782021-01-12 14:33:36 +010034 EthosU::ethosu_core_queue_header header;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020035 uint8_t data[SIZE];
36
37 constexpr Queue() : header({SIZE, 0, 0}) {}
38
Per Åstrand4edc1782021-01-12 14:33:36 +010039 constexpr EthosU::ethosu_core_queue *toQueue() {
40 return reinterpret_cast<EthosU::ethosu_core_queue *>(&header);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020041 }
42};
43
44class QueueImpl {
45public:
46 struct Vec {
47 const void *base;
48 size_t length;
49 };
50
Per Åstrand4edc1782021-01-12 14:33:36 +010051 QueueImpl(EthosU::ethosu_core_queue &queue);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020052
53 bool empty() const;
54 size_t available() const;
55 size_t capacity() const;
Jonny Svärddc84f4f2021-01-14 19:54:54 +010056 void reset();
Kristofer Jonsson641c0912020-08-31 11:34:14 +020057 bool read(uint8_t *dst, uint32_t length);
58 bool write(const Vec *vec, size_t length);
59 bool write(const uint32_t type, const void *src = nullptr, uint32_t length = 0);
Jonny Svärddc84f4f2021-01-14 19:54:54 +010060 template <typename T>
61 bool write(const uint32_t type, const T &src) {
62 return write(type, reinterpret_cast<const void *>(&src), sizeof(src));
63 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +020064
65 template <typename T>
66 bool read(T &dst) {
67 return read(reinterpret_cast<uint8_t *>(&dst), sizeof(dst));
68 }
69
Kristofer Jonsson641c0912020-08-31 11:34:14 +020070private:
Kristofer Jonsson2cbaaa92020-11-19 16:14:46 +010071 void cleanHeader() const;
72 void cleanHeaderData() const;
73 void invalidateHeader() const;
74 void invalidateHeaderData() const;
75
Per Åstrand4edc1782021-01-12 14:33:36 +010076 EthosU::ethosu_core_queue &queue;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020077};
78
79class MessageProcess {
80public:
Per Åstrand4edc1782021-01-12 14:33:36 +010081 MessageProcess(EthosU::ethosu_core_queue &in,
82 EthosU::ethosu_core_queue &out,
Jonny Svärd44398c82020-10-06 14:18:28 +020083 Mailbox::Mailbox &mbox,
84 InferenceProcess::InferenceProcess &inferenceProcess);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020085
86 void run();
Kristofer Jonsson641c0912020-08-31 11:34:14 +020087 bool handleMessage();
88 void sendPong();
Per Åstrand4edc1782021-01-12 14:33:36 +010089 void sndErrorRspAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message);
Jonny Svärddc84f4f2021-01-14 19:54:54 +010090 void sendVersionRsp();
Davide Grohmann53cc13d2021-06-01 14:21:42 +020091 void sendCapabilityRsp(uint64_t userArg);
Bhavik Patelffe845d2020-11-16 12:13:56 +010092 void sendInferenceRsp(uint64_t userArg,
93 std::vector<InferenceProcess::DataPtr> &ofm,
94 bool failed,
95 std::vector<uint8_t> &pmuEventConfig,
96 uint32_t pmuCycleCounterEnable,
97 std::vector<uint32_t> &pmuEventCount,
98 uint64_t pmuCycleCounterCount);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020099
100private:
101 QueueImpl queueIn;
102 QueueImpl queueOut;
Jonny Svärd44398c82020-10-06 14:18:28 +0200103 Mailbox::Mailbox &mailbox;
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200104 InferenceProcess::InferenceProcess &inferenceProcess;
Jonny Svärd44398c82020-10-06 14:18:28 +0200105 void handleIrq();
106 static void mailboxCallback(void *userArg);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200107};
108
109} // namespace MessageProcess
110
111#endif