blob: 94615696cd71777b994d99cd411eac095c1eeccc [file] [log] [blame]
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +01001/*
Mikael Olsson34b190b2024-02-12 13:14:18 +01002 * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +01003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the License); you may
6 * not use _this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20/*****************************************************************************
21 * Includes
22 *****************************************************************************/
23
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010024#include <mailbox.hpp>
Mikael Olsson34b190b2024-02-12 13:14:18 +010025#include <rpmsg/ethosu_rpmsg.h>
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010026
27#include "queue.hpp"
28#include "remoteproc.hpp"
29
30/*****************************************************************************
31 * Messages
32 *****************************************************************************/
33
34struct Message {
35 Message() {}
36
37 Message(const uint32_t _src,
38 const EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX,
39 const uint64_t msgId = 0,
40 const uint32_t _length = 0) :
41 src(_src),
42 length(_length) {
43 rpmsg.header.magic = ETHOSU_CORE_MSG_MAGIC;
44 rpmsg.header.type = _type;
45 rpmsg.header.msg_id = msgId;
46 }
47
48 uint32_t src = 0;
49 uint32_t length = 0;
50 EthosU::ethosu_core_rpmsg rpmsg;
51};
52
53/*****************************************************************************
54 * MessageHandler
55 *****************************************************************************/
56
57class MessageHandler : public Rpmsg {
58public:
59 using InferenceQueue = Queue<Message *>;
60 using ResponseQueue = Queue<Message *>;
61
62 MessageHandler(RProc &rproc, const char *const name);
63 virtual ~MessageHandler();
64
65 InferenceQueue &getInferenceQueue() {
66 return inferenceQueue;
67 }
68
69 InferenceQueue &getResponseQueue() {
70 return responseQueue;
71 }
72
73protected:
74 // Handle incoming rpmsg
75 int handleMessage(void *data, size_t len, uint32_t src) override;
76
77 // Outgoing messages
78 void sendError(const uint32_t src, const EthosU::ethosu_core_err_type type, const char *message);
79 void sendPong(const uint32_t src, const uint64_t msgId);
80 void sendVersionRsp(const uint32_t src, const uint64_t msgId);
81 void sendCapabilitiesRsp(const uint32_t src, const uint64_t msgId);
82 void sendNetworkInfoRsp(const uint32_t src, const uint64_t msgId, EthosU::ethosu_core_network_buffer &network);
83 void forwardInferenceReq(const uint32_t src,
84 const uint64_t msgId,
85 const EthosU::ethosu_core_msg_inference_req &inference);
86 void sendInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status);
87 void sendCancelInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status);
88
89 EthosU::ethosu_core_msg_capabilities_rsp getCapabilities() const;
Mikael Olssonc078ced2023-06-05 15:59:27 +020090 bool getNetwork(EthosU::ethosu_core_network_buffer &buffer);
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010091
92 // Tasks returning response messages
93 static void responseTask(void *param);
94
95private:
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010096 InferenceQueue inferenceQueue;
97 ResponseQueue responseQueue;
98 EthosU::ethosu_core_msg_capabilities_rsp capabilities;
99
100 // FreeRTOS
101 TaskHandle_t taskHandle;
102};