blob: 480156ef2062e937ab80ceb95eec84e07061bfa9 [file] [log] [blame]
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +01001/*
Mikael Olsson10ecba82024-03-21 14:07:54 +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/*****************************************************************************
19 * Includes
20 *****************************************************************************/
21
Kristofer Jonsson7716cee2023-02-10 13:30:16 +010022#include <cinttypes>
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010023#include <memory>
24#include <stdio.h>
25
26#include <ethosu_log.h>
27#include <mailbox.hpp>
28
29#if defined(MHU_V2)
30#include <mhu_v2.hpp>
31#elif defined(MHU_JUNO)
32#include <mhu_juno.hpp>
33#else
34#include <mhu_dummy.hpp>
35#endif
36
37#include "inference_runner.hpp"
38#include "message_handler.hpp"
39#include "remoteproc.hpp"
40
41/*****************************************************************************
42 * TFLM arena
43 *****************************************************************************/
44
45// Number of parallell inference tasks. Typically one per NPU.
46#if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0
47constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT;
48#else
49constexpr size_t NUM_PARALLEL_TASKS = 1;
50#endif
51
52#ifndef TENSOR_ARENA_SIZE
Mikael Olsson10ecba82024-03-21 14:07:54 +010053#define TENSOR_ARENA_SIZE 0x200000
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010054#endif
55
56// TensorArena static initialisation
57constexpr size_t arenaSize = TENSOR_ARENA_SIZE;
Mikael Olsson10ecba82024-03-21 14:07:54 +010058static_assert(arenaSize && ((arenaSize & (arenaSize - 1)) == 0), "TENSOR_ARENA_SIZE must be a power of two");
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010059
60/*****************************************************************************
61 * Resource table
62 *****************************************************************************/
63
Ledion Dajab00e4ed2023-05-22 10:45:31 +020064#if defined(REMOTEPROC_TRACE_BUFFER)
65#if defined(__ARMCC_VERSION)
66extern uint32_t Image$$trace_buffer$$Base;
67extern uint32_t Image$$trace_buffer$$Length;
68#define __ethosu_core_trace_buffer_start__ Image$$trace_buffer$$Base
69#define __ethosu_core_trace_buffer_size__ Image$$trace_buffer$$Length
70#else
71extern uint32_t __ethosu_core_trace_buffer_start__;
72extern uint32_t __ethosu_core_trace_buffer_size__;
73#endif
74#endif
75
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010076extern "C" {
Ledion Dajab00e4ed2023-05-22 10:45:31 +020077// clang-format off
78__attribute__((section(".resource_table"))) ResourceTable resourceTable = {
79 // Table
80 {
81 ResourceTable::VERSION,
82 ResourceTable::NUM_RESOURCES,
83 {ResourceTable::RESERVED, ResourceTable::RESERVED},
84 {}
85 },
86 // Offset
87 {
Mikael Olsson67add212023-05-31 19:00:48 +020088 offsetof(ResourceTable, mapping),
Ledion Dajab00e4ed2023-05-22 10:45:31 +020089#if defined(REMOTEPROC_TRACE_BUFFER)
90 offsetof(ResourceTable, trace),
91#endif
92 offsetof(ResourceTable, vdev),
93 offsetof(ResourceTable, carveout),
Mikael Olsson67add212023-05-31 19:00:48 +020094 },
95 // Mappings
96 {
97 RSC_MAPPING,
98 ResourceTable::NUM_RANGES,
99 {}
100 },
101 // Ranges
102 {
103 { 0, 0, 0 },
104 { 0, 0, 0 },
105 },
Ledion Dajab00e4ed2023-05-22 10:45:31 +0200106 // Trace buffer
107#if defined(REMOTEPROC_TRACE_BUFFER)
108 {
109 RSC_TRACE,
110 reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_start__),
111 reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_size__),
112 ResourceTable::RESERVED,
113 "Trace resource"
114 },
115#endif
116 // VDEV
117 {
118 RSC_VDEV,
119 VIRTIO_ID_RPMSG,
120 2, // Notify ID
121 1 << VIRTIO_RPMSG_F_NS,
122 0,
123 0,
124 0,
125 ResourceTable::NUM_VRINGS,
126 {ResourceTable::RESERVED, ResourceTable::RESERVED},
127 {}
128 },
129 // Vrings
130 {
131 {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 1, ResourceTable::RESERVED},
132 {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 2, ResourceTable::RESERVED}
133 },
134 // Carveout
135 {
136 RSC_CARVEOUT,
137 FW_RSC_U32_ADDR_ANY,
138 FW_RSC_U32_ADDR_ANY,
139 arenaSize * NUM_PARALLEL_TASKS,
140 0,
141 ResourceTable::RESERVED,
142 "TFLM arena"
143 }
144};
145// clang-format on
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100146}
147
148/*****************************************************************************
149 * Mailbox
150 *****************************************************************************/
151
152namespace {
153
154#ifdef MHU_V2
155Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
156#elif defined(MHU_JUNO)
157Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
158#else
159Mailbox::MHUDummy mailbox;
160#endif
161
162#ifdef MHU_IRQ
163void mailboxIrqHandler() {
164 LOG_DEBUG("");
165 mailbox.handleMessage();
166}
Mikael Olssonca4c3422023-09-21 10:05:54 +0200167#define MHU_IRQ_PRIORITY 5
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100168#endif
169
170} // namespace
171
172/*****************************************************************************
173 * main
174 *****************************************************************************/
175
176int main() {
177 printf("Ethos-U Message Handler OpenAMP\n");
178
Mikael Olsson67add212023-05-31 19:00:48 +0200179 auto rproc = std::make_shared<RProc>(mailbox, resourceTable.table, sizeof(resourceTable));
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100180 auto messageHandler = std::make_shared<MessageHandler>(*rproc, "ethos-u-0.0");
181
Kristofer Jonsson7716cee2023-02-10 13:30:16 +0100182 printf("TFLM arena. pa=%" PRIx32 ", da=%" PRIx32 ", len=%" PRIx32 "\n",
Kristofer Jonsson7716cee2023-02-10 13:30:16 +0100183 resourceTable.carveout.pa,
Mikael Olssonc078ced2023-06-05 15:59:27 +0200184 resourceTable.carveout.da,
Kristofer Jonsson7716cee2023-02-10 13:30:16 +0100185 resourceTable.carveout.len);
186
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100187 std::array<std::shared_ptr<InferenceRunner>, NUM_PARALLEL_TASKS> inferenceRunner;
188
189 for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) {
Mikael Olssonc078ced2023-06-05 15:59:27 +0200190 auto tensorArena = reinterpret_cast<uint8_t *>(resourceTable.carveout.da);
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100191
192 inferenceRunner[i] = std::make_shared<InferenceRunner>(&tensorArena[arenaSize * i],
193 arenaSize,
194 messageHandler->getInferenceQueue(),
195 messageHandler->getResponseQueue());
196 }
197
198#ifdef MHU_IRQ
199 // Register mailbox interrupt handler
Mikael Olssonca4c3422023-09-21 10:05:54 +0200200 NVIC_SetVector(static_cast<IRQn_Type>(MHU_IRQ), (uint32_t)&mailboxIrqHandler);
201 NVIC_SetPriority(static_cast<IRQn_Type>(MHU_IRQ), MHU_IRQ_PRIORITY);
202 NVIC_EnableIRQ(static_cast<IRQn_Type>(MHU_IRQ));
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100203#endif
204
205 // Start Scheduler
206 vTaskStartScheduler();
207
208 return 0;
209}