blob: 9d3c074b655aafaec0ea34d5db8ca03e7c82c7ad [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/*****************************************************************************
20 * Includes
21 *****************************************************************************/
22
Kristofer Jonsson7716cee2023-02-10 13:30:16 +010023#include <cinttypes>
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010024#include <memory>
25#include <stdio.h>
26
27#include <ethosu_log.h>
28#include <mailbox.hpp>
29
30#if defined(MHU_V2)
31#include <mhu_v2.hpp>
32#elif defined(MHU_JUNO)
33#include <mhu_juno.hpp>
34#else
35#include <mhu_dummy.hpp>
36#endif
37
38#include "inference_runner.hpp"
39#include "message_handler.hpp"
40#include "remoteproc.hpp"
41
42/*****************************************************************************
43 * TFLM arena
44 *****************************************************************************/
45
46// Number of parallell inference tasks. Typically one per NPU.
47#if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0
48constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT;
49#else
50constexpr size_t NUM_PARALLEL_TASKS = 1;
51#endif
52
53#ifndef TENSOR_ARENA_SIZE
54#define TENSOR_ARENA_SIZE 2000000
55#endif
56
57// TensorArena static initialisation
58constexpr size_t arenaSize = TENSOR_ARENA_SIZE;
59
60/*****************************************************************************
61 * Resource table
62 *****************************************************************************/
63
64extern "C" {
Kristofer Jonsson7716cee2023-02-10 13:30:16 +010065__attribute__((section(".resource_table"))) ResourceTable resourceTable(8, arenaSize *NUM_PARALLEL_TASKS);
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +010066}
67
68/*****************************************************************************
69 * Mailbox
70 *****************************************************************************/
71
72namespace {
73
74#ifdef MHU_V2
75Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
76#elif defined(MHU_JUNO)
77Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
78#else
79Mailbox::MHUDummy mailbox;
80#endif
81
82#ifdef MHU_IRQ
83void mailboxIrqHandler() {
84 LOG_DEBUG("");
85 mailbox.handleMessage();
86}
87#endif
88
89} // namespace
90
91/*****************************************************************************
92 * main
93 *****************************************************************************/
94
95int main() {
96 printf("Ethos-U Message Handler OpenAMP\n");
97
98 auto mem = std::make_shared<MetalIO>();
99 auto rproc = std::make_shared<RProc>(mailbox, resourceTable.table, sizeof(resourceTable), *mem);
100 auto messageHandler = std::make_shared<MessageHandler>(*rproc, "ethos-u-0.0");
101
Kristofer Jonsson7716cee2023-02-10 13:30:16 +0100102 printf("TFLM arena. pa=%" PRIx32 ", da=%" PRIx32 ", len=%" PRIx32 "\n",
103 resourceTable.carveout.da,
104 resourceTable.carveout.pa,
105 resourceTable.carveout.len);
106
Kristofer Jonsson3f5510f2023-02-08 14:23:00 +0100107 std::array<std::shared_ptr<InferenceRunner>, NUM_PARALLEL_TASKS> inferenceRunner;
108
109 for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) {
110 auto tensorArena = static_cast<uint8_t *>(messageHandler->physicalToVirtual(resourceTable.carveout.pa));
111
112 inferenceRunner[i] = std::make_shared<InferenceRunner>(&tensorArena[arenaSize * i],
113 arenaSize,
114 messageHandler->getInferenceQueue(),
115 messageHandler->getResponseQueue());
116 }
117
118#ifdef MHU_IRQ
119 // Register mailbox interrupt handler
120 NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
121 NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
122#endif
123
124 // Start Scheduler
125 vTaskStartScheduler();
126
127 return 0;
128}