blob: e2d3f5ba9f3bf871dd5d7d002463cf99ee8ebaa8 [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
Jonny Svärd4140d662024-04-17 18:02:15 +02002 * SPDX-FileCopyrightText: Copyright 2019-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the License); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Kristofer Jonsson3c439172020-08-05 09:38:40 +020018#ifndef ETHOSU_DRIVER_H
19#define ETHOSU_DRIVER_H
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020020
21/******************************************************************************
22 * Includes
23 ******************************************************************************/
24
Jonny Svärd136810f2021-10-13 16:04:26 +020025#include "ethosu_types.h"
Bhavik Pateldae5be02020-06-18 15:25:15 +020026
27#include <stdbool.h>
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020028#include <stddef.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020029#include <stdint.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +020036 * Defines
37 ******************************************************************************/
38
39#define ETHOSU_DRIVER_VERSION_MAJOR 0 ///< Driver major version
40#define ETHOSU_DRIVER_VERSION_MINOR 16 ///< Driver minor version
41#define ETHOSU_DRIVER_VERSION_PATCH 0 ///< Driver patch version
42
Jonny Svärda2732ec2023-12-18 17:19:15 +010043#define ETHOSU_SEMAPHORE_WAIT_FOREVER (UINT64_MAX)
44
45#ifndef ETHOSU_SEMAPHORE_WAIT_INFERENCE
46#define ETHOSU_SEMAPHORE_WAIT_INFERENCE ETHOSU_SEMAPHORE_WAIT_FOREVER
47#endif
48
Jonny Svärda830f172021-06-07 16:57:00 +020049/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020050 * Types
51 ******************************************************************************/
52
Jonny Svärd136810f2021-10-13 16:04:26 +020053// Forward declare
54struct ethosu_device;
55
Jonny Svärd1a3bb922022-02-25 16:28:21 +010056enum ethosu_job_state
57{
58 ETHOSU_JOB_IDLE = 0,
59 ETHOSU_JOB_RUNNING,
60 ETHOSU_JOB_DONE
61};
62
Jonny Svärda2732ec2023-12-18 17:19:15 +010063enum ethosu_job_result
64{
65 ETHOSU_JOB_RESULT_OK = 0,
66 ETHOSU_JOB_RESULT_TIMEOUT,
67 ETHOSU_JOB_RESULT_ERROR
68};
69
Jonny Svärd1a3bb922022-02-25 16:28:21 +010070struct ethosu_job
71{
72 volatile enum ethosu_job_state state;
Jonny Svärda2732ec2023-12-18 17:19:15 +010073 volatile enum ethosu_job_result result;
Jonny Svärd1a3bb922022-02-25 16:28:21 +010074 const void *custom_data_ptr;
75 int custom_data_size;
76 const uint64_t *base_addr;
77 const size_t *base_addr_size;
78 int num_base_addr;
79 void *user_arg;
80};
81
Bhavik Pateldae5be02020-06-18 15:25:15 +020082struct ethosu_driver
83{
Jonny Svärd136810f2021-10-13 16:04:26 +020084 struct ethosu_device *dev;
Jonny Svärda830f172021-06-07 16:57:00 +020085 struct ethosu_driver *next;
Jonny Svärd1a3bb922022-02-25 16:28:21 +010086 struct ethosu_job job;
Jonny Svärda830f172021-06-07 16:57:00 +020087 void *semaphore;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020088 uint64_t fast_memory;
89 size_t fast_memory_size;
Jonny Svärd301399d2022-04-26 18:31:24 +020090 uint32_t power_request_counter;
Anton Moberg61da4d32020-12-22 16:00:31 +010091 bool reserved;
Bhavik Pateldae5be02020-06-18 15:25:15 +020092};
93
Jonny Svärda830f172021-06-07 16:57:00 +020094struct ethosu_driver_version
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020095{
Jonny Svärda830f172021-06-07 16:57:00 +020096 uint8_t major;
97 uint8_t minor;
98 uint8_t patch;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020099};
100
Anton Moberg0a614292021-03-24 14:08:22 +0100101enum ethosu_request_clients
102{
103 ETHOSU_PMU_REQUEST = 0,
104 ETHOSU_INFERENCE_REQUEST = 1,
105};
106
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200107/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +0200108 * Prototypes (weak functions in driver)
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200109 ******************************************************************************/
110
Jonny Svärda830f172021-06-07 16:57:00 +0200111/**
112 * Interrupt handler to be called on IRQ from Ethos-U
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100113 *
114 * @param drv Pointer to driver handle
Jonny Svärda830f172021-06-07 16:57:00 +0200115 */
116void ethosu_irq_handler(struct ethosu_driver *drv);
117
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100118/**
Jonny Svärda830f172021-06-07 16:57:00 +0200119 * Flush/clean the data cache by address and size. Passing NULL as p argument
120 * expects the whole cache to be flushed.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100121 *
122 * Addresses passed to this function must be 16 byte aligned.
123 *
124 * @param p 16 byte aligned address
125 * @param bytes Size of memory block in bytes
Jonny Svärda830f172021-06-07 16:57:00 +0200126 */
Jonny Svärda830f172021-06-07 16:57:00 +0200127void ethosu_flush_dcache(uint32_t *p, size_t bytes);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100128
129/**
Jonny Svärda830f172021-06-07 16:57:00 +0200130 * Invalidate the data cache by address and size. Passing NULL as p argument
131 * expects the whole cache to be invalidated.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100132 *
133 * Addresses passed to this function must be 16 byte aligned.
134 *
135 * @param p 16 byte aligned address
136 * @param bytes Size in bytes
Jonny Svärda830f172021-06-07 16:57:00 +0200137 */
138void ethosu_invalidate_dcache(uint32_t *p, size_t bytes);
139
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100140/**
141 * Minimal mutex implementation for baremetal applications. See
Jonny Svärda830f172021-06-07 16:57:00 +0200142 * ethosu_driver.c.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100143 *
144 * @return Pointer to mutex handle
Jonny Svärda830f172021-06-07 16:57:00 +0200145 */
146void *ethosu_mutex_create(void);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100147
148/**
Jonny Svärd4140d662024-04-17 18:02:15 +0200149 * Destroy mutex.
150 *
151 * @param mutex Pointer to mutex handle
152 */
153void ethosu_mutex_destroy(void *mutex);
154
155/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100156 * Minimal sempahore implementation for baremetal applications. See
157 * ethosu_driver.c.
158 *
159 * @return Pointer to semaphore handle
160 */
Jonny Svärda830f172021-06-07 16:57:00 +0200161void *ethosu_semaphore_create(void);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100162
163/**
Jonny Svärd4140d662024-04-17 18:02:15 +0200164 * Destroy semaphore.
165 *
166 * @param sem Pointer to semaphore handle
167 */
168void ethosu_semaphore_destroy(void *sem);
169
170/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100171 * Lock mutex.
172 *
173 * @param mutex Pointer to mutex handle
174 * @returns 0 on success, else negative error code
Ledion Dajac6505f32022-04-20 09:55:21 +0200175 */
176int ethosu_mutex_lock(void *mutex);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100177
178/**
179 * Unlock mutex.
180 *
181 * @param mutex Pointer to mutex handle
182 * @returns 0 on success, else negative error code
183 */
Ledion Dajac6505f32022-04-20 09:55:21 +0200184int ethosu_mutex_unlock(void *mutex);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100185
186/**
187 * Take semaphore.
188 *
189 * @param sem Pointer to semaphore handle
Jonny Svärda2732ec2023-12-18 17:19:15 +0100190 * @param timeout Timeout value (unit impl. defined)
191 * @returns 0 on success else negative error code
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100192 */
Jonny Svärda2732ec2023-12-18 17:19:15 +0100193int ethosu_semaphore_take(void *sem, uint64_t timeout);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100194
195/**
196 * Give semaphore.
197 *
198 * @param sem Pointer to semaphore handle
199 * @returns 0 on success, else negative error code
200 */
Ledion Dajac6505f32022-04-20 09:55:21 +0200201int ethosu_semaphore_give(void *sem);
Jonny Svärda830f172021-06-07 16:57:00 +0200202
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100203/**
204 * Callback invoked just before the inference is started.
205 *
206 * @param drv Pointer to driver handle
207 * @param user_arg User argument provided to ethosu_invoke_*()
Jonny Svärda830f172021-06-07 16:57:00 +0200208 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100209void ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100210
211/**
212 * Callback invoked just after the inference has completed.
213 *
214 * @param drv Pointer to driver handle
215 * @param user_arg User argument provided to ethosu_invoke_*()
216 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100217void ethosu_inference_end(struct ethosu_driver *drv, void *user_arg);
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200218
Kristofer Jonsson96703322022-11-04 15:54:32 +0100219/**
220 * Remapping command stream and base pointer addresses.
221 *
222 * @param address Address to be remapped.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100223 * @param index -1 command stream, 0-n base address index
Kristofer Jonsson96703322022-11-04 15:54:32 +0100224 *
225 * @return Remapped address
226 */
227uint64_t ethosu_address_remap(uint64_t address, int index);
228
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200229/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200230 * Prototypes
231 ******************************************************************************/
232
233/**
234 * Initialize the Ethos-U driver.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100235 *
236 * @param drv Pointer to driver handle
237 * @param base_address NPU register base address
238 * @param fast_memory Fast memory area, used for Ethos-U65 with spilling
239 * @param fast_memory_size Size in bytes of fast memory area
240 * @param secure_enable Configure NPU in secure- or non-secure mode
241 * @param privilege_enable Configure NPU in privileged- or non-privileged mode
242 * @return 0 on success, else negative error code
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200243 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200244int ethosu_init(struct ethosu_driver *drv,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100245 void *const base_address,
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200246 const void *fast_memory,
247 const size_t fast_memory_size,
248 uint32_t secure_enable,
249 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200250
251/**
Anton Moberg547ca532021-06-14 09:43:53 +0200252 * Deinitialize the Ethos-U driver.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100253 *
254 * @param drv Pointer to driver handle
Anton Moberg547ca532021-06-14 09:43:53 +0200255 */
256void ethosu_deinit(struct ethosu_driver *drv);
257
258/**
Jonny Svärd301399d2022-04-26 18:31:24 +0200259 * Soft resets the Ethos-U device.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100260 *
261 * @param drv Pointer to driver handle
262 * @return 0 on success, else negative error code
Jonny Svärd301399d2022-04-26 18:31:24 +0200263 */
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100264int ethosu_soft_reset(struct ethosu_driver *drv);
Jonny Svärd301399d2022-04-26 18:31:24 +0200265
266/**
267 * Request to disable Q-channel power gating of the Ethos-U device.
268 * Power requests are ref.counted. Increases count.
269 * (Note: clock gating is made to follow power gating)
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100270 *
271 * @param drv Pointer to driver handle
272 * @return 0 on success, else negative error code
Jonny Svärd301399d2022-04-26 18:31:24 +0200273 */
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100274int ethosu_request_power(struct ethosu_driver *drv);
Jonny Svärd301399d2022-04-26 18:31:24 +0200275
276/**
277 * Release disable request for Q-channel power gating of the Ethos-U device.
278 * Power requests are ref.counted. Decreases count.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100279 *
280 * @param drv Pointer to driver handle
Jonny Svärd301399d2022-04-26 18:31:24 +0200281 */
282void ethosu_release_power(struct ethosu_driver *drv);
283
284/**
Jonny Svärda830f172021-06-07 16:57:00 +0200285 * Get Ethos-U driver version.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100286 *
287 * @param ver Driver version struct
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200288 */
Jonny Svärda830f172021-06-07 16:57:00 +0200289void ethosu_get_driver_version(struct ethosu_driver_version *ver);
290
291/**
292 * Get Ethos-U hardware information.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100293 *
294 * @param drv Pointer to driver handle
295 * @param hw Hardware information struct
Jonny Svärda830f172021-06-07 16:57:00 +0200296 */
297void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200298
299/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100300 * Invoke command stream.
301 *
302 * @param drv Pointer to driver handle
303 * @param custom_data_ptr Custom data payload
304 * @param custom_data_size Size in bytes of custom data
305 * @param base_addr Array of base address pointers
306 * @param base_addr_size Size in bytes of each address in base_addr
307 * @param num_base_addr Number of elements in base_addr array
308 * @param user_arg User argument, will be passed to
309 * ethosu_inference_begin() and ethosu_inference_end()
310 * @return 0 on success, else negative error code
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200311 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100312int ethosu_invoke_v3(struct ethosu_driver *drv,
313 const void *custom_data_ptr,
314 const int custom_data_size,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100315 uint64_t *const base_addr,
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100316 const size_t *base_addr_size,
317 const int num_base_addr,
318 void *user_arg);
319
320#define ethosu_invoke(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr) \
321 ethosu_invoke_v3(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr, 0)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200322
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200323/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100324 * Invoke command stream using async interface.
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100325 * Must be followed by call(s) to ethosu_wait() upon successful return.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100326 *
327 * @see ethosu_invoke_v3 for documentation.
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100328 */
329int ethosu_invoke_async(struct ethosu_driver *drv,
330 const void *custom_data_ptr,
331 const int custom_data_size,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100332 uint64_t *const base_addr,
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100333 const size_t *base_addr_size,
334 const int num_base_addr,
335 void *user_arg);
336
337/**
338 * Wait for inference to complete (block=true)
339 * Poll status or finish up if inference is complete (block=false)
340 * (This function is only intended to be used in conjuction with ethosu_invoke_async)
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100341 *
342 * @param drv Pointer to driver handle
343 * @param block If call should block if inference is running
344 * @return -2 on inference not invoked, -1 on inference error, 0 on success, 1 on inference running
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100345 */
346int ethosu_wait(struct ethosu_driver *drv, bool block);
347
348/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100349 * Reserves a driver to execute inference with. Call will block until a driver
350 * is available.
351 *
352 * @return Pointer to driver handle.
Anton Moberg61da4d32020-12-22 16:00:31 +0100353 */
354struct ethosu_driver *ethosu_reserve_driver(void);
355
356/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100357 * Release driver that was previously reserved with @see ethosu_reserve_driver.
358 *
359 * @param drv Pointer to driver handle
Anton Moberg61da4d32020-12-22 16:00:31 +0100360 */
361void ethosu_release_driver(struct ethosu_driver *drv);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100362
Anton Mobergdf386e02021-02-02 11:26:48 +0100363/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100364 * Static inline for backwards-compatibility.
365 *
366 * @see ethosu_invoke_v3 for documentation.
Kristofer Jonssonbe0bc462021-06-28 11:44:42 +0000367 */
368static inline int ethosu_invoke_v2(const void *custom_data_ptr,
369 const int custom_data_size,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100370 uint64_t *const base_addr,
Kristofer Jonssonbe0bc462021-06-28 11:44:42 +0000371 const size_t *base_addr_size,
372 const int num_base_addr)
373{
374 struct ethosu_driver *drv = ethosu_reserve_driver();
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100375 if (!drv)
376 {
377 return -1;
378 }
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100379 int result = ethosu_invoke_v3(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr, 0);
Kristofer Jonssonbe0bc462021-06-28 11:44:42 +0000380 ethosu_release_driver(drv);
381 return result;
382}
383
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200384#ifdef __cplusplus
385}
386#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200387
388#endif // ETHOSU_DRIVER_H