aboutsummaryrefslogtreecommitdiff
path: root/applications/message_handler_openamp/main.cpp
blob: c120610707e03d6c606de2c0bcf7061630cac4ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use _this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*****************************************************************************
 * Includes
 *****************************************************************************/

#include <cinttypes>
#include <memory>
#include <stdio.h>

#include <ethosu_log.h>
#include <mailbox.hpp>

#if defined(MHU_V2)
#include <mhu_v2.hpp>
#elif defined(MHU_JUNO)
#include <mhu_juno.hpp>
#else
#include <mhu_dummy.hpp>
#endif

#include "inference_runner.hpp"
#include "message_handler.hpp"
#include "remoteproc.hpp"

/*****************************************************************************
 * TFLM arena
 *****************************************************************************/

// Number of parallell inference tasks. Typically one per NPU.
#if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0
constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT;
#else
constexpr size_t NUM_PARALLEL_TASKS = 1;
#endif

#ifndef TENSOR_ARENA_SIZE
#define TENSOR_ARENA_SIZE 2000000
#endif

// TensorArena static initialisation
constexpr size_t arenaSize = TENSOR_ARENA_SIZE;

/*****************************************************************************
 * Resource table
 *****************************************************************************/

#if defined(REMOTEPROC_TRACE_BUFFER)
#if defined(__ARMCC_VERSION)
extern uint32_t Image$$trace_buffer$$Base;
extern uint32_t Image$$trace_buffer$$Length;
#define __ethosu_core_trace_buffer_start__ Image$$trace_buffer$$Base
#define __ethosu_core_trace_buffer_size__  Image$$trace_buffer$$Length
#else
extern uint32_t __ethosu_core_trace_buffer_start__;
extern uint32_t __ethosu_core_trace_buffer_size__;
#endif
#endif

extern "C" {
// clang-format off
__attribute__((section(".resource_table"))) ResourceTable resourceTable = {
    // Table
    {
        ResourceTable::VERSION,
        ResourceTable::NUM_RESOURCES,
        {ResourceTable::RESERVED, ResourceTable::RESERVED},
        {}
    },
    // Offset
    {
        offsetof(ResourceTable, mapping),
#if defined(REMOTEPROC_TRACE_BUFFER)
        offsetof(ResourceTable, trace),
#endif
        offsetof(ResourceTable, vdev),
        offsetof(ResourceTable, carveout),
    },
    // Mappings
    {
        RSC_MAPPING,
        ResourceTable::NUM_RANGES,
        {}
    },
    // Ranges
    {
        { 0, 0, 0 },
        { 0, 0, 0 },
    },
    // Trace buffer
#if defined(REMOTEPROC_TRACE_BUFFER)
    {
        RSC_TRACE,
        reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_start__),
        reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_size__),
        ResourceTable::RESERVED,
        "Trace resource"
    },
#endif
    // VDEV
    {
        RSC_VDEV,
        VIRTIO_ID_RPMSG,
        2, // Notify ID
        1 << VIRTIO_RPMSG_F_NS,
        0,
        0,
        0,
        ResourceTable::NUM_VRINGS,
        {ResourceTable::RESERVED, ResourceTable::RESERVED},
        {}
    },
    // Vrings
    {
        {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 1, ResourceTable::RESERVED},
        {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 2, ResourceTable::RESERVED}
    },
    // Carveout
    {
        RSC_CARVEOUT,
        FW_RSC_U32_ADDR_ANY,
        FW_RSC_U32_ADDR_ANY,
        arenaSize * NUM_PARALLEL_TASKS,
        0,
        ResourceTable::RESERVED,
        "TFLM arena"
    }
};
// clang-format on
}

/*****************************************************************************
 * Mailbox
 *****************************************************************************/

namespace {

#ifdef MHU_V2
Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
#elif defined(MHU_JUNO)
Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
#else
Mailbox::MHUDummy mailbox;
#endif

#ifdef MHU_IRQ
void mailboxIrqHandler() {
    LOG_DEBUG("");
    mailbox.handleMessage();
}
#define MHU_IRQ_PRIORITY 5
#endif

} // namespace

/*****************************************************************************
 * main
 *****************************************************************************/

int main() {
    printf("Ethos-U Message Handler OpenAMP\n");

    auto rproc          = std::make_shared<RProc>(mailbox, resourceTable.table, sizeof(resourceTable));
    auto messageHandler = std::make_shared<MessageHandler>(*rproc, "ethos-u-0.0");

    printf("TFLM arena. pa=%" PRIx32 ", da=%" PRIx32 ", len=%" PRIx32 "\n",
           resourceTable.carveout.pa,
           resourceTable.carveout.da,
           resourceTable.carveout.len);

    std::array<std::shared_ptr<InferenceRunner>, NUM_PARALLEL_TASKS> inferenceRunner;

    for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) {
        auto tensorArena = reinterpret_cast<uint8_t *>(resourceTable.carveout.da);

        inferenceRunner[i] = std::make_shared<InferenceRunner>(&tensorArena[arenaSize * i],
                                                               arenaSize,
                                                               messageHandler->getInferenceQueue(),
                                                               messageHandler->getResponseQueue());
    }

#ifdef MHU_IRQ
    // Register mailbox interrupt handler
    NVIC_SetVector(static_cast<IRQn_Type>(MHU_IRQ), (uint32_t)&mailboxIrqHandler);
    NVIC_SetPriority(static_cast<IRQn_Type>(MHU_IRQ), MHU_IRQ_PRIORITY);
    NVIC_EnableIRQ(static_cast<IRQn_Type>(MHU_IRQ));
#endif

    // Start Scheduler
    vTaskStartScheduler();

    return 0;
}