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