Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2022 Arm Limited. All rights reserved. |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 19 | /****************************************************************************** |
| 20 | * Includes |
| 21 | ******************************************************************************/ |
| 22 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 23 | #include "ethosu_driver.h" |
Bhavik Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 24 | #include "ethosu_config.h" |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 25 | #include "ethosu_device.h" |
Anton Moberg | 6eab40b | 2021-07-07 11:43:51 +0200 | [diff] [blame] | 26 | #include "ethosu_log.h" |
Per Åstrand | 25d78c0 | 2020-04-21 14:19:44 +0200 | [diff] [blame] | 27 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 28 | #include <assert.h> |
Per Åstrand | 25d78c0 | 2020-04-21 14:19:44 +0200 | [diff] [blame] | 29 | #include <cmsis_compiler.h> |
Per Åstrand | 14ccfee | 2020-09-25 10:40:20 +0200 | [diff] [blame] | 30 | #include <inttypes.h> |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 31 | #include <stdbool.h> |
Bhavik Patel | bf7ae63 | 2020-06-11 21:00:16 +0200 | [diff] [blame] | 32 | #include <stddef.h> |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 36 | /****************************************************************************** |
| 37 | * Defines |
| 38 | ******************************************************************************/ |
| 39 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 40 | #define UNUSED(x) ((void)x) |
| 41 | |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 42 | #define BYTES_IN_32_BITS 4 |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 43 | #define MASK_16_BYTE_ALIGN (0xF) |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 44 | #define OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD 2 |
| 45 | #define DRIVER_ACTION_LENGTH_32_BIT_WORD 1 |
| 46 | #define ETHOSU_FOURCC ('1' << 24 | 'P' << 16 | 'O' << 8 | 'C') // "Custom Operator Payload 1" |
| 47 | |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 48 | #define FAST_MEMORY_BASE_ADDR_INDEX 2 |
| 49 | |
| 50 | /****************************************************************************** |
| 51 | * Types |
| 52 | ******************************************************************************/ |
| 53 | |
| 54 | // Driver actions |
| 55 | enum DRIVER_ACTION_e |
| 56 | { |
| 57 | RESERVED = 0, |
| 58 | OPTIMIZER_CONFIG = 1, |
| 59 | COMMAND_STREAM = 2, |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 60 | NOP = 5, |
| 61 | }; |
| 62 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 63 | // Custom operator payload data struct |
| 64 | struct cop_data_s |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 65 | { |
| 66 | union |
| 67 | { |
| 68 | // Driver action data |
| 69 | struct |
| 70 | { |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 71 | uint8_t driver_action_command; // (valid values in DRIVER_ACTION_e) |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 72 | uint8_t reserved; |
| 73 | |
| 74 | // Driver action data |
| 75 | union |
| 76 | { |
| 77 | // DA_CMD_OPT_CFG |
| 78 | struct |
| 79 | { |
| 80 | uint16_t rel_nbr : 4; |
| 81 | uint16_t patch_nbr : 4; |
| 82 | uint16_t opt_cfg_reserved : 8; |
| 83 | }; |
| 84 | |
| 85 | // DA_CMD_CMSTRM |
| 86 | struct |
| 87 | { |
| 88 | uint16_t length; |
| 89 | }; |
| 90 | |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 91 | uint16_t driver_action_data; |
| 92 | }; |
| 93 | }; |
| 94 | |
| 95 | uint32_t word; |
| 96 | }; |
| 97 | }; |
| 98 | |
| 99 | // optimizer config struct |
| 100 | struct opt_cfg_s |
| 101 | { |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 102 | struct cop_data_s da_data; |
| 103 | uint32_t cfg; |
| 104 | uint32_t id; |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /****************************************************************************** |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 108 | * Variables |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 109 | ******************************************************************************/ |
| 110 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 111 | // Registered drivers linked list HEAD |
| 112 | static struct ethosu_driver *registered_drivers = NULL; |
| 113 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 114 | /****************************************************************************** |
| 115 | * Weak functions - Cache |
| 116 | * |
| 117 | * Default NOP operations. Override if available on the targeted device. |
| 118 | ******************************************************************************/ |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 119 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Flush/clean the data cache by address and size. Passing NULL as p argument |
| 122 | * expects the whole cache to be flushed. |
| 123 | */ |
| 124 | void __attribute__((weak)) ethosu_flush_dcache(uint32_t *p, size_t bytes) |
| 125 | { |
| 126 | UNUSED(p); |
| 127 | UNUSED(bytes); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Invalidate the data cache by address and size. Passing NULL as p argument |
| 132 | * expects the whole cache to be invalidated. |
| 133 | */ |
| 134 | void __attribute__((weak)) ethosu_invalidate_dcache(uint32_t *p, size_t bytes) |
| 135 | { |
| 136 | UNUSED(p); |
| 137 | UNUSED(bytes); |
| 138 | } |
| 139 | |
| 140 | /****************************************************************************** |
| 141 | * Weak functions - Semaphore/Mutex for multi NPU |
| 142 | * |
| 143 | * Following section handles the minimal sempahore and mutex implementation in |
| 144 | * case of baremetal applications. Weak symbols will be overridden by RTOS |
| 145 | * definitions and implement true thread-safety (in application layer). |
| 146 | ******************************************************************************/ |
| 147 | |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 148 | struct ethosu_semaphore_t |
| 149 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 150 | uint8_t count; |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 153 | static void *ethosu_mutex; |
| 154 | static void *ethosu_semaphore; |
| 155 | |
Anton Moberg | 9f346ab | 2021-05-21 17:20:21 +0200 | [diff] [blame] | 156 | void *__attribute__((weak)) ethosu_mutex_create(void) |
| 157 | { |
| 158 | return NULL; |
| 159 | } |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 160 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 161 | void __attribute__((weak)) ethosu_mutex_destroy(void *mutex) |
| 162 | { |
| 163 | UNUSED(mutex); |
| 164 | } |
| 165 | |
Anton Moberg | 61ec36b | 2021-04-30 17:10:48 +0200 | [diff] [blame] | 166 | void __attribute__((weak)) ethosu_mutex_lock(void *mutex) |
| 167 | { |
| 168 | UNUSED(mutex); |
| 169 | } |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 170 | |
Anton Moberg | 61ec36b | 2021-04-30 17:10:48 +0200 | [diff] [blame] | 171 | void __attribute__((weak)) ethosu_mutex_unlock(void *mutex) |
| 172 | { |
| 173 | UNUSED(mutex); |
| 174 | } |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 175 | |
| 176 | // Baremetal implementation of creating a semaphore |
| 177 | void *__attribute__((weak)) ethosu_semaphore_create(void) |
| 178 | { |
| 179 | struct ethosu_semaphore_t *sem = malloc(sizeof(*sem)); |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 180 | sem->count = 0; |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 181 | return sem; |
| 182 | } |
| 183 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 184 | void __attribute__((weak)) ethosu_semaphore_destroy(void *sem) |
| 185 | { |
| 186 | free((struct ethosu_semaphore_t *)sem); |
| 187 | } |
| 188 | |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 189 | // Baremetal simulation of waiting/sleeping for and then taking a semaphore using intrisics |
| 190 | void __attribute__((weak)) ethosu_semaphore_take(void *sem) |
| 191 | { |
| 192 | struct ethosu_semaphore_t *s = sem; |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 193 | while (s->count == 0) |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 194 | { |
| 195 | __WFE(); |
| 196 | } |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 197 | s->count = 0; |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Baremetal simulation of giving a semaphore and waking up processes using intrinsics |
| 201 | void __attribute__((weak)) ethosu_semaphore_give(void *sem) |
| 202 | { |
| 203 | struct ethosu_semaphore_t *s = sem; |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 204 | s->count = 1; |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 205 | __SEV(); |
| 206 | } |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 207 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 208 | /****************************************************************************** |
| 209 | * Weak functions - Inference begin/end callbacks |
| 210 | ******************************************************************************/ |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 211 | |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 212 | void __attribute__((weak)) ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg) |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 213 | { |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 214 | UNUSED(user_arg); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 215 | UNUSED(drv); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 218 | void __attribute__((weak)) ethosu_inference_end(struct ethosu_driver *drv, void *user_arg) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 219 | { |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 220 | UNUSED(user_arg); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 221 | UNUSED(drv); |
| 222 | } |
| 223 | |
| 224 | /****************************************************************************** |
| 225 | * Static functions |
| 226 | ******************************************************************************/ |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 227 | static void ethosu_register_driver(struct ethosu_driver *drv) |
Jens Elofsson | 04961a4 | 2021-04-08 18:51:38 +0200 | [diff] [blame] | 228 | { |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 229 | // Register driver as new HEAD of list |
| 230 | drv->next = registered_drivers; |
| 231 | registered_drivers = drv; |
| 232 | |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 233 | LOG_INFO("New NPU driver registered (handle: 0x%p, NPU: 0x%p)", drv, drv->dev->reg); |
Jens Elofsson | 04961a4 | 2021-04-08 18:51:38 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 236 | static int ethosu_deregister_driver(struct ethosu_driver *drv) |
| 237 | { |
| 238 | struct ethosu_driver *cur = registered_drivers; |
| 239 | struct ethosu_driver **prev = ®istered_drivers; |
| 240 | |
| 241 | while (cur != NULL) |
| 242 | { |
| 243 | if (cur == drv) |
| 244 | { |
| 245 | *prev = cur->next; |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 246 | LOG_INFO("NPU driver handle %p deregistered.", drv); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | prev = &cur->next; |
| 251 | cur = cur->next; |
| 252 | } |
| 253 | |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 254 | LOG_ERR("No NPU driver handle registered at address %p.", drv); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 255 | |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | static struct ethosu_driver *ethosu_find_and_reserve_driver(void) |
| 260 | { |
| 261 | struct ethosu_driver *drv = registered_drivers; |
| 262 | |
| 263 | while (drv != NULL) |
| 264 | { |
| 265 | if (!drv->reserved) |
| 266 | { |
| 267 | drv->reserved = true; |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 268 | LOG_DEBUG("NPU driver handle %p reserved.", drv); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 269 | return drv; |
| 270 | } |
| 271 | drv = drv->next; |
| 272 | } |
| 273 | |
Jonny Svärd | 20ce37f | 2021-12-17 17:00:57 +0100 | [diff] [blame] | 274 | LOG_WARN("No NPU driver handle available."); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 275 | |
| 276 | return NULL; |
| 277 | } |
| 278 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 279 | static void ethosu_reset_job(struct ethosu_driver *drv) |
| 280 | { |
| 281 | memset(&drv->job, 0, sizeof(struct ethosu_job)); |
| 282 | } |
| 283 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 284 | static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 285 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 286 | LOG_INFO("Optimizer release nbr: %d patch: %d", opt_cfg_p->da_data.rel_nbr, opt_cfg_p->da_data.patch_nbr); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 287 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 288 | if (ethosu_dev_verify_optimizer_config(drv->dev, opt_cfg_p->cfg, opt_cfg_p->id) != true) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 289 | { |
| 290 | return -1; |
| 291 | } |
| 292 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 296 | static int handle_command_stream(struct ethosu_driver *drv, const uint8_t *cmd_stream, const int cms_length) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 297 | { |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 298 | uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS; |
| 299 | ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream; |
| 300 | |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 301 | LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d", cmd_stream, cms_length); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 302 | |
| 303 | if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN)) |
| 304 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 305 | LOG_ERR("Command stream addr %p not aligned to 16 bytes", cmd_stream); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 309 | // Verify 16 byte alignment for base address' |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 310 | for (int i = 0; i < drv->job.num_base_addr; i++) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 311 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 312 | if (0 != (drv->job.base_addr[i] & MASK_16_BYTE_ALIGN)) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 313 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 314 | LOG_ERR("Base addr %d: 0x%llx not aligned to 16 bytes", i, drv->job.base_addr[i]); |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 315 | return -1; |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 319 | drv->job.state = ETHOSU_JOB_RUNNING; |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 320 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 321 | // Flush the cache if available on CPU. |
| 322 | // The upcasting to uin32_t* is ok since the pointer never is dereferenced. |
| 323 | // The base_addr_size is null if invoking from prior to invoke_V2, in that case |
| 324 | // the whole cache is being flushed. |
| 325 | |
| 326 | if (drv->job.base_addr_size != NULL) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 327 | { |
| 328 | ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes); |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 329 | for (int i = 0; i < drv->job.num_base_addr; i++) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 330 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 331 | ethosu_flush_dcache((uint32_t *)(uintptr_t)drv->job.base_addr[i], drv->job.base_addr_size[i]); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | ethosu_flush_dcache(NULL, 0); |
| 337 | } |
| 338 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 339 | // Request power gating disabled during inference run |
| 340 | if (!drv->dev_power_always_on) |
| 341 | { |
| 342 | // Will soft reset if security state or privilege level needs changing. |
| 343 | // Also note that any configurations done in the NPU prior to this point |
| 344 | // are lost in case power gating has been in effect. |
| 345 | set_clock_and_power_request(drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE); |
| 346 | |
| 347 | // Make sure AXI settings are applied |
| 348 | ethosu_dev_axi_init(drv->dev); |
| 349 | } |
| 350 | |
| 351 | // Inference begin callback |
| 352 | ethosu_inference_begin(drv, drv->job.user_arg); |
| 353 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 354 | // Execute the command stream |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 355 | ethosu_dev_run_command_stream(drv->dev, cmd_stream, cms_bytes, drv->job.base_addr, drv->job.num_base_addr); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 356 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /****************************************************************************** |
| 361 | * Weak functions - Interrupt handler |
| 362 | ******************************************************************************/ |
| 363 | void __attribute__((weak)) ethosu_irq_handler(struct ethosu_driver *drv) |
| 364 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 365 | LOG_DEBUG("Got interrupt from Ethos-U"); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 366 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 367 | drv->job.state = ETHOSU_JOB_DONE; |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 368 | if (!ethosu_dev_handle_interrupt(drv->dev)) |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 369 | { |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 370 | drv->status_error = true; |
| 371 | } |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 372 | ethosu_semaphore_give(drv->semaphore); |
| 373 | } |
| 374 | |
| 375 | /****************************************************************************** |
| 376 | * Functions API |
| 377 | ******************************************************************************/ |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 378 | |
Anton Moberg | effc7aa | 2021-05-03 09:25:06 +0200 | [diff] [blame] | 379 | int ethosu_init(struct ethosu_driver *drv, |
| 380 | const void *base_address, |
| 381 | const void *fast_memory, |
| 382 | const size_t fast_memory_size, |
| 383 | uint32_t secure_enable, |
| 384 | uint32_t privilege_enable) |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 385 | { |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 386 | LOG_INFO("Initializing NPU: base_address=%p, fast_memory=%p, fast_memory_size=%zu, secure=%" PRIu32 |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 387 | ", privileged=%" PRIu32, |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 388 | base_address, |
| 389 | fast_memory, |
Per Åstrand | e6498f0 | 2020-11-09 15:33:12 +0100 | [diff] [blame] | 390 | fast_memory_size, |
| 391 | secure_enable, |
| 392 | privilege_enable); |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 393 | |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 394 | if (!ethosu_mutex) |
| 395 | { |
| 396 | ethosu_mutex = ethosu_mutex_create(); |
| 397 | } |
| 398 | |
| 399 | if (!ethosu_semaphore) |
| 400 | { |
| 401 | ethosu_semaphore = ethosu_semaphore_create(); |
| 402 | } |
| 403 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 404 | drv->fast_memory = (uint32_t)fast_memory; |
| 405 | drv->fast_memory_size = fast_memory_size; |
| 406 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 407 | // Initialize the device and set requested security state and privilege mode |
| 408 | drv->dev = ethosu_dev_init(base_address, secure_enable, privilege_enable); |
| 409 | |
| 410 | if (drv->dev == NULL) |
Bhavik Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 411 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 412 | LOG_ERR("Failed to initialize Ethos-U device"); |
Bhavik Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 413 | return -1; |
| 414 | } |
| 415 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 416 | // Power always ON requested |
| 417 | if (drv->dev_power_always_on) |
Bhavik Patel | e645fed | 2020-06-12 14:46:47 +0200 | [diff] [blame] | 418 | { |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 419 | if (set_clock_and_power_request(drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE) != |
| 420 | ETHOSU_SUCCESS) |
| 421 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 422 | LOG_ERR("Failed to disable power-q for Ethos-U"); |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 423 | return -1; |
| 424 | } |
Bhavik Patel | e645fed | 2020-06-12 14:46:47 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 427 | drv->semaphore = ethosu_semaphore_create(); |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 428 | drv->status_error = false; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 429 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 430 | ethosu_register_driver(drv); |
| 431 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 432 | return 0; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Anton Moberg | 547ca53 | 2021-06-14 09:43:53 +0200 | [diff] [blame] | 435 | void ethosu_deinit(struct ethosu_driver *drv) |
| 436 | { |
| 437 | ethosu_deregister_driver(drv); |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 438 | ethosu_semaphore_destroy(drv->semaphore); |
| 439 | ethosu_dev_deinit(drv->dev); |
| 440 | drv->dev = NULL; |
Anton Moberg | 547ca53 | 2021-06-14 09:43:53 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 443 | void ethosu_get_driver_version(struct ethosu_driver_version *ver) |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 444 | { |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 445 | assert(ver != NULL); |
| 446 | ver->major = ETHOSU_DRIVER_VERSION_MAJOR; |
| 447 | ver->minor = ETHOSU_DRIVER_VERSION_MINOR; |
| 448 | ver->patch = ETHOSU_DRIVER_VERSION_PATCH; |
| 449 | } |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 450 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 451 | void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw) |
| 452 | { |
| 453 | assert(hw != NULL); |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 454 | ethosu_dev_get_hw_info(drv->dev, hw); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 457 | int ethosu_wait(struct ethosu_driver *drv, bool block) |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 458 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 459 | int ret = 0; |
| 460 | |
| 461 | switch (drv->job.state) |
| 462 | { |
| 463 | case ETHOSU_JOB_IDLE: |
| 464 | LOG_ERR("Inference job not running..."); |
| 465 | ret = -2; |
| 466 | break; |
| 467 | case ETHOSU_JOB_RUNNING: |
| 468 | if (!block) |
| 469 | { |
| 470 | // Inference still running, do not block |
| 471 | ret = 1; |
| 472 | break; |
| 473 | } |
| 474 | // fall through |
| 475 | case ETHOSU_JOB_DONE: |
| 476 | // Wait for interrupt in blocking mode. In non-blocking mode |
| 477 | // the interrupt has already triggered |
| 478 | ethosu_semaphore_take(drv->semaphore); |
| 479 | |
| 480 | // Inference done callback |
| 481 | ethosu_inference_end(drv, drv->job.user_arg); |
| 482 | |
| 483 | // Check NPU and interrupt status |
| 484 | if (drv->status_error) |
| 485 | { |
| 486 | LOG_ERR("NPU error(s) occured during inference."); |
| 487 | ethosu_dev_print_err_status(drv->dev); |
| 488 | |
| 489 | // Reset the NPU |
| 490 | (void)ethosu_dev_soft_reset(drv->dev); |
| 491 | // NPU is no longer in error state |
| 492 | drv->status_error = false; |
| 493 | |
| 494 | ret = -1; |
| 495 | } |
| 496 | |
| 497 | // Clear the clock/power gating disable request |
| 498 | if (!drv->dev_power_always_on) |
| 499 | { |
| 500 | // NOTE: Other requesters (like PMU) can be active, keeping |
| 501 | // clock/power gating disabled until no requests remain. |
| 502 | set_clock_and_power_request(drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE); |
| 503 | } |
| 504 | |
| 505 | if (ret == 0) |
| 506 | { |
| 507 | // Invalidate cache |
| 508 | if (drv->job.base_addr_size != NULL) |
| 509 | { |
| 510 | for (int i = 0; i < drv->job.num_base_addr; i++) |
| 511 | { |
| 512 | ethosu_invalidate_dcache((uint32_t *)(uintptr_t)drv->job.base_addr[i], drv->job.base_addr_size[i]); |
| 513 | } |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | ethosu_invalidate_dcache(NULL, 0); |
| 518 | } |
| 519 | |
| 520 | LOG_DEBUG("Inference finished successfully..."); |
| 521 | } |
| 522 | |
| 523 | // Reset internal job (state resets to IDLE) |
| 524 | ethosu_reset_job(drv); |
| 525 | break; |
| 526 | |
| 527 | default: |
| 528 | LOG_ERR("Unexpected job state"); |
| 529 | ethosu_reset_job(drv); |
| 530 | ret = -1; |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | // Return inference job status |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | int ethosu_invoke_async(struct ethosu_driver *drv, |
| 539 | const void *custom_data_ptr, |
| 540 | const int custom_data_size, |
| 541 | const uint64_t *base_addr, |
| 542 | const size_t *base_addr_size, |
| 543 | const int num_base_addr, |
| 544 | void *user_arg) |
| 545 | { |
| 546 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 547 | const struct cop_data_s *data_ptr = custom_data_ptr; |
Kristofer Jonsson | 24455ee | 2022-02-08 13:33:22 +0100 | [diff] [blame] | 548 | const struct cop_data_s *data_end = (struct cop_data_s *)((ptrdiff_t)custom_data_ptr + custom_data_size); |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 549 | |
| 550 | // Make sure an inference is not already running |
| 551 | if (drv->job.state != ETHOSU_JOB_IDLE) |
| 552 | { |
| 553 | LOG_ERR("Inference already running, or waiting to be cleared..."); |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | drv->job.state = ETHOSU_JOB_IDLE; |
| 558 | drv->job.custom_data_ptr = custom_data_ptr; |
| 559 | drv->job.custom_data_size = custom_data_size; |
| 560 | drv->job.base_addr = base_addr; |
| 561 | drv->job.base_addr_size = base_addr_size; |
| 562 | drv->job.num_base_addr = num_base_addr; |
| 563 | drv->job.user_arg = user_arg; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 564 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 565 | // First word in custom_data_ptr should contain "Custom Operator Payload 1" |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 566 | if (data_ptr->word != ETHOSU_FOURCC) |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 567 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 568 | LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x", data_ptr->word, ETHOSU_FOURCC); |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 569 | goto err; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 570 | } |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 571 | |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 572 | // Custom data length must be a multiple of 32 bits |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 573 | if ((custom_data_size % BYTES_IN_32_BITS) != 0) |
| 574 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 575 | LOG_ERR("custom_data_size=0x%x not a multiple of 4", custom_data_size); |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 576 | goto err; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 577 | } |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 578 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 579 | data_ptr++; |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 580 | |
| 581 | // Adjust base address to fast memory area |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 582 | if (drv->fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX) |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 583 | { |
| 584 | uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX]; |
| 585 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 586 | if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > drv->fast_memory_size) |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 587 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 588 | LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u", |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 589 | drv->fast_memory_size, |
Kristofer Jonsson | 4c94b30 | 2020-11-06 10:33:21 +0100 | [diff] [blame] | 590 | base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]); |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 591 | goto err; |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 592 | } |
Kristofer Jonsson | 4c94b30 | 2020-11-06 10:33:21 +0100 | [diff] [blame] | 593 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 594 | *fast_memory = drv->fast_memory; |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 595 | } |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 596 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 597 | drv->status_error = false; |
Kristofer Jonsson | 125429a | 2020-08-20 16:52:23 +0200 | [diff] [blame] | 598 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 599 | // Parse Custom Operator Payload data |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 600 | while (data_ptr < data_end) |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 601 | { |
| 602 | switch (data_ptr->driver_action_command) |
| 603 | { |
| 604 | case OPTIMIZER_CONFIG: |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 605 | LOG_DEBUG("OPTIMIZER_CONFIG"); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 606 | struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr; |
| 607 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 608 | if (handle_optimizer_config(drv, opt_cfg_p) < 0) |
| 609 | { |
| 610 | goto err; |
| 611 | } |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 612 | data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD; |
| 613 | break; |
| 614 | case COMMAND_STREAM: |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 615 | // Vela only supports putting one COMMAND_STREAM per op |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 616 | LOG_DEBUG("COMMAND_STREAM"); |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 617 | void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct cop_data_s); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 618 | int cms_length = (data_ptr->reserved << 16) | data_ptr->length; |
| 619 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 620 | if (handle_command_stream(drv, command_stream, cms_length) < 0) |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 621 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 622 | goto err; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 623 | } |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 624 | data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length; |
| 625 | break; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 626 | case NOP: |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 627 | LOG_DEBUG("NOP"); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 628 | data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD; |
| 629 | break; |
| 630 | default: |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 631 | LOG_ERR("UNSUPPORTED driver_action_command: %d", data_ptr->driver_action_command); |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 632 | goto err; |
Bhavik Patel | e645fed | 2020-06-12 14:46:47 +0200 | [diff] [blame] | 633 | break; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 634 | } |
| 635 | } |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 636 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 637 | return 0; |
| 638 | err: |
| 639 | LOG_ERR("Failed to invoke inference."); |
| 640 | ethosu_reset_job(drv); |
| 641 | return -1; |
| 642 | } |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 643 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 644 | int ethosu_invoke_v3(struct ethosu_driver *drv, |
| 645 | const void *custom_data_ptr, |
| 646 | const int custom_data_size, |
| 647 | const uint64_t *base_addr, |
| 648 | const size_t *base_addr_size, |
| 649 | const int num_base_addr, |
| 650 | void *user_arg) |
| 651 | { |
| 652 | if (ethosu_invoke_async( |
| 653 | drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr, user_arg) < 0) |
Bhavik Patel | 5f8dad1 | 2020-09-30 09:06:52 +0200 | [diff] [blame] | 654 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 655 | return -1; |
Bhavik Patel | 5f8dad1 | 2020-09-30 09:06:52 +0200 | [diff] [blame] | 656 | } |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 657 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 658 | return ethosu_wait(drv, true); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Anton Moberg | effc7aa | 2021-05-03 09:25:06 +0200 | [diff] [blame] | 661 | void ethosu_set_power_mode(struct ethosu_driver *drv, bool always_on) |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 662 | { |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 663 | drv->dev_power_always_on = always_on; |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 664 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 665 | if (always_on) |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 666 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 667 | if (ethosu_dev_verify_access_state(drv->dev) == false) |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 668 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 669 | // Reset to enter correct security state/privilege mode |
| 670 | if (ethosu_dev_soft_reset(drv->dev) == false) |
| 671 | { |
| 672 | LOG_ERR("Failed to set power mode for Ethos-U"); |
| 673 | return; |
| 674 | } |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 675 | } |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 676 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 677 | ethosu_dev_set_clock_and_power(drv->dev, ETHOSU_CLOCK_Q_UNCHANGED, ETHOSU_POWER_Q_DISABLE); |
| 678 | ethosu_dev_axi_init(drv->dev); |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | ethosu_dev_set_clock_and_power(drv->dev, ETHOSU_CLOCK_Q_UNCHANGED, ETHOSU_POWER_Q_ENABLE); |
| 683 | } |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 686 | struct ethosu_driver *ethosu_reserve_driver(void) |
| 687 | { |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 688 | struct ethosu_driver *drv = NULL; |
| 689 | |
| 690 | do |
| 691 | { |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 692 | ethosu_mutex_lock(ethosu_mutex); |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 693 | drv = ethosu_find_and_reserve_driver(); |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 694 | ethosu_mutex_unlock(ethosu_mutex); |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 695 | |
| 696 | if (drv != NULL) |
| 697 | { |
| 698 | break; |
| 699 | } |
| 700 | |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 701 | LOG_INFO("Waiting for NPU driver handle to become available..."); |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 702 | ethosu_semaphore_take(ethosu_semaphore); |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 703 | |
| 704 | } while (1); |
| 705 | |
| 706 | return drv; |
| 707 | } |
| 708 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 709 | void ethosu_release_driver(struct ethosu_driver *drv) |
| 710 | { |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 711 | ethosu_mutex_lock(ethosu_mutex); |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 712 | if (drv != NULL && drv->reserved) |
| 713 | { |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame^] | 714 | if (drv->job.state == ETHOSU_JOB_RUNNING || drv->job.state == ETHOSU_JOB_DONE) |
| 715 | { |
| 716 | // Give the inference one shot to complete or force kill the job |
| 717 | if (ethosu_wait(drv, false) == 1) |
| 718 | { |
| 719 | // Still running, soft reset the NPU and reset driver |
| 720 | ethosu_dev_soft_reset(drv->dev); |
| 721 | ethosu_reset_job(drv); |
| 722 | drv->status_error = false; |
| 723 | ethosu_semaphore_give(drv->semaphore); |
| 724 | (void)set_clock_and_power_request( |
| 725 | drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE); |
| 726 | } |
| 727 | } |
| 728 | |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 729 | drv->reserved = false; |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 730 | LOG_DEBUG("NPU driver handle %p released", drv); |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 731 | ethosu_semaphore_give(ethosu_semaphore); |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 732 | } |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 733 | ethosu_mutex_unlock(ethosu_mutex); |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 736 | enum ethosu_error_codes set_clock_and_power_request(struct ethosu_driver *drv, |
| 737 | enum ethosu_request_clients client, |
| 738 | enum ethosu_clock_q_request clock_request, |
| 739 | enum ethosu_power_q_request power_request) |
| 740 | { |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 741 | // Keep track of which client requests clock gating to be disabled |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 742 | if (clock_request == ETHOSU_CLOCK_Q_DISABLE) |
| 743 | { |
| 744 | drv->clock_request |= (1 << client); |
| 745 | } |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 746 | else if (clock_request == ETHOSU_CLOCK_Q_ENABLE) // Remove client from bitmask |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 747 | { |
| 748 | drv->clock_request &= ~(1 << client); |
| 749 | } |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 750 | |
| 751 | // Only enable clock gating when no client has asked for it to be disabled |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 752 | clock_request = drv->clock_request == 0 ? ETHOSU_CLOCK_Q_ENABLE : ETHOSU_CLOCK_Q_DISABLE; |
| 753 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 754 | // Keep track of which client requests power gating to be disabled |
Anton Moberg | 35b5d0e | 2021-04-13 13:32:17 +0200 | [diff] [blame] | 755 | if (power_request == ETHOSU_POWER_Q_DISABLE) |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 756 | { |
| 757 | drv->power_request |= (1 << client); |
| 758 | } |
Jonny Svärd | c1b9dbb | 2021-11-05 12:41:23 +0100 | [diff] [blame] | 759 | else if (power_request == ETHOSU_POWER_Q_ENABLE) |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 760 | { |
| 761 | drv->power_request &= ~(1 << client); |
| 762 | } |
Anton Moberg | 35b5d0e | 2021-04-13 13:32:17 +0200 | [diff] [blame] | 763 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 764 | // Override if power has been requested to be always on |
| 765 | if (drv->dev_power_always_on == true) |
| 766 | { |
| 767 | power_request = ETHOSU_POWER_Q_DISABLE; |
| 768 | } |
| 769 | else |
| 770 | { |
| 771 | // Only enable power gating when no client has asked for it to be disabled |
| 772 | power_request = drv->power_request == 0 ? ETHOSU_POWER_Q_ENABLE : ETHOSU_POWER_Q_DISABLE; |
| 773 | } |
| 774 | |
| 775 | // Verify security state and privilege mode if power is requested to be on |
| 776 | if (power_request == ETHOSU_POWER_Q_DISABLE) |
| 777 | { |
| 778 | if (ethosu_dev_verify_access_state(drv->dev) == false) |
| 779 | { |
| 780 | if (ethosu_dev_soft_reset(drv->dev) != ETHOSU_SUCCESS) |
| 781 | { |
Kristofer Jonsson | 089a347 | 2021-11-12 12:52:07 +0100 | [diff] [blame] | 782 | LOG_ERR("Failed to set clock and power q channels for Ethos-U"); |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 783 | return ETHOSU_GENERIC_FAILURE; |
| 784 | } |
| 785 | } |
| 786 | } |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 787 | // Set clock and power |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 788 | return ethosu_dev_set_clock_and_power(drv->dev, clock_request, power_request); |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 789 | } |