alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 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 | */ |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 17 | #include "hal.h" /* API */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 19 | #include "hal_config.h" /* HAL configuration */ |
| 20 | #include "platform_drivers.h" /* Platform drivers */ |
| 21 | #include "log_macros.h" /* Logging macros */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <assert.h> |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 25 | #include <string.h> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | |
| 27 | #if defined(ARM_NPU) |
| 28 | |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 29 | #include "ethosu_mem_config.h" /* Arm Ethos-U memory config */ |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 30 | #include "ethosu_driver.h" /* Arm Ethos-U driver header */ |
| 31 | #include "timing_adapter.h" /* Arm Ethos-U timing adapter driver header */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(TIMING_ADAPTER_AVAILABLE) |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 34 | #include "timing_adapter_settings.h" /* Arm Ethos-U timing adapter settings */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 35 | #endif /* defined(TIMING_ADAPTER_AVAILABLE) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 36 | |
Cisco Cervellera | 414b1b9 | 2021-09-28 18:15:10 +0100 | [diff] [blame] | 37 | struct ethosu_driver ethosu_drv; /* Default Ethos-U device driver */ |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 38 | |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 39 | #if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) |
| 40 | static uint8_t cache_arena[ETHOS_U_CACHE_BUF_SZ] CACHE_BUF_ATTRIBUTE; |
| 41 | #else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 42 | static uint8_t* cache_arena = NULL; |
| 43 | #endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 44 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | /** |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 46 | * @brief Initialises the Arm Ethos-U NPU |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | * @return 0 if successful, error code otherwise |
| 48 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 49 | static int arm_npu_init(void); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 50 | |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 51 | static uint8_t * get_cache_arena() |
| 52 | { |
| 53 | return cache_arena; |
| 54 | } |
| 55 | |
| 56 | static size_t get_cache_arena_size() |
| 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 65 | #endif /* ARM_NPU */ |
| 66 | |
| 67 | int hal_init(hal_platform* platform, data_acq_module* data_acq, |
| 68 | data_psn_module* data_psn, platform_timer* timer) |
| 69 | { |
| 70 | assert(platform && data_acq && data_psn); |
| 71 | |
| 72 | platform->data_acq = data_acq; |
| 73 | platform->data_psn = data_psn; |
| 74 | platform->timer = timer; |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 75 | platform->platform_init = platform_init; |
| 76 | platform->platform_release = platform_release; |
| 77 | platform_name(platform->plat_name, sizeof(platform->plat_name)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @brief Local helper function to clean the slate for current platform. |
| 84 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 85 | static void hal_platform_clear(hal_platform* platform) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | { |
| 87 | assert(platform); |
| 88 | platform->inited = 0; |
| 89 | } |
| 90 | |
| 91 | int hal_platform_init(hal_platform* platform) |
| 92 | { |
| 93 | int state; |
| 94 | assert(platform && platform->platform_init); |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 95 | hal_platform_clear(platform); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | |
| 97 | /* Initialise platform */ |
| 98 | if (0 != (state = platform->platform_init())) { |
| 99 | printf_err("failed to initialise platform %s\n", platform->plat_name); |
| 100 | return state; |
| 101 | } |
| 102 | |
| 103 | /* Initialise the data acquisition module */ |
| 104 | if (0 != (state = data_acq_channel_init(platform->data_acq))) { |
| 105 | if (!platform->data_acq->inited) { |
| 106 | printf_err("failed to initialise data acq module: %s\n", |
| 107 | platform->data_acq->system_name); |
| 108 | } |
| 109 | hal_platform_release(platform); |
| 110 | return state; |
| 111 | } |
| 112 | |
| 113 | /* Initialise the presentation module */ |
| 114 | if (0 != (state = data_psn_system_init(platform->data_psn))) { |
| 115 | printf_err("failed to initialise data psn module: %s\n", |
| 116 | platform->data_psn->system_name); |
| 117 | data_acq_channel_release(platform->data_acq); |
| 118 | hal_platform_release(platform); |
| 119 | return state; |
| 120 | } |
| 121 | |
| 122 | #if defined(ARM_NPU) |
| 123 | |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 124 | /* If Arm Ethos-U NPU is to be used, we initialise it here */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 125 | if (0 != (state = arm_npu_init())) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 126 | return state; |
| 127 | } |
| 128 | |
| 129 | #endif /* ARM_NPU */ |
| 130 | |
| 131 | /* followed by the timer module */ |
| 132 | init_timer(platform->timer); |
| 133 | |
| 134 | info("%s platform initialised\n", platform->plat_name); |
| 135 | debug("using %s module for data acquisition\n", |
| 136 | platform->data_acq->system_name); |
| 137 | debug("using %s module for data presentation\n", |
| 138 | platform->data_psn->system_name); |
| 139 | |
| 140 | platform->inited = !state; |
| 141 | |
| 142 | return state; |
| 143 | } |
| 144 | |
| 145 | void hal_platform_release(hal_platform *platform) |
| 146 | { |
| 147 | assert(platform && platform->platform_release); |
| 148 | data_acq_channel_release(platform->data_acq); |
| 149 | data_psn_system_release(platform->data_psn); |
| 150 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 151 | hal_platform_clear(platform); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 152 | info("releasing platform %s\n", platform->plat_name); |
| 153 | platform->platform_release(); |
| 154 | } |
| 155 | |
| 156 | #if defined(ARM_NPU) |
| 157 | /** |
| 158 | * @brief Defines the Ethos-U interrupt handler: just a wrapper around the default |
| 159 | * implementation. |
| 160 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 161 | static void arm_npu_irq_handler(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 162 | { |
| 163 | /* Call the default interrupt handler from the NPU driver */ |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 164 | ethosu_irq_handler(ðosu_drv); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @brief Initialises the NPU IRQ |
| 169 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 170 | static void arm_npu_irq_init(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | { |
| 172 | const IRQn_Type ethosu_irqnum = (IRQn_Type)EthosU_IRQn; |
| 173 | |
| 174 | /* Register the EthosU IRQ handler in our vector table. |
| 175 | * Note, this handler comes from the EthosU driver */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 176 | NVIC_SetVector(ethosu_irqnum, (uint32_t)arm_npu_irq_handler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 177 | |
| 178 | /* Enable the IRQ */ |
| 179 | NVIC_EnableIRQ(ethosu_irqnum); |
| 180 | |
| 181 | debug("EthosU IRQ#: %u, Handler: 0x%p\n", |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 182 | ethosu_irqnum, arm_npu_irq_handler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 183 | } |
| 184 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 185 | #if defined(TIMING_ADAPTER_AVAILABLE) |
| 186 | static int _arm_npu_timing_adapter_init(void) |
| 187 | { |
| 188 | #if defined (TA0_BASE) |
| 189 | struct timing_adapter ta_0; |
| 190 | struct timing_adapter_settings ta_0_settings = { |
| 191 | .maxr = TA0_MAXR, |
| 192 | .maxw = TA0_MAXW, |
| 193 | .maxrw = TA0_MAXRW, |
| 194 | .rlatency = TA0_RLATENCY, |
| 195 | .wlatency = TA0_WLATENCY, |
| 196 | .pulse_on = TA0_PULSE_ON, |
| 197 | .pulse_off = TA0_PULSE_OFF, |
| 198 | .bwcap = TA0_BWCAP, |
| 199 | .perfctrl = TA0_PERFCTRL, |
| 200 | .perfcnt = TA0_PERFCNT, |
| 201 | .mode = TA0_MODE, |
| 202 | .maxpending = 0, /* This is a read-only parameter */ |
| 203 | .histbin = TA0_HISTBIN, |
| 204 | .histcnt = TA0_HISTCNT |
| 205 | }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 206 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 207 | if (0 != ta_init(&ta_0, TA0_BASE)) { |
| 208 | printf_err("TA0 initialisation failed\n"); |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | ta_set_all(&ta_0, &ta_0_settings); |
| 213 | #endif /* defined (TA0_BASE) */ |
| 214 | |
| 215 | #if defined (TA1_BASE) |
| 216 | struct timing_adapter ta_1; |
| 217 | struct timing_adapter_settings ta_1_settings = { |
| 218 | .maxr = TA1_MAXR, |
| 219 | .maxw = TA1_MAXW, |
| 220 | .maxrw = TA1_MAXRW, |
| 221 | .rlatency = TA1_RLATENCY, |
| 222 | .wlatency = TA1_WLATENCY, |
| 223 | .pulse_on = TA1_PULSE_ON, |
| 224 | .pulse_off = TA1_PULSE_OFF, |
| 225 | .bwcap = TA1_BWCAP, |
| 226 | .perfctrl = TA1_PERFCTRL, |
| 227 | .perfcnt = TA1_PERFCNT, |
| 228 | .mode = TA1_MODE, |
| 229 | .maxpending = 0, /* This is a read-only parameter */ |
| 230 | .histbin = TA1_HISTBIN, |
| 231 | .histcnt = TA1_HISTCNT |
| 232 | }; |
| 233 | |
| 234 | if (0 != ta_init(&ta_1, TA1_BASE)) { |
| 235 | printf_err("TA1 initialisation failed\n"); |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | ta_set_all(&ta_1, &ta_1_settings); |
| 240 | #endif /* defined (TA1_BASE) */ |
| 241 | |
| 242 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 243 | } |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 244 | #endif /* defined(TIMING_ADAPTER_AVAILABLE) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 245 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 246 | static int arm_npu_init(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 247 | { |
| 248 | int err = 0; |
| 249 | |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 250 | /* If the platform has timing adapter blocks along with Ethos-U core |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 251 | * block, initialise them here. */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 252 | #if defined(TIMING_ADAPTER_AVAILABLE) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 253 | if (0 != (err = _arm_npu_timing_adapter_init())) { |
| 254 | return err; |
| 255 | } |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 256 | #endif /* defined(TIMING_ADAPTER_AVAILABLE) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 257 | |
| 258 | /* Initialise the IRQ */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 259 | arm_npu_irq_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 260 | |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 261 | /* Initialise Ethos-U device */ |
| 262 | const void * ethosu_base_address = (void *)(SEC_ETHOS_U_NPU_BASE); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 263 | |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 264 | if (0 != (err = ethosu_init( |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 265 | ðosu_drv, /* Ethos-U driver device pointer */ |
| 266 | ethosu_base_address, /* Ethos-U NPU's base address. */ |
| 267 | get_cache_arena(), /* Pointer to fast mem area - NULL for U55. */ |
| 268 | get_cache_arena_size(), /* Fast mem region size. */ |
| 269 | 1, /* Security enable. */ |
| 270 | 1))) { /* Privilege enable. */ |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 271 | printf_err("failed to initalise Ethos-U device\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 272 | return err; |
| 273 | } |
| 274 | |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 275 | info("Ethos-U device initialised\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 276 | |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 277 | /* Get Ethos-U version */ |
Cisco Cervellera | 414b1b9 | 2021-09-28 18:15:10 +0100 | [diff] [blame] | 278 | struct ethosu_driver_version driver_version; |
| 279 | struct ethosu_hw_info hw_info; |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 280 | |
Cisco Cervellera | 414b1b9 | 2021-09-28 18:15:10 +0100 | [diff] [blame] | 281 | ethosu_get_driver_version(&driver_version); |
| 282 | ethosu_get_hw_info(ðosu_drv, &hw_info); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 283 | |
Cisco Cervellera | f085fa5 | 2021-08-02 09:32:07 +0100 | [diff] [blame] | 284 | info("Ethos-U version info:\n"); |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 285 | info("\tArch: v%"PRIu32".%"PRIu32".%"PRIu32"\n", |
Cisco Cervellera | 414b1b9 | 2021-09-28 18:15:10 +0100 | [diff] [blame] | 286 | hw_info.version.arch_major_rev, |
| 287 | hw_info.version.arch_minor_rev, |
| 288 | hw_info.version.arch_patch_rev); |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 289 | info("\tDriver: v%"PRIu8".%"PRIu8".%"PRIu8"\n", |
Cisco Cervellera | 414b1b9 | 2021-09-28 18:15:10 +0100 | [diff] [blame] | 290 | driver_version.major, |
| 291 | driver_version.minor, |
| 292 | driver_version.patch); |
| 293 | info("\tMACs/cc: %"PRIu32"\n", (uint32_t)(1 << hw_info.cfg.macs_per_cc)); |
| 294 | info("\tCmd stream: v%"PRIu32"\n", hw_info.cfg.cmd_stream_version); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 298 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 299 | #endif /* ARM_NPU */ |