blob: 053b529ea3427758ac6b25346ff719c359157794 [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;
Bhavik Patel5f8dad12020-09-30 09:06:52 +020077 bool status_error;
Anton Moberg8d65b6f2020-12-21 09:37:18 +010078 bool dev_power_always_on;
Anton Moberg61da4d32020-12-22 16:00:31 +010079 bool reserved;
Anton Moberg0a614292021-03-24 14:08:22 +010080 uint8_t clock_request;
81 uint8_t power_request;
Bhavik Pateldae5be02020-06-18 15:25:15 +020082};
83
Jonny Svärda830f172021-06-07 16:57:00 +020084struct ethosu_driver_version
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020085{
Jonny Svärda830f172021-06-07 16:57:00 +020086 uint8_t major;
87 uint8_t minor;
88 uint8_t patch;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020089};
90
Anton Moberg0a614292021-03-24 14:08:22 +010091enum ethosu_request_clients
92{
93 ETHOSU_PMU_REQUEST = 0,
94 ETHOSU_INFERENCE_REQUEST = 1,
95};
96
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020097/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +020098 * Prototypes (weak functions in driver)
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020099 ******************************************************************************/
100
Jonny Svärda830f172021-06-07 16:57:00 +0200101/**
102 * Interrupt handler to be called on IRQ from Ethos-U
103 */
104void ethosu_irq_handler(struct ethosu_driver *drv);
105
106/*
107 * Flush/clean the data cache by address and size. Passing NULL as p argument
108 * expects the whole cache to be flushed.
109 */
110
111void ethosu_flush_dcache(uint32_t *p, size_t bytes);
112/*
113 * Invalidate the data cache by address and size. Passing NULL as p argument
114 * expects the whole cache to be invalidated.
115 */
116void ethosu_invalidate_dcache(uint32_t *p, size_t bytes);
117
118/*
119 * Minimal sempahore and mutex implementation for baremetal applications. See
120 * ethosu_driver.c.
121 */
122void *ethosu_mutex_create(void);
Jonny Svärda830f172021-06-07 16:57:00 +0200123void *ethosu_semaphore_create(void);
Ledion Dajac6505f32022-04-20 09:55:21 +0200124/*
125 * Returns:
126 * -1 on error
127 * 0 on success
128 */
129int ethosu_mutex_lock(void *mutex);
130int ethosu_mutex_unlock(void *mutex);
131int ethosu_semaphore_take(void *sem);
132int ethosu_semaphore_give(void *sem);
Jonny Svärda830f172021-06-07 16:57:00 +0200133
134/*
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100135 * Callbacks for begin/end of inference. user_data pointer is passed to the
136 * ethosu_invoke() call and forwarded to the callback functions.
Jonny Svärda830f172021-06-07 16:57:00 +0200137 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100138void ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg);
139void ethosu_inference_end(struct ethosu_driver *drv, void *user_arg);
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200140
141/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200142 * Prototypes
143 ******************************************************************************/
144
145/**
146 * Initialize the Ethos-U driver.
147 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200148int ethosu_init(struct ethosu_driver *drv,
149 const void *base_address,
150 const void *fast_memory,
151 const size_t fast_memory_size,
152 uint32_t secure_enable,
153 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200154
155/**
Anton Moberg547ca532021-06-14 09:43:53 +0200156 * Deinitialize the Ethos-U driver.
157 */
158void ethosu_deinit(struct ethosu_driver *drv);
159
160/**
Jonny Svärda830f172021-06-07 16:57:00 +0200161 * Get Ethos-U driver version.
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200162 */
Jonny Svärda830f172021-06-07 16:57:00 +0200163void ethosu_get_driver_version(struct ethosu_driver_version *ver);
164
165/**
166 * Get Ethos-U hardware information.
167 */
168void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200169
170/**
171 * Invoke Vela command stream.
172 */
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100173int ethosu_invoke_v3(struct ethosu_driver *drv,
174 const void *custom_data_ptr,
175 const int custom_data_size,
176 const uint64_t *base_addr,
177 const size_t *base_addr_size,
178 const int num_base_addr,
179 void *user_arg);
180
181#define ethosu_invoke(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr) \
182 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 +0200183
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200184/**
Jonny Svärd1a3bb922022-02-25 16:28:21 +0100185 * Invoke Vela command stream using async interface.
186 * Must be followed by call(s) to ethosu_wait() upon successful return.
187 * Returns
188 * -1 on error
189 * 0 on success
190 */
191int ethosu_invoke_async(struct ethosu_driver *drv,
192 const void *custom_data_ptr,
193 const int custom_data_size,
194 const uint64_t *base_addr,
195 const size_t *base_addr_size,
196 const int num_base_addr,
197 void *user_arg);
198
199/**
200 * Wait for inference to complete (block=true)
201 * Poll status or finish up if inference is complete (block=false)
202 * (This function is only intended to be used in conjuction with ethosu_invoke_async)
203 * Returns
204 * 1 on inference running (only for block=false)
205 * 0 on inference success
206 * -1 on inference error
207 * -2 on inference not invoked
208 */
209int ethosu_wait(struct ethosu_driver *drv, bool block);
210
211/**
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100212 * Set Ethos-U power mode.
213 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200214void ethosu_set_power_mode(struct ethosu_driver *drv, bool always_on);
Anton Moberg61da4d32020-12-22 16:00:31 +0100215
216/**
Anton Mobergdf386e02021-02-02 11:26:48 +0100217 * Reserves a driver to execute inference with
Anton Moberg61da4d32020-12-22 16:00:31 +0100218 */
219struct ethosu_driver *ethosu_reserve_driver(void);
220
221/**
222 * Change driver status to available
223 */
224void ethosu_release_driver(struct ethosu_driver *drv);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100225
Anton Mobergdf386e02021-02-02 11:26:48 +0100226/**
Anton Moberg0a614292021-03-24 14:08:22 +0100227 * Set clock and power request bits
228 */
229enum ethosu_error_codes set_clock_and_power_request(struct ethosu_driver *drv,
230 enum ethosu_request_clients client,
231 enum ethosu_clock_q_request clock_request,
232 enum ethosu_power_q_request power_request);
233
Kristofer Jonssonbe0bc462021-06-28 11:44:42 +0000234/**
235 * Static inline for backwards-compatibility
236 */
237static inline int ethosu_invoke_v2(const void *custom_data_ptr,
238 const int custom_data_size,
239 const uint64_t *base_addr,
240 const size_t *base_addr_size,
241 const int num_base_addr)
242{
243 struct ethosu_driver *drv = ethosu_reserve_driver();
Kristofer Jonssonb3cde3c2022-01-27 17:30:15 +0100244 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 +0000245 ethosu_release_driver(drv);
246 return result;
247}
248
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200249#ifdef __cplusplus
250}
251#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200252
253#endif // ETHOSU_DRIVER_H