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