blob: 77a015c5eb89a19455f5d4b94f9b91b5f7233ee8 [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 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
44/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020045 * Types
46 ******************************************************************************/
47
Jonny Svärd136810f2021-10-13 16:04:26 +020048// Forward declare
49struct ethosu_device;
50
Jonny Svärd1a3bb922022-02-25 16:28:21 +010051enum ethosu_job_state
52{
53 ETHOSU_JOB_IDLE = 0,
54 ETHOSU_JOB_RUNNING,
55 ETHOSU_JOB_DONE
56};
57
58struct ethosu_job
59{
60 volatile enum ethosu_job_state state;
61 const void *custom_data_ptr;
62 int custom_data_size;
63 const uint64_t *base_addr;
64 const size_t *base_addr_size;
65 int num_base_addr;
66 void *user_arg;
67};
68
Bhavik Pateldae5be02020-06-18 15:25:15 +020069struct ethosu_driver
70{
Jonny Svärd136810f2021-10-13 16:04:26 +020071 struct ethosu_device *dev;
Jonny Svärda830f172021-06-07 16:57:00 +020072 struct ethosu_driver *next;
Jonny Svärd1a3bb922022-02-25 16:28:21 +010073 struct ethosu_job job;
Jonny Svärda830f172021-06-07 16:57:00 +020074 void *semaphore;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020075 uint64_t fast_memory;
76 size_t fast_memory_size;
Jonny Svärd301399d2022-04-26 18:31:24 +020077 uint32_t power_request_counter;
Bhavik Patel5f8dad12020-09-30 09:06:52 +020078 bool status_error;
Anton Moberg61da4d32020-12-22 16:00:31 +010079 bool reserved;
Bhavik Pateldae5be02020-06-18 15:25:15 +020080};
81
Jonny Svärda830f172021-06-07 16:57:00 +020082struct ethosu_driver_version
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020083{
Jonny Svärda830f172021-06-07 16:57:00 +020084 uint8_t major;
85 uint8_t minor;
86 uint8_t patch;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020087};
88
Anton Moberg0a614292021-03-24 14:08:22 +010089enum ethosu_request_clients
90{
91 ETHOSU_PMU_REQUEST = 0,
92 ETHOSU_INFERENCE_REQUEST = 1,
93};
94
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020095/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +020096 * Prototypes (weak functions in driver)
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020097 ******************************************************************************/
98
Jonny Svärda830f172021-06-07 16:57:00 +020099/**
100 * Interrupt handler to be called on IRQ from Ethos-U
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100101 *
102 * @param drv Pointer to driver handle
Jonny Svärda830f172021-06-07 16:57:00 +0200103 */
104void ethosu_irq_handler(struct ethosu_driver *drv);
105
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100106/**
Jonny Svärda830f172021-06-07 16:57:00 +0200107 * Flush/clean the data cache by address and size. Passing NULL as p argument
108 * expects the whole cache to be flushed.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100109 *
110 * Addresses passed to this function must be 16 byte aligned.
111 *
112 * @param p 16 byte aligned address
113 * @param bytes Size of memory block in bytes
Jonny Svärda830f172021-06-07 16:57:00 +0200114 */
Jonny Svärda830f172021-06-07 16:57:00 +0200115void ethosu_flush_dcache(uint32_t *p, size_t bytes);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100116
117/**
Jonny Svärda830f172021-06-07 16:57:00 +0200118 * Invalidate the data cache by address and size. Passing NULL as p argument
119 * expects the whole cache to be invalidated.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100120 *
121 * Addresses passed to this function must be 16 byte aligned.
122 *
123 * @param p 16 byte aligned address
124 * @param bytes Size in bytes
Jonny Svärda830f172021-06-07 16:57:00 +0200125 */
126void ethosu_invalidate_dcache(uint32_t *p, size_t bytes);
127
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100128/**
129 * Minimal mutex implementation for baremetal applications. See
Jonny Svärda830f172021-06-07 16:57:00 +0200130 * ethosu_driver.c.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100131 *
132 * @return Pointer to mutex handle
Jonny Svärda830f172021-06-07 16:57:00 +0200133 */
134void *ethosu_mutex_create(void);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100135
136/**
137 * Minimal sempahore implementation for baremetal applications. See
138 * ethosu_driver.c.
139 *
140 * @return Pointer to semaphore handle
141 */
Jonny Svärda830f172021-06-07 16:57:00 +0200142void *ethosu_semaphore_create(void);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100143
144/**
145 * Lock mutex.
146 *
147 * @param mutex Pointer to mutex handle
148 * @returns 0 on success, else negative error code
Ledion Dajac6505f32022-04-20 09:55:21 +0200149 */
150int ethosu_mutex_lock(void *mutex);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100151
152/**
153 * Unlock mutex.
154 *
155 * @param mutex Pointer to mutex handle
156 * @returns 0 on success, else negative error code
157 */
Ledion Dajac6505f32022-04-20 09:55:21 +0200158int ethosu_mutex_unlock(void *mutex);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100159
160/**
161 * Take semaphore.
162 *
163 * @param sem Pointer to semaphore handle
164 * @returns 0 on success, else negative error code
165 */
Ledion Dajac6505f32022-04-20 09:55:21 +0200166int ethosu_semaphore_take(void *sem);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100167
168/**
169 * Give semaphore.
170 *
171 * @param sem Pointer to semaphore handle
172 * @returns 0 on success, else negative error code
173 */
Ledion Dajac6505f32022-04-20 09:55:21 +0200174int ethosu_semaphore_give(void *sem);
Jonny Svärda830f172021-06-07 16:57:00 +0200175
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100176/**
177 * Callback invoked just before the inference is started.
178 *
179 * @param drv Pointer to driver handle
180 * @param user_arg User argument provided to ethosu_invoke_*()
Jonny Svärda830f172021-06-07 16:57:00 +0200181 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100182void ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg);
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100183
184/**
185 * Callback invoked just after the inference has completed.
186 *
187 * @param drv Pointer to driver handle
188 * @param user_arg User argument provided to ethosu_invoke_*()
189 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100190void ethosu_inference_end(struct ethosu_driver *drv, void *user_arg);
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200191
Kristofer Jonsson96703322022-11-04 15:54:32 +0100192/**
193 * Remapping command stream and base pointer addresses.
194 *
195 * @param address Address to be remapped.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100196 * @param index -1 command stream, 0-n base address index
Kristofer Jonsson96703322022-11-04 15:54:32 +0100197 *
198 * @return Remapped address
199 */
200uint64_t ethosu_address_remap(uint64_t address, int index);
201
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200202/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200203 * Prototypes
204 ******************************************************************************/
205
206/**
207 * Initialize the Ethos-U driver.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100208 *
209 * @param drv Pointer to driver handle
210 * @param base_address NPU register base address
211 * @param fast_memory Fast memory area, used for Ethos-U65 with spilling
212 * @param fast_memory_size Size in bytes of fast memory area
213 * @param secure_enable Configure NPU in secure- or non-secure mode
214 * @param privilege_enable Configure NPU in privileged- or non-privileged mode
215 * @return 0 on success, else negative error code
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200216 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200217int ethosu_init(struct ethosu_driver *drv,
218 const void *base_address,
219 const void *fast_memory,
220 const size_t fast_memory_size,
221 uint32_t secure_enable,
222 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200223
224/**
Anton Moberg547ca532021-06-14 09:43:53 +0200225 * Deinitialize the Ethos-U driver.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100226 *
227 * @param drv Pointer to driver handle
Anton Moberg547ca532021-06-14 09:43:53 +0200228 */
229void ethosu_deinit(struct ethosu_driver *drv);
230
231/**
Jonny Svärd301399d2022-04-26 18:31:24 +0200232 * Soft resets the Ethos-U device.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100233 *
234 * @param drv Pointer to driver handle
235 * @return 0 on success, else negative error code
Jonny Svärd301399d2022-04-26 18:31:24 +0200236 */
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100237int ethosu_soft_reset(struct ethosu_driver *drv);
Jonny Svärd301399d2022-04-26 18:31:24 +0200238
239/**
240 * Request to disable Q-channel power gating of the Ethos-U device.
241 * Power requests are ref.counted. Increases count.
242 * (Note: clock gating is made to follow power gating)
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100243 *
244 * @param drv Pointer to driver handle
245 * @return 0 on success, else negative error code
Jonny Svärd301399d2022-04-26 18:31:24 +0200246 */
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100247int ethosu_request_power(struct ethosu_driver *drv);
Jonny Svärd301399d2022-04-26 18:31:24 +0200248
249/**
250 * Release disable request for Q-channel power gating of the Ethos-U device.
251 * Power requests are ref.counted. Decreases count.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100252 *
253 * @param drv Pointer to driver handle
Jonny Svärd301399d2022-04-26 18:31:24 +0200254 */
255void ethosu_release_power(struct ethosu_driver *drv);
256
257/**
Jonny Svärda830f172021-06-07 16:57:00 +0200258 * Get Ethos-U driver version.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100259 *
260 * @param ver Driver version struct
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200261 */
Jonny Svärda830f172021-06-07 16:57:00 +0200262void ethosu_get_driver_version(struct ethosu_driver_version *ver);
263
264/**
265 * Get Ethos-U hardware information.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100266 *
267 * @param drv Pointer to driver handle
268 * @param hw Hardware information struct
Jonny Svärda830f172021-06-07 16:57:00 +0200269 */
270void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200271
272/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100273 * Invoke command stream.
274 *
275 * @param drv Pointer to driver handle
276 * @param custom_data_ptr Custom data payload
277 * @param custom_data_size Size in bytes of custom data
278 * @param base_addr Array of base address pointers
279 * @param base_addr_size Size in bytes of each address in base_addr
280 * @param num_base_addr Number of elements in base_addr array
281 * @param user_arg User argument, will be passed to
282 * ethosu_inference_begin() and ethosu_inference_end()
283 * @return 0 on success, else negative error code
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200284 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100285int ethosu_invoke_v3(struct ethosu_driver *drv,
286 const void *custom_data_ptr,
287 const int custom_data_size,
288 const uint64_t *base_addr,
289 const size_t *base_addr_size,
290 const int num_base_addr,
291 void *user_arg);
292
293#define ethosu_invoke(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr) \
294 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 +0200295
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200296/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100297 * Invoke command stream using async interface.
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100298 * Must be followed by call(s) to ethosu_wait() upon successful return.
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100299 *
300 * @see ethosu_invoke_v3 for documentation.
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100301 */
302int ethosu_invoke_async(struct ethosu_driver *drv,
303 const void *custom_data_ptr,
304 const int custom_data_size,
305 const uint64_t *base_addr,
306 const size_t *base_addr_size,
307 const int num_base_addr,
308 void *user_arg);
309
310/**
311 * Wait for inference to complete (block=true)
312 * Poll status or finish up if inference is complete (block=false)
313 * (This function is only intended to be used in conjuction with ethosu_invoke_async)
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100314 *
315 * @param drv Pointer to driver handle
316 * @param block If call should block if inference is running
317 * @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 +0100318 */
319int ethosu_wait(struct ethosu_driver *drv, bool block);
320
321/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100322 * Reserves a driver to execute inference with. Call will block until a driver
323 * is available.
324 *
325 * @return Pointer to driver handle.
Anton Moberg61da4d32020-12-22 16:00:31 +0100326 */
327struct ethosu_driver *ethosu_reserve_driver(void);
328
329/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100330 * Release driver that was previously reserved with @see ethosu_reserve_driver.
331 *
332 * @param drv Pointer to driver handle
Anton Moberg61da4d32020-12-22 16:00:31 +0100333 */
334void ethosu_release_driver(struct ethosu_driver *drv);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100335
Anton Mobergdf386e02021-02-02 11:26:48 +0100336/**
Kristofer Jonsson68b5c912022-11-14 10:52:38 +0100337 * Static inline for backwards-compatibility.
338 *
339 * @see ethosu_invoke_v3 for documentation.
Kristofer Jonssonbe0bc462021-06-28 11:44:42 +0000340 */
341static inline int ethosu_invoke_v2(const void *custom_data_ptr,
342 const int custom_data_size,
343 const uint64_t *base_addr,
344 const size_t *base_addr_size,
345 const int num_base_addr)
346{
347 struct ethosu_driver *drv = ethosu_reserve_driver();
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100348 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 +0000349 ethosu_release_driver(drv);
350 return result;
351}
352
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200353#ifdef __cplusplus
354}
355#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200356
357#endif // ETHOSU_DRIVER_H