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