blob: 2eedfe35f4bc748b935ac733ec5872015a2a04b3 [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
2 * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020019/******************************************************************************
20 * Includes
21 ******************************************************************************/
22
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020023#include "ethosu_driver.h"
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020024#include "ethosu_common.h"
Bhavik Pateldae5be02020-06-18 15:25:15 +020025#include "ethosu_config.h"
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020026#include "ethosu_device.h"
Per Åstrand25d78c02020-04-21 14:19:44 +020027
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020028#include <assert.h>
Per Åstrand25d78c02020-04-21 14:19:44 +020029#include <cmsis_compiler.h>
Per Åstrand14ccfee2020-09-25 10:40:20 +020030#include <inttypes.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020031#include <stdbool.h>
Bhavik Patelbf7ae632020-06-11 21:00:16 +020032#include <stddef.h>
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020033#include <stdio.h>
34#include <stdlib.h>
35
Kristofer Jonsson2b201c32020-09-02 16:42:43 +020036/******************************************************************************
37 * Defines
38 ******************************************************************************/
39
40#define MACS_PER_CYCLE_LOG2_MASK 0x000F
41#define SHRAM_SIZE_MASK 0xFF00
42#define SHRAM_SIZE_RIGHT_SHIFT 8
43#define BYTES_IN_32_BITS 4
44#define CUSTOM_OPTION_LENGTH_32_BIT_WORD 1
45#define DRIVER_ACTION_LENGTH_32_BIT_WORD 1
46#define OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD 2
47#define ETHOSU_FOURCC ('1' << 24 | 'P' << 16 | 'O' << 8 | 'C') // "Custom Operator Payload 1"
48#define APB_START_ADDR_MASK 0x0FFF
49#define APB_NUM_REG_BIT_SHIFT 12
50#define BYTES_1KB 1024
51#define PRODUCT_MAJOR_ETHOSU55 (4)
52#define MASK_16_BYTE_ALIGN (0xF)
53#define FAST_MEMORY_BASE_ADDR_INDEX 2
54
55/******************************************************************************
56 * Types
57 ******************************************************************************/
58
59// Driver actions
60enum DRIVER_ACTION_e
61{
62 RESERVED = 0,
63 OPTIMIZER_CONFIG = 1,
64 COMMAND_STREAM = 2,
65 READ_APB_REG = 3,
66 DUMP_SHRAM = 4,
67 NOP = 5,
68};
69
70// Custom data struct
71struct custom_data_s
72{
73 union
74 {
75 // Driver action data
76 struct
77 {
78 // Driver action command (valid values in DRIVER_ACTION_e)
79 uint8_t driver_action_command;
80
81 // reserved
82 uint8_t reserved;
83
84 // Driver action data
85 union
86 {
87 // DA_CMD_OPT_CFG
88 struct
89 {
90 uint16_t rel_nbr : 4;
91 uint16_t patch_nbr : 4;
92 uint16_t opt_cfg_reserved : 8;
93 };
94
95 // DA_CMD_CMSTRM
96 struct
97 {
98 uint16_t length;
99 };
100
101 // DA_CMD_READAPB
102 struct
103 {
104 uint16_t start_address : 12;
105 uint16_t nbr_reg_minus1 : 4;
106 };
107
108 uint16_t driver_action_data;
109 };
110 };
111
112 uint32_t word;
113 };
114};
115
116// optimizer config struct
117struct opt_cfg_s
118{
119 struct custom_data_s da_data;
120 union
121 {
122 struct
123 {
124 uint32_t macs_per_cc : 4;
125 uint32_t cmd_stream_version : 4;
126 uint32_t shram_size : 8;
127 uint32_t reserved1 : 16;
128 };
129 uint32_t npu_cfg;
130 };
131 union
132 {
133 struct
134 {
135 uint32_t version_status : 4;
136 uint32_t version_minor : 4;
137 uint32_t version_major : 4;
138 uint32_t product_major : 4;
139 uint32_t arch_patch_rev : 4;
140 uint32_t arch_minor_rev : 8;
141 uint32_t arch_major_rev : 4;
142 };
143 uint32_t ethosu_id;
144 };
145};
146
147/******************************************************************************
148 * Functions
149 ******************************************************************************/
150
Kristofer Jonssonef387ea2020-08-25 16:32:21 +0200151struct ethosu_driver ethosu_drv = {
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100152 .dev = {.base_address = NULL, .proto = 0, .pmccntr = {0}, .pmu_evcntr = {0, 0, 0, 0}, .pmu_evtypr = {0, 0, 0, 0}},
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100153 .abort_inference = false,
154 .status_error = false,
155 .dev_power_always_on = false};
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200156
Anton Moberg61da4d32020-12-22 16:00:31 +0100157// Registered drivers linked list HEAD
158static struct ethosu_driver *registered_drivers = NULL;
159
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200160// IRQ
161static volatile bool irq_triggered = false;
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100162static int ethosu_soft_reset_and_restore(struct ethosu_driver *drv);
Anton Moberg61da4d32020-12-22 16:00:31 +0100163
164void ethosu_irq_handler_v2(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200165{
166 uint8_t irq_raised = 0;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200167
168 LOG_DEBUG("Interrupt. status=0x%08x, qread=%d\n",
Anton Moberg61da4d32020-12-22 16:00:31 +0100169 ethosu_read_reg(&drv->dev, NPU_REG_STATUS),
170 ethosu_read_reg(&drv->dev, NPU_REG_QREAD));
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200171
172 // Verify that interrupt has been raised
Anton Moberg61da4d32020-12-22 16:00:31 +0100173 (void)ethosu_is_irq_raised(&drv->dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200174 ASSERT(irq_raised == 1);
175 irq_triggered = true;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200176
177 // Clear interrupt
Anton Moberg61da4d32020-12-22 16:00:31 +0100178 (void)ethosu_clear_irq_status(&drv->dev);
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200179
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200180 // Verify that interrupt has been successfully cleared
Anton Moberg61da4d32020-12-22 16:00:31 +0100181 (void)ethosu_is_irq_raised(&drv->dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200182 ASSERT(irq_raised == 0);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200183
Anton Moberg61da4d32020-12-22 16:00:31 +0100184 if (ethosu_status_has_error(&drv->dev))
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200185 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100186 ethosu_soft_reset_and_restore(drv);
187 drv->status_error = true;
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200188 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200189}
190
Bhavik Pateldae5be02020-06-18 15:25:15 +0200191static inline void wait_for_irq(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200192{
193 while (1)
194 {
195 __disable_irq();
Bhavik Pateldae5be02020-06-18 15:25:15 +0200196 if (irq_triggered || drv->abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200197 {
198 __enable_irq();
199 break;
200 }
201
Per Åstrand25d78c02020-04-21 14:19:44 +0200202 __WFI();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200203
204 __enable_irq();
205 }
206}
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200207
Bhavik Pateldae5be02020-06-18 15:25:15 +0200208static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p);
209static int handle_command_stream(struct ethosu_driver *drv,
210 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200211 const int cms_length,
212 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200213 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200214 const int num_base_addr);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200215static int read_apb_reg(struct ethosu_driver *drv, uint16_t);
216static int dump_shram(struct ethosu_driver *drv);
217static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200218static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200219static void npu_axi_init(struct ethosu_driver *drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200220
Anton Moberg61da4d32020-12-22 16:00:31 +0100221int ethosu_init_v4(struct ethosu_driver *drv,
222 const void *base_address,
Per Åstrande6498f02020-11-09 15:33:12 +0100223 const void *fast_memory,
224 const size_t fast_memory_size,
225 uint32_t secure_enable,
226 uint32_t privilege_enable)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200227{
228 int return_code = 0;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200229
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100230 LOG_INFO("%s. base_address=%p, fast_memory=%p, fast_memory_size=%zu, secure=%" PRIu32 ", privileged=%" PRIu32 "\n",
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200231 __FUNCTION__,
232 base_address,
233 fast_memory,
Per Åstrande6498f02020-11-09 15:33:12 +0100234 fast_memory_size,
235 secure_enable,
236 privilege_enable);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200237
Anton Moberg61da4d32020-12-22 16:00:31 +0100238 ethosu_register_driver(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200239
Anton Moberg61da4d32020-12-22 16:00:31 +0100240 drv->fast_memory = (uint32_t)fast_memory;
241 drv->fast_memory_size = fast_memory_size;
242
243 if (ETHOSU_SUCCESS != ethosu_dev_init(&drv->dev, base_address, secure_enable, privilege_enable))
Bhavik Pateldae5be02020-06-18 15:25:15 +0200244 {
245 LOG_ERR("Failed in ethosu_dev_init");
246 return -1;
247 }
248
Anton Moberg61da4d32020-12-22 16:00:31 +0100249 if (ETHOSU_SUCCESS != ethosu_set_clock_and_power(&drv->dev, ETHOSU_CLOCK_Q_DISABLE, ETHOSU_POWER_Q_DISABLE))
Bhavik Patele645fed2020-06-12 14:46:47 +0200250 {
251 LOG_ERR("Failed to disable clock-q & power-q for Ethos-U\n");
252 return -1;
253 }
254
Anton Moberg61da4d32020-12-22 16:00:31 +0100255 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
Per Åstrand849cf692020-11-24 07:39:55 +0100256 {
257 return -1;
258 }
Kristofer Jonssondaa0d202020-05-12 12:23:16 +0200259
Anton Moberg61da4d32020-12-22 16:00:31 +0100260 if (ETHOSU_SUCCESS != ethosu_wait_for_reset(&drv->dev))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200261 {
262 LOG_ERR("Failed reset of Ethos-U\n");
263 return -1;
264 }
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100265
Anton Moberg61da4d32020-12-22 16:00:31 +0100266 drv->status_error = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200267
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200268 return return_code;
269}
270
Anton Moberg61da4d32020-12-22 16:00:31 +0100271int ethosu_get_version_v2(struct ethosu_driver *drv, struct ethosu_version *version)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200272{
273 int return_code = 0;
274
275 if (NULL != version)
276 {
277 struct ethosu_id id;
278 struct ethosu_config cfg;
Anton Moberg61da4d32020-12-22 16:00:31 +0100279 (void)ethosu_get_id(&drv->dev, &id);
280 (void)ethosu_get_config(&drv->dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200281
282 version->id.version_status = id.version_status;
283 version->id.version_minor = id.version_minor;
284 version->id.version_major = id.version_major;
285 version->id.product_major = id.product_major;
286 version->id.arch_patch_rev = id.arch_patch_rev;
287 version->id.arch_minor_rev = id.arch_minor_rev;
288 version->id.arch_major_rev = id.arch_major_rev;
289 version->id.driver_patch_rev = ETHOSU_DRIVER_VERSION_PATCH;
290 version->id.driver_minor_rev = ETHOSU_DRIVER_VERSION_MINOR;
291 version->id.driver_major_rev = ETHOSU_DRIVER_VERSION_MAJOR;
292 version->cfg.macs_per_cc = cfg.macs_per_cc;
293 version->cfg.cmd_stream_version = cfg.cmd_stream_version;
294 version->cfg.shram_size = cfg.shram_size;
295 }
296 else
297 {
298 return_code = -1;
299 }
300
301 return return_code;
302}
303
Anton Moberg61da4d32020-12-22 16:00:31 +0100304int ethosu_invoke_v3(struct ethosu_driver *drv,
305 const void *custom_data_ptr,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200306 const int custom_data_size,
307 const uint64_t *base_addr,
308 const size_t *base_addr_size,
309 const int num_base_addr)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200310{
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200311 const struct custom_data_s *data_ptr = custom_data_ptr;
312 const struct custom_data_s *data_end = custom_data_ptr + custom_data_size;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200313 int return_code = 0;
314
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200315 LOG_INFO("%s\n", __FUNCTION__);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200316
317 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200318 if (data_ptr->word != ETHOSU_FOURCC)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200319 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200320 LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x\n", data_ptr->word, ETHOSU_FOURCC);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200321 return -1;
322 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200323
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200324 // Custom data length must be a multiple of 32 bits
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200325 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
326 {
327 LOG_ERR("ethosu_invoke ERROR custom_data_size=0x%x not a multiple of 4\n", custom_data_size);
328 return -1;
329 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200330
331 ++data_ptr;
332
333 // Adjust base address to fast memory area
Anton Moberg61da4d32020-12-22 16:00:31 +0100334 if (drv->fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200335 {
336 uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX];
337
Anton Moberg61da4d32020-12-22 16:00:31 +0100338 if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > drv->fast_memory_size)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200339 {
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100340 LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u\n",
Anton Moberg61da4d32020-12-22 16:00:31 +0100341 drv->fast_memory_size,
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100342 base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]);
343 return -1;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200344 }
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100345
Anton Moberg61da4d32020-12-22 16:00:31 +0100346 *fast_memory = drv->fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200347 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200348
Anton Moberg61da4d32020-12-22 16:00:31 +0100349 if (!drv->dev_power_always_on)
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200350 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100351 // Only soft reset if securty state or privilege level needs changing
352 if (drv->dev.proto != ethosu_read_reg(&drv->dev, NPU_REG_PROT))
Per Åstrand849cf692020-11-24 07:39:55 +0100353 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100354 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100355 {
356 return -1;
357 }
Per Åstrand849cf692020-11-24 07:39:55 +0100358 }
Anton Moberg61da4d32020-12-22 16:00:31 +0100359
360 drv->status_error = false;
361 ethosu_set_clock_and_power(&drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
362 ethosu_restore_pmu_config(&drv->dev);
363 npu_axi_init(drv);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200364 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100365
Anton Moberg61da4d32020-12-22 16:00:31 +0100366 drv->status_error = false;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200367
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200368 while (data_ptr < data_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200369 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200370 int ret = 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200371 switch (data_ptr->driver_action_command)
372 {
373 case OPTIMIZER_CONFIG:
374 LOG_INFO("ethosu_invoke OPTIMIZER_CONFIG\n");
375 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
376
Anton Moberg61da4d32020-12-22 16:00:31 +0100377 ret = handle_optimizer_config(drv, opt_cfg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200378 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
379 break;
380 case COMMAND_STREAM:
381 LOG_INFO("ethosu_invoke COMMAND_STREAM\n");
382 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct custom_data_s);
383 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
384
Anton Moberg61da4d32020-12-22 16:00:31 +0100385 drv->abort_inference = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200386 // It is safe to clear this flag without atomic, because npu is not running.
387 irq_triggered = false;
388
Anton Moberg61da4d32020-12-22 16:00:31 +0100389 ret = handle_command_stream(drv, command_stream, cms_length, base_addr, base_addr_size, num_base_addr);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200390
Anton Moberg61da4d32020-12-22 16:00:31 +0100391 if (return_code == -1 && drv->abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200392 {
393 uint32_t qread = 0;
Anton Moberg61da4d32020-12-22 16:00:31 +0100394 ethosu_get_qread(&drv->dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200395 LOG_ERR("NPU timeout\n");
396 dump_command_stream(command_stream, cms_length, qread);
Anton Moberg61da4d32020-12-22 16:00:31 +0100397 dump_npu_register(drv, 0x200, 0x2BF);
398 dump_npu_register(drv, 0x800, 0xB3F);
399 dump_shram(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200400 }
401
402 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
403 break;
404 case READ_APB_REG:
405 LOG_INFO("ethosu_invoke READ_APB_REG\n");
Anton Moberg61da4d32020-12-22 16:00:31 +0100406 ret = read_apb_reg(drv, data_ptr->driver_action_data);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200407 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
408 break;
409 case DUMP_SHRAM:
410 LOG_INFO("ethosu_invoke DUMP_SHRAM\n");
Anton Moberg61da4d32020-12-22 16:00:31 +0100411 ret = dump_shram(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200412 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
413 break;
414 case NOP:
415 LOG_INFO("ethosu_invoke NOP\n");
416 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
417 break;
418 default:
419 LOG_ERR("ethosu_invoke UNSUPPORTED driver_action_command %d \n", data_ptr->driver_action_command);
Bhavik Patele645fed2020-06-12 14:46:47 +0200420 ret = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200421 break;
422 }
Bhavik Patele645fed2020-06-12 14:46:47 +0200423 if (ret != 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200424 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200425 return_code = -1;
426 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200427 }
428 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200429
Anton Moberg61da4d32020-12-22 16:00:31 +0100430 if (!drv->status_error && !drv->dev_power_always_on)
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200431 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100432 ethosu_save_pmu_counters(&drv->dev);
433 ethosu_set_clock_and_power(&drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200434 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200435
Bhavik Patele645fed2020-06-12 14:46:47 +0200436 return return_code;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200437}
438
Anton Moberg61da4d32020-12-22 16:00:31 +0100439void ethosu_abort_v2(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200440{
Anton Moberg61da4d32020-12-22 16:00:31 +0100441 drv->abort_inference = true;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200442}
443
Anton Moberg61da4d32020-12-22 16:00:31 +0100444void ethosu_set_power_mode_v2(struct ethosu_driver *drv, bool always_on)
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100445{
Anton Moberg61da4d32020-12-22 16:00:31 +0100446 drv->dev_power_always_on = always_on;
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100447
448 if (always_on)
449 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100450 npu_axi_init(drv);
451 }
452}
453
454int ethosu_register_driver(struct ethosu_driver *drv)
455{
456 // Safeguard check for if driver is already registered
457 struct ethosu_driver *cur = registered_drivers;
458 while (cur != NULL)
459 {
460 if (cur == drv)
461 {
462 LOG_ERR("%s: NPU driver at address %p is already registered.\n", __FUNCTION__, drv);
463 return -1;
464 }
465 cur = cur->next;
466 }
467
468 drv->next = registered_drivers;
469 // Designate new registered driver HEAD
470 registered_drivers = drv;
471
472 LOG_INFO("%s: New NPU driver at address %p is registered.\n", __FUNCTION__, drv);
473
474 return 0;
475}
476
477int ethosu_deregister_driver(struct ethosu_driver *drv)
478{
479 struct ethosu_driver *cur = registered_drivers;
480 struct ethosu_driver **prev = &registered_drivers;
481
482 while (cur != NULL)
483 {
484 if (cur == drv)
485 {
486 *prev = cur->next;
487 LOG_INFO("%s: NPU driver at address %p is deregistered.\n", __FUNCTION__, drv);
488 return 0;
489 }
490
491 prev = &cur->next;
492 cur = cur->next;
493 }
494
495 LOG_ERR("%s: NPU driver at address %p does not match a registered driver and therefore may not be deregistered.\n",
496 __FUNCTION__,
497 drv);
498 return -1;
499}
500
501struct ethosu_driver *ethosu_reserve_driver(void)
502{
503 struct ethosu_driver *drv = registered_drivers;
504
505 while (drv != NULL)
506 {
507 if (!drv->reserved)
508 {
509 drv->reserved = true;
510 LOG_INFO("%s - Driver %p reserved.\n", __FUNCTION__, drv);
511 return drv;
512 }
513 drv = drv->next;
514 }
515
516 LOG_INFO("%s: No available drivers.\n", __FUNCTION__, drv);
517
518 return NULL;
519}
520
521void ethosu_release_driver(struct ethosu_driver *drv)
522{
523 if (drv != NULL && drv->reserved)
524 {
525 drv->reserved = false;
526 LOG_INFO("%s - Driver %p released\n", __FUNCTION__, drv);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100527 }
528}
529
530static int ethosu_soft_reset_and_restore(struct ethosu_driver *drv)
531{
532
533 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
534 {
535 return -1;
536 }
537
538 ethosu_set_clock_and_power(&drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
539
540 npu_axi_init(drv);
541 ethosu_restore_pmu_config(&drv->dev);
542
543 return 0;
544}
545
Bhavik Pateldae5be02020-06-18 15:25:15 +0200546static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200547{
548 struct ethosu_config cfg;
549 struct ethosu_id id;
550 int return_code = 0;
551
552 LOG_INFO("handle_optimizer_config:\n");
553 LOG_INFO("Optimizer release nbr: %d patch: %d\n", opt_cfg_p->da_data.rel_nbr, opt_cfg_p->da_data.patch_nbr);
554 LOG_INFO("Optimizer config cmd_stream_version: %d macs_per_cc: %d shram_size: %d\n",
555 opt_cfg_p->cmd_stream_version,
556 opt_cfg_p->macs_per_cc,
557 opt_cfg_p->shram_size);
558 LOG_INFO("Optimizer config Ethos-U version: %d.%d.%d\n",
559 opt_cfg_p->arch_major_rev,
560 opt_cfg_p->arch_minor_rev,
561 opt_cfg_p->arch_patch_rev);
562
Bhavik Pateldae5be02020-06-18 15:25:15 +0200563 (void)ethosu_get_config(&drv->dev, &cfg);
564 (void)ethosu_get_id(&drv->dev, &id);
Per Åstrand14ccfee2020-09-25 10:40:20 +0200565 LOG_INFO("Ethos-U config cmd_stream_version: %" PRIu32 " macs_per_cc: %" PRIu32 " shram_size: %" PRIu32 "\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200566 cfg.cmd_stream_version,
567 cfg.macs_per_cc,
568 cfg.shram_size);
Per Åstrand14ccfee2020-09-25 10:40:20 +0200569 LOG_INFO("Ethos-U version: %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
570 id.arch_major_rev,
571 id.arch_minor_rev,
572 id.arch_patch_rev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200573
574 if ((cfg.macs_per_cc != opt_cfg_p->macs_per_cc) || (cfg.shram_size != opt_cfg_p->shram_size) ||
575 (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version))
576 {
577 if (cfg.macs_per_cc != opt_cfg_p->macs_per_cc)
578 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200579 LOG_ERR("NPU config mismatch: npu.macs_per_cc=%" PRIu32 " optimizer.macs_per_cc=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200580 cfg.macs_per_cc,
581 opt_cfg_p->macs_per_cc);
582 }
583 if (cfg.shram_size != opt_cfg_p->shram_size)
584 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200585 LOG_ERR("NPU config mismatch: npu.shram_size=%" PRIu32 " optimizer.shram_size=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200586 cfg.shram_size,
587 opt_cfg_p->shram_size);
588 }
589 if (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version)
590 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200591 LOG_ERR("NPU config mismatch: npu.cmd_stream_version=%" PRIu32 " optimizer.cmd_stream_version=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200592 cfg.cmd_stream_version,
593 opt_cfg_p->cmd_stream_version);
594 }
595 return_code = -1;
596 }
597
Douglas Troha91e0be52021-01-18 13:57:38 +0100598 if ((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 +0200599 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200600 LOG_ERR("NPU arch mismatch: npu.arch=%" PRIu32 ".%" PRIu32 ".%" PRIu32 " optimizer.arch=%d.%d.%d\n",
Bhavik Patel790ef362020-06-03 10:05:28 +0200601 id.arch_major_rev,
602 id.arch_minor_rev,
603 id.arch_patch_rev,
604 opt_cfg_p->arch_major_rev,
605 opt_cfg_p->arch_minor_rev,
606 opt_cfg_p->arch_patch_rev);
607 return_code = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200608 }
609
610#if !defined(LOG_ENABLED)
611 UNUSED(opt_cfg_p);
612#endif
613 return return_code;
614}
615
Bhavik Pateldae5be02020-06-18 15:25:15 +0200616static void npu_axi_init(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200617{
Bhavik Pateldae5be02020-06-18 15:25:15 +0200618 ethosu_set_qconfig(&drv->dev, NPU_QCONFIG);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200619
Bhavik Pateldae5be02020-06-18 15:25:15 +0200620 ethosu_set_regioncfg(&drv->dev, 0, NPU_REGIONCFG_0);
621 ethosu_set_regioncfg(&drv->dev, 1, NPU_REGIONCFG_1);
622 ethosu_set_regioncfg(&drv->dev, 2, NPU_REGIONCFG_2);
623 ethosu_set_regioncfg(&drv->dev, 3, NPU_REGIONCFG_3);
624 ethosu_set_regioncfg(&drv->dev, 4, NPU_REGIONCFG_4);
625 ethosu_set_regioncfg(&drv->dev, 5, NPU_REGIONCFG_5);
626 ethosu_set_regioncfg(&drv->dev, 6, NPU_REGIONCFG_6);
627 ethosu_set_regioncfg(&drv->dev, 7, NPU_REGIONCFG_7);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200628
Bhavik Pateldae5be02020-06-18 15:25:15 +0200629 (void)ethosu_set_axi_limit0(&drv->dev,
630 AXI_LIMIT0_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200631 AXI_LIMIT0_MEM_TYPE,
632 AXI_LIMIT0_MAX_OUTSTANDING_READS,
633 AXI_LIMIT0_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200634 (void)ethosu_set_axi_limit1(&drv->dev,
635 AXI_LIMIT1_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200636 AXI_LIMIT1_MEM_TYPE,
637 AXI_LIMIT1_MAX_OUTSTANDING_READS,
638 AXI_LIMIT1_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200639 (void)ethosu_set_axi_limit2(&drv->dev,
640 AXI_LIMIT2_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200641 AXI_LIMIT2_MEM_TYPE,
642 AXI_LIMIT2_MAX_OUTSTANDING_READS,
643 AXI_LIMIT2_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200644 (void)ethosu_set_axi_limit3(&drv->dev,
645 AXI_LIMIT3_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200646 AXI_LIMIT3_MEM_TYPE,
647 AXI_LIMIT3_MAX_OUTSTANDING_READS,
648 AXI_LIMIT3_MAX_OUTSTANDING_WRITES);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200649}
650
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200651/* Default implementation to flush the data cache. Override if available on the targeted device.
652 * Passing NULL as p argument expects the whole cache to be flushed.
653 */
654void __attribute__((weak)) ethosu_flush_dcache(uint32_t *p, size_t bytes)
655{
656 (void)p;
657 (void)bytes;
658}
659
660/* Default implementation to invalidate the data cache. Override if available on the targeted device.
661 * Passing NULL as p argument expects the whole cache to be flushed.
662 */
663void __attribute__((weak)) ethosu_invalidate_dcache(uint32_t *p, size_t bytes)
664{
665 (void)p;
666 (void)bytes;
667}
668
Bhavik Pateldae5be02020-06-18 15:25:15 +0200669static int handle_command_stream(struct ethosu_driver *drv,
670 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200671 const int cms_length,
672 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200673 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200674 const int num_base_addr)
675{
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100676 uint32_t qread = 0;
677 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
678 ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream;
679
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200680 LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d\n", cmd_stream, cms_length);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200681
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200682 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200683 {
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200684 LOG_ERR("Error: Command stream addr %p not aligned to 16 bytes\n", cmd_stream);
685 return -1;
686 }
687
688 bool base_addr_invalid = false;
689 for (int i = 0; i < num_base_addr; i++)
690 {
691 if (0 != (base_addr[i] & MASK_16_BYTE_ALIGN))
692 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200693 LOG_ERR("Error: Base addr %d: 0x%llx not aligned to 16 bytes\n", i, base_addr[i]);
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200694 base_addr_invalid = true;
695 }
696 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100697
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200698 if (base_addr_invalid)
699 {
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200700 return -1;
701 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100702
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200703 /* Flush the cache if available on our CPU.
704 * The upcasting to uin32_t* is ok since the pointer never is dereferenced.
705 * The base_addr_size is null if invoking from prior to invoke_V2, in that case
706 * the whole cache is being flushed.
707 */
708
709 if (base_addr_size != NULL)
710 {
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100711 ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200712 for (int i = 0; i < num_base_addr; i++)
713 {
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100714 ethosu_flush_dcache((uint32_t *)(uintptr_t)base_addr[i], base_addr_size[i]);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200715 }
716 }
717 else
718 {
719 ethosu_flush_dcache(NULL, 0);
720 }
721
Bhavik Pateldae5be02020-06-18 15:25:15 +0200722 if (ETHOSU_SUCCESS != ethosu_run_command_stream(&drv->dev, cmd_stream, cms_bytes, base_addr, num_base_addr))
Bhavik Patel790ef362020-06-03 10:05:28 +0200723 {
724 return -1;
725 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200726
Bhavik Pateldae5be02020-06-18 15:25:15 +0200727 wait_for_irq(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200728
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200729 if (drv->status_error)
730 {
731 return -1;
732 }
733
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200734 if (base_addr_size != NULL)
735 {
736 for (int i = 0; i < num_base_addr; i++)
737 {
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100738 ethosu_invalidate_dcache((uint32_t *)(uintptr_t)base_addr[i], base_addr_size[i]);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200739 }
740 }
741 else
742 {
743 ethosu_invalidate_dcache(NULL, 0);
744 }
745
Bhavik Pateldae5be02020-06-18 15:25:15 +0200746 (void)ethosu_get_qread(&drv->dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200747 if (qread != cms_bytes)
748 {
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200749 LOG_WARN(
Per Åstrand14ccfee2020-09-25 10:40:20 +0200750 "Failure: IRQ received but qread (%" PRIu32 ") not at end of stream (%" PRIu32 ").\n", qread, cms_bytes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200751 return -1;
752 }
753
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200754 return 0;
755}
756
Bhavik Pateldae5be02020-06-18 15:25:15 +0200757static int read_apb_reg(struct ethosu_driver *drv, uint16_t da_data)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200758{
759 uint32_t *reg_p;
760 uint32_t start_address = (uint32_t)(da_data & APB_START_ADDR_MASK);
761 uint16_t num_reg = (da_data >> APB_NUM_REG_BIT_SHIFT) + 1;
762
763 reg_p = (uint32_t *)malloc(num_reg * sizeof(uint32_t));
764 if (reg_p == NULL)
765 {
766 LOG_INFO("read_apb_reg, Error! memory not allocated.");
767 return -1;
768 }
769
Bhavik Pateldae5be02020-06-18 15:25:15 +0200770 if (ETHOSU_SUCCESS == ethosu_read_apb_reg(&drv->dev, start_address, num_reg, reg_p))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200771 {
772 for (int i = 0; i < num_reg; i++)
773 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200774 LOG_INFO(
775 "NPU_REG ADDR 0x%04" PRIu32 " = 0x%08" PRIu32 "\n", (start_address + (i * BYTES_IN_32_BITS)), reg_p[i]);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200776 }
777 }
778 else
779 {
780 free(reg_p);
781 return -1;
782 }
783
784 free(reg_p);
785 return 0;
786}
787
Bhavik Pateldae5be02020-06-18 15:25:15 +0200788static int dump_shram(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200789{
790 struct ethosu_config cfg;
791 uint32_t *shram_p;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200792 (void)ethosu_get_config(&drv->dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200793
Per Åstrand14ccfee2020-09-25 10:40:20 +0200794 LOG_INFO("dump_shram size = %" PRIu32 " KB\n", cfg.shram_size);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200795
796 shram_p = (uint32_t *)malloc(BYTES_1KB);
797 if (shram_p == NULL)
798 {
799 LOG_ERR("read_shram, Error! memory not allocated.");
800 return -1;
801 }
802
803 for (uint32_t i = 0; i < cfg.shram_size; i++)
804 {
Bhavik Pateldae5be02020-06-18 15:25:15 +0200805 ethosu_get_shram_data(&drv->dev, i, (uint32_t *)shram_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200806 // Output 1KB of SHRAM
Per Åstrand14ccfee2020-09-25 10:40:20 +0200807 LOG_INFO("***SHRAM SECTION %" PRIu32 "***\n", i);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200808 for (int j = 0; j < (BYTES_1KB / BYTES_IN_32_BITS); j++)
809 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200810 LOG_INFO("[0x%04" PRIx32 "] %" PRIx32 "\n", (i * 1024 + j * 4), shram_p[j]);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200811 }
812 }
813 free(shram_p);
814
815 return 0;
816}
817
818typedef struct
819{
820 int number;
821 const char *name;
822} name_lookup_t;
823
824static const name_lookup_t npu_reg_name_tbl[] = {
825 {0x200, "KERNEL_X"},
826 {0x204, "KERNEL_Y"},
827 {0x208, "KERNEL_W_M1"},
828 {0x20C, "KERNEL_H_M1"},
829 {0x210, "OFM_CBLK_WIDTH_M1"},
830 {0x214, "OFM_CBLK_HEIGHT_M1"},
831 {0x218, "OFM_CBLK_DEPTH_M1"},
832 {0x21c, "IFM_CBLK_DEPTH_M1"},
833 {0x220, "OFM_X"},
834 {0x224, "OFM_Y"},
835 {0x228, "OFM_Z"},
836 {0x22C, "IFM_Z"},
837 {0x230, "PAD_TOP"},
838 {0x234, "PAD_LEFT"},
839 {0x238, "IFM_CBLK_WIDTH"},
840 {0x23C, "IFM_CBLK_HEIGHT"},
841 {0x240, "DMA_IFM_SRC"},
842 {0x244, "DMA_IFM_SRC_HI"},
843 {0x248, "DMA_IFM_DST"},
844 {0x24c, "DMA_OFM_SRC"},
845 {0x250, "DMA_OFM_DST"},
846 {0x254, "DMA_OFM_DST_HI"},
847 {0x258, "DMA_WEIGHT_SRC"},
848 {0x25c, "DMA_WEIGHT_SRC_HI"},
849 {0x260, "DMA_CMD_SRC"},
850 {0x264, "DMA_CMD_SRC_HI"},
851 {0x268, "DMA_CMD_SIZE"},
852 {0x26c, "DMA_M2M_SRC"},
853 {0x270, "DMA_M2M_SRC_HI"},
854 {0x274, "DMA_M2M_DST"},
855 {0x278, "DMA_M2M_DST_HI"},
856 {0x27c, "CURRENT_QREAD"},
857 {0x280, "DMA_SCALE_SRC"},
858 {0x284, "DMA_SCALE_SRC_HI"},
859 {0x2BC, "CURRENT_CMD"},
860 {0x800, "IFM_PAD_TOP"},
861 {0x804, "IFM_PAD_LEFT"},
862 {0x808, "IFM_PAD_RIGHT"},
863 {0x80C, "IFM_PAD_BOTTOM"},
864 {0x810, "IFM_DEPTH_M1"},
865 {0x814, "IFM_PRECISION"},
866 {0x81C, "IFM_UPSCALE"},
867 {0x824, "IFM_ZERO_POINT"},
868 {0x828, "IFM_WIDTH0_M1"},
869 {0x82C, "IFM_HEIGHT0_M1"},
870 {0x830, "IFM_HEIGHT1_M1"},
871 {0x834, "IFM_IB_END"},
872 {0x83C, "IFM_REGION"},
873 {0x844, "OFM_WIDTH_M1"},
874 {0x848, "OFM_HEIGHT_M1"},
875 {0x84C, "OFM_DEPTH_M1"},
876 {0x850, "OFM_PRECISION"},
877 {0x854, "OFM_BLK_WIDTH_M1"},
878 {0x858, "OFM_BLK_HEIGHT_M1"},
879 {0x85C, "OFM_BLK_DEPTH_M1"},
880 {0x860, "OFM_ZERO_POINT"},
881 {0x868, "OFM_WIDTH0_M1"},
882 {0x86C, "OFM_HEIGHT0_M1"},
883 {0x870, "OFM_HEIGHT1_M1"},
884 {0x87C, "OFM_REGION"},
885 {0x880, "KERNEL_WIDTH_M1"},
886 {0x884, "KERNEL_HEIGHT_M1"},
887 {0x888, "KERNEL_STRIDE"},
888 {0x88C, "PARALLEL_MODE"},
889 {0x890, "ACC_FORMAT"},
890 {0x894, "ACTIVATION"},
891 {0x898, "ACTIVATION_MIN"},
892 {0x89C, "ACTIVATION_MAX"},
893 {0x8A0, "WEIGHT_REGION"},
894 {0x8A4, "SCALE_REGION"},
895 {0x8B4, "AB_START"},
896 {0x8BC, "BLOCKDEP"},
897 {0x8C0, "DMA0_SRC_REGION"},
898 {0x8C4, "DMA0_DST_REGION"},
899 {0x8C8, "DMA0_SIZE0"},
900 {0x8CC, "DMA0_SIZE1"},
901 {0x900, "IFM2_BROADCAST"},
902 {0x904, "IFM2_SCALAR"},
903 {0x924, "IFM2_ZERO_POINT"},
904 {0x928, "IFM2_WIDTH0_M1"},
905 {0x92C, "IFM2_HEIGHT0_M1"},
906 {0x930, "IFM2_HEIGHT1_M1"},
907 {0x934, "IFM2_IB_START"},
908 {0x93C, "IFM2_REGION"},
909 {0xA00, "IFM_BASE0"},
910 {0xA04, "IFM_BASE0_HI"},
911 {0xA08, "IFM_BASE1"},
912 {0xA0C, "IFM_BASE1_HI"},
913 {0xA10, "IFM_BASE2"},
914 {0xA14, "IFM_BASE2_HI"},
915 {0xA18, "IFM_BASE3"},
916 {0xA1C, "IFM_BASE3_HI"},
917 {0xA20, "IFM_STRIDE_X"},
918 {0xA24, "IFM_STRIDE_X_HI"},
919 {0xA28, "IFM_STRIDE_Y"},
920 {0xA2C, "IFM_STRIDE_Y_HI"},
921 {0xA30, "IFM_STRIDE_C"},
922 {0xA34, "IFM_STRIDE_C_HI"},
923 {0xA40, "OFM_BASE0"},
924 {0xA44, "OFM_BASE0_HI"},
925 {0xA48, "OFM_BASE1"},
926 {0xA4C, "OFM_BASE1_HI"},
927 {0xA50, "OFM_BASE2"},
928 {0xA54, "OFM_BASE2_HI"},
929 {0xA58, "OFM_BASE3"},
930 {0xA5C, "OFM_BASE3_HI"},
931 {0xA60, "OFM_STRIDE_X"},
932 {0xA64, "OFM_STRIDE_X_HI"},
933 {0xA68, "OFM_STRIDE_Y"},
934 {0xA6C, "OFM_STRIDE_Y_HI"},
935 {0xA70, "OFM_STRIDE_C"},
936 {0xA74, "OFM_STRIDE_C_HI"},
937 {0xA80, "WEIGHT_BASE"},
938 {0xA84, "WEIGHT_BASE_HI"},
939 {0xA88, "WEIGHT_LENGTH"},
940 {0xA8C, "WEIGHT_LENGTH_HI"},
941 {0xA90, "SCALE_BASE"},
942 {0xA94, "SCALE_BASE_HI"},
943 {0xA98, "SCALE_LENGTH"},
944 {0xAA0, "OFM_SCALE"},
945 {0xAA4, "OFM_SCALE_SHIFT"},
946 {0xAA8, "OPA_SCALE "},
947 {0xAB0, "OPB_SCALE"},
948 {0xAC0, "DMA0_SRC"},
949 {0xAC4, "DMA0_SRC_HI"},
950 {0xAC8, "DMA0_DST"},
951 {0xACC, "DMA0_DST_HI"},
952 {0xAD0, "DMA0_LEN"},
953 {0xAD4, "DMA0_LEN_HI"},
954 {0xAD8, "DMA0_SKIP0"},
955 {0xADC, "DMA0_SKIP0_HI"},
956 {0xAE0, "DMA0_SKIP1"},
957 {0xAE4, "DMA0_SKIP1_HI"},
958 {0xB00, "IFM2_BASE0"},
959 {0xB04, "IFM2_BASE0_HI"},
960 {0xB08, "IFM2_BASE1"},
961 {0xB0C, "IFM2_BASE1_HI"},
962 {0xB10, "IFM2_BASE2"},
963 {0xB14, "IFM2_BASE2_HI"},
964 {0xB18, "IFM2_BASE3"},
965 {0xB1C, "IFM2_BASE3_HI"},
966 {0xB20, "IFM2_STRIDE_X"},
967 {0xB24, "IFM2_STRIDE_X_HI"},
968 {0xB28, "IFM2_STRIDE_Y"},
969 {0xB2C, "IFM2_STRIDE_Y_HI"},
970 {0xB30, "IFM2_STRIDE_C"},
971 {0xB34, "IFM2_STRIDE_C_HI"},
972 {0xB40, "WEIGHT1_BASE"},
973 {0xB44, "WEIGHT1_BASE_HI"},
974 {0xB48, "WEIGHT1_LENGTH"},
975 {0xB4C, "WEIGHT1_LENGTH_HI"},
976 {0xB50, "SCALE1_BASE"},
977 {0xB54, "SCALE1_BASE_HI"},
978 {0xB58, "SCALE1_LENGTH"},
979};
980
981static const char *lookup_name(const name_lookup_t *lookup_table, int lookup_table_count, int find)
982{
983 int n;
984 for (n = 0; n < lookup_table_count; n++)
985 {
986 if (lookup_table[n].number == find)
987 {
988 return lookup_table[n].name;
989 }
990 }
991 // Not found
992 return 0;
993}
994
Bhavik Pateldae5be02020-06-18 15:25:15 +0200995static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200996{
997 unsigned int reg_val;
998 const char *reg_name;
999 int npu_reg_name_tbl_count = sizeof(npu_reg_name_tbl) / sizeof(npu_reg_name_tbl[0]);
1000
1001 LOG_INFO("dump_register %X - %X\n", npu_reg, npu_reg_end);
1002 for (; npu_reg <= npu_reg_end; npu_reg += sizeof(int))
1003 {
Bhavik Pateldae5be02020-06-18 15:25:15 +02001004 reg_val = ethosu_read_reg(&drv->dev, npu_reg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001005 reg_name = lookup_name(npu_reg_name_tbl, npu_reg_name_tbl_count, npu_reg);
1006 LOG_INFO("[0x%.4X] 0x%.8X\t%s\n", npu_reg, reg_val, (reg_name) ? reg_name : "");
1007 }
1008}
1009
1010static const name_lookup_t cmd0_name_tbl[] = {
1011 {0x000, "NPU_OP_STOP"},
1012 {0x001, "NPU_OP_IRQ"},
1013 {0x002, "NPU_OP_CONV"},
1014 {0x003, "NPU_OP_DEPTHWISE"},
1015 {0x004, "NPU_OP_VECTOR_PROD"},
1016 {0x005, "NPU_OP_POOL"},
1017 {0x006, "NPU_OP_ELEMENTWISE"},
1018 {0x010, "NPU_OP_DMA_START"},
1019 {0x011, "NPU_OP_DMA_WAIT"},
1020 {0x012, "NPU_OP_KERNEL_WAIT"},
1021 {0x100, "NPU_SET_IFM_PAD_TOP"},
1022 {0x101, "NPU_SET_IFM_PAD_LEFT"},
1023 {0x102, "NPU_SET_IFM_PAD_RIGHT"},
1024 {0x103, "NPU_SET_IFM_PAD_BOTTOM"},
1025 {0x104, "NPU_SET_IFM_DEPTH_M1"},
1026 {0x105, "NPU_SET_IFM_PRECISION"},
1027 {0x107, "NPU_SET_IFM_UPSCALE"},
1028 {0x109, "NPU_SET_IFM_ZERO_POINT"},
1029 {0x10A, "NPU_SET_IFM_WIDTH0_M1"},
1030 {0x10B, "NPU_SET_IFM_HEIGHT0_M1"},
1031 {0x10C, "NPU_SET_IFM_HEIGHT1_M1"},
1032 {0x10D, "NPU_SET_IFM_IB_END"},
1033 {0x10F, "NPU_SET_IFM_REGION"},
1034 {0x110, "NPU_SET_OFM_BATCH_SIZE_M1"},
1035 {0x111, "NPU_SET_OFM_WIDTH_M1"},
1036 {0x112, "NPU_SET_OFM_HEIGHT_M1"},
1037 {0x113, "NPU_SET_OFM_DEPTH_M1"},
1038 {0x114, "NPU_SET_OFM_PRECISION"},
1039 {0x115, "NPU_SET_OFM_BLK_WIDTH_M1"},
1040 {0x116, "NPU_SET_OFM_BLK_HEIGHT_M1"},
1041 {0x117, "NPU_SET_OFM_BLK_DEPTH_M1"},
1042 {0x118, "NPU_SET_OFM_ZERO_POINT"},
1043 {0x11A, "NPU_SET_OFM_WIDTH0_M1"},
1044 {0x11B, "NPU_SET_OFM_HEIGHT0_M1"},
1045 {0x11C, "NPU_SET_OFM_HEIGHT1_M1"},
1046 {0x11F, "NPU_SET_OFM_REGION"},
1047 {0x120, "NPU_SET_KERNEL_WIDTH_M1"},
1048 {0x121, "NPU_SET_KERNEL_HEIGHT_M1"},
1049 {0x122, "NPU_SET_KERNEL_STRIDE"},
1050 {0x124, "NPU_SET_ACC_FORMAT"},
1051 {0x125, "NPU_SET_ACTIVATION"},
1052 {0x126, "NPU_SET_ACTIVATION_MIN"},
1053 {0x127, "NPU_SET_ACTIVATION_MAX"},
1054 {0x128, "NPU_SET_WEIGHT_REGION"},
1055 {0x129, "NPU_SET_SCALE_REGION"},
1056 {0x12D, "NPU_SET_AB_START"},
1057 {0x12F, "NPU_SET_BLOCKDEP"},
1058 {0x130, "NPU_SET_DMA0_SRC_REGION"},
1059 {0x131, "NPU_SET_DMA0_DST_REGION"},
1060 {0x180, "NPU_SET_IFM2_BROADCAST"},
1061 {0x181, "NPU_SET_IFM2_SCALAR"},
1062 {0x185, "NPU_SET_IFM2_PRECISION"},
1063 {0x189, "NPU_SET_IFM2_ZERO_POINT"},
1064 {0x18A, "NPU_SET_IFM2_WIDTH0_M1"},
1065 {0x18B, "NPU_SET_IFM2_HEIGHT0_M1"},
1066 {0x18C, "NPU_SET_IFM2_HEIGHT1_M1"},
1067 {0x18D, "NPU_SET_IFM2_IB_START"},
1068 {0x18F, "NPU_SET_IFM2_REGION"},
1069};
1070
1071static const name_lookup_t cmd1_name_tbl[] = {
1072 {0x000, "NPU_SET_IFM_BASE0"}, {0x001, "NPU_SET_IFM_BASE1"}, {0x002, "NPU_SET_IFM_BASE2"},
1073 {0x003, "NPU_SET_IFM_BASE3"}, {0x004, "NPU_SET_IFM_STRIDE_X"}, {0x005, "NPU_SET_IFM_STRIDE_Y"},
1074 {0x006, "NPU_SET_IFM_STRIDE_C"}, {0x007, "NPU_SET_IFM_STRIDE_N"}, {0x010, "NPU_SET_OFM_BASE0"},
1075 {0x011, "NPU_SET_OFM_BASE1"}, {0x012, "NPU_SET_OFM_BASE2"}, {0x013, "NPU_SET_OFM_BASE3"},
1076 {0x014, "NPU_SET_OFM_STRIDE_X"}, {0x015, "NPU_SET_OFM_STRIDE_Y"}, {0x016, "NPU_SET_OFM_STRIDE_C"},
1077 {0x017, "NPU_SET_OFM_STRIDE_N"}, {0x020, "NPU_SET_WEIGHT_BASE"}, {0x021, "NPU_SET_WEIGHT_LENGTH"},
1078 {0x022, "NPU_SET_SCALE_BASE"}, {0x023, "NPU_SET_SCALE_LENGTH"}, {0x024, "NPU_SET_OFM_SCALE"},
1079 {0x025, "NPU_SET_OPA_SCALE"}, {0x026, "NPU_SET_OPB_SCALE"}, {0x030, "NPU_SET_DMA0_SRC"},
1080 {0x031, "NPU_SET_DMA0_DST"}, {0x032, "NPU_SET_DMA0_LEN"}, {0x080, "NPU_SET_IFM2_BASE0"},
1081 {0x081, "NPU_SET_IFM2_BASE1"}, {0x082, "NPU_SET_IFM2_BASE2"}, {0x083, "NPU_SET_IFM2_BASE3"},
1082 {0x084, "NPU_SET_IFM2_STRIDE_X"}, {0x085, "NPU_SET_IFM2_STRIDE_Y"}, {0x086, "NPU_SET_IFM2_STRIDE_C"},
1083};
1084
1085static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread)
1086{
1087 int n;
1088 int offset;
1089 uint32_t cmd_val;
1090 const uint8_t *cmd_ptr;
1091 const char *cmd_name;
1092 int cmd0_name_tbl_count = sizeof(cmd0_name_tbl) / sizeof(cmd0_name_tbl[0]);
1093 int cmd1_name_tbl_count = sizeof(cmd1_name_tbl) / sizeof(cmd1_name_tbl[0]);
1094
1095 LOG_INFO("dump_command_stream cmd_stream = 0x%8p cms_length = %d\n", cmd_stream, cms_length);
1096 for (n = 0; n < cms_length; n++)
1097 {
1098 // Offset
1099 offset = n * sizeof(int);
1100 LOG_INFO("[%.4d] ", offset);
1101 // Command
1102 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1103 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1104 // Command name and payload
1105 if (cmd_stream[n] & 0x4000)
1106 {
1107 cmd_name = lookup_name(cmd1_name_tbl, cmd1_name_tbl_count, cmd_stream[n] & 0x3FF);
1108 n++;
1109 cmd_val = cmd_stream[n];
1110 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1111 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1112 }
1113 else
1114 {
1115 cmd_val = cmd_stream[n] >> 16;
1116 cmd_name = lookup_name(cmd0_name_tbl, cmd0_name_tbl_count, cmd_stream[n] & 0x3FF);
1117 }
1118 if (cmd_name)
1119 {
Per Åstrand14ccfee2020-09-25 10:40:20 +02001120 LOG_INFO("\t%s 0x%.8" PRIX32, cmd_name, cmd_val);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001121 }
1122 if (offset == qread)
1123 {
1124 LOG_INFO(" <<== QREAD\n");
1125 }
1126 else
1127 {
1128 LOG_INFO("\n");
1129 }
1130 }
1131}