Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 1 | /* |
| 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 Jonsson | 7716cee | 2023-02-10 13:30:16 +0100 | [diff] [blame] | 23 | #include <cinttypes> |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 24 | #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 |
| 48 | constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT; |
| 49 | #else |
| 50 | constexpr 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 |
| 58 | constexpr size_t arenaSize = TENSOR_ARENA_SIZE; |
| 59 | |
| 60 | /***************************************************************************** |
| 61 | * Resource table |
| 62 | *****************************************************************************/ |
| 63 | |
Ledion Daja | b00e4ed | 2023-05-22 10:45:31 +0200 | [diff] [blame^] | 64 | #if defined(REMOTEPROC_TRACE_BUFFER) |
| 65 | #if defined(__ARMCC_VERSION) |
| 66 | extern uint32_t Image$$trace_buffer$$Base; |
| 67 | extern 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 |
| 71 | extern uint32_t __ethosu_core_trace_buffer_start__; |
| 72 | extern uint32_t __ethosu_core_trace_buffer_size__; |
| 73 | #endif |
| 74 | #endif |
| 75 | |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 76 | extern "C" { |
Ledion Daja | b00e4ed | 2023-05-22 10:45:31 +0200 | [diff] [blame^] | 77 | // 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 | { |
| 88 | #if defined(REMOTEPROC_TRACE_BUFFER) |
| 89 | offsetof(ResourceTable, trace), |
| 90 | #endif |
| 91 | offsetof(ResourceTable, vdev), |
| 92 | offsetof(ResourceTable, carveout), |
| 93 | }, |
| 94 | // Trace buffer |
| 95 | #if defined(REMOTEPROC_TRACE_BUFFER) |
| 96 | { |
| 97 | RSC_TRACE, |
| 98 | reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_start__), |
| 99 | reinterpret_cast<uint32_t>(&__ethosu_core_trace_buffer_size__), |
| 100 | ResourceTable::RESERVED, |
| 101 | "Trace resource" |
| 102 | }, |
| 103 | #endif |
| 104 | // VDEV |
| 105 | { |
| 106 | RSC_VDEV, |
| 107 | VIRTIO_ID_RPMSG, |
| 108 | 2, // Notify ID |
| 109 | 1 << VIRTIO_RPMSG_F_NS, |
| 110 | 0, |
| 111 | 0, |
| 112 | 0, |
| 113 | ResourceTable::NUM_VRINGS, |
| 114 | {ResourceTable::RESERVED, ResourceTable::RESERVED}, |
| 115 | {} |
| 116 | }, |
| 117 | // Vrings |
| 118 | { |
| 119 | {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 1, ResourceTable::RESERVED}, |
| 120 | {FW_RSC_U32_ADDR_ANY, ResourceTable::VRING_ALIGN, ResourceTable::VRING_SIZE, 2, ResourceTable::RESERVED} |
| 121 | }, |
| 122 | // Carveout |
| 123 | { |
| 124 | RSC_CARVEOUT, |
| 125 | FW_RSC_U32_ADDR_ANY, |
| 126 | FW_RSC_U32_ADDR_ANY, |
| 127 | arenaSize * NUM_PARALLEL_TASKS, |
| 128 | 0, |
| 129 | ResourceTable::RESERVED, |
| 130 | "TFLM arena" |
| 131 | } |
| 132 | }; |
| 133 | // clang-format on |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /***************************************************************************** |
| 137 | * Mailbox |
| 138 | *****************************************************************************/ |
| 139 | |
| 140 | namespace { |
| 141 | |
| 142 | #ifdef MHU_V2 |
| 143 | Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase |
| 144 | #elif defined(MHU_JUNO) |
| 145 | Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS); |
| 146 | #else |
| 147 | Mailbox::MHUDummy mailbox; |
| 148 | #endif |
| 149 | |
| 150 | #ifdef MHU_IRQ |
| 151 | void mailboxIrqHandler() { |
| 152 | LOG_DEBUG(""); |
| 153 | mailbox.handleMessage(); |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | } // namespace |
| 158 | |
| 159 | /***************************************************************************** |
| 160 | * main |
| 161 | *****************************************************************************/ |
| 162 | |
| 163 | int main() { |
| 164 | printf("Ethos-U Message Handler OpenAMP\n"); |
| 165 | |
| 166 | auto mem = std::make_shared<MetalIO>(); |
| 167 | auto rproc = std::make_shared<RProc>(mailbox, resourceTable.table, sizeof(resourceTable), *mem); |
| 168 | auto messageHandler = std::make_shared<MessageHandler>(*rproc, "ethos-u-0.0"); |
| 169 | |
Kristofer Jonsson | 7716cee | 2023-02-10 13:30:16 +0100 | [diff] [blame] | 170 | printf("TFLM arena. pa=%" PRIx32 ", da=%" PRIx32 ", len=%" PRIx32 "\n", |
| 171 | resourceTable.carveout.da, |
| 172 | resourceTable.carveout.pa, |
| 173 | resourceTable.carveout.len); |
| 174 | |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 175 | std::array<std::shared_ptr<InferenceRunner>, NUM_PARALLEL_TASKS> inferenceRunner; |
| 176 | |
| 177 | for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) { |
| 178 | auto tensorArena = static_cast<uint8_t *>(messageHandler->physicalToVirtual(resourceTable.carveout.pa)); |
| 179 | |
| 180 | inferenceRunner[i] = std::make_shared<InferenceRunner>(&tensorArena[arenaSize * i], |
| 181 | arenaSize, |
| 182 | messageHandler->getInferenceQueue(), |
| 183 | messageHandler->getResponseQueue()); |
| 184 | } |
| 185 | |
| 186 | #ifdef MHU_IRQ |
| 187 | // Register mailbox interrupt handler |
| 188 | NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler); |
| 189 | NVIC_EnableIRQ((IRQn_Type)MHU_IRQ); |
| 190 | #endif |
| 191 | |
| 192 | // Start Scheduler |
| 193 | vTaskStartScheduler(); |
| 194 | |
| 195 | return 0; |
| 196 | } |