blob: eff905490bf3839196828bdb282941166278e218 [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
2 * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
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 Jonsson3c439172020-08-05 09:38:40 +020019#ifndef ETHOSU_DEVICE_H
20#define ETHOSU_DEVICE_H
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020021
22/******************************************************************************
23 * Includes
24 ******************************************************************************/
25
Bhavik Patel5da40922020-07-15 10:06:43 +020026#include "pmu_ethosu.h"
27
28#include <stdbool.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020029#include <stdint.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/******************************************************************************
36 * Defines
37 ******************************************************************************/
38
39#define ETHOSU_DRIVER_VERSION_MAJOR 0 ///< Driver major version
Douglas Trohaf6a85da2020-05-11 11:45:28 +020040#define ETHOSU_DRIVER_VERSION_MINOR 16 ///< Driver minor version
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020041#define ETHOSU_DRIVER_VERSION_PATCH 0 ///< Driver patch version
42#define ETHOSU_DRIVER_BASEP_INDEXES 8 ///< Number of base pointer indexes
43
44/******************************************************************************
45 * Types
46 ******************************************************************************/
47
48enum ethosu_error_codes
49{
50 ETHOSU_SUCCESS = 0, ///< Success
51 ETHOSU_GENERIC_FAILURE = -1, ///< Generic failure
52 ETHOSU_INVALID_PARAM = -2 ///< Invalid parameter
53};
54
Bhavik Pateldae5be02020-06-18 15:25:15 +020055struct ethosu_device
56{
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020057 volatile uint32_t *base_address;
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +010058 uint32_t proto;
Kristofer Jonssonef387ea2020-08-25 16:32:21 +020059 uint32_t pmcr;
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020060 uint32_t pmccntr[2];
Kristofer Jonssonef387ea2020-08-25 16:32:21 +020061 uint32_t pmcnten;
62 uint32_t pmint;
63 uint32_t pmccntr_cfg;
Bhavik Patel5da40922020-07-15 10:06:43 +020064 uint32_t pmu_evcntr[ETHOSU_PMU_NCOUNTERS];
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020065 uint32_t pmu_evtypr[ETHOSU_PMU_NCOUNTERS];
Per Åstrande6498f02020-11-09 15:33:12 +010066 uint32_t secure;
67 uint32_t privileged;
Bhavik Pateldae5be02020-06-18 15:25:15 +020068};
69
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020070struct ethosu_id
71{
72 uint32_t version_status; ///< Version status
73 uint32_t version_minor; ///< Version minor
74 uint32_t version_major; ///< Version major
75 uint32_t product_major; ///< Product major
76 uint32_t arch_patch_rev; ///< Architecture version patch
77 uint32_t arch_minor_rev; ///< Architecture version minor
78 uint32_t arch_major_rev; ///< Architecture version major
79};
80
81struct ethosu_config
82{
83 struct
84 {
85 uint32_t macs_per_cc; ///< MACs per clock cycle
86 uint32_t cmd_stream_version; ///< NPU command stream version
87 uint32_t shram_size; ///< SHRAM size
88 };
89};
90
91/**
92 * Memory type parameter for set_regioncfg_reg:
93 * Counter{0,1}: Outstanding transactions for
94 * AXI port 0 for memory type/region a=0,b=1
95 * Counter{2,3}: Outstanding transactions for
96 * AXI port 1 for memory type/region a=2,b=3
97 */
98enum ethosu_memory_type
99{
100 ETHOSU_AXI0_OUTSTANDING_COUNTER0 = 0, ///< NPU axi0_outstanding_counter0
101 ETHOSU_AXI0_OUTSTANDING_COUNTER1 = 1, ///< NPU axi0_outstanding_counter1
102 ETHOSU_AXI1_OUTSTANDING_COUNTER2 = 2, ///< NPU axi1_outstanding_counter2
103 ETHOSU_AXI1_OUTSTANDING_COUNTER3 = 3 ///< NPU axi1_outstanding_counter3
104};
105
106enum ethosu_axi_limit_beats
107{
108 ETHOSU_AXI_LIMIT_64_BYTES = 0, ///< NPU AXI limit 64 byte burst split alignment.
109 ETHOSU_AXI_LIMIT_128_BYTES = 1, ///< NPU AXI limit 128 byte burst split alignment.
110 ETHOSU_AXI_LIMIT_256_BYTES = 2 ///< NPU AXI limit 256 byte burst split alignment.
111};
112
113enum ethosu_axi_limit_mem_type
114{
115 ETHOSU_MEM_TYPE_DEVICE_NON_BUFFERABLE = 0,
116 ETHOSU_MEM_TYPE_DEVICE_BUFFERABLE = 1,
117 ETHOSU_MEM_TYPE_NORMAL_NON_CACHEABLE_NON_BUFFERABLE = 2,
118 ETHOSU_MEM_TYPE_NORMAL_NON_CACHEABLE_BUFFERABLE = 3,
119 ETHOSU_MEM_TYPE_WRITE_THROUGH_NO_ALLOCATE = 4,
120 ETHOSU_MEM_TYPE_WRITE_THROUGH_READ_ALLOCATE = 5,
121 ETHOSU_MEM_TYPE_WRITE_THROUGH_WRITE_ALLOCATE = 6,
122 ETHOSU_MEM_TYPE_WRITE_THROUGH_READ_AND_WRITE_ALLOCATE = 7,
123 ETHOSU_MEM_TYPE_WRITE_BACK_NO_ALLOCATE = 8,
124 ETHOSU_MEM_TYPE_WRITE_BACK_READ_ALLOCATE = 9,
125 ETHOSU_MEM_TYPE_WRITE_BACK_WRITE_ALLOCATE = 10,
126 ETHOSU_MEM_TYPE_WRITE_BACK_READ_AND_WRITE_ALLOCATE = 11
127};
128
129enum ethosu_clock_q_request
130{
131 ETHOSU_CLOCK_Q_DISABLE = 0, ///< Disble NPU signal ready for clock off.
132 ETHOSU_CLOCK_Q_ENABLE = 1 ///< Enable NPU signal ready for clock off when stop+idle state reached.
133};
134
135enum ethosu_power_q_request
136{
137 ETHOSU_POWER_Q_DISABLE = 0, ///< Disble NPU signal ready for power off.
138 ETHOSU_POWER_Q_ENABLE = 1 ///< Enable NPU signal ready for power off when stop+idle state reached.
139};
140
141/******************************************************************************
142 * Prototypes
143 ******************************************************************************/
144
145/**
146 * Initialize the device.
147 */
Per Åstrande6498f02020-11-09 15:33:12 +0100148enum ethosu_error_codes ethosu_dev_init(struct ethosu_device *dev,
149 const void *base_address,
150 uint32_t secure_enable,
151 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200152
153/**
154 * Get device id.
155 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200156enum ethosu_error_codes ethosu_get_id(struct ethosu_device *dev, struct ethosu_id *id);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200157
158/**
159 * Get device configuration.
160 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200161enum ethosu_error_codes ethosu_get_config(struct ethosu_device *dev, struct ethosu_config *config);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200162
163/**
164 * Execute a given command stream on NPU.
165 * \param[in] cmd_stream_ptr Pointer to the command stream
166 * \param[in] cms_length Command stream length
167 * \param[in] base_addr Pointer to array of base addresses
168 * - 0: weight tensor
169 * - 1: scratch tensor
170 * - All input tensors
171 * - All output tensors
172 * \param[in] num_base_addr Number of base addresses.
173 * \return \ref ethosu_error_codes
174 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200175enum ethosu_error_codes ethosu_run_command_stream(struct ethosu_device *dev,
176 const uint8_t *cmd_stream_ptr,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200177 uint32_t cms_length,
178 const uint64_t *base_addr,
179 int num_base_addr);
180
181/**
182 * Check if IRQ is raised.
183 * \param[out] irq_status Pointer to IRQ status
184 * - 0 IRQ not raised
185 * - 1 IRQ raised
186 * \return \ref ethosu_error_codes
187 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200188enum ethosu_error_codes ethosu_is_irq_raised(struct ethosu_device *dev, uint8_t *irq_status);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200189
190/**
191 * Clear IRQ status.
192 * \return \ref ethosu_error_codes
193 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200194enum ethosu_error_codes ethosu_clear_irq_status(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200195
196/**
197 * Get the 16 bit status mask.
198 * \param[out] irq_status_mask Pointer to the status mask.
199 * The lower 16 bits of status reg are returned.
200 * bit0: state
201 * bit1: irq_raised
202 * bit2: bus_status
203 * bit3: reset_status
204 * bit4: cmd_parse_error
205 * bit5: cmd_end_reached
206 * bit6: pmu_irq_raised
207 * bit7-15: reserved
208 * \return \ref ethosu_error_codes
209 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200210enum ethosu_error_codes ethosu_get_status_mask(struct ethosu_device *dev, uint16_t *status_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200211
212/**
213 * Get the 16 bit IRQ history mask.
214 * \param[out] irq_history_mask Pointer to the IRQ history mask.
215 * \return \ref ethosu_error_codes
216 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200217enum ethosu_error_codes ethosu_get_irq_history_mask(struct ethosu_device *dev, uint16_t *irq_history_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200218
219/**
220 * Clear the given bits in the
221 * IRQ history mask.
222 * \param[in] irq_history_clear_mask 16 bit mask indicating which bits to
223 * clear in the IRQ history mask.
224 * \return \ref ethosu_error_codes
225 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200226enum ethosu_error_codes ethosu_clear_irq_history_mask(struct ethosu_device *dev, uint16_t irq_history_clear_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200227
228/**
229 * Perform a NPU soft reset.
230 * \return \ref ethosu_error_codes
231 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200232enum ethosu_error_codes ethosu_soft_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200233
234/**
235 * Wait for reset ready.
236 * \return \ref ethosu_error_codes
237 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200238enum ethosu_error_codes ethosu_wait_for_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200239
240/**
241 * Read and return the content of a given NPU APB
242 * register range.
243 * \param[in] start_address Start address.
244 * \param[in] num_reg Number of registers to read.
245 * \param[out] reg_p Pointer to a output area, allocated by the
246 * caller, where the register content shall be
247 * written.
248 * \return \ref ethosu_error_codes
249 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200250enum ethosu_error_codes ethosu_read_apb_reg(struct ethosu_device *dev,
251 uint32_t start_address,
252 uint16_t num_reg,
253 uint32_t *reg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200254
255/**
256 * Set qconfig register. I.e.
257 * AXI configuration for the command stream.
258 * \param[in] memory_type Memory_type to use for command stream:
259 * enum ethosu_memory_type.
260 * \return \ref ethosu_error_codes
261 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200262enum ethosu_error_codes ethosu_set_qconfig(struct ethosu_device *dev, enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200263
264/**
265 * Set register REGIONCFG.
266 * Base pointer configuration.
267 * Bits[2*k+1:2*k] give the memory type for BASEP[k].
268 * \param[in] region Region field to set: 0 - 7.
269 * \param[in] memory_type Memory_type to use for region: enum ethosu_memory_type.
270 * \return \ref ethosu_error_codes
271 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200272enum ethosu_error_codes ethosu_set_regioncfg(struct ethosu_device *dev,
273 uint8_t region,
274 enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200275
276/**
277 * Set AXI limit parameters for port 0 counter 0.
278 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
279 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
280 * \param[in] max_reads Maximum number of outstanding reads.
281 * \param[in] max_writes Maximum number of outstanding writes.
282 * \return \ref ethosu_error_codes
283 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200284enum ethosu_error_codes ethosu_set_axi_limit0(struct ethosu_device *dev,
285 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200286 enum ethosu_axi_limit_mem_type memtype,
287 uint8_t max_reads,
288 uint8_t max_writes);
289/**
290 * Set AXI limit parameters for port 0 counter 1.
291 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
292 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
293 * \param[in] max_reads Maximum number of outstanding reads.
294 * \param[in] max_writes Maximum number of outstanding writes.
295 * \return \ref ethosu_error_codes
296 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200297enum ethosu_error_codes ethosu_set_axi_limit1(struct ethosu_device *dev,
298 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200299 enum ethosu_axi_limit_mem_type memtype,
300 uint8_t max_reads,
301 uint8_t max_writes);
302/**
303 * Set AXI limit parameters for port 1 counter 2.
304 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
305 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
306 * \param[in] max_reads Maximum number of outstanding reads.
307 * \param[in] max_writes Maximum number of outstanding writes.
308 * \return \ref ethosu_error_codes
309 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200310enum ethosu_error_codes ethosu_set_axi_limit2(struct ethosu_device *dev,
311 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200312 enum ethosu_axi_limit_mem_type memtype,
313 uint8_t max_reads,
314 uint8_t max_writes);
315/**
316 * Set AXI limit parameters for port 1 counter 3.
317 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
318 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
319 * \param[in] max_reads Maximum number of outstanding reads.
320 * \param[in] max_writes Maximum number of outstanding writes.
321 * \return \ref ethosu_error_codes
322 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200323enum ethosu_error_codes ethosu_set_axi_limit3(struct ethosu_device *dev,
324 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200325 enum ethosu_axi_limit_mem_type memtype,
326 uint8_t max_reads,
327 uint8_t max_writes);
328
329/**
330 * Get current command stream queue read position.
331 * \param[out] qread Pointer to queue read.
332 * \return \ref ethosu_error_codes
333 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200334enum ethosu_error_codes ethosu_get_qread(struct ethosu_device *dev, uint32_t *qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200335
336/**
337 * Get revision of NPU
338 * \param[out] revision Pointer to revision read.
339 * \return \ref ethosu_error_codes
340 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200341enum ethosu_error_codes ethosu_get_revision(struct ethosu_device *dev, uint32_t *revision);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200342
343/**
344 * Issue run command for the currently programmed
345 * command stream, starting at current queue read
346 * position.
347 * \return \ref ethosu_error_codes
348 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200349enum ethosu_error_codes ethosu_set_command_run(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200350
351/**
352 * Dump a 1KB section of SHRAM.
353 * \param[in] section Section offset to 1KB section in SHRAM.
354 * \param[out] shram_p Pointer to a output area, allocated by the
355 * caller, where the SHRAM content shall be
356 * written.
357 * \return \ref ethosu_error_codes
358 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200359enum ethosu_error_codes ethosu_get_shram_data(struct ethosu_device *dev, int section, uint32_t *shram_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200360
361/**
362 * Set clock and power q request enable bits.
363 * \param[in] clock_q Clock q ENABLE/DISABLE \ref clock_q_request.
364 * \param[in] power_q Power q ENABLE/DISABLE \ref power_q_request.
365 * \return \ref ethosu_error_codes
366 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200367enum ethosu_error_codes ethosu_set_clock_and_power(struct ethosu_device *dev,
368 enum ethosu_clock_q_request clock_q,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200369 enum ethosu_power_q_request power_q);
370
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200371/**
372 * Read register.
373 * \param[in] address Address to read.
374 * \return Register value.
375 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200376uint32_t ethosu_read_reg(struct ethosu_device *dev, uint32_t address);
377
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200378/**
379 * Write register.
380 * \param[in] address Address to read.
381 * \param[in] value Value to be written.
382 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200383void ethosu_write_reg(struct ethosu_device *dev, uint32_t address, uint32_t value);
384
Bhavik Patel5da40922020-07-15 10:06:43 +0200385/**
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200386 * Write register with shadow variable.
387 * \param[in] address Address to read.
388 * \param[in] value Value to be written.
389 */
390void ethosu_write_reg_shadow(struct ethosu_device *dev, uint32_t address, uint32_t value, uint32_t *shadow);
391
392/**
Bhavik Patel5da40922020-07-15 10:06:43 +0200393 * Save the PMU configuration to ethosu_device struct.
394 * \param[in] dev Ethos-U device where the PMU configuration is
395 * saved.
396 * \return \ref ethosu_error_codes
397 */
398enum ethosu_error_codes ethosu_save_pmu_config(struct ethosu_device *dev);
399
400/**
401 * Restore the PMU configuration from a ethosu_device struct.
402 * \param[in] dev Ethos-U device where the PMU configuration is
403 * stored.
404 * \return \ref ethosu_error_codes
405 */
406enum ethosu_error_codes ethosu_restore_pmu_config(struct ethosu_device *dev);
407
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200408/**
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200409 * Save PMU counters to shadow variables in memory.
410 * \param[in] dev Ethos-U device where the PMU configuration is
411 * stored.
412 * \return \ref ethosu_error_codes
413 */
414enum ethosu_error_codes ethosu_save_pmu_counters(struct ethosu_device *dev);
415
416/**
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200417 * Check if the STATUS register has any error bits set or not.
418 * \param[in] dev Ethos-U device to check.
419 * \return true if any error bits set,
420 * false otherwise.
421 */
422bool ethosu_status_has_error(struct ethosu_device *dev);
423
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200424#ifdef __cplusplus
425}
426#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200427
428#endif // ETHOSU_DEVICE_H