blob: 3b6ebcf12d26383342d59f72372a9241dee103d9 [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 Jonsson2b201c32020-09-02 16:42:43 +020019/******************************************************************************
20 * Includes
21 ******************************************************************************/
22
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020023#include "ethosu_driver.h"
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020024#include "ethosu_common.h"
Bhavik Pateldae5be02020-06-18 15:25:15 +020025#include "ethosu_config.h"
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020026#include "ethosu_device.h"
Per Åstrand25d78c02020-04-21 14:19:44 +020027
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020028#include <assert.h>
Per Åstrand25d78c02020-04-21 14:19:44 +020029#include <cmsis_compiler.h>
Per Åstrand14ccfee2020-09-25 10:40:20 +020030#include <inttypes.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020031#include <stdbool.h>
Bhavik Patelbf7ae632020-06-11 21:00:16 +020032#include <stddef.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020033#include <stdio.h>
34#include <stdlib.h>
35
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020036/******************************************************************************
37 * Defines
38 ******************************************************************************/
39
40#define MACS_PER_CYCLE_LOG2_MASK 0x000F
41#define SHRAM_SIZE_MASK 0xFF00
42#define SHRAM_SIZE_RIGHT_SHIFT 8
43#define BYTES_IN_32_BITS 4
44#define CUSTOM_OPTION_LENGTH_32_BIT_WORD 1
45#define DRIVER_ACTION_LENGTH_32_BIT_WORD 1
46#define OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD 2
47#define ETHOSU_FOURCC ('1' << 24 | 'P' << 16 | 'O' << 8 | 'C') // "Custom Operator Payload 1"
48#define APB_START_ADDR_MASK 0x0FFF
49#define APB_NUM_REG_BIT_SHIFT 12
50#define BYTES_1KB 1024
51#define PRODUCT_MAJOR_ETHOSU55 (4)
52#define MASK_16_BYTE_ALIGN (0xF)
53#define FAST_MEMORY_BASE_ADDR_INDEX 2
54
55/******************************************************************************
56 * Types
57 ******************************************************************************/
58
59// Driver actions
60enum DRIVER_ACTION_e
61{
62 RESERVED = 0,
63 OPTIMIZER_CONFIG = 1,
64 COMMAND_STREAM = 2,
65 READ_APB_REG = 3,
66 DUMP_SHRAM = 4,
67 NOP = 5,
68};
69
70// Custom data struct
71struct custom_data_s
72{
73 union
74 {
75 // Driver action data
76 struct
77 {
78 // Driver action command (valid values in DRIVER_ACTION_e)
79 uint8_t driver_action_command;
80
81 // reserved
82 uint8_t reserved;
83
84 // Driver action data
85 union
86 {
87 // DA_CMD_OPT_CFG
88 struct
89 {
90 uint16_t rel_nbr : 4;
91 uint16_t patch_nbr : 4;
92 uint16_t opt_cfg_reserved : 8;
93 };
94
95 // DA_CMD_CMSTRM
96 struct
97 {
98 uint16_t length;
99 };
100
101 // DA_CMD_READAPB
102 struct
103 {
104 uint16_t start_address : 12;
105 uint16_t nbr_reg_minus1 : 4;
106 };
107
108 uint16_t driver_action_data;
109 };
110 };
111
112 uint32_t word;
113 };
114};
115
116// optimizer config struct
117struct opt_cfg_s
118{
119 struct custom_data_s da_data;
120 union
121 {
122 struct
123 {
124 uint32_t macs_per_cc : 4;
125 uint32_t cmd_stream_version : 4;
126 uint32_t shram_size : 8;
127 uint32_t reserved1 : 16;
128 };
129 uint32_t npu_cfg;
130 };
131 union
132 {
133 struct
134 {
135 uint32_t version_status : 4;
136 uint32_t version_minor : 4;
137 uint32_t version_major : 4;
138 uint32_t product_major : 4;
139 uint32_t arch_patch_rev : 4;
140 uint32_t arch_minor_rev : 8;
141 uint32_t arch_major_rev : 4;
142 };
143 uint32_t ethosu_id;
144 };
145};
146
147/******************************************************************************
148 * Functions
149 ******************************************************************************/
150
Kristofer Jonssonef387ea2020-08-25 16:32:21 +0200151struct ethosu_driver ethosu_drv = {
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100152 .dev = {.base_address = NULL, .proto = 0, .pmccntr = {0}, .pmu_evcntr = {0, 0, 0, 0}, .pmu_evtypr = {0, 0, 0, 0}},
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200153 .abort_inference = false,
154 .status_error = false};
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200155
156// IRQ
157static volatile bool irq_triggered = false;
158#if defined(CPU_CORTEX_M3) || defined(CPU_CORTEX_M4) || defined(CPU_CORTEX_M7) || defined(CPU_CORTEX_M33) || \
159 defined(CPU_CORTEX_M55)
Per Åstrand25d78c02020-04-21 14:19:44 +0200160void ethosu_irq_handler(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200161{
162 uint8_t irq_raised = 0;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200163
164 LOG_DEBUG("Interrupt. status=0x%08x, qread=%d\n",
165 ethosu_read_reg(&ethosu_drv.dev, NPU_REG_STATUS),
166 ethosu_read_reg(&ethosu_drv.dev, NPU_REG_QREAD));
167
168 // Verify that interrupt has been raised
Bhavik Pateldae5be02020-06-18 15:25:15 +0200169 (void)ethosu_is_irq_raised(&ethosu_drv.dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200170 ASSERT(irq_raised == 1);
171 irq_triggered = true;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200172
173 // Clear interrupt
Bhavik Pateldae5be02020-06-18 15:25:15 +0200174 (void)ethosu_clear_irq_status(&ethosu_drv.dev);
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200175
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200176 // Verify that interrupt has been successfully cleared
Bhavik Pateldae5be02020-06-18 15:25:15 +0200177 (void)ethosu_is_irq_raised(&ethosu_drv.dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200178 ASSERT(irq_raised == 0);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200179
180 if (ethosu_status_has_error(&ethosu_drv.dev))
181 {
Per Åstrand849cf692020-11-24 07:39:55 +0100182 (void)ethosu_soft_reset(&ethosu_drv.dev);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200183 ethosu_drv.status_error = true;
184 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200185}
186
Bhavik Pateldae5be02020-06-18 15:25:15 +0200187static inline void wait_for_irq(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200188{
189 while (1)
190 {
191 __disable_irq();
Bhavik Pateldae5be02020-06-18 15:25:15 +0200192 if (irq_triggered || drv->abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200193 {
194 __enable_irq();
195 break;
196 }
197
Per Åstrand25d78c02020-04-21 14:19:44 +0200198 __WFI();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200199
200 __enable_irq();
201 }
202}
203#else
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200204// Just polling the status register
Bhavik Pateldae5be02020-06-18 15:25:15 +0200205static inline void wait_for_irq(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200206{
207 uint8_t irq_raised = 0;
208
209 for (int i = 0; i < 5000; ++i)
210 {
Bhavik Pateldae5be02020-06-18 15:25:15 +0200211 (void)ethosu_is_irq_raised(&drv->dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200212 if (1 == irq_raised)
213 {
214 break;
215 }
216 }
217 ASSERT(1 == irq_raised);
218
219 irq_triggered = true;
220}
221#endif
222
Bhavik Pateldae5be02020-06-18 15:25:15 +0200223static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p);
224static int handle_command_stream(struct ethosu_driver *drv,
225 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200226 const int cms_length,
227 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200228 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200229 const int num_base_addr);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200230static int read_apb_reg(struct ethosu_driver *drv, uint16_t);
231static int dump_shram(struct ethosu_driver *drv);
232static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200233static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200234static void npu_axi_init(struct ethosu_driver *drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200235
Per Åstrande6498f02020-11-09 15:33:12 +0100236int ethosu_init_v3(const void *base_address,
237 const void *fast_memory,
238 const size_t fast_memory_size,
239 uint32_t secure_enable,
240 uint32_t privilege_enable)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200241{
242 int return_code = 0;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200243
Per Åstrande6498f02020-11-09 15:33:12 +0100244 LOG_INFO("%s. base_address=%p, fast_memory=%p, fast_memory_size=%zu, secure=%u, privileged=%u\n",
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200245 __FUNCTION__,
246 base_address,
247 fast_memory,
Per Åstrande6498f02020-11-09 15:33:12 +0100248 fast_memory_size,
249 secure_enable,
250 privilege_enable);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200251
Per Åstrandc6c1db12020-09-28 08:41:45 +0200252 ethosu_drv.fast_memory = (uint32_t)fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200253 ethosu_drv.fast_memory_size = fast_memory_size;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200254
Per Åstrande6498f02020-11-09 15:33:12 +0100255 if (ETHOSU_SUCCESS != ethosu_dev_init(&ethosu_drv.dev, base_address, secure_enable, privilege_enable))
Bhavik Pateldae5be02020-06-18 15:25:15 +0200256 {
257 LOG_ERR("Failed in ethosu_dev_init");
258 return -1;
259 }
260
261 if (ETHOSU_SUCCESS != ethosu_set_clock_and_power(&ethosu_drv.dev, ETHOSU_CLOCK_Q_DISABLE, ETHOSU_POWER_Q_DISABLE))
Bhavik Patele645fed2020-06-12 14:46:47 +0200262 {
263 LOG_ERR("Failed to disable clock-q & power-q for Ethos-U\n");
264 return -1;
265 }
266
Per Åstrand849cf692020-11-24 07:39:55 +0100267 if (ETHOSU_SUCCESS != ethosu_soft_reset(&ethosu_drv.dev))
268 {
269 return -1;
270 }
Kristofer Jonssondaa0d202020-05-12 12:23:16 +0200271
Bhavik Pateldae5be02020-06-18 15:25:15 +0200272 if (ETHOSU_SUCCESS != ethosu_wait_for_reset(&ethosu_drv.dev))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200273 {
274 LOG_ERR("Failed reset of Ethos-U\n");
275 return -1;
276 }
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200277 ethosu_drv.status_error = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200278
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200279 return return_code;
280}
281
282int ethosu_get_version(struct ethosu_version *version)
283{
284 int return_code = 0;
285
286 if (NULL != version)
287 {
288 struct ethosu_id id;
289 struct ethosu_config cfg;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200290 (void)ethosu_get_id(&ethosu_drv.dev, &id);
291 (void)ethosu_get_config(&ethosu_drv.dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200292
293 version->id.version_status = id.version_status;
294 version->id.version_minor = id.version_minor;
295 version->id.version_major = id.version_major;
296 version->id.product_major = id.product_major;
297 version->id.arch_patch_rev = id.arch_patch_rev;
298 version->id.arch_minor_rev = id.arch_minor_rev;
299 version->id.arch_major_rev = id.arch_major_rev;
300 version->id.driver_patch_rev = ETHOSU_DRIVER_VERSION_PATCH;
301 version->id.driver_minor_rev = ETHOSU_DRIVER_VERSION_MINOR;
302 version->id.driver_major_rev = ETHOSU_DRIVER_VERSION_MAJOR;
303 version->cfg.macs_per_cc = cfg.macs_per_cc;
304 version->cfg.cmd_stream_version = cfg.cmd_stream_version;
305 version->cfg.shram_size = cfg.shram_size;
306 }
307 else
308 {
309 return_code = -1;
310 }
311
312 return return_code;
313}
314
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200315int ethosu_invoke_v2(const void *custom_data_ptr,
316 const int custom_data_size,
317 const uint64_t *base_addr,
318 const size_t *base_addr_size,
319 const int num_base_addr)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200320{
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200321 const struct custom_data_s *data_ptr = custom_data_ptr;
322 const struct custom_data_s *data_end = custom_data_ptr + custom_data_size;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200323 int return_code = 0;
324
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200325 LOG_INFO("%s\n", __FUNCTION__);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200326
327 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200328 if (data_ptr->word != ETHOSU_FOURCC)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200329 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200330 LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x\n", data_ptr->word, ETHOSU_FOURCC);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200331 return -1;
332 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200333
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200334 // Custom data length must be a multiple of 32 bits
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200335 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
336 {
337 LOG_ERR("ethosu_invoke ERROR custom_data_size=0x%x not a multiple of 4\n", custom_data_size);
338 return -1;
339 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200340
341 ++data_ptr;
342
343 // Adjust base address to fast memory area
Per Åstrandc8019012020-09-28 08:44:42 +0200344 if (ethosu_drv.fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200345 {
346 uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX];
347
348 if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > ethosu_drv.fast_memory_size)
349 {
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100350 LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u\n",
351 ethosu_drv.fast_memory_size,
352 base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]);
353 return -1;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200354 }
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100355
356 *fast_memory = ethosu_drv.fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200357 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200358
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100359 // Only soft reset if securty state or privilege level needs changing
360 if (ethosu_drv.dev.proto != ethosu_read_reg(&ethosu_drv.dev, NPU_REG_PROT))
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200361 {
Per Åstrand849cf692020-11-24 07:39:55 +0100362 if (ETHOSU_SUCCESS != ethosu_soft_reset(&ethosu_drv.dev))
363 {
364 return -1;
365 }
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200366 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100367
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200368 ethosu_drv.status_error = false;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200369 ethosu_set_clock_and_power(&ethosu_drv.dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
Bhavik Patel5da40922020-07-15 10:06:43 +0200370 ethosu_restore_pmu_config(&ethosu_drv.dev);
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200371
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200372 while (data_ptr < data_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200373 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200374 int ret = 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200375 switch (data_ptr->driver_action_command)
376 {
377 case OPTIMIZER_CONFIG:
378 LOG_INFO("ethosu_invoke OPTIMIZER_CONFIG\n");
379 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
380
Bhavik Pateldae5be02020-06-18 15:25:15 +0200381 ret = handle_optimizer_config(&ethosu_drv, opt_cfg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200382 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
383 break;
384 case COMMAND_STREAM:
385 LOG_INFO("ethosu_invoke COMMAND_STREAM\n");
386 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct custom_data_s);
387 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
388
Bhavik Pateldae5be02020-06-18 15:25:15 +0200389 ethosu_drv.abort_inference = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200390 // It is safe to clear this flag without atomic, because npu is not running.
391 irq_triggered = false;
392
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200393 ret = handle_command_stream(
394 &ethosu_drv, command_stream, cms_length, base_addr, base_addr_size, num_base_addr);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200395
Bhavik Pateldae5be02020-06-18 15:25:15 +0200396 if (return_code == -1 && ethosu_drv.abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200397 {
398 uint32_t qread = 0;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200399 ethosu_get_qread(&ethosu_drv.dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200400 LOG_ERR("NPU timeout\n");
401 dump_command_stream(command_stream, cms_length, qread);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200402 dump_npu_register(&ethosu_drv, 0x200, 0x2BF);
403 dump_npu_register(&ethosu_drv, 0x800, 0xB3F);
404 dump_shram(&ethosu_drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200405 }
406
407 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
408 break;
409 case READ_APB_REG:
410 LOG_INFO("ethosu_invoke READ_APB_REG\n");
Bhavik Pateldae5be02020-06-18 15:25:15 +0200411 ret = read_apb_reg(&ethosu_drv, data_ptr->driver_action_data);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200412 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
413 break;
414 case DUMP_SHRAM:
415 LOG_INFO("ethosu_invoke DUMP_SHRAM\n");
Bhavik Pateldae5be02020-06-18 15:25:15 +0200416 ret = dump_shram(&ethosu_drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200417 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
418 break;
419 case NOP:
420 LOG_INFO("ethosu_invoke NOP\n");
421 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
422 break;
423 default:
424 LOG_ERR("ethosu_invoke UNSUPPORTED driver_action_command %d \n", data_ptr->driver_action_command);
Bhavik Patele645fed2020-06-12 14:46:47 +0200425 ret = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200426 break;
427 }
Bhavik Patele645fed2020-06-12 14:46:47 +0200428 if (ret != 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200429 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200430 return_code = -1;
431 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200432 }
433 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200434
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200435 if (!ethosu_drv.status_error)
436 {
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200437 ethosu_save_pmu_counters(&ethosu_drv.dev);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200438 ethosu_set_clock_and_power(&ethosu_drv.dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
439 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200440
Bhavik Patele645fed2020-06-12 14:46:47 +0200441 return return_code;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200442}
443
444void ethosu_abort(void)
445{
Bhavik Pateldae5be02020-06-18 15:25:15 +0200446 ethosu_drv.abort_inference = true;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200447}
448
Bhavik Pateldae5be02020-06-18 15:25:15 +0200449static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200450{
451 struct ethosu_config cfg;
452 struct ethosu_id id;
453 int return_code = 0;
454
455 LOG_INFO("handle_optimizer_config:\n");
456 LOG_INFO("Optimizer release nbr: %d patch: %d\n", opt_cfg_p->da_data.rel_nbr, opt_cfg_p->da_data.patch_nbr);
457 LOG_INFO("Optimizer config cmd_stream_version: %d macs_per_cc: %d shram_size: %d\n",
458 opt_cfg_p->cmd_stream_version,
459 opt_cfg_p->macs_per_cc,
460 opt_cfg_p->shram_size);
461 LOG_INFO("Optimizer config Ethos-U version: %d.%d.%d\n",
462 opt_cfg_p->arch_major_rev,
463 opt_cfg_p->arch_minor_rev,
464 opt_cfg_p->arch_patch_rev);
465
Bhavik Pateldae5be02020-06-18 15:25:15 +0200466 (void)ethosu_get_config(&drv->dev, &cfg);
467 (void)ethosu_get_id(&drv->dev, &id);
Per Åstrand14ccfee2020-09-25 10:40:20 +0200468 LOG_INFO("Ethos-U config cmd_stream_version: %" PRIu32 " macs_per_cc: %" PRIu32 " shram_size: %" PRIu32 "\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200469 cfg.cmd_stream_version,
470 cfg.macs_per_cc,
471 cfg.shram_size);
Per Åstrand14ccfee2020-09-25 10:40:20 +0200472 LOG_INFO("Ethos-U version: %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
473 id.arch_major_rev,
474 id.arch_minor_rev,
475 id.arch_patch_rev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200476
477 if ((cfg.macs_per_cc != opt_cfg_p->macs_per_cc) || (cfg.shram_size != opt_cfg_p->shram_size) ||
478 (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version))
479 {
480 if (cfg.macs_per_cc != opt_cfg_p->macs_per_cc)
481 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200482 LOG_ERR("NPU config mismatch: npu.macs_per_cc=%" PRIu32 " optimizer.macs_per_cc=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200483 cfg.macs_per_cc,
484 opt_cfg_p->macs_per_cc);
485 }
486 if (cfg.shram_size != opt_cfg_p->shram_size)
487 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200488 LOG_ERR("NPU config mismatch: npu.shram_size=%" PRIu32 " optimizer.shram_size=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200489 cfg.shram_size,
490 opt_cfg_p->shram_size);
491 }
492 if (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version)
493 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200494 LOG_ERR("NPU config mismatch: npu.cmd_stream_version=%" PRIu32 " optimizer.cmd_stream_version=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200495 cfg.cmd_stream_version,
496 opt_cfg_p->cmd_stream_version);
497 }
498 return_code = -1;
499 }
500
Bhavik Patel790ef362020-06-03 10:05:28 +0200501 if ((id.product_major == PRODUCT_MAJOR_ETHOSU55) &&
Douglas Troha60d50ae2020-06-15 12:48:10 +0200502 ((id.arch_major_rev != opt_cfg_p->arch_major_rev) || (id.arch_minor_rev != opt_cfg_p->arch_minor_rev)))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200503 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200504 LOG_ERR("NPU arch mismatch: npu.arch=%" PRIu32 ".%" PRIu32 ".%" PRIu32 " optimizer.arch=%d.%d.%d\n",
Bhavik Patel790ef362020-06-03 10:05:28 +0200505 id.arch_major_rev,
506 id.arch_minor_rev,
507 id.arch_patch_rev,
508 opt_cfg_p->arch_major_rev,
509 opt_cfg_p->arch_minor_rev,
510 opt_cfg_p->arch_patch_rev);
511 return_code = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200512 }
513
514#if !defined(LOG_ENABLED)
515 UNUSED(opt_cfg_p);
516#endif
517 return return_code;
518}
519
Bhavik Pateldae5be02020-06-18 15:25:15 +0200520static void npu_axi_init(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200521{
Bhavik Pateldae5be02020-06-18 15:25:15 +0200522 ethosu_set_qconfig(&drv->dev, NPU_QCONFIG);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200523
Bhavik Pateldae5be02020-06-18 15:25:15 +0200524 ethosu_set_regioncfg(&drv->dev, 0, NPU_REGIONCFG_0);
525 ethosu_set_regioncfg(&drv->dev, 1, NPU_REGIONCFG_1);
526 ethosu_set_regioncfg(&drv->dev, 2, NPU_REGIONCFG_2);
527 ethosu_set_regioncfg(&drv->dev, 3, NPU_REGIONCFG_3);
528 ethosu_set_regioncfg(&drv->dev, 4, NPU_REGIONCFG_4);
529 ethosu_set_regioncfg(&drv->dev, 5, NPU_REGIONCFG_5);
530 ethosu_set_regioncfg(&drv->dev, 6, NPU_REGIONCFG_6);
531 ethosu_set_regioncfg(&drv->dev, 7, NPU_REGIONCFG_7);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200532
Bhavik Pateldae5be02020-06-18 15:25:15 +0200533 (void)ethosu_set_axi_limit0(&drv->dev,
534 AXI_LIMIT0_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200535 AXI_LIMIT0_MEM_TYPE,
536 AXI_LIMIT0_MAX_OUTSTANDING_READS,
537 AXI_LIMIT0_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200538 (void)ethosu_set_axi_limit1(&drv->dev,
539 AXI_LIMIT1_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200540 AXI_LIMIT1_MEM_TYPE,
541 AXI_LIMIT1_MAX_OUTSTANDING_READS,
542 AXI_LIMIT1_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200543 (void)ethosu_set_axi_limit2(&drv->dev,
544 AXI_LIMIT2_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200545 AXI_LIMIT2_MEM_TYPE,
546 AXI_LIMIT2_MAX_OUTSTANDING_READS,
547 AXI_LIMIT2_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200548 (void)ethosu_set_axi_limit3(&drv->dev,
549 AXI_LIMIT3_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200550 AXI_LIMIT3_MEM_TYPE,
551 AXI_LIMIT3_MAX_OUTSTANDING_READS,
552 AXI_LIMIT3_MAX_OUTSTANDING_WRITES);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200553}
554
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200555/* Default implementation to flush the data cache. Override if available on the targeted device.
556 * Passing NULL as p argument expects the whole cache to be flushed.
557 */
558void __attribute__((weak)) ethosu_flush_dcache(uint32_t *p, size_t bytes)
559{
560 (void)p;
561 (void)bytes;
562}
563
564/* Default implementation to invalidate the data cache. Override if available on the targeted device.
565 * Passing NULL as p argument expects the whole cache to be flushed.
566 */
567void __attribute__((weak)) ethosu_invalidate_dcache(uint32_t *p, size_t bytes)
568{
569 (void)p;
570 (void)bytes;
571}
572
Bhavik Pateldae5be02020-06-18 15:25:15 +0200573static int handle_command_stream(struct ethosu_driver *drv,
574 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200575 const int cms_length,
576 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200577 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200578 const int num_base_addr)
579{
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100580 uint32_t qread = 0;
581 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
582 ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream;
583
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200584 LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d\n", cmd_stream, cms_length);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200585
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200586 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200587 {
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200588 LOG_ERR("Error: Command stream addr %p not aligned to 16 bytes\n", cmd_stream);
589 return -1;
590 }
591
592 bool base_addr_invalid = false;
593 for (int i = 0; i < num_base_addr; i++)
594 {
595 if (0 != (base_addr[i] & MASK_16_BYTE_ALIGN))
596 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200597 LOG_ERR("Error: Base addr %d: 0x%llx not aligned to 16 bytes\n", i, base_addr[i]);
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200598 base_addr_invalid = true;
599 }
600 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100601
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200602 if (base_addr_invalid)
603 {
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200604 return -1;
605 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100606
Bhavik Pateldae5be02020-06-18 15:25:15 +0200607 npu_axi_init(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200608
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200609 /* Flush the cache if available on our CPU.
610 * The upcasting to uin32_t* is ok since the pointer never is dereferenced.
611 * The base_addr_size is null if invoking from prior to invoke_V2, in that case
612 * the whole cache is being flushed.
613 */
614
615 if (base_addr_size != NULL)
616 {
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100617 ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200618 for (int i = 0; i < num_base_addr; i++)
619 {
620 ethosu_flush_dcache((uint32_t *)base_addr[i], base_addr_size[i]);
621 }
622 }
623 else
624 {
625 ethosu_flush_dcache(NULL, 0);
626 }
627
Bhavik Pateldae5be02020-06-18 15:25:15 +0200628 if (ETHOSU_SUCCESS != ethosu_run_command_stream(&drv->dev, cmd_stream, cms_bytes, base_addr, num_base_addr))
Bhavik Patel790ef362020-06-03 10:05:28 +0200629 {
630 return -1;
631 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200632
Bhavik Pateldae5be02020-06-18 15:25:15 +0200633 wait_for_irq(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200634
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200635 if (drv->status_error)
636 {
637 return -1;
638 }
639
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200640 if (base_addr_size != NULL)
641 {
642 for (int i = 0; i < num_base_addr; i++)
643 {
644 ethosu_invalidate_dcache((uint32_t *)base_addr[i], base_addr_size[i]);
645 }
646 }
647 else
648 {
649 ethosu_invalidate_dcache(NULL, 0);
650 }
651
Bhavik Pateldae5be02020-06-18 15:25:15 +0200652 (void)ethosu_get_qread(&drv->dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200653 if (qread != cms_bytes)
654 {
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200655 LOG_WARN(
Per Åstrand14ccfee2020-09-25 10:40:20 +0200656 "Failure: IRQ received but qread (%" PRIu32 ") not at end of stream (%" PRIu32 ").\n", qread, cms_bytes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200657 return -1;
658 }
659
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200660 return 0;
661}
662
Bhavik Pateldae5be02020-06-18 15:25:15 +0200663static int read_apb_reg(struct ethosu_driver *drv, uint16_t da_data)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200664{
665 uint32_t *reg_p;
666 uint32_t start_address = (uint32_t)(da_data & APB_START_ADDR_MASK);
667 uint16_t num_reg = (da_data >> APB_NUM_REG_BIT_SHIFT) + 1;
668
669 reg_p = (uint32_t *)malloc(num_reg * sizeof(uint32_t));
670 if (reg_p == NULL)
671 {
672 LOG_INFO("read_apb_reg, Error! memory not allocated.");
673 return -1;
674 }
675
Bhavik Pateldae5be02020-06-18 15:25:15 +0200676 if (ETHOSU_SUCCESS == ethosu_read_apb_reg(&drv->dev, start_address, num_reg, reg_p))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200677 {
678 for (int i = 0; i < num_reg; i++)
679 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200680 LOG_INFO(
681 "NPU_REG ADDR 0x%04" PRIu32 " = 0x%08" PRIu32 "\n", (start_address + (i * BYTES_IN_32_BITS)), reg_p[i]);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200682 }
683 }
684 else
685 {
686 free(reg_p);
687 return -1;
688 }
689
690 free(reg_p);
691 return 0;
692}
693
Bhavik Pateldae5be02020-06-18 15:25:15 +0200694static int dump_shram(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200695{
696 struct ethosu_config cfg;
697 uint32_t *shram_p;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200698 (void)ethosu_get_config(&drv->dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200699
Per Åstrand14ccfee2020-09-25 10:40:20 +0200700 LOG_INFO("dump_shram size = %" PRIu32 " KB\n", cfg.shram_size);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200701
702 shram_p = (uint32_t *)malloc(BYTES_1KB);
703 if (shram_p == NULL)
704 {
705 LOG_ERR("read_shram, Error! memory not allocated.");
706 return -1;
707 }
708
709 for (uint32_t i = 0; i < cfg.shram_size; i++)
710 {
Bhavik Pateldae5be02020-06-18 15:25:15 +0200711 ethosu_get_shram_data(&drv->dev, i, (uint32_t *)shram_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200712 // Output 1KB of SHRAM
Per Åstrand14ccfee2020-09-25 10:40:20 +0200713 LOG_INFO("***SHRAM SECTION %" PRIu32 "***\n", i);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200714 for (int j = 0; j < (BYTES_1KB / BYTES_IN_32_BITS); j++)
715 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200716 LOG_INFO("[0x%04" PRIx32 "] %" PRIx32 "\n", (i * 1024 + j * 4), shram_p[j]);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200717 }
718 }
719 free(shram_p);
720
721 return 0;
722}
723
724typedef struct
725{
726 int number;
727 const char *name;
728} name_lookup_t;
729
730static const name_lookup_t npu_reg_name_tbl[] = {
731 {0x200, "KERNEL_X"},
732 {0x204, "KERNEL_Y"},
733 {0x208, "KERNEL_W_M1"},
734 {0x20C, "KERNEL_H_M1"},
735 {0x210, "OFM_CBLK_WIDTH_M1"},
736 {0x214, "OFM_CBLK_HEIGHT_M1"},
737 {0x218, "OFM_CBLK_DEPTH_M1"},
738 {0x21c, "IFM_CBLK_DEPTH_M1"},
739 {0x220, "OFM_X"},
740 {0x224, "OFM_Y"},
741 {0x228, "OFM_Z"},
742 {0x22C, "IFM_Z"},
743 {0x230, "PAD_TOP"},
744 {0x234, "PAD_LEFT"},
745 {0x238, "IFM_CBLK_WIDTH"},
746 {0x23C, "IFM_CBLK_HEIGHT"},
747 {0x240, "DMA_IFM_SRC"},
748 {0x244, "DMA_IFM_SRC_HI"},
749 {0x248, "DMA_IFM_DST"},
750 {0x24c, "DMA_OFM_SRC"},
751 {0x250, "DMA_OFM_DST"},
752 {0x254, "DMA_OFM_DST_HI"},
753 {0x258, "DMA_WEIGHT_SRC"},
754 {0x25c, "DMA_WEIGHT_SRC_HI"},
755 {0x260, "DMA_CMD_SRC"},
756 {0x264, "DMA_CMD_SRC_HI"},
757 {0x268, "DMA_CMD_SIZE"},
758 {0x26c, "DMA_M2M_SRC"},
759 {0x270, "DMA_M2M_SRC_HI"},
760 {0x274, "DMA_M2M_DST"},
761 {0x278, "DMA_M2M_DST_HI"},
762 {0x27c, "CURRENT_QREAD"},
763 {0x280, "DMA_SCALE_SRC"},
764 {0x284, "DMA_SCALE_SRC_HI"},
765 {0x2BC, "CURRENT_CMD"},
766 {0x800, "IFM_PAD_TOP"},
767 {0x804, "IFM_PAD_LEFT"},
768 {0x808, "IFM_PAD_RIGHT"},
769 {0x80C, "IFM_PAD_BOTTOM"},
770 {0x810, "IFM_DEPTH_M1"},
771 {0x814, "IFM_PRECISION"},
772 {0x81C, "IFM_UPSCALE"},
773 {0x824, "IFM_ZERO_POINT"},
774 {0x828, "IFM_WIDTH0_M1"},
775 {0x82C, "IFM_HEIGHT0_M1"},
776 {0x830, "IFM_HEIGHT1_M1"},
777 {0x834, "IFM_IB_END"},
778 {0x83C, "IFM_REGION"},
779 {0x844, "OFM_WIDTH_M1"},
780 {0x848, "OFM_HEIGHT_M1"},
781 {0x84C, "OFM_DEPTH_M1"},
782 {0x850, "OFM_PRECISION"},
783 {0x854, "OFM_BLK_WIDTH_M1"},
784 {0x858, "OFM_BLK_HEIGHT_M1"},
785 {0x85C, "OFM_BLK_DEPTH_M1"},
786 {0x860, "OFM_ZERO_POINT"},
787 {0x868, "OFM_WIDTH0_M1"},
788 {0x86C, "OFM_HEIGHT0_M1"},
789 {0x870, "OFM_HEIGHT1_M1"},
790 {0x87C, "OFM_REGION"},
791 {0x880, "KERNEL_WIDTH_M1"},
792 {0x884, "KERNEL_HEIGHT_M1"},
793 {0x888, "KERNEL_STRIDE"},
794 {0x88C, "PARALLEL_MODE"},
795 {0x890, "ACC_FORMAT"},
796 {0x894, "ACTIVATION"},
797 {0x898, "ACTIVATION_MIN"},
798 {0x89C, "ACTIVATION_MAX"},
799 {0x8A0, "WEIGHT_REGION"},
800 {0x8A4, "SCALE_REGION"},
801 {0x8B4, "AB_START"},
802 {0x8BC, "BLOCKDEP"},
803 {0x8C0, "DMA0_SRC_REGION"},
804 {0x8C4, "DMA0_DST_REGION"},
805 {0x8C8, "DMA0_SIZE0"},
806 {0x8CC, "DMA0_SIZE1"},
807 {0x900, "IFM2_BROADCAST"},
808 {0x904, "IFM2_SCALAR"},
809 {0x924, "IFM2_ZERO_POINT"},
810 {0x928, "IFM2_WIDTH0_M1"},
811 {0x92C, "IFM2_HEIGHT0_M1"},
812 {0x930, "IFM2_HEIGHT1_M1"},
813 {0x934, "IFM2_IB_START"},
814 {0x93C, "IFM2_REGION"},
815 {0xA00, "IFM_BASE0"},
816 {0xA04, "IFM_BASE0_HI"},
817 {0xA08, "IFM_BASE1"},
818 {0xA0C, "IFM_BASE1_HI"},
819 {0xA10, "IFM_BASE2"},
820 {0xA14, "IFM_BASE2_HI"},
821 {0xA18, "IFM_BASE3"},
822 {0xA1C, "IFM_BASE3_HI"},
823 {0xA20, "IFM_STRIDE_X"},
824 {0xA24, "IFM_STRIDE_X_HI"},
825 {0xA28, "IFM_STRIDE_Y"},
826 {0xA2C, "IFM_STRIDE_Y_HI"},
827 {0xA30, "IFM_STRIDE_C"},
828 {0xA34, "IFM_STRIDE_C_HI"},
829 {0xA40, "OFM_BASE0"},
830 {0xA44, "OFM_BASE0_HI"},
831 {0xA48, "OFM_BASE1"},
832 {0xA4C, "OFM_BASE1_HI"},
833 {0xA50, "OFM_BASE2"},
834 {0xA54, "OFM_BASE2_HI"},
835 {0xA58, "OFM_BASE3"},
836 {0xA5C, "OFM_BASE3_HI"},
837 {0xA60, "OFM_STRIDE_X"},
838 {0xA64, "OFM_STRIDE_X_HI"},
839 {0xA68, "OFM_STRIDE_Y"},
840 {0xA6C, "OFM_STRIDE_Y_HI"},
841 {0xA70, "OFM_STRIDE_C"},
842 {0xA74, "OFM_STRIDE_C_HI"},
843 {0xA80, "WEIGHT_BASE"},
844 {0xA84, "WEIGHT_BASE_HI"},
845 {0xA88, "WEIGHT_LENGTH"},
846 {0xA8C, "WEIGHT_LENGTH_HI"},
847 {0xA90, "SCALE_BASE"},
848 {0xA94, "SCALE_BASE_HI"},
849 {0xA98, "SCALE_LENGTH"},
850 {0xAA0, "OFM_SCALE"},
851 {0xAA4, "OFM_SCALE_SHIFT"},
852 {0xAA8, "OPA_SCALE "},
853 {0xAB0, "OPB_SCALE"},
854 {0xAC0, "DMA0_SRC"},
855 {0xAC4, "DMA0_SRC_HI"},
856 {0xAC8, "DMA0_DST"},
857 {0xACC, "DMA0_DST_HI"},
858 {0xAD0, "DMA0_LEN"},
859 {0xAD4, "DMA0_LEN_HI"},
860 {0xAD8, "DMA0_SKIP0"},
861 {0xADC, "DMA0_SKIP0_HI"},
862 {0xAE0, "DMA0_SKIP1"},
863 {0xAE4, "DMA0_SKIP1_HI"},
864 {0xB00, "IFM2_BASE0"},
865 {0xB04, "IFM2_BASE0_HI"},
866 {0xB08, "IFM2_BASE1"},
867 {0xB0C, "IFM2_BASE1_HI"},
868 {0xB10, "IFM2_BASE2"},
869 {0xB14, "IFM2_BASE2_HI"},
870 {0xB18, "IFM2_BASE3"},
871 {0xB1C, "IFM2_BASE3_HI"},
872 {0xB20, "IFM2_STRIDE_X"},
873 {0xB24, "IFM2_STRIDE_X_HI"},
874 {0xB28, "IFM2_STRIDE_Y"},
875 {0xB2C, "IFM2_STRIDE_Y_HI"},
876 {0xB30, "IFM2_STRIDE_C"},
877 {0xB34, "IFM2_STRIDE_C_HI"},
878 {0xB40, "WEIGHT1_BASE"},
879 {0xB44, "WEIGHT1_BASE_HI"},
880 {0xB48, "WEIGHT1_LENGTH"},
881 {0xB4C, "WEIGHT1_LENGTH_HI"},
882 {0xB50, "SCALE1_BASE"},
883 {0xB54, "SCALE1_BASE_HI"},
884 {0xB58, "SCALE1_LENGTH"},
885};
886
887static const char *lookup_name(const name_lookup_t *lookup_table, int lookup_table_count, int find)
888{
889 int n;
890 for (n = 0; n < lookup_table_count; n++)
891 {
892 if (lookup_table[n].number == find)
893 {
894 return lookup_table[n].name;
895 }
896 }
897 // Not found
898 return 0;
899}
900
Bhavik Pateldae5be02020-06-18 15:25:15 +0200901static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200902{
903 unsigned int reg_val;
904 const char *reg_name;
905 int npu_reg_name_tbl_count = sizeof(npu_reg_name_tbl) / sizeof(npu_reg_name_tbl[0]);
906
907 LOG_INFO("dump_register %X - %X\n", npu_reg, npu_reg_end);
908 for (; npu_reg <= npu_reg_end; npu_reg += sizeof(int))
909 {
Bhavik Pateldae5be02020-06-18 15:25:15 +0200910 reg_val = ethosu_read_reg(&drv->dev, npu_reg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200911 reg_name = lookup_name(npu_reg_name_tbl, npu_reg_name_tbl_count, npu_reg);
912 LOG_INFO("[0x%.4X] 0x%.8X\t%s\n", npu_reg, reg_val, (reg_name) ? reg_name : "");
913 }
914}
915
916static const name_lookup_t cmd0_name_tbl[] = {
917 {0x000, "NPU_OP_STOP"},
918 {0x001, "NPU_OP_IRQ"},
919 {0x002, "NPU_OP_CONV"},
920 {0x003, "NPU_OP_DEPTHWISE"},
921 {0x004, "NPU_OP_VECTOR_PROD"},
922 {0x005, "NPU_OP_POOL"},
923 {0x006, "NPU_OP_ELEMENTWISE"},
924 {0x010, "NPU_OP_DMA_START"},
925 {0x011, "NPU_OP_DMA_WAIT"},
926 {0x012, "NPU_OP_KERNEL_WAIT"},
927 {0x100, "NPU_SET_IFM_PAD_TOP"},
928 {0x101, "NPU_SET_IFM_PAD_LEFT"},
929 {0x102, "NPU_SET_IFM_PAD_RIGHT"},
930 {0x103, "NPU_SET_IFM_PAD_BOTTOM"},
931 {0x104, "NPU_SET_IFM_DEPTH_M1"},
932 {0x105, "NPU_SET_IFM_PRECISION"},
933 {0x107, "NPU_SET_IFM_UPSCALE"},
934 {0x109, "NPU_SET_IFM_ZERO_POINT"},
935 {0x10A, "NPU_SET_IFM_WIDTH0_M1"},
936 {0x10B, "NPU_SET_IFM_HEIGHT0_M1"},
937 {0x10C, "NPU_SET_IFM_HEIGHT1_M1"},
938 {0x10D, "NPU_SET_IFM_IB_END"},
939 {0x10F, "NPU_SET_IFM_REGION"},
940 {0x110, "NPU_SET_OFM_BATCH_SIZE_M1"},
941 {0x111, "NPU_SET_OFM_WIDTH_M1"},
942 {0x112, "NPU_SET_OFM_HEIGHT_M1"},
943 {0x113, "NPU_SET_OFM_DEPTH_M1"},
944 {0x114, "NPU_SET_OFM_PRECISION"},
945 {0x115, "NPU_SET_OFM_BLK_WIDTH_M1"},
946 {0x116, "NPU_SET_OFM_BLK_HEIGHT_M1"},
947 {0x117, "NPU_SET_OFM_BLK_DEPTH_M1"},
948 {0x118, "NPU_SET_OFM_ZERO_POINT"},
949 {0x11A, "NPU_SET_OFM_WIDTH0_M1"},
950 {0x11B, "NPU_SET_OFM_HEIGHT0_M1"},
951 {0x11C, "NPU_SET_OFM_HEIGHT1_M1"},
952 {0x11F, "NPU_SET_OFM_REGION"},
953 {0x120, "NPU_SET_KERNEL_WIDTH_M1"},
954 {0x121, "NPU_SET_KERNEL_HEIGHT_M1"},
955 {0x122, "NPU_SET_KERNEL_STRIDE"},
956 {0x124, "NPU_SET_ACC_FORMAT"},
957 {0x125, "NPU_SET_ACTIVATION"},
958 {0x126, "NPU_SET_ACTIVATION_MIN"},
959 {0x127, "NPU_SET_ACTIVATION_MAX"},
960 {0x128, "NPU_SET_WEIGHT_REGION"},
961 {0x129, "NPU_SET_SCALE_REGION"},
962 {0x12D, "NPU_SET_AB_START"},
963 {0x12F, "NPU_SET_BLOCKDEP"},
964 {0x130, "NPU_SET_DMA0_SRC_REGION"},
965 {0x131, "NPU_SET_DMA0_DST_REGION"},
966 {0x180, "NPU_SET_IFM2_BROADCAST"},
967 {0x181, "NPU_SET_IFM2_SCALAR"},
968 {0x185, "NPU_SET_IFM2_PRECISION"},
969 {0x189, "NPU_SET_IFM2_ZERO_POINT"},
970 {0x18A, "NPU_SET_IFM2_WIDTH0_M1"},
971 {0x18B, "NPU_SET_IFM2_HEIGHT0_M1"},
972 {0x18C, "NPU_SET_IFM2_HEIGHT1_M1"},
973 {0x18D, "NPU_SET_IFM2_IB_START"},
974 {0x18F, "NPU_SET_IFM2_REGION"},
975};
976
977static const name_lookup_t cmd1_name_tbl[] = {
978 {0x000, "NPU_SET_IFM_BASE0"}, {0x001, "NPU_SET_IFM_BASE1"}, {0x002, "NPU_SET_IFM_BASE2"},
979 {0x003, "NPU_SET_IFM_BASE3"}, {0x004, "NPU_SET_IFM_STRIDE_X"}, {0x005, "NPU_SET_IFM_STRIDE_Y"},
980 {0x006, "NPU_SET_IFM_STRIDE_C"}, {0x007, "NPU_SET_IFM_STRIDE_N"}, {0x010, "NPU_SET_OFM_BASE0"},
981 {0x011, "NPU_SET_OFM_BASE1"}, {0x012, "NPU_SET_OFM_BASE2"}, {0x013, "NPU_SET_OFM_BASE3"},
982 {0x014, "NPU_SET_OFM_STRIDE_X"}, {0x015, "NPU_SET_OFM_STRIDE_Y"}, {0x016, "NPU_SET_OFM_STRIDE_C"},
983 {0x017, "NPU_SET_OFM_STRIDE_N"}, {0x020, "NPU_SET_WEIGHT_BASE"}, {0x021, "NPU_SET_WEIGHT_LENGTH"},
984 {0x022, "NPU_SET_SCALE_BASE"}, {0x023, "NPU_SET_SCALE_LENGTH"}, {0x024, "NPU_SET_OFM_SCALE"},
985 {0x025, "NPU_SET_OPA_SCALE"}, {0x026, "NPU_SET_OPB_SCALE"}, {0x030, "NPU_SET_DMA0_SRC"},
986 {0x031, "NPU_SET_DMA0_DST"}, {0x032, "NPU_SET_DMA0_LEN"}, {0x080, "NPU_SET_IFM2_BASE0"},
987 {0x081, "NPU_SET_IFM2_BASE1"}, {0x082, "NPU_SET_IFM2_BASE2"}, {0x083, "NPU_SET_IFM2_BASE3"},
988 {0x084, "NPU_SET_IFM2_STRIDE_X"}, {0x085, "NPU_SET_IFM2_STRIDE_Y"}, {0x086, "NPU_SET_IFM2_STRIDE_C"},
989};
990
991static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread)
992{
993 int n;
994 int offset;
995 uint32_t cmd_val;
996 const uint8_t *cmd_ptr;
997 const char *cmd_name;
998 int cmd0_name_tbl_count = sizeof(cmd0_name_tbl) / sizeof(cmd0_name_tbl[0]);
999 int cmd1_name_tbl_count = sizeof(cmd1_name_tbl) / sizeof(cmd1_name_tbl[0]);
1000
1001 LOG_INFO("dump_command_stream cmd_stream = 0x%8p cms_length = %d\n", cmd_stream, cms_length);
1002 for (n = 0; n < cms_length; n++)
1003 {
1004 // Offset
1005 offset = n * sizeof(int);
1006 LOG_INFO("[%.4d] ", offset);
1007 // Command
1008 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1009 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1010 // Command name and payload
1011 if (cmd_stream[n] & 0x4000)
1012 {
1013 cmd_name = lookup_name(cmd1_name_tbl, cmd1_name_tbl_count, cmd_stream[n] & 0x3FF);
1014 n++;
1015 cmd_val = cmd_stream[n];
1016 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1017 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1018 }
1019 else
1020 {
1021 cmd_val = cmd_stream[n] >> 16;
1022 cmd_name = lookup_name(cmd0_name_tbl, cmd0_name_tbl_count, cmd_stream[n] & 0x3FF);
1023 }
1024 if (cmd_name)
1025 {
Per Åstrand14ccfee2020-09-25 10:40:20 +02001026 LOG_INFO("\t%s 0x%.8" PRIX32, cmd_name, cmd_val);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001027 }
1028 if (offset == qread)
1029 {
1030 LOG_INFO(" <<== QREAD\n");
1031 }
1032 else
1033 {
1034 LOG_INFO("\n");
1035 }
1036 }
1037}