From 3f5510fa3444217a442ac2e63ae50b27c9c26cf3 Mon Sep 17 00:00:00 2001 From: Kristofer Jonsson Date: Wed, 8 Feb 2023 14:23:00 +0100 Subject: Updating message handler firmware The 'message handler' firmware was based on a custom interface between Linux and the firmware. Because the kernel driver has been converted into a rpmsg driver, the 'message handler' application has been updated into an OpenAMP based firmware. Change-Id: I1339180c4f53cbad42501a2827863b7b49561ff4 --- applications/CMakeLists.txt | 4 +- applications/message_handler/CMakeLists.txt | 63 --- .../indexed_networks/indexed_networks.hpp | 105 ----- .../indexed_networks/network_template.hpp | 23 - applications/message_handler/lib/CMakeLists.txt | 38 -- .../message_handler/lib/core_driver_mutex.cpp | 81 ---- .../message_handler/lib/freertos_allocator.cpp | 45 -- .../lib/include/message_handler.hpp | 187 -------- .../message_handler/lib/include/message_queue.hpp | 74 --- .../message_handler/lib/include/networks.hpp | 64 --- .../message_handler/lib/message_handler.cpp | 523 --------------------- applications/message_handler/lib/message_queue.cpp | 151 ------ applications/message_handler/main.cpp | 194 -------- applications/message_handler/test/CMakeLists.txt | 62 --- .../test/cancel_reject_inference_test.cpp | 255 ---------- .../message_handler/test/message_client.cpp | 91 ---- .../message_handler/test/message_client.hpp | 54 --- .../message_handler/test/run_inference_test.cpp | 418 ---------------- .../message_handler/test/test_assertions.hpp | 33 -- applications/message_handler/test/test_helpers.hpp | 131 ------ .../test/test_message_handler/CMakeLists.txt | 38 -- .../test/test_message_handler/main.cpp | 151 ------ .../message_handler_openamp/CMakeLists.txt | 76 +++ .../message_handler_openamp/core_driver_mutex.cpp | 81 ++++ .../message_handler_openamp/freertos_allocator.cpp | 45 ++ .../message_handler_openamp/inference_runner.cpp | 194 ++++++++ .../message_handler_openamp/inference_runner.hpp | 56 +++ applications/message_handler_openamp/main.cpp | 122 +++++ .../message_handler_openamp/message_handler.cpp | 437 +++++++++++++++++ .../message_handler_openamp/message_handler.hpp | 106 +++++ applications/message_handler_openamp/queue.hpp | 87 ++++ .../message_handler_openamp/remoteproc.cpp | 243 ++++++++++ .../message_handler_openamp/remoteproc.hpp | 200 ++++++++ 33 files changed, 1649 insertions(+), 2783 deletions(-) delete mode 100644 applications/message_handler/CMakeLists.txt delete mode 100644 applications/message_handler/indexed_networks/indexed_networks.hpp delete mode 100644 applications/message_handler/indexed_networks/network_template.hpp delete mode 100644 applications/message_handler/lib/CMakeLists.txt delete mode 100644 applications/message_handler/lib/core_driver_mutex.cpp delete mode 100644 applications/message_handler/lib/freertos_allocator.cpp delete mode 100644 applications/message_handler/lib/include/message_handler.hpp delete mode 100644 applications/message_handler/lib/include/message_queue.hpp delete mode 100644 applications/message_handler/lib/include/networks.hpp delete mode 100644 applications/message_handler/lib/message_handler.cpp delete mode 100644 applications/message_handler/lib/message_queue.cpp delete mode 100644 applications/message_handler/main.cpp delete mode 100644 applications/message_handler/test/CMakeLists.txt delete mode 100644 applications/message_handler/test/cancel_reject_inference_test.cpp delete mode 100644 applications/message_handler/test/message_client.cpp delete mode 100644 applications/message_handler/test/message_client.hpp delete mode 100644 applications/message_handler/test/run_inference_test.cpp delete mode 100644 applications/message_handler/test/test_assertions.hpp delete mode 100644 applications/message_handler/test/test_helpers.hpp delete mode 100644 applications/message_handler/test/test_message_handler/CMakeLists.txt delete mode 100644 applications/message_handler/test/test_message_handler/main.cpp create mode 100644 applications/message_handler_openamp/CMakeLists.txt create mode 100644 applications/message_handler_openamp/core_driver_mutex.cpp create mode 100644 applications/message_handler_openamp/freertos_allocator.cpp create mode 100644 applications/message_handler_openamp/inference_runner.cpp create mode 100644 applications/message_handler_openamp/inference_runner.hpp create mode 100644 applications/message_handler_openamp/main.cpp create mode 100644 applications/message_handler_openamp/message_handler.cpp create mode 100644 applications/message_handler_openamp/message_handler.hpp create mode 100644 applications/message_handler_openamp/queue.hpp create mode 100644 applications/message_handler_openamp/remoteproc.cpp create mode 100644 applications/message_handler_openamp/remoteproc.hpp diff --git a/applications/CMakeLists.txt b/applications/CMakeLists.txt index 11ed236..1fa2b2e 100644 --- a/applications/CMakeLists.txt +++ b/applications/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2021 Arm Limited. All rights reserved. +# SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates # # SPDX-License-Identifier: Apache-2.0 # @@ -26,7 +26,7 @@ add_subdirectory(hello_world) add_subdirectory(threadx_demo) -add_subdirectory(message_handler) +add_subdirectory(message_handler_openamp) if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang") # Only armclang supported for now diff --git a/applications/message_handler/CMakeLists.txt b/applications/message_handler/CMakeLists.txt deleted file mode 100644 index 5e95bdd..0000000 --- a/applications/message_handler/CMakeLists.txt +++ /dev/null @@ -1,63 +0,0 @@ -# -# Copyright (c) 2020-2022 Arm Limited. -# -# 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. -# - -if(NOT TARGET freertos_kernel) - message("Skipping message handler") - return() -endif() - -# Split total tensor arena equally for each NPU -if(TARGET ethosu_core_driver AND ETHOSU_TARGET_NPU_COUNT GREATER 0) - set(NUM_ARENAS ${ETHOSU_TARGET_NPU_COUNT}) -else() - set(NUM_ARENAS 1) -endif() - -set(MESSAGE_HANDLER_ARENA_SIZE 2000000 CACHE STRING "Total size of all message handler tensor arenas") -math(EXPR TENSOR_ARENA_SIZE "${MESSAGE_HANDLER_ARENA_SIZE} / ${NUM_ARENAS}") - -add_subdirectory(lib) -add_subdirectory(test) - -set(MESSAGE_HANDLER_MODEL_0 "" CACHE STRING "Path to built in model 0") -set(MESSAGE_HANDLER_MODEL_1 "" CACHE STRING "Path to built in model 1") -set(MESSAGE_HANDLER_MODEL_2 "" CACHE STRING "Path to built in model 2") -set(MESSAGE_HANDLER_MODEL_3 "" CACHE STRING "Path to built in model 3") - -ethosu_add_executable(message_handler - SOURCES - main.cpp - LIBRARIES - message_handler_lib - freertos_kernel) - -target_include_directories(message_handler PRIVATE - indexed_networks - ${LINUX_DRIVER_STACK_PATH}/kernel) - -target_compile_definitions(message_handler PRIVATE - TENSOR_ARENA_SIZE=${TENSOR_ARENA_SIZE} - $<$:MODEL_0=${MESSAGE_HANDLER_MODEL_0}> - $<$:MODEL_1=${MESSAGE_HANDLER_MODEL_1}> - $<$:MODEL_2=${MESSAGE_HANDLER_MODEL_2}> - $<$:MODEL_3=${MESSAGE_HANDLER_MODEL_3}>) - -install(FILES $ - DESTINATION "lib/firmware" - RENAME "arm-${ETHOSU_TARGET_NPU_CONFIG}.fw" -) diff --git a/applications/message_handler/indexed_networks/indexed_networks.hpp b/applications/message_handler/indexed_networks/indexed_networks.hpp deleted file mode 100644 index d37ddba..0000000 --- a/applications/message_handler/indexed_networks/indexed_networks.hpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#ifndef INDEXED_NETWORKS_H -#define INDEXED_NETWORKS_H - -#include "networks.hpp" - -#include -#include - -#define XSTRINGIFY(src) #src -#define STRINGIFY(src) XSTRINGIFY(src) - -namespace { -#if defined(__has_include) - -#if defined(MODEL_0) -namespace Model0 { -#include STRINGIFY(MODEL_0) -} -#endif - -#if defined(MODEL_1) -namespace Model1 { -#include STRINGIFY(MODEL_1) -} -#endif - -#if defined(MODEL_2) -namespace Model2 { -#include STRINGIFY(MODEL_2) -} -#endif - -#if defined(MODEL_3) -namespace Model3 { -#include STRINGIFY(MODEL_3) -} -#endif - -#endif -} // namespace - -namespace MessageHandler { - -class WithIndexedNetworks : public BaseNetworks { -public: - static bool getIndexedNetwork(const uint32_t index, void *&data, size_t &size) { - switch (index) { -#if defined(MODEL_0) - case 0: - data = reinterpret_cast(Model0::networkModelData); - size = sizeof(Model0::networkModelData); - break; -#endif - -#if defined(MODEL_1) - case 1: - data = reinterpret_cast(Model1::networkModelData); - size = sizeof(Model1::networkModelData); - break; -#endif - -#if defined(MODEL_2) - case 2: - data = reinterpret_cast(Model2::networkModelData); - size = sizeof(Model2::networkModelData); - break; -#endif - -#if defined(MODEL_3) - case 3: - data = reinterpret_cast(Model3::networkModelData); - size = sizeof(Model3::networkModelData); - break; -#endif - - default: - printf("Error: Network model index out of range. index=%" PRIu32 "\n", index); - return true; - } - - return false; - } -}; - -} // namespace MessageHandler - -#endif diff --git a/applications/message_handler/indexed_networks/network_template.hpp b/applications/message_handler/indexed_networks/network_template.hpp deleted file mode 100644 index a477b84..0000000 --- a/applications/message_handler/indexed_networks/network_template.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#include - -__attribute__((section(".sram.data"), aligned(16))) uint8_t networkModelData[] = { - /* Add network model here */ -}; diff --git a/applications/message_handler/lib/CMakeLists.txt b/applications/message_handler/lib/CMakeLists.txt deleted file mode 100644 index c17742a..0000000 --- a/applications/message_handler/lib/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -# -# Copyright (c) 2020-2022 Arm Limited. -# -# 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. -# - -add_library(message_handler_lib STATIC) - -target_include_directories(message_handler_lib PUBLIC include - PRIVATE ${LINUX_DRIVER_STACK_PATH}/kernel) - -target_link_libraries(message_handler_lib - PUBLIC - ethosu_mailbox - $<$:ethosu_core_driver> - inference_process - PRIVATE - cmsis_device - freertos_kernel - tflu) - -target_sources(message_handler_lib PRIVATE - message_handler.cpp - message_queue.cpp - core_driver_mutex.cpp - freertos_allocator.cpp) diff --git a/applications/message_handler/lib/core_driver_mutex.cpp b/applications/message_handler/lib/core_driver_mutex.cpp deleted file mode 100644 index d023ad2..0000000 --- a/applications/message_handler/lib/core_driver_mutex.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates - * - * 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. - */ - -#if defined(ETHOSU) - -#include "FreeRTOS.h" -#include "semphr.h" - -#include -#include - -extern "C" { - -void *ethosu_mutex_create(void) { - return xSemaphoreCreateMutex(); -} - -int ethosu_mutex_lock(void *mutex) { - SemaphoreHandle_t handle = reinterpret_cast(mutex); - if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { - printf("Error: Failed to lock mutex.\n"); - return -1; - } - return 0; -} - -int ethosu_mutex_unlock(void *mutex) { - SemaphoreHandle_t handle = reinterpret_cast(mutex); - if (xSemaphoreGive(handle) != pdTRUE) { - printf("Error: Failed to unlock mutex.\n"); - return -1; - } - return 0; -} - -void *ethosu_semaphore_create(void) { - return xSemaphoreCreateCounting(255, 0); -} - -int ethosu_semaphore_take(void *sem) { - SemaphoreHandle_t handle = reinterpret_cast(sem); - if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { - printf("Error: Failed to take semaphore.\n"); - return -1; - } - return 0; -} - -int ethosu_semaphore_give(void *sem) { - SemaphoreHandle_t handle = reinterpret_cast(sem); - if (xPortIsInsideInterrupt()) { - if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) { - printf("Error: Failed to give semaphore from ISR.\n"); - return -1; - } - } else { - if (xSemaphoreGive(handle) != pdTRUE) { - printf("Error: Failed to give semaphore.\n"); - return -1; - } - } - return 0; -} -} - -#endif diff --git a/applications/message_handler/lib/freertos_allocator.cpp b/applications/message_handler/lib/freertos_allocator.cpp deleted file mode 100644 index 883ada8..0000000 --- a/applications/message_handler/lib/freertos_allocator.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#include "FreeRTOS.h" -#include -#include - -void *operator new(size_t size) { - return pvPortMalloc(size); -} - -void *operator new[](size_t size) { - return pvPortMalloc(size); -} - -void operator delete(void *ptr) noexcept { - vPortFree(ptr); -} - -void operator delete(void *ptr, std::size_t) noexcept { - vPortFree(ptr); -} - -void operator delete[](void *ptr) noexcept { - vPortFree(ptr); -} - -void operator delete[](void *ptr, std::size_t) noexcept { - vPortFree(ptr); -} diff --git a/applications/message_handler/lib/include/message_handler.hpp b/applications/message_handler/lib/include/message_handler.hpp deleted file mode 100644 index 98875f4..0000000 --- a/applications/message_handler/lib/include/message_handler.hpp +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright (c) 2020-2022 Arm Limited. - * - * 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. - */ - -#ifndef MESSAGE_HANDLER_H -#define MESSAGE_HANDLER_H - -#include "FreeRTOS.h" -#include "queue.h" -#include "semphr.h" - -#include "message_queue.hpp" -#include "networks.hpp" -#include -#if defined(ETHOSU) -#include -#endif -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace MessageHandler { - -template -class Queue { -public: - using Predicate = std::function; - - Queue() { - mutex = xSemaphoreCreateMutex(); - size = xSemaphoreCreateCounting(capacity, 0u); - - if (mutex == nullptr || size == nullptr) { - printf("Error: failed to allocate memory for inference queue\n"); - } - } - - ~Queue() { - vSemaphoreDelete(mutex); - vSemaphoreDelete(size); - } - - bool push(const T &data) { - xSemaphoreTake(mutex, portMAX_DELAY); - if (list.size() >= capacity) { - xSemaphoreGive(mutex); - return false; - } - - list.push_back(data); - xSemaphoreGive(mutex); - - // increase number of available inferences to pop - xSemaphoreGive(size); - return true; - } - - void pop(T &data) { - // decrease the number of available inferences to pop - xSemaphoreTake(size, portMAX_DELAY); - - xSemaphoreTake(mutex, portMAX_DELAY); - data = list.front(); - list.pop_front(); - xSemaphoreGive(mutex); - } - - bool erase(Predicate pred) { - // let's optimistically assume we are removing an inference, so decrease pop - if (pdFALSE == xSemaphoreTake(size, 0)) { - // if there are no inferences return immediately - return false; - } - - xSemaphoreTake(mutex, portMAX_DELAY); - auto found = std::find_if(list.begin(), list.end(), pred); - bool erased = found != list.end(); - if (erased) { - list.erase(found); - } - xSemaphoreGive(mutex); - - if (!erased) { - // no inference erased, so let's put the size count back - xSemaphoreGive(size); - } - - return erased; - } - -private: - std::list list; - - SemaphoreHandle_t mutex; - SemaphoreHandle_t size; -}; - -class IncomingMessageHandler { -public: - IncomingMessageHandler(EthosU::ethosu_core_queue &inputMessageQueue, - EthosU::ethosu_core_queue &outputMessageQueue, - Mailbox::Mailbox &mailbox, - std::shared_ptr> inferenceInputQueue, - QueueHandle_t inferenceOutputQueue, - SemaphoreHandle_t messageNotify, - std::shared_ptr networks); - - void run(); - -private: - bool handleMessage(); - bool handleInferenceOutput(); - static void handleIrq(void *userArg); - - void sendPong(); - void sendErrorAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message); - void sendVersionRsp(); - void sendCapabilitiesRsp(uint64_t userArg); - void sendNetworkInfoRsp(uint64_t userArg, EthosU::ethosu_core_network_buffer &network); - void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference); - void sendFailedInferenceRsp(uint64_t userArg, uint32_t status); - void sendCancelInferenceRsp(uint64_t userArg, uint32_t status); - void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp); - - MessageQueue::QueueImpl inputMessageQueue; - MessageQueue::QueueImpl outputMessageQueue; - Mailbox::Mailbox &mailbox; - InferenceProcess::InferenceParser parser; - std::shared_ptr> inferenceInputQueue; - QueueHandle_t inferenceOutputQueue; - SemaphoreHandle_t messageNotify; - EthosU::ethosu_core_msg_capabilities_rsp capabilities; - std::shared_ptr networks; -}; - -class InferenceHandler { -public: - InferenceHandler(uint8_t *tensorArena, - size_t arenaSize, - std::shared_ptr> inferenceInputQueue, - QueueHandle_t inferenceOutputQueue, - SemaphoreHandle_t messageNotify, - std::shared_ptr networks); - - void run(); - -private: - void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp); - bool getInferenceJob(const EthosU::ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job); - -#if defined(ETHOSU) - friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg); - friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg); -#endif - std::shared_ptr> inferenceInputQueue; - QueueHandle_t inferenceOutputQueue; - SemaphoreHandle_t messageNotify; - InferenceProcess::InferenceProcess inference; - EthosU::ethosu_core_inference_req *currentReq; - EthosU::ethosu_core_inference_rsp *currentRsp; - std::shared_ptr networks; -}; - -} // namespace MessageHandler - -#endif diff --git a/applications/message_handler/lib/include/message_queue.hpp b/applications/message_handler/lib/include/message_queue.hpp deleted file mode 100644 index ec9d7b6..0000000 --- a/applications/message_handler/lib/include/message_queue.hpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2020-2022 Arm Limited. - * - * 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. - */ - -#ifndef MESSAGE_QUEUE_H -#define MESSAGE_QUEUE_H - -#include -#include - -namespace MessageQueue { - -template -struct Queue { - EthosU::ethosu_core_queue_header header; - uint8_t data[SIZE]; - - constexpr Queue() : header({SIZE, 0, {0}, 0}) {} - - constexpr EthosU::ethosu_core_queue *toQueue() { - return reinterpret_cast(&header); - } -}; - -class QueueImpl { -public: - struct Vec { - const void *base; - size_t length; - }; - - QueueImpl(EthosU::ethosu_core_queue &queue); - - bool empty() const; - size_t available() const; - size_t capacity() const; - void reset(); - bool read(uint8_t *dst, uint32_t length); - template - bool read(T &dst) { - return read(reinterpret_cast(&dst), sizeof(dst)); - } - bool write(const Vec *vec, size_t length); - bool write(const uint32_t type, const void *src = nullptr, uint32_t length = 0); - template - bool write(const uint32_t type, const T &src) { - return write(type, reinterpret_cast(&src), sizeof(src)); - } - -private: - void cleanHeader() const; - void cleanHeaderData() const; - void invalidateHeader() const; - void invalidateHeaderData() const; - - EthosU::ethosu_core_queue &queue; -}; -} // namespace MessageQueue - -#endif diff --git a/applications/message_handler/lib/include/networks.hpp b/applications/message_handler/lib/include/networks.hpp deleted file mode 100644 index eb01d10..0000000 --- a/applications/message_handler/lib/include/networks.hpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#ifndef NETWORKS_H -#define NETWORKS_H - -#include - -#include -#include - -using namespace EthosU; - -namespace MessageHandler { - -class Networks { -public: - virtual ~Networks() {} - virtual bool getNetwork(const ethosu_core_network_buffer &buffer, void *&data, size_t &size) = 0; -}; - -template -class BaseNetworks : public Networks { -public: - bool getNetwork(const ethosu_core_network_buffer &buffer, void *&data, size_t &size) override { - switch (buffer.type) { - case ETHOSU_CORE_NETWORK_BUFFER: - data = reinterpret_cast(buffer.buffer.ptr); - size = buffer.buffer.size; - return false; - case ETHOSU_CORE_NETWORK_INDEX: - return T::getIndexedNetwork(buffer.index, data, size); - default: - printf("Error: Unsupported network model type. type=%" PRIu32 "\n", buffer.type); - return true; - } - } -}; - -class NoIndexedNetworks : public BaseNetworks { - static bool getIndexedNetwork(const uint32_t index, void *&data, size_t &size) { - printf("Error: Network model index out of range. index=%" PRIu32 "\n", index); - return true; - } -}; - -} // namespace MessageHandler - -#endif diff --git a/applications/message_handler/lib/message_handler.cpp b/applications/message_handler/lib/message_handler.cpp deleted file mode 100644 index 66623f9..0000000 --- a/applications/message_handler/lib/message_handler.cpp +++ /dev/null @@ -1,523 +0,0 @@ -/* - * Copyright (c) 2020-2022 Arm Limited. - * - * 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. - */ - -#include "message_handler.hpp" - -#include "cmsis_compiler.h" - -#ifdef ETHOSU -#include -#include -#endif - -#include "FreeRTOS.h" -#include "queue.h" -#include "semphr.h" - -#include -#include - -using namespace EthosU; -using namespace MessageQueue; - -namespace MessageHandler { - -/**************************************************************************** - * IncomingMessageHandler - ****************************************************************************/ - -IncomingMessageHandler::IncomingMessageHandler( - EthosU::ethosu_core_queue &_inputMessageQueue, - EthosU::ethosu_core_queue &_outputMessageQueue, - Mailbox::Mailbox &_mailbox, - std::shared_ptr> _inferenceInputQueue, - QueueHandle_t _inferenceOutputQueue, - SemaphoreHandle_t _messageNotify, - std::shared_ptr _networks) : - inputMessageQueue(_inputMessageQueue), - outputMessageQueue(_outputMessageQueue), mailbox(_mailbox), inferenceInputQueue(_inferenceInputQueue), - inferenceOutputQueue(_inferenceOutputQueue), messageNotify(_messageNotify), networks(_networks) { - mailbox.registerCallback(handleIrq, reinterpret_cast(this)); - readCapabilties(capabilities); -} - -void IncomingMessageHandler::run() { - while (true) { - // Wait for event - xSemaphoreTake(messageNotify, portMAX_DELAY); - - // Handle all inference outputs and all messages in queue - while (handleInferenceOutput() || handleMessage()) {} - } -} - -void IncomingMessageHandler::handleIrq(void *userArg) { - if (userArg == nullptr) { - return; - } - IncomingMessageHandler *_this = reinterpret_cast(userArg); - xSemaphoreGiveFromISR(_this->messageNotify, nullptr); -} - -void IncomingMessageHandler::sendErrorAndResetQueue(ethosu_core_msg_err_type type, const char *message) { - ethosu_core_msg_err error; - error.type = type; - - for (size_t i = 0; i < sizeof(error.msg) && message[i]; i++) { - error.msg[i] = message[i]; - } - printf("ERROR: Msg: \"%s\"\n", error.msg); - - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_ERR, error)) { - printf("ERROR: Msg: Failed to write error response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } - inputMessageQueue.reset(); -} - -bool IncomingMessageHandler::handleInferenceOutput() { - struct ethosu_core_inference_rsp rsp; - if (pdTRUE != xQueueReceive(inferenceOutputQueue, &rsp, 0)) { - return false; - } - - sendInferenceRsp(rsp); - return true; -} - -bool IncomingMessageHandler::handleMessage() { - struct ethosu_core_msg msg; - - if (inputMessageQueue.available() == 0) { - return false; - } - - // Read msg header - // Only process a complete message header, else send error message - // and reset queue - if (!inputMessageQueue.read(msg)) { - sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_SIZE, "Failed to read a complete header"); - return false; - } - - printf("Msg: header magic=%" PRIX32 ", type=%" PRIu32 ", length=%" PRIu32 "\n", msg.magic, msg.type, msg.length); - - if (msg.magic != ETHOSU_CORE_MSG_MAGIC) { - printf("Msg: Invalid Magic\n"); - sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_MAGIC, "Invalid magic"); - return false; - } - - switch (msg.type) { - case ETHOSU_CORE_MSG_PING: { - printf("Msg: Ping\n"); - sendPong(); - break; - } - case ETHOSU_CORE_MSG_ERR: { - ethosu_core_msg_err error; - if (!inputMessageQueue.read(error)) { - printf("ERROR: Msg: Failed to receive error message\n"); - } else { - printf("Msg: Received an error response, type=%" PRIu32 ", msg=\"%s\"\n", error.type, error.msg); - } - - inputMessageQueue.reset(); - return false; - } - case ETHOSU_CORE_MSG_VERSION_REQ: { - printf("Msg: Version request\n"); - sendVersionRsp(); - break; - } - case ETHOSU_CORE_MSG_CAPABILITIES_REQ: { - ethosu_core_capabilities_req req; - if (!inputMessageQueue.read(req)) { - sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "CapabilitiesReq. Failed to read payload"); - break; - } - - printf("Msg: Capabilities request.user_arg=0x%" PRIx64 "\n", req.user_arg); - sendCapabilitiesRsp(req.user_arg); - break; - } - case ETHOSU_CORE_MSG_INFERENCE_REQ: { - ethosu_core_inference_req req; - if (!inputMessageQueue.read(req)) { - sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "InferenceReq. Failed to read payload"); - break; - } - - printf("Msg: InferenceReq. user_arg=0x%" PRIx64 ", network_type=%" PRIu32 ", ", req.user_arg, req.network.type); - - if (req.network.type == ETHOSU_CORE_NETWORK_BUFFER) { - printf("network.buffer={0x%" PRIx32 ", %" PRIu32 "},\n", req.network.buffer.ptr, req.network.buffer.size); - } else { - printf("network.index=%" PRIu32 ",\n", req.network.index); - } - - printf("ifm_count=%" PRIu32 ", ifm=[", req.ifm_count); - for (uint32_t i = 0; i < req.ifm_count; ++i) { - if (i > 0) { - printf(", "); - } - - printf("{0x%" PRIx32 ", %" PRIu32 "}", req.ifm[i].ptr, req.ifm[i].size); - } - printf("]"); - - printf(", ofm_count=%" PRIu32 ", ofm=[", req.ofm_count); - for (uint32_t i = 0; i < req.ofm_count; ++i) { - if (i > 0) { - printf(", "); - } - - printf("{0x%" PRIx32 ", %" PRIu32 "}", req.ofm[i].ptr, req.ofm[i].size); - } - printf("]\n"); - - if (!inferenceInputQueue->push(req)) { - printf("Msg: Inference queue full. Rejecting inference user_arg=0x%" PRIx64 "\n", req.user_arg); - sendFailedInferenceRsp(req.user_arg, ETHOSU_CORE_STATUS_REJECTED); - } - break; - } - case ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ: { - ethosu_core_cancel_inference_req req; - if (!inputMessageQueue.read(req)) { - sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "CancelInferenceReq. Failed to read payload"); - break; - } - printf("Msg: CancelInferenceReq. user_arg=0x%" PRIx64 ", inference_handle=0x%" PRIx64 "\n", - req.user_arg, - req.inference_handle); - - bool found = - inferenceInputQueue->erase([req](auto &inf_req) { return inf_req.user_arg == req.inference_handle; }); - - // NOTE: send an inference response with status ABORTED if the inference has been droped from the queue - if (found) { - sendFailedInferenceRsp(req.inference_handle, ETHOSU_CORE_STATUS_ABORTED); - } - - sendCancelInferenceRsp(req.user_arg, found ? ETHOSU_CORE_STATUS_OK : ETHOSU_CORE_STATUS_ERROR); - break; - } - case ETHOSU_CORE_MSG_NETWORK_INFO_REQ: { - ethosu_core_network_info_req req; - if (!inputMessageQueue.read(req)) { - sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "NetworkInfoReq. Failed to read payload"); - break; - } - - printf("Msg: NetworkInfoReq. user_arg=0x%" PRIx64 "\n", req.user_arg); - sendNetworkInfoRsp(req.user_arg, req.network); - break; - } - default: { - char errMsg[128]; - snprintf(&errMsg[0], - sizeof(errMsg), - "Msg: Unknown type: %" PRIu32 " with payload length %" PRIu32 " bytes\n", - msg.type, - msg.length); - - sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_UNSUPPORTED_TYPE, errMsg); - return false; - } - } - - return true; -} - -void IncomingMessageHandler::sendPong() { - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_PONG)) { - printf("ERROR: Msg: Failed to write pong response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } -} - -void IncomingMessageHandler::sendVersionRsp() { - ethosu_core_msg_version version = { - ETHOSU_CORE_MSG_VERSION_MAJOR, - ETHOSU_CORE_MSG_VERSION_MINOR, - ETHOSU_CORE_MSG_VERSION_PATCH, - 0, - }; - - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_VERSION_RSP, version)) { - printf("ERROR: Failed to write version response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } -} - -void IncomingMessageHandler::sendCapabilitiesRsp(uint64_t userArg) { - capabilities.user_arg = userArg; - - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_CAPABILITIES_RSP, capabilities)) { - printf("ERROR: Failed to write capabilities response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } -} - -void IncomingMessageHandler::sendNetworkInfoRsp(uint64_t userArg, ethosu_core_network_buffer &network) { - ethosu_core_network_info_rsp rsp; - rsp.user_arg = userArg; - rsp.ifm_count = 0; - rsp.ofm_count = 0; - - void *buffer; - size_t size; - - bool failed = networks->getNetwork(network, buffer, size); - - if (!failed) { - failed = parser.parseModel(buffer, - size, - rsp.desc, - InferenceProcess::makeArray(rsp.ifm_size, rsp.ifm_count, ETHOSU_CORE_BUFFER_MAX), - InferenceProcess::makeArray(rsp.ofm_size, rsp.ofm_count, ETHOSU_CORE_BUFFER_MAX)); - } - rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; - - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp)) { - printf("ERROR: Msg: Failed to write network info response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } -} - -void IncomingMessageHandler::sendInferenceRsp(ethosu_core_inference_rsp &rsp) { - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)) { - printf("ERROR: Msg: Failed to write inference response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } -} - -void IncomingMessageHandler::sendFailedInferenceRsp(uint64_t userArg, uint32_t status) { - ethosu_core_inference_rsp rsp; - rsp.user_arg = userArg; - rsp.status = status; - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)) { - printf("ERROR: Msg: Failed to write inference response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } -} -void IncomingMessageHandler::sendCancelInferenceRsp(uint64_t userArg, uint32_t status) { - ethosu_core_cancel_inference_rsp cancellation; - cancellation.user_arg = userArg; - cancellation.status = status; - if (!outputMessageQueue.write(ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP, cancellation)) { - printf("ERROR: Msg: Failed to write cancel inference response. No mailbox message sent\n"); - } else { - mailbox.sendMessage(); - } -} - -void IncomingMessageHandler::readCapabilties(ethosu_core_msg_capabilities_rsp &rsp) { - rsp = {}; - -#ifdef ETHOSU - struct ethosu_driver_version version; - ethosu_get_driver_version(&version); - - struct ethosu_hw_info info; - struct ethosu_driver *drv = ethosu_reserve_driver(); - ethosu_get_hw_info(drv, &info); - ethosu_release_driver(drv); - - rsp.user_arg = 0; - rsp.version_status = info.version.version_status; - rsp.version_minor = info.version.version_minor; - rsp.version_major = info.version.version_major; - rsp.product_major = info.version.product_major; - rsp.arch_patch_rev = info.version.arch_patch_rev; - rsp.arch_minor_rev = info.version.arch_minor_rev; - rsp.arch_major_rev = info.version.arch_major_rev; - rsp.driver_patch_rev = version.patch; - rsp.driver_minor_rev = version.minor; - rsp.driver_major_rev = version.major; - rsp.macs_per_cc = info.cfg.macs_per_cc; - rsp.cmd_stream_version = info.cfg.cmd_stream_version; - rsp.custom_dma = info.cfg.custom_dma; -#endif -} - -/**************************************************************************** - * InferenceHandler - ****************************************************************************/ - -InferenceHandler::InferenceHandler(uint8_t *tensorArena, - size_t arenaSize, - std::shared_ptr> _inferenceInputQueue, - QueueHandle_t _inferenceOutputQueue, - SemaphoreHandle_t _messageNotify, - std::shared_ptr _networks) : - inferenceInputQueue(_inferenceInputQueue), - inferenceOutputQueue(_inferenceOutputQueue), messageNotify(_messageNotify), inference(tensorArena, arenaSize), - networks(_networks) {} - -void InferenceHandler::run() { - ethosu_core_inference_req req; - ethosu_core_inference_rsp rsp; - - while (true) { - inferenceInputQueue->pop(req); - - runInference(req, rsp); - - xQueueSend(inferenceOutputQueue, &rsp, portMAX_DELAY); - xSemaphoreGive(messageNotify); - } -} - -void InferenceHandler::runInference(ethosu_core_inference_req &req, ethosu_core_inference_rsp &rsp) { - currentReq = &req; - currentRsp = &rsp; - - /* - * Run inference - */ - - InferenceProcess::InferenceJob job; - bool failed = getInferenceJob(req, job); - - if (!failed) { - job.invalidate(); - failed = inference.runJob(job); - job.clean(); - } - -#if defined(ETHOSU) - /* - * Print PMU counters - */ - - if (!failed) { - const int numEvents = std::min(static_cast(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); - - for (int i = 0; i < numEvents; i++) { - printf("ethosu_pmu_cntr%d : %" PRIu32 "\n", i, rsp.pmu_event_count[i]); - } - - if (rsp.pmu_cycle_counter_enable) { - printf("ethosu_pmu_cycle_cntr : %" PRIu64 " cycles\n", rsp.pmu_cycle_counter_count); - } - } -#endif - - /* - * Send inference response - */ - - rsp.user_arg = req.user_arg; - rsp.ofm_count = job.output.size(); - rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; - - for (size_t i = 0; i < job.output.size(); ++i) { - rsp.ofm_size[i] = job.output[i].size; - } - - currentReq = nullptr; - currentRsp = nullptr; -} - -bool InferenceHandler::getInferenceJob(const ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job) { - bool failed = networks->getNetwork(req.network, job.networkModel.data, job.networkModel.size); - if (failed) { - return true; - } - - for (uint32_t i = 0; i < req.ifm_count; ++i) { - job.input.push_back(InferenceProcess::DataPtr(reinterpret_cast(req.ifm[i].ptr), req.ifm[i].size)); - } - - for (uint32_t i = 0; i < req.ofm_count; ++i) { - job.output.push_back(InferenceProcess::DataPtr(reinterpret_cast(req.ofm[i].ptr), req.ofm[i].size)); - } - - job.externalContext = this; - - return false; -} - -} // namespace MessageHandler - -#if defined(ETHOSU) -extern "C" void ethosu_inference_begin(struct ethosu_driver *drv, void *userArg) { - MessageHandler::InferenceHandler *self = static_cast(userArg); - - // Calculate maximum number of events - const int numEvents = std::min(static_cast(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); - - // Enable PMU - ETHOSU_PMU_Enable(drv); - - // Configure and enable events - for (int i = 0; i < numEvents; i++) { - ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast(self->currentReq->pmu_event_config[i])); - ETHOSU_PMU_CNTR_Enable(drv, 1 << i); - } - - // Enable cycle counter - if (self->currentReq->pmu_cycle_counter_enable) { - ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(drv, ETHOSU_PMU_NPU_IDLE); - ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(drv, ETHOSU_PMU_NPU_ACTIVE); - - ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); - ETHOSU_PMU_CYCCNT_Reset(drv); - } - - // Reset all counters - ETHOSU_PMU_EVCNTR_ALL_Reset(drv); -} - -extern "C" void ethosu_inference_end(struct ethosu_driver *drv, void *userArg) { - MessageHandler::InferenceHandler *self = static_cast(userArg); - - // Get cycle counter - self->currentRsp->pmu_cycle_counter_enable = self->currentReq->pmu_cycle_counter_enable; - if (self->currentReq->pmu_cycle_counter_enable) { - self->currentRsp->pmu_cycle_counter_count = ETHOSU_PMU_Get_CCNTR(drv); - } - - // Calculate maximum number of events - const int numEvents = std::min(static_cast(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); - - // Get event counters - int i; - for (i = 0; i < numEvents; i++) { - self->currentRsp->pmu_event_config[i] = self->currentReq->pmu_event_config[i]; - self->currentRsp->pmu_event_count[i] = ETHOSU_PMU_Get_EVCNTR(drv, i); - } - - for (; i < ETHOSU_CORE_PMU_MAX; i++) { - self->currentRsp->pmu_event_config[i] = 0; - self->currentRsp->pmu_event_count[i] = 0; - } - - // Disable PMU - ETHOSU_PMU_Disable(drv); -} -#endif diff --git a/applications/message_handler/lib/message_queue.cpp b/applications/message_handler/lib/message_queue.cpp deleted file mode 100644 index 4001f8c..0000000 --- a/applications/message_handler/lib/message_queue.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2020-2022 Arm Limited. - * - * 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. - */ - -#include "message_queue.hpp" - -#include -#include -#include -#include - -namespace MessageQueue { - -QueueImpl::QueueImpl(EthosU::ethosu_core_queue &_queue) : queue(_queue) { - cleanHeaderData(); -} - -bool QueueImpl::empty() const { - invalidateHeaderData(); - - return queue.header.read == queue.header.write; -} - -size_t QueueImpl::available() const { - invalidateHeaderData(); - - size_t avail = queue.header.write - queue.header.read; - - if (queue.header.read > queue.header.write) { - avail += queue.header.size; - } - - return avail; -} - -size_t QueueImpl::capacity() const { - return queue.header.size - available() - 1; -} - -bool QueueImpl::read(uint8_t *dst, uint32_t length) { - const uint8_t *end = dst + length; - - // Available will invalidate the cache - if (length > available()) { - return false; - } - - uint32_t rpos = queue.header.read; - - while (dst < end) { - *dst++ = queue.data[rpos]; - rpos = (rpos + 1) % queue.header.size; - } - - queue.header.read = rpos; - - cleanHeader(); - - return true; -} - -bool QueueImpl::write(const Vec *vec, size_t length) { - size_t total = 0; - - for (size_t i = 0; i < length; i++) { - total += vec[i].length; - } - - invalidateHeader(); - - if (total > capacity()) { - return false; - } - - uint32_t wpos = queue.header.write; - - for (size_t i = 0; i < length; i++) { - const uint8_t *src = reinterpret_cast(vec[i].base); - const uint8_t *end = src + vec[i].length; - - while (src < end) { - queue.data[wpos] = *src++; - wpos = (wpos + 1) % queue.header.size; - } - } - - // Update the write position last - queue.header.write = wpos; - - cleanHeaderData(); - - return true; -} - -bool QueueImpl::write(const uint32_t type, const void *src, uint32_t length) { - EthosU::ethosu_core_msg msg = {ETHOSU_CORE_MSG_MAGIC, type, length}; - Vec vec[2] = {{&msg, sizeof(msg)}, {src, length}}; - - return write(vec, 2); -} - -// Skip to magic or end of queue -void QueueImpl::reset() { - invalidateHeader(); - queue.header.read = queue.header.write; - cleanHeader(); -} - -void QueueImpl::cleanHeader() const { -#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) - SCB_CleanDCache_by_Addr(reinterpret_cast(&queue.header), sizeof(queue.header)); -#endif -} - -void QueueImpl::cleanHeaderData() const { -#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) - SCB_CleanDCache_by_Addr(reinterpret_cast(&queue.header), sizeof(queue.header)); - uintptr_t queueDataPtr = reinterpret_cast(&queue.data[0]); - SCB_CleanDCache_by_Addr(reinterpret_cast(queueDataPtr & ~3), queue.header.size + (queueDataPtr & 3)); -#endif -} - -void QueueImpl::invalidateHeader() const { -#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) - SCB_InvalidateDCache_by_Addr(reinterpret_cast(&queue.header), sizeof(queue.header)); -#endif -} - -void QueueImpl::invalidateHeaderData() const { -#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) - SCB_InvalidateDCache_by_Addr(reinterpret_cast(&queue.header), sizeof(queue.header)); - uintptr_t queueDataPtr = reinterpret_cast(&queue.data[0]); - SCB_InvalidateDCache_by_Addr(reinterpret_cast(queueDataPtr & ~3), - queue.header.size + (queueDataPtr & 3)); -#endif -} -} // namespace MessageQueue diff --git a/applications/message_handler/main.cpp b/applications/message_handler/main.cpp deleted file mode 100644 index 4bd721e..0000000 --- a/applications/message_handler/main.cpp +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright (c) 2019-2022 Arm Limited. - * - * 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 "FreeRTOS.h" -#include "queue.h" -#include "semphr.h" -#include "task.h" - -#include -#include - -#include "ethosu_core_interface.h" -#include "indexed_networks.hpp" -#include "message_handler.hpp" -#include "message_queue.hpp" -#include "networks.hpp" - -#include -#if defined(MHU_V2) -#include -#elif defined(MHU_JUNO) -#include -#else -#include -#endif - -/* Disable semihosting */ -__asm(".global __use_no_semihosting\n\t"); - -using namespace EthosU; -using namespace MessageHandler; - -/**************************************************************************** - * Defines - ****************************************************************************/ - -// Nr. of tasks to process inferences with, reserves driver & runs inference (Normally 1 per NPU, but not a must) -#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 - -// TensorArena static initialisation -constexpr size_t arenaSize = TENSOR_ARENA_SIZE; - -__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[NUM_PARALLEL_TASKS][arenaSize]; - -// Message queue from remote host -__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue; - -// Message queue to remote host -__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue; - -namespace { - -// Mailbox driver -#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 - -} // namespace - -/**************************************************************************** - * Application - ****************************************************************************/ -namespace { - -struct TaskParams { - TaskParams() : - messageNotify(xSemaphoreCreateBinary()), - inferenceInputQueue(std::make_shared>()), - inferenceOutputQueue(xQueueCreate(5, sizeof(ethosu_core_inference_rsp))), - networks(std::make_shared()) {} - - SemaphoreHandle_t messageNotify; - // Used to pass inference requests to the inference runner task - std::shared_ptr> inferenceInputQueue; - // Queue for message responses to the remote host - QueueHandle_t inferenceOutputQueue; - // Networks provider - std::shared_ptr networks; -}; - -struct InferenceTaskParams { - TaskParams *taskParams; - uint8_t *arena; -}; - -#ifdef MHU_IRQ -void mailboxIrqHandler() { - mailbox.handleMessage(); -} -#endif - -void inferenceTask(void *pvParameters) { - printf("Starting inference task\n"); - InferenceTaskParams *params = reinterpret_cast(pvParameters); - - InferenceHandler process(params->arena, - arenaSize, - params->taskParams->inferenceInputQueue, - params->taskParams->inferenceOutputQueue, - params->taskParams->messageNotify, - params->taskParams->networks); - - process.run(); -} - -void messageTask(void *pvParameters) { - printf("Starting message task\n"); - TaskParams *params = reinterpret_cast(pvParameters); - - IncomingMessageHandler process(*inputMessageQueue.toQueue(), - *outputMessageQueue.toQueue(), - mailbox, - params->inferenceInputQueue, - params->inferenceOutputQueue, - params->messageNotify, - params->networks); - -#ifdef MHU_IRQ - // Register mailbox interrupt handler - NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler); - NVIC_EnableIRQ((IRQn_Type)MHU_IRQ); -#endif - - process.run(); -} - -/* - * Keep task parameters as global data as FreeRTOS resets the stack when the - * scheduler is started. - */ -TaskParams taskParams; -InferenceTaskParams infParams[NUM_PARALLEL_TASKS]; - -} // namespace - -// FreeRTOS application. NOTE: Additional tasks may require increased heap size. -int main() { - BaseType_t ret; - - if (!mailbox.verifyHardware()) { - printf("Failed to verify mailbox hardware\n"); - return 1; - } - - // Task for handling incoming /outgoing messages from the remote host - ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'messageTask'\n"); - return ret; - } - - // One inference task for each NPU - for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) { - infParams[n].taskParams = &taskParams; - infParams[n].arena = reinterpret_cast(&tensorArena[n]); - ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &infParams[n], 3, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'inferenceTask%d'\n", n); - return ret; - } - } - - // Start Scheduler - vTaskStartScheduler(); - - return 1; -} diff --git a/applications/message_handler/test/CMakeLists.txt b/applications/message_handler/test/CMakeLists.txt deleted file mode 100644 index 3334ff2..0000000 --- a/applications/message_handler/test/CMakeLists.txt +++ /dev/null @@ -1,62 +0,0 @@ -# -# Copyright (c) 2022 Arm Limited. -# -# 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. -# - -add_subdirectory(test_message_handler) - -set(TEST_MESSAGE_HANDLER_MODEL_0 "model.h" CACHE STRING "Path to built in model 0") -set(TEST_MESSAGE_HANDLER_MODEL_1 "" CACHE STRING "Path to built in model 1") -set(TEST_MESSAGE_HANDLER_MODEL_2 "" CACHE STRING "Path to built in model 2") -set(TEST_MESSAGE_HANDLER_MODEL_3 "" CACHE STRING "Path to built in model 3") - -function(ethosu_add_message_handler_test testname) - if(TARGET ethosu_core_driver) - file(GLOB models LIST_DIRECTORIES true "${CMAKE_CURRENT_SOURCE_DIR}/../../baremetal/models/${ETHOSU_TARGET_NPU_CONFIG}/*") - endif() - - foreach(model ${models}) - get_filename_component(modelname ${model} NAME) - ethosu_add_executable_test(mh_${testname}_${modelname} - SOURCES - ${testname}.cpp - message_client.cpp - LIBRARIES - message_handler_lib - freertos_kernel) - - get_target_property(INTERFACE_LINK_LIBRARIES ethosu_target_startup INTERFACE_LINK_LIBRARIES) - if (NOT ethosu_mhu_dummy IN_LIST INTERFACE_LINK_LIBRARIES) - target_link_libraries(mh_${testname}_${modelname} PRIVATE ethosu_mhu_dummy) - endif() - - - target_include_directories(mh_${testname}_${modelname} PRIVATE - ../indexed_networks - ${model} - ${LINUX_DRIVER_STACK_PATH}/kernel) - - target_compile_definitions(mh_${testname}_${modelname} PRIVATE - TENSOR_ARENA_SIZE=${MESSAGE_HANDLER_ARENA_SIZE} - $<$:MODEL_0=${TEST_MESSAGE_HANDLER_MODEL_0}> - $<$:MODEL_1=${TEST_MESSAGE_HANDLER_MODEL_1}> - $<$:MODEL_2=${TEST_MESSAGE_HANDLER_MODEL_2}> - $<$:MODEL_3=${TEST_MESSAGE_HANDLER_MODEL_3}>) - endforeach() -endfunction() - -ethosu_add_message_handler_test(run_inference_test) -ethosu_add_message_handler_test(cancel_reject_inference_test) diff --git a/applications/message_handler/test/cancel_reject_inference_test.cpp b/applications/message_handler/test/cancel_reject_inference_test.cpp deleted file mode 100644 index 9f4f9b4..0000000 --- a/applications/message_handler/test/cancel_reject_inference_test.cpp +++ /dev/null @@ -1,255 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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 "FreeRTOS.h" -#include "queue.h" -#include "semphr.h" -#include "task.h" - -#include -#include - -#include "ethosu_core_interface.h" -#include "indexed_networks.hpp" -#include "message_client.hpp" -#include "message_handler.hpp" -#include "message_queue.hpp" -#include "networks.hpp" -#include "test_assertions.hpp" -#include "test_helpers.hpp" - -#include -#include - -/* Disable semihosting */ -__asm(".global __use_no_semihosting\n\t"); - -using namespace EthosU; -using namespace MessageHandler; - -/**************************************************************************** - * Defines - ****************************************************************************/ - -// TensorArena static initialisation -constexpr size_t arenaSize = TENSOR_ARENA_SIZE; - -__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[arenaSize]; - -// Message queue from remote host -__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue; - -// Message queue to remote host -__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue; - -namespace { -Mailbox::MHUDummy mailbox; -} // namespace - -/**************************************************************************** - * Application - ****************************************************************************/ -namespace { - -struct TaskParams { - TaskParams() : - messageNotify(xSemaphoreCreateBinary()), - inferenceInputQueue(std::make_shared>()), - inferenceOutputQueue(xQueueCreate(5, sizeof(ethosu_core_inference_rsp))), - networks(std::make_shared()) {} - - SemaphoreHandle_t messageNotify; - // Used to pass inference requests to the inference runner task - std::shared_ptr> inferenceInputQueue; - // Queue for message responses to the remote host - QueueHandle_t inferenceOutputQueue; - // Networks provider - std::shared_ptr networks; -}; - -void messageTask(void *pvParameters) { - printf("Starting message task\n"); - TaskParams *params = reinterpret_cast(pvParameters); - - IncomingMessageHandler process(*inputMessageQueue.toQueue(), - *outputMessageQueue.toQueue(), - mailbox, - params->inferenceInputQueue, - params->inferenceOutputQueue, - params->messageNotify, - params->networks); - process.run(); -} - -void testCancelInference(MessageClient client) { - const uint64_t fake_inference_user_arg = 42; - const uint32_t network_index = 0; - ethosu_core_inference_req inference_req = - inferenceIndexedRequest(fake_inference_user_arg, network_index, nullptr, 0, nullptr, 0); - - const uint64_t fake_cancel_inference_user_arg = 55; - ethosu_core_cancel_inference_req cancel_req = {fake_cancel_inference_user_arg, fake_inference_user_arg}; - - ethosu_core_inference_rsp inference_rsp; - ethosu_core_cancel_inference_rsp cancel_rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, inference_req)); - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ, cancel_req)); - - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, inference_rsp)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP, cancel_rsp)); - - TEST_ASSERT(inference_req.user_arg == inference_rsp.user_arg); - TEST_ASSERT(inference_rsp.status == ETHOSU_CORE_STATUS_ABORTED); - - TEST_ASSERT(cancel_req.user_arg == cancel_rsp.user_arg); - TEST_ASSERT(cancel_rsp.status == ETHOSU_CORE_STATUS_OK); -} - -void testCancelNonExistentInference(MessageClient client) { - const uint64_t fake_inference_user_arg = 42; - const uint64_t fake_cancel_inference_user_arg = 55; - ethosu_core_cancel_inference_req cancel_req = {fake_cancel_inference_user_arg, fake_inference_user_arg}; - ethosu_core_cancel_inference_rsp cancel_rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ, cancel_req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP, cancel_rsp)); - - TEST_ASSERT(cancel_req.user_arg == cancel_rsp.user_arg); - TEST_ASSERT(cancel_rsp.status == ETHOSU_CORE_STATUS_ERROR); -} - -void testCannotCancelRunningInference(MessageClient client, - std::shared_ptr> inferenceInputQueue) { - const uint64_t fake_inference_user_arg = 42; - const uint32_t network_index = 0; - ethosu_core_inference_req inference_req = - inferenceIndexedRequest(fake_inference_user_arg, network_index, nullptr, 0, nullptr, 0); - - const uint64_t fake_cancel_inference_user_arg = 55; - ethosu_core_cancel_inference_req cancel_req = {fake_cancel_inference_user_arg, fake_inference_user_arg}; - ethosu_core_cancel_inference_rsp cancel_rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, inference_req)); - - // fake start of the inference by removing the inference from the queue - ethosu_core_inference_req start_req; - inferenceInputQueue->pop(start_req); - TEST_ASSERT(inference_req.user_arg == start_req.user_arg); - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ, cancel_req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP, cancel_rsp)); - - TEST_ASSERT(cancel_req.user_arg == cancel_rsp.user_arg); - TEST_ASSERT(cancel_rsp.status == ETHOSU_CORE_STATUS_ERROR); -} - -void testRejectInference(MessageClient client) { - int runs = 6; - const uint64_t fake_inference_user_arg = 42; - const uint32_t network_index = 0; - const uint64_t fake_cancel_inference_user_arg = 55; - ethosu_core_inference_req req; - ethosu_core_inference_rsp rsp; - - for (int i = 0; i < runs; i++) { - - req = inferenceIndexedRequest(fake_inference_user_arg + i, network_index, nullptr, 0, nullptr, 0); - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req)); - vTaskDelay(150); - } - - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)); - TEST_ASSERT(uint64_t(fake_inference_user_arg + runs - 1) == rsp.user_arg); - TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_REJECTED); - - // let's cleanup the queue - ethosu_core_cancel_inference_req cancel_req = {0, 0}; - ethosu_core_cancel_inference_rsp cancel_rsp; - ethosu_core_inference_rsp inference_rsp; - - for (int i = 0; i < runs - 1; i++) { - cancel_req.user_arg = fake_cancel_inference_user_arg + i; - cancel_req.inference_handle = fake_inference_user_arg + i; - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ, cancel_req)); - - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, inference_rsp)); - TEST_ASSERT(inference_rsp.user_arg = cancel_req.inference_handle); - - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP, cancel_rsp)); - TEST_ASSERT(cancel_req.user_arg == cancel_rsp.user_arg); - TEST_ASSERT(cancel_rsp.status == ETHOSU_CORE_STATUS_OK); - } -} - -void clientTask(void *pvParameters) { - printf("Starting client task\n"); - TaskParams *params = reinterpret_cast(pvParameters); - - MessageClient client(*inputMessageQueue.toQueue(), *outputMessageQueue.toQueue(), mailbox); - - vTaskDelay(50); - - testCancelInference(client); - testCancelNonExistentInference(client); - testCannotCancelRunningInference(client, params->inferenceInputQueue); - testRejectInference(client); - - exit(0); -} - -/* - * Keep task parameters as global data as FreeRTOS resets the stack when the - * scheduler is started. - */ -TaskParams taskParams; - -} // namespace - -// FreeRTOS application. NOTE: Additional tasks may require increased heap size. -int main() { - BaseType_t ret; - - if (!mailbox.verifyHardware()) { - printf("Failed to verify mailbox hardware\n"); - return 1; - } - - // Task for handling incoming /outgoing messages from the remote host - ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'messageTask'\n"); - return ret; - } - - // Task for handling incoming /outgoing messages from the remote host - ret = xTaskCreate(clientTask, "clientTask", 1024, &taskParams, 2, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'messageTask'\n"); - return ret; - } - - // Start Scheduler - vTaskStartScheduler(); - - return 1; -} diff --git a/applications/message_handler/test/message_client.cpp b/applications/message_handler/test/message_client.cpp deleted file mode 100644 index 39d1392..0000000 --- a/applications/message_handler/test/message_client.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#include "FreeRTOS.h" -#include "task.h" - -#include "ethosu_core_interface.h" -#include "message_client.hpp" - -using namespace EthosU; - -namespace MessageHandler { - -MessageClient::MessageClient(EthosU::ethosu_core_queue &_inputMessageQueue, - EthosU::ethosu_core_queue &_outputMessageQueue, - Mailbox::Mailbox &_mailbox) : - input(_inputMessageQueue), - output(_outputMessageQueue), mailbox(_mailbox) {} - -bool MessageClient::sendInputMessage(const uint32_t type, const void *src, uint32_t length) { - if (!input.write(type, src, length)) { - printf("ERROR: Msg: Failed to write message request. No mailbox message sent\n"); - return false; - } - - mailbox.sendMessage(); - mailbox.handleMessage(); - return true; -} - -bool MessageClient::waitAndReadOutputMessage(const uint32_t expected_type, uint8_t *dst, uint32_t length) { - constexpr TickType_t delay = pdMS_TO_TICKS(5); - constexpr TickType_t deadline = pdMS_TO_TICKS(/* 1 minute */ 60 * 1000 * 1000); - struct ethosu_core_msg msg; - - TickType_t totalDelay = 0; - while (output.available() == 0) { - vTaskDelay(delay); - totalDelay += delay; - if (totalDelay >= deadline) { - return false; - } - } - - if (!output.read(msg)) { - printf("ERROR: Failed to read msg header\n"); - return false; - } - - if (msg.magic != ETHOSU_CORE_MSG_MAGIC) { - printf("ERROR: Invalid Magic\n"); - return false; - } - - if (msg.type != expected_type) { - printf("ERROR: Wrong message type. Got %" PRIu32 " expected %" PRIu32 "\n", msg.type, expected_type); - return false; - } - - if (msg.length != length) { - printf("ERROR: Wrong message size\n"); - return false; - } - - if (length == 0) { - return true; - } - - if (!output.read(dst, length)) { - printf("ERROR: Failed to read msg payload\n"); - return false; - } - - return true; -} -} // namespace MessageHandler diff --git a/applications/message_handler/test/message_client.hpp b/applications/message_handler/test/message_client.hpp deleted file mode 100644 index e90843b..0000000 --- a/applications/message_handler/test/message_client.hpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#ifndef MESSAGE_CLIENT_H -#define MESSAGE_CLIENT_H - -#include -#include - -#include "message_queue.hpp" -#include - -namespace MessageHandler { - -class MessageClient { -public: - MessageClient(EthosU::ethosu_core_queue &inputMessageQueue, - EthosU::ethosu_core_queue &outputMessageQueue, - Mailbox::Mailbox &mailbox); - - template - bool sendInputMessage(const uint32_t type, const T &src) { - return sendInputMessage(type, reinterpret_cast(&src), sizeof(src)); - } - bool sendInputMessage(const uint32_t type, const void *src = nullptr, uint32_t length = 0); - template - bool waitAndReadOutputMessage(const uint32_t expected_type, T &dst) { - return waitAndReadOutputMessage(expected_type, reinterpret_cast(&dst), sizeof(dst)); - } - bool waitAndReadOutputMessage(const uint32_t expected_type, uint8_t *dst = nullptr, uint32_t length = 0); - -private: - MessageQueue::QueueImpl input; - MessageQueue::QueueImpl output; - Mailbox::Mailbox &mailbox; -}; -} // namespace MessageHandler - -#endif diff --git a/applications/message_handler/test/run_inference_test.cpp b/applications/message_handler/test/run_inference_test.cpp deleted file mode 100644 index d05224f..0000000 --- a/applications/message_handler/test/run_inference_test.cpp +++ /dev/null @@ -1,418 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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 "FreeRTOS.h" -#include "queue.h" -#include "semphr.h" -#include "task.h" - -#include -#include - -#include "ethosu_core_interface.h" -#include "indexed_networks.hpp" -#include "input.h" -#include "message_client.hpp" -#include "message_handler.hpp" -#include "message_queue.hpp" -#include "networks.hpp" -#include "output.h" -#include "test_assertions.hpp" -#include "test_helpers.hpp" - -#include -#include - -/* Disable semihosting */ -__asm(".global __use_no_semihosting\n\t"); - -using namespace EthosU; -using namespace MessageHandler; - -/**************************************************************************** - * Defines - ****************************************************************************/ - -// TensorArena static initialisation -constexpr size_t arenaSize = TENSOR_ARENA_SIZE; - -__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[arenaSize]; - -// Message queue from remote host -__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue; - -// Message queue to remote host -__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue; - -namespace { -Mailbox::MHUDummy mailbox; -} // namespace - -/**************************************************************************** - * Application - ****************************************************************************/ -namespace { - -struct TaskParams { - TaskParams() : - messageNotify(xSemaphoreCreateBinary()), - inferenceInputQueue(std::make_shared>()), - inferenceOutputQueue(xQueueCreate(5, sizeof(ethosu_core_inference_rsp))), - networks(std::make_shared()) {} - - SemaphoreHandle_t messageNotify; - // Used to pass inference requests to the inference runner task - std::shared_ptr> inferenceInputQueue; - // Queue for message responses to the remote host - QueueHandle_t inferenceOutputQueue; - // Networks provider - std::shared_ptr networks; -}; - -void inferenceTask(void *pvParameters) { - printf("Starting inference task\n"); - TaskParams *params = reinterpret_cast(pvParameters); - - InferenceHandler process(tensorArena, - arenaSize, - params->inferenceInputQueue, - params->inferenceOutputQueue, - params->messageNotify, - params->networks); - - process.run(); -} - -void messageTask(void *pvParameters) { - printf("Starting message task\n"); - TaskParams *params = reinterpret_cast(pvParameters); - - IncomingMessageHandler process(*inputMessageQueue.toQueue(), - *outputMessageQueue.toQueue(), - mailbox, - params->inferenceInputQueue, - params->inferenceOutputQueue, - params->messageNotify, - params->networks); - process.run(); -} - -void testPing(MessageClient client) { - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_PING)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_PONG)); -} - -void testVersion(MessageClient client) { - ethosu_core_msg_version ver; - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_VERSION_REQ)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_VERSION_RSP, ver)); - - TEST_ASSERT(ver.major == ETHOSU_CORE_MSG_VERSION_MAJOR); - TEST_ASSERT(ver.minor == ETHOSU_CORE_MSG_VERSION_MINOR); - TEST_ASSERT(ver.patch == ETHOSU_CORE_MSG_VERSION_PATCH); -} - -void readCapabilities(ethosu_core_msg_capabilities_rsp &rsp) { -#ifdef ETHOSU - struct ethosu_driver_version version; - ethosu_get_driver_version(&version); - - struct ethosu_hw_info info; - struct ethosu_driver *drv = ethosu_reserve_driver(); - ethosu_get_hw_info(drv, &info); - ethosu_release_driver(drv); - - rsp.version_status = info.version.version_status; - rsp.version_minor = info.version.version_minor; - rsp.version_major = info.version.version_major; - rsp.product_major = info.version.product_major; - rsp.arch_patch_rev = info.version.arch_patch_rev; - rsp.arch_minor_rev = info.version.arch_minor_rev; - rsp.arch_major_rev = info.version.arch_major_rev; - rsp.driver_patch_rev = version.patch; - rsp.driver_minor_rev = version.minor; - rsp.driver_major_rev = version.major; - rsp.macs_per_cc = info.cfg.macs_per_cc; - rsp.cmd_stream_version = info.cfg.cmd_stream_version; - rsp.custom_dma = info.cfg.custom_dma; -#endif -} - -void testCapabilities(MessageClient client) { - const uint64_t fake_user_arg = 42; - ethosu_core_capabilities_req req = {fake_user_arg}; - ethosu_core_msg_capabilities_rsp expected_rsp; - ethosu_core_msg_capabilities_rsp rsp; - - readCapabilities(expected_rsp); - expected_rsp.user_arg = req.user_arg; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_CAPABILITIES_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_CAPABILITIES_RSP, rsp)); - - TEST_ASSERT(expected_rsp.version_status == rsp.version_status); - TEST_ASSERT(expected_rsp.version_minor == rsp.version_minor); - TEST_ASSERT(expected_rsp.version_major == rsp.version_major); - TEST_ASSERT(expected_rsp.product_major == rsp.product_major); - TEST_ASSERT(expected_rsp.arch_patch_rev == rsp.arch_patch_rev); - TEST_ASSERT(expected_rsp.arch_minor_rev == rsp.arch_minor_rev); - TEST_ASSERT(expected_rsp.arch_major_rev == rsp.arch_major_rev); - TEST_ASSERT(expected_rsp.driver_patch_rev == rsp.driver_patch_rev); - TEST_ASSERT(expected_rsp.driver_minor_rev == rsp.driver_minor_rev); - TEST_ASSERT(expected_rsp.driver_major_rev == rsp.driver_major_rev); - TEST_ASSERT(expected_rsp.macs_per_cc == rsp.macs_per_cc); - TEST_ASSERT(expected_rsp.cmd_stream_version == rsp.cmd_stream_version); - TEST_ASSERT(expected_rsp.custom_dma == rsp.custom_dma); - -#ifdef ETHOSU - TEST_ASSERT(rsp.version_status > 0); - TEST_ASSERT(rsp.product_major > 0); - TEST_ASSERT(rsp.arch_major_rev > 0 || rsp.arch_minor_rev > 0 || rsp.arch_patch_rev > 0); - TEST_ASSERT(rsp.driver_major_rev > 0 || rsp.driver_minor_rev > 0 || rsp.driver_patch_rev > 0); - TEST_ASSERT(rsp.macs_per_cc > 0); -#endif -} - -void testNetworkInfoIndex(MessageClient client) { - const uint64_t fake_user_arg = 42; - const uint32_t network_index = 0; - ethosu_core_network_info_req req = networkInfoIndexedRequest(fake_user_arg, network_index); - ethosu_core_network_info_rsp rsp; - ethosu_core_network_info_rsp expected_rsp = networkInfoResponse(fake_user_arg); - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp)); - - TEST_ASSERT(expected_rsp.user_arg == rsp.user_arg); - TEST_ASSERT(std::strncmp(expected_rsp.desc, rsp.desc, sizeof(rsp.desc)) == 0); - TEST_ASSERT(expected_rsp.ifm_count == rsp.ifm_count); - TEST_ASSERT(expected_rsp.ofm_count == rsp.ofm_count); - TEST_ASSERT(expected_rsp.status == rsp.status); -} - -void testNetworkInfoNonExistantIndex(MessageClient client) { - const uint64_t fake_user_arg = 42; - const uint32_t network_index = 1; - ethosu_core_network_info_req req = networkInfoIndexedRequest(fake_user_arg, network_index); - ethosu_core_network_info_rsp rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp)); - - TEST_ASSERT(fake_user_arg == rsp.user_arg); - TEST_ASSERT(ETHOSU_CORE_STATUS_ERROR == rsp.status); -} - -void testNetworkInfoBuffer(MessageClient client) { - const uint64_t fake_user_arg = 42; - uint32_t size = sizeof(Model0::networkModelData); - unsigned char *ptr = Model0::networkModelData; - ethosu_core_network_info_req req = networkInfoBufferRequest(fake_user_arg, ptr, size); - ethosu_core_network_info_rsp rsp; - ethosu_core_network_info_rsp expected_rsp = networkInfoResponse(fake_user_arg); - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp)); - - TEST_ASSERT(expected_rsp.user_arg == rsp.user_arg); - TEST_ASSERT(std::strncmp(expected_rsp.desc, rsp.desc, sizeof(rsp.desc)) == 0); - TEST_ASSERT(expected_rsp.ifm_count == rsp.ifm_count); - TEST_ASSERT(expected_rsp.ofm_count == rsp.ofm_count); - TEST_ASSERT(expected_rsp.status == rsp.status); -} - -void testNetworkInfoUnparsableBuffer(MessageClient client) { - const uint64_t fake_user_arg = 42; - uint32_t size = sizeof(Model0::networkModelData) / 4; - unsigned char *ptr = Model0::networkModelData + size; - ethosu_core_network_info_req req = networkInfoBufferRequest(fake_user_arg, ptr, size); - ethosu_core_network_info_rsp rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp)); - - TEST_ASSERT(42 == rsp.user_arg); - TEST_ASSERT(ETHOSU_CORE_STATUS_ERROR == rsp.status); -} - -void testInferenceRunIndex(MessageClient client) { - const uint64_t fake_user_arg = 42; - const uint32_t network_index = 0; - uint8_t data[sizeof(expectedOutputData)]; - ethosu_core_inference_req req = - inferenceIndexedRequest(fake_user_arg, network_index, inputData, sizeof(inputData), data, sizeof(data)); - ethosu_core_inference_rsp rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)); - - TEST_ASSERT(req.user_arg == rsp.user_arg); - TEST_ASSERT(rsp.ofm_count == 1); - TEST_ASSERT(std::memcmp(expectedOutputData, data, sizeof(expectedOutputData)) == 0); - TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_OK); - TEST_ASSERT(rsp.pmu_cycle_counter_enable == req.pmu_cycle_counter_enable); - TEST_ASSERT(std::memcmp(rsp.pmu_event_config, req.pmu_event_config, sizeof(req.pmu_event_config)) == 0); -} - -void testInferenceRunNonExistingIndex(MessageClient client) { - const uint64_t fake_user_arg = 42; - const uint32_t network_index = 1; - uint8_t data[sizeof(expectedOutputData)]; - ethosu_core_inference_req req = - inferenceIndexedRequest(fake_user_arg, network_index, inputData, sizeof(inputData), data, sizeof(data)); - ethosu_core_inference_rsp rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)); - - TEST_ASSERT(req.user_arg == rsp.user_arg); - TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_ERROR); -} - -void testInferenceRunBuffer(MessageClient client) { - const uint64_t fake_user_arg = 42; - uint32_t network_size = sizeof(Model0::networkModelData); - unsigned char *network_ptr = Model0::networkModelData; - uint8_t data[sizeof(expectedOutputData)]; - ethosu_core_inference_req req = inferenceBufferRequest( - fake_user_arg, network_ptr, network_size, inputData, sizeof(inputData), data, sizeof(data)); - ethosu_core_inference_rsp rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)); - - TEST_ASSERT(req.user_arg == rsp.user_arg); - TEST_ASSERT(rsp.ofm_count == 1); - TEST_ASSERT(std::memcmp(expectedOutputData, data, sizeof(expectedOutputData)) == 0); - TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_OK); - TEST_ASSERT(rsp.pmu_cycle_counter_enable == req.pmu_cycle_counter_enable); - TEST_ASSERT(std::memcmp(rsp.pmu_event_config, req.pmu_event_config, sizeof(req.pmu_event_config)) == 0); -} - -void testInferenceRunUnparsableBuffer(MessageClient client) { - const uint64_t fake_user_arg = 42; - uint32_t network_size = sizeof(Model0::networkModelData) / 4; - unsigned char *network_ptr = Model0::networkModelData + network_size; - uint8_t data[sizeof(expectedOutputData)]; - ethosu_core_inference_req req = inferenceBufferRequest( - fake_user_arg, network_ptr, network_size, inputData, sizeof(inputData), data, sizeof(data)); - ethosu_core_inference_rsp rsp; - - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req)); - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)); - - TEST_ASSERT(req.user_arg == rsp.user_arg); - TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_ERROR); -} - -void testSequentiallyQueuedInferenceRuns(MessageClient client) { - int runs = 5; - uint8_t data[runs][sizeof(expectedOutputData)]; - const uint64_t fake_user_arg = 42; - const uint32_t network_index = 0; - ethosu_core_inference_req req; - ethosu_core_inference_rsp rsp[runs]; - - for (int i = 0; i < runs; i++) { - vTaskDelay(150); - - req = inferenceIndexedRequest( - fake_user_arg + i, network_index, inputData, sizeof(inputData), data[i], sizeof(data[i])); - TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req)); - } - - for (int i = 0; i < runs; i++) { - TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp[i])); - TEST_ASSERT(uint64_t(fake_user_arg + i) == rsp[i].user_arg); - TEST_ASSERT(rsp[i].ofm_count == 1); - TEST_ASSERT(std::memcmp(expectedOutputData, data[i], sizeof(expectedOutputData)) == 0); - TEST_ASSERT(rsp[i].status == ETHOSU_CORE_STATUS_OK); - TEST_ASSERT(rsp[i].pmu_cycle_counter_enable == req.pmu_cycle_counter_enable); - TEST_ASSERT(std::memcmp(rsp[i].pmu_event_config, req.pmu_event_config, sizeof(req.pmu_event_config)) == 0); - } -} - -void clientTask(void *) { - printf("Starting client task\n"); - - MessageClient client(*inputMessageQueue.toQueue(), *outputMessageQueue.toQueue(), mailbox); - - vTaskDelay(50); - - testPing(client); - testVersion(client); - testCapabilities(client); - testNetworkInfoIndex(client); - testNetworkInfoNonExistantIndex(client); - testNetworkInfoBuffer(client); - testNetworkInfoUnparsableBuffer(client); - testInferenceRunIndex(client); - testInferenceRunNonExistingIndex(client); - testInferenceRunBuffer(client); - testInferenceRunUnparsableBuffer(client); - testSequentiallyQueuedInferenceRuns(client); - - exit(0); -} - -/* - * Keep task parameters as global data as FreeRTOS resets the stack when the - * scheduler is started. - */ -TaskParams taskParams; - -} // namespace - -// FreeRTOS application. NOTE: Additional tasks may require increased heap size. -int main() { - BaseType_t ret; - - if (!mailbox.verifyHardware()) { - printf("Failed to verify mailbox hardware\n"); - return 1; - } - - // Task for handling incoming /outgoing messages from the remote host - ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'messageTask'\n"); - return ret; - } - - ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &taskParams, 3, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'inferenceTask'\n"); - return ret; - } - - // Task for handling incoming /outgoing messages from the remote host - ret = xTaskCreate(clientTask, "clientTask", 1024, nullptr, 2, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'messageTask'\n"); - return ret; - } - - // Start Scheduler - vTaskStartScheduler(); - - return 1; -} diff --git a/applications/message_handler/test/test_assertions.hpp b/applications/message_handler/test/test_assertions.hpp deleted file mode 100644 index 7c4cb5c..0000000 --- a/applications/message_handler/test/test_assertions.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#ifndef TEST_ASSERTIONS_H -#define TEST_ASSERTIONS_H - -#include -#include - -#define TEST_ASSERT(v) \ - do { \ - if (!(v)) { \ - fprintf(stderr, "%s:%d ERROR test failed: '%s'\n", __FILE__, __LINE__, #v); \ - exit(1); \ - } \ - } while (0) - -#endif diff --git a/applications/message_handler/test/test_helpers.hpp b/applications/message_handler/test/test_helpers.hpp deleted file mode 100644 index 0440b58..0000000 --- a/applications/message_handler/test/test_helpers.hpp +++ /dev/null @@ -1,131 +0,0 @@ - -/* - * Copyright (c) 2022 Arm Limited. - * - * 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. - */ - -#ifndef TEST_HELPERS_H -#define TEST_HELPERS_H - -#include -#include - -#include "ethosu_core_interface.h" - -namespace MessageHandler { - -ethosu_core_network_info_req networkInfoIndexedRequest(uint64_t user_arg, uint32_t index) { - ethosu_core_network_info_req req = {user_arg, // user_arg - { // network - ETHOSU_CORE_NETWORK_INDEX, // type - {{ - index, // index - 0 // ignored padding of union - }}}}; - return req; -} - -ethosu_core_network_info_req networkInfoBufferRequest(uint64_t user_arg, unsigned char *ptr, uint32_t ptr_size) { - ethosu_core_network_info_req req = {user_arg, // user_arg - { // network - ETHOSU_CORE_NETWORK_BUFFER, // type - {{ - reinterpret_cast(ptr), // ptr - ptr_size // size - }}}}; - return req; -} - -ethosu_core_network_info_rsp networkInfoResponse(uint64_t user_arg) { - ethosu_core_network_info_rsp rsp = { - user_arg, // user_arg - "Vela Optimised", // description - 1, // ifm_count - {/* not comparable */}, // ifm_sizes - 1, // ofm_count - {/* not comparable */}, // ofm_sizes - ETHOSU_CORE_STATUS_OK // status - }; - return rsp; -} - -ethosu_core_inference_req inferenceIndexedRequest(uint64_t user_arg, - uint32_t index, - unsigned char *input_data, - uint32_t input_data_size, - uint8_t *output_data, - uint32_t output_data_size) { - ethosu_core_inference_req req = { - user_arg, // user_arg - 1, // ifm_count - { // ifm - { - reinterpret_cast(input_data), // ptr - input_data_size // size - }}, - 1, // ofm_count - { // ofm - { - reinterpret_cast(output_data), // ptr - output_data_size // size - }}, - { // network - ETHOSU_CORE_NETWORK_INDEX, // type - {{ - index, // index - 0 // ignored padding of union - }}}, - {0, 0, 0, 0, 0, 0, 0, 0}, // pmu_event_config - 0 // pmu_cycle_counter_enable - }; - return req; -} - -ethosu_core_inference_req inferenceBufferRequest(uint64_t user_arg, - unsigned char *ptr, - uint32_t ptr_size, - unsigned char *input_data, - uint32_t input_data_size, - uint8_t *output_data, - uint32_t output_data_size) { - ethosu_core_inference_req req = { - user_arg, // user_arg - 1, // ifm_count - { // ifm - { - reinterpret_cast(input_data), // ptr - input_data_size // size - }}, - 1, // ofm_count - { // ofm - { - reinterpret_cast(output_data), // ptr - output_data_size // size - }}, - { // network - ETHOSU_CORE_NETWORK_BUFFER, // type - {{ - reinterpret_cast(ptr), // ptr - ptr_size // size - }}}, - {0, 0, 0, 0, 0, 0, 0, 0}, // pmu_event_config - 0 // pmu_cycle_counter_enable - }; - return req; -} -} // namespace MessageHandler - -#endif diff --git a/applications/message_handler/test/test_message_handler/CMakeLists.txt b/applications/message_handler/test/test_message_handler/CMakeLists.txt deleted file mode 100644 index bdccf54..0000000 --- a/applications/message_handler/test/test_message_handler/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -# -# Copyright (c) 2020-2022 Arm Limited. -# -# 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. -# - -if(NOT BUILD_TEST_MESSAGE_HANDLER OR NOT TARGET freertos_kernel) - message("Skipping test message handler") - return() -endif() - -ethosu_add_executable(test_message_handler - SOURCES - main.cpp - LIBRARIES - message_handler_lib - freertos_kernel) - -target_include_directories(test_message_handler PRIVATE - ../../indexed_networks - ${LINUX_DRIVER_STACK_PATH}/kernel) - -install(FILES $ - DESTINATION "lib/firmware" - RENAME "arm-test-${ETHOSU_TARGET_NPU_CONFIG}.fw" -) diff --git a/applications/message_handler/test/test_message_handler/main.cpp b/applications/message_handler/test/test_message_handler/main.cpp deleted file mode 100644 index 0a83a56..0000000 --- a/applications/message_handler/test/test_message_handler/main.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2019-2022 Arm Limited. - * - * 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 "FreeRTOS.h" -#include "queue.h" -#include "semphr.h" -#include "task.h" - -#include -#include - -#include "ethosu_core_interface.h" -#include "indexed_networks.hpp" -#include "message_handler.hpp" -#include "message_queue.hpp" -#include "networks.hpp" - -#include -#if defined(MHU_V2) -#include -#elif defined(MHU_JUNO) -#include -#else -#include -#endif - -/* Disable semihosting */ -__asm(".global __use_no_semihosting\n\t"); - -using namespace EthosU; -using namespace MessageHandler; - -/**************************************************************************** - * Defines - ****************************************************************************/ - -// Message queue from remote host -__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue; - -// Message queue to remote host -__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue; - -namespace { - -// Mailbox driver -#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 - -} // namespace - -/**************************************************************************** - * Application - ****************************************************************************/ -namespace { - -struct TaskParams { - TaskParams() : - messageNotify(xSemaphoreCreateBinary()), - inferenceInputQueue(std::make_shared>()), - inferenceOutputQueue(xQueueCreate(10, sizeof(ethosu_core_inference_rsp))), - networks(std::make_shared()) {} - - SemaphoreHandle_t messageNotify; - // Used to pass inference requests to the inference runner task - std::shared_ptr> inferenceInputQueue; - // Queue for message responses to the remote host - QueueHandle_t inferenceOutputQueue; - // Networks provider - std::shared_ptr networks; -}; - -#ifdef MHU_IRQ -void mailboxIrqHandler() { - mailbox.handleMessage(); -} -#endif - -void messageTask(void *pvParameters) { - printf("Starting message task\n"); - TaskParams *params = reinterpret_cast(pvParameters); - - IncomingMessageHandler process(*inputMessageQueue.toQueue(), - *outputMessageQueue.toQueue(), - mailbox, - params->inferenceInputQueue, - params->inferenceOutputQueue, - params->messageNotify, - params->networks); - -#ifdef MHU_IRQ - // Register mailbox interrupt handler - NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler); - NVIC_EnableIRQ((IRQn_Type)MHU_IRQ); -#endif - - process.run(); -} - -/* - * Keep task parameters as global data as FreeRTOS resets the stack when the - * scheduler is started. - */ -TaskParams taskParams; - -} // namespace - -// FreeRTOS application. NOTE: Additional tasks may require increased heap size. -int main() { - BaseType_t ret; - - if (!mailbox.verifyHardware()) { - printf("Failed to verify mailbox hardware\n"); - return 1; - } - - // Task for handling incoming /outgoing messages from the remote host - ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr); - if (ret != pdPASS) { - printf("Failed to create 'messageTask'\n"); - return ret; - } - - // Start Scheduler - vTaskStartScheduler(); - - return 1; -} diff --git a/applications/message_handler_openamp/CMakeLists.txt b/applications/message_handler_openamp/CMakeLists.txt new file mode 100644 index 0000000..f2624c3 --- /dev/null +++ b/applications/message_handler_openamp/CMakeLists.txt @@ -0,0 +1,76 @@ +# +# SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates +# +# 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. +# + +if(NOT TARGET freertos_kernel) + message("Skipping message handler openamp") + return() +endif() + +############################################################################# +# Configuration +############################################################################# + +set(MESSAGE_HANDLER_MODEL_0 "" CACHE STRING "Path to built in model 0") +set(MESSAGE_HANDLER_MODEL_1 "" CACHE STRING "Path to built in model 1") +set(MESSAGE_HANDLER_MODEL_2 "" CACHE STRING "Path to built in model 2") +set(MESSAGE_HANDLER_MODEL_3 "" CACHE STRING "Path to built in model 3") + +set(MESSAGE_HANDLER_ARENA_SIZE 2000000 CACHE STRING "Total size of all message handler tensor arenas") + +############################################################################# +# TFLM arena +############################################################################# + +# Split total tensor arena equally for each NPU +if(TARGET ethosu_core_driver AND ETHOSU_TARGET_NPU_COUNT GREATER 0) + set(NUM_ARENAS ${ETHOSU_TARGET_NPU_COUNT}) +else() + set(NUM_ARENAS 1) +endif() + +math(EXPR TENSOR_ARENA_SIZE "${MESSAGE_HANDLER_ARENA_SIZE} / ${NUM_ARENAS}") + +############################################################################# +# Message handler application +############################################################################# + +ethosu_add_executable(message_handler_openamp + SOURCES + main.cpp + core_driver_mutex.cpp + freertos_allocator.cpp + inference_runner.cpp + message_handler.cpp + remoteproc.cpp + LIBRARIES + $<$:ethosu_core_driver> + ethosu_log + ethosu_mailbox + freertos_kernel + inference_process + openamp-freertos) + +target_include_directories(message_handler_openamp PRIVATE + ${LINUX_DRIVER_STACK_PATH}/kernel) + +target_compile_definitions(message_handler_openamp PRIVATE + TENSOR_ARENA_SIZE=${TENSOR_ARENA_SIZE} + $<$:MODEL_0=${MESSAGE_HANDLER_MODEL_0}> + $<$:MODEL_1=${MESSAGE_HANDLER_MODEL_1}> + $<$:MODEL_2=${MESSAGE_HANDLER_MODEL_2}> + $<$:MODEL_3=${MESSAGE_HANDLER_MODEL_3}>) diff --git a/applications/message_handler_openamp/core_driver_mutex.cpp b/applications/message_handler_openamp/core_driver_mutex.cpp new file mode 100644 index 0000000..d023ad2 --- /dev/null +++ b/applications/message_handler_openamp/core_driver_mutex.cpp @@ -0,0 +1,81 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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. + */ + +#if defined(ETHOSU) + +#include "FreeRTOS.h" +#include "semphr.h" + +#include +#include + +extern "C" { + +void *ethosu_mutex_create(void) { + return xSemaphoreCreateMutex(); +} + +int ethosu_mutex_lock(void *mutex) { + SemaphoreHandle_t handle = reinterpret_cast(mutex); + if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { + printf("Error: Failed to lock mutex.\n"); + return -1; + } + return 0; +} + +int ethosu_mutex_unlock(void *mutex) { + SemaphoreHandle_t handle = reinterpret_cast(mutex); + if (xSemaphoreGive(handle) != pdTRUE) { + printf("Error: Failed to unlock mutex.\n"); + return -1; + } + return 0; +} + +void *ethosu_semaphore_create(void) { + return xSemaphoreCreateCounting(255, 0); +} + +int ethosu_semaphore_take(void *sem) { + SemaphoreHandle_t handle = reinterpret_cast(sem); + if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { + printf("Error: Failed to take semaphore.\n"); + return -1; + } + return 0; +} + +int ethosu_semaphore_give(void *sem) { + SemaphoreHandle_t handle = reinterpret_cast(sem); + if (xPortIsInsideInterrupt()) { + if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) { + printf("Error: Failed to give semaphore from ISR.\n"); + return -1; + } + } else { + if (xSemaphoreGive(handle) != pdTRUE) { + printf("Error: Failed to give semaphore.\n"); + return -1; + } + } + return 0; +} +} + +#endif diff --git a/applications/message_handler_openamp/freertos_allocator.cpp b/applications/message_handler_openamp/freertos_allocator.cpp new file mode 100644 index 0000000..9850928 --- /dev/null +++ b/applications/message_handler_openamp/freertos_allocator.cpp @@ -0,0 +1,45 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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. + */ + +#include "FreeRTOS.h" +#include +#include + +void *operator new(size_t size) { + return pvPortMalloc(size); +} + +void *operator new[](size_t size) { + return pvPortMalloc(size); +} + +void operator delete(void *ptr) noexcept { + vPortFree(ptr); +} + +void operator delete(void *ptr, std::size_t) noexcept { + vPortFree(ptr); +} + +void operator delete[](void *ptr) noexcept { + vPortFree(ptr); +} + +void operator delete[](void *ptr, std::size_t) noexcept { + vPortFree(ptr); +} diff --git a/applications/message_handler_openamp/inference_runner.cpp b/applications/message_handler_openamp/inference_runner.cpp new file mode 100644 index 0000000..2f9d8ec --- /dev/null +++ b/applications/message_handler_openamp/inference_runner.cpp @@ -0,0 +1,194 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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 "inference_runner.hpp" + +#include + +#include + +#if defined(ETHOSU) +#include +#include +#endif + +/***************************************************************************** + * InferenceRunner + *****************************************************************************/ + +InferenceRunner::InferenceRunner(uint8_t *tensorArena, + size_t arenaSize, + MessageHandler::InferenceQueue &_inferenceQueue, + MessageHandler::ResponseQueue &_responseQueue) : + inferenceQueue(_inferenceQueue), + responseQueue(_responseQueue), inference(tensorArena, arenaSize) { + BaseType_t ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, this, 4, &taskHandle); + if (ret != pdPASS) { + LOG_ERR("Failed to create inference task"); + abort(); + } +} + +InferenceRunner::~InferenceRunner() { + vTaskDelete(taskHandle); +} + +void InferenceRunner::inferenceTask(void *param) { + auto _this = static_cast(param); + + LOG_DEBUG("Starting inference task"); + + while (true) { + Message *message; + auto ret = _this->inferenceQueue.receive(message); + if (ret) { + abort(); + } + + auto &rpmsg = message->rpmsg; + + switch (rpmsg.header.type) { + case EthosU::ETHOSU_CORE_MSG_INFERENCE_REQ: { + _this->handleInferenceRequest(message->src, rpmsg.header.msg_id, rpmsg.inf_req); + break; + } + default: { + LOG_WARN("Unsupported message for inference runner. type=%lu", rpmsg.header.type); + } + } + + delete message; + } +} + +void InferenceRunner::handleInferenceRequest(const uint32_t src, + const uint64_t msgId, + const EthosU::ethosu_core_msg_inference_req &request) { + auto message = + new Message(src, EthosU::ETHOSU_CORE_MSG_INFERENCE_RSP, msgId, sizeof(EthosU::ethosu_core_msg_inference_rsp)); + auto &response = message->rpmsg.inf_rsp; + + // Setup PMU configuration + response.pmu_cycle_counter_enable = request.pmu_cycle_counter_enable; + + for (int i = 0; i < ETHOSU_CORE_PMU_MAX; i++) { + response.pmu_event_config[i] = request.pmu_event_config[i]; + } + + // Run inference + auto job = makeInferenceJob(request, response); + auto failed = inference.runJob(job); + + // Send response rpmsg + response.ofm_count = job.output.size(); + response.status = failed ? EthosU::ETHOSU_CORE_STATUS_ERROR : EthosU::ETHOSU_CORE_STATUS_OK; + + for (size_t i = 0; i < job.output.size(); ++i) { + response.ofm_size[i] = job.output[i].size; + } + + responseQueue.send(message); +} + +InferenceProcess::InferenceJob InferenceRunner::makeInferenceJob(const EthosU::ethosu_core_msg_inference_req &request, + EthosU::ethosu_core_msg_inference_rsp &response) { + InferenceProcess::InferenceJob job; + + job.networkModel = + InferenceProcess::DataPtr(reinterpret_cast(request.network.buffer.ptr), request.network.buffer.size); + + for (uint32_t i = 0; i < request.ifm_count; ++i) { + job.input.push_back( + InferenceProcess::DataPtr(reinterpret_cast(request.ifm[i].ptr), request.ifm[i].size)); + } + + for (uint32_t i = 0; i < request.ofm_count; ++i) { + job.output.push_back( + InferenceProcess::DataPtr(reinterpret_cast(request.ofm[i].ptr), request.ofm[i].size)); + } + + job.externalContext = &response; + + return job; +} + +#if defined(ETHOSU) +extern "C" { + +void ethosu_inference_begin(ethosu_driver *drv, void *userArg) { + LOG_DEBUG(""); + + auto response = static_cast(userArg); + + // Calculate maximum number of events + const int numEvents = std::min(static_cast(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); + + // Enable PMU + ETHOSU_PMU_Enable(drv); + + // Configure and enable events + for (int i = 0; i < numEvents; i++) { + ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast(response->pmu_event_config[i])); + ETHOSU_PMU_CNTR_Enable(drv, 1 << i); + } + + // Enable cycle counter + if (response->pmu_cycle_counter_enable) { + ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(drv, ETHOSU_PMU_NPU_IDLE); + ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(drv, ETHOSU_PMU_NPU_ACTIVE); + + ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); + ETHOSU_PMU_CYCCNT_Reset(drv); + } + + // Reset all counters + ETHOSU_PMU_EVCNTR_ALL_Reset(drv); +} + +void ethosu_inference_end(ethosu_driver *drv, void *userArg) { + auto response = static_cast(userArg); + + // Get cycle counter + if (response->pmu_cycle_counter_enable) { + response->pmu_cycle_counter_count = ETHOSU_PMU_Get_CCNTR(drv); + } + + // Calculate maximum number of events + const int numEvents = std::min(static_cast(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); + + // Get event counters + int i; + for (i = 0; i < numEvents; i++) { + response->pmu_event_count[i] = ETHOSU_PMU_Get_EVCNTR(drv, i); + } + + for (; i < ETHOSU_CORE_PMU_MAX; i++) { + response->pmu_event_config[i] = 0; + response->pmu_event_count[i] = 0; + } + + // Disable PMU + ETHOSU_PMU_Disable(drv); +} +} + +#endif diff --git a/applications/message_handler_openamp/inference_runner.hpp b/applications/message_handler_openamp/inference_runner.hpp new file mode 100644 index 0000000..c9461a0 --- /dev/null +++ b/applications/message_handler_openamp/inference_runner.hpp @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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. + */ + +#pragma once + +/***************************************************************************** + * Includes + *****************************************************************************/ + +#include "message_handler.hpp" + +#include + +/***************************************************************************** + * InferenceRunner + *****************************************************************************/ + +class InferenceRunner { +public: + InferenceRunner(uint8_t *tensorArena, + size_t arenaSize, + MessageHandler::InferenceQueue &inferenceQueue, + MessageHandler::ResponseQueue &responseQueue); + ~InferenceRunner(); + +private: + static void inferenceTask(void *param); + + void handleInferenceRequest(const uint32_t src, + const uint64_t msgId, + const EthosU::ethosu_core_msg_inference_req &request); + InferenceProcess::InferenceJob makeInferenceJob(const EthosU::ethosu_core_msg_inference_req &request, + EthosU::ethosu_core_msg_inference_rsp &response); + + MessageHandler::InferenceQueue &inferenceQueue; + MessageHandler::ResponseQueue &responseQueue; + InferenceProcess::InferenceProcess inference; + + // FreeRTOS + TaskHandle_t taskHandle; +}; diff --git a/applications/message_handler_openamp/main.cpp b/applications/message_handler_openamp/main.cpp new file mode 100644 index 0000000..2aad224 --- /dev/null +++ b/applications/message_handler_openamp/main.cpp @@ -0,0 +1,122 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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 +#include + +#include +#include + +#if defined(MHU_V2) +#include +#elif defined(MHU_JUNO) +#include +#else +#include +#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 + *****************************************************************************/ + +extern "C" { +__attribute__((section(".resource_table"))) ResourceTable resourceTable(8, sizeof(arenaSize *NUM_PARALLEL_TASKS)); +} + +/***************************************************************************** + * 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(); +} +#endif + +} // namespace + +/***************************************************************************** + * main + *****************************************************************************/ + +int main() { + printf("Ethos-U Message Handler OpenAMP\n"); + + auto mem = std::make_shared(); + auto rproc = std::make_shared(mailbox, resourceTable.table, sizeof(resourceTable), *mem); + auto messageHandler = std::make_shared(*rproc, "ethos-u-0.0"); + + std::array, NUM_PARALLEL_TASKS> inferenceRunner; + + for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) { + auto tensorArena = static_cast(messageHandler->physicalToVirtual(resourceTable.carveout.pa)); + + inferenceRunner[i] = std::make_shared(&tensorArena[arenaSize * i], + arenaSize, + messageHandler->getInferenceQueue(), + messageHandler->getResponseQueue()); + } + +#ifdef MHU_IRQ + // Register mailbox interrupt handler + NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler); + NVIC_EnableIRQ((IRQn_Type)MHU_IRQ); +#endif + + // Start Scheduler + vTaskStartScheduler(); + + return 0; +} diff --git a/applications/message_handler_openamp/message_handler.cpp b/applications/message_handler_openamp/message_handler.cpp new file mode 100644 index 0000000..4bd611d --- /dev/null +++ b/applications/message_handler_openamp/message_handler.cpp @@ -0,0 +1,437 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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 "message_handler.hpp" + +#include +#include + +#include +#include + +#ifdef ETHOSU +#include +#endif + +/***************************************************************************** + * Networks + *****************************************************************************/ + +namespace { +#if defined(__has_include) + +#if defined(MODEL_0) +namespace Model0 { +#include STRINGIFY(MODEL_0) +} +#endif + +#if defined(MODEL_1) +namespace Model1 { +#include STRINGIFY(MODEL_1) +} +#endif + +#if defined(MODEL_2) +namespace Model2 { +#include STRINGIFY(MODEL_2) +} +#endif + +#if defined(MODEL_3) +namespace Model3 { +#include STRINGIFY(MODEL_3) +} +#endif + +#endif + +bool getIndexedNetwork(const uint32_t index, void *&data, size_t &size) { + switch (index) { +#if defined(MODEL_0) + case 0: + data = reinterpret_cast(Model0::networkModelData); + size = sizeof(Model0::networkModelData); + break; +#endif + +#if defined(MODEL_1) + case 1: + data = reinterpret_cast(Model1::networkModelData); + size = sizeof(Model1::networkModelData); + break; +#endif + +#if defined(MODEL_2) + case 2: + data = reinterpret_cast(Model2::networkModelData); + size = sizeof(Model2::networkModelData); + break; +#endif + +#if defined(MODEL_3) + case 3: + data = reinterpret_cast(Model3::networkModelData); + size = sizeof(Model3::networkModelData); + break; +#endif + + default: + LOG_WARN("Network model index out of range. index=%" PRIu32, index); + return true; + } + + return false; +} + +} // namespace + +/***************************************************************************** + * MessageHandler + *****************************************************************************/ + +MessageHandler::MessageHandler(RProc &_rproc, const char *const _name) : + Rpmsg(_rproc, _name), capabilities(getCapabilities()) { + BaseType_t ret = xTaskCreate(responseTask, "responseTask", 1024, this, 3, &taskHandle); + if (ret != pdPASS) { + LOG_ERR("Failed to create response task"); + abort(); + } +} + +MessageHandler::~MessageHandler() { + vTaskDelete(taskHandle); +} + +int MessageHandler::handleMessage(void *data, size_t len, uint32_t src) { + auto rpmsg = static_cast(data); + + LOG_DEBUG("Msg: src=%" PRIX32 ", len=%zu, magic=%" PRIX32 ", type=%" PRIu32, + src, + len, + rpmsg->header.magic, + rpmsg->header.type); + + if (rpmsg->header.magic != ETHOSU_CORE_MSG_MAGIC) { + LOG_WARN("Msg: Invalid Magic"); + sendError(src, EthosU::ETHOSU_CORE_MSG_ERR_INVALID_MAGIC, "Invalid magic"); + return 0; + } + + switch (rpmsg->header.type) { + case EthosU::ETHOSU_CORE_MSG_PING: { + LOG_INFO("Msg: Ping"); + sendPong(src, rpmsg->header.msg_id); + break; + } + case EthosU::ETHOSU_CORE_MSG_VERSION_REQ: { + LOG_INFO("Msg: Version request"); + sendVersionRsp(src, rpmsg->header.msg_id); + break; + } + case EthosU::ETHOSU_CORE_MSG_CAPABILITIES_REQ: { + if (len != sizeof(rpmsg->header)) { + sendError( + src, EthosU::ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "Incorrect capabilities request payload length."); + break; + } + + LOG_INFO("Msg: Capabilities request"); + + sendCapabilitiesRsp(src, rpmsg->header.msg_id); + break; + } + case EthosU::ETHOSU_CORE_MSG_INFERENCE_REQ: { + if (len != sizeof(rpmsg->header) + sizeof(rpmsg->inf_req)) { + sendError(src, EthosU::ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "Incorrect inference request payload length."); + break; + } + + forwardInferenceReq(src, rpmsg->header.msg_id, rpmsg->inf_req); + break; + } + case EthosU::ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ: { + if (len != sizeof(rpmsg->header) + sizeof(rpmsg->cancel_req)) { + sendError( + src, EthosU::ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "Incorrect cancel inference request payload length."); + break; + } + + auto &request = rpmsg->cancel_req; + bool found = false; + inferenceQueue.erase([request, &found](auto &message) { + if (message->rpmsg.header.msg_id == request.inference_handle) { + found = true; + delete message; + return true; + } + + return false; + }); + + if (found) { + sendInferenceRsp(src, request.inference_handle, EthosU::ETHOSU_CORE_STATUS_ABORTED); + } + + sendCancelInferenceRsp( + src, rpmsg->header.msg_id, found ? EthosU::ETHOSU_CORE_STATUS_OK : EthosU::ETHOSU_CORE_STATUS_ERROR); + break; + } + case EthosU::ETHOSU_CORE_MSG_NETWORK_INFO_REQ: { + if (len != sizeof(rpmsg->header) + sizeof(rpmsg->net_info_req)) { + sendError( + src, EthosU::ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "Incorrect network info request payload length."); + break; + } + + LOG_INFO("Msg: NetworkInfoReq. network={ type=%" PRIu32 ", index=%" PRIu32 ", buffer={ ptr=0x%" PRIX32 + ", size=%" PRIu32 " } }", + rpmsg->net_info_req.network.type, + rpmsg->net_info_req.network.index, + rpmsg->net_info_req.network.buffer.ptr, + rpmsg->net_info_req.network.buffer.size); + + sendNetworkInfoRsp(src, rpmsg->header.msg_id, rpmsg->net_info_req.network); + break; + } + default: { + LOG_WARN("Msg: Unsupported message. type=%" PRIu32, rpmsg->header.type); + + char errMsg[128]; + snprintf( + &errMsg[0], sizeof(errMsg), "Msg: Unknown message. type=%" PRIu32 ", length=%zu", rpmsg->header.type, len); + + sendError(src, EthosU::ETHOSU_CORE_MSG_ERR_UNSUPPORTED_TYPE, errMsg); + } + } + + return 0; +} + +void MessageHandler::sendError(const uint32_t src, const EthosU::ethosu_core_err_type type, const char *msg) { + auto message = new Message(src, EthosU::ETHOSU_CORE_MSG_ERR, 0, sizeof(EthosU::ethosu_core_msg_err)); + + message->rpmsg.error.type = type; + + for (size_t i = 0; i < sizeof(message->rpmsg.error.msg) && msg[i]; i++) { + message->rpmsg.error.msg[i] = msg[i]; + } + + responseQueue.send(message); +} + +void MessageHandler::sendPong(const uint32_t src, const uint64_t msgId) { + auto message = new Message(src, EthosU::ETHOSU_CORE_MSG_PONG, msgId); + + responseQueue.send(message); +} + +void MessageHandler::sendVersionRsp(const uint32_t src, const uint64_t msgId) { + auto message = + new Message(src, EthosU::ETHOSU_CORE_MSG_VERSION_RSP, msgId, sizeof(EthosU::ethosu_core_msg_version_rsp)); + + message->rpmsg.version_rsp = { + ETHOSU_CORE_MSG_VERSION_MAJOR, + ETHOSU_CORE_MSG_VERSION_MINOR, + ETHOSU_CORE_MSG_VERSION_PATCH, + 0, + }; + + responseQueue.send(message); +} + +void MessageHandler::sendCapabilitiesRsp(const uint32_t src, const uint64_t msgId) { + auto message = new Message( + src, EthosU::ETHOSU_CORE_MSG_CAPABILITIES_RSP, msgId, sizeof(EthosU::ethosu_core_msg_capabilities_rsp)); + + message->rpmsg.cap_rsp = capabilities; + + responseQueue.send(message); +} + +EthosU::ethosu_core_msg_capabilities_rsp MessageHandler::getCapabilities() const { + EthosU::ethosu_core_msg_capabilities_rsp cap = {}; + +#ifdef ETHOSU + ethosu_driver_version version; + ethosu_get_driver_version(&version); + + ethosu_hw_info info; + ethosu_driver *drv = ethosu_reserve_driver(); + ethosu_get_hw_info(drv, &info); + ethosu_release_driver(drv); + + cap.version_status = info.version.version_status; + cap.version_minor = info.version.version_minor; + cap.version_major = info.version.version_major; + cap.product_major = info.version.product_major; + cap.arch_patch_rev = info.version.arch_patch_rev; + cap.arch_minor_rev = info.version.arch_minor_rev; + cap.arch_major_rev = info.version.arch_major_rev; + cap.driver_patch_rev = version.patch; + cap.driver_minor_rev = version.minor; + cap.driver_major_rev = version.major; + cap.macs_per_cc = info.cfg.macs_per_cc; + cap.cmd_stream_version = info.cfg.cmd_stream_version; + cap.custom_dma = info.cfg.custom_dma; +#endif + + return cap; +} + +void MessageHandler::sendNetworkInfoRsp(const uint32_t src, + const uint64_t msgId, + EthosU::ethosu_core_network_buffer &network) { + auto message = new Message( + src, EthosU::ETHOSU_CORE_MSG_NETWORK_INFO_RSP, msgId, sizeof(EthosU::ethosu_core_msg_network_info_rsp)); + auto &rsp = message->rpmsg.net_info_rsp; + + rsp.ifm_count = 0; + rsp.ofm_count = 0; + + bool failed = networkToVirtual(network); + + if (!failed) { + InferenceProcess::InferenceParser parser; + + failed = parser.parseModel(reinterpret_cast(network.buffer.ptr), + network.buffer.size, + rsp.desc, + InferenceProcess::makeArray(rsp.ifm_size, rsp.ifm_count, ETHOSU_CORE_BUFFER_MAX), + InferenceProcess::makeArray(rsp.ofm_size, rsp.ofm_count, ETHOSU_CORE_BUFFER_MAX)); + } + + rsp.status = failed ? EthosU::ETHOSU_CORE_STATUS_ERROR : EthosU::ETHOSU_CORE_STATUS_OK; + + responseQueue.send(message); +} + +void MessageHandler::forwardInferenceReq(const uint32_t src, + const uint64_t msgId, + const EthosU::ethosu_core_msg_inference_req &inference) { + auto message = new Message(src, EthosU::ETHOSU_CORE_MSG_INFERENCE_REQ, msgId); + auto &req = message->rpmsg.inf_req; + + req = inference; + + for (uint32_t i = 0; i < req.ifm_count; i++) { + bufferToVirtual(req.ifm[i]); + } + + for (uint32_t i = 0; i < req.ofm_count; i++) { + bufferToVirtual(req.ofm[i]); + } + + networkToVirtual(req.network); + + inferenceQueue.send(message); +} + +void MessageHandler::sendInferenceRsp(const uint32_t src, + const uint64_t msgId, + const EthosU::ethosu_core_status status) { + auto message = + new Message(src, EthosU::ETHOSU_CORE_MSG_INFERENCE_RSP, msgId, sizeof(EthosU::ethosu_core_msg_inference_rsp)); + + message->rpmsg.inf_rsp.status = status; + + responseQueue.send(message); +} + +void MessageHandler::sendCancelInferenceRsp(const uint32_t src, + const uint64_t msgId, + const EthosU::ethosu_core_status status) { + auto message = new Message( + src, EthosU::ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP, msgId, sizeof(EthosU::ethosu_core_msg_cancel_inference_rsp)); + + message->rpmsg.cancel_rsp.status = status; + + responseQueue.send(message); +} + +bool MessageHandler::getNetwork(const EthosU::ethosu_core_network_buffer &buffer, void *&data, size_t &size) { + switch (buffer.type) { + case EthosU::ETHOSU_CORE_NETWORK_BUFFER: + data = physicalToVirtual(buffer.buffer.ptr); + size = buffer.buffer.size; + return false; + case EthosU::ETHOSU_CORE_NETWORK_INDEX: + return getIndexedNetwork(buffer.index, data, size); + default: + LOG_WARN("Unsupported network model type. type=%" PRIu32, buffer.type); + return true; + } +} + +bool MessageHandler::bufferToVirtual(EthosU::ethosu_core_buffer &buffer) { + void *ptr = physicalToVirtual(buffer.ptr); + if (ptr == nullptr) { + return true; + } + + buffer.ptr = reinterpret_cast(ptr); + + return false; +} + +bool MessageHandler::networkToVirtual(EthosU::ethosu_core_network_buffer &buffer) { + switch (buffer.type) { + case EthosU::ETHOSU_CORE_NETWORK_BUFFER: + return bufferToVirtual(buffer.buffer); + case EthosU::ETHOSU_CORE_NETWORK_INDEX: { + void *ptr; + size_t size; + if (getIndexedNetwork(buffer.index, ptr, size)) { + return true; + } + + buffer.type = EthosU::ETHOSU_CORE_NETWORK_BUFFER; + buffer.buffer.ptr = reinterpret_cast(ptr); + buffer.buffer.size = size; + + return false; + } + default: + LOG_WARN("Unsupported network model type. type=%" PRIu32, buffer.type); + return true; + } +} + +void MessageHandler::responseTask(void *param) { + auto _this = static_cast(param); + + LOG_DEBUG("Starting message response task"); + + while (true) { + Message *message; + auto ret = _this->responseQueue.receive(message); + if (ret) { + abort(); + } + + LOG_DEBUG("Sending message. type=%" PRIu32, message->rpmsg.header.type); + + _this->send(&message->rpmsg, sizeof(message->rpmsg.header) + message->length, message->src); + + delete message; + } +} diff --git a/applications/message_handler_openamp/message_handler.hpp b/applications/message_handler_openamp/message_handler.hpp new file mode 100644 index 0000000..779b05f --- /dev/null +++ b/applications/message_handler_openamp/message_handler.hpp @@ -0,0 +1,106 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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. + */ + +#pragma once + +/***************************************************************************** + * Includes + *****************************************************************************/ + +#include +#include + +#include "queue.hpp" +#include "remoteproc.hpp" + +/***************************************************************************** + * Messages + *****************************************************************************/ + +struct Message { + Message() {} + + Message(const uint32_t _src, + const EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX, + const uint64_t msgId = 0, + const uint32_t _length = 0) : + src(_src), + length(_length) { + rpmsg.header.magic = ETHOSU_CORE_MSG_MAGIC; + rpmsg.header.type = _type; + rpmsg.header.msg_id = msgId; + } + + uint32_t src = 0; + uint32_t length = 0; + EthosU::ethosu_core_rpmsg rpmsg; +}; + +/***************************************************************************** + * MessageHandler + *****************************************************************************/ + +class MessageHandler : public Rpmsg { +public: + using InferenceQueue = Queue; + using ResponseQueue = Queue; + + MessageHandler(RProc &rproc, const char *const name); + virtual ~MessageHandler(); + + InferenceQueue &getInferenceQueue() { + return inferenceQueue; + } + + InferenceQueue &getResponseQueue() { + return responseQueue; + } + +protected: + // Handle incoming rpmsg + int handleMessage(void *data, size_t len, uint32_t src) override; + + // Outgoing messages + void sendError(const uint32_t src, const EthosU::ethosu_core_err_type type, const char *message); + void sendPong(const uint32_t src, const uint64_t msgId); + void sendVersionRsp(const uint32_t src, const uint64_t msgId); + void sendCapabilitiesRsp(const uint32_t src, const uint64_t msgId); + void sendNetworkInfoRsp(const uint32_t src, const uint64_t msgId, EthosU::ethosu_core_network_buffer &network); + void forwardInferenceReq(const uint32_t src, + const uint64_t msgId, + const EthosU::ethosu_core_msg_inference_req &inference); + void sendInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status); + void sendCancelInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status); + + EthosU::ethosu_core_msg_capabilities_rsp getCapabilities() const; + bool getNetwork(const EthosU::ethosu_core_network_buffer &buffer, void *&data, size_t &size); + + // Tasks returning response messages + static void responseTask(void *param); + +private: + bool bufferToVirtual(EthosU::ethosu_core_buffer &buffer); + bool networkToVirtual(EthosU::ethosu_core_network_buffer &buffer); + + InferenceQueue inferenceQueue; + ResponseQueue responseQueue; + EthosU::ethosu_core_msg_capabilities_rsp capabilities; + + // FreeRTOS + TaskHandle_t taskHandle; +}; diff --git a/applications/message_handler_openamp/queue.hpp b/applications/message_handler_openamp/queue.hpp new file mode 100644 index 0000000..dcf676b --- /dev/null +++ b/applications/message_handler_openamp/queue.hpp @@ -0,0 +1,87 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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. + */ + +#pragma once + +/***************************************************************************** + * Includes + *****************************************************************************/ + +#include +#include + +#include +#include + +#include + +/***************************************************************************** + * Queue + *****************************************************************************/ + +template +class Queue { +public: + using Predicate = std::function; + + Queue(const size_t size = 10) : queue(xQueueCreate(size, sizeof(T))) {} + + ~Queue() { + vQueueDelete(queue); + } + + int send(const T &msg, TickType_t delay = portMAX_DELAY) { + if (pdPASS != xQueueSend(queue, &msg, delay)) { + LOG_ERR("Failed to send message"); + return -1; + } + + return 0; + } + + int receive(T &msg, TickType_t delay = portMAX_DELAY) { + if (pdTRUE != xQueueReceive(queue, &msg, delay)) { + LOG_ERR("Failed to receive message"); + return -1; + } + + return 0; + } + + void erase(Predicate pred) { + const size_t count = uxQueueMessagesWaiting(queue); + for (size_t i = 0; i < count; i++) { + T data; + + if (pdPASS != xQueueReceive(queue, &data, 0)) { + LOG_ERR("Failed to dequeue message"); + abort(); + } + + if (!pred(data)) { + if (pdPASS != xQueueSend(queue, &data, 0)) { + LOG_ERR("Failed to requeue message"); + abort(); + } + } + } + } + +private: + QueueHandle_t queue; +}; diff --git a/applications/message_handler_openamp/remoteproc.cpp b/applications/message_handler_openamp/remoteproc.cpp new file mode 100644 index 0000000..f355634 --- /dev/null +++ b/applications/message_handler_openamp/remoteproc.cpp @@ -0,0 +1,243 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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 "remoteproc.hpp" + +#include + +#include + +/***************************************************************************** + * MetalIO + *****************************************************************************/ + +extern "C" { + +__attribute__((weak)) void *ethosu_phys_to_virt(const uint64_t pa) { + return reinterpret_cast(pa); +} +} + +MetalIO::MetalIO() : + ops{.read = nullptr, + .write = nullptr, + .block_read = nullptr, + .block_write = nullptr, + .block_set = nullptr, + .close = nullptr, + .offset_to_phys = nullptr, + .phys_to_offset = physToOffset} { + remoteproc_init_mem(&mem, "shm", 0, 0, 0xffffffff, ®ion); + + metal_io_init(®ion, + reinterpret_cast(0), /* virt */ + &mem.pa, /* physmap */ + 0xffffffff, /* size */ + -1L, /* pagemask */ + 0, /* attributes */ + &ops); /* ops */ +} + +remoteproc_mem *MetalIO::operator&() { + return &mem; +} + +unsigned long MetalIO::physToOffset(metal_io_region *io, metal_phys_addr_t pa) { + auto offset = reinterpret_cast(ethosu_phys_to_virt(pa)); + LOG_DEBUG("Translate PA to offset. pa=%lx, offset=%lx", pa, offset); + return offset; +} + +/***************************************************************************** + * RProc + *****************************************************************************/ + +RProc::RProc(Mailbox::Mailbox &_mailbox, resource_table &table, size_t tableSize, MetalIO &_mem) : + mailbox(_mailbox), mem(_mem), + ops{ + .init = init, // initialize the remoteproc instance + .remove = remove, // remove the remoteproc instance + .mmap = nullptr, // memory mapped the memory with physical address as input + .handle_rsc = nullptr, // handle the vendor specific resource + .config = nullptr, // configure the remoteproc to make it ready to load and run executable + .start = nullptr, // kick the remoteproc to run application + .stop = nullptr, // stop the remoteproc from running application, the resource such as memory may not be off. + .shutdown = nullptr, // shutdown the remoteproc and release its resources. + .notify = notify, // notify the remote + .get_mem = nullptr, // get remoteproc memory I/O region. + }, + vdev(nullptr), notifySemaphore(xSemaphoreCreateBinary()) { + mailbox.registerCallback(mailboxCallback, static_cast(this)); + + if (!remoteproc_init(&rproc, &ops, this)) { + LOG_ERR("Failed to intialize remoteproc"); + abort(); + } + + int ret = remoteproc_set_rsc_table(&rproc, &table, tableSize); + if (ret) { + LOG_ERR("Failed to set resource table. ret=%d", ret); + abort(); + } + + vdev = remoteproc_create_virtio(&rproc, 0, VIRTIO_DEV_DEVICE, nullptr); + if (!vdev) { + LOG_ERR("Failed to create vdev"); + abort(); + } + + BaseType_t taskret = xTaskCreate(notifyTask, "notifyTask", 1024, this, 2, ¬ifyHandle); + if (taskret != pdPASS) { + LOG_ERR("Failed to create remoteproc notify task"); + abort(); + } +} + +RProc::~RProc() { + mailbox.deregisterCallback(mailboxCallback, static_cast(this)); + vTaskDelete(notifyHandle); +} + +remoteproc *RProc::getRProc() { + return &rproc; +} + +virtio_device *RProc::getVDev() { + return vdev; +} + +void RProc::mailboxCallback(void *userArg) { + auto _this = static_cast(userArg); + + xSemaphoreGiveFromISR(_this->notifySemaphore, nullptr); +} + +void RProc::notifyTask(void *param) { + LOG_DEBUG("Starting message notify task"); + + auto _this = static_cast(param); + + while (true) { + // Wait for event + xSemaphoreTake(_this->notifySemaphore, portMAX_DELAY); + + // Read virtio queue and notify all rpmsg clients + rproc_virtio_notified(_this->vdev, RSC_NOTIFY_ID_ANY); + } +} + +struct remoteproc *RProc::init(remoteproc *rproc, const remoteproc_ops *ops, void *arg) { + LOG_DEBUG(""); + + auto _this = static_cast(arg); + + rproc->ops = ops; + rproc->priv = arg; + remoteproc_add_mem(rproc, &_this->mem); + + return rproc; +} + +void RProc::remove(remoteproc *rproc) { + LOG_DEBUG(""); +} + +int RProc::notify(remoteproc *rproc, uint32_t id) { + LOG_DEBUG(""); + + auto *_this = static_cast(rproc->priv); + _this->mailbox.sendMessage(); + return 0; +} + +/***************************************************************************** + * Rpmsg + *****************************************************************************/ + +Rpmsg::Rpmsg(RProc &rproc, const char *const name) { + + metal_io_region *region = remoteproc_get_io_with_name(rproc.getRProc(), "shm"); + if (!region) { + LOG_ERR("Failed to get shared mem region"); + abort(); + } + + if (rpmsg_init_vdev(&rvdev, rproc.getVDev(), nullptr, region, nullptr)) { + LOG_ERR("Failed to initialize rpmsg vdev"); + abort(); + } + + rdev = rpmsg_virtio_get_rpmsg_device(&rvdev); + if (!rdev) { + LOG_ERR("Failed to get rpmsg dev"); + abort(); + } + + int ret = + rpmsg_create_ept(&endpoint, rdev, name, RPMSG_ADDR_ANY, RPMSG_ADDR_ANY, endpointCallback, nsUnbindCallback); + if (ret != RPMSG_SUCCESS) { + LOG_ERR("Failed to create rpmsg endpoint. ret=%d", ret); + abort(); + } + + endpoint.priv = static_cast(this); +} + +int Rpmsg::send(void *data, size_t len, uint32_t dst) { + LOG_DEBUG("Sending rpmsg. dst=%" PRIu32 ", len=%zu", dst, len); + + int ret = rpmsg_sendto(&endpoint, data, len, dst); + return ret; +} + +void *Rpmsg::physicalToVirtual(metal_phys_addr_t pa) { + return metal_io_phys_to_virt(rvdev.shbuf_io, pa); +} + +void Rpmsg::rpmsgNsBind(rpmsg_device *rdev, const char *name, uint32_t dest) { + LOG_DEBUG(""); +} + +void Rpmsg::nsUnbindCallback(rpmsg_endpoint *ept) { + LOG_DEBUG(""); +} + +int Rpmsg::endpointCallback(rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv) { + LOG_DEBUG("src=%" PRIX32 ", len=%zu", src, len); + + auto _this = static_cast(priv); + _this->handleMessage(data, len, src); + + return 0; +} + +int Rpmsg::handleMessage(void *data, size_t len, uint32_t src) { + LOG_DEBUG("Receiving rpmsg. src=%" PRIu32 ", len=%zu", src, len); + + auto c = static_cast(data); + for (size_t i = 0; i < len; i++) { + printf("%c", c[i]); + } + printf("\n"); + + return 0; +} diff --git a/applications/message_handler_openamp/remoteproc.hpp b/applications/message_handler_openamp/remoteproc.hpp new file mode 100644 index 0000000..2f16e24 --- /dev/null +++ b/applications/message_handler_openamp/remoteproc.hpp @@ -0,0 +1,200 @@ +/* + * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates + * + * 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. + */ + +#pragma once + +/***************************************************************************** + * Includes + *****************************************************************************/ + +#include +#include +#include + +#include +#include +#include + +#include + +/***************************************************************************** + * Resource table + *****************************************************************************/ + +struct ResourceTable { + static constexpr uint32_t VERSION = 1; + static constexpr uint32_t NUM_RESOURCES = 2; + static constexpr uint32_t NUM_VRINGS = 2; + static constexpr uint32_t VRING_ALIGN = 0x100; + // static constexpr uint32_t VRING_SIZE = 0x10; + static constexpr uint32_t RESERVED = 0; + + resource_table table; + uint32_t offset[NUM_RESOURCES]; + fw_rsc_vdev vdev; + fw_rsc_vdev_vring vring[NUM_VRINGS]; + fw_rsc_carveout carveout; + + // clang-format off + constexpr ResourceTable(const uint32_t vringSize = 0x100, const uint32_t carveoutSize = 0) : + table { + VERSION, + NUM_RESOURCES, + { RESERVED, RESERVED }, + {} + }, + offset { + offsetof(ResourceTable, vdev), + offsetof(ResourceTable, carveout), + }, + vdev { + RSC_VDEV, + VIRTIO_ID_RPMSG, + 2, // Notify ID + 1 << VIRTIO_RPMSG_F_NS, + 0, + 0, + 0, + NUM_VRINGS, + { 0, RESERVED }, + {} + }, + vring { + { + FW_RSC_U32_ADDR_ANY, + VRING_ALIGN, + vringSize, + 1, + RESERVED + }, + { + FW_RSC_U32_ADDR_ANY, + VRING_ALIGN, + vringSize, + 2, + RESERVED + } + }, + carveout { + RSC_CARVEOUT, + FW_RSC_U32_ADDR_ANY, + FW_RSC_U32_ADDR_ANY, + carveoutSize, + 0, + RESERVED, + "TFLM arena" + } + {} + // clang-format off +} __attribute__((packed)); + +/***************************************************************************** + * MetalIO + *****************************************************************************/ + +class MetalIO { +public: + MetalIO(); + + remoteproc_mem *operator&(); + +private: + static metal_phys_addr_t offsetToPhys(metal_io_region *io, unsigned long offset); + static unsigned long physToOffset(metal_io_region *io, metal_phys_addr_t phys); + + metal_io_ops ops; + metal_io_region region; + remoteproc_mem mem; +}; + +/***************************************************************************** + * RProc + *****************************************************************************/ + +class RProc { +public: + RProc(Mailbox::Mailbox &_mailbox, resource_table &table, size_t tableSize, MetalIO &_mem); + ~RProc(); + + remoteproc *getRProc(); + virtio_device *getVDev(); + +private: + // IRQ notification callback + static void mailboxCallback(void *userArg); + + // Notification task handling virtio messages + static void notifyTask(void *param); + + // Remote proc ops + static struct remoteproc *init(remoteproc *rproc, const remoteproc_ops *ops, void *arg); + static void remove(remoteproc *rproc); + static void *mmap(remoteproc *rproc, + metal_phys_addr_t *pa, + metal_phys_addr_t *da, + size_t size, + unsigned int attribute, + metal_io_region **io); + static int notify(remoteproc *rproc, uint32_t id); + static struct remoteproc_mem *getMem(remoteproc *rproc, + const char *name, + metal_phys_addr_t pa, + metal_phys_addr_t da, + void *va, + size_t size, + remoteproc_mem *buf); + + // IRQ notification + Mailbox::Mailbox &mailbox; + + // Remoteproc + MetalIO &mem; + remoteproc rproc; + remoteproc_ops ops; + virtio_device *vdev; + + // FreeRTOS + SemaphoreHandle_t notifySemaphore; + TaskHandle_t notifyHandle; +}; + +/***************************************************************************** + * Rpmsg + *****************************************************************************/ + +class Rpmsg { +public: + Rpmsg(RProc &rproc, const char *const name); + + int send(void *data, size_t len, uint32_t dst = 0); + void *physicalToVirtual(metal_phys_addr_t pa); + +protected: + virtual int handleMessage(void *data, size_t len, uint32_t src); + +private: + // RPMsg ops + static void rpmsgNsBind(rpmsg_device *rdev, const char *name, uint32_t dest); + static void nsUnbindCallback(rpmsg_endpoint *ept); + static int endpointCallback(rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv); + + // RPMsg + rpmsg_virtio_device rvdev; + rpmsg_device *rdev; + rpmsg_endpoint endpoint; +}; -- cgit v1.2.1