Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 1 | /* |
Mikael Olsson | 10ecba8 | 2024-03-21 14:07:54 +0100 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 3 | * 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 Jonsson | 7716cee | 2023-02-10 13:30:16 +0100 | [diff] [blame] | 22 | #include <cinttypes> |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 23 | #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 |
| 47 | constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT; |
| 48 | #else |
| 49 | constexpr size_t NUM_PARALLEL_TASKS = 1; |
| 50 | #endif |
| 51 | |
| 52 | #ifndef TENSOR_ARENA_SIZE |
Mikael Olsson | 10ecba8 | 2024-03-21 14:07:54 +0100 | [diff] [blame] | 53 | #define TENSOR_ARENA_SIZE 0x200000 |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | // TensorArena static initialisation |
| 57 | constexpr size_t arenaSize = TENSOR_ARENA_SIZE; |
Mikael Olsson | 10ecba8 | 2024-03-21 14:07:54 +0100 | [diff] [blame] | 58 | static_assert(arenaSize && ((arenaSize & (arenaSize - 1)) == 0), "TENSOR_ARENA_SIZE must be a power of two"); |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 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 | { |
Mikael Olsson | 67add21 | 2023-05-31 19:00:48 +0200 | [diff] [blame] | 88 | offsetof(ResourceTable, mapping), |
Ledion Daja | b00e4ed | 2023-05-22 10:45:31 +0200 | [diff] [blame] | 89 | #if defined(REMOTEPROC_TRACE_BUFFER) |
| 90 | offsetof(ResourceTable, trace), |
| 91 | #endif |
| 92 | offsetof(ResourceTable, vdev), |
| 93 | offsetof(ResourceTable, carveout), |
Mikael Olsson | 67add21 | 2023-05-31 19:00:48 +0200 | [diff] [blame] | 94 | }, |
| 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 Daja | b00e4ed | 2023-05-22 10:45:31 +0200 | [diff] [blame] | 106 | // 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 Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /***************************************************************************** |
| 149 | * Mailbox |
| 150 | *****************************************************************************/ |
| 151 | |
| 152 | namespace { |
| 153 | |
| 154 | #ifdef MHU_V2 |
| 155 | Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase |
| 156 | #elif defined(MHU_JUNO) |
| 157 | Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS); |
| 158 | #else |
| 159 | Mailbox::MHUDummy mailbox; |
| 160 | #endif |
| 161 | |
| 162 | #ifdef MHU_IRQ |
| 163 | void mailboxIrqHandler() { |
| 164 | LOG_DEBUG(""); |
| 165 | mailbox.handleMessage(); |
| 166 | } |
Mikael Olsson | ca4c342 | 2023-09-21 10:05:54 +0200 | [diff] [blame] | 167 | #define MHU_IRQ_PRIORITY 5 |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 168 | #endif |
| 169 | |
| 170 | } // namespace |
| 171 | |
| 172 | /***************************************************************************** |
| 173 | * main |
| 174 | *****************************************************************************/ |
| 175 | |
| 176 | int main() { |
| 177 | printf("Ethos-U Message Handler OpenAMP\n"); |
| 178 | |
Mikael Olsson | 67add21 | 2023-05-31 19:00:48 +0200 | [diff] [blame] | 179 | auto rproc = std::make_shared<RProc>(mailbox, resourceTable.table, sizeof(resourceTable)); |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 180 | auto messageHandler = std::make_shared<MessageHandler>(*rproc, "ethos-u-0.0"); |
| 181 | |
Kristofer Jonsson | 7716cee | 2023-02-10 13:30:16 +0100 | [diff] [blame] | 182 | printf("TFLM arena. pa=%" PRIx32 ", da=%" PRIx32 ", len=%" PRIx32 "\n", |
Kristofer Jonsson | 7716cee | 2023-02-10 13:30:16 +0100 | [diff] [blame] | 183 | resourceTable.carveout.pa, |
Mikael Olsson | c078ced | 2023-06-05 15:59:27 +0200 | [diff] [blame] | 184 | resourceTable.carveout.da, |
Kristofer Jonsson | 7716cee | 2023-02-10 13:30:16 +0100 | [diff] [blame] | 185 | resourceTable.carveout.len); |
| 186 | |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 187 | std::array<std::shared_ptr<InferenceRunner>, NUM_PARALLEL_TASKS> inferenceRunner; |
| 188 | |
| 189 | for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) { |
Mikael Olsson | c078ced | 2023-06-05 15:59:27 +0200 | [diff] [blame] | 190 | auto tensorArena = reinterpret_cast<uint8_t *>(resourceTable.carveout.da); |
Kristofer Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 191 | |
| 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 Olsson | ca4c342 | 2023-09-21 10:05:54 +0200 | [diff] [blame] | 200 | 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 Jonsson | 3f5510f | 2023-02-08 14:23:00 +0100 | [diff] [blame] | 203 | #endif |
| 204 | |
| 205 | // Start Scheduler |
| 206 | vTaskStartScheduler(); |
| 207 | |
| 208 | return 0; |
| 209 | } |