blob: 35f79248fca71ddad7465405d4e9c64d9ad953e6 [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
37#define ETHOSU_DRIVER_VERSION_MAJOR 0 ///< Driver major version
Douglas Trohaf6a85da2020-05-11 11:45:28 +020038#define ETHOSU_DRIVER_VERSION_MINOR 16 ///< Driver minor version
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020039#define ETHOSU_DRIVER_VERSION_PATCH 0 ///< Driver patch version
40#define ETHOSU_DRIVER_BASEP_INDEXES 8 ///< Number of base pointer indexes
41
Anton Moberg61da4d32020-12-22 16:00:31 +010042#ifndef ETHOSU_PMU_NCOUNTERS
43#define ETHOSU_PMU_NCOUNTERS 4
44#endif
45
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020046/******************************************************************************
47 * Types
48 ******************************************************************************/
49
50enum ethosu_error_codes
51{
52 ETHOSU_SUCCESS = 0, ///< Success
53 ETHOSU_GENERIC_FAILURE = -1, ///< Generic failure
54 ETHOSU_INVALID_PARAM = -2 ///< Invalid parameter
55};
56
Bhavik Pateldae5be02020-06-18 15:25:15 +020057struct ethosu_device
58{
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020059 volatile uint32_t *base_address;
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +010060 uint32_t proto;
Kristofer Jonssonef387ea2020-08-25 16:32:21 +020061 uint32_t pmcr;
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020062 uint32_t pmccntr[2];
Kristofer Jonssonef387ea2020-08-25 16:32:21 +020063 uint32_t pmcnten;
64 uint32_t pmint;
65 uint32_t pmccntr_cfg;
Bhavik Patel5da40922020-07-15 10:06:43 +020066 uint32_t pmu_evcntr[ETHOSU_PMU_NCOUNTERS];
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +020067 uint32_t pmu_evtypr[ETHOSU_PMU_NCOUNTERS];
Per Åstrande6498f02020-11-09 15:33:12 +010068 uint32_t secure;
69 uint32_t privileged;
Bhavik Pateldae5be02020-06-18 15:25:15 +020070};
71
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020072struct ethosu_id
73{
74 uint32_t version_status; ///< Version status
75 uint32_t version_minor; ///< Version minor
76 uint32_t version_major; ///< Version major
77 uint32_t product_major; ///< Product major
78 uint32_t arch_patch_rev; ///< Architecture version patch
79 uint32_t arch_minor_rev; ///< Architecture version minor
80 uint32_t arch_major_rev; ///< Architecture version major
81};
82
83struct ethosu_config
84{
85 struct
86 {
87 uint32_t macs_per_cc; ///< MACs per clock cycle
88 uint32_t cmd_stream_version; ///< NPU command stream version
89 uint32_t shram_size; ///< SHRAM size
Anton Mobergb8bcf132021-03-29 10:02:25 +020090 uint32_t custom_dma; ///< Custom DMA enabled
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020091 };
92};
93
94/**
95 * Memory type parameter for set_regioncfg_reg:
96 * Counter{0,1}: Outstanding transactions for
97 * AXI port 0 for memory type/region a=0,b=1
98 * Counter{2,3}: Outstanding transactions for
99 * AXI port 1 for memory type/region a=2,b=3
100 */
101enum ethosu_memory_type
102{
103 ETHOSU_AXI0_OUTSTANDING_COUNTER0 = 0, ///< NPU axi0_outstanding_counter0
104 ETHOSU_AXI0_OUTSTANDING_COUNTER1 = 1, ///< NPU axi0_outstanding_counter1
105 ETHOSU_AXI1_OUTSTANDING_COUNTER2 = 2, ///< NPU axi1_outstanding_counter2
106 ETHOSU_AXI1_OUTSTANDING_COUNTER3 = 3 ///< NPU axi1_outstanding_counter3
107};
108
109enum ethosu_axi_limit_beats
110{
111 ETHOSU_AXI_LIMIT_64_BYTES = 0, ///< NPU AXI limit 64 byte burst split alignment.
112 ETHOSU_AXI_LIMIT_128_BYTES = 1, ///< NPU AXI limit 128 byte burst split alignment.
113 ETHOSU_AXI_LIMIT_256_BYTES = 2 ///< NPU AXI limit 256 byte burst split alignment.
114};
115
116enum ethosu_axi_limit_mem_type
117{
118 ETHOSU_MEM_TYPE_DEVICE_NON_BUFFERABLE = 0,
119 ETHOSU_MEM_TYPE_DEVICE_BUFFERABLE = 1,
120 ETHOSU_MEM_TYPE_NORMAL_NON_CACHEABLE_NON_BUFFERABLE = 2,
121 ETHOSU_MEM_TYPE_NORMAL_NON_CACHEABLE_BUFFERABLE = 3,
122 ETHOSU_MEM_TYPE_WRITE_THROUGH_NO_ALLOCATE = 4,
123 ETHOSU_MEM_TYPE_WRITE_THROUGH_READ_ALLOCATE = 5,
124 ETHOSU_MEM_TYPE_WRITE_THROUGH_WRITE_ALLOCATE = 6,
125 ETHOSU_MEM_TYPE_WRITE_THROUGH_READ_AND_WRITE_ALLOCATE = 7,
126 ETHOSU_MEM_TYPE_WRITE_BACK_NO_ALLOCATE = 8,
127 ETHOSU_MEM_TYPE_WRITE_BACK_READ_ALLOCATE = 9,
128 ETHOSU_MEM_TYPE_WRITE_BACK_WRITE_ALLOCATE = 10,
129 ETHOSU_MEM_TYPE_WRITE_BACK_READ_AND_WRITE_ALLOCATE = 11
130};
131
132enum ethosu_clock_q_request
133{
134 ETHOSU_CLOCK_Q_DISABLE = 0, ///< Disble NPU signal ready for clock off.
135 ETHOSU_CLOCK_Q_ENABLE = 1 ///< Enable NPU signal ready for clock off when stop+idle state reached.
136};
137
138enum ethosu_power_q_request
139{
140 ETHOSU_POWER_Q_DISABLE = 0, ///< Disble NPU signal ready for power off.
141 ETHOSU_POWER_Q_ENABLE = 1 ///< Enable NPU signal ready for power off when stop+idle state reached.
142};
143
144/******************************************************************************
145 * Prototypes
146 ******************************************************************************/
147
148/**
149 * Initialize the device.
150 */
Per Åstrande6498f02020-11-09 15:33:12 +0100151enum ethosu_error_codes ethosu_dev_init(struct ethosu_device *dev,
152 const void *base_address,
153 uint32_t secure_enable,
154 uint32_t privilege_enable);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200155
156/**
157 * Get device id.
158 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200159enum ethosu_error_codes ethosu_get_id(struct ethosu_device *dev, struct ethosu_id *id);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200160
161/**
162 * Get device configuration.
163 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200164enum ethosu_error_codes ethosu_get_config(struct ethosu_device *dev, struct ethosu_config *config);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200165
166/**
167 * Execute a given command stream on NPU.
168 * \param[in] cmd_stream_ptr Pointer to the command stream
169 * \param[in] cms_length Command stream length
170 * \param[in] base_addr Pointer to array of base addresses
171 * - 0: weight tensor
172 * - 1: scratch tensor
173 * - All input tensors
174 * - All output tensors
175 * \param[in] num_base_addr Number of base addresses.
176 * \return \ref ethosu_error_codes
177 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200178enum ethosu_error_codes ethosu_run_command_stream(struct ethosu_device *dev,
179 const uint8_t *cmd_stream_ptr,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200180 uint32_t cms_length,
181 const uint64_t *base_addr,
182 int num_base_addr);
183
184/**
185 * Check if IRQ is raised.
186 * \param[out] irq_status Pointer to IRQ status
187 * - 0 IRQ not raised
188 * - 1 IRQ raised
189 * \return \ref ethosu_error_codes
190 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200191enum ethosu_error_codes ethosu_is_irq_raised(struct ethosu_device *dev, uint8_t *irq_status);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200192
193/**
194 * Clear IRQ status.
195 * \return \ref ethosu_error_codes
196 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200197enum ethosu_error_codes ethosu_clear_irq_status(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200198
199/**
200 * Get the 16 bit status mask.
201 * \param[out] irq_status_mask Pointer to the status mask.
202 * The lower 16 bits of status reg are returned.
203 * bit0: state
204 * bit1: irq_raised
205 * bit2: bus_status
206 * bit3: reset_status
207 * bit4: cmd_parse_error
208 * bit5: cmd_end_reached
209 * bit6: pmu_irq_raised
210 * bit7-15: reserved
211 * \return \ref ethosu_error_codes
212 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200213enum ethosu_error_codes ethosu_get_status_mask(struct ethosu_device *dev, uint16_t *status_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200214
215/**
216 * Get the 16 bit IRQ history mask.
217 * \param[out] irq_history_mask Pointer to the IRQ history mask.
218 * \return \ref ethosu_error_codes
219 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200220enum ethosu_error_codes ethosu_get_irq_history_mask(struct ethosu_device *dev, uint16_t *irq_history_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200221
222/**
223 * Clear the given bits in the
224 * IRQ history mask.
225 * \param[in] irq_history_clear_mask 16 bit mask indicating which bits to
226 * clear in the IRQ history mask.
227 * \return \ref ethosu_error_codes
228 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200229enum 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 +0200230
231/**
232 * Perform a NPU soft reset.
233 * \return \ref ethosu_error_codes
234 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200235enum ethosu_error_codes ethosu_soft_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200236
237/**
238 * Wait for reset ready.
239 * \return \ref ethosu_error_codes
240 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200241enum ethosu_error_codes ethosu_wait_for_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200242
243/**
244 * Read and return the content of a given NPU APB
245 * register range.
246 * \param[in] start_address Start address.
247 * \param[in] num_reg Number of registers to read.
248 * \param[out] reg_p Pointer to a output area, allocated by the
249 * caller, where the register content shall be
250 * written.
251 * \return \ref ethosu_error_codes
252 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200253enum ethosu_error_codes ethosu_read_apb_reg(struct ethosu_device *dev,
254 uint32_t start_address,
255 uint16_t num_reg,
256 uint32_t *reg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200257
258/**
259 * Set qconfig register. I.e.
260 * AXI configuration for the command stream.
261 * \param[in] memory_type Memory_type to use for command stream:
262 * enum ethosu_memory_type.
263 * \return \ref ethosu_error_codes
264 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200265enum ethosu_error_codes ethosu_set_qconfig(struct ethosu_device *dev, enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200266
267/**
268 * Set register REGIONCFG.
269 * Base pointer configuration.
270 * Bits[2*k+1:2*k] give the memory type for BASEP[k].
271 * \param[in] region Region field to set: 0 - 7.
272 * \param[in] memory_type Memory_type to use for region: enum ethosu_memory_type.
273 * \return \ref ethosu_error_codes
274 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200275enum ethosu_error_codes ethosu_set_regioncfg(struct ethosu_device *dev,
276 uint8_t region,
277 enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200278
279/**
280 * Set AXI limit parameters for port 0 counter 0.
281 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
282 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
283 * \param[in] max_reads Maximum number of outstanding reads.
284 * \param[in] max_writes Maximum number of outstanding writes.
285 * \return \ref ethosu_error_codes
286 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200287enum ethosu_error_codes ethosu_set_axi_limit0(struct ethosu_device *dev,
288 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200289 enum ethosu_axi_limit_mem_type memtype,
290 uint8_t max_reads,
291 uint8_t max_writes);
292/**
293 * Set AXI limit parameters for port 0 counter 1.
294 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
295 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
296 * \param[in] max_reads Maximum number of outstanding reads.
297 * \param[in] max_writes Maximum number of outstanding writes.
298 * \return \ref ethosu_error_codes
299 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200300enum ethosu_error_codes ethosu_set_axi_limit1(struct ethosu_device *dev,
301 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200302 enum ethosu_axi_limit_mem_type memtype,
303 uint8_t max_reads,
304 uint8_t max_writes);
305/**
306 * Set AXI limit parameters for port 1 counter 2.
307 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
308 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
309 * \param[in] max_reads Maximum number of outstanding reads.
310 * \param[in] max_writes Maximum number of outstanding writes.
311 * \return \ref ethosu_error_codes
312 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200313enum ethosu_error_codes ethosu_set_axi_limit2(struct ethosu_device *dev,
314 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200315 enum ethosu_axi_limit_mem_type memtype,
316 uint8_t max_reads,
317 uint8_t max_writes);
318/**
319 * Set AXI limit parameters for port 1 counter 3.
320 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
321 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
322 * \param[in] max_reads Maximum number of outstanding reads.
323 * \param[in] max_writes Maximum number of outstanding writes.
324 * \return \ref ethosu_error_codes
325 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200326enum ethosu_error_codes ethosu_set_axi_limit3(struct ethosu_device *dev,
327 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200328 enum ethosu_axi_limit_mem_type memtype,
329 uint8_t max_reads,
330 uint8_t max_writes);
331
332/**
333 * Get current command stream queue read position.
334 * \param[out] qread Pointer to queue read.
335 * \return \ref ethosu_error_codes
336 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200337enum ethosu_error_codes ethosu_get_qread(struct ethosu_device *dev, uint32_t *qread);
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 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200344enum ethosu_error_codes ethosu_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 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200352enum ethosu_error_codes ethosu_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 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200362enum ethosu_error_codes ethosu_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 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200370enum ethosu_error_codes ethosu_set_clock_and_power(struct ethosu_device *dev,
371 enum ethosu_clock_q_request clock_q,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200372 enum ethosu_power_q_request power_q);
373
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200374/**
375 * Read register.
376 * \param[in] address Address to read.
377 * \return Register value.
378 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200379uint32_t ethosu_read_reg(struct ethosu_device *dev, uint32_t address);
380
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 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200386void ethosu_write_reg(struct ethosu_device *dev, uint32_t address, uint32_t value);
387
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 */
393void ethosu_write_reg_shadow(struct ethosu_device *dev, uint32_t address, uint32_t value, uint32_t *shadow);
394
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 */
401enum ethosu_error_codes ethosu_save_pmu_config(struct ethosu_device *dev);
402
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 */
409enum ethosu_error_codes ethosu_restore_pmu_config(struct ethosu_device *dev);
410
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 */
417enum ethosu_error_codes ethosu_save_pmu_counters(struct ethosu_device *dev);
418
419/**
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200420 * Check if the STATUS register has any error bits set or not.
421 * \param[in] dev Ethos-U device to check.
422 * \return true if any error bits set,
423 * false otherwise.
424 */
425bool ethosu_status_has_error(struct ethosu_device *dev);
426
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200427#ifdef __cplusplus
428}
429#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200430
431#endif // ETHOSU_DEVICE_H