blob: e08aa3d8a7f26b89268c47257bb1689b8725de58 [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{
57 uintptr_t base_address;
Kristofer Jonssonef387ea2020-08-25 16:32:21 +020058 uint32_t pmcr;
Bhavik Patel5da40922020-07-15 10:06:43 +020059 uint64_t pmccntr;
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];
64 enum ethosu_pmu_event_type pmu_evtypr[ETHOSU_PMU_NCOUNTERS];
Bhavik Pateldae5be02020-06-18 15:25:15 +020065};
66
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020067struct ethosu_id
68{
69 uint32_t version_status; ///< Version status
70 uint32_t version_minor; ///< Version minor
71 uint32_t version_major; ///< Version major
72 uint32_t product_major; ///< Product major
73 uint32_t arch_patch_rev; ///< Architecture version patch
74 uint32_t arch_minor_rev; ///< Architecture version minor
75 uint32_t arch_major_rev; ///< Architecture version major
76};
77
78struct ethosu_config
79{
80 struct
81 {
82 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 };
86};
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 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200145enum ethosu_error_codes ethosu_dev_init(struct ethosu_device *dev, const void *base_address);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200146
147/**
148 * Get device id.
149 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200150enum ethosu_error_codes ethosu_get_id(struct ethosu_device *dev, struct ethosu_id *id);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200151
152/**
153 * Get device configuration.
154 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200155enum ethosu_error_codes ethosu_get_config(struct ethosu_device *dev, struct ethosu_config *config);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200156
157/**
158 * Execute a given command stream on NPU.
159 * \param[in] cmd_stream_ptr Pointer to the command stream
160 * \param[in] cms_length Command stream length
161 * \param[in] base_addr Pointer to array of base addresses
162 * - 0: weight tensor
163 * - 1: scratch tensor
164 * - All input tensors
165 * - All output tensors
166 * \param[in] num_base_addr Number of base addresses.
167 * \return \ref ethosu_error_codes
168 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200169enum ethosu_error_codes ethosu_run_command_stream(struct ethosu_device *dev,
170 const uint8_t *cmd_stream_ptr,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200171 uint32_t cms_length,
172 const uint64_t *base_addr,
173 int num_base_addr);
174
175/**
176 * Check if IRQ is raised.
177 * \param[out] irq_status Pointer to IRQ status
178 * - 0 IRQ not raised
179 * - 1 IRQ raised
180 * \return \ref ethosu_error_codes
181 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200182enum ethosu_error_codes ethosu_is_irq_raised(struct ethosu_device *dev, uint8_t *irq_status);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200183
184/**
185 * Clear IRQ status.
186 * \return \ref ethosu_error_codes
187 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200188enum ethosu_error_codes ethosu_clear_irq_status(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200189
190/**
191 * Get the 16 bit status mask.
192 * \param[out] irq_status_mask Pointer to the status mask.
193 * The lower 16 bits of status reg are returned.
194 * bit0: state
195 * bit1: irq_raised
196 * bit2: bus_status
197 * bit3: reset_status
198 * bit4: cmd_parse_error
199 * bit5: cmd_end_reached
200 * bit6: pmu_irq_raised
201 * bit7-15: reserved
202 * \return \ref ethosu_error_codes
203 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200204enum ethosu_error_codes ethosu_get_status_mask(struct ethosu_device *dev, uint16_t *status_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200205
206/**
207 * Get the 16 bit IRQ history mask.
208 * \param[out] irq_history_mask Pointer to the IRQ history mask.
209 * \return \ref ethosu_error_codes
210 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200211enum ethosu_error_codes ethosu_get_irq_history_mask(struct ethosu_device *dev, uint16_t *irq_history_mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200212
213/**
214 * Clear the given bits in the
215 * IRQ history mask.
216 * \param[in] irq_history_clear_mask 16 bit mask indicating which bits to
217 * clear in the IRQ history mask.
218 * \return \ref ethosu_error_codes
219 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200220enum 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 +0200221
222/**
223 * Perform a NPU soft reset.
224 * \return \ref ethosu_error_codes
225 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200226enum ethosu_error_codes ethosu_soft_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200227
228/**
229 * Wait for reset ready.
230 * \return \ref ethosu_error_codes
231 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200232enum ethosu_error_codes ethosu_wait_for_reset(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200233
234/**
235 * Read and return the content of a given NPU APB
236 * register range.
237 * \param[in] start_address Start address.
238 * \param[in] num_reg Number of registers to read.
239 * \param[out] reg_p Pointer to a output area, allocated by the
240 * caller, where the register content shall be
241 * written.
242 * \return \ref ethosu_error_codes
243 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200244enum ethosu_error_codes ethosu_read_apb_reg(struct ethosu_device *dev,
245 uint32_t start_address,
246 uint16_t num_reg,
247 uint32_t *reg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200248
249/**
250 * Set qconfig register. I.e.
251 * AXI configuration for the command stream.
252 * \param[in] memory_type Memory_type to use for command stream:
253 * enum ethosu_memory_type.
254 * \return \ref ethosu_error_codes
255 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200256enum ethosu_error_codes ethosu_set_qconfig(struct ethosu_device *dev, enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200257
258/**
259 * Set register REGIONCFG.
260 * Base pointer configuration.
261 * Bits[2*k+1:2*k] give the memory type for BASEP[k].
262 * \param[in] region Region field to set: 0 - 7.
263 * \param[in] memory_type Memory_type to use for region: enum ethosu_memory_type.
264 * \return \ref ethosu_error_codes
265 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200266enum ethosu_error_codes ethosu_set_regioncfg(struct ethosu_device *dev,
267 uint8_t region,
268 enum ethosu_memory_type memory_type);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200269
270/**
271 * Set AXI limit parameters for port 0 counter 0.
272 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
273 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
274 * \param[in] max_reads Maximum number of outstanding reads.
275 * \param[in] max_writes Maximum number of outstanding writes.
276 * \return \ref ethosu_error_codes
277 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200278enum ethosu_error_codes ethosu_set_axi_limit0(struct ethosu_device *dev,
279 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200280 enum ethosu_axi_limit_mem_type memtype,
281 uint8_t max_reads,
282 uint8_t max_writes);
283/**
284 * Set AXI limit parameters for port 0 counter 1.
285 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
286 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
287 * \param[in] max_reads Maximum number of outstanding reads.
288 * \param[in] max_writes Maximum number of outstanding writes.
289 * \return \ref ethosu_error_codes
290 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200291enum ethosu_error_codes ethosu_set_axi_limit1(struct ethosu_device *dev,
292 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200293 enum ethosu_axi_limit_mem_type memtype,
294 uint8_t max_reads,
295 uint8_t max_writes);
296/**
297 * Set AXI limit parameters for port 1 counter 2.
298 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
299 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
300 * \param[in] max_reads Maximum number of outstanding reads.
301 * \param[in] max_writes Maximum number of outstanding writes.
302 * \return \ref ethosu_error_codes
303 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200304enum ethosu_error_codes ethosu_set_axi_limit2(struct ethosu_device *dev,
305 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200306 enum ethosu_axi_limit_mem_type memtype,
307 uint8_t max_reads,
308 uint8_t max_writes);
309/**
310 * Set AXI limit parameters for port 1 counter 3.
311 * \param[in] max_beats Burst split alignment, \ref ethosu_axi_limit_beats.
312 * \param[in] memtype Cache policy \ref ethosu_axi_limit_mem_type
313 * \param[in] max_reads Maximum number of outstanding reads.
314 * \param[in] max_writes Maximum number of outstanding writes.
315 * \return \ref ethosu_error_codes
316 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200317enum ethosu_error_codes ethosu_set_axi_limit3(struct ethosu_device *dev,
318 enum ethosu_axi_limit_beats max_beats,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200319 enum ethosu_axi_limit_mem_type memtype,
320 uint8_t max_reads,
321 uint8_t max_writes);
322
323/**
324 * Get current command stream queue read position.
325 * \param[out] qread Pointer to queue read.
326 * \return \ref ethosu_error_codes
327 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200328enum ethosu_error_codes ethosu_get_qread(struct ethosu_device *dev, uint32_t *qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200329
330/**
331 * Get revision of NPU
332 * \param[out] revision Pointer to revision read.
333 * \return \ref ethosu_error_codes
334 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200335enum ethosu_error_codes ethosu_get_revision(struct ethosu_device *dev, uint32_t *revision);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200336
337/**
338 * Issue run command for the currently programmed
339 * command stream, starting at current queue read
340 * position.
341 * \return \ref ethosu_error_codes
342 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200343enum ethosu_error_codes ethosu_set_command_run(struct ethosu_device *dev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200344
345/**
346 * Dump a 1KB section of SHRAM.
347 * \param[in] section Section offset to 1KB section in SHRAM.
348 * \param[out] shram_p Pointer to a output area, allocated by the
349 * caller, where the SHRAM content shall be
350 * written.
351 * \return \ref ethosu_error_codes
352 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200353enum ethosu_error_codes ethosu_get_shram_data(struct ethosu_device *dev, int section, uint32_t *shram_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200354
355/**
356 * Set clock and power q request enable bits.
357 * \param[in] clock_q Clock q ENABLE/DISABLE \ref clock_q_request.
358 * \param[in] power_q Power q ENABLE/DISABLE \ref power_q_request.
359 * \return \ref ethosu_error_codes
360 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200361enum ethosu_error_codes ethosu_set_clock_and_power(struct ethosu_device *dev,
362 enum ethosu_clock_q_request clock_q,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200363 enum ethosu_power_q_request power_q);
364
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200365/**
366 * Read register.
367 * \param[in] address Address to read.
368 * \return Register value.
369 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200370uint32_t ethosu_read_reg(struct ethosu_device *dev, uint32_t address);
371
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200372/**
373 * Write register.
374 * \param[in] address Address to read.
375 * \param[in] value Value to be written.
376 */
Bhavik Pateldae5be02020-06-18 15:25:15 +0200377void ethosu_write_reg(struct ethosu_device *dev, uint32_t address, uint32_t value);
378
Bhavik Patel5da40922020-07-15 10:06:43 +0200379/**
380 * Save the PMU configuration to ethosu_device struct.
381 * \param[in] dev Ethos-U device where the PMU configuration is
382 * saved.
383 * \return \ref ethosu_error_codes
384 */
385enum ethosu_error_codes ethosu_save_pmu_config(struct ethosu_device *dev);
386
387/**
388 * Restore the PMU configuration from a ethosu_device struct.
389 * \param[in] dev Ethos-U device where the PMU configuration is
390 * stored.
391 * \return \ref ethosu_error_codes
392 */
393enum ethosu_error_codes ethosu_restore_pmu_config(struct ethosu_device *dev);
394
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200395#ifdef __cplusplus
396}
397#endif
Kristofer Jonsson3c439172020-08-05 09:38:40 +0200398
399#endif // ETHOSU_DEVICE_H