blob: 62bdc9707206a027b9c5f5d85b8c03c0b7d857fe [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>
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
53#define TENSOR_ARENA_SIZE 2000000
54#endif
55
56// TensorArena static initialisation
57constexpr size_t arenaSize = TENSOR_ARENA_SIZE;
58
59/*****************************************************************************
60 * Resource table
61 *****************************************************************************/
62
Ledion Dajab00e4ed2023-05-22 10:45:31 +020063#if defined(REMOTEPROC_TRACE_BUFFER)
64#if defined(__ARMCC_VERSION)
65extern uint32_t Image$$trace_buffer$$Base;
66extern uint32_t Image$$trace_buffer$$Length;
67#define __ethosu_core_trace_buffer_start__ Image$$trace_buffer$$Base
68#define __ethosu_core_trace_buffer_size__ Image$$trace_buffer$$Length
69#else
70extern uint32_t __ethosu_core_trace_buffer_start__;
71extern uint32_t __ethosu_core_trace_buffer_size__;
72#endif
73#endif
74
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010075extern "C" {
Ledion Dajab00e4ed2023-05-22 10:45:31 +020076// clang-format off
77__attribute__((section(".resource_table"))) ResourceTable resourceTable = {
78 // Table
79 {
80 ResourceTable::VERSION,
81 ResourceTable::NUM_RESOURCES,
82 {ResourceTable::RESERVED, ResourceTable::RESERVED},
83 {}
84 },
85 // Offset
86 {
Mikael Olsson67add212023-05-31 19:00:48 +020087 offsetof(ResourceTable, mapping),
Ledion Dajab00e4ed2023-05-22 10:45:31 +020088#if defined(REMOTEPROC_TRACE_BUFFER)
89 offsetof(ResourceTable, trace),
90#endif
91 offsetof(ResourceTable, vdev),
92 offsetof(ResourceTable, carveout),
Mikael Olsson67add212023-05-31 19:00:48 +020093 },
94 // Mappings
95 {
96 RSC_MAPPING,
97 ResourceTable::NUM_RANGES,
98 {}
99 },
100 // Ranges
101 {
102 { 0, 0, 0 },
103 { 0, 0, 0 },
104 },
Ledion Dajab00e4ed2023-05-22 10:45:31 +0200105 // Trace buffer
106#if defined(REMOTEPROC_TRACE_BUFFER)
107 {
108 RSC_TRACE,
109 reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_start__),
110 reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_size__),
111 ResourceTable::RESERVED,
112 "Trace resource"
113 },
114#endif
115 // VDEV
116 {
117 RSC_VDEV,
118 VIRTIO_ID_RPMSG,
119 2, // Notify ID
120 1 << VIRTIO_RPMSG_F_NS,
121 0,
122 0,
123 0,
124 ResourceTable::NUM_VRINGS,
125 {ResourceTable::RESERVED, ResourceTable::RESERVED},
126 {}
127 },
128 // Vrings
129 {
130 {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 1, ResourceTable::RESERVED},
131 {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 2, ResourceTable::RESERVED}
132 },
133 // Carveout
134 {
135 RSC_CARVEOUT,
136 FW_RSC_U32_ADDR_ANY,
137 FW_RSC_U32_ADDR_ANY,
138 arenaSize * NUM_PARALLEL_TASKS,
139 0,
140 ResourceTable::RESERVED,
141 "TFLM arena"
142 }
143};
144// clang-format on
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100145}
146
147/*****************************************************************************
148 * Mailbox
149 *****************************************************************************/
150
151namespace {
152
153#ifdef MHU_V2
154Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
155#elif defined(MHU_JUNO)
156Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
157#else
158Mailbox::MHUDummy mailbox;
159#endif
160
161#ifdef MHU_IRQ
162void mailboxIrqHandler() {
163 LOG_DEBUG("");
164 mailbox.handleMessage();
165}
Mikael Olssonca4c3422023-09-21 10:05:54 +0200166#define MHU_IRQ_PRIORITY 5
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100167#endif
168
169} // namespace
170
171/*****************************************************************************
172 * main
173 *****************************************************************************/
174
175int main() {
176 printf("Ethos-U Message Handler OpenAMP\n");
177
Mikael Olsson67add212023-05-31 19:00:48 +0200178 auto rproc = std::make_shared<RProc>(mailbox, resourceTable.table, sizeof(resourceTable));
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100179 auto messageHandler = std::make_shared<MessageHandler>(*rproc, "ethos-u-0.0");
180
Kristofer Jonsson7716cee2023-02-10 13:30:16 +0100181 printf("TFLM arena. pa=%" PRIx32 ", da=%" PRIx32 ", len=%" PRIx32 "\n",
182 resourceTable.carveout.da,
183 resourceTable.carveout.pa,
184 resourceTable.carveout.len);
185
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100186 std::array<std::shared_ptr<InferenceRunner>, NUM_PARALLEL_TASKS> inferenceRunner;
187
188 for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) {
189 auto tensorArena = static_cast<uint8_t *>(messageHandler->physicalToVirtual(resourceTable.carveout.pa));
190
191 inferenceRunner[i] = std::make_shared<InferenceRunner>(&tensorArena[arenaSize * i],
192 arenaSize,
193 messageHandler->getInferenceQueue(),
194 messageHandler->getResponseQueue());
195 }
196
197#ifdef MHU_IRQ
198 // Register mailbox interrupt handler
Mikael Olssonca4c3422023-09-21 10:05:54 +0200199 NVIC_SetVector(static_cast<IRQn_Type>(MHU_IRQ), (uint32_t)&mailboxIrqHandler);
200 NVIC_SetPriority(static_cast<IRQn_Type>(MHU_IRQ), MHU_IRQ_PRIORITY);
201 NVIC_EnableIRQ(static_cast<IRQn_Type>(MHU_IRQ));
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100202#endif
203
204 // Start Scheduler
205 vTaskStartScheduler();
206
207 return 0;
208}