blob: e3723120349505230f8aba0e900ffd08f0194427 [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
19#include "ethosu_driver.h"
Bhavik Patel790ef362020-06-03 10:05:28 +020020#include "ethosu_config.h"
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020021
22#include "ethosu_common.h"
23#include "ethosu_device.h"
Per Åstrand25d78c02020-04-21 14:19:44 +020024
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020025#include <assert.h>
Per Åstrand25d78c02020-04-21 14:19:44 +020026#include <cmsis_compiler.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020027#include <stdbool.h>
Bhavik Patelbf7ae632020-06-11 21:00:16 +020028#include <stddef.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020029#include <stdint.h>
30#include <stdio.h>
31#include <stdlib.h>
32
33// Abort flag
34static int abort_inference = false;
35
36// IRQ
37static volatile bool irq_triggered = false;
38#if defined(CPU_CORTEX_M3) || defined(CPU_CORTEX_M4) || defined(CPU_CORTEX_M7) || defined(CPU_CORTEX_M33) || \
39 defined(CPU_CORTEX_M55)
Per Åstrand25d78c02020-04-21 14:19:44 +020040void ethosu_irq_handler(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020041{
42 uint8_t irq_raised = 0;
43 (void)ethosu_is_irq_raised(&irq_raised);
44 ASSERT(irq_raised == 1);
45 irq_triggered = true;
46 (void)ethosu_clear_irq_status();
47 (void)ethosu_is_irq_raised(&irq_raised);
48 ASSERT(irq_raised == 0);
49}
50
51static inline void wait_for_irq(void)
52{
53 while (1)
54 {
55 __disable_irq();
56 if (irq_triggered || abort_inference)
57 {
58 __enable_irq();
59 break;
60 }
61
Per Åstrand25d78c02020-04-21 14:19:44 +020062 __WFI();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020063
64 __enable_irq();
65 }
66}
67#else
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020068// Just polling the status register
69static inline void wait_for_irq(void)
70{
71 uint8_t irq_raised = 0;
72
73 for (int i = 0; i < 5000; ++i)
74 {
75 (void)ethosu_is_irq_raised(&irq_raised);
76 if (1 == irq_raised)
77 {
78 break;
79 }
80 }
81 ASSERT(1 == irq_raised);
82
83 irq_triggered = true;
84}
85#endif
86
87#define MACS_PER_CYCLE_LOG2_MASK 0x000F
88#define SHRAM_SIZE_MASK 0xFF00
89#define SHRAM_SIZE_RIGHT_SHIFT 8
90#define BYTES_IN_32_BITS 4
91#define CUSTOM_OPTION_LENGTH_32_BIT_WORD 1
92#define DRIVER_ACTION_LENGTH_32_BIT_WORD 1
93#define OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD 2
94#define ETHOSU_FOURCC ('1' << 24 | 'P' << 16 | 'O' << 8 | 'C') // "Custom Operator Payload 1"
95#define APB_START_ADDR_MASK 0x0FFF
96#define APB_NUM_REG_BIT_SHIFT 12
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020097#define BYTES_1KB 1024
Bhavik Patel790ef362020-06-03 10:05:28 +020098#define PRODUCT_MAJOR_ETHOSU55 (4)
Bhavik Patelbf7ae632020-06-11 21:00:16 +020099#define MASK_16_BYTE_ALIGN (0xF)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200100
101// Driver actions
102enum DRIVER_ACTION_e
103{
104 RESERVED = 0,
105 OPTIMIZER_CONFIG = 1,
106 COMMAND_STREAM = 2,
107 READ_APB_REG = 3,
108 DUMP_SHRAM = 4,
109 NOP = 5,
110};
111
112// Custom data struct
113struct custom_data_s
114{
115 union
116 {
117 // Driver action data
118 struct
119 {
120 // Driver action command (valid values in DRIVER_ACTION_e)
121 uint8_t driver_action_command;
122 // reserved
123 uint8_t reserved;
124 // Driver action data
125 union
126 {
127 struct
128 { // DA_CMD_OPT_CFG
129 uint16_t rel_nbr : 4;
130 uint16_t patch_nbr : 4;
131 uint16_t opt_cfg_reserved : 8;
132 };
133 struct
134 { // DA_CMD_CMSTRM
135 uint16_t length;
136 };
137 struct
138 { // DA_CMD_READAPB
139 uint16_t start_address : 12;
140 uint16_t nbr_reg_minus1 : 4;
141 };
142 uint16_t driver_action_data;
143 };
144 };
145 uint32_t word;
146 };
147};
148
149// optimizer config struct
150struct opt_cfg_s
151{
152 struct custom_data_s da_data;
153 union
154 {
155 struct
156 {
157 uint32_t macs_per_cc : 4;
158 uint32_t cmd_stream_version : 4;
159 uint32_t shram_size : 8;
160 uint32_t reserved1 : 16;
161 };
162 uint32_t npu_cfg;
163 };
164 union
165 {
166 struct
167 {
168 uint32_t version_status : 4;
169 uint32_t version_minor : 4;
170 uint32_t version_major : 4;
171 uint32_t product_major : 4;
172 uint32_t arch_patch_rev : 4;
173 uint32_t arch_minor_rev : 8;
174 uint32_t arch_major_rev : 4;
175 };
176 uint32_t ethosu_id;
177 };
178};
179
180static int handle_optimizer_config(struct opt_cfg_s *opt_cfg_p);
181static int handle_command_stream(const uint8_t *cmd_stream,
182 const int cms_length,
183 const uint64_t *base_addr,
184 const int num_base_addr);
185static int read_apb_reg(uint16_t);
186static int dump_shram();
187static void dump_npu_register(int npu_reg, int npu_reg_end);
188static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread);
189
190int ethosu_init(void)
191{
192 int return_code = 0;
193 LOG_INFO("ethosu_init calling NPU embed driver ethosu_dev_init\n");
194
Bhavik Patele645fed2020-06-12 14:46:47 +0200195 if (ETHOSU_SUCCESS != ethosu_set_clock_and_power(ETHOSU_CLOCK_Q_DISABLE, ETHOSU_POWER_Q_DISABLE))
196 {
197 LOG_ERR("Failed to disable clock-q & power-q for Ethos-U\n");
198 return -1;
199 }
200
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200201 ethosu_soft_reset();
Kristofer Jonssondaa0d202020-05-12 12:23:16 +0200202
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200203 if (ETHOSU_SUCCESS != ethosu_wait_for_reset())
204 {
205 LOG_ERR("Failed reset of Ethos-U\n");
206 return -1;
207 }
208
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200209 return_code = ethosu_dev_init();
210
211 return return_code;
212}
213
214int ethosu_get_version(struct ethosu_version *version)
215{
216 int return_code = 0;
217
218 if (NULL != version)
219 {
220 struct ethosu_id id;
221 struct ethosu_config cfg;
222 (void)ethosu_get_id(&id);
223 (void)ethosu_get_config(&cfg);
224
225 version->id.version_status = id.version_status;
226 version->id.version_minor = id.version_minor;
227 version->id.version_major = id.version_major;
228 version->id.product_major = id.product_major;
229 version->id.arch_patch_rev = id.arch_patch_rev;
230 version->id.arch_minor_rev = id.arch_minor_rev;
231 version->id.arch_major_rev = id.arch_major_rev;
232 version->id.driver_patch_rev = ETHOSU_DRIVER_VERSION_PATCH;
233 version->id.driver_minor_rev = ETHOSU_DRIVER_VERSION_MINOR;
234 version->id.driver_major_rev = ETHOSU_DRIVER_VERSION_MAJOR;
235 version->cfg.macs_per_cc = cfg.macs_per_cc;
236 version->cfg.cmd_stream_version = cfg.cmd_stream_version;
237 version->cfg.shram_size = cfg.shram_size;
238 }
239 else
240 {
241 return_code = -1;
242 }
243
244 return return_code;
245}
246
247int ethosu_invoke(const void *custom_data_ptr,
248 const int custom_data_size,
249 const uint64_t *base_addr,
250 const int num_base_addr)
251{
252 struct custom_data_s *data_start_ptr = (struct custom_data_s *)custom_data_ptr;
253 int return_code = 0;
254
255 LOG_INFO("ethosu_invoke\n");
256
257 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
258 if (data_start_ptr->word != ETHOSU_FOURCC)
259 {
260 LOG_ERR("Custom Operator Payload: %x is not correct, expected %x\n", data_start_ptr->word, ETHOSU_FOURCC);
261 return -1;
262 }
263 data_start_ptr += CUSTOM_OPTION_LENGTH_32_BIT_WORD;
264 struct custom_data_s *data_ptr = data_start_ptr;
265
266 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
267 {
268 LOG_ERR("ethosu_invoke ERROR custom_data_size=0x%x not a multiple of 4\n", custom_data_size);
269 return -1;
270 }
271 int custom_data_32bit_size = (custom_data_size / BYTES_IN_32_BITS - CUSTOM_OPTION_LENGTH_32_BIT_WORD);
272
Bhavik Patele645fed2020-06-12 14:46:47 +0200273 ethosu_set_clock_and_power(ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200274 while (data_ptr < (data_start_ptr + custom_data_32bit_size))
275 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200276 int ret = 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200277 switch (data_ptr->driver_action_command)
278 {
279 case OPTIMIZER_CONFIG:
280 LOG_INFO("ethosu_invoke OPTIMIZER_CONFIG\n");
281 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
282
Bhavik Patele645fed2020-06-12 14:46:47 +0200283 ret = handle_optimizer_config(opt_cfg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200284 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
285 break;
286 case COMMAND_STREAM:
287 LOG_INFO("ethosu_invoke COMMAND_STREAM\n");
288 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct custom_data_s);
289 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
290
291 abort_inference = false;
292 // It is safe to clear this flag without atomic, because npu is not running.
293 irq_triggered = false;
294
Bhavik Patele645fed2020-06-12 14:46:47 +0200295 ret = handle_command_stream(command_stream, cms_length, base_addr, num_base_addr);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200296
Bhavik Patele645fed2020-06-12 14:46:47 +0200297 if (ret == -1 && abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200298 {
299 uint32_t qread = 0;
300 ethosu_get_qread(&qread);
301 LOG_ERR("NPU timeout\n");
302 dump_command_stream(command_stream, cms_length, qread);
303 dump_npu_register(0x200, 0x2BF);
304 dump_npu_register(0x800, 0xB3F);
305 dump_shram();
306 }
307
308 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
309 break;
310 case READ_APB_REG:
311 LOG_INFO("ethosu_invoke READ_APB_REG\n");
Bhavik Patele645fed2020-06-12 14:46:47 +0200312 ret = read_apb_reg(data_ptr->driver_action_data);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200313 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
314 break;
315 case DUMP_SHRAM:
316 LOG_INFO("ethosu_invoke DUMP_SHRAM\n");
Bhavik Patele645fed2020-06-12 14:46:47 +0200317 ret = dump_shram();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200318 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
319 break;
320 case NOP:
321 LOG_INFO("ethosu_invoke NOP\n");
322 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
323 break;
324 default:
325 LOG_ERR("ethosu_invoke UNSUPPORTED driver_action_command %d \n", data_ptr->driver_action_command);
Bhavik Patele645fed2020-06-12 14:46:47 +0200326 ret = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200327 break;
328 }
Bhavik Patele645fed2020-06-12 14:46:47 +0200329 if (ret != 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200330 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200331 return_code = -1;
332 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200333 }
334 }
Bhavik Patele645fed2020-06-12 14:46:47 +0200335 ethosu_set_clock_and_power(ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
336 return return_code;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200337}
338
339void ethosu_abort(void)
340{
341 abort_inference = true;
342}
343
344static int handle_optimizer_config(struct opt_cfg_s *opt_cfg_p)
345{
346 struct ethosu_config cfg;
347 struct ethosu_id id;
348 int return_code = 0;
349
350 LOG_INFO("handle_optimizer_config:\n");
351 LOG_INFO("Optimizer release nbr: %d patch: %d\n", opt_cfg_p->da_data.rel_nbr, opt_cfg_p->da_data.patch_nbr);
352 LOG_INFO("Optimizer config cmd_stream_version: %d macs_per_cc: %d shram_size: %d\n",
353 opt_cfg_p->cmd_stream_version,
354 opt_cfg_p->macs_per_cc,
355 opt_cfg_p->shram_size);
356 LOG_INFO("Optimizer config Ethos-U version: %d.%d.%d\n",
357 opt_cfg_p->arch_major_rev,
358 opt_cfg_p->arch_minor_rev,
359 opt_cfg_p->arch_patch_rev);
360
361 (void)ethosu_get_config(&cfg);
362 (void)ethosu_get_id(&id);
363 LOG_INFO("Ethos-U config cmd_stream_version: %d macs_per_cc: %d shram_size: %d\n",
364 cfg.cmd_stream_version,
365 cfg.macs_per_cc,
366 cfg.shram_size);
367 LOG_INFO("Ethos-U version: %d.%d.%d\n", id.arch_major_rev, id.arch_minor_rev, id.arch_patch_rev);
368
369 if ((cfg.macs_per_cc != opt_cfg_p->macs_per_cc) || (cfg.shram_size != opt_cfg_p->shram_size) ||
370 (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version))
371 {
372 if (cfg.macs_per_cc != opt_cfg_p->macs_per_cc)
373 {
374 LOG_ERR("NPU config mismatch: npu.macs_per_cc=%d optimizer.macs_per_cc=%d\n",
375 cfg.macs_per_cc,
376 opt_cfg_p->macs_per_cc);
377 }
378 if (cfg.shram_size != opt_cfg_p->shram_size)
379 {
380 LOG_ERR("NPU config mismatch: npu.shram_size=%d optimizer.shram_size=%d\n",
381 cfg.shram_size,
382 opt_cfg_p->shram_size);
383 }
384 if (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version)
385 {
386 LOG_ERR("NPU config mismatch: npu.cmd_stream_version=%d optimizer.cmd_stream_version=%d\n",
387 cfg.cmd_stream_version,
388 opt_cfg_p->cmd_stream_version);
389 }
390 return_code = -1;
391 }
392
Bhavik Patel790ef362020-06-03 10:05:28 +0200393 if ((id.product_major == PRODUCT_MAJOR_ETHOSU55) &&
Douglas Troha60d50ae2020-06-15 12:48:10 +0200394 ((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 +0200395 {
Bhavik Patel790ef362020-06-03 10:05:28 +0200396 LOG_ERR("NPU arch mismatch: npu.arch=%d.%d.%d optimizer.arch=%d.%d.%d\n",
397 id.arch_major_rev,
398 id.arch_minor_rev,
399 id.arch_patch_rev,
400 opt_cfg_p->arch_major_rev,
401 opt_cfg_p->arch_minor_rev,
402 opt_cfg_p->arch_patch_rev);
403 return_code = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200404 }
405
406#if !defined(LOG_ENABLED)
407 UNUSED(opt_cfg_p);
408#endif
409 return return_code;
410}
411
Jonny Svärd341a0df2020-05-20 17:56:37 +0200412void npu_axi_init()
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200413{
Jonny Svärd341a0df2020-05-20 17:56:37 +0200414 ethosu_set_qconfig(NPU_QCONFIG);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200415
Jonny Svärd341a0df2020-05-20 17:56:37 +0200416 ethosu_set_regioncfg(0, NPU_REGIONCFG_0);
417 ethosu_set_regioncfg(1, NPU_REGIONCFG_1);
418 ethosu_set_regioncfg(2, NPU_REGIONCFG_2);
419 ethosu_set_regioncfg(3, NPU_REGIONCFG_3);
420 ethosu_set_regioncfg(4, NPU_REGIONCFG_4);
421 ethosu_set_regioncfg(5, NPU_REGIONCFG_5);
422 ethosu_set_regioncfg(6, NPU_REGIONCFG_6);
423 ethosu_set_regioncfg(7, NPU_REGIONCFG_7);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200424
Bhavik Patel790ef362020-06-03 10:05:28 +0200425 (void)ethosu_set_axi_limit0(AXI_LIMIT0_MAX_BEATS_BYTES,
426 AXI_LIMIT0_MEM_TYPE,
427 AXI_LIMIT0_MAX_OUTSTANDING_READS,
428 AXI_LIMIT0_MAX_OUTSTANDING_WRITES);
429 (void)ethosu_set_axi_limit1(AXI_LIMIT1_MAX_BEATS_BYTES,
430 AXI_LIMIT1_MEM_TYPE,
431 AXI_LIMIT1_MAX_OUTSTANDING_READS,
432 AXI_LIMIT1_MAX_OUTSTANDING_WRITES);
433 (void)ethosu_set_axi_limit2(AXI_LIMIT2_MAX_BEATS_BYTES,
434 AXI_LIMIT2_MEM_TYPE,
435 AXI_LIMIT2_MAX_OUTSTANDING_READS,
436 AXI_LIMIT2_MAX_OUTSTANDING_WRITES);
437 (void)ethosu_set_axi_limit3(AXI_LIMIT3_MAX_BEATS_BYTES,
438 AXI_LIMIT3_MEM_TYPE,
439 AXI_LIMIT3_MAX_OUTSTANDING_READS,
440 AXI_LIMIT3_MAX_OUTSTANDING_WRITES);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200441}
442
443static int handle_command_stream(const uint8_t *cmd_stream,
444 const int cms_length,
445 const uint64_t *base_addr,
446 const int num_base_addr)
447{
448 uint32_t qread = 0;
449 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
450 LOG_INFO("handle_command_stream cms_length %d\n", cms_length);
451
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200452 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200453 {
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200454 LOG_ERR("Error: Command stream addr %p not aligned to 16 bytes\n", cmd_stream);
455 return -1;
456 }
457
458 bool base_addr_invalid = false;
459 for (int i = 0; i < num_base_addr; i++)
460 {
461 if (0 != (base_addr[i] & MASK_16_BYTE_ALIGN))
462 {
463 LOG_ERR("Error: Base addr %d: %p not aligned to 16 bytes\n", i, (void *)(base_addr[i]));
464 base_addr_invalid = true;
465 }
466 }
467 if (base_addr_invalid)
468 {
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200469 return -1;
470 }
471 npu_axi_init();
472
Bhavik Patel790ef362020-06-03 10:05:28 +0200473 if (ETHOSU_SUCCESS != ethosu_run_command_stream(cmd_stream, cms_bytes, base_addr, num_base_addr))
474 {
475 return -1;
476 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200477
478 wait_for_irq();
479
480 (void)ethosu_get_qread(&qread);
481 if (qread != cms_bytes)
482 {
483 LOG_ERR("Failure: IRQ received but qread (%d) not at end of stream (%d).\n", qread, cms_bytes);
484 return -1;
485 }
486
487 // TODO Power off
488 return 0;
489}
490
491static int read_apb_reg(uint16_t da_data)
492{
493 uint32_t *reg_p;
494 uint32_t start_address = (uint32_t)(da_data & APB_START_ADDR_MASK);
495 uint16_t num_reg = (da_data >> APB_NUM_REG_BIT_SHIFT) + 1;
496
497 reg_p = (uint32_t *)malloc(num_reg * sizeof(uint32_t));
498 if (reg_p == NULL)
499 {
500 LOG_INFO("read_apb_reg, Error! memory not allocated.");
501 return -1;
502 }
503
504 if (ETHOSU_SUCCESS == ethosu_read_apb_reg(start_address, num_reg, reg_p))
505 {
506 for (int i = 0; i < num_reg; i++)
507 {
508 LOG_INFO("NPU_REG ADDR 0x%04x = 0x%08x\n", (start_address + (i * BYTES_IN_32_BITS)), reg_p[i]);
509 }
510 }
511 else
512 {
513 free(reg_p);
514 return -1;
515 }
516
517 free(reg_p);
518 return 0;
519}
520
521static int dump_shram()
522{
523 struct ethosu_config cfg;
524 uint32_t *shram_p;
525 (void)ethosu_get_config(&cfg);
526
527 LOG_INFO("dump_shram size = %d KB\n", cfg.shram_size);
528
529 shram_p = (uint32_t *)malloc(BYTES_1KB);
530 if (shram_p == NULL)
531 {
532 LOG_ERR("read_shram, Error! memory not allocated.");
533 return -1;
534 }
535
536 for (uint32_t i = 0; i < cfg.shram_size; i++)
537 {
538 ethosu_get_shram_data(i, (uint32_t *)shram_p);
539 // Output 1KB of SHRAM
540 LOG_INFO("***SHRAM SECTION %d***\n", i);
541 for (int j = 0; j < (BYTES_1KB / BYTES_IN_32_BITS); j++)
542 {
543 LOG_INFO("[0x%04x] %x\n", (i * 1024 + j * 4), shram_p[j]);
544 }
545 }
546 free(shram_p);
547
548 return 0;
549}
550
551typedef struct
552{
553 int number;
554 const char *name;
555} name_lookup_t;
556
557static const name_lookup_t npu_reg_name_tbl[] = {
558 {0x200, "KERNEL_X"},
559 {0x204, "KERNEL_Y"},
560 {0x208, "KERNEL_W_M1"},
561 {0x20C, "KERNEL_H_M1"},
562 {0x210, "OFM_CBLK_WIDTH_M1"},
563 {0x214, "OFM_CBLK_HEIGHT_M1"},
564 {0x218, "OFM_CBLK_DEPTH_M1"},
565 {0x21c, "IFM_CBLK_DEPTH_M1"},
566 {0x220, "OFM_X"},
567 {0x224, "OFM_Y"},
568 {0x228, "OFM_Z"},
569 {0x22C, "IFM_Z"},
570 {0x230, "PAD_TOP"},
571 {0x234, "PAD_LEFT"},
572 {0x238, "IFM_CBLK_WIDTH"},
573 {0x23C, "IFM_CBLK_HEIGHT"},
574 {0x240, "DMA_IFM_SRC"},
575 {0x244, "DMA_IFM_SRC_HI"},
576 {0x248, "DMA_IFM_DST"},
577 {0x24c, "DMA_OFM_SRC"},
578 {0x250, "DMA_OFM_DST"},
579 {0x254, "DMA_OFM_DST_HI"},
580 {0x258, "DMA_WEIGHT_SRC"},
581 {0x25c, "DMA_WEIGHT_SRC_HI"},
582 {0x260, "DMA_CMD_SRC"},
583 {0x264, "DMA_CMD_SRC_HI"},
584 {0x268, "DMA_CMD_SIZE"},
585 {0x26c, "DMA_M2M_SRC"},
586 {0x270, "DMA_M2M_SRC_HI"},
587 {0x274, "DMA_M2M_DST"},
588 {0x278, "DMA_M2M_DST_HI"},
589 {0x27c, "CURRENT_QREAD"},
590 {0x280, "DMA_SCALE_SRC"},
591 {0x284, "DMA_SCALE_SRC_HI"},
592 {0x2BC, "CURRENT_CMD"},
593 {0x800, "IFM_PAD_TOP"},
594 {0x804, "IFM_PAD_LEFT"},
595 {0x808, "IFM_PAD_RIGHT"},
596 {0x80C, "IFM_PAD_BOTTOM"},
597 {0x810, "IFM_DEPTH_M1"},
598 {0x814, "IFM_PRECISION"},
599 {0x81C, "IFM_UPSCALE"},
600 {0x824, "IFM_ZERO_POINT"},
601 {0x828, "IFM_WIDTH0_M1"},
602 {0x82C, "IFM_HEIGHT0_M1"},
603 {0x830, "IFM_HEIGHT1_M1"},
604 {0x834, "IFM_IB_END"},
605 {0x83C, "IFM_REGION"},
606 {0x844, "OFM_WIDTH_M1"},
607 {0x848, "OFM_HEIGHT_M1"},
608 {0x84C, "OFM_DEPTH_M1"},
609 {0x850, "OFM_PRECISION"},
610 {0x854, "OFM_BLK_WIDTH_M1"},
611 {0x858, "OFM_BLK_HEIGHT_M1"},
612 {0x85C, "OFM_BLK_DEPTH_M1"},
613 {0x860, "OFM_ZERO_POINT"},
614 {0x868, "OFM_WIDTH0_M1"},
615 {0x86C, "OFM_HEIGHT0_M1"},
616 {0x870, "OFM_HEIGHT1_M1"},
617 {0x87C, "OFM_REGION"},
618 {0x880, "KERNEL_WIDTH_M1"},
619 {0x884, "KERNEL_HEIGHT_M1"},
620 {0x888, "KERNEL_STRIDE"},
621 {0x88C, "PARALLEL_MODE"},
622 {0x890, "ACC_FORMAT"},
623 {0x894, "ACTIVATION"},
624 {0x898, "ACTIVATION_MIN"},
625 {0x89C, "ACTIVATION_MAX"},
626 {0x8A0, "WEIGHT_REGION"},
627 {0x8A4, "SCALE_REGION"},
628 {0x8B4, "AB_START"},
629 {0x8BC, "BLOCKDEP"},
630 {0x8C0, "DMA0_SRC_REGION"},
631 {0x8C4, "DMA0_DST_REGION"},
632 {0x8C8, "DMA0_SIZE0"},
633 {0x8CC, "DMA0_SIZE1"},
634 {0x900, "IFM2_BROADCAST"},
635 {0x904, "IFM2_SCALAR"},
636 {0x924, "IFM2_ZERO_POINT"},
637 {0x928, "IFM2_WIDTH0_M1"},
638 {0x92C, "IFM2_HEIGHT0_M1"},
639 {0x930, "IFM2_HEIGHT1_M1"},
640 {0x934, "IFM2_IB_START"},
641 {0x93C, "IFM2_REGION"},
642 {0xA00, "IFM_BASE0"},
643 {0xA04, "IFM_BASE0_HI"},
644 {0xA08, "IFM_BASE1"},
645 {0xA0C, "IFM_BASE1_HI"},
646 {0xA10, "IFM_BASE2"},
647 {0xA14, "IFM_BASE2_HI"},
648 {0xA18, "IFM_BASE3"},
649 {0xA1C, "IFM_BASE3_HI"},
650 {0xA20, "IFM_STRIDE_X"},
651 {0xA24, "IFM_STRIDE_X_HI"},
652 {0xA28, "IFM_STRIDE_Y"},
653 {0xA2C, "IFM_STRIDE_Y_HI"},
654 {0xA30, "IFM_STRIDE_C"},
655 {0xA34, "IFM_STRIDE_C_HI"},
656 {0xA40, "OFM_BASE0"},
657 {0xA44, "OFM_BASE0_HI"},
658 {0xA48, "OFM_BASE1"},
659 {0xA4C, "OFM_BASE1_HI"},
660 {0xA50, "OFM_BASE2"},
661 {0xA54, "OFM_BASE2_HI"},
662 {0xA58, "OFM_BASE3"},
663 {0xA5C, "OFM_BASE3_HI"},
664 {0xA60, "OFM_STRIDE_X"},
665 {0xA64, "OFM_STRIDE_X_HI"},
666 {0xA68, "OFM_STRIDE_Y"},
667 {0xA6C, "OFM_STRIDE_Y_HI"},
668 {0xA70, "OFM_STRIDE_C"},
669 {0xA74, "OFM_STRIDE_C_HI"},
670 {0xA80, "WEIGHT_BASE"},
671 {0xA84, "WEIGHT_BASE_HI"},
672 {0xA88, "WEIGHT_LENGTH"},
673 {0xA8C, "WEIGHT_LENGTH_HI"},
674 {0xA90, "SCALE_BASE"},
675 {0xA94, "SCALE_BASE_HI"},
676 {0xA98, "SCALE_LENGTH"},
677 {0xAA0, "OFM_SCALE"},
678 {0xAA4, "OFM_SCALE_SHIFT"},
679 {0xAA8, "OPA_SCALE "},
680 {0xAB0, "OPB_SCALE"},
681 {0xAC0, "DMA0_SRC"},
682 {0xAC4, "DMA0_SRC_HI"},
683 {0xAC8, "DMA0_DST"},
684 {0xACC, "DMA0_DST_HI"},
685 {0xAD0, "DMA0_LEN"},
686 {0xAD4, "DMA0_LEN_HI"},
687 {0xAD8, "DMA0_SKIP0"},
688 {0xADC, "DMA0_SKIP0_HI"},
689 {0xAE0, "DMA0_SKIP1"},
690 {0xAE4, "DMA0_SKIP1_HI"},
691 {0xB00, "IFM2_BASE0"},
692 {0xB04, "IFM2_BASE0_HI"},
693 {0xB08, "IFM2_BASE1"},
694 {0xB0C, "IFM2_BASE1_HI"},
695 {0xB10, "IFM2_BASE2"},
696 {0xB14, "IFM2_BASE2_HI"},
697 {0xB18, "IFM2_BASE3"},
698 {0xB1C, "IFM2_BASE3_HI"},
699 {0xB20, "IFM2_STRIDE_X"},
700 {0xB24, "IFM2_STRIDE_X_HI"},
701 {0xB28, "IFM2_STRIDE_Y"},
702 {0xB2C, "IFM2_STRIDE_Y_HI"},
703 {0xB30, "IFM2_STRIDE_C"},
704 {0xB34, "IFM2_STRIDE_C_HI"},
705 {0xB40, "WEIGHT1_BASE"},
706 {0xB44, "WEIGHT1_BASE_HI"},
707 {0xB48, "WEIGHT1_LENGTH"},
708 {0xB4C, "WEIGHT1_LENGTH_HI"},
709 {0xB50, "SCALE1_BASE"},
710 {0xB54, "SCALE1_BASE_HI"},
711 {0xB58, "SCALE1_LENGTH"},
712};
713
714static const char *lookup_name(const name_lookup_t *lookup_table, int lookup_table_count, int find)
715{
716 int n;
717 for (n = 0; n < lookup_table_count; n++)
718 {
719 if (lookup_table[n].number == find)
720 {
721 return lookup_table[n].name;
722 }
723 }
724 // Not found
725 return 0;
726}
727
728static void dump_npu_register(int npu_reg, int npu_reg_end)
729{
730 unsigned int reg_val;
731 const char *reg_name;
732 int npu_reg_name_tbl_count = sizeof(npu_reg_name_tbl) / sizeof(npu_reg_name_tbl[0]);
733
734 LOG_INFO("dump_register %X - %X\n", npu_reg, npu_reg_end);
735 for (; npu_reg <= npu_reg_end; npu_reg += sizeof(int))
736 {
737 reg_val = read_reg(npu_reg);
738 reg_name = lookup_name(npu_reg_name_tbl, npu_reg_name_tbl_count, npu_reg);
739 LOG_INFO("[0x%.4X] 0x%.8X\t%s\n", npu_reg, reg_val, (reg_name) ? reg_name : "");
740 }
741}
742
743static const name_lookup_t cmd0_name_tbl[] = {
744 {0x000, "NPU_OP_STOP"},
745 {0x001, "NPU_OP_IRQ"},
746 {0x002, "NPU_OP_CONV"},
747 {0x003, "NPU_OP_DEPTHWISE"},
748 {0x004, "NPU_OP_VECTOR_PROD"},
749 {0x005, "NPU_OP_POOL"},
750 {0x006, "NPU_OP_ELEMENTWISE"},
751 {0x010, "NPU_OP_DMA_START"},
752 {0x011, "NPU_OP_DMA_WAIT"},
753 {0x012, "NPU_OP_KERNEL_WAIT"},
754 {0x100, "NPU_SET_IFM_PAD_TOP"},
755 {0x101, "NPU_SET_IFM_PAD_LEFT"},
756 {0x102, "NPU_SET_IFM_PAD_RIGHT"},
757 {0x103, "NPU_SET_IFM_PAD_BOTTOM"},
758 {0x104, "NPU_SET_IFM_DEPTH_M1"},
759 {0x105, "NPU_SET_IFM_PRECISION"},
760 {0x107, "NPU_SET_IFM_UPSCALE"},
761 {0x109, "NPU_SET_IFM_ZERO_POINT"},
762 {0x10A, "NPU_SET_IFM_WIDTH0_M1"},
763 {0x10B, "NPU_SET_IFM_HEIGHT0_M1"},
764 {0x10C, "NPU_SET_IFM_HEIGHT1_M1"},
765 {0x10D, "NPU_SET_IFM_IB_END"},
766 {0x10F, "NPU_SET_IFM_REGION"},
767 {0x110, "NPU_SET_OFM_BATCH_SIZE_M1"},
768 {0x111, "NPU_SET_OFM_WIDTH_M1"},
769 {0x112, "NPU_SET_OFM_HEIGHT_M1"},
770 {0x113, "NPU_SET_OFM_DEPTH_M1"},
771 {0x114, "NPU_SET_OFM_PRECISION"},
772 {0x115, "NPU_SET_OFM_BLK_WIDTH_M1"},
773 {0x116, "NPU_SET_OFM_BLK_HEIGHT_M1"},
774 {0x117, "NPU_SET_OFM_BLK_DEPTH_M1"},
775 {0x118, "NPU_SET_OFM_ZERO_POINT"},
776 {0x11A, "NPU_SET_OFM_WIDTH0_M1"},
777 {0x11B, "NPU_SET_OFM_HEIGHT0_M1"},
778 {0x11C, "NPU_SET_OFM_HEIGHT1_M1"},
779 {0x11F, "NPU_SET_OFM_REGION"},
780 {0x120, "NPU_SET_KERNEL_WIDTH_M1"},
781 {0x121, "NPU_SET_KERNEL_HEIGHT_M1"},
782 {0x122, "NPU_SET_KERNEL_STRIDE"},
783 {0x124, "NPU_SET_ACC_FORMAT"},
784 {0x125, "NPU_SET_ACTIVATION"},
785 {0x126, "NPU_SET_ACTIVATION_MIN"},
786 {0x127, "NPU_SET_ACTIVATION_MAX"},
787 {0x128, "NPU_SET_WEIGHT_REGION"},
788 {0x129, "NPU_SET_SCALE_REGION"},
789 {0x12D, "NPU_SET_AB_START"},
790 {0x12F, "NPU_SET_BLOCKDEP"},
791 {0x130, "NPU_SET_DMA0_SRC_REGION"},
792 {0x131, "NPU_SET_DMA0_DST_REGION"},
793 {0x180, "NPU_SET_IFM2_BROADCAST"},
794 {0x181, "NPU_SET_IFM2_SCALAR"},
795 {0x185, "NPU_SET_IFM2_PRECISION"},
796 {0x189, "NPU_SET_IFM2_ZERO_POINT"},
797 {0x18A, "NPU_SET_IFM2_WIDTH0_M1"},
798 {0x18B, "NPU_SET_IFM2_HEIGHT0_M1"},
799 {0x18C, "NPU_SET_IFM2_HEIGHT1_M1"},
800 {0x18D, "NPU_SET_IFM2_IB_START"},
801 {0x18F, "NPU_SET_IFM2_REGION"},
802};
803
804static const name_lookup_t cmd1_name_tbl[] = {
805 {0x000, "NPU_SET_IFM_BASE0"}, {0x001, "NPU_SET_IFM_BASE1"}, {0x002, "NPU_SET_IFM_BASE2"},
806 {0x003, "NPU_SET_IFM_BASE3"}, {0x004, "NPU_SET_IFM_STRIDE_X"}, {0x005, "NPU_SET_IFM_STRIDE_Y"},
807 {0x006, "NPU_SET_IFM_STRIDE_C"}, {0x007, "NPU_SET_IFM_STRIDE_N"}, {0x010, "NPU_SET_OFM_BASE0"},
808 {0x011, "NPU_SET_OFM_BASE1"}, {0x012, "NPU_SET_OFM_BASE2"}, {0x013, "NPU_SET_OFM_BASE3"},
809 {0x014, "NPU_SET_OFM_STRIDE_X"}, {0x015, "NPU_SET_OFM_STRIDE_Y"}, {0x016, "NPU_SET_OFM_STRIDE_C"},
810 {0x017, "NPU_SET_OFM_STRIDE_N"}, {0x020, "NPU_SET_WEIGHT_BASE"}, {0x021, "NPU_SET_WEIGHT_LENGTH"},
811 {0x022, "NPU_SET_SCALE_BASE"}, {0x023, "NPU_SET_SCALE_LENGTH"}, {0x024, "NPU_SET_OFM_SCALE"},
812 {0x025, "NPU_SET_OPA_SCALE"}, {0x026, "NPU_SET_OPB_SCALE"}, {0x030, "NPU_SET_DMA0_SRC"},
813 {0x031, "NPU_SET_DMA0_DST"}, {0x032, "NPU_SET_DMA0_LEN"}, {0x080, "NPU_SET_IFM2_BASE0"},
814 {0x081, "NPU_SET_IFM2_BASE1"}, {0x082, "NPU_SET_IFM2_BASE2"}, {0x083, "NPU_SET_IFM2_BASE3"},
815 {0x084, "NPU_SET_IFM2_STRIDE_X"}, {0x085, "NPU_SET_IFM2_STRIDE_Y"}, {0x086, "NPU_SET_IFM2_STRIDE_C"},
816};
817
818static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread)
819{
820 int n;
821 int offset;
822 uint32_t cmd_val;
823 const uint8_t *cmd_ptr;
824 const char *cmd_name;
825 int cmd0_name_tbl_count = sizeof(cmd0_name_tbl) / sizeof(cmd0_name_tbl[0]);
826 int cmd1_name_tbl_count = sizeof(cmd1_name_tbl) / sizeof(cmd1_name_tbl[0]);
827
828 LOG_INFO("dump_command_stream cmd_stream = 0x%8p cms_length = %d\n", cmd_stream, cms_length);
829 for (n = 0; n < cms_length; n++)
830 {
831 // Offset
832 offset = n * sizeof(int);
833 LOG_INFO("[%.4d] ", offset);
834 // Command
835 cmd_ptr = (const uint8_t *)&cmd_stream[n];
836 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
837 // Command name and payload
838 if (cmd_stream[n] & 0x4000)
839 {
840 cmd_name = lookup_name(cmd1_name_tbl, cmd1_name_tbl_count, cmd_stream[n] & 0x3FF);
841 n++;
842 cmd_val = cmd_stream[n];
843 cmd_ptr = (const uint8_t *)&cmd_stream[n];
844 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
845 }
846 else
847 {
848 cmd_val = cmd_stream[n] >> 16;
849 cmd_name = lookup_name(cmd0_name_tbl, cmd0_name_tbl_count, cmd_stream[n] & 0x3FF);
850 }
851 if (cmd_name)
852 {
853 LOG_INFO("\t%s 0x%.8X", cmd_name, cmd_val);
854 }
855 if (offset == qread)
856 {
857 LOG_INFO(" <<== QREAD\n");
858 }
859 else
860 {
861 LOG_INFO("\n");
862 }
863 }
864}