blob: 2a28988ee729b84193e617688ed281b89c2240ba [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
Jonny Svärdc9644242023-01-09 15:54:24 +01002 * SPDX-FileCopyrightText: Copyright 2019-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02003 *
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 Jonsson2b201c32020-09-02 16:42:43 +020019/******************************************************************************
20 * Includes
21 ******************************************************************************/
22
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020023#include "ethosu_driver.h"
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020024#include "ethosu_device.h"
Anton Moberg6eab40b2021-07-07 11:43:51 +020025#include "ethosu_log.h"
Per Åstrand25d78c02020-04-21 14:19:44 +020026
Kristofer Jonsson1c0e7ae2022-05-12 11:35:33 +020027#ifdef ETHOSU55
28#include "ethosu_config_u55.h"
29#else
30#include "ethosu_config_u65.h"
31#endif
32
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020033#include <assert.h>
Per Åstrand25d78c02020-04-21 14:19:44 +020034#include <cmsis_compiler.h>
Per Åstrand14ccfee2020-09-25 10:40:20 +020035#include <inttypes.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020036#include <stdbool.h>
Bhavik Patelbf7ae632020-06-11 21:00:16 +020037#include <stddef.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020038#include <stdio.h>
39#include <stdlib.h>
40
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020041/******************************************************************************
42 * Defines
43 ******************************************************************************/
44
Jonny Svärd136810f2021-10-13 16:04:26 +020045#define UNUSED(x) ((void)x)
46
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020047#define BYTES_IN_32_BITS 4
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020048#define MASK_16_BYTE_ALIGN (0xF)
Jonny Svärd136810f2021-10-13 16:04:26 +020049#define OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD 2
50#define DRIVER_ACTION_LENGTH_32_BIT_WORD 1
51#define ETHOSU_FOURCC ('1' << 24 | 'P' << 16 | 'O' << 8 | 'C') // "Custom Operator Payload 1"
52
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020053#define FAST_MEMORY_BASE_ADDR_INDEX 2
54
55/******************************************************************************
56 * Types
57 ******************************************************************************/
58
59// Driver actions
60enum DRIVER_ACTION_e
61{
62 RESERVED = 0,
63 OPTIMIZER_CONFIG = 1,
64 COMMAND_STREAM = 2,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020065 NOP = 5,
66};
67
Jonny Svärd136810f2021-10-13 16:04:26 +020068// Custom operator payload data struct
69struct cop_data_s
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020070{
71 union
72 {
73 // Driver action data
74 struct
75 {
Jonny Svärd136810f2021-10-13 16:04:26 +020076 uint8_t driver_action_command; // (valid values in DRIVER_ACTION_e)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020077 uint8_t reserved;
78
79 // Driver action data
80 union
81 {
82 // DA_CMD_OPT_CFG
83 struct
84 {
85 uint16_t rel_nbr : 4;
86 uint16_t patch_nbr : 4;
87 uint16_t opt_cfg_reserved : 8;
88 };
89
90 // DA_CMD_CMSTRM
91 struct
92 {
93 uint16_t length;
94 };
95
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020096 uint16_t driver_action_data;
97 };
98 };
99
100 uint32_t word;
101 };
102};
103
104// optimizer config struct
105struct opt_cfg_s
106{
Jonny Svärd136810f2021-10-13 16:04:26 +0200107 struct cop_data_s da_data;
108 uint32_t cfg;
109 uint32_t id;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200110};
111
112/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +0200113 * Variables
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200114 ******************************************************************************/
115
Anton Moberg61da4d32020-12-22 16:00:31 +0100116// Registered drivers linked list HEAD
117static struct ethosu_driver *registered_drivers = NULL;
118
Jonny Svärda830f172021-06-07 16:57:00 +0200119/******************************************************************************
120 * Weak functions - Cache
121 *
122 * Default NOP operations. Override if available on the targeted device.
123 ******************************************************************************/
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100124
Jonny Svärda830f172021-06-07 16:57:00 +0200125/*
126 * Flush/clean the data cache by address and size. Passing NULL as p argument
127 * expects the whole cache to be flushed.
128 */
129void __attribute__((weak)) ethosu_flush_dcache(uint32_t *p, size_t bytes)
130{
131 UNUSED(p);
132 UNUSED(bytes);
133}
134
135/*
136 * Invalidate the data cache by address and size. Passing NULL as p argument
137 * expects the whole cache to be invalidated.
138 */
139void __attribute__((weak)) ethosu_invalidate_dcache(uint32_t *p, size_t bytes)
140{
141 UNUSED(p);
142 UNUSED(bytes);
143}
144
145/******************************************************************************
146 * Weak functions - Semaphore/Mutex for multi NPU
147 *
148 * Following section handles the minimal sempahore and mutex implementation in
149 * case of baremetal applications. Weak symbols will be overridden by RTOS
150 * definitions and implement true thread-safety (in application layer).
151 ******************************************************************************/
152
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100153struct ethosu_semaphore_t
154{
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100155 uint8_t count;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100156};
157
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100158static void *ethosu_mutex;
159static void *ethosu_semaphore;
160
Anton Moberg9f346ab2021-05-21 17:20:21 +0200161void *__attribute__((weak)) ethosu_mutex_create(void)
162{
163 return NULL;
164}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100165
Jonny Svärd136810f2021-10-13 16:04:26 +0200166void __attribute__((weak)) ethosu_mutex_destroy(void *mutex)
167{
168 UNUSED(mutex);
169}
170
Ledion Dajac6505f32022-04-20 09:55:21 +0200171int __attribute__((weak)) ethosu_mutex_lock(void *mutex)
Anton Moberg61ec36b2021-04-30 17:10:48 +0200172{
173 UNUSED(mutex);
Ledion Dajac6505f32022-04-20 09:55:21 +0200174 return 0;
Anton Moberg61ec36b2021-04-30 17:10:48 +0200175}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100176
Ledion Dajac6505f32022-04-20 09:55:21 +0200177int __attribute__((weak)) ethosu_mutex_unlock(void *mutex)
Anton Moberg61ec36b2021-04-30 17:10:48 +0200178{
179 UNUSED(mutex);
Ledion Dajac6505f32022-04-20 09:55:21 +0200180 return 0;
Anton Moberg61ec36b2021-04-30 17:10:48 +0200181}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100182
183// Baremetal implementation of creating a semaphore
184void *__attribute__((weak)) ethosu_semaphore_create(void)
185{
186 struct ethosu_semaphore_t *sem = malloc(sizeof(*sem));
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100187 sem->count = 0;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100188 return sem;
189}
190
Jonny Svärd136810f2021-10-13 16:04:26 +0200191void __attribute__((weak)) ethosu_semaphore_destroy(void *sem)
192{
193 free((struct ethosu_semaphore_t *)sem);
194}
195
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100196// Baremetal simulation of waiting/sleeping for and then taking a semaphore using intrisics
Ledion Dajac6505f32022-04-20 09:55:21 +0200197int __attribute__((weak)) ethosu_semaphore_take(void *sem)
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100198{
199 struct ethosu_semaphore_t *s = sem;
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100200 while (s->count == 0)
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100201 {
202 __WFE();
203 }
Jonny Svärdc9644242023-01-09 15:54:24 +0100204 s->count--;
Ledion Dajac6505f32022-04-20 09:55:21 +0200205 return 0;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100206}
207
208// Baremetal simulation of giving a semaphore and waking up processes using intrinsics
Ledion Dajac6505f32022-04-20 09:55:21 +0200209int __attribute__((weak)) ethosu_semaphore_give(void *sem)
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100210{
211 struct ethosu_semaphore_t *s = sem;
Jonny Svärdc9644242023-01-09 15:54:24 +0100212 s->count++;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100213 __SEV();
Ledion Dajac6505f32022-04-20 09:55:21 +0200214 return 0;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100215}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100216
Jonny Svärda830f172021-06-07 16:57:00 +0200217/******************************************************************************
218 * Weak functions - Inference begin/end callbacks
219 ******************************************************************************/
Anton Moberg61da4d32020-12-22 16:00:31 +0100220
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100221void __attribute__((weak)) ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200222{
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100223 UNUSED(user_arg);
Jonny Svärda830f172021-06-07 16:57:00 +0200224 UNUSED(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200225}
226
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100227void __attribute__((weak)) ethosu_inference_end(struct ethosu_driver *drv, void *user_arg)
Jonny Svärda830f172021-06-07 16:57:00 +0200228{
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100229 UNUSED(user_arg);
Jonny Svärda830f172021-06-07 16:57:00 +0200230 UNUSED(drv);
231}
232
233/******************************************************************************
234 * Static functions
235 ******************************************************************************/
Jonny Svärda830f172021-06-07 16:57:00 +0200236static void ethosu_register_driver(struct ethosu_driver *drv)
Jens Elofsson04961a42021-04-08 18:51:38 +0200237{
Jonny Svärdc9644242023-01-09 15:54:24 +0100238 ethosu_mutex_lock(ethosu_mutex);
Jonny Svärda830f172021-06-07 16:57:00 +0200239 drv->next = registered_drivers;
240 registered_drivers = drv;
Jonny Svärdc9644242023-01-09 15:54:24 +0100241 ethosu_mutex_unlock(ethosu_mutex);
242
243 ethosu_semaphore_give(ethosu_semaphore);
Jonny Svärda830f172021-06-07 16:57:00 +0200244
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100245 LOG_INFO("New NPU driver registered (handle: 0x%p, NPU: 0x%p)", drv, drv->dev->reg);
Jens Elofsson04961a42021-04-08 18:51:38 +0200246}
247
Jonny Svärda830f172021-06-07 16:57:00 +0200248static int ethosu_deregister_driver(struct ethosu_driver *drv)
249{
Jonny Svärdc9644242023-01-09 15:54:24 +0100250 struct ethosu_driver *curr;
251 struct ethosu_driver **prev;
Jonny Svärda830f172021-06-07 16:57:00 +0200252
Jonny Svärdc9644242023-01-09 15:54:24 +0100253 ethosu_mutex_lock(ethosu_mutex);
254 curr = registered_drivers;
255 prev = &registered_drivers;
256
257 while (curr != NULL)
Jonny Svärda830f172021-06-07 16:57:00 +0200258 {
Jonny Svärdc9644242023-01-09 15:54:24 +0100259 if (curr == drv)
Jonny Svärda830f172021-06-07 16:57:00 +0200260 {
Jonny Svärdc9644242023-01-09 15:54:24 +0100261 *prev = curr->next;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100262 LOG_INFO("NPU driver handle %p deregistered.", drv);
Jonny Svärdc9644242023-01-09 15:54:24 +0100263 ethosu_semaphore_take(ethosu_semaphore);
264 break;
Jonny Svärda830f172021-06-07 16:57:00 +0200265 }
266
Jonny Svärdc9644242023-01-09 15:54:24 +0100267 prev = &curr->next;
268 curr = curr->next;
Jonny Svärda830f172021-06-07 16:57:00 +0200269 }
270
Jonny Svärdc9644242023-01-09 15:54:24 +0100271 ethosu_mutex_unlock(ethosu_mutex);
Jonny Svärda830f172021-06-07 16:57:00 +0200272
Jonny Svärdc9644242023-01-09 15:54:24 +0100273 if (curr == NULL)
Jonny Svärda830f172021-06-07 16:57:00 +0200274 {
Jonny Svärdc9644242023-01-09 15:54:24 +0100275 LOG_ERR("No NPU driver handle registered at address %p.", drv);
276 return -1;
Jonny Svärda830f172021-06-07 16:57:00 +0200277 }
278
Jonny Svärdc9644242023-01-09 15:54:24 +0100279 return 0;
Jonny Svärda830f172021-06-07 16:57:00 +0200280}
281
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100282static void ethosu_reset_job(struct ethosu_driver *drv)
283{
284 memset(&drv->job, 0, sizeof(struct ethosu_job));
285}
286
Jonny Svärd136810f2021-10-13 16:04:26 +0200287static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p)
Jonny Svärda830f172021-06-07 16:57:00 +0200288{
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100289 LOG_INFO("Optimizer release nbr: %d patch: %d", opt_cfg_p->da_data.rel_nbr, opt_cfg_p->da_data.patch_nbr);
Jonny Svärda830f172021-06-07 16:57:00 +0200290
Jonny Svärd136810f2021-10-13 16:04:26 +0200291 if (ethosu_dev_verify_optimizer_config(drv->dev, opt_cfg_p->cfg, opt_cfg_p->id) != true)
Jonny Svärda830f172021-06-07 16:57:00 +0200292 {
293 return -1;
294 }
295
Jonny Svärda830f172021-06-07 16:57:00 +0200296 return 0;
297}
298
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100299static int handle_command_stream(struct ethosu_driver *drv, const uint8_t *cmd_stream, const int cms_length)
Jonny Svärda830f172021-06-07 16:57:00 +0200300{
Jonny Svärda830f172021-06-07 16:57:00 +0200301 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
302 ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream;
303
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100304 LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d", cmd_stream, cms_length);
Jonny Svärda830f172021-06-07 16:57:00 +0200305
306 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
307 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100308 LOG_ERR("Command stream addr %p not aligned to 16 bytes", cmd_stream);
Jonny Svärda830f172021-06-07 16:57:00 +0200309 return -1;
310 }
311
Jonny Svärd136810f2021-10-13 16:04:26 +0200312 // Verify 16 byte alignment for base address'
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100313 for (int i = 0; i < drv->job.num_base_addr; i++)
Jonny Svärda830f172021-06-07 16:57:00 +0200314 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100315 if (0 != (drv->job.base_addr[i] & MASK_16_BYTE_ALIGN))
Jonny Svärda830f172021-06-07 16:57:00 +0200316 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100317 LOG_ERR("Base addr %d: 0x%llx not aligned to 16 bytes", i, drv->job.base_addr[i]);
Jonny Svärd136810f2021-10-13 16:04:26 +0200318 return -1;
Jonny Svärda830f172021-06-07 16:57:00 +0200319 }
320 }
321
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100322 // Flush the cache if available on CPU.
323 // The upcasting to uin32_t* is ok since the pointer never is dereferenced.
324 // The base_addr_size is null if invoking from prior to invoke_V2, in that case
325 // the whole cache is being flushed.
326
327 if (drv->job.base_addr_size != NULL)
Jonny Svärda830f172021-06-07 16:57:00 +0200328 {
329 ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100330 for (int i = 0; i < drv->job.num_base_addr; i++)
Jonny Svärda830f172021-06-07 16:57:00 +0200331 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100332 ethosu_flush_dcache((uint32_t *)(uintptr_t)drv->job.base_addr[i], drv->job.base_addr_size[i]);
Jonny Svärda830f172021-06-07 16:57:00 +0200333 }
334 }
335 else
336 {
337 ethosu_flush_dcache(NULL, 0);
338 }
339
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100340 // Request power gating disabled during inference run
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100341 if (ethosu_request_power(drv))
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100342 {
Jonny Svärd301399d2022-04-26 18:31:24 +0200343 LOG_ERR("Failed to request power");
344 return -1;
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100345 }
346
Jonny Svärd301399d2022-04-26 18:31:24 +0200347 drv->job.state = ETHOSU_JOB_RUNNING;
348
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100349 // Inference begin callback
350 ethosu_inference_begin(drv, drv->job.user_arg);
351
Jonny Svärd136810f2021-10-13 16:04:26 +0200352 // Execute the command stream
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100353 ethosu_dev_run_command_stream(drv->dev, cmd_stream, cms_bytes, drv->job.base_addr, drv->job.num_base_addr);
Jonny Svärda830f172021-06-07 16:57:00 +0200354
Jonny Svärda830f172021-06-07 16:57:00 +0200355 return 0;
356}
357
358/******************************************************************************
359 * Weak functions - Interrupt handler
360 ******************************************************************************/
361void __attribute__((weak)) ethosu_irq_handler(struct ethosu_driver *drv)
362{
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100363 LOG_DEBUG("Got interrupt from Ethos-U");
Jonny Svärda830f172021-06-07 16:57:00 +0200364
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100365 drv->job.state = ETHOSU_JOB_DONE;
Jonny Svärd136810f2021-10-13 16:04:26 +0200366 if (!ethosu_dev_handle_interrupt(drv->dev))
Jonny Svärda830f172021-06-07 16:57:00 +0200367 {
Jonny Svärda830f172021-06-07 16:57:00 +0200368 drv->status_error = true;
369 }
Jonny Svärda830f172021-06-07 16:57:00 +0200370 ethosu_semaphore_give(drv->semaphore);
371}
372
373/******************************************************************************
374 * Functions API
375 ******************************************************************************/
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200376
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200377int ethosu_init(struct ethosu_driver *drv,
378 const void *base_address,
379 const void *fast_memory,
380 const size_t fast_memory_size,
381 uint32_t secure_enable,
382 uint32_t privilege_enable)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200383{
Jonny Svärda830f172021-06-07 16:57:00 +0200384 LOG_INFO("Initializing NPU: base_address=%p, fast_memory=%p, fast_memory_size=%zu, secure=%" PRIu32
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100385 ", privileged=%" PRIu32,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200386 base_address,
387 fast_memory,
Per Åstrande6498f02020-11-09 15:33:12 +0100388 fast_memory_size,
389 secure_enable,
390 privilege_enable);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200391
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100392 if (!ethosu_mutex)
393 {
394 ethosu_mutex = ethosu_mutex_create();
395 }
396
397 if (!ethosu_semaphore)
398 {
399 ethosu_semaphore = ethosu_semaphore_create();
Jonny Svärdc9644242023-01-09 15:54:24 +0100400 if (!ethosu_semaphore)
401 {
402 LOG_ERR("Failed to create global driver semaphore");
403 return -1;
404 }
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100405 }
406
Jonny Svärd301399d2022-04-26 18:31:24 +0200407 drv->fast_memory = (uint32_t)fast_memory;
408 drv->fast_memory_size = fast_memory_size;
409 drv->power_request_counter = 0;
Anton Moberg61da4d32020-12-22 16:00:31 +0100410
Jonny Svärd136810f2021-10-13 16:04:26 +0200411 // Initialize the device and set requested security state and privilege mode
412 drv->dev = ethosu_dev_init(base_address, secure_enable, privilege_enable);
413
414 if (drv->dev == NULL)
Bhavik Pateldae5be02020-06-18 15:25:15 +0200415 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100416 LOG_ERR("Failed to initialize Ethos-U device");
Bhavik Pateldae5be02020-06-18 15:25:15 +0200417 return -1;
418 }
419
Jonny Svärdc9644242023-01-09 15:54:24 +0100420 drv->semaphore = ethosu_semaphore_create();
421 if (!drv->semaphore)
422 {
423 LOG_ERR("Failed to create driver semaphore");
424 ethosu_dev_deinit(drv->dev);
425 drv->dev = NULL;
426 return -1;
427 }
428
Anton Moberg61da4d32020-12-22 16:00:31 +0100429 drv->status_error = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200430
Jonny Svärd301399d2022-04-26 18:31:24 +0200431 ethosu_reset_job(drv);
Jonny Svärda830f172021-06-07 16:57:00 +0200432 ethosu_register_driver(drv);
433
Jonny Svärd136810f2021-10-13 16:04:26 +0200434 return 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200435}
436
Anton Moberg547ca532021-06-14 09:43:53 +0200437void ethosu_deinit(struct ethosu_driver *drv)
438{
439 ethosu_deregister_driver(drv);
Jonny Svärd136810f2021-10-13 16:04:26 +0200440 ethosu_semaphore_destroy(drv->semaphore);
441 ethosu_dev_deinit(drv->dev);
442 drv->dev = NULL;
Anton Moberg547ca532021-06-14 09:43:53 +0200443}
444
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100445int ethosu_soft_reset(struct ethosu_driver *drv)
Jonny Svärd301399d2022-04-26 18:31:24 +0200446{
447 // Soft reset the NPU
448 if (ethosu_dev_soft_reset(drv->dev) != ETHOSU_SUCCESS)
449 {
450 LOG_ERR("Failed to soft-reset NPU");
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100451 return -1;
Jonny Svärd301399d2022-04-26 18:31:24 +0200452 }
453
454 // Update power and clock gating after the soft reset
455 ethosu_dev_set_clock_and_power(drv->dev,
456 drv->power_request_counter > 0 ? ETHOSU_CLOCK_Q_DISABLE : ETHOSU_CLOCK_Q_ENABLE,
457 drv->power_request_counter > 0 ? ETHOSU_POWER_Q_DISABLE : ETHOSU_POWER_Q_ENABLE);
458
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100459 return 0;
Jonny Svärd301399d2022-04-26 18:31:24 +0200460}
461
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100462int ethosu_request_power(struct ethosu_driver *drv)
Jonny Svärd301399d2022-04-26 18:31:24 +0200463{
464 // Check if this is the first power request, increase counter
465 if (drv->power_request_counter++ == 0)
466 {
467 // Always reset to a known state. Changes to requested
468 // security state/privilege mode if necessary.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100469 if (ethosu_soft_reset(drv))
Jonny Svärd301399d2022-04-26 18:31:24 +0200470 {
471 LOG_ERR("Failed to request power for Ethos-U");
472 drv->power_request_counter--;
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100473 return -1;
Jonny Svärd301399d2022-04-26 18:31:24 +0200474 }
475 }
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100476 return 0;
Jonny Svärd301399d2022-04-26 18:31:24 +0200477}
478
479void ethosu_release_power(struct ethosu_driver *drv)
480{
481 if (drv->power_request_counter == 0)
482 {
483 LOG_WARN("No power request left to release, reference counter is 0");
484 }
485 else
486 {
487 // Decrement ref counter and enable power gating if no requests remain
488 if (--drv->power_request_counter == 0)
489 {
490 ethosu_dev_set_clock_and_power(drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
491 }
492 }
493}
494
Jonny Svärda830f172021-06-07 16:57:00 +0200495void ethosu_get_driver_version(struct ethosu_driver_version *ver)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200496{
Jonny Svärda830f172021-06-07 16:57:00 +0200497 assert(ver != NULL);
498 ver->major = ETHOSU_DRIVER_VERSION_MAJOR;
499 ver->minor = ETHOSU_DRIVER_VERSION_MINOR;
500 ver->patch = ETHOSU_DRIVER_VERSION_PATCH;
501}
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200502
Jonny Svärda830f172021-06-07 16:57:00 +0200503void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw)
504{
505 assert(hw != NULL);
Jonny Svärd136810f2021-10-13 16:04:26 +0200506 ethosu_dev_get_hw_info(drv->dev, hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200507}
508
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100509int ethosu_wait(struct ethosu_driver *drv, bool block)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200510{
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100511 int ret = 0;
512
513 switch (drv->job.state)
514 {
515 case ETHOSU_JOB_IDLE:
516 LOG_ERR("Inference job not running...");
517 ret = -2;
518 break;
519 case ETHOSU_JOB_RUNNING:
520 if (!block)
521 {
522 // Inference still running, do not block
523 ret = 1;
524 break;
525 }
526 // fall through
527 case ETHOSU_JOB_DONE:
528 // Wait for interrupt in blocking mode. In non-blocking mode
529 // the interrupt has already triggered
530 ethosu_semaphore_take(drv->semaphore);
531
532 // Inference done callback
533 ethosu_inference_end(drv, drv->job.user_arg);
534
Jonny Svärd301399d2022-04-26 18:31:24 +0200535 // Relase power gating disabled requirement
536 ethosu_release_power(drv);
537
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100538 // Check NPU and interrupt status
539 if (drv->status_error)
540 {
541 LOG_ERR("NPU error(s) occured during inference.");
542 ethosu_dev_print_err_status(drv->dev);
543
544 // Reset the NPU
Jonny Svärd301399d2022-04-26 18:31:24 +0200545 (void)ethosu_soft_reset(drv);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100546 // NPU is no longer in error state
547 drv->status_error = false;
548
549 ret = -1;
550 }
551
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100552 if (ret == 0)
553 {
554 // Invalidate cache
555 if (drv->job.base_addr_size != NULL)
556 {
557 for (int i = 0; i < drv->job.num_base_addr; i++)
558 {
559 ethosu_invalidate_dcache((uint32_t *)(uintptr_t)drv->job.base_addr[i], drv->job.base_addr_size[i]);
560 }
561 }
562 else
563 {
564 ethosu_invalidate_dcache(NULL, 0);
565 }
566
567 LOG_DEBUG("Inference finished successfully...");
568 }
569
570 // Reset internal job (state resets to IDLE)
571 ethosu_reset_job(drv);
572 break;
573
574 default:
575 LOG_ERR("Unexpected job state");
576 ethosu_reset_job(drv);
577 ret = -1;
578 break;
579 }
580
581 // Return inference job status
582 return ret;
583}
584
585int ethosu_invoke_async(struct ethosu_driver *drv,
586 const void *custom_data_ptr,
587 const int custom_data_size,
588 const uint64_t *base_addr,
589 const size_t *base_addr_size,
590 const int num_base_addr,
591 void *user_arg)
592{
593
Jonny Svärd136810f2021-10-13 16:04:26 +0200594 const struct cop_data_s *data_ptr = custom_data_ptr;
Kristofer Jonsson24455ee2022-02-08 13:33:22 +0100595 const struct cop_data_s *data_end = (struct cop_data_s *)((ptrdiff_t)custom_data_ptr + custom_data_size);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100596
597 // Make sure an inference is not already running
598 if (drv->job.state != ETHOSU_JOB_IDLE)
599 {
600 LOG_ERR("Inference already running, or waiting to be cleared...");
601 return -1;
602 }
603
604 drv->job.state = ETHOSU_JOB_IDLE;
605 drv->job.custom_data_ptr = custom_data_ptr;
606 drv->job.custom_data_size = custom_data_size;
607 drv->job.base_addr = base_addr;
608 drv->job.base_addr_size = base_addr_size;
609 drv->job.num_base_addr = num_base_addr;
610 drv->job.user_arg = user_arg;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200611
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200612 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200613 if (data_ptr->word != ETHOSU_FOURCC)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200614 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100615 LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x", data_ptr->word, ETHOSU_FOURCC);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100616 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200617 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200618
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200619 // Custom data length must be a multiple of 32 bits
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200620 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
621 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100622 LOG_ERR("custom_data_size=0x%x not a multiple of 4", custom_data_size);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100623 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200624 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200625
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100626 data_ptr++;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200627
628 // Adjust base address to fast memory area
Anton Moberg61da4d32020-12-22 16:00:31 +0100629 if (drv->fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200630 {
631 uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX];
632
Anton Moberg61da4d32020-12-22 16:00:31 +0100633 if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > drv->fast_memory_size)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200634 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100635 LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u",
Anton Moberg61da4d32020-12-22 16:00:31 +0100636 drv->fast_memory_size,
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100637 base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100638 goto err;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200639 }
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100640
Anton Moberg61da4d32020-12-22 16:00:31 +0100641 *fast_memory = drv->fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200642 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200643
Anton Moberg61da4d32020-12-22 16:00:31 +0100644 drv->status_error = false;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200645
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100646 // Parse Custom Operator Payload data
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200647 while (data_ptr < data_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200648 {
649 switch (data_ptr->driver_action_command)
650 {
651 case OPTIMIZER_CONFIG:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100652 LOG_DEBUG("OPTIMIZER_CONFIG");
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200653 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
654
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100655 if (handle_optimizer_config(drv, opt_cfg_p) < 0)
656 {
657 goto err;
658 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200659 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
660 break;
661 case COMMAND_STREAM:
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100662 // Vela only supports putting one COMMAND_STREAM per op
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100663 LOG_DEBUG("COMMAND_STREAM");
Jonny Svärd136810f2021-10-13 16:04:26 +0200664 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct cop_data_s);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200665 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
666
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100667 if (handle_command_stream(drv, command_stream, cms_length) < 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200668 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100669 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200670 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200671 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
672 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200673 case NOP:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100674 LOG_DEBUG("NOP");
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200675 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
676 break;
677 default:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100678 LOG_ERR("UNSUPPORTED driver_action_command: %d", data_ptr->driver_action_command);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100679 goto err;
Bhavik Patele645fed2020-06-12 14:46:47 +0200680 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200681 }
682 }
Jonny Svärd136810f2021-10-13 16:04:26 +0200683
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100684 return 0;
685err:
686 LOG_ERR("Failed to invoke inference.");
687 ethosu_reset_job(drv);
688 return -1;
689}
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200690
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100691int ethosu_invoke_v3(struct ethosu_driver *drv,
692 const void *custom_data_ptr,
693 const int custom_data_size,
694 const uint64_t *base_addr,
695 const size_t *base_addr_size,
696 const int num_base_addr,
697 void *user_arg)
698{
699 if (ethosu_invoke_async(
700 drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr, user_arg) < 0)
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200701 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100702 return -1;
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200703 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200704
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100705 return ethosu_wait(drv, true);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200706}
707
Anton Moberg61da4d32020-12-22 16:00:31 +0100708struct ethosu_driver *ethosu_reserve_driver(void)
709{
Anton Mobergdf386e02021-02-02 11:26:48 +0100710 struct ethosu_driver *drv = NULL;
711
Jonny Svärdc9644242023-01-09 15:54:24 +0100712 LOG_INFO("Acquiring NPU driver handle");
713 ethosu_semaphore_take(ethosu_semaphore); // This is meant to block until available
Anton Mobergdf386e02021-02-02 11:26:48 +0100714
Jonny Svärdc9644242023-01-09 15:54:24 +0100715 ethosu_mutex_lock(ethosu_mutex);
716 drv = registered_drivers;
717
718 while (drv != NULL)
719 {
720 if (!drv->reserved)
Anton Mobergdf386e02021-02-02 11:26:48 +0100721 {
Jonny Svärdc9644242023-01-09 15:54:24 +0100722 drv->reserved = true;
723 LOG_DEBUG("NPU driver handle %p reserved", drv);
Anton Mobergdf386e02021-02-02 11:26:48 +0100724 break;
725 }
Jonny Svärdc9644242023-01-09 15:54:24 +0100726 drv = drv->next;
727 }
728 ethosu_mutex_unlock(ethosu_mutex);
Anton Mobergdf386e02021-02-02 11:26:48 +0100729
Jonny Svärdc9644242023-01-09 15:54:24 +0100730 if (!drv)
731 {
732 LOG_ERR("No NPU driver handle available, but semaphore taken");
733 }
Anton Mobergdf386e02021-02-02 11:26:48 +0100734
735 return drv;
736}
737
Anton Moberg61da4d32020-12-22 16:00:31 +0100738void ethosu_release_driver(struct ethosu_driver *drv)
739{
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100740 ethosu_mutex_lock(ethosu_mutex);
Anton Moberg61da4d32020-12-22 16:00:31 +0100741 if (drv != NULL && drv->reserved)
742 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100743 if (drv->job.state == ETHOSU_JOB_RUNNING || drv->job.state == ETHOSU_JOB_DONE)
744 {
745 // Give the inference one shot to complete or force kill the job
746 if (ethosu_wait(drv, false) == 1)
747 {
748 // Still running, soft reset the NPU and reset driver
Jonny Svärd301399d2022-04-26 18:31:24 +0200749 drv->power_request_counter = 0;
750 ethosu_soft_reset(drv);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100751 ethosu_reset_job(drv);
752 drv->status_error = false;
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100753 }
754 }
755
Anton Moberg61da4d32020-12-22 16:00:31 +0100756 drv->reserved = false;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100757 LOG_DEBUG("NPU driver handle %p released", drv);
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100758 ethosu_semaphore_give(ethosu_semaphore);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100759 }
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100760 ethosu_mutex_unlock(ethosu_mutex);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100761}