blob: cb1790e8a82963e184d4cbee39130fa4b9b5b515 [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
Anton Mobergdf386e02021-02-02 11:26:48 +0100164/* Default implementation to initialise ethosu driver mutex. Override if available on the targeted RTOS.
165 * If not overridden, will do nothing (assumes baremetal).
166 */
167void __attribute__((weak)) ethosu_mutex_init() {}
168
169/* Default implementation to initialise ethosu driver binary semaphore. Override if available on the targeted RTOS.
170 * If not overridden, will do nothing (assumes baremetal).
171 */
172void __attribute__((weak)) ethosu_semaphore_init() {}
173
174/* Default implementation to lock ethosu driver mutex. Override if available on the targeted RTOS.
175 * If not overridden, will do nothing (assumes baremetal).
176 */
177void __attribute__((weak)) ethosu_mutex_lock() {}
178
179/* Default implementation to unlock ethosu driver mutex. Override if available on the targeted RTOS.
180 * If not overridden, will do nothing (assumes baremetal).
181 */
182void __attribute__((weak)) ethosu_mutex_unlock() {}
183
184/* Default implementation to wait for and take free ethosu driver semaphore. Override if available on the targeted RTOS.
185 * If not overridden, will do nothing (assumes baremetal).
186 */
187void __attribute__((weak)) ethosu_semaphore_take() {}
188
189/* Default implementation to give ethosu driver semaphore. Override if available on the targeted RTOS.
190 * If not overridden, will do nothing (assumes baremetal).
191 */
192void __attribute__((weak)) ethosu_semaphore_give() {}
193
194/* Default implementation to force context-switch while waiting for Ethos-U IRQ. Override if available on the targeted
195 * RTOS. If not overridden, will do nothing (assumes baremetal).
196 */
197void __attribute__((weak)) ethosu_yield() {}
198
199/* Default implementation to indicate thread/task is resuming. Override if available on the targeted RTOS.
200 * If not overridden, will do nothing (assumes baremetal).
201 */
202void __attribute__((weak)) ethosu_resume() {}
203
Anton Moberg61da4d32020-12-22 16:00:31 +0100204void ethosu_irq_handler_v2(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200205{
206 uint8_t irq_raised = 0;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200207
208 LOG_DEBUG("Interrupt. status=0x%08x, qread=%d\n",
Anton Moberg61da4d32020-12-22 16:00:31 +0100209 ethosu_read_reg(&drv->dev, NPU_REG_STATUS),
210 ethosu_read_reg(&drv->dev, NPU_REG_QREAD));
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200211
212 // Verify that interrupt has been raised
Anton Moberg61da4d32020-12-22 16:00:31 +0100213 (void)ethosu_is_irq_raised(&drv->dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200214 ASSERT(irq_raised == 1);
215 irq_triggered = true;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200216
217 // Clear interrupt
Anton Moberg61da4d32020-12-22 16:00:31 +0100218 (void)ethosu_clear_irq_status(&drv->dev);
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200219
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200220 // Verify that interrupt has been successfully cleared
Anton Moberg61da4d32020-12-22 16:00:31 +0100221 (void)ethosu_is_irq_raised(&drv->dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200222 ASSERT(irq_raised == 0);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200223
Anton Moberg61da4d32020-12-22 16:00:31 +0100224 if (ethosu_status_has_error(&drv->dev))
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200225 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100226 ethosu_soft_reset_and_restore(drv);
227 drv->status_error = true;
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200228 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200229}
230
Bhavik Pateldae5be02020-06-18 15:25:15 +0200231static inline void wait_for_irq(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200232{
233 while (1)
234 {
235 __disable_irq();
Bhavik Pateldae5be02020-06-18 15:25:15 +0200236 if (irq_triggered || drv->abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200237 {
238 __enable_irq();
239 break;
240 }
241
Per Åstrand25d78c02020-04-21 14:19:44 +0200242 __WFI();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200243
Anton Mobergdf386e02021-02-02 11:26:48 +0100244 ethosu_yield();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200245 __enable_irq();
Anton Mobergdf386e02021-02-02 11:26:48 +0100246 ethosu_resume();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200247 }
248}
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200249
Bhavik Pateldae5be02020-06-18 15:25:15 +0200250static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p);
251static int handle_command_stream(struct ethosu_driver *drv,
252 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200253 const int cms_length,
254 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200255 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200256 const int num_base_addr);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200257static int read_apb_reg(struct ethosu_driver *drv, uint16_t);
258static int dump_shram(struct ethosu_driver *drv);
259static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200260static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200261static void npu_axi_init(struct ethosu_driver *drv);
Anton Mobergdf386e02021-02-02 11:26:48 +0100262static struct ethosu_driver *ethosu_find_and_reserve_driver(void);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200263
Anton Moberg61da4d32020-12-22 16:00:31 +0100264int ethosu_init_v4(struct ethosu_driver *drv,
265 const void *base_address,
Per Åstrande6498f02020-11-09 15:33:12 +0100266 const void *fast_memory,
267 const size_t fast_memory_size,
268 uint32_t secure_enable,
269 uint32_t privilege_enable)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200270{
271 int return_code = 0;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200272
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100273 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 +0200274 __FUNCTION__,
275 base_address,
276 fast_memory,
Per Åstrande6498f02020-11-09 15:33:12 +0100277 fast_memory_size,
278 secure_enable,
279 privilege_enable);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200280
Anton Mobergdf386e02021-02-02 11:26:48 +0100281 ethosu_mutex_init();
282 ethosu_semaphore_init();
Anton Moberg61da4d32020-12-22 16:00:31 +0100283 ethosu_register_driver(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200284
Anton Moberg61da4d32020-12-22 16:00:31 +0100285 drv->fast_memory = (uint32_t)fast_memory;
286 drv->fast_memory_size = fast_memory_size;
287
288 if (ETHOSU_SUCCESS != ethosu_dev_init(&drv->dev, base_address, secure_enable, privilege_enable))
Bhavik Pateldae5be02020-06-18 15:25:15 +0200289 {
290 LOG_ERR("Failed in ethosu_dev_init");
291 return -1;
292 }
293
Anton Moberg61da4d32020-12-22 16:00:31 +0100294 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 +0200295 {
296 LOG_ERR("Failed to disable clock-q & power-q for Ethos-U\n");
297 return -1;
298 }
299
Anton Moberg61da4d32020-12-22 16:00:31 +0100300 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
Per Åstrand849cf692020-11-24 07:39:55 +0100301 {
302 return -1;
303 }
Kristofer Jonssondaa0d202020-05-12 12:23:16 +0200304
Anton Moberg61da4d32020-12-22 16:00:31 +0100305 if (ETHOSU_SUCCESS != ethosu_wait_for_reset(&drv->dev))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200306 {
307 LOG_ERR("Failed reset of Ethos-U\n");
308 return -1;
309 }
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100310
Anton Moberg61da4d32020-12-22 16:00:31 +0100311 drv->status_error = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200312
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200313 return return_code;
314}
315
Anton Moberg61da4d32020-12-22 16:00:31 +0100316int ethosu_get_version_v2(struct ethosu_driver *drv, struct ethosu_version *version)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200317{
318 int return_code = 0;
319
320 if (NULL != version)
321 {
322 struct ethosu_id id;
323 struct ethosu_config cfg;
Anton Moberg61da4d32020-12-22 16:00:31 +0100324 (void)ethosu_get_id(&drv->dev, &id);
325 (void)ethosu_get_config(&drv->dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200326
327 version->id.version_status = id.version_status;
328 version->id.version_minor = id.version_minor;
329 version->id.version_major = id.version_major;
330 version->id.product_major = id.product_major;
331 version->id.arch_patch_rev = id.arch_patch_rev;
332 version->id.arch_minor_rev = id.arch_minor_rev;
333 version->id.arch_major_rev = id.arch_major_rev;
334 version->id.driver_patch_rev = ETHOSU_DRIVER_VERSION_PATCH;
335 version->id.driver_minor_rev = ETHOSU_DRIVER_VERSION_MINOR;
336 version->id.driver_major_rev = ETHOSU_DRIVER_VERSION_MAJOR;
337 version->cfg.macs_per_cc = cfg.macs_per_cc;
338 version->cfg.cmd_stream_version = cfg.cmd_stream_version;
339 version->cfg.shram_size = cfg.shram_size;
340 }
341 else
342 {
343 return_code = -1;
344 }
345
346 return return_code;
347}
348
Anton Moberg61da4d32020-12-22 16:00:31 +0100349int ethosu_invoke_v3(struct ethosu_driver *drv,
350 const void *custom_data_ptr,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200351 const int custom_data_size,
352 const uint64_t *base_addr,
353 const size_t *base_addr_size,
354 const int num_base_addr)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200355{
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200356 const struct custom_data_s *data_ptr = custom_data_ptr;
357 const struct custom_data_s *data_end = custom_data_ptr + custom_data_size;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200358 int return_code = 0;
359
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200360 LOG_INFO("%s\n", __FUNCTION__);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200361
362 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200363 if (data_ptr->word != ETHOSU_FOURCC)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200364 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200365 LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x\n", data_ptr->word, ETHOSU_FOURCC);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200366 return -1;
367 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200368
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200369 // Custom data length must be a multiple of 32 bits
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200370 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
371 {
372 LOG_ERR("ethosu_invoke ERROR custom_data_size=0x%x not a multiple of 4\n", custom_data_size);
373 return -1;
374 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200375
376 ++data_ptr;
377
378 // Adjust base address to fast memory area
Anton Moberg61da4d32020-12-22 16:00:31 +0100379 if (drv->fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200380 {
381 uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX];
382
Anton Moberg61da4d32020-12-22 16:00:31 +0100383 if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > drv->fast_memory_size)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200384 {
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100385 LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u\n",
Anton Moberg61da4d32020-12-22 16:00:31 +0100386 drv->fast_memory_size,
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100387 base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]);
388 return -1;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200389 }
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100390
Anton Moberg61da4d32020-12-22 16:00:31 +0100391 *fast_memory = drv->fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200392 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200393
Anton Moberg61da4d32020-12-22 16:00:31 +0100394 if (!drv->dev_power_always_on)
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200395 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100396 // Only soft reset if securty state or privilege level needs changing
397 if (drv->dev.proto != ethosu_read_reg(&drv->dev, NPU_REG_PROT))
Per Åstrand849cf692020-11-24 07:39:55 +0100398 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100399 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100400 {
401 return -1;
402 }
Per Åstrand849cf692020-11-24 07:39:55 +0100403 }
Anton Moberg61da4d32020-12-22 16:00:31 +0100404
405 drv->status_error = false;
406 ethosu_set_clock_and_power(&drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
407 ethosu_restore_pmu_config(&drv->dev);
408 npu_axi_init(drv);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200409 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100410
Anton Moberg61da4d32020-12-22 16:00:31 +0100411 drv->status_error = false;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200412
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200413 while (data_ptr < data_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200414 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200415 int ret = 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200416 switch (data_ptr->driver_action_command)
417 {
418 case OPTIMIZER_CONFIG:
419 LOG_INFO("ethosu_invoke OPTIMIZER_CONFIG\n");
420 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
421
Anton Moberg61da4d32020-12-22 16:00:31 +0100422 ret = handle_optimizer_config(drv, opt_cfg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200423 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
424 break;
425 case COMMAND_STREAM:
426 LOG_INFO("ethosu_invoke COMMAND_STREAM\n");
427 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct custom_data_s);
428 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
429
Anton Moberg61da4d32020-12-22 16:00:31 +0100430 drv->abort_inference = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200431 // It is safe to clear this flag without atomic, because npu is not running.
432 irq_triggered = false;
433
Anton Moberg61da4d32020-12-22 16:00:31 +0100434 ret = handle_command_stream(drv, command_stream, cms_length, base_addr, base_addr_size, num_base_addr);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200435
Anton Moberg61da4d32020-12-22 16:00:31 +0100436 if (return_code == -1 && drv->abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200437 {
438 uint32_t qread = 0;
Anton Moberg61da4d32020-12-22 16:00:31 +0100439 ethosu_get_qread(&drv->dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200440 LOG_ERR("NPU timeout\n");
441 dump_command_stream(command_stream, cms_length, qread);
Anton Moberg61da4d32020-12-22 16:00:31 +0100442 dump_npu_register(drv, 0x200, 0x2BF);
443 dump_npu_register(drv, 0x800, 0xB3F);
444 dump_shram(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200445 }
446
447 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
448 break;
449 case READ_APB_REG:
450 LOG_INFO("ethosu_invoke READ_APB_REG\n");
Anton Moberg61da4d32020-12-22 16:00:31 +0100451 ret = read_apb_reg(drv, data_ptr->driver_action_data);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200452 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
453 break;
454 case DUMP_SHRAM:
455 LOG_INFO("ethosu_invoke DUMP_SHRAM\n");
Anton Moberg61da4d32020-12-22 16:00:31 +0100456 ret = dump_shram(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200457 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
458 break;
459 case NOP:
460 LOG_INFO("ethosu_invoke NOP\n");
461 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
462 break;
463 default:
464 LOG_ERR("ethosu_invoke UNSUPPORTED driver_action_command %d \n", data_ptr->driver_action_command);
Bhavik Patele645fed2020-06-12 14:46:47 +0200465 ret = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200466 break;
467 }
Bhavik Patele645fed2020-06-12 14:46:47 +0200468 if (ret != 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200469 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200470 return_code = -1;
471 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200472 }
473 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200474
Anton Moberg61da4d32020-12-22 16:00:31 +0100475 if (!drv->status_error && !drv->dev_power_always_on)
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200476 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100477 ethosu_save_pmu_counters(&drv->dev);
478 ethosu_set_clock_and_power(&drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200479 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200480
Bhavik Patele645fed2020-06-12 14:46:47 +0200481 return return_code;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200482}
483
Anton Moberg61da4d32020-12-22 16:00:31 +0100484void ethosu_abort_v2(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200485{
Anton Moberg61da4d32020-12-22 16:00:31 +0100486 drv->abort_inference = true;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200487}
488
Anton Moberg61da4d32020-12-22 16:00:31 +0100489void ethosu_set_power_mode_v2(struct ethosu_driver *drv, bool always_on)
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100490{
Anton Moberg61da4d32020-12-22 16:00:31 +0100491 drv->dev_power_always_on = always_on;
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100492
493 if (always_on)
494 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100495 npu_axi_init(drv);
496 }
497}
498
499int ethosu_register_driver(struct ethosu_driver *drv)
500{
501 // Safeguard check for if driver is already registered
502 struct ethosu_driver *cur = registered_drivers;
503 while (cur != NULL)
504 {
505 if (cur == drv)
506 {
507 LOG_ERR("%s: NPU driver at address %p is already registered.\n", __FUNCTION__, drv);
508 return -1;
509 }
510 cur = cur->next;
511 }
512
513 drv->next = registered_drivers;
514 // Designate new registered driver HEAD
515 registered_drivers = drv;
516
517 LOG_INFO("%s: New NPU driver at address %p is registered.\n", __FUNCTION__, drv);
Anton Moberg61da4d32020-12-22 16:00:31 +0100518 return 0;
519}
520
521int ethosu_deregister_driver(struct ethosu_driver *drv)
522{
523 struct ethosu_driver *cur = registered_drivers;
524 struct ethosu_driver **prev = &registered_drivers;
525
526 while (cur != NULL)
527 {
528 if (cur == drv)
529 {
530 *prev = cur->next;
531 LOG_INFO("%s: NPU driver at address %p is deregistered.\n", __FUNCTION__, drv);
532 return 0;
533 }
534
535 prev = &cur->next;
536 cur = cur->next;
537 }
538
539 LOG_ERR("%s: NPU driver at address %p does not match a registered driver and therefore may not be deregistered.\n",
540 __FUNCTION__,
541 drv);
Anton Mobergdf386e02021-02-02 11:26:48 +0100542
Anton Moberg61da4d32020-12-22 16:00:31 +0100543 return -1;
544}
545
546struct ethosu_driver *ethosu_reserve_driver(void)
547{
Anton Mobergdf386e02021-02-02 11:26:48 +0100548 struct ethosu_driver *drv = NULL;
549
550 do
551 {
552 ethosu_mutex_lock();
553 drv = ethosu_find_and_reserve_driver();
554 ethosu_mutex_unlock();
555
556 if (drv != NULL)
557 {
558 break;
559 }
560
561 ethosu_semaphore_take();
562
563 } while (1);
564
565 return drv;
566}
567
568static struct ethosu_driver *ethosu_find_and_reserve_driver(void)
569{
Anton Moberg61da4d32020-12-22 16:00:31 +0100570 struct ethosu_driver *drv = registered_drivers;
571
572 while (drv != NULL)
573 {
574 if (!drv->reserved)
575 {
576 drv->reserved = true;
577 LOG_INFO("%s - Driver %p reserved.\n", __FUNCTION__, drv);
578 return drv;
579 }
580 drv = drv->next;
581 }
582
583 LOG_INFO("%s: No available drivers.\n", __FUNCTION__, drv);
584
585 return NULL;
586}
587
588void ethosu_release_driver(struct ethosu_driver *drv)
589{
Anton Mobergdf386e02021-02-02 11:26:48 +0100590 ethosu_mutex_lock();
Anton Moberg61da4d32020-12-22 16:00:31 +0100591 if (drv != NULL && drv->reserved)
592 {
593 drv->reserved = false;
594 LOG_INFO("%s - Driver %p released\n", __FUNCTION__, drv);
Anton Mobergdf386e02021-02-02 11:26:48 +0100595 ethosu_semaphore_give();
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100596 }
Anton Mobergdf386e02021-02-02 11:26:48 +0100597 ethosu_mutex_unlock();
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100598}
599
600static int ethosu_soft_reset_and_restore(struct ethosu_driver *drv)
601{
602
603 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
604 {
605 return -1;
606 }
607
608 ethosu_set_clock_and_power(&drv->dev, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
609
610 npu_axi_init(drv);
611 ethosu_restore_pmu_config(&drv->dev);
612
613 return 0;
614}
615
Bhavik Pateldae5be02020-06-18 15:25:15 +0200616static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200617{
618 struct ethosu_config cfg;
619 struct ethosu_id id;
620 int return_code = 0;
621
622 LOG_INFO("handle_optimizer_config:\n");
623 LOG_INFO("Optimizer release nbr: %d patch: %d\n", opt_cfg_p->da_data.rel_nbr, opt_cfg_p->da_data.patch_nbr);
624 LOG_INFO("Optimizer config cmd_stream_version: %d macs_per_cc: %d shram_size: %d\n",
625 opt_cfg_p->cmd_stream_version,
626 opt_cfg_p->macs_per_cc,
627 opt_cfg_p->shram_size);
628 LOG_INFO("Optimizer config Ethos-U version: %d.%d.%d\n",
629 opt_cfg_p->arch_major_rev,
630 opt_cfg_p->arch_minor_rev,
631 opt_cfg_p->arch_patch_rev);
632
Bhavik Pateldae5be02020-06-18 15:25:15 +0200633 (void)ethosu_get_config(&drv->dev, &cfg);
634 (void)ethosu_get_id(&drv->dev, &id);
Per Åstrand14ccfee2020-09-25 10:40:20 +0200635 LOG_INFO("Ethos-U config cmd_stream_version: %" PRIu32 " macs_per_cc: %" PRIu32 " shram_size: %" PRIu32 "\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200636 cfg.cmd_stream_version,
637 cfg.macs_per_cc,
638 cfg.shram_size);
Per Åstrand14ccfee2020-09-25 10:40:20 +0200639 LOG_INFO("Ethos-U version: %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
640 id.arch_major_rev,
641 id.arch_minor_rev,
642 id.arch_patch_rev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200643
644 if ((cfg.macs_per_cc != opt_cfg_p->macs_per_cc) || (cfg.shram_size != opt_cfg_p->shram_size) ||
645 (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version))
646 {
647 if (cfg.macs_per_cc != opt_cfg_p->macs_per_cc)
648 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200649 LOG_ERR("NPU config mismatch: npu.macs_per_cc=%" PRIu32 " optimizer.macs_per_cc=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200650 cfg.macs_per_cc,
651 opt_cfg_p->macs_per_cc);
652 }
653 if (cfg.shram_size != opt_cfg_p->shram_size)
654 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200655 LOG_ERR("NPU config mismatch: npu.shram_size=%" PRIu32 " optimizer.shram_size=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200656 cfg.shram_size,
657 opt_cfg_p->shram_size);
658 }
659 if (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version)
660 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200661 LOG_ERR("NPU config mismatch: npu.cmd_stream_version=%" PRIu32 " optimizer.cmd_stream_version=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200662 cfg.cmd_stream_version,
663 opt_cfg_p->cmd_stream_version);
664 }
665 return_code = -1;
666 }
667
Douglas Troha91e0be52021-01-18 13:57:38 +0100668 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 +0200669 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200670 LOG_ERR("NPU arch mismatch: npu.arch=%" PRIu32 ".%" PRIu32 ".%" PRIu32 " optimizer.arch=%d.%d.%d\n",
Bhavik Patel790ef362020-06-03 10:05:28 +0200671 id.arch_major_rev,
672 id.arch_minor_rev,
673 id.arch_patch_rev,
674 opt_cfg_p->arch_major_rev,
675 opt_cfg_p->arch_minor_rev,
676 opt_cfg_p->arch_patch_rev);
677 return_code = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200678 }
679
680#if !defined(LOG_ENABLED)
681 UNUSED(opt_cfg_p);
682#endif
683 return return_code;
684}
685
Bhavik Pateldae5be02020-06-18 15:25:15 +0200686static void npu_axi_init(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200687{
Bhavik Pateldae5be02020-06-18 15:25:15 +0200688 ethosu_set_qconfig(&drv->dev, NPU_QCONFIG);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200689
Bhavik Pateldae5be02020-06-18 15:25:15 +0200690 ethosu_set_regioncfg(&drv->dev, 0, NPU_REGIONCFG_0);
691 ethosu_set_regioncfg(&drv->dev, 1, NPU_REGIONCFG_1);
692 ethosu_set_regioncfg(&drv->dev, 2, NPU_REGIONCFG_2);
693 ethosu_set_regioncfg(&drv->dev, 3, NPU_REGIONCFG_3);
694 ethosu_set_regioncfg(&drv->dev, 4, NPU_REGIONCFG_4);
695 ethosu_set_regioncfg(&drv->dev, 5, NPU_REGIONCFG_5);
696 ethosu_set_regioncfg(&drv->dev, 6, NPU_REGIONCFG_6);
697 ethosu_set_regioncfg(&drv->dev, 7, NPU_REGIONCFG_7);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200698
Bhavik Pateldae5be02020-06-18 15:25:15 +0200699 (void)ethosu_set_axi_limit0(&drv->dev,
700 AXI_LIMIT0_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200701 AXI_LIMIT0_MEM_TYPE,
702 AXI_LIMIT0_MAX_OUTSTANDING_READS,
703 AXI_LIMIT0_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200704 (void)ethosu_set_axi_limit1(&drv->dev,
705 AXI_LIMIT1_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200706 AXI_LIMIT1_MEM_TYPE,
707 AXI_LIMIT1_MAX_OUTSTANDING_READS,
708 AXI_LIMIT1_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200709 (void)ethosu_set_axi_limit2(&drv->dev,
710 AXI_LIMIT2_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200711 AXI_LIMIT2_MEM_TYPE,
712 AXI_LIMIT2_MAX_OUTSTANDING_READS,
713 AXI_LIMIT2_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200714 (void)ethosu_set_axi_limit3(&drv->dev,
715 AXI_LIMIT3_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200716 AXI_LIMIT3_MEM_TYPE,
717 AXI_LIMIT3_MAX_OUTSTANDING_READS,
718 AXI_LIMIT3_MAX_OUTSTANDING_WRITES);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200719}
720
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200721/* Default implementation to flush the data cache. Override if available on the targeted device.
722 * Passing NULL as p argument expects the whole cache to be flushed.
723 */
724void __attribute__((weak)) ethosu_flush_dcache(uint32_t *p, size_t bytes)
725{
726 (void)p;
727 (void)bytes;
728}
729
730/* Default implementation to invalidate the data cache. Override if available on the targeted device.
731 * Passing NULL as p argument expects the whole cache to be flushed.
732 */
733void __attribute__((weak)) ethosu_invalidate_dcache(uint32_t *p, size_t bytes)
734{
735 (void)p;
736 (void)bytes;
737}
738
Bhavik Pateldae5be02020-06-18 15:25:15 +0200739static int handle_command_stream(struct ethosu_driver *drv,
740 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200741 const int cms_length,
742 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200743 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200744 const int num_base_addr)
745{
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100746 uint32_t qread = 0;
747 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
748 ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream;
749
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200750 LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d\n", cmd_stream, cms_length);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200751
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200752 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200753 {
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200754 LOG_ERR("Error: Command stream addr %p not aligned to 16 bytes\n", cmd_stream);
755 return -1;
756 }
757
758 bool base_addr_invalid = false;
759 for (int i = 0; i < num_base_addr; i++)
760 {
761 if (0 != (base_addr[i] & MASK_16_BYTE_ALIGN))
762 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200763 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 +0200764 base_addr_invalid = true;
765 }
766 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100767
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200768 if (base_addr_invalid)
769 {
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200770 return -1;
771 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100772
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200773 /* Flush the cache if available on our CPU.
774 * The upcasting to uin32_t* is ok since the pointer never is dereferenced.
775 * The base_addr_size is null if invoking from prior to invoke_V2, in that case
776 * the whole cache is being flushed.
777 */
778
779 if (base_addr_size != NULL)
780 {
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100781 ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200782 for (int i = 0; i < num_base_addr; i++)
783 {
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100784 ethosu_flush_dcache((uint32_t *)(uintptr_t)base_addr[i], base_addr_size[i]);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200785 }
786 }
787 else
788 {
789 ethosu_flush_dcache(NULL, 0);
790 }
791
Bhavik Pateldae5be02020-06-18 15:25:15 +0200792 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 +0200793 {
794 return -1;
795 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200796
Bhavik Pateldae5be02020-06-18 15:25:15 +0200797 wait_for_irq(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200798
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200799 if (drv->status_error)
800 {
801 return -1;
802 }
803
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200804 if (base_addr_size != NULL)
805 {
806 for (int i = 0; i < num_base_addr; i++)
807 {
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100808 ethosu_invalidate_dcache((uint32_t *)(uintptr_t)base_addr[i], base_addr_size[i]);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200809 }
810 }
811 else
812 {
813 ethosu_invalidate_dcache(NULL, 0);
814 }
815
Bhavik Pateldae5be02020-06-18 15:25:15 +0200816 (void)ethosu_get_qread(&drv->dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200817 if (qread != cms_bytes)
818 {
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200819 LOG_WARN(
Per Åstrand14ccfee2020-09-25 10:40:20 +0200820 "Failure: IRQ received but qread (%" PRIu32 ") not at end of stream (%" PRIu32 ").\n", qread, cms_bytes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200821 return -1;
822 }
823
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200824 return 0;
825}
826
Bhavik Pateldae5be02020-06-18 15:25:15 +0200827static int read_apb_reg(struct ethosu_driver *drv, uint16_t da_data)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200828{
829 uint32_t *reg_p;
830 uint32_t start_address = (uint32_t)(da_data & APB_START_ADDR_MASK);
831 uint16_t num_reg = (da_data >> APB_NUM_REG_BIT_SHIFT) + 1;
832
833 reg_p = (uint32_t *)malloc(num_reg * sizeof(uint32_t));
834 if (reg_p == NULL)
835 {
836 LOG_INFO("read_apb_reg, Error! memory not allocated.");
837 return -1;
838 }
839
Bhavik Pateldae5be02020-06-18 15:25:15 +0200840 if (ETHOSU_SUCCESS == ethosu_read_apb_reg(&drv->dev, start_address, num_reg, reg_p))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200841 {
842 for (int i = 0; i < num_reg; i++)
843 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200844 LOG_INFO(
845 "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 +0200846 }
847 }
848 else
849 {
850 free(reg_p);
851 return -1;
852 }
853
854 free(reg_p);
855 return 0;
856}
857
Bhavik Pateldae5be02020-06-18 15:25:15 +0200858static int dump_shram(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200859{
860 struct ethosu_config cfg;
861 uint32_t *shram_p;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200862 (void)ethosu_get_config(&drv->dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200863
Per Åstrand14ccfee2020-09-25 10:40:20 +0200864 LOG_INFO("dump_shram size = %" PRIu32 " KB\n", cfg.shram_size);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200865
866 shram_p = (uint32_t *)malloc(BYTES_1KB);
867 if (shram_p == NULL)
868 {
869 LOG_ERR("read_shram, Error! memory not allocated.");
870 return -1;
871 }
872
873 for (uint32_t i = 0; i < cfg.shram_size; i++)
874 {
Bhavik Pateldae5be02020-06-18 15:25:15 +0200875 ethosu_get_shram_data(&drv->dev, i, (uint32_t *)shram_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200876 // Output 1KB of SHRAM
Per Åstrand14ccfee2020-09-25 10:40:20 +0200877 LOG_INFO("***SHRAM SECTION %" PRIu32 "***\n", i);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200878 for (int j = 0; j < (BYTES_1KB / BYTES_IN_32_BITS); j++)
879 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200880 LOG_INFO("[0x%04" PRIx32 "] %" PRIx32 "\n", (i * 1024 + j * 4), shram_p[j]);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200881 }
882 }
883 free(shram_p);
884
885 return 0;
886}
887
888typedef struct
889{
890 int number;
891 const char *name;
892} name_lookup_t;
893
894static const name_lookup_t npu_reg_name_tbl[] = {
895 {0x200, "KERNEL_X"},
896 {0x204, "KERNEL_Y"},
897 {0x208, "KERNEL_W_M1"},
898 {0x20C, "KERNEL_H_M1"},
899 {0x210, "OFM_CBLK_WIDTH_M1"},
900 {0x214, "OFM_CBLK_HEIGHT_M1"},
901 {0x218, "OFM_CBLK_DEPTH_M1"},
902 {0x21c, "IFM_CBLK_DEPTH_M1"},
903 {0x220, "OFM_X"},
904 {0x224, "OFM_Y"},
905 {0x228, "OFM_Z"},
906 {0x22C, "IFM_Z"},
907 {0x230, "PAD_TOP"},
908 {0x234, "PAD_LEFT"},
909 {0x238, "IFM_CBLK_WIDTH"},
910 {0x23C, "IFM_CBLK_HEIGHT"},
911 {0x240, "DMA_IFM_SRC"},
912 {0x244, "DMA_IFM_SRC_HI"},
913 {0x248, "DMA_IFM_DST"},
914 {0x24c, "DMA_OFM_SRC"},
915 {0x250, "DMA_OFM_DST"},
916 {0x254, "DMA_OFM_DST_HI"},
917 {0x258, "DMA_WEIGHT_SRC"},
918 {0x25c, "DMA_WEIGHT_SRC_HI"},
919 {0x260, "DMA_CMD_SRC"},
920 {0x264, "DMA_CMD_SRC_HI"},
921 {0x268, "DMA_CMD_SIZE"},
922 {0x26c, "DMA_M2M_SRC"},
923 {0x270, "DMA_M2M_SRC_HI"},
924 {0x274, "DMA_M2M_DST"},
925 {0x278, "DMA_M2M_DST_HI"},
926 {0x27c, "CURRENT_QREAD"},
927 {0x280, "DMA_SCALE_SRC"},
928 {0x284, "DMA_SCALE_SRC_HI"},
929 {0x2BC, "CURRENT_CMD"},
930 {0x800, "IFM_PAD_TOP"},
931 {0x804, "IFM_PAD_LEFT"},
932 {0x808, "IFM_PAD_RIGHT"},
933 {0x80C, "IFM_PAD_BOTTOM"},
934 {0x810, "IFM_DEPTH_M1"},
935 {0x814, "IFM_PRECISION"},
936 {0x81C, "IFM_UPSCALE"},
937 {0x824, "IFM_ZERO_POINT"},
938 {0x828, "IFM_WIDTH0_M1"},
939 {0x82C, "IFM_HEIGHT0_M1"},
940 {0x830, "IFM_HEIGHT1_M1"},
941 {0x834, "IFM_IB_END"},
942 {0x83C, "IFM_REGION"},
943 {0x844, "OFM_WIDTH_M1"},
944 {0x848, "OFM_HEIGHT_M1"},
945 {0x84C, "OFM_DEPTH_M1"},
946 {0x850, "OFM_PRECISION"},
947 {0x854, "OFM_BLK_WIDTH_M1"},
948 {0x858, "OFM_BLK_HEIGHT_M1"},
949 {0x85C, "OFM_BLK_DEPTH_M1"},
950 {0x860, "OFM_ZERO_POINT"},
951 {0x868, "OFM_WIDTH0_M1"},
952 {0x86C, "OFM_HEIGHT0_M1"},
953 {0x870, "OFM_HEIGHT1_M1"},
954 {0x87C, "OFM_REGION"},
955 {0x880, "KERNEL_WIDTH_M1"},
956 {0x884, "KERNEL_HEIGHT_M1"},
957 {0x888, "KERNEL_STRIDE"},
958 {0x88C, "PARALLEL_MODE"},
959 {0x890, "ACC_FORMAT"},
960 {0x894, "ACTIVATION"},
961 {0x898, "ACTIVATION_MIN"},
962 {0x89C, "ACTIVATION_MAX"},
963 {0x8A0, "WEIGHT_REGION"},
964 {0x8A4, "SCALE_REGION"},
965 {0x8B4, "AB_START"},
966 {0x8BC, "BLOCKDEP"},
967 {0x8C0, "DMA0_SRC_REGION"},
968 {0x8C4, "DMA0_DST_REGION"},
969 {0x8C8, "DMA0_SIZE0"},
970 {0x8CC, "DMA0_SIZE1"},
971 {0x900, "IFM2_BROADCAST"},
972 {0x904, "IFM2_SCALAR"},
973 {0x924, "IFM2_ZERO_POINT"},
974 {0x928, "IFM2_WIDTH0_M1"},
975 {0x92C, "IFM2_HEIGHT0_M1"},
976 {0x930, "IFM2_HEIGHT1_M1"},
977 {0x934, "IFM2_IB_START"},
978 {0x93C, "IFM2_REGION"},
979 {0xA00, "IFM_BASE0"},
980 {0xA04, "IFM_BASE0_HI"},
981 {0xA08, "IFM_BASE1"},
982 {0xA0C, "IFM_BASE1_HI"},
983 {0xA10, "IFM_BASE2"},
984 {0xA14, "IFM_BASE2_HI"},
985 {0xA18, "IFM_BASE3"},
986 {0xA1C, "IFM_BASE3_HI"},
987 {0xA20, "IFM_STRIDE_X"},
988 {0xA24, "IFM_STRIDE_X_HI"},
989 {0xA28, "IFM_STRIDE_Y"},
990 {0xA2C, "IFM_STRIDE_Y_HI"},
991 {0xA30, "IFM_STRIDE_C"},
992 {0xA34, "IFM_STRIDE_C_HI"},
993 {0xA40, "OFM_BASE0"},
994 {0xA44, "OFM_BASE0_HI"},
995 {0xA48, "OFM_BASE1"},
996 {0xA4C, "OFM_BASE1_HI"},
997 {0xA50, "OFM_BASE2"},
998 {0xA54, "OFM_BASE2_HI"},
999 {0xA58, "OFM_BASE3"},
1000 {0xA5C, "OFM_BASE3_HI"},
1001 {0xA60, "OFM_STRIDE_X"},
1002 {0xA64, "OFM_STRIDE_X_HI"},
1003 {0xA68, "OFM_STRIDE_Y"},
1004 {0xA6C, "OFM_STRIDE_Y_HI"},
1005 {0xA70, "OFM_STRIDE_C"},
1006 {0xA74, "OFM_STRIDE_C_HI"},
1007 {0xA80, "WEIGHT_BASE"},
1008 {0xA84, "WEIGHT_BASE_HI"},
1009 {0xA88, "WEIGHT_LENGTH"},
1010 {0xA8C, "WEIGHT_LENGTH_HI"},
1011 {0xA90, "SCALE_BASE"},
1012 {0xA94, "SCALE_BASE_HI"},
1013 {0xA98, "SCALE_LENGTH"},
1014 {0xAA0, "OFM_SCALE"},
1015 {0xAA4, "OFM_SCALE_SHIFT"},
1016 {0xAA8, "OPA_SCALE "},
1017 {0xAB0, "OPB_SCALE"},
1018 {0xAC0, "DMA0_SRC"},
1019 {0xAC4, "DMA0_SRC_HI"},
1020 {0xAC8, "DMA0_DST"},
1021 {0xACC, "DMA0_DST_HI"},
1022 {0xAD0, "DMA0_LEN"},
1023 {0xAD4, "DMA0_LEN_HI"},
1024 {0xAD8, "DMA0_SKIP0"},
1025 {0xADC, "DMA0_SKIP0_HI"},
1026 {0xAE0, "DMA0_SKIP1"},
1027 {0xAE4, "DMA0_SKIP1_HI"},
1028 {0xB00, "IFM2_BASE0"},
1029 {0xB04, "IFM2_BASE0_HI"},
1030 {0xB08, "IFM2_BASE1"},
1031 {0xB0C, "IFM2_BASE1_HI"},
1032 {0xB10, "IFM2_BASE2"},
1033 {0xB14, "IFM2_BASE2_HI"},
1034 {0xB18, "IFM2_BASE3"},
1035 {0xB1C, "IFM2_BASE3_HI"},
1036 {0xB20, "IFM2_STRIDE_X"},
1037 {0xB24, "IFM2_STRIDE_X_HI"},
1038 {0xB28, "IFM2_STRIDE_Y"},
1039 {0xB2C, "IFM2_STRIDE_Y_HI"},
1040 {0xB30, "IFM2_STRIDE_C"},
1041 {0xB34, "IFM2_STRIDE_C_HI"},
1042 {0xB40, "WEIGHT1_BASE"},
1043 {0xB44, "WEIGHT1_BASE_HI"},
1044 {0xB48, "WEIGHT1_LENGTH"},
1045 {0xB4C, "WEIGHT1_LENGTH_HI"},
1046 {0xB50, "SCALE1_BASE"},
1047 {0xB54, "SCALE1_BASE_HI"},
1048 {0xB58, "SCALE1_LENGTH"},
1049};
1050
1051static const char *lookup_name(const name_lookup_t *lookup_table, int lookup_table_count, int find)
1052{
1053 int n;
1054 for (n = 0; n < lookup_table_count; n++)
1055 {
1056 if (lookup_table[n].number == find)
1057 {
1058 return lookup_table[n].name;
1059 }
1060 }
1061 // Not found
1062 return 0;
1063}
1064
Bhavik Pateldae5be02020-06-18 15:25:15 +02001065static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001066{
1067 unsigned int reg_val;
1068 const char *reg_name;
1069 int npu_reg_name_tbl_count = sizeof(npu_reg_name_tbl) / sizeof(npu_reg_name_tbl[0]);
1070
1071 LOG_INFO("dump_register %X - %X\n", npu_reg, npu_reg_end);
1072 for (; npu_reg <= npu_reg_end; npu_reg += sizeof(int))
1073 {
Bhavik Pateldae5be02020-06-18 15:25:15 +02001074 reg_val = ethosu_read_reg(&drv->dev, npu_reg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001075 reg_name = lookup_name(npu_reg_name_tbl, npu_reg_name_tbl_count, npu_reg);
1076 LOG_INFO("[0x%.4X] 0x%.8X\t%s\n", npu_reg, reg_val, (reg_name) ? reg_name : "");
1077 }
1078}
1079
1080static const name_lookup_t cmd0_name_tbl[] = {
1081 {0x000, "NPU_OP_STOP"},
1082 {0x001, "NPU_OP_IRQ"},
1083 {0x002, "NPU_OP_CONV"},
1084 {0x003, "NPU_OP_DEPTHWISE"},
1085 {0x004, "NPU_OP_VECTOR_PROD"},
1086 {0x005, "NPU_OP_POOL"},
1087 {0x006, "NPU_OP_ELEMENTWISE"},
1088 {0x010, "NPU_OP_DMA_START"},
1089 {0x011, "NPU_OP_DMA_WAIT"},
1090 {0x012, "NPU_OP_KERNEL_WAIT"},
1091 {0x100, "NPU_SET_IFM_PAD_TOP"},
1092 {0x101, "NPU_SET_IFM_PAD_LEFT"},
1093 {0x102, "NPU_SET_IFM_PAD_RIGHT"},
1094 {0x103, "NPU_SET_IFM_PAD_BOTTOM"},
1095 {0x104, "NPU_SET_IFM_DEPTH_M1"},
1096 {0x105, "NPU_SET_IFM_PRECISION"},
1097 {0x107, "NPU_SET_IFM_UPSCALE"},
1098 {0x109, "NPU_SET_IFM_ZERO_POINT"},
1099 {0x10A, "NPU_SET_IFM_WIDTH0_M1"},
1100 {0x10B, "NPU_SET_IFM_HEIGHT0_M1"},
1101 {0x10C, "NPU_SET_IFM_HEIGHT1_M1"},
1102 {0x10D, "NPU_SET_IFM_IB_END"},
1103 {0x10F, "NPU_SET_IFM_REGION"},
1104 {0x110, "NPU_SET_OFM_BATCH_SIZE_M1"},
1105 {0x111, "NPU_SET_OFM_WIDTH_M1"},
1106 {0x112, "NPU_SET_OFM_HEIGHT_M1"},
1107 {0x113, "NPU_SET_OFM_DEPTH_M1"},
1108 {0x114, "NPU_SET_OFM_PRECISION"},
1109 {0x115, "NPU_SET_OFM_BLK_WIDTH_M1"},
1110 {0x116, "NPU_SET_OFM_BLK_HEIGHT_M1"},
1111 {0x117, "NPU_SET_OFM_BLK_DEPTH_M1"},
1112 {0x118, "NPU_SET_OFM_ZERO_POINT"},
1113 {0x11A, "NPU_SET_OFM_WIDTH0_M1"},
1114 {0x11B, "NPU_SET_OFM_HEIGHT0_M1"},
1115 {0x11C, "NPU_SET_OFM_HEIGHT1_M1"},
1116 {0x11F, "NPU_SET_OFM_REGION"},
1117 {0x120, "NPU_SET_KERNEL_WIDTH_M1"},
1118 {0x121, "NPU_SET_KERNEL_HEIGHT_M1"},
1119 {0x122, "NPU_SET_KERNEL_STRIDE"},
1120 {0x124, "NPU_SET_ACC_FORMAT"},
1121 {0x125, "NPU_SET_ACTIVATION"},
1122 {0x126, "NPU_SET_ACTIVATION_MIN"},
1123 {0x127, "NPU_SET_ACTIVATION_MAX"},
1124 {0x128, "NPU_SET_WEIGHT_REGION"},
1125 {0x129, "NPU_SET_SCALE_REGION"},
1126 {0x12D, "NPU_SET_AB_START"},
1127 {0x12F, "NPU_SET_BLOCKDEP"},
1128 {0x130, "NPU_SET_DMA0_SRC_REGION"},
1129 {0x131, "NPU_SET_DMA0_DST_REGION"},
1130 {0x180, "NPU_SET_IFM2_BROADCAST"},
1131 {0x181, "NPU_SET_IFM2_SCALAR"},
1132 {0x185, "NPU_SET_IFM2_PRECISION"},
1133 {0x189, "NPU_SET_IFM2_ZERO_POINT"},
1134 {0x18A, "NPU_SET_IFM2_WIDTH0_M1"},
1135 {0x18B, "NPU_SET_IFM2_HEIGHT0_M1"},
1136 {0x18C, "NPU_SET_IFM2_HEIGHT1_M1"},
1137 {0x18D, "NPU_SET_IFM2_IB_START"},
1138 {0x18F, "NPU_SET_IFM2_REGION"},
1139};
1140
1141static const name_lookup_t cmd1_name_tbl[] = {
1142 {0x000, "NPU_SET_IFM_BASE0"}, {0x001, "NPU_SET_IFM_BASE1"}, {0x002, "NPU_SET_IFM_BASE2"},
1143 {0x003, "NPU_SET_IFM_BASE3"}, {0x004, "NPU_SET_IFM_STRIDE_X"}, {0x005, "NPU_SET_IFM_STRIDE_Y"},
1144 {0x006, "NPU_SET_IFM_STRIDE_C"}, {0x007, "NPU_SET_IFM_STRIDE_N"}, {0x010, "NPU_SET_OFM_BASE0"},
1145 {0x011, "NPU_SET_OFM_BASE1"}, {0x012, "NPU_SET_OFM_BASE2"}, {0x013, "NPU_SET_OFM_BASE3"},
1146 {0x014, "NPU_SET_OFM_STRIDE_X"}, {0x015, "NPU_SET_OFM_STRIDE_Y"}, {0x016, "NPU_SET_OFM_STRIDE_C"},
1147 {0x017, "NPU_SET_OFM_STRIDE_N"}, {0x020, "NPU_SET_WEIGHT_BASE"}, {0x021, "NPU_SET_WEIGHT_LENGTH"},
1148 {0x022, "NPU_SET_SCALE_BASE"}, {0x023, "NPU_SET_SCALE_LENGTH"}, {0x024, "NPU_SET_OFM_SCALE"},
1149 {0x025, "NPU_SET_OPA_SCALE"}, {0x026, "NPU_SET_OPB_SCALE"}, {0x030, "NPU_SET_DMA0_SRC"},
1150 {0x031, "NPU_SET_DMA0_DST"}, {0x032, "NPU_SET_DMA0_LEN"}, {0x080, "NPU_SET_IFM2_BASE0"},
1151 {0x081, "NPU_SET_IFM2_BASE1"}, {0x082, "NPU_SET_IFM2_BASE2"}, {0x083, "NPU_SET_IFM2_BASE3"},
1152 {0x084, "NPU_SET_IFM2_STRIDE_X"}, {0x085, "NPU_SET_IFM2_STRIDE_Y"}, {0x086, "NPU_SET_IFM2_STRIDE_C"},
1153};
1154
1155static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread)
1156{
1157 int n;
1158 int offset;
1159 uint32_t cmd_val;
1160 const uint8_t *cmd_ptr;
1161 const char *cmd_name;
1162 int cmd0_name_tbl_count = sizeof(cmd0_name_tbl) / sizeof(cmd0_name_tbl[0]);
1163 int cmd1_name_tbl_count = sizeof(cmd1_name_tbl) / sizeof(cmd1_name_tbl[0]);
1164
1165 LOG_INFO("dump_command_stream cmd_stream = 0x%8p cms_length = %d\n", cmd_stream, cms_length);
1166 for (n = 0; n < cms_length; n++)
1167 {
1168 // Offset
1169 offset = n * sizeof(int);
1170 LOG_INFO("[%.4d] ", offset);
1171 // Command
1172 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1173 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1174 // Command name and payload
1175 if (cmd_stream[n] & 0x4000)
1176 {
1177 cmd_name = lookup_name(cmd1_name_tbl, cmd1_name_tbl_count, cmd_stream[n] & 0x3FF);
1178 n++;
1179 cmd_val = cmd_stream[n];
1180 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1181 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1182 }
1183 else
1184 {
1185 cmd_val = cmd_stream[n] >> 16;
1186 cmd_name = lookup_name(cmd0_name_tbl, cmd0_name_tbl_count, cmd_stream[n] & 0x3FF);
1187 }
1188 if (cmd_name)
1189 {
Per Åstrand14ccfee2020-09-25 10:40:20 +02001190 LOG_INFO("\t%s 0x%.8" PRIX32, cmd_name, cmd_val);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001191 }
1192 if (offset == qread)
1193 {
1194 LOG_INFO(" <<== QREAD\n");
1195 }
1196 else
1197 {
1198 LOG_INFO("\n");
1199 }
1200 }
1201}