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 | |
Bhavik Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 51 | struct ethosu_driver |
| 52 | { |
Jonny Svärd | 136810f | 2021-10-13 16:04:26 +0200 | [diff] [blame] | 53 | struct ethosu_device *dev; |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 54 | struct ethosu_driver *next; |
| 55 | void *semaphore; |
Kristofer Jonsson | 2b201c3 | 2020-09-02 16:42:43 +0200 | [diff] [blame] | 56 | uint64_t fast_memory; |
| 57 | size_t fast_memory_size; |
Bhavik Patel | 5f8dad1 | 2020-09-30 09:06:52 +0200 | [diff] [blame] | 58 | bool status_error; |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 59 | bool dev_power_always_on; |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 60 | bool reserved; |
Anton Moberg | dfed5fd | 2021-03-11 14:41:11 +0100 | [diff] [blame] | 61 | volatile bool irq_triggered; |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 62 | uint8_t clock_request; |
| 63 | uint8_t power_request; |
Bhavik Patel | dae5be0 | 2020-06-18 15:25:15 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 66 | struct ethosu_driver_version |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 67 | { |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 68 | uint8_t major; |
| 69 | uint8_t minor; |
| 70 | uint8_t patch; |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 73 | enum ethosu_request_clients |
| 74 | { |
| 75 | ETHOSU_PMU_REQUEST = 0, |
| 76 | ETHOSU_INFERENCE_REQUEST = 1, |
| 77 | }; |
| 78 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 79 | /****************************************************************************** |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 80 | * Prototypes (weak functions in driver) |
Kristofer Jonsson | 4dc73dc | 2020-10-16 12:33:47 +0200 | [diff] [blame] | 81 | ******************************************************************************/ |
| 82 | |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 83 | /** |
| 84 | * Interrupt handler to be called on IRQ from Ethos-U |
| 85 | */ |
| 86 | void ethosu_irq_handler(struct ethosu_driver *drv); |
| 87 | |
| 88 | /* |
| 89 | * Flush/clean the data cache by address and size. Passing NULL as p argument |
| 90 | * expects the whole cache to be flushed. |
| 91 | */ |
| 92 | |
| 93 | void ethosu_flush_dcache(uint32_t *p, size_t bytes); |
| 94 | /* |
| 95 | * Invalidate the data cache by address and size. Passing NULL as p argument |
| 96 | * expects the whole cache to be invalidated. |
| 97 | */ |
| 98 | void ethosu_invalidate_dcache(uint32_t *p, size_t bytes); |
| 99 | |
| 100 | /* |
| 101 | * Minimal sempahore and mutex implementation for baremetal applications. See |
| 102 | * ethosu_driver.c. |
| 103 | */ |
| 104 | void *ethosu_mutex_create(void); |
| 105 | void ethosu_mutex_lock(void *mutex); |
| 106 | void ethosu_mutex_unlock(void *mutex); |
| 107 | void *ethosu_semaphore_create(void); |
| 108 | void ethosu_semaphore_take(void *sem); |
| 109 | void ethosu_semaphore_give(void *sem); |
| 110 | |
| 111 | /* |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame^] | 112 | * Callbacks for begin/end of inference. user_data pointer is passed to the |
| 113 | * ethosu_invoke() call and forwarded to the callback functions. |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 114 | */ |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame^] | 115 | void ethosu_inference_begin(struct ethosu_driver *drv, void *user_arg); |
| 116 | void ethosu_inference_end(struct ethosu_driver *drv, void *user_arg); |
Kristofer Jonsson | 4dc73dc | 2020-10-16 12:33:47 +0200 | [diff] [blame] | 117 | |
| 118 | /****************************************************************************** |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 119 | * Prototypes |
| 120 | ******************************************************************************/ |
| 121 | |
| 122 | /** |
| 123 | * Initialize the Ethos-U driver. |
| 124 | */ |
Anton Moberg | effc7aa | 2021-05-03 09:25:06 +0200 | [diff] [blame] | 125 | int ethosu_init(struct ethosu_driver *drv, |
| 126 | const void *base_address, |
| 127 | const void *fast_memory, |
| 128 | const size_t fast_memory_size, |
| 129 | uint32_t secure_enable, |
| 130 | uint32_t privilege_enable); |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 131 | |
| 132 | /** |
Anton Moberg | 547ca53 | 2021-06-14 09:43:53 +0200 | [diff] [blame] | 133 | * Deinitialize the Ethos-U driver. |
| 134 | */ |
| 135 | void ethosu_deinit(struct ethosu_driver *drv); |
| 136 | |
| 137 | /** |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 138 | * Get Ethos-U driver version. |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 139 | */ |
Jonny Svärd | a830f17 | 2021-06-07 16:57:00 +0200 | [diff] [blame] | 140 | void ethosu_get_driver_version(struct ethosu_driver_version *ver); |
| 141 | |
| 142 | /** |
| 143 | * Get Ethos-U hardware information. |
| 144 | */ |
| 145 | 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] | 146 | |
| 147 | /** |
| 148 | * Invoke Vela command stream. |
| 149 | */ |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame^] | 150 | int ethosu_invoke_v3(struct ethosu_driver *drv, |
| 151 | const void *custom_data_ptr, |
| 152 | const int custom_data_size, |
| 153 | const uint64_t *base_addr, |
| 154 | const size_t *base_addr_size, |
| 155 | const int num_base_addr, |
| 156 | void *user_arg); |
| 157 | |
| 158 | #define ethosu_invoke(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr) \ |
| 159 | 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] | 160 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 161 | /** |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 162 | * Set Ethos-U power mode. |
| 163 | */ |
Anton Moberg | effc7aa | 2021-05-03 09:25:06 +0200 | [diff] [blame] | 164 | void ethosu_set_power_mode(struct ethosu_driver *drv, bool always_on); |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 165 | |
| 166 | /** |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 167 | * Reserves a driver to execute inference with |
Anton Moberg | 61da4d3 | 2020-12-22 16:00:31 +0100 | [diff] [blame] | 168 | */ |
| 169 | struct ethosu_driver *ethosu_reserve_driver(void); |
| 170 | |
| 171 | /** |
| 172 | * Change driver status to available |
| 173 | */ |
| 174 | void ethosu_release_driver(struct ethosu_driver *drv); |
Anton Moberg | 8d65b6f | 2020-12-21 09:37:18 +0100 | [diff] [blame] | 175 | |
Anton Moberg | df386e0 | 2021-02-02 11:26:48 +0100 | [diff] [blame] | 176 | /** |
Anton Moberg | 0a61429 | 2021-03-24 14:08:22 +0100 | [diff] [blame] | 177 | * Set clock and power request bits |
| 178 | */ |
| 179 | enum ethosu_error_codes set_clock_and_power_request(struct ethosu_driver *drv, |
| 180 | enum ethosu_request_clients client, |
| 181 | enum ethosu_clock_q_request clock_request, |
| 182 | enum ethosu_power_q_request power_request); |
| 183 | |
Kristofer Jonsson | be0bc46 | 2021-06-28 11:44:42 +0000 | [diff] [blame] | 184 | /** |
| 185 | * Static inline for backwards-compatibility |
| 186 | */ |
| 187 | static inline int ethosu_invoke_v2(const void *custom_data_ptr, |
| 188 | const int custom_data_size, |
| 189 | const uint64_t *base_addr, |
| 190 | const size_t *base_addr_size, |
| 191 | const int num_base_addr) |
| 192 | { |
| 193 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
Kristofer Jonsson | b3cde3c | 2022-01-27 17:30:15 +0100 | [diff] [blame^] | 194 | 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] | 195 | ethosu_release_driver(drv); |
| 196 | return result; |
| 197 | } |
| 198 | |
Kristofer Jonsson | 49bdee8 | 2020-04-06 13:21:21 +0200 | [diff] [blame] | 199 | #ifdef __cplusplus |
| 200 | } |
| 201 | #endif |
Kristofer Jonsson | 3c43917 | 2020-08-05 09:38:40 +0200 | [diff] [blame] | 202 | |
| 203 | #endif // ETHOSU_DRIVER_H |