blob: 779b05f8eee1f59b821e38a3ad500bc3e0c7c898 [file] [log] [blame]
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +01001/*
2 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
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#pragma once
20
21/*****************************************************************************
22 * Includes
23 *****************************************************************************/
24
25#include <ethosu_core_rpmsg.h>
26#include <mailbox.hpp>
27
28#include "queue.hpp"
29#include "remoteproc.hpp"
30
31/*****************************************************************************
32 * Messages
33 *****************************************************************************/
34
35struct Message {
36 Message() {}
37
38 Message(const uint32_t _src,
39 const EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX,
40 const uint64_t msgId = 0,
41 const uint32_t _length = 0) :
42 src(_src),
43 length(_length) {
44 rpmsg.header.magic = ETHOSU_CORE_MSG_MAGIC;
45 rpmsg.header.type = _type;
46 rpmsg.header.msg_id = msgId;
47 }
48
49 uint32_t src = 0;
50 uint32_t length = 0;
51 EthosU::ethosu_core_rpmsg rpmsg;
52};
53
54/*****************************************************************************
55 * MessageHandler
56 *****************************************************************************/
57
58class MessageHandler : public Rpmsg {
59public:
60 using InferenceQueue = Queue<Message *>;
61 using ResponseQueue = Queue<Message *>;
62
63 MessageHandler(RProc &rproc, const char *const name);
64 virtual ~MessageHandler();
65
66 InferenceQueue &getInferenceQueue() {
67 return inferenceQueue;
68 }
69
70 InferenceQueue &getResponseQueue() {
71 return responseQueue;
72 }
73
74protected:
75 // Handle incoming rpmsg
76 int handleMessage(void *data, size_t len, uint32_t src) override;
77
78 // Outgoing messages
79 void sendError(const uint32_t src, const EthosU::ethosu_core_err_type type, const char *message);
80 void sendPong(const uint32_t src, const uint64_t msgId);
81 void sendVersionRsp(const uint32_t src, const uint64_t msgId);
82 void sendCapabilitiesRsp(const uint32_t src, const uint64_t msgId);
83 void sendNetworkInfoRsp(const uint32_t src, const uint64_t msgId, EthosU::ethosu_core_network_buffer &network);
84 void forwardInferenceReq(const uint32_t src,
85 const uint64_t msgId,
86 const EthosU::ethosu_core_msg_inference_req &inference);
87 void sendInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status);
88 void sendCancelInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status);
89
90 EthosU::ethosu_core_msg_capabilities_rsp getCapabilities() const;
91 bool getNetwork(const EthosU::ethosu_core_network_buffer &buffer, void *&data, size_t &size);
92
93 // Tasks returning response messages
94 static void responseTask(void *param);
95
96private:
97 bool bufferToVirtual(EthosU::ethosu_core_buffer &buffer);
98 bool networkToVirtual(EthosU::ethosu_core_network_buffer &buffer);
99
100 InferenceQueue inferenceQueue;
101 ResponseQueue responseQueue;
102 EthosU::ethosu_core_msg_capabilities_rsp capabilities;
103
104 // FreeRTOS
105 TaskHandle_t taskHandle;
106};