blob: dad88efe2b8e6ce91e83fa24a9e0394d6a7b5371 [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
Anton Mobergb8bcf132021-03-29 10:02:25 +02002 * 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_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 <stdbool.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020027#include <stdint.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/******************************************************************************
34 * Defines
35 ******************************************************************************/
36
Jonny Svärda830f172021-06-07 16:57:00 +020037#define ETHOSU_BASEP_INDEXES 8 ///< Number of base pointer indexes
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020038
Anton Moberg61da4d32020-12-22 16:00:31 +010039#ifndef ETHOSU_PMU_NCOUNTERS
40#define ETHOSU_PMU_NCOUNTERS 4
41#endif
42
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020043/******************************************************************************
44 * Types
45 ******************************************************************************/
46
47enum ethosu_error_codes
48{
49 ETHOSU_SUCCESS = 0, ///< Success
50 ETHOSU_GENERIC_FAILURE = -1, ///< Generic failure
51 ETHOSU_INVALID_PARAM = -2 ///< Invalid parameter
52};
53
Bhavik Pateldae5be02020-06-18 15:25:15 +020054struct ethosu_device
55{
Kristofer Jonsson1c893b52021-05-26 12:06:14 +020056 volatile uintptr_t base_address;
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +010057 uint32_t proto;
Kristofer Jonssonef387ea2020-08-25 16:32:21 +020058 uint32_t pmcr;
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020059 uint32_t pmccntr[2];
Kristofer Jonssonef387ea2020-08-25 16:32:21 +020060 uint32_t pmcnten;
61 uint32_t pmint;
62 uint32_t pmccntr_cfg;
Bhavik Patel5da40922020-07-15 10:06:43 +020063 uint32_t pmu_evcntr[ETHOSU_PMU_NCOUNTERS];
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020064 uint32_t pmu_evtypr[ETHOSU_PMU_NCOUNTERS];
Per Åstrande6498f02020-11-09 15:33:12 +010065 uint32_t secure;
66 uint32_t privileged;
Bhavik Pateldae5be02020-06-18 15:25:15 +020067};
68
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020069struct ethosu_id
70{
71 uint32_t version_status; ///< Version status
72 uint32_t version_minor; ///< Version minor
73 uint32_t version_major; ///< Version major
74 uint32_t product_major; ///< Product major
75 uint32_t arch_patch_rev; ///< Architecture version patch
76 uint32_t arch_minor_rev; ///< Architecture version minor
77 uint32_t arch_major_rev; ///< Architecture version major
78};
79
80struct ethosu_config
81{
Jonny Svärda830f172021-06-07 16:57:00 +020082 uint32_t macs_per_cc; ///< MACs per clock cycle
83 uint32_t cmd_stream_version; ///< NPU command stream version
84 uint32_t shram_size; ///< SHRAM size
85 uint32_t custom_dma; ///< Custom DMA enabled
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020086};
87
88/**
89 * Memory type parameter for set_regioncfg_reg:
90 * Counter{0,1}: Outstanding transactions for
91 * AXI port 0 for memory type/region a=0,b=1
92 * Counter{2,3}: Outstanding transactions for
93 * AXI port 1 for memory type/region a=2,b=3
94 */
95enum ethosu_memory_type
96{
97 ETHOSU_AXI0_OUTSTANDING_COUNTER0 = 0, ///< NPU axi0_outstanding_counter0
98 ETHOSU_AXI0_OUTSTANDING_COUNTER1 = 1, ///< NPU axi0_outstanding_counter1
99 ETHOSU_AXI1_OUTSTANDING_COUNTER2 = 2, ///< NPU axi1_outstanding_counter2
100 ETHOSU_AXI1_OUTSTANDING_COUNTER3 = 3 ///< NPU axi1_outstanding_counter3
101};
102
103enum ethosu_axi_limit_beats
104{
105 ETHOSU_AXI_LIMIT_64_BYTES = 0, ///< NPU AXI limit 64 byte burst split alignment.
106 ETHOSU_AXI_LIMIT_128_BYTES = 1, ///< NPU AXI limit 128 byte burst split alignment.
107 ETHOSU_AXI_LIMIT_256_BYTES = 2 ///< NPU AXI limit 256 byte burst split alignment.
108};
109
110enum ethosu_axi_limit_mem_type
111{
112 ETHOSU_MEM_TYPE_DEVICE_NON_BUFFERABLE = 0,
113 ETHOSU_MEM_TYPE_DEVICE_BUFFERABLE = 1,
114 ETHOSU_MEM_TYPE_NORMAL_NON_CACHEABLE_NON_BUFFERABLE = 2,
115 ETHOSU_MEM_TYPE_NORMAL_NON_CACHEABLE_BUFFERABLE = 3,
116 ETHOSU_MEM_TYPE_WRITE_THROUGH_NO_ALLOCATE = 4,
117 ETHOSU_MEM_TYPE_WRITE_THROUGH_READ_ALLOCATE = 5,
118 ETHOSU_MEM_TYPE_WRITE_THROUGH_WRITE_ALLOCATE = 6,
119 ETHOSU_MEM_TYPE_WRITE_THROUGH_READ_AND_WRITE_ALLOCATE = 7,
120 ETHOSU_MEM_TYPE_WRITE_BACK_NO_ALLOCATE = 8,
121 ETHOSU_MEM_TYPE_WRITE_BACK_READ_ALLOCATE = 9,
122 ETHOSU_MEM_TYPE_WRITE_BACK_WRITE_ALLOCATE = 10,
123 ETHOSU_MEM_TYPE_WRITE_BACK_READ_AND_WRITE_ALLOCATE = 11
124};
125
126enum ethosu_clock_q_request
127{
128 ETHOSU_CLOCK_Q_DISABLE = 0, ///< Disble NPU signal ready for clock off.
129 ETHOSU_CLOCK_Q_ENABLE = 1 ///< Enable NPU signal ready for clock off when stop+idle state reached.
130};
131
132enum ethosu_power_q_request
133{
134 ETHOSU_POWER_Q_DISABLE = 0, ///< Disble NPU signal ready for power off.
135 ETHOSU_POWER_Q_ENABLE = 1 ///< Enable NPU signal ready for power off when stop+idle state reached.
136};
137
138/******************************************************************************
139 * Prototypes
140 ******************************************************************************/
141
142/**
143 * Initialize the device.
144 */
Per Åstrande6498f02020-11-09 15:33:12 +0100145enum ethosu_error_codes ethosu_dev_init(struct ethosu_device *dev,
146 const void *base_address,
147 uint32_t secure_enable,
148 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200149
150/**
151 * Get device id.
152 */
Jonny Svärda830f172021-06-07 16:57:00 +0200153enum ethosu_error_codes ethosu_dev_get_id(struct ethosu_device *dev, struct ethosu_id *id);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200154
155/**
156 * Get device configuration.
157 */
Jonny Svärda830f172021-06-07 16:57:00 +0200158enum ethosu_error_codes ethosu_dev_get_config(struct ethosu_device *dev, struct ethosu_config *config);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200159
160/**
161 * Execute a given command stream on NPU.
162 * \param[in] cmd_stream_ptr Pointer to the command stream
163 * \param[in] cms_length Command stream length
164 * \param[in] base_addr Pointer to array of base addresses
165 * - 0: weight tensor
166 * - 1: scratch tensor
167 * - All input tensors
168 * - All output tensors
169 * \param[in] num_base_addr Number of base addresses.
170 * \return \ref ethosu_error_codes
171 */
Jonny Svärda830f172021-06-07 16:57:00 +0200172enum ethosu_error_codes ethosu_dev_run_command_stream(struct ethosu_device *dev,
173 const uint8_t *cmd_stream_ptr,
174 uint32_t cms_length,
175 const uint64_t *base_addr,
176 int num_base_addr);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200177
178/**
179 * Check if IRQ is raised.
180 * \param[out] irq_status Pointer to IRQ status
181 * - 0 IRQ not raised
182 * - 1 IRQ raised
183 * \return \ref ethosu_error_codes
184 */
Jonny Svärda830f172021-06-07 16:57:00 +0200185enum ethosu_error_codes ethosu_dev_is_irq_raised(struct ethosu_device *dev, uint8_t *irq_status);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200186
187/**
188 * Clear IRQ status.
189 * \return \ref ethosu_error_codes
190 */
Jonny Svärda830f172021-06-07 16:57:00 +0200191enum ethosu_error_codes ethosu_dev_clear_irq_status(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200192
193/**
194 * Get the 16 bit status mask.
195 * \param[out] irq_status_mask Pointer to the status mask.
196 * The lower 16 bits of status reg are returned.
197 * bit0: state
198 * bit1: irq_raised
199 * bit2: bus_status
200 * bit3: reset_status
201 * bit4: cmd_parse_error
202 * bit5: cmd_end_reached
203 * bit6: pmu_irq_raised
204 * bit7-15: reserved
205 * \return \ref ethosu_error_codes
206 */
Jonny Svärda830f172021-06-07 16:57:00 +0200207
208enum ethosu_error_codes ethosu_dev_get_status_mask(struct ethosu_device *dev, uint16_t *status_mask);
209
210/**
211 * Get current NPU status
212 * \return 32 bit status value
213 */
214uint32_t ethosu_dev_get_status(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200215
216/**
217 * Get the 16 bit IRQ history mask.
218 * \param[out] irq_history_mask Pointer to the IRQ history mask.
219 * \return \ref ethosu_error_codes
220 */
Jonny Svärda830f172021-06-07 16:57:00 +0200221enum ethosu_error_codes ethosu_dev_get_irq_history_mask(struct ethosu_device *dev, uint16_t *irq_history_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200222
223/**
224 * Clear the given bits in the
225 * IRQ history mask.
226 * \param[in] irq_history_clear_mask 16 bit mask indicating which bits to
227 * clear in the IRQ history mask.
228 * \return \ref ethosu_error_codes
229 */
Jonny Svärda830f172021-06-07 16:57:00 +0200230enum ethosu_error_codes ethosu_dev_clear_irq_history_mask(struct ethosu_device *dev, uint16_t irq_history_clear_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200231
232/**
233 * Perform a NPU soft reset.
234 * \return \ref ethosu_error_codes
235 */
Jonny Svärda830f172021-06-07 16:57:00 +0200236enum ethosu_error_codes ethosu_dev_soft_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200237
238/**
239 * Wait for reset ready.
240 * \return \ref ethosu_error_codes
241 */
Jonny Svärda830f172021-06-07 16:57:00 +0200242enum ethosu_error_codes ethosu_dev_wait_for_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200243
244/**
245 * Read and return the content of a given NPU APB
246 * register range.
247 * \param[in] start_address Start address.
248 * \param[in] num_reg Number of registers to read.
249 * \param[out] reg_p Pointer to a output area, allocated by the
250 * caller, where the register content shall be
251 * written.
252 * \return \ref ethosu_error_codes
253 */
Jonny Svärda830f172021-06-07 16:57:00 +0200254enum ethosu_error_codes ethosu_dev_read_apb_reg(struct ethosu_device *dev,
255 uint32_t start_address,
256 uint16_t num_reg,
257 uint32_t *reg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200258
259/**
260 * Set qconfig register. I.e.
261 * AXI configuration for the command stream.
262 * \param[in] memory_type Memory_type to use for command stream:
263 * enum ethosu_memory_type.
264 * \return \ref ethosu_error_codes
265 */
Jonny Svärda830f172021-06-07 16:57:00 +0200266enum ethosu_error_codes ethosu_dev_set_qconfig(struct ethosu_device *dev, enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200267
268/**
269 * Set register REGIONCFG.
270 * Base pointer configuration.
271 * Bits[2*k+1:2*k] give the memory type for BASEP[k].
272 * \param[in] region Region field to set: 0 - 7.
273 * \param[in] memory_type Memory_type to use for region: enum ethosu_memory_type.
274 * \return \ref ethosu_error_codes
275 */
Jonny Svärda830f172021-06-07 16:57:00 +0200276enum ethosu_error_codes ethosu_dev_set_regioncfg(struct ethosu_device *dev,
277 uint8_t region,
278 enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200279
280/**
281 * Set AXI limit parameters for port 0 counter 0.
282 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
283 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
284 * \param[in] max_reads Maximum number of outstanding reads.
285 * \param[in] max_writes Maximum number of outstanding writes.
286 * \return \ref ethosu_error_codes
287 */
Jonny Svärda830f172021-06-07 16:57:00 +0200288enum ethosu_error_codes ethosu_dev_set_axi_limit0(struct ethosu_device *dev,
289 enum ethosu_axi_limit_beats max_beats,
290 enum ethosu_axi_limit_mem_type memtype,
291 uint8_t max_reads,
292 uint8_t max_writes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200293/**
294 * Set AXI limit parameters for port 0 counter 1.
295 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
296 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
297 * \param[in] max_reads Maximum number of outstanding reads.
298 * \param[in] max_writes Maximum number of outstanding writes.
299 * \return \ref ethosu_error_codes
300 */
Jonny Svärda830f172021-06-07 16:57:00 +0200301enum ethosu_error_codes ethosu_dev_set_axi_limit1(struct ethosu_device *dev,
302 enum ethosu_axi_limit_beats max_beats,
303 enum ethosu_axi_limit_mem_type memtype,
304 uint8_t max_reads,
305 uint8_t max_writes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200306/**
307 * Set AXI limit parameters for port 1 counter 2.
308 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
309 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
310 * \param[in] max_reads Maximum number of outstanding reads.
311 * \param[in] max_writes Maximum number of outstanding writes.
312 * \return \ref ethosu_error_codes
313 */
Jonny Svärda830f172021-06-07 16:57:00 +0200314enum ethosu_error_codes ethosu_dev_set_axi_limit2(struct ethosu_device *dev,
315 enum ethosu_axi_limit_beats max_beats,
316 enum ethosu_axi_limit_mem_type memtype,
317 uint8_t max_reads,
318 uint8_t max_writes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200319/**
320 * Set AXI limit parameters for port 1 counter 3.
321 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
322 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
323 * \param[in] max_reads Maximum number of outstanding reads.
324 * \param[in] max_writes Maximum number of outstanding writes.
325 * \return \ref ethosu_error_codes
326 */
Jonny Svärda830f172021-06-07 16:57:00 +0200327enum ethosu_error_codes ethosu_dev_set_axi_limit3(struct ethosu_device *dev,
328 enum ethosu_axi_limit_beats max_beats,
329 enum ethosu_axi_limit_mem_type memtype,
330 uint8_t max_reads,
331 uint8_t max_writes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200332
333/**
334 * Get current command stream queue read position.
Jonny Svärda830f172021-06-07 16:57:00 +0200335 * \return qread position
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200336 */
Jonny Svärda830f172021-06-07 16:57:00 +0200337uint32_t ethosu_dev_get_qread(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200338
339/**
340 * Get revision of NPU
341 * \param[out] revision Pointer to revision read.
342 * \return \ref ethosu_error_codes
343 */
Jonny Svärda830f172021-06-07 16:57:00 +0200344enum ethosu_error_codes ethosu_dev_get_revision(struct ethosu_device *dev, uint32_t *revision);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200345
346/**
347 * Issue run command for the currently programmed
348 * command stream, starting at current queue read
349 * position.
350 * \return \ref ethosu_error_codes
351 */
Jonny Svärda830f172021-06-07 16:57:00 +0200352enum ethosu_error_codes ethosu_dev_set_command_run(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200353
354/**
355 * Dump a 1KB section of SHRAM.
356 * \param[in] section Section offset to 1KB section in SHRAM.
357 * \param[out] shram_p Pointer to a output area, allocated by the
358 * caller, where the SHRAM content shall be
359 * written.
360 * \return \ref ethosu_error_codes
361 */
Jonny Svärda830f172021-06-07 16:57:00 +0200362enum ethosu_error_codes ethosu_dev_get_shram_data(struct ethosu_device *dev, int section, uint32_t *shram_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200363
364/**
365 * Set clock and power q request enable bits.
366 * \param[in] clock_q Clock q ENABLE/DISABLE \ref clock_q_request.
367 * \param[in] power_q Power q ENABLE/DISABLE \ref power_q_request.
368 * \return \ref ethosu_error_codes
369 */
Jonny Svärda830f172021-06-07 16:57:00 +0200370enum ethosu_error_codes ethosu_dev_set_clock_and_power(struct ethosu_device *dev,
371 enum ethosu_clock_q_request clock_q,
372 enum ethosu_power_q_request power_q);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200373
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200374/**
375 * Read register.
376 * \param[in] address Address to read.
377 * \return Register value.
378 */
Jonny Svärda830f172021-06-07 16:57:00 +0200379uint32_t ethosu_dev_read_reg(struct ethosu_device *dev, uint32_t address);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200380
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200381/**
382 * Write register.
383 * \param[in] address Address to read.
384 * \param[in] value Value to be written.
385 */
Jonny Svärda830f172021-06-07 16:57:00 +0200386void ethosu_dev_write_reg(struct ethosu_device *dev, uint32_t address, uint32_t value);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200387
Bhavik Patel5da40922020-07-15 10:06:43 +0200388/**
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200389 * Write register with shadow variable.
390 * \param[in] address Address to read.
391 * \param[in] value Value to be written.
392 */
Jonny Svärda830f172021-06-07 16:57:00 +0200393void ethosu_dev_write_reg_shadow(struct ethosu_device *dev, uint32_t address, uint32_t value, uint32_t *shadow);
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200394
395/**
Bhavik Patel5da40922020-07-15 10:06:43 +0200396 * Save the PMU configuration to ethosu_device struct.
397 * \param[in] dev Ethos-U device where the PMU configuration is
398 * saved.
399 * \return \ref ethosu_error_codes
400 */
Jonny Svärda830f172021-06-07 16:57:00 +0200401enum ethosu_error_codes ethosu_dev_save_pmu_config(struct ethosu_device *dev);
Bhavik Patel5da40922020-07-15 10:06:43 +0200402
403/**
404 * Restore the PMU configuration from a ethosu_device struct.
405 * \param[in] dev Ethos-U device where the PMU configuration is
406 * stored.
407 * \return \ref ethosu_error_codes
408 */
Jonny Svärda830f172021-06-07 16:57:00 +0200409enum ethosu_error_codes ethosu_dev_restore_pmu_config(struct ethosu_device *dev);
Bhavik Patel5da40922020-07-15 10:06:43 +0200410
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200411/**
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200412 * Save PMU counters to shadow variables in memory.
413 * \param[in] dev Ethos-U device where the PMU configuration is
414 * stored.
415 * \return \ref ethosu_error_codes
416 */
Jonny Svärda830f172021-06-07 16:57:00 +0200417enum ethosu_error_codes ethosu_dev_save_pmu_counters(struct ethosu_device *dev);
418
419/**
420 * Check if the PROT register value has changed compared to cached value.
421 * \param[in] dev Ethos-U device to check.
422 * \return true if register value differs from cached value,
423 * false otherwise.
424 */
425bool ethosu_dev_prot_has_changed(struct ethosu_device *dev);
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200426
427/**
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200428 * Check if the STATUS register has any error bits set or not.
429 * \param[in] dev Ethos-U device to check.
430 * \return true if any error bits set,
431 * false otherwise.
432 */
Jonny Svärda830f172021-06-07 16:57:00 +0200433bool ethosu_dev_status_has_error(struct ethosu_device *dev);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200434
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200435#ifdef __cplusplus
436}
437#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200438
439#endif // ETHOSU_DEVICE_H