blob: 7d55500b41e7e1cd55ae51783c391b03f3ed0aa0 [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
Ledion Dajaecdce6c2023-01-16 15:39:29 +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 Jonsson3c439172020-08-05 09:38:40 +020019#ifndef ETHOSU_DRIVER_H
20#define ETHOSU_DRIVER_H
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020021
22/******************************************************************************
23 * Includes
24 ******************************************************************************/
25
Jonny Svärd136810f2021-10-13 16:04:26 +020026#include "ethosu_types.h"
Bhavik Pateldae5be02020-06-18 15:25:15 +020027
28#include <stdbool.h>
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020029#include <stddef.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020030#include <stdint.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +020037 * Defines
38 ******************************************************************************/
39
40#define ETHOSU_DRIVER_VERSION_MAJOR 0 ///< Driver major version
41#define ETHOSU_DRIVER_VERSION_MINOR 16 ///< Driver minor version
42#define ETHOSU_DRIVER_VERSION_PATCH 0 ///< Driver patch version
43
Jonny Svärda2732ec2023-12-18 17:19:15 +010044#define ETHOSU_SEMAPHORE_WAIT_FOREVER (UINT64_MAX)
45
46#ifndef ETHOSU_SEMAPHORE_WAIT_INFERENCE
47#define ETHOSU_SEMAPHORE_WAIT_INFERENCE ETHOSU_SEMAPHORE_WAIT_FOREVER
48#endif
49
Jonny Svärda830f172021-06-07 16:57:00 +020050/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020051 * Types
52 ******************************************************************************/
53
Jonny Svärd136810f2021-10-13 16:04:26 +020054// Forward declare
55struct ethosu_device;
56
Jonny Svärd1a3bb922022-02-25 16:28:21 +010057enum ethosu_job_state
58{
59 ETHOSU_JOB_IDLE = 0,
60 ETHOSU_JOB_RUNNING,
61 ETHOSU_JOB_DONE
62};
63
Jonny Svärda2732ec2023-12-18 17:19:15 +010064enum ethosu_job_result
65{
66 ETHOSU_JOB_RESULT_OK = 0,
67 ETHOSU_JOB_RESULT_TIMEOUT,
68 ETHOSU_JOB_RESULT_ERROR
69};
70
Jonny Svärd1a3bb922022-02-25 16:28:21 +010071struct ethosu_job
72{
73 volatile enum ethosu_job_state state;
Jonny Svärda2732ec2023-12-18 17:19:15 +010074 volatile enum ethosu_job_result result;
Jonny Svärd1a3bb922022-02-25 16:28:21 +010075 const void *custom_data_ptr;
76 int custom_data_size;
77 const uint64_t *base_addr;
78 const size_t *base_addr_size;
79 int num_base_addr;
80 void *user_arg;
81};
82
Bhavik Pateldae5be02020-06-18 15:25:15 +020083struct ethosu_driver
84{
Jonny Svärd136810f2021-10-13 16:04:26 +020085 struct ethosu_device *dev;
Jonny Svärda830f172021-06-07 16:57:00 +020086 struct ethosu_driver *next;
Jonny Svärd1a3bb922022-02-25 16:28:21 +010087 struct ethosu_job job;
Jonny Svärda830f172021-06-07 16:57:00 +020088 void *semaphore;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020089 uint64_t fast_memory;
90 size_t fast_memory_size;
Jonny Svärd301399d2022-04-26 18:31:24 +020091 uint32_t power_request_counter;
Anton Moberg61da4d32020-12-22 16:00:31 +010092 bool reserved;
Bhavik Pateldae5be02020-06-18 15:25:15 +020093};
94
Jonny Svärda830f172021-06-07 16:57:00 +020095struct ethosu_driver_version
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020096{
Jonny Svärda830f172021-06-07 16:57:00 +020097 uint8_t major;
98 uint8_t minor;
99 uint8_t patch;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200100};
101
Anton Moberg0a614292021-03-24 14:08:22 +0100102enum ethosu_request_clients
103{
104 ETHOSU_PMU_REQUEST = 0,
105 ETHOSU_INFERENCE_REQUEST = 1,
106};
107
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200108/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +0200109 * Prototypes (weak functions in driver)
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200110 ******************************************************************************/
111
Jonny Svärda830f172021-06-07 16:57:00 +0200112/**
113 * Interrupt handler to be called on IRQ from Ethos-U
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100114 *
115 * @param drv Pointer to driver handle
Jonny Svärda830f172021-06-07 16:57:00 +0200116 */
117void ethosu_irq_handler(struct ethosu_driver *drv);
118
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100119/**
Jonny Svärda830f172021-06-07 16:57:00 +0200120 * Flush/clean the data cache by address and size. Passing NULL as p argument
121 * expects the whole cache to be flushed.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100122 *
123 * Addresses passed to this function must be 16 byte aligned.
124 *
125 * @param p 16 byte aligned address
126 * @param bytes Size of memory block in bytes
Jonny Svärda830f172021-06-07 16:57:00 +0200127 */
Jonny Svärda830f172021-06-07 16:57:00 +0200128void ethosu_flush_dcache(uint32_t *p, size_t bytes);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100129
130/**
Jonny Svärda830f172021-06-07 16:57:00 +0200131 * Invalidate the data cache by address and size. Passing NULL as p argument
132 * expects the whole cache to be invalidated.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100133 *
134 * Addresses passed to this function must be 16 byte aligned.
135 *
136 * @param p 16 byte aligned address
137 * @param bytes Size in bytes
Jonny Svärda830f172021-06-07 16:57:00 +0200138 */
139void ethosu_invalidate_dcache(uint32_t *p, size_t bytes);
140
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100141/**
142 * Minimal mutex implementation for baremetal applications. See
Jonny Svärda830f172021-06-07 16:57:00 +0200143 * ethosu_driver.c.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100144 *
145 * @return Pointer to mutex handle
Jonny Svärda830f172021-06-07 16:57:00 +0200146 */
147void *ethosu_mutex_create(void);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100148
149/**
150 * Minimal sempahore implementation for baremetal applications. See
151 * ethosu_driver.c.
152 *
153 * @return Pointer to semaphore handle
154 */
Jonny Svärda830f172021-06-07 16:57:00 +0200155void *ethosu_semaphore_create(void);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100156
157/**
158 * Lock mutex.
159 *
160 * @param mutex Pointer to mutex handle
161 * @returns 0 on success, else negative error code
Ledion Dajac6505f32022-04-20 09:55:21 +0200162 */
163int ethosu_mutex_lock(void *mutex);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100164
165/**
166 * Unlock mutex.
167 *
168 * @param mutex Pointer to mutex handle
169 * @returns 0 on success, else negative error code
170 */
Ledion Dajac6505f32022-04-20 09:55:21 +0200171int ethosu_mutex_unlock(void *mutex);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100172
173/**
174 * Take semaphore.
175 *
176 * @param sem Pointer to semaphore handle
Jonny Svärda2732ec2023-12-18 17:19:15 +0100177 * @param timeout Timeout value (unit impl. defined)
178 * @returns 0 on success else negative error code
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100179 */
Jonny Svärda2732ec2023-12-18 17:19:15 +0100180int ethosu_semaphore_take(void *sem, uint64_t timeout);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100181
182/**
183 * Give semaphore.
184 *
185 * @param sem Pointer to semaphore handle
186 * @returns 0 on success, else negative error code
187 */
Ledion Dajac6505f32022-04-20 09:55:21 +0200188int ethosu_semaphore_give(void *sem);
Jonny Svärda830f172021-06-07 16:57:00 +0200189
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100190/**
191 * Callback invoked just before the inference is started.
192 *
193 * @param drv Pointer to driver handle
194 * @param user_arg User argument provided to ethosu_invoke_*()
Jonny Svärda830f172021-06-07 16:57:00 +0200195 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100196void ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100197
198/**
199 * Callback invoked just after the inference has completed.
200 *
201 * @param drv Pointer to driver handle
202 * @param user_arg User argument provided to ethosu_invoke_*()
203 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100204void ethosu_inference_end(struct ethosu_driver *drv, void *user_arg);
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200205
Kristofer Jonsson96703322022-11-04 15:54:32 +0100206/**
207 * Remapping command stream and base pointer addresses.
208 *
209 * @param address Address to be remapped.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100210 * @param index -1 command stream, 0-n base address index
Kristofer Jonsson96703322022-11-04 15:54:32 +0100211 *
212 * @return Remapped address
213 */
214uint64_t ethosu_address_remap(uint64_t address, int index);
215
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200216/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200217 * Prototypes
218 ******************************************************************************/
219
220/**
221 * Initialize the Ethos-U driver.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100222 *
223 * @param drv Pointer to driver handle
224 * @param base_address NPU register base address
225 * @param fast_memory Fast memory area, used for Ethos-U65 with spilling
226 * @param fast_memory_size Size in bytes of fast memory area
227 * @param secure_enable Configure NPU in secure- or non-secure mode
228 * @param privilege_enable Configure NPU in privileged- or non-privileged mode
229 * @return 0 on success, else negative error code
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200230 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200231int ethosu_init(struct ethosu_driver *drv,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100232 void *const base_address,
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200233 const void *fast_memory,
234 const size_t fast_memory_size,
235 uint32_t secure_enable,
236 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200237
238/**
Anton Moberg547ca532021-06-14 09:43:53 +0200239 * Deinitialize the Ethos-U driver.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100240 *
241 * @param drv Pointer to driver handle
Anton Moberg547ca532021-06-14 09:43:53 +0200242 */
243void ethosu_deinit(struct ethosu_driver *drv);
244
245/**
Jonny Svärd301399d2022-04-26 18:31:24 +0200246 * Soft resets the Ethos-U device.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100247 *
248 * @param drv Pointer to driver handle
249 * @return 0 on success, else negative error code
Jonny Svärd301399d2022-04-26 18:31:24 +0200250 */
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100251int ethosu_soft_reset(struct ethosu_driver *drv);
Jonny Svärd301399d2022-04-26 18:31:24 +0200252
253/**
254 * Request to disable Q-channel power gating of the Ethos-U device.
255 * Power requests are ref.counted. Increases count.
256 * (Note: clock gating is made to follow power gating)
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100257 *
258 * @param drv Pointer to driver handle
259 * @return 0 on success, else negative error code
Jonny Svärd301399d2022-04-26 18:31:24 +0200260 */
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100261int ethosu_request_power(struct ethosu_driver *drv);
Jonny Svärd301399d2022-04-26 18:31:24 +0200262
263/**
264 * Release disable request for Q-channel power gating of the Ethos-U device.
265 * Power requests are ref.counted. Decreases count.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100266 *
267 * @param drv Pointer to driver handle
Jonny Svärd301399d2022-04-26 18:31:24 +0200268 */
269void ethosu_release_power(struct ethosu_driver *drv);
270
271/**
Jonny Svärda830f172021-06-07 16:57:00 +0200272 * Get Ethos-U driver version.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100273 *
274 * @param ver Driver version struct
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200275 */
Jonny Svärda830f172021-06-07 16:57:00 +0200276void ethosu_get_driver_version(struct ethosu_driver_version *ver);
277
278/**
279 * Get Ethos-U hardware information.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100280 *
281 * @param drv Pointer to driver handle
282 * @param hw Hardware information struct
Jonny Svärda830f172021-06-07 16:57:00 +0200283 */
284void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200285
286/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100287 * Invoke command stream.
288 *
289 * @param drv Pointer to driver handle
290 * @param custom_data_ptr Custom data payload
291 * @param custom_data_size Size in bytes of custom data
292 * @param base_addr Array of base address pointers
293 * @param base_addr_size Size in bytes of each address in base_addr
294 * @param num_base_addr Number of elements in base_addr array
295 * @param user_arg User argument, will be passed to
296 * ethosu_inference_begin() and ethosu_inference_end()
297 * @return 0 on success, else negative error code
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200298 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100299int ethosu_invoke_v3(struct ethosu_driver *drv,
300 const void *custom_data_ptr,
301 const int custom_data_size,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100302 uint64_t *const base_addr,
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100303 const size_t *base_addr_size,
304 const int num_base_addr,
305 void *user_arg);
306
307#define ethosu_invoke(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr) \
308 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 +0200309
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200310/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100311 * Invoke command stream using async interface.
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100312 * Must be followed by call(s) to ethosu_wait() upon successful return.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100313 *
314 * @see ethosu_invoke_v3 for documentation.
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100315 */
316int ethosu_invoke_async(struct ethosu_driver *drv,
317 const void *custom_data_ptr,
318 const int custom_data_size,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100319 uint64_t *const base_addr,
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100320 const size_t *base_addr_size,
321 const int num_base_addr,
322 void *user_arg);
323
324/**
325 * Wait for inference to complete (block=true)
326 * Poll status or finish up if inference is complete (block=false)
327 * (This function is only intended to be used in conjuction with ethosu_invoke_async)
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100328 *
329 * @param drv Pointer to driver handle
330 * @param block If call should block if inference is running
331 * @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 +0100332 */
333int ethosu_wait(struct ethosu_driver *drv, bool block);
334
335/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100336 * Reserves a driver to execute inference with. Call will block until a driver
337 * is available.
338 *
339 * @return Pointer to driver handle.
Anton Moberg61da4d32020-12-22 16:00:31 +0100340 */
341struct ethosu_driver *ethosu_reserve_driver(void);
342
343/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100344 * Release driver that was previously reserved with @see ethosu_reserve_driver.
345 *
346 * @param drv Pointer to driver handle
Anton Moberg61da4d32020-12-22 16:00:31 +0100347 */
348void ethosu_release_driver(struct ethosu_driver *drv);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100349
Anton Mobergdf386e02021-02-02 11:26:48 +0100350/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100351 * Static inline for backwards-compatibility.
352 *
353 * @see ethosu_invoke_v3 for documentation.
Kristofer Jonssonbe0bc462021-06-28 11:44:42 +0000354 */
355static inline int ethosu_invoke_v2(const void *custom_data_ptr,
356 const int custom_data_size,
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100357 uint64_t *const base_addr,
Kristofer Jonssonbe0bc462021-06-28 11:44:42 +0000358 const size_t *base_addr_size,
359 const int num_base_addr)
360{
361 struct ethosu_driver *drv = ethosu_reserve_driver();
Ledion Dajaecdce6c2023-01-16 15:39:29 +0100362 if (!drv)
363 {
364 return -1;
365 }
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100366 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 +0000367 ethosu_release_driver(drv);
368 return result;
369}
370
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200371#ifdef __cplusplus
372}
373#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200374
375#endif // ETHOSU_DRIVER_H