blob: 30d707fb25e4757a2dfaaa5eec155f82a18541c7 [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
Anton Mobergdfed5fd2021-03-11 14:41:11 +01002 * Copyright (c) 2019-2021 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
Bhavik Pateldae5be02020-06-18 15:25:15 +020026#include "ethosu_device.h"
27
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
Bhavik Pateldae5be02020-06-18 15:25:15 +020048struct ethosu_driver
49{
50 struct ethosu_device dev;
Jonny Svärda830f172021-06-07 16:57:00 +020051 struct ethosu_driver *next;
52 void *semaphore;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020053 uint64_t fast_memory;
54 size_t fast_memory_size;
Bhavik Patel5f8dad12020-09-30 09:06:52 +020055 bool status_error;
Jonny Svärda830f172021-06-07 16:57:00 +020056 bool abort_inference;
Anton Moberg8d65b6f2020-12-21 09:37:18 +010057 bool dev_power_always_on;
Anton Moberg61da4d32020-12-22 16:00:31 +010058 bool reserved;
Anton Mobergdfed5fd2021-03-11 14:41:11 +010059 volatile bool irq_triggered;
Anton Moberg0a614292021-03-24 14:08:22 +010060 uint8_t clock_request;
61 uint8_t power_request;
Bhavik Pateldae5be02020-06-18 15:25:15 +020062};
63
Jonny Svärda830f172021-06-07 16:57:00 +020064struct ethosu_driver_version
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020065{
Jonny Svärda830f172021-06-07 16:57:00 +020066 uint8_t major;
67 uint8_t minor;
68 uint8_t patch;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020069};
70
Jonny Svärda830f172021-06-07 16:57:00 +020071struct ethosu_hw_info
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020072{
Jonny Svärda830f172021-06-07 16:57:00 +020073 struct ethosu_id version;
74 struct ethosu_config cfg;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020075};
76
Anton Moberg0a614292021-03-24 14:08:22 +010077enum ethosu_request_clients
78{
79 ETHOSU_PMU_REQUEST = 0,
80 ETHOSU_INFERENCE_REQUEST = 1,
81};
82
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020083/******************************************************************************
Jonny Svärda830f172021-06-07 16:57:00 +020084 * Prototypes (weak functions in driver)
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020085 ******************************************************************************/
86
Jonny Svärda830f172021-06-07 16:57:00 +020087/**
88 * Interrupt handler to be called on IRQ from Ethos-U
89 */
90void ethosu_irq_handler(struct ethosu_driver *drv);
91
92/*
93 * Flush/clean the data cache by address and size. Passing NULL as p argument
94 * expects the whole cache to be flushed.
95 */
96
97void ethosu_flush_dcache(uint32_t *p, size_t bytes);
98/*
99 * Invalidate the data cache by address and size. Passing NULL as p argument
100 * expects the whole cache to be invalidated.
101 */
102void ethosu_invalidate_dcache(uint32_t *p, size_t bytes);
103
104/*
105 * Minimal sempahore and mutex implementation for baremetal applications. See
106 * ethosu_driver.c.
107 */
108void *ethosu_mutex_create(void);
109void ethosu_mutex_lock(void *mutex);
110void ethosu_mutex_unlock(void *mutex);
111void *ethosu_semaphore_create(void);
112void ethosu_semaphore_take(void *sem);
113void ethosu_semaphore_give(void *sem);
114
115/*
116 * Callbacks for begin/end of inference. inference_data pointer is set in the
117 * ethosu_invoke() call, referenced as custom_data_ptr.
118 */
119void ethosu_inference_begin(struct ethosu_driver *drv, const void *inference_data);
120void ethosu_inference_end(struct ethosu_driver *drv, const void *inference_data);
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200121
122/******************************************************************************
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200123 * Prototypes
124 ******************************************************************************/
125
126/**
127 * Initialize the Ethos-U driver.
128 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200129int ethosu_init(struct ethosu_driver *drv,
130 const void *base_address,
131 const void *fast_memory,
132 const size_t fast_memory_size,
133 uint32_t secure_enable,
134 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200135
136/**
Jonny Svärda830f172021-06-07 16:57:00 +0200137 * Get Ethos-U driver version.
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200138 */
Jonny Svärda830f172021-06-07 16:57:00 +0200139void ethosu_get_driver_version(struct ethosu_driver_version *ver);
140
141/**
142 * Get Ethos-U hardware information.
143 */
144void ethosu_get_hw_info(struct ethosu_driver *drv, struct ethosu_hw_info *hw);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200145
146/**
147 * Invoke Vela command stream.
148 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200149int ethosu_invoke(struct ethosu_driver *drv,
150 const void *custom_data_ptr,
151 const int custom_data_size,
152 const uint64_t *base_addr,
153 const size_t *base_addr_size,
154 const int num_base_addr);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200155
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200156/**
157 * Abort Ethos-U inference.
158 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200159void ethosu_abort(struct ethosu_driver *drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200160
Per Åstrand25d78c02020-04-21 14:19:44 +0200161/**
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100162 * Set Ethos-U power mode.
163 */
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200164void ethosu_set_power_mode(struct ethosu_driver *drv, bool always_on);
Anton Moberg61da4d32020-12-22 16:00:31 +0100165
166/**
Anton Mobergdf386e02021-02-02 11:26:48 +0100167 * Reserves a driver to execute inference with
Anton Moberg61da4d32020-12-22 16:00:31 +0100168 */
169struct ethosu_driver *ethosu_reserve_driver(void);
170
171/**
172 * Change driver status to available
173 */
174void ethosu_release_driver(struct ethosu_driver *drv);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100175
Anton Mobergdf386e02021-02-02 11:26:48 +0100176/**
Anton Moberg0a614292021-03-24 14:08:22 +0100177 * Set clock and power request bits
178 */
179enum 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
184/**
Anton Mobergdf386e02021-02-02 11:26:48 +0100185 * Static inline for backwards-compatibility
186 */
187static 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();
Anton Mobergeffc7aa2021-05-03 09:25:06 +0200194 int result = ethosu_invoke(drv, custom_data_ptr, custom_data_size, base_addr, base_addr_size, num_base_addr);
Anton Mobergdf386e02021-02-02 11:26:48 +0100195 ethosu_release_driver(drv);
196 return result;
197}
198
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200199#ifdef __cplusplus
200}
201#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200202
203#endif // ETHOSU_DRIVER_H