Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | 5385a64 | 2024-01-17 13:29:43 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates |
| 3 | * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0 |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "ethosu_npu_init.h" |
| 19 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 20 | #include "RTE_Components.h" /* For CPU related defintiions */ |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 21 | #include "log_macros.h" /* Logging functions */ |
| 22 | |
| 23 | #include "ethosu_mem_config.h" /* Arm Ethos-U memory config */ |
| 24 | #include "ethosu_driver.h" /* Arm Ethos-U driver header */ |
| 25 | |
Kshitij Sisodia | 5385a64 | 2024-01-17 13:29:43 +0000 | [diff] [blame] | 26 | /* Mandatory definition checks. */ |
| 27 | #if !defined(ETHOS_U_BASE_ADDR) |
| 28 | #error "NPU base address is undefined." |
| 29 | #endif /* defnied(ETHOS_U_BASE_ADDR) */ |
| 30 | |
| 31 | #if !defined(ETHOS_U_IRQN) |
| 32 | #error "Arm NPU interrupt number is undefined." |
| 33 | #endif /* !defined(ETHOS_U_IRQN) */ |
| 34 | |
| 35 | #if !defined(ETHOS_U_SEC_ENABLED) |
| 36 | #error "Arm NPU security mode is undefined." |
| 37 | #endif /* !defined(ETHOS_U_SEC_ENABLED) */ |
| 38 | |
| 39 | #if !defined(ETHOS_U_PRIV_ENABLED) |
| 40 | #error "Arm NPU privilege mode is undefined." |
| 41 | #endif /* !defined(ETHOS_U_PRIV_ENABLED) */ |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 42 | |
| 43 | #if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) |
| 44 | static uint8_t cache_arena[ETHOS_U_CACHE_BUF_SZ] CACHE_BUF_ATTRIBUTE; |
| 45 | #else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 46 | static uint8_t *cache_arena = NULL; |
| 47 | #endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 48 | |
Kshitij Sisodia | 5385a64 | 2024-01-17 13:29:43 +0000 | [diff] [blame] | 49 | struct ethosu_driver ethosu_drv; /* Default Ethos-U device driver */ |
| 50 | |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 51 | static uint8_t *get_cache_arena() |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 52 | { |
| 53 | return cache_arena; |
| 54 | } |
| 55 | |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 56 | static size_t get_cache_arena_size() |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 57 | { |
| 58 | #if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) |
| 59 | return sizeof(cache_arena); |
| 60 | #else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 61 | return 0; |
| 62 | #endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 63 | } |
| 64 | |
| 65 | /** |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 66 | * @brief Initialises the NPU IRQ |
| 67 | **/ |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 68 | static void arm_ethosu_npu_irq_init(void) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 69 | { |
Kshitij Sisodia | 8bc863d | 2022-03-24 17:53:34 +0000 | [diff] [blame] | 70 | const IRQn_Type ethosu_irqnum = (IRQn_Type)ETHOS_U_IRQN; |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 71 | |
| 72 | /* Register the EthosU IRQ handler in our vector table. |
| 73 | * Note, this handler comes from the EthosU driver */ |
| 74 | NVIC_SetVector(ethosu_irqnum, (uint32_t)arm_ethosu_npu_irq_handler); |
| 75 | |
| 76 | /* Enable the IRQ */ |
| 77 | NVIC_EnableIRQ(ethosu_irqnum); |
| 78 | |
| 79 | debug("EthosU IRQ#: %u, Handler: 0x%p\n", |
| 80 | ethosu_irqnum, arm_ethosu_npu_irq_handler); |
| 81 | } |
| 82 | |
Kshitij Sisodia | 1ec169b | 2022-06-01 09:06:21 +0100 | [diff] [blame] | 83 | /** |
| 84 | * @brief Defines the Ethos-U interrupt handler: just a wrapper around the default |
| 85 | * implementation. |
| 86 | **/ |
| 87 | void arm_ethosu_npu_irq_handler(void) |
| 88 | { |
| 89 | /* Call the default interrupt handler from the NPU driver */ |
| 90 | ethosu_irq_handler(ðosu_drv); |
| 91 | } |
| 92 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 93 | int arm_ethosu_npu_init(void) |
| 94 | { |
| 95 | int err = 0; |
| 96 | |
| 97 | /* Initialise the IRQ */ |
| 98 | arm_ethosu_npu_irq_init(); |
| 99 | |
| 100 | /* Initialise Ethos-U device */ |
Kshitij Sisodia | 26bc923 | 2023-03-10 16:33:23 +0000 | [diff] [blame] | 101 | void* const ethosu_base_address = (void *)(ETHOS_U_BASE_ADDR); |
Kshitij Sisodia | 5385a64 | 2024-01-17 13:29:43 +0000 | [diff] [blame] | 102 | info("Initialising Ethos-U device@0x%" PRIx32 "\n", ETHOS_U_BASE_ADDR); |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 103 | |
Kshitij Sisodia | 5385a64 | 2024-01-17 13:29:43 +0000 | [diff] [blame] | 104 | if (0 != (err = ethosu_init(ðosu_drv, /* Ethos-U driver device pointer */ |
| 105 | ethosu_base_address, /* Ethos-U NPU's base address. */ |
| 106 | get_cache_arena(), /* Pointer to fast mem area - NULL for U55. */ |
| 107 | get_cache_arena_size(), /* Fast mem region size. */ |
| 108 | ETHOS_U_SEC_ENABLED, /* Security enable. */ |
| 109 | ETHOS_U_PRIV_ENABLED))) /* Privilege enable. */ { |
| 110 | printf_err("Failed to initialise Ethos-U device\n"); |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 111 | return err; |
| 112 | } |
| 113 | |
| 114 | info("Ethos-U device initialised\n"); |
| 115 | |
| 116 | /* Get Ethos-U version */ |
| 117 | struct ethosu_driver_version driver_version; |
| 118 | struct ethosu_hw_info hw_info; |
| 119 | |
| 120 | ethosu_get_driver_version(&driver_version); |
| 121 | ethosu_get_hw_info(ðosu_drv, &hw_info); |
| 122 | |
| 123 | info("Ethos-U version info:\n"); |
| 124 | info("\tArch: v%" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", |
| 125 | hw_info.version.arch_major_rev, |
| 126 | hw_info.version.arch_minor_rev, |
| 127 | hw_info.version.arch_patch_rev); |
| 128 | info("\tDriver: v%" PRIu8 ".%" PRIu8 ".%" PRIu8 "\n", |
| 129 | driver_version.major, |
| 130 | driver_version.minor, |
| 131 | driver_version.patch); |
| 132 | info("\tMACs/cc: %" PRIu32 "\n", (uint32_t)(1 << hw_info.cfg.macs_per_cc)); |
| 133 | info("\tCmd stream: v%" PRIu32 "\n", hw_info.cfg.cmd_stream_version); |
| 134 | |
| 135 | return 0; |
| 136 | } |