blob: 201eeff845be109faffdeda09c3346124830353d [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
Kristofer Jonsson09273d12021-03-15 08:43:08 +01002 * Copyright (c) 2019-2021 Arm Limited. All rights reserved.
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02003 *
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;
Anton Mobergb8bcf132021-03-29 10:02:25 +0200127 uint32_t reserved0 : 11;
128 uint32_t custom_dma : 1;
129 uint32_t product : 4;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200130 };
131 uint32_t npu_cfg;
132 };
133 union
134 {
135 struct
136 {
137 uint32_t version_status : 4;
138 uint32_t version_minor : 4;
139 uint32_t version_major : 4;
140 uint32_t product_major : 4;
141 uint32_t arch_patch_rev : 4;
142 uint32_t arch_minor_rev : 8;
143 uint32_t arch_major_rev : 4;
144 };
145 uint32_t ethosu_id;
146 };
147};
148
149/******************************************************************************
150 * Functions
151 ******************************************************************************/
152
Kristofer Jonssonef387ea2020-08-25 16:32:21 +0200153struct ethosu_driver ethosu_drv = {
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100154 .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 +0100155 .abort_inference = false,
156 .status_error = false,
157 .dev_power_always_on = false};
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200158
Anton Moberg61da4d32020-12-22 16:00:31 +0100159// Registered drivers linked list HEAD
160static struct ethosu_driver *registered_drivers = NULL;
161
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100162/*
163 * Following section handles the minimal sempahore and mutex implementation in case of baremetal applications.
164 * Weak symbols will be overwritten by RTOS definitions and implement true thread-safety. (Done in application layer)
165 */
166
167// Baremetal sempahore implementation
168struct ethosu_semaphore_t
169{
170 int count;
171};
172
173// Minimal needed declaration to allow baremetal functionality.
174static void *ethosu_mutex;
175static void *ethosu_semaphore;
176
177void *__attribute__((weak)) ethosu_mutex_create(void) {}
178
179void __attribute__((weak)) ethosu_mutex_lock(void *mutex) {}
180
181void __attribute__((weak)) ethosu_mutex_unlock(void *mutex) {}
182
183// Baremetal implementation of creating a semaphore
184void *__attribute__((weak)) ethosu_semaphore_create(void)
185{
186 struct ethosu_semaphore_t *sem = malloc(sizeof(*sem));
187 sem->count = 1;
188 return sem;
189}
190
191// Baremetal simulation of waiting/sleeping for and then taking a semaphore using intrisics
192void __attribute__((weak)) ethosu_semaphore_take(void *sem)
193{
194 struct ethosu_semaphore_t *s = sem;
195 while (s->count <= 0)
196 {
197 __WFE();
198 }
199 s->count--;
200}
201
202// Baremetal simulation of giving a semaphore and waking up processes using intrinsics
203void __attribute__((weak)) ethosu_semaphore_give(void *sem)
204{
205 struct ethosu_semaphore_t *s = sem;
206 s->count++;
207 __SEV();
208}
209// <--- End of semaphore and mutex implementations
210
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100211static int ethosu_soft_reset_and_restore(struct ethosu_driver *drv);
Anton Moberg61da4d32020-12-22 16:00:31 +0100212
Kristofer Jonsson09273d12021-03-15 08:43:08 +0100213void __attribute__((weak)) ethosu_irq_handler_v2(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200214{
215 uint8_t irq_raised = 0;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200216
217 LOG_DEBUG("Interrupt. status=0x%08x, qread=%d\n",
Anton Moberg61da4d32020-12-22 16:00:31 +0100218 ethosu_read_reg(&drv->dev, NPU_REG_STATUS),
219 ethosu_read_reg(&drv->dev, NPU_REG_QREAD));
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200220
221 // Verify that interrupt has been raised
Anton Moberg61da4d32020-12-22 16:00:31 +0100222 (void)ethosu_is_irq_raised(&drv->dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200223 ASSERT(irq_raised == 1);
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100224 drv->irq_triggered = true;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200225
226 // Clear interrupt
Anton Moberg61da4d32020-12-22 16:00:31 +0100227 (void)ethosu_clear_irq_status(&drv->dev);
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200228
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200229 // Verify that interrupt has been successfully cleared
Anton Moberg61da4d32020-12-22 16:00:31 +0100230 (void)ethosu_is_irq_raised(&drv->dev, &irq_raised);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200231 ASSERT(irq_raised == 0);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200232
Anton Moberg61da4d32020-12-22 16:00:31 +0100233 if (ethosu_status_has_error(&drv->dev))
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200234 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100235 ethosu_soft_reset_and_restore(drv);
236 drv->status_error = true;
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200237 }
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100238
239 ethosu_semaphore_give(drv->semaphore);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200240}
241
Bhavik Pateldae5be02020-06-18 15:25:15 +0200242static inline void wait_for_irq(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200243{
244 while (1)
245 {
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100246 if (drv->irq_triggered || drv->abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200247 {
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100248 drv->irq_triggered = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200249 break;
250 }
251
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100252 ethosu_semaphore_take(drv->semaphore);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200253 }
254}
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200255
Bhavik Pateldae5be02020-06-18 15:25:15 +0200256static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p);
257static int handle_command_stream(struct ethosu_driver *drv,
258 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200259 const int cms_length,
260 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200261 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200262 const int num_base_addr);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200263static int read_apb_reg(struct ethosu_driver *drv, uint16_t);
264static int dump_shram(struct ethosu_driver *drv);
265static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200266static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200267static void npu_axi_init(struct ethosu_driver *drv);
Anton Mobergdf386e02021-02-02 11:26:48 +0100268static struct ethosu_driver *ethosu_find_and_reserve_driver(void);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200269
Anton Moberg61da4d32020-12-22 16:00:31 +0100270int ethosu_init_v4(struct ethosu_driver *drv,
271 const void *base_address,
Per Åstrande6498f02020-11-09 15:33:12 +0100272 const void *fast_memory,
273 const size_t fast_memory_size,
274 uint32_t secure_enable,
275 uint32_t privilege_enable)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200276{
277 int return_code = 0;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200278
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100279 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 +0200280 __FUNCTION__,
281 base_address,
282 fast_memory,
Per Åstrande6498f02020-11-09 15:33:12 +0100283 fast_memory_size,
284 secure_enable,
285 privilege_enable);
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200286
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100287 if (!ethosu_mutex)
288 {
289 ethosu_mutex = ethosu_mutex_create();
290 }
291
292 if (!ethosu_semaphore)
293 {
294 ethosu_semaphore = ethosu_semaphore_create();
295 }
296
Anton Moberg61da4d32020-12-22 16:00:31 +0100297 ethosu_register_driver(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200298
Anton Moberg61da4d32020-12-22 16:00:31 +0100299 drv->fast_memory = (uint32_t)fast_memory;
300 drv->fast_memory_size = fast_memory_size;
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100301 drv->irq_triggered = false;
302 drv->semaphore = ethosu_semaphore_create();
Anton Moberg61da4d32020-12-22 16:00:31 +0100303
304 if (ETHOSU_SUCCESS != ethosu_dev_init(&drv->dev, base_address, secure_enable, privilege_enable))
Bhavik Pateldae5be02020-06-18 15:25:15 +0200305 {
306 LOG_ERR("Failed in ethosu_dev_init");
307 return -1;
308 }
309
Anton Moberg0a614292021-03-24 14:08:22 +0100310 if (ETHOSU_SUCCESS !=
311 set_clock_and_power_request(drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_DISABLE, ETHOSU_POWER_Q_DISABLE))
Bhavik Patele645fed2020-06-12 14:46:47 +0200312 {
313 LOG_ERR("Failed to disable clock-q & power-q for Ethos-U\n");
314 return -1;
315 }
316
Anton Moberg61da4d32020-12-22 16:00:31 +0100317 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
Per Åstrand849cf692020-11-24 07:39:55 +0100318 {
319 return -1;
320 }
Kristofer Jonssondaa0d202020-05-12 12:23:16 +0200321
Anton Moberg61da4d32020-12-22 16:00:31 +0100322 if (ETHOSU_SUCCESS != ethosu_wait_for_reset(&drv->dev))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200323 {
324 LOG_ERR("Failed reset of Ethos-U\n");
325 return -1;
326 }
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100327
Anton Moberg61da4d32020-12-22 16:00:31 +0100328 drv->status_error = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200329
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200330 return return_code;
331}
332
Anton Moberg61da4d32020-12-22 16:00:31 +0100333int ethosu_get_version_v2(struct ethosu_driver *drv, struct ethosu_version *version)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200334{
335 int return_code = 0;
336
337 if (NULL != version)
338 {
339 struct ethosu_id id;
340 struct ethosu_config cfg;
Anton Moberg61da4d32020-12-22 16:00:31 +0100341 (void)ethosu_get_id(&drv->dev, &id);
342 (void)ethosu_get_config(&drv->dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200343
344 version->id.version_status = id.version_status;
345 version->id.version_minor = id.version_minor;
346 version->id.version_major = id.version_major;
347 version->id.product_major = id.product_major;
348 version->id.arch_patch_rev = id.arch_patch_rev;
349 version->id.arch_minor_rev = id.arch_minor_rev;
350 version->id.arch_major_rev = id.arch_major_rev;
351 version->id.driver_patch_rev = ETHOSU_DRIVER_VERSION_PATCH;
352 version->id.driver_minor_rev = ETHOSU_DRIVER_VERSION_MINOR;
353 version->id.driver_major_rev = ETHOSU_DRIVER_VERSION_MAJOR;
354 version->cfg.macs_per_cc = cfg.macs_per_cc;
355 version->cfg.cmd_stream_version = cfg.cmd_stream_version;
356 version->cfg.shram_size = cfg.shram_size;
Anton Mobergb8bcf132021-03-29 10:02:25 +0200357 version->cfg.custom_dma = cfg.custom_dma;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200358 }
359 else
360 {
361 return_code = -1;
362 }
363
364 return return_code;
365}
366
Anton Moberg61da4d32020-12-22 16:00:31 +0100367int ethosu_invoke_v3(struct ethosu_driver *drv,
368 const void *custom_data_ptr,
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200369 const int custom_data_size,
370 const uint64_t *base_addr,
371 const size_t *base_addr_size,
372 const int num_base_addr)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200373{
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200374 const struct custom_data_s *data_ptr = custom_data_ptr;
375 const struct custom_data_s *data_end = custom_data_ptr + custom_data_size;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200376 int return_code = 0;
377
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200378 LOG_INFO("%s\n", __FUNCTION__);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200379
380 // First word in custom_data_ptr should contain "Custom Operator Payload 1"
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200381 if (data_ptr->word != ETHOSU_FOURCC)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200382 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200383 LOG_ERR("Custom Operator Payload: %" PRIu32 " is not correct, expected %x\n", data_ptr->word, ETHOSU_FOURCC);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200384 return -1;
385 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200386
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200387 // Custom data length must be a multiple of 32 bits
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200388 if ((custom_data_size % BYTES_IN_32_BITS) != 0)
389 {
390 LOG_ERR("ethosu_invoke ERROR custom_data_size=0x%x not a multiple of 4\n", custom_data_size);
391 return -1;
392 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200393
394 ++data_ptr;
395
396 // Adjust base address to fast memory area
Anton Moberg61da4d32020-12-22 16:00:31 +0100397 if (drv->fast_memory != 0 && num_base_addr >= FAST_MEMORY_BASE_ADDR_INDEX)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200398 {
399 uint64_t *fast_memory = (uint64_t *)&base_addr[FAST_MEMORY_BASE_ADDR_INDEX];
400
Anton Moberg61da4d32020-12-22 16:00:31 +0100401 if (base_addr_size != NULL && base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX] > drv->fast_memory_size)
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200402 {
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100403 LOG_ERR("Fast memory area too small. fast_memory_size=%u, base_addr_size=%u\n",
Anton Moberg61da4d32020-12-22 16:00:31 +0100404 drv->fast_memory_size,
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100405 base_addr_size[FAST_MEMORY_BASE_ADDR_INDEX]);
406 return -1;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200407 }
Kristofer Jonsson4c94b302020-11-06 10:33:21 +0100408
Anton Moberg61da4d32020-12-22 16:00:31 +0100409 *fast_memory = drv->fast_memory;
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200410 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200411
Anton Moberg61da4d32020-12-22 16:00:31 +0100412 if (!drv->dev_power_always_on)
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200413 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100414 // Only soft reset if securty state or privilege level needs changing
415 if (drv->dev.proto != ethosu_read_reg(&drv->dev, NPU_REG_PROT))
Per Åstrand849cf692020-11-24 07:39:55 +0100416 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100417 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100418 {
419 return -1;
420 }
Per Åstrand849cf692020-11-24 07:39:55 +0100421 }
Anton Moberg61da4d32020-12-22 16:00:31 +0100422
423 drv->status_error = false;
Anton Moberg0a614292021-03-24 14:08:22 +0100424 set_clock_and_power_request(drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
Anton Moberg61da4d32020-12-22 16:00:31 +0100425 ethosu_restore_pmu_config(&drv->dev);
426 npu_axi_init(drv);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200427 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100428
Anton Moberg61da4d32020-12-22 16:00:31 +0100429 drv->status_error = false;
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200430
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200431 while (data_ptr < data_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200432 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200433 int ret = 0;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200434 switch (data_ptr->driver_action_command)
435 {
436 case OPTIMIZER_CONFIG:
437 LOG_INFO("ethosu_invoke OPTIMIZER_CONFIG\n");
438 struct opt_cfg_s *opt_cfg_p = (struct opt_cfg_s *)data_ptr;
439
Anton Moberg61da4d32020-12-22 16:00:31 +0100440 ret = handle_optimizer_config(drv, opt_cfg_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200441 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + OPTIMIZER_CONFIG_LENGTH_32_BIT_WORD;
442 break;
443 case COMMAND_STREAM:
444 LOG_INFO("ethosu_invoke COMMAND_STREAM\n");
445 void *command_stream = (uint8_t *)(data_ptr) + sizeof(struct custom_data_s);
446 int cms_length = (data_ptr->reserved << 16) | data_ptr->length;
447
Anton Moberg61da4d32020-12-22 16:00:31 +0100448 drv->abort_inference = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200449 // It is safe to clear this flag without atomic, because npu is not running.
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100450 drv->irq_triggered = false;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200451
Anton Moberg61da4d32020-12-22 16:00:31 +0100452 ret = handle_command_stream(drv, command_stream, cms_length, base_addr, base_addr_size, num_base_addr);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200453
Anton Moberg61da4d32020-12-22 16:00:31 +0100454 if (return_code == -1 && drv->abort_inference)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200455 {
456 uint32_t qread = 0;
Anton Moberg61da4d32020-12-22 16:00:31 +0100457 ethosu_get_qread(&drv->dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200458 LOG_ERR("NPU timeout\n");
459 dump_command_stream(command_stream, cms_length, qread);
Anton Moberg61da4d32020-12-22 16:00:31 +0100460 dump_npu_register(drv, 0x200, 0x2BF);
461 dump_npu_register(drv, 0x800, 0xB3F);
462 dump_shram(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200463 }
464
465 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD + cms_length;
466 break;
467 case READ_APB_REG:
468 LOG_INFO("ethosu_invoke READ_APB_REG\n");
Anton Moberg61da4d32020-12-22 16:00:31 +0100469 ret = read_apb_reg(drv, data_ptr->driver_action_data);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200470 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
471 break;
472 case DUMP_SHRAM:
473 LOG_INFO("ethosu_invoke DUMP_SHRAM\n");
Anton Moberg61da4d32020-12-22 16:00:31 +0100474 ret = dump_shram(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200475 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
476 break;
477 case NOP:
478 LOG_INFO("ethosu_invoke NOP\n");
479 data_ptr += DRIVER_ACTION_LENGTH_32_BIT_WORD;
480 break;
481 default:
482 LOG_ERR("ethosu_invoke UNSUPPORTED driver_action_command %d \n", data_ptr->driver_action_command);
Bhavik Patele645fed2020-06-12 14:46:47 +0200483 ret = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200484 break;
485 }
Bhavik Patele645fed2020-06-12 14:46:47 +0200486 if (ret != 0)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200487 {
Bhavik Patele645fed2020-06-12 14:46:47 +0200488 return_code = -1;
489 break;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200490 }
491 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200492
Anton Moberg61da4d32020-12-22 16:00:31 +0100493 if (!drv->status_error && !drv->dev_power_always_on)
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200494 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100495 ethosu_save_pmu_counters(&drv->dev);
Anton Moberg0a614292021-03-24 14:08:22 +0100496 set_clock_and_power_request(drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200497 }
Kristofer Jonsson2b201c32020-09-02 16:42:43 +0200498
Bhavik Patele645fed2020-06-12 14:46:47 +0200499 return return_code;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200500}
501
Anton Moberg61da4d32020-12-22 16:00:31 +0100502void ethosu_abort_v2(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200503{
Anton Moberg61da4d32020-12-22 16:00:31 +0100504 drv->abort_inference = true;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200505}
506
Anton Moberg61da4d32020-12-22 16:00:31 +0100507void ethosu_set_power_mode_v2(struct ethosu_driver *drv, bool always_on)
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100508{
Anton Moberg61da4d32020-12-22 16:00:31 +0100509 drv->dev_power_always_on = always_on;
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100510
511 if (always_on)
512 {
Anton Moberg61da4d32020-12-22 16:00:31 +0100513 npu_axi_init(drv);
514 }
515}
516
517int ethosu_register_driver(struct ethosu_driver *drv)
518{
519 // Safeguard check for if driver is already registered
520 struct ethosu_driver *cur = registered_drivers;
521 while (cur != NULL)
522 {
523 if (cur == drv)
524 {
525 LOG_ERR("%s: NPU driver at address %p is already registered.\n", __FUNCTION__, drv);
526 return -1;
527 }
528 cur = cur->next;
529 }
530
531 drv->next = registered_drivers;
532 // Designate new registered driver HEAD
533 registered_drivers = drv;
534
535 LOG_INFO("%s: New NPU driver at address %p is registered.\n", __FUNCTION__, drv);
Anton Moberg61da4d32020-12-22 16:00:31 +0100536 return 0;
537}
538
539int ethosu_deregister_driver(struct ethosu_driver *drv)
540{
541 struct ethosu_driver *cur = registered_drivers;
542 struct ethosu_driver **prev = &registered_drivers;
543
544 while (cur != NULL)
545 {
546 if (cur == drv)
547 {
548 *prev = cur->next;
549 LOG_INFO("%s: NPU driver at address %p is deregistered.\n", __FUNCTION__, drv);
550 return 0;
551 }
552
553 prev = &cur->next;
554 cur = cur->next;
555 }
556
557 LOG_ERR("%s: NPU driver at address %p does not match a registered driver and therefore may not be deregistered.\n",
558 __FUNCTION__,
559 drv);
Anton Mobergdf386e02021-02-02 11:26:48 +0100560
Anton Moberg61da4d32020-12-22 16:00:31 +0100561 return -1;
562}
563
564struct ethosu_driver *ethosu_reserve_driver(void)
565{
Anton Mobergdf386e02021-02-02 11:26:48 +0100566 struct ethosu_driver *drv = NULL;
567
568 do
569 {
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100570 ethosu_mutex_lock(ethosu_mutex);
Anton Mobergdf386e02021-02-02 11:26:48 +0100571 drv = ethosu_find_and_reserve_driver();
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100572 ethosu_mutex_unlock(ethosu_mutex);
Anton Mobergdf386e02021-02-02 11:26:48 +0100573
574 if (drv != NULL)
575 {
576 break;
577 }
578
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100579 LOG_INFO("%s - Waiting for driver \n", __FUNCTION__);
580 ethosu_semaphore_take(ethosu_semaphore);
Anton Mobergdf386e02021-02-02 11:26:48 +0100581
582 } while (1);
583
584 return drv;
585}
586
587static struct ethosu_driver *ethosu_find_and_reserve_driver(void)
588{
Anton Moberg61da4d32020-12-22 16:00:31 +0100589 struct ethosu_driver *drv = registered_drivers;
590
591 while (drv != NULL)
592 {
593 if (!drv->reserved)
594 {
595 drv->reserved = true;
596 LOG_INFO("%s - Driver %p reserved.\n", __FUNCTION__, drv);
597 return drv;
598 }
599 drv = drv->next;
600 }
601
602 LOG_INFO("%s: No available drivers.\n", __FUNCTION__, drv);
603
604 return NULL;
605}
606
607void ethosu_release_driver(struct ethosu_driver *drv)
608{
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100609 ethosu_mutex_lock(ethosu_mutex);
Anton Moberg61da4d32020-12-22 16:00:31 +0100610 if (drv != NULL && drv->reserved)
611 {
612 drv->reserved = false;
613 LOG_INFO("%s - Driver %p released\n", __FUNCTION__, drv);
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100614 ethosu_semaphore_give(ethosu_semaphore);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100615 }
Anton Mobergdfed5fd2021-03-11 14:41:11 +0100616 ethosu_mutex_unlock(ethosu_mutex);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100617}
618
619static int ethosu_soft_reset_and_restore(struct ethosu_driver *drv)
620{
621
622 if (ETHOSU_SUCCESS != ethosu_soft_reset(&drv->dev))
623 {
624 return -1;
625 }
626
Anton Moberg0a614292021-03-24 14:08:22 +0100627 set_clock_and_power_request(drv, ETHOSU_INFERENCE_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_DISABLE);
Anton Moberg8d65b6f2020-12-21 09:37:18 +0100628
629 npu_axi_init(drv);
630 ethosu_restore_pmu_config(&drv->dev);
631
632 return 0;
633}
634
Anton Moberg0a614292021-03-24 14:08:22 +0100635enum ethosu_error_codes set_clock_and_power_request(struct ethosu_driver *drv,
636 enum ethosu_request_clients client,
637 enum ethosu_clock_q_request clock_request,
638 enum ethosu_power_q_request power_request)
639{
640 // Set clock request bit for client
641 if (clock_request == ETHOSU_CLOCK_Q_DISABLE)
642 {
643 drv->clock_request |= (1 << client);
644 }
645 else
646 {
647 drv->clock_request &= ~(1 << client);
648 }
649 // Get current clock request (ENABLE if both PMU and INFERENCE asks for clock request, else DISABLE)
650 clock_request = drv->clock_request == 0 ? ETHOSU_CLOCK_Q_ENABLE : ETHOSU_CLOCK_Q_DISABLE;
651
652 // Set power request bit for client
Anton Moberg35b5d0e2021-04-13 13:32:17 +0200653 if (power_request == ETHOSU_POWER_Q_DISABLE)
Anton Moberg0a614292021-03-24 14:08:22 +0100654 {
655 drv->power_request |= (1 << client);
656 }
657 else
658 {
659 drv->power_request &= ~(1 << client);
660 }
661 // Get current power request (ENABLE if both PMU and INFERENCE asks for power request, else DISABLE)
662 power_request = drv->power_request == 0 ? ETHOSU_POWER_Q_ENABLE : ETHOSU_POWER_Q_DISABLE;
Anton Moberg35b5d0e2021-04-13 13:32:17 +0200663
Anton Moberg0a614292021-03-24 14:08:22 +0100664 // Set clock and power
665 enum ethosu_error_codes ret = ethosu_set_clock_and_power(&drv->dev, clock_request, power_request);
666
667 return ret;
668}
669
Bhavik Pateldae5be02020-06-18 15:25:15 +0200670static int handle_optimizer_config(struct ethosu_driver *drv, struct opt_cfg_s *opt_cfg_p)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200671{
672 struct ethosu_config cfg;
673 struct ethosu_id id;
674 int return_code = 0;
675
676 LOG_INFO("handle_optimizer_config:\n");
677 LOG_INFO("Optimizer release nbr: %d patch: %d\n", opt_cfg_p->da_data.rel_nbr, opt_cfg_p->da_data.patch_nbr);
Anton Mobergb8bcf132021-03-29 10:02:25 +0200678 LOG_INFO("Optimizer config cmd_stream_version: %d macs_per_cc: %d shram_size: %d custom_dma: %d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200679 opt_cfg_p->cmd_stream_version,
680 opt_cfg_p->macs_per_cc,
Anton Mobergb8bcf132021-03-29 10:02:25 +0200681 opt_cfg_p->shram_size,
682 opt_cfg_p->custom_dma);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200683 LOG_INFO("Optimizer config Ethos-U version: %d.%d.%d\n",
684 opt_cfg_p->arch_major_rev,
685 opt_cfg_p->arch_minor_rev,
686 opt_cfg_p->arch_patch_rev);
687
Bhavik Pateldae5be02020-06-18 15:25:15 +0200688 (void)ethosu_get_config(&drv->dev, &cfg);
689 (void)ethosu_get_id(&drv->dev, &id);
Anton Mobergb8bcf132021-03-29 10:02:25 +0200690 LOG_INFO("Ethos-U config cmd_stream_version: %" PRIu32 " macs_per_cc: %" PRIu32 " shram_size: %" PRIu32
691 " custom_dma: %" PRIu32 "\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200692 cfg.cmd_stream_version,
693 cfg.macs_per_cc,
Anton Mobergb8bcf132021-03-29 10:02:25 +0200694 cfg.shram_size,
695 cfg.custom_dma);
Per Åstrand14ccfee2020-09-25 10:40:20 +0200696 LOG_INFO("Ethos-U version: %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
697 id.arch_major_rev,
698 id.arch_minor_rev,
699 id.arch_patch_rev);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200700
701 if ((cfg.macs_per_cc != opt_cfg_p->macs_per_cc) || (cfg.shram_size != opt_cfg_p->shram_size) ||
Anton Mobergb8bcf132021-03-29 10:02:25 +0200702 (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version) || (!cfg.custom_dma && opt_cfg_p->custom_dma))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200703 {
704 if (cfg.macs_per_cc != opt_cfg_p->macs_per_cc)
705 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200706 LOG_ERR("NPU config mismatch: npu.macs_per_cc=%" PRIu32 " optimizer.macs_per_cc=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200707 cfg.macs_per_cc,
708 opt_cfg_p->macs_per_cc);
709 }
710 if (cfg.shram_size != opt_cfg_p->shram_size)
711 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200712 LOG_ERR("NPU config mismatch: npu.shram_size=%" PRIu32 " optimizer.shram_size=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200713 cfg.shram_size,
714 opt_cfg_p->shram_size);
715 }
716 if (cfg.cmd_stream_version != opt_cfg_p->cmd_stream_version)
717 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200718 LOG_ERR("NPU config mismatch: npu.cmd_stream_version=%" PRIu32 " optimizer.cmd_stream_version=%d\n",
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200719 cfg.cmd_stream_version,
720 opt_cfg_p->cmd_stream_version);
721 }
Anton Mobergb8bcf132021-03-29 10:02:25 +0200722 if (!cfg.custom_dma && opt_cfg_p->custom_dma)
723 {
724 LOG_ERR("NPU config mismatch: npu.custom_dma=%" PRIu32 " optimize.custom_dma=%d\n",
725 cfg.custom_dma,
726 opt_cfg_p->custom_dma);
727 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200728 return_code = -1;
729 }
730
Douglas Troha91e0be52021-01-18 13:57:38 +0100731 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 +0200732 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200733 LOG_ERR("NPU arch mismatch: npu.arch=%" PRIu32 ".%" PRIu32 ".%" PRIu32 " optimizer.arch=%d.%d.%d\n",
Bhavik Patel790ef362020-06-03 10:05:28 +0200734 id.arch_major_rev,
735 id.arch_minor_rev,
736 id.arch_patch_rev,
737 opt_cfg_p->arch_major_rev,
738 opt_cfg_p->arch_minor_rev,
739 opt_cfg_p->arch_patch_rev);
740 return_code = -1;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200741 }
742
743#if !defined(LOG_ENABLED)
744 UNUSED(opt_cfg_p);
745#endif
746 return return_code;
747}
748
Bhavik Pateldae5be02020-06-18 15:25:15 +0200749static void npu_axi_init(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200750{
Bhavik Pateldae5be02020-06-18 15:25:15 +0200751 ethosu_set_qconfig(&drv->dev, NPU_QCONFIG);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200752
Bhavik Pateldae5be02020-06-18 15:25:15 +0200753 ethosu_set_regioncfg(&drv->dev, 0, NPU_REGIONCFG_0);
754 ethosu_set_regioncfg(&drv->dev, 1, NPU_REGIONCFG_1);
755 ethosu_set_regioncfg(&drv->dev, 2, NPU_REGIONCFG_2);
756 ethosu_set_regioncfg(&drv->dev, 3, NPU_REGIONCFG_3);
757 ethosu_set_regioncfg(&drv->dev, 4, NPU_REGIONCFG_4);
758 ethosu_set_regioncfg(&drv->dev, 5, NPU_REGIONCFG_5);
759 ethosu_set_regioncfg(&drv->dev, 6, NPU_REGIONCFG_6);
760 ethosu_set_regioncfg(&drv->dev, 7, NPU_REGIONCFG_7);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200761
Bhavik Pateldae5be02020-06-18 15:25:15 +0200762 (void)ethosu_set_axi_limit0(&drv->dev,
763 AXI_LIMIT0_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200764 AXI_LIMIT0_MEM_TYPE,
765 AXI_LIMIT0_MAX_OUTSTANDING_READS,
766 AXI_LIMIT0_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200767 (void)ethosu_set_axi_limit1(&drv->dev,
768 AXI_LIMIT1_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200769 AXI_LIMIT1_MEM_TYPE,
770 AXI_LIMIT1_MAX_OUTSTANDING_READS,
771 AXI_LIMIT1_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200772 (void)ethosu_set_axi_limit2(&drv->dev,
773 AXI_LIMIT2_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200774 AXI_LIMIT2_MEM_TYPE,
775 AXI_LIMIT2_MAX_OUTSTANDING_READS,
776 AXI_LIMIT2_MAX_OUTSTANDING_WRITES);
Bhavik Pateldae5be02020-06-18 15:25:15 +0200777 (void)ethosu_set_axi_limit3(&drv->dev,
778 AXI_LIMIT3_MAX_BEATS_BYTES,
Bhavik Patel790ef362020-06-03 10:05:28 +0200779 AXI_LIMIT3_MEM_TYPE,
780 AXI_LIMIT3_MAX_OUTSTANDING_READS,
781 AXI_LIMIT3_MAX_OUTSTANDING_WRITES);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200782}
783
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200784/* Default implementation to flush the data cache. Override if available on the targeted device.
785 * Passing NULL as p argument expects the whole cache to be flushed.
786 */
787void __attribute__((weak)) ethosu_flush_dcache(uint32_t *p, size_t bytes)
788{
789 (void)p;
790 (void)bytes;
791}
792
793/* Default implementation to invalidate the data cache. Override if available on the targeted device.
794 * Passing NULL as p argument expects the whole cache to be flushed.
795 */
796void __attribute__((weak)) ethosu_invalidate_dcache(uint32_t *p, size_t bytes)
797{
798 (void)p;
799 (void)bytes;
800}
801
Bhavik Pateldae5be02020-06-18 15:25:15 +0200802static int handle_command_stream(struct ethosu_driver *drv,
803 const uint8_t *cmd_stream,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200804 const int cms_length,
805 const uint64_t *base_addr,
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200806 const size_t *base_addr_size,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200807 const int num_base_addr)
808{
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100809 uint32_t qread = 0;
810 uint32_t cms_bytes = cms_length * BYTES_IN_32_BITS;
811 ptrdiff_t cmd_stream_ptr = (ptrdiff_t)cmd_stream;
812
Kristofer Jonsson125429a2020-08-20 16:52:23 +0200813 LOG_INFO("handle_command_stream: cmd_stream=%p, cms_length %d\n", cmd_stream, cms_length);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200814
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200815 if (0 != ((ptrdiff_t)cmd_stream & MASK_16_BYTE_ALIGN))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200816 {
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200817 LOG_ERR("Error: Command stream addr %p not aligned to 16 bytes\n", cmd_stream);
818 return -1;
819 }
820
821 bool base_addr_invalid = false;
822 for (int i = 0; i < num_base_addr; i++)
823 {
824 if (0 != (base_addr[i] & MASK_16_BYTE_ALIGN))
825 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200826 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 +0200827 base_addr_invalid = true;
828 }
829 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100830
Bhavik Patelbf7ae632020-06-11 21:00:16 +0200831 if (base_addr_invalid)
832 {
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200833 return -1;
834 }
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100835
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200836 /* Flush the cache if available on our CPU.
837 * The upcasting to uin32_t* is ok since the pointer never is dereferenced.
838 * The base_addr_size is null if invoking from prior to invoke_V2, in that case
839 * the whole cache is being flushed.
840 */
841
842 if (base_addr_size != NULL)
843 {
Kristofer Jonssonc6e7a1f2020-11-24 09:20:14 +0100844 ethosu_flush_dcache((uint32_t *)cmd_stream_ptr, cms_bytes);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200845 for (int i = 0; i < num_base_addr; i++)
846 {
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100847 ethosu_flush_dcache((uint32_t *)(uintptr_t)base_addr[i], base_addr_size[i]);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200848 }
849 }
850 else
851 {
852 ethosu_flush_dcache(NULL, 0);
853 }
854
Bhavik Pateldae5be02020-06-18 15:25:15 +0200855 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 +0200856 {
857 return -1;
858 }
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200859
Bhavik Pateldae5be02020-06-18 15:25:15 +0200860 wait_for_irq(drv);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200861
Bhavik Patel5f8dad12020-09-30 09:06:52 +0200862 if (drv->status_error)
863 {
864 return -1;
865 }
866
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200867 if (base_addr_size != NULL)
868 {
869 for (int i = 0; i < num_base_addr; i++)
870 {
Bhavik Patel033bb1b2020-12-17 15:33:33 +0100871 ethosu_invalidate_dcache((uint32_t *)(uintptr_t)base_addr[i], base_addr_size[i]);
Per Åstrand3c8afcc2020-10-20 10:29:59 +0200872 }
873 }
874 else
875 {
876 ethosu_invalidate_dcache(NULL, 0);
877 }
878
Bhavik Pateldae5be02020-06-18 15:25:15 +0200879 (void)ethosu_get_qread(&drv->dev, &qread);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200880 if (qread != cms_bytes)
881 {
Kristofer Jonsson4dc73dc2020-10-16 12:33:47 +0200882 LOG_WARN(
Per Åstrand14ccfee2020-09-25 10:40:20 +0200883 "Failure: IRQ received but qread (%" PRIu32 ") not at end of stream (%" PRIu32 ").\n", qread, cms_bytes);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200884 return -1;
885 }
886
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200887 return 0;
888}
889
Bhavik Pateldae5be02020-06-18 15:25:15 +0200890static int read_apb_reg(struct ethosu_driver *drv, uint16_t da_data)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200891{
892 uint32_t *reg_p;
893 uint32_t start_address = (uint32_t)(da_data & APB_START_ADDR_MASK);
894 uint16_t num_reg = (da_data >> APB_NUM_REG_BIT_SHIFT) + 1;
895
896 reg_p = (uint32_t *)malloc(num_reg * sizeof(uint32_t));
897 if (reg_p == NULL)
898 {
899 LOG_INFO("read_apb_reg, Error! memory not allocated.");
900 return -1;
901 }
902
Bhavik Pateldae5be02020-06-18 15:25:15 +0200903 if (ETHOSU_SUCCESS == ethosu_read_apb_reg(&drv->dev, start_address, num_reg, reg_p))
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200904 {
905 for (int i = 0; i < num_reg; i++)
906 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200907 LOG_INFO(
908 "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 +0200909 }
910 }
911 else
912 {
913 free(reg_p);
914 return -1;
915 }
916
917 free(reg_p);
918 return 0;
919}
920
Bhavik Pateldae5be02020-06-18 15:25:15 +0200921static int dump_shram(struct ethosu_driver *drv)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200922{
923 struct ethosu_config cfg;
924 uint32_t *shram_p;
Bhavik Pateldae5be02020-06-18 15:25:15 +0200925 (void)ethosu_get_config(&drv->dev, &cfg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200926
Per Åstrand14ccfee2020-09-25 10:40:20 +0200927 LOG_INFO("dump_shram size = %" PRIu32 " KB\n", cfg.shram_size);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200928
929 shram_p = (uint32_t *)malloc(BYTES_1KB);
930 if (shram_p == NULL)
931 {
932 LOG_ERR("read_shram, Error! memory not allocated.");
933 return -1;
934 }
935
936 for (uint32_t i = 0; i < cfg.shram_size; i++)
937 {
Bhavik Pateldae5be02020-06-18 15:25:15 +0200938 ethosu_get_shram_data(&drv->dev, i, (uint32_t *)shram_p);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200939 // Output 1KB of SHRAM
Per Åstrand14ccfee2020-09-25 10:40:20 +0200940 LOG_INFO("***SHRAM SECTION %" PRIu32 "***\n", i);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200941 for (int j = 0; j < (BYTES_1KB / BYTES_IN_32_BITS); j++)
942 {
Per Åstrand14ccfee2020-09-25 10:40:20 +0200943 LOG_INFO("[0x%04" PRIx32 "] %" PRIx32 "\n", (i * 1024 + j * 4), shram_p[j]);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200944 }
945 }
946 free(shram_p);
947
948 return 0;
949}
950
951typedef struct
952{
953 int number;
954 const char *name;
955} name_lookup_t;
956
957static const name_lookup_t npu_reg_name_tbl[] = {
958 {0x200, "KERNEL_X"},
959 {0x204, "KERNEL_Y"},
960 {0x208, "KERNEL_W_M1"},
961 {0x20C, "KERNEL_H_M1"},
962 {0x210, "OFM_CBLK_WIDTH_M1"},
963 {0x214, "OFM_CBLK_HEIGHT_M1"},
964 {0x218, "OFM_CBLK_DEPTH_M1"},
965 {0x21c, "IFM_CBLK_DEPTH_M1"},
966 {0x220, "OFM_X"},
967 {0x224, "OFM_Y"},
968 {0x228, "OFM_Z"},
969 {0x22C, "IFM_Z"},
970 {0x230, "PAD_TOP"},
971 {0x234, "PAD_LEFT"},
972 {0x238, "IFM_CBLK_WIDTH"},
973 {0x23C, "IFM_CBLK_HEIGHT"},
974 {0x240, "DMA_IFM_SRC"},
975 {0x244, "DMA_IFM_SRC_HI"},
976 {0x248, "DMA_IFM_DST"},
977 {0x24c, "DMA_OFM_SRC"},
978 {0x250, "DMA_OFM_DST"},
979 {0x254, "DMA_OFM_DST_HI"},
980 {0x258, "DMA_WEIGHT_SRC"},
981 {0x25c, "DMA_WEIGHT_SRC_HI"},
982 {0x260, "DMA_CMD_SRC"},
983 {0x264, "DMA_CMD_SRC_HI"},
984 {0x268, "DMA_CMD_SIZE"},
985 {0x26c, "DMA_M2M_SRC"},
986 {0x270, "DMA_M2M_SRC_HI"},
987 {0x274, "DMA_M2M_DST"},
988 {0x278, "DMA_M2M_DST_HI"},
989 {0x27c, "CURRENT_QREAD"},
990 {0x280, "DMA_SCALE_SRC"},
991 {0x284, "DMA_SCALE_SRC_HI"},
992 {0x2BC, "CURRENT_CMD"},
993 {0x800, "IFM_PAD_TOP"},
994 {0x804, "IFM_PAD_LEFT"},
995 {0x808, "IFM_PAD_RIGHT"},
996 {0x80C, "IFM_PAD_BOTTOM"},
997 {0x810, "IFM_DEPTH_M1"},
998 {0x814, "IFM_PRECISION"},
999 {0x81C, "IFM_UPSCALE"},
1000 {0x824, "IFM_ZERO_POINT"},
1001 {0x828, "IFM_WIDTH0_M1"},
1002 {0x82C, "IFM_HEIGHT0_M1"},
1003 {0x830, "IFM_HEIGHT1_M1"},
1004 {0x834, "IFM_IB_END"},
1005 {0x83C, "IFM_REGION"},
1006 {0x844, "OFM_WIDTH_M1"},
1007 {0x848, "OFM_HEIGHT_M1"},
1008 {0x84C, "OFM_DEPTH_M1"},
1009 {0x850, "OFM_PRECISION"},
1010 {0x854, "OFM_BLK_WIDTH_M1"},
1011 {0x858, "OFM_BLK_HEIGHT_M1"},
1012 {0x85C, "OFM_BLK_DEPTH_M1"},
1013 {0x860, "OFM_ZERO_POINT"},
1014 {0x868, "OFM_WIDTH0_M1"},
1015 {0x86C, "OFM_HEIGHT0_M1"},
1016 {0x870, "OFM_HEIGHT1_M1"},
1017 {0x87C, "OFM_REGION"},
1018 {0x880, "KERNEL_WIDTH_M1"},
1019 {0x884, "KERNEL_HEIGHT_M1"},
1020 {0x888, "KERNEL_STRIDE"},
1021 {0x88C, "PARALLEL_MODE"},
1022 {0x890, "ACC_FORMAT"},
1023 {0x894, "ACTIVATION"},
1024 {0x898, "ACTIVATION_MIN"},
1025 {0x89C, "ACTIVATION_MAX"},
1026 {0x8A0, "WEIGHT_REGION"},
1027 {0x8A4, "SCALE_REGION"},
1028 {0x8B4, "AB_START"},
1029 {0x8BC, "BLOCKDEP"},
1030 {0x8C0, "DMA0_SRC_REGION"},
1031 {0x8C4, "DMA0_DST_REGION"},
1032 {0x8C8, "DMA0_SIZE0"},
1033 {0x8CC, "DMA0_SIZE1"},
1034 {0x900, "IFM2_BROADCAST"},
1035 {0x904, "IFM2_SCALAR"},
1036 {0x924, "IFM2_ZERO_POINT"},
1037 {0x928, "IFM2_WIDTH0_M1"},
1038 {0x92C, "IFM2_HEIGHT0_M1"},
1039 {0x930, "IFM2_HEIGHT1_M1"},
1040 {0x934, "IFM2_IB_START"},
1041 {0x93C, "IFM2_REGION"},
1042 {0xA00, "IFM_BASE0"},
1043 {0xA04, "IFM_BASE0_HI"},
1044 {0xA08, "IFM_BASE1"},
1045 {0xA0C, "IFM_BASE1_HI"},
1046 {0xA10, "IFM_BASE2"},
1047 {0xA14, "IFM_BASE2_HI"},
1048 {0xA18, "IFM_BASE3"},
1049 {0xA1C, "IFM_BASE3_HI"},
1050 {0xA20, "IFM_STRIDE_X"},
1051 {0xA24, "IFM_STRIDE_X_HI"},
1052 {0xA28, "IFM_STRIDE_Y"},
1053 {0xA2C, "IFM_STRIDE_Y_HI"},
1054 {0xA30, "IFM_STRIDE_C"},
1055 {0xA34, "IFM_STRIDE_C_HI"},
1056 {0xA40, "OFM_BASE0"},
1057 {0xA44, "OFM_BASE0_HI"},
1058 {0xA48, "OFM_BASE1"},
1059 {0xA4C, "OFM_BASE1_HI"},
1060 {0xA50, "OFM_BASE2"},
1061 {0xA54, "OFM_BASE2_HI"},
1062 {0xA58, "OFM_BASE3"},
1063 {0xA5C, "OFM_BASE3_HI"},
1064 {0xA60, "OFM_STRIDE_X"},
1065 {0xA64, "OFM_STRIDE_X_HI"},
1066 {0xA68, "OFM_STRIDE_Y"},
1067 {0xA6C, "OFM_STRIDE_Y_HI"},
1068 {0xA70, "OFM_STRIDE_C"},
1069 {0xA74, "OFM_STRIDE_C_HI"},
1070 {0xA80, "WEIGHT_BASE"},
1071 {0xA84, "WEIGHT_BASE_HI"},
1072 {0xA88, "WEIGHT_LENGTH"},
1073 {0xA8C, "WEIGHT_LENGTH_HI"},
1074 {0xA90, "SCALE_BASE"},
1075 {0xA94, "SCALE_BASE_HI"},
1076 {0xA98, "SCALE_LENGTH"},
1077 {0xAA0, "OFM_SCALE"},
1078 {0xAA4, "OFM_SCALE_SHIFT"},
1079 {0xAA8, "OPA_SCALE "},
1080 {0xAB0, "OPB_SCALE"},
1081 {0xAC0, "DMA0_SRC"},
1082 {0xAC4, "DMA0_SRC_HI"},
1083 {0xAC8, "DMA0_DST"},
1084 {0xACC, "DMA0_DST_HI"},
1085 {0xAD0, "DMA0_LEN"},
1086 {0xAD4, "DMA0_LEN_HI"},
1087 {0xAD8, "DMA0_SKIP0"},
1088 {0xADC, "DMA0_SKIP0_HI"},
1089 {0xAE0, "DMA0_SKIP1"},
1090 {0xAE4, "DMA0_SKIP1_HI"},
1091 {0xB00, "IFM2_BASE0"},
1092 {0xB04, "IFM2_BASE0_HI"},
1093 {0xB08, "IFM2_BASE1"},
1094 {0xB0C, "IFM2_BASE1_HI"},
1095 {0xB10, "IFM2_BASE2"},
1096 {0xB14, "IFM2_BASE2_HI"},
1097 {0xB18, "IFM2_BASE3"},
1098 {0xB1C, "IFM2_BASE3_HI"},
1099 {0xB20, "IFM2_STRIDE_X"},
1100 {0xB24, "IFM2_STRIDE_X_HI"},
1101 {0xB28, "IFM2_STRIDE_Y"},
1102 {0xB2C, "IFM2_STRIDE_Y_HI"},
1103 {0xB30, "IFM2_STRIDE_C"},
1104 {0xB34, "IFM2_STRIDE_C_HI"},
1105 {0xB40, "WEIGHT1_BASE"},
1106 {0xB44, "WEIGHT1_BASE_HI"},
1107 {0xB48, "WEIGHT1_LENGTH"},
1108 {0xB4C, "WEIGHT1_LENGTH_HI"},
1109 {0xB50, "SCALE1_BASE"},
1110 {0xB54, "SCALE1_BASE_HI"},
1111 {0xB58, "SCALE1_LENGTH"},
1112};
1113
1114static const char *lookup_name(const name_lookup_t *lookup_table, int lookup_table_count, int find)
1115{
1116 int n;
1117 for (n = 0; n < lookup_table_count; n++)
1118 {
1119 if (lookup_table[n].number == find)
1120 {
1121 return lookup_table[n].name;
1122 }
1123 }
1124 // Not found
1125 return 0;
1126}
1127
Bhavik Pateldae5be02020-06-18 15:25:15 +02001128static void dump_npu_register(struct ethosu_driver *drv, int npu_reg, int npu_reg_end)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001129{
1130 unsigned int reg_val;
1131 const char *reg_name;
1132 int npu_reg_name_tbl_count = sizeof(npu_reg_name_tbl) / sizeof(npu_reg_name_tbl[0]);
1133
1134 LOG_INFO("dump_register %X - %X\n", npu_reg, npu_reg_end);
1135 for (; npu_reg <= npu_reg_end; npu_reg += sizeof(int))
1136 {
Bhavik Pateldae5be02020-06-18 15:25:15 +02001137 reg_val = ethosu_read_reg(&drv->dev, npu_reg);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001138 reg_name = lookup_name(npu_reg_name_tbl, npu_reg_name_tbl_count, npu_reg);
1139 LOG_INFO("[0x%.4X] 0x%.8X\t%s\n", npu_reg, reg_val, (reg_name) ? reg_name : "");
1140 }
1141}
1142
1143static const name_lookup_t cmd0_name_tbl[] = {
1144 {0x000, "NPU_OP_STOP"},
1145 {0x001, "NPU_OP_IRQ"},
1146 {0x002, "NPU_OP_CONV"},
1147 {0x003, "NPU_OP_DEPTHWISE"},
1148 {0x004, "NPU_OP_VECTOR_PROD"},
1149 {0x005, "NPU_OP_POOL"},
1150 {0x006, "NPU_OP_ELEMENTWISE"},
1151 {0x010, "NPU_OP_DMA_START"},
1152 {0x011, "NPU_OP_DMA_WAIT"},
1153 {0x012, "NPU_OP_KERNEL_WAIT"},
1154 {0x100, "NPU_SET_IFM_PAD_TOP"},
1155 {0x101, "NPU_SET_IFM_PAD_LEFT"},
1156 {0x102, "NPU_SET_IFM_PAD_RIGHT"},
1157 {0x103, "NPU_SET_IFM_PAD_BOTTOM"},
1158 {0x104, "NPU_SET_IFM_DEPTH_M1"},
1159 {0x105, "NPU_SET_IFM_PRECISION"},
1160 {0x107, "NPU_SET_IFM_UPSCALE"},
1161 {0x109, "NPU_SET_IFM_ZERO_POINT"},
1162 {0x10A, "NPU_SET_IFM_WIDTH0_M1"},
1163 {0x10B, "NPU_SET_IFM_HEIGHT0_M1"},
1164 {0x10C, "NPU_SET_IFM_HEIGHT1_M1"},
1165 {0x10D, "NPU_SET_IFM_IB_END"},
1166 {0x10F, "NPU_SET_IFM_REGION"},
1167 {0x110, "NPU_SET_OFM_BATCH_SIZE_M1"},
1168 {0x111, "NPU_SET_OFM_WIDTH_M1"},
1169 {0x112, "NPU_SET_OFM_HEIGHT_M1"},
1170 {0x113, "NPU_SET_OFM_DEPTH_M1"},
1171 {0x114, "NPU_SET_OFM_PRECISION"},
1172 {0x115, "NPU_SET_OFM_BLK_WIDTH_M1"},
1173 {0x116, "NPU_SET_OFM_BLK_HEIGHT_M1"},
1174 {0x117, "NPU_SET_OFM_BLK_DEPTH_M1"},
1175 {0x118, "NPU_SET_OFM_ZERO_POINT"},
1176 {0x11A, "NPU_SET_OFM_WIDTH0_M1"},
1177 {0x11B, "NPU_SET_OFM_HEIGHT0_M1"},
1178 {0x11C, "NPU_SET_OFM_HEIGHT1_M1"},
1179 {0x11F, "NPU_SET_OFM_REGION"},
1180 {0x120, "NPU_SET_KERNEL_WIDTH_M1"},
1181 {0x121, "NPU_SET_KERNEL_HEIGHT_M1"},
1182 {0x122, "NPU_SET_KERNEL_STRIDE"},
1183 {0x124, "NPU_SET_ACC_FORMAT"},
1184 {0x125, "NPU_SET_ACTIVATION"},
1185 {0x126, "NPU_SET_ACTIVATION_MIN"},
1186 {0x127, "NPU_SET_ACTIVATION_MAX"},
1187 {0x128, "NPU_SET_WEIGHT_REGION"},
1188 {0x129, "NPU_SET_SCALE_REGION"},
1189 {0x12D, "NPU_SET_AB_START"},
1190 {0x12F, "NPU_SET_BLOCKDEP"},
1191 {0x130, "NPU_SET_DMA0_SRC_REGION"},
1192 {0x131, "NPU_SET_DMA0_DST_REGION"},
1193 {0x180, "NPU_SET_IFM2_BROADCAST"},
1194 {0x181, "NPU_SET_IFM2_SCALAR"},
1195 {0x185, "NPU_SET_IFM2_PRECISION"},
1196 {0x189, "NPU_SET_IFM2_ZERO_POINT"},
1197 {0x18A, "NPU_SET_IFM2_WIDTH0_M1"},
1198 {0x18B, "NPU_SET_IFM2_HEIGHT0_M1"},
1199 {0x18C, "NPU_SET_IFM2_HEIGHT1_M1"},
1200 {0x18D, "NPU_SET_IFM2_IB_START"},
1201 {0x18F, "NPU_SET_IFM2_REGION"},
1202};
1203
1204static const name_lookup_t cmd1_name_tbl[] = {
1205 {0x000, "NPU_SET_IFM_BASE0"}, {0x001, "NPU_SET_IFM_BASE1"}, {0x002, "NPU_SET_IFM_BASE2"},
1206 {0x003, "NPU_SET_IFM_BASE3"}, {0x004, "NPU_SET_IFM_STRIDE_X"}, {0x005, "NPU_SET_IFM_STRIDE_Y"},
1207 {0x006, "NPU_SET_IFM_STRIDE_C"}, {0x007, "NPU_SET_IFM_STRIDE_N"}, {0x010, "NPU_SET_OFM_BASE0"},
1208 {0x011, "NPU_SET_OFM_BASE1"}, {0x012, "NPU_SET_OFM_BASE2"}, {0x013, "NPU_SET_OFM_BASE3"},
1209 {0x014, "NPU_SET_OFM_STRIDE_X"}, {0x015, "NPU_SET_OFM_STRIDE_Y"}, {0x016, "NPU_SET_OFM_STRIDE_C"},
1210 {0x017, "NPU_SET_OFM_STRIDE_N"}, {0x020, "NPU_SET_WEIGHT_BASE"}, {0x021, "NPU_SET_WEIGHT_LENGTH"},
1211 {0x022, "NPU_SET_SCALE_BASE"}, {0x023, "NPU_SET_SCALE_LENGTH"}, {0x024, "NPU_SET_OFM_SCALE"},
1212 {0x025, "NPU_SET_OPA_SCALE"}, {0x026, "NPU_SET_OPB_SCALE"}, {0x030, "NPU_SET_DMA0_SRC"},
1213 {0x031, "NPU_SET_DMA0_DST"}, {0x032, "NPU_SET_DMA0_LEN"}, {0x080, "NPU_SET_IFM2_BASE0"},
1214 {0x081, "NPU_SET_IFM2_BASE1"}, {0x082, "NPU_SET_IFM2_BASE2"}, {0x083, "NPU_SET_IFM2_BASE3"},
1215 {0x084, "NPU_SET_IFM2_STRIDE_X"}, {0x085, "NPU_SET_IFM2_STRIDE_Y"}, {0x086, "NPU_SET_IFM2_STRIDE_C"},
1216};
1217
1218static void dump_command_stream(const uint32_t *cmd_stream, const int cms_length, int qread)
1219{
1220 int n;
1221 int offset;
1222 uint32_t cmd_val;
1223 const uint8_t *cmd_ptr;
1224 const char *cmd_name;
1225 int cmd0_name_tbl_count = sizeof(cmd0_name_tbl) / sizeof(cmd0_name_tbl[0]);
1226 int cmd1_name_tbl_count = sizeof(cmd1_name_tbl) / sizeof(cmd1_name_tbl[0]);
1227
1228 LOG_INFO("dump_command_stream cmd_stream = 0x%8p cms_length = %d\n", cmd_stream, cms_length);
1229 for (n = 0; n < cms_length; n++)
1230 {
1231 // Offset
1232 offset = n * sizeof(int);
1233 LOG_INFO("[%.4d] ", offset);
1234 // Command
1235 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1236 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1237 // Command name and payload
1238 if (cmd_stream[n] & 0x4000)
1239 {
1240 cmd_name = lookup_name(cmd1_name_tbl, cmd1_name_tbl_count, cmd_stream[n] & 0x3FF);
1241 n++;
1242 cmd_val = cmd_stream[n];
1243 cmd_ptr = (const uint8_t *)&cmd_stream[n];
1244 LOG_INFO("0x%.2X 0x%.2X 0x%.2X 0x%.2X ", cmd_ptr[0], cmd_ptr[1], cmd_ptr[2], cmd_ptr[3]);
1245 }
1246 else
1247 {
1248 cmd_val = cmd_stream[n] >> 16;
1249 cmd_name = lookup_name(cmd0_name_tbl, cmd0_name_tbl_count, cmd_stream[n] & 0x3FF);
1250 }
1251 if (cmd_name)
1252 {
Per Åstrand14ccfee2020-09-25 10:40:20 +02001253 LOG_INFO("\t%s 0x%.8" PRIX32, cmd_name, cmd_val);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001254 }
1255 if (offset == qread)
1256 {
1257 LOG_INFO(" <<== QREAD\n");
1258 }
1259 else
1260 {
1261 LOG_INFO("\n");
1262 }
1263 }
1264}