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