blob: ee9d08c1555465aadbf96ae61853df5ed638b24a [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +01002 * Copyright (c) 2019-2022 Arm Limited. All rights reserved.
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"
Bhavik Pateldae5be02020-06-18 15:25:15 +020024#include "ethosu_config.h"
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020025#include "ethosu_device.h"
Anton Moberg6eab40b2021-07-07 11:43:51 +020026#include "ethosu_log.h"
Per Åstrand25d78c02020-04-21 14:19:44 +020027
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020028#include <assert.h>
Per Åstrand25d78c02020-04-21 14:19:44 +020029#include <cmsis_compiler.h>
Per Åstrand14ccfee2020-09-25 10:40:20 +020030#include <inttypes.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020031#include <stdbool.h>
Bhavik Patelbf7ae632020-06-11 21:00:16 +020032#include <stddef.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020033#include <stdio.h>
34#include <stdlib.h>
35
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020036/******************************************************************************
37 * Defines
38 ******************************************************************************/
39
Jonny Svärd136810f2021-10-13 16:04:26 +020040#define UNUSED(x) ((void)x)
41
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020042#define BYTES_IN_32_BITS 4
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020043#define MASK_16_BYTE_ALIGN (0xF)
Jonny Svärd136810f2021-10-13 16:04:26 +020044#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 Jonsson2b201c32020-09-02 16:42:43 +020048#define FAST_MEMORY_BASE_ADDR_INDEX 2
49
50/******************************************************************************
51 * Types
52 ******************************************************************************/
53
54// Driver actions
55enum DRIVER_ACTION_e
56{
57 RESERVED = 0,
58 OPTIMIZER_CONFIG = 1,
59 COMMAND_STREAM = 2,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020060 NOP = 5,
61};
62
Jonny Svärd136810f2021-10-13 16:04:26 +020063// Custom operator payload data struct
64struct cop_data_s
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020065{
66 union
67 {
68 // Driver action data
69 struct
70 {
Jonny Svärd136810f2021-10-13 16:04:26 +020071 uint8_t driver_action_command; // (valid values in DRIVER_ACTION_e)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020072 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 Jonsson2b201c32020-09-02 16:42:43 +020091 uint16_t driver_action_data;
92 };
93 };
94
95 uint32_t word;
96 };
97};
98
99// optimizer config struct
100struct opt_cfg_s
101{
Jonny Svärd136810f2021-10-13 16:04:26 +0200102 struct cop_data_s da_data;
103 uint32_t cfg;
104 uint32_t id;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200105};
106
107/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +0200108 * Variables
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200109 ******************************************************************************/
110
Anton Moberg61da4d32020-12-22 16:00:31 +0100111// Registered drivers linked list HEAD
112static struct ethosu_driver *registered_drivers = NULL;
113
Jonny Svärda830f172021-06-07 16:57:00 +0200114/******************************************************************************
115 * Weak functions - Cache
116 *
117 * Default NOP operations. Override if available on the targeted device.
118 ******************************************************************************/
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100119
Jonny Svärda830f172021-06-07 16:57:00 +0200120/*
121 * Flush/clean the data cache by address and size. Passing NULL as p argument
122 * expects the whole cache to be flushed.
123 */
124void __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 */
134void __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 Mobergdfed5fd2021-03-11 14:41:11 +0100148struct ethosu_semaphore_t
149{
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100150 uint8_t count;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100151};
152
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100153static void *ethosu_mutex;
154static void *ethosu_semaphore;
155
Anton Moberg9f346ab2021-05-21 17:20:21 +0200156void *__attribute__((weak)) ethosu_mutex_create(void)
157{
158 return NULL;
159}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100160
Jonny Svärd136810f2021-10-13 16:04:26 +0200161void __attribute__((weak)) ethosu_mutex_destroy(void *mutex)
162{
163 UNUSED(mutex);
164}
165
Ledion Dajac6505f32022-04-20 09:55:21 +0200166int __attribute__((weak)) ethosu_mutex_lock(void *mutex)
Anton Moberg61ec36b2021-04-30 17:10:48 +0200167{
168 UNUSED(mutex);
Ledion Dajac6505f32022-04-20 09:55:21 +0200169 return 0;
Anton Moberg61ec36b2021-04-30 17:10:48 +0200170}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100171
Ledion Dajac6505f32022-04-20 09:55:21 +0200172int __attribute__((weak)) ethosu_mutex_unlock(void *mutex)
Anton Moberg61ec36b2021-04-30 17:10:48 +0200173{
174 UNUSED(mutex);
Ledion Dajac6505f32022-04-20 09:55:21 +0200175 return 0;
Anton Moberg61ec36b2021-04-30 17:10:48 +0200176}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100177
178// Baremetal implementation of creating a semaphore
179void *__attribute__((weak)) ethosu_semaphore_create(void)
180{
181 struct ethosu_semaphore_t *sem = malloc(sizeof(*sem));
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100182 sem->count = 0;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100183 return sem;
184}
185
Jonny Svärd136810f2021-10-13 16:04:26 +0200186void __attribute__((weak)) ethosu_semaphore_destroy(void *sem)
187{
188 free((struct ethosu_semaphore_t *)sem);
189}
190
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100191// Baremetal simulation of waiting/sleeping for and then taking a semaphore using intrisics
Ledion Dajac6505f32022-04-20 09:55:21 +0200192int __attribute__((weak)) ethosu_semaphore_take(void *sem)
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100193{
194 struct ethosu_semaphore_t *s = sem;
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100195 while (s->count == 0)
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100196 {
197 __WFE();
198 }
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100199 s->count = 0;
Ledion Dajac6505f32022-04-20 09:55:21 +0200200 return 0;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100201}
202
203// Baremetal simulation of giving a semaphore and waking up processes using intrinsics
Ledion Dajac6505f32022-04-20 09:55:21 +0200204int __attribute__((weak)) ethosu_semaphore_give(void *sem)
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100205{
206 struct ethosu_semaphore_t *s = sem;
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100207 s->count = 1;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100208 __SEV();
Ledion Dajac6505f32022-04-20 09:55:21 +0200209 return 0;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100210}
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100211
Jonny Svärda830f172021-06-07 16:57:00 +0200212/******************************************************************************
213 * Weak functions - Inference begin/end callbacks
214 ******************************************************************************/
Anton Moberg61da4d32020-12-22 16:00:31 +0100215
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100216void __attribute__((weak)) ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200217{
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100218 UNUSED(user_arg);
Jonny Svärda830f172021-06-07 16:57:00 +0200219 UNUSED(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200220}
221
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100222void __attribute__((weak)) ethosu_inference_end(struct ethosu_driver *drv, void *user_arg)
Jonny Svärda830f172021-06-07 16:57:00 +0200223{
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100224 UNUSED(user_arg);
Jonny Svärda830f172021-06-07 16:57:00 +0200225 UNUSED(drv);
226}
227
228/******************************************************************************
229 * Static functions
230 ******************************************************************************/
Jonny Svärda830f172021-06-07 16:57:00 +0200231static void ethosu_register_driver(struct ethosu_driver *drv)
Jens Elofsson04961a42021-04-08 18:51:38 +0200232{
Jonny Svärda830f172021-06-07 16:57:00 +0200233 // Register driver as new HEAD of list
234 drv->next = registered_drivers;
235 registered_drivers = drv;
236
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100237 LOG_INFO("New NPU driver registered (handle: 0x%p, NPU: 0x%p)", drv, drv->dev->reg);
Jens Elofsson04961a42021-04-08 18:51:38 +0200238}
239
Jonny Svärda830f172021-06-07 16:57:00 +0200240static int ethosu_deregister_driver(struct ethosu_driver *drv)
241{
242 struct ethosu_driver *cur = registered_drivers;
243 struct ethosu_driver **prev = &registered_drivers;
244
245 while (cur != NULL)
246 {
247 if (cur == drv)
248 {
249 *prev = cur->next;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100250 LOG_INFO("NPU driver handle %p deregistered.", drv);
Jonny Svärda830f172021-06-07 16:57:00 +0200251 return 0;
252 }
253
254 prev = &cur->next;
255 cur = cur->next;
256 }
257
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100258 LOG_ERR("No NPU driver handle registered at address %p.", drv);
Jonny Svärda830f172021-06-07 16:57:00 +0200259
260 return -1;
261}
262
263static struct ethosu_driver *ethosu_find_and_reserve_driver(void)
264{
265 struct ethosu_driver *drv = registered_drivers;
266
267 while (drv != NULL)
268 {
269 if (!drv->reserved)
270 {
271 drv->reserved = true;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100272 LOG_DEBUG("NPU driver handle %p reserved.", drv);
Jonny Svärda830f172021-06-07 16:57:00 +0200273 return drv;
274 }
275 drv = drv->next;
276 }
277
Jonny Svärd20ce37f2021-12-17 17:00:57 +0100278 LOG_WARN("No NPU driver handle available.");
Jonny Svärda830f172021-06-07 16:57:00 +0200279
280 return NULL;
281}
282
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100283static void ethosu_reset_job(struct ethosu_driver *drv)
284{
285 memset(&drv->job, 0, sizeof(struct ethosu_job));
286}
287
Jonny Svärd136810f2021-10-13 16:04:26 +0200288static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p)
Jonny Svärda830f172021-06-07 16:57:00 +0200289{
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100290 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 +0200291
Jonny Svärd136810f2021-10-13 16:04:26 +0200292 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 +0200293 {
294 return -1;
295 }
296
Jonny Svärda830f172021-06-07 16:57:00 +0200297 return 0;
298}
299
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100300static 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 +0200301{
Jonny Svärda830f172021-06-07 16:57:00 +0200302 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
303 ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream;
304
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100305 LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d", cmd_stream, cms_length);
Jonny Svärda830f172021-06-07 16:57:00 +0200306
307 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
308 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100309 LOG_ERR("Command stream addr %p not aligned to 16 bytes", cmd_stream);
Jonny Svärda830f172021-06-07 16:57:00 +0200310 return -1;
311 }
312
Jonny Svärd136810f2021-10-13 16:04:26 +0200313 // Verify 16 byte alignment for base address'
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100314 for (int i = 0; i < drv->job.num_base_addr; i++)
Jonny Svärda830f172021-06-07 16:57:00 +0200315 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100316 if (0 != (drv->job.base_addr[i] & MASK_16_BYTE_ALIGN))
Jonny Svärda830f172021-06-07 16:57:00 +0200317 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100318 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 +0200319 return -1;
Jonny Svärda830f172021-06-07 16:57:00 +0200320 }
321 }
322
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100323 // Flush the cache if available on CPU.
324 // The upcasting to uin32_t* is ok since the pointer never is dereferenced.
325 // The base_addr_size is null if invoking from prior to invoke_V2, in that case
326 // the whole cache is being flushed.
327
328 if (drv->job.base_addr_size != NULL)
Jonny Svärda830f172021-06-07 16:57:00 +0200329 {
330 ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100331 for (int i = 0; i < drv->job.num_base_addr; i++)
Jonny Svärda830f172021-06-07 16:57:00 +0200332 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100333 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 +0200334 }
335 }
336 else
337 {
338 ethosu_flush_dcache(NULL, 0);
339 }
340
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100341 // Request power gating disabled during inference run
Jonny Svärd301399d2022-04-26 18:31:24 +0200342 if (!ethosu_request_power(drv))
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100343 {
Jonny Svärd301399d2022-04-26 18:31:24 +0200344 LOG_ERR("Failed to request power");
345 return -1;
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100346 }
347
Jonny Svärd301399d2022-04-26 18:31:24 +0200348 drv->job.state = ETHOSU_JOB_RUNNING;
349
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100350 // Inference begin callback
351 ethosu_inference_begin(drv, drv->job.user_arg);
352
Jonny Svärd136810f2021-10-13 16:04:26 +0200353 // Execute the command stream
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100354 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 +0200355
Jonny Svärda830f172021-06-07 16:57:00 +0200356 return 0;
357}
358
359/******************************************************************************
360 * Weak functions - Interrupt handler
361 ******************************************************************************/
362void __attribute__((weak)) ethosu_irq_handler(struct ethosu_driver *drv)
363{
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100364 LOG_DEBUG("Got interrupt from Ethos-U");
Jonny Svärda830f172021-06-07 16:57:00 +0200365
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100366 drv->job.state = ETHOSU_JOB_DONE;
Jonny Svärd136810f2021-10-13 16:04:26 +0200367 if (!ethosu_dev_handle_interrupt(drv->dev))
Jonny Svärda830f172021-06-07 16:57:00 +0200368 {
Jonny Svärda830f172021-06-07 16:57:00 +0200369 drv->status_error = true;
370 }
Ledion Dajac6505f32022-04-20 09:55:21 +0200371 /* TODO: feedback needed aout how to handle error (-1) return value */
Jonny Svärda830f172021-06-07 16:57:00 +0200372 ethosu_semaphore_give(drv->semaphore);
373}
374
375/******************************************************************************
376 * Functions API
377 ******************************************************************************/
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200378
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200379int 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 Jonsson49bdee82020-04-06 13:21:21 +0200385{
Jonny Svärda830f172021-06-07 16:57:00 +0200386 LOG_INFO("Initializing NPU: base_address=%p, fast_memory=%p, fast_memory_size=%zu, secure=%" PRIu32
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100387 ", privileged=%" PRIu32,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200388 base_address,
389 fast_memory,
Per Åstrande6498f02020-11-09 15:33:12 +0100390 fast_memory_size,
391 secure_enable,
392 privilege_enable);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200393
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100394 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
Jonny Svärd301399d2022-04-26 18:31:24 +0200404 drv->fast_memory = (uint32_t)fast_memory;
405 drv->fast_memory_size = fast_memory_size;
406 drv->power_request_counter = 0;
Anton Moberg61da4d32020-12-22 16:00:31 +0100407
Jonny Svärd136810f2021-10-13 16:04:26 +0200408 // Initialize the device and set requested security state and privilege mode
409 drv->dev = ethosu_dev_init(base_address, secure_enable, privilege_enable);
410
411 if (drv->dev == NULL)
Bhavik Pateldae5be02020-06-18 15:25:15 +0200412 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100413 LOG_ERR("Failed to initialize Ethos-U device");
Bhavik Pateldae5be02020-06-18 15:25:15 +0200414 return -1;
415 }
416
Jonny Svärd136810f2021-10-13 16:04:26 +0200417 drv->semaphore = ethosu_semaphore_create();
Anton Moberg61da4d32020-12-22 16:00:31 +0100418 drv->status_error = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200419
Jonny Svärd301399d2022-04-26 18:31:24 +0200420 ethosu_reset_job(drv);
421
Jonny Svärda830f172021-06-07 16:57:00 +0200422 ethosu_register_driver(drv);
423
Jonny Svärd136810f2021-10-13 16:04:26 +0200424 return 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200425}
426
Anton Moberg547ca532021-06-14 09:43:53 +0200427void ethosu_deinit(struct ethosu_driver *drv)
428{
429 ethosu_deregister_driver(drv);
Jonny Svärd136810f2021-10-13 16:04:26 +0200430 ethosu_semaphore_destroy(drv->semaphore);
431 ethosu_dev_deinit(drv->dev);
432 drv->dev = NULL;
Anton Moberg547ca532021-06-14 09:43:53 +0200433}
434
Jonny Svärd301399d2022-04-26 18:31:24 +0200435bool ethosu_soft_reset(struct ethosu_driver *drv)
436{
437 // Soft reset the NPU
438 if (ethosu_dev_soft_reset(drv->dev) != ETHOSU_SUCCESS)
439 {
440 LOG_ERR("Failed to soft-reset NPU");
441 return false;
442 }
443
444 // Update power and clock gating after the soft reset
445 ethosu_dev_set_clock_and_power(drv->dev,
446 drv->power_request_counter > 0 ? ETHOSU_CLOCK_Q_DISABLE : ETHOSU_CLOCK_Q_ENABLE,
447 drv->power_request_counter > 0 ? ETHOSU_POWER_Q_DISABLE : ETHOSU_POWER_Q_ENABLE);
448
449 return true;
450}
451
452bool ethosu_request_power(struct ethosu_driver *drv)
453{
454 // Check if this is the first power request, increase counter
455 if (drv->power_request_counter++ == 0)
456 {
457 // Always reset to a known state. Changes to requested
458 // security state/privilege mode if necessary.
459 if (ethosu_soft_reset(drv) == false)
460 {
461 LOG_ERR("Failed to request power for Ethos-U");
462 drv->power_request_counter--;
463 return false;
464 }
465 }
466 return true;
467}
468
469void ethosu_release_power(struct ethosu_driver *drv)
470{
471 if (drv->power_request_counter == 0)
472 {
473 LOG_WARN("No power request left to release, reference counter is 0");
474 }
475 else
476 {
477 // Decrement ref counter and enable power gating if no requests remain
478 if (--drv->power_request_counter == 0)
479 {
480 ethosu_dev_set_clock_and_power(drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
481 }
482 }
483}
484
Jonny Svärda830f172021-06-07 16:57:00 +0200485void ethosu_get_driver_version(struct ethosu_driver_version *ver)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200486{
Jonny Svärda830f172021-06-07 16:57:00 +0200487 assert(ver != NULL);
488 ver->major = ETHOSU_DRIVER_VERSION_MAJOR;
489 ver->minor = ETHOSU_DRIVER_VERSION_MINOR;
490 ver->patch = ETHOSU_DRIVER_VERSION_PATCH;
491}
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200492
Jonny Svärda830f172021-06-07 16:57:00 +0200493void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw)
494{
495 assert(hw != NULL);
Jonny Svärd136810f2021-10-13 16:04:26 +0200496 ethosu_dev_get_hw_info(drv->dev, hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200497}
498
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100499int ethosu_wait(struct ethosu_driver *drv, bool block)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200500{
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100501 int ret = 0;
502
503 switch (drv->job.state)
504 {
505 case ETHOSU_JOB_IDLE:
506 LOG_ERR("Inference job not running...");
507 ret = -2;
508 break;
509 case ETHOSU_JOB_RUNNING:
510 if (!block)
511 {
512 // Inference still running, do not block
513 ret = 1;
514 break;
515 }
516 // fall through
517 case ETHOSU_JOB_DONE:
518 // Wait for interrupt in blocking mode. In non-blocking mode
519 // the interrupt has already triggered
Ledion Dajac6505f32022-04-20 09:55:21 +0200520 /* TODO: feedback needed aout how to handle error (-1) return value */
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100521 ethosu_semaphore_take(drv->semaphore);
522
523 // Inference done callback
524 ethosu_inference_end(drv, drv->job.user_arg);
525
Jonny Svärd301399d2022-04-26 18:31:24 +0200526 // Relase power gating disabled requirement
527 ethosu_release_power(drv);
528
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100529 // Check NPU and interrupt status
530 if (drv->status_error)
531 {
532 LOG_ERR("NPU error(s) occured during inference.");
533 ethosu_dev_print_err_status(drv->dev);
534
535 // Reset the NPU
Jonny Svärd301399d2022-04-26 18:31:24 +0200536 (void)ethosu_soft_reset(drv);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100537 // NPU is no longer in error state
538 drv->status_error = false;
539
540 ret = -1;
541 }
542
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100543 if (ret == 0)
544 {
545 // Invalidate cache
546 if (drv->job.base_addr_size != NULL)
547 {
548 for (int i = 0; i < drv->job.num_base_addr; i++)
549 {
550 ethosu_invalidate_dcache((uint32_t *)(uintptr_t)drv->job.base_addr[i], drv->job.base_addr_size[i]);
551 }
552 }
553 else
554 {
555 ethosu_invalidate_dcache(NULL, 0);
556 }
557
558 LOG_DEBUG("Inference finished successfully...");
559 }
560
561 // Reset internal job (state resets to IDLE)
562 ethosu_reset_job(drv);
563 break;
564
565 default:
566 LOG_ERR("Unexpected job state");
567 ethosu_reset_job(drv);
568 ret = -1;
569 break;
570 }
571
572 // Return inference job status
573 return ret;
574}
575
576int ethosu_invoke_async(struct ethosu_driver *drv,
577 const void *custom_data_ptr,
578 const int custom_data_size,
579 const uint64_t *base_addr,
580 const size_t *base_addr_size,
581 const int num_base_addr,
582 void *user_arg)
583{
584
Jonny Svärd136810f2021-10-13 16:04:26 +0200585 const struct cop_data_s *data_ptr = custom_data_ptr;
Kristofer Jonsson24455ee2022-02-08 13:33:22 +0100586 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 +0100587
588 // Make sure an inference is not already running
589 if (drv->job.state != ETHOSU_JOB_IDLE)
590 {
591 LOG_ERR("Inference already running, or waiting to be cleared...");
592 return -1;
593 }
594
595 drv->job.state = ETHOSU_JOB_IDLE;
596 drv->job.custom_data_ptr = custom_data_ptr;
597 drv->job.custom_data_size = custom_data_size;
598 drv->job.base_addr = base_addr;
599 drv->job.base_addr_size = base_addr_size;
600 drv->job.num_base_addr = num_base_addr;
601 drv->job.user_arg = user_arg;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200602
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200603 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200604 if (data_ptr->word != ETHOSU_FOURCC)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200605 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100606 LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x", data_ptr->word, ETHOSU_FOURCC);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100607 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200608 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200609
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200610 // Custom data length must be a multiple of 32 bits
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200611 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
612 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100613 LOG_ERR("custom_data_size=0x%x not a multiple of 4", custom_data_size);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100614 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200615 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200616
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100617 data_ptr++;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200618
619 // Adjust base address to fast memory area
Anton Moberg61da4d32020-12-22 16:00:31 +0100620 if (drv->fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200621 {
622 uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX];
623
Anton Moberg61da4d32020-12-22 16:00:31 +0100624 if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > drv->fast_memory_size)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200625 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100626 LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u",
Anton Moberg61da4d32020-12-22 16:00:31 +0100627 drv->fast_memory_size,
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100628 base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100629 goto err;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200630 }
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100631
Anton Moberg61da4d32020-12-22 16:00:31 +0100632 *fast_memory = drv->fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200633 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200634
Anton Moberg61da4d32020-12-22 16:00:31 +0100635 drv->status_error = false;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200636
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100637 // Parse Custom Operator Payload data
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200638 while (data_ptr < data_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200639 {
640 switch (data_ptr->driver_action_command)
641 {
642 case OPTIMIZER_CONFIG:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100643 LOG_DEBUG("OPTIMIZER_CONFIG");
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200644 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
645
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100646 if (handle_optimizer_config(drv, opt_cfg_p) < 0)
647 {
648 goto err;
649 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200650 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
651 break;
652 case COMMAND_STREAM:
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100653 // Vela only supports putting one COMMAND_STREAM per op
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100654 LOG_DEBUG("COMMAND_STREAM");
Jonny Svärd136810f2021-10-13 16:04:26 +0200655 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct cop_data_s);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200656 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
657
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100658 if (handle_command_stream(drv, command_stream, cms_length) < 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200659 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100660 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200661 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200662 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
663 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200664 case NOP:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100665 LOG_DEBUG("NOP");
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200666 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
667 break;
668 default:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100669 LOG_ERR("UNSUPPORTED driver_action_command: %d", data_ptr->driver_action_command);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100670 goto err;
Bhavik Patele645fed2020-06-12 14:46:47 +0200671 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200672 }
673 }
Jonny Svärd136810f2021-10-13 16:04:26 +0200674
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100675 return 0;
676err:
677 LOG_ERR("Failed to invoke inference.");
678 ethosu_reset_job(drv);
679 return -1;
680}
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200681
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100682int ethosu_invoke_v3(struct ethosu_driver *drv,
683 const void *custom_data_ptr,
684 const int custom_data_size,
685 const uint64_t *base_addr,
686 const size_t *base_addr_size,
687 const int num_base_addr,
688 void *user_arg)
689{
690 if (ethosu_invoke_async(
691 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 +0200692 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100693 return -1;
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200694 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200695
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100696 return ethosu_wait(drv, true);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200697}
698
Anton Moberg61da4d32020-12-22 16:00:31 +0100699struct ethosu_driver *ethosu_reserve_driver(void)
700{
Anton Mobergdf386e02021-02-02 11:26:48 +0100701 struct ethosu_driver *drv = NULL;
702
703 do
704 {
Ledion Dajac6505f32022-04-20 09:55:21 +0200705 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100706 ethosu_mutex_lock(ethosu_mutex);
Anton Mobergdf386e02021-02-02 11:26:48 +0100707 drv = ethosu_find_and_reserve_driver();
Ledion Dajac6505f32022-04-20 09:55:21 +0200708 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100709 ethosu_mutex_unlock(ethosu_mutex);
Anton Mobergdf386e02021-02-02 11:26:48 +0100710
711 if (drv != NULL)
712 {
713 break;
714 }
715
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100716 LOG_INFO("Waiting for NPU driver handle to become available...");
Ledion Dajac6505f32022-04-20 09:55:21 +0200717 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100718 ethosu_semaphore_take(ethosu_semaphore);
Anton Mobergdf386e02021-02-02 11:26:48 +0100719
720 } while (1);
721
722 return drv;
723}
724
Anton Moberg61da4d32020-12-22 16:00:31 +0100725void ethosu_release_driver(struct ethosu_driver *drv)
726{
Ledion Dajac6505f32022-04-20 09:55:21 +0200727 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100728 ethosu_mutex_lock(ethosu_mutex);
Anton Moberg61da4d32020-12-22 16:00:31 +0100729 if (drv != NULL && drv->reserved)
730 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100731 if (drv->job.state == ETHOSU_JOB_RUNNING || drv->job.state == ETHOSU_JOB_DONE)
732 {
733 // Give the inference one shot to complete or force kill the job
734 if (ethosu_wait(drv, false) == 1)
735 {
736 // Still running, soft reset the NPU and reset driver
Jonny Svärd301399d2022-04-26 18:31:24 +0200737 drv->power_request_counter = 0;
738 ethosu_soft_reset(drv);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100739 ethosu_reset_job(drv);
740 drv->status_error = false;
Ledion Dajac6505f32022-04-20 09:55:21 +0200741 /* TODO: feedback needed aout how to handle error (-1) return value */
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100742 ethosu_semaphore_give(drv->semaphore);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100743 }
744 }
745
Anton Moberg61da4d32020-12-22 16:00:31 +0100746 drv->reserved = false;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100747 LOG_DEBUG("NPU driver handle %p released", drv);
Ledion Dajac6505f32022-04-20 09:55:21 +0200748 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100749 ethosu_semaphore_give(ethosu_semaphore);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100750 }
Ledion Dajac6505f32022-04-20 09:55:21 +0200751 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100752 ethosu_mutex_unlock(ethosu_mutex);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100753}