Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2022 Arm Limited. All rights reserved. |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 3 | * |
| 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 Jonsson | 3c43917 | 2020-08-05 09:38:40 +0200 | [diff] [blame] | 19 | #ifndef ETHOSU_DRIVER_H |
| 20 | #define ETHOSU_DRIVER_H |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 21 | |
| 22 | /****************************************************************************** |
| 23 | * Includes |
| 24 | ******************************************************************************/ |
| 25 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 26 | #include "ethosu_types.h" |
Bhavik Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 27 | |
| 28 | #include <stdbool.h> |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 29 | #include <stddef.h> |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 30 | #include <stdint.h> |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /****************************************************************************** |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 37 | * 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 Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 45 | * Types |
| 46 | ******************************************************************************/ |
| 47 | |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 48 | // Forward declare |
| 49 | struct ethosu_device; |
| 50 | |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame] | 51 | enum ethosu_job_state |
| 52 | { |
| 53 | ETHOSU_JOB_IDLE = 0, |
| 54 | ETHOSU_JOB_RUNNING, |
| 55 | ETHOSU_JOB_DONE |
| 56 | }; |
| 57 | |
| 58 | struct 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 Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 69 | struct ethosu_driver |
| 70 | { |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 71 | struct ethosu_device *dev; |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 72 | struct ethosu_driver *next; |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame] | 73 | struct ethosu_job job; |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 74 | void *semaphore; |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 75 | uint64_t fast_memory; |
| 76 | size_t fast_memory_size; |
Jonny Svärd | 301399d | 2022-04-26 18:31:24 +0200 | [diff] [blame] | 77 | uint32_t power_request_counter; |
Bhavik Patel | 5f8dad1 | 2020-09-30 09:06:52 +0200 | [diff] [blame] | 78 | bool status_error; |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 79 | bool reserved; |
Bhavik Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 82 | struct ethosu_driver_version |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 83 | { |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 84 | uint8_t major; |
| 85 | uint8_t minor; |
| 86 | uint8_t patch; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 89 | enum ethosu_request_clients |
| 90 | { |
| 91 | ETHOSU_PMU_REQUEST = 0, |
| 92 | ETHOSU_INFERENCE_REQUEST = 1, |
| 93 | }; |
| 94 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 95 | /****************************************************************************** |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 96 | * Prototypes (weak functions in driver) |
Kristofer Jonsson | 4dc73dc | 2020-10-16 12:33:47 +0200 | [diff] [blame] | 97 | ******************************************************************************/ |
| 98 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 99 | /** |
| 100 | * Interrupt handler to be called on IRQ from Ethos-U |
| 101 | */ |
| 102 | void ethosu_irq_handler(struct ethosu_driver *drv); |
| 103 | |
| 104 | /* |
| 105 | * Flush/clean the data cache by address and size. Passing NULL as p argument |
| 106 | * expects the whole cache to be flushed. |
| 107 | */ |
| 108 | |
| 109 | void ethosu_flush_dcache(uint32_t *p, size_t bytes); |
| 110 | /* |
| 111 | * Invalidate the data cache by address and size. Passing NULL as p argument |
| 112 | * expects the whole cache to be invalidated. |
| 113 | */ |
| 114 | void ethosu_invalidate_dcache(uint32_t *p, size_t bytes); |
| 115 | |
| 116 | /* |
| 117 | * Minimal sempahore and mutex implementation for baremetal applications. See |
| 118 | * ethosu_driver.c. |
| 119 | */ |
| 120 | void *ethosu_mutex_create(void); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 121 | void *ethosu_semaphore_create(void); |
Ledion Daja | c6505f3 | 2022-04-20 09:55:21 +0200 | [diff] [blame] | 122 | /* |
| 123 | * Returns: |
| 124 | * -1 on error |
| 125 | * 0 on success |
| 126 | */ |
| 127 | int ethosu_mutex_lock(void *mutex); |
| 128 | int ethosu_mutex_unlock(void *mutex); |
| 129 | int ethosu_semaphore_take(void *sem); |
| 130 | int ethosu_semaphore_give(void *sem); |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 131 | |
| 132 | /* |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 133 | * Callbacks for begin/end of inference. user_data pointer is passed to the |
| 134 | * ethosu_invoke() call and forwarded to the callback functions. |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 135 | */ |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 136 | void ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg); |
| 137 | void ethosu_inference_end(struct ethosu_driver *drv, void *user_arg); |
Kristofer Jonsson | 4dc73dc | 2020-10-16 12:33:47 +0200 | [diff] [blame] | 138 | |
Kristofer Jonsson | 9670332 | 2022-11-04 15:54:32 +0100 | [diff] [blame^] | 139 | /** |
| 140 | * Remapping command stream and base pointer addresses. |
| 141 | * |
| 142 | * @param address Address to be remapped. |
| 143 | * @param index -1: command stream, 0-n base address index |
| 144 | * |
| 145 | * @return Remapped address |
| 146 | */ |
| 147 | uint64_t ethosu_address_remap(uint64_t address, int index); |
| 148 | |
Kristofer Jonsson | 4dc73dc | 2020-10-16 12:33:47 +0200 | [diff] [blame] | 149 | /****************************************************************************** |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 150 | * Prototypes |
| 151 | ******************************************************************************/ |
| 152 | |
| 153 | /** |
| 154 | * Initialize the Ethos-U driver. |
| 155 | */ |
Anton Moberg | effc7aa | 2021-05-03 09:25:06 +0200 | [diff] [blame] | 156 | int ethosu_init(struct ethosu_driver *drv, |
| 157 | const void *base_address, |
| 158 | const void *fast_memory, |
| 159 | const size_t fast_memory_size, |
| 160 | uint32_t secure_enable, |
| 161 | uint32_t privilege_enable); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 162 | |
| 163 | /** |
Anton Moberg | 547ca53 | 2021-06-14 09:43:53 +0200 | [diff] [blame] | 164 | * Deinitialize the Ethos-U driver. |
| 165 | */ |
| 166 | void ethosu_deinit(struct ethosu_driver *drv); |
| 167 | |
| 168 | /** |
Jonny Svärd | 301399d | 2022-04-26 18:31:24 +0200 | [diff] [blame] | 169 | * Soft resets the Ethos-U device. |
| 170 | */ |
| 171 | bool ethosu_soft_reset(struct ethosu_driver *drv); |
| 172 | |
| 173 | /** |
| 174 | * Request to disable Q-channel power gating of the Ethos-U device. |
| 175 | * Power requests are ref.counted. Increases count. |
| 176 | * (Note: clock gating is made to follow power gating) |
| 177 | */ |
| 178 | bool ethosu_request_power(struct ethosu_driver *drv); |
| 179 | |
| 180 | /** |
| 181 | * Release disable request for Q-channel power gating of the Ethos-U device. |
| 182 | * Power requests are ref.counted. Decreases count. |
| 183 | */ |
| 184 | void ethosu_release_power(struct ethosu_driver *drv); |
| 185 | |
| 186 | /** |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 187 | * Get Ethos-U driver version. |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 188 | */ |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 189 | void ethosu_get_driver_version(struct ethosu_driver_version *ver); |
| 190 | |
| 191 | /** |
| 192 | * Get Ethos-U hardware information. |
| 193 | */ |
| 194 | void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Invoke Vela command stream. |
| 198 | */ |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 199 | int ethosu_invoke_v3(struct ethosu_driver *drv, |
| 200 | const void *custom_data_ptr, |
| 201 | const int custom_data_size, |
| 202 | const uint64_t *base_addr, |
| 203 | const size_t *base_addr_size, |
| 204 | const int num_base_addr, |
| 205 | void *user_arg); |
| 206 | |
| 207 | #define ethosu_invoke(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr) \ |
| 208 | ethosu_invoke_v3(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr, 0) |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 209 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 210 | /** |
Jonny Svärd | 1a3bb92 | 2022-02-25 16:28:21 +0100 | [diff] [blame] | 211 | * Invoke Vela command stream using async interface. |
| 212 | * Must be followed by call(s) to ethosu_wait() upon successful return. |
| 213 | * Returns |
| 214 | * -1 on error |
| 215 | * 0 on success |
| 216 | */ |
| 217 | int ethosu_invoke_async(struct ethosu_driver *drv, |
| 218 | const void *custom_data_ptr, |
| 219 | const int custom_data_size, |
| 220 | const uint64_t *base_addr, |
| 221 | const size_t *base_addr_size, |
| 222 | const int num_base_addr, |
| 223 | void *user_arg); |
| 224 | |
| 225 | /** |
| 226 | * Wait for inference to complete (block=true) |
| 227 | * Poll status or finish up if inference is complete (block=false) |
| 228 | * (This function is only intended to be used in conjuction with ethosu_invoke_async) |
| 229 | * Returns |
| 230 | * 1 on inference running (only for block=false) |
| 231 | * 0 on inference success |
| 232 | * -1 on inference error |
| 233 | * -2 on inference not invoked |
| 234 | */ |
| 235 | int ethosu_wait(struct ethosu_driver *drv, bool block); |
| 236 | |
| 237 | /** |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 238 | * Reserves a driver to execute inference with |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 239 | */ |
| 240 | struct ethosu_driver *ethosu_reserve_driver(void); |
| 241 | |
| 242 | /** |
| 243 | * Change driver status to available |
| 244 | */ |
| 245 | void ethosu_release_driver(struct ethosu_driver *drv); |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 246 | |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 247 | /** |
Kristofer Jonsson | be0bc46 | 2021-06-28 11:44:42 +0000 | [diff] [blame] | 248 | * Static inline for backwards-compatibility |
| 249 | */ |
| 250 | static inline int ethosu_invoke_v2(const void *custom_data_ptr, |
| 251 | const int custom_data_size, |
| 252 | const uint64_t *base_addr, |
| 253 | const size_t *base_addr_size, |
| 254 | const int num_base_addr) |
| 255 | { |
| 256 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame] | 257 | int result = ethosu_invoke_v3(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr, 0); |
Kristofer Jonsson | be0bc46 | 2021-06-28 11:44:42 +0000 | [diff] [blame] | 258 | ethosu_release_driver(drv); |
| 259 | return result; |
| 260 | } |
| 261 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 262 | #ifdef __cplusplus |
| 263 | } |
| 264 | #endif |
Kristofer Jonsson | 3c43917 | 2020-08-05 09:38:40 +0200 | [diff] [blame] | 265 | |
| 266 | #endif // ETHOSU_DRIVER_H |