blob: 11c51f685e1a62ea18df10c9acebcd88df14080f [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"
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ärd1a3bb922022-02-25 16:28:21 +0100204 s->count = 0;
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ärd1a3bb922022-02-25 16:28:21 +0100212 s->count = 1;
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ärda830f172021-06-07 16:57:00 +0200238 // Register driver as new HEAD of list
239 drv->next = registered_drivers;
240 registered_drivers = drv;
241
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100242 LOG_INFO("New NPU driver registered (handle: 0x%p, NPU: 0x%p)", drv, drv->dev->reg);
Jens Elofsson04961a42021-04-08 18:51:38 +0200243}
244
Jonny Svärda830f172021-06-07 16:57:00 +0200245static int ethosu_deregister_driver(struct ethosu_driver *drv)
246{
247 struct ethosu_driver *cur = registered_drivers;
248 struct ethosu_driver **prev = &registered_drivers;
249
250 while (cur != NULL)
251 {
252 if (cur == drv)
253 {
254 *prev = cur->next;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100255 LOG_INFO("NPU driver handle %p deregistered.", drv);
Jonny Svärda830f172021-06-07 16:57:00 +0200256 return 0;
257 }
258
259 prev = &cur->next;
260 cur = cur->next;
261 }
262
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100263 LOG_ERR("No NPU driver handle registered at address %p.", drv);
Jonny Svärda830f172021-06-07 16:57:00 +0200264
265 return -1;
266}
267
268static struct ethosu_driver *ethosu_find_and_reserve_driver(void)
269{
270 struct ethosu_driver *drv = registered_drivers;
271
272 while (drv != NULL)
273 {
274 if (!drv->reserved)
275 {
276 drv->reserved = true;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100277 LOG_DEBUG("NPU driver handle %p reserved.", drv);
Jonny Svärda830f172021-06-07 16:57:00 +0200278 return drv;
279 }
280 drv = drv->next;
281 }
282
Jonny Svärd20ce37f2021-12-17 17:00:57 +0100283 LOG_WARN("No NPU driver handle available.");
Jonny Svärda830f172021-06-07 16:57:00 +0200284
285 return NULL;
286}
287
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100288static void ethosu_reset_job(struct ethosu_driver *drv)
289{
290 memset(&drv->job, 0, sizeof(struct ethosu_job));
291}
292
Jonny Svärd136810f2021-10-13 16:04:26 +0200293static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p)
Jonny Svärda830f172021-06-07 16:57:00 +0200294{
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100295 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 +0200296
Jonny Svärd136810f2021-10-13 16:04:26 +0200297 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 +0200298 {
299 return -1;
300 }
301
Jonny Svärda830f172021-06-07 16:57:00 +0200302 return 0;
303}
304
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100305static 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 +0200306{
Jonny Svärda830f172021-06-07 16:57:00 +0200307 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
308 ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream;
309
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100310 LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d", cmd_stream, cms_length);
Jonny Svärda830f172021-06-07 16:57:00 +0200311
312 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
313 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100314 LOG_ERR("Command stream addr %p not aligned to 16 bytes", cmd_stream);
Jonny Svärda830f172021-06-07 16:57:00 +0200315 return -1;
316 }
317
Jonny Svärd136810f2021-10-13 16:04:26 +0200318 // Verify 16 byte alignment for base address'
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100319 for (int i = 0; i < drv->job.num_base_addr; i++)
Jonny Svärda830f172021-06-07 16:57:00 +0200320 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100321 if (0 != (drv->job.base_addr[i] & MASK_16_BYTE_ALIGN))
Jonny Svärda830f172021-06-07 16:57:00 +0200322 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100323 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 +0200324 return -1;
Jonny Svärda830f172021-06-07 16:57:00 +0200325 }
326 }
327
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100328 // Flush the cache if available on CPU.
329 // The upcasting to uin32_t* is ok since the pointer never is dereferenced.
330 // The base_addr_size is null if invoking from prior to invoke_V2, in that case
331 // the whole cache is being flushed.
332
333 if (drv->job.base_addr_size != NULL)
Jonny Svärda830f172021-06-07 16:57:00 +0200334 {
335 ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100336 for (int i = 0; i < drv->job.num_base_addr; i++)
Jonny Svärda830f172021-06-07 16:57:00 +0200337 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100338 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 +0200339 }
340 }
341 else
342 {
343 ethosu_flush_dcache(NULL, 0);
344 }
345
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100346 // Request power gating disabled during inference run
Jonny Svärd301399d2022-04-26 18:31:24 +0200347 if (!ethosu_request_power(drv))
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100348 {
Jonny Svärd301399d2022-04-26 18:31:24 +0200349 LOG_ERR("Failed to request power");
350 return -1;
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100351 }
352
Jonny Svärd301399d2022-04-26 18:31:24 +0200353 drv->job.state = ETHOSU_JOB_RUNNING;
354
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100355 // Inference begin callback
356 ethosu_inference_begin(drv, drv->job.user_arg);
357
Jonny Svärd136810f2021-10-13 16:04:26 +0200358 // Execute the command stream
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100359 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 +0200360
Jonny Svärda830f172021-06-07 16:57:00 +0200361 return 0;
362}
363
364/******************************************************************************
365 * Weak functions - Interrupt handler
366 ******************************************************************************/
367void __attribute__((weak)) ethosu_irq_handler(struct ethosu_driver *drv)
368{
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100369 LOG_DEBUG("Got interrupt from Ethos-U");
Jonny Svärda830f172021-06-07 16:57:00 +0200370
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100371 drv->job.state = ETHOSU_JOB_DONE;
Jonny Svärd136810f2021-10-13 16:04:26 +0200372 if (!ethosu_dev_handle_interrupt(drv->dev))
Jonny Svärda830f172021-06-07 16:57:00 +0200373 {
Jonny Svärda830f172021-06-07 16:57:00 +0200374 drv->status_error = true;
375 }
Ledion Dajac6505f32022-04-20 09:55:21 +0200376 /* TODO: feedback needed aout how to handle error (-1) return value */
Jonny Svärda830f172021-06-07 16:57:00 +0200377 ethosu_semaphore_give(drv->semaphore);
378}
379
380/******************************************************************************
381 * Functions API
382 ******************************************************************************/
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200383
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200384int ethosu_init(struct ethosu_driver *drv,
385 const void *base_address,
386 const void *fast_memory,
387 const size_t fast_memory_size,
388 uint32_t secure_enable,
389 uint32_t privilege_enable)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200390{
Jonny Svärda830f172021-06-07 16:57:00 +0200391 LOG_INFO("Initializing NPU: base_address=%p, fast_memory=%p, fast_memory_size=%zu, secure=%" PRIu32
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100392 ", privileged=%" PRIu32,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200393 base_address,
394 fast_memory,
Per Åstrande6498f02020-11-09 15:33:12 +0100395 fast_memory_size,
396 secure_enable,
397 privilege_enable);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200398
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100399 if (!ethosu_mutex)
400 {
401 ethosu_mutex = ethosu_mutex_create();
402 }
403
404 if (!ethosu_semaphore)
405 {
406 ethosu_semaphore = ethosu_semaphore_create();
407 }
408
Jonny Svärd301399d2022-04-26 18:31:24 +0200409 drv->fast_memory = (uint32_t)fast_memory;
410 drv->fast_memory_size = fast_memory_size;
411 drv->power_request_counter = 0;
Anton Moberg61da4d32020-12-22 16:00:31 +0100412
Jonny Svärd136810f2021-10-13 16:04:26 +0200413 // Initialize the device and set requested security state and privilege mode
414 drv->dev = ethosu_dev_init(base_address, secure_enable, privilege_enable);
415
416 if (drv->dev == NULL)
Bhavik Pateldae5be02020-06-18 15:25:15 +0200417 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100418 LOG_ERR("Failed to initialize Ethos-U device");
Bhavik Pateldae5be02020-06-18 15:25:15 +0200419 return -1;
420 }
421
Jonny Svärd136810f2021-10-13 16:04:26 +0200422 drv->semaphore = ethosu_semaphore_create();
Anton Moberg61da4d32020-12-22 16:00:31 +0100423 drv->status_error = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200424
Jonny Svärd301399d2022-04-26 18:31:24 +0200425 ethosu_reset_job(drv);
426
Jonny Svärda830f172021-06-07 16:57:00 +0200427 ethosu_register_driver(drv);
428
Jonny Svärd136810f2021-10-13 16:04:26 +0200429 return 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200430}
431
Anton Moberg547ca532021-06-14 09:43:53 +0200432void ethosu_deinit(struct ethosu_driver *drv)
433{
434 ethosu_deregister_driver(drv);
Jonny Svärd136810f2021-10-13 16:04:26 +0200435 ethosu_semaphore_destroy(drv->semaphore);
436 ethosu_dev_deinit(drv->dev);
437 drv->dev = NULL;
Anton Moberg547ca532021-06-14 09:43:53 +0200438}
439
Jonny Svärd301399d2022-04-26 18:31:24 +0200440bool ethosu_soft_reset(struct ethosu_driver *drv)
441{
442 // Soft reset the NPU
443 if (ethosu_dev_soft_reset(drv->dev) != ETHOSU_SUCCESS)
444 {
445 LOG_ERR("Failed to soft-reset NPU");
446 return false;
447 }
448
449 // Update power and clock gating after the soft reset
450 ethosu_dev_set_clock_and_power(drv->dev,
451 drv->power_request_counter > 0 ? ETHOSU_CLOCK_Q_DISABLE : ETHOSU_CLOCK_Q_ENABLE,
452 drv->power_request_counter > 0 ? ETHOSU_POWER_Q_DISABLE : ETHOSU_POWER_Q_ENABLE);
453
454 return true;
455}
456
457bool ethosu_request_power(struct ethosu_driver *drv)
458{
459 // Check if this is the first power request, increase counter
460 if (drv->power_request_counter++ == 0)
461 {
462 // Always reset to a known state. Changes to requested
463 // security state/privilege mode if necessary.
464 if (ethosu_soft_reset(drv) == false)
465 {
466 LOG_ERR("Failed to request power for Ethos-U");
467 drv->power_request_counter--;
468 return false;
469 }
470 }
471 return true;
472}
473
474void ethosu_release_power(struct ethosu_driver *drv)
475{
476 if (drv->power_request_counter == 0)
477 {
478 LOG_WARN("No power request left to release, reference counter is 0");
479 }
480 else
481 {
482 // Decrement ref counter and enable power gating if no requests remain
483 if (--drv->power_request_counter == 0)
484 {
485 ethosu_dev_set_clock_and_power(drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
486 }
487 }
488}
489
Jonny Svärda830f172021-06-07 16:57:00 +0200490void ethosu_get_driver_version(struct ethosu_driver_version *ver)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200491{
Jonny Svärda830f172021-06-07 16:57:00 +0200492 assert(ver != NULL);
493 ver->major = ETHOSU_DRIVER_VERSION_MAJOR;
494 ver->minor = ETHOSU_DRIVER_VERSION_MINOR;
495 ver->patch = ETHOSU_DRIVER_VERSION_PATCH;
496}
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200497
Jonny Svärda830f172021-06-07 16:57:00 +0200498void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw)
499{
500 assert(hw != NULL);
Jonny Svärd136810f2021-10-13 16:04:26 +0200501 ethosu_dev_get_hw_info(drv->dev, hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200502}
503
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100504int ethosu_wait(struct ethosu_driver *drv, bool block)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200505{
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100506 int ret = 0;
507
508 switch (drv->job.state)
509 {
510 case ETHOSU_JOB_IDLE:
511 LOG_ERR("Inference job not running...");
512 ret = -2;
513 break;
514 case ETHOSU_JOB_RUNNING:
515 if (!block)
516 {
517 // Inference still running, do not block
518 ret = 1;
519 break;
520 }
521 // fall through
522 case ETHOSU_JOB_DONE:
523 // Wait for interrupt in blocking mode. In non-blocking mode
524 // the interrupt has already triggered
Ledion Dajac6505f32022-04-20 09:55:21 +0200525 /* TODO: feedback needed aout how to handle error (-1) return value */
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100526 ethosu_semaphore_take(drv->semaphore);
527
528 // Inference done callback
529 ethosu_inference_end(drv, drv->job.user_arg);
530
Jonny Svärd301399d2022-04-26 18:31:24 +0200531 // Relase power gating disabled requirement
532 ethosu_release_power(drv);
533
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100534 // Check NPU and interrupt status
535 if (drv->status_error)
536 {
537 LOG_ERR("NPU error(s) occured during inference.");
538 ethosu_dev_print_err_status(drv->dev);
539
540 // Reset the NPU
Jonny Svärd301399d2022-04-26 18:31:24 +0200541 (void)ethosu_soft_reset(drv);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100542 // NPU is no longer in error state
543 drv->status_error = false;
544
545 ret = -1;
546 }
547
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100548 if (ret == 0)
549 {
550 // Invalidate cache
551 if (drv->job.base_addr_size != NULL)
552 {
553 for (int i = 0; i < drv->job.num_base_addr; i++)
554 {
555 ethosu_invalidate_dcache((uint32_t *)(uintptr_t)drv->job.base_addr[i], drv->job.base_addr_size[i]);
556 }
557 }
558 else
559 {
560 ethosu_invalidate_dcache(NULL, 0);
561 }
562
563 LOG_DEBUG("Inference finished successfully...");
564 }
565
566 // Reset internal job (state resets to IDLE)
567 ethosu_reset_job(drv);
568 break;
569
570 default:
571 LOG_ERR("Unexpected job state");
572 ethosu_reset_job(drv);
573 ret = -1;
574 break;
575 }
576
577 // Return inference job status
578 return ret;
579}
580
581int ethosu_invoke_async(struct ethosu_driver *drv,
582 const void *custom_data_ptr,
583 const int custom_data_size,
584 const uint64_t *base_addr,
585 const size_t *base_addr_size,
586 const int num_base_addr,
587 void *user_arg)
588{
589
Jonny Svärd136810f2021-10-13 16:04:26 +0200590 const struct cop_data_s *data_ptr = custom_data_ptr;
Kristofer Jonsson24455ee2022-02-08 13:33:22 +0100591 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 +0100592
593 // Make sure an inference is not already running
594 if (drv->job.state != ETHOSU_JOB_IDLE)
595 {
596 LOG_ERR("Inference already running, or waiting to be cleared...");
597 return -1;
598 }
599
600 drv->job.state = ETHOSU_JOB_IDLE;
601 drv->job.custom_data_ptr = custom_data_ptr;
602 drv->job.custom_data_size = custom_data_size;
603 drv->job.base_addr = base_addr;
604 drv->job.base_addr_size = base_addr_size;
605 drv->job.num_base_addr = num_base_addr;
606 drv->job.user_arg = user_arg;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200607
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200608 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200609 if (data_ptr->word != ETHOSU_FOURCC)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200610 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100611 LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x", data_ptr->word, ETHOSU_FOURCC);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100612 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200613 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200614
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200615 // Custom data length must be a multiple of 32 bits
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200616 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
617 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100618 LOG_ERR("custom_data_size=0x%x not a multiple of 4", custom_data_size);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100619 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200620 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200621
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100622 data_ptr++;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200623
624 // Adjust base address to fast memory area
Anton Moberg61da4d32020-12-22 16:00:31 +0100625 if (drv->fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200626 {
627 uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX];
628
Anton Moberg61da4d32020-12-22 16:00:31 +0100629 if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > drv->fast_memory_size)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200630 {
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100631 LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u",
Anton Moberg61da4d32020-12-22 16:00:31 +0100632 drv->fast_memory_size,
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100633 base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100634 goto err;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200635 }
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100636
Anton Moberg61da4d32020-12-22 16:00:31 +0100637 *fast_memory = drv->fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200638 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200639
Anton Moberg61da4d32020-12-22 16:00:31 +0100640 drv->status_error = false;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200641
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100642 // Parse Custom Operator Payload data
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200643 while (data_ptr < data_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200644 {
645 switch (data_ptr->driver_action_command)
646 {
647 case OPTIMIZER_CONFIG:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100648 LOG_DEBUG("OPTIMIZER_CONFIG");
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200649 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
650
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100651 if (handle_optimizer_config(drv, opt_cfg_p) < 0)
652 {
653 goto err;
654 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200655 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
656 break;
657 case COMMAND_STREAM:
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100658 // Vela only supports putting one COMMAND_STREAM per op
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100659 LOG_DEBUG("COMMAND_STREAM");
Jonny Svärd136810f2021-10-13 16:04:26 +0200660 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct cop_data_s);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200661 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
662
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100663 if (handle_command_stream(drv, command_stream, cms_length) < 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200664 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100665 goto err;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200666 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200667 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
668 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200669 case NOP:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100670 LOG_DEBUG("NOP");
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200671 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
672 break;
673 default:
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100674 LOG_ERR("UNSUPPORTED driver_action_command: %d", data_ptr->driver_action_command);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100675 goto err;
Bhavik Patele645fed2020-06-12 14:46:47 +0200676 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200677 }
678 }
Jonny Svärd136810f2021-10-13 16:04:26 +0200679
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100680 return 0;
681err:
682 LOG_ERR("Failed to invoke inference.");
683 ethosu_reset_job(drv);
684 return -1;
685}
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200686
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100687int ethosu_invoke_v3(struct ethosu_driver *drv,
688 const void *custom_data_ptr,
689 const int custom_data_size,
690 const uint64_t *base_addr,
691 const size_t *base_addr_size,
692 const int num_base_addr,
693 void *user_arg)
694{
695 if (ethosu_invoke_async(
696 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 +0200697 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100698 return -1;
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200699 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200700
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100701 return ethosu_wait(drv, true);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200702}
703
Anton Moberg61da4d32020-12-22 16:00:31 +0100704struct ethosu_driver *ethosu_reserve_driver(void)
705{
Anton Mobergdf386e02021-02-02 11:26:48 +0100706 struct ethosu_driver *drv = NULL;
707
708 do
709 {
Ledion Dajac6505f32022-04-20 09:55:21 +0200710 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100711 ethosu_mutex_lock(ethosu_mutex);
Anton Mobergdf386e02021-02-02 11:26:48 +0100712 drv = ethosu_find_and_reserve_driver();
Ledion Dajac6505f32022-04-20 09:55:21 +0200713 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100714 ethosu_mutex_unlock(ethosu_mutex);
Anton Mobergdf386e02021-02-02 11:26:48 +0100715
716 if (drv != NULL)
717 {
718 break;
719 }
720
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100721 LOG_INFO("Waiting for NPU driver handle to become available...");
Ledion Dajac6505f32022-04-20 09:55:21 +0200722 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100723 ethosu_semaphore_take(ethosu_semaphore);
Anton Mobergdf386e02021-02-02 11:26:48 +0100724
725 } while (1);
726
727 return drv;
728}
729
Anton Moberg61da4d32020-12-22 16:00:31 +0100730void ethosu_release_driver(struct ethosu_driver *drv)
731{
Ledion Dajac6505f32022-04-20 09:55:21 +0200732 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100733 ethosu_mutex_lock(ethosu_mutex);
Anton Moberg61da4d32020-12-22 16:00:31 +0100734 if (drv != NULL && drv->reserved)
735 {
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100736 if (drv->job.state == ETHOSU_JOB_RUNNING || drv->job.state == ETHOSU_JOB_DONE)
737 {
738 // Give the inference one shot to complete or force kill the job
739 if (ethosu_wait(drv, false) == 1)
740 {
741 // Still running, soft reset the NPU and reset driver
Jonny Svärd301399d2022-04-26 18:31:24 +0200742 drv->power_request_counter = 0;
743 ethosu_soft_reset(drv);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100744 ethosu_reset_job(drv);
745 drv->status_error = false;
Ledion Dajac6505f32022-04-20 09:55:21 +0200746 /* TODO: feedback needed aout how to handle error (-1) return value */
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100747 ethosu_semaphore_give(drv->semaphore);
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100748 }
749 }
750
Anton Moberg61da4d32020-12-22 16:00:31 +0100751 drv->reserved = false;
Kristofer Jonsson089a3472021-11-12 12:52:07 +0100752 LOG_DEBUG("NPU driver handle %p released", drv);
Ledion Dajac6505f32022-04-20 09:55:21 +0200753 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100754 ethosu_semaphore_give(ethosu_semaphore);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100755 }
Ledion Dajac6505f32022-04-20 09:55:21 +0200756 /* TODO: feedback needed aout how to handle error (-1) return value */
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100757 ethosu_mutex_unlock(ethosu_mutex);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100758}