blob: ea8bc3e7713f7a17903810cab0fef61f5b7d99f6 [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,
Mikael Olsson68709512024-02-12 13:30:31 +010038 const EthosU::ethosu_rpmsg_type _type = EthosU::ETHOSU_RPMSG_MAX,
39 const uint64_t msgId = 0,
40 const uint32_t _length = 0) :
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010041 src(_src),
42 length(_length) {
Mikael Olsson68709512024-02-12 13:30:31 +010043 rpmsg.header.magic = ETHOSU_RPMSG_MAGIC;
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010044 rpmsg.header.type = _type;
45 rpmsg.header.msg_id = msgId;
46 }
47
48 uint32_t src = 0;
49 uint32_t length = 0;
Mikael Olsson68709512024-02-12 13:30:31 +010050 EthosU::ethosu_rpmsg rpmsg;
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010051};
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
Mikael Olsson68709512024-02-12 13:30:31 +010078 void sendError(const uint32_t src, const EthosU::ethosu_rpmsg_err_type type, const char *message);
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010079 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);
Mikael Olsson68709512024-02-12 13:30:31 +010082 void sendNetworkInfoRsp(const uint32_t src, const uint64_t msgId, EthosU::ethosu_rpmsg_network_buffer &network);
83 void
84 forwardInferenceReq(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_rpmsg_inference_req &inference);
85 void sendInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_rpmsg_status status);
86 void sendCancelInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_rpmsg_status status);
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010087
Mikael Olsson68709512024-02-12 13:30:31 +010088 EthosU::ethosu_rpmsg_capabilities_rsp getCapabilities() const;
89 bool getNetwork(EthosU::ethosu_rpmsg_network_buffer &buffer);
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010090
91 // Tasks returning response messages
92 static void responseTask(void *param);
93
94private:
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010095 InferenceQueue inferenceQueue;
96 ResponseQueue responseQueue;
Mikael Olsson68709512024-02-12 13:30:31 +010097 EthosU::ethosu_rpmsg_capabilities_rsp capabilities;
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010098
99 // FreeRTOS
100 TaskHandle_t taskHandle;
101};